MSVC: Eliminate C4996 warnings in API libs

The primary purpose of this is to encourage adoption of libjpeg-turbo in
downstream Windows projects that forbid the use of "deprecated"
functions.  libjpeg-turbo's usage of those functions was not actually
unsafe, because:

- libjpeg-turbo always checks the return value of fopen() and ensures
  that a NULL filename can never be passed to it.

- libjpeg-turbo always checks the return value of getenv() and never
  passes a NULL argument to it.

- The sprintf() calls in format_message() (jerror.c) could never
  overflow the destination string buffer or leave it unterminated as
  long as the buffer was at least JMSG_LENGTH_MAX bytes in length, as
  instructed. (Regardless, this commit replaces those calls with
  snprintf() calls.)

- libjpeg-turbo never uses sscanf() to read strings or multi-byte
  character arrays.

- Because of b7d6e84d6a, wrjpgcom
  explicitly checks the bounds of the source and destination strings
  before calling strcat() and strcpy().

- libjpeg-turbo always ensures that the destination string is
  terminated when using strncpy().
  (548490fe5e made this explicit.)

Regarding thread safety:

Technically speaking, getenv() is not thread-safe, because the returned
pointer may be invalidated if another thread sets the same environment
variable between the time that the first thread calls getenv() and the
time that that thread uses the return value.  In practice, however, this
could only occur with libjpeg-turbo if:

(1) A multithreaded calling application used the deprecated and
undocumented TJFLAG_FORCEMMX/TJFLAG_FORCESSE/TJFLAG_FORCESSE2 flags in
the TurboJPEG API or set one of the corresponding environment variables
(which are only intended for testing purposes.)  Since the TurboJPEG API
library only ever passed string constants to putenv(), the only inherent
risk (i.e. the only risk introduced by the library and not the calling
application) was that the SIMD extensions may have read an incorrect
value from one of the aforementioned environment variables.

or

(2) A multithreaded calling application modified the value of the
JPEGMEM environment variable in one thread while another thread was
reading the value of that environment variable (in the body of
jpeg_create_compress() or jpeg_create_decompress().)  Given that the
libjpeg API provides a thread-safe way for applications to modify the
default memory limit without using the JPEGMEM environment variable,
direct modification of that environment variable by calling applications
is not supported.

Microsoft's implementation of getenv_s() does not claim to be
thread-safe either, so this commit uses getenv_s() solely to mollify
Visual Studio.  New inline functions and macros (GETENV_S() and
PUTENV_S) wrap getenv_s()/_putenv_s() when building for Visual Studio
and getenv()/setenv() otherwise, but GETENV_S()/PUTENV_S() provide no
advantages over getenv()/setenv() other than parameter validation.  They
are implemented solely for convenience.

Technically speaking, strerror() is not thread-safe, because the
returned pointer may be invalidated if another thread changes the locale
and/or calls strerror() between the time that the first thread calls
strerror() and the time that that thread uses the return value.  In
practice, however, this could only occur with libjpeg-turbo if a
multithreaded calling application encountered a file I/O error in
tjLoadImage() or tjSaveImage().  Since both of those functions
immediately copy the string returned from strerror() into a thread-local
buffer, the risk is minimal, and the worst case would involve an
incorrect error string being reported to the calling application.
Regardless, this commit uses strerror_s() in the TurboJPEG API library
when building for Visual Studio.  Note that strerror_r() could have been
used on Un*x systems, but it would have been necessary to handle both
the POSIX and GNU implementations of that function and perform
widespread compatibility testing.  Such is left as an exercise for
another day.

Fixes #568
This commit is contained in:
DRC
2022-02-10 11:33:49 -06:00
parent ab6cae6f3b
commit 607b668ff9
22 changed files with 414 additions and 112 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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"

View File

@@ -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]);
}

View File

@@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
* 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 <errno.h>
/* 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 */

View File

@@ -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;

View File

@@ -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 */

View File

@@ -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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

View File

@@ -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, <stdio.h> */

View File

@@ -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 <ctype.h> /* to declare isdigit(), isspace() */

View File

@@ -3,7 +3,7 @@
*
* Copyright 2009 Pierre Ossman <ossman@cendio.se> 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
}

View File

@@ -3,7 +3,7 @@
*
* Copyright 2009 Pierre Ossman <ossman@cendio.se> 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
}

View File

@@ -2,7 +2,7 @@
* jsimd_i386.c
*
* Copyright 2009 Pierre Ossman <ossman@cendio.se> 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
}

View File

@@ -2,7 +2,7 @@
* jsimd_x86_64.c
*
* Copyright 2009 Pierre Ossman <ossman@cendio.se> 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
}

165
strtest.c Normal file
View File

@@ -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;
}

View File

@@ -26,6 +26,10 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@@ -27,11 +27,6 @@
*/
#ifdef _WIN32
#ifndef __MINGW32__
#include <stdio.h>
#define snprintf(str, n, format, ...) \
_snprintf_s(str, n, _TRUNCATE, format, ##__VA_ARGS__)
#endif
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif

View File

@@ -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 <stdlib.h>
#include <string.h>
#include "turbojpeg.h"
#ifdef WIN32
#include "tjutil.h"
#endif
#include "jinclude.h"
#include <jni.h>
#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:

View File

@@ -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)) {

View File

@@ -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, <stdio.h> */