#ifndef DTOB_INTERNAL_H
#define DTOB_INTERNAL_H
#include "dtob.h"
#include "json.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
size_t trit_encode_padded(const uint8_t *bytes, size_t byte_len,
uint8_t **out_buf);
size_t trit_decode_padded(const uint8_t *buf, size_t buf_len,
uint8_t **out_bytes);
typedef enum {
TOK_DATA,
TOK_OPEN,
TOK_ARR_CLOSE,
TOK_KV_CLOSE,
TOK_TYPES_CLOSE,
TOK_UQT,
TOK_T_RAW,
TOK_T_FLOAT,
TOK_T_DOUBLE,
TOK_T_INT8,
TOK_T_INT16,
TOK_T_INT32,
TOK_T_INT64,
TOK_T_UINT8,
TOK_T_UINT16,
TOK_T_UINT32,
TOK_T_UINT64,
TOK_CUSTOM,
TOK_ERROR,
TOK_END
} TokenType;
typedef struct {
TokenType type;
uint16_t custom_code;
uint8_t *data;
size_t data_len;
} Token;
typedef struct {
const uint8_t *buf;
size_t len;
size_t pos;
} Lexer;
void lexer_init(Lexer *l, const uint8_t *buf, size_t len);
Token lexer_next(Lexer *l);
DtobValue *ast_make(DtobType type);
void ast_add_element(DtobValue *arr, DtobValue *el);
void ast_add_pair(DtobValue *kvs, uint8_t *key, size_t key_len,
DtobValue *val);
#define PTRCACHE_BUCKETS 256
typedef struct PtrCacheEntry PtrCacheEntry;
struct PtrCacheEntry {
size_t offset;
DtobValue *value;
PtrCacheEntry *next;
};
typedef struct {
PtrCacheEntry *buckets[PTRCACHE_BUCKETS];
} PtrCache;
void ptrcache_init(PtrCache *cache);
DtobValue *ptrcache_lookup(PtrCache *cache, size_t offset);
void ptrcache_add(PtrCache *cache, size_t offset, DtobValue *value);
void ptrcache_free(PtrCache *cache);
typedef struct {
uint8_t *buf;
size_t cap;
size_t pos;
int error;
int strict_validation;
} ByteWriter;
void bw_init(ByteWriter *w, int strict_validation);
void bw_write_byte(ByteWriter *w, uint8_t b);
void bw_write_ctrl(ByteWriter *w, uint16_t code);
void bw_write_data(ByteWriter *w, const uint8_t *bytes, size_t len);
#endif