Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

903 lines
32 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. #include "precomp.h"
  4. const ASN1uint32_t c_BitMaskMap[sizeof(ASN1uint32_t) * 8] =
  5. {
  6. #ifdef USE_BIG_ENDIAN
  7. 0x80000000, 0x40000000, 0x20000000, 0x10000000,
  8. 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  9. 0x800000, 0x400000, 0x200000, 0x100000,
  10. 0x080000, 0x040000, 0x020000, 0x010000,
  11. 0x8000, 0x4000, 0x2000, 0x1000,
  12. 0x0800, 0x0400, 0x0200, 0x0100,
  13. 0x80, 0x40, 0x20, 0x10,
  14. 0x08, 0x04, 0x02, 0x01,
  15. #else // little endian
  16. 0x80, 0x40, 0x20, 0x10,
  17. 0x08, 0x04, 0x02, 0x01,
  18. 0x8000, 0x4000, 0x2000, 0x1000,
  19. 0x0800, 0x0400, 0x0200, 0x0100,
  20. 0x800000, 0x400000, 0x200000, 0x100000,
  21. 0x080000, 0x040000, 0x020000, 0x010000,
  22. 0x80000000, 0x40000000, 0x20000000, 0x10000000,
  23. 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  24. #endif
  25. };
  26. void GenComponents(AssignmentList_t ass, ComponentList_t components,
  27. char *identifier, char *completeidentifier, int *choiceoffset,
  28. int *optionaloffset);
  29. void GenType(AssignmentList_t ass, char *identifier, char *completeidentifier,
  30. Type_t *type, int withmodule, int pointer, int array, int fTypeDef);
  31. void GenEnumeration(AssignmentList_t ass, ComponentList_t components,
  32. char *identifier, char *completeidentifier, int *choiceoffset,
  33. int *optionaloffset);
  34. int NotInFunTbl(Assignment_t *a);
  35. unsigned g_cPDUs = 0;
  36. unsigned g_cOptionValueSize = 0;
  37. extern int g_fAllEndians;
  38. /* generate include file */
  39. void
  40. GenInc(AssignmentList_t ass, FILE *finc, char *module)
  41. {
  42. Assignment_t *a;
  43. char *identifier;
  44. int offset, i;
  45. Type_t *type, *subtype;
  46. Value_t *value;
  47. unsigned npdu;
  48. char *pszPrivateValueName;
  49. unsigned cOptionValueSizeSave;
  50. setoutfile(finc);
  51. module = Identifier2C(module);
  52. // print verbatim
  53. PrintVerbatim();
  54. /* file header */
  55. output("#ifndef _%s_H_\n", module);
  56. output("#define _%s_H_\n\n", module);
  57. switch (g_eEncodingRule)
  58. {
  59. case eEncoding_Packed:
  60. output("#include \"msper.h\"\n");
  61. break;
  62. case eEncoding_Basic:
  63. output("#include \"msber.h\"\n");
  64. break;
  65. default:
  66. ASSERT(0);
  67. break;
  68. }
  69. output("\n");
  70. for (i = 0; i < g_cGhostFiles; i++)
  71. {
  72. StripModuleName(g_aGhostFiles[i].pszFileName, g_aGhostFiles[i].pszFileName);
  73. strcat(g_aGhostFiles[i].pszFileName, ".h");
  74. output("#include \"%s\"\n", g_aGhostFiles[i].pszFileName);
  75. }
  76. if (g_cGhostFiles)
  77. {
  78. output("\n");
  79. }
  80. // ghost all the data structures from the ghost asn1 files
  81. for (a = ass; a; a = a->Next)
  82. {
  83. if (a->Type != eAssignment_Type)
  84. continue;
  85. type = a->U.Type.Type;
  86. if (type->Flags & eTypeFlags_Null)
  87. continue;
  88. if (!(type->Flags & eTypeFlags_GenType))
  89. continue;
  90. if (a->Module && a->Module->Identifier)
  91. {
  92. for (i = 0; i < g_cGhostFiles; i++)
  93. {
  94. if (! strcmp(a->Module->Identifier, g_aGhostFiles[i].pszModuleName))
  95. {
  96. a->fGhost = 1;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. /* language dependent interface */
  103. switch (g_eProgramLanguage) {
  104. case eLanguage_C:
  105. // output("#include \"cinterf.h\"\n\n");
  106. output("#ifdef __cplusplus\n");
  107. outputni("extern \"C\" {\n");
  108. output("#endif\n\n");
  109. break;
  110. case eLanguage_Cpp:
  111. // output("#include \"cppinterf.h\"\n\n");
  112. // break;
  113. default:
  114. ASSERT(0);
  115. break;
  116. }
  117. /* create endian independent macros */
  118. if (g_fAllEndians)
  119. {
  120. output("#ifdef ASN1_BIG_ENDIAN\n");
  121. output("#define ASN1_LITE_TO_BIGE_16(n16) ((((n16) & 0xFF) << 8) | (((n16) & 0xFF00) >> 8))\n");
  122. output("#define ASN1_LITE_TO_BIGE_32(n32) ((((n32) & 0xFF) << 24) | (((n32) & 0xFF00) << 8)) | (((n32) & 0xFF0000) >> 8) | (((n32) & 0xFF000000) >> 24)\n");
  123. output("#define ASN1_ENDIAN_16(n16) ASN1_LITE_TO_BIGE_16(n16)\n");
  124. output("#define ASN1_ENDIAN_32(n32) ASN1_LITE_TO_BIGE_32(n32)\n");
  125. output("#else\n");
  126. output("#define ASN1_ENDIAN_16(n16) (n16)\n");
  127. output("#define ASN1_ENDIAN_32(n32) (n32)\n");
  128. output("#endif // ASN1_BIG_ENDIAN\n\n");
  129. }
  130. /* typedefs for sequence of/set of with list representation */
  131. npdu = 0;
  132. for (a = ass; a; a = a->Next) {
  133. if (a->Type != eAssignment_Type)
  134. continue;
  135. type = a->U.Type.Type;
  136. if (type->Flags & eTypeFlags_Null)
  137. continue;
  138. if (!(type->Flags & eTypeFlags_GenType))
  139. continue;
  140. if (a->fGhost)
  141. continue;
  142. identifier = GetName(a);
  143. switch (type->Type) {
  144. case eType_SequenceOf:
  145. case eType_SetOf:
  146. if (GetTypeRules(ass, type) & (eTypeRules_LinkedListMask | eTypeRules_PointerToElement))
  147. {
  148. if (g_fExtraStructPtrTypeSS)
  149. {
  150. output("typedef struct %s_s * P%s;\n",
  151. identifier, identifier);
  152. }
  153. else
  154. {
  155. output("typedef struct %s * P%s;\n",
  156. identifier, identifier);
  157. }
  158. output("\n");
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. /* other type definitions */
  166. for (a = ass; a; a = a->Next) {
  167. if (a->Type != eAssignment_Type)
  168. continue;
  169. type = a->U.Type.Type;
  170. /* skip null type */
  171. if (type->Flags & eTypeFlags_Null)
  172. {
  173. if (type->Type == eType_Sequence)
  174. {
  175. identifier = GetName(a);
  176. output("typedef struct %s {\n", identifier);
  177. output("char placeholder;\n");
  178. output("} %s;\n\n", identifier);
  179. }
  180. continue;
  181. }
  182. /* type definition wanted? */
  183. if (!(type->Flags & eTypeFlags_GenType))
  184. continue;
  185. if ((! g_fLongNameForImported) && a->fImportedLocalDuplicate)
  186. continue;
  187. if (a->fGhost)
  188. continue;
  189. /* assign an id number to the type */
  190. identifier = GetName(a);
  191. /* write type definitions */
  192. switch (type->Type) {
  193. case eType_Sequence:
  194. case eType_Set:
  195. case eType_InstanceOf:
  196. // save
  197. cOptionValueSizeSave = g_cOptionValueSize;
  198. // generate scope-free enumeration
  199. offset = 0;
  200. GenEnumeration(ass, type->U.SSC.Components, identifier, identifier, NULL, &offset);
  201. /* use struct for sequence/set/instanceof type */
  202. /* add a bit field for optional components */
  203. output("typedef struct %s {\n", identifier);
  204. if (type->U.SSC.Optionals || type->U.SSC.Extensions) {
  205. int cOctets = (type->U.SSC.Optionals + 7) / 8 +
  206. (type->U.SSC.Extensions + 7) / 8;
  207. g_cOptionValueSize = (cOctets <= 2) ? 16 : 32;
  208. output("union {\nASN1uint%u_t %s;\nASN1octet_t o[%d];\n};\n",
  209. g_cOptionValueSize, g_pszOptionValue, cOctets);
  210. }
  211. offset = 0;
  212. GenComponents(ass, type->U.SSC.Components,
  213. identifier, identifier, NULL, &offset);
  214. output("} %s;\n", identifier);
  215. // restore
  216. g_cOptionValueSize = cOptionValueSizeSave;
  217. break;
  218. case eType_Choice:
  219. // generate scope-free enumeration
  220. offset = 0;
  221. GenEnumeration(ass, type->U.SSC.Components, identifier, identifier, NULL, &offset);
  222. /* use a struct of an selector and a union for choice type */
  223. output("typedef struct %s {\n", identifier);
  224. // output("%s o;\n", GetChoiceType(type));
  225. output("ASN1choice_t choice;\n");
  226. if (!(type->Flags & eTypeFlags_NullChoice))
  227. output("union {\n");
  228. offset = ASN1_CHOICE_BASE;
  229. GenComponents(ass, type->U.Choice.Components,
  230. identifier, identifier, &offset, NULL);
  231. if (!(type->Flags & eTypeFlags_NullChoice))
  232. output("} u;\n");
  233. output("} %s;\n", identifier);
  234. break;
  235. case eType_SequenceOf:
  236. case eType_SetOf:
  237. //
  238. // LONCHANC: The following two lines of code do not apply to
  239. // SEQUENCE OF and SET OF.
  240. //
  241. // generate scope-free enumeration
  242. // offset = 0;
  243. // GenEnumeration(ass, type->U.SSC.Components, identifier, identifier, NULL, &offset);
  244. /* use a struct of length+values for sequence of/set of with */
  245. /* length-pointer representation */
  246. /* use a struct of next+value for sequence of/set of with */
  247. /* singly-linked-list representation */
  248. /* use a struct of next+prev+value for sequence of/set of with */
  249. /* doubly-linked-list representation */
  250. subtype = type->U.SS.Type;
  251. if (g_fExtraStructPtrTypeSS &&
  252. (type->Rules & (eTypeRules_LinkedListMask | eTypeRules_PointerToElement)))
  253. {
  254. output("typedef struct %s_s {\n", identifier);
  255. }
  256. else
  257. {
  258. output("typedef struct %s {\n", identifier);
  259. }
  260. // fix (xyz..MAX) problem.
  261. if (type->Rules == eTypeRules_FixedArray && type->PERTypeInfo.Root.LUpperVal == 0)
  262. {
  263. type->Rules &= ~ eTypeRules_FixedArray;
  264. type->Rules |= g_eDefTypeRuleSS_NonSized;
  265. type->PERTypeInfo.Rules &= ~ eTypeRules_FixedArray;
  266. type->PERTypeInfo.Rules |= g_eDefTypeRuleSS_NonSized;
  267. type->BERTypeInfo.Rules &= ~ eTypeRules_FixedArray;
  268. type->BERTypeInfo.Rules |= g_eDefTypeRuleSS_NonSized;
  269. }
  270. pszPrivateValueName = GetPrivateValueName(&type->PrivateDirectives, "value");
  271. if (type->Rules & eTypeRules_FixedArray)
  272. {
  273. output("ASN1uint32_t count;\n");
  274. GenType(ass, pszPrivateValueName, pszPrivateValueName, subtype,
  275. 0, 0, type->PERTypeInfo.Root.LUpperVal, 0);
  276. }
  277. else
  278. if (type->Rules & eTypeRules_LengthPointer)
  279. {
  280. output("ASN1uint32_t count;\n");
  281. GenType(ass, pszPrivateValueName, pszPrivateValueName, subtype,
  282. 0, 1, 0, 0);
  283. }
  284. else
  285. if (type->Rules & eTypeRules_DoublyLinkedList)
  286. {
  287. output("P%s next;\n", identifier);
  288. output("P%s prev;\n", identifier);
  289. GenType(ass, pszPrivateValueName, pszPrivateValueName, subtype,
  290. 0, 0, 0, 0);
  291. }
  292. else
  293. if (type->Rules & eTypeRules_SinglyLinkedList)
  294. {
  295. output("P%s next;\n", identifier);
  296. GenType(ass, pszPrivateValueName, pszPrivateValueName, subtype,
  297. 0, 0, 0, 0);
  298. }
  299. else
  300. {
  301. MyAbort();
  302. }
  303. if (type->Rules & eTypeRules_LinkedListMask)
  304. {
  305. if (g_fExtraStructPtrTypeSS)
  306. {
  307. output("} %s_Element, *%s;\n", identifier, identifier);
  308. }
  309. else
  310. {
  311. output("} %s_Element;\n", identifier);
  312. }
  313. } else {
  314. output("} %s;\n", identifier);
  315. }
  316. break;
  317. default:
  318. /* use the builtin type for other types */
  319. output("typedef ");
  320. GenType(ass, a->Identifier, identifier, type, 1, 0, 0, 1);
  321. break;
  322. }
  323. if (! NotInFunTbl(a))
  324. {
  325. output("#define %s_%s %u\n", identifier, g_pszApiPostfix, npdu);
  326. switch (type->Type)
  327. {
  328. case eType_SequenceOf:
  329. case eType_SetOf:
  330. if (type->Rules & eTypeRules_LinkedListMask)
  331. {
  332. output("#define SIZE_%s_%s_%u sizeof(%s_Element)\n", module, g_pszApiPostfix, npdu, identifier);
  333. break;
  334. }
  335. // intentionally fall through
  336. default:
  337. output("#define SIZE_%s_%s_%u sizeof(%s)\n", module, g_pszApiPostfix, npdu, identifier);
  338. break;
  339. }
  340. npdu++;
  341. }
  342. output("\n");
  343. }
  344. /* write extern declarations for values */
  345. for (a = ass; a; a = a->Next)
  346. {
  347. if (a->Type != eAssignment_Value)
  348. continue;
  349. /* extern value wanted? */
  350. if (!(a->U.Value.Value->Flags & eValueFlags_GenExternValue))
  351. continue;
  352. value = GetValue(ass, a->U.Value.Value);
  353. /* skip value of null type */
  354. if (value->Type->Flags & eTypeFlags_Null)
  355. continue;
  356. /* output an extern declaration */
  357. switch (value->Type->Type)
  358. {
  359. case eType_ObjectIdentifier:
  360. if (value->Type->PrivateDirectives.fOidPacked)
  361. {
  362. output("extern ASN1encodedOID_t %s;\n", GetName(a));
  363. break;
  364. }
  365. else
  366. if (value->Type->PrivateDirectives.fOidArray || g_fOidArray)
  367. {
  368. output("extern ASN1objectidentifier2_t %s;\n", GetName(a));
  369. break;
  370. }
  371. // intentionally fall through
  372. default:
  373. output("extern %s %s;\n", GetTypeName(ass, value->Type), GetName(a));
  374. break;
  375. }
  376. }
  377. output("\n");
  378. /* write vars, functions and macros for the interface */
  379. output("extern ASN1module_t %s;\n", module);
  380. output("extern void ASN1CALL %s_Startup(void);\n", module);
  381. output("extern void ASN1CALL %s_Cleanup(void);\n", module);
  382. output("\n");
  383. output("/* Prototypes of element functions for SEQUENCE OF and SET OF constructs */\n");
  384. g_cPDUs = npdu;
  385. }
  386. /* generate a type */
  387. void
  388. GenType(AssignmentList_t ass, char *identifier, char *completeidentifier, Type_t *type, int withmodule, int pointer, int array, int fTypeDef)
  389. {
  390. char *ptr, *pszOldEnumName;
  391. NamedNumber_t *namedNumbers;
  392. char arr[20];
  393. char modide[256];
  394. /* skip null type */
  395. // if (type->Flags & eTypeFlags_Null)
  396. // return;
  397. /* get type name */
  398. ptr = pointer ? "*" : "";
  399. if (array)
  400. sprintf(arr, "[%u]", array);
  401. else
  402. arr[0] = '\0';
  403. identifier = Identifier2C(identifier);
  404. if (withmodule)
  405. sprintf(modide, "%s%s%s", ptr, completeidentifier, arr);
  406. else
  407. sprintf(modide, "%s%s%s", ptr, identifier, arr);
  408. /* output type declaration */
  409. switch (type->Type) {
  410. case eType_Reference:
  411. /* use struct ..._s syntax for pointer to structured type */
  412. if (pointer && IsStructuredType(GetReferencedType(ass, type))) {
  413. output("struct %s %s;\n", GetTypeName(ass, type), modide);
  414. } else {
  415. char *psz = PGetTypeName(ass, type);
  416. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  417. output("%s %s%s;\n", psz, modide, psz2);
  418. }
  419. break;
  420. case eType_OctetString:
  421. if (g_eEncodingRule == eEncoding_Packed)
  422. {
  423. if (type->PERTypeInfo.Root.LConstraint == ePERSTIConstraint_Constrained &&
  424. (! type->PrivateDirectives.fLenPtr))
  425. {
  426. if (strcmp(completeidentifier, modide) == 0)
  427. {
  428. output("struct %s {\nASN1uint32_t length;\nASN1octet_t value[%u];\n} %s;\n",
  429. modide, type->PERTypeInfo.Root.LUpperVal, modide);
  430. }
  431. else
  432. {
  433. output("struct %s_%s {\nASN1uint32_t length;\nASN1octet_t value[%u];\n} %s;\n",
  434. completeidentifier, modide, type->PERTypeInfo.Root.LUpperVal, modide);
  435. }
  436. }
  437. else
  438. {
  439. char *psz = GetTypeName(ass, type);
  440. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  441. output("%s %s%s;\n", psz, modide, psz2);
  442. }
  443. }
  444. else
  445. {
  446. // only support unbounded in BER
  447. char *psz = GetTypeName(ass, type);
  448. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  449. output("%s %s%s;\n", psz, modide, psz2);
  450. }
  451. break;
  452. case eType_UTF8String:
  453. {
  454. char *psz = GetTypeName(ass, type);
  455. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  456. output("%s %s%s;\n", psz, modide, psz2);
  457. }
  458. break;
  459. case eType_InstanceOf:
  460. case eType_SequenceOf:
  461. case eType_SetOf:
  462. case eType_Boolean:
  463. case eType_Real:
  464. case eType_Sequence:
  465. case eType_Set:
  466. case eType_Choice:
  467. case eType_External:
  468. case eType_ObjectIdentifier:
  469. case eType_ObjectDescriptor:
  470. case eType_BMPString:
  471. case eType_UniversalString:
  472. case eType_GeneralizedTime:
  473. case eType_UTCTime:
  474. case eType_EmbeddedPdv:
  475. case eType_Open:
  476. /* use struct ..._s syntax for pointer to structured type */
  477. if (pointer && IsStructuredType(type)) {
  478. output("struct %s %s;\n", GetTypeName(ass, type), modide);
  479. } else {
  480. char *psz = GetTypeName(ass, type);
  481. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  482. output("%s %s%s;\n", psz, modide, psz2);
  483. }
  484. break;
  485. case eType_NumericString:
  486. case eType_PrintableString:
  487. case eType_VisibleString:
  488. case eType_ISO646String:
  489. case eType_GraphicString:
  490. case eType_GeneralString:
  491. case eType_VideotexString:
  492. case eType_IA5String:
  493. case eType_T61String:
  494. case eType_TeletexString:
  495. case eType_CharacterString:
  496. #ifdef ENABLE_CHAR_STR_SIZE
  497. {
  498. char *psz = GetTypeName(ass, type);
  499. if (g_eEncodingRule == eEncoding_Packed &&
  500. type->PERTypeInfo.Root.LConstraint == ePERSTIConstraint_Constrained &&
  501. strcmp(psz, "ASN1char_t") == 0)
  502. {
  503. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  504. output("%s %s%s[%u];\n", psz, modide, psz2, type->PERTypeInfo.Root.LUpperVal + 1);
  505. }
  506. else
  507. {
  508. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  509. output("%s %s%s;\n", psz, modide, psz2);
  510. }
  511. }
  512. #else
  513. {
  514. char *psz = GetTypeName(ass, type);
  515. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  516. output("%s %s%s;\n", psz, modide, psz2);
  517. }
  518. #endif
  519. break;
  520. case eType_Enumerated:
  521. /* use name of the type */
  522. if (fTypeDef)
  523. {
  524. if (type->PrivateDirectives.pszTypeName)
  525. {
  526. output("enum %s {\n", type->PrivateDirectives.pszTypeName);
  527. pszOldEnumName = type->PrivateDirectives.pszTypeName;
  528. }
  529. else
  530. {
  531. output("enum %s {\n", modide);
  532. pszOldEnumName = "";
  533. }
  534. /* dump named numbers */
  535. namedNumbers = type->U.IEB.NamedNumbers;
  536. while (namedNumbers) {
  537. switch (namedNumbers->Type) {
  538. case eNamedNumber_Normal:
  539. if (intxisuint32(&GetValue(ass,
  540. namedNumbers->U.Normal.Value)->U.Integer.Value)) {
  541. char *psz = Identifier2C(namedNumbers->U.Normal.Identifier);
  542. if (IsReservedWord(psz) || DoesEnumNameConflict(psz))
  543. {
  544. output("%s_%s = %u,\n", *pszOldEnumName ? pszOldEnumName : modide, psz,
  545. intx2uint32(&GetValue(ass,
  546. namedNumbers->U.Normal.Value)->U.Integer.Value));
  547. }
  548. else
  549. {
  550. output("%s = %u,\n", psz,
  551. intx2uint32(&GetValue(ass,
  552. namedNumbers->U.Normal.Value)->U.Integer.Value));
  553. }
  554. } else if (intxisint32(&GetValue(ass,
  555. namedNumbers->U.Normal.Value)->U.Integer.Value)) {
  556. char *psz = Identifier2C(namedNumbers->U.Normal.Identifier);
  557. if (IsReservedWord(psz) || DoesEnumNameConflict(psz))
  558. {
  559. output("%s_%s = %d,\n", *pszOldEnumName ? pszOldEnumName : modide, psz,
  560. intx2uint32(&GetValue(ass,
  561. namedNumbers->U.Normal.Value)->U.Integer.Value));
  562. }
  563. else
  564. {
  565. output("%s = %d,\n", psz,
  566. intx2uint32(&GetValue(ass,
  567. namedNumbers->U.Normal.Value)->U.Integer.Value));
  568. }
  569. }
  570. break;
  571. case eNamedNumber_ExtensionMarker:
  572. break;
  573. }
  574. namedNumbers = namedNumbers->Next;
  575. }
  576. output("} %s;\n", type->PrivateDirectives.pszTypeName ? type->PrivateDirectives.pszTypeName : modide);
  577. }
  578. else
  579. {
  580. if (type->PrivateDirectives.pszTypeName)
  581. {
  582. pszOldEnumName = type->PrivateDirectives.pszTypeName;
  583. }
  584. else
  585. {
  586. pszOldEnumName = modide;
  587. }
  588. output("%s %s;\n", pszOldEnumName, modide);
  589. }
  590. break;
  591. case eType_Integer:
  592. /* use name of the type */
  593. {
  594. char *psz = GetTypeName(ass, type);
  595. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  596. output("%s %s%s;\n", psz, modide, psz2);
  597. }
  598. /* dump named numbers */
  599. namedNumbers = type->U.IEB.NamedNumbers;
  600. while (namedNumbers) {
  601. switch (namedNumbers->Type) {
  602. case eNamedNumber_Normal:
  603. if (intxisuint32(&GetValue(ass,
  604. namedNumbers->U.Normal.Value)->U.Integer.Value)) {
  605. output("#define %s_%s %u\n", completeidentifier,
  606. Identifier2C(namedNumbers->U.Normal.Identifier),
  607. intx2uint32(&GetValue(ass,
  608. namedNumbers->U.Normal.Value)->U.Integer.Value));
  609. } else if (intxisint32(&GetValue(ass,
  610. namedNumbers->U.Normal.Value)->U.Integer.Value)) {
  611. output("#define %s_%s %d\n", completeidentifier,
  612. Identifier2C(namedNumbers->U.Normal.Identifier),
  613. intx2uint32(&GetValue(ass,
  614. namedNumbers->U.Normal.Value)->U.Integer.Value));
  615. }
  616. break;
  617. case eNamedNumber_ExtensionMarker:
  618. break;
  619. }
  620. namedNumbers = namedNumbers->Next;
  621. }
  622. break;
  623. case eType_BitString: // lonchanc: split from eType_Integer
  624. /* use name of the type */
  625. if (g_eEncodingRule == eEncoding_Packed)
  626. {
  627. if (type->PERTypeInfo.Root.cbFixedSizeBitString)
  628. {
  629. output("ASN1uint%u_t %s;\n",
  630. type->PERTypeInfo.Root.cbFixedSizeBitString * 8, modide);
  631. }
  632. else
  633. {
  634. char *psz = GetTypeName(ass, type);
  635. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  636. output("%s %s%s;\n", psz, modide, psz2);
  637. }
  638. }
  639. else
  640. {
  641. // only support unbounded in BER
  642. char *psz = GetTypeName(ass, type);
  643. char *psz2 = (0 == strcmp(psz, modide)) ? "_dont_care" : "";
  644. output("%s %s%s;\n", psz, modide, psz2);
  645. }
  646. /* dump named numbers */
  647. namedNumbers = type->U.IEB.NamedNumbers;
  648. while (namedNumbers) {
  649. switch (namedNumbers->Type) {
  650. case eNamedNumber_Normal:
  651. if (intxisuint32(&GetValue(ass,
  652. namedNumbers->U.Normal.Value)->U.Integer.Value)) {
  653. char *psz = Identifier2C(namedNumbers->U.Normal.Identifier);
  654. ASN1uint32_t idx = intx2uint32(&GetValue(ass,
  655. namedNumbers->U.Normal.Value)->U.Integer.Value);
  656. if (IsReservedWord(psz) || DoesEnumNameConflict(psz))
  657. {
  658. output("#define %s_%s 0x%lx\n", completeidentifier,
  659. psz, c_BitMaskMap[idx]);
  660. }
  661. else
  662. {
  663. output("#define %s 0x%lx\n", psz, c_BitMaskMap[idx]);
  664. }
  665. } else if (intxisint32(&GetValue(ass,
  666. namedNumbers->U.Normal.Value)->U.Integer.Value)) {
  667. char *psz = Identifier2C(namedNumbers->U.Normal.Identifier);
  668. ASN1uint32_t idx = intx2uint32(&GetValue(ass,
  669. namedNumbers->U.Normal.Value)->U.Integer.Value);
  670. if (IsReservedWord(psz) || DoesEnumNameConflict(psz))
  671. {
  672. output("#define %s_%s 0x%lx\n", completeidentifier,
  673. psz, c_BitMaskMap[idx]);
  674. }
  675. else
  676. {
  677. output("#define %s 0x%lx\n", psz, c_BitMaskMap[idx]);
  678. }
  679. }
  680. break;
  681. case eNamedNumber_ExtensionMarker:
  682. break;
  683. }
  684. namedNumbers = namedNumbers->Next;
  685. }
  686. break;
  687. case eType_Null:
  688. break;
  689. case eType_Undefined:
  690. MyAbort();
  691. /*NOTREACHED*/
  692. }
  693. }
  694. /* write declarations for components */
  695. void
  696. GenComponents(AssignmentList_t ass, ComponentList_t components, char *identifier, char *completeidentifier, int *choiceoffset, int *optionaloffset)
  697. {
  698. NamedType_t *namedType;
  699. int extended = 0;
  700. char cidebuf[256];
  701. /* add dummy for empty components */
  702. if (components && !components->Next &&
  703. components->Type == eComponent_ExtensionMarker) {
  704. output("char placeholder;\n");
  705. return;
  706. }
  707. /* write a declaration for every component */
  708. for (; components; components = components->Next) {
  709. switch (components->Type) {
  710. case eComponent_Normal:
  711. case eComponent_Optional:
  712. case eComponent_Default:
  713. /* write a selector for optional/default components */
  714. namedType = components->U.NOD.NamedType;
  715. if ((extended && optionaloffset) ||
  716. components->Type == eComponent_Optional ||
  717. components->Type == eComponent_Default)
  718. {
  719. char *psz;
  720. // construct the option value
  721. char szOptionValue[64];
  722. if (g_fAllEndians)
  723. {
  724. sprintf(szOptionValue, "ASN1_ENDIAN_%u(0x%lx)", g_cOptionValueSize, c_BitMaskMap[*optionaloffset]);
  725. }
  726. else
  727. {
  728. sprintf(szOptionValue, "0x%lx", c_BitMaskMap[*optionaloffset]);
  729. }
  730. psz = Identifier2C(namedType->Identifier);
  731. if (IsReservedWord(psz) || DoesOptNameConflict(psz))
  732. // lonchanc: do we always put in _preset definition after extended mark???
  733. // yes, we do. take an example of Setup-UUIE in q931asn.asn.
  734. // but, the extension mark does not have option-flag definition associated.
  735. // || (extended && optionaloffset))
  736. {
  737. output("#define %s_%s_%s %s\n",
  738. identifier, psz, g_pszOptionPostfix,
  739. &szOptionValue[0]);
  740. }
  741. else
  742. {
  743. output("#define %s_%s %s\n",
  744. psz, g_pszOptionPostfix,
  745. &szOptionValue[0]);
  746. }
  747. (*optionaloffset)++;
  748. }
  749. /* write a selector for choice alternatives */
  750. if (choiceoffset)
  751. {
  752. char *psz = Identifier2C(namedType->Identifier);
  753. if (IsReservedWord(psz) || DoesChoiceNameConflict(psz))
  754. {
  755. output("#define %s_%s_%s %d\n",
  756. identifier, psz, g_pszChoicePostfix,
  757. *choiceoffset);
  758. }
  759. else
  760. {
  761. output("#define %s_%s %d\n",
  762. psz, g_pszChoicePostfix,
  763. *choiceoffset);
  764. }
  765. (*choiceoffset)++;
  766. }
  767. /* write the declaration itself */
  768. sprintf(cidebuf, "%s_%s", completeidentifier,
  769. Identifier2C(namedType->Identifier));
  770. GenType(ass, namedType->Identifier, cidebuf, namedType->Type,
  771. 0, GetTypeRules(ass, namedType->Type) & eTypeRules_Pointer, 0, 0);
  772. break;
  773. case eComponent_ExtensionMarker:
  774. /* update the offset when an extension marker is met for a */
  775. /* sequence/set type */
  776. extended = 1;
  777. // lonchanc: however, the code generated by TELES handles this properly.
  778. // moreover, the code requires it to be rounded up.
  779. // as a result, we should enable this feature.
  780. if (optionaloffset)
  781. *optionaloffset = (*optionaloffset + 7) & ~7;
  782. break;
  783. }
  784. }
  785. }
  786. // The following is added by Microsoft
  787. /* write enumerations for components */
  788. void
  789. GenEnumeration(AssignmentList_t ass, ComponentList_t components, char *identifier, char *completeidentifier, int *choiceoffset, int *optionaloffset)
  790. {
  791. NamedType_t *namedType;
  792. int extended = 0;
  793. char cidebuf[256];
  794. /* write a declaration for every component */
  795. for (; components; components = components->Next)
  796. {
  797. switch (components->Type)
  798. {
  799. case eComponent_Normal:
  800. case eComponent_Optional:
  801. case eComponent_Default:
  802. namedType = components->U.NOD.NamedType;
  803. if (namedType && namedType->Type && namedType->Type->Type == eType_Enumerated)
  804. {
  805. output("typedef ");
  806. /* write the declaration itself */
  807. sprintf(cidebuf, "%s_%s", completeidentifier,
  808. Identifier2C(namedType->Identifier));
  809. GenType(ass, namedType->Identifier, cidebuf, namedType->Type,
  810. 0, GetTypeRules(ass, namedType->Type) & eTypeRules_Pointer, 0, 1);
  811. }
  812. break;
  813. }
  814. }
  815. }