@@ -557,7 +557,10 @@ static nr_library_table_t libraries[] = {
557
557
558
558
static size_t num_libraries = sizeof (libraries ) / sizeof (nr_library_table_t );
559
559
560
+ #define EXT_LEN 4
561
+
560
562
// find the minimum length of the file_to_check
563
+ #if 0
561
564
static size_t nr_library_table_min_file_to_check_len (
562
565
const nr_library_table_t * table ,
563
566
size_t num_tables ) {
@@ -574,7 +577,92 @@ static size_t nr_library_table_min_file_to_check_len(
574
577
575
578
return min_len ;
576
579
}
580
+ #endif
581
+
582
+ typedef struct _nr_suffix_trie_node_t nr_suffix_trie_node_t ;
583
+
584
+ struct _nr_suffix_trie_node_t {
585
+ nr_suffix_trie_node_t * children [256 ];
586
+ nr_suffix_trie_node_t * parent ;
587
+ void * value ;
588
+ };
589
+
590
+ static nr_suffix_trie_node_t suffix_trie ;
591
+
592
+ static nr_suffix_trie_node_t * nr_suffix_trie_node_create (void ) {
593
+ nr_suffix_trie_node_t * node = nr_zalloc (sizeof (nr_suffix_trie_node_t ));
594
+ return node ;
595
+ }
596
+
597
+ static void * nr_suffix_trie_lookup (nr_suffix_trie_node_t * root ,
598
+ const char * filename ,
599
+ size_t filename_len ) {
600
+ nr_suffix_trie_node_t * node = root ;
601
+ nr_suffix_trie_node_t * parent = NULL ;
602
+ const char * cp = filename + filename_len - (1 + EXT_LEN );
603
+ // nrl_always("Looking up filename=%s", filename);
604
+ while (node ) {
605
+ char c = * cp ;
606
+ if (nr_isupper (c )) {
607
+ c = nr_tolower (c );
608
+ }
609
+ if (!node -> children [c ]) {
610
+ // nrl_always("No match at %c", c);
611
+ break ;
612
+ }
613
+ // nrl_always("Match at %c, current node value=%p, moving back", c, node->value);
614
+ parent = node ;
615
+ node = node -> children [c ];
616
+ cp -- ;
617
+ }
618
+ // nrl_always("Returning value=%p", parent? parent->value : NULL);
619
+ return parent ? parent -> value : NULL ;
620
+ }
621
+
622
+ static void nr_suffix_trie_add (nr_suffix_trie_node_t * root ,
623
+ const char * suffix ,
624
+ size_t suffix_len ,
625
+ void * value ) {
626
+ char c ;
627
+ c = suffix [suffix_len - 1 ];
628
+ // nrl_always("Adding suffix=%s, char=%c", suffix, c);
629
+ if (NULL == root -> children [c ]) {
630
+ // nrl_always("Creating new node for char=%c", c);
631
+ root -> children [c ] = nr_suffix_trie_node_create ();
632
+ root -> children [c ]-> parent = root ;
633
+ }
634
+ if (1 == suffix_len ) {
635
+ nr_library_table_t * library = (nr_library_table_t * )value ;
636
+ // nrl_always("Adding library=%s", library->file_to_check);
637
+ root -> value = value ;
638
+ return ;
639
+ }
640
+ else {
641
+ nr_suffix_trie_add (root -> children [c ], suffix , suffix_len - 1 , value );
642
+ }
643
+ }
644
+
645
+ static void nr_suffix_trie_add_library (nr_suffix_trie_node_t * root , nr_library_table_t * library ) {
646
+ const char * suffix = library -> file_to_check ;
647
+ size_t suffix_len = library -> file_to_check_len - EXT_LEN ;
648
+ // char* suffix_sans_ext = nr_alloca(suffix_len + 1);
649
+ // nr_strxcpy(suffix_sans_ext, suffix, suffix_len);
650
+ // nrl_always("Adding suffix to trie=%s", suffix_sans_ext);
651
+ nr_suffix_trie_add (root , suffix , suffix_len , library );
652
+ }
577
653
654
+ static void nr_suffix_trie_destroy (nr_suffix_trie_node_t * root ) {
655
+ for (int i = 0 ; i < 256 ; i ++ ) {
656
+ if (root -> children [i ]) {
657
+ nr_suffix_trie_destroy (root -> children [i ]);
658
+ }
659
+ }
660
+ if (root -> parent ) {
661
+ nr_free (root );
662
+ }
663
+ }
664
+
665
+ #if 0
578
666
#include "util_hash.h"
579
667
#include "util_hashmap_private.h"
580
668
@@ -588,6 +676,7 @@ typedef struct _nr_suffix_lookup_hashmap {
588
676
static nr_suffix_lookup_hashmap_t library_lookup = {
589
677
.log2_num_buckets = 8 ,
590
678
};
679
+ #endif
591
680
#if 0
592
681
static nr_suffix_lookup_hashmap_t logging_library_lookup = {
593
682
.log2_num_buckets = 8 ,
@@ -597,9 +686,7 @@ static nr_suffix_lookup_hashmap_t vuln_mgmt_package_lookup = {
597
686
.log2_num_buckets = 8 ,
598
687
};
599
688
#endif
600
-
601
- #define EXT_LEN 4
602
-
689
+ #if 0
603
690
size_t nr_suffix_lookup_hashmap_hash_key (nr_suffix_lookup_hashmap_t * hashmap ,
604
691
const char * filename ,
605
692
const size_t filename_len ) {
@@ -680,6 +767,7 @@ void* nr_suffix_lookup_hashmap_get(nr_suffix_lookup_hashmap_t* hashmap,
680
767
}
681
768
return NULL ;
682
769
}
770
+ #endif
683
771
684
772
#if 0
685
773
// clang-format: off
@@ -754,16 +842,26 @@ nr_status_t nr_suffix_lookup_hashmap_add_package(nr_suffix_lookup_hashmap_t* has
754
842
#endif
755
843
756
844
void nr_php_execute_minit () {
845
+ #if 0
757
846
library_lookup .h = nr_hashmap_create_buckets (1 << library_lookup .log2_num_buckets , NULL );
758
847
library_lookup .suffix_len = nr_library_table_min_file_to_check_len (libraries , num_libraries );
759
848
760
849
for (size_t i = 0 ; i < num_libraries ; i ++ ) {
761
850
nr_suffix_lookup_hashmap_add_library (& library_lookup , & libraries [i ]);
762
851
}
852
+ #else
853
+ for (size_t i = 0 ; i < num_libraries ; i ++ ) {
854
+ nr_suffix_trie_add_library (& suffix_trie , & libraries [i ]);
855
+ }
856
+ #endif
763
857
}
764
858
765
859
void nr_php_execute_mshutdown () {
860
+ #if 0
766
861
nr_hashmap_destroy (& library_lookup .h );
862
+ #else
863
+ nr_suffix_trie_destroy (& suffix_trie );
864
+ #endif
767
865
}
768
866
769
867
/*
@@ -1082,9 +1180,15 @@ static inline void nr_execute_handle_library(const char* filename,
1082
1180
}
1083
1181
}
1084
1182
#else
1085
- nr_library_table_t * library = (nr_library_table_t * ) nr_suffix_lookup_hashmap_get (& library_lookup , filename , filename_len );
1086
- if (library && library -> handler ) {
1087
- library -> handler (library );
1183
+ //nr_library_table_t* library = (nr_library_table_t *) nr_suffix_lookup_hashmap_get(&library_lookup, filename, filename_len);
1184
+ nr_library_table_t * library = (nr_library_table_t * ) nr_suffix_trie_lookup (& suffix_trie , filename , filename_len );
1185
+ if (library ){
1186
+ if (!nr_striendswith (STR_AND_LEN (filename ), NR_PSTR (".php" ))) {
1187
+ return ;
1188
+ }
1189
+ if (library -> handler ) {
1190
+ library -> handler (library );
1191
+ }
1088
1192
}
1089
1193
#endif
1090
1194
}
0 commit comments