ObjFW  Check-in [38057e75f0]

Overview
Comment:Add OFDataArray (Hashing) category.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 38057e75f0888a0d6fc16371860320fd175b4c4cc893b91891c3f424c65036ac
User & Date: js on 2011-02-04 16:01:09
Other Links: manifest | tags
Context
2011-02-05
11:57
Fix a typo. check-in: 6fe8882d64 user: js tags: trunk
2011-02-04
16:01
Add OFDataArray (Hashing) category. check-in: 38057e75f0 user: js tags: trunk
15:36
Oops. Forgot to remove a fixme before committing. check-in: 4bace0f852 user: js tags: trunk
Changes

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

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
include ../extra.mk

LIB = ${OBJFW_SHARED_LIB}
LIB_MAJOR = ${OBJFW_LIB_MAJOR}
LIB_MINOR = ${OBJFW_LIB_MINOR}
STATIC_LIB = ${OBJFW_STATIC_LIB}

SRCS = OFApplication.m		\
       OFArray.m		\
       OFAutoreleasePool.m	\
       OFBlock.m		\
       OFConstantString.m	\
       OFDataArray.m		\

       OFDate.m			\
       OFDictionary.m		\
       OFExceptions.m		\
       OFFile.m			\
       OFHash.m			\
       OFEnumerator.m		\
       OFList.m			\













>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
include ../extra.mk

LIB = ${OBJFW_SHARED_LIB}
LIB_MAJOR = ${OBJFW_LIB_MAJOR}
LIB_MINOR = ${OBJFW_LIB_MINOR}
STATIC_LIB = ${OBJFW_STATIC_LIB}

SRCS = OFApplication.m		\
       OFArray.m		\
       OFAutoreleasePool.m	\
       OFBlock.m		\
       OFConstantString.m	\
       OFDataArray.m		\
       OFDataArray+Hashing.m	\
       OFDate.m			\
       OFDictionary.m		\
       OFExceptions.m		\
       OFFile.m			\
       OFHash.m			\
       OFEnumerator.m		\
       OFList.m			\

Added src/OFDataArray+Hashing.h version [de4d700a3e].























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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.
 */

#import "OFDataArray.h"

@class OFString;

#ifdef __cplusplus
extern "C" {
#endif
extern int _OFDataArray_Hashing_reference;
#ifdef __cplusplus
}
#endif

/**
 * The OFDataArray (Hashing) category provides methods to calculate hashes for
 * data arrays.
 */
@interface OFDataArray (Hashing)
/**
 * \return The MD5 hash of the data array as an autoreleased OFString
 */
- (OFString*)MD5Hash;

/**
 * \return The SHA1 hash of the data array as an autoreleased OFString
 */
- (OFString*)SHA1Hash;
@end

Added src/OFDataArray+Hashing.m version [499df88bcb].







































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
78
79
80
81
82
83
/*
 * 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 "OFDataArray.h"
#import "OFString.h"
#import "OFMD5Hash.h"
#import "OFSHA1Hash.h"
#import "OFAutoreleasePool.h"

int _OFDataArray_Hashing_reference;

@implementation OFDataArray (Hashing)
- (OFString*)MD5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char ret_c[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: data
			ofSize: count * itemSize];
	digest = [hash digest];

	for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: ret_c
				    length: 32];
}

- (OFString*)SHA1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char ret_c[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: data
			ofSize: count * itemSize];
	digest = [hash digest];

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: ret_c
				    length: 40];
}
@end

Modified src/OFDataArray.h from [2a4a8a3519] to [bd7e19c676].

172
173
174
175
176
177
178


 * memory in pages rather than a chunk of memory for each item.
 */
@interface OFBigDataArray: OFDataArray
{
	size_t size;
}
@end









>
>
172
173
174
175
176
177
178
179
180
 * memory in pages rather than a chunk of memory for each item.
 */
@interface OFBigDataArray: OFDataArray
{
	size_t size;
}
@end

#import "OFDataArray+Hashing.h"

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

21
22
23
24
25
26
27






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

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







