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
wvInitFSPA(FSPA * item)29 wvInitFSPA (FSPA * item)
30 {
31     item->spid = 0;
32     item->xaLeft = 0;
33     item->yaTop = 0;
34     item->xaRight = 0;
35     item->yaBottom = 0;
36     item->fHdr = 0;
37     item->bx = 0;
38     item->by = 0;
39     item->wr = 0;
40     item->wrk = 0;
41     item->fRcaSimple = 0;
42     item->fBelowText = 0;
43     item->fAnchorLock = 0;
44     item->cTxbx = 0;
45 }
46 
47 void
wvGetFSPA(FSPA * item,wvStream * fd)48 wvGetFSPA (FSPA * item, wvStream * fd)
49 {
50     U16 temp16;
51 #ifdef PURIFY
52     wvInitFSPA (item);
53 #endif
54     item->spid = read_32ubit (fd);;
55     item->xaLeft = (S32) read_32ubit (fd);
56     item->yaTop = (S32) read_32ubit (fd);
57     item->xaRight = (S32) read_32ubit (fd);
58     item->yaBottom = (S32) read_32ubit (fd);
59     temp16 = read_16ubit (fd);
60     item->fHdr = temp16 & 0x0001;
61     item->bx = (temp16 & 0x0006) >> 1;
62     item->by = (temp16 & 0x0018) >> 3;
63     item->wr = (temp16 & 0x01E0) >> 5;
64     item->wrk = (temp16 & 0x1E00) >> 9;
65     item->fRcaSimple = (temp16 & 0x2000) >> 13;
66     item->fBelowText = (temp16 & 0x4000) >> 14;
67     item->fAnchorLock = (temp16 & 0x8000) >> 15;
68     item->cTxbx = (S32) read_32ubit (fd);
69 }
70 
71 
72 int
wvGetFSPA_PLCF(FSPA ** fspa,U32 ** pos,U32 * nofspa,U32 offset,U32 len,wvStream * fd)73 wvGetFSPA_PLCF (FSPA ** fspa, U32 ** pos, U32 * nofspa, U32 offset, U32 len,
74 		wvStream * fd)
75 {
76     U32 i;
77     if (len == 0)
78       {
79 	  *fspa = NULL;
80 	  *pos = NULL;
81 	  *nofspa = 0;
82       }
83     else
84       {
85 	  *nofspa = (len - 4) / 30;
86 	  *pos = (U32 *) wvMalloc ((*nofspa + 1) * sizeof (U32));
87 	  if (*pos == NULL)
88 	    {
89 		wvError (
90 			 ("NO MEM 1, failed to alloc %d bytes\n",
91 			  (*nofspa + 1) * sizeof (U32)));
92 		return (1);
93 	    }
94 
95 	  *fspa = (FSPA *) wvMalloc (*nofspa * sizeof (FSPA));
96 	  if (*fspa == NULL)
97 	    {
98 		wvError (
99 			 ("NO MEM 1, failed to alloc %d bytes\n",
100 			  *nofspa * sizeof (FSPA)));
101 
102 		/* I believe it is not always right to free this. Sometimes len == 4 and
103 		 * although *nofspa == 0, the data structure is needed.
104 		 * (Wild guesswork by MV 20.12.2000 -- correct me if I'm wrong)        */
105 		/*                      wvFree(pos); */
106 		return (1);
107 	    }
108 	  wvStream_goto (fd, offset);
109 	  for (i = 0; i <= *nofspa; i++)
110 	      (*pos)[i] = read_32ubit (fd);
111 	  for (i = 0; i < *nofspa; i++)
112 	      wvGetFSPA (&((*fspa)[i]), fd);
113       }
114     return (0);
115 }
116 
117 FSPA *
wvGetFSPAFromCP(U32 currentcp,FSPA * fspa,U32 * pos,U32 nofspa)118 wvGetFSPAFromCP (U32 currentcp, FSPA * fspa, U32 * pos, U32 nofspa)
119 {
120     U32 i;
121     wvTrace (("nofspa is %d\n", nofspa));
122     for (i = 0; i < nofspa; i++)
123       {
124 	  wvTrace (("compare %x %d\n", currentcp, pos[i]));
125 	  if (pos[i] == currentcp)
126 	      return (&(fspa[i]));
127       }
128     wvError (("found no fspa, panic\n"));
129     return (NULL);
130 }
131