ObjFW  Check-in [b6a39dd3d1]

Overview
Comment:Rename a few methods returning BOOL.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b6a39dd3d1ccef5ed8e8f3e5bf82bd1c1d8c369c30449a1f55bfd2e502a817fc
User & Date: js on 2010-10-24 13:15:01
Other Links: manifest | tags
Context
2010-10-24
13:30
Always write abbreviations uppercase in method names. check-in: 1d19a5586b user: js tags: trunk
13:15
Rename a few methods returning BOOL. check-in: b6a39dd3d1 user: js tags: trunk
12:54
Add -[streamDidReceiveException:] to OFStreamObserverDelegate. check-in: b5da54000d user: js tags: trunk
Changes

Modified src/OFFile.m from [1cd28fbc32] to [9c2025e097].

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

	@try {
		src = [OFFile fileWithPath: from
				      mode: @"rb"];
		dest = [OFFile fileWithPath: to
				       mode: @"wb"];

		while (![src atEndOfStream]) {
			size_t len = [src readNBytes: 4096
					  intoBuffer: buf];
			[dest writeNBytes: len
			       fromBuffer: buf];
		}

#ifndef _WIN32







|







321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

	@try {
		src = [OFFile fileWithPath: from
				      mode: @"rb"];
		dest = [OFFile fileWithPath: to
				       mode: @"wb"];

		while (![src isAtEndOfStream]) {
			size_t len = [src readNBytes: 4096
					  intoBuffer: buf];
			[dest writeNBytes: len
			       fromBuffer: buf];
		}

#ifndef _WIN32
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
	self = [super init];

	fd = fd_;

	return self;
}

- (BOOL)_atEndOfStream
{
	if (fd == -1)
		return YES;

	return eos;
}








|







453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
	self = [super init];

	fd = fd_;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	if (fd == -1)
		return YES;

	return eos;
}

Modified src/OFHash.h from [90cfc63c8c] to [11414f28b0].

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
#import "OFObject.h"

/**
 * \brief A base class for classes providing hash functions.
 */
@interface OFHash: OFObject
{
	BOOL	 calculated;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly) BOOL calculated;
#endif

/**
 * Adds a buffer to the hash to be calculated.
 *
 * \param buf The buffer which should be included into the calculation.
 * \param size The size of the buffer
 */
- (void)updateWithBuffer: (const char*)buf
		  ofSize: (size_t)size;

/**
 * \return A buffer containing the hash. The size of the buffer is depending
 *	   on the hash used. The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;

/**
 * \return A boolean whether the hash has already been calculated
 */
- (BOOL)calculated;
@end







|



|




















|

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
#import "OFObject.h"

/**
 * \brief A base class for classes providing hash functions.
 */
@interface OFHash: OFObject
{
	BOOL	 isCalculated;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly) BOOL isCalculated;
#endif

/**
 * Adds a buffer to the hash to be calculated.
 *
 * \param buf The buffer which should be included into the calculation.
 * \param size The size of the buffer
 */
- (void)updateWithBuffer: (const char*)buf
		  ofSize: (size_t)size;

/**
 * \return A buffer containing the hash. The size of the buffer is depending
 *	   on the hash used. The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;

/**
 * \return A boolean whether the hash has already been calculated
 */
- (BOOL)isCalculated;
@end

Modified src/OFHash.m from [0764c2c77a] to [9d73a3e0b8].

24
25
26
27
28
29
30
31
32
33
34
35

- (uint8_t*)digest
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)calculated
{
	return calculated;
}
@end







|

|


24
25
26
27
28
29
30
31
32
33
34
35

- (uint8_t*)digest
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)isCalculated
{
	return isCalculated;
}
@end

