$treeview $search $mathjax $extrastylesheet
librsync
2.0.2
$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 Load signatures from a file. */ 00024 00025 #include "config.h" 00026 00027 #include <assert.h> 00028 #include <stdlib.h> 00029 #include <stdio.h> 00030 #include <string.h> 00031 00032 #include "librsync.h" 00033 #include "sumset.h" 00034 #include "job.h" 00035 #include "trace.h" 00036 #include "netint.h" 00037 #include "util.h" 00038 #include "stream.h" 00039 00040 static rs_result rs_loadsig_s_weak(rs_job_t *job); 00041 static rs_result rs_loadsig_s_strong(rs_job_t *job); 00042 00043 /** Add a just-read-in checksum pair to the signature block. */ 00044 static rs_result rs_loadsig_add_sum(rs_job_t *job, rs_strong_sum_t *strong) 00045 { 00046 rs_signature_t *sig = job->signature; 00047 00048 if (rs_trace_enabled()) { 00049 char hexbuf[RS_MAX_STRONG_SUM_LENGTH * 2 + 2]; 00050 rs_hexify(hexbuf, strong, sig->strong_sum_len); 00051 rs_trace("got block: weak=" FMT_WEAKSUM ", strong=%s", job->weak_sig, 00052 hexbuf); 00053 } 00054 rs_signature_add_block(job->signature, job->weak_sig, strong); 00055 job->stats.sig_blocks++; 00056 return RS_RUNNING; 00057 } 00058 00059 static rs_result rs_loadsig_s_weak(rs_job_t *job) 00060 { 00061 int l; 00062 rs_result result; 00063 00064 if ((result = rs_suck_n4(job, &l)) != RS_DONE) { 00065 if (result == RS_INPUT_ENDED) /* ending here is OK */ 00066 return RS_DONE; 00067 return result; 00068 } 00069 job->weak_sig = l; 00070 job->statefn = rs_loadsig_s_strong; 00071 return RS_RUNNING; 00072 } 00073 00074 static rs_result rs_loadsig_s_strong(rs_job_t *job) 00075 { 00076 rs_result result; 00077 rs_strong_sum_t *strongsum; 00078 00079 if ((result = 00080 rs_scoop_read(job, job->signature->strong_sum_len, 00081 (void **)&strongsum)) != RS_DONE) 00082 return result; 00083 job->statefn = rs_loadsig_s_weak; 00084 return rs_loadsig_add_sum(job, strongsum); 00085 } 00086 00087 static rs_result rs_loadsig_s_stronglen(rs_job_t *job) 00088 { 00089 int l; 00090 rs_result result; 00091 00092 if ((result = rs_suck_n4(job, &l)) != RS_DONE) 00093 return result; 00094 if (l < 0 || l > RS_MAX_STRONG_SUM_LENGTH) { 00095 rs_error("strong sum length %d is implausible", l); 00096 return RS_CORRUPT; 00097 } 00098 rs_trace("got strong sum length %d", l); 00099 job->sig_strong_len = l; 00100 /* Initialize the signature. */ 00101 if ((result = 00102 rs_signature_init(job->signature, job->sig_magic, job->sig_block_len, 00103 job->sig_strong_len, job->sig_fsize)) != RS_DONE) 00104 return result; 00105 job->statefn = rs_loadsig_s_weak; 00106 return RS_RUNNING; 00107 } 00108 00109 static rs_result rs_loadsig_s_blocklen(rs_job_t *job) 00110 { 00111 int l; 00112 rs_result result; 00113 00114 if ((result = rs_suck_n4(job, &l)) != RS_DONE) 00115 return result; 00116 if (l < 1) { 00117 rs_error("block length of %d is bogus", l); 00118 return RS_CORRUPT; 00119 } 00120 rs_trace("got block length %d", l); 00121 job->sig_block_len = l; 00122 job->stats.block_len = l; 00123 job->statefn = rs_loadsig_s_stronglen; 00124 return RS_RUNNING; 00125 } 00126 00127 static rs_result rs_loadsig_s_magic(rs_job_t *job) 00128 { 00129 int l; 00130 rs_result result; 00131 00132 if ((result = rs_suck_n4(job, &l)) != RS_DONE) 00133 return result; 00134 rs_trace("got signature magic %#x", l); 00135 job->sig_magic = l; 00136 job->statefn = rs_loadsig_s_blocklen; 00137 return RS_RUNNING; 00138 } 00139 00140 rs_job_t *rs_loadsig_begin(rs_signature_t **signature) 00141 { 00142 rs_job_t *job; 00143 00144 job = rs_job_new("loadsig", rs_loadsig_s_magic); 00145 *signature = job->signature = rs_alloc_struct(rs_signature_t); 00146 return job; 00147 }