librsync  2.3.3
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 <assert.h>
31#include "librsync.h"
32#include "netint.h"
33#include "scoop.h"
34
35#define RS_MAX_INT_BYTES 8
36
37rs_result rs_squirt_byte(rs_job_t *job, rs_byte_t val)
38{
39 rs_tube_write(job, &val, 1);
40 return RS_DONE;
41}
42
43rs_result rs_squirt_netint(rs_job_t *job, rs_long_t val, int len)
44{
45 rs_byte_t buf[RS_MAX_INT_BYTES];
46 int i;
47
48 assert(len <= RS_MAX_INT_BYTES);
49 /* Fill the output buffer with a bigendian representation of the number. */
50 for (i = len - 1; i >= 0; i--) {
51 buf[i] = (rs_byte_t)val; /* truncated */
52 val >>= 8;
53 }
54 rs_tube_write(job, buf, len);
55 return RS_DONE;
56}
57
58rs_result rs_squirt_n4(rs_job_t *job, int val)
59{
60 return rs_squirt_netint(job, val, 4);
61}
62
63rs_result rs_suck_byte(rs_job_t *job, rs_byte_t *val)
64{
65 rs_result result;
66 rs_byte_t *buf;
67
68 if ((result = rs_scoop_read(job, 1, (void **)&buf)) == RS_DONE)
69 *val = *buf;
70 return result;
71}
72
73rs_result rs_suck_netint(rs_job_t *job, rs_long_t *val, int len)
74{
75 rs_result result;
76 rs_byte_t *buf;
77 int i;
78
79 assert(len <= RS_MAX_INT_BYTES);
80 if ((result = rs_scoop_read(job, len, (void **)&buf)) == RS_DONE) {
81 *val = 0;
82 for (i = 0; i < len; i++)
83 *val = (*val << 8) | (rs_long_t)buf[i];
84 }
85 return result;
86}
87
88rs_result rs_suck_n4(rs_job_t *job, int *val)
89{
90 rs_result result;
91 rs_long_t buf;
92
93 if ((result = rs_suck_netint(job, &buf, 4)) == RS_DONE)
94 *val = (int)buf;
95 return result;
96}
97
98int rs_int_len(rs_long_t val)
99{
100 assert(val >= 0);
101 if (!(val & ~(rs_long_t)0xff))
102 return 1;
103 if (!(val & ~(rs_long_t)0xffff))
104 return 2;
105 if (!(val & ~(rs_long_t)0xffffffff))
106 return 4;
107 assert(!(val & ~(rs_long_t)0xffffffffffffffff));
108 return 8;
109}
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_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:191
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:173
The contents of this structure are private.
Definition: job.h:47