1 17 package org.alfresco.filesys.smb.mailslot; 18 19 import org.alfresco.filesys.util.DataPacker; 20 21 24 public class SMBMailslotPacket 25 { 26 28 public static final int SIGNATURE = 0; 29 public static final int COMMAND = 4; 30 public static final int ERRORCODE = 5; 31 public static final int ERRORCLASS = 5; 32 public static final int ERROR = 7; 33 public static final int FLAGS = 9; 34 public static final int FLAGS2 = 10; 35 public static final int PIDHIGH = 12; 36 public static final int SID = 18; 37 public static final int SEQNO = 20; 38 public static final int TID = 24; 39 public static final int PID = 26; 40 public static final int UID = 28; 41 public static final int MID = 30; 42 public static final int WORDCNT = 32; 43 public static final int ANDXCOMMAND = 33; 44 public static final int ANDXRESERVED= 34; 45 public static final int PARAMWORDS = 33; 46 47 49 public static final int TRANS_HEADERLEN = 66; 50 51 53 public static final int MIN_RXLEN = 32; 54 55 57 public static final int DEFAULT_BUFSIZE = 500; 58 59 61 public static final int FLG_SUBDIALECT = 0x01; 62 public static final int FLG_CASELESS = 0x08; 63 public static final int FLG_CANONICAL = 0x10; 64 public static final int FLG_OPLOCK = 0x20; 65 public static final int FLG_NOTIFY = 0x40; 66 public static final int FLG_RESPONSE = 0x80; 67 68 70 public static final int FLG2_LONGFILENAMES = 0x0001; 71 public static final int FLG2_EXTENDEDATTRIB = 0x0002; 72 public static final int FLG2_READIFEXE = 0x2000; 73 public static final int FLG2_LONGERRORCODE = 0x4000; 74 public static final int FLG2_UNICODE = 0x8000; 75 76 78 private byte[] m_smbbuf; 79 private int m_offset; 80 81 83 private static final int STD_PARAMS = 14; 84 85 87 public static final int Transaction = 0x25; 88 public static final int Transaction2 = 0x32; 89 90 93 public SMBMailslotPacket() 94 { 95 m_smbbuf = new byte[DEFAULT_BUFSIZE]; 96 m_offset = 0; 97 } 98 99 104 public SMBMailslotPacket(byte[] buf) 105 { 106 m_smbbuf = buf; 107 m_offset = 0; 108 } 109 110 116 public SMBMailslotPacket(byte[] buf, int off) 117 { 118 m_smbbuf = buf; 119 m_offset = off; 120 } 121 122 128 public final void resetPacket(byte[] buf, int offset) 129 { 130 m_smbbuf = buf; 131 m_offset = offset; 132 } 133 134 139 public final int getAndXCommand() 140 { 141 return (int) (m_smbbuf[ANDXCOMMAND + m_offset] & 0xFF); 142 } 143 144 149 public final byte[] getBuffer() 150 { 151 return m_smbbuf; 152 } 153 154 159 public final int getBufferLength() 160 { 161 return m_smbbuf.length - m_offset; 162 } 163 164 169 public final int getByteCount() 170 { 171 172 174 int pos = PARAMWORDS + (2 * getParameterCount()); 175 return (int) DataPacker.getIntelShort(m_smbbuf, pos); 176 } 177 178 183 public final int getByteOffset() 184 { 185 186 188 int pCnt = getParameterCount(); 189 int pos = WORDCNT + (2 * pCnt) + 3 + m_offset; 190 return pos; 191 } 192 193 198 public final int getCommand() 199 { 200 return (int) (m_smbbuf[COMMAND + m_offset] & 0xFF); 201 } 202 203 208 public final boolean hasLongErrorCode() 209 { 210 if ((getFlags2() & FLG2_LONGERRORCODE) == 0) 211 return false; 212 return true; 213 } 214 215 220 public final int getErrorClass() 221 { 222 return (int) m_smbbuf[ERRORCLASS + m_offset] & 0xFF; 223 } 224 225 230 public final int getErrorCode() 231 { 232 return (int) m_smbbuf[ERROR + m_offset] & 0xFF; 233 } 234 235 240 public final int getFlags() 241 { 242 return (int) m_smbbuf[FLAGS + m_offset] & 0xFF; 243 } 244 245 250 public final int getFlags2() 251 { 252 return (int) DataPacker.getIntelShort(m_smbbuf, FLAGS2 + m_offset); 253 } 254 255 260 public final int getLength() 261 { 262 return (getByteOffset() + getByteCount()) - m_offset; 263 } 264 265 270 public final int getLongErrorCode() 271 { 272 return DataPacker.getIntelInt(m_smbbuf, ERRORCODE + m_offset); 273 } 274 275 280 public final int getMultiplexId() 281 { 282 return DataPacker.getIntelShort(m_smbbuf, MID + m_offset); 283 } 284 285 292 public final int getParameter(int idx) throws java.lang.IndexOutOfBoundsException 293 { 294 295 297 if (idx > getParameterCount()) 298 throw new java.lang.IndexOutOfBoundsException (); 299 300 302 int pos = WORDCNT + (2 * idx) + 1 + m_offset; 303 return (int) (DataPacker.getIntelShort(m_smbbuf, pos) & 0xFFFF); 304 } 305 306 311 public final int getParameterCount() 312 { 313 return (int) m_smbbuf[WORDCNT + m_offset]; 314 } 315 316 321 public final int getProcessId() 322 { 323 return DataPacker.getIntelShort(m_smbbuf, PID + m_offset); 324 } 325 326 331 public final int getTreeId() 332 { 333 return DataPacker.getIntelShort(m_smbbuf, TID + m_offset); 334 } 335 336 341 public final int getUserId() 342 { 343 return DataPacker.getIntelShort(m_smbbuf, UID + m_offset); 344 } 345 346 353 public final int getDataBlockOffset() 354 { 355 356 358 int pos = (getParameterBlockOffset() + getParameter(3)) + m_offset; 359 if ((pos & 0x01) != 0) 360 pos++; 361 return pos; 362 } 363 364 372 public final int getDataBlockOffset(int prmLen) 373 { 374 375 377 int pos = getParameterBlockOffset() + prmLen; 378 if ((pos & 0x01) != 0) 379 pos++; 380 return pos; 381 } 382 383 389 public final int getParameterBlockOffset() 390 { 391 392 394 int pos = getByteOffset() + m_offset; 395 if ((pos & 0x01) != 0) 396 pos++; 397 return pos; 398 } 399 400 405 public final int getRxDataBlock() 406 { 407 return getParameter(12) + m_offset; 408 } 409 410 415 public final int getRxDataBlockLength() 416 { 417 return getParameter(11); 418 } 419 420 426 public final int getRxParameter(int prmIdx) 427 { 428 429 431 int pos = getRxParameterBlock(); 432 433 435 pos += prmIdx * 2; return DataPacker.getIntelShort(getBuffer(), pos); 437 } 438 439 444 public final int getRxParameterBlock() 445 { 446 447 449 return getParameter(10) + m_offset; 450 } 451 452 457 public final int getRxParameterBlockLength() 458 { 459 return getParameter(9); 460 } 461 462 467 public final int getRxParameterCount() 468 { 469 return getParameterCount() - STD_PARAMS; 470 } 471 472 478 public final int getRxParameterInt(int prmIdx) 479 { 480 481 483 int pos = getRxParameterBlock(); 484 485 487 pos += prmIdx * 2; return DataPacker.getIntelInt(getBuffer(), pos); 489 } 490 491 497 public final String getRxParameterString(int pos) 498 { 499 500 502 pos += getRxParameterBlock(); 503 504 506 byte[] buf = getBuffer(); 507 int len = (buf[pos++] & 0x00FF); 508 return DataPacker.getString(buf, pos, len); 509 } 510 511 518 public final String getRxParameterString(int pos, int len) 519 { 520 521 523 pos += getRxParameterBlock(); 524 525 527 byte[] buf = getBuffer(); 528 return DataPacker.getString(buf, pos, len); 529 } 530 531 536 public final String getRxTransactName() 537 { 538 539 541 if (getCommand() == Transaction2) 542 return ""; 543 544 546 int pos = getByteOffset(); 547 return DataPacker.getString(getBuffer(), pos, getByteCount()); 548 } 549 550 555 public final int getSetupParameter(int idx) throws java.lang.ArrayIndexOutOfBoundsException 556 { 557 558 560 if (idx >= getRxParameterCount()) 561 throw new java.lang.ArrayIndexOutOfBoundsException (); 562 563 565 return getParameter(idx + STD_PARAMS); 566 } 567 568 573 public final int getMailslotOpcode() 574 { 575 try 576 { 577 return getSetupParameter(0); 578 } 579 catch (ArrayIndexOutOfBoundsException ex) 580 { 581 } 582 return -1; 583 } 584 585 590 public final int getMailslotPriority() 591 { 592 try 593 { 594 return getSetupParameter(1); 595 } 596 catch (ArrayIndexOutOfBoundsException ex) 597 { 598 } 599 return -1; 600 } 601 602 607 public final int getMailslotClass() 608 { 609 try 610 { 611 return getSetupParameter(2); 612 } 613 catch (ArrayIndexOutOfBoundsException ex) 614 { 615 } 616 return -1; 617 } 618 619 624 public final int getMailslotSubOpcode() 625 { 626 return (int) (m_smbbuf[getMailslotDataOffset()] & 0xFF); 627 } 628 629 634 public final int getMailslotDataOffset() 635 { 636 return getRxDataBlock(); 637 } 638 639 646 public final void initializeMailslotSMB(String name, byte[] data, int dlen) 647 { 648 649 651 initializeBuffer(); 652 653 655 setFlags(0); 656 setFlags2(0); 657 setUserId(0); 658 setMultiplexId(0); 659 setTreeId(0); 660 setProcessId(0); 661 662 664 initializeTransact(name, 17, null, 0, data, dlen); 665 666 668 setSetupParameter(0, MailSlot.WRITE); 669 setSetupParameter(1, 1); 670 setSetupParameter(2, MailSlot.UNRELIABLE); 671 } 672 673 683 protected final void initializeTransact(String name, int pcnt, byte[] paramblk, int plen, byte[] datablk, int dlen) 684 { 685 686 688 if (name == null) 689 setCommand(Transaction2); 690 else 691 setCommand(Transaction); 692 693 695 setParameterCount(pcnt); 696 697 699 setParameter(0, plen); setParameter(1, dlen); 702 for (int i = 2; i < 9; setParameter(i++, 0)) 703 ; 704 705 setParameter(6, 1000); setParameter(9, plen); setParameter(11, dlen); 709 setParameter(13, pcnt - STD_PARAMS); 711 713 int pos = getByteOffset(); 714 int startPos = pos; 715 716 718 int idx; 719 byte[] buf = getBuffer(); 720 721 if (name != null) 722 { 723 724 726 byte[] nam = name.getBytes(); 727 728 for (idx = 0; idx < nam.length; idx++) 729 buf[pos++] = nam[idx]; 730 } 731 732 734 if ((pos % 2) > 0) 735 pos++; 736 737 739 if (paramblk != null) 740 { 741 742 744 setParameter(10, pos - m_offset); 745 746 748 for (idx = 0; idx < plen; idx++) 749 buf[pos++] = paramblk[idx]; 750 } 751 else 752 { 753 754 756 setParameter(10, 0); 757 } 758 759 761 if ((pos % 2) > 0) 762 pos++; 763 764 766 if (datablk != null) 767 { 768 769 771 setParameter(12, pos - m_offset); 772 773 775 for (idx = 0; idx < dlen; idx++) 776 buf[pos++] = datablk[idx]; 777 } 778 else 779 { 780 781 783 setParameter(12, 0); 784 } 785 786 788 setByteCount(pos - startPos); 789 } 790 791 796 public final void setAndXCommand(int cmd) 797 { 798 m_smbbuf[ANDXCOMMAND + m_offset] = (byte) cmd; 799 m_smbbuf[ANDXRESERVED + m_offset] = (byte) 0; 800 } 801 802 807 public final void setByteCount(int cnt) 808 { 809 int offset = getByteOffset() - 2; 810 DataPacker.putIntelShort(cnt, m_smbbuf, offset); 811 } 812 813 818 public final void setBytes(byte[] byts) 819 { 820 int offset = getByteOffset() - 2; 821 DataPacker.putIntelShort(byts.length, m_smbbuf, offset); 822 823 offset += 2; 824 825 for (int idx = 0; idx < byts.length; m_smbbuf[offset + idx] = byts[idx++]) 826 ; 827 } 828 829 834 public final void setCommand(int cmd) 835 { 836 m_smbbuf[COMMAND + m_offset] = (byte) cmd; 837 } 838 839 844 public final void setErrorClass(int cl) 845 { 846 m_smbbuf[ERRORCLASS + m_offset] = (byte) (cl & 0xFF); 847 } 848 849 854 public final void setErrorCode(int sts) 855 { 856 m_smbbuf[ERROR + m_offset] = (byte) (sts & 0xFF); 857 } 858 859 864 public final void setFlags(int flg) 865 { 866 m_smbbuf[FLAGS + m_offset] = (byte) flg; 867 } 868 869 874 public final void setFlags2(int flg) 875 { 876 DataPacker.putIntelShort(flg, m_smbbuf, FLAGS2 + m_offset); 877 } 878 879 884 public final void setMultiplexId(int mid) 885 { 886 DataPacker.putIntelShort(mid, m_smbbuf, MID + m_offset); 887 } 888 889 895 public final void setParameter(int idx, int val) 896 { 897 int pos = WORDCNT + (2 * idx) + 1 + m_offset; 898 DataPacker.putIntelShort(val, m_smbbuf, pos); 899 } 900 901 906 public final void setParameterCount(int cnt) 907 { 908 m_smbbuf[WORDCNT + m_offset] = (byte) cnt; 909 } 910 911 916 public final void setProcessId(int pid) 917 { 918 DataPacker.putIntelShort(pid, m_smbbuf, PID + m_offset); 919 } 920 921 926 public final void setSeqNo(int seq) 927 { 928 DataPacker.putIntelShort(seq, m_smbbuf, SEQNO + m_offset); 929 } 930 931 936 public final void setSID(int sid) 937 { 938 DataPacker.putIntelShort(sid, m_smbbuf, SID + m_offset); 939 } 940 941 946 public final void setTreeId(int tid) 947 { 948 DataPacker.putIntelShort(tid, m_smbbuf, TID + m_offset); 949 } 950 951 956 public final void setUserId(int uid) 957 { 958 DataPacker.putIntelShort(uid, m_smbbuf, UID + m_offset); 959 } 960 961 967 public final void setSetupParameter(int idx, int val) 968 { 969 setParameter(STD_PARAMS + idx, val); 970 } 971 972 975 private final void initializeBuffer() 976 { 977 978 980 m_smbbuf[SIGNATURE + m_offset] = (byte) 0xFF; 981 m_smbbuf[SIGNATURE + 1 + m_offset] = (byte) 'S'; 982 m_smbbuf[SIGNATURE + 2 + m_offset] = (byte) 'M'; 983 m_smbbuf[SIGNATURE + 3 + m_offset] = (byte) 'B'; 984 } 985 } | Popular Tags |