PackMem, UnPackMem Function

Purpose

Compresses a block of memory into a string.

Syntax

$ = PackMem(address, length [,flag = 0])

$ = UnPackMem(string, length)

address, length: iexp
string:sexp
flag:iexp

Description

The function PackMem returns a compressed string from a block of memory at the specified address and length. The function UnPackMem decompresses a string compressed with PackMem.

PackMem will place a 12 byte label in front of a compressed string. The first four signs are "PCK0" (PeCehKahZero), after this, four more signs follow with the length of the compressed data and last four with the original length:

"PCK0" + Mkl$(length_after_compression) + Mkl$(original length) + packed data

When both the original data size as the compressed data size are smaller than 65536, a header of 8 bytes is used, with a lowercase k instead of K, and both lengths in a 16-bit value. Data that cannot be compressed (random byte sequences or a Crypt$) are marked with a lowercase c, followed by only one length (k=16 bit, K=32 bit), so 6 or 8 bytes.

The optional flag can have a value of 0, 1, or 2. If flag = 1 an additional bit pack run is performed. This run will take a bit of time, but as a result, you get a better compression rate (1-10%, sometimes more). In addition, plain text snippets are mostly removed from the compressed string. Packing with default value of flag (= 0) often results in a compressed string where words might be readable. A packed string with flag is 1 is marked as PCK1 or PCk1 instead of PCK0.

flag = 2 forces a bit pack, whether or not the packed string becomes longer.

Example

OpenW 1

Local a$, b$, c$, d$, e%, x%, b1$

// read

a$ = Peek$(4096 * 1024, 60000)

// pack the first part into b$, and

// the rest into c$

b$ = PackMem(V:a$, 30000)

c$ = PackMem(V:a$ + 30000, 30000)

// unpack

d$ = UnPackMem(V:b$, Len(b$)) + UnPackMem(V:c$, Len(c$))

// display: before, packed 2 x times, after

Print Len(a$), Len(b$), Len(c$), Len(d$)

// comparison: before - after

Print a$ = d$

// all with flag 1

b$ = PackMem(V:a$, 30000, 1)

c$ = PackMem(V:a$ + 30000, 30000, 1)

d$ = UnPackMem(V:b$, Len(b$)) + _

UnPackMem(V:c$, Len(c$))

Print Len(a$), Len(b$), Len(c$), Len(d$)

Print a$ = d$

//all with flag 2

b$ = PackMem(V:a$, 30000, 2)

c$ = PackMem(V:a$ + 30000, 30000, 2)

d$ = UnPackMem(V:b$, Len(b$)) + _

UnPackMem(V:c$, Len(c$))

Print Len(a$), Len(b$), Len(c$), Len(d$)

Print a$ = d$

Remarks

The compression rate of PackMem compares to ARC, the grand father of all compression programs, or Compress the program from Microsoft.

See Also

Pack$

{Created by Sjouke Hamstra; Last updated: 21/10/2014 by James Gaite}