hex.c revision 169695
1169695Skan/* Hex character manipulation support. 2169695Skan Copyright (C) 1995, 2001 Free Software Foundation, Inc. 3169695Skan 4169695SkanThis file is part of the libiberty library. 5169695SkanLibiberty is free software; you can redistribute it and/or 6169695Skanmodify it under the terms of the GNU Library General Public 7169695SkanLicense as published by the Free Software Foundation; either 8169695Skanversion 2 of the License, or (at your option) any later version. 9169695Skan 10169695SkanLibiberty is distributed in the hope that it will be useful, 11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13169695SkanLibrary General Public License for more details. 14169695Skan 15169695SkanYou should have received a copy of the GNU Library General Public 16169695SkanLicense along with libiberty; see the file COPYING.LIB. If 17169695Skannot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 18169695SkanBoston, MA 02110-1301, USA. */ 19169695Skan 20169695Skan#include <stdio.h> /* for EOF */ 21169695Skan#ifdef HAVE_CONFIG_H 22169695Skan#include "config.h" 23169695Skan#endif 24169695Skan#include "libiberty.h" 25169695Skan#include "safe-ctype.h" /* for HOST_CHARSET_ASCII */ 26169695Skan 27169695Skan#if EOF != -1 28169695Skan #error "hex.c requires EOF == -1" 29169695Skan#endif 30169695Skan 31169695Skan/* 32169695Skan 33169695Skan@deftypefn Extension void hex_init (void) 34169695Skan 35169695SkanInitializes the array mapping the current character set to 36169695Skancorresponding hex values. This function must be called before any 37169695Skancall to @code{hex_p} or @code{hex_value}. If you fail to call it, a 38169695Skandefault ASCII-based table will normally be used on ASCII systems. 39169695Skan 40169695Skan@end deftypefn 41169695Skan 42169695Skan@deftypefn Extension int hex_p (int @var{c}) 43169695Skan 44169695SkanEvaluates to non-zero if the given character is a valid hex character, 45169695Skanor zero if it is not. Note that the value you pass will be cast to 46169695Skan@code{unsigned char} within the macro. 47169695Skan 48169695Skan@end deftypefn 49169695Skan 50169695Skan@deftypefn Extension {unsigned int} hex_value (int @var{c}) 51169695Skan 52169695SkanReturns the numeric equivalent of the given character when interpreted 53169695Skanas a hexidecimal digit. The result is undefined if you pass an 54169695Skaninvalid hex digit. Note that the value you pass will be cast to 55169695Skan@code{unsigned char} within the macro. 56169695Skan 57169695SkanThe @code{hex_value} macro returns @code{unsigned int}, rather than 58169695Skansigned @code{int}, to make it easier to use in parsing addresses from 59169695Skanhex dump files: a signed @code{int} would be sign-extended when 60169695Skanconverted to a wider unsigned type --- like @code{bfd_vma}, on some 61169695Skansystems. 62169695Skan 63169695Skan@end deftypefn 64169695Skan 65169695Skan@undocumented _hex_array_size 66169695Skan@undocumented _hex_bad 67169695Skan@undocumented _hex_value 68169695Skan 69169695Skan*/ 70169695Skan 71169695Skan 72169695Skan/* Are we ASCII? */ 73169695Skan#if HOST_CHARSET == HOST_CHARSET_ASCII 74169695Skan 75169695Skanconst unsigned char _hex_value[_hex_array_size] = 76169695Skan{ 77169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* NUL SOH STX ETX */ 78169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* EOT ENQ ACK BEL */ 79169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* BS HT LF VT */ 80169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FF CR SO SI */ 81169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DLE DC1 DC2 DC3 */ 82169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DC4 NAK SYN ETB */ 83169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* CAN EM SUB ESC */ 84169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FS GS RS US */ 85169695Skan 86169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* SP ! " # */ 87169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* $ % & ' */ 88169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* ( ) * + */ 89169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* , - . / */ 90169695Skan 0, 1, 2, 3, /* 0 1 2 3 */ 91169695Skan 4, 5, 6, 7, /* 4 5 6 7 */ 92169695Skan 8, 9, _hex_bad, _hex_bad, /* 8 9 : ; */ 93169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* < = > ? */ 94169695Skan 95169695Skan _hex_bad, 10, 11, 12, /* @ A B C */ 96169695Skan 13, 14, 15, _hex_bad, /* D E F G */ 97169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* H I J K */ 98169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* L M N O */ 99169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* P Q R S */ 100169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* T U V W */ 101169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* X Y Z [ */ 102169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* \ ] ^ _ */ 103169695Skan 104169695Skan _hex_bad, 10, 11, 12, /* ` a b c */ 105169695Skan 13, 14, 15, _hex_bad, /* d e f g */ 106169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* h i j k */ 107169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* l m n o */ 108169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* p q r s */ 109169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* t u v w */ 110169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* x y z { */ 111169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* | } ~ DEL */ 112169695Skan 113169695Skan /* The high half of unsigned char, all values are _hex_bad. */ 114169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 115169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 116169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 117169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 118169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 119169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 120169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 121169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 122169695Skan 123169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 124169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 125169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 126169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 127169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 128169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 129169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 130169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 131169695Skan 132169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 133169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 134169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 135169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 136169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 137169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 138169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 139169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 140169695Skan 141169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 142169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 143169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 144169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 145169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 146169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 147169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 148169695Skan _hex_bad, _hex_bad, _hex_bad, _hex_bad, 149169695Skan}; 150169695Skan#define HEX_TABLE_INITIALIZED 151169695Skan 152169695Skan#else 153169695Skan 154169695Skanunsigned char _hex_value[_hex_array_size]; 155169695Skan 156169695Skan#endif /* not ASCII */ 157169695Skan 158169695Skanvoid 159169695Skanhex_init (void) 160169695Skan{ 161169695Skan#ifndef HEX_TABLE_INITIALIZED 162169695Skan int i; 163169695Skan 164169695Skan for (i=0; i<_hex_array_size; i++) 165169695Skan { 166169695Skan switch (i) 167169695Skan { 168169695Skan case '0': _hex_value[i] = 0; break; 169169695Skan case '1': _hex_value[i] = 1; break; 170169695Skan case '2': _hex_value[i] = 2; break; 171169695Skan case '3': _hex_value[i] = 3; break; 172169695Skan case '4': _hex_value[i] = 4; break; 173169695Skan case '5': _hex_value[i] = 5; break; 174169695Skan case '6': _hex_value[i] = 6; break; 175169695Skan case '7': _hex_value[i] = 7; break; 176169695Skan case '8': _hex_value[i] = 8; break; 177169695Skan case '9': _hex_value[i] = 9; break; 178169695Skan 179169695Skan case 'a': case 'A': _hex_value[i] = 10; break; 180169695Skan case 'b': case 'B': _hex_value[i] = 11; break; 181169695Skan case 'c': case 'C': _hex_value[i] = 12; break; 182169695Skan case 'd': case 'D': _hex_value[i] = 13; break; 183169695Skan case 'e': case 'E': _hex_value[i] = 14; break; 184169695Skan case 'f': case 'F': _hex_value[i] = 15; break; 185169695Skan 186169695Skan default: 187169695Skan _hex_value[i] = _hex_bad; 188169695Skan break; 189169695Skan } 190169695Skan } 191169695Skan#endif 192169695Skan} 193