Path: blob/master/Utilities/cmlibarchive/libarchive/archive_entry_sparse.c
3153 views
/*-1* Copyright (c) 2003-2007 Tim Kientzle2* Copyright (c) 2010-2011 Michihiro NAKAJIMA3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include "archive_platform.h"2728#include "archive.h"29#include "archive_entry.h"30#include "archive_private.h"31#include "archive_entry_private.h"3233/*34* sparse handling35*/3637void38archive_entry_sparse_clear(struct archive_entry *entry)39{40struct ae_sparse *sp;4142while (entry->sparse_head != NULL) {43sp = entry->sparse_head->next;44free(entry->sparse_head);45entry->sparse_head = sp;46}47entry->sparse_tail = NULL;48}4950void51archive_entry_sparse_add_entry(struct archive_entry *entry,52la_int64_t offset, la_int64_t length)53{54struct ae_sparse *sp;5556if (offset < 0 || length < 0)57/* Invalid value */58return;59if (offset > INT64_MAX - length ||60offset + length > archive_entry_size(entry))61/* A value of "length" parameter is too large. */62return;63if ((sp = entry->sparse_tail) != NULL) {64if (sp->offset + sp->length > offset)65/* Invalid value. */66return;67if (sp->offset + sp->length == offset) {68if (sp->offset + sp->length + length < 0)69/* A value of "length" parameter is70* too large. */71return;72/* Expand existing sparse block size. */73sp->length += length;74return;75}76}7778if ((sp = malloc(sizeof(*sp))) == NULL)79/* XXX Error XXX */80return;8182sp->offset = offset;83sp->length = length;84sp->next = NULL;8586if (entry->sparse_head == NULL)87entry->sparse_head = entry->sparse_tail = sp;88else {89/* Add a new sparse block to the tail of list. */90if (entry->sparse_tail != NULL)91entry->sparse_tail->next = sp;92entry->sparse_tail = sp;93}94}959697/*98* returns number of the sparse entries99*/100int101archive_entry_sparse_count(struct archive_entry *entry)102{103struct ae_sparse *sp;104int count = 0;105106for (sp = entry->sparse_head; sp != NULL; sp = sp->next)107count++;108109/*110* Sanity check if this entry is exactly sparse.111* If amount of sparse blocks is just one and it indicates the whole112* file data, we should remove it and return zero.113*/114if (count == 1) {115sp = entry->sparse_head;116if (sp->offset == 0 &&117sp->length >= archive_entry_size(entry)) {118count = 0;119archive_entry_sparse_clear(entry);120}121}122123return (count);124}125126int127archive_entry_sparse_reset(struct archive_entry * entry)128{129entry->sparse_p = entry->sparse_head;130131return archive_entry_sparse_count(entry);132}133134int135archive_entry_sparse_next(struct archive_entry * entry,136la_int64_t *offset, la_int64_t *length)137{138if (entry->sparse_p) {139*offset = entry->sparse_p->offset;140*length = entry->sparse_p->length;141142entry->sparse_p = entry->sparse_p->next;143144return (ARCHIVE_OK);145} else {146*offset = 0;147*length = 0;148return (ARCHIVE_WARN);149}150}151152/*153* end of sparse handling154*/155156157