Skip to content

Instantly share code, notes, and snippets.

@draganglumac
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save draganglumac/6af4f5890c3511d71b14 to your computer and use it in GitHub Desktop.

Select an option

Save draganglumac/6af4f5890c3511d71b14 to your computer and use it in GitHub Desktop.
Shifty solution
uint8_t* jnx_encoder_b64_encode(jnx_encoder *e,uint8_t *data, size_t input_length, size_t *output_length) {
JNXCHECK(data);
JNXCHECK(input_length);
*output_length = 4 * ((input_length + 2) / 3);
uint8_t *encoded_data = malloc(((*output_length) +1) * sizeof (char));
if (encoded_data == NULL) return NULL;
int i,j;
for ( i = 0, j = 0; i < input_length; i += 3) {
uint32_t triplet = 0;
for (c = 0; c < 3; c++)
triplet = (triplet << 0x08) & data[i + c];
encoded_data[j++] = e->b64_encoding_table[(triplet >> 3 * 6) & 0x3F];
encoded_data[j++] = e->b64_encoding_table[(triplet >> 2 * 6) & 0x3F];
encoded_data[j++] = e->b64_encoding_table[(triplet >> 1 * 6) & 0x3F];
encoded_data[j++] = e->b64_encoding_table[(triplet >> 0 * 6) & 0x3F];
}
int l;
for(l = 0; l < e->b64_mod_table[input_length %3]; l++) {
encoded_data[*output_length -1 -l] = '=';
}
encoded_data[*output_length ] = '\0';
return encoded_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment