librsync  2.0.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 encoding output routines.
23  *
24  * \todo Pluggable encoding formats: gdiff-style, rsync 24, ed (text), Delta
25  * HTTP. */
26 
27 #include "config.h"
28 
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 
33 #include "librsync.h"
34 #include "command.h"
35 #include "trace.h"
36 #include "emit.h"
37 #include "prototab.h"
38 #include "netint.h"
39 #include "sumset.h"
40 #include "job.h"
41 
42 /** Write the magic for the start of a delta. */
44 {
45  rs_trace("emit DELTA magic");
46  rs_squirt_n4(job, RS_DELTA_MAGIC);
47 }
48 
49 /** Write a LITERAL command. */
50 void rs_emit_literal_cmd(rs_job_t *job, int len)
51 {
52  int cmd;
53  int param_len = rs_int_len(len);
54 
55  if (param_len == 1)
56  cmd = RS_OP_LITERAL_N1;
57  else if (param_len == 2)
58  cmd = RS_OP_LITERAL_N2;
59  else {
60  assert(param_len == 4);
61  cmd = RS_OP_LITERAL_N4;
62  }
63 
64  rs_trace("emit LITERAL_N%d(len=%d), cmd_byte=%#04x", param_len, len, cmd);
65  rs_squirt_byte(job, cmd);
66  rs_squirt_netint(job, len, param_len);
67 
68  job->stats.lit_cmds++;
69  job->stats.lit_bytes += len;
70  job->stats.lit_cmdbytes += 1 + param_len;
71 }
72 
73 /** Write a COPY command for given offset and length.
74  *
75  * There is a choice of variable-length encodings, depending on the size of
76  * representation for the parameters. */
77 void rs_emit_copy_cmd(rs_job_t *job, rs_long_t where, rs_long_t len)
78 {
79  int cmd;
80  rs_stats_t *stats = &job->stats;
81  const int where_bytes = rs_int_len(where);
82  const int len_bytes = rs_int_len(len);
83 
84  /* Commands ascend (1,1), (1,2), ... (8, 8) */
85  if (where_bytes == 8)
86  cmd = RS_OP_COPY_N8_N1;
87  else if (where_bytes == 4)
88  cmd = RS_OP_COPY_N4_N1;
89  else if (where_bytes == 2)
90  cmd = RS_OP_COPY_N2_N1;
91  else {
92  assert(where_bytes == 1);
93  cmd = RS_OP_COPY_N1_N1;
94  }
95 
96  if (len_bytes == 1) ;
97  else if (len_bytes == 2)
98  cmd += 1;
99  else if (len_bytes == 4)
100  cmd += 2;
101  else {
102  assert(len_bytes == 8);
103  cmd += 3;
104  }
105 
106  rs_trace("emit COPY_N%d_N%d(where=" FMT_LONG ", len=" FMT_LONG
107  "), cmd_byte=%#04x", where_bytes, len_bytes, where, len, cmd);
108  rs_squirt_byte(job, cmd);
109  rs_squirt_netint(job, where, where_bytes);
110  rs_squirt_netint(job, len, len_bytes);
111 
112  stats->copy_cmds++;
113  stats->copy_bytes += len;
114  stats->copy_cmdbytes += 1 + where_bytes + len_bytes;
115 
116  /* \todo All the stats */
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, cmd);
126 }
rs_squirt_byte
rs_result rs_squirt_byte(rs_job_t *job, unsigned char d)
Write a single byte to a stream output.
Definition: netint.c:63
rs_emit_literal_cmd
void rs_emit_literal_cmd(rs_job_t *job, int len)
Write a LITERAL command.
Definition: emit.c:50
trace.h
emit.h
librsync.h
rs_job
Definition: job.h:26
command.h
RS_DELTA_MAGIC
@ RS_DELTA_MAGIC
A delta file.
Definition: librsync.h:74
rs_emit_end_cmd
void rs_emit_end_cmd(rs_job_t *job)
Write an END command.
Definition: emit.c:120
rs_stats::lit_cmds
int lit_cmds
Number of literal commands.
Definition: librsync.h:194
rs_job::stats
rs_stats_t stats
Encoding statistics.
Definition: job.h:72
rs_stats::lit_bytes
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:195
rs_emit_delta_header
void rs_emit_delta_header(rs_job_t *job)
Write the magic for the start of a delta.
Definition: emit.c:43
rs_squirt_netint
rs_result rs_squirt_netint(rs_job_t *job, rs_long_t d, int len)
Write a variable-length integer to a stream.
Definition: netint.c:76
prototab.h
rs_stats
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:191
rs_stats::lit_cmdbytes
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:196
rs_emit_copy_cmd
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:77