diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b94d7d..ae8ea61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(libdicformat C) set(CMAKE_C_STANDARD 99) -add_library(libdicformat SHARED include/dicformat/consts.h include/dicformat/enums.h include/dic.h include/dicformat.h include/dicformat/decls.h include/dicformat/structs.h) +add_library(libdicformat SHARED include/dicformat/consts.h include/dicformat/enums.h include/dic.h include/dicformat.h + include/dicformat/decls.h include/dicformat/structs.h src/identify.c) include_directories(include include/dicformat) \ No newline at end of file diff --git a/include/dicformat.h b/include/dicformat.h index 948e708..b307382 100644 --- a/include/dicformat.h +++ b/include/dicformat.h @@ -35,5 +35,7 @@ #include "dicformat/consts.h" #include "dicformat/enums.h" +#include "dicformat/decls.h" +#include "dicformat/structs.h" #endif //LIBDICFORMAT_DICFORMAT_H diff --git a/include/dicformat/decls.h b/include/dicformat/decls.h index ea8ab67..537c9f4 100644 --- a/include/dicformat/decls.h +++ b/include/dicformat/decls.h @@ -33,4 +33,8 @@ #ifndef LIBDICFORMAT_DECLS_H #define LIBDICFORMAT_DECLS_H +int identify(const char *filename); + +int identifyStream(FILE *imageStream); + #endif //LIBDICFORMAT_DECLS_H diff --git a/src/identify.c b/src/identify.c new file mode 100644 index 0000000..3002f1f --- /dev/null +++ b/src/identify.c @@ -0,0 +1,82 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : identify.c +// Author(s) : Natalia Portillo +// +// Component : Disk image plugins. +// +// --[ Description ] ---------------------------------------------------------- +// +// Identifies DiscImageChef format disk images. +// +// --[ License ] -------------------------------------------------------------- +// +// 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2019 Natalia Portillo +// ****************************************************************************/ + +#include +#include +#include + +//! Identifies a file as dicformat, using path +/*! + * + * @param filename path to the file to identify + * @return If positive, confidence value, with 100 being maximum confidentiality, and 0 not recognizing the file. + * If negative, error value + */ +int identify(const char *filename) +{ + FILE *stream; + + stream = fopen(filename, "rb"); + + if(stream == NULL) + return errno; + + int ret = identifyStream(stream); + + fclose(stream); + + return ret; +} + +//! Identifies a file as dicformat, using an already existing stream +/*! + * + * @param imageStream stream of the file to identify + * @return If positive, confidence value, with 100 being maximum confidentiality, and 0 not recognizing the file. + * If negative, error value + */ +int identifyStream(FILE *imageStream) +{ + fseek(imageStream, 0, SEEK_SET); + + DicHeader header; + + size_t ret = fread(&header, sizeof(DicHeader), 1, imageStream); + + if(ret < sizeof(DicHeader)) + return 0; + + if(header.identifier == DIC_MAGIC && header.imageMajorVersion <= DICF_VERSION) + return 100; + + return 0; +} \ No newline at end of file