Files
libaaruformat/src/open.c

362 lines
12 KiB
C
Raw Normal View History

2022-05-28 12:57:21 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2025-08-01 21:19:45 +01:00
* Copyright (c) 2019-2025 Natalia Portillo.
2022-05-28 12:57:21 +01:00
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
2019-03-17 21:14:40 +00:00
#include <inttypes.h>
2019-03-31 20:52:06 +01:00
#include <stdio.h>
2022-10-03 18:15:13 +01:00
#include <stdlib.h>
2019-03-17 21:14:40 +00:00
#include <string.h>
2022-05-28 12:10:04 +01:00
#include <aaruformat.h>
2025-08-01 21:10:36 +01:00
#include "internal.h"
#include "utarray.h"
2024-04-30 15:51:32 +01:00
void *aaruf_open(const char *filepath)
{
2025-08-01 21:10:36 +01:00
aaruformatContext *ctx = NULL;
int errorNo = 0;
size_t readBytes = 0;
long pos = 0;
uint8_t *data = NULL;
int i = 0, j = 0;
2025-08-01 21:10:36 +01:00
ChecksumHeader checksum_header;
ChecksumEntry const *checksum_entry = NULL;
2025-08-01 21:10:36 +01:00
uint32_t signature = 0;
UT_array *index_entries = NULL;
2020-03-01 19:51:13 +00:00
2024-04-30 15:51:32 +01:00
ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext));
2020-03-01 19:51:13 +00:00
memset(ctx, 0, sizeof(aaruformatContext));
2022-10-02 17:55:45 +01:00
if(ctx == NULL)
{
errno = AARUF_ERROR_NOT_ENOUGH_MEMORY;
return NULL;
}
ctx->imageStream = fopen(filepath, "rb");
if(ctx->imageStream == NULL)
{
errorNo = errno;
free(ctx);
errno = errorNo;
return NULL;
}
fseek(ctx->imageStream, 0, SEEK_SET);
2022-10-02 17:55:45 +01:00
readBytes = fread(&ctx->header, 1, sizeof(AaruHeader), ctx->imageStream);
2020-03-01 19:55:22 +00:00
if(readBytes != sizeof(AaruHeader))
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_FILE_TOO_SMALL;
return NULL;
}
2020-03-01 19:58:09 +00:00
if(ctx->header.identifier != DIC_MAGIC && ctx->header.identifier != AARU_MAGIC)
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_NOT_AARUFORMAT;
return NULL;
}
2020-03-01 19:53:05 +00:00
if(ctx->header.imageMajorVersion > AARUF_VERSION)
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_INCOMPATIBLE_VERSION;
return NULL;
}
2024-04-30 15:51:32 +01:00
fprintf(stderr, "libaaruformat: Opening image version %d.%d\n", ctx->header.imageMajorVersion,
ctx->header.imageMinorVersion);
2024-04-30 15:51:32 +01:00
ctx->readableSectorTags = (bool *)malloc(sizeof(bool) * MaxSectorTag);
2019-03-31 14:56:03 +01:00
if(ctx->readableSectorTags == NULL)
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_NOT_ENOUGH_MEMORY;
2019-03-31 14:56:03 +01:00
return NULL;
}
memset(ctx->readableSectorTags, 0, sizeof(bool) * MaxSectorTag);
2019-03-20 22:31:50 +00:00
ctx->imageInfo.Application = ctx->header.application;
2024-04-30 15:51:32 +01:00
ctx->imageInfo.ApplicationVersion = (uint8_t *)malloc(32);
2019-03-20 22:31:50 +00:00
if(ctx->imageInfo.ApplicationVersion != NULL)
{
memset(ctx->imageInfo.ApplicationVersion, 0, 32);
2024-04-30 15:51:32 +01:00
sprintf((char *)ctx->imageInfo.ApplicationVersion, "%d.%d", ctx->header.applicationMajorVersion,
2019-03-20 22:31:50 +00:00
ctx->header.applicationMinorVersion);
}
2024-04-30 15:51:32 +01:00
ctx->imageInfo.Version = (uint8_t *)malloc(32);
2019-03-20 22:31:50 +00:00
if(ctx->imageInfo.Version != NULL)
{
memset(ctx->imageInfo.Version, 0, 32);
2024-04-30 15:51:32 +01:00
sprintf((char *)ctx->imageInfo.Version, "%d.%d", ctx->header.imageMajorVersion, ctx->header.imageMinorVersion);
2019-03-20 22:31:50 +00:00
}
ctx->imageInfo.MediaType = ctx->header.mediaType;
2019-03-17 21:14:40 +00:00
// Read the index header
pos = fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
2019-03-17 21:14:40 +00:00
if(pos < 0)
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_CANNOT_READ_INDEX;
2019-03-17 21:14:40 +00:00
return NULL;
}
pos = ftell(ctx->imageStream);
if(pos != ctx->header.indexOffset)
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_CANNOT_READ_INDEX;
2019-03-17 21:14:40 +00:00
return NULL;
}
2025-08-01 21:10:36 +01:00
readBytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream);
2019-03-17 21:14:40 +00:00
2025-08-01 21:18:48 +01:00
if(readBytes != sizeof(uint32_t) || (signature != IndexBlock && signature != IndexBlock2))
2019-03-17 21:14:40 +00:00
{
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_CANNOT_READ_INDEX;
2019-03-17 21:14:40 +00:00
return NULL;
}
2025-08-01 21:18:48 +01:00
if(signature == IndexBlock)
index_entries = process_index_v1(ctx);
else if(signature == IndexBlock2)
index_entries = process_index_v2(ctx);
2019-03-17 21:14:40 +00:00
2025-08-01 21:10:36 +01:00
if(index_entries == NULL)
2019-03-17 21:14:40 +00:00
{
2025-08-01 21:10:36 +01:00
fprintf(stderr, "Could not process index.\n");
utarray_free(index_entries);
2019-03-17 21:14:40 +00:00
free(ctx);
2020-03-01 19:53:05 +00:00
errno = AARUF_ERROR_CANNOT_READ_INDEX;
2019-03-17 21:14:40 +00:00
return NULL;
}
2025-08-01 21:10:36 +01:00
fprintf(stderr, "libaaruformat: Index at %" PRIu64 " contains %d entries\n", ctx->header.indexOffset,
utarray_len(index_entries));
for(i = 0; i < utarray_len(index_entries); i++)
2019-03-17 21:14:40 +00:00
{
2025-08-01 21:10:36 +01:00
IndexEntry *entry = (IndexEntry *)utarray_eltptr(index_entries, i);
2024-04-30 15:51:32 +01:00
fprintf(stderr, "libaaruformat: Block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n",
2025-08-01 21:10:36 +01:00
(char *)&entry->blockType, entry->dataType, entry->offset);
2019-03-17 21:14:40 +00:00
}
2019-03-31 20:52:06 +01:00
bool foundUserDataDdt = false;
2019-03-20 22:31:50 +00:00
ctx->imageInfo.ImageSize = 0;
2025-08-01 21:10:36 +01:00
for(i = 0; i < utarray_len(index_entries); i++)
2019-03-17 21:32:02 +00:00
{
2025-08-01 21:10:36 +01:00
IndexEntry *entry = (IndexEntry *)utarray_eltptr(index_entries, i);
pos = fseek(ctx->imageStream, entry->offset, SEEK_SET);
2019-03-17 21:32:02 +00:00
2025-08-01 21:10:36 +01:00
if(pos < 0 || ftell(ctx->imageStream) != entry->offset)
2019-03-17 21:32:02 +00:00
{
fprintf(stderr,
"libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...\n",
2025-08-01 21:10:36 +01:00
entry->offset, i);
2019-03-17 21:32:02 +00:00
continue;
}
2025-08-01 21:10:36 +01:00
switch(entry->blockType)
2019-03-17 21:32:02 +00:00
{
case DataBlock:
errorNo = process_data_block(ctx, entry);
if(errorNo != AARUF_STATUS_OK)
{
utarray_free(index_entries);
free(ctx);
errno = errorNo;
return NULL;
}
2019-03-17 21:32:02 +00:00
break;
2019-03-31 20:52:06 +01:00
case DeDuplicationTable:
errorNo = process_ddt_v1(ctx, entry, &foundUserDataDdt);
if(errorNo != AARUF_STATUS_OK)
{
utarray_free(index_entries);
free(ctx);
errno = errorNo;
return NULL;
}
break;
2019-03-31 20:52:06 +01:00
case GeometryBlock:
process_geometry_block(ctx, entry);
2019-03-17 23:29:29 +00:00
2019-03-17 21:32:02 +00:00
break;
2019-03-31 20:52:06 +01:00
case MetadataBlock:
process_metadata_block(ctx, entry);
2019-03-20 22:31:50 +00:00
2019-03-17 21:32:02 +00:00
break;
2019-03-31 20:52:06 +01:00
case TracksBlock:
process_tracks_block(ctx, entry);
2022-10-12 16:19:48 +01:00
2019-03-17 21:32:02 +00:00
break;
2019-03-31 20:52:06 +01:00
case CicmBlock:
process_cicm_block(ctx, entry);
2019-03-18 22:06:10 +00:00
2019-03-17 21:32:02 +00:00
break;
2019-03-20 00:23:30 +00:00
// Dump hardware block
2019-03-31 20:52:06 +01:00
case DumpHardwareBlock:
process_dumphw_block(ctx, entry);
2019-03-20 00:23:30 +00:00
2022-10-04 19:44:34 +01:00
break;
case ChecksumBlock:
readBytes = fread(&checksum_header, 1, sizeof(ChecksumHeader), ctx->imageStream);
if(readBytes != sizeof(ChecksumHeader))
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
fprintf(stderr, "libaaruformat: Could not read checksums block header, continuing...\n");
break;
}
if(checksum_header.identifier != ChecksumBlock)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
2024-04-30 15:51:32 +01:00
fprintf(stderr, "libaaruformat: Incorrect identifier for checksum block at position %" PRIu64 "\n",
2025-08-01 21:10:36 +01:00
entry->offset);
2022-10-04 19:44:34 +01:00
}
2024-04-30 15:51:32 +01:00
data = (uint8_t *)malloc(checksum_header.length);
2022-10-04 19:44:34 +01:00
if(data == NULL)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
fprintf(stderr, "libaaruformat: Could not allocate memory for checksum block, continuing...\n");
break;
}
readBytes = fread(data, 1, checksum_header.length, ctx->imageStream);
if(readBytes != checksum_header.length)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
free(data);
fprintf(stderr, "libaaruformat: Could not read checksums block, continuing...\n");
break;
}
pos = 0;
for(j = 0; j < checksum_header.entries; j++)
{
2024-04-30 15:51:32 +01:00
checksum_entry = (ChecksumEntry *)(&data[pos]);
2022-10-04 19:44:34 +01:00
pos += sizeof(ChecksumEntry);
if(checksum_entry->type == Md5)
{
memcpy(ctx->checksums.md5, &data[pos], MD5_DIGEST_LENGTH);
ctx->checksums.hasMd5 = true;
}
else if(checksum_entry->type == Sha1)
{
memcpy(ctx->checksums.sha1, &data[pos], SHA1_DIGEST_LENGTH);
ctx->checksums.hasSha1 = true;
}
else if(checksum_entry->type == Sha256)
{
memcpy(ctx->checksums.sha256, &data[pos], SHA256_DIGEST_LENGTH);
ctx->checksums.hasSha256 = true;
}
else if(checksum_entry->type == SpamSum)
{
ctx->checksums.spamsum = malloc(checksum_entry->length + 1);
if(ctx->checksums.spamsum != NULL)
{
memcpy(ctx->checksums.spamsum, &data[pos], checksum_entry->length);
ctx->checksums.hasSpamSum = true;
}
ctx->checksums.spamsum[checksum_entry->length] = 0;
}
pos += checksum_entry->length;
}
checksum_entry = NULL;
free(data);
2019-03-17 21:32:02 +00:00
break;
default:
fprintf(stderr,
"libaaruformat: Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n",
2025-08-01 21:10:36 +01:00
(char *)&entry->blockType, entry->dataType, entry->offset);
2019-03-17 21:32:02 +00:00
break;
}
}
2025-08-01 21:10:36 +01:00
utarray_free(index_entries);
2019-03-20 22:45:05 +00:00
2019-03-20 00:35:11 +00:00
if(!foundUserDataDdt)
{
fprintf(stderr, "libaaruformat: Could not find user data deduplication table, aborting...\n");
2022-05-28 12:01:55 +01:00
aaruf_close(ctx);
2019-03-20 00:35:11 +00:00
return NULL;
}
2019-03-20 22:31:50 +00:00
ctx->imageInfo.CreationTime = ctx->header.creationTime;
ctx->imageInfo.LastModificationTime = ctx->header.lastWrittenTime;
2022-05-28 12:01:55 +01:00
ctx->imageInfo.XmlMediaType = aaruf_get_xml_mediatype(ctx->header.mediaType);
2019-03-20 22:31:50 +00:00
2019-03-31 20:52:06 +01:00
if(ctx->geometryBlock.identifier != GeometryBlock && ctx->imageInfo.XmlMediaType == BlockMedia)
2019-03-20 22:31:50 +00:00
{
ctx->imageInfo.Cylinders = (uint32_t)(ctx->imageInfo.Sectors / 16 / 63);
ctx->imageInfo.Heads = 16;
ctx->imageInfo.SectorsPerTrack = 63;
}
2019-03-20 00:35:11 +00:00
2022-10-02 16:05:25 +01:00
// Initialize caches
ctx->blockHeaderCache.cache = NULL;
ctx->blockHeaderCache.max_items = MAX_CACHE_SIZE / (ctx->imageInfo.SectorSize * (1 << ctx->shift));
ctx->blockCache.cache = NULL;
ctx->blockCache.max_items = ctx->blockHeaderCache.max_items;
2019-03-20 00:35:11 +00:00
// TODO: Cache tracks and sessions?
2019-03-23 22:59:36 +00:00
// Initialize ECC for Compact Disc
2024-04-30 15:51:32 +01:00
ctx->eccCdContext = (CdEccContext *)aaruf_ecc_cd_init();
2019-03-20 00:35:11 +00:00
2020-03-01 19:58:09 +00:00
ctx->magic = AARU_MAGIC;
2020-03-01 19:53:05 +00:00
ctx->libraryMajorVersion = LIBAARUFORMAT_MAJOR_VERSION;
ctx->libraryMinorVersion = LIBAARUFORMAT_MINOR_VERSION;
2019-03-17 21:14:40 +00:00
return ctx;
}