summaryrefslogtreecommitdiff
path: root/src/protocol/internal/fqterm_ssh_buffer.cpp
blob: 9775891ddf1aaa5d543c9140e4e2c9a3b5a095af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/***************************************************************************
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 *   Copyright (C) 2008 fqterm development group.                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 ***************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <vector>

#include "fqterm_trace.h"
#include "fqterm_ssh_buffer.h"
#include "fqterm_serialization.h"

namespace FQTerm {
//==============================================================================
// FQTermSSHBuffer
//==============================================================================

FQTermSSHBuffer::FQTermSSHBuffer(int size) {
  alloc_size_ = size;
  buffer_size_ = 0;
  offset_ = 0;
  buffer_ = new u_char[alloc_size_];
}

FQTermSSHBuffer::~FQTermSSHBuffer() {
  delete [] buffer_;
  offset_ = 0;
  buffer_size_ = 0;
  alloc_size_ = 0;
}

void FQTermSSHBuffer::ensure(int len) {
  if (len <= (alloc_size_ - (offset_ + buffer_size_))) {
    return ;
  } else {
    alloc_size_ = buffer_size_ + len;
    rebuffer();
  }
}

void FQTermSSHBuffer::rebuffer() {
  u_char *newBuffer;
  newBuffer = new u_char[alloc_size_];
  memset(newBuffer, 0, alloc_size_);
  memcpy(newBuffer, buffer_ + offset_, buffer_size_);
  delete [] buffer_;
  buffer_ = newBuffer;
  offset_ = 0;
}

void FQTermSSHBuffer::clear() {
  memset(buffer_, 0, alloc_size_);
  offset_ = 0;
  buffer_size_ = 0;
}

void FQTermSSHBuffer::consume(int len) {
  if (len > buffer_size_) {
    len = buffer_size_;
  } 
  
  offset_ += len;
  buffer_size_ -= len;
}

void FQTermSSHBuffer::putRawData(const char *data, int len) {
  if (len < 0) {
    FQ_TRACE("sshbuffer", 0) << "Write data error.";
  }

  ensure(len);
  memcpy((buffer_ + offset_ + buffer_size_), data, len);
  buffer_size_ += len;
}

void FQTermSSHBuffer::getRawData(char *data, int len) {
  if (len <= buffer_size_ && len >= 0) {
    memcpy(data, buffer_ + offset_, len);
    consume(len);
  } else {
    FQ_TRACE("sshbuffer", 0) << "Read too many data: " << len << " bytes.";
  } 
}

//==============================================================================
// Store an BIGNUM in the buffer with a 2-byte msb first bit count, followed by
// (bits+7)/8 bytes of binary data, msb first.
//==============================================================================

void FQTermSSHBuffer::putSSH1BN(BIGNUM *bignum) {
  int bits = BN_num_bits(bignum);
  int bin_size = (bits + 7) / 8;
  u_char *buf = new u_char[bin_size];
  int oi;
  u_char msg[2];

  // Get the value of in binary
  oi = BN_bn2bin(bignum, buf);
  if (oi != bin_size) {
    FQ_TRACE("sshbuffer", 0) << "BN_bn2bin() failed: oi = " << oi
                             << " != bin_size." << bin_size;
  } 

  // Store the number of bits in the buffer in two bytes, msb first
  htonu16(msg, bits);
  putRawData((char*)msg, 2);
  // Store the binary data.
  putRawData((char*)buf, oi);

  memset(buf, 0, bin_size);
  delete [] buf;
}

//==============================================================================
// Retrieves a BIGNUM from the buffer.
//==============================================================================

void FQTermSSHBuffer::getSSH1BN(BIGNUM *bignum) {
  int bits, bytes;
  u_char buf[2];
  u_char *bin;

  // Get the number for bits.
  getRawData((char*)buf, 2);
  bits = ntohu16(buf);
  // Compute the number of binary bytes that follow.
  bytes = (bits + 7) / 8;
  if (bytes > 8 *1024) {
    FQ_TRACE("sshbuffer", 0) << "Can't handle BN of size " << bytes;
    return ;
  }
  if (len() < bytes) {
    FQ_TRACE("sshbuffer", 0) << "The input buffer is too small.";
    return ;
  }
  bin = data();
  BN_bin2bn(bin, bytes, bignum);
  consume(bytes);
}

u_short FQTermSSHBuffer::getWord() {
  u_char buf[2];
  u_short data;

  getRawData((char*)buf, 2);
  data = ntohu16(buf);
  return data;
}

void FQTermSSHBuffer::putWord(u_short data) {
  u_char buf[2];

  htonu16(buf, data);
  putRawData((char*)buf, 2);
}

u_int FQTermSSHBuffer::getInt() {
  u_char buf[4];
  u_int data;
  getRawData((char*)buf, 4);
  data = ntohu32(buf);
  return data;
}

void FQTermSSHBuffer::putInt(u_int data) {
  u_char buf[4];

  htonu32(buf, data);
  putRawData((char*)buf, 4);
}

//==============================================================================
// Return a character from the buffer (0-255).
//==============================================================================

u_char FQTermSSHBuffer::getByte() {
  u_char ch;

  getRawData((char*) &ch, 1);
  return ch;
}

//==============================================================================
// Stores a character in the buffer.
//==============================================================================

void FQTermSSHBuffer::putByte(int data) {
  u_char ch = data;

  putRawData((char*) &ch, 1);
}

//==============================================================================
// Stores an arbitrary binary string in the buffer.
//==============================================================================

void FQTermSSHBuffer::putString(const char *str, int len) {
  if (str == NULL) {
    FQ_TRACE("sshbuffer", 0) << "Can't put a null pointer string.";
    return ;
  }

  if (len < 0) {
    len = strlen(str);
  }

  putInt(len);
  putRawData(str, len);
}

//==============================================================================
// Return an arbitrary binary string from the buffer. The string cannot be 
// longer than 256k. The returned value points to memory allocated with new;
// It is the responsibility of the calling function to free the data. 
//==============================================================================

void *FQTermSSHBuffer::getString(int *length) {
  u_char *data;
  u_int len;

  // Get the length.
  len = getInt();
  if ((long)len > buffer_size_) {
    FQ_TRACE("sshbuffer", 0)
        << "String length " << len
        << " is greater than buffer size " << buffer_size_;
    return 0;
  }
  // Allocate space for the string. Add one byte for a null character.
  data = new u_char[len + 1];
  // Get the string.
  getRawData((char*)data, len);
  // Append a null character to make processing easier.
  data[len] = 0;
  if (length != NULL) {
    *length = len;
  } 
  // return the length of the string.
  return data;
}

}  // namespace FQTerm