ObjFW  Diff

Differences From Artifact [aa03d9ef76]:

To Artifact [976db374f8]:


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
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
149
150
151
152
153

154
155
156

157
158
159

160
161
162

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

151
152
153

154
155


156
157


158
159


160
161


162
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







-
+








-
+




-
+


-
+

-
+








-
+











-
+


















-
+







-
+



















-
+
-

-
+
-

-
+














-
+


-
+

-
-
+

-
-
+

-
-
+

-
-
+

-
-
+






-
+


-
+
-





-
+







@end

OF_APPLICATION_DELEGATE(OFHash)

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

	[OFApplication terminateWithStatus: 1];
}

static void
printHash(OFString *algo, OFString *path, id <OFCryptoHash> hash)
printHash(OFString *algo, OFString *path, id <OFCryptographicHash> hash)
{
	const unsigned char *digest = hash.digest;
	size_t digestSize = hash.digestSize;

	[of_stdout writeFormat: @"%@ ", algo];
	[OFStdOut writeFormat: @"%@ ", algo];

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

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

@implementation OFHash
- (void)applicationDidFinishLaunching
{
	int exitStatus = 0;
	bool calculateMD5, calculateRIPEMD160, calculateSHA1, calculateSHA224;
	bool calculateSHA256, calculateSHA384, calculateSHA512;
	const of_options_parser_option_t options[] = {
	const OFOptionsParserOption options[] = {
		{ '\0', @"md5", 0, &calculateMD5, NULL },
		{ '\0', @"ripemd160", 0, &calculateRIPEMD160, NULL },
		{ '\0', @"sha1", 0, &calculateSHA1, NULL },
		{ '\0', @"sha224", 0, &calculateSHA224, NULL },
		{ '\0', @"sha256", 0, &calculateSHA256, NULL },
		{ '\0', @"sha384", 0, &calculateSHA384, NULL },
		{ '\0', @"sha512", 0, &calculateSHA512, NULL },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: options];
	of_unichar_t option;
	OFUnichar option;
	OFMD5Hash *MD5Hash = nil;
	OFRIPEMD160Hash *RIPEMD160Hash = nil;
	OFSHA1Hash *SHA1Hash = nil;
	OFSHA224Hash *SHA224Hash = nil;
	OFSHA256Hash *SHA256Hash = nil;
	OFSHA384Hash *SHA384Hash = nil;
	OFSHA512Hash *SHA512Hash = nil;

#ifndef OF_AMIGAOS
	[OFLocale addLanguageDirectory: @LANGUAGE_DIR];
#else
	[OFLocale addLanguageDirectory: @"PROGDIR:/share/ofhash/lang"];
#endif

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case '?':
			if (optionsParser.lastLongOption != nil)
				[of_stderr writeLine:
				[OFStdErr writeLine:
				    OF_LOCALIZED(@"unknown_long_option",
				    @"%[prog]: Unknown option: --%[opt]",
				    @"prog", [OFApplication programName],
				    @"opt", optionsParser.lastLongOption)];
			else {
				OFString *optStr = [OFString stringWithFormat:
				    @"%c", optionsParser.lastOption];
				[of_stderr writeLine:
				[OFStdErr writeLine:
				    OF_LOCALIZED(@"unknown_option",
				    @"%[prog]: Unknown option: -%[opt]",
				    @"prog", [OFApplication programName],
				    @"opt", optStr)];
			}

			[OFApplication terminateWithStatus: 1];
			break;
		}
	}

#ifdef OF_HAVE_SANDBOX
	OFSandbox *sandbox = [OFSandbox sandbox];
	@try {
		sandbox.allowsStdIO = true;
		sandbox.allowsReadingFiles = true;
		sandbox.allowsUserDatabaseReading = true;

		for (OFString *path in optionsParser.remainingArguments)
			[sandbox unveilPath: path
			[sandbox unveilPath: path permissions: @"r"];
				permissions: @"r"];

		[sandbox unveilPath: @LANGUAGE_DIR
		[sandbox unveilPath: @LANGUAGE_DIR permissions: @"r"];
			permissions: @"r"];

		[OFApplication activateSandbox: sandbox];
		[OFApplication of_activateSandbox: sandbox];
	} @finally {
		[sandbox release];
	}
#endif

	if (!calculateMD5 && !calculateRIPEMD160 && !calculateSHA1 &&
	    !calculateSHA224 && !calculateSHA256 && !calculateSHA384 &&
	    !calculateSHA512)
		help();

	if (optionsParser.remainingArguments.count < 1)
		help();

	if (calculateMD5)
		MD5Hash = [OFMD5Hash cryptoHashWithAllowsSwappableMemory: true];
		MD5Hash = [OFMD5Hash hashWithAllowsSwappableMemory: true];
	if (calculateRIPEMD160)
		RIPEMD160Hash =
		    [OFRIPEMD160Hash cryptoHashWithAllowsSwappableMemory: true];
		    [OFRIPEMD160Hash hashWithAllowsSwappableMemory: true];
	if (calculateSHA1)
		SHA1Hash =
		    [OFSHA1Hash cryptoHashWithAllowsSwappableMemory: true];
		SHA1Hash = [OFSHA1Hash hashWithAllowsSwappableMemory: true];
	if (calculateSHA224)
		SHA224Hash =
		    [OFSHA224Hash cryptoHashWithAllowsSwappableMemory: true];
		SHA224Hash = [OFSHA224Hash hashWithAllowsSwappableMemory: true];
	if (calculateSHA256)
		SHA256Hash =
		    [OFSHA256Hash cryptoHashWithAllowsSwappableMemory: true];
		SHA256Hash = [OFSHA256Hash hashWithAllowsSwappableMemory: true];
	if (calculateSHA384)
		SHA384Hash =
		    [OFSHA384Hash cryptoHashWithAllowsSwappableMemory: true];
		SHA384Hash = [OFSHA384Hash hashWithAllowsSwappableMemory: true];
	if (calculateSHA512)
		SHA512Hash =
		    [OFSHA512Hash cryptoHashWithAllowsSwappableMemory: true];
		SHA512Hash = [OFSHA512Hash hashWithAllowsSwappableMemory: true];

	for (OFString *path in optionsParser.remainingArguments) {
		void *pool = objc_autoreleasePoolPush();
		OFStream *file;

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

				[of_stderr writeLine: OF_LOCALIZED(
				[OFStdErr writeLine: OF_LOCALIZED(
				    @"failed_to_open_file",
				    @"Failed to open file %[file]: %[error]",
				    @"file", e.path,
				    @"error", error)];

				exitStatus = 1;
				goto outer_loop_end;
212
213
214
215
216
217
218
219

220
221
222
223
224
225
226
204
205
206
207
208
209
210

211
212
213
214
215
216
217
218







-
+







				length = [file readIntoBuffer: buffer
						       length: 1024];
			} @catch (OFReadFailedException *e) {
				OFString *error = [OFString
				    stringWithCString: strerror(e.errNo)
					     encoding: [OFLocale encoding]];

				[of_stderr writeLine: OF_LOCALIZED(
				[OFStdErr writeLine: OF_LOCALIZED(
				    @"failed_to_read_file",
				    @"Failed to read %[file]: %[error]",
				    @"file", path,
				    @"error", error)];

				exitStatus = 1;
				goto outer_loop_end;