1 11 12 13 package com.sun.jmx.snmp; 14 15 16 33 34 public class BerEncoder { 35 36 41 42 public BerEncoder(byte b[]) { 43 bytes = b ; 44 start = b.length ; 45 stackTop = 0 ; 46 } 47 48 49 61 62 public int trim() { 63 final int result = bytes.length - start ; 64 65 if (result > 0) 69 java.lang.System.arraycopy(bytes,start,bytes,0,result); 70 71 start = bytes.length ; 72 stackTop = 0 ; 73 74 return result ; 75 } 76 77 82 83 public void putInteger(int v) { 84 putInteger(v, IntegerTag) ; 85 } 86 87 88 94 95 public void putInteger(int v, int tag) { 96 putIntegerValue(v) ; 97 putTag(tag) ; 98 } 99 100 101 102 107 108 public void putInteger(long v) { 109 putInteger(v, IntegerTag) ; 110 } 111 112 113 119 120 public void putInteger(long v, int tag) { 121 putIntegerValue(v) ; 122 putTag(tag) ; 123 } 124 125 126 127 132 133 public void putOctetString(byte[] s) { 134 putOctetString(s, OctetStringTag) ; 135 } 136 137 138 144 145 public void putOctetString(byte[] s, int tag) { 146 putStringValue(s) ; 147 putTag(tag) ; 148 } 149 150 151 156 157 public void putOid(long[] s) { 158 putOid(s, OidTag) ; 159 } 160 161 162 168 169 public void putOid(long[] s, int tag) { 170 putOidValue(s) ; 171 putTag(tag) ; 172 } 173 174 175 178 179 public void putNull() { 180 putNull(NullTag) ; 181 } 182 183 184 189 190 public void putNull(int tag) { 191 putLength(0) ; 192 putTag(tag) ; 193 } 194 195 196 197 203 204 public void putAny(byte[] s) { 205 putAny(s, s.length) ; 206 } 207 208 209 215 216 public void putAny(byte[] s, int byteCount) { 217 java.lang.System.arraycopy(s,0,bytes,start-byteCount,byteCount); 218 start -= byteCount; 219 } 223 224 225 229 230 public void openSequence() { 231 stackBuf[stackTop++] = start ; 232 } 233 234 235 239 240 public void closeSequence() { 241 closeSequence(SequenceTag) ; 242 } 243 244 245 248 249 public void closeSequence(int tag) { 250 final int end = stackBuf[--stackTop] ; 251 putLength(end - start) ; 252 putTag(tag) ; 253 } 254 255 256 public final static int BooleanTag = 1 ; 260 public final static int IntegerTag = 2 ; 261 public final static int OctetStringTag = 4 ; 262 public final static int NullTag = 5 ; 263 public final static int OidTag = 6 ; 264 public final static int SequenceTag = 0x30 ; 265 266 267 268 269 271 272 273 278 279 protected final void putTag(int tag) { 280 if (tag < 256) { 281 bytes[--start] = (byte)tag ; 282 } 283 else { 284 while (tag != 0) { 285 bytes[--start] = (byte)(tag & 127) ; 286 tag = tag << 7 ; 287 } 288 } 289 } 290 291 292 297 298 protected final void putLength(final int length) { 299 if (length < 0) { 300 throw new IllegalArgumentException () ; 301 } 302 else if (length < 128) { 303 bytes[--start] = (byte)length ; 304 } 305 else if (length < 256) { 306 bytes[--start] = (byte)length ; 307 bytes[--start] = (byte)0x81 ; 308 } 309 else if (length < 65536) { 310 bytes[--start] = (byte)(length) ; 311 bytes[--start] = (byte)(length >> 8) ; 312 bytes[--start] = (byte)0x82 ; 313 } 314 else if (length < 16777126) { 315 bytes[--start] = (byte)(length) ; 316 bytes[--start] = (byte)(length >> 8) ; 317 bytes[--start] = (byte)(length >> 16) ; 318 bytes[--start] = (byte)0x83 ; 319 } 320 else { 321 bytes[--start] = (byte)(length) ; 322 bytes[--start] = (byte)(length >> 8) ; 323 bytes[--start] = (byte)(length >> 16) ; 324 bytes[--start] = (byte)(length >> 24) ; 325 bytes[--start] = (byte)0x84 ; 326 } 327 } 328 329 330 335 336 protected final void putIntegerValue(int v) { 337 final int end = start ; 338 int mask = 0x7f800000 ; 339 int byteNeeded = 4 ; 340 if (v < 0) { 341 while (((mask & v) == mask) && (byteNeeded > 1)) { 342 mask = mask >> 8 ; 343 byteNeeded-- ; 344 } 345 } 346 else { 347 while (((mask & v) == 0) && (byteNeeded > 1)) { 348 mask = mask >> 8 ; 349 byteNeeded-- ; 350 } 351 } 352 for (int i = 0 ; i < byteNeeded ; i++) { 353 bytes[--start] = (byte)v ; 354 v = v >> 8 ; 355 } 356 putLength(end - start) ; 357 } 358 359 360 365 366 protected final void putIntegerValue(long v) { 367 final int end = start ; 368 long mask = 0x7f80000000000000L ; 369 int byteNeeded = 8 ; 370 if (v < 0) { 371 while (((mask & v) == mask) && (byteNeeded > 1)) { 372 mask = mask >> 8 ; 373 byteNeeded-- ; 374 } 375 } 376 else { 377 while (((mask & v) == 0) && (byteNeeded > 1)) { 378 mask = mask >> 8 ; 379 byteNeeded-- ; 380 } 381 } 382 for (int i = 0 ; i < byteNeeded ; i++) { 383 bytes[--start] = (byte)v ; 384 v = v >> 8 ; 385 } 386 putLength(end - start) ; 387 } 388 389 390 395 396 protected final void putStringValue(byte[] s) { 397 final int datalen = s.length; 398 java.lang.System.arraycopy(s,0,bytes,start-datalen,datalen); 399 start -= datalen; 400 putLength(datalen) ; 404 } 405 406 407 408 413 414 protected final void putOidValue(final long[] s) { 415 final int end = start ; 416 final int slength = s.length; 417 418 if ((slength < 2) || (s[0] > 2) || (s[1] >= 40)) { 420 throw new IllegalArgumentException () ; 421 } 422 for (int i = slength - 1 ; i >= 2 ; i--) { 423 long c = s[i] ; 424 if (c < 0) { 425 throw new IllegalArgumentException () ; 426 } 427 else if (c < 128) { 428 bytes[--start] = (byte)c ; 429 } 430 else { 431 bytes[--start] = (byte)(c & 127) ; 432 c = c >> 7 ; 433 while (c != 0) { 434 bytes[--start] = (byte)(c | 128) ; 435 c = c >> 7 ; 436 } 437 } 438 } 439 bytes[--start] = (byte)(s[0] * 40 + s[1]) ; 440 putLength(end - start) ; 441 } 442 443 444 protected final byte bytes[]; 448 449 protected int start = -1 ; 455 456 protected final int stackBuf[] = new int[200] ; 463 protected int stackTop = 0 ; 464 465 } 466 467 468 | Popular Tags |