]> err.no Git - sope/blobdiff - sope-core/NGExtensions/NGQuotedPrintableCoding.m
fixed resource/module lookup on 64bit systems
[sope] / sope-core / NGExtensions / NGQuotedPrintableCoding.m
index 23c0845a0a8ad608bfe177857ff68168f5c50e02..22780df013b127cfcf2d4f0522be9a236f249225 100644 (file)
@@ -1,27 +1,26 @@
 /*
-  Copyright (C) 2000-2003 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OGo
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.
 
   You should have received a copy of the GNU Lesser General Public
-  License along with OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
-// $Id$
 
-#include "common.h"
 #include "NGQuotedPrintableCoding.h"
+#include "common.h"
 #include "NGMemoryAllocation.h"
 
 @implementation NSString(QuotedPrintableCoding)
   NSData   *data;
   unsigned len;
   
-  if ((len = [self cStringLength])) {
+  if ((len = [self cStringLength]) > 0) {
     void *buf;
     buf = malloc(len + 10);
     [self getCString:buf];
     data = [NSData dataWithBytes:buf length:len];
-    free(buf);
+    if (buf) free(buf);
   }
   else
     data = [NSData data];
 
 // implementation
 
-static inline char __hexToChar(char c) {
+static inline signed char __hexToChar(char c) {
   if ((c > 47) && (c < 58)) // '0' .. '9'
     return c - 48;
   if ((c > 64) && (c < 71)) // 'A' .. 'F'
@@ -111,7 +110,14 @@ static inline char __hexToChar(char c) {
 }
 
 int NGDecodeQuotedPrintable(const char *_src, unsigned _srcLen,
-                            char *_dest, unsigned _destLen) {
+                            char *_dest, unsigned _destLen)
+{
+  /*
+    Eg: "Hello=20World" => "Hello World"
+
+    =XY where XY is a hex encoded byte. In addition '_' is decoded as 0x20
+    (not as space!, this depends on the charset, see RFC 2047 4.2).
+  */
   unsigned cnt     = 0;
   unsigned destCnt = 0;
 
@@ -120,33 +126,38 @@ int NGDecodeQuotedPrintable(const char *_src, unsigned _srcLen,
 
   for (cnt = 0; ((cnt < _srcLen) && (destCnt < _destLen)); cnt++) {
     if (_src[cnt] != '=') {
-      _dest[destCnt++] = _src[cnt];
+      _dest[destCnt] = _src[cnt] == '_' ? 0x20 : _src[cnt];
+      destCnt++;
     }
     else {
       if ((_srcLen - cnt) > 1) {
         signed char c1, c2;
 
-        c1 = _src[++cnt];
-
+       cnt++;          // skip '='
+        c1 = _src[cnt]; // first hex digit
+       
         if (c1 == '\r' || c1 == '\n') {
-          if (_src[cnt+1] == '\r' || _src[cnt+1] == '\n' )
+          if (_src[cnt + 1] == '\r' || _src[cnt + 1] == '\n' )
             cnt++;
           continue;
         }
         c1 = __hexToChar(c1);
-        c2 = __hexToChar(_src[++cnt]);
+       
+       cnt++; // skip first hex digit
+        c2 = __hexToChar(_src[cnt]);
         
         if ((c1 == -1) || (c2 == -1)) {
           if ((_destLen - destCnt) > 1) {
-            _dest[destCnt++] = _src[cnt - 1];
-            _dest[destCnt++] = _src[cnt];
+            _dest[destCnt] = _src[cnt - 1]; destCnt++;
+            _dest[destCnt] = _src[cnt];     destCnt++;
           }
           else
             break;
         }
         else {
-          char c = ((c1 << 4) | c2);
-          _dest[destCnt++] = c;
+          register unsigned char c = ((c1 << 4) | c2);
+          _dest[destCnt] = c;
+         destCnt++;
         }
       }
       else