summaryrefslogtreecommitdiff
path: root/registry/tools/checksingleton.cxx
blob: 18c96139659c7140ddca02bbd4155407f9b1e1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include "registry/registry.hxx"
#include "registry/reflread.hxx"
#include "fileurl.hxx"
#include "options.hxx"

#include "rtl/ustring.hxx"
#include "osl/diagnose.h"

#include <stdio.h>
#include <string.h>

#include <vector>
#include <string>

using namespace rtl;
using namespace registry::tools;

#define U2S( s ) \
    OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
#define S2U( s ) \
    OStringToOUString(s, RTL_TEXTENCODING_UTF8)

class Options_Impl : public Options
{
public:
    explicit Options_Impl(char const * program)
        : Options (program), m_bForceOutput(false)
        {}

    std::string const & getIndexReg() const
        { return m_indexRegName; }
    std::string const & getTypeReg() const
        { return m_typeRegName; }
    bool hasBase() const
        { return (!m_base.isEmpty()); }
    const OString & getBase() const
        { return m_base; }
    bool forceOutput() const
        { return m_bForceOutput; }

protected:
    virtual void printUsage_Impl() const;
    virtual bool initOptions_Impl (std::vector< std::string > & rArgs);

    std::string m_indexRegName;
    std::string m_typeRegName;
    OString     m_base;
    bool m_bForceOutput;
};

// virtual
void Options_Impl::printUsage_Impl() const
{
    std::string const & rProgName = getProgramName();
    fprintf(stderr,
            "Usage: %s -r<filename> -o<filename> [-options] | @<filename>\n", rProgName.c_str()
            );
    fprintf(stderr,
            "    -o<filename>  = filename specifies the name of the new singleton index registry.\n"
            "    -r<filename>  = filename specifies the name of the type registry.\n"
            "    @<filename>   = filename specifies a command file.\n"
            "Options:\n"
            "    -b<name>  = name specifies the name of a start key. The types will be searched\n"
            "                under this key in the type registry.\n"
            "    -f        = force the output of all found singletons.\n"
            "    -h|-?     = print this help message and exit.\n"
            );
    fprintf(stderr,
            "\n%s Version 1.0\n\n", rProgName.c_str()
            );
}

// virtual
bool Options_Impl::initOptions_Impl(std::vector< std::string > & rArgs)
{
    std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end();
    for (; first != last; ++first)
    {
        std::string option (*first);
        if ((*first)[0] != '-')
        {
            return badOption("invalid", option.c_str());
        }
        switch ((*first)[1])
        {
        case 'r':
        case 'R':
            {
                if (!((++first != last) && ((*first)[0] != '-')))
                {
                    return badOption("invalid", option.c_str());
                }
                m_typeRegName = *first;
                break;
            }
        case 'o':
        case 'O':
            {
                if (!((++first != last) && ((*first)[0] != '-')))
                {
                    return badOption("invalid", option.c_str());
                }
                m_indexRegName = (*first);
                break;
            }
        case 'b':
        case 'B':
            {
                if (!((++first != last) && ((*first)[0] != '-')))
                {
                    return badOption("invalid", option.c_str());
                }
                m_base = OString((*first).c_str(), (*first).size());
                break;
            }
        case 'f':
        case 'F':
            {
                if ((*first).size() > 2)
                {
                    return badOption("invalid", option.c_str());
                }
                m_bForceOutput = sal_True;
                break;
            }
        case 'h':
        case '?':
            {
                if ((*first).size() > 2)
                {
                    return badOption("invalid", option.c_str());
                }
                return printUsage();
                // break; // unreachable
            }
        default:
            return badOption("unknown", option.c_str());
            // break; // unreachable
        }
    }
    return true;
}

