1 /* arm_nrv2e_d8.S -- ARM decompressor for NRV2E
3 This file is part of the UPX executable compressor.
5 Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 1996-2008 Laszlo Molnar
7 Copyright (C) 2000-2008 John F. Reiser
10 UPX and the UCL library are free software; you can redistribute them
11 and/or modify them under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; see the file COPYING.
22 If not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 Markus F.X.J. Oberhumer Laszlo Molnar
26 <markus@oberhumer.com> <ml1050@users.sourceforge.net>
29 <jreiser@users.sourceforge.net>
31 #define SAFE 0 /* 1 for src+dst bounds checking: cost 40 bytes */
34 #define len r1 /* overlaps 'cnt' */
39 #define wrnk r6 /* 0x500 M2_MAX_OFFSET before "wrinkle" */
45 #define cnt r1 /* overlaps 'len' while reading an offset */
48 #define CHECK_SRC cmp src,srclim; bhs bad_src_n2e
49 #define CHECK_DST cmp dst,dstlim; bhs bad_dst_n2e
51 #define CHECK_SRC /*empty*/
52 #define CHECK_DST /*empty*/
55 #if 0 /*{ DEBUG only: check newly-decompressed against original dst */
59 cmp wrnk,tmp; beq 0f; bkpt; \
62 #define CHECK_BYTE /*empty*/
65 /* "mov lr,pc; bxx ..." implements conditional subroutine call */
66 #define GETBIT add bits,bits; mov lr,pc; beq get1_n2e
68 #define getnextb(reg) GETBIT; adc reg,reg
69 #define jnextb0 GETBIT; bcc
70 #define jnextb1 GETBIT; bcs
73 ucl_nrv2e_decompress_8: .globl ucl_nrv2e_decompress_8 @ ARM mode
74 .type ucl_nrv2e_decompress_8, %function
75 /* error = (*)(char const *src, int len_src, char *dst, int *plen_dst)
76 Actual decompressed length is stored through plen_dst.
77 For SAFE mode: at call, *plen_dst must be allowed length of output buffer.
79 adr r12,1+.thumb_nrv2e_d8; bx r12 @ enter THUMB mode
86 push {r2,r3, r4,r5,r6,r7, lr}
87 #define sp_DST0 0 /* stack offset of original dst */
89 add srclim,len,src @ srclim= eof_src;
91 ldr tmp,[r3] @ len_dst
95 mov bits,#1; neg off,bits @ off= -1 initial condition
96 lsl bits,#31 @ 1<<31: refill next time
98 lsl wrnk,#8 @ 0x500 @ nrv2e M2_MAX_OFFSET
102 bad_dst_n2e: # return value will be 2
104 bad_src_n2e: # return value will be 1
109 pop {r3,r4} @ r3= orig_dst; r4= plen_dst
110 sub src,srclim @ 0 if actual src length equals expected length
111 sub dst,r3 @ actual dst length
113 pop {r4,r5,r6,r7 /*,pc*/}
114 pop {r1}; bx r1 @ "pop {,pc}" fails return to ARM mode on ARMv4T
117 bx r0 /* Branch to 0x0, switch to ARM mode */
120 get1_n2e: @ In: Carry set [from adding 0x80000000 (1<<31) to itself]
121 ldrb bits,[src] @ zero-extend next byte
122 adc bits,bits @ double and insert CarryIn as low bit
125 lsl bits,#24 @ move to top byte, and set CarryOut from old bit 8
126 mov pc,lr @ return, stay in current (THUMB) mode
129 CHECK_SRC; ldrb tmp,[src]; add src,#1
131 CHECK_DST; strb tmp,[dst]; add dst,#1
134 mov cnt,#1; b getoff_n2e
143 sub tmp,cnt,#3 @ set Carry
144 mov len,#0 @ Carry unaffected
145 blo offprev_n2e @ cnt was 2; tests Carry only
147 CHECK_SRC; ldrb off,[src]; add src,#1 @ low 7+1 bits
149 mvn off,off; beq eof_n2e @ off= ~off
150 asr off,#1; bcs lenlast_n2e
165 getnextb(len) @ 0,1,2,3
167 gotlen_n2e: @ 'cmn': add the inputs, set condition codes, discard the sum
168 cmn wrnk,off; bcs near_n2e @ within M2_MAX_OFFSET
169 add len,#1 @ too far away, so minimum match length is 3
172 ldr tmp,[sp,#sp_DST0]
174 sub tmp,off; bhi bad_dst_n2e @ reaching back too far
177 cmp tmp,dstlim; bhi bad_dst_n2e @ too much output
179 ldrb tmp,[dst] @ force cacheline allocate
183 strb tmp,[dst]; add dst,#1
184 sub len,#1; bne copy_n2e
188 .size ucl_nrv2e_decompress_8, .-ucl_nrv2e_decompress_8