11#include "unzip.h"
2+
23#include "hashing.h"
34
45static 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 --------------------------
180181void 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