Back to index

cell-binutils  2.17cvs20070401
hex.c
Go to the documentation of this file.
00001 /* Hex character manipulation support.
00002    Copyright (C) 1995, 2001 Free Software Foundation, Inc.
00003 
00004 This file is part of the libiberty library.
00005 Libiberty is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 Libiberty is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with libiberty; see the file COPYING.LIB.  If
00017 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00018 Boston, MA 02110-1301, USA.  */
00019 
00020 #include <stdio.h>  /* for EOF */
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 #include "libiberty.h"
00025 #include "safe-ctype.h" /* for HOST_CHARSET_ASCII */
00026 
00027 #if EOF != -1
00028  #error "hex.c requires EOF == -1"
00029 #endif
00030 
00031 /*
00032 
00033 @deftypefn Extension void hex_init (void)
00034 
00035 Initializes the array mapping the current character set to
00036 corresponding hex values.  This function must be called before any
00037 call to @code{hex_p} or @code{hex_value}.  If you fail to call it, a
00038 default ASCII-based table will normally be used on ASCII systems.
00039 
00040 @end deftypefn
00041 
00042 @deftypefn Extension int hex_p (int @var{c})
00043 
00044 Evaluates to non-zero if the given character is a valid hex character,
00045 or zero if it is not.  Note that the value you pass will be cast to
00046 @code{unsigned char} within the macro.
00047 
00048 @end deftypefn
00049 
00050 @deftypefn Extension {unsigned int} hex_value (int @var{c})
00051 
00052 Returns the numeric equivalent of the given character when interpreted
00053 as a hexadecimal digit.  The result is undefined if you pass an
00054 invalid hex digit.  Note that the value you pass will be cast to
00055 @code{unsigned char} within the macro.
00056 
00057 The @code{hex_value} macro returns @code{unsigned int}, rather than
00058 signed @code{int}, to make it easier to use in parsing addresses from
00059 hex dump files: a signed @code{int} would be sign-extended when
00060 converted to a wider unsigned type --- like @code{bfd_vma}, on some
00061 systems.
00062 
00063 @end deftypefn
00064 
00065 @undocumented _hex_array_size
00066 @undocumented _hex_bad
00067 @undocumented _hex_value
00068 
00069 */
00070 
00071 
00072 /* Are we ASCII? */
00073 #if HOST_CHARSET == HOST_CHARSET_ASCII
00074 
00075 const unsigned char _hex_value[_hex_array_size] =
00076 {
00077   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* NUL SOH STX ETX */
00078   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* EOT ENQ ACK BEL */
00079   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* BS  HT  LF  VT  */
00080   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* FF  CR  SO  SI  */
00081   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* DLE DC1 DC2 DC3 */
00082   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* DC4 NAK SYN ETB */
00083   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* CAN EM  SUB ESC */
00084   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* FS  GS  RS  US  */
00085 
00086   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* SP  !   "   #   */
00087   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* $   %   &   '   */
00088   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* (   )   *   +   */
00089   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* ,   -   .   /   */
00090   0,        1,        2,        3,          /* 0   1   2   3   */
00091   4,        5,        6,        7,          /* 4   5   6   7   */
00092   8,        9,        _hex_bad, _hex_bad,   /* 8   9   :   ;   */
00093   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* <   =   >   ?   */
00094 
00095   _hex_bad, 10,       11,       12,         /* @   A   B   C   */
00096   13,       14,       15,       _hex_bad,   /* D   E   F   G   */
00097   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* H   I   J   K   */
00098   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* L   M   N   O   */
00099   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* P   Q   R   S   */
00100   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* T   U   V   W   */
00101   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* X   Y   Z   [   */
00102   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* \   ]   ^   _   */
00103 
00104   _hex_bad, 10,       11,       12,         /* `   a   b   c   */
00105   13,       14,       15,       _hex_bad,   /* d   e   f   g   */
00106   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* h   i   j   k   */
00107   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* l   m   n   o   */
00108   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* p   q   r   s   */
00109   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* t   u   v   w   */
00110   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* x   y   z   {   */
00111   _hex_bad, _hex_bad, _hex_bad, _hex_bad,   /* |   }   ~   DEL */
00112 
00113   /* The high half of unsigned char, all values are _hex_bad.  */
00114   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00115   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00116   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00117   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00118   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00119   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00120   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00121   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00122 
00123   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00124   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00125   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00126   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00127   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00128   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00129   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00130   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00131 
00132   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00133   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00134   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00135   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00136   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00137   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00138   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00139   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00140 
00141   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00142   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00143   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00144   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00145   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00146   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00147   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00148   _hex_bad, _hex_bad, _hex_bad, _hex_bad,
00149 };
00150 #define HEX_TABLE_INITIALIZED
00151 
00152 #else
00153 
00154 unsigned char _hex_value[_hex_array_size];
00155 
00156 #endif /* not ASCII */
00157 
00158 void
00159 hex_init (void)
00160 {
00161 #ifndef HEX_TABLE_INITIALIZED
00162   int i;
00163 
00164   for (i=0; i<_hex_array_size; i++)
00165     {
00166       switch (i)
00167        {
00168        case '0': _hex_value[i] = 0; break;
00169        case '1': _hex_value[i] = 1; break;
00170        case '2': _hex_value[i] = 2; break;
00171        case '3': _hex_value[i] = 3; break;
00172        case '4': _hex_value[i] = 4; break;
00173        case '5': _hex_value[i] = 5; break;
00174        case '6': _hex_value[i] = 6; break;
00175        case '7': _hex_value[i] = 7; break;
00176        case '8': _hex_value[i] = 8; break;
00177        case '9': _hex_value[i] = 9; break;
00178 
00179        case 'a': case 'A': _hex_value[i] = 10; break;
00180        case 'b': case 'B': _hex_value[i] = 11; break;
00181        case 'c': case 'C': _hex_value[i] = 12; break;
00182        case 'd': case 'D': _hex_value[i] = 13; break;
00183        case 'e': case 'E': _hex_value[i] = 14; break;
00184        case 'f': case 'F': _hex_value[i] = 15; break;
00185 
00186        default:
00187          _hex_value[i] = _hex_bad;
00188          break;
00189        }
00190     }
00191 #endif
00192 }