Subversion Repositories oidconverter

Rev

Rev 2 | Rev 7 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 6
Line 1... Line 1...
1
/*###################################################################
1
/*#################################################################################
2
###                                                               ###
2
###                                                                             ###
3
### Object ID converter. Matthias Gaertner, 06/1999               ###
3
### Object ID converter. Matthias Gaertner, 06/1999                             ###
4
### Converted to plain 'C' 07/2001                                ###
4
### Converted to plain 'C' 07/2001                                              ###
5
###                                                               ###
5
###                                                                             ###
6
### Enhanced version by Daniel Marschall, ViaThinkSoft 06-07/2011 ###
6
### Enhanced version by Daniel Marschall, ViaThinkSoft 06-07/2011               ###
-
 
7
### based on (or rather synchronized to) upstream version 1.3                   ###
7
### -- NEW 1.2: 2.48 can also be encoded!                         ###
8
### -- NEW in +viathinksoft2: 2.48 can also be encoded!                         ###
8
### -- NEW 1.2: UUIDs (128-bit) are now supported!                ###
9
### -- NEW in +viathinksoft2: UUIDs (128-bit) are now supported!                ###
9
###             (requires GMPLib)                                 ###
10
###                           (requires GMPLib)                                 ###
10
### -- NEW 1.3: Length can now have more than 1 byte              ###
11
### -- NEW in +viathinksoft3: Length can now have more than 1 byte              ###
11
### -- NEW 1.4: No command line limitation anymore.               ###
12
### -- NEW in +viathinksoft4: No command line limitation anymore.               ###
12
### -- NEW 1.5: Now also relative OIDs supported                  ###
13
### -- NEW in +viathinksoft5: Now also relative OIDs supported                  ###
13
### -- NEW 1.6: 0x80 paddings are now disallowed                  ###
14
### -- NEW in +viathinksoft6: 0x80 paddings are now disallowed                  ###
14
### -- NEW 1.8: Removed Application/Context/Private "OID"s        ###
15
### -- NEW in +viathinksoft8: Removed Application/Context/Private "OID"s        ###
15
### -- NEW 1.9: Also allow decoding C-notation with "-x"          ###
16
### -- NEW in +viathinksoft9: Also allow decoding C-notation with "-x"          ###
16
### -- AS WELL AS SEVERAL BUG FIXES                               ###
17
### -- AS WELL AS SEVERAL BUG FIXES                                             ###
17
###                                                               ###
18
###                                                                             ###
18
### To compile with gcc simply use:                               ###
19
### To compile with gcc simply use:                                             ###
19
###   gcc -O2 -o oid oid.c -lgmp -lm                              ###
20
###   gcc -O2 -o oid oid.c -lgmp -lm                                            ###
20
###                                                               ###
21
###                                                                             ###
-
 
22
### To compile using lcc-win32, use:                                            ###
-
 
23
###   lcc oid.c & lcclnk oid.obj                                                ###
-
 
24
###                                                                             ###
21
### To compile using cl, use:                                     ###
25
### To compile using cl, use:                                                   ###
22
###   cl -DWIN32 -O1 oid.c (+ include gmp library)                ###
26
###   cl -DWIN32 -O1 oid.c (+ include gmp library)                              ###
23
###                                                               ###
27
###                                                                             ###
24
### Freeware - do with it whatever you want.                      ###
28
### Freeware - do with it whatever you want.                                    ###
25
### Use at your own risk. No warranty of any kind.                ###
29
### Use at your own risk. No warranty of any kind.                              ###
26
###                                                               ###
30
###                                                                             ###
27
###################################################################*/
31
#################################################################################*/
28
/* $Version: 1.11$ */
32
/* $Version: 1.3+viathinksoft11$ */
29
 
33
 
30
// FUTURE
34
// FUTURE
31
// - Alles in Funktionen kapseln. Als Parameter: Array of integer (= dot notation) oder Array of byte (= hex notation)
35
// - Alles in Funktionen kapseln. Als Parameter: Array of integer (= dot notation) oder Array of byte (= hex notation)
32
 
36
 
33
// MINOR THINGS
37
// MINOR THINGS
Line 42... Line 46...
42
// - 2.9a9 is not recognized as error
46
// - 2.9a9 is not recognized as error
43
// - "./oid R 2.999" is not interpretet correctly
47
// - "./oid R 2.999" is not interpretet correctly
44
// - "./oid .2.999" will be interpreted as "0.2.999"
48
// - "./oid .2.999" will be interpreted as "0.2.999"
45
 
49
 
