inflate.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. extern void diff(char*); //XXX
  34. int
  35. gunzip(uchar *out, int outn, uchar *in, int inn)
  36. {
  37. Biobuf bin, bout;
  38. int err;
  39. crc = 0;
  40. crctab = mkcrctab(GZCRCPOLY);
  41. err = inflateinit();
  42. if(err != FlateOk)
  43. print("inflateinit failed: %s\n", flateerr(err));
  44. bin.bp = bin.p = in;
  45. bin.ep = in+inn;
  46. bout.bp = bout.p = out;
  47. bout.ep = out+outn;
  48. err = header(&bin);
  49. if(err != FlateOk)
  50. return err;
  51. err = inflate(&bout, crcwrite, &bin, getc);
  52. if(err != FlateOk)
  53. print("inflate failed: %s\n", flateerr(err));
  54. err = trailer(&bout, &bin);
  55. if(err != FlateOk)
  56. return err;
  57. return Boffset(&bout);
  58. }
  59. static int
  60. header(Biobuf *bin)
  61. {
  62. int i, flag;
  63. if(getc(bin) != 0x1f || getc(bin) != 0x8b){
  64. print("bad magic\n");
  65. return FlateCorrupted;
  66. }
  67. if(getc(bin) != 8){
  68. print("unknown compression type\n");
  69. return FlateCorrupted;
  70. }
  71. flag = getc(bin);
  72. /* mod time */
  73. get4(bin);
  74. /* extra flags */
  75. getc(bin);
  76. /* OS type */
  77. getc(bin);
  78. if(flag & Fextra)
  79. for(i=getc(bin); i>0; i--)
  80. getc(bin);
  81. /* name */
  82. if(flag&Fname)
  83. while(getc(bin) != 0)
  84. ;
  85. /* comment */
  86. if(flag&Fcomment)
  87. while(getc(bin) != 0)
  88. ;
  89. /* crc16 */
  90. if(flag&Fhcrc) {
  91. getc(bin);
  92. getc(bin);
  93. }
  94. return FlateOk;
  95. }
  96. static int
  97. trailer(Biobuf *bout, Biobuf *bin)
  98. {
  99. /* crc32 */
  100. if(crc != get4(bin)){
  101. print("crc mismatch\n");
  102. return FlateCorrupted;
  103. }
  104. /* length */
  105. if(get4(bin) != Boffset(bout)){
  106. print("bad output len\n");
  107. return FlateCorrupted;
  108. }
  109. return FlateOk;
  110. }
  111. static ulong
  112. get4(Biobuf *b)
  113. {
  114. ulong v;
  115. int i, c;
  116. v = 0;
  117. for(i = 0; i < 4; i++){
  118. c = getc(b);
  119. v |= c << (i * 8);
  120. }
  121. return v;
  122. }
  123. static int
  124. getc(void *in)
  125. {
  126. Biobuf *bp = in;
  127. // if((bp->p - bp->bp) % 10000 == 0)
  128. // print(".");
  129. if(bp->p >= bp->ep)
  130. return -1;
  131. return *bp->p++;
  132. }
  133. static ulong
  134. Boffset(Biobuf *bp)
  135. {
  136. return bp->p - bp->bp;
  137. }
  138. static int
  139. crcwrite(void *out, void *buf, int n)
  140. {
  141. Biobuf *bp;
  142. int nn;
  143. crc = blockcrc(crctab, crc, buf, n);
  144. bp = out;
  145. nn = n;
  146. if(nn > bp->ep-bp->p)
  147. nn = bp->ep-bp->p;
  148. if(nn > 0)
  149. memmove(bp->p, buf, nn);
  150. bp->p += n;
  151. return n;
  152. }
  153. #undef malloc
  154. #undef free
  155. void *
  156. malloc(ulong n)
  157. {
  158. return ialloc(n, 8);
  159. }
  160. void
  161. free(void *)
  162. {
  163. }