guacamole-spice-protocol/src/protocols/ssh/ssh_buffer.c

135 lines
3.2 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
2013-12-02 10:07:17 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2013-12-02 10:07:17 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2013-12-02 10:07:17 +00:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2014-01-01 22:44:28 +00:00
#include "config.h"
2013-12-02 10:07:17 +00:00
#include <stdint.h>
#include <string.h>
#include <openssl/bn.h>
void buffer_write_byte(char** buffer, uint8_t value) {
uint8_t* data = (uint8_t*) *buffer;
*data = value;
(*buffer)++;
}
void buffer_write_uint32(char** buffer, uint32_t value) {
uint8_t* data = (uint8_t*) *buffer;
data[0] = (value & 0xFF000000) >> 24;
data[1] = (value & 0x00FF0000) >> 16;
data[2] = (value & 0x0000FF00) >> 8;
data[3] = value & 0x000000FF;
*buffer += 4;
}
void buffer_write_data(char** buffer, const char* data, int length) {
memcpy(*buffer, data, length);
*buffer += length;
}
void buffer_write_bignum(char** buffer, BIGNUM* value) {
unsigned char* bn_buffer;
int length;
/* If zero, just write zero length */
if (BN_is_zero(value)) {
buffer_write_uint32(buffer, 0);
return;
}
/* Allocate output buffer, add padding byte */
length = BN_num_bytes(value);
bn_buffer = malloc(length);
/* Convert BIGNUM */
BN_bn2bin(value, bn_buffer);
/* If first byte has high bit set, write padding byte */
if (bn_buffer[0] & 0x80) {
buffer_write_uint32(buffer, length+1);
buffer_write_byte(buffer, 0);
}
else
buffer_write_uint32(buffer, length);
/* Write data */
memcpy(*buffer, bn_buffer, length);
*buffer += length;
free(bn_buffer);
}
void buffer_write_string(char** buffer, const char* string, int length) {
buffer_write_uint32(buffer, length);
buffer_write_data(buffer, string, length);
}
uint8_t buffer_read_byte(char** buffer) {
uint8_t* data = (uint8_t*) *buffer;
uint8_t value = *data;
(*buffer)++;
return value;
}
uint32_t buffer_read_uint32(char** buffer) {
uint8_t* data = (uint8_t*) *buffer;
uint32_t value =
(data[0] << 24)
| (data[1] << 16)
| (data[2] << 8)
| data[3];
*buffer += 4;
return value;
}
char* buffer_read_string(char** buffer, int* length) {
char* value;
*length = buffer_read_uint32(buffer);
value = *buffer;
*buffer += *length;
return value;
}