Revision: 28589
          http://sourceforge.net/p/bibdesk/svn/28589
Author:   hofman
Date:     2024-01-11 15:25:16 +0000 (Thu, 11 Jan 2024)
Log Message:
-----------
use Foundation methods to convert to and from base64

Modified Paths:
--------------
    trunk/bibdesk/BDSKLinkedFile.m
    trunk/bibdesk/BDSKSoapBinding.m
    trunk/bibdesk/NSData_BDSKExtensions.h
    trunk/bibdesk/NSData_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKLinkedFile.m
===================================================================
--- trunk/bibdesk/BDSKLinkedFile.m      2024-01-11 14:57:34 UTC (rev 28588)
+++ trunk/bibdesk/BDSKLinkedFile.m      2024-01-11 15:25:16 UTC (rev 28589)
@@ -40,7 +40,6 @@
 #import <CoreServices/CoreServices.h>
 #import "BDSKRuntime.h"
 #import "NSString_BDSKExtensions.h"
-#import "NSData_BDSKExtensions.h"
 #import "NSURL_BDSKExtensions.h"
 #import "BDSKComplexString.h"
 
@@ -351,7 +350,7 @@
         
         NSData *data = nil;
         @try {
-            data = [[NSData alloc] initWithBase64String:base64String];
+            data = [[NSData alloc] initWithBase64EncodedString:base64String 
options:NSDataBase64DecodingIgnoreUnknownCharacters];
         }
         @catch(id exception) {
             data = nil;
@@ -523,9 +522,9 @@
     if (dictionary == nil)
         dictionary = [NSDictionary dictionaryWithObjectsAndKeys:path, 
RELATIVEPATH_KEY, nil];
     if (saveArchivedData)
-        return [[NSKeyedArchiver archivedDataWithRootObject:dictionary] 
base64String];
+        return [[NSKeyedArchiver archivedDataWithRootObject:dictionary] 
base64EncodedStringWithOptions:0];
     else
-        return [[NSPropertyListSerialization dataWithPropertyList:dictionary 
format:NSPropertyListBinaryFormat_v1_0 options:0 error:NULL] base64String];
+        return [[NSPropertyListSerialization dataWithPropertyList:dictionary 
format:NSPropertyListBinaryFormat_v1_0 options:0 error:NULL] 
base64EncodedStringWithOptions:0];
 }
 
 // implemented by the subclasses

Modified: trunk/bibdesk/BDSKSoapBinding.m
===================================================================
--- trunk/bibdesk/BDSKSoapBinding.m     2024-01-11 14:57:34 UTC (rev 28588)
+++ trunk/bibdesk/BDSKSoapBinding.m     2024-01-11 15:25:16 UTC (rev 28589)
@@ -114,7 +114,7 @@
        // set version 1.1 - how?
        [request setHTTPBody:bodyData];
        if (self.authUsername && self.authPassword) {
-               NSString *authString = [[[NSString stringWithFormat:@"%@:%@", 
self.authUsername, self.authPassword] dataUsingEncoding:NSUTF8StringEncoding] 
base64String];
+               NSString *authString = [[[NSString stringWithFormat:@"%@:%@", 
self.authUsername, self.authPassword] dataUsingEncoding:NSUTF8StringEncoding] 
base64EncodedStringWithOptions:0];
                [request setValue:[NSString stringWithFormat:@"Basic %@", 
authString] forHTTPHeaderField:@"Authorization"];
        }
        

Modified: trunk/bibdesk/NSData_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSData_BDSKExtensions.h       2024-01-11 14:57:34 UTC (rev 
28588)
+++ trunk/bibdesk/NSData_BDSKExtensions.h       2024-01-11 15:25:16 UTC (rev 
28589)
@@ -58,9 +58,6 @@
 
 @property (nonatomic, readonly) NSData *sha1Signature;
 
-- (id)initWithBase64String:(NSString *)base64String;
-@property (nonatomic, readonly) NSString *base64String;
-
 - (id)initWithHexString:(NSString *)hexString;
 @property (nonatomic, readonly) NSString *hexString;
 

Modified: trunk/bibdesk/NSData_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSData_BDSKExtensions.m       2024-01-11 14:57:34 UTC (rev 
28588)
+++ trunk/bibdesk/NSData_BDSKExtensions.m       2024-01-11 15:25:16 UTC (rev 
28589)
@@ -128,133 +128,9 @@
     return offset > 0 ? [NSData dataWithBytes:signature 
length:signatureLength] : nil;
 }
 
-// The following code is taken and modified from Matt Gallagher's code at 
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
-
-// Mapping from 6 bit pattern to ASCII character.
-static unsigned char base64EncodeTable[65] = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-// Definition for "masked-out" areas of the base64DecodeTable and 
hexDecodeTable mapping
+// Definition for "masked-out" areas of the hexDecodeTable mapping
 #define xx 0xFF
 
