1 17 package org.alfresco.filesys.smb.dcerpc; 18 19 import org.alfresco.filesys.smb.NTTime; 20 import org.alfresco.filesys.smb.SMBStatus; 21 import org.alfresco.filesys.smb.TransactBuffer; 22 import org.alfresco.filesys.util.DataBuffer; 23 import org.alfresco.filesys.util.DataPacker; 24 import org.alfresco.filesys.util.HexDump; 25 26 29 public class DCEBuffer 30 { 31 32 34 public static final int HDR_VERMAJOR = 0; 35 public static final int HDR_VERMINOR = 1; 36 public static final int HDR_PDUTYPE = 2; 37 public static final int HDR_FLAGS = 3; 38 public static final int HDR_DATAREP = 4; 39 public static final int HDR_FRAGLEN = 5; 40 public static final int HDR_AUTHLEN = 6; 41 public static final int HDR_CALLID = 7; 42 public static final int HDR_ALLOCHINT = 8; 43 public static final int HDR_OPCODE = 9; 44 45 47 public static final int FLG_FIRSTFRAG = 0x01; 48 public static final int FLG_LASTFRAG = 0x02; 49 public static final int FLG_CANCEL = 0x04; 50 public static final int FLG_IDEMPOTENT = 0x20; 51 public static final int FLG_BROADCAST = 0x40; 52 53 public static final int FLG_ONLYFRAG = 0x03; 54 55 57 public static final int VERSIONMAJOR = 0; 58 public static final int VERSIONMINOR = 1; 59 public static final int PDUTYPE = 2; 60 public static final int HEADERFLAGS = 3; 61 public static final int PACKEDDATAREP = 4; 62 public static final int FRAGMENTLEN = 8; 63 public static final int AUTHLEN = 10; 64 public static final int CALLID = 12; 65 public static final int DCEDATA = 16; 66 67 69 public static final int ALLOCATIONHINT = 16; 70 public static final int PRESENTIDENT = 20; 71 public static final int OPERATIONID = 22; 72 public static final int OPERATIONDATA = 24; 73 74 76 private static final byte VAL_VERSIONMAJOR = 5; 77 private static final byte VAL_VERSIONMINOR = 0; 78 private static final int VAL_PACKEDDATAREP = 0x00000010; 79 80 82 public final static int ALIGN_NONE = -1; 83 public final static int ALIGN_SHORT = 0; 84 public final static int ALIGN_INT = 1; 85 public final static int ALIGN_LONG = 2; 86 87 89 public final static int MAX_STRING_LEN = 1000; 90 91 93 private final static int[] _alignMask = { 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8 }; 94 private final static int[] _alignRound = { 1, 3, 7 }; 95 96 98 private static final int DEFAULT_BUFSIZE = 8192; 99 100 102 private static final int MAX_BUFFER_SIZE = 65536; 103 104 106 private static final int DUMMY_ADDRESS = 0x12345678; 107 108 110 private byte[] m_buffer; 111 private int m_base; 112 private int m_pos; 113 private int m_rdpos; 114 115 117 private int m_errorCode; 118 119 122 public DCEBuffer() 123 { 124 m_buffer = new byte[DEFAULT_BUFSIZE]; 125 m_pos = 0; 126 m_rdpos = 0; 127 m_base = 0; 128 } 129 130 135 public DCEBuffer(int siz) 136 { 137 m_buffer = new byte[siz]; 138 m_pos = 0; 139 m_rdpos = 0; 140 m_base = 0; 141 } 142 143 150 public DCEBuffer(byte[] buf, int startPos, int len) 151 { 152 m_buffer = buf; 153 m_pos = startPos + len; 154 m_rdpos = startPos; 155 m_base = startPos; 156 } 157 158 164 public DCEBuffer(byte[] buf, int startPos) 165 { 166 m_buffer = buf; 167 m_pos = startPos; 168 m_rdpos = startPos; 169 m_base = startPos; 170 } 171 172 177 public DCEBuffer(TransactBuffer tbuf) 178 { 179 DataBuffer dataBuf = tbuf.getDataBuffer(); 180 m_buffer = dataBuf.getBuffer(); 181 m_rdpos = dataBuf.getOffset(); 182 m_base = dataBuf.getOffset(); 183 m_pos = m_rdpos + dataBuf.getLength(); 184 } 185 186 191 public final byte[] getBuffer() 192 { 193 return m_buffer; 194 } 195 196 201 public final int getLength() 202 { 203 return m_pos; 204 } 205 206 211 public final int getReadPosition() 212 { 213 return m_rdpos; 214 } 215 216 221 public final int getWritePosition() 222 { 223 return m_pos; 224 } 225 226 231 public final int getAvailableLength() 232 { 233 return m_pos - m_rdpos; 234 } 235 236 243 public final int getByte(int align) throws DCEBufferException 244 { 245 246 248 if (m_buffer.length - m_rdpos < 1) 249 throw new DCEBufferException("End of DCE buffer"); 250 251 253 int bval = (int) (m_buffer[m_rdpos++] & 0xFF); 254 alignRxPosition(align); 255 return bval; 256 } 257 258 266 public final byte[] getBytes(byte[] buf, int len) throws DCEBufferException 267 { 268 269 271 if (m_buffer.length - m_rdpos < len) 272 throw new DCEBufferException("End of DCE buffer"); 273 274 276 if (buf == null) 277 buf = new byte[len]; 278 279 281 for (int i = 0; i < len; i++) 282 buf[i] = m_buffer[m_rdpos++]; 283 return buf; 284 } 285 286 292 public final int getShort() throws DCEBufferException 293 { 294 295 297 if (m_buffer.length - m_rdpos < 2) 298 throw new DCEBufferException("End of DCE buffer"); 299 300 302 int sval = (int) DataPacker.getIntelShort(m_buffer, m_rdpos); 303 m_rdpos += 2; 304 return sval; 305 } 306 307 314 public final int getShort(int align) throws DCEBufferException 315 { 316 317 319 int sval = getShort(); 320 321 323 alignRxPosition(align); 324 325 327 return sval; 328 } 329 330 336 public final int getInt() throws DCEBufferException 337 { 338 339 341 if (m_buffer.length - m_rdpos < 4) 342 throw new DCEBufferException("End of DCE buffer"); 343 344 346 int ival = DataPacker.getIntelInt(m_buffer, m_rdpos); 347 m_rdpos += 4; 348 return ival; 349 } 350 351 357 public final int getPointer() throws DCEBufferException 358 { 359 return getInt(); 360 } 361 362 369 public final String getStringPointer() throws DCEBufferException 370 { 371 if (getInt() == 0) 372 return null; 373 return ""; 374 } 375 376 383 public final String getCharArrayPointer() throws DCEBufferException 384 { 385 386 388 int len = getShort(); 389 int siz = getShort(); 390 return getStringPointer(); 391 } 392 393 402 public final String getCharArrayNotNull(String strVar, int align) throws DCEBufferException 403 { 404 405 407 String str = ""; 408 409 if (strVar != null) 410 { 411 412 414 str = getCharArray(); 415 416 418 alignRxPosition(align); 419 } 420 421 423 return str; 424 } 425 426 432 public final long getLong() throws DCEBufferException 433 { 434 435 437 if (m_buffer.length - m_rdpos < 8) 438 throw new DCEBufferException("End of DCE buffer"); 439 440 442 long lval = DataPacker.getIntelLong(m_buffer, m_rdpos); 443 m_rdpos += 8; 444 return lval; 445 } 446 447 453 public final int getHeaderValue(int valTyp) 454 { 455 456 int result = -1; 457 458 switch (valTyp) 459 { 460 461 463 case HDR_VERMAJOR: 464 result = (int) (m_buffer[m_base + VERSIONMAJOR] & 0xFF); 465 break; 466 467 469 case HDR_VERMINOR: 470 result = (int) (m_buffer[m_base + VERSIONMINOR] & 0xFF); 471 break; 472 473 475 case HDR_PDUTYPE: 476 result = (int) (m_buffer[m_base + PDUTYPE] & 0xFF); 477 break; 478 479 481 case HDR_FLAGS: 482 result = (int) (m_buffer[m_base + HEADERFLAGS] & 0xFF); 483 break; 484 485 487 case HDR_DATAREP: 488 result = DataPacker.getIntelInt(m_buffer, m_base + VERSIONMINOR); 489 break; 490 491 493 case HDR_AUTHLEN: 494 result = DataPacker.getIntelInt(m_buffer, m_base + AUTHLEN); 495 break; 496 497 499 case HDR_FRAGLEN: 500 result = DataPacker.getIntelInt(m_buffer, m_base + FRAGMENTLEN); 501 break; 502 503 505 case HDR_CALLID: 506 result = DataPacker.getIntelInt(m_buffer, m_base + CALLID); 507 break; 508 509 511 case HDR_ALLOCHINT: 512 result = DataPacker.getIntelInt(m_buffer, m_base + ALLOCATIONHINT); 513 break; 514 515 517 case HDR_OPCODE: 518 result = DataPacker.getIntelShort(m_buffer, m_base + OPERATIONID); 519 break; 520 } 521 522 524 return result; 525 } 526 527 533 public final void setHeaderValue(int typ, int val) 534 { 535 536 switch (typ) 537 { 538 539 541 case HDR_VERMAJOR: 542 m_buffer[m_base + VERSIONMAJOR] = (byte) (val & 0xFF); 543 break; 544 545 547 case HDR_VERMINOR: 548 m_buffer[m_base + VERSIONMINOR] = (byte) (val & 0xFF); 549 break; 550 551 553 case HDR_PDUTYPE: 554 m_buffer[m_base + PDUTYPE] = (byte) (val & 0xFF); 555 break; 556 557 559 case HDR_FLAGS: 560 m_buffer[m_base + HEADERFLAGS] = (byte) (val & 0xFF); 561 break; 562 563 565 case HDR_DATAREP: 566 DataPacker.putIntelInt(val, m_buffer, m_base + PACKEDDATAREP); 567 break; 568 569 571 case HDR_AUTHLEN: 572 DataPacker.putIntelInt(val, m_buffer, m_base + AUTHLEN); 573 break; 574 575 577 case HDR_FRAGLEN: 578 DataPacker.putIntelInt(val, m_buffer, m_base + FRAGMENTLEN); 579 break; 580 581 583 case HDR_CALLID: 584 DataPacker.putIntelInt(val, m_buffer, m_base + CALLID); 585 break; 586 587 589 case HDR_ALLOCHINT: 590 DataPacker.putIntelInt(val, m_buffer, m_base + ALLOCATIONHINT); 591 break; 592 593 595 case HDR_OPCODE: 596 DataPacker.putIntelShort(val, m_buffer, m_base + OPERATIONID); 597 break; 598 } 599 } 600 601 606 public final boolean isFirstFragment() 607 { 608 if ((getHeaderValue(HDR_FLAGS) & FLG_FIRSTFRAG) != 0) 609 return true; 610 return false; 611 } 612 613 618 public final boolean isLastFragment() 619 { 620 if ((getHeaderValue(HDR_FLAGS) & FLG_LASTFRAG) != 0) 621 return true; 622 return false; 623 } 624 625 630 public final boolean isOnlyFragment() 631 { 632 if ((getHeaderValue(HDR_FLAGS) & FLG_ONLYFRAG) == FLG_ONLYFRAG) 633 return true; 634 return false; 635 } 636 637 642 public final boolean hasMoreEntries() 643 { 644 return getStatusCode() == SMBStatus.Win32MoreEntries ? true : false; 645 } 646 647 652 public final boolean hasSuccessStatus() 653 { 654 return getStatusCode() == SMBStatus.NTSuccess ? true : false; 655 } 656 657 663 public final void skipBytes(int cnt) throws DCEBufferException 664 { 665 666 668 if (m_buffer.length - m_rdpos < cnt) 669 throw new DCEBufferException("End of DCE buffer"); 670 671 673 m_rdpos += cnt; 674 } 675 676 681 public final void skipPointer() throws DCEBufferException 682 { 683 684 686 if (m_buffer.length - m_rdpos < 4) 687 throw new DCEBufferException("End of DCE buffer"); 688 689 691 m_rdpos += 4; 692 } 693 694 700 public final void positionAt(int pos) throws DCEBufferException 701 { 702 703 705 if (m_buffer.length < pos) 706 throw new DCEBufferException("End of DCE buffer"); 707 708 710 m_rdpos = pos; 711 } 712 713 720 public final String getChars(int len) throws DCEBufferException 721 { 722 723 725 if (m_buffer.length - m_rdpos < (len * 2)) 726 throw new DCEBufferException("End of DCE buffer"); 727 728 730 StringBuffer str = new StringBuffer (len); 731 char curChar; 732 733 while (len-- > 0) 734 { 735 736 738 curChar = (char) ((m_buffer[m_rdpos + 1] << 8) + m_buffer[m_rdpos]); 739 m_rdpos += 2; 740 741 743 str.append(curChar); 744 } 745 746 748 return str.toString(); 749 } 750 751 756 public final int getStatusCode() 757 { 758 759 761 int ival = DataPacker.getIntelInt(m_buffer, m_pos - 4); 762 return ival; 763 } 764 765 771 public final String getString() throws DCEBufferException 772 { 773 774 776 if (m_buffer.length - m_rdpos < 12) 777 throw new DCEBufferException("End of DCE buffer"); 778 779 781 int maxLen = getInt(); 782 skipBytes(4); int strLen = getInt(); 784 785 String str = DataPacker.getUnicodeString(m_buffer, m_rdpos, strLen); 786 m_rdpos += (strLen * 2); 787 return str; 788 } 789 790 796 public final String getCharArray() throws DCEBufferException 797 { 798 799 801 if (m_buffer.length - m_rdpos < 12) 802 throw new DCEBufferException("End of DCE buffer"); 803 804 806 int maxLen = getInt(); 807 skipBytes(4); int strLen = getInt(); 810 String str = null; 811 if (strLen > 0) 812 { 813 str = DataPacker.getUnicodeString(m_buffer, m_rdpos, strLen); 814 m_rdpos += (strLen * 2); 815 } 816 return str; 817 } 818 819 826 public final String getCharArray(int align) throws DCEBufferException 827 { 828 829 831 String str = getCharArray(); 832 833 835 alignRxPosition(align); 836 837 839 return str; 840 } 841 842 849 public final String getString(int align) throws DCEBufferException 850 { 851 852 854 String str = getString(); 855 856 858 alignRxPosition(align); 859 860 862 return str; 863 } 864 865 874 public final String getStringNotNull(String strVar, int align) throws DCEBufferException 875 { 876 877 879 String str = ""; 880 881 if (strVar != null) 882 { 883 884 886 str = getString(); 887 888 890 alignRxPosition(align); 891 } 892 893 895 return str; 896 } 897 898 905 public final String getStringAt(int pos) throws DCEBufferException 906 { 907 908 910 if (m_buffer.length < pos) 911 throw new DCEBufferException("Buffer offset out of range, " + pos); 912 913 915 String str = DataPacker.getUnicodeString(m_buffer, pos, MAX_STRING_LEN); 916 return str; 917 } 918 919 926 public final int getUnicodeHeaderLength() throws DCEBufferException 927 { 928 929 931 if (m_buffer.length - m_rdpos < 8) 932 throw new DCEBufferException("End of DCE buffer"); 933 934 936 int len = (int) DataPacker.getIntelShort(m_buffer, m_rdpos); 937 m_rdpos += 4; int ptr = DataPacker.getIntelInt(m_buffer, m_rdpos); 939 m_rdpos += 4; 940 941 943 if (ptr == 0) 944 return -1; 945 return len; 946 } 947 948 954 public final String getUnicodeString() throws DCEBufferException 955 { 956 957 959 if (m_buffer.length - m_rdpos <= 0) 960 throw new DCEBufferException("No more buffer"); 961 962 964 String str = DataPacker.getUnicodeString(m_buffer, m_rdpos, MAX_STRING_LEN); 965 if (str != null) 966 m_rdpos += (str.length() * 2) + 2; 967 return str; 968 } 969 970 977 public final byte[] getDataBlock(int align) throws DCEBufferException 978 { 979 980 982 if (m_buffer.length - m_rdpos < 12) 983 throw new DCEBufferException("End of DCE buffer"); 984 985 987 int len = getInt(); 988 m_rdpos += 8; 990 992 byte[] dataBlk = null; 993 994 if (len > 0) 995 { 996 997 999 dataBlk = new byte[len]; 1000 1001 1003 System.arraycopy(m_buffer, m_rdpos, dataBlk, 0, len); 1004 } 1005 1006 1008 m_rdpos += len; 1009 alignRxPosition(align); 1010 return dataBlk; 1011 } 1012 1013 1020 public final UUID getUUID(boolean readVer) throws DCEBufferException 1021 { 1022 1023 1025 int len = UUID.UUID_LENGTH_BINARY; 1026 if (readVer == true) 1027 len += 4; 1028 1029 if (m_buffer.length - m_rdpos < len) 1030 throw new DCEBufferException("End of DCE buffer"); 1031 1032 1034 UUID uuid = new UUID(m_buffer, m_rdpos); 1035 m_rdpos += UUID.UUID_LENGTH_BINARY; 1036 1037 if (readVer == true) 1038 { 1039 int ver = getInt(); 1040 uuid.setVersion(ver); 1041 } 1042 1043 return uuid; 1044 } 1045 1046 1052 public final long getNTTime() throws DCEBufferException 1053 { 1054 1055 1057 long ntTime = getLong(); 1058 if (ntTime == 0 || ntTime == NTTime.InfiniteTime) 1059 return ntTime; 1060 1061 1063 return NTTime.toJavaDate(ntTime); 1064 } 1065 1066 1072 public final byte[] getByteStructure(byte[] buf) throws DCEBufferException 1073 { 1074 1075 1077 if (m_buffer.length - m_rdpos < 12) 1078 throw new DCEBufferException("End of DCE buffer"); 1079 1080 1082 int maxLen = getInt(); 1083 skipBytes(4); int bytLen = getInt(); 1085 1086 byte[] bytBuf = buf; 1087 if (bytBuf.length < bytLen) 1088 bytBuf = new byte[bytLen]; 1089 return getBytes(bytBuf, bytLen); 1090 } 1091 1092 1098 public final void getHandle(PolicyHandle handle) throws DCEBufferException 1099 { 1100 1101 1103 if (m_buffer.length - m_rdpos < PolicyHandle.POLICY_HANDLE_SIZE) 1104 throw new DCEBufferException("End of DCE buffer"); 1105 1106 1108 m_rdpos = handle.loadPolicyHandle(m_buffer, m_rdpos); 1109 } 1110 1111 1120 public final int copyData(byte[] buf, int off, int cnt) throws DCEBufferException 1121 { 1122 1123 1125 if (m_rdpos == m_pos) 1126 return 0; 1127 1128 1130 int siz = m_pos - m_rdpos; 1131 if (siz > cnt) 1132 siz = cnt; 1133 1134 1136 System.arraycopy(m_buffer, m_rdpos, buf, off, siz); 1137 m_rdpos += siz; 1138 1139 1141 return siz; 1142 } 1143 1144 1152 public final void appendData(byte[] buf, int off, int len) throws DCEBufferException 1153 { 1154 1155 1157 if (m_buffer.length - m_pos < len) 1158 extendBuffer(len); 1159 1160 1162 System.arraycopy(buf, off, m_buffer, m_pos, len); 1163 m_pos += len; 1164 } 1165 1166 1171 public final void putInt(int ival) 1172 { 1173 1174 1176 if (m_buffer.length - m_pos < 4) 1177 extendBuffer(); 1178 1179 1181 DataPacker.putIntelInt(ival, m_buffer, m_pos); 1182 m_pos += 4; 1183 } 1184 1185 1190 public final void putByte(int bval) 1191 { 1192 1193 1195 if (m_buffer.length - m_pos < 1) 1196 extendBuffer(); 1197 1198 1200 m_buffer[m_pos++] = (byte) (bval & 0xFF); 1201 } 1202 1203 1209 public final void putByte(byte bval, int align) 1210 { 1211 1212 1214 if (m_buffer.length - m_pos < 1) 1215 extendBuffer(); 1216 1217 1219 m_buffer[m_pos++] = bval; 1220 alignPosition(align); 1221 } 1222 1223 1229 public final void putByte(int bval, int align) 1230 { 1231 1232 1234 if (m_buffer.length - m_pos < 1) 1235 extendBuffer(); 1236 1237 1239 m_buffer[m_pos++] = (byte) (bval & 0xFF); 1240 alignPosition(align); 1241 } 1242 1243 1249 public final void putBytes(byte[] bval, int len) 1250 { 1251 1252 1254 if (m_buffer.length - m_pos < len) 1255 extendBuffer(); 1256 1257 1259 for (int i = 0; i < len; i++) 1260 m_buffer[m_pos++] = bval[i]; 1261 } 1262 1263 1270 public final void putBytes(byte[] bval, int len, int align) 1271 { 1272 1273 1275 if (m_buffer.length - m_pos < len) 1276 extendBuffer(); 1277 1278 1280 for (int i = 0; i < len; i++) 1281 m_buffer[m_pos++] = bval[i]; 1282 1283 1285 alignPosition(align); 1286 } 1287 1288 1293 public final void putShort(int sval) 1294 { 1295 1296 1298 if (m_buffer.length - m_pos < 2) 1299 extendBuffer(); 1300 1301 1303 DataPacker.putIntelShort(sval, m_buffer, m_pos); 1304 m_pos += 2; 1305 } 1306 1307 1312 public final void putString(String str) 1313 { 1314 1315 1317 int reqLen = (str.length() * 2) + 24; 1318 1319 if (m_buffer.length - m_pos < reqLen) 1320 extendBuffer(reqLen); 1321 1322 1324 m_pos = DCEDataPacker.putDCEString(m_buffer, m_pos, str, false); 1325 } 1326 1327 1333 public final void putString(String str, int align) 1334 { 1335 1336 1338 int reqLen = (str.length() * 2) + 24; 1339 1340 if (m_buffer.length - m_pos < reqLen) 1341 extendBuffer(reqLen); 1342 1343 1345 m_pos = DCEDataPacker.putDCEString(m_buffer, m_pos, str, false); 1346 1347 1349 alignPosition(align); 1350 } 1351 1352 1360 public final void putString(String str, int align, boolean incNul) 1361 { 1362 1363 1365 int reqLen = (str.length() * 2) + 24; 1366 if (incNul) 1367 reqLen += 2; 1368 1369 if (m_buffer.length - m_pos < reqLen) 1370 extendBuffer(reqLen); 1371 1372 1374 m_pos = DCEDataPacker.putDCEString(m_buffer, m_pos, str, incNul); 1375 1376 1378 alignPosition(align); 1379 } 1380 1381 1388 public final void putStringReturn(int len, int align) 1389 { 1390 1391 1393 if (m_buffer.length - m_pos < 20) 1394 extendBuffer(); 1395 1396 1398 DataPacker.putIntelInt(len, m_buffer, m_pos); 1399 DataPacker.putZeros(m_buffer, m_pos + 4, 8); 1400 DataPacker.putIntelInt(DUMMY_ADDRESS, m_buffer, m_pos + 12); 1401 m_pos += 16; 1402 1403 1405 alignPosition(align); 1406 } 1407 1408 1414 public final void putUnicodeHeader(String str, boolean incNul) 1415 { 1416 1417 1419 if (m_buffer.length - m_pos < 8) 1420 extendBuffer(); 1421 1422 1424 int sLen = 0; 1425 if (str != null) 1426 sLen = str.length() * 2; 1427 1428 1430 if (str != null) 1431 DataPacker.putIntelShort(incNul ? sLen + 2 : sLen, m_buffer, m_pos); 1432 else 1433 DataPacker.putIntelShort(0, m_buffer, m_pos); 1434 1435 DataPacker.putIntelShort(sLen != 0 ? sLen + 2 : 0, m_buffer, m_pos + 2); 1436 DataPacker.putIntelInt(str != null ? DUMMY_ADDRESS : 0, m_buffer, m_pos + 4); 1437 1438 m_pos += 8; 1439 } 1440 1441 1447 public final void putUnicodeReturn(int len) 1448 { 1449 1450 1452 if (m_buffer.length - m_pos < 8) 1453 extendBuffer(); 1454 1455 1457 DataPacker.putIntelShort(0, m_buffer, m_pos); 1458 DataPacker.putIntelShort(len, m_buffer, m_pos + 2); 1459 DataPacker.putIntelInt(DUMMY_ADDRESS, m_buffer, m_pos + 4); 1460 1461 m_pos += 8; 1462 } 1463 1464 1470 public final void putUnicodeHeader(int len) 1471 { 1472 1473 1475 if (m_buffer.length - m_pos < 8) 1476 extendBuffer(); 1477 1478 1480 int sLen = len * 2; 1481 1482 1484 DataPacker.putIntelShort(sLen, m_buffer, m_pos); 1485 DataPacker.putIntelShort(sLen + 2, m_buffer, m_pos + 2); 1486 DataPacker.putIntelInt(sLen != 0 ? DUMMY_ADDRESS : 0, m_buffer, m_pos + 4); 1487 1488 m_pos += 8; 1489 } 1490 1491 1497 public final void putASCIIString(String str, boolean incNul) 1498 { 1499 1500 1502 if (m_buffer.length - m_pos < (str.length() + 1)) 1503 extendBuffer(str.length() + 2); 1504 1505 1507 m_pos = DataPacker.putString(str, m_buffer, m_pos, incNul); 1508 } 1509 1510 1517 public final void putASCIIString(String str, boolean incNul, int align) 1518 { 1519 1520 1522 if (m_buffer.length - m_pos < (str.length() + 1)) 1523 extendBuffer(str.length() + 8); 1524 1525 1527 m_pos = DataPacker.putString(str, m_buffer, m_pos, incNul); 1528 1529 1531 alignPosition(align); 1532 } 1533 1534 1539 public final void putPointer(Object obj) 1540 { 1541 1542 1544 if (m_buffer.length - m_pos < 4) 1545 extendBuffer(); 1546 1547 1549 if (obj == null) 1550 DataPacker.putZeros(m_buffer, m_pos, 4); 1551 else 1552 DataPacker.putIntelInt(DUMMY_ADDRESS, m_buffer, m_pos); 1553 m_pos += 4; 1554 } 1555 1556 1561 public final void putPointer(boolean notNull) 1562 { 1563 1564 1566 if (m_buffer.length - m_pos < 4) 1567 extendBuffer(); 1568 1569 1571 if (notNull == false) 1572 DataPacker.putZeros(m_buffer, m_pos, 4); 1573 else 1574 DataPacker.putIntelInt(DUMMY_ADDRESS, m_buffer, m_pos); 1575 m_pos += 4; 1576 } 1577 1578 1584 public final void putUUID(UUID uuid, boolean writeVer) 1585 { 1586 1587 1589 int len = UUID.UUID_LENGTH_BINARY; 1590 if (writeVer == true) 1591 len += 4; 1592 1593 if (m_buffer.length - m_pos < len) 1594 extendBuffer(); 1595 1596 1598 m_pos = uuid.storeUUID(m_buffer, m_pos, writeVer); 1599 } 1600 1601 1606 public final void putHandle(PolicyHandle handle) 1607 { 1608 1609 1611 if (m_buffer.length - m_pos < PolicyHandle.POLICY_HANDLE_SIZE) 1612 extendBuffer(PolicyHandle.POLICY_HANDLE_SIZE); 1613 1614 1616 m_pos = handle.storePolicyHandle(m_buffer, m_pos); 1617 } 1618 1619 1624 public final void putBuffer(DCEBuffer buf) 1625 { 1626 try 1627 { 1628 appendData(buf.getBuffer(), buf.getReadPosition(), buf.getLength()); 1629 } 1630 catch (DCEBufferException ex) 1631 { 1632 } 1633 } 1634 1635 1640 public final void putErrorStatus(int sts) 1641 { 1642 1643 1645 if (m_buffer.length - m_pos < 4) 1646 extendBuffer(); 1647 1648 1650 DataPacker.putIntelInt(sts, m_buffer, m_pos); 1651 m_pos += 4; 1652 1653 1655 m_errorCode = sts; 1656 } 1657 1658 1664 public final void putHeader(int pdutyp, int callid) 1665 { 1666 m_buffer[m_pos++] = VAL_VERSIONMAJOR; 1667 m_buffer[m_pos++] = VAL_VERSIONMINOR; 1668 m_buffer[m_pos++] = (byte) (pdutyp & 0xFF); 1669 m_buffer[m_pos++] = 0; 1670 1671 DataPacker.putIntelInt(VAL_PACKEDDATAREP, m_buffer, m_pos); 1672 m_pos += 4; 1673 1674 DataPacker.putZeros(m_buffer, m_pos, 4); 1675 m_pos += 4; 1676 1677 DataPacker.putIntelInt(callid, m_buffer, m_pos); 1678 m_pos += 4; 1679 } 1680 1681 1686 public final void putBindHeader(int callid) 1687 { 1688 putHeader(DCECommand.BIND, callid); 1689 } 1690 1691 1696 public final void putBindAckHeader(int callid) 1697 { 1698 putHeader(DCECommand.BINDACK, callid); 1699 } 1700 1701 1708 public final void putRequestHeader(int callid, int opcode, int allocHint) 1709 { 1710 putHeader(DCECommand.REQUEST, callid); 1711 DataPacker.putIntelInt(allocHint, m_buffer, m_pos); 1712 m_pos += 4; 1713 DataPacker.putZeros(m_buffer, m_pos, 2); 1714 m_pos += 2; 1715 DataPacker.putIntelShort(opcode, m_buffer, m_pos); 1716 m_pos += 2; 1717 } 1718 1719 1725 public final void putResponseHeader(int callid, int allocHint) 1726 { 1727 putHeader(DCECommand.RESPONSE, callid); 1728 DataPacker.putIntelInt(allocHint, m_buffer, m_pos); 1729 m_pos += 4; 1730 DataPacker.putZeros(m_buffer, m_pos, 4); 1731 m_pos += 4; 1732 } 1733 1734 1739 public final void putZeroInts(int cnt) 1740 { 1741 1742 1744 int bytCnt = cnt * 4; 1745 if (m_buffer.length - m_pos < bytCnt) 1746 extendBuffer(bytCnt * 2); 1747 1748 1750 DataPacker.putZeros(m_buffer, m_pos, bytCnt); 1751 m_pos += bytCnt; 1752 } 1753 1754 1757 public final void resetBuffer() 1758 { 1759 1760 1762 m_pos = 0; 1763 m_rdpos = 0; 1764 1765 1767 if (m_buffer.length >= MAX_BUFFER_SIZE) 1768 m_buffer = new byte[DEFAULT_BUFSIZE]; 1769 } 1770 1771 1776 public final void setWritePosition(int pos) 1777 { 1778 m_pos = pos; 1779 } 1780 1781 1786 public final void updateWritePosition(int len) 1787 { 1788 m_pos += len; 1789 } 1790 1791 1796 public final boolean hasErrorStatus() 1797 { 1798 return m_errorCode != 0 ? true : false; 1799 } 1800 1801 1806 public final int getErrorStatus() 1807 { 1808 return m_errorCode; 1809 } 1810 1811 1816 public final void setErrorStatus(int sts) 1817 { 1818 m_errorCode = sts; 1819 } 1820 1821 1826 private final void extendBuffer(int ext) 1827 { 1828 1829 1831 byte[] newBuf = new byte[m_buffer.length + ext]; 1832 1833 1835 System.arraycopy(m_buffer, 0, newBuf, 0, m_buffer.length); 1836 1837 1839 m_buffer = newBuf; 1840 } 1841 1842 1845 private final void extendBuffer() 1846 { 1847 extendBuffer(m_buffer.length * 2); 1848 } 1849 1850 1855 private final void alignPosition(int align) 1856 { 1857 1858 1860 if (align < 0 || align > 2) 1861 return; 1862 1863 1865 m_pos = (m_pos + _alignRound[align]) & _alignMask[align]; 1866 } 1867 1868 1873 private final void alignRxPosition(int align) 1874 { 1875 1876 1878 if (align < 0 || align > 2 || m_rdpos >= m_buffer.length) 1879 return; 1880 1881 1883 m_rdpos = (m_rdpos + _alignRound[align]) & _alignMask[align]; 1884 } 1885 1886 1889 public final void Dump() 1890 { 1891 int len = getLength(); 1892 if (len == 0) 1893 len = 24; 1894 HexDump.Dump(getBuffer(), len, m_base); 1895 } 1896} 1897 | Popular Tags |