Move decl. of rsa_sec_compute_root_tr to internal header.
[gd/nettle] / base16-decode.c
1 /* base16-encode.c
2
3    Hex decoding.
4
5    Copyright (C) 2002 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33  
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40
41 #include "base16.h"
42
43 void
44 base16_decode_init(struct base16_decode_ctx *ctx)
45 {
46   ctx->word = ctx->bits = 0;
47 }
48
49 enum { HEX_INVALID = -1, HEX_SPACE=-2 };
50
51 static const signed char
52 hex_decode_table[0x80] =
53   {
54     -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1, 
55     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56     -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
58     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62   };
63
64 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
65  * errors. */
66 int
67 base16_decode_single(struct base16_decode_ctx *ctx,
68                      uint8_t *dst,
69                      char src)
70 {
71   /* Avoid signed char for indexing. */
72   unsigned char usrc = src;
73   int digit;
74
75   if (usrc >= 0x80)
76     return -1;
77
78   digit = hex_decode_table[usrc];
79   switch (digit)
80     {
81     case -1:
82       return -1;
83     case -2:
84       return 0;
85     default:
86       assert(digit >= 0);
87       assert(digit < 0x10);
88
89       if (ctx->bits)
90         {
91           *dst = (ctx->word << 4) | digit;
92           ctx->bits = 0;
93           return 1;
94         }
95       else
96         {
97           ctx->word = digit;
98           ctx->bits = 4;
99           return 0;
100         }
101     }
102 }
103
104 int
105 base16_decode_update(struct base16_decode_ctx *ctx,
106                      size_t *dst_length,
107                      uint8_t *dst,
108                      size_t src_length,
109                      const char *src)
110 {
111   size_t done;
112   size_t i;
113
114   for (i = done = 0; i<src_length; i++)
115     switch(base16_decode_single(ctx, dst + done, src[i]))
116       {
117       case -1:
118         return 0;
119       case 1:
120         done++;
121         /* Fall through */
122       case 0:
123         break;
124       default:
125         abort();
126       }
127   
128   assert(done <= BASE16_DECODE_LENGTH(src_length));
129
130   *dst_length = done;
131   return 1;
132 }
133
134 int
135 base16_decode_final(struct base16_decode_ctx *ctx)
136 {
137   return ctx->bits == 0;
138 }