e8d983ac171fa0e47c87f0e0593518ceddde0bdb
[platform/upstream/libjpeg-turbo.git] / jinclude.h
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
20 #ifndef __JINCLUDE_H__
21 #define __JINCLUDE_H__
22
23 /* Include auto-config file to find out which system include files we need. */
24
25 #include "jconfig.h"            /* auto configuration options */
26 #include "jconfigint.h"
27 #define JCONFIG_INCLUDED        /* so that jpeglib.h doesn't do it again */
28
29 /*
30  * Note that the core JPEG library does not require <stdio.h>;
31  * only the default error handler and data source/destination modules do.
32  * But we must pull it in because of the references to FILE in jpeglib.h.
33  * You can remove those references if you want to compile without <stdio.h>.
34  */
35
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 /*
42  * These macros/inline functions facilitate using Microsoft's "safe string"
43  * functions with Visual Studio builds without the need to scatter #ifdefs
44  * throughout the code base.
45  */
46
47
48 #ifdef _MSC_VER
49
50 #define SNPRINTF(str, n, format, ...) \
51   _snprintf_s(str, n, _TRUNCATE, format, ##__VA_ARGS__)
52
53 #else
54
55 #define SNPRINTF  snprintf
56
57 #endif
58
59
60 #ifndef NO_GETENV
61
62 #ifdef _MSC_VER
63
64 static INLINE int GETENV_S(char *buffer, size_t buffer_size, const char *name)
65 {
66   size_t required_size;
67
68   return (int)getenv_s(&required_size, buffer, buffer_size, name);
69 }
70
71 #else /* _MSC_VER */
72
73 #include <errno.h>
74
75 /* This provides a similar interface to the Microsoft/C11 getenv_s() function,
76  * but other than parameter validation, it has no advantages over getenv().
77  */
78
79 static INLINE int GETENV_S(char *buffer, size_t buffer_size, const char *name)
80 {
81   char *env;
82
83   if (!buffer) {
84     if (buffer_size == 0)
85       return 0;
86     else
87       return (errno = EINVAL);
88   }
89   if (buffer_size == 0)
90     return (errno = EINVAL);
91   if (!name) {
92     *buffer = 0;
93     return 0;
94   }
95
96   env = getenv(name);
97   if (!env)
98   {
99     *buffer = 0;
100     return 0;
101   }
102
103   if (strlen(env) + 1 > buffer_size) {
104     *buffer = 0;
105     return ERANGE;
106   }
107
108   strncpy(buffer, env, buffer_size);
109
110   return 0;
111 }
112
113 #endif /* _MSC_VER */
114
115 #endif /* NO_GETENV */
116
117
118 #ifndef NO_PUTENV
119
120 #ifdef _WIN32
121
122 #define PUTENV_S(name, value)  _putenv_s(name, value)
123
124 #else
125
126 /* This provides a similar interface to the Microsoft _putenv_s() function, but
127  * other than parameter validation, it has no advantages over setenv().
128  */
129
130 static INLINE int PUTENV_S(const char *name, const char *value)
131 {
132   if (!name || !value)
133     return (errno = EINVAL);
134
135   setenv(name, value, 1);
136
137   return errno;
138 }
139
140 #endif /* _WIN32 */
141
142 #endif /* NO_PUTENV */
143
144
145 #endif /* JINCLUDE_H */