Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unaligned read in datasource.cc #151

Open
m-7761 opened this issue Dec 24, 2020 · 0 comments
Open

Unaligned read in datasource.cc #151

m-7761 opened this issue Dec 24, 2020 · 0 comments

Comments

@m-7761
Copy link

m-7761 commented Dec 24, 2020

I noticed the read family of methods is dereferencing unaligned pointers (the MM3D format is completely unaligned unfortunately) in which case I don't think it will work on architectures like ARM that require alignment.

I was just removing endianconfig.h to put in a simpler system in its place, since none of the loaders are using "swapEndianness" and the setup was sloppy. Below is the new code I wrote in case useful.

template<class T>
inline bool DataSource::_read(T &val)
{
	if(!requireBytes(sizeof(val))) return false;

	//This is an unaligned read, which doesn't 
	//work for ARM systems.
	//m_endfunc16(*(uint16_t*)m_buf);
	memcpy(&val,m_buf,sizeof(T));

	if(m_swap) swapEndianness(val);

	advanceBytes(sizeof(val)); return true;
}
bool DataSource::read(int8_t &val){ return _read(val); }
bool DataSource::read(uint8_t &val){ return _read(val); }
bool DataSource::read(int16_t &val){ return _read(val); }
bool DataSource::read(uint16_t &val){ return _read(val); }
bool DataSource::read(int32_t &val){ return _read(val); }
bool DataSource::read(uint32_t &val){ return _read(val); }
bool DataSource::read(float &val){ return _read(val); }
template<class T>
inline bool DataDest::_write(T &val)
{
	if(!canWrite(sizeof(T))) return false;

	if(m_swap) swapEndianness(val);

	return internalWrite((const uint8_t*)&val,sizeof(T));
}
bool DataDest::write(int8_t val){ return _write(val); }
bool DataDest::write(uint8_t val){ return _write(val); }
bool DataDest::write(int16_t val){ return _write(val); }
bool DataDest::write(uint16_t val){ return _write(val); }
bool DataDest::write(int32_t val){ return _write(val); }
bool DataDest::write(uint32_t val){ return _write(val); }
bool DataDest::write(float val){ return _write(val); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant