OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / lib / atoul.c
1 /*
2  * convert from ASCII form of unsigned long to binary
3  * Copyright (C) 1998, 1999  Henry Spencer.
4  * 
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Library General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.  See <http://www.fsf.org/copyleft/lgpl.txt>.
9  * 
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
13  * License for more details.
14  *
15  * RCSID $Id: atoul.c,v 1.6 1999/12/07 05:00:52 henry Exp $
16  */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21  - atoul - convert ASCII substring to unsigned long number
22  */
23 const char *                    /* NULL for success, else string literal */
24 atoul(src, srclen, base, resultp)
25 const char *src;
26 size_t srclen;                  /* 0 means strlen(src) */
27 int base;                       /* 0 means figure it out */
28 unsigned long *resultp;
29 {
30         const char *stop;
31         static char hex[] = "0123456789abcdef";
32         static char uchex[] = "0123456789ABCDEF";
33         int d;
34         char c;
35         char *p;
36         unsigned long r;
37         unsigned long rlimit;
38         int dlimit;
39
40         if (srclen == 0)
41                 srclen = strlen(src);
42         if (srclen == 0)
43                 return "empty string";
44
45         if (base == 0 || base == 13) {
46                 if (srclen > 2 && *src == '0' && CIEQ(*(src+1), 'x'))
47                         return atoul(src+2, srclen-2, 16, resultp);
48                 if (srclen > 1 && *src == '0' && base != 13)
49                         return atoul(src+1, srclen-1, 8, resultp);
50                 return atoul(src, srclen, 10, resultp);
51         }
52         if (base != 8 && base != 10 && base != 16)
53                 return "unsupported number base";
54
55         r = 0;
56         stop = src + srclen;
57         if (base == 16) {
58                 while (src < stop) {
59                         c = *src++;
60                         p = strchr(hex, c);
61                         if (p != NULL)
62                                 d = p - hex;
63                         else {
64                                 p = strchr(uchex, c);
65                                 if (p == NULL)
66                                         return "non-hex-digit in hex number";
67                                 d = p - uchex;
68                         }
69                         r = (r << 4) | d;
70                 }
71                 /* defer length check to catch invalid digits first */
72                 if (srclen > sizeof(unsigned long) * 2)
73                         return "hex number too long";
74         } else {
75                 rlimit = ULONG_MAX / base;
76                 dlimit = (int)(ULONG_MAX - rlimit*base);
77                 while (src < stop) {
78                         c = *src++;
79                         d = c - '0';
80                         if (d < 0 || d >= base)
81                                 return "non-digit in number";
82                         if (r > rlimit || (r == rlimit && d > dlimit))
83                                 return "unsigned-long overflow";
84                         r = r*base + d;
85                 }
86         }
87
88         *resultp = r;
89         return NULL;
90 }