• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "aemu/base/files/DecompressingStream.h"
16 
17 #include "aemu/base/files/StreamSerializing.h"
18 
19 #include "lz4.h"
20 
21 #include <errno.h>
22 #include <cassert>
23 
24 namespace android {
25 namespace base {
26 
DecompressingStream(Stream & input)27 DecompressingStream::DecompressingStream(Stream& input) {
28     mLzStream = reinterpret_cast<void *>(LZ4_createStreamDecode());
29     loadBuffer(&input, &mBuffer);
30 }
31 
~DecompressingStream()32 DecompressingStream::~DecompressingStream() {
33     LZ4_freeStreamDecode((LZ4_streamDecode_t*)mLzStream);
34 }
35 
read(void * buffer,size_t size)36 ssize_t DecompressingStream::read(void* buffer, size_t size) {
37     assert(mBufferPos < mBuffer.size() ||
38            (mBufferPos == mBuffer.size() && size == 0));
39     if (!size) {
40         return 0;
41     }
42     int read = 0;
43     read = LZ4_decompress_fast_continue(
44            (LZ4_streamDecode_t*)mLzStream, mBuffer.data() + mBufferPos,
45            (char*)buffer, size);
46     if (!read) {
47         return -EIO;
48     }
49     mBufferPos += read;
50     assert(mBufferPos <= mBuffer.size());
51     return size;
52 }
53 
write(const void *,size_t)54 ssize_t DecompressingStream::write(const void*, size_t) {
55     return -EPERM;
56 }
57 
58 }  // namespace base
59 }  // namespace android
60