Rizin
unix-like reverse engineering framework and cli tools
zip_string.c File Reference
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "zipint.h"

Go to the source code of this file.

Functions

zip_uint32_t _zip_string_crc32 (const zip_string_t *s)
 
int _zip_string_equal (const zip_string_t *a, const zip_string_t *b)
 
void _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_uint16_t _zip_string_length (const zip_string_t *s)
 
zip_string_t_zip_string_new (const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error)
 
int _zip_string_write (zip_t *za, const zip_string_t *s)
 

Function Documentation

◆ _zip_string_crc32()

zip_uint32_t _zip_string_crc32 ( const zip_string_t s)

Definition at line 42 of file zip_string.c.

42  {
43  zip_uint32_t crc;
44 
45  crc = (zip_uint32_t)crc32(0L, Z_NULL, 0);
46 
47  if (s != NULL)
48  crc = (zip_uint32_t)crc32(crc, s->raw, s->length);
49 
50  return crc;
51 }
#define NULL
Definition: cris-opc.c:27
static RzSocket * s
Definition: rtr.c:28
#define L
Definition: zip_err_str.c:7
uint32_t zip_uint32_t
Definition: zipconf.h:37
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:1063
#define Z_NULL
Definition: zlib.h:212

References crc32(), L, NULL, s, and Z_NULL.

Referenced by _zip_dirent_process_ef_utf_8(), and _zip_ef_utf8().

◆ _zip_string_equal()

int _zip_string_equal ( const zip_string_t a,
const zip_string_t b 
)

Definition at line 55 of file zip_string.c.

55  {
56  if (a == NULL || b == NULL)
57  return a == b;
58 
59  if (a->length != b->length)
60  return 0;
61 
62  /* TODO: encoding */
63 
64  return (memcmp(a->raw, b->raw, a->length) == 0);
65 }
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, b, and NULL.

Referenced by _zip_headercomp(), _zip_set_name(), zip_file_set_comment(), and zip_set_archive_comment().

◆ _zip_string_free()

void _zip_string_free ( zip_string_t s)

Definition at line 69 of file zip_string.c.

69  {
70  if (s == NULL)
71  return;
72 
73  free(s->raw);
74  free(s->converted);
75  free(s);
76 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), NULL, and s.

Referenced by _zip_cdir_free(), _zip_dirent_finalize(), _zip_dirent_process_ef_utf_8(), _zip_name_locate(), _zip_set_name(), _zip_string_new(), zip_discard(), zip_file_set_comment(), zip_set_archive_comment(), and zip_unchange_archive().

◆ _zip_string_get()

const zip_uint8_t* _zip_string_get ( zip_string_t string,
zip_uint32_t lenp,
zip_flags_t  flags,
zip_error_t error 
)

Definition at line 80 of file zip_string.c.

80  {
81  static const zip_uint8_t empty[1] = "";
82 
83  if (string == NULL) {
84  if (lenp)
85  *lenp = 0;
86  return empty;
87  }
88 
89  if ((flags & ZIP_FL_ENC_RAW) == 0) {
90  /* start guessing */
91  if (string->encoding == ZIP_ENCODING_UNKNOWN)
93 
94  if (((flags & ZIP_FL_ENC_STRICT) && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) || (string->encoding == ZIP_ENCODING_CP437)) {
95  if (string->converted == NULL) {
96  if ((string->converted = _zip_cp437_to_utf8(string->raw, string->length, &string->converted_length, error)) == NULL)
97  return NULL;
98  }
99  if (lenp)
100  *lenp = string->converted_length;
101  return string->converted;
102  }
103  }
104 
105  if (lenp)
106  *lenp = string->length;
107  return string->raw;
108 }
#define ZIP_FL_ENC_STRICT
Definition: zip.h:84
#define ZIP_FL_ENC_RAW
Definition: zip.h:83
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
zip_uint8_t * raw
Definition: zipint.h:419
enum zip_encoding_type encoding
Definition: zipint.h:421
zip_uint32_t converted_length
Definition: zipint.h:423
zip_uint8_t * converted
Definition: zipint.h:422
zip_uint16_t length
Definition: zipint.h:420
void error(const char *msg)
Definition: untgz.c:593
zip_uint8_t * _zip_cp437_to_utf8(const zip_uint8_t *const _cp437buf, zip_uint32_t len, zip_uint32_t *utf8_lenp, zip_error_t *error)
Definition: zip_utf-8.c:198
zip_encoding_type_t _zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding)
Definition: zip_utf-8.c:100
uint8_t zip_uint8_t
Definition: zipconf.h:33
@ ZIP_ENCODING_UTF8_KNOWN
Definition: zipint.h:256
@ ZIP_ENCODING_UNKNOWN
Definition: zipint.h:254
@ ZIP_ENCODING_CP437
Definition: zipint.h:258
@ ZIP_ENCODING_ASCII
Definition: zipint.h:255