-// Mapping from ASCII character to 6 bit pattern.
-static unsigned char base64DecodeTable[256] =
-{
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63, 
-    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, 
-    xx,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 
-    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx, 
-    xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
-    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
-};
-
-// Fundamental sizes of the binary and base64 encode/decode units in bytes
-#define BINARY_UNIT_SIZE 3
-#define BASE64_UNIT_SIZE 4
-
-- (id)initWithBase64String:(NSString *)base64String {
-    NSData *data = [base64String dataUsingEncoding:NSASCIIStringEncoding];
-    size_t length = [data length];
-    const unsigned char *inputBuffer = (const unsigned char *)[data bytes];
-    size_t outputBufferSize = (length / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;
-    unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize);
-    
-    size_t i = 0, j = 0;
-    while (i < length) {
-               // Accumulate 4 valid characters (ignore everything else)
-               unsigned char accumulated[BASE64_UNIT_SIZE];
-               size_t accumulateIndex = 0;
-               while (i < length) {
-                       unsigned char decode = 
base64DecodeTable[inputBuffer[i++]];
-                       if (decode != xx) {
-                               accumulated[accumulateIndex] = decode;
-                               accumulateIndex++;
-                               
-                               if (accumulateIndex == BASE64_UNIT_SIZE)
-                                       break;
-                       }
-               }
-               
-               // Store the 6 bits from each of the 4 characters as 3 bytes
-               outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);
-               outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] 
>> 2);
-               outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3];
-               j += accumulateIndex - 1;
-    }
-    
-    NSData *result = [self initWithBytes:outputBuffer length:j];
-    
-    free(outputBuffer);
-    
-    return result;
-}
-
-- (NSString *)base64String {
-    size_t length = [self length];
-    const unsigned char *inputBuffer = (const unsigned char *)[self bytes];
-    
-    #define MAX_NUM_PADDING_CHARS 2
-    #define OUTPUT_LINE_LENGTH 64
-    #define INPUT_LINE_LENGTH ((OUTPUT_LINE_LENGTH / BASE64_UNIT_SIZE) * 
BINARY_UNIT_SIZE)
-    
-    // Byte accurate calculation of final buffer size
-    size_t outputBufferSize = ((length / BINARY_UNIT_SIZE) + ((length % 
BINARY_UNIT_SIZE) ? 1 : 0)) * BASE64_UNIT_SIZE;
-    
-    // Allocate the output buffer
-    char *outputBuffer = (char *)malloc(outputBufferSize);
-    if (outputBuffer == NULL)
-               return NULL;
-
-    size_t i = 0;
-    size_t j = 0;
-    size_t lineEnd = length;
-    
-    while (true) {
-               if (lineEnd > length)
-                       lineEnd = length;
-
-               for (; i + BINARY_UNIT_SIZE - 1 < lineEnd; i += 
BINARY_UNIT_SIZE) {
-                       // Inner loop: turn 48 bytes into 64 base64 characters
-                       outputBuffer[j++] = base64EncodeTable[(inputBuffer[i] & 
0xFC) >> 2];
-                       outputBuffer[j++] = base64EncodeTable[((inputBuffer[i] 
& 0x03) << 4) | ((inputBuffer[i + 1] & 0xF0) >> 4)];
-                       outputBuffer[j++] = base64EncodeTable[((inputBuffer[i + 
1] & 0x0F) << 2) | ((inputBuffer[i + 2] & 0xC0) >> 6)];
-                       outputBuffer[j++] = base64EncodeTable[inputBuffer[i + 
2] & 0x3F];
-               }
-               
-               if (lineEnd == length)
-                       break;
-    }
-    
-    if (i + 1 < length) {
-               // Handle the single '=' case
-               outputBuffer[j++] = base64EncodeTable[(inputBuffer[i] & 0xFC) 
>> 2];
-               outputBuffer[j++] = base64EncodeTable[((inputBuffer[i] & 0x03) 
<< 4) | ((inputBuffer[i + 1] & 0xF0) >> 4)];
-               outputBuffer[j++] = base64EncodeTable[(inputBuffer[i + 1] & 
0x0F) << 2];
-               outputBuffer[j++] = '=';
-    } else if (i < length) {
-               // Handle the double '=' case
-               outputBuffer[j++] = base64EncodeTable[(inputBuffer[i] & 0xFC) 
>> 2];
-               outputBuffer[j++] = base64EncodeTable[(inputBuffer[i] & 0x03) 
<< 4];
-               outputBuffer[j++] = '=';
-               outputBuffer[j++] = '=';
-    }
-    
-    NSString *result = [[NSString alloc] initWithBytes:outputBuffer length:j 
encoding:NSASCIIStringEncoding];
-    
-    free(outputBuffer);
-    
-    return result;
-}
-
 // Mapping from 4 bit pattern to ASCII character.
 static unsigned char hexEncodeTable[17] = "0123456789ABCDEF";
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to