1 /* wvWare
2  * Copyright (C) Caolan McNamara, Dom Lachowicz, and others
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "wv.h"
27 
28 void
wvGetFDOA(FDOA * item,wvStream * fd)29 wvGetFDOA (FDOA * item, wvStream * fd)
30 {
31     item->fc = (S32) read_32ubit (fd);
32     item->ctxbx = (S16) read_16ubit (fd);
33 }
34 
35 
36 int
wvGetFDOA_PLCF(FDOA ** fdoa,U32 ** pos,U32 * nofdoa,U32 offset,U32 len,wvStream * fd)37 wvGetFDOA_PLCF (FDOA ** fdoa, U32 ** pos, U32 * nofdoa, U32 offset, U32 len,
38 		wvStream * fd)
39 {
40     U32 i;
41     if ((len == 0) || (offset == 0))
42       {
43 	  *fdoa = NULL;
44 	  *pos = NULL;
45 	  *nofdoa = 0;
46       }
47     else
48       {
49 	  *nofdoa = (len - 4) / (cbFDOA + 4);
50 	  *pos = (U32 *) wvMalloc ((*nofdoa + 1) * sizeof (U32));
51 	  if (*pos == NULL)
52 	    {
53 		wvError (
54 			 ("NO MEM 1, failed to alloc %d bytes\n",
55 			  (*nofdoa + 1) * sizeof (U32)));
56 		return (1);
57 	    }
58 
59 	  *fdoa = (FDOA *) wvMalloc ((*nofdoa + 1) * sizeof (FDOA));
60 	  if (*fdoa == NULL)
61 	    {
62 		wvError (
63 			 ("NO MEM 1, failed to alloc %d bytes\n",
64 			  *nofdoa * sizeof (FDOA)));
65 		wvFree (pos);
66 		return (1);
67 	    }
68 	  wvStream_goto (fd, offset);
69 	  for (i = 0; i <= *nofdoa; i++)
70 	      (*pos)[i] = read_32ubit (fd);
71 	  for (i = 0; i < *nofdoa; i++)
72 	      wvGetFDOA (&((*fdoa)[i]), fd);
73       }
74     return (0);
75 }
76 
77 FDOA *
wvGetFDOAFromCP(U32 currentcp,FDOA * fdoa,U32 * pos,U32 nofdoa)78 wvGetFDOAFromCP (U32 currentcp, FDOA * fdoa, U32 * pos, U32 nofdoa)
79 {
80     U32 i;
81     wvTrace (("nofdoa is %d\n", nofdoa));
82     for (i = 0; i < nofdoa; i++)
83       {
84 	  wvTrace (("compare %x %x\n", currentcp, pos[i]));
85 	  if (pos[i] == currentcp)
86 	      return (&(fdoa[i]));
87       }
88     wvError (("found no fdoa, panic\n"));
89     return (NULL);
90 }
91