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