From b953fca1660be4d25447475a6c406374e2a21da0 Mon Sep 17 00:00:00 2001
From: david gauchard <gauchard@laas.fr>
Date: Tue, 5 Jun 2018 00:24:26 +0200
Subject: [PATCH] fix base64_encode_expected_len

---
 cores/esp8266/libb64/cencode.c | 6 ++----
 cores/esp8266/libb64/cencode.h | 7 ++++++-
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/cores/esp8266/libb64/cencode.c b/cores/esp8266/libb64/cencode.c
index 0859f36605..4b902997de 100755
--- a/cores/esp8266/libb64/cencode.c
+++ b/cores/esp8266/libb64/cencode.c
@@ -7,13 +7,11 @@ For details, see http://sourceforge.net/projects/libb64
 
 #include "cencode.h"
 
-const int CHARS_PER_LINE = 72;
-
 void base64_init_encodestate(base64_encodestate* state_in){
   state_in->step = step_A;
   state_in->result = 0;
   state_in->stepcount = 0;
-  state_in->stepsnewline = CHARS_PER_LINE;
+  state_in->stepsnewline = BASE64_CHARS_PER_LINE;
 }
 
 
@@ -72,7 +70,7 @@ int base64_encode_block(const char* plaintext_in, int length_in, char* code_out,
       *codechar++ = base64_encode_value(result);
       
       ++(state_in->stepcount);
-      if ((state_in->stepcount == CHARS_PER_LINE/4) && (state_in->stepsnewline > 0)){
+      if ((state_in->stepcount == BASE64_CHARS_PER_LINE/4) && (state_in->stepsnewline > 0)){
         *codechar++ = '\n';
         state_in->stepcount = 0;
       }
diff --git a/cores/esp8266/libb64/cencode.h b/cores/esp8266/libb64/cencode.h
index 7890e7a4a6..7c0efc22a0 100755
--- a/cores/esp8266/libb64/cencode.h
+++ b/cores/esp8266/libb64/cencode.h
@@ -8,7 +8,12 @@ For details, see http://sourceforge.net/projects/libb64
 #ifndef BASE64_CENCODE_H
 #define BASE64_CENCODE_H
 
-#define base64_encode_expected_len(n) ((((4 * n) / 3) + 3) & ~3)
+#define BASE64_CHARS_PER_LINE 72
+
+#define base64_encode_expected_len_nonewlines(n) ((((4 * (n)) / 3) + 3) & ~3)
+#define base64_encode_expected_len(n) \
+       (base64_encode_expected_len_nonewlines(n) + ((n / ((BASE64_CHARS_PER_LINE * 3) / 4)) + 1))
+
 
 #ifdef __cplusplus
 extern "C" {