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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
25 #include <gpxe/base16.h>
37 * @v len Length of raw data
38 * @v encoded Buffer for encoded string
40 * The buffer must be the correct length for the encoded string. Use
43 * char buf[ base16_encoded_len ( len ) + 1 ];
45 * (the +1 is for the terminating NUL) to provide a buffer of the
48 void base16_encode ( const uint8_t *raw
, size_t len
, char *encoded
) {
49 const uint8_t *raw_bytes
= raw
;
50 char *encoded_bytes
= encoded
;
51 size_t remaining
= len
;
53 for ( ; remaining
-- ; encoded_bytes
+= 2 ) {
54 sprintf ( encoded_bytes
, "%02x", *(raw_bytes
++) );
57 DBG ( "Base16-encoded to \"%s\":\n", encoded
);
58 DBG_HDA ( 0, raw
, len
);
59 assert ( strlen ( encoded
) == base16_encoded_len ( len
) );
65 * @v encoded Encoded string
67 * @ret len Length of raw data, or negative error
69 * The buffer must be large enough to contain the decoded data. Use
72 * char buf[ base16_decoded_max_len ( encoded ) ];
74 * to provide a buffer of the correct size.
76 int base16_decode ( const char *encoded
, uint8_t *raw
) {
77 const char *encoded_bytes
= encoded
;
78 uint8_t *raw_bytes
= raw
;
83 while ( encoded_bytes
[0] ) {
84 if ( ! encoded_bytes
[1] ) {
85 DBG ( "Base16-encoded string \"%s\" has invalid "
86 "length\n", encoded
);
89 memcpy ( buf
, encoded_bytes
, 2 );
91 *(raw_bytes
++) = strtoul ( buf
, &endp
, 16 );
92 if ( *endp
!= '\0' ) {
93 DBG ( "Base16-encoded string \"%s\" has invalid "
94 "byte \"%s\"\n", encoded
, buf
);
99 len
= ( raw_bytes
- raw
);
101 DBG ( "Base16-decoded \"%s\" to:\n", encoded
);
102 DBG_HDA ( 0, raw
, len
);
103 assert ( len
<= base16_decoded_max_len ( encoded
) );