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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
/* $Id: oiddb2c.cpp $ */
/** @file
* IPRT - OID text database to C converter.
*
* The output is used by asn1-dump.cpp.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/types.h>
#include <iprt/ctype.h>
#include <iprt/stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* Include the string table code.
*/
#define BLDPROG_STRTAB_MAX_STRLEN 48
#define BLDPROG_STRTAB_WITH_COMPRESSION
#define BLDPROG_STRTAB_PURE_ASCII
#define BLDPROG_STRTAB_WITH_CAMEL_WORDS
#include <iprt/bldprog-strtab-template.cpp.h>
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
#define OID2C_MAX_COMP_VALUE _1G
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* Raw OID tree node.
*
* This is what we produce while loading OID input files.
*/
typedef struct RAWOIDNODE
{
/** The component value. */
uint32_t uKey;
/** Number of children. */
uint32_t cChildren;
/** Pointer to the children pointers (sorted by key). */
struct RAWOIDNODE **papChildren;
/** Pointer to the parent. */
struct RAWOIDNODE *pParent;
/** The string table entry for this node. */
BLDPROGSTRING StrTabEntry;
/** The table index of the children. */
uint32_t idxChildren;
/** Set if we've got one or more children with large keys. */
bool fChildrenInBigTable;
} RAWOIDNODE;
/** Pointer to a raw OID node. */
typedef RAWOIDNODE *PRAWOIDNODE;
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** What to prefix errors with. */
static const char *g_pszProgName = "oiddb2c";
/** The OID tree. */
static PRAWOIDNODE g_pOidRoot = NULL;
/** Number of nodes in the OID tree. */
static uint32_t g_cOidNodes = 0;
/** Number of nodes in the OID tree that has strings (for the string table). */
static uint32_t g_cOidNodesWithStrings = 0;
/** Max number of children of a node in the OID tree. */
static uint32_t g_cMaxOidChildren = 0;
/** Number of nodes which key fits within 6-bits. */
static uint32_t g_cOidNodiesWith6bitKeys = 0;
static RTEXITCODE error(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
fprintf(stderr, "%s: error: ", g_pszProgName);
vfprintf(stderr, pszFormat, va);
va_end(va);
return RTEXITCODE_FAILURE;
}
static RTEXITCODE warning(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
fprintf(stderr, "%s: warning: ", g_pszProgName);
vfprintf(stderr, pszFormat, va);
va_end(va);
return RTEXITCODE_FAILURE;
}
static void writeDottedOidForNode(PRAWOIDNODE pCurNode, FILE *pOut)
{
if (pCurNode->pParent)
{
writeDottedOidForNode(pCurNode->pParent, pOut);
fprintf(pOut, ".%u", pCurNode->uKey);
}
else
fprintf(pOut, "%u", pCurNode->uKey);
}
static void writeOidTree(PRAWOIDNODE pCurNode, FILE *pOut, bool fBigTable, PBLDPROGSTRTAB pStrTab)
{
/*
* First we produce the entries for our children.
*/
if (pCurNode->fChildrenInBigTable == fBigTable)
{
for (unsigned i = 0; i < pCurNode->cChildren; i++)
{
PRAWOIDNODE pChild = pCurNode->papChildren[i];
fprintf(pOut,
fBigTable
? " { %7u, %2u, %u, %2u, %4u, %#06x }, /* "
: " { %2u, %2u, %u, %2u, %4u, %#06x }, /* "
,
pChild->uKey,
(unsigned)pChild->StrTabEntry.cchString,
pChild->fChildrenInBigTable,
pChild->cChildren,
pChild->idxChildren,
pChild->StrTabEntry.offStrTab);
writeDottedOidForNode(pChild, pOut);
if (pChild->StrTabEntry.pszString)
{
fputs(" = \"", pOut);
BldProgStrTab_PrintCStringLitteral(pStrTab, &pChild->StrTabEntry, pOut);
fputs("\" */\n", pOut);
}
else
fputs(" */\n", pOut);
}
}
/*
* Then we decend and let our children do the same.
*/
for (unsigned i = 0; i < pCurNode->cChildren; i++)
writeOidTree(pCurNode->papChildren[i], pOut, fBigTable, pStrTab);
}
static uint32_t prepareOidTreeForWriting(PRAWOIDNODE pCurNode, uint32_t idxCur, bool fBigTable)
{
if (pCurNode->fChildrenInBigTable == fBigTable)
{
pCurNode->idxChildren = pCurNode->cChildren ? idxCur : 0;
idxCur += pCurNode->cChildren;
}
for (unsigned i = 0; i < pCurNode->cChildren; i++)
idxCur = prepareOidTreeForWriting(pCurNode->papChildren[i], idxCur, fBigTable);
return idxCur;
}
static void addStringFromOidTree(PRAWOIDNODE pCurNode, PBLDPROGSTRTAB pStrTab)
{
/* Do self. */
if (pCurNode->StrTabEntry.pszString)
BldProgStrTab_AddString(pStrTab, &pCurNode->StrTabEntry);
/* Recurse into children. */
unsigned i = pCurNode->cChildren;
while (i-- > 0)
addStringFromOidTree(pCurNode->papChildren[i], pStrTab);
}
static bool isNiceAsciiString(const char *psz)
{
unsigned uch;
while ((uch = *psz) != '\0')
if ( !(uch & 0x80)
&& ( uch >= 0x20
|| uch == '\t') )
psz++;
else
return false;
return true;
}
static RTEXITCODE addOidToTree(uint32_t const *pauComponents, unsigned cComponents, const char *pszName,
const char *pszFile, unsigned iLineNo)
{
/*
* Check preconditions.
*/
size_t cchName = strlen(pszName);
if (cchName == '\0')
return warning("%s(%d): Empty OID name!\n", pszFile, iLineNo);
if (cchName >= BLDPROG_STRTAB_MAX_STRLEN)
return warning("%s(%d): OID name is too long (%u)!\n", pszFile, iLineNo, (unsigned)cchName);
if (cComponents == 0)
return warning("%s(%d): 'Description' without valid OID preceeding it!\n", pszFile, iLineNo);
if (!isNiceAsciiString(pszName))
return warning("%s(%d): Contains unwanted characters!\n", pszFile, iLineNo);
/*
* Make sure we've got a root node (it has no actual OID componet value,
* it's just a place to put the top level children).
*/
if (!g_pOidRoot)
{
g_pOidRoot = (PRAWOIDNODE)calloc(sizeof(*g_pOidRoot), 1);
if (!g_pOidRoot)
return error("Out of memory!\n");
}
/*
* Decend into the tree, adding any missing nodes as we go along.
* We'll end up with the node which is being named.
*/
PRAWOIDNODE pCur = g_pOidRoot;
while (cComponents-- > 0)
{
uint32_t const uKey = *pauComponents++;
uint32_t i = pCur->cChildren;
while ( i > 0
&& pCur->papChildren[i - 1]->uKey >= uKey)
i--;
if ( i < pCur->cChildren
&& pCur->papChildren[i]->uKey == uKey)
pCur = pCur->papChildren[i];
else
{
/* Resize the child pointer array? */
if ((pCur->cChildren % 16) == 0)
{
void *pvNew = realloc(pCur->papChildren, sizeof(pCur->papChildren[0]) * (pCur->cChildren + 16));
if (!pvNew)
return error("Out of memory!\n");
pCur->papChildren = (PRAWOIDNODE *)pvNew;
}
/* Allocate and initialize the node. */
PRAWOIDNODE pNew = (PRAWOIDNODE)malloc(sizeof(*pNew));
if (!pNew)
return error("Out of memory!\n");
pNew->uKey = uKey;
pNew->pParent = pCur;
pNew->papChildren = NULL;
pNew->cChildren = 0;
pNew->fChildrenInBigTable = false;
memset(&pNew->StrTabEntry, 0, sizeof(pNew->StrTabEntry));
/* Insert it. */
if (i < pCur->cChildren)
memmove(&pCur->papChildren[i + 1], &pCur->papChildren[i], (pCur->cChildren - i) * sizeof(pCur->papChildren[0]));
pCur->papChildren[i] = pNew;
pCur->cChildren++;
if (pCur->cChildren > g_cMaxOidChildren)
g_cMaxOidChildren = pCur->cChildren;
g_cOidNodes++;
if (uKey < 64)
g_cOidNodiesWith6bitKeys++;
else
{
pCur->fChildrenInBigTable = true;
if (!pCur->pParent)
return error("Invalid OID! Top level componet value is out of range: %u (max 2)\n", uKey);
}
/* Decend (could optimize insertion of the remaining nodes, but
too much work for very little gain). */
pCur = pNew;
}
}
/*
* Update the node.
*/
if (!pCur->StrTabEntry.pszString)
{
pCur->StrTabEntry.pszString = (char *)malloc(cchName + 1);
if (pCur->StrTabEntry.pszString)
memcpy(pCur->StrTabEntry.pszString, pszName, cchName + 1);
else
return error("Out of memory!\n");
pCur->StrTabEntry.cchString = cchName;
if (cchName >= 64)
pCur->fChildrenInBigTable = true;
g_cOidNodesWithStrings++;
}
/* Ignore duplicates, but warn if different name. */
else if ( pCur->StrTabEntry.cchString != cchName
|| strcmp(pszName, pCur->StrTabEntry.pszString) != 0)
warning("%s(%d): Duplicate OID, name differs: '%s' vs '%s'\n", pszFile, iLineNo, pCur->StrTabEntry.pszString, pszName);
return RTEXITCODE_SUCCESS;
}
static RTEXITCODE parseOid(uint32_t *pauComponents, unsigned *pcComponents, unsigned cMaxComponents, char const *pszOid,
const char *pszFile, unsigned iLine)
{
const char *pszInput = pszOid;
unsigned i = 0;
char ch;
for (;;)
{
/*
* Parse the value.
*/
unsigned uValue = 0;
if (RT_C_IS_DIGIT((ch = *pszOid)))
{
do
{
uValue *= 10;
uValue += ch - '0';
if (uValue < OID2C_MAX_COMP_VALUE)
pszOid++;
else
return warning("%s(%d): Component %u in OID attribute value '%s' is out side the supported!\n",
pszFile, iLine, i, pszInput);
} while (RT_C_IS_DIGIT((ch = *pszOid)));
if ( ch == '\0'
|| ch == '.'
|| RT_C_IS_BLANK(ch))
{
if (i < cMaxComponents)
{
pauComponents[i] = uValue;
i++;
if (ch != '\0')
pszOid++;
else
{
*pcComponents = i;
return RTEXITCODE_SUCCESS;
}
}
else
return warning("%s(%d): Too many OID components in '%s'!\n", pszFile, iLine, pszInput);
}
else
return warning("%s(%d): Invalid OID attribute value '%s' (ch=%c)!\n", pszFile, iLine, pszInput, ch);
}
else
return warning("%s(%d): Invalid OID attribute value '%s' (ch=%c)!\n", pszFile, iLine, pszInput, ch);
}
}
static RTEXITCODE loadOidFile(FILE *pIn, const char *pszFile)
{
/*
* We share the format used by dumpasn1.cfg, except that we accept
* dotted OIDs.
*
* An OID entry starts with a 'OID = <space or dot separated OID>'.
* It is usually followed by an 'Comment = ', which we ignore, and a
* 'Description = <name>' which we keep. We save the entry once we
* see the description attribute.
*/
unsigned cOidComponents = 0;
uint32_t auOidComponents[16];
unsigned iLineNo = 0;
char szLine[16384];
char *pszLine;
szLine[sizeof(szLine) - 1] = '\0';
while ((pszLine = fgets(szLine, sizeof(szLine) - 1, pIn)) != NULL)
{
iLineNo++;
/* Strip leading spaces.*/
char ch;
while (RT_C_IS_SPACE((ch = *pszLine)) )
pszLine++;
/* We only care about lines starting with 'OID =', 'Description =' or
a numbered OID. */
if ( ch == 'O' || ch == 'o'
|| ch == 'D' || ch == 'd'
|| ch == '0' || ch == '1' || ch == '2')
{
/* Right strip the line. */
size_t cchLine = strlen(pszLine);
while (cchLine > 0 && RT_C_IS_SPACE(pszLine[cchLine - 1]))
cchLine--;
pszLine[cchLine] = '\0';
/* Separate the attribute name from the value. */
char *pszValue = (char *)memchr(pszLine, '=', cchLine);
if (pszValue)
{
size_t cchName = pszValue - pszLine;
/* Right strip the name. */
while (cchName > 0 && RT_C_IS_SPACE(pszLine[cchName - 1]))
cchName--;
pszLine[cchName] = '\0';
/* Left strip the value. */
do
pszValue++;
while (RT_C_IS_SPACE(*pszValue));
/* Attribute switch */
if ( cchName == 3
&& (pszLine[0] == 'O' || pszLine[0] == 'o')
&& (pszLine[1] == 'I' || pszLine[1] == 'i')
&& (pszLine[2] == 'D' || pszLine[2] == 'd'))
{
cOidComponents = 0;
parseOid(auOidComponents, &cOidComponents, RT_ELEMENTS(auOidComponents), pszValue, pszFile, iLineNo);
}
else if ( cchName == 11
&& (pszLine[0] == 'D' || pszLine[0] == 'd')
&& (pszLine[1] == 'e' || pszLine[1] == 'E')
&& (pszLine[2] == 's' || pszLine[2] == 'S')
&& (pszLine[3] == 'c' || pszLine[3] == 'C')
&& (pszLine[4] == 'r' || pszLine[4] == 'R')
&& (pszLine[5] == 'i' || pszLine[5] == 'I')
&& (pszLine[6] == 'p' || pszLine[6] == 'P')
&& (pszLine[7] == 't' || pszLine[7] == 'T')
&& (pszLine[8] == 'i' || pszLine[8] == 'I')
&& (pszLine[9] == 'o' || pszLine[9] == 'O')
&& (pszLine[10] == 'n' || pszLine[10] == 'N'))
{
if ( addOidToTree(auOidComponents, cOidComponents, pszValue, pszFile, iLineNo)
!= RTEXITCODE_SUCCESS)
return RTEXITCODE_FAILURE;
cOidComponents = 0;
}
else
{
/* <OID> = <Value> */
cOidComponents = 0;
if ( parseOid(auOidComponents, &cOidComponents, RT_ELEMENTS(auOidComponents), pszLine, pszLine, iLineNo)
== RTEXITCODE_SUCCESS)
{
if ( addOidToTree(auOidComponents, cOidComponents, pszValue, pszFile, iLineNo)
!= RTEXITCODE_SUCCESS)
return RTEXITCODE_FAILURE;
}
cOidComponents = 0;
}
}
}
}
if (feof(pIn))
return RTEXITCODE_SUCCESS;
return error("error or something reading '%s'.\n", pszFile);
}
static RTEXITCODE usage(FILE *pOut, const char *argv0, RTEXITCODE rcExit)
{
fprintf(pOut, "usage: %s <out-file.c> <oid-file> [oid-file2 [...]]\n", argv0);
return rcExit;
}
int main(int argc, char **argv)
{
/*
* Process arguments and input files.
*/
bool fVerbose = false;
unsigned cInFiles = 0;
const char *pszOutFile = NULL;
for (int i = 1; i < argc; i++)
{
const char *pszFile = NULL;
if (argv[i][0] != '-')
pszFile = argv[i];
else if (!strcmp(argv[i], "-"))
pszFile = argv[i];
else
return usage(stderr, argv[0], RTEXITCODE_SYNTAX);
if (!pszOutFile)
pszOutFile = pszFile;
else
{
cInFiles++;
FILE *pInFile = fopen(pszFile, "r");
if (!pInFile)
return error("opening '%s' for reading.\n", pszFile);
RTEXITCODE rcExit = loadOidFile(pInFile, pszFile);
fclose(pInFile);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
}
}
/*
* Check that the user specified at least one input and an output file.
*/
if (!pszOutFile)
return error("No output file specified specified!\n");
if (!cInFiles)
return error("No input files specified!\n");
if (!g_cOidNodes)
return error("No OID found!\n");
if (fVerbose)
printf("debug: %u nodes with strings; %u nodes without strings; %u nodes in total;\n"
"debug: max %u children; %u nodes with 6-bit keys (%u others)\n",
g_cOidNodesWithStrings, g_cOidNodes - g_cOidNodesWithStrings, g_cOidNodes,
g_cMaxOidChildren, g_cOidNodiesWith6bitKeys, g_cOidNodes - g_cOidNodiesWith6bitKeys);
/*
* Compile the string table.
*/
BLDPROGSTRTAB StrTab;
if (!BldProgStrTab_Init(&StrTab, g_cOidNodesWithStrings))
return error("Out of memory!\n");
addStringFromOidTree(g_pOidRoot, &StrTab);
if (!BldProgStrTab_CompileIt(&StrTab, fVerbose))
return error("BldProgStrTab_CompileIt failed!\n");
/*
* Open the output file and write out the stuff.
*/
FILE *pOut;
if (!strcmp(pszOutFile, "-"))
pOut = stdout;
else
pOut = fopen(pszOutFile, "w");
if (!pOut)
return error("opening '%s' for writing.\n", pszOutFile);
/* Write the string table. */
BldProgStrTab_WriteStringTable(&StrTab, pOut, "static ", "g_", "OidDbStrTab");
prepareOidTreeForWriting(g_pOidRoot, 0, false /*fBigTable*/);
prepareOidTreeForWriting(g_pOidRoot, 0, true /*fBigTable*/);
fprintf(pOut,
"\n"
"#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)\n"
"# pragma pack(2)\n"
"#endif\n"
"typedef struct RTOIDENTRYSMALL\n"
"{\n"
" uint32_t uKey : 6;\n"
" uint32_t cchString : 6;\n"
" uint32_t fBigTable : 1;\n"
" uint32_t cChildren : 7;\n"
" uint32_t idxChildren : 12;\n"
" uint16_t offString;\n"
"} RTOIDENTRYSMALL;\n"
"#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)\n"
"# pragma pack()\n"
"AssertCompileSize(RTOIDENTRYSMALL, 6);\n"
"#endif\n"
"typedef RTOIDENTRYSMALL const *PCRTOIDENTRYSMALL;\n"
"\n"
"static const RTOIDENTRYSMALL g_aSmallOidTable[] = \n{\n");
writeOidTree(g_pOidRoot, pOut, false /*fBigTable*/, &StrTab);
fprintf(pOut, "};\n");
fprintf(pOut,
"\n"
"#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)\n"
"# pragma pack(2)\n"
"#endif\n"
"typedef struct RTOIDENTRYBIG\n"
"{\n"
" uint32_t uKey;\n"
" uint8_t cchString;\n"
" uint8_t fBigTable : 1;\n"
" uint8_t cChildren : 7;\n"
" uint16_t idxChildren;\n"
" uint16_t offString;\n"
"} RTOIDENTRYBIG;\n"
"#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)\n"
"# pragma pack()\n"
"AssertCompileSize(RTOIDENTRYBIG, 10);\n"
"#endif\n"
"typedef RTOIDENTRYBIG const *PCRTOIDENTRYBIG;\n"
"\n"
"static const RTOIDENTRYBIG g_aBigOidTable[] = \n{\n");
writeOidTree(g_pOidRoot, pOut, true /*fBigTable*/, &StrTab);
fprintf(pOut, "};\n");
/* Carefully close the output file. */
if (ferror(pOut))
return error("problem writing '%s'!\n", pszOutFile);
if (fclose(pOut) != 0)
return error("closing '%s' after writing it.\n", pszOutFile);
return RTEXITCODE_SUCCESS;
}
|