This source file includes following definitions.
- _zip_string_crc32
- _zip_string_equal
- _zip_string_free
- _zip_string_get
- _zip_string_length
- _zip_string_new
- _zip_string_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
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "zipint.h"
40
41
42
43 zip_uint32_t
44 _zip_string_crc32(const struct zip_string *s)
45 {
46 zip_uint32_t crc;
47
48 crc = (zip_uint32_t)crc32(0L, Z_NULL, 0);
49
50 if (s != NULL)
51 crc = (zip_uint32_t)crc32(crc, s->raw, s->length);
52
53 return crc;
54 }
55
56
57
58 int
59 _zip_string_equal(const struct zip_string *a, const struct zip_string *b)
60 {
61 if (a == NULL || b == NULL)
62 return a == b;
63
64 if (a->length != b->length)
65 return 0;
66
67
68
69 return (memcmp(a->raw, b->raw, a->length) == 0);
70 }
71
72
73
74 void
75 _zip_string_free(struct zip_string *s)
76 {
77 if (s == NULL)
78 return;
79
80 free(s->raw);
81 free(s->converted);
82 free(s);
83 }
84
85
86
87 const zip_uint8_t *
88 _zip_string_get(struct zip_string *string, zip_uint32_t *lenp, zip_flags_t flags, struct zip_error *error)
89 {
90 static const zip_uint8_t empty[1] = "";
91
92 if (string == NULL) {
93 if (lenp)
94 *lenp = 0;
95 return empty;
96 }
97
98 if ((flags & ZIP_FL_ENC_RAW) == 0) {
99
100 if (string->encoding == ZIP_ENCODING_UNKNOWN)
101 _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN);
102
103 if (((flags & ZIP_FL_ENC_STRICT)
104 && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN)
105 || (string->encoding == ZIP_ENCODING_CP437)) {
106 if (string->converted == NULL) {
107 if ((string->converted=_zip_cp437_to_utf8(string->raw, string->length,
108 &string->converted_length, error)) == NULL)
109 return NULL;
110 }
111 if (lenp)
112 *lenp = string->converted_length;
113 return string->converted;
114 }
115 }
116
117 if (lenp)
118 *lenp = string->length;
119 return string->raw;
120 }
121
122
123
124 zip_uint16_t
125 _zip_string_length(const struct zip_string *s)
126 {
127 if (s == NULL)
128 return 0;
129
130 return s->length;
131 }
132
133
134
135 struct zip_string *
136 _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, struct zip_error *error)
137 {
138 struct zip_string *s;
139 enum zip_encoding_type expected_encoding;
140
141 if (length == 0)
142 return NULL;
143
144 switch (flags & ZIP_FL_ENCODING_ALL) {
145 case ZIP_FL_ENC_GUESS:
146 expected_encoding = ZIP_ENCODING_UNKNOWN;
147 break;
148 case ZIP_FL_ENC_UTF_8:
149 expected_encoding = ZIP_ENCODING_UTF8_KNOWN;
150 break;
151 case ZIP_FL_ENC_CP437:
152 expected_encoding = ZIP_ENCODING_CP437;
153 break;
154 default:
155 _zip_error_set(error, ZIP_ER_INVAL, 0);
156 return NULL;
157 }
158
159 if ((s=(struct zip_string *)malloc(sizeof(*s))) == NULL) {
160 _zip_error_set(error, ZIP_ER_MEMORY, 0);
161 return NULL;
162 }
163
164 if ((s->raw=(zip_uint8_t *)malloc(length+1)) == NULL) {
165 free(s);
166 return NULL;
167 }
168
169 memcpy(s->raw, raw, length);
170 s->raw[length] = '\0';
171 s->length = length;
172 s->encoding = ZIP_ENCODING_UNKNOWN;
173 s->converted = NULL;
174 s->converted_length = 0;
175
176 if (expected_encoding != ZIP_ENCODING_UNKNOWN) {
177 if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) {
178 _zip_string_free(s);
179 _zip_error_set(error, ZIP_ER_INVAL, 0);
180 return NULL;
181 }
182 }
183
184 return s;
185 }
186
187
188
189 void
190 _zip_string_write(const struct zip_string *s, FILE *f)
191 {
192 if (s == NULL)
193 return;
194
195 fwrite(s->raw, s->length, 1, f);
196 }