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