/src/libzip/lib/zip_string.c
Line | Count | Source |
1 | | /* |
2 | | zip_string.c -- string handling (with encoding) |
3 | | Copyright (C) 2012-2025 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | |
35 | | #include "zipint.h" |
36 | | |
37 | | #include <stdlib.h> |
38 | | #include <string.h> |
39 | | #include <zlib.h> |
40 | | |
41 | 7.28k | zip_uint32_t _zip_string_crc32(const zip_string_t *s) { |
42 | 7.28k | zip_uint32_t crc; |
43 | | |
44 | 7.28k | crc = (zip_uint32_t)crc32(0L, Z_NULL, 0); |
45 | | |
46 | 7.28k | if (s != NULL) { |
47 | 3.52k | crc = (zip_uint32_t)crc32(crc, s->raw, s->length); |
48 | 3.52k | } |
49 | | |
50 | 7.28k | return crc; |
51 | 7.28k | } |
52 | | |
53 | | |
54 | 0 | int _zip_string_equal(const zip_string_t *a, const zip_string_t *b) { |
55 | 0 | if (a == NULL || b == NULL) { |
56 | 0 | return a == b; |
57 | 0 | } |
58 | | |
59 | 0 | if (a->length != b->length) { |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | | /* TODO: encoding */ |
64 | | |
65 | 0 | return (memcmp(a->raw, b->raw, a->length) == 0); |
66 | 0 | } |
67 | | |
68 | | |
69 | 208k | void _zip_string_free(zip_string_t *s) { |
70 | 208k | if (s == NULL) { |
71 | 144k | return; |
72 | 144k | } |
73 | | |
74 | 64.2k | free(s->raw); |
75 | 64.2k | free(s->converted); |
76 | 64.2k | free(s); |
77 | 64.2k | } |
78 | | |
79 | | |
80 | 48.2k | const zip_uint8_t *_zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) { |
81 | 48.2k | static const zip_uint8_t empty[1] = ""; |
82 | | |
83 | 48.2k | if (string == NULL) { |
84 | 37.7k | if (lenp) { |
85 | 0 | *lenp = 0; |
86 | 0 | } |
87 | 37.7k | return empty; |
88 | 37.7k | } |
89 | | |
90 | 10.4k | if ((flags & ZIP_FL_ENC_RAW) == 0) { |
91 | | /* start guessing */ |
92 | 10.4k | if (string->encoding == ZIP_ENCODING_UNKNOWN) { |
93 | | /* guess encoding, sets string->encoding */ |
94 | 5.11k | (void)_zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN); |
95 | 5.11k | } |
96 | | |
97 | 10.4k | if (((flags & ZIP_FL_ENC_STRICT) && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) || (string->encoding == ZIP_ENCODING_CP437)) { |
98 | 3.58k | if (string->converted == NULL) { |
99 | 1.79k | if ((string->converted = _zip_cp437_to_utf8(string->raw, string->length, &string->converted_length, error)) == NULL) { |
100 | 0 | return NULL; |
101 | 0 | } |
102 | 1.79k | } |
103 | 3.58k | if (lenp) { |
104 | 0 | *lenp = string->converted_length; |
105 | 0 | } |
106 | 3.58k | return string->converted; |
107 | 3.58k | } |
108 | 10.4k | } |
109 | | |
110 | 6.90k | if (lenp) { |
111 | 0 | *lenp = string->length; |
112 | 0 | } |
113 | 6.90k | return string->raw; |
114 | 10.4k | } |
115 | | |
116 | 0 | bool _zip_string_is_ascii(const zip_string_t *string) { |
117 | 0 | if (string->encoding != ZIP_ENCODING_ASCII) { |
118 | 0 | zip_uint16_t i; |
119 | |
|
120 | 0 | for (i = 0; i < string->length; i++) { |
121 | 0 | if (string->raw[i] & 0x80) { |
122 | 0 | return false; |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | 0 | return true; |
128 | 0 | } |
129 | | |
130 | | |
131 | 2.09k | zip_uint16_t _zip_string_length(const zip_string_t *s) { |
132 | 2.09k | if (s == NULL) { |
133 | 1.65k | return 0; |
134 | 1.65k | } |
135 | | |
136 | 441 | return s->length; |
137 | 2.09k | } |
138 | | |
139 | | |
140 | 64.7k | zip_string_t *_zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error) { |
141 | 64.7k | zip_string_t *s; |
142 | 64.7k | zip_encoding_type_t expected_encoding; |
143 | | |
144 | 64.7k | if (length == 0) { |
145 | 508 | return NULL; |
146 | 508 | } |
147 | | |
148 | 64.2k | switch (flags & ZIP_FL_ENCODING_ALL) { |
149 | 58.9k | case ZIP_FL_ENC_GUESS: |
150 | 58.9k | expected_encoding = ZIP_ENCODING_UNKNOWN; |
151 | 58.9k | break; |
152 | 5.23k | case ZIP_FL_ENC_UTF_8: |
153 | 5.23k | expected_encoding = ZIP_ENCODING_UTF8_KNOWN; |
154 | 5.23k | break; |
155 | 0 | case ZIP_FL_ENC_CP437: |
156 | 0 | expected_encoding = ZIP_ENCODING_CP437; |
157 | 0 | break; |
158 | 0 | default: |
159 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
160 | 0 | return NULL; |
161 | 64.2k | } |
162 | | |
163 | 64.2k | if ((s = (zip_string_t *)malloc(sizeof(*s))) == NULL) { |
164 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
165 | 0 | return NULL; |
166 | 0 | } |
167 | | |
168 | 64.2k | if ((s->raw = (zip_uint8_t *)malloc((size_t)length + 1)) == NULL) { |
169 | 0 | free(s); |
170 | 0 | return NULL; |
171 | 0 | } |
172 | | |
173 | 64.2k | (void)memcpy_s(s->raw, length + 1, raw, length); |
174 | 64.2k | s->raw[length] = '\0'; |
175 | 64.2k | s->length = length; |
176 | 64.2k | s->encoding = ZIP_ENCODING_UNKNOWN; |
177 | 64.2k | s->converted = NULL; |
178 | 64.2k | s->converted_length = 0; |
179 | | |
180 | 64.2k | if (expected_encoding != ZIP_ENCODING_UNKNOWN) { |
181 | 5.23k | if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) { |
182 | 2.70k | _zip_string_free(s); |
183 | 2.70k | zip_error_set(error, ZIP_ER_INVAL, 0); |
184 | 2.70k | return NULL; |
185 | 2.70k | } |
186 | 5.23k | } |
187 | | |
188 | 61.5k | return s; |
189 | 64.2k | } |
190 | | |
191 | | |
192 | 0 | int _zip_string_write(zip_t *za, const zip_string_t *s) { |
193 | 0 | if (s == NULL) { |
194 | 0 | return 0; |
195 | 0 | } |
196 | | |
197 | 0 | return _zip_write(za, s->raw, s->length); |
198 | 0 | } |