1/*
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
20#include "Diagnostics.h"
21#include "ResourceTable.h"
22#include "Source.h"
23#include "proto/ProtoHelpers.h"
24
25#include <android-base/macros.h>
26#include <google/protobuf/io/coded_stream.h>
27#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
28
29namespace aapt {
30
31std::unique_ptr<pb::ResourceTable> serializeTableToPb(ResourceTable* table);
32std::unique_ptr<ResourceTable> deserializeTableFromPb(const pb::ResourceTable& pbTable,
33                                                      const Source& source,
34                                                      IDiagnostics* diag);
35
36std::unique_ptr<pb::CompiledFile> serializeCompiledFileToPb(const ResourceFile& file);
37std::unique_ptr<ResourceFile> deserializeCompiledFileFromPb(const pb::CompiledFile& pbFile,
38                                                            const Source& source,
39                                                            IDiagnostics* diag);
40
41class CompiledFileOutputStream : public google::protobuf::io::CopyingOutputStream {
42public:
43    CompiledFileOutputStream(google::protobuf::io::ZeroCopyOutputStream* out,
44                             pb::CompiledFile* pbFile);
45    bool Write(const void* data, int size) override;
46    bool Finish();
47
48private:
49    bool ensureFileWritten();
50
51    google::protobuf::io::CodedOutputStream mOut;
52    pb::CompiledFile* mPbFile;
53
54    DISALLOW_COPY_AND_ASSIGN(CompiledFileOutputStream);
55};
56
57class CompiledFileInputStream {
58public:
59    CompiledFileInputStream(const void* data, size_t size);
60
61    const pb::CompiledFile* CompiledFile();
62
63    const void* data();
64    size_t size();
65
66private:
67    google::protobuf::io::CodedInputStream mIn;
68    std::unique_ptr<pb::CompiledFile> mPbFile;
69    const uint8_t* mData;
70    size_t mSize;
71
72    DISALLOW_COPY_AND_ASSIGN(CompiledFileInputStream);
73};
74
75} // namespace aapt
76
77#endif /* AAPT_FLATTEN_TABLEPROTOSERIALIZER_H */
78