oidcalc.c (1775B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 int 10 main(int argc, char **argv) 11 { 12 char *curstr; 13 char *nextstr; 14 unsigned int firstval; 15 unsigned int secondval; 16 unsigned int val; 17 unsigned char buf[5]; 18 int count; 19 20 if (argc != 2) { 21 fprintf(stderr, "wrong number of args\n"); 22 exit(-1); 23 } 24 25 curstr = argv[1]; 26 27 nextstr = strchr(curstr, '.'); 28 29 if (nextstr == NULL) { 30 fprintf(stderr, "only one component\n"); 31 exit(-1); 32 } 33 34 *nextstr = '\0'; 35 firstval = atoi(curstr); 36 37 curstr = nextstr + 1; 38 39 nextstr = strchr(curstr, '.'); 40 41 if (nextstr) { 42 *nextstr = '\0'; 43 } 44 45 secondval = atoi(curstr); 46 47 if (firstval > 2) { 48 fprintf(stderr, "first component out of range\n"); 49 exit(-1); 50 } 51 52 if (secondval > 39) { 53 fprintf(stderr, "second component out of range\n"); 54 exit(-1); 55 } 56 57 printf("0x%x, ", (firstval * 40) + secondval); 58 while (nextstr) { 59 curstr = nextstr + 1; 60 61 nextstr = strchr(curstr, '.'); 62 63 if (nextstr) { 64 *nextstr = '\0'; 65 } 66 67 memset(buf, 0, sizeof(buf)); 68 val = atoi(curstr); 69 count = 0; 70 while (val) { 71 buf[count] = (val & 0x7f); 72 val = val >> 7; 73 count++; 74 } 75 76 while (count--) { 77 if (count) { 78 printf("0x%x, ", buf[count] | 0x80); 79 } else { 80 printf("0x%x, ", buf[count]); 81 } 82 } 83 } 84 printf("\n"); 85 return 0; 86 }