Advertisement
jewalky

packing.acs

Aug 16th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. //str Pack_CurrentBuffer = "";
  2. int Pack_CurrentBufferLen = 0;
  3. int Pack_CurrentBuffer[65536];
  4. int Pack_Position = 0;
  5.  
  6. function int PackInternal_GetByte(int idx)
  7. {
  8.     if (idx >= Pack_CurrentBufferLen)
  9.         return 0;
  10.  
  11.     int idx_dword = idx / 4;
  12.     int idx_int = (idx % 4) * 8;
  13.    
  14.     int dword = Pack_CurrentBuffer[idx_dword];
  15.     return (dword >> idx_int) & 0xFF;
  16. }
  17.  
  18. function void PackInternal_SetByte(int idx, int b)
  19. {
  20.     int idx_dword = idx / 4;
  21.     int idx_int = (idx % 4) * 8;
  22.    
  23.     int mask = 0xFF;
  24.     mask <<= idx_int;
  25.     mask ^= 0xFFFFFFFF;
  26.    
  27.     int dword = Pack_CurrentBuffer[idx_dword];
  28.     int odword = dword;
  29.     b &= 0xFF;
  30.     dword = (dword & mask) | (b << idx_int);
  31.     Pack_CurrentBuffer[idx_dword] = dword;
  32.    
  33.     if (idx+1 > Pack_CurrentBufferLen)
  34.         Pack_CurrentBufferLen = idx+1;
  35. }
  36.  
  37. function void Pack_ResetBuffer(void)
  38. {
  39.     Pack_CurrentBufferLen = 0;
  40.     Pack_Position = 0;
  41. }
  42.  
  43. function void Pack_SetBuffer(str buf)
  44. {
  45.     Pack_CurrentBufferLen = 0;
  46.     int byteidx = 0;
  47.     int len = StrLen(buf);
  48.     for (int i = 0; i < len; i++)
  49.     {
  50.         int c = GetChar(buf, i);
  51.         int outc = c-1;
  52.         // FF 00 = FE
  53.         // FF 01 = FF
  54.         if (c == 0xFF)
  55.         {
  56.             i++;
  57.             int c1 = GetChar(buf, i);
  58.             outc = c1+0xFD;
  59.         }
  60.        
  61.         PackInternal_SetByte(byteidx, outc);
  62.         byteidx++;
  63.     }
  64.        
  65.     Pack_Position = 0;
  66. }
  67.  
  68. function str Pack_GetBuffer(void)
  69. {
  70.     str out = "";
  71.     for (int i = 0; i < Pack_CurrentBufferLen; i++)
  72.     {
  73.         int b = PackInternal_GetByte(i);
  74.         if (b == 0xFE || b == 0xFF)
  75.             out = StrParam(s:out, c:0xFF, c:b-0xFD);
  76.         else out = StrParam(s:out, c:b+1);
  77.     }
  78.    
  79.     return out;
  80. }
  81.  
  82. function int Pack_GetBufferSize(void)
  83. {
  84.     return Pack_CurrentBufferLen;
  85. }
  86.  
  87. function int Pack_GetPosition(void)
  88. {
  89.     return Pack_Position;
  90. }
  91.  
  92. function int Pack_SetPosition(int p)
  93. {
  94.     Pack_Position = p;
  95.     return p;
  96. }
  97.  
  98. function void Pack_WriteByte(int b)
  99. {
  100.     PackInternal_SetByte(Pack_Position, b);
  101.     Pack_Position++;
  102. }  
  103.  
  104. function void Pack_WriteInt(int i)
  105. {
  106.     int b1 = i&0xFF;
  107.     int b2 = (i>>8)&0xFF;
  108.     int b3 = (i>>16)&0xFF;
  109.     int b4 = (i>>24)&0xFF;
  110.     Pack_WriteByte(b1);
  111.     Pack_WriteByte(b2);
  112.     Pack_WriteByte(b3);
  113.     Pack_WriteByte(b4);
  114. }
  115.  
  116. function void Pack_WriteString(str s)
  117. {
  118.     int len = StrLen(s);
  119.     Pack_WriteInt(len);
  120.     for (int i = 0; i < len; i++)
  121.         Pack_WriteByte(GetChar(s, i));
  122. }
  123.  
  124. function void Pack_WriteStringFixed(str s, int len)
  125. {
  126.     int lenreal = StrLen(s);
  127.     for (int i = 0; i < len; i++)
  128.     {
  129.         if (i < lenreal) Pack_WriteByte(GetChar(s, i));
  130.         else Pack_WriteByte(0);
  131.     }
  132. }
  133.  
  134. function int Pack_ReadByte(void)
  135. {
  136.     int c = PackInternal_GetByte(Pack_Position);
  137.     Pack_Position++;
  138.     return c;
  139. }
  140.  
  141. function int Pack_ReadInt(void)
  142. {
  143.     int b1 = Pack_ReadByte();
  144.     int b2 = Pack_ReadByte();
  145.     int b3 = Pack_ReadByte();
  146.     int b4 = Pack_ReadByte();
  147.     return (b1 | (b2 << 8) | (b3 << 16) | (b4 << 24));
  148. }
  149.  
  150. function str Pack_ReadString(void)
  151. {
  152.     int len = Pack_ReadInt();
  153.     str out = "";
  154.     for (int i = 0; i < len; i++)
  155.         out = StrParam(s:out, c:Pack_ReadByte());
  156.     return out;
  157. }
  158.  
  159. function str Pack_ReadStringFixed(int len)
  160. {
  161.     str out = "";
  162.     bool zerofound = false;
  163.     for (int i = 0; i < len; i++)
  164.     {
  165.         int c = Pack_ReadByte();
  166.         if (!c) zerofound = true;
  167.         if (!zerofound) out = StrParam(s:out, c:c);
  168.     }
  169.     return out;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement