/*
 * strnlen.sx
 *
 * Implementation of POSIX.1-2008 conforming strnlen(), and a wrapper
 * extending it to conform with ISO-C11 TR-24731-1 strnlen_s().
 *
 * $Id$
 *
 * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
 * Copyright (C) 2016, 2017, MinGW.org Project
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 *
 * This file implements a POSIX.1-2008 conforming strnlen() function, under
 * the alternative public name of __mingw_strnlen(), (to avoid any possible
 * conflict with Microsoft's DLL implementations, or a replacement function
 * implemented by the user).
 *
 * Note that POSIX does not prescribe any error returns for its strnlen()
 * function; in particular, it does not specify handling of a NULL pointer,
 * passed as the string argument to count, and thus, a segmentation fault
 * is a plausible outcome.  This is consistent with the behaviour exhibited
 * by Microsoft's implementation, and this implementation acts likewise.
 *
 * OTOH, ISO-C11 TR-24731-1 prescribes that strnlen_s() shall return zero,
 * when passed a NULL pointer as the string argument; the alternative entry
 * point, __mingw_strnlen_s(), checks for this anomaly, and acts accordingly,
 * before continuing as for __mingw_strnlen(), when passed a valid pointer
 * to a string argument; (this is consistent with the documented behaviour
 * of Microsoft's strnlen_s() implementation).
 *
 * Note that I've chosen to implement this in assembly language to enable
 * sharing of common code between two distinct entry points, with subtly
 * differing behaviours, while avoiding the overhead of an extra function
 * call to invoke such shared code; this also allows the code to exploit
 * the CPU's string scanning instructions, (which GCC does not), and so
 * achieves a more compact, (and likely more efficient), implementation.
 *
 */
#if defined UNICODE || defined _UNICODE
/* The algorithm defined herein is effectively the same as that required
 * as a substitute for wcsnlen()/wcsnlen_s(); map entry point names to the
 * appropriate alternatives for scanning wchar_t *, (rather than char *),
 * strings, (i.e. Microsoft's so-called UNICODE strings).
 */
# define ___mingw_strnlen    ___mingw_wcsnlen
# define ___mingw_strnlen_s  ___mingw_wcsnlen_s

/* The fundamental change required to scan whcar_t * strings, rather than
 * char * strings, is that we must use the scasw instruction in place of
 * the scasb instruction; map it accordingly.
 */
# define scasb  scasw
#endif

.text
.align	4
.globl	___mingw_strnlen
.def	___mingw_strnlen;	.scl	2;	.type	32;	.endef

___mingw_strnlen:
/* Implements: size_t __mingw_strnlen (const char *string, size_t maxlen );
 *
 * Scans at most maxlen chars, returning the lesser of strlen (string) and
 * maxlen; does NOT check for string == NULL, which may thus induce failure
 * with a segmentation fault.  Note that initialization of return count to
 * zero, in EAX, also serves as the NUL char reference for SCASB, in AL.
 */
	pushl	%edi		/* must preserve this */
	xorl	%eax, %eax	/* initialize return count to zero */
	movl	8(%esp), %edx	/* load address of string argument */
.L1:
	movl	12(%esp), %ecx	/* load maxlen ... */
	jecxz	.L4		/* and jump to end, if it's zero */
.L2:
	cld			/* scan string from low-->high address */
	movl	%edx, %edi	/* using this as the scan pointer ... */
	repne	scasb		/* as required by this CPU scan */
	mov	%edi, %eax	/* note where we stopped ... */
	jnz	.L3		/* no NUL found; count is complete ... */
	decl	%eax		/* NUL found and counted; discount it */
.L3:
	sub	%edx, %eax	/* compute effective count to return */

#if defined UNICODE || defined _UNICODE
/* By the time we get to here, the EAX register contains the effective
 * length of the scanned string in bytes; this is the correct value to be
 * returned, in the case of char * strings, but it is twice the correct
 * length for wchar_t * strings, (possibly with one odd residual byte,
 * left over from a partially discounted NUL wchar_t); we may adjust this,
 * for the wchar_t * case, by a simple logical right shift, effectively
 * dividing the even extent of the (unsigned) count by two, extracting
 * the full wchar_t count, leaving any odd byte residual as remainder
 * in the carry flag, where we may simply (and safely) ignore it.
 */
	shrl	%eax		/* convert byte count to wchar_t count */
#endif

.L4:	popl	%edi		/* restore saved register ... */
	ret			/* and we're done */

.align	4
.globl	___mingw_strnlen_s
.def	___mingw_strnlen_s;	.scl	2;	.type	32;	.endef

___mingw_strnlen_s:
/* Implements: size_t __mingw_strnlen_s (const char *string, size_t maxlen );
 *
 * Exhibits identical behaviour to __mingw_strnlen(), EXCEPT that it DOES
 * check for string == NULL, returning zero when found.
 */
	pushl	%edi		/* must preserve this */
	xorl	%eax, %eax	/* initialize return count to zero */
	movl	8(%esp), %edx	/* load address of string argument ... */
	testl	%edx, %edx	/* checking for NULL pointer, and ... */
	jnz	.L1		/* proceeding as strnlen(), if not ... */
	popl	%edi		/* otherwise restore saved register ... */
	ret			/* and return zero count value */

/* $RCSfile$: end of file */
