From 9de4152f90c3b9eb9d8e2c0ecde029b83bb196c0 Mon Sep 17 00:00:00 2001 From: AJR Date: Sun, 24 Sep 2023 00:57:06 -0400 Subject: chdman.cpp, imgtool/modules: Use multibyte.h functions --- src/tools/chdman.cpp | 7 +++-- src/tools/imgtool/modules/bml3.cpp | 6 ++-- src/tools/imgtool/modules/cybiko.cpp | 55 ++++++++++------------------------ src/tools/imgtool/modules/cybikoxt.cpp | 42 +++++++------------------- src/tools/imgtool/modules/psion.cpp | 12 ++++---- 5 files changed, 39 insertions(+), 83 deletions(-) diff --git a/src/tools/chdman.cpp b/src/tools/chdman.cpp index 2bbf2ed6058..3a4277c50d1 100644 --- a/src/tools/chdman.cpp +++ b/src/tools/chdman.cpp @@ -16,6 +16,7 @@ #include "coretmpl.h" #include "hashing.h" #include "md5.h" +#include "multibyte.h" #include "path.h" #include "strformat.h" #include "vbiparse.h" @@ -1881,9 +1882,9 @@ static void do_create_hd(parameters_map ¶ms) // must be at least 14 bytes; extract CHS data from there if (identdata.size() < 14) report_error(1, "Ident file '%s' is invalid (too short)", *ident_str->second); - cylinders = (identdata[3] << 8) | identdata[2]; - heads = (identdata[7] << 8) | identdata[6]; - sectors = (identdata[13] << 8) | identdata[12]; + cylinders = get_u16le(&identdata[2]); + heads = get_u16le(&identdata[6]); + sectors = get_u16le(&identdata[12]); // ignore CHS for > 8GB drives if (cylinders * heads * sectors >= 16514064) diff --git a/src/tools/imgtool/modules/bml3.cpp b/src/tools/imgtool/modules/bml3.cpp index ecb33c0a2e8..8ab81bc12cf 100644 --- a/src/tools/imgtool/modules/bml3.cpp +++ b/src/tools/imgtool/modules/bml3.cpp @@ -23,6 +23,7 @@ #include "iflopimg.h" #include "corestr.h" +#include "multibyte.h" #include "opresolv.h" #include @@ -134,7 +135,7 @@ static floperr_t get_bml3_dirent(imgtool::image &f, int index_loc, struct bml3_d ent->ftype = buf[11]; ent->asciiflag = buf[12]; ent->first_granule = buf[13]; - ent->lastsectorbytes = (buf[14] << 8) | buf[15]; + ent->lastsectorbytes = get_u16be(&buf[14]); break; default: return FLOPPY_ERROR_INVALIDIMAGE; @@ -167,8 +168,7 @@ static floperr_t put_bml3_dirent(imgtool::image &f, int index_loc, const struct buf[11] = ent->ftype; buf[12] = ent->asciiflag; buf[13] = ent->first_granule; - buf[14] = ent->lastsectorbytes >> 8; - buf[15] = ent->lastsectorbytes & 0xff; + put_u16be(&buf[14], ent->lastsectorbytes); break; default: return FLOPPY_ERROR_INVALIDIMAGE; diff --git a/src/tools/imgtool/modules/cybiko.cpp b/src/tools/imgtool/modules/cybiko.cpp index f508e423ad5..bfe9bfdddb2 100644 --- a/src/tools/imgtool/modules/cybiko.cpp +++ b/src/tools/imgtool/modules/cybiko.cpp @@ -10,6 +10,7 @@ #include "imgtool.h" +#include "multibyte.h" #include "opresolv.h" #include @@ -54,8 +55,8 @@ enum }; #define BLOCK_USED(x) (x[0] & 0x80) -#define BLOCK_FILE_ID(x) buffer_read_16_be( x + 2) -#define BLOCK_PART_ID(x) buffer_read_16_be( x + 4) +#define BLOCK_FILE_ID(x) get_u16be( x + 2) +#define BLOCK_PART_ID(x) get_u16be( x + 4) #define BLOCK_FILENAME(x) (char*)(x + 7) #define FILE_HEADER_SIZE 0x48 @@ -81,30 +82,6 @@ uint32_t cybiko_time_setup(const imgtool::datetime &t) return cybiko_time_point.time_since_epoch().count(); } -static uint32_t buffer_read_32_be( uint8_t *buffer) -{ - return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3] << 0); -} - -static uint16_t buffer_read_16_be( uint8_t *buffer) -{ - return (buffer[0] << 8) | (buffer[1] << 0); -} - -static void buffer_write_32_be( uint8_t *buffer, uint32_t data) -{ - buffer[0] = (data >> 24) & 0xFF; - buffer[1] = (data >> 16) & 0xFF; - buffer[2] = (data >> 8) & 0xFF; - buffer[3] = (data >> 0) & 0xFF; -} - -static void buffer_write_16_be( uint8_t *buffer, uint16_t data) -{ - buffer[0] = (data >> 8) & 0xFF; - buffer[1] = (data >> 0) & 0xFF; -} - // page = crc1 (4) + wcnt (2) + crc2 (2) + data (x) + unk (2) static uint32_t page_buffer_calc_checksum_1( uint8_t *buffer, uint32_t size, int block_type) @@ -115,9 +92,9 @@ static uint32_t page_buffer_calc_checksum_1( uint8_t *buffer, uint32_t size, int static uint16_t page_buffer_calc_checksum_2( uint8_t *buffer) { uint16_t val = 0xAF17; - val ^= buffer_read_16_be( buffer + 0); - val ^= buffer_read_16_be( buffer + 2); - val ^= buffer_read_16_be( buffer + 4); + val ^= get_u16be( buffer + 0); + val ^= get_u16be( buffer + 2); + val ^= get_u16be( buffer + 4); return swapendian_int16(val); } @@ -126,11 +103,11 @@ static int page_buffer_verify( uint8_t *buffer, uint32_t size, int block_type) uint32_t checksum_page, checksum_calc; // checksum 1 checksum_calc = page_buffer_calc_checksum_1( buffer, size, block_type); - checksum_page = buffer_read_32_be( buffer + 0); + checksum_page = get_u32be( buffer + 0); if (checksum_calc != checksum_page) return false; // checksum 2 checksum_calc = page_buffer_calc_checksum_2( buffer); - checksum_page = buffer_read_16_be( buffer + 6); + checksum_page = get_u16be( buffer + 6); if (checksum_calc != checksum_page) return false; // ok return true; @@ -200,10 +177,10 @@ static int cfs_block_write( cybiko_file_system *cfs, uint8_t *buffer, int block_ uint8_t buffer_page[MAX_PAGE_SIZE]; uint32_t page; memcpy( buffer_page + 8, buffer, cfs->page_size - 10); - buffer_write_32_be( buffer_page + 0, page_buffer_calc_checksum_1( buffer_page, cfs->page_size, block_type)); - buffer_write_16_be( buffer_page + 4, cfs->write_count++); - buffer_write_16_be( buffer_page + 6, page_buffer_calc_checksum_2( buffer_page)); - buffer_write_16_be( buffer_page + cfs->page_size - 2, 0xFFFF); + put_u32be( buffer_page + 0, page_buffer_calc_checksum_1( buffer_page, cfs->page_size, block_type)); + put_u16be( buffer_page + 4, cfs->write_count++); + put_u16be( buffer_page + 6, page_buffer_calc_checksum_2( buffer_page)); + put_u16be( buffer_page + cfs->page_size - 2, 0xFFFF); if (!cfs_block_to_page( cfs, block_type, block, &page)) return false; if (!cfs_page_write( cfs, buffer_page, page)) return false; return true; @@ -238,7 +215,7 @@ static int cfs_file_info( cybiko_file_system *cfs, uint16_t file_id, cfs_file *f if (BLOCK_PART_ID(buffer) == 0) { strcpy( file->name, BLOCK_FILENAME(buffer)); - file->date = buffer_read_32_be( buffer + 6 + FILE_HEADER_SIZE - 4); + file->date = get_u32be( buffer + 6 + FILE_HEADER_SIZE - 4); } file->size += buffer[1]; file->blocks++; @@ -501,13 +478,13 @@ static imgtoolerr_t cybiko_image_write_file(imgtool::partition &partition, const buffer[0] = 0x80; buffer[1] = cfs->page_size - 0x10 - ((part_id == 0) ? FILE_HEADER_SIZE : 0); if (bytes_left < buffer[1]) buffer[1] = bytes_left; - buffer_write_16_be( buffer + 2, file_id); - buffer_write_16_be( buffer + 4, part_id); + put_u16be( buffer + 2, file_id); + put_u16be( buffer + 4, part_id); if (part_id == 0) { buffer[6] = 0; strcpy(BLOCK_FILENAME(buffer), filename); - buffer_write_32_be(buffer + 6 + FILE_HEADER_SIZE - 4, cybiko_time_setup(imgtool::datetime::now(imgtool::datetime::datetime_type::LOCAL))); + put_u32be(buffer + 6 + FILE_HEADER_SIZE - 4, cybiko_time_setup(imgtool::datetime::now(imgtool::datetime::datetime_type::LOCAL))); sourcef.read(buffer + 6 + FILE_HEADER_SIZE, buffer[1]); } else diff --git a/src/tools/imgtool/modules/cybikoxt.cpp b/src/tools/imgtool/modules/cybikoxt.cpp index 8c2980de449..556f37fde76 100644 --- a/src/tools/imgtool/modules/cybikoxt.cpp +++ b/src/tools/imgtool/modules/cybikoxt.cpp @@ -10,6 +10,8 @@ #include "imgtool.h" +#include "multibyte.h" + #include struct cybiko_file_system @@ -44,8 +46,8 @@ enum #define INVALID_FILE_ID 0xFFFF #define BLOCK_USED(x) (x[0] & 0x80) -#define BLOCK_FILE_ID(x) buffer_read_16_be( x + 2) -#define BLOCK_PART_ID(x) buffer_read_16_be( x + 4) +#define BLOCK_FILE_ID(x) get_u16be( x + 2) +#define BLOCK_PART_ID(x) get_u16be( x + 4) #define BLOCK_FILENAME(x) (char*)(x + 7) #define FILE_HEADER_SIZE 0x48 @@ -58,30 +60,6 @@ static cybiko_file_system *get_cfs(imgtool::image &image) extern imgtool::datetime cybiko_time_crack(uint32_t cfs_time); extern uint32_t cybiko_time_setup(const imgtool::datetime &t); -static uint32_t buffer_read_32_be( uint8_t *buffer) -{ - return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3] << 0); -} - -static uint16_t buffer_read_16_be( uint8_t *buffer) -{ - return (buffer[0] << 8) | (buffer[1] << 0); -} - -static void buffer_write_32_be( uint8_t *buffer, uint32_t data) -{ - buffer[0] = (data >> 24) & 0xFF; - buffer[1] = (data >> 16) & 0xFF; - buffer[2] = (data >> 8) & 0xFF; - buffer[3] = (data >> 0) & 0xFF; -} - -static void buffer_write_16_be( uint8_t *buffer, uint16_t data) -{ - buffer[0] = (data >> 8) & 0xFF; - buffer[1] = (data >> 0) & 0xFF; -} - // page = crc (2) + data (x) static uint16_t page_buffer_calc_checksum( uint8_t *data, uint32_t size) @@ -103,7 +81,7 @@ static int page_buffer_verify( uint8_t *buffer, uint32_t size, int block_type) { uint32_t checksum_page, checksum_calc; checksum_calc = page_buffer_calc_checksum( buffer + 2, size - 2); - checksum_page = buffer_read_16_be( buffer + 0); + checksum_page = get_u16be( buffer + 0); if (checksum_calc != checksum_page) return false; } // ok @@ -183,7 +161,7 @@ static int cfs_block_write( cybiko_file_system *cfs, uint8_t *buffer, int block_ { checksum = page_buffer_calc_checksum( buffer_page + 2, cfs->page_size - 2); } - buffer_write_16_be( buffer_page + 0, checksum); + put_u16be( buffer_page + 0, checksum); if (!cfs_block_to_page( cfs, block_type, block, &page)) return false; if (!cfs_page_write( cfs, buffer_page, page)) return false; return true; @@ -218,7 +196,7 @@ static int cfs_file_info( cybiko_file_system *cfs, uint16_t file_id, cfs_file *f if (BLOCK_PART_ID(buffer) == 0) { strcpy( file->name, BLOCK_FILENAME(buffer)); - file->date = buffer_read_32_be( buffer + 6 + FILE_HEADER_SIZE - 4); + file->date = get_u32be( buffer + 6 + FILE_HEADER_SIZE - 4); } file->size += buffer[1]; file->blocks++; @@ -460,13 +438,13 @@ static imgtoolerr_t cybiko_image_write_file(imgtool::partition &partition, const buffer[0] = 0x80; buffer[1] = (cfs->page_size - 2) - 6 - ((part_id == 0) ? FILE_HEADER_SIZE : 0); if (bytes_left < buffer[1]) buffer[1] = bytes_left; - buffer_write_16_be( buffer + 2, file_id); - buffer_write_16_be( buffer + 4, part_id); + put_u16be( buffer + 2, file_id); + put_u16be( buffer + 4, part_id); if (part_id == 0) { buffer[6] = 0x20; strcpy(BLOCK_FILENAME(buffer), filename); - buffer_write_32_be( buffer + 6 + FILE_HEADER_SIZE - 4, cybiko_time_setup(imgtool::datetime::now(imgtool::datetime::datetime_type::LOCAL))); + put_u32be( buffer + 6 + FILE_HEADER_SIZE - 4, cybiko_time_setup(imgtool::datetime::now(imgtool::datetime::datetime_type::LOCAL))); sourcef.read(buffer + 6 + FILE_HEADER_SIZE, buffer[1]); } else diff --git a/src/tools/imgtool/modules/psion.cpp b/src/tools/imgtool/modules/psion.cpp index ec556bd1cff..dba1130ddab 100644 --- a/src/tools/imgtool/modules/psion.cpp +++ b/src/tools/imgtool/modules/psion.cpp @@ -24,6 +24,7 @@ #include "imgtool.h" +#include "multibyte.h" #include "opresolv.h" #include @@ -58,24 +59,23 @@ static psion_pack *get_psion_pack(imgtool::image &image) return (psion_pack*)image.extra_bytes(); } -uint16_t head_checksum(uint8_t* data) +uint16_t head_checksum(const uint8_t* data) { uint16_t checksum = 0; for (int i=0; i<6; i+=2) - checksum += (data[i]<<8 | data[i+1]); + checksum += get_u16be(&data[i]); return checksum; } uint16_t get_long_rec_size(imgtool::stream &stream) { - uint8_t size_h, size_l; + uint8_t size[2]; - stream.read(&size_h, 1); - stream.read(&size_l, 1); + stream.read(size, 2); - return (size_h<<8) | size_l; + return get_u16be(size); } uint32_t update_pack_index(psion_pack *pack) -- cgit v1.2.3