Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 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 | */ |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 16 | #include "json_utils.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 17 | |
| 18 | #include <stdio.h> |
| 19 | |
| 20 | #include <cstring> |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace openpower |
| 25 | { |
| 26 | namespace pels |
| 27 | { |
| 28 | |
| 29 | std::string escapeJSON(const std::string& input) |
| 30 | { |
| 31 | std::string output; |
| 32 | output.reserve(input.length()); |
| 33 | |
| 34 | for (const auto c : input) |
| 35 | { |
| 36 | switch (c) |
| 37 | { |
| 38 | case '"': |
| 39 | output += "\\\""; |
| 40 | break; |
| 41 | case '/': |
| 42 | output += "\\/"; |
| 43 | break; |
| 44 | case '\b': |
| 45 | output += "\\b"; |
| 46 | break; |
| 47 | case '\f': |
| 48 | output += "\\f"; |
| 49 | break; |
| 50 | case '\n': |
| 51 | output += "\\n"; |
| 52 | break; |
| 53 | case '\r': |
| 54 | output += "\\r"; |
| 55 | break; |
| 56 | case '\t': |
| 57 | output += "\\t"; |
| 58 | break; |
| 59 | case '\\': |
| 60 | output += "\\\\"; |
| 61 | break; |
| 62 | default: |
| 63 | output += c; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return output; |
| 69 | } |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 70 | char* dumpHex(const void* data, size_t size, size_t indentCount, bool toJson) |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 71 | { |
| 72 | const int symbolSize = 100; |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 73 | std::string jsonIndent(indentLevel * indentCount, 0x20); |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 74 | if (toJson) |
| 75 | { |
| 76 | jsonIndent.append("\""); |
| 77 | } |
Harisuddin Mohamed Isa | 097ad12 | 2020-06-11 21:19:41 +0800 | [diff] [blame] | 78 | char* buffer = (char*)calloc(std::max(70, 10 * (int)size), sizeof(char)); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 79 | char* symbol = (char*)calloc(symbolSize, sizeof(char)); |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 80 | char* byteCount = (char*)calloc(11, sizeof(char)); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 81 | char ascii[17]; |
| 82 | size_t i, j; |
| 83 | ascii[16] = '\0'; |
| 84 | for (i = 0; i < size; ++i) |
| 85 | { |
| 86 | if (i % 16 == 0) |
| 87 | { |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 88 | if (!toJson) |
| 89 | { |
| 90 | snprintf(byteCount, 11, "%08X ", static_cast<uint32_t>(i)); |
| 91 | strcat(buffer, byteCount); |
| 92 | } |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 93 | strcat(buffer, jsonIndent.c_str()); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 94 | } |
| 95 | snprintf(symbol, symbolSize, "%02X ", ((unsigned char*)data)[i]); |
| 96 | strcat(buffer, symbol); |
| 97 | memset(symbol, 0, strlen(symbol)); |
| 98 | if (((unsigned char*)data)[i] >= ' ' && |
| 99 | ((unsigned char*)data)[i] <= '~') |
| 100 | { |
| 101 | ascii[i % 16] = ((unsigned char*)data)[i]; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | ascii[i % 16] = '.'; |
| 106 | } |
| 107 | if ((i + 1) % 8 == 0 || i + 1 == size) |
| 108 | { |
| 109 | std::string asciiString(ascii); |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 110 | if (toJson) |
| 111 | { |
| 112 | asciiString = escapeJSON(asciiString); |
| 113 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 114 | strcat(buffer, " "); |
| 115 | if ((i + 1) % 16 == 0) |
| 116 | { |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 117 | if (i + 1 != size && toJson) |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 118 | { |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 119 | snprintf(symbol, symbolSize, "| %s\",\n", |
| 120 | asciiString.c_str()); |
| 121 | } |
| 122 | else if (toJson) |
| 123 | { |
| 124 | snprintf(symbol, symbolSize, "| %s\"\n", |
| 125 | asciiString.c_str()); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 129 | snprintf(symbol, symbolSize, "| %s\n", |
| 130 | asciiString.c_str()); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 131 | } |
| 132 | strcat(buffer, symbol); |
| 133 | memset(symbol, 0, strlen(symbol)); |
| 134 | } |
| 135 | else if (i + 1 == size) |
| 136 | { |
| 137 | ascii[(i + 1) % 16] = '\0'; |
| 138 | if ((i + 1) % 16 <= 8) |
| 139 | { |
| 140 | strcat(buffer, " "); |
| 141 | } |
| 142 | for (j = (i + 1) % 16; j < 16; ++j) |
| 143 | { |
| 144 | strcat(buffer, " "); |
| 145 | } |
Aatir | 6a53862 | 2019-11-11 13:13:42 -0600 | [diff] [blame] | 146 | std::string asciiString2(ascii); |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 147 | if (toJson) |
| 148 | { |
| 149 | asciiString2 = escapeJSON(asciiString2); |
| 150 | snprintf(symbol, symbolSize, "| %s\"\n", |
| 151 | asciiString2.c_str()); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | snprintf(symbol, symbolSize, "| %s\n", |
| 156 | asciiString2.c_str()); |
| 157 | } |
| 158 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 159 | strcat(buffer, symbol); |
| 160 | memset(symbol, 0, strlen(symbol)); |
| 161 | } |
| 162 | } |
| 163 | } |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 164 | free(byteCount); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 165 | free(symbol); |
| 166 | return buffer; |
| 167 | } |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 168 | |
| 169 | void jsonInsert(std::string& jsonStr, const std::string& fieldName, |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 170 | std::string fieldValue, uint8_t indentCount) |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 171 | { |
| 172 | const int8_t spacesToAppend = |
| 173 | colAlign - (indentCount * indentLevel) - fieldName.length() - 3; |
| 174 | const std::string jsonIndent(indentCount * indentLevel, 0x20); |
| 175 | jsonStr.append(jsonIndent + "\"" + fieldName + "\":"); |
Harisuddin Mohamed Isa | c32e551 | 2020-02-06 18:05:21 +0800 | [diff] [blame] | 176 | if (spacesToAppend >= 0) |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 177 | { |
| 178 | jsonStr.append(spacesToAppend, 0x20); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | jsonStr.append(1, 0x20); |
| 183 | } |
| 184 | jsonStr.append("\"" + fieldValue + "\",\n"); |
| 185 | } |
| 186 | |
| 187 | void jsonInsertArray(std::string& jsonStr, const std::string& fieldName, |
| 188 | std::vector<std::string>& values, uint8_t indentCount) |
| 189 | { |
| 190 | const std::string jsonIndent(indentCount * indentLevel, 0x20); |
| 191 | if (!values.empty()) |
| 192 | { |
| 193 | jsonStr.append(jsonIndent + "\"" + fieldName + "\": [\n"); |
| 194 | for (size_t i = 0; i < values.size(); i++) |
| 195 | { |
| 196 | jsonStr.append(colAlign, 0x20); |
| 197 | if (i == values.size() - 1) |
| 198 | { |
| 199 | jsonStr.append("\"" + values[i] + "\"\n"); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | jsonStr.append("\"" + values[i] + "\",\n"); |
| 204 | } |
| 205 | } |
| 206 | jsonStr.append(jsonIndent + "],\n"); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | const int8_t spacesToAppend = |
| 211 | colAlign - (indentCount * indentLevel) - fieldName.length() - 3; |
| 212 | jsonStr.append(jsonIndent + "\"" + fieldName + "\":"); |
| 213 | if (spacesToAppend > 0) |
| 214 | { |
| 215 | jsonStr.append(spacesToAppend, 0x20); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | jsonStr.append(1, 0x20); |
| 220 | } |
| 221 | jsonStr.append("[],\n"); |
| 222 | } |
| 223 | } |
Harisuddin Mohamed Isa | e2d1bf3 | 2020-02-06 17:32:38 +0800 | [diff] [blame] | 224 | |
| 225 | std::string trimEnd(std::string s) |
| 226 | { |
| 227 | const char* t = " \t\n\r\f\v"; |
| 228 | if (s.find_last_not_of(t) != std::string::npos) |
| 229 | { |
| 230 | s.erase(s.find_last_not_of(t) + 1); |
| 231 | } |
| 232 | return s; |
| 233 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 234 | } // namespace pels |
| 235 | } // namespace openpower |