blob: 858a78a4724fbbd0ce37ba93ba439efbcaa6df55 [file] [log] [blame]
Dieter Baronbbb63691999-07-26 22:04:20 +00001/*
Dieter Baronb2ed74d2004-04-14 14:01:31 +00002 zip_file_get_offset.c -- get offset of file data in archive.
Thomas Klausnere91cc0f2019-03-12 12:39:36 +01003 Copyright (C) 1999-2018 Dieter Baron and Thomas Klausner
Dieter Baronbbb63691999-07-26 22:04:20 +00004
Dieter Barondd9afca2003-10-02 14:13:37 +00005 This file is part of libzip, a library to manipulate ZIP archives.
Dieter Baronb86c4332007-11-07 14:35:13 +01006 The authors can be contacted at <libzip@nih.at>
Dieter Baronbbb63691999-07-26 22:04:20 +00007
Dieter Barondd9afca2003-10-02 14:13:37 +00008 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.
Thomas Klausner8eab1a22018-01-15 14:10:11 +010020
Dieter Barondd9afca2003-10-02 14:13:37 +000021 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.
Dieter Baronbbb63691999-07-26 22:04:20 +000032*/
33
Dieter Baronbbb63691999-07-26 22:04:20 +000034
Dieter Baronb2ed74d2004-04-14 14:01:31 +000035#include <stdio.h>
Thomas Klausnerc1364c51999-07-26 02:44:54 +000036#include <stdlib.h>
Dieter Baronb2ed74d2004-04-14 14:01:31 +000037#include <string.h>
Dieter Baronb2ed74d2004-04-14 14:01:31 +000038#include <sys/stat.h>
Thomas Klausner8eab1a22018-01-15 14:10:11 +010039#include <sys/types.h>
Dieter Baronb2ed74d2004-04-14 14:01:31 +000040
Dieter Baronb2ed74d2004-04-14 14:01:31 +000041#include "zipint.h"
Thomas Klausneraf93f081999-07-26 01:39:56 +000042
Dieter Baronb2ed74d2004-04-14 14:01:31 +000043
44/* _zip_file_get_offset(za, ze):
45 Returns the offset of the file data for entry ze.
46
47 On error, fills in za->error and returns 0.
48*/
49
Dieter Baronc61eb212012-03-15 12:07:04 +010050zip_uint64_t
Thomas Klausner8eab1a22018-01-15 14:10:11 +010051_zip_file_get_offset(const zip_t *za, zip_uint64_t idx, zip_error_t *error) {
Dieter Baron0e5eeab2012-04-24 18:47:12 +020052 zip_uint64_t offset;
53 zip_int32_t size;
Thomas Klausneraf93f081999-07-26 01:39:56 +000054
Dieter Baron5caa9c92017-12-11 16:50:12 +010055 if (za->entry[idx].orig == NULL) {
Thomas Klausner8eab1a22018-01-15 14:10:11 +010056 zip_error_set(error, ZIP_ER_INTERNAL, 0);
57 return 0;
Dieter Baron5caa9c92017-12-11 16:50:12 +010058 }
59
Dieter Baron0e5eeab2012-04-24 18:47:12 +020060 offset = za->entry[idx].orig->offset;
Dieter Baronb2ed74d2004-04-14 14:01:31 +000061
Thomas Klausnerea8ba492014-09-23 16:54:47 +020062 if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) {
Thomas Klausnerd97f5442014-09-23 17:02:03 +020063 _zip_error_set_from_source(error, za->src);
Dieter Baronb2ed74d2004-04-14 14:01:31 +000064 return 0;
65 }
66
Thomas Klausnerb52bda02013-11-28 18:01:40 +010067 /* TODO: cache? */
Thomas Klausner8eab1a22018-01-15 14:10:11 +010068 if ((size = _zip_dirent_size(za->src, ZIP_EF_LOCAL, error)) < 0)
Dieter Baronb2ed74d2004-04-14 14:01:31 +000069 return 0;
70
Thomas Klausner8eab1a22018-01-15 14:10:11 +010071 if (offset + (zip_uint32_t)size > ZIP_INT64_MAX) {
72 zip_error_set(error, ZIP_ER_SEEK, EFBIG);
73 return 0;
Dieter Baron3ed41e92012-10-12 15:53:18 +020074 }
Thomas Klausner8eab1a22018-01-15 14:10:11 +010075
Dieter Baronabc6fd72012-07-22 15:49:45 +020076 return offset + (zip_uint32_t)size;
Thomas Klausneraf93f081999-07-26 01:39:56 +000077}
Dieter Baron5caa9c92017-12-11 16:50:12 +010078
79zip_uint64_t
Thomas Klausner8eab1a22018-01-15 14:10:11 +010080_zip_file_get_end(const zip_t *za, zip_uint64_t index, zip_error_t *error) {
Dieter Baron5caa9c92017-12-11 16:50:12 +010081 zip_uint64_t offset;
82 zip_dirent_t *entry;
83
84 if ((offset = _zip_file_get_offset(za, index, error)) == 0) {
Thomas Klausner8eab1a22018-01-15 14:10:11 +010085 return 0;
Dieter Baron5caa9c92017-12-11 16:50:12 +010086 }
87
88 entry = za->entry[index].orig;
89
90 if (offset + entry->comp_size < offset || offset + entry->comp_size > ZIP_INT64_MAX) {
Thomas Klausner8eab1a22018-01-15 14:10:11 +010091 zip_error_set(error, ZIP_ER_SEEK, EFBIG);
92 return 0;
Dieter Baron5caa9c92017-12-11 16:50:12 +010093 }
94 offset += entry->comp_size;
95
96 if (entry->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
Thomas Klausner8eab1a22018-01-15 14:10:11 +010097 zip_uint8_t buf[4];
98 if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) {
99 _zip_error_set_from_source(error, za->src);
100 return 0;
101 }
102 if (zip_source_read(za->src, buf, 4) != 4) {
103 _zip_error_set_from_source(error, za->src);
104 return 0;
105 }
106 if (memcmp(buf, DATADES_MAGIC, 4) == 0) {
107 offset += 4;
108 }
109 offset += 12;
110 if (_zip_dirent_needs_zip64(entry, 0)) {
111 offset += 8;
112 }
113 if (offset > ZIP_INT64_MAX) {
114 zip_error_set(error, ZIP_ER_SEEK, EFBIG);
115 return 0;
116 }
Dieter Baron5caa9c92017-12-11 16:50:12 +0100117 }
118
119 return offset;
120}