1 // NAME
2 // $RCSfile: AsnSequence.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.15 $
7 // CREATED
8 // $Date: 2006/11/29 16:35:29 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 1995, 1996 by West Consulting BV
16 *
17 * Permission to use, copy, modify, and distribute this software
18 * for any purpose and without fee is hereby granted, provided
19 * that the above copyright notices appear in all copies and that
20 * both the copyright notice and this permission notice appear in
21 * supporting documentation.
22 * This software is provided "as is" without express or implied
23 * warranty.
24 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
25 * original version by hargrave@dellgate.us.dell.com (Jordan Hargrave)
26 */
27
28 /*
29 * Copyright (C) 1996 - 2006 by Westhawk Ltd
30 * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
31 *
32 * Permission to use, copy, modify, and distribute this software
33 * for any purpose and without fee is hereby granted, provided
34 * that the above copyright notices appear in all copies and that
35 * both the copyright notice and this permission notice appear in
36 * supporting documentation.
37 * This software is provided "as is" without express or implied
38 * warranty.
39 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
40 */
41
42 package uk.co.westhawk.snmp.stack;
43
44 /*-
45 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
46 * SNMP Java Client
47 * ჻჻჻჻჻჻
48 * Copyright 2023 MetricsHub, Westhawk
49 * ჻჻჻჻჻჻
50 * This program is free software: you can redistribute it and/or modify
51 * it under the terms of the GNU Lesser General Public License as
52 * published by the Free Software Foundation, either version 3 of the
53 * License, or (at your option) any later version.
54 *
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Lesser Public License for more details.
59 *
60 * You should have received a copy of the GNU General Lesser Public
61 * License along with this program. If not, see
62 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
63 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
64 */
65
66 import java.io.*;
67 import java.util.*;
68
69
70 /**
71 * This class represents the ASN.1 sequence class.
72 * This class contains a Sequence of AsnObjects.
73 *
74 * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
75 * @version $Revision: 3.15 $ $Date: 2006/11/29 16:35:29 $
76 */
77 class AsnSequence extends AsnObject {
78 private static final String version_id = "@(#)$Id: AsnSequence.java,v 3.15 2006/11/29 16:35:29 birgit Exp $ Copyright Westhawk Ltd";
79
80 private Vector children;
81
82 /**
83 * Constructors.
84 */
85 AsnSequence() {
86 this(CONS_SEQ);
87 }
88
89 /**
90 * Constructors.
91 */
92 AsnSequence(byte oddtype) {
93 type = oddtype;
94 children = new Vector(1, 1);
95 }
96
97 /**
98 * Constructors.
99 *
100 * @param pos The position of the first child
101 */
102 AsnSequence(InputStream in, int len, int pos) throws IOException {
103 this();
104 if (debug > 10) {
105 System.out.println("AsnSequence(): Length = " + len
106 + ", Pos = " + pos);
107 }
108 AsnObject a = null;
109 while (true) {
110 a = AsnReadHeader(in, pos);
111 if (a != null) {
112 pos += (a.headerLength + a.contentsLength);
113 add(a);
114 } else {
115 break; // all done
116 }
117 }
118 }
119
120 /**
121 * Returns the string representation of the AsnSequence.
122 *
123 * @return The string of the AsnSequence
124 */
125 public String toString() {
126 return "";
127 }
128
129 /**
130 * Adds a child to the sequence.
131 */
132 AsnObject add(AsnObject child) {
133 if (child.isCorrect == true) {
134 children.addElement(child);
135 }
136 return child;
137 }
138
139 /**
140 * Replaces one child by the other.
141 * This is used by v3 to insert the security / auth fingerprint.
142 */
143 AsnObject replaceChild(AsnObject oldChild, AsnObject newChild) {
144 AsnObject ret = oldChild;
145 int at = children.indexOf(oldChild);
146 if (at > -1) {
147 children.setElementAt(newChild, at);
148 ret = newChild;
149 }
150 return ret;
151 }
152
153 /**
154 * Returns the total size of the children.
155 */
156 int size() throws EncodingException {
157 Enumeration childList = children.elements();
158 int cnt, sz = 0;
159
160 while (childList.hasMoreElements()) {
161 // Get size of child
162 cnt = ((AsnObject) childList.nextElement()).size();
163
164 // Add space for type byte & length encoding
165 cnt += (1 + getLengthBytes(cnt));
166 sz += cnt;
167 }
168 return sz;
169 }
170
171 void write(OutputStream out)
172 throws IOException, EncodingException {
173 write(out, 0);
174 }
175
176 void write(OutputStream out, int pos)
177 throws IOException, EncodingException {
178 // Output header
179 int length = size();
180 startPos = pos;
181 AsnBuildHeader(out, type, length);
182 if (debug > 10) {
183 System.out.println("\tAsnSequence.write(): begin, startPos = "
184 + startPos);
185 }
186
187 // Output children
188 pos += headerLength;
189 Enumeration childList = children.elements();
190 while (childList.hasMoreElements()) {
191 AsnObject child = ((AsnObject) childList.nextElement());
192 child.write(out, pos);
193 child.startPos = pos;
194 pos += child.headerLength + child.contentsLength;
195 }
196
197 if (debug > 10) {
198 System.out.println("\tAsnSequence.write(): end");
199 }
200 }
201
202 /**
203 * recursively look for a pduSequence object.
204 */
205 AsnObject findPdu() {
206 AsnObject res = null;
207
208 Enumeration childList = children.elements();
209 while (childList.hasMoreElements()) {
210 AsnObject child = ((AsnObject) childList.nextElement());
211 res = child.findPdu();
212 if (res != null) {
213 break;
214 }
215 }
216
217 if (this.isCorrect == false && res != null) {
218 res.isCorrect = false;
219 }
220 return res;
221 }
222
223 /**
224 * recursively look for a trapPduv1Sequence object.
225 */
226 AsnObject findTrapPduv1() {
227 AsnObject res = null;
228
229 Enumeration childList = children.elements();
230 while (childList.hasMoreElements()) {
231 AsnObject child = ((AsnObject) childList.nextElement());
232 res = child.findTrapPduv1();
233 if (res != null) {
234 break;
235 }
236 }
237 if (this.isCorrect == false && res != null) {
238 res.isCorrect = false;
239 }
240 return res;
241 }
242
243 AsnObject getObj(int offset) {
244 AsnObject res = new AsnNull();
245 try {
246 res = (AsnObject) children.elementAt(offset);
247 } catch (ArrayIndexOutOfBoundsException exc) {
248 }
249 return res;
250 }
251
252 int getObjCount() {
253 return children.size();
254 }
255
256 }