VerifyAndMapCode: Only map the code if it passes validation
[nativeclient.git] / service_runtime / gio.h
blob9878c69c6492583485f87930f5762c7318ad7b6a
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Generic I/O interface.
36 #ifndef SERVICE_RUNTIME_GIO_H__
37 #define SERVICE_RUNTIME_GIO_H__ 1
39 /* is this needed? - maybe for size_t */
40 #include "native_client/include/portability.h"
42 #include <stdarg.h>
43 #include <stdio.h>
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
49 struct Gio; /* fwd */
51 struct GioVtbl {
52 int (*Read)(struct Gio *vself,
53 void *buf,
54 size_t count); /* returns nbytes read */
55 int (*Write)(struct Gio *vself,
56 void *buf,
57 size_t count); /* returns nbytes written */
58 off_t (*Seek)(struct Gio *vself,
59 off_t offset,
60 int whence); /* returns -1 on error */
61 int (*Flush)(struct Gio *vself); /* only used for write */
62 int (*Close)(struct Gio *vself); /* returns -1 on error */
63 void (*Dtor)(struct Gio *vself); /* implicit close */
66 struct Gio {
67 struct GioVtbl const *vtbl;
70 struct GioFile {
71 struct Gio base;
72 FILE *iop;
75 int GioFileCtor(struct GioFile *self,
76 char const *fname,
77 char const *mode);
79 int GioFileRead(struct Gio *vself,
80 void *buf,
81 size_t count);
83 int GioFileWrite(struct Gio *vself,
84 void *buf,
85 size_t count);
87 off_t GioFileSeek(struct Gio *vself,
88 off_t offset,
89 int whence);
91 int GioFileFlush(struct Gio *vself);
93 int GioFileClose(struct Gio *vself);
95 void GioFileDtor(struct Gio *vself);
97 int GioFileRefCtor(struct GioFile *self,
98 FILE *iop);
100 struct GioMemoryFile {
101 struct Gio base;
102 char *buffer;
103 size_t len;
104 size_t curpos;
105 /* invariant: 0 <= curpos && curpos <= len */
106 /* if curpos == len, everything has been read */
109 int GioMemoryFileCtor(struct GioMemoryFile *self,
110 char *buffer,
111 size_t len);
113 int GioMemoryFileRead(struct Gio *vself,
114 void *buf,
115 size_t count);
117 int GioMemoryFileWrite(struct Gio *vself,
118 void *buf,
119 size_t count);
121 off_t GioMemoryFileSeek(struct Gio *vself,
122 off_t offset,
123 int whence);
125 int GioMemoryFileFlush(struct Gio *vself);
127 int GioMemoryFileClose(struct Gio *vself);
129 void GioMemoryFileDtor(struct Gio *vself);
131 struct GioMemoryFileSnapshot {
132 struct GioMemoryFile base;
135 int GioMemoryFileSnapshotCtor(struct GioMemoryFileSnapshot *self,
136 char *fn);
138 void GioMemoryFileSnapshotDtor(struct Gio *vself);
140 #define ggetc(gp) ({ char ch; (*gp->vtbl->Read)(gp, &ch, 1) == 1 ? ch : EOF;})
142 int fggetc(struct Gio *gp);
144 int gprintf(struct Gio *gp,
145 char *fmt,
146 ...);
148 int gvprintf(struct Gio *gp,
149 char *fmt,
150 va_list ap);
152 #ifdef __cplusplus
153 } /* end of extern "C" */
154 #endif
156 #endif