mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Implement read long in tool.
This commit is contained in:
@@ -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_
|
||||
|
||||
43
tool/main.c
43
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<filename>\tPath to AaruFormat image to print information from.\n");
|
||||
}
|
||||
|
||||
void usage_read_long()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool read_long <sector_number> <filename>\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<sector_number>\tSector number to read and print out.\n");
|
||||
printf("\t<filename>\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)
|
||||
{
|
||||
|
||||
50
tool/read.c
50
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);
|
||||
}
|
||||
Reference in New Issue
Block a user