1238384Sjkim/* Hex character manipulation support. 2238384Sjkim Copyright (C) 1995, 2001 Free Software Foundation, Inc. 3238384Sjkim 4238384SjkimThis file is part of the libiberty library. 5238384SjkimLibiberty is free software; you can redistribute it and/or 6238384Sjkimmodify it under the terms of the GNU Library General Public 7238384SjkimLicense as published by the Free Software Foundation; either 8238384Sjkimversion 2 of the License, or (at your option) any later version. 9238384Sjkim 10238384SjkimLibiberty is distributed in the hope that it will be useful, 11238384Sjkimbut WITHOUT ANY WARRANTY; without even the implied warranty of 12238384SjkimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13238384SjkimLibrary General Public License for more details. 14238384Sjkim 15238384SjkimYou should have received a copy of the GNU Library General Public 16238384SjkimLicense along with libiberty; see the file COPYING.LIB. If 17238384Sjkimnot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 18238384SjkimBoston, MA 02110-1301, USA. */ 19238384Sjkim 20238384Sjkim#include <stdio.h> /* for EOF */ 21238384Sjkim#ifdef HAVE_CONFIG_H 22238384Sjkim#include "config.h" 23238384Sjkim#endif 24238384Sjkim#include "libiberty.h" 25238384Sjkim#include "safe-ctype.h" /* for HOST_CHARSET_ASCII */ 26238384Sjkim 27238384Sjkim#if EOF != -1 28238384Sjkim #error "hex.c requires EOF == -1" 29238384Sjkim#endif 30238384Sjkim 31238384Sjkim/* 32238384Sjkim 33238384Sjkim@deftypefn Extension void hex_init (void) 34238384Sjkim 35238384SjkimInitializes the array mapping the current character set to 36238384Sjkimcorresponding hex values. This function must be called before any 37238384Sjkimcall to @code{hex_p} or @code{hex_value}. If you fail to call it, a 38238384Sjkimdefault ASCII-based table will normally be used on ASCII systems. 39238384Sjkim 40238384Sjkim@end deftypefn 41238384Sjkim 42238384Sjkim@deftypefn Extension int hex_p (int @var{c}) 43238384Sjkim 44238384SjkimEvaluates to non-zero if the given character is a valid hex character, 45238384Sjkimor zero if it is not. Note that the value you pass will be cast to 46238384Sjkim@code{unsigned char} within the macro. 47238384Sjkim 48238384Sjkim@end deftypefn 49238384Sjkim 50238384Sjkim@deftypefn Extension {unsigned int} hex_value (int @var{c}) 51238384Sjkim 52238384SjkimReturns the numeric equivalent of the given character when interpreted 53238384Sjkimas a hexidecimal digit. The result is undefined if you pass an 54238384Sjkiminvalid hex digit. Note that the value you pass will be cast to 55238384Sjkim@code{unsigned char} within the macro. 56238384Sjkim 57238384SjkimThe @code{hex_value} macro returns @code{unsigned int}, rather than 58238384Sjkimsigned @code{int}, to make it easier to use in parsing addresses from 59238384Sjkimhex dump files: a signed @code{int} would be sign-extended when 60238384Sjkimconverted to a wider unsigned type --- like @code{bfd_vma}, on some 61238384Sjkimsystems. 62238384Sjkim 63238384Sjkim@end deftypefn 64238384Sjkim 65238384Sjkim@undocumented _hex_array_size 66238384Sjkim@undocumented _hex_bad 67238384Sjkim@undocumented _hex_value 68238384Sjkim 69238384Sjkim*/ 70238384Sjkim 71238384Sjkim 72238384Sjkim/* Are we ASCII? */ 73238384Sjkim#if HOST_CHARSET == HOST_CHARSET_ASCII 74238384Sjkim 75238384Sjkimconst unsigned char _hex_value[_hex_array_size] = 76238384Sjkim{ 77238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* NUL SOH STX ETX */ 78238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* EOT ENQ ACK BEL */ 79238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* BS HT LF VT */ 80238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FF CR SO SI */ 81238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DLE DC1 DC2 DC3 */ 82238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* DC4 NAK SYN ETB */ 83238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* CAN EM SUB ESC */ 84238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* FS GS RS US */ 85238384Sjkim 86238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* SP ! " # */ 87238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* $ % & ' */ 88238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* ( ) * + */ 89238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* , - . / */ 90238384Sjkim 0, 1, 2, 3, /* 0 1 2 3 */ 91238384Sjkim 4, 5, 6, 7, /* 4 5 6 7 */ 92238384Sjkim 8, 9, _hex_bad, _hex_bad, /* 8 9 : ; */ 93238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* < = > ? */ 94238384Sjkim 95238384Sjkim _hex_bad, 10, 11, 12, /* @ A B C */ 96238384Sjkim 13, 14, 15, _hex_bad, /* D E F G */ 97238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* H I J K */ 98238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* L M N O */ 99238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* P Q R S */ 100238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* T U V W */ 101238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* X Y Z [ */ 102238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* \ ] ^ _ */ 103238384Sjkim 104238384Sjkim _hex_bad, 10, 11, 12, /* ` a b c */ 105238384Sjkim 13, 14, 15, _hex_bad, /* d e f g */ 106238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* h i j k */ 107238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* l m n o */ 108238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* p q r s */ 109238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* t u v w */ 110238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* x y z { */ 111238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, /* | } ~ DEL */ 112238384Sjkim 113238384Sjkim /* The high half of unsigned char, all values are _hex_bad. */ 114238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 115238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 116238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 117238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 118238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 119238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 120238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 121238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 122238384Sjkim 123238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 124238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 125238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 126238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 127238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 128238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 129238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 130238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 131238384Sjkim 132238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 133238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 134238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 135238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 136238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 137238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 138238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 139238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 140238384Sjkim 141238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 142238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 143238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 144238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 145238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 146238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 147238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 148238384Sjkim _hex_bad, _hex_bad, _hex_bad, _hex_bad, 149238384Sjkim}; 150238384Sjkim#define HEX_TABLE_INITIALIZED 151238384Sjkim 152238384Sjkim#else 153238384Sjkim 154238384Sjkimunsigned char _hex_value[_hex_array_size]; 155238384Sjkim 156238384Sjkim#endif /* not ASCII */ 157238384Sjkim 158238384Sjkimvoid 159238384Sjkimhex_init (void) 160238384Sjkim{ 161238384Sjkim#ifndef HEX_TABLE_INITIALIZED 162238384Sjkim int i; 163238384Sjkim 164238384Sjkim for (i=0; i<_hex_array_size; i++) 165238384Sjkim { 166238384Sjkim switch (i) 167238384Sjkim { 168238384Sjkim case '0': _hex_value[i] = 0; break; 169238384Sjkim case '1': _hex_value[i] = 1; break; 170238384Sjkim case '2': _hex_value[i] = 2; break; 171238384Sjkim case '3': _hex_value[i] = 3; break; 172238384Sjkim case '4': _hex_value[i] = 4; break; 173238384Sjkim case '5': _hex_value[i] = 5; break; 174238384Sjkim case '6': _hex_value[i] = 6; break; 175238384Sjkim case '7': _hex_value[i] = 7; break; 176238384Sjkim case '8': _hex_value[i] = 8; break; 177238384Sjkim case '9': _hex_value[i] = 9; break; 178238384Sjkim 179238384Sjkim case 'a': case 'A': _hex_value[i] = 10; break; 180238384Sjkim case 'b': case 'B': _hex_value[i] = 11; break; 181238384Sjkim case 'c': case 'C': _hex_value[i] = 12; break; 182238384Sjkim case 'd': case 'D': _hex_value[i] = 13; break; 183238384Sjkim case 'e': case 'E': _hex_value[i] = 14; break; 184238384Sjkim case 'f': case 'F': _hex_value[i] = 15; break; 185238384Sjkim 186238384Sjkim default: 187238384Sjkim _hex_value[i] = _hex_bad; 188238384Sjkim break; 189238384Sjkim } 190238384Sjkim } 191238384Sjkim#endif 192238384Sjkim} 193238384Sjkim