1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/encode/zipstreams.h"
#include "gate/results.h"
#include "gate/debugging.h"
#include "gate/files.h"
#if defined(GATE_EXTLIB_MINIZIP)
# define GATE_ENCODE_ZIPSTREAM_IMPL 1
#else
# define GATE_ECNODE_ZIPSTREAM_NO_IMPL 1
#endif
void gate_zipentry_init(gate_zipentry_t* entry, gate_string_t const* path, gate_uint64_t size,
gate_enumint_t attribs, gate_enumint_t accessbits, gate_string_t const* comment)
{
gate_mem_clear(entry, sizeof(gate_zipentry_t));
if (path)
{
gate_string_to_buffer(path, entry->path, sizeof(entry->path));
}
if (comment)
{
gate_string_to_buffer(comment, entry->comment, sizeof(entry->comment));
}
entry->size = size;
entry->attribs = attribs;
entry->access = accessbits;
}
struct gate_zip_attrib_mapping
{
gate_enumint_t gate_attr;
gate_uint32_t zip_attr;
};
static struct gate_zip_attrib_mapping const zip_attribs[] =
{
{ GATE_FILEENTRY_ATTRIB_READONLY, 0x00000001 },
{ GATE_FILEENTRY_ATTRIB_HIDDEN, 0x00000002 },
{ GATE_FILEENTRY_ATTRIB_SYSTEM, 0x00000004 },
{ GATE_FILEENTRY_ATTRIB_DIRECTORY, 0x40000010 },
{ GATE_FILEENTRY_ATTRIB_ARCHIVE, 0x00000020 }
};
static gate_size_t const zip_attribs_count = sizeof(zip_attribs) / sizeof(zip_attribs[0]);
static struct gate_zip_attrib_mapping const zip_accessbits[] =
{
{ GATE_FILEENTRY_ACCESS_OWNERREAD, 0x01000000 },
{ GATE_FILEENTRY_ACCESS_OWNERWRITE, 0x00800000 },
{ GATE_FILEENTRY_ACCESS_OWNEREXECUTE, 0x00400000 },
{ GATE_FILEENTRY_ACCESS_OWNERSETID, 0x08000000 },
{ GATE_FILEENTRY_ACCESS_GROUPREAD, 0x00200000 },
{ GATE_FILEENTRY_ACCESS_GROUPWRITE, 0x00100000 },
{ GATE_FILEENTRY_ACCESS_GROUPEXECUTE, 0x00080000 },
{ GATE_FILEENTRY_ACCESS_GROUPSETID, 0x04000000 },
{ GATE_FILEENTRY_ACCESS_ALLREAD, 0x00040000 },
{ GATE_FILEENTRY_ACCESS_ALLWRITE, 0x00020000 },
{ GATE_FILEENTRY_ACCESS_ALLEXECUTE, 0x00010000 },
{ GATE_FILEENTRY_ACCESS_NODELETE, 0x02000000 },
};
static gate_size_t const zip_accessbits_count = sizeof(zip_accessbits) / sizeof(zip_accessbits[0]);
#define GATE_ZIP_FILETYPE_FILE 0x08
#define GATE_ZIP_FILETYPE_DIR 0x04
#define GATE_ZIP_FILETYPE_LINK 0x0a
static gate_uint32_t gate_attribs_to_zip_attribs(gate_enumint_t gateattrs)
{
gate_uint32_t zip_attrs = 0;
gate_size_t n;
for (n = 0; n != zip_attribs_count; ++n)
{
GATE_FLAG_SET(zip_attrs, zip_attribs[n].zip_attr, GATE_FLAG_ENABLED(gateattrs, zip_attribs[n].gate_attr));
}
if (GATE_FLAG_ENABLED(gateattrs, GATE_FILEENTRY_ATTRIB_DIRECTORY))
{
zip_attrs |= (((gate_uint32_t)GATE_ZIP_FILETYPE_DIR) << 28);
}
if (GATE_FLAG_ENABLED(gateattrs, GATE_FILEENTRY_ATTRIB_LINK))
{
zip_attrs |= (((gate_uint32_t)GATE_ZIP_FILETYPE_DIR) << 28);
}
return zip_attrs;
}
static gate_uint32_t gate_accessbits_to_zip_attribs(gate_enumint_t gateaccessbits)
{
gate_uint32_t zip_attrs = 0;
gate_size_t n;
for (n = 0; n != zip_accessbits_count; ++n)
{
GATE_FLAG_SET(zip_attrs, zip_accessbits[n].zip_attr, GATE_FLAG_ENABLED(gateaccessbits, zip_accessbits[n].gate_attr));
}
return zip_attrs;
}
static gate_enumint_t zip_attribs_to_gate_attribs(gate_uint32_t attrs)
{
gate_enumint_t gate_attrs = 0;
gate_size_t n;
for (n = 0; n != zip_attribs_count; ++n)
{
GATE_FLAG_SET(gate_attrs, zip_attribs[n].gate_attr, GATE_FLAG_ENABLED(attrs, zip_attribs[n].zip_attr));
}
attrs >>= 28;
if (attrs == GATE_ZIP_FILETYPE_DIR)
{
gate_attrs |= GATE_FILEENTRY_ATTRIB_DIRECTORY;
}
if (attrs == GATE_ZIP_FILETYPE_LINK)
{
gate_attrs |= GATE_FILEENTRY_ATTRIB_LINK;
}
return gate_attrs;
}
static gate_enumint_t zip_attribs_to_gate_accessbits(gate_uint32_t attrs)
{
gate_enumint_t gate_access = 0;
gate_size_t n;
for (n = 0; n != zip_accessbits_count; ++n)
{
GATE_FLAG_SET(gate_access, zip_accessbits[n].gate_attr, GATE_FLAG_ENABLED(attrs, zip_accessbits[n].zip_attr));
}
return gate_access;
}
#if defined(GATE_ENCODE_ZIPSTREAM_IMPL)
#if defined(GATE_EXTLIB_BZIP2) && !defined(HAVE_BZIP2)
# define HAVE_BZIP2
#endif
#include "zlib.h"
#include "zip.h"
#include "unzip.h"
static voidpf ZCALLBACK gate_zipwriter_open_file_func(voidpf opaque, const char* filename, int mode)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
GATE_UNUSED_ARG(filename);
GATE_UNUSED_ARG(mode);
return target->stream;
}
static uLong ZCALLBACK gate_zipwriter_read_file_func(voidpf opaque, voidpf stream, void* buf, uLong size)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
gate_size_t returned = 0;
GATE_DEBUG_ASSERT(target->stream == (gate_stream_t*)stream);
GATE_UNUSED_ARG(stream);
target->last_result = gate_stream_read_block(target->stream, (char*)buf, (gate_size_t)size, &returned);
if (GATE_SUCCEEDED(target->last_result))
{
target->bytes_received += returned;
return (uLong)returned;
}
else
{
return 0;
}
}
static uLong ZCALLBACK gate_zipwriter_write_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
gate_size_t written = 0;
GATE_DEBUG_ASSERT(target->stream == (gate_stream_t*)stream);
GATE_UNUSED_ARG(stream);
target->last_result = gate_stream_write_block(target->stream, (char const*)buf, (gate_size_t)size, &written);
if (GATE_SUCCEEDED(target->last_result))
{
target->bytes_written += written;
return (uLong)written;
}
else
{
return 0;
}
}
static int ZCALLBACK gate_zipwriter_close_file_func(voidpf opaque, voidpf stream)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
GATE_DEBUG_ASSERT(target->stream == (gate_stream_t*)stream);
GATE_UNUSED_ARG(stream);
if (target->control_stream)
{
target->last_result = gate_stream_close(target->control_stream, GATE_STREAM_CLOSE_DEFAULT);
}
return 0;
}
static int ZCALLBACK gate_zipwriter_testerror_file_func(voidpf opaque, voidpf stream)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
GATE_DEBUG_ASSERT(target->stream == (gate_stream_t*)stream);
if (GATE_FAILED(target->last_result))
{
return 1;
}
return 0;
}
static long ZCALLBACK gate_zipwriter_tell_file_func(voidpf opaque, voidpf stream)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
gate_int64_t position = 0;
gate_result_t res;
GATE_DEBUG_ASSERT(target->stream == (gate_stream_t*)stream);
GATE_UNUSED_ARG(stream);
if (target->control_stream)
{
res = gate_stream_get_position(target->control_stream, &position);
if (GATE_SUCCEEDED(res))
{
return (long)position;
}
}
else
{
return (long)(target->bytes_received + target->bytes_written);
}
return -1;
}
static long ZCALLBACK gate_zipwriter_seek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin)
{
gate_ziptarget_t* target = (gate_ziptarget_t*)opaque;
gate_result_t res = 0;
gate_int64_t stream_pos = (gate_int64_t)offset;
gate_int64_t new_pos = 0;
gate_uint32_t stream_origin;
GATE_DEBUG_ASSERT(target->stream == (gate_stream_t*)stream);
GATE_UNUSED_ARG(stream);
if (target->control_stream)
{
switch (origin)
{
case ZLIB_FILEFUNC_SEEK_CUR: stream_origin = GATE_STREAM_SEEK_CURRENT; break;
case ZLIB_FILEFUNC_SEEK_END: stream_origin = GATE_STREAM_SEEK_END; break;
case ZLIB_FILEFUNC_SEEK_SET: stream_origin = GATE_STREAM_SEEK_BEGIN; break;
default: return -1;
}
res = gate_stream_seek(target->control_stream, stream_pos, stream_origin, &new_pos);
if (GATE_SUCCEEDED(res))
{
return 0;
}
}
else
{
switch (origin)
{
case ZLIB_FILEFUNC_SEEK_CUR:
{
if (offset == 0)<--- Assuming condition 'offset==0' is false
{
return 0; /* no change is supported */
}
return -1; /* any other change is not supported */
}
case ZLIB_FILEFUNC_SEEK_END:
{
return -1; /* moving to end is not supported */
}
case ZLIB_FILEFUNC_SEEK_SET:
{
if (offset == 0)<--- Condition 'offset==0' is always false
{
return ((target->bytes_received == 0) && (target->bytes_written == 0)) ? 0 : -1;
}
return ((target->bytes_received == offset) || (target->bytes_written == offset)) ? 0 : -1;
}
default:
{
return -1;
}
}
}
return -1;
}
gate_result_t gate_zipwriter_create(gate_zipwriter_t* writer, gate_stream_t* stream)
{
gate_result_t ret = GATE_RESULT_FAILED;
zlib_filefunc_def* file_functions;<--- The scope of the variable 'file_functions' can be reduced. [+]The scope of the variable 'file_functions' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
if (!writer || !stream)
{
ret = GATE_RESULT_NULLPOINTER;
break;
}
gate_mem_clear(writer, sizeof(gate_zipwriter_t*));
writer->zipstream.last_result = GATE_RESULT_OK;
writer->zipstream.bytes_written = 0;
writer->zipstream.bytes_received = 0;
writer->zipstream.stream = stream;
writer->zipstream.control_stream = NULL;
if (gate_object_implements_interface((gate_object_t*)stream, GATE_INTERFACE_NAME_CONTROLSTREAM))
{
writer->zipstream.control_stream = (gate_controlstream_t*)stream;
}
file_functions = (zlib_filefunc_def*)&writer->native_params;
file_functions->zopen_file = &gate_zipwriter_open_file_func;
file_functions->zread_file = &gate_zipwriter_read_file_func;
file_functions->zwrite_file = &gate_zipwriter_write_file_func;
file_functions->ztell_file = &gate_zipwriter_tell_file_func;
file_functions->zseek_file = &gate_zipwriter_seek_file_func;
file_functions->zclose_file = &gate_zipwriter_close_file_func;
file_functions->zerror_file = &gate_zipwriter_testerror_file_func;
file_functions->opaque = &writer->zipstream;
writer->native_handle = (void*)zipOpen2((char const*)&writer->zipstream, APPEND_STATUS_CREATE, NULL, file_functions);
if (NULL == writer->native_handle)
{
gate_mem_clear(writer, sizeof(gate_zipwriter_t*));
ret = GATE_RESULT_FAILED;
break;
}
gate_object_retain(writer->zipstream.stream);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_zipwriter_add(gate_zipwriter_t* writer, gate_zipentry_t const* entry, gate_stream_t* stream)
{
gate_result_t ret = GATE_RESULT_FAILED;
char buffer[GATE_MAX_COPYBUFFER_LENGTH];
gate_size_t buffer_used;
gate_size_t comment_length = 0;
zip_fileinfo zip_info = GATE_INIT_EMPTY;
gate_datetime_t dt;
int result;<--- The scope of the variable 'result' can be reduced. [+]The scope of the variable 'result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
#if defined(GATE_SYS_DOS)
int window_bits = (MAX_WBITS - 1);
int compression_level = (Z_BEST_COMPRESSION + Z_BEST_SPEED) / 2;
int memlevel = (DEF_MEM_LEVEL - 1);
#else
int window_bits = MAX_WBITS;
int compression_level = Z_BEST_COMPRESSION;
int memlevel = DEF_MEM_LEVEL;
#endif
do
{
comment_length = gate_str_length(entry->comment);
if (entry->modified_date.timestamp)
{
ret = gate_time_to_datetime(&entry->modified_date, &dt);
GATE_BREAK_IF_FAILED(ret);
zip_info.tmz_date.tm_year = dt.date.year;
zip_info.tmz_date.tm_mon = dt.date.month - 1;
zip_info.tmz_date.tm_mday = dt.date.day;
zip_info.tmz_date.tm_hour = dt.time.hour;
zip_info.tmz_date.tm_min = dt.time.minute;
zip_info.tmz_date.tm_sec = dt.time.second;
}
zip_info.external_fa = gate_accessbits_to_zip_attribs(entry->access);
zip_info.external_fa |= gate_attribs_to_zip_attribs(entry->attribs);
/*
result = zipOpenNewFileInZip64((zipFile)writer->native_handle, entry->path, &zip_info, 0, 0, 0, 0,
((comment_length == 0) ? NULL : entry->comment),
Z_DEFLATED, Z_BEST_COMPRESSION,
(entry->size >= 0xffffffff) ? 1 : 0);
*/
result = zipOpenNewFileInZip4_64((zipFile)writer->native_handle, entry->path, &zip_info, 0, 0, 0, 0,
((comment_length == 0) ? NULL : entry->comment),
Z_DEFLATED, compression_level, 0,
-window_bits, memlevel, Z_DEFAULT_STRATEGY,
NULL, 0, 0, 0, (entry->size >= 0xffffffff) ? 1 : 0);
if (ZIP_OK != result)
{
ret = GATE_RESULT_FAILED;
break;
}
do
{
ret = gate_stream_read_block(stream, buffer, sizeof(buffer), &buffer_used);
if (GATE_SUCCEEDED(ret))
{
if (buffer_used == 0)
{
/* end of stream reached */
break;
}
result = zipWriteInFileInZip((zipFile)writer->native_handle, buffer, (unsigned int)buffer_used);
if (ZIP_OK != result)
{
ret = GATE_RESULT_INVALIDOUTPUT;
break;
}
}
} while (GATE_SUCCEEDED(ret));
zipCloseFileInZip((zipFile)writer->native_handle);
} while (0);
return ret;
}
gate_result_t gate_zipwriter_add_data(gate_zipwriter_t* writer, gate_zipentry_t const* entry, void const* data)
{
gate_memstream_impl_t impl = GATE_INIT_EMPTY;
gate_size_t datalen = (gate_size_t)entry->size;
gate_memstream_t* stream = gate_memstream_create_static_unmanaged_readonly(&impl, data, datalen, datalen);
return gate_zipwriter_add(writer, entry, (gate_stream_t*)stream);
}
gate_result_t gate_zipwriter_flush(gate_zipwriter_t* writer)
{
return gate_stream_flush(writer->zipstream.stream);
}
gate_result_t gate_zipwriter_destroy(gate_zipwriter_t* writer)
{
gate_result_t ret = GATE_RESULT_OK;
if (writer->native_handle)
{
zipClose(writer->native_handle, NULL);
}
if (writer->zipstream.stream)
{
gate_object_release(writer->zipstream.stream);
}
gate_mem_clear(writer, sizeof(gate_zipwriter_t));
return ret;
}
/* zip reader */
gate_result_t gate_zipreader_create(gate_zipreader_t* reader, gate_stream_t* stream)
{
gate_result_t ret = GATE_RESULT_FAILED;
zlib_filefunc_def* file_functions;<--- The scope of the variable 'file_functions' can be reduced. [+]The scope of the variable 'file_functions' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
if (!reader || !stream)
{
ret = GATE_RESULT_NULLPOINTER;
break;
}
gate_mem_clear(reader, sizeof(gate_zipreader_t));
reader->zipstream.stream = stream;
reader->zipstream.bytes_written = 0;
reader->zipstream.bytes_received = 0;
reader->zipstream.last_result = GATE_RESULT_OK;
reader->zipstream.stream = stream;
reader->zipstream.control_stream = NULL;
if (gate_object_implements_interface((gate_object_t*)stream, GATE_INTERFACE_NAME_CONTROLSTREAM))
{
reader->zipstream.control_stream = (gate_controlstream_t*)stream;
}
file_functions = (zlib_filefunc_def*)&reader->native_params;
file_functions->zopen_file = &gate_zipwriter_open_file_func;
file_functions->zread_file = &gate_zipwriter_read_file_func;
file_functions->zwrite_file = &gate_zipwriter_write_file_func;
file_functions->ztell_file = &gate_zipwriter_tell_file_func;
file_functions->zseek_file = &gate_zipwriter_seek_file_func;
file_functions->zclose_file = &gate_zipwriter_close_file_func;
file_functions->zerror_file = &gate_zipwriter_testerror_file_func;
file_functions->opaque = &reader->zipstream;
reader->native_handle = (void*)unzOpen2((char const*)&reader->zipstream, file_functions);
if (NULL == reader->native_handle)
{
gate_mem_clear(reader, sizeof(gate_zipwriter_t*));
ret = GATE_RESULT_FAILED;
break;
}
gate_object_retain(reader->zipstream.stream);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static gate_result_t gate_zipreader_build_entry(unzFile unz_handle, gate_zipentry_t* ptr_entry)
{
unz_file_info64 info = GATE_INIT_EMPTY;
gate_datetime_t dtm;
gate_time_t now = GATE_INIT_EMPTY;
gate_size_t pathlen;
int result;
gate_mem_clear(ptr_entry, sizeof(gate_zipentry_t));
result = unzGetCurrentFileInfo64(unz_handle, &info,
ptr_entry->path, sizeof(ptr_entry->path),
NULL, 0,
ptr_entry->comment, sizeof(ptr_entry->comment));
if (UNZ_OK != result)
{
GATE_DEBUG_TRACE("unzGetCurrentFileInfo64() failed");
return GATE_RESULT_FAILED;
}
gate_time_now(&now);
ptr_entry->crc = info.crc;
ptr_entry->version = info.version;
ptr_entry->flag = info.flag;
ptr_entry->compression_method = info.compression_method;
ptr_entry->attribs = zip_attribs_to_gate_attribs(info.external_fa);
ptr_entry->access = zip_attribs_to_gate_accessbits(info.external_fa);
if (!GATE_FLAG_ENABLED(ptr_entry->attribs, GATE_FILEENTRY_ATTRIB_FILE)
&& !GATE_FLAG_ENABLED(ptr_entry->attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY)
&& !GATE_FLAG_ENABLED(ptr_entry->attribs, GATE_FILEENTRY_ATTRIB_LINK))
{
pathlen = gate_str_length_max(ptr_entry->path, sizeof(ptr_entry->path));
if (pathlen > 0)
{
if (ptr_entry->path[pathlen - 1] == '/')
{
GATE_FLAG_SET(ptr_entry->attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY, true);
}
else
{
GATE_FLAG_SET(ptr_entry->attribs, GATE_FILEENTRY_ATTRIB_FILE, true);
}
}
}
ptr_entry->size = info.uncompressed_size;
ptr_entry->compressed_size = info.compressed_size;
ptr_entry->modified_date.bias = 0;
dtm.date.year = (gate_uint16_t)info.tmu_date.tm_year;
dtm.date.month = (gate_uint8_t)(info.tmu_date.tm_mon + 1);
dtm.date.day = (gate_uint8_t)info.tmu_date.tm_mday;
dtm.time.hour = (gate_uint8_t)info.tmu_date.tm_hour;
dtm.time.minute = (gate_uint8_t)info.tmu_date.tm_min;
dtm.time.second = (gate_uint8_t)info.tmu_date.tm_sec;
dtm.time.microsecond = 0;
gate_date_to_time(&dtm, &ptr_entry->modified_date);
//ptr_entry->modified_date.bias = now.bias;
return GATE_RESULT_OK;
}
gate_result_t gate_zipreader_get_first_entry(gate_zipreader_t* reader, gate_zipentry_t* ptr_entry)
{
unzFile unz_handle = (unzFile)reader->native_handle;
int result = unzGoToFirstFile(unz_handle);
if (UNZ_OK != result)
{
GATE_DEBUG_TRACE("unzGoToFirstFile() failed");
return GATE_RESULT_FAILED;
}
reader->eof_reached = false;
return gate_zipreader_build_entry(unz_handle, ptr_entry);
}
gate_result_t gate_zipreader_get_next_entry(gate_zipreader_t* reader, gate_zipentry_t* ptr_entry)
{
unzFile unz_handle = (unzFile)reader->native_handle;
int result = unzGoToNextFile(unz_handle);
if (UNZ_END_OF_LIST_OF_FILE == result)
{
GATE_DEBUG_TRACE("unzGoToNextFile() == UNZ_END_OF_LIST_OF_FILE");
return GATE_RESULT_ENDOFSTREAM;
}
if (UNZ_OK != result)
{
GATE_DEBUG_TRACE("unzGoToNextFile() failed");
return GATE_RESULT_FAILED;
}
return gate_zipreader_build_entry(unz_handle, ptr_entry);
}
gate_result_t gate_zipreader_find_entry(gate_zipreader_t* reader, gate_string_t const* path, gate_zipentry_t* ptr_entry)
{
unzFile unz_handle = (unzFile)reader->native_handle;
int result;
char path_buffer[GATE_MAX_FILEPATH_LENGTH];
gate_string_to_buffer(path, path_buffer, sizeof(path_buffer));
result = unzLocateFile(unz_handle, path_buffer, 0);
if (UNZ_OK != result)
{
return GATE_RESULT_NOMATCH;
}
return gate_zipreader_build_entry(unz_handle, ptr_entry);
}
gate_result_t gate_zipreader_extract_content(gate_zipreader_t* reader, gate_stream_t* output_target)
{
gate_result_t ret = GATE_RESULT_FAILED;
char buffer[8192];
unzFile unz_handle = (unzFile)reader->native_handle;
gate_size_t written;
int result;<--- The scope of the variable 'result' can be reduced. [+]The scope of the variable 'result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
result = unzOpenCurrentFile(unz_handle);
if (UNZ_OK != result)
{
ret = GATE_RESULT_FAILED;
break;
}
ret = GATE_RESULT_OK;
for (;;)
{
result = unzReadCurrentFile(unz_handle, buffer, sizeof(buffer));
if (result == 0)
{
break;
}
if (result < 0)
{
GATE_DEBUG_TRACE("unzReadCurrentFile() failed");
ret = GATE_RESULT_FAILED;
break;
}
ret = gate_stream_write_block(output_target, buffer, (gate_size_t)result, &written);
GATE_BREAK_IF_FAILED(ret);
}
unzCloseCurrentFile(unz_handle);
} while (0);
return ret;
}
gate_result_t gate_zipreader_destroy(gate_zipreader_t* reader)
{
if (reader->native_handle)
{
unzClose(reader->native_handle);
}
if (reader->zipstream.stream)
{
gate_object_release(reader->zipstream.stream);
}
return GATE_RESULT_OK;
}
#endif /* GATE_ENCODE_ZIPSTREAM_IMPL */
#if defined(GATE_ECNODE_ZIPSTREAM_NO_IMPL)
gate_result_t gate_zipwriter_create(gate_zipwriter_t* writer, gate_stream_t* stream)
{
(void)writer;
(void)stream;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipwriter_add(gate_zipwriter_t* writer, gate_zipentry_t const* entry, gate_stream_t* stream)
{
(void)writer;
(void)entry;
(void)stream;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipwriter_add_data(gate_zipwriter_t* writer, gate_zipentry_t const* entry, void const* data)
{
(void)writer;
(void)entry;
(void)data;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipwriter_flush(gate_zipwriter_t* writer)
{
(void)writer;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipwriter_destroy(gate_zipwriter_t* writer)
{
(void)writer;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipreader_create(gate_zipreader_t* reader, gate_stream_t* stream)
{
(void)reader;
(void)stream;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipreader_get_first_entry(gate_zipreader_t* reader, gate_zipentry_t* ptr_entry)
{
(void)reader;
(void)ptr_entry;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipreader_get_next_entry(gate_zipreader_t* reader, gate_zipentry_t* ptr_entry)
{
(void)reader;
(void)ptr_entry;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipreader_find_entry(gate_zipreader_t* reader, gate_string_t const* path, gate_zipentry_t* ptr_entry)
{
(void)reader;
(void)path;
(void)ptr_entry;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipreader_extract_content(gate_zipreader_t* reader, gate_stream_t* output_target)
{
(void)reader;
(void)output_target;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_zipreader_destroy(gate_zipreader_t* reader)
{
(void)reader;
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif /* GATE_ECNODE_ZIPSTREAM_NO_IMPL */
|