File size: 4,249 Bytes
d94b56e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
#
#   pyhwp : hwp file format parser in python
#   Copyright (C) 2010-2023 mete0r <https://github.com/mete0r>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import codecs
import zlib  # this codec needs the optional zlib module !

_wbits = -15


def zlib_raw_encode(input, errors='strict'):
    assert errors == 'strict'
    output = zlib.compress(input)[2:-4]
    return (output, len(input))


def zlib_raw_decode(input, errors='strict'):
    assert errors == 'strict'
    output = zlib.decompress(input, _wbits)
    return (output, len(input))


class Codec(codecs.Codec):

    def encode(self, input, errors='strict'):
        return zlib_raw_encode(input, errors)

    def decode(self, input, errors='strict'):
        return zlib_raw_decode(input, errors)


class IncrementalEncoder(codecs.IncrementalEncoder):
    def __init__(self, errors='strict'):
        assert errors == 'strict'
        self.errors = errors
        self.compressobj = zlib.compressobj()
        self.initial = True

    def encode(self, input, final=False):
        c = self.compressobj.compress(input)
        if self.initial:
            c = c[2:]
            self.initial = False
        if final:
            c += self.compressobj.flush()[:-4]
        return c

    def reset(self):
        self.compressobj = zlib.compressobj()


class IncrementalDecoder(codecs.IncrementalDecoder):
    def __init__(self, errors='strict'):
        assert errors == 'strict'
        self.errors = errors
        self.decompressobj = zlib.decompressobj(_wbits)

    def decode(self, input, final=False):
        if final:
            if len(input) > 0:
                d = self.decompressobj.decompress(input)
            else:
                d = b''
            return d + self.decompressobj.flush()
        else:
            return self.decompressobj.decompress(input)

    def reset(self):
        self.decompressobj = zlib.decompressobj(_wbits)


class StreamWriter(object):
    def __init__(self, stream, errors='strict'):
        assert errors == 'strict'
        self.stream = stream
        self.encoder = IncrementalEncoder(errors)

    def write(self, data):
        raise NotImplementedError


class StreamReader(object):
    def __init__(self, stream, errors='strict'):
        assert errors == 'strict'
        self.stream = stream
        self.decoder = IncrementalDecoder(errors)
        self.buffer = b''
        self.offset = 0

    def read(self, size=-1):
        if size < 0:
            c = self.stream.read()
            d = self.buffer + self.decoder.decode(c, True)
            self.buffer = b''
            self.offset += len(d)
            return d

        final = False
        while True:
            if size <= len(self.buffer):
                d = self.buffer[:size]
                self.buffer = self.buffer[size:]
                self.offset += size
                return d

            if final:
                d = self.buffer
                self.buffer = b''
                self.offset += len(d)
                return d

            c = self.stream.read(8196)
            final = len(c) < 8196 or len(c)
            self.buffer += self.decoder.decode(c, final)

    def tell(self):
        return self.offset


_codecinfo = codecs.CodecInfo(
    name='zlib_raw',
    encode=zlib_raw_encode,
    decode=zlib_raw_decode,
    incrementalencoder=IncrementalEncoder,
    incrementaldecoder=IncrementalDecoder,
    streamreader=StreamReader,
    streamwriter=StreamWriter,
)