Class: Cosmos::BinaryAccessor
- Defined in:
- lib/cosmos/packets/binary_accessor.rb,
ext/cosmos/ext/structure/structure.c
Overview
Provides methods for binary reading and writing
Constant Summary collapse
- PACK_8_BIT_INT =
Constants for ruby packing directives
'c'
- PACK_NATIVE_16_BIT_INT =
's'
- PACK_LITTLE_ENDIAN_16_BIT_UINT =
'v'
- PACK_BIG_ENDIAN_16_BIT_UINT =
'n'
- PACK_NATIVE_32_BIT_INT =
'l'
- PACK_NATIVE_32_BIT_UINT =
'L'
- PACK_NATIVE_64_BIT_INT =
'q'
- PACK_NATIVE_64_BIT_UINT =
'Q'
- PACK_LITTLE_ENDIAN_32_BIT_UINT =
'V'
- PACK_BIG_ENDIAN_32_BIT_UINT =
'N'
- PACK_LITTLE_ENDIAN_32_BIT_FLOAT =
'e'
- PACK_LITTLE_ENDIAN_64_BIT_FLOAT =
'E'
- PACK_BIG_ENDIAN_32_BIT_FLOAT =
'g'
- PACK_BIG_ENDIAN_64_BIT_FLOAT =
'G'
- PACK_NULL_TERMINATED_STRING =
'Z*'
- PACK_BLOCK =
'a*'
- PACK_8_BIT_INT_ARRAY =
'c*'
- PACK_8_BIT_UINT_ARRAY =
'C*'
- PACK_NATIVE_16_BIT_INT_ARRAY =
's*'
- PACK_BIG_ENDIAN_16_BIT_UINT_ARRAY =
'n*'
- PACK_LITTLE_ENDIAN_16_BIT_UINT_ARRAY =
'v*'
- PACK_NATIVE_32_BIT_INT_ARRAY =
'l*'
- PACK_BIG_ENDIAN_32_BIT_UINT_ARRAY =
'N*'
- PACK_LITTLE_ENDIAN_32_BIT_UINT_ARRAY =
'V*'
- PACK_NATIVE_64_BIT_INT_ARRAY =
'q*'
- PACK_NATIVE_64_BIT_UINT_ARRAY =
'Q*'
- PACK_LITTLE_ENDIAN_32_BIT_FLOAT_ARRAY =
'e*'
- PACK_LITTLE_ENDIAN_64_BIT_FLOAT_ARRAY =
'E*'
- PACK_BIG_ENDIAN_32_BIT_FLOAT_ARRAY =
'g*'
- PACK_BIG_ENDIAN_64_BIT_FLOAT_ARRAY =
'G*'
- ZERO_STRING =
Additional Constants
"\000"
- DATA_TYPES =
Valid data types
[:INT, :UINT, :FLOAT, :STRING, :BLOCK]
- OVERFLOW_TYPES =
Valid overflow types
[:TRUNCATE, :SATURATE, :ERROR, :ERROR_ALLOW_HEX]
- HOST_ENDIANNESS =
Store the host endianness so that it only has to be determined once
get_host_endianness()
- ENDIANNESS =
Valid endianess
[:BIG_ENDIAN, :LITTLE_ENDIAN]
- MIN_INT8 =
MIN_INT8
- MAX_INT8 =
MAX_INT8
- MAX_UINT8 =
MAX_UINT8
- MIN_INT16 =
MIN_INT16
- MAX_INT16 =
MAX_INT16
- MAX_UINT16 =
MAX_UINT16
- MIN_INT32 =
MIN_INT32
- MAX_INT32 =
MAX_INT32
- MAX_UINT32 =
MAX_UINT32
- MIN_INT64 =
MIN_INT64
- MAX_INT64 =
MAX_INT64
- MAX_UINT64 =
MAX_UINT64
Class Method Summary collapse
-
.adjust_packed_size(num_bytes, packed) ⇒ Object
Adjusts the packed array to be the given number of bytes.
-
.byte_swap_buffer(buffer, num_bytes_per_word) ⇒ String
Byte swaps every X bytes of data in a buffer into a new buffer.
-
.byte_swap_buffer!(buffer, num_bytes_per_word) ⇒ String
Byte swaps every X bytes of data in a buffer overwriting the buffer.
-
.check_overflow(value, min_value, max_value, hex_max_value, bit_size, data_type, overflow) ⇒ Integer
Checks for overflow of an integer data type.
-
.check_overflow_array(values, min_value, max_value, hex_max_value, bit_size, data_type, overflow) ⇒ Array[Integer]
Checks for overflow of an array of integer data types.
-
.read(param_bit_offset, param_bit_size, param_data_type, param_buffer, param_endianness) ⇒ Integer
Reads binary data of any data type from a buffer.
-
.read_array(bit_offset, bit_size, data_type, array_size, buffer, endianness) ⇒ Array
Reads an array of binary data of any data type from a buffer.
-
.write(value, param_bit_offset, param_bit_size, param_data_type, param_buffer, param_endianness, param_overflow) ⇒ Integer
Writes binary data of any data type to a buffer.
-
.write_array(values, bit_offset, bit_size, data_type, array_size, buffer, endianness, overflow) ⇒ Array
Writes an array of binary data of any data type to a buffer.
Class Method Details
.adjust_packed_size(num_bytes, packed) ⇒ Object
Adjusts the packed array to be the given number of bytes
546 547 548 549 550 551 552 553 554 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 546 def self.adjust_packed_size(num_bytes, packed) difference = num_bytes - packed.length if difference > 0 packed << (ZERO_STRING * difference) elsif difference < 0 packed = packed[0..(packed.length - 1 + difference)] end packed end |
.byte_swap_buffer(buffer, num_bytes_per_word) ⇒ String
Byte swaps every X bytes of data in a buffer into a new buffer
577 578 579 580 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 577 def self.byte_swap_buffer(buffer, num_bytes_per_word) buffer = buffer.clone self.byte_swap_buffer!(buffer, num_bytes_per_word) end |
.byte_swap_buffer!(buffer, num_bytes_per_word) ⇒ String
Byte swaps every X bytes of data in a buffer overwriting the buffer
561 562 563 564 565 566 567 568 569 570 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 561 def self.byte_swap_buffer!(buffer, num_bytes_per_word) num_swaps = buffer.length / num_bytes_per_word index = 0 num_swaps.times do range = index..(index + num_bytes_per_word - 1) buffer[range] = buffer[range].reverse index += num_bytes_per_word end buffer end |
.check_overflow(value, min_value, max_value, hex_max_value, bit_size, data_type, overflow) ⇒ Integer
Checks for overflow of an integer data type
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 592 def self.check_overflow(value, min_value, max_value, hex_max_value, bit_size, data_type, overflow) if overflow != :TRUNCATE if value > max_value if overflow == :SATURATE value = max_value else if overflow == :ERROR or value > hex_max_value raise ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}" end end elsif value < min_value if overflow == :SATURATE value = min_value else raise ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}" end end end value end |
.check_overflow_array(values, min_value, max_value, hex_max_value, bit_size, data_type, overflow) ⇒ Array[Integer]
Checks for overflow of an array of integer data types
623 624 625 626 627 628 629 630 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 623 def self.check_overflow_array(values, min_value, max_value, hex_max_value, bit_size, data_type, overflow) if overflow != :TRUNCATE values.each_with_index do |value, index| values[index] = check_overflow(value, min_value, max_value, hex_max_value, bit_size, data_type, overflow) end end values end |
.read(param_bit_offset, param_bit_size, param_data_type, param_buffer, param_endianness) ⇒ Integer
Reads binary data of any data type from a buffer
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'ext/cosmos/ext/structure/structure.c', line 425
static VALUE binary_accessor_read(VALUE self, VALUE param_bit_offset, VALUE param_bit_size, VALUE param_data_type, VALUE param_buffer, VALUE param_endianness)
{
/* Convert Parameters to C Data Types */
int bit_offset = FIX2INT(param_bit_offset);
int bit_size = FIX2INT(param_bit_size);
/* Local Variables */
int given_bit_offset = bit_offset;
int given_bit_size = bit_size;
signed char signed_char_value = 0;
unsigned char unsigned_char_value = 0;
signed short signed_short_value = 0;
unsigned short unsigned_short_value = 0;
signed int signed_int_value = 0;
signed long signed_long_value = 0;
unsigned int unsigned_int_value = 0;
signed long long signed_long_long_value = 0;
unsigned long long unsigned_long_long_value = 0;
unsigned char* unsigned_char_array = NULL;
int array_length = 0;
char* string = NULL;
int string_length = 0;
float float_value = 0.0;
double double_value = 0.0;
int shift_needed = 0;
int shift_count = 0;
int index = 0;
int num_bits = 0;
int num_bytes = 0;
int num_words = 0;
int upper_bound = 0;
int lower_bound = 0;
volatile VALUE temp_value = Qnil;
volatile VALUE return_value = Qnil;
unsigned char* buffer = NULL;
long buffer_length = 0;
Check_Type(param_buffer, T_STRING);
buffer = (unsigned char*) RSTRING_PTR(param_buffer);
buffer_length = RSTRING_LEN(param_buffer);
check_bit_offset_and_size(self, symbol_read, param_bit_offset, param_bit_size,
param_data_type, param_buffer, &bit_offset);
/* If passed a negative bit size with strings or blocks
* recalculate based on the buffer length */
if ((bit_size <= 0) && ((param_data_type == symbol_STRING) || (param_data_type == symbol_BLOCK))) {
bit_size = (((int)buffer_length * 8) - bit_offset + bit_size);
if (bit_size == 0) {
return rb_str_new2("");
} else if (bit_size < 0) {
rb_funcall(self, id_method_raise_buffer_error, 5, symbol_read, param_buffer, param_data_type, param_bit_offset, param_bit_size);
}
}
if (!check_bounds_and_buffer_size(bit_offset, bit_size, buffer_length, param_endianness, param_data_type, &lower_bound, &upper_bound))
{
rb_funcall(self, id_method_raise_buffer_error, 5, symbol_read, param_buffer, param_data_type, param_bit_offset, param_bit_size);
}
if ((param_data_type == symbol_STRING) || (param_data_type == symbol_BLOCK)) {
/*#######################################
*# Handle :STRING and :BLOCK data types
*#######################################*/
if (BYTE_ALIGNED(bit_offset)) {
string_length = upper_bound - lower_bound + 1;
string = malloc(string_length + 1);
memcpy(string, buffer + lower_bound, string_length);
string[string_length] = 0;
if (param_data_type == symbol_STRING) {
return_value = rb_str_new2(string);
} else /* param_data_type == symbol_BLOCK */ {
return_value = rb_str_new(string, string_length);
}
free(string);
} else {
rb_raise(rb_eArgError, "bit_offset %d is not byte aligned for data_type %s", given_bit_offset, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
} else if (param_data_type == symbol_INT) {
/*###################################
*# Handle :INT data type
*###################################*/
if ((BYTE_ALIGNED(bit_offset)) && (even_bit_size(bit_size)))
{
/*###########################################################
*# Handle byte-aligned 8, 16, 32, and 64 bit :INT
*###########################################################*/
switch (bit_size) {
case 8:
signed_char_value = *((signed char*) &buffer[lower_bound]);
return_value = INT2FIX(signed_char_value);
break;
case 16:
read_aligned_16(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &signed_short_value);
return_value = INT2FIX(signed_short_value);
break;
case 32:
read_aligned_32(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &signed_int_value);
return_value = INT2NUM(signed_int_value);
break;
case 64:
read_aligned_64(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &signed_long_long_value);
return_value = LL2NUM(signed_long_long_value);
break;
}
} else {
string_length = ((bit_size - 1)/ 8) + 1;
array_length = string_length + 4; /* Required number of bytes plus slack */
unsigned_char_array = (unsigned char*) malloc(array_length);
read_bitfield(lower_bound, upper_bound, bit_offset, bit_size, given_bit_offset, given_bit_size, param_endianness, buffer, (int)buffer_length, unsigned_char_array);
num_words = ((string_length - 1) / 4) + 1;
num_bytes = num_words * 4;
num_bits = num_bytes * 8;
shift_needed = num_bits - bit_size;
shift_count = shift_needed / 8;
shift_needed = shift_needed % 8;
if (bit_size > 1) {
for (index = 0; index < shift_count; index++) {
signed_right_shift_byte_array(unsigned_char_array, num_bytes, 8);
}
if (shift_needed > 0) {
signed_right_shift_byte_array(unsigned_char_array, num_bytes, shift_needed);
}
} else {
for (index = 0; index < shift_count; index++) {
unsigned_right_shift_byte_array(unsigned_char_array, num_bytes, 8);
}
if (shift_needed > 0) {
unsigned_right_shift_byte_array(unsigned_char_array, num_bytes, shift_needed);
}
}
if (HOST_ENDIANNESS == symbol_LITTLE_ENDIAN) {
for (index = 0; index < num_bytes; index += 4) {
reverse_bytes(&(unsigned_char_array[index]), 4);
}
}
if (bit_size <= 31) {
return_value = INT2FIX(*((int*) unsigned_char_array));
} else if (bit_size == 32) {
return_value = INT2NUM(*((int*) unsigned_char_array));
} else {
return_value = rb_int2big(*((int*) unsigned_char_array));
temp_value = INT2FIX(32);
for (index = 4; index < num_bytes; index += 4) {
return_value = rb_big_lshift(return_value, temp_value);
if (FIXNUM_P(return_value)) {
signed_long_value = FIX2LONG(return_value);
return_value = rb_int2big(signed_long_value);
}
return_value = rb_big_plus(return_value, UINT2NUM(*((unsigned int*) &(unsigned_char_array[index]))));
if (FIXNUM_P(return_value)) {
signed_long_value = FIX2LONG(return_value);
return_value = rb_int2big(signed_long_value);
}
}
return_value = rb_big_norm(return_value);
}
free(unsigned_char_array);
}
} else if (param_data_type == symbol_UINT) {
/*###################################
*# Handle :UINT data type
*###################################*/
if ((BYTE_ALIGNED(bit_offset)) && (even_bit_size(bit_size)))
{
/*###########################################################
*# Handle byte-aligned 8, 16, 32, and 64 bit :UINT
*###########################################################*/
switch (bit_size) {
case 8:
unsigned_char_value = buffer[lower_bound];
return_value = INT2FIX(unsigned_char_value);
break;
case 16:
read_aligned_16(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &unsigned_short_value);
return_value = INT2FIX(unsigned_short_value);
break;
case 32:
read_aligned_32(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &unsigned_int_value);
return_value = UINT2NUM(unsigned_int_value);
break;
case 64:
read_aligned_64(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &unsigned_long_long_value);
return_value = ULL2NUM(unsigned_long_long_value);
break;
}
} else {
string_length = ((bit_size - 1)/ 8) + 1;
array_length = string_length + 4; /* Required number of bytes plus slack */
unsigned_char_array = (unsigned char*) malloc(array_length);
read_bitfield(lower_bound, upper_bound, bit_offset, bit_size, given_bit_offset, given_bit_size, param_endianness, buffer, (int)buffer_length, unsigned_char_array);
num_words = ((string_length - 1) / 4) + 1;
num_bytes = num_words * 4;
num_bits = num_bytes * 8;
shift_needed = num_bits - bit_size;
shift_count = shift_needed / 8;
shift_needed = shift_needed % 8;
for (index = 0; index < shift_count; index++) {
unsigned_right_shift_byte_array(unsigned_char_array, num_bytes, 8);
}
if (shift_needed > 0) {
unsigned_right_shift_byte_array(unsigned_char_array, num_bytes, shift_needed);
}
if (HOST_ENDIANNESS == symbol_LITTLE_ENDIAN) {
for (index = 0; index < num_bytes; index += 4) {
reverse_bytes(&(unsigned_char_array[index]), 4);
}
}
if (bit_size <= 30) {
return_value = INT2FIX(*((int*) unsigned_char_array));
} else if (bit_size <= 32) {
return_value = UINT2NUM(*((unsigned int*) unsigned_char_array));
} else {
return_value = rb_uint2big(*((unsigned int*) unsigned_char_array));
temp_value = INT2FIX(32);
for (index = 4; index < num_bytes; index += 4) {
return_value = rb_big_lshift(return_value, temp_value);
if (FIXNUM_P(return_value)) {
signed_long_value = FIX2LONG(return_value);
return_value = rb_int2big(signed_long_value);
}
return_value = rb_big_plus(return_value, UINT2NUM(*((unsigned int*) &(unsigned_char_array[index]))));
if (FIXNUM_P(return_value)) {
signed_long_value = FIX2LONG(return_value);
return_value = rb_int2big(signed_long_value);
}
}
return_value = rb_big_norm(return_value);
}
free(unsigned_char_array);
}
} else if (param_data_type == symbol_FLOAT) {
/*##########################
*# Handle :FLOAT data type
*##########################*/
if (BYTE_ALIGNED(bit_offset)) {
switch (bit_size) {
case 32:
read_aligned_32(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &float_value);
return_value = rb_float_new(float_value);
break;
case 64:
read_aligned_64(lower_bound, upper_bound, param_endianness, buffer, (unsigned char*) &double_value);
return_value = rb_float_new(double_value);
break;
default:
rb_raise(rb_eArgError, "bit_size is %d but must be 32 or 64 for data_type %s", given_bit_size, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
break;
};
} else {
rb_raise(rb_eArgError, "bit_offset %d is not byte aligned for data_type %s", given_bit_offset, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
} else {
/*############################
*# Handle Unknown data types
*############################*/
rb_raise(rb_eArgError, "data_type %s is not recognized", RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
return return_value;
}
|
.read_array(bit_offset, bit_size, data_type, array_size, buffer, endianness) ⇒ Array
Reads an array of binary data of any data type from a buffer
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 128 def self.read_array(bit_offset, bit_size, data_type, array_size, buffer, endianness) # Save given values of bit offset, bit size, and array_size given_bit_offset = bit_offset given_bit_size = bit_size given_array_size = array_size # Handle negative and zero bit sizes raise ArgumentError, "bit_size #{given_bit_size} must be positive for arrays" if bit_size <= 0 # Handle negative bit offsets if bit_offset < 0 bit_offset = ((buffer.length * 8) + bit_offset) raise_buffer_error(:read, buffer, data_type, given_bit_offset, given_bit_size) if bit_offset < 0 end # Handle negative and zero array sizes if array_size <= 0 if given_bit_offset < 0 raise ArgumentError, "negative or zero array_size (#{given_array_size}) cannot be given with negative bit_offset (#{given_bit_offset})" else array_size = ((buffer.length * 8) - bit_offset + array_size) if array_size == 0 return [] elsif array_size < 0 raise_buffer_error(:read, buffer, data_type, given_bit_offset, given_bit_size) end end end # Calculate number of items in the array # If there is a remainder then we have a problem raise ArgumentError, "array_size #{given_array_size} not a multiple of bit_size #{given_bit_size}" if array_size % bit_size != 0 num_items = array_size / bit_size # Define bounds of string to access this item lower_bound = bit_offset / 8 upper_bound = (bit_offset + array_size - 1) / 8 # Check for byte alignment byte_aligned = ((bit_offset % 8) == 0) case data_type when :STRING, :BLOCK ####################################### # Handle :STRING and :BLOCK data types ####################################### if byte_aligned value = [] num_items.times do value << self.read(bit_offset, bit_size, data_type, buffer, endianness) bit_offset += bit_size end else raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}" end when :INT, :UINT ################################### # Handle :INT and :UINT data types ################################### if byte_aligned and (bit_size == 8 or bit_size == 16 or bit_size == 32 or bit_size == 64) ########################################################### # Handle byte-aligned 8, 16, 32, and 64 bit :INT and :UINT ########################################################### case bit_size when 8 if data_type == :INT value = buffer[lower_bound..upper_bound].unpack(PACK_8_BIT_INT_ARRAY) else # data_type == :UINT value = buffer[lower_bound..upper_bound].unpack(PACK_8_BIT_UINT_ARRAY) end when 16 if data_type == :INT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_16_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 2) value = temp.to_s.unpack(PACK_NATIVE_16_BIT_INT_ARRAY) end else # data_type == :UINT if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_16_BIT_UINT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_16_BIT_UINT_ARRAY) end end when 32 if data_type == :INT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_32_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 4) value = temp.to_s.unpack(PACK_NATIVE_32_BIT_INT_ARRAY) end else # data_type == :UINT if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_32_BIT_UINT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_32_BIT_UINT_ARRAY) end end when 64 if data_type == :INT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_64_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 8) value = temp.to_s.unpack(PACK_NATIVE_64_BIT_INT_ARRAY) end else # data_type == :UINT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_64_BIT_UINT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 8) value = temp.to_s.unpack(PACK_NATIVE_64_BIT_UINT_ARRAY) end end end else ################################## # Handle :INT and :UINT Bitfields ################################## raise ArgumentError, "read_array does not support little endian bit fields with bit_size greater than 1-bit" if endianness == :LITTLE_ENDIAN and bit_size > 1 value = [] num_items.times do value << self.read(bit_offset, bit_size, data_type, buffer, endianness) bit_offset += bit_size end end when :FLOAT ########################## # Handle :FLOAT data type ########################## if byte_aligned case bit_size when 32 if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_32_BIT_FLOAT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_32_BIT_FLOAT_ARRAY) end when 64 if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_64_BIT_FLOAT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_64_BIT_FLOAT_ARRAY) end else raise ArgumentError, "bit_size is #{given_bit_size} but must be 32 or 64 for data_type #{data_type}" end else raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}" end else ############################ # Handle Unknown data types ############################ raise ArgumentError, "data_type #{data_type} is not recognized" end value end |
.write(value, param_bit_offset, param_bit_size, param_data_type, param_buffer, param_endianness, param_overflow) ⇒ Integer
Writes binary data of any data type to a buffer
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 |
# File 'ext/cosmos/ext/structure/structure.c', line 828
static VALUE binary_accessor_write(VALUE self, VALUE value, VALUE param_bit_offset, VALUE param_bit_size, VALUE param_data_type, VALUE param_buffer, VALUE param_endianness, VALUE param_overflow)
{
/* Convert Parameters to C Data Types */
int bit_offset = NUM2INT(param_bit_offset);
int bit_size = NUM2INT(param_bit_size);
/* Local Variables */
int given_bit_offset = bit_offset;
int given_bit_size = bit_size;
int upper_bound = 0;
int lower_bound = 0;
int end_bytes = 0;
int old_upper_bound = 0;
int byte_size = 0;
unsigned long long c_value = 0;
float float_value = 0.0;
double double_value = 0.0;
unsigned char* buffer = NULL;
long buffer_length = 0;
long value_length = 0;
volatile VALUE temp_shift = Qnil;
volatile VALUE temp_mask = Qnil;
volatile VALUE temp_result = Qnil;
int string_length = 0;
unsigned char* unsigned_char_array = NULL;
int array_length = 0;
int shift_needed = 0;
int shift_count = 0;
int index = 0;
int num_bits = 0;
int num_bytes = 0;
int num_words = 0;
Check_Type(param_buffer, T_STRING);
buffer = (unsigned char*) RSTRING_PTR(param_buffer);
buffer_length = RSTRING_LEN(param_buffer);
check_bit_offset_and_size(self, symbol_write, param_bit_offset, param_bit_size,
param_data_type, param_buffer, &bit_offset);
/* If passed a negative bit size with strings or blocks
* recalculate based on the value length in bytes */
if ((bit_size <= 0) && ((param_data_type == symbol_STRING) || (param_data_type == symbol_BLOCK))) {
if (!RB_TYPE_P(value, T_STRING)) {
value = rb_funcall(value, id_method_to_s, 0);
}
bit_size = RSTRING_LEN(value) * 8;
}
if ((!check_bounds_and_buffer_size(bit_offset, bit_size, buffer_length, param_endianness, param_data_type, &lower_bound, &upper_bound)) && (given_bit_size > 0))
{
rb_funcall(self, id_method_raise_buffer_error, 5, symbol_write, param_buffer, param_data_type, param_bit_offset, param_bit_size);
}
/* Check overflow type */
if ((param_overflow != symbol_TRUNCATE) &&
(param_overflow != symbol_SATURATE) &&
(param_overflow != symbol_ERROR) &&
(param_overflow != symbol_ERROR_ALLOW_HEX)) {
rb_raise(rb_eArgError, "unknown overflow type %s", RSTRING_PTR(rb_funcall(param_overflow, id_method_to_s, 0)));
}
if ((param_data_type == symbol_STRING) || (param_data_type == symbol_BLOCK)) {
/*#######################################
*# Handle :STRING and :BLOCK data types
*#######################################*/
/* Force value to be a string */
if (!RB_TYPE_P(value, T_STRING)) {
value = rb_funcall(value, id_method_to_s, 0);
}
if (BYTE_ALIGNED(bit_offset)) {
value_length = RSTRING_LEN(value);
if (given_bit_size <= 0) {
end_bytes = -(given_bit_size / 8);
old_upper_bound = buffer_length - 1 - end_bytes;
/* Lower bound + end_bytes can never be more than 1 byte outside of the given buffer */
if ((lower_bound + end_bytes) > buffer_length)
{
rb_funcall(self, id_method_raise_buffer_error, 5, symbol_write, param_buffer, param_data_type, param_bit_offset, param_bit_size);
}
if (old_upper_bound < lower_bound) {
/* String was completely empty */
if (end_bytes > 0) {
/* Preserve bytes at end of buffer */
rb_str_concat(param_buffer, rb_str_times(ZERO_STRING, INT2FIX(value_length)));
buffer = (unsigned char*) RSTRING_PTR(param_buffer);
memmove((buffer + lower_bound + value_length), (buffer + lower_bound), end_bytes);
}
} else if (bit_size == 0) {
/* Remove entire string */
rb_str_update(param_buffer, lower_bound, old_upper_bound - lower_bound + 1, rb_str_new2(""));
} else if (upper_bound < old_upper_bound) {
/* Remove extra bytes from old string */
rb_str_update(param_buffer, upper_bound + 1, old_upper_bound - upper_bound, rb_str_new2(""));
} else if ((upper_bound > old_upper_bound) && (end_bytes > 0)) {
/* Preserve bytes at end of buffer */
rb_str_concat(param_buffer, rb_str_times(ZERO_STRING, INT2FIX(upper_bound - old_upper_bound)));
buffer = (unsigned char*) RSTRING_PTR(param_buffer);
memmove((buffer + upper_bound + 1), (buffer + old_upper_bound + 1), end_bytes);
}
} else {
byte_size = bit_size / 8;
if (value_length < byte_size) {
/* Pad the requested size with zeros.
* Tell Ruby we are going to be modifying the buffer with a memset */
rb_str_modify(param_buffer);
memset(RSTRING_PTR(param_buffer) + lower_bound + value_length, 0, byte_size - value_length);
} else if (value_length > byte_size) {
if (param_overflow == symbol_TRUNCATE) {
/* Resize the value to fit the field */
rb_str_update(value, byte_size, RSTRING_LEN(value) - byte_size, rb_str_new2(""));
} else {
rb_raise(rb_eArgError, "value of %d bytes does not fit into %d bytes for data_type %s", (int)value_length, byte_size, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
}
}
if (bit_size != 0) {
rb_str_update(param_buffer, lower_bound, RSTRING_LEN(value), value);
}
} else {
rb_raise(rb_eArgError, "bit_offset %d is not byte aligned for data_type %s", given_bit_offset, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
} else if ((param_data_type == symbol_INT) || (param_data_type == symbol_UINT)) {
/*###################################
*# Handle :INT data type
*###################################*/
value = rb_funcall(rb_mKernel, id_method_Integer, 1, value);
if ((BYTE_ALIGNED(bit_offset)) && (even_bit_size(bit_size)))
{
/*###########################################################
*# Handle byte-aligned 8, 16, 32, and 64 bit
*###########################################################*/
value = check_overflow(value, bit_size, param_data_type, param_overflow);
switch (bit_size) {
case 8:
c_value = NUM2CHR(value);
break;
case 16:
c_value = NUM2USHORT(value);
break;
case 32:
c_value = NUM2UINT(value);
break;
case 64:
c_value = NUM2ULL(value);
break;
}
/* If the passed endianess doesn't match the host we reverse the bytes.
* Then shift the result over so it's at the bottom of the long long value. */
if (param_endianness != HOST_ENDIANNESS) {
reverse_bytes((unsigned char *)&c_value, 8);
c_value = (c_value >> (64 - bit_size));
}
/* Tell Ruby we are going to be modifying the buffer with a memcpy */
rb_str_modify(param_buffer);
memcpy((RSTRING_PTR(param_buffer) + lower_bound), &c_value, bit_size / 8);
} else {
/*###########################################################
*# Handle bit fields
*###########################################################*/
value = check_overflow(value, bit_size, param_data_type, param_overflow);
string_length = ((bit_size - 1)/ 8) + 1;
array_length = string_length + 4; /* Required number of bytes plus slack */
unsigned_char_array = (unsigned char*) malloc(array_length);
num_words = ((string_length - 1) / 4) + 1;
num_bytes = num_words * 4;
num_bits = num_bytes * 8;
shift_needed = num_bits - bit_size;
shift_count = shift_needed / 8;
shift_needed = shift_needed % 8;
/* Convert value into array of bytes */
if (bit_size <= 30) {
*((int *)unsigned_char_array) = FIX2INT(value);
} else if (bit_size <= 32) {
*((unsigned int *)unsigned_char_array) = NUM2UINT(value);
} else {
temp_mask = UINT2NUM(0xFFFFFFFF);
temp_shift = INT2FIX(32);
temp_result = rb_big_and(TO_BIGNUM(value), temp_mask);
/* Work around bug where rb_big_and will return Qfalse if given a first parameter of 0 */
if (temp_result == Qfalse) { temp_result = INT2FIX(0); }
*((unsigned int *)&(unsigned_char_array[num_bytes - 4])) = NUM2UINT(temp_result);
for (index = num_bytes - 8; index >= 0; index -= 4) {
value = rb_big_rshift(TO_BIGNUM(value), temp_shift);
temp_result = rb_big_and(TO_BIGNUM(value), temp_mask);
/* Work around bug where rb_big_and will return Qfalse if given a first parameter of 0 */
if (temp_result == Qfalse) { temp_result = INT2FIX(0); }
*((unsigned int *)&(unsigned_char_array[index])) = NUM2UINT(temp_result);
}
}
if (HOST_ENDIANNESS == symbol_LITTLE_ENDIAN) {
for (index = 0; index < num_bytes; index += 4) {
reverse_bytes(&(unsigned_char_array[index]), 4);
}
}
for (index = 0; index < shift_count; index++) {
left_shift_byte_array(unsigned_char_array, num_bytes, 8);
}
if (shift_needed > 0) {
left_shift_byte_array(unsigned_char_array, num_bytes, shift_needed);
}
rb_str_modify(param_buffer);
write_bitfield(lower_bound, upper_bound, bit_offset, bit_size, given_bit_offset, given_bit_size, param_endianness, (unsigned char*) RSTRING_PTR(param_buffer), (int)buffer_length, unsigned_char_array);
free(unsigned_char_array);
}
} else if (param_data_type == symbol_FLOAT) {
/*##########################
*# Handle :FLOAT data type
*##########################*/
value = rb_funcall(rb_mKernel, id_method_Float, 1, value);
if (BYTE_ALIGNED(bit_offset)) {
switch (bit_size) {
case 32:
float_value = (float)RFLOAT_VALUE(value);
if (param_endianness != HOST_ENDIANNESS) {
reverse_bytes((unsigned char *)&float_value, 4);
}
rb_str_modify(param_buffer);
memcpy((RSTRING_PTR(param_buffer) + lower_bound), &float_value, 4);
break;
case 64:
double_value = RFLOAT_VALUE(value);
if (param_endianness != HOST_ENDIANNESS) {
reverse_bytes((unsigned char *)&double_value, 8);
}
rb_str_modify(param_buffer);
memcpy((RSTRING_PTR(param_buffer) + lower_bound), &double_value, 8);
break;
default:
rb_raise(rb_eArgError, "bit_size is %d but must be 32 or 64 for data_type %s", given_bit_size, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
break;
};
} else {
rb_raise(rb_eArgError, "bit_offset %d is not byte aligned for data_type %s", given_bit_offset, RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
} else {
/*############################
*# Handle Unknown data types
*############################*/
rb_raise(rb_eArgError, "data_type %s is not recognized", RSTRING_PTR(rb_funcall(param_data_type, id_method_to_s, 0)));
}
return value;
}
|
.write_array(values, bit_offset, bit_size, data_type, array_size, buffer, endianness, overflow) ⇒ Array
Writes an array of binary data of any data type to a buffer
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/cosmos/packets/binary_accessor.rb', line 319 def self.write_array(values, bit_offset, bit_size, data_type, array_size, buffer, endianness, overflow) # Save given values of bit offset, bit size, and array_size given_bit_offset = bit_offset given_bit_size = bit_size given_array_size = array_size # Verify an array was given raise ArgumentError, "values must be an Array type class is #{values.class}" unless values.kind_of? Array # Handle negative and zero bit sizes raise ArgumentError, "bit_size #{given_bit_size} must be positive for arrays" if bit_size <= 0 # Handle negative bit offsets if bit_offset < 0 bit_offset = ((buffer.length * 8) + bit_offset) raise_buffer_error(:write, buffer, data_type, given_bit_offset, given_bit_size) if bit_offset < 0 end # Handle negative and zero array sizes if array_size <= 0 if given_bit_offset < 0 raise ArgumentError, "negative or zero array_size (#{given_array_size}) cannot be given with negative bit_offset (#{given_bit_offset})" else end_bytes = -(given_array_size / 8) lower_bound = bit_offset / 8 upper_bound = (bit_offset + (bit_size * values.length) - 1) / 8 old_upper_bound = buffer.length - 1 - end_bytes if upper_bound < old_upper_bound # Remove extra bytes from old buffer buffer[(upper_bound + 1)..old_upper_bound] = '' elsif upper_bound > old_upper_bound # Grow buffer and preserve bytes at end of buffer if necesssary buffer_length = buffer.length diff = upper_bound - old_upper_bound buffer << ZERO_STRING * diff if end_bytes > 0 buffer[(upper_bound + 1)..(buffer.length - 1)] = buffer[(old_upper_bound + 1)..(buffer_length - 1)] end end array_size = ((buffer.length * 8) - bit_offset + array_size) end end # Get data bounds for this array lower_bound = bit_offset / 8 upper_bound = (bit_offset + array_size - 1) / 8 num_bytes = upper_bound - lower_bound + 1 # Check for byte alignment byte_aligned = ((bit_offset % 8) == 0) # Calculate the number of writes num_writes = array_size / bit_size # Check for a negative array_size and adjust the number of writes # to simply be the number of values in the passed in array if given_array_size <= 0 num_writes = values.length end # Ensure the buffer has enough room if bit_offset + num_writes * bit_size > buffer.length * 8 raise_buffer_error(:write, buffer, data_type, given_bit_offset, given_bit_size) end # Ensure the given_array_size is an even multiple of bit_size raise ArgumentError, "array_size #{given_array_size} not a multiple of bit_size #{given_bit_size}" if array_size % bit_size != 0 raise ArgumentError, "too many values #{values.length} for given array_size #{given_array_size} and bit_size #{given_bit_size}" if num_writes < values.length # Check overflow type raise "unknown overflow type #{overflow}" unless OVERFLOW_TYPES.include?(overflow) case data_type when :STRING, :BLOCK ####################################### # Handle :STRING and :BLOCK data types ####################################### if byte_aligned num_writes.times do |index| self.write(values[index], bit_offset, bit_size, data_type, buffer, endianness, overflow) bit_offset += bit_size end else raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}" end when :INT, :UINT ################################### # Handle :INT and :UINT data types ################################### if byte_aligned and (bit_size == 8 or bit_size == 16 or bit_size == 32 or bit_size == 64) ########################################################### # Handle byte-aligned 8, 16, 32, and 64 bit :INT and :UINT ########################################################### case bit_size when 8 if data_type == :INT values = self.check_overflow_array(values, -128, 127, 255, bit_size, data_type, overflow) packed = values.pack(PACK_8_BIT_INT_ARRAY) else # data_type == :UINT values = self.check_overflow_array(values, 0, 255, 255, bit_size, data_type, overflow) packed = values.pack(PACK_8_BIT_UINT_ARRAY) end when 16 if data_type == :INT values = self.check_overflow_array(values, -32768, 32767, 65535, bit_size, data_type, overflow) if endianness == HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_16_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_16_BIT_INT_ARRAY) self.byte_swap_buffer!(packed, 2) end else # data_type == :UINT values = self.check_overflow_array(values, 0, 65535, 65535, bit_size, data_type, overflow) if endianness == :BIG_ENDIAN packed = values.pack(PACK_BIG_ENDIAN_16_BIT_UINT_ARRAY) else # endianness == :LITTLE_ENDIAN packed = values.pack(PACK_LITTLE_ENDIAN_16_BIT_UINT_ARRAY) end end when 32 if data_type == :INT values = self.check_overflow_array(values, -2147483648, 2147483647, 4294967295, bit_size, data_type, overflow) if endianness == HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_32_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_32_BIT_INT_ARRAY) self.byte_swap_buffer!(packed, 4) end else # data_type == :UINT values = self.check_overflow_array(values, 0, 4294967295, 4294967295, bit_size, data_type, overflow) if endianness == :BIG_ENDIAN packed = values.pack(PACK_BIG_ENDIAN_32_BIT_UINT_ARRAY) else # endianness == :LITTLE_ENDIAN packed = values.pack(PACK_LITTLE_ENDIAN_32_BIT_UINT_ARRAY) end end when 64 if data_type == :INT values = self.check_overflow_array(values, -9223372036854775808, 9223372036854775807, 18446744073709551615, bit_size, data_type, overflow) if endianness == HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_64_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_64_BIT_INT_ARRAY) self.byte_swap_buffer!(packed, 8) end else # data_type == :UINT values = self.check_overflow_array(values, 0, 18446744073709551615, 18446744073709551615, bit_size, data_type, overflow) if endianness == HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_64_BIT_UINT_ARRAY) else # endianness != HOST_ENDIANNESS packed = values.pack(PACK_NATIVE_64_BIT_UINT_ARRAY) self.byte_swap_buffer!(packed, 8) end end end # Adjust packed size to hold number of items written buffer[lower_bound..upper_bound] = adjust_packed_size(num_bytes, packed) if num_bytes > 0 else ################################## # Handle :INT and :UINT Bitfields ################################## raise ArgumentError, "write_array does not support little endian bit fields with bit_size greater than 1-bit" if endianness == :LITTLE_ENDIAN and bit_size > 1 num_writes.times do |index| self.write(values[index], bit_offset, bit_size, data_type, buffer, endianness, overflow) bit_offset += bit_size end end when :FLOAT ########################## # Handle :FLOAT data type ########################## if byte_aligned case bit_size when 32 if endianness == :BIG_ENDIAN packed = values.pack(PACK_BIG_ENDIAN_32_BIT_FLOAT_ARRAY) else # endianness == :LITTLE_ENDIAN packed = values.pack(PACK_LITTLE_ENDIAN_32_BIT_FLOAT_ARRAY) end when 64 if endianness == :BIG_ENDIAN packed = values.pack(PACK_BIG_ENDIAN_64_BIT_FLOAT_ARRAY) else # endianness == :LITTLE_ENDIAN packed = values.pack(PACK_LITTLE_ENDIAN_64_BIT_FLOAT_ARRAY) end else raise ArgumentError, "bit_size is #{given_bit_size} but must be 32 or 64 for data_type #{data_type}" end # Adjust packed size to hold number of items written buffer[lower_bound..upper_bound] = adjust_packed_size(num_bytes, packed) if num_bytes > 0 else raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}" end else ############################ # Handle Unknown data types ############################ raise ArgumentError, "data_type #{data_type} is not recognized" end # case data_type values end |