Rizin
unix-like reverse engineering framework and cli tools
buffer.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: unknown
2 // SPDX-License-Identifier: CC-PDDC
3 
4 #include "buffer.h"
5 
6 void buffer_init(buffer *s, BufferOp op, int fd, char *buf, ut32 len) {
7  s->x = buf;
8  s->fd = fd;
9  s->op = op;
10  s->p = 0;
11  s->n = len;
12 }
13 
14 static int allwrite(BufferOp op, int fd, const char *buf, ut32 len) {
15  ut32 w;
16  while (len > 0) {
17  w = op(fd, buf, len);
18  if (w != len) {
19  return 0;
20  }
21  buf += w;
22  len -= w;
23  }
24  return 1;
25 }
26 
28  int p = s->p;
29  if (!p) {
30  return 1;
31  }
32  s->p = 0;
33  return allwrite(s->op, s->fd, s->x, p);
34 }
35 
36 int buffer_putalign(buffer *s, const char *buf, ut32 len) {
37  ut32 n;
38  if (!s || !s->x || !buf) {
39  return 0;
40  }
41  while (len > (n = s->n - s->p)) {
42  memcpy(s->x + s->p, buf, n);
43  s->p += n;
44  buf += n;
45  len -= n;
46  if (!buffer_flush(s)) {
47  return 0;
48  }
49  }
50  /* now len <= s->n - s->p */
51  memcpy(s->x + s->p, buf, len);
52  s->p += len;
53  return 1;
54 }
55 
56 int buffer_putflush(buffer *s, const char *buf, ut32 len) {
57  if (!buffer_flush(s)) {
58  return 0;
59  }
60  return allwrite(s->op, s->fd, buf, len);
61 }
size_t len
Definition: 6502dis.c:15
ut8 op
Definition: 6502dis.c:13
int buffer_putalign(buffer *s, const char *buf, ut32 len)
Definition: buffer.c:36
int buffer_putflush(buffer *s, const char *buf, ut32 len)
Definition: buffer.c:56
int buffer_flush(buffer *s)
Definition: buffer.c:27
static int allwrite(BufferOp op, int fd, const char *buf, ut32 len)
Definition: buffer.c:14
void buffer_init(buffer *s, BufferOp op, int fd, char *buf, ut32 len)
Definition: buffer.c:6
int(* BufferOp)(int, const char *, int)
Definition: buffer.h:13
#define w
Definition: crypto_rc6.c:13
uint32_t ut32
voidpf void * buf
Definition: ioapi.h:138
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
Definition: buffer.h:15
Definition: dis.c:32
static const z80_opcode fd[]
Definition: z80_tab.h:997