diff --git a/include/aaruformat.h b/include/aaruformat.h index b9acd1a..a3bd036 100644 --- a/include/aaruformat.h +++ b/include/aaruformat.h @@ -26,6 +26,7 @@ #include "aaruformat/context.h" #include "aaruformat/crc64.h" #include "aaruformat/decls.h" +#include "aaruformat/endian.h" #include "aaruformat/enums.h" #include "aaruformat/errors.h" #include "aaruformat/lru.h" diff --git a/include/aaruformat/endian.h b/include/aaruformat/endian.h new file mode 100644 index 0000000..4bbd735 --- /dev/null +++ b/include/aaruformat/endian.h @@ -0,0 +1,89 @@ +/* + * This file is part of the Aaru Data Preservation Suite. + * Copyright (c) 2019-2022 Natalia Portillo. + * + * 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 . + */ + +#ifndef LIBAARUFORMAT_ENDIAN_H +#define LIBAARUFORMAT_ENDIAN_H + +#ifdef _MSC_VER + +#include +#define bswap_16(x) _byteswap_ushort(x) +#define bswap_32(x) _byteswap_ulong(x) +#define bswap_64(x) _byteswap_uint64(x) + +#elif defined(__APPLE__) + +// Mac OS X / Darwin features +#include + +#define bswap_16(x) OSSwapInt16(x) +#define bswap_32(x) OSSwapInt32(x) +#define bswap_64(x) OSSwapInt64(x) + +#elif defined(__sun) || defined(sun) + +#include +#define bswap_16(x) BSWAP_16(x) +#define bswap_32(x) BSWAP_32(x) +#define bswap_64(x) BSWAP_64(x) + +#elif defined(__FreeBSD__) + +#include +#define bswap_16(x) bswap16(x) +#define bswap_32(x) bswap32(x) +#define bswap_64(x) bswap64(x) + +#elif defined(__OpenBSD__) + +#include +#define bswap_16(x) swap16(x) +#define bswap_32(x) swap32(x) +#define bswap_64(x) swap64(x) + +#elif defined(__NetBSD__) + +#include +#include +#if defined(__BSWAP_RENAME) && !defined(__bswap_32) +#define bswap_16(x) bswap16(x) +#define bswap_32(x) bswap32(x) +#define bswap_64(x) bswap64(x) +#endif + +#elif defined(__linux__) + +#include + +#else + +#include + +#define bswap_16(x) ((uint16_t)((((uint16_t)(x)&0xff00) >> 8) | (((uint16_t)(x)&0x00ff) << 8))) +#define bswap_32(x) \ + ((uint32_t)((((uint32_t)(x)&0xff000000) >> 24) | (((uint32_t)(x)&0x00ff0000) >> 8) | \ + (((uint32_t)(x)&0x0000ff00) << 8) | (((uint32_t)(x)&0x000000ff) << 24))) +#define bswap_64(x) \ + ((uint64_t)((((uint64_t)(x)&0xff00000000000000ULL) >> 56) | (((uint64_t)(x)&0x00ff000000000000ULL) >> 40) | \ + (((uint64_t)(x)&0x0000ff0000000000ULL) >> 24) | (((uint64_t)(x)&0x000000ff00000000ULL) >> 8) | \ + (((uint64_t)(x)&0x00000000ff000000ULL) << 8) | (((uint64_t)(x)&0x0000000000ff0000ULL) << 24) | \ + (((uint64_t)(x)&0x000000000000ff00ULL) << 40) | (((uint64_t)(x)&0x00000000000000ffULL) << 56))) + +#endif + +#endif //LIBAARUFORMAT_ENDIAN_H diff --git a/src/open.c b/src/open.c index 54ef813..4f20d5e 100644 --- a/src/open.c +++ b/src/open.c @@ -288,6 +288,10 @@ void* aaruf_open(const char* filepath) } crc64 = aaruf_crc64_data(data, blockHeader.length); + + // Due to how C# wrote it, it is effectively reversed + if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64); + if(crc64 != blockHeader.crc64) { fprintf(stderr, @@ -763,6 +767,10 @@ void* aaruf_open(const char* filepath) crc64 = aaruf_crc64_data((const uint8_t*)ctx->trackEntries, ctx->tracksHeader.entries * sizeof(TrackEntry)); + + // Due to how C# wrote it, it is effectively reversed + if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64); + if(crc64 != ctx->tracksHeader.crc64) { fprintf(stderr, @@ -877,6 +885,10 @@ void* aaruf_open(const char* filepath) if(readBytes == ctx->dumpHardwareHeader.length) { crc64 = aaruf_crc64_data(data, ctx->dumpHardwareHeader.length); + + // Due to how C# wrote it, it is effectively reversed + if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64); + if(crc64 != ctx->dumpHardwareHeader.crc64) { free(data); @@ -1133,8 +1145,7 @@ void* aaruf_open(const char* filepath) break; default: fprintf(stderr, - "libaaruformat: Unhandled block type %4.4s with data type %4.4s is indexed to be at %" PRIu64 - "\n", + "libaaruformat: Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n", (char*)&idxEntries[i].blockType, (char*)&idxEntries[i].dataType, idxEntries[i].offset);