------------------------------------------------------------------------- strsearch_ioreader Looks for string pattern in an iobuffer Input: reader reader on a iobuffer pattern string to look for (nul terminated) Output: nparse number of chars scanned, excluding the matching pattern Return Value: STR_SUCCESS if pattern found STR_PARTIAL if pattern found partially
| 202 | STR_FAIL if pattern not found |
| 203 | -------------------------------------------------------------------------*/ |
| 204 | static StrOperationResult |
| 205 | strsearch_ioreader(TSIOBufferReader reader, const char *pattern, int *nparse) |
| 206 | { |
| 207 | int index = 0; |
| 208 | TSIOBufferBlock block = TSIOBufferReaderStart(reader); |
| 209 | int slen = strlen(pattern); |
| 210 | |
| 211 | if (slen <= 0) { |
| 212 | return STR_FAIL; |
| 213 | } |
| 214 | |
| 215 | *nparse = 0; |
| 216 | |
| 217 | /* Loop thru each block while we've not yet found the pattern */ |
| 218 | while ((block != nullptr) && (index < slen)) { |
| 219 | int64_t blocklen; |
| 220 | const char *blockptr = TSIOBufferBlockReadStart(block, reader, &blocklen); |
| 221 | const char *ptr; |
| 222 | |
| 223 | for (ptr = blockptr; ptr < blockptr + blocklen; ptr++) { |
| 224 | (*nparse)++; |
| 225 | if (*ptr == pattern[index]) { |
| 226 | index++; |
| 227 | if (index == slen) { |
| 228 | break; |
| 229 | } |
| 230 | } else { |
| 231 | index = 0; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* Parse next block */ |
| 236 | block = TSIOBufferBlockNext(block); |
| 237 | } |
| 238 | |
| 239 | *nparse -= index; /* Adjust nparse so it doesn't include matching chars */ |
| 240 | if (index == slen) { |
| 241 | Dbg(dbg_ctl, "strfind: match for %s at position %d", pattern, *nparse); |
| 242 | return STR_SUCCESS; |
| 243 | } else if (index > 0) { |
| 244 | Dbg(dbg_ctl, "strfind: partial match for %s at position %d", pattern, *nparse); |
| 245 | return STR_PARTIAL; |
| 246 | } else { |
| 247 | Dbg(dbg_ctl, "strfind no match for %s", pattern); |
| 248 | return STR_FAIL; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /*------------------------------------------------------------------------- |
| 253 | strextract_ioreader |
no test coverage detected