Use m_tempfile and better errors msg
[apps/madmutt.git] / base64.c
index ad6c6c4..9fdf037 100644 (file)
--- a/base64.c
+++ b/base64.c
@@ -13,8 +13,8 @@
  * 
  *     You should have received a copy of the GNU General Public
  *     License along with this program; if not, write to the Free
- *     Software Foundation, Inc., 59 Temple Place - Suite 330, 
- *     Boston, MA  02111, USA.
+ *     Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ *     Boston, MA 02110-1301, USA.
  * 
  */
 
  * 
  */
 
-#if HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include <lib-lib/lib-lib.h>
+#include <lib-mime/mime.h>
 
 #include "mutt.h"
-#include "mime.h"
-
-#define BAD     -1
 
 /* raw bytes to null-terminated base 64 string */
-void mutt_to_base64 (unsigned char *out, const unsigned char *in, size_t len,
-                     size_t olen)
+void mutt_to_base64 (unsigned char *out, const unsigned char *in, ssize_t len,
+                     ssize_t olen)
 {
   while (len >= 3 && olen > 10) {
-    *out++ = B64Chars[in[0] >> 2];
-    *out++ = B64Chars[((in[0] << 4) & 0x30) | (in[1] >> 4)];
-    *out++ = B64Chars[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
-    *out++ = B64Chars[in[2] & 0x3f];
+    *out++ = __m_b64chars[in[0] >> 2];
+    *out++ = __m_b64chars[((in[0] << 4) & 0x30) | (in[1] >> 4)];
+    *out++ = __m_b64chars[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
+    *out++ = __m_b64chars[in[2] & 0x3f];
     olen -= 4;
     len -= 3;
     in += 3;
@@ -66,12 +62,12 @@ void mutt_to_base64 (unsigned char *out, const unsigned char *in, size_t len,
   if (len > 0 && olen > 4) {
     unsigned char fragment;
 
-    *out++ = B64Chars[in[0] >> 2];
+    *out++ = __m_b64chars[in[0] >> 2];
     fragment = (in[0] << 4) & 0x30;
     if (len > 1)
       fragment |= in[1] >> 4;
-    *out++ = B64Chars[fragment];
-    *out++ = (len < 2) ? '=' : B64Chars[(in[1] << 2) & 0x3c];
+    *out++ = __m_b64chars[fragment];
+    *out++ = (len < 2) ? '=' : __m_b64chars[(in[1] << 2) & 0x3c];
     *out++ = '=';
   }
   *out = '\0';
@@ -86,32 +82,31 @@ int mutt_from_base64 (char *out, const char *in)
 
   do {
     digit1 = in[0];
-    if (digit1 > 127 || base64val (digit1) == BAD)
+    if (base64val(digit1) < 0)
       return -1;
     digit2 = in[1];
-    if (digit2 > 127 || base64val (digit2) == BAD)
+    if (base64val(digit2) < 0)
       return -1;
     digit3 = in[2];
-    if (digit3 > 127 || ((digit3 != '=') && (base64val (digit3) == BAD)))
+    if (digit3 != '=' && base64val(digit3) < 0)
       return -1;
     digit4 = in[3];
-    if (digit4 > 127 || ((digit4 != '=') && (base64val (digit4) == BAD)))
+    if (digit4 != '=' && base64val(digit4) < 0)
       return -1;
     in += 4;
 
     /* digits are already sanity-checked */
-    *out++ = (base64val (digit1) << 2) | (base64val (digit2) >> 4);
+    *out++ = (base64val(digit1) << 2) | (base64val(digit2) >> 4);
     len++;
     if (digit3 != '=') {
-      *out++ = ((base64val (digit2) << 4) & 0xf0) | (base64val (digit3) >> 2);
+      *out++ = ((base64val(digit2) << 4) & 0xf0) | (base64val(digit3) >> 2);
       len++;
       if (digit4 != '=') {
-        *out++ = ((base64val (digit3) << 6) & 0xc0) | base64val (digit4);
+        *out++ = ((base64val(digit3) << 6) & 0xc0) | base64val(digit4);
         len++;
       }
     }
-  }
-  while (*in && digit4 != '=');
+  } while (*in && digit4 != '=');
 
   return len;
 }