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 <string.h>
27 #include "wv.h"
28 
29 void
wvGetBTE(BTE * bte,wvStream * fd)30 wvGetBTE (BTE * bte, wvStream * fd)
31 {
32     U32 temp32 = read_32ubit (fd);
33 #ifdef PURIFY
34     wvInitBTE (bte);
35 #endif
36     bte->pn = temp32 & 0x003fffffL;
37     bte->unused = (temp32 & 0xffc00000L) >> 22;
38 }
39 
40 void
wvInitBTE(BTE * bte)41 wvInitBTE (BTE * bte)
42 {
43     bte->pn = 0;
44     bte->unused = 0;
45 }
46 
47 void
wvListBTE_PLCF(BTE ** bte,U32 ** pos,U32 * nobte)48 wvListBTE_PLCF (BTE ** bte, U32 ** pos, U32 * nobte)
49 {
50     U32 i = 0;
51     for (i = 0; i < *nobte; i++)
52 	wvError (
53 		 ("range %x %x is pn %d\n", (*pos)[i], (*pos)[i + 1],
54 		  (*bte)[i].pn));
55 }
56 
57 int
wvGetBTE_PLCF6(BTE ** bte,U32 ** pos,U32 * nobte,U32 offset,U32 len,wvStream * fd)58 wvGetBTE_PLCF6 (BTE ** bte, U32 ** pos, U32 * nobte, U32 offset, U32 len,
59 		wvStream * fd)
60 {
61     U32 i;
62     if (len == 0)
63       {
64 	  *bte = NULL;
65 	  *pos = NULL;
66 	  *nobte = 0;
67       }
68     else
69       {
70 	  wvTrace (("offset is %x, len is %d\n", offset, len));
71 	  *nobte = (len - 4) / (cb6BTE + 4);
72 	  wvTrace (("no of bte is %d at %x\n", *nobte, offset));
73 	  *pos = (U32 *) wvMalloc ((*nobte + 1) * sizeof (U32));
74 	  if (*pos == NULL)
75 	    {
76 		wvError (
77 			 ("NO MEM 1, failed to alloc %d bytes\n",
78 			  (*nobte + 1) * sizeof (U32)));
79 		return (1);
80 	    }
81 
82 	  *bte = (BTE *) wvMalloc (*nobte * sizeof (BTE));
83 	  if (*bte == NULL)
84 	    {
85 		wvError (
86 			 ("NO MEM 1, failed to alloc %d bytes\n",
87 			  *nobte * sizeof (BTE)));
88 		wvFree (pos);
89 		return (1);
90 	    }
91 	  wvStream_goto (fd, offset);
92 	  for (i = 0; i <= *nobte; i++)
93 	    {
94 		(*pos)[i] = read_32ubit (fd);
95 		wvTrace (("pos is %x\n", (*pos)[i]));
96 	    }
97 	  for (i = 0; i < *nobte; i++)
98 	    {
99 		wvInitBTE (&((*bte)[i]));
100 		(*bte)[i].pn = read_16ubit (fd);
101 	    }
102       }
103     return (0);
104 }
105 
106 int
wvGetBTE_PLCF(BTE ** bte,U32 ** pos,U32 * nobte,U32 offset,U32 len,wvStream * fd)107 wvGetBTE_PLCF (BTE ** bte, U32 ** pos, U32 * nobte, U32 offset, U32 len,
108 	       wvStream * fd)
109 {
110     U32 i;
111     if (len == 0)
112       {
113 	  *bte = NULL;
114 	  *pos = NULL;
115 	  *nobte = 0;
116       }
117     else
118       {
119 	  *nobte = (len - 4) / (cbBTE + 4);
120 	  wvTrace (("no of bte is %d at %x\n", *nobte, offset));
121 	  *pos = (U32 *) wvMalloc ((*nobte + 1) * sizeof (U32));
122 	  if (*pos == NULL)
123 	    {
124 		wvError (
125 			 ("NO MEM 1, failed to alloc %d bytes\n",
126 			  (*nobte + 1) * sizeof (U32)));
127 		return (1);
128 	    }
129 
130 	  *bte = (BTE *) wvMalloc (*nobte * sizeof (BTE));
131 	  if (*bte == NULL)
132 	    {
133 		wvError (
134 			 ("NO MEM 1, failed to alloc %d bytes\n",
135 			  *nobte * sizeof (BTE)));
136 		wvFree (*pos);
137 		return (1);
138 	    }
139 	  wvStream_goto (fd, offset);
140 	  for (i = 0; i <= *nobte; i++)
141 	      (*pos)[i] = read_32ubit (fd);
142 	  for (i = 0; i < *nobte; i++)
143 	      wvGetBTE (&((*bte)[i]), fd);
144       }
145     return (0);
146 }
147 
148 void
wvCopyBTE(BTE * dest,BTE * src)149 wvCopyBTE (BTE * dest, BTE * src)
150 {
151     memcpy (dest, src, sizeof (BTE));
152 }
153 
154 int
wvGetBTE_FromFC(BTE * bte,U32 currentfc,BTE * list,U32 * fcs,int nobte)155 wvGetBTE_FromFC (BTE * bte, U32 currentfc, BTE * list, U32 * fcs, int nobte)
156 {
157     int i = 0;
158     while (i < nobte)
159       {
160 	  if ((currentfc >= wvNormFC (fcs[i], NULL))
161 	      && (currentfc < wvNormFC (fcs[i + 1], NULL)))
162 	    {
163 		wvTrace (("valid\n"));
164 		wvCopyBTE (bte, &list[i]);
165 		return (0);
166 	    }
167 	  i++;
168       }
169     wvCopyBTE (bte, &list[i - 1]);
170     return (0);
171     /*
172        return(1);
173      */
174 }
175