[libc] Rewrite strtoul()
[ipxe.git] / src / core / base16.c
1 /*
2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <ipxe/string.h>
27 #include <ipxe/base16.h>
28
29 /** @file
30 *
31 * Base16 encoding
32 *
33 */
34
35 /**
36 * Base16-encode data
37 *
38 * @v raw Raw data
39 * @v len Length of raw data
40 * @v encoded Buffer for encoded string
41 *
42 * The buffer must be the correct length for the encoded string. Use
43 * something like
44 *
45 * char buf[ base16_encoded_len ( len ) + 1 ];
46 *
47 * (the +1 is for the terminating NUL) to provide a buffer of the
48 * correct size.
49 */
50 void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) {
51 const uint8_t *raw_bytes = raw;
52 char *encoded_bytes = encoded;
53 size_t remaining = len;
54
55 /* Encode each byte */
56 for ( ; remaining-- ; encoded_bytes += 2 ) {
57 sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
58 }
59
60 /* Ensure terminating NUL exists even if length was zero */
61 *encoded_bytes = '\0';
62
63 DBG ( "Base16-encoded to \"%s\":\n", encoded );
64 DBG_HDA ( 0, raw, len );
65 assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
66 }
67
68 /**
69 * Decode hexadecimal string
70 *
71 * @v encoded Encoded string
72 * @v separator Byte separator character, or 0 for no separator
73 * @v data Buffer
74 * @v len Length of buffer
75 * @ret len Length of data, or negative error
76 */
77 int hex_decode ( const char *encoded, char separator, void *data, size_t len ) {
78 uint8_t *out = data;
79 unsigned int count = 0;
80 unsigned int sixteens;
81 unsigned int units;
82
83 while ( *encoded ) {
84
85 /* Check separator, if applicable */
86 if ( count && separator && ( ( *(encoded++) != separator ) ) )
87 return -EINVAL;
88
89 /* Extract digits. Note that either digit may be NUL,
90 * which would be interpreted as an invalid value by
91 * digit_value(); there is therefore no need for an
92 * explicit end-of-string check.
93 */
94 sixteens = digit_value ( *(encoded++) );
95 if ( sixteens >= 16 )
96 return -EINVAL;
97 units = digit_value ( *(encoded++) );
98 if ( units >= 16 )
99 return -EINVAL;
100
101 /* Store result */
102 if ( count < len )
103 out[count] = ( ( sixteens << 4 ) | units );
104 count++;
105
106 }
107 return count;
108 }
109
110 /**
111 * Base16-decode data
112 *
113 * @v encoded Encoded string
114 * @v raw Raw data
115 * @ret len Length of raw data, or negative error
116 *
117 * The buffer must be large enough to contain the decoded data. Use
118 * something like
119 *
120 * char buf[ base16_decoded_max_len ( encoded ) ];
121 *
122 * to provide a buffer of the correct size.
123 */
124 int base16_decode ( const char *encoded, uint8_t *raw ) {
125 int len;
126
127 len = hex_decode ( encoded, 0, raw, -1UL );
128 if ( len < 0 )
129 return len;
130
131 DBG ( "Base16-decoded \"%s\" to:\n", encoded );
132 DBG_HDA ( 0, raw, len );
133 assert ( len <= ( int ) base16_decoded_max_len ( encoded ) );
134
135 return len;
136 }