bytearray.anubis 1.8 KB

 *Project*                             The Anubis Project
   
 *Title*                               ByteArray Toolbox
   
 *Copyright*                     Copyright (c) David René 2007. 
 
 *Created: 2007 01 23
 *Author*  David René
 *Status*  Released

 *Overview* 
 
read tools/basis.anubis

public define ByteArray
  duplicate
  (
    ByteArray source
  )=
  extract(source, 0, length(source)).
  
define ByteArray
  xor_ByteArray
  (
    ByteArray ba_a,
    ByteArray ba_b,
    ByteArray result,
    Int32     count_down
  )=
  if count_down = 0 then
    result
  else
    with value = truncate_to_word8(word8_to_int32(force_nth(count_down - 1, ba_a)):
                                  word8_to_int32(force_nth(count_down - 1, ba_b))),
    forget(put(result, count_down - 1, value));
    xor_ByteArray(ba_a, ba_b, result, count_down -1).
  
 /** make bitwise XOR of 2 ByteArray
  */   
public define ByteArray
  ByteArray a : ByteArray b =
  with len_a = length(a),
       len_b = length(b),
   result_ba = if len_a > len_b then duplicate(a) else duplicate(b),
    xor_ByteArray(a, b, result_ba, min(len_a, len_b)).

    
public define ByteArray
  fill_ByteArray
  (
    ByteArray ba,
    ByteArray fill
  )=
  with fill_len = length(fill),       //length of given string
       ba_len   = length(ba),         //length of given byte array
  //if the fill ByteArray is more longer than given byte array, we just truncate it
  if fill_len > ba_len then
    extract(fill,0,ba_len)
  //otherwise we extract the tail of given byte array and paste to string
  else
    fill + extract(ba, fill_len, ba_len).
    
public define ByteArray
  fill_ByteArray
  (
    ByteArray ba,
    String    string
  )=
  with ba_string = to_byte_array(string),  //convert string into byte array, for later manipulation
  fill_ByteArray(ba, ba_string).