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_buffer.c
Views: 1401
/*1zip_buffer.c -- bounds checked access to memory buffer2Copyright (C) 2014-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*/3233#include <stdlib.h>34#include <string.h>3536#include "zipint.h"3738zip_uint8_t *39_zip_buffer_data(zip_buffer_t *buffer) {40return buffer->data;41}424344void45_zip_buffer_free(zip_buffer_t *buffer) {46if (buffer == NULL) {47return;48}4950if (buffer->free_data) {51free(buffer->data);52}5354free(buffer);55}565758bool59_zip_buffer_eof(zip_buffer_t *buffer) {60return buffer->ok && buffer->offset == buffer->size;61}626364zip_uint8_t *65_zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length) {66zip_uint8_t *data;6768data = _zip_buffer_peek(buffer, length);6970if (data != NULL) {71buffer->offset += length;72}7374return data;75}767778zip_uint16_t79_zip_buffer_get_16(zip_buffer_t *buffer) {80zip_uint8_t *data = _zip_buffer_get(buffer, 2);8182if (data == NULL) {83return 0;84}8586return (zip_uint16_t)(data[0] + (data[1] << 8));87}888990zip_uint32_t91_zip_buffer_get_32(zip_buffer_t *buffer) {92zip_uint8_t *data = _zip_buffer_get(buffer, 4);9394if (data == NULL) {95return 0;96}9798return ((((((zip_uint32_t)data[3] << 8) + data[2]) << 8) + data[1]) << 8) + data[0];99}100101102zip_uint64_t103_zip_buffer_get_64(zip_buffer_t *buffer) {104zip_uint8_t *data = _zip_buffer_get(buffer, 8);105106if (data == NULL) {107return 0;108}109110return ((zip_uint64_t)data[7] << 56) + ((zip_uint64_t)data[6] << 48) + ((zip_uint64_t)data[5] << 40) + ((zip_uint64_t)data[4] << 32) + ((zip_uint64_t)data[3] << 24) + ((zip_uint64_t)data[2] << 16) + ((zip_uint64_t)data[1] << 8) + (zip_uint64_t)data[0];111}112113114zip_uint8_t115_zip_buffer_get_8(zip_buffer_t *buffer) {116zip_uint8_t *data = _zip_buffer_get(buffer, 1);117118if (data == NULL) {119return 0;120}121122return data[0];123}124125126zip_uint64_t127_zip_buffer_left(zip_buffer_t *buffer) {128return buffer->ok ? buffer->size - buffer->offset : 0;129}130131132zip_uint64_t133_zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) {134if (_zip_buffer_left(buffer) < length) {135length = _zip_buffer_left(buffer);136}137138memcpy(data, _zip_buffer_get(buffer, length), length);139140return length;141}142143144zip_buffer_t *145_zip_buffer_new(zip_uint8_t *data, zip_uint64_t size) {146bool free_data = (data == NULL);147zip_buffer_t *buffer;148149if (data == NULL) {150if ((data = (zip_uint8_t *)malloc(size)) == NULL) {151return NULL;152}153}154155if ((buffer = (zip_buffer_t *)malloc(sizeof(*buffer))) == NULL) {156if (free_data) {157free(data);158}159return NULL;160}161162buffer->ok = true;163buffer->data = data;164buffer->size = size;165buffer->offset = 0;166buffer->free_data = free_data;167168return buffer;169}170171172zip_buffer_t *173_zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error) {174zip_buffer_t *buffer;175176if ((buffer = _zip_buffer_new(buf, size)) == NULL) {177zip_error_set(error, ZIP_ER_MEMORY, 0);178return NULL;179}180181if (_zip_read(src, buffer->data, size, error) < 0) {182_zip_buffer_free(buffer);183return NULL;184}185186return buffer;187}188189190zip_uint64_t191_zip_buffer_offset(zip_buffer_t *buffer) {192return buffer->ok ? buffer->offset : 0;193}194195196bool197_zip_buffer_ok(zip_buffer_t *buffer) {198return buffer->ok;199}200201202zip_uint8_t *203_zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length) {204zip_uint8_t *data;205206if (!buffer->ok || buffer->offset + length < length || buffer->offset + length > buffer->size) {207buffer->ok = false;208return NULL;209}210211data = buffer->data + buffer->offset;212return data;213}214215int216_zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length) {217zip_uint8_t *dst = _zip_buffer_get(buffer, length);218219if (dst == NULL) {220return -1;221}222223memcpy(dst, src, length);224return 0;225}226227228int229_zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i) {230zip_uint8_t *data = _zip_buffer_get(buffer, 2);231232if (data == NULL) {233return -1;234}235236data[0] = (zip_uint8_t)(i & 0xff);237data[1] = (zip_uint8_t)((i >> 8) & 0xff);238239return 0;240}241242243int244_zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i) {245zip_uint8_t *data = _zip_buffer_get(buffer, 4);246247if (data == NULL) {248return -1;249}250251data[0] = (zip_uint8_t)(i & 0xff);252data[1] = (zip_uint8_t)((i >> 8) & 0xff);253data[2] = (zip_uint8_t)((i >> 16) & 0xff);254data[3] = (zip_uint8_t)((i >> 24) & 0xff);255256return 0;257}258259260int261_zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i) {262zip_uint8_t *data = _zip_buffer_get(buffer, 8);263264if (data == NULL) {265return -1;266}267268data[0] = (zip_uint8_t)(i & 0xff);269data[1] = (zip_uint8_t)((i >> 8) & 0xff);270data[2] = (zip_uint8_t)((i >> 16) & 0xff);271data[3] = (zip_uint8_t)((i >> 24) & 0xff);272data[4] = (zip_uint8_t)((i >> 32) & 0xff);273data[5] = (zip_uint8_t)((i >> 40) & 0xff);274data[6] = (zip_uint8_t)((i >> 48) & 0xff);275data[7] = (zip_uint8_t)((i >> 56) & 0xff);276277return 0;278}279280281int282_zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i) {283zip_uint8_t *data = _zip_buffer_get(buffer, 1);284285if (data == NULL) {286return -1;287}288289data[0] = i;290291return 0;292}293294295int296_zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset) {297if (offset > buffer->size) {298buffer->ok = false;299return -1;300}301302buffer->ok = true;303buffer->offset = offset;304305return 0;306}307308309int310_zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length) {311zip_uint64_t offset = buffer->offset + length;312313if (offset < buffer->offset) {314buffer->ok = false;315return -1;316}317return _zip_buffer_set_offset(buffer, offset);318}319320zip_uint64_t321_zip_buffer_size(zip_buffer_t *buffer) {322return buffer->size;323}324325326