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
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
/* New memory searcher functions */ 
 
#include "global.h" 
#include "stuff.h" 
#include "mapping.h" 
#include "object.h" 
#include "pike_memory.h" 
#include "stralloc.h" 
#include "interpret.h" 
#include "pike_error.h" 
#include "module_support.h" 
#include "pike_macros.h" 
#include "pike_search.h" 
 
ptrdiff_t pike_search_struct_offset; 
#define OB2MSEARCH(O) ((struct pike_mem_searcher *)((O)->storage+pike_search_struct_offset)) 
#define THIS_MSEARCH ((struct pike_mem_searcher *)(Pike_fp->current_storage)) 
 
/* NB: We use the least significant bit of memsearch_cache_threshold 
 *     to indicate whether we are at MIN_MEMSEARCH_THRESHOLD or not. 
 */ 
#define MIN_MEMSEARCH_THRESHOLD       128 
static int memsearch_cache_threshold; 
 
static struct mapping *memsearch_cache; 
static struct program *pike_search_program; 
 
 
static void *nil_search(void *UNUSED(no_data), 
                        void *haystack, 
                        ptrdiff_t UNUSED(haystacklen)) 
{ 
  return haystack; 
} 
 
/* Needed on architectures where struct returns have 
 * incompatible calling conventions (sparc v8). 
 */ 
static PCHARP nil_searchN(void *UNUSED(no_data), 
                          PCHARP haystack, 
                          ptrdiff_t UNUSED(haystacklen)) 
{ 
  return haystack; 
} 
 
static const struct SearchMojtVtable nil_search_vtable = { 
  (SearchMojtFunc0) nil_search, 
  (SearchMojtFunc1) nil_search, 
  (SearchMojtFunc2) nil_search, 
  (SearchMojtFuncN) nil_searchN, 
}; 
 
/* magic stuff for hubbesearch */ 
/* NOTE: GENERIC_GET4_CHARS(PTR) must be compatible with 
 *       the GET_4_{,UN}ALIGNED_CHARS0() variants! 
 */ 
#if PIKE_BYTEORDER == 4321 
#define GENERIC_GET4_CHARS(PTR) \ 
 ( ((PTR)[0] << 24) + ( (PTR)[1] << 16 ) +( (PTR)[2] << 8 ) +  (PTR)[3] ) 
#else /* PIKE_BYTEORDER != 4321 */ 
#define GENERIC_GET4_CHARS(PTR) \ 
 ( ((PTR)[3] << 24) + ( (PTR)[2] << 16 ) +( (PTR)[1] << 8 ) +  (PTR)[0] ) 
#endif /* PIKE_BYTEORDER == 4321 */ 
 
#define HUBBE_ALIGN0(q) q=(p_wchar0 *)(PTR_TO_INT(q) & ~(sizeof(INT32) - 1)) 
#define GET_4_ALIGNED_CHARS0(PTR)  (*(INT32 *)(PTR)) 
#define GET_4_UNALIGNED_CHARS0(PTR)  EXTRACT_INT(PTR) 
 
#define HUBBE_ALIGN1(q) 
#define GET_4_ALIGNED_CHARS1 GENERIC_GET4_CHARS 
#define GET_4_UNALIGNED_CHARS1 GENERIC_GET4_CHARS 
 
#define HUBBE_ALIGN2(q) 
#define GET_4_ALIGNED_CHARS2 GENERIC_GET4_CHARS 
#define GET_4_UNALIGNED_CHARS2 GENERIC_GET4_CHARS 
 
 
#define PxC(X,Y) PIKE_CONCAT(X,Y) 
#define PxC2(X,Y) PIKE_CONCAT(X,Y) 
#define PxC3(X,Y,Z) PIKE_CONCAT3(X,Y,Z) 
#define PxC4(X,Y,Z,Q) PIKE_CONCAT4(X,Y,Z,Q) 
#define NameN(X) PxC2(X,NSHIFT) 
#define NameH(X) PxC2(X,HSHIFT) 
#define NameNH(X) PxC3(X,NSHIFT,HSHIFT) 
 
#define BMHASH00(X) (X) 
#define BMHASH01(X) ((X)>CHARS ? CHARS : (X)) 
#define BMHASH02    BMHASH01 
 
