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 <ctype.h>
28
29 #ifdef HAVE_MALLOC_H
30 #include <malloc.h>
31 #endif
32
33 #include "wv.h"
34
35 #define SOME_ARBITRARY_LIMIT 1
36
37 int
wvGetEmpty_PLCF(U32 ** cps,U32 * nocps,U32 offset,U32 len,wvStream * fd)38 wvGetEmpty_PLCF (U32 ** cps, U32 * nocps, U32 offset, U32 len, wvStream * fd)
39 {
40 U32 i;
41 if (len == 0)
42 {
43 *cps = NULL;
44 *nocps = 0;
45 }
46 else
47 {
48 *nocps = len / 4;
49 *cps = (U32 *) malloc (*nocps * sizeof (U32));
50 if (*cps == NULL)
51 {
52 wvError (
53 ("NO MEM 3, failed to alloc %d bytes\n",
54 *nocps * sizeof (U32)));
55 return (1);
56 }
57 wvStream_goto (fd, offset);
58 for (i = 0; i < *nocps; i++)
59 (*cps)[i] = read_32ubit (fd);
60 }
61 return (0);
62 }
63
64 void
_wvFree(void * ptr)65 _wvFree (void* ptr)
66 {
67 g_free(ptr);
68 }
69
70 /**
71 * Very simple malloc wrapper
72 */
73 void *
wvMalloc(U32 size)74 wvMalloc (U32 size)
75 {
76 void *p = NULL;
77 int ntries = 0;
78
79 if (size == 0)
80 return NULL;
81
82 /* loop trying to obtain memory */
83 do
84 {
85 p = (void *) g_try_malloc (size);
86 if (p)
87 break;
88 ntries++;
89 }
90 while (ntries < SOME_ARBITRARY_LIMIT);
91
92 if (!p)
93 {
94 wvError (("Could not allocate %d bytes\n", size));
95 exit (-1);
96 }
97
98 /* zero out the memory */
99 memset ( p, 0, size ) ;
100
101 return p;
102 }
103
104 /*
105 If the
106 second most significant bit is clear, then this indicates the actual file
107 offset of the unicode character (two bytes). If the second most significant
108 bit is set, then the actual address of the codepage-1252 compressed version
109 of the unicode character (one byte), is actually at the offset indicated by
110 clearing this bit and dividing by two.
111 */
112 /*
113 flag = 1 means that this is a one byte char, 0 means that this is a type
114 byte char
115 */
116 U32
wvNormFC(U32 fc,int * flag)117 wvNormFC (U32 fc, int *flag)
118 {
119 if (fc & 0x40000000UL)
120 {
121 fc = fc & 0xbfffffffUL;
122 fc = fc / 2;
123 if (flag)
124 *flag = 1;
125 }
126 else if (flag)
127 *flag = 0;
128 return (fc);
129 }
130
131 U16
wvGetChar(wvStream * fd,U8 chartype)132 wvGetChar (wvStream * fd, U8 chartype)
133 {
134 if (chartype == 1)
135 return (read_8ubit (fd));
136 else
137 return (read_16ubit (fd));
138 return (0);
139 }
140
141 int
wvIncFC(U8 chartype)142 wvIncFC (U8 chartype)
143 {
144 if (chartype == 1)
145 return (1);
146 return (2);
147 }
148
149 int
wvStrlen(const char * str)150 wvStrlen (const char *str)
151 {
152 if (str == NULL)
153 return (0);
154 return (strlen (str));
155 }
156
157 char *
wvStrcat(char * dest,const char * src)158 wvStrcat (char *dest, const char *src)
159 {
160 if (src != NULL)
161 return (strcat (dest, src));
162 else
163 return (dest);
164 }
165
166 void
wvAppendStr(char ** orig,const char * add)167 wvAppendStr (char **orig, const char *add)
168 {
169 int pos;
170 wvTrace (("got this far\n"));
171 pos = wvStrlen (*orig);
172 wvTrace (("len is %d %d\n", pos, wvStrlen (add)));
173 (*orig) = (char *) realloc (*orig, pos + wvStrlen (add) + 1);
174 (*orig)[pos] = '\0';
175 wvTrace (("3 test str of %s\n", *orig));
176 wvStrcat (*orig, add);
177 wvTrace (("3 test str of %s\n", *orig));
178 }
179
180 void
wvStrToUpper(char * str)181 wvStrToUpper (char *str)
182 {
183 int i;
184 if (str == NULL)
185 return;
186 for (i = 0; i < wvStrlen (str); i++)
187 str[i] = toupper (str[i]);
188 }
189