forked from RealDeuce/OpenDoors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathODUtil.c
More file actions
461 lines (395 loc) · 13.6 KB
/
Copy pathODUtil.c
File metadata and controls
461 lines (395 loc) · 13.6 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* OpenDoors Online Software Programming Toolkit
* (C) Copyright 1991 - 1999 by Brian Pirie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* File: ODUtil.c
*
* Description: Implements the non-platform specific utility functions that
* are defined in odutil.h. Platform specific utility functions
* are implemented in odplat.c.
*
*
* Revisions: Date Ver Who Change
* ---------------------------------------------------------------
* Nov 01, 1994 6.00 BP Created.
* Dec 31, 1994 6.00 BP Added ODMakeFilename().
* Nov 13, 1995 6.00 BP 32-bit portability.
* Nov 23, 1995 6.00 BP Added ODDWordDivide().
* Nov 23, 1995 6.00 BP Added ODDStringHasTail().
* Nov 23, 1995 6.00 BP Added ODFileSize().
* Nov 24, 1995 6.00 BP ODMakeFilename(): handle empty path.
* Feb 19, 1996 6.00 BP Changed version number to 6.00.
* Mar 03, 1996 6.10 BP Begin version 6.10.
* Mar 06, 1996 6.10 BP Added ODDWordMultiply().
* Aug 10, 2003 6.23 SH *nix support
*/
#define BUILDING_OPENDOORS
#include <string.h>
#include <stdio.h>
#include "OpenDoor.h"
#include "ODStr.h"
#include "ODUtil.h"
#include "ODGen.h"
/* ========================================================================= */
/* General string manipulation functions. */
/* ========================================================================= */
/* ----------------------------------------------------------------------------
* ODStringCopy()
*
* Safely copies one string to another. Unlike strncpy(), ODStringCopy()
* ensures that the destination string is always '\0' terminated.
*
* Parameters: pszDest - Pointer to destination string to which to copy
* characters.
*
* pszSource - Pointer to source string from which to copy
* characters.
*
* nSizeOfDest - Maximum number of characters to place in pszDest,
* INCLUDING the '\0' string terminator.
*
* Return: void
*/
void ODStringCopy(char *pszDest, CONST char *pszSource, INT nSizeofDest)
{
ASSERT(pszDest != NULL);
ASSERT(pszSource != NULL);
ASSERT(nSizeofDest > 0);
/* Copy at most the specified number of bytes from source to dest, using */
/* (presumably well optimized) strncpy(). */
strncpy(pszDest, pszSource, nSizeofDest);
/* Ensure that destination string is '\0' terminated. This will not */
/* already be the case if strlen(pszSource) >= nSizeofDest. */
pszDest[nSizeofDest - 1] = '\0';
}
/* ----------------------------------------------------------------------------
* ODStringCToPascal()
*
* Converts a string from C's zero-terminated string format to Pascal's
* length byte + string data format.
*
* Parameters: psPascalString - Pointer to the destination string.
*
* btMaxPascalLength - Size of the destination string, as declared
* in Pascal.
*
* pszCString - Pointer to the source string, in C format.
*
* Return: A pointer to psPascalString.
*/
char *ODStringCToPascal(char *psPascalString, BYTE btMaxPascalLength,
char *pszCString)
{
BYTE btCStringLength = strlen(pszCString);
ASSERT(psPascalString != NULL);
ASSERT(btMaxPascalLength > 0);
ASSERT(pszCString != NULL);
memcpy((char *)psPascalString + 1,
pszCString, *psPascalString = (btCStringLength < btMaxPascalLength)
? btCStringLength : btMaxPascalLength);
return(psPascalString);
}
/* ----------------------------------------------------------------------------
* ODStringPascalToC()
*
* Converts a string from Pascal's length byte + string data format to C's
* zero-terminated string format.
*
* Parameters: pszCString - Pointer to destination string.
*
* psPascalString - Pointer to Pascal format source string.
*
* btMaxLength - Length of C string.
*
* Return: A pointer to pszCString.
*/
char *ODStringPascalToC(char *pszCString, char *psPascalString,
BYTE btMaxLength)
{
ASSERT(pszCString != NULL);
ASSERT(psPascalString != NULL);
ASSERT(btMaxLength > 0);
if(*(BYTE *)psPascalString <= btMaxLength)
{
memcpy(pszCString, (char *)psPascalString + 1, *psPascalString);
pszCString[(int)psPascalString[0]] = '\0';
}
else
{
pszCString[0] = '\0';
}
return(pszCString);
}
/* ----------------------------------------------------------------------------
* ODStringHasTail()
*
* Determines whether a string ends in exactly the specified sequence of
* characters.
*
* Parameters: pszFullString - String to examine.
*
* pszTail - String to look for at the end of
* pszFullString.
*
* Return: TRUE if the pszFullString does end with pszTail, FALSE if
* it does not.
*/
BOOL ODStringHasTail(char *pszFullString, char *pszTail)
{
INT nTailLength = strlen(pszTail);
INT nFullStringLength = strlen(pszFullString);
ASSERT(pszFullString != NULL);
ASSERT(pszTail != NULL);
if(nFullStringLength < nTailLength)
{
return(FALSE);
}
return(stricmp(pszFullString + (nFullStringLength - nTailLength), pszTail) == 0);
}
/* ========================================================================= */
/* File-related functions. */
/* ========================================================================= */
/* ----------------------------------------------------------------------------
* ODMakeFilename()
*
* Generates a fully-qualified filename from a path and base filename.
*
* Parameters: pszOut - String to store generated filename in.
*
* pszPath - Directory name. May be the same as pszOut, or
* may be different.
*
* pszFilename - Base filename.
*
* nMaxOutSize - Size of pszOut. This value should be one more
* than the maximum number of characters to be
* stored in the output string.
*
* Return: kODRCSuccess on success, or an error code on failure.
*/
tODResult ODMakeFilename(char *pszOut, CONST char *pszPath,
CONST char *pszFilename, INT nMaxOutSize)
{
/* Validate parameters in debug mode */
ASSERT(pszPath != NULL);
ASSERT(pszFilename != NULL);
ASSERT(pszOut != NULL);
ASSERT(pszFilename != pszOut);
ASSERT(nMaxOutSize > 0);
/* Check that there is enough room in the destination string to hold */
/* both source strings plus possibly an additional \-seperator. */
if((INT)(strlen(pszPath) + strlen(pszFilename) + 1) > nMaxOutSize - 1)
{
return(kODRCFilenameTooLong);
}
/* Copy path to output filename, if the addresses are different. */
if(pszPath != pszOut)
{
strcpy(pszOut, pszPath);
}
/* Ensure there is a trailing backslash, if path was not empty. */
#ifdef ODPLAT_NIX
#else
if(pszOut[strlen(pszOut) - 1] != DIRSEP && strlen(pszOut) > 0)
{
strcat(pszOut, DIRSEP_STR);
}
#endif
/* Append base filename. */
strcat(pszOut, pszFilename);
return(kODRCSuccess);
}
/* ----------------------------------------------------------------------------
* ODFileSize()
*
* Determines the size of a currently open file.
*
* Parameters: pfFile - Pointer to an already open file to examine.
*
* Return: The size of the file. In the case of a file that is open in
* binary mode, this will be the file length in bytes.
*/
DWORD ODFileSize(FILE *pfFile)
{
DWORD dwOriginal;
DWORD dwFileSize;
ASSERT(pfFile != NULL);
dwOriginal = ftell(pfFile);
fseek(pfFile, 0L, SEEK_END);
dwFileSize = ftell(pfFile);
fseek(pfFile, dwOriginal, SEEK_SET);
return(dwFileSize);
}
/* ========================================================================= */
/* DWORD math functions. */
/* ========================================================================= */
/* ----------------------------------------------------------------------------
* ODDWordShiftLeft()
*
* Shifts a DWORD to the left by the specified number of bits.
*
* Parameters: dwValue - Value to be shifted.
*
* btDistance - Distance to shift dwValue by.
*
* Return: Result of the shift operation.
*/
DWORD ODDWordShiftLeft(DWORD dwValue, BYTE btDistance)
{
WORD wUpper;
WORD wLower;
wLower = (WORD)dwValue;
wUpper = *(WORD *)(((BYTE *)(&dwValue)) + 2);
while(btDistance--)
{
wUpper <<= 1;
wUpper |= (wLower & 0x8000) >> 15;
wLower <<= 1;
}
dwValue = wLower;
*(WORD *)(((BYTE *)(&dwValue)) + 2) = wUpper;
return(dwValue);
}
/* ----------------------------------------------------------------------------
* ODDWordShiftRight()
*
* Shifts a DWORD to the right by the specified number of bits.
*
* Parameters: dwValue - Value to be shifted.
*
* btDistance - Distance to shift dwValue by.
*
* Return: Result of the shift operation.
*/
DWORD ODDWordShiftRight(DWORD dwValue, BYTE btDistance)
{
WORD wUpper;
WORD wLower;
wLower = (WORD)dwValue;
wUpper = *(WORD *)(((BYTE *)(&dwValue)) + 2);
while(btDistance--)
{
wLower >>= 1;
wLower |= (wUpper & 0x0001) << 15;
wUpper >>= 1;
}
dwValue=wLower;
*(WORD *)(((BYTE *)(&dwValue)) + 2) = wUpper;
return(dwValue);
}
/* ----------------------------------------------------------------------------
* ODDWordDivide()
*
* Divides one DWORD by another DWORD, calculating the quotient and remainder.
*
* Parameters: pdwQuotient - Location where the quotient should be stored,
* or NULL if quotient is not required.
*
* pdwRemainder - Location where remainder should be stored,
* or NULL if remainder is not required.
*
* dwDividend - Dividend to be divided by divisor.
*
* dwDivisor - Divisor to divide dividend by.
*
* Return: TRUE on success or FALSE on failure.
*/
BOOL ODDWordDivide(DWORD *pdwQuotient, DWORD *pdwRemainder,
DWORD dwDividend, DWORD dwDivisor)
{
INT nTimes = 0;
DWORD dwQuotient;
DWORD dwRemainder;
/* Check for divide by zero in debug versions. */
ASSERT(dwDivisor != 0);
/* Check that divisor is not zero. (An attempt to divide by zero will */
/* put this algorithm into an infinite loop, rather than triggering */
/* a divide fault.) */
if(dwDivisor == 0L)
{
return(FALSE);
}
/* Initialize remainder to be entire dividend */
dwRemainder = dwDividend;
/* Initialize quotient to 0 */
dwQuotient = 0L;
/* Determine largest required multiple of divisor */
while(dwRemainder >= dwDivisor)
{
dwDivisor = ODDWordShiftLeft(dwDivisor, 1);
++nTimes;
}
/* Loop across for all multiples of divisor, beginning with the largest */
do
{
dwQuotient = ODDWordShiftLeft(dwQuotient, 1);
/* If current remainder is >= this multiple of the divisor */
if(dwRemainder >= dwDivisor)
{
/* Subtract the multiple of the divisor from the remainder */
dwRemainder -= dwDivisor;
/* The next bit of the quotient should be a 1 */
dwQuotient |= 1L;
}
/* Divide current multiple of divisor by two */
dwDivisor = ODDWordShiftRight(dwDivisor, 1);
/* Repeat for all multiples of the divisor */
} while(nTimes--);
/* If caller asked for quotient, then return it */
if(pdwQuotient != NULL)
{
*pdwQuotient = dwQuotient;
}
/* If caller asked for remainder, then return it */
if(pdwRemainder != NULL)
{
*pdwRemainder = dwRemainder;
}
return(TRUE);
}
/* ----------------------------------------------------------------------------
* ODDWordDivide()
*
* Multiplies one DWORD by another, returning the product. Multiplication
* is performed by using at most 32 additions.
*
* Parameters: dwMultiplicand - The multiplicand.
*
* dwMultiplier - The multiplier.
*
* Return: Result of the multiplication.
*/
DWORD ODDWordMultiply(DWORD dwMultiplicand, DWORD dwMultiplier)
{
DWORD dwResult = 0;
/* Loop while multiplier is not zero */
while(dwMultiplier != 0)
{
/* If least significant bit of multiplier is set */
if(dwMultiplier & 0x00000001)
{
/* Add multiplicand to product */
dwResult += dwMultiplicand;
}
/* Shift multiplicand left one bit */
dwMultiplicand = ODDWordShiftLeft(dwMultiplicand, 1);
/* Shift multiplier right one bit */
dwMultiplier = ODDWordShiftRight(dwMultiplier, 1);
}
/* Return the final result to the caller. */
return(dwResult);
}