Browse Source

[base64] Allow base64_encode() to handle arbitrary data

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
dfcce165a5
3 changed files with 16 additions and 14 deletions
  1. 9
    7
      src/core/base64.c
  2. 3
    3
      src/include/ipxe/base64.h
  3. 4
    4
      src/net/tcp/http.c

+ 9
- 7
src/core/base64.c View File

33
 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33
 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34
 
34
 
35
 /**
35
 /**
36
- * Base64-encode a string
36
+ * Base64-encode data
37
  *
37
  *
38
- * @v raw		Raw string
38
+ * @v raw		Raw data
39
+ * @v len		Length of raw data
39
  * @v encoded		Buffer for encoded string
40
  * @v encoded		Buffer for encoded string
40
  *
41
  *
41
  * The buffer must be the correct length for the encoded string.  Use
42
  * The buffer must be the correct length for the encoded string.  Use
42
  * something like
43
  * something like
43
  *
44
  *
44
- *     char buf[ base64_encoded_len ( strlen ( raw ) ) + 1 ];
45
+ *     char buf[ base64_encoded_len ( len ) + 1 ];
45
  *
46
  *
46
  * (the +1 is for the terminating NUL) to provide a buffer of the
47
  * (the +1 is for the terminating NUL) to provide a buffer of the
47
  * correct size.
48
  * correct size.
48
  */
49
  */
49
-void base64_encode ( const char *raw, char *encoded ) {
50
+void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
50
 	const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
51
 	const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
51
 	uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
52
 	uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
52
-	size_t raw_bit_len = ( 8 * strlen ( raw ) );
53
+	size_t raw_bit_len = ( 8 * len );
53
 	unsigned int bit;
54
 	unsigned int bit;
54
 	unsigned int tmp;
55
 	unsigned int tmp;
55
 
56
 
63
 		*(encoded_bytes++) = '=';
64
 		*(encoded_bytes++) = '=';
64
 	*(encoded_bytes++) = '\0';
65
 	*(encoded_bytes++) = '\0';
65
 
66
 
66
-	DBG ( "Base64-encoded \"%s\" as \"%s\"\n", raw, encoded );
67
-	assert ( strlen ( encoded ) == base64_encoded_len ( strlen ( raw ) ) );
67
+	DBG ( "Base64-encoded to \"%s\":\n", encoded );
68
+	DBG_HDA ( 0, raw, len );
69
+	assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
68
 }
70
 }

+ 3
- 3
src/include/ipxe/base64.h View File

12
 #include <stdint.h>
12
 #include <stdint.h>
13
 
13
 
14
 /**
14
 /**
15
- * Calculate length of base64-encoded string
15
+ * Calculate length of base64-encoded data
16
  *
16
  *
17
- * @v raw_len		Raw string length (excluding NUL)
17
+ * @v raw_len		Raw data length
18
  * @ret encoded_len	Encoded string length (excluding NUL)
18
  * @ret encoded_len	Encoded string length (excluding NUL)
19
  */
19
  */
20
 static inline size_t base64_encoded_len ( size_t raw_len ) {
20
 static inline size_t base64_encoded_len ( size_t raw_len ) {
21
 	return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
21
 	return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
22
 }
22
 }
23
 
23
 
24
-extern void base64_encode ( const char *raw, char *encoded );
24
+extern void base64_encode ( const uint8_t *raw, size_t len, char *encoded );
25
 
25
 
26
 #endif /* _IPXE_BASE64_H */
26
 #endif /* _IPXE_BASE64_H */

+ 4
- 4
src/net/tcp/http.c View File

424
 	size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
424
 	size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
425
 					strlen ( password ) ) : 0 );
425
 					strlen ( password ) ) : 0 );
426
 	size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
426
 	size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
427
-	char user_pw[ user_pw_len + 1 /* NUL */ ];
427
+	uint8_t user_pw[ user_pw_len + 1 /* NUL */ ];
428
 	char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
428
 	char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
429
 	int rc;
429
 	int rc;
430
 	int request_len = unparse_uri ( NULL, 0, http->uri,
430
 	int request_len = unparse_uri ( NULL, 0, http->uri,
443
 		/* Construct authorisation, if applicable */
443
 		/* Construct authorisation, if applicable */
444
 		if ( user ) {
444
 		if ( user ) {
445
 			/* Make "user:password" string from decoded fields */
445
 			/* Make "user:password" string from decoded fields */
446
-			snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
447
-				   user, password );
446
+			snprintf ( ( ( char * ) user_pw ), sizeof ( user_pw ),
447
+				   "%s:%s", user, password );
448
 
448
 
449
 			/* Base64-encode the "user:password" string */
449
 			/* Base64-encode the "user:password" string */
450
-			base64_encode ( user_pw, user_pw_base64 );
450
+			base64_encode ( user_pw, user_pw_len, user_pw_base64 );
451
 		}
451
 		}
452
 
452
 
453
 		/* Send GET request */
453
 		/* Send GET request */

Loading…
Cancel
Save