mark the "short segment" message field as generated and add an expert_info to it
[obnox/wireshark/wip.git] / epan / g_ascii_strtoull.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /*
28  * $Id$
29  *
30  * "g_ascii_strtoull()" extracted from GLib 2.4.5, for use with GLibs
31  * that don't have it (e.g., GLib 1.2[.x]).
32  */
33
34 /*
35  * MT safe
36  */
37
38 #include <errno.h>
39
40 #include <glib.h>
41
42 #include "g_ascii_strtoull.h"
43
44 #ifndef G_MAXUINT64
45 #define G_MAXUINT64     ((guint64)G_GINT64_CONSTANT(0xFFFFFFFFFFFFFFFFU))
46 #endif
47
48 /**
49  * g_ascii_strtoull:
50  * @nptr:    the string to convert to a numeric value.
51  * @endptr:  if non-%NULL, it returns the character after
52  *           the last character used in the conversion.
53  * @base:    to be used for the conversion, 2..36 or 0
54  *
55  * Converts a string to a #guint64 value.
56  * This function behaves like the standard strtoull() function
57  * does in the C locale. It does this without actually
58  * changing the current locale, since that would not be
59  * thread-safe.
60  *
61  * This function is typically used when reading configuration
62  * files or other non-user input that should be locale independent.
63  * To handle input from the user you should normally use the
64  * locale-sensitive system strtoull() function.
65  *
66  * If the correct value would cause overflow, %G_MAXUINT64
67  * is returned, and %ERANGE is stored in %errno.
68  *
69  * Return value: the #guint64 value.
70  *
71  * Since: 2.2
72  **/
73 guint64
74 g_ascii_strtoull (const gchar *nptr,
75                   gchar      **endptr,
76                   guint        base)
77 {
78   /* this code is based on on the strtol(3) code from GNU libc released under
79    * the GNU Lesser General Public License.
80    *
81    * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
82    *        Free Software Foundation, Inc.
83    */
84 #define ISSPACE(c)              ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
85                                  (c) == '\r' || (c) == '\t' || (c) == '\v')
86 #define ISUPPER(c)              ((c) >= 'A' && (c) <= 'Z')
87 #define ISLOWER(c)              ((c) >= 'a' && (c) <= 'z')
88 #define ISALPHA(c)              (ISUPPER (c) || ISLOWER (c))
89 #define TOUPPER(c)              (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
90 #define TOLOWER(c)              (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
91   gboolean negative, overflow;
92   guint64 cutoff;
93   guint64 cutlim;
94   guint64 ui64;
95   const gchar *s, *save;
96   guchar c;
97   
98   if (base == 1 || base > 36)
99     {
100       errno = EINVAL;
101       return 0;
102     }
103   
104   save = s = nptr;
105   
106   /* Skip white space.  */
107   while (ISSPACE (*s))
108     ++s;
109   if (!*s)
110     goto noconv;
111   
112   /* Check for a sign.  */
113   negative = FALSE;
114   if (*s == '-')
115     {
116       negative = TRUE;
117       ++s;
118     }
119   else if (*s == '+')
120     ++s;
121   
122   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
123   if (*s == '0')
124     {
125       if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
126         {
127           s += 2;
128           base = 16;
129         }
130       else if (base == 0)
131         base = 8;
132     }
133   else if (base == 0)
134     base = 10;
135   
136   /* Save the pointer so we can check later if anything happened.  */
137   save = s;
138   cutoff = G_MAXUINT64 / base;
139   cutlim = G_MAXUINT64 % base;
140   
141   overflow = FALSE;
142   ui64 = 0;
143   c = *s;
144   for (; c; c = *++s)
145     {
146       if (c >= '0' && c <= '9')
147         c -= '0';
148       else if (ISALPHA (c))
149         c = TOUPPER (c) - 'A' + 10;
150       else
151         break;
152       if (c >= base)
153         break;
154       /* Check for overflow.  */
155       if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
156         overflow = TRUE;
157       else
158         {
159           ui64 *= base;
160           ui64 += c;
161         }
162     }
163   
164   /* Check if anything actually happened.  */
165   if (s == save)
166     goto noconv;
167   
168   /* Store in ENDPTR the address of one character
169      past the last character we converted.  */
170   if (endptr)
171     *endptr = (gchar*) s;
172   
173   if (overflow)
174     {
175       errno = ERANGE;
176       return G_MAXUINT64;
177     }
178   
179   /* Return the result of the appropriate sign.  */
180   return negative ? -ui64 : ui64;
181   
182  noconv:
183   /* We must handle a special case here: the base is 0 or 16 and the
184      first two characters are '0' and 'x', but the rest are no
185      hexadecimal digits.  This is no error case.  We return 0 and
186      ENDPTR points to the `x`.  */
187   if (endptr)
188     {
189       if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
190           && save[-2] == '0')
191         *endptr = (gchar*) &save[-1];
192       else
193         /*  There was no number to convert.  */
194         *endptr = (gchar*) nptr;
195     }
196   return 0;
197 }