summaryrefslogtreecommitdiff
path: root/emit.c
diff options
context:
space:
mode:
authorMartin Pool <mbp@sourcefrog.net>2000-12-12 01:12:21 +0000
committerMartin Pool <mbp@sourcefrog.net>2000-12-12 01:12:21 +0000
commit6e28210b74d9982344e4cfb6db15ae0fd6950e56 (patch)
treea77af598846345572309e616d2d1a8ad3bfc11a2 /emit.c
parent93a26cc59a104c2680ca1cf152b25a4995abec38 (diff)
downloadlibrsync-6e28210b74d9982344e4cfb6db15ae0fd6950e56.tar.gz
Lots of random hacking on the new mem-mem stream interface. Applying
deltas kinda slightly works with the new API. Give domain-credit to Linuxcare; change to Linux code style cause every emacs understands it.
Diffstat (limited to 'emit.c')
-rw-r--r--emit.c243
1 files changed, 23 insertions, 220 deletions
diff --git a/emit.c b/emit.c
index 28f44a2..dc84996 100644
--- a/emit.c
+++ b/emit.c
@@ -1,9 +1,9 @@
-/*= -*- c-file-style: "bsd" -*-
+/*= -*- c-file-style: "linux" -*-
*
* libhsync -- dynamic caching and delta update in HTTP
* $Id$
*
- * Copyright (C) 2000 by Martin Pool <mbp@samba.org>
+ * Copyright (C) 2000 by Martin Pool <mbp@linuxcare.com.au>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -22,8 +22,10 @@
/*
- * They don't sleep anymore on the beach
- */
+ * [almost sobbing] They don't sleep
+ * anymore on the beach. They don't
+ * sleep on the beach anymore.
+ */
#include "config.h"
@@ -38,7 +40,6 @@
#include <limits.h>
#include <inttypes.h>
#include <stdlib.h>
-#include <stdarg.h>
#include <stdio.h>
#include "hsync.h"
@@ -46,45 +47,44 @@
#include "protocol.h"
#include "trace.h"
#include "emit.h"
+#include "prototab.h"
#include "netint.h"
-#if 0
-static int
+int
_hs_fits_in_n8(size_t val)
{
return val <= UINT8_MAX;
}
-static int
+int
_hs_fits_in_n16(size_t val)
{
return val <= UINT16_MAX;
}
-static int
+int
_hs_fits_in_n32(size_t val)
{
return val <= UINT32_MAX;
}
-static int
+int
_hs_int_len(off_t val)
{
- if (_hs_fits_in_n8(val))
- return 1;
- else if (_hs_fits_in_n16(val))
- return 2;
- else if (_hs_fits_in_n32(val))
- return 4;
- else {
- _hs_fatal("can't handle files this long yet");
- }
+ if (_hs_fits_in_n8(val))
+ return 1;
+ else if (_hs_fits_in_n16(val))
+ return 2;
+ else if (_hs_fits_in_n32(val))
+ return 4;
+ else {
+ _hs_fatal("can't handle integer this long yet");
+ }
}
-#endif
/*
@@ -93,6 +93,7 @@ _hs_int_len(off_t val)
void
_hs_emit_delta_header(hs_stream_t *stream)
{
+ _hs_trace("emit DELTA");
_hs_squirt_n32(stream, HS_DELTA_MAGIC);
}
@@ -102,205 +103,7 @@ _hs_emit_delta_header(hs_stream_t *stream)
void
_hs_emit_literal_cmd(hs_stream_t *stream, int len)
{
- _hs_squirt_n8(stream, op_literal_n32);
+ _hs_trace("emit LITERAL(%d)", len);
+ _hs_squirt_n8(stream, HS_OP_LITERAL_N32);
_hs_squirt_n32(stream, len);
}
-
-
-
-#if 0
-int _hs_emit_eof(hs_write_fn_t write_fn, void *write_priv,
- UNUSED(hs_stats_t *stats))
-{
- _hs_trace("Writing EOF");
- return _hs_write_netbyte(write_fn, write_priv, (uint8_t) op_eof);
-}
-
-
-int
-_hs_emit_checksum_cmd(hs_write_fn_t write_fn, void *write_priv, size_t size)
-{
- int ret;
-
- _hs_trace("Writing CHECKSUM(len=%d)", size);
- ret = _hs_write_netbyte(write_fn, write_priv,
- (uint8_t) op_checksum_short);
- if (ret != 1)
- return -1;
-
- assert(_hs_fits_in_short(size));
- ret = _hs_write_netshort(write_fn, write_priv, (uint16_t) size);
- if (ret != 2)
- return -1;
-
- return 3;
-}
-
-
-
-int
-_hs_emit_filesum(hs_write_fn_t write_fn, void *write_priv,
- byte_t const *buf, size_t size)
-{
- int ret;
-
- ret = _hs_emit_checksum_cmd(write_fn, write_priv, size);
- if (ret <= 0)
- return -1;
-
- ret = _hs_write_loop(write_fn, write_priv, buf, size);
- if (ret != (int) size)
- return -1;
-
- return 3 + size;
-}
-
-
-/* Emit the command header for literal data. */
-int
-_hs_emit_literal_cmd(hs_write_fn_t write_fn, void *write_priv, size_t size)
-{
- int type;
- uint8_t cmd;
-
- _hs_trace("Writing LITERAL(len=%d)", size);
-
- if ((size >= 1) && (size < (op_literal_last - op_literal_1))) {
- cmd = (uint8_t) (op_literal_1 + size - 1);
- return _hs_write_netbyte(write_fn, write_priv, cmd);
- }
-
- type = _hs_int_len(size);
- if (type == 1) {
- cmd = (uint8_t) op_literal_byte;
- } else if (type == 2) {
- cmd = (uint8_t) op_literal_short;
- } else if (type == 4) {
- cmd = (uint8_t) op_literal_int;
- } else {
- _hs_fatal("can't happen!");
- }
-
- if (_hs_write_netbyte(write_fn, write_priv, cmd) < 0)
- return -1;
-
- return _hs_write_netvar(write_fn, write_priv, size, type);
-}
-
-
-int
-_hs_send_literal(hs_write_fn_t write_fn,
- void *write_priv,
- int kind,
- byte_t const *buf,
- size_t amount)
-{
- int ret;
-
- assert(amount > 0);
-
- _hs_trace("flush %d bytes of %s data",
- (int) amount,
- kind == op_kind_literal ? "literal" : "signature");
-
- if (kind == op_kind_literal) {
- if (_hs_emit_literal_cmd(write_fn, write_priv, amount) < 0)
- return -1;
- } else {
- if (_hs_emit_signature_cmd(write_fn, write_priv, amount) < 0)
- return -1;
- }
-
- ret = hs_must_write(write_fn, write_priv, buf, amount);
- assert(ret == (int)amount);
- return amount;
-}
-
-
-/* Emit the command header for signature data. */
-int
-_hs_emit_signature_cmd(hs_write_fn_t write_fn, void *write_priv,
- size_t size)
-{
- int type;
- uint8_t cmd;
-
- _hs_trace("Writing SIGNATURE(len=%d)", size);
-
- if ((size >= 1) && (size < (op_signature_last - op_signature_1))) {
- cmd = (uint8_t) (op_signature_1 + size - 1);
- return _hs_write_netbyte(write_fn, write_priv, cmd);
- }
-
- type = _hs_int_len((long) size);
- if (type == 1) {
- cmd = (uint8_t) op_signature_byte;
- } else if (type == 2) {
- cmd = (uint8_t) op_signature_short;
- } else if (type == 4) {
- cmd = (uint8_t) op_signature_int;
- } else {
- _hs_fatal("can't happen!");
- }
-
- if (_hs_write_netbyte(write_fn, write_priv, cmd) < 0)
- return -1;
-
- return _hs_write_netvar(write_fn, write_priv, size, type);
-}
-
-
-int
-_hs_emit_copy(hs_write_fn_t write_fn, void *write_priv,
- off_t offset, size_t length,
- hs_stats_t * stats)
-{
- int ret;
- int len_type, off_type;
- int cmd;
-
- stats->copy_cmds++;
- stats->copy_bytes += length;
-
- _hs_trace("Writing COPY(off=%ld, len=%ld)", (long) offset, (long) length);
- len_type = _hs_int_len(length);
- off_type = _hs_int_len(offset);
-
- /* We cannot specify the offset as a byte, because that would so
- rarely be useful. */
- if (off_type == 1)
- off_type = 2;
-
- /* Make sure this formula lines up with the values in protocol.h */
-
- if (off_type == 2) {
- cmd = op_copy_short_byte;
- } else if (off_type == 4) {
- cmd = op_copy_int_byte;
- } else {
- _hs_fatal("can't pack offset %ld!", (long) offset);
- }
-
- if (len_type == 1) {
- cmd += 0;
- } else if (len_type == 2) {
- cmd += 1;
- } else if (len_type == 4) {
- cmd += 2;
- } else {
- _hs_fatal("can't pack length %ld as a %d byte number",
- (long) length, len_type);
- }
-
- ret = _hs_write_netbyte(write_fn, write_priv, (uint8_t) cmd);
- if (ret < 0) return -1;
-
- ret = _hs_write_netvar(write_fn, write_priv, offset, off_type);
- if (ret < 0) return -1;
-
- ret = _hs_write_netvar(write_fn, write_priv, length, len_type);
- if (ret < 0) return -1;
-
- return 1;
-}
-#endif