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