1 // NAME
2 // $RCSfile: AsnUnsInteger.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.16 $
7 // CREATED
8 // $Date: 2006/01/17 17:43:54 $
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 ASN.1 32-bit unsigned integer. It is used for
72 * TIMETICKS, COUNTER, GAUGE.
73 *
74 * @see SnmpConstants#TIMETICKS
75 * @see SnmpConstants#COUNTER
76 * @see SnmpConstants#GAUGE
77 *
78 * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
79 * @version $Revision: 3.16 $ $Date: 2006/01/17 17:43:54 $
80 */
81 public class AsnUnsInteger extends AsnObject {
82 private static final String version_id = "@(#)$Id: AsnUnsInteger.java,v 3.16 2006/01/17 17:43:54 birgit Exp $ Copyright Westhawk Ltd";
83
84 /**
85 * The internal value of AsnUnsInteger.
86 */
87 protected long value;
88
89 /**
90 * Constructor. The type of the AsnUnsInteger defaults to TIMETICKS.
91 *
92 * @param v The value of the AsnUnsInteger
93 * @see SnmpConstants#TIMETICKS
94 */
95 public AsnUnsInteger(long v) {
96 this(v, TIMETICKS);
97 }
98
99 /**
100 * Constructor to create a specific type of AsnUnsInteger.
101 *
102 * @param v The value of the AsnUnsInteger
103 * @param t The type of the AsnUnsInteger
104 * @see SnmpConstants#TIMETICKS
105 * @see SnmpConstants#COUNTER
106 * @see SnmpConstants#GAUGE
107 */
108 public AsnUnsInteger(long v, byte t) {
109 this.value = v;
110 this.type = t;
111 }
112
113 /**
114 * Constructor.
115 *
116 * @param in The input stream from which the value should be read
117 * @param len The length of the AsnUnsInteger
118 */
119 public AsnUnsInteger(InputStream in, int len) throws IOException {
120 byte data[] = new byte[len];
121 if (len != in.read(data, 0, len)) {
122 throw new IOException("AsnUnsInteger(): Not enough data");
123 }
124 long val = bytesToLong(data);
125 this.value = val;
126 }
127
128 /**
129 * Returns the value representation of the AsnUnsInteger.
130 *
131 * @return The value of the AsnUnsInteger
132 */
133 public long getValue() {
134 return value;
135 }
136
137 /**
138 * Returns the string representation of the AsnUnsInteger.
139 *
140 * @return The string of the AsnUnsInteger
141 */
142 public String toString() {
143 return (String.valueOf(value));
144 }
145
146 /**
147 * Returns the number of bytes the integer occupies.
148 */
149 int size() {
150 int count, empty = 0x00, sign = 0x00;
151
152 if (value < 0) {
153 empty = 0xFF;
154 sign = 0x80;
155 }
156
157 // 32-bit integer.. change to 56 to write 64-bit long
158 // loop through bytes in value while it is 'empty'
159 for (count = 24; count > 0; count -= 8) {
160 if (((value >> count) & 0xFF) != empty)
161 break;
162 }
163
164 // Check sign bit.. make sure negative's MSB bit is 1,
165 // positives is 0
166 // (0x00000080 = 0x00 0x80) 0xFFFFFF01 => 0xFF 0x01
167 // (0x0000007F = 0x7F) 0xFFFFFF80 => 0x80
168 if (((value >> count) & 0x80) != sign)
169 count += 8;
170 return (count >> 3) + 1;
171 }
172
173 /**
174 * Output integer.
175 */
176 void write(OutputStream out, int pos) throws IOException {
177 int count, empty = 0x00, sign = 0x00;
178
179 if (value < 0) {
180 empty = 0xFF;
181 sign = 0x80;
182 }
183
184 // Get count
185 for (count = 24; count > 0; count -= 8) {
186 if (((value >> count) & 0xFF) != empty)
187 break;
188 }
189 if (((value >> count) & 0x80) != sign)
190 count += 8;
191
192 // Build header and write value
193 AsnBuildHeader(out, type, (count >> 3) + 1);
194
195 if (debug > 10) {
196 System.out.println("\tAsnUnsInteger(): value = " + value
197 + ", pos = " + pos);
198 }
199
200 for (; count >= 0; count -= 8) {
201 out.write((byte) ((value >> count) & 0xFF));
202 }
203 }
204
205 /**
206 * Changes an array of bytes into a long.
207 * Thanks to Julien Conan (jconan@protego.net) for improving
208 * this method.
209 *
210 * @param data the array of bytes
211 * @return the int representation of the array
212 */
213 protected long bytesToLong(byte[] data) throws IOException {
214 DataInputStream dis = new DataInputStream(
215 new ByteArrayInputStream(data));
216
217 long val = 0;
218 int size = data.length;
219
220 for (int n = 0; n < size; n++) {
221 val = (val << 8) + dis.readUnsignedByte();
222 }
223
224 return val;
225 }
226
227 /**
228 * Compares this object to the specified object. The result is
229 * <code>true</code> if and only if the argument is not
230 * <code>null</code> and is an <code>AsnUnsInteger</code> object that
231 * contains the same <code>long</code> value as this object.
232 *
233 * @param obj the object to compare with.
234 * @return <code>true</code> if the objects are the same;
235 * <code>false</code> otherwise.
236 */
237 public boolean equals(Object obj) {
238 if (obj instanceof AsnUnsInteger) {
239 return value == ((AsnUnsInteger) obj).value;
240 }
241 return false;
242 }
243
244 /**
245 * Returns a hash code for this <code>AsnUnsInteger</code>.
246 *
247 * @return a hash code value for this object, equal to the
248 * hash of the primitive <code>long</code> value represented
249 * by this <code>AsnUnsInteger</code> object.
250 */
251 public int hashCode() {
252 // nicked from Long.hashCode
253 return (int) (value ^ (value >>> 32));
254 }
255 }