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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
30 #include <ipxe/string.h>
31 #include <ipxe/vsprintf.h>
32 #include <ipxe/base16.h>
41 * Encode hexadecimal string (with optional byte separator character)
43 * @v separator Byte separator character, or 0 for no separator
45 * @v raw_len Length of raw data
47 * @v len Length of buffer
48 * @ret len Encoded length
50 size_t hex_encode ( char separator
, const void *raw
, size_t raw_len
,
51 char *data
, size_t len
) {
52 const uint8_t *bytes
= raw
;
53 const char delimiter
[2] = { separator
, '\0' };
58 data
[0] = 0; /* Ensure that a terminating NUL exists */
59 for ( i
= 0 ; i
< raw_len
; i
++ ) {
60 used
+= ssnprintf ( ( data
+ used
), ( len
- used
),
61 "%s%02x", ( used ? delimiter
: "" ),
68 * Decode hexadecimal string (with optional byte separator character)
70 * @v separator Byte separator character, or 0 for no separator
71 * @v encoded Encoded string
73 * @v len Length of buffer
74 * @ret len Length of data, or negative error
76 int hex_decode ( char separator
, const char *encoded
, 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 * digit_value(); there is therefore no need for an
91 * explicit end-of-string check.
93 sixteens
= digit_value ( *(encoded
++) );
96 units
= digit_value ( *(encoded
++) );
102 out
[count
] = ( ( sixteens
<< 4 ) | units
);