Improve bin_to_base64() and use const for source.

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@743 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-05-05 09:16:08 +00:00
parent fdc1073e8a
commit c7806a6a16
2 changed files with 30 additions and 19 deletions

View File

@@ -647,7 +647,7 @@ u32 buffer_pass_bytes(BUFFER *buffer, u32 len);
/* in base64.c */ /* in base64.c */
BUFFER *base64_to_bin(const char *source); BUFFER *base64_to_bin(const char *source);
unsigned char *bin_to_base64(unsigned char *source, int len); unsigned char *bin_to_base64(const unsigned char *source, int len);
/* gzip.c */ /* gzip.c */
int compress_buffer(SSH_SESSION *session,BUFFER *buf); int compress_buffer(SSH_SESSION *session,BUFFER *buf);

View File

@@ -231,7 +231,7 @@ static int get_equals(char *string) {
/* thanks sysk for debugging my mess :) */ /* thanks sysk for debugging my mess :) */
#define BITS(n) ((1 << (n)) - 1) #define BITS(n) ((1 << (n)) - 1)
static void _bin_to_base64(unsigned char *dest, unsigned char source[3], static void _bin_to_base64(unsigned char *dest, const unsigned char source[3],
int len) { int len) {
switch (len) { switch (len) {
case 1: case 1:
@@ -255,22 +255,33 @@ static void _bin_to_base64(unsigned char *dest, unsigned char source[3],
} }
} }
/** \brief Converts binary data to a base64 string /**
* \returns the converted string * @internal
* \internal *
* @brief Converts binary data to a base64 string.
*
* @returns the converted string
*/ */
unsigned char *bin_to_base64(unsigned char *source, int len){ unsigned char *bin_to_base64(const unsigned char *source, int len) {
int flen=len + (3 - (len %3)); /* round to upper 3 multiple */ unsigned char *base64;
unsigned char *buffer; unsigned char *ptr;
unsigned char *ptr; int flen = len + (3 - (len % 3)); /* round to upper 3 multiple */
flen=(4 * flen)/3 + 1 ; flen = (4 * flen) / 3 + 1;
ptr=buffer=malloc(flen);
while(len>0){ base64 = malloc(flen);
_bin_to_base64(ptr,source,len>3?3:len); if (base64 == NULL) {
ptr+=4; return NULL;
source +=3; }
len -=3; ptr = base64;
}
ptr[0]=0; while(len > 0){
return buffer; _bin_to_base64(ptr, source, len > 3 ? 3 : len);
ptr += 4;
source += 3;
len -= 3;
}
ptr[0] = '\0';
return base64;
} }