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