Modified src/OFMD5Hash.m from [063d887ad6] to [e3c55f9a8d].

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		  ofSize: (size_t)size
{
	uint32_t t;

	if (size == 0)
		return;

	if (calculated)
		@throw [OFHashAlreadyCalculatedException newWithClass: isa];

	/* Update bitcount */
	t = bits[0];
	if ((bits[0] = t + ((uint32_t)size << 3)) < t)
		/* Carry from low to high */
		bits[1]++;







|







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		  ofSize: (size_t)size
{
	uint32_t t;

	if (size == 0)
		return;

	if (isCalculated)
		@throw [OFHashAlreadyCalculatedException newWithClass: isa];

	/* Update bitcount */
	t = bits[0];
	if ((bits[0] = t + ((uint32_t)size << 3)) < t)
		/* Carry from low to high */
		bits[1]++;
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
}

- (uint8_t*)digest
{
	uint8_t	*p;
	size_t	count;

	if (calculated)
		return (uint8_t*)buf;

	/* Compute number of bytes mod 64 */
	count = (bits[0] >> 3) & 0x3F;

	/*
	 * Set the first char of padding to 0x80. This is safe since there is







|







184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
}

- (uint8_t*)digest
{
	uint8_t	*p;
	size_t	count;

	if (isCalculated)
		return (uint8_t*)buf;

	/* Compute number of bytes mod 64 */
	count = (bits[0] >> 3) & 0x3F;

	/*
	 * Set the first char of padding to 0x80. This is safe since there is
222
223
224
225
226
227
228
229
230
231
232
233
	/* Append length in bits and transform */
	((uint32_t*)in)[14] = bits[0];
	((uint32_t*)in)[15] = bits[1];

	md5_transform(buf, (uint32_t*)in);
	OF_BSWAP32_V_IF_BE(buf, 4);

	calculated = YES;

	return (uint8_t*)buf;
}
@end







|




222
223
224
225
226
227
228
229
230
231
232
233
	/* Append length in bits and transform */
	((uint32_t*)in)[14] = bits[0];
	((uint32_t*)in)[15] = bits[1];

	md5_transform(buf, (uint32_t*)in);
	OF_BSWAP32_V_IF_BE(buf, 4);

	isCalculated = YES;

	return (uint8_t*)buf;
}
@end

Modified src/OFSHA1Hash.m from [20af66d343] to [0f9a7bf269].

146
147
148
149
150
151
152
153
154
155
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
183
184
185

- (void)updateWithBuffer: (const char*)buf
		  ofSize: (size_t)size
{
	if (size == 0)
		return;

	if (calculated)
		@throw [OFHashAlreadyCalculatedException newWithClass: isa];

	sha1_update(state, &count, buffer, buf, size);
}

- (uint8_t*)digest
{
	size_t i;
	char   finalcount[8];

	if (calculated)
		return digest;

	for (i = 0; i < 8; i++)
		/* Endian independent */
		finalcount[i] = (char)((count >> ((7 - (i & 7)) * 8)) & 255);
	sha1_update(state, &count, buffer, "\200", 1);

	while ((count & 504) != 448)
		sha1_update(state, &count, buffer, "\0", 1);
	/* Should cause a sha1_transform() */
	sha1_update(state, &count, buffer, finalcount, 8);

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++)
		digest[i] = (char)((state[i >> 2] >>
		    ((3 - (i & 3)) * 8)) & 255);

	calculated = YES;

	return digest;
}
@end







|










|
















|




146
147
148
149
150
151
152
153
154
155
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
183
184
185

- (void)updateWithBuffer: (const char*)buf
		  ofSize: (size_t)size
{
	if (size == 0)
		return;

	if (isCalculated)
		@throw [OFHashAlreadyCalculatedException newWithClass: isa];

	sha1_update(state, &count, buffer, buf, size);
}

- (uint8_t*)digest
{
	size_t i;
	char   finalcount[8];

	if (isCalculated)
		return digest;

	for (i = 0; i < 8; i++)
		/* Endian independent */
		finalcount[i] = (char)((count >> ((7 - (i & 7)) * 8)) & 255);
	sha1_update(state, &count, buffer, "\200", 1);

	while ((count & 504) != 448)
		sha1_update(state, &count, buffer, "\0", 1);
	/* Should cause a sha1_transform() */
	sha1_update(state, &count, buffer, finalcount, 8);

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++)
		digest[i] = (char)((state[i >> 2] >>
		    ((3 - (i & 3)) * 8)) & 255);

	isCalculated = YES;

	return digest;
}
@end

Modified src/OFStream.h from [0915ec3638] to [d7703b509d].

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
@class OFString;
@class OFDataArray;

/**
 * \brief A base class for different types of streams.
 *
 * IMPORTANT: If you want to subclass this, override _readNBytes:intoBuffer:,
 * _writeNBytes:fromBuffer: and _atEndOfStream, but nothing else. Those are not
 * defined in the headers, but do the actual work. OFStream uses those and does
 * all the caching and other stuff. If you override these methods without the
 * _ prefix, you *WILL* break caching and get broken results!
 */
@interface OFStream: OFObject
{
@public
	char   *cache;
@protected
	char   *wBuffer;
	size_t cacheLen, wBufferLen;
	BOOL   bufferWrites;;
}

/**
 * Returns a boolean whether the end of the stream has been reached.
 *
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)atEndOfStream;

/**
 * Reads at most size bytes from the stream into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read at most.
 *	       The buffer MUST be at least size big!







|
|
|
|








|







|







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
@class OFString;
@class OFDataArray;

/**
 * \brief A base class for different types of streams.
 *
 * IMPORTANT: If you want to subclass this, override _readNBytes:intoBuffer:,
 * _writeNBytes:fromBuffer: and _isAtEndOfStream, but nothing else. Those are
 * not defined in the headers, but do the actual work. OFStream uses those and
 * does all the caching and other stuff. If you override these methods without
 * the _ prefix, you *WILL* break caching and get broken results!
 */
@interface OFStream: OFObject
{
@public
	char   *cache;
@protected
	char   *wBuffer;
	size_t cacheLen, wBufferLen;
	BOOL   buffersWrites;
}

/**
 * Returns a boolean whether the end of the stream has been reached.
 *
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)isAtEndOfStream;

/**
 * Reads at most size bytes from the stream into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read at most.
 *	       The buffer MUST be at least size big!
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
 */
- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (enum of_string_encoding)encoding;

/**
 * \return A boolean whether writes are buffered
 */
- (BOOL)bufferWrites;

/**
 * Enables or disables the write buffer.
 *
 * \param enable Whether the write buffer should be enabled or disabled
 */
- (void)setBufferWrites: (BOOL)enable;

/**
 * Writes everythig in the write buffer to the stream.
 */
- (void)flushWriteBuffer;

/**







|






|







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
 */
- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (enum of_string_encoding)encoding;

/**
 * \return A boolean whether writes are buffered
 */
- (BOOL)buffersWrites;

/**
 * Enables or disables the write buffer.
 *
 * \param enable Whether the write buffer should be enabled or disabled
 */
- (void)setBuffersWrites: (BOOL)enable;

/**
 * Writes everythig in the write buffer to the stream.
 */
- (void)flushWriteBuffer;

/**

Modified src/OFStream.m from [55e134209d] to [712dcb6dbd].

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

	cache = NULL;
	wBuffer = NULL;

	return self;
}

- (BOOL)_atEndOfStream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)atEndOfStream
{
	if (cache != NULL)
		return NO;

	return [self _atEndOfStream];
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	if (cache == NULL)
		return [self _readNBytes: size







|



















|




|







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

	cache = NULL;
	wBuffer = NULL;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)isAtEndOfStream
{
	if (cache != NULL)
		return NO;

	return [self _isAtEndOfStream];
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	if (cache == NULL)
		return [self _readNBytes: size
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	OFDataArray *a;
	char *buf;

	a = [OFDataArray dataArrayWithItemSize: 1];
	buf = [self allocMemoryWithSize: of_pagesize];

	@try {
		while (![self atEndOfStream]) {
			size_t size;

			size = [self readNBytes: of_pagesize
				     intoBuffer: buf];
			[a addNItems: size
			  fromCArray: buf];
		}







|







208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	OFDataArray *a;
	char *buf;

	a = [OFDataArray dataArrayWithItemSize: 1];
	buf = [self allocMemoryWithSize: of_pagesize];

	@try {
		while (![self isAtEndOfStream]) {
			size_t size;

			size = [self readNBytes: of_pagesize
				     intoBuffer: buf];
			[a addNItems: size
			  fromCArray: buf];
		}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	}

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _atEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret_len = cacheLen;

				if (ret_len > 0 && cache[ret_len - 1] == '\r')
					ret_len--;







|







268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	}

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _isAtEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret_len = cacheLen;

				if (ret_len > 0 && cache[ret_len - 1] == '\r')
					ret_len--;
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	}

	/* Read until we get the delimiter or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _atEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cacheLen];








|







427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	}

	/* Read until we get the delimiter or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _isAtEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cacheLen];

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
		[self freeMemory: tmp];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (BOOL)bufferWrites
{
	return bufferWrites;
}

- (void)setBufferWrites: (BOOL)enable
{
	bufferWrites = enable;
}

- (void)flushWriteBuffer
{
	if (wBuffer == NULL)
		return;

	[self _writeNBytes: wBufferLen
		fromBuffer: wBuffer];

	[self freeMemory: wBuffer];
	wBuffer = NULL;
	wBufferLen = 0;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	if (!bufferWrites)
		return [self _writeNBytes: size
			       fromBuffer: buf];
	else {
		wBuffer = [self resizeMemory: wBuffer
				      toSize: wBufferLen + size];
		memcpy(wBuffer + wBufferLen, buf, size);
		wBufferLen += size;







|

|


|

|


















|







511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
		[self freeMemory: tmp];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (BOOL)buffersWrites
{
	return buffersWrites;
}

- (void)setBuffersWrites: (BOOL)enable
{
	buffersWrites = enable;
}

- (void)flushWriteBuffer
{
	if (wBuffer == NULL)
		return;

	[self _writeNBytes: wBufferLen
		fromBuffer: wBuffer];

	[self freeMemory: wBuffer];
	wBuffer = NULL;
	wBufferLen = 0;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	if (!buffersWrites)
		return [self _writeNBytes: size
			       fromBuffer: buf];
	else {
		wBuffer = [self resizeMemory: wBuffer
				      toSize: wBufferLen + size];
		memcpy(wBuffer + wBufferLen, buf, size);
		wBufferLen += size;

Modified src/OFStreamSocket.m from [6a144d8c67] to [a907635fe9].

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#endif

+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_atEndOfStream
{
	return eos;
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{







|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#endif

+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_isAtEndOfStream
{
	return eos;
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{

Modified tests/OFMD5HashTests.m from [2445ed3eca] to [38087890ea].

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *md5;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash]))


	while (![f atEndOfStream]) {
		char buf[64];
		size_t len = [f readNBytes: 64
				intoBuffer: buf];
		[md5 updateWithBuffer: buf
			       ofSize: len];
	}
	[f close];







<
|







32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *md5;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash]))


	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readNBytes: 64
				intoBuffer: buf];
		[md5 updateWithBuffer: buf
			       ofSize: len];
	}
	[f close];

Modified tests/OFSHA1HashTests.m from [cd8e81d091] to [04c4da8f40].

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA1Hash *sha1;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash]))

	while (![f atEndOfStream]) {
		char buf[64];
		size_t len = [f readNBytes: 64
				intoBuffer: buf];
		[sha1 updateWithBuffer: buf
				ofSize: len];
	}
	[f close];







|







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFSHA1Hash *sha1;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash]))

	while (![f isAtEndOfStream]) {
		char buf[64];
		size_t len = [f readNBytes: 64
				intoBuffer: buf];
		[sha1 updateWithBuffer: buf
				ofSize: len];
	}
	[f close];

Modified tests/OFStreamTests.m from [837d422c65] to [6e872e9aa9].

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@interface StreamTester: OFStream
{
	int state;
}
@end

@implementation StreamTester
- (BOOL)_atEndOfStream
{
	return (state > 1 ? YES : NO);
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{







|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@interface StreamTester: OFStream
{
	int state;
}
@end

@implementation StreamTester
- (BOOL)_isAtEndOfStream
{
	return (state > 1 ? YES : NO);
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{