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