Files
libaaruformat/src/identify.c

69 lines
2.0 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/>.
*/
2019-03-17 19:07:57 +00:00
#include <errno.h>
#include <stdio.h>
2022-05-28 12:10:04 +01:00
#include <aaruformat.h>
/**
* @brief Identifies a file as an AaruFormat image using a file path.
2019-03-17 19:07:57 +00:00
*
* Opens the file at the given path and determines if it is an AaruFormat image.
*
* @param filename Path to the file to identify.
* @return If positive, confidence value (100 = maximum confidence, 0 = not recognized). If negative, error value.
2019-03-17 19:07:57 +00:00
*/
2024-04-30 15:51:32 +01:00
int aaruf_identify(const char *filename)
2019-03-17 19:07:57 +00:00
{
FILE *stream = NULL;
2019-03-17 19:07:57 +00:00
stream = fopen(filename, "rb");
2019-03-31 20:52:06 +01:00
if(stream == NULL) return errno;
2019-03-17 19:07:57 +00:00
2022-05-28 12:01:55 +01:00
int ret = aaruf_identify_stream(stream);
2019-03-17 19:07:57 +00:00
fclose(stream);
return ret;
}
/**
* @brief Identifies a file as an AaruFormat image using an open stream.
*
* Determines if the provided stream is an AaruFormat image.
2019-03-17 19:07:57 +00:00
*
* @param image_stream Stream of the file to identify.
* @return If positive, confidence value (100 = maximum confidence, 0 = not recognized). If negative, error value.
2019-03-17 19:07:57 +00:00
*/
int aaruf_identify_stream(FILE *image_stream)
2019-03-17 19:07:57 +00:00
{
fseek(image_stream, 0, SEEK_SET);
2019-03-17 19:07:57 +00:00
2020-03-01 19:55:22 +00:00
AaruHeader header;
2019-03-17 19:07:57 +00:00
size_t ret = fread(&header, sizeof(AaruHeader), 1, image_stream);
2019-03-17 19:07:57 +00:00
if(ret != 1) return 0;
2019-03-17 19:07:57 +00:00
2020-03-01 19:58:09 +00:00
if((header.identifier == DIC_MAGIC || header.identifier == AARU_MAGIC) && header.imageMajorVersion <= AARUF_VERSION)
return 100;
2019-03-17 19:07:57 +00:00
return 0;
}