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