tpl.h | tpl.h | |||
---|---|---|---|---|
/* | /* | |||
Copyright (c) 2005-2006, Troy Hanson http://tpl.sourceforge.net | Copyright (c) 2005-2007, Troy Hanson http://tpl.sourceforge.net | |||
All rights reserved. | All rights reserved. | |||
Redistribution and use in source and binary forms, with or without | Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions are met: | modification, are permitted provided that the following conditions are met: | |||
* Redistributions of source code must retain the above copyright | * Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | notice, this list of conditions and the following disclaimer. | |||
* Redistributions in binary form must reproduce the above copyright | * Redistributions in binary form must reproduce the above copyright | |||
notice, this list of conditions and the following disclaimer in | notice, this list of conditions and the following disclaimer in | |||
the documentation and/or other materials provided with the | the documentation and/or other materials provided with the | |||
skipping to change at line 45 | skipping to change at line 45 | |||
#include <inttypes.h> /* uint32_t */ | #include <inttypes.h> /* uint32_t */ | |||
#if defined __cplusplus | #if defined __cplusplus | |||
extern "C" { | extern "C" { | |||
#endif | #endif | |||
#define TPL_API | #define TPL_API | |||
#define TPL_MAGIC "tpl" | #define TPL_MAGIC "tpl" | |||
/* macro to add a structure to a doubly-linked list */ | ||||
#define DL_ADD(head,add) \ | ||||
do { \ | ||||
if (head) { \ | ||||
(add)->prev = (head)->prev; \ | ||||
(head)->prev->next = (add); \ | ||||
(head)->prev = (add); \ | ||||
(add)->next = NULL; \ | ||||
} else { \ | ||||
(head)=(add); \ | ||||
(head)->prev = (head); \ | ||||
(head)->next = NULL; \ | ||||
} \ | ||||
} while (0); | ||||
/* values for the flags byte that appears after the magic prefix */ | /* values for the flags byte that appears after the magic prefix */ | |||
#define TPL_SUPPORTED_BITFLAGS 1 | #define TPL_SUPPORTED_BITFLAGS 1 | |||
#define TPL_FL_BIGENDIAN (1 << 0) | #define TPL_FL_BIGENDIAN (1 << 0) | |||
/* bit flags used at runtime, not stored in the tpl */ | /* bit flags used at runtime, not stored in the tpl */ | |||
#define TPL_FILE (1 << 0) | #define TPL_FILE (1 << 0) | |||
#define TPL_MEM (1 << 1) | #define TPL_MEM (1 << 1) | |||
#define TPL_FD (1 << 2) | #define TPL_FD (1 << 2) | |||
#define TPL_RDONLY (1 << 3) /* tpl was loaded (for unpacking) */ | #define TPL_RDONLY (1 << 3) /* tpl was loaded (for unpacking) */ | |||
#define TPL_WRONLY (1 << 4) /* app has initiated tpl packing */ | #define TPL_WRONLY (1 << 4) /* app has initiated tpl packing */ | |||
#define TPL_XENDIAN (1 << 5) /* swap endianness when unpacking */ | #define TPL_XENDIAN (1 << 5) /* swap endianness when unpacking */ | |||
#define TPL_UFREE (1 << 6) /* free mem img when freeing tpl */ | #define TPL_UFREE (1 << 6) /* free mem img when freeing tpl */ | |||
#define TPL_INFER_FMT (1 << 7) /* infer format string from image */ | ||||
/* char values for node type */ | /* char values for node type */ | |||
#define TPL_TYPE_ROOT 0 | #define TPL_TYPE_ROOT 0 | |||
#define TPL_TYPE_INT32 1 | #define TPL_TYPE_INT32 1 | |||
#define TPL_TYPE_UINT32 2 | #define TPL_TYPE_UINT32 2 | |||
#define TPL_TYPE_BYTE 3 | #define TPL_TYPE_BYTE 3 | |||
#define TPL_TYPE_STR 4 | #define TPL_TYPE_STR 4 | |||
#define TPL_TYPE_ARY 5 | #define TPL_TYPE_ARY 5 | |||
#define TPL_TYPE_BIN 6 | #define TPL_TYPE_BIN 6 | |||
#define TPL_TYPE_DOUBLE 7 | #define TPL_TYPE_DOUBLE 7 | |||
#define TPL_TYPE_INT64 8 | #define TPL_TYPE_INT64 8 | |||
#define TPL_TYPE_UINT64 9 | #define TPL_TYPE_UINT64 9 | |||
/* flags for tpl_gather mode */ | ||||
#define TPL_GATHER_BLOCKING 1 | ||||
#define TPL_GATHER_NONBLOCKING 2 | ||||
#define TPL_GATHER_MEM 3 | ||||
/* error codes */ | /* error codes */ | |||
#define ERR_NOT_MINSIZE (-1) | #define ERR_NOT_MINSIZE (-1) | |||
#define ERR_MAGIC_MISMATCH (-2) | #define ERR_MAGIC_MISMATCH (-2) | |||
#define ERR_INCONSISTENT_SZ (-3) | #define ERR_INCONSISTENT_SZ (-3) | |||
#define ERR_FMT_INVALID (-4) | #define ERR_FMT_INVALID (-4) | |||
#define ERR_FMT_MISSING_NUL (-5) | #define ERR_FMT_MISSING_NUL (-5) | |||
#define ERR_FMT_MISMATCH (-6) | #define ERR_FMT_MISMATCH (-6) | |||
#define ERR_INCONSISTENT_SZ2 (-7) | #define ERR_FLEN_MISMATCH (-7) | |||
#define ERR_INCONSISTENT_SZ3 (-8) | #define ERR_INCONSISTENT_SZ2 (-8) | |||
#define ERR_UNSUPPORTED_FLAGS (-9) | #define ERR_INCONSISTENT_SZ3 (-9) | |||
#define ERR_INCONSISTENT_SZ4 (-10) | ||||
#define ERR_UNSUPPORTED_FLAGS (-11) | ||||
/* Hooks for error logging, memory allocation functions and other config | /* Hooks for error logging, memory allocation functions and other config | |||
* parameters to allow customization. | * parameters to allow customization. | |||
*/ | */ | |||
typedef int (tpl_print_fcn)(const char *fmt, ...); | typedef int (tpl_print_fcn)(const char *fmt, ...); | |||
typedef void *(tpl_malloc_fcn)(size_t sz); | typedef void *(tpl_malloc_fcn)(size_t sz); | |||
typedef void *(tpl_realloc_fcn)(void *ptr, size_t sz); | typedef void *(tpl_realloc_fcn)(void *ptr, size_t sz); | |||
typedef void (tpl_free_fcn)(void *ptr); | typedef void (tpl_free_fcn)(void *ptr); | |||
typedef void (tpl_fatal_fcn)(char *fmt, ...); | typedef void (tpl_fatal_fcn)(char *fmt, ...); | |||
typedef struct tpl_hook_t { | typedef struct tpl_hook_t { | |||
tpl_print_fcn *oops; | tpl_print_fcn *oops; | |||
tpl_malloc_fcn *malloc; | tpl_malloc_fcn *malloc; | |||
tpl_realloc_fcn *realloc; | tpl_realloc_fcn *realloc; | |||
tpl_free_fcn *free; | tpl_free_fcn *free; | |||
tpl_fatal_fcn *fatal; | tpl_fatal_fcn *fatal; | |||
int dequeue_maxlen; | size_t gather_max; | |||
} tpl_hook_t; | } tpl_hook_t; | |||
typedef struct tpl_pidx { | typedef struct tpl_pidx { | |||
struct tpl_node *node; | struct tpl_node *node; | |||
struct tpl_pidx *next; | struct tpl_pidx *next,*prev; | |||
} tpl_pidx; | } tpl_pidx; | |||
typedef struct tpl_node { | typedef struct tpl_node { | |||
char type; | char type; | |||
void *addr; | void *addr; | |||
void *data; /* r:tpl_root_data*. A:tpl_atyp*. ow:szof ty | void *data; /* r:tpl_root_data*. A:tpl_atyp*. ow:szof | |||
pe */ | type */ | |||
struct tpl_node *children; /* child linked-list head */ | int num; /* length of type if its a C array */ | |||
struct tpl_node *next; /* next child of parent */ | size_t ser_osz; /* serialization output size for subtree * | |||
struct tpl_node *parent; /* parent of this node */ | / | |||
struct tpl_node *children; /* my children; linked-list */ | ||||
struct tpl_node *next,*prev; /* my siblings (next child of my parent) * | ||||
/ | ||||
struct tpl_node *parent; /* my parent */ | ||||
} tpl_node; | } tpl_node; | |||
typedef struct tpl_atyp { | typedef struct tpl_atyp { | |||
uint32_t num; /* num elements */ | uint32_t num; /* num elements */ | |||
size_t sz; /* size of each backbone's datum */ | size_t sz; /* size of each backbone's datum */ | |||
struct tpl_backbone *bb,*bbtail; | struct tpl_backbone *bb,*bbtail; | |||
void *cur; | void *cur; | |||
} tpl_atyp; | } tpl_atyp; | |||
typedef struct tpl_backbone { | typedef struct tpl_backbone { | |||
void *data; | ||||
struct tpl_backbone *next; | struct tpl_backbone *next; | |||
/* when this structure is malloc'd, extra space is alloc'd at the | ||||
* end to store the backbone "datum", and data points to it. */ | ||||
char data[]; | ||||
} tpl_backbone; | } tpl_backbone; | |||
typedef struct tpl_mmap_rec { | typedef struct tpl_mmap_rec { | |||
int fd; | int fd; | |||
void *text; | void *text; | |||
size_t text_sz; | size_t text_sz; | |||
} tpl_mmap_rec; | } tpl_mmap_rec; | |||
typedef struct tpl_root_data { | typedef struct tpl_root_data { | |||
int flags; | int flags; | |||
tpl_pidx *pidx; | tpl_pidx *pidx; | |||
tpl_mmap_rec mmap; | tpl_mmap_rec mmap; | |||
char *fmt; | ||||
int *fxlens, num_fxlens; | ||||
} tpl_root_data; | } tpl_root_data; | |||
struct tpl_type_t { | struct tpl_type_t { | |||
char idx; | ||||
char c; | char c; | |||
int sz; | int sz; | |||
}; | }; | |||
/* used when un/packing 'B' type (binary buffers) */ | /* used when un/packing 'B' type (binary buffers) */ | |||
typedef struct tpl_bin { | typedef struct tpl_bin { | |||
void *addr; | void *addr; | |||
uint32_t sz; | uint32_t sz; | |||
} tpl_bin; | } tpl_bin; | |||
/* The next structure is used for async/piecemeal reading of tpl images */ | /* The next structure is used for async/piecemeal reading of tpl images */ | |||
typedef struct tpl_gather_t { | typedef struct tpl_gather_t { | |||
char *img; | char *img; | |||
int len; | int len; | |||
} tpl_gather_t; | } tpl_gather_t; | |||
/* Callback used when tpl_gather has read a full tpl image */ | /* Callback used when tpl_gather has read a full tpl image */ | |||
typedef int (tpl_gather_cb)(void *img, int sz, void *data); | typedef int (tpl_gather_cb)(void *img, size_t sz, void *data); | |||
/* Prototypes */ | /* Prototypes */ | |||
TPL_API tpl_node *tpl_map(char *fmt,...); /* define tpl using format, | TPL_API tpl_node *tpl_map(char *fmt,...); /* define tpl using format | |||
addresses */ | */ | |||
TPL_API void tpl_free(tpl_node *r); /* free a tpl map */ | TPL_API void tpl_free(tpl_node *r); /* free a tpl map */ | |||
TPL_API int tpl_pack(tpl_node *r, int i); /* pack the n'th packable * / | TPL_API int tpl_pack(tpl_node *r, int i); /* pack the n'th packable * / | |||
TPL_API int tpl_unpack(tpl_node *r, int i); /* unpack the n'th packable */ | TPL_API int tpl_unpack(tpl_node *r, int i); /* unpack the n'th packable */ | |||
TPL_API int tpl_dump(tpl_node *r, int mode, ...); /* serialize to mem or fi le */ | TPL_API int tpl_dump(tpl_node *r, int mode, ...); /* serialize to mem/file */ | |||
TPL_API int tpl_load(tpl_node *r, int mode, ...); /* set mem/file to unpack */ | TPL_API int tpl_load(tpl_node *r, int mode, ...); /* set mem/file to unpack */ | |||
TPL_API int tpl_Alen(tpl_node *r, int i); /* array len of packable i * | ||||
TPL_API int tpl_Alen(tpl_node *r, int i); /* array len of packable i | / | |||
*/ | TPL_API char* tpl_peek(int mode, ...); /* sneak peek at format stri | |||
ng */ | ||||
TPL_API int tpl_packN(tpl_node *r, int i, int count, int stride); | TPL_API int tpl_gather( int mode, ...); /* non-blocking image gather | |||
TPL_API int tpl_unpackN(tpl_node *r, int i, int count, int stride); | */ | |||
/* tpl_acquire reads a single tpl image from a blocking fd then returns. */ | ||||
/* tpl_gather can be used by select()-driven apps to read tpl images piecem | ||||
eal */ | ||||
TPL_API int tpl_acquire(int fd, void **img, size_t *sz); | ||||
TPL_API int tpl_gather( int fd, tpl_gather_t **gs, tpl_gather_cb *cb, void | ||||
*data); | ||||
/* Internal prototypes */ | ||||
tpl_node *tpl_node_new(tpl_node *parent); | ||||
tpl_node *tpl_find_i(tpl_node *n, int i); | ||||
void *tpl_cpv(void *datav, void *data, size_t sz); | ||||
void *tpl_extend_backbone(tpl_node *n); | ||||
char *tpl_fmt(tpl_node *n); | ||||
void *tpl_dump_atyp(tpl_node *n, tpl_atyp* at, void *dv); | ||||
int tpl_sersz(tpl_node *n,tpl_atyp *at); | ||||
void tpl_free_atyp(tpl_node *n,tpl_atyp *atyp); | ||||
int tpl_dump_to_mem(tpl_node *r, void *addr, int sz); | ||||
int tpl_mmap_file(char *filename, tpl_mmap_rec *map_rec); | ||||
int tpl_mmap_output_file(char *filename, size_t sz, void **text_out); | ||||
int tpl_cpu_bigendian(void); | ||||
int tpl_needs_endian_swap(void *); | ||||
void tpl_byteswap(void *word, int len); | ||||
void tpl_fatal(char *fmt, ...); | ||||
int tpl_serlen(tpl_node *r, tpl_node *n, void *dv, size_t *serlen); | ||||
int tpl_unpackA0(tpl_node *r); | ||||
#if defined __cplusplus | #if defined __cplusplus | |||
} | } | |||
#endif | #endif | |||
#endif /* TPL_H */ | #endif /* TPL_H */ | |||
End of changes. 16 change blocks. | ||||
50 lines changed or deleted | 54 lines changed or added | |||