Index: stable/11/contrib/libc-vis/vis.c =================================================================== --- stable/11/contrib/libc-vis/vis.c (revision 314633) +++ stable/11/contrib/libc-vis/vis.c (revision 314634) @@ -1,761 +1,766 @@ /* $NetBSD: vis.c,v 1.71 2016/01/14 20:41:23 christos Exp $ */ /*- * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /*- * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #if defined(LIBC_SCCS) && !defined(lint) __RCSID("$NetBSD: vis.c,v 1.71 2016/01/14 20:41:23 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #ifdef __FBSDID __FBSDID("$FreeBSD$"); #define _DIAGASSERT(x) assert(x) #endif #include "namespace.h" #include #include #include #include #include #include #include #include #ifdef __weak_alias __weak_alias(strvisx,_strvisx) #endif #if !HAVE_VIS || !HAVE_SVIS #include #include #include #include /* * The reason for going through the trouble to deal with character encodings * in vis(3), is that we use this to safe encode output of commands. This * safe encoding varies depending on the character set. For example if we * display ps output in French, we don't want to display French characters * as M-foo. */ static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *); #undef BELL #define BELL L'\a' #if defined(LC_C_LOCALE) #define iscgraph(c) isgraph_l(c, LC_C_LOCALE) #else /* Keep it simple for now, no locale stuff */ #define iscgraph(c) isgraph(c) #ifdef notyet #include static int iscgraph(int c) { int rv; char *ol; ol = setlocale(LC_CTYPE, "C"); rv = isgraph(c); if (ol) setlocale(LC_CTYPE, ol); return rv; } #endif #endif #define ISGRAPH(flags, c) \ (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c)) #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7') #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n') #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r') #define xtoa(c) L"0123456789abcdef"[c] #define XTOA(c) L"0123456789ABCDEF"[c] #define MAXEXTRAS 30 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~"; static const wchar_t char_glob[] = L"*?[#"; #if !HAVE_NBTOOL_CONFIG_H #ifndef __NetBSD__ /* * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer * integral type and it is probably wrong, since currently the maximum * number of bytes and character needs is 6. Until this is fixed, the * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and * the assertion is commented out. */ #ifdef __FreeBSD__ /* * On FreeBSD including for CTASSERT only works in kernel * mode. */ #ifndef CTASSERT #define CTASSERT(x) _CTASSERT(x, __LINE__) #define _CTASSERT(x, y) __CTASSERT(x, y) #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1] #endif #endif /* __FreeBSD__ */ CTASSERT(MB_LEN_MAX <= sizeof(uint64_t)); #endif /* !__NetBSD__ */ #endif /* * This is do_hvis, for HTTP style (RFC 1808) */ static wchar_t * do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) { if (iswalnum(c) /* safe */ || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+' /* extra */ || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')' || c == L',') dst = do_svis(dst, c, flags, nextc, extra); else { *dst++ = L'%'; *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); *dst++ = xtoa((unsigned int)c & 0xf); } return dst; } /* * This is do_mvis, for Quoted-Printable MIME (RFC 2045) * NB: No handling of long lines or CRLF. */ static wchar_t * do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) { if ((c != L'\n') && /* Space at the end of the line */ ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) || /* Out of range */ (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) || /* Specific char to be escaped */ wcschr(L"#$@[\\]^`{|}~", c) != NULL)) { *dst++ = L'='; *dst++ = XTOA(((unsigned int)c >> 4) & 0xf); *dst++ = XTOA((unsigned int)c & 0xf); } else dst = do_svis(dst, c, flags, nextc, extra); return dst; } /* * Output single byte of multibyte character. */ static wchar_t * do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra) { if (flags & VIS_CSTYLE) { switch (c) { case L'\n': *dst++ = L'\\'; *dst++ = L'n'; return dst; case L'\r': *dst++ = L'\\'; *dst++ = L'r'; return dst; case L'\b': *dst++ = L'\\'; *dst++ = L'b'; return dst; case BELL: *dst++ = L'\\'; *dst++ = L'a'; return dst; case L'\v': *dst++ = L'\\'; *dst++ = L'v'; return dst; case L'\t': *dst++ = L'\\'; *dst++ = L't'; return dst; case L'\f': *dst++ = L'\\'; *dst++ = L'f'; return dst; case L' ': *dst++ = L'\\'; *dst++ = L's'; return dst; case L'\0': *dst++ = L'\\'; *dst++ = L'0'; if (iswoctal(nextc)) { *dst++ = L'0'; *dst++ = L'0'; } return dst; /* We cannot encode these characters in VIS_CSTYLE * because they special meaning */ case L'n': case L'r': case L'b': case L'a': case L'v': case L't': case L'f': case L's': case L'0': case L'M': case L'^': case L'$': /* vis(1) -l */ break; default: if (ISGRAPH(flags, c) && !iswoctal(c)) { *dst++ = L'\\'; *dst++ = c; return dst; } } } if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) { *dst++ = L'\\'; *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0'; *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0'; *dst++ = (c & 07) + L'0'; } else { if ((flags & VIS_NOSLASH) == 0) *dst++ = L'\\'; if (c & 0200) { c &= 0177; *dst++ = L'M'; } if (iswcntrl(c)) { *dst++ = L'^'; if (c == 0177) *dst++ = L'?'; else *dst++ = c + L'@'; } else { *dst++ = L'-'; *dst++ = c; } } return dst; } /* * This is do_vis, the central code of vis. * dst: Pointer to the destination buffer * c: Character to encode * flags: Flags word * nextc: The character following 'c' * extra: Pointer to the list of extra characters to be * backslash-protected. */ static wchar_t * do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) { int iswextra, i, shft; uint64_t bmsk, wmsk; iswextra = wcschr(extra, c) != NULL; if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) || ((flags & VIS_SAFE) && iswsafe(c)))) { *dst++ = c; return dst; } /* See comment in istrsenvisx() output loop, below. */ wmsk = 0; for (i = sizeof(wmsk) - 1; i >= 0; i--) { shft = i * NBBY; bmsk = (uint64_t)0xffLL << shft; wmsk |= bmsk; if ((c & wmsk) || i == 0) dst = do_mbyte(dst, (wint_t)( (uint64_t)(c & bmsk) >> shft), flags, nextc, iswextra); } return dst; } typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *); /* * Return the appropriate encoding function depending on the flags given. */ static visfun_t getvisfun(int flags) { if (flags & VIS_HTTPSTYLE) return do_hvis; if (flags & VIS_MIMESTYLE) return do_mvis; return do_svis; } /* * Expand list of extra characters to not visually encode. */ static wchar_t * makeextralist(int flags, const char *src) { wchar_t *dst, *d; size_t len; const wchar_t *s; + mbstate_t mbstate; + bzero(&mbstate, sizeof(mbstate)); len = strlen(src); if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL) return NULL; - if ((flags & VIS_NOLOCALE) || mbstowcs(dst, src, len) == (size_t)-1) { + if ((flags & VIS_NOLOCALE) || mbsrtowcs(dst, &src, len, &mbstate) == (size_t)-1) { size_t i; for (i = 0; i < len; i++) dst[i] = (wchar_t)(u_char)src[i]; d = dst + len; } else d = dst + wcslen(dst); if (flags & VIS_GLOB) for (s = char_glob; *s; *d++ = *s++) continue; if (flags & VIS_SHELL) for (s = char_shell; *s; *d++ = *s++) continue; if (flags & VIS_SP) *d++ = L' '; if (flags & VIS_TAB) *d++ = L'\t'; if (flags & VIS_NL) *d++ = L'\n'; if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\'; *d = L'\0'; return dst; } /* * istrsenvisx() * The main internal function. * All user-visible functions call this one. */ static int istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength, int flags, const char *mbextra, int *cerr_ptr) { wchar_t *dst, *src, *pdst, *psrc, *start, *extra; size_t len, olen; uint64_t bmsk, wmsk; wint_t c; visfun_t f; int clen = 0, cerr, error = -1, i, shft; char *mbdst, *mdst; ssize_t mbslength, maxolen; + mbstate_t mbstate; _DIAGASSERT(mbdstp != NULL); _DIAGASSERT(mbsrc != NULL || mblength == 0); _DIAGASSERT(mbextra != NULL); /* * Input (mbsrc) is a char string considered to be multibyte * characters. The input loop will read this string pulling * one character, possibly multiple bytes, from mbsrc and * converting each to wchar_t in src. * * The vis conversion will be done using the wide char * wchar_t string. * * This will then be converted back to a multibyte string to * return to the caller. */ /* Allocate space for the wide char strings */ psrc = pdst = extra = NULL; mdst = NULL; if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL) return -1; if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL) goto out; if (*mbdstp == NULL) { if ((mdst = calloc((4 * mblength) + 1, sizeof(*mdst))) == NULL) goto out; *mbdstp = mdst; } mbdst = *mbdstp; dst = pdst; src = psrc; if (flags & VIS_NOLOCALE) { /* Do one byte at a time conversion */ cerr = 1; } else { /* Use caller's multibyte conversion error flag. */ cerr = cerr_ptr ? *cerr_ptr : 0; } /* * Input loop. * Handle up to mblength characters (not bytes). We do not * stop at NULs because we may be processing a block of data * that includes NULs. */ mbslength = (ssize_t)mblength; /* * When inputing a single character, must also read in the * next character for nextc, the look-ahead character. */ if (mbslength == 1) mbslength++; + bzero(&mbstate, sizeof(mbstate)); while (mbslength > 0) { /* Convert one multibyte character to wchar_t. */ if (!cerr) - clen = mbtowc(src, mbsrc, MB_LEN_MAX); + clen = mbrtowc(src, mbsrc, MB_LEN_MAX, &mbstate); if (cerr || clen < 0) { /* Conversion error, process as a byte instead. */ *src = (wint_t)(u_char)*mbsrc; clen = 1; cerr = 1; } if (clen == 0) /* * NUL in input gives 0 return value. process * as single NUL byte and keep going. */ clen = 1; /* Advance buffer character pointer. */ src++; /* Advance input pointer by number of bytes read. */ mbsrc += clen; /* Decrement input byte count. */ mbslength -= clen; } len = src - psrc; src = psrc; /* * In the single character input case, we will have actually * processed two characters, c and nextc. Reset len back to * just a single character. */ if (mblength < len) len = mblength; /* Convert extra argument to list of characters for this mode. */ extra = makeextralist(flags, mbextra); if (!extra) { if (dlen && *dlen == 0) { errno = ENOSPC; goto out; } *mbdst = '\0'; /* can't create extra, return "" */ error = 0; goto out; } /* Look up which processing function to call. */ f = getvisfun(flags); /* * Main processing loop. * Call do_Xvis processing function one character at a time * with next character available for look-ahead. */ for (start = dst; len > 0; len--) { c = *src++; dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra); if (dst == NULL) { errno = ENOSPC; goto out; } } /* Terminate the string in the buffer. */ *dst = L'\0'; /* * Output loop. * Convert wchar_t string back to multibyte output string. * If we have hit a multi-byte conversion error on input, * output byte-by-byte here. Else use wctomb(). */ len = wcslen(start); maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1); olen = 0; + bzero(&mbstate, sizeof(mbstate)); for (dst = start; len > 0; len--) { if (!cerr) - clen = wctomb(mbdst, *dst); + clen = wcrtomb(mbdst, *dst, &mbstate); if (cerr || clen < 0) { /* * Conversion error, process as a byte(s) instead. * Examine each byte and higher-order bytes for * data. E.g., * 0x000000000000a264 -> a2 64 * 0x000000001f00a264 -> 1f 00 a2 64 */ clen = 0; wmsk = 0; for (i = sizeof(wmsk) - 1; i >= 0; i--) { shft = i * NBBY; bmsk = (uint64_t)0xffLL << shft; wmsk |= bmsk; if ((*dst & wmsk) || i == 0) mbdst[clen++] = (char)( (uint64_t)(*dst & bmsk) >> shft); } cerr = 1; } /* If this character would exceed our output limit, stop. */ if (olen + clen > (size_t)maxolen) break; /* Advance output pointer by number of bytes written. */ mbdst += clen; /* Advance buffer character pointer. */ dst++; /* Incrment output character count. */ olen += clen; } /* Terminate the output string. */ *mbdst = '\0'; if (flags & VIS_NOLOCALE) { /* Pass conversion error flag out. */ if (cerr_ptr) *cerr_ptr = cerr; } free(extra); free(pdst); free(psrc); return (int)olen; out: free(extra); free(pdst); free(psrc); free(mdst); return error; } static int istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc, int flags, const char *mbextra, int *cerr_ptr) { return istrsenvisx(mbdstp, dlen, mbsrc, mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr); } #endif #if !HAVE_SVIS /* * The "svis" variants all take an "extra" arg that is a pointer * to a NUL-terminated list of characters to be encoded, too. * These functions are useful e. g. to encode strings in such a * way so that they are not interpreted by a shell. */ char * svis(char *mbdst, int c, int flags, int nextc, const char *mbextra) { char cc[2]; int ret; cc[0] = c; cc[1] = nextc; ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL); if (ret < 0) return NULL; return mbdst + ret; } char * snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra) { char cc[2]; int ret; cc[0] = c; cc[1] = nextc; ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL); if (ret < 0) return NULL; return mbdst + ret; } int strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra) { return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL); } int strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra) { return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL); } int strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra) { return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL); } int strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, const char *mbextra) { return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL); } int strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, const char *mbextra, int *cerr_ptr) { return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr); } #endif #if !HAVE_VIS /* * vis - visually encode characters */ char * vis(char *mbdst, int c, int flags, int nextc) { char cc[2]; int ret; cc[0] = c; cc[1] = nextc; ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL); if (ret < 0) return NULL; return mbdst + ret; } char * nvis(char *mbdst, size_t dlen, int c, int flags, int nextc) { char cc[2]; int ret; cc[0] = c; cc[1] = nextc; ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL); if (ret < 0) return NULL; return mbdst + ret; } /* * strvis - visually encode characters from src into dst * * Dst must be 4 times the size of src to account for possible * expansion. The length of dst, not including the trailing NULL, * is returned. */ int strvis(char *mbdst, const char *mbsrc, int flags) { return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL); } int strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags) { return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL); } int stravis(char **mbdstp, const char *mbsrc, int flags) { *mbdstp = NULL; return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL); } /* * strvisx - visually encode characters from src into dst * * Dst must be 4 times the size of src to account for possible * expansion. The length of dst, not including the trailing NULL, * is returned. * * Strvisx encodes exactly len characters from src into dst. * This is useful for encoding a block of data. */ int strvisx(char *mbdst, const char *mbsrc, size_t len, int flags) { return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL); } int strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags) { return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL); } int strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, int *cerr_ptr) { return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr); } #endif Index: stable/11/contrib/netbsd-tests/lib/libc/locale/t_mbtowc.c =================================================================== --- stable/11/contrib/netbsd-tests/lib/libc/locale/t_mbtowc.c (revision 314633) +++ stable/11/contrib/netbsd-tests/lib/libc/locale/t_mbtowc.c (revision 314634) @@ -1,157 +1,151 @@ /* $NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $ */ /*- * Copyright (c) 2011 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /*- * Copyright (c)2007 Citrus Project, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include __COPYRIGHT("@(#) Copyright (c) 2011\ The NetBSD Foundation, inc. All rights reserved."); __RCSID("$NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $"); #include #include #include #include #include #include #include #include static void h_mbtowc(const char *locale, const char *illegal, const char *legal) { char buf[64]; size_t stateful, ret; char *str; ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); #ifdef __NetBSD__ ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL); #else if (setlocale(LC_CTYPE, locale) == NULL) { fprintf(stderr, "Locale %s not found.\n", locale); return; } #endif ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL); (void)printf("Using locale: %s\n", str); stateful = wctomb(NULL, L'\0'); (void)printf("Locale is state-%sdependent\n", stateful ? "in" : ""); /* initialize internal state */ ret = mbtowc(NULL, NULL, 0); ATF_REQUIRE(stateful ? ret : !ret); (void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL); (void)printf("Checking illegal sequence: \"%s\"\n", buf); ret = mbtowc(NULL, illegal, strlen(illegal)); (void)printf("mbtowc() returned: %zd\n", ret); ATF_REQUIRE_EQ(ret, (size_t)-1); (void)printf("errno: %s\n", strerror(errno)); ATF_REQUIRE_EQ(errno, EILSEQ); /* if this is stateless encoding, this re-initialization is not required. */ if (stateful) { /* re-initialize internal state */ ret = mbtowc(NULL, NULL, 0); ATF_REQUIRE(stateful ? ret : !ret); } /* valid multibyte sequence case */ (void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL); (void)printf("Checking legal sequence: \"%s\"\n", buf); errno = 0; ret = mbtowc(NULL, legal, strlen(legal)); (void)printf("mbtowc() returned: %zd\n", ret); ATF_REQUIRE(ret != (size_t)-1); (void)printf("errno: %s\n", strerror(errno)); ATF_REQUIRE_EQ(errno, 0); (void)printf("Ok.\n"); } ATF_TC(mbtowc); ATF_TC_HEAD(mbtowc, tc) { atf_tc_set_md_var(tc, "descr", "Checks mbtowc(3)"); } ATF_TC_BODY(mbtowc, tc) { h_mbtowc("en_US.UTF-8", "\240", "\302\240"); h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B"); h_mbtowc("ja_JP.SJIS", "\202", "\202\240"); h_mbtowc("ja_JP.eucJP", "\244", "\244\242"); -#ifndef __FreeBSD__ /* Moved last as it fails */ h_mbtowc("zh_CN.GB18030", "\241", "\241\241"); -#endif h_mbtowc("zh_TW.Big5", "\241", "\241@"); h_mbtowc("zh_TW.eucTW", "\241", "\241\241"); -#ifdef __FreeBSD__ - atf_tc_expect_fail("zh_CN.GB18030"); - h_mbtowc("zh_CN.GB18030", "\241", "\241\241"); -#endif } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, mbtowc); return atf_no_error(); } Index: stable/11/lib/libc/tests/nss/getgr_test.c =================================================================== --- stable/11/lib/libc/tests/nss/getgr_test.c (revision 314633) +++ stable/11/lib/libc/tests/nss/getgr_test.c (revision 314634) @@ -1,541 +1,546 @@ /*- * Copyright (c) 2006 Michael Bushkov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "testutil.h" enum test_methods { TEST_GETGRENT = 1, TEST_GETGRNAM = 2, TEST_GETGRGID = 4, TEST_GETGRENT_2PASS = 8, TEST_BUILD_SNAPSHOT = 16, }; static enum test_methods method = TEST_BUILD_SNAPSHOT; DECLARE_TEST_DATA(group) DECLARE_TEST_FILE_SNAPSHOT(group) DECLARE_1PASS_TEST(group) DECLARE_2PASS_TEST(group) static void clone_group(struct group *, struct group const *); static int compare_group(struct group *, struct group *, void *); static void dump_group(struct group *); static void free_group(struct group *); static void sdump_group(struct group *, char *, size_t); static int group_read_snapshot_func(struct group *, char *); static int group_check_ambiguity(struct group_test_data *, struct group *); static int group_fill_test_data(struct group_test_data *); static int group_test_correctness(struct group *, void *); static int group_test_getgrnam(struct group *, void *); static int group_test_getgrgid(struct group *, void *); static int group_test_getgrent(struct group *, void *); IMPLEMENT_TEST_DATA(group) IMPLEMENT_TEST_FILE_SNAPSHOT(group) IMPLEMENT_1PASS_TEST(group) IMPLEMENT_2PASS_TEST(group) static void clone_group(struct group *dest, struct group const *src) { ATF_REQUIRE(dest != NULL); ATF_REQUIRE(src != NULL); char **cp; int members_num; memset(dest, 0, sizeof(struct group)); if (src->gr_name != NULL) { dest->gr_name = strdup(src->gr_name); ATF_REQUIRE(dest->gr_name != NULL); } if (src->gr_passwd != NULL) { dest->gr_passwd = strdup(src->gr_passwd); ATF_REQUIRE(dest->gr_passwd != NULL); } dest->gr_gid = src->gr_gid; if (src->gr_mem != NULL) { members_num = 0; for (cp = src->gr_mem; *cp; ++cp) ++members_num; dest->gr_mem = calloc(1, (members_num + 1) * sizeof(char *)); ATF_REQUIRE(dest->gr_mem != NULL); for (cp = src->gr_mem; *cp; ++cp) { dest->gr_mem[cp - src->gr_mem] = strdup(*cp); ATF_REQUIRE(dest->gr_mem[cp - src->gr_mem] != NULL); } } } static void free_group(struct group *grp) { char **cp; ATF_REQUIRE(grp != NULL); free(grp->gr_name); free(grp->gr_passwd); for (cp = grp->gr_mem; *cp; ++cp) free(*cp); free(grp->gr_mem); } static int compare_group(struct group *grp1, struct group *grp2, void *mdata) { char **c1, **c2; if (grp1 == grp2) return (0); if (grp1 == NULL || grp2 == NULL) goto errfin; if (strcmp(grp1->gr_name, grp2->gr_name) != 0 || strcmp(grp1->gr_passwd, grp2->gr_passwd) != 0 || grp1->gr_gid != grp2->gr_gid) goto errfin; c1 = grp1->gr_mem; c2 = grp2->gr_mem; if (grp1->gr_mem == NULL || grp2->gr_mem == NULL) goto errfin; for (; *c1 && *c2; ++c1, ++c2) if (strcmp(*c1, *c2) != 0) goto errfin; if (*c1 != '\0' || *c2 != '\0') goto errfin; return 0; errfin: if (mdata == NULL) { printf("following structures are not equal:\n"); dump_group(grp1); dump_group(grp2); } return (-1); } static void sdump_group(struct group *grp, char *buffer, size_t buflen) { char **cp; int written; - written = snprintf(buffer, buflen, "%s %s %d", + written = snprintf(buffer, buflen, "%s:%s:%d:", grp->gr_name, grp->gr_passwd, grp->gr_gid); buffer += written; if (written > buflen) return; buflen -= written; if (grp->gr_mem != NULL) { if (*(grp->gr_mem) != '\0') { for (cp = grp->gr_mem; *cp; ++cp) { - written = snprintf(buffer, buflen, " %s",*cp); + written = snprintf(buffer, buflen, "%s%s", + cp == grp->gr_mem ? "" : ",", *cp); buffer += written; if (written > buflen) return; buflen -= written; if (buflen == 0) return; } } else - snprintf(buffer, buflen, " nomem"); + snprintf(buffer, buflen, "nomem"); } else - snprintf(buffer, buflen, " (null)"); + snprintf(buffer, buflen, "(null)"); } static int group_read_snapshot_func(struct group *grp, char *line) { StringList *sl; char *s, *ps, *ts; + const char *sep; int i; printf("1 line read from snapshot:\n%s\n", line); i = 0; sl = NULL; ps = line; + sep = ":"; memset(grp, 0, sizeof(struct group)); - while ((s = strsep(&ps, " ")) != NULL) { + while ((s = strsep(&ps, sep)) != NULL) { switch (i) { case 0: grp->gr_name = strdup(s); ATF_REQUIRE(grp->gr_name != NULL); break; case 1: grp->gr_passwd = strdup(s); ATF_REQUIRE(grp->gr_passwd != NULL); break; case 2: grp->gr_gid = (gid_t)strtol(s, &ts, 10); if (*ts != '\0') { free(grp->gr_name); free(grp->gr_passwd); grp->gr_name = NULL; grp->gr_passwd = NULL; return (-1); } + /* Change to parsing groups. */ + sep = ","; break; default: if (sl == NULL) { if (strcmp(s, "(null)") == 0) return (0); sl = sl_init(); ATF_REQUIRE(sl != NULL); if (strcmp(s, "nomem") != 0) { ts = strdup(s); ATF_REQUIRE(ts != NULL); sl_add(sl, ts); } } else { ts = strdup(s); ATF_REQUIRE(ts != NULL); sl_add(sl, ts); } break; } ++i; } if (i < 3) { free(grp->gr_name); free(grp->gr_passwd); memset(grp, 0, sizeof(struct group)); return (-1); } sl_add(sl, NULL); grp->gr_mem = sl->sl_str; /* NOTE: is it a dirty hack or not? */ free(sl); return (0); } static void dump_group(struct group *result) { if (result != NULL) { char buffer[1024]; sdump_group(result, buffer, sizeof(buffer)); printf("%s\n", buffer); } else printf("(null)\n"); } static int group_fill_test_data(struct group_test_data *td) { struct group *grp; setgroupent(1); while ((grp = getgrent()) != NULL) { if (group_test_correctness(grp, NULL) == 0) TEST_DATA_APPEND(group, td, grp); else return (-1); } endgrent(); return (0); } static int group_test_correctness(struct group *grp, void *mdata) { printf("testing correctness with the following data:\n"); dump_group(grp); if (grp == NULL) goto errfin; if (grp->gr_name == NULL) goto errfin; if (grp->gr_passwd == NULL) goto errfin; if (grp->gr_mem == NULL) goto errfin; printf("correct\n"); return (0); errfin: printf("incorrect\n"); return (-1); } /* group_check_ambiguity() is needed here because when doing the getgrent() * calls sequence, records from different nsswitch sources can be different, * though having the same pw_name/pw_uid */ static int group_check_ambiguity(struct group_test_data *td, struct group *pwd) { return (TEST_DATA_FIND(group, td, pwd, compare_group, NULL) != NULL ? 0 : -1); } static int group_test_getgrnam(struct group *grp_model, void *mdata) { struct group *grp; printf("testing getgrnam() with the following data:\n"); dump_group(grp_model); grp = getgrnam(grp_model->gr_name); if (group_test_correctness(grp, NULL) != 0) goto errfin; if (compare_group(grp, grp_model, NULL) != 0 && group_check_ambiguity((struct group_test_data *)mdata, grp) != 0) goto errfin; return (0); errfin: return (-1); } static int group_test_getgrgid(struct group *grp_model, void *mdata) { struct group *grp; printf("testing getgrgid() with the following data...\n"); dump_group(grp_model); grp = getgrgid(grp_model->gr_gid); if (group_test_correctness(grp, NULL) != 0 || (compare_group(grp, grp_model, NULL) != 0 && group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)) { return (-1); } else { return (0); } } static int group_test_getgrent(struct group *grp, void *mdata) { /* Only correctness can be checked when doing 1-pass test for * getgrent(). */ return (group_test_correctness(grp, NULL)); } static int run_tests(const char *snapshot_file, enum test_methods method) { struct group_test_data td, td_snap, td_2pass; int rv; TEST_DATA_INIT(group, &td, clone_group, free_group); TEST_DATA_INIT(group, &td_snap, clone_group, free_group); if (snapshot_file != NULL) { if (access(snapshot_file, W_OK | R_OK) != 0) { if (errno == ENOENT) method = TEST_BUILD_SNAPSHOT; else { printf("can't access the file %s\n", snapshot_file); rv = -1; goto fin; } } else { if (method == TEST_BUILD_SNAPSHOT) { rv = 0; goto fin; } TEST_SNAPSHOT_FILE_READ(group, snapshot_file, &td_snap, group_read_snapshot_func); } } rv = group_fill_test_data(&td); if (rv == -1) return (-1); switch (method) { case TEST_GETGRNAM: if (snapshot_file == NULL) rv = DO_1PASS_TEST(group, &td, group_test_getgrnam, (void *)&td); else rv = DO_1PASS_TEST(group, &td_snap, group_test_getgrnam, (void *)&td_snap); break; case TEST_GETGRGID: if (snapshot_file == NULL) rv = DO_1PASS_TEST(group, &td, group_test_getgrgid, (void *)&td); else rv = DO_1PASS_TEST(group, &td_snap, group_test_getgrgid, (void *)&td_snap); break; case TEST_GETGRENT: if (snapshot_file == NULL) rv = DO_1PASS_TEST(group, &td, group_test_getgrent, (void *)&td); else rv = DO_2PASS_TEST(group, &td, &td_snap, compare_group, NULL); break; case TEST_GETGRENT_2PASS: TEST_DATA_INIT(group, &td_2pass, clone_group, free_group); rv = group_fill_test_data(&td_2pass); if (rv != -1) rv = DO_2PASS_TEST(group, &td, &td_2pass, compare_group, NULL); TEST_DATA_DESTROY(group, &td_2pass); break; case TEST_BUILD_SNAPSHOT: if (snapshot_file != NULL) rv = TEST_SNAPSHOT_FILE_WRITE(group, snapshot_file, &td, sdump_group); break; default: rv = 0; break; } fin: TEST_DATA_DESTROY(group, &td_snap); TEST_DATA_DESTROY(group, &td); return (rv); } #define SNAPSHOT_FILE "snapshot_grp" ATF_TC_BODY(getgrent, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0); } ATF_TC_WITHOUT_HEAD(getgrent_with_snapshot); ATF_TC_BODY(getgrent_with_snapshot, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0); } ATF_TC_WITHOUT_HEAD(getgrent_with_two_pass); ATF_TC_BODY(getgrent_with_two_pass, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT_2PASS) == 0); } ATF_TC_WITHOUT_HEAD(getgrgid); ATF_TC_BODY(getgrgid, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0); } ATF_TC_WITHOUT_HEAD(getgrgid_with_snapshot); ATF_TC_BODY(getgrgid_with_snapshot, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0); } ATF_TC_WITHOUT_HEAD(getgrnam); ATF_TC_BODY(getgrnam, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0); } ATF_TC_WITHOUT_HEAD(getgrnam_with_snapshot); ATF_TC_BODY(getgrnam_with_snapshot, tc) { ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0); } ATF_TC_WITHOUT_HEAD(getgrent); ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, getgrent); ATF_TP_ADD_TC(tp, getgrent_with_snapshot); ATF_TP_ADD_TC(tp, getgrent_with_two_pass); ATF_TP_ADD_TC(tp, getgrgid); ATF_TP_ADD_TC(tp, getgrgid_with_snapshot); ATF_TP_ADD_TC(tp, getgrnam); ATF_TP_ADD_TC(tp, getgrnam_with_snapshot); return (atf_no_error()); } Index: stable/11 =================================================================== --- stable/11 (revision 314633) +++ stable/11 (revision 314634) Property changes on: stable/11 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r309626-309627,309659