]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Changes armor.c to be able to handle both LF and CRLF inputs (output is
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Sun, 10 Apr 2011 07:22:43 +0000 (09:22 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Sun, 10 Apr 2011 07:22:43 +0000 (09:22 +0200)
still either LF-only or CRLF-only depending on the platform). Patch by LRN.

Optimizations in the usage of strlen().

lib/opencdk/armor.c

index 2928182d92be275cbbdcf8048eebf8ea65b463c5..8491aa993c1661aa4e4a46b7c3950f2b1d8f0622 100644 (file)
 
 #ifdef __MINGW32__
 #define LF "\r\n"
+#define ALTLF "\n"
 #else
 #define LF "\n"
+#define ALTLF "\r\n"
 #endif
 
 #define CRCINIT 0xB704CE
@@ -493,6 +495,7 @@ armor_decode (void *data, FILE * in, FILE * out)
   ssize_t nread = 0;
   int i, pgp_data = 0;
   cdk_error_t rc = 0;
+  int len;
 
   if (!afx)
     {
@@ -526,7 +529,7 @@ armor_decode (void *data, FILE * in, FILE * out)
       s = fgets (buf, DIM (buf) - 1, in);
       if (!s)
         return CDK_EOF;
-      if (strlen (s) == strlen (LF))
+      if (strcmp (s, LF) == 0 || strcmp (s, ALTLF) == 0)
         {
           rc = 0;
           break;                /* empty line */
@@ -560,7 +563,13 @@ armor_decode (void *data, FILE * in, FILE * out)
       s = fgets (buf, DIM (buf) - 1, in);
       if (!s)
         break;
-      buf[strlen (buf) - strlen (LF)] = '\0';
+        
+      len = strlen(buf);
+
+      if (buf[len - 1] == '\n')
+        buf[len - 1] = '\0';
+      if (buf[len - 1] == '\r')
+        buf[len - 1] = '\0';
       if (buf[0] == '=' && strlen (s) == 5)
         {                       /* CRC */
           memset (crcbuf, 0, sizeof (crcbuf));
@@ -582,7 +591,11 @@ armor_decode (void *data, FILE * in, FILE * out)
   s = fgets (buf, DIM (buf) - 1, in);
   if (s)
     {
-      buf[strlen (buf) - strlen (LF)] = '\0';
+      int len = strlen(buf);
+      if (buf[len - 1] == '\n')
+        buf[len - 1] = '\0';
+      if (buf[len - 1] == '\r')
+        buf[len - 1] = '\0';
       rc = CDK_General_Error;
       afx->idx2 = search_header (buf, armor_end);
       if (afx->idx2 >= 0)