librsync  2.3.3
emit.c
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#include <assert.h>
23#include "librsync.h"
24#include "emit.h"
25#include "job.h"
26#include "netint.h"
27#include "prototab.h"
28#include "trace.h"
29
30void rs_emit_delta_header(rs_job_t *job)
31{
32 rs_trace("emit DELTA magic");
33 rs_squirt_n4(job, RS_DELTA_MAGIC);
34}
35
36void rs_emit_literal_cmd(rs_job_t *job, int len)
37{
38 int cmd;
39 int param_len = len <= 64 ? 0 : rs_int_len(len);
40
41 if (param_len == 0) {
42 cmd = len;
43 rs_trace("emit LITERAL_%d, cmd_byte=%#04x", len, cmd);
44 } else if (param_len == 1) {
45 cmd = RS_OP_LITERAL_N1;
46 rs_trace("emit LITERAL_N1(len=%d), cmd_byte=%#04x", len, cmd);
47 } else if (param_len == 2) {
48 cmd = RS_OP_LITERAL_N2;
49 rs_trace("emit LITERAL_N2(len=%d), cmd_byte=%#04x", len, cmd);
50 } else {
51 assert(param_len == 4);
52 cmd = RS_OP_LITERAL_N4;
53 rs_trace("emit LITERAL_N4(len=%d), cmd_byte=%#04x", len, cmd);
54 }
55
56 rs_squirt_byte(job, (rs_byte_t)cmd);
57 if (param_len)
58 rs_squirt_netint(job, len, param_len);
59
60 job->stats.lit_cmds++;
61 job->stats.lit_bytes += len;
62 job->stats.lit_cmdbytes += 1 + param_len;
63}
64
65void rs_emit_copy_cmd(rs_job_t *job, rs_long_t where, rs_long_t len)
66{
67 int cmd;
68 rs_stats_t *stats = &job->stats;
69 const int where_bytes = rs_int_len(where);
70 const int len_bytes = rs_int_len(len);
71
72 /* Commands ascend (1,1), (1,2), ... (8, 8) */
73 if (where_bytes == 8)
74 cmd = RS_OP_COPY_N8_N1;
75 else if (where_bytes == 4)
76 cmd = RS_OP_COPY_N4_N1;
77 else if (where_bytes == 2)
78 cmd = RS_OP_COPY_N2_N1;
79 else {
80 assert(where_bytes == 1);
81 cmd = RS_OP_COPY_N1_N1;
82 }
83 if (len_bytes == 1) ;
84 else if (len_bytes == 2)
85 cmd += 1;
86 else if (len_bytes == 4)
87 cmd += 2;
88 else {
89 assert(len_bytes == 8);
90 cmd += 3;
91 }
92
93 rs_trace("emit COPY_N%d_N%d(where=" FMT_LONG ", len=" FMT_LONG
94 "), cmd_byte=%#04x", where_bytes, len_bytes, where, len, cmd);
95 rs_squirt_byte(job, (rs_byte_t)cmd);
96 rs_squirt_netint(job, where, where_bytes);
97 rs_squirt_netint(job, len, len_bytes);
98
99 stats->copy_cmds++;
100 stats->copy_bytes += len;
101 stats->copy_cmdbytes += 1 + where_bytes + len_bytes;
102}
103
104void rs_emit_end_cmd(rs_job_t *job)
105{
106 int cmd = RS_OP_END;
107
108 rs_trace("emit END, cmd_byte=%#04x", cmd);
109 rs_squirt_byte(job, (rs_byte_t)cmd);
110}
encoding output routines.
Generic state-machine interface.
Public header for librsync.
@ RS_DELTA_MAGIC
A delta file.
Definition: librsync.h:71
Network-byte-order output to the tube.
Delta file commands.
The contents of this structure are private.
Definition: job.h:47
rs_stats_t stats
Encoding statistics.
Definition: job.h:93
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.