/*******************************************************************************/
/*                                                                             */
/*  Copyright 2005 Pascal Gloor <pascal.gloor@spale.com>                       */
/*                                                                             */
/*  Licensed under the Apache License, Version 2.0 (the "License");            */
/*  you may not use this file except in compliance with the License.           */
/*  You may obtain a copy of the License at                                    */
/*                                                                             */
/*     http://www.apache.org/licenses/LICENSE-2.0                              */
/*                                                                             */
/*  Unless required by applicable law or agreed to in writing, software        */
/*  distributed under the License is distributed on an "AS IS" BASIS,          */
/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/*  See the License for the specific language governing permissions and        */
/*  limitations under the License.                                             */
/*                                                                             */
/*******************************************************************************/

#include <string.h>

#include "memmem.h"

void *__memmem(void *big, size_t big_len, void *small, size_t small_len)
{
	char *pos, *last;
	char *cbig = (char*)big;
	char *csmall = (char*)small;

	if ( small_len == 0 )
		return big;

	if ( big_len < small_len )
		return NULL;

	last = cbig + big_len - small_len;

	for(pos = cbig; pos <= last; ++pos)
		if ( pos[0] == csmall[0] && !memcmp(&pos[1], csmall + 1, small_len - 1) )
			return pos;

	return NULL;
}