Advertisement
Guest User

Xtea/LZO

a guest
Apr 6th, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 48.97 KB | None | 0 0
  1. /*
  2. The Following Source Code Was Originaly Created By Balika01 @ elitepvpers.com
  3. Modificated By .RazerX For His Own Compiler
  4. The Source Code It's Already Public So... Feel Free To COPY!
  5.  
  6.  
  7. 24/3/13
  8.  
  9. */
  10. #define _CRT_SECURE_NO_WARNINGS
  11. #include <stdio.h>
  12. #include <vector>
  13. #include <sstream>
  14. #include <fstream>
  15. #ifdef _WIN32 || _WIN64
  16.     #include <windows.h>
  17.     #include <conio.h>
  18.     #define mkfolder(dirname) CreateDirectoryA(dirname, NULL)
  19. #else
  20.     #ifdef __linux__ //untested
  21.         #include <sys/types.h>
  22.         #include <sys/stat.h>
  23.  
  24.         #include <termios.h>
  25.         #include <unistd.h>
  26.  
  27.         #define mkfolder(dirname) mkdir(dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
  28.  
  29.         int getch(void)
  30.         {
  31.             struct termios oldt,
  32.             newt;
  33.             int ch;
  34.             tcgetattr( STDIN_FILENO, &oldt );
  35.             newt = oldt;
  36.             newt.c_lflag &= ~( ICANON | ECHO );
  37.             tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  38.             ch = getchar();
  39.             tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  40.             return ch;
  41.         }
  42.     #else
  43.         #error "THIS OS NOT SUPPORTED YET!"
  44.     #endif
  45. #endif
  46.  
  47. #define FourCC 0x5A4F434D
  48. #define ItemFourCC 0x5850494D
  49. #define MobFourCC 0x5450494D
  50. #define IndexHeader 0x444B5045
  51. #define IndexVersion 2
  52. #define IndexBlockSize 0xC0
  53. #define delta -1640531527 //-0x61C88647
  54. #define LZO_E_OK 1
  55. #define LZO_E_INPUT_OVERRUN (-1)
  56. #define LZO_E_EOF_NOT_FOUND (-2)
  57. #define LZO_E_INPUT_NOT_CONSUMED (-3)
  58.  
  59. #define IndexName ".eix"
  60. #define DataName ".epk"
  61.  
  62. int crashi = 0;
  63.  
  64. unsigned char IndexXTEA[] = {0xB9, 0x9E, 0xB0, 0x02, 0x6F, 0x69, 0x81, 0x05, 0x63, 0x98, 0x9B, 0x28, 0x79, 0x18, 0x1A, 0x00,};
  65. unsigned char DataXTEA[] = {0x22, 0xB8, 0xB4, 0x04, 0x64, 0xB2, 0x6E, 0x1F, 0xAE, 0xEA, 0x18, 0x00, 0xA6, 0xF6, 0xFB, 0x1C,};
  66.  
  67.  
  68. int XteaEncrypt(unsigned long* output, unsigned long* input, unsigned char* XTEA, int size)
  69. {
  70.     int round_count = 32;
  71.     int rounds = 0;
  72.     if (size & 7) size = (size & 0xFFFFFFF8) + 8;
  73.     if(size) {
  74.         do {
  75.             output[0] = input[0];
  76.             output[1] = input[1];
  77.             unsigned int sum  = 0;
  78.             for (int i=0; i < round_count; i++) {
  79.                 output[0] += (((output[1] << 4) ^ (output[1] >> 5)) + output[1]) ^ (sum  + *(unsigned long*)(XTEA + 4 * (sum  & 3)));
  80.                 sum  += delta;
  81.                 output[1] += (((output[0] << 4)  ^ (output[0] >> 5)) + output[0]) ^ (sum  + *(unsigned long*)(XTEA + 4 * ((sum  >> 11) & 3)));
  82.             }
  83.             input += 2;
  84.             output += 2;
  85.             ++rounds;
  86.         } while ( size/8 > rounds );
  87.     }
  88.     return size;
  89. }
  90.  
  91. int XteaDecrypt(unsigned long* output, unsigned long* input, unsigned char* XTEA, int size)
  92. {
  93.     int round_count = 32;
  94.     int rounds = 0;
  95.     if (size & 7) size = (size & 0xFFFFFFF8) + 8;
  96.     if(size) {
  97.         do {
  98.             output[0] = input[0];
  99.             output[1] = input[1];
  100.             unsigned int sum  = delta * round_count;
  101.             for (int i=0; i < round_count; i++) {
  102.                 output[1] -= (((output[0] << 4)  ^ (output[0] >> 5)) + output[0]) ^ (sum  + *(unsigned long*)(XTEA + 4 * ((sum  >> 11) & 3)));
  103.                 sum  -= delta;
  104.                 output[0] -= (((output[1] << 4) ^ (output[1] >> 5)) + output[1]) ^ (sum  + *(unsigned long*)(XTEA + 4 * (sum  & 3)));
  105.             }
  106.             input += 2;
  107.             output += 2;
  108.             ++rounds;
  109.         } while ( size/8 > rounds );
  110.     }
  111.     return size;
  112. }
  113.  
  114. static unsigned long lzo_docompress(const unsigned char* in ,  unsigned long  in_len, unsigned char* out,  unsigned long* out_len,  unsigned long  ti,  void* wrkmem)
  115. {
  116.     register const unsigned char* ip;
  117.     unsigned char* op;
  118.     const unsigned char* const in_end = in + in_len;
  119.     const unsigned char* const ip_end = in + in_len - 20;
  120.     const unsigned char* ii;
  121.     unsigned short* const dict = (unsigned short*)wrkmem;
  122.  
  123.     op = out;
  124.     ip = in;
  125.     ii = ip;
  126.  
  127.     ip += ti < 4 ? 4 - ti : 0;
  128.     for (;;)
  129.     {
  130.         const unsigned char* m_pos;
  131.         unsigned long m_off;
  132.         unsigned long m_len;
  133.         {
  134.         int dv;
  135.         unsigned long dindex;
  136. literal:
  137.         ip += 1 + ((ip - ii) >> 5);
  138. next:
  139.         if (ip >= ip_end)
  140.             break;
  141.         dv = (* (volatile const int*) (volatile const void*) (ip));
  142.         dindex = ((unsigned long) (((((((unsigned long) ((0x1824429d) * (dv)))) >> (32-14))) & ( ((1u << 14) - 1) >> (0))) << (0)));
  143.         m_pos = in+dict[dindex];
  144.         dict[dindex] = ((unsigned short)  ((unsigned long) ((ip)-(in))));
  145.         if (dv != (* (volatile const int*) (volatile const void*) (m_pos)))
  146.             goto literal;
  147.         }
  148.  
  149.         ii -= ti; ti = 0;
  150.         {
  151.         register unsigned long t = (unsigned long) ((ip)-(ii));
  152.         if (t != 0)
  153.         {
  154.             if (t <= 3)
  155.             {
  156.                 op[-2] |= (unsigned char)(t);
  157.                 ((* (volatile int*) (volatile void*) (op)) = (int) ((* (volatile const int*) (volatile const void*) (ii))));
  158.                 op += t;
  159.             }
  160.             else if (t <= 16)
  161.             {
  162.                 *op++ = (unsigned char)(t - 3);
  163.                 ((* (volatile int*) (volatile void*) (op)) = (int) ((* (volatile const int*) (volatile const void*) (ii))));
  164.                 ((* (volatile int*) (volatile void*) (op+4)) = (int) ((* (volatile const int*) (volatile const void*) (ii+4))));
  165.                 ((* (volatile int*) (volatile void*) (op+8)) = (int) ((* (volatile const int*) (volatile const void*) (ii+8))));
  166.                 ((* (volatile int*) (volatile void*) (op+12)) = (int) ((* (volatile const int*) (volatile const void*) (ii+12))));
  167.                 op += t;
  168.             }
  169.             else
  170.             {
  171.                 if (t <= 18)
  172.                     *op++ = (unsigned char)(t - 3);
  173.                 else
  174.                 {
  175.                     register unsigned long tt = t - 18;
  176.                     *op++ = 0;
  177.                     while (tt > 255)
  178.                     {
  179.                         tt -= 255;
  180.                         * (volatile unsigned char *) op++ = 0;
  181.                     }
  182.                     *op++ = (unsigned char)(tt);
  183.                 }
  184.                 do {
  185.                     ((* (volatile int*) (volatile void*) (op)) = (int) ((* (volatile const int*) (volatile const void*) (ii))));
  186.                     ((* (volatile int*) (volatile void*) (op+4)) = (int) ((* (volatile const int*) (volatile const void*) (ii+4))));
  187.                     ((* (volatile int*) (volatile void*) (op+8)) = (int) ((* (volatile const int*) (volatile const void*) (ii+8))));
  188.                     ((* (volatile int*) (volatile void*) (op+12)) = (int) ((* (volatile const int*) (volatile const void*) (ii+12))));
  189.                     op += 16; ii += 16; t -= 16;
  190.                 } while (t >= 16); if (t > 0)
  191.                 { do *op++ = *ii++; while (--t > 0); }
  192.             }
  193.         }
  194.         }
  195.         m_len = 4;
  196.         {
  197.         int v;
  198.         v = (* (volatile const int*) (volatile const void*) (ip + m_len)) ^ (* (volatile const int*) (volatile const void*) (m_pos + m_len));
  199.         if (v == 0) {
  200.             do {
  201.                 m_len += 4;
  202.                 v = (* (volatile const int*) (volatile const void*) (ip + m_len)) ^ (* (volatile const int*) (volatile const void*) (m_pos + m_len));
  203.                 if (ip + m_len >= ip_end)
  204.                     goto m_len_done;
  205.             } while (v == 0);
  206.         }
  207.         unsigned long res = 0;
  208.         m_len += res / 8;
  209.         }
  210. m_len_done:
  211.         m_off = (unsigned long) ((ip)-(m_pos));
  212.         ip += m_len;
  213.         ii = ip;
  214.         if (m_len <= 8 && m_off <= 0x0800)
  215.         {
  216.             m_off -= 1;
  217.             *op++ = (unsigned char)(((m_len - 1) << 5) | ((m_off & 7) << 2));
  218.             *op++ = (unsigned char)(m_off >> 3);
  219.         }
  220.         else if (m_off <= 0x4000)
  221.         {
  222.             m_off -= 1;
  223.             if (m_len <= 33)
  224.                 *op++ = (unsigned char)(32 | (m_len - 2));
  225.             else
  226.             {
  227.                 m_len -= 33;
  228.                 *op++ = 32 | 0;
  229.                 while (m_len > 255)
  230.                 {
  231.                     m_len -= 255;
  232.                     * (volatile unsigned char *) op++ = 0;
  233.                 }
  234.                 *op++ = (unsigned char)(m_len);
  235.             }
  236.             *op++ = (unsigned char)(m_off << 2);
  237.             *op++ = (unsigned char)(m_off >> 6);
  238.         }
  239.         else
  240.         {
  241.             m_off -= 0x4000;
  242.             if (m_len <= 9)
  243.                 *op++ = (unsigned char)(16 | ((m_off >> 11) & 8) | (m_len - 2));
  244.             else
  245.             {
  246.                 m_len -= 9;
  247.                 *op++ = (unsigned char)(16 | ((m_off >> 11) & 8));
  248.                 while (m_len > 255)
  249.                 {
  250.                     m_len -= 255;
  251.                     * (volatile unsigned char *) op++ = 0;
  252.                 }
  253.                 *op++ = (unsigned char)(m_len);
  254.             }
  255.             *op++ = (unsigned char)(m_off << 2);
  256.             *op++ = (unsigned char)(m_off >> 6);
  257.         }
  258.         goto next;
  259.     }
  260.  
  261.     *out_len = (unsigned long) ((op)-(out));
  262.     return (unsigned long) ((in_end)-(ii-ti));
  263. }
  264.  
  265. int lzo_compress(const unsigned char* in , unsigned long  in_len, unsigned char* out, unsigned long* out_len)
  266. {
  267.     const unsigned char* ip = in;
  268.     unsigned char* op = out;
  269.     unsigned long l = in_len;
  270.     unsigned long t = 0;
  271.     void* wrkmem = new void*[16384];
  272.  
  273.     while (l > 20)
  274.     {
  275.         unsigned long ll = l;
  276.         size_t ll_end;
  277.         ll = ((ll) <= (49152) ? (ll) : (49152));
  278.         ll_end = (size_t)ip + ll;
  279.         if ((ll_end + ((t + ll) >> 5)) <= ll_end || (const unsigned char*)(ll_end + ((t + ll) >> 5)) <= ip + ll) break;
  280.         memset(wrkmem, 0, ((unsigned long)1 << 14) * sizeof(unsigned short));
  281.         t = lzo_docompress(ip,ll,op,out_len,t,wrkmem);
  282.         ip += ll;
  283.         op += *out_len;
  284.         l  -= ll;
  285.     }
  286.     t += l;
  287.  
  288.     if (t > 0)
  289.     {
  290.         const unsigned char* ii = in + in_len - t;
  291.  
  292.         if (op == out && t <= 238)
  293.             *op++ = (unsigned char)(17 + t);
  294.         else if (t <= 3)
  295.             op[-2] |= (unsigned char)(t);
  296.         else if (t <= 18)
  297.             *op++ = (unsigned char)(t - 3);
  298.         else
  299.         {
  300.             unsigned long tt = t - 18;
  301.  
  302.             *op++ = 0;
  303.             while (tt > 255)
  304.             {
  305.                 tt -= 255;
  306.  
  307.                 * (volatile unsigned char *) op++ = 0;
  308.             }
  309.             *op++ = (unsigned char)(tt);
  310.         }
  311.         do *op++ = *ii++; while (--t > 0);
  312.     }
  313.  
  314.     *op++ = 16 | 1;
  315.     *op++ = 0;
  316.     *op++ = 0;
  317.  
  318.     *out_len = (unsigned long) ((op)-(out));
  319.     return LZO_E_OK;
  320. }
  321.  
  322. int lzo_decompress( const unsigned char* in , unsigned long in_len, unsigned char* out, unsigned long* out_len)
  323. {
  324.     register unsigned char* op;
  325.     register const unsigned char* ip;
  326.     register unsigned long t;
  327.     register const unsigned char* m_pos;
  328.  
  329.     const unsigned char* const ip_end = in + in_len;
  330.     unsigned char* const op_end = out + *out_len;
  331.  
  332.     *out_len = 0;
  333.  
  334.     op = out;
  335.     ip = in;
  336.  
  337.     if (*ip > 17)
  338.     {
  339.         t = *ip++ - 17;
  340.         if (t < 4) goto match_next;
  341.         do *op++ = *ip++; while (--t > 0);
  342.         goto first_literal_run;
  343.     }
  344.  
  345.     while (ip < ip_end)
  346.     {
  347.         t = *ip++;
  348.         if (t >= 16)
  349.             goto match;
  350.         if (t == 0)
  351.         {
  352.             while (*ip == 0)
  353.             {
  354.                 t += 255;
  355.                 ip++;
  356.             }
  357.             t += 15 + *ip++;
  358.         }
  359.         ((* (volatile unsigned int*) (volatile void*) (op)) = (unsigned long) (* (volatile const unsigned int*) (volatile const void*) (ip)));
  360.         op += 4; ip += 4;
  361.         if (--t > 0)
  362.         {
  363.             if (t >= 4)
  364.             {
  365.                 do {
  366.                     ((* (volatile unsigned int*) (volatile void*) (op)) = (unsigned long) (* (volatile const unsigned int*) (volatile const void*) (ip)));
  367.                     op += 4; ip += 4; t -= 4;
  368.                 } while (t >= 4);
  369.                 if (t > 0) do *op++ = *ip++; while (--t > 0);
  370.             }
  371.             else
  372.                 do *op++ = *ip++; while (--t > 0);
  373.         }
  374.  
  375. first_literal_run:
  376.  
  377.         t = *ip++;
  378.         if (t >= 16) goto match;
  379.         m_pos = op - (1 + 0x0800);
  380.         m_pos -= t >> 2;
  381.         m_pos -= *ip++ << 2;
  382.         *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
  383.         goto match_done;
  384.  
  385.         do {
  386. match:
  387.             if (t >= 64)
  388.             {
  389.                 m_pos = op - 1;
  390.                 m_pos -= (t >> 2) & 7;
  391.                 m_pos -= *ip++ << 3;
  392.                 t = (t >> 5) - 1;
  393.                 goto copy_match;
  394.             }
  395.             else if (t >= 32)
  396.             {
  397.                 t &= 31;
  398.                 if (t == 0)
  399.                 {
  400.                     while (*ip == 0)
  401.                     {
  402.                         t += 255;
  403.                         ip++;
  404.                     }
  405.                     t += 31 + *ip++;
  406.                 }
  407.                 m_pos = op - 1;
  408.                 m_pos -= (* (volatile const unsigned short*) (volatile const void*) (ip)) >> 2;
  409.                 ip += 2;
  410.             }
  411.             else if (t >= 16)
  412.             {
  413.                 m_pos = op;
  414.                 m_pos -= (t & 8) << 11;
  415.                 t &= 7;
  416.                 if (t == 0)
  417.                 {
  418.                     while (*ip == 0)
  419.                     {
  420.                         t += 255;
  421.                         ip++;
  422.                     }
  423.                     t += 7 + *ip++;
  424.                 }
  425.                 m_pos -= (* (volatile const unsigned short*) (volatile const void*) (ip)) >> 2;
  426.                 ip += 2;
  427.                 if (m_pos == op)
  428.                     goto eof_found;
  429.                 m_pos -= 0x4000;
  430.             }
  431.             else
  432.             {
  433.                 m_pos = op - 1;
  434.                 m_pos -= t >> 2;
  435.                 m_pos -= *ip++ << 2;
  436.                 *op++ = *m_pos++; *op++ = *m_pos;
  437.                 goto match_done;
  438.             }
  439.  
  440.             if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
  441.             {
  442.                 ((* (volatile unsigned int*) (volatile void*) (op)) = (unsigned long) (* (volatile const unsigned int*) (volatile const void*) (m_pos)));
  443.                 op += 4; m_pos += 4; t -= 4 - (3 - 1);
  444.                 do {
  445.                     ((* (volatile unsigned int*) (volatile void*) (op)) = (unsigned long) (* (volatile const unsigned int*) (volatile const void*) (m_pos)));
  446.                     op += 4; m_pos += 4; t -= 4;
  447.                 } while (t >= 4);
  448.                 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
  449.             }
  450.             else
  451.             {
  452. copy_match:
  453.                 *op++ = *m_pos++; *op++ = *m_pos++;
  454.                 do *op++ = *m_pos++; while (--t > 0);
  455.             }
  456.  
  457.  
  458. match_done:
  459.             t = ip[-2] & 3;
  460.             if (t == 0)
  461.                 break;
  462.  
  463. match_next:
  464.             *op++ = *ip++;
  465.             if (t > 1) {
  466.                 *op++ = *ip++;
  467.                 if (t > 2)
  468.                 {
  469.                     *op++ = *ip++;
  470.                 }
  471.             }
  472.             t = *ip++;
  473.         } while (ip < ip_end);
  474.     }
  475.     *out_len = ((unsigned long) ((op)-(out)));
  476.     return LZO_E_EOF_NOT_FOUND;
  477.  
  478. eof_found:
  479.     *out_len = ((unsigned long) ((op)-(out)));
  480.     return (ip == ip_end ? LZO_E_OK :
  481.            (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  482. }
  483.  
  484. static const int crctable[] = {
  485.     0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
  486.     0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
  487.     0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
  488.     0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
  489.     0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
  490.     0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
  491.     0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
  492.     0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
  493.     0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
  494.     0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
  495.     0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
  496.     0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
  497.     0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
  498.     0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
  499.     0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
  500.     0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
  501.     0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
  502.     0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
  503.     0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
  504.     0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
  505.     0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
  506.     0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
  507.     0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
  508.     0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
  509.     0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
  510.     0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
  511.     0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
  512.     0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
  513.     0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
  514.     0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
  515.     0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
  516.     0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
  517.     0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
  518.     0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
  519.     0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
  520.     0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
  521.     0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
  522.     0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
  523.     0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
  524.     0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
  525.     0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
  526.     0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
  527.     0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
  528.     0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
  529.     0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
  530.     0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
  531.     0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
  532.     0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
  533.     0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
  534.     0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
  535.     0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
  536.     0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
  537.     0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
  538.     0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
  539.     0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
  540.     0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
  541.     0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
  542.     0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
  543.     0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
  544.     0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
  545.     0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
  546.     0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
  547.     0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
  548.     0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
  549. };
  550.  
  551. unsigned long CRC32(unsigned char* buffer, int size) {
  552.     unsigned long crc = 0xffffffff;
  553.     for (int i = 0; i < size; i++) crc = (crc >> 8) ^ crctable[buffer[i] ^ crc & 0x000000FF];
  554.     return ~crc;
  555. }
  556.  
  557. void pause()
  558. {
  559.     printf("Press a key to continue...\n");
  560.     getch();
  561. }
  562.  
  563. int main(int argc, char * argv[])
  564. {
  565.    
  566.     if(argc != 2)
  567.     {
  568.         std::string exename = argv[0];
  569.         exename = exename.substr(exename.find_last_of("\\") + 1, exename.find_last_of(".") - exename.find_last_of("\\") - 1);
  570.         pause();
  571.         return -1;
  572.     }
  573.  
  574.     std::ifstream myfile(argv[1]);
  575.     if (myfile.is_open())
  576.     {
  577.         while(1)
  578.         {
  579.             std::string line;
  580.             std::getline(myfile,line);
  581. LAB1:
  582.             if(strlen(line.c_str()) == 0 || line.substr(0, 2) == "--") if(!myfile.good()) break; else continue;
  583.             if(line.substr(0, 4) == "pack")
  584.             {
  585.                 if(strlen(line.c_str())-5 < 1) continue;
  586.                 std::string outfile = line.substr(5, strlen(line.c_str())-5);
  587.                 bool agin = 0;
  588.                 std::string file[9999];
  589.                 std::string path[9999];
  590.                 int type[9999];
  591.                 int filecount = 0;
  592.                 while(1)
  593.                 {
  594.                     std::getline(myfile,line);
  595.                     if(strlen(line.c_str()) == 0 || line.substr(0, 2) == "--") if(!myfile.good()) break; else continue;
  596.                     if(line.substr(0, 1) != "\t")
  597.                     {
  598.                         agin = 1;
  599.                         break;
  600.                     }
  601.                     if(strlen(line.c_str())-1 < 1 || line.substr(2, 3) == "--") if(!myfile.good()) break; else continue;
  602.                     std::string filename = line.substr(1, strlen(line.c_str())-1);
  603.                     file[filecount] = strtok((char*)(filename.c_str()),"|");
  604.                     path[filecount] = strtok(NULL, "|");
  605.                     type[filecount] = atoi(strtok(NULL, "|"));
  606.                     ++filecount;
  607.                     if(!myfile.good()) break;
  608.                 }
  609.                 unsigned char* eixdecompressedBuffer = new unsigned char[0x0C + filecount * IndexBlockSize];
  610.                 memset(eixdecompressedBuffer, 0, 0x0C + filecount * IndexBlockSize);
  611.  
  612.                 eixdecompressedBuffer += 0x0C;
  613.  
  614.                 unsigned char* epkBuffer = new unsigned char[536870911]; //512MB
  615.  
  616.                 int lastoffset = 0;
  617.                 int donefiles = 0;
  618.                 for(int i=0; i < filecount; ++i) {
  619.                     unsigned char* Buffer;
  620.                     unsigned long Size;
  621.                    
  622.                     FILE* File = fopen(path[i].c_str() , "rb" );
  623.                     if (File == NULL) {
  624.                         printf("Could not open %s file.\n", path[i].c_str());
  625.                         if(agin) goto LAB1; else return 0;
  626.                     }
  627.                     fseek(File , 0 , SEEK_END);
  628.                     Size = ftell(File);
  629.                     rewind(File);
  630.                     Buffer = new unsigned char[Size];
  631.                     if (Buffer == NULL) {
  632.                         printf("Memory error when make buffer for %s file.\n", path[i].c_str());
  633.                         if(agin) goto LAB1; else return 0;
  634.                     }
  635.                     if (fread(Buffer,1,Size,File) != Size) {
  636.                         printf("Could not read %s file.\n", path[i].c_str());
  637.                         if(agin) goto LAB1; else return 0;
  638.                     }
  639.                     fclose(File);
  640.  
  641.                     *(unsigned long*)eixdecompressedBuffer = i;
  642.                     memcpy(eixdecompressedBuffer + 4, file[i].c_str(), strlen(file[i].c_str()));
  643.                     *(unsigned long*)(eixdecompressedBuffer + 168) = CRC32((unsigned char*)file[i].c_str(),strlen(file[i].c_str()));
  644.                     *(unsigned long*)(eixdecompressedBuffer + 184) = lastoffset;
  645.                     *(unsigned char*)(eixdecompressedBuffer + 188) = type[i];
  646.  
  647.                     if(type[i] == 0){
  648.                         *(unsigned long*)(eixdecompressedBuffer + 172) = Size;
  649.                         *(unsigned long*)(eixdecompressedBuffer + 176) = Size;
  650.                         *(unsigned long*)(eixdecompressedBuffer + 180) = CRC32(Buffer, Size);
  651.                         memset(epkBuffer + lastoffset, 0, Size);
  652.                         memcpy(epkBuffer + lastoffset, Buffer, Size);
  653.                         lastoffset += Size;
  654.                     }
  655.                     else if(type[i] == 1){
  656.                         unsigned char* compressedBuffer = new unsigned char[Size + (Size / 16) + 64 + 3 + 20];
  657.                         memset(compressedBuffer, 0, Size + (Size / 16) + 64 + 3 + 20);
  658.    
  659.                         unsigned long compSize = 0;
  660.                         if(lzo_compress(Buffer, Size, compressedBuffer + 20, &compSize) != LZO_E_OK)
  661.                         {
  662.                             printf("Error when compressing %s.\n", file[i].c_str());
  663.                             delete [] compressedBuffer;
  664.                             pause();
  665.                             if(agin) goto LAB1; else return 0;
  666.                         }
  667.                        
  668.                         *(unsigned long*)compressedBuffer = FourCC;
  669.                         *(unsigned long*)(compressedBuffer + 8) = compSize;
  670.                         *(unsigned long*)(compressedBuffer + 12) = Size;
  671.                         *(unsigned long*)(compressedBuffer + 16) = FourCC;
  672.  
  673.                         memset(epkBuffer + lastoffset, 0, compSize + 20);
  674.                         memcpy(epkBuffer + lastoffset, compressedBuffer, compSize + 20);
  675.  
  676.                         *(unsigned long*)(eixdecompressedBuffer + 172) = Size;
  677.                         *(unsigned long*)(eixdecompressedBuffer + 176) = compSize + 4;
  678.                         *(unsigned long*)(eixdecompressedBuffer + 180) = CRC32(Buffer, Size);
  679.                         lastoffset += compSize + 20;
  680.                         delete[] compressedBuffer;
  681.                     }
  682.                     else if(type[i] == 2)
  683.                     {
  684.                         unsigned char* compressedBuffer = new unsigned char[Size + (Size / 16) + 64 + 3 + 4];
  685.                         memset(compressedBuffer, 0, Size + (Size / 16) + 64 + 3 + 4);
  686.    
  687.                         unsigned long compSize = 0;
  688.                         if(lzo_compress(Buffer, Size, compressedBuffer + 4, &compSize) != LZO_E_OK)
  689.                         {
  690.                             printf("Error when compressing %s.\n", file[i].c_str());
  691.                             delete [] compressedBuffer;
  692.                             pause();
  693.                             if(agin) goto LAB1; else return 0;
  694.                         }
  695.                         *(unsigned long*)compressedBuffer = FourCC;
  696.  
  697.                         unsigned char* encryptedBuffer = new unsigned char[compSize + 4 + 7];
  698.                         memset(encryptedBuffer, 0, compSize + 4 + 7);
  699.    
  700.                         unsigned long cryptSize = XteaEncrypt((unsigned long*)encryptedBuffer, (unsigned long*)compressedBuffer, DataXTEA, compSize + 4);
  701.  
  702.                         memset(epkBuffer + lastoffset, 0, cryptSize + 16);
  703.                         *(unsigned long*)(epkBuffer + lastoffset) = FourCC;
  704.                         *(unsigned long*)(epkBuffer + lastoffset + 4)  = cryptSize;
  705.                         *(unsigned long*)(epkBuffer + lastoffset + 8)  = compSize;
  706.                         *(unsigned long*)(epkBuffer + lastoffset + 12)  = Size;
  707.                         memcpy(epkBuffer + lastoffset + 16, encryptedBuffer, cryptSize);
  708.  
  709.                        
  710.                         *(unsigned long*)(eixdecompressedBuffer + 172) = 512 + (512 / (cryptSize + 16));
  711.                         *(unsigned long*)(eixdecompressedBuffer + 176) = cryptSize;
  712.                         *(unsigned long*)(eixdecompressedBuffer + 180) = CRC32(epkBuffer + lastoffset, cryptSize);
  713.                         lastoffset += cryptSize + 16;
  714.                         delete[] compressedBuffer;
  715.                         delete[] encryptedBuffer;
  716.                     }
  717.                     else
  718.                     {
  719.                         printf("type %i is unsupported\n", type[i]);
  720.                         delete [] Buffer;
  721.                         continue;
  722.                     }
  723.                     eixdecompressedBuffer += IndexBlockSize;
  724.                     ++donefiles;
  725.                     delete [] Buffer;
  726.                 }
  727.  
  728.                 unsigned long eixdecompressedSize = 0x0C + donefiles * IndexBlockSize;
  729.                 eixdecompressedBuffer -= eixdecompressedSize;
  730.  
  731.                 *(unsigned long*)eixdecompressedBuffer = IndexHeader;
  732.                 *(unsigned long*)(eixdecompressedBuffer + 4) = IndexVersion;
  733.                 *(unsigned long*)(eixdecompressedBuffer + 8) = donefiles;
  734.  
  735.                 unsigned char* compressedBuffer = new unsigned char[eixdecompressedSize + (eixdecompressedSize / 16) + 64 + 3 + 4];
  736.                 memset(compressedBuffer, 0, eixdecompressedSize + (eixdecompressedSize / 16) + 64 + 3 + 4);
  737.    
  738.                 unsigned long compSize = 0;
  739.                 if(lzo_compress(eixdecompressedBuffer, eixdecompressedSize, compressedBuffer + 4, &compSize) != LZO_E_OK)
  740.                 {
  741.                     printf("Error when compressing index.\n");
  742.                     delete [] compressedBuffer;
  743.                     pause();
  744.                     if(agin) goto LAB1; else return 0;
  745.                 }
  746.                 *(unsigned long*)compressedBuffer = FourCC;
  747.  
  748.                 unsigned char* encryptedBuffer = new unsigned char[compSize + 4];
  749.                 memset(encryptedBuffer, 0, compSize + 4);
  750.    
  751.                 unsigned long cryptSize = XteaEncrypt((unsigned long*)encryptedBuffer, (unsigned long*)compressedBuffer, IndexXTEA, compSize + 4);
  752.  
  753.                 unsigned char* DoneBuffer = new unsigned char[cryptSize + 16];
  754.                 memset(DoneBuffer, 0, cryptSize + 16);
  755.                 *(unsigned long*)DoneBuffer = FourCC;
  756.                 *(unsigned long*)(DoneBuffer + 4)  = cryptSize;
  757.                 *(unsigned long*)(DoneBuffer + 8)  = compSize;
  758.                 *(unsigned long*)(DoneBuffer + 12)  = eixdecompressedSize;
  759.                 memcpy(DoneBuffer + 16, encryptedBuffer, cryptSize);
  760.  
  761.                 FILE * indexof = fopen((outfile + IndexName).c_str(), "wb");
  762.                 if(indexof)
  763.                 {
  764.                     fwrite(DoneBuffer, 1, cryptSize + 16, indexof);
  765.                     fclose(indexof);
  766.                 }
  767.                
  768.                 FILE * dataof = fopen((outfile + DataName).c_str(), "wb");
  769.                 if(dataof)
  770.                 {
  771.                     fwrite(epkBuffer, 1, lastoffset + 16, dataof);
  772.                     fclose(dataof);
  773.                 }
  774.                
  775.                 printf("Done!\n");
  776.                 if(agin) goto LAB1;
  777.             }
  778.             else if(line.substr(0, 6) == "unpack")
  779.             {
  780.                 if(strlen(line.c_str())-7 < 1) continue;
  781.                 std::string in = line.substr(7, strlen(line.c_str())-7);
  782.                 char* infile = strtok((char*)(in.c_str()),"|");
  783.                 std::string outfolder = strtok(NULL, "|");
  784.                 char* outfile = strtok(NULL, "|");
  785.  
  786.                 std::string eixName_ = (std::string)infile + IndexName;
  787.                 std::string epkName_ = (std::string)infile + DataName;
  788.                 const char * eixName = eixName_.c_str();
  789.                 const char * epkName = epkName_.c_str();
  790.    
  791.                 unsigned char* eixBuffer;
  792.                 unsigned long eixSize;
  793.                
  794.                 FILE* eixFile = fopen(eixName , "rb" );
  795.                 if (eixFile == NULL) {
  796.                     printf("Could not open %s file.\n", eixName);
  797.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  798.                 }
  799.                 fseek(eixFile , 0 , SEEK_END);
  800.                 eixSize = ftell(eixFile);
  801.                 rewind(eixFile);
  802.                 eixBuffer = new unsigned char[eixSize];
  803.                 if (eixBuffer == NULL) {
  804.                     printf("Memory error when make buffer for %s file.\n", eixName);
  805.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  806.                 }
  807.                 if (fread(eixBuffer,1,eixSize,eixFile) != eixSize) {
  808.                     printf("Could not read %s file.\n", eixName);
  809.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  810.                 }
  811.                 fclose(eixFile);
  812.                
  813.                 unsigned char* epkBuffer;
  814.                 unsigned long epkSize;
  815.                
  816.                 FILE* epkFile = fopen(epkName , "rb" );
  817.                 if (epkFile == NULL) {
  818.                     printf("Could not open %s file.\n", epkName);
  819.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  820.                 }
  821.                 fseek(epkFile , 0 , SEEK_END);
  822.                 epkSize = ftell(epkFile);
  823.                 rewind(epkFile);
  824.                 epkBuffer = new unsigned char[epkSize];
  825.                 if (epkBuffer == NULL) {
  826.                     printf("Memory error when make buffer for %s file.\n", epkName);
  827.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  828.                 }
  829.                 if (fread(epkBuffer,1,epkSize,epkFile) != epkSize) {
  830.                     printf("Could not read %s file.\n", epkName);
  831.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  832.                 }
  833.                 fclose(epkFile);
  834.  
  835.                 if(eixSize < 0x0C)
  836.                 {
  837.                     printf("The file size for the %s file is too small. The program will now exit.\n", eixName);
  838.                     pause();
  839.                     if(!myfile.good()) {pause(); return 0;} else goto LAB1;
  840.                 }
  841.  
  842.                 unsigned char* eixdecompressedBuffer;
  843.                 unsigned long eixdecompressedSize;
  844.  
  845.                 if (*(unsigned long*)eixBuffer != IndexHeader)
  846.                 {
  847.                     unsigned long eixheader = *(unsigned long*)eixBuffer;
  848.                     unsigned long eixcryptedSize = *(unsigned long*)(eixBuffer + 4);
  849.                     unsigned long eixcompressedSize = *(unsigned long*)(eixBuffer + 8);
  850.                     eixdecompressedSize = *(unsigned long*)(eixBuffer + 12);
  851.  
  852.                     if (eixheader != FourCC)
  853.                     {
  854.                         printf("The FourCC of %s is incorrect.\n", eixName);
  855.                         pause();
  856.                         if(!myfile.good()) return 0; else goto LAB1;
  857.                     }
  858.        
  859.                     if(eixcryptedSize == 0)
  860.                     {
  861.                         printf("[TODO] -- Index of %s is not encrypted!\n", eixName);
  862.                         pause();
  863.                         if(!myfile.good()) return 0; else goto LAB1;
  864.                     }
  865.  
  866.                     unsigned char* eixcompressedBuffer = new unsigned char[eixcryptedSize];
  867.                     memset(eixcompressedBuffer, 0, eixcryptedSize);
  868.  
  869.                     XteaDecrypt((unsigned long*)eixcompressedBuffer, (unsigned long*)(eixBuffer + 0x10), IndexXTEA, eixcryptedSize);
  870.                     if(*(unsigned long*)eixcompressedBuffer != FourCC)
  871.                     {
  872.                         delete [] eixcompressedBuffer;
  873.                         printf("The XTEA Key of %s is incorrect.\n", eixName);
  874.                         pause();
  875.                         if(!myfile.good()) return 0; else goto LAB1;
  876.                     }
  877.  
  878.                     eixdecompressedBuffer = new unsigned char[eixdecompressedSize];
  879.                     memset(eixdecompressedBuffer, 0, eixdecompressedSize);
  880.  
  881.                     unsigned long finalSize = 0;
  882.                     if(lzo_decompress(eixcompressedBuffer + 4, eixcompressedSize, eixdecompressedBuffer, &finalSize) != LZO_E_OK || finalSize != eixdecompressedSize)
  883.                     {
  884.                         delete [] eixdecompressedBuffer;
  885.                         delete [] eixcompressedBuffer;
  886.                         printf("There was an error when decompressing %s.\n", eixName);
  887.                         if(!myfile.good()) return 0; else goto LAB1;
  888.                     }
  889.                     delete [] eixcompressedBuffer;
  890.                 }
  891.                 else
  892.                 {
  893.                     eixdecompressedBuffer = eixBuffer + 4;
  894.                     eixdecompressedSize = eixSize - 4;
  895.                 }
  896.  
  897.                 unsigned long eixheader2 = *(unsigned long*)eixdecompressedBuffer;
  898.                 unsigned long eixversion = *(unsigned long*)(eixdecompressedBuffer + 4);
  899.                 unsigned long eixfileCount = *(unsigned long*)(eixdecompressedBuffer + 8);
  900.  
  901.                 if(eixversion != IndexVersion)
  902.                 {
  903.                     delete [] eixdecompressedBuffer;
  904.                     printf("The version of %s file is incorrect. Found: %i Supported: %i).\n", eixName, eixversion, IndexVersion);
  905.                     pause();
  906.                     if(!myfile.good()) return 0; else goto LAB1;
  907.                 }
  908.  
  909.                 std::string filename = ((std::string)infile).substr(1 + ((std::string)infile).find_last_of("\\/"));
  910.  
  911.                 eixdecompressedBuffer += 0x0C;
  912.  
  913.                 FILE * of = fopen(outfile, "w");
  914.                 if(!of)
  915.                 {
  916.                     printf("There was an error creating %s file.\n", "name");
  917.                     pause();
  918.                     if(!myfile.good()) return 0; else goto LAB1;
  919.                 }
  920.                 if(of) fprintf(of, "pack|%s\n", infile);
  921.  
  922.                 for(unsigned long x = 0; x < eixfileCount; ++x)
  923.                 {
  924.                     unsigned long index = *(unsigned long*)eixdecompressedBuffer;
  925.                     char filename[160];
  926.                     memcpy(filename,eixdecompressedBuffer + 4,160);
  927.                     unsigned long filenameCRC = *(unsigned long*)(eixdecompressedBuffer + 168);
  928.                     unsigned long dw3 = *(unsigned long*)(eixdecompressedBuffer + 172);
  929.                     unsigned long dwSrcSize = *(unsigned long*)(eixdecompressedBuffer + 176);
  930.                     unsigned long unpackedCRC = *(unsigned long*)(eixdecompressedBuffer + 180);
  931.                     unsigned long dwFileOffset = *(unsigned long*)(eixdecompressedBuffer + 184);
  932.                     unsigned char packedType = *(unsigned char*)(eixdecompressedBuffer + 188);
  933.                     eixdecompressedBuffer += IndexBlockSize;
  934.                     std::string outfilename;
  935.                     if(packedType == 0)
  936.                     {
  937.                         std::stringstream dirPath;
  938.                         std::vector<std::string> pathTokens;
  939.                         size_t p0 = 0, p1 = std::string::npos;
  940.                         while(p0 != std::string::npos)
  941.                         {
  942.                             p1 = ((const std::string&)filename).find_first_of("\\/", p0);
  943.                             if(p1 != p0)
  944.                             {
  945.                                 std::string token = ((const std::string&)filename).substr(p0, p1 - p0);
  946.                                 pathTokens.push_back(token);
  947.                             }
  948.                             p0 = ((const std::string&)filename).find_first_not_of("\\/", p1);
  949.                         }
  950.                         dirPath << outfolder;
  951.                         mkfolder(dirPath.str().c_str());
  952.                         dirPath << "\\";
  953.                         size_t index = 0;
  954.                         for(index = 0; index < pathTokens.size() - 1; ++index)
  955.                         {
  956.                             if(pathTokens[index].find_first_of(":") != std::string::npos)
  957.                                 continue;
  958.                             dirPath << pathTokens[index];
  959.                             mkfolder(dirPath.str().c_str());
  960.                             dirPath << "\\";
  961.                         }
  962.                         dirPath << pathTokens[index];
  963.                         outfilename = dirPath.str();
  964.                         FILE * of = fopen(outfilename.c_str(), "wb");
  965.                         if(!of)
  966.                         {
  967.                             std::string buff = outfolder + "\\crashfile" + (char)crashi;
  968.                             ++crashi;
  969.                             of = fopen(buff.c_str(), "wb");
  970.                             printf("Crash file %s saved as: %s\n", outfilename.c_str(), buff.c_str());
  971.                             outfilename = buff;
  972.                         }
  973.                         if(of)
  974.                         {
  975.                             fwrite(epkBuffer + dwFileOffset, 1, dwSrcSize, of);
  976.                             fclose(of);
  977.                         }
  978.                         else
  979.                         {
  980.                             printf("Could not save the file %s\n", outfilename.c_str());
  981.                         }
  982.                     }
  983.                     else if(packedType == 1)
  984.                     {
  985.                         unsigned long dataheader = *(unsigned long*)(epkBuffer + dwFileOffset);
  986.                         unsigned long datacryptedSize = *(unsigned long*)(epkBuffer + dwFileOffset + 4);
  987.                         unsigned long datacompressedSize = *(unsigned long*)(epkBuffer + dwFileOffset + 8);
  988.                         unsigned long datadecompressedSize = *(unsigned long*)(epkBuffer + dwFileOffset + 12);
  989.  
  990.                         if(dataheader != FourCC)
  991.                         {
  992.                             printf("The FourCC is incorrect of %s.\n", filename);
  993.                             continue;
  994.                         }
  995.  
  996.                         unsigned char* datauncompressedBuffer = new unsigned char[datadecompressedSize];
  997.                         memset(datauncompressedBuffer, 0, datadecompressedSize);
  998.  
  999.                         unsigned long finalSize = 0;
  1000.                         if(lzo_decompress(epkBuffer + dwFileOffset + 16 + 4, datacompressedSize, datauncompressedBuffer, &finalSize) == LZO_E_OK && finalSize == datadecompressedSize)
  1001.                         {
  1002.                             std::stringstream dirPath;
  1003.                             std::vector<std::string> pathTokens;
  1004.                             size_t p0 = 0, p1 = std::string::npos;
  1005.                             while(p0 != std::string::npos)
  1006.                             {
  1007.                                 p1 = ((const std::string&)filename).find_first_of("\\/", p0);
  1008.                                 if(p1 != p0)
  1009.                                 {
  1010.                                     std::string token = ((const std::string&)filename).substr(p0, p1 - p0);
  1011.                                     pathTokens.push_back(token);
  1012.                                 }
  1013.                                 p0 = ((const std::string&)filename).find_first_not_of("\\/", p1);
  1014.                             }
  1015.                             dirPath << outfolder;
  1016.                             mkfolder(dirPath.str().c_str());
  1017.                             dirPath << "\\";
  1018.                             size_t index = 0;
  1019.                             for(index = 0; index < pathTokens.size() - 1; ++index)
  1020.                             {
  1021.                                 if(pathTokens[index].find_first_of(":") != std::string::npos)
  1022.                                     continue;
  1023.                                 dirPath << pathTokens[index];
  1024.                                 mkfolder(dirPath.str().c_str());
  1025.                                 dirPath << "\\";
  1026.                             }
  1027.                             dirPath << pathTokens[index];
  1028.                             outfilename = dirPath.str();
  1029.                             FILE * of = fopen(outfilename.c_str(), "wb");
  1030.                             if(!of)
  1031.                             {
  1032.                                 std::string buff = outfolder + "\\crashfile" + (char)crashi;
  1033.                                 ++crashi;
  1034.                                 of = fopen(buff.c_str(), "wb");
  1035.                                 printf("Crash file %s saved as: %s\n", outfilename.c_str(), buff.c_str());
  1036.                                 outfilename = buff;
  1037.                             }
  1038.                             if(of)
  1039.                             {
  1040.                                 fwrite(datauncompressedBuffer, 1, datadecompressedSize, of);
  1041.                                 fclose(of);
  1042.                             }
  1043.                             else
  1044.                             {
  1045.                                 printf("Could not save the file %s\n", outfilename.c_str());
  1046.                             }
  1047.                         }
  1048.                         else
  1049.                         {
  1050.                             printf("There was an error when decompressing %s file.\n", filename);
  1051.                         }
  1052.  
  1053.                         delete [] datauncompressedBuffer;
  1054.                     }
  1055.                     else if(packedType == 2)
  1056.                     {
  1057.                         unsigned long dataheader = *(unsigned long*)(epkBuffer + dwFileOffset);
  1058.                         unsigned long datacryptedSize = *(unsigned long*)(epkBuffer + dwFileOffset + 4);
  1059.                         unsigned long datacompressedSize = *(unsigned long*)(epkBuffer + dwFileOffset + 8);
  1060.                         unsigned long datadecompressedSize = *(unsigned long*)(epkBuffer + dwFileOffset + 12);
  1061.  
  1062.                         if(dataheader != FourCC)
  1063.                         {
  1064.                             printf("The FourCC is incorrect of %s.\n", filename);
  1065.                             continue;
  1066.                         }
  1067.  
  1068.                         unsigned char* datadecryptedBuffer = new unsigned char[datacryptedSize];
  1069.                         memset(datadecryptedBuffer, 0, datacryptedSize);
  1070.            
  1071.                         XteaDecrypt((unsigned long*)datadecryptedBuffer, (unsigned long*)(epkBuffer + dwFileOffset + 16), DataXTEA, datacryptedSize);
  1072.                         if(*(unsigned long*)datadecryptedBuffer != FourCC)
  1073.                         {
  1074.                             printf("There was an error decrypting the data for the %s file. It will be skipped.\n", filename);
  1075.                             delete [] datadecryptedBuffer;
  1076.                             continue;
  1077.                         }
  1078.  
  1079.                         unsigned char* datauncompressedBuffer = new unsigned char[datadecompressedSize];
  1080.                         memset(datauncompressedBuffer, 0, datadecompressedSize);
  1081.  
  1082.                         unsigned long finalSize = 0;
  1083.                         if(lzo_decompress(datadecryptedBuffer + 4, datacompressedSize, datauncompressedBuffer, &finalSize) == LZO_E_OK && finalSize == datadecompressedSize)
  1084.                         {
  1085.                             std::stringstream dirPath;
  1086.                             std::vector<std::string> pathTokens;
  1087.                             size_t p0 = 0, p1 = std::string::npos;
  1088.                             while(p0 != std::string::npos)
  1089.                             {
  1090.                                 p1 = ((const std::string&)filename).find_first_of("\\/", p0);
  1091.                                 if(p1 != p0)
  1092.                                 {
  1093.                                     std::string token = ((const std::string&)filename).substr(p0, p1 - p0);
  1094.                                     pathTokens.push_back(token);
  1095.                                 }
  1096.                                 p0 = ((const std::string&)filename).find_first_not_of("\\/", p1);
  1097.                             }
  1098.                             dirPath << outfolder;
  1099.                             mkfolder(dirPath.str().c_str());
  1100.                             dirPath << "\\";
  1101.                             size_t index = 0;
  1102.                             for(index = 0; index < pathTokens.size() - 1; ++index)
  1103.                             {
  1104.                                 if(pathTokens[index].find_first_of(":") != std::string::npos)
  1105.                                     continue;
  1106.                                 dirPath << pathTokens[index];
  1107.                                 mkfolder(dirPath.str().c_str());
  1108.                                 dirPath << "\\";
  1109.                             }
  1110.                             dirPath << pathTokens[index];
  1111.                             outfilename = dirPath.str();
  1112.                             FILE * of = fopen(outfilename.c_str(), "wb");
  1113.                             if(!of)
  1114.                             {
  1115.                                 std::string buff = outfolder + "\\crashfile" + (char)crashi;
  1116.                                 ++crashi;
  1117.                                 of = fopen(buff.c_str(), "wb");
  1118.                                 printf("Crash file %s saved as: %s\n", outfilename.c_str(), buff.c_str());
  1119.                                 outfilename = buff;
  1120.                             }
  1121.                             if(of)
  1122.                             {
  1123.                                 fwrite(datauncompressedBuffer, 1, datadecompressedSize, of);
  1124.                                 fclose(of);
  1125.                             }
  1126.                             else
  1127.                             {
  1128.                                 printf("Could not save the file %s\n", outfilename.c_str());
  1129.                             }
  1130.                         }
  1131.                         else
  1132.                         {
  1133.                             printf("There was an error when decompressing %s file.\n", filename);
  1134.                         }
  1135.  
  1136.                         delete [] datadecryptedBuffer;
  1137.                         delete [] datauncompressedBuffer;
  1138.                     }
  1139.                     else
  1140.                     {
  1141.                         printf("Unsupported type: %i (%s).\n", packedType, filename);
  1142.                         continue;
  1143.                     }
  1144.                     fprintf(of, "\t%s|%s|%i\n", filename, outfilename.c_str(), packedType);
  1145.                 }
  1146.                 fclose(of);
  1147.                
  1148.                 printf("Done!\n");
  1149.             }
  1150.             else printf("dat file error!\n");
  1151.             if(!myfile.good()) break;
  1152.         }
  1153.        
  1154.         printf("All done!\n");
  1155.         myfile.close();
  1156.     }
  1157.     else printf("%s file not found!\n", argv[1]);
  1158.  
  1159.     pause();
  1160.     return 0;
  1161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement