Author: rfm
Date: Sun Aug 30 11:23:27 2015
New Revision: 38954

URL: http://svn.gna.org/viewcvs/gnustep?rev=38954&view=rev
Log:
more tweaks ... move base64 encoding to shared private function

Modified:
    libs/base/trunk/Source/Additions/GSMime.m
    libs/base/trunk/Source/GSHTTPURLHandle.m
    libs/base/trunk/Source/GSPrivate.h
    libs/base/trunk/Source/NSPropertyList.m
    libs/base/trunk/Source/NSString.m
    libs/base/trunk/Source/NSURLProtocol.m

Modified: libs/base/trunk/Source/Additions/GSMime.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/Additions/GSMime.m?rev=38954&r1=38953&r2=38954&view=diff
==============================================================================
--- libs/base/trunk/Source/Additions/GSMime.m   (original)
+++ libs/base/trunk/Source/Additions/GSMime.m   Sun Aug 30 11:23:27 2015
@@ -139,17 +139,16 @@
   dst[2] = ((src[2] & 0x03) << 6) |  (src[3] & 0x3F);
 }
 
-static char b64[]
-  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-static int
-encodebase64(unsigned char *dst, const unsigned char *src, int length)
+void
+GSPrivateEncodeBase64(const uint8_t *src, NSUInteger length, uint8_t *dst)
 {
   int  dIndex = 0;
   int  sIndex;
 
   for (sIndex = 0; sIndex < length; sIndex += 3)
     {
+      static char b64[]
+        = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
       int      c0 = src[sIndex];
       int      c1 = (sIndex+1 < length) ? src[sIndex+1] : 0;
       int      c2 = (sIndex+2 < length) ? src[sIndex+2] : 0;
@@ -174,9 +173,7 @@
        dst[dIndex - 1] = '=';
        dst[dIndex - 2] = '=';
      }
-  return dIndex;
-}
-
+}
 
 static void
 encodeQuotedPrintable(NSMutableData *result,
@@ -4442,7 +4439,7 @@
   dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen);
 #endif
 
-  destlen = encodebase64(dBuf, sBuf, length);
+  GSPrivateEncodeBase64(sBuf, length, dBuf);
 
   return AUTORELEASE([[NSData allocWithZone: NSDefaultMallocZone()]
     initWithBytesNoCopy: dBuf length: destlen]);
@@ -5727,14 +5724,12 @@
 - (NSString*) makeBoundary
 {
   static int           count = 0;
-  unsigned char                output[20];
-  unsigned char                *ptr;
-  NSMutableData                *md;
+  uint8_t              output[20];
+  uint8_t              *ptr;
   NSString             *result;
   NSData               *source;
   NSData               *digest;
   int                  sequence = ++count;
-  int                  length;
 
   source = [[[NSProcessInfo processInfo] globallyUniqueString]
     dataUsingEncoding: NSUTF8StringEncoding];
@@ -5746,16 +5741,15 @@
   output[18] = (sequence >> 8) & 0xff;
   output[19] = sequence & 0xff;
 
-  md = [NSMutableData allocWithZone: NSDefaultMallocZone()];
-  md = [md initWithLength: 40];
-  length = encodebase64([md mutableBytes], output, 20);
-  [md setLength: length + 2];
-  ptr = (unsigned char*)[md mutableBytes];
-  ptr[length] = '=';
-  ptr[length+1] = '_';
+  ptr = (uint8_t*)NSZoneMalloc(NSDefaultMallocZone(), 30);
+  GSPrivateEncodeBase64(output, 20, ptr);
+  ptr[28] = '=';
+  ptr[29] = '_';
   result = [NSStringClass allocWithZone: NSDefaultMallocZone()];
-  result = [result initWithData: md encoding: NSASCIIStringEncoding];
-  RELEASE(md);
+  result = [result initWithBytesNoCopy: ptr
+                                length: 30
+                              encoding: NSASCIIStringEncoding
+                          freeWhenDone: YES];
   return AUTORELEASE(result);
 }
 

Modified: libs/base/trunk/Source/GSHTTPURLHandle.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/GSHTTPURLHandle.m?rev=38954&r1=38953&r2=38954&view=diff
==============================================================================
--- libs/base/trunk/Source/GSHTTPURLHandle.m    (original)
+++ libs/base/trunk/Source/GSHTTPURLHandle.m    Sun Aug 30 11:23:27 2015
@@ -231,46 +231,62 @@
 static void
 debugRead(GSHTTPURLHandle *handle, NSData *data)
 {
-  unsigned     len = (unsigned)[data length];
-  const char   *ptr = (const char*)[data bytes];
+  NSUInteger   len = [data length];
+  const uint8_t        *ptr = (const uint8_t*)[data bytes];
+  uint8_t       *hex;
+  NSUInteger    hl;
   int           pos;
 
+  hl = ((len + 2) / 3) * 4;
+  hex = malloc(hl + 1);
+  hex[hl] = '\0';
+  GSPrivateEncodeBase64(ptr, len, hex);
   for (pos = 0; pos < len; pos++)
     {
       if (0 == ptr[pos])
         {
           char  *esc = [data escapedRepresentation: 0];
 
-          NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@",
-            handle, len, esc, data); 
+          NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n<[%*.*s]>",
+            handle, (unsigned)len, esc, hex); 
           free(esc);
+          free(hex);
           return;
         }
     }
