mp_msg.c: Avoid out-of-bounds read if empty string is printed
[mplayer/glamo.git] / TOOLS / realcodecs / drv4.c
blobad68c1ac3676bcd37e28a2cdb381206874b46410
1 /*
2 * This is a small DLL that works as a wrapper for the actual realdrv4.so.6.0
3 * DLL from RealPlayer 8.0.
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 Assuming that RACloseCodec is the last call.
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <dlfcn.h>
31 #include <sys/time.h>
33 typedef unsigned long ulong;
35 //000000000000a410 g DF .text 0000000000000043 G2 RV20toYUV420Free
36 //000000000000a6c0 g DF .text 0000000000000060 G2 RV20toYUV420CustomMessage
37 //000000000000a200 g DF .text 000000000000020c G2 RV20toYUV420Init
38 //000000000000a724 g DF .text 0000000000000132 G2 RV20toYUV420HiveMessage
39 //000000000000a458 g DF .text 0000000000000262 G2 RV20toYUV420Transform
41 ulong (*rvyuvCustomMessage)(ulong,ulong);
42 ulong (*rvyuvFree)(ulong);
43 ulong (*rvyuvHiveMessage)(ulong,ulong);
44 ulong (*rvyuvInit)(ulong,ulong);
45 ulong (*rvyuvTransform)(ulong,ulong,ulong,ulong,ulong);
47 //void (*setDLLAccessPath)(ulong);
49 int b_dlOpened=0;
50 void *handle=NULL;
52 /* exits program when failure */
53 void loadSyms(void) {
54 fputs("loadSyms()\n", stderr);
55 if (!b_dlOpened) {
56 char *error;
58 fputs("opening dll...\n",stderr);
59 handle = dlopen ("/usr/local/RealPlayer8/Codecs/realdrv4.so.6.0", RTLD_LAZY);
60 if (!handle) {
61 fputs (dlerror(), stderr);
62 exit(1);
65 rvyuvCustomMessage = dlsym(handle, "RV20toYUV420CustomMessage");
66 if ((error = dlerror()) != NULL) {
67 fprintf (stderr, "dlsym(rvyuvCustomMessage): %s\n", error);
68 exit(1);
70 fprintf(stderr, "RV20toYUV420CustomMessage()=0x%0x\n", rvyuvCustomMessage);
71 rvyuvFree = dlsym(handle, "RV20toYUV420Free");
72 if ((error = dlerror()) != NULL) {
73 fprintf (stderr, "dlsym(rvyuvFree): %s\n", error);
74 exit(1);
76 fprintf(stderr, "RV20toYUV420Free()=0x%0x\n", rvyuvFree);
77 rvyuvHiveMessage = dlsym(handle, "RV20toYUV420HiveMessage");
78 if ((error = dlerror()) != NULL) {
79 fprintf (stderr, "dlsym(rvyuvHiveMessage): %s\n", error);
80 exit(1);
82 fprintf(stderr, "RV20toYUV420HiveMessage()=0x%0x\n", rvyuvHiveMessage);
83 rvyuvInit = dlsym(handle, "RV20toYUV420Init");
84 if ((error = dlerror()) != NULL) {
85 fprintf (stderr, "dlsym(rvyuvInit): %s\n", error);
86 exit(1);
88 fprintf(stderr, "RV20toYUV420Init()=0x%0x\n", rvyuvInit);
89 rvyuvTransform = dlsym(handle, "RV20toYUV420Transform");
90 if ((error = dlerror()) != NULL) {
91 fprintf (stderr, "dlsym(rvyuvTransform): %s\n", error);
92 exit(1);
94 fprintf(stderr, "RV20toYUV420Transform()=0x%0x\n", rvyuvTransform);
95 b_dlOpened=1;
99 void closeDll(void) {
100 if (handle) {
101 b_dlOpened=0;
102 dlclose(handle);
103 handle=NULL;
107 void _init(void) {
108 loadSyms();
111 struct timezone tz;
112 struct timeval tv1, tv2;
114 void tic(void) {
115 gettimeofday(&tv1, &tz);
118 void toc(void) {
119 long secs, usecs;
120 gettimeofday(&tv2, &tz);
121 secs=tv2.tv_sec-tv1.tv_sec;
122 usecs=tv2.tv_usec-tv1.tv_usec;
123 if (usecs<0) {
124 usecs+=1000000;
125 --secs;
127 // fprintf(stderr, "Duration: %ld.%.6lds\n", secs, usecs);
131 static void hexdump(void *pos, int len) {
132 unsigned char *cpos=pos, *cpos1;
133 int lines=(len+15)>>4;
134 while(lines--) {
135 int len1=len, i;
136 fprintf(stderr, "#R# %0x ", (int)cpos-(int)pos);
137 cpos1=cpos;
138 for (i=0;i<16;i++) {
139 if (len1>0) {
140 fprintf(stderr, "%02x ", *(cpos++));
141 } else {
142 fprintf(stderr, " ");
144 len1--;
146 fputs(" ", stderr);
147 cpos=cpos1;
148 for (i=0;i<16;i++) {
149 if (len>0) {
150 unsigned char ch=(*(cpos++));
151 if ((ch<32)||(ch>127)) ch='.';
152 fputc(ch, stderr);
154 len--;
156 fputs("\n", stderr);
158 fputc('\n', stderr);
162 ulong RV20toYUV420CustomMessage(ulong* p1,ulong p2) {
163 ulong result;
164 // ulong *pp1=p1;
165 // ulong temp[16];
166 fprintf(stderr, "#R# => RV20toYUV420CustomMessage(%p,%p) [%ld,%ld,%ld] \n", p1, p2, p1[0],p1[1],p1[2]);
167 #if 0
168 if(p1[0]==0x24){
169 hexdump(p1[2],64);
170 memset(temp,0x77,16*4);
171 memcpy(temp,p1[2],16);
172 p1[2]=temp;
173 } else {
174 return 0;
176 #endif
178 // fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
179 // hexdump((void*)p1, 12);
180 // if (pp1[0]==0x24) {
181 // hexdump((void*)(pp1[2]),128);
182 // }
183 // tic();
184 result=(*rvyuvCustomMessage)(p1,p2);
185 // toc();
186 fprintf(stderr, "#R# <= RV20toYUV420CustomMessage --> 0x%0lx(%ld)\n", result, result);
187 return result;
190 ulong RV20toYUV420Free(ulong p1) {
191 ulong result;
192 fprintf(stderr, "RV20toYUV420Free(ulong p1=0x%0lx(%ld))\n", p1, p1);
193 // hexdump((void*)p1, 44);
194 tic();
195 result=(*rvyuvFree)(p1);
196 toc();
197 // hexdump((void*)p1, 44);
198 fprintf(stderr, "RV20toYUV420Free --> 0x%0lx(%ld)\n\n\n", result, result);
199 return result;
202 char h_temp[32768];
204 ulong RV20toYUV420HiveMessage(ulong *p1,ulong p2) {
205 ulong result;
206 fprintf(stderr, "#R# RV20toYUV420HiveMessage(%p,%p)\n", p1, p2);
207 // p1->constant,p1->width,p1->height,p1->format1,p1->format2);
208 // fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
209 // hexdump((void*)p1, sizeof(struct init_data));
211 fprintf(stderr,">HIVE %ld %p\n",p1[0],p1[1]);
213 fprintf(stderr,"COPY INIT DATA!\n");
214 memset(h_temp,0x77,1000);
215 memcpy(h_temp,p1,4);
216 fprintf(stderr,"COPY OK!\n");
218 // tic();
219 // result=(*rvyuvHiveMessage)(p1,p2);
220 result=(*rvyuvHiveMessage)(h_temp,p2);
221 // toc();
223 fprintf(stderr,"COPY INIT DATA!\n");
224 memcpy(p1,h_temp,8);
225 fprintf(stderr,"COPY OK!\n");
227 memset(h_temp,0x77,1000);
229 // p1[0]=0;
230 // p1[1]=0x20000000;
232 fprintf(stderr,"<HIVE %ld %p\n",p1[0],p1[1]);
234 // hexdump((void*)p1, sizeof(struct init_data));
235 // hexdump((void*)p1, 8);
236 fprintf(stderr, "#R# RV20toYUV420HiveMessage --> 0x%0lx(%ld)\n\n", result, result);
237 return result;
240 struct init_data {
241 short constant; //=0xb;
242 short width, height;
243 short x1,x2,x3;
244 // 12
245 ulong format1;
246 long x4;
247 ulong format2;
248 // long unknown[32];
251 static char i_temp[32768];
253 ulong RV20toYUV420Init(ulong p1,ulong p2) {
254 ulong result;
255 fprintf(stderr, "#R# RV20toYUV420Init(ulong p1=0x%0lx(%ld), ", p1, p1);
256 fprintf(stderr, "ulong p2=0x%0lx(%ld))\n", p2, p2);
258 fprintf(stderr,"COPY INIT DATA!\n");
259 memcpy(i_temp,p1,24);
260 p1=i_temp;
261 fprintf(stderr,"COPY OK!\n");
263 hexdump((void*)p1, 24);
264 tic();
265 result=(*rvyuvInit)(p1,p2);
266 toc();
267 hexdump((void*)p1, 24);
269 memset(i_temp,0x77,1000);
271 // hexdump(*((void**)p2), 512);
272 fprintf(stderr, "#R# RV20toYUV420Init --> 0x%0lx(%ld)\n\n\n", result, result);
273 return result;
276 unsigned long build_crc(unsigned char *pch, unsigned long len) {
277 unsigned long crc=0, a;
278 // unsigned long b;
279 // it's not the real crc function, but so what...
280 while (len--) {
281 a=*(pch++);
282 // a=a+(a<<6);
283 // a^=0x555;
284 // b=(crc>>29)&7;
285 // crc=((crc<<3)+b)^a;
286 crc^=a;
288 return crc;
291 #define MIN(a,b) ((a)<(b)?(a):(b))
293 // p1=input data (stream)
294 // p2=output buffer
295 // p3=input struct
296 // p4=output struct
297 // p5=rvyuv_main
298 ulong RV20toYUV420Transform(ulong p1,ulong p2,ulong p3,ulong p4,ulong p5) {
300 //result=RV20toYUV420Transform(char *input_stream, char *output_data,
301 // struct transin *, struct transout *, struct rvyuvMain *);
303 ulong result;
304 ulong *pp3=p3;
305 ulong *pp4=p4;
306 void *v;
307 ulong temp[128];
308 int i;
310 unsigned long crc_src, crc0;
311 // unsigned long len, crc1, crc2;
312 unsigned char *pch=(char *)p1;
313 fprintf(stderr, "#R# RV20toYUV420Transform(in=%p,out=%p,tin=%p,tout=%p,yuv=%p)\n",p1,p2,p3,p4,p5);
314 // input data, length=*p3
315 // hexdump((void*)p1, /*MIN(64,*/ *((ulong*)p3) /*)*/ );
316 // v=p5;
317 // v+=0x3c;
318 // v=*((void **)v);
319 // pp3=v;
320 // len=pp3[3]*pp3[4]*3/2;
321 // pch=p2;
322 // while(--len) *(pch++)=0;
323 // hexdump((char*)p2, 64);
324 // hexdump((void*)p3, 32);
325 // hexdump((void*)p5, 64);
326 // pp3=p3;
327 // if (pp3[3]>1024) {
328 // hexdump((void*)(pp3[3]),32);
329 // pp3=pp3[3];
330 // }
332 pp3=p3;
333 // it's not the real crc function, but so what...
334 pch=p1;
335 crc_src=build_crc(pch, pp3[0]);
337 pp4=pp3[3];
338 fprintf(stderr,"transin1[%p]: {%ld/%ld} ",pp4,pp3[2],pp3[0]);
339 // pp4[0],pp4[1],pp4[2],pp4[3],
340 // pp4[4],pp4[5],pp4[6],pp4[7]);
342 memset(temp,0x77,128*4);
344 memcpy(temp,pp4,8*(pp3[2]+1));
345 for(i=0;i<=pp3[2];i++){
346 fprintf(stderr," %p(%ld)",temp[i*2],temp[i*2+1]);
348 fprintf(stderr,"\n");
351 pp3[3]=pp4=temp;
353 // pp4[2]=
354 // pp4[3]=
355 // pp4[4]=NULL;
357 //pp4[6]=pp4[5];
359 v=p5;
360 /* fprintf(stderr, "rvyuvMain=0x%0x\n", v);
361 v+=0x3c;
362 v=*((void **)v);
363 fprintf(stderr, "[$+3ch]=0x%0x\n", v);
364 hexdump(v, 512);
365 v+=0x60;
366 v=*((void **)v);
367 fprintf(stderr, "[$+60h]=0x%0x\n", v);
368 hexdump(v, 512);
369 v+=0x28;
370 v=*((void **)v);
371 fprintf(stderr, "[$+28h]=0x%0x\n", v);
372 hexdump(v, 512);
374 /* v+=0x178;
375 hexdump(v, 16);
376 v=*((void **)v);
377 if (v>0x8000000) {
378 fprintf(stderr, "[$+178h]=0x%0x\n", v);
379 hexdump(v, 128);
382 // tic();
383 result=(*rvyuvTransform)(p1,p2,p3,p4,p5);
384 // toc();
386 crc0=build_crc(p2, 176*144);
387 // crc1=build_crc(p2+pp4[3]*pp4[4]/2, pp4[3]*pp4[4]/2);
388 // crc2=build_crc(p2+pp4[3]*pp4[4], pp4[3]*pp4[4]/2);
390 // pp3=p3;
391 // TRANSFORM: <timestamp> <numblocks> <len> <crc_src> <crc_dest> <p4[4]>
392 // fprintf(stderr, "TRAFO:\t%ld\t%ld\t%ld\t%.8lX\t%.8lX\t%ld\n",
393 // pp3[5], pp3[2], pp3[0], crc_src, crc0, pp3[4]);
394 fprintf(stderr, "#R# Decode: %ld(%ld) [%08lX] pts=%ld -> %ld [%08lX]\n",
395 pp3[0],pp3[2],crc_src,pp3[5],
396 result,crc0);
398 // output
399 // hexdump((char*)p2, /*64*/ pp4[3]*pp4[4]/2);
400 // hexdump((void*)p4, 20);
401 // hexdump((void*)p5, 512);
402 // fprintf(stderr, "RV20toYUV420Transform --> 0x%0lx(%ld)\n\n\n", result, result);
403 return result;