void igtl_export igtl_image_convert_byte_order(igtl_image_header * header) { int i; igtl_uint32 tmp[12]; if (igtl_is_little_endian()) { header->header_version = BYTE_SWAP_INT16(header->header_version); for (i = 0; i < 3; i ++) { header->size[i] = BYTE_SWAP_INT16(header->size[i]); header->subvol_size[i] = BYTE_SWAP_INT16(header->subvol_size[i]); header->subvol_offset[i] = BYTE_SWAP_INT16(header->subvol_offset[i]); } memcpy(tmp, header->matrix, sizeof(igtl_uint32)*12); /* * TODO: The following loop may cause segmentation fault, when it is compiled * with '-ftree-vectorize' optimization option on 64-bit Linux. * ('-ftree-vectorize' becomes active, when '-O3' optimization is specified.) * -- code has been updated on June 24 -- needs test */ for (i = 0; i < 12; i ++) { tmp[i] = BYTE_SWAP_INT32(tmp[i]); } memcpy(header->matrix, tmp, sizeof(igtl_uint32)*12); } }
void igtl_export igtl_command_convert_byte_order(igtl_command_header* header) { if (igtl_is_little_endian()) { header->encoding = BYTE_SWAP_INT16(header->encoding); header->length = BYTE_SWAP_INT16(header->length); } }
void igtl_export igtl_extended_header_convert_byte_order(igtl_extended_header * extended_header) { if (igtl_is_little_endian()) { extended_header->extended_header_size = BYTE_SWAP_INT16(extended_header->extended_header_size); extended_header->meta_data_header_size = BYTE_SWAP_INT16(extended_header->meta_data_header_size); extended_header->meta_data_size = BYTE_SWAP_INT32(extended_header->meta_data_size); extended_header->message_id = BYTE_SWAP_INT32(extended_header->message_id); } }
igtl_uint64 igtl_export igtl_image_get_crc(igtl_image_header * header, void* image) { igtl_uint64 crc; igtl_uint64 img_size; /* calculate image size (we do not call igtl_image_get_data_size() * because header has already been serialized. */ igtl_uint64 si; igtl_uint64 sj; igtl_uint64 sk; igtl_uint64 sp; if (igtl_is_little_endian()) { si = BYTE_SWAP_INT16(header->subvol_size[0]); sj = BYTE_SWAP_INT16(header->subvol_size[1]); sk = BYTE_SWAP_INT16(header->subvol_size[2]); } else { si = header->subvol_size[0]; sj = header->subvol_size[1]; sk = header->subvol_size[2]; } switch (header->scalar_type) { case IGTL_IMAGE_STYPE_TYPE_INT8: case IGTL_IMAGE_STYPE_TYPE_UINT8: sp = 1; break; case IGTL_IMAGE_STYPE_TYPE_INT16: case IGTL_IMAGE_STYPE_TYPE_UINT16: sp = 2; break; case IGTL_IMAGE_STYPE_TYPE_INT32: case IGTL_IMAGE_STYPE_TYPE_UINT32: sp = 4; break; default: sp = 0; break; } img_size = si*sj*sk*sp; crc = crc64(0, 0, 0); crc = crc64((unsigned char*) header, IGTL_IMAGE_HEADER_SIZE, crc); crc = crc64((unsigned char*) image, (int)img_size, crc); return crc; }
void igtl_export igtl_lbmeta_convert_byte_order(igtl_lbmeta_element* metalist, int nitem) { igtl_lbmeta_element* elem; int i; for (i = 0; i < nitem; i ++) { elem = &(metalist[i]); if (igtl_is_little_endian()) { elem->size[0] = BYTE_SWAP_INT16(elem->size[0]); elem->size[1] = BYTE_SWAP_INT16(elem->size[1]); elem->size[2] = BYTE_SWAP_INT16(elem->size[2]); } } }
void igtl_export igtl_header_convert_byte_order(igtl_header * header) { if (igtl_is_little_endian()) { header->version = BYTE_SWAP_INT16(header->version); header->timestamp = BYTE_SWAP_INT64(header->timestamp); header->body_size = BYTE_SWAP_INT64(header->body_size); header->crc = BYTE_SWAP_INT64(header->crc); } }
void igtl_export igtl_query_convert_byte_order(igtl_query_header* header) { if (igtl_is_little_endian()) { header->queryID = BYTE_SWAP_INT32(header->queryID); header->deviceUIDLength = BYTE_SWAP_INT16(header->deviceUIDLength); } }
void igtl_export igtl_colortable_convert_byte_order(igtl_colortable_header* header, void* table) { int i; int n; igtl_uint16* tmp; if (igtl_is_little_endian()) { if (header->mapType == IGTL_COLORTABLE_MAP_UINT16) { n = (header->indexType == IGTL_COLORTABLE_INDEX_UINT16)? 256*256 : 256; tmp = (igtl_uint16*) table; for (i = 0; i < n; i ++) { tmp[i] = BYTE_SWAP_INT16(tmp[i]); } } } }
int igtl_export igtl_ndarray_pack(igtl_ndarray_info * info, void * byte_array, int type) { char * ptr; igtl_uint16 dim; igtl_uint16 * size; igtl_uint16 i; igtl_uint64 len; igtl_uint16 * ptr16_src; igtl_uint16 * ptr16_src_end; igtl_uint16 * ptr16_dst; igtl_uint32 * ptr32_src; igtl_uint32 * ptr32_src_end; igtl_uint32 * ptr32_dst; igtl_uint64 * ptr64_src; igtl_uint64 * ptr64_src_end; igtl_uint64 * ptr64_dst; if (byte_array == NULL || info == NULL) { return 0; } ptr = (char *) byte_array; /*** Type field ***/ * (igtl_uint8 *) ptr = info->type; ptr ++; /*** Dimension field ***/ * (igtl_uint8 *) ptr = info->dim; ptr ++; /*** Size array field ***/ size = (igtl_uint16 *) ptr; if (igtl_is_little_endian()) { for (i = 0; i < info->dim; i ++) { size[i] = BYTE_SWAP_INT16(info->size[i]); } } else { memcpy(ptr, info->size, sizeof(igtl_uint16) * info->dim); } dim = info->dim; ptr += sizeof(igtl_uint16) * dim; /*** N-D array field ***/ /* Calculate number of elements in N-D array */ len = 1; for (i = 0; i < dim; i ++) { len *= info->size[i]; } /* Copy array */ if (igtl_ndarray_get_nbyte(info->type) == 1 || !igtl_is_little_endian()) { /* If single-byte data type is used or the program runs on a big-endian machine, just copy the array from the pack to the strucutre */ memcpy(ptr, info->array, (size_t) (len * igtl_ndarray_get_nbyte(info->type))); } else if (igtl_ndarray_get_nbyte(info->type) == 2) /* 16-bit */ { ptr16_src = (igtl_uint16 *) info->array; ptr16_src_end = ptr16_src + len; ptr16_dst = (igtl_uint16 *) ptr; while (ptr16_src < ptr16_src_end) { *ptr16_dst = BYTE_SWAP_INT16(*ptr16_src); ptr16_dst ++; ptr16_src ++; } } else if (igtl_ndarray_get_nbyte(info->type) == 4) /* 32-bit */ { ptr32_src = (igtl_uint32 *) info->array; ptr32_src_end = ptr32_src + len; ptr32_dst = (igtl_uint32 *) ptr; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } else /* 64-bit or Complex type */ { ptr64_src = (igtl_uint64 *) info->array; /* Adding number of elements to the pointer -- 64-bit: len * 1; Complex: len * 2*/ ptr64_src_end = ptr64_src + len * igtl_ndarray_get_nbyte(info->type)/8; ptr64_dst = (igtl_uint64 *) ptr; while (ptr64_src < ptr64_src_end) { *ptr64_dst = BYTE_SWAP_INT64(*ptr64_src); ptr64_dst ++; ptr64_src ++; } } return 1; }
int igtl_export igtl_ndarray_unpack(int type, void * byte_array, igtl_ndarray_info * info, igtl_uint64 pack_size) { char * ptr; igtl_uint16 dim; igtl_uint16 * size; igtl_uint16 i; igtl_uint64 len; igtl_uint16 * ptr16_src; igtl_uint16 * ptr16_src_end; igtl_uint16 * ptr16_dst; igtl_uint32 * ptr32_src; igtl_uint32 * ptr32_src_end; igtl_uint32 * ptr32_dst; igtl_uint64 * ptr64_src; igtl_uint64 * ptr64_src_end; igtl_uint64 * ptr64_dst; if (byte_array == NULL || info == NULL) { return 0; } igtl_ndarray_init_info(info); ptr = (char *) byte_array; /*** Type field ***/ info->type = * (igtl_uint8 *) ptr; ptr ++; /*** Dimension field ***/ info->dim = * (igtl_uint8 *) ptr; ptr ++; /*** Size array field ***/ size = (igtl_uint16 *) ptr; dim = info->dim; if (igtl_is_little_endian()) { /* Change byte order -- this overwrites memory area for the pack !!*/ for (i = 0; i < dim; i ++) { size[i] = BYTE_SWAP_INT16(size[i]); } } igtl_ndarray_alloc_info(info, size); memcpy(info->size, size, sizeof(igtl_uint16) * dim); if (igtl_is_little_endian()) { /* Resotore the overwritten memory area */ /* Don't use size[] array after this !! */ for (i = 0; i < dim; i ++) { size[i] = BYTE_SWAP_INT16(size[i]); } } ptr += sizeof(igtl_uint16) * dim; /*** N-D array field ***/ /* Calculate number of elements in N-D array */ len = 1; for (i = 0; i < dim; i ++) { len *= info->size[i]; } /* Copy array */ if (igtl_ndarray_get_nbyte(info->type) == 1 || !igtl_is_little_endian()) { /* If single-byte data type is used or the program runs on a big-endian machine, just copy the array from the pack to the strucutre */ memcpy(info->array, ptr, (size_t)(len * igtl_ndarray_get_nbyte(info->type))); } else if (igtl_ndarray_get_nbyte(info->type) == 2) /* 16-bit */ { ptr16_src = (igtl_uint16 *) ptr; ptr16_src_end = ptr16_src + len; ptr16_dst = (igtl_uint16 *) info->array; while (ptr16_src < ptr16_src_end) { *ptr16_dst = BYTE_SWAP_INT16(*ptr16_src); ptr16_dst ++; ptr16_src ++; } } else if (igtl_ndarray_get_nbyte(info->type) == 4) /* 32-bit */ { ptr32_src = (igtl_uint32 *) ptr; ptr32_src_end = ptr32_src + len; ptr32_dst = (igtl_uint32 *) info->array; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } else /* 64-bit or Complex type */ { ptr64_src = (igtl_uint64 *) ptr; /* Adding number of elements to the pointer -- 64-bit: len * 1; Complex: len * 2*/ ptr64_src_end = ptr64_src + len * igtl_ndarray_get_nbyte(info->type)/8; ptr64_dst = (igtl_uint64 *) info->array; while (ptr64_src < ptr64_src_end) { *ptr64_dst = BYTE_SWAP_INT64(*ptr64_src); ptr64_dst ++; ptr64_src ++; } } /* TODO: check if the pack size is valid */ return 1; }
/* * Function to unpack BIND message */ int igtl_bind_unpack_normal(void * byte_array, igtl_bind_info * info) { igtl_uint16 i; igtl_uint16 ncmessages; igtl_uint16 nametable_size; size_t namelen; char * ptr; char * ptr2; igtl_uint64 tmp64; if (byte_array == NULL || info == NULL) { return 0; } /* Number of child messages */ ptr = (char *) byte_array; if (igtl_is_little_endian()) { ncmessages = BYTE_SWAP_INT16(*((igtl_uint16*)ptr)); } else { ncmessages = *((igtl_uint16*)ptr); } /* Allocate an array of bind_info, if neccessary */ if (ncmessages != info->ncmessages) { if (igtl_bind_alloc_info(info, ncmessages) == 0) { return 0; } } /* Pointer to the first element in the BIND header section */ ptr += sizeof(igtl_uint16); /* Extract types and body sizes from the BIND header section */ for (i = 0; i < ncmessages; i ++) { /* Type of child message */ strncpy(info->child_info_array[i].type, (char*)ptr, IGTL_HEADER_TYPE_SIZE); info->child_info_array[i].type[IGTL_HEADER_TYPE_SIZE] = '\0'; /* Body size of child message */ ptr += IGTL_HEADER_TYPE_SIZE; if (igtl_is_little_endian()) { memcpy(&tmp64, ptr, sizeof(igtl_uint64)); info->child_info_array[i].size = BYTE_SWAP_INT64(tmp64); } else { memcpy(&(info->child_info_array[i].size), ptr, sizeof(igtl_uint64)); } ptr += sizeof(igtl_uint64); } /* Name table size */ if (igtl_is_little_endian()) { nametable_size = BYTE_SWAP_INT16(*((igtl_uint16 *) ptr)); } else { nametable_size = *((igtl_uint16 *) ptr); } /* * Check if the name table field is aligned to WORD (The length of * the field must be even. */ if (nametable_size % 2 != 0) { return 0; } ptr += sizeof(igtl_uint16); ptr2 = ptr; /* Extract device names from the name table section */ if (nametable_size > 0) { for (i = 0; i < ncmessages; i ++) { strncpy(info->child_info_array[i].name, ptr, IGTL_HEADER_NAME_SIZE); info->child_info_array[i].name[IGTL_HEADER_NAME_SIZE] = '\0'; namelen = strlen(info->child_info_array[i].name); ptr += namelen + 1; } } ptr = ptr2 + nametable_size; /* Set pointers to the child message bodies */ for (i = 0; i < ncmessages; i ++) { info->child_info_array[i].ptr = ptr; /* Note: a padding byte is added, if the size of the child message body is odd. */ ptr += info->child_info_array[i].size + (info->child_info_array[i].size % 2); } /** TODO: check the total size of the message? **/ return 1; }
/* * Function to unpack STT_BIND and GET_BIND messages */ int igtl_bind_pack_request(igtl_bind_info * info, void * byte_array) { char * ptr; igtl_uint32 i; igtl_uint32 nc; igtl_uint16 * nts; size_t wb; size_t len; igtl_uint16 tmp16; ptr = (char *) byte_array; nc = info->ncmessages; /* If requesting all available, no body is generated. */ if (info->request_all == 1) { return 1; } /* Validate info */ if (info->ncmessages == 0 || info->child_info_array == NULL) { return 0; } /* Number of child messages */ if (igtl_is_little_endian()) { /* *((igtl_uint16*) ptr) = BYTE_SWAP_INT16(info->ncmessages); */ tmp16 = BYTE_SWAP_INT16(info->ncmessages); memcpy(ptr, &tmp16, sizeof(igtl_uint16)); } else { /* *((igtl_uint16*) ptr) = info->ncmessages; */ memcpy(ptr, &(info->ncmessages), sizeof(igtl_uint16)); } ptr += sizeof(igtl_uint16); /* BIND header section */ for (i = 0; i < nc; i ++) { /*memset(ptr, 0, IGTL_HEADER_TYPE_SIZE); */ /* Fill with 0 */ strncpy((char*)ptr, info->child_info_array[i].type, IGTL_HEADER_TYPE_SIZE); ptr += IGTL_HEADER_TYPE_SIZE; } /* Name table section */ nts = (igtl_uint16 *) ptr; /* save address for name table size field */ ptr += sizeof(igtl_uint16); wb = 0; for (i = 0; i < nc; i ++) { len = strlen(info->child_info_array[i].name); if (len > IGTL_HEADER_NAME_SIZE) { return 0; } /* copy string + NULL-terminator */ strncpy(ptr, info->child_info_array[i].name, len+1); ptr += (len + 1); wb += len + 1; } /* Substitute name table size */ *nts = (igtl_uint16) wb; if (igtl_is_little_endian()) { *nts = BYTE_SWAP_INT16(*nts); } return 1; }
/* * Function to unpack BIND message */ int igtl_bind_pack_normal(igtl_bind_info * info, void * byte_array) { char * ptr; igtl_uint32 i; igtl_uint32 nc; igtl_uint16 * nts; size_t wb; size_t len; igtl_uint16 tmp16; igtl_uint64 tmp64; ptr = (char *) byte_array; nc = info->ncmessages; /* Validate info */ if (info->ncmessages == 0 || info->child_info_array == NULL) { return 0; } /* Number of child messages */ if (igtl_is_little_endian()) { /* *((igtl_uint16*) ptr) = BYTE_SWAP_INT16(info->ncmessages); */ tmp16 = BYTE_SWAP_INT16(info->ncmessages); memcpy(ptr, &tmp16, sizeof(igtl_uint16)); } else { /* *((igtl_uint16*) ptr) = info->ncmessages; */ memcpy(ptr, &(info->ncmessages), sizeof(igtl_uint16)); } ptr += sizeof(igtl_uint16); /* BIND header section */ for (i = 0; i < nc; i ++) { /* Fill with 0 */ memset(ptr, 0, IGTL_HEADER_TYPE_SIZE); strncpy((char*)ptr, info->child_info_array[i].type, IGTL_HEADER_TYPE_SIZE); ptr += IGTL_HEADER_TYPE_SIZE; /* 2011-04-29 * Since ptr is not alined to double-word order at this point, * "* ((igtl_uint64 *) ptr) = <value>" may fail in some archtecture. * In the following section, memcpy() is called instaed of using * ((igtl_uint64 *) ptr) * expression. */ if (igtl_is_little_endian()) { tmp64 = BYTE_SWAP_INT64(info->child_info_array[i].size); memcpy(ptr, &tmp64, sizeof(igtl_uint64)); } else { memcpy(ptr, &(info->child_info_array[i].size), sizeof(igtl_uint64)); } ptr += sizeof(igtl_uint64); } /* Name table section */ nts = (igtl_uint16 *) ptr; /* save address for name table size field */ ptr += sizeof(igtl_uint16); wb = 0; for (i = 0; i < nc; i ++) { len = strlen(info->child_info_array[i].name); if (len > IGTL_HEADER_NAME_SIZE) { return 0; } /* copy string + NULL-terminator */ strncpy(ptr, info->child_info_array[i].name, len+1); ptr += (len + 1); wb += len + 1; } /* Add padding if the size of the name table section is not even */ if (wb % 2 > 0) { *((igtl_uint8*)ptr) = 0; ptr ++; wb ++; } /* Substitute name table size */ *nts = (igtl_uint16) wb; if (igtl_is_little_endian()) { *nts = BYTE_SWAP_INT16(*nts); } return 1; }