Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -41,10 +41,11 @@ OFURL.m \ OFXMLAttribute.m \ OFXMLElement.m \ OFXMLElementBuilder.m \ OFXMLParser.m \ + base64.m \ of_asprintf.m \ unicode.m INCLUDES := ${SRCS:.m=.h} \ OFCollection.h \ Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -160,10 +160,15 @@ * \param nitems The number of items to remove * \param index The index at which the items are removed */ - (void)removeNItems: (size_t)nitems atIndex: (size_t)index; + +/** + * \return A string containing the data in Base64 encoding + */ +- (OFString*)stringByBase64Encoding; @end /** * \brief A class for storing arbitrary big data in an array. * Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -22,10 +22,11 @@ #import "OFDataArray.h" #import "OFString.h" #import "OFFile.h" #import "OFExceptions.h" +#import "base64.h" #import "macros.h" /* References for static linking */ void _references_to_categories_of_OFDataArray() { @@ -301,10 +302,15 @@ OF_HASH_ADD(hash, ((char*)data)[i]); OF_HASH_FINALIZE(hash); return hash; } + +- (OFString*)stringByBase64Encoding +{ + return of_base64_encode(data, count * itemSize); +} @end @implementation OFBigDataArray - (void)addItem: (void*)item { Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -66,6 +66,7 @@ # import "OFThread.h" # import "threading.h" #endif #import "asprintf.h" +#import "base64.h" #import "of_asprintf.h" ADDED src/base64.h Index: src/base64.h ================================================================== --- src/base64.h +++ src/base64.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include + +@class OFString; + +#ifdef __cplusplus +extern "C" { +#endif +extern const char of_base64_table[64]; +extern OFString *of_base64_encode(const char *buf, size_t len); +#ifdef __cplusplus +} +#endif ADDED src/base64.m Index: src/base64.m ================================================================== --- src/base64.m +++ src/base64.m @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFString.h" +#import "base64.h" + +const char of_base64_table[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', + 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', + 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' +}; + +OFString* +of_base64_encode(const char *buf, size_t len) +{ + OFMutableString *ret = [OFMutableString string]; + size_t i, rest; + char tb[4]; + uint32_t sb; + + rest = len % 3; + + for (i = 0; i < len - rest; i += 3) { + sb = (buf[i] << 16) | (buf[i + 1] << 8) | buf[i + 2]; + + tb[0] = of_base64_table[(sb & 0xFC0000) >> 18]; + tb[1] = of_base64_table[(sb & 0x03F000) >> 12]; + tb[2] = of_base64_table[(sb & 0x000FC0) >> 6]; + tb[3] = of_base64_table[(sb & 0x00003F)]; + + [ret appendCStringWithoutUTF8Checking: tb + length: 4]; + } + + switch (rest) { + case 1:; + tb[0] = of_base64_table[buf[i] >> 2]; + tb[1] = of_base64_table[(buf[i] & 3) << 4]; + tb[2] = tb[3] = '='; + + [ret appendCStringWithoutUTF8Checking: tb + length: 4]; + + break; + case 2:; + sb = (buf[i] << 16) | (buf[i + 1] << 8); + + tb[0] = of_base64_table[(sb & 0xFC0000) >> 18]; + tb[1] = of_base64_table[(sb & 0x03F000) >> 12]; + tb[2] = of_base64_table[(sb & 0x000FC0) >> 6]; + tb[3] = '='; + + [ret appendCStringWithoutUTF8Checking: tb + length: 4]; + + break; + } + + return ret; +}