File: Compress.k

package info (click to toggle)
kaya 0.4.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,448 kB
  • ctags: 1,694
  • sloc: cpp: 9,536; haskell: 7,461; sh: 3,013; yacc: 910; makefile: 816; perl: 90
file content (88 lines) | stat: -rw-r--r-- 4,535 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/
"<summary>Data compression and uncompression</summary>
<prose>This module provides an interface to zlib to allow the compression and uncompression of Strings or <moduleref>Binary</moduleref> data.</prose>"
module Compress; 

import Regex;
import Prelude;
import Strings;
import Binary;

%include "zlib_glue.h";
%imported "zlib_glue";
%link "z";

foreign "zlib_glue.o" {
    Ptr compressBuffer(Ptr vm, Ptr ind, Int len, outint buflen) = compressBuffer;
    Ptr uncompressBuffer(Ptr vm, Ptr ind, Int len, outint buflen) = uncompressBuffer;
}

"<summary>Error uncompressing data</summary>
<prose>Thrown if there is an error uncompressing the data</prose>
<related><functionref>uncompressBinary</functionref></related>
<related><functionref>uncompressString</functionref></related>"
Exception UncompressionFailed();

"<summary>Error compressing data</summary>
<prose>Thrown if there is an error compressing the data</prose>
<related><functionref>compressBinary</functionref></related>
<related><functionref>compressString</functionref></related>"
Exception CompressionFailed();

"<argument name='b'>The block to compress</argument>
<summary>Compress a block of binary data.</summary>
<prose>Compress a block of binary data using zlib. The compressed block will be returned. If compression fails, a <exceptref>CompressionFailed</exceptref> Exception will be thrown.</prose>
<related><functionref>uncompressBinary</functionref></related>
<related><functionref>compressString</functionref></related>"
public Binary compressBinary(Binary b)
{
    initlen = blockSize(b);
    buflen = 0;
    dat = compressBuffer(getVM(), blockData(b), initlen, buflen);
    return createInitialisedBlock(dat, buflen);
}

"<argument name='b'>The block to uncompress</argument>
<argument name='outlen'>The expected uncompressed length</argument>
<summary>Uncompress a block of binary data.</summary>
<prose>Uncompress a block of binary data that has been compressed with <functionref>compressBinary</functionref>. The uncompressed block will be returned. If uncompression fails, a <exceptref>UncompressionFailed</exceptref> Exception will be thrown.</prose>
<prose>If the real uncompressed length is larger than the expected length, uncompression will fail. If it is smaller, this is wasted memory.</prose>
<related><functionref>compressBinary</functionref></related>
<related><functionref>uncompressString</functionref></related>"
public Binary uncompressBinary(Binary b, var Int outlen) {
    newdat = uncompressBuffer(getVM(), blockData(b), blockSize(b), outlen);
    return createInitialisedBlock(newdat, outlen);
}

"<argument name='instr'>The String to compress</argument>
<summary>Compress a string.</summary>
<prose>Compress a string using zlib and returned a base64-encoded compressed string. If compression fails, a <exceptref>CompressionFailed</exceptref> Exception will be thrown. Note that because base64-encoding increases the space required, if the compression factor is small, the compressed string may be larger than the original! In this case converting the string to Binary form and using <code>compressBinary</code> may be better.</prose>
<related><functionref>compressBinary</functionref></related>
<related><functionref>uncompressString</functionref></related>"
public String compressString(String instr) {
  initlen = byteLength(instr)+1;
  b = createBlock(initlen);
  pokeString(b,0,instr);
  bin = compressBinary(b);
  outstr = String(initlen)+"\n"+Binary::base64Encode(bin);
  return outstr;
}

"<argument name='instr'>The String to uncompress</argument>
<summary>Uncompress a string.</summary>
<prose>Uncompress a string generated with compressString. If uncompression fails, a <exceptref>UncompressionFailed</exceptref> Exception will be thrown. Unlike <code>uncompressBinary</code> you do not have to guess or record an uncompressed size, as compressed strings store this information internally (if the string is corrupted, then this information may be wrong, so uncompression will fail with an Exception)</prose>
<related><functionref>uncompressBinary</functionref></related>
<related><functionref>compressString</functionref></related>"
public String uncompressString(String instr) {
  odata = split("\n",instr);
  bin = Binary::base64Decode(odata[1]);
  raw = uncompressBinary(bin,Int(odata[0]));
  return peekString(raw,0);
}