Overview of bin.inc

Public Variable Summary

Public variables are intended to be accessed by the code that imports this library.

Name Summary
ALIGN_DEAFULT
ALIGN_LEFT
ALIGN_PAD_BYTE
ALIGN_RIGHT
NATIVE_BYTE_ORDER
OP_BIG_ENDIAN
OP_DEFAULT_ENDIAN
OP_LITTLE_ENDIAN
OP_NATIVE_ENDIAN
OP_NETWORK_ENDIAN
OP_PAD_BYTE
OP_STRING
OP_STRING_HEX
OP_STRING_NULL
OP_UINT_16
OP_UINT_32
OP_UINT_64
OP_UINT_8
VAL_PAD_BYTE

Public Function Summary

Public functions are intended to be called by the code that imports this library.

Name Summary
align
bin_pack
bin_unpack
dump_arr
pack_pad_byte
pack_string
pack_string_hex
pack_string_null
pack_uint_16
pack_uint_32
pack_uint_64
pack_uint_8
populate
unpack_pad_byte
unpack_string
unpack_string_hex
unpack_string_null
unpack_uint_16
unpack_uint_32
unpack_uint_64
unpack_uint_8

Public Variable Details

ALIGN_DEAFULT

top

ALIGN_LEFT

top

ALIGN_PAD_BYTE

top

ALIGN_RIGHT

top

NATIVE_BYTE_ORDER

top

OP_BIG_ENDIAN

top

OP_DEFAULT_ENDIAN

top

OP_LITTLE_ENDIAN

top

OP_NATIVE_ENDIAN

top

OP_NETWORK_ENDIAN

top

OP_PAD_BYTE

top

OP_STRING

top

OP_STRING_HEX

top

OP_STRING_NULL

top

OP_UINT_16

top

OP_UINT_32

top

OP_UINT_64

top

OP_UINT_8

top

VAL_PAD_BYTE

top

Public Function Details

align

Named Parameters

al
blob
boundary

Code

function align(blob, boundary, al){
  local_var padding_size, padding, i;
  if(isnull(al))
    al = ALIGN_DEFAULT;
  padding_size = boundary - (strlen(blob) % boundary);
  padding = '';
  for(i = 0; i < padding_size; i++)
    padding += ALIGN_PAD_BYTE;
  if(al == ALIGN_LEFT){
    return blob + padding;
  }
  else{
    return padding + blob;
  }
}

#  @brief Make a string indexed array(dictionary) from an regular array.

		
top

bin_pack

Named Parameters

format

Code

function bin_pack(format){
  local_var args;
  local_var format_len, endianess, iOp;
  local_var result, iArg;

  args = _FCT_ANON_ARGS;
  format_len = strlen(format);
  if(format[0] == OP_LITTLE_ENDIAN || format[0] == OP_BIG_ENDIAN || format[0] == OP_NATIVE_ENDIAN || format[0] == OP_NETWORK_ENDIAN){
    endianess = format[0];
    iOp = 1;
  }
  else{
    endianess = OP_DEFAULT_ENDIAN;
    iOp = 0;
  }
  #Process format string
  result = '';
  iArg = 0;
  for(iOp = iOp; iOp < format_len; iOp++){
    if(format[iOp] == OP_UINT_8){
      result += pack_uint_8(data: args[iArg]);
      iArg++;
    }
    else if(format[iOp] == OP_UINT_16){
      result += pack_uint_16(data: args[iArg], endianess: endianess);
      iArg++;
    }
    else if(format[iOp] == OP_UINT_32){
      result += pack_uint_32(data: args[iArg], endianess: endianess);
      iArg++;
    }
    else if(format[iOp] == OP_UINT_64){
      result += pack_uint_64(data: args[iArg], endianess: endianess);
      iArg++;
    }
    else if(format[iOp] == OP_PAD_BYTE){
      result += pack_pad_byte();
    }
    else if(format[iOp] == OP_STRING){
      result += pack_string(data: args[iArg]);
      iArg++;
    }
    else if(format[iOp] == OP_STRING_NULL){
      result += pack_string_null(data: args[iArg]);
      iArg++;
    }
    else if(format[iOp] == OP_STRING_HEX){
      result += pack_string_hex(data: args[iArg]);
      iArg++;
    }
    else if(format[iOp] == OP_LITTLE_ENDIAN || format[iOp] == OP_BIG_ENDIAN || format[iOp] == OP_NATIVE_ENDIAN || format[iOp] == OP_NETWORK_ENDIAN){
      endianess = format[iOp];
      continue;
    }
  }
  return result;
}

