-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.c
188 lines (168 loc) · 3.62 KB
/
generic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* wvWare
* Copyright (C) Caolan McNamara, Dom Lachowicz, and others
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include "wv.h"
#define SOME_ARBITRARY_LIMIT 1
int
wvGetEmpty_PLCF (U32 ** cps, U32 * nocps, U32 offset, U32 len, wvStream * fd)
{
U32 i;
if (len == 0)
{
*cps = NULL;
*nocps = 0;
}
else
{
*nocps = len / 4;
*cps = (U32 *) malloc (*nocps * sizeof (U32));
if (*cps == NULL)
{
wvError (
("NO MEM 3, failed to alloc %d bytes\n",
*nocps * sizeof (U32)));
return (1);
}
wvStream_goto (fd, offset);
for (i = 0; i < *nocps; i++)
(*cps)[i] = read_32ubit (fd);
}
return (0);
}
void
_wvFree (void* ptr)
{
g_free(ptr);
}
/**
* Very simple malloc wrapper
*/
void *
wvMalloc (U32 size)
{
void *p = NULL;
int ntries = 0;
if (size == 0)
return NULL;
/* loop trying to obtain memory */
do
{
p = (void *) g_try_malloc (size);
if (p)
break;
ntries++;
}
while (ntries < SOME_ARBITRARY_LIMIT);
if (!p)
{
wvError (("Could not allocate %d bytes\n", size));
exit (-1);
}
/* zero out the memory */
memset ( p, 0, size ) ;
return p;
}
/*
If the
second most significant bit is clear, then this indicates the actual file
offset of the unicode character (two bytes). If the second most significant
bit is set, then the actual address of the codepage-1252 compressed version
of the unicode character (one byte), is actually at the offset indicated by
clearing this bit and dividing by two.
*/
/*
flag = 1 means that this is a one byte char, 0 means that this is a type
byte char
*/
U32
wvNormFC (U32 fc, int *flag)
{
if (fc & 0x40000000UL)
{
fc = fc & 0xbfffffffUL;
fc = fc / 2;
if (flag)
*flag = 1;
}
else if (flag)
*flag = 0;
return (fc);
}
U16
wvGetChar (wvStream * fd, U8 chartype)
{
if (chartype == 1)
return (read_8ubit (fd));
else
return (read_16ubit (fd));
return (0);
}
int
wvIncFC (U8 chartype)
{
if (chartype == 1)
return (1);
return (2);
}
int
wvStrlen (const char *str)
{
if (str == NULL)
return (0);
return (strlen (str));
}
char *
wvStrcat (char *dest, const char *src)
{
if (src != NULL)
return (strcat (dest, src));
else
return (dest);
}
void
wvAppendStr (char **orig, const char *add)
{
int pos;
wvTrace (("got this far\n"));
pos = wvStrlen (*orig);
wvTrace (("len is %d %d\n", pos, wvStrlen (add)));
(*orig) = (char *) realloc (*orig, pos + wvStrlen (add) + 1);
(*orig)[pos] = '\0';
wvTrace (("3 test str of %s\n", *orig));
wvStrcat (*orig, add);
wvTrace (("3 test str of %s\n", *orig));
}
void
wvStrToUpper (char *str)
{
int i;
if (str == NULL)
return;
for (i = 0; i < wvStrlen (str); i++)
str[i] = toupper (str[i]);
}