/*
   This is simple filter for RC5DES client's log file. It extracts
   completed block type, size and base-key. After processing whole log
   it writes simple summary.

   This source is in pure ANSI C (I hope) and is FREEWARE but please 
   notify me if you modify it. Mail me at jrzeuski@neptun.gdansk.tpsa.pl
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main(int argc, char *argv[])
{
 FILE *fp;
 char buf[80],type[8],id[24];
 char *p, *p1;
 unsigned long size, tot_rc5 = 0, tot_des = 0;
 unsigned int n;

 if (argc != 1)
  {
   if (fp = fopen(argv[1],"r"))
    {
     while (p = fgets(buf,80,fp))
      {
       if (p1 = strstr(p, "Completed"))
        {
         sscanf(p1,"Completed %s block %s (%ld keys)\n",type,id,&size);
         if (strcmp(type,"block") == 0) /* RC5DES client 2.7013.396c */
          {
           strcpy(type,"RC5");          /* Assuming RC5 block - no DES contest during this client version*/
           sscanf(p1,"Completed block %s (%ld keys)\n",id,&size);
          }
         n = size >> 28;
         if (strcmp(type,"RC5") == 0) tot_rc5 += n;
         else
          {
           if (n == 0) n = 16;   /* 2^32 equals 0 in 32-bit arithmetics */
           tot_des += n;
          }
         printf("%s %2u*2^28 block %s\n",type,n,id);
       }
      }
     fclose(fp);
     printf("----------------------------------\n");
     printf("Total: %6lu 2^28 RC5 blocks\nTotal: %6lu 2^28 DES blocks\n",tot_rc5,tot_des);
    }
   else printf("Cannot open file\n");
  }
 else printf("Usage: %s log_file\n",argv[0]);
}
