From 445bcc8f464252649ae5d13add727420abe661d7 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 14 Apr 2016 00:42:49 +0200 Subject: Add base64 string decoder. --- include/mupdf/fitz/buffer.h | 5 +++++ source/fitz/buffer.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) 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 @@ -77,6 +77,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 @@ -56,6 +56,37 @@ fz_new_buffer_from_shared_data(fz_context *ctx, const char *data, int size) return b; } +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) { -- cgit v1.2.3