xref: /haiku/src/kits/app/MessageUtils.cpp (revision 76441bab6ebb9132d0bb0ab623087bd2b9e861e0)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		MessageUtils.cpp
23 //	Author(s):		Erik Jaesler <erik@cgsoftware.com>
24 //
25 //	Description:	Extra messaging utility functions
26 //
27 //
28 //------------------------------------------------------------------------------
29 // Standard Includes -----------------------------------------------------------
30 #include <string.h>
31 
32 // System Includes -------------------------------------------------------------
33 #include <ByteOrder.h>
34 
35 // Project Includes ------------------------------------------------------------
36 #include <MessageUtils.h>
37 
38 // Local Includes --------------------------------------------------------------
39 
40 // Local Defines ---------------------------------------------------------------
41 
42 // Globals ---------------------------------------------------------------------
43 
44 //------------------------------------------------------------------------------
45 uint32 _checksum_(const uchar* buf, int32 size)
46 {
47 	uint32 sum = 0;
48 	uint32 temp = 0;
49 
50 	while (size > 3) {
51 #if defined(__INTEL__)
52 		sum += B_SWAP_INT32(*(int*)buf);
53 #else
54 		sum += *(int*)buf;
55 #endif
56 
57 		buf += 4;
58 		size -= 4;
59 	}
60 
61 	while (size > 0) {
62 		temp = (temp << 8) + *buf++;
63 		size -= 1;
64 		sum += temp;
65 	}
66 
67 	return sum;
68 }
69 //------------------------------------------------------------------------------
70 
71 
72 namespace BPrivate {	// Only putting these here because Be did
73 //------------------------------------------------------------------------------
74 status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref)
75 {
76 	memcpy((void*)buffer, (const void*)&ref->device, sizeof (ref->device));
77 	buffer += sizeof (ref->device);
78 	memcpy((void*)buffer, (const void*)&ref->directory, sizeof (ref->directory));
79 	buffer += sizeof (ref->directory);
80 
81 	size_t len = 0;
82 	if (ref->name)
83 	{
84 		len = strlen(ref->name) + 1;	// extra for NULL terminator
85 		memcpy((void*)buffer, (const void*)ref->name, len);
86 	}
87 
88 	*size = sizeof (ref->device) + sizeof (ref->directory) + len;
89 	return B_OK;
90 }
91 //------------------------------------------------------------------------------
92 status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size)
93 {
94 	if (size < (sizeof (ref->device) + sizeof (ref->directory)))
95 	{
96 		*ref = entry_ref();
97 		return B_BAD_VALUE;
98 	}
99 
100 	memcpy((void*)&ref->device, (const void*)buffer, sizeof (ref->device));
101 	buffer += sizeof (ref->device);
102 	memcpy((void*)&ref->directory, (const void*)buffer,
103 		   sizeof (ref->directory));
104 	buffer += sizeof (ref->directory);
105 
106 	if (ref->device != -1 &&
107 		size > (sizeof (ref->device) + sizeof (ref->directory)))
108 	{
109 		ref->set_name(buffer);
110 		if (ref->name == NULL)
111 		{
112 			*ref = entry_ref();
113 			return B_NO_MEMORY;
114 		}
115 	}
116 	else
117 	{
118 		ref->set_name(NULL);
119 	}
120 
121 	return B_OK;
122 }
123 //------------------------------------------------------------------------------
124 status_t entry_ref_swap(char* buffer, size_t size)
125 {
126 	if (size < (sizeof (dev_t) + sizeof (ino_t)))
127 	{
128 		return B_BAD_DATA;
129 	}
130 
131 	dev_t* dev = (dev_t*)buffer;
132 	*dev = B_SWAP_INT32(*dev);
133 	buffer += sizeof (dev_t);
134 
135 	ino_t* ino = (ino_t*)buffer;
136 	*ino = B_SWAP_INT64(*ino);
137 
138 	return B_OK;
139 }
140 //------------------------------------------------------------------------------
141 
142 }	// namespace BPrivate
143 
144 //------------------------------------------------------------------------------
145 int32 TChecksumHelper::CheckSum()
146 {
147 	 return _checksum_(fBuffer, fBufPtr - fBuffer);
148 }
149 //------------------------------------------------------------------------------
150 
151 /*
152  * $Log $
153  *
154  * $Id  $
155  *
156  */
157 
158