security/nss/cmd/oidcalc/oidcalc.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/oidcalc/oidcalc.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +#include <stdlib.h>
    1.10 +#include <string.h>
    1.11 +
    1.12 +int
    1.13 +main(int argc, char **argv)
    1.14 +{
    1.15 +    char *curstr;
    1.16 +    char *nextstr;
    1.17 +    unsigned int firstval;
    1.18 +    unsigned int secondval;
    1.19 +    unsigned int val;
    1.20 +    unsigned char buf[5];
    1.21 +    int count;
    1.22 +    
    1.23 +    if ( argc != 2 ) {
    1.24 +	fprintf(stderr, "wrong number of args\n");
    1.25 +	exit(-1);
    1.26 +    }
    1.27 +    
    1.28 +    curstr = argv[1];
    1.29 +    
    1.30 +    nextstr = strchr(curstr, '.');
    1.31 +    
    1.32 +    if ( nextstr == NULL ) {
    1.33 +	fprintf(stderr, "only one component\n");
    1.34 +	exit(-1);
    1.35 +    }
    1.36 +    
    1.37 +    *nextstr = '\0';
    1.38 +    firstval = atoi(curstr);
    1.39 +
    1.40 +    curstr = nextstr + 1;
    1.41 +    
    1.42 +    nextstr = strchr(curstr, '.');
    1.43 +
    1.44 +    if ( nextstr ) {
    1.45 +	*nextstr = '\0';
    1.46 +    }
    1.47 +
    1.48 +    secondval = atoi(curstr);
    1.49 +    
    1.50 +    if ( ( firstval < 0 ) || ( firstval > 2 ) ) {
    1.51 +	fprintf(stderr, "first component out of range\n");
    1.52 +	exit(-1);
    1.53 +	
    1.54 +    }
    1.55 +    
    1.56 +    if ( ( secondval < 0 ) || ( secondval > 39 ) ) {
    1.57 +	fprintf(stderr, "second component out of range\n");
    1.58 +	exit(-1);
    1.59 +    }
    1.60 +    
    1.61 +    printf("0x%x, ", ( firstval * 40 ) + secondval );
    1.62 +    while ( nextstr ) {
    1.63 +	curstr = nextstr + 1;
    1.64 +
    1.65 +	nextstr = strchr(curstr, '.');
    1.66 +
    1.67 +	if ( nextstr ) {
    1.68 +	    *nextstr = '\0';
    1.69 +	}
    1.70 +
    1.71 +	memset(buf, 0, sizeof(buf));
    1.72 +	val = atoi(curstr);
    1.73 +	count = 0;
    1.74 +	while ( val ) {
    1.75 +	    buf[count] = ( val & 0x7f );
    1.76 +	    val = val >> 7;
    1.77 +	    count++;
    1.78 +	}
    1.79 +
    1.80 +	while ( count-- ) {
    1.81 +	    if ( count ) {
    1.82 +		printf("0x%x, ", buf[count] | 0x80 );
    1.83 +	    } else {
    1.84 +		printf("0x%x, ", buf[count] );
    1.85 +	    }
    1.86 +	}
    1.87 +    }
    1.88 +    printf("\n");
    1.89 +    return 0;
    1.90 +}
    1.91 +

mercurial