// reads a little endian 64bit double from data double ReadNumber(const char *data) { #if __FLOAT_WORD_ORDER == __BYTE_ORDER #if __BYTE_ORDER == __BIG_ENDIAN double dVal; memcpy(&dVal, data, 8); return dVal; #elif __BYTE_ORDER == __LITTLE_ENDIAN uint64_t in = *((uint64_t*)data); uint64_t res = __bswap_64(in); return *((double *)&res); #endif #else #if __BYTE_ORDER == __LITTLE_ENDIAN // __FLOAT_WORD_ORER == __BIG_ENDIAN uint32_t in1 = *((uint32_t*)data); uint32_t in2 = *((uint32_t*)(data+4)); in1 = __bswap_32(in1); in2 = __bswap_32(in2); uint64_t res = ((uint64_t)in2<<32) | (uint64_t)in1; return *((double *)&res); #else // __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN uint32_t in1 = *((uint32_t*)data); uint32_t in2 = *((uint32_t*)(data+4)); uint64_t res = ((uint64_t)in1<<32) | (uint64_t)in2; return *((double *)&res); #endif #endif }
// write dVal as 64bit little endian double void WriteNumber(char *data, double dVal) { uint64_t res; #if __FLOAT_WORD_ORDER == __BYTE_ORDER #if __BYTE_ORDER == __BIG_ENDIAN res = *((uint64_t*)&dVal); #elif __BYTE_ORDER == __LITTLE_ENDIAN uint64_t in = *((uint64_t*)&dVal); res = __bswap_64(in); #endif #else #if __BYTE_ORDER == __LITTLE_ENDIAN // __FLOAT_WORD_ORER == __BIG_ENDIAN uint32_t in1 = *((uint32_t*)&dVal); uint32_t in2 = *((uint32_t*)((char *)&dVal+4)); in1 = __bswap_32(in1); in2 = __bswap_32(in2); res = ((uint64_t)in2<<32) | (uint64_t)in1; #else // __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN uint32_t in1 = *((uint32_t*)&dVal); uint32_t in2 = *((uint32_t*)((char*)&dVal+4)); res = ((uint64_t)in1<<32) | (uint64_t)in2; #endif #endif memcpy(data, &res, 8); }
inline uint64_t bswap(uint64_t x) { union { unsigned long long int __ll; \ unsigned int __l[2]; } __w, __r; \ { \ __w.__ll = (x); \ __r.__l[0] = __bswap_32 (__w.__l[1]); \ __r.__l[1] = __bswap_32 (__w.__l[0]); \ } \ (x) = __r.__ll; return x; }
vk_hdr* read_vk(vk_hdr *vk, struct hive *h, int offset ) { memcpy(vk, h->base+offset+4, sizeof(vk_hdr)); vk->value_name = (h->base+offset+4+20); #if BYTE_ORDER == LITTLE_ENDIAN #elif BYTE_ORDER == BIG_ENDIAN vk->id = __bswap_16(vk->id); vk->name_len = __bswap_16(vk->name_len); vk->flag = __bswap_16(vk->flag); vk->data_len = __bswap_32(vk->data_len); vk->data_off = __bswap_32(vk->data_off); vk->data_type = __bswap_32(vk->data_type); #endif return vk; }
void ipbus_encode_transaction(CircularBuffer* into, const ipbus_transaction_t* transaction, int swapbytes) { uint32_t headerword = ipbus_transaction_header( 2, transaction->id, transaction->words, transaction->type, transaction->info); if (swapbytes) { headerword = __bswap_32(headerword); } cbuffer_push_back_net(into, headerword); for (size_t i = 0; i < transaction->data.size; ++i) { uint32_t datum = transaction->data.words[i]; if (swapbytes) { datum = __bswap_32(datum); } cbuffer_push_back_net(into, datum); } }
CanPacket::CanPacket ( UInt32 id , CBuffer * out ) //# OUT DATA { //...PACK THE PACKET char localData[BUFF_SIZE_L]; int size = out->bytesleft(); out->StreamRead ( &localData , size ); out->clear(); // - Bus Header UInt64 wrBUS_ID = 0x0054726974697560, virtualUId = 0x000000000000000D, wrFullID = 0x0054726974697560 & 0x000000000000000D; wrFullID = htonll(wrFullID); out->StreamWrite ( &wrFullID , sizeof( UInt64 ) ); // - Client Identification UInt64 wrClient_ID = htonll(0xAABBCCDDEEFF0000); out->StreamWrite ( &wrClient_ID , sizeof( UInt64 ) ) ; // - Packet identification UInt32 wrHexId = id; out->writeuint ( __bswap_32 ( wrHexId ) ); // - Fields ( FLags then length ) out->writebyte( flags ); //...Write : Buffer out->writebyte ( (char) min(size,255) ); out->StreamWrite ( &localData , size ); }
int _RegOpenHive( unsigned char *filename, struct hive *h ) { FILE *hiveh; unsigned long hsize; /* Prova ad aprire l'hive */ if( ( hiveh = fopen((char *) filename, "rb" ) ) != NULL ) { if( fseek( hiveh, 0, SEEK_END ) == 0 ) { hsize = ftell( hiveh ); /* Legge il file in memoria */ /* MMF ??? -_- */ h->base = (unsigned char *) malloc( hsize ); fseek( hiveh, 0, SEEK_SET ); if( fread( (void *) h->base, hsize, 1, hiveh ) == 1 ){ #if BYTE_ORDER == LITTLE_ENDIAN if( *((int*)h->base) == 0x66676572 ) { fclose( hiveh ); return 0; } #elif BYTE_ORDER == BIG_ENDIAN if( *((int*)h->base) == __bswap_32( 0x66676572)) { fclose( hiveh ); return 0; } #endif } } fclose( hiveh ); } return -1; }
static int encode_snappy(avro_codec_t c, void * data, int64_t len) { uint32_t crc; size_t outlen = snappy_max_compressed_length(len); if (!c->block_data) { c->block_data = avro_malloc(outlen+4); c->block_size = outlen+4; } else if (c->block_size < (int64_t) (outlen+4)) { c->block_data = avro_realloc(c->block_data, c->block_size, (outlen+4)); c->block_size = outlen+4; } if (!c->block_data) { avro_set_error("Cannot allocate memory for snappy"); return 1; } if (snappy_compress(data, len, c->block_data, &outlen) != SNAPPY_OK) { avro_set_error("Error compressing block with Snappy"); return 1; } crc = __bswap_32(crc32(0, data, len)); memcpy(c->block_data+outlen, &crc, 4); c->used_size = outlen+4; return 0; }
static char* test_ipbus_detect_packet_header() { uint32_t aheader = ipbus_packet_header(0xBEEF, 0); mu_assert_eq("hdr", ipbus_detect_packet_header(aheader), IPBUS_ISTREAM_PACKET); mu_assert_eq("hdr swapped", ipbus_detect_packet_header(__bswap_32(aheader)), IPBUS_ISTREAM_PACKET_SWP_ORD); mu_assert_eq("not a hdr", ipbus_detect_packet_header(0xDEADBEEF), 0); mu_assert_eq("a transaction", ipbus_detect_packet_header(ipbus_transaction_header(2, 0xEEF, IPBUS_READ, 2, 0xf)), 0); return 0; }
int getFilenameSize(char recvline[]){ char buffer[4]; for(int i=0; i<4; i++){ buffer[i]=recvline[i]; } uint32_t num = *(uint32_t *)&buffer; return __bswap_32(num); }
hashrecord* read_hr(hashrecord *hr, unsigned char *pos, int index ) { pos+=(8*index); memcpy(hr, pos, sizeof(hashrecord)); #if BYTE_ORDER == LITTLE_ENDIAN #elif BYTE_ORDER == BIG_ENDIAN hr->nk_offset = __bswap_32(hr->nk_offset); #endif return hr; }
int* read_valuelist(int *value, struct hive *h, int offset, int size ){ memcpy(value, h->base+offset+4, size*sizeof(int)); #if BYTE_ORDER == LITTLE_ENDIAN #elif BYTE_ORDER == BIG_ENDIAN int i; for (i=0; i<size; i++) value[i] = __bswap_32(value[i]); #endif return value; }
uint32_t little_endian_chg32(uint32_t n) { #if BYTE_ORDER == BIG_ENDIAN return __bswap_32(n); #elif BYTE_ORDER == LITTLE_ENDIAN return n; #else # error "What kind of system is this?" #endif }
// write little-endian 32bit integer int EncodeInt32LE(char *output, int nVal) { #if __BYTE_ORDER == __BIG_ENDIAN nVal = __bswap_32(nVal); #endif memcpy(output, &nVal, sizeof(int)); return sizeof(int); }
uint32_t htonl (uint32_t x) { #if BYTE_ORDER == BIG_ENDIAN return x; #elif BYTE_ORDER == LITTLE_ENDIAN return __bswap_32 (x); #else # error "What kind of system is this?" #endif }
// read little-endian 32bit integer int ReadInt32LE(const char *data) { int val; memcpy(&val, data, sizeof(int)); #if __BYTE_ORDER == __BIG_ENDIAN val = __bswap_32(val); #endif return val; }
double amf::AMF_DecodeNumber(const char *data) { #if __FLOAT_WORD_ORDER == __BYTE_ORDER #if __BYTE_ORDER == __BIG_ENDIAN double dVal; memcpy(&dVal, data, 8); return dVal; #elif __BYTE_ORDER == __LITTLE_ENDIAN double dVal; unsigned char *ci, *co; ci = (unsigned char *) data; co = (unsigned char *) &dVal; co[0] = ci[7]; co[1] = ci[6]; co[2] = ci[5]; co[3] = ci[4]; co[4] = ci[3]; co[5] = ci[2]; co[6] = ci[1]; co[7] = ci[0]; return dVal; #endif #else #if __BYTE_ORDER == __LITTLE_ENDIAN // __FLOAT_WORD_ORER == __BIG_ENDIAN unsigned long int in1 = *((unsigned long int *) data); unsigned long int in2 = *((unsigned long int *) (data + 4)); in1 = __bswap_32(in1); in2 = __bswap_32(in2); unsigned long long int res = ((unsigned long long int) in2 << 32) | (unsigned long long int) in1; return *((double *) &res); #else // __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN unsigned long int in1 = *((unsigned long int *) data); unsigned long int in2 = *((unsigned long int *) (data + 4)); unsigned long long int res = ((unsigned long long int) in1 << 32) | (unsigned long long int) in2; return *((double *) &res); #endif #endif }
int _RegOpenHiveBuffer( unsigned char *buffer, unsigned long size, struct hive *h ) { h->base = (unsigned char *) malloc( size ); memcpy(h->base, buffer, size); #if BYTE_ORDER == LITTLE_ENDIAN if( *((int*)h->base) == 0x66676572 ) return 0; #elif BYTE_ORDER == BIG_ENDIAN if( *((int*)h->base) == __bswap_32( 0x66676572)) return 0; #endif return -1; }
void FilterSwap4::process() { unsigned int const * first = reinterpret_cast<unsigned int const *>(inputBuffer()); unsigned int const * last = reinterpret_cast<unsigned int const *>(inputBuffer() + imageSize_); unsigned int * dest = reinterpret_cast<unsigned int *>(outputBuffer()); while (first < last) { *(dest++) = __bswap_32(*(first++)); } }
static void easycwmp_netlink_interface(struct nlmsghdr *nlh) { struct ifaddrmsg *ifa = (struct ifaddrmsg *) NLMSG_DATA(nlh); struct rtattr *rth = IFA_RTA(ifa); int rtl = IFA_PAYLOAD(nlh); char if_name[IFNAMSIZ], if_addr[INET_ADDRSTRLEN]; static old_addr=0; memset(&if_name, 0, sizeof(if_name)); memset(&if_addr, 0, sizeof(if_addr)); while (rtl && RTA_OK(rth, rtl)) { if (rth->rta_type != IFA_LOCAL) { rth = RTA_NEXT(rth, rtl); continue; } uint32_t addr = htonl(* (uint32_t *)RTA_DATA(rth)); if (htonl(13) == 13) { // running on big endian system } else { // running on little endian system addr = __bswap_32(addr); } if_indextoname(ifa->ifa_index, if_name); if (strncmp(config->local->interface, if_name, IFNAMSIZ)) { rth = RTA_NEXT(rth, rtl); continue; } if ((addr != old_addr) && (old_addr != 0)) { log_message(NAME, L_DEBUG, "ip address of the interface %s is changed\n", if_name); cwmp_add_event(EVENT_VALUE_CHANGE, NULL, 0, EVENT_NO_BACKUP); cwmp_add_inform_timer(); } old_addr = addr; inet_ntop(AF_INET, &(addr), if_addr, INET_ADDRSTRLEN); if (config->local) FREE(config->local->ip); config->local->ip = strdup(if_addr); break; } if (strlen(if_addr) == 0) return; log_message(NAME, L_DEBUG, "interface %s has ip %s\n", if_name, if_addr); }
void put_int32(buffer *b, const uint32_t val) { Assert(b->ptr + sizeof(uint32_t) <= b->max_size); #if __BYTE_ORDER == __LITTLE_ENDIAN *((uint32_t*) BUFFER_POS(b)) = __bswap_32(val); #elif __BYTE_ORDER == __BIG_ENDIAN *((uint32_t*) BUFFER_POS(b)) = val; #else #error __BYTE_ORDER not specified! #endif b->ptr += sizeof(uint32_t); b->fill_size += sizeof(uint32_t); }
// read an IPbus transaction from a stream ipbus_transaction_t ipbus_decode_transaction(const CircularBuffer *buf, int swapbytes) { ipbus_transaction_t output = ipbus_decode_transaction_header(buf, swapbytes); size_t words_read = 1; output.data.words = (uint32_t*)malloc(output.data.size * sizeof(uint32_t)); for (unsigned int i = 0; i < output.data.size; ++i) { uint32_t dataword = network_to_host(cbuffer_value_at(buf, words_read)); words_read += 1; if (swapbytes) { dataword = __bswap_32(dataword); } output.data.words[i] = dataword; } return output; }
uint32_t get_int32(buffer *b) { uint32_t res; Assert(b->ptr + sizeof(uint32_t) <= b->fill_size); res = *((uint32_t*) BUFFER_POS(b)); b->ptr += sizeof(uint32_t); #if __BYTE_ORDER == __LITTLE_ENDIAN return __bswap_32(res); #elif __BYTE_ORDER == __BIG_ENDIAN return res; #else #error __BYTE_ORDER not specified! #endif }
/* * check whether data is native or not */ int check_data_endianness(struct trace_event_element *t) { if ((t->magic & 0xffffff00) == ENDIAN_MAGIC) { data_is_native = 1; return 0; } fprintf(stderr, "%u didnt match %u\n", t->magic, ENDIAN_MAGIC); t->magic = __bswap_32(t->magic); if ((t->magic & 0xffffff00) == ENDIAN_MAGIC) { data_is_native = 0; return 0; } fprintf(stderr, "%u didnt match %u\n", t->magic, ENDIAN_MAGIC); return 1; }
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) { uint32_t spiData ; int temp ; int chan = pin - node->pinBase ; wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ; spiData = __bswap_32(spiData) ; switch (chan) { case 0: // Existing read - return raw value * 4 spiData >>= 18 ; temp = spiData & 0x1FFF ; // Bottom 13 bits if ((spiData & 0x2000) != 0) // Negative temp = -temp ; return temp ; case 1: // Return error bits return spiData & 0x7 ; case 2: // Return temp in C * 10 spiData >>= 18 ; temp = spiData & 0x1FFF ; // Bottom 13 bits if ((spiData & 0x2000) != 0) // Negative temp = -temp ; return (int)((((double)temp * 25) + 0.5) / 10.0) ; case 3: // Return temp in F * 10 spiData >>= 18 ; temp = spiData & 0x1FFF ; // Bottom 13 bits if ((spiData & 0x2000) != 0) // Negative temp = -temp ; return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ; default: // Who knows... return 0 ; } }
static int decode_snappy(avro_codec_t c, void * data, int64_t len) { uint32_t crc; size_t outlen; if (snappy_uncompressed_length(data, len-4, &outlen) != SNAPPY_OK) { avro_set_error("Uncompressed length error in snappy"); return 1; } if (!c->block_data) { c->block_data = avro_malloc(outlen); c->block_size = outlen; } else if ( (size_t)c->block_size < outlen) { c->block_data = avro_realloc(c->block_data, c->block_size, outlen); c->block_size = outlen; } if (!c->block_data) { avro_set_error("Cannot allocate memory for snappy"); return 1; } if (snappy_uncompress(data, len-4, c->block_data, &outlen) != SNAPPY_OK) { avro_set_error("Error uncompressing block with Snappy"); return 1; } crc = __bswap_32(crc32(0, c->block_data, outlen)); if (memcmp(&crc, (char*)data+len-4, 4)) { avro_set_error("CRC32 check failure uncompressing block with Snappy"); return 1; } c->used_size = outlen; return 0; }
// initialize IPbus transaction from a stream ipbus_transaction_t ipbus_decode_transaction_header(const CircularBuffer* buf, int swapbytes) { ipbus_transaction_t output; uint32_t headerword = cbuffer_value_at_net(buf, 0); if (swapbytes) headerword = __bswap_32(headerword); output.info = headerword & 0x0f; output.type = (headerword & 0xf0) >> 4; output.words = (headerword & 0xff00) >> 8; output.id = (headerword & 0x0fff0000) >> 16; // determine payload size output.data.size = ipbus_transaction_payload_size( output.words, output.type, output.info); output.data.words = NULL; return output; }
nk_hdr* read_nk(nk_hdr *nk, struct hive *h, int offset ) { memcpy(nk, h->base+offset+4, sizeof(nk_hdr)); nk->key_name = (h->base+offset+4+76); #if BYTE_ORDER == LITTLE_ENDIAN #elif BYTE_ORDER == BIG_ENDIAN nk->id = __bswap_16(nk->id); nk->type = __bswap_16(nk->type); nk->name_len = __bswap_16(nk->name_len); nk->classname_len = __bswap_16(nk->classname_len); nk->classname_off = __bswap_32(nk->classname_off); nk->unk2 = __bswap_32(nk->unk2); nk->lf_off = __bswap_32(nk->lf_off); nk->value_cnt = __bswap_32(nk->value_cnt); nk->value_off = __bswap_32(nk->value_off); nk->subkey_num = __bswap_32(nk->subkey_num); #endif return nk; }
int32_t ns_csi32(int32_t n) { if(ns_isne()) return n; return __bswap_32(n); }
uint32_t htonl (uint32_t x) { return __bswap_32(x); }