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 Sequence implementation.
26 *
27 * @package asn1
28 */
29class ASN1Sequence extends ASN1Object {
30
31    protected $objects = array();
32
33    /**
34     * Constructs a new ASN1Sequence instance.
35     *
36     */
37    public function __construct() {
38    }
39
40    /**
41     * Adds a new object to this sequence.
42     *
43     * @param  ASN1Object $object
44     * @return void
45     */
46    public function add($object) {
47        array_push($this->objects, $object);
48    }
49
50    /**
51     * Gets the object at index.
52     *
53     * @param  $index 0 based index
54     * @return ASN1Object object at index
55     */
56    public function getObjectAt($index) {
57        return $this->objects[$index];
58    }
59
60    /**
61     * Gets the number of objects stored inside this sequence.
62     *
63     * @return int object count
64     */
65    public function getObjectCount() {
66        return count($this->objects);
67    }
68
69    /**
70     * Gets all objects stored inside this sequence.
71     *
72     * @return array stored objects
73     */
74    public function getObjects() {
75        return $this->objects;
76    }
77
78    /**
79     * Encodes this sequence and the objects stored to DER byte array.
80     *
81     * @return array DER encoding of this sequence
82     */
83    public function encodeDER() {
84
85        $bytes = array();
86
87        foreach ($this->objects as $object) {
88            $this->append($bytes, $object->encodeDER());
89        }
90
91        $this->prepend($bytes, ASN1DER::encodeLength(count($bytes)));
92        $this->prepend($bytes, ASN1DER::encodeType(ASN1_TAG_SEQUENCE));
93
94        return $bytes;
95    }
96
97    /**
98     * Decodes an ASN1Sequence from the given byte stream.
99     *
100     * @param  $bytes V bytes from the encoding of ASN1Sequence TLV
101     * @return void
102     */
103    public function decodeDER($bytes) {
104
105        while (count($bytes) > 0) {
106
107            $object = ASN1DER::decodeType($bytes);
108            $length = ASN1DER::decodeLength($bytes);
109
110            $object->decodeDER($this->readBytes($bytes, $length));
111
112            array_push($this->objects, $object);
113        }
114
115    }
116}
117
118?>
119