This commit is contained in:
2025-11-24 14:19:51 +05:30
commit f5c1412b28
6734 changed files with 1527575 additions and 0 deletions

View File

@ -0,0 +1,214 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
// Not currently being maintained.
// FIXME: Big-endian code is untested
// FIXME: Not suitable for storing large buffers > 500 MB in size
#ifndef CAT_BIT_STREAM_HPP
#define CAT_BIT_STREAM_HPP
#include <cat/Platform.hpp>
namespace cat {
//// BitStream manipulators ////
namespace bs_bit
{
template<u32 N_BITS> struct set
{
u32 bits;
set(u32 c_bits) : bits(c_bits) { }
};
template<u32 N_BITS> struct get
{
u32 &ref;
get(u32 &c_ref) : ref(c_ref) { }
};
}
namespace bs_byte
{
struct set
{
u32 bytes;
const void *ref;
set(u32 b, const void *r) : ref(r), bytes(b) { }
};
struct get
{
u32 bytes;
void *ref;
get(u32 b, void *r) : ref(r), bytes(b) { }
};
}
//// BitStream ////
class BitStream
{
bool fixed_buffer;
u8 *buffer;
u32 buffer_bytes;
bool read_underrun;
// grow to be able to write a number of bits
void grow(u32 bits);
public:
u32 read_offset, write_offset; // in bits
public:
BitStream(u32 bytes = 0, void *vbuffer = 0);
~BitStream();
// free unused buffer space
void shrink();
public:
u8 *get() { return buffer; }
bool aligned() { return read_offset % 8 == 0; }
u8 *peek() { return buffer + read_offset / 8; }
public:
// returns true iff the buffer is valid
bool valid() { return buffer != 0; }
// returns count of remaining readable bits
int unread() { return write_offset - read_offset; }
// returns true if a recent read operation would have overrun the buffer
bool underrun();
// skip ahead a number of bits
void skip(u32 bits);
public:
// insertion
void write1(u8 data); // data MUST be 1 or 0
template<class T> void write(T data)
{
grow(sizeof(T) * 8);
u32 byte_offset = write_offset / 8;
u32 shift = write_offset % 8;
if (shift)
{
buffer[byte_offset] |= (u8)(data << shift);
data = data >> (8 - shift);
++byte_offset;
}
*(T*)(buffer + byte_offset) = data;
write_offset += sizeof(T) * 8;
}
template<> void write(s8 data) { write((u8)data); }
template<> void write(s16 data) { write((u16)data); }
template<> void write(s32 data) { write((u32)data); }
template<> void write(s64 data) { write((u64)data); }
template<> void write(float data) { write(*(u32*)&data); }
template<> void write(double data) { write(*(u64*)&data); }
void writeBits(u32 data, int count);
void writeBytes(const void *data, u32 byte_count);
public:
// stream-mode insertion
BitStream &operator<<(const char *data) { writeBytes(data, (u32)strlen(data)); return *this; }
template<class T> BitStream &operator<<(T data) { write(data); return *this; }
template<u32 N_BITS> BitStream &operator<<(const bs_bit::set<N_BITS> &n) { writeBits(n.bits, N_BITS); return *this; }
template<> BitStream &operator<<(const bs_bit::set<1> &n) { write1((u8)n.bits); return *this; }
template<> BitStream &operator<<(const bs_bit::set<8> &n) { write((u8)n.bits); return *this; }
template<> BitStream &operator<<(const bs_bit::set<16> &n) { write((u16)n.bits); return *this; }
template<> BitStream &operator<<(const bs_bit::set<32> &n) { write((u32)n.bits); return *this; }
BitStream &operator<<(const bs_byte::set &n) { writeBytes(n.ref, n.bytes); return *this; }
public:
// extraction
u8 read1();
template<class T> void read(T &data)
{
const u32 bits = sizeof(T) * 8;
if (read_offset + bits > write_offset)
{
read_underrun = true;
return;
}
u32 byte_offset = read_offset / 8;
u32 shift = read_offset % 8;
if (shift)
data = (*(T*)(buffer + byte_offset + 1) << (8 - shift)) | (buffer[byte_offset] >> shift);
else
data = *(T*)(buffer + byte_offset);
read_offset += bits;
}
template<> void read(float &data) { read((u32&)data); }
template<> void read(double &data) { write(*(u64*)&data); }
template<class T> T read() { T temp; read(temp); return temp; }
u32 readBits(u32 count);
void readBytes(void *data, u32 byte_count);
public:
// stream-mode extraction
template<class T> BitStream &operator>>(T &data) { read(data); return *this; }
template<u32 N_BITS> BitStream &operator>>(const bs_bit::get<N_BITS> &n) { n.ref = readBits(N_BITS); return *this; }
template<> BitStream &operator>>(const bs_bit::get<1> &n) { n.ref = read1(); return *this; }
template<> BitStream &operator>>(const bs_bit::get<8> &n) { n.ref = read<u8>(); return *this; }
template<> BitStream &operator>>(const bs_bit::get<16> &n) { n.ref = read<u16>(); return *this; }
template<> BitStream &operator>>(const bs_bit::get<32> &n) { read(n.ref); return *this; }
BitStream &operator>>(const bs_byte::get &n) { readBytes(n.ref, n.bytes); return *this; }
};
} // namespace cat
#endif // CAT_BIT_STREAM_HPP

