1 //==================================================================//
2 /*
3     AtomicParsley - util.h
4 
5     AtomicParsley is GPL software; you can freely distribute,
6     redistribute, modify & use under the terms of the GNU General
7     Public License; either version 2 or its successor.
8 
9     AtomicParsley is distributed under the GPL "AS IS", without
10     any warranty; without the implied warranty of merchantability
11     or fitness for either an expressed or implied particular purpose.
12 
13     Please see the included GNU General Public License (GPL) for
14     your rights and further details; see the file COPYING. If you
15     cannot, write to the Free Software Foundation, 59 Temple Place
16     Suite 330, Boston, MA 02111-1307, USA.  Or www.fsf.org
17 
18     Copyright �2006-2007 puck_lock
19     with contributions from others; see the CREDITS file
20                                                                                                                                                                                                                                                                                 */
21 //==================================================================//
22 #include "ap_types.h"
23 
24 #if defined(__ppc__) || defined(__ppc64__)
25 #define SWAP16(x) (x)
26 #define SWAP32(x) (x)
27 #else
28 #define SWAP16(x) ((((x)&0xFF) << 8) | (((x) >> 8) & 0xFF))
29 #define SWAP32(x)                                                              \
30   ((((x)&0xFF) << 24) | (((x) >> 24) & 0xFF) | (((x)&0x0000FF00) << 8) |       \
31    (((x)&0x00FF0000) >> 8))
32 #endif
33 
34 #if defined(_WIN32) && defined(_MSC_VER)
35 #undef HAVE_GETOPT_H
36 #undef HAVE_LROUNDF
37 #undef HAVE_STRSEP
38 //#undef HAVE_ZLIB_H //comment this IN when compiling on win32 withOUT zlib
39 // present #define HAVE_ZLIB_H 1 //and comment this OUT
40 #undef HAVE_SRANDDEV
41 #endif
42 
43 #define MAXTIME_32 6377812095ULL
44 
45 uint64_t findFileSize(const char *utf8_filepath);
46 FILE *APar_OpenFile(const char *utf8_filepath, const char *file_flags);
47 FILE *APar_OpenISOBaseMediaFile(const char *file, bool open); // openSomeFile
48 void TestFileExistence(const char *filePath, bool errorOut);
49 
50 #ifndef HAVE_FSEEKO
51 int fseeko(FILE *stream, off_t pos, int whence);
52 off_t ftello(FILE *stream);
53 #endif
54 
55 #if defined(_WIN32)
56 HANDLE APar_OpenFileWin32(const char *utf8_filepath,
57                           DWORD dwDesiredAccess,
58                           DWORD dwShareMode,
59                           LPSECURITY_ATTRIBUTES lpSecurityAttributes,
60                           DWORD dwCreationDisposition,
61                           DWORD dwFlagsAndAttributes,
62                           HANDLE hTemplateFile);
63 #endif
64 bool IsUnicodeWinOS();
65 
66 const char *APar_strferror(FILE *f);
67 uint8_t APar_read8(FILE *ISObasemediafile, uint64_t pos);
68 uint16_t APar_read16(char *buffer, FILE *ISObasemediafile, uint64_t pos);
69 uint32_t APar_read32(char *buffer, FILE *ISObasemediafile, uint64_t pos);
70 uint64_t APar_read64(char *buffer, FILE *ISObasemediafile, uint64_t pos);
71 void APar_readX_noseek(char *buffer, FILE *ISObasemediafile, uint32_t length);
72 void APar_readX(char *buffer,
73                 FILE *ISObasemediafile,
74                 uint64_t pos,
75                 uint32_t length);
76 uint32_t
77 APar_ReadFile(char *destination_buffer, FILE *a_file, uint32_t bytes_to_read);
78 uint32_t APar_FindValueInAtom(char *uint32_buffer,
79                               FILE *ISObasemediafile,
80                               short an_atom,
81                               uint64_t start_position,
82                               uint32_t eval_number);
83 
84 void APar_UnpackLanguage(unsigned char lang_code[], uint16_t packed_language);
85 uint16_t PackLanguage(const char *language_code, uint8_t lang_offset);
86 
87 #ifndef HAVE_STRSEP
88 char *strsep(char **stringp, const char *delim);
89 #endif
90 
91 char *APar_extract_UTC(uint64_t total_secs);
92 uint32_t APar_get_mpeg4_time();
93 void APar_StandardTime(char *&formed_time);
94 
95 wchar_t *Convert_multibyteUTF16_to_wchar(char *input_unicode,
96                                          size_t glyph_length,
97                                          bool skip_BOM);
98 unsigned char *Convert_multibyteUTF16_to_UTF8(char *input_utf8,
99                                               size_t glyph_length,
100                                               size_t byte_count);
101 wchar_t *Convert_multibyteUTF8_to_wchar(const char *input_utf8);
102 uint32_t
103 findstringNULLterm(char *in_string, uint8_t encodingFlag, uint32_t max_len);
104 uint32_t skipNULLterm(char *in_string, uint8_t encodingFlag, uint32_t max_len);
105 
106 uint16_t UInt16FromBigEndian(const char *string);
107 uint32_t UInt32FromBigEndian(const char *string);
108 uint64_t UInt64FromBigEndian(const char *string);
109 void UInt16_TO_String2(uint16_t snum, char *data);
110 void UInt32_TO_String4(uint32_t lnum, char *data);
111 void UInt64_TO_String8(uint64_t ullnum, char *data);
112 
113 uint32_t float_to_16x16bit_fixed_point(double floating_val);
114 double fixed_point_16x16bit_to_double(uint32_t fixed_point);
115 
116 uint32_t widechar_len(char *instring, uint32_t _bytes_);
117 
118 bool APar_assert(bool expression, int error_msg, const char *supplemental_info);
119 
120 unsigned long xor4096i();
121