#  @brief Blob is unpacked according to the unpacking format.

		
top

bin_unpack

Named Parameters

blob
format
pos

Code

function bin_unpack(format, blob, pos){
  local_var lens, pos_old;
  local_var format_len, endianess, iOp;
  local_var result, iLen, iField, unpack_struct, data;
  if(isnull(pos))
    pos = 0;
  lens = _FCT_ANON_ARGS;
  pos_old = pos;
  format_len = strlen(format);
  if(format[0] == OP_LITTLE_ENDIAN || format[0] == OP_BIG_ENDIAN || format[0] == OP_NATIVE_ENDIAN || format[0] == OP_NETWORK_ENDIAN){
    endianess = format[0];
    iOp = 1;
  }
  else{
    endianess = OP_DEFAULT_ENDIAN;
    iOp = 0;
  }
  #Process format string
  iField = 0;
  iLen = 0;
  for(iOp = iOp; iOp < format_len; iOp++){
    if(format[iOp] == OP_UINT_8){
      unpack_struct = unpack_uint_8(blob: blob, pos: pos);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
    }
    else if(format[iOp] == OP_UINT_16){
      unpack_struct = unpack_uint_16(blob: blob, pos: pos, endianess: endianess);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
    }
    else if(format[iOp] == OP_UINT_32){
      unpack_struct = unpack_uint_32(blob: blob, pos: pos, endianess: endianess);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
    }
    else if(format[iOp] == OP_UINT_64){
      unpack_struct = unpack_uint_64(blob: blob, pos: pos, endianess: endianess);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
    }
    else if(format[iOp] == OP_PAD_BYTE){
      pos += unpack_pad_byte(blob: blob, pos: pos); #drop pad bytes...not interested in pad bytes. Kill all them padders, muahaaa!
    }
    else if(format[iOp] == OP_STRING){
      unpack_struct = unpack_string(blob: blob, pos: pos, len: lens[iLen]);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
      iLen++;
    }
    else if(format[iOp] == OP_STRING_NULL){
      unpack_struct = unpack_string_null(blob: blob, pos: pos);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
    }
    else if(format[iOp] == OP_STRING_HEX){
      unpack_struct = unpack_string_hex(blob: blob, pos: pos, len: lens[iLen]);
      data[iField] = unpack_struct['data'];
      pos += strlen(unpack_struct['blob']);
      iField++;
      iLen++;
    }
    else if(format[iOp] == OP_LITTLE_ENDIAN || format[iOp] == OP_BIG_ENDIAN || format[iOp] == OP_NATIVE_ENDIAN || format[iOp] == OP_NETWORK_ENDIAN){
      endianess = format[iOp];
      continue;
    }
  }
  result['blob'] = substr(blob, pos_old, pos - 1);
  result['data'] = data;
  return result;
}

#  @brief Aligns a blob to a boundary.

		
top

dump_arr

Named Parameters

arr
title

Code

function dump_arr(arr, title){
  display('---[ ' + title + ' ]---\n' );
  local_var k, e, key, elem, keyz, elemz;
  k = 0;
  foreach key (keys(arr)) keyz[k++] = key;
  for(e = 0; e < k; e++) display(keyz[e] + ':' + arr[keyz[e]] + '\n');
  display('--------------\n' );
}
###########################

		
top

pack_pad_byte

Named Parameters

Code

function pack_pad_byte(){
  return VAL_PAD_BYTE;
}

#  @brief Unpack a 1B blob.

		
top

pack_string

Named Parameters

data

Code

function pack_string(data){
  return data;
}

#  @brief Unpack a blob of 'len' length into a string object.

		
top

pack_string_hex

Named Parameters

data

Code

function pack_string_hex(data){
  local_var hex, hex_len, result;
  local_var val;
  #Prep data
  hex = tolower(data);
  hex = ereg_replace(string: hex, pattern: "0x|[' ''\r''\n''\t']", replace: '');
  hex_len = strlen(hex);
  #Parse
  result = '';
  for(i = 0; i < hex_len; i = i+2){
    if(ord(hex[i]) >= ord('a') && ord(hex[i]) <= ord('f')){
      val = 16*(ord(hex[i]) - ord('a') + 10);
    }
    else if(ord(hex[i]) >= ord('0') && ord(hex[i]) <= ord('9')){
      val = 16*(ord(hex[i]) - ord('0'));
    }
    else continue;

    if(ord(hex[i+1]) >= ord('a') && ord(hex[i+1]) <= ord('f')){
      val += (ord(hex[i+1]) - ord('a') + 10);
    }
    else if(ord(hex[i+1]) >= ord('0') && ord(hex[i+1]) <= ord('9')){
      val += ord(hex[i+1]) - ord('0');;
    }
    else continue;
    result += raw_string(val);
  }
  return result;
}

