Merge from gcc
[binutils.git] / libiberty / hex.c
blobe4c5dfa2f1dc6786a02d5a9875921d361bed47d3
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"
22 #include "safe-ctype.h" /* for HOST_CHARSET_ASCII */
24 #if EOF != -1
25 #error "hex.c requires EOF == -1"
26 #endif
30 @deftypefn Extension void hex_init (void)
32 Initializes the array mapping the current character set to
33 corresponding hex values. This function must be called before any
34 call to @code{hex_p} or @code{hex_value}. If you fail to call it, a
35 default ASCII-based table will normally be used on ASCII systems.
37 @end deftypefn
39 @deftypefn Extension int hex_p (int @var{c})
41 Evaluates to non-zero if the given character is a valid hex character,
42 or zero if it is not. Note that the value you pass will be cast to
43 @code{unsigned char} within the macro.
45 @end deftypefn
47 @deftypefn Extension unsigned int hex_value (int @var{c})
49 Returns the numeric equivalent of the given character when interpreted
50 as a hexidecimal digit. The result is undefined if you pass an
51 invalid hex digit. Note that the value you pass will be cast to
52 @code{unsigned char} within the macro.
54 The @code{hex_value} macro returns @code{unsigned int}, rather than
55 signed @code{int}, to make it easier to use in parsing addresses from
56 hex dump files: a signed @code{int} would be sign-extended when
57 converted to a wider unsigned type --- like @code{bfd_vma}, on some
58 systems.
60 @end deftypefn
62 @undocumented _hex_array_size
63 @undocumented _hex_bad
64 @undocumented _hex_value
69 /* Are we ASCII? */
70 #if HOST_CHARSET == HOST_CHARSET_ASCII
72 const unsigned char _hex_value[_hex_array_size] =
74 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* NUL SOH STX ETX */
75 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* EOT ENQ ACK BEL */
76 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* BS HT LF VT */
77 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FF CR SO SI */
78 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DLE DC1 DC2 DC3 */
79 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DC4 NAK SYN ETB */
80 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* CAN EM SUB ESC */
81 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FS GS RS US */
83 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* SP ! " # */
84 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* $ % & ' */
85 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* ( ) * + */
86 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* , - . / */
87 0, 1, 2, 3, /* 0 1 2 3 */
88 4, 5, 6, 7, /* 4 5 6 7 */
89 8, 9, _hex_bad, _hex_bad, /* 8 9 : ; */
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, /* \ ] ^ _ */
101 _hex_bad, 10, 11, 12, /* ` a b c */
102 13, 14, 15, _hex_bad, /* d e f g */
103 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* h i j k */
104 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* l m n o */
105 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* p q r s */
106 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* t u v w */
107 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* x y z { */
108 _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* | } ~ DEL */
110 /* The high half of unsigned char, all values are _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 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
139 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
140 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
141 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
142 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
143 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
144 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
145 _hex_bad, _hex_bad, _hex_bad, _hex_bad,
147 #define HEX_TABLE_INITIALIZED
149 #else
151 unsigned char _hex_value[_hex_array_size];
153 #endif /* not ASCII */
155 void
156 hex_init ()
158 #ifndef HEX_TABLE_INITIALIZED
159 int i;
161 for (i=0; i<_hex_array_size; i++)
163 switch (i)
165 case '0': _hex_value[i] = 0; break;
166 case '1': _hex_value[i] = 1; break;
167 case '2': _hex_value[i] = 2; break;
168 case '3': _hex_value[i] = 3; break;
169 case '4': _hex_value[i] = 4; break;
170 case '5': _hex_value[i] = 5; break;
171 case '6': _hex_value[i] = 6; break;
172 case '7': _hex_value[i] = 7; break;
173 case '8': _hex_value[i] = 8; break;
174 case '9': _hex_value[i] = 9; break;
176 case 'a': case 'A': _hex_value[i] = 10; break;
177 case 'b': case 'B': _hex_value[i] = 11; break;
178 case 'c': case 'C': _hex_value[i] = 12; break;
179 case 'd': case 'D': _hex_value[i] = 13; break;
180 case 'e': case 'E': _hex_value[i] = 14; break;
181 case 'f': case 'F': _hex_value[i] = 15; break;
183 default:
184 _hex_value[i] = _hex_bad;
185 break;
188 #endif