zqs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <lib9.h>
  2. #include <a.out.h>
  3. #include "squeeze.h"
  4. /*
  5. * forsyth@vitanuova.com
  6. */
  7. /*
  8. * for details of `unsqueeze' see:
  9. *
  10. * %A Mark Taunton
  11. * %T Compressed Executables: An Exercise in Thinking Small
  12. * %P 385-404
  13. * %I USENIX
  14. * %B USENIX Conference Proceedings
  15. * %D Summer 1991
  16. * %C Nashville, TN
  17. *
  18. * several of the unimplemented improvements described in the paper
  19. * have been implemented here
  20. *
  21. * there is a further transformation on the powerpc (QFLAG!=0) to shuffle bits
  22. * in certain instructions so as to push the fixed bits to the top of the word.
  23. */
  24. typedef struct Squeeze Squeeze;
  25. struct Squeeze {
  26. int n;
  27. ulong tab[7*256];
  28. };
  29. enum {
  30. CHECK = 1 /* check precise bounds in Squeeze array */
  31. };
  32. #define GET4(p) (((((((p)[0]<<8)|(p)[1])<<8)|(p)[2])<<8)|(p)[3])
  33. static uchar out[3*1024*1024];
  34. static uchar bigb[1024*1024];
  35. static ulong top;
  36. static int qflag = 1;
  37. static int islittle = 0;
  38. static ulong chksum, oldsum;
  39. static int rdtab(int, Squeeze*, int);
  40. static long unsqueezefd(int, void*);
  41. static uchar* unsqueeze(uchar*, uchar*, uchar*, Squeeze*, Squeeze*, ulong);
  42. static uchar* unsqzseg(int, uchar*, long, ulong);
  43. void
  44. main(int argc, char **argv)
  45. {
  46. int fd;
  47. long n;
  48. if(argc < 2)
  49. exits("args");
  50. fd = open(argv[1], OREAD);
  51. if(fd < 0)
  52. exits("open");
  53. n = unsqueezefd(fd, out);
  54. if(n < 0){
  55. fprint(2, "zqs: can't unsqueeze\n");
  56. exits("err");
  57. }
  58. if(write(1, out, n) != n){
  59. fprint(2, "zqs: write error: %r\n");
  60. exits("err");
  61. }
  62. fprint(2, "%ld bytes, %8.8lux csum\n", n, chksum);
  63. exits(0);
  64. }
  65. static long
  66. unsqueezefd(int fd, void *v)
  67. {
  68. uchar *wp, *out;
  69. ulong toptxt, topdat;
  70. long asis, nst, nsd;
  71. Sqhdr sqh;
  72. Exec ex;
  73. out = (uchar*)v;
  74. if(read(fd, &sqh, SQHDRLEN) != SQHDRLEN)
  75. return -1;
  76. if(GET4(sqh.magic) != SQMAGIC)
  77. return -1;
  78. if(read(fd, &ex, sizeof(Exec)) != sizeof(Exec))
  79. return -1;
  80. toptxt = GET4(sqh.toptxt);
  81. topdat = GET4(sqh.topdat);
  82. oldsum = GET4(sqh.sum);
  83. asis = GET4(sqh.asis);
  84. if(asis < 0)
  85. asis = 0;
  86. nst = GET4(sqh.text);
  87. nsd = GET4(sqh.data);
  88. switch(GET4((uchar*)&ex.magic)){
  89. case Q_MAGIC:
  90. if(qflag)
  91. fprint(2, "PowerPC mode\n");
  92. islittle = 0;
  93. break;
  94. case E_MAGIC:
  95. case 0xA0E1: /* arm AIF */
  96. islittle = 1;
  97. qflag = 0;
  98. break;
  99. default:
  100. fprint(2, "Unknown magic: %8.8ux\n", GET4((uchar*)&ex.magic));
  101. qflag = 0;
  102. break;
  103. }
  104. memmove(out, &ex, sizeof(ex));
  105. wp = unsqzseg(fd, out + sizeof(ex), nst, toptxt);
  106. if(wp == nil)
  107. return -1;
  108. wp = unsqzseg(fd, wp, nsd, topdat);
  109. if(wp == nil)
  110. return -1;
  111. if(asis){
  112. if(read(fd, wp, asis) != asis)
  113. return -1;
  114. wp += asis;
  115. }
  116. return wp-out;
  117. }
  118. static uchar*
  119. unsqzseg(int fd, uchar *wp, long ns, ulong top)
  120. {
  121. Squeeze sq3, sq4;
  122. if(ns == 0)
  123. return wp;
  124. if(rdtab(fd, &sq3, 0) < 0)
  125. return nil;
  126. if(rdtab(fd, &sq4, 8) < 0)
  127. return nil;
  128. fprint(2, "tables: %d %d\n", sq3.n, sq4.n);
  129. if(read(fd, bigb, ns) != ns)
  130. return nil;
  131. return unsqueeze(wp, bigb, bigb+ns, &sq3, &sq4, top);
  132. }
  133. static uchar*
  134. unsqueeze(uchar *wp, uchar *rp, uchar *ep, Squeeze *sq3, Squeeze *sq4, ulong top)
  135. {
  136. ulong nx;
  137. int code, n;
  138. if(qflag){
  139. QREMAP(top); /* adjust top just once, outside the loop */
  140. }
  141. while(rp < ep){
  142. code = *rp++;
  143. n = 0;
  144. nx = code>>4;
  145. do{
  146. if(nx == 0){
  147. nx = top;
  148. }else{
  149. if(nx==1){
  150. if(rp+3 >= ep)
  151. return nil;
  152. nx = (((((rp[3]<<8)|rp[2])<<8)|rp[1])<<8)|rp[0];
  153. rp += 4;
  154. }else if(nx <= 8){ /* 2 to 8 */
  155. if(rp+1 >= ep)
  156. return nil;
  157. nx = ((nx-2)<<8) | rp[0];
  158. if(CHECK && nx >= sq4->n)
  159. return nil; /* corrupted file */
  160. nx = sq4->tab[nx] | rp[1];
  161. rp += 2;
  162. }else{ /* 9 to 15 */
  163. if(rp >= ep)
  164. return nil; /* corrupted file */
  165. nx = ((nx-9)<<8) | rp[0];
  166. if(CHECK && nx >= sq3->n)
  167. return nil; /* corrupted file */
  168. nx = sq3->tab[nx];
  169. rp++;
  170. }
  171. if(rp > ep)
  172. return nil; /* corrupted file */
  173. if(qflag){
  174. QREMAP(nx);
  175. }
  176. }
  177. if(islittle){
  178. wp[0] = nx;
  179. wp[1] = nx>>8;
  180. wp[2] = nx>>16;
  181. wp[3] = nx>>24;
  182. }else{
  183. wp[0] = nx>>24;
  184. wp[1] = nx>>16;
  185. wp[2] = nx>>8;
  186. wp[3] = nx;
  187. }
  188. wp += 4;
  189. chksum += nx;
  190. nx = code & 0xF;
  191. }while(++n == 1);
  192. }
  193. return wp;
  194. }
  195. static int
  196. rdtab(int fd, Squeeze *sq, int shift)
  197. {
  198. uchar b[7*256*5], *p, *ep;
  199. ulong v, w;
  200. int i;
  201. if(read(fd, b, 2) != 2)
  202. return -1;
  203. i = (b[0]<<8) | b[1];
  204. if(1)
  205. fprint(2, "table: %d\n", i);
  206. if((i -= 2) > 0){
  207. if(read(fd, b, i) != i)
  208. return -1;
  209. }
  210. sq->n = 0;
  211. p = b;
  212. ep = b+i;
  213. v = 0;
  214. while(p < ep){
  215. w = 0;
  216. do{
  217. if(p >= ep)
  218. return -1;
  219. w = (w<<7) | (*p & 0x7F);
  220. }while(*p++ & 0x80);
  221. v += w;
  222. if(0)
  223. fprint(2, "%d %8.8lux %8.8lux\n", sq->n, v, w);
  224. sq->tab[sq->n++] = v << shift;
  225. }
  226. return 0;
  227. }