28acc40480f17b79242cadad379d653efb2d50c0
[platform/upstream/nettle.git] / 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                      uint8_t src)
70 {
71   int digit;
72
73   if (src >= 0x80)
74     return -1;
75
76   digit = hex_decode_table[src];
77   switch (digit)
78     {
79     case -1:
80       return -1;
81     case -2:
82       return 0;
83     default:
84       assert(digit >= 0);
85       assert(digit < 0x10);
86
87       if (ctx->bits)
88         {
89           *dst = (ctx->word << 4) | digit;
90           ctx->bits = 0;
91           return 1;
92         }
93       else
94         {
95           ctx->word = digit;
96           ctx->bits = 4;
97           return 0;
98         }
99     }
100 }
101
102 int
103 base16_decode_update(struct base16_decode_ctx *ctx,
104                      size_t *dst_length,
105                      uint8_t *dst,
106                      size_t src_length,
107                      const uint8_t *src)
108 {
109   size_t done;
110   size_t i;
111
112   for (i = done = 0; i<src_length; i++)
113     switch(base16_decode_single(ctx, dst + done, src[i]))
114       {
115       case -1:
116         return 0;
117       case 1:
118         done++;
119         /* Fall through */
120       case 0:
121         break;
122       default:
123         abort();
124       }
125   
126   assert(done <= BASE16_DECODE_LENGTH(src_length));
127
128   *dst_length = done;
129   return 1;
130 }
131
132 int
133 base16_decode_final(struct base16_decode_ctx *ctx)
134 {
135   return ctx->bits == 0;
136 }