1 /* 2 * Copyright 2001-2005, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Erik Jaesler (erik@cgsoftware.com) 7 */ 8 9 /** Extra messaging utility functions */ 10 11 #include <string.h> 12 #include <ByteOrder.h> 13 14 #include <MessageUtils.h> 15 16 namespace BPrivate { 17 18 uint32 19 CalculateChecksum(const uint8 *buffer, int32 size) 20 { 21 uint32 sum = 0; 22 uint32 temp = 0; 23 24 while (size > 3) { 25 #if defined(__INTEL__) 26 sum += B_SWAP_INT32(*(int32 *)buffer); 27 #else 28 sum += *(int32 *)buffer; 29 #endif 30 buffer += 4; 31 size -= 4; 32 } 33 34 while (size > 0) { 35 temp = (temp << 8) + *buffer++; 36 size -= 1; 37 } 38 39 return sum + temp; 40 } 41 42 43 /* entry_ref support functions */ 44 status_t 45 entry_ref_flatten(char *buffer, size_t *size, const entry_ref *ref) 46 { 47 memcpy((void *)buffer, (const void *)&ref->device, sizeof(ref->device)); 48 buffer += sizeof(ref->device); 49 memcpy((void *)buffer, (const void *)&ref->directory, sizeof(ref->directory)); 50 buffer += sizeof (ref->directory); 51 52 size_t nameLength = 0; 53 if (ref->name) { 54 nameLength = strlen(ref->name) + 1; 55 memcpy((void *)buffer, (const void *)ref->name, nameLength); 56 } 57 58 *size = sizeof(ref->device) + sizeof(ref->directory) + nameLength; 59 return B_OK; 60 } 61 62 63 status_t 64 entry_ref_unflatten(entry_ref *ref, const char *buffer, size_t size) 65 { 66 if (size < sizeof(ref->device) + sizeof(ref->directory)) { 67 *ref = entry_ref(); 68 return B_BAD_VALUE; 69 } 70 71 memcpy((void *)&ref->device, (const void *)buffer, sizeof(ref->device)); 72 buffer += sizeof (ref->device); 73 memcpy((void *)&ref->directory, (const void *)buffer, sizeof(ref->directory)); 74 buffer += sizeof(ref->directory); 75 76 if (ref->device != -1 && size > sizeof(ref->device) 77 + sizeof(ref->directory)) { 78 ref->set_name(buffer); 79 if (ref->name == NULL) { 80 *ref = entry_ref(); 81 return B_NO_MEMORY; 82 } 83 } else 84 ref->set_name(NULL); 85 86 return B_OK; 87 } 88 89 90 status_t 91 entry_ref_swap(char *buffer, size_t size) 92 { 93 if (size < sizeof(dev_t) + sizeof(ino_t)) 94 return B_BAD_VALUE; 95 96 dev_t *dev = (dev_t *)buffer; 97 *dev = B_SWAP_INT32(*dev); 98 buffer += sizeof(dev_t); 99 100 ino_t *ino = (ino_t *)buffer; 101 *ino = B_SWAP_INT64(*ino); 102 103 return B_OK; 104 } 105 106 } // namespace BPrivate 107