<?php
/*
 * Copyright 2008-2010 GuardTime AS
 *
 * This file is part of the GuardTime PHP SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @package util
 */

/**
 * An implementation of RFC 4648 Base-16 encoding.
 *
 * @link http://tools.ietf.org/html/rfc4648#section-8 RFC 4648
 * @see GTBaseX, GTBase32, GTBase64
 *
 * @package util
 */
class GTBase16 {

    private static $instance;

    /**
     * Encodes the given bytes into a base-16 string.
     *
     * @static
     * @param  array $bytes an array containing the bytes to encode.
     * @param  int $offset the start offset of the data within bytes
     * @param  int $length the number of bytes to encode
     * @return string the base-16 string
     */
    public static function encode(array $bytes, $offset = null, $length = null) {
        return self::getInstance()->encode($bytes, $offset, $length);
    }

    /**
     * Encodes the given bytes into a base-16 string, inserting colons after every 2 characters of output.
     *
     * @static
     * @param  array $bytes an array containing the bytes to encode
     * @param  int $offset the start offset of the data within bytes
     * @param  int $length the number of bytes to encode
     * @return string the base-16 string
     */
    public static function encodeWithColons(array $bytes, $offset = null, $length = null) {
        return self::getInstance()->encode($bytes, $offset, $length, ':', 2);
    }

    /**
     * Encodes the given bytes into a base-16 string, inserting spaces after every 2 characters of output.
     *
     * @static
     * @param  array $bytes an array containing the bytes to encode
     * @param  int $offset the start offset of the data within bytes
     * @param  int $length the number of bytes to encode
     * @return string the base-16 string
     */
    public static function encodeWithSpaces(array $bytes, $offset = null, $length = null) {
        return self::getInstance()->encode($bytes, $offset, $length, ' ', 2);
    }


    /**
     * Decodes the given base-16 string into a byte array.
     *
     * @static
     * @param  string $string base-16 encoded string
     * @return array array containing the decoded bytes
     */
    public static function decode($string) {
        return self::getInstance()->decode($string);
    }

    /**
     * Gets singleton instance of GTBaseX configured for base-16 encoding/decoding.
     *
     * @static
     * @return GTBaseX singleton instance of GTBaseX
     */
    private static function getInstance() {

        if (self::$instance == null) {
            self::$instance = new GTBaseX("0123456789ABCDEF", false, ' ');
        }

        return self::$instance;
    }

}

?>
