ObjFW  Check-in [722b6213c0]

Overview
Comment:Use char* instead of uint8_t* for OFStreams and OFHashes.
This way, less casts are needed when using C libraries.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 722b6213c04a219cd6ed3ca37579e16b82d75bf14f087416ce86bd3ba7684053
User & Date: js on 2009-04-09 13:55:55
Other Links: manifest | tags
Context
2009-04-10
00:52
Fix warnings on Leopard in OFConstString.
As there are no warnings anymore, reintroduce -Werror.
check-in: 72af773f72 user: js tags: trunk
2009-04-09
13:55
Use char* instead of uint8_t* for OFStreams and OFHashes.
This way, less casts are needed when using C libraries.
check-in: 722b6213c0 user: js tags: trunk
2009-04-08
17:11
Ouch. Really. Fixed recursion loop in OFExceptions. check-in: 0c8a28c5ac user: js tags: trunk
Changes

Modified src/OFFile.h from [8001780b38] to [938ce6b8f3].

121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136
137
138
139
140

141
121
122
123
124
125
126
127

128
129
130
131
132
133
134
135
136
137
138
139

140
141







-
+











-
+

 *	  The buffer MUST be at least size * nitems big!
 * \param nitems nitem The number of items to read
 *	  The buffer MUST be at least size * nitems big!
 * \return The number of bytes read
 */
- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (uint8_t*)buf;
	  intoBuffer: (char*)buf;

/**
 * Writes from a buffer into the file.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \param nitem The number of items to write
 * \return The number of bytes written
 */
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf;
	   fromBuffer: (const char*)buf;
@end

Modified src/OFFile.m from [2951d584dc] to [08cbbc44cd].

104
105
106
107
108
109
110
111

112
113
114
115
116
117
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
104
105
106
107
108
109
110

111
112
113
114
115
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
156
157

158
159
160
161
162
163
164
165
166
167
168







-
+












-
+








-
+













-
+










-
+










- (BOOL)atEndOfFile
{
	return (feof(fp) == 0 ? NO : YES);
}

- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (uint8_t*)buf
	  intoBuffer: (char*)buf
{
	size_t ret;

	if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp))
		@throw [OFReadFailedException newWithClass: [self class]
						   andSize: size
						 andNItems: nitems];

	return ret;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf
	  intoBuffer: (char*)buf
{
	return [self readNItems: size
			 ofSize: 1
		     intoBuffer: buf];
}

- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf
	   fromBuffer: (const char*)buf
{
	size_t ret;

	if ((ret = fwrite(buf, size, nitems, fp)) == 0 &&
	    size != 0 && nitems != 0)
		@throw [OFWriteFailedException newWithClass: [self class]
						    andSize: size
						  andNItems: nitems];

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
	   fromBuffer: (const char*)buf
{
	return [self writeNItems: size
			  ofSize: 1
		      fromBuffer: buf];
}

- (size_t)writeCString: (const char*)str
{
	return [self writeNItems: strlen(str)
			  ofSize: 1
		      fromBuffer: (const uint8_t*)str];
		      fromBuffer: str];
}

- close
{
	fclose(fp);
	fp = NULL;

	return self;
}
@end

Modified src/OFHashes.h from [d9a99aea79] to [6473ae53f6].

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
78

79
80
81
82
83
84
85

86
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

78
79
80
81
82
83
84

85
86







-
+

















-
+






-
+









-
-
+
+

















-
+






-
+

/**
 * The OFMD5Hash class provides functions to create an MD5 hash.
 */
@interface OFMD5Hash: OFObject
{
	uint32_t buf[4];
	uint32_t bits[2];
	uint8_t	 in[64];
	char	 in[64];

	BOOL	 calculated;
}

/**
 * \return A new autoreleased MD5 Hash
 */
+ md5Hash;

- init;

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

/**
 * \return A buffer containing the hash (MD5_DIGEST_SIZE = 16 bytes).
 *	   The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;
- (char*)digest;
@end

/**
 * The OFSHA1Hash class provides functions to create an SHA1 hash.
 */
@interface OFSHA1Hash: OFObject
{
	uint32_t    state[5];
	uint64_t    count;
	uint8_t	    buffer[64];
	uint8_t	    digest[SHA1_DIGEST_SIZE];
	char	    buffer[64];
	char	    digest[SHA1_DIGEST_SIZE];

	BOOL	 calculated;
}

/**
 * \return A new autoreleased SHA1 Hash
 */
