mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2026-05-24 10:15:18 +00:00
style
This commit is contained in:
+1
-1
@@ -594,7 +594,7 @@ void felica_sendraw(const PacketCommandNG *c) {
|
||||
|
||||
if (g_dbglevel >= DBG_DEBUG) {
|
||||
Dbprintf("Transmit Frame (no CRC shown):");
|
||||
// 0,1,2, n
|
||||
// 0,1,2, n
|
||||
Dbhexdump(len + 1 + 2, buf, 0);
|
||||
// total buffer length: len + 1 len byte + 2 sync bytes + 2 crc bytes
|
||||
Dbprintf("Buffer Length: %i", buf[2] + 4);
|
||||
|
||||
@@ -36,7 +36,7 @@ DDF_PPSE = b"2PAY.SYS.DDF01"
|
||||
|
||||
class BridgePM3:
|
||||
"""Bridge class for communicating with Proxmark3 device."""
|
||||
|
||||
|
||||
def __init__(self, hw_debug: bool, pm3: Any = None):
|
||||
self._debug = hw_debug
|
||||
if pm3 is None:
|
||||
@@ -48,7 +48,7 @@ class BridgePM3:
|
||||
"""Receive data from PM3."""
|
||||
if self.recv_buff is None:
|
||||
raise ValueError("No data in receive buffer")
|
||||
|
||||
|
||||
ret_buff = bytes.fromhex(self.recv_buff)
|
||||
|
||||
if self._debug:
|
||||
@@ -62,7 +62,7 @@ class BridgePM3:
|
||||
def hw_reset(self) -> None:
|
||||
"""Reset the Proxmark3 hardware."""
|
||||
self.pm3.console("hw reset")
|
||||
|
||||
|
||||
def send(self, data: bytes, select: bool = False) -> None:
|
||||
"""Send APDU command to card via PM3."""
|
||||
|
||||
@@ -70,7 +70,7 @@ class BridgePM3:
|
||||
|
||||
if select:
|
||||
exec_cmd += "s" # activate field and select card
|
||||
|
||||
|
||||
exec_cmd += "d " # full APDU package
|
||||
|
||||
# Convert bytearray to string
|
||||
@@ -113,7 +113,7 @@ class BridgePM3:
|
||||
if recvdata is None:
|
||||
raise ValueError("Did not receive any data from PM3")
|
||||
return recvdata
|
||||
|
||||
|
||||
def waitForCard(self, max_tries: int = MAX_CARD_POLL_TRIES) -> bool:
|
||||
"""Poll for a card up to max_tries. Return True if found, else False."""
|
||||
tries = 0
|
||||
@@ -276,43 +276,43 @@ def parse_return_code(ret_code: Optional[bytes], console_print: bool = True) ->
|
||||
def parse_tlv(data: bytes, tag_length: int) -> List[Tuple[bytes, bytes]]:
|
||||
"""
|
||||
Parse TLV (Tag-Length-Value) structure.
|
||||
|
||||
|
||||
Args:
|
||||
data: bytes or bytearray containing TLV data
|
||||
tag_length: int, number of bytes for the tag field (must be > 0)
|
||||
|
||||
|
||||
Returns:
|
||||
list of tuples: [(tag, value), (tag, value), ...]
|
||||
"""
|
||||
if tag_length <= 0:
|
||||
raise ValueError(f"Invalid tag_length: {tag_length}, must be > 0")
|
||||
|
||||
|
||||
result = []
|
||||
offset = 0
|
||||
|
||||
|
||||
while offset < len(data):
|
||||
# Check if we have enough bytes for tag and length
|
||||
if offset + tag_length + 1 > len(data):
|
||||
break
|
||||
|
||||
|
||||
# Extract tag
|
||||
tag = data[offset:offset + tag_length]
|
||||
offset += tag_length
|
||||
|
||||
|
||||
# Extract length (assuming 1 byte for length)
|
||||
length = data[offset]
|
||||
offset += 1
|
||||
|
||||
|
||||
# Check if we have enough bytes for value
|
||||
if offset + length > len(data):
|
||||
break
|
||||
|
||||
|
||||
# Extract value
|
||||
value = data[offset:offset + length]
|
||||
offset += length
|
||||
|
||||
|
||||
result.append((tag, value))
|
||||
|
||||
|
||||
return result
|
||||
|
||||
# https://github.com/SocialSisterYi/T-Union_Master/blob/857ffec87d67413e759c5e055e6a410a93536b2e/src/protocol/t_union_poller_i.c#L88
|
||||
@@ -320,7 +320,7 @@ def parse_tunion_meta(level: int, tlv_data: bytes) -> None:
|
||||
"""Parse T-Union card metadata from TLV data."""
|
||||
if len(tlv_data) < 0x1C:
|
||||
raise ValueError(f"TLV data too short: {len(tlv_data)} bytes, expected at least 28")
|
||||
|
||||
|
||||
card_type = tlv_data[0]
|
||||
city_id = int.from_bytes(tlv_data[1:3], byteorder='big')
|
||||
card_number = tlv_data[10:20].hex().upper()
|
||||
@@ -346,7 +346,7 @@ def decode_transaction(data: bytes) -> None:
|
||||
"""Decode and display transaction record."""
|
||||
if len(data) < 23:
|
||||
raise ValueError(f"Transaction data too short: {len(data)} bytes, expected at least 23")
|
||||
|
||||
|
||||
sequence = int.from_bytes(data[0:2], byteorder='big')
|
||||
money = int.from_bytes(data[5:9], byteorder='big')
|
||||
trans_type = data[9]
|
||||
@@ -379,7 +379,7 @@ def decode_travel(data: bytes) -> None:
|
||||
"""Decode and display travel record."""
|
||||
if len(data) < 42:
|
||||
raise ValueError(f"Travel data too short: {len(data)} bytes, expected at least 42")
|
||||
|
||||
|
||||
travel_type = data[0]
|
||||
terminal_id = data[1:9].hex().upper()
|
||||
sub_type = data[9]
|
||||
@@ -456,10 +456,10 @@ def process_tlv(tlv_data: bytes) -> None:
|
||||
"""Process TLV data after checking status word."""
|
||||
if DEBUG:
|
||||
print(f"[{color('+', fg='green')}] Calling: {sys._getframe(0).f_code.co_name}")
|
||||
|
||||
|
||||
if len(tlv_data) < 2:
|
||||
raise ValueError("TLV data too short")
|
||||
|
||||
|
||||
SW1_SW2 = tlv_data[-2:]
|
||||
answer = tlv_data[:-2]
|
||||
|
||||
@@ -479,7 +479,7 @@ def strToint16(hex_str: str) -> List[int]:
|
||||
"""
|
||||
if len(hex_str) % 2 != 0:
|
||||
raise ValueError(f"Hex string must have even length, got {len(hex_str)}")
|
||||
|
||||
|
||||
return [int(hex_str[i:i+2], 16) for i in range(0, len(hex_str), 2)]
|
||||
|
||||
def GetRecData(pm3_conn: BridgePM3) -> bytes:
|
||||
@@ -492,7 +492,7 @@ def GetRecData(pm3_conn: BridgePM3) -> bytes:
|
||||
parse_return_code(nfcdata[-2:], DEBUG)
|
||||
return nfcdata
|
||||
|
||||
def sendCommand(pm3_conn: BridgePM3, cla: int, ins: int, p1: int, p2: int,
|
||||
def sendCommand(pm3_conn: BridgePM3, cla: int, ins: int, p1: int, p2: int,
|
||||
Data: Optional[bytes] = None, le: Optional[int] = None) -> bytes:
|
||||
"""Send APDU command and receive response."""
|
||||
context = [cla, ins, p1, p2]
|
||||
@@ -514,7 +514,7 @@ def sendCommand(pm3_conn: BridgePM3, cla: int, ins: int, p1: int, p2: int,
|
||||
pm3_conn.sendToNfc(bytes(context))
|
||||
recdata = GetRecData(pm3_conn)
|
||||
return recdata
|
||||
|
||||
|
||||
def cmd_select(pm3_conn: BridgePM3, fileID: Optional[str] = None, name: Optional[bytes] = None) -> bytes:
|
||||
"""Send SELECT command to card."""
|
||||
if DEBUG:
|
||||
@@ -552,7 +552,7 @@ def cmd_get_balance(pm3_conn: BridgePM3) -> bytes:
|
||||
ins = 0x5C
|
||||
p1 = 0x00
|
||||
p2 = 0x02
|
||||
|
||||
|
||||
ret = sendCommand(pm3_conn, cla=cla, ins=ins, p1=p1, p2=p2, le=4)
|
||||
if DEBUG:
|
||||
print(f"[{color('=', fg='yellow')}] GET_BALANCE => {bytes_to_hexstr(ret)}\n")
|
||||
@@ -562,7 +562,7 @@ def cmd_read_record(pm3_conn: BridgePM3, record_number: int, file_id: int) -> by
|
||||
"""Read a record from a file on the card."""
|
||||
if DEBUG:
|
||||
print(f"[{color('+', fg='green')}] Calling: {sys._getframe(0).f_code.co_name}")
|
||||
|
||||
|
||||
cla = 0x00
|
||||
ins = 0xB2
|
||||
p1 = record_number
|
||||
@@ -597,7 +597,7 @@ def process_tunion_transit_card(pm3_conn: BridgePM3) -> None:
|
||||
except (AssertionError, ValueError) as e:
|
||||
print(f" Error: {e}")
|
||||
break
|
||||
|
||||
|
||||
print("\nReading Travel Records...")
|
||||
for i in range(MAX_TRAVEL_RECORDS):
|
||||
print(f"Reading Travel Record {i+1}...", end="")
|
||||
@@ -624,7 +624,7 @@ def main() -> None:
|
||||
print("\nSelecting DDF...")
|
||||
ret = cmd_select(pm3_conn, name=DDF_PPSE)
|
||||
assert_success(ret)
|
||||
|
||||
|
||||
for aidl in [AID_PBOC_DEBIT_CREDIT, AID_TUNION_TRANSIT]:
|
||||
print(f"\nSelecting AID: {aidl}")
|
||||
ret = cmd_select(pm3_conn, name=bytes.fromhex(aidl))
|
||||
@@ -648,4 +648,4 @@ def main() -> None:
|
||||
pm3_conn.hw_reset()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
+139
-114
@@ -63,11 +63,11 @@ static const uint8_t NUM_ERROR_CORRECTION_BLOCKS[4][40] = {
|
||||
|
||||
static const uint16_t NUM_RAW_DATA_MODULES[40] = {
|
||||
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
||||
208, 359, 567, 807, 1079, 1383, 1568, 1936, 2336, 2768, 3232, 3728, 4256, 4651, 5243, 5867, 6523,
|
||||
208, 359, 567, 807, 1079, 1383, 1568, 1936, 2336, 2768, 3232, 3728, 4256, 4651, 5243, 5867, 6523,
|
||||
// 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
7211, 7931, 8683, 9252, 10068, 10916, 11796, 12708, 13652, 14628, 15371, 16411, 17483, 18587,
|
||||
7211, 7931, 8683, 9252, 10068, 10916, 11796, 12708, 13652, 14628, 15371, 16411, 17483, 18587,
|
||||
// 32, 33, 34, 35, 36, 37, 38, 39, 40
|
||||
19723, 20891, 22091, 23008, 24272, 25568, 26896, 28256, 29648
|
||||
19723, 20891, 22091, 23008, 24272, 25568, 26896, 28256, 29648
|
||||
};
|
||||
|
||||
// @TODO: Put other LOCK_VERSIONS here
|
||||
@@ -98,22 +98,31 @@ static int max(int a, int b) {
|
||||
// Mode testing and conversion
|
||||
|
||||
static int8_t getAlphanumeric(char c) {
|
||||
|
||||
|
||||
if (c >= '0' && c <= '9') { return (c - '0'); }
|
||||
if (c >= 'A' && c <= 'Z') { return (c - 'A' + 10); }
|
||||
|
||||
|
||||
switch (c) {
|
||||
case ' ': return 36;
|
||||
case '$': return 37;
|
||||
case '%': return 38;
|
||||
case '*': return 39;
|
||||
case '+': return 40;
|
||||
case '-': return 41;
|
||||
case '.': return 42;
|
||||
case '/': return 43;
|
||||
case ':': return 44;
|
||||
case ' ':
|
||||
return 36;
|
||||
case '$':
|
||||
return 37;
|
||||
case '%':
|
||||
return 38;
|
||||
case '*':
|
||||
return 39;
|
||||
case '+':
|
||||
return 40;
|
||||
case '-':
|
||||
return 41;
|
||||
case '.':
|
||||
return 42;
|
||||
case '/':
|
||||
return 43;
|
||||
case ':':
|
||||
return 44;
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -145,18 +154,18 @@ static char getModeBits(uint8_t version, uint8_t mode) {
|
||||
// Note: We use 15 instead of 16; since 15 doesn't exist and we cannot store 16 (8 + 8) in 3 bits
|
||||
// hex(int("".join(reversed([('00' + bin(x - 8)[2:])[-3:] for x in [10, 9, 8, 12, 11, 15, 14, 13, 15]])), 2))
|
||||
unsigned int modeInfo = 0x7bbb80a;
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION > 9
|
||||
if (version > 9) { modeInfo >>= 9; }
|
||||
#endif
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION > 26
|
||||
if (version > 26) { modeInfo >>= 9; }
|
||||
#endif
|
||||
|
||||
|
||||
char result = 8 + ((modeInfo >> (3 * mode)) & 0x07);
|
||||
if (result == 15) { result = 16; }
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -192,7 +201,7 @@ static void bb_initBuffer(BitBucket *bitBuffer, uint8_t *data, int32_t capacityB
|
||||
bitBuffer->bitOffsetOrWidth = 0;
|
||||
bitBuffer->capacityBytes = capacityBytes;
|
||||
bitBuffer->data = data;
|
||||
|
||||
|
||||
memset(data, 0, bitBuffer->capacityBytes);
|
||||
}
|
||||
|
||||
@@ -253,21 +262,37 @@ static bool bb_getBit(BitBucket *bitGrid, uint8_t x, uint8_t y) {
|
||||
// well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.).
|
||||
static void applyMask(BitBucket *modules, BitBucket *isFunction, uint8_t mask) {
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
|
||||
for (uint8_t y = 0; y < size; y++) {
|
||||
for (uint8_t x = 0; x < size; x++) {
|
||||
if (bb_getBit(isFunction, x, y)) { continue; }
|
||||
|
||||
|
||||
bool invert = 0;
|
||||
switch (mask) {
|
||||
case 0: invert = (x + y) % 2 == 0; break;
|
||||
case 1: invert = y % 2 == 0; break;
|
||||
case 2: invert = x % 3 == 0; break;
|
||||
case 3: invert = (x + y) % 3 == 0; break;
|
||||
case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
|
||||
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
|
||||
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
|
||||
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
|
||||
case 0:
|
||||
invert = (x + y) % 2 == 0;
|
||||
break;
|
||||
case 1:
|
||||
invert = y % 2 == 0;
|
||||
break;
|
||||
case 2:
|
||||
invert = x % 3 == 0;
|
||||
break;
|
||||
case 3:
|
||||
invert = (x + y) % 3 == 0;
|
||||
break;
|
||||
case 4:
|
||||
invert = (x / 3 + y / 2) % 2 == 0;
|
||||
break;
|
||||
case 5:
|
||||
invert = x * y % 2 + x * y % 3 == 0;
|
||||
break;
|
||||
case 6:
|
||||
invert = (x * y % 2 + x * y % 3) % 2 == 0;
|
||||
break;
|
||||
case 7:
|
||||
invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
|
||||
break;
|
||||
}
|
||||
bb_invertBit(modules, x, y, invert);
|
||||
}
|
||||
@@ -306,7 +331,7 @@ static void drawAlignmentPattern(BitBucket *modules, BitBucket *isFunction, uint
|
||||
// Draws two copies of the format bits (with its own error correction code)
|
||||
// based on the given mask and this object's error correction level field.
|
||||
static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ecc, uint8_t mask) {
|
||||
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
// Calculate error correction code and pack bits
|
||||
@@ -315,32 +340,32 @@ static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ec
|
||||
for (int i = 0; i < 10; i++) {
|
||||
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
||||
}
|
||||
|
||||
|
||||
data = data << 10 | rem;
|
||||
data ^= 0x5412; // uint15
|
||||
|
||||
|
||||
// Draw first copy
|
||||
for (uint8_t i = 0; i <= 5; i++) {
|
||||
setFunctionModule(modules, isFunction, 8, i, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
|
||||
setFunctionModule(modules, isFunction, 8, 7, ((data >> 6) & 1) != 0);
|
||||
setFunctionModule(modules, isFunction, 8, 8, ((data >> 7) & 1) != 0);
|
||||
setFunctionModule(modules, isFunction, 7, 8, ((data >> 8) & 1) != 0);
|
||||
|
||||
|
||||
for (int8_t i = 9; i < 15; i++) {
|
||||
setFunctionModule(modules, isFunction, 14 - i, 8, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
|
||||
// Draw second copy
|
||||
for (int8_t i = 0; i <= 7; i++) {
|
||||
setFunctionModule(modules, isFunction, size - 1 - i, 8, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
|
||||
for (int8_t i = 8; i < 15; i++) {
|
||||
setFunctionModule(modules, isFunction, 8, size - 15 + i, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
|
||||
setFunctionModule(modules, isFunction, 8, size - 8, true);
|
||||
}
|
||||
|
||||
@@ -348,23 +373,23 @@ static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ec
|
||||
// Draws two copies of the version bits (with its own error correction code),
|
||||
// based on this object's version field (which only has an effect for 7 <= version <= 40).
|
||||
static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t version) {
|
||||
|
||||
|
||||
int8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
#if LOCK_VERSION != 0 && LOCK_VERSION < 7
|
||||
return;
|
||||
|
||||
|
||||
#else
|
||||
if (version < 7) { return; }
|
||||
|
||||
|
||||
// Calculate error correction code and pack bits
|
||||
uint32_t rem = version; // version is uint6, in the range [7, 40]
|
||||
for (uint8_t i = 0; i < 12; i++) {
|
||||
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
|
||||
}
|
||||
|
||||
|
||||
uint32_t data = version << 12 | rem; // uint18
|
||||
|
||||
|
||||
// Draw two copies
|
||||
for (uint8_t i = 0; i < 18; i++) {
|
||||
bool bit = ((data >> i) & 1) != 0;
|
||||
@@ -372,12 +397,12 @@ static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t versi
|
||||
setFunctionModule(modules, isFunction, a, b, bit);
|
||||
setFunctionModule(modules, isFunction, b, a, bit);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint8_t version, uint8_t ecc) {
|
||||
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
// Draw the horizontal and vertical timing patterns
|
||||
@@ -385,18 +410,18 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint
|
||||
setFunctionModule(modules, isFunction, 6, i, i % 2 == 0);
|
||||
setFunctionModule(modules, isFunction, i, 6, i % 2 == 0);
|
||||
}
|
||||
|
||||
|
||||
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
||||
drawFinderPattern(modules, isFunction, 3, 3);
|
||||
drawFinderPattern(modules, isFunction, size - 4, 3);
|
||||
drawFinderPattern(modules, isFunction, 3, size - 4);
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION > 1
|
||||
|
||||
if (version > 1) {
|
||||
|
||||
// Draw the numerous alignment patterns
|
||||
|
||||
|
||||
uint8_t alignCount = version / 7 + 2;
|
||||
uint8_t step;
|
||||
if (version != 32) {
|
||||
@@ -404,17 +429,17 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint
|
||||
} else { // C-C-C-Combo breaker!
|
||||
step = 26;
|
||||
}
|
||||
|
||||
|
||||
uint8_t alignPositionIndex = alignCount - 1;
|
||||
uint8_t alignPosition[alignCount];
|
||||
|
||||
|
||||
alignPosition[0] = 6;
|
||||
|
||||
|
||||
size = version * 4 + 17;
|
||||
for (uint8_t i = 0, pos = size - 7; i < alignCount - 1; i++, pos -= step) {
|
||||
alignPosition[alignPositionIndex--] = pos;
|
||||
}
|
||||
|
||||
|
||||
for (uint8_t i = 0; i < alignCount; i++) {
|
||||
for (uint8_t j = 0; j < alignCount; j++) {
|
||||
if ((i == 0 && j == 0) || (i == 0 && j == alignCount - 1) || (i == alignCount - 1 && j == 0)) {
|
||||
@@ -425,9 +450,9 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// Draw configuration data
|
||||
drawFormatBits(modules, isFunction, ecc, 0); // Dummy mask value; overwritten later in the constructor
|
||||
drawVersion(modules, isFunction, version);
|
||||
@@ -437,19 +462,19 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint
|
||||
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
||||
// data area of this QR Code symbol. Function modules need to be marked off before this is called.
|
||||
static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket *codewords) {
|
||||
|
||||
|
||||
uint32_t bitLength = codewords->bitOffsetOrWidth;
|
||||
uint8_t *data = codewords->data;
|
||||
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
|
||||
// Bit index into the data
|
||||
uint32_t i = 0;
|
||||
|
||||
|
||||
// Do the funny zigzag scan
|
||||
for (int16_t right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
|
||||
if (right == 6) { right = 5; }
|
||||
|
||||
|
||||
for (uint8_t vert = 0; vert < size; vert++) { // Vertical counter
|
||||
for (int j = 0; j < 2; j++) {
|
||||
uint8_t x = right - j; // Actual x coordinate
|
||||
@@ -480,19 +505,19 @@ static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket *
|
||||
// @TODO: This can be optimized by working with the bytes instead of bits.
|
||||
static uint32_t getPenaltyScore(BitBucket *modules) {
|
||||
uint32_t result = 0;
|
||||
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
|
||||
// Adjacent modules in row having same color
|
||||
for (uint8_t y = 0; y < size; y++) {
|
||||
|
||||
|
||||
bool colorX = bb_getBit(modules, 0, y);
|
||||
for (uint8_t x = 1, runX = 1; x < size; x++) {
|
||||
bool cx = bb_getBit(modules, x, y);
|
||||
if (cx != colorX) {
|
||||
colorX = cx;
|
||||
runX = 1;
|
||||
|
||||
|
||||
} else {
|
||||
runX++;
|
||||
if (runX == 5) {
|
||||
@@ -503,7 +528,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Adjacent modules in column having same color
|
||||
for (uint8_t x = 0; x < size; x++) {
|
||||
bool colorY = bb_getBit(modules, x, 0);
|
||||
@@ -522,7 +547,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint16_t black = 0;
|
||||
for (uint8_t y = 0; y < size; y++) {
|
||||
uint16_t bitsRow = 0, bitsCol = 0;
|
||||
@@ -563,7 +588,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) {
|
||||
for (uint16_t k = 0; black * 20 < (9 - k) * total || black * 20 > (11 + k) * total; k++) {
|
||||
result += PENALTY_N4;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -584,7 +609,7 @@ static uint8_t rs_multiply(uint8_t x, uint8_t y) {
|
||||
static void rs_init(uint8_t degree, uint8_t *coeff) {
|
||||
memset(coeff, 0, degree);
|
||||
coeff[degree - 1] = 1;
|
||||
|
||||
|
||||
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
||||
// drop the highest term, and store the rest of the coefficients in order of descending powers.
|
||||
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
||||
@@ -603,17 +628,17 @@ static void rs_init(uint8_t degree, uint8_t *coeff) {
|
||||
|
||||
static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8_t length, uint8_t *result, uint8_t stride) {
|
||||
// Compute the remainder by performing polynomial division
|
||||
|
||||
|
||||
//for (uint8_t i = 0; i < degree; i++) { result[] = 0; }
|
||||
//memset(result, 0, degree);
|
||||
|
||||
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
uint8_t factor = data[i] ^ result[0];
|
||||
for (uint8_t j = 1; j < degree; j++) {
|
||||
result[(j - 1) * stride] = result[j * stride];
|
||||
}
|
||||
result[(degree - 1) * stride] = 0;
|
||||
|
||||
|
||||
for (uint8_t j = 0; j < degree; j++) {
|
||||
result[j * stride] ^= rs_multiply(coeff[j], factor);
|
||||
}
|
||||
@@ -626,8 +651,8 @@ static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8
|
||||
|
||||
static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) {
|
||||
int8_t mode = MODE_BYTE;
|
||||
|
||||
if (isNumeric((char*)text, length)) {
|
||||
|
||||
if (isNumeric((char *)text, length)) {
|
||||
mode = MODE_NUMERIC;
|
||||
bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4);
|
||||
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_NUMERIC));
|
||||
@@ -643,13 +668,13 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text,
|
||||
accumCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 1 or 2 digits remaining
|
||||
if (accumCount > 0) {
|
||||
bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1);
|
||||
}
|
||||
|
||||
} else if (isAlphanumeric((char*)text, length)) {
|
||||
|
||||
} else if (isAlphanumeric((char *)text, length)) {
|
||||
mode = MODE_ALPHANUMERIC;
|
||||
bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4);
|
||||
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_ALPHANUMERIC));
|
||||
@@ -665,12 +690,12 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text,
|
||||
accumCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 1 character remaining
|
||||
if (accumCount > 0) {
|
||||
bb_appendBits(dataCodewords, accumData, 6);
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
bb_appendBits(dataCodewords, 1 << MODE_BYTE, 4);
|
||||
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_BYTE));
|
||||
@@ -678,16 +703,16 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text,
|
||||
bb_appendBits(dataCodewords, (char)(text[i]), 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//bb_setBits(dataCodewords, length, 4, getModeBits(version, mode));
|
||||
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data) {
|
||||
|
||||
|
||||
// See: http://www.thonky.com/qr-code-tutorial/structure-final-message
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0
|
||||
uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc][version - 1];
|
||||
uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc][version - 1];
|
||||
@@ -697,37 +722,37 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data
|
||||
uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc];
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES;
|
||||
#endif
|
||||
|
||||
|
||||
uint8_t blockEccLen = totalEcc / numBlocks;
|
||||
uint8_t numShortBlocks = numBlocks - moduleCount / 8 % numBlocks;
|
||||
uint8_t shortBlockLen = moduleCount / 8 / numBlocks;
|
||||
|
||||
|
||||
uint8_t shortDataBlockLen = shortBlockLen - blockEccLen;
|
||||
|
||||
|
||||
uint8_t result[data->capacityBytes];
|
||||
memset(result, 0, sizeof(result));
|
||||
|
||||
|
||||
uint8_t coeff[blockEccLen];
|
||||
rs_init(blockEccLen, coeff);
|
||||
|
||||
|
||||
uint16_t offset = 0;
|
||||
uint8_t *dataBytes = data->data;
|
||||
|
||||
|
||||
|
||||
|
||||
// Interleave all short blocks
|
||||
for (uint8_t i = 0; i < shortDataBlockLen; i++) {
|
||||
uint16_t index = i;
|
||||
uint8_t stride = shortDataBlockLen;
|
||||
for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) {
|
||||
result[offset++] = dataBytes[index];
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION >= 5
|
||||
if (blockNum == numShortBlocks) { stride++; }
|
||||
#endif
|
||||
index += stride;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Version less than 5 only have short blocks
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION >= 5
|
||||
{
|
||||
@@ -736,24 +761,24 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data
|
||||
uint8_t stride = shortDataBlockLen;
|
||||
for (uint8_t blockNum = 0; blockNum < numBlocks - numShortBlocks; blockNum++) {
|
||||
result[offset++] = dataBytes[index];
|
||||
|
||||
|
||||
if (blockNum == 0) { stride++; }
|
||||
index += stride;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// Add all ecc blocks, interleaved
|
||||
uint8_t blockSize = shortDataBlockLen;
|
||||
for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) {
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION >= 5
|
||||
if (blockNum == numShortBlocks) { blockSize++; }
|
||||
#endif
|
||||
rs_getRemainder(blockEccLen, coeff, dataBytes, blockSize, &result[offset + blockNum], numBlocks);
|
||||
dataBytes += blockSize;
|
||||
}
|
||||
|
||||
|
||||
memcpy(data->data, result, data->capacityBytes);
|
||||
data->bitOffsetOrWidth = moduleCount;
|
||||
}
|
||||
@@ -775,9 +800,9 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
|
||||
qrcode->size = size;
|
||||
qrcode->ecc = ecc;
|
||||
qrcode->modules = modules;
|
||||
|
||||
|
||||
uint8_t eccFormatBits = (ECC_FORMAT_BITS >> (2 * ecc)) & 0x03;
|
||||
|
||||
|
||||
#if LOCK_VERSION == 0
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1];
|
||||
uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits][version - 1];
|
||||
@@ -786,19 +811,19 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES;
|
||||
uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits];
|
||||
#endif
|
||||
|
||||
|
||||
struct BitBucket codewords;
|
||||
uint8_t codewordBytes[bb_getBufferSizeBytes(moduleCount)];
|
||||
bb_initBuffer(&codewords, codewordBytes, (int32_t)sizeof(codewordBytes));
|
||||
|
||||
|
||||
// Place the data code words into the buffer
|
||||
int8_t mode = encodeDataCodewords(&codewords, data, length, version);
|
||||
|
||||
if (mode < 0) {
|
||||
return -1;
|
||||
|
||||
if (mode < 0) {
|
||||
return -1;
|
||||
}
|
||||
qrcode->mode = mode;
|
||||
|
||||
|
||||
// Add terminator and pad up to a byte if applicable
|
||||
uint32_t padding = (dataCapacity * 8) - codewords.bitOffsetOrWidth;
|
||||
if (padding > 4) { padding = 4; }
|
||||
@@ -812,16 +837,16 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
|
||||
|
||||
BitBucket modulesGrid;
|
||||
bb_initGrid(&modulesGrid, modules, size);
|
||||
|
||||
|
||||
BitBucket isFunctionGrid;
|
||||
uint8_t isFunctionGridBytes[bb_getGridSizeBytes(size)];
|
||||
bb_initGrid(&isFunctionGrid, isFunctionGridBytes, size);
|
||||
|
||||
|
||||
// Draw function patterns, draw all codewords, do masking
|
||||
drawFunctionPatterns(&modulesGrid, &isFunctionGrid, version, eccFormatBits);
|
||||
performErrorCorrection(version, eccFormatBits, &codewords);
|
||||
drawCodewords(&modulesGrid, &isFunctionGrid, &codewords);
|
||||
|
||||
|
||||
// Find the best (lowest penalty) mask
|
||||
uint8_t mask = 0;
|
||||
int32_t minPenalty = INT32_MAX;
|
||||
@@ -835,12 +860,12 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
|
||||
}
|
||||
applyMask(&modulesGrid, &isFunctionGrid, i); // Undoes the mask due to XOR
|
||||
}
|
||||
|
||||
|
||||
qrcode->mask = mask;
|
||||
|
||||
|
||||
// Overwrite old format bits
|
||||
drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, mask);
|
||||
|
||||
|
||||
// Apply the final choice of mask
|
||||
applyMask(&modulesGrid, &isFunctionGrid, mask);
|
||||
|
||||
@@ -848,7 +873,7 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
|
||||
}
|
||||
|
||||
int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data) {
|
||||
return qrcode_initBytes(qrcode, modules, version, ecc, (uint8_t*)data, strlen(data));
|
||||
return qrcode_initBytes(qrcode, modules, version, ecc, (uint8_t *)data, strlen(data));
|
||||
}
|
||||
|
||||
bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y) {
|
||||
@@ -877,7 +902,7 @@ void qrcode_print_matrix_utf8(QRCode *q) {
|
||||
if ((y + 1) < q->size) {
|
||||
bottom = qrcode_getModule(q, x, y + 1);
|
||||
}
|
||||
|
||||
|
||||
if (top && bottom) {
|
||||
PrintAndLogEx(NORMAL, "%s" NOLF, QRCODE_PRINT_BLOCK_FULL);
|
||||
} else if (top && (bottom == false)) {
|
||||
@@ -897,7 +922,7 @@ void qrcode_print_matrix_utf8_2x2(QRCode *q) {
|
||||
// ab
|
||||
// cd
|
||||
// UTF-8 block elements for 2x2 pixel blocks
|
||||
static const char* block_chars[16] = {
|
||||
static const char *block_chars[16] = {
|
||||
" ", // 0000: 0
|
||||
"\xE2\x96\x97", // 0001: 1
|
||||
"\xE2\x96\x96", // 0010: 2
|
||||
@@ -909,7 +934,7 @@ void qrcode_print_matrix_utf8_2x2(QRCode *q) {
|
||||
"\xE2\x96\x98", // 1000: 8
|
||||
"\xE2\x96\x9A", // 1001: 9
|
||||
"\xE2\x96\x8C", // 1010: a
|
||||
"\xE2\x96\x99" , // 1011: b
|
||||
"\xE2\x96\x99", // 1011: b
|
||||
"\xE2\x96\x80", // 1100: c
|
||||
"\xE2\x96\x9C", // 1101: D
|
||||
"\xE2\x96\x9B", // 1110: E
|
||||
@@ -927,7 +952,7 @@ void qrcode_print_matrix_utf8_2x2(QRCode *q) {
|
||||
|
||||
uint8_t c = qrcode_getModule(q, x, y + 1);
|
||||
uint8_t d = 0;
|
||||
if ( ((x + 1) < q->size) && (y + 1) < q->size) {
|
||||
if (((x + 1) < q->size) && (y + 1) < q->size) {
|
||||
d = qrcode_getModule(q, x + 1, y + 1);
|
||||
}
|
||||
|
||||
@@ -936,4 +961,4 @@ void qrcode_print_matrix_utf8_2x2(QRCode *q) {
|
||||
}
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ typedef struct QRCode {
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user