ObjFW  Check-in [39c5a46ea5]

Overview
Comment:OFDataArray: Support for string representations.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 39c5a46ea5ccb1f4c21d4d92411b8cc39e873580c090a39cb11c81c20b0aa812
User & Date: js on 2012-10-05 12:10:05
Other Links: manifest | tags
Context
2012-10-08
08:12
Coding style. check-in: 9276a1c3c8 user: js tags: trunk
2012-10-05
12:10
OFDataArray: Support for string representations. check-in: 39c5a46ea5 user: js tags: trunk
09:54
Add -[OFDataArray description]. check-in: 23ad67cdf7 user: js tags: trunk
Changes

Modified src/OFDataArray.h from [2fa85a1a7b] to [7e3588642e].

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * OFBigDataArray, which allocates the memory in pages rather than in bytes.
 *
 * For security reasons, serialization and deserialization is only implemented
 * for OFDataArrays with item size 1.
 */
@interface OFDataArray: OFObject <OFCopying, OFComparing, OFSerialization>
{
	char   *data;
	size_t count;
	size_t itemSize;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, getter=cArray) void *data;
@property (readonly) size_t count;







|







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * OFBigDataArray, which allocates the memory in pages rather than in bytes.
 *
 * For security reasons, serialization and deserialization is only implemented
 * for OFDataArrays with item size 1.
 */
@interface OFDataArray: OFObject <OFCopying, OFComparing, OFSerialization>
{
	uint8_t *data;
	size_t count;
	size_t itemSize;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, getter=cArray) void *data;
@property (readonly) size_t count;
71
72
73
74
75
76
77









78
79
80
81
82
83
84
 *	  of the specified URL.
 *
 * \param URL The URL to the contents for the OFDataArray
 * \return A new autoreleased OFDataArray
 */
+ dataArrayWithContentsOfURL: (OFURL*)URL;










/**
 * \brief Creates a new OFDataArray with an item size of 1, containing the data
 *	  of the Base64-encoded string.
 *
 * \param string The string with the Base64-encoded data
 * \return A new autoreleased OFDataArray
 */







>
>
>
>
>
>
>
>
>







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 *	  of the specified URL.
 *
 * \param URL The URL to the contents for the OFDataArray
 * \return A new autoreleased OFDataArray
 */
+ dataArrayWithContentsOfURL: (OFURL*)URL;

/**
 * \brief Creates a new OFDataArray with an item size of 1, containing the data
 *	  of the string representation.
 *
 * \param string The string representation of the data
 * \return A new autoreleased OFDataArray
 */
+ dataArrayWithStringRepresentation: (OFString*)string;

/**
 * \brief Creates a new OFDataArray with an item size of 1, containing the data
 *	  of the Base64-encoded string.
 *
 * \param string The string with the Base64-encoded data
 * \return A new autoreleased OFDataArray
 */
114
115
116
117
118
119
120









121
122
123
124
125
126
127
 *	  containing the data of the specified URL.
 *
 * \param URL The URL to the contents for the OFDataArray
 * \return A new autoreleased OFDataArray
 */
- initWithContentsOfURL: (OFURL*)URL;










/**
 * \brief Initializes an already allocated OFDataArray with an item size of 1,
 *	  containing the data of the Base64-encoded string.
 *
 * \param string The string with the Base64-encoded data
 * \return A initialized OFDataArray
 */







>
>
>
>
>
>
>
>
>







123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 *	  containing the data of the specified URL.
 *
 * \param URL The URL to the contents for the OFDataArray
 * \return A new autoreleased OFDataArray
 */
- initWithContentsOfURL: (OFURL*)URL;

/**
 * \brief Initializes an already allocated OFDataArray with an item size of 1,
 *	  containing the data of the string representation.
 *
 * \param string The string representation of the data
 * \return A new autoreleased OFDataArray
 */
- initWithStringRepresentation: (OFString*)string;

/**
 * \brief Initializes an already allocated OFDataArray with an item size of 1,
 *	  containing the data of the Base64-encoded string.
 *
 * \param string The string with the Base64-encoded data
 * \return A initialized OFDataArray
 */
