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