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 .
29 #include <osl/diagnose.h>
37 const int BUFSIZE
= 1024;
38 static uchar rBuf
[BUFSIZE
];
40 // HIODev abstract class
58 int HIODev::read1b(void *ptr
, int nmemb
)
60 uchar
*p
= (uchar
*) ptr
;
65 for (ii
= 0; ii
< nmemb
; ii
++)
67 p
[ii
] = sal::static_int_cast
<uchar
>(read1b());
75 int HIODev::read2b(void *ptr
, int nmemb
)
77 ushort
*p
= (ushort
*) ptr
;
82 for (ii
= 0; ii
< nmemb
; ii
++)
84 p
[ii
] = sal::static_int_cast
<uchar
>(read2b());
92 int HIODev::read4b(void *ptr
, int nmemb
)
94 ulong
*p
= (ulong
*) ptr
;
99 for (ii
= 0; ii
< nmemb
; ii
++)
110 HStreamIODev::HStreamIODev(HStream
& stream
):_stream(stream
)
116 HStreamIODev::~HStreamIODev()
122 void HStreamIODev::init()
129 bool HStreamIODev::open()
131 if (!(_stream
.available()))
137 void HStreamIODev::flush(void)
140 gz_flush(_gzfp
, Z_FINISH
);
144 void HStreamIODev::close(void)
146 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
149 gz_close(_gzfp
); /* gz_close() calls stream_closeInput() */
151 _stream
.closeInput();
156 int HStreamIODev::state(void) const
163 bool HStreamIODev::setCompressed(bool flag
)
167 return 0 != (_gzfp
= gz_open(_stream
));
170 gz_flush(_gzfp
, Z_FINISH
);
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);
189 return (unsigned char) rBuf
[0];
193 int HStreamIODev::read2b()
195 int res
= (compressed
) ? GZREAD(rBuf
, 2) : _stream
.readBytes(rBuf
, 2);
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);
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
)
219 (compressed
) ? GZREAD(ptr
, size
) : _stream
.readBytes((byte
*) ptr
,
227 int HStreamIODev::skipBlock(int size
)
230 if( size
<= BUFSIZE
)
231 return GZREAD(rBuf
, size
);
235 if( remain
> BUFSIZE
)
236 remain
-= GZREAD(rBuf
, BUFSIZE
);
238 remain
-= GZREAD(rBuf
, remain
);
242 return size
- remain
;
245 return _stream
.skipBytes(size
);
249 HMemIODev::HMemIODev(char *s
, int len
)
257 HMemIODev::~HMemIODev()
263 void HMemIODev::init()
271 bool HMemIODev::open()
277 void HMemIODev::flush(void)
282 void HMemIODev::close(void)
287 int HMemIODev::state(void) const
296 bool HMemIODev::setCompressed(bool )
302 int HMemIODev::read1b()
311 int HMemIODev::read2b()
315 return ptr
[pos
- 1] << 8 | ptr
[pos
- 2];
321 long HMemIODev::read4b()
325 return DWORD(ptr
[pos
- 1] << 24 | ptr
[pos
- 2] << 16 |
326 ptr
[pos
- 3] << 8 | ptr
[pos
- 4]);
332 int HMemIODev::readBlock(void *p
, int size
)
334 if (length
< pos
+ size
)
336 memcpy(p
, ptr
+ pos
, size
);
342 int HMemIODev::skipBlock(int size
)
344 if (length
< pos
+ size
)
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */