Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //str Pack_CurrentBuffer = "";
- int Pack_CurrentBufferLen = 0;
- int Pack_CurrentBuffer[65536];
- int Pack_Position = 0;
- function int PackInternal_GetByte(int idx)
- {
- if (idx >= Pack_CurrentBufferLen)
- return 0;
- int idx_dword = idx / 4;
- int idx_int = (idx % 4) * 8;
- int dword = Pack_CurrentBuffer[idx_dword];
- return (dword >> idx_int) & 0xFF;
- }
- function void PackInternal_SetByte(int idx, int b)
- {
- int idx_dword = idx / 4;
- int idx_int = (idx % 4) * 8;
- int mask = 0xFF;
- mask <<= idx_int;
- mask ^= 0xFFFFFFFF;
- int dword = Pack_CurrentBuffer[idx_dword];
- int odword = dword;
- b &= 0xFF;
- dword = (dword & mask) | (b << idx_int);
- Pack_CurrentBuffer[idx_dword] = dword;
- if (idx+1 > Pack_CurrentBufferLen)
- Pack_CurrentBufferLen = idx+1;
- }
- function void Pack_ResetBuffer(void)
- {
- Pack_CurrentBufferLen = 0;
- Pack_Position = 0;
- }
- function void Pack_SetBuffer(str buf)
- {
- Pack_CurrentBufferLen = 0;
- int byteidx = 0;
- int len = StrLen(buf);
- for (int i = 0; i < len; i++)
- {
- int c = GetChar(buf, i);
- int outc = c-1;
- // FF 00 = FE
- // FF 01 = FF
- if (c == 0xFF)
- {
- i++;
- int c1 = GetChar(buf, i);
- outc = c1+0xFD;
- }
- PackInternal_SetByte(byteidx, outc);
- byteidx++;
- }
- Pack_Position = 0;
- }
- function str Pack_GetBuffer(void)
- {
- str out = "";
- for (int i = 0; i < Pack_CurrentBufferLen; i++)
- {
- int b = PackInternal_GetByte(i);
- if (b == 0xFE || b == 0xFF)
- out = StrParam(s:out, c:0xFF, c:b-0xFD);
- else out = StrParam(s:out, c:b+1);
- }
- return out;
- }
- function int Pack_GetBufferSize(void)
- {
- return Pack_CurrentBufferLen;
- }
- function int Pack_GetPosition(void)
- {
- return Pack_Position;
- }
- function int Pack_SetPosition(int p)
- {
- Pack_Position = p;
- return p;
- }
- function void Pack_WriteByte(int b)
- {
- PackInternal_SetByte(Pack_Position, b);
- Pack_Position++;
- }
- function void Pack_WriteInt(int i)
- {
- int b1 = i&0xFF;
- int b2 = (i>>8)&0xFF;
- int b3 = (i>>16)&0xFF;
- int b4 = (i>>24)&0xFF;
- Pack_WriteByte(b1);
- Pack_WriteByte(b2);
- Pack_WriteByte(b3);
- Pack_WriteByte(b4);
- }
- function void Pack_WriteString(str s)
- {
- int len = StrLen(s);
- Pack_WriteInt(len);
- for (int i = 0; i < len; i++)
- Pack_WriteByte(GetChar(s, i));
- }
- function void Pack_WriteStringFixed(str s, int len)
- {
- int lenreal = StrLen(s);
- for (int i = 0; i < len; i++)
- {
- if (i < lenreal) Pack_WriteByte(GetChar(s, i));
- else Pack_WriteByte(0);
- }
- }
- function int Pack_ReadByte(void)
- {
- int c = PackInternal_GetByte(Pack_Position);
- Pack_Position++;
- return c;
- }
- function int Pack_ReadInt(void)
- {
- int b1 = Pack_ReadByte();
- int b2 = Pack_ReadByte();
- int b3 = Pack_ReadByte();
- int b4 = Pack_ReadByte();
- return (b1 | (b2 << 8) | (b3 << 16) | (b4 << 24));
- }
- function str Pack_ReadString(void)
- {
- int len = Pack_ReadInt();
- str out = "";
- for (int i = 0; i < len; i++)
- out = StrParam(s:out, c:Pack_ReadByte());
- return out;
- }
- function str Pack_ReadStringFixed(int len)
- {
- str out = "";
- bool zerofound = false;
- for (int i = 0; i < len; i++)
- {
- int c = Pack_ReadByte();
- if (!c) zerofound = true;
- if (!zerofound) out = StrParam(s:out, c:c);
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement