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 12namespace BPrivate { // Only putting these here because Be did 13 14// entry_ref 15status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref); 16status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size); 17status_t entry_ref_swap(char* buffer, size_t size); 18 19// node_ref 20status_t node_ref_flatten(char* buffer, size_t* size, const node_ref* ref); 21status_t node_ref_unflatten(node_ref* ref, const char* buffer, size_t size); 22status_t node_ref_swap(char* buffer, size_t size); 23 24uint32 CalculateChecksum(const uint8 *buffer, int32 size); 25 26} // namespace BPrivate 27 28 29template<class T> 30inline void 31byte_swap(T &/*data*/) 32{ 33 // Specialize for data types which actually swap 34} 35 36 37inline void 38write_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 46class TReadHelper { 47public: 48 TReadHelper(BDataIO *stream) 49 : fStream(stream), 50 fError(B_OK), 51 fSwap(false) 52 { 53 } 54 55 TReadHelper(BDataIO *stream, bool swap) 56 : fStream(stream), 57 fError(B_OK), 58 fSwap(swap) 59 { 60 } 61 62 template<class T> 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> 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 87 status_t Status() const { return fError >= B_OK ? B_OK : fError; } 88 89 void SetSwap(bool yesNo) { fSwap = yesNo; } 90 bool IsSwapping() const { return fSwap; } 91 92private: 93 BDataIO *fStream; 94 status_t fError; 95 bool fSwap; 96}; 97 98 99class TChecksumHelper { 100public: 101 TChecksumHelper(uchar* buffer) 102 : fBuffer(buffer), 103 fBufPtr(buffer) 104 { 105 } 106 107 template<class T> 108 inline void Cache(const T &data) 109 { 110 *((T*)fBufPtr) = data; 111 fBufPtr += sizeof (T); 112 } 113 114 int32 CheckSum(); 115 116private: 117 uchar *fBuffer; 118 uchar *fBufPtr; 119}; 120 121 122template<> 123inline void 124byte_swap(double &data) 125{ 126 data = __swap_double(data); 127} 128 129 130template<> 131inline void 132byte_swap(float &data) 133{ 134 data = __swap_float(data); 135} 136 137 138template<> 139inline void 140byte_swap(int64 &data) 141{ 142 data = __swap_int64(data); 143} 144 145 146template<> 147inline void 148byte_swap(uint64 &data) 149{ 150 data = __swap_int64(data); 151} 152 153 154template<> 155inline void 156byte_swap(int32 &data) 157{ 158 data = __swap_int32(data); 159} 160 161 162template<> 163inline void 164byte_swap(uint32 &data) 165{ 166 data = __swap_int32(data); 167} 168 169 170template<> 171inline void 172byte_swap(int16 &data) 173{ 174 data = __swap_int16(data); 175} 176 177 178template<> 179inline void 180byte_swap(uint16 &data) 181{ 182 data = __swap_int16(data); 183} 184 185 186template<> 187inline void 188byte_swap(entry_ref &data) 189{ 190 byte_swap(data.device); 191 byte_swap(data.directory); 192} 193 194#endif // _MESSAGE_UTILS_H_ 195