-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp_crc_fast.cpp
More file actions
1267 lines (1077 loc) · 42.6 KB
/
php_crc_fast.cpp
File metadata and controls
1267 lines (1077 loc) · 42.6 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
/* crc_fast extension for PHP */
/* Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
extern "C" {
#include "php.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
}
#include "php_crc_fast.h"
#include "crc_fast_arginfo.h"
#include <string>
// Define htonll/ntohll for platforms that don't provide them
#if defined(_WIN32) || defined(_WIN64)
// Windows: winsock2.h provides htonll/ntohll in SDK 10.0.26100.0+
#include <winsock2.h>
// For older Windows SDKs that don't have htonll/ntohll, provide fallback
// We use different function names to avoid conflicts with newer SDKs
namespace {
inline uint64_t php_htonll_fallback(uint64_t x) {
return (((uint64_t)htonl((uint32_t)(x & 0xFFFFFFFF))) << 32) | htonl((uint32_t)(x >> 32));
}
inline uint64_t php_ntohll_fallback(uint64_t x) {
return (((uint64_t)ntohl((uint32_t)(x & 0xFFFFFFFF))) << 32) | ntohl((uint32_t)(x >> 32));
}
}
// Check if we have the native functions (SDK 10.0.26100.0+)
#if defined(NTDDI_VERSION) && defined(NTDDI_WIN10_NI) && (NTDDI_VERSION >= NTDDI_WIN10_NI)
// Use the native functions from winsock2.h
#define PHP_HTONLL(x) htonll(x)
#define PHP_NTOHLL(x) ntohll(x)
#else
// Use our fallback implementations
#define PHP_HTONLL(x) php_htonll_fallback(x)
#define PHP_NTOHLL(x) php_ntohll_fallback(x)
#endif
#else
// Unix-like platforms
#ifndef htonll
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint64_t htonll(uint64_t x) {
return (((uint64_t)htonl(x & 0xFFFFFFFF)) << 32) | htonl(x >> 32);
}
#define ntohll htonll
#else
#define htonll(x) (x)
#define ntohll(x) (x)
#endif
#endif
#define PHP_HTONLL(x) htonll(x)
#define PHP_NTOHLL(x) ntohll(x)
#endif
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
/* CrcFast\Digest class */
zend_class_entry *php_crc_fast_digest_ce;
static zend_object_handlers php_crc_fast_digest_object_handlers;
/* CrcFast\Params class */
zend_class_entry *php_crc_fast_params_ce;
static zend_object_handlers php_crc_fast_params_object_handlers;
/* Free the Digest object */
static void php_crc_fast_digest_free_obj(zend_object *object)
{
php_crc_fast_digest_obj *obj = php_crc_fast_digest_from_obj(object);
if (obj->digest) {
crc_fast_digest_free(obj->digest);
obj->digest = NULL;
}
zend_object_std_dtor(&obj->std);
}
/* Create a new Digest object */
static zend_object *php_crc_fast_digest_create_object(zend_class_entry *ce)
{
php_crc_fast_digest_obj *obj = (php_crc_fast_digest_obj*)ecalloc(1, sizeof(php_crc_fast_digest_obj) + zend_object_properties_size(ce));
zend_object_std_init(&obj->std, ce);
object_properties_init(&obj->std, ce);
obj->std.handlers = &php_crc_fast_digest_object_handlers;
obj->digest = NULL;
obj->algorithm = 0; // Explicitly initialize to 0 to prevent garbage values
obj->is_custom = false; // Initialize to false
memset(&obj->custom_params, 0, sizeof(CrcFastParams)); // Initialize custom params to zero
return &obj->std;
}
/* Free the Params object */
static void php_crc_fast_params_free_obj(zend_object *object)
{
php_crc_fast_params_obj *obj = php_crc_fast_params_from_obj(object);
// Free the allocated keys storage if it exists
if (obj->keys_storage) {
efree(obj->keys_storage);
obj->keys_storage = NULL;
}
zend_object_std_dtor(&obj->std);
}
/* Create a new Params object */
static zend_object *php_crc_fast_params_create_object(zend_class_entry *ce)
{
php_crc_fast_params_obj *obj = (php_crc_fast_params_obj*)ecalloc(1, sizeof(php_crc_fast_params_obj) + zend_object_properties_size(ce));
zend_object_std_init(&obj->std, ce);
object_properties_init(&obj->std, ce);
obj->std.handlers = &php_crc_fast_params_object_handlers;
// Initialize the CrcFastParams struct to zero
memset(&obj->params, 0, sizeof(CrcFastParams));
obj->keys_storage = NULL;
return &obj->std;
}
/* Helper function to format checksum output */
static inline void php_crc_fast_format_result(INTERNAL_FUNCTION_PARAMETERS, zend_long algorithm, uint64_t result, zend_bool binary, bool is_custom = false, uint8_t custom_width = 0)
{
bool is_32bit;
if (is_custom) {
// For custom parameters, use the width from the parameters
is_32bit = (custom_width == 32);
} else {
// For predefined algorithms, determine width based on algorithm constant
is_32bit = (algorithm <= PHP_CRC_FAST_CRC32_XFER);
}
if (binary) {
// For binary output, return the raw bytes
if (is_32bit) {
// 32-bit CRC
uint32_t result32 = (uint32_t)result;
result32 = htonl(result32);
RETURN_STRINGL((char*)&result32, sizeof(result32));
} else {
// 64-bit CRC
result = PHP_HTONLL(result);
RETURN_STRINGL((char*)&result, sizeof(result));
}
} else {
if (is_32bit) {
// 32-bit CRC
char checksum_str[9]; // 8 hex digits + null terminator
snprintf(checksum_str, sizeof(checksum_str), "%08x", (uint32_t)result);
RETURN_STRING(checksum_str);
} else {
// 64-bit CRC
char checksum_str[17]; // 16 hex digits + null terminator
snprintf(checksum_str, sizeof(checksum_str), "%016" PRIx64, result);
RETURN_STRING(checksum_str);
}
}
}
/* Helper function to convert PHP algorithm int to FFICrcAlgorithm enum */
static inline CrcFastAlgorithm php_crc_fast_get_algorithm(zend_long algo) {
switch (algo) {
case PHP_CRC_FAST_CRC32_AIXM: return CrcFastAlgorithm::Crc32Aixm;
case PHP_CRC_FAST_CRC32_AUTOSAR: return CrcFastAlgorithm::Crc32Autosar;
case PHP_CRC_FAST_CRC32_BASE91D: return CrcFastAlgorithm::Crc32Base91D;
case PHP_CRC_FAST_CRC32_BZIP2: return CrcFastAlgorithm::Crc32Bzip2;
case PHP_CRC_FAST_CRC32_CDROM_EDC: return CrcFastAlgorithm::Crc32CdRomEdc;
case PHP_CRC_FAST_CRC32_CKSUM: return CrcFastAlgorithm::Crc32Cksum;
case PHP_CRC_FAST_CRC32_ISCSI: return CrcFastAlgorithm::Crc32Iscsi;
case PHP_CRC_FAST_CRC32_ISO_HDLC: return CrcFastAlgorithm::Crc32IsoHdlc;
case PHP_CRC_FAST_CRC32_JAMCRC: return CrcFastAlgorithm::Crc32Jamcrc;
case PHP_CRC_FAST_CRC32_MEF: return CrcFastAlgorithm::Crc32Mef;
case PHP_CRC_FAST_CRC32_MPEG2: return CrcFastAlgorithm::Crc32Mpeg2;
case PHP_CRC_FAST_CRC32_XFER: return CrcFastAlgorithm::Crc32Xfer;
case PHP_CRC_FAST_CRC64_ECMA182: return CrcFastAlgorithm::Crc64Ecma182;
case PHP_CRC_FAST_CRC64_GO_ISO: return CrcFastAlgorithm::Crc64GoIso;
case PHP_CRC_FAST_CRC64_MS: return CrcFastAlgorithm::Crc64Ms;
case PHP_CRC_FAST_CRC64_NVME: return CrcFastAlgorithm::Crc64Nvme;
case PHP_CRC_FAST_CRC64_REDIS: return CrcFastAlgorithm::Crc64Redis;
case PHP_CRC_FAST_CRC64_WE: return CrcFastAlgorithm::Crc64We;
case PHP_CRC_FAST_CRC64_XZ: return CrcFastAlgorithm::Crc64Xz;
// this is a special flower, just to handle PHP's `hash('crc32')` operation,
// which is actually byte-reversed CRC-32/BZIP2
case PHP_CRC_FAST_CRC32_PHP: return CrcFastAlgorithm::Crc32Bzip2;
default:
zend_throw_exception_ex(zend_ce_exception, 0,
"Invalid algorithm constant %lld. Use CrcFast\\get_supported_algorithms() to see valid values", algo);
return CrcFastAlgorithm::Crc32IsoHdlc; // Fallback (never reached due to exception)
}
}
/* Helper function to reverse bytes for CRC-32/PHP compatibility which matches PHP's `hash('crc32')` output */
static inline uint64_t php_crc_fast_reverse_bytes_if_needed(uint64_t result, zend_long algorithm)
{
// Only reverse bytes for PHP_CRC_FAST_CRC32_PHP
if (algorithm == PHP_CRC_FAST_CRC32_PHP) {
// For CRC32, we only need to reverse the lower 32 bits
uint32_t result32 = (uint32_t)result;
result32 = ((result32 & 0xFF) << 24) |
((result32 & 0xFF00) << 8) |
((result32 & 0xFF0000) >> 8) |
((result32 & 0xFF000000) >> 24);
return result32;
}
return result;
}
/* Helper function to detect parameter type and extract CrcFastParams if needed */
static inline bool php_crc_fast_get_params_from_zval(zval *algorithm_zval, zend_long *algorithm_out, CrcFastParams *params_out)
{
if (Z_TYPE_P(algorithm_zval) == IS_LONG) {
// It's an integer algorithm constant
*algorithm_out = Z_LVAL_P(algorithm_zval);
return false; // Not custom parameters
} else if (Z_TYPE_P(algorithm_zval) == IS_OBJECT &&
instanceof_function(Z_OBJCE_P(algorithm_zval), php_crc_fast_params_ce)) {
// It's a CrcFast\Params object
php_crc_fast_params_obj *params_obj = Z_CRC_FAST_PARAMS_P(algorithm_zval);
if (!params_obj) {
zend_throw_exception(zend_ce_exception, "Invalid CrcFast\\Params object", 0);
return false;
}
*params_out = params_obj->params;
*algorithm_out = 0; // Not used for custom parameters
return true; // Custom parameters
} else {
// Invalid type - provide more descriptive error message
const char *type_name = zend_get_type_by_const(Z_TYPE_P(algorithm_zval));
zend_throw_exception_ex(zend_ce_exception, 0,
"Algorithm parameter must be an integer constant or CrcFast\\Params object, %s given", type_name);
return false;
}
}
/* {{{ CrcFast\crc32(string $data): int */
PHP_FUNCTION(CrcFast_crc32)
{
char *data;
size_t data_len;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(data, data_len)
ZEND_PARSE_PARAMETERS_END();
uint64_t result = crc_fast_checksum(CrcFastAlgorithm::Crc32IsoHdlc, data, data_len);
// Return as integer
RETURN_LONG((zend_long)result);
}
/* }}} */
/* {{{ CrcFast\hash(int|CrcFast\Params $algorithm, string $data, bool $binary = false): string */
PHP_FUNCTION(CrcFast_hash)
{
zval *algorithm_zval;
char *data;
size_t data_len;
zend_bool binary = 0;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_ZVAL(algorithm_zval)
Z_PARAM_STRING(data, data_len)
Z_PARAM_BOOL(binary)
ZEND_PARSE_PARAMETERS_END();
// Validate data parameter
if (!data) {
zend_throw_exception(zend_ce_exception, "Data parameter cannot be null", 0);
return;
}
zend_long algorithm;
CrcFastParams custom_params;
bool is_custom = php_crc_fast_get_params_from_zval(algorithm_zval, &algorithm, &custom_params);
if (EG(exception)) {
return; // Exception was thrown by helper function
}
uint64_t result;
if (is_custom) {
// Use custom parameters - handle potential C library errors
try {
result = crc_fast_checksum_with_params(custom_params, data, data_len);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to compute CRC checksum with custom parameters", 0);
return;
}
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, result, binary, true, custom_params.width);
} else {
// Use predefined algorithm - handle potential C library errors
CrcFastAlgorithm algo = php_crc_fast_get_algorithm(algorithm);
if (EG(exception)) {
return; // Exception was thrown by get_algorithm
}
try {
result = crc_fast_checksum(algo, data, data_len);
} catch (...) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Failed to compute CRC checksum for algorithm %lld", algorithm);
return;
}
// Apply byte reversal if needed
result = php_crc_fast_reverse_bytes_if_needed(result, algorithm);
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, algorithm, result, binary);
}
}
/* }}} */
/* {{{ CrcFast\hash_file(int|CrcFast\Params $algorithm, string $filename, bool $binary = false, ?int $chunk_size = null): string */
PHP_FUNCTION(CrcFast_hash_file)
{
zval *algorithm_zval;
char *filename;
size_t filename_len;
zend_bool binary = 0;
zval *chunk_size_zval = NULL;
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_ZVAL(algorithm_zval)
Z_PARAM_STRING(filename, filename_len)
Z_PARAM_BOOL(binary)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL_OR_NULL(chunk_size_zval)
ZEND_PARSE_PARAMETERS_END();
// Validate filename parameter
if (!filename || filename_len == 0) {
zend_throw_exception(zend_ce_exception, "Filename cannot be empty", 0);
return;
}
// Check if file exists and is readable
if (php_check_open_basedir(filename)) {
zend_throw_exception_ex(zend_ce_exception, 0, "File '%s' is not within the allowed path(s)", filename);
return;
}
zend_long algorithm;
CrcFastParams custom_params;
bool is_custom = php_crc_fast_get_params_from_zval(algorithm_zval, &algorithm, &custom_params);
if (EG(exception)) {
return; // Exception was thrown by helper function
}
uint64_t result;
if (is_custom) {
// Use custom parameters - handle potential C library errors
try {
result = crc_fast_checksum_file_with_params(custom_params, (const uint8_t*)filename, filename_len);
} catch (...) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Failed to compute CRC checksum for file '%s' with custom parameters", filename);
return;
}
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, result, binary, true, custom_params.width);
} else {
// Use predefined algorithm - handle potential C library errors
CrcFastAlgorithm algo = php_crc_fast_get_algorithm(algorithm);
if (EG(exception)) {
return; // Exception was thrown by get_algorithm
}
try {
result = crc_fast_checksum_file(algo, (const uint8_t*)filename, filename_len);
} catch (...) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Failed to compute CRC checksum for file '%s' with algorithm %lld", filename, algorithm);
return;
}
// Apply byte reversal if needed
result = php_crc_fast_reverse_bytes_if_needed(result, algorithm);
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, algorithm, result, binary);
}
}
/* }}} */
/* {{{ CrcFast\get_supported_algorithms(): array */
PHP_FUNCTION(CrcFast_get_supported_algorithms)
{
ZEND_PARSE_PARAMETERS_NONE();
array_init(return_value);
add_assoc_long(return_value, "CRC-32/AIXM", PHP_CRC_FAST_CRC32_AIXM);
add_assoc_long(return_value, "CRC32_AUTOSAR", PHP_CRC_FAST_CRC32_AUTOSAR);
add_assoc_long(return_value, "CRC-32/BASE-91-D", PHP_CRC_FAST_CRC32_BASE91D);
add_assoc_long(return_value, "CRC-32/BZIP2", PHP_CRC_FAST_CRC32_BZIP2);
add_assoc_long(return_value, "CRC-32/CD-ROM-EDC", PHP_CRC_FAST_CRC32_CDROM_EDC);
add_assoc_long(return_value, "CRC-32/CKSUM", PHP_CRC_FAST_CRC32_CKSUM);
add_assoc_long(return_value, "CRC-32/ISCSI", PHP_CRC_FAST_CRC32_ISCSI);
add_assoc_long(return_value, "CRC-32/ISO_HDLC", PHP_CRC_FAST_CRC32_ISO_HDLC);
add_assoc_long(return_value, "CRC-32/JAMCRC", PHP_CRC_FAST_CRC32_JAMCRC);
add_assoc_long(return_value, "CRC-32/MEF", PHP_CRC_FAST_CRC32_MEF);
add_assoc_long(return_value, "CRC-32/MPEG-2", PHP_CRC_FAST_CRC32_MPEG2);
add_assoc_long(return_value, "CRC-32/XFER", PHP_CRC_FAST_CRC32_XFER);
add_assoc_long(return_value, "CRC-64/ECMA-182", PHP_CRC_FAST_CRC64_ECMA182);
add_assoc_long(return_value, "CRC-64/GO-ISO", PHP_CRC_FAST_CRC64_GO_ISO);
add_assoc_long(return_value, "CRC-64/MS", PHP_CRC_FAST_CRC64_MS);
add_assoc_long(return_value, "CRC-64/NVME", PHP_CRC_FAST_CRC64_NVME);
add_assoc_long(return_value, "CRC-64/REDIS", PHP_CRC_FAST_CRC64_REDIS);
add_assoc_long(return_value, "CRC-64/WE", PHP_CRC_FAST_CRC64_WE);
add_assoc_long(return_value, "CRC-64/XZ", PHP_CRC_FAST_CRC64_XZ);
}
/* }}} */
/* {{{ CrcFast\combine(int|CrcFast\Params $algorithm, string $checksum1, string $checksum2, int $length2, bool $binary = false): string */
PHP_FUNCTION(CrcFast_combine)
{
zval *algorithm_zval;
char *checksum1, *checksum2;
size_t checksum1_len, checksum2_len;
zend_long length2;
zend_bool binary = 0;
ZEND_PARSE_PARAMETERS_START(5, 5)
Z_PARAM_ZVAL(algorithm_zval)
Z_PARAM_STRING(checksum1, checksum1_len)
Z_PARAM_STRING(checksum2, checksum2_len)
Z_PARAM_LONG(length2)
Z_PARAM_BOOL(binary)
ZEND_PARSE_PARAMETERS_END();
// Validate parameters
if (!checksum1 || !checksum2) {
zend_throw_exception(zend_ce_exception, "Checksum parameters cannot be null", 0);
return;
}
if (length2 < 0) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Length parameter must be non-negative, got %lld", length2);
return;
}
zend_long algorithm;
CrcFastParams custom_params;
bool is_custom = php_crc_fast_get_params_from_zval(algorithm_zval, &algorithm, &custom_params);
if (EG(exception)) {
return; // Exception was thrown by helper function
}
uint64_t cs1 = 0, cs2 = 0;
// Determine if we're dealing with 32-bit or 64-bit CRC
bool is_crc32;
if (is_custom) {
is_crc32 = (custom_params.width == 32);
} else {
is_crc32 = (algorithm <= PHP_CRC_FAST_CRC32_XFER);
}
size_t expected_binary_size = is_crc32 ? 4 : 8; // 4 bytes for CRC32, 8 bytes for CRC64
size_t expected_hex_size = is_crc32 ? 8 : 16; // 8 hex chars for CRC32, 16 for CRC64
// Process checksum1
if (checksum1_len == expected_binary_size) {
// Binary input
if (is_crc32) {
cs1 = ntohl(*((uint32_t*)checksum1));
} else {
cs1 = PHP_NTOHLL(*((uint64_t*)checksum1));
}
} else if (checksum1_len == expected_hex_size) {
// Hex input
if (is_crc32) {
uint32_t value = 0;
if (sscanf(checksum1, "%8x", &value) == 1) {
cs1 = value;
} else {
php_error_docref(NULL, E_WARNING, "Invalid hexadecimal input for checksum1");
RETURN_FALSE;
}
} else {
// For 64-bit values, we need to parse it manually because sscanf might not
// handle uint64_t correctly on all platforms
cs1 = 0;
for (size_t i = 0; i < 16; i++) {
char c = checksum1[i];
uint64_t digit;
if (c >= '0' && c <= '9') {
digit = c - '0';
} else if (c >= 'a' && c <= 'f') {
digit = c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
digit = c - 'A' + 10;
} else {
php_error_docref(NULL, E_WARNING, "Invalid hexadecimal character in checksum1");
RETURN_FALSE;
}
cs1 = (cs1 << 4) | digit;
}
}
} else {
php_error_docref(NULL, E_WARNING, "Invalid checksum1 length (expected %zu bytes for binary or %zu chars for hex)",
expected_binary_size, expected_hex_size);
RETURN_FALSE;
}
// Process checksum2
if (checksum2_len == expected_binary_size) {
// Binary input
if (is_crc32) {
cs2 = ntohl(*((uint32_t*)checksum2));
} else {
cs2 = PHP_NTOHLL(*((uint64_t*)checksum2));
}
} else if (checksum2_len == expected_hex_size) {
// Hex input
if (is_crc32) {
uint32_t value = 0;
if (sscanf(checksum2, "%8x", &value) == 1) {
cs2 = value;
} else {
php_error_docref(NULL, E_WARNING, "Invalid hexadecimal input for checksum2");
RETURN_FALSE;
}
} else {
// For 64-bit values, parse it manually
cs2 = 0;
for (size_t i = 0; i < 16; i++) {
char c = checksum2[i];
uint64_t digit;
if (c >= '0' && c <= '9') {
digit = c - '0';
} else if (c >= 'a' && c <= 'f') {
digit = c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
digit = c - 'A' + 10;
} else {
php_error_docref(NULL, E_WARNING, "Invalid hexadecimal character in checksum2");
RETURN_FALSE;
}
cs2 = (cs2 << 4) | digit;
}
}
} else {
php_error_docref(NULL, E_WARNING, "Invalid checksum2 length (expected %zu bytes for binary or %zu chars for hex)",
expected_binary_size, expected_hex_size);
RETURN_FALSE;
}
uint64_t result;
if (is_custom) {
// Use custom parameters - handle potential C library errors
try {
result = crc_fast_checksum_combine_with_params(custom_params, cs1, cs2, length2);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to combine CRC checksums with custom parameters", 0);
return;
}
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, result, binary, true, custom_params.width);
} else {
// Use predefined algorithm - handle potential C library errors
CrcFastAlgorithm algo = php_crc_fast_get_algorithm(algorithm);
if (EG(exception)) {
return; // Exception was thrown by get_algorithm
}
try {
result = crc_fast_checksum_combine(algo, cs1, cs2, length2);
} catch (...) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Failed to combine CRC checksums for algorithm %lld", algorithm);
return;
}
// Apply byte reversal if needed
result = php_crc_fast_reverse_bytes_if_needed(result, algorithm);
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, algorithm, result, binary);
}
}
/* }}} */
/* {{{ CrcFast\Digest::__construct(int|CrcFast\Params $algorithm) */
PHP_METHOD(CrcFast_Digest, __construct)
{
php_crc_fast_digest_obj *obj = Z_CRC_FAST_DIGEST_P(getThis());
zval *algorithm_zval;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(algorithm_zval)
ZEND_PARSE_PARAMETERS_END();
if (!obj) {
zend_throw_exception(zend_ce_exception, "Failed to initialize Digest object", 0);
return;
}
// Free previous digest if it exists
if (obj->digest) {
crc_fast_digest_free(obj->digest);
obj->digest = NULL;
}
zend_long algorithm;
CrcFastParams custom_params;
bool is_custom = php_crc_fast_get_params_from_zval(algorithm_zval, &algorithm, &custom_params);
if (EG(exception)) {
return; // Exception was thrown by helper function
}
if (is_custom) {
// Use custom parameters - handle potential C library errors
try {
obj->digest = crc_fast_digest_new_with_params(custom_params);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to create digest with custom parameters", 0);
return;
}
if (!obj->digest) {
zend_throw_exception(zend_ce_exception, "C library failed to create digest with custom parameters", 0);
return;
}
obj->is_custom = true;
obj->custom_params = custom_params;
obj->algorithm = 0; // Not used for custom parameters
} else {
// Use predefined algorithm - handle potential C library errors
CrcFastAlgorithm algo = php_crc_fast_get_algorithm(algorithm);
if (EG(exception)) {
return; // Exception was thrown by get_algorithm
}
try {
obj->digest = crc_fast_digest_new(algo);
} catch (...) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Failed to create digest for algorithm %lld", algorithm);
return;
}
if (!obj->digest) {
zend_throw_exception_ex(zend_ce_exception, 0,
"C library failed to create digest for algorithm %lld", algorithm);
return;
}
obj->is_custom = false;
obj->algorithm = algorithm;
// Initialize custom_params to zero for safety
memset(&obj->custom_params, 0, sizeof(CrcFastParams));
}
}
/* }}} */
/* {{{ CrcFast\Digest::update(string $data): void */
PHP_METHOD(CrcFast_Digest, update)
{
php_crc_fast_digest_obj *obj = Z_CRC_FAST_DIGEST_P(getThis());
char *data;
size_t data_len;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(data, data_len)
ZEND_PARSE_PARAMETERS_END();
if (!obj) {
zend_throw_exception(zend_ce_exception, "Invalid Digest object", 0);
return;
}
if (!obj->digest) {
zend_throw_exception(zend_ce_exception, "Digest object not initialized. Call constructor first", 0);
return;
}
// Validate data parameter
if (!data) {
zend_throw_exception(zend_ce_exception, "Data parameter cannot be null", 0);
return;
}
// Handle potential C library errors
try {
crc_fast_digest_update(obj->digest, data, data_len);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to update digest with data", 0);
return;
}
// Return $this for method chaining
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ CrcFast\Digest::finalize(bool $binary = false): string */
PHP_METHOD(CrcFast_Digest, finalize)
{
php_crc_fast_digest_obj *obj = Z_CRC_FAST_DIGEST_P(getThis());
zend_bool binary = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(binary)
ZEND_PARSE_PARAMETERS_END();
if (!obj) {
zend_throw_exception(zend_ce_exception, "Invalid Digest object", 0);
return;
}
if (!obj->digest) {
zend_throw_exception(zend_ce_exception, "Digest object not initialized. Call constructor first", 0);
return;
}
uint64_t result;
try {
result = crc_fast_digest_finalize(obj->digest);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to finalize digest", 0);
return;
}
if (obj->is_custom) {
// Use custom parameter formatting
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, result, binary, true, obj->custom_params.width);
} else {
// Apply byte reversal if needed for predefined algorithms
result = php_crc_fast_reverse_bytes_if_needed(result, obj->algorithm);
// Format and return the result using predefined algorithm formatting
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, obj->algorithm, result, binary);
}
}
/* }}} */
/* {{{ CrcFast\Digest::reset(): void */
PHP_METHOD(CrcFast_Digest, reset)
{
php_crc_fast_digest_obj *obj = Z_CRC_FAST_DIGEST_P(getThis());
ZEND_PARSE_PARAMETERS_NONE();
if (!obj) {
zend_throw_exception(zend_ce_exception, "Invalid Digest object", 0);
return;
}
if (!obj->digest) {
zend_throw_exception(zend_ce_exception, "Digest object not initialized. Call constructor first", 0);
return;
}
try {
crc_fast_digest_reset(obj->digest);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to reset digest", 0);
return;
}
// Return $this for method chaining
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ CrcFast\Digest::finalizeReset(bool $binary = false): string */
PHP_METHOD(CrcFast_Digest, finalizeReset)
{
php_crc_fast_digest_obj *obj = Z_CRC_FAST_DIGEST_P(getThis());
zend_bool binary = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(binary)
ZEND_PARSE_PARAMETERS_END();
if (!obj) {
zend_throw_exception(zend_ce_exception, "Invalid Digest object", 0);
return;
}
if (!obj->digest) {
zend_throw_exception(zend_ce_exception, "Digest object not initialized. Call constructor first", 0);
return;
}
uint64_t result;
try {
result = crc_fast_digest_finalize_reset(obj->digest);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to finalize and reset digest", 0);
return;
}
if (obj->is_custom) {
// Use custom parameter formatting
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, result, binary, true, obj->custom_params.width);
} else {
// Apply byte reversal if needed for predefined algorithms
result = php_crc_fast_reverse_bytes_if_needed(result, obj->algorithm);
php_crc_fast_format_result(INTERNAL_FUNCTION_PARAM_PASSTHRU, obj->algorithm, result, binary);
}
}
/* }}} */
/* {{{ CrcFast\Digest::combine(CrcFast\Digest $other): void */
PHP_METHOD(CrcFast_Digest, combine)
{
php_crc_fast_digest_obj *obj = Z_CRC_FAST_DIGEST_P(getThis());
zval *other_zval;
php_crc_fast_digest_obj *other_obj;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT_OF_CLASS(other_zval, php_crc_fast_digest_ce)
ZEND_PARSE_PARAMETERS_END();
if (!obj) {
zend_throw_exception(zend_ce_exception, "Invalid Digest object", 0);
return;
}
if (!obj->digest) {
zend_throw_exception(zend_ce_exception, "Digest object not initialized. Call constructor first", 0);
return;
}
other_obj = Z_CRC_FAST_DIGEST_P(other_zval);
if (!other_obj) {
zend_throw_exception(zend_ce_exception, "Invalid other Digest object", 0);
return;
}
if (!other_obj->digest) {
zend_throw_exception(zend_ce_exception, "Other digest object not initialized. Call constructor first", 0);
return;
}
try {
crc_fast_digest_combine(obj->digest, other_obj->digest);
} catch (...) {
zend_throw_exception(zend_ce_exception, "Failed to combine digest objects", 0);
return;
}
// Return $this for method chaining
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/* {{{ CrcFast\Params::__construct(int $width, int $poly, int $init, bool $refin, bool $refout, int $xorout, int $check, ?array $keys = null) */
PHP_METHOD(CrcFast_Params, __construct)
{
php_crc_fast_params_obj *obj = Z_CRC_FAST_PARAMS_P(getThis());
zend_long width, poly, init, xorout, check;
zend_bool refin, refout;
zval *keys_array = NULL;
ZEND_PARSE_PARAMETERS_START(7, 8)
Z_PARAM_LONG(width)
Z_PARAM_LONG(poly)
Z_PARAM_LONG(init)
Z_PARAM_BOOL(refin)
Z_PARAM_BOOL(refout)
Z_PARAM_LONG(xorout)
Z_PARAM_LONG(check)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_NULL(keys_array)
ZEND_PARSE_PARAMETERS_END();
// Validate width - only 32 and 64 are supported
if (width != 32 && width != 64) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Invalid width %lld. Only 32 and 64 bit widths are supported", width);
return;
}
// Validate parameters are not negative
if (poly < 0) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Polynomial value %lld cannot be negative", poly);
return;
}
if (init < 0) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Init value %lld cannot be negative", init);
return;
}
if (xorout < 0) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Xorout value %lld cannot be negative", xorout);
return;
}
if (check < 0) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Check value %lld cannot be negative", check);
return;
}
// Validate polynomial fits within width
uint64_t max_poly = (width == 32) ? 0xFFFFFFFFULL : 0xFFFFFFFFFFFFFFFFULL;
if ((uint64_t)poly > max_poly) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Polynomial 0x%llx exceeds maximum value for %lld-bit width", poly, width);
return;
}
// Validate init value fits within width
uint64_t max_init = (width == 32) ? 0xFFFFFFFFULL : 0xFFFFFFFFFFFFFFFFULL;
if ((uint64_t)init > max_init) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Init value 0x%llx exceeds maximum value for %lld-bit width", init, width);
return;
}
// Validate xorout value fits within width
uint64_t max_xorout = (width == 32) ? 0xFFFFFFFFULL : 0xFFFFFFFFFFFFFFFFULL;
if ((uint64_t)xorout > max_xorout) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Xorout value 0x%llx exceeds maximum value for %lld-bit width", xorout, width);
return;
}
// Validate check value fits within width
uint64_t max_check = (width == 32) ? 0xFFFFFFFFULL : 0xFFFFFFFFFFFFFFFFULL;
if ((uint64_t)check > max_check) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Check value 0x%llx exceeds maximum value for %lld-bit width", check, width);
return;
}
// Set up the CrcFastParams struct
obj->params.algorithm = (width == 32) ? CrcFastAlgorithm::Crc32Custom : CrcFastAlgorithm::Crc64Custom;
obj->params.width = (uint8_t)width;
obj->params.poly = (uint64_t)poly;
obj->params.init = (uint64_t)init;
obj->params.refin = refin;
obj->params.refout = refout;
obj->params.xorout = (uint64_t)xorout;
obj->params.check = (uint64_t)check;
// Allocate memory for keys array (23 elements)
obj->keys_storage = (uint64_t*)emalloc(23 * sizeof(uint64_t));
obj->params.key_count = 23;
obj->params.keys = obj->keys_storage;
// Handle keys parameter
if (keys_array && Z_TYPE_P(keys_array) == IS_ARRAY) {
// Validate keys array has exactly 23 elements
if (zend_hash_num_elements(Z_ARRVAL_P(keys_array)) != 23) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Keys array must contain exactly 23 elements, got %d",
zend_hash_num_elements(Z_ARRVAL_P(keys_array)));
return;
}
// Copy keys from PHP array to C array
zval *key_val;
int i = 0;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys_array), key_val) {
if (Z_TYPE_P(key_val) != IS_LONG) {
zend_throw_exception_ex(zend_ce_exception, 0,
"All keys must be integers, element %d is not an integer", i);
return;
}
zend_long key_value = Z_LVAL_P(key_val);
if (key_value < 0) {
zend_throw_exception_ex(zend_ce_exception, 0,
"Key values cannot be negative, element %d has value %lld", i, key_value);
return;