- for (offset = 0; offset + len <= sz; offset++)
- if (!strncmp(needle, data + offset, len))
- return 1;
- return 0;
+ cnt = 0;
+
+ if (regexp) {
+ regmatch_t regmatch;
+ int flags = 0;
+
+ while (*data && !regexec(regexp, data, 1, ®match, flags)) {
+ flags |= REG_NOTBOL;
+ data += regmatch.rm_so;
+ if (*data) data++;
+ cnt++;
+ }
+
+ } else { /* Classic exact string match */
+ /* Yes, I've heard of strstr(), but the thing is *data may
+ * not be NUL terminated. Sue me.
+ */
+ for (offset = 0; offset + len <= sz; offset++) {
+ /* we count non-overlapping occurrences of needle */
+ if (!memcmp(needle, data + offset, len)) {
+ offset += len - 1;
+ cnt++;
+ }
+ }
+ }
+ return cnt;