LibreOffice Module hwpfilter (master) 1
hiodev.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <stdio.h>
21#include <errno.h>
22
23#ifdef _WIN32
24# include <io.h>
25#else
26# include <unistd.h>
27#endif
28
29
30#include "hwplib.h"
31#include "hgzip.h"
32#include "hiodev.h"
33#include "hwpfile.h"
34#include "hstream.hxx"
35
36const size_t BUFSIZE = 1024;
38
39// HIODev abstract class
41{
42 init();
43}
44
45
47{
48}
49
50
52{
53 compressed = false;
54}
55
56size_t HIODev::read2b(void *ptr, size_t nmemb)
57{
58 ushort *p = static_cast<ushort *>(ptr);
59
60 if (state())
61 return 0;
62 size_t ii;
63 for (ii = 0; ii < nmemb; ++ii)
64 {
65 if (!read2b(p[ii]))
66 break;
67 if (state())
68 break;
69 }
70 return ii;
71}
72
73size_t HIODev::read4b(void *ptr, size_t nmemb)
74{
75 uint *p = static_cast<uint *>(ptr);
76
77 if (state())
78 return 0;
79 size_t ii;
80 for (ii = 0; ii < nmemb; ++ii)
81 {
82 if (!read4b(p[ii]))
83 break;
84 if (state())
85 break;
86 }
87 return ii;
88}
89
90
91// hfileiodev class
92HStreamIODev::HStreamIODev(std::unique_ptr<HStream> stream):_stream(std::move(stream))
93{
94 init();
95}
96
97
99{
100/* 플러시한 후 닫는다. */
101 flush();
102 if (_gzfp)
104 _gzfp = nullptr;
105}
106
107
109{
110 _gzfp = nullptr;
111 compressed = false;
112}
113
114
116{
117 return _stream->available() != 0;
118}
119
120
122{
123 if (_gzfp)
124 gz_flush(_gzfp, Z_FINISH);
125}
126
128{
129 return false;
130}
131
132/* zlib 관련 부분 */
134{
135 compressed = flag;
136 if (flag)
137 {
139 return nullptr != _gzfp;
140 }
141 else if (_gzfp)
142 {
143 gz_flush(_gzfp, Z_FINISH);
145 _gzfp = nullptr;
146 }
147 return true;
148}
149
150
151// IO routines
152
153#define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
154
155bool HStreamIODev::read1b(unsigned char &out)
156{
157 size_t res = compressed ? GZREAD(rBuf, 1) : _stream->readBytes(rBuf, 1);
158
159 if (res < 1)
160 return false;
161
162 out = static_cast<unsigned char>(rBuf[0]);
163 return true;
164}
165
166bool HStreamIODev::read1b(char &out)
167{
168 unsigned char tmp8;
169 if (!read1b(tmp8))
170 return false;
171 out = tmp8;
172 return true;
173}
174
175bool HStreamIODev::read2b(unsigned short &out)
176{
177 size_t res = compressed ? GZREAD(rBuf, 2) : _stream->readBytes(rBuf, 2);
178
179 if (res < 2)
180 return false;
181
182 out = (static_cast<unsigned char>(rBuf[1]) << 8 | static_cast<unsigned char>(rBuf[0]));
183 return true;
184}
185
186bool HStreamIODev::read4b(unsigned int &out)
187{
188 size_t res = compressed ? GZREAD(rBuf, 4) : _stream->readBytes(rBuf, 4);
189
190 if (res < 4)
191 return false;
192
193 out = (static_cast<unsigned char>(rBuf[3]) << 24 | static_cast<unsigned char>(rBuf[2]) << 16 |
194 static_cast<unsigned char>(rBuf[1]) << 8 | static_cast<unsigned char>(rBuf[0]));
195 return true;
196}
197
199{
200 unsigned int tmp32;
201 if (!read4b(tmp32))
202 return false;
203 out = tmp32;
204 return true;
205}
206
207size_t HStreamIODev::readBlock(void *ptr, size_t size)
208{
209 size_t count =
211 ? GZREAD(ptr, size)
212 : _stream->readBytes(static_cast<byte *>(ptr), size);
213
214 return count;
215}
216
217size_t HStreamIODev::skipBlock(size_t size)
218{
219 if (compressed){
220 if( size <= BUFSIZE )
221 return GZREAD(rBuf, size);
222 else{
223 size_t remain = size;
224 while(remain){
225 if( remain > BUFSIZE ) {
226 size_t read = GZREAD(rBuf, BUFSIZE);
227 remain -= read;
228 if (read != BUFSIZE)
229 break;
230 }
231 else{
232 remain -= GZREAD(rBuf, remain);
233 break;
234 }
235 }
236 return size - remain;
237 }
238 }
239 return _stream->skipBytes(size);
240}
241
242
243HMemIODev::HMemIODev(char *s, size_t len)
244{
245 init();
246 ptr = reinterpret_cast<uchar *>(s);
247 length = len;
248}
249
250
252{
253}
254
255
257{
258 ptr = nullptr;
259 length = 0;
260 pos = 0;
261}
262
263
265{
266 return true;
267}
268
269
271{
272}
273
275{
276 return pos > length;
277}
278
280{
281 return false;
282}
283
284bool HMemIODev::read1b(unsigned char &out)
285{
286 ++pos;
287 if (!state())
288 {
289 out = ptr[pos - 1];
290 return true;
291 }
292 return false;
293}
294
295bool HMemIODev::read1b(char &out)
296{
297 unsigned char tmp8;
298 if (!read1b(tmp8))
299 return false;
300 out = tmp8;
301 return true;
302}
303
304bool HMemIODev::read2b(unsigned short &out)
305{
306 pos += 2;
307 if (!state())
308 {
309 out = ptr[pos - 1] << 8 | ptr[pos - 2];
310 return true;
311 }
312 return false;
313}
314
315bool HMemIODev::read4b(unsigned int &out)
316{
317 pos += 4;
318 if (!state())
319 {
320 out = static_cast<unsigned int>(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
321 ptr[pos - 3] << 8 | ptr[pos - 4]);
322 return true;
323 }
324 return false;
325}
326
327bool HMemIODev::read4b(int &out)
328{
329 unsigned int tmp32;
330 if (!read4b(tmp32))
331 return false;
332 out = tmp32;
333 return true;
334}
335
336size_t HMemIODev::readBlock(void *p, size_t size)
337{
338 if (state())
339 return 0;
340 if (size > length - pos)
341 size = length - pos;
342 memcpy(p, ptr + pos, size);
343 pos += size;
344 return size;
345}
346
347size_t HMemIODev::skipBlock(size_t size)
348{
349 if (state() || size > length - pos)
350 return 0;
351 pos += size;
352 return size;
353}
354
355/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
unsigned int uint
bool compressed
Definition: hiodev.h:42
virtual ~HIODev()
Definition: hiodev.cxx:46
virtual bool read2b(unsigned short &out)=0
virtual bool state() const =0
virtual void init()
Definition: hiodev.cxx:51
virtual bool read4b(unsigned int &out)=0
HIODev()
Definition: hiodev.cxx:40
virtual size_t skipBlock(size_t size) override
Definition: hiodev.cxx:347
virtual size_t readBlock(void *ptr, size_t size) override
Definition: hiodev.cxx:336
virtual bool read1b(unsigned char &out) override
Definition: hiodev.cxx:284
virtual ~HMemIODev() override
Definition: hiodev.cxx:251
virtual void init() override
Definition: hiodev.cxx:256
size_t length
Definition: hiodev.h:138
virtual bool read4b(unsigned int &out) override
Definition: hiodev.cxx:315
virtual bool state() const override
Definition: hiodev.cxx:274
HMemIODev(char *s, size_t len)
Definition: hiodev.cxx:243
virtual bool read2b(unsigned short &out) override
Definition: hiodev.cxx:304
virtual void flush() override
Definition: hiodev.cxx:270
virtual bool setCompressed(bool) override
Definition: hiodev.cxx:279
virtual bool open() override
Definition: hiodev.cxx:264
uchar * ptr
Definition: hiodev.h:137
size_t pos
Definition: hiodev.h:138
virtual size_t readBlock(void *ptr, size_t size) override
Read some bytes from stream to given pointer as amount of size.
Definition: hiodev.cxx:207
virtual void flush() override
If the stream is gzipped, flush the stream.
Definition: hiodev.cxx:121
virtual bool read1b(unsigned char &out) override
Read one byte from stream.
Definition: hiodev.cxx:155
virtual ~HStreamIODev() override
Definition: hiodev.cxx:98
virtual size_t skipBlock(size_t size) override
Move current pointer of stream as amount of size.
Definition: hiodev.cxx:217
virtual bool read2b(unsigned short &out) override
Definition: hiodev.cxx:175
std::unique_ptr< HStream > _stream
Definition: hiodev.h:78
HStreamIODev(std::unique_ptr< HStream > stream)
Definition: hiodev.cxx:92
virtual bool setCompressed(bool) override
Set whether the stream is compressed or not.
Definition: hiodev.cxx:133
virtual bool read4b(unsigned int &out) override
Definition: hiodev.cxx:186
virtual bool open() override
Check whether the stream is available.
Definition: hiodev.cxx:115
gz_stream * _gzfp
Definition: hiodev.h:79
virtual void init() override
Initialize this object.
Definition: hiodev.cxx:108
virtual bool state() const override
Not implemented.
Definition: hiodev.cxx:127
Reference< XOutputStream > stream
gz_stream * gz_open(HStream &_stream)
Opens a gzipped stream for reading.
Definition: hgzip.cxx:47
int gz_close(gz_stream *file)
Flushes all pending output if necessary, closes the compressed stream and deallocates all the (de)com...
Definition: hgzip.cxx:278
int gz_flush(gz_stream *file, int flush)
Flushes all pending output into the compressed file gz_flush should be called only when strictly nece...
Definition: hgzip.cxx:215
#define GZREAD(ptr, len)
Definition: hiodev.cxx:153
static uchar rBuf[BUFSIZE]
Definition: hiodev.cxx:37
const size_t BUFSIZE
Definition: hiodev.cxx:36
unsigned char uchar
Definition: hwplib.h:40
unsigned short ushort
Definition: hwplib.h:41
void * p
size