Abdelilah Essiari <aes@george.lbl.gov> reports that for very small
[openssl.git] / crypto / evp / encode.c
index 20d6ec6dd00d936435891aac7a0a361580890da3..6ff9c1783cf60720ee60a29298746ec5e0427356 100644 (file)
 #include "cryptlib.h"
 #include <openssl/evp.h>
 
+#ifndef CHARSET_EBCDIC
 #define conv_bin2ascii(a)      (data_bin2ascii[(a)&0x3f])
 #define conv_ascii2bin(a)      (data_ascii2bin[(a)&0x7f])
+#else
+/* We assume that PEM encoded files are EBCDIC files
+ * (i.e., printable text files). Convert them here while decoding.
+ * When encoding, output is EBCDIC (text) format again.
+ * (No need for conversion in the conv_bin2ascii macro, as the
+ * underlying textstring data_bin2ascii[] is already EBCDIC)
+ */
+#define conv_bin2ascii(a)      (data_bin2ascii[(a)&0x3f])
+#define conv_ascii2bin(a)      (data_ascii2bin[os_toascii[a]&0x7f])
+#endif
 
 /* 64 char lines
  * pad input with 0
@@ -174,7 +185,7 @@ void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
        *outl=ret;
        }
 
-int EVP_EncodeBlock(unsigned char *t, unsigned char *f, int dlen)
+int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen)
        {
        int i,ret=0;
        unsigned long l;
@@ -281,7 +292,17 @@ int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
                /* If we are at the end of input and it looks like a
                 * line, process it. */
                if (((i+1) == inl) && (((n&3) == 0) || eof))
+                       {
                        v=B64_EOF;
+                       /* In case things were given us in really small
+                          records (so two '=' were given in separate
+                          updates), eof may contain the incorrect number
+                          of ending bytes to skip, so let's redo the count */
+                       eof = 0;
+                       if (d[n-1] == '=') eof++;
+                       if (d[n-2] == '=') eof++;
+                       /* There will never be more than two '=' */
+                       }
 
                if ((v == B64_EOF) || (n >= 64))
                        {
@@ -326,7 +347,7 @@ end:
        return(rv);
        }
 
-int EVP_DecodeBlock(unsigned char *t, unsigned char *f, int n)
+int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
        {
        int i,ret=0,a,b,c,d;
        unsigned long l;