|
@@ -20,6 +20,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
20
|
20
|
|
21
|
21
|
#include <stdint.h>
|
22
|
22
|
#include <string.h>
|
|
23
|
+#include <ctype.h>
|
|
24
|
+#include <errno.h>
|
23
|
25
|
#include <assert.h>
|
24
|
26
|
#include <ipxe/base64.h>
|
25
|
27
|
|
|
@@ -68,3 +70,86 @@ void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
|
68
|
70
|
DBG_HDA ( 0, raw, len );
|
69
|
71
|
assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
|
70
|
72
|
}
|
|
73
|
+
|
|
74
|
+/**
|
|
75
|
+ * Base64-decode string
|
|
76
|
+ *
|
|
77
|
+ * @v encoded Encoded string
|
|
78
|
+ * @v raw Raw data
|
|
79
|
+ * @ret len Length of raw data, or negative error
|
|
80
|
+ *
|
|
81
|
+ * The buffer must be large enough to contain the decoded data. Use
|
|
82
|
+ * something like
|
|
83
|
+ *
|
|
84
|
+ * char buf[ base64_decoded_max_len ( encoded ) ];
|
|
85
|
+ *
|
|
86
|
+ * to provide a buffer of the correct size.
|
|
87
|
+ */
|
|
88
|
+int base64_decode ( const char *encoded, uint8_t *raw ) {
|
|
89
|
+ const uint8_t *encoded_bytes = ( ( const uint8_t * ) encoded );
|
|
90
|
+ uint8_t *raw_bytes = ( ( uint8_t * ) raw );
|
|
91
|
+ uint8_t encoded_byte;
|
|
92
|
+ char *match;
|
|
93
|
+ int decoded;
|
|
94
|
+ unsigned int bit = 0;
|
|
95
|
+ unsigned int pad_count = 0;
|
|
96
|
+ size_t len;
|
|
97
|
+
|
|
98
|
+ /* Zero the raw data */
|
|
99
|
+ memset ( raw, 0, base64_decoded_max_len ( encoded ) );
|
|
100
|
+
|
|
101
|
+ /* Decode string */
|
|
102
|
+ while ( ( encoded_byte = *(encoded_bytes++) ) ) {
|
|
103
|
+
|
|
104
|
+ /* Ignore whitespace characters */
|
|
105
|
+ if ( isspace ( encoded_byte ) )
|
|
106
|
+ continue;
|
|
107
|
+
|
|
108
|
+ /* Process pad characters */
|
|
109
|
+ if ( encoded_byte == '=' ) {
|
|
110
|
+ if ( pad_count >= 2 ) {
|
|
111
|
+ DBG ( "Base64-encoded string \"%s\" has too "
|
|
112
|
+ "many pad characters\n", encoded );
|
|
113
|
+ return -EINVAL;
|
|
114
|
+ }
|
|
115
|
+ pad_count++;
|
|
116
|
+ bit -= 2; /* unused_bits = ( 2 * pad_count ) */
|
|
117
|
+ continue;
|
|
118
|
+ }
|
|
119
|
+ if ( pad_count ) {
|
|
120
|
+ DBG ( "Base64-encoded string \"%s\" has invalid pad "
|
|
121
|
+ "sequence\n", encoded );
|
|
122
|
+ return -EINVAL;
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ /* Process normal characters */
|
|
126
|
+ match = strchr ( base64, encoded_byte );
|
|
127
|
+ if ( ! match ) {
|
|
128
|
+ DBG ( "Base64-encoded string \"%s\" contains invalid "
|
|
129
|
+ "character '%c'\n", encoded, encoded_byte );
|
|
130
|
+ return -EINVAL;
|
|
131
|
+ }
|
|
132
|
+ decoded = ( match - base64 );
|
|
133
|
+
|
|
134
|
+ /* Add to raw data */
|
|
135
|
+ decoded <<= 2;
|
|
136
|
+ raw_bytes[ bit / 8 ] |= ( decoded >> ( bit % 8 ) );
|
|
137
|
+ raw_bytes[ bit / 8 + 1 ] |= ( decoded << ( 8 - ( bit % 8 ) ) );
|
|
138
|
+ bit += 6;
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ /* Check that we decoded a whole number of bytes */
|
|
142
|
+ if ( ( bit % 8 ) != 0 ) {
|
|
143
|
+ DBG ( "Base64-encoded string \"%s\" has invalid bit length "
|
|
144
|
+ "%d\n", encoded, bit );
|
|
145
|
+ return -EINVAL;
|
|
146
|
+ }
|
|
147
|
+ len = ( bit / 8 );
|
|
148
|
+
|
|
149
|
+ DBG ( "Base64-decoded \"%s\" to:\n", encoded );
|
|
150
|
+ DBG_HDA ( 0, raw, len );
|
|
151
|
+ assert ( len <= base64_decoded_max_len ( encoded ) );
|
|
152
|
+
|
|
153
|
+ /* Return length in bytes */
|
|
154
|
+ return ( len );
|
|
155
|
+}
|