]> git.saurik.com Git - redis.git/blame - src/rio.c
SDIFF fuzz test added.
[redis.git] / src / rio.c
CommitLineData
5a181d43 1/* rio.c is a simple stream-oriented I/O abstraction that provides an interface
2 * to write code that can consume/produce data using different concrete input
4365e5b2 3 * and output devices. For instance the same rdb.c code using the rio
4 * abstraction can be used to read and write the RDB format using in-memory
5 * buffers or files.
5a181d43 6 *
7 * A rio object provides the following methods:
8 * read: read from stream.
9 * write: write to stream.
10 * tell: get the current offset.
11 *
12 * It is also possible to set a 'checksum' method that is used by rio.c in order
13 * to compute a checksum of the data written or read, or to query the rio object
4365e5b2 14 * for the current checksum.
15 *
16 * ----------------------------------------------------------------------------
17 *
18 * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
19 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions are met:
24 *
25 * * Redistributions of source code must retain the above copyright notice,
26 * this list of conditions and the following disclaimer.
27 * * Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * * Neither the name of Redis nor the names of its contributors may be used
31 * to endorse or promote products derived from this software without
32 * specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 * POSSIBILITY OF SUCH DAMAGE.
45 */
46
5a181d43 47
f013f400 48#include "fmacros.h"
2e4b0e77 49#include <string.h>
f013f400 50#include <stdio.h>
2e4b0e77
PN
51#include "rio.h"
52#include "util.h"
2f62c966 53#include "crc64.h"
8491f1d9 54
2e4b0e77
PN
55/* Returns 1 or 0 for success/failure. */
56static size_t rioBufferWrite(rio *r, const void *buf, size_t len) {
57 r->io.buffer.ptr = sdscatlen(r->io.buffer.ptr,(char*)buf,len);
58 r->io.buffer.pos += len;
69cecb51 59 return 1;
2e4b0e77
PN
60}
61
62/* Returns 1 or 0 for success/failure. */
63static size_t rioBufferRead(rio *r, void *buf, size_t len) {
64 if (sdslen(r->io.buffer.ptr)-r->io.buffer.pos < len)
69cecb51 65 return 0; /* not enough buffer to return len bytes. */
2e4b0e77
PN
66 memcpy(buf,r->io.buffer.ptr+r->io.buffer.pos,len);
67 r->io.buffer.pos += len;
68 return 1;
69}
70
71/* Returns read/write position in buffer. */
72static off_t rioBufferTell(rio *r) {
73 return r->io.buffer.pos;
74}
75
76/* Returns 1 or 0 for success/failure. */
77static size_t rioFileWrite(rio *r, const void *buf, size_t len) {
78 return fwrite(buf,len,1,r->io.file.fp);
79}
80
81/* Returns 1 or 0 for success/failure. */
82static size_t rioFileRead(rio *r, void *buf, size_t len) {
83 return fread(buf,len,1,r->io.file.fp);
84}
85
86/* Returns read/write position in file. */
87static off_t rioFileTell(rio *r) {
88 return ftello(r->io.file.fp);
89}
90
91static const rio rioBufferIO = {
92 rioBufferRead,
93 rioBufferWrite,
94 rioBufferTell,
736b7c3f 95 NULL, /* update_checksum */
96 0, /* current checksum */
2e4b0e77
PN
97 { { NULL, 0 } } /* union for io-specific vars */
98};
99
100static const rio rioFileIO = {
101 rioFileRead,
102 rioFileWrite,
103 rioFileTell,
736b7c3f 104 NULL, /* update_checksum */
105 0, /* current checksum */
2e4b0e77
PN
106 { { NULL, 0 } } /* union for io-specific vars */
107};
108
f96a8a80 109void rioInitWithFile(rio *r, FILE *fp) {
110 *r = rioFileIO;
111 r->io.file.fp = fp;
2e4b0e77 112}
f96a8a80 113
114void rioInitWithBuffer(rio *r, sds s) {
115 *r = rioBufferIO;
116 r->io.buffer.ptr = s;
117 r->io.buffer.pos = 0;
2e4b0e77
PN
118}
119
736b7c3f 120/* This function can be installed both in memory and file streams when checksum
121 * computation is needed. */
122void rioGenericUpdateChecksum(rio *r, const void *buf, size_t len) {
8491f1d9 123 r->cksum = crc64(r->cksum,buf,len);
736b7c3f 124}
125
5a181d43 126/* ------------------------------ Higher level interface ---------------------------
127 * The following higher level functions use lower level rio.c functions to help
128 * generating the Redis protocol for the Append Only File. */
129
2e4b0e77
PN
130/* Write multi bulk count in the format: "*<count>\r\n". */
131size_t rioWriteBulkCount(rio *r, char prefix, int count) {
132 char cbuf[128];
133 int clen;
134
135 cbuf[0] = prefix;
136 clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,count);
137 cbuf[clen++] = '\r';
138 cbuf[clen++] = '\n';
139 if (rioWrite(r,cbuf,clen) == 0) return 0;
140 return clen;
141}
142
143/* Write binary-safe string in the format: "$<count>\r\n<payload>\r\n". */
144size_t rioWriteBulkString(rio *r, const char *buf, size_t len) {
145 size_t nwritten;
146
147 if ((nwritten = rioWriteBulkCount(r,'$',len)) == 0) return 0;
148 if (len > 0 && rioWrite(r,buf,len) == 0) return 0;
149 if (rioWrite(r,"\r\n",2) == 0) return 0;
150 return nwritten+len+2;
151}
152
153/* Write a long long value in format: "$<count>\r\n<payload>\r\n". */
154size_t rioWriteBulkLongLong(rio *r, long long l) {
155 char lbuf[32];
156 unsigned int llen;
157
158 llen = ll2string(lbuf,sizeof(lbuf),l);
159 return rioWriteBulkString(r,lbuf,llen);
160}
161
162/* Write a double value in the format: "$<count>\r\n<payload>\r\n" */
163size_t rioWriteBulkDouble(rio *r, double d) {
164 char dbuf[128];
165 unsigned int dlen;
166
167 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
168 return rioWriteBulkString(r,dbuf,dlen);
169}