KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > dcerpc > ndr > NdrBuffer


1 /* jcifs msrpc client library in Java
2  * Copyright (C) 2006 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.dcerpc.ndr;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import jcifs.util.Encdec;
25
26 public class NdrBuffer {
27     int referent;
28     HashMap JavaDoc referents;
29
30     static class Entry {
31         int referent;
32         Object JavaDoc obj;
33     }
34
35     public byte[] buf;
36     public int start;
37     public int index;
38     public int length;
39     public NdrBuffer deferred;
40
41     public NdrBuffer(byte[] buf, int start) {
42         this.buf = buf;
43         this.start = index = start;
44         length = 0;
45         deferred = this;
46     }
47
48     public NdrBuffer derive(int idx) {
49         NdrBuffer nb = new NdrBuffer(buf, start);
50         nb.index = idx;
51         nb.deferred = deferred;
52         return nb;
53     }
54
55
56     
57     public void reset() {
58         this.index = start;
59         length = 0;
60         deferred = this;
61     }
62     public int getIndex() {
63         return index;
64     }
65     public void setIndex(int index) {
66         this.index = index;
67     }
68     public int getCapacity() {
69         return buf.length - start;
70     }
71     public int getTailSpace() {
72         return buf.length - index;
73     }
74     public byte[] getBuffer() {
75         return buf;
76     }
77     public int align(int boundary, byte value) {
78         int n = align(boundary);
79         int i = n;
80         while (i > 0) {
81             buf[index - i] = value;
82             i--;
83         }
84         return n;
85     }
86     public void writeOctetArray(byte[] b, int i, int l) {
87         System.arraycopy(b, i, buf, index, l);
88         advance(l);
89     }
90     public void readOctetArray(byte[] b, int i, int l) {
91         System.arraycopy(buf, index, b, i, l);
92         advance(l);
93     }
94
95
96     public int getLength() {
97         return deferred.length;
98     }
99     public void advance(int n) {
100         index += n;
101         if ((index - start) > deferred.length) {
102             deferred.length = index - start;
103         }
104     }
105     public int align(int boundary) {
106         int m = boundary - 1;
107         int i = index - start;
108         int n = ((i + m) & ~m) - i;
109         advance(n);
110         return n;
111     }
112     public void enc_ndr_small(int s) {
113         buf[index] = (byte)(s & 0xFF);
114         advance(1);
115     }
116     public int dec_ndr_small() {
117         int val = buf[index] & 0xFF;
118         advance(1);
119         return val;
120     }
121     public void enc_ndr_short(int s) {
122         align(2);
123         Encdec.enc_uint16le((short)s, buf, index);
124         advance(2);
125     }
126     public int dec_ndr_short() {
127         align(2);
128         int val = Encdec.dec_uint16le(buf, index);
129         advance(2);
130         return val;
131     }
132     public void enc_ndr_long(int l) {
133         align(4);
134         Encdec.enc_uint32le(l, buf, index);
135         advance(4);
136     }
137     public int dec_ndr_long() {
138         align(4);
139         int val = Encdec.dec_uint32le(buf, index);
140         advance(4);
141         return val;
142     }
143     public void enc_ndr_hyper(long h) {
144         align(8);
145         Encdec.enc_uint64le(h, buf, index);
146         advance(8);
147     }
148     public long dec_ndr_hyper() {
149         align(8);
150         long val = Encdec.dec_uint64le(buf, index);
151         advance(8);
152         return val;
153     }
154     /* float */
155     /* double */
156     public void enc_ndr_string(String JavaDoc s) {
157         align(4);
158         int i = index;
159         int len = s.length();
160         Encdec.enc_uint32le(len + 1, buf, i); i += 4;
161         Encdec.enc_uint32le(0, buf, i); i += 4;
162         Encdec.enc_uint32le(len + 1, buf, i); i += 4;
163         try {
164             System.arraycopy(s.getBytes("UnicodeLittleUnmarked"), 0, buf, i, len * 2);
165         } catch( UnsupportedEncodingException JavaDoc uee ) {
166         }
167         i += len * 2;
168         buf[i++] = (byte)'\0';
169         buf[i++] = (byte)'\0';
170         advance(i - index);
171     }
172     public String JavaDoc dec_ndr_string() throws NdrException {
173         align(4);
174         int i = index;
175         String JavaDoc val = null;
176         int len = Encdec.dec_uint32le(buf, i);
177         i += 12;
178         if (len != 0) {
179             len--;
180             int size = len * 2;
181             try {
182                 if (size < 0 || size > 0xFFFF) throw new NdrException( NdrException.INVALID_CONFORMANCE );
183                 val = new String JavaDoc(buf, i, size, "UnicodeLittleUnmarked");
184                 i += size + 2;
185             } catch( UnsupportedEncodingException JavaDoc uee ) {
186             }
187         }
188         advance(i - index);
189         return val;
190     }
191     private int getDceReferent(Object JavaDoc obj) {
192         Entry e;
193
194         if (referents == null) {
195             referents = new HashMap JavaDoc();
196             referent = 1;
197         }
198
199         if ((e = (Entry)referents.get(obj)) == null) {
200             e = new Entry();
201             e.referent = referent++;
202             e.obj = obj;
203             referents.put(obj, e);
204         }
205
206         return e.referent;
207     }
208     public void enc_ndr_referent(Object JavaDoc obj, int type) {
209         if (obj == null) {
210             enc_ndr_long(0);
211             return;
212         }
213         switch (type) {
214             case 1: /* unique */
215             case 3: /* ref */
216                 enc_ndr_long(System.identityHashCode(obj));
217                 return;
218             case 2: /* ptr */
219                 enc_ndr_long(getDceReferent(obj));
220                 return;
221         }
222     }
223
224     public String JavaDoc toString() {
225         return "start=" + start + ",index=" + index + ",length=" + getLength();
226     }
227 }
228
229
Popular Tags