pub fn bitcode_to_bytes<BitCodeIterator: IntoIterator<Item=BitCode>>(bitcode: BitCodeIterator) -> Vec<u8> {
    // Allocate space for the result (start with 1024 bytes so we don't reallocate every time)
    let mut result      = vec![0u8; 1024];

    // Current and highest bit position (highest bit position sets the size of the file)
    let mut cur_bit_pos = 0usize;
    let mut max_bit_pos = 0usize;

    // Iterate through the code
    for code in bitcode {
        use self::BitCode::*;

        let code = code;

        match code {
            Bits(len, pattern)              => { 
                // Resize so that the pattern will fit in to the result
                let new_bit_pos = cur_bit_pos + len as usize;
                while (new_bit_pos/8) > result.len() {
                    result.resize(result.len() * 2, 0u8);
                }

                // Write out the pattern
                let mut bits_remaining      = len as usize;
                let mut pattern_remaining   = pattern;

                while bits_remaining > 0 {
                    // Work out how many bits to write
                    let mut to_write    = bits_remaining;
                    let cur_byte        = (cur_bit_pos/8)*8;
                    let next_byte       = cur_byte+8;
                    let pos             = cur_byte/8;

                    if cur_bit_pos + to_write > next_byte {
                        to_write        = next_byte - cur_bit_pos;
                    }

                    // Create the mask
                    let mask            = ((1u16 << to_write) - 1u16) as u8;
                    let shift           = cur_bit_pos - cur_byte;

                    // Mask out the bits
                    result[pos]         &= !(mask<<shift);

                    // Store the bits from the pattern
                    let pattern         = pattern_remaining&(mask as u128);
                    result[pos]         |= (pattern as u8)<<shift;

                    // Next part of the pattern
                    pattern_remaining   >>= to_write;
                    bits_remaining      -= to_write;
                    cur_bit_pos         += to_write;
                }
            },

            Align(pattern_len, pattern, alignment)  => {
                // Work out where we're going to align to 
                let alignment           = alignment as usize;
                let align_target_pos    = if (cur_bit_pos%alignment) == 0 {
                    cur_bit_pos
                } else {
                    cur_bit_pos + (alignment - cur_bit_pos%alignment)
                };

                let pattern     = pattern & ((1<<pattern_len)-1);
                let pattern_len = if pattern_len == 0 { 8 } else { pattern_len as usize };

                // Write out the pattern
                let mut bits_remaining      = align_target_pos - cur_bit_pos;
                let mut pattern_pos         = 0usize;

                while bits_remaining > 0 {
                    // Work out how many bits to write
                    let mut to_write    = bits_remaining;
                    let cur_byte        = (cur_bit_pos/8)*8;
                    let next_byte       = cur_byte+8;
                    let pos             = cur_byte/8;

                    // Wrap the pattern position if needed
                    if pattern_pos >= pattern_len {
                        pattern_pos = 0;
                    }

                    // Write to the end of the current byte
                    if cur_bit_pos + to_write > next_byte {
                        to_write        = next_byte - cur_bit_pos;
                    }

                    // Also to the end of the pattern
                    if pattern_pos + to_write > pattern_len {
                        to_write = (pattern_len - pattern_pos) as usize;
                    }

                    // Create the mask
                    let mask            = ((1u16 << to_write) - 1u16) as u8;
                    let shift           = cur_bit_pos - cur_byte;

                    // Mask out the bits
                    result[pos]         &= !(mask<<shift);

                    // Store the bits from the pattern
                    let pattern         = (pattern >> pattern_pos) & (mask as u128);
                    result[pos]         |= (pattern as u8)<<shift;

                    // Next part of the pattern
                    pattern_pos         += to_write;
                    bits_remaining      -= to_write;
                    cur_bit_pos         += to_write;
                }
            }

            Move(new_bit_pos)               => {
                // Move and resize
                cur_bit_pos = new_bit_pos as usize;
                while (cur_bit_pos >> 3) > result.len() {
                    result.resize(result.len() * 2, 0u8);
                }
            }
        }

        // Update the maximum bit position
        if cur_bit_pos > max_bit_pos { max_bit_pos = cur_bit_pos; }
    }

    let len = if (cur_bit_pos & 0x7) == 0 {
        cur_bit_pos / 8
    } else {
        cur_bit_pos / 8 + 1
    };
    result.resize(len, 0);
    result
}
///
/// Converts a bitcode sequence into a set of bytes
///
Converts a bitcode sequence into a set of bytes
Converts a bitcode sequence into a set of bytes
pub fn bitcode_to_bytes<BitCodeIterator: IntoIterator<Item=BitCode>>(bitcode: BitCodeIterator) -> Vec<u8> {
    // Allocate space for the result (start with 1024 bytes so we don't reallocate every time)
    let mut result      = vec![0u8; 1024];
    // Current and highest bit position (highest bit position sets the size of the file)
    let mut cur_bit_pos = 0usize;
    let mut max_bit_pos = 0usize;
    // Iterate through the code
    for code in bitcode {
        use self::BitCode::*;
        let code = code;
        match code {
            Bits(len, pattern)              => { 
                // Resize so that the pattern will fit in to the result
                let new_bit_pos = cur_bit_pos + len as usize;
                while (new_bit_pos/8) > result.len() {
                    result.resize(result.len() * 2, 0u8);
                }
                // Write out the pattern
                let mut bits_remaining      = len as usize;
                let mut pattern_remaining   = pattern;
                while bits_remaining > 0 {
                    // Work out how many bits to write
                    let mut to_write    = bits_remaining;
                    let cur_byte        = (cur_bit_pos/8)*8;
                    let next_byte       = cur_byte+8;
                    let pos             = cur_byte/8;
                    if cur_bit_pos + to_write > next_byte {
                        to_write        = next_byte - cur_bit_pos;
                    }
                    // Create the mask
                    let mask            = ((1u16 << to_write) - 1u16) as u8;
                    let shift           = cur_bit_pos - cur_byte;
                    // Mask out the bits
                    result[pos]         &= !(mask<<shift);
                    // Store the bits from the pattern
                    let pattern         = pattern_remaining&(mask as u128);
                    result[pos]         |= (pattern as u8)<<shift;
                    // Next part of the pattern
                    pattern_remaining   >>= to_write;
                    bits_remaining      -= to_write;
                    cur_bit_pos         += to_write;
                }
            },
            Align(pattern_len, pattern, alignment)  => {
                // Work out where we're going to align to 
                let alignment           = alignment as usize;
                let align_target_pos    = if (cur_bit_pos%alignment) == 0 {
                    cur_bit_pos
                } else {
                    cur_bit_pos + (alignment - cur_bit_pos%alignment)
                };
                let pattern     = pattern & ((1<<pattern_len)-1);
                let pattern_len = if pattern_len == 0 { 8 } else { pattern_len as usize };
                // Write out the pattern
                let mut bits_remaining      = align_target_pos - cur_bit_pos;
                let mut pattern_pos         = 0usize;
                while bits_remaining > 0 {
                    // Work out how many bits to write
                    let mut to_write    = bits_remaining;
                    let cur_byte        = (cur_bit_pos/8)*8;
                    let next_byte       = cur_byte+8;
                    let pos             = cur_byte/8;
                    // Wrap the pattern position if needed
                    if pattern_pos >= pattern_len {
                        pattern_pos = 0;
                    }
                    // Write to the end of the current byte
                    if cur_bit_pos + to_write > next_byte {
                        to_write        = next_byte - cur_bit_pos;
                    }
                    // Also to the end of the pattern
                    if pattern_pos + to_write > pattern_len {
                        to_write = (pattern_len - pattern_pos) as usize;
                    }
                    // Create the mask
                    let mask            = ((1u16 << to_write) - 1u16) as u8;
                    let shift           = cur_bit_pos - cur_byte;
                    // Mask out the bits
                    result[pos]         &= !(mask<<shift);
                    // Store the bits from the pattern
                    let pattern         = (pattern >> pattern_pos) & (mask as u128);
                    result[pos]         |= (pattern as u8)<<shift;
                    // Next part of the pattern
                    pattern_pos         += to_write;
                    bits_remaining      -= to_write;
                    cur_bit_pos         += to_write;
                }
            }
            Move(new_bit_pos)               => {
                // Move and resize
                cur_bit_pos = new_bit_pos as usize;
                while (cur_bit_pos >> 3) > result.len() {
                    result.resize(result.len() * 2, 0u8);
                }
            }
        }
        // Update the maximum bit position
        if cur_bit_pos > max_bit_pos { max_bit_pos = cur_bit_pos; }
    }
    let len = if (cur_bit_pos & 0x7) == 0 {
        cur_bit_pos / 8
    } else {
        cur_bit_pos / 8 + 1
    };
    result.resize(len, 0);
    result
}
pub fn bitcode_to_bytes < BitCodeIterator : IntoIterator < Item = BitCode > > ( bitcode : BitCodeIterator ) -> Vec < u8 > { // Allocate space for the result (start with 1024 bytes so we don't reallocate every time) let mut result = vec ! [ 0u8 1024 ] ; // Current and highest bit position (highest bit position sets the size of the file) let mut cur_bit_pos = 0usize ; let mut max_bit_pos = 0usize ; // Iterate through the code for code in bitcode { use self :: BitCode :: * ; let code = code ; match code { Bits ( len , pattern ) => { // Resize so that the pattern will fit in to the result let new_bit_pos = cur_bit_pos + len as usize ; while ( new_bit_pos / 8 ) > result . len ( ) { result . resize ( result . len ( ) * 2 , 0u8 ) ; } // Write out the pattern let mut bits_remaining = len as usize ; let mut pattern_remaining = pattern ; while bits_remaining > 0 { // Work out how many bits to write let mut to_write = bits_remaining ; let cur_byte = ( cur_bit_pos / 8 ) * 8 ; let next_byte = cur_byte + 8 ; let pos = cur_byte / 8 ; if cur_bit_pos + to_write > next_byte { to_write = next_byte - cur_bit_pos ; } // Create the mask let mask = ( ( 1u16 << to_write ) - 1u16 ) as u8 ; let shift = cur_bit_pos - cur_byte ; // Mask out the bits result [ pos ] &= ! ( mask << shift ) ; // Store the bits from the pattern let pattern = pattern_remaining & ( mask as u128 ) ; result [ pos ] |= ( pattern as u8 ) << shift ; // Next part of the pattern pattern_remaining >>= to_write ; bits_remaining -= to_write ; cur_bit_pos += to_write ; } } , Align ( pattern_len , pattern , alignment ) => { // Work out where we're going to align to  let alignment = alignment as usize ; let align_target_pos = if ( cur_bit_pos % alignment ) == 0 { cur_bit_pos } else { cur_bit_pos + ( alignment - cur_bit_pos % alignment ) } ; let pattern = pattern & ( ( 1 << pattern_len ) - 1 ) ; let pattern_len = if pattern_len == 0 { 8 } else { pattern_len as usize } ; // Write out the pattern let mut bits_remaining = align_target_pos - cur_bit_pos ; let mut pattern_pos = 0usize ; while bits_remaining > 0 { // Work out how many bits to write let mut to_write = bits_remaining ; let cur_byte = ( cur_bit_pos / 8 ) * 8 ; let next_byte = cur_byte + 8 ; let pos = cur_byte / 8 ; // Wrap the pattern position if needed if pattern_pos >= pattern_len { pattern_pos = 0 ; } // Write to the end of the current byte if cur_bit_pos + to_write > next_byte { to_write = next_byte - cur_bit_pos ; } // Also to the end of the pattern if pattern_pos + to_write > pattern_len { to_write = ( pattern_len - pattern_pos ) as usize ; } // Create the mask let mask = ( ( 1u16 << to_write ) - 1u16 ) as u8 ; let shift = cur_bit_pos - cur_byte ; // Mask out the bits result [ pos ] &= ! ( mask << shift ) ; // Store the bits from the pattern let pattern = ( pattern >> pattern_pos ) & ( mask as u128 ) ; result [ pos ] |= ( pattern as u8 ) << shift ; // Next part of the pattern pattern_pos += to_write ; bits_remaining -= to_write ; cur_bit_pos += to_write ; } } Move ( new_bit_pos ) => { // Move and resize cur_bit_pos = new_bit_pos as usize ; while ( cur_bit_pos >> 3 ) > result . len ( ) { result . resize ( result . len ( ) * 2 , 0u8 ) ; } } } // Update the maximum bit position if cur_bit_pos > max_bit_pos { max_bit_pos = cur_bit_pos ; } } let len = if ( cur_bit_pos & 0x7 ) == 0 { cur_bit_pos / 8 } else { cur_bit_pos / 8 + 1 } ; result . resize ( len , 0 ) ; result }
Converts a bitcode sequence into a set of bytes
Converts a bitcode sequence into a set of bytes
bitcode
BitCodeIterator
bitcode
BitCodeIterator