[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / base16.c
blobd74556deb66363bea48dcbc9c1be45a227da8194
1 /*
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 );
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <gpxe/base16.h>
27 /** @file
29 * Base16 encoding
33 /**
34 * Base16-encode data
36 * @v raw Raw data
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
41 * something like
43 * char buf[ base16_encoded_len ( len ) + 1 ];
45 * (the +1 is for the terminating NUL) to provide a buffer of the
46 * correct size.
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 ) );
62 /**
63 * Base16-decode data
65 * @v encoded Encoded string
66 * @v raw Raw data
67 * @ret len Length of raw data, or negative error
69 * The buffer must be large enough to contain the decoded data. Use
70 * something like
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;
79 char buf[3];
80 char *endp;
81 size_t len;
83 while ( encoded_bytes[0] ) {
84 if ( ! encoded_bytes[1] ) {
85 DBG ( "Base16-encoded string \"%s\" has invalid "
86 "length\n", encoded );
87 return -EINVAL;
89 memcpy ( buf, encoded_bytes, 2 );
90 buf[2] = '\0';
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 );
95 return -EINVAL;
97 encoded_bytes += 2;
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 ) );
105 return ( len );