]> sjero.net Git - wget/blob - src/safe-ctype.c
[svn] Merge of fix for bugs 20341 and 20410.
[wget] / src / safe-ctype.c
1 /* <ctype.h> replacement macros.
2
3    Copyright (C) 2000 Free Software Foundation, Inc.
4    Contributed by Zack Weinberg <zackw@stanford.edu>.
5
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
11
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty.  If not, see <http://www.gnu.org/licenses/>.
19
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables.  You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL".  If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so.  If you do not wish to do
28 so, delete this exception statement from your version.  */
29
30 /* This is a compatible replacement of the standard C library's <ctype.h>
31    with the following properties:
32
33    - Implements all isxxx() macros required by C99.
34    - Also implements some character classes useful when
35      parsing C-like languages.
36    - Does not change behavior depending on the current locale.
37    - Behaves properly for all values in the range of a signed or
38      unsigned char.  */
39
40 #include <config.h>
41 #include <safe-ctype.h>
42 #include <stdio.h>  /* for EOF */
43
44 /* Shorthand */
45 #define bl _sch_isblank
46 #define cn _sch_iscntrl
47 #define di _sch_isdigit
48 #define is _sch_isidst
49 #define lo _sch_islower
50 #define nv _sch_isnvsp
51 #define pn _sch_ispunct
52 #define pr _sch_isprint
53 #define sp _sch_isspace
54 #define up _sch_isupper
55 #define vs _sch_isvsp
56 #define xd _sch_isxdigit
57
58 /* Masks.  */
59 #define L  lo|is   |pr  /* lower case letter */
60 #define XL lo|is|xd|pr  /* lowercase hex digit */
61 #define U  up|is   |pr  /* upper case letter */
62 #define XU up|is|xd|pr  /* uppercase hex digit */
63 #define D  di   |xd|pr  /* decimal digit */
64 #define P  pn      |pr  /* punctuation */
65 #define _  pn|is   |pr  /* underscore */
66
67 #define C           cn  /* control character */
68 #define Z  nv      |cn  /* NUL */
69 #define M  nv|sp   |cn  /* cursor movement: \f \v */
70 #define V  vs|sp   |cn  /* vertical space: \r \n */
71 #define T  nv|sp|bl|cn  /* tab */
72 #define S  nv|sp|bl|pr  /* space */
73
74 /* Are we ASCII? */
75 #if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
76   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21 \
77   && EOF == -1
78
79 const unsigned short _sch_istable[256] =
80 {
81   Z,  C,  C,  C,   C,  C,  C,  C,   /* NUL SOH STX ETX  EOT ENQ ACK BEL */
82   C,  T,  V,  M,   M,  V,  C,  C,   /* BS  HT  LF  VT   FF  CR  SO  SI  */
83   C,  C,  C,  C,   C,  C,  C,  C,   /* DLE DC1 DC2 DC3  DC4 NAK SYN ETB */
84   C,  C,  C,  C,   C,  C,  C,  C,   /* CAN EM  SUB ESC  FS  GS  RS  US  */
85   S,  P,  P,  P,   P,  P,  P,  P,   /* SP  !   "   #    $   %   &   '   */
86   P,  P,  P,  P,   P,  P,  P,  P,   /* (   )   *   +    ,   -   .   /   */
87   D,  D,  D,  D,   D,  D,  D,  D,   /* 0   1   2   3    4   5   6   7   */
88   D,  D,  P,  P,   P,  P,  P,  P,   /* 8   9   :   ;    <   =   >   ?   */
89   P, XU, XU, XU,  XU, XU, XU,  U,   /* @   A   B   C    D   E   F   G   */
90   U,  U,  U,  U,   U,  U,  U,  U,   /* H   I   J   K    L   M   N   O   */
91   U,  U,  U,  U,   U,  U,  U,  U,   /* P   Q   R   S    T   U   V   W   */
92   U,  U,  U,  P,   P,  P,  P,  _,   /* X   Y   Z   [    \   ]   ^   _   */
93   P, XL, XL, XL,  XL, XL, XL,  L,   /* `   a   b   c    d   e   f   g   */
94   L,  L,  L,  L,   L,  L,  L,  L,   /* h   i   j   k    l   m   n   o   */
95   L,  L,  L,  L,   L,  L,  L,  L,   /* p   q   r   s    t   u   v   w   */
96   L,  L,  L,  P,   P,  P,  P,  C,   /* x   y   z   {    |   }   ~   DEL */
97
98   /* high half of unsigned char is locale-specific, so all tests are
99      false in "C" locale */
100   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
101   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
102   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
103   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
104
105   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
106   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
107   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
108   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
109 };
110
111 const unsigned char _sch_tolower[256] =
112 {
113    0,  1,  2,  3,   4,  5,  6,  7,   8,  9, 10, 11,  12, 13, 14, 15,
114   16, 17, 18, 19,  20, 21, 22, 23,  24, 25, 26, 27,  28, 29, 30, 31,
115   32, 33, 34, 35,  36, 37, 38, 39,  40, 41, 42, 43,  44, 45, 46, 47,
116   48, 49, 50, 51,  52, 53, 54, 55,  56, 57, 58, 59,  60, 61, 62, 63,
117   64,
118
119   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
120   'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
121
122   91, 92, 93, 94, 95, 96,
123
124   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
125   'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
126
127  123,124,125,126,127,
128
129  128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
130  144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
131  160,161,162,163, 164,165,166,167, 168,169,170,171, 172,173,174,175,
132  176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,190,191,
133
134  192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207,
135  208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
136  224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
137  240,241,242,243, 244,245,246,247, 248,249,250,251, 252,253,254,255,
138 };
139
140 const unsigned char _sch_toupper[256] =
141 {
142    0,  1,  2,  3,   4,  5,  6,  7,   8,  9, 10, 11,  12, 13, 14, 15,
143   16, 17, 18, 19,  20, 21, 22, 23,  24, 25, 26, 27,  28, 29, 30, 31,
144   32, 33, 34, 35,  36, 37, 38, 39,  40, 41, 42, 43,  44, 45, 46, 47,
145   48, 49, 50, 51,  52, 53, 54, 55,  56, 57, 58, 59,  60, 61, 62, 63,
146   64,
147
148   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
149   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
150
151   91, 92, 93, 94, 95, 96,
152
153   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
154   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
155
156  123,124,125,126,127,
157
158  128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
159  144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
160  160,161,162,163, 164,165,166,167, 168,169,170,171, 172,173,174,175,
161  176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,190,191,
162
163  192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207,
164  208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
165  224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
166  240,241,242,243, 244,245,246,247, 248,249,250,251, 252,253,254,255,
167 };
168
169 #else
170  #error "Unsupported host character set"
171 #endif /* not ASCII */