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