-
Notifications
You must be signed in to change notification settings - Fork 3
/
K2hash.c
1735 lines (1633 loc) · 56 KB
/
K2hash.c
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
/*
* K2HASH PHP Extension library
*
* Copyright 2015 Yahoo Japan Corporation.
*
* K2HASH is key-valuew store base libraries.
* K2HASH is made for the purpose of the construction of
* original KVS system and the offer of the library.
* The characteristic is this KVS library which Key can
* layer. And can support multi-processing and multi-thread,
* and is provided safely as available KVS.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* AUTHOR: Takeshi Nakatani
* CREATE: Tue, Feb 22 2022
* REVISION:
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
// cppcheck-suppress missingInclude
#include "php.h"
// cppcheck-suppress missingInclude
#include "ext/standard/info.h"
#include "php_k2hash.h"
#include "k2hash_arginfo.h"
extern php_stream* k2hpx_da_open(k2h_h handle, const char* key, const char* mode STREAMS_DC);
extern zend_class_entry *php_k2hqueue_ce;
extern zend_class_entry *php_k2hkeyqueue_ce;
extern zend_class_entry *php_k2hiterator_ce;
/* {{{ Class K2hash */
zend_class_entry * php_k2hash_ce = NULL;
static zend_object_handlers k2hash_object_handlers;
/* {{{ Methods */
/* {{{ proto bool create(string filepath[, int maskbitcnt = K2H_DEF_MASK_BIT[, int cmaskbitcnt = K2H_DEF_CMASK_BIT[, int maxelementcnt = K2H_DEF_ELEMENT[, int pagesize = K2H_DEF_PAGESIZE]]]])
K2hash::create method */
PHP_METHOD(K2hash, create)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_long maskbitcnt = K2H_VAL_MASK_BIT;
zend_long cmaskbitcnt = K2H_VAL_CMASK_BIT;
zend_long maxelementcnt = K2H_VAL_ELEMENT;
zend_long pagesize = K2H_VAL_PAGESIZE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|llll", &filepath, &filepath_len, &maskbitcnt, &cmaskbitcnt, &maxelementcnt, &pagesize) == FAILURE) {
return;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_ERROR, "K2hash::create: filepath is empty.");
RETURN_FALSE;
}
// create file
if(!k2h_create(filepath, (int)maskbitcnt, (int)cmaskbitcnt, (int)maxelementcnt, (size_t)pagesize)) {
php_error_docref(NULL, E_NOTICE, "K2hash::create: failed to create k2hash file.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} create */
/* {{{ proto bool open(string filepath, bool readonly[, bool removefile = false[, bool fullmap = true[, int maskbitcnt = K2H_DEF_MASK_BIT[, int cmaskbitcnt = K2H_DEF_CMASK_BIT[, int maxelementcnt = K2H_DEF_ELEMENT[, int pagesize = K2H_DEF_PAGESIZE]]]]]])
K2hash::open method */
PHP_METHOD(K2hash, open)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_bool readonly = 0;
zend_bool removefile = false;
zend_bool fullmap = true;
zend_long maskbitcnt = K2H_VAL_MASK_BIT;
zend_long cmaskbitcnt = K2H_VAL_CMASK_BIT;
zend_long maxelementcnt = K2H_VAL_ELEMENT;
zend_long pagesize = K2H_VAL_PAGESIZE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sb|bbllll", &filepath, &filepath_len, &readonly, &removefile, &fullmap, &maskbitcnt, &cmaskbitcnt, &maxelementcnt, &pagesize) == FAILURE) {
return;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_ERROR, "K2hash::open: filepath is empty.");
RETURN_FALSE;
}
// open new handle
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_newhandle = intern->handle;
if (!res_newhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::open: custom object initialize error.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
*res_newhandle = k2h_open(filepath, 0 != readonly ? true : false, 0 != removefile ? true : false, 0 != fullmap ? true : false, (int)maskbitcnt, (int)cmaskbitcnt, (int)maxelementcnt, (size_t)pagesize);
// cppcheck-suppress nullPointerRedundantCheck
if(K2H_INVALID_HANDLE == *res_newhandle) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} open */
/* {{{ proto bool openRW(string filepath[, bool fullmap = true[, int maskbitcnt = K2H_DEF_MASK_BIT[, int cmaskbitcnt = K2H_DEF_CMASK_BIT[, int maxelementcnt = K2H_DEF_ELEMENT[, int pagesize = K2H_DEF_PAGESIZE]]]]])
K2hash::openRW method */
PHP_METHOD(K2hash, openRW)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_bool fullmap = true;
zend_long maskbitcnt = K2H_VAL_MASK_BIT;
zend_long cmaskbitcnt = K2H_VAL_CMASK_BIT;
zend_long maxelementcnt = K2H_VAL_ELEMENT;
zend_long pagesize = K2H_VAL_PAGESIZE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bllll", &filepath, &filepath_len, &fullmap, &maskbitcnt, &cmaskbitcnt, &maxelementcnt, &pagesize) == FAILURE) {
return;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_ERROR, "K2hash::openRW: filepath is empty.");
RETURN_FALSE;
}
// open new handle
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_newhandle = intern->handle;
if (!res_newhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::open: custom object initialize error.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
*res_newhandle = k2h_open_rw(filepath, 0 != fullmap ? true : false, (int)maskbitcnt, (int)cmaskbitcnt, (int)maxelementcnt, (size_t)pagesize);
// cppcheck-suppress nullPointerRedundantCheck
if(K2H_INVALID_HANDLE == *res_newhandle) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} openRW */
/* {{{ proto bool openRO(string filepath[, bool fullmap = true[, int maskbitcnt = K2H_DEF_MASK_BIT[, int cmaskbitcnt = K2H_DEF_CMASK_BIT[, int maxelementcnt = K2H_DEF_ELEMENT[, int pagesize = K2H_DEF_PAGESIZE]]]]])
K2hash::openRO method */
PHP_METHOD(K2hash, openRO)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_bool fullmap = true;
zend_long maskbitcnt = K2H_VAL_MASK_BIT;
zend_long cmaskbitcnt = K2H_VAL_CMASK_BIT;
zend_long maxelementcnt = K2H_VAL_ELEMENT;
zend_long pagesize = K2H_VAL_PAGESIZE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bllll", &filepath, &filepath_len, &fullmap, &maskbitcnt, &cmaskbitcnt, &maxelementcnt, &pagesize) == FAILURE) {
return;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_ERROR, "K2hash::openRO: filepath is empty.");
RETURN_FALSE;
}
// open new handle
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_newhandle = intern->handle;
if (!res_newhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::open: custom object initialize error.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
*res_newhandle = k2h_open_ro(filepath, 0 != fullmap ? true : false, (int)maskbitcnt, (int)cmaskbitcnt, (int)maxelementcnt, (size_t)pagesize);
// cppcheck-suppress nullPointerRedundantCheck
if(K2H_INVALID_HANDLE == *res_newhandle) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} openRO */
/* {{{ proto bool openTempfile(string filepath[, bool fullmap = true[, int maskbitcnt = K2H_DEF_MASK_BIT[, int cmaskbitcnt = K2H_DEF_CMASK_BIT[, int maxelementcnt = K2H_DEF_ELEMENT[, int pagesize = K2H_DEF_PAGESIZE]]]]])
K2hash::openTempfile method */
PHP_METHOD(K2hash, openTempfile)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_bool fullmap = true;
zend_long maskbitcnt = K2H_VAL_MASK_BIT;
zend_long cmaskbitcnt = K2H_VAL_CMASK_BIT;
zend_long maxelementcnt = K2H_VAL_ELEMENT;
zend_long pagesize = K2H_VAL_PAGESIZE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bllll", &filepath, &filepath_len, &fullmap, &maskbitcnt, &cmaskbitcnt, &maxelementcnt, &pagesize) == FAILURE) {
return;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_ERROR, "K2hash::openTempfile: filepath is empty.");
RETURN_FALSE;
}
// open new handle
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_newhandle = intern->handle;
if (!res_newhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::open: custom object initialize error.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
*res_newhandle = k2h_open_tempfile(filepath, 0 != fullmap ? true : false, (int)maskbitcnt, (int)cmaskbitcnt, (int)maxelementcnt, (size_t)pagesize);
// cppcheck-suppress nullPointerRedundantCheck
if(K2H_INVALID_HANDLE == *res_newhandle) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} openTempfile */
/* {{{ proto bool openMem([int maskbitcnt = K2H_DEF_MASK_BIT[, int cmaskbitcnt = K2H_DEF_CMASK_BIT[, int maxelementcnt = K2H_DEF_ELEMENT[, int pagesize = K2H_DEF_PAGESIZE]]]])
K2hash::openMem method */
PHP_METHOD(K2hash, openMem)
{
zend_long maskbitcnt = K2H_VAL_MASK_BIT;
zend_long cmaskbitcnt = K2H_VAL_CMASK_BIT;
zend_long maxelementcnt = K2H_VAL_ELEMENT;
zend_long pagesize = K2H_VAL_PAGESIZE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|llll", &maskbitcnt, &cmaskbitcnt, &maxelementcnt, &pagesize) == FAILURE) {
return;
}
// open new handle
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_newhandle = intern->handle;
if (!res_newhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::open: custom object initialize error.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
*res_newhandle = k2h_open_mem((int)maskbitcnt, (int)cmaskbitcnt, (int)maxelementcnt, (size_t)pagesize);
// cppcheck-suppress nullPointerRedundantCheck
if(K2H_INVALID_HANDLE == *res_newhandle) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} openMem */
/* {{{ proto bool close([int waitms = 0])
K2hash::close method */
PHP_METHOD(K2hash, close)
{
zend_long waitms = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &waitms) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
/*
1555 k2h_h* res_k2hhandle = intern->handle;
(gdb) p intern
$1 = (php_k2hash_object *) 0x7ffff605ddc0
(gdb) p *intern
$2 = {handle = 0x7ffff6075050, std = {gc = {refcount = 2, u = {v = {type = 8 '\b', flags = 0 '\000', gc_info = 49154},
*/
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::close: custom object initialize error.");
RETURN_FALSE;
}
// check parameter
if(waitms < -1) {
php_error_docref(NULL, E_NOTICE, "K2hash::close: waitms must be -1, 0, 1...");
RETURN_FALSE;
}
// close with timeout
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_close_wait(*res_k2hhandle, waitms)) {
php_error_docref(NULL, E_NOTICE, "K2hash::close: failed to close k2hash.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} close */
/* {{{ proto bool transaction(bool enable[, string transfile = NULL[, string prefix = NULL[, string param = NULL[, int expire = 0]]]])
K2hash::transaction method */
PHP_METHOD(K2hash, transaction)
{
zend_bool enable = 0;
const char * transfile = NULL;
size_t transfile_len = 0;
const char * prefix = NULL;
size_t prefix_len = 0;
const char * param = NULL;
size_t param_len = 0;
zend_long expire = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "b|sssl", &enable, &transfile, &transfile_len, &prefix, &prefix_len, ¶m, ¶m_len, &expire) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::transaction: could not open k2hash.");
RETURN_FALSE;
}
// cppcheck-suppress unreadVariable
time_t tmexpire = 0;
time_t* ptmexpire = NULL;
if(0 < expire) {
tmexpire = expire;
ptmexpire = &tmexpire;
}
// transaction
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_transaction_param_we(*res_k2hhandle, 0 != enable ? true : false, (!transfile || 0 == transfile_len) ? NULL : transfile, (!prefix || 0 == prefix_len) ? NULL : (const unsigned char*)prefix, prefix_len, (!param || 0 == param_len) ? NULL : (const unsigned char*)param, param_len, ptmexpire)) {
php_error_docref(NULL, E_NOTICE, "K2hash::transaction: failed to enable/disable transaction.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} transaction */
/* {{{ proto bool enableTransaction([string transfile = NULL[, string prefix = NULL[, string param = NULL[, int expire = 0]]]])
K2hash::enableTransaction method */
PHP_METHOD(K2hash, enableTransaction)
{
const char * transfile = NULL;
size_t transfile_len = 0;
const char * prefix = NULL;
size_t prefix_len = 0;
const char * param = NULL;
size_t param_len = 0;
zend_long expire = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sssl", &transfile, &transfile_len, &prefix, &prefix_len, ¶m, ¶m_len, &expire) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::enableTransaction: could not open k2hash.");
RETURN_FALSE;
}
// cppcheck-suppress unreadVariable
time_t tmexpire = 0;
time_t* ptmexpire = NULL;
if(0 < expire) {
tmexpire = expire;
ptmexpire = &tmexpire;
}
// transaction
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_enable_transaction_param_we(*res_k2hhandle, (!transfile || 0 == transfile_len) ? NULL : transfile, (!prefix || 0 == prefix_len) ? NULL : (const unsigned char*)prefix, prefix_len, (!param || 0 == param_len) ? NULL : (const unsigned char*)param, param_len, ptmexpire)) {
php_error_docref(NULL, E_NOTICE, "K2hash::enableTransaction: failed to enable transaction.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} enableTransaction */
/* {{{ proto bool disableTransaction()
K2hash::disableTransaction method */
PHP_METHOD(K2hash, disableTransaction)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::disableTransaction: could not open k2hash.");
RETURN_FALSE;
}
// transaction
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_disable_transaction(*res_k2hhandle)) {
php_error_docref(NULL, E_NOTICE, "K2hash::disableTransaction: failed to disable transaction.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} disableTransaction */
/* {{{ proto int getTransactionThreadPool()
K2hash::getTransactionThreadPool method */
PHP_METHOD(K2hash, getTransactionThreadPool)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
int count = k2h_get_transaction_thread_pool();
RETURN_LONG((long)count);
}
/* }}} getTransactionThreadPool */
/* {{{ proto bool setTransactionThreadPool(int count)
K2hash::getTransactionThreadPool method */
PHP_METHOD(K2hash, setTransactionThreadPool)
{
zend_long count = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "l", &count) == FAILURE) {
return;
}
if(!k2h_set_transaction_thread_pool((int)count)) {
php_error_docref(NULL, E_NOTICE, "K2hash::setTransactionThreadPool: failed to set transaction thread pool.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} setTransactionThreadPool */
/* {{{ proto bool unsetTransactionThreadPool()
K2hash::unsetTransactionThreadPool method */
PHP_METHOD(K2hash, unsetTransactionThreadPool)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if(!k2h_unset_transaction_thread_pool()) {
php_error_docref(NULL, E_NOTICE, "K2hash::setTransactionThreadPool: failed to unset transaction thread pool.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} unsetTransactionThreadPool */
/* {{{ proto bool putArchive(string filepath[, bool errskip = true])
K2hash::putArchive method */
PHP_METHOD(K2hash, putArchive)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_bool errskip = true;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &filepath, &filepath_len, &errskip) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::putArchive: could not open k2hash.");
RETURN_FALSE;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::putArchive: file path is empty.");
RETURN_FALSE;
}
// archive
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_put_archive(*res_k2hhandle, filepath, 0 != errskip ? true : false)) {
php_error_docref(NULL, E_NOTICE, "K2hash::putArchive: failed to put archive.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} putArchive */
/* {{{ proto bool loadArchive(string filepath[, bool errskip = true])
K2hash::loadArchive method */
PHP_METHOD(K2hash, loadArchive)
{
const char * filepath = NULL;
size_t filepath_len = 0;
zend_bool errskip = true;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &filepath, &filepath_len, &errskip) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::loadArchive: could not open k2hash.");
RETURN_FALSE;
}
if(!filepath || 0 == filepath_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::loadArchive: file path is empty.");
RETURN_FALSE;
}
// archive
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_load_archive(*res_k2hhandle, filepath, 0 != errskip ? true : false)) {
php_error_docref(NULL, E_NOTICE, "K2hash::loadArchive: failed to load archive.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} loadArchive */
/* {{{ proto bool setCommonAttribute([int is_mtime = K2H_ATTR_DEFAULT[, int is_history = K2H_ATTR_DEFAULT[, int is_encrypt = K2H_ATTR_DEFAULT, string passfile = NULL[, int is_expire = K2H_ATTR_DEFAULT, int expire = 0]]]])
K2hash::setCommonAttribute method */
PHP_METHOD(K2hash, setCommonAttribute)
{
zend_long is_mtime = K2H_VAL_ATTR_DEFAULT;
zend_long is_history = K2H_VAL_ATTR_DEFAULT;
zend_long is_encrypt = K2H_VAL_ATTR_DEFAULT;
const char * passfile = NULL;
size_t passfile_len = 0;
zend_long is_expire = K2H_VAL_ATTR_DEFAULT;
zend_long expire = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lllsll", &is_mtime, &is_history, &is_encrypt, &passfile, &passfile_len, &is_expire, &expire) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::setCommonAttribute: could not open k2hash.");
RETURN_FALSE;
}
// cppcheck-suppress unreadVariable
bool mtime_val = false;
bool* pmtime_val = NULL;
if(K2H_VAL_ATTR_DEFAULT != is_mtime) {
mtime_val = K2H_VAL_ATTR_ENABLE == is_mtime ? true : false;
pmtime_val = &mtime_val;
}
// cppcheck-suppress unreadVariable
bool enc_val = false;
bool* penc_val = NULL;
const char* pfile = NULL;
if(K2H_VAL_ATTR_DEFAULT != is_encrypt) {
if(K2H_VAL_ATTR_ENABLE == is_encrypt) {
if(!passfile || passfile_len == 0) {
php_error_docref(NULL, E_ERROR, "K2hash::setCommonAttribute: passfile must not be empty.");
RETURN_FALSE;
}
enc_val = false;
penc_val = &enc_val;
pfile = passfile;
} else {
enc_val = false;
penc_val = &enc_val;
pfile = !passfile || passfile_len == 0 ? NULL : passfile;
}
}
// cppcheck-suppress unreadVariable
bool his_val = false;
bool* phis_val = NULL;
if(K2H_VAL_ATTR_DEFAULT != is_history) {
his_val = K2H_VAL_ATTR_ENABLE == is_history ? true : false;
phis_val = &his_val;
}
// cppcheck-suppress unreadVariable
time_t expire_val = 0;
time_t* pexpire_val = NULL;
if(K2H_VAL_ATTR_DEFAULT != is_expire) {
if(K2H_VAL_ATTR_ENABLE == is_expire) {
if(expire <= 0) {
php_error_docref(NULL, E_ERROR, "K2hash::setCommonAttribute: expire must not be over zero.");
RETURN_FALSE;
}
expire_val = (time_t)expire;
pexpire_val = &expire_val;
} else {
expire_val = -1; // == NOT_EXPIRE
pexpire_val = &expire_val;
}
}
// set
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_set_common_attr(*res_k2hhandle, pmtime_val, penc_val, pfile, phis_val, pexpire_val)) {
php_error_docref(NULL, E_NOTICE, "K2hash::setCommonAttribute: failed to set common attributes.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} setCommonAttribute */
/* {{{ proto bool cleanCommonAttribute()
K2hash::cleanCommonAttribute method */
PHP_METHOD(K2hash, cleanCommonAttribute)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::cleanCommonAttribute: could not open k2hash.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_clean_common_attr(*res_k2hhandle)) {
php_error_docref(NULL, E_NOTICE, "K2hash::cleanCommonAttribute: failed to clear common attributes.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} cleanCommonAttribute */
/* {{{ proto bool addAttrPluginLib(string libfile)
K2hash::addAttrPluginLib method */
PHP_METHOD(K2hash, addAttrPluginLib)
{
const char * libfile = NULL;
size_t libfile_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &libfile, &libfile_len) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::addAttrPluginLib: could not open k2hash.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_add_attr_plugin_library(*res_k2hhandle, libfile)) {
php_error_docref(NULL, E_NOTICE, "K2hash::addAttrPluginLib: failed to load plugin attribute library.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} addAttrPluginLib */
/* {{{ proto bool addAttrCryptPass(string encpass[, bool is_default_encrypt = false])
K2hash::addAttrCryptPass method */
PHP_METHOD(K2hash, addAttrCryptPass)
{
const char * encpass = NULL;
size_t encpass_len = 0;
zend_bool is_default_encrypt = false;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &encpass, &encpass_len, &is_default_encrypt) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::addAttrCryptPass: could not open k2hash.");
RETURN_FALSE;
}
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_add_attr_crypt_pass(*res_k2hhandle, encpass, is_default_encrypt)) {
php_error_docref(NULL, E_NOTICE, "K2hash::addAttrCryptPass: failed to add pass phrase.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} addAttrCryptPass */
/* {{{ proto bool getAttrVersionInfos([stream output = NULL])
K2hash::getAttrVersionInfos method */
PHP_METHOD(K2hash, getAttrVersionInfos)
{
zval * output = NULL;
php_stream * res_output = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &output) == FAILURE) {
return;
}
php_stream_from_zval(res_output, output);
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrVersionInfos: could not open k2hash.");
RETURN_FALSE;
}
FILE* fp = NULL;
if(res_output) {
if(FAILURE == php_stream_cast(res_output, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
php_error_docref(NULL, E_ERROR, "K2hash::getAttrVersionInfos: failed to convert php stream to FILE.");
RETURN_FALSE;
}
}
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_print_attr_version(*res_k2hhandle, fp)) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrVersionInfos: failed to print attribute version inrmation.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} getAttrVersionInfos */
/* {{{ proto bool getAttrInfos([stream output = NULL])
K2hash::getAttrInfos method */
PHP_METHOD(K2hash, getAttrInfos)
{
zval * output = NULL;
php_stream * res_output = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &output) == FAILURE) {
return;
}
php_stream_from_zval(res_output, output);
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrInfos: could not open k2hash.");
RETURN_FALSE;
}
FILE* fp = NULL;
if(res_output) {
if(FAILURE == php_stream_cast(res_output, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
php_error_docref(NULL, E_ERROR, "K2hash::getAttrInfos: failed to convert php stream to FILE.");
RETURN_FALSE;
}
}
// cppcheck-suppress nullPointerRedundantCheck
if(!k2h_print_attr_version(*res_k2hhandle, fp)) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrInfos: failed to print all attribute inrmation.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} getAttrInfos */
/* {{{ proto string getValue(string key[, string subkey = NULL[, bool attrcheck = true[, string pass = NULL]]])
K2hash::getValue method */
PHP_METHOD(K2hash, getValue)
{
const char * key = NULL;
size_t key_len = 0;
const char * subkey = NULL;
size_t subkey_len = 0;
zend_bool attrcheck = true;
const char * pass = NULL;
size_t pass_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sbs", &key, &key_len, &subkey, &subkey_len, &attrcheck, &pass, &pass_len) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::getValue: could not open k2hash.");
RETURN_FALSE;
}
if(!key || 0 == key_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::getValue: key is empty.");
RETURN_FALSE;
}
// get
char* pvalue = NULL;
if(!subkey || 0 == subkey_len) {
if(attrcheck) {
if(NULL == (pvalue = k2h_get_str_direct_value_wp(*res_k2hhandle, key, pass))) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: failed to get value.");
RETURN_FALSE;
}
} else {
if(NULL == (pvalue = k2h_get_str_direct_value_np(*res_k2hhandle, key))) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: failed to get value.");
RETURN_FALSE;
}
}
} else {
// cppcheck-suppress nullPointerRedundantCheck
char** pskeyarray = NULL;
int subkey_count;
bool isFound = false;
if(attrcheck) {
subkey_count = k2h_get_str_subkeys(*res_k2hhandle, key, &pskeyarray);
if (subkey_count == -1 || subkey_count == 0) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: the key does not have subkey.");
if(pskeyarray) {
k2h_free_keyarray(pskeyarray);
}
RETURN_FALSE;
}
} else {
subkey_count = k2h_get_str_subkeys_np(*res_k2hhandle, key, &pskeyarray);
if (subkey_count == -1 || subkey_count == 0) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: the key does not have subkey.");
if(pskeyarray) {
k2h_free_keyarray(pskeyarray);
}
RETURN_FALSE;
}
}
char** pptmp;
for(pptmp = pskeyarray; pptmp && *pptmp; pptmp++) {
if(0 == strcmp(*pptmp, subkey)) {
isFound = true;
break;
}
}
k2h_free_keyarray(pskeyarray);
if(!isFound) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: the key does not have subkey.");
RETURN_FALSE;
}
if(attrcheck) {
if(NULL == (pvalue = k2h_get_str_direct_value_wp(*res_k2hhandle, subkey, pass))) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: the key does not exist.");
RETURN_FALSE;
}
} else {
if(NULL == (pvalue = k2h_get_str_direct_value_np(*res_k2hhandle, subkey))) {
//php_error_docref(NULL, E_NOTICE, "K2hash::getValue: the key does not exist.");
RETURN_FALSE;
}
}
}
RETVAL_STRING(pvalue);
free(pvalue);
}
/* }}} getValue */
/* {{{ proto array getSubkeys(string key[, bool attrcheck = true])
K2hash::getSubkeys method */
PHP_METHOD(K2hash, getSubkeys)
{
const char * key = NULL;
size_t key_len = 0;
zend_bool attrcheck = true;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &key, &key_len, &attrcheck) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::getSubkeys: could not open k2hash.");
RETURN_FALSE;
}
if(!key || 0 == key_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::getSubkeys: key is empty.");
RETURN_FALSE;
}
array_init(return_value);
// get
char** pskeyarray = NULL;
if(attrcheck) {
if(0 != k2h_get_str_subkeys(*res_k2hhandle, key, &pskeyarray)) {
char** pptmp;
for(pptmp = pskeyarray; pptmp && *pptmp; pptmp++) {
add_next_index_string(return_value, *pptmp);
}
}
} else {
if(0 != k2h_get_str_subkeys_np(*res_k2hhandle, key, &pskeyarray)) {
char** pptmp;
for(pptmp = pskeyarray; pptmp && *pptmp; pptmp++) {
add_next_index_string(return_value, *pptmp);
}
}
}
if(pskeyarray) {
k2h_free_keyarray(pskeyarray);
}
}
/* }}} getSubkeys */
/* {{{ proto array getAttrs(string key)
K2hash::getAttrs method */
PHP_METHOD(K2hash, getAttrs)
{
const char * key = NULL;
size_t key_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
return;
}
array_init(return_value);
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrs: could not open k2hash.");
RETURN_FALSE;
}
if(!key || 0 == key_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrs: key is empty.");
RETURN_FALSE;
}
// get
PK2HATTRPCK pattrs;
int attrspckcnt = 0;
// cppcheck-suppress nullPointerRedundantCheck
if(NULL != (pattrs = k2h_get_str_direct_attrs(*res_k2hhandle, key, &attrspckcnt)) && 0 < attrspckcnt) {
int cnt;
for(cnt = 0; cnt < attrspckcnt; ++cnt) {
char* pattrkey;
if(NULL != (pattrkey = (char*)malloc(pattrs[cnt].keylength + 1))) {
memcpy(pattrkey, pattrs[cnt].pkey, pattrs[cnt].keylength);
pattrkey[pattrs[cnt].keylength] = '\0';
add_next_index_string(return_value, pattrkey);
free(pattrkey);
}
}
}
if(pattrs) {
k2h_free_attrpack(pattrs, attrspckcnt);
}
}
/* }}} getAttrs */
/* {{{ proto string getAttrValue(string key, string attrkey)
K2hash::getAttrValue method */
PHP_METHOD(K2hash, getAttrValue)
{
const char * key = NULL;
size_t key_len = 0;
const char * attrkey = NULL;
size_t attrkey_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &key, &key_len, &attrkey, &attrkey_len) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrValue: could not open k2hash.");
RETURN_FALSE;
}
if(!key || 0 == key_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrValue: key is empty.");
RETURN_FALSE;
}
if(!attrkey || 0 == attrkey_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrValue: attribute key is empty.");
RETURN_FALSE;
}
// get
PK2HATTRPCK pattrs;
int attrspckcnt = 0;
bool is_found = false;
// cppcheck-suppress nullPointerRedundantCheck
if(NULL != (pattrs = k2h_get_str_direct_attrs(*res_k2hhandle, key, &attrspckcnt)) && 0 < attrspckcnt) {
int cnt;
for(cnt = 0; cnt < attrspckcnt; ++cnt) {
if(pattrs[cnt].keylength < (size_t)attrkey_len) {
continue; // wrong length
}
if(0 == memcmp(attrkey, pattrs[cnt].pkey, (size_t)attrkey_len)) {
// found attribute key
char* pattrval;
if(NULL == (pattrval = (char*)malloc(pattrs[cnt].vallength + 1))) {
php_error_docref(NULL, E_ERROR, "K2hash::getAttrValue: could not allocate memory.");
k2h_free_attrpack(pattrs, attrspckcnt);
RETURN_FALSE;
}
// copy
// cppcheck-suppress nullPointerRedundantCheck
memcpy(pattrval, pattrs[cnt].pval, pattrs[cnt].vallength);
// cppcheck-suppress nullPointerRedundantCheck
pattrval[pattrs[cnt].vallength] = '\0';
// set return value
RETVAL_STRING(pattrval);
free(pattrval);
is_found = true;
break;
}
}
}
if(pattrs) {
k2h_free_attrpack(pattrs, attrspckcnt);
}
if(!is_found) {
php_error_docref(NULL, E_NOTICE, "K2hash::getAttrValue: could not find attribute key name.");
RETURN_FALSE;
}
}
/* }}} getAttrValue */
/* {{{ proto bool setValue(string key, string value[, string subkey = NULL[, string pass=NULL[, int expire=0]]])
K2hash::setValue method */
PHP_METHOD(K2hash, setValue)
{
const char * key = NULL;
size_t key_len = 0;
const char * value = NULL;
size_t value_len = 0;
const char * subkey = NULL;
size_t subkey_len = 0;
const char * pass = NULL;
size_t pass_len = 0;
zend_long expire = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|ssl", &key, &key_len, &value, &value_len, &subkey, &subkey_len, &pass, &pass_len, &expire) == FAILURE) {
return;
}
php_k2hash_object* intern = Z_K2HASH_OBJECT_P(getThis());
k2h_h* res_k2hhandle = intern->handle;
if (!res_k2hhandle || K2H_INVALID_HANDLE == res_k2hhandle) {
php_error_docref(NULL, E_NOTICE, "K2hash::setValue: could not open k2hash.");
RETURN_FALSE;
}
if(!key || 0 == key_len) {
php_error_docref(NULL, E_NOTICE, "K2hash::setValue: key is empty.");
RETURN_FALSE;
}
// cppcheck-suppress unreadVariable
time_t tmexpire = 0;
time_t* ptmexpire = NULL;
if(0 < expire) {
tmexpire = expire;
ptmexpire = &tmexpire;
}
// set
if(!subkey || 0 == subkey_len) {
if(!k2h_set_str_value_wa(*res_k2hhandle, key, value, pass, ptmexpire)) {
php_error_docref(NULL, E_NOTICE, "K2hash::setValue: failed to set value to key");
RETURN_FALSE;
}
} else {