1
+ /*
2
+ Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
23
+ /* cJSON */
24
+ /* JSON parser in C. */
25
+
26
+ /* disable warnings about old C89 functions in MSVC */
27
+ #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
28
+ #define _CRT_SECURE_NO_DEPRECATE
29
+ #endif
30
+
31
+ #ifdef __GNUC__
32
+ #pragma GCC visibility push(default)
33
+ #endif
34
+ #if defined(_MSC_VER)
35
+ #pragma warning (push)
36
+ /* disable warning about single line comments in system headers */
37
+ #pragma warning (disable : 4001)
38
+ #endif
39
+
40
+ #include < string.h>
41
+ #include < stdio.h>
42
+ #include < stdlib.h>
43
+ #include < limits.h>
44
+ #include < ctype.h>
45
+
46
+ #ifdef ENABLE_LOCALES
47
+ #include < locale.h>
48
+ #endif
49
+
50
+ #if defined(_MSC_VER)
51
+ #pragma warning (pop)
52
+ #endif
53
+ #ifdef __GNUC__
54
+ #pragma GCC visibility pop
55
+ #endif
56
+
57
+ #include < aws/core/external/cjson/cJSON.h>
58
+
59
+ typedef struct internal_hooks {
60
+ void *(CJSON_CDECL *allocate)(size_t size);
61
+ void (CJSON_CDECL *deallocate)(void *pointer);
62
+ void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
63
+ } internal_hooks;
64
+
65
+ #if defined(_MSC_VER)
66
+ /* work around MSVC error C2322: '...' address of dllimport '...' is not static */
67
+ static void * CJSON_AS4CPP_CDECL internal_malloc (size_t size)
68
+ {
69
+ return malloc (size);
70
+ }
71
+ static void CJSON_AS4CPP_CDECL internal_free (void *pointer)
72
+ {
73
+ free (pointer);
74
+ }
75
+ static void * CJSON_AS4CPP_CDECL internal_realloc (void *pointer, size_t size)
76
+ {
77
+ return realloc (pointer, size);
78
+ }
79
+ #else
80
+ #define internal_malloc malloc
81
+ #define internal_free free
82
+ #define internal_realloc realloc
83
+ #endif
84
+
85
+ static internal_hooks global_hooks = {internal_malloc, internal_free, internal_realloc};
86
+
87
+ static unsigned char *cJSON_AS4CPP_strdup (const unsigned char *string, const internal_hooks *const hooks) {
88
+ size_t length = 0 ;
89
+ unsigned char *copy = NULL ;
90
+
91
+ if (string == NULL ) {
92
+ return NULL ;
93
+ }
94
+
95
+ length = strlen ((const char *) string) + sizeof (" " );
96
+ copy = (unsigned char *) hooks->allocate (length);
97
+ if (copy == NULL ) {
98
+ return NULL ;
99
+ }
100
+ memcpy (copy, string, length);
101
+
102
+ return copy;
103
+ }
104
+
105
+ /* Internal constructor. */
106
+ static cJSON *cJSON_AS4CPP_New_Item (const internal_hooks *const hooks) {
107
+ cJSON *node = (cJSON *) hooks->allocate (sizeof (cJSON));
108
+ if (node) {
109
+ memset (node, ' \0 ' , sizeof (cJSON));
110
+ }
111
+
112
+ return node;
113
+ }
114
+
115
+ CJSON_PUBLIC (cJSON *)cJSON_AS4CPP_CreateInt64(long long num) {
116
+ cJSON *item = cJSON_AS4CPP_New_Item (&global_hooks);
117
+ if (item) {
118
+ item->type = cJSON_Number;
119
+ item->valuedouble = static_cast <double >(num);
120
+
121
+ // For integer which is out of the range of [INT_MIN, INT_MAX], it may lose precision if we cast it to double.
122
+ // Instead, we keep the integer literal as a string.
123
+ if (num > INT_MAX || num < INT_MIN) {
124
+ char buf[21 ];
125
+ sprintf (buf, " %lld" , num);
126
+ item->valuestring = (char *) cJSON_AS4CPP_strdup ((const unsigned char *) buf, &global_hooks);
127
+ }
128
+
129
+ /* use saturation in case of overflow */
130
+ if (num >= INT_MAX) {
131
+ item->valueint = INT_MAX;
132
+ } else if (num <= INT_MIN) {
133
+ item->valueint = INT_MIN;
134
+ } else {
135
+ item->valueint = (int ) num;
136
+ }
137
+ }
138
+
139
+ return item;
140
+ }
0 commit comments