jinclude.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * jinclude.h
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1994, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2022, D. R. Commander.
  8. * For conditions of distribution and use, see the accompanying README.ijg
  9. * file.
  10. *
  11. * This file exists to provide a single place to fix any problems with
  12. * including the wrong system include files. (Common problems are taken
  13. * care of by the standard jconfig symbols, but on really weird systems
  14. * you may have to edit this file.)
  15. *
  16. * NOTE: this file is NOT intended to be included by applications using the
  17. * JPEG library. Most applications need only include jpeglib.h.
  18. */
  19. #ifndef __JINCLUDE_H__
  20. #define __JINCLUDE_H__
  21. /* Include auto-config file to find out which system include files we need. */
  22. #include "jconfig.h" /* auto configuration options */
  23. #include "jconfigint.h"
  24. #define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */
  25. /*
  26. * Note that the core JPEG library does not require <stdio.h>;
  27. * only the default error handler and data source/destination modules do.
  28. * But we must pull it in because of the references to FILE in jpeglib.h.
  29. * You can remove those references if you want to compile without <stdio.h>.
  30. */
  31. #include <stddef.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. /*
  36. * These macros/inline functions facilitate using Microsoft's "safe string"
  37. * functions with Visual Studio builds without the need to scatter #ifdefs
  38. * throughout the code base.
  39. */
  40. #ifdef _MSC_VER
  41. #define SNPRINTF(str, n, format, ...) \
  42. _snprintf_s(str, n, _TRUNCATE, format, ##__VA_ARGS__)
  43. #else
  44. #define SNPRINTF snprintf
  45. #endif
  46. #ifndef NO_GETENV
  47. #ifdef _MSC_VER
  48. static INLINE int GETENV_S(char *buffer, size_t buffer_size, const char *name)
  49. {
  50. size_t required_size;
  51. return (int)getenv_s(&required_size, buffer, buffer_size, name);
  52. }
  53. #else /* _MSC_VER */
  54. #include <errno.h>
  55. /* This provides a similar interface to the Microsoft/C11 getenv_s() function,
  56. * but other than parameter validation, it has no advantages over getenv().
  57. */
  58. static INLINE int GETENV_S(char *buffer, size_t buffer_size, const char *name)
  59. {
  60. char *env;
  61. if (!buffer) {
  62. if (buffer_size == 0)
  63. return 0;
  64. else
  65. return (errno = EINVAL);
  66. }
  67. if (buffer_size == 0)
  68. return (errno = EINVAL);
  69. if (!name) {
  70. *buffer = 0;
  71. return 0;
  72. }
  73. env = getenv(name);
  74. if (!env)
  75. {
  76. *buffer = 0;
  77. return 0;
  78. }
  79. if (strlen(env) + 1 > buffer_size) {
  80. *buffer = 0;
  81. return ERANGE;
  82. }
  83. strncpy(buffer, env, buffer_size);
  84. return 0;
  85. }
  86. #endif /* _MSC_VER */
  87. #endif /* NO_GETENV */
  88. #ifndef NO_PUTENV
  89. #ifdef _WIN32
  90. #define PUTENV_S(name, value) _putenv_s(name, value)
  91. #else
  92. /* This provides a similar interface to the Microsoft _putenv_s() function, but
  93. * other than parameter validation, it has no advantages over setenv().
  94. */
  95. static INLINE int PUTENV_S(const char *name, const char *value)
  96. {
  97. if (!name || !value)
  98. return (errno = EINVAL);
  99. setenv(name, value, 1);
  100. return errno;
  101. }
  102. #endif /* _WIN32 */
  103. #endif /* NO_PUTENV */
  104. #ifdef WITH_SANITIZER
  105. # define malloc(sz) calloc((sz), 1)
  106. #endif
  107. #endif /* JINCLUDE_H */