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