-  NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@",
-    handle, len, len, len, ptr, data); 
+  NSLog(@"Read for %p of %d bytes - '%s'\n<[%*.*s]>",
+    handle, (unsigned)len, ptr, hex); 
+  free(hex);
 }
 static void
 debugWrite(GSHTTPURLHandle *handle, NSData *data)
 {
-  unsigned     len = (unsigned)[data length];
-  const char   *ptr = (const char*)[data bytes];
+  NSUInteger   len = [data length];
+  const uint8_t        *ptr = (const uint8_t*)[data bytes];
+  uint8_t       *hex;
+  NSUInteger    hl;
   int           pos;
 
+  hl = ((len + 2) / 3) * 4;
+  hex = malloc(hl + 1);
+  hex[hl] = '\0';
+  GSPrivateEncodeBase64(ptr, len, hex);
   for (pos = 0; pos < len; pos++)
     {
       if (0 == ptr[pos])
         {
           char  *esc = [data escapedRepresentation: 0];
 
-          NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@",
-            handle, len, esc, data); 
+          NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n<[%s]>",
+            handle, (unsigned)len, esc, hex); 
           free(esc);
+          free(hex);
           return;
         }
     }
-  NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@",
-    handle, len, len, len, ptr, data); 
+  NSLog(@"Write for %p of %d bytes - '%s'\n<[%s]>",
+    handle, (unsigned)len, ptr, hex); 
+  free(hex);
 }
 
 + (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl

Modified: libs/base/trunk/Source/GSPrivate.h
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/GSPrivate.h?rev=38954&r1=38953&r2=38954&view=diff
==============================================================================
--- libs/base/trunk/Source/GSPrivate.h  (original)
+++ libs/base/trunk/Source/GSPrivate.h  Sun Aug 30 11:23:27 2015
@@ -593,5 +593,13 @@
 GSPrivateThreadID()
   GS_ATTRIB_PRIVATE;
 
+/** Function to base64 encode data.  The destination buffer must be of
+ * size (((length + 2) / 3) * 4) or more.
+ */
+void
+GSPrivateEncodeBase64(const uint8_t *src, NSUInteger length, uint8_t *dst)
+  GS_ATTRIB_PRIVATE;
+
+
 #endif /* _GSPrivate_h_ */
 

Modified: libs/base/trunk/Source/NSPropertyList.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSPropertyList.m?rev=38954&r1=38953&r2=38954&view=diff
==============================================================================
--- libs/base/trunk/Source/NSPropertyList.m     (original)
+++ libs/base/trunk/Source/NSPropertyList.m     Sun Aug 30 11:23:27 2015
@@ -1475,57 +1475,19 @@
 
 #include <math.h>
 
-static char base64[]
-  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
 static void
 encodeBase64(NSData *source, NSMutableData *dest)
 {
-  int          length = [source length];
-  int          enclen = length / 3;
-  int          remlen = length - 3 * enclen;
-  int          destlen = 4 * ((length + 2) / 3);
-  unsigned char *sBuf;
-  unsigned char *dBuf;
-  int          sIndex = 0;
-  int          dIndex = [dest length];
-
-  [dest setLength: dIndex + destlen];
-
-  if (length == 0)
-    {
-      return;
-    }
-  sBuf = (unsigned char*)[source bytes];
-  dBuf = [dest mutableBytes];
-
-  for (sIndex = 0; sIndex < length - 2; sIndex += 3, dIndex += 4)
-    {
-      dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
-      dBuf[dIndex + 1]
-       = base64[((sBuf[sIndex] << 4) | (sBuf[sIndex + 1] >> 4)) & 0x3f];
-      dBuf[dIndex + 2]
-       = base64[((sBuf[sIndex + 1] << 2) | (sBuf[sIndex + 2] >> 6)) & 0x3f];
-      dBuf[dIndex + 3] = base64[sBuf[sIndex + 2] & 0x3f];
-    }
-
-  if (remlen == 1)
-    {
-      dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
-      dBuf[dIndex + 1] = (sBuf[sIndex] << 4) & 0x30;
-      dBuf[dIndex + 1] = base64[dBuf[dIndex + 1]];
-      dBuf[dIndex + 2] = '=';
-      dBuf[dIndex + 3] = '=';
-    }
-  else if (remlen == 2)
-    {
-      dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
-      dBuf[dIndex + 1] = (sBuf[sIndex] << 4) & 0x30;
-      dBuf[dIndex + 1] |= sBuf[sIndex + 1] >> 4;
-      dBuf[dIndex + 1] = base64[dBuf[dIndex + 1]];
-      dBuf[dIndex + 2] = (sBuf[sIndex + 1] << 2) & 0x3c;
-      dBuf[dIndex + 2] = base64[dBuf[dIndex + 2]];
-      dBuf[dIndex + 3] = '=';
+  NSUInteger   length = [source length];
+
+  if (length > 0)
+    {
+      NSUInteger       base = [dest length];
+      NSUInteger       destlen = 4 * ((length + 2) / 3);
+
+      [dest setLength: base + destlen];
+      GSPrivateEncodeBase64((const uint8_t*)[source bytes],
+        length, (uint8_t*)[dest mutableBytes]);
     }
 }
 

Modified: libs/base/trunk/Source/NSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSString.m?rev=38954&r1=38953&r2=38954&view=diff
==============================================================================
--- libs/base/trunk/Source/NSString.m   (original)
+++ libs/base/trunk/Source/NSString.m   Sun Aug 30 11:23:27 2015
@@ -109,8 +109,6 @@
 #endif
 
 #import "GNUstepBase/Unicode.h"
-
-#import "GSPrivate.h"
 
 extern BOOL GSScanDouble(unichar*, unsigned, double*);
 

Modified: libs/base/trunk/Source/NSURLProtocol.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSURLProtocol.m?rev=38954&r1=38953&r2=38954&view=diff
==============================================================================
--- libs/base/trunk/Source/NSURLProtocol.m      (original)
+++ libs/base/trunk/Source/NSURLProtocol.m      Sun Aug 30 11:23:27 2015
@@ -72,55 +72,70 @@
 debugRead(id handle, unsigned len, const unsigned char *ptr)
 {
   int           pos;
-  NSData        *data;
-
-  data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
-                                      length: len
-                                freeWhenDone: NO];
+  uint8_t       *hex;
+  NSUInteger    hl;
+
+  hl = ((len + 2) / 3) * 4;
+  hex = malloc(hl + 1);
+  hex[hl] = '\0';
+  GSPrivateEncodeBase64(ptr, len, hex);
 
   for (pos = 0; pos < len; pos++)
     {
       if (0 == ptr[pos])
         {
-          char  *esc = [data escapedRepresentation: 0];
-
-          NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@",
-            handle, len, esc, data); 
+          NSData        *data;
+          char          *esc;
+
+          data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
+                                              length: len
+                                        freeWhenDone: NO];
+          esc = [data escapedRepresentation: 0];
+
+          NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n<[%s]>",
+            handle, len, esc, hex); 
           free(esc);
           RELEASE(data);
+          free(hex);
           return;
         }
     }
