diff --git a/CMakeLists.txt b/CMakeLists.txt index 639ea570..1198eced 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -360,7 +360,7 @@ if(MSVC) endif() endforeach() endif() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /wd4996") + add_definitions(-D_CRT_NONSTDC_NO_WARNINGS) endif() if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID STREQUAL "Clang") @@ -709,6 +709,8 @@ if(WITH_FUZZ) add_subdirectory(fuzz) endif() +add_executable(strtest strtest.c) + add_subdirectory(md5) if(MSVC_IDE OR XCODE) diff --git a/cjpeg.c b/cjpeg.c index 41ef5c18..41020a8f 100644 --- a/cjpeg.c +++ b/cjpeg.c @@ -5,7 +5,7 @@ * Copyright (C) 1991-1998, Thomas G. Lane. * Modified 2003-2011 by Guido Vollbeding. * libjpeg-turbo Modifications: - * Copyright (C) 2010, 2013-2014, 2017, 2019-2021, D. R. Commander. + * Copyright (C) 2010, 2013-2014, 2017, 2019-2022, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -27,6 +27,10 @@ * works regardless of which command line style is used. */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #ifdef CJPEG_FUZZER #define JPEG_INTERNALS #endif diff --git a/djpeg.c b/djpeg.c index dfd6d900..5190e687 100644 --- a/djpeg.c +++ b/djpeg.c @@ -28,6 +28,10 @@ * works regardless of which command line style is used. */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ #include "jversion.h" /* for version message */ #include "jconfigint.h" diff --git a/jerror.c b/jerror.c index 936c4f5d..d5447029 100644 --- a/jerror.c +++ b/jerror.c @@ -3,8 +3,8 @@ * * This file was part of the Independent JPEG Group's software: * Copyright (C) 1991-1998, Thomas G. Lane. - * It was modified by The libjpeg-turbo Project to include only code relevant - * to libjpeg-turbo. + * libjpeg-turbo Modifications: + * Copyright (C) 2022, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -189,13 +189,13 @@ format_message(j_common_ptr cinfo, char *buffer) /* Format the message into the passed buffer */ if (isstring) - sprintf(buffer, msgtext, err->msg_parm.s); + snprintf(buffer, JMSG_LENGTH_MAX, msgtext, err->msg_parm.s); else - sprintf(buffer, msgtext, - err->msg_parm.i[0], err->msg_parm.i[1], - err->msg_parm.i[2], err->msg_parm.i[3], - err->msg_parm.i[4], err->msg_parm.i[5], - err->msg_parm.i[6], err->msg_parm.i[7]); + snprintf(buffer, JMSG_LENGTH_MAX, msgtext, + err->msg_parm.i[0], err->msg_parm.i[1], + err->msg_parm.i[2], err->msg_parm.i[3], + err->msg_parm.i[4], err->msg_parm.i[5], + err->msg_parm.i[6], err->msg_parm.i[7]); } diff --git a/jinclude.h b/jinclude.h index 6ed982d3..120614b2 100644 --- a/jinclude.h +++ b/jinclude.h @@ -3,8 +3,8 @@ * * This file was part of the Independent JPEG Group's software: * Copyright (C) 1991-1994, Thomas G. Lane. - * It was modified by The libjpeg-turbo Project to include only code relevant - * to libjpeg-turbo. + * libjpeg-turbo Modifications: + * Copyright (C) 2022, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -17,10 +17,13 @@ * JPEG library. Most applications need only include jpeglib.h. */ +#ifndef __JINCLUDE_H__ +#define __JINCLUDE_H__ /* Include auto-config file to find out which system include files we need. */ #include "jconfig.h" /* auto configuration options */ +#include "jconfigint.h" #define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */ /* @@ -34,3 +37,97 @@ #include #include #include + +/* + * These macros/inline functions facilitate using Microsoft's "safe string" + * functions with Visual Studio builds without the need to scatter #ifdefs + * throughout the code base. + */ + + +#ifndef NO_GETENV + +#ifdef _MSC_VER + +static INLINE int GETENV_S(char *buffer, size_t buffer_size, const char *name) +{ + size_t required_size; + + return (int)getenv_s(&required_size, buffer, buffer_size, name); +} + +#else /* _MSC_VER */ + +#include + +/* This provides a similar interface to the Microsoft/C11 getenv_s() function, + * but other than parameter validation, it has no advantages over getenv(). + */ + +static INLINE int GETENV_S(char *buffer, size_t buffer_size, const char *name) +{ + char *env; + + if (!buffer) { + if (buffer_size == 0) + return 0; + else + return (errno = EINVAL); + } + if (buffer_size == 0) + return (errno = EINVAL); + if (!name) { + *buffer = 0; + return 0; + } + + env = getenv(name); + if (!env) + { + *buffer = 0; + return 0; + } + + if (strlen(env) + 1 > buffer_size) { + *buffer = 0; + return ERANGE; + } + + strncpy(buffer, env, buffer_size); + + return 0; +} + +#endif /* _MSC_VER */ + +#endif /* NO_GETENV */ + + +#ifndef NO_PUTENV + +#ifdef _WIN32 + +#define PUTENV_S(name, value) _putenv_s(name, value) + +#else + +/* This provides a similar interface to the Microsoft _putenv_s() function, but + * other than parameter validation, it has no advantages over setenv(). + */ + +static INLINE int PUTENV_S(const char *name, const char *value) +{ + if (!name || !value) + return (errno = EINVAL); + + setenv(name, value, 1); + + return errno; +} + +#endif /* _WIN32 */ + +#endif /* NO_PUTENV */ + + +#endif /* JINCLUDE_H */ diff --git a/jmemmgr.c b/jmemmgr.c index d053813d..8f5a4ab1 100644 --- a/jmemmgr.c +++ b/jmemmgr.c @@ -4,7 +4,7 @@ * This file was part of the Independent JPEG Group's software: * Copyright (C) 1991-1997, Thomas G. Lane. * libjpeg-turbo Modifications: - * Copyright (C) 2016, 2021, D. R. Commander. + * Copyright (C) 2016, 2021-2022, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -1156,12 +1156,16 @@ jinit_memory_mgr(j_common_ptr cinfo) */ #ifndef NO_GETENV { - char *memenv; + char memenv[30] = { 0 }; - if ((memenv = getenv("JPEGMEM")) != NULL) { + if (!GETENV_S(memenv, 30, "JPEGMEM") && strlen(memenv) > 0) { char ch = 'x'; +#ifdef _MSC_VER + if (sscanf_s(memenv, "%ld%c", &max_to_use, &ch, 1) > 0) { +#else if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { +#endif if (ch == 'm' || ch == 'M') max_to_use *= 1000L; mem->pub.max_memory_to_use = max_to_use * 1000L; diff --git a/jpegtran.c b/jpegtran.c index 7dd27232..4c7ab772 100644 --- a/jpegtran.c +++ b/jpegtran.c @@ -4,7 +4,7 @@ * This file was part of the Independent JPEG Group's software: * Copyright (C) 1995-2019, Thomas G. Lane, Guido Vollbeding. * libjpeg-turbo Modifications: - * Copyright (C) 2010, 2014, 2017, 2019-2021, D. R. Commander. + * Copyright (C) 2010, 2014, 2017, 2019-2022, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -14,6 +14,10 @@ * provides some lossless and sort-of-lossless transformations of JPEG data. */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ #include "transupp.h" /* Support routines for jpegtran */ #include "jversion.h" /* for version message */ diff --git a/md5/md5hl.c b/md5/md5hl.c index 8a4a762f..849a1366 100644 --- a/md5/md5hl.c +++ b/md5/md5hl.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * libjpeg-turbo Modifications: - * Copyright (C)2016, 2018-2019 D. R. Commander. All Rights Reserved. + * Copyright (C)2016, 2018-2019, 2022 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -34,6 +34,10 @@ * ---------------------------------------------------------------------------- */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include #include #include diff --git a/rdjpgcom.c b/rdjpgcom.c index fc0c08c0..9910a634 100644 --- a/rdjpgcom.c +++ b/rdjpgcom.c @@ -15,6 +15,10 @@ * JPEG markers. */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */ #include "jinclude.h" /* get auto-config symbols, */ diff --git a/rdswitch.c b/rdswitch.c index f9ed70f5..33449c86 100644 --- a/rdswitch.c +++ b/rdswitch.c @@ -17,6 +17,10 @@ * -sample HxV[,HxV,...] Set component sampling factors */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ #include /* to declare isdigit(), isspace() */ diff --git a/simd/arm/aarch32/jsimd.c b/simd/arm/aarch32/jsimd.c index fac55dfb..e3adf23d 100644 --- a/simd/arm/aarch32/jsimd.c +++ b/simd/arm/aarch32/jsimd.c @@ -3,7 +3,7 @@ * * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies). - * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, D. R. Commander. + * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2022, D. R. Commander. * Copyright (C) 2015-2016, 2018, Matthieu Darbois. * Copyright (C) 2019, Google LLC. * Copyright (C) 2020, Arm Limited. @@ -105,7 +105,7 @@ LOCAL(void) init_simd(void) { #ifndef NO_GETENV - char *env = NULL; + char env[2] = { 0 }; #endif #if !defined(__ARM_NEON__) && (defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)) int bufsize = 1024; /* an initial guess for the line buffer size limit */ @@ -131,14 +131,11 @@ init_simd(void) #ifndef NO_GETENV /* Force different settings through environment variables */ - env = getenv("JSIMD_FORCENEON"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCENEON") && !strcmp(env, "1")) simd_support = JSIMD_NEON; - env = getenv("JSIMD_FORCENONE"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCENONE") && !strcmp(env, "1")) simd_support = 0; - env = getenv("JSIMD_NOHUFFENC"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_NOHUFFENC") && !strcmp(env, "1")) simd_huffman = 0; #endif } diff --git a/simd/arm/aarch64/jsimd.c b/simd/arm/aarch64/jsimd.c index 8570b82c..604d5472 100644 --- a/simd/arm/aarch64/jsimd.c +++ b/simd/arm/aarch64/jsimd.c @@ -3,7 +3,7 @@ * * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies). - * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2020, D. R. Commander. + * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2020, 2022, D. R. Commander. * Copyright (C) 2015-2016, 2018, Matthieu Darbois. * Copyright (C) 2020, Arm Limited. * @@ -125,7 +125,7 @@ LOCAL(void) init_simd(void) { #ifndef NO_GETENV - char *env = NULL; + char env[2] = { 0 }; #endif #if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) int bufsize = 1024; /* an initial guess for the line buffer size limit */ @@ -147,24 +147,19 @@ init_simd(void) #ifndef NO_GETENV /* Force different settings through environment variables */ - env = getenv("JSIMD_FORCENEON"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCENEON") && !strcmp(env, "1")) simd_support = JSIMD_NEON; - env = getenv("JSIMD_FORCENONE"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCENONE") && !strcmp(env, "1")) simd_support = 0; - env = getenv("JSIMD_NOHUFFENC"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_NOHUFFENC") && !strcmp(env, "1")) simd_huffman = 0; - env = getenv("JSIMD_FASTLD3"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FASTLD3") && !strcmp(env, "1")) simd_features |= JSIMD_FASTLD3; - if ((env != NULL) && (strcmp(env, "0") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FASTLD3") && !strcmp(env, "0")) simd_features &= ~JSIMD_FASTLD3; - env = getenv("JSIMD_FASTST3"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FASTST3") && !strcmp(env, "1")) simd_features |= JSIMD_FASTST3; - if ((env != NULL) && (strcmp(env, "0") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FASTST3") && !strcmp(env, "0")) simd_features &= ~JSIMD_FASTST3; #endif } diff --git a/simd/i386/jsimd.c b/simd/i386/jsimd.c index 563949a0..80bc821f 100644 --- a/simd/i386/jsimd.c +++ b/simd/i386/jsimd.c @@ -2,7 +2,7 @@ * jsimd_i386.c * * Copyright 2009 Pierre Ossman for Cendio AB - * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, D. R. Commander. + * Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2022, D. R. Commander. * Copyright (C) 2015-2016, 2018, Matthieu Darbois. * * Based on the x86 SIMD extension for IJG JPEG library, @@ -44,7 +44,7 @@ LOCAL(void) init_simd(void) { #ifndef NO_GETENV - char *env = NULL; + char env[2] = { 0 }; #endif if (simd_support != ~0U) @@ -54,26 +54,19 @@ init_simd(void) #ifndef NO_GETENV /* Force different settings through environment variables */ - env = getenv("JSIMD_FORCEMMX"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCEMMX") && !strcmp(env, "1")) simd_support &= JSIMD_MMX; - env = getenv("JSIMD_FORCE3DNOW"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCE3DNOW") && !strcmp(env, "1")) simd_support &= JSIMD_3DNOW | JSIMD_MMX; - env = getenv("JSIMD_FORCESSE"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCESSE") && !strcmp(env, "1")) simd_support &= JSIMD_SSE | JSIMD_MMX; - env = getenv("JSIMD_FORCESSE2"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCESSE2") && !strcmp(env, "1")) simd_support &= JSIMD_SSE2; - env = getenv("JSIMD_FORCEAVX2"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCEAVX2") && !strcmp(env, "1")) simd_support &= JSIMD_AVX2; - env = getenv("JSIMD_FORCENONE"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCENONE") && !strcmp(env, "1")) simd_support = 0; - env = getenv("JSIMD_NOHUFFENC"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_NOHUFFENC") && !strcmp(env, "1")) simd_huffman = 0; #endif } diff --git a/simd/x86_64/jsimd.c b/simd/x86_64/jsimd.c index eb766799..584a010a 100644 --- a/simd/x86_64/jsimd.c +++ b/simd/x86_64/jsimd.c @@ -2,7 +2,7 @@ * jsimd_x86_64.c * * Copyright 2009 Pierre Ossman for Cendio AB - * Copyright (C) 2009-2011, 2014, 2016, 2018, D. R. Commander. + * Copyright (C) 2009-2011, 2014, 2016, 2018, 2022, D. R. Commander. * Copyright (C) 2015-2016, 2018, Matthieu Darbois. * * Based on the x86 SIMD extension for IJG JPEG library, @@ -44,7 +44,7 @@ LOCAL(void) init_simd(void) { #ifndef NO_GETENV - char *env = NULL; + char env[2] = { 0 }; #endif if (simd_support != ~0U) @@ -54,17 +54,13 @@ init_simd(void) #ifndef NO_GETENV /* Force different settings through environment variables */ - env = getenv("JSIMD_FORCESSE2"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCESSE2") && !strcmp(env, "1")) simd_support &= JSIMD_SSE2; - env = getenv("JSIMD_FORCEAVX2"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCEAVX2") && !strcmp(env, "1")) simd_support &= JSIMD_AVX2; - env = getenv("JSIMD_FORCENONE"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_FORCENONE") && !strcmp(env, "1")) simd_support = 0; - env = getenv("JSIMD_NOHUFFENC"); - if ((env != NULL) && (strcmp(env, "1") == 0)) + if (!GETENV_S(env, 2, "JSIMD_NOHUFFENC") && !strcmp(env, "1")) simd_huffman = 0; #endif } diff --git a/strtest.c b/strtest.c new file mode 100644 index 00000000..ceaad91a --- /dev/null +++ b/strtest.c @@ -0,0 +1,165 @@ +/* + * Copyright (C)2022 D. R. Commander. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * - 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. + * - Neither the name of the libjpeg-turbo Project 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDERS 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 "jinclude.h" + + +#define CHECK_VALUE(actual, expected, desc) \ + if (actual != expected) { \ + printf("ERROR in line %d: " desc " is %d, should be %d\n", \ + __LINE__, actual, expected); \ + return -1; \ + } + +#define CHECK_ERRNO(errno_return, expected_errno) \ + CHECK_VALUE(errno_return, expected_errno, "Return value") \ + CHECK_VALUE(errno, expected_errno, "errno") \ + + +#ifdef _MSC_VER + +void invalid_parameter_handler(const wchar_t *expression, + const wchar_t *function, const wchar_t *file, + unsigned int line, uintptr_t pReserved) +{ +} + +#endif + + +int main(int argc, char **argv) +{ + int err; + char env[3]; + +#ifdef _MSC_VER + _set_invalid_parameter_handler(invalid_parameter_handler); +#endif + + /***************************************************************************/ + +#ifndef NO_PUTENV + + printf("PUTENV_S():\n"); + + errno = 0; + err = PUTENV_S(NULL, "12"); + CHECK_ERRNO(err, EINVAL); + + errno = 0; + err = PUTENV_S("TESTENV", NULL); + CHECK_ERRNO(err, EINVAL); + + errno = 0; + err = PUTENV_S("TESTENV", "12"); + CHECK_ERRNO(err, 0); + + printf("SUCCESS!\n\n"); + +#endif + + /***************************************************************************/ + +#ifndef NO_GETENV + + printf("GETENV_S():\n"); + + errno = 0; + env[0] = 1; + env[1] = 2; + env[2] = 3; + err = GETENV_S(env, 3, NULL); + CHECK_ERRNO(err, 0); + CHECK_VALUE(env[0], 0, "env[0]"); + CHECK_VALUE(env[1], 2, "env[1]"); + CHECK_VALUE(env[2], 3, "env[2]"); + + errno = 0; + env[0] = 1; + env[1] = 2; + env[2] = 3; + err = GETENV_S(env, 3, "TESTENV2"); + CHECK_ERRNO(err, 0); + CHECK_VALUE(env[0], 0, "env[0]"); + CHECK_VALUE(env[1], 2, "env[1]"); + CHECK_VALUE(env[2], 3, "env[2]"); + + errno = 0; + err = GETENV_S(NULL, 3, "TESTENV"); + CHECK_ERRNO(err, EINVAL); + + errno = 0; + err = GETENV_S(NULL, 0, "TESTENV"); + CHECK_ERRNO(err, 0); + + errno = 0; + env[0] = 1; + err = GETENV_S(env, 0, "TESTENV"); + CHECK_ERRNO(err, EINVAL); + CHECK_VALUE(env[0], 1, "env[0]"); + + errno = 0; + env[0] = 1; + env[1] = 2; + env[2] = 3; + err = GETENV_S(env, 1, "TESTENV"); + CHECK_VALUE(err, ERANGE, "Return value"); + CHECK_VALUE(errno, 0, "errno"); + CHECK_VALUE(env[0], 0, "env[0]"); + CHECK_VALUE(env[1], 2, "env[1]"); + CHECK_VALUE(env[2], 3, "env[2]"); + + errno = 0; + env[0] = 1; + env[1] = 2; + env[2] = 3; + err = GETENV_S(env, 2, "TESTENV"); + CHECK_VALUE(err, ERANGE, "Return value"); + CHECK_VALUE(errno, 0, "errno"); + CHECK_VALUE(env[0], 0, "env[0]"); + CHECK_VALUE(env[1], 2, "env[1]"); + CHECK_VALUE(env[2], 3, "env[2]"); + + errno = 0; + env[0] = 1; + env[1] = 2; + env[2] = 3; + err = GETENV_S(env, 3, "TESTENV"); + CHECK_ERRNO(err, 0); + CHECK_VALUE(env[0], '1', "env[0]"); + CHECK_VALUE(env[1], '2', "env[1]"); + CHECK_VALUE(env[2], 0, "env[2]"); + + printf("SUCCESS!\n\n"); + +#endif + + /***************************************************************************/ + + return 0; +} diff --git a/tjbench.c b/tjbench.c index 344cb4cb..b7878b61 100644 --- a/tjbench.c +++ b/tjbench.c @@ -26,6 +26,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include #include #include diff --git a/tjexample.c b/tjexample.c index a9cd865b..505c9dd4 100644 --- a/tjexample.c +++ b/tjexample.c @@ -1,6 +1,6 @@ /* - * Copyright (C)2011-2012, 2014-2015, 2017, 2019, 2021 D. R. Commander. - * All Rights Reserved. + * Copyright (C)2011-2012, 2014-2015, 2017, 2019, 2021-2022 + * D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -32,6 +32,10 @@ * images using the TurboJPEG C API */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include #include #include diff --git a/tjunittest.c b/tjunittest.c index f59939fd..bda65f95 100644 --- a/tjunittest.c +++ b/tjunittest.c @@ -1,5 +1,6 @@ /* - * Copyright (C)2009-2014, 2017-2019 D. R. Commander. All Rights Reserved. + * Copyright (C)2009-2014, 2017-2019, 2022 D. R. Commander. + * All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,6 +31,10 @@ * This program tests the various code paths in the TurboJPEG C Wrapper */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include #include #include diff --git a/tjutil.h b/tjutil.h index 8542bab9..fc8a17a7 100644 --- a/tjutil.h +++ b/tjutil.h @@ -27,11 +27,6 @@ */ #ifdef _WIN32 -#ifndef __MINGW32__ -#include -#define snprintf(str, n, format, ...) \ - _snprintf_s(str, n, _TRUNCATE, format, ##__VA_ARGS__) -#endif #define strcasecmp stricmp #define strncasecmp strnicmp #endif diff --git a/turbojpeg-jni.c b/turbojpeg-jni.c index 4fa6dfdf..dc80e23c 100644 --- a/turbojpeg-jni.c +++ b/turbojpeg-jni.c @@ -1,5 +1,5 @@ /* - * Copyright (C)2011-2021 D. R. Commander. All Rights Reserved. + * Copyright (C)2011-2022 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -29,9 +29,7 @@ #include #include #include "turbojpeg.h" -#ifdef WIN32 -#include "tjutil.h" -#endif +#include "jinclude.h" #include #include "java/org_libjpegturbo_turbojpeg_TJCompressor.h" #include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h" @@ -88,10 +86,7 @@ BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "handle", "J")); \ handle = (tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid); -#ifdef _WIN32 -#define setenv(envvar, value, dummy) _putenv_s(envvar, value) -#endif - +#ifndef NO_PUTENV #define PROP2ENV(property, envvar) { \ if ((jName = (*env)->NewStringUTF(env, property)) != NULL) { \ jboolean exception; \ @@ -99,11 +94,12 @@ exception = (*env)->ExceptionCheck(env); \ if (jValue && !exception && \ (value = (*env)->GetStringUTFChars(env, jValue, 0)) != NULL) { \ - setenv(envvar, value, 1); \ + PUTENV_S(envvar, value); \ (*env)->ReleaseStringUTFChars(env, jValue, value); \ } \ } \ } +#endif #define SAFE_RELEASE(javaArray, cArray) { \ if (javaArray && cArray) \ @@ -122,10 +118,12 @@ static int ProcessSystemProperties(JNIEnv *env) BAILIF0(mid = (*env)->GetStaticMethodID(env, cls, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;")); +#ifndef NO_PUTENV PROP2ENV("turbojpeg.optimize", "TJ_OPTIMIZE"); PROP2ENV("turbojpeg.arithmetic", "TJ_ARITHMETIC"); PROP2ENV("turbojpeg.restart", "TJ_RESTART"); PROP2ENV("turbojpeg.progressive", "TJ_PROGRESSIVE"); +#endif return 0; bailout: diff --git a/turbojpeg.c b/turbojpeg.c index d780dc25..71cbc46b 100644 --- a/turbojpeg.c +++ b/turbojpeg.c @@ -196,10 +196,19 @@ static int cs2pf[JPEG_NUMCS] = { snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \ retval = -1; goto bailout; \ } +#ifdef _MSC_VER +#define THROW_UNIX(m) { \ + char strerrorBuf[80] = { 0 }; \ + strerror_s(strerrorBuf, 80, errno); \ + snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, strerrorBuf); \ + retval = -1; goto bailout; \ +} +#else #define THROW_UNIX(m) { \ snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, strerror(errno)); \ retval = -1; goto bailout; \ } +#endif #define THROW(m) { \ snprintf(this->errStr, JMSG_LENGTH_MAX, "%s", m); \ this->isInstanceError = TRUE; THROWG(m) \ @@ -271,7 +280,7 @@ static void setCompDefaults(struct jpeg_compress_struct *cinfo, int flags) { #ifndef NO_GETENV - char *env = NULL; + char env[7] = { 0 }; #endif cinfo->in_color_space = pf2cs[pixelFormat]; @@ -279,18 +288,21 @@ static void setCompDefaults(struct jpeg_compress_struct *cinfo, jpeg_set_defaults(cinfo); #ifndef NO_GETENV - if ((env = getenv("TJ_OPTIMIZE")) != NULL && strlen(env) > 0 && - !strcmp(env, "1")) + if (!GETENV_S(env, 7, "TJ_OPTIMIZE") && !strcmp(env, "1")) cinfo->optimize_coding = TRUE; - if ((env = getenv("TJ_ARITHMETIC")) != NULL && strlen(env) > 0 && - !strcmp(env, "1")) + if (!GETENV_S(env, 7, "TJ_ARITHMETIC") && !strcmp(env, "1")) cinfo->arith_code = TRUE; - if ((env = getenv("TJ_RESTART")) != NULL && strlen(env) > 0) { + if (!GETENV_S(env, 7, "TJ_RESTART") && strlen(env) > 0) { int temp = -1; char tempc = 0; +#ifdef _MSC_VER + if (sscanf_s(env, "%d%c", &temp, &tempc, 1) >= 1 && temp >= 0 && + temp <= 65535) { +#else if (sscanf(env, "%d%c", &temp, &tempc) >= 1 && temp >= 0 && temp <= 65535) { +#endif if (toupper(tempc) == 'B') { cinfo->restart_interval = temp; cinfo->restart_in_rows = 0; @@ -317,8 +329,7 @@ static void setCompDefaults(struct jpeg_compress_struct *cinfo, if (flags & TJFLAG_PROGRESSIVE) jpeg_simple_progression(cinfo); #ifndef NO_GETENV - else if ((env = getenv("TJ_PROGRESSIVE")) != NULL && strlen(env) > 0 && - !strcmp(env, "1")) + else if (!GETENV_S(env, 7, "TJ_PROGRESSIVE") && !strcmp(env, "1")) jpeg_simple_progression(cinfo); #endif @@ -697,9 +708,9 @@ DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf, cinfo->image_height = height; #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif if (flags & TJFLAG_NOREALLOC) { @@ -799,9 +810,9 @@ DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf, cinfo->image_height = height; #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags); @@ -1009,9 +1020,9 @@ DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle, cinfo->image_height = height; #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif if (flags & TJFLAG_NOREALLOC) { @@ -1295,9 +1306,9 @@ DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf, THROW("tjDecompress2(): Invalid argument"); #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif if (flags & TJFLAG_LIMITSCANS) { @@ -1471,9 +1482,9 @@ DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle, dinfo->image_height = height; #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif dinfo->progressive_mode = dinfo->inputctl->has_multiple_scans = FALSE; @@ -1643,9 +1654,9 @@ DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle, THROW("tjDecompressToYUVPlanes(): Invalid argument"); #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif if (flags & TJFLAG_LIMITSCANS) { @@ -1906,9 +1917,9 @@ DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf, THROW("tjTransform(): Invalid argument"); #ifndef NO_PUTENV - if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); - else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1"); - else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); + if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1"); + else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1"); + else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1"); #endif if (flags & TJFLAG_LIMITSCANS) { @@ -2075,7 +2086,11 @@ DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width, this = (tjinstance *)handle; cinfo = &this->cinfo; +#ifdef _MSC_VER + if (fopen_s(&file, filename, "rb") || file == NULL) +#else if ((file = fopen(filename, "rb")) == NULL) +#endif THROW_UNIX("tjLoadImage(): Cannot open input file"); if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF) @@ -2171,7 +2186,11 @@ DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer, this = (tjinstance *)handle; dinfo = &this->dinfo; +#ifdef _MSC_VER + if (fopen_s(&file, filename, "wb") || file == NULL) +#else if ((file = fopen(filename, "wb")) == NULL) +#endif THROW_UNIX("tjSaveImage(): Cannot open output file"); if (setjmp(this->jerr.setjmp_buffer)) { diff --git a/wrjpgcom.c b/wrjpgcom.c index 6a4dc6bc..3ee08a0e 100644 --- a/wrjpgcom.c +++ b/wrjpgcom.c @@ -4,7 +4,7 @@ * This file was part of the Independent JPEG Group's software: * Copyright (C) 1994-1997, Thomas G. Lane. * libjpeg-turbo Modifications: - * Copyright (C) 2014, D. R. Commander. + * Copyright (C) 2014, 2022, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -14,6 +14,10 @@ * JPEG markers. */ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */ #include "jinclude.h" /* get auto-config symbols, */