XDROutPacket.c revision 4b05e555
1#include "XDROutPacket.h"
2
3#include <malloc.h>
4#include <string.h>
5#include <ByteOrder.h>
6
7extern const uint8 *
8XDROutPacketBuffer(struct XDROutPacket *packet)
9{
10	return packet->fBuffer;
11}
12
13extern size_t
14XDROutPacketLength(struct XDROutPacket *packet)
15{
16	return packet->fLength;
17}
18
19extern void
20XDROutPacketInit(struct XDROutPacket *packet)
21{
22	packet->fBuffer=NULL;
23	packet->fSize=packet->fLength=0;
24}
25
26extern void
27XDROutPacketDestroy(struct XDROutPacket *packet)
28{
29	free (packet->fBuffer);
30}
31
32extern void
33XDROutPacketGrow(struct XDROutPacket *packet, size_t size)
34{
35	if (packet->fLength+size>packet->fSize)
36	{
37		while (packet->fLength+size>packet->fSize)
38			packet->fSize+=XDROUTPACKET_BUFFER_INCREMENT;
39
40		packet->fBuffer=(uint8 *)realloc(packet->fBuffer,packet->fSize);
41	}
42}
43
44extern void
45XDROutPacketAddInt32(struct XDROutPacket *packet, int32 val)
46{
47	XDROutPacketGrow (packet,4);
48	*(int32 *)(&packet->fBuffer[packet->fLength])=B_HOST_TO_BENDIAN_INT32(val);
49	packet->fLength+=4;
50}
51
52extern void
53XDROutPacketAddDynamic(struct XDROutPacket *packet, const void *data, size_t size)
54{
55	XDROutPacketAddInt32 (packet,size);
56	XDROutPacketAddFixed (packet,data,size);
57}
58
59extern void
60XDROutPacketAddFixed(struct XDROutPacket *packet, const void *data, size_t size)
61{
62	size_t roundedSize=(size+3)&~3;
63	XDROutPacketGrow (packet,roundedSize);
64	memcpy (&packet->fBuffer[packet->fLength],data,size);
65	memset (&packet->fBuffer[packet->fLength+size],0,roundedSize-size);
66	packet->fLength+=roundedSize;
67}
68
69extern void
70XDROutPacketAddString(struct XDROutPacket *packet, const char *string)
71{
72	XDROutPacketAddDynamic(packet,string,strlen(string));
73}
74
75extern void
76XDROutPacketAppend(struct XDROutPacket *me, const struct XDROutPacket *packet)
77{
78	XDROutPacketGrow (me,packet->fLength);
79	memcpy (&me->fBuffer[me->fLength],packet->fBuffer,packet->fLength);
80	me->fLength+=packet->fLength;
81}
82
83extern void
84XDROutPacketClear(struct XDROutPacket *packet)
85{
86	packet->fLength=0;
87}
88
89