diff --git a/src/apdu.c b/src/apdu.c index fd41df2..6a32225 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -179,7 +179,7 @@ uint16_t set_res_sw(uint8_t sw1, uint8_t sw2) { if (sw1 != 0x90) { res_APDU_size = 0; } - return make_uint16_t(sw1, sw2); + return make_uint16_t_be(sw1, sw2); } void apdu_thread(void) { diff --git a/src/fs/file.c b/src/fs/file.c index ac7a3fa..27e362d 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -88,7 +88,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(pe->fid, res_APDU + res_APDU_size); + put_uint16_t_be(pe->fid, res_APDU + res_APDU_size); res_APDU_size += 2; if (pe->name) { res_APDU[res_APDU_size++] = 0x84; @@ -176,13 +176,13 @@ uint8_t make_path_buf(const file_t *pe, uint8_t *buf, uint8_t buflen, const file if (pe == top) { //MF or relative DF return 0; } - put_uint16_t(pe->fid, buf); + put_uint16_t_be(pe->fid, buf); return make_path_buf(&file_entries[pe->parent], buf + 2, buflen - 2, top) + 2; } uint8_t make_path(const file_t *pe, const file_t *top, uint8_t *path) { uint8_t buf[MAX_DEPTH * 2], *p = path; - put_uint16_t(pe->fid, buf); + put_uint16_t_be(pe->fid, buf); uint8_t depth = make_path_buf(&file_entries[pe->parent], buf + 2, sizeof(buf) - 2, top) + 2; for (int d = depth - 2; d >= 0; d -= 2) { memcpy(p, buf + d, 2); diff --git a/src/pico_keys.h b/src/pico_keys.h index 761b3ec..c2133d6 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -64,37 +64,65 @@ extern bool wait_button(); extern void low_flash_init_core1(); -static inline uint16_t make_uint16_t(uint8_t b1, uint8_t b2) { +static inline uint16_t make_uint16_t_be(uint8_t b1, uint8_t b2) { return (b1 << 8) | b2; } -static inline uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) { - return make_uint16_t(b[offset], b[offset + 1]); +static inline uint16_t make_uint16_t_le(uint8_t b1, uint8_t b2) { + return (b2 << 8) | b1; } -static inline void put_uint16_t(uint16_t n, uint8_t *b) { +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_le(const uint8_t *b, uint16_t offset) { + return make_uint16_t_le(b[offset], b[offset + 1]); +} +static inline void put_uint16_t_be(uint16_t n, uint8_t *b) { *b++ = (n >> 8) & 0xff; *b = n & 0xff; } +static inline void put_uint16_t_le(uint16_t n, uint8_t *b) { + *b++ = n & 0xff; + *b = (n >> 8) & 0xff; +} -static inline uint32_t make_uint32_t(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) { +static inline uint32_t make_uint32_t_be(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) { return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; } -static inline uint32_t get_uint32_t(const uint8_t *b, uint16_t offset) { - return make_uint32_t(b[offset], b[offset + 1], b[offset + 2], b[offset + 3]); +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 void put_uint32_t(uint32_t n, uint8_t *b) { +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_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 void 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; } +static inline void 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; +} -static inline uint64_t make_uint64_t(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) { +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) { return ((uint64_t) b1 << 56) | ((uint64_t) b2 << 48) | ((uint64_t) b3 << 40) | ((uint64_t) b4 << 32) | ((uint64_t) b5 << 24) | ((uint64_t) b6 << 16) | ((uint64_t) b7 << 8) | b8; } -static inline uint64_t get_uint64_t(const uint8_t *b, uint16_t offset) { - return make_uint64_t(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 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 void put_uint64_t(uint64_t n, uint8_t *b) { +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_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 void put_uint64_t_be(uint64_t n, uint8_t *b) { *b++ = (n >> 56) & 0xff; *b++ = (n >> 48) & 0xff; *b++ = (n >> 40) & 0xff; @@ -104,6 +132,16 @@ static inline void put_uint64_t(uint64_t n, uint8_t *b) { *b++ = (n >> 8) & 0xff; *b = n & 0xff; } +static inline void put_uint64_t_le(uint64_t n, uint8_t *b) { + *b++ = n & 0xff; + *b++ = (n >> 8) & 0xff; + *b++ = (n >> 16) & 0xff; + *b++ = (n >> 24) & 0xff; + *b++ = (n >> 32) & 0xff; + *b++ = (n >> 40) & 0xff; + *b++ = (n >> 48) & 0xff; + *b = (n >> 56) & 0xff; +} extern void low_flash_available(); extern int flash_clear_file(file_t *file);