MCPcopy Create free account
hub / github.com/assimp/assimp / CheckMagicToken

Method CheckMagicToken

code/Common/BaseImporter.cpp:304–355  ·  view source on GitHub ↗

------------------------------------------------------------------------------------------------ Check for magic bytes at the beginning of the file. static */

Source from the content-addressed store, hash-verified

302// ------------------------------------------------------------------------------------------------
303// Check for magic bytes at the beginning of the file.
304/* static */ bool BaseImporter::CheckMagicToken(IOSystem *pIOHandler, const std::string &pFile,
305 const void *_magic, std::size_t num, unsigned int offset, unsigned int size) {
306 ai_assert(size <= 16);
307 ai_assert(_magic);
308
309 if (!pIOHandler) {
310 return false;
311 }
312 const char *magic = reinterpret_cast<const char *>(_magic);
313 std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile));
314 if (pStream) {
315
316 // skip to offset
317 pStream->Seek(offset, aiOrigin_SET);
318
319 // read 'size' characters from the file
320 union {
321 char data[16];
322 uint16_t data_u16[8];
323 uint32_t data_u32[4];
324 };
325 if (size != pStream->Read(data, 1, size)) {
326 return false;
327 }
328
329 for (unsigned int i = 0; i < num; ++i) {
330 // also check against big endian versions of tokens with size 2,4
331 // that's just for convenience, the chance that we cause conflicts
332 // is quite low and it can save some lines and prevent nasty bugs
333 if (2 == size) {
334 uint16_t magic_u16;
335 memcpy(&magic_u16, magic, 2);
336 if (data_u16[0] == magic_u16 || data_u16[0] == ByteSwap::Swapped(magic_u16)) {
337 return true;
338 }
339 } else if (4 == size) {
340 uint32_t magic_u32;
341 memcpy(&magic_u32, magic, 4);
342 if (data_u32[0] == magic_u32 || data_u32[0] == ByteSwap::Swapped(magic_u32)) {
343 return true;
344 }
345 } else {
346 // any length ... just compare
347 if (!memcmp(magic, data, size)) {
348 return true;
349 }
350 }
351 magic += size;
352 }
353 }
354 return false;
355}
356
357#include "utf8.h"
358

Callers

nothing calls this directly

Calls 3

OpenMethod · 0.45
SeekMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected