xref: /haiku/src/add-ons/kernel/file_systems/nfs/XDROutPacket.c (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 #include "XDROutPacket.h"
2 
3 #include <malloc.h>
4 #include <string.h>
5 #include <ByteOrder.h>
6 
7 extern const uint8 *
8 XDROutPacketBuffer(struct XDROutPacket *packet)
9 {
10 	return packet->fBuffer;
11 }
12 
13 extern size_t
14 XDROutPacketLength(struct XDROutPacket *packet)
15 {
16 	return packet->fLength;
17 }
18 
19 extern void
20 XDROutPacketInit(struct XDROutPacket *packet)
21 {
22 	packet->fBuffer=NULL;
23 	packet->fSize=packet->fLength=0;
24 }
25 
26 extern void
27 XDROutPacketDestroy(struct XDROutPacket *packet)
28 {
29 	free (packet->fBuffer);
30 }
31 
32 extern void
33 XDROutPacketGrow(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 
44 extern void
45 XDROutPacketAddInt32(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 
52 extern void
53 XDROutPacketAddDynamic(struct XDROutPacket *packet, const void *data, size_t size)
54 {
55 	XDROutPacketAddInt32 (packet,size);
56 	XDROutPacketAddFixed (packet,data,size);
57 }
58 
59 extern void
60 XDROutPacketAddFixed(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 
69 extern void
70 XDROutPacketAddString(struct XDROutPacket *packet, const char *string)
71 {
72 	XDROutPacketAddDynamic(packet,string,strlen(string));
73 }
74 
75 extern void
76 XDROutPacketAppend(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 
83 extern void
84 XDROutPacketClear(struct XDROutPacket *packet)
85 {
86 	packet->fLength=0;
87 }
88 
89