]> git.imager.perl.org - imager.git/blob - iolayer.h
- added many bad BMP files to test various code paths in bmp.c, and
[imager.git] / iolayer.h
1 #ifndef _IOLAYER_H_
2 #define _IOLAYER_H_
3
4
5 /* How the IO layer works:
6  * 
7  * Start by getting an io_glue object.  Then define its
8  * datasource via io_obj_setp_buffer or io_obj_setp_cb.  Before
9  * using the io_glue object be sure to call io_glue_commit_types().
10  * After that data can be read via the io_glue->readcb() method.
11  *
12  */
13
14
15 #include <stdio.h>
16 #ifndef _MSC_VER
17 #include <unistd.h>
18 #endif
19 #include <sys/types.h>
20
21 /* #define BBSIZ 1096 */
22 #define BBSIZ 16384
23 #define IO_FAKE_SEEK 1<<0L
24 #define IO_TEMP_SEEK 1<<1L
25
26
27 typedef union { int i; void *p; } iorp;
28
29 typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type;
30
31 #ifdef _MSC_VER
32 typedef int ssize_t;
33 #endif
34
35 struct io_glue;
36
37 /* Callbacks we give out */
38
39 typedef ssize_t(*readp) (struct io_glue *ig, void *buf, size_t count);
40 typedef ssize_t(*writep)(struct io_glue *ig, const void *buf, size_t count);
41 typedef off_t  (*seekp) (struct io_glue *ig, off_t offset, int whence);
42 typedef void   (*closep)(struct io_glue *ig);
43 typedef ssize_t(*sizep) (struct io_glue *ig);
44
45 typedef void   (*closebufp)(void *p);
46
47
48 /* Callbacks we get */
49
50 typedef ssize_t(*readl) (void *p, void *buf, size_t count);
51 typedef ssize_t(*writel)(void *p, const void *buf, size_t count);
52 typedef off_t  (*seekl) (void *p, off_t offset, int whence);
53 typedef void   (*closel)(void *p);
54 typedef void   (*destroyl)(void *p);
55 typedef ssize_t(*sizel) (void *p);
56
57 extern char *io_type_names[];
58
59
60 typedef struct io_blink {
61   char buf[BBSIZ];
62   /* size_t cnt; */
63   size_t len;                   /* How large is this buffer = BBZIS for now */
64   struct io_blink *next;
65   struct io_blink *prev;
66 } io_blink;
67
68
69 /* Structures that describe callback interfaces */
70
71 typedef struct {
72   off_t offset;
73   off_t cpos;
74 } io_ex_rseek;
75
76
77 typedef struct {
78   off_t offset;
79   off_t cpos;
80   io_blink *head;
81   io_blink *tail;
82   io_blink *cp;
83 } io_ex_fseek;
84
85
86 typedef struct {
87   off_t offset;                 /* Offset of the source - not used */
88   off_t length;                 /* Total length of chain in bytes */
89   io_blink *head;               /* Start of chain */
90   io_blink *tail;               /* End of chain */
91   off_t tfill;                  /* End of stream in last link */
92   io_blink *cp;                 /* Current element of list */
93   off_t cpos;                   /* Offset within the current */
94   off_t gpos;                   /* Global position in stream */
95 } io_ex_bchain;
96
97 typedef struct {
98   off_t offset;                 /* Offset of the source - not used */
99   off_t cpos;                   /* Offset within the current */
100 } io_ex_buffer;
101
102
103
104 /* Structures to describe data sources */
105
106 typedef struct {
107   io_type       type;
108   int           fd;
109 } io_fdseek;
110
111 typedef struct {
112   io_type       type;           /* Must be first parameter */
113   char          *name;          /* Data source name */
114   char          *data;
115   size_t        len;
116   closebufp     closecb;        /* free memory mapped segment or decrement refcount */
117   void          *closedata;
118 } io_buffer;
119
120 typedef struct {
121   io_type       type;           /* Must be first parameter */
122   char          *name;          /* Data source name */
123   void          *p;             /* Callback data */
124   readl         readcb;
125   writel        writecb;
126   seekl         seekcb;
127   closel        closecb;
128   destroyl      destroycb;
129 } io_cb;
130
131 typedef union {
132   io_type       type;
133   io_fdseek     fdseek;
134   io_buffer     buffer;
135   io_cb         cb;
136 } io_obj;
137
138 typedef struct io_glue {
139   io_obj        source;
140   int           flags;          /* Flags */
141   void          *exdata;        /* Pair specific data */
142   readp         readcb;
143   writep        writecb;
144   seekp         seekcb;
145   closep        closecb;
146   sizep         sizecb;
147 } io_glue;
148
149 void io_obj_setp_buffer(io_obj *io, char *p, size_t len, closebufp closecb, void *closedata);
150 void io_obj_setp_cb      (io_obj *io, void *p, readl readcb, writel writecb, seekl seekcb);
151 void io_obj_setp_cb2     (io_obj *io, void *p, readl readcb, writel writecb, seekl seekcb, closel closecb, destroyl destroycb);
152 void io_glue_commit_types(io_glue *ig);
153 void io_glue_gettypes    (io_glue *ig, int reqmeth);
154
155
156 /* XS functions */
157 io_glue *io_new_fd(int fd);
158 io_glue *io_new_bufchain(void);
159 io_glue *io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata);
160 io_glue *io_new_cb(void *p, readl readcb, writel writecb, seekl seekcb, closel closecb, destroyl destroycb);
161 size_t   io_slurp(io_glue *ig, unsigned char **c);
162 void     io_glue_DESTROY(io_glue *ig);
163
164 #endif /* _IOLAYER_H_ */