From 9f83c9307d701de81f19419392f512f45802223a Mon Sep 17 00:00:00 2001 From: munzzyy Date: Sat, 4 Jul 2026 18:31:02 -0500 Subject: [PATCH] Fix lf em 4x05 dump writing corrupted (byte-swapped) dump files em4x05_print_blocks() byte-swaps its buffer in place to display the blocks. In CmdEM4x05Dump() that call runs before pm3_save_dump(), so the buffer handed to the saver has been swapped out of the canonical on-disk order and the saved .bin/.json reloads with every block reversed. The read loop stores each word as data[addr] = BSWAP_32(word), which is what the loader in CmdEM4x05View() expects (it reads blocks MSB-first via bytes_to_num()). The extra in-place swap in the print routine undoes that right before the save. It also corrupts the auto-generated filename, which is built from BSWAP_32(data[1]) after the print call. Symmetric words such as 0x00000000 survive the swap, so it's easy to miss. Give em4x05_print_blocks() a private copy to swap and print, and take the input as const so it can't mutate the caller's buffer. On-screen output is unchanged; the dump path now saves the untouched buffer. Fixes #3403 --- CHANGELOG.md | 1 + client/src/cmdlfem4x05.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e39702d7..6a0a18f0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac ## [unreleased][unreleased] +- Fixed `lf em 4x05 dump` writing byte-swapped (corrupted) block data to the saved dump file (@munzzyy) - Added `hf 14b dump` support for Standard ISO14443-B tags (@thisiscamk) - Added `hf 14b ctrdbl` and `hf 14b ctdump` commands for interacting with ASK CTS tags (@kormax) - Fixed `hf legic migrate` failing to parse the optional DCF argument as hex (@IdanHo) diff --git a/client/src/cmdlfem4x05.c b/client/src/cmdlfem4x05.c index 17e1b73be..80940c5f2 100644 --- a/client/src/cmdlfem4x05.c +++ b/client/src/cmdlfem4x05.c @@ -934,14 +934,20 @@ static void em4x05_print_footer(void) { PrintAndLogEx(NORMAL, ""); } -static void em4x05_print_blocks(em_tech_type_t cardtype, uint8_t *data, uint8_t dlen) { +static void em4x05_print_blocks(em_tech_type_t cardtype, const uint8_t *src, uint8_t dlen) { // must have 4 byte alignment - if ((data == NULL) || (dlen % EM4X05_BLOCK_SIZE) != 0) { + if ((src == NULL) || (dlen % EM4X05_BLOCK_SIZE) != 0) { return; } - uint32_t *d = (void *)data; + // Work on a private copy. This routine byte-swaps the buffer for display, + // and callers such as CmdEM4x05Dump() still need the original bytes + // afterwards to save an uncorrupted dump to file (issue #3403). + // dlen is a uint8_t, so 64 words is enough to hold any valid input. + uint32_t d[64] = {0}; + memcpy(d, src, dlen); + uint8_t *data = (uint8_t *)d; uint8_t i; for (i = 0; i < (dlen >> 2); i++) {