Coverage Report

Created: 2026-08-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jansson/src/jansson_private.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3
 *
4
 * Jansson is free software; you can redistribute it and/or modify
5
 * it under the terms of the MIT license. See LICENSE for details.
6
 */
7
8
#ifndef JANSSON_PRIVATE_H
9
#define JANSSON_PRIVATE_H
10
11
#include "hashtable.h"
12
#include "jansson.h"
13
#ifdef HAVE_CONFIG_H
14
#include "jansson_private_config.h"
15
#endif
16
#include "strbuffer.h"
17
#include <stddef.h>
18
19
#define container_of(ptr_, type_, member_)                                               \
20
917k
    ((type_ *)((char *)ptr_ - offsetof(type_, member_)))
21
22
/* On some platforms, max() may already be defined */
23
#ifndef max
24
10.9k
#define max(a, b) ((a) > (b) ? (a) : (b))
25
#endif
26
27
/* va_copy is a C99 feature. In C89 implementations, it's sometimes
28
   available as __va_copy. If not, memcpy() should do the trick. */
29
#ifndef va_copy
30
#ifdef __va_copy
31
#define va_copy __va_copy
32
#else
33
#define va_copy(a, b) memcpy(&(a), &(b), sizeof(va_list))
34
#endif
35
#endif
36
37
typedef struct {
38
    json_t json;
39
    hashtable_t hashtable;
40
} json_object_t;
41
42
typedef struct {
43
    json_t json;
44
    size_t size;
45
    size_t entries;
46
    json_t **table;
47
} json_array_t;
48
49
typedef struct {
50
    json_t json;
51
    char *value;
52
    size_t length;
53
} json_string_t;
54
55
typedef struct {
56
    json_t json;
57
    double value;
58
} json_real_t;
59
60
typedef struct {
61
    json_t json;
62
    json_int_t value;
63
} json_integer_t;
64
65
177k
#define json_to_object(json_)  container_of(json_, json_object_t, json)
66
164k
#define json_to_array(json_)   container_of(json_, json_array_t, json)
67
32.0k
#define json_to_string(json_)  container_of(json_, json_string_t, json)
68
80.5k
#define json_to_real(json_)    container_of(json_, json_real_t, json)
69
64.1k
#define json_to_integer(json_) container_of(json_, json_integer_t, json)
70
71
/* Create a string by taking ownership of an existing buffer */
72
json_t *jsonp_stringn_nocheck_own(const char *value, size_t len);
73
74
/* Error message formatting */
75
void jsonp_error_init(json_error_t *error, const char *source);
76
void jsonp_error_set_source(json_error_t *error, const char *source);
77
void jsonp_error_set(json_error_t *error, int line, int column, size_t position,
78
                     enum json_error_code code, const char *msg, ...);
79
void jsonp_error_vset(json_error_t *error, int line, int column, size_t position,
80
                      enum json_error_code code, const char *msg, va_list ap);
81
82
/* Locale independent string<->double conversions */
83
int jsonp_strtod(strbuffer_t *strbuffer, double *out);
84
int jsonp_dtostr(char *buffer, size_t size, double value, int prec);
85
86
/* Wrappers for custom memory functions */
87
void *jsonp_malloc(size_t size) JANSSON_ATTRS((warn_unused_result));
88
void *jsonp_realloc(void *ptr, size_t originalSize, size_t newSize)
89
    JANSSON_ATTRS((warn_unused_result));
90
void jsonp_free(void *ptr);
91
char *jsonp_strndup(const char *str, size_t len) JANSSON_ATTRS((warn_unused_result));
92
93
/* Circular reference check*/
94
/* Space for "0x", double the sizeof a pointer for the hex and a terminator. */
95
#define LOOP_KEY_LEN (2 + (sizeof(json_t *) * 2) + 1)
96
int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size,
97
                     size_t *key_len_out);
98
99
/* Windows compatibility */
100
#if defined(_WIN32) || defined(WIN32)
101
#if defined(_MSC_VER) /* MS compiller */
102
#if (_MSC_VER < 1900) &&                                                                 \
103
    !defined(snprintf) /* snprintf not defined yet & not introduced */
104
#define snprintf _snprintf
105
#endif
106
#if (_MSC_VER < 1500) &&                                                                 \
107
    !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */
108
#define vsnprintf(b, c, f, a) _vsnprintf(b, c, f, a)
109
#endif
110
#else /* Other Windows compiller, old definition */
111
#define snprintf  _snprintf
112
#define vsnprintf _vsnprintf
113
#endif
114
#endif
115
116
#endif