]>
Commit | Line | Data |
---|---|---|
7ba0088d A |
1 | /* $KAME: str2val.c,v 1.10 2001/04/03 15:51:57 thorpej 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 <sys/types.h> | |
33 | #include <sys/param.h> | |
34 | #include <ctype.h> | |
35 | ||
36 | #include <stdlib.h> | |
37 | #include <stdio.h> | |
38 | ||
39 | #include "str2val.h" | |
40 | #include "gcmalloc.h" | |
41 | ||
42 | /* | |
43 | * exchange a value to a hex string. | |
44 | * must free buffer allocated later. | |
45 | */ | |
46 | caddr_t | |
47 | val2str(buf, mlen) | |
48 | const char *buf; | |
49 | size_t mlen; | |
50 | { | |
51 | caddr_t new; | |
52 | size_t len = (mlen * 2) + mlen / 8 + 10; | |
53 | size_t i, j; | |
54 | ||
55 | if ((new = racoon_malloc(len)) == 0) return(0); | |
56 | ||
57 | for (i = 0, j = 0; i < mlen; i++) { | |
58 | snprintf(&new[j], len - j, "%02x", (u_char)buf[i]); | |
59 | j += 2; | |
60 | if (i % 8 == 7) { | |
61 | new[j++] = ' '; | |
62 | new[j] = '\0'; | |
63 | } | |
64 | } | |
65 | new[j] = '\0'; | |
66 | ||
67 | return(new); | |
68 | } | |
69 | ||
70 | /* | |
71 | * exchange a string based "base" to a value. | |
72 | */ | |
73 | char * | |
74 | str2val(str, base, len) | |
75 | const char *str; | |
76 | int base; | |
77 | size_t *len; | |
78 | { | |
79 | int f; | |
80 | size_t i; | |
81 | char *dst; | |
82 | char *rp; | |
83 | const char *p; | |
84 | char b[3]; | |
85 | ||
86 | i = 0; | |
87 | for (p = str; *p != '\0'; p++) { | |
88 | if (isxdigit(*p)) | |
89 | i++; | |
90 | else if (isspace(*p)) | |
91 | ; | |
92 | else | |
93 | return NULL; | |
94 | } | |
95 | if (i == 0 || (i % 2) != 0) | |
96 | return NULL; | |
97 | i /= 2; | |
98 | ||
99 | if ((dst = racoon_malloc(i)) == NULL) | |
100 | return NULL; | |
101 | ||
102 | i = 0; | |
103 | f = 0; | |
104 | for (rp = dst, p = str; *p != '\0'; p++) { | |
105 | if (isxdigit(*p)) { | |
106 | if (!f) { | |
107 | b[0] = *p; | |
108 | f = 1; | |
109 | } else { | |
110 | b[1] = *p; | |
111 | b[2] = '\0'; | |
112 | *rp++ = (char)strtol(b, NULL, base); | |
113 | i++; | |
114 | f = 0; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | *len = i; | |
120 | ||
121 | return(dst); | |
122 | } |