php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
css.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2022 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7#include "lexbor/css/css.h"
8
9
10typedef struct {
13}
15
16
17static lxb_status_t
18lxb_css_str_cb(const lxb_char_t *data, size_t len, void *cb_ctx);
19
20
23{
24 return lexbor_calloc(1, sizeof(lxb_css_memory_t));
25}
26
28lxb_css_memory_init(lxb_css_memory_t *memory, size_t prepare_count)
29{
31
32 static const size_t size_mem = lexbor_max(sizeof(lxb_css_selector_t),
34
35 if (memory == NULL) {
37 }
38
39 if (prepare_count < 64) {
40 prepare_count = 64;
41 }
42
43 if (memory->objs == NULL) {
44 memory->objs = lexbor_dobject_create();
45 status = lexbor_dobject_init(memory->objs, prepare_count, size_mem);
46 if (status != LXB_STATUS_OK) {
47 goto failed;
48 }
49 }
50
51 if (memory->tree == NULL) {
52 prepare_count = prepare_count * 96;
53
54 memory->tree = lexbor_mraw_create();
55 status = lexbor_mraw_init(memory->tree, prepare_count);
56 if (status != LXB_STATUS_OK) {
57 goto failed;
58 }
59 }
60
61 if (memory->mraw == NULL) {
62 memory->mraw = lexbor_mraw_create();
63 status = lexbor_mraw_init(memory->mraw, 4096);
64 if (status != LXB_STATUS_OK) {
65 goto failed;
66 }
67 }
68
69 memory->ref_count = 1;
70
71 return LXB_STATUS_OK;
72
73failed:
74
75 (void) lxb_css_memory_destroy(memory, false);
76
77 return status;
78}
79
80void
82{
83 if (memory->objs != NULL) {
85 }
86
87 if (memory->mraw != NULL) {
88 lexbor_mraw_clean(memory->mraw);
89 }
90
91 if (memory->tree != NULL) {
92 lexbor_mraw_clean(memory->tree);
93 }
94}
95
97lxb_css_memory_destroy(lxb_css_memory_t *memory, bool self_destroy)
98{
99 if (memory == NULL) {
100 return NULL;
101 }
102
103 if (memory->objs != NULL) {
104 memory->objs = lexbor_dobject_destroy(memory->objs, true);
105 }
106
107 if (memory->mraw != NULL) {
108 memory->mraw = lexbor_mraw_destroy(memory->mraw, true);
109 }
110
111 if (memory->tree != NULL) {
112 memory->tree = lexbor_mraw_destroy(memory->tree, true);
113 }
114
115 if (self_destroy) {
116 return lexbor_free(memory);
117 }
118
119 return memory;
120}
121
124{
125 if (SIZE_MAX - memory->ref_count == 0) {
126 return NULL;
127 }
128
129 memory->ref_count++;
130
131 return memory;
132}
133
134void
136{
137 if (memory->ref_count > 0) {
138 memory->ref_count--;
139 }
140}
141
144{
145 if (memory->ref_count > 0) {
146 memory->ref_count--;
147 }
148
149 if (memory->ref_count == 0) {
150 return lxb_css_memory_destroy(memory, true);
151 }
152
153 return memory;
154}
155
158 uintptr_t begin, uintptr_t end)
159{
160 size_t length, offlen, len;
161 const lxb_char_t *pos;
162 const lexbor_str_t *tmp;
163
164 tmp = &parser->str;
165
166 offlen = begin - parser->offset;
167 length = end - begin;
168
169 if (str->data == NULL) {
170 (void) lexbor_str_init(str, parser->memory->mraw, length);
171 if (str->data == NULL) {
173 }
174 }
175
176 if (tmp->length > offlen) {
177 len = tmp->length - offlen;
178
179 if (len >= length) {
180 memcpy(str->data + str->length, tmp->data + offlen, length);
181 goto done;
182 }
183 else {
184 memcpy(str->data + str->length, tmp->data + offlen, len);
185 }
186
187 str->length += len;
188
189 pos = parser->pos;
190 length -= len;
191 }
192 else {
193 pos = parser->pos + (offlen - tmp->length);
194 }
195
196 memcpy(str->data + str->length, pos, length);
197
198done:
199
200 str->length += length;
201 str->data[str->length] = '\0';
202
203 return LXB_STATUS_OK;
204}
205
208 size_t *out_length)
209{
210 size_t length = 0;
212 lexbor_str_t str;
213
214 status = cb(style, lexbor_serialize_length_cb, &length);
215 if (status != LXB_STATUS_OK) {
216 goto failed;
217 }
218
219 /* + 1 == '\0' */
220 str.data = lexbor_malloc(length + 1);
221 if (str.data == NULL) {
222 goto failed;
223 }
224
225 str.length = 0;
226
227 status = cb(style, lexbor_serialize_copy_cb, &str);
228 if (status != LXB_STATUS_OK) {
229 lexbor_free(str.data);
230 goto failed;
231 }
232
233 str.data[str.length] = '\0';
234
235 if (out_length != NULL) {
236 *out_length = str.length;
237 }
238
239 return str.data;
240
241failed:
242
243 if (out_length != NULL) {
244 *out_length = 0;
245 }
246
247 return NULL;
248}
249
252 lexbor_mraw_t *mraw,
254{
256
257 ctx.str = str;
258 ctx.mraw = mraw;
259
260 if (str->data == NULL) {
261 lexbor_str_init(str, mraw, 1);
262 if (str->data == NULL) {
264 }
265 }
266
267 return cb(style, lxb_css_str_cb, &ctx);
268}
269
270static lxb_status_t
271lxb_css_str_cb(const lxb_char_t *data, size_t len, void *cb_ctx)
272{
274 lxb_css_str_ctx_t *ctx = (lxb_css_str_ctx_t *) cb_ctx;
275
276 ptr = lexbor_str_append(ctx->str, ctx->mraw, data, len);
277
279}
size_t len
Definition apprentice.c:174
char * cb
Definition assert.c:26
@ LXB_STATUS_ERROR_INCOMPLETE_OBJECT
Definition base.h:54
@ LXB_STATUS_ERROR_MEMORY_ALLOCATION
Definition base.h:51
@ LXB_STATUS_OK
Definition base.h:49
#define lexbor_max(val1, val2)
Definition base.h:39
struct lxb_css_memory lxb_css_memory_t
lxb_status_t(* lxb_css_style_serialize_f)(const void *style, lexbor_serialize_cb_f cb, void *ctx)
Definition base.h:56
struct lxb_css_parser lxb_css_parser_t
Definition base.h:41
struct lxb_css_selector lxb_css_selector_t
Definition base.h:39
struct lxb_css_selector_list lxb_css_selector_list_t
Definition base.h:40
DNS_STATUS status
Definition dns_win32.c:49
lxb_status_t lexbor_dobject_init(lexbor_dobject_t *dobject, size_t chunk_size, size_t struct_size)
Definition dobject.c:22
void lexbor_dobject_clean(lexbor_dobject_t *dobject)
Definition dobject.c:64
lexbor_dobject_t * lexbor_dobject_destroy(lexbor_dobject_t *dobject, bool destroy_self)
Definition dobject.c:75
lexbor_dobject_t * lexbor_dobject_create(void)
Definition dobject.c:16
lxb_status_t lxb_css_serialize_str_handler(const void *style, lexbor_str_t *str, lexbor_mraw_t *mraw, lxb_css_style_serialize_f cb)
Definition css.c:251
lxb_status_t lxb_css_memory_init(lxb_css_memory_t *memory, size_t prepare_count)
Definition css.c:28
lxb_css_memory_t * lxb_css_memory_ref_dec_destroy(lxb_css_memory_t *memory)
Definition css.c:143
lxb_css_memory_t * lxb_css_memory_destroy(lxb_css_memory_t *memory, bool self_destroy)
Definition css.c:97
lxb_css_memory_t * lxb_css_memory_ref_inc(lxb_css_memory_t *memory)
Definition css.c:123
lxb_css_memory_t * lxb_css_memory_create(void)
Definition css.c:22
lxb_status_t lxb_css_make_data(lxb_css_parser_t *parser, lexbor_str_t *str, uintptr_t begin, uintptr_t end)
Definition css.c:157
void lxb_css_memory_ref_dec(lxb_css_memory_t *memory)
Definition css.c:135
void lxb_css_memory_clean(lxb_css_memory_t *memory)
Definition css.c:81
lxb_char_t * lxb_css_serialize_char_handler(const void *style, lxb_css_style_serialize_f cb, size_t *out_length)
Definition css.c:207
int begin
Definition eaw_table.h:20
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
#define SIZE_MAX
Definition funcs.c:51
#define NULL
Definition gdcache.h:45
LXB_API void * lexbor_free(void *dst)
Definition memory.c:33
LXB_API void * lexbor_malloc(size_t size)
Definition memory.c:15
LXB_API void * lexbor_calloc(size_t num, size_t size)
Definition memory.c:27
lexbor_mraw_t * lexbor_mraw_create(void)
Definition mraw.c:32
void lexbor_mraw_clean(lexbor_mraw_t *mraw)
Definition mraw.c:76
lxb_status_t lexbor_mraw_init(lexbor_mraw_t *mraw, size_t chunk_size)
Definition mraw.c:38
lexbor_mraw_t * lexbor_mraw_destroy(lexbor_mraw_t *mraw, bool destroy_self)
Definition mraw.c:87
unsigned const char * end
Definition php_ffi.h:51
unsigned const char * pos
Definition php_ffi.h:52
zend_constant * data
lxb_status_t lexbor_serialize_length_cb(const lxb_char_t *data, size_t length, void *ctx)
Definition serialize.c:12
lxb_status_t lexbor_serialize_copy_cb(const lxb_char_t *data, size_t length, void *ctx)
Definition serialize.c:19
lxb_char_t * lexbor_str_append(lexbor_str_t *str, lexbor_mraw_t *mraw, const lxb_char_t *buff, size_t length)
Definition str.c:131
lxb_char_t * lexbor_str_init(lexbor_str_t *str, lexbor_mraw_t *mraw, size_t size)
Definition str.c:22
lxb_char_t * data
Definition str.h:47
size_t length
Definition str.h:48
lexbor_dobject_t * objs
Definition base.h:31
lexbor_mraw_t * mraw
Definition base.h:32
size_t ref_count
Definition base.h:35
lexbor_mraw_t * tree
Definition base.h:33
uintptr_t offset
Definition parser.h:163
lxb_css_memory_t * memory
Definition parser.h:141
const lxb_char_t * pos
Definition parser.h:162
lexbor_str_t str
Definition parser.h:165
lexbor_mraw_t * mraw
Definition css.c:12
lexbor_str_t * str
Definition css.c:11
unsigned int lxb_status_t
Definition types.h:28
unsigned char lxb_char_t
Definition types.h:27
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)