Page:
n__avro_8c
Pages
Examples
Files
Home
Module AABB3D
Module ACCEPT_POOL
Module ALLEGRO4
Module ALLEGRO5
Module ANIM_LIB
Module ASTAR
Module ATLAS_LIB
Module AVRO
Module CLOCK_SYNC
Module COMMONS
Module CONFIGFILE
Module CYPHER_BASE64
Module CYPHER_VIGENERE
Module DEAD_RECKONING
Module ENUMS
Module EXCEPTIONS
Module GAME_ENV
Module GIT
Module GUI
Module HASH_TABLE
Module ISOMETRIC_ENGINE
Module LIST
Module LOG
Module LOGNODUP
Module LZ4
Module NETWORKING
Module NETWORK_MSG
Module N_COOKIES
Module N_DIFF
Module N_DIGEST
Module N_DISK_CACHE
Module N_DNS
Module N_ECC
Module N_ENTROPY
Module N_FILES
Module N_FLUIDS
Module N_GUI_COLOR
Module N_GUI_COLORWHEEL
Module N_GUI_CURVE
Module N_GUI_GRID
Module N_GUI_POPUP
Module N_HEX
Module N_HTTP3
Module N_HTTP_COOKIE
Module N_HTTP_FORM
Module N_HTTP_MIME
Module N_HTTP_REQ
Module N_HTTP_RESP
Module N_HTTP_ROUTER
Module N_HTTP_SERVER
Module N_ICC
Module N_JSON
Module N_KAFKA
Module N_LINALG
Module N_MODULE
Module N_OPENSSL_COMPAT
Module N_PRETTY
Module N_PTH
Module N_QUERY
Module N_RANDOM
Module N_SHA256
Module N_STR
Module N_TIFF
Module N_TIME
Module N_USER
Module N_X509
Module N_ZIP
Module PARTICLES
Module PCRE
Module PHYSICS
Module SIGNALS
Module STACK
Module THREADS
Module TRAJECTORY
Module TREE
Module ZLIB
Modules
Pages
Struct A4_BMP
Struct A4_TIMER
Struct AABB3D
Struct ANIM_DATA
Struct ANIM_FRAME
Struct ANIM_GFX
Struct ANIM_LIB
Struct ASTAR_BOX
Struct ASTAR_CELL
Struct ASTAR_GRID
Struct ASTAR_HEAP
Struct ASTAR_HEAP_NODE
Struct ASTAR_NODE
Struct ASTAR_PATH
Struct AVRO_FIELD
Struct AVRO_READER
Struct AVRO_SCHEMA
Struct BITMAP
Struct CALLBACK_DATA
Struct CA_CTX
Struct CLIENT_JOB
Struct CLIENT_STATE
Struct CONFIG_FILE
Struct CONFIG_FILE_SECTION
Struct CYPHER_PARAM
Struct DATA
Struct DEMO_PANEL
Struct DICTIONARY_DEFINITION
Struct DICTIONARY_ENTRY
Struct DIFF_OUT
Struct DIFF_TEXT
Struct DRAW_STATE
Struct DR_ENTITY
Struct DR_STATE
Struct DR_VEC3
Struct ExceptionContextList
Struct FONT
Struct GAME_ENV
Struct H2BUF
Struct H2_DYN_ENTRY
Struct H2_STREAM
Struct HASH_NODE
Struct HASH_TABLE
Struct ISO_DRAW_ENTRY
Struct ISO_MAP
Struct ISO_PROJECTION
Struct ISO_TILE_SEGMENT
Struct ISO_TILE_SEGMENTS
Struct ISO_VISIBLE_EDGES
Struct JS_STATE
Struct LEX_ITEM
Struct LIST
Struct LIST_NODE
Struct LOG_LEVELS
Struct LexMenu
Struct MEMBER
Struct MIDI
Struct NESTED
Struct NETWORK
Struct NETWORK_HTTP_INFO
Struct NETWORK_POOL
Struct NETWORK_SSL_THREAD_PARAMS
Struct NETW_ACCEPT_POOL
Struct NETW_ACCEPT_POOL_STATS
Struct NETW_MSG
Struct NETW_SNI_ARG
Struct NODE_DATA
Struct N_ATLAS
Struct N_ATLAS_PAGE
Struct N_CLOCK_SYNC
Struct N_COOKIE
Struct N_COOKIE_JAR
Struct N_DIFF_LINE
Struct N_DIFF_RESULT
Struct N_DISK_CACHE
Struct N_ENTROPY_BIT
Struct N_ENTROPY_POS
Struct N_FILE_INFO
Struct N_FLUID
Struct N_FLUID_THREAD_PARAMS
Struct N_FORM_FIELD
Struct N_GIT_COMMIT_INFO
Struct N_GIT_REMOTE_AUTH
Struct N_GIT_REPO
Struct N_GIT_STATUS_ENTRY
Struct N_GUI_BUTTON_DATA
Struct N_GUI_CHECKBOX_DATA
Struct N_GUI_COLOR_PICKER
Struct N_GUI_COMBOBOX_DATA
Struct N_GUI_CTX
Struct N_GUI_CUSTOM_DATA
Struct N_GUI_DATAGRID_COL
Struct N_GUI_DATAGRID_DATA
Struct N_GUI_DROPMENU_DATA
Struct N_GUI_DROPMENU_ENTRY
Struct N_GUI_GRID_CALLBACKS
Struct N_GUI_HEXVIEW_DATA
Struct N_GUI_IMAGE_DATA
Struct N_GUI_KVTABLE
Struct N_GUI_KV_CONTENT
Struct N_GUI_KV_ROW
Struct N_GUI_LABEL_DATA
Struct N_GUI_LISTBOX_DATA
Struct N_GUI_LISTITEM
Struct N_GUI_MARKDOWNVIEW_DATA
Struct N_GUI_MD_FONTS
Struct N_GUI_MD_STYLE
Struct N_GUI_MEAS_ENTRY
Struct N_GUI_MOUSE_EVENT
Struct N_GUI_PENDING_GEOM
Struct N_GUI_PENDING_WIDGET
Struct N_GUI_POPUP
Struct N_GUI_PROGRESSBAR_DATA
Struct N_GUI_RADIOLIST_DATA
Struct N_GUI_SB_TRACK
Struct N_GUI_SCROLLBAR_DATA
Struct N_GUI_SECTION
Struct N_GUI_SECTIONLIST
Struct N_GUI_SLIDER_DATA
Struct N_GUI_SPLITPANE_DATA
Struct N_GUI_STYLE
Struct N_GUI_SUGGEST_SINK
Struct N_GUI_SYNTAXVIEW_DATA
Struct N_GUI_TAB_PANEL
Struct N_GUI_TB_BUTTONS
Struct N_GUI_TEXTAREA_DATA
Struct N_GUI_TEXT_DIMS
Struct N_GUI_THEME
Struct N_GUI_TREE
Struct N_GUI_TREE_NODE
Struct N_GUI_TREE_STATE
Struct N_GUI_WIDGET
Struct N_GUI_WINDOW
Struct N_H2_FRAME
Struct N_H2_HEADER
Struct N_H2_HPACK
Struct N_H2_REQUEST
Struct N_H2_SERVER
Struct N_H2_SETTING
Struct N_HTML_ATTR_
Struct N_HTML_FORM
Struct N_HTML_TAG_
Struct N_HTTP3_RESPONSE
Struct N_HTTP_FIELD
Struct N_HTTP_FORM
Struct N_HTTP_FORM_MULTIPART
Struct N_HTTP_FORM_PART
Struct N_HTTP_MULTIPART_PART
Struct N_HTTP_REQUEST
Struct N_HTTP_RESP
Struct N_HTTP_RESPONSE
Struct N_HTTP_SERVER
Struct N_HTTP_SERVER_CONFIG
Struct N_HTTP_SERVER_REQUEST
Struct N_HTTP_SERVER_STATS
Struct N_HTTP_SET_COOKIE
Struct N_HTTP_STREAM
Struct N_ISO_CAMERA
Struct N_ISO_OBJECT
Struct N_KAFKA
Struct N_KAFKA_EVENT
Struct N_MOCK_SERVER
Struct N_MODULE
Struct N_PARALLEL_SLICE
Struct N_PARALLEL_TEAM
Struct N_PARALLEL_WORKER
Struct N_PCRE
Struct N_PROXY_CFG
Struct N_PTH
Struct N_PTH_TENSOR
Struct N_QUERY
Struct N_ROUTE
Struct N_ROUTER
Struct N_ROUTE_MATCH
Struct N_SHA256
Struct N_SOCKET
Struct N_SPAWN_PIPE
Struct N_SSE_CONN
Struct N_SSE_EVENT
Struct N_STR
Struct N_TIFF
Struct N_TIFF_ENTRY
Struct N_TIME
Struct N_URL
Struct N_URL_PARAM
Struct N_USER
Struct N_USERLIST
Struct N_WS_CONN
Struct N_WS_FRAME
Struct N_WS_MESSAGE
Struct N_ZIP_MEMBER
Struct OCTREE
Struct OCTREE_NODE
Struct PARSER
Struct PARTICLE
Struct PARTICLE_EMITTER
Struct PARTICLE_SYSTEM
Struct PEER
Struct PHYSICS
Struct PK_OBJ
Struct PK_VM
Struct POINT2D
Struct POINT3D
Struct PRETTY_OUT
Struct QNODE
Struct QUADTREE
Struct QUADTREE_NODE
Struct REC
Struct RGB
Struct SAMPLE
Struct SEEN
Struct SHARER
Struct SRV
Struct SRV_ARG
Struct STACK
Struct STACK_ITEM
Struct SYNTAX_RUN
Struct THREAD_POOL
Struct THREAD_POOL_NODE
Struct THREAD_WAITING_PROC
Struct TRAJECTORY
Struct TRAJECTORY_POINT
Struct TREE
Struct TREE_NODE
Struct TS_LOG
Struct WAYPOINT3D
Struct WORKER_ARG
Struct ZIP_SINK
Struct _N_DC_ENTRY
Struct _N_DC_GROUP_ENTRY
Struct _N_GUI_COLORWHEEL
Struct _N_GUI_COLORWHEEL_BLOCK
Struct _N_GUI_COLORWHEEL_MOUSE
Struct _N_GUI_CURVE
Struct _N_GUI_CURVE_BLOCK
Struct _N_GUI_CURVE_MOUSE
Struct _N_GUI_GRID
Struct _N_GUI_POPUP_ENTRY
Struct _N_GUI_POPUP_LEVEL
Struct _N_GUI_SB_SKIN
Struct _N_GUI_TA_STEP
Struct _N_GUI_THUMB
Struct _N_GUI_WRAP_ACC
Struct _compiled
Struct _conn
Struct _ip_slot
Struct _mp_param
Struct _n_civil
Struct _seg
Struct client
Struct collector
Struct date_vector
Struct md_atom
Struct md_cells
Struct md_hit
Struct md_line_ctx
Struct mline
Struct n_http_req_header
Struct n_http_req_limits
Struct n_http_request
Struct n_markdown_block
Struct n_markdown_doc
Struct n_markdown_inline
Struct n_markdown_span
Struct n_reactor_stats
Struct n_url_safe
Struct n_url_safe_head
Struct n_wrap_item
Struct producer_state
Struct rects
Struct reply
Struct runner
Struct sink
Struct tbuf
Struct wrap_hit
Struct wrap_span
Structures
Union COORD_VALUE
Union HASH_DATA
Union NODE_DATA_TYPES
Union STACK_DATA
dir_310dcdd4703e877a6b99d5dc3fc54d27
dir_68267d1309a1af8e8297ef4c3efbcdba
dir_d28a4824dc47e487b107a5db32ef43c4
dir_d44c64559bbebec7f509842c48db8b23
ex__accept__pool__client_8c
ex__accept__pool__server_8c
ex__allegro__4_8c
ex__atlas_8c
ex__avro_8c
ex__base64_8c
ex__bind__exclusive_8c
ex__clipboard_8c
ex__clock__sync_8c
ex__common_8c
ex__configfile_8c
ex__cookies_8c
ex__crypto_8c
ex__diff_8c
ex__digest_8c
ex__disk__cache_8c
ex__dns_8c
ex__ecc_8c
ex__entropy_8c
ex__exceptions_8c
ex__file_8c
ex__file__copy_8c
ex__fluid_8c
ex__fluid__config_8c
ex__fluid__config_8h
ex__git_8c
ex__gui_8c
ex__gui__bench_8c
ex__gui__button__keys_8c
ex__gui__checkbox__label_8c
ex__gui__checkbox__release_8c
ex__gui__color__picker_8c
ex__gui__colorwheel_8c
ex__gui__combobox__items_8c
ex__gui__combobox__wheel_8c
ex__gui__cursor_8c
ex__gui__curve_8c
ex__gui__custom_8c
ex__gui__custom__mouse_8c
ex__gui__datagrid_8c
ex__gui__datagrid__keys_8c
ex__gui__datagrid__sort_8c
ex__gui__detach_8c
ex__gui__dictionary_8c
ex__gui__display__fallback_8c
ex__gui__double__click_8c
ex__gui__dropmenu_8c
ex__gui__frameless__drag_8c
ex__gui__global__scroll_8c
ex__gui__glyph_8c
ex__gui__grid_8c
ex__gui__hexview_8c
ex__gui__hover_8c
ex__gui__isometric_8c
ex__gui__kvtable_8c
ex__gui__label__fits_8c
ex__gui__label__lines_8c
ex__gui__layout__persist_8c
ex__gui__listbox_8c
ex__gui__md_8c
ex__gui__multiwin_8c
ex__gui__network_8c
ex__gui__particles_8c
ex__gui__placeholder_8c
ex__gui__popup_8c
ex__gui__progressbar_8c
ex__gui__reentrant_8c
ex__gui__round_8c
ex__gui__sectionlist_8c
ex__gui__slider__default_8c
ex__gui__slider__handle_8c
ex__gui__slider__step_8c
ex__gui__splitpane_8c
ex__gui__suggest_8c
ex__gui__syntaxview_8c
ex__gui__text__shorten_8c
ex__gui__textarea__selection_8c
ex__gui__tooltip_8c
ex__gui__tree__lazy_8c
ex__gui__tree__order_8c
ex__gui__tree__state_8c
ex__gui__window__rect_8c
ex__gui__zorder_8c
ex__hash_8c
ex__hex_8c
ex__html_8c
ex__http2_8c
ex__http2__server_8c
ex__http3_8c
ex__http__cookie_8c
ex__http__form_8c
ex__http__mime_8c
ex__http__req_8c
ex__http__response_8c
ex__http__router_8c
ex__http__server_8c
ex__icc_8c
ex__iso__astar_8c
ex__json_8c
ex__kafka_8c
ex__linalg_8c
ex__list_8c
ex__log_8c
ex__markdown_8c
ex__module_8c
ex__monolith_8c
ex__network_8c
ex__network_8h
ex__network__accept_8c
ex__network__alpn_8c
ex__network__connect__abort_8c
ex__network__mock_8c
ex__network__proxy_8c
ex__network__reactor_8c
ex__network__sni_8c
ex__network__sse_8c
ex__network__ssl_8c
ex__network__ssl__hardened_8c
ex__network__ssl__hostname_8c
ex__network__ssl__nonblock_8c
ex__network__tls__defaults_8c
ex__network__ws_8c
ex__network__ws__limits_8c
ex__nstr_8c
ex__parallel__for_8c
ex__parallel__team_8c
ex__pcre_8c
ex__pretty_8c
ex__pth_8c
ex__query_8c
ex__random_8c
ex__sha256_8c
ex__signals_8c
ex__spawn_8c
ex__ssrf_8c
ex__stack_8c
ex__thread__pool__discard_8c
ex__thread__pool__wait_8c
ex__threads_8c
ex__tiff_8c
ex__time__formats_8c
ex__trajectory_8c
ex__trees_8c
ex__url_8c
ex__url__safe_8c
ex__wrap_8c
ex__x509_8c
ex__zip_8c
ex__zlib_8c
ex_accept_pool_client_8c example
ex_accept_pool_server_8c example
ex_allegro_4_8c example
ex_atlas_8c example
ex_base64_8c example
ex_clock_sync_8c example
ex_common_8c example
ex_configfile_8c example
ex_cookies_8c example
ex_crypto_8c example
ex_diff_8c example
ex_digest_8c example
ex_disk_cache_8c example
ex_dns_8c example
ex_ecc_8c example
ex_entropy_8c example
ex_exceptions_8c example
ex_file_8c example
ex_file_copy_8c example
ex_fluid_8c example
ex_fluid_config_8c example
ex_fluid_config_8h example
ex_git_8c example
ex_gui_8c example
ex_gui_bench_8c example
ex_gui_button_keys_8c example
ex_gui_checkbox_label_8c example
ex_gui_checkbox_release_8c example
ex_gui_color_picker_8c example
ex_gui_colorwheel_8c example
ex_gui_combobox_items_8c example
ex_gui_combobox_wheel_8c example
ex_gui_cursor_8c example
ex_gui_curve_8c example
ex_gui_custom_8c example
ex_gui_custom_mouse_8c example
ex_gui_datagrid_8c example
ex_gui_datagrid_keys_8c example
ex_gui_datagrid_sort_8c example
ex_gui_detach_8c example
ex_gui_dictionary_8c example
ex_gui_display_fallback_8c example
ex_gui_double_click_8c example
ex_gui_dropmenu_8c example
ex_gui_frameless_drag_8c example
ex_gui_global_scroll_8c example
ex_gui_glyph_8c example
ex_gui_grid_8c example
ex_gui_hexview_8c example
ex_gui_hover_8c example
ex_gui_isometric_8c example
ex_gui_kvtable_8c example
ex_gui_label_fits_8c example
ex_gui_label_lines_8c example
ex_gui_layout_persist_8c example
ex_gui_listbox_8c example
ex_gui_md_8c example
ex_gui_multiwin_8c example
ex_gui_network_8c example
ex_gui_particles_8c example
ex_gui_placeholder_8c example
ex_gui_popup_8c example
ex_gui_progressbar_8c example
ex_gui_reentrant_8c example
ex_gui_round_8c example
ex_gui_sectionlist_8c example
ex_gui_slider_default_8c example
ex_gui_slider_handle_8c example
ex_gui_slider_step_8c example
ex_gui_splitpane_8c example
ex_gui_suggest_8c example
ex_gui_syntaxview_8c example
ex_gui_text_shorten_8c example
ex_gui_textarea_selection_8c example
ex_gui_tooltip_8c example
ex_gui_tree_lazy_8c example
ex_gui_tree_order_8c example
ex_gui_tree_state_8c example
ex_gui_window_rect_8c example
ex_gui_zorder_8c example
ex_hash_8c example
ex_hex_8c example
ex_html_8c example
ex_http2_8c example
ex_http2_server_8c example
ex_http_cookie_8c example
ex_http_form_8c example
ex_http_mime_8c example
ex_http_req_8c example
ex_http_response_8c example
ex_http_router_8c example
ex_http_server_8c example
ex_icc_8c example
ex_iso_astar_8c example
ex_json_8c example
ex_kafka_8c example
ex_linalg_8c example
ex_list_8c example
ex_log_8c example
ex_markdown_8c example
ex_module_8c example
ex_monolith_8c example
ex_network_8c example
ex_network_8h example
ex_network_accept_8c example
ex_network_alpn_8c example
ex_network_connect_abort_8c example
ex_network_mock_8c example
ex_network_proxy_8c example
ex_network_reactor_8c example
ex_network_sni_8c example
ex_network_sse_8c example
ex_network_ssl_8c example
ex_network_ssl_hardened_8c example
ex_network_ssl_hostname_8c example
ex_network_ssl_nonblock_8c example
ex_network_tls_defaults_8c example
ex_network_ws_8c example
ex_network_ws_limits_8c example
ex_nstr_8c example
ex_parallel_for_8c example
ex_parallel_team_8c example
ex_pcre_8c example
ex_pretty_8c example
ex_pth_8c example
ex_query_8c example
ex_random_8c example
ex_sha256_8c example
ex_signals_8c example
ex_spawn_8c example
ex_ssrf_8c example
ex_stack_8c example
ex_threads_8c example
ex_tiff_8c example
ex_time_formats_8c example
ex_trajectory_8c example
ex_trees_8c example
ex_url_8c example
ex_url_safe_8c example
ex_wrap_8c example
ex_x509_8c example
ex_zip_8c example
ex_zlib_8c example
mainpage_8dox
n__3d_8c
n__3d_8h
n__aabb_8c
n__aabb_8h
n__allegro5_8c
n__allegro5_8h
n__allegro__4_8c
n__allegro__4_8h
n__anim_8c
n__anim_8h
n__astar_8c
n__astar_8h
n__atlas_8c
n__atlas_8h
n__avro_8c
n__avro_8h
n__base64_8c
n__base64_8h
n__clipboard_8c
n__clipboard_8h
n__clock__sync_8c
n__clock__sync_8h
n__common_8c
n__common_8h
n__config__file_8c
n__config__file_8h
n__cookies_8c
n__cookies_8h
n__crypto_8c
n__crypto_8h
n__dead__reckoning_8c
n__dead__reckoning_8h
n__diff_8c
n__diff_8h
n__digest_8c
n__digest_8h
n__disk__cache_8c
n__disk__cache_8h
n__dns_8c
n__dns_8h
n__ecc_8c
n__ecc_8h
n__entropy_8c
n__entropy_8h
n__enum_8h
n__exceptions_8c
n__exceptions_8h
n__files_8c
n__files_8h
n__fluids_8c
n__fluids_8h
n__games_8c
n__games_8h
n__git_8c
n__git_8h
n__gui_8c
n__gui_8h
n__gui__color_8c
n__gui__color_8h
n__gui__colorwheel_8c
n__gui__colorwheel_8h
n__gui__curve_8c
n__gui__curve_8h
n__gui__grid_8c
n__gui__grid_8h
n__gui__md_8c
n__gui__md_8h
n__gui__popup_8c
n__gui__popup_8h
n__hash_8c
n__hash_8h
n__hex_8c
n__hex_8h
n__html_8c
n__html_8h
n__http2_8c
n__http2_8h
n__http2__server_8c
n__http2__server_8h
n__http3_8c
n__http3_8h
n__http__cookie_8c
n__http__cookie_8h
n__http__form_8c
n__http__form_8h
n__http__mime_8c
n__http__mime_8h
n__http__req_8c
n__http__req_8h
n__http__response_8c
n__http__response_8h
n__http__router_8c
n__http__router_8h
n__http__server_8c
n__http__server_8h
n__icc_8c
n__icc_8h
n__iso__engine_8c
n__iso__engine_8h
n__json_8c
n__json_8h
n__kafka_8c
n__kafka_8h
n__linalg_8c
n__linalg_8h
n__list_8c
n__list_8h
n__log_8c
n__log_8h
n__lz4_8c
n__lz4_8h
n__markdown_8c
n__markdown_8h
n__module_8c
n__module_8h
n__network_8c
n__network_8h
n__network__accept__pool_8c
n__network__accept__pool_8h
n__network__msg_8c
n__network__msg_8h
n__nodup__log_8c
n__nodup__log_8h
n__openssl__compat_8h
n__particles_8c
n__particles_8h
n__pcre_8c
n__pcre_8h
n__pretty_8c
n__pretty_8h
n__pth_8c
n__pth_8h
n__query_8c
n__query_8h
n__random_8c
n__random_8h
n__reactor_8c
n__reactor_8h
n__sha256_8c
n__sha256_8h
n__signals_8c
n__signals_8h
n__ssrf_8c
n__ssrf_8h
n__stack_8c
n__stack_8h
n__str_8c
n__str_8h
n__thread__pool_8c
n__thread__pool_8h
n__tiff_8c
n__tiff_8h
n__time_8c
n__time_8h
n__trajectory_8c
n__trajectory_8h
n__trees_8c
n__trees_8h
n__url__safe_8c
n__url__safe_8h
n__user_8c
n__user_8h
n__windows_8h
n__wrap_8c
n__wrap_8h
n__x509_8c
n__x509_8h
n__zip_8c
n__zip_8h
n__zlib_8c
n__zlib_8h
nilorea_8h
No results
6
n__avro_8c
gullradriel edited this page 2026-10-09 00:55:18 +02:00
src/n_avro.c
Avro binary format encoding/decoding with JSON conversion. More...
Functions
| Name | |
|---|---|
| const char * | avro_type_to_string(AVRO_TYPE type) helper to get type name from AVRO_TYPE |
| AVRO_TYPE | avro_type_from_string(const char * type_str) helper to get AVRO_TYPE from a type name string |
| int | avro_find_union_branch(const AVRO_SCHEMA * schema, const cJSON * json) helper: find which union branch matches a cJSON value |
| int | avro_encode_bytes_raw(N_STR ** dest, const unsigned char * data, size_t len) helper: encode raw bytes with length prefix |
| int | avro_decode_bytes_raw(AVRO_READER * reader, unsigned char ** out, size_t * out_len) helper: read raw bytes with length prefix |
Detailed Description
Avro binary format encoding/decoding with JSON conversion.
Author: Castagnier Mickael
Version: 1.0
Date: 11/03/2026
Functions Documentation
function avro_type_to_string
static const char * avro_type_to_string(
AVRO_TYPE type
)
helper to get type name from AVRO_TYPE
function avro_type_from_string
static AVRO_TYPE avro_type_from_string(
const char * type_str
)
helper to get AVRO_TYPE from a type name string
function avro_find_union_branch
static int avro_find_union_branch(
const AVRO_SCHEMA * schema,
const cJSON * json
)
helper: find which union branch matches a cJSON value
function avro_encode_bytes_raw
static int avro_encode_bytes_raw(
N_STR ** dest,
const unsigned char * data,
size_t len
)
helper: encode raw bytes with length prefix
function avro_decode_bytes_raw
static int avro_decode_bytes_raw(
AVRO_READER * reader,
unsigned char ** out,
size_t * out_len
)
helper: read raw bytes with length prefix
Source code
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_avro.h"
#include <string.h>
#include <time.h>
/* Schema parsing */
static AVRO_TYPE avro_type_from_string(const char* type_str) {
if (strcmp(type_str, "null") == 0) return AVRO_NULL;
if (strcmp(type_str, "boolean") == 0) return AVRO_BOOLEAN;
if (strcmp(type_str, "int") == 0) return AVRO_INT;
if (strcmp(type_str, "long") == 0) return AVRO_LONG;
if (strcmp(type_str, "float") == 0) return AVRO_FLOAT;
if (strcmp(type_str, "double") == 0) return AVRO_DOUBLE;
if (strcmp(type_str, "bytes") == 0) return AVRO_BYTES;
if (strcmp(type_str, "string") == 0) return AVRO_STRING;
return AVRO_NULL;
}
static const char* avro_type_to_string(AVRO_TYPE type) {
switch (type) {
case AVRO_NULL:
return "null";
case AVRO_BOOLEAN:
return "boolean";
case AVRO_INT:
return "int";
case AVRO_LONG:
return "long";
case AVRO_FLOAT:
return "float";
case AVRO_DOUBLE:
return "double";
case AVRO_BYTES:
return "bytes";
case AVRO_STRING:
return "string";
default:
return "null";
}
}
AVRO_SCHEMA* avro_schema_from_cjson(const cJSON* json) {
__n_assert(json, return NULL);
AVRO_SCHEMA* schema = NULL;
Malloc(schema, AVRO_SCHEMA, 1);
__n_assert(schema, return NULL);
/* primitive type as string: "null", "boolean", "int", etc. */
if (cJSON_IsString(json)) {
schema->type = avro_type_from_string(cJSON_GetStringValue((cJSON*)json));
return schema;
}
/* union: JSON array of schemas */
if (cJSON_IsArray(json)) {
schema->type = AVRO_UNION;
int nb = cJSON_GetArraySize(json);
schema->nb_branches = (size_t)nb;
Malloc(schema->union_branches, AVRO_SCHEMA*, (size_t)nb);
__n_assert(schema->union_branches, Free(schema); return NULL);
for (int i = 0; i < nb; i++) {
schema->union_branches[i] = avro_schema_from_cjson(cJSON_GetArrayItem(json, i));
}
return schema;
}
/* complex type as object */
if (cJSON_IsObject(json)) {
const cJSON* type_node = cJSON_GetObjectItemCaseSensitive(json, "type");
__n_assert(type_node, Free(schema); return NULL);
const char* type_str = cJSON_GetStringValue(type_node);
/* type field can itself be a complex type (array or object) */
if (!type_str) {
/* nested complex type in the "type" field */
AVRO_SCHEMA* inner = avro_schema_from_cjson(type_node);
if (inner) {
memcpy(schema, inner, sizeof(AVRO_SCHEMA));
Free(inner);
}
/* copy name/namespace from outer object if present */
const cJSON* name_node = cJSON_GetObjectItemCaseSensitive(json, "name");
if (name_node && cJSON_IsString(name_node)) {
schema->name = local_strdup(cJSON_GetStringValue(name_node));
}
return schema;
}
if (strcmp(type_str, "record") == 0) {
schema->type = AVRO_RECORD;
const cJSON* name_node = cJSON_GetObjectItemCaseSensitive(json, "name");
if (name_node && cJSON_IsString(name_node)) {
schema->name = local_strdup(cJSON_GetStringValue(name_node));
}
const cJSON* ns_node = cJSON_GetObjectItemCaseSensitive(json, "namespace");
if (ns_node && cJSON_IsString(ns_node)) {
schema->namespace = local_strdup(cJSON_GetStringValue(ns_node));
}
const cJSON* fields_node = cJSON_GetObjectItemCaseSensitive(json, "fields");
if (fields_node && cJSON_IsArray(fields_node)) {
int nb = cJSON_GetArraySize(fields_node);
schema->nb_fields = (size_t)nb;
Malloc(schema->fields, AVRO_FIELD, (size_t)nb);
__n_assert(schema->fields, avro_schema_free(&schema); return NULL);
for (int i = 0; i < nb; i++) {
const cJSON* field = cJSON_GetArrayItem(fields_node, i);
const cJSON* fname = cJSON_GetObjectItemCaseSensitive(field, "name");
const cJSON* ftype = cJSON_GetObjectItemCaseSensitive(field, "type");
if (fname && cJSON_IsString(fname)) {
schema->fields[i].name = local_strdup(cJSON_GetStringValue(fname));
}
schema->fields[i].schema = avro_schema_from_cjson(ftype);
}
}
} else if (strcmp(type_str, "array") == 0) {
schema->type = AVRO_ARRAY;
const cJSON* items_node = cJSON_GetObjectItemCaseSensitive(json, "items");
schema->items = avro_schema_from_cjson(items_node);
} else if (strcmp(type_str, "map") == 0) {
schema->type = AVRO_MAP;
const cJSON* values_node = cJSON_GetObjectItemCaseSensitive(json, "values");
schema->values = avro_schema_from_cjson(values_node);
} else if (strcmp(type_str, "enum") == 0) {
schema->type = AVRO_ENUM;
const cJSON* name_node = cJSON_GetObjectItemCaseSensitive(json, "name");
if (name_node && cJSON_IsString(name_node)) {
schema->name = local_strdup(cJSON_GetStringValue(name_node));
}
const cJSON* ns_node = cJSON_GetObjectItemCaseSensitive(json, "namespace");
if (ns_node && cJSON_IsString(ns_node)) {
schema->namespace = local_strdup(cJSON_GetStringValue(ns_node));
}
const cJSON* symbols_node = cJSON_GetObjectItemCaseSensitive(json, "symbols");
if (symbols_node && cJSON_IsArray(symbols_node)) {
int nb = cJSON_GetArraySize(symbols_node);
schema->nb_symbols = (size_t)nb;
Malloc(schema->symbols, char*, (size_t)nb);
__n_assert(schema->symbols, avro_schema_free(&schema); return NULL);
for (int i = 0; i < nb; i++) {
const cJSON* sym = cJSON_GetArrayItem(symbols_node, i);
if (cJSON_IsString(sym)) {
schema->symbols[i] = local_strdup(cJSON_GetStringValue(sym));
}
}
}
} else if (strcmp(type_str, "fixed") == 0) {
schema->type = AVRO_FIXED;
const cJSON* name_node = cJSON_GetObjectItemCaseSensitive(json, "name");
if (name_node && cJSON_IsString(name_node)) {
schema->name = local_strdup(cJSON_GetStringValue(name_node));
}
const cJSON* size_node = cJSON_GetObjectItemCaseSensitive(json, "size");
if (size_node && cJSON_IsNumber(size_node)) {
schema->fixed_size = (size_t)cJSON_GetNumberValue(size_node);
}
} else {
/* primitive type name in object form */
schema->type = avro_type_from_string(type_str);
}
return schema;
}
/* fallback */
schema->type = AVRO_NULL;
return schema;
}
AVRO_SCHEMA* avro_schema_parse(const char* json_str) {
__n_assert(json_str, return NULL);
cJSON* json = cJSON_Parse(json_str);
__n_assert(json, return NULL);
AVRO_SCHEMA* schema = avro_schema_from_cjson(json);
cJSON_Delete(json);
return schema;
}
void avro_schema_free(AVRO_SCHEMA** schema_ptr) {
if (!schema_ptr || !*schema_ptr) return;
AVRO_SCHEMA* schema = *schema_ptr;
FreeNoLog(schema->name);
FreeNoLog(schema->namespace);
if (schema->fields) {
for (size_t i = 0; i < schema->nb_fields; i++) {
FreeNoLog(schema->fields[i].name);
avro_schema_free(&schema->fields[i].schema);
}
FreeNoLog(schema->fields);
}
avro_schema_free(&schema->items);
avro_schema_free(&schema->values);
if (schema->union_branches) {
for (size_t i = 0; i < schema->nb_branches; i++) {
avro_schema_free(&schema->union_branches[i]);
}
FreeNoLog(schema->union_branches);
}
if (schema->symbols) {
for (size_t i = 0; i < schema->nb_symbols; i++) {
FreeNoLog(schema->symbols[i]);
}
FreeNoLog(schema->symbols);
}
FreeNoLog(*schema_ptr);
}
char* avro_schema_to_json(const AVRO_SCHEMA* schema) {
__n_assert(schema, return NULL);
cJSON* json = NULL;
switch (schema->type) {
case AVRO_NULL:
case AVRO_BOOLEAN:
case AVRO_INT:
case AVRO_LONG:
case AVRO_FLOAT:
case AVRO_DOUBLE:
case AVRO_BYTES:
case AVRO_STRING:
/* for simple types used inside complex structures, return just the string */
json = cJSON_CreateString(avro_type_to_string(schema->type));
break;
case AVRO_RECORD: {
json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "type", "record");
if (schema->name) {
cJSON_AddStringToObject(json, "name", schema->name);
}
if (schema->namespace) {
cJSON_AddStringToObject(json, "namespace", schema->namespace);
}
cJSON* fields_arr = cJSON_AddArrayToObject(json, "fields");
for (size_t i = 0; i < schema->nb_fields; i++) {
cJSON* field = cJSON_CreateObject();
cJSON_AddStringToObject(field, "name", schema->fields[i].name);
char* ftype_str = avro_schema_to_json(schema->fields[i].schema);
if (ftype_str) {
cJSON* ftype = cJSON_Parse(ftype_str);
cJSON_AddItemToObject(field, "type", ftype);
Free(ftype_str);
}
cJSON_AddItemToArray(fields_arr, field);
}
break;
}
case AVRO_ARRAY: {
json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "type", "array");
char* items_str = avro_schema_to_json(schema->items);
if (items_str) {
cJSON* items = cJSON_Parse(items_str);
cJSON_AddItemToObject(json, "items", items);
Free(items_str);
}
break;
}
case AVRO_MAP: {
json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "type", "map");
char* values_str = avro_schema_to_json(schema->values);
if (values_str) {
cJSON* values = cJSON_Parse(values_str);
cJSON_AddItemToObject(json, "values", values);
Free(values_str);
}
break;
}
case AVRO_ENUM: {
json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "type", "enum");
if (schema->name) {
cJSON_AddStringToObject(json, "name", schema->name);
}
if (schema->namespace) {
cJSON_AddStringToObject(json, "namespace", schema->namespace);
}
cJSON* syms = cJSON_AddArrayToObject(json, "symbols");
for (size_t i = 0; i < schema->nb_symbols; i++) {
cJSON_AddItemToArray(syms, cJSON_CreateString(schema->symbols[i]));
}
break;
}
case AVRO_UNION: {
json = cJSON_CreateArray();
for (size_t i = 0; i < schema->nb_branches; i++) {
char* branch_str = avro_schema_to_json(schema->union_branches[i]);
if (branch_str) {
cJSON* branch = cJSON_Parse(branch_str);
cJSON_AddItemToArray(json, branch);
Free(branch_str);
}
}
break;
}
case AVRO_FIXED: {
json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "type", "fixed");
if (schema->name) {
cJSON_AddStringToObject(json, "name", schema->name);
}
cJSON_AddNumberToObject(json, "size", (double)schema->fixed_size);
break;
}
}
char* result = NULL;
if (json) {
result = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
}
return result;
}
/* Zig-zag varint encoding/decoding */
int avro_encode_long(N_STR** dest, int64_t value) {
__n_assert(dest, return FALSE);
/* zig-zag encode: portable sign-extension without signed shift UB */
uint64_t n = ((uint64_t)value << 1) ^ -(uint64_t)(value < 0);
unsigned char buf[10];
int len = 0;
while (n > 0x7F) {
buf[len++] = (unsigned char)((n & 0x7F) | 0x80);
n >>= 7;
}
buf[len++] = (unsigned char)n;
nstrcat_ex(dest, buf, (NSTRBYTE)len, 1);
return TRUE;
}
int avro_decode_long(AVRO_READER* reader, int64_t* value) {
__n_assert(reader, return FALSE);
__n_assert(value, return FALSE);
uint64_t n = 0;
int shift = 0;
while (reader->pos < reader->size) {
unsigned char b = reader->data[reader->pos++];
n |= ((uint64_t)(b & 0x7F)) << shift;
if ((b & 0x80) == 0) {
/* zig-zag decode */
*value = (int64_t)((n >> 1) ^ (~(n & 1) + 1));
return TRUE;
}
shift += 7;
if (shift >= 70) {
n_log(LOG_ERR, "varint too long");
return FALSE;
}
}
n_log(LOG_ERR, "unexpected end of data while reading varint");
return FALSE;
}
/* Binary encoding */
static int avro_encode_bytes_raw(N_STR** dest, const unsigned char* data, size_t len) {
int ret = avro_encode_long(dest, (int64_t)len);
if (ret != TRUE) return ret;
if (len > 0) {
nstrcat_ex(dest, (void*)data, (NSTRBYTE)len, 1);
}
return TRUE;
}
static int avro_find_union_branch(const AVRO_SCHEMA* schema, const cJSON* json) {
for (size_t i = 0; i < schema->nb_branches; i++) {
const AVRO_SCHEMA* branch = schema->union_branches[i];
switch (branch->type) {
case AVRO_NULL:
if (cJSON_IsNull(json)) return (int)i;
break;
case AVRO_BOOLEAN:
if (cJSON_IsBool(json)) return (int)i;
break;
case AVRO_INT:
case AVRO_LONG:
case AVRO_FLOAT:
case AVRO_DOUBLE:
if (cJSON_IsNumber(json)) return (int)i;
break;
case AVRO_STRING:
if (cJSON_IsString(json)) return (int)i;
break;
case AVRO_BYTES:
if (cJSON_IsString(json)) return (int)i;
break;
case AVRO_RECORD:
case AVRO_MAP:
if (cJSON_IsObject(json)) return (int)i;
break;
case AVRO_ARRAY:
if (cJSON_IsArray(json)) return (int)i;
break;
case AVRO_ENUM:
if (cJSON_IsString(json)) return (int)i;
break;
case AVRO_UNION:
case AVRO_FIXED:
break;
}
}
return -1;
}
int avro_encode_datum(N_STR** dest, const AVRO_SCHEMA* schema, const cJSON* json) {
__n_assert(dest, return FALSE);
__n_assert(schema, return FALSE);
switch (schema->type) {
case AVRO_NULL:
/* null is zero bytes */
return TRUE;
case AVRO_BOOLEAN: {
unsigned char b = cJSON_IsTrue(json) ? 1 : 0;
nstrcat_ex(dest, &b, 1, 1);
return TRUE;
}
case AVRO_INT: {
int32_t val = 0;
if (cJSON_IsNumber(json)) {
val = (int32_t)cJSON_GetNumberValue(json);
}
return avro_encode_long(dest, (int64_t)val);
}
case AVRO_LONG: {
int64_t val = 0;
if (cJSON_IsNumber(json)) {
val = (int64_t)cJSON_GetNumberValue(json);
}
return avro_encode_long(dest, val);
}
case AVRO_FLOAT: {
float val = 0.0f;
if (cJSON_IsNumber(json)) {
val = (float)cJSON_GetNumberValue(json);
}
/* Avro float is 4 bytes little-endian IEEE 754 */
unsigned char buf[4];
memcpy(buf, &val, 4);
#if BYTEORDER_ENDIAN == BYTEORDER_BIG_ENDIAN
unsigned char tmp;
tmp = buf[0];
buf[0] = buf[3];
buf[3] = tmp;
tmp = buf[1];
buf[1] = buf[2];
buf[2] = tmp;
#endif
nstrcat_ex(dest, buf, 4, 1);
return TRUE;
}
case AVRO_DOUBLE: {
double val = 0.0;
if (cJSON_IsNumber(json)) {
val = cJSON_GetNumberValue(json);
}
/* Avro double is 8 bytes little-endian IEEE 754 */
unsigned char buf[8];
memcpy(buf, &val, 8);
#if BYTEORDER_ENDIAN == BYTEORDER_BIG_ENDIAN
unsigned char tmp;
tmp = buf[0];
buf[0] = buf[7];
buf[7] = tmp;
tmp = buf[1];
buf[1] = buf[6];
buf[6] = tmp;
tmp = buf[2];
buf[2] = buf[5];
buf[5] = tmp;
tmp = buf[3];
buf[3] = buf[4];
buf[4] = tmp;
#endif
nstrcat_ex(dest, buf, 8, 1);
return TRUE;
}
case AVRO_BYTES: {
const char* str = cJSON_GetStringValue((cJSON*)json);
if (str) {
return avro_encode_bytes_raw(dest, (const unsigned char*)str, strlen(str));
}
return avro_encode_bytes_raw(dest, NULL, 0);
}
case AVRO_STRING: {
const char* str = cJSON_GetStringValue((cJSON*)json);
if (str) {
return avro_encode_bytes_raw(dest, (const unsigned char*)str, strlen(str));
}
return avro_encode_bytes_raw(dest, (const unsigned char*)"", 0);
}
case AVRO_RECORD: {
if (!cJSON_IsObject(json)) {
n_log(LOG_ERR, "expected JSON object for record type");
return FALSE;
}
for (size_t i = 0; i < schema->nb_fields; i++) {
const cJSON* field_val = cJSON_GetObjectItemCaseSensitive(json, schema->fields[i].name);
if (!field_val) {
/* try with a null placeholder */
cJSON null_val;
memset(&null_val, 0, sizeof(null_val));
null_val.type = cJSON_NULL;
if (avro_encode_datum(dest, schema->fields[i].schema, &null_val) != TRUE) {
return FALSE;
}
} else {
if (avro_encode_datum(dest, schema->fields[i].schema, field_val) != TRUE) {
return FALSE;
}
}
}
return TRUE;
}
case AVRO_ENUM: {
const char* sym = cJSON_GetStringValue((cJSON*)json);
if (!sym) {
n_log(LOG_ERR, "expected string for enum type");
return FALSE;
}
for (size_t i = 0; i < schema->nb_symbols; i++) {
if (strcmp(sym, schema->symbols[i]) == 0) {
return avro_encode_long(dest, (int64_t)i);
}
}
n_log(LOG_ERR, "unknown enum symbol: %s", sym);
return FALSE;
}
case AVRO_ARRAY: {
if (!cJSON_IsArray(json)) {
n_log(LOG_ERR, "expected JSON array for array type");
return FALSE;
}
int count = cJSON_GetArraySize(json);
if (count > 0) {
/* write block with count */
if (avro_encode_long(dest, (int64_t)count) != TRUE) return FALSE;
for (int i = 0; i < count; i++) {
if (avro_encode_datum(dest, schema->items, cJSON_GetArrayItem(json, i)) != TRUE) {
return FALSE;
}
}
}
/* terminating zero-count block */
return avro_encode_long(dest, 0);
}
case AVRO_MAP: {
if (!cJSON_IsObject(json)) {
n_log(LOG_ERR, "expected JSON object for map type");
return FALSE;
}
int count = cJSON_GetArraySize(json);
if (count > 0) {
if (avro_encode_long(dest, (int64_t)count) != TRUE) return FALSE;
const cJSON* item = NULL;
cJSON_ArrayForEach(item, json) {
/* encode key as string */
const char* key = item->string;
if (avro_encode_bytes_raw(dest, (const unsigned char*)key, strlen(key)) != TRUE) {
return FALSE;
}
/* encode value */
if (avro_encode_datum(dest, schema->values, item) != TRUE) {
return FALSE;
}
}
}
return avro_encode_long(dest, 0);
}
case AVRO_UNION: {
int branch_idx = avro_find_union_branch(schema, json);
if (branch_idx < 0) {
n_log(LOG_ERR, "no matching union branch for JSON value");
return FALSE;
}
if (avro_encode_long(dest, (int64_t)branch_idx) != TRUE) return FALSE;
return avro_encode_datum(dest, schema->union_branches[branch_idx], json);
}
case AVRO_FIXED: {
const char* str = cJSON_GetStringValue((cJSON*)json);
if (!str) {
n_log(LOG_ERR, "expected string for fixed type");
return FALSE;
}
size_t len = strlen(str);
if (len > schema->fixed_size) {
len = schema->fixed_size;
}
/* write exactly fixed_size bytes, pad with zeros if needed */
nstrcat_ex(dest, (void*)str, (NSTRBYTE)len, 1);
if (len < schema->fixed_size) {
size_t pad = schema->fixed_size - len;
unsigned char* zeros = NULL;
Malloc(zeros, unsigned char, pad);
if (zeros) {
nstrcat_ex(dest, zeros, (NSTRBYTE)pad, 1);
Free(zeros);
}
}
return TRUE;
}
}
return FALSE;
}
/* Binary decoding */
static int avro_decode_bytes_raw(AVRO_READER* reader, unsigned char** out, size_t* out_len) {
int64_t len = 0;
if (avro_decode_long(reader, &len) != TRUE) return FALSE;
if (len < 0) {
n_log(LOG_ERR, "negative byte length: %" PRId64, len);
return FALSE;
}
*out_len = (size_t)len;
if ((size_t)len > reader->size - reader->pos) {
n_log(LOG_ERR, "not enough data for bytes: need %zu, have %zu", (size_t)len, reader->size - reader->pos);
return FALSE;
}
Malloc(*out, unsigned char, (size_t)len + 1);
__n_assert(*out, return FALSE);
if (len > 0) {
memcpy(*out, reader->data + reader->pos, (size_t)len);
}
(*out)[(size_t)len] = '\0';
reader->pos += (size_t)len;
return TRUE;
}
cJSON* avro_decode_datum(AVRO_READER* reader, const AVRO_SCHEMA* schema) {
__n_assert(reader, return NULL);
__n_assert(schema, return NULL);
switch (schema->type) {
case AVRO_NULL:
return cJSON_CreateNull();
case AVRO_BOOLEAN: {
if (reader->pos >= reader->size) {
n_log(LOG_ERR, "unexpected end of data reading boolean");
return NULL;
}
unsigned char b = reader->data[reader->pos++];
return cJSON_CreateBool(b != 0);
}
case AVRO_INT: {
int64_t val = 0;
if (avro_decode_long(reader, &val) != TRUE) return NULL;
return cJSON_CreateNumber((double)(int32_t)val);
}
case AVRO_LONG: {
int64_t val = 0;
if (avro_decode_long(reader, &val) != TRUE) return NULL;
return cJSON_CreateNumber((double)val);
}
case AVRO_FLOAT: {
if (reader->pos + 4 > reader->size) {
n_log(LOG_ERR, "unexpected end of data reading float");
return NULL;
}
unsigned char buf[4];
memcpy(buf, reader->data + reader->pos, 4);
reader->pos += 4;
#if BYTEORDER_ENDIAN == BYTEORDER_BIG_ENDIAN
unsigned char tmp;
tmp = buf[0];
buf[0] = buf[3];
buf[3] = tmp;
tmp = buf[1];
buf[1] = buf[2];
buf[2] = tmp;
#endif
float val;
memcpy(&val, buf, 4);
return cJSON_CreateNumber((double)val);
}
case AVRO_DOUBLE: {
if (reader->pos + 8 > reader->size) {
n_log(LOG_ERR, "unexpected end of data reading double");
return NULL;
}
unsigned char buf[8];
memcpy(buf, reader->data + reader->pos, 8);
reader->pos += 8;
#if BYTEORDER_ENDIAN == BYTEORDER_BIG_ENDIAN
unsigned char tmp;
tmp = buf[0];
buf[0] = buf[7];
buf[7] = tmp;
tmp = buf[1];
buf[1] = buf[6];
buf[6] = tmp;
tmp = buf[2];
buf[2] = buf[5];
buf[5] = tmp;
tmp = buf[3];
buf[3] = buf[4];
buf[4] = tmp;
#endif
double val;
memcpy(&val, buf, 8);
return cJSON_CreateNumber(val);
}
case AVRO_BYTES: {
unsigned char* data = NULL;
size_t len = 0;
if (avro_decode_bytes_raw(reader, &data, &len) != TRUE) return NULL;
cJSON* result = cJSON_CreateString((const char*)data);
Free(data);
return result;
}
case AVRO_STRING: {
unsigned char* data = NULL;
size_t len = 0;
if (avro_decode_bytes_raw(reader, &data, &len) != TRUE) return NULL;
cJSON* result = cJSON_CreateString((const char*)data);
Free(data);
return result;
}
case AVRO_RECORD: {
cJSON* obj = cJSON_CreateObject();
for (size_t i = 0; i < schema->nb_fields; i++) {
cJSON* field_val = avro_decode_datum(reader, schema->fields[i].schema);
if (!field_val) {
cJSON_Delete(obj);
return NULL;
}
cJSON_AddItemToObject(obj, schema->fields[i].name, field_val);
}
return obj;
}
case AVRO_ENUM: {
int64_t idx = 0;
if (avro_decode_long(reader, &idx) != TRUE) return NULL;
if (idx < 0 || (size_t)idx >= schema->nb_symbols) {
n_log(LOG_ERR, "enum index %" PRId64 " out of range (0-%zu)", idx, schema->nb_symbols - 1);
return NULL;
}
return cJSON_CreateString(schema->symbols[(size_t)idx]);
}
case AVRO_ARRAY: {
cJSON* arr = cJSON_CreateArray();
int64_t block_count = 0;
for (;;) {
if (avro_decode_long(reader, &block_count) != TRUE) {
cJSON_Delete(arr);
return NULL;
}
if (block_count == 0) break;
if (block_count < 0) {
/* negative count means block size follows */
block_count = -block_count;
int64_t block_size = 0;
if (avro_decode_long(reader, &block_size) != TRUE) {
cJSON_Delete(arr);
return NULL;
}
(void)block_size;
}
for (int64_t i = 0; i < block_count; i++) {
cJSON* item = avro_decode_datum(reader, schema->items);
if (!item) {
cJSON_Delete(arr);
return NULL;
}
cJSON_AddItemToArray(arr, item);
}
}
return arr;
}
case AVRO_MAP: {
cJSON* obj = cJSON_CreateObject();
int64_t block_count = 0;
for (;;) {
if (avro_decode_long(reader, &block_count) != TRUE) {
cJSON_Delete(obj);
return NULL;
}
if (block_count == 0) break;
if (block_count < 0) {
block_count = -block_count;
int64_t block_size = 0;
if (avro_decode_long(reader, &block_size) != TRUE) {
cJSON_Delete(obj);
return NULL;
}
(void)block_size;
}
for (int64_t i = 0; i < block_count; i++) {
/* decode key */
unsigned char* key = NULL;
size_t key_len = 0;
if (avro_decode_bytes_raw(reader, &key, &key_len) != TRUE) {
cJSON_Delete(obj);
return NULL;
}
/* decode value */
cJSON* val = avro_decode_datum(reader, schema->values);
if (!val) {
Free(key);
cJSON_Delete(obj);
return NULL;
}
cJSON_AddItemToObject(obj, (const char*)key, val);
Free(key);
}
}
return obj;
}
case AVRO_UNION: {
int64_t branch_idx = 0;
if (avro_decode_long(reader, &branch_idx) != TRUE) return NULL;
if (branch_idx < 0 || (size_t)branch_idx >= schema->nb_branches) {
n_log(LOG_ERR, "union branch index %" PRId64 " out of range (0-%zu)", branch_idx, schema->nb_branches - 1);
return NULL;
}
return avro_decode_datum(reader, schema->union_branches[(size_t)branch_idx]);
}
case AVRO_FIXED: {
if (reader->pos + schema->fixed_size > reader->size) {
n_log(LOG_ERR, "unexpected end of data reading fixed(%zu)", schema->fixed_size);
return NULL;
}
char* buf = NULL;
Malloc(buf, char, schema->fixed_size + 1);
__n_assert(buf, return NULL);
memcpy(buf, reader->data + reader->pos, schema->fixed_size);
buf[schema->fixed_size] = '\0';
reader->pos += schema->fixed_size;
cJSON* result = cJSON_CreateString(buf);
Free(buf);
return result;
}
}
return NULL;
}
/* Object Container File format */
N_STR* avro_encode_container(const AVRO_SCHEMA* schema, const cJSON* records) {
__n_assert(schema, return NULL);
__n_assert(records, return NULL);
if (!cJSON_IsArray(records)) {
n_log(LOG_ERR, "records must be a JSON array");
return NULL;
}
N_STR* output = NULL;
nstrprintf(output, "%s", "");
__n_assert(output, return NULL);
/* 1. Magic bytes */
nstrcat_ex(&output, (void*)AVRO_MAGIC, AVRO_MAGIC_LEN, 1);
/* 2. File metadata as Avro map */
char* schema_json = avro_schema_to_json(schema);
__n_assert(schema_json, free_nstr(&output); return NULL);
/* metadata map: 1 block with 2 entries, then 0 block */
/* block count = 2 (avro.schema + avro.codec) */
avro_encode_long(&output, 2);
/* entry 1: avro.schema */
const char* key1 = "avro.schema";
avro_encode_long(&output, (int64_t)strlen(key1));
nstrcat_ex(&output, (void*)key1, (NSTRBYTE)strlen(key1), 1);
avro_encode_long(&output, (int64_t)strlen(schema_json));
nstrcat_ex(&output, (void*)schema_json, (NSTRBYTE)strlen(schema_json), 1);
/* entry 2: avro.codec = "null" (no compression) */
const char* key2 = "avro.codec";
avro_encode_long(&output, (int64_t)strlen(key2));
nstrcat_ex(&output, (void*)key2, (NSTRBYTE)strlen(key2), 1);
const char* codec = "null";
avro_encode_long(&output, (int64_t)strlen(codec));
nstrcat_ex(&output, (void*)codec, (NSTRBYTE)strlen(codec), 1);
/* end of metadata map */
avro_encode_long(&output, 0);
Free(schema_json);
/* 3. Generate sync marker (16 bytes) */
unsigned char sync[AVRO_SYNC_LEN];
srand((unsigned)time(NULL));
for (int i = 0; i < AVRO_SYNC_LEN; i++) {
sync[i] = (unsigned char)(rand() % 256);
}
nstrcat_ex(&output, sync, AVRO_SYNC_LEN, 1);
/* 4. Data block: encode all records into a single block */
int count = cJSON_GetArraySize(records);
N_STR* block_data = NULL;
nstrprintf(block_data, "%s", "");
__n_assert(block_data, free_nstr(&output); return NULL);
for (int i = 0; i < count; i++) {
const cJSON* record = cJSON_GetArrayItem(records, i);
if (avro_encode_datum(&block_data, schema, record) != TRUE) {
n_log(LOG_ERR, "failed to encode record %d", i);
free_nstr(&block_data);
free_nstr(&output);
return NULL;
}
}
/* write block header: object count, then byte size of serialized data */
avro_encode_long(&output, (int64_t)count);
avro_encode_long(&output, (int64_t)block_data->written);
/* write serialized data */
nstrcat_ex(&output, block_data->data, (NSTRBYTE)block_data->written, 1);
free_nstr(&block_data);
/* write sync marker */
nstrcat_ex(&output, sync, AVRO_SYNC_LEN, 1);
return output;
}
cJSON* avro_decode_container(const AVRO_SCHEMA* schema, const N_STR* avro_data) {
__n_assert(schema, return NULL);
__n_assert(avro_data, return NULL);
__n_assert(avro_data->data, return NULL);
AVRO_READER reader;
reader.data = (const unsigned char*)avro_data->data;
reader.size = avro_data->written;
reader.pos = 0;
/* 1. Check magic */
if (reader.size < AVRO_MAGIC_LEN + AVRO_SYNC_LEN) {
n_log(LOG_ERR, "data too small for Avro container");
return NULL;
}
if (memcmp(reader.data, AVRO_MAGIC, AVRO_MAGIC_LEN) != 0) {
n_log(LOG_ERR, "invalid Avro magic bytes");
return NULL;
}
reader.pos = AVRO_MAGIC_LEN;
/* 2. Read metadata map (skip it, we use the provided schema) */
int64_t block_count = 0;
for (;;) {
if (avro_decode_long(&reader, &block_count) != TRUE) return NULL;
if (block_count == 0) break;
if (block_count < 0) {
/* negative count: skip block_size bytes */
int64_t block_size = 0;
if (avro_decode_long(&reader, &block_size) != TRUE) return NULL;
if (block_size < 0 || (size_t)block_size > reader.size - reader.pos) {
n_log(LOG_ERR, "invalid metadata block size");
return NULL;
}
reader.pos += (size_t)block_size;
continue;
}
/* positive count: read key-value pairs */
for (int64_t i = 0; i < block_count; i++) {
unsigned char* key = NULL;
size_t key_len = 0;
if (avro_decode_bytes_raw(&reader, &key, &key_len) != TRUE) return NULL;
Free(key);
unsigned char* val = NULL;
size_t val_len = 0;
if (avro_decode_bytes_raw(&reader, &val, &val_len) != TRUE) return NULL;
Free(val);
}
}
/* 3. Read sync marker */
if (reader.pos + AVRO_SYNC_LEN > reader.size) {
n_log(LOG_ERR, "unexpected end of data reading sync marker");
return NULL;
}
unsigned char sync[AVRO_SYNC_LEN];
memcpy(sync, reader.data + reader.pos, AVRO_SYNC_LEN);
reader.pos += AVRO_SYNC_LEN;
/* 4. Read data blocks */
cJSON* all_records = cJSON_CreateArray();
while (reader.pos < reader.size) {
int64_t obj_count = 0;
if (avro_decode_long(&reader, &obj_count) != TRUE) {
cJSON_Delete(all_records);
return NULL;
}
if (obj_count <= 0) break;
int64_t block_byte_size = 0;
if (avro_decode_long(&reader, &block_byte_size) != TRUE) {
cJSON_Delete(all_records);
return NULL;
}
if (block_byte_size < 0 || (size_t)block_byte_size > reader.size - reader.pos) {
n_log(LOG_ERR, "invalid data block size: %" PRId64, block_byte_size);
cJSON_Delete(all_records);
return NULL;
}
size_t block_end = reader.pos + (size_t)block_byte_size;
for (int64_t i = 0; i < obj_count; i++) {
cJSON* record = avro_decode_datum(&reader, schema);
if (!record) {
n_log(LOG_ERR, "failed to decode record %" PRId64, i);
cJSON_Delete(all_records);
return NULL;
}
cJSON_AddItemToArray(all_records, record);
}
/* verify we consumed exactly the block */
if (reader.pos != block_end) {
n_log(LOG_WARNING, "block size mismatch: expected pos %zu, got %zu", block_end, reader.pos);
reader.pos = block_end;
}
/* read and verify sync marker */
if (reader.pos + AVRO_SYNC_LEN > reader.size) {
n_log(LOG_ERR, "unexpected end of data reading block sync marker");
cJSON_Delete(all_records);
return NULL;
}
if (memcmp(reader.data + reader.pos, sync, AVRO_SYNC_LEN) != 0) {
n_log(LOG_ERR, "sync marker mismatch");
cJSON_Delete(all_records);
return NULL;
}
reader.pos += AVRO_SYNC_LEN;
}
return all_records;
}
/* File-level convenience functions */
int avro_json_to_file(const char* avro_filename, const char* schema_filename, const char* json_filename) {
__n_assert(avro_filename, return FALSE);
__n_assert(schema_filename, return FALSE);
__n_assert(json_filename, return FALSE);
/* load schema file */
N_STR* schema_str = file_to_nstr((char*)schema_filename);
__n_assert(schema_str, return FALSE);
AVRO_SCHEMA* schema = avro_schema_parse(schema_str->data);
free_nstr(&schema_str);
__n_assert(schema, return FALSE);
/* load JSON file */
N_STR* json_str = file_to_nstr((char*)json_filename);
if (!json_str) {
avro_schema_free(&schema);
return FALSE;
}
cJSON* json = cJSON_Parse(json_str->data);
free_nstr(&json_str);
if (!json) {
n_log(LOG_ERR, "failed to parse JSON file %s: %s", json_filename, _str(cJSON_GetErrorPtr()));
avro_schema_free(&schema);
return FALSE;
}
/* if json is a single object, wrap it in an array */
cJSON* records = json;
int wrapped = 0;
if (cJSON_IsObject(json)) {
records = cJSON_CreateArray();
cJSON_AddItemToArray(records, cJSON_Duplicate(json, 1));
wrapped = 1;
}
/* encode to Avro container */
N_STR* avro_data = avro_encode_container(schema, records);
if (wrapped) {
cJSON_Delete(records);
}
cJSON_Delete(json);
avro_schema_free(&schema);
if (!avro_data) {
n_log(LOG_ERR, "failed to encode Avro container");
return FALSE;
}
/* write Avro file */
int ret = nstr_to_file(avro_data, (char*)avro_filename);
free_nstr(&avro_data);
if (ret != TRUE) {
n_log(LOG_ERR, "failed to write Avro file %s", avro_filename);
return FALSE;
}
n_log(LOG_INFO, "wrote Avro file %s", avro_filename);
return TRUE;
}
int avro_file_to_json(const char* avro_filename, const char* schema_filename, const char* json_filename) {
__n_assert(avro_filename, return FALSE);
__n_assert(schema_filename, return FALSE);
__n_assert(json_filename, return FALSE);
/* load schema file */
N_STR* schema_str = file_to_nstr((char*)schema_filename);
__n_assert(schema_str, return FALSE);
AVRO_SCHEMA* schema = avro_schema_parse(schema_str->data);
free_nstr(&schema_str);
__n_assert(schema, return FALSE);
/* load Avro file */
N_STR* avro_data = file_to_nstr((char*)avro_filename);
if (!avro_data) {
avro_schema_free(&schema);
return FALSE;
}
/* decode from Avro container */
cJSON* records = avro_decode_container(schema, avro_data);
free_nstr(&avro_data);
avro_schema_free(&schema);
if (!records) {
n_log(LOG_ERR, "failed to decode Avro file %s", avro_filename);
return FALSE;
}
/* write JSON file */
char* json_output = cJSON_Print(records);
cJSON_Delete(records);
if (!json_output) {
n_log(LOG_ERR, "failed to serialize JSON");
return FALSE;
}
N_STR* json_nstr = char_to_nstr(json_output);
free(json_output);
if (!json_nstr) {
return FALSE;
}
int ret = nstr_to_file(json_nstr, (char*)json_filename);
free_nstr(&json_nstr);
if (ret != TRUE) {
n_log(LOG_ERR, "failed to write JSON file %s", json_filename);
return FALSE;
}
n_log(LOG_INFO, "wrote JSON file %s", json_filename);
return TRUE;
}
/* N_STR in-memory convenience functions */
AVRO_SCHEMA* avro_schema_parse_nstr(const N_STR* schema_nstr) {
__n_assert(schema_nstr, return NULL);
__n_assert(schema_nstr->data, return NULL);
return avro_schema_parse(schema_nstr->data);
}
N_STR* avro_nstr_json_to_avro(const N_STR* schema_nstr, const N_STR* json_nstr) {
__n_assert(schema_nstr, return NULL);
__n_assert(schema_nstr->data, return NULL);
__n_assert(json_nstr, return NULL);
__n_assert(json_nstr->data, return NULL);
AVRO_SCHEMA* schema = avro_schema_parse(schema_nstr->data);
__n_assert(schema, return NULL);
cJSON* json = cJSON_Parse(json_nstr->data);
if (!json) {
n_log(LOG_ERR, "failed to parse JSON: %s", _str(cJSON_GetErrorPtr()));
avro_schema_free(&schema);
return NULL;
}
/* if json is a single object, wrap it in an array */
cJSON* records = json;
int wrapped = 0;
if (cJSON_IsObject(json)) {
records = cJSON_CreateArray();
cJSON_AddItemToArray(records, cJSON_Duplicate(json, 1));
wrapped = 1;
}
N_STR* avro_data = avro_encode_container(schema, records);
if (wrapped) {
cJSON_Delete(records);
}
cJSON_Delete(json);
avro_schema_free(&schema);
return avro_data;
}
N_STR* avro_nstr_avro_to_json(const N_STR* schema_nstr, const N_STR* avro_nstr) {
__n_assert(schema_nstr, return NULL);
__n_assert(schema_nstr->data, return NULL);
__n_assert(avro_nstr, return NULL);
__n_assert(avro_nstr->data, return NULL);
AVRO_SCHEMA* schema = avro_schema_parse(schema_nstr->data);
__n_assert(schema, return NULL);
cJSON* records = avro_decode_container(schema, avro_nstr);
avro_schema_free(&schema);
if (!records) {
n_log(LOG_ERR, "failed to decode Avro container");
return NULL;
}
char* json_output = cJSON_Print(records);
cJSON_Delete(records);
if (!json_output) {
n_log(LOG_ERR, "failed to serialize JSON");
return NULL;
}
N_STR* result = char_to_nstr(json_output);
free(json_output);
return result;
}
Modules
- AABB3D
- ACCEPT_POOL
- ALLEGRO4
- ALLEGRO5
- ANIM_LIB
- ASTAR
- ATLAS_LIB
- AVRO
- CLOCK_SYNC
- COMMONS
- CONFIGFILE
- CYPHER_BASE64
- CYPHER_VIGENERE
- DEAD_RECKONING
- ENUMS
- EXCEPTIONS
- GAME_ENV
- GIT
- GUI
- HASH_TABLE
- ISOMETRIC_ENGINE
- LIST
- LOG
- LOGNODUP
- LZ4
- NETWORKING
- NETWORK_MSG
- N_COOKIES
- N_DIFF
- N_DIGEST
- N_DISK_CACHE
- N_DNS
- N_ECC
- N_ENTROPY
- N_FILES
- N_FLUIDS
- N_GUI_COLOR
- N_GUI_COLORWHEEL
- N_GUI_CURVE
- N_GUI_GRID
- N_GUI_POPUP
- N_HEX
- N_HTTP3
- N_HTTP_COOKIE
- N_HTTP_FORM
- N_HTTP_MIME
- N_HTTP_REQ
- N_HTTP_RESP
- N_HTTP_ROUTER
- N_HTTP_SERVER
- N_ICC
- N_JSON
- N_KAFKA
- N_LINALG
- N_MODULE
- N_OPENSSL_COMPAT
- N_PRETTY
- N_PTH
- N_QUERY
- N_RANDOM
- N_SHA256
- N_STR
- N_TIFF
- N_TIME
- N_USER
- N_X509
- N_ZIP
- PARTICLES
- PCRE
- PHYSICS
- SIGNALS
- STACK
- THREADS
- TRAJECTORY
- TREE
- ZLIB