From a4c27d029a84ba079e25f3bf58ab1184901c4e0d Mon Sep 17 00:00:00 2001 From: Christian Emmer <10749361+emmercm@users.noreply.github.com> Date: Wed, 15 Nov 2023 21:18:42 -0800 Subject: [PATCH] Fix BPS patch TARGET_COPY reading --- src/types/patches/bpsPatch.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/types/patches/bpsPatch.ts b/src/types/patches/bpsPatch.ts index bde039f89..6758479cd 100644 --- a/src/types/patches/bpsPatch.ts +++ b/src/types/patches/bpsPatch.ts @@ -102,11 +102,18 @@ export default class BPSPatch extends Patch { sourceRelativeOffset += (offset & 1 ? -1 : +1) * (offset >> 1); await targetFile.write(await sourceFile.readAt(sourceRelativeOffset, length)); sourceRelativeOffset += length; - } else { + } else if (action === BPSAction.TARGET_COPY) { const offset = await Patch.readUpsUint(patchFile); targetRelativeOffset += (offset & 1 ? -1 : +1) * (offset >> 1); - await targetFile.write(await targetFile.readAt(targetRelativeOffset, length)); - targetRelativeOffset += length; + // WARN: you explicitly can't read the target file all at once, you have to read byte by + // byte, because later iterations of the loop may need to read data that was changed by + // earlier iterations of the loop. + for (let i = 0; i < length; i += 1) { + await targetFile.write(await targetFile.readAt(targetRelativeOffset, 1)); + targetRelativeOffset += 1; + } + } else { + throw new Error(`BPS action ${action} isn't supported`); } } }