1 #ifndef _MESSAGE_UTILS_H_
2 #define _MESSAGE_UTILS_H_
3
4 #include <ByteOrder.h>
5 #include <DataIO.h>
6 #include <Entry.h>
7 #include <Message.h>
8 #include <Node.h>
9 #include <SupportDefs.h>
10
11
12 namespace BPrivate { // Only putting these here because Be did
13
14 // entry_ref
15 status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref);
16 status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size);
17 status_t entry_ref_swap(char* buffer, size_t size);
18
19 // node_ref
20 status_t node_ref_flatten(char* buffer, size_t* size, const node_ref* ref);
21 status_t node_ref_unflatten(node_ref* ref, const char* buffer, size_t size);
22 status_t node_ref_swap(char* buffer, size_t size);
23
24 uint32 CalculateChecksum(const uint8 *buffer, int32 size);
25
26 } // namespace BPrivate
27
28
29 template<class T>
30 inline void
byte_swap(T &)31 byte_swap(T &/*data*/)
32 {
33 // Specialize for data types which actually swap
34 }
35
36
37 inline void
write_helper(BDataIO * stream,const void * data,size_t size)38 write_helper(BDataIO *stream, const void *data, size_t size)
39 {
40 status_t error = stream->Write(data, size);
41 if (error < B_OK)
42 throw error;
43 }
44
45
46 class TReadHelper {
47 public:
TReadHelper(BDataIO * stream)48 TReadHelper(BDataIO *stream)
49 : fStream(stream),
50 fError(B_OK),
51 fSwap(false)
52 {
53 }
54
TReadHelper(BDataIO * stream,bool swap)55 TReadHelper(BDataIO *stream, bool swap)
56 : fStream(stream),
57 fError(B_OK),
58 fSwap(swap)
59 {
60 }
61
62 template<class T>
operator()63 inline void operator()(T &data)
64 {
65 fError = fStream->Read((void *)&data, sizeof(T));
66 if (fError > B_OK) {
67 if (IsSwapping())
68 byte_swap(data);
69 return;
70 }
71
72 if (fError == 0)
73 fError = B_ERROR;
74 throw fError;
75 }
76
77 template<class T>
operator()78 inline void operator()(T data, size_t len)
79 {
80 fError = fStream->Read((void *)data, len);
81 if (fError >= B_OK)
82 return;
83
84 throw fError;
85 }
86
Status()87 status_t Status() const { return fError >= B_OK ? B_OK : fError; }
88
SetSwap(bool yesNo)89 void SetSwap(bool yesNo) { fSwap = yesNo; }
IsSwapping()90 bool IsSwapping() const { return fSwap; }
91
92 private:
93 BDataIO *fStream;
94 status_t fError;
95 bool fSwap;
96 };
97
98
99 class TChecksumHelper {
100 public:
TChecksumHelper(uchar * buffer)101 TChecksumHelper(uchar* buffer)
102 : fBuffer(buffer),
103 fBufPtr(buffer)
104 {
105 }
106
107 template<class T>
Cache(const T & data)108 inline void Cache(const T &data)
109 {
110 *((T*)fBufPtr) = data;
111 fBufPtr += sizeof (T);
112 }
113
114 int32 CheckSum();
115
116 private:
117 uchar *fBuffer;
118 uchar *fBufPtr;
119 };
120
121
122 template<>
123 inline void
byte_swap(double & data)124 byte_swap(double &data)
125 {
126 data = __swap_double(data);
127 }
128
129
130 template<>
131 inline void
byte_swap(float & data)132 byte_swap(float &data)
133 {
134 data = __swap_float(data);
135 }
136
137
138 template<>
139 inline void
byte_swap(int64 & data)140 byte_swap(int64 &data)
141 {
142 data = __swap_int64(data);
143 }
144
145
146 template<>
147 inline void
byte_swap(uint64 & data)148 byte_swap(uint64 &data)
149 {
150 data = __swap_int64(data);
151 }
152
153
154 template<>
155 inline void
byte_swap(int32 & data)156 byte_swap(int32 &data)
157 {
158 data = __swap_int32(data);
159 }
160
161
162 template<>
163 inline void
byte_swap(uint32 & data)164 byte_swap(uint32 &data)
165 {
166 data = __swap_int32(data);
167 }
168
169
170 template<>
171 inline void
byte_swap(int16 & data)172 byte_swap(int16 &data)
173 {
174 data = __swap_int16(data);
175 }
176
177
178 template<>
179 inline void
byte_swap(uint16 & data)180 byte_swap(uint16 &data)
181 {
182 data = __swap_int16(data);
183 }
184
185
186 template<>
187 inline void
byte_swap(entry_ref & data)188 byte_swap(entry_ref &data)
189 {
190 byte_swap(data.device);
191 byte_swap(data.directory);
192 }
193
194 #endif // _MESSAGE_UTILS_H_
195