1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 // DVO: add zlib/ prefix
26 #include <zlib/zlib.h>
34 #include <osl/diagnose.h>
42 const int BUFSIZE
= 1024;
43 static uchar rBuf
[BUFSIZE
];
45 // HIODev abstract class
63 int HIODev::read1b(void *ptr
, int nmemb
)
65 uchar
*p
= (uchar
*) ptr
;
70 for (ii
= 0; ii
< nmemb
; ii
++)
72 p
[ii
] = sal::static_int_cast
<uchar
>(read1b());
80 int HIODev::read2b(void *ptr
, int nmemb
)
82 ushort
*p
= (ushort
*) ptr
;
87 for (ii
= 0; ii
< nmemb
; ii
++)
89 p
[ii
] = sal::static_int_cast
<uchar
>(read2b());
97 int HIODev::read4b(void *ptr
, int nmemb
)
99 ulong
*p
= (ulong
*) ptr
;
104 for (ii
= 0; ii
< nmemb
; ii
++)
115 HStreamIODev::HStreamIODev(HStream
& stream
):_stream(stream
)
121 HStreamIODev::~HStreamIODev()
127 void HStreamIODev::init()
134 bool HStreamIODev::open()
136 if (!(_stream
.available()))
142 void HStreamIODev::flush(void)
145 gz_flush(_gzfp
, Z_FINISH
);
149 void HStreamIODev::close(void)
151 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
154 gz_close(_gzfp
); /* gz_close() calls stream_closeInput() */
156 _stream
.closeInput();
161 int HStreamIODev::state(void) const
168 bool HStreamIODev::setCompressed(bool flag
)
172 return 0 != (_gzfp
= gz_open(_stream
));
175 gz_flush(_gzfp
, Z_FINISH
);
185 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
187 int HStreamIODev::read1b()
189 int res
= (compressed
) ? GZREAD(rBuf
, 1) : _stream
.readBytes(rBuf
, 1);
194 return (unsigned char) rBuf
[0];
198 int HStreamIODev::read2b()
200 int res
= (compressed
) ? GZREAD(rBuf
, 2) : _stream
.readBytes(rBuf
, 2);
205 return ((unsigned char) rBuf
[1] << 8 | (unsigned char) rBuf
[0]);
209 long HStreamIODev::read4b()
211 int res
= (compressed
) ? GZREAD(rBuf
, 4) : _stream
.readBytes(rBuf
, 4);
216 return ((unsigned char) rBuf
[3] << 24 | (unsigned char) rBuf
[2] << 16 |
217 (unsigned char) rBuf
[1] << 8 | (unsigned char) rBuf
[0]);
221 int HStreamIODev::readBlock(void *ptr
, int size
)
224 (compressed
) ? GZREAD(ptr
, size
) : _stream
.readBytes((byte
*) ptr
,
232 int HStreamIODev::skipBlock(int size
)
235 if( size
<= BUFSIZE
)
236 return GZREAD(rBuf
, size
);
240 if( remain
> BUFSIZE
)
241 remain
-= GZREAD(rBuf
, BUFSIZE
);
243 remain
-= GZREAD(rBuf
, remain
);
247 return size
- remain
;
250 return _stream
.skipBytes(size
);
254 HMemIODev::HMemIODev(char *s
, int len
)
262 HMemIODev::~HMemIODev()
268 void HMemIODev::init()
276 bool HMemIODev::open()
282 void HMemIODev::flush(void)
287 void HMemIODev::close(void)
292 int HMemIODev::state(void) const
301 bool HMemIODev::setCompressed(bool )
307 int HMemIODev::read1b()
316 int HMemIODev::read2b()
320 return ptr
[pos
- 1] << 8 | ptr
[pos
- 2];
326 long HMemIODev::read4b()
330 return DWORD(ptr
[pos
- 1] << 24 | ptr
[pos
- 2] << 16 |
331 ptr
[pos
- 3] << 8 | ptr
[pos
- 4]);
337 int HMemIODev::readBlock(void *p
, int size
)
339 if (length
< pos
+ size
)
341 memcpy(p
, ptr
+ pos
, size
);
347 int HMemIODev::skipBlock(int size
)
349 if (length
< pos
+ size
)
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */