xref: /aosp_15_r20/external/flac/include/share/compat.h (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2012-2023  Xiph.Org Foundation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /* This is the preferred location of all CPP hackery to make $random_compiler
33  * work like something approaching a C99 (or maybe more accurately GNU99)
34  * compiler.
35  *
36  * It is assumed that this header will be included after "config.h".
37  */
38 
39 #ifndef FLAC__SHARE__COMPAT_H
40 #define FLAC__SHARE__COMPAT_H
41 
42 #include <stddef.h>
43 #include <stdarg.h>
44 
45 #if defined _WIN32 && !defined __CYGWIN__
46 /* where MSVC puts unlink() */
47 # include <io.h>
48 #else
49 # include <unistd.h>
50 #endif
51 
52 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
53 #include <sys/types.h> /* for off_t */
54 #define FLAC__off_t __int64 /* use this instead of off_t to fix the 2 GB limit */
55 #define FLAC__OFF_T_MAX INT64_MAX
56 #if !defined __MINGW32__
57 #define fseeko _fseeki64
58 #define ftello _ftelli64
59 #else /* MinGW */
60 #if !defined(HAVE_FSEEKO)
61 #define fseeko fseeko64
62 #define ftello ftello64
63 #endif
64 #endif
65 #else
66 #define FLAC__off_t off_t
67 #define FLAC__OFF_T_MAX OFF_T_MAX
68 #endif
69 
70 
71 
72 #ifdef HAVE_INTTYPES_H
73 #include <inttypes.h>
74 #endif
75 
76 #if defined(_MSC_VER)
77 #define strtoll _strtoi64
78 #define strtoull _strtoui64
79 #endif
80 
81 #if defined(_MSC_VER) && !defined(__cplusplus)
82 #define inline __inline
83 #endif
84 
85 #if defined __INTEL_COMPILER || (defined _MSC_VER && defined _WIN64)
86 /* MSVS generates VERY slow 32-bit code with __restrict */
87 #define flac_restrict __restrict
88 #elif defined __GNUC__
89 #define flac_restrict __restrict__
90 #else
91 #define flac_restrict
92 #endif
93 
94 #define FLAC__U64L(x) x##ULL
95 
96 #if defined _MSC_VER || defined __MINGW32__
97 #define FLAC__STRCASECMP _stricmp
98 #define FLAC__STRNCASECMP _strnicmp
99 #elif defined __BORLANDC__
100 #define FLAC__STRCASECMP stricmp
101 #define FLAC__STRNCASECMP strnicmp
102 #else
103 #define FLAC__STRCASECMP strcasecmp
104 #define FLAC__STRNCASECMP strncasecmp
105 #endif
106 
107 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
108 #include <io.h> /* for _setmode(), chmod() */
109 #include <fcntl.h> /* for _O_BINARY */
110 #else
111 #include <unistd.h> /* for chown(), unlink() */
112 #endif
113 
114 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
115 #if defined __BORLANDC__
116 #include <utime.h> /* for utime() */
117 #else
118 #include <sys/utime.h> /* for utime() */
119 #endif
120 #else
121 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
122 #include <fcntl.h>
123 #else
124 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
125 #include <utime.h> /* for utime() */
126 #endif
127 #endif
128 
129 #if defined _MSC_VER
130 #  if _MSC_VER >= 1800
131 #    include <inttypes.h>
132 #  elif _MSC_VER >= 1600
133 /* Visual Studio 2010 has decent C99 support */
134 #    include <stdint.h>
135 #    define PRIu64 "llu"
136 #    define PRId64 "lld"
137 #    define PRIx64 "llx"
138 #  else
139 #    include <limits.h>
140 #    ifndef UINT32_MAX
141 #      define UINT32_MAX _UI32_MAX
142 #    endif
143 #    define PRIu64 "I64u"
144 #    define PRId64 "I64d"
145 #    define PRIx64 "I64x"
146 #  endif
147 #  if defined(_USING_V110_SDK71_) && !defined(_DLL)
148 #    pragma message("WARNING: This compile will NOT FUNCTION PROPERLY on Windows XP. See comments in include/share/compat.h for details")
149 #define FLAC__USE_FILELENGTHI64
150 /*
151  *************************************************************************************
152  * V110_SDK71, in MSVC 2017 also known as v141_xp, is a platform toolset that is supposed
153  * to target Windows XP. It turns out however that certain functions provided silently fail
154  * on Windows XP only, which makes debugging challenging. This only occurs when building with
155  * /MT. This problem has been reported to Microsoft, but there hasn't been a fix for years. See
156  * https://web.archive.org/web/20170327195018/https://connect.microsoft.com/VisualStudio/feedback/details/1557168/wstat64-returns-1-on-xp-always
157  *
158  * It is known that this problem affects the functions _wstat64 (used by flac_stat i.e.
159  * stat64_utf8) and _fstat64 (i.e. flac_fstat) and therefore affects both libFLAC in
160  * several places as well as the flac and metaflac command line tools
161  *
162  * As the extent of this problem is unknown and Microsoft seems unwilling to fix it,
163  * users of libFLAC building with Visual Studio are encouraged to not use the /MT compile
164  * switch when explicitly targeting Windows XP. When use of /MT is deemed necessary with
165  * this toolset, be sure to check whether your application works properly on Windows XP.
166  * It is also possible to build for Windows XP with MinGW instead.
167  *************************************************************************************
168 */
169 #  endif
170 #endif /* defined _MSC_VER */
171 
172 #ifdef _WIN32
173 /* All char* strings are in UTF-8 format. Added to support Unicode files on Windows */
174 
175 #include "share/win_utf8_io.h"
176 #define flac_printf printf_utf8
177 #define flac_fprintf fprintf_utf8
178 #define flac_vfprintf vfprintf_utf8
179 #define flac_fopen fopen_utf8
180 #define flac_chmod chmod_utf8
181 #define flac_utime utime_utf8
182 #define flac_unlink unlink_utf8
183 #define flac_rename rename_utf8
184 #define flac_stat stat64_utf8
185 
186 #else
187 
188 #define flac_printf printf
189 #define flac_fprintf fprintf
190 #define flac_vfprintf vfprintf
191 
192 #define flac_fopen fopen
193 #define flac_chmod chmod
194 #define flac_unlink unlink
195 #define flac_rename rename
196 #define flac_stat stat
197 
198 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
199 #define flac_utime(a, b) utimensat (AT_FDCWD, a, *b, 0)
200 #else
201 #define flac_utime utime
202 #endif
203 #endif
204 
205 #ifdef _WIN32
206 #define flac_stat_s __stat64 /* stat struct */
207 #define flac_fstat _fstat64
208 #else
209 #define flac_stat_s stat /* stat struct */
210 #define flac_fstat fstat
211 #endif
212 
213 #ifdef ANDROID
214 #include <limits.h>
215 #endif
216 
217 #ifndef M_LN2
218 #define M_LN2 0.69314718055994530942
219 #endif
220 #ifndef M_PI
221 #define M_PI 3.14159265358979323846
222 #endif
223 
224 /* FLAC needs to compile and work correctly on systems with a normal ISO C99
225  * snprintf as well as Microsoft Visual Studio which has an non-standards
226  * conformant snprint_s function.
227  *
228  * This function wraps the MS version to behave more like the ISO version.
229  */
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233 int flac_snprintf(char *str, size_t size, const char *fmt, ...);
234 int flac_vsnprintf(char *str, size_t size, const char *fmt, va_list va);
235 #ifdef __cplusplus
236 };
237 #endif
238 
239 #endif /* FLAC__SHARE__COMPAT_H */
240