From 08cf2e430ef29adb2fd0e987bf0fb93622b566e9 Mon Sep 17 00:00:00 2001 From: YoungJules Date: Mon, 31 Aug 2026 13:14:35 +0200 Subject: [PATCH] Added possibilty to write a found stamp to a binary file. --- client/experimental_lib/KGH_TOOLS.md | 3 ++ .../example_c/legic_migrate_tool.c | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/client/experimental_lib/KGH_TOOLS.md b/client/experimental_lib/KGH_TOOLS.md index 26a7c93d4..c646e2c47 100644 --- a/client/experimental_lib/KGH_TOOLS.md +++ b/client/experimental_lib/KGH_TOOLS.md @@ -9,6 +9,8 @@ Examples: ```bash ./kgh_tools COM13 read_badge_number ./kgh_tools COM13 read_info +./kgh_tools COM13 read_stamp +./kgh_tools COM13 read_stamp -o stamp.bin ./kgh_tools COM13 write_card 600001 --stamp-file stamp.bin ``` @@ -16,6 +18,7 @@ Commands: - `read_badge_number` prints the badge number only. - `read_info` prints one-line JSON. +- `read_stamp` prints the stamp as 8 hex chars, or writes raw 4 bytes with `-o/--output-file`. - `write_card (--stamp-file | --stamp-hex <8hex>)` writes the card. Notes: diff --git a/client/experimental_lib/example_c/legic_migrate_tool.c b/client/experimental_lib/example_c/legic_migrate_tool.c index 1f4d298a6..bedbcef3a 100644 --- a/client/experimental_lib/example_c/legic_migrate_tool.c +++ b/client/experimental_lib/example_c/legic_migrate_tool.c @@ -125,6 +125,17 @@ static bool load_stamp_file(const char *path, uint8_t stamp[4]) { return true; } +static bool save_stamp_file(const char *path, const uint8_t stamp[4]) { + FILE *fp = fopen(path, "wb"); + if (fp == NULL) { + return false; + } + + size_t written = fwrite(stamp, 1, 4, fp); + fclose(fp); + return written == 4; +} + static bool get_arg_value(int argc, char *argv[], const char *flag, const char **value) { for (int i = 3; i < argc - 1; ++i) { if (strcmp(argv[i], flag) == 0) { @@ -380,6 +391,15 @@ static bool decode_badge_value(const uint8_t *dump, uint16_t len, uint32_t *badg return decode_badge_bcd(&dump[31], badge_out); } +static bool extract_stamp(const uint8_t *dump, uint16_t len, uint8_t stamp[4]) { + if (len <= 30) { + return false; + } + + memcpy(stamp, dump + 27, 4); + return true; +} + static void print_json_string(const char *key, const char *value, bool last) { printf("\"%s\": \"%s\"%s", key, value, last ? "" : ","); } @@ -510,6 +530,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Usage:\n"); fprintf(stderr, " %s read_badge_number [-v|--verbose]\n", argv[0]); fprintf(stderr, " %s read_info\n", argv[0]); + fprintf(stderr, " %s read_stamp [-o|--output-file ]\n", argv[0]); fprintf(stderr, " %s write_card (--stamp-file | --stamp-hex <8hex>)\n", argv[0]); return EXIT_FAILURE; } @@ -535,6 +556,29 @@ int main(int argc, char *argv[]) { } else if (verbose) { fprintf(stderr, "failed to read card info\n"); } + } else if (strcmp(argv[2], "read_stamp") == 0) { + const char *output_file = NULL; + bool has_output = get_arg_value(argc, argv, "-o", &output_file) || get_arg_value(argc, argv, "--output-file", &output_file); + + legic_read_result_t res; + if (read_legic_dump(&res, verbose)) { + uint8_t stamp[4] = {0}; + if (extract_stamp(res.dump, res.len, stamp)) { + if (has_output) { + if (save_stamp_file(output_file, stamp)) { + rc = EXIT_SUCCESS; + } else { + fprintf(stderr, "failed to write stamp file\n"); + } + } else { + printf("%02X%02X%02X%02X\n", stamp[0], stamp[1], stamp[2], stamp[3]); + rc = EXIT_SUCCESS; + } + } + free_legic_read_result(&res); + } else if (verbose) { + fprintf(stderr, "failed to read stamp\n"); + } } else if (strcmp(argv[2], "write_card") == 0) { if (argc < 4) { fprintf(stderr, "missing badge number\n");