Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | zip_string.c -- string handling (with encoding) |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 3 | Copyright (C) 2012-2014 Dieter Baron and Thomas Klausner |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 4 | |
| 5 | This file is part of libzip, a library to manipulate ZIP archives. |
| 6 | The authors can be contacted at <libzip@nih.at> |
| 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 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "zipint.h" |
| 39 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 40 | |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 41 | zip_uint32_t |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 42 | _zip_string_crc32(const zip_string_t *s) |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 43 | { |
| 44 | zip_uint32_t crc; |
| 45 | |
Dieter Baron | abc6fd7 | 2012-07-22 15:49:45 +0200 | [diff] [blame] | 46 | crc = (zip_uint32_t)crc32(0L, Z_NULL, 0); |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 47 | |
| 48 | if (s != NULL) |
Dieter Baron | abc6fd7 | 2012-07-22 15:49:45 +0200 | [diff] [blame] | 49 | crc = (zip_uint32_t)crc32(crc, s->raw, s->length); |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 50 | |
| 51 | return crc; |
| 52 | } |
| 53 | |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 54 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 55 | int |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 56 | _zip_string_equal(const zip_string_t *a, const zip_string_t *b) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 57 | { |
| 58 | if (a == NULL || b == NULL) |
| 59 | return a == b; |
| 60 | |
| 61 | if (a->length != b->length) |
| 62 | return 0; |
| 63 | |
Thomas Klausner | b52bda0 | 2013-11-28 18:01:40 +0100 | [diff] [blame] | 64 | /* TODO: encoding */ |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 65 | |
| 66 | return (memcmp(a->raw, b->raw, a->length) == 0); |
| 67 | } |
| 68 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 69 | |
| 70 | void |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 71 | _zip_string_free(zip_string_t *s) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 72 | { |
| 73 | if (s == NULL) |
| 74 | return; |
| 75 | |
| 76 | free(s->raw); |
| 77 | free(s->converted); |
| 78 | free(s); |
| 79 | } |
| 80 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 81 | |
| 82 | const zip_uint8_t * |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 83 | _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 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 | |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 93 | if ((flags & ZIP_FL_ENC_RAW) == 0) { |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 94 | /* start guessing */ |
| 95 | if (string->encoding == ZIP_ENCODING_UNKNOWN) |
| 96 | _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN); |
| 97 | |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 98 | if (((flags & ZIP_FL_ENC_STRICT) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 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 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 117 | |
| 118 | zip_uint16_t |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 119 | _zip_string_length(const zip_string_t *s) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 120 | { |
| 121 | if (s == NULL) |
| 122 | return 0; |
| 123 | |
| 124 | return s->length; |
| 125 | } |
| 126 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 127 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 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) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 130 | { |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 131 | zip_string_t *s; |
| 132 | zip_encoding_type_t expected_encoding; |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 133 | |
| 134 | if (length == 0) |
| 135 | return NULL; |
| 136 | |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 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: |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 148 | zip_error_set(error, ZIP_ER_INVAL, 0); |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 149 | return NULL; |
| 150 | } |
| 151 | |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 152 | if ((s=(zip_string_t *)malloc(sizeof(*s))) == NULL) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 153 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | |
Thomas Klausner | 03ca1c1 | 2014-09-24 01:02:15 +0200 | [diff] [blame] | 157 | if ((s->raw=(zip_uint8_t *)malloc((size_t)(length+1))) == NULL) { |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 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 | |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 169 | if (expected_encoding != ZIP_ENCODING_UNKNOWN) { |
| 170 | if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) { |
| 171 | _zip_string_free(s); |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 172 | zip_error_set(error, ZIP_ER_INVAL, 0); |
Thomas Klausner | 0830a77 | 2012-05-18 19:52:08 +0200 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | } |
| 176 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 177 | return s; |
| 178 | } |
| 179 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 180 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 181 | int |
| 182 | _zip_string_write(zip_t *za, const zip_string_t *s) |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 183 | { |
| 184 | if (s == NULL) |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 185 | return 0; |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 186 | |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 187 | return _zip_write(za, s->raw, s->length); |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 188 | } |