@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
{
	return [[[self alloc] initWithItemSize: is] autorelease];
}








>
>
>
>
>
>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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;
};

@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
{
	return [[[self alloc] initWithItemSize: is] autorelease];
}

Modified src/OFString+Hashing.h from [47219e07e4] to [ba06c39430].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#endif
extern int _OFString_Hashing_reference;
#ifdef __cplusplus
}
#endif

/**
 * The OFString (OFHashing) category provides methods to calculate hashes for
 * strings.
 */
@interface OFString (Hashing)
/**
 * \return The MD5 hash of the string as an autoreleased OFString
 */
- (OFString*)MD5Hash;

/**
 * \return The SHA1 hash of the string as an autoreleased OFString
 */
- (OFString*)SHA1Hash;
@end







|













21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#endif
extern int _OFString_Hashing_reference;
#ifdef __cplusplus
}
#endif

/**
 * The OFString (Hashing) category provides methods to calculate hashes for
 * strings.
 */
@interface OFString (Hashing)
/**
 * \return The MD5 hash of the string as an autoreleased OFString
 */
- (OFString*)MD5Hash;

/**
 * \return The SHA1 hash of the string as an autoreleased OFString
 */
- (OFString*)SHA1Hash;
@end

Modified src/OFString+Hashing.m from [780c4c8311] to [856205c228].

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

#include <string.h>

#import "OFString.h"
#import "OFMD5Hash.h"
#import "OFSHA1Hash.h"
#import "OFAutoreleasePool.h"

int _OFString_Hashing_reference;

@implementation OFString (Hashing)
- (OFString*)MD5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char ret_c[32];
	size_t i;

	[hash updateWithBuffer: string
			ofSize: length];
	digest = [hash digest];

	for (i = 0; i < 16; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: ret_c
				    length: 32];
}

- (OFString*)SHA1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char ret_c[40];
	size_t i;

	[hash updateWithBuffer: string
			ofSize: length];
	digest = [hash digest];

	for (i = 0; i < 20; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');







<
<













|






|




















|






|







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
 * 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 "OFMD5Hash.h"
#import "OFSHA1Hash.h"
#import "OFAutoreleasePool.h"

int _OFString_Hashing_reference;

@implementation OFString (Hashing)
- (OFString*)MD5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char ret_c[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: string
			ofSize: length];
	digest = [hash digest];

	for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: ret_c
				    length: 32];
}

- (OFString*)SHA1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char ret_c[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: string
			ofSize: length];
	digest = [hash digest];

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		ret_c[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		ret_c[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');

Modified tests/OFDataArrayTests.m from [f79c61c7b6] to [9acf5c037e].

94
95
96
97
98
99
100




101
102
103
104
105
106
107

	TEST(@"-[addNItems:atIndex:]",
	    R([array[0] addNItems: 2
		       fromCArray: "bc"
			  atIndex: 1]) && [array[0] count] == 5 &&
	    !memcmp([array[0] cArray], "abcde", 5))





	TEST(@"Building strings",
	    (array[0] = [class dataArrayWithItemSize: 1]) &&
	    R([array[0] addNItems: 6
		       fromCArray: (void*)str]) && R([array[0] addItem: ""]) &&
	    !strcmp([array[0] cArray], str))

	EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]",







>
>
>
>







94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

	TEST(@"-[addNItems:atIndex:]",
	    R([array[0] addNItems: 2
		       fromCArray: "bc"
			  atIndex: 1]) && [array[0] count] == 5 &&
	    !memcmp([array[0] cArray], "abcde", 5))

	TEST(@"-[MD5Hash]", [[array[0] MD5Hash] isEqual: [@"abcde" MD5Hash]])

	TEST(@"-[SHA1Hash]", [[array[0] SHA1Hash] isEqual: [@"abcde" SHA1Hash]])

	TEST(@"Building strings",
	    (array[0] = [class dataArrayWithItemSize: 1]) &&
	    R([array[0] addNItems: 6
		       fromCArray: (void*)str]) && R([array[0] addItem: ""]) &&
	    !strcmp([array[0] cArray], str))

	EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]",