bump product version to 4.1.6.2
[LibreOffice.git] / hwpfilter / source / hiodev.cxx
blob5de1b5c7e890d92cf4ffcb732a982d4b345446c4
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
29 #include <osl/diagnose.h>
31 #include "hwplib.h"
32 #include "hgzip.h"
33 #include "hiodev.h"
34 #include "hwpfile.h"
35 #include "hstream.h"
37 const int BUFSIZE = 1024;
38 static uchar rBuf[BUFSIZE];
40 // HIODev abstract class
41 HIODev::HIODev()
43 init();
47 HIODev::~HIODev()
52 void HIODev::init()
54 compressed = false;
58 int HIODev::read1b(void *ptr, int nmemb)
60 uchar *p = (uchar *) ptr;
61 int ii;
63 if (state())
64 return -1;
65 for (ii = 0; ii < nmemb; ii++)
67 p[ii] = sal::static_int_cast<uchar>(read1b());
68 if (state())
69 break;
71 return ii;
75 int HIODev::read2b(void *ptr, int nmemb)
77 ushort *p = (ushort *) ptr;
78 int ii;
80 if (state())
81 return -1;
82 for (ii = 0; ii < nmemb; ii++)
84 p[ii] = sal::static_int_cast<uchar>(read2b());
85 if (state())
86 break;
88 return ii;
92 int HIODev::read4b(void *ptr, int nmemb)
94 ulong *p = (ulong *) ptr;
95 int ii;
97 if (state())
98 return -1;
99 for (ii = 0; ii < nmemb; ii++)
101 p[ii] = read4b();
102 if (state())
103 break;
105 return ii;
109 // hfileiodev class
110 HStreamIODev::HStreamIODev(HStream & stream):_stream(stream)
112 init();
116 HStreamIODev::~HStreamIODev()
118 close();
122 void HStreamIODev::init()
124 _gzfp = NULL;
125 compressed = false;
129 bool HStreamIODev::open()
131 if (!(_stream.available()))
132 return false;
133 return true;
137 void HStreamIODev::flush(void)
139 if (_gzfp)
140 gz_flush(_gzfp, Z_FINISH);
144 void HStreamIODev::close(void)
146 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
147 this->flush();
148 if (_gzfp)
149 gz_close(_gzfp); /* gz_close() calls stream_closeInput() */
150 else
151 _stream.closeInput();
152 _gzfp = NULL;
156 int HStreamIODev::state(void) const
158 return 0;
162 /* zlib °ü·Ã ºÎºÐ */
163 bool HStreamIODev::setCompressed(bool flag)
165 compressed = flag;
166 if (flag == true)
167 return 0 != (_gzfp = gz_open(_stream));
168 else if (_gzfp)
170 gz_flush(_gzfp, Z_FINISH);
171 gz_close(_gzfp);
172 _gzfp = 0;
174 return true;
178 // IO routines
180 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
182 int HStreamIODev::read1b()
184 int res = (compressed) ? GZREAD(rBuf, 1) : _stream.readBytes(rBuf, 1);
186 if (res <= 0)
187 return -1;
188 else
189 return (unsigned char) rBuf[0];
193 int HStreamIODev::read2b()
195 int res = (compressed) ? GZREAD(rBuf, 2) : _stream.readBytes(rBuf, 2);
197 if (res <= 0)
198 return -1;
199 else
200 return ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
204 long HStreamIODev::read4b()
206 int res = (compressed) ? GZREAD(rBuf, 4) : _stream.readBytes(rBuf, 4);
208 if (res <= 0)
209 return -1;
210 else
211 return ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
212 (unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
216 int HStreamIODev::readBlock(void *ptr, int size)
218 int count =
219 (compressed) ? GZREAD(ptr, size) : _stream.readBytes((byte *) ptr,
221 size);
223 return count;
227 int HStreamIODev::skipBlock(int size)
229 if (compressed){
230 if( size <= BUFSIZE )
231 return GZREAD(rBuf, size);
232 else{
233 int remain = size;
234 while(remain){
235 if( remain > BUFSIZE )
236 remain -= GZREAD(rBuf, BUFSIZE);
237 else{
238 remain -= GZREAD(rBuf, remain);
239 break;
242 return size - remain;
245 return _stream.skipBytes(size);
249 HMemIODev::HMemIODev(char *s, int len)
251 init();
252 ptr = (uchar *) s;
253 length = len;
257 HMemIODev::~HMemIODev()
259 close();
263 void HMemIODev::init()
265 ptr = 0;
266 length = 0;
267 pos = 0;
271 bool HMemIODev::open()
273 return true;
277 void HMemIODev::flush(void)
282 void HMemIODev::close(void)
287 int HMemIODev::state(void) const
289 if (pos <= length)
290 return 0;
291 else
292 return -1;
296 bool HMemIODev::setCompressed(bool )
298 return false;
302 int HMemIODev::read1b()
304 if (pos <= length)
305 return ptr[pos++];
306 else
307 return 0;
311 int HMemIODev::read2b()
313 pos += 2;
314 if (pos <= length)
315 return ptr[pos - 1] << 8 | ptr[pos - 2];
316 else
317 return 0;
321 long HMemIODev::read4b()
323 pos += 4;
324 if (pos <= length)
325 return DWORD(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
326 ptr[pos - 3] << 8 | ptr[pos - 4]);
327 else
328 return 0;
332 int HMemIODev::readBlock(void *p, int size)
334 if (length < pos + size)
335 size = length - pos;
336 memcpy(p, ptr + pos, size);
337 pos += size;
338 return size;
342 int HMemIODev::skipBlock(int size)
344 if (length < pos + size)
345 return 0;
346 pos += size;
347 return size;
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */