Преглед изворни кода

[base64] Allow base64_encode() to handle arbitrary data

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown пре 14 година
родитељ
комит
dfcce165a5
3 измењених фајлова са 16 додато и 14 уклоњено
  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 Прегледај датотеку

@@ -33,23 +33,24 @@ static const char base64[64] =
33 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 40
  * @v encoded		Buffer for encoded string
40 41
  *
41 42
  * The buffer must be the correct length for the encoded string.  Use
42 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 47
  * (the +1 is for the terminating NUL) to provide a buffer of the
47 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 51
 	const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
51 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 54
 	unsigned int bit;
54 55
 	unsigned int tmp;
55 56
 
@@ -63,6 +64,7 @@ void base64_encode ( const char *raw, char *encoded ) {
63 64
 		*(encoded_bytes++) = '=';
64 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 Прегледај датотеку

@@ -12,15 +12,15 @@ FILE_LICENCE ( GPL2_OR_LATER );
12 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 18
  * @ret encoded_len	Encoded string length (excluding NUL)
19 19
  */
20 20
 static inline size_t base64_encoded_len ( size_t raw_len ) {
21 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 26
 #endif /* _IPXE_BASE64_H */

+ 4
- 4
src/net/tcp/http.c Прегледај датотеку

@@ -424,7 +424,7 @@ static void http_step ( struct process *process ) {
424 424
 	size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
425 425
 					strlen ( password ) ) : 0 );
426 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 428
 	char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
429 429
 	int rc;
430 430
 	int request_len = unparse_uri ( NULL, 0, http->uri,
@@ -443,11 +443,11 @@ static void http_step ( struct process *process ) {
443 443
 		/* Construct authorisation, if applicable */
444 444
 		if ( user ) {
445 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 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 453
 		/* Send GET request */

Loading…
Откажи
Сачувај