Files
tp/include/JSystem/JGadget/binary.h
T

192 lines
5.5 KiB
C++
Raw Normal View History

2021-03-28 22:49:05 +02:00
#ifndef BINARY_H
#define BINARY_H
2021-01-05 09:47:26 -08:00
#include "dolphin/types.h"
#include "JSystem/JGadget/search.h"
2021-01-05 09:47:26 -08:00
2021-11-26 06:50:22 -08:00
namespace JGadget {
namespace binary {
struct TEBit {
u32 value;
};
const void* parseVariableUInt_16_32_following(const void* pu16, u32* pu32First, u32* pu32Second,
TEBit* tebit);
inline u32 align_roundUp(u32 arg0, u32 uAlign) {
return (arg0 + uAlign - 1) & ~(uAlign - 1);
}
struct TParseData {
TParseData(const void* pContent) : raw(pContent) {}
const void* getRaw() const { return raw; }
void setRaw(const void* p) { raw = p; }
/* 0x0 */ const void* raw;
2021-11-26 06:50:22 -08:00
};
template <int S>
2021-11-26 06:50:22 -08:00
struct TParseData_aligned : public TParseData {
TParseData_aligned(const void* pContent) : TParseData(pContent) {}
void setRaw(const void* p) {
/* if ((u32)p % S != 0) {
2021-11-26 06:50:22 -08:00
JUTWarn w;
w << "misaligned : " << (u32)p;
2023-02-19 09:40:57 -08:00
} */
2021-11-26 06:50:22 -08:00
static_cast<TParseData*>(this)->setRaw(p);
}
};
// Base for header and/or block parsing
struct TParse_header_block {
2025-04-09 16:45:30 -04:00
virtual ~TParse_header_block() = 0;
2021-11-26 06:50:22 -08:00
virtual bool parseHeader_next(const void** ppData_inout, u32* puBlock_out, u32 arg2) = 0;
virtual bool parseBlock_next(const void** ppData_inout, u32* puData_out, u32 arg2) = 0;
bool parse_next(const void** ppData_inout, u32 a2);
bool parse(const void* ppData_inout, u32 a2) {
return parse_next(&ppData_inout, a2);
}
2023-09-27 07:45:37 +03:00
bool checkNext(const void** ptrLocation, u32* headerEnd, u32 idx) {
bool checkNext = false;
if (parseHeader_next(ptrLocation, headerEnd, idx)) {
checkNext = true;
}
return checkNext;
}
2021-11-26 06:50:22 -08:00
};
2023-02-19 09:40:57 -08:00
template <typename T>
struct TParseValue_raw_ {
2024-10-18 00:21:08 +03:00
typedef T ParseType;
static T parse(const void* data) { return (T)*(T*)data; }
};
template <typename T>
struct TParseValue_raw : public TParseValue_raw_<T> {
typedef TParseValue_raw_<T> InnerParseValueClass;
2023-02-19 09:40:57 -08:00
};
template <typename T>
struct TParseValue_endian_big_ : public TParseValue_raw_<T> {
static T parse(const void* data) { return TParseValue_raw_<T>::parse(data); }
2023-02-19 09:40:57 -08:00
};
template <class Parser>
struct TParseValue : public Parser {
static typename Parser::ParseType parse(const void* data) { return Parser::parse(data); }
2023-02-19 09:40:57 -08:00
static typename Parser::ParseType parse(const void* data, s32 advanceNum) {
return Parser::parse(advance(data, advanceNum));
2023-02-19 09:40:57 -08:00
}
static const void* advance(const void* data, s32 advanceNum) {
return (char*)data + (advanceNum * sizeof(Parser::ParseType));
2023-02-19 09:40:57 -08:00
}
};
2024-10-18 00:21:08 +03:00
template<class Parser, int size>
struct TValueIterator
: public JGadget::TIterator<
std::random_access_iterator_tag,
typename Parser::ParseType,
ptrdiff_t,
typename Parser::ParseType*,
typename Parser::ParseType&
>
{
typedef typename Parser::ParseType ValueType;
2024-10-18 00:21:08 +03:00
TValueIterator(const void* begin) {
mBegin = reinterpret_cast<const char*>(begin);
2024-10-18 00:21:08 +03:00
}
2024-11-10 02:18:28 -08:00
const void* get() const { return mBegin; }
typename Parser::ParseType operator*() const {
return TParseValue<typename Parser::InnerParseValueClass>::parse(get());
2024-11-10 02:18:28 -08:00
}
TValueIterator& operator++() {
mBegin += size;
2024-11-10 02:18:28 -08:00
return *this;
}
const TValueIterator operator++(int) {
TValueIterator old(*this);
++(*this);
return old;
}
TValueIterator& operator+=(s32 n) {
mBegin += size * n;
2024-11-10 02:18:28 -08:00
return *this;
2024-10-18 00:21:08 +03:00
}
TValueIterator& operator--() {
mBegin -= size;
return *this;
}
char const* mBegin;
2024-10-18 00:21:08 +03:00
};
template<typename T>
struct TValueIterator_raw : public TValueIterator<TParseValue_raw<T>, sizeof(T)> {
TValueIterator_raw(const void* begin) : TValueIterator<TParseValue_raw<T>, sizeof(T)>(begin) {}
friend bool operator==(TValueIterator<TParseValue_raw<T>, sizeof(T)> a, TValueIterator<TParseValue_raw<T>, sizeof(T)> b) {
return a.mBegin == b.mBegin;
}
friend bool operator!=(TValueIterator<TParseValue_raw<T>, sizeof(T)> a, TValueIterator<TParseValue_raw<T>, sizeof(T)> b) {
return !operator==(a, b);
}
friend TValueIterator<TParseValue_raw<T>, sizeof(T)> operator+(TValueIterator<TParseValue_raw<T>, sizeof(T)> a, s32 b) {
TValueIterator<TParseValue_raw<T>, sizeof(T)> it = a;
it += b;
return it;
}
2024-10-18 00:21:08 +03:00
};
2024-11-10 02:18:28 -08:00
template <typename T>
struct TParseValue_misaligned_ : public TParseValue_raw_<T> {
typedef T ParseType;
static T parse(const void* data) { return TParseValue_raw_<T>::parse(data); }
};
template <typename T>
struct TParseValue_misaligned : public TParseValue_raw_<T> {
typedef TParseValue_misaligned_<T> InnerParseValueClass;
2024-11-10 02:18:28 -08:00
};
template<typename T>
struct TValueIterator_misaligned : public TValueIterator<TParseValue_misaligned<T>, sizeof(T)> {
TValueIterator_misaligned(const void* begin) : TValueIterator<TParseValue_misaligned<T>, sizeof(T)>(begin) {}
friend bool operator==(TValueIterator<TParseValue_misaligned<T>, sizeof(T)> a, TValueIterator<TParseValue_misaligned<T>, sizeof(T)> b) {
return a.mBegin == b.mBegin;
}
friend bool operator!=(TValueIterator<TParseValue_misaligned<T>, sizeof(T)> a, TValueIterator<TParseValue_misaligned<T>, sizeof(T)> b) {
return !operator==(a, b);
}
friend TValueIterator<TParseValue_misaligned<T>, sizeof(T)> operator+(TValueIterator<TParseValue_misaligned<T>, sizeof(T)> a, s32 b) {
TValueIterator<TParseValue_misaligned<T>, sizeof(T)> it(a);
it += b;
return it;
}
2024-11-10 02:18:28 -08:00
};
2021-11-26 06:50:22 -08:00
} // namespace binary
} // namespace JGadget
2021-03-28 22:49:05 +02:00
#endif /* BINARY_H */