blob: 53a5dd2653bc13761fdb3323d809a02d94351e80 [file] [log] [blame]
Thomas Klausnerea8ba492014-09-23 16:54:47 +02001/*
2 zip_io_util.c -- I/O helper functions
Thomas Klausnerdf77dc62016-12-31 13:40:32 +01003 Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner
Thomas Klausnerea8ba492014-09-23 16:54:47 +02004
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
Thomas Klausnerea8ba492014-09-23 16:54:47 +020034#include <stdlib.h>
35#include <string.h>
36
37#include "zipint.h"
38
Thomas Klausnerea8ba492014-09-23 16:54:47 +020039int
Dieter Baron1d9dfeb2014-09-28 23:02:54 +020040_zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error)
Thomas Klausnerea8ba492014-09-23 16:54:47 +020041{
42 zip_int64_t n;
43
44 if (length > ZIP_INT64_MAX) {
45 zip_error_set(error, ZIP_ER_INTERNAL, 0);
46 return -1;
47 }
48
49 if ((n = zip_source_read(src, b, length)) < 0) {
Thomas Klausnerd97f5442014-09-23 17:02:03 +020050 _zip_error_set_from_source(error, src);
Thomas Klausnerea8ba492014-09-23 16:54:47 +020051 return -1;
52 }
53
54 if (n < (zip_int64_t)length) {
55 zip_error_set(error, ZIP_ER_EOF, 0);
56 return -1;
57 }
58
59 return 0;
60}
61
62
63zip_uint8_t *
Dieter Baronee25b7d2014-10-11 18:30:13 +020064_zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error)
Thomas Klausnerea8ba492014-09-23 16:54:47 +020065{
66 zip_uint8_t *r;
Dieter Baronee25b7d2014-10-11 18:30:13 +020067
68 if (length == 0 && !nulp) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +020069 return NULL;
Dieter Baronee25b7d2014-10-11 18:30:13 +020070 }
Thomas Klausnerea8ba492014-09-23 16:54:47 +020071
Dieter Baronee25b7d2014-10-11 18:30:13 +020072 r = (zip_uint8_t *)malloc(length + (nulp ? 1 : 0));
Thomas Klausnerea8ba492014-09-23 16:54:47 +020073 if (!r) {
74 zip_error_set(error, ZIP_ER_MEMORY, 0);
75 return NULL;
76 }
77
Dieter Baronee25b7d2014-10-11 18:30:13 +020078 if (buffer) {
79 zip_uint8_t *data = _zip_buffer_get(buffer, length);
80
81 if (data == NULL) {
82 zip_error_set(error, ZIP_ER_MEMORY, 0);
83 free(r);
84 return NULL;
85 }
86 memcpy(r, data, length);
Thomas Klausnerea8ba492014-09-23 16:54:47 +020087 }
88 else {
Dieter Baronee25b7d2014-10-11 18:30:13 +020089 if (_zip_read(src, r, length, error) < 0) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +020090 free(r);
91 return NULL;
92 }
93 }
94
95 if (nulp) {
96 zip_uint8_t *o;
97 /* replace any in-string NUL characters with spaces */
Dieter Baronee25b7d2014-10-11 18:30:13 +020098 r[length] = 0;
99 for (o=r; o<r+length; o++)
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200100 if (*o == '\0')
101 *o = ' ';
102 }
103
104 return r;
105}
106
107
Dieter Baron1d9dfeb2014-09-28 23:02:54 +0200108zip_string_t *
Dieter Baronee25b7d2014-10-11 18:30:13 +0200109_zip_read_string(zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t len, bool nulp, zip_error_t *error)
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200110{
111 zip_uint8_t *raw;
Dieter Baron1d9dfeb2014-09-28 23:02:54 +0200112 zip_string_t *s;
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200113
Dieter Baronee25b7d2014-10-11 18:30:13 +0200114 if ((raw=_zip_read_data(buffer, src, len, nulp, error)) == NULL)
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200115 return NULL;
116
117 s = _zip_string_new(raw, len, ZIP_FL_ENC_GUESS, error);
118 free(raw);
119 return s;
120}
121
122
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200123int
Dieter Baron1d9dfeb2014-09-28 23:02:54 +0200124_zip_write(zip_t *za, const void *data, zip_uint64_t length)
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200125{
126 zip_int64_t n;
127
128 if ((n = zip_source_write(za->src, data, length)) < 0) {
Thomas Klausnerd97f5442014-09-23 17:02:03 +0200129 _zip_error_set_from_source(&za->error, za->src);
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200130 return -1;
131 }
132 if ((zip_uint64_t)n != length) {
133 zip_error_set(&za->error, ZIP_ER_WRITE, EINTR);
134 return -1;
135 }
136
137 return 0;
138}