summaryrefslogtreecommitdiffstats
path: root/squashfs-tools/unsquash-34.c
blob: 59d28f1bb62ca6efdada5eaec376243b8ff7033d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Unsquash a squashfs filesystem.  This is a highly compressed read only
 * filesystem.
 *
 * Copyright (c) 2019, 2022, 2023
 * Phillip Lougher <phillip@squashfs.org.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * unsquash-34.c
 *
 * Helper functions used by unsquash-3 and unsquash-4.
 */

#include "unsquashfs.h"

static unsigned int **inumber_table = NULL;
static char ***lookup_table = NULL;

long long *alloc_index_table(int indexes)
{
	static long long *alloc_table = NULL;
	static int alloc_size = 0;
	int length = indexes * sizeof(long long);

	if(alloc_size < length || length == 0) {
		long long *table = realloc(alloc_table, length);

		if(table == NULL && length !=0)
			MEM_ERROR();

		alloc_table = table;
		alloc_size = length;
	}

	return alloc_table;
}


/* These functions implement a bit-table to track whether directories have been
 * already visited.  This is to trap corrupted filesystems which have multiple
 * links to the same directory, which is invalid, and which may also create
 * a directory loop, where Unsquashfs will endlessly recurse until either
 * the pathname is too large (extracting), or the stack overflows.
 *
 * Each index entry is 8 Kbytes, and tracks 65536 inode numbers.  The index is
 * allocated on demand because Unsquashfs may not walk the complete filesystem.
 */
static void create_inumber_table()
{
	int indexes = INUMBER_INDEXES(sBlk.s.inodes);

	inumber_table = malloc(indexes * sizeof(unsigned int *));
	if(inumber_table == NULL)
		MEM_ERROR();
	memset(inumber_table, 0, indexes * sizeof(unsigned int *));
}


int inumber_lookup(unsigned int number)
{
	int index = INUMBER_INDEX(number - 1);
	int offset = INUMBER_OFFSET(number - 1);
	int bit = INUMBER_BIT(number - 1);

	if(inumber_table == NULL)
		create_inumber_table();

	/* Lookup number in the bit table */
	if(inumber_table[index] && (inumber_table[index][offset] & bit))
		return TRUE;

	if(inumber_table[index] == NULL) {
		inumber_table[index] = malloc(INUMBER_BYTES);
		if(inumber_table[index] == NULL)
			MEM_ERROR();
		memset(inumber_table[index], 0, INUMBER_BYTES);
	}

	inumber_table[index][offset] |= bit;
	return FALSE;
}


void free_inumber_table()
{
	int i, indexes = INUMBER_INDEXES(sBlk.s.inodes);

	if(inumber_table) {
		for(i = 0; i < indexes; i++)
			if(inumber_table[i])
				free(inumber_table[i]);
		free(inumber_table);
		inumber_table = NULL;
	}
}


/* These functions implement a lookup table to track creation of (non-directory)
 * inodes, and to discover if a hard-link to a previously created file should
 * be made.
 *
 * Each index entry is 32 Kbytes, and tracks 4096 inode numbers.  The index is
 * allocated on demand because Unsquashfs may not walk the complete filesystem.
 */
static void create_lookup_table()
{
	int indexes = LOOKUP_INDEXES(sBlk.s.inodes);

	lookup_table = malloc(indexes * sizeof(char *));
	if(lookup_table == NULL)
		MEM_ERROR();
	memset(lookup_table, 0, indexes * sizeof(char *));
}


char *lookup(unsigned int number)
{
	int index = LOOKUP_INDEX(number - 1);
	int offset = LOOKUP_OFFSET(number - 1);

	if(lookup_table == NULL)
		create_lookup_table();

	/* Lookup number in table */
	if(lookup_table[index] == NULL)
		return NULL;

	return lookup_table[index][offset];
}


void insert_lookup(unsigned int number, char *pathname)
{
	int index = LOOKUP_INDEX(number - 1);
	int offset = LOOKUP_OFFSET(number - 1);

	if(lookup_table == NULL)
		create_lookup_table();

	if(lookup_table[index] == NULL) {
		lookup_table[index] = malloc(LOOKUP_BYTES);
		if(lookup_table[index] == NULL)
			MEM_ERROR();
		memset(lookup_table[index], 0, LOOKUP_BYTES);
	}

	lookup_table[index][offset] = pathname;
}


void free_lookup_table(int free_pathname)
{
	int i, indexes = LOOKUP_INDEXES(sBlk.s.inodes);

	if(lookup_table) {
		for(i = 0; i < indexes; i++)
			if(lookup_table[i]) {
				if(free_pathname) {
					int j;

					for(j = 0; j < LOOKUP_OFFSETS; j++)
						if(lookup_table[i][j])
							free(lookup_table[i][j]);
				}
				free(lookup_table[i]);
			}
		free(lookup_table);
		lookup_table = NULL;
	}
}