Advertisement
Guest User

Untitled

a guest
May 26th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <iterator>
  7. #include <fstream>
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <memory.h>
  12. #include <vector>
  13.  
  14. #pragma warning(disable:4996)
  15.  
  16. #define __attribute__(A)
  17.  
  18. #if _WIN32
  19. unsigned int s32(unsigned int i)
  20. {
  21.   return ((i >> 24) | (i << 24) | ((i & 0x00FF0000) >> 8) | ((i & 0x0000FF00) << 8));
  22. }
  23. unsigned short s16(unsigned short s)
  24. {
  25.   return (s >> 8) | (s << 8);
  26. }
  27. #else
  28. #include <arpa/inet.h>
  29. unsigned int s32(unsigned int i) { return ntohl(i); }
  30. unsigned int s16(unsigned short s) { return ntohs(s); }
  31. #endif
  32.  
  33. #ifndef ATTRIBUTE_ALIGN
  34. # define ATTRIBUTE_ALIGN(v)                 __attribute__((aligned(v)))
  35. #endif
  36.  
  37. #define CHARBITS12  11
  38. #define CHARBITS9   9
  39. #define CODESIZE12  (1<<CHARBITS12)
  40. #define CODESIZE9   (1<<CHARBITS9)
  41. #define ASRSIZE12   4096
  42. #define ASRSIZE9    512
  43. #define TREESTACKSIZE   256
  44.  
  45. static int getbitsc(unsigned char *, int, int);
  46. static int getbit1c(unsigned char *, int);
  47. static int readtree9(unsigned char *, unsigned short *, unsigned short *, unsigned short *);
  48. static int readtree12(unsigned char *, unsigned short *, unsigned short *, unsigned short *);
  49.  
  50. static int Rvl_decode_ash_size(const unsigned char* s);
  51. int Rvl_decode_ash(unsigned char *d, unsigned char *s);
  52. extern void* DecompAsh(const void* src);
  53.  
  54. static int bitlen[2], ccnt[2], wptr[2];
  55. static unsigned bitcode[2];
  56. static unsigned char work[(ASRSIZE9 + ASRSIZE9 + 1 + ASRSIZE12 + ASRSIZE12 + 1) * 4] ATTRIBUTE_ALIGN(32);
  57.  
  58. /*---------------------------------------------------------------------------*
  59.   Name:         DecmoAsh
  60.   Description:  Expands an ASH-compressed file
  61.   Arguments:    src
  62.                 memHandle
  63.   Returns:      None.
  64.  *---------------------------------------------------------------------------*/
  65. void*
  66. DecompAsh(void* src)
  67. {
  68.   int dstSize = Rvl_decode_ash_size(static_cast<const unsigned char*>(src));
  69.   printf("dstSize: %i\n", dstSize);
  70.  
  71.   void* dstBuf = malloc(dstSize);
  72.  
  73.   Rvl_decode_ash(static_cast<unsigned char*>(dstBuf), static_cast<unsigned char*>(src));
  74.  
  75.   return dstBuf;
  76. }
  77.  
  78. static int Rvl_decode_ash_size(const unsigned char* s)
  79. {
  80.   return (s[5] << 16) | (s[6] << 8) | s[7];
  81. }
  82.  
  83. /* Main ASH expansion*/
  84. int Rvl_decode_ash(unsigned char *d, unsigned char *s)
  85. {
  86.   int i, j, k, q, t, root9, root12;
  87.   unsigned short *left9, *right9, *left12, *right12, *stack;
  88.  
  89.   t = (s[5] << 16) | (s[6] << 8) | s[7];                /* Expanded size */
  90.   k = (s[8] << 24) | (s[9] << 16) | (s[10] << 8) | s[11];   /* Flag information offset*/
  91.   /* Initialization */
  92.   wptr[0] = 12; wptr[1] = k;
  93.   bitlen[0] = bitlen[1] = 0;
  94.   ccnt[0] = CODESIZE9; ccnt[1] = CODESIZE12;
  95.   left9 = (unsigned short *)work;
  96.   right9 = left9 + (2 * CODESIZE9 - 1) * sizeof(short);
  97.   left12 = right9 + (2 * CODESIZE9 - 1) * sizeof(short);
  98.   right12 = left12 + (2 * CODESIZE12 - 1) * sizeof(short);
  99.   stack = right12 + (2 * CODESIZE12 - 1) * sizeof(short);
  100.   (void)getbitsc(s, 32, 0); (void)getbitsc(s, 32, 1);
  101.   root9 = readtree9(s, left9, right9, stack);
  102.   printf("root9: %i\n", root9);
  103.   root12 = readtree12(s, left12, right12, stack);
  104.   printf("root12: %i\n", root12);
  105.   /* Expansion*/
  106.   for (q = 0; q < t;) {
  107.     j = root9;
  108.     while (j >= CODESIZE9) {
  109.       j = (getbit1c(s, 0)) ? right9[j] : left9[j];
  110.     }
  111.     if (j < 256) {
  112.       d[q++] = (unsigned char)j;
  113.     }
  114.     else {
  115.       i = root12;
  116.       while (i >= CODESIZE12) {
  117.         i = (getbit1c(s, 1)) ? right12[i] : left12[i];
  118.       }
  119.       i = q - i - 1;    /* Distance */
  120.       j -= 253;
  121.       for (; j > 0; j--, q++, i++)d[q] = d[i];
  122.     }
  123.   }
  124.   return(q);
  125. }
  126.  
  127. int getbitsc(unsigned char *s, int size, int f)
  128. {
  129.   int k, m;
  130.   unsigned i, j, g;
  131.   k = wptr[f]; m = bitlen[f]; g = bitcode[f];
  132.   if (m + size > 32) {
  133.     j = (unsigned)((s[k] << 24) | (s[k + 1] << 16) | (s[k + 2] << 8) | s[k + 3]);
  134.     i = (g >> (32 - size)) | (j >> (64 - size - m));
  135.     m += size - 32; bitcode[f] = j << m; bitlen[f] = m;
  136.     wptr[f] = k + 4;
  137.   }
  138.   else if (m + size == 32) {
  139.     i = g >> (32 - size);
  140.     bitcode[f] = (unsigned)((s[k] << 24) | (s[k + 1] << 16) | (s[k + 2] << 8) | s[k + 3]);
  141.     wptr[f] = k + 4; bitlen[f] = 0;
  142.   }
  143.   else {
  144.     i = g >> (32 - size); bitcode[f] = g << size; bitlen[f] = m + size;
  145.   }
  146.   return((int)i);
  147. }
  148.  
  149. int getbit1c(unsigned char *s, int f)
  150. {
  151.   int k, m;
  152.   unsigned i, g;
  153.   k = wptr[f]; m = bitlen[f]; g = bitcode[f];
  154.   i = g >> 31;
  155.   if (m == 31) {
  156.     bitcode[f] = (unsigned)((s[k] << 24) | (s[k + 1] << 16) | (s[k + 2] << 8) | s[k + 3]);
  157.     wptr[f] = k + 4; bitlen[f] = 0;
  158.   }
  159.   else {
  160.     bitcode[f] = g << 1; bitlen[f] = m + 1;
  161.   }
  162.   return((int)i);
  163. }
  164.  
  165. int readtree9(unsigned char *s, unsigned short *left9, unsigned short *right9, unsigned short *stack)
  166. {
  167.  
  168.   int f, j, m, z, sp;
  169.   for (j = m = ccnt[0], sp = 0;;) {
  170.     if (getbit1c(s, 0)) {
  171.       stack[sp] = (unsigned short)(0x8000 | j);
  172.       stack[sp + 1] = (unsigned short)(0x4000 | j);
  173.       j++; m++; sp += 2;
  174.       if (sp >= TREESTACKSIZE) {
  175.         //              OS_Printf("stack over flow\n");
  176.       }
  177.     }
  178.     else {
  179.       z = getbitsc(s, CHARBITS9, 0);
  180.       while (1) {
  181.         f = (int)stack[--sp];
  182.         if (f & 0x8000) {
  183.           right9[f & 0x3FFF] = (unsigned short)z;
  184.           z = f & 0x3FFF;
  185.           if (sp == 0)return(z);
  186.         }
  187.         else {
  188.           left9[f & 0x3FFF] = (unsigned short)z;
  189.           j = m;
  190.           break;
  191.         }
  192.       }
  193.     }
  194.   }
  195. }
  196.  
  197. int readtree12(unsigned char *s, unsigned short *left12, unsigned short *right12, unsigned short *stack)
  198. {
  199.  
  200.   int f, j, m, z, sp;
  201.   for (j = m = ccnt[1], sp = 0;;) {
  202.     if (getbit1c(s, 1)) {
  203.       //if (sp < 100) printf("sp: %i, j: %i\n", sp, 0x8000 | j);
  204.       stack[sp] = (unsigned short)(0x8000 | j);
  205.       stack[sp + 1] = (unsigned short)(0x4000 | j);
  206.       j++; m++; sp += 2;
  207.       if (sp >= TREESTACKSIZE) {
  208.         //printf("stack over flow\n");
  209.       }
  210.     }
  211.     else {
  212.       z = getbitsc(s, CHARBITS12, 1);
  213.       while (1) {
  214.         f = (int)stack[--sp];
  215.         if (f & 0x8000) {
  216.           right12[f & 0x3FFF] = (unsigned short)z;
  217.           z = f & 0x3FFF;
  218.           if (sp == 0)return(z);
  219.         }
  220.         else {
  221.           left12[f & 0x3FFF] = (unsigned short)z;
  222.           j = m;
  223.           break;
  224.         }
  225.       }
  226.     }
  227.   }
  228. }
  229.  
  230. std::vector<uint8_t> readFile(std::string filename) {
  231.   std::ifstream file(filename, std::ios::binary);
  232.   file.unsetf(std::ios::skipws);
  233.   std::streampos fileSize;
  234.   file.seekg(0, std::ios::end);
  235.   fileSize = file.tellg();
  236.   file.seekg(0, std::ios::beg);
  237.   std::vector<uint8_t> vec;
  238.   vec.reserve(fileSize);
  239.   vec.insert(vec.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
  240.   return vec;
  241. }
  242.  
  243. int main() {
  244.   std::cout << "Hello World!\n";
  245.  
  246.   std::string filePath = "C:/Users/User/source/repos/ConsoleApplication7/Debug/mii.ash";
  247.  
  248.   std::vector<uint8_t> buffer = readFile(filePath);
  249.  
  250.   void* out = DecompAsh(buffer.data());
  251.  
  252.   int size = Rvl_decode_ash_size(static_cast<const unsigned char*>(buffer.data()));
  253.  
  254.   std::FILE* file = std::fopen("C:/Users/User/source/repos/ConsoleApplication7/Debug/course.arc", "wb");
  255.  
  256.   size_t numWritten = std::fwrite(out, 1, size * sizeof(uint8_t), file);
  257.   std::fclose(file);
  258.  
  259.   std::cout << "Success!" << std::endl;
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement