1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #define _FILE_OFFSET_BITS 64
35 #include <unistd.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <sys/time.h>
39 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__DragonFly__) && !defined(__NetBSD__)
40 #include <sys/prctl.h>
41 #endif
42 #include <pthread.h>
43 #include <poll.h>
44 #include <semaphore.h>
45 #include <stdint.h>
46
47 #include <inttypes.h>
48 #include <stdint.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #if defined(__APPLE__)
52 // for HRESULT
53 #include <CoreFoundation/CFPlugInCOM.h>
54 #endif
55
56 #define LIBTYPE
57 #define DECLSPEC
58
59 typedef int socket_t;
60 typedef socket_t tcp_socket_t;
61 #define INVALID_SOCKET_VALUE (-1)
62 typedef socket_t serial_socket_t;
63 #define INVALID_SERIAL_SOCKET_VALUE (-1)
64 typedef socket_t chardev_socket_t;
65 #define INVALID_CHARDEV_SOCKET_VALUE (-1)
66
67 typedef long LONG;
68 #if !defined(__APPLE__)
69 typedef LONG HRESULT;
70 #endif
71
72 #define _FILE_OFFSET_BITS 64
73 #define FILE_BEGIN 0
74 #define FILE_CURRENT 1
75 #define FILE_END 2
76
77 // Success codes
78 #ifndef S_OK
79 #define S_OK 0L
80 #endif
81
82 #ifndef S_FALSE
83 #define S_FALSE 1L
84 #endif
85
86 #ifndef FAILED
87 #define FAILED(Status) ((HRESULT)(Status)<0)
88 #endif
89
90 #ifndef SUCCEEDED
91 #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
92 #endif
93
94 // Error codes
95 #define ERROR_FILENAME_EXCED_RANGE 206L
96 #define ERROR_INVALID_NAME 123L
97
98 #ifndef E_OUTOFMEMORY
99 #define E_OUTOFMEMORY 0x8007000EL
100 #endif
101
102 #ifndef E_FAIL
103 #define E_FAIL 0x8004005EL
104 #endif
105
106 #ifdef TARGET_LINUX
107 #include <limits.h>
108 #define MAX_PATH PATH_MAX
109 #elif defined TARGET_DARWIN || defined __FreeBSD__ || defined __DragonFly__
110 #include <sys/syslimits.h>
111 #define MAX_PATH PATH_MAX
112 #else
113 #define MAX_PATH 256
114 #endif
115
116 #if defined(__APPLE__)
117 #include <stdio.h> // for fpos_t
118 #include <sched.h>
119 #include <AvailabilityMacros.h>
120 typedef int64_t off64_t;
121 typedef off_t __off_t;
122 typedef off64_t __off64_t;
123 typedef fpos_t fpos64_t;
124 #define __stat64 stat
125 #define stat64 stat
126 #if defined(TARGET_DARWIN_IOS)
127 #define statfs64 statfs
128 #endif
129 #define fstat64 fstat
130 #elif defined(__FreeBSD__) || defined(__DragonFly__)
131 #include <stdio.h> // for fpos_t
132 typedef int64_t off64_t;
133 typedef off_t __off_t;
134 typedef off64_t __off64_t;
135 typedef fpos_t fpos64_t;
136 #define __stat64 stat
137 #define stat64 stat
138 #define statfs64 statfs
139 #define fstat64 fstat
140 #else
141 #define __stat64 stat64
142 #endif
143
144 #include <string.h>
145 #define strnicmp(X,Y,N) strncasecmp(X,Y,N)
146
147 typedef unsigned char byte;
148
149 /* Platform dependent path separator */
150 #ifndef PATH_SEPARATOR_CHAR
151 #define PATH_SEPARATOR_CHAR '/'
152 #define PATH_SEPARATOR_STRING "/"
153 #endif
154
155 #ifdef TARGET_LINUX
156 // Retrieve the number of milliseconds that have elapsed since the system was started
157 #include <time.h>
GetTickCount(void)158 inline unsigned long GetTickCount(void)
159 {
160 struct timespec ts;
161 if(clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
162 {
163 return 0;
164 }
165 return (unsigned long)( (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000) );
166 };
167 #else
168 #include <time.h>
GetTickCount(void)169 inline unsigned long GetTickCount(void)
170 {
171 struct timeval tv;
172 gettimeofday(&tv, NULL);
173 return (unsigned long)( (tv.tv_sec * 1000) + (tv.tv_usec / 1000) );
174 };
175 #endif /* TARGET_LINUX || TARGET_DARWIN */
176
177 /* Handling of 2-byte Windows wchar strings on non-Windows targets
178 * Used by The MediaPortal and ForTheRecord pvr addons
179 */
180 typedef uint16_t Wchar_t; /* sizeof(wchar_t) = 4 bytes on Linux, but the MediaPortal buffer files have 2-byte wchars */
181
182 /* This is a replacement of the Windows wcslen() function which assumes that
183 * wchar_t is a 2-byte character.
184 * It is used for processing Windows wchar strings
185 */
WcsLen(const Wchar_t * str)186 inline size_t WcsLen(const Wchar_t *str)
187 {
188 const unsigned short *eos = (const unsigned short*)str;
189 while( *eos++ ) ;
190 return( (size_t)(eos - (const unsigned short*)str) -1);
191 };
192
193 /* This is a replacement of the Windows wcstombs() function which assumes that
194 * wchar_t is a 2-byte character.
195 * It is used for processing Windows wchar strings
196 */
WcsToMbs(char * s,const Wchar_t * w,size_t n)197 inline size_t WcsToMbs(char *s, const Wchar_t *w, size_t n)
198 {
199 size_t i = 0;
200 const unsigned short *wc = (const unsigned short*) w;
201 while(wc[i] && (i < n))
202 {
203 s[i] = wc[i];
204 ++i;
205 }
206 if (i < n) s[i] = '\0';
207
208 return (i);
209 };
210