-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfastchart_graph.c
More file actions
211 lines (190 loc) · 6.93 KB
/
Copy pathfastchart_graph.c
File metadata and controls
211 lines (190 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
+----------------------------------------------------------------------+
| Copyright (c) 2025-2026, Ilia Alshanetsky |
| Copyright (c) 2025-2026, Advanced Internet Designs Inc. |
+----------------------------------------------------------------------+
| This source file is subject to the BSD 3-Clause license that is |
| bundled with this package in the file LICENSE. |
+----------------------------------------------------------------------+
| Author: Ilia Alshanetsky <ilia@ilia.ws> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <string.h>
#include "php.h"
#include "php_fastchart.h"
#include "fastchart_graph.h"
#include "fastchart_axis.h"
int fastchart_graph_validate_node_labels(zval *arr, const char *method)
{
size_t total = 0;
zval *entry;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arr), entry) {
if (entry) ZVAL_DEREF(entry);
if (Z_TYPE_P(entry) != IS_ARRAY) continue;
zval *label = zend_hash_str_find(Z_ARRVAL_P(entry),
"label", sizeof("label") - 1);
if (label) ZVAL_DEREF(label);
if (!label || Z_TYPE_P(label) != IS_STRING
|| Z_STRLEN_P(label) > FASTCHART_MAX_TEXT_BYTES
|| memchr(Z_STRVAL_P(label), 0, Z_STRLEN_P(label)) != NULL) {
continue;
}
if (Z_STRLEN_P(label) > FASTCHART_MAX_GRAPH_LABEL_BYTES - total) {
zend_value_error("%s labels exceed the %u-byte aggregate limit",
method, (unsigned)FASTCHART_MAX_GRAPH_LABEL_BYTES);
return -1;
}
total += Z_STRLEN_P(label);
} ZEND_HASH_FOREACH_END();
return 0;
}
int fastchart_graph_parse_nodes(zval *arr, int max,
fastchart_graph_node **out, int *count)
{
*out = NULL;
*count = 0;
HashTable *ht = Z_ARRVAL_P(arr);
int n = zend_hash_num_elements(ht);
if (n > max) n = max;
if (n <= 0) return 0;
fastchart_graph_node *parsed = ecalloc(n, sizeof(*parsed));
int kept = 0;
zval *entry;
ZEND_HASH_FOREACH_VAL(ht, entry) {
if (kept >= n) break;
if (entry) ZVAL_DEREF(entry);
if (Z_TYPE_P(entry) != IS_ARRAY) {
parsed[kept].label = NULL;
parsed[kept].color_rgb = -1;
kept++;
continue;
}
HashTable *eht = Z_ARRVAL_P(entry);
const char *lbl = fastchart_label_or_null(
zend_hash_str_find(eht, "label", sizeof("label") - 1));
parsed[kept].label = lbl ? estrdup(lbl) : NULL;
parsed[kept].color_rgb = -1;
zval *zc = zend_hash_str_find(eht, "color", sizeof("color") - 1);
if (zc) ZVAL_DEREF(zc);
if (zc && Z_TYPE_P(zc) == IS_LONG) {
zend_long c = Z_LVAL_P(zc);
if (c >= 0 && c <= 0xFFFFFF) parsed[kept].color_rgb = (int)c;
}
kept++;
} ZEND_HASH_FOREACH_END();
*out = parsed;
*count = kept;
return 0;
}
int fastchart_graph_parse_links(zval *arr, int node_count, int max,
fastchart_graph_link **out, int *count)
{
*out = NULL;
*count = 0;
HashTable *ht = Z_ARRVAL_P(arr);
int n = zend_hash_num_elements(ht);
if (n > max) n = max;
if (n <= 0) return 0;
fastchart_graph_link *parsed = ecalloc(n, sizeof(*parsed));
int kept = 0;
zval *entry;
ZEND_HASH_FOREACH_VAL(ht, entry) {
if (kept >= n) break;
if (entry) ZVAL_DEREF(entry);
if (Z_TYPE_P(entry) != IS_ARRAY) continue;
HashTable *eht = Z_ARRVAL_P(entry);
zval *zf = zend_hash_str_find(eht, "from", sizeof("from") - 1);
zval *zt = zend_hash_str_find(eht, "to", sizeof("to") - 1);
zval *zv = zend_hash_str_find(eht, "value", sizeof("value") - 1);
if (!zf || !zt || !zv) continue;
ZVAL_DEREF(zf);
ZVAL_DEREF(zt);
if (Z_TYPE_P(zf) != IS_LONG || Z_TYPE_P(zt) != IS_LONG) continue;
zend_long from = Z_LVAL_P(zf), to = Z_LVAL_P(zt);
double val;
if (fastchart_zval_to_double(zv, &val) != 0 || !isfinite(val) ||
val <= 0 || val > FASTCHART_MAX_DATA_MAG) {
continue;
}
if (from < 0 || from >= node_count) continue;
if (to < 0 || to >= node_count) continue;
if (from == to) continue;
parsed[kept].from = (int)from;
parsed[kept].to = (int)to;
parsed[kept].value = val;
kept++;
} ZEND_HASH_FOREACH_END();
if (kept == 0) { efree(parsed); return 0; }
*out = parsed;
*count = kept;
return 0;
}
void fastchart_graph_free_nodes(fastchart_graph_node *nodes, int count)
{
if (!nodes) return;
for (int i = 0; i < count; i++) {
if (nodes[i].label) efree(nodes[i].label);
}
efree(nodes);
}
void fastchart_graph_free_links(fastchart_graph_link *links)
{
if (links) efree(links);
}
fastchart_graph_node *fastchart_graph_clone_nodes(
const fastchart_graph_node *nodes, int count)
{
if (!nodes || count <= 0) return NULL;
fastchart_graph_node *copy = emalloc(sizeof(*copy) * count);
for (int i = 0; i < count; i++) {
copy[i] = nodes[i];
copy[i].label = nodes[i].label ? estrdup(nodes[i].label) : NULL;
}
return copy;
}
fastchart_graph_link *fastchart_graph_clone_links(
const fastchart_graph_link *links, int count)
{
if (!links || count <= 0) return NULL;
size_t bytes = (size_t)count * sizeof(*links);
fastchart_graph_link *copy = emalloc(bytes);
memcpy(copy, links, bytes);
return copy;
}
void fastchart_graph_fields_release(fastchart_graph_node **nodes, int *ncount,
fastchart_graph_link **links, int *lcount)
{
fastchart_graph_free_nodes(*nodes, *ncount);
*nodes = NULL;
*ncount = 0;
fastchart_graph_free_links(*links);
*links = NULL;
*lcount = 0;
}
void fastchart_graph_fields_addref(fastchart_graph_node **nodes, int ncount,
fastchart_graph_link **links, int lcount)
{
*nodes = fastchart_graph_clone_nodes(*nodes, ncount);
*links = fastchart_graph_clone_links(*links, lcount);
}
void fastchart_graph_fields_set_nodes(fastchart_graph_node **nodes, int *ncount,
fastchart_graph_link **links, int *lcount,
zval *arr)
{
fastchart_graph_fields_release(nodes, ncount, links, lcount);
fastchart_graph_parse_nodes(arr, FASTCHART_MAX_GRAPH_NODES, nodes, ncount);
}
void fastchart_graph_fields_set_links(int node_count,
fastchart_graph_link **links, int *lcount,
zval *arr)
{
fastchart_graph_free_links(*links);
*links = NULL;
*lcount = 0;
fastchart_graph_parse_links(arr, node_count, FASTCHART_MAX_GRAPH_LINKS,
links, lcount);
}