231
232
233
234
235
236
237







238
239
240
241
242
243
244
- (void)removeLastItem;

/**
 * \brief Removes all items.
 */
- (void)removeAllItems;








/**
 * \brief Returns a string containing the data in Base64 encoding.
 *
 * \return A string containing the data in Base64 encoding
 */
- (OFString*)stringByBase64Encoding;








>
>
>
>
>
>
>







249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
- (void)removeLastItem;

/**
 * \brief Removes all items.
 */
- (void)removeAllItems;

/**
 * \brief Returns the string representation of the data array.
 *
 * \return The string representation of the data array.
 */
- (OFString*)stringRepresentation;

/**
 * \brief Returns a string containing the data in Base64 encoding.
 *
 * \return A string containing the data in Base64 encoding
 */
- (OFString*)stringByBase64Encoding;

Modified src/OFDataArray.m from [31a910dc3b] to [3f32c8135b].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFFile.h"
#import "OFURL.h"
#import "OFHTTPRequest.h"
#import "OFXMLElement.h"

#import "OFHTTPRequestFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "autorelease.h"
#import "base64.h"
#import "macros.h"







|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFFile.h"
#import "OFURL.h"
#import "OFHTTPRequest.h"
#import "OFXMLElement.h"

#import "OFHTTPRequestFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "autorelease.h"
#import "base64.h"
#import "macros.h"
60
61
62
63
64
65
66






67
68
69
70
71
72
73
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

+ dataArrayWithContentsOfURL: (OFURL*)URL
{
	return [[[self alloc] initWithContentsOfURL: URL] autorelease];
}







+ dataArrayWithBase64EncodedString: (OFString*)string
{
	return [[[self alloc] initWithBase64EncodedString: string] autorelease];
}

- init







>
>
>
>
>
>







60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

+ dataArrayWithContentsOfURL: (OFURL*)URL
{
	return [[[self alloc] initWithContentsOfURL: URL] autorelease];
}

+ dataArrayWithStringRepresentation: (OFString*)string
{
	return [[[self alloc]
	    initWithStringRepresentation: string] autorelease];
}

+ dataArrayWithBase64EncodedString: (OFString*)string
{
	return [[[self alloc] initWithBase64EncodedString: string] autorelease];
}

- init
156
157
158
159
160
161
162
163






















































164
165
166
167

168
169
170
171
172
173
174
175




176
177
178
179
180
181
182
			       request: request
				result: result];

	self = [[result data] retain];
	objc_autoreleasePoolPop(pool);
	return self;
}























































- initWithBase64EncodedString: (OFString*)string
{
	self = [super init];


	itemSize = 1;

	if (!of_base64_decode(self,
	    [string cStringWithEncoding: OF_STRING_ENCODING_ASCII],
	    [string cStringLengthWithEncoding: OF_STRING_ENCODING_ASCII])) {
		Class c = [self class];
		[self release];
		@throw [OFInvalidEncodingException exceptionWithClass: c];




	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
|

|
|
|
|
|
|
>
>
>
>







162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
			       request: request
				result: result];

	self = [[result data] retain];
	objc_autoreleasePoolPop(pool);
	return self;
}

- initWithStringRepresentation: (OFString*)string
{
	self = [super init];

	@try {
		const char *cString;
		size_t i;

		itemSize = 1;
		count = [string UTF8StringLength];

		if (count & 1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		count >>= 1;
		cString = [string UTF8String];
		data = [self allocMemoryWithSize: count];

		for (i = 0; i < count; i++) {
			uint8_t c1 = cString[2 * i];
			uint8_t c2 = cString[2 * i + 1];
			uint8_t byte;

			if (c1 >= '0' && c1 <= '9')
				byte = (c1 - '0') << 4;
			else if (c1 >= 'a' && c1 <= 'f')
				byte = (c1 - 'a' + 10) << 4;
			else if (c1 >= 'A' && c1 <= 'F')
				byte = (c1 - 'A' + 10) << 4;
			else
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

			if (c2 >= '0' && c2 <= '9')
				byte |= c2 - '0';
			else if (c2 >= 'a' && c2 <= 'f')
				byte |= c2 - 'a' + 10;
			else if (c2 >= 'A' && c2 <= 'F')
				byte |= c2 - 'A' + 10;
			else
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

			data[i] = byte;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithBase64EncodedString: (OFString*)string
{
	self = [super init];

	@try {
		itemSize = 1;

		if (!of_base64_decode(self, [string cStringWithEncoding:
		    OF_STRING_ENCODING_ASCII], [string
		    cStringLengthWithEncoding: OF_STRING_ENCODING_ASCII])) {
			Class c = [self class];
			[self release];
			@throw [OFInvalidFormatException exceptionWithClass: c];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

		stringValue = [element stringValue];

		if (!of_base64_decode(self,
		    [stringValue cStringWithEncoding: OF_STRING_ENCODING_ASCII],
		    [stringValue cStringLengthWithEncoding:
		    OF_STRING_ENCODING_ASCII]))
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}







|







261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

		stringValue = [element stringValue];

		if (!of_base64_decode(self,
		    [stringValue cStringWithEncoding: OF_STRING_ENCODING_ASCII],
		    [stringValue cStringLengthWithEncoding:
		    OF_STRING_ENCODING_ASCII]))
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}
441
442
443
444
445
446
447













448
449
450
451
452
453
454

		for (j = 0; j < itemSize; j++)
			[ret appendFormat: @"%02x", data[i * itemSize + j]];
	}

	[ret appendString: @">"];














	[ret makeImmutable];
	return ret;
}

- (OFString*)stringByBase64Encoding
{
	return of_base64_encode(data, count * itemSize);







>
>
>
>
>
>
>
>
>
>
>
>
>







506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532

		for (j = 0; j < itemSize; j++)
			[ret appendFormat: @"%02x", data[i * itemSize + j]];
	}

	[ret appendString: @">"];

	[ret makeImmutable];
	return ret;
}

- (OFString*)stringRepresentation
{
	OFMutableString *ret = [OFMutableString string];
	size_t i, j;

	for (i = 0; i < count; i++)
		for (j = 0; j < itemSize; j++)
			[ret appendFormat: @"%02x", data[i * itemSize + j]];

	[ret makeImmutable];
	return ret;
}

- (OFString*)stringByBase64Encoding
{
	return of_base64_encode(data, count * itemSize);

Modified src/base64.h from [86d7b49ac9] to [bb1592a059].

32
33
34
35
36
37
38
39
40
41
42
43
@class OFString;
@class OFDataArray;

#ifdef __cplusplus
extern "C" {
#endif
extern const char of_base64_table[64];
extern OFString *of_base64_encode(const char*, size_t);
extern BOOL of_base64_decode(OFDataArray*, const char*, size_t);
#ifdef __cplusplus
}
#endif







|




32
33
34
35
36
37
38
39
40
41
42
43
@class OFString;
@class OFDataArray;

#ifdef __cplusplus
extern "C" {
#endif
extern const char of_base64_table[64];
extern OFString *of_base64_encode(const void*, size_t);
extern BOOL of_base64_decode(OFDataArray*, const char*, size_t);
#ifdef __cplusplus
}
#endif

Modified src/base64.m from [afe4456b9c] to [8d7fe753db].

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
	20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1
};

OFString*
of_base64_encode(const char *data, size_t length)
{
	OFMutableString *ret = [OFMutableString string];
	uint8_t *buffer = (uint8_t*)data;
	size_t i;
	uint8_t rest;
	char tb[4];
	uint32_t sb;







|







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
	20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1
};

OFString*
of_base64_encode(const void *data, size_t length)
{
	OFMutableString *ret = [OFMutableString string];
	uint8_t *buffer = (uint8_t*)data;
	size_t i;
	uint8_t rest;
	char tb[4];
	uint32_t sb;