76 lines
1.5 KiB
C++
76 lines
1.5 KiB
C++
#ifndef WC3RE_UTIL_HH__
|
|
#define WC3RE_UTIL_HH__
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
#ifdef WIN32
|
|
#define NOMINMAX
|
|
#include <Windows.h>
|
|
#endif
|
|
|
|
#include "common.hh"
|
|
#include "Resource.hh"
|
|
|
|
class MmapFile : public Resource {
|
|
public:
|
|
MmapFile(std::string fileName);
|
|
MmapFile(MmapFile const& copy) = delete;
|
|
MmapFile(MmapFile && move);
|
|
|
|
~MmapFile() override;
|
|
|
|
MmapFile& operator=(MmapFile const& copy) = delete;
|
|
MmapFile& operator=(MmapFile && move);
|
|
|
|
std::string const& name() const;
|
|
size_t size() const override;
|
|
uint8_t const* data() const override;
|
|
|
|
operator bool() const override;
|
|
|
|
size_t footprint() const {
|
|
return sizeof(MmapFile)+name_.capacity()+sizeof(FILE);
|
|
}
|
|
private:
|
|
std::string name_;
|
|
#ifndef WIN32
|
|
FILEUPtr fd_;
|
|
#else
|
|
HANDLE file_hnd_, fm_hnd_;
|
|
#endif
|
|
size_t size_;
|
|
void *base_;
|
|
};
|
|
|
|
// Helper functions to read big endian numbers
|
|
uint16_t readU16BE(uint8_t const* data);
|
|
uint32_t readU24BE(uint8_t const* data);
|
|
uint32_t readU32BE(uint8_t const* data);
|
|
|
|
// Helper functions to read little endian numbers
|
|
uint16_t readU16LE(uint8_t const* data);
|
|
uint32_t readU24LE(uint8_t const* data);
|
|
uint32_t readU32LE(uint8_t const* data);
|
|
|
|
// Sign-extend b starting at msb
|
|
int sextend(unsigned b, unsigned msb);
|
|
|
|
// Load simple resource from file
|
|
std::string fileToString(std::string const& name);
|
|
|
|
class BitReader {
|
|
public:
|
|
BitReader(uint8_t const* data, size_t len);
|
|
|
|
unsigned getBits(unsigned count);
|
|
unsigned getBit();
|
|
|
|
private:
|
|
uint8_t const* data_;
|
|
size_t len_, pos_;
|
|
uint8_t buf_;
|
|
unsigned bufValid_;
|
|
};
|
|
#endif
|