Home | History | Annotate | Download | only in racoon
      1 /*	$NetBSD: str2val.c,v 1.4 2006/09/09 16:22:10 manu Exp $	*/
      2 
      3 /*	$KAME: str2val.c,v 1.11 2001/08/16 14:37:29 itojun Exp $	*/
      4 
      5 /*
      6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the project nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include "config.h"
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <ctype.h>
     39 
     40 #include <stdlib.h>
     41 #include <stdio.h>
     42 
     43 #include "str2val.h"
     44 #include "gcmalloc.h"
     45 
     46 /*
     47  * exchange a value to a hex string.
     48  * must free buffer allocated later.
     49  */
     50 caddr_t
     51 val2str(buf, mlen)
     52 	const char *buf;
     53 	size_t mlen;
     54 {
     55 	caddr_t new;
     56 	size_t len = (mlen * 2) + mlen / 8 + 10;
     57 	size_t i, j;
     58 
     59 	if ((new = racoon_malloc(len)) == 0) return(0);
     60 
     61 	for (i = 0, j = 0; i < mlen; i++) {
     62 		snprintf(&new[j], len - j, "%02x", (u_char)buf[i]);
     63 		j += 2;
     64 		if (i % 8 == 7) {
     65 			new[j++] = ' ';
     66 			new[j] = '\0';
     67 		}
     68 	}
     69 	new[j] = '\0';
     70 
     71 	return(new);
     72 }
     73 
     74 /*
     75  * exchange a string based "base" to a value.
     76  */
     77 char *
     78 str2val(str, base, len)
     79 	const char *str;
     80 	int base;
     81 	size_t *len;
     82 {
     83 	int f;
     84 	size_t i;
     85 	char *dst;
     86 	char *rp;
     87 	const char *p;
     88 	char b[3];
     89 
     90 	i = 0;
     91 	for (p = str; *p != '\0'; p++) {
     92 		if (isxdigit((int)*p))
     93 			i++;
     94 		else if (isspace((int)*p))
     95 			;
     96 		else
     97 			return NULL;
     98 	}
     99 	if (i == 0 || (i % 2) != 0)
    100 		return NULL;
    101 	i /= 2;
    102 
    103 	if ((dst = racoon_malloc(i)) == NULL)
    104 		return NULL;
    105 
    106 	i = 0;
    107 	f = 0;
    108 	for (rp = dst, p = str; *p != '\0'; p++) {
    109 		if (isxdigit((int)*p)) {
    110 			if (!f) {
    111 				b[0] = *p;
    112 				f = 1;
    113 			} else {
    114 				b[1] = *p;
    115 				b[2] = '\0';
    116 				*rp++ = (char)strtol(b, NULL, base);
    117 				i++;
    118 				f = 0;
    119 			}
    120 		}
    121 	}
    122 
    123 	*len = i;
    124 
    125 	return(dst);
    126 }
    127