1 /*****************************************************************
2 * SQUID - a library of functions for biological sequence analysis
3 * Copyright (C) 1992-2002 Washington University School of Medicine
5 * This source code is freely distributed under the terms of the
6 * GNU General Public License. See the files COPYRIGHT and LICENSE
8 *****************************************************************/
12 * Finicky type checkers for strings. Return 1 (TRUE) if ok, 0 elsewise.
13 * Also, finicky type converters (sre_ntoh32() and friends)
15 * CVS $Id: types.c,v 1.5 2001/01/08 22:58:12 eddy Exp)
24 * Returns TRUE if s points to something that atoi() will parse
25 * completely and convert to an integer.
32 if (s == NULL) {squid_errno = SQERR_PARAMETER; return 0; }
35 while (isspace((int) (*s))) s++;
36 /* skip leading sign */
37 if (*s == '-' || *s == '+') s++;
38 /* skip leading conversion signals */
39 if ((strncmp(s, "0x", 2) == 0 && (int) strlen(s) > 2) ||
40 (strncmp(s, "0X", 2) == 0 && (int) strlen(s) > 2))
45 else if (*s == '0' && (int) strlen(s) > 1)
47 /* examine remainder for garbage chars */
51 if (!isdigit((int) (*s))) return 0;
57 if (!isxdigit((int) (*s))) return 0;
67 * Purpose: Returns TRUE if s is a string representation
68 * of a valid floating point number.
77 if (s == NULL) return 0;
79 while (isspace((int) (*s))) s++; /* skip leading whitespace */
80 if (*s == '-' || *s == '+') s++; /* skip leading sign */
82 /* Examine remainder for garbage. Allowed one '.' and
83 * one 'e' or 'E'; if both '.' and e/E occur, '.'
88 if (isdigit((int) (*s)))
92 if (gotdecimal) return 0; /* can't have two */
93 if (gotexp) return 0; /* e/E preceded . */
96 else if (*s == 'e' || *s == 'E')
98 if (gotexp) return 0; /* can't have two */
101 else if (isspace((int) (*s)))
107 while (isspace((int) (*s))) s++; /* skip trailing whitespace */
108 if (*s == '\0' && gotreal) return 1;
113 /* Function: Byteswap()
115 * Purpose: Swap between big-endian and little-endian.
117 * int foo = 0x12345678;
118 * byteswap((char *) &foo, sizeof(int));
119 * printf("%x\n", foo)
122 * I don't fully understand byte-swapping issues.
123 * However, I have tested this on chars through floats,
124 * on various machines:
125 * SGI IRIX 4.0.5, SunOS 4.1.3, DEC Alpha OSF/1, Alliant
127 * Date: Sun Feb 12 10:26:22 1995
130 Byteswap(char *swap, int nbytes)
135 for (x = 0; x < nbytes / 2; x++)
137 byte = swap[nbytes - x - 1];
138 swap[nbytes - x - 1] = swap[x];
145 /* Functions: sre_ntoh16(), etc.
146 * Date: SRE, Sun Dec 31 11:26:53 2000 [St. Louis]
148 * Purpose: Provide functionality of ntohs(), etc; extended
149 * to 64-bit unsigned ints, and explicitly provided
150 * in case a machine doesn't have the ntohs()
153 * If we're using the host functions,
154 * USE_HOST_BYTESWAP_FUNCTIONS was set to 1 in
155 * squidconf.h, and we #define'd sre_hton16(x)=hton(x), etc.
156 * in squid.h. In doing this, we assumed that the
157 * host functions work on 16- and 32-bit unsigned quantities.
158 * If for some reason that's not true, set
159 * USE_HOST_BYTESWAP_FUNCTIONS to 0.
161 #ifndef USE_HOST_BYTESWAP_FUNCTIONS
163 sre_ntoh16(sqd_uint16 netshort)
165 #ifdef WORDS_BIGENDIAN
168 Byteswap((char *) &netshort, 2);
173 sre_ntoh32(sqd_uint32 netlong)
175 #ifdef WORDS_BIGENDIAN
178 Byteswap((char *) &netlong, 4);
183 sre_hton16(sqd_uint16 hostshort)
185 #ifdef WORDS_BIGENDIAN
188 Byteswap((char *) &hostshort, 2);
193 sre_hton32(sqd_uint32 hostlong)
195 #ifdef WORDS_BIGENDIAN
198 Byteswap((char *) &hostlong, 4);
202 #endif /*USE_HOST_BYTESWAP_FUNCTIONS*/
205 sre_ntoh64(sqd_uint64 net_int64)
207 #ifdef WORDS_BIGENDIAN
210 Byteswap((char *) &net_int64, 8);
215 sre_hton64(sqd_uint64 host_int64)
217 #ifdef WORDS_BIGENDIAN
220 Byteswap((char *) &host_int64, 8);