2004-10-14 05:23:00 +04:00
|
|
|
#include "RAMLinkMsgReader.h"
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
RAMLinkMsgReader::RAMLinkMsgReader(int8 *buffer)
|
2005-06-15 01:28:56 +04:00
|
|
|
: BPrivate::LinkReceiver(-1)
|
2004-10-14 05:23:00 +04:00
|
|
|
{
|
|
|
|
SetBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RAMLinkMsgReader::RAMLinkMsgReader(void)
|
2005-06-15 01:28:56 +04:00
|
|
|
: BPrivate::LinkReceiver(-1)
|
2004-10-14 05:23:00 +04:00
|
|
|
{
|
2005-06-15 01:28:56 +04:00
|
|
|
SetBuffer(NULL);
|
2004-10-14 05:23:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RAMLinkMsgReader::~RAMLinkMsgReader(void)
|
|
|
|
{
|
|
|
|
// Note that this class does NOT take responsibility for the buffer it reads from.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-08 08:01:59 +04:00
|
|
|
void
|
|
|
|
RAMLinkMsgReader::SetBuffer(int8 *buffer)
|
2004-10-14 05:23:00 +04:00
|
|
|
{
|
2005-06-15 01:28:56 +04:00
|
|
|
if (!buffer) {
|
|
|
|
fBuffer = NULL;
|
|
|
|
fAttachStart = NULL;
|
|
|
|
fPosition = NULL;
|
|
|
|
fAttachSize = 0;
|
|
|
|
fCode = B_ERROR;
|
2004-10-14 05:23:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2005-06-15 01:28:56 +04:00
|
|
|
|
|
|
|
fBuffer = buffer;
|
|
|
|
fPosition = fBuffer + 4;
|
|
|
|
|
|
|
|
fCode = *((int32*)fBuffer);
|
|
|
|
fAttachSize = *((size_t *)fPosition);
|
|
|
|
fPosition += sizeof(size_t);
|
|
|
|
fAttachStart = fPosition;
|
2004-10-14 05:23:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-08 08:01:59 +04:00
|
|
|
int8 *
|
|
|
|
RAMLinkMsgReader::GetBuffer(void)
|
2004-10-14 05:23:00 +04:00
|
|
|
{
|
|
|
|
return fBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-08 08:01:59 +04:00
|
|
|
size_t
|
|
|
|
RAMLinkMsgReader::GetBufferSize(void)
|
2004-10-14 05:23:00 +04:00
|
|
|
{
|
|
|
|
return fAttachSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-08 08:01:59 +04:00
|
|
|
status_t
|
|
|
|
RAMLinkMsgReader::Read(void *data, ssize_t size)
|
2004-10-14 05:23:00 +04:00
|
|
|
{
|
2005-06-08 08:01:59 +04:00
|
|
|
if (!fBuffer || fAttachSize == 0)
|
2004-10-14 05:23:00 +04:00
|
|
|
return B_NO_INIT;
|
2005-06-08 08:01:59 +04:00
|
|
|
|
|
|
|
if (size < 1)
|
2004-10-14 05:23:00 +04:00
|
|
|
return B_BAD_VALUE;
|
2005-06-08 08:01:59 +04:00
|
|
|
|
|
|
|
if (fPosition + size > fAttachStart + fAttachSize) {
|
2004-10-14 05:23:00 +04:00
|
|
|
// read past end of buffer
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
}
|
2005-06-08 08:01:59 +04:00
|
|
|
|
2004-10-14 05:23:00 +04:00
|
|
|
memcpy(data, fPosition, size);
|
2005-06-08 08:01:59 +04:00
|
|
|
fPosition += size;
|
2004-10-14 05:23:00 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|