blob: 1e1a218163f042221088a612dbd216506607f519 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/ZipFile.h"
18
Mårten Kongstad02751232018-04-27 13:16:32 +020019#include <memory>
20#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020021
Mårten Kongstad0f763112018-11-19 14:14:37 +010022#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020023
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010024namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020025
26std::unique_ptr<MemoryChunk> MemoryChunk::Allocate(size_t size) {
27 void* ptr = ::operator new(sizeof(MemoryChunk) + size);
28 std::unique_ptr<MemoryChunk> chunk(reinterpret_cast<MemoryChunk*>(ptr));
29 chunk->size = size;
30 return chunk;
31}
32
33std::unique_ptr<const ZipFile> ZipFile::Open(const std::string& path) {
34 ::ZipArchiveHandle handle;
35 int32_t status = ::OpenArchive(path.c_str(), &handle);
36 if (status != 0) {
Mårten Kongstad0955d202019-09-26 15:03:50 +020037 ::CloseArchive(handle);
Mårten Kongstad02751232018-04-27 13:16:32 +020038 return nullptr;
39 }
40 return std::unique_ptr<ZipFile>(new ZipFile(handle));
41}
42
43ZipFile::~ZipFile() {
44 ::CloseArchive(handle_);
45}
46
47std::unique_ptr<const MemoryChunk> ZipFile::Uncompress(const std::string& entryPath) const {
48 ::ZipEntry entry;
Elliott Hughesbf42c722019-05-06 12:22:59 -070049 int32_t status = ::FindEntry(handle_, entryPath, &entry);
Mårten Kongstad02751232018-04-27 13:16:32 +020050 if (status != 0) {
51 return nullptr;
52 }
53 std::unique_ptr<MemoryChunk> chunk = MemoryChunk::Allocate(entry.uncompressed_length);
54 status = ::ExtractToMemory(handle_, &entry, chunk->buf, chunk->size);
55 if (status != 0) {
56 return nullptr;
57 }
58 return chunk;
59}
60
Mårten Kongstad0f763112018-11-19 14:14:37 +010061Result<uint32_t> ZipFile::Crc(const std::string& entryPath) const {
Mårten Kongstad02751232018-04-27 13:16:32 +020062 ::ZipEntry entry;
Elliott Hughesbf42c722019-05-06 12:22:59 -070063 int32_t status = ::FindEntry(handle_, entryPath, &entry);
Mårten Kongstad49d835d2019-01-31 10:50:48 +010064 if (status != 0) {
65 return Error("failed to find zip entry %s", entryPath.c_str());
66 }
67 return entry.crc32;
Mårten Kongstad02751232018-04-27 13:16:32 +020068}
69
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010070} // namespace android::idmap2