2017-09-30 14:19:50 +00:00
|
|
|
#ifndef __TRACYFILEREAD_HPP__
|
|
|
|
#define __TRACYFILEREAD_HPP__
|
|
|
|
|
2017-09-30 16:43:39 +00:00
|
|
|
#include <algorithm>
|
2018-04-21 12:53:40 +00:00
|
|
|
#include <stdexcept>
|
2017-09-30 14:19:50 +00:00
|
|
|
#include <stdio.h>
|
2017-10-01 00:23:30 +00:00
|
|
|
#include <string.h>
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2018-04-21 12:53:40 +00:00
|
|
|
#include "TracyFileHeader.hpp"
|
2017-09-30 17:25:24 +00:00
|
|
|
#include "../common/tracy_lz4.hpp"
|
2017-11-19 21:02:04 +00:00
|
|
|
#include "../common/TracyForceInline.hpp"
|
2017-09-30 17:25:24 +00:00
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2018-04-21 12:53:40 +00:00
|
|
|
struct NotTracyDump : public std::exception {};
|
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
class FileRead
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static FileRead* Open( const char* fn )
|
|
|
|
{
|
|
|
|
auto f = fopen( fn, "rb" );
|
|
|
|
return f ? new FileRead( f ) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~FileRead()
|
|
|
|
{
|
|
|
|
fclose( m_file );
|
2017-09-30 17:25:24 +00:00
|
|
|
LZ4_freeStreamDecode( m_stream );
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:02:04 +00:00
|
|
|
tracy_force_inline void Read( void* ptr, size_t size )
|
2017-09-30 14:19:50 +00:00
|
|
|
{
|
2017-09-30 23:59:07 +00:00
|
|
|
if( size <= BufSize - m_offset )
|
2017-09-30 16:43:39 +00:00
|
|
|
{
|
2018-03-17 12:57:32 +00:00
|
|
|
ReadSmall( ptr, size );
|
2017-09-30 23:59:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-17 12:57:32 +00:00
|
|
|
ReadBig( ptr, size );
|
2017-09-30 16:43:39 +00:00
|
|
|
}
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 00:31:03 +00:00
|
|
|
tracy_force_inline void Skip( size_t size )
|
2018-04-20 12:27:20 +00:00
|
|
|
{
|
|
|
|
if( size <= BufSize - m_offset )
|
|
|
|
{
|
|
|
|
m_offset += size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-30 00:31:03 +00:00
|
|
|
SkipBig( size );
|
2018-04-20 12:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 23:54:25 +00:00
|
|
|
bool IsEOF()
|
|
|
|
{
|
|
|
|
if( m_lastBlock != BufSize && m_offset == m_lastBlock ) return true;
|
|
|
|
if( m_offset == BufSize )
|
|
|
|
{
|
|
|
|
if( fseek( m_file, 1, SEEK_CUR ) != 0 ) return true;
|
|
|
|
fseek( m_file, -1, SEEK_CUR );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
private:
|
2017-09-30 16:43:39 +00:00
|
|
|
FileRead( FILE* f )
|
2017-09-30 17:25:24 +00:00
|
|
|
: m_stream( LZ4_createStreamDecode() )
|
|
|
|
, m_file( f )
|
2018-04-30 00:26:15 +00:00
|
|
|
, m_buf( m_bufData[0] )
|
|
|
|
, m_second( m_bufData[1] )
|
2017-09-30 16:43:39 +00:00
|
|
|
, m_offset( BufSize )
|
2018-04-01 23:54:25 +00:00
|
|
|
, m_lastBlock( 0 )
|
2018-04-21 12:53:40 +00:00
|
|
|
{
|
|
|
|
char hdr[4];
|
|
|
|
if( fread( hdr, 1, sizeof( hdr ), m_file ) != sizeof( hdr ) ) throw NotTracyDump();
|
|
|
|
if( memcmp( hdr, Lz4Header, sizeof( hdr ) ) != 0 )
|
|
|
|
{
|
|
|
|
fseek( m_file, 0, SEEK_SET );
|
|
|
|
uint32_t sz;
|
|
|
|
static_assert( sizeof( sz ) == sizeof( hdr ), "Size mismatch" );
|
|
|
|
memcpy( &sz, hdr, sizeof( sz ) );
|
|
|
|
if( sz > LZ4Size ) throw NotTracyDump();
|
|
|
|
}
|
|
|
|
}
|
2017-09-30 16:43:39 +00:00
|
|
|
|
2018-03-17 12:57:32 +00:00
|
|
|
tracy_force_inline void ReadSmall( void* ptr, size_t size )
|
|
|
|
{
|
2018-04-30 00:26:15 +00:00
|
|
|
memcpy( ptr, m_buf + m_offset, size );
|
2018-03-17 12:57:32 +00:00
|
|
|
m_offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReadBig( void* ptr, size_t size )
|
|
|
|
{
|
2018-03-17 13:22:36 +00:00
|
|
|
char m_lz4buf[LZ4Size];
|
2018-03-17 12:57:32 +00:00
|
|
|
auto dst = (char*)ptr;
|
|
|
|
while( size > 0 )
|
|
|
|
{
|
|
|
|
if( m_offset == BufSize )
|
|
|
|
{
|
2018-04-30 00:26:15 +00:00
|
|
|
std::swap( m_buf, m_second );
|
2018-03-17 12:57:32 +00:00
|
|
|
m_offset = 0;
|
|
|
|
uint32_t sz;
|
|
|
|
fread( &sz, 1, sizeof( sz ), m_file );
|
|
|
|
fread( m_lz4buf, 1, sz, m_file );
|
2018-04-30 00:26:15 +00:00
|
|
|
m_lastBlock = LZ4_decompress_safe_continue( m_stream, m_lz4buf, m_buf, sz, BufSize );
|
2018-03-17 12:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto sz = std::min( size, BufSize - m_offset );
|
2018-04-30 00:26:15 +00:00
|
|
|
memcpy( dst, m_buf + m_offset, sz );
|
2018-03-17 12:57:32 +00:00
|
|
|
m_offset += sz;
|
|
|
|
dst += sz;
|
|
|
|
size -= sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:31:03 +00:00
|
|
|
void SkipBig( size_t size )
|
|
|
|
{
|
|
|
|
char m_lz4buf[LZ4Size];
|
|
|
|
while( size > 0 )
|
|
|
|
{
|
|
|
|
if( m_offset == BufSize )
|
|
|
|
{
|
|
|
|
std::swap( m_buf, m_second );
|
|
|
|
m_offset = 0;
|
|
|
|
uint32_t sz;
|
|
|
|
fread( &sz, 1, sizeof( sz ), m_file );
|
|
|
|
fread( m_lz4buf, 1, sz, m_file );
|
|
|
|
m_lastBlock = LZ4_decompress_safe_continue( m_stream, m_lz4buf, m_buf, sz, BufSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto sz = std::min( size, BufSize - m_offset );
|
|
|
|
m_offset += sz;
|
|
|
|
size -= sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 16:43:39 +00:00
|
|
|
enum { BufSize = 64 * 1024 };
|
2017-09-30 17:25:24 +00:00
|
|
|
enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) };
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2017-09-30 17:25:24 +00:00
|
|
|
LZ4_streamDecode_t* m_stream;
|
2017-09-30 14:19:50 +00:00
|
|
|
FILE* m_file;
|
2018-04-30 00:26:15 +00:00
|
|
|
char m_bufData[2][BufSize];
|
|
|
|
char* m_buf;
|
|
|
|
char* m_second;
|
2017-09-30 16:43:39 +00:00
|
|
|
size_t m_offset;
|
2018-04-01 23:54:25 +00:00
|
|
|
int m_lastBlock;
|
2017-09-30 14:19:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|