xref: /haiku/src/kits/app/MessageUtils.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 //------------------------------------------------------------------------------
2 //	MessageUtils.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <string.h>
8 
9 // System Includes -------------------------------------------------------------
10 #include <ByteOrder.h>
11 
12 // Project Includes ------------------------------------------------------------
13 #include <MessageUtils.h>
14 
15 // Local Includes --------------------------------------------------------------
16 
17 // Local Defines ---------------------------------------------------------------
18 
19 // Globals ---------------------------------------------------------------------
20 
21 //------------------------------------------------------------------------------
22 uint32 _checksum_(const uchar* buf, int32 size)
23 {
24 	uint32 sum = 0;
25 	uint32 temp = 0;
26 
27 	while (size > 3) {
28 #if defined(__INTEL__)
29 		sum += B_SWAP_INT32(*(int*)buf);
30 #else
31 		sum += *(int*)buf;
32 #endif
33 
34 		buf += 4;
35 		size -= 4;
36 	}
37 
38 	while (size > 0) {
39 		temp = (temp << 8) + *buf++;
40 		size -= 1;
41 		sum += temp;
42 	}
43 
44 	return sum;
45 }
46 //------------------------------------------------------------------------------
47 
48 
49 namespace BPrivate {	// Only putting these here because Be did
50 //------------------------------------------------------------------------------
51 status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref)
52 {
53 	memcpy((void*)buffer, (const void*)&ref->device, sizeof (ref->device));
54 	buffer += sizeof (ref->device);
55 	memcpy((void*)buffer, (const void*)&ref->directory, sizeof (ref->directory));
56 	buffer += sizeof (ref->directory);
57 
58 	size_t len = 0;
59 	if (ref->name)
60 	{
61 		len = strlen(ref->name) + 1;	// extra for NULL terminator
62 		memcpy((void*)buffer, (const void*)ref->name, len);
63 	}
64 
65 	*size = sizeof (ref->device) + sizeof (ref->directory) + len;
66 	return B_OK;
67 }
68 //------------------------------------------------------------------------------
69 status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size)
70 {
71 	if (size < (sizeof (ref->device) + sizeof (ref->directory)))
72 	{
73 		*ref = entry_ref();
74 		return B_BAD_VALUE;
75 	}
76 
77 	memcpy((void*)&ref->device, (const void*)buffer, sizeof (ref->device));
78 	buffer += sizeof (ref->device);
79 	memcpy((void*)&ref->directory, (const void*)buffer,
80 		   sizeof (ref->directory));
81 	buffer += sizeof (ref->directory);
82 
83 	if (ref->device != -1 &&
84 		size > (sizeof (ref->device) + sizeof (ref->directory)))
85 	{
86 		ref->set_name(buffer);
87 		if (ref->name == NULL)
88 		{
89 			*ref = entry_ref();
90 			return B_NO_MEMORY;
91 		}
92 	}
93 	else
94 	{
95 		ref->set_name(NULL);
96 	}
97 
98 	return B_OK;
99 }
100 //------------------------------------------------------------------------------
101 status_t entry_ref_swap(char* buffer, size_t size)
102 {
103 	if (size < (sizeof (dev_t) + sizeof (ino_t)))
104 	{
105 		return B_BAD_DATA;
106 	}
107 
108 	dev_t* dev = (dev_t*)buffer;
109 	*dev = B_SWAP_INT32(*dev);
110 	buffer += sizeof (dev_t);
111 
112 	ino_t* ino = (ino_t*)buffer;
113 	*ino = B_SWAP_INT64(*ino);
114 
115 	return B_OK;
116 }
117 //------------------------------------------------------------------------------
118 
119 }	// namespace BPrivate
120 
121 //------------------------------------------------------------------------------
122 int32 TChecksumHelper::CheckSum()
123 {
124 	 return _checksum_(fBuffer, fBufPtr - fBuffer);
125 }
126 //------------------------------------------------------------------------------
127 
128 /*
129  * $Log $
130  *
131  * $Id  $
132  *
133  */
134 
135