30 |
30 |
31 #include <string.h> |
31 #include <string.h> |
32 |
32 |
33 #include "gstrtspbase64.h" |
33 #include "gstrtspbase64.h" |
34 |
34 |
35 static char base64table[64] = { |
|
36 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
37 'P', |
|
38 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', |
|
39 'f', |
|
40 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', |
|
41 'v', |
|
42 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
|
43 }; |
|
44 |
|
45 /** |
35 /** |
46 * gst_rtsp_base64_encode: |
36 * gst_rtsp_base64_encode: |
47 * @data: the binary data to encode |
37 * @data: the binary data to encode |
48 * @len: the length of @data |
38 * @len: the length of @data |
49 * |
39 * |
50 * Encode a sequence of binary data into its Base-64 stringified representation. |
40 * Encode a sequence of binary data into its Base-64 stringified representation. |
51 * |
41 * |
|
42 * Deprecated: Use g_base64_encode() |
|
43 * |
52 * Returns: a newly allocated, zero-terminated Base-64 encoded string |
44 * Returns: a newly allocated, zero-terminated Base-64 encoded string |
53 * representing @data. |
45 * representing @data. |
54 */ |
46 */ |
55 /* This isn't efficient, but it doesn't need to be */ |
47 /* This isn't efficient, but it doesn't need to be */ |
56 #ifdef __SYMBIAN32__ |
48 #ifndef GST_REMOVE_DEPRECATED |
57 EXPORT_C |
|
58 #endif |
|
59 |
|
60 gchar * |
49 gchar * |
61 gst_rtsp_base64_encode (const gchar * data, gsize len) |
50 gst_rtsp_base64_encode (const gchar * data, gsize len) |
62 { |
51 { |
63 gchar *out = g_malloc (len * 4 / 3 + 4); |
52 return g_base64_encode ((const guchar *) data, len); |
64 gchar *result = out; |
|
65 int chunk; |
|
66 |
|
67 while (len > 0) { |
|
68 chunk = (len > 3) ? 3 : len; |
|
69 *out++ = base64table[(*data & 0xFC) >> 2]; |
|
70 *out++ = base64table[((*data & 0x03) << 4) | ((*(data + 1) & 0xF0) >> 4)]; |
|
71 switch (chunk) { |
|
72 case 3: |
|
73 *out++ = |
|
74 base64table[((*(data + 1) & 0x0F) << 2) | ((*(data + |
|
75 2) & 0xC0) >> 6)]; |
|
76 *out++ = base64table[(*(data + 2)) & 0x3F]; |
|
77 break; |
|
78 case 2: |
|
79 *out++ = base64table[((*(data + 1) & 0x0F) << 2)]; |
|
80 *out++ = '='; |
|
81 break; |
|
82 case 1: |
|
83 *out++ = '='; |
|
84 *out++ = '='; |
|
85 break; |
|
86 } |
|
87 data += chunk; |
|
88 len -= chunk; |
|
89 } |
|
90 *out = 0; |
|
91 |
|
92 return result; |
|
93 } |
53 } |
|
54 #endif |
94 |
55 |
95 /** |
56 /** |
96 * gst_rtsp_base64_decode_ip: |
57 * gst_rtsp_base64_decode_ip: |
97 * @data: the base64 encoded data |
58 * @data: the base64 encoded data |
98 * @len: location for output length or NULL |
59 * @len: location for output length or NULL |
99 * |
60 * |
100 * Decode the base64 string pointed to by @data in-place. When @len is not #NULL |
61 * Decode the base64 string pointed to by @data in-place. When @len is not #NULL |
101 * it will contain the length of the decoded data. |
62 * it will contain the length of the decoded data. |
102 */ |
63 */ |
|
64 /* FIXME: Deprecate this once we depend on GLib 2.20 and |
|
65 * use g_base64_decode_inplace then. |
|
66 */ |
103 void |
67 void |
104 gst_rtsp_base64_decode_ip (gchar * data, gsize * len) |
68 gst_rtsp_base64_decode_ip (gchar * data, gsize * len) |
105 { |
69 { |
106 char dtable[256]; |
70 gint input_length, output_length, state = 0; |
107 int i, j, k = 0, n = strlen (data); |
71 guint save = 0; |
108 |
72 |
109 for (i = 0; i < 255; i++) |
73 g_return_if_fail (data != NULL); |
110 dtable[i] = 0x80; |
|
111 for (i = 'A'; i <= 'Z'; i++) |
|
112 dtable[i] = 0 + (i - 'A'); |
|
113 for (i = 'a'; i <= 'z'; i++) |
|
114 dtable[i] = 26 + (i - 'a'); |
|
115 for (i = '0'; i <= '9'; i++) |
|
116 dtable[i] = 52 + (i - '0'); |
|
117 dtable['+'] = 62; |
|
118 dtable['/'] = 63; |
|
119 dtable['='] = 0; |
|
120 |
74 |
121 for (j = 0; j < n; j += 4) { |
75 input_length = strlen (data); |
122 char a[4], b[4]; |
|
123 |
76 |
124 for (i = 0; i < 4; i++) { |
77 g_return_if_fail (input_length > 1); |
125 int c = data[i + j]; |
|
126 |
78 |
127 if (dtable[c] & 0x80) { |
79 output_length = |
128 if (len) |
80 g_base64_decode_step (data, input_length, (guchar *) data, &state, &save); |
129 *len = 0; |
81 |
130 return; |
|
131 } |
|
132 a[i] = (char) c; |
|
133 b[i] = (char) dtable[c]; |
|
134 } |
|
135 data[k++] = (b[0] << 2) | (b[1] >> 4); |
|
136 data[k++] = (b[1] << 4) | (b[2] >> 2); |
|
137 data[k++] = (b[2] << 6) | b[3]; |
|
138 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3); |
|
139 if (i < 3) { |
|
140 data[k] = 0; |
|
141 if (len) |
|
142 *len = k; |
|
143 return; |
|
144 } |
|
145 } |
|
146 data[k] = 0; |
|
147 if (len) |
82 if (len) |
148 *len = k; |
83 *len = output_length; |
149 return; |
|
150 } |
84 } |