zip_string.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. zip_string.c -- string handling (with encoding)
  3. Copyright (C) 2012 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <[email protected]>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "zipint.h"
  33. zip_uint32_t
  34. _zip_string_crc32(const struct zip_string *s)
  35. {
  36. zip_uint32_t crc;
  37. crc = (zip_uint32_t)crc32(0L, Z_NULL, 0);
  38. if (s != NULL)
  39. crc = (zip_uint32_t)crc32(crc, s->raw, s->length);
  40. return crc;
  41. }
  42. int
  43. _zip_string_equal(const struct zip_string *a, const struct zip_string *b)
  44. {
  45. if (a == NULL || b == NULL)
  46. return a == b;
  47. if (a->length != b->length)
  48. return 0;
  49. /* TODO: encoding */
  50. return (memcmp(a->raw, b->raw, a->length) == 0);
  51. }
  52. void
  53. _zip_string_free(struct zip_string *s)
  54. {
  55. if (s == NULL)
  56. return;
  57. free(s->raw);
  58. free(s->converted);
  59. free(s);
  60. }
  61. const zip_uint8_t *
  62. _zip_string_get(struct zip_string *string, zip_uint32_t *lenp, zip_flags_t flags, struct zip_error *error)
  63. {
  64. static const zip_uint8_t empty[1] = "";
  65. if (string == NULL) {
  66. if (lenp)
  67. *lenp = 0;
  68. return empty;
  69. }
  70. if ((flags & ZIP_FL_ENC_RAW) == 0) {
  71. /* start guessing */
  72. if (string->encoding == ZIP_ENCODING_UNKNOWN)
  73. _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN);
  74. if (((flags & ZIP_FL_ENC_STRICT)
  75. && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN)
  76. || (string->encoding == ZIP_ENCODING_CP437)) {
  77. if (string->converted == NULL) {
  78. if ((string->converted=_zip_cp437_to_utf8(string->raw, string->length,
  79. &string->converted_length, error)) == NULL)
  80. return NULL;
  81. }
  82. if (lenp)
  83. *lenp = string->converted_length;
  84. return string->converted;
  85. }
  86. }
  87. if (lenp)
  88. *lenp = string->length;
  89. return string->raw;
  90. }
  91. zip_uint16_t
  92. _zip_string_length(const struct zip_string *s)
  93. {
  94. if (s == NULL)
  95. return 0;
  96. return s->length;
  97. }
  98. struct zip_string *
  99. _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, struct zip_error *error)
  100. {
  101. struct zip_string *s;
  102. enum zip_encoding_type expected_encoding;
  103. if (length == 0)
  104. return NULL;
  105. switch (flags & ZIP_FL_ENCODING_ALL) {
  106. case ZIP_FL_ENC_GUESS:
  107. expected_encoding = ZIP_ENCODING_UNKNOWN;
  108. break;
  109. case ZIP_FL_ENC_UTF_8:
  110. expected_encoding = ZIP_ENCODING_UTF8_KNOWN;
  111. break;
  112. case ZIP_FL_ENC_CP437:
  113. expected_encoding = ZIP_ENCODING_CP437;
  114. break;
  115. default:
  116. _zip_error_set(error, ZIP_ER_INVAL, 0);
  117. return NULL;
  118. }
  119. if ((s=(struct zip_string *)malloc(sizeof(*s))) == NULL) {
  120. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  121. return NULL;
  122. }
  123. if ((s->raw=(zip_uint8_t *)malloc(length+1)) == NULL) {
  124. free(s);
  125. return NULL;
  126. }
  127. memcpy(s->raw, raw, length);
  128. s->raw[length] = '\0';
  129. s->length = length;
  130. s->encoding = ZIP_ENCODING_UNKNOWN;
  131. s->converted = NULL;
  132. s->converted_length = 0;
  133. if (expected_encoding != ZIP_ENCODING_UNKNOWN) {
  134. if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) {
  135. _zip_string_free(s);
  136. _zip_error_set(error, ZIP_ER_INVAL, 0);
  137. return NULL;
  138. }
  139. }
  140. return s;
  141. }
  142. void
  143. _zip_string_write(const struct zip_string *s, FILE *f)
  144. {
  145. if (s == NULL)
  146. return;
  147. fwrite(s->raw, s->length, 1, f);
  148. }