You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unnrv2b.S 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  3. *
  4. * This file is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * Originally this code was part of ucl the data compression library
  10. * for upx the ``Ultimate Packer of eXecutables''.
  11. *
  12. * - Converted to gas assembly, and refitted to work with etherboot.
  13. * Eric Biederman 20 Aug 2002
  14. *
  15. * - Structure modified to be a subroutine call rather than an
  16. * executable prefix.
  17. * Michael Brown 30 Mar 2004
  18. *
  19. * - Modified to be compilable as either 16-bit or 32-bit code.
  20. * Michael Brown 9 Mar 2005
  21. */
  22. /****************************************************************************
  23. * This file provides the decompress() and decompress16() functions
  24. * which can be called in order to decompress an image compressed with
  25. * the nrv2b utility in src/util.
  26. *
  27. * These functions are designed to be called by the prefix. They are
  28. * position-independent code.
  29. *
  30. * The same basic assembly code is used to compile both
  31. * decompress() and decompress16().
  32. ****************************************************************************
  33. */
  34. .text
  35. .arch i386
  36. .section ".prefix.lib", "ax", @progbits
  37. #ifdef CODE16
  38. /****************************************************************************
  39. * decompress16 (real-mode near call, position independent)
  40. *
  41. * Decompress data in 16-bit mode
  42. *
  43. * Parameters (passed via registers):
  44. * %ds:%esi - Start of compressed input data
  45. * %es:%edi - Start of output buffer
  46. * Returns:
  47. * %ds:%esi - End of compressed input data
  48. * %es:%edi - End of decompressed output data
  49. * All other registers are preserved
  50. *
  51. * NOTE: It would be possible to build a smaller version of the
  52. * decompression code for -DKEEP_IT_REAL by using
  53. * #define REG(x) x
  54. * to use 16-bit registers where possible. This would impose limits
  55. * that the compressed data size must be in the range [1,65533-%si]
  56. * and the uncompressed data size must be in the range [1,65536-%di]
  57. * (where %si and %di are the input values for those registers). Note
  58. * particularly that the lower limit is 1, not 0, and that the upper
  59. * limit on the input (compressed) data really is 65533, since the
  60. * algorithm may read up to three bytes beyond the end of the input
  61. * data, since it reads dwords.
  62. ****************************************************************************
  63. */
  64. #define REG(x) e ## x
  65. #define ADDR32 addr32
  66. .code16
  67. .globl decompress16
  68. decompress16:
  69. #else /* CODE16 */
  70. /****************************************************************************
  71. * decompress (32-bit protected-mode near call, position independent)
  72. *
  73. * Parameters (passed via registers):
  74. * %ds:%esi - Start of compressed input data
  75. * %es:%edi - Start of output buffer
  76. * Returns:
  77. * %ds:%esi - End of compressed input data
  78. * %es:%edi - End of decompressed output data
  79. * All other registers are preserved
  80. ****************************************************************************
  81. */
  82. #define REG(x) e ## x
  83. #define ADDR32
  84. .code32
  85. .globl decompress
  86. decompress:
  87. #endif /* CODE16 */
  88. #define xAX REG(ax)
  89. #define xCX REG(cx)
  90. #define xBP REG(bp)
  91. #define xSI REG(si)
  92. #define xDI REG(di)
  93. /* Save registers */
  94. push %xAX
  95. pushl %ebx
  96. push %xCX
  97. push %xBP
  98. /* Do the decompression */
  99. cld
  100. xor %xBP, %xBP
  101. dec %xBP /* last_m_off = -1 */
  102. jmp dcl1_n2b
  103. decompr_literals_n2b:
  104. ADDR32 movsb
  105. decompr_loop_n2b:
  106. addl %ebx, %ebx
  107. jnz dcl2_n2b
  108. dcl1_n2b:
  109. call getbit32
  110. dcl2_n2b:
  111. jc decompr_literals_n2b
  112. xor %xAX, %xAX
  113. inc %xAX /* m_off = 1 */
  114. loop1_n2b:
  115. call getbit1
  116. adc %xAX, %xAX /* m_off = m_off*2 + getbit() */
  117. call getbit1
  118. jnc loop1_n2b /* while(!getbit()) */
  119. sub $3, %xAX
  120. jb decompr_ebpeax_n2b /* if (m_off == 2) goto decompr_ebpeax_n2b ? */
  121. shl $8, %xAX
  122. ADDR32 movb (%xSI), %al /* m_off = (m_off - 3)*256 + src[ilen++] */
  123. inc %xSI
  124. xor $-1, %xAX
  125. jz decompr_end_n2b /* if (m_off == 0xffffffff) goto decomp_end_n2b */
  126. mov %xAX, %xBP /* last_m_off = m_off ?*/
  127. decompr_ebpeax_n2b:
  128. xor %xCX, %xCX
  129. call getbit1
  130. adc %xCX, %xCX /* m_len = getbit() */
  131. call getbit1
  132. adc %xCX, %xCX /* m_len = m_len*2 + getbit()) */
  133. jnz decompr_got_mlen_n2b /* if (m_len == 0) goto decompr_got_mlen_n2b */
  134. inc %xCX /* m_len++ */
  135. loop2_n2b:
  136. call getbit1
  137. adc %xCX, %xCX /* m_len = m_len*2 + getbit() */
  138. call getbit1
  139. jnc loop2_n2b /* while(!getbit()) */
  140. inc %xCX
  141. inc %xCX /* m_len += 2 */
  142. decompr_got_mlen_n2b:
  143. cmp $-0xd00, %xBP
  144. adc $1, %xCX /* m_len = m_len + 1 + (last_m_off > 0xd00) */
  145. push %xSI
  146. ADDR32 lea (%xBP,%xDI), %xSI /* m_pos = dst + olen + -m_off */
  147. rep
  148. es ADDR32 movsb /* dst[olen++] = *m_pos++ while(m_len > 0) */
  149. pop %xSI
  150. jmp decompr_loop_n2b
  151. getbit1:
  152. addl %ebx, %ebx
  153. jnz 1f
  154. getbit32:
  155. ADDR32 movl (%xSI), %ebx
  156. sub $-4, %xSI /* sets carry flag */
  157. adcl %ebx, %ebx
  158. 1:
  159. ret
  160. decompr_end_n2b:
  161. /* Restore registers and return */
  162. pop %xBP
  163. pop %xCX
  164. popl %ebx
  165. pop %xAX
  166. ret