rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* Copyright Statement: |
| 2 | * |
| 3 | * This software/firmware and related documentation ("MediaTek Software") are |
| 4 | * protected under relevant copyright laws. The information contained herein |
| 5 | * is confidential and proprietary to MediaTek Inc. and/or its licensors. |
| 6 | * Without the prior written permission of MediaTek inc. and/or its licensors, |
| 7 | * any reproduction, modification, use or disclosure of MediaTek Software, |
| 8 | * and information contained herein, in whole or in part, shall be strictly prohibited. |
| 9 | */ |
| 10 | /* MediaTek Inc. (C) 2010. All rights reserved. |
| 11 | * |
| 12 | * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 13 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 14 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON |
| 15 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 18 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 19 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 20 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH |
| 21 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES |
| 22 | * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES |
| 23 | * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK |
| 24 | * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR |
| 25 | * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND |
| 26 | * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 27 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 28 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO |
| 29 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 30 | * |
| 31 | * The following software/firmware and/or related documentation ("MediaTek Software") |
| 32 | * have been modified by MediaTek Inc. All revisions are subject to any receiver's |
| 33 | * applicable license agreements with MediaTek Inc. |
| 34 | */ |
| 35 | #include <string> |
| 36 | #include <algorithm> |
| 37 | using namespace std; |
| 38 | |
| 39 | #include "HexDump.h" |
| 40 | |
| 41 | const char HexDump::HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', |
| 42 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 43 | const char HexDump::HEX_LOWER_CASE_DIGITS[] = { '0', '1', '2', '3', '4', '5', |
| 44 | '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 45 | HexDump::HexDump() { |
| 46 | // TODO Auto-generated constructor stub |
| 47 | |
| 48 | } |
| 49 | |
| 50 | HexDump::~HexDump() { |
| 51 | // TODO Auto-generated destructor stub |
| 52 | } |
| 53 | |
| 54 | std::string HexDump::dumpHexString(uint8_t* array, int length) { |
| 55 | return dumpHexString(array, 0, length); |
| 56 | } |
| 57 | std::string HexDump::dumpHexString(uint8_t* array, uint32_t offset, |
| 58 | int length) { |
| 59 | string result; |
| 60 | |
| 61 | uint8_t line[16] = { 0 }; |
| 62 | int lineIndex = 0; |
| 63 | |
| 64 | result.append("\n0x"); |
| 65 | result.append(toHexString(offset)); |
| 66 | |
| 67 | for (uint32_t i = offset; i < offset + length; i++) { |
| 68 | if (lineIndex == 16) { |
| 69 | result.append(" "); |
| 70 | |
| 71 | for (int j = 0; j < 16; j++) { |
| 72 | if (line[j] > ' ' && line[j] < '~') { |
| 73 | //(unsigned char)line[j] |
| 74 | //result.append(string(line, j, 1)); |
| 75 | result.push_back(line[j]); |
| 76 | } else { |
| 77 | result.append("."); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | result.append("\n0x"); |
| 82 | result.append(toHexString(i)); |
| 83 | lineIndex = 0; |
| 84 | } |
| 85 | |
| 86 | uint8_t b = array[i]; |
| 87 | result.append(" "); |
| 88 | result.push_back(HEX_DIGITS[(b >> 4) & 0x0F]); |
| 89 | result.push_back(HEX_DIGITS[b & 0x0F]); |
| 90 | |
| 91 | line[lineIndex++] = b; |
| 92 | } |
| 93 | |
| 94 | if (lineIndex != 16) { |
| 95 | int count = (16 - lineIndex) * 3; |
| 96 | count++; |
| 97 | for (int i = 0; i < count; i++) { |
| 98 | result.append(" "); |
| 99 | } |
| 100 | |
| 101 | for (int i = 0; i < lineIndex; i++) { |
| 102 | if (line[i] > ' ' && line[i] < '~') { |
| 103 | result.push_back(line[i]); |
| 104 | } else { |
| 105 | result.append("."); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | std::string HexDump::toHexString(uint8_t b) { |
| 113 | int length = 0; |
| 114 | return toHexString(toByteArray(b, &length), length, true); |
| 115 | } |
| 116 | |
| 117 | std::string HexDump::toHexString(uint8_t* array, int length, bool free) { |
| 118 | return toHexString(array, 0, length, true, free); |
| 119 | } |
| 120 | |
| 121 | std::string HexDump::toHexString(std::vector<uint8_t> v) { |
| 122 | int length = v.size(); |
| 123 | uint8_t array[length]; |
| 124 | std::copy(v.begin(), v.end(), array); |
| 125 | return toHexString(array, 0, length, true, false); |
| 126 | } |
| 127 | |
| 128 | std::string HexDump::toHexString(uint8_t* array, int length, bool upperCase, |
| 129 | bool free) { |
| 130 | return toHexString(array, 0, length, upperCase); |
| 131 | } |
| 132 | |
| 133 | std::string HexDump::toHexString(uint8_t* array, int offset, int length, |
| 134 | bool free) { |
| 135 | return toHexString(array, offset, length, true); |
| 136 | } |
| 137 | |
| 138 | std::string HexDump::toHexString(uint8_t* array, int offset, int length, |
| 139 | bool upperCase, bool free) { |
| 140 | const char* digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS; |
| 141 | char buf[length * 2]; |
| 142 | |
| 143 | int bufIndex = 0; |
| 144 | for (int i = offset; i < offset + length; i++) { |
| 145 | uint8_t b = array[i]; |
| 146 | buf[bufIndex++] = digits[(b >> 4) & 0x0F]; |
| 147 | buf[bufIndex++] = digits[b & 0x0F]; |
| 148 | } |
| 149 | string str(buf, (length * 2)); |
| 150 | if (free) { |
| 151 | delete[] array; |
| 152 | } |
| 153 | return str; |
| 154 | } |
| 155 | |
| 156 | std::string HexDump::toHexString(uint32_t i) { |
| 157 | int length = 0; |
| 158 | return toHexString(toByteArray(i, &length), length, true); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @return need free. |
| 163 | */ |
| 164 | uint8_t* HexDump::toByteArray(uint8_t b, int* length) { |
| 165 | uint8_t* array = new uint8_t[1]; |
| 166 | (*length) = 1; |
| 167 | array[0] = b; |
| 168 | return array; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return need free; |
| 173 | */ |
| 174 | uint8_t* HexDump::toByteArray(uint32_t i, int* length) { |
| 175 | uint8_t* array = new uint8_t[4]; |
| 176 | (*length) = 4; |
| 177 | array[3] = (uint8_t) (i & 0xFF); |
| 178 | array[2] = (uint8_t) ((i >> 8) & 0xFF); |
| 179 | array[1] = (uint8_t) ((i >> 16) & 0xFF); |
| 180 | array[0] = (uint8_t) ((i >> 24) & 0xFF); |
| 181 | |
| 182 | return array; |
| 183 | } |
| 184 | |
| 185 | uint32_t HexDump::toByte(char c) { |
| 186 | if (c >= '0' && c <= '9') { |
| 187 | return (c - '0'); |
| 188 | } |
| 189 | if (c >= 'A' && c <= 'F') { |
| 190 | return (c - 'A' + 10); |
| 191 | } |
| 192 | if (c >= 'a' && c <= 'f') { |
| 193 | return (c - 'a' + 10); |
| 194 | } |
| 195 | return 0; //wrong |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @return need free |
| 200 | */ |
| 201 | uint8_t* HexDump::hexStringToByteArray(std::string hexString, int* length) { |
| 202 | int len = hexString.length(); |
| 203 | uint8_t* buffer = new uint8_t[len / 2]; |
| 204 | |
| 205 | for (int i = 0; i < len; i += 2) { |
| 206 | buffer[i / 2] = (uint8_t) ((toByte(hexString[i]) << 4) |
| 207 | | toByte(hexString[i + 1])); |
| 208 | } |
| 209 | (*length) = len; |
| 210 | return buffer; |
| 211 | } |
| 212 | std::string HexDump::appendByteAsHex(std::string& sb, uint8_t b, |
| 213 | bool upperCase) { |
| 214 | const char* digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS; |
| 215 | sb.push_back(digits[(b >> 4) & 0xf]); |
| 216 | sb.push_back(digits[b & 0xf]); |
| 217 | return sb; |
| 218 | } |