wok rev 3908

linux: no historic allocation for initramfs unlzma
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Aug 16 20:44:30 2009 +0200 (2009-08-16)
parents 9a3ad6f0607b
children 1ef7110e0997
files linux/receipt linux/stuff/linux-unlzma-2.6.30.4.u
line diff
     1.1 --- a/linux/receipt	Sun Aug 16 19:13:58 2009 +0000
     1.2 +++ b/linux/receipt	Sun Aug 16 20:44:30 2009 +0200
     1.3 @@ -35,6 +35,7 @@
     1.4  $PACKAGE-utf8-$VERSION.u
     1.5  $PACKAGE-diff-$VERSION.u
     1.6  $PACKAGE-freeinitrd-$VERSION.u
     1.7 +$PACKAGE-unlzma-$VERSION.u
     1.8  EOT
     1.9  	make mrproper
    1.10  	cp ../stuff/$PACKAGE-$VERSION-slitaz.config .config
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/linux/stuff/linux-unlzma-2.6.30.4.u	Sun Aug 16 20:44:30 2009 +0200
     2.3 @@ -0,0 +1,222 @@
     2.4 +--- linux-2.6.30.4/init/initramfs.c
     2.5 ++++ linux-2.6.30.4/init/initramfs.c
     2.6 +@@ -425,7 +425,8 @@
     2.7 + 	return len - count;
     2.8 + }
     2.9 + 
    2.10 +-static int __init flush_buffer(void *bufv, unsigned len)
    2.11 +#define flush_buffer cpio_flush_buffer
    2.12 ++int __init flush_buffer(void *bufv, unsigned len)
    2.13 + {
    2.14 + 	char *buf = (char *) bufv;
    2.15 + 	int written;
    2.16 +
    2.17 +--- linux-2.6.30.4/lib/decompress_unlzma.c
    2.18 ++++ linux-2.6.30.4/lib/decompress_unlzma.c
    2.19 +@@ -278,6 +278,10 @@
    2.20 + 	size_t global_pos;
    2.21 + 	int(*flush)(void*, unsigned int);
    2.22 + 	struct lzma_header *header;
    2.23 ++	int is_cpio_flush;
    2.24 ++	uint8_t **buffer_index;
    2.25 ++	int next_index;
    2.26 ++	int max_index;
    2.27 + };
    2.28 + 
    2.29 + struct cstate {
    2.30 +@@ -294,6 +298,14 @@
    2.31 + static inline uint8_t INIT peek_old_byte(struct writer *wr,
    2.32 + 						uint32_t offs)
    2.33 + {
    2.34 ++	if (wr->is_cpio_flush) {
    2.35 ++		int32_t pos;
    2.36 ++		while (offs > wr->header->dict_size)
    2.37 ++			offs -= wr->header->dict_size;
    2.38 ++		pos = wr->buffer_pos - offs;
    2.39 ++		return wr->buffer_index[pos / LZMA_IOBUF_SIZE]
    2.40 ++				       [pos % LZMA_IOBUF_SIZE];
    2.41 ++	}
    2.42 + 	if (!wr->flush) {
    2.43 + 		int32_t pos;
    2.44 + 		while (offs > wr->header->dict_size)
    2.45 +@@ -311,6 +323,34 @@
    2.46 + 
    2.47 + static inline void INIT write_byte(struct writer *wr, uint8_t byte)
    2.48 + {
    2.49 ++	if (wr->is_cpio_flush) {
    2.50 ++		if (wr->buffer_pos % LZMA_IOBUF_SIZE == 0) {
    2.51 ++			// if the following large_malloc fails, the initramfs
    2.52 ++			// whould not be load with is_cpio_flush forced 0 too.
    2.53 ++			// Remember we do not allocate historic buffer.
    2.54 ++			// Let's assume it will never fail !
    2.55 ++			if (wr->next_index >= wr->max_index) {
    2.56 ++				// realloc wr->buffer_index
    2.57 ++				uint8_t **p = wr->buffer_index;
    2.58 ++				wr->buffer_index = (uint8_t **) 
    2.59 ++					large_malloc(LZMA_IOBUF_SIZE + 
    2.60 ++						    sizeof(*p) * wr->max_index);
    2.61 ++				if (wr->max_index) {
    2.62 ++					memcpy(wr->buffer_index, p,
    2.63 ++					       sizeof(*p) * wr->max_index);
    2.64 ++					free(p);
    2.65 ++				}
    2.66 ++				wr->max_index += LZMA_IOBUF_SIZE / sizeof(*p);
    2.67 ++			}
    2.68 ++			wr->buffer_index[wr->next_index++] =
    2.69 ++				(uint8_t *) large_malloc(LZMA_IOBUF_SIZE);
    2.70 ++		}
    2.71 ++		wr->buffer_index[wr->buffer_pos / LZMA_IOBUF_SIZE]
    2.72 ++				[wr->buffer_pos % LZMA_IOBUF_SIZE] =
    2.73 ++			wr->previous_byte = byte;
    2.74 ++		wr->buffer_pos++;
    2.75 ++		return;
    2.76 ++	}
    2.77 + 	wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte;
    2.78 + 	if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
    2.79 + 		wr->buffer_pos = 0;
    2.80 +@@ -339,6 +379,9 @@
    2.81 + 				     int pos_state, uint16_t *prob,
    2.82 + 				     int lc, uint32_t literal_pos_mask) {
    2.83 + 	int mi = 1;
    2.84 ++	static const int state[LZMA_NUM_STATES] = 
    2.85 ++		{ 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
    2.86 ++
    2.87 + 	rc_update_bit_0(rc, prob);
    2.88 + 	prob = (p + LZMA_LITERAL +
    2.89 + 		(LZMA_LIT_SIZE
    2.90 +@@ -369,18 +412,13 @@
    2.91 + 		rc_get_bit(rc, prob_lit, &mi);
    2.92 + 	}
    2.93 + 	write_byte(wr, mi);
    2.94 +-	if (cst->state < 4)
    2.95 +-		cst->state = 0;
    2.96 +-	else if (cst->state < 10)
    2.97 +-		cst->state -= 3;
    2.98 +-	else
    2.99 +-		cst->state -= 6;
   2.100 ++	cst->state = state[cst->state];
   2.101 + }
   2.102 + 
   2.103 + static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
   2.104 + 					    struct cstate *cst, uint16_t *p,
   2.105 + 					    int pos_state, uint16_t *prob) {
   2.106 +-  int offset;
   2.107 ++	int offset;
   2.108 + 	uint16_t *prob_len;
   2.109 + 	int num_bits;
   2.110 + 	int len;
   2.111 +@@ -396,7 +434,7 @@
   2.112 + 		prob = p + LZMA_LEN_CODER;
   2.113 + 	} else {
   2.114 + 		rc_update_bit_1(rc, prob);
   2.115 +-		prob = p + LZMA_IS_REP_G0 + cst->state;
   2.116 ++		prob += LZMA_IS_REP_G0 - LZMA_IS_REP;
   2.117 + 		if (rc_is_bit_0(rc, prob)) {
   2.118 + 			rc_update_bit_0(rc, prob);
   2.119 + 			prob = (p + LZMA_IS_REP_0_LONG
   2.120 +@@ -417,13 +455,13 @@
   2.121 + 			uint32_t distance;
   2.122 + 
   2.123 + 			rc_update_bit_1(rc, prob);
   2.124 +-			prob = p + LZMA_IS_REP_G1 + cst->state;
   2.125 ++			prob += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
   2.126 + 			if (rc_is_bit_0(rc, prob)) {
   2.127 + 				rc_update_bit_0(rc, prob);
   2.128 + 				distance = cst->rep1;
   2.129 + 			} else {
   2.130 + 				rc_update_bit_1(rc, prob);
   2.131 +-				prob = p + LZMA_IS_REP_G2 + cst->state;
   2.132 ++				prob += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
   2.133 + 				if (rc_is_bit_0(rc, prob)) {
   2.134 + 					rc_update_bit_0(rc, prob);
   2.135 + 					distance = cst->rep2;
   2.136 +@@ -451,7 +489,7 @@
   2.137 + 		num_bits = LZMA_LEN_NUM_LOW_BITS;
   2.138 + 	} else {
   2.139 + 		rc_update_bit_1(rc, prob_len);
   2.140 +-		prob_len = prob + LZMA_LEN_CHOICE_2;
   2.141 ++		prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
   2.142 + 		if (rc_is_bit_0(rc, prob_len)) {
   2.143 + 			rc_update_bit_0(rc, prob_len);
   2.144 + 			prob_len = (prob + LZMA_LEN_MID
   2.145 +@@ -461,7 +499,7 @@
   2.146 + 			num_bits = LZMA_LEN_NUM_MID_BITS;
   2.147 + 		} else {
   2.148 + 			rc_update_bit_1(rc, prob_len);
   2.149 +-			prob_len = prob + LZMA_LEN_HIGH;
   2.150 ++			prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
   2.151 + 			offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
   2.152 + 				  + (1 << LZMA_LEN_NUM_MID_BITS));
   2.153 + 			num_bits = LZMA_LEN_NUM_HIGH_BITS;
   2.154 +@@ -529,6 +567,7 @@
   2.155 + 			      void(*error_fn)(char *x)
   2.156 + 	)
   2.157 + {
   2.158 ++	extern int cpio_flush_buffer(void*, unsigned int);
   2.159 + 	struct lzma_header header;
   2.160 + 	int lc, pb, lp;
   2.161 + 	uint32_t pos_state_mask;
   2.162 +@@ -563,6 +602,10 @@
   2.163 + 	wr.global_pos = 0;
   2.164 + 	wr.previous_byte = 0;
   2.165 + 	wr.buffer_pos = 0;
   2.166 ++	wr.is_cpio_flush = 0;
   2.167 ++	if (flush == cpio_flush_buffer)
   2.168 ++		wr.is_cpio_flush = 1;
   2.169 ++	wr.buffer_index = NULL;
   2.170 + 
   2.171 + 	rc_init(&rc, fill, inbuf, in_len);
   2.172 + 
   2.173 +@@ -596,13 +639,13 @@
   2.174 + 	if (header.dict_size == 0)
   2.175 + 		header.dict_size = 1;
   2.176 + 
   2.177 +-	if (output)
   2.178 ++	if (output || wr.is_cpio_flush)
   2.179 + 		wr.buffer = output;
   2.180 + 	else {
   2.181 + 		wr.bufsize = MIN(header.dst_size, header.dict_size);
   2.182 + 		wr.buffer = large_malloc(wr.bufsize);
   2.183 + 	}
   2.184 +-	if (wr.buffer == NULL)
   2.185 ++	if (wr.buffer == NULL && !wr.is_cpio_flush)
   2.186 + 		goto exit_1;
   2.187 + 
   2.188 + 	num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
   2.189 +@@ -612,7 +655,7 @@
   2.190 + 	num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
   2.191 + 	for (i = 0; i < num_probs; i++)
   2.192 + 		p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
   2.193 +-
   2.194 ++	wr.max_index = wr.next_index = 0;
   2.195 + 	rc_init_code(&rc);
   2.196 + 
   2.197 + 	while (get_pos(&wr) < header.dst_size) {
   2.198 +@@ -631,12 +674,25 @@
   2.199 + 
   2.200 + 	if (posp)
   2.201 + 		*posp = rc.ptr-rc.buffer;
   2.202 +-	if (wr.flush)
   2.203 ++	if (wr.is_cpio_flush) {
   2.204 ++		int i;
   2.205 ++		for (i = 0; i < wr.next_index -1; i++) {
   2.206 ++			wr.flush(wr.buffer_index[i], LZMA_IOBUF_SIZE);
   2.207 ++			large_free(wr.buffer_index[i]);
   2.208 ++		}
   2.209 ++		if (i < wr.next_index) {
   2.210 ++			wr.flush(wr.buffer_index[i], 
   2.211 ++				 wr.buffer_pos % LZMA_IOBUF_SIZE);
   2.212 ++			large_free(wr.buffer_index[i]);
   2.213 ++		}
   2.214 ++		large_free(wr.buffer_index);
   2.215 ++	}
   2.216 ++	else if (wr.flush)
   2.217 + 		wr.flush(wr.buffer, wr.buffer_pos);
   2.218 + 	ret = 0;
   2.219 + 	large_free(p);
   2.220 + exit_2:
   2.221 +-	if (!output)
   2.222 ++	if (!output && !wr.is_cpio_flush)
   2.223 + 		large_free(wr.buffer);
   2.224 + exit_1:
   2.225 + 	if (!buf)