$treeview $search $mathjax $extrastylesheet
librsync
2.3.1
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- 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, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU 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 /*= 00024 | Ummm, well, OK. The network's the 00025 | network, the computer's the 00026 | computer. Sorry for the confusion. 00027 | -- Sun Microsystems 00028 */ 00029 00030 /** \file netint.c 00031 * Network-byte-order output to the tube. 00032 * 00033 * All the `suck' routines return a result code. The most common values are 00034 * RS_DONE if they have enough data, or RS_BLOCKED if there is not enough input 00035 * to proceed. 00036 * 00037 * The `squirt` routines also return a result code which in theory could be 00038 * RS_BLOCKED if there is not enough output space to proceed, but in practice 00039 * is always RS_DONE. */ 00040 00041 #include "config.h" 00042 #include <assert.h> 00043 #include <stdlib.h> 00044 #include "librsync.h" 00045 #include "netint.h" 00046 #include "stream.h" 00047 00048 #define RS_MAX_INT_BYTES 8 00049 00050 /** Write a single byte to a stream output. */ 00051 rs_result rs_squirt_byte(rs_job_t *job, rs_byte_t val) 00052 { 00053 rs_tube_write(job, &val, 1); 00054 return RS_DONE; 00055 } 00056 00057 /** Write a variable-length integer to a stream. 00058 * 00059 * \param job - Job of data. 00060 * 00061 * \param val - Value to write out. 00062 * 00063 * \param len - Length of integer, in bytes. */ 00064 rs_result rs_squirt_netint(rs_job_t *job, rs_long_t val, int len) 00065 { 00066 rs_byte_t buf[RS_MAX_INT_BYTES]; 00067 int i; 00068 00069 assert(len <= RS_MAX_INT_BYTES); 00070 /* Fill the output buffer with a bigendian representation of the number. */ 00071 for (i = len - 1; i >= 0; i--) { 00072 buf[i] = val; /* truncated */ 00073 val >>= 8; 00074 } 00075 rs_tube_write(job, buf, len); 00076 return RS_DONE; 00077 } 00078 00079 rs_result rs_squirt_n4(rs_job_t *job, int val) 00080 { 00081 return rs_squirt_netint(job, val, 4); 00082 } 00083 00084 rs_result rs_suck_byte(rs_job_t *job, rs_byte_t *val) 00085 { 00086 rs_result result; 00087 rs_byte_t *buf; 00088 00089 if ((result = rs_scoop_read(job, 1, (void **)&buf)) == RS_DONE) 00090 *val = *buf; 00091 return result; 00092 } 00093 00094 rs_result rs_suck_netint(rs_job_t *job, rs_long_t *val, int len) 00095 { 00096 rs_result result; 00097 rs_byte_t *buf; 00098 int i; 00099 00100 assert(len <= RS_MAX_INT_BYTES); 00101 if ((result = rs_scoop_read(job, len, (void **)&buf)) == RS_DONE) { 00102 *val = 0; 00103 for (i = 0; i < len; i++) 00104 *val = *val << 8 | buf[i]; 00105 } 00106 return result; 00107 } 00108 00109 rs_result rs_suck_n4(rs_job_t *job, int *val) 00110 { 00111 rs_result result; 00112 rs_long_t buf; 00113 00114 if ((result = rs_suck_netint(job, &buf, 4)) == RS_DONE) 00115 *val = buf; 00116 return result; 00117 } 00118 00119 int rs_int_len(rs_long_t val) 00120 { 00121 assert(val >= 0); 00122 if (!(val & ~(rs_long_t)0xff)) 00123 return 1; 00124 if (!(val & ~(rs_long_t)0xffff)) 00125 return 2; 00126 if (!(val & ~(rs_long_t)0xffffffff)) 00127 return 4; 00128 assert(!(val & ~(rs_long_t)0xffffffffffffffff)); 00129 return 8; 00130 }