From 352850a698088a964f330883bf509f35a497f7e4 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 30 Sep 2025 13:48:31 +0100 Subject: [PATCH] Refactor function signatures to use const char* for string parameters --- include/aaruformat/decls.h | 2 +- include/aaruformat/lru.h | 4 +-- src/lru.c | 8 +++--- src/write.c | 3 +- tool/aaruformattool.h | 14 +++++----- tool/cli_compare.c | 56 +++++++++++++++++--------------------- tool/compare.c | 2 +- tool/convert.c | 2 +- tool/identify.c | 2 +- tool/info.c | 2 +- tool/read.c | 24 +++++++++------- 11 files changed, 59 insertions(+), 60 deletions(-) diff --git a/include/aaruformat/decls.h b/include/aaruformat/decls.h index 5339de9..431aac6 100644 --- a/include/aaruformat/decls.h +++ b/include/aaruformat/decls.h @@ -83,7 +83,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector(void *context, uint64_t sectorAd AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(void *context, uint64_t sectorAddress, uint8_t *data, uint32_t *length); -AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, +AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t *data, uint8_t sectorStatus, uint32_t length); AARU_EXPORT int32_t AARU_CALL aaruf_verify_image(void *context); diff --git a/include/aaruformat/lru.h b/include/aaruformat/lru.h index aec931f..8244e4b 100644 --- a/include/aaruformat/lru.h +++ b/include/aaruformat/lru.h @@ -27,7 +27,7 @@ struct CacheHeader * @param key Key * @return Value if found, NULL if not */ -void *find_in_cache(struct CacheHeader *cache, char *key); +void *find_in_cache(struct CacheHeader *cache, const char *key); /** * Adds an item to the specified cache @@ -35,7 +35,7 @@ void *find_in_cache(struct CacheHeader *cache, char *key); * @param key Key * @param value Value */ -void add_to_cache(struct CacheHeader *cache, char *key, void *value); +void add_to_cache(struct CacheHeader *cache, const char *key, void *value); /** * Finds an item in the specified cache using a 64-bit integer key diff --git a/src/lru.c b/src/lru.c index a6fc379..e4f996a 100644 --- a/src/lru.c +++ b/src/lru.c @@ -13,15 +13,15 @@ // this code is in the public domain http://unlicense.org/ /** - * @brief Finds a value in the cache by string key and updates its LRU position. + * @brief Finds a value in the cache by string key. * - * Searches for a cache entry by key. If found, moves it to the front (most recently used). + * Searches for a value in the cache using a string key and moves it to the front if found. * * @param cache Pointer to the cache header. * @param key String key to search for. * @return Pointer to the value if found, or NULL if not found. */ -void *find_in_cache(struct CacheHeader *cache, char *key) +void *find_in_cache(struct CacheHeader *cache, const char *key) { struct CacheEntry *entry; HASH_FIND_STR(cache->cache, key, entry); @@ -44,7 +44,7 @@ void *find_in_cache(struct CacheHeader *cache, char *key) * @param key String key to add. * @param value Pointer to the value to store. */ -void add_to_cache(struct CacheHeader *cache, char *key, void *value) +void add_to_cache(struct CacheHeader *cache, const char *key, void *value) { struct CacheEntry *entry, *tmp_entry; // TODO: Is this needed or we're just losing cycles? uthash does not free the entry diff --git a/src/write.c b/src/write.c index 471d4ee..533b36d 100644 --- a/src/write.c +++ b/src/write.c @@ -38,7 +38,8 @@ * @param length Length of the data buffer. * @return AARUF_STATUS_OK on success, or an error code on failure. */ -int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, uint8_t sectorStatus, uint32_t length) +int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t *data, uint8_t sectorStatus, + uint32_t length) { TRACE("Entering aaruf_write_sector(%p, %" PRIu64 ", %p, %u, %u)", context, sectorAddress, data, sectorStatus, length); diff --git a/tool/aaruformattool.h b/tool/aaruformattool.h index 329558a..023ae34 100644 --- a/tool/aaruformattool.h +++ b/tool/aaruformattool.h @@ -23,18 +23,18 @@ #include -int identify(char *path); -int info(char *path); +int identify(const char *path); +int info(const char *path); char *byte_array_to_hex_string(const unsigned char *array, int array_size); -int read(unsigned long long sector_no, char *path); +int read(unsigned long long sector_no, const char *path); int printhex(unsigned char *array, unsigned int length, int width, bool color); -int read_long(unsigned long long sector_no, char *path); +int read_long(unsigned long long sector_no, const char *path); int verify(const char *path); int verify_sectors(const char *path); bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unknown, bool *has_edc, bool *edc_correct, bool *has_ecc_p, bool *ecc_p_correct, bool *has_ecc_q, bool *ecc_q_correct); -int compare(char *path1, char *path2); -int cli_compare(char *path1, char *path2); -int convert(char *input_path, char *output_path); +int compare(const char *path1, const char *path2); +int cli_compare(const char *path1, const char *path2); +int convert(const char *input_path, const char *output_path); #endif // LIBAARUFORMAT_TOOL_AARUFORMATTOOL_H_ diff --git a/tool/cli_compare.c b/tool/cli_compare.c index 694e179..ed0b958 100644 --- a/tool/cli_compare.c +++ b/tool/cli_compare.c @@ -18,28 +18,28 @@ */ #include +#include +#include #include #include #include -#include -#include #include "aaruformattool.h" -int cli_compare(char *path1, char *path2) +int cli_compare(const char *path1, const char *path2) { - aaruformatContext *ctx1 = NULL; - aaruformatContext *ctx2 = NULL; - uint8_t *buffer1 = NULL; - uint8_t *buffer2 = NULL; - uint32_t buffer1_length = 0; - uint32_t buffer2_length = 0; - uint64_t total_sectors = 0; + aaruformatContext *ctx1 = NULL; + aaruformatContext *ctx2 = NULL; + uint8_t *buffer1 = NULL; + uint8_t *buffer2 = NULL; + uint32_t buffer1_length = 0; + uint32_t buffer2_length = 0; + uint64_t total_sectors = 0; uint64_t different_sectors = 0; uint64_t sectors_processed = 0; - int result = 0; - int32_t read_result1 = 0; - int32_t read_result2 = 0; + int result = 0; + int32_t read_result1 = 0; + int32_t read_result2 = 0; printf("Opening first image: %s\n", path1); ctx1 = aaruf_open(path1); @@ -60,27 +60,24 @@ int cli_compare(char *path1, char *path2) // Access image information through context structure printf("\nImage Information:\n"); - printf("Image 1: %llu sectors, %u bytes per sector\n", - (unsigned long long)ctx1->imageInfo.Sectors, ctx1->imageInfo.SectorSize); - printf("Image 2: %llu sectors, %u bytes per sector\n", - (unsigned long long)ctx2->imageInfo.Sectors, ctx2->imageInfo.SectorSize); + printf("Image 1: %llu sectors, %u bytes per sector\n", (unsigned long long)ctx1->imageInfo.Sectors, + ctx1->imageInfo.SectorSize); + printf("Image 2: %llu sectors, %u bytes per sector\n", (unsigned long long)ctx2->imageInfo.Sectors, + ctx2->imageInfo.SectorSize); if(ctx1->imageInfo.Sectors != ctx2->imageInfo.Sectors) { fprintf(stderr, "Warning: Images have different number of sectors\n"); - total_sectors = ctx1->imageInfo.Sectors < ctx2->imageInfo.Sectors ? - ctx1->imageInfo.Sectors : ctx2->imageInfo.Sectors; + total_sectors = + ctx1->imageInfo.Sectors < ctx2->imageInfo.Sectors ? ctx1->imageInfo.Sectors : ctx2->imageInfo.Sectors; printf("Will compare first %llu sectors\n", (unsigned long long)total_sectors); } - else - { - total_sectors = ctx1->imageInfo.Sectors; - } + else { total_sectors = ctx1->imageInfo.Sectors; } if(ctx1->imageInfo.SectorSize != ctx2->imageInfo.SectorSize) { - fprintf(stderr, "Error: Images have different sector sizes (%u vs %u)\n", - ctx1->imageInfo.SectorSize, ctx2->imageInfo.SectorSize); + fprintf(stderr, "Error: Images have different sector sizes (%u vs %u)\n", ctx1->imageInfo.SectorSize, + ctx2->imageInfo.SectorSize); aaruf_close(ctx1); aaruf_close(ctx2); return -1; @@ -128,8 +125,7 @@ int cli_compare(char *path1, char *path2) sectors_different = true; } else if(sector1_available && sector2_available && - (buffer1_length != buffer2_length || - memcmp(buffer1, buffer2, buffer1_length) != 0)) + (buffer1_length != buffer2_length || memcmp(buffer1, buffer2, buffer1_length) != 0)) { // Both sectors are available - compare their content sectors_different = true; @@ -154,8 +150,7 @@ int cli_compare(char *path1, char *path2) if(sectors_processed % 1000 == 0 || sector == total_sectors - 1) { int progress = (int)((sectors_processed * 100) / total_sectors); - printf("Progress: %d%% (%llu/%llu sectors)\r", - progress, (unsigned long long)sectors_processed, + printf("Progress: %d%% (%llu/%llu sectors)\r", progress, (unsigned long long)sectors_processed, (unsigned long long)total_sectors); fflush(stdout); } @@ -173,8 +168,7 @@ int cli_compare(char *path1, char *path2) } else { - printf("✗ Images are different (%llu sectors differ)\n", - (unsigned long long)different_sectors); + printf("✗ Images are different (%llu sectors differ)\n", (unsigned long long)different_sectors); result = 1; // Non-zero exit code to indicate differences } diff --git a/tool/compare.c b/tool/compare.c index a70b1ad..3b1ac50 100644 --- a/tool/compare.c +++ b/tool/compare.c @@ -42,7 +42,7 @@ void draw_progress_bar(int row, int percent) tb_present(); } -int compare(char *path1, char *path2) +int compare(const char *path1, const char *path2) { int ret = AARUF_STATUS_OK; aaruformatContext *ctx1 = NULL; diff --git a/tool/convert.c b/tool/convert.c index 32cf70a..45f33a8 100644 --- a/tool/convert.c +++ b/tool/convert.c @@ -26,7 +26,7 @@ #include "aaruformattool.h" -int convert(char *input_path, char *output_path) +int convert(const char *input_path, const char *output_path) { aaruformatContext *input_ctx = NULL; aaruformatContext *output_ctx = NULL; diff --git a/tool/identify.c b/tool/identify.c index c96f2ff..3e17e79 100644 --- a/tool/identify.c +++ b/tool/identify.c @@ -19,7 +19,7 @@ #include -int identify(char *path) +int identify(const char *path) { int ret = 0; diff --git a/tool/info.c b/tool/info.c index dcafc33..b22baf2 100644 --- a/tool/info.c +++ b/tool/info.c @@ -27,7 +27,7 @@ #include "aaruformattool.h" -int info(char *path) +int info(const char *path) { aaruformatContext *ctx = NULL; char *strBuffer = NULL; diff --git a/tool/read.c b/tool/read.c index c116023..eacf02a 100644 --- a/tool/read.c +++ b/tool/read.c @@ -24,12 +24,12 @@ #include "aaruformattool.h" -int read(unsigned long long sector_no, char *path) +int read(unsigned long long sector_no, const char *path) { - aaruformatContext *ctx = NULL; - int32_t res = 0; - uint32_t length = 0; - uint8_t *data = NULL; + aaruformatContext *ctx = NULL; + int32_t res = 0; + uint32_t length = 0; + uint8_t *data = NULL; ctx = aaruf_open(path); @@ -72,14 +72,16 @@ int read(unsigned long long sector_no, char *path) free(data); aaruf_close(ctx); + + return AARUF_STATUS_OK; } -int read_long(unsigned long long sector_no, char *path) +int read_long(unsigned long long sector_no, const char *path) { - aaruformatContext *ctx = NULL; - int32_t res = 0; - uint32_t length = 0; - uint8_t *data = NULL; + aaruformatContext *ctx = NULL; + int32_t res = 0; + uint32_t length = 0; + uint8_t *data = NULL; ctx = aaruf_open(path); @@ -122,4 +124,6 @@ int read_long(unsigned long long sector_no, char *path) free(data); aaruf_close(ctx); + + return AARUF_STATUS_OK; } \ No newline at end of file