mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 09:54:25 +09:00
base64: Reformat _bin_to_base64()
Fixes T188 Signed-off-by: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
47
src/base64.c
47
src/base64.c
@@ -234,29 +234,32 @@ static int get_equals(char *string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* thanks sysk for debugging my mess :) */
|
/* thanks sysk for debugging my mess :) */
|
||||||
|
static void _bin_to_base64(unsigned char *dest,
|
||||||
|
const unsigned char source[3],
|
||||||
|
int len)
|
||||||
|
{
|
||||||
#define BITS(n) ((1 << (n)) - 1)
|
#define BITS(n) ((1 << (n)) - 1)
|
||||||
static void _bin_to_base64(unsigned char *dest, const unsigned char source[3],
|
switch (len) {
|
||||||
int len) {
|
case 1:
|
||||||
switch (len) {
|
dest[0] = alphabet[(source[0] >> 2)];
|
||||||
case 1:
|
dest[1] = alphabet[((source[0] & BITS(2)) << 4)];
|
||||||
dest[0] = alphabet[(source[0] >> 2)];
|
dest[2] = '=';
|
||||||
dest[1] = alphabet[((source[0] & BITS(2)) << 4)];
|
dest[3] = '=';
|
||||||
dest[2] = '=';
|
break;
|
||||||
dest[3] = '=';
|
case 2:
|
||||||
break;
|
dest[0] = alphabet[source[0] >> 2];
|
||||||
case 2:
|
dest[1] = alphabet[(source[1] >> 4) | ((source[0] & BITS(2)) << 4)];
|
||||||
dest[0] = alphabet[source[0] >> 2];
|
dest[2] = alphabet[(source[1] & BITS(4)) << 2];
|
||||||
dest[1] = alphabet[(source[1] >> 4) | ((source[0] & BITS(2)) << 4)];
|
dest[3] = '=';
|
||||||
dest[2] = alphabet[(source[1] & BITS(4)) << 2];
|
break;
|
||||||
dest[3] = '=';
|
case 3:
|
||||||
break;
|
dest[0] = alphabet[(source[0] >> 2)];
|
||||||
case 3:
|
dest[1] = alphabet[(source[1] >> 4) | ((source[0] & BITS(2)) << 4)];
|
||||||
dest[0] = alphabet[(source[0] >> 2)];
|
dest[2] = alphabet[(source[2] >> 6) | (source[1] & BITS(4)) << 2];
|
||||||
dest[1] = alphabet[(source[1] >> 4) | ((source[0] & BITS(2)) << 4)];
|
dest[3] = alphabet[source[2] & BITS(6)];
|
||||||
dest[2] = alphabet[ (source[2] >> 6) | (source[1] & BITS(4)) << 2];
|
break;
|
||||||
dest[3] = alphabet[source[2] & BITS(6)];
|
}
|
||||||
break;
|
#undef BITS
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user