Improve base64_to_bin().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@735 7dcaeef0-15fb-0310-b436-a5af3365683c
Этот коммит содержится в:
родитель
8f8e9a2f50
Коммит
fc50facaa3
@ -646,7 +646,7 @@ u32 buffer_pass_bytes_end(BUFFER *buffer, u32 len);
|
|||||||
u32 buffer_pass_bytes(BUFFER *buffer, u32 len);
|
u32 buffer_pass_bytes(BUFFER *buffer, u32 len);
|
||||||
|
|
||||||
/* in base64.c */
|
/* in base64.c */
|
||||||
BUFFER *base64_to_bin(char *source);
|
BUFFER *base64_to_bin(const char *source);
|
||||||
unsigned char *bin_to_base64(unsigned char *source, int len);
|
unsigned char *bin_to_base64(unsigned char *source, int len);
|
||||||
|
|
||||||
/* gzip.c */
|
/* gzip.c */
|
||||||
|
@ -45,91 +45,122 @@ static char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|||||||
static int _base64_to_bin(unsigned char dest[3], char *source,int num);
|
static int _base64_to_bin(unsigned char dest[3], char *source,int num);
|
||||||
static int get_equals(char *string);
|
static int get_equals(char *string);
|
||||||
|
|
||||||
/* first part : base 64 to binary */
|
/* First part: base64 to binary */
|
||||||
|
|
||||||
/** \brief base64_to_bin translates a base64 string into a binary one. important,
|
/**
|
||||||
* \returns NULL if something went wrong (ie incorrect char)
|
* @internal
|
||||||
* \returns BUFFER containing the decoded string
|
*
|
||||||
* \internal
|
* @brief Translates a base64 string into a binary one.
|
||||||
|
*
|
||||||
|
* @returns A buffer containing the decoded string, NULL if something went
|
||||||
|
* wrong (e.g. incorrect char).
|
||||||
*/
|
*/
|
||||||
|
BUFFER *base64_to_bin(const char *source) {
|
||||||
BUFFER *base64_to_bin(char *source){
|
|
||||||
int len;
|
|
||||||
int equals;
|
|
||||||
BUFFER *buffer = NULL;
|
BUFFER *buffer = NULL;
|
||||||
unsigned char block[3];
|
unsigned char block[3];
|
||||||
|
char *base64;
|
||||||
|
char *ptr;
|
||||||
|
size_t len;
|
||||||
|
int equals;
|
||||||
|
|
||||||
/* get the number of equals signs, which mirrors the padding */
|
base64 = strdup(source);
|
||||||
equals=get_equals(source);
|
if (base64 == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ptr = base64;
|
||||||
|
|
||||||
|
/* Get the number of equals signs, which mirrors the padding */
|
||||||
|
equals = get_equals(ptr);
|
||||||
if (equals > 2) {
|
if (equals > 2) {
|
||||||
|
SAFE_FREE(base64);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = buffer_new();
|
buffer = buffer_new();
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
|
SAFE_FREE(base64);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
len=strlen(source);
|
len = strlen(ptr);
|
||||||
while (len > 4) {
|
while (len > 4) {
|
||||||
if(_base64_to_bin(block,source,3)){
|
if (_base64_to_bin(block, ptr, 3) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (buffer_add_data(buffer, block, 3) < 0) {
|
if (buffer_add_data(buffer, block, 3) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
len -= 4;
|
len -= 4;
|
||||||
source+=4;
|
ptr += 4;
|
||||||
}
|
}
|
||||||
/* depending of the number of bytes resting, there are 3 possibilities (from the rfc) */
|
|
||||||
|
/*
|
||||||
|
* Depending on the number of bytes resting, there are 3 possibilities
|
||||||
|
* from the RFC.
|
||||||
|
*/
|
||||||
switch (len) {
|
switch (len) {
|
||||||
/* (1) the final quantum of encoding input is an integral
|
/*
|
||||||
multiple of 24 bits; here, the final unit of encoded output will be
|
* (1) The final quantum of encoding input is an integral multiple of
|
||||||
an integral multiple of 4 characters with no "=" padding */
|
* 24 bits. Here, the final unit of encoded output will be an integral
|
||||||
|
* multiple of 4 characters with no "=" padding
|
||||||
|
*/
|
||||||
case 4:
|
case 4:
|
||||||
if (equals != 0) {
|
if (equals != 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if(_base64_to_bin(block,source,3)){
|
if (_base64_to_bin(block, ptr, 3) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (buffer_add_data(buffer, block, 3) < 0) {
|
if (buffer_add_data(buffer, block, 3) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
SAFE_FREE(base64);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
/*(2) the final quantum of encoding input is exactly 8 bits; here, the final
|
/*
|
||||||
unit of encoded output will be two characters followed by two "="
|
* (2) The final quantum of encoding input is exactly 8 bits; here, the
|
||||||
padding characters */
|
* final unit of encoded output will be two characters followed by
|
||||||
|
* two "=" padding characters.
|
||||||
|
*/
|
||||||
case 2:
|
case 2:
|
||||||
if (equals != 2){
|
if (equals != 2){
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if(_base64_to_bin(block,source,1)){
|
|
||||||
|
if (_base64_to_bin(block, ptr, 1) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (buffer_add_data(buffer, block, 1) < 0) {
|
if (buffer_add_data(buffer, block, 1) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
SAFE_FREE(base64);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
/* the final quantum of encoding input is
|
/*
|
||||||
exactly 16 bits; here, the final unit of encoded output will be three
|
* The final quantum of encoding input is exactly 16 bits. Here, the final
|
||||||
characters followed by one "=" padding character */
|
* unit of encoded output will be three characters followed by one "="
|
||||||
|
* padding character.
|
||||||
|
*/
|
||||||
case 3:
|
case 3:
|
||||||
if (equals != 1) {
|
if (equals != 1) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if(_base64_to_bin(block,source,2)){
|
if (_base64_to_bin(block, ptr, 2) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (buffer_add_data(buffer,block,2) < 0) {
|
if (buffer_add_data(buffer,block,2) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
SAFE_FREE(base64);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
default:
|
default:
|
||||||
/* 4,3,2 are the only padding size allowed */
|
/* 4,3,2 are the only padding size allowed */
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
error:
|
error:
|
||||||
|
SAFE_FREE(base64);
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user