-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwideint.cpp
188 lines (161 loc) · 3.83 KB
/
wideint.cpp
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
/**
** wideint.cpp
** Monday, July 8, 2002
**
** Extend the C library to cope more often with wide int's.
**/
/**
** Link with standard C libraries.
**/
#include <limits.h> /* INT_MAX ... */
#include <stdio.h> /* sprintf ... */
#include <stdlib.h> /* calloc ... */
#include <string.h> /* memset ... */
/**
** Link with the *.c* of ../plscsi/.
**/
#include "plscsi.h"
#ifdef __MSDOS__
/**
** Divide a wide length into the next of a series of thin lengths.
**/
static int TO_THIN_INT(UINT length)
{
int thinLength = (int) length;
if (thinLength != length)
{
if (length < 0)
{
return INT_MIN;
}
return INT_MAX;
}
return thinLength;
}
/**
** Repeat fread for a wide length.
**/
UINT FREAD(void * to, UINT width, UINT length, FILE * fi)
{
UINT result = 0;
int thinWidth = TO_THIN_INT(width);
if (thinWidth != width)
{
return -1;
}
if (!(length <= (INT_MOST_POS / thinWidth)))
{
return -1;
}
while (0 < length)
{
int thinLength = TO_THIN_INT(length);
int ioLength = fread(to, thinWidth, thinLength, fi);
if (0 <= ioLength) result += ioLength;
if (ioLength != thinLength) return result;
to = TO_VOID_P(TO_INT(to) + ((INT) thinWidth * (INT) thinLength));
length -= thinLength;
}
return result;
}
/**
** Repeat fwrite for a wide length.
**/
UINT FWRITE(void const * from, UINT width, UINT length, FILE * fi)
{
UINT result = 0;
int thinWidth = TO_THIN_INT(width);
if (thinWidth != width)
{
return -1;
}
if (!(length <= (INT_MOST_POS / thinWidth)))
{
return -1;
}
while (0 < length)
{
int thinLength = TO_THIN_INT(length);
int ioLength = fwrite(from, thinWidth, thinLength, fi);
if (0 <= ioLength) result += ioLength;
if (ioLength != thinLength) return result;
from = TO_VOID_P(TO_INT(from) + ((INT) thinWidth * (INT) thinLength));
length -= thinLength;
}
return result;
}
/**
** Repeat memcmp for a wide length.
**/
int MEMCMP(void * to, void * from, UINT length)
{
int result = 0;
while (0 < length)
{
int thinLength = TO_THIN_INT(length);
result = memcmp(to, from, thinLength);
if (result != 0)
{
return result;
}
to = TO_VOID_P(TO_INT(to) + thinLength);
from = TO_VOID_P(TO_INT(from) + thinLength);
length -= thinLength;
}
return result;
}
/**
** Repeat memmove for a wide length.
**/
void * MEMMOVE(void * to, void * from, UINT length)
{
void * result = to;
while (0 < length)
{
int thinLength = TO_THIN_INT(length);
memmove(to, from, thinLength);
to = TO_VOID_P(TO_INT(to) + thinLength);
from = TO_VOID_P(TO_INT(from) + thinLength);
length -= thinLength;
}
return result;
}
/**
** Repeat memset for a wide length.
**/
void * MEMSET(void * to, char cc, UINT length)
{
void * result = to;
while (0 < length)
{
int thinLength = TO_THIN_INT(length);
memset(to, cc, thinLength);
to = TO_VOID_P(TO_INT(to) + thinLength);
length -= thinLength;
}
return result;
}
/**
** Convert to a wide byte offset from an address.
**/
INT TO_INT(const void * vo)
{
INT ii = ((INT) vo);
#ifdef __MSDOS__
ii = ((((ii >> 0x10) & 0xFFFF) << 4) + (ii & 0xFFFF));
#endif
return ii;
}
/**
** Convert from a wide byte offset to an address.
**/
void * TO_VOID_P(INT ii)
{
#ifdef __MSDOS__
ii = (((ii >> 4) << 0x10) | (ii & 0xF));
#endif
void * vo = ((void *) ii);
return vo;
}
#endif /* __MSDOS__ */
/* end of file */