From 1477b2a650707eb28d6a8988526db9ed9e20fee1 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 12 Oct 2022 16:22:53 +0100 Subject: [PATCH] Implement read long in tool. --- tool/aaruformattool.h | 1 + tool/main.c | 43 ++++++++++++++++++++++++++++++++++++- tool/read.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/tool/aaruformattool.h b/tool/aaruformattool.h index a72860a..52fed52 100644 --- a/tool/aaruformattool.h +++ b/tool/aaruformattool.h @@ -26,5 +26,6 @@ int info(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 printhex(unsigned char* array, unsigned int length, int width, bool color); +int read_long(unsigned long long sector_no, char* path); #endif // LIBAARUFORMAT_TOOL_AARUFORMATTOOL_H_ diff --git a/tool/main.c b/tool/main.c index c1dfbe6..f83e16d 100644 --- a/tool/main.c +++ b/tool/main.c @@ -36,6 +36,7 @@ void usage() printf("\tidentify\tIdentifies if the indicated file is a supported AaruFormat image.\n"); printf("\tinfo\tPrints information about a given AaruFormat image.\n"); printf("\tread\tReads a sector and prints it out on screen.\n"); + printf("\tread_long\tReads a sector with all its prefixes and suffixes and prints it out on screen.\n"); printf("\n"); printf("For help on the verb invoke the tool with the verb and no arguments.\n"); } @@ -74,6 +75,18 @@ void usage_read() printf("\t\tPath to AaruFormat image to print information from.\n"); } +void usage_read_long() +{ + printf("\n"); + printf("Usage:\n"); + printf("aaruformattool read_long \n"); + printf("Reads a sector with all its prefixes and suffixes and prints it out on screen.\n"); + printf("\n"); + printf("Arguments:\n"); + printf("\t\tSector number to read and print out.\n"); + printf("\t\tPath to AaruFormat image to print information from.\n"); +} + int main(int argc, char* argv[]) { uint64_t sector_no = 0; @@ -125,7 +138,35 @@ int main(int argc, char* argv[]) return info(argv[2]); } - if(strncmp(argv[1], "read", strlen("read")) == 0) + if(strncmp(argv[1], "read_long", strlen("read_long")) == 0) + { + if(argc < 4) + { + usage_read_long(); + return -1; + } + + if(argc > 5) + { + fprintf(stderr, "Invalid number of arguments\n"); + usage_read_long(); + return -1; + } + + errno = 0; + + sector_no = strtoll(argv[2], NULL, 10); + + if(errno != 0) + { + fprintf(stderr, "Invalid sector number\n"); + usage_read_long(); + return -1; + } + + return read_long(sector_no, argv[3]); + } + else if(strncmp(argv[1], "read", strlen("read")) == 0) { if(argc < 4) { diff --git a/tool/read.c b/tool/read.c index 580b759..57dc5dc 100644 --- a/tool/read.c +++ b/tool/read.c @@ -71,5 +71,55 @@ int read(unsigned long long sector_no, char* path) free(data); + aaruf_close(ctx); +} + +int read_long(unsigned long long sector_no, char* path) +{ + aaruformatContext* ctx; + int32_t res; + uint32_t length; + uint8_t* data; + + ctx = aaruf_open(path); + + if(ctx == NULL) + { + printf("Error %d when opening AaruFormat image.\n", errno); + return errno; + } + + res = aaruf_read_sector_long(ctx, sector_no, NULL, &length); + + if(res != AARUF_STATUS_OK && res != AARUF_ERROR_BUFFER_TOO_SMALL) + { + printf("Error %d when guessing sector size.\n", res); + aaruf_close(ctx); + return res; + } + + printf("Sector is %d bytes.\n", length); + + data = malloc(length); + + if(data == NULL) + { + printf("Error allocating memory for sector.\n"); + return AARUF_ERROR_NOT_ENOUGH_MEMORY; + } + + res = aaruf_read_sector_long(ctx, sector_no, data, &length); + + if(res != AARUF_STATUS_OK) + { + printf("Error %d when reading sector.\n", res); + aaruf_close(ctx); + return res; + } + + printhex(data, length, 16, true); + + free(data); + aaruf_close(ctx); } \ No newline at end of file