Debian Codegraph @

src/ext/fmemopen.c

raw
1 /**
2 * Copyright (c) 2009 Open Information Security Foundation
3 *
4 * \author Pablo Rincon Crespo <[email protected]>
5 * Based on FMem.c of Alexandre Flori (2008/10/17 AF)
6 */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "fmemopen.h"
14
15 #ifdef OS_DARWIN
16 #define USE_FMEM_WRAPPER 1
17 #endif
18
19 #ifdef OS_FREEBSD
20 #define USE_FMEM_WRAPPER 1
21 #endif
22
23 #ifdef USE_FMEM_WRAPPER
24
25 /**
26 * \brief Seek the mem file from offset and whence
27 * \param handler pointer to the memfile
28 * \param osffset number of bytes to move from whence
29 * \param whence SEEK_SET, SEEK_CUR, SEEK_END
30 * \retval pos the position by the last operation, -1 if sizes are out of bounds
31 */
32 static fpos_t SeekFn(void *handler, fpos_t offset, int whence) {
33 size_t pos = 0;
34 fmem_t *mem = handler;
35
36 switch (whence) {
37 case SEEK_SET:
38 if (offset > 0 && (size_t)offset <= mem->size)
39 return mem->pos = offset;
40 break;
41 case SEEK_CUR:
42 if (mem->pos + offset <= mem->size)
43 return mem->pos = offset;
44 break;
45 case SEEK_END:
46 /* must be negative */
47 if (mem->size + offset <= mem->size)
48 return pos = mem->size + offset;
49 break;
50 }
51
52 return -1;
53 }
54
55 /**
56 * \brief Read from the buffer looking for the available memory limits
57 * \param handler pointer to the memfile
58 * \param buf buffer to read from the handler
59 * \param number of bytes to read
60 * \retval count , the number of bytes read
61 */
62 static int ReadFn(void *handler, char *buf, int size) {
63 size_t count = 0;
64 fmem_t *mem = handler;
65 size_t available = mem->size - mem->pos;
66
67 if (size < 0) return - 1;
68
69 if ((size_t)size > available)
70 size = available;
71
72 while (count < (size_t)size)
73 buf[count++] = mem->buffer[mem->pos++];
74
75 return count;
76 }
77
78 /**
79 * \brief Write into the buffer looking for the available memory limits
80 * \param handler pointer to the memfile
81 * \param buf buffer to write in the handler
82 * \param number of bytes to write
83 * \retval count , the number of bytes writen
84 */
85 static int WriteFn(void *handler, const char *buf, int size) {
86 size_t count = 0;
87 fmem_t *mem = handler;
88 size_t available = mem->size - mem->pos;
89
90 if (size < 0) return - 1;
91
92 if ((size_t)size > available)
93 size = available;
94
95 while (count < (size_t)size)
96 mem->buffer[mem->pos++] = buf[count++];
97
98 return count;
99 }
100
101 /**
102 * \brief close the mem file handler
103 * \param handler pointer to the memfile
104 * \retval 0 on succesful
105 */
106 static int CloseFn(void *handler) {
107 free (handler);
108 return 0;
109 }
110
111 /**
112 * \brief portable version of SCFmemopen for OS X / BSD built on top of funopen()
113 * \param buffer that holds the file content
114 * \param size of the file buffer
115 * \param mode mode of the file to open
116 * \retval pointer to the file; NULL if something is wrong
117 */
118 FILE *fmemopen(void *buf, size_t size, const char *mode) {
119 fmem_t *mem = (fmem_t *) malloc(sizeof(fmem_t));
120
121 memset(mem, 0, sizeof(fmem_t));
122 mem->size = size, mem->buffer = buf;
123
124 return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn);
125 }
126
127 #endif