Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4.  
  5. char get_bit(char *data, int *offset, int *index) {
  6. char abyte;
  7. abyte = (data[*index] & (1 << (7 - *offset))) >> (7 - *offset);
  8. (*offset)++;
  9. if (*offset == 8) {
  10. (*index)++;
  11. *offset = 0;
  12. }
  13. return abyte;
  14. }
  15.  
  16. char *get_nbits(int nbits, char *data, int *offset, int *index) {
  17. char *bytes;
  18. int nbytes = (nbits % 8) ? nbits / 8 + 1 : nbits / 8;
  19. int boffset = (nbits % 8) ? 8 - nbits % 8 : 0;
  20. int bindex = 0;
  21.  
  22. bytes = malloc(sizeof(char) * nbytes);
  23. bzero(bytes, sizeof(char) * nbytes);
  24.  
  25. int i;
  26. char bit;
  27. for (i = 0; i < nbits; i++) {
  28. bit = get_bit(data, offset, index);
  29.  
  30. bytes[bindex] = bytes[bindex] | ((bit << 7) >> boffset);
  31. boffset++;
  32. if (boffset == 8) {
  33. bindex++;
  34. boffset = 0;
  35. }
  36. }
  37.  
  38. return bytes;
  39. }
  40.  
  41. int is_endmarker(char *data, int *offset, int *index) {
  42. int end = 0;
  43. int offset_tmp = *offset, index_tmp = *index;
  44. char *bytes = get_nbits(2, data, offset, index);
  45.  
  46. if (bytes[0] == (char) 3) {
  47. bytes = get_nbits(7, data, offset, index);
  48.  
  49. if (bytes[0] == (char) 0) {
  50. end = 1;
  51. }
  52. }
  53.  
  54. *offset = offset_tmp;
  55. *index = index_tmp;
  56. return end;
  57. }
  58.  
  59. char *uncompress_lzs(char *data) {
  60. int data_offset = 0, data_index = 0;
  61. char abyte, *bytes;
  62.  
  63. char *history = malloc(sizeof(char) * 2048);
  64. int hindex = 0;
  65.  
  66. bzero(history, sizeof(char) * 2048);
  67.  
  68. while (1) {
  69. abyte = get_bit(data, &data_offset, &data_index);
  70.  
  71. if (abyte == (char) 0) {
  72. // raw pattern
  73. bytes = get_nbits(8, data, &data_offset, &data_index);
  74. history[hindex] = bytes[0];
  75. hindex++;
  76.  
  77. } else {
  78. // string pattern
  79.  
  80. int sp_offset = 0;
  81.  
  82. abyte = get_bit(data, &data_offset, &data_index);
  83.  
  84. if (abyte == (char) 0) {
  85. // offset > 127
  86. bytes = get_nbits(11, data, &data_offset, &data_index);
  87. char aux = bytes[0];
  88. bytes[0] = bytes[1];
  89. bytes[1] = aux;
  90. sp_offset = *((short *) bytes);
  91.  
  92. } else {
  93. // offset <= 127
  94. bytes = get_nbits(7, data, &data_offset, &data_index);
  95. sp_offset = *((char *) bytes);
  96. }
  97.  
  98. int sp_length = 0;
  99.  
  100. bytes = get_nbits(2, data, &data_offset, &data_index);
  101. if (bytes[0] != 3) {
  102. // length <= 4
  103. sp_length = ((int) bytes[0]) + 2;
  104. } else {
  105. bytes = get_nbits(2, data, &data_offset, &data_index);
  106.  
  107. if (bytes[0] != 3) {
  108. // length <= 7
  109. sp_length = ((int) bytes[0]) + 5;
  110. } else {
  111. // length > 7
  112. int n = 0;
  113. while (1) {
  114. bytes = get_nbits(4, data, &data_offset, &data_index);
  115. if (bytes[0] == (char) 15) {
  116. n++;
  117. } else {
  118. break;
  119. }
  120. }
  121. sp_length = (n * 15) + ((int) bytes[0]) + 8;
  122. }
  123. }
  124.  
  125. // copiar patron
  126. int count = 0;
  127. for (count = 0; count < sp_length; count++) {
  128. history[hindex] = history[hindex - sp_offset];
  129. //printf("%c\n", history[hindex]);
  130. hindex++;
  131. }
  132. }
  133.  
  134. if (is_endmarker(data, &data_offset, &data_index) == 1) {
  135. // end marker
  136. break;
  137. }
  138.  
  139. }
  140.  
  141. return history;
  142. }
  143.  
  144.  
  145. int main(int argc, char **argv) {
  146.  
  147. if( argc == 0) {
  148. printf("Yayy: %s <data.lzs>\n", argv[0]);
  149. return -1;
  150. }
  151.  
  152. FILE *file = NULL;
  153.  
  154. file = fopen(argv[1], "rb");
  155. if (!file) {
  156. printf("Error.\n");
  157. return -1;
  158. }
  159.  
  160. char *data = malloc(sizeof(char) * 2048);
  161. fread(data, sizeof(char), 2048, file);
  162. fclose(file);
  163.  
  164. char *uncompress = uncompress_lzs(data);
  165. int count;
  166. for (count = 0; count < 2048; count++) {
  167. printf("%c", uncompress[count]);
  168. }
  169.  
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement