Index: srtp/crypto/math/datatypes.c |
diff --git a/srtp/crypto/math/datatypes.c b/srtp/crypto/math/datatypes.c |
index 609a057d6a8ba03fc141ea8e6e0078625b608bcb..c4af8d7e45377e75927f07c2d2486f1bff13a1cd 100644 |
--- a/srtp/crypto/math/datatypes.c |
+++ b/srtp/crypto/math/datatypes.c |
@@ -43,6 +43,10 @@ |
* |
*/ |
+#ifdef HAVE_CONFIG_H |
+ #include <config.h> |
+#endif |
+ |
#include "datatypes.h" |
int |
@@ -124,7 +128,7 @@ octet_string_hex_string(const void *s, int length) { |
return bit_string; |
} |
-static INLINE int |
+static inline int |
hex_char_to_nibble(uint8_t c) { |
switch(c) { |
case ('0'): return 0x0; |
@@ -525,194 +529,41 @@ octet_string_set_to_zero(uint8_t *s, int len) { |
} |
+#ifdef TESTAPP_SOURCE |
-/* |
- * From RFC 1521: The Base64 Alphabet |
- * |
- * Value Encoding Value Encoding Value Encoding Value Encoding |
- * 0 A 17 R 34 i 51 z |
- * 1 B 18 S 35 j 52 0 |
- * 2 C 19 T 36 k 53 1 |
- * 3 D 20 U 37 l 54 2 |
- * 4 E 21 V 38 m 55 3 |
- * 5 F 22 W 39 n 56 4 |
- * 6 G 23 X 40 o 57 5 |
- * 7 H 24 Y 41 p 58 6 |
- * 8 I 25 Z 42 q 59 7 |
- * 9 J 26 a 43 r 60 8 |
- * 10 K 27 b 44 s 61 9 |
- * 11 L 28 c 45 t 62 + |
- * 12 M 29 d 46 u 63 / |
- * 13 N 30 e 47 v |
- * 14 O 31 f 48 w (pad) = |
- * 15 P 32 g 49 x |
- * 16 Q 33 h 50 y |
- */ |
+static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
+ "abcdefghijklmnopqrstuvwxyz0123456789+/"; |
-int |
-base64_char_to_sextet(uint8_t c) { |
- switch(c) { |
- case 'A': |
- return 0; |
- case 'B': |
- return 1; |
- case 'C': |
- return 2; |
- case 'D': |
- return 3; |
- case 'E': |
- return 4; |
- case 'F': |
- return 5; |
- case 'G': |
- return 6; |
- case 'H': |
- return 7; |
- case 'I': |
- return 8; |
- case 'J': |
- return 9; |
- case 'K': |
- return 10; |
- case 'L': |
- return 11; |
- case 'M': |
- return 12; |
- case 'N': |
- return 13; |
- case 'O': |
- return 14; |
- case 'P': |
- return 15; |
- case 'Q': |
- return 16; |
- case 'R': |
- return 17; |
- case 'S': |
- return 18; |
- case 'T': |
- return 19; |
- case 'U': |
- return 20; |
- case 'V': |
- return 21; |
- case 'W': |
- return 22; |
- case 'X': |
- return 23; |
- case 'Y': |
- return 24; |
- case 'Z': |
- return 25; |
- case 'a': |
- return 26; |
- case 'b': |
- return 27; |
- case 'c': |
- return 28; |
- case 'd': |
- return 29; |
- case 'e': |
- return 30; |
- case 'f': |
- return 31; |
- case 'g': |
- return 32; |
- case 'h': |
- return 33; |
- case 'i': |
- return 34; |
- case 'j': |
- return 35; |
- case 'k': |
- return 36; |
- case 'l': |
- return 37; |
- case 'm': |
- return 38; |
- case 'n': |
- return 39; |
- case 'o': |
- return 40; |
- case 'p': |
- return 41; |
- case 'q': |
- return 42; |
- case 'r': |
- return 43; |
- case 's': |
- return 44; |
- case 't': |
- return 45; |
- case 'u': |
- return 46; |
- case 'v': |
- return 47; |
- case 'w': |
- return 48; |
- case 'x': |
- return 49; |
- case 'y': |
- return 50; |
- case 'z': |
- return 51; |
- case '0': |
- return 52; |
- case '1': |
- return 53; |
- case '2': |
- return 54; |
- case '3': |
- return 55; |
- case '4': |
- return 56; |
- case '5': |
- return 57; |
- case '6': |
- return 58; |
- case '7': |
- return 59; |
- case '8': |
- return 60; |
- case '9': |
- return 61; |
- case '+': |
- return 62; |
- case '/': |
- return 63; |
- case '=': |
- return 64; |
- default: |
- break; |
- } |
- return -1; |
-} |
+static int base64_block_to_octet_triple(char *out, char *in) { |
+ unsigned char sextets[4] = {}; |
+ int j = 0; |
+ int i; |
-/* |
- * base64_string_to_octet_string converts a hexadecimal string |
- * of length 2 * len to a raw octet string of length len |
- */ |
+ for (i = 0; i < 4; i++) { |
+ char *p = strchr(b64chars, in[i]); |
+ if (p != NULL) sextets[i] = p - b64chars; |
+ else j++; |
+ } |
-int |
-base64_string_to_octet_string(char *raw, char *base64, int len) { |
- uint8_t x; |
- int tmp; |
- int base64_len; |
+ out[0] = (sextets[0]<<2)|(sextets[1]>>4); |
+ if (j < 2) out[1] = (sextets[1]<<4)|(sextets[2]>>2); |
+ if (j < 1) out[2] = (sextets[2]<<6)|sextets[3]; |
+ return j; |
+} |
- base64_len = 0; |
- while (base64_len < len) { |
- tmp = base64_char_to_sextet(base64[0]); |
- if (tmp == -1) |
- return base64_len; |
- x = (tmp << 6); |
- base64_len++; |
- tmp = base64_char_to_sextet(base64[1]); |
- if (tmp == -1) |
- return base64_len; |
- x |= (tmp & 0xffff); |
- base64_len++; |
- *raw++ = x; |
- base64 += 2; |
+int base64_string_to_octet_string(char *out, int *pad, char *in, int len) { |
+ int k = 0; |
+ int i = 0; |
+ int j = 0; |
+ if (len % 4 != 0) return 0; |
+ |
+ while (i < len && j == 0) { |
+ j = base64_block_to_octet_triple(out + k, in + i); |
+ k += 3; |
+ i += 4; |
} |
- return base64_len; |
+ *pad = j; |
+ return i; |
} |
+ |
+#endif |