⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fpgenerator.html

📁 用JAVA编写的,在做实验的时候留下来的,本来想删的,但是传上来,大家分享吧
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" /><title>FPGenerator xref</title><link type="text/css" rel="stylesheet" href="../../../stylesheet.css" /></head><body><div id="overview"><a href="../../../../apidocs/st/ata/util/FPGenerator.html">View Javadoc</a></div><pre><a name="1" href="#1">1</a>   <a name="2" href="#2">2</a>   <strong>package</strong> <a href="../../../st/ata/util/package-summary.html">st.ata.util</a>;<a name="3" href="#3">3</a>   <a name="4" href="#4">4</a>   <strong>import</strong> java.util.Hashtable;<a name="5" href="#5">5</a>   <a name="6" href="#6">6</a>   <em>/**<em>*</em></em><a name="7" href="#7">7</a>   <a name="8" href="#8">8</a>   <em>&lt;p> This class provides methods that construct fingerprints of strings</em><a name="9" href="#9">9</a>   <em>of bytes via operations in &lt;i>GF[2^d]&lt;/i> for &lt;i>0 &lt; d &lt;= 64&lt;/i>.</em><a name="10" href="#10">10</a>  <em>&lt;i>GF[2^d]&lt;/i> is represented as the set of polynomials of degree</em><a name="11" href="#11">11</a>  <em>&lt;i>d&lt;/i> with coefficients in &lt;i>Z(2)&lt;/i>, modulo an irreducible</em><a name="12" href="#12">12</a>  <em>polynomial &lt;i>P&lt;/i> of degree &lt;i>d&lt;/i>.  The representation of</em><a name="13" href="#13">13</a>  <em>polynomials is as an unsigned binary number in which the least</em><a name="14" href="#14">14</a>  <em>significant exponent is kept in the most significant bit.</em><a name="15" href="#15">15</a>  <a name="16" href="#16">16</a>  <em>&lt;p> Let S be a string of bytes and &lt;i>g(S)&lt;/i> the string obtained by</em><a name="17" href="#17">17</a>  <em>taking the byte &lt;code>0x01&lt;/code> followed by eight &lt;code>0x00&lt;/code></em><a name="18" href="#18">18</a>  <em>bytes followed by &lt;code>S&lt;/code>.  Let &lt;i>f(S)&lt;/i> be the polynomial</em><a name="19" href="#19">19</a>  <em>associated to the string &lt;i>S&lt;/i> viewed as a polynomial with</em><a name="20" href="#20">20</a>  <em>coefficients in the field &lt;i>Z(2)&lt;/i>.  The fingerprint of S is simply</em><a name="21" href="#21">21</a>  <em>the value &lt;i>f(g(S))&lt;/i> modulo &lt;i>P&lt;/i>.  Because polynomials are</em><a name="22" href="#22">22</a>  <em>represented with the least significant coefficient in the most</em><a name="23" href="#23">23</a>  <em>significant bit, fingerprints of degree &lt;i>d&lt;/i> are stored in the</em><a name="24" href="#24">24</a>  <em>&lt;code>d&lt;/code> &lt;strong>most&lt;/code> significant bits of a long word.</em><a name="25" href="#25">25</a>  <a name="26" href="#26">26</a>  <em>&lt;p> Fingerprints can be used as a probably unique id for the input</em><a name="27" href="#27">27</a>  <em>string.  More precisely, if &lt;i>P&lt;/i> is chosen at random among</em><a name="28" href="#28">28</a>  <em>irreducible polynomials of degree &lt;i>d&lt;/i>, then the probability that</em><a name="29" href="#29">29</a>  <em>any two strings &lt;i>A&lt;/i> and &lt;i>B&lt;/i> have the same fingerprint is</em><a name="30" href="#30">30</a>  <em>less than &lt;i>max(|A|,|B|)/2^(d+1)&lt;/i> where &lt;i>|A|&lt;/i> is the length</em><a name="31" href="#31">31</a>  <em>of A in bits.</em><a name="32" href="#32">32</a>  <a name="33" href="#33">33</a>  <em>&lt;p> The routines named &lt;code>extend[8]&lt;/code> and &lt;code>fp[8]&lt;/code></em><a name="34" href="#34">34</a>  <em>return reduced results, while &lt;code>extend_[byte/char/int/long]&lt;/code></em><a name="35" href="#35">35</a>  <em>do not.  An &lt;em>un&lt;/em>reduced result is a number that is equal (mod</em><a name="36" href="#36">36</a>  <em>&lt;/code>polynomial&lt;/code> to the desired fingerprint but may have</em><a name="37" href="#37">37</a>  <em>degree &lt;code>degree&lt;/code> or higher.  The method &lt;code>reduce&lt;/code></em><a name="38" href="#38">38</a>  <em>reduces such a result to a polynomial of degree less than</em><a name="39" href="#39">39</a>  <em>&lt;code>degree&lt;/code>.  Obtaining reduced results takes longer than</em><a name="40" href="#40">40</a>  <em>obtaining unreduced results; thus, when fingerprinting long strings,</em><a name="41" href="#41">41</a>  <em>it's better to obtain irreduced results inside the fingerprinting loop</em><a name="42" href="#42">42</a>  <em>and use &lt;code>reduce&lt;/code> to reduce to a fingerprint after the loop.</em><a name="43" href="#43">43</a>  <a name="44" href="#44">44</a>  <em>*/</em><a name="45" href="#45">45</a>  <a name="46" href="#46">46</a>  <em class="comment">// Tested by: TestFPGenerator</em><a name="47" href="#47">47</a>  <a name="48" href="#48">48</a>  <strong>public</strong> <strong>final</strong> <strong>class</strong> <a href="../../../st/ata/util/FPGenerator.html">FPGenerator</a> {<a name="49" href="#49">49</a>  <a name="50" href="#50">50</a>      <em>/**<em>* Return a fingerprint generator.  The fingerprints generated</em></em><a name="51" href="#51">51</a>  <em>        will have degree &lt;code>degree&lt;/code> and will be generated by</em><a name="52" href="#52">52</a>  <em>        &lt;code>polynomial&lt;/code>.  If a generator based on</em><a name="53" href="#53">53</a>  <em>        &lt;code>polynomial&lt;/code> has already been created, it will be</em><a name="54" href="#54">54</a>  <em>        returned.  Requires that &lt;code>polynomial&lt;/code> is an</em><a name="55" href="#55">55</a>  <em>        irreducible polynomial of degree &lt;code>degree&lt;/code> (the</em><a name="56" href="#56">56</a>  <em>        array &lt;code>polynomials&lt;/code> contains some irreducible</em><a name="57" href="#57">57</a>  <em>        polynomials). */</em><a name="58" href="#58">58</a>      <strong>public</strong> <strong>static</strong> <a href="../../../st/ata/util/FPGenerator.html">FPGenerator</a> make(<strong>long</strong> polynomial, <strong>int</strong> degree) {<a name="59" href="#59">59</a>          Long l = <strong>new</strong> Long(polynomial);<a name="60" href="#60">60</a>          <a href="../../../st/ata/util/FPGenerator.html">FPGenerator</a> fpgen = (FPGenerator) generators.get(l);<a name="61" href="#61">61</a>          <strong>if</strong> (fpgen == <strong>null</strong>) {<a name="62" href="#62">62</a>              fpgen = <strong>new</strong> <a href="../../../st/ata/util/FPGenerator.html">FPGenerator</a>(polynomial, degree);<a name="63" href="#63">63</a>              generators.put(l, fpgen);<a name="64" href="#64">64</a>          }<a name="65" href="#65">65</a>          <strong>return</strong> fpgen;<a name="66" href="#66">66</a>      }<a name="67" href="#67">67</a>      <strong>private</strong> <strong>static</strong> <strong>final</strong> Hashtable generators = <strong>new</strong> Hashtable(10);<a name="68" href="#68">68</a>  <a name="69" href="#69">69</a>      <strong>private</strong> <strong>static</strong> <strong>final</strong> <strong>long</strong> zero = 0;<a name="70" href="#70">70</a>      <strong>private</strong> <strong>static</strong> <strong>final</strong> <strong>long</strong> one = 0x8000000000000000L;<a name="71" href="#71">71</a>  <a name="72" href="#72">72</a>  <a name="73" href="#73">73</a>      <em>/**<em>* Return a value equal (mod &lt;code>polynomial&lt;/code>) to</em></em><a name="74" href="#74">74</a>  <em>        &lt;code>fp&lt;/code> and of degree less than &lt;code>degree&lt;/code>. */</em><a name="75" href="#75">75</a>      <strong>public</strong> <strong>long</strong> reduce(<strong>long</strong> fp) {<a name="76" href="#76">76</a>          <strong>int</strong> N = (8 - degree/8);<a name="77" href="#77">77</a>          <strong>long</strong> local = (N == 8 ? 0 : fp &amp; (-1L &lt;&lt; 8*N));<a name="78" href="#78">78</a>          <strong>long</strong> temp = zero;<a name="79" href="#79">79</a>          <strong>for</strong> (<strong>int</strong> i = 0; i &lt; N; i++) {<a name="80" href="#80">80</a>              temp ^= ByteModTable[8+i][((<strong>int</strong>)fp) &amp; 0xff];<a name="81" href="#81">81</a>              fp >>>= 8;<a name="82" href="#82">82</a>          };<a name="83" href="#83">83</a>          <strong>return</strong> local ^ temp;<a name="84" href="#84">84</a>      }<a name="85" href="#85">85</a>  <a name="86" href="#86">86</a>      <em>/**<em>* Extends &lt;code>f&lt;/code> with lower eight bits of &lt;code>v&lt;/code></em></em><a name="87" href="#87">87</a>  <em>        with&lt;em>out&lt;/em> full reduction.  In other words, returns a</em><a name="88" href="#88">88</a>  <em>        polynomial that is equal (mod &lt;code>polynomial&lt;/code>) to the</em><a name="89" href="#89">89</a>  <em>        desired fingerprint but may be of higher degree than the</em><a name="90" href="#90">90</a>  <em>        desired fingerprint. */</em><a name="91" href="#91">91</a>      <strong>public</strong> <strong>long</strong> extend_byte(<strong>long</strong> f, <strong>int</strong> v) {<a name="92" href="#92">92</a>          f ^= (0xff &amp; v);<a name="93" href="#93">93</a>          <strong>int</strong> i = (<strong>int</strong>)f;<a name="94" href="#94">94</a>          <strong>long</strong> result = (f>>>8);<a name="95" href="#95">95</a>          result ^= ByteModTable[7][i &amp; 0xff];<a name="96" href="#96">96</a>          <strong>return</strong> result;<a name="97" href="#97">97</a>      }<a name="98" href="#98">98</a>  <a name="99" href="#99">99</a>      <em>/**<em>* Extends &lt;code>f&lt;/code> with lower sixteen bits of &lt;code>v&lt;/code>.</em></em><a name="100" href="#100">100</a> <em>        Does not reduce. */</em><a name="101" href="#101">101</a>     <strong>public</strong> <strong>long</strong> extend_<strong>char</strong>(<strong>long</strong> f, <strong>int</strong> v) {<a name="102" href="#102">102</a>         f ^= (0xffff &amp; v);<a name="103" href="#103">103</a>         <strong>int</strong> i = (<strong>int</strong>)f;<a name="104" href="#104">104</a>         <strong>long</strong> result = (f>>>16);<a name="105" href="#105">105</a>         result ^= ByteModTable[6][i &amp; 0xff]; i >>>= 8;<a name="106" href="#106">106</a>         result ^= ByteModTable[7][i &amp; 0xff];<a name="107" href="#107">107</a>         <strong>return</strong> result;<a name="108" href="#108">108</a>     }<a name="109" href="#109">109</a> <a name="110" href="#110">110</a>     <em>/**<em>* Extends &lt;code>f&lt;/code> with (all bits of) &lt;code>v&lt;/code>.</em></em><a name="111" href="#111">111</a> <em>        Does not reduce. */</em><a name="112" href="#112">112</a>     <strong>public</strong> <strong>long</strong> extend_<strong>int</strong>(<strong>long</strong> f, <strong>int</strong> v) {<a name="113" href="#113">113</a>         f ^= (0xffffffffL &amp; v);<a name="114" href="#114">114</a>         <strong>int</strong> i = (<strong>int</strong>)f;<a name="115" href="#115">115</a>         <strong>long</strong> result = (f>>>32);<a name="116" href="#116">116</a>         result ^= ByteModTable[4][i &amp; 0xff]; i >>>= 8;<a name="117" href="#117">117</a>         result ^= ByteModTable[5][i &amp; 0xff]; i >>>= 8;<a name="118" href="#118">118</a>         result ^= ByteModTable[6][i &amp; 0xff]; i >>>= 8;<a name="119" href="#119">119</a>         result ^= ByteModTable[7][i &amp; 0xff];<a name="120" href="#120">120</a>         <strong>return</strong> result;<a name="121" href="#121">121</a>     }<a name="122" href="#122">122</a> <a name="123" href="#123">123</a>     <em>/**<em>* Extends &lt;code>f&lt;/code> with &lt;code>v&lt;/code>.</em></em><a name="124" href="#124">124</a> <em>        Does not reduce. */</em><a name="125" href="#125">125</a>     <strong>public</strong> <strong>long</strong> extend_<strong>long</strong>(<strong>long</strong> f, <strong>long</strong> v) {<a name="126" href="#126">126</a>         f ^= v;<a name="127" href="#127">127</a>         <strong>long</strong> result = ByteModTable[0][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="128" href="#128">128</a>         result ^= ByteModTable[1][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="129" href="#129">129</a>         result ^= ByteModTable[2][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="130" href="#130">130</a>         result ^= ByteModTable[3][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="131" href="#131">131</a>         result ^= ByteModTable[4][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="132" href="#132">132</a>         result ^= ByteModTable[5][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="133" href="#133">133</a>         result ^= ByteModTable[6][(<strong>int</strong>)(f &amp; 0xff)]; f >>>= 8;<a name="134" href="#134">134</a>         result ^= ByteModTable[7][(<strong>int</strong>)(f &amp; 0xff)];<a name="135" href="#135">135</a>         <strong>return</strong> result;<a name="136" href="#136">136</a>     }<a name="137" href="#137">137</a> <a name="138" href="#138">138</a> <a name="139" href="#139">139</a>     <em>/**<em>* Compute fingerprint of "n" bytes of "buf" starting from</em></em><a name="140" href="#140">140</a> <em>        "buf[start]".  Requires "[start, start+n)" is in bounds. */</em><a name="141" href="#141">141</a>     <strong>public</strong> <strong>long</strong> fp(byte[] buf, <strong>int</strong> start, <strong>int</strong> n) {<a name="142" href="#142">142</a>         <strong>return</strong> extend(empty, buf, start, n);<a name="143" href="#143">143</a>     }<a name="144" href="#144">144</a> <a name="145" href="#145">145</a>     <em>/**<em>* Compute fingerprint of (all bits of) "n" characters of "buf"</em></em><a name="146" href="#146">146</a> <em>        starting from "buf[i]".  Requires "[i, i+n)" is in bounds. */</em>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -