| 4819 | } |
| 4820 | |
| 4821 | UINT8 FfsEngine::patchViaOffset(QByteArray & data, const UINT32 offset, const QByteArray & hexReplacePattern) |
| 4822 | { |
| 4823 | QByteArray body = data; |
| 4824 | |
| 4825 | // Skip patterns with odd length |
| 4826 | if (hexReplacePattern.length() % 2 > 0) |
| 4827 | return ERR_INVALID_PARAMETER; |
| 4828 | |
| 4829 | // Check offset bounds |
| 4830 | if (offset > (UINT32)(body.length() - hexReplacePattern.length() / 2)) |
| 4831 | return ERR_PATCH_OFFSET_OUT_OF_BOUNDS; |
| 4832 | |
| 4833 | // Parse replace pattern |
| 4834 | QByteArray replacePattern; |
| 4835 | bool converted; |
| 4836 | for (int i = 0; i < hexReplacePattern.length() / 2; i++) { |
| 4837 | QByteArray hex = hexReplacePattern.mid(2 * i, 2); |
| 4838 | UINT8 value = 0; |
| 4839 | |
| 4840 | if (!hex.contains('.')) { // Normal byte pattern |
| 4841 | value = (UINT8)hex.toUShort(&converted, 16); |
| 4842 | if (!converted) |
| 4843 | return ERR_INVALID_SYMBOL; |
| 4844 | } |
| 4845 | else { // Placeholder byte pattern |
| 4846 | if (hex[0] == '.' && hex[1] == '.') { // Full byte placeholder |
| 4847 | value = body.at(offset + i); |
| 4848 | } |
| 4849 | else if (hex[0] == '.') {// Upper byte part placeholder |
| 4850 | hex[0] = '0'; |
| 4851 | value = (UINT8)(body.at(offset + i) & 0xF0); |
| 4852 | value += (UINT8)hex.toUShort(&converted, 16); |
| 4853 | if (!converted) |
| 4854 | return ERR_INVALID_SYMBOL; |
| 4855 | } |
| 4856 | else if (hex[1] == '.') { // Lower byte part placeholder |
| 4857 | hex[1] = '0'; |
| 4858 | value = (UINT8)(body.at(offset + i) & 0x0F); |
| 4859 | value += (UINT8)hex.toUShort(&converted, 16); |
| 4860 | if (!converted) |
| 4861 | return ERR_INVALID_SYMBOL; |
| 4862 | } |
| 4863 | else |
| 4864 | return ERR_INVALID_SYMBOL; |
| 4865 | } |
| 4866 | |
| 4867 | // Append calculated value to real pattern |
| 4868 | replacePattern.append(value); |
| 4869 | } |
| 4870 | |
| 4871 | body.replace(offset, replacePattern.length(), replacePattern); |
| 4872 | msg(tr("patch: replaced %1 bytes at offset %2h %3 -> %4") |
| 4873 | .arg(replacePattern.length()) |
| 4874 | .hexarg(offset) |
| 4875 | .arg(QString(data.mid(offset, replacePattern.length()).toHex()).toUpper()) |
| 4876 | .arg(QString(replacePattern.toHex()).toUpper())); |
| 4877 | data = body; |
| 4878 | return ERR_SUCCESS; |