Browse code

pcre: offsets scanned if start is valid pcre: moved no offset info calculations to static function pcre: scanbuf quick skips CLI_OFF_NONE pcres paradiagm: pcre offsets are determined invalid by scanbuf

Kevin Lin authored on 2014/10/28 00:35:21
Showing 1 changed files
... ...
@@ -447,6 +447,9 @@ int cli_pcre_recaloff(struct cli_matcher *root, struct cli_pcre_off *data, struc
447 447
         return CL_EMEM;
448 448
     }
449 449
 
450
+    pm_dbgmsg("CLI_OFF_NONE: %u\n", CLI_OFF_NONE);
451
+    pm_dbgmsg("CLI_OFF_ANY: %u\n", CLI_OFF_ANY);
452
+
450 453
     /* iterate across all pcre metadata and recalc offsets */
451 454
     for (i = 0; i < root->pcre_metas; ++i) {
452 455
         pm = root->pcre_metatable[i];
... ...
@@ -454,7 +457,7 @@ int cli_pcre_recaloff(struct cli_matcher *root, struct cli_pcre_off *data, struc
454 454
         /* skip broken pcres, not getting executed anyways */
455 455
         if (pm->flags & CLI_PCRE_DISABLED) {
456 456
             data->offset[i] = CLI_OFF_NONE;
457
-            data->shift[i] = CLI_OFF_NONE;
457
+            data->shift[i] = 0;
458 458
             continue;
459 459
         }
460 460
 
... ...
@@ -462,11 +465,11 @@ int cli_pcre_recaloff(struct cli_matcher *root, struct cli_pcre_off *data, struc
462 462
             data->offset[i] = 0;
463 463
             data->shift[i] = 0;
464 464
         }
465
-        else if (pm->offdata[0] == CLI_OFF_ABSOLUTE) {
466
-            data->offset[i] = pm->offdata[1];
467
-            data->shift[i] = pm->offdata[2];
465
+        else if (pm->offdata[0] == CLI_OFF_NONE) {
466
+            data->offset[i] = CLI_OFF_NONE;
467
+            data->shift[i] = 0;
468 468
         }
469
-        else if (pm->offdata[0] == CLI_OFF_EOF_MINUS) {
469
+        else if (pm->offdata[0] == CLI_OFF_ABSOLUTE) {
470 470
             data->offset[i] = pm->offdata[1];
471 471
             data->shift[i] = pm->offdata[2];
472 472
         }
... ...
@@ -478,8 +481,18 @@ int cli_pcre_recaloff(struct cli_matcher *root, struct cli_pcre_off *data, struc
478 478
                 free(data->offset);
479 479
                 return ret;
480 480
             }
481
-            data->shift[i] = endoff-(data->offset[i]);
481
+            /* CLI_OFF_NONE gets passed down, CLI_OFF_ANY gets reinterpreted */
482
+            /* TODO - CLI_OFF_VERSION is interpreted as CLI_OFF_ANY(?) */
483
+            if (data->offset[i] == CLI_OFF_ANY) {
484
+                data->offset[i] = 0;
485
+                data->shift[i] = 0;
486
+            } else {
487
+                data->shift[i] = endoff-(data->offset[i]);
488
+            }
482 489
         }
490
+
491
+        pm_dbgmsg("%u: %u %u->%u(+%u)\n", i, pm->offdata[0], data->offset[i],
492
+                  data->offset[i]+data->shift[i], data->shift[i]);
483 493
     }
484 494
 
485 495
     return CL_SUCCESS;
... ...
@@ -495,6 +508,32 @@ void cli_pcre_freeoff(struct cli_pcre_off *data)
495 495
     }
496 496
 }
497 497
 
498
+int cli_pcre_qoff(struct cli_pcre_meta *pm, uint32_t length, uint32_t *adjbuffer, uint32_t *adjshift)
499
+{
500
+    if (!pm)
501
+        return CL_ENULLARG;
502
+
503
+    /* default to scanning whole buffer but try to use existing offdata */
504
+    if (pm->offdata[0] == CLI_OFF_NONE) {
505
+        return CL_BREAK;
506
+    }
507
+    else if (pm->offdata[0] == CLI_OFF_ABSOLUTE) {
508
+        *adjbuffer = pm->offdata[1];
509
+        *adjshift = pm->offdata[2];
510
+    }
511
+    else if (pm->offdata[0] == CLI_OFF_EOF_MINUS) {
512
+        *adjbuffer = length - pm->offdata[1];
513
+        *adjshift = pm->offdata[2];
514
+    }
515
+    else {
516
+        /* CLI_OFF_ANY and all relative offsets; TODO - check if relative offsets apply for normal hex substrs */
517
+        *adjbuffer = 0;
518
+        *adjshift = 0;
519
+    }
520
+
521
+    return CL_SUCCESS;
522
+}
523
+
498 524
 int cli_pcre_scanbuf(const unsigned char *buffer, uint32_t length, const struct cli_matcher *root, struct cli_ac_data *mdata, struct cli_ac_result **res, const struct cli_pcre_off *data, cli_ctx *ctx)
499 525
 {
500 526
     struct cli_pcre_meta **metatable = root->pcre_metatable, *pm = NULL;
... ...
@@ -522,6 +561,12 @@ int cli_pcre_scanbuf(const unsigned char *buffer, uint32_t length, const struct
522 522
             continue;
523 523
         }
524 524
 
525
+        /* skip checking and running CLI_OFF_NONE pcres */
526
+        if (data && data->offset[i] == CLI_OFF_NONE) {
527
+            pm_dbgmsg("cli_pcre_scanbuf: skipping CLI_OFF_NONE regex /%s/\n", pd->expression);
528
+            continue;
529
+        }
530
+
525 531
         /* evaluate trigger */
526 532
         if (pm->lsigid[0]) {
527 533
             cli_dbgmsg("cli_pcre_scanbuf: checking %s; running regex /%s/\n", pm->trigger, pd->expression);
... ...
@@ -544,25 +589,8 @@ int cli_pcre_scanbuf(const unsigned char *buffer, uint32_t length, const struct
544 544
 
545 545
         /* adjust the buffer sent to cli_pcre_match for offset and maxshift */
546 546
         if (!data) {
547
-            /* default to scanning whole buffer but try to use existing offdata */
548
-            if (pm->offdata[0] == CLI_OFF_ABSOLUTE) {
549
-                adjbuffer = pm->offdata[1];
550
-                adjshift = pm->offdata[2];
551
-            }
552
-            else if (pm->offdata[0] == CLI_OFF_EOF_MINUS) {
553
-                if (length > pm->offdata[1]) {
554
-                    adjbuffer = length - pm->offdata[1];
555
-                    adjshift = pm->offdata[2];
556
-                }
557
-                else {
558
-                    /* EOF is invalid */
559
-                    continue;
560
-                }
561
-            }
562
-            else {
563
-                adjbuffer = 0;
564
-                adjshift = 0;
565
-            }
547
+            if (cli_pcre_qoff(pm, length, &adjbuffer, &adjshift) != CL_SUCCESS)
548
+                continue;
566 549
         }
567 550
         else {
568 551
             adjbuffer = data->offset[i];
... ...
@@ -579,6 +607,7 @@ int cli_pcre_scanbuf(const unsigned char *buffer, uint32_t length, const struct
579 579
                         adjlength = adjshift;
580 580
             }
581 581
             else {
582
+                /* NOTE - if using non-encompass method 2, alter shift universally */
582 583
                 adjlength = length - adjbuffer;
583 584
             }
584 585
         }