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