References _zip_cp437_to_utf8(), _zip_guess_encoding(), zip_string::converted, zip_string::converted_length, zip_string::encoding, error(), flags, zip_string::length, NULL, zip_string::raw, ZIP_ENCODING_ASCII, ZIP_ENCODING_CP437, ZIP_ENCODING_UNKNOWN, ZIP_ENCODING_UTF8_KNOWN, ZIP_FL_ENC_RAW, and ZIP_FL_ENC_STRICT.

Referenced by _zip_ef_utf8(), _zip_get_name(), _zip_name_locate(), _zip_open(), _zip_set_name(), zip_file_get_comment(), and zip_get_archive_comment().

◆ _zip_string_length()

zip_uint16_t _zip_string_length ( const zip_string_t s)

Definition at line 112 of file zip_string.c.

112  {
113  if (s == NULL)
114  return 0;
115 
116  return s->length;
117 }

References NULL, and s.

Referenced by _zip_checkcons(), _zip_dirent_apply_attributes(), and _zip_dirent_write().

◆ _zip_string_new()

zip_string_t* _zip_string_new ( const zip_uint8_t raw,
zip_uint16_t  length,
zip_flags_t  flags,
zip_error_t error 
)

Definition at line 121 of file zip_string.c.

121  {
122  zip_string_t *s;
123  zip_encoding_type_t expected_encoding;
124 
125  if (length == 0)
126  return NULL;
127 
128  switch (flags & ZIP_FL_ENCODING_ALL) {
129  case ZIP_FL_ENC_GUESS:
130  expected_encoding = ZIP_ENCODING_UNKNOWN;
131  break;
132  case ZIP_FL_ENC_UTF_8:
133  expected_encoding = ZIP_ENCODING_UTF8_KNOWN;
134  break;
135  case ZIP_FL_ENC_CP437:
136  expected_encoding = ZIP_ENCODING_CP437;
137  break;
138  default:
140  return NULL;
141  }
142 
143  if ((s = (zip_string_t *)malloc(sizeof(*s))) == NULL) {
145  return NULL;
146  }
147 
148  if ((s->raw = (zip_uint8_t *)malloc((size_t)length + 1)) == NULL) {
149  free(s);
150  return NULL;
151  }
152 
153  memcpy(s->raw, raw, length);
154  s->raw[length] = '\0';
155  s->length = length;
156  s->encoding = ZIP_ENCODING_UNKNOWN;
157  s->converted = NULL;
158  s->converted_length = 0;
159 
160  if (expected_encoding != ZIP_ENCODING_UNKNOWN) {
161  if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) {
164  return NULL;
165  }
166  }
167 
168  return s;
169 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_FL_ENC_GUESS
Definition: zip.h:82
#define ZIP_FL_ENC_CP437
Definition: zip.h:89
#define ZIP_ER_INVAL
Definition: zip.h:123
#define ZIP_FL_ENC_UTF_8
Definition: zip.h:88
void * malloc(size_t size)
Definition: malloc.c:123
void _zip_string_free(zip_string_t *s)
Definition: zip_string.c:69
enum zip_encoding_type zip_encoding_type_t
Definition: zipint.h:262
#define ZIP_FL_ENCODING_ALL
Definition: zipint.h:249
@ ZIP_ENCODING_ERROR
Definition: zipint.h:259

References _zip_guess_encoding(), _zip_string_free(), error(), flags, free(), length, malloc(), memcpy(), NULL, s, ZIP_ENCODING_CP437, ZIP_ENCODING_ERROR, ZIP_ENCODING_UNKNOWN, ZIP_ENCODING_UTF8_KNOWN, ZIP_ER_INVAL, ZIP_ER_MEMORY, zip_error_set(), ZIP_FL_ENC_CP437, ZIP_FL_ENC_GUESS, ZIP_FL_ENC_UTF_8, and ZIP_FL_ENCODING_ALL.

Referenced by _zip_dirent_process_ef_utf_8(), _zip_name_locate(), _zip_read_cdir(), _zip_read_string(), _zip_set_name(), zip_file_set_comment(), and zip_set_archive_comment().

◆ _zip_string_write()

int _zip_string_write ( zip_t za,
const zip_string_t s 
)

Definition at line 173 of file zip_string.c.

173  {
174  if (s == NULL)
175  return 0;
176 
177  return _zip_write(za, s->raw, s->length);
178 }
int _zip_write(zip_t *za, const void *data, zip_uint64_t length)
Definition: zip_io_util.c:121
zip_t * za
Definition: ziptool.c:79

References _zip_write(), NULL, s, and za.

Referenced by _zip_dirent_write().