librsync  2.3.2
emit.c
Go to the documentation of this file.
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- dynamic caching and delta update in HTTP
4 *
5 * Copyright (C) 2000, 2001, 2004 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/** \file emit.c
23 * encoding output routines.
24 *
25 * \todo Pluggable encoding formats: gdiff-style, rsync 24, ed (text), Delta
26 * HTTP. */
27
28#include "config.h"
29#include <assert.h>
30#include <stdlib.h>
31#include "librsync.h"
32#include "emit.h"
33#include "job.h"
34#include "netint.h"
35#include "command.h"
36#include "prototab.h"
37#include "trace.h"
38
39/** Write the magic for the start of a delta. */
41{
42 rs_trace("emit DELTA magic");
43 rs_squirt_n4(job, RS_DELTA_MAGIC);
44}
45
46/** Write a LITERAL command. */
47void rs_emit_literal_cmd(rs_job_t *job, int len)
48{
49 int cmd;
50 int param_len = len <= 64 ? 0 : rs_int_len(len);
51
52 if (param_len == 0) {
53 cmd = len;
54 rs_trace("emit LITERAL_%d, cmd_byte=%#04x", len, cmd);
55 } else if (param_len == 1) {
56 cmd = RS_OP_LITERAL_N1;
57 rs_trace("emit LITERAL_N1(len=%d), cmd_byte=%#04x", len, cmd);
58 } else if (param_len == 2) {
59 cmd = RS_OP_LITERAL_N2;
60 rs_trace("emit LITERAL_N2(len=%d), cmd_byte=%#04x", len, cmd);
61 } else {
62 assert(param_len == 4);
63 cmd = RS_OP_LITERAL_N4;
64 rs_trace("emit LITERAL_N4(len=%d), cmd_byte=%#04x", len, cmd);
65 }
66
67 rs_squirt_byte(job, (rs_byte_t)cmd);
68 if (param_len)
69 rs_squirt_netint(job, len, param_len);
70
71 job->stats.lit_cmds++;
72 job->stats.lit_bytes += len;
73 job->stats.lit_cmdbytes += 1 + param_len;
74}
75
76/** Write a COPY command for given offset and length.
77 *
78 * There is a choice of variable-length encodings, depending on the size of
79 * representation for the parameters. */
80void rs_emit_copy_cmd(rs_job_t *job, rs_long_t where, rs_long_t len)
81{
82 int cmd;
83 rs_stats_t *stats = &job->stats;
84 const int where_bytes = rs_int_len(where);
85 const int len_bytes = rs_int_len(len);
86
87 /* Commands ascend (1,1), (1,2), ... (8, 8) */
88 if (where_bytes == 8)
89 cmd = RS_OP_COPY_N8_N1;
90 else if (where_bytes == 4)
91 cmd = RS_OP_COPY_N4_N1;
92 else if (where_bytes == 2)
93 cmd = RS_OP_COPY_N2_N1;
94 else {
95 assert(where_bytes == 1);
96 cmd = RS_OP_COPY_N1_N1;
97 }
98 if (len_bytes == 1) ;
99 else if (len_bytes == 2)
100 cmd += 1;
101 else if (len_bytes == 4)
102 cmd += 2;
103 else {
104 assert(len_bytes == 8);
105 cmd += 3;
106 }
107
108 rs_trace("emit COPY_N%d_N%d(where=" FMT_LONG ", len=" FMT_LONG
109 "), cmd_byte=%#04x", where_bytes, len_bytes, where, len, cmd);
110 rs_squirt_byte(job, (rs_byte_t)cmd);
111 rs_squirt_netint(job, where, where_bytes);
112 rs_squirt_netint(job, len, len_bytes);
113
114 stats->copy_cmds++;
115 stats->copy_bytes += len;
116 stats->copy_cmdbytes += 1 + where_bytes + len_bytes;
117}
118
119/** Write an END command. */
121{
122 int cmd = RS_OP_END;
123
124 rs_trace("emit END, cmd_byte=%#04x", cmd);
125 rs_squirt_byte(job, (rs_byte_t)cmd);
126}
Types of commands present in the encoding stream.
void rs_emit_end_cmd(rs_job_t *job)
Write an END command.
Definition: emit.c:120
void rs_emit_literal_cmd(rs_job_t *job, int len)
Write a LITERAL command.
Definition: emit.c:47
void rs_emit_copy_cmd(rs_job_t *job, rs_long_t where, rs_long_t len)
Write a COPY command for given offset and length.
Definition: emit.c:80
void rs_emit_delta_header(rs_job_t *job)
Write the magic for the start of a delta.
Definition: emit.c:40
How to emit commands to the client.
Public header for librsync.
@ RS_DELTA_MAGIC
A delta file.
Definition: librsync.h:71
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
Delta file commands.
The contents of this structure are private.
Definition: job.h:26
rs_stats_t stats
Encoding statistics.
Definition: job.h:72
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:210
int lit_cmds
Number of literal commands.
Definition: librsync.h:213
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:215
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:214
logging functions.