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 * Base class for ASN.1 objects.
26 *
27 * @package asn1
28 */
29abstract class ASN1Object implements ASN1DEREncodable, ASN1DERDecodable {
30
31    protected $tagClass;
32    protected $tagType;
33    protected $tagValue;
34
35    /**
36     * Appends bytes to the given byte stream.
37     *
38     * @param  arrayref &$stream stream to append bytes to
39     * @param  array $bytes the bytes to append
40     * @return void
41     */
42    protected function append(&$stream, $bytes) {
43
44        if (!is_array($bytes)) {
45            $bytes = array($bytes);
46        }
47
48        foreach ($bytes as $b) {
49            array_push($stream, $b);
50        }
51    }
52
53    /**
54     * Prepends bytes to the given byte stream.
55     *
56     * @param  arrayref &$stream stream to prepend bytes to
57     * @param  array $bytes the bytes to prepend
58     * @return void
59     */
60    protected function prepend(&$stream, $bytes) {
61
62        if (!is_array($bytes)) {
63            $bytes = array($bytes);
64        }
65
66        for ($i = count($bytes) - 1; $i >= 0; $i--) {
67            array_unshift($stream, $bytes[$i]);
68        }
69
70    }
71
72    /**
73     * Reads a single byte from the given stream.
74     *
75     * @param  arrayref &$stream the stream to read from
76     * @return int single byte
77     */
78    protected function readByte(&$stream) {
79
80        $bytes = $this->readBytes($stream, 1);
81
82        return array_shift($bytes);
83
84    }
85
86    /**
87     * Reads multiple bytes from the given stream.
88     *
89     * @throws GTException
90     * @param  arrayref &$stream the stream to read from
91     * @param  int $limit number of bytes to read
92     * @return array byte array containing the bytes read from stream
93     */
94    protected function readBytes(&$stream, $limit) {
95
96        if (!is_array($stream)) {
97            throw new GTException("parameter stream must be an array of bytes");
98        }
99
100        if (count($stream) < $limit) {
101            throw new GTException("not enough bytes, tried to read {$limit}, but only " . count($stream) . " remaining");
102        }
103
104        $bytes = array();
105
106        for ($i = 0; $i < $limit; $i++) {
107            array_push($bytes, array_shift($stream));
108
109        }
110
111        return $bytes;
112    }
113
114
115    /**
116     * Sets the tag class for this ASN1 Object.
117     *
118     * Valid values are:
119     * <pre>
120     * ASN1_TAG_CONEXT
121     * ASN1_TAG_PRIVATE
122     * ASN1_TAG_UNIVERSAL
123     * ASN1_TAG_APPLICATION
124     * </pre>
125     *
126     * @param  $tagClass the tag class to set
127     * @return void
128     */
129    public function setTagClass($tagClass) {
130        $this->tagClass = $tagClass;
131    }
132
133    /**
134     * Gets the tag class for this ASN1 Object.
135     *
136     * @return string the tag class
137     */
138    public function getTagClass() {
139        return $this->tagClass;
140    }
141
142    /**
143     * Sets the tag type for this ASN1 Object.
144     *
145     * Valid values are:
146     * <pre>
147     * ASN1_TAG_PRIMITIVE
148     * ASN1_TAG_CONSTRUCTED
149     * </pre>
150     *
151     * @param  $tagType the tag type to set
152     * @return void
153     */
154    public function setTagType($tagType) {
155        $this->tagType = $tagType;
156    }
157
158    /**
159     * Gets the tag value for this ASN1 Object.
160     *
161     * @return string the tag type
162     */
163    public function getTagType() {
164        return $this->tagType;
165    }
166
167    /**
168     * Sets the tag value for this ASN1 Object.
169     *
170     * @param  int $tagValue the tag value
171     * @return void
172     */
173    public function setTagValue($tagValue) {
174        $this->tagValue = $tagValue;
175    }
176
177    /**
178     * Gets the tag value for this ASN1 Object.
179     *
180     * @return int the tag value
181     */
182    public function getTagValue() {
183        return $this->tagValue;
184    }
185
186}
187
188?>
189