KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > dcerpc > DCEDataPacker


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.dcerpc;
18
19 import org.alfresco.filesys.util.DataPacker;
20
21 /**
22  * DCE Data Packer Class
23  */

24 public class DCEDataPacker
25 {
26
27     /**
28      * Unpack a DCE string from the buffer
29      *
30      * @param buf byte[]
31      * @param off int
32      * @return String
33      * @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
34      */

35     public final static String JavaDoc getDCEString(byte[] buf, int off) throws IndexOutOfBoundsException JavaDoc
36     {
37
38         // Check if the buffer is big enough to hold the String header
39

40         if (buf.length < off + 12)
41             throw new IndexOutOfBoundsException JavaDoc();
42
43         // Get the maximum and actual string length
44

45         int maxLen = DataPacker.getIntelInt(buf, off);
46         int strLen = DataPacker.getIntelInt(buf, off + 8);
47
48         // Read the Unicode string
49

50         return DataPacker.getUnicodeString(buf, off + 12, strLen);
51     }
52
53     /**
54      * Pack a DCE string into the buffer
55      *
56      * @param buf byte[]
57      * @param off int
58      * @param str String
59      * @param incNul boolean
60      * @return int
61      */

62     public final static int putDCEString(byte[] buf, int off, String JavaDoc str, boolean incNul)
63     {
64
65         // Pack the string header
66

67         DataPacker.putIntelInt(str.length() + 1, buf, off);
68         DataPacker.putZeros(buf, off + 4, 4);
69
70         if (incNul == false)
71             DataPacker.putIntelInt(str.length(), buf, off + 8);
72         else
73             DataPacker.putIntelInt(str.length() + 1, buf, off + 8);
74
75         // Pack the string
76

77         return DataPacker.putUnicodeString(str, buf, off + 12, incNul);
78     }
79
80     /**
81      * Align a buffer offset on a longword boundary
82      *
83      * @param pos int
84      * @return int
85      */

86     public final static int wordAlign(int pos)
87     {
88         return (pos + 1) & 0xFFFFFFFE;
89     }
90
91     /**
92      * Align a buffer offset on a longword boundary
93      *
94      * @param pos int
95      * @return int
96      */

97     public final static int longwordAlign(int pos)
98     {
99         return (pos + 3) & 0xFFFFFFFC;
100     }
101 }
102
Popular Tags