"examples/nas/legacy/pdarts/search.py" did not exist on "a9b87c9ad3cc1516869884530853c339a394dc52"
lznt1.c 4.39 KB
Newer Older
longpanda's avatar
longpanda committed
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
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.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 of the
 * License, 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * @file
 *
 * LZNT1 decompression
 *
 */

#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "wimboot.h"
#include "lznt1.h"

/**
 * Decompress LZNT1-compressed data block
 *
 * @v data		Compressed data
 * @v limit		Length of compressed data up to end of block
 * @v offset		Starting offset within compressed data
 * @v block		Decompression buffer for this block, or NULL
 * @ret out_len		Length of decompressed block, or negative error
 */
static ssize_t lznt1_block ( const void *data, size_t limit, size_t offset,
			     void *block ) {
	const uint16_t *tuple;
	const uint8_t *copy_src;
	uint8_t *copy_dest = block;
	size_t copy_len;
	size_t block_out_len = 0;
	unsigned int split = 12;
	unsigned int next_threshold = 16;
	unsigned int tag_bit = 0;
	unsigned int tag = 0;

	while ( offset != limit ) {

		/* Extract tag */
		if ( tag_bit == 0 ) {
			tag = *( ( uint8_t * ) ( data + offset ) );
			offset++;
			if ( offset == limit )
				break;
		}

		/* Calculate copy source and length */
		if ( tag & 1 ) {

			/* Compressed value */
			if ( offset + sizeof ( *tuple ) > limit ) {
				DBG ( "LZNT1 compressed value overrun at "
				      "%#zx\n", offset );
				return -1;
			}
			tuple = ( data + offset );
			offset += sizeof ( *tuple );
			copy_len = LZNT1_VALUE_LEN ( *tuple, split );
			block_out_len += copy_len;
			if ( copy_dest ) {
				copy_src = ( copy_dest -
					     LZNT1_VALUE_OFFSET ( *tuple,
								  split ) );
				while ( copy_len-- )
					*(copy_dest++) = *(copy_src++);
			}

		} else {

			/* Uncompressed value */
			copy_src = ( data + offset );
			if ( copy_dest )
				*(copy_dest++) = *copy_src;
			offset++;
			block_out_len++;
		}

		/* Update split, if applicable */
		while ( block_out_len > next_threshold ) {
			split--;
			next_threshold <<= 1;
		}

		/* Move to next value */
		tag >>= 1;
		tag_bit = ( ( tag_bit + 1 ) % 8 );
	}

	return block_out_len;
}

/**
 * Decompress LZNT1-compressed data
 *
 * @v data		Compressed data
 * @v len		Length of compressed data
 * @v buf		Decompression buffer, or NULL
 * @ret out_len		Length of decompressed data, or negative error
 */
ssize_t lznt1_decompress ( const void *data, size_t len, void *buf ) {
	const uint16_t *header;
	const uint8_t *end;
	size_t offset = 0;
	ssize_t out_len = 0;
	size_t block_len;
	size_t limit;
	void *block;
	ssize_t block_out_len;

	while ( offset != len ) {

		/* Check for end marker */
		if ( ( offset + sizeof ( *end ) ) == len ) {
			end = ( data + offset );
			if ( *end == 0 )
				break;
		}

		/* Extract block header */
		if ( ( offset + sizeof ( *header ) ) > len ) {
			DBG ( "LZNT1 block header overrun at %#zx\n", offset );
			return -1;
		}
		header = ( data + offset );
		offset += sizeof ( *header );

		/* Process block */
		block_len = LZNT1_BLOCK_LEN ( *header );
		if ( LZNT1_BLOCK_COMPRESSED ( *header ) ) {

			/* Compressed block */
			DBG2 ( "LZNT1 compressed block %#zx+%#zx\n",
			       offset, block_len );
			limit = ( offset + block_len );
			block = ( buf ? ( buf + out_len ) : NULL );
			block_out_len = lznt1_block ( data, limit, offset,
						      block );
			if ( block_out_len < 0 )
				return block_out_len;
			offset += block_len;
			out_len += block_out_len;

		} else {

			/* Uncompressed block */
			if ( ( offset + block_len ) > len ) {
				DBG ( "LZNT1 uncompressed block overrun at "
				      "%#zx+%#zx\n", offset, block_len );
				return -1;
			}
			DBG2 ( "LZNT1 uncompressed block %#zx+%#zx\n",
			       offset, block_len );
			if ( buf ) {
				memcpy ( ( buf + out_len ), ( data + offset ),
					 block_len );
			}
			offset += block_len;
			out_len += block_len;
		}
	}

	return out_len;
}