+ sha1Hash;

- init;

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

/**
 * \return A buffer containing the hash (SHA1_DIGEST_SIZE = 20 bytes).
 *	   The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;
- (char*)digest;
@end

Modified src/OFHashes.m from [78e42ea05b] to [fb812c0e73].

133
134
135
136
137
138
139
140

141
142
143
144
145
146
147
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147







-
+








		calculated = NO;
	}

	return self;
}

- updateWithBuffer: (const uint8_t*)buffer
- updateWithBuffer: (const char*)buffer
	    ofSize: (size_t)size
{
	uint32_t t;

	if (calculated)
		return self;
	if (size == 0)
155
156
157
158
159
160
161
162

163
164
165
166
167
168
169
155
156
157
158
159
160
161

162
163
164
165
166
167
168
169







-
+







	bits[1] += size >> 29;

	/* Bytes already in shsInfo->data */
	t = (t >> 3) & 0x3F;

	/* Handle any leading odd-sized chunks */
	if (t) {
		uint8_t *p = (uint8_t*)in + t;
		char *p = in + t;

		t = 64 - t;

		if (size < t) {
			memcpy(p, buffer, size);
			return self;
		}
188
189
190
191
192
193
194
195

196
197

198
199
200
201

202
203
204
205
206
207
208
188
189
190
191
192
193
194

195
196

197
198
199
200

201
202
203
204
205
206
207
208







-
+

-
+



-
+








	/* Handle any remaining bytes of data. */
	memcpy(in, buffer, size);

	return self;
}

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

	if (calculated)
		return (uint8_t*)buf;
		return (char*)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
	 * always at least one byte free
229
230
231
232
233
234
235
236

237
238
239
240

241
242
243
244
245
246
247
229
230
231
232
233
234
235

236
237
238
239

240
241
242
243
244
245
246
247







-
+



-
+







	OF_BSWAP_V(in, 14);

	/* 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_BSWAP_V((uint8_t*)buf, 4);
	OF_BSWAP_V((char*)buf, 4);

	calculated = YES;

	return (uint8_t*)buf;
	return (char*)buf;
}
@end

#undef F1
#undef F2
#undef F3
#undef F4
278
279
280
281
282
283
284
285

286
287
288
289
290

291
292
293

294
295
296
297
298
299
300
278
279
280
281
282
283
284

285
286
287
288
289

290
291
292

293
294
295
296
297
298
299
300







-
+




-
+


-
+







	z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + OF_ROL(v, 5); \
	w = OF_ROL(w, 30);
#define R4(v, w, x, y, z, i)						\
	z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + OF_ROL(v, 5);		\
	w = OF_ROL(w, 30);

typedef union {
	uint8_t	 c[64];
	char	 c[64];
	uint32_t l[16];
} sha1_c64l16_t;

static inline void
sha1_transform(uint32_t state[5], const uint8_t buffer[64])
sha1_transform(uint32_t state[5], const char buffer[64])
{
	uint32_t      a, b, c, d, e;
	uint8_t	      workspace[64];
	char	      workspace[64];
	sha1_c64l16_t *block;

	block = (sha1_c64l16_t*)workspace;
	memcpy(block, buffer, 64);

	/* Copy state[] to working vars */
	a = state[0];
330
331
332
333
334
335
336
337
338


339
340
341
342
343
344
345
330
331
332
333
334
335
336


337
338
339
340
341
342
343
344
345







-
-
+
+







	state[1] += b;
	state[2] += c;
	state[3] += d;
	state[4] += e;
}

static inline void
sha1_update(uint32_t *state, uint64_t *count, uint8_t *buffer,
    const uint8_t *buf, size_t size)
sha1_update(uint32_t *state, uint64_t *count, char *buffer,
    const char *buf, size_t size)
{
	size_t i, j;

	j = (size_t)((*count >> 3) & 63);
	*count += (size << 3);

	if ((j + size) > 63) {
373
374
375
376
377
378
379
380

381
382
383
384
385
386
387
388
389
390
391
392
393

394
395
396

397
398
399
400
401
402
403
404


405
406
407

408
409
410
411
412

413
414
415
416
417
418
419
420
421
422
423
424
425
373
374
375
376
377
378
379

380
381
382
383
384
385
386
387
388
389
390
391
392

393
394
395

396
397
398
399
400
401
402


403
404
405
406

407
408
409
410
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425







-
+












-
+


-
+






-
-
+
+


-
+




-
+













		state[3] = 0x10325476;
		state[4] = 0xC3D2E1F0;
	}

	return self;
}

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

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

	return self;
}

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

	if (calculated)
		return digest;

	for (i = 0; i < 8; i++)
		/* Endian independent */
		finalcount[i] = (uint8_t)((count >> ((7 - (i & 7)) * 8)) & 255);
	sha1_update(state, &count, buffer, (const uint8_t*)"\200", 1);
		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, (const uint8_t*)"\0", 1);
		sha1_update(state, &count, buffer, "\0", 1);
	/* Should cause a sha1_transform() */
	sha1_update(state, &count, buffer, finalcount, 8);

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

	return digest;
}
@end

