bufferpack.c 3.71 KB
Newer Older
1
#include "bytebuffer.h"
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
#include "bufferpack.h"

#include <string.h>

#if defined (__APPLE__) && defined (__MACH__)
#include <machine/endian.h>

#if BYTE_ORDER == BIG_ENDIAN
#define htobe64(x) (x)
#else // BYTE_ORDER != BIG_ENDIAN
#define htobe64(x) __DARWIN_OSSwapInt64((x))
#endif

#else // !(__APPLE__ && __MACH__)
#include <endian.h>
#endif

#include <arpa/inet.h>


// ****************************************************************************
// BUFFER DATA PACKING OPERATIONS
// ****************************************************************************

void 
buffer_append_jboolean (buffer_t * restrict buffer, jboolean value) {
28
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (jboolean));
29 30 31 32 33 34
	*((jboolean *) dest) = value;
}


void
buffer_append_jbyte (buffer_t * restrict buffer, jbyte value) {
35
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (jbyte));
36 37 38 39 40 41
	*((jbyte *) dest) = value;
}


void 
buffer_append_jchar (buffer_t * restrict buffer, jchar value) {
42
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (jchar));
43 44 45 46 47 48
	*((jchar *) dest) = htons (value);
}


void
buffer_append_jshort (buffer_t * restrict buffer, jshort value) {
49
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (jshort));
50 51 52 53 54 55
	*((jshort *) dest) = htons (value);
}


void
buffer_append_jint (buffer_t * restrict buffer, jint value) {
56
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (jint));
57 58 59 60 61 62
	*((jint *) dest) = htonl (value);
}


void
buffer_append_jlong (buffer_t * restrict buffer, jlong value) {
63
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (jlong));
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
	*((jlong *) dest) = htobe64 (value);
}


void
buffer_append_jfloat (buffer_t * restrict buffer, jfloat value) {
	union {
		jfloat as_jfloat;
		jint as_jint;
	} view = { .as_jfloat = value };

	buffer_append_jint (buffer, view.as_jint);
}


void
buffer_append_jdouble (buffer_t * restrict buffer, jdouble value) {
	union {
		jdouble as_jdouble;
		jlong as_jlong;
	} view = { .as_jdouble = value };

	buffer_append_jlong (buffer, view.as_jlong);
}

//

91 92 93 94 95 96 97
void
buffer_append_uint8 (buffer_t * restrict buffer, uint8_t value) {
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (uint8_t));
	*dest = value;
}


98 99
void
buffer_append_uint16 (buffer_t * restrict buffer, uint16_t value) {
100
	uint8_t * restrict dest = buffer_claim (buffer, sizeof (uint16_t));
101 102 103 104 105 106 107 108
	*((uint16_t *) dest) = htons (value);
}


void
buffer_append_bytes (
	buffer_t * restrict buffer, const void * restrict bytes, size_t count
) {
109
	uint8_t * restrict dest = buffer_claim (buffer, count);
110 111 112 113 114 115 116
	memcpy (dest, bytes, count);
}

//

void
buffer_put_jshort (buffer_t * restrict buffer, jshort value, size_t position) {
117
	uint8_t * restrict dest = buffer_ptr_block  (buffer, position, sizeof (jshort));
118 119 120 121 122 123
	*((jshort *) dest) = htons (value);
}


void 
buffer_put_jint (buffer_t * restrict buffer, jint value, size_t position) {
124
	uint8_t * restrict dest = buffer_ptr_block  (buffer, position, sizeof (jint));
125 126 127 128 129 130
	*((jint *) dest) = htonl (value);
}


void 
buffer_put_jlong (buffer_t * restrict buffer, jlong value, size_t position) {
131
	uint8_t * restrict dest = buffer_ptr_block  (buffer, position, sizeof (jlong));
132 133 134 135 136 137 138
	*((jlong *) dest) = htobe64 (value);
}

//

void
buffer_append_utf8 (
139
	buffer_t * restrict buffer, const char * value, uint16_t length
140 141 142 143 144
) {
	// Encode length followed by the UTF-encoded bytes.
	buffer_append_uint16 (buffer, length);
	buffer_append_bytes (buffer, value, length);
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158


/**
 * Drains all bytes from the source buffer to the destination
 * buffer. The source buffer is cleared upon completion.
 */
void
buffer_drain_to (buffer_t * restrict src, buffer_t * restrict dst) {
	size_t src_len = buffer_length (src);
	uint8_t * src_bytes = buffer_ptr_block  (src, 0, src_len);
	buffer_append_bytes (dst, src_bytes, src_len);
	buffer_clear (src);
}