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