1 21 22 package org.apache.derby.client.net; 23 24 25 import java.io.ByteArrayOutputStream ; 26 import java.util.Arrays ; 27 28 import org.apache.derby.client.am.SignedBinary; 29 import org.apache.derby.client.am.SqlException; 30 import org.apache.derby.client.am.DisconnectException; 31 import org.apache.derby.client.am.SqlState; 32 import org.apache.derby.client.am.ClientMessageId; 33 34 import org.apache.derby.shared.common.reference.SQLState; 35 import org.apache.derby.shared.common.reference.MessageId; 36 37 public class Reply { 38 protected org.apache.derby.client.am.Agent agent_; 39 protected NetAgent netAgent_; 41 private CcsidManager ccsidManager_; 42 protected final static int DEFAULT_BUFFER_SIZE = 32767; 43 protected byte[] buffer_; 44 protected int pos_; 45 protected int count_; 46 47 private int topDdmCollectionStack_; 48 private final static int MAX_MARKS_NESTING = 10; 49 private int[] ddmCollectionLenStack_; 50 private int ddmScalarLen_; private final static int EMPTY_STACK = -1; 52 53 protected boolean ensuredLengthForDecryption_ = false; protected byte[] longBufferForDecryption_ = null; 55 protected int longPosForDecryption_ = 0; 56 protected byte[] longValueForDecryption_ = null; 57 protected int longCountForDecryption_ = 0; 58 59 protected int dssLength_; 60 protected boolean dssIsContinued_; 61 private boolean dssIsChainedWithSameID_; 62 private boolean dssIsChainedWithDiffID_; 63 protected int dssCorrelationID_; 64 65 protected int peekedLength_ = 0; 66 protected int peekedCodePoint_ = END_OF_COLLECTION; private int peekedNumOfExtendedLenBytes_ = 0; 68 private int currentPos_ = 0; 69 70 public final static int END_OF_COLLECTION = -1; 71 public final static int END_OF_SAME_ID_CHAIN = -2; 72 73 Reply(NetAgent netAgent, int bufferSize) { 74 buffer_ = new byte[bufferSize]; 75 agent_ = netAgent_ = netAgent; 76 ccsidManager_ = netAgent.targetCcsidManager_; 77 ddmCollectionLenStack_ = new int[Reply.MAX_MARKS_NESTING]; 78 initialize(); 79 } 80 81 final void initialize() { 82 pos_ = 0; 83 count_ = 0; 84 topDdmCollectionStack_ = Reply.EMPTY_STACK; 85 Arrays.fill(ddmCollectionLenStack_, 0); 86 ddmScalarLen_ = 0; 87 dssLength_ = 0; 88 dssIsContinued_ = false; 89 dssIsChainedWithSameID_ = false; 90 dssIsChainedWithDiffID_ = false; 91 dssCorrelationID_ = 1; 92 } 93 94 final int getDdmLength() { 95 return ddmScalarLen_; 96 } 97 98 private final void shiftBuffer(byte[] destinationBuffer) { 103 int sz = count_ - pos_; 105 106 System.arraycopy(buffer_, pos_, destinationBuffer, 0, sz); 108 109 pos_ = 0; 111 count_ = sz; 112 113 buffer_ = destinationBuffer; 115 } 116 117 protected final void ensureSpaceInBufferForFill(int desiredSpace) { 126 int currentAvailableSpace = (buffer_.length - count_) + pos_; 130 131 if (currentAvailableSpace < desiredSpace) { 133 134 int doubleBufferSize = (2 * buffer_.length); 138 139 int minumNewBufferSize = (desiredSpace - currentAvailableSpace) + buffer_.length; 140 int newsz = minumNewBufferSize <= doubleBufferSize ? doubleBufferSize : minumNewBufferSize; 141 142 byte[] newBuffer = new byte[newsz]; 143 144 shiftBuffer(newBuffer); 146 } else { 147 148 if (pos_ != 0) { 153 shiftBuffer(buffer_); 154 } 155 } 156 } 157 158 protected int fill(int minimumBytesNeeded) throws DisconnectException { 163 ensureSpaceInBufferForFill(minimumBytesNeeded); 166 167 int totalBytesRead = 0; 170 int actualBytesRead = 0; 171 do { 172 try { 173 actualBytesRead = netAgent_.getInputStream().read(buffer_, count_, buffer_.length - count_); 175 } catch (java.io.IOException ioe) { 176 netAgent_.throwCommunicationsFailure(ioe); 177 } finally { 178 if (agent_.loggingEnabled()) { 179 ((NetLogWriter) netAgent_.logWriter_).traceProtocolFlow(buffer_, 180 count_, 181 actualBytesRead, 182 NetLogWriter.TYPE_TRACE_RECEIVE, 183 "Reply", 184 "fill", 185 2); } 187 } 188 count_ += actualBytesRead; 189 totalBytesRead += actualBytesRead; 190 191 } while ((totalBytesRead < minimumBytesNeeded) && (actualBytesRead != -1)); 192 193 if (actualBytesRead == -1) { 194 if (totalBytesRead < minimumBytesNeeded) { 195 netAgent_.accumulateChainBreakingReadExceptionAndThrow( 196 new DisconnectException(netAgent_, 197 new ClientMessageId(SQLState.NET_INSUFFICIENT_DATA), 198 new Integer (minimumBytesNeeded), 199 new Integer (totalBytesRead))); 200 } 201 } 202 return totalBytesRead; 203 } 204 205 protected final int ensureALayerDataInBuffer(int desiredDataSize) throws DisconnectException { 209 int totalBytesRead = 0; 210 int avail = count_ - pos_; 212 213 if (avail < desiredDataSize) { 215 totalBytesRead = fill(desiredDataSize - avail); 216 } 217 return totalBytesRead; 218 } 219 220 protected final void ensureBLayerDataInBuffer(int desiredDataSize) throws DisconnectException { 221 if (dssIsContinued_ && (desiredDataSize > dssLength_)) { 222 int continueDssHeaderCount = 223 (((desiredDataSize - dssLength_) / 32767) + 1); 224 ensureALayerDataInBuffer(desiredDataSize + (continueDssHeaderCount * 2)); 225 compressBLayerData(continueDssHeaderCount); 226 return; 227 } 228 ensureALayerDataInBuffer(desiredDataSize); 229 } 230 231 private final void compressBLayerData(int continueDssHeaderCount) throws DisconnectException { 239 int tempPos = 0; 240 241 for (int i = 0; i < continueDssHeaderCount; i++) { 243 if (i == 0) { 245 tempPos = pos_ + dssLength_; 247 } else { 248 tempPos += 32767; 250 } 251 } 252 253 int shiftSize = 0; 257 int bytesToShift = 0; 258 int continueHeaderLength = 0; 259 int newDssLength = 0; 260 for (int i = 0; i < continueDssHeaderCount; i++) { 261 262 continueHeaderLength = ((buffer_[tempPos] & 0xFF) << 8) + 263 ((buffer_[tempPos + 1] & 0xFF) << 0); 264 265 if (i == 0) { 266 268 if ((continueHeaderLength & 0x8000) == 0x8000) { 269 continueHeaderLength = 32767; 271 dssIsContinued_ = true; 272 } else { 273 dssIsContinued_ = false; 275 } 276 shiftSize = 2; 278 } else { 279 if ((continueHeaderLength & 0x8000) == 0x8000) { 281 continueHeaderLength = 32767; 282 } else { 283 doSyntaxrmSemantics(CodePoint.SYNERRCD_DSS_LENGTH_BYTE_NUMBER_MISMATCH); 287 } 288 shiftSize += 2; 290 } 291 292 if (continueHeaderLength <= 2) { 294 doSyntaxrmSemantics(CodePoint.SYNERRCD_DSS_CONT_LESS_OR_EQUAL_2); 295 } 296 297 newDssLength += (continueHeaderLength - 2); 298 299 if (i != (continueDssHeaderCount - 1)) { 301 bytesToShift = 32767; 302 } else { 303 bytesToShift = dssLength_; 304 } 305 306 tempPos -= (bytesToShift - 2); 307 System.arraycopy(buffer_, tempPos - shiftSize, buffer_, tempPos , bytesToShift); 308 } 309 pos_ = tempPos; 311 dssLength_ = dssLength_ + newDssLength; 312 } 313 314 protected final void readDssHeader() throws DisconnectException { 315 int correlationID = 0; 316 int nextCorrelationID = 0; 317 ensureALayerDataInBuffer(6); 318 319 dssLength_ = 321 ((buffer_[pos_++] & 0xFF) << 8) + 322 ((buffer_[pos_++] & 0xFF) << 0); 323 324 int oldDssLength = dssLength_; 326 327 if ((dssLength_ & 0x8000) == 0x8000) { 329 dssLength_ = 32767; 330 dssIsContinued_ = true; 331 } else { 332 dssIsContinued_ = false; 333 } 334 335 if (dssLength_ < 6) { 336 doSyntaxrmSemantics(CodePoint.SYNERRCD_DSS_LESS_THAN_6); 337 } 338 339 if ((buffer_[pos_++] & 0xFF) != 0xd0) { 343 doSyntaxrmSemantics(CodePoint.SYNERRCD_CBYTE_NOT_D0); 344 } 345 346 int gdsFormatter = buffer_[pos_++] & 0xFF; 347 if (((gdsFormatter & 0x02) != 0x02) 348 && ((gdsFormatter & 0x03) != 0x03) 349 && ((gdsFormatter & 0x04) != 0x04)) { 350 doSyntaxrmSemantics(CodePoint.SYNERRCD_FBYTE_NOT_SUPPORTED); 351 } 352 353 if ((gdsFormatter & 0x40) == 0x40) { if ((gdsFormatter & 0x10) == 0x10) { 357 dssIsChainedWithSameID_ = true; 358 dssIsChainedWithDiffID_ = false; 359 nextCorrelationID = dssCorrelationID_; 360 } else { 361 dssIsChainedWithSameID_ = false; 362 dssIsChainedWithDiffID_ = true; 363 nextCorrelationID = dssCorrelationID_ + 1; 364 } 365 } else { 366 if ((gdsFormatter & 0x10) == 0x10) { doSyntaxrmSemantics(CodePoint.SYNERRCD_CHAIN_OFF_SAME_NEXT_CORRELATOR); 369 } 370 371 if ((gdsFormatter & 0x20) == 0x20) { doSyntaxrmSemantics(CodePoint.SYNERRCD_CHAIN_OFF_ERROR_CONTINUE); 374 } 375 376 dssIsChainedWithSameID_ = false; 377 dssIsChainedWithDiffID_ = false; 378 nextCorrelationID = 1; 379 } 380 381 correlationID = 382 ((buffer_[pos_++] & 0xFF) << 8) + 383 ((buffer_[pos_++] & 0xFF) << 0); 384 385 if ((correlationID != dssCorrelationID_) && (correlationID != 0xFFFF)) { 387 doSyntaxrmSemantics(CodePoint.SYNERRCD_INVALID_CORRELATOR); 388 } else { 389 dssCorrelationID_ = nextCorrelationID; 390 } 391 dssLength_ -= 6; 392 if ((gdsFormatter & 0x04) == 0x04) { 393 decryptData(gdsFormatter, oldDssLength); } 395 } 400 401 402 private final void decryptData(int gdsFormatter, int oldDssLength) throws DisconnectException { 403 boolean readHeader; 404 405 if (dssLength_ == 32761) { 406 ByteArrayOutputStream baos; 407 int copySize = 0; 408 409 baos = new ByteArrayOutputStream (); 410 411 copySize = dssLength_; 414 do { 415 if (dssIsContinued_) { 417 readHeader = true; 418 } else { 419 readHeader = false; 420 } 421 422 ensureALayerDataInBuffer(copySize); 424 adjustLengths(copySize); 425 baos.write(buffer_, pos_, copySize); 426 pos_ += copySize; 427 428 if (readHeader) { 430 readDSSContinuationHeader(); 431 } 432 433 copySize = dssLength_; 434 } while (readHeader == true); 435 byte[] cipherBytes = baos.toByteArray(); 436 byte[] clearedByte = null; 437 try { 438 clearedByte = netAgent_.netConnection_.getEncryptionManager().decryptData(cipherBytes, 439 NetConfiguration.SECMEC_EUSRIDPWD, 440 netAgent_.netConnection_.getTargetPublicKey(), 441 netAgent_.netConnection_.getTargetPublicKey()); 442 } catch (SqlException e) { 443 } 445 446 longBufferForDecryption_ = new byte[buffer_.length - pos_]; 448 longPosForDecryption_ = 0; 449 count_ = count_ - pos_; 450 longCountForDecryption_ = count_; 451 System.arraycopy(buffer_, pos_, longBufferForDecryption_, 0, buffer_.length - pos_); 452 453 if (clearedByte.length >= 32767) { 455 System.arraycopy(clearedByte, 0, buffer_, 0, 32767); 456 } else { 457 System.arraycopy(clearedByte, 0, buffer_, 0, clearedByte.length); 458 } 459 460 pos_ = 0; 461 dssLength_ = buffer_.length; 462 463 int lobLength = 0; 464 if (clearedByte.length > 32767) { lobLength = ((clearedByte[4] & 0xFF) << 24) + 466 ((clearedByte[5] & 0xFF) << 16) + 467 ((clearedByte[6] & 0xFF) << 8) + 468 ((clearedByte[7] & 0xFF) << 0); 469 longValueForDecryption_ = new byte[lobLength]; 470 System.arraycopy(clearedByte, 8, longValueForDecryption_, 0, clearedByte.length - 8); 471 } else { 472 lobLength = ((clearedByte[0] & 0xFF) << 8) + 473 ((clearedByte[1] & 0xFF) << 0); 474 longValueForDecryption_ = new byte[lobLength - 4]; 475 System.arraycopy(clearedByte, 4, longValueForDecryption_, 0, clearedByte.length - 4); 476 } 477 } else { 478 int bytesRead = ensureALayerDataInBuffer(dssLength_); if (bytesRead > 0) { 481 ensuredLengthForDecryption_ = true; 482 } 483 byte[] encryptedByte = new byte[dssLength_]; 484 System.arraycopy(buffer_, pos_, encryptedByte, 0, dssLength_); 485 byte[] array1 = new byte[pos_]; 486 System.arraycopy(buffer_, 0, array1, 0, pos_); byte[] array3 = new byte[buffer_.length - dssLength_ - pos_]; 488 System.arraycopy(buffer_, pos_ + dssLength_, array3, 0, buffer_.length - dssLength_ - pos_); byte[] clearedByte = null; 490 try { 491 clearedByte = netAgent_.netConnection_.getEncryptionManager().decryptData(encryptedByte, 492 NetConfiguration.SECMEC_EUSRIDPWD, 493 netAgent_.netConnection_.getTargetPublicKey(), 494 netAgent_.netConnection_.getTargetPublicKey()); 495 } catch (SqlException e) { 496 } 498 dssLength_ -= (encryptedByte.length - clearedByte.length); 499 byte[] buffer = new byte[array1.length + clearedByte.length + array3.length]; 500 System.arraycopy(array1, 0, buffer, 0, array1.length); 501 System.arraycopy(clearedByte, 0, buffer, array1.length, clearedByte.length); 502 System.arraycopy(array3, 0, buffer, array1.length + clearedByte.length, array3.length); 503 buffer_ = buffer; 504 int oldCount = count_; 505 count_ = count_ - (encryptedByte.length - clearedByte.length); 506 if (((clearedByte[2] & 0xff) << 8) + ((clearedByte[3] & 0xff) << 0) == 0x146c) { 507 int firstLobLength = ((clearedByte[0] & 0xFF) << 8) + 508 ((clearedByte[1] & 0xFF) << 0); 509 510 boolean flag = false; 511 if (gdsFormatter == 0x54) { 512 flag = true; 513 } 514 if (flag) { 515 if (oldCount - oldDssLength < 6) { 516 int totalBytesRead = fill(6); if (totalBytesRead > 0) { 518 longBufferForDecryption_ = new byte[totalBytesRead]; 519 longPosForDecryption_ = 0; 520 System.arraycopy(buffer_, pos_ + firstLobLength, longBufferForDecryption_, 0, 521 totalBytesRead); 522 } 523 524 } else { 525 longBufferForDecryption_ = new byte[count_ - pos_ - firstLobLength]; 526 longPosForDecryption_ = 0; 527 System.arraycopy(buffer_, pos_ + firstLobLength, longBufferForDecryption_, 0, 528 longBufferForDecryption_.length); 529 530 } 531 } int lobLength = ((clearedByte[0] & 0xFF) << 8) + 533 ((clearedByte[1] & 0xFF) << 0) - 4; 534 535 longValueForDecryption_ = new byte[lobLength]; 536 537 System.arraycopy(clearedByte, 4, longValueForDecryption_, 0, clearedByte.length - 4); } else if (((clearedByte[2] & 0xff) << 8) + ((clearedByte[3] & 0xff) << 0) == 0x241B) { 539 int length = ((clearedByte[0] & 0xFF) << 8) + 540 ((clearedByte[1] & 0xFF) << 0); 541 boolean noData = false; 542 if (clearedByte[4] == -1 && clearedByte[5] == -1) { 543 noData = true; } 545 if (!noData) { 546 if (length == 32776) { 547 length = ((clearedByte[4] & 0xFF) << 24) + 548 ((clearedByte[5] & 0xFF) << 16) + 549 ((clearedByte[6] & 0xFF) << 8) + 550 ((clearedByte[7] & 0xFF) << 0); 551 longValueForDecryption_ = new byte[length]; 552 System.arraycopy(clearedByte, 8, longValueForDecryption_, 0, 553 clearedByte.length - 8); 554 longCountForDecryption_ = count_ - (pos_ + length + 8); 555 longBufferForDecryption_ = new byte[buffer_.length - pos_ - length - 8]; 556 System.arraycopy(buffer_, pos_ + length + 8, longBufferForDecryption_, 0, 557 longBufferForDecryption_.length); 558 559 } else { 560 longPosForDecryption_ = 0; 561 longCountForDecryption_ = count_ - (pos_ + length); 562 longBufferForDecryption_ = new byte[buffer_.length - pos_ - length]; 563 System.arraycopy(buffer_, pos_ + length, longBufferForDecryption_, 0, 564 longBufferForDecryption_.length); 565 566 longValueForDecryption_ = new byte[length - 4]; 567 568 System.arraycopy(clearedByte, 4, longValueForDecryption_, 0, 569 clearedByte.length - 4); 570 } 571 } 572 } 573 } 574 } 575 576 577 final int readUnsignedShort() throws DisconnectException { 578 ensureBLayerDataInBuffer(2); 581 adjustLengths(2); 582 return ((buffer_[pos_++] & 0xff) << 8) + 583 ((buffer_[pos_++] & 0xff) << 0); 584 } 585 586 final short readShort() throws DisconnectException { 587 ensureBLayerDataInBuffer(2); 589 adjustLengths(2); 590 short s = SignedBinary.getShort(buffer_, pos_); 591 592 pos_ += 2; 593 594 return s; 595 } 596 597 final int readInt() throws DisconnectException { 598 ensureBLayerDataInBuffer(4); 600 adjustLengths(4); 601 int i = SignedBinary.getInt(buffer_, pos_); 602 pos_ += 4; 603 604 return i; 605 } 606 607 final void readIntArray(int[] array) throws DisconnectException { 608 ensureBLayerDataInBuffer(array.length * 4); 609 adjustLengths(array.length * 4); 610 611 for (int i = 0; i < array.length; i++) { 612 array[i] = SignedBinary.getInt(buffer_, pos_); 613 pos_ += 4; 614 } 615 } 616 617 618 final long readLong() throws DisconnectException { 619 ensureBLayerDataInBuffer(8); 621 adjustLengths(8); 622 long l = SignedBinary.getLong(buffer_, pos_); 623 624 pos_ += 8; 625 626 return l; 627 } 628 629 630 final int[] readUnsignedShortList() throws DisconnectException { 631 int len = ddmScalarLen_; 632 ensureBLayerDataInBuffer(len); 633 adjustLengths(len); 634 635 int count = len / 2; 636 int[] list = new int[count]; 637 638 for (int i = 0; i < count; i++) { 639 list[i] = ((buffer_[pos_++] & 0xff) << 8) + 640 ((buffer_[pos_++] & 0xff) << 0); 641 } 642 643 return list; 644 } 645 646 final int readUnsignedByte() throws DisconnectException { 647 ensureBLayerDataInBuffer(1); 648 adjustLengths(1); 649 return (buffer_[pos_++] & 0xff); 650 } 651 652 final byte readByte() throws DisconnectException { 653 ensureBLayerDataInBuffer(1); 654 adjustLengths(1); 655 return (byte) (buffer_[pos_++] & 0xff); 656 } 657 658 final boolean readBoolean() throws DisconnectException { 659 ensureBLayerDataInBuffer(1); 660 adjustLengths(1); 661 return buffer_[pos_++] != 0; 662 } 663 664 final String readString(int length) throws DisconnectException { 665 ensureBLayerDataInBuffer(length); 666 adjustLengths(length); 667 668 String result = ccsidManager_.convertToUCS2(buffer_, pos_, length); 669 pos_ += length; 670 return result; 671 } 672 673 final String readString(int length, String encoding) throws DisconnectException { 674 ensureBLayerDataInBuffer(length); 675 adjustLengths(length); 676 String s = null; 677 678 try { 679 s = new String (buffer_, pos_, length, encoding); 680 } catch (java.io.UnsupportedEncodingException e) { 681 agent_.accumulateChainBreakingReadExceptionAndThrow( 682 new DisconnectException(agent_, 683 new ClientMessageId(SQLState.NET_ENCODING_NOT_SUPPORTED), 684 e)); 685 } 686 687 pos_ += length; 688 return s; 689 } 690 691 final String readString() throws DisconnectException { 692 int len = ddmScalarLen_; 693 ensureBLayerDataInBuffer(len); 694 adjustLengths(len); 695 String result = ccsidManager_.convertToUCS2(buffer_, pos_, len); 696 pos_ += len; 697 return result; 698 } 699 700 final byte[] readBytes(int length) throws DisconnectException { 701 ensureBLayerDataInBuffer(length); 702 adjustLengths(length); 703 704 byte[] b = new byte[length]; 705 System.arraycopy(buffer_, pos_, b, 0, length); 706 pos_ += length; 707 return b; 708 } 709 710 final byte[] readBytes() throws DisconnectException { 711 int len = ddmScalarLen_; 712 ensureBLayerDataInBuffer(len); 713 adjustLengths(len); 714 715 byte[] b = new byte[len]; 716 System.arraycopy(buffer_, pos_, b, 0, len); 717 pos_ += len; 718 return b; 719 } 720 721 final byte[] readLDBytes() throws DisconnectException { 722 ensureBLayerDataInBuffer(2); 723 int len = ((buffer_[pos_++] & 0xff) << 8) + ((buffer_[pos_++] & 0xff) << 0); 724 725 if (len == 0) { 726 adjustLengths(2); 727 return null; 728 } 729 730 ensureBLayerDataInBuffer(len); 731 adjustLengths(len + 2); 732 733 byte[] b = new byte[len]; 734 System.arraycopy(buffer_, pos_, b, 0, len); 735 pos_ += len; 736 return b; 737 } 738 739 final void skipBytes(int length) throws DisconnectException { 740 ensureBLayerDataInBuffer(length); 741 adjustLengths(length); 742 pos_ += length; 743 } 744 745 final void skipBytes() throws DisconnectException { 746 int len = ddmScalarLen_; 747 ensureBLayerDataInBuffer(len); 748 adjustLengths(len); 749 pos_ += len; 750 } 751 752 final ByteArrayOutputStream getData(ByteArrayOutputStream existingBuffer) throws DisconnectException { 755 boolean readHeader; 756 int copySize; 757 ByteArrayOutputStream baos; 758 759 if (existingBuffer != null) { 761 baos = existingBuffer; 762 } else { 763 if (ddmScalarLen_ != -1) { 764 baos = new ByteArrayOutputStream (ddmScalarLen_); 766 } else { 767 baos = new ByteArrayOutputStream (); 769 } 771 } 772 773 copySize = dssLength_; 776 do { 777 if (dssIsContinued_) { 779 readHeader = true; 780 } else { 781 readHeader = false; 782 } 783 784 ensureALayerDataInBuffer(copySize); 786 adjustLengths(copySize); 787 baos.write(buffer_, pos_, copySize); 788 pos_ += copySize; 789 790 if (readHeader) { 792 readDSSContinuationHeader(); 793 } 794 795 copySize = dssLength_; 796 } while (readHeader == true); 797 798 return baos; 799 } 800 801 protected final void readDSSContinuationHeader() throws DisconnectException { 807 ensureALayerDataInBuffer(2); 808 809 dssLength_ = 810 ((buffer_[pos_++] & 0xFF) << 8) + 811 ((buffer_[pos_++] & 0xFF) << 0); 812 813 if ((dssLength_ & 0x8000) == 0x8000) { 814 dssLength_ = DssConstants.MAX_DSS_LEN; 815 dssIsContinued_ = true; 816 } else { 817 dssIsContinued_ = false; 818 } 819 if (dssLength_ <= 2) { 822 doSyntaxrmSemantics(CodePoint.SYNERRCD_DSS_CONT_LESS_OR_EQUAL_2); 823 } 824 825 dssLength_ -= 2; } 827 828 829 final void doSyntaxrmSemantics(int syntaxErrorCode) throws DisconnectException { 949 agent_.accumulateChainBreakingReadExceptionAndThrow( 950 new DisconnectException(agent_, 951 new ClientMessageId(SQLState.DRDA_CONNECTION_TERMINATED), 952 SqlException.getMessageUtil().getTextMessage( 953 MessageId.CONN_DRDA_DATASTREAM_SYNTAX_ERROR, 954 new Integer (syntaxErrorCode)))); 955 } 956 957 958 961 protected final void pushLengthOnCollectionStack() { 962 ddmCollectionLenStack_[++topDdmCollectionStack_] = ddmScalarLen_; 963 ddmScalarLen_ = 0; 964 } 965 966 protected final void adjustLengths(int length) { 967 ddmScalarLen_ -= length; 968 adjustCollectionAndDssLengths(length); 969 975 } 976 977 protected int adjustDdmLength(int ddmLength, int length) { 978 ddmLength -= length; 979 if (ddmLength == 0) { 980 adjustLengths(getDdmLength()); 981 } 982 return ddmLength; 983 } 984 985 protected final void popCollectionStack() { 990 topDdmCollectionStack_--; 991 } 992 993 protected final int peekCodePoint() throws DisconnectException { 994 if (topDdmCollectionStack_ != EMPTY_STACK) { 995 if (ddmCollectionLenStack_[topDdmCollectionStack_] == 0) { 996 return END_OF_COLLECTION; 997 } else if (ddmCollectionLenStack_[topDdmCollectionStack_] < 4) { 998 } 1000 } 1001 1002 if ((dssLength_ == 0) && (!dssIsContinued_)) { 1005 if (!dssIsChainedWithSameID_) { 1006 return END_OF_SAME_ID_CHAIN; 1007 } 1008 readDssHeader(); 1009 } 1010 1011 if (longBufferForDecryption_ == null) { 1013 ensureBLayerDataInBuffer(4); 1014 } 1015 peekedLength_ = ((buffer_[pos_] & 0xff) << 8) + ((buffer_[pos_ + 1] & 0xff) << 0); 1016 peekedCodePoint_ = ((buffer_[pos_ + 2] & 0xff) << 8) + ((buffer_[pos_ + 3] & 0xff) << 0); 1017 1018 if ((peekedLength_ & 0x8000) == 0x8000) { 1020 peekExtendedLength(); 1021 } else { 1022 peekedNumOfExtendedLenBytes_ = 0; 1023 } 1024 return peekedCodePoint_; 1025 } 1026 1027 protected final int peekLength() throws DisconnectException { 1029 ensureBLayerDataInBuffer(2); 1030 return (((buffer_[pos_] & 0xff) << 8) + 1031 ((buffer_[pos_ + 1] & 0xff) << 0)); 1032 } 1033 1034 protected final int peekFastBytes(byte[] b, int offset, int length) throws DisconnectException { 1037 for (int i = 0; i < length; i++) { 1038 b[offset + i] = buffer_[pos_ + i]; 1039 } 1040 return offset + length; 1041 } 1042 1043 protected final void parseLengthAndMatchCodePoint(int expectedCodePoint) throws DisconnectException { 1044 int actualCodePoint = 0; 1045 if (peekedCodePoint_ == END_OF_COLLECTION) { 1046 actualCodePoint = readLengthAndCodePoint(); 1047 } else { 1048 actualCodePoint = peekedCodePoint_; 1049 pos_ += (4 + peekedNumOfExtendedLenBytes_); 1050 ddmScalarLen_ = peekedLength_; 1051 if (peekedNumOfExtendedLenBytes_ == 0 && ddmScalarLen_ != -1) { 1052 adjustLengths(4); 1053 } else { 1054 adjustCollectionAndDssLengths(4 + peekedNumOfExtendedLenBytes_); 1055 } 1056 peekedLength_ = 0; 1057 peekedCodePoint_ = END_OF_COLLECTION; 1058 peekedNumOfExtendedLenBytes_ = 0; 1059 } 1060 1061 if (actualCodePoint != expectedCodePoint) { 1062 agent_.accumulateChainBreakingReadExceptionAndThrow( 1063 new DisconnectException(agent_, 1064 new ClientMessageId(SQLState.NET_NOT_EXPECTED_CODEPOINT), 1065 new Integer (actualCodePoint), 1066 new Integer (expectedCodePoint))); 1067 } 1068 } 1069 1070 protected final int readLengthAndCodePoint() throws DisconnectException { 1071 if (topDdmCollectionStack_ != EMPTY_STACK) { 1072 if (ddmCollectionLenStack_[topDdmCollectionStack_] == 0) { 1073 return END_OF_COLLECTION; 1074 } else if (ddmCollectionLenStack_[topDdmCollectionStack_] < 4) { 1075 agent_.accumulateChainBreakingReadExceptionAndThrow( 1076 new DisconnectException(agent_, 1077 new ClientMessageId(SQLState.NET_DDM_COLLECTION_TOO_SMALL))); 1078 } 1079 } 1080 1081 if ((dssLength_ == 0) && (!dssIsContinued_)) { 1084 if (!dssIsChainedWithSameID_) { 1085 return END_OF_SAME_ID_CHAIN; 1086 } 1087 readDssHeader(); 1088 } 1089 1090 ensureBLayerDataInBuffer(4); 1091 ddmScalarLen_ = 1092 ((buffer_[pos_++] & 0xff) << 8) + 1093 ((buffer_[pos_++] & 0xff) << 0); 1094 int codePoint = ((buffer_[pos_++] & 0xff) << 8) + 1095 ((buffer_[pos_++] & 0xff) << 0); 1096 adjustLengths(4); 1097 1098 if ((ddmScalarLen_ & 0x8000) == 0x8000) { 1100 readExtendedLength(); 1101 } 1102 return codePoint; 1103 } 1104 1105 private final void readExtendedLength() throws DisconnectException { 1106 int numberOfExtendedLenBytes = (ddmScalarLen_ - 0x8000); int adjustSize = 0; 1108 switch (numberOfExtendedLenBytes) { 1109 case 4: 1110 ensureBLayerDataInBuffer(4); 1111 ddmScalarLen_ = 1112 ((buffer_[pos_++] & 0xff) << 24) + 1113 ((buffer_[pos_++] & 0xff) << 16) + 1114 ((buffer_[pos_++] & 0xff) << 8) + 1115 ((buffer_[pos_++] & 0xff) << 0); 1116 adjustSize = 4; 1117 break; 1118 case 0: 1119 ddmScalarLen_ = -1; 1120 adjustSize = 0; 1121 break; 1122 default: 1123 doSyntaxrmSemantics(CodePoint.SYNERRCD_INCORRECT_EXTENDED_LEN); 1124 } 1125 1126 adjustCollectionAndDssLengths(adjustSize); 1127 1135 } 1136 1137 private final void adjustCollectionAndDssLengths(int length) { 1138 for (int i = 0; i <= topDdmCollectionStack_; i++) { 1141 ddmCollectionLenStack_[i] -= length; 1142 } 1143 dssLength_ -= length; 1144 } 1145 1146 protected final void startSameIdChainParse() throws DisconnectException { 1147 readDssHeader(); 1148 netAgent_.clearSvrcod(); 1149 } 1150 1151 protected final void endOfSameIdChainData() throws DisconnectException { 1152 netAgent_.targetTypdef_ = netAgent_.originalTargetTypdef_; 1153 netAgent_.targetSqlam_ = netAgent_.orignalTargetSqlam_; 1154 1155 if (this.topDdmCollectionStack_ != Reply.EMPTY_STACK) { 1156 agent_.accumulateChainBreakingReadExceptionAndThrow( 1157 new DisconnectException(agent_, 1158 new ClientMessageId(SQLState.NET_COLLECTION_STACK_NOT_EMPTY))); 1159 } 1160 if (this.dssLength_ != 0) { 1161 agent_.accumulateChainBreakingReadExceptionAndThrow( 1162 new DisconnectException(agent_, 1163 new ClientMessageId(SQLState.NET_DSS_NOT_ZERO))); 1164 } 1165 if (dssIsChainedWithSameID_ == true) { 1166 agent_.accumulateChainBreakingReadExceptionAndThrow( 1167 new DisconnectException(agent_, 1168 new ClientMessageId(SQLState.NET_DSS_CHAINED_WITH_SAME_ID))); 1169 } 1170 } 1171 1172 protected final int peekTotalColumnCount(int tripletLength) throws DisconnectException { 1173 int columnCount = 0; 1174 int offset = 0; 1175 int tripletType = FdocaConstants.CPT_TRIPLET_TYPE; 1176 while (tripletType == FdocaConstants.CPT_TRIPLET_TYPE) { 1177 columnCount += ((tripletLength - 3) / 3); 1178 ensureBLayerDataInBuffer(tripletLength - 3); 1181 offset += (tripletLength - 3); 1182 tripletLength = (buffer_[pos_ + offset++] & 0xff); 1183 tripletType = (buffer_[pos_ + offset++] & 0xff); 1184 offset++; 1186 } 1187 return columnCount; 1188 } 1189 1190 private final void peekExtendedLength() throws DisconnectException { 1191 peekedNumOfExtendedLenBytes_ = (peekedLength_ - 0x8004); 1192 switch (peekedNumOfExtendedLenBytes_) { 1193 case 4: 1194 if (longBufferForDecryption_ == null) { 1201 ensureBLayerDataInBuffer(4 + 4); 1202 } 1203 peekedLength_ = 1208 ((buffer_[pos_ + 4] & 0xff) << 24) + 1209 ((buffer_[pos_ + 5] & 0xff) << 16) + 1210 ((buffer_[pos_ + 6] & 0xff) << 8) + 1211 ((buffer_[pos_ + 7] & 0xff) << 0); 1212 break; 1213 case 0: 1214 peekedLength_ = -1; break; 1216 default: 1217 doSyntaxrmSemantics(CodePoint.SYNERRCD_INCORRECT_EXTENDED_LEN); 1218 } 1219 } 1220 1221 final int readFastUnsignedByte() throws DisconnectException { 1222 return (buffer_[pos_++] & 0xff); 1223 } 1224 1225 final short readFastShort() throws DisconnectException { 1226 short s = SignedBinary.getShort(buffer_, pos_); 1227 pos_ += 2; 1228 return s; 1229 } 1230 1231 final int readFastUnsignedShort() throws DisconnectException { 1232 return ((buffer_[pos_++] & 0xff) << 8) + 1233 ((buffer_[pos_++] & 0xff) << 0); 1234 } 1235 1236 final int readFastInt() throws DisconnectException { 1237 int i = SignedBinary.getInt(buffer_, pos_); 1238 pos_ += 4; 1239 return i; 1240 } 1241 1242 final String readFastString(int length) throws DisconnectException { 1243 String result = ccsidManager_.convertToUCS2(buffer_, pos_, length); 1244 pos_ += length; 1245 return result; 1246 } 1247 1248 final byte[] readFastBytes(int length) throws DisconnectException { 1249 byte[] b = new byte[length]; 1250 System.arraycopy(buffer_, pos_, b, 0, length); 1251 pos_ += length; 1252 return b; 1253 } 1254 1255 protected final int peekFastLength() throws DisconnectException { 1256 return (((buffer_[pos_] & 0xff) << 8) + 1257 ((buffer_[pos_ + 1] & 0xff) << 0)); 1258 } 1259 1260 final void skipFastBytes(int length) throws DisconnectException { 1261 pos_ += length; 1262 } 1263 1264 final void readFastIntArray(int[] array) throws DisconnectException { 1265 for (int i = 0; i < array.length; i++) { 1266 array[i] = SignedBinary.getInt(buffer_, pos_); 1267 pos_ += 4; 1268 } 1269 } 1270 1271 final String readFastString(int length, String encoding) throws DisconnectException { 1272 String s = null; 1273 1274 try { 1275 s = new String (buffer_, pos_, length, encoding); 1276 } catch (java.io.UnsupportedEncodingException e) { 1277 agent_.accumulateChainBreakingReadExceptionAndThrow( 1278 new DisconnectException(agent_, 1279 new ClientMessageId(SQLState.NET_ENCODING_NOT_SUPPORTED), 1280 e)); 1281 } 1282 pos_ += length; 1283 return s; 1284 } 1285 1286 final byte[] readFastLDBytes() throws DisconnectException { 1287 int len = ((buffer_[pos_++] & 0xff) << 8) + ((buffer_[pos_++] & 0xff) << 0); 1288 if (len == 0) { 1289 return null; 1290 } 1291 1292 byte[] b = new byte[len]; 1293 System.arraycopy(buffer_, pos_, b, 0, len); 1294 pos_ += len; 1295 return b; 1296 } 1297 1298 final long readFastLong() throws DisconnectException { 1299 long l = SignedBinary.getLong(buffer_, pos_); 1300 pos_ += 8; 1301 return l; 1302 } 1303 1304 final byte readFastByte() throws DisconnectException { 1305 return (byte) (buffer_[pos_++] & 0xff); 1306 } 1307 1308 final void mark() { 1309 currentPos_ = pos_; 1310 } 1311 1312 final int popMark() { 1314 return currentPos_; 1315 } 1316 1317 final int getFastSkipSQLCARDrowLength() { 1318 return pos_ - popMark(); 1319 } 1320 1321 final ByteArrayOutputStream getFastData(ByteArrayOutputStream existingBuffer) throws DisconnectException { 1324 boolean readHeader; 1325 int copySize; 1326 ByteArrayOutputStream baos; 1327 1328 if (existingBuffer != null) { 1330 baos = existingBuffer; 1331 } else { 1332 if (ddmScalarLen_ != -1) { 1333 baos = new ByteArrayOutputStream (ddmScalarLen_); 1335 } else { 1336 baos = new ByteArrayOutputStream (); 1338 } 1340 } 1341 1342 copySize = dssLength_; 1345 do { 1346 if (dssIsContinued_) { 1348 readHeader = true; 1349 } else { 1350 readHeader = false; 1351 } 1352 1353 adjustLengths(copySize); 1356 baos.write(buffer_, pos_, copySize); 1357 pos_ += copySize; 1358 1359 if (readHeader) { 1361 readDSSContinuationHeader(); 1362 } 1363 1364 copySize = dssLength_; 1365 } while (readHeader == true); 1366 1367 return baos; 1368 } 1369 1370 final protected void matchCodePoint(int expectedCodePoint) throws DisconnectException { 1373 int actualCodePoint = 0; 1374 actualCodePoint = peekedCodePoint_; 1375 pos_ += 4; 1376 if (actualCodePoint != expectedCodePoint) { 1377 agent_.accumulateChainBreakingReadExceptionAndThrow( 1378 new DisconnectException(agent_, 1379 new ClientMessageId(SQLState.NET_NOT_EXPECTED_CODEPOINT), 1380 new Integer (actualCodePoint), 1381 new Integer (expectedCodePoint))); 1382 } 1383 } 1384 1385 1386 protected final int peekNumOfColumns() throws DisconnectException { 1387 int offset = (4 + peekedNumOfExtendedLenBytes_ + 1); 1389 1390 offset = skipSQLDHROW(offset); 1391 1392 return SignedBinary.getShort(buffer_, pos_ + offset); 1393 } 1394 1395 protected final boolean peekForNullSqlcagrp() { 1396 int offset = (4 + peekedNumOfExtendedLenBytes_); 1398 int nullInd = buffer_[pos_ + offset] & 0xff; 1399 return (nullInd == CodePoint.NULLDATA); 1400 } 1401 1402 private final int skipSQLDHROW(int offset) throws DisconnectException { 1403 int sqldhrowgrpNullInd = buffer_[pos_ + offset++] & 0xff; 1404 if (sqldhrowgrpNullInd == CodePoint.NULLDATA) { 1405 return offset; 1406 } 1407 1408 offset += 12; 1409 1410 int stringLength = ((buffer_[pos_ + offset++] & 0xff) << 8) + 1412 ((buffer_[pos_ + offset++] & 0xff) << 0); 1413 offset += stringLength; 1414 1415 stringLength = ((buffer_[pos_ + offset++] & 0xff) << 8) + 1417 ((buffer_[pos_ + offset++] & 0xff) << 0); 1418 offset += stringLength; 1419 1420 stringLength = ((buffer_[pos_ + offset++] & 0xff) << 8) + 1421 ((buffer_[pos_ + offset++] & 0xff) << 0); 1422 offset += stringLength; 1423 1424 return offset; 1425 } 1426} 1427 1428 1429 1430 1431 | Popular Tags |