librsync  2.3.2
netint.c
Go to the documentation of this file.
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/** \file netint.c
31 * Network-byte-order output to the tube.
32 *
33 * All the `suck' routines return a result code. The most common values are
34 * RS_DONE if they have enough data, or RS_BLOCKED if there is not enough input
35 * to proceed.
36 *
37 * The `squirt` routines also return a result code which in theory could be
38 * RS_BLOCKED if there is not enough output space to proceed, but in practice
39 * is always RS_DONE. */
40
41#include "config.h"
42#include <assert.h>
43#include <stdlib.h>
44#include "librsync.h"
45#include "netint.h"
46#include "stream.h"
47
48#define RS_MAX_INT_BYTES 8
49
50/** Write a single byte to a stream output. */
51rs_result rs_squirt_byte(rs_job_t *job, rs_byte_t val)
52{
53 rs_tube_write(job, &val, 1);
54 return RS_DONE;
55}
56
57/** Write a variable-length integer to a stream.
58 *
59 * \param job - Job of data.
60 *
61 * \param val - Value to write out.
62 *
63 * \param len - Length of integer, in bytes. */
64rs_result rs_squirt_netint(rs_job_t *job, rs_long_t val, int len)
65{
66 rs_byte_t buf[RS_MAX_INT_BYTES];
67 int i;
68
69 assert(len <= RS_MAX_INT_BYTES);
70 /* Fill the output buffer with a bigendian representation of the number. */
71 for (i = len - 1; i >= 0; i--) {
72 buf[i] = (rs_byte_t)val; /* truncated */
73 val >>= 8;
74 }
75 rs_tube_write(job, buf, len);
76 return RS_DONE;
77}
78
79rs_result rs_squirt_n4(rs_job_t *job, int val)
80{
81 return rs_squirt_netint(job, val, 4);
82}
83
84rs_result rs_suck_byte(rs_job_t *job, rs_byte_t *val)
85{
86 rs_result result;
87 rs_byte_t *buf;
88
89 if ((result = rs_scoop_read(job, 1, (void **)&buf)) == RS_DONE)
90 *val = *buf;
91 return result;
92}
93
94rs_result rs_suck_netint(rs_job_t *job, rs_long_t *val, int len)
95{
96 rs_result result;
97 rs_byte_t *buf;
98 int i;
99
100 assert(len <= RS_MAX_INT_BYTES);
101 if ((result = rs_scoop_read(job, len, (void **)&buf)) == RS_DONE) {
102 *val = 0;
103 for (i = 0; i < len; i++)
104 *val = (*val << 8) | (rs_long_t)buf[i];
105 }
106 return result;
107}
108
109rs_result rs_suck_n4(rs_job_t *job, int *val)
110{
111 rs_result result;
112 rs_long_t buf;
113
114 if ((result = rs_suck_netint(job, &buf, 4)) == RS_DONE)
115 *val = (int)buf;
116 return result;
117}
118
119int rs_int_len(rs_long_t val)
120{
121 assert(val >= 0);
122 if (!(val & ~(rs_long_t)0xff))
123 return 1;
124 if (!(val & ~(rs_long_t)0xffff))
125 return 2;
126 if (!(val & ~(rs_long_t)0xffffffff))
127 return 4;
128 assert(!(val & ~(rs_long_t)0xffffffffffffffff));
129 return 8;
130}
Public header for librsync.
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
rs_result rs_squirt_byte(rs_job_t *job, rs_byte_t val)
Write a single byte to a stream output.
Definition: netint.c:51
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:64
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:214
The contents of this structure are private.
Definition: job.h:26