src/server/util/util.c

changeset 62
c47e081b6c0f
parent 59
ab25c0a231d0
child 63
66442f81f823
--- a/src/server/util/util.c	Thu May 09 13:19:51 2013 +0200
+++ b/src/server/util/util.c	Thu May 09 19:41:11 2013 +0200
@@ -56,6 +56,58 @@
 
 #include "util.h"
 
+
+
+/* ------------------------------ _uudecode ------------------------------- */
+
+static const unsigned char pr2six[256] = {
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
+    52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,
+    10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
+    28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
+    64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
+    64,64,64,64,64,64,64,64,64,64,64,64,64
+};
+
+/** you MUST reserve at least 2 additional bytes for bufout */
+size_t util_base64decode(char *bufcoded, size_t codedbytes, char *bufout) {
+    register char *bufin = bufcoded;
+    register int nprbytes;
+    size_t nbytesdecoded;
+
+    /* Find the length */
+    nprbytes = (int) codedbytes;
+    while(pr2six[(int)(bufin[nprbytes-1])] >= 64) {
+      nprbytes--;
+    }
+    nbytesdecoded = ((nprbytes+3)/4) * 3;
+
+    while (nprbytes > 0) {
+        *(bufout++) = (unsigned char)
+            (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
+        *(bufout++) = (unsigned char)
+            (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
+        *(bufout++) = (unsigned char)
+            (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
+        bufin += 4;
+        nprbytes -= 4;
+    }
+
+    if(nprbytes & 03) {
+        if(pr2six[(int)bufin[-2]] > 63)
+            nbytesdecoded -= 2;
+        else
+            nbytesdecoded -= 1;
+    }
+
+    return nbytesdecoded;
+}
+
 /*
 NSAPI_PUBLIC int util_getboolean(const char *v, int def) {
     if(v[0] == 'T' || v[0] == 't') {

mercurial