$treeview $search $mathjax $extrastylesheet
librsync
2.3.0
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- the library for network deltas 00004 * 00005 * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@sourcefrog.net> 00006 * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org> 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public License 00010 * as published by the Free Software Foundation; either version 2.1 of 00011 * the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00021 */ 00022 00023 /** \file readsums.c 00024 * Load signatures from a file. */ 00025 00026 #include "config.h" 00027 #include <assert.h> 00028 #include <stdlib.h> 00029 #include "librsync.h" 00030 #include "job.h" 00031 #include "sumset.h" 00032 #include "stream.h" 00033 #include "netint.h" 00034 #include "trace.h" 00035 #include "util.h" 00036 00037 static rs_result rs_loadsig_s_weak(rs_job_t *job); 00038 static rs_result rs_loadsig_s_strong(rs_job_t *job); 00039 00040 /** Add a just-read-in checksum pair to the signature block. */ 00041 static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong) 00042 { 00043 rs_signature_t *sig = job->signature; 00044 00045 if (rs_trace_enabled()) { 00046 char hexbuf[RS_MAX_STRONG_SUM_LENGTH * 2 + 2]; 00047 rs_hexify(hexbuf, strong, sig->strong_sum_len); 00048 rs_trace("got block: weak=" FMT_WEAKSUM ", strong=%s", job->weak_sig, 00049 hexbuf); 00050 } 00051 rs_signature_add_block(job->signature, job->weak_sig, strong); 00052 job->stats.sig_blocks++; 00053 return RS_RUNNING; 00054 } 00055 00056 static rs_result rs_loadsig_s_weak(rs_job_t *job) 00057 { 00058 int l; 00059 rs_result result; 00060 00061 if ((result = rs_suck_n4(job, &l)) != RS_DONE) { 00062 if (result == RS_INPUT_ENDED) /* ending here is OK */ 00063 return RS_DONE; 00064 return result; 00065 } 00066 job->weak_sig = l; 00067 job->statefn = rs_loadsig_s_strong; 00068 return RS_RUNNING; 00069 } 00070 00071 static rs_result rs_loadsig_s_strong(rs_job_t *job) 00072 { 00073 rs_result result; 00074 rs_strong_sum_t *strongsum; 00075 00076 if ((result = 00077 rs_scoop_read(job, job->signature->strong_sum_len, 00078 (void **)&strongsum)) != RS_DONE) 00079 return result; 00080 job->statefn = rs_loadsig_s_weak; 00081 return rs_loadsig_add_sum(job, strongsum); 00082 } 00083 00084 static rs_result rs_loadsig_s_stronglen(rs_job_t *job) 00085 { 00086 int l; 00087 rs_result result; 00088 00089 if ((result = rs_suck_n4(job, &l)) != RS_DONE) 00090 return result; 00091 if (l < 0 || l > RS_MAX_STRONG_SUM_LENGTH) { 00092 rs_error("strong sum length %d is implausible", l); 00093 return RS_CORRUPT; 00094 } 00095 rs_trace("got strong sum length %d", l); 00096 job->sig_strong_len = l; 00097 /* Initialize the signature. */ 00098 if ((result = 00099 rs_signature_init(job->signature, job->sig_magic, job->sig_block_len, 00100 job->sig_strong_len, job->sig_fsize)) != RS_DONE) 00101 return result; 00102 job->statefn = rs_loadsig_s_weak; 00103 return RS_RUNNING; 00104 } 00105 00106 static rs_result rs_loadsig_s_blocklen(rs_job_t *job) 00107 { 00108 int l; 00109 rs_result result; 00110 00111 if ((result = rs_suck_n4(job, &l)) != RS_DONE) 00112 return result; 00113 if (l < 1) { 00114 rs_error("block length of %d is bogus", l); 00115 return RS_CORRUPT; 00116 } 00117 rs_trace("got block length %d", l); 00118 job->sig_block_len = l; 00119 job->stats.block_len = l; 00120 job->statefn = rs_loadsig_s_stronglen; 00121 return RS_RUNNING; 00122 } 00123 00124 static rs_result rs_loadsig_s_magic(rs_job_t *job) 00125 { 00126 int l; 00127 rs_result result; 00128 00129 if ((result = rs_suck_n4(job, &l)) != RS_DONE) 00130 return result; 00131 rs_trace("got signature magic %#x", l); 00132 job->sig_magic = l; 00133 job->statefn = rs_loadsig_s_blocklen; 00134 return RS_RUNNING; 00135 } 00136 00137 rs_job_t *rs_loadsig_begin(rs_signature_t **signature) 00138 { 00139 rs_job_t *job; 00140 00141 job = rs_job_new("loadsig", rs_loadsig_s_magic); 00142 *signature = job->signature = rs_alloc_struct(rs_signature_t); 00143 return job; 00144 }