Rizin
unix-like reverse engineering framework and cli tools
zip_io_util.c
Go to the documentation of this file.
1 /*
2  zip_io_util.c -- I/O helper functions
3  Copyright (C) 1999-2021 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 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "zipint.h"
38 
39 int
41  zip_int64_t n;
42 
43  if (length > ZIP_INT64_MAX) {
45  return -1;
46  }
47 
48  if ((n = zip_source_read(src, b, length)) < 0) {
50  return -1;
51  }
52 
53  if (n < (zip_int64_t)length) {
55  return -1;
56  }
57 
58  return 0;
59 }
60 
61 
64  zip_uint8_t *r;
65 
66  if (length == 0 && !nulp) {
67  return NULL;
68  }
69 
70  r = (zip_uint8_t *)malloc(length + (nulp ? 1 : 0));
71  if (!r) {
73  return NULL;
74  }
75 
76  if (buffer) {
78 
79  if (data == NULL) {
81  free(r);
82  return NULL;
83  }
84  memcpy(r, data, length);
85  }
86  else {
87  if (_zip_read(src, r, length, error) < 0) {
88  free(r);
89  return NULL;
90  }
91  }
92 
93  if (nulp) {
94  zip_uint8_t *o;
95  /* replace any in-string NUL characters with spaces */
96  r[length] = 0;
97  for (o = r; o < r + length; o++)
98  if (*o == '\0')
99  *o = ' ';
100  }
101 
102  return r;
103 }
104 
105 
106 zip_string_t *
108  zip_uint8_t *raw;
109  zip_string_t *s;
110 
111  if ((raw = _zip_read_data(buffer, src, len, nulp, error)) == NULL)
112  return NULL;
113 
115  free(raw);
116  return s;
117 }
118 
119 
120 int
121 _zip_write(zip_t *za, const void *data, zip_uint64_t length) {
122  zip_int64_t n;
123 
124  if ((n = zip_source_write(za->src, data, length)) < 0) {
126  return -1;
127  }
128  if ((zip_uint64_t)n != length) {
130  return -1;
131  }
132 
133  return 0;
134 }
size_t len
Definition: 6502dis.c:15
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define ZIP_ER_INTERNAL
Definition: zip.h:125
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_WRITE
Definition: zip.h:111
#define ZIP_ER_MEMORY
Definition: zip.h:119
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
#define ZIP_FL_ENC_GUESS
Definition: zip.h:82
ZIP_EXTERN zip_int64_t zip_source_write(zip_source_t *_Nonnull, const void *_Nullable, zip_uint64_t)
#define ZIP_ER_EOF
Definition: zip.h:122
void * malloc(size_t size)
Definition: malloc.c:123
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
#define EINTR
Definition: sftypes.h:114
#define b(i)
Definition: sha256.c:42
Definition: buffer.h:15
Definition: zip.h:284
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
zip_source_t * src
Definition: zipint.h:279
void error(const char *msg)
Definition: untgz.c:593
zip_uint8_t * _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length)
Definition: zip_buffer.c:66
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
zip_uint8_t * _zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error)
Definition: zip_io_util.c:63
int _zip_write(zip_t *za, const void *data, zip_uint64_t length)
Definition: zip_io_util.c:121
int _zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error)
Definition: zip_io_util.c:40
zip_string_t * _zip_read_string(zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t len, bool nulp, zip_error_t *error)
Definition: zip_io_util.c:107
zip_string_t * _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:121
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
#define ZIP_INT64_MAX
Definition: zipconf.h:54
int64_t zip_int64_t
Definition: zipconf.h:38
zip_t * za
Definition: ziptool.c:79