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