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 for ( ; remaining-- ; encoded_bytes += 2 ) {
55 sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
58 DBG ( "Base16-encoded to \"%s\":\n", encoded );
59 DBG_HDA ( 0, raw, len );
60 assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
66 * @v encoded Encoded string
68 * @ret len Length of raw data, or negative error
70 * The buffer must be large enough to contain the decoded data. Use
73 * char buf[ base16_decoded_max_len ( encoded ) ];
75 * to provide a buffer of the correct size.
77 int base16_decode ( const char *encoded, uint8_t *raw ) {
78 const char *encoded_bytes = encoded;
79 uint8_t *raw_bytes = raw;
84 while ( encoded_bytes[0] ) {
85 if ( ! encoded_bytes[1] ) {
86 DBG ( "Base16-encoded string \"%s\" has invalid "
87 "length\n", encoded );
90 memcpy ( buf, encoded_bytes, 2 );
92 *(raw_bytes++) = strtoul ( buf, &endp, 16 );
93 if ( *endp != '\0' ) {
94 DBG ( "Base16-encoded string \"%s\" has invalid "
95 "byte \"%s\"\n", encoded, buf );
100 len = ( raw_bytes - raw );
102 DBG ( "Base16-decoded \"%s\" to:\n", encoded );
103 DBG_HDA ( 0, raw, len );
104 assert ( len <= base16_decoded_max_len ( encoded ) );