source: rtems-libbsd/ipsec-tools/src/racoon/str2val.c @ b376ae1

55-freebsd-126-freebsd-12
Last change on this file since b376ae1 was b376ae1, checked in by Christian Mauderer <christian.mauderer@…>, on 05/03/18 at 12:15:11

ipsec-tools: Port libipsec, setkey and racoon.

Note that this replaces the libipsec from FreeBSD with the one provided
by ipsec-tools.

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