View File

@ -0,0 +1,114 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CAT_BUFFER_STREAM_HPP
#define CAT_BUFFER_STREAM_HPP
#include <cat/Platform.hpp>
#include <cat/port/EndianNeutral.hpp>
namespace cat {
/*
BufferStream
Extremely light-weight wrapper for a buffer data insertion and extraction.
Performs automatic endian conversion into and out of the buffer (sometimes).
Performs no checking for buffer overruns.
This class does not add any security to your code; it just makes it shorter.
Use at your peril.
*/
class BufferStream
{
protected:
u8 *_buffer;
public:
CAT_INLINE BufferStream(u8 *buffer) { _buffer = buffer; }
CAT_INLINE BufferStream &operator=(u8 *buffer) { _buffer = buffer; }
CAT_INLINE u32 GetOffset(void *buffer) { return (u32)(_buffer - reinterpret_cast<u8*>( buffer )); }
public:
// Auto-cast to u8* or char*
CAT_INLINE operator u8*() { return _buffer; }
CAT_INLINE char *c_str() { return reinterpret_cast<char*>( _buffer ); }
CAT_INLINE BufferStream &operator++() { _buffer++; return *this; }
CAT_INLINE BufferStream &operator+=(int skip_bytes) { _buffer += skip_bytes; return *this; }
public:
// Insertion
CAT_INLINE BufferStream &operator<<(s8 data) { *_buffer++ = (u8)data; return *this; }
CAT_INLINE BufferStream &operator<<(s16 data) { *(u16*)_buffer = getLE16((u16)data); _buffer += 2; return *this; }
CAT_INLINE BufferStream &operator<<(s32 data) { *(u32*)_buffer = getLE32((u32)data); _buffer += 4; return *this; }
CAT_INLINE BufferStream &operator<<(s64 data) { *(u64*)_buffer = getLE64((u64)data); _buffer += 8; return *this; }
CAT_INLINE BufferStream &operator<<(u8 data) { *_buffer++ = data; return *this; }
CAT_INLINE BufferStream &operator<<(u16 data) { *(u16*)_buffer = getLE16(data); _buffer += 2; return *this; }
CAT_INLINE BufferStream &operator<<(u32 data) { *(u32*)_buffer = getLE32(data); _buffer += 4; return *this; }
CAT_INLINE BufferStream &operator<<(u64 data) { *(u64*)_buffer = getLE64(data); _buffer += 8; return *this; }
CAT_INLINE void write(const void *data, u32 bytes)
{
memcpy(_buffer, data, bytes);
_buffer += bytes;
}
template<class T>
CAT_INLINE BufferStream &operator<<(const T &data) { write(&data, sizeof(T)); return *this; }
public:
// Extraction
CAT_INLINE BufferStream &operator>>(s8 &data) { data = (s8)*_buffer++; return *this; }
CAT_INLINE BufferStream &operator>>(s16 &data) { data = (s16)getLE16(*(u16*)_buffer); _buffer += 2; return *this; }
CAT_INLINE BufferStream &operator>>(s32 &data) { data = (s32)getLE32(*(u32*)_buffer); _buffer += 4; return *this; }
CAT_INLINE BufferStream &operator>>(s64 &data) { data = (s64)getLE64(*(u64*)_buffer); _buffer += 8; return *this; }
CAT_INLINE BufferStream &operator>>(u8 &data) { data = *_buffer++; return *this; }
CAT_INLINE BufferStream &operator>>(u16 &data) { data = getLE16(*(u16*)_buffer); _buffer += 2; return *this; }
CAT_INLINE BufferStream &operator>>(u32 &data) { data = getLE32(*(u32*)_buffer); _buffer += 4; return *this; }
CAT_INLINE BufferStream &operator>>(u64 &data) { data = getLE64(*(u64*)_buffer); _buffer += 8; return *this; }
CAT_INLINE void read(void *data, u32 bytes)
{
memcpy(data, _buffer, bytes);
_buffer += bytes;
}
template<class T>
CAT_INLINE BufferStream &operator>>(T &data) { read(&data, sizeof(T)); return *this; }
};
} // namespace cat
#endif // CAT_BUFFER_STREAM_HPP

