OSDN Git Service

[COMMON][FILEIO] common.h : Fix pair16_t and pair64_t to fileio.cpp .
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ common header ]
8 */
9
10 #ifndef _COMMON_H_
11 #define _COMMON_H_
12
13 // move shared codes to DLL???
14 //#ifdef _USE_QT
15 //      #define USE_SHARED_DLL
16 //#endif
17
18 // use zlib to decompress gzip file???
19 #ifdef _WIN32
20         #if defined(_MSC_VER) && (_MSC_VER >= 1500)
21                 #ifndef _ANY2D88
22                         #define USE_ZLIB
23                 #endif
24         #endif
25 #endif
26 // check environemnt/language
27 #ifdef _WIN32
28         #ifdef _MSC_VER
29                 // Microsoft Visual C++
30                 #if _MSC_VER == 1200
31                         // variable scope of 'for' loop for Microsoft Visual C++ 6.0
32                         #define for if(0);else for
33                 #endif
34                 #if _MSC_VER >= 1200
35                         // Microsoft Visual C++ 6.0 or later
36                         #define SUPPORT_TCHAR_TYPE
37                 #endif
38                 #if _MSC_VER >= 1400
39                         // Microsoft Visual C++ 8.0 (2005) or later
40                         #define SUPPORT_SECURE_FUNCTIONS
41                         #pragma warning( disable : 4819 )
42                         //#pragma warning( disable : 4995 )
43                         #pragma warning( disable : 4996 )
44                 #endif
45                 #if _MSC_VER >= 1800
46                         // Microsoft Visual C++ 12.0 (2013) or later
47                         #define SUPPORT_CPLUSPLUS_11
48                 #endif
49                 #define CSP_OS_WINDOWS
50         #else
51                 // Win32, but not Microsoft Visual C++
52                 #define SUPPORT_TCHAR_TYPE
53 //              #define SUPPORT_SECURE_FUNCTIONS
54         #endif
55 #endif
56 #ifdef __GNUC__
57         #if defined(Q_OS_CYGWIN) 
58                 #define CSP_OS_GCC_CYGWIN
59                 #define CSP_OS_WINDOWS
60         #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
61                 #define CSP_OS_GCC_WINDOWS
62                 #define CSP_OS_WINDOWS
63                 #ifdef USE_SHARED_DLL
64                         #define DLL_PREFIX   __declspec(dllexport)
65                         #define DLL_PREFIX_I __declspec(dllimport)
66                 #endif
67         #else
68                 #define CSP_OS_GCC_GENERIC
69                 #define CSP_OS_GENERIC
70                 #define DLL_PREFIX
71                 #define DLL_PREFIX_I
72         #endif
73         #if defined(__clang__)
74                 #define __CSP_COMPILER_CLANG
75         #else
76                 #define __CSP_COMPILER_GCC
77         #endif
78         #define SUPPORT_CPLUSPLUS_11
79 #else
80                 #define DLL_PREFIX
81                 #define DLL_PREFIX_I
82 #endif
83
84 #ifndef SUPPORT_CPLUSPLUS_11
85         #if defined(__cplusplus) && (__cplusplus > 199711L)
86                 #define SUPPORT_CPLUSPLUS_11
87         #endif
88 #endif
89 #ifndef SUPPORT_TCHAR_TYPE
90         // secure functions need tchar type
91         #undef SUPPORT_SECURE_FUNCTIONS
92 #endif
93 #ifndef DLL_PREFIX
94         #define DLL_PREFIX
95 #endif
96 #ifndef DLL_PREFIX_I
97         #define DLL_PREFIX_I
98 #endif
99
100 // include common header files
101 #ifdef SUPPORT_TCHAR_TYPE
102         #include <tchar.h>
103 #endif
104 #ifdef SUPPORT_CPLUSPLUS_11
105         #include <stdint.h>
106 #endif
107 #include <stdio.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #ifdef _MSC_VER
111 #include <typeinfo.h>
112 #else
113 #include <typeinfo>
114 #endif
115 #include <assert.h>
116 #include <errno.h>
117
118 // include environment/language dependent header files
119 #ifdef _WIN32
120         #include <windows.h>
121         #include <windowsx.h>
122         #include <winuser.h>
123         #include <mmsystem.h>
124         #include <process.h>
125 #endif
126 #ifdef __GNUC__
127         #include <stdarg.h>
128 #endif
129 #ifdef _USE_QT
130         #ifdef _USE_QT5
131                 #include <QString>
132                 #include <QFile>
133                 #include <QtEndian>
134                 #if defined(__MINGW32__) || (__MINGW64__)
135                         #include <windows.h>
136                         #include <winbase.h>
137                 #endif
138         #else
139                 #include <QtCore/QString>
140                 #include <QtCore/QFile>
141         #endif
142         #include <sys/param.h>
143 #endif
144 #ifndef _MAX_PATH
145         #define _MAX_PATH 2048
146 #endif
147
148 // endian
149 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
150         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
151                 #if __BYTE_ORDER == __LITTLE_ENDIAN
152                         #define __LITTLE_ENDIAN__
153                 #elif __BYTE_ORDER == __BIG_ENDIAN
154                         #define __BIG_ENDIAN__
155                 #endif
156         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
157                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
158                         #define __LITTLE_ENDIAN__
159                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
160                         #define __BIG_ENDIAN__
161                 #endif
162         #elif defined(WORDS_LITTLEENDIAN)
163                 #define __LITTLE_ENDIAN__
164         #elif defined(WORDS_BIGENDIAN)
165                 #define __BIG_ENDIAN__
166         #endif
167 #endif
168 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
169         // may be Microsoft Visual C++
170         #define __LITTLE_ENDIAN__
171 #endif
172
173 // type definition
174 #ifndef SUPPORT_TCHAR_TYPE
175         #ifndef _TCHAR
176                 typedef char _TCHAR;
177         #endif
178 #endif
179
180 #ifndef SUPPORT_CPLUSPLUS_11
181         #ifndef int8_t
182                 typedef signed char int8_t;
183         #endif
184         #ifndef int16_t
185                 typedef signed short int16_t;
186         #endif
187         #ifndef int32_t
188                 typedef signed int int32_t;
189         #endif
190         #ifndef int64_t
191                 typedef signed long long int64_t;
192         #endif
193         #ifndef uint8_t
194                 typedef unsigned char uint8_t;
195         #endif
196         #ifndef uint16_t
197                 typedef unsigned short uint16_t;
198         #endif
199         #ifndef uint32_t
200                 typedef unsigned int uint32_t;
201         #endif
202         #ifndef uint64_t
203                 typedef unsigned long long uint64_t;
204         #endif
205 #endif
206
207 #ifndef _WIN32
208         #ifndef LPTSTR
209                 typedef _TCHAR* LPTSTR;
210         #endif
211         #ifndef LPCTSTR
212                 typedef const _TCHAR* LPCTSTR;
213         #endif
214         #ifndef BOOL
215                 typedef int BOOL;
216         #endif
217         #ifndef TRUE
218                 #define TRUE 1
219         #endif
220         #ifndef FALSE
221                 #define FALSE 0
222         #endif
223         #ifndef BYTE
224                 typedef uint8_t BYTE;
225         #endif
226         #ifndef WORD
227                 typedef uint16_t WORD;
228         #endif
229         #ifndef DWORD
230                 typedef uint32_t DWORD;
231         #endif
232         #ifndef QWORD
233                 typedef uint64_t QWORD;
234         #endif
235         #ifndef INT8
236                 typedef int8_t INT8;
237         #endif
238         #ifndef INT16
239                 typedef int16_t INT16;
240         #endif
241         #ifndef INT32
242                 typedef int32_t INT32;
243         #endif
244         #ifndef INT64
245                 typedef int64_t INT64;
246         #endif
247         #ifndef UINT8
248                 typedef uint8_t UINT8;
249         #endif
250         #ifndef UINT16
251                 typedef uint16_t UINT16;
252         #endif
253         #ifndef UINT32
254                 typedef uint32_t UINT32;
255         #endif
256         #ifndef UINT64
257                 typedef uint64_t UINT64;
258         #endif
259         #ifndef INT
260                 typedef int INT;
261         #endif
262         #ifndef UINT
263                 typedef unsigned int UINT;
264         #endif
265 #endif
266
267 typedef union {
268         struct {
269 #ifdef __BIG_ENDIAN__
270                 uint8_t h, l;
271 #else
272                 uint8_t l, h;
273 #endif
274         } b;
275         struct {
276 #ifdef __BIG_ENDIAN__
277                 int8_t h, l;
278 #else
279                 int8_t l, h;
280 #endif
281         } sb;
282         uint16_t u16;
283         int16_t s16;
284
285         inline void read_2bytes_le_from(uint8_t *t)
286         {
287                 b.l = t[0]; b.h = t[1];
288         }
289         inline void write_2bytes_le_to(uint8_t *t)
290         {
291                 t[0] = b.l; t[1] = b.h;
292         }
293         inline void read_2bytes_be_from(uint8_t *t)
294         {
295                 b.h = t[0]; b.l = t[1];
296         }
297         inline void write_2bytes_be_to(uint8_t *t)
298         {
299                 t[0] = b.h; t[1] = b.l;
300         }
301         
302         inline void set_2bytes_be_from(uint16_t n)
303         {
304                 union {
305                         uint16_t w;
306                         struct {
307                                 uint8_t h, l;
308                         }b;
309                 } bigv;
310                 bigv.w = n;
311                 b.l = bigv.b.l; b.h = bigv.b.h;
312         }
313         inline void set_2bytes_le_from(uint16_t n)
314         {
315                 union {
316                         uint16_t w;
317                         struct {
318                                 uint8_t l, h;
319                         }b;
320                 } littlev;
321                 littlev.w = n;
322                 b.l = littlev.b.l; b.h = littlev.b.h;
323         }
324         inline uint16_t get_2bytes_be_to()
325         {
326                 union {
327                         uint16_t w;
328                         struct {
329                                 uint8_t h, l;
330                         }b;
331                 } bigv;
332                 bigv.b.l = b.l; bigv.b.h = b.h;
333                 return bigv.w;
334         }
335         inline uint16_t get_2bytes_le_to()
336         {
337                 union {
338                         uint16_t w;
339                         struct {
340                                 uint8_t l, h;
341                         }b;
342                 } littlev;
343                 littlev.b.l = b.l; littlev.b.h = b.h;
344                 return littlev.w;
345         }
346
347 } pair16_t;
348
349 typedef union {
350         struct {
351 #ifdef __BIG_ENDIAN__
352                 uint8_t h3, h2, h, l;
353 #else
354                 uint8_t l, h, h2, h3;
355 #endif
356         } b;
357         struct {
358 #ifdef __BIG_ENDIAN__
359                 int8_t h3, h2, h, l;
360 #else
361                 int8_t l, h, h2, h3;
362 #endif
363         } sb;
364         struct {
365 #ifdef __BIG_ENDIAN__
366                 uint16_t h, l;
367 #else
368                 uint16_t l, h;
369 #endif
370         } w;
371         struct {
372 #ifdef __BIG_ENDIAN__
373                 int16_t h, l;
374 #else
375                 int16_t l, h;
376 #endif
377         } sw;
378         struct {
379 #ifdef __BIG_ENDIAN__
380                 pair16_t h, l;
381 #else
382                 pair16_t l, h;
383 #endif
384         } p16;
385         uint32_t d;
386         int32_t sd;
387         float f;
388         inline void read_2bytes_le_from(uint8_t *t)
389         {
390                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
391         }
392         inline void write_2bytes_le_to(uint8_t *t)
393         {
394                 t[0] = b.l; t[1] = b.h;
395         }
396         inline void read_2bytes_be_from(uint8_t *t)
397         {
398                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
399         }
400         inline void write_2bytes_be_to(uint8_t *t)
401         {
402                 t[0] = b.h; t[1] = b.l;
403         }
404         inline void read_4bytes_le_from(uint8_t *t)
405         {
406                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
407         }
408         inline void write_4bytes_le_to(uint8_t *t)
409         {
410                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
411         }
412         inline void read_4bytes_be_from(uint8_t *t)
413         {
414                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
415         }
416         inline void write_4bytes_be_to(uint8_t *t)
417         {
418                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
419         }
420
421         inline void set_2bytes_be_from(uint16_t n)
422         {
423                 union {
424                         uint16_t w;
425                         struct {
426                                 uint8_t h, l;
427                         }b;
428                 } bigv;
429                 bigv.w = n;
430                 b.l = bigv.b.l; b.h = bigv.b.h;
431                 b.h2 = 0; b.h3 = 0;
432         }
433         inline void set_2bytes_le_from(uint16_t n)
434         {
435                 union {
436                         uint16_t w;
437                         struct {
438                                 uint8_t l, h;
439                         }b;
440                 } littlev;
441                 littlev.w = n;
442                 b.l = littlev.b.l; b.h = littlev.b.h;
443                 b.h2 = 0; b.h3 = 0;
444         }
445         inline uint16_t get_2bytes_be_to()
446         {
447                 union {
448                         uint16_t w;
449                         struct {
450                                 uint8_t h, l;
451                         }b;
452                 } bigv;
453                 bigv.b.l = b.l; bigv.b.h = b.h;
454                 return bigv.w;
455         }
456         inline uint16_t get_2bytes_le_to()
457         {
458                 union {
459                         uint16_t w;
460                         struct {
461                                 uint8_t l, h;
462                         }b;
463                 } littlev;
464                 littlev.b.l = b.l; littlev.b.h = b.h;
465                 return littlev.w;
466         }
467         
468         inline void set_4bytes_be_from(uint32_t n)
469         {
470                 union {
471                         uint32_t dw;
472                         struct {
473                                 uint8_t h3, h2, h, l;
474                         }b;
475                 } bigv;
476                 bigv.dw = n;
477                 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
478         }
479         inline void set_4bytes_le_from(uint32_t n)
480         {
481                 union {
482                         uint32_t dw;
483                         struct {
484                                 uint8_t l, h, h2, h3;
485                         }b;
486                 } littlev;
487                 littlev.dw = n;
488                 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
489         }
490         inline uint32_t get_4bytes_be_to()
491         {
492                 union {
493                         uint32_t dw;
494                         struct {
495                                 uint8_t h3, h2, h, l;
496                         }b;
497                 } bigv;
498                 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
499                 return bigv.dw;
500         }
501         inline uint32_t get_4bytes_le_to()
502         {
503                 union {
504                         uint32_t dw;
505                         struct {
506                                 uint8_t l, h, h2, h3;
507                         }b;
508                 } littlev;
509                 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
510                 return littlev.dw;
511         }
512 } pair_t;
513
514
515 typedef union {
516         struct {
517 #ifdef __BIG_ENDIAN__
518                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
519 #else
520                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
521 #endif
522         } b;
523         struct {
524 #ifdef __BIG_ENDIAN__
525                 int8_t h7, h6, h5, h4, h3, h2, h, l;
526 #else
527                 int8_t l, h, h2, h3, h4, h5, h6, h7;
528 #endif
529         } sb;
530         struct {
531 #ifdef __BIG_ENDIAN__
532                 uint16_t h3, h2, h, l;
533 #else
534                 uint16_t l, h, h2, h3;
535 #endif
536         } w;
537         struct {
538 #ifdef __BIG_ENDIAN__
539                 int16_t h3, h2, h, l;
540 #else
541                 int16_t l, h, h2, h3;
542 #endif
543         } sw;
544         struct {
545 #ifdef __BIG_ENDIAN__
546                 pair16_t h3, h2, h, l;
547 #else
548                 pair16_t l, h, h2, h3;
549 #endif
550         } p16;
551         struct {
552 #ifdef __BIG_ENDIAN__
553                 uint32_t h, l;
554 #else
555                 uint32_t l, h;
556 #endif
557         } d;
558         struct {
559 #ifdef __BIG_ENDIAN__
560                 int32_t h, l;
561 #else
562                 int32_t l, h;
563 #endif
564         } sd;
565         struct {
566 #ifdef __BIG_ENDIAN__
567                 pair_t h, l;
568 #else
569                 pair_t l, h;
570 #endif
571         } p32;
572         uint64_t u64;
573         int64_t s64;
574         double d64;
575         inline void read_2bytes_le_from(uint8_t *t)
576         {
577                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
578                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
579         }
580         inline void write_2bytes_le_to(uint8_t *t)
581         {
582                 t[0] = b.l; t[1] = b.h;
583         }
584         inline void read_2bytes_be_from(uint8_t *t)
585         {
586                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
587                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
588         }
589         inline void write_2bytes_be_to(uint8_t *t)
590         {
591                 t[0] = b.h; t[1] = b.l;
592         }
593         inline void read_4bytes_le_from(uint8_t *t)
594         {
595                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
596                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
597         }
598         inline void write_4bytes_le_to(uint8_t *t)
599         {
600                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
601         }
602         inline void read_4bytes_be_from(uint8_t *t)
603         {
604                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
605                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
606         }
607         inline void write_4bytes_be_to(uint8_t *t)
608         {
609                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
610         }
611         
612         inline void read_8bytes_le_from(uint8_t *t)
613         {
614                 b.l = t[0];  b.h = t[1];  b.h2 = t[2]; b.h3 = t[3];
615                 b.h4 = t[4]; b.h5 = t[5]; b.h6 = t[6]; b.h7 = t[7];
616         }
617         inline void write_8bytes_le_to(uint8_t *t)
618         {
619                 t[0] = b.l;  t[1] = b.h;  t[2] = b.h2; t[3] = b.h3;
620                 t[4] = b.h4; t[5] = b.h5; t[6] = b.h6; t[7] = b.h7;
621         }
622         inline void read_8bytes_be_from(uint8_t *t)
623         {
624                 b.h7 = t[0]; b.h6 = t[1]; b.h5 = t[2]; b.h4 = t[3];
625                 b.h3 = t[4]; b.h2 = t[5]; b.h = t[6];  b.l = t[7];
626         }
627         inline void write_8bytes_be_to(uint8_t *t)
628         {
629                 t[0] = b.h7; t[1] = b.h6; t[2] = b.h5; t[3] = b.h4;
630                 t[4] = b.h3; t[5] = b.h2; t[6] = b.h;  t[7] = b.l;
631         }
632
633         inline void set_2bytes_be_from(uint16_t n)
634         {
635                 union {
636                         uint16_t w;
637                         struct {
638                                 uint8_t h, l;
639                         }b;
640                 } bigv;
641                 bigv.w = n;
642                 b.l = bigv.b.l; b.h = bigv.b.h;
643                 b.h2 = 0; b.h3 = 0;
644                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
645         }
646         inline void set_2bytes_le_from(uint16_t n)
647         {
648                 union {
649                         uint16_t w;
650                         struct {
651                                 uint8_t l, h;
652                         }b;
653                 } littlev;
654                 littlev.w = n;
655                 b.l = littlev.b.l; b.h = littlev.b.h;
656                 b.h2 = 0; b.h3 = 0;
657                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
658         }
659         inline uint16_t get_2bytes_be_to()
660         {
661                 union {
662                         uint16_t w;
663                         struct {
664                                 uint8_t h, l;
665                         }b;
666                 } bigv;
667                 bigv.b.l = b.l; bigv.b.h = b.h;
668                 return bigv.w;
669         }
670         inline uint16_t get_2bytes_le_to()
671         {
672                 union {
673                         uint16_t w;
674                         struct {
675                                 uint8_t l, h;
676                         }b;
677                 } littlev;
678                 littlev.b.l = b.l; littlev.b.h = b.h;
679                 return littlev.w;
680         }
681         
682         inline void set_4bytes_be_from(uint32_t n)
683         {
684                 union {
685                         uint32_t dw;
686                         struct {
687                                 uint8_t h3, h2, h, l;
688                         }b;
689                 } bigv;
690                 bigv.dw = n;
691                 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
692                 b.h4 = 0;       b.h5 = 0;       b.h6 = 0;         b.h7 = 0;
693         }
694         inline void set_4bytes_le_from(uint32_t n)
695         {
696                 union {
697                         uint32_t dw;
698                         struct {
699                                 uint8_t l, h, h2, h3;
700                         }b;
701                 } littlev;
702                 littlev.dw = n;
703                 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
704                 b.h4 = 0;          b.h5 = 0;          b.h6 = 0;            b.h7 = 0;
705         }
706         inline uint32_t get_4bytes_be_to()
707         {
708                 union {
709                         uint32_t dw;
710                         struct {
711                                 uint8_t h3, h2, h, l;
712                         }b;
713                 } bigv;
714                 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
715                 return bigv.dw;
716         }
717         inline uint32_t get_4bytes_le_to()
718         {
719                 union {
720                         uint32_t dw;
721                         struct {
722                                 uint8_t l, h, h2, h3;
723                         }b;
724                 } littlev;
725                 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
726                 return littlev.dw;
727         }
728
729         inline void set_8bytes_be_from(uint64_t n)
730         {
731                 union {
732                         uint64_t qw;
733                         struct {
734                                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
735                         }b;
736                 } bigv;
737                 bigv.qw = n;
738                 b.l = bigv.b.l;   b.h = bigv.b.h;   b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
739                 b.h4 = bigv.b.h4; b.h5 = bigv.b.h5; b.h6 = bigv.b.h6; b.h7 = bigv.b.h7;
740         }
741         inline void set_8bytes_le_from(uint64_t n)
742         {
743                 union {
744                         uint64_t qw;
745                         struct {
746                                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
747                         }b;
748                 } littlev;
749                 littlev.qw = n;
750                 b.l = littlev.b.l;   b.h = littlev.b.h;   b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
751                 b.h4 = littlev.b.h4; b.h5 = littlev.b.h5; b.h6 = littlev.b.h6; b.h7 = littlev.b.h7;
752         }
753         inline uint64_t get_8bytes_be_to()
754         {
755                 union {
756                         uint64_t qw;
757                         struct {
758                                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
759                         }b;
760                 } bigv;
761                 bigv.b.l = b.l;   bigv.b.h = b.h;   bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
762                 bigv.b.h4 = b.h4; bigv.b.h5 = b.h5; bigv.b.h6 = b.h6; bigv.b.h7 = b.h7;
763                 return bigv.qw;
764         }
765         inline uint64_t get_8bytes_le_to()
766         {
767                 union {
768                         uint64_t qw;
769                         struct {
770                                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
771                         }b;
772                 } littlev;
773                 littlev.b.l = b.l;   littlev.b.h = b.h;   littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
774                 littlev.b.h4 = b.h4; littlev.b.h5 = b.h5; littlev.b.h6 = b.h6; littlev.b.h7 = b.h7;
775                 return littlev.qw;
776         }
777
778 } pair64_t;
779
780 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
781 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
782 uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x);
783 uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x);
784
785 uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x);
786 uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x);
787 uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x);
788 uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x);
789 // max/min
790 #ifndef _MSC_VER
791         #undef max
792         #undef min
793         int DLL_PREFIX max(int a, int b);
794         unsigned int DLL_PREFIX max(int a, unsigned int b);
795         unsigned int DLL_PREFIX max(unsigned int a, int b);
796         unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
797         int DLL_PREFIX min(int a, int b);
798         int DLL_PREFIX min(unsigned int a, int b);
799         int DLL_PREFIX min(int a, unsigned int b);
800         unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
801 #endif
802
803 // string
804 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
805         #define stricmp(a,b) strcasecmp(a,b)
806         #define strnicmp(a,b,n) strncasecmp(a,b,n)
807 #endif
808
809 #ifndef SUPPORT_TCHAR_TYPE
810         #ifndef _fgetts
811                 #define _fgetts fgets
812         #endif
813         #ifndef _ftprintf
814                 #define _ftprintf printf
815         #endif
816         #ifndef _tfopen
817                 #define _tfopen fopen
818         #endif
819         #ifndef _tcscmp
820                 #define _tcscmp strcmp
821         #endif
822         #ifndef _tcscpy
823                 #define _tcscpy strcpy
824         #endif
825         #ifndef _tcsicmp
826                 #define _tcsicmp stricmp
827         #endif
828         #ifndef _tcslen
829                 #define _tcslen strlen
830         #endif
831         #ifndef _tcscat
832                 #define _tcscat strcat
833         #endif
834         #ifndef _tcsncat
835                 #define _tcsncat strncat
836         #endif
837         #ifndef _tcsncpy
838                 #define _tcsncpy strncpy
839         #endif
840         #ifndef _tcsncicmp
841                 #define _tcsncicmp strnicmp
842         #endif
843         #ifndef _tcschr
844                 #define _tcschr strchr
845         #endif
846         #ifndef _tcsrchr
847                 #define _tcsrchr strrchr
848         #endif
849         #ifndef _tcsstr
850                 #define _tcsstr strstr
851         #endif
852         #ifndef _tcstok
853                 #define _tcstok strtok
854         #endif
855         #ifndef _tstoi
856                 #define _tstoi atoi
857         #endif
858         #ifndef _tcstol
859                 #define _tcstol strtol
860         #endif
861         #ifndef _tcstoul
862                 #define _tcstoul strtoul
863         #endif
864         #ifndef _stprintf
865                 #define _stprintf sprintf
866         #endif
867         #ifndef _vstprintf
868                 #define _vstprintf vsprintf
869         #endif
870         #ifndef _taccess
871                 #define _taccess access
872         #endif
873         #ifndef _tremove
874                 #define _tremove remove
875         #endif
876         #ifndef _trename
877                 #define _trename rename
878         #endif
879         #define __T(x) x
880         #define _T(x) __T(x)
881         #define _TEXT(x) __T(x)
882 #endif
883
884 #ifndef SUPPORT_SECURE_FUNCTIONS
885         #ifndef errno_t
886                 typedef int errno_t;
887         #endif
888 //      errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
889         errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
890         errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
891         errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
892         errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
893         errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
894         char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
895         _TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
896         #define my_fprintf_s fprintf
897         #define my_ftprintf_s fprintf
898         int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
899         int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
900         int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
901         int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
902         int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
903 #else
904 //      #define my_tfopen_s _tfopen_s
905         #define my_tcscat_s _tcscat_s
906         #define my_strcpy_s strcpy_s
907         #define my_tcscpy_s _tcscpy_s
908         #define my_strncpy_s strncpy_s
909         #define my_tcsncpy_s _tcsncpy_s
910         #define my_strtok_s strtok_s
911         #define my_tcstok_s _tcstok_s
912         #define my_fprintf_s fprintf_s
913         #define my_ftprintf_s _ftprintf_s
914         #define my_sprintf_s sprintf_s
915         #define my_swprintf_s swprintf_s
916         #define my_stprintf_s _stprintf_s
917         #define my_vsprintf_s vsprintf_s
918         #define my_vstprintf_s _vstprintf_s
919 #endif
920
921 // memory
922 #ifndef _MSC_VER
923         void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
924 #else
925         #define my_memcpy memcpy
926 #endif
927
928 // hint for SIMD
929 #if defined(__clang__)
930         #define __DECL_VECTORIZED_LOOP   _Pragma("clang loop vectorize(enable) interleave(enable)")
931 #elif defined(__GNUC__)
932         #define __DECL_VECTORIZED_LOOP  _Pragma("GCC ivdep")
933 #else
934         #define __DECL_VECTORIZED_LOOP
935 #endif
936
937 // C99 math functions
938 #ifdef _MSC_VER
939         #define my_isfinite _finite
940         #define my_log2(v) (log((double)(v)) / log(2.0))
941 #else
942         #include <cmath>
943         #define my_isfinite std::isfinite
944         #define my_log2 log2
945 #endif
946
947 // win32 api
948 #ifndef _WIN32
949         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
950         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
951         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
952         // used only in winmain and win32 osd class
953 //      #define ZeroMemory(p,s) memset(p,0x00,s)
954 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
955 #else
956         #define MyWritePrivateProfileString WritePrivateProfileString
957         #define MyGetPrivateProfileString GetPrivateProfileString
958         #define MyGetPrivateProfileInt GetPrivateProfileInt
959 #endif
960
961 // rgb color
962 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
963         #define _RGB888
964 #endif
965
966 #if defined(_RGB555) || defined(_RGB565)
967         typedef uint16_t scrntype_t;
968         scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
969         scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
970         uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
971         uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
972         uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
973         uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
974 #elif defined(_RGB888)
975         typedef uint32_t scrntype_t;
976 #if defined(__LITTLE_ENDIAN__)
977         #define RGB_COLOR(r, g, b)      (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | (0xff << 24))
978         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | ((uint32_t)(a) << 24))
979         #define R_OF_COLOR(c)           (((c)      ) & 0xff)
980         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
981         #define B_OF_COLOR(c)           (((c) >> 16) & 0xff)
982         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
983 #else
984         #define RGB_COLOR(r, g, b)      (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | (0xff << 24))
985         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
986         #define R_OF_COLOR(c)           (((c) >> 16) & 0xff)
987         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
988         #define B_OF_COLOR(c)           (((c)      ) & 0xff)
989         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
990 #endif
991 #endif
992
993 inline uint64_t ExchangeEndianU64(uint64_t __in)
994 {
995         pair64_t __i, __o;
996         __i.u64 = __in;
997         __o.b.h7  = __i.b.l;
998         __o.b.h6  = __i.b.h;
999         __o.b.h5  = __i.b.h2;
1000         __o.b.h4  = __i.b.h3;
1001         __o.b.h3  = __i.b.h4;
1002         __o.b.h2  = __i.b.h5;
1003         __o.b.h   = __i.b.h6;
1004         __o.b.l   = __i.b.h7;
1005         return __o.u64;
1006 }
1007
1008 inline int64_t ExchangeEndianS64(uint64_t __in)
1009 {
1010         pair64_t __i, __o;
1011         __i.u64 = __in;
1012         __o.b.h7  = __i.b.l;
1013         __o.b.h6  = __i.b.h;
1014         __o.b.h5  = __i.b.h2;
1015         __o.b.h4  = __i.b.h3;
1016         __o.b.h3  = __i.b.h4;
1017         __o.b.h2  = __i.b.h5;
1018         __o.b.h   = __i.b.h6;
1019         __o.b.l   = __i.b.h7;
1020         return __o.s64;
1021 }
1022 inline uint32_t ExchangeEndianU32(uint32_t __in)
1023 {
1024         pair_t __i, __o;
1025         __i.d = __in;
1026         __o.b.h3 = __i.b.l;
1027         __o.b.h2 = __i.b.h;
1028         __o.b.h  = __i.b.h2;
1029         __o.b.l  = __i.b.h3;
1030         return __o.d;
1031 }
1032
1033 inline int32_t ExchangeEndianS32(uint32_t __in)
1034 {
1035         pair_t __i, __o;
1036         __i.d = __in;
1037         __o.b.h3 = __i.b.l;
1038         __o.b.h2 = __i.b.h;
1039         __o.b.h  = __i.b.h2;
1040         __o.b.l  = __i.b.h3;
1041         return __o.sd;
1042 }
1043
1044 inline uint16_t ExchangeEndianU16(uint16_t __in)
1045 {
1046         pair16_t __i, __o;
1047         __i.u16 = __in;
1048         __o.b.h = __i.b.l;
1049         __o.b.l  = __i.b.h;
1050         return __o.u16;
1051 }
1052
1053 inline int16_t ExchangeEndianS16(uint16_t __in)
1054 {
1055         pair16_t __i, __o;
1056         __i.u16 = __in;
1057         __o.b.h = __i.b.l;
1058         __o.b.l = __i.b.h;
1059         return __o.s16;
1060 }
1061
1062 // wav file header
1063 #pragma pack(1)
1064 typedef struct {
1065         char id[4];
1066         uint32_t size;
1067 } wav_chunk_t;
1068 #pragma pack()
1069
1070 #pragma pack(1)
1071 typedef struct {
1072         wav_chunk_t riff_chunk;
1073         char wave[4];
1074         wav_chunk_t fmt_chunk;
1075         uint16_t format_id;
1076         uint16_t channels;
1077         uint32_t sample_rate;
1078         uint32_t data_speed;
1079         uint16_t block_size;
1080         uint16_t sample_bits;
1081 } wav_header_t;
1082 #pragma pack()
1083
1084 //  See http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html.
1085 #pragma pack(1)
1086 typedef struct {
1087         wav_chunk_t riff_chunk;
1088         char wave[4];
1089         wav_chunk_t fmt_chunk;
1090         uint16_t format_id;
1091         uint16_t channels;
1092         uint32_t sample_rate;
1093         uint32_t data_speed;
1094         uint16_t block_size;
1095         uint16_t sample_bits;
1096         uint16_t cbsize; // Extension size.Normaly set to 0.
1097         wav_chunk_t fact_chunk; // "fact", 4.
1098 } wav_header_float_t;
1099 #pragma pack()
1100
1101 // Use this before writing wav_data.
1102 bool DLL_PREFIX write_dummy_wav_header(void *__fio);
1103 // Use this after writng wav_data.
1104 bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
1105                                                            uint16_t bits, size_t file_length);
1106 bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples);
1107 bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples);
1108
1109 // file path
1110 const _TCHAR *DLL_PREFIX get_application_path();
1111 const _TCHAR *DLL_PREFIX get_initial_current_path();
1112 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
1113 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
1114 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
1115 bool DLL_PREFIX is_absolute_path(const _TCHAR *file_path);
1116 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
1117 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
1118 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
1119 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
1120 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
1121
1122 // string
1123 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
1124 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
1125 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
1126 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
1127 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
1128 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
1129 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
1130
1131
1132 // misc
1133 void DLL_PREFIX common_initialize();
1134
1135 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
1136 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
1137
1138 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
1139 uint32_t DLL_PREFIX calc_crc32(uint32_t seed, uint8_t data[], int size);
1140 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
1141
1142 int DLL_PREFIX decibel_to_volume(int decibel);
1143 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
1144
1145 #define array_length(array) (sizeof(array) / sizeof(array[0]))
1146
1147 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
1148 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
1149 #define TO_BCD_LO(v)    ((v) % 10)
1150 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
1151
1152 // time
1153 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1154
1155 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
1156
1157 typedef DLL_PREFIX struct cur_time_s {
1158         int year, month, day, day_of_week, hour, minute, second;
1159         bool initialized;
1160         cur_time_s()
1161         {
1162                 initialized = false;
1163         }
1164         void increment();
1165         void update_year();
1166         void update_day_of_week();
1167         bool process_state(void *f, bool loading);
1168 } cur_time_t;
1169
1170 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
1171 const _TCHAR DLL_PREFIX *get_lib_common_version();
1172
1173 // symbol
1174 typedef struct symbol_s {
1175         uint32_t addr;
1176         _TCHAR *name;
1177         struct symbol_s *next_symbol;
1178 } symbol_t;
1179
1180 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
1181 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1182 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1183
1184 #endif