summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-04-14 00:42:49 +0200
committerTor Andersson <tor.andersson@artifex.com>2016-04-26 15:12:57 +0200
commit445bcc8f464252649ae5d13add727420abe661d7 (patch)
tree94d4b00ce0eebafec9f8905e2c3611fd7144ef97
parentf0d993da8a086aaac201ec3c5e246446e9b40987 (diff)
downloadmupdf-445bcc8f464252649ae5d13add727420abe661d7.tar.xz
Add base64 string decoder.
-rw-r--r--include/mupdf/fitz/buffer.h5
-rw-r--r--source/fitz/buffer.c31
2 files changed, 36 insertions, 0 deletions
diff --git a/include/mupdf/fitz/buffer.h b/include/mupdf/fitz/buffer.h
index 28adc46b..2749c32c 100644
--- a/include/mupdf/fitz/buffer.h
+++ b/include/mupdf/fitz/buffer.h
@@ -78,6 +78,11 @@ fz_buffer *fz_new_buffer_from_data(fz_context *ctx, unsigned char *data, int siz
fz_buffer *fz_new_buffer_from_shared_data(fz_context *ctx, const char *data, int size);
/*
+ fz_new_buffer_from_base64: Create a new buffer with data decoded from a base64 input string.
+*/
+fz_buffer *fz_new_buffer_from_base64(fz_context *ctx, const char *data, int size);
+
+/*
fz_resize_buffer: Ensure that a buffer has a given capacity,
truncating data if required.
diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c
index 6f0e7057..2d6f9932 100644
--- a/source/fitz/buffer.c
+++ b/source/fitz/buffer.c
@@ -57,6 +57,37 @@ fz_new_buffer_from_shared_data(fz_context *ctx, const char *data, int size)
}
fz_buffer *
+fz_new_buffer_from_base64(fz_context *ctx, const char *data, int size)
+{
+ fz_buffer *buf = fz_new_buffer(ctx, size);
+ const char *end = data + size;
+ const char *s = data;
+ fz_try(ctx)
+ {
+ while (s < end)
+ {
+ int c = *s++;
+ if (c >= 'A' && c <= 'Z')
+ fz_write_buffer_bits(ctx, buf, c - 'A', 6);
+ else if (c >= 'a' && c <= 'z')
+ fz_write_buffer_bits(ctx, buf, c - 'a' + 26, 6);
+ else if (c >= '0' && c <= '9')
+ fz_write_buffer_bits(ctx, buf, c - '0' + 52, 6);
+ else if (c == '+')
+ fz_write_buffer_bits(ctx, buf, 62, 6);
+ else if (c == '/')
+ fz_write_buffer_bits(ctx, buf, 63, 6);
+ }
+ }
+ fz_catch(ctx)
+ {
+ fz_drop_buffer(ctx, buf);
+ fz_rethrow(ctx);
+ }
+ return buf;
+}
+
+fz_buffer *
fz_keep_buffer(fz_context *ctx, fz_buffer *buf)
{
if (buf)