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