#  @brief Unpack a blob of 'len' length into a hex string.

		
top

pack_string_null

Named Parameters

data

Code

function pack_string_null(data){
  return data + raw_string(0x00);
}

#  @brief Unpack a null terminated blob.

		
top

pack_uint_16

Named Parameters

data
endianess

Code

function pack_uint_16(data, endianess){
  local_var uint_16, uint_16_lo, uint_16_hi;
  local_var result;
  uint_16 = data % (256*256); #modulo normalization - if data is bigger than short
  uint_16_lo = uint_16 % 256;
  uint_16_hi = uint_16 / 256;

  if(endianess == OP_NATIVE_ENDIAN){
    endianess = NATIVE_BYTE_ORDER; #retrieved from INIT
  }

  if(endianess == OP_LITTLE_ENDIAN){
    result = raw_string(uint_16_lo, uint_16_hi);
  }
  else if(endianess == OP_BIG_ENDIAN || endianess == OP_NETWORK_ENDIAN){
    result = raw_string(uint_16_hi, uint_16_lo);
  }
  return result;
}

#  @brief Unpack a 2B blob into an unsigned short object.

		
top

pack_uint_32

Named Parameters

data
endianess

Code

function pack_uint_32(data, endianess){
  local_var uint_32, uint_32_lo, uint_32_hi, uint_32_lo_lo, uint_32_lo_hi, uint_32_hi_lo, uint_32_hi_hi;
  local_var result;
  uint_32 = data;
  uint_32_lo = uint_32 % (256*256);
  uint_32_hi = uint_32 / (256*256);
  uint_32_lo_lo = uint_32_lo % 256;
  uint_32_lo_hi = uint_32_lo / 256;
  uint_32_hi_lo = uint_32_hi % 256;
  uint_32_hi_hi = uint_32_hi / 256;

  if(endianess == OP_NATIVE_ENDIAN){
    endianess = NATIVE_BYTE_ORDER; #retrieved from INIT
  }

  if(endianess == OP_LITTLE_ENDIAN){
    result = raw_string(uint_32_lo_lo, uint_32_lo_hi, uint_32_hi_lo, uint_32_hi_hi);
  }
  else if(endianess == OP_BIG_ENDIAN || endianess == OP_NETWORK_ENDIAN){
    result = raw_string(uint_32_hi_hi, uint_32_hi_lo, uint_32_lo_hi, uint_32_lo_lo);
  }
  return result;
}


#  @brief Unpack a 4B blob into an unsigned long object.

		
top

pack_uint_64

Named Parameters

data
endianess

Code

function pack_uint_64(data, endianess){
  local_var result;
  if(endianess == OP_NATIVE_ENDIAN){
    endianess = NATIVE_BYTE_ORDER; #retrieved from INIT
  }

  if(endianess == OP_LITTLE_ENDIAN){
    result = pack_uint_32(data: data, endianess: endianess) + raw_string(0x00, 0x00, 0x00, 0x00);
  }
  else if(endianess == OP_BIG_ENDIAN || endianess == OP_NETWORK_ENDIAN){
    result = raw_string(0x00, 0x00, 0x00, 0x00) + pack_uint_32(data: data, endianess: endianess);
  }
  return result;
}


#  @brief Unpack a 8B blob into an unsigned long long object.

		
top

pack_uint_8

Named Parameters

data

Code

function pack_uint_8(data){
  return raw_string(data % 256);
}

#  @brief Unpack a 1B blob into an unsigned char object.

		
top

populate

Named Parameters

arr

Code

function populate(arr){
  local_var result;
  for(i = 0; i < max_index(arr); i++){
    result[_FCT_ANON_ARGS[i]] = arr[i];
  }
  return result;
}
#  @brief Display an array

		
top

unpack_pad_byte

Named Parameters

blob
pos

Code

function unpack_pad_byte(blob, pos){
  return 1;
}

#  @brief Pack a string into a strlen(string)B blob.

		
top

