From f8cb36c2cf5de7f0e8b7cd4a497160e86de50107 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 23 Dec 2024 21:23:13 +0100 Subject: [PATCH] Use uint16 funcs. Signed-off-by: Pol Henarejos --- src/apdu.c | 8 ++++---- src/fs/file.c | 21 ++++++++------------- src/fs/phy.c | 5 ++--- src/pico_keys.h | 42 ++++++++++++++++++++++++------------------ 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/apdu.c b/src/apdu.c index 20ccde8..20ec597 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -85,17 +85,17 @@ uint16_t apdu_process(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) } else if (apdu.header[4] == 0x0 && buffer_size >= 7) { if (buffer_size == 7) { - apdu.ne = (apdu.header[5] << 8) | apdu.header[6]; + apdu.ne = get_uint16_t_be(apdu.header + 5); if (apdu.ne == 0) { apdu.ne = 65536; } } else { apdu.ne = 0; - apdu.nc = (apdu.header[5] << 8) | apdu.header[6]; + apdu.nc = get_uint16_t_be(apdu.header + 5); apdu.data = apdu.header + 7; if (apdu.nc + 7 + 2 == buffer_size) { - apdu.ne = (apdu.header[buffer_size - 2] << 8) | apdu.header[buffer_size - 1]; + apdu.ne = get_uint16_t_be(apdu.header + buffer_size - 2); if (apdu.ne == 0) { apdu.ne = 65536; } @@ -175,7 +175,7 @@ uint16_t apdu_process(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) } uint16_t set_res_sw(uint8_t sw1, uint8_t sw2) { - apdu.sw = (sw1 << 8) | sw2; + apdu.sw = make_uint16_t_be(sw1, sw2); if (sw1 != 0x90) { res_APDU_size = 0; } diff --git a/src/fs/file.c b/src/fs/file.c index 2643147..6cb7ebc 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -56,13 +56,11 @@ void process_fci(const file_t *pe, int fmd) { if (pe->data) { if ((pe->type & FILE_DATA_FUNC) == FILE_DATA_FUNC) { uint16_t len = (uint16_t)((int (*)(const file_t *, int))(pe->data))(pe, 0); - put_uint16_t_be(len, res_APDU + res_APDU_size); - res_APDU_size += 2; + res_APDU_size += put_uint16_t_be(len, res_APDU + res_APDU_size); } else { uint16_t v = file_get_size(pe); - put_uint16_t_be(v, res_APDU + res_APDU_size); - res_APDU_size += 2; + res_APDU_size += put_uint16_t_be(v, res_APDU + res_APDU_size); } } else { @@ -88,8 +86,7 @@ void process_fci(const file_t *pe, int fmd) { res_APDU[res_APDU_size++] = 0x83; res_APDU[res_APDU_size++] = 2; - put_uint16_t_be(pe->fid, res_APDU + res_APDU_size); - res_APDU_size += 2; + res_APDU_size += put_uint16_t_be(pe->fid, res_APDU + res_APDU_size); if (pe->name) { res_APDU[res_APDU_size++] = 0x84; res_APDU[res_APDU_size++] = MIN(pe->name[0], 16); @@ -393,7 +390,7 @@ uint16_t meta_find(uint16_t fid, uint8_t **out) { if (tag_len < 2) { continue; } - uint16_t cfid = (tag_data[0] << 8 | tag_data[1]); + uint16_t cfid = get_uint16_t_be(tag_data); if (cfid == fid) { if (out) { *out = tag_data + 2; @@ -419,7 +416,7 @@ int meta_delete(uint16_t fid) { if (tag_len < 2) { continue; } - uint16_t cfid = (tag_data[0] << 8 | tag_data[1]); + uint16_t cfid = get_uint16_t_be(tag_data); if (cfid == fid) { uint16_t new_len = ctxi.len - 1 - tag_len - format_tlv_len(tag_len, NULL); if (new_len == 0) { @@ -463,7 +460,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { if (tag_len < 2) { continue; } - uint16_t cfid = (tag_data[0] << 8 | tag_data[1]); + uint16_t cfid = get_uint16_t_be(tag_data); if (cfid == fid) { if (tag_len - 2 == len) { //an update memcpy(p - tag_len + 2, data, len); @@ -493,8 +490,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { uint8_t *f = fdata + meta_offset; *f++ = fid & 0xff; f += format_tlv_len(len + 2, f); - put_uint16_t_be(fid, f); - f += 2; + f += put_uint16_t_be(fid, f); memcpy(f, data, len); r = file_put_data(ef, fdata, ef_size); free(fdata); @@ -509,8 +505,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { uint8_t *f = fdata + ef_size; *f++ = fid & 0x1f; f += format_tlv_len(len + 2, f); - put_uint16_t_be(fid, f); - f += 2; + f += put_uint16_t_be(fid, f); memcpy(f, data, len); r = file_put_data(ef, fdata, ef_size + (uint16_t)asn1_len_tag(fid & 0x1f, len + 2)); free(fdata); diff --git a/src/fs/phy.c b/src/fs/phy.c index 2bcb272..d451413 100644 --- a/src/fs/phy.c +++ b/src/fs/phy.c @@ -43,8 +43,7 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) { *p++ = phy->led_brightness; } *p++ = PHY_OPTS; - put_uint16_t_be(phy->opts, p); - p += 2; + p += put_uint16_t_be(phy->opts, p); if (phy->up_btn_present) { *p++ = PHY_UP_BTN; *p++ = phy->up_btn; @@ -85,7 +84,7 @@ int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy) { phy->led_brightness_present = true; break; case PHY_OPTS: - phy->opts = (*p << 8) | *(p + 1); + phy->opts = get_uint16_t_be(p); p += 2; break; case PHY_UP_BTN: diff --git a/src/pico_keys.h b/src/pico_keys.h index c2133d6..8a5022b 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -70,19 +70,21 @@ static inline uint16_t make_uint16_t_be(uint8_t b1, uint8_t b2) { static inline uint16_t make_uint16_t_le(uint8_t b1, uint8_t b2) { return (b2 << 8) | b1; } -static inline uint16_t get_uint16_t_be(const uint8_t *b, uint16_t offset) { - return make_uint16_t_be(b[offset], b[offset + 1]); +static inline uint16_t get_uint16_t_be(const uint8_t *b) { + return make_uint16_t_be(b[0], b[1]); } -static inline uint16_t get_uint16_t_le(const uint8_t *b, uint16_t offset) { - return make_uint16_t_le(b[offset], b[offset + 1]); +static inline uint16_t get_uint16_t_le(const uint8_t *b) { + return make_uint16_t_le(b[0], b[1]); } -static inline void put_uint16_t_be(uint16_t n, uint8_t *b) { +static inline uint32_t put_uint16_t_be(uint16_t n, uint8_t *b) { *b++ = (n >> 8) & 0xff; *b = n & 0xff; + return 2; } -static inline void put_uint16_t_le(uint16_t n, uint8_t *b) { +static inline uint32_t put_uint16_t_le(uint16_t n, uint8_t *b) { *b++ = n & 0xff; *b = (n >> 8) & 0xff; + return 2; } static inline uint32_t make_uint32_t_be(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) { @@ -91,23 +93,25 @@ static inline uint32_t make_uint32_t_be(uint8_t b1, uint8_t b2, uint8_t b3, uint static inline uint32_t make_uint32_t_le(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) { return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1; } -static inline uint32_t get_uint32_t_be(const uint8_t *b, uint16_t offset) { - return make_uint32_t_be(b[offset], b[offset + 1], b[offset + 2], b[offset + 3]); +static inline uint32_t get_uint32_t_be(const uint8_t *b) { + return make_uint32_t_be(b[0], b[1], b[2], b[3]); } -static inline uint32_t get_uint32_t_le(const uint8_t *b, uint16_t offset) { - return make_uint32_t_le(b[offset], b[offset + 1], b[offset + 2], b[offset + 3]); +static inline uint32_t get_uint32_t_le(const uint8_t *b) { + return make_uint32_t_le(b[0], b[1], b[2], b[3]); } -static inline void put_uint32_t_be(uint32_t n, uint8_t *b) { +static inline uint32_t put_uint32_t_be(uint32_t n, uint8_t *b) { *b++ = (n >> 24) & 0xff; *b++ = (n >> 16) & 0xff; *b++ = (n >> 8) & 0xff; *b = n & 0xff; + return 4; } -static inline void put_uint32_t_le(uint32_t n, uint8_t *b) { +static inline uint32_t put_uint32_t_le(uint32_t n, uint8_t *b) { *b++ = n & 0xff; *b++ = (n >> 8) & 0xff; *b++ = (n >> 16) & 0xff; *b = (n >> 24) & 0xff; + return 4; } static inline uint64_t make_uint64_t_be(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) { @@ -116,13 +120,13 @@ static inline uint64_t make_uint64_t_be(uint8_t b1, uint8_t b2, uint8_t b3, uint static inline uint64_t make_uint64_t_le(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) { return ((uint64_t) b8 << 56) | ((uint64_t) b7 << 48) | ((uint64_t) b6 << 40) | ((uint64_t) b5 << 32) | ((uint64_t) b4 << 24) | ((uint64_t) b3 << 16) | ((uint64_t) b2 << 8) | b1; } -static inline uint64_t get_uint64_t_be(const uint8_t *b, uint16_t offset) { - return make_uint64_t_be(b[offset], b[offset + 1], b[offset + 2], b[offset + 3], b[offset + 4], b[offset + 5], b[offset + 6], b[offset + 7]); +static inline uint64_t get_uint64_t_be(const uint8_t *b) { + return make_uint64_t_be(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); } -static inline uint64_t get_uint64_t_le(const uint8_t *b, uint16_t offset) { - return make_uint64_t_le(b[offset], b[offset + 1], b[offset + 2], b[offset + 3], b[offset + 4], b[offset + 5], b[offset + 6], b[offset + 7]); +static inline uint64_t get_uint64_t_le(const uint8_t *b) { + return make_uint64_t_le(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); } -static inline void put_uint64_t_be(uint64_t n, uint8_t *b) { +static inline uint32_t put_uint64_t_be(uint64_t n, uint8_t *b) { *b++ = (n >> 56) & 0xff; *b++ = (n >> 48) & 0xff; *b++ = (n >> 40) & 0xff; @@ -131,8 +135,9 @@ static inline void put_uint64_t_be(uint64_t n, uint8_t *b) { *b++ = (n >> 16) & 0xff; *b++ = (n >> 8) & 0xff; *b = n & 0xff; + return 8; } -static inline void put_uint64_t_le(uint64_t n, uint8_t *b) { +static inline uint32_t put_uint64_t_le(uint64_t n, uint8_t *b) { *b++ = n & 0xff; *b++ = (n >> 8) & 0xff; *b++ = (n >> 16) & 0xff; @@ -141,6 +146,7 @@ static inline void put_uint64_t_le(uint64_t n, uint8_t *b) { *b++ = (n >> 40) & 0xff; *b++ = (n >> 48) & 0xff; *b = (n >> 56) & 0xff; + return 8; } extern void low_flash_available();