diff options
-rw-r--r-- | include/mupdf/fitz/buffer.h | 5 | ||||
-rw-r--r-- | source/fitz/buffer.c | 31 |
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) |