unpack_string

Named Parameters

blob
len
pos

Code

function unpack_string(blob, pos, len){
  local_var result;
  if(isnull(len))
    len = strlen(blob) - pos;
  result['data'] = substr(blob, pos, pos + len - 1);
  result['blob'] = result['data'];
  return result;
}

#  @brief Pack a string into a strlen(string)B+1B NULL terminated blob.

		
top

unpack_string_hex

Named Parameters

blob
len
pos

Code

function unpack_string_hex(blob, pos, len){
  local_var hexstr;
  local_var result;
  if(isnull(len))
    len = strlen(blob) - pos;
  hexstr = '';
  for(i = 0; i < len; i++){
    hexstr += hex(ord(blob[pos + i])) + ',';
  }
  result['data'] = hexstr;
  result['blob'] = substr(blob, pos, pos + len - 1);
  return result;
}



		
top

unpack_string_null

Named Parameters

blob
pos

Code

function unpack_string_null(blob, pos){
  local_var result, i, blob_len;
  result['data'] = '';
  result['blob'] = '';
  blob_len = strlen(blob);
  for(i = pos; i < blob_len; i++){
    result['blob'] += blob[i];
    if(blob[i] == raw_string(0x00)) break;
    result['data'] += blob[i];
  }
  return result;
}

#  @brief Pack a hex ascii string into a raw string blob.

		
top

unpack_uint_16

Named Parameters

blob
endianess
pos

Code

function unpack_uint_16(blob, pos, endianess){
  local_var result;
  if(isnull(pos))
    pos = 0;
  if(endianess == OP_NATIVE_ENDIAN){
    endianess = NATIVE_BYTE_ORDER; #retrieved from INIT
  }
  if(endianess == OP_LITTLE_ENDIAN){
    result['data'] = ord(blob[pos]) + ord(blob[pos+1])*256;
  }
  else if(endianess == OP_BIG_ENDIAN || endianess == OP_NETWORK_ENDIAN){
    result['data'] = ord(blob[pos])*256 + ord(blob[pos+1]);
  }
  result['blob'] = substr(blob, pos, pos + 1);
  return result;
}

#  @brief Pack an unsigned long into a 4B blob.

		
top

unpack_uint_32

Named Parameters

blob
endianess
pos

Code

function unpack_uint_32(blob, pos, endianess){
  local_var result;
  if(isnull(pos))
    pos = 0;
  if(endianess == OP_NATIVE_ENDIAN){
    endianess = NATIVE_BYTE_ORDER; #retrieved from INIT
  }
  if(endianess == OP_LITTLE_ENDIAN){
    result['data'] = ord(blob[pos]) + ord(blob[pos+1])*256+ ord(blob[pos+2])*256*256 + ord(blob[pos+3])*256*256*256;
  }
  else if(endianess == OP_BIG_ENDIAN || endianess == OP_NETWORK_ENDIAN){
    result['data'] = ord(blob[pos+3]) + ord(blob[pos+2])*256+ ord(blob[pos+1])*256*256 + ord(blob[pos])*256*256*256;
  }
  result['blob'] = substr(blob, pos, pos + 3);
  return result;
}

#  @brief Pack an unsigned long long into a 8B blob.

		
top

unpack_uint_64

Named Parameters

blob
endianess
pos

Code

function unpack_uint_64(blob, pos, endianess){
  local_var unpack_struct, result;
  if(endianess == OP_NATIVE_ENDIAN){
    endianess = NATIVE_BYTE_ORDER; #retrieved from INIT
  }
  if(endianess == OP_LITTLE_ENDIAN){
    unpack_struct = unpack_uint_32(blob: blob, endianess: endianess);
    result['data'] = unpack_struct['data'];
  }
  else if(endianess == OP_BIG_ENDIAN || endianess == OP_NETWORK_ENDIAN){
    unpack_struct = unpack_uint_32(blob: blob, pos: pos + 4, endianess: endianess);
    result['data'] = unpack_struct['data'];
  }
  result['blob'] = substr(blob, pos, pos + 7);
  return result;
}

#  @brief Pack VAL_PAD_BYTE into a 1B blob.

		
top

unpack_uint_8

Named Parameters

blob
pos

Code

function unpack_uint_8(blob, pos){
  local_var result;
  result['data'] = ord(blob[pos]);
  result['blob'] = substr(blob, pos, pos);
  return result;
}

#  @brief Pack an unsigned short into a 2B blob.

		
top