ObjFW  Check-in [7459fabb87]

Overview
Comment:Add support for Base64 encoding.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7459fabb870c402b653fffb04171a358c9e7a4e5ebd7945684b1f0afbc22382a
User & Date: js on 2011-02-05 15:15:25
Other Links: manifest | tags
Context
2011-02-05
16:48
Add support for Base64 decoding. check-in: 43cadc9699 user: js tags: trunk
15:15
Add support for Base64 encoding. check-in: 7459fabb87 user: js tags: trunk
14:47
Make sure we always have a return buffer in of_asprintf. check-in: a0ff17e12b user: js tags: trunk
Changes

Modified src/Makefile from [27cc76a605] to [c370e44199].

39
40
41
42
43
44
45

46
47
48
49
50
51
52
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\
       OFURL.m			\
       OFXMLAttribute.m		\
       OFXMLElement.m		\
       OFXMLElementBuilder.m	\
       OFXMLParser.m		\

       of_asprintf.m		\
       unicode.m

INCLUDES := ${SRCS:.m=.h}	\
	    OFCollection.h	\
	    ObjFW.h		\
	    asprintf.h		\







>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
       OFTCPSocket.m		\
       ${OFTHREAD_M}		\
       OFURL.m			\
       OFXMLAttribute.m		\
       OFXMLElement.m		\
       OFXMLElementBuilder.m	\
       OFXMLParser.m		\
       base64.m			\
       of_asprintf.m		\
       unicode.m

INCLUDES := ${SRCS:.m=.h}	\
	    OFCollection.h	\
	    ObjFW.h		\
	    asprintf.h		\

Modified src/OFDataArray.h from [bd7e19c676] to [5fefeb5d65].

158
159
160
161
162
163
164





165
166
167
168
169
170
171
 * Removes the specified amount of items at the specified index.
 *
 * \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;





@end

/**
 * \brief A class for storing arbitrary big data in an array.
 *
 * The OFBigDataArray class is a class for storing arbitrary data in an array
 * and is designed to store large hunks of data. Therefore, it allocates







>
>
>
>
>







158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 * Removes the specified amount of items at the specified index.
 *
 * \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.
 *
 * The OFBigDataArray class is a class for storing arbitrary data in an array
 * and is designed to store large hunks of data. Therefore, it allocates

Modified src/OFDataArray.m from [caf2773ad7] to [cb8976524d].

20
21
22
23
24
25
26

27
28
29
30
31
32
33
#include <string.h>
#include <limits.h>

#import "OFDataArray.h"
#import "OFString.h"
#import "OFFile.h"
#import "OFExceptions.h"

#import "macros.h"

/* References for static linking */
void _references_to_categories_of_OFDataArray()
{
	_OFDataArray_Hashing_reference = 1;
};







>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string.h>
#include <limits.h>

#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()
{
	_OFDataArray_Hashing_reference = 1;
};
299
300
301
302
303
304
305





306
307
308
309
310
311
312
	OF_HASH_INIT(hash);
	for (i = 0; i < count * itemSize; i++)
		OF_HASH_ADD(hash, ((char*)data)[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}





@end

@implementation OFBigDataArray
- (void)addItem: (void*)item
{
	size_t nsize, lastpagebyte;








>
>
>
>
>







300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
	OF_HASH_INIT(hash);
	for (i = 0; i < count * itemSize; i++)
		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
{
	size_t nsize, lastpagebyte;

Modified src/ObjFW.h from [5bba582bed] to [f0cdbd0d51].

64
65
66
67
68
69
70

71

#ifdef OF_THREADS
# import "OFThread.h"
# import "threading.h"
#endif

#import "asprintf.h"

#import "of_asprintf.h"







>

64
65
66
67
68
69
70
71
72

#ifdef OF_THREADS
# import "OFThread.h"
# import "threading.h"
#endif

#import "asprintf.h"
#import "base64.h"
#import "of_asprintf.h"

Added src/base64.h version [6b049639b8].

























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
26
27
28
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * 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 <stddef.h>

@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 version [9111828109].



























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * 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;
}