FSF GCC merge 02/23/03
[official-gcc.git] / libiberty / hex.c
blob5f8229253494de21455e83b86766ca9e5002de11
1 /* Hex character manipulation support.
2 Copyright (C) 1995, 2001 Free Software Foundation, Inc.
4 This file is part of the libiberty library.
5 Libiberty is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 Libiberty is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with libiberty; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdio.h> /* for EOF */
21 #include "libiberty.h"
25 @deftypefn Extension void hex_init (void)
27 Initializes the array mapping the current character set to
28 corresponding hex values. This function must be called before any
29 call to @code{hex_p} or @code{hex_value}. If you fail to call it, a
30 default ASCII-based table will normally be used on ASCII systems.
32 @end deftypefn
34 @deftypefn Extension int hex_p (int @var{c})
36 Evaluates to non-zero if the given character is a valid hex character,
37 or zero if it is not. Note that the value you pass will be cast to
38 @code{unsigned char} within the macro.
40 @end deftypefn
42 @deftypefn Extension int hex_value (int @var{c})
44 Returns the numeric equivalent of the given character when interpreted
45 as a hexidecimal digit. The result is undefined if you pass an
46 invalid hex digit. Note that the value you pass will be cast to
47 @code{unsigned char} within the macro.
49 @end deftypefn
51 @undocumented _hex_array_size
52 @undocumented _hex_bad
53 @undocumented _hex_value
58 /* Are we ASCII? */
59 #if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
60 && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21 \
61 && EOF == -1
63 const char _hex_value[_hex_array_size] =
65 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* NUL SOH STX ETX */
66 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* EOT ENQ ACK BEL */
67 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* BS HT LF VT */
68 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FF CR SO SI */
69 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DLE DC1 DC2 DC3 */
70 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DC4 NAK SYN ETB */
71 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* CAN EM SUB ESC */
72 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FS GS RS US */
74 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* SP ! " # */
75 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* $ % & ' */
76 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* ( ) * + */
77 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* , - . / */
78 0, 1, 2, 3, /* 0 1 2 3 */
79 4, 5, 6, 7, /* 4 5 6 7 */
80 8, 9, _hex_bad, _hex_bad, /* 8 9 : ; */
81 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* < = > ? */
83 _hex_bad, 10, 11, 12, /* @ A B C */
84 13, 14, 15, _hex_bad, /* D E F G */
85 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* H I J K */
86 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* L M N O */
87 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* P Q R S */
88 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* T U V W */
89 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* X Y Z [ */
90 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* \ ] ^ _ */
92 _hex_bad, 10, 11, 12, /* ` a b c */
93 13, 14, 15, _hex_bad, /* d e f g */
94 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* h i j k */
95 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* l m n o */
96 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* p q r s */
97 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* t u v w */
98 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* x y z { */
99 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* | } ~ DEL */
101 /* The high half of unsigned char, all values are _hex_bad. */
102 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
103 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
104 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
105 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
106 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
107 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
108 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
109 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
111 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
112 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
113 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
114 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
115 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
116 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
117 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
118 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
120 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
121 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
122 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
123 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
124 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
125 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
126 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
127 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
129 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
130 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
131 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
132 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
133 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
134 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
135 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
136 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
138 #define HEX_TABLE_INITIALIZED
140 #else
142 char _hex_value[_hex_array_size];
144 #endif /* not ASCII */
146 void
147 hex_init ()
149 #ifndef HEX_TABLE_INITIALIZED
150 int i;
152 for (i=0; i<_hex_array_size; i++)
154 switch (i)
156 case '0': _hex_value[i] = 0; break;
157 case '1': _hex_value[i] = 1; break;
158 case '2': _hex_value[i] = 2; break;
159 case '3': _hex_value[i] = 3; break;
160 case '4': _hex_value[i] = 4; break;
161 case '5': _hex_value[i] = 5; break;
162 case '6': _hex_value[i] = 6; break;
163 case '7': _hex_value[i] = 7; break;
164 case '8': _hex_value[i] = 8; break;
165 case '9': _hex_value[i] = 9; break;
167 case 'a': case 'A': _hex_value[i] = 10; break;
168 case 'b': case 'B': _hex_value[i] = 11; break;
169 case 'c': case 'C': _hex_value[i] = 12; break;
170 case 'd': case 'D': _hex_value[i] = 13; break;
171 case 'e': case 'E': _hex_value[i] = 14; break;
172 case 'f': case 'F': _hex_value[i] = 15; break;
174 default:
175 _hex_value[i] = _hex_bad;
176 break;
179 #endif