Update git submodules
[LibreOffice.git] / hwpfilter / source / hiodev.cxx
blob6437004143cb85e177cbd06003a5aedf91acf7e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <stdio.h>
21 #include <errno.h>
23 #ifdef _WIN32
24 # include <io.h>
25 #else
26 # include <unistd.h>
27 #endif
30 #include "hwplib.h"
31 #include "hgzip.h"
32 #include "hiodev.h"
33 #include "hwpfile.h"
34 #include "hstream.hxx"
36 const size_t BUFSIZE = 1024;
37 static uchar rBuf[BUFSIZE];
39 // HIODev abstract class
40 HIODev::HIODev()
42 init();
46 HIODev::~HIODev()
51 void HIODev::init()
53 compressed = false;
56 size_t HIODev::read2b(void *ptr, size_t nmemb)
58 ushort *p = static_cast<ushort *>(ptr);
60 if (state())
61 return 0;
62 size_t ii;
63 for (ii = 0; ii < nmemb; ++ii)
65 if (!read2b(p[ii]))
66 break;
67 if (state())
68 break;
70 return ii;
73 size_t HIODev::read4b(void *ptr, size_t nmemb)
75 uint *p = static_cast<uint *>(ptr);
77 if (state())
78 return 0;
79 size_t ii;
80 for (ii = 0; ii < nmemb; ++ii)
82 if (!read4b(p[ii]))
83 break;
84 if (state())
85 break;
87 return ii;
91 // hfileiodev class
92 HStreamIODev::HStreamIODev(std::unique_ptr<HStream> stream):_stream(std::move(stream))
94 init();
98 HStreamIODev::~HStreamIODev()
100 /* 플러시한 후 닫는다. */
101 flush();
102 if (_gzfp)
103 gz_close(_gzfp);
104 _gzfp = nullptr;
108 void HStreamIODev::init()
110 _gzfp = nullptr;
111 compressed = false;
115 bool HStreamIODev::open()
117 return _stream->available() != 0;
121 void HStreamIODev::flush()
123 if (_gzfp)
124 gz_flush(_gzfp, Z_FINISH);
127 bool HStreamIODev::state() const
129 return false;
132 /* zlib 관련 부분 */
133 bool HStreamIODev::setCompressed(bool flag)
135 compressed = flag;
136 if (flag)
138 _gzfp = gz_open(*_stream);
139 return nullptr != _gzfp;
141 else if (_gzfp)
143 gz_flush(_gzfp, Z_FINISH);
144 gz_close(_gzfp);
145 _gzfp = nullptr;
147 return true;
151 // IO routines
153 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
155 bool HStreamIODev::read1b(unsigned char &out)
157 size_t res = compressed ? GZREAD(rBuf, 1) : _stream->readBytes(rBuf, 1);
159 if (res < 1)
160 return false;
162 out = static_cast<unsigned char>(rBuf[0]);
163 return true;
166 bool HStreamIODev::read1b(char &out)
168 unsigned char tmp8;
169 if (!read1b(tmp8))
170 return false;
171 out = tmp8;
172 return true;
175 bool HStreamIODev::read2b(unsigned short &out)
177 size_t res = compressed ? GZREAD(rBuf, 2) : _stream->readBytes(rBuf, 2);
179 if (res < 2)
180 return false;
182 out = (static_cast<unsigned char>(rBuf[1]) << 8 | static_cast<unsigned char>(rBuf[0]));
183 return true;
186 bool HStreamIODev::read4b(unsigned int &out)
188 size_t res = compressed ? GZREAD(rBuf, 4) : _stream->readBytes(rBuf, 4);
190 if (res < 4)
191 return false;
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;
198 bool HStreamIODev::read4b(int &out)
200 unsigned int tmp32;
201 if (!read4b(tmp32))
202 return false;
203 out = tmp32;
204 return true;
207 size_t HStreamIODev::readBlock(void *ptr, size_t size)
209 size_t count =
210 compressed
211 ? GZREAD(ptr, size)
212 : _stream->readBytes(static_cast<byte *>(ptr), size);
214 return count;
217 size_t HStreamIODev::skipBlock(size_t size)
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;
231 else{
232 remain -= GZREAD(rBuf, remain);
233 break;
236 return size - remain;
239 return _stream->skipBytes(size);
243 HMemIODev::HMemIODev(char *s, size_t len)
245 init();
246 ptr = reinterpret_cast<uchar *>(s);
247 length = len;
251 HMemIODev::~HMemIODev()
256 void HMemIODev::init()
258 ptr = nullptr;
259 length = 0;
260 pos = 0;
264 bool HMemIODev::open()
266 return true;
270 void HMemIODev::flush()
274 bool HMemIODev::state() const
276 return pos > length;
279 bool HMemIODev::setCompressed(bool )
281 return false;
284 bool HMemIODev::read1b(unsigned char &out)
286 ++pos;
287 if (!state())
289 out = ptr[pos - 1];
290 return true;
292 return false;
295 bool HMemIODev::read1b(char &out)
297 unsigned char tmp8;
298 if (!read1b(tmp8))
299 return false;
300 out = tmp8;
301 return true;
304 bool HMemIODev::read2b(unsigned short &out)
306 pos += 2;
307 if (!state())
309 out = ptr[pos - 1] << 8 | ptr[pos - 2];
310 return true;
312 return false;
315 bool HMemIODev::read4b(unsigned int &out)
317 pos += 4;
318 if (!state())
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;
324 return false;
327 bool HMemIODev::read4b(int &out)
329 unsigned int tmp32;
330 if (!read4b(tmp32))
331 return false;
332 out = tmp32;
333 return true;
336 size_t HMemIODev::readBlock(void *p, size_t size)
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;
347 size_t HMemIODev::skipBlock(size_t size)
349 if (state() || size > length - pos)
350 return 0;
351 pos += size;
352 return size;
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */