Refactor library serialization to dedicated namespace
This commit is contained in:
parent
1bcbae1163
commit
327c3197c4
31 changed files with 443 additions and 369 deletions
|
@ -105,27 +105,17 @@ struct byteseq_tag
|
|||
std::vector<uint8_t> st;
|
||||
};
|
||||
|
||||
template<typename T> struct unsigned_of {};
|
||||
template<> struct unsigned_of<int8_t> { typedef uint8_t t; };
|
||||
template<> struct unsigned_of<uint8_t> { typedef uint8_t t; };
|
||||
template<> struct unsigned_of<int16_t> { typedef uint16_t t; };
|
||||
template<> struct unsigned_of<uint16_t> { typedef uint16_t t; };
|
||||
template<> struct unsigned_of<int32_t> { typedef uint32_t t; };
|
||||
template<> struct unsigned_of<uint32_t> { typedef uint32_t t; };
|
||||
template<> struct unsigned_of<int64_t> { typedef uint64_t t; };
|
||||
template<> struct unsigned_of<uint64_t> { typedef uint64_t t; };
|
||||
|
||||
template<typename T> byteseq_tag vle(T v)
|
||||
{
|
||||
uint8_t b[sizeof(T)];
|
||||
_write_common<T,typename unsigned_of<T>::t, sizeof(T), false>(b, v);
|
||||
serialization::write_common<T, false>(b, v);
|
||||
return byteseq_tag(b, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T> byteseq_tag vbe(T v)
|
||||
{
|
||||
uint8_t b[sizeof(T)];
|
||||
_write_common<T,typename unsigned_of<T>::t, sizeof(T), true>(b, v);
|
||||
serialization::write_common<T, true>(b, v);
|
||||
return byteseq_tag(b, sizeof(T));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,63 +1,172 @@
|
|||
#ifndef _library__serialization__hpp__included__
|
||||
#define _library__serialization__hpp__included__
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
|
||||
template<typename T1, typename T2, size_t ssize, bool be>
|
||||
void _write_common(unsigned char* target, T1 value)
|
||||
namespace serialization
|
||||
{
|
||||
for(size_t i = 0; i < ssize; i++)
|
||||
template<typename T> struct unsigned_of {};
|
||||
template<> struct unsigned_of<int8_t> { typedef uint8_t t; };
|
||||
template<> struct unsigned_of<uint8_t> { typedef uint8_t t; };
|
||||
template<> struct unsigned_of<int16_t> { typedef uint16_t t; };
|
||||
template<> struct unsigned_of<uint16_t> { typedef uint16_t t; };
|
||||
template<> struct unsigned_of<int32_t> { typedef uint32_t t; };
|
||||
template<> struct unsigned_of<uint32_t> { typedef uint32_t t; };
|
||||
template<> struct unsigned_of<int64_t> { typedef uint64_t t; };
|
||||
template<> struct unsigned_of<uint64_t> { typedef uint64_t t; };
|
||||
|
||||
template<typename T1, bool be>
|
||||
void write_common(uint8_t* target, T1 value)
|
||||
{
|
||||
for(size_t i = 0; i < sizeof(T1); i++)
|
||||
if(be)
|
||||
target[i] = static_cast<T2>(value) >> 8 * (ssize - i - 1);
|
||||
target[i] = static_cast<typename unsigned_of<T1>::t>(value) >> 8 * (sizeof(T1) - i - 1);
|
||||
else
|
||||
target[i] = static_cast<T2>(value) >> 8 * i;
|
||||
target[i] = static_cast<typename unsigned_of<T1>::t>(value) >> 8 * i;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, size_t ssize, bool be>
|
||||
T1 _read_common(const unsigned char* source)
|
||||
template<typename T1, bool be>
|
||||
T1 read_common(const uint8_t* source)
|
||||
{
|
||||
T2 value = 0;
|
||||
for(size_t i = 0; i < ssize; i++)
|
||||
typename unsigned_of<T1>::t value = 0;
|
||||
for(size_t i = 0; i < sizeof(T1); i++)
|
||||
if(be)
|
||||
value |= static_cast<T2>(source[i]) << 8 * (ssize - i - 1);
|
||||
value |= static_cast<typename unsigned_of<T1>::t>(source[i]) << 8 * (sizeof(T1) - i - 1);
|
||||
else
|
||||
value |= static_cast<T2>(source[i]) << 8 * i;
|
||||
value |= static_cast<typename unsigned_of<T1>::t>(source[i]) << 8 * i;
|
||||
return static_cast<T1>(value);
|
||||
}
|
||||
|
||||
#define write8sbe(t, v) _write_common< int8_t, uint8_t, 1, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write8sle(t, v) _write_common< int8_t, uint8_t, 1, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write8ube(t, v) _write_common< uint8_t, uint8_t, 1, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write8ule(t, v) _write_common< uint8_t, uint8_t, 1, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write16sbe(t, v) _write_common< int16_t, uint16_t, 2, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write16sle(t, v) _write_common< int16_t, uint16_t, 2, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write16ube(t, v) _write_common<uint16_t, uint16_t, 2, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write16ule(t, v) _write_common<uint16_t, uint16_t, 2, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write32sbe(t, v) _write_common< int32_t, uint32_t, 4, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write32sle(t, v) _write_common< int32_t, uint32_t, 4, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write32ube(t, v) _write_common<uint32_t, uint32_t, 4, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write32ule(t, v) _write_common<uint32_t, uint32_t, 4, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write64sbe(t, v) _write_common< int64_t, uint64_t, 8, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write64sle(t, v) _write_common< int64_t, uint64_t, 8, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write64ube(t, v) _write_common<uint64_t, uint64_t, 8, true>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define write64ule(t, v) _write_common<uint64_t, uint64_t, 8, false>(reinterpret_cast<unsigned char*>(t), (v))
|
||||
#define read8sbe(t) _read_common< int8_t, uint8_t, 1, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read8sle(t) _read_common< int8_t, uint8_t, 1, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read8ube(t) _read_common< uint8_t, uint8_t, 1, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read8ule(t) _read_common< uint8_t, uint8_t, 1, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read16sbe(t) _read_common< int16_t, uint16_t, 2, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read16sle(t) _read_common< int16_t, uint16_t, 2, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read16ube(t) _read_common<uint16_t, uint16_t, 2, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read16ule(t) _read_common<uint16_t, uint16_t, 2, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read32sbe(t) _read_common< int32_t, uint32_t, 4, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read32sle(t) _read_common< int32_t, uint32_t, 4, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read32ube(t) _read_common<uint32_t, uint32_t, 4, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read32ule(t) _read_common<uint32_t, uint32_t, 4, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read64sbe(t) _read_common< int64_t, uint64_t, 8, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read64sle(t) _read_common< int64_t, uint64_t, 8, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read64ube(t) _read_common<uint64_t, uint64_t, 8, true>(reinterpret_cast<const unsigned char*>(t))
|
||||
#define read64ule(t) _read_common<uint64_t, uint64_t, 8, false>(reinterpret_cast<const unsigned char*>(t))
|
||||
inline void s8b(void* t, int8_t v)
|
||||
{
|
||||
write_common< int8_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s8l(void* t, int8_t v)
|
||||
{
|
||||
write_common< int8_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u8b(void* t, uint8_t v)
|
||||
{
|
||||
write_common< uint8_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u8l(void* t, uint8_t v)
|
||||
{
|
||||
write_common< uint8_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s16b(void* t, int16_t v)
|
||||
{
|
||||
write_common< int16_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s16l(void* t, int16_t v)
|
||||
{
|
||||
write_common< int16_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u16b(void* t, uint16_t v)
|
||||
{
|
||||
write_common<uint16_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u16l(void* t, uint16_t v)
|
||||
{
|
||||
write_common<uint16_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s32b(void* t, int32_t v)
|
||||
{
|
||||
write_common< int32_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s32l(void* t, int32_t v)
|
||||
{
|
||||
write_common< int32_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u32b(void* t, uint32_t v)
|
||||
{
|
||||
write_common<uint32_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u32l(void* t, uint32_t v)
|
||||
{
|
||||
write_common<uint32_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s64b(void* t, int64_t v)
|
||||
{
|
||||
write_common< int64_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void s64l(void* t, int64_t v)
|
||||
{
|
||||
write_common< int64_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u64b(void* t, uint64_t v)
|
||||
{
|
||||
write_common<uint64_t, true>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline void u64l(void* t, uint64_t v)
|
||||
{
|
||||
write_common<uint64_t, false>(reinterpret_cast<uint8_t*>(t), v);
|
||||
}
|
||||
inline int8_t s8b(const void* t)
|
||||
{
|
||||
return read_common< int8_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int8_t s8l(const void* t)
|
||||
{
|
||||
return read_common< int8_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint8_t u8b(const void* t)
|
||||
{
|
||||
return read_common< uint8_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint8_t u8l(const void* t)
|
||||
{
|
||||
return read_common< uint8_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int16_t s16b(const void* t)
|
||||
{
|
||||
return read_common< int16_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int16_t s16l(const void* t)
|
||||
{
|
||||
return read_common< int16_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint16_t u16b(const void* t)
|
||||
{
|
||||
return read_common<uint16_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint16_t u16l(const void* t)
|
||||
{
|
||||
return read_common<uint16_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int32_t s32b(const void* t)
|
||||
{
|
||||
return read_common< int32_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int32_t s32l(const void* t)
|
||||
{
|
||||
return read_common< int32_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint32_t u32b(const void* t)
|
||||
{
|
||||
return read_common<uint32_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint32_t u32l(const void* t)
|
||||
{
|
||||
return read_common<uint32_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int64_t s64b(const void* t)
|
||||
{
|
||||
return read_common< int64_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline int64_t s64l(const void* t)
|
||||
{
|
||||
return read_common< int64_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint64_t u64b(const void* t)
|
||||
{
|
||||
return read_common<uint64_t, true>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
inline uint64_t u64l(const void* t)
|
||||
{
|
||||
return read_common<uint64_t, false>(reinterpret_cast<const uint8_t*>(t));
|
||||
}
|
||||
|
||||
template<typename T> void swap_endian(T& value)
|
||||
{
|
||||
|
@ -75,7 +184,7 @@ template<typename T> void swap_endian(T& value, int endian)
|
|||
swap_endian(value);
|
||||
}
|
||||
|
||||
template<typename T> T read_of_endian(const void* value, int endian)
|
||||
template<typename T> T read_endian(const void* value, int endian)
|
||||
{
|
||||
T val;
|
||||
memcpy(&val, value, sizeof(T));
|
||||
|
@ -83,11 +192,12 @@ template<typename T> T read_of_endian(const void* value, int endian)
|
|||
return val;
|
||||
}
|
||||
|
||||
template<typename T> void write_of_endian(void* value, const T& val, int endian)
|
||||
template<typename T> void write_endian(void* value, const T& val, int endian)
|
||||
{
|
||||
T val2;
|
||||
memcpy(value, &val, sizeof(T));
|
||||
swap_endian(*reinterpret_cast<T*>(value), endian);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -385,17 +385,17 @@ namespace
|
|||
//This is a trailer entry.
|
||||
if(buf[i + 3] == 2) {
|
||||
//Pregap.
|
||||
pregap_length = read32ube(buf + i) >> 8;
|
||||
pregap_length = serialization::u32b(buf + i) >> 8;
|
||||
} else if(buf[i + 3] == 3) {
|
||||
//Postgap.
|
||||
postgap_length = read32ube(buf + i) >> 8;
|
||||
postgap_length = serialization::u32b(buf + i) >> 8;
|
||||
} else if(buf[i + 3] == 4) {
|
||||
//Gain.
|
||||
gain = read16sbe(buf + i);
|
||||
gain = serialization::s16b(buf + i);
|
||||
}
|
||||
} else {
|
||||
uint16_t psize = read16ube(buf + i);
|
||||
uint8_t plen = read8ube(buf + i + 2);
|
||||
uint16_t psize = serialization::u16b(buf + i);
|
||||
uint8_t plen = serialization::u8b(buf + i + 2);
|
||||
total_size += psize;
|
||||
total_len += 120 * plen;
|
||||
opus_packetinfo p(psize, plen, 1ULL * next_cluster * CLUSTER_SIZE +
|
||||
|
@ -536,17 +536,17 @@ out:
|
|||
data.read(header, 32);
|
||||
if(!data)
|
||||
throw std::runtime_error("Can't read .sox header");
|
||||
if(read32ule(header + 0) != 0x586F532EULL)
|
||||
if(serialization::u32l(header + 0) != 0x586F532EULL)
|
||||
throw std::runtime_error("Bad .sox header magic");
|
||||
if(read8ube(header + 4) > 28)
|
||||
data.read(header + 32, read8ube(header + 4) - 28);
|
||||
if(serialization::u8b(header + 4) > 28)
|
||||
data.read(header + 32, serialization::u8b(header + 4) - 28);
|
||||
if(!data)
|
||||
throw std::runtime_error("Can't read .sox header");
|
||||
if(read64ule(header + 16) != 4676829883349860352ULL)
|
||||
if(serialization::u64l(header + 16) != 4676829883349860352ULL)
|
||||
throw std::runtime_error("Bad .sox sampling rate");
|
||||
if(read32ule(header + 24) != 1)
|
||||
if(serialization::u32l(header + 24) != 1)
|
||||
throw std::runtime_error("Only mono streams are supported");
|
||||
uint64_t samples = read64ule(header + 8);
|
||||
uint64_t samples = serialization::u64l(header + 8);
|
||||
opus::encoder enc(opus::samplerate::r48k, false, opus::application::voice);
|
||||
enc.ctl(opus::bitrate(opus_bitrate.get()));
|
||||
int32_t pregap = enc.ctl(opus::lookahead);
|
||||
|
@ -569,7 +569,7 @@ out:
|
|||
throw std::runtime_error("Can't read .sox data");
|
||||
}
|
||||
for(size_t j = 0; j < bs; j++)
|
||||
tmp[j] = static_cast<float>(read32sle(tmpi + 4 * j)) / 268435456;
|
||||
tmp[j] = static_cast<float>(serialization::s32l(tmpi + 4 * j)) / 268435456;
|
||||
if(bs < OPUS_BLOCK_SIZE)
|
||||
postgap_length = OPUS_BLOCK_SIZE - bs;
|
||||
for(size_t j = bs; j < OPUS_BLOCK_SIZE; j++)
|
||||
|
@ -681,9 +681,9 @@ out:
|
|||
std::vector<unsigned char> p;
|
||||
float tmp[OPUS_MAX_OUT];
|
||||
char header[32];
|
||||
write64ule(header, 0x1C586F532EULL); //Magic and header size.
|
||||
write64ule(header + 16, 4676829883349860352ULL); //Sampling rate.
|
||||
write32ule(header + 24, 1);
|
||||
serialization::u64l(header, 0x1C586F532EULL); //Magic and header size.
|
||||
serialization::u64l(header + 16, 4676829883349860352ULL); //Sampling rate.
|
||||
serialization::u32l(header + 24, 1);
|
||||
uint64_t tlen = 0;
|
||||
uint32_t lookahead_thrown = 0;
|
||||
data.write(header, 32);
|
||||
|
@ -710,7 +710,7 @@ out:
|
|||
tlen += (len - pregap_throw - postgap_throw);
|
||||
for(uint32_t j = pregap_throw; j < len - postgap_throw; j++) {
|
||||
int32_t s = (int32_t)(tmp[j] * lgain * 268435456.0);
|
||||
write32sle(blank, s);
|
||||
serialization::s32l(blank, s);
|
||||
data.write(blank, 4);
|
||||
if(!data)
|
||||
throw std::runtime_error("Error writing PCM data.");
|
||||
|
@ -720,7 +720,7 @@ out:
|
|||
}
|
||||
}
|
||||
data.seekp(0, std::ios_base::beg);
|
||||
write64ule(header + 8, tlen);
|
||||
serialization::u64l(header + 8, tlen);
|
||||
data.write(header, 32);
|
||||
if(!data) {
|
||||
throw std::runtime_error("Error writing PCM data.");
|
||||
|
@ -745,9 +745,9 @@ out:
|
|||
next_cluster = data_cluster = fs.allocate_cluster();
|
||||
if(!next_mcluster)
|
||||
next_mcluster = ctrl_cluster = fs.allocate_cluster();
|
||||
write16ube(descriptor, payload_len);
|
||||
write8ube(descriptor + 2, len);
|
||||
write8ube(descriptor + 3, 1);
|
||||
serialization::u16b(descriptor, payload_len);
|
||||
serialization::u8b(descriptor + 2, len);
|
||||
serialization::u8b(descriptor + 3, 1);
|
||||
fs.write_data(next_cluster, next_offset, payload, payload_len, used_cluster, used_offset);
|
||||
fs.write_data(next_mcluster, next_moffset, descriptor, 4, used_mcluster, used_moffset);
|
||||
uint64_t off = static_cast<uint64_t>(used_cluster) * CLUSTER_SIZE + used_offset;
|
||||
|
@ -770,11 +770,11 @@ out:
|
|||
//But the write must not update the pointers..
|
||||
uint32_t tmp_mcluster = next_mcluster;
|
||||
uint32_t tmp_moffset = next_moffset;
|
||||
write32ube(descriptor, 0);
|
||||
write32ube(descriptor + 4, (pregap_length << 8) | 0x02);
|
||||
write32ube(descriptor + 8, (postgap_length << 8) | 0x03);
|
||||
write16sbe(descriptor + 12, gain);
|
||||
write16ube(descriptor + 14, 0x0004);
|
||||
serialization::u32b(descriptor, 0);
|
||||
serialization::u32b(descriptor + 4, (pregap_length << 8) | 0x02);
|
||||
serialization::u32b(descriptor + 8, (postgap_length << 8) | 0x03);
|
||||
serialization::s16b(descriptor + 12, gain);
|
||||
serialization::u16b(descriptor + 14, 0x0004);
|
||||
fs.write_data(tmp_mcluster, tmp_moffset, descriptor, 16, used_mcluster, used_moffset);
|
||||
} catch(std::exception& e) {
|
||||
(stringfmt() << "Can't write stream trailer: " << e.what()).throwex();
|
||||
|
@ -1017,9 +1017,9 @@ out:
|
|||
size_t r = fs.read_data(next_cluster, next_offset, buffer, 16);
|
||||
if(r < 16)
|
||||
break;
|
||||
uint64_t timebase = read64ube(buffer);
|
||||
uint32_t ctrl_cluster = read32ube(buffer + 8);
|
||||
uint32_t data_cluster = read32ube(buffer + 12);
|
||||
uint64_t timebase = serialization::u64b(buffer);
|
||||
uint32_t ctrl_cluster = serialization::u32b(buffer + 8);
|
||||
uint32_t data_cluster = serialization::u32b(buffer + 12);
|
||||
if(ctrl_cluster) {
|
||||
opus_stream* x = new opus_stream(timebase, fs, ctrl_cluster, data_cluster);
|
||||
entries[next_index] = i;
|
||||
|
@ -1069,10 +1069,10 @@ out:
|
|||
idx = next_index++;
|
||||
streams[idx] = &stream;
|
||||
char buffer[16];
|
||||
write64ube(buffer, stream.timebase());
|
||||
serialization::u64b(buffer, stream.timebase());
|
||||
auto r = stream.get_clusters();
|
||||
write32ube(buffer + 8, r.first);
|
||||
write32ube(buffer + 12, r.second);
|
||||
serialization::u32b(buffer + 8, r.first);
|
||||
serialization::u32b(buffer + 12, r.second);
|
||||
uint64_t entry_number = 0;
|
||||
if(free_indices.empty())
|
||||
entry_number = next_stream++;
|
||||
|
@ -1135,7 +1135,7 @@ out:
|
|||
uint32_t write_cluster = 2;
|
||||
uint32_t write_offset = 0;
|
||||
uint32_t dummy1, dummy2;
|
||||
write64ube(buffer, newts);
|
||||
serialization::u64b(buffer, newts);
|
||||
fs.skip_data(write_cluster, write_offset, 16 * entries[index]);
|
||||
fs.write_data(write_cluster, write_offset, buffer, 8, dummy1, dummy2);
|
||||
}
|
||||
|
@ -1188,10 +1188,10 @@ out:
|
|||
}
|
||||
}
|
||||
char header[32];
|
||||
write64ule(header, 0x1C586F532EULL); //Magic and header size.
|
||||
write64ule(header + 8, len);
|
||||
write64ule(header + 16, 4676829883349860352ULL); //Sampling rate.
|
||||
write64ule(header + 24, 1);
|
||||
serialization::u64l(header, 0x1C586F532EULL); //Magic and header size.
|
||||
serialization::u64l(header + 8, len);
|
||||
serialization::u64l(header + 16, 4676829883349860352ULL); //Sampling rate.
|
||||
serialization::u64l(header + 24, 1);
|
||||
out.write(header, 32);
|
||||
if(!out)
|
||||
throw std::runtime_error("Error writing PCM output");
|
||||
|
@ -1252,7 +1252,7 @@ out:
|
|||
t++;
|
||||
}
|
||||
for(size_t t = 0; t < maxsamples; t++)
|
||||
write32sle(outbuf + 4 * t, buf1[t] * 268435456);
|
||||
serialization::s32l(outbuf + 4 * t, buf1[t] * 268435456);
|
||||
out.write(outbuf, 4 * maxsamples);
|
||||
if(!out)
|
||||
throw std::runtime_error("Failed to write PCM");
|
||||
|
|
|
@ -92,8 +92,8 @@ namespace
|
|||
x.resize(rseed.length() + slots * 8 + 8);
|
||||
std::copy(rseed.begin(), rseed.end(), x.begin());
|
||||
for(unsigned i = 0; i < slots; i++)
|
||||
write64ule(&x[rseed.length() + 8 * i], buf[i]);
|
||||
write64ule(&x[rseed.length() + 8 * slots], arch_get_random());
|
||||
serialization::u64l(&x[rseed.length() + 8 * i], buf[i]);
|
||||
serialization::u64l(&x[rseed.length() + 8 * slots], arch_get_random());
|
||||
rseed = "32 " + sha256::hash(reinterpret_cast<uint8_t*>(&x[0]), x.size());
|
||||
count = 0;
|
||||
}
|
||||
|
@ -435,7 +435,7 @@ void highrandom_256(uint8_t* buf)
|
|||
std::string s = get_random_hexstring(0);
|
||||
std::copy(s.begin(), s.end(), reinterpret_cast<char*>(tmp));
|
||||
arch_random_256(tmp + 64);
|
||||
write64ube(tmp + 96, arch_get_tsc());
|
||||
serialization::u64b(tmp + 96, arch_get_tsc());
|
||||
sha256::hash(buf, tmp, 104);
|
||||
#ifdef USE_LIBGCRYPT_SHA256
|
||||
memset(tmp, 0, 32);
|
||||
|
|
|
@ -1079,7 +1079,7 @@ namespace
|
|||
void emerg_write_number32(int handle, uint32_t num)
|
||||
{
|
||||
char buf[4];
|
||||
write32ube(buf, num);
|
||||
serialization::u32b(buf, num);
|
||||
emerg_write_bytes(handle, (const uint8_t*)buf, 4);
|
||||
}
|
||||
void emerg_write_member(int handle, uint32_t tag, uint64_t size)
|
||||
|
|
|
@ -557,7 +557,7 @@ namespace
|
|||
size_t osize = out.size();
|
||||
out.resize(osize + 4 * sizeof(primary_framebuffer) / sizeof(primary_framebuffer[0]));
|
||||
for(size_t i = 0; i < sizeof(primary_framebuffer) / sizeof(primary_framebuffer[0]); i++)
|
||||
write32ube(&out[osize + 4 * i], primary_framebuffer[i]);
|
||||
serialization::u32b(&out[osize + 4 * i], primary_framebuffer[i]);
|
||||
out.push_back(frame_overflow >> 8);
|
||||
out.push_back(frame_overflow);
|
||||
}
|
||||
|
@ -571,7 +571,7 @@ namespace
|
|||
memcpy(&tmp[0], in, foffset);
|
||||
instance->loadState(tmp);
|
||||
for(size_t i = 0; i < sizeof(primary_framebuffer) / sizeof(primary_framebuffer[0]); i++)
|
||||
primary_framebuffer[i] = read32ube(&in[foffset + 4 * i]);
|
||||
primary_framebuffer[i] = serialization::u32b(&in[foffset + 4 * i]);
|
||||
|
||||
unsigned x1 = (unsigned char)in[insize - 2];
|
||||
unsigned x2 = (unsigned char)in[insize - 1];
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace sky
|
|||
void random::init()
|
||||
{
|
||||
memset(state, 0, 32);
|
||||
write64ule(state, ecore_callbacks->get_randomseed());
|
||||
serialization::u64l(state, ecore_callbacks->get_randomseed());
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ namespace sky
|
|||
uint8_t buf[4];
|
||||
sha256 h;
|
||||
h.write(state, 32);
|
||||
write32ule(buf, x);
|
||||
serialization::u32l(buf, x);
|
||||
h.write(buf, 4);
|
||||
h.read(state);
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ namespace sky
|
|||
{
|
||||
if(!initialized)
|
||||
init();
|
||||
uint32_t val = read32ule(state);
|
||||
uint32_t val = serialization::u32l(state);
|
||||
sha256 h;
|
||||
h.write(state, 32);
|
||||
h.read(state);
|
||||
|
|
|
@ -99,7 +99,7 @@ void i386_reloc_rel8(uint8_t* location, size_t target, size_t source)
|
|||
std::cerr << "Out-of-range offset: " << d << std::endl;
|
||||
throw std::runtime_error("Relative reference (8-bit) out of range");
|
||||
}
|
||||
write8sle(location, d);
|
||||
serialization::s8l(location, d);
|
||||
}
|
||||
|
||||
void i386_reloc_rel16(uint8_t* location, size_t target, size_t source)
|
||||
|
@ -109,7 +109,7 @@ void i386_reloc_rel16(uint8_t* location, size_t target, size_t source)
|
|||
std::cerr << "Out-of-range offset: " << d << std::endl;
|
||||
throw std::runtime_error("Relative reference (16-bit) out of range");
|
||||
}
|
||||
write32sle(location, d);
|
||||
serialization::s32l(location, d);
|
||||
}
|
||||
|
||||
void i386_reloc_rel32(uint8_t* location, size_t target, size_t source)
|
||||
|
@ -119,19 +119,19 @@ void i386_reloc_rel32(uint8_t* location, size_t target, size_t source)
|
|||
std::cerr << "Out-of-range offset: " << d << std::endl;
|
||||
throw std::runtime_error("Relative reference (32-bit) out of range");
|
||||
}
|
||||
write32sle(location, d);
|
||||
serialization::s32l(location, d);
|
||||
}
|
||||
|
||||
void i386_reloc_abs32(uint8_t* location, size_t target, size_t source)
|
||||
{
|
||||
if(target > 0xFFFFFFFFU)
|
||||
throw std::runtime_error("Absolute reference out of range");
|
||||
write32ule(location, target);
|
||||
serialization::u32l(location, target);
|
||||
}
|
||||
|
||||
void i386_reloc_abs64(uint8_t* location, size_t target, size_t source)
|
||||
{
|
||||
write64ule(location, target);
|
||||
serialization::u64l(location, target);
|
||||
}
|
||||
|
||||
uint8_t i386_modrm(uint8_t reg, uint8_t mod, uint8_t rm)
|
||||
|
|
|
@ -50,7 +50,7 @@ size_t output::numberbytes(uint64_t number)
|
|||
void output::number32(uint32_t number)
|
||||
{
|
||||
char data[4];
|
||||
write32ube(data, number);
|
||||
serialization::u32b(data, number);
|
||||
strm.write(data, 4);
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,7 @@ uint32_t input::number32()
|
|||
{
|
||||
char c[4];
|
||||
read(c, 4);
|
||||
return read32ube(c);
|
||||
return serialization::u32b(c);
|
||||
}
|
||||
|
||||
std::string input::string()
|
||||
|
@ -201,7 +201,7 @@ void input::extension(std::initializer_list<binary_tag_handler> funcs,
|
|||
char c[4];
|
||||
if(!read(c, 4, true))
|
||||
break;
|
||||
uint32_t tagid = read32ube(c);
|
||||
uint32_t tagid = serialization::u32b(c);
|
||||
if(tagid != TAG_)
|
||||
throw std::runtime_error("Binary file packet structure desync");
|
||||
uint32_t tag = number32();
|
||||
|
|
|
@ -58,8 +58,8 @@ namespace
|
|||
gettimeofday(&tv, NULL);
|
||||
unsigned char buffer[48];
|
||||
memcpy(buffer, state, 32);
|
||||
write64ube(buffer + 32, tv.tv_sec);
|
||||
write64ube(buffer + 40, tv.tv_usec);
|
||||
serialization::u64b(buffer + 32, tv.tv_sec);
|
||||
serialization::u64b(buffer + 40, tv.tv_usec);
|
||||
sha256::hash(state, buffer, 48);
|
||||
extracted = 0;
|
||||
}
|
||||
|
|
|
@ -266,7 +266,7 @@ void filesystem::supercluster::load(std::fstream& s, uint32_t index)
|
|||
throw std::runtime_error("Can't read cluster table");
|
||||
free_clusters = 0;
|
||||
for(unsigned i = 0; i < CLUSTERS_PER_SUPER; i++) {
|
||||
if(!(clusters[i] = read32ube(buffer + 4 * i)))
|
||||
if(!(clusters[i] = serialization::u32b(buffer + 4 * i)))
|
||||
free_clusters++;
|
||||
}
|
||||
}
|
||||
|
@ -276,7 +276,7 @@ void filesystem::supercluster::save(std::fstream& s, uint32_t index)
|
|||
uint64_t offset = SUPERCLUSTER_SIZE * index;
|
||||
char buffer[CLUSTER_SIZE];
|
||||
for(unsigned i = 0; i < CLUSTERS_PER_SUPER; i++)
|
||||
write32ube(buffer + 4 * i, clusters[i]);
|
||||
serialization::u32b(buffer + 4 * i, clusters[i]);
|
||||
s.clear();
|
||||
s.seekp(offset, std::ios_base::beg);
|
||||
s.write(buffer, CLUSTER_SIZE);
|
||||
|
|
|
@ -104,9 +104,9 @@ font2::glyph::glyph(std::istream& s)
|
|||
s.read(header, 26);
|
||||
if(!s)
|
||||
throw std::runtime_error("Can't read glyph bitmap header");
|
||||
if(read16ule(header + 0) != 0x4D42)
|
||||
if(serialization::u16l(header + 0) != 0x4D42)
|
||||
throw std::runtime_error("Bad glyph BMP magic");
|
||||
if(read16ule(header + 14) != 12) {
|
||||
if(serialization::u16l(header + 14) != 12) {
|
||||
//Not OS/2 format.
|
||||
old = false;
|
||||
rcount = 40;
|
||||
|
@ -115,19 +115,19 @@ font2::glyph::glyph(std::istream& s)
|
|||
throw std::runtime_error("Can't read glyph bitmap header");
|
||||
}
|
||||
|
||||
uint32_t startoff = read32ule(header + 10);
|
||||
uint32_t startoff = serialization::u32l(header + 10);
|
||||
if(old) {
|
||||
width = read16ule(header + 18);
|
||||
height = read16ule(header + 20);
|
||||
if(read16ule(header + 22) != 1)
|
||||
width = serialization::u16l(header + 18);
|
||||
height = serialization::u16l(header + 20);
|
||||
if(serialization::u16l(header + 22) != 1)
|
||||
throw std::runtime_error("Bad glyph BMP planecount");
|
||||
if(read16ule(header + 24) != 1)
|
||||
if(serialization::u16l(header + 24) != 1)
|
||||
throw std::runtime_error("Bad glyph BMP bitdepth");
|
||||
if(startoff < 26)
|
||||
throw std::runtime_error("Glyph BMP data can't overlap header");
|
||||
} else {
|
||||
long _width = read32sle(header + 18);
|
||||
long _height = read32sle(header + 22);
|
||||
long _width = serialization::s32l(header + 18);
|
||||
long _height = serialization::s32l(header + 22);
|
||||
if(_width < 0)
|
||||
throw std::runtime_error("Bad glyph BMP size");
|
||||
if(_height < 0)
|
||||
|
@ -135,11 +135,11 @@ font2::glyph::glyph(std::istream& s)
|
|||
width = _width;
|
||||
height = (_height >= 0) ? height : -height;
|
||||
|
||||
if(read16ule(header + 26) != 1)
|
||||
if(serialization::u16l(header + 26) != 1)
|
||||
throw std::runtime_error("Bad glyph BMP planecount");
|
||||
if(read16ule(header + 28) != 1)
|
||||
if(serialization::u16l(header + 28) != 1)
|
||||
throw std::runtime_error("Bad glyph BMP bitdepth");
|
||||
if(read32ule(header + 30) != 0)
|
||||
if(serialization::u32l(header + 30) != 0)
|
||||
throw std::runtime_error("Bad glyph BMP compression method");
|
||||
if(startoff < 40)
|
||||
throw std::runtime_error("Glyph BMP data can't overlap header");
|
||||
|
|
|
@ -184,7 +184,7 @@ void raw::load(const std::vector<char>& data) throw(std::bad_alloc, std::runtime
|
|||
throw std::runtime_error("Target framebuffer is not writable");
|
||||
pixfmt* nfmt = NULL;
|
||||
const uint8_t* data2 = reinterpret_cast<const uint8_t*>(&data[0]);
|
||||
size_t legacy_width = read16ube(data2);
|
||||
size_t legacy_width = serialization::u16b(data2);
|
||||
size_t dataoffset;
|
||||
size_t _width;
|
||||
size_t _height;
|
||||
|
@ -204,13 +204,13 @@ void raw::load(const std::vector<char>& data) throw(std::bad_alloc, std::runtime
|
|||
if(data.size() < 8)
|
||||
throw std::runtime_error("Bad screenshot data");
|
||||
dataoffset = 8;
|
||||
uint32_t magic = read32ube(data2 + 2);
|
||||
uint32_t magic = serialization::u32b(data2 + 2);
|
||||
for(pixfmt* f : pixfmts())
|
||||
if(f->get_magic() == magic)
|
||||
nfmt = f;
|
||||
if(!nfmt)
|
||||
throw std::runtime_error("Unknown screenshot format");
|
||||
_width = read16ube(data2 + 6);
|
||||
_width = serialization::u16b(data2 + 6);
|
||||
_height = (data.size() - 8) / (nfmt->get_ss_bpp() * _width);
|
||||
}
|
||||
if(data.size() < dataoffset + nfmt->get_ss_bpp() * _width * _height)
|
||||
|
@ -257,7 +257,7 @@ void raw::save(std::vector<char>& data) throw(std::bad_alloc)
|
|||
offset = 2;
|
||||
data.resize(offset + sbpp * static_cast<size_t>(width) * height);
|
||||
data2 = reinterpret_cast<uint8_t*>(&data[0]);
|
||||
write16ube(&data[0], width);
|
||||
serialization::u16b(&data[0], width);
|
||||
break;
|
||||
default:
|
||||
//Choose the first two bytes so that screenshot is bad in legacy format.
|
||||
|
@ -266,9 +266,9 @@ void raw::save(std::vector<char>& data) throw(std::bad_alloc)
|
|||
m++;
|
||||
offset = 8;
|
||||
data.resize(offset + sbpp * static_cast<size_t>(width) * height);
|
||||
write16ube(&data[0], m);
|
||||
write32ube(&data[2], magic);
|
||||
write16ube(&data[6], width);
|
||||
serialization::u16b(&data[0], m);
|
||||
serialization::u32b(&data[2], magic);
|
||||
serialization::u16b(&data[6], width);
|
||||
break;
|
||||
}
|
||||
data2 = reinterpret_cast<uint8_t*>(&data[0]);
|
||||
|
|
|
@ -139,8 +139,8 @@ struct search_value_helper
|
|||
{
|
||||
if(left < sizeof(value_type))
|
||||
return false;
|
||||
value_type v1 = read_of_endian<value_type>(oldv, endian);
|
||||
value_type v2 = read_of_endian<value_type>(newv, endian);
|
||||
value_type v1 = serialization::read_endian<value_type>(oldv, endian);
|
||||
value_type v2 = serialization::read_endian<value_type>(newv, endian);
|
||||
return val(v1, v2);
|
||||
}
|
||||
const T& val;
|
||||
|
@ -389,7 +389,7 @@ template<typename T> T memory_search::v_readold(uint64_t addr) throw()
|
|||
if(previous_content.size() < linaddr + maxr)
|
||||
return 0;
|
||||
memcpy(buf, &previous_content[linaddr], maxr);
|
||||
return read_of_endian<T>(buf, t.first->endian);
|
||||
return serialization::read_endian<T>(buf, t.first->endian);
|
||||
}
|
||||
i += t.first->size - t.second;
|
||||
}
|
||||
|
@ -610,18 +610,18 @@ void memory_search::savestate(std::vector<char>& buffer, enum savestate_type typ
|
|||
throw std::runtime_error("Invalid savestate type");
|
||||
buffer.resize(size);
|
||||
buffer[0] = type;
|
||||
write64ube(&buffer[1], linsize);
|
||||
serialization::u64b(&buffer[1], linsize);
|
||||
size_t offset = 9;
|
||||
if(type == ST_PREVMEM || type == ST_ALL) {
|
||||
memcpy(&buffer[offset], &previous_content[0], min(linsize, (uint64_t)previous_content.size()));
|
||||
offset += linsize;
|
||||
}
|
||||
if(type == ST_SET || type == ST_ALL) {
|
||||
write64ube(&buffer[offset], candidates);
|
||||
serialization::u64b(&buffer[offset], candidates);
|
||||
offset += 8;
|
||||
size_t bound = min((linsize + 63) / 64, (uint64_t)still_in.size());
|
||||
for(unsigned i = 0; i < bound; i++) {
|
||||
write64ube(&buffer[offset], still_in[i]);
|
||||
serialization::u64b(&buffer[offset], still_in[i]);
|
||||
offset += 8;
|
||||
}
|
||||
}
|
||||
|
@ -631,7 +631,7 @@ void memory_search::loadstate(const std::vector<char>& buffer)
|
|||
{
|
||||
if(buffer.size() < 9 || buffer[0] < ST_PREVMEM || buffer[0] > ST_ALL)
|
||||
throw std::runtime_error("Invalid memory search save");
|
||||
uint64_t linsize = read64ube(&buffer[1]);
|
||||
uint64_t linsize = serialization::u64b(&buffer[1]);
|
||||
if(linsize != mspace.get_linear_size())
|
||||
throw std::runtime_error("Save size mismatch (not from this game)");
|
||||
if(!previous_content.size())
|
||||
|
@ -643,11 +643,11 @@ void memory_search::loadstate(const std::vector<char>& buffer)
|
|||
offset += linsize;
|
||||
}
|
||||
if(type == ST_SET || type == ST_ALL) {
|
||||
candidates = read64ube(&buffer[offset]);
|
||||
candidates = serialization::u64b(&buffer[offset]);
|
||||
offset += 8;
|
||||
size_t bound = min((linsize + 63) / 64, (uint64_t)still_in.size());
|
||||
for(unsigned i = 0; i < bound; i++) {
|
||||
still_in[i] = read64ube(&buffer[offset]);
|
||||
still_in[i] = serialization::u64b(&buffer[offset]);
|
||||
offset += 8;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,11 +17,11 @@ namespace
|
|||
if(!g.first || g.second + sizeof(T) > g.first->size)
|
||||
return 0;
|
||||
if(g.first->direct_map)
|
||||
return read_of_endian<T>(g.first->direct_map + g.second, g.first->endian);
|
||||
return serialization::read_endian<T>(g.first->direct_map + g.second, g.first->endian);
|
||||
else {
|
||||
T buf;
|
||||
g.first->read(g.second, &buf, sizeof(T));
|
||||
return read_of_endian<T>(&buf, g.first->endian);
|
||||
return serialization::read_endian<T>(&buf, g.first->endian);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,10 +36,10 @@ namespace
|
|||
if(!g.first || g.first->readonly || g.second + sizeof(T) > g.first->size)
|
||||
return false;
|
||||
if(g.first->direct_map)
|
||||
write_of_endian(g.first->direct_map + g.second, value, g.first->endian);
|
||||
serialization::write_endian(g.first->direct_map + g.second, value, g.first->endian);
|
||||
else {
|
||||
T buf;
|
||||
write_of_endian(&buf, value, g.first->endian);
|
||||
serialization::write_endian(&buf, value, g.first->endian);
|
||||
g.first->write(g.second, &buf, sizeof(T));
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -438,7 +438,7 @@ page::page(const char* buffer, size_t& advance) throw(std::runtime_error)
|
|||
data_count += (unsigned char)buffer[27 + i];
|
||||
}
|
||||
//Check the CRC.
|
||||
uint32_t claimed = read32ule(buffer + 22);
|
||||
uint32_t claimed = serialization::u32l(buffer + 22);
|
||||
uint32_t x = 0;
|
||||
uint32_t actual = oggcrc32(0, NULL, 0);
|
||||
actual = oggcrc32(actual, reinterpret_cast<const uint8_t*>(buffer), 22);
|
||||
|
@ -452,9 +452,9 @@ page::page(const char* buffer, size_t& advance) throw(std::runtime_error)
|
|||
flag_continue = (flags & 1);
|
||||
flag_bos = (flags & 2);
|
||||
flag_eos = (flags & 4);
|
||||
granulepos = read64ule(buffer + 6);
|
||||
stream = read32ule(buffer + 14);
|
||||
sequence = read32ule(buffer + 18);
|
||||
granulepos = serialization::u64l(buffer + 6);
|
||||
stream = serialization::u32l(buffer + 14);
|
||||
sequence = serialization::u32l(buffer + 18);
|
||||
segment_count = buffer[26];
|
||||
memset(segments, 0, sizeof(segments));
|
||||
if(segment_count)
|
||||
|
@ -530,7 +530,7 @@ bool page::scan(const char* buffer, size_t bufferlen, bool eof, size_t& advance)
|
|||
}
|
||||
}
|
||||
//Check the CRC.
|
||||
uint32_t claimed = read32ule(_buffer + 22);
|
||||
uint32_t claimed = serialization::u32l(_buffer + 22);
|
||||
uint32_t x = 0;
|
||||
uint32_t actual = oggcrc32(0, NULL, 0);
|
||||
actual = oggcrc32(actual, reinterpret_cast<const uint8_t*>(_buffer), 22);
|
||||
|
@ -637,16 +637,16 @@ void page::serialize(char* buffer) const throw()
|
|||
memcpy(buffer, "OggS", 4);
|
||||
buffer[4] = version;
|
||||
buffer[5] = (flag_continue ? 1 : 0) | (flag_bos ? 2 : 0) | (flag_eos ? 4 : 0);
|
||||
write64ule(buffer + 6, granulepos);
|
||||
write32ule(buffer + 14, stream);
|
||||
write32ule(buffer + 18, sequence);
|
||||
write32ule(buffer + 22, 0); //CRC will be fixed later.
|
||||
serialization::u64l(buffer + 6, granulepos);
|
||||
serialization::u32l(buffer + 14, stream);
|
||||
serialization::u32l(buffer + 18, sequence);
|
||||
serialization::u32l(buffer + 22, 0); //CRC will be fixed later.
|
||||
buffer[26] = segment_count;
|
||||
memcpy(buffer + 27, segments, segment_count);
|
||||
memcpy(buffer + 27 + segment_count, data, data_count);
|
||||
size_t plen = 27 + segment_count + data_count;
|
||||
//Fix the CRC.
|
||||
write32ule(buffer + 22, oggcrc32(oggcrc32(0, NULL, 0), reinterpret_cast<uint8_t*>(buffer), plen));
|
||||
serialization::u32l(buffer + 22, oggcrc32(oggcrc32(0, NULL, 0), reinterpret_cast<uint8_t*>(buffer), plen));
|
||||
}
|
||||
|
||||
const uint64_t page::granulepos_none = 0xFFFFFFFFFFFFFFFFULL;
|
||||
|
|
|
@ -23,9 +23,9 @@ struct oggopus_header parse_oggopus_header(struct ogg::packet& packet) throw(std
|
|||
throw std::runtime_error("Zero channels not allowed");
|
||||
h.version = p[8];
|
||||
h.channels = p[9];
|
||||
h.preskip = read16ule(&p[10]);
|
||||
h.rate = read32ule(&p[12]);
|
||||
h.gain = read16sle(&p[16]);
|
||||
h.preskip = serialization::u16l(&p[10]);
|
||||
h.rate = serialization::u32l(&p[12]);
|
||||
h.gain = serialization::s16l(&p[16]);
|
||||
h.map_family = p[18];
|
||||
memset(h.chanmap, 255, sizeof(h.chanmap));
|
||||
if(h.map_family) {
|
||||
|
@ -67,18 +67,18 @@ struct oggopus_tags parse_oggopus_tags(struct ogg::packet& packet) throw(std::ba
|
|||
//Scan the thing.
|
||||
size_t itr = 8;
|
||||
size_t oitr = 8;
|
||||
itr = itr + 4 + read32ule(&p[itr]);
|
||||
itr = itr + 4 + serialization::u32l(&p[itr]);
|
||||
if(itr + 4 > p.size())
|
||||
throw std::runtime_error("OggOpus header packet truncated");
|
||||
h.vendor = std::string(&p[oitr + 4], &p[itr]);
|
||||
if(itr + 4 > p.size())
|
||||
throw std::runtime_error("OggOpus header packet truncated");
|
||||
uint32_t headers = read32ule(&p[itr]);
|
||||
uint32_t headers = serialization::u32l(&p[itr]);
|
||||
itr += 4;
|
||||
for(uint32_t i = 0; i < headers; i++) {
|
||||
if(itr + 4 > p.size())
|
||||
throw std::runtime_error("OggOpus header packet truncated");
|
||||
itr = itr + 4 + read32ule(&p[itr]);
|
||||
itr = itr + 4 + serialization::u32l(&p[itr]);
|
||||
if(itr > p.size())
|
||||
throw std::runtime_error("OggOpus header packet truncated");
|
||||
h.comments.push_back(std::string(&p[oitr + 4], &p[itr]));
|
||||
|
@ -102,12 +102,12 @@ struct ogg::page serialize_oggopus_header(struct oggopus_header& header) throw(s
|
|||
for(unsigned i = 0; i < header.channels; i++)
|
||||
if(header.chanmap[i] != 255 && header.chanmap[i] > header.streams + header.coupled)
|
||||
throw std::runtime_error("Logical channel mapped to invalid physical channel");
|
||||
write64ube(buffer, 0x4F70757348656164ULL);
|
||||
serialization::u64b(buffer, 0x4F70757348656164ULL);
|
||||
buffer[8] = header.version;
|
||||
buffer[9] = header.channels;
|
||||
write16ule(buffer + 10, header.preskip);
|
||||
write32ule(buffer + 12, header.rate);
|
||||
write16sle(buffer + 16, header.gain);
|
||||
serialization::u16l(buffer + 10, header.preskip);
|
||||
serialization::u32l(buffer + 12, header.rate);
|
||||
serialization::s16l(buffer + 16, header.gain);
|
||||
buffer[18] = header.map_family;
|
||||
if(header.map_family) {
|
||||
buffer[19] = header.streams;
|
||||
|
@ -137,14 +137,14 @@ uint32_t serialize_oggopus_tags(struct oggopus_tags& tags, std::function<void(co
|
|||
std::vector<uint8_t> contents;
|
||||
contents.resize(needed);
|
||||
size_t itr = 0;
|
||||
write64ube(&contents[0], 0x4F70757354616773ULL);
|
||||
write32ule(&contents[8], tags.vendor.length());
|
||||
serialization::u64b(&contents[0], 0x4F70757354616773ULL);
|
||||
serialization::u32l(&contents[8], tags.vendor.length());
|
||||
std::copy(tags.vendor.begin(), tags.vendor.end(), reinterpret_cast<char*>(&contents[12]));
|
||||
itr = 12 + tags.vendor.length();
|
||||
write32ule(&contents[itr], tags.comments.size());
|
||||
serialization::u32l(&contents[itr], tags.comments.size());
|
||||
itr += 4;
|
||||
for(auto i : tags.comments) {
|
||||
write32ule(&contents[itr], i.length());
|
||||
serialization::u32l(&contents[itr], i.length());
|
||||
std::copy(i.begin(), i.end(), reinterpret_cast<char*>(&contents[itr + 4]));
|
||||
itr += (i.length() + 4);
|
||||
}
|
||||
|
|
|
@ -81,12 +81,12 @@ namespace
|
|||
size_t psize = patch.size() - 12;
|
||||
uint32_t crc_init = crc32(0, NULL, 0);
|
||||
uint32_t pchcrc_c = crc32(crc_init, reinterpret_cast<const uint8_t*>(&patch[0]), patch.size() - 4);
|
||||
uint32_t pchcrc = read32ule(_patch + psize + 8);
|
||||
uint32_t pchcrc = serialization::u32l(_patch + psize + 8);
|
||||
if(pchcrc_c != pchcrc)
|
||||
(stringfmt() << "CRC mismatch on patch: Claimed: " << pchcrc << " Actual: " << pchcrc_c
|
||||
<< ".").throwex();
|
||||
uint32_t srccrc = read32ule(_patch + psize + 0);
|
||||
uint32_t dstcrc = read32ule(_patch + psize + 4);
|
||||
uint32_t srccrc = serialization::u32l(_patch + psize + 0);
|
||||
uint32_t dstcrc = serialization::u32l(_patch + psize + 4);
|
||||
uint64_t srcsize = decode_varint(_patch, ioffset, psize);
|
||||
uint64_t dstsize = decode_varint(_patch, ioffset, psize);
|
||||
uint64_t mdtsize = decode_varint(_patch, ioffset, psize);
|
||||
|
|
|
@ -136,12 +136,12 @@ namespace
|
|||
{
|
||||
uint32_t crc = crc32(0, NULL, 0);
|
||||
char fixed[12];
|
||||
write32ube(fixed, stream.size());
|
||||
write32ube(fixed + 4, type);
|
||||
serialization::u32b(fixed, stream.size());
|
||||
serialization::u32b(fixed + 4, type);
|
||||
crc = crc32(crc, reinterpret_cast<Bytef*>(fixed + 4), 4);
|
||||
if(stream.size() > 0)
|
||||
crc = crc32(crc, reinterpret_cast<Bytef*>(&stream[0]), stream.size());
|
||||
write32ube(fixed + 8, crc);
|
||||
serialization::u32b(fixed + 8, crc);
|
||||
os.write(fixed, 8);
|
||||
os.write(&stream[0], stream.size());
|
||||
os.write(fixed + 8, 4);
|
||||
|
@ -258,8 +258,8 @@ namespace
|
|||
}
|
||||
throw std::runtime_error("PNG file truncated");
|
||||
}
|
||||
size = read32ube(buf + 0);
|
||||
type = read32ube(buf + 4);
|
||||
size = serialization::u32b(buf + 0);
|
||||
type = serialization::u32b(buf + 4);
|
||||
crc = crc32(0, NULL, 0);
|
||||
crc = crc32(crc, buf + 4, 4);
|
||||
ptr = 0;
|
||||
|
@ -273,7 +273,7 @@ namespace
|
|||
stream.read(buf, 4);
|
||||
if(!stream)
|
||||
throw std::runtime_error("PNG file truncated");
|
||||
uint32_t claim_crc = read32ube(buf);
|
||||
uint32_t claim_crc = serialization::u32b(buf);
|
||||
if(crc != claim_crc)
|
||||
throw std::runtime_error("PNG file chunk CRC check failed");
|
||||
}
|
||||
|
@ -306,8 +306,8 @@ badtype:
|
|||
throw std::runtime_error("Expected IHDR chunk to be 13 bytes");
|
||||
uint8_t buf[13];
|
||||
d.chunk_read(buf, 13);
|
||||
width = read32ube(buf + 0);
|
||||
height = read32ube(buf + 4);
|
||||
width = serialization::u32b(buf + 0);
|
||||
height = serialization::u32b(buf + 4);
|
||||
depth = buf[8];
|
||||
type = buf[9];
|
||||
compression = buf[10];
|
||||
|
@ -593,10 +593,10 @@ badtype:
|
|||
case 2: v = (*in >> (6 - bit)) & 3; m = 0x555555; break;
|
||||
case 4: v = (*in >> (4 - bit)) & 15; m = 0x111111; break;
|
||||
case 8: v = *in; m = 0xFFFFFF; m = 0x010101; break;
|
||||
case 16: v = read16ube(in); m = 0x010101; s = 8; break;
|
||||
case 16: v = serialization::u16b(in); m = 0x010101; s = 8; break;
|
||||
};
|
||||
uint32_t alpha = 0xFF000000U;
|
||||
if(v == read16ube(trans))
|
||||
if(v == serialization::u16b(trans))
|
||||
alpha = 0;
|
||||
return alpha | (m * (v >> s));
|
||||
}
|
||||
|
@ -631,7 +631,7 @@ badtype:
|
|||
case 2: return (*in >> (6 - bit)) & 3;
|
||||
case 4: return (*in >> (4 - bit)) & 15;
|
||||
case 8: return *in;
|
||||
case 16: return read16ube(in);
|
||||
case 16: return serialization::u16b(in);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -983,8 +983,8 @@ void png_encodedable_image::encode(std::ostream& file) const
|
|||
file.write(png_magic, sizeof(png_magic));
|
||||
//Write the IHDR
|
||||
char ihdr[13];
|
||||
write32ube(ihdr + 0, width);
|
||||
write32ube(ihdr + 4, height);
|
||||
serialization::u32b(ihdr + 0, width);
|
||||
serialization::u32b(ihdr + 4, height);
|
||||
ihdr[8] = has_palette ? size_to_bits(palette.size()) : 8;
|
||||
ihdr[9] = has_palette ? 3 : (has_alpha ? 6 : 2);
|
||||
ihdr[10] = 0; //Deflate,
|
||||
|
|
|
@ -47,8 +47,8 @@ namespace
|
|||
{
|
||||
uint8_t header[] = {31, 139, 8, 0, 0, 0, 0, 0, 0, 255};
|
||||
uint8_t trailer[8];
|
||||
write32ule(trailer + 0, crc);
|
||||
write32ule(trailer + 4, size);
|
||||
serialization::u32l(trailer + 0, crc);
|
||||
serialization::u32l(trailer + 4, size);
|
||||
while(hdr < 10) {
|
||||
if(!outsize) return false;
|
||||
*(out++) = header[hdr++];
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "zip.hpp"
|
||||
#include "directory.hpp"
|
||||
#include "serialization.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
@ -30,33 +31,6 @@ int rename_overwrite(const char* oldname, const char* newname)
|
|||
|
||||
namespace
|
||||
{
|
||||
uint32_t read32(const unsigned char* buf, unsigned offset = 0, unsigned modulo = 4) throw()
|
||||
{
|
||||
return (uint32_t)buf[offset % modulo] |
|
||||
((uint32_t)buf[(offset + 1) % modulo] << 8) |
|
||||
((uint32_t)buf[(offset + 2) % modulo] << 16) |
|
||||
((uint32_t)buf[(offset + 3) % modulo] << 24);
|
||||
}
|
||||
|
||||
uint16_t read16(const unsigned char* buf) throw()
|
||||
{
|
||||
return (uint16_t)buf[0] | ((uint16_t)buf[1] << 8);
|
||||
}
|
||||
|
||||
void write16(unsigned char* buf, uint16_t value) throw()
|
||||
{
|
||||
buf[0] = (value) & 0xFF;
|
||||
buf[1] = (value >> 8) & 0xFF;
|
||||
}
|
||||
|
||||
void write32(unsigned char* buf, uint32_t value) throw()
|
||||
{
|
||||
buf[0] = (value) & 0xFF;
|
||||
buf[1] = (value >> 8) & 0xFF;
|
||||
buf[2] = (value >> 16) & 0xFF;
|
||||
buf[3] = (value >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
class file_input
|
||||
{
|
||||
public:
|
||||
|
@ -250,23 +224,23 @@ namespace
|
|||
unsigned char buffer[30];
|
||||
if(!(file.read(reinterpret_cast<char*>(buffer), 30)))
|
||||
throw std::runtime_error("Can't read file header from ZIP file");
|
||||
uint32_t magic = read32(buffer);
|
||||
uint32_t magic = serialization::u32l(buffer);
|
||||
if(magic == 0x02014b50) {
|
||||
info.central_directory_special = true;
|
||||
return info;
|
||||
}
|
||||
if(magic != 0x04034b50)
|
||||
throw std::runtime_error("ZIP archive corrupt: Expected file or central directory magic");
|
||||
info.version_needed = read16(buffer + 4);
|
||||
info.flags = read16(buffer + 6);
|
||||
info.compression = read16(buffer + 8);
|
||||
info.mtime_time = read16(buffer + 10);
|
||||
info.mtime_day = read16(buffer + 12);
|
||||
info.crc = read32(buffer + 14);
|
||||
info.compressed_size = read32(buffer + 18);
|
||||
info.uncompressed_size = read32(buffer + 22);
|
||||
uint16_t filename_len = read16(buffer + 26);
|
||||
uint16_t extra_len = read16(buffer + 28);
|
||||
info.version_needed = serialization::u16l(buffer + 4);
|
||||
info.flags = serialization::u16l(buffer + 6);
|
||||
info.compression = serialization::u16l(buffer + 8);
|
||||
info.mtime_time = serialization::u16l(buffer + 10);
|
||||
info.mtime_day = serialization::u16l(buffer + 12);
|
||||
info.crc = serialization::u32l(buffer + 14);
|
||||
info.compressed_size = serialization::u32l(buffer + 18);
|
||||
info.uncompressed_size = serialization::u32l(buffer + 22);
|
||||
uint16_t filename_len = serialization::u16l(buffer + 26);
|
||||
uint16_t extra_len = serialization::u16l(buffer + 28);
|
||||
if(!filename_len)
|
||||
throw std::runtime_error("Unsupported ZIP feature: Empty filename not allowed");
|
||||
if(info.version_needed > 20) {
|
||||
|
@ -439,37 +413,37 @@ void writer::commit() throw(std::bad_alloc, std::logic_error, std::runtime_error
|
|||
for(auto i : files) {
|
||||
cdirsize += (46 + i.first.length());
|
||||
directory_entry.resize(46 + i.first.length());
|
||||
write32(&directory_entry[0], 0x02014b50);
|
||||
write16(&directory_entry[4], 3);
|
||||
write16(&directory_entry[6], 20);
|
||||
write16(&directory_entry[8], 0);
|
||||
write16(&directory_entry[10], compression ? 8 : 0);
|
||||
write16(&directory_entry[12], 0);
|
||||
write16(&directory_entry[14], 10273);
|
||||
write32(&directory_entry[16], i.second.crc);
|
||||
write32(&directory_entry[20], i.second.compressed_size);
|
||||
write32(&directory_entry[24], i.second.uncompressed_size);
|
||||
write16(&directory_entry[28], i.first.length());
|
||||
write16(&directory_entry[30], 0);
|
||||
write16(&directory_entry[32], 0);
|
||||
write16(&directory_entry[34], 0);
|
||||
write16(&directory_entry[36], 0);
|
||||
write32(&directory_entry[38], 0);
|
||||
write32(&directory_entry[42], i.second.offset);
|
||||
serialization::u32l(&directory_entry[0], 0x02014b50);
|
||||
serialization::u16l(&directory_entry[4], 3);
|
||||
serialization::u16l(&directory_entry[6], 20);
|
||||
serialization::u16l(&directory_entry[8], 0);
|
||||
serialization::u16l(&directory_entry[10], compression ? 8 : 0);
|
||||
serialization::u16l(&directory_entry[12], 0);
|
||||
serialization::u16l(&directory_entry[14], 10273);
|
||||
serialization::u32l(&directory_entry[16], i.second.crc);
|
||||
serialization::u32l(&directory_entry[20], i.second.compressed_size);
|
||||
serialization::u32l(&directory_entry[24], i.second.uncompressed_size);
|
||||
serialization::u16l(&directory_entry[28], i.first.length());
|
||||
serialization::u16l(&directory_entry[30], 0);
|
||||
serialization::u16l(&directory_entry[32], 0);
|
||||
serialization::u16l(&directory_entry[34], 0);
|
||||
serialization::u16l(&directory_entry[36], 0);
|
||||
serialization::u32l(&directory_entry[38], 0);
|
||||
serialization::u32l(&directory_entry[42], i.second.offset);
|
||||
memcpy(&directory_entry[46], i.first.c_str(), i.first.length());
|
||||
zipstream->write(reinterpret_cast<char*>(&directory_entry[0]), directory_entry.size());
|
||||
if(!*zipstream)
|
||||
throw std::runtime_error("Failed to write central directory entry to output file");
|
||||
}
|
||||
directory_entry.resize(22);
|
||||
write32(&directory_entry[0], 0x06054b50);
|
||||
write16(&directory_entry[4], 0);
|
||||
write16(&directory_entry[6], 0);
|
||||
write16(&directory_entry[8], files.size());
|
||||
write16(&directory_entry[10], files.size());
|
||||
write32(&directory_entry[12], cdirsize);
|
||||
write32(&directory_entry[16], cdiroff);
|
||||
write16(&directory_entry[20], 0);
|
||||
serialization::u32l(&directory_entry[0], 0x06054b50);
|
||||
serialization::u16l(&directory_entry[4], 0);
|
||||
serialization::u16l(&directory_entry[6], 0);
|
||||
serialization::u16l(&directory_entry[8], files.size());
|
||||
serialization::u16l(&directory_entry[10], files.size());
|
||||
serialization::u32l(&directory_entry[12], cdirsize);
|
||||
serialization::u32l(&directory_entry[16], cdiroff);
|
||||
serialization::u16l(&directory_entry[20], 0);
|
||||
zipstream->write(reinterpret_cast<char*>(&directory_entry[0]), directory_entry.size());
|
||||
if(!*zipstream)
|
||||
throw std::runtime_error("Failed to write central directory end marker to output file");
|
||||
|
@ -520,16 +494,16 @@ void writer::close_file() throw(std::bad_alloc, std::logic_error, std::runtime_e
|
|||
throw std::runtime_error("Can't read current ZIP stream position");
|
||||
unsigned char header[30];
|
||||
memset(header, 0, 30);
|
||||
write32(header, 0x04034b50);
|
||||
serialization::u32l(header, 0x04034b50);
|
||||
header[4] = 20;
|
||||
header[6] = 0;
|
||||
header[8] = compression ? 8 : 0;
|
||||
header[12] = 33;
|
||||
header[13] = 40;
|
||||
write32(header + 14, crc32);
|
||||
write32(header + 18, cs);
|
||||
write32(header + 22, ucs);
|
||||
write16(header + 26, open_file.length());
|
||||
serialization::u32l(header + 14, crc32);
|
||||
serialization::u32l(header + 18, cs);
|
||||
serialization::u32l(header + 22, ucs);
|
||||
serialization::u16l(header + 26, open_file.length());
|
||||
zipstream->write(reinterpret_cast<char*>(header), 30);
|
||||
zipstream->write(open_file.c_str(), open_file.length());
|
||||
zipstream->write(¤t_compressed_file[0], current_compressed_file.size());
|
||||
|
|
|
@ -329,15 +329,15 @@ namespace
|
|||
memset(buf, 0, buffersize);
|
||||
if(lua_class<lua_bitmap>::is(L, 1)) {
|
||||
lua_bitmap* b = lua_class<lua_bitmap>::get(L, 1, fname.c_str());
|
||||
write64ube(buf + 0, b->width);
|
||||
write64ube(buf + 8, b->height);
|
||||
serialization::u64b(buf + 0, b->width);
|
||||
serialization::u64b(buf + 8, b->height);
|
||||
bufferuse = 16;
|
||||
for(unsigned i = 0; i < b->width * b->height; i++) {
|
||||
if(bufferuse + 2 > buffersize) {
|
||||
h.write(buf, bufferuse);
|
||||
bufferuse = 0;
|
||||
}
|
||||
write16ube(buf + bufferuse + 0, b->pixels[i]);
|
||||
serialization::u16b(buf + bufferuse + 0, b->pixels[i]);
|
||||
bufferuse += 2;
|
||||
}
|
||||
if(bufferuse > 0) h.write(buf, bufferuse);
|
||||
|
@ -345,16 +345,16 @@ namespace
|
|||
return 1;
|
||||
} else if(lua_class<lua_dbitmap>::is(L, 1)) {
|
||||
lua_dbitmap* b = lua_class<lua_dbitmap>::get(L, 1, fname.c_str());
|
||||
write64ube(buf + 0, b->width);
|
||||
write64ube(buf + 4, b->height);
|
||||
serialization::u64b(buf + 0, b->width);
|
||||
serialization::u64b(buf + 4, b->height);
|
||||
bufferuse = 16;
|
||||
for(unsigned i = 0; i < b->width * b->height; i++) {
|
||||
if(bufferuse + 6 > buffersize) {
|
||||
h.write(buf, bufferuse);
|
||||
bufferuse = 0;
|
||||
}
|
||||
write32ube(buf + bufferuse + 0, b->pixels[i].orig);
|
||||
write16ube(buf + bufferuse + 4, b->pixels[i].origa);
|
||||
serialization::u32b(buf + bufferuse + 0, b->pixels[i].orig);
|
||||
serialization::u16b(buf + bufferuse + 4, b->pixels[i].origa);
|
||||
bufferuse += 6;
|
||||
}
|
||||
if(bufferuse > 0) h.write(buf, bufferuse);
|
||||
|
@ -379,8 +379,8 @@ namespace
|
|||
h.write(buf, bufferuse);
|
||||
bufferuse = 0;
|
||||
}
|
||||
write32ube(buf + bufferuse + 0, p->colors[i].orig);
|
||||
write16ube(buf + bufferuse + 4, p->colors[i].origa);
|
||||
serialization::u32b(buf + bufferuse + 0, p->colors[i].orig);
|
||||
serialization::u16b(buf + bufferuse + 4, p->colors[i].origa);
|
||||
bufferuse += 6;
|
||||
}
|
||||
if(bufferuse > 0) h.write(buf, bufferuse);
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace
|
|||
L.pushboolean(0);
|
||||
return 1;
|
||||
}
|
||||
L.pushnumber(read_of_endian<S>(&h[address], 1));
|
||||
L.pushnumber(serialization::read_endian<S>(&h[address], 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ namespace
|
|||
auto& h = get_host_memory();
|
||||
if(address + sizeof(S) > h.size())
|
||||
h.resize(address + sizeof(S));
|
||||
write_of_endian<S>(&h[address], value, 1);
|
||||
serialization::write_endian<S>(&h[address], value, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ namespace
|
|||
template<typename T> T bswap(T val)
|
||||
{
|
||||
T val2 = val;
|
||||
swap_endian(val2);
|
||||
serialization::swap_endian(val2);
|
||||
return val2;
|
||||
}
|
||||
|
||||
|
|
|
@ -46,9 +46,9 @@ stream_statistics dump_stream(filesystem& fs, uint32_t ccluster, uint32_t dclust
|
|||
size_t r = fs.read_data(ctrl_c, ctrl_o, buffer, 4);
|
||||
if(r < 4)
|
||||
break;
|
||||
uint16_t psize = read16ube(buffer + 0);
|
||||
uint16_t plength = 120 * read8ube(buffer + 2);
|
||||
if(!read8ube(buffer + 3))
|
||||
uint16_t psize = serialization::u16b(buffer + 0);
|
||||
uint16_t plength = 120 * serialization::u8b(buffer + 2);
|
||||
if(!serialization::u8b(buffer + 3))
|
||||
break;
|
||||
r = fs.skip_data(data_c, data_o, psize);
|
||||
if(r < psize)
|
||||
|
@ -97,9 +97,9 @@ int main(int argc, char** argv)
|
|||
size_t r = fs->read_data(maindir_c, maindir_o, buffer, 16);
|
||||
if(r < 16)
|
||||
break;
|
||||
uint64_t stream_ts = read64ube(buffer);
|
||||
uint32_t stream_c = read32ube(buffer + 8);
|
||||
uint32_t stream_d = read32ube(buffer + 12);
|
||||
uint64_t stream_ts = serialization::u64b(buffer);
|
||||
uint32_t stream_c = serialization::u32b(buffer + 8);
|
||||
uint32_t stream_d = serialization::u32b(buffer + 12);
|
||||
if(!stream_c)
|
||||
continue;
|
||||
std::cout << "Found stream (from " << maindir_uc << ":" << maindir_uo << "): ts=" << stream_ts
|
||||
|
|
|
@ -54,9 +54,9 @@ namespace
|
|||
{
|
||||
uint32_t fulltype = get_actual_packet_type(track, pkt.typecode);
|
||||
char buf[8 + PADGRANULARITY];
|
||||
write32ule(buf + 0, fulltype);
|
||||
write32ule(buf + 4, pkt.payload.size());
|
||||
write32ule(buf + 8, 0);
|
||||
serialization::u32l(buf + 0, fulltype);
|
||||
serialization::u32l(buf + 4, pkt.payload.size());
|
||||
serialization::u32l(buf + 8, 0);
|
||||
size_t padding = (PADGRANULARITY - pkt.payload.size() % PADGRANULARITY) % PADGRANULARITY;
|
||||
avifile.outstream->write(buf, 8);
|
||||
avifile.outstream->write(&pkt.payload[0], pkt.payload.size());
|
||||
|
|
|
@ -35,7 +35,7 @@ namespace
|
|||
{
|
||||
out.payload.resize(2 * chans * samples);
|
||||
for(size_t i = 0; i < chans * samples; i++)
|
||||
write16sle(&out.payload[2 * i], data[i]);
|
||||
serialization::s16l(&out.payload[2 * i], data[i]);
|
||||
out.typecode = 0x6277;
|
||||
out.indexflags = 0x10;
|
||||
out.hidden = false;
|
||||
|
|
|
@ -20,19 +20,19 @@ void stream_format_video::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(size());
|
||||
write32ule(&buf[0], 0x66727473UL); //Type
|
||||
write32ule(&buf[4], size() - 8); //Size.
|
||||
write32ule(&buf[8], 40 + extra.size()); //BITMAPINFOHEADER size.
|
||||
write32ule(&buf[12], width);
|
||||
write32ule(&buf[16], height);
|
||||
write16ule(&buf[20], planes);
|
||||
write16ule(&buf[22], bit_count);
|
||||
write32ule(&buf[24], compression);
|
||||
write32ule(&buf[28], size_image);
|
||||
write32ule(&buf[32], resolution_x);
|
||||
write32ule(&buf[36], resolution_y);
|
||||
write32ule(&buf[40], clr_used);
|
||||
write32ule(&buf[44], clr_important);
|
||||
serialization::u32l(&buf[0], 0x66727473UL); //Type
|
||||
serialization::u32l(&buf[4], size() - 8); //Size.
|
||||
serialization::u32l(&buf[8], 40 + extra.size()); //BITMAPINFOHEADER size.
|
||||
serialization::u32l(&buf[12], width);
|
||||
serialization::u32l(&buf[16], height);
|
||||
serialization::u16l(&buf[20], planes);
|
||||
serialization::u16l(&buf[22], bit_count);
|
||||
serialization::u32l(&buf[24], compression);
|
||||
serialization::u32l(&buf[28], size_image);
|
||||
serialization::u32l(&buf[32], resolution_x);
|
||||
serialization::u32l(&buf[36], resolution_y);
|
||||
serialization::u32l(&buf[40], clr_used);
|
||||
serialization::u32l(&buf[44], clr_important);
|
||||
memcpy(&buf[48], &extra[0], extra.size());
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
|
@ -53,15 +53,15 @@ void stream_format_audio::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(size());
|
||||
write32ule(&buf[0], 0x66727473UL); //Type
|
||||
write32ule(&buf[4], size() - 8); //Size.
|
||||
write16ule(&buf[8], format_tag);
|
||||
write16ule(&buf[10], channels);
|
||||
write32ule(&buf[12], samples_per_second);
|
||||
write32ule(&buf[16], average_bytes_per_second);
|
||||
write16ule(&buf[20], block_align);
|
||||
write16ule(&buf[22], bits_per_sample);
|
||||
write16ule(&buf[24], extra.size()); //Extension data.
|
||||
serialization::u32l(&buf[0], 0x66727473UL); //Type
|
||||
serialization::u32l(&buf[4], size() - 8); //Size.
|
||||
serialization::u16l(&buf[8], format_tag);
|
||||
serialization::u16l(&buf[10], channels);
|
||||
serialization::u32l(&buf[12], samples_per_second);
|
||||
serialization::u32l(&buf[16], average_bytes_per_second);
|
||||
serialization::u16l(&buf[20], block_align);
|
||||
serialization::u16l(&buf[22], bits_per_sample);
|
||||
serialization::u16l(&buf[24], extra.size()); //Extension data.
|
||||
memset(&buf[26], 0, size() - 26); //Pad
|
||||
memcpy(&buf[26], &extra[0], extra.size());
|
||||
out.write(&buf[0], buf.size());
|
||||
|
@ -78,25 +78,25 @@ void stream_header::serialize(std::ostream& out, struct stream_format_base& form
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(size());
|
||||
write32ule(&buf[0], 0x68727473UL); //Type
|
||||
write32ule(&buf[4], size() - 8); //Size.
|
||||
write32ule(&buf[8], format.type());
|
||||
write32ule(&buf[12], handler);
|
||||
write32ule(&buf[16], flags);
|
||||
write16ule(&buf[20], priority);
|
||||
write16ule(&buf[22], language);
|
||||
write32ule(&buf[24], initial_frames);
|
||||
write32ule(&buf[28], format.scale());
|
||||
write32ule(&buf[32], format.rate());
|
||||
write32ule(&buf[36], start);
|
||||
write32ule(&buf[40], length);
|
||||
write32ule(&buf[44], suggested_buffer_size);
|
||||
write32ule(&buf[48], quality);
|
||||
write32ule(&buf[52], format.sample_size());
|
||||
write32ule(&buf[56], format.rect_left());
|
||||
write32ule(&buf[60], format.rect_top());
|
||||
write32ule(&buf[64], format.rect_right());
|
||||
write32ule(&buf[68], format.rect_bottom());
|
||||
serialization::u32l(&buf[0], 0x68727473UL); //Type
|
||||
serialization::u32l(&buf[4], size() - 8); //Size.
|
||||
serialization::u32l(&buf[8], format.type());
|
||||
serialization::u32l(&buf[12], handler);
|
||||
serialization::u32l(&buf[16], flags);
|
||||
serialization::u16l(&buf[20], priority);
|
||||
serialization::u16l(&buf[22], language);
|
||||
serialization::u32l(&buf[24], initial_frames);
|
||||
serialization::u32l(&buf[28], format.scale());
|
||||
serialization::u32l(&buf[32], format.rate());
|
||||
serialization::u32l(&buf[36], start);
|
||||
serialization::u32l(&buf[40], length);
|
||||
serialization::u32l(&buf[44], suggested_buffer_size);
|
||||
serialization::u32l(&buf[48], quality);
|
||||
serialization::u32l(&buf[52], format.sample_size());
|
||||
serialization::u32l(&buf[56], format.rect_left());
|
||||
serialization::u32l(&buf[60], format.rect_top());
|
||||
serialization::u32l(&buf[64], format.rect_right());
|
||||
serialization::u32l(&buf[68], format.rect_bottom());
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
throw std::runtime_error("Can't write strh");
|
||||
|
@ -110,9 +110,9 @@ void stream_header_list<format>::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(12);
|
||||
write32ule(&buf[0], 0x5453494CUL); //List.
|
||||
write32ule(&buf[4], size() - 8);
|
||||
write32ule(&buf[8], 0x6c727473UL); //Type.
|
||||
serialization::u32l(&buf[0], 0x5453494CUL); //List.
|
||||
serialization::u32l(&buf[4], size() - 8);
|
||||
serialization::u32l(&buf[8], 0x6c727473UL); //Type.
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
throw std::runtime_error("Can't write strl");
|
||||
|
@ -128,22 +128,22 @@ void avi_header::serialize(std::ostream& out, stream_header_list<stream_format_v
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(size());
|
||||
write32ule(&buf[0], 0x68697661); //Type.
|
||||
write32ule(&buf[4], size() - 8);
|
||||
write32ule(&buf[8], microsec_per_frame);
|
||||
write32ule(&buf[12], max_bytes_per_sec);
|
||||
write32ule(&buf[16], padding_granularity);
|
||||
write32ule(&buf[20], flags);
|
||||
write32ule(&buf[24], videotrack.strh.length);
|
||||
write32ule(&buf[28], initial_frames);
|
||||
write32ule(&buf[32], tracks);
|
||||
write32ule(&buf[36], suggested_buffer_size);
|
||||
write32ule(&buf[40], videotrack.strf.width);
|
||||
write32ule(&buf[44], videotrack.strf.height);
|
||||
write32ule(&buf[48], 0);
|
||||
write32ule(&buf[52], 0);
|
||||
write32ule(&buf[56], 0);
|
||||
write32ule(&buf[60], 0);
|
||||
serialization::u32l(&buf[0], 0x68697661); //Type.
|
||||
serialization::u32l(&buf[4], size() - 8);
|
||||
serialization::u32l(&buf[8], microsec_per_frame);
|
||||
serialization::u32l(&buf[12], max_bytes_per_sec);
|
||||
serialization::u32l(&buf[16], padding_granularity);
|
||||
serialization::u32l(&buf[20], flags);
|
||||
serialization::u32l(&buf[24], videotrack.strh.length);
|
||||
serialization::u32l(&buf[28], initial_frames);
|
||||
serialization::u32l(&buf[32], tracks);
|
||||
serialization::u32l(&buf[36], suggested_buffer_size);
|
||||
serialization::u32l(&buf[40], videotrack.strf.width);
|
||||
serialization::u32l(&buf[44], videotrack.strf.height);
|
||||
serialization::u32l(&buf[48], 0);
|
||||
serialization::u32l(&buf[52], 0);
|
||||
serialization::u32l(&buf[56], 0);
|
||||
serialization::u32l(&buf[60], 0);
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
throw std::runtime_error("Can't write avih");
|
||||
|
@ -154,9 +154,9 @@ void header_list::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(12);
|
||||
write32ule(&buf[0], 0x5453494CUL); //List.
|
||||
write32ule(&buf[4], size() - 8);
|
||||
write32ule(&buf[8], 0x6c726468UL); //Type.
|
||||
serialization::u32l(&buf[0], 0x5453494CUL); //List.
|
||||
serialization::u32l(&buf[4], size() - 8);
|
||||
serialization::u32l(&buf[8], 0x6c726468UL); //Type.
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
throw std::runtime_error("Can't write hdrl");
|
||||
|
@ -180,9 +180,9 @@ void movi_chunk::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(12);
|
||||
write32ule(&buf[0], 0x5453494CUL); //List.
|
||||
write32ule(&buf[4], size() - 8);
|
||||
write32ule(&buf[8], 0x69766f6d); //Type.
|
||||
serialization::u32l(&buf[0], 0x5453494CUL); //List.
|
||||
serialization::u32l(&buf[4], size() - 8);
|
||||
serialization::u32l(&buf[8], 0x69766f6d); //Type.
|
||||
out.write(&buf[0], buf.size());
|
||||
out.seekp(payload_size, std::ios_base::cur);
|
||||
if(!out)
|
||||
|
@ -202,10 +202,10 @@ void index_entry::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(16);
|
||||
write32ule(&buf[0], chunk_type);
|
||||
write32ule(&buf[4], flags);
|
||||
write32ule(&buf[8], offset);
|
||||
write32ule(&buf[12], length);
|
||||
serialization::u32l(&buf[0], chunk_type);
|
||||
serialization::u32l(&buf[4], flags);
|
||||
serialization::u32l(&buf[8], offset);
|
||||
serialization::u32l(&buf[12], length);
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
throw std::runtime_error("Can't write index entry");
|
||||
|
@ -226,8 +226,8 @@ void idx1_chunk::serialize(std::ostream& out)
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(8);
|
||||
write32ule(&buf[0], 0x31786469UL); //Type.
|
||||
write32ule(&buf[4], size() - 8);
|
||||
serialization::u32l(&buf[0], 0x31786469UL); //Type.
|
||||
serialization::u32l(&buf[4], size() - 8);
|
||||
out.write(&buf[0], buf.size());
|
||||
if(!out)
|
||||
throw std::runtime_error("Can't write idx1");
|
||||
|
@ -246,9 +246,9 @@ void avi_file_structure::serialize()
|
|||
{
|
||||
std::vector<char> buf;
|
||||
buf.resize(12);
|
||||
write32ule(&buf[0], 0x46464952UL); //RIFF.
|
||||
write32ule(&buf[4], size() - 8);
|
||||
write32ule(&buf[8], 0x20495641UL); //Type.
|
||||
serialization::u32l(&buf[0], 0x46464952UL); //RIFF.
|
||||
serialization::u32l(&buf[4], size() - 8);
|
||||
serialization::u32l(&buf[8], 0x20495641UL); //Type.
|
||||
outstream->write(&buf[0], buf.size());
|
||||
if(!*outstream)
|
||||
throw std::runtime_error("Can't write AVI header");
|
||||
|
|
|
@ -133,7 +133,7 @@ namespace
|
|||
return;
|
||||
}
|
||||
char dummypacket[8] = {0x00, 0x03};
|
||||
write32ube(dummypacket + 2, maxtc - last_written_ts);
|
||||
serialization::u32b(dummypacket + 2, maxtc - last_written_ts);
|
||||
last_written_ts = maxtc;
|
||||
jmd->write(dummypacket, sizeof(dummypacket));
|
||||
if(!*jmd)
|
||||
|
@ -218,8 +218,8 @@ namespace
|
|||
|
||||
size_t usize = 4;
|
||||
ret.resize(4);
|
||||
write16ube(&ret[0], width);
|
||||
write16ube(&ret[2], height);
|
||||
serialization::u16b(&ret[0], width);
|
||||
serialization::u16b(&ret[2], height);
|
||||
uint8_t input_buffer[4 * INBUF_PIXELS];
|
||||
size_t ptr = 0;
|
||||
size_t pixels = static_cast<size_t>(width) * height;
|
||||
|
@ -230,7 +230,7 @@ namespace
|
|||
if(input_clear) {
|
||||
size_t pixel = ptr;
|
||||
for(unsigned i = 0; i < INBUF_PIXELS && pixel < pixels; i++, pixel++)
|
||||
write32ule(input_buffer + (4 * i), memory[pixel]);
|
||||
serialization::u32l(input_buffer + (4 * i), memory[pixel]);
|
||||
bsize = pixel - ptr;
|
||||
ptr = pixel;
|
||||
input_clear = false;
|
||||
|
@ -294,7 +294,7 @@ namespace
|
|||
{
|
||||
//Channel 0, minor 1.
|
||||
char videopacketh[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
|
||||
write32ube(videopacketh + 2, f.ts - last_written_ts);
|
||||
serialization::u32b(videopacketh + 2, f.ts - last_written_ts);
|
||||
last_written_ts = f.ts;
|
||||
unsigned lneed = 0;
|
||||
uint64_t datasize = f.data.size(); //Possibly upcast to avoid warnings.
|
||||
|
@ -316,10 +316,10 @@ namespace
|
|||
{
|
||||
//Channel 1, minor 1, payload 4.
|
||||
char soundpacket[12] = {0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04};
|
||||
write32ube(soundpacket + 2, s.ts - last_written_ts);
|
||||
serialization::u32b(soundpacket + 2, s.ts - last_written_ts);
|
||||
last_written_ts = s.ts;
|
||||
write16sbe(soundpacket + 8, s.l);
|
||||
write16sbe(soundpacket + 10, s.r);
|
||||
serialization::s16b(soundpacket + 8, s.l);
|
||||
serialization::s16b(soundpacket + 10, s.r);
|
||||
jmd->write(soundpacket, sizeof(soundpacket));
|
||||
if(!*jmd)
|
||||
throw std::runtime_error("Can't write JMD sound packet");
|
||||
|
|
|
@ -123,8 +123,8 @@ namespace
|
|||
}
|
||||
if(have_dumped_frame && audio) {
|
||||
char buffer[4];
|
||||
write16sbe(buffer + 0, l);
|
||||
write16sbe(buffer + 2, r);
|
||||
serialization::s16b(buffer + 0, l);
|
||||
serialization::s16b(buffer + 2, r);
|
||||
audio->write(buffer, 4);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace
|
|||
if(v >= 1)
|
||||
v -= 1;
|
||||
}
|
||||
write64ule(buf, v2);
|
||||
serialization::u64l(buf, v2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,9 +36,9 @@ sox_dumper::sox_dumper(const std::string& filename, double samplerate, uint32_t
|
|||
throw std::runtime_error("Can't open sox file for output");
|
||||
try {
|
||||
uint8_t buffer[32] = {0};
|
||||
write64ule(buffer, 0x1C586F532E); //Magic and header size.
|
||||
serialization::u64l(buffer, 0x1C586F532E); //Magic and header size.
|
||||
write_double(buffer + 16, samplerate);
|
||||
write32ule(buffer + 24, channels);
|
||||
serialization::u32l(buffer + 24, channels);
|
||||
sox_file.write(reinterpret_cast<char*>(buffer), 32);
|
||||
if(!sox_file)
|
||||
throw std::runtime_error("Can't write audio header");
|
||||
|
@ -64,7 +64,7 @@ void sox_dumper::close() throw(std::bad_alloc, std::runtime_error)
|
|||
sox_file.seekp(8, std::ios::beg);
|
||||
uint8_t buffer[8];
|
||||
uint64_t raw_samples = samples_dumped * samplebuffer.size();
|
||||
write64ule(buffer, raw_samples);
|
||||
serialization::u64l(buffer, raw_samples);
|
||||
sox_file.write(reinterpret_cast<char*>(buffer), 8);
|
||||
if(!sox_file)
|
||||
throw std::runtime_error("Can't fixup audio header");
|
||||
|
@ -74,7 +74,7 @@ void sox_dumper::close() throw(std::bad_alloc, std::runtime_error)
|
|||
void sox_dumper::internal_dump_sample()
|
||||
{
|
||||
for(size_t i = 0; i < samplebuffer.size(); ++i)
|
||||
write32ule(&databuf[4 * i], static_cast<uint32_t>(samplebuffer[i]));
|
||||
serialization::u32l(&databuf[4 * i], static_cast<uint32_t>(samplebuffer[i]));
|
||||
sox_file.write(&databuf[0], databuf.size());
|
||||
if(!sox_file)
|
||||
throw std::runtime_error("Failed to dump sample");
|
||||
|
|
Loading…
Add table
Reference in a new issue