-  NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@",
-    handle, len, len, len, ptr, data); 
-  RELEASE(data);
+  NSLog(@"Read for %p of %d bytes - '%s'\n<[%s]>", handle, len, ptr, hex); 
+  free(hex);
 }
 static void
 debugWrite(id handle, unsigned len, const unsigned char *ptr)
 {
   int           pos;
-  NSData        *data;
-
-  data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
-                                      length: len
-                                freeWhenDone: NO];
+  uint8_t       *hex;
+  NSUInteger    hl;
+
+  hl = ((len + 2) / 3) * 4;
+  hex = malloc(hl + 1);
+  hex[hl] = '\0';
+  GSPrivateEncodeBase64(ptr, len, hex);
 
   for (pos = 0; pos < len; pos++)
     {
       if (0 == ptr[pos])
         {
-          char  *esc = [data escapedRepresentation: 0];
-
-          NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@",
-            handle, len, esc, data); 
+          NSData        *data;
+          char          *esc;
+
+          data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
+                                              length: len
+                                        freeWhenDone: NO];
+          esc = [data escapedRepresentation: 0];
+          NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n<[%s]>",
+            handle, len, esc, hex); 
           free(esc);
           RELEASE(data);
+          free(hex);
           return;
         }
     }
-  NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@",
-    handle, len, len, len, ptr, data); 
-  RELEASE(data);
+  NSLog(@"Write for %p of %d bytes - '%s'\n<[%s]>", handle, len, ptr, hex); 
+  free(hex);
 }
 
 @interface     GSSocketStreamPair : NSObject


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to