aboutsummaryrefslogtreecommitdiff
path: root/libssh/base64.c
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2009-05-05 09:16:08 +0000
committerAndreas Schneider <mail@cynapses.org>2009-05-05 09:16:08 +0000
commitc7806a6a1663eec349b9b68030338bdad0933717 (patch)
tree077eaa60629db81a50022b388780920aef775f03 /libssh/base64.c
parentfdc1073e8ade96210db688eb23748931f7235768 (diff)
downloadlibssh-c7806a6a1663eec349b9b68030338bdad0933717.tar.gz
libssh-c7806a6a1663eec349b9b68030338bdad0933717.tar.xz
libssh-c7806a6a1663eec349b9b68030338bdad0933717.zip
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
Diffstat (limited to 'libssh/base64.c')
-rw-r--r--libssh/base64.c47
1 files changed, 29 insertions, 18 deletions
diff --git a/libssh/base64.c b/libssh/base64.c
index 6583915..63b50dc 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;
}
+