46
// NICE TO HAVE:
50
// NICE TO HAVE:
47
// - also allow -x to interpret "\x06\x02\x88\x37"
51
// - also allow -x to interpret "\x06\x02\x88\x37" and { 0x06, 0x02, 0x88, 0x37 }
48
// - makefile, manpage, linuxpackage
52
// - makefile, manpage, linuxpackage
49
// - better make functions instead of putting everything in main() with fprintf...
53
// - better make functions instead of putting everything in main() with fprintf...
50
 
54
 
51
// NICE TO HAVE (INFINITY-IDEA - NOT IMPORTANT):
55
// NICE TO HAVE (INFINITY-IDEA - NOT IMPORTANT):
52
// - Is it possible to detect integer overflows and therefore output errors?
56
// - Is it possible to detect integer overflows and therefore output errors?
Line 162... Line 166...
162
        char *fInName = NULL;
166
        char *fInName = NULL;
163
        FILE *fOut = NULL;
167
        FILE *fOut = NULL;
164
 
168
 
165
        int n = 1;
169
        int n = 1;
166
        int nMode = MODE_DOT_TO_HEX;
170
        int nMode = MODE_DOT_TO_HEX;
167
        bool nCHex = false;
171
        int nCHex = 0;
168
        int nAfterOption = 0;
172
        int nAfterOption = 0;
169
        bool isRelative = false;
173
        bool isRelative = false;
170
 
174
 
171
        if (argc == 1) {
175
        if (argc == 1) {
172
                fprintf(stderr,
176
                fprintf(stderr,
173
                "OID encoder/decoder 1.11 - Matthias Gaertner 1999/2001, Daniel Marschall 2011/2012 - Freeware\n"
177
                "OID encoder/decoder 1.3+viathinksoft11 - Matthias Gaertner 1999/2001, Daniel Marschall 2011/2012 - Freeware\n"
174
                #ifdef is_gmp
178
                #ifdef is_gmp
175
                "GMP Edition (unlimited arc sizes)\n"
179
                "GMP Edition (unlimited arc sizes)\n"
176
                #else
180
                #else
177
                "%d-bit Edition (arc sizes are limited!)\n"
181
                "%d-bit Edition (arc sizes are limited!)\n"
178
                #endif
182
                #endif
179
                "\nUsage:\n"
183
                "\nUsage:\n"
180
                " OID [-C] [-r] [-o<outfile>] {-i<infile>|2.999.1}\n"
184
                " OID [-c|-C] [-r] [-o<outfile>] {-i<infile>|2.999.1}\n"
181
                "   converts dotted form to ASCII HEX DER output.\n"
185
                "   converts dotted form to ASCII HEX DER output.\n"
-
 
186
                "   -c: Output as C-syntax (array).\n"
182
                "   -C: Output as C-syntax.\n"
187
                "   -C: Output as C-syntax (string).\n"
183
                "   -r: Handle the OID as relative and not absolute.\n"
188
                "   -r: Handle the OID as relative and not absolute.\n"
184
                " OID -x [-o<outfile>] {-i<infile>|hex-digits}\n"
189
                " OID -x [-o<outfile>] {-i<infile>|hex-digits}\n"
185
                "   decodes ASCII HEX DER and gives dotted form.\n" , sizeof(unsigned long) * 8);
190
                "   decodes ASCII HEX DER and gives dotted form.\n" , sizeof(unsigned long) * 8);
186
                return 1;
191
                return 1;
187
        }
192
        }
Line 192... Line 197...
192
                                nMode = MODE_HEX_TO_DOT;
197
                                nMode = MODE_HEX_TO_DOT;
193
                                if (argv[n][2] != '\0') {
198
                                if (argv[n][2] != '\0') {
194
                                        argv[n--] += 2;
199
                                        argv[n--] += 2;
195
                                        nAfterOption = 1;
200
                                        nAfterOption = 1;
196
                                }
201
                                }
-
 
202
                        } else if (argv[n][1] == 'c') {
-
 
203
                                nMode = MODE_DOT_TO_HEX;
-
 
204
                                nCHex = 1;
-
 
205
 
-
 
206
                                if (argv[n][2] != '\0') {
-
 
207
                                        argv[n--] += 2;
-
 
208
                                        nAfterOption = 1;
-
 
209
                                }
197
                        } else if (argv[n][1] == 'C') {
210
                        } else if (argv[n][1] == 'C') {
198
                                nMode = MODE_DOT_TO_HEX;
211
                                nMode = MODE_DOT_TO_HEX;
199
                                nCHex = true;
212
                                nCHex = 2;
200
 
213
 
201
                                if (argv[n][2] != '\0') {
214
                                if (argv[n][2] != '\0') {
202
                                        argv[n--] += 2;
215
                                        argv[n--] += 2;
203
                                        nAfterOption = 1;
216
                                        nAfterOption = 1;
204
                                }
217
                                }
Line 837... Line 850...
837
                } else {
850
                } else {
838
                        fOut = stdout;
851
                        fOut = stdout;
839
                }
