ObjFW  Diff

Differences From Artifact [aa7d967739]:

To Artifact [3f3d3563e3]:


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







+












-
-
-
-
+
+
+
+







#import "OFRIPEMD160Hash.h"
#import "OFSHA1Hash.h"
#import "OFSHA224Hash.h"
#import "OFSHA256Hash.h"
#import "OFSHA384Hash.h"
#import "OFSHA512Hash.h"
#import "OFStdIOStream.h"
#import "OFLocalization.h"

#import "OFOpenItemFailedException.h"
#import "OFReadFailedException.h"

@interface OFHash: OFObject
@end

OF_APPLICATION_DELEGATE(OFHash)

static void
help(void)
{
	[of_stderr writeFormat:
	    @"Usage: %@ [md5|rmd160|sha1|sha224|sha256|sha384|sha512] "
	    @"file1 [file2 ...]\n",
	    [OFApplication programName]];
	[of_stderr writeLine: OF_LOCALIZED(@"usage",
	    @"Usage: %[prog] [md5|rmd160|sha1|sha224|sha256|sha384|sha512] "
	    @"file1 [file2 ...]",
	    @"prog", [OFApplication programName])];

	[OFApplication terminateWithStatus: 1];
}

static id <OFCryptoHash>
hashForName(OFString *name)
{
72
73
74
75
76
77
78


79
80
81
82
83
84
85
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88







+
+







- (void)applicationDidFinishLaunching
{
	OFArray OF_GENERIC(OFString*) *arguments = [OFApplication arguments];
	id <OFCryptoHash> hash;
	bool first = true;
	int exitStatus = 0;

	[OFLocalization addLanguageDirectory: @LANGUAGE_DIR];

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

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

	for (OFString *path in arguments) {
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
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
149
150







+
+
+
+
+
-
-
-
+
+
+
+
+
















+
+
+
+
+
-
-
-
+
+
+
+
+







		if ([path isEqual: @"-"])
			file = of_stdin;
		else {
			@try {
				file = [OFFile fileWithPath: path
						       mode: @"rb"];
			} @catch (OFOpenItemFailedException *e) {
				OFString *error = [OFString
				    stringWithCString: strerror([e errNo])
					     encoding: [OFLocalization
							   encoding]];

				[of_stderr writeFormat:
				    @"Failed to open file %@: %s\n",
				    [e path], strerror([e errNo])];
				[of_stderr writeLine: OF_LOCALIZED(
				    @"failed_to_open_file",
				    @"Failed to open file %[file]: %[error]",
				    @"file", [e path],
				    @"error", error)];

				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) {
				OFString *error = [OFString
				    stringWithCString: strerror([e errNo])
					     encoding: [OFLocalization
							   encoding]];

				[of_stderr writeFormat:
				    @"Failed to read %@: %s\n",
				    path, strerror([e errNo])];
				[of_stderr writeLine: OF_LOCALIZED(
				    @"failed_to_read_file",
				    @"Failed to read %[file]: %[error]",
				    @"file", path,
				    @"error", error)];

				exitStatus = 1;
				goto outer_loop_end;
			}

			[hash updateWithBuffer: buffer
					length: length];