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
)
137 return nullptr != (_gzfp
= gz_open(*_stream
));
140 gz_flush(_gzfp
, Z_FINISH
);
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);
159 out
= static_cast<unsigned char>(rBuf
[0]);
163 bool HStreamIODev::read1b(char &out
)
172 bool HStreamIODev::read2b(unsigned short &out
)
174 size_t res
= compressed
? GZREAD(rBuf
, 2) : _stream
->readBytes(rBuf
, 2);
179 out
= (static_cast<unsigned char>(rBuf
[1]) << 8 | static_cast<unsigned char>(rBuf
[0]));
183 bool HStreamIODev::read4b(unsigned int &out
)
185 size_t res
= compressed
? GZREAD(rBuf
, 4) : _stream
->readBytes(rBuf
, 4);
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]));
195 bool HStreamIODev::read4b(int &out
)
204 size_t HStreamIODev::readBlock(void *ptr
, size_t size
)
209 : _stream
->readBytes(static_cast<byte
*>(ptr
), size
);
214 size_t HStreamIODev::skipBlock(size_t size
)
217 if( size
<= BUFSIZE
)
218 return GZREAD(rBuf
, size
);
220 size_t remain
= size
;
222 if( remain
> BUFSIZE
) {
223 size_t read
= GZREAD(rBuf
, BUFSIZE
);
229 remain
-= GZREAD(rBuf
, remain
);
233 return size
- remain
;
236 return _stream
->skipBytes(size
);
240 HMemIODev::HMemIODev(char *s
, size_t len
)
243 ptr
= reinterpret_cast<uchar
*>(s
);
248 HMemIODev::~HMemIODev()
253 void HMemIODev::init()
261 bool HMemIODev::open()
267 void HMemIODev::flush()
271 bool HMemIODev::state() const
276 bool HMemIODev::setCompressed(bool )
281 bool HMemIODev::read1b(unsigned char &out
)
292 bool HMemIODev::read1b(char &out
)
301 bool HMemIODev::read2b(unsigned short &out
)
306 out
= ptr
[pos
- 1] << 8 | ptr
[pos
- 2];
312 bool HMemIODev::read4b(unsigned int &out
)
317 out
= static_cast<unsigned int>(ptr
[pos
- 1] << 24 | ptr
[pos
- 2] << 16 |
318 ptr
[pos
- 3] << 8 | ptr
[pos
- 4]);
324 bool HMemIODev::read4b(int &out
)
333 size_t HMemIODev::readBlock(void *p
, size_t size
)
337 if (length
< pos
+ size
)
339 memcpy(p
, ptr
+ pos
, size
);
344 size_t HMemIODev::skipBlock(size_t size
)
346 if (state() || length
< pos
+ size
)
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */