"Fossies" - the Fresh Open Source Software Archive

Member "apg-2.2.3/bfconvert/bfconvert.c" (7 Aug 2003, 5619 Bytes) of package /linux/privat/old/apg-2.2.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2 ** Copyright (c) 2003
    3 ** Adel I. Mirzazhanov. All rights reserved
    4 **
    5 ** Redistribution and use in source and binary forms, with or without
    6 ** modification, are permitted provided that the following conditions
    7 ** are met:
    8 ** 
    9 **     1.Redistributions  of  source  code  must  retain  the  above
   10 **       copyright notice, this list of conditions and the following
   11 **       disclaimer. 
   12 **     2.Redistributions  in  binary  form  must reproduce the above
   13 **       copyright notice, this list of conditions and the following
   14 **       disclaimer  in  the  documentation  and/or  other materials
   15 **       provided with the distribution. 
   16 **     3.The  name  of  the  author  may  not  be used to endorse or
   17 **       promote  products  derived   from   this  software  without
   18 **       specific prior written permission. 
   19 **        
   20 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND ANY EXPRESS
   21 ** OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED
   22 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23 ** ARE DISCLAIMED.  IN  NO  EVENT  SHALL THE AUTHOR BE LIABLE FOR ANY
   24 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE
   26 ** GOODS OR SERVICES;  LOSS OF USE,  DATA,  OR  PROFITS;  OR BUSINESS
   27 ** INTERRUPTION)  HOWEVER  CAUSED  AND  ON  ANY  THEORY OF LIABILITY,
   28 ** WHETHER  IN  CONTRACT,   STRICT   LIABILITY,  OR  TORT  (INCLUDING
   29 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   30 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   31 */
   32 
   33 /*
   34 ***************************************************************
   35 ** NAME        : BFCONVERT                                   **
   36 ** DESCRIPTION : convert APG Bloom-filter version 1.0.1 to   **
   37 **               version 1.1.0                               **
   38 ** USAGE       : bfconvert old_bf_file_name new_bf_file_name **
   39 ** RETURN      :  0 - success                                **
   40 **               -1 - something wrong                        **
   41 ***************************************************************
   42 */
   43 
   44 #include <stdio.h>
   45 
   46 #define OLD_APGBF_HEADER_LEN  12
   47 #define OLD_APGBF_HEADER_ID   "APGBF101"
   48 
   49 #define NEW_APGBF_HEADER_LEN  13
   50 #define NEW_APGBF_HEADER_ID   "APGBF"
   51 #define NEW_APGBF_HEADER_VER  "110"
   52 #define NEW_APGBF_HEADER_MODE 0x00
   53 
   54 
   55 int main (int argc, char *argv[]);
   56 
   57 int
   58 main(int argc, char *argv[])
   59 {
   60  typedef unsigned char f_mode;
   61 
   62  struct new_apg_bf_hdr {
   63    char id[5];           /* filter ID      */
   64    char version[3];      /* filter version */
   65    unsigned long int fs; /* filter size    */
   66    f_mode mode;          /* filter flags   */
   67  };
   68 
   69  struct old_apg_bf_hdr {
   70    char id[8];           /* ID          */
   71    unsigned long int fs; /* filter size */
   72  };
   73  struct new_apg_bf_hdr  new_bf_hdr;
   74  struct old_apg_bf_hdr  old_bf_hdr;
   75  
   76  char old_etalon_bf_id[]  = OLD_APGBF_HEADER_ID;
   77 
   78  char new_etalon_bf_id[]  = NEW_APGBF_HEADER_ID;
   79  char new_etalon_bf_ver[] = NEW_APGBF_HEADER_VER;
   80 
   81  FILE *old_f;  /* old filter file descriptor */
   82  FILE *new_f;  /* new filter file descriptor */
   83  
   84  unsigned char tmp_buf; /* Temporary buffer */
   85 
   86  /* Checking arguments */
   87  if (argc != 3)
   88   {
   89    printf ("Usage: bfconvert old_bf_file_name new_bf_file_name\n");
   90    return(-1);
   91   }
   92 
   93  /* Opening input and output files */
   94  if ((old_f = fopen (argv[1], "r")) == NULL)
   95   {
   96    perror("open the old bloom-filter file");
   97    return(-1);
   98   }
   99  if ((new_f = fopen (argv[2], "w")) == NULL)
  100   {
  101    perror("open the new bloom-filter file");
  102    return(-1);
  103   }
  104  if (fread ( (void *)&old_bf_hdr, OLD_APGBF_HEADER_LEN, 1, old_f) != 1)
  105     if (ferror (old_f) != 0)
  106      {
  107       perror("read from the old bloom-filter file");
  108       return(-1);
  109      }
  110 
  111  /* Checking input file */
  112  if ((old_bf_hdr.id[0] != old_etalon_bf_id[0]) || (old_bf_hdr.id[1] != old_etalon_bf_id[1]) ||
  113      (old_bf_hdr.id[2] != old_etalon_bf_id[2]) || (old_bf_hdr.id[3] != old_etalon_bf_id[3]) ||
  114      (old_bf_hdr.id[4] != old_etalon_bf_id[4]) || (old_bf_hdr.id[5] != old_etalon_bf_id[5]) ||
  115      (old_bf_hdr.id[6] != old_etalon_bf_id[6]) || (old_bf_hdr.id[7] != old_etalon_bf_id[7]) )
  116      {
  117       fprintf(stderr,"Input file is not APG bloom filter file v1.0.1\n");
  118       fflush (stderr);
  119       return (-1);
  120      }
  121 
  122  /* Constructing output BF file header */
  123  new_bf_hdr.id[0] = new_etalon_bf_id[0];
  124  new_bf_hdr.id[1] = new_etalon_bf_id[1];
  125  new_bf_hdr.id[2] = new_etalon_bf_id[2];
  126  new_bf_hdr.id[3] = new_etalon_bf_id[3];
  127  new_bf_hdr.id[4] = new_etalon_bf_id[4];
  128  new_bf_hdr.version[0] = new_etalon_bf_ver[0];
  129  new_bf_hdr.version[1] = new_etalon_bf_ver[1];
  130  new_bf_hdr.version[2] = new_etalon_bf_ver[2];
  131  new_bf_hdr.fs = old_bf_hdr.fs;
  132  new_bf_hdr.mode = NEW_APGBF_HEADER_MODE;
  133  
  134  /* Writing new filter header to output file */
  135  if (fwrite ( (void *)&new_bf_hdr, NEW_APGBF_HEADER_LEN, 1, new_f) != 1)
  136      {
  137       perror("write to the new bloom-filter file");
  138       return(-1);
  139      }
  140  /* Reading filter content from the old BF file and writing it to the new BF file */
  141  while (fread ( (void *)&tmp_buf, 1, 1, old_f) == 1)
  142   {
  143    if(fwrite( (void *)&tmp_buf, 1, 1, new_f) != 1)
  144     {
  145      perror("write to the new bloom-filter file");
  146      return(-1);
  147     }
  148   }
  149  if (ferror (old_f) != 0)
  150   {
  151    perror("read from the old bloom-filter file");
  152    return(-1);
  153   }
  154  /* Close input and output files */
  155  if (fclose(old_f) == EOF)
  156   {
  157    perror("close old bloom-filter file");
  158    return(-1);
  159   }
  160  if (fclose(new_f) == EOF)
  161   {
  162    perror("close new bloom-filter file");
  163    return(-1);
  164   }
  165    
  166  printf("\nInput file has been successfuly converted\n");
  167  return(0);
  168 }