inflate.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include <flate.h>
  8. typedef struct Biobuf Biobuf;
  9. struct Biobuf
  10. {
  11. uchar *bp;
  12. uchar *p;
  13. uchar *ep;
  14. };
  15. static int header(Biobuf*);
  16. static int trailer(Biobuf*, Biobuf*);
  17. static int getc(void*);
  18. static ulong offset(Biobuf*);
  19. static int crcwrite(void *out, void *buf, int n);
  20. static ulong get4(Biobuf *b);
  21. static ulong Boffset(Biobuf *bp);
  22. /* GZIP flags */
  23. enum {
  24. Ftext= (1<<0),
  25. Fhcrc= (1<<1),
  26. Fextra= (1<<2),
  27. Fname= (1<<3),
  28. Fcomment= (1<<4),
  29. GZCRCPOLY = 0xedb88320UL,
  30. };
  31. static ulong *crctab;
  32. static ulong crc;
  33. int
  34. gunzip(uchar *out, int outn, uchar *in, int inn)
  35. {
  36. Biobuf bin, bout;
  37. int err;
  38. crc = 0;
  39. crctab = mkcrctab(GZCRCPOLY);
  40. err = inflateinit();
  41. if(err != FlateOk)
  42. print("inflateinit failed: %s\n", flateerr(err));
  43. bin.bp = bin.p = in;
  44. bin.ep = in+inn;
  45. bout.bp = bout.p = out;
  46. bout.ep = out+outn;
  47. err = header(&bin);
  48. if(err != FlateOk)
  49. return err;
  50. err = inflate(&bout, crcwrite, &bin, getc);
  51. if(err != FlateOk)
  52. print("inflate failed: %s\n", flateerr(err));
  53. err = trailer(&bout, &bin);
  54. if(err != FlateOk)
  55. return err;
  56. return Boffset(&bout);
  57. }
  58. static int
  59. header(Biobuf *bin)
  60. {
  61. int i, flag;
  62. if(getc(bin) != 0x1f || getc(bin) != 0x8b){
  63. print("bad magic\n");
  64. return FlateCorrupted;
  65. }
  66. if(getc(bin) != 8){
  67. print("unknown compression type\n");
  68. return FlateCorrupted;
  69. }
  70. flag = getc(bin);
  71. /* mod time */
  72. get4(bin);
  73. /* extra flags */
  74. getc(bin);
  75. /* OS type */
  76. getc(bin);
  77. if(flag & Fextra)
  78. for(i=getc(bin); i>0; i--)
  79. getc(bin);
  80. /* name */
  81. if(flag&Fname)
  82. while(getc(bin) != 0)
  83. ;
  84. /* comment */
  85. if(flag&Fcomment)
  86. while(getc(bin) != 0)
  87. ;
  88. /* crc16 */
  89. if(flag&Fhcrc) {
  90. getc(bin);
  91. getc(bin);
  92. }
  93. return FlateOk;
  94. }
  95. static int
  96. trailer(Biobuf *bout, Biobuf *bin)
  97. {
  98. /* crc32 */
  99. ulong x;
  100. x = get4(bin);
  101. if(crc != x){
  102. print("crc mismatch %lux %lux\n", crc, x);
  103. return FlateCorrupted;
  104. }
  105. /* length */
  106. if(get4(bin) != Boffset(bout)){
  107. print("bad output len\n");
  108. return FlateCorrupted;
  109. }
  110. return FlateOk;
  111. }
  112. static ulong
  113. get4(Biobuf *b)
  114. {
  115. ulong v;
  116. int i, c;
  117. v = 0;
  118. for(i = 0; i < 4; i++){
  119. c = getc(b);
  120. v |= c << (i * 8);
  121. }
  122. return v;
  123. }
  124. static int
  125. getc(void *in)
  126. {
  127. Biobuf *bp = in;
  128. if((bp->p - bp->bp) % 10000 == 0)
  129. print(".");
  130. if(bp->p >= bp->ep)
  131. return -1;
  132. return *bp->p++;
  133. }
  134. static ulong
  135. Boffset(Biobuf *bp)
  136. {
  137. return bp->p - bp->bp;
  138. }
  139. static int
  140. crcwrite(void *out, void *buf, int n)
  141. {
  142. Biobuf *bp;
  143. crc = blockcrc(crctab, crc, buf, n);
  144. bp = out;
  145. if(n > bp->ep-bp->p)
  146. n = bp->ep-bp->p;
  147. memmove(bp->p, buf, n);
  148. bp->p += n;
  149. return n;
  150. }
  151. #undef malloc
  152. #undef free
  153. static ulong ibrkp = ~0;
  154. void *
  155. malloc(ulong n)
  156. {
  157. ulong rv;
  158. if(ibrkp == ~0)
  159. ibrkp = ((ulong)end)+1024*1024;
  160. n = (n+3)>>2;
  161. n <<= 2;
  162. rv = ibrkp;
  163. ibrkp += n;
  164. return (void*)rv;
  165. }
  166. void
  167. free(void *)
  168. {
  169. }