001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // This program is free software; you can redistribute it and/or modify
004: // it under the terms of the GNU General Public License and GNU Library
005: // General Public License as published by the Free Software Foundation;
006: // either version 2, or (at your option) any later version.
007: //
008: // This program is distributed in the hope that it will be useful,
009: // but WITHOUT ANY WARRANTY; without even the implied warranty of
010: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: // GNU General Public License and GNU Library General Public License
012: // for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // and GNU Library General Public License along with this program; if
016: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
017: // MA 02139, USA.
018: //
019: ///////////////////////////////////////////////////////////////////////////////
020:
021: package org.rdesktop.server.rdp;
022:
023: import java.io.*;
024:
025: public class RdpPacket extends RdpAbstractPacket {
026: private byte[] m_bb = null;
027: private int m_size = -1;
028: private int m_position = -1;
029:
030: public RdpPacket(int capacity) {
031: m_bb = new byte[capacity];
032: m_size = capacity;
033: m_position = 0;
034: }
035:
036: public int capacity() {
037: return m_size;
038: }
039:
040: public void set8(int where, int what) {
041: if (where < 0 || where >= m_size) {
042: throw new ArrayIndexOutOfBoundsException(
043: "memory accessed out of Range!");
044: }
045:
046: m_bb[where] = (byte) what;
047: }
048:
049: public void set8(int what) {
050: if (m_position >= m_size) {
051: throw new ArrayIndexOutOfBoundsException(
052: "memory accessed out of Range!");
053: }
054:
055: m_bb[m_position++] = (byte) what;
056: }
057:
058: public int get8(int where) {
059: if (where < 0 || where >= m_size) {
060: throw new ArrayIndexOutOfBoundsException(
061: "memory accessed out of Range!");
062: }
063:
064: return m_bb[where] & 0xff;
065: }
066:
067: public int get8() {
068: if (m_position >= m_size) {
069: throw new ArrayIndexOutOfBoundsException(
070: "memory accessed out of Range!");
071: }
072:
073: return m_bb[m_position++] & 0xff;
074: }
075:
076: public void copyFromByteArray(byte[] array, int array_offset,
077: int mem_offset, int len) {
078: if ((array_offset >= array.length)
079: || (array_offset + len > array.length)
080: || (mem_offset + len > m_size)) {
081: throw new ArrayIndexOutOfBoundsException(
082: "memory accessed out of Range!");
083: }
084:
085: System.arraycopy(array, array_offset, m_bb, mem_offset, len);
086: }
087:
088: public void copyToByteArray(byte[] array, int array_offset,
089: int mem_offset, int len) {
090: if ((array_offset >= array.length)
091: || (array_offset + len > array.length)
092: || (mem_offset + len > m_size)) {
093: throw new ArrayIndexOutOfBoundsException(
094: "memory accessed out of Range!");
095: }
096:
097: System.arraycopy(m_bb, mem_offset, array, array_offset, len);
098: }
099:
100: public void copyToPacket(RdpPacket dst, int srcOffset,
101: int dstOffset, int len) {
102: System.arraycopy(m_bb, srcOffset, dst.m_bb, dstOffset, len);
103: }
104:
105: public void copyFromPacket(RdpPacket src, int srcOffset,
106: int dstOffset, int len) {
107: System.arraycopy(src.m_bb, srcOffset, m_bb, dstOffset, len);
108: }
109:
110: public int getSize() {
111: return m_size;
112: }
113:
114: public int getPosition() {
115: return m_position;
116: }
117:
118: public int getLittleEndian16(int where) {
119: return (m_bb[where] & 0xff) + ((m_bb[where + 1] & 0xff) << 8);
120: }
121:
122: public int getLittleEndian16() {
123: return (m_bb[m_position++] & 0xff)
124: + ((m_bb[m_position++] & 0xff) << 8);
125: }
126:
127: public int getBigEndian16(int where) {
128: return ((m_bb[where] & 0xff) << 8) + (m_bb[where + 1] & 0xff);
129: }
130:
131: public int getBigEndian16() {
132: return ((m_bb[m_position++] & 0xff) << 8)
133: + (m_bb[m_position++] & 0xff);
134: }
135:
136: public void setLittleEndian16(int where, int what) {
137: m_bb[where] = (byte) (what & 0xff);
138: m_bb[where + 1] = (byte) ((what >> 8) & 0xff);
139: }
140:
141: public void setLittleEndian16(int what) {
142: m_bb[m_position++] = (byte) (what & 0xff);
143: m_bb[m_position++] = (byte) ((what >> 8) & 0xff);
144: }
145:
146: public void setBigEndian16(int where, int what) {
147: m_bb[where] = (byte) ((what >> 8) & 0xff);
148: m_bb[where + 1] = (byte) (what & 0xff);
149: }
150:
151: public void setBigEndian16(int what) {
152: m_bb[m_position++] = (byte) ((what >> 8) & 0xff);
153: m_bb[m_position++] = (byte) (what & 0xff);
154: }
155:
156: public int getLittleEndian32(int where) {
157: return (m_bb[where] & 0xff) + ((m_bb[where + 1] & 0xff) << 8)
158: + ((m_bb[where + 2] & 0xff) << 16)
159: + ((m_bb[where + 3] & 0xff) << 24);
160: }
161:
162: public int getLittleEndian32() {
163: return (m_bb[m_position++] & 0xff)
164: + ((m_bb[m_position++] & 0xff) << 8)
165: + ((m_bb[m_position++] & 0xff) << 16)
166: + ((m_bb[m_position++] & 0xff) << 24);
167: }
168:
169: public int getBigEndian32(int where) {
170: return ((m_bb[where] & 0xff) << 24)
171: + ((m_bb[where + 1] & 0xff) << 16)
172: + ((m_bb[where + 2] & 0xff) << 8)
173: + (m_bb[where + 3] & 0xff);
174: }
175:
176: public int getBigEndian32() {
177: return ((m_bb[m_position++] & 0xff) << 24)
178: + ((m_bb[m_position++] & 0xff) << 16)
179: + ((m_bb[m_position++] & 0xff) << 8)
180: + (m_bb[m_position++] & 0xff);
181: }
182:
183: public void setLittleEndian32(int where, int what) {
184: m_bb[where] = (byte) (what & 0xff);
185: m_bb[where + 1] = (byte) ((what >> 8) & 0xff);
186: m_bb[where + 2] = (byte) ((what >> 16) & 0xff);
187: m_bb[where + 3] = (byte) ((what >> 24) & 0xff);
188: }
189:
190: public void setLittleEndian32(int what) {
191: m_bb[m_position++] = (byte) (what & 0xff);
192: m_bb[m_position++] = (byte) ((what >> 8) & 0xff);
193: m_bb[m_position++] = (byte) ((what >> 16) & 0xff);
194: m_bb[m_position++] = (byte) ((what >> 24) & 0xff);
195: }
196:
197: public void setBigEndian32(int where, int what) {
198: m_bb[where] = (byte) ((what >> 24) & 0xff);
199: m_bb[where + 1] = (byte) ((what >> 16) & 0xff);
200: m_bb[where + 2] = (byte) ((what >> 8) & 0xff);
201: m_bb[where + 3] = (byte) (what & 0xff);
202: }
203:
204: public void setBigEndian32(int what) {
205: m_bb[m_position++] = (byte) ((what >> 24) & 0xff);
206: m_bb[m_position++] = (byte) ((what >> 16) & 0xff);
207: m_bb[m_position++] = (byte) ((what >> 8) & 0xff);
208: m_bb[m_position++] = (byte) (what & 0xff);
209: }
210:
211: public void incrementPosition(int length) {
212: if ((length > m_size) || (length + m_position > m_size)
213: || (length < 0)) {
214: throw new ArrayIndexOutOfBoundsException();
215: }
216:
217: m_position += length;
218: }
219:
220: public void setPosition(int position) {
221: if ((position > m_size) || (position < 0)) {
222: throw new ArrayIndexOutOfBoundsException();
223: }
224:
225: m_position = position;
226: }
227:
228: public RdpPacket copy() {
229: RdpPacket dup = new RdpPacket(m_bb.length);
230: dup.m_mcs = m_mcs;
231: dup.m_secure = m_secure;
232: dup.m_rdp = m_rdp;
233: dup.m_channel = m_channel;
234: dup.m_start = m_start;
235: dup.m_end = m_end;
236: dup.m_size = m_size;
237: dup.m_position = m_position;
238: System.arraycopy(m_bb, 0, dup.m_bb, 0, m_bb.length);
239: return dup;
240: }
241: }
|