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