File: bfconvert.c

package info (click to toggle)
apg 2.2.3-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 608 kB
  • ctags: 533
  • sloc: ansic: 4,867; php: 744; sh: 194; makefile: 105; perl: 9
file content (168 lines) | stat: -rw-r--r-- 5,619 bytes parent folder | download | duplicates (7)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
** Copyright (c) 2003
** Adel I. Mirzazhanov. All rights reserved
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 
**     1.Redistributions  of  source  code  must  retain  the  above
**       copyright notice, this list of conditions and the following
**       disclaimer. 
**     2.Redistributions  in  binary  form  must reproduce the above
**       copyright notice, this list of conditions and the following
**       disclaimer  in  the  documentation  and/or  other materials
**       provided with the distribution. 
**     3.The  name  of  the  author  may  not  be used to endorse or
**       promote  products  derived   from   this  software  without
**       specific prior written permission. 
** 		  
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND ANY EXPRESS
** OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN  NO  EVENT  SHALL THE AUTHOR BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES;  LOSS OF USE,  DATA,  OR  PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER  CAUSED  AND  ON  ANY  THEORY OF LIABILITY,
** WHETHER  IN  CONTRACT,   STRICT   LIABILITY,  OR  TORT  (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
***************************************************************
** NAME        : BFCONVERT                                   **
** DESCRIPTION : convert APG Bloom-filter version 1.0.1 to   **
**               version 1.1.0                               **
** USAGE       : bfconvert old_bf_file_name new_bf_file_name **
** RETURN      :  0 - success                                **
**               -1 - something wrong                        **
***************************************************************
*/

#include <stdio.h>

#define OLD_APGBF_HEADER_LEN  12
#define OLD_APGBF_HEADER_ID   "APGBF101"

#define NEW_APGBF_HEADER_LEN  13
#define NEW_APGBF_HEADER_ID   "APGBF"
#define NEW_APGBF_HEADER_VER  "110"
#define NEW_APGBF_HEADER_MODE 0x00


int main (int argc, char *argv[]);

int
main(int argc, char *argv[])
{
 typedef unsigned char f_mode;

 struct new_apg_bf_hdr {
   char id[5];           /* filter ID      */
   char version[3];      /* filter version */
   unsigned long int fs; /* filter size    */
   f_mode mode;          /* filter flags   */
 };

 struct old_apg_bf_hdr {
   char id[8];           /* ID          */
   unsigned long int fs; /* filter size */
 };
 struct new_apg_bf_hdr  new_bf_hdr;
 struct old_apg_bf_hdr  old_bf_hdr;
 
 char old_etalon_bf_id[]  = OLD_APGBF_HEADER_ID;

 char new_etalon_bf_id[]  = NEW_APGBF_HEADER_ID;
 char new_etalon_bf_ver[] = NEW_APGBF_HEADER_VER;

 FILE *old_f;  /* old filter file descriptor */
 FILE *new_f;  /* new filter file descriptor */
 
 unsigned char tmp_buf; /* Temporary buffer */

 /* Checking arguments */
 if (argc != 3)
  {
   printf ("Usage: bfconvert old_bf_file_name new_bf_file_name\n");
   return(-1);
  }

 /* Opening input and output files */
 if ((old_f = fopen (argv[1], "r")) == NULL)
  {
   perror("open the old bloom-filter file");
   return(-1);
  }
 if ((new_f = fopen (argv[2], "w")) == NULL)
  {
   perror("open the new bloom-filter file");
   return(-1);
  }
 if (fread ( (void *)&old_bf_hdr, OLD_APGBF_HEADER_LEN, 1, old_f) != 1)
    if (ferror (old_f) != 0)
     {
      perror("read from the old bloom-filter file");
      return(-1);
     }

 /* Checking input file */
 if ((old_bf_hdr.id[0] != old_etalon_bf_id[0]) || (old_bf_hdr.id[1] != old_etalon_bf_id[1]) ||
     (old_bf_hdr.id[2] != old_etalon_bf_id[2]) || (old_bf_hdr.id[3] != old_etalon_bf_id[3]) ||
     (old_bf_hdr.id[4] != old_etalon_bf_id[4]) || (old_bf_hdr.id[5] != old_etalon_bf_id[5]) ||
     (old_bf_hdr.id[6] != old_etalon_bf_id[6]) || (old_bf_hdr.id[7] != old_etalon_bf_id[7]) )
     {
      fprintf(stderr,"Input file is not APG bloom filter file v1.0.1\n");
      fflush (stderr);
      return (-1);
     }

 /* Constructing output BF file header */
 new_bf_hdr.id[0] = new_etalon_bf_id[0];
 new_bf_hdr.id[1] = new_etalon_bf_id[1];
 new_bf_hdr.id[2] = new_etalon_bf_id[2];
 new_bf_hdr.id[3] = new_etalon_bf_id[3];
 new_bf_hdr.id[4] = new_etalon_bf_id[4];
 new_bf_hdr.version[0] = new_etalon_bf_ver[0];
 new_bf_hdr.version[1] = new_etalon_bf_ver[1];
 new_bf_hdr.version[2] = new_etalon_bf_ver[2];
 new_bf_hdr.fs = old_bf_hdr.fs;
 new_bf_hdr.mode = NEW_APGBF_HEADER_MODE;
 
 /* Writing new filter header to output file */
 if (fwrite ( (void *)&new_bf_hdr, NEW_APGBF_HEADER_LEN, 1, new_f) != 1)
     {
      perror("write to the new bloom-filter file");
      return(-1);
     }
 /* Reading filter content from the old BF file and writing it to the new BF file */
 while (fread ( (void *)&tmp_buf, 1, 1, old_f) == 1)
  {
   if(fwrite( (void *)&tmp_buf, 1, 1, new_f) != 1)
    {
     perror("write to the new bloom-filter file");
     return(-1);
    }
  }
 if (ferror (old_f) != 0)
  {
   perror("read from the old bloom-filter file");
   return(-1);
  }
 /* Close input and output files */
 if (fclose(old_f) == EOF)
  {
   perror("close old bloom-filter file");
   return(-1);
  }
 if (fclose(new_f) == EOF)
  {
   perror("close new bloom-filter file");
   return(-1);
  }
   
 printf("\nInput file has been successfuly converted\n");
 return(0);
}