diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2017-07-26 15:58:33 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2017-07-27 14:44:04 +0200 |
commit | 44cff76eb82cf11097e4e01348240e3556e22a7f (patch) | |
tree | 9289a3eea90f621846a6c31d0ae82bb6394a0789 /source/fitz/output.c | |
parent | 1840cf480522ee03e22a0f36b76a85dde62c4c8f (diff) | |
download | mupdf-44cff76eb82cf11097e4e01348240e3556e22a7f.tar.xz |
Add fz_write_base64 function.
Diffstat (limited to 'source/fitz/output.c')
-rw-r--r-- | source/fitz/output.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/source/fitz/output.c b/source/fitz/output.c index c9f04b96..75872ab2 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -373,6 +373,50 @@ fz_write_rune(fz_context *ctx, fz_output *out, int rune) } void +fz_write_base64(fz_context *ctx, fz_output *out, const unsigned char *data, int size, int newline) +{ + static const char set[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + int i; + for (i = 0; i + 3 < size; i += 3) + { + int c = data[i]; + int d = data[i+1]; + int e = data[i+2]; + if (newline && (i & 15) == 0) + fz_write_byte(ctx, out, '\n'); + fz_write_byte(ctx, out, set[c>>2]); + fz_write_byte(ctx, out, set[((c&3)<<4)|(d>>4)]); + fz_write_byte(ctx, out, set[((d&15)<<2)|(e>>6)]); + fz_write_byte(ctx, out, set[e&63]); + } + if (size - i == 2) + { + int c = data[i]; + int d = data[i+1]; + fz_write_byte(ctx, out, set[c>>2]); + fz_write_byte(ctx, out, set[((c&3)<<4)|(d>>4)]); + fz_write_byte(ctx, out, set[((d&15)<<2)]); + fz_write_byte(ctx, out, '='); + } + else if (size - i == 1) + { + int c = data[i]; + fz_write_byte(ctx, out, set[c>>2]); + fz_write_byte(ctx, out, set[((c&3)<<4)]); + fz_write_byte(ctx, out, '='); + fz_write_byte(ctx, out, '='); + } +} + +void +fz_write_base64_buffer(fz_context *ctx, fz_output *out, fz_buffer *buf, int newline) +{ + unsigned char *data; + size_t size = fz_buffer_storage(ctx, buf, &data); + fz_write_base64(ctx, out, data, size, newline); +} + +void fz_save_buffer(fz_context *ctx, fz_buffer *buf, const char *filename) { fz_output *out = fz_new_output_with_path(ctx, filename, 0); |