diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index 87a8275..b0c7218 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -4,5 +4,5 @@ find_package(ICU COMPONENTS uc REQUIRED) include_directories(${ICU_INCLUDE_DIRS}) -add_executable(aaruformattool main.c main.h aaruformattool.h identify.c info.c helpers.c printhex.c) +add_executable(aaruformattool main.c main.h aaruformattool.h identify.c info.c helpers.c read.c printhex.c) target_link_libraries(aaruformattool "aaruformat" ICU::uc) diff --git a/tool/aaruformattool.h b/tool/aaruformattool.h index 07e0d6f..a72860a 100644 --- a/tool/aaruformattool.h +++ b/tool/aaruformattool.h @@ -24,6 +24,7 @@ int identify(char* path); 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); #endif // LIBAARUFORMAT_TOOL_AARUFORMATTOOL_H_ diff --git a/tool/main.c b/tool/main.c index 7b4492b..c1dfbe6 100644 --- a/tool/main.c +++ b/tool/main.c @@ -17,7 +17,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include #include +#include #include @@ -33,6 +35,7 @@ void usage() printf("Available verbs:\n"); 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("\n"); printf("For help on the verb invoke the tool with the verb and no arguments.\n"); } @@ -59,8 +62,22 @@ void usage_info() printf("\t\tPath to AaruFormat image to print information from.\n"); } +void usage_read() +{ + printf("\n"); + printf("Usage:\n"); + printf("aaruformattool read \n"); + printf("Reads a sector 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; + printf("AaruFormat Tool version %d.%d\n", AARUFORMAT_TOOL_MAJOR_VERSION, AARUFORMAT_TOOL_MINOR_VERSION); printf("Copyright (C) 2019-2022 Natalia Portillo\n"); printf("libaaruformat version %d.%d\n", LIBAARUFORMAT_MAJOR_VERSION, LIBAARUFORMAT_MINOR_VERSION); @@ -90,7 +107,6 @@ int main(int argc, char* argv[]) return identify(argv[2]); } - if(strncmp(argv[1], "info", strlen("info")) == 0) { if(argc == 2) @@ -109,6 +125,34 @@ int main(int argc, char* argv[]) return info(argv[2]); } + if(strncmp(argv[1], "read", strlen("read")) == 0) + { + if(argc < 4) + { + usage_read(); + return -1; + } + + if(argc > 5) + { + fprintf(stderr, "Invalid number of arguments\n"); + usage_read(); + return -1; + } + + errno = 0; + + sector_no = strtoll(argv[2], NULL, 10); + + if(errno != 0) + { + fprintf(stderr, "Invalid sector number\n"); + usage_read(); + return -1; + } + + return read(sector_no, argv[3]); + } return 0; } diff --git a/tool/read.c b/tool/read.c new file mode 100644 index 0000000..580b759 --- /dev/null +++ b/tool/read.c @@ -0,0 +1,75 @@ +/* + * This file is part of the Aaru Data Preservation Suite. + * Copyright (c) 2019-2022 Natalia Portillo. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include + +#include + +#include "aaruformattool.h" + +int read(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(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(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