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 .
34 #include "hstream.hxx"
36 const size_t BUFSIZE
= 1024;
37 static uchar rBuf
[BUFSIZE
];
39 // HIODev abstract class
56 size_t HIODev::read2b(void *ptr
, size_t nmemb
)
58 ushort
*p
= static_cast<ushort
*>(ptr
);
63 for (ii
= 0; ii
< nmemb
; ++ii
)
73 size_t HIODev::read4b(void *ptr
, size_t nmemb
)
75 uint
*p
= static_cast<uint
*>(ptr
);
80 for (ii
= 0; ii
< nmemb
; ++ii
)
92 HStreamIODev::HStreamIODev(std::unique_ptr
<HStream
> stream
):_stream(std::move(stream
))
98 HStreamIODev::~HStreamIODev()
108 void HStreamIODev::init()
115 bool HStreamIODev::open()
117 return _stream
->available() != 0;
121 void HStreamIODev::flush()
124 gz_flush(_gzfp
, Z_FINISH
);
127 bool HStreamIODev::state() const
133 bool HStreamIODev::setCompressed(bool flag
)
138 _gzfp
= gz_open(*_stream
);
139 return nullptr != _gzfp
;
143 gz_flush(_gzfp
, Z_FINISH
);
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);
162 out
= static_cast<unsigned char>(rBuf
[0]);
166 bool HStreamIODev::read1b(char &out
)
175 bool HStreamIODev::read2b(unsigned short &out
)
177 size_t res
= compressed
? GZREAD(rBuf
, 2) : _stream
->readBytes(rBuf
, 2);
182 out
= (static_cast<unsigned char>(rBuf
[1]) << 8 | static_cast<unsigned char>(rBuf
[0]));
186 bool HStreamIODev::read4b(unsigned int &out
)
188 size_t res
= compressed
? GZREAD(rBuf
, 4) : _stream
->readBytes(rBuf
, 4);
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]));
198 bool HStreamIODev::read4b(int &out
)
207 size_t HStreamIODev::readBlock(void *ptr
, size_t size
)
212 : _stream
->readBytes(static_cast<byte
*>(ptr
), size
);
217 size_t HStreamIODev::skipBlock(size_t size
)
220 if( size
<= BUFSIZE
)
221 return GZREAD(rBuf
, size
);
223 size_t remain
= size
;
225 if( remain
> BUFSIZE
) {
226 size_t read
= GZREAD(rBuf
, BUFSIZE
);
232 remain
-= GZREAD(rBuf
, remain
);
236 return size
- remain
;
239 return _stream
->skipBytes(size
);
243 HMemIODev::HMemIODev(char *s
, size_t len
)
246 ptr
= reinterpret_cast<uchar
*>(s
);
251 HMemIODev::~HMemIODev()
256 void HMemIODev::init()
264 bool HMemIODev::open()
270 void HMemIODev::flush()
274 bool HMemIODev::state() const
279 bool HMemIODev::setCompressed(bool )
284 bool HMemIODev::read1b(unsigned char &out
)
295 bool HMemIODev::read1b(char &out
)
304 bool HMemIODev::read2b(unsigned short &out
)
309 out
= ptr
[pos
- 1] << 8 | ptr
[pos
- 2];
315 bool HMemIODev::read4b(unsigned int &out
)
320 out
= static_cast<unsigned int>(ptr
[pos
- 1] << 24 | ptr
[pos
- 2] << 16 |
321 ptr
[pos
- 3] << 8 | ptr
[pos
- 4]);
327 bool HMemIODev::read4b(int &out
)
336 size_t HMemIODev::readBlock(void *p
, size_t size
)
340 if (size
> length
- pos
)
342 memcpy(p
, ptr
+ pos
, size
);
347 size_t HMemIODev::skipBlock(size_t size
)
349 if (state() || size
> length
- pos
)
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */