1 // Copyright (C) 2019 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 #pragma once 16 17 #include <string> 18 #include <map> 19 #include <memory> 20 21 #include <google/protobuf/dynamic_message.h> 22 #include <google/protobuf/compiler/importer.h> 23 24 #include "androidfw/ZipFileRO.h" 25 26 #include "apk_errors.h" 27 28 namespace tuningfork { 29 30 namespace pb = google::protobuf; 31 32 class ErrorCollector : public pb::compiler::MultiFileErrorCollector { 33 public: 34 void AddError(const std::string& file_name, int line, int column, 35 const std::string& message) override; 36 }; 37 38 class ZipSourceTree : public pb::compiler::SourceTree { 39 std::shared_ptr<android::ZipFileRO> zip_file_; 40 std::string include_path_; 41 std::string last_error_msg_; 42 int last_error_code_; 43 std::map<std::string, std::vector<char>> file_cache_; 44 45 public: 46 ZipSourceTree(const std::shared_ptr<android::ZipFileRO>& zip_file, 47 const std::string& include_path); 48 pb::io::ZeroCopyInputStream* Open(const std::string& relative_path) override; 49 std::string GetLastErrorMessage() override; 50 int GetLastErrorCode(); 51 const std::shared_ptr<android::ZipFileRO>& ZipFile() const; 52 std::string IncludePath() const; 53 }; 54 55 class ApkValidator { 56 ZipSourceTree source_tree_; 57 ErrorCollector error_collector_; 58 std::unique_ptr<pb::compiler::Importer> importer_; 59 pb::DynamicMessageFactory factory_; 60 bool debug_; 61 62 public: 63 ApkValidator(const std::shared_ptr<android::ZipFileRO>& zip_file, 64 const std::string& asset_path, bool debug); 65 int Validate(bool enforce_enums_in_annotations, 66 bool check_dev_fidelity_params); 67 68 private: 69 int ValidateAnnotation(const pb::Descriptor* desc, 70 bool enforce_enums_in_annotations, 71 std::vector<int>& enum_sizes); 72 int ValidateFidelityParams(const pb::Descriptor* desc); 73 int ValidateSettings(const pb::Descriptor* sdesc, 74 const std::vector<int>& def_ann_enum_size); 75 int ValidateDevFidelityParams(); 76 const pb::Descriptor* FindMessageIgnoringScope( 77 const pb::FileDescriptor* fdesc, 78 const std::string& name); 79 void DebugFileDesc(const pb::FileDescriptor* fdesc); 80 void DebugDesc(const pb::Descriptor* desc); 81 82 }; 83 84 } 85