------------------------------------------------------------------------- strextract_ioreader Extract a string from an iobuffer. Start reading at position offset in iobuffer and extract until the string end_pattern is found. Input: reader reader on a iobuffer offset position to start reading end_pattern the termination string (nul terminated) Output: buffer
| 268 | STR_FAIL if extraction failed |
| 269 | -------------------------------------------------------------------------*/ |
| 270 | static int |
| 271 | strextract_ioreader(TSIOBufferReader reader, int offset, const char *end_pattern, char *buffer, int *buflen) |
| 272 | { |
| 273 | int buf_idx = 0; |
| 274 | int p_idx = 0; |
| 275 | int nbytes_so_far = 0; |
| 276 | int plen = strlen(end_pattern); |
| 277 | const char *ptr; |
| 278 | TSIOBufferBlock block = TSIOBufferReaderStart(reader); |
| 279 | |
| 280 | if (plen <= 0) { |
| 281 | return STR_FAIL; |
| 282 | } |
| 283 | |
| 284 | /* Now start extraction */ |
| 285 | while ((block != nullptr) && (p_idx < plen) && (buf_idx < PSI_FILENAME_MAX_SIZE)) { |
| 286 | int64_t blocklen; |
| 287 | const char *blockptr = TSIOBufferBlockReadStart(block, reader, &blocklen); |
| 288 | |
| 289 | for (ptr = blockptr; ptr < blockptr + blocklen; ptr++, nbytes_so_far++) { |
| 290 | if (nbytes_so_far >= offset) { |
| 291 | /* Add a new character to the filename */ |
| 292 | buffer[buf_idx++] = *ptr; |
| 293 | |
| 294 | /* If we have reach the end of the filename, we're done */ |
| 295 | if (end_pattern[p_idx] == *ptr) { |
| 296 | p_idx++; |
| 297 | if (p_idx == plen) { |
| 298 | break; |
| 299 | } |
| 300 | } else { |
| 301 | p_idx = 0; |
| 302 | } |
| 303 | |
| 304 | /* The filename is too long, something is fishy... let's abort extraction */ |
| 305 | if (buf_idx >= PSI_FILENAME_MAX_SIZE) { |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | block = TSIOBufferBlockNext(block); |
| 312 | } |
| 313 | |
| 314 | /* Error, could not read end of filename */ |
| 315 | if (buf_idx >= PSI_FILENAME_MAX_SIZE) { |
| 316 | Dbg(dbg_ctl, "strextract: filename too long"); |
| 317 | *buflen = 0; |
| 318 | return STR_FAIL; |
| 319 | } |
| 320 | |
| 321 | /* Full Match */ |
| 322 | if (p_idx == plen) { |
| 323 | /* Nul terminate the filename, remove the end_pattern copied into the buffer */ |
| 324 | *buflen = buf_idx - plen; |
| 325 | buffer[*buflen] = '\0'; |
| 326 | Dbg(dbg_ctl, "strextract: filename = |%s|", buffer); |
| 327 | return STR_SUCCESS; |
no test coverage detected