Subversion Repositories oidconverter

Rev

Rev 6 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*###################################################################
  2. ###                                                               ###
  3. ### Object ID converter. Matthias Gaertner, 06/1999               ###
  4. ### Converted to plain 'C' 07/2001                                ###
  5. ###                                                               ###
  6. ### Enhanced version by Daniel Marschall, ViaThinkSoft 06-07/2011 ###
  7. ### -- NEW 1.2: 2.48 can also be encoded!                         ###
  8. ### -- NEW 1.2: UUIDs (128-bit) are now supported!                ###
  9. ###             (requires GMPLib)                                 ###
  10. ### -- NEW 1.3: Length can now have more than 1 byte              ###
  11. ### -- NEW 1.4: No command line limitation anymore.               ###
  12. ### -- NEW 1.5: Now also relative OIDs supported                  ###
  13. ### -- NEW 1.6: 0x80 paddings are now disallowed                  ###
  14. ### -- NEW 1.8: Removed Application/Context/Private "OID"s        ###
  15. ### -- NEW 1.9: Also allow decoding C-notation with "-x"          ###
  16. ### -- AS WELL AS SEVERAL BUG FIXES                               ###
  17. ###                                                               ###
  18. ### To compile with gcc simply use:                               ###
  19. ###   gcc -O2 -o oid oid.c -lgmp -lm                              ###
  20. ###                                                               ###
  21. ### To compile using cl, use:                                     ###
  22. ###   cl -DWIN32 -O1 oid.c (+ include gmp library)                ###
  23. ###                                                               ###
  24. ### Freeware - do with it whatever you want.                      ###
  25. ### Use at your own risk. No warranty of any kind.                ###
  26. ###                                                               ###
  27. ###################################################################*/
  28. /* $Version: 1.11$ */
  29.  
  30. // FUTURE
  31. // - Alles in Funktionen kapseln. Als Parameter: Array of integer (= dot notation) oder Array of byte (= hex notation)
  32.  
  33. // MINOR THINGS
  34. // - All stderr: Output new line at stdOut and close stdOut
  35. // - Make as much GMP as possible (e.g. nBinary counter, nBinaryWork etc)
  36. // - überlegen, wie man die return-codes (errorcodes) besser verteilt/definiert
  37. // - irgendwie in funktionen kapseln (z.b. class-tag-parser usw)
  38. // - "TODO"s beachten (unklare dinge)
  39.  
  40. // MINOR PROBLEMS IN CLI-INTERPRETATION:
  41. // - A wrong error message is shown when trying to encode "-0.0" or "x"
  42. // - 2.9a9 is not recognized as error
  43. // - "./oid R 2.999" is not interpretet correctly
  44. // - "./oid .2.999" will be interpreted as "0.2.999"
  45.  
  46. // NICE TO HAVE:
  47. // - also allow -x to interpret "\x06\x02\x88\x37"
  48. // - makefile, manpage, linuxpackage
  49. // - better make functions instead of putting everything in main() with fprintf...
  50.  
  51. // NICE TO HAVE (INFINITY-IDEA - NOT IMPORTANT):
  52. // - Is it possible to detect integer overflows and therefore output errors?
  53.  
  54.  
  55. // -------------------------------------------------------
  56.  
  57. // Allows OIDs which are bigger than "long"
  58. // Compile with "gcc oid.c -lgmp -lm"
  59. #define is_gmp
  60.  
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <stdlib.h>
  64.  
  65. #ifdef is_gmp
  66. #include <gmp.h>
  67. #endif
  68.  
  69. #include <stdbool.h>
  70.  
  71. #ifndef __STRNICMP_LOCAL
  72. #ifdef WIN32
  73. #define __STRNICMP_LOCAL strnicmp
  74. #else
  75. #define __STRNICMP_LOCAL strncasecmp
  76. #endif
  77. #endif
  78.  
  79. // char abCommandLine[4096];
  80. const unsigned int CLI_INITIAL_SIZE = 1024;
  81. const unsigned int CLI_EXPANSION_SIZE = 1024;
  82. unsigned int cli_size;
  83. char * abCommandLine;
  84.  
  85. // unsigned char abBinary[128];
  86. const unsigned int ABB_INITIAL_SIZE = 1024;
  87. const unsigned int ABB_EXPANSION_SIZE = 1024;
  88. unsigned int abb_size;
  89. unsigned char * abBinary;
  90.  
  91. unsigned int nBinary = 0;
  92.  
  93. const int MODE_DOT_TO_HEX = 0;
  94. const int MODE_HEX_TO_DOT = 1;
  95.  
  96. #ifdef is_gmp
  97. static void MakeBase128(mpz_t l, int first) {
  98.         if (mpz_cmp_si(l, 127) > 0) {
  99.                 mpz_t l2;
  100.                 mpz_init(l2);
  101.                 mpz_div_ui(l2, l, 128);
  102.                 MakeBase128(l2, 0);
  103.         }
  104.         mpz_mod_ui(l, l, 128);
  105.  
  106.         if (nBinary+1 >= abb_size) {
  107.                 abb_size += ABB_EXPANSION_SIZE;
  108.                 abBinary = (unsigned char*) realloc(abBinary, abb_size);
  109.                 if (abBinary == NULL) {
  110.                         fprintf(stderr, "Memory reallocation failure!\n");
  111.                         exit(EXIT_FAILURE);
  112.                 }
  113.         }
  114.  
  115.         if (first) {
  116.                 abBinary[nBinary++] = mpz_get_ui(l);
  117.         } else {
  118.                 abBinary[nBinary++] = 0x80 | mpz_get_ui(l);
  119.         }
  120. }
  121. #else
  122. static void MakeBase128(unsigned long l, int first) {
  123.         if (l > 127) {
  124.                 MakeBase128(l / 128, 0);
  125.         }
  126.         l %= 128;
  127.  
  128.         if (nBinary+1 >= abb_size) {
  129.                 abb_size += ABB_EXPANSION_SIZE;
  130.                 abBinary = (unsigned char*) realloc(abBinary, abb_size);
  131.                 if (abBinary == NULL) {
  132.                         fprintf(stderr, "Memory reallocation failure!\n");
  133.                         exit(EXIT_FAILURE);
  134.                 }
  135.         }
  136.  
  137.         if (first) {
  138.                 abBinary[nBinary++] = (unsigned char)l;
  139.         } else {
  140.                 abBinary[nBinary++] = 0x80 | (unsigned char)l;
  141.         }
  142. }
  143. #endif
  144.  
  145. int main(int argc, char **argv) {
  146.         cli_size = CLI_INITIAL_SIZE;
  147.         abCommandLine  = (char*) malloc(cli_size * sizeof(char*));
  148.         if (abCommandLine == NULL) {
  149.                 fprintf(stderr, "Memory allocation failure!\n");
  150.                 return EXIT_FAILURE;
  151.         }
  152.  
  153.         abb_size = ABB_INITIAL_SIZE;
  154.         abBinary  = (unsigned char*) malloc(abb_size * sizeof(unsigned char*));
  155.         if (abBinary == NULL) {
  156.                 fprintf(stderr, "Memory allocation failure!\n");
  157.                 return EXIT_FAILURE;
  158.         }
  159.  
  160.  
  161.         char *fOutName = NULL;
  162.         char *fInName = NULL;
  163.         FILE *fOut = NULL;
  164.  
  165.         int n = 1;
  166.         int nMode = MODE_DOT_TO_HEX;
  167.         bool nCHex = false;
  168.         int nAfterOption = 0;
  169.         bool isRelative = false;
  170.  
  171.         if (argc == 1) {
  172.                 fprintf(stderr,
  173.                 "OID encoder/decoder 1.11 - Matthias Gaertner 1999/2001, Daniel Marschall 2011/2012 - Freeware\n"
  174.                 #ifdef is_gmp
  175.                 "GMP Edition (unlimited arc sizes)\n"
  176.                 #else
  177.                 "%d-bit Edition (arc sizes are limited!)\n"
  178.                 #endif
  179.                 "\nUsage:\n"
  180.                 " OID [-C] [-r] [-o<outfile>] {-i<infile>|2.999.1}\n"
  181.                 "   converts dotted form to ASCII HEX DER output.\n"
  182.                 "   -C: Output as C-syntax.\n"
  183.                 "   -r: Handle the OID as relative and not absolute.\n"
  184.                 " OID -x [-o<outfile>] {-i<infile>|hex-digits}\n"
  185.                 "   decodes ASCII HEX DER and gives dotted form.\n" , sizeof(unsigned long) * 8);
  186.                 return 1;
  187.         }
  188.  
  189.         while (n < argc) {
  190.                 if (!nAfterOption && argv[n][0] == '-') {
  191.                         if (argv[n][1] == 'x') {
  192.                                 nMode = MODE_HEX_TO_DOT;
  193.                                 if (argv[n][2] != '\0') {
  194.                                         argv[n--] += 2;
  195.                                         nAfterOption = 1;
  196.                                 }
  197.                         } else if (argv[n][1] == 'C') {
  198.                                 nMode = MODE_DOT_TO_HEX;
  199.                                 nCHex = true;
  200.  
  201.                                 if (argv[n][2] != '\0') {
  202.                                         argv[n--] += 2;
  203.                                         nAfterOption = 1;
  204.                                 }
  205.                         } else if (argv[n][1] == 'r') {
  206.                                 nMode = MODE_DOT_TO_HEX;
  207.                                 isRelative = true;
  208.  
  209.                                 if (argv[n][2] != '\0') {
  210.                                         argv[n--] += 2;
  211.                                         nAfterOption = 1;
  212.                                 }
  213.                         } else if (argv[n][1] == 'o') {
  214.                                 if (argv[n][2] != '\0') {
  215.                                         fOutName = &argv[n][2];
  216.                                 } else if (n < argc-1) {
  217.                                         fOutName = argv[++n];
  218.                                 } else {
  219.                                         fprintf(stderr, "Incomplete command line.\n");
  220.                                         return EXIT_FAILURE;
  221.                                 }
  222.                         } else if (argv[n][1] == 'i') {
  223.                                 if (argv[n][2] != '\0') {
  224.                                         fInName = &argv[n][2];
  225.                                 } else if (n < argc-1) {
  226.                                         fInName = argv[++n];
  227.                                 } else {
  228.                                         fprintf(stderr, "Incomplete command line.\n");
  229.                                         return EXIT_FAILURE;
  230.                                 }
  231.                         }
  232.                 } else {
  233.                         if (fInName != NULL) { // TODO: (Unklar) Was bewirkt das? Auch für fOutName notwendig?
  234.                                 break;
  235.                         }
  236.  
  237.                         nAfterOption = 1;
  238.                         if (strlen(argv[n]) + strlen(abCommandLine) >= sizeof(abCommandLine)-2) { // TODO: warum -2 ?
  239.                                 // fprintf(stderr, "Command line too long.\n");
  240.                                 // return 2;
  241.  
  242.                                 cli_size += CLI_EXPANSION_SIZE + strlen(argv[n]) + 1; // 1 = "."
  243.                                 abCommandLine = (char*) realloc(abCommandLine, cli_size);
  244.                                 if (abCommandLine == NULL) {
  245.                                         fprintf(stderr, "Memory reallocation failure!\n");
  246.                                         return EXIT_FAILURE;
  247.                                 }
  248.                         }
  249.                         strcat(abCommandLine, argv[n]); // (fügt ein \0 automatisch an)
  250.                         if (n != argc - 1 && nMode != MODE_HEX_TO_DOT) {
  251.                                 strcat(abCommandLine, ".");
  252.                         }
  253.                 }
  254.                 n++;
  255.         }
  256.  
  257.         if (fInName != NULL && nMode == MODE_HEX_TO_DOT) {
  258.                 FILE *fIn = fopen(fInName, "rb");
  259.                 size_t nRead = 0;
  260.                 if (fIn == NULL) {
  261.                         fprintf(stderr, "Unable to open input file %s.\n", fInName);
  262.                         return 11;
  263.                 }
  264.                 nRead = fread(abCommandLine, 1, sizeof(abCommandLine), fIn);
  265.                 abCommandLine[nRead] = '\0';
  266.                 fclose(fIn);
  267.         } else if (fInName != NULL && nMode == MODE_DOT_TO_HEX) {
  268.                 FILE *fIn = fopen(fInName, "rt");
  269.                 if (fIn == NULL) {
  270.                         fprintf(stderr, "Unable to open input file %s.\n", fInName);
  271.                         return 11;
  272.                 }
  273.                 fgets(abCommandLine, sizeof(abCommandLine), fIn);
  274.                 fclose(fIn);
  275.         }
  276.  
  277.         while (nMode == MODE_HEX_TO_DOT) { /* better if */
  278.                 /* hex->dotted */
  279.                 /*printf("Hex-In: %s\n", abCommandLine);*/
  280.  
  281.                 char *p = abCommandLine;
  282.                 char *q = p;
  283.  
  284.                 unsigned char *pb = NULL;
  285.                 unsigned int nn = 0;
  286.                 #ifdef is_gmp
  287.                 mpz_t ll;
  288.                 mpz_init(ll);
  289.                 #else
  290.                 unsigned long ll = 0;
  291.                 #endif
  292.                 bool fOK = false;
  293.                 int fSub = 0; // Subtract value from next number output. Used when encoding {2 48} and up
  294.  
  295.                 while (*p) {
  296.                         if (*p != '.' && *p != '\r' && *p != '\n' && *p != '\x20' && *p != '\t') {
  297.                                 *q++ = *p;
  298.                         }
  299.                         p++;
  300.                 }
  301.                 *q = '\0';
  302.  
  303.                 if (strlen(abCommandLine) % 2 != 0) {
  304.                         fprintf(stderr, "Encoded OID must have even number of hex digits!\n");
  305.                         return 2;
  306.                 }
  307.  
  308.                 if (strlen(abCommandLine) < 3) {
  309.                         fprintf(stderr, "Encoded OID must have at least three bytes!\n");
  310.                         return 2;
  311.                 }
  312.  
  313.                 nBinary = 0;
  314.                 p = abCommandLine;
  315.  
  316.                 while (*p) {
  317.                         unsigned char b;
  318.  
  319.                         // This allows also C-notation
  320.                         if ((p[0] == '\\') && (p[1] == 'x')) {
  321.                                 p += 2;
  322.                                 continue;
  323.                         }
  324.  
  325.                         // Interpret upper nibble
  326.                         if (p[0] >= 'A' && p[0] <= 'F') {
  327.                                 b = (p[0] - 'A' + 10) * 16;
  328.                         } else if (p[0] >= 'a' && p[0] <= 'f') {
  329.                                 b = (p[0] - 'a' + 10) * 16;
  330.                         } else if (p[0] >= '0' && p[0] <= '9') {
  331.                                 b = (p[0] - '0') * 16;
  332.                         } else {
  333.                                 fprintf(stderr, "Must have hex digits only!\n");
  334.                                 return 2;
  335.                         }
  336.  
  337.                         // Interpret lower nibble
  338.                         if (p[1] >= 'A' && p[1] <= 'F') {
  339.                                 b += (p[1] - 'A' + 10);
  340.                         } else if (p[1] >= 'a' && p[1] <= 'f') {
  341.                                 b += (p[1] - 'a' + 10);
  342.                         } else if (p[1] >= '0' && p[1] <= '9') {
  343.                                 b += (p[1] - '0');
  344.                         } else {
  345.                                 fprintf(stderr, "Must have hex digits only!\n");
  346.                                 return 2;
  347.                         }
  348.  
  349.                         if (nBinary+1 >= abb_size) {
  350.                                 abb_size += ABB_EXPANSION_SIZE;
  351.                                 abBinary = (unsigned char*) realloc(abBinary, abb_size);
  352.                                 if (abBinary == NULL) {
  353.                                         fprintf(stderr, "Memory reallocation failure!\n");
  354.                                         return EXIT_FAILURE;
  355.                                 }
  356.                         }
  357.  
  358.                         abBinary[nBinary++] = b;
  359.                         p += 2;
  360.                 }
  361.  
  362.                 /*printf("Hex-In: %s\n", abCommandLine);*/
  363.  
  364.                 if (fOutName != NULL) {
  365.                         fOut = fopen(fOutName, "wt");
  366.                         if (fOut == NULL) {
  367.                                 fprintf(stderr, "Unable to open output file %s\n", fOutName);
  368.                                 return 33;
  369.                         }
  370.                 } else {
  371.                         fOut = stdout;
  372.                 }
  373.  
  374.                 pb = abBinary;
  375.                 nn = 0;
  376.                 #ifdef is_gmp
  377.                 mpz_init(ll);
  378.                 #else
  379.                 ll = 0;
  380.                 #endif
  381.                 fOK = false;
  382.                 fSub = 0;
  383.  
  384.                 // 0 = Universal Class Identifier Tag (can be more than 1 byte, but not in our case)
  385.                 // 1 = Length part (may have more than 1 byte!)
  386.                 // 2 = First two arc encoding
  387.                 // 3 = Encoding of arc three and higher
  388.                 unsigned char part = 0;
  389.  
  390.                 unsigned char lengthbyte_count = 0;
  391.                 unsigned char lengthbyte_pos = 0;
  392.                 bool lengthfinished = false;
  393.  
  394.                 bool arcBeginning = true;
  395.                 bool firstWrittenArc = true;
  396.  
  397.                 while (nn < nBinary) {
  398.                         if (part == 0) { // Class Tag
  399.  
  400.                                 // Leading octet
  401.                                 // Bit 7 / Bit 6 = Universal (00), Application (01), Context (10), Private(11)
  402.                                 // Bit 5 = Primitive (0), Constructed (1)
  403.                                 // Bit 4..0 = 00000 .. 11110 => Tag 0..30, 11111 for Tag > 30 (following bytes with the highest bit as "more" bit)
  404.                                 // --> We don't need to respect 11111 (class-tag encodes in more than 1 octet)
  405.                                 //     as we terminate when the tag is not of type OID or RELATEIVE-OID
  406.                                 // See page 396 of "ASN.1 - Communication between Heterogeneous Systems" by Olivier Dubuisson.
  407.  
  408.                                 // Class: 8. - 7. bit
  409.                                 // 0 (00) = Universal
  410.                                 // 1 (01) = Application
  411.                                 // 2 (10) = Context
  412.                                 // 3 (11) = Private
  413.                                 unsigned char cl = ((*pb & 0xC0) >> 6) & 0x03;
  414.                                 if (cl != 0) {
  415.                                         fprintf(stderr, "\nError at type: The OID tags are only defined as UNIVERSAL class tags.\n");
  416.                                         fprintf(fOut, "\n");
  417.                                         return 6;
  418.                                 }
  419.  
  420.                                 // Primitive/Constructed: 6. bit
  421.                                 // 0 = Primitive
  422.                                 // 1 = Constructed
  423.                                 unsigned char pc = *pb & 0x20;
  424.                                 if (pc != 0) {
  425.                                         fprintf(stderr, "\nError at type: OIDs must be primitive, not constructed.\n");
  426.                                         fprintf(fOut, "\n");
  427.                                         return 6;
  428.                                 }
  429.  
  430.                                 // Tag number: 5. - 1. bit
  431.                                 unsigned char tag = *pb & 0x1F;
  432.                                 if (tag == 0x0D) {
  433.                                         isRelative = true;
  434.                                 } else if (tag == 0x06) {
  435.                                         isRelative = false;
  436.                                 } else {
  437.                                         fprintf(stderr, "\nError at type: The tag number is neither an absolute OID (0x06) nor a relative OID (0x0D).\n");
  438.                                         fprintf(fOut, "\n");
  439.                                         return 6;
  440.                                 }
  441.  
  442.                                 // Output
  443.                                 if (isRelative) {
  444.                                         fprintf(fOut, "RELATIVE");
  445.                                 } else {
  446.                                         fprintf(fOut, "ABSOLUTE");
  447.                                 }
  448.  
  449.                                 fprintf(fOut, " OID");
  450.                                 part++;
  451.                         } else if (part == 1) { // Length
  452.  
  453.                                 // Find out the length and save it into ll
  454.  
  455.                                 // [length] is encoded as follows:
  456.                                 //  0x00 .. 0x7F = The actual length is in this byte, followed by [data].
  457.                                 //  0x80 + n     = The length of [data] is spread over the following 'n' bytes. (0 < n < 0x7F)
  458.                                 //  0x80         = "indefinite length" (only constructed form) -- Invalid
  459.                                 //  0xFF         = Reserved for further implementations -- Invalid
  460.                                 //  See page 396 of "ASN.1 - Communication between Heterogeneous Systems" by Olivier Dubuisson.
  461.  
  462.                                 if (nn == 1) { // The first length byte
  463.                                         lengthbyte_pos = 0;
  464.                                         if ((*pb & 0x80) != 0) {
  465.                                                 // 0x80 + n => The length is spread over the following 'n' bytes
  466.                                                 lengthfinished = false;
  467.                                                 lengthbyte_count = *pb & 0x7F;
  468.                                                 if (lengthbyte_count == 0x00) {
  469.                                                         fprintf(stderr, "\nLength value 0x80 is invalid (\"indefinite length\") for primitive types.\n");
  470.                                                         fprintf(fOut, "\n");
  471.                                                         return 7;
  472.                                                 } else if (lengthbyte_count == 0x7F) {
  473.                                                         fprintf(stderr, "\nLength value 0xFF is reserved for further extensions.\n");
  474.                                                         fprintf(fOut, "\n");
  475.                                                         return 7;
  476.                                                 }
  477.                                                 fOK = false;
  478.                                         } else {
  479.                                                 // 0x01 .. 0x7F => The actual length
  480.  
  481.                                                 if (*pb == 0x00) {
  482.                                                         fprintf(stderr, "\nLength value 0x00 is invalid for an OID.\n");
  483.                                                         fprintf(fOut, "\n");
  484.                                                         return 7;
  485.                                                 }
  486.  
  487.                                                 #ifdef is_gmp
  488.                                                 mpz_set_ui(ll, *pb);
  489.                                                 #else
  490.                                                 ll = *pb;
  491.                                                 #endif
  492.                                                 lengthfinished = true;
  493.                                                 lengthbyte_count = 0;
  494.                                                 fOK = true;
  495.                                         }
  496.                                 } else {
  497.                                         if (lengthbyte_count > lengthbyte_pos) {
  498.                                                 #ifdef is_gmp
  499.                                                 mpz_mul_ui(ll, ll, 0x100);
  500.                                                 mpz_add_ui(ll, ll, *pb);
  501.                                                 #else
  502.                                                 ll *= 0x100;
  503.                                                 ll += *pb;
  504.                                                 #endif
  505.                                                 lengthbyte_pos++;
  506.                                         }
  507.  
  508.                                         if (lengthbyte_count == lengthbyte_pos) {
  509.                                                 lengthfinished = true;
  510.                                                 fOK = true;
  511.                                         }
  512.                                 }
  513.  
  514.                                 if (lengthfinished) { // The length is now in ll
  515.                                         #ifdef is_gmp
  516.                                         if (mpz_cmp_ui(ll,  nBinary - 2 - lengthbyte_count) != 0) {
  517.                                                 fprintf(fOut, "\n");
  518.                                                 if (fOut != stdout) {
  519.                                                         fclose(fOut);
  520.                                                 }
  521.                                                 fprintf(stderr, "\nInvalid length (%d entered, but %s expected)\n", nBinary - 2, mpz_get_str(NULL, 10, ll));
  522.                                                 return 3;
  523.                                         }
  524.                                         mpz_set_ui(ll, 0); // reset for later usage
  525.                                         #else
  526.                                         if (ll != nBinary - 2 - lengthbyte_count) {
  527.                                                 fprintf(fOut, "\n");
  528.                                                 if (fOut != stdout) {
  529.                                                         fclose(fOut);
  530.                                                 }
  531.                                                 fprintf(stderr, "\nInvalid length (%d entered, but %d expected)\n", nBinary - 2, ll);
  532.                                                 return 3;
  533.                                         }
  534.                                         ll = 0; // reset for later usage
  535.                                         #endif
  536.                                         fOK = true;
  537.                                         part++;
  538.                                         if (isRelative) part++; // Goto step 3!
  539.                                 }
  540.                         } else if (part == 2) { // First two arcs
  541.                                 int first = *pb / 40;
  542.                                 int second = *pb % 40;
  543.                                 if (first > 2) {
  544.                                         first = 2;
  545.                                         fprintf(fOut, " %d", first);
  546.                                         firstWrittenArc = false;
  547.                                         arcBeginning = true;
  548.  
  549.                                         if ((*pb & 0x80) != 0) {
  550.                                                 // 2.48 and up => 2+ octets
  551.                                                 // Output in "part 3"
  552.  
  553.                                                 if (arcBeginning && (*pb == 0x80)) {
  554.                                                         fprintf(fOut, "\n");
  555.                                                         if (fOut != stdout) {
  556.                                                                 fclose(fOut);
  557.                                                         }
  558.                                                         fprintf(stderr, "\nEncoding error. Illegal 0x80 paddings. (See Rec. ITU-T X.690, clause 8.19.2)\n");
  559.                                                         return 4;
  560.                                                 } else {
  561.                                                         arcBeginning = false;
  562.                                                 }
  563.  
  564.                                                 #ifdef is_gmp
  565.                                                 mpz_add_ui(ll, ll, (*pb & 0x7F));
  566.                                                 #else
  567.                                                 ll += (*pb & 0x7F);
  568.                                                 #endif
  569.                                                 fSub = 80;
  570.                                                 fOK = false;
  571.                                         } else {
  572.                                                 // 2.0 till 2.47 => 1 octet
  573.                                                 second = *pb - 80;
  574.                                                 fprintf(fOut, ".%d", second);
  575.                                                 arcBeginning = true;
  576.                                                 fOK = true;
  577.                                                 #ifdef is_gmp
  578.                                                 mpz_set_ui(ll, 0);
  579.                                                 #else
  580.                                                 ll = 0;
  581.                                                 #endif
  582.                                         }
  583.                                 } else {
  584.                                         // 0.0 till 0.37 => 1 octet
  585.                                         // 1.0 till 1.37 => 1 octet
  586.                                         fprintf(fOut, " %d.%d", first, second);
  587.                                         firstWrittenArc = false;
  588.                                         arcBeginning = true;
  589.                                         fOK = true;
  590.                                         #ifdef is_gmp
  591.                                         mpz_set_ui(ll, 0);
  592.                                         #else
  593.                                         ll = 0;
  594.                                         #endif
  595.                                 }
  596.                                 part++;
  597.                         } else if (part == 3) { // Arc three and higher
  598.                                 if ((*pb & 0x80) != 0) {
  599.                                         if (arcBeginning && (*pb == 0x80)) {
  600.                                                 fprintf(fOut, "\n");
  601.                                                 if (fOut != stdout) {
  602.                                                         fclose(fOut);
  603.                                                 }
  604.                                                 fprintf(stderr, "\nEncoding error. Illegal 0x80 paddings. (See Rec. ITU-T X.690, clause 8.19.2)\n");
  605.                                                 return 4;
  606.                                         } else {
  607.                                                 arcBeginning = false;
  608.                                         }
  609.  
  610.                                         #ifdef is_gmp
  611.                                         mpz_mul_ui(ll, ll, 0x80);
  612.                                         mpz_add_ui(ll, ll, (*pb & 0x7F));
  613.                                         #else
  614.                                         ll *= 0x80;
  615.                                         ll += (*pb & 0x7F);
  616.                                         #endif
  617.                                         fOK = false;
  618.                                 } else {
  619.                                         fOK = true;
  620.                                         #ifdef is_gmp
  621.                                         mpz_mul_ui(ll, ll, 0x80);
  622.                                         mpz_add_ui(ll, ll, *pb);
  623.                                         mpz_sub_ui(ll, ll, fSub);
  624.                                         if (firstWrittenArc) {
  625.                                                 fprintf(fOut, " %s", mpz_get_str(NULL, 10, ll));
  626.                                                 firstWrittenArc = false;
  627.                                         } else {
  628.                                                 fprintf(fOut, ".%s", mpz_get_str(NULL, 10, ll));
  629.                                         }
  630.                                         // Happens only if 0x80 paddings are allowed
  631.                                         // fOK = mpz_cmp_ui(ll, 0) >= 0;
  632.                                         mpz_set_ui(ll, 0);
  633.                                         #else
  634.                                         ll *= 0x80;
  635.                                         ll += *pb;
  636.                                         ll -= fSub;
  637.                                         if (firstWrittenArc) {
  638.                                                 fprintf(fOut, " %lu", ll);
  639.                                                 firstWrittenArc = false;
  640.                                         } else {
  641.                                                 fprintf(fOut, ".%lu", ll);
  642.                                         }
  643.                                         // Happens only if 0x80 paddings are allowed
  644.                                         // fOK = ll >= 0;
  645.                                         ll = 0;
  646.                                         #endif
  647.                                         fSub = 0;
  648.                                         arcBeginning = true;
  649.                                 }
  650.                         }
  651.  
  652.                         pb++;
  653.                         nn++;
  654.                 }
  655.  
  656.                 if (!fOK) {
  657.                         fprintf(fOut, "\n");
  658.                         if (fOut != stdout) {
  659.                                 fclose(fOut);
  660.                         }
  661.                         fprintf(stderr, "\nEncoding error. The OID is not constructed properly.\n");
  662.                         return 4;
  663.                 } else {
  664.                         fprintf(fOut, "\n");
  665.                 }
  666.  
  667.                 if (fOut != stdout) {
  668.                         fclose(fOut);
  669.                 }
  670.                 break;
  671.         };
  672.  
  673.         while (nMode == MODE_DOT_TO_HEX) { /* better if */
  674.                 /* dotted->hex */
  675.                 /* printf("OID %s\n", abCommandLine); */
  676.  
  677.                 char *p = abCommandLine;
  678.                 unsigned char cl = 0x00;
  679.                 char *q = NULL;
  680.                 int nPieces = 1;
  681.                 int n = 0;
  682.                 unsigned char b = 0;
  683.                 unsigned int nn = 0;
  684.                 #ifdef is_gmp
  685.                 mpz_t l;
  686.                 #else
  687.                 unsigned long l = 0;
  688.                 #endif
  689.                 bool isjoint = false;
  690.  
  691.                 // Alternative call: ./oid RELATIVE.2.999
  692.                 if (__STRNICMP_LOCAL(p, "ABSOLUTE.", 9) == 0) {
  693.                         isRelative = false;
  694.                         p+=9;
  695.                 } else if (__STRNICMP_LOCAL(p, "RELATIVE.", 9) == 0) {
  696.                         isRelative = true;
  697.                         p+=9;
  698.                 } else {
  699.                         // use the CLI option
  700.                         // isRelative = false;
  701.                 }
  702.  
  703.                 cl = 0x00; // Class. Always UNIVERSAL (00)
  704.  
  705.                 // Tag for Universal Class
  706.                 if (isRelative) {
  707.                         cl |= 0x0D;
  708.                 } else {
  709.                         cl |= 0x06;
  710.                 }
  711.  
  712.                 /* if (__STRNICMP_LOCAL(p, "OID.", 4) == 0) {
  713.                         p+=4;
  714.                 } */
  715.  
  716.                 q = p;
  717.                 nPieces = 1;
  718.                 while (*p) {
  719.                         if (*p == '.') {
  720.                                 nPieces++;
  721.                         }
  722.                         p++;
  723.                 }
  724.  
  725.                 n = 0;
  726.                 b = 0;
  727.                 p = q;
  728.                 while (n < nPieces) {
  729.                         q = p;
  730.                         while (*p) {
  731.                                 if (*p == '.') {
  732.                                         break;
  733.                                 }
  734.                                 p++;
  735.                         }
  736.  
  737.                         #ifdef is_gmp
  738.                         mpz_init(l);
  739.                         #else
  740.                         l = 0;
  741.                         #endif
  742.                         if (*p == '.') {
  743.                                 *p = 0;
  744.                                 #ifdef is_gmp
  745.                                 mpz_set_str(l, q, 10);
  746.                                 #else
  747.                                 l = (unsigned long) atoi(q);
  748.                                 #endif
  749.                                 q = p+1;
  750.                                 p = q;
  751.                         } else {
  752.                                 #ifdef is_gmp
  753.                                 mpz_set_str(l, q, 10);
  754.                                 #else
  755.                                 l = (unsigned long) atoi(q);
  756.                                 #endif
  757.                                 q = p;
  758.                         }
  759.  
  760.                         /* Digit is in l. */
  761.                         if ((!isRelative) && (n == 0)) {
  762.                                 #ifdef is_gmp
  763.                                 if (mpz_cmp_ui(l, 2) > 0) {
  764.                                 #else
  765.                                 if (l > 2) {
  766.                                 #endif
  767.                                         fprintf(stderr, "\nEncoding error. The top arc is limited to 0, 1 and 2.\n");
  768.                                         return 5;
  769.                                 }
  770.                                 #ifdef is_gmp
  771.                                 b += 40 * mpz_get_ui(l);
  772.                                 isjoint = mpz_cmp_ui(l, 2) == 0;
  773.                                 #else
  774.                                 b = 40 * ((unsigned char)l);
  775.                                 isjoint = l == 2;
  776.                                 #endif
  777.                         } else if ((!isRelative) && (n == 1)) {
  778.                                 #ifdef is_gmp
  779.                                 if ((!isjoint) && (mpz_cmp_ui(l, 39) > 0)) {
  780.                                 #else
  781.                                 if ((l > 39) && (!isjoint)) {
  782.                                 #endif
  783.                                         fprintf(stderr, "\nEncoding error. The second arc is limited to 0..39 for root arcs 0 and 1.\n");
  784.                                         return 5;
  785.                                 }
  786.  
  787.                                 #ifdef is_gmp
  788.                                 if (mpz_cmp_ui(l, 47) > 0) {
  789.                                         mpz_add_ui(l, l, 80);
  790.                                         MakeBase128(l, 1);
  791.                                 } else {
  792.                                         b += mpz_get_ui(l);
  793.                                         if (nBinary+1 >= abb_size) {
  794.                                                 abb_size += ABB_EXPANSION_SIZE;
  795.                                                 abBinary = (unsigned char*) realloc(abBinary, abb_size);
  796.                                                 if (abBinary == NULL) {
  797.                                                         fprintf(stderr, "Memory reallocation failure!\n");
  798.                                                         return EXIT_FAILURE;
  799.                                                 }
  800.                                         }
  801.                                         abBinary[nBinary++] = b;
  802.                                 }
  803.                                 #else
  804.                                 if (l > 47) {
  805.                                         l += 80;
  806.                                         MakeBase128(l, 1);
  807.                                 } else {
  808.                                         b += ((unsigned char) l);
  809.                                         if (nBinary+1 >= abb_size) {
  810.                                                 abb_size += ABB_EXPANSION_SIZE;
  811.                                                 abBinary = (unsigned char*) realloc(abBinary, abb_size);
  812.                                                 if (abBinary == NULL) {
  813.                                                         fprintf(stderr, "Memory reallocation failure!\n");
  814.                                                         return EXIT_FAILURE;
  815.                                                 }
  816.                                         }
  817.                                         abBinary[nBinary++] = b;
  818.                                 }
  819.                                 #endif
  820.                         } else {
  821.                                 MakeBase128(l, 1);
  822.                         }
  823.                         n++;
  824.                 }
  825.  
  826.                 if ((!isRelative) && (n < 2)) {
  827.                         fprintf(stderr, "\nEncoding error. The minimum depth of an encodeable absolute OID is 2. (e.g. 2.999)\n");
  828.                         return 5;
  829.                 }
  830.  
  831.                 if (fOutName != NULL) {
  832.                         fOut = fopen(fOutName, "wt");
  833.                         if (fOut == NULL) {
  834.                                 fprintf(stderr, "Unable to open output file %s\n", fOutName);
  835.                                 return 33;
  836.                         }
  837.                 } else {
  838.                         fOut = stdout;
  839.                 }
  840.  
  841.                 // Write class-tag
  842.                 if (nCHex) {
  843.                         fprintf(fOut, "\"\\x%02X", cl);
  844.                 } else {
  845.                         fprintf(fOut, "%02X ", cl);
  846.                 }
  847.  
  848.                 // Write length
  849.                 if (nBinary <= 0x7F) {
  850.                         if (nCHex) {
  851.                                 fprintf(fOut, "\\x%02X", nBinary);
  852.                         } else {
  853.                                 fprintf(fOut, "%02X ", nBinary);
  854.                         }
  855.                 } else {
  856.                         unsigned int nBinaryWork;
  857.                         unsigned int lengthCount = 0;
  858.  
  859.                         nBinaryWork = nBinary;
  860.                         do {
  861.                                 lengthCount++;
  862.                                 nBinaryWork /= 0x100;
  863.                         } while (nBinaryWork > 0);
  864.  
  865.                         if (lengthCount >= 0x7F) {
  866.                                 fprintf(stderr, "\nThe length cannot be encoded.\n");
  867.                                 return 8;
  868.                         }
  869.  
  870.                         if (nCHex) {
  871.                                 fprintf(fOut, "\\x%02X", 0x80 + lengthCount);
  872.                         } else {
  873.                                 fprintf(fOut, "%02X ", 0x80 + lengthCount);
  874.                         }
  875.  
  876.                         nBinaryWork = nBinary;
  877.                         do {
  878.                                 if (nCHex) {
  879.                                         fprintf(fOut, "\\x%02X", nBinaryWork & 0xFF);
  880.                                 } else {
  881.                                         fprintf(fOut, "%02X ", nBinaryWork & 0xFF);
  882.                                 }
  883.                                 nBinaryWork /= 0x100;
  884.                         } while (nBinaryWork > 0);
  885.                 }
  886.  
  887.                 nn = 0;
  888.                 while (nn < nBinary) {
  889.                         unsigned char b = abBinary[nn++];
  890.                         if (nn == nBinary) {
  891.                                 if (nCHex) {
  892.                                         fprintf(fOut, "\\x%02X\"\n", b);
  893.                                 } else {
  894.                                         fprintf(fOut, "%02X\n", b);
  895.                                 }
  896.                         } else {
  897.                                 if (nCHex) {
  898.                                         fprintf(fOut, "\\x%02X", b);
  899.                                 } else {
  900.                                         fprintf(fOut, "%02X ", b);
  901.                                 }
  902.                         }
  903.                 }
  904.                 if (fOut != stdout) {
  905.                         fclose(fOut);
  906.                 }
  907.                 break;
  908.         }
  909.  
  910.         free(abCommandLine);
  911.         free(abBinary);
  912.  
  913.         return 0;
  914. }
  915.