| 1 |
#include <stdint.h> |
| 2 |
#include <stddef.h> |
| 3 |
#ifdef _OPENMP |
| 4 |
#include <omp.h> |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "libbase64.h" |
| 8 |
#include "tables/tables.h" |
| 9 |
#include "codecs.h" |
| 10 |
#include "env.h" |
| 11 |
|
| 12 |
// These static function pointers are initialized once when the library is |
| 13 |
// first used, and remain in use for the remaining lifetime of the program. |
| 14 |
// The idea being that CPU features don't change at runtime. |
| 15 |
static struct codec codec = { NULL, NULL }; |
| 16 |
|
| 17 |
void |
| 18 |
base64_stream_encode_init (struct base64_state *state, int flags) |
| 19 |
{ |
| 20 |
// If any of the codec flags are set, redo choice: |
| 21 |
if (codec.enc == NULL || flags & 0xFF) { |
| 22 |
codec_choose(&codec, flags); |
| 23 |
} |
| 24 |
state->eof = 0; |
| 25 |
state->bytes = 0; |
| 26 |
state->carry = 0; |
| 27 |
state->flags = flags; |
| 28 |
} |
| 29 |
|
| 30 |
void |
| 31 |
base64_stream_encode |
| 32 |
( struct base64_state *state |
| 33 |
, const char *src |
| 34 |
, size_t srclen |
| 35 |
, char *out |
| 36 |
, size_t *outlen |
| 37 |
) |
| 38 |
{ |
| 39 |
codec.enc(state, src, srclen, out, outlen); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
base64_stream_encode_final |
| 44 |
( struct base64_state *state |
| 45 |
, char *out |
| 46 |
, size_t *outlen |
| 47 |
) |
| 48 |
{ |
| 49 |
uint8_t *o = (uint8_t *)out; |
| 50 |
|
| 51 |
if (state->bytes == 1) { |
| 52 |
*o++ = base64_table_enc_6bit[state->carry]; |
| 53 |
*o++ = '='; |
| 54 |
*o++ = '='; |
| 55 |
*outlen = 3; |
| 56 |
return; |
| 57 |
} |
| 58 |
if (state->bytes == 2) { |
| 59 |
*o++ = base64_table_enc_6bit[state->carry]; |
| 60 |
*o++ = '='; |
| 61 |
*outlen = 2; |
| 62 |
return; |
| 63 |
} |
| 64 |
*outlen = 0; |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
base64_stream_decode_init (struct base64_state *state, int flags) |
| 69 |
{ |
| 70 |
// If any of the codec flags are set, redo choice: |
| 71 |
if (codec.dec == NULL || flags & 0xFFFF) { |
| 72 |
codec_choose(&codec, flags); |
| 73 |
} |
| 74 |
state->eof = 0; |
| 75 |
state->bytes = 0; |
| 76 |
state->carry = 0; |
| 77 |
state->flags = flags; |
| 78 |
} |
| 79 |
|
| 80 |
int |
| 81 |
base64_stream_decode |
| 82 |
( struct base64_state *state |
| 83 |
, const char *src |
| 84 |
, size_t srclen |
| 85 |
, char *out |
| 86 |
, size_t *outlen |
| 87 |
) |
| 88 |
{ |
| 89 |
return codec.dec(state, src, srclen, out, outlen); |
| 90 |
} |
| 91 |
|
| 92 |
#ifdef _OPENMP |
| 93 |
|
| 94 |
// Due to the overhead of initializing OpenMP and creating a team of |
| 95 |
// threads, we require the data length to be larger than a threshold: |
| 96 |
#define OMP_THRESHOLD 20000 |
| 97 |
|
| 98 |
// Conditionally include OpenMP-accelerated codec implementations: |
| 99 |
#include "lib_openmp.c" |
| 100 |
#endif |
| 101 |
|
| 102 |
void |
| 103 |
base64_encode |
| 104 |
( const char *src |
| 105 |
, size_t srclen |
| 106 |
, char *out |
| 107 |
, size_t *outlen |
| 108 |
, int flags |
| 109 |
) |
| 110 |
{ |
| 111 |
size_t s; |
| 112 |
size_t t; |
| 113 |
struct base64_state state; |
| 114 |
|
| 115 |
#ifdef _OPENMP |
| 116 |
if (srclen >= OMP_THRESHOLD) { |
| 117 |
base64_encode_openmp(src, srclen, out, outlen, flags); |
| 118 |
return; |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
// Init the stream reader: |
| 123 |
base64_stream_encode_init(&state, flags); |
| 124 |
|
| 125 |
// Feed the whole string to the stream reader: |
| 126 |
base64_stream_encode(&state, src, srclen, out, &s); |
| 127 |
|
| 128 |
// Finalize the stream by writing trailer if any: |
| 129 |
base64_stream_encode_final(&state, out + s, &t); |
| 130 |
|
| 131 |
// Final output length is stream length plus tail: |
| 132 |
*outlen = s + t; |
| 133 |
} |
| 134 |
|
| 135 |
int |
| 136 |
base64_decode |
| 137 |
( const char *src |
| 138 |
, size_t srclen |
| 139 |
, char *out |
| 140 |
, size_t *outlen |
| 141 |
, int flags |
| 142 |
) |
| 143 |
{ |
| 144 |
int ret; |
| 145 |
struct base64_state state; |
| 146 |
|
| 147 |
#ifdef _OPENMP |
| 148 |
if (srclen >= OMP_THRESHOLD) { |
| 149 |
return base64_decode_openmp(src, srclen, out, outlen, flags); |
| 150 |
} |
| 151 |
#endif |
| 152 |
|
| 153 |
// Init the stream reader: |
| 154 |
base64_stream_decode_init(&state, flags); |
| 155 |
|
| 156 |
// Feed the whole string to the stream reader: |
| 157 |
ret = base64_stream_decode(&state, src, srclen, out, outlen); |
| 158 |
|
| 159 |
// If when decoding a whole block, we're still waiting for input then fail: |
| 160 |
if (ret && (state.bytes == 0)) { |
| 161 |
return ret; |
| 162 |
} |
| 163 |
return 0; |
| 164 |
} |