librsync  2.3.4
netint.c
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- library for network deltas
4 *
5 * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6 * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*=
24 | Ummm, well, OK. The network's the
25 | network, the computer's the
26 | computer. Sorry for the confusion.
27 | -- Sun Microsystems
28 */
29
30#include "config.h" /* IWYU pragma: keep */
31#include <assert.h>
32#include "librsync.h"
33#include "netint.h"
34#include "scoop.h"
35
36#define RS_MAX_INT_BYTES 8
37
38rs_result rs_squirt_byte(rs_job_t *job, rs_byte_t val)
39{
40 rs_tube_write(job, &val, 1);
41 return RS_DONE;
42}
43
44rs_result rs_squirt_netint(rs_job_t *job, rs_long_t val, int len)
45{
46 rs_byte_t buf[RS_MAX_INT_BYTES];
47 int i;
48
49 assert(len <= RS_MAX_INT_BYTES);
50 /* Fill the output buffer with a bigendian representation of the number. */
51 for (i = len - 1; i >= 0; i--) {
52 buf[i] = (rs_byte_t)val; /* truncated */
53 val >>= 8;
54 }
55 rs_tube_write(job, buf, len);
56 return RS_DONE;
57}
58
59rs_result rs_squirt_n4(rs_job_t *job, int val)
60{
61 return rs_squirt_netint(job, val, 4);
62}
63
64rs_result rs_suck_byte(rs_job_t *job, rs_byte_t *val)
65{
66 rs_result result;
67 rs_byte_t *buf;
68
69 if ((result = rs_scoop_read(job, 1, (void **)&buf)) == RS_DONE)
70 *val = *buf;
71 return result;
72}
73
74rs_result rs_suck_netint(rs_job_t *job, rs_long_t *val, int len)
75{
76 rs_result result;
77 rs_byte_t *buf;
78 int i;
79
80 assert(len <= RS_MAX_INT_BYTES);
81 if ((result = rs_scoop_read(job, len, (void **)&buf)) == RS_DONE) {
82 *val = 0;
83 for (i = 0; i < len; i++)
84 *val = (*val << 8) | (rs_long_t)buf[i];
85 }
86 return result;
87}
88
89rs_result rs_suck_n4(rs_job_t *job, int *val)
90{
91 rs_result result;
92 rs_long_t buf;
93
94 if ((result = rs_suck_netint(job, &buf, 4)) == RS_DONE)
95 *val = (int)buf;
96 return result;
97}
98
99int rs_int_len(rs_long_t val)
100{
101 assert(val >= 0);
102 if (!(val & ~(rs_long_t)0xff))
103 return 1;
104 if (!(val & ~(rs_long_t)0xffff))
105 return 2;
106 if (!(val & ~(rs_long_t)0xffffffff))
107 return 4;
108 assert(!(val & ~(rs_long_t)0xffffffffffffffff));
109 return 8;
110}
Public header for librsync.
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
Network-byte-order output to the tube.
rs_result rs_squirt_byte(rs_job_t *job, rs_byte_t val)
Write a single byte to a stream output.
Definition: netint.c:38
rs_result rs_squirt_netint(rs_job_t *job, rs_long_t val, int len)
Write a variable-length integer to a stream.
Definition: netint.c:44
rs_result rs_scoop_read(rs_job_t *job, size_t len, void **ptr)
Read LEN bytes if possible, and remove them from the input scoop.
Definition: scoop.c:192
Manage librsync streams of IO.
void rs_tube_write(rs_job_t *job, void const *buf, size_t len)
Push some data into the tube for storage.
Definition: tube.c:174
The contents of this structure are private.
Definition: job.h:47