Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / hwpfilter / source / hiodev.cxx
blobf7cd75ead04a51058d34894d3824f50036d96b02
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)
137 return nullptr != (_gzfp = gz_open(*_stream));
138 else if (_gzfp)
140 gz_flush(_gzfp, Z_FINISH);
141 gz_close(_gzfp);
142 _gzfp = nullptr;
144 return true;
148 // IO routines
150 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
152 bool HStreamIODev::read1b(unsigned char &out)
154 size_t res = compressed ? GZREAD(rBuf, 1) : _stream->readBytes(rBuf, 1);
156 if (res < 1)
157 return false;
159 out = static_cast<unsigned char>(rBuf[0]);
160 return true;
163 bool HStreamIODev::read1b(char &out)
165 unsigned char tmp8;
166 if (!read1b(tmp8))
167 return false;
168 out = tmp8;
169 return true;
172 bool HStreamIODev::read2b(unsigned short &out)
174 size_t res = compressed ? GZREAD(rBuf, 2) : _stream->readBytes(rBuf, 2);
176 if (res < 2)
177 return false;
179 out = (static_cast<unsigned char>(rBuf[1]) << 8 | static_cast<unsigned char>(rBuf[0]));
180 return true;
183 bool HStreamIODev::read4b(unsigned int &out)
185 size_t res = compressed ? GZREAD(rBuf, 4) : _stream->readBytes(rBuf, 4);
187 if (res < 4)
188 return false;
190 out = (static_cast<unsigned char>(rBuf[3]) << 24 | static_cast<unsigned char>(rBuf[2]) << 16 |
191 static_cast<unsigned char>(rBuf[1]) << 8 | static_cast<unsigned char>(rBuf[0]));
192 return true;
195 bool HStreamIODev::read4b(int &out)
197 unsigned int tmp32;
198 if (!read4b(tmp32))
199 return false;
200 out = tmp32;
201 return true;
204 size_t HStreamIODev::readBlock(void *ptr, size_t size)
206 size_t count =
207 compressed
208 ? GZREAD(ptr, size)
209 : _stream->readBytes(static_cast<byte *>(ptr), size);
211 return count;
214 size_t HStreamIODev::skipBlock(size_t size)
216 if (compressed){
217 if( size <= BUFSIZE )
218 return GZREAD(rBuf, size);
219 else{
220 size_t remain = size;
221 while(remain){
222 if( remain > BUFSIZE ) {
223 size_t read = GZREAD(rBuf, BUFSIZE);
224 remain -= read;
225 if (read != BUFSIZE)
226 break;
228 else{
229 remain -= GZREAD(rBuf, remain);
230 break;
233 return size - remain;
236 return _stream->skipBytes(size);
240 HMemIODev::HMemIODev(char *s, size_t len)
242 init();
243 ptr = reinterpret_cast<uchar *>(s);
244 length = len;
248 HMemIODev::~HMemIODev()
253 void HMemIODev::init()
255 ptr = nullptr;
256 length = 0;
257 pos = 0;
261 bool HMemIODev::open()
263 return true;
267 void HMemIODev::flush()
271 bool HMemIODev::state() const
273 return pos > length;
276 bool HMemIODev::setCompressed(bool )
278 return false;
281 bool HMemIODev::read1b(unsigned char &out)
283 ++pos;
284 if (!state())
286 out = ptr[pos - 1];
287 return true;
289 return false;
292 bool HMemIODev::read1b(char &out)
294 unsigned char tmp8;
295 if (!read1b(tmp8))
296 return false;
297 out = tmp8;
298 return true;
301 bool HMemIODev::read2b(unsigned short &out)
303 pos += 2;
304 if (!state())
306 out = ptr[pos - 1] << 8 | ptr[pos - 2];
307 return true;
309 return false;
312 bool HMemIODev::read4b(unsigned int &out)
314 pos += 4;
315 if (!state())
317 out = static_cast<unsigned int>(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
318 ptr[pos - 3] << 8 | ptr[pos - 4]);
319 return true;
321 return false;
324 bool HMemIODev::read4b(int &out)
326 unsigned int tmp32;
327 if (!read4b(tmp32))
328 return false;
329 out = tmp32;
330 return true;
333 size_t HMemIODev::readBlock(void *p, size_t size)
335 if (state())
336 return 0;
337 if (length < pos + size)
338 size = length - pos;
339 memcpy(p, ptr + pos, size);
340 pos += size;
341 return size;
344 size_t HMemIODev::skipBlock(size_t size)
346 if (state() || length < pos + size)
347 return 0;
348 pos += size;
349 return size;
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */