| 1 | /* |
| 2 | * Copyright (C) 2017 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 INCIDENT_HELPER_UTIL_H |
| 18 | #define INCIDENT_HELPER_UTIL_H |
| 19 | |
| 20 | #include <map> |
| 21 | #include <stack> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <android/util/ProtoOutputStream.h> |
| 26 | |
| 27 | using namespace android::util; |
| 28 | |
| 29 | typedef std::vector<std::string> header_t; |
| 30 | typedef std::vector<std::string> record_t; |
| 31 | typedef std::string (*trans_func) (const std::string&); |
| 32 | |
| 33 | const std::string DEFAULT_WHITESPACE = " \t"; |
| 34 | const std::string DEFAULT_NEWLINE = "\r\n"; |
| 35 | const std::string TAB_DELIMITER = "\t"; |
| 36 | const std::string COMMA_DELIMITER = ","; |
| 37 | const std::string PIPE_DELIMITER = "|"; |
| 38 | const std::string PARENTHESES_DELIMITER = "()"; |
| 39 | |
| 40 | // returns true if c is a-zA-Z0-9 or underscore |
| 41 | bool isValidChar(char c); |
| 42 | |
| 43 | // trim the string with the given charset |
| 44 | std::string trim(const std::string& s, const std::string& charset); |
| 45 | |
| 46 | /** |
| 47 | * When a text has a table format like this |
| 48 | * line 1: HeadA HeadB HeadC |
| 49 | * line 2: v1 v2 v3 |
| 50 | * line 3: v11 v12 v13 |
| 51 | * |
| 52 | * We want to parse the line in structure given the delimiter. |
| 53 | * parseHeader is used to parse the firse line of the table and returns a list of strings in lower case |
| 54 | * parseRecord is used to parse other lines and returns a list of strings |
| 55 | * empty strings are skipped |
| 56 | */ |
| 57 | header_t parseHeader(const std::string& line, const std::string& delimiters = DEFAULT_WHITESPACE); |
| 58 | record_t parseRecord(const std::string& line, const std::string& delimiters = DEFAULT_WHITESPACE); |
| 59 | |
| 60 | /** |
| 61 | * Gets the list of end indices of each word in the line and places it in the given vector, |
| 62 | * clearing out the vector beforehand. These indices can be used with parseRecordByColumns. |
| 63 | * Will return false if there was a problem getting the indices. headerNames |
| 64 | * must be NULL terminated. |
| 65 | */ |
| 66 | bool getColumnIndices(std::vector<int>& indices, const char* headerNames[], const std::string& line); |
| 67 | |
| 68 | /** |
| 69 | * When a text-format table aligns by its vertical position, it is not possible to split them by purely delimiters. |
| 70 | * This function allows to parse record by its header's column position' indices, must in ascending order. |
| 71 | * At the same time, it still looks at the char at index, if it doesn't belong to delimiters, moves forward to find the delimiters. |
| 72 | */ |
| 73 | record_t parseRecordByColumns(const std::string& line, const std::vector<int>& indices, const std::string& delimiters = DEFAULT_WHITESPACE); |
| 74 | |
| 75 | /** Prints record_t to stderr */ |
| 76 | void printRecord(const record_t& record); |
| 77 | |
| 78 | /** |
| 79 | * When the line starts/ends with the given key, the function returns true |
| 80 | * as well as the line argument is changed to the rest trimmed part of the original. |
| 81 | * e.g. "ZRAM: 6828K physical used for 31076K in swap (524284K total swap)" becomes |
| 82 | * "6828K physical used for 31076K in swap (524284K total swap)" when given key "ZRAM:", |
| 83 | * otherwise the line is not changed. |
| 84 | * |
| 85 | * In order to prevent two values have same prefix which cause entering to incorrect conditions, |
| 86 | * stripPrefix and stripSuffix can turn on a flag that requires the ending char in the line must not be a valid |
| 87 | * character or digits, this feature is off by default. |
| 88 | * i.e. ABC%some value, ABCD%other value |
| 89 | */ |
| 90 | bool stripPrefix(std::string* line, const char* key, bool endAtDelimiter = false); |
| 91 | bool stripSuffix(std::string* line, const char* key, bool endAtDelimiter = false); |
| 92 | |
| 93 | /** |
| 94 | * behead the given line by the cut, return the head and reassign the line to be the rest. |
| 95 | */ |
| 96 | std::string behead(std::string* line, const char cut); |
| 97 | |
| 98 | /** |
| 99 | * Converts string to the desired type |
| 100 | */ |
| 101 | int toInt(const std::string& s); |
| 102 | long long toLongLong(const std::string& s); |
| 103 | double toDouble(const std::string& s); |
| 104 | |
| 105 | /** |
| 106 | * Reader class reads data from given fd in streaming fashion. |
| 107 | * The buffer size is controlled by capacity parameter. |
| 108 | */ |
| 109 | class Reader |
| 110 | { |
| 111 | public: |
| 112 | explicit Reader(const int fd); |
| 113 | ~Reader(); |
| 114 | |
| 115 | bool readLine(std::string* line); |
| 116 | bool ok(std::string* error); |
| 117 | |
| 118 | private: |
| 119 | FILE* mFile; |
| 120 | char* mBuffer; |
| 121 | std::string mStatus; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * The Table class is constructed from two arrays generated by the given message with |
| 126 | * option (stream_proto.stream_msg).enable_fields_mapping = true. |
| 127 | * The names are each field's names in the message and must corresponding to the header/name of |
| 128 | * the text to be parsed, and the ids are the streaming proto encoded field ids. |
| 129 | * |
| 130 | * This class then allows users to insert the table values to proto based on its header. |
| 131 | * |
| 132 | * Advance feature: if some fields in the message are enums, user must explicitly add the |
| 133 | * mapping from enum name string to its enum values. |
| 134 | */ |
| 135 | class Message; |
| 136 | class Table |
| 137 | { |
| 138 | friend class Message; |
| 139 | public: |
| 140 | Table(const char* names[], const uint64_t ids[], const int count); |
| 141 | ~Table(); |
| 142 | |
| 143 | // Add enum names to values for parsing purpose. |
| 144 | void addEnumTypeMap(const char* field, const char* enumNames[], const int enumValues[], const int enumSize); |
| 145 | |
| 146 | // Manually add enum names to values mapping, useful when an Enum type is used by |
| 147 | // a number of fields, there must not be any enum name conflicts. |
| 148 | void addEnumNameToValue(const char* enumName, const int enumValue); |
| 149 | |
| 150 | // Based on given name, find the right field id, parse the text value and insert to proto. |
| 151 | // Return false if the given name can't be found. |
| 152 | bool insertField(ProtoOutputStream* proto, const std::string& name, const std::string& value); |
| 153 | private: |
| 154 | std::map<std::string, uint64_t> mFields; |
| 155 | std::map<std::string, std::map<std::string, int>> mEnums; |
| 156 | std::map<std::string, int> mEnumValuesByName; |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * Reconstructs a typical proto message given its message Table, adds submessage fields explicitly. |
| 161 | * It allows user to insert nested proto values purely by the names. See insertField for detail. |
| 162 | */ |
| 163 | class Message |
| 164 | { |
| 165 | public: |
| 166 | explicit Message(Table* table); |
| 167 | ~Message(); |
| 168 | |
| 169 | // Reconstructs the typical proto message by adding its message fields. |
| 170 | void addSubMessage(uint64_t fieldId, Message* fieldMsg); |
| 171 | |
| 172 | // Inserts value if the given name has the corresponding field in its message and return true. |
| 173 | // It will recursively search the name in submessages and find the correct field to insert. |
| 174 | // For example, when the name is dalvik_vm_heapsize, and the message's corresponding proto is: |
| 175 | // message Properties { |
| 176 | // message DalvikVm { |
| 177 | // int32 heapsize = 1; |
| 178 | // bool usejit = 2; |
| 179 | // } |
| 180 | // DalvikVm dalvik_vm = 1; |
| 181 | // string hack_in = 2; |
| 182 | // } |
| 183 | // The value will be inserted into field heapsize in dalvik_vm submessage. |
| 184 | // |
| 185 | // Also value belongs to same submessage MUST be inserted contiguously. |
| 186 | // For example, dalvik_vm_usejit must be inserted directly after dalvik_vm_heapsize, otherwise |
| 187 | // if hack_in attempts to be inserted before dalvik_vm_usejit, value of usejit isn't added as expected. |
| 188 | bool insertField(ProtoOutputStream* proto, const std::string& name, const std::string& value); |
| 189 | |
| 190 | // Starts a new message field proto session. |
| 191 | void startSession(ProtoOutputStream* proto, const std::string& name); |
| 192 | |
| 193 | // Ends the previous message field proto session. |
| 194 | void endSession(ProtoOutputStream* proto); |
| 195 | private: |
| 196 | Table* mTable; |
| 197 | std::string mPreviousField; |
| 198 | std::stack<uint64_t> mTokens; |
| 199 | std::map<std::string, Message*> mSubMessages; |
| 200 | }; |
| 201 | |
| 202 | #endif // INCIDENT_HELPER_UTIL_H |