| 4780 | } |
| 4781 | |
| 4782 | UINT8 FfsEngine::patch(const QModelIndex & index, const QVector<PatchData> & patches) |
| 4783 | { |
| 4784 | if (!index.isValid() || patches.isEmpty() || model->rowCount(index)) |
| 4785 | return ERR_INVALID_PARAMETER; |
| 4786 | |
| 4787 | // Skip removed items |
| 4788 | if (model->action(index) == Actions::Remove) |
| 4789 | return ERR_NOTHING_TO_PATCH; |
| 4790 | |
| 4791 | UINT8 result; |
| 4792 | |
| 4793 | // Apply patches to item's body |
| 4794 | QByteArray body = model->body(index); |
| 4795 | PatchData current; |
| 4796 | Q_FOREACH(current, patches) |
| 4797 | { |
| 4798 | if (current.type == PATCH_TYPE_OFFSET) { |
| 4799 | result = patchViaOffset(body, current.offset, current.hexReplacePattern); |
| 4800 | if (result) |
| 4801 | return result; |
| 4802 | } |
| 4803 | else if (current.type == PATCH_TYPE_PATTERN) { |
| 4804 | result = patchViaPattern(body, current.hexFindPattern, current.hexReplacePattern); |
| 4805 | if (result) |
| 4806 | return result; |
| 4807 | } |
| 4808 | else |
| 4809 | return ERR_UNKNOWN_PATCH_TYPE; |
| 4810 | } |
| 4811 | |
| 4812 | if (body != model->body(index)) { |
| 4813 | QByteArray patched = model->header(index); |
| 4814 | patched.append(body); |
| 4815 | return replace(index, patched, REPLACE_MODE_AS_IS); |
| 4816 | } |
| 4817 | |
| 4818 | return ERR_NOTHING_TO_PATCH; |
| 4819 | } |
| 4820 | |
| 4821 | UINT8 FfsEngine::patchViaOffset(QByteArray & data, const UINT32 offset, const QByteArray & hexReplacePattern) |
| 4822 | { |