From 9302ebd75164835b985fd511746689c32cb2d55c Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 4 Jun 2023 15:06:51 +0300 Subject: [PATCH] Abstract FIRSTNBITS(), fix logic --- src/header.h | 2 ++ src/xp.cpp | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/header.h b/src/header.h index 84c308e..6017459 100644 --- a/src/header.h +++ b/src/header.h @@ -39,6 +39,8 @@ #define FIELD_BITS_2003 512 #define FIELD_BYTES_2003 64 +#define FIRSTNBITS(field, n) ((field) & ((1ULL << (n)) - 1)) + // Confirmation ID generator constants #define SUCCESS 0 #define ERR_TOO_SHORT 1 diff --git a/src/xp.cpp b/src/xp.cpp index cabda5a..933ba53 100644 --- a/src/xp.cpp +++ b/src/xp.cpp @@ -24,21 +24,21 @@ void unpackXP(DWORD (&pRaw)[4], DWORD &pSerial, DWORD &pHash, DWORD (&pSignature // log2(24^25) = 114. // Serial = Bits [0..30] -> 31 bits - pSerial = pRaw[0] & 0x7fffffff; + pSerial = FIRSTNBITS(pRaw[0], 31); // Hash (e) = Bits [31..58] -> 28 bits - pHash = ((pRaw[0] >> 31) | (pRaw[1] << 1)) & 0xfffffff; + pHash = FIRSTNBITS(pRaw[1] << 1 | pRaw[0] >> 31, 28); // Signature (s) = Bits [59..113] -> 55 bits - pSignature[0] = (pRaw[1] >> 27) | (pRaw[2] << 5); - pSignature[1] = (pRaw[2] >> 27) | (pRaw[3] << 5); + pSignature[0] = pRaw[2] << 5 | pRaw[1] >> 27; + pSignature[1] = pRaw[3] << 5 | pRaw[2] >> 27; } /* Packs the Windows XP Product Key. */ void packXP(DWORD (&pRaw)[4], DWORD pSerial, DWORD pHash, DWORD (&pSignature)[2]) { - pRaw[0] = pSerial | ((pHash & 1) << 31); - pRaw[1] = (pHash >> 1) | ((pSignature[0] & 0x1f) << 27); - pRaw[2] = (pSignature[0] >> 5) | (pSignature[1] << 27); + pRaw[0] = pSerial | FIRSTNBITS(pHash, 1) << 31; + pRaw[1] = FIRSTNBITS(pSignature[0], 5) << 27 | pHash >> 1; + pRaw[2] = pSignature[1] << 27 | pSignature[0] >> 5; pRaw[3] = pSignature[1] >> 5; }