Skip to content

Commit 0064bcb

Browse files
committed
clang
1 parent 1d6bef0 commit 0064bcb

6 files changed

Lines changed: 209 additions & 208 deletions

File tree

src/hashing.c

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
#include "hashing.h"
22

3-
int calculate_file_hash(const char* filename, unsigned char hash[SHA256_DIGEST_LENGTH]) {
4-
FILE* file = fopen(filename, "rb");
5-
if (file == NULL) {
6-
perror("Error opening file");
7-
return -1;
8-
}
9-
10-
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
11-
if (mdctx == NULL) {
12-
perror("Error creating context");
13-
fclose(file);
14-
return -1;
15-
}
3+
int calculate_file_hash(const char* filename,
4+
unsigned char hash[SHA256_DIGEST_LENGTH]) {
5+
FILE* file = fopen(filename, "rb");
6+
if (file == NULL) {
7+
perror("Error opening file");
8+
return -1;
9+
}
1610

17-
if (EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1) {
18-
perror("Error initializing digest");
19-
EVP_MD_CTX_free(mdctx);
20-
fclose(file);
21-
return -1;
22-
}
11+
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
12+
if (mdctx == NULL) {
13+
perror("Error creating context");
14+
fclose(file);
15+
return -1;
16+
}
2317

24-
unsigned char buffer[2 << 13];
25-
size_t bytesRead;
26-
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) != 0) {
27-
if (EVP_DigestUpdate(mdctx, buffer, bytesRead) != 1) {
28-
perror("Error updating digest");
29-
EVP_MD_CTX_free(mdctx);
30-
fclose(file);
31-
return -1;
32-
}
33-
}
18+
if (EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1) {
19+
perror("Error initializing digest");
20+
EVP_MD_CTX_free(mdctx);
21+
fclose(file);
22+
return -1;
23+
}
3424

35-
unsigned int digestLength;
36-
if (EVP_DigestFinal_ex(mdctx, hash, &digestLength) != 1) {
37-
perror("Error finalizing digest");
38-
EVP_MD_CTX_free(mdctx);
39-
fclose(file);
40-
return -1;
25+
unsigned char buffer[2 << 13];
26+
size_t bytesRead;
27+
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) != 0) {
28+
if (EVP_DigestUpdate(mdctx, buffer, bytesRead) != 1) {
29+
perror("Error updating digest");
30+
EVP_MD_CTX_free(mdctx);
31+
fclose(file);
32+
return -1;
4133
}
34+
}
4235

36+
unsigned int digestLength;
37+
if (EVP_DigestFinal_ex(mdctx, hash, &digestLength) != 1) {
38+
perror("Error finalizing digest");
4339
EVP_MD_CTX_free(mdctx);
4440
fclose(file);
41+
return -1;
42+
}
43+
44+
EVP_MD_CTX_free(mdctx);
45+
fclose(file);
4546

46-
return 0;
47+
return 0;
4748
}

src/hashing.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef SRC_HASHING_H_
22
#define SRC_HASHING_H_
33

4-
#include <stdio.h>
5-
#include <string.h>
64
#include <openssl/evp.h>
75
#include <openssl/sha.h>
6+
#include <stdio.h>
7+
#include <string.h>
88

9-
int calculate_file_hash(const char* filename, unsigned char hash[SHA256_DIGEST_LENGTH]);
10-
9+
int calculate_file_hash(const char* filename,
10+
unsigned char hash[SHA256_DIGEST_LENGTH]);
1111

12-
#endif // SRC_HASHING_H_
12+
#endif // SRC_HASHING_H_

src/unzip.c

Lines changed: 106 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "unzip.h"
2+
23
#include "hashing.h"
34

45
static void printInfo();
@@ -17,10 +18,9 @@ int main(int argc, char** argv) {
1718
retCode = (int)ERROR;
1819
}
1920
} else {
20-
retCode = (int)ERROR;
21+
retCode = (int)ERROR;
2122
}
2223

23-
2424
if (config.hFlag) {
2525
retCode = ERROR;
2626
printInfo();
@@ -37,7 +37,6 @@ int main(int argc, char** argv) {
3737
}
3838
} else if (retCode == ERROR) {
3939
fprintf(stderr, USAGE);
40-
4140
}
4241