View File

@ -0,0 +1,105 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CAT_BUFFER_TOK_HPP
#define CAT_BUFFER_TOK_HPP
#include <cat/Platform.hpp>
namespace cat {
/*
MyBuf = " 123 5 : 6 "
BufferTok bt(MyBuf, 256);
int a, b, c;
bt() >> a; // empty delimiter means white space and newlines
bt(':') >> b >> c;
if (!bt) // out of buffer space
*/
// Tokenize a buffer
class BufferTok
{
const char *buffer;
int len;
char delimiter;
bool newline;
u32 readNext(char *token, u32 tokenBufferSize);
public:
BufferTok(const char *buffer, int len);
// Returns true when the buffer is empty
inline bool operator!() { return len <= 0; }
// Returns true when the buffer extraction is stuck on a new line
// Use operator[] to reset this flag
inline bool onNewline() { return newline; }
/*
* operator(char)
* Set the delimiter character
*
* This character is used to find the end of the current token
* Newlines are always a delimiter
*/
BufferTok &operator()(char ch = 0);
/*
* Same as operator(), see above
*
* Also resets the newline() flag, so that data can start being
* read from the next line. If newline() wasn't set, it searches
* for the next line to start reading.
*/
BufferTok &operator[](char ch);
/*
* operator>>
* Extract a token
*
* Strips whitespace and the end-of-token delimiter
* Newlines are always a delimiter
*
* After the tokenizer encounters a newline, it will continue
* returning no results with newline() flag set, until the
* delimiter is reset with [], so incomplete lines don't wrap.
*/
BufferTok &operator>>(int &n);
BufferTok &operator>>(char *n); // buffer must have at least 256 characters
};
} // namespace cat
#endif // CAT_BUFFER_TOK_HPP

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,54 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CAT_MESSAGE_ROUTER_HPP
#define CAT_MESSAGE_ROUTER_HPP
#include <cat/parse/BitStream.hpp>
#include <cat/port/FastDelegate.h>
namespace cat {
typedef fastdelegate::FastDelegate<void (BitStream &m)> MessageHandler;
class MessageRouter
{
MessageHandler handlers[256];
public:
void Set(u8 opcode, const MessageHandler &handler);
void Clear(u8 opcode);
void Invoke(u8 opcode, BitStream &msg);
};
} // namespace cat
#endif // CAT_MESSAGE_ROUTER_HPP