<?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 asn1
 */

/**
 * ASN.1 BitString implementation.
 *
 * @package asn1
 */
class ASN1BitString extends ASN1Object {

    protected $value;

    /**
     * Constructs a new instance of ASN1BitString.
     *
     * @throws GTException
     * @param  string $value bit string consisting only of 0-s and 1-s
     * @return void
     */
    public function __construct($value = null) {

        if (!is_null($value)) {

            $value = trim($value);

            foreach (GTUtil::toArray($value) as $c) {
                if ($c != '0' && $c != '1') {
                    throw new GTException("invalid character found in bit string: {$c}");
                }
            }

            $this->value = $value;
        }
    }

    /**
     * Gets the value of this bit string.
     *
     * @return string bit string consisting only of 0-s and 1-s
     */
    public function getValue() {
        return $this->value;
    }

    /**
     * Encodes the contents of this ASN1BitString as DER.
     *
     * @return array DER encoding of this ASN1BitString
     */
    public function encodeDER() {

        $bytes = array();
        $padding = 0;

        foreach (str_split($this->value, 8) as $byte) {

            while (strlen($byte) < 8) {
                $byte = '0' . $byte;
                $padding++;
            }

            $this->append($bytes, bindec($byte));
        }

        $this->prepend($bytes, $padding);
        $this->prepend($bytes, ASN1DER::encodeLength(count($bytes)));
        $this->prepend($bytes, ASN1DER::encodeType(ASN1_TAG_BIT_STRING));

        return $bytes;
    }

    /**
     * Decodes an ASN1BitString from the given byte stream.
     *
     * @throws GTException
     * @param  array $bytes V bytes from the encoding of ASN1BitString TLV
     * @return void
     */
    public function decodeDER($bytes) {

        $this->value = '';

        $padding = $this->readByte($bytes);

        if ($padding < 0 || $padding > 7) {
            throw new GTException("Invalid ASN1BitString padding: {$padding}");
        }

        while (count($bytes) > 0) {

            $byte = $this->readByte($bytes);
            $byte = decbin($byte);         

            while (strlen($byte) < 8) {
                $byte = '0' . $byte;
            }

            $this->value .= $byte;
        }

    }
}

?>
