blob: 8c46642e9090abe053880180b90c2040671a4690 [file] [log] [blame]
Adam Lesinski59e04c62016-02-04 15:59:23 -08001/*
2 * Copyright (C) 2016 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
17#ifndef AAPT_FLATTEN_TABLEPROTOSERIALIZER_H
18#define AAPT_FLATTEN_TABLEPROTOSERIALIZER_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include "android-base/macros.h"
21#include "google/protobuf/io/coded_stream.h"
22#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
23
Adam Lesinski59e04c62016-02-04 15:59:23 -080024#include "Diagnostics.h"
25#include "ResourceTable.h"
26#include "Source.h"
27#include "proto/ProtoHelpers.h"
28
Adam Lesinski59e04c62016-02-04 15:59:23 -080029namespace aapt {
30
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070031class CompiledFileOutputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 public:
Adam Lesinski4ffea042017-08-10 15:37:28 -070033 explicit CompiledFileOutputStream(google::protobuf::io::ZeroCopyOutputStream* out);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 void WriteLittleEndian32(uint32_t value);
Adam Lesinski4ffea042017-08-10 15:37:28 -070036 void WriteCompiledFile(const pb::internal::CompiledFile* compiledFile);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 void WriteData(const BigBuffer* buffer);
38 void WriteData(const void* data, size_t len);
39 bool HadError();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 private:
42 DISALLOW_COPY_AND_ASSIGN(CompiledFileOutputStream);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 void EnsureAlignedWrite();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 google::protobuf::io::CodedOutputStream out_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070047};
48
49class CompiledFileInputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 public:
51 explicit CompiledFileInputStream(const void* data, size_t size);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 bool ReadLittleEndian32(uint32_t* outVal);
Adam Lesinski4ffea042017-08-10 15:37:28 -070054 bool ReadCompiledFile(pb::internal::CompiledFile* outVal);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 bool ReadDataMetaData(uint64_t* outOffset, uint64_t* outLen);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 private:
58 DISALLOW_COPY_AND_ASSIGN(CompiledFileInputStream);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070059
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 void EnsureAlignedRead();
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 google::protobuf::io::CodedInputStream in_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070063};
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table);
Adam Lesinski4ffea042017-08-10 15:37:28 -070066std::unique_ptr<ResourceTable> DeserializeTableFromPb(const pb::ResourceTable& pbTable,
67 const Source& source, IDiagnostics* diag);
Adam Lesinski59e04c62016-02-04 15:59:23 -080068
Adam Lesinski4ffea042017-08-10 15:37:28 -070069std::unique_ptr<pb::internal::CompiledFile> SerializeCompiledFileToPb(const ResourceFile& file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070std::unique_ptr<ResourceFile> DeserializeCompiledFileFromPb(
Adam Lesinski4ffea042017-08-10 15:37:28 -070071 const pb::internal::CompiledFile& pbFile, const Source& source, IDiagnostics* diag);
Adam Lesinski59e04c62016-02-04 15:59:23 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073} // namespace aapt
Adam Lesinski59e04c62016-02-04 15:59:23 -080074
75#endif /* AAPT_FLATTEN_TABLEPROTOSERIALIZER_H */