MFC r1.11 (HEAD):
[dragonfly.git] / lib / libiberty / hex.c
blob039871232c550f32bd15667546356156dd793348
1 /*
2 * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
25 * $DragonFly: src/lib/libiberty/hex.c,v 1.1 2004/10/23 12:15:21 joerg Exp $
28 #include <libiberty.h>
30 static const char hex_table[128] = {
31 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0,
35 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0,
37 1, 2, 3, 4, 5, 6, 7, 8,
38 9, 10, 0, 0, 0, 0, 0, 0,
39 0, 11, 12, 13, 14, 15, 16, 0,
40 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 11, 12, 13, 14, 15, 16, 0,
44 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0
48 #define HEX_TABLE_INITIALZED
50 #ifndef HEX_TABLE_INITIALZED
51 static char hex_table[128];
52 #endif
54 void
55 hex_init(void)
57 #ifndef HEX_TABLE_INITIALZED
58 hex_table['0'] = 1;
59 hex_table['1'] = 2;
60 hex_table['2'] = 3;
61 hex_table['3'] = 4;
62 hex_table['4'] = 5;
63 hex_table['5'] = 6;
64 hex_table['6'] = 7;
65 hex_table['7'] = 8;
66 hex_table['8'] = 9;
67 hex_table['9'] = 10;
68 hex_table['a'] = 11;
69 hex_table['b'] = 12;
70 hex_table['c'] = 13;
71 hex_table['d'] = 14;
72 hex_table['e'] = 15;
73 hex_table['f'] = 16;
74 hex_table['a'] = 11;
75 hex_table['b'] = 12;
76 hex_table['c'] = 13;
77 hex_table['d'] = 14;
78 hex_table['e'] = 15;
79 hex_table['f'] = 16;
80 hex_table['A'] = 11;
81 hex_table['B'] = 12;
82 hex_table['C'] = 13;
83 hex_table['D'] = 14;
84 hex_table['E'] = 15;
85 hex_table['F'] = 16;
86 #endif
89 int
90 hex_p(int c)
92 if ((c & ~127) != 0)
93 return(0);
94 return(hex_table[c]);
97 unsigned int
98 hex_value(int c)
100 if ((c & ~127) != 0)
101 return(0);
102 return(hex_table[c] - 1);