Commit 66aa352a authored by Lubomir Bulej's avatar Lubomir Bulej

Data packing operations for the simple byte buffer

parent 1603731f
#include "bufferbase.h"
#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) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (jboolean));
*((jboolean *) dest) = value;
}
void
buffer_append_jbyte (buffer_t * restrict buffer, jbyte value) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (jbyte));
*((jbyte *) dest) = value;
}
void
buffer_append_jchar (buffer_t * restrict buffer, jchar value) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (jchar));
*((jchar *) dest) = htons (value);
}
void
buffer_append_jshort (buffer_t * restrict buffer, jshort value) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (jshort));
*((jshort *) dest) = htons (value);
}
void
buffer_append_jint (buffer_t * restrict buffer, jint value) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (jint));
*((jint *) dest) = htonl (value);
}
void
buffer_append_jlong (buffer_t * restrict buffer, jlong value) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (jlong));
*((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);
}
//
void
buffer_append_uint16 (buffer_t * restrict buffer, uint16_t value) {
uint8_t * restrict dest = buffer_reserve (buffer, sizeof (uint16_t));
*((uint16_t *) dest) = htons (value);
}
void
buffer_append_bytes (
buffer_t * restrict buffer, const void * restrict bytes, size_t count
) {
uint8_t * restrict dest = buffer_reserve (buffer, count);
memcpy (dest, bytes, count);
}
//
void
buffer_put_jshort (buffer_t * restrict buffer, jshort value, size_t position) {
uint8_t * restrict dest = buffer_ptr (buffer, position, sizeof (jshort));
*((jshort *) dest) = htons (value);
}
void
buffer_put_jint (buffer_t * restrict buffer, jint value, size_t position) {
uint8_t * restrict dest = buffer_ptr (buffer, position, sizeof (jint));
*((jint *) dest) = htonl (value);
}
void
buffer_put_jlong (buffer_t * restrict buffer, jlong value, size_t position) {
uint8_t * restrict dest = buffer_ptr (buffer, position, sizeof (jlong));
*((jlong *) dest) = htobe64 (value);
}
//
void
buffer_append_utf8 (
buffer_t * restrict buffer, const uint8_t * value, uint16_t length
) {
// Encode length followed by the UTF-encoded bytes.
buffer_append_uint16 (buffer, length);
buffer_append_bytes (buffer, value, length);
}
#ifndef _BUFFERPACK_H_
#define _BUFFERPACK_H_
#include "bufferbase.h"
#include <jvmti.h>
#include <stdint.h>
//
void buffer_append_jboolean (buffer_t * restrict buffer, jboolean value);
void buffer_append_jbyte (buffer_t * restrict buffer, jbyte value);
void buffer_append_jchar (buffer_t * restrict buffer, jchar value);
void buffer_append_jshort (buffer_t * restrict buffer, jshort value);
void buffer_append_jint (buffer_t * restrict buffer, jint value);
void buffer_append_jlong (buffer_t * restrict buffer, jlong value);
void buffer_append_jfloat (buffer_t * restrict buffer, jfloat value);
void buffer_append_jdouble (buffer_t * restrict buffer, jdouble value);
//
void buffer_append_uint16 (buffer_t * restrict buffer, uint16_t value);
void buffer_append_bytes (
buffer_t * restrict buffer, const void * restrict bytes, size_t count
);
//
void buffer_put_jshort (buffer_t * restrict buffer, jshort value, size_t position);
void buffer_put_jint (buffer_t * restrict buffer, jint value, size_t position);
void buffer_put_jlong (buffer_t * restrict buffer, jlong value, size_t position);
//
void buffer_append_utf8 (
buffer_t * restrict buffer, const uint8_t * value, uint16_t length
);
#endif /* _BUFFERPACK_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment