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
wvInitLVL(LVL * lvl)30 wvInitLVL (LVL * lvl)
31 {
32     lvl->grpprlPapx = NULL;
33     lvl->grpprlChpx = NULL;
34     lvl->numbertext = NULL;
35     wvInitLVLF (&(lvl->lvlf));
36 }
37 
38 void
wvCopyLVL(LVL * dest,LVL * src)39 wvCopyLVL (LVL * dest, LVL * src)
40 {
41     int len;
42     wvReleaseLVL (dest);
43     wvInitLVL (dest);
44 
45     wvCopyLVLF (&(dest->lvlf), &(src->lvlf));
46 
47 
48     if (src->lvlf.cbGrpprlPapx > 0)
49       {
50 	  dest->grpprlPapx = (U8 *) wvMalloc (src->lvlf.cbGrpprlPapx);
51 	  memcpy (dest->grpprlPapx, src->grpprlPapx, src->lvlf.cbGrpprlPapx);
52       }
53     else
54 	dest->grpprlPapx = NULL;
55 
56     if (src->lvlf.cbGrpprlChpx > 0)
57       {
58 	  dest->grpprlChpx = (U8 *) wvMalloc (src->lvlf.cbGrpprlChpx);
59 	  memcpy (dest->grpprlChpx, src->grpprlChpx, src->lvlf.cbGrpprlChpx);
60       }
61     else
62 	dest->grpprlChpx = NULL;
63 
64     if (src->numbertext == NULL)
65 	dest->numbertext = NULL;
66     else
67       {
68 	  len = src->numbertext[0];
69 	  dest->numbertext = (U16 *) wvMalloc (sizeof (U16) * (len + 2));
70 	  memcpy (dest->numbertext, src->numbertext, len + 2);
71       }
72 }
73 
74 void
wvGetLVL(LVL * lvl,wvStream * fd)75 wvGetLVL (LVL * lvl, wvStream * fd)
76 {
77     int len;
78     wvGetLVLF (&(lvl->lvlf), fd);
79     if (lvl->lvlf.cbGrpprlPapx > 0)
80       {
81 	  lvl->grpprlPapx = (U8 *) wvMalloc (lvl->lvlf.cbGrpprlPapx);
82 	  wvStream_read (lvl->grpprlPapx, sizeof (U8), lvl->lvlf.cbGrpprlPapx,
83 			 fd);
84       }
85     else
86 	lvl->grpprlPapx = NULL;
87     if (lvl->lvlf.cbGrpprlChpx > 0)
88       {
89 	  lvl->grpprlChpx = (U8 *) wvMalloc (lvl->lvlf.cbGrpprlChpx);
90 	  wvStream_read (lvl->grpprlChpx, sizeof (U8), lvl->lvlf.cbGrpprlChpx,
91 			 fd);
92       }
93     else
94 	lvl->grpprlChpx = NULL;
95     len = read_16ubit (fd);
96     if (len > 0)
97       {
98 	  int i = 0;
99 	  lvl->numbertext = (U16 *) wvMalloc (sizeof (U16) * (len + 2));
100 	  lvl->numbertext[0] = len;
101 	  for(i = 0; i < len; i++)
102 	    lvl->numbertext[i + 1] = read_16ubit (fd);
103 	  lvl->numbertext[len + 1] = 0;
104       }
105     else
106 	lvl->numbertext = NULL;
107     wvTrace (("len is %d\n", len));
108 }
109 
110 void
wvReleaseLVL(LVL * lvl)111 wvReleaseLVL (LVL * lvl)
112 {
113     if (lvl == NULL)
114 	return;
115     wvFree (lvl->grpprlPapx);
116     wvFree (lvl->grpprlChpx);
117     wvFree (lvl->numbertext);
118 }
119 
120 void
wvCopyLVLF(LVLF * dest,LVLF * src)121 wvCopyLVLF (LVLF * dest, LVLF * src)
122 {
123     memcpy (dest, src, sizeof (LVLF));
124 }
125 
126 void
wvGetLVLF(LVLF * item,wvStream * fd)127 wvGetLVLF (LVLF * item, wvStream * fd)
128 {
129     U8 temp8;
130     int i;
131 #ifdef PURIFY
132     wvInitLVLF (item);
133 #endif
134     item->iStartAt = read_32ubit (fd);
135     wvTrace (("iStartAt is %d\n", item->iStartAt));
136     item->nfc = read_8ubit (fd);
137     temp8 = read_8ubit (fd);
138     item->jc = temp8 & 0x03;
139     item->fLegal = (temp8 & 0x04) >> 2;
140     item->fNoRestart = (temp8 & 0x08) >> 3;
141     item->fPrev = (temp8 & 0x10) >> 4;
142     item->fPrevSpace = (temp8 & 0x20) >> 5;
143     item->fWord6 = (temp8 & 0x40) >> 6;
144     item->reserved1 = (temp8 & 0x80) >> 7;
145     for (i = 0; i < 9; i++)
146 	item->rgbxchNums[i] = read_8ubit (fd);
147     item->ixchFollow = read_8ubit (fd);;
148     item->dxaSpace = read_32ubit (fd);
149     item->dxaIndent = read_32ubit (fd);
150     item->cbGrpprlChpx = read_8ubit (fd);
151     item->cbGrpprlPapx = read_8ubit (fd);
152     item->reserved2 = read_16ubit (fd);;
153 }
154 
155 
156 void
wvInitLVLF(LVLF * item)157 wvInitLVLF (LVLF * item)
158 {
159     int i;
160     item->iStartAt = 1;
161     item->nfc = 0;
162     item->jc = 0;
163     item->fLegal = 0;
164     item->fNoRestart = 0;
165     item->fPrev = 0;
166     item->fPrevSpace = 0;
167     item->fWord6 = 0;
168     item->reserved1 = 0;
169     for (i = 0; i < 9; i++)
170 	item->rgbxchNums[i] = 0;
171     item->ixchFollow = 0;
172     item->dxaSpace = 0;
173     item->dxaIndent = 0;
174     item->cbGrpprlChpx = 0;
175     item->cbGrpprlPapx = 0;
176     item->reserved2 = 0;
177 }
178