852
                }
840
 
853
 
841
                // Write class-tag
854
                // Write class-tag
842
                if (nCHex) {
855
                if (nCHex == 1) {
-
 
856
                        fprintf(fOut, "{ 0x%02X, ", cl);
-
 
857
                } else if (nCHex == 2) {
843
                        fprintf(fOut, "\"\\x%02X", cl);
858
                        fprintf(fOut, "\"\\x%02X", cl);
844
                } else {
859
                } else {
845
                        fprintf(fOut, "%02X ", cl);
860
                        fprintf(fOut, "%02X ", cl);
846
                }
861
                }
847
 
862
 
848
                // Write length
863
                // Write length
849
                if (nBinary <= 0x7F) {
864
                if (nBinary <= 0x7F) {
850
                        if (nCHex) {
865
                        if (nCHex == 1) {
-
 
866
                                fprintf(fOut, "0x%02X, ", nBinary);
-
 
867
                        } else if (nCHex == 2) {
851
                                fprintf(fOut, "\\x%02X", nBinary);
868
                                fprintf(fOut, "\\x%02X", nBinary);
852
                        } else {
869
                        } else {
853
                                fprintf(fOut, "%02X ", nBinary);
870
                                fprintf(fOut, "%02X ", nBinary);
854
                        }
871
                        }
855
                } else {
872
                } else {
Line 865... Line 882...
865
                        if (lengthCount >= 0x7F) {
882
                        if (lengthCount >= 0x7F) {
866
                                fprintf(stderr, "\nThe length cannot be encoded.\n");
883
                                fprintf(stderr, "\nThe length cannot be encoded.\n");
867
                                return 8;
884
                                return 8;
868
                        }
885
                        }
869
 
886
 
870
                        if (nCHex) {
887
                        if (nCHex == 1) {
-
 
888
                                fprintf(fOut, "0x%02X, ", 0x80 + lengthCount);
-
 
889
                        } else if (nCHex == 2) {
871
                                fprintf(fOut, "\\x%02X", 0x80 + lengthCount);
890
                                fprintf(fOut, "\\x%02X", 0x80 + lengthCount);
872
                        } else {
891
                        } else {
873
                                fprintf(fOut, "%02X ", 0x80 + lengthCount);
892
                                fprintf(fOut, "%02X ", 0x80 + lengthCount);
874
                        }
893
                        }
875
 
894
 
876
                        nBinaryWork = nBinary;
895
                        nBinaryWork = nBinary;
877
                        do {
896
                        do {
878
                                if (nCHex) {
897
                                if (nCHex == 1) {
-
 
898
                                        fprintf(fOut, "0x%02X, ", nBinaryWork & 0xFF);
-
 
899
                                } else if (nCHex == 2) {
879
                                        fprintf(fOut, "\\x%02X", nBinaryWork & 0xFF);
900
                                        fprintf(fOut, "\\x%02X", nBinaryWork & 0xFF);
880
                                } else {
901
                                } else {
881
                                        fprintf(fOut, "%02X ", nBinaryWork & 0xFF);
902
                                        fprintf(fOut, "%02X ", nBinaryWork & 0xFF);
882
                                }
903
                                }
883
                                nBinaryWork /= 0x100;
904
                                nBinaryWork /= 0x100;
Line 886... Line 907...
886
 
907
 
887
                nn = 0;
908
                nn = 0;
888
                while (nn < nBinary) {
909
                while (nn < nBinary) {
889
                        unsigned char b = abBinary[nn++];
910
                        unsigned char b = abBinary[nn++];
890
                        if (nn == nBinary) {
911
                        if (nn == nBinary) {
891
                                if (nCHex) {
912
                                if (nCHex == 1) {
-
 
913
                                        fprintf(fOut, "0x%02X }\n", b);
-
 
914
                                } else if (nCHex == 2) {
892
                                        fprintf(fOut, "\\x%02X\"\n", b);
915
                                        fprintf(fOut, "\\x%02X\"\n", b);
893
                                } else {
916
                                } else {
894
                                        fprintf(fOut, "%02X\n", b);
917
                                        fprintf(fOut, "%02X\n", b);
895
                                }
918
                                }
896
                        } else {
919
                        } else {
897
                                if (nCHex) {
920
                                if (nCHex == 1) {
-
 
921
                                        fprintf(fOut, "0x%02X, ", b);
-
 
922
                                } else if (nCHex == 2) {
898
                                        fprintf(fOut, "\\x%02X", b);
923
                                        fprintf(fOut, "\\x%02X", b);
899
                                } else {
924
                                } else {
900
                                        fprintf(fOut, "%02X ", b);
925
                                        fprintf(fOut, "%02X ", b);
901
                                }
926
                                }
902
                        }
927
                        }