|
@@ -0,0 +1,106 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+#include <stdint.h>
|
|
22
|
+#include <stdlib.h>
|
|
23
|
+#include <stdio.h>
|
|
24
|
+#include <errno.h>
|
|
25
|
+#include <ipxe/base16.h>
|
|
26
|
+
|
|
27
|
+/** @file
|
|
28
|
+ *
|
|
29
|
+ * Base16 encoding
|
|
30
|
+ *
|
|
31
|
+ */
|
|
32
|
+
|
|
33
|
+/**
|
|
34
|
+ * Base16-encode data
|
|
35
|
+ *
|
|
36
|
+ * @v raw Raw data
|
|
37
|
+ * @v len Length of raw data
|
|
38
|
+ * @v encoded Buffer for encoded string
|
|
39
|
+ *
|
|
40
|
+ * The buffer must be the correct length for the encoded string. Use
|
|
41
|
+ * something like
|
|
42
|
+ *
|
|
43
|
+ * char buf[ base16_encoded_len ( len ) + 1 ];
|
|
44
|
+ *
|
|
45
|
+ * (the +1 is for the terminating NUL) to provide a buffer of the
|
|
46
|
+ * correct size.
|
|
47
|
+ */
|
|
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;
|
|
52
|
+
|
|
53
|
+ for ( ; remaining-- ; encoded_bytes += 2 ) {
|
|
54
|
+ sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ DBG ( "Base16-encoded to \"%s\":\n", encoded );
|
|
58
|
+ DBG_HDA ( 0, raw, len );
|
|
59
|
+ assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+/**
|
|
63
|
+ * Base16-decode data
|
|
64
|
+ *
|
|
65
|
+ * @v encoded Encoded string
|
|
66
|
+ * @v raw Raw data
|
|
67
|
+ * @ret len Length of raw data, or negative error
|
|
68
|
+ *
|
|
69
|
+ * The buffer must be large enough to contain the decoded data. Use
|
|
70
|
+ * something like
|
|
71
|
+ *
|
|
72
|
+ * char buf[ base16_decoded_max_len ( encoded ) ];
|
|
73
|
+ *
|
|
74
|
+ * to provide a buffer of the correct size.
|
|
75
|
+ */
|
|
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;
|
|
82
|
+
|
|
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;
|
|
88
|
+ }
|
|
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;
|
|
96
|
+ }
|
|
97
|
+ encoded_bytes += 2;
|
|
98
|
+ }
|
|
99
|
+ len = ( raw_bytes - raw );
|
|
100
|
+
|
|
101
|
+ DBG ( "Base16-decoded \"%s\" to:\n", encoded );
|
|
102
|
+ DBG_HDA ( 0, raw, len );
|
|
103
|
+ assert ( len <= base16_decoded_max_len ( encoded ) );
|
|
104
|
+
|
|
105
|
+ return ( len );
|
|
106
|
+}
|