ObjFW  Check-in [1d7ed6556d]

Overview
Comment:Use a union instead of casting pointers in OF{MD5,SHA1}Hash.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1d7ed6556d4896d24e894289a50af266b9d820fb6a6c1b6725a7098dba0f8409
User & Date: js on 2011-03-11 16:44:50
Other Links: manifest | tags
Context
2011-03-11
16:47
Get rid of warnings when using latest clang. check-in: 36c4b260aa user: js tags: trunk
16:44
Use a union instead of casting pointers in OF{MD5,SHA1}Hash. check-in: 1d7ed6556d user: js tags: trunk
13:16
Don't test OFHTTPRequest if we have no threads. check-in: 80b8456b4b user: js tags: trunk
Changes

Modified src/OFMD5Hash.h from [e0170a3623] to [bbea038319].

21
22
23
24
25
26
27

28



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

29
30
31
32
33
34
35
36
37
38







+
-
+
+
+







/**
 * \brief A class which provides functions to create an MD5 hash.
 */
@interface OFMD5Hash: OFHash
{
	uint32_t buf[4];
	uint32_t bits[2];
	union {
	uint8_t	 in[64];
		uint8_t	u8[64];
		uint32_t u32[16];
	} in;
}

/**
 * \return A new autoreleased MD5 Hash
 */
+ MD5Hash;
@end

Modified src/OFMD5Hash.m from [f05769188c] to [239cec3ca7].

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







-
+









-
-
+
+







-
-
-
+
+
+






-
+

















-
+









-
-
+
+


-
+




-
+


-
-
+
+

-
+







	bits[1] += size >> 29;

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

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

		t = 64 - t;

		if (size < t) {
			memcpy(p, buffer, size);
			return;
		}

		memcpy(p, buffer, t);
		of_bswap32_vec_if_be((uint32_t*)in, 16);
		md5_transform(buf, (uint32_t*)in);
		of_bswap32_vec_if_be(in.u32, 16);
		md5_transform(buf, in.u32);

		buffer += t;
		size -= t;
	}

	/* Process data in 64-byte chunks */
	while (size >= 64) {
		memcpy(in, buffer, 64);
		of_bswap32_vec_if_be((uint32_t*)in, 16);
		md5_transform(buf, (uint32_t*)in);
		memcpy(in.u8, buffer, 64);
		of_bswap32_vec_if_be(in.u32, 16);
		md5_transform(buf, in.u32);

		buffer += 64;
		size -= 64;
	}

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

- (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
	 * always at least one byte free
	 */
	p = in + count;
	p = in.u8 + count;
	*p++ = 0x80;

	/* Bytes of padding needed to make 64 bytes */
	count = 64 - 1 - count;

	/* Pad out to 56 mod 64 */
	if (count < 8) {
		/* Two lots of padding: Pad the first block to 64 bytes */
		memset(p, 0, count);
		of_bswap32_vec_if_be((uint32_t*)in, 16);
		md5_transform(buf, (uint32_t*)in);
		of_bswap32_vec_if_be(in.u32, 16);
		md5_transform(buf, in.u32);

		/* Now fill the next block with 56 bytes */
		memset(in, 0, 56);
		memset(in.u8, 0, 56);
	} else {
		/* Pad block to 56 bytes */
		memset(p, 0, count - 8);
	}
	of_bswap32_vec_if_be((uint32_t*)in, 14);
	of_bswap32_vec_if_be(in.u32, 14);

	/* Append length in bits and transform */
	((uint32_t*)in)[14] = bits[0];
	((uint32_t*)in)[15] = bits[1];
	in.u32[14] = bits[0];
	in.u32[15] = bits[1];

	md5_transform(buf, (uint32_t*)in);
	md5_transform(buf, in.u32);
	of_bswap32_vec_if_be(buf, 4);

	isCalculated = YES;

	return (uint8_t*)buf;
}
@end

Modified src/OFSHA1Hash.m from [ba33b42a53] to [76bf5a4c3b].

21
22
23
24
25
26
27
28
29


30
31

32
33
34
35
36



37
38
39
40
41
42
43
21
22
23
24
25
26
27


28
29
30

31
32
33



34
35
36
37
38
39
40
41
42
43







-
-
+
+

-
+


-
-
-
+
+
+







#import "OFSHA1Hash.h"
#import "OFExceptions.h"
#import "macros.h"

/* blk0() and blk() perform the initial expand. */
#ifndef OF_BIG_ENDIAN
#define blk0(i)							\
	(block->l[i] = (OF_ROL(block->l[i], 24) & 0xFF00FF00) |	\
	    (OF_ROL(block->l[i], 8) & 0x00FF00FF))
	(block.l[i] = (OF_ROL(block.l[i], 24) & 0xFF00FF00) |	\
	    (OF_ROL(block.l[i], 8) & 0x00FF00FF))
#else
#define blk0(i) block->l[i]
#define blk0(i) block.l[i]
#endif
#define blk(i) \
	(block->l[i & 15] = OF_ROL(block->l[(i + 13) & 15] ^	\
	    block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^	\
	    block->l[i & 15], 1))
	(block.l[i & 15] = OF_ROL(block.l[(i + 13) & 15] ^	\
	    block.l[(i + 8) & 15] ^ block.l[(i + 2) & 15] ^	\
	    block.l[i & 15], 1))

/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
#define R0(v, w, x, y, z, i)						\
	z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + OF_ROL(v, 5);	\
	w = OF_ROL(w, 30);
#define R1(v, w, x, y, z, i)						\
	z += ((w & (x ^ y)) ^ y) + blk(i) +  0x5A827999 + OF_ROL(v, 5);	\
57
58
59
60
61
62
63
64
65

66
67
68

69
70
71
72
73
74
75
57
58
59
60
61
62
63


64
65


66
67
68
69
70
71
72
73







-
-
+

-
-
+







	uint32_t l[16];
} sha1_c64l16_t;

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

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

	/* Copy state[] to working vars */
	a = state[0];
	b = state[1];
	c = state[2];
	d = state[3];
	e = state[4];