1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Handling of Index in Metadata
6 // Copyright (C) 2007 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
24 * \brief Duplicates an Index list
26 * \return A copy of the Index list, or NULL if memory allocation
27 * failed or the original Index was empty.
29 extern LZMA_API lzma_index *
30 lzma_index_dup(const lzma_index *old_current, lzma_allocator *allocator)
32 lzma_index *new_head = NULL;
33 lzma_index *new_current = NULL;
35 while (old_current != NULL) {
36 lzma_index *i = lzma_alloc(sizeof(lzma_index), allocator);
38 lzma_index_free(new_head, allocator);
42 i->total_size = old_current->total_size;
43 i->uncompressed_size = old_current->uncompressed_size;
49 new_current->next = i;
52 old_current = old_current->next;
60 * \brief Frees an Index list
62 * All Index Recors in the list are freed. This function is convenient when
63 * getting rid of lzma_metadata structures containing an Index.
66 lzma_index_free(lzma_index *i, lzma_allocator *allocator)
69 lzma_index *tmp = i->next;
70 lzma_free(i, allocator);
79 * \brief Calculates properties of an Index list
83 extern LZMA_API lzma_ret
84 lzma_index_count(const lzma_index *i, size_t *count,
85 lzma_vli *lzma_restrict total_size,
86 lzma_vli *lzma_restrict uncompressed_size)
90 *uncompressed_size = 0;
93 if (i->total_size == LZMA_VLI_VALUE_UNKNOWN) {
94 *total_size = LZMA_VLI_VALUE_UNKNOWN;
95 } else if (i->total_size > LZMA_VLI_VALUE_MAX) {
96 return LZMA_PROG_ERROR;
97 } else if (*total_size != LZMA_VLI_VALUE_UNKNOWN) {
98 *total_size += i->total_size;
99 if (*total_size > LZMA_VLI_VALUE_MAX)
100 return LZMA_PROG_ERROR;
103 if (i->uncompressed_size == LZMA_VLI_VALUE_UNKNOWN) {
104 *uncompressed_size = LZMA_VLI_VALUE_UNKNOWN;
105 } else if (i->uncompressed_size > LZMA_VLI_VALUE_MAX) {
106 return LZMA_PROG_ERROR;
107 } else if (*uncompressed_size != LZMA_VLI_VALUE_UNKNOWN) {
108 *uncompressed_size += i->uncompressed_size;
109 if (*uncompressed_size > LZMA_VLI_VALUE_MAX)
110 return LZMA_PROG_ERROR;
118 if (*total_size == LZMA_VLI_VALUE_UNKNOWN
119 || *uncompressed_size == LZMA_VLI_VALUE_UNKNOWN)
120 return LZMA_HEADER_ERROR;
127 extern LZMA_API lzma_bool
128 lzma_index_is_equal(const lzma_index *a, const lzma_index *b)
130 while (a != NULL && b != NULL) {
131 if (a->total_size != b->total_size || a->uncompressed_size
132 != b->uncompressed_size)