4342
return retCode;
@@ -50,136 +49,139 @@ int scanOptions(int argc, char** argv, OptionsConfig* config) {
5049
while (((flag = getopt(argc, argv, "hf"))) != -1) {
5150
switch (flag) {
5251
case 'h':
53-
config->hFlag = 1;
54-
break;
52+
config->hFlag = 1;
53+
break;
5554
case 'f':
56-
config->fFlag = 1;
57-
break;
55+
config->fFlag = 1;
56+
break;
5857
default:
59-
result = (int)ERROR;
58+
result = (int)ERROR;
6059
}
6160

6261
if (result < 0) break;
6362
}
64-
63+
6564
if (result >= 0) {
6665
result = optind;
6766
}
6867

6968
return result;
7069
}
7170

72-
7371
// decompression func
74-
int unzip(const char* archive_filename, const char* filename, OptionsConfig config) {
75-
// calc archive hash at start
76-
unsigned char hash_start[SHA256_DIGEST_LENGTH];
77-
if (calculate_file_hash(archive_filename, hash_start) == -1) {
78-
perror("Error while hashing files");
79-
return EXIT_FAILURE;
80-
}
72+
int unzip(const char* archive_filename, const char* filename,
73+
OptionsConfig config) {
74+
// calc archive hash at start
75+
unsigned char hash_start[SHA256_DIGEST_LENGTH];
76+
if (calculate_file_hash(archive_filename, hash_start) == -1) {
77+
perror("Error while hashing files");
78+
return EXIT_FAILURE;
79+
}
8180

82-
// open archive file for reading
83-
int archive_fd = open(archive_filename, O_RDONLY);
84-
if (archive_fd == -1) {
85-
perror("Error opening input file");
86-
return EXIT_FAILURE;
87-
}
81+
// open archive file for reading
82+
int archive_fd = open(archive_filename, O_RDONLY);
83+
if (archive_fd == -1) {
84+
perror("Error opening input file");
85+
return EXIT_FAILURE;
86+
}
8887

89-
// getting input file stats
90-
struct stat st;
91-
if (fstat(archive_fd, &st) == -1) {
92-
perror("Error getting file size");
93-
close(archive_fd);
94-
return EXIT_FAILURE;
95-
}
88+
// getting input file stats
89+
struct stat st;
90+
if (fstat(archive_fd, &st) == -1) {
91+
perror("Error getting file size");
92+
close(archive_fd);
93+
return EXIT_FAILURE;
94+
}
9695

97-
// open output file for writing
98-
int fileOverwrite = (config.fFlag) ? (O_CREAT | O_WRONLY | O_TRUNC) : (O_CREAT | O_WRONLY | O_TRUNC | O_EXCL);
99-
int output_fd = open(filename, fileOverwrite, 0666);
100-
if (output_fd == -1) {
101-
perror("Error opening output file");
102-
close(archive_fd);
103-
return EXIT_FAILURE;
104-
}
96+
// open output file for writing
97+
int fileOverwrite = (config.fFlag) ? (O_CREAT | O_WRONLY | O_TRUNC)
98+
: (O_CREAT | O_WRONLY | O_TRUNC | O_EXCL);
99+
int output_fd = open(filename, fileOverwrite, 0666);
100+
if (output_fd == -1) {
101+
perror("Error opening output file");
102+
close(archive_fd);
103+
return EXIT_FAILURE;
104+
}
105105

106-
// getting archive file size
107-
off_t input_size = st.st_size;
108-
109-
// mem mapping of archive file
110-
void* archive_data = mmap(NULL, input_size, PROT_READ, MAP_PRIVATE, archive_fd, 0);
111-
if (archive_data == MAP_FAILED) {
112-
perror("Error mapping input file to memory");
113-
close(archive_fd);
114-
close(output_fd);
115-
return EXIT_FAILURE;
116-
}
106+
// getting archive file size
107+
off_t input_size = st.st_size;
117108

118-
// struct init for decompression
119-
z_stream stream;
120-
memset(&stream, 0, sizeof(stream));
109+
// mem mapping of archive file
110+
void* archive_data =
111+
mmap(NULL, input_size, PROT_READ, MAP_PRIVATE, archive_fd, 0);
112+
if (archive_data == MAP_FAILED) {
113+
perror("Error mapping input file to memory");
114+
close(archive_fd);
115+
close(output_fd);
116+
return EXIT_FAILURE;
117+
}
121118

122-
if (inflateInit(&stream) != Z_OK) {
123-
perror("Error initializing decompression stream");
124-
munmap(archive_data, input_size);
125-
close(archive_fd);
126-
close(output_fd);
127-
return EXIT_FAILURE;
128-
}
119+
// struct init for decompression
120+
z_stream stream;
121+
memset(&stream, 0, sizeof(stream));
129122

130-
// translating archive data ptr and its size to struct
131-
stream.avail_in = input_size;
132-
stream.next_in = (Bytef*)archive_data;
133-
134-
unsigned char out[DEFAULT_CHUNK_SIZE];
135-
int ret;
136-
do {
137-
stream.avail_out = DEFAULT_CHUNK_SIZE;
138-
stream.next_out = out;
139-
// archive decompression and writing to output file
140-
ret = inflate(&stream, Z_NO_FLUSH);
141-
if (ret == Z_STREAM_ERROR) {
142-
perror("Error decompressing data");
143-
inflateEnd(&stream);
144-
munmap(archive_data, input_size);
145-
close(archive_fd);
146-
close(output_fd);
147-
return EXIT_FAILURE;
148-
}
149-
write(output_fd, out, DEFAULT_CHUNK_SIZE - stream.avail_out);
150-
} while (ret != Z_STREAM_END);
151-
152-
// calc file hash at end
153-
unsigned char hash_end[SHA256_DIGEST_LENGTH];
154-
if (calculate_file_hash(archive_filename, hash_end) == -1) {
155-
perror("Error while hashing files");
156-
munmap(archive_data, input_size);
157-
close(archive_fd);
158-
close(output_fd);
159-
return EXIT_FAILURE;
160-
}
123+
if (inflateInit(&stream) != Z_OK) {
124+
perror("Error initializing decompression stream");
125+
munmap(archive_data, input_size);
126+
close(archive_fd);
127+
close(output_fd);
128+
return EXIT_FAILURE;
129+
}
161130

162-
// checking hashes (archive at start <-> archive at end)
163-
if ((memcmp(hash_end, hash_start, SHA256_DIGEST_LENGTH) != 0)) {
164-
fprintf(stderr, "Error: File content was modified during copying\n");
165-
munmap(archive_data, input_size);
166-
close(archive_fd);
167-
close(output_fd);
168-
return EXIT_FAILURE;
131+
// translating archive data ptr and its size to struct
132+
stream.avail_in = input_size;
133+
stream.next_in = (Bytef*)archive_data;
134+
135+
unsigned char out[DEFAULT_CHUNK_SIZE];
136+
int ret;
137+
do {
138+
stream.avail_out = DEFAULT_CHUNK_SIZE;
139+
stream.next_out = out;
140+
// archive decompression and writing to output file
141+
ret = inflate(&stream, Z_NO_FLUSH);
142+
if (ret == Z_STREAM_ERROR) {
143+
perror("Error decompressing data");
144+
inflateEnd(&stream);
145+
munmap(archive_data, input_size);
146+
close(archive_fd);
147+
close(output_fd);
148+
return EXIT_FAILURE;
169149
}
150+
write(output_fd, out, DEFAULT_CHUNK_SIZE - stream.avail_out);
151+
} while (ret != Z_STREAM_END);
152+
153+
// calc file hash at end
154+
unsigned char hash_end[SHA256_DIGEST_LENGTH];
155+
if (calculate_file_hash(archive_filename, hash_end) == -1) {
156+
perror("Error while hashing files");
157+
munmap(archive_data, input_size);
158+
close(archive_fd);
159+
close(output_fd);
160+
return EXIT_FAILURE;
161+
}
170162

171-
// free
172-
inflateEnd(&stream);
163+
// checking hashes (archive at start <-> archive at end)
164+
if ((memcmp(hash_end, hash_start, SHA256_DIGEST_LENGTH) != 0)) {
165+
fprintf(stderr, "Error: File content was modified during copying\n");
173166
munmap(archive_data, input_size);
174167
close(archive_fd);
175168
close(output_fd);
176-
return 0;
169+
return EXIT_FAILURE;
170+
}
171+
172+
// free
173+
inflateEnd(&stream);
174+
munmap(archive_data, input_size);
175+
close(archive_fd);
176+
close(output_fd);
177+
return 0;
177178
}
178179

179180
// -------------------------- static funcs --------------------------
180181
void printInfo() {
181-
printf("Usage: Usage: unzip source_archive.zip [options] [output]\n"\
182-
"Options:\n"\
183-
" -f\n"\
184-
" Force overwriting of destonation file, if it's existing.");
182+
printf(
183+
"Usage: Usage: unzip source_archive.zip [options] [output]\n"
184+
"Options:\n"
185+
" -f\n"
186+
" Force overwriting of destonation file, if it's existing.");
185187
}

0 commit comments

Comments
 (0)