CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libzip/zip_file_get_offset.c
Views: 1401
/*1zip_file_get_offset.c -- get offset of file data in archive.2Copyright (C) 1999-2019 Dieter Baron and Thomas Klausner34This file is part of libzip, a library to manipulate ZIP archives.5The authors can be contacted at <[email protected]>67Redistribution and use in source and binary forms, with or without8modification, are permitted provided that the following conditions9are met:101. Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.122. Redistributions in binary form must reproduce the above copyright13notice, this list of conditions and the following disclaimer in14the documentation and/or other materials provided with the15distribution.163. The names of the authors may not be used to endorse or promote17products derived from this software without specific prior18written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS21OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY24DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE26GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER28IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR29OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/323334#include <stdio.h>35#include <string.h>3637#include "zipint.h"383940/* _zip_file_get_offset(za, ze):41Returns the offset of the file data for entry ze.4243On error, fills in za->error and returns 0.44*/4546zip_uint64_t47_zip_file_get_offset(const zip_t *za, zip_uint64_t idx, zip_error_t *error) {48zip_uint64_t offset;49zip_int32_t size;5051if (za->entry[idx].orig == NULL) {52zip_error_set(error, ZIP_ER_INTERNAL, 0);53return 0;54}5556offset = za->entry[idx].orig->offset;5758if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) {59_zip_error_set_from_source(error, za->src);60return 0;61}6263/* TODO: cache? */64if ((size = _zip_dirent_size(za->src, ZIP_EF_LOCAL, error)) < 0)65return 0;6667if (offset + (zip_uint32_t)size > ZIP_INT64_MAX) {68zip_error_set(error, ZIP_ER_SEEK, EFBIG);69return 0;70}7172return offset + (zip_uint32_t)size;73}7475zip_uint64_t76_zip_file_get_end(const zip_t *za, zip_uint64_t index, zip_error_t *error) {77zip_uint64_t offset;78zip_dirent_t *entry;7980if ((offset = _zip_file_get_offset(za, index, error)) == 0) {81return 0;82}8384entry = za->entry[index].orig;8586if (offset + entry->comp_size < offset || offset + entry->comp_size > ZIP_INT64_MAX) {87zip_error_set(error, ZIP_ER_SEEK, EFBIG);88return 0;89}90offset += entry->comp_size;9192if (entry->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {93zip_uint8_t buf[4];94if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) {95_zip_error_set_from_source(error, za->src);96return 0;97}98if (zip_source_read(za->src, buf, 4) != 4) {99_zip_error_set_from_source(error, za->src);100return 0;101}102if (memcmp(buf, DATADES_MAGIC, 4) == 0) {103offset += 4;104}105offset += 12;106if (_zip_dirent_needs_zip64(entry, 0)) {107offset += 8;108}109if (offset > ZIP_INT64_MAX) {110zip_error_set(error, ZIP_ER_SEEK, EFBIG);111return 0;112}113}114115return offset;116}117118119