[librsvg] Use g_base64_decode() instead of our own base64 function.
- From: Hiroyuki Ikezoe <hiikezoe src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] Use g_base64_decode() instead of our own base64 function.
- Date: Thu, 27 May 2010 11:02:12 +0000 (UTC)
commit 27692fc6e1a7ea87f117423857b4cebc744dba12
Author: Hiroyuki Ikezoe <hiikezoe gnome org>
Date: Thu May 27 20:01:26 2010 +0900
Use g_base64_decode() instead of our own base64 function.
We can use g_base64_decode here since librsvg already
requires GLib-2.12.
rsvg-image.c | 172 ++--------------------------------------------------------
1 files changed, 6 insertions(+), 166 deletions(-)
---
diff --git a/rsvg-image.c b/rsvg-image.c
index 40832be..9c8627a 100644
--- a/rsvg-image.c
+++ b/rsvg-image.c
@@ -38,163 +38,12 @@
#include <gio/gio.h>
#endif
-static const char s_UTF8_B64Alphabet[64] = {
- 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
- 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, /* A-Z */
- 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
- 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, /* a-z */
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, /* 0-9 */
- 0x2b, /* + */
- 0x2f /* / */
-};
-static const char utf8_b64_pad = 0x3d;
-
-static gboolean
-b64_decode_char (char c, int *b64)
-{
- if ((c >= 0x41) && (c <= 0x5a)) {
- *b64 = c - 0x41;
- return TRUE;
- }
- if ((c >= 0x61) && (c <= 0x7a)) {
- *b64 = c - (0x61 - 26);
- return TRUE;
- }
- if ((c >= 0x30) && (c <= 0x39)) {
- *b64 = c + (52 - 0x30);
- return TRUE;
- }
- if (c == 0x2b) {
- *b64 = 62;
- return TRUE;
- }
- if (c == 0x2f) {
- *b64 = 63;
- return TRUE;
- }
- return FALSE;
-}
-
-static gboolean
-utf8_base64_decode (guchar ** binptr, size_t * binlen, const char *b64ptr, size_t b64len)
-{
- gboolean decoded = TRUE;
- gboolean padding = FALSE;
-
- int i = 0;
- glong ucs4_len, j;
-
- unsigned char byte1 = 0;
- unsigned char byte2;
-
- gunichar ucs4, *ucs4_str;
-
- if (b64len == 0)
- return TRUE;
-
- if ((binptr == 0) || (b64ptr == 0))
- return FALSE;
-
- ucs4_str = g_utf8_to_ucs4_fast (b64ptr, b64len, &ucs4_len);
-
- for (j = 0; j < ucs4_len; j++) {
- ucs4 = ucs4_str[j];
- if ((ucs4 & 0x7f) == ucs4) {
- int b64;
- char c = (char) (ucs4);
-
- if (b64_decode_char (c, &b64)) {
- if (padding || (*binlen == 0)) {
- decoded = FALSE;
- break;
- }
-
- switch (i) {
- case 0:
- byte1 = (unsigned char) (b64) << 2;
- i++;
- break;
- case 1:
- byte2 = (unsigned char) (b64);
- byte1 |= byte2 >> 4;
- *(*binptr)++ = (char) (byte1);
- (*binlen)--;
- byte1 = (byte2 & 0x0f) << 4;
- i++;
- break;
- case 2:
- byte2 = (unsigned char) (b64);
- byte1 |= byte2 >> 2;
- *(*binptr)++ = (char) (byte1);
- (*binlen)--;
- byte1 = (byte2 & 0x03) << 6;
- i++;
- break;
- default:
- byte1 |= (unsigned char) (b64);
- *(*binptr)++ = (char) (byte1);
- (*binlen)--;
- i = 0;
- break;
- }
-
- if (!decoded)
- break;
-
- continue;
- } else if (c == utf8_b64_pad) {
- switch (i) {
- case 0:
- case 1:
- decoded = FALSE;
- break;
- case 2:
- if (*binlen == 0)
- decoded = FALSE;
- else {
- *(*binptr)++ = (char) (byte1);
- (*binlen)--;
- padding = TRUE;
- }
- i++;
- break;
- default:
- if (!padding) {
- if (*binlen == 0)
- decoded = FALSE;
- else {
- *(*binptr)++ = (char) (byte1);
- (*binlen)--;
- padding = TRUE;
- }
- }
- i = 0;
- break;
- }
- if (!decoded)
- break;
-
- continue;
- }
- }
- if (g_unichar_isspace (ucs4))
- continue;
-
- decoded = FALSE;
- break;
- }
-
- g_free (ucs4_str);
- return decoded;
-}
-
static GByteArray *
rsvg_acquire_base64_resource (const char *data, GError ** error)
{
- GByteArray *array;
-
+ GByteArray *array = NULL;
guchar *bufptr;
- size_t buffer_len, buffer_max_len, data_len;
+ gsize buffer_len;
rsvg_return_val_if_fail (data != NULL, NULL, error);
@@ -202,21 +51,12 @@ rsvg_acquire_base64_resource (const char *data, GError ** error)
if (*data++ == ',')
break;
- data_len = strlen (data);
-
- buffer_max_len = ((data_len >> 2) + 1) * 3;
- buffer_len = buffer_max_len;
-
- array = g_byte_array_sized_new (buffer_max_len);
- bufptr = array->data;
-
- if (!utf8_base64_decode (&bufptr, &buffer_len, data, data_len)) {
- g_byte_array_free (array, TRUE);
- return NULL;
+ bufptr = g_base64_decode (data, &buffer_len);
+ if (bufptr) {
+ array = g_byte_array_sized_new (buffer_len);
+ g_byte_array_append (array, bufptr, buffer_len);
}
- array->len = buffer_max_len - buffer_len;
-
return array;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]