[gPXE-devel] [PATCHv2 1/2] [base64] Add decoding support.
Piotr Jaroszyński
p.jaroszynski at gmail.com
Fri Apr 2 21:10:25 EDT 2010
Signed-off-by: Piotr Jaroszyński <p.jaroszynski at gmail.com>
---
src/core/base64.c | 70 +++++++++++++++++++++++++++++++++++++++++++++
src/include/gpxe/base64.h | 12 ++++++++
2 files changed, 82 insertions(+), 0 deletions(-)
diff --git a/src/core/base64.c b/src/core/base64.c
index 5619ef7..3c0d354 100644
--- a/src/core/base64.c
+++ b/src/core/base64.c
@@ -66,3 +66,73 @@ void base64_encode ( const char *raw, char *encoded ) {
DBG ( "Base64-encoded \"%s\" as \"%s\"\n", raw, encoded );
assert ( strlen ( encoded ) == base64_encoded_len ( strlen ( raw ) ) );
}
+
+static const char base64d[80] =
+ "\x3e\x80\x80\x80\x3f\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x80"
+// + (,) (-) (.) / 0 1 2 3 4 5 6 7 8 9 (:)
+ "\x80\x80\xc0\x80\x80\x80\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
+// (;) (<) = (>) (?) (@) A B C D E F G H I J
+ "\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"
+// K L M N O P Q R S T U V W X Y Z
+ "\x80\x80\x80\x80\x80\x80\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23"
+// ([) (\) (]) (^) (_) (`) a b c d e f g h i j
+ "\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33";
+// k l m n o p q r s t u v w x y z
+
+/**
+ * Base64-decode a string
+ *
+ * @v encoded Encoded string
+ * @v raw Buffer for raw data
+ * @ret rc Return length of the decoded data or -1 to indicate an error
+ *
+ * The buffer must be the correct length for the raw data. Use
+ * something like
+ *
+ * char buf[ base64_decode_buf_len ( strlen ( encoded ) ) ];
+ *
+ * to provide a buffer of the correct size.
+ */
+int base64_decode ( const char *encoded, char *raw ) {
+ size_t encoded_len = strlen ( encoded );
+ size_t len = ( encoded_len / 4 ) * 3;
+ unsigned int byte;
+ unsigned int i;
+ unsigned char buf[4];
+
+ if ( encoded_len % 4 != 0 )
+ return -1;
+
+ for ( byte = 0 ; byte < len ; byte += 3 ) {
+ for ( i = 0 ; i < 4 ; ++i) {
+ if ( ( *encoded < '+' ) || ( *encoded > 'z' ) )
+ return -1;
+
+ buf[ i ] = base64d[ *(encoded++) - '+' ];
+
+ if ( buf[ i ] & 0x80 ) {
+ if ( ( buf[ i ] == 0xc0 ) && ( byte + i + 1 >= len ) &&
+ ( ( i == 3 ) || ( *encoded == '=' ) ) ) {
+ buf[ i ] = 0;
+ --len;
+ } else {
+ return -1;
+ }
+ }
+ }
+
+ /*
+ * this loop does:
+ * *(raw++) = ( buf[ 0 ] << 2 ) | ( buf[ 1 ] >> 4 );
+ * *(raw++) = ( buf[ 1 ] << 4 ) | ( buf[ 2 ] >> 2 );
+ * *(raw++) = ( buf[ 2 ] << 6 ) | ( buf[ 3 ] >> 0 );
+ */
+ for ( i = 0 ; i < 3 ; ++i )
+ *(raw++) = ( buf[ i ] << ( i * 2 + 2 ) ) |
+ ( buf[ i + 1 ] >> ( 4 - i * 2 ) );
+ }
+
+ DBG ( "Base64-decoded \"%s\" to \"%s\"\n", encoded, raw );
+
+ return len;
+}
diff --git a/src/include/gpxe/base64.h b/src/include/gpxe/base64.h
index e38bef0..e4255a3 100644
--- a/src/include/gpxe/base64.h
+++ b/src/include/gpxe/base64.h
@@ -21,6 +21,18 @@ static inline size_t base64_encoded_len ( size_t raw_len ) {
return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
}
+/**
+ * Calculate length of the buffer needed to base64-decode string
+ *
+ * @v encoded_len Encoded string length (excluding NUL)
+ * @ret buf_len Buffer length
+ */
+static inline size_t base64_decode_buf_len ( size_t encoded_len ) {
+ return ( ( encoded_len / 4 ) * 3 );
+}
+
extern void base64_encode ( const char *raw, char *encoded );
+extern int base64_decode ( const char *encoded, char *raw );
+
#endif /* _GPXE_BASE64_H */
--
1.7.0.3
More information about the gPXE-devel
mailing list