Last active
April 2, 2017 22:19
-
-
Save pascaldekloe/05e903f12a4f02a995f71d0c18872b65 to your computer and use it in GitHub Desktop.
Demo C Compilation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by colf(1); DO NOT EDIT. | |
// The compiler used schema file demo.colf for package demo. | |
#include "Colfer.h" | |
#include <errno.h> | |
#include <stdlib.h> | |
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \ | |
defined(__BIG_ENDIAN__) || \ | |
defined(__ARMEB__) || \ | |
defined(__AARCH64EB__) || \ | |
defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \ | |
defined(__SYSC_ZARCH__) | |
#define COLFER_ENDIAN | |
#endif | |
size_t colfer_size_max = 16 * 1024 * 1024; | |
size_t colfer_list_max = 64 * 1024; | |
size_t demo_course_marshal_len(const demo_course* o) { | |
size_t l = 1; | |
{ | |
uint_fast64_t x = o->ID; | |
if (x) { | |
if (x >= (uint_fast64_t) 1 << 49) l += 9; | |
else for (l += 2; x > 127; x >>= 7, ++l); | |
} | |
} | |
{ | |
size_t n = o->name.len; | |
if (n > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
if (n) for (l += 2 + n; n > 127; n >>= 7, ++l); | |
} | |
{ | |
size_t n = o->holes.len; | |
if (n) { | |
if (n > colfer_list_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
demo_hole* a = o->holes.list; | |
for (size_t i = 0; i < n; ++i) l += demo_hole_marshal_len(&a[i]); | |
for (l += 2; n > 127; n >>= 7, ++l); | |
if (l > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
} | |
} | |
{ | |
size_t n = o->image.len; | |
if (n > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
if (n) for (l += 2 + n; n > 127; n >>= 7, ++l); | |
} | |
{ | |
size_t n = o->tags.len; | |
if (n) { | |
if (n > colfer_list_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
colfer_text* a = o->tags.list; | |
for (size_t i = 0; i < n; ++i) { | |
size_t len = a[i].len; | |
if (len > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
for (l += len + 1; len > 127; len >>= 7, ++l); | |
} | |
for (l += 2; n > 127; n >>= 7, ++l); | |
if (l > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
} | |
} | |
if (l > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
return l; | |
} | |
size_t demo_course_marshal(const demo_course* o, void* buf) { | |
// octet pointer navigation | |
uint8_t* p = buf; | |
{ | |
uint_fast64_t x = o->ID; | |
if (x) { | |
if (x < (uint_fast64_t) 1 << 49) { | |
*p++ = 0; | |
for (; x >= 128; x >>= 7) *p++ = x | 128; | |
*p++ = x; | |
} else { | |
*p++ = 0 | 128; | |
#ifdef COLFER_ENDIAN | |
memcpy(p, &o->ID, 8); | |
p += 8; | |
#else | |
*p++ = x >> 56; | |
*p++ = x >> 48; | |
*p++ = x >> 40; | |
*p++ = x >> 32; | |
*p++ = x >> 24; | |
*p++ = x >> 16; | |
*p++ = x >> 8; | |
*p++ = x; | |
#endif | |
} | |
} | |
} | |
{ | |
size_t n = o->name.len; | |
if (n) { | |
*p++ = 1; | |
uint_fast32_t x = n; | |
for (; x >= 128; x >>= 7) *p++ = x | 128; | |
*p++ = x; | |
memcpy(p, o->name.utf8, n); | |
p += n; | |
} | |
} | |
{ | |
size_t n = o->holes.len; | |
if (n) { | |
*p++ = 2; | |
uint_fast32_t x = n; | |
for (; x >= 128; x >>= 7) *p++ = x | 128; | |
*p++ = x; | |
demo_hole* a = o->holes.list; | |
for (size_t i = 0; i < n; ++i) p += demo_hole_marshal(&a[i], p); | |
} | |
} | |
{ | |
size_t n = o->image.len; | |
if (n) { | |
*p++ = 3; | |
uint_fast32_t x = n; | |
for (; x >= 128; x >>= 7) *p++ = x | 128; | |
*p++ = x; | |
memcpy(p, o->image.octets, n); | |
p += n; | |
} | |
} | |
{ | |
size_t count = o->tags.len; | |
if (count) { | |
*p++ = 4; | |
uint_fast32_t x = count; | |
for (; x >= 128; x >>= 7) *p++ = x | 128; | |
*p++ = x; | |
colfer_text* text = o->tags.list; | |
do { | |
size_t n = text->len; | |
for (x = n; x >= 128; x >>= 7) *p++ = x | 128; | |
*p++ = x; | |
memcpy(p, text->utf8, n); | |
p += n; | |
++text; | |
} while (--count != 0); | |
} | |
} | |
*p++ = 127; | |
return p - (uint8_t*) buf; | |
} | |
size_t demo_course_unmarshal(demo_course* o, const void* data, size_t datalen) { | |
// octet pointer navigation | |
const uint8_t* p = data; | |
const uint8_t* end; | |
int enderr; | |
if (datalen < colfer_size_max) { | |
end = p + datalen; | |
enderr = EWOULDBLOCK; | |
} else { | |
end = p + colfer_size_max; | |
enderr = EFBIG; | |
} | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
uint_fast8_t header = *p++; | |
if (header == 0) { | |
if (p+1 >= end) { | |
errno = enderr; | |
return 0; | |
} | |
uint_fast64_t x = *p++; | |
if (x > 127) { | |
x &= 127; | |
for (int shift = 7; ; shift += 7) { | |
uint_fast64_t b = *p++; | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
if (b <= 127) { | |
x |= b << shift; | |
break; | |
} | |
x |= (b & 127) << shift; | |
} | |
} | |
o->ID = x; | |
header = *p++; | |
} else if (header == (0 | 128)) { | |
if (p+8 >= end) { | |
errno = enderr; | |
return 0; | |
} | |
uint_fast64_t x = *p++; | |
x <<= 56; | |
x |= (uint_fast64_t) *p++ << 48; | |
x |= (uint_fast64_t) *p++ << 40; | |
x |= (uint_fast64_t) *p++ << 32; | |
x |= (uint_fast64_t) *p++ << 24; | |
x |= (uint_fast64_t) *p++ << 16; | |
x |= (uint_fast64_t) *p++ << 8; | |
x |= (uint_fast64_t) *p++; | |
o->ID = x; | |
header = *p++; | |
} | |
if (header == 1) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t n = *p++; | |
if (n > 127) { | |
n &= 127; | |
for (int shift = 7; shift < sizeof(size_t) * CHAR_BIT; shift += 7) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t c = *p++; | |
if (c <= 127) { | |
n |= c << shift; | |
break; | |
} | |
n |= (c & 127) << shift; | |
} | |
} | |
if (p+n >= end) { | |
errno = enderr; | |
return 0; | |
} | |
void* a = malloc(n); | |
memcpy(a, p, n); | |
p += n; | |
o->name.len = n; | |
o->name.utf8 = (char*) a; | |
header = *p++; | |
} | |
if (header == 2) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t n = *p++; | |
if (n > 127) { | |
n &= 127; | |
for (int shift = 7; ; shift += 7) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t c = *p++; | |
if (c <= 127) { | |
n |= c << shift; | |
break; | |
} | |
n |= (c & 127) << shift; | |
} | |
} | |
if (n > colfer_list_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
demo_hole* a = calloc(n, sizeof(demo_hole)); | |
for (size_t i = 0; i < n; ++i) { | |
size_t read = demo_hole_unmarshal(&a[i], p, (size_t) (end - p)); | |
if (!read) { | |
if (errno == EWOULDBLOCK) errno = enderr; | |
return read; | |
} | |
p += read; | |
} | |
o->holes.len = n; | |
o->holes.list = a; | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
header = *p++; | |
} | |
if (header == 3) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t n = *p++; | |
if (n > 127) { | |
n &= 127; | |
for (int shift = 7; ; shift += 7) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t c = *p++; | |
if (c <= 127) { | |
n |= c << shift; | |
break; | |
} | |
n |= (c & 127) << shift; | |
} | |
} | |
if (p+n >= end) { | |
errno = enderr; | |
return 0; | |
} | |
void* a = malloc(n); | |
memcpy(a, p, n); | |
p += n; | |
o->image.len = n; | |
o->image.octets = (uint8_t*) a; | |
header = *p++; | |
} | |
if (header == 4) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t n = *p++; | |
if (n > 127) { | |
n &= 127; | |
for (int shift = 7; ; shift += 7) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t c = *p++; | |
if (c <= 127) { | |
n |= c << shift; | |
break; | |
} | |
n |= (c & 127) << shift; | |
} | |
} | |
if (n > colfer_list_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
colfer_text* text = malloc(n * sizeof(colfer_text)); | |
o->tags.len = n; | |
o->tags.list = text; | |
for (; n != 0; --n, ++text) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t len = *p++; | |
if (len > 127) { | |
len &= 127; | |
for (int shift = 7; ; shift += 7) { | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
size_t c = *p++; | |
if (c <= 127) { | |
len |= c << shift; | |
break; | |
} | |
len |= (c & 127) << shift; | |
} | |
} | |
if (p+len >= end) { | |
errno = enderr; | |
return 0; | |
} | |
char* a = malloc(len); | |
memcpy(a, p, len); | |
p += len; | |
text->len = len; | |
text->utf8 = a; | |
} | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
header = *p++; | |
} | |
if (header != 127) { | |
errno = EILSEQ; | |
return 0; | |
} | |
return (size_t) (p - (const uint8_t*) data); | |
} | |
size_t demo_hole_marshal_len(const demo_hole* o) { | |
size_t l = 1; | |
if (o->lat != 0.0) l += 9; | |
if (o->lon != 0.0) l += 9; | |
if (o->par) l += 2; | |
if (o->water) l++; | |
if (o->sand) l++; | |
if (l > colfer_size_max) { | |
errno = EFBIG; | |
return 0; | |
} | |
return l; | |
} | |
size_t demo_hole_marshal(const demo_hole* o, void* buf) { | |
// octet pointer navigation | |
uint8_t* p = buf; | |
if (o->lat != 0.0) { | |
*p++ = 0; | |
#ifdef COLFER_ENDIAN | |
memcpy(p, &o->lat, 8); | |
p += 8; | |
#else | |
uint_fast64_t x; | |
memcpy(&x, &o->lat, 8); | |
*p++ = x >> 56; | |
*p++ = x >> 48; | |
*p++ = x >> 40; | |
*p++ = x >> 32; | |
*p++ = x >> 24; | |
*p++ = x >> 16; | |
*p++ = x >> 8; | |
*p++ = x; | |
#endif | |
} | |
if (o->lon != 0.0) { | |
*p++ = 1; | |
#ifdef COLFER_ENDIAN | |
memcpy(p, &o->lon, 8); | |
p += 8; | |
#else | |
uint_fast64_t x; | |
memcpy(&x, &o->lon, 8); | |
*p++ = x >> 56; | |
*p++ = x >> 48; | |
*p++ = x >> 40; | |
*p++ = x >> 32; | |
*p++ = x >> 24; | |
*p++ = x >> 16; | |
*p++ = x >> 8; | |
*p++ = x; | |
#endif | |
} | |
if (o->par) { | |
*p++ = 2; | |
*p++ = o->par; | |
} | |
if (o->water) *p++ = 3; | |
if (o->sand) *p++ = 4; | |
*p++ = 127; | |
return p - (uint8_t*) buf; | |
} | |
size_t demo_hole_unmarshal(demo_hole* o, const void* data, size_t datalen) { | |
// octet pointer navigation | |
const uint8_t* p = data; | |
const uint8_t* end; | |
int enderr; | |
if (datalen < colfer_size_max) { | |
end = p + datalen; | |
enderr = EWOULDBLOCK; | |
} else { | |
end = p + colfer_size_max; | |
enderr = EFBIG; | |
} | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
uint_fast8_t header = *p++; | |
if (header == 0) { | |
if (p+8 >= end) { | |
errno = enderr; | |
return 0; | |
} | |
#ifdef COLFER_ENDIAN | |
memcpy(&o->lat, p, 8); | |
p += 8; | |
#else | |
uint_fast64_t x = *p++; | |
x <<= 56; | |
x |= (uint_fast64_t) *p++ << 48; | |
x |= (uint_fast64_t) *p++ << 40; | |
x |= (uint_fast64_t) *p++ << 32; | |
x |= (uint_fast64_t) *p++ << 24; | |
x |= (uint_fast64_t) *p++ << 16; | |
x |= (uint_fast64_t) *p++ << 8; | |
x |= (uint_fast64_t) *p++; | |
memcpy(&o->lat, &x, 8); | |
#endif | |
header = *p++; | |
} | |
if (header == 1) { | |
if (p+8 >= end) { | |
errno = enderr; | |
return 0; | |
} | |
#ifdef COLFER_ENDIAN | |
memcpy(&o->lon, p, 8); | |
p += 8; | |
#else | |
uint_fast64_t x = *p++; | |
x <<= 56; | |
x |= (uint_fast64_t) *p++ << 48; | |
x |= (uint_fast64_t) *p++ << 40; | |
x |= (uint_fast64_t) *p++ << 32; | |
x |= (uint_fast64_t) *p++ << 24; | |
x |= (uint_fast64_t) *p++ << 16; | |
x |= (uint_fast64_t) *p++ << 8; | |
x |= (uint_fast64_t) *p++; | |
memcpy(&o->lon, &x, 8); | |
#endif | |
header = *p++; | |
} | |
if (header == 2) { | |
if (p+1 >= end) { | |
errno = enderr; | |
return 0; | |
} | |
o->par = *p++; | |
header = *p++; | |
} | |
if (header == 3) { | |
o->water = 1; | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
header = *p++; | |
} | |
if (header == 4) { | |
o->sand = 1; | |
if (p >= end) { | |
errno = enderr; | |
return 0; | |
} | |
header = *p++; | |
} | |
if (header != 127) { | |
errno = EILSEQ; | |
return 0; | |
} | |
return (size_t) (p - (const uint8_t*) data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by colf(1); DO NOT EDIT. | |
// The compiler used schema file demo.colf for package demo. | |
#ifndef COLFER_H | |
#define COLFER_H | |
#include <limits.h> | |
#include <stdint.h> | |
#include <string.h> | |
#if CHAR_BIT != 8 | |
#error "octet byte size" | |
#endif | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
// colfer_size_max is the upper limit for serial octet sizes. | |
extern size_t colfer_size_max; | |
// colfer_list_max is the upper limit for the number of elements in a list. | |
extern size_t colfer_list_max; | |
typedef struct { | |
// sec is the Unix time. | |
int_fast64_t sec; | |
// nanos is the nanosecond adjustment. | |
int_fast64_t nanos; | |
} colfer_timestamp; | |
// colfer_text is a UTF-8 CLOB. | |
typedef struct { | |
const char* utf8; | |
size_t len; | |
} colfer_text; | |
// colfer_binary is a BLOB. | |
typedef struct { | |
uint8_t* octets; | |
size_t len; | |
} colfer_binary; | |
typedef struct demo_course demo_course; | |
typedef struct demo_hole demo_hole; | |
// Course is the grounds where the game of golf is played. | |
struct demo_course { | |
uint64_t ID; | |
colfer_text name; | |
struct { | |
struct demo_hole* list; | |
size_t len; | |
} holes; | |
colfer_binary image; | |
struct { | |
colfer_text* list; | |
size_t len; | |
} tags; | |
}; | |
// demo_course_marshal_len returns the Colfer serial octet size. | |
// When the return is zero then errno is set to EFBIG to indicate a breach of | |
// either colfer_size_max or colfer_list_max. | |
size_t demo_course_marshal_len(const demo_course* o); | |
// demo_course_marshal encodes o as Colfer into buf and returns the number | |
// of octets written. | |
size_t demo_course_marshal(const demo_course* o, void* buf); | |
// demo_course_unmarshal decodes data as Colfer into o and returns the | |
// number of octets read. The data is read up to a maximum of datalen or | |
// colfer_size_max, whichever occurs first. | |
// When the return is zero then errno is set to one of the following 3 values: | |
// EWOULDBLOCK on incomplete data, EFBIG on a breach of either colfer_size_max | |
// or colfer_list_max and EILSEQ on schema mismatch. | |
size_t demo_course_unmarshal(demo_course* o, const void* data, size_t datalen); | |
struct demo_hole { | |
// Lat is the latitude of the cup. | |
double lat; | |
// Lon is the longitude of the cup. | |
double lon; | |
// Par is the difficulty index. | |
uint8_t par; | |
// Water marks the presence of water. | |
char water; | |
// Sand marks the presence of sand. | |
char sand; | |
}; | |
// demo_hole_marshal_len returns the Colfer serial octet size. | |
// When the return is zero then errno is set to EFBIG to indicate a breach of | |
// either colfer_size_max or colfer_list_max. | |
size_t demo_hole_marshal_len(const demo_hole* o); | |
// demo_hole_marshal encodes o as Colfer into buf and returns the number | |
// of octets written. | |
size_t demo_hole_marshal(const demo_hole* o, void* buf); | |
// demo_hole_unmarshal decodes data as Colfer into o and returns the | |
// number of octets read. The data is read up to a maximum of datalen or | |
// colfer_size_max, whichever occurs first. | |
// When the return is zero then errno is set to one of the following 3 values: | |
// EWOULDBLOCK on incomplete data, EFBIG on a breach of either colfer_size_max | |
// or colfer_list_max and EILSEQ on schema mismatch. | |
size_t demo_hole_unmarshal(demo_hole* o, const void* data, size_t datalen); | |
#ifdef __cplusplus | |
} // extern "C" | |
#endif | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment