-
Notifications
You must be signed in to change notification settings - Fork 3
/
mfs.cpp
477 lines (420 loc) · 11.9 KB
/
mfs.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
--------------------------------------------------------------------------------
- Module : mfs.cpp
- Description : A general purpose library for manipulating a memory area
- as if it were a file.
- mfs_ stands for memory file system.
- Author : Mike Johnson - Banctec AB 03/07/96
- Adaptations : Tim Zaman - Picturae, Pixelprisma, 06/05/2015
--------------------------------------------------------------------------------
-
- Copyright (c) 1996 Mike Johnson
- Copyright (c) 1996 BancTec AB
-
- Permission to use, copy, modify, distribute, and sell this software
- for any purpose is hereby granted without fee, provided
- that (i) the above copyright notices and this permission notice appear in
- all copies of the software and related documentation, and (ii) the names of
- Mike Johnson and BancTec may not be used in any advertising or
- publicity relating to the software.
--------------------------------------------------------------------------------
*/
/*
--------------------------------------------------------------------------------
- Includes
--------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "mfs.h"
/*
--------------------------------------------------------------------------------
- Function code
--------------------------------------------------------------------------------
*/
/*
--------------------------------------------------------------------------------
- Function : extend_mem_file ()
-
- Arguments : File descriptor, length to extend to.
-
- Returns : 0 - All OK, -1 - realloc () failed.
-
- Description : Increase the amount of memory allocated to a file.
-
--------------------------------------------------------------------------------
*/
static int extend_mem_file (mfs_file *fd, int size)
{
void *new_mem;
int ret;
if ((new_mem = realloc (fd->buf, size)) == (void *) NULL)
ret = -1;
else
{
fd->buf = (char *) new_mem;
ret = 0;
}
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_open ()
-
- Arguments : Pointer to allocated buffer, initial size of buffer,
- mode spec (r, w, a)
-
- Returns : File descriptor or -1 if error.
-
- Description : Register this area of memory (which has been allocated
- and has a file read into it) under the mem_file library.
- A file descriptor is returned which can the be passed
- back to TIFFClientOpen and used as if it was a disk
- based fd.
- If the call is for mode 'w' then pass (void *)NULL as
- the buffer and zero size and the library will
- allocate memory for you.
- If the mode is append then pass (void *)NULL and size
- zero or with a valid address.
-
--------------------------------------------------------------------------------
*/
int mfs_open (void *buffer, int size, char *mode, mfs_file *fd)
{
int ret;
void *tmp;
ret = 0;
fd->buf_open = false;
if (*mode == 'r')
{
if (buffer == (void *)NULL)
{
ret = -1;
errno = EINVAL;
}
else
{
fd->buf = (char *)buffer;
fd->buf_size = size;
fd->buf_off = 0;
}
}
else if (*mode == 'w')
{
if (buffer != (void *)NULL)
{
ret = -1;
errno = EINVAL;
}
else
{
tmp = malloc (0); /* Get a pointer */
if (tmp == (void *)NULL)
{
ret = -1;
errno = ENOMEM;
}
else
{
fd->buf = (char *)tmp;
fd->buf_size = 0;
fd->buf_off = 0;
}
}
}
else if (*mode == 'a')
{
if (buffer == (void *) NULL) /* Create space for client */
{
tmp = malloc (0); /* Get a pointer */
if (tmp == (void *)NULL)
{
ret = -1;
errno = ENOMEM;
}
else
{
fd->buf = (char *)tmp;
fd->buf_size = 0;
fd->buf_off = 0;
}
}
else /* Client has file read in already */
{
fd->buf = (char *)buffer;
fd->buf_size = size;
fd->buf_off = 0;
}
}
else /* Some other invalid combination of parameters */
{
ret = -1;
errno = EINVAL;
}
if (ret != -1)
{
fd->buf_mode = *mode;
fd->buf_open = true;
}
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_lseek ()
-
- Arguments : File descriptor, offset, whence
-
- Returns : as per man lseek (2)
-
- Description : Does the same as lseek (2) except on a memory based file.
- Note: the memory area will be extended if the caller
- attempts to seek past the current end of file (memory).
-
--------------------------------------------------------------------------------
*/
int mfs_lseek (mfs_file *fd, int offset, int whence)
{
int ret;
long test_off;
if (!fd->buf_open) /* Not open */
{
ret = -1;
errno = EBADF;
}
else if (offset < 0 && whence == SEEK_SET)
{
ret = -1;
errno = EINVAL;
}
else
{
switch (whence)
{
case SEEK_SET:
if (offset > fd->buf_size)
extend_mem_file (fd, offset);
fd->buf_off = offset;
ret = offset;
break;
case SEEK_CUR:
test_off = fd->buf_off + offset;
if (test_off < 0)
{
ret = -1;
errno = EINVAL;
}
else
{
if (test_off > fd->buf_size)
extend_mem_file (fd, test_off);
fd->buf_off = test_off;
ret = test_off;
}
break;
case SEEK_END:
test_off = fd->buf_size + offset;
if (test_off < 0)
{
ret = -1;
errno = EINVAL;
}
else
{
if (test_off > fd->buf_size)
extend_mem_file (fd, test_off);
fd->buf_off = test_off;
ret = test_off;
}
break;
default:
errno = EINVAL;
ret = -1;
break;
}
}
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_read ()
-
- Arguments : File descriptor, buffer, size
-
- Returns : as per man read (2)
-
- Description : Does the same as read (2) except on a memory based file.
- Note: An attempt to read past the end of memory currently
- allocated to the file will return 0 (End Of File)
-
--------------------------------------------------------------------------------
*/
int mfs_read (mfs_file *fd, void *clnt_buf, int size)
{
int ret;
if (!fd->buf_open || fd->buf_mode != 'r')
{
/* File is either not open, or not opened for read */
ret = -1;
errno = EBADF;
}
else if (fd->buf_off + size > fd->buf_size)
{
ret = 0; /* EOF */
}
else
{
memcpy (clnt_buf, (void *) (fd->buf + fd->buf_off), size);
fd->buf_off = fd->buf_off + size;
ret = size;
}
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_write ()
-
- Arguments : File descriptor, buffer, size
-
- Returns : as per man write (2)
-
- Description : Does the same as write (2) except on a memory based file.
- Note: the memory area will be extended if the caller
- attempts to write past the current end of file (memory).
-
--------------------------------------------------------------------------------
*/
int mfs_write (mfs_file *fd, void *clnt_buf, int size)
{
int ret;
if (!fd->buf_open || fd->buf_mode == 'r')
{
/* Either the file is not open or it is opened for reading only */
ret = -1;
errno = EBADF;
}
else if (fd->buf_mode == 'w')
{
/* Write */
if (fd->buf_off + size > fd->buf_size)
{
extend_mem_file (fd, fd->buf_off + size);
fd->buf_size = (fd->buf_off + size);
}
memcpy ((fd->buf + fd->buf_off), clnt_buf, size);
fd->buf_off = fd->buf_off + size;
ret = size;
}
else
{
/* Append */
if (fd->buf_off != fd->buf_size)
fd->buf_off = fd->buf_size;
extend_mem_file (fd, fd->buf_off + size);
fd->buf_size += size;
memcpy ((fd->buf + fd->buf_off), clnt_buf, size);
fd->buf_off = fd->buf_off + size;
ret = size;
}
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_size ()
-
- Arguments : File descriptor
-
- Returns : integer file size
-
- Description : This function returns the current size of the file in bytes.
-
--------------------------------------------------------------------------------
*/
int mfs_size (mfs_file *fd)
{
int ret;
if (!fd->buf_open) /* Not open */
{
ret = -1;
errno = EBADF;
}
else
ret = fd->buf_size;
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_map ()
-
- Arguments : File descriptor, ptr to address, ptr to length
-
- Returns : Map status (succeeded or otherwise)
-
- Description : This function tells the client where the file is mapped
- in memory and what size the mapped area is. It is provided
- to satisfy the MapProc function in libtiff. It pretends
- that the file has been mmap (2)ped.
-
--------------------------------------------------------------------------------
*/
int mfs_map (mfs_file *fd, char **addr, size_t *len)
{
int ret;
if (!fd->buf_open) /* Not open */
{
ret = -1;
errno = EBADF;
}
else
{
*addr = fd->buf;
*len = fd->buf_size;
ret = 0;
}
return (ret);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_unmap ()
-
- Arguments : File descriptor
-
- Returns : UnMap status (succeeded or otherwise)
-
- Description : This function does nothing as the file is always
- in memory.
-
--------------------------------------------------------------------------------
*/
int mfs_unmap (mfs_file *fd)
{
return (0);
}
/*
--------------------------------------------------------------------------------
- Function : mfs_close ()
-
- Arguments : File descriptor
-
- Returns : close status (succeeded or otherwise)
-
- Description : Close the open memory file. (Make fd available again.)
-
--------------------------------------------------------------------------------
*/
int mfs_close (mfs_file *fd)
{
int ret;
if (!fd->buf_open) /* Not open */
{
ret = -1;
errno = EBADF;
}
else
{
fd->buf_open = false;
ret = 0;
}
return (ret);
}