static sal_Bool checkSingletons(Options_Impl const & options, RegistryKey& singletonKey, RegistryKey& typeKey)
{
    RegValueType valueType = RG_VALUETYPE_NOT_DEFINED;
    sal_uInt32 size = 0;
    OUString tmpName;
    sal_Bool bRet = sal_False;

    RegError e = typeKey.getValueInfo(tmpName, &valueType, &size);
    if ((e != REG_VALUE_NOT_EXISTS) && (e != REG_INVALID_VALUE) && (valueType == RG_VALUETYPE_BINARY))
    {
        std::vector< sal_uInt8 > value(size);
        typeKey.getValue(tmpName, &value[0]); // @@@ broken api: write to buffer w/o buffer size.

        RegistryTypeReader reader(&value[0], value.size(), sal_False);
        if ( reader.isValid() && reader.getTypeClass() == RT_TYPE_SINGLETON )
        {
            RegistryKey entryKey;
            OUString    singletonName = reader.getTypeName().replace('/', '.');
            if ( singletonKey.createKey(singletonName, entryKey) )
            {
                fprintf(stderr, "%s: could not create SINGLETONS entry for \"%s\"\n",
                    options.getProgramName().c_str(), U2S( singletonName ));
            }
            else
            {
                bRet = sal_True;
                OUString value2 = reader.getSuperTypeName();

                if ( entryKey.setValue(tmpName, RG_VALUETYPE_UNICODE,
                                       (RegValue)value2.getStr(), sizeof(sal_Unicode)* (value2.getLength()+1)) )
                {
                    fprintf(stderr, "%s: could not create data entry for singleton \"%s\"\n",
                            options.getProgramName().c_str(), U2S( singletonName ));
                }

                if ( options.forceOutput() )
                {
                    fprintf(stderr, "%s: create SINGLETON entry for \"%s\" -> \"%s\"\n",
                            options.getProgramName().c_str(), U2S( singletonName ), U2S(value2));
                }
            }
        }
    }

       RegistryKeyArray subKeys;
    typeKey.openSubKeys(tmpName, subKeys);

    sal_uInt32 length = subKeys.getLength();
    for (sal_uInt32 i = 0; i < length; i++)
    {
        RegistryKey elementKey = subKeys.getElement(i);
        if ( checkSingletons(options, singletonKey, elementKey) )
        {
            bRet = sal_True;
        }
    }
    return bRet;
}

#if (defined UNX) || (defined __MINGW32__)
int main( int argc, char * argv[] )
#else
int _cdecl main( int argc, char * argv[] )
#endif
{
    std::vector< std::string > args;
    for (int i = 1; i < argc; i++)
    {
        int result = Options::checkArgument(args, argv[i], strlen(argv[i]));
        if (result != 0)
        {
            // failure.
            return (result);
        }
    }

    Options_Impl options(argv[0]);
    if (!options.initOptions(args))
    {
        options.printUsage();
        return (1);
    }

    OUString indexRegName( convertToFileUrl(options.getIndexReg().c_str(), options.getIndexReg().size()) );
    Registry indexReg;
    if ( indexReg.open(indexRegName, REG_READWRITE) )
    {
        if ( indexReg.create(indexRegName) )
        {
            fprintf(stderr, "%s: open registry \"%s\" failed\n",
                    options.getProgramName().c_str(), options.getIndexReg().c_str());
            return (2);
        }
    }

    OUString typeRegName( convertToFileUrl(options.getTypeReg().c_str(), options.getTypeReg().size()) );
    Registry typeReg;
    if ( typeReg.open(typeRegName, REG_READONLY) )
    {
        fprintf(stderr, "%s: open registry \"%s\" failed\n",
                options.getProgramName().c_str(), options.getTypeReg().c_str());
        return (3);
    }

    RegistryKey indexRoot;
    if ( indexReg.openRootKey(indexRoot) )
    {
        fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
                options.getProgramName().c_str(), options.getIndexReg().c_str());
        return (4);
    }

    RegistryKey typeRoot;
    if ( typeReg.openRootKey(typeRoot) )
    {
        fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
                options.getProgramName().c_str(), options.getTypeReg().c_str());
        return (5);
    }

    RegistryKey typeKey;
    if ( options.hasBase() )
    {
        if ( typeRoot.openKey(S2U(options.getBase()), typeKey) )
        {
            fprintf(stderr, "%s: open base key of registry \"%s\" failed\n",
                    options.getProgramName().c_str(), options.getTypeReg().c_str());
            return (6);
        }
    }
    else
    {
        typeKey = typeRoot;
    }

    RegistryKey singletonKey;
    if ( indexRoot.createKey(OUString("SINGLETONS"), singletonKey) )
    {
        fprintf(stderr, "%s: open/create SINGLETONS key of registry \"%s\" failed\n",
                options.getProgramName().c_str(), options.getIndexReg().c_str());
        return (7);
    }

    sal_Bool bSingletonsExist = checkSingletons(options, singletonKey, typeKey);

    indexRoot.releaseKey();
    typeRoot.releaseKey();
    typeKey.releaseKey();
    singletonKey.releaseKey();
    if ( indexReg.close() )
    {
        fprintf(stderr, "%s: closing registry \"%s\" failed\n",
                options.getProgramName().c_str(), options.getIndexReg().c_str());
        return (9);
    }
    if ( !bSingletonsExist )
    {
        if ( indexReg.destroy(OUString()) )
        {
            fprintf(stderr, "%s: destroy registry \"%s\" failed\n",
                    options.getProgramName().c_str(), options.getIndexReg().c_str());
            return (10);
        }
    }
    if ( typeReg.close() )
    {
        fprintf(stderr, "%s: closing registry \"%s\" failed\n",
                options.getProgramName().c_str(), options.getTypeReg().c_str());
        return (11);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */