
use utils

global radix64EncTable : [u8 x 64] ({
   '.', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
   'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
   'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
   'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
})
 
global radix64DecTable : [u8 x 128] ({
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01,
   0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   0xFF, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
   0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   0xFF, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
   0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
})

fn radix64_encode(input: utils:ByteBuffer, output: utils:ByteBuffer) {
    let a : u8 = 0
    let b : u8 = 0
    let c : u8 = 0
    let d : u8 = 0
  
    // Divide the input stream into blocks of 3 bytes
    let len = input.length
    let parts : uint = len / 3
    output.minimum_free_space(parts * 4)
    output.length = parts * 4
  
    // The input data is processed block by block
    let n = parts
    while n-- > 0 {
       // Read input data
       a = (input[n * 3] & 0xFC) >> 2
       b = ((input[n * 3] & 0x3) << 4) | ((input[n * 3 + 1] & 0xF0) >> 4)
       c = ((input[n * 3 + 1] & 0xF) << 2) | ((input[n * 3 + 2] & 0xC0) >> 6)
       d = input[n * 3 + 2] & 0x3F
  
       // Map each 3-byte block to 4 printable characters using the Radix64 character set
       output[n * 4] = radix64EncTable[a]
       output[n * 4 + 1] = radix64EncTable[b]
       output[n * 4 + 2] = radix64EncTable[c]
       output[n * 4 + 3] = radix64EncTable[d]
    }

    // A full encoding quantum is always completed at the end of a quantity
    if len == (parts * 3 + 1) {
        // Read input data
        a = (input[parts * 3] & 0xFC) >> 2
        b = (input[parts * 3] & 0x3) << 4
  
        // The final unit of encoded output will be two characters
        output.append_byte(radix64EncTable[a])
        output.append_byte(radix64EncTable[b])

    } else if len == (parts * 3 + 2) {
        // Read input data
        a = (input[parts * 3] & 0xFC) >> 2
        b = ((input[parts * 3] & 0x3) << 4) | ((input[parts * 3 + 1] & 0xF0) >> 4)
        c = (input[parts * 3 + 1] & 0xF) << 2
  
        // The final unit of encoded output will be three characters followed by one "=" padding character
        output.append_byte(radix64EncTable[a])
        output.append_byte(radix64EncTable[b])
        output.append_byte(radix64EncTable[c])
    }
}

fn radix64_decode(input: utils:ByteBuffer, output: utils:ByteBuffer) !invalid_input {
    let value : u32 = 0
    let i : uint = 0
    output.clear()
  
    // Check the length of the input string
    let len = input.length
    if (len % 4) == 1 : throw invalid_input
  
    // Process the Radix64-encoded string
    while i < len {
        // Get current character
        let c : uint = input[i]
  
        // Check the value of the current character
        if c < 128 && radix64DecTable[c] < 64 {
            // Decode the current character
            value = (value << 6) | radix64DecTable[c]
  
            // Divide the input stream into blocks of 4 characters
            if (i % 4) == 3 {
                // Map each 4-character block to 3 bytes
                output.append_byte(((value >> 16) & 0xFF).to(u8))
                output.append_byte(((value >> 8) & 0xFF).to(u8))
                output.append_byte((value & 0xFF).to(u8))
                // Decode next block
                value = 0
            }
        } else {
           // Implementations must reject the encoded data if it contains characters outside the base alphabet
           throw invalid_input
        }
        i++
    }
  
    // Check status code
    // All trailing pad characters are omitted in Radix64
    let rem = len % 4
    if rem == 2 {
        // The last block contains only 1 byte
        output.append_byte(((value >> 4) & 0xFF).to(u8))
    } else if rem == 3 {
        // The last block contains only 2 bytes
        output.append_byte(((value >> 10) & 0xFF).to(u8))
        output.append_byte(((value >> 2) & 0xFF).to(u8))
    }
 }
