Skip to content

Commit

Permalink
mymemmem: update with patch from musl mailing list
Browse files Browse the repository at this point in the history
Fix by Alexander Monakov.
  • Loading branch information
leahneukirchen committed Jul 11, 2017
1 parent 4a685de commit f7c2cd9
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions mymemmem.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// taken straight from musl@c718f9fc
// twobyte_memmem fixed to avoid 1 byte read over end of buffer
// incooperates fix from <[email protected]>

/*
Copyright © 2005-2014 Rich Felker, et al.
Expand Down Expand Up @@ -30,32 +30,27 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
{
uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
h++;
k--;
for (;;) {
if (hw == nw) return (char *)h-1;
if (!--k) return 0;
hw = hw<<8 | *++h;
}
return 0;
for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
if (hw == nw) return (char *)h-2;
return hw == nw ? (char *)h-2 : 0;
}

static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
{
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
for (h+=2, k-=2; k; k--, hw = (hw|*++h)<<8)
if (hw == nw) return (char *)h-2;
return 0;
for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
if (hw == nw) return (char *)h-3;
return hw == nw ? (char *)h-3 : 0;
}

static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
{
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
for (h+=3, k-=3; k; k--, hw = hw<<8 | *++h)
if (hw == nw) return (char *)h-3;
return 0;
for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
if (hw == nw) return (char *)h-4;
return hw == nw ? (char *)h-4 : 0;
}

#define MAX(a,b) ((a)>(b)?(a):(b))
Expand Down

0 comments on commit f7c2cd9

Please sign in to comment.