tizen 2.3.1 release
[external/qemu.git] / roms / ipxe / src / core / base16.c
1 /*
2  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <ipxe/base16.h>
27
28 /** @file
29  *
30  * Base16 encoding
31  *
32  */
33
34 /**
35  * Base16-encode data
36  *
37  * @v raw               Raw data
38  * @v len               Length of raw data
39  * @v encoded           Buffer for encoded string
40  *
41  * The buffer must be the correct length for the encoded string.  Use
42  * something like
43  *
44  *     char buf[ base16_encoded_len ( len ) + 1 ];
45  *
46  * (the +1 is for the terminating NUL) to provide a buffer of the
47  * correct size.
48  */
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;
53
54         for ( ; remaining-- ; encoded_bytes += 2 ) {
55                 sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
56         }
57
58         DBG ( "Base16-encoded to \"%s\":\n", encoded );
59         DBG_HDA ( 0, raw, len );
60         assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
61 }
62
63 /**
64  * Base16-decode data
65  *
66  * @v encoded           Encoded string
67  * @v raw               Raw data
68  * @ret len             Length of raw data, or negative error
69  *
70  * The buffer must be large enough to contain the decoded data.  Use
71  * something like
72  *
73  *     char buf[ base16_decoded_max_len ( encoded ) ];
74  *
75  * to provide a buffer of the correct size.
76  */
77 int base16_decode ( const char *encoded, uint8_t *raw ) {
78         const char *encoded_bytes = encoded;
79         uint8_t *raw_bytes = raw;
80         char buf[3];
81         char *endp;
82         size_t len;
83
84         while ( encoded_bytes[0] ) {
85                 if ( ! encoded_bytes[1] ) {
86                         DBG ( "Base16-encoded string \"%s\" has invalid "
87                               "length\n", encoded );
88                         return -EINVAL;
89                 }
90                 memcpy ( buf, encoded_bytes, 2 );
91                 buf[2] = '\0';
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 );
96                         return -EINVAL;
97                 }
98                 encoded_bytes += 2;
99         }
100         len = ( raw_bytes - raw );
101
102         DBG ( "Base16-decoded \"%s\" to:\n", encoded );
103         DBG_HDA ( 0, raw, len );
104         assert ( len <= base16_decoded_max_len ( encoded ) );
105
106         return ( len );
107 }