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.
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.
42 @deftypefn Extension unsigned 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 The @code{hex_value} macro returns @code{unsigned int}, rather than
50 signed @code{int}, to make it easier to use in parsing addresses from
51 hex dump files: a signed @code{int} would be sign-extended when
52 converted to a wider unsigned type --- like @code{bfd_vma}, on some
57 @undocumented _hex_array_size
58 @undocumented _hex_bad
59 @undocumented _hex_value
65 #if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
66 && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21 \
69 const unsigned char _hex_value
[_hex_array_size
] =
71 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* NUL SOH STX ETX */
72 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* EOT ENQ ACK BEL */
73 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* BS HT LF VT */
74 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* FF CR SO SI */
75 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* DLE DC1 DC2 DC3 */
76 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* DC4 NAK SYN ETB */
77 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* CAN EM SUB ESC */
78 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* FS GS RS US */
80 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* SP ! " # */
81 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* $ % & ' */
82 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* ( ) * + */
83 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* , - . / */
84 0, 1, 2, 3, /* 0 1 2 3 */
85 4, 5, 6, 7, /* 4 5 6 7 */
86 8, 9, _hex_bad
, _hex_bad
, /* 8 9 : ; */
87 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* < = > ? */
89 _hex_bad
, 10, 11, 12, /* @ A B C */
90 13, 14, 15, _hex_bad
, /* D E F G */
91 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* H I J K */
92 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* L M N O */
93 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* P Q R S */
94 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* T U V W */
95 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* X Y Z [ */
96 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* \ ] ^ _ */
98 _hex_bad
, 10, 11, 12, /* ` a b c */
99 13, 14, 15, _hex_bad
, /* d e f g */
100 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* h i j k */
101 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* l m n o */
102 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* p q r s */
103 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* t u v w */
104 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* x y z { */
105 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
, /* | } ~ DEL */
107 /* The high half of unsigned char, all values are _hex_bad. */
108 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
109 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
110 _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
,
117 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
118 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
119 _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
,
126 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
127 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
128 _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
,
135 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
136 _hex_bad
, _hex_bad
, _hex_bad
, _hex_bad
,
137 _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
,
144 #define HEX_TABLE_INITIALIZED
148 unsigned char _hex_value
[_hex_array_size
];
150 #endif /* not ASCII */
155 #ifndef HEX_TABLE_INITIALIZED
158 for (i
=0; i
<_hex_array_size
; i
++)
162 case '0': _hex_value
[i
] = 0; break;
163 case '1': _hex_value
[i
] = 1; break;
164 case '2': _hex_value
[i
] = 2; break;
165 case '3': _hex_value
[i
] = 3; break;
166 case '4': _hex_value
[i
] = 4; break;
167 case '5': _hex_value
[i
] = 5; break;
168 case '6': _hex_value
[i
] = 6; break;
169 case '7': _hex_value
[i
] = 7; break;
170 case '8': _hex_value
[i
] = 8; break;
171 case '9': _hex_value
[i
] = 9; break;
173 case 'a': case 'A': _hex_value
[i
] = 10; break;
174 case 'b': case 'B': _hex_value
[i
] = 11; break;
175 case 'c': case 'C': _hex_value
[i
] = 12; break;
176 case 'd': case 'D': _hex_value
[i
] = 13; break;
177 case 'e': case 'E': _hex_value
[i
] = 14; break;
178 case 'f': case 'F': _hex_value
[i
] = 15; break;
181 _hex_value
[i
] = _hex_bad
;