ObjFW  Diff

Differences From Artifact [27bb220d00]:

To Artifact [9c55df296d]:


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
	    @"Usage: %@ [md5|rmd160|sha1|sha224|sha256|sha384|sha512] "
	    @"file1 [file2 ...]\n",
	    [OFApplication programName]];

	[OFApplication terminateWithStatus: 1];
}

static Class
hashClassForName(OFString *name)
{
	if ([name isEqual: @"md5"])
		return [OFMD5Hash class];
	else if ([name isEqual: @"rmd160"] || [name isEqual: @"ripemd160"])
		return [OFRIPEMD160Hash class];
	else if ([name isEqual: @"sha1"])
		return [OFSHA1Hash class];
	else if ([name isEqual: @"sha224"])
		return [OFSHA224Hash class];
	else if ([name isEqual: @"sha256"])
		return [OFSHA256Hash class];
	else if ([name isEqual: @"sha384"])
		return [OFSHA384Hash class];
	else if ([name isEqual: @"sha512"])
		return [OFSHA512Hash class];

	return nil;
}

@implementation OFHash
- (void)applicationDidFinishLaunching
{
	OFArray *arguments = [OFApplication arguments];
	Class <OFHash> hashClass;
	OFEnumerator *enumerator;
	OFString *path;
	int exitStatus = 0;

	if ([arguments count] < 2)
		help();

	if ((hashClass = hashClassForName([arguments objectAtIndex: 0])) == Nil)
		help();

	enumerator = [[arguments objectsInRange:
	    of_range(1, [arguments count] - 1)] objectEnumerator];
	while ((path = [enumerator nextObject]) != nil) {
		void *pool = objc_autoreleasePoolPush();
		id <OFHash> hash = [hashClass hash];
		OFStream *file;
		const uint8_t *digest;
		size_t i, digestSize;

		if ([path isEqual: @"-"])
			file = of_stdin;
		else {
			@try {
				file = [OFFile fileWithPath: path
						       mode: @"rb"];
			} @catch (OFOpenFileFailedException *e) {
				[of_stderr writeFormat:
				    @"Failed to open file %@: %s\n",
				    [e path], strerror([e errNo])];

				exitStatus = 1;
				goto outer_loop_end;
			}
		}



		while (![file isAtEndOfStream]) {
			uint8_t buffer[1024];
			size_t length;

			@try {
				length = [file readIntoBuffer: buffer
						       length: 1024];
			} @catch (OFReadFailedException *e) {
				[of_stderr writeFormat:
				    @"Failed to read %@: %s\n",
				    path, strerror([e errNo])];

				exitStatus = 1;
				goto outer_loop_end;
			}

			[hash updateWithBuffer: buffer
					length: length];
		}

		[file close];

		digest = [hash digest];
		digestSize = [hashClass digestSize];

		for (i = 0; i < digestSize; i++)
			[of_stdout writeFormat: @"%02x", digest[i]];

		[of_stdout writeFormat: @"  %@\n", path];

outer_loop_end:
		objc_autoreleasePoolPop(pool);
	}

	[OFApplication terminateWithStatus: exitStatus];
}
@end







|
|


|

|

|

|

|

|

|








|







|






<



















>
>




















>



|
>












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
87
88
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
	    @"Usage: %@ [md5|rmd160|sha1|sha224|sha256|sha384|sha512] "
	    @"file1 [file2 ...]\n",
	    [OFApplication programName]];

	[OFApplication terminateWithStatus: 1];
}

static id <OFHash>
hashForName(OFString *name)
{
	if ([name isEqual: @"md5"])
		return [OFMD5Hash hash];
	else if ([name isEqual: @"rmd160"] || [name isEqual: @"ripemd160"])
		return [OFRIPEMD160Hash hash];
	else if ([name isEqual: @"sha1"])
		return [OFSHA1Hash hash];
	else if ([name isEqual: @"sha224"])
		return [OFSHA224Hash hash];
	else if ([name isEqual: @"sha256"])
		return [OFSHA256Hash hash];
	else if ([name isEqual: @"sha384"])
		return [OFSHA384Hash hash];
	else if ([name isEqual: @"sha512"])
		return [OFSHA512Hash hash];

	return nil;
}

@implementation OFHash
- (void)applicationDidFinishLaunching
{
	OFArray *arguments = [OFApplication arguments];
	id <OFHash> hash;
	OFEnumerator *enumerator;
	OFString *path;
	int exitStatus = 0;

	if ([arguments count] < 2)
		help();

	if ((hash = hashForName([arguments firstObject])) == nil)
		help();

	enumerator = [[arguments objectsInRange:
	    of_range(1, [arguments count] - 1)] objectEnumerator];
	while ((path = [enumerator nextObject]) != nil) {
		void *pool = objc_autoreleasePoolPush();

		OFStream *file;
		const uint8_t *digest;
		size_t i, digestSize;

		if ([path isEqual: @"-"])
			file = of_stdin;
		else {
			@try {
				file = [OFFile fileWithPath: path
						       mode: @"rb"];
			} @catch (OFOpenFileFailedException *e) {
				[of_stderr writeFormat:
				    @"Failed to open file %@: %s\n",
				    [e path], strerror([e errNo])];

				exitStatus = 1;
				goto outer_loop_end;
			}
		}

		[hash reset];

		while (![file isAtEndOfStream]) {
			uint8_t buffer[1024];
			size_t length;

			@try {
				length = [file readIntoBuffer: buffer
						       length: 1024];
			} @catch (OFReadFailedException *e) {
				[of_stderr writeFormat:
				    @"Failed to read %@: %s\n",
				    path, strerror([e errNo])];

				exitStatus = 1;
				goto outer_loop_end;
			}

			[hash updateWithBuffer: buffer
					length: length];
		}

		[file close];

		digest = [hash digest];
		digestSize = [[hash class] digestSize];

		for (i = 0; i < digestSize; i++)
			[of_stdout writeFormat: @"%02x", digest[i]];

		[of_stdout writeFormat: @"  %@\n", path];

outer_loop_end:
		objc_autoreleasePoolPop(pool);
	}

	[OFApplication terminateWithStatus: exitStatus];
}
@end