File: types.c

package info (click to toggle)
biosquid 1.9g%2Bcvs20050121-15.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,632 kB
  • sloc: ansic: 12,750; sh: 1,412; perl: 243; makefile: 231
file content (224 lines) | stat: -rw-r--r-- 4,904 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*****************************************************************
 * @LICENSE@
 *****************************************************************/

/* file: types.c
 * 
 * Finicky type checkers for strings. Return 1 (TRUE) if ok, 0 elsewise.
 * Also, finicky type converters (sre_ntoh32() and friends)
 *
 * CVS $Id: types.c,v 1.6 2003/04/14 16:00:16 eddy Exp $
 */

#include "squidconf.h"

#include <string.h>
#include <ctype.h>
#include "squid.h"

/* Function: IsInt()
 * 
 * Returns TRUE if s points to something that atoi() will parse
 * completely and convert to an integer.
 */
int
IsInt(char *s)
{
  int hex = 0;

  if (s == NULL) {squid_errno = SQERR_PARAMETER; return 0; }

				/* skip whitespace */
  while (isspace((int) (*s))) s++;      
				/* skip leading sign */
  if (*s == '-' || *s == '+') s++;
				/* skip leading conversion signals */
  if ((strncmp(s, "0x", 2) == 0 && (int) strlen(s) > 2) ||
      (strncmp(s, "0X", 2) == 0 && (int) strlen(s) > 2))
    {
      s += 2;
      hex = 1;
    }
  else if (*s == '0' && (int) strlen(s) > 1)
    s++;
				/* examine remainder for garbage chars */
  if (!hex)
    while (*s != '\0')
      {
	if (!isdigit((int) (*s))) return 0;
	s++;
      }
  else
    while (*s != '\0')
      {
	if (!isxdigit((int) (*s))) return 0;
	s++;
      }

  return 1;
}


/* Function: IsReal()
 * 
 * Purpose:  Returns TRUE if s is a string representation
 *           of a valid floating point number.
 */
int
IsReal(char *s)
{
  int gotdecimal = 0;
  int gotexp     = 0;
  int gotreal    = 0;

  if (s == NULL) return 0;

  while (isspace((int) (*s))) s++;         /* skip leading whitespace */
  if (*s == '-' || *s == '+') s++; /* skip leading sign */

  /* Examine remainder for garbage. Allowed one '.' and
   * one 'e' or 'E'; if both '.' and e/E occur, '.'
   * must be first.
   */
  while (*s != '\0')
    {
      if (isdigit((int) (*s))) 
	gotreal++;
      else if (*s == '.')
	{
	  if (gotdecimal) return 0; /* can't have two */
	  if (gotexp) return 0;	/* e/E preceded . */
	  else gotdecimal++;
	}
      else if (*s == 'e' || *s == 'E')
	{
	  if (gotexp) return 0;	/* can't have two */
	  else gotexp++;
	}
      else if (isspace((int) (*s)))
	break;

      s++;
    }

  while (isspace((int) (*s))) s++;         /* skip trailing whitespace */
  if (*s == '\0' && gotreal) return 1;
  else return 0;
}


/* Function: Byteswap()
 * 
 * Purpose:  Swap between big-endian and little-endian.
 *           For example:
 *               int foo = 0x12345678;
 *               byteswap((char *) &foo, sizeof(int));
 *               printf("%x\n", foo)
 *           gives 78563412.
 *           
 *           I don't fully understand byte-swapping issues.
 *           However, I have tested this on chars through floats,
 *           on various machines:
 *               SGI IRIX 4.0.5, SunOS 4.1.3, DEC Alpha OSF/1, Alliant
 *
 * Date: Sun Feb 12 10:26:22 1995              
 */
void
Byteswap(char *swap, int nbytes)
{
  int  x;
  char byte;
  
  for (x = 0; x < nbytes / 2; x++)
    {
      byte = swap[nbytes - x - 1];
      swap[nbytes - x - 1] = swap[x];
      swap[x] = byte;
    }
}



/* Functions: sre_ntoh16(), etc.
 * Date:      SRE, Sun Dec 31 11:26:53 2000 [St. Louis]
 *
 * Purpose:   Provide functionality of ntohs(), etc; extended
 *            to 64-bit unsigned ints, and explicitly provided
 *            in case a machine doesn't have the ntohs()
 *            family. 
 *            
 *            If we're using the host functions, 
 *            USE_HOST_BYTESWAP_FUNCTIONS was set to 1 in
 *            squidconf.h, and we #define'd sre_hton16(x)=hton(x), etc.
 *            in squid.h. In doing this, we assumed that the
 *            host functions work on 16- and 32-bit unsigned quantities.
 *            If for some reason that's not true, set 
 *            USE_HOST_BYTESWAP_FUNCTIONS to 0.
 */
#ifndef USE_HOST_BYTESWAP_FUNCTIONS
sqd_uint16
sre_ntoh16(sqd_uint16 netshort)
{
#ifdef WORDS_BIGENDIAN
  return netshort;
#else
  Byteswap((char *) &netshort, 2);
  return netshort;
#endif
}
sqd_uint32
sre_ntoh32(sqd_uint32 netlong)
{
#ifdef WORDS_BIGENDIAN
  return netlong;
#else
  Byteswap((char *) &netlong, 4);
  return netlong;
#endif
}
sqd_uint16
sre_hton16(sqd_uint16 hostshort)
{
#ifdef WORDS_BIGENDIAN
  return hostshort;
#else
  Byteswap((char *) &hostshort, 2);
  return hostshort;
#endif
}
sqd_uint32
sre_hton32(sqd_uint32 hostlong)
{
#ifdef WORDS_BIGENDIAN
  return hostlong;
#else
  Byteswap((char *) &hostlong, 4);
  return hostlong;
#endif
}
#endif /*USE_HOST_BYTESWAP_FUNCTIONS*/

sqd_uint64
sre_ntoh64(sqd_uint64 net_int64)
{
#ifdef WORDS_BIGENDIAN
  return net_int64;
#else
  Byteswap((char *) &net_int64, 8);
  return net_int64;
#endif
}
sqd_uint64
sre_hton64(sqd_uint64 host_int64)
{
#ifdef WORDS_BIGENDIAN
  return host_int64;
#else
  Byteswap((char *) &host_int64, 8);
  return host_int64;
#endif
}