1<?php
2/*
3 * Copyright 2008-2010 GuardTime AS
4 *
5 * This file is part of the GuardTime PHP SDK.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @package asn1
22 */
23
24/**
25 * ASN.1 BitString implementation.
26 *
27 * @package asn1
28 */
29class ASN1BitString extends ASN1Object {
30
31    protected $value;
32
33    /**
34     * Constructs a new instance of ASN1BitString.
35     *
36     * @throws GTException
37     * @param  string $value bit string consisting only of 0-s and 1-s
38     * @return void
39     */
40    public function __construct($value = null) {
41
42        if (!is_null($value)) {
43
44            $value = trim($value);
45
46            foreach (GTUtil::toArray($value) as $c) {
47                if ($c != '0' && $c != '1') {
48                    throw new GTException("invalid character found in bit string: {$c}");
49                }
50            }
51
52            $this->value = $value;
53        }
54    }
55
56    /**
57     * Gets the value of this bit string.
58     *
59     * @return string bit string consisting only of 0-s and 1-s
60     */
61    public function getValue() {
62        return $this->value;
63    }
64
65    /**
66     * Encodes the contents of this ASN1BitString as DER.
67     *
68     * @return array DER encoding of this ASN1BitString
69     */
70    public function encodeDER() {
71
72        $bytes = array();
73        $padding = 0;
74
75        foreach (str_split($this->value, 8) as $byte) {
76
77            while (strlen($byte) < 8) {
78                $byte = '0' . $byte;
79                $padding++;
80            }
81
82            $this->append($bytes, bindec($byte));
83        }
84
85        $this->prepend($bytes, $padding);
86        $this->prepend($bytes, ASN1DER::encodeLength(count($bytes)));
87        $this->prepend($bytes, ASN1DER::encodeType(ASN1_TAG_BIT_STRING));
88
89        return $bytes;
90    }
91
92    /**
93     * Decodes an ASN1BitString from the given byte stream.
94     *
95     * @throws GTException
96     * @param  array $bytes V bytes from the encoding of ASN1BitString TLV
97     * @return void
98     */
99    public function decodeDER($bytes) {
100
101        $this->value = '';
102
103        $padding = $this->readByte($bytes);
104
105        if ($padding < 0 || $padding > 7) {
106            throw new GTException("Invalid ASN1BitString padding: {$padding}");
107        }
108
109        while (count($bytes) > 0) {
110
111            $byte = $this->readByte($bytes);
112            $byte = decbin($byte);
113
114            while (strlen($byte) < 8) {
115                $byte = '0' . $byte;
116            }
117
118            $this->value .= $byte;
119        }
120
121    }
122}
123
124?>
125