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 "android-base/macros.h"
21 #include "google/protobuf/io/coded_stream.h"
22 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
23 
24 #include "Diagnostics.h"
25 #include "ResourceTable.h"
26 #include "Source.h"
27 #include "proto/ProtoHelpers.h"
28 
29 namespace aapt {
30 
31 class CompiledFileOutputStream {
32  public:
33   explicit CompiledFileOutputStream(
34       google::protobuf::io::ZeroCopyOutputStream* out);
35 
36   void WriteLittleEndian32(uint32_t value);
37   void WriteCompiledFile(const pb::CompiledFile* compiledFile);
38   void WriteData(const BigBuffer* buffer);
39   void WriteData(const void* data, size_t len);
40   bool HadError();
41 
42  private:
43   DISALLOW_COPY_AND_ASSIGN(CompiledFileOutputStream);
44 
45   void EnsureAlignedWrite();
46 
47   google::protobuf::io::CodedOutputStream out_;
48 };
49 
50 class CompiledFileInputStream {
51  public:
52   explicit CompiledFileInputStream(const void* data, size_t size);
53 
54   bool ReadLittleEndian32(uint32_t* outVal);
55   bool ReadCompiledFile(pb::CompiledFile* outVal);
56   bool ReadDataMetaData(uint64_t* outOffset, uint64_t* outLen);
57 
58  private:
59   DISALLOW_COPY_AND_ASSIGN(CompiledFileInputStream);
60 
61   void EnsureAlignedRead();
62 
63   google::protobuf::io::CodedInputStream in_;
64 };
65 
66 std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table);
67 std::unique_ptr<ResourceTable> DeserializeTableFromPb(
68     const pb::ResourceTable& pbTable, const Source& source, IDiagnostics* diag);
69 
70 std::unique_ptr<pb::CompiledFile> SerializeCompiledFileToPb(
71     const ResourceFile& file);
72 std::unique_ptr<ResourceFile> DeserializeCompiledFileFromPb(
73     const pb::CompiledFile& pbFile, const Source& source, IDiagnostics* diag);
74 
75 }  // namespace aapt
76 
77 #endif /* AAPT_FLATTEN_TABLEPROTOSERIALIZER_H */
78