Subversion Repositories oidconverter

Rev

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