about summary refs log tree commit diff stats
path: root/src/wrapped/wrappedflac.c
blob: 28005fcd14c7b30017e36d90ce05d7e7df9ecfdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <dlfcn.h>

#include "wrappedlibs.h"

#include "debug.h"
#include "wrapper.h"
#include "bridge.h"
#include "librarian/library_private.h"
#include "x64emu.h"
#include "emu/x64emu_private.h"
#include "callback.h"
#include "box64context.h"
#include "librarian.h"
#include "myalign.h"
#include "bridge.h"

const char* flacName = "libFLAC.so.8";
#define ALTNAME "libFLAC.so"
#define ALTNAME2 "libFLAC.so.12"

#define LIBNAME flac

typedef struct {
  size_t (*read_func)  (void *ptr, size_t size, size_t nmemb, void *handle);
  size_t (*write_func)  (void *ptr, size_t size, size_t nmemb, void *handle);
  int    (*seek_func)  (void *handle, int64_t offset, int whence);
  long   (*tell_func)  (void *handle);
  int    (*eof_func) (void *handle);
  int    (*close_func) (void *handle);
} flac_callbacks;

typedef int (*iFppC_t)(void*, void*, flac_callbacks);
typedef int (*iFpppppppppp_t)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);

#define SUPER() \
    GO(FLAC__metadata_chain_read_with_callbacks, iFppC_t)   \
    GO(FLAC__stream_decoder_init_stream, iFpppppppppp_t)    \

#include "wrappercallback.h"

#define SUPER() \
GO(0)           \
GO(1)           \
GO(2)           \
GO(3)           \
GO(4)           \
GO(5)           \
GO(6)           \
GO(7)

// read_write
#define GO(A)   \
static uintptr_t my_read_write_fct_##A = 0;                                                              \
static unsigned long my_read_write_##A(void* ptr, unsigned long size, unsigned long nmemb, void* handle) \
{                                                                                                        \
    return RunFunctionFmt(my_read_write_fct_##A, "pLLp", ptr, size, nmemb, handle);               \
}
SUPER()
#undef GO
static void* findread_writeFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_read_write_fct_##A == (uintptr_t)fct) return my_read_write_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_read_write_fct_##A == 0) {my_read_write_fct_##A = (uintptr_t)fct; return my_read_write_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC read_write callback\n");
    return NULL;
}
// seek
#define GO(A)   \
static uintptr_t my_seek_fct_##A = 0;                                                    \
static int my_seek_##A(void* ptr, int64_t offset, int whence)                            \
{                                                                                        \
    return (int)RunFunctionFmt(my_seek_fct_##A, "pIi", ptr, offset, whence);      \
}
SUPER()
#undef GO
static void* findseekFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_seek_fct_##A == (uintptr_t)fct) return my_seek_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_seek_fct_##A == 0) {my_seek_fct_##A = (uintptr_t)fct; return my_seek_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC seek callback\n");
    return NULL;
}
// close_eof
#define GO(A)   \
static uintptr_t my_close_eof_fct_##A = 0;                                  \
static int my_close_eof_##A(void* ptr)                                      \
{                                                                           \
    return (int)RunFunctionFmt(my_close_eof_fct_##A, "p", ptr);       \
}
SUPER()
#undef GO
static void* findclose_eofFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_close_eof_fct_##A == (uintptr_t)fct) return my_close_eof_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_close_eof_fct_##A == 0) {my_close_eof_fct_##A = (uintptr_t)fct; return my_close_eof_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC close_eof callback\n");
    return NULL;
}
// tell
#define GO(A)   \
static uintptr_t my_tell_fct_##A = 0;                                   \
static long my_tell_##A(void* ptr)                                      \
{                                                                       \
    return (long)RunFunctionFmt(my_tell_fct_##A, "p", ptr);       \
}
SUPER()
#undef GO
static void* findtellFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_tell_fct_##A == (uintptr_t)fct) return my_tell_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_tell_fct_##A == 0) {my_tell_fct_##A = (uintptr_t)fct; return my_tell_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for vorbisfile tell callback\n");
    return NULL;
}
// Read
#define GO(A)   \
static uintptr_t my_Read_fct_##A = 0;                                                              \
static int my_Read_##A(void* decoder, void* buffer, size_t* bytes, void* data)                     \
{                                                                                                  \
    return (int)RunFunctionFmt(my_Read_fct_##A, "pppp", decoder, buffer, bytes, data);        \
}
SUPER()
#undef GO
static void* findReadFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Read_fct_##A == (uintptr_t)fct) return my_Read_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Read_fct_##A == 0) {my_Read_fct_##A = (uintptr_t)fct; return my_Read_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Read callback\n");
    return NULL;
}
// Seek
#define GO(A)   \
static uintptr_t my_Seek_fct_##A = 0;                                                      \
static int my_Seek_##A(void* decoder, uint64_t offset, void* data)                         \
{                                                                                          \
    return (int)RunFunctionFmt(my_Seek_fct_##A, "pUp", decoder, offset, data);        \
}
SUPER()
#undef GO
static void* findSeekFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Seek_fct_##A == (uintptr_t)fct) return my_Seek_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Seek_fct_##A == 0) {my_Seek_fct_##A = (uintptr_t)fct; return my_Seek_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Seek callback\n");
    return NULL;
}
// Tell
#define GO(A)   \
static uintptr_t my_Tell_fct_##A = 0;                                                      \
static int my_Tell_##A(void* decoder, uint64_t *offset, void* data)                        \
{                                                                                          \
    return (int)RunFunctionFmt(my_Tell_fct_##A, "ppp", decoder, offset, data);        \
}
SUPER()
#undef GO
static void* findTellFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Tell_fct_##A == (uintptr_t)fct) return my_Tell_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Tell_fct_##A == 0) {my_Tell_fct_##A = (uintptr_t)fct; return my_Tell_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Tell callback\n");
    return NULL;
}
// Length
#define GO(A)                                                                                \
static uintptr_t my_Length_fct_##A = 0;                                                      \
static int my_Length_##A(void* decoder, uint64_t *length, void* data)                        \
{                                                                                            \
    return (int)RunFunctionFmt(my_Length_fct_##A, "ppp", decoder, length, data);      \
}
SUPER()
#undef GO
static void* findLengthFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Length_fct_##A == (uintptr_t)fct) return my_Length_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Length_fct_##A == 0) {my_Length_fct_##A = (uintptr_t)fct; return my_Length_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Length callback\n");
    return NULL;
}
// Eof
#define GO(A)   \
static uintptr_t my_Eof_fct_##A = 0;                                             \
static int my_Eof_##A(void* decoder, void* data)                                 \
{                                                                                \
    return (int)RunFunctionFmt(my_Eof_fct_##A, "pp", decoder, data);      \
}
SUPER()
#undef GO
static void* findEofFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Eof_fct_##A == (uintptr_t)fct) return my_Eof_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Eof_fct_##A == 0) {my_Eof_fct_##A = (uintptr_t)fct; return my_Eof_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Eof callback\n");
    return NULL;
}
// Write
#define GO(A)   \
static uintptr_t my_Write_fct_##A = 0;                                                              \
static int my_Write_##A(void* decoder, void* frame, void* buffer, void* data)                       \
{                                                                                                   \
    return (int)RunFunctionFmt(my_Write_fct_##A, "pppp", decoder, frame, buffer, data);       \
}
SUPER()
#undef GO
static void* findWriteFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Write_fct_##A == (uintptr_t)fct) return my_Write_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Write_fct_##A == 0) {my_Write_fct_##A = (uintptr_t)fct; return my_Write_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Write callback\n");
    return NULL;
}
// Metadata
#define GO(A)   \
static uintptr_t my_Metadata_fct_##A = 0;                                                        \
static int my_Metadata_##A(void* decoder, void* metadata, void* data)                            \
{                                                                                                \
    return (int)RunFunctionFmt(my_Metadata_fct_##A, "ppp", decoder, metadata, data);      \
}
SUPER()
#undef GO
static void* findMetadataFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Metadata_fct_##A == (uintptr_t)fct) return my_Metadata_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Metadata_fct_##A == 0) {my_Metadata_fct_##A = (uintptr_t)fct; return my_Metadata_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Metadata callback\n");
    return NULL;
}
// Error
#define GO(A)   \
static uintptr_t my_Error_fct_##A = 0;                                          \
static void my_Error_##A(void* decoder, int status, void* data)                 \
{                                                                               \
    RunFunctionFmt(my_Error_fct_##A, "pip", decoder, status, data);       \
}
SUPER()
#undef GO
static void* findErrorFct(void* fct)
{
    if(!fct) return NULL;
    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_Error_fct_##A == (uintptr_t)fct) return my_Error_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_Error_fct_##A == 0) {my_Error_fct_##A = (uintptr_t)fct; return my_Error_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for FLAC Error callback\n");
    return NULL;
}


#undef SUPER

EXPORT int my_FLAC__metadata_chain_read_with_callbacks(x64emu_t* emu, void* chain, void* handle,
    void* read_fnc, void* write_fnc, void* seek_fnc, void* tell_fnc, void* eof_fnc, void* close_fnc)
{
    flac_callbacks cbs = {0};
    cbs.read_func = findread_writeFct(read_fnc);
    cbs.write_func = findread_writeFct(write_fnc);
    cbs.seek_func = findseekFct(seek_fnc);
    cbs.tell_func = findtellFct(tell_fnc);
    cbs.eof_func = findclose_eofFct(eof_fnc);
    cbs.close_func = findclose_eofFct(close_fnc);
    int ret =  my->FLAC__metadata_chain_read_with_callbacks(chain, handle, cbs);
    return ret;
}

EXPORT int my_FLAC__stream_decoder_init_stream(x64emu_t* emu, void* decoder,
    void* read_fnc, void* seek_fnc, void* tell_fnc, void* length_fnc, void* eof_fnc,
    void* write_fnc, void* metadata_fnc, void* error_fnc, void* data)
{
    int ret =  my->FLAC__stream_decoder_init_stream(decoder,
        findReadFct(read_fnc), findSeekFct(seek_fnc), findTellFct(tell_fnc),
        findLengthFct(length_fnc), findEofFct(eof_fnc), findWriteFct(write_fnc),
        findMetadataFct(metadata_fnc), findErrorFct(error_fnc), data);
    return ret;
}

#include "wrappedlib_init.h"