Repository navigation
Expand file tree
/
Copy pathexcel.c
More file actions
11814 lines (10104 loc) · 343 KB
/
Copy pathexcel.c
File metadata and controls
11814 lines (10104 loc) · 343 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2026 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Ilia Alshanetsky <ilia@ilia.ws> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "libxl.h"
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include "php.h"
#include "php_ini.h"
#if PHP_VERSION_ID < 80200
#include "ext/standard/php_random.h"
#else
#include "ext/random/php_random.h"
#endif
#include "ext/standard/info.h"
#include "ext/date/php_date.h"
#include "php_excel.h"
#include "libxl_compat.h"
#include "zend_exceptions.h"
#include "Zend/zend_smart_str.h"
#include <locale.h>
#ifdef HAVE_NL_LANGINFO
# include <langinfo.h>
#endif
#if !defined(LIBXL_VERSION) || LIBXL_VERSION < 0x04060000
#error "LibXL version 4.6.0+ required"
#endif
/* gen_stub.php on PHP master emits register_class_*() functions that call
* zend_register_internal_class_with_flags(), which only exists in PHP 8.4+.
* Polyfill for older targets (we support 8.1+) so the generated arginfo
* header compiles unchanged. */
#if PHP_VERSION_ID < 80400
static zend_always_inline zend_class_entry *zend_register_internal_class_with_flags(
zend_class_entry *class_entry,
zend_class_entry *parent_ce,
uint32_t ce_flags)
{
zend_class_entry *ce = zend_register_internal_class_ex(class_entry, parent_ce);
if (ce && ce_flags) {
ce->ce_flags |= ce_flags;
}
return ce;
}
#endif
/* Deliberately not done:
* - Splitting this file: the 12 classes share the generation-guard macros
* and the bulk read/write and save helpers below.
* - One generation counter instead of several: handle families must fail
* independently (sheet-topology changes must not invalidate formats).
* - Generating docs/ from the stub: docs/ has hand-written descriptions
* the stub cannot carry. */
#define PHP_EXCEL_DATE 1
#define PHP_EXCEL_FORMULA 2
#define PHP_EXCEL_NUMERIC_STRING 3
#define PHP_EXCEL_TEXT 4
#define PHP_EXCEL_VERSION "2.9.1"
#ifdef COMPILE_DL_EXCEL
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(excel)
#endif
ZEND_DECLARE_MODULE_GLOBALS(excel)
static PHP_GINIT_FUNCTION(excel);
PHP_INI_BEGIN()
#if defined(HAVE_LIBXL_SETKEY)
/* Commercial license credentials: PHP_INI_SYSTEM so a per-request
* ini_set() or per-directory .user.ini/.htaccess cannot mutate the key
* in a shared pool. A runtime key can still be supplied per book via the
* ExcelBook constructor. */
STD_PHP_INI_ENTRY("excel.license_name", NULL, PHP_INI_SYSTEM, OnUpdateString, ini_license_name, zend_excel_globals, excel_globals)
STD_PHP_INI_ENTRY("excel.license_key", NULL, PHP_INI_SYSTEM, OnUpdateString, ini_license_key, zend_excel_globals, excel_globals)
#endif
STD_PHP_INI_ENTRY("excel.skip_empty", "0", PHP_INI_ALL, OnUpdateLong, ini_skip_empty, zend_excel_globals, excel_globals)
PHP_INI_END()
/* {{{ OO init/structure stuff */
/* default_object_handlers is a class-entry field added in PHP 8.3; on
* 8.1/8.2 each create_object handler sets intern->std.handlers directly,
* which is the correct per-object mechanism there. */
#if PHP_VERSION_ID >= 80300
#define EXCEL_SET_DEFAULT_OBJECT_HANDLERS(c_name) \
excel_ce_ ## c_name->default_object_handlers = &excel_object_handlers_ ## c_name
#else
#define EXCEL_SET_DEFAULT_OBJECT_HANDLERS(c_name) ((void)0)
#endif
/* Class registration uses the gen_stub-generated register_class_*()
* functions from excel_arginfo.h; this macro layers on create_object,
* NOT_SERIALIZABLE, and per-class object_handlers. */
#define REGISTER_EXCEL_CLASS(name, c_name, clone) \
{ \
excel_ce_ ## c_name = register_class_Excel ## name(); \
excel_ce_ ## c_name->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; \
excel_ce_ ## c_name->create_object = excel_object_new_ ## c_name; \
memcpy(&excel_object_handlers_ ## c_name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
EXCEL_SET_DEFAULT_OBJECT_HANDLERS(c_name); \
excel_object_handlers_ ## c_name .offset = offsetof(excel_ ## c_name ## _object, std); \
excel_object_handlers_ ## c_name .free_obj = excel_ ## c_name ## _object_free_storage; \
excel_object_handlers_ ## c_name .clone_obj = clone; \
}
zend_class_entry *excel_ce_book, *excel_ce_sheet, *excel_ce_format, *excel_ce_font, *excel_ce_filtercolumn, *excel_ce_autofilter;
zend_class_entry *excel_ce_richstring, *excel_ce_formcontrol, *excel_ce_conditionalformat, *excel_ce_conditionalformatting, *excel_ce_coreproperties, *excel_ce_table;
static zend_object_handlers excel_object_handlers_book;
static zend_object_handlers excel_object_handlers_sheet;
static zend_object_handlers excel_object_handlers_format;
static zend_object_handlers excel_object_handlers_font;
static zend_object_handlers excel_object_handlers_autofilter;
static zend_object_handlers excel_object_handlers_filtercolumn;
static zend_object_handlers excel_object_handlers_richstring;
static zend_object_handlers excel_object_handlers_formcontrol;
static zend_object_handlers excel_object_handlers_conditionalformat;
static zend_object_handlers excel_object_handlers_conditionalformatting;
static zend_object_handlers excel_object_handlers_coreproperties;
static zend_object_handlers excel_object_handlers_table;
typedef struct _excel_book_object {
BookHandle book;
/* Bumped whenever the underlying libxl book state is reset (load,
* loadFile, clear, loadInfo, loadInfoRaw, manual __construct reuse).
* Every child wrapper stamps this at creation and refuses to use a
* stale book after it diverges. */
uint64_t generation;
/* Bumped for sheet-topology mutations that leave workbook-scoped
* handles valid but can retarget sheet-derived wrappers. */
uint64_t sheet_generation;
/* Bumped when libxl removes child handles that existing PHP wrappers
* may still hold; scoped by handle family to keep owners usable. */
uint64_t autofilter_generation;
uint64_t conditional_formatting_generation;
/* True for xlCreateXMLBook() (XLSX, 1048576x16384), false for
* xlCreateBook() (XLS, 65536x256). Used by EXCEL_VALIDATE_ROW_COL to
* reject impossible coordinates per book type. */
bool is_xlsx;
bool locale_is_utf8;
/* True when libxl decodes narrow strings with its built-in UTF-8
* decoder instead of mbstowcs() under the process LC_CTYPE. */
bool text_utf8_decoder;
/* An XLS rich-string run that libxl dropped leaves state that crashes
* the next save inside libxl; saves are refused until the handle is
* replaced or cleared. */
bool rich_text_add_failed;
/* Explicit per-book license credentials from __construct (NULL when the
* INI fallback supplied the key). Re-applied to scratch reload handles
* so a licensed book stays licensed across a successful reload-swap.
* INI credentials need no storage: PHP_INI_SYSTEM pins them process-wide. */
char *license_name;
char *license_key;
/* Locale string libxl last accepted: "UTF-8" from __construct, or the
* setLocale() argument. Re-applied to scratch reload handles so encoding
* behavior survives a successful reload-swap. */
char *locale;
/* Copies of text re-read by php_excel_lossy_read_end(); each stays valid
* until the next lossy re-read on this book, like a LibXL buffer. */
zend_string *lossy_text[2];
/* Lazily-allocated default format used by IS_DATE writes when the
* caller doesn't supply an explicit format, so bulk date exports
* don't create one new format per cell and bloat the style table.
* Reset along with `book` on __construct reuse. */
FormatHandle default_date_format;
/* True while a LibXL-owned buffer (SaveRaw/getPicture) is being copied
* into a PHP string. Mutating APIs must not free/replace that buffer. */
bool native_buffer_borrowed;
/* Set after loadInfo()/loadInfoRaw() commit. LibXL then holds only the
* sheet names, and most book APIs dereference NULL on XLSX handles. */
bool info_only;
zend_object std;
} excel_book_object;
static inline excel_book_object *php_excel_book_object_fetch_object(zend_object *obj) {
return (excel_book_object *)((char *)(obj) - offsetof(excel_book_object, std));
}
#define Z_EXCEL_BOOK_OBJ_P(zv) php_excel_book_object_fetch_object(Z_OBJ_P(zv));
#define PHP_EXCEL_BOOK_INFO_ONLY_MSG \
"The book was loaded with loadInfo() or loadInfoRaw(); load it fully first"
#define BOOK_FROM_OBJECT(book, object) \
{ \
excel_book_object *obj = Z_EXCEL_BOOK_OBJ_P(object); \
book = obj->book; \
if (!book) { \
php_error_docref(NULL, E_WARNING, "The book wasn't initialized"); \
RETURN_FALSE; \
} \
if (obj->info_only) { \
php_error_docref(NULL, E_WARNING, PHP_EXCEL_BOOK_INFO_ONLY_MSG); \
RETURN_FALSE; \
} \
}
/* Only for the methods that are safe on an info-only book. */
#define BOOK_FROM_OBJECT_INFO_OK(book, object) \
{ \
excel_book_object *obj = Z_EXCEL_BOOK_OBJ_P(object); \
book = obj->book; \
if (!book) { \
php_error_docref(NULL, E_WARNING, "The book wasn't initialized"); \
RETURN_FALSE; \
} \
}
/* Post-stream-read variant: the fetch runs after user stream callbacks, so an
* owned buffer is already live and must be released on the bail path. */
#define BOOK_FROM_OBJECT_RELEASE_STR(book, object, contents_zs) \
{ \
excel_book_object *obj = Z_EXCEL_BOOK_OBJ_P(object); \
book = obj->book; \
if (!book) { \
zend_string_release(contents_zs); \
php_error_docref(NULL, E_WARNING, "The book wasn't initialized"); \
RETURN_FALSE; \
} \
if (obj->info_only) { \
zend_string_release(contents_zs); \
php_error_docref(NULL, E_WARNING, PHP_EXCEL_BOOK_INFO_ONLY_MSG); \
RETURN_FALSE; \
} \
}
typedef struct _excel_sheet_object {
SheetHandle sheet;
BookHandle book;
uint64_t book_generation;
uint64_t sheet_generation;
zval parent;
zend_object std;
} excel_sheet_object;
static inline excel_sheet_object *php_excel_sheet_object_fetch_object(zend_object *obj) {
return (excel_sheet_object *)((char *)(obj) - offsetof(excel_sheet_object, std));
}
#define Z_EXCEL_SHEET_OBJ_P(zv) php_excel_sheet_object_fetch_object(Z_OBJ_P(zv));
#define SHEET_FROM_OBJECT(sheet, object) \
{ \
excel_sheet_object *obj = Z_EXCEL_SHEET_OBJ_P(object); \
sheet = obj->sheet; \
if (!sheet) { \
php_error_docref(NULL, E_WARNING, "The sheet wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_AND_SHEET_GENERATION(obj); \
}
#define SHEET_AND_BOOK_FROM_OBJECT(sheet, book, object) \
{ \
excel_sheet_object *obj = Z_EXCEL_SHEET_OBJ_P(object); \
sheet = obj->sheet; \
book = obj->book; \
if (!sheet) { \
php_error_docref(NULL, E_WARNING, "The sheet wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_AND_SHEET_GENERATION(obj); \
}
typedef struct _excel_font_object {
FontHandle font;
BookHandle book;
uint64_t book_generation;
zval parent;
zend_object std;
} excel_font_object;
static inline excel_font_object *php_excel_font_object_fetch_object(zend_object *obj) {
return (excel_font_object *)((char *)(obj) - offsetof(excel_font_object, std));
}
#define Z_EXCEL_FONT_OBJ_P(zv) php_excel_font_object_fetch_object(Z_OBJ_P(zv));
#define FONT_FROM_OBJECT(font, object) \
{ \
excel_font_object *obj = Z_EXCEL_FONT_OBJ_P(object); \
font = obj->font; \
if (!font) { \
php_error_docref(NULL, E_WARNING, "The font wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_GENERATION(obj); \
}
#define FORMAT_FROM_OBJECT(format, object) \
{ \
excel_format_object *obj = Z_EXCEL_FORMAT_OBJ_P(object); \
format = obj->format; \
if (!format) { \
php_error_docref(NULL, E_WARNING, "The format wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_GENERATION(obj); \
}
#define AUTOFILTER_FROM_OBJECT(autofilter, object) \
{ \
excel_autofilter_object *obj = Z_EXCEL_AUTOFILTER_OBJ_P(object); \
autofilter = obj->autofilter; \
if (!autofilter) { \
php_error_docref(NULL, E_WARNING, "The autofilter wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_SHEET_AND_AUTOFILTER_GENERATION(obj); \
}
#define FILTERCOLUMN_FROM_OBJECT(filtercolumn, object) \
{ \
excel_filtercolumn_object *obj = Z_EXCEL_FILTERCOLUMN_OBJ_P(object); \
filtercolumn = obj->filtercolumn; \
if (!filtercolumn) { \
php_error_docref(NULL, E_WARNING, "The filtercolumn wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_SHEET_AND_AUTOFILTER_GENERATION(obj); \
}
typedef struct _excel_format_object {
FormatHandle format;
BookHandle book;
uint64_t book_generation;
zval parent;
zend_object std;
} excel_format_object;
static inline excel_format_object *php_excel_format_object_fetch_object(zend_object *obj) {
return (excel_format_object *)((char *)(obj) - offsetof(excel_format_object, std));
}
#define Z_EXCEL_FORMAT_OBJ_P(zv) php_excel_format_object_fetch_object(Z_OBJ_P(zv));
typedef struct _excel_autofilter_object {
AutoFilterHandle autofilter;
SheetHandle sheet;
uint64_t book_generation;
uint64_t sheet_generation;
uint64_t autofilter_generation;
zval parent;
zend_object std;
} excel_autofilter_object;
static inline excel_autofilter_object *php_excel_autofilter_object_fetch_object(zend_object *obj) {
return (excel_autofilter_object *)((char *)(obj) - offsetof(excel_autofilter_object, std));
}
#define Z_EXCEL_AUTOFILTER_OBJ_P(zv) php_excel_autofilter_object_fetch_object(Z_OBJ_P(zv));
typedef struct _excel_filtercolumn_object {
FilterColumnHandle filtercolumn;
AutoFilterHandle autofilter;
uint64_t book_generation;
uint64_t sheet_generation;
uint64_t autofilter_generation;
zval parent;
zend_object std;
} excel_filtercolumn_object;
static inline excel_filtercolumn_object *php_excel_filtercolumn_object_fetch_object(zend_object *obj) {
return (excel_filtercolumn_object *)((char *)(obj) - offsetof(excel_filtercolumn_object, std));
}
#define Z_EXCEL_FILTERCOLUMN_OBJ_P(zv) php_excel_filtercolumn_object_fetch_object(Z_OBJ_P(zv));
typedef struct _excel_richstring_object {
RichStringHandle richstring;
BookHandle book;
uint64_t book_generation;
zval parent;
zend_object std;
} excel_richstring_object;
static inline excel_richstring_object *php_excel_richstring_object_fetch_object(zend_object *obj) {
return (excel_richstring_object *)((char *)(obj) - offsetof(excel_richstring_object, std));
}
#define Z_EXCEL_RICHSTRING_OBJ_P(zv) php_excel_richstring_object_fetch_object(Z_OBJ_P(zv));
#define RICHSTRING_FROM_OBJECT(rs_var, object) \
{ \
excel_richstring_object *obj = Z_EXCEL_RICHSTRING_OBJ_P(object); \
rs_var = obj->richstring; \
if (!rs_var) { \
php_error_docref(NULL, E_WARNING, "The richstring wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_GENERATION(obj); \
}
typedef struct _excel_formcontrol_object {
FormControlHandle formcontrol;
SheetHandle sheet;
uint64_t book_generation;
uint64_t sheet_generation;
zval parent;
zend_object std;
} excel_formcontrol_object;
static inline excel_formcontrol_object *php_excel_formcontrol_object_fetch_object(zend_object *obj) {
return (excel_formcontrol_object *)((char *)(obj) - offsetof(excel_formcontrol_object, std));
}
#define Z_EXCEL_FORMCONTROL_OBJ_P(zv) php_excel_formcontrol_object_fetch_object(Z_OBJ_P(zv));
#define FORMCONTROL_FROM_OBJECT(fc_var, object) \
{ \
excel_formcontrol_object *obj = Z_EXCEL_FORMCONTROL_OBJ_P(object); \
fc_var = obj->formcontrol; \
if (!fc_var) { \
php_error_docref(NULL, E_WARNING, "The formcontrol wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_AND_SHEET_GENERATION(obj); \
}
typedef struct _excel_conditionalformat_object {
ConditionalFormatHandle conditionalformat;
BookHandle book;
uint64_t book_generation;
zval parent;
zend_object std;
} excel_conditionalformat_object;
static inline excel_conditionalformat_object *php_excel_conditionalformat_object_fetch_object(zend_object *obj) {
return (excel_conditionalformat_object *)((char *)(obj) - offsetof(excel_conditionalformat_object, std));
}
#define Z_EXCEL_CONDITIONALFORMAT_OBJ_P(zv) php_excel_conditionalformat_object_fetch_object(Z_OBJ_P(zv));
#define CONDITIONALFORMAT_FROM_OBJECT(cf_var, object) \
{ \
excel_conditionalformat_object *obj = Z_EXCEL_CONDITIONALFORMAT_OBJ_P(object); \
cf_var = obj->conditionalformat; \
if (!cf_var) { \
php_error_docref(NULL, E_WARNING, "The conditionalformat wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_GENERATION(obj); \
}
typedef struct _excel_conditionalformatting_object {
ConditionalFormattingHandle conditionalformatting;
SheetHandle sheet;
uint64_t book_generation;
uint64_t sheet_generation;
uint64_t conditional_formatting_generation;
zval parent;
zend_object std;
} excel_conditionalformatting_object;
static inline excel_conditionalformatting_object *php_excel_conditionalformatting_object_fetch_object(zend_object *obj) {
return (excel_conditionalformatting_object *)((char *)(obj) - offsetof(excel_conditionalformatting_object, std));
}
#define Z_EXCEL_CONDITIONALFORMATTING_OBJ_P(zv) php_excel_conditionalformatting_object_fetch_object(Z_OBJ_P(zv));
#define CONDITIONALFORMATTING_FROM_OBJECT(cfing_var, object) \
{ \
excel_conditionalformatting_object *obj = Z_EXCEL_CONDITIONALFORMATTING_OBJ_P(object); \
cfing_var = obj->conditionalformatting; \
if (!cfing_var) { \
php_error_docref(NULL, E_WARNING, "The conditionalformatting wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_SHEET_AND_CONDITIONALFORMATTING_GENERATION(obj); \
}
typedef struct _excel_coreproperties_object {
CorePropertiesHandle coreproperties;
BookHandle book;
uint64_t book_generation;
zval parent;
zend_object std;
} excel_coreproperties_object;
static inline excel_coreproperties_object *php_excel_coreproperties_object_fetch_object(zend_object *obj) {
return (excel_coreproperties_object *)((char *)(obj) - offsetof(excel_coreproperties_object, std));
}
#define Z_EXCEL_COREPROPERTIES_OBJ_P(zv) php_excel_coreproperties_object_fetch_object(Z_OBJ_P(zv));
#define COREPROPERTIES_FROM_OBJECT(cp_var, object) \
{ \
excel_coreproperties_object *obj = Z_EXCEL_COREPROPERTIES_OBJ_P(object); \
cp_var = obj->coreproperties; \
if (!cp_var) { \
php_error_docref(NULL, E_WARNING, "The coreproperties wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_GENERATION(obj); \
}
typedef struct _excel_table_object {
TableHandle table;
SheetHandle sheet;
uint64_t book_generation;
uint64_t sheet_generation;
zval parent;
zend_object std;
} excel_table_object;
static inline excel_table_object *php_excel_table_object_fetch_object(zend_object *obj) {
return (excel_table_object *)((char *)(obj) - offsetof(excel_table_object, std));
}
#define Z_EXCEL_TABLE_OBJ_P(zv) php_excel_table_object_fetch_object(Z_OBJ_P(zv));
#define TABLE_FROM_OBJECT(tbl_var, object) \
{ \
excel_table_object *obj = Z_EXCEL_TABLE_OBJ_P(object); \
tbl_var = obj->table; \
if (!tbl_var) { \
php_error_docref(NULL, E_WARNING, "The table wasn't initialized"); \
RETURN_FALSE; \
} \
CHECK_BOOK_AND_SHEET_GENERATION(obj); \
}
/* Walk the parent zval chain up to the owning ExcelBook so that any descendant
* (sheet, format, autofilter, filtercolumn, table, ...) can compare its
* book_generation against the book's current generation counter. */
static inline excel_book_object *php_excel_resolve_book_obj(zval *parent_zv) {
int hops = 0;
while (parent_zv && Z_TYPE_P(parent_zv) == IS_OBJECT && hops++ < 8) {
zend_class_entry *ce = Z_OBJCE_P(parent_zv);
if (instanceof_function(ce, excel_ce_book)) {
return php_excel_book_object_fetch_object(Z_OBJ_P(parent_zv));
} else if (instanceof_function(ce, excel_ce_sheet)) {
parent_zv = &php_excel_sheet_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_format)) {
parent_zv = &php_excel_format_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_font)) {
parent_zv = &php_excel_font_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_autofilter)) {
parent_zv = &php_excel_autofilter_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_filtercolumn)) {
parent_zv = &php_excel_filtercolumn_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_richstring)) {
parent_zv = &php_excel_richstring_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_formcontrol)) {
parent_zv = &php_excel_formcontrol_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_conditionalformat)) {
parent_zv = &php_excel_conditionalformat_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_conditionalformatting)) {
parent_zv = &php_excel_conditionalformatting_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_coreproperties)) {
parent_zv = &php_excel_coreproperties_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else if (instanceof_function(ce, excel_ce_table)) {
parent_zv = &php_excel_table_object_fetch_object(Z_OBJ_P(parent_zv))->parent;
} else {
return NULL;
}
}
return NULL;
}
/* libxl Format/Font/RichString/AutoFilter/ConditionalFormat handles are scoped
* to the workbook that created them and index into that book's internal
* tables. Applying one to a different workbook's sheet/cell/rule silently
* produces wrong output and dangles once the source book is freed. Reject a
* child-wrapper argument whose owning book differs from the target's. Returns 1
* when both resolve to the same ExcelBook, 0 otherwise. Template-copy methods
* such as Book::addFormat/addFont legitimately accept handles from another
* book and must NOT use this guard. */
static zend_always_inline int php_excel_same_book(zval *arg_zv, zval *target_zv) {
excel_book_object *ba = php_excel_resolve_book_obj(arg_zv);
excel_book_object *bb = php_excel_resolve_book_obj(target_zv);
return ba && bb && ba == bb;
}
/* Warn-and-reject form of php_excel_same_book for method bodies. Returns true
* when the argument is usable; false after warning when books differ. */
static zend_always_inline bool php_excel_require_same_book(zval *arg_zv, zval *target_zv)
{
if (php_excel_same_book(arg_zv, target_zv)) {
return true;
}
php_error_docref(NULL, E_WARNING, "Argument belongs to a different ExcelBook");
return false;
}
#define EXCEL_REQUIRE_SAME_BOOK(arg_zv, target_zv) \
if (!php_excel_require_same_book((arg_zv), (target_zv))) { \
RETURN_FALSE; \
}
/* Generation predicates and warn-path checks. Throw variants (below) share the
* same matchers so stale messages stay in one place per failure mode. */
static zend_always_inline bool php_excel_book_generation_matches(excel_book_object *b, uint64_t stamped)
{
return b && b->book && b->generation == stamped;
}
static zend_always_inline bool php_excel_book_generation_ok(excel_book_object *b, uint64_t stamped)
{
if (php_excel_book_generation_matches(b, stamped)) {
return true;
}
php_error_docref(NULL, E_WARNING,
"Underlying ExcelBook handle is stale (parent was reloaded, cleared, or reinitialized)");
return false;
}
static zend_always_inline bool php_excel_sheet_generation_ok(excel_book_object *b, uint64_t stamped)
{
if (b->sheet_generation == stamped) {
return true;
}
php_error_docref(NULL, E_WARNING,
"Underlying ExcelBook sheet topology changed; wrapper must be re-fetched");
return false;
}
static zend_always_inline bool php_excel_autofilter_generation_ok(excel_book_object *b, uint64_t stamped)
{
if (b->autofilter_generation == stamped) {
return true;
}
php_error_docref(NULL, E_WARNING,
"Underlying ExcelBook autofilter state changed; wrapper must be re-fetched");
return false;
}
static zend_always_inline bool php_excel_conditional_formatting_generation_ok(excel_book_object *b, uint64_t stamped)
{
if (b->conditional_formatting_generation == stamped) {
return true;
}
php_error_docref(NULL, E_WARNING,
"Underlying ExcelBook conditional formatting state changed; wrapper must be re-fetched");
return false;
}
/* Preresolved: caller already has the owning book (hot paths that also need
* coordinate limits can resolve once and feed both checks). */
static zend_always_inline bool php_excel_check_book_generation_pr(excel_book_object *vb, uint64_t book_stamped)
{
return php_excel_book_generation_ok(vb, book_stamped);
}
static zend_always_inline bool php_excel_check_book_and_sheet_generation_pr(
excel_book_object *vb, uint64_t book_stamped, uint64_t sheet_stamped)
{
if (!php_excel_book_generation_ok(vb, book_stamped)) {
return false;
}
return php_excel_sheet_generation_ok(vb, sheet_stamped);
}
static zend_always_inline bool php_excel_check_book_sheet_and_autofilter_generation_pr(
excel_book_object *vb, uint64_t book_stamped, uint64_t sheet_stamped, uint64_t autofilter_stamped)
{
if (!php_excel_check_book_and_sheet_generation_pr(vb, book_stamped, sheet_stamped)) {
return false;
}
return php_excel_autofilter_generation_ok(vb, autofilter_stamped);
}
static zend_always_inline bool php_excel_check_book_sheet_and_conditional_formatting_generation_pr(
excel_book_object *vb, uint64_t book_stamped, uint64_t sheet_stamped, uint64_t cf_stamped)
{
if (!php_excel_check_book_and_sheet_generation_pr(vb, book_stamped, sheet_stamped)) {
return false;
}
return php_excel_conditional_formatting_generation_ok(vb, cf_stamped);
}
static zend_always_inline bool php_excel_check_book_generation(zval *parent_zv, uint64_t book_stamped)
{
return php_excel_check_book_generation_pr(php_excel_resolve_book_obj(parent_zv), book_stamped);
}
static zend_always_inline bool php_excel_check_book_and_sheet_generation(
zval *parent_zv, uint64_t book_stamped, uint64_t sheet_stamped)
{
return php_excel_check_book_and_sheet_generation_pr(
php_excel_resolve_book_obj(parent_zv), book_stamped, sheet_stamped);
}
static zend_always_inline bool php_excel_check_book_sheet_and_autofilter_generation(
zval *parent_zv, uint64_t book_stamped, uint64_t sheet_stamped, uint64_t autofilter_stamped)
{
return php_excel_check_book_sheet_and_autofilter_generation_pr(
php_excel_resolve_book_obj(parent_zv), book_stamped, sheet_stamped, autofilter_stamped);
}
static zend_always_inline bool php_excel_check_book_sheet_and_conditional_formatting_generation(
zval *parent_zv, uint64_t book_stamped, uint64_t sheet_stamped, uint64_t cf_stamped)
{
return php_excel_check_book_sheet_and_conditional_formatting_generation_pr(
php_excel_resolve_book_obj(parent_zv), book_stamped, sheet_stamped, cf_stamped);
}
#define CHECK_BOOK_GENERATION(child_obj) \
do { \
if (!php_excel_check_book_generation(&(child_obj)->parent, (child_obj)->book_generation)) { \
RETURN_FALSE; \
} \
} while (0)
#define CHECK_BOOK_AND_SHEET_GENERATION(child_obj) \
do { \
if (!php_excel_check_book_and_sheet_generation(&(child_obj)->parent, \
(child_obj)->book_generation, (child_obj)->sheet_generation)) { \
RETURN_FALSE; \
} \
} while (0)
#define CHECK_BOOK_SHEET_AND_AUTOFILTER_GENERATION(child_obj) \
do { \
if (!php_excel_check_book_sheet_and_autofilter_generation(&(child_obj)->parent, \
(child_obj)->book_generation, (child_obj)->sheet_generation, \
(child_obj)->autofilter_generation)) { \
RETURN_FALSE; \
} \
} while (0)
#define CHECK_BOOK_SHEET_AND_CONDITIONALFORMATTING_GENERATION(child_obj) \
do { \
if (!php_excel_check_book_sheet_and_conditional_formatting_generation(&(child_obj)->parent, \
(child_obj)->book_generation, (child_obj)->sheet_generation, \
(child_obj)->conditional_formatting_generation)) { \
RETURN_FALSE; \
} \
} while (0)
/* Preresolved variants: resolve_book_obj once for stale check + coord limits. */
#define CHECK_BOOK_GENERATION_PR(child_obj, vb) \
do { \
if (!php_excel_check_book_generation_pr((vb), (child_obj)->book_generation)) { \
RETURN_FALSE; \
} \
} while (0)
#define CHECK_BOOK_AND_SHEET_GENERATION_PR(child_obj, vb) \
do { \
if (!php_excel_check_book_and_sheet_generation_pr((vb), \
(child_obj)->book_generation, (child_obj)->sheet_generation)) { \
RETURN_FALSE; \
} \
} while (0)
/* Stamp child wrapper with current book generation and copy parent zval.
* Use at every child creation site instead of a bare ZVAL_COPY(&x->parent, p).
* Re-invoking a child __construct() reaches this with parent already holding a
* refcounted zval from the first construct; drop it before the overwrite or
* that reference leaks. First construct is safe: zend_object_alloc zeroed
* parent to IS_UNDEF. */
static zend_always_inline void php_excel_replace_parent_zval(zval *parent_field, zval *parent_zv)
{
if (Z_TYPE_P(parent_field) != IS_UNDEF) {
zval_ptr_dtor(parent_field);
}
ZVAL_COPY(parent_field, parent_zv);
}
static zend_always_inline void php_excel_init_parent(
uint64_t *book_generation, zval *parent_field, zval *parent_zv)
{
excel_book_object *bg = php_excel_resolve_book_obj(parent_zv);
*book_generation = bg ? bg->generation : 0;
php_excel_replace_parent_zval(parent_field, parent_zv);
}
static zend_always_inline void php_excel_init_sheet_parent(
uint64_t *book_generation, uint64_t *sheet_generation, zval *parent_field, zval *parent_zv)
{
excel_book_object *bg = php_excel_resolve_book_obj(parent_zv);
*book_generation = bg ? bg->generation : 0;
*sheet_generation = bg ? bg->sheet_generation : 0;
php_excel_replace_parent_zval(parent_field, parent_zv);
}
static zend_always_inline void php_excel_init_autofilter_parent(
uint64_t *book_generation, uint64_t *sheet_generation, uint64_t *autofilter_generation,
zval *parent_field, zval *parent_zv)
{
excel_book_object *bg = php_excel_resolve_book_obj(parent_zv);
*book_generation = bg ? bg->generation : 0;
*sheet_generation = bg ? bg->sheet_generation : 0;
*autofilter_generation = bg ? bg->autofilter_generation : 0;
php_excel_replace_parent_zval(parent_field, parent_zv);
}
static zend_always_inline void php_excel_init_conditional_formatting_parent(
uint64_t *book_generation, uint64_t *sheet_generation, uint64_t *cf_generation,
zval *parent_field, zval *parent_zv)
{
excel_book_object *bg = php_excel_resolve_book_obj(parent_zv);
*book_generation = bg ? bg->generation : 0;
*sheet_generation = bg ? bg->sheet_generation : 0;
*cf_generation = bg ? bg->conditional_formatting_generation : 0;
php_excel_replace_parent_zval(parent_field, parent_zv);
}
#define EXCEL_INIT_PARENT(child_obj, parent_zv) \
php_excel_init_parent(&(child_obj)->book_generation, &(child_obj)->parent, (parent_zv))
#define EXCEL_INIT_SHEET_PARENT(child_obj, parent_zv) \
php_excel_init_sheet_parent(&(child_obj)->book_generation, &(child_obj)->sheet_generation, \
&(child_obj)->parent, (parent_zv))
#define EXCEL_INIT_AUTOFILTER_PARENT(child_obj, parent_zv) \
php_excel_init_autofilter_parent(&(child_obj)->book_generation, &(child_obj)->sheet_generation, \
&(child_obj)->autofilter_generation, &(child_obj)->parent, (parent_zv))
#define EXCEL_INIT_CONDITIONALFORMATTING_PARENT(child_obj, parent_zv) \
php_excel_init_conditional_formatting_parent(&(child_obj)->book_generation, \
&(child_obj)->sheet_generation, &(child_obj)->conditional_formatting_generation, \
&(child_obj)->parent, (parent_zv))
#define EXCEL_REJECT_RECONSTRUCTION(child_obj, handle_field) \
do { \
if ((child_obj)->handle_field) { \
zend_throw_exception(NULL, "Cannot call constructor twice", 0); \
RETURN_THROWS(); \
} \
} while (0)
/* Bump the sheet-topology counter so sheet-derived wrappers fail their
* stale-check. Workbook-scoped handles remain valid. */
static inline void php_excel_book_bump_sheet_generation(zval *book_zv) {
excel_book_object *bobj = php_excel_book_object_fetch_object(Z_OBJ_P(book_zv));
bobj->sheet_generation++;
}
static inline void php_excel_book_bump_autofilter_generation(zval *parent_zv) {
excel_book_object *bobj = php_excel_resolve_book_obj(parent_zv);
if (bobj) {
bobj->autofilter_generation++;
}
}
static inline void php_excel_book_bump_conditional_formatting_generation(zval *parent_zv) {
excel_book_object *bobj = php_excel_resolve_book_obj(parent_zv);
if (bobj) {
bobj->conditional_formatting_generation++;
}
}
/* libxl picks its narrow-string decoder per book: the built-in UTF-8 decoder
* after setLocale("UTF-8"), and since 5.2 also when the LC_CTYPE name in
* effect at creation or setLocale() contains "UTF-8"; mbstowcs() under the
* LC_CTYPE current at each call otherwise, including after a failed
* setLocale(). */
static bool php_excel_locale_name_selects_utf8_decoder(const char *name)
{
#if LIBXL_VERSION >= 0x05020000
return name && strstr(name, "UTF-8") != NULL;
#else
(void) name;
return false;
#endif
}
static bool php_excel_locale_selects_utf8_decoder(const char *locale)
{
return strcmp(locale, "UTF-8") == 0
|| php_excel_locale_name_selects_utf8_decoder(locale)
|| php_excel_locale_name_selects_utf8_decoder(setlocale(LC_CTYPE, NULL));
}
/* Full state reset: bump every wrapper generation domain and drop the
* cached default date format. Use after libxl operations that free the
* internal format table (load, loadFile, loadInfo, loadInfoRaw, clear,
* __construct reuse). Reusing the stale FormatHandle from
* default_date_format after one of these would silently write cells with
* no format, corrupting AS_DATE-typed output. */
static inline int php_excel_book_reset_state(zval *book_zv) {
excel_book_object *bobj = php_excel_book_object_fetch_object(Z_OBJ_P(book_zv));
if (bobj->native_buffer_borrowed) {
php_error_docref(NULL, E_WARNING,
"Cannot reinitialize workbook while a native I/O buffer is in use");
return 0;
}
bobj->generation++;
bobj->sheet_generation++;
bobj->autofilter_generation++;
bobj->conditional_formatting_generation++;
bobj->default_date_format = NULL;
bobj->info_only = false;
return 1;
}
/* Reload contract: every load* path verifies into a scratch handle
* and adopts it only on success. A failed reload leaves the live BookHandle,
* all generation counters, and default_date_format untouched, so existing
* child wrappers keep working against the previous workbook. Generations
* bump (invalidating children) only via php_excel_book_commit_scratch().
* Children stash the raw BookHandle at creation, but the stale-generation
* check always runs before any dereference, so a committed swap cannot
* strand them on the released handle. */
/* Allocate a scratch book with the object's own flavor for one reload
* attempt. The stored per-book credentials (or the INI fallback, which is
* process-pinned) and the stored locale are applied so a committed swap
* preserves licensing and encoding behavior. Returns NULL on failure. */
static BookHandle php_excel_book_create_scratch(zval *object)
{
excel_book_object *bobj = Z_EXCEL_BOOK_OBJ_P(object);
BookHandle scratch = bobj->is_xlsx ? xlCreateXMLBook() : xlCreateBook();
if (!scratch) {
return NULL;
}
#if defined(HAVE_LIBXL_SETKEY)
if (bobj->license_name && bobj->license_key) {
xlBookSetKey(scratch, bobj->license_name, bobj->license_key);
} else if (EXCEL_G(ini_license_name) && EXCEL_G(ini_license_key)
&& *EXCEL_G(ini_license_name) && *EXCEL_G(ini_license_key)) {
xlBookSetKey(scratch, EXCEL_G(ini_license_name), EXCEL_G(ini_license_key));
}
#endif
if (bobj->locale) {
/* The live book accepted this string; re-apply it so the scratch
* decodes identically. A failure here is cosmetic next to the load
* itself, so it never fails the reload. */
xlBookSetLocale(scratch, bobj->locale);
}
return scratch;
}
/* Adopt a successfully-loaded scratch book: release the live handle, install
* the scratch, and invalidate children through the standard reset. Refuses
* while a native buffer is borrowed (same guard as
* php_excel_book_reset_state); the caller releases the scratch on refusal.
* Returns 1 on commit, 0 with a warning otherwise. */
static int php_excel_book_commit_scratch(zval *object, BookHandle scratch)
{
excel_book_object *bobj = Z_EXCEL_BOOK_OBJ_P(object);
bool utf8_decoder;
if (bobj->native_buffer_borrowed) {
php_error_docref(NULL, E_WARNING,
"Cannot reinitialize workbook while a native I/O buffer is in use");
return 0;
}
/* The scratch is a fresh libxl book, so its decoder is re-derived. The
* LC_CTYPE read must precede xlBookRelease(), which restores the LC_CTYPE
* that was in effect before the released book was created. */
utf8_decoder = php_excel_locale_name_selects_utf8_decoder(setlocale(LC_CTYPE, NULL));
bobj->rich_text_add_failed = false;
if (bobj->book) {
xlBookRelease(bobj->book);
}
bobj->book = scratch;
if (bobj->locale) {
/* The release above also undid the LC_CTYPE that replaying a
* non-UTF-8 locale on the scratch had set. */
xlBookSetLocale(scratch, bobj->locale);
utf8_decoder = php_excel_locale_selects_utf8_decoder(bobj->locale);
}
bobj->text_utf8_decoder = utf8_decoder;
/* Scratch carries the object's own flavor, credentials, and locale, so
* is_xlsx, license_*, and locale_* fields remain correct as-is. */
return php_excel_book_reset_state(object);
}
static int php_excel_book_commit_info_scratch(zval *object, BookHandle scratch)
{
if (!php_excel_book_commit_scratch(object, scratch)) {
return 0;
}
excel_book_object *bobj = Z_EXCEL_BOOK_OBJ_P(object);
bobj->info_only = true;
return 1;
}
/* Scratch-reload prologue: refuse an uninitialized book without exposing the
* live handle, then allocate the scratch. Warns and RETURN_FALSE on either
* failure (the stream-owned variant also releases the caller's buffer). */
#define BOOK_BEGIN_SCRATCH_RELOAD(scratch, object) \
{ \
excel_book_object *scratch_bobj = Z_EXCEL_BOOK_OBJ_P(object); \
if (!scratch_bobj->book) { \
php_error_docref(NULL, E_WARNING, "The book wasn't initialized"); \
RETURN_FALSE; \
} \
scratch = php_excel_book_create_scratch(object); \
if (!scratch) { \