aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libssh/priv.h2
-rw-r--r--libssh/base64.c47
2 files changed, 30 insertions, 19 deletions
diff --git a/include/libssh/priv.h b/include/libssh/priv.h
index 27293321..2bbe4d8a 100644
--- a/include/libssh/priv.h
+++ b/include/libssh/priv.h
@@ -647,7 +647,7 @@ u32 buffer_pass_bytes(BUFFER *buffer, u32 len);
/* in base64.c */
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 */
int compress_buffer(SSH_SESSION *session,BUFFER *buf);
diff --git a/libssh/base64.c b/libssh/base64.c
index 6583915d..63b50dce 100644
--- a/libssh/base64.c
+++ b/libssh/base64.c
@@ -231,7 +231,7 @@ static int get_equals(char *string) {
/* thanks sysk for debugging my mess :) */
#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) {
switch (len) {
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){
- int flen=len + (3 - (len %3)); /* round to upper 3 multiple */
- unsigned char *buffer;
- unsigned char *ptr;
- flen=(4 * flen)/3 + 1 ;
- ptr=buffer=malloc(flen);
- while(len>0){
- _bin_to_base64(ptr,source,len>3?3:len);
- ptr+=4;
- source +=3;
- len -=3;
- }
- ptr[0]=0;
- return buffer;
+unsigned char *bin_to_base64(const unsigned char *source, int len) {
+ unsigned char *base64;
+ unsigned char *ptr;
+ int flen = len + (3 - (len % 3)); /* round to upper 3 multiple */
+ flen = (4 * flen) / 3 + 1;
+
+ base64 = malloc(flen);
+ if (base64 == NULL) {
+ return NULL;
+ }
+ ptr = base64;
+
+ while(len > 0){
+ _bin_to_base64(ptr, source, len > 3 ? 3 : len);
+ ptr += 4;
+ source += 3;
+ len -= 3;
+ }
+ ptr[0] = '\0';
+
+ return base64;
}
+