2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/base16.h>
38 * @v len Length of raw data
39 * @v encoded Buffer for encoded string
41 * The buffer must be the correct length for the encoded string. Use
44 * char buf[ base16_encoded_len ( len ) + 1 ];
46 * (the +1 is for the terminating NUL) to provide a buffer of the
49 void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) {
50 const uint8_t *raw_bytes = raw;
51 char *encoded_bytes = encoded;
52 size_t remaining = len;
54 /* Encode each byte */
55 for ( ; remaining-- ; encoded_bytes += 2 ) {
56 sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
59 /* Ensure terminating NUL exists even if length was zero */
60 *encoded_bytes = '\0';
62 DBG ( "Base16-encoded to \"%s\":\n", encoded );
63 DBG_HDA ( 0, raw, len );
64 assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
68 * Decode hexadecimal string
70 * @v encoded Encoded string
71 * @v separator Byte separator character, or 0 for no separator
73 * @v len Length of buffer
74 * @ret len Length of data, or negative error
76 int hex_decode ( const char *encoded, char separator, void *data, size_t len ) {
78 unsigned int count = 0;
79 unsigned int sixteens;
84 /* Check separator, if applicable */
85 if ( count && separator && ( ( *(encoded++) != separator ) ) )
88 /* Extract digits. Note that either digit may be NUL,
89 * which would be interpreted as an invalid value by
90 * strtoul_charval(); there is therefore no need for an
91 * explicit end-of-string check.
93 sixteens = strtoul_charval ( *(encoded++) );
96 units = strtoul_charval ( *(encoded++) );
102 out[count] = ( ( sixteens << 4 ) | units );
112 * @v encoded Encoded string
114 * @ret len Length of raw data, or negative error
116 * The buffer must be large enough to contain the decoded data. Use
119 * char buf[ base16_decoded_max_len ( encoded ) ];
121 * to provide a buffer of the correct size.
123 int base16_decode ( const char *encoded, uint8_t *raw ) {
126 len = hex_decode ( encoded, 0, raw, -1UL );
130 DBG ( "Base16-decoded \"%s\" to:\n", encoded );
131 DBG_HDA ( 0, raw, len );
132 assert ( len <= ( int ) base16_decoded_max_len ( encoded ) );