]> err.no Git - sope/blobdiff - sope-core/NGExtensions/NGBase64Coding.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / NGBase64Coding.m
index 2105406f582430e9dabc8072fd28ca85f2b1ac00..3146101dd59abc2cf6f4f8412692a716b2fd3249 100644 (file)
@@ -57,7 +57,7 @@ static int NSStringMaxLineWidth = 1024;
     return @"";
   
   destSize = (len + 2) / 3 * 4; // 3:4 conversion ratio
-  destSize += destSize / NSStringMaxLineWidth + 2; // space for newlines and '\0'
+  destSize += destSize / NSStringMaxLineWidth + 2; // space for '\n' and '\0'
   destSize += 64;
   dest = malloc(destSize + 4);
   if (dest == NULL) return nil;
@@ -162,9 +162,10 @@ static int NSStringMaxLineWidth = 1024;
 
 @implementation NSData(Base64Coding)
 
+// TODO: explain that size (which RFC specifies that?)
 static int NSDataMaxLineWidth = 72;
 
-- (NSData *)dataByEncodingBase64 {
+- (NSData *)dataByEncodingBase64WithLineLength:(unsigned)_lineLength {
   unsigned len;
   size_t destSize;
   size_t destLength = -1;
@@ -174,7 +175,7 @@ static int NSDataMaxLineWidth = 72;
     return [NSData data];
   
   destSize   = (len + 2) / 3 * 4; // 3:4 conversion ratio
-  destSize += destSize / NSDataMaxLineWidth + 2; // space for newlines and '\0'
+  destSize += destSize / _lineLength + 2; // space for newlines and '\0'
   destSize += 64;
 
   dest = malloc(destSize + 4);
@@ -183,13 +184,15 @@ static int NSDataMaxLineWidth = 72;
   NSAssert(dest,         @"invalid buffer ..");
   
   if (encode_base64([self bytes], len,
-                    dest, destSize, &destLength, NSDataMaxLineWidth) == 0) {
+                    dest, destSize, &destLength, _lineLength) == 0) {
     return [NSData dataWithBytesNoCopy:dest length:destLength];
   }
-  else {
-    if (dest) free((void *)dest);
-    return nil;
-  }
+
+  if (dest != NULL) free((void *)dest);
+  return nil;
+}
+- (NSData *)dataByEncodingBase64 {
+  return [self dataByEncodingBase64WithLineLength:NSDataMaxLineWidth];
 }
 
 - (NSData *)dataByDecodingBase64 {