#undef blk0
#undef blk
#undef R0
#undef R1
#undef R2
#undef R3
#undef R4

Modified src/OFStream.h from [ed38ea4d16] to [7f1379bb1f].

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
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







-
+









-
+







 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf;
	  intoBuffer: (char*)buf;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf;
	   fromBuffer: (const char*)buf;

/**
 * Writes a C string into the stream, without the trailing zero.
 *
 * \param str The C string from which the data is written to the stream
 * \return The number of bytes written
 */

Modified src/OFTCPSocket.m from [86db944fc0] to [b0a40b2d84].

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
248
249
250
251
252
253

254
255
256
257
258
259
260
261
262
263
264
265
266
267

268
269
270
271
272
273
274
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
248
249
250
251
252

253
254
255
256
257
258
259
260
261
262
263
264
265
266

267
268
269
270
271
272
273
274







-
+






-
+












-
+






-
+













-
+







	[newsock setSocketAddress: addr
		       withLength: addrlen];

	return newsock;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf
	  intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	switch ((ret = recv(sock, (char*)buf, size, 0))) {
	switch ((ret = recv(sock, buf, size, 0))) {
	case 0:
		@throw [OFNotConnectedException newWithClass: [self class]];
	case -1:
		@throw [OFReadFailedException newWithClass: [self class]
						   andSize: size];
	}

	/* This is safe, as we already checked < 1 */
	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
	   fromBuffer: (const char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	if ((ret = send(sock, (char*)buf, size, 0)) == -1)
	if ((ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: [self class]
						    andSize: size];

	/* This is safe, as we already checked for -1 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	return [self writeNBytes: strlen(str)
		      fromBuffer: (const uint8_t*)str];
		      fromBuffer: str];
}

- setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

Modified tests/OFHashes/OFHashes.m from [c778845c1b] to [4b23857f2b].

22
23
24
25
26
27
28
29
30


31
32
33
34
35
36
37
22
23
24
25
26
27
28


29
30
31
32
33
34
35
36
37







-
-
+
+







const uint8_t testfile_sha1[SHA1_DIGEST_SIZE] =
    "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5"
    "\x94\xE7\x17";

int
main()
{
	uint8_t buf[64];
	size_t	len;
	char   buf[64];
	size_t len;

	OFMD5Hash  *md5  = [OFMD5Hash md5Hash];
	OFSHA1Hash *sha1 = [OFSHA1Hash sha1Hash];
	OFFile *f = [OFFile fileWithPath: "testfile"
				 andMode: "rb"];

	while (![f atEndOfFile]) {

Modified tests/OFTCPSocket/OFTCPSocket.m from [ba29c3b0ba] to [7883bf415d].

54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
54
55
56
57
58
59
60

61
62
63
64
65
66
67
68







-
+







		[client connectTo: "localhost"
			   onPort: port];

		accepted = [server accept];

		[client writeCString: "Hallo!"];
		[accepted readNBytes: 6
			  intoBuffer: (uint8_t*)buf];
			  intoBuffer: buf];
		buf[6] = 0;

		if (!strcmp(buf, "Hallo!"))
			puts("Received correct string!");
		else {
			puts("Received INCORRECT string!");
			return 1;
86
87
88
89
90
91
92
93

94
95
96
97
98
99
100
86
87
88
89
90
91
92

93
94
95
96
97
98
99
100







-
+







		[client connectTo: "::1"
			   onPort: port];

		accepted = [server accept];

		[client writeCString: "IPv6:)"];
		[accepted readNBytes: 6
			  intoBuffer: (uint8_t*)buf];
			  intoBuffer: buf];
		buf[6] = 0;

		if (!strcmp(buf, "IPv6:)"))
			puts("Received correct string!");
		else {
			puts("Received INCORRECT string!");
			return 1;