Initial commit
This commit is contained in:
commit
4b2180d4ba
28
eebie/ebml.h
Normal file
28
eebie/ebml.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef EEBIE_H
|
||||||
|
#define EEBIE_H
|
||||||
|
|
||||||
|
#include<stdint.h>
|
||||||
|
|
||||||
|
typedef enum EBMLElementType {
|
||||||
|
EBML_SIGNED_INTEGER,
|
||||||
|
EBML_UNSIGNED_INTEGER,
|
||||||
|
EBML_FLOAT4,
|
||||||
|
EBML_FLOAT8,
|
||||||
|
EBML_STRING,
|
||||||
|
EBML_UTF8,
|
||||||
|
EBML_DATE,
|
||||||
|
EBML_TREE,
|
||||||
|
EBML_BINARY,
|
||||||
|
} EBMLElementType;
|
||||||
|
|
||||||
|
typedef union EBMLPrimitive {
|
||||||
|
int64_t sInt;
|
||||||
|
uint64_t uInt;
|
||||||
|
float flt4;
|
||||||
|
double flt8;
|
||||||
|
const char *string;
|
||||||
|
const uint8_t *binary;
|
||||||
|
uint64_t date;
|
||||||
|
} EBMLPrimitive;
|
||||||
|
|
||||||
|
#endif
|
175
eebie/reader.c
Normal file
175
eebie/reader.c
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
#include<stddef.h>
|
||||||
|
#include<stdint.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<string.h>
|
||||||
|
#include<endian.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
#include"ebml.h"
|
||||||
|
|
||||||
|
#include"reader.h"
|
||||||
|
|
||||||
|
void ebml_reader_init(EBMLReader *this) {
|
||||||
|
memset(this, 0, sizeof(*this));
|
||||||
|
this->state = EBMLRS_WAITING_FOR_ELEMENT_ID;
|
||||||
|
this->currentDepth = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t VARINT_MASKS[] = {0, 0x80L, 0xC000L, 0xE00000L, 0xF0000000L, 0xF800000000L, 0xFC0000000000L, 0xFE000000000000L, 0L};
|
||||||
|
|
||||||
|
#define I(x) ((uint64_t)(x))
|
||||||
|
static int read_varint(const uint8_t *data, size_t length, uint64_t *result) {
|
||||||
|
if(data[0] & 0x80) {
|
||||||
|
*result = data[0];
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else if(data[0] & 0xC0) {
|
||||||
|
if(length < 2) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[0]) << 8) | data[1];
|
||||||
|
|
||||||
|
return 2;
|
||||||
|
} else if(data[0] & 0xE0) {
|
||||||
|
if(length < 3) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[0]) << 16) | (I(data[1]) << 8) | data[2];
|
||||||
|
|
||||||
|
return 3;
|
||||||
|
} else if(data[0] & 0xF0) {
|
||||||
|
if(length < 4) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[0]) << 24) | (I(data[1]) << 16) | (I(data[2]) << 8) | data[3];
|
||||||
|
|
||||||
|
return 4;
|
||||||
|
} else if(data[0] & 0xF8) {
|
||||||
|
if(length < 5) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[0]) << 32) | (I(data[1]) << 24) | (I(data[2]) << 16) | (I(data[3]) << 8) | data[4];
|
||||||
|
|
||||||
|
return 5;
|
||||||
|
} else if(data[0] & 0xFC) {
|
||||||
|
if(length < 6) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[0]) << 40) | (I(data[1]) << 32) | (I(data[2]) << 24) | (I(data[3]) << 16) | (I(data[4]) << 8) | data[5];
|
||||||
|
|
||||||
|
return 6;
|
||||||
|
} else if(data[0] & 0xFE) {
|
||||||
|
if(length < 7) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[0]) << 48) | (I(data[1]) << 40) | (I(data[2]) << 32) | (I(data[3]) << 24) | (I(data[4]) << 16) | (I(data[5]) << 8) | data[6];
|
||||||
|
|
||||||
|
return 7;
|
||||||
|
} else if(data[0] == 0x01) {
|
||||||
|
if(length < 8) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*result = (I(data[1]) << 48) | (I(data[2]) << 40) | (I(data[3]) << 32) | (I(data[4]) << 24) | (I(data[5]) << 16) | (I(data[6]) << 8) | data[7];
|
||||||
|
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_varint(const uint8_t *data, size_t length, uint64_t *result) {
|
||||||
|
int ret = read_varint(data, length, result);
|
||||||
|
|
||||||
|
if(ret >= 0) {
|
||||||
|
*result &= ~VARINT_MASKS[ret];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ebml_reader_feed(EBMLReader *this, const uint8_t *data, size_t length) {
|
||||||
|
if(length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t eaten = -1;
|
||||||
|
|
||||||
|
if(this->state == EBMLRS_WAITING_FOR_ELEMENT_ID) {
|
||||||
|
|
||||||
|
uint64_t elId;
|
||||||
|
|
||||||
|
int status = read_varint(data, length, &elId);
|
||||||
|
|
||||||
|
if(status <= 0) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->state = EBMLRS_WAITING_FOR_ELEMENT_LENGTH;
|
||||||
|
this->inside.id = elId;
|
||||||
|
|
||||||
|
eaten = status;
|
||||||
|
|
||||||
|
} else if(this->state == EBMLRS_WAITING_FOR_ELEMENT_LENGTH) {
|
||||||
|
|
||||||
|
uint64_t elLength;
|
||||||
|
|
||||||
|
int status = get_varint(data, length, &elLength);
|
||||||
|
|
||||||
|
if(status <= 0) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->inside.type = this->eventEnterElement(this, this->inside.id, elLength);
|
||||||
|
|
||||||
|
if(this->inside.type == EBML_TREE) {
|
||||||
|
this->state = EBMLRS_WAITING_FOR_ELEMENT_ID;
|
||||||
|
} else {
|
||||||
|
this->state = EBMLRS_WAITING_FOR_ELEMENT_DATA;
|
||||||
|
|
||||||
|
this->inside.length = elLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->idStack[this->currentDepth] = this->inside.id;
|
||||||
|
this->stack[this->currentDepth] = elLength + status;
|
||||||
|
|
||||||
|
this->currentDepth++;
|
||||||
|
|
||||||
|
eaten = status;
|
||||||
|
|
||||||
|
} else if(this->state == EBMLRS_WAITING_FOR_ELEMENT_DATA) {
|
||||||
|
|
||||||
|
size_t realLen = this->stack[this->currentDepth - 1] < length ? this->stack[this->currentDepth - 1] : length;
|
||||||
|
|
||||||
|
if(this->eventDataChunk) {
|
||||||
|
this->eventDataChunk(this, data, realLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
eaten = realLen;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < this->currentDepth; i++) {
|
||||||
|
this->stack[i] -= eaten;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(this->currentDepth > 0 && this->stack[this->currentDepth - 1] == 0) {
|
||||||
|
|
||||||
|
if(this->eventExitElement) {
|
||||||
|
this->eventExitElement(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->currentDepth--;
|
||||||
|
|
||||||
|
this->state = EBMLRS_WAITING_FOR_ELEMENT_ID;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return eaten;
|
||||||
|
}
|
45
eebie/reader.h
Normal file
45
eebie/reader.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#ifndef EEBIE_READER_H
|
||||||
|
#define EEBIE_READER_H
|
||||||
|
|
||||||
|
#define EBML_READ_MAXIMUM_DEPTH 128
|
||||||
|
|
||||||
|
#include"ebml.h"
|
||||||
|
|
||||||
|
enum EBMLReaderState {
|
||||||
|
EBMLRS_WAITING_FOR_ELEMENT_ID,
|
||||||
|
EBMLRS_WAITING_FOR_ELEMENT_LENGTH,
|
||||||
|
EBMLRS_WAITING_FOR_ELEMENT_DATA,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct EBMLReader;
|
||||||
|
|
||||||
|
typedef EBMLElementType(*EBMLEventEnterElement)(struct EBMLReader*, uint64_t id, uint64_t length);
|
||||||
|
typedef void(*EBMLEventDataChunk)(struct EBMLReader*, const uint8_t *data, size_t length);
|
||||||
|
typedef void(*EBMLEventExitElement)(struct EBMLReader*);
|
||||||
|
|
||||||
|
typedef struct EBMLReader {
|
||||||
|
enum EBMLReaderState state;
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
uint64_t id;
|
||||||
|
uint64_t length;
|
||||||
|
|
||||||
|
EBMLElementType type;
|
||||||
|
} inside;
|
||||||
|
};
|
||||||
|
|
||||||
|
int currentDepth;
|
||||||
|
uint64_t stack[EBML_READ_MAXIMUM_DEPTH];
|
||||||
|
uint64_t idStack[EBML_READ_MAXIMUM_DEPTH];
|
||||||
|
|
||||||
|
EBMLEventEnterElement eventEnterElement;
|
||||||
|
EBMLEventDataChunk eventDataChunk;
|
||||||
|
EBMLEventExitElement eventExitElement;
|
||||||
|
|
||||||
|
void *ud;
|
||||||
|
} EBMLReader;
|
||||||
|
|
||||||
|
void ebml_reader_init(EBMLReader *this);
|
||||||
|
int ebml_reader_feed(EBMLReader *this, const uint8_t *data, size_t length);
|
||||||
|
|
||||||
|
#endif
|
112
eebie/writer.c
Normal file
112
eebie/writer.c
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
#include"writer.h"
|
||||||
|
|
||||||
|
#include<string.h>
|
||||||
|
#include<endian.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
static uint64_t VARINT_MASKS[] = {0, 0x80L, 0xC000L, 0xE00000L, 0xF0000000L, 0xF800000000L, 0xFC0000000000L, 0xFE000000000000L, 0L};
|
||||||
|
|
||||||
|
static int byte_length(uint64_t i) {
|
||||||
|
if(i == 0) return 1;
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
while(i) {
|
||||||
|
i >>= 8;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bit_length(uint64_t i) {
|
||||||
|
if(i == 0) return 1;
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
while(i) {
|
||||||
|
i >>= 1;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t encode_int(uint64_t i) {
|
||||||
|
return i | VARINT_MASKS[(bit_length(i) + 6) / 7];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void advance(EBMLWriter *this, uint64_t amount) {
|
||||||
|
for(int i = 0; i < this->currentDepth; i++) {
|
||||||
|
this->stack[i] += amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add(EBMLWriter *this, const void *data, size_t length) {
|
||||||
|
if(this->bufferLen + length > this->bufferCapacity) {
|
||||||
|
this->buffer = realloc(this->buffer, (this->bufferLen + length + 1023) & ~1023);
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(this->buffer + this->bufferLen, data, length);
|
||||||
|
|
||||||
|
this->bufferLen += length;
|
||||||
|
|
||||||
|
advance(this, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_varint(EBMLWriter *this, uint64_t i) {
|
||||||
|
int len = byte_length(i);
|
||||||
|
|
||||||
|
i = htobe64(i);
|
||||||
|
|
||||||
|
add(this, (uint8_t*) &i + 8 - len, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ebml_writer_init(EBMLWriter *this) {
|
||||||
|
memset(this, 0, sizeof(EBMLWriter));
|
||||||
|
|
||||||
|
this->buffer = calloc(this->bufferCapacity = 1024, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ebml_writer_push(EBMLWriter *this, uint64_t id) {
|
||||||
|
add_varint(this, id);
|
||||||
|
|
||||||
|
add(this, &(uint64_t) {htobe64(0x0100000000000000L)}, sizeof(uint64_t)); // LENGTH OF MAX SIZE (BECAUSE UNKNOWN)
|
||||||
|
|
||||||
|
this->stack[this->currentDepth++] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ebml_writer_pop(EBMLWriter *this) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void ebml_writer_put(EBMLWriter *this, uint64_t id, EBMLElementType type, EBMLPrimitive primitive) {
|
||||||
|
add_varint(this, id);
|
||||||
|
|
||||||
|
int len = byte_length(id);
|
||||||
|
|
||||||
|
if(type == EBML_UNSIGNED_INTEGER) {
|
||||||
|
len = byte_length(primitive.uInt);
|
||||||
|
} else if(type == EBML_FLOAT4) {
|
||||||
|
len = 4;
|
||||||
|
} else if(type == EBML_FLOAT8) {
|
||||||
|
len = 8;
|
||||||
|
} else if(type == EBML_STRING) {
|
||||||
|
len = strlen(primitive.string);
|
||||||
|
} else if(type == EBML_DATE) {
|
||||||
|
len = 8;
|
||||||
|
} else abort();
|
||||||
|
|
||||||
|
add_varint(this, encode_int(len));
|
||||||
|
|
||||||
|
if(type == EBML_UNSIGNED_INTEGER) {
|
||||||
|
primitive.uInt = htobe64(primitive.uInt);
|
||||||
|
add(this, (uint8_t*) &primitive.uInt + 8 - len, len);
|
||||||
|
} else if(type == EBML_FLOAT4) {
|
||||||
|
uint32_t fl = *(uint32_t*) &primitive.flt4;
|
||||||
|
add(this, &fl, 4);
|
||||||
|
} else if(type == EBML_FLOAT8) {
|
||||||
|
uint64_t fl = *(uint64_t*) &primitive.flt8;
|
||||||
|
add(this, &fl, 8);
|
||||||
|
} else if(type == EBML_STRING) {
|
||||||
|
add(this, primitive.string, len);
|
||||||
|
} else if(type == EBML_DATE) {
|
||||||
|
add(this, &primitive.date, 8);
|
||||||
|
} else abort();
|
||||||
|
}
|
32
eebie/writer.h
Normal file
32
eebie/writer.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef EEBIE_WRITER_H
|
||||||
|
#define EEBIE_WRITER_H
|
||||||
|
|
||||||
|
#include"ebml.h"
|
||||||
|
|
||||||
|
#include<stddef.h>
|
||||||
|
|
||||||
|
#define EBML_WRITE_MAXIMUM_DEPTH 128
|
||||||
|
|
||||||
|
typedef size_t(*EBMLWriteCallback)(void *ud, const void *data, size_t length);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int currentDepth;
|
||||||
|
uint64_t stack[EBML_WRITE_MAXIMUM_DEPTH];
|
||||||
|
|
||||||
|
EBMLWriteCallback callbackWrite;
|
||||||
|
|
||||||
|
uint8_t *buffer;
|
||||||
|
size_t bufferLen;
|
||||||
|
size_t bufferCapacity;
|
||||||
|
|
||||||
|
void *ud;
|
||||||
|
} EBMLWriter;
|
||||||
|
|
||||||
|
void ebml_writer_init(EBMLWriter *this);
|
||||||
|
|
||||||
|
void ebml_writer_push(EBMLWriter *this, uint64_t id);
|
||||||
|
void ebml_writer_pop(EBMLWriter *this);
|
||||||
|
|
||||||
|
void ebml_writer_put(EBMLWriter *this, uint64_t id, EBMLElementType type, EBMLPrimitive primitive);
|
||||||
|
|
||||||
|
#endif
|
1015
schemagen/ezxml.c
Normal file
1015
schemagen/ezxml.c
Normal file
File diff suppressed because it is too large
Load Diff
167
schemagen/ezxml.h
Normal file
167
schemagen/ezxml.h
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/* ezxml.h
|
||||||
|
*
|
||||||
|
* Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
|
||||||
|
*
|
||||||
|
* 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:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _EZXML_H
|
||||||
|
#define _EZXML_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define EZXML_BUFSIZE 1024 // size of internal memory buffers
|
||||||
|
#define EZXML_NAMEM 0x80 // name is malloced
|
||||||
|
#define EZXML_TXTM 0x40 // txt is malloced
|
||||||
|
#define EZXML_DUP 0x20 // attribute name and value are strduped
|
||||||
|
|
||||||
|
typedef struct ezxml *ezxml_t;
|
||||||
|
struct ezxml {
|
||||||
|
char *name; // tag name
|
||||||
|
char **attr; // tag attributes { name, value, name, value, ... NULL }
|
||||||
|
char *txt; // tag character content, empty string if none
|
||||||
|
size_t off; // tag offset from start of parent tag character content
|
||||||
|
ezxml_t next; // next tag with same name in this section at this depth
|
||||||
|
ezxml_t sibling; // next tag with different name in same section and depth
|
||||||
|
ezxml_t ordered; // next tag, same section and depth, in original order
|
||||||
|
ezxml_t child; // head of sub tag list, NULL if none
|
||||||
|
ezxml_t parent; // parent tag, NULL if current tag is root tag
|
||||||
|
short flags; // additional information
|
||||||
|
};
|
||||||
|
|
||||||
|
// Given a string of xml data and its length, parses it and creates an ezxml
|
||||||
|
// structure. For efficiency, modifies the data by adding null terminators
|
||||||
|
// and decoding ampersand sequences. If you don't want this, copy the data and
|
||||||
|
// pass in the copy. Returns NULL on failure.
|
||||||
|
ezxml_t ezxml_parse_str(char *s, size_t len);
|
||||||
|
|
||||||
|
// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
|
||||||
|
// attempts to mem map the file. Failing that, reads the file into memory.
|
||||||
|
// Returns NULL on failure.
|
||||||
|
ezxml_t ezxml_parse_fd(int fd);
|
||||||
|
|
||||||
|
// a wrapper for ezxml_parse_fd() that accepts a file name
|
||||||
|
ezxml_t ezxml_parse_file(const char *file);
|
||||||
|
|
||||||
|
// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
|
||||||
|
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
|
||||||
|
// or ezxml_parse_fd()
|
||||||
|
ezxml_t ezxml_parse_fp(FILE *fp);
|
||||||
|
|
||||||
|
// returns the first child tag (one level deeper) with the given name or NULL
|
||||||
|
// if not found
|
||||||
|
ezxml_t ezxml_child(ezxml_t xml, const char *name);
|
||||||
|
|
||||||
|
// returns the next tag of the same name in the same section and depth or NULL
|
||||||
|
// if not found
|
||||||
|
#define ezxml_next(xml) ((xml) ? xml->next : NULL)
|
||||||
|
|
||||||
|
// Returns the Nth tag with the same name in the same section at the same depth
|
||||||
|
// or NULL if not found. An index of 0 returns the tag given.
|
||||||
|
ezxml_t ezxml_idx(ezxml_t xml, int idx);
|
||||||
|
|
||||||
|
// returns the name of the given tag
|
||||||
|
#define ezxml_name(xml) ((xml) ? xml->name : NULL)
|
||||||
|
|
||||||
|
// returns the given tag's character content or empty string if none
|
||||||
|
#define ezxml_txt(xml) ((xml) ? xml->txt : "")
|
||||||
|
|
||||||
|
// returns the value of the requested tag attribute, or NULL if not found
|
||||||
|
const char *ezxml_attr(ezxml_t xml, const char *attr);
|
||||||
|
|
||||||
|
// Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
|
||||||
|
// variable length list of tag names and indexes. The argument list must be
|
||||||
|
// terminated by either an index of -1 or an empty string tag name. Example:
|
||||||
|
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
|
||||||
|
// This retrieves the title of the 3rd book on the 1st shelf of library.
|
||||||
|
// Returns NULL if not found.
|
||||||
|
ezxml_t ezxml_get(ezxml_t xml, ...);
|
||||||
|
|
||||||
|
// Converts an ezxml structure back to xml. Returns a string of xml data that
|
||||||
|
// must be freed.
|
||||||
|
char *ezxml_toxml(ezxml_t xml);
|
||||||
|
|
||||||
|
// returns a NULL terminated array of processing instructions for the given
|
||||||
|
// target
|
||||||
|
const char **ezxml_pi(ezxml_t xml, const char *target);
|
||||||
|
|
||||||
|
// frees the memory allocated for an ezxml structure
|
||||||
|
void ezxml_free(ezxml_t xml);
|
||||||
|
|
||||||
|
// returns parser error message or empty string if none
|
||||||
|
const char *ezxml_error(ezxml_t xml);
|
||||||
|
|
||||||
|
// returns a new empty ezxml structure with the given root tag name
|
||||||
|
ezxml_t ezxml_new(const char *name);
|
||||||
|
|
||||||
|
// wrapper for ezxml_new() that strdup()s name
|
||||||
|
#define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
|
||||||
|
|
||||||
|
// Adds a child tag. off is the offset of the child tag relative to the start
|
||||||
|
// of the parent tag's character content. Returns the child tag.
|
||||||
|
ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off);
|
||||||
|
|
||||||
|
// wrapper for ezxml_add_child() that strdup()s name
|
||||||
|
#define ezxml_add_child_d(xml, name, off) \
|
||||||
|
ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
|
||||||
|
|
||||||
|
// sets the character content for the given tag and returns the tag
|
||||||
|
ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
|
||||||
|
|
||||||
|
// wrapper for ezxml_set_txt() that strdup()s txt
|
||||||
|
#define ezxml_set_txt_d(xml, txt) \
|
||||||
|
ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
|
||||||
|
|
||||||
|
// Sets the given tag attribute or adds a new attribute if not found. A value
|
||||||
|
// of NULL will remove the specified attribute. Returns the tag given.
|
||||||
|
ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
|
||||||
|
|
||||||
|
// Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
|
||||||
|
#define ezxml_set_attr_d(xml, name, value) \
|
||||||
|
ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
|
||||||
|
|
||||||
|
// sets a flag for the given tag and returns the tag
|
||||||
|
ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
|
||||||
|
|
||||||
|
// removes a tag along with its subtags without freeing its memory
|
||||||
|
ezxml_t ezxml_cut(ezxml_t xml);
|
||||||
|
|
||||||
|
// inserts an existing tag into an ezxml structure
|
||||||
|
ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off);
|
||||||
|
|
||||||
|
// Moves an existing tag to become a subtag of dest at the given offset from
|
||||||
|
// the start of dest's character content. Returns the moved tag.
|
||||||
|
#define ezxml_move(xml, dest, off) ezxml_insert(ezxml_cut(xml), dest, off)
|
||||||
|
|
||||||
|
// removes a tag along with all its subtags
|
||||||
|
#define ezxml_remove(xml) ezxml_free(ezxml_cut(xml))
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // _EZXML_H
|
150
schemagen/schemagen.c
Normal file
150
schemagen/schemagen.c
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
#include"ezxml.h"
|
||||||
|
|
||||||
|
#include<ctype.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<string.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
ezxml_t schema = ezxml_parse_file(argv[1]);
|
||||||
|
|
||||||
|
char *Name = strdup(argv[2]);
|
||||||
|
|
||||||
|
char *NAME = strdup(Name);
|
||||||
|
for(char *c = NAME; *c; c++) {
|
||||||
|
*c = toupper(*c);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *name = strdup(Name);
|
||||||
|
for(char *c = name; *c; c++) {
|
||||||
|
*c = tolower(*c);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("#ifndef %s_PARSER_H\n", NAME);
|
||||||
|
printf("#define %s_PARSER_H\n", NAME);
|
||||||
|
|
||||||
|
printf("#include<stdint.h>\n");
|
||||||
|
printf("#include<stddef.h>\n");
|
||||||
|
printf("#include<stdlib.h>\n");
|
||||||
|
printf("#include<string.h>\n");
|
||||||
|
|
||||||
|
printf("#include\"reader.h\"\n");
|
||||||
|
|
||||||
|
for(ezxml_t el = ezxml_child(schema, "element"); el; el = el->next) {
|
||||||
|
printf("#define %s_%s %sL\n", NAME, ezxml_attr(el, "name"), ezxml_attr(el, "id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("typedef struct %sParser {\n", Name);
|
||||||
|
printf("\tvoid *ud;\n");
|
||||||
|
printf("\tuint8_t *accum;\n");
|
||||||
|
printf("\tsize_t accumLen;\n");
|
||||||
|
printf("\tEBMLReader ebml;\n");
|
||||||
|
for(ezxml_t el = ezxml_child(schema, "element"); el; el = el->next) {
|
||||||
|
const char *type = ezxml_attr(el, "type");
|
||||||
|
|
||||||
|
if(!strcmp(type, "master")) {
|
||||||
|
printf("\tvoid(*On%s)(struct %sParser*);\n", ezxml_attr(el, "name"), Name);
|
||||||
|
} else if(!strcmp(type, "uinteger")) {
|
||||||
|
printf("\tvoid(*On%s)(struct %sParser*, uint64_t value);\n", ezxml_attr(el, "name"), Name);
|
||||||
|
} else if(!strcmp(type, "integer")) {
|
||||||
|
printf("\tvoid(*On%s)(struct %sParser*, int64_t value);\n", ezxml_attr(el, "name"), Name);
|
||||||
|
} else if(!strcmp(type, "date")) {
|
||||||
|
printf("\tvoid(*On%s)(struct %sParser*, int64_t value);\n", ezxml_attr(el, "name"), Name);
|
||||||
|
} else if(!strcmp(type, "float")) {
|
||||||
|
printf("\tvoid(*On%s)(struct %sParser*, double value);\n", ezxml_attr(el, "name"), Name);
|
||||||
|
} else {
|
||||||
|
if(strcmp(type, "binary") && strcmp(type, "utf-8") && strcmp(type, "string")) {
|
||||||
|
fprintf(stderr, "Interpreting unknown element type \"%s\" as binary\n", type);
|
||||||
|
}
|
||||||
|
printf("\tvoid(*On%s)(struct %sParser*, const uint8_t *data, size_t length);\n", ezxml_attr(el, "name"), Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("} %sParser;\n", Name);
|
||||||
|
|
||||||
|
printf("void %s_parser_init(%sParser*);\n", name, Name);
|
||||||
|
printf("size_t %s_parser_feed(%sParser*, const uint8_t *data, size_t length);\n", name, Name);
|
||||||
|
|
||||||
|
printf("#ifdef %s_PARSER_IMPLEMENTATION\n", NAME);
|
||||||
|
|
||||||
|
printf("EBMLElementType %s_event_enter_element(EBMLReader *rdr, uint64_t id, uint64_t length) {\n", name);
|
||||||
|
printf("\t%sParser *self = rdr->ud;\n", Name);
|
||||||
|
for(ezxml_t el = ezxml_child(schema, "element"); el; el = el->next) {
|
||||||
|
const char *type = ezxml_attr(el, "type");
|
||||||
|
|
||||||
|
if(!strcmp(type, "master")) {
|
||||||
|
printf("\tif(id == %s_%s) {\n", NAME, ezxml_attr(el, "name"));
|
||||||
|
printf("\t\tif(self->On%s) self->On%s(self);\n", ezxml_attr(el, "name"), ezxml_attr(el, "name"));
|
||||||
|
printf("\t\treturn EBML_TREE;\n");
|
||||||
|
printf("\t}\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\treturn EBML_BINARY;\n");
|
||||||
|
printf("}\n");
|
||||||
|
|
||||||
|
printf("void %s_event_data_chunk(EBMLReader *rdr, const uint8_t *data, uint64_t length) {\n", name);
|
||||||
|
printf("\t%sParser *self = rdr->ud;\n", Name);
|
||||||
|
printf("\tself->accum = realloc(self->accum, self->accumLen + length);\n");
|
||||||
|
printf("\tmemcpy(self->accum + self->accumLen, data, length);\n");
|
||||||
|
printf("\tself->accumLen += length;\n");
|
||||||
|
printf("}\n");
|
||||||
|
|
||||||
|
printf("void %s_event_exit_element(EBMLReader *rdr) {\n", name);
|
||||||
|
printf("\t%sParser *self = rdr->ud;\n", Name);
|
||||||
|
printf("\tuint64_t id = rdr->idStack[rdr->currentDepth - 1];\n");
|
||||||
|
for(ezxml_t el = ezxml_child(schema, "element"); el; el = el->next) {
|
||||||
|
const char *type = ezxml_attr(el, "type");
|
||||||
|
|
||||||
|
printf("\tif(id == %s_%s) {\n", NAME, ezxml_attr(el, "name"));
|
||||||
|
|
||||||
|
if(!strcmp(type, "uinteger") || !strcmp(type, "integer") || !strcmp(type, "date")) {
|
||||||
|
printf("\t\t%s val = 0;\n", !strcmp(type, "uinteger") ? "uint64_t" : "int64_t");
|
||||||
|
printf("\t\tmemcpy((uint8_t*) &val + 8 - self->accumLen, self->accum, self->accumLen);\n");
|
||||||
|
printf("\t\tval = be64toh(val);\n");
|
||||||
|
printf("\t\tif(self->On%s) self->On%s(self, val);\n", ezxml_attr(el, "name"), ezxml_attr(el, "name"));
|
||||||
|
} else if(!strcmp(type, "string") || !strcmp(type, "utf-8") || !strcmp(type, "binary")) {
|
||||||
|
if(strcmp(type, "binary") != 0) {
|
||||||
|
printf("\t\tself->accum = realloc(self->accum, self->accumLen + 1);\n");
|
||||||
|
printf("\t\tself->accum[self->accumLen] = 0;\n");
|
||||||
|
}
|
||||||
|
printf("\t\tif(self->On%s) self->On%s(self, self->accum, self->accumLen);\n", ezxml_attr(el, "name"), ezxml_attr(el, "name"));
|
||||||
|
} else if(!strcmp(type, "float")) {
|
||||||
|
printf("\t\tdouble val;\n");
|
||||||
|
printf("\t\tif(self->accumLen == 4) {\n");
|
||||||
|
printf("\t\t\tfloat fval;\n");
|
||||||
|
printf("\t\t\tmemcpy(&fval, self->accum, 4);\n");
|
||||||
|
printf("\t\t\t*(uint32_t*) &fval = be32toh(*(uint32_t*) &fval);\n");
|
||||||
|
printf("\t\t\tval = fval;\n");
|
||||||
|
printf("\t\t} else {\n");
|
||||||
|
printf("\t\t\tmemcpy(&val, self->accum, 8);\n");
|
||||||
|
printf("\t\t\t*(uint64_t*) &val = be64toh(*(uint64_t*) &val);\n");
|
||||||
|
printf("\t\t}\n");
|
||||||
|
printf("\t\tif(self->On%s) self->On%s(self, val);\n", ezxml_attr(el, "name"), ezxml_attr(el, "name"));
|
||||||
|
} else if(!strcmp(type, "master")) {
|
||||||
|
// Do nothing.
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Ignoring unknown type \"%s\"\n", type);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\t}\n");
|
||||||
|
}
|
||||||
|
printf("\tfree(self->accum); self->accum = NULL; self->accumLen = 0;\n");
|
||||||
|
printf("}\n");
|
||||||
|
|
||||||
|
printf("void %s_parser_init(%sParser *self) {\n", name, Name);
|
||||||
|
printf("\tmemset(self, 0, sizeof(*self));\n");
|
||||||
|
printf("\tebml_reader_init(&self->ebml);\n");
|
||||||
|
printf("\tself->ebml.eventEnterElement = %s_event_enter_element;\n", name);
|
||||||
|
printf("\tself->ebml.eventDataChunk = %s_event_data_chunk;\n", name);
|
||||||
|
printf("\tself->ebml.eventExitElement = %s_event_exit_element;\n", name);
|
||||||
|
printf("\tself->ebml.ud = self;\n");
|
||||||
|
printf("}\n");
|
||||||
|
|
||||||
|
printf("size_t %s_parser_feed(%sParser *self, const uint8_t *data, size_t length) {\n", name, Name);
|
||||||
|
printf("\treturn ebml_reader_feed(&self->ebml, data, length);\n");
|
||||||
|
printf("}\n");
|
||||||
|
|
||||||
|
printf("#endif\n");
|
||||||
|
|
||||||
|
printf("#endif\n");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user