Guest User

lzrrv.c

a guest
Nov 13th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /* Ridge Racer Vita decompressor (simplified LZSS) by bnnm */
  5. int main(int argc, char *argv[]) {
  6. if (argc < 2) {
  7. printf("LZRRV decoder by bnnm\nUsage: %s infile\n", argv[0]);
  8. return 1;
  9. }
  10.  
  11. FILE* f_out = NULL;
  12. FILE* f_in = NULL;
  13.  
  14. f_in = fopen(argv[1], "rb");
  15. if (f_in == NULL) {
  16. printf("cannot open infile %s", argv[1]);
  17. goto end;
  18. }
  19.  
  20. char s_out[32768];
  21. strcpy(s_out, argv[1]);
  22. strcat(s_out, ".dec");
  23. f_out = fopen(s_out, "wb+");
  24. if (f_out == NULL) {
  25. printf("cannot open outfile %s", s_out);
  26. goto end;
  27. }
  28.  
  29.  
  30. unsigned char buf[256] = {0};
  31. unsigned char buf0[256] = {0};
  32. int c1,c2;
  33. while (1) {
  34. /* get 16b LE command */
  35. if ((c1 = getc(f_in)) == EOF) break;
  36. if ((c2 = getc(f_in)) == EOF) break;
  37.  
  38. if (c2 == 0xFF) { /* copy uncompressed value */
  39. int len = c1 + 1; /* tested max is 0x100 */
  40.  
  41. int bytes = fread(buf, 1, len, f_in);
  42.  
  43. fwrite(buf, 1, bytes, f_out);
  44. }
  45. else { /* copy window value */
  46. int flag = (c2 << 8) | c1;
  47. int len = ((flag >> 0) & 0x01F) + 3;
  48. int off = ((flag >> 5) & 0x7FF) + 1;
  49.  
  50. /* use file as window, meh */
  51. fseek(f_out, -off, SEEK_END);
  52. int bytes = fread(buf, 1, len, f_out);
  53. fseek(f_out, 0, SEEK_END);
  54.  
  55. fwrite(buf, 1, bytes, f_out);
  56. if (len != bytes) { /* reading further on 0-init window, happens sometimes */
  57. fwrite(buf0, 1, len - bytes, f_out);
  58. }
  59. }
  60. }
  61.  
  62. end:
  63. fclose(f_in);
  64. fclose(f_out);
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment