File: cm.cn

package info (click to toggle)
xdelta 0.14-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 684 kB
  • ctags: 682
  • sloc: ansic: 5,668; sh: 1,740; makefile: 62
file content (130 lines) | stat: -rw-r--r-- 3,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* -*-Mode: C;-*-
 * XDELTA - RCS replacement and delta generator
 * Copyright (C) 1997  Josh MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: cm.cn 1.4.1.2 Sun, 12 Oct 1997 20:29:34 -0700 jmacd $
 */

MatchLevel*
FMNAME (MatchQuery *query,
	gint        width,
	gint        low_to_index,
	gint        high_to_index)
{
#ifdef DEBUG_HASH
  gint lti0 = low_to_index;
  gint hti0 = high_to_index;
#endif
  gint segment_len       = ipow2(width);
  gint buffer_index      = low_to_index + segment_len;
  gint buffer_index_old  = low_to_index;
  gint cksum_count       = (high_to_index - low_to_index) / segment_len;

  MatchLevel    *match_level;
  Checksum       cksum;
  ChecksumTable *table;

  if (cksum_count == 0)
    return NULL;

  table              = query->table;
  match_level        = g_new0 (MatchLevel, 1);
  match_level->chunk = g_mem_chunk_new ("match_level", sizeof (Match), 1024, G_ALLOC_ONLY);

  INITCKSUM (query->to_seg.ACCESS + low_to_index, segment_len, &cksum);

  for (;; buffer_index += 1, buffer_index_old += 1)
    {
      guint16 old_c;
      guint16 new_c;
      gint val;

#ifdef DEBUG_CKSUM
      if (buffer_index % segment_len == 0)
	g_print ("Looking up cksum %04x %04x at to_index = %d\n",
		 cksum.low,
		 cksum.high,
		 buffer_index - segment_len);
#endif

#ifdef DEBUG_XD
      {
	Checksum cmp;

	INITCKSUM (query->to_seg.ACCESS + buffer_index - segment_len, segment_len, &cmp);

	if (cmp.low != cksum.low || cmp.high != cksum.high)
	  g_print ("warning: checksum incorrectly computed\n");

	cksum = cmp;
      }
#endif

      val = lookup_checksum (query,
			     match_level,
			     table,
			     buffer_index,
			     segment_len,
			     &cksum,
			     low_to_index,
			     high_to_index);

      if (val >= 0)
	{
	  if (val + segment_len > high_to_index)
	    break;

	  INITCKSUM (query->to_seg.ACCESS + val, segment_len, &cksum);
	  buffer_index = val + segment_len - 1;
	  low_to_index = val;
	  buffer_index_old = val - 1;
	  continue;
	}

      if (buffer_index == high_to_index)
	break;

      old_c = CHEW(query->to_seg.ACCESS[buffer_index_old]);
      new_c = CHEW(query->to_seg.ACCESS[buffer_index]);

      cksum.low -= old_c;
      cksum.low += new_c;

      cksum.high -= segment_len * old_c;
      cksum.high += cksum.low;
    }

  match_level->to_intervals = g_slist_reverse (match_level->to_intervals);

  if (match_level->to_intervals == NULL)
    {
      match_level_free (match_level);
      return NULL;
    }

#ifdef DEBUG_HASH
  g_print ("***** Lookup results *****\n");
  g_print ("Lookup range:           %d-%d (%d)\n", lti0, hti0, hti0 - lti0);
  g_print ("Lookup segment width:   %d\n", segment_len);
  g_print ("Lookup matches:         %d\n", g_slist_length (match_level->to_intervals));
  g_print ("Lookup matches width:   %d\n", match_level->matches_width);
  g_print ("Lookup false alarms:    %d\n", match_level->false_alarms);
  g_print ("Lookup useless matches: %d\n", match_level->useless_matches);
#endif

  return match_level;
}