#define BMHASH10(X) (((X)*997)&(CHARS-1)) 
#define BMHASH11 BMHASH10 
#define BMHASH12 BMHASH10 
 
#define BMHASH20 BMHASH10 
#define BMHASH21 BMHASH20 
#define BMHASH22 BMHASH20 
 
#define NCHAR NameN(p_wchar) 
#define HCHAR NameH(p_wchar) 
 
#define NEEDLE ((NCHAR *)(s->needle)) 
#define NEEDLELEN s->needlelen 
 
#define NSHIFT 0 
#include "pike_search_engine.c" 
#undef NSHIFT 
 
#define NSHIFT 1 
#include "pike_search_engine.c" 
#undef NSHIFT 
 
#define NSHIFT 2 
#include "pike_search_engine.c" 
#undef NSHIFT 
 
 
#define NSHIFT 0 
 
 
 
PMOD_EXPORT void pike_init_memsearch(struct pike_mem_searcher *s, 
                                     PCHARP needle, 
                                     ptrdiff_t needlelen, 
                                     ptrdiff_t max_haystacklen) 
{ 
  switch(needle.shift) 
  { 
    case 0: 
      init_memsearch0(s,(p_wchar0*)needle.ptr, needlelen, max_haystacklen); 
      return; 
    case 1: 
      init_memsearch1(s,(p_wchar1*)needle.ptr, needlelen, max_haystacklen); 
      return; 
    case 2: 
      init_memsearch2(s,(p_wchar2*)needle.ptr, needlelen, max_haystacklen); 
      return; 
  } 
#ifdef PIKE_DEBUG 
  Pike_fatal("Illegal shift\n"); 
#endif 
} 
 
PMOD_EXPORT SearchMojt compile_memsearcher(PCHARP needle, 
                                           ptrdiff_t needlelen, 
                                           int max_haystacklen, 
                                           struct pike_string *hashkey) 
{ 
  switch(needle.shift) 
  { 
    default: 
#ifdef PIKE_DEBUG 
      Pike_fatal("Illegal shift\n"); 
    case 0: 
#endif 
      return compile_memsearcher0((p_wchar0*)needle.ptr, needlelen, max_haystacklen,hashkey); 
    case 1: 
      return compile_memsearcher1((p_wchar1*)needle.ptr, needlelen, max_haystacklen,hashkey); 
    case 2: 
      return compile_memsearcher2((p_wchar2*)needle.ptr, needlelen, max_haystacklen,hashkey); 
  } 
  UNREACHABLE(); 
} 
 
PMOD_EXPORT SearchMojt simple_compile_memsearcher(struct pike_string *str) 
{ 
  return compile_memsearcher(MKPCHARP_STR(str), 
                             str->len, 
                             0x7fffffff, 
                             str); 
} 
 
/* This function is thread safe, most other functions in this 
 * file are not thread safe. 
 */ 
PMOD_EXPORT char *my_memmem(char *needle, 
                            size_t needlelen, 
                            char *haystack, 
                            size_t haystacklen) 
{ 
  struct pike_mem_searcher tmp; 
  init_memsearch0(&tmp, (p_wchar0 *) needle, (ptrdiff_t)needlelen, 
                  (ptrdiff_t)haystacklen); 
  return (char *) tmp.mojt.vtab->func0(tmp.mojt.data, (p_wchar0 *) haystack, 
                                       (ptrdiff_t)haystacklen); 
  /* No free required - Hubbe */ 
} 
 
void init_pike_searching(void) 
{ 
  start_new_program(); 
  pike_search_struct_offset=ADD_STORAGE(struct pike_mem_searcher); 
  MAP_VARIABLE("__s", tStr, 0, 
               pike_search_struct_offset + OFFSETOF(pike_mem_searcher,s), 
               PIKE_T_STRING); 
  pike_search_program=end_program(); 
  add_program_constant("Search",pike_search_program,ID_PROTECTED); 
 
  memsearch_cache=allocate_mapping(10); 
  memsearch_cache->data->flags |= MAPPING_FLAG_WEAK; 
} 
 
void exit_pike_searching(void) 
{ 
  if(pike_search_program) 
  { 
    free_program(pike_search_program); 
    pike_search_program=0; 
  } 
  if(memsearch_cache) 
  { 
    free_mapping(memsearch_cache); 
    memsearch_cache=0; 
  } 
}