f8b687
diff --git a/include/base64.h b/include/base64.h
f8b687
index 0ff7116..381ef5d 100644
f8b687
--- a/include/base64.h
f8b687
+++ b/include/base64.h
f8b687
@@ -36,7 +36,6 @@
f8b687
 #include <assert.h>
f8b687
 #include "types.h"
f8b687
 
f8b687
-#define B64_BUF 16384
f8b687
 #define FAIL -1
f8b687
 #define SKIP -2
f8b687
 
f8b687
diff --git a/src/base64.c b/src/base64.c
f8b687
index fd01bac..1b0f301 100644
f8b687
--- a/src/base64.c
f8b687
+++ b/src/base64.c
f8b687
@@ -85,11 +85,9 @@ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL
f8b687
 };
f8b687
 
f8b687
 /* Returns NULL on error */
f8b687
-/* FIXME Possible buffer overflow on outputs larger than B64_BUF */
f8b687
 char* encode_base64(byte* src,size_t ssize)
f8b687
 {
f8b687
   char* outbuf;
f8b687
-  char* retbuf;
f8b687
   int pos;
f8b687
   int i, l, left;
f8b687
   unsigned long triple;
f8b687
@@ -101,7 +99,10 @@ char* encode_base64(byte* src,size_t ssize)
f8b687
     error(240,"\n");
f8b687
     return NULL;
f8b687
   }
f8b687
-  outbuf = (char *)malloc(sizeof(char)*B64_BUF);
f8b687
+
f8b687
+  /* length of encoded base64 string (padded) */
f8b687
+  size_t length = sizeof(char)* ((ssize + 2) / 3) * 4;
f8b687
+  outbuf = (char *)malloc(length + 1);
f8b687
   
f8b687
   /* Initialize working pointers */
f8b687
   inb = src;
f8b687
@@ -162,20 +163,14 @@ char* encode_base64(byte* src,size_t ssize)
f8b687
       inb++;
f8b687
   }
f8b687
   
f8b687
-  /* outbuf is not completely used so we use retbuf */
f8b687
-  retbuf=(char*)malloc(sizeof(char)*(pos+1));
f8b687
-  memcpy(retbuf,outbuf,pos);
f8b687
-  retbuf[pos]='\0';
f8b687
-  free(outbuf);
f8b687
+  outbuf[pos]='\0';
f8b687
 
f8b687
-  return retbuf;
f8b687
+  return outbuf;
f8b687
 }
f8b687
 
f8b687
-/* FIXME Possible buffer overflow on outputs larger than B64_BUF */
f8b687
 byte* decode_base64(char* src,size_t ssize, size_t *ret_len)
f8b687
 {
f8b687
   byte* outbuf;
f8b687
-  byte* retbuf;
f8b687
   char* inb;
f8b687
   int i;
f8b687
   int l;
f8b687
@@ -188,10 +183,18 @@ byte* decode_base64(char* src,size_t ssize, size_t *ret_len)
f8b687
   if (!ssize||src==NULL)
f8b687
     return NULL;
f8b687
 
f8b687
+  /* exit on unpadded input */
f8b687
+  if (ssize % 4) {
f8b687
+    error(3, "decode_base64: '%s' has invalid length (missing padding characters?)", src);
f8b687
+    return NULL;
f8b687
+  }
f8b687
+
f8b687
+  /* calculate length of decoded string, substract padding chars if any (ssize is >= 4) */
f8b687
+  size_t length = sizeof(byte) * ((ssize / 4) * 3)- (src[ssize-1] == '=') - (src[ssize-2] == '=');
f8b687
 
f8b687
   /* Initialize working pointers */
f8b687
   inb = src;
f8b687
-  outbuf = (byte *)malloc(sizeof(byte)*B64_BUF);
f8b687
+  outbuf = (byte *)malloc(length + 1);
f8b687
 
f8b687
   l = 0;
f8b687
   triple = 0;
f8b687
@@ -243,15 +246,11 @@ byte* decode_base64(char* src,size_t ssize, size_t *ret_len)
f8b687
       inb++;
f8b687
     }
f8b687
   
f8b687
-  retbuf=(byte*)malloc(sizeof(byte)*(pos+1));
f8b687
-  memcpy(retbuf,outbuf,pos);
f8b687
-  retbuf[pos]='\0';
f8b687
-  
f8b687
-  free(outbuf);
f8b687
+  outbuf[pos]='\0';
f8b687
 
f8b687
   if (ret_len) *ret_len = pos;
f8b687
   
f8b687
-  return retbuf;
f8b687
+  return outbuf;
f8b687
 }
f8b687
 
f8b687
 size_t length_base64(char* src,size_t ssize)
f8b687
diff --git a/src/db.c b/src/db.c
f8b687
index 858240d..62c4faa 100644
f8b687
--- a/src/db.c
f8b687
+++ b/src/db.c
f8b687
@@ -664,13 +664,15 @@ db_line* db_char2line(char** ss,int db){
f8b687
 
f8b687
 time_t base64totime_t(char* s){
f8b687
   
f8b687
+  if(strcmp(s,"0")==0){
f8b687
+      return 0;
f8b687
+  }
f8b687
   byte* b=decode_base64(s,strlen(s),NULL);
f8b687
   char* endp;
f8b687
   
f8b687
-  if (b==NULL||strcmp(s,"0")==0) {
f8b687
+  if (b==NULL) {
f8b687
     
f8b687
     /* Should we print error here? */
f8b687
-    free(b);
f8b687
     
f8b687
     return 0;
f8b687
   } else {