<commit_before>#include <limits.h>
#include <string.h>
#include "byteshift_memmem.h"
#include "make_big_endian.h"


void *byteshift_memmem(const void *haystack, size_t haystack_len,
                       const void *needle, size_t needle_len)
{
    const void* needle_ptr;
    const void* haystack_ptr;
    int sums_diff;
    int compare_len;
    unsigned long first_needle_chars = 0;
    unsigned long first_haystack_chars = 0;

    switch (needle_len) {
        case (0):
            /* empty needle */
            return (void *) haystack;
            break;
        case (1):
            /* special case for single-character needles */
            return memchr(haystack, *((unsigned char*) needle), haystack_len);
            break;
    }

    /* start searching through haystack only from the first occurence of the
       first character of needle */
    haystack_ptr = memchr(haystack, *((unsigned char*) needle), haystack_len);
    if (!haystack_ptr) {
        /* the first character of needle isn't in haystack */
        return NULL;
    }
    haystack_len -= (haystack_ptr - haystack);
    if (haystack_len < needle_len) {
        /* the remaining haystack is smaller than needle */
        return NULL;
    }
    haystack = haystack_ptr;


    if (needle_len > LONG_INT_N_BYTES + 1)
    {
        needle_ptr = needle;
        sums_diff = 0;
#ifndef MAKE_ULONG_BIGENDIAN
        needle += LONG_INT_N_BYTES;
        while (needle_ptr != needle) {
            first_needle_chars <<= 8;
            first_needle_chars ^= *(unsigned char *)needle_ptr;
            first_haystack_chars <<= 8;
            first_haystack_chars ^= *(unsigned char *)haystack_ptr;
            sums_diff -= *((unsigned char *) needle_ptr++);
            sums_diff += *((unsigned char *) haystack_ptr++);
        }

        needle += needle_len - LONG_INT_N_BYTES;
        while (needle_ptr != needle) {
            sums_diff -= *((unsigned char *) needle_ptr++);
            sums_diff += *((unsigned char *) haystack_ptr++);
        }
#else
        needle += needle_len;
        while (needle_ptr != needle) {
            sums_diff -= *((unsigned char *) needle_ptr++);
            sums_diff += *((unsigned char *) haystack_ptr++);
        }

        first_needle_chars = MAKE_ULONG_BIGENDIAN(*(((unsigned long *)needle)));
        first_haystack_chars = MAKE_ULONG_BIGENDIAN(*(((unsigned long *)haystack)));
#endif /* MAKE_ULONG_BIGENDIAN */
        needle -= needle_len;

        /* we will call memcmp() only once we know that the sums are equal and
           that LONG_INT_N_BYTES last chars are equal, so it will be enough to
           compare all but the last LONG_INT_N_BYTES + 1 characters */
        compare_len = needle_len - (LONG_INT_N_BYTES + 1);

        /* At this point:
           * needle is at least two characters long
           * haystack is at least needle_len characters long (also at least two)
           * the first characters of needle and haystack are identical
        */
        if (   sums_diff == 0
            && first_haystack_chars == first_needle_chars
            && memcmp(haystack, needle, compare_len) == 0)
        {
            return (void *) haystack;<commit_msg>fixed a bug in bytehift_memmem()<commit_after>#include <limits.h>
#include <string.h>
#include "byteshift_memmem.h"
#include "make_big_endian.h"


void *byteshift_memmem(const void *haystack, size_t haystack_len,
                       const void *needle, size_t needle_len)
{
    const void* needle_ptr;
    const void* haystack_ptr;
    int sums_diff;
    int compare_len;
    unsigned long first_needle_chars = 0;
    unsigned long first_haystack_chars = 0;

    switch (needle_len) {
        case (0):
            /* empty needle */
            return (void *) haystack;
            break;
        case (1):
            /* special case for single-character needles */
            return memchr(haystack, *((unsigned char*) needle), haystack_len);
            break;
    }

    /* start searching through haystack only from the first occurence of the
       first character of needle */
    haystack_ptr = memchr(haystack, *((unsigned char*) needle), haystack_len);
    if (!haystack_ptr) {
        /* the first character of needle isn't in haystack */
        return NULL;
    }
    haystack_len -= (haystack_ptr - haystack);
    if (haystack_len < needle_len) {
        /* the remaining haystack is smaller than needle */
        return NULL;
    }
    haystack = haystack_ptr;


    if (needle_len > LONG_INT_N_BYTES + 1)
    {
        needle_ptr = needle;
        sums_diff = 0;
#ifndef MAKE_ULONG_BIGENDIAN
        needle += LONG_INT_N_BYTES;
        while (needle_ptr != needle) {
            first_needle_chars <<= 8;
            first_needle_chars ^= *(unsigned char *)needle_ptr;
            first_haystack_chars <<= 8;
            first_haystack_chars ^= *(unsigned char *)haystack_ptr;
            sums_diff -= *((unsigned char *) needle_ptr++);
            sums_diff += *((unsigned char *) haystack_ptr++);
        }

        needle += needle_len - LONG_INT_N_BYTES;
        while (needle_ptr != needle) {
            sums_diff -= *((unsigned char *) needle_ptr++);
            sums_diff += *((unsigned char *) haystack_ptr++);
        }
#else
        first_needle_chars = MAKE_ULONG_BIGENDIAN(*(((unsigned long *)needle)));
        first_haystack_chars = MAKE_ULONG_BIGENDIAN(*(((unsigned long *)haystack)));

        needle += needle_len;
        while (needle_ptr != needle) {
            sums_diff -= *((unsigned char *) needle_ptr++);
            sums_diff += *((unsigned char *) haystack_ptr++);
        }
#endif /* MAKE_ULONG_BIGENDIAN */
        needle -= needle_len;

        /* we will call memcmp() only once we know that the sums are equal and
           that LONG_INT_N_BYTES last chars are equal, so it will be enough to
           compare all but the last LONG_INT_N_BYTES + 1 characters */
        compare_len = needle_len - (LONG_INT_N_BYTES + 1);

        /* At this point:
           * needle is at least two characters long
           * haystack is at least needle_len characters long (also at least two)
           * the first characters of needle and haystack are identical
        */
        if (   sums_diff == 0
            && first_haystack_chars == first_needle_chars
            && memcmp(haystack, needle, compare_len) == 0)
        {
            return (void *) haystack;
{'fraction_non_alphanumeric': 0.101044752845782, 'fraction_numerical': 0.0037423982535474817, 'mean_word_length': 2.2574911122397157, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3578857', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1896, 'n_words': 535}