PageRenderTime 35ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/security/nss/cmd/oidcalc/oidcalc.c

http://github.com/zpao/v8monkey
C | 120 lines | 65 code | 20 blank | 35 comment | 15 complexity | c892b10ceffca91910cbbd72107468c1 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37#include <stdio.h>
 38#include <stdlib.h>
 39#include <string.h>
 40
 41int
 42main(int argc, char **argv)
 43{
 44    char *curstr;
 45    char *nextstr;
 46    unsigned int firstval;
 47    unsigned int secondval;
 48    unsigned int val;
 49    unsigned char buf[5];
 50    int count;
 51    
 52    if ( argc != 2 ) {
 53	fprintf(stderr, "wrong number of args\n");
 54	exit(-1);
 55    }
 56    
 57    curstr = argv[1];
 58    
 59    nextstr = strchr(curstr, '.');
 60    
 61    if ( nextstr == NULL ) {
 62	fprintf(stderr, "only one component\n");
 63	exit(-1);
 64    }
 65    
 66    *nextstr = '\0';
 67    firstval = atoi(curstr);
 68
 69    curstr = nextstr + 1;
 70    
 71    nextstr = strchr(curstr, '.');
 72
 73    if ( nextstr ) {
 74	*nextstr = '\0';
 75    }
 76
 77    secondval = atoi(curstr);
 78    
 79    if ( ( firstval < 0 ) || ( firstval > 2 ) ) {
 80	fprintf(stderr, "first component out of range\n");
 81	exit(-1);
 82	
 83    }
 84    
 85    if ( ( secondval < 0 ) || ( secondval > 39 ) ) {
 86	fprintf(stderr, "second component out of range\n");
 87	exit(-1);
 88    }
 89    
 90    printf("0x%x, ", ( firstval * 40 ) + secondval );
 91    while ( nextstr ) {
 92	curstr = nextstr + 1;
 93
 94	nextstr = strchr(curstr, '.');
 95
 96	if ( nextstr ) {
 97	    *nextstr = '\0';
 98	}
 99
100	memset(buf, 0, sizeof(buf));
101	val = atoi(curstr);
102	count = 0;
103	while ( val ) {
104	    buf[count] = ( val & 0x7f );
105	    val = val >> 7;
106	    count++;
107	}
108
109	while ( count-- ) {
110	    if ( count ) {
111		printf("0x%x, ", buf[count] | 0x80 );
112	    } else {
113		printf("0x%x, ", buf[count] );
114	    }
115	}
116    }
117    printf("\n");
118    return 0;
119}
120