diff options
Diffstat (limited to 'src/Common/libzip/zip_string.c')
-rw-r--r-- | src/Common/libzip/zip_string.c | 134 |
1 files changed, 62 insertions, 72 deletions
diff --git a/src/Common/libzip/zip_string.c b/src/Common/libzip/zip_string.c index 307a425f..1c964491 100644 --- a/src/Common/libzip/zip_string.c +++ b/src/Common/libzip/zip_string.c @@ -1,9 +1,9 @@ /* zip_string.c -- string handling (with encoding) - Copyright (C) 2012-2014 Dieter Baron and Thomas Klausner + Copyright (C) 2012-2021 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. - The authors can be contacted at <libzip@nih.at> + The authors can be contacted at <info@libzip.org> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -34,32 +34,30 @@ #include <stdlib.h> #include <string.h> +#include <zlib.h> #include "zipint.h" - zip_uint32_t -_zip_string_crc32(const zip_string_t *s) -{ +_zip_string_crc32(const zip_string_t *s) { zip_uint32_t crc; - + crc = (zip_uint32_t)crc32(0L, Z_NULL, 0); - if (s != NULL) - crc = (zip_uint32_t)crc32(crc, s->raw, s->length); + if (s != NULL) + crc = (zip_uint32_t)crc32(crc, s->raw, s->length); return crc; } int -_zip_string_equal(const zip_string_t *a, const zip_string_t *b) -{ +_zip_string_equal(const zip_string_t *a, const zip_string_t *b) { if (a == NULL || b == NULL) - return a == b; + return a == b; if (a->length != b->length) - return 0; + return 0; /* TODO: encoding */ @@ -68,10 +66,9 @@ _zip_string_equal(const zip_string_t *a, const zip_string_t *b) void -_zip_string_free(zip_string_t *s) -{ +_zip_string_free(zip_string_t *s) { if (s == NULL) - return; + return; free(s->raw); free(s->converted); @@ -80,86 +77,80 @@ _zip_string_free(zip_string_t *s) const zip_uint8_t * -_zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) -{ +_zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) { static const zip_uint8_t empty[1] = ""; if (string == NULL) { - if (lenp) - *lenp = 0; - return empty; + if (lenp) + *lenp = 0; + return empty; } if ((flags & ZIP_FL_ENC_RAW) == 0) { - /* start guessing */ - if (string->encoding == ZIP_ENCODING_UNKNOWN) - _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN); - - if (((flags & ZIP_FL_ENC_STRICT) - && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) - || (string->encoding == ZIP_ENCODING_CP437)) { - if (string->converted == NULL) { - if ((string->converted=_zip_cp437_to_utf8(string->raw, string->length, - &string->converted_length, error)) == NULL) - return NULL; - } - if (lenp) - *lenp = string->converted_length; - return string->converted; - } + /* start guessing */ + if (string->encoding == ZIP_ENCODING_UNKNOWN) + _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN); + + if (((flags & ZIP_FL_ENC_STRICT) && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) || (string->encoding == ZIP_ENCODING_CP437)) { + if (string->converted == NULL) { + if ((string->converted = _zip_cp437_to_utf8(string->raw, string->length, &string->converted_length, error)) == NULL) + return NULL; + } + if (lenp) + *lenp = string->converted_length; + return string->converted; + } } - + if (lenp) - *lenp = string->length; + *lenp = string->length; return string->raw; } zip_uint16_t -_zip_string_length(const zip_string_t *s) -{ +_zip_string_length(const zip_string_t *s) { if (s == NULL) - return 0; + return 0; return s->length; } zip_string_t * -_zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error) -{ +_zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error) { zip_string_t *s; zip_encoding_type_t expected_encoding; - + if (length == 0) - return NULL; + return NULL; switch (flags & ZIP_FL_ENCODING_ALL) { case ZIP_FL_ENC_GUESS: - expected_encoding = ZIP_ENCODING_UNKNOWN; - break; + expected_encoding = ZIP_ENCODING_UNKNOWN; + break; case ZIP_FL_ENC_UTF_8: - expected_encoding = ZIP_ENCODING_UTF8_KNOWN; - break; + expected_encoding = ZIP_ENCODING_UTF8_KNOWN; + break; case ZIP_FL_ENC_CP437: - expected_encoding = ZIP_ENCODING_CP437; - break; + expected_encoding = ZIP_ENCODING_CP437; + break; default: - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; } - - if ((s=(zip_string_t *)malloc(sizeof(*s))) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return NULL; + + if ((s = (zip_string_t *)malloc(sizeof(*s))) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + return NULL; } - if ((s->raw=(zip_uint8_t *)malloc((size_t)(length+1))) == NULL) { - free(s); - return NULL; + if ((s->raw = (zip_uint8_t *)malloc((size_t)length + 1)) == NULL) { + free(s); + return NULL; } - memcpy(s->raw, raw, length); + (void)memcpy_s(s->raw, length + 1, raw, length); s->raw[length] = '\0'; s->length = length; s->encoding = ZIP_ENCODING_UNKNOWN; @@ -167,22 +158,21 @@ _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, s->converted_length = 0; if (expected_encoding != ZIP_ENCODING_UNKNOWN) { - if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) { - _zip_string_free(s); - zip_error_set(error, ZIP_ER_INVAL, 0); - return NULL; - } + if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) { + _zip_string_free(s); + zip_error_set(error, ZIP_ER_INVAL, 0); + return NULL; + } } - + return s; } int -_zip_string_write(zip_t *za, const zip_string_t *s) -{ +_zip_string_write(zip_t *za, const zip_string_t *s) { if (s == NULL) - return 0; - + return 0; + return _zip_write(za, s->raw, s->length); } |