This source file includes following definitions.
- _zip_read
- _zip_read_data
- _zip_read_string
- _zip_write
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 #include <stdlib.h>
35 #include <string.h>
36
37 #include "zipint.h"
38
39 int
40 _zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error)
41 {
42 zip_int64_t n;
43
44 if (length > ZIP_INT64_MAX) {
45 zip_error_set(error, ZIP_ER_INTERNAL, 0);
46 return -1;
47 }
48
49 if ((n = zip_source_read(src, b, length)) < 0) {
50 _zip_error_set_from_source(error, src);
51 return -1;
52 }
53
54 if (n < (zip_int64_t)length) {
55 zip_error_set(error, ZIP_ER_EOF, 0);
56 return -1;
57 }
58
59 return 0;
60 }
61
62
63 zip_uint8_t *
64 _zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error)
65 {
66 zip_uint8_t *r;
67
68 if (length == 0 && !nulp) {
69 return NULL;
70 }
71
72 r = (zip_uint8_t *)malloc(length + (nulp ? 1 : 0));
73 if (!r) {
74 zip_error_set(error, ZIP_ER_MEMORY, 0);
75 return NULL;
76 }
77
78 if (buffer) {
79 zip_uint8_t *data = _zip_buffer_get(buffer, length);
80
81 if (data == NULL) {
82 zip_error_set(error, ZIP_ER_MEMORY, 0);
83 free(r);
84 return NULL;
85 }
86 memcpy(r, data, length);
87 }
88 else {
89 if (_zip_read(src, r, length, error) < 0) {
90 free(r);
91 return NULL;
92 }
93 }
94
95 if (nulp) {
96 zip_uint8_t *o;
97
98 r[length] = 0;
99 for (o=r; o<r+length; o++)
100 if (*o == '\0')
101 *o = ' ';
102 }
103
104 return r;
105 }
106
107
108 zip_string_t *
109 _zip_read_string(zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t len, bool nulp, zip_error_t *error)
110 {
111 zip_uint8_t *raw;
112 zip_string_t *s;
113
114 if ((raw=_zip_read_data(buffer, src, len, nulp, error)) == NULL)
115 return NULL;
116
117 s = _zip_string_new(raw, len, ZIP_FL_ENC_GUESS, error);
118 free(raw);
119 return s;
120 }
121
122
123 int
124 _zip_write(zip_t *za, const void *data, zip_uint64_t length)
125 {
126 zip_int64_t n;
127
128 if ((n = zip_source_write(za->src, data, length)) < 0) {
129 _zip_error_set_from_source(&za->error, za->src);
130 return -1;
131 }
132 if ((zip_uint64_t)n != length) {
133 zip_error_set(&za->error, ZIP_ER_WRITE, EINTR);
134 return -1;
135 }
136
137 return 0;
138 }