xref: /haiku/src/add-ons/kernel/file_systems/nfs/XDRInPacket.c (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
1 #include "XDRInPacket.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 #include "nfs.h"
12 
13 extern void
14 XDRInPacketInit(struct XDRInPacket *packet)
15 {
16 	packet->fBuffer=NULL;
17 	packet->fOffset=0;
18 }
19 
20 extern void
21 XDRInPacketDestroy(struct XDRInPacket *packet)
22 {
23 	free (packet->fBuffer);
24 }
25 
26 extern int32
27 XDRInPacketGetInt32(struct XDRInPacket *packet)
28 {
29 	int32 val=B_BENDIAN_TO_HOST_INT32(*((int32 *)&packet->fBuffer[packet->fOffset]));
30 
31 	packet->fOffset+=4;
32 
33 	return val;
34 }
35 
36 
37 status_t
38 XDRInPacketGetFixed(struct XDRInPacket *packet, void *buffer, size_t len)
39 {
40 	if (!IS_USER_ADDRESS(buffer))
41 		memcpy(buffer, &packet->fBuffer[packet->fOffset], len);
42 	else if (user_memcpy(buffer, &packet->fBuffer[packet->fOffset], len) != B_OK)
43 		return B_BAD_ADDRESS;
44 
45 	packet->fOffset += (len + 3) & ~3;
46 	return B_OK;
47 }
48 
49 
50 status_t
51 XDRInPacketGetDynamic(struct XDRInPacket *packet, void *buffer, size_t *len)
52 {
53 	*len = XDRInPacketGetInt32(packet);
54 	return XDRInPacketGetFixed(packet, buffer, *len);
55 }
56 
57 
58 extern char *
59 XDRInPacketGetString(struct XDRInPacket *packet)
60 {
61 	int32 size=XDRInPacketGetInt32(packet);
62 	char *string=(char *)malloc(size+1);
63 	string[size]=0;
64 	XDRInPacketGetFixed(packet,string,size);
65 
66 	return string;
67 }
68 
69 extern void
70 XDRInPacketSetTo(struct XDRInPacket *packet, uint8 *buffer, size_t offset)
71 {
72 	free (packet->fBuffer);
73 	packet->fBuffer=buffer;
74 	packet->fOffset=offset;
75 }
76