Back to index

lightning-sunbird  0.9+nobinonly
testmime.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: 
00004   CREATOR: eric 25 June 2000
00005   
00006   $Id: testmime.c,v 1.3 2002/06/28 10:45:12 acampi Exp $
00007   $Locker:  $
00008     
00009  The contents of this file are subject to the Mozilla Public License
00010  Version 1.0 (the "License"); you may not use this file except in
00011  compliance with the License. You may obtain a copy of the License at
00012  http://www.mozilla.org/MPL/
00013  
00014  Software distributed under the License is distributed on an "AS IS"
00015  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00016  the License for the specific language governing rights and
00017  limitations under the License.
00018  
00019 
00020  This program is free software; you can redistribute it and/or modify
00021  it under the terms of either: 
00022 
00023     The LGPL as published by the Free Software Foundation, version
00024     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00025 
00026   Or:
00027 
00028     The Mozilla Public License Version 1.0. You may obtain a copy of
00029     the License at http://www.mozilla.org/MPL/
00030 
00031   The Initial Developer of the Original Code is Eric Busboom
00032 
00033  (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
00034  ======================================================================*/
00035 
00036 #include "ical.h"
00037 
00038 #include <stdlib.h> /* For rand */
00039 #include <string.h> /* for strrchr, strdup*/
00040 #include <unistd.h>   /* for getopt */
00041 
00042 /*int sspm_parse_mime(struct sspm_part *parts, 
00043               size_t max_parts,
00044               struct sspm_action_map *actions,
00045               char* (*get_string)(char *s, size_t size, void* data),
00046               void *get_string_data,
00047               struct sspm_header *first_header
00048     );
00049 */
00050 
00051 
00052 
00053 char* major_type_string[] = {
00054     "TEXT",
00055     "IMAGE",
00056     "AUDIO",
00057     "VIDEO",
00058     "APPLICATION",
00059     "MULTIPART",
00060     "MESSAGE",
00061     "UNKNOWN",
00062     "NO"
00063 };
00064 
00065 char* minor_type_string[] = {
00066     "ANY",
00067     "PLAIN",
00068     "RFC822",
00069     "DIGEST",
00070     "CALENDAR",
00071     "MIXED",
00072     "RELATED",
00073     "ALTERNATIVE",
00074     "PARALLEL",
00075     "UNKNOWN",
00076     "NO"
00077 };
00078 
00079 
00080 char* read_stream(char *s, size_t size, void *d)
00081 {
00082     char *c = fgets(s,size, (FILE*)d);
00083 
00084     return c;
00085 
00086 }
00087 
00088 
00089 int main(int argc, char* argv[]) {
00090 
00091     FILE *f;
00092     int c;
00093     extern char *optarg;
00094     extern int optind, optopt;
00095     int errflg=0;
00096     char* program_name;
00097 
00098     struct options{
00099            int normal;
00100            int stress;
00101            int base64;
00102            int qp;
00103            int sleep;
00104            int count;
00105            char* input_file;
00106     } opt;
00107 
00108     memset(&opt,0,sizeof(opt));
00109     
00110     program_name = (char*)strrchr((char*)argv[0],'/');
00111     program_name++;
00112 
00113     while ((c = getopt(argc, argv, "nsbqi:S:c:")) != -1) {
00114        switch (c) {
00115            case 'i': { /* Input comes from named file */
00116               opt.input_file = strdup(optarg);
00117               break;
00118            }
00119            case 'n':{ /* Normal */
00120 
00121               if(opt.stress+opt.base64+opt.qp != 0){
00122                   fprintf(stderr,
00123                          "%s: Use only one of  n,s,b and q\n",
00124                          program_name);
00125               }
00126               opt.normal = 1;
00127               break;
00128            }
00129            case 's':{ /* Stress-test*/
00130               if(opt.base64+opt.normal+opt.qp != 0){
00131                   fprintf(stderr,
00132                          "%s: Use only one of  n,s,b and q\n",
00133                          program_name);
00134               }
00135               opt.stress = 1;
00136               break;
00137            }
00138            case 'b':{ /* test base64 decoding*/
00139               if(opt.stress+opt.normal+opt.qp != 0){
00140                   fprintf(stderr,
00141                          "%s: Use only one of  n,s,b and q\n",
00142                          program_name);
00143               }
00144               opt.base64 = 1;
00145               break;
00146            }
00147            case 'q':{ /* test quoted-printable decoding*/
00148               if(opt.stress+opt.base64+opt.normal != 0){
00149                   fprintf(stderr,
00150                          "%s: Use only one of  n,s,b and q\n",
00151                          program_name);
00152               }
00153               opt.qp = 1;
00154               break;
00155            }
00156            case 'S':{ /* sleep at end of run */
00157               opt.sleep = atoi(optarg);
00158               break;
00159            }
00160 
00161            case 'c':{ /* number of iterations of stress test */
00162               opt.count = atoi(optarg);
00163               break;
00164            }
00165            
00166            case ':': {/* Option given without an operand */
00167               fprintf(stderr,
00168                      "%s: Option -%c requires an operand\n", 
00169                      program_name,optopt);
00170               errflg++;
00171               break;
00172            }
00173            case '?': {
00174               errflg++;
00175            }
00176        }
00177     }
00178     
00179     if (errflg >0){
00180        fprintf(stderr,"Usage: %s [-n|-s|-b|-q] [-i input_file]\n",
00181               program_name);
00182        exit(1);
00183     }
00184     
00185     if(opt.stress+opt.base64+opt.normal+opt.qp == 0){
00186        fprintf(stderr,
00187               "%s: Must have one of n,s,b or q\n",
00188               program_name);
00189     }
00190     
00191     if(opt.input_file){
00192        f = fopen(opt.input_file,"r");
00193        if (f == 0){
00194            fprintf(stderr,"Could not open input file \"%s\"\n",
00195                   opt.input_file);
00196            exit(1);
00197        }
00198     } else {
00199        f = stdin;
00200     }
00201        
00202        
00203 
00204     if(opt.normal == 1){
00205        icalcomponent *c;
00206 
00207        c = icalmime_parse(read_stream,f);
00208        
00209        printf("%s\n",icalcomponent_as_ical_string(c));
00210 
00211        icalcomponent_free(c);
00212 
00213     }  else if (opt.stress==1 ){
00214        /* Read file in by lines, then randomize the lines into a
00215            string buffer */
00216 
00217        char *array[1024];
00218        char temp[1024];
00219               char *buf;
00220        int i,last;
00221        int size;
00222        int non_rand;
00223        int rand_lines;
00224        int r;
00225        int j;
00226        icalcomponent *c;
00227        struct slg_data {
00228               char* pos;
00229               char* str;
00230        } d;
00231     
00232        for(i=0; !feof(f); i++){
00233            fgets(temp,1024,f);
00234            array[i] = strdup(temp);
00235            size += strlen(temp);
00236        }
00237        last = i;
00238  
00239        buf = malloc(size*2);
00240        assert(buf != 0);
00241        
00242 
00243        for(j=0; j<opt.count; j++){
00244 
00245            srand(j);
00246            memset(buf,0,size*2);
00247            /* First insert some non-randomized lines */
00248            non_rand = ((float)rand()/(float)RAND_MAX) * last;
00249            for(i=0;i<non_rand;i++){
00250               strcat(buf,array[i]);
00251            }
00252            
00253            /* Then, insert some lines at random */
00254            
00255            rand_lines = last - non_rand;
00256            
00257            for(i=0;i<rand_lines;i++){
00258               srand(i);
00259               r = ((float)rand()/(float)RAND_MAX) * rand_lines;
00260               strcat(buf,array[r+non_rand]);
00261               
00262            }
00263 
00264            d.pos = 0;
00265            d.str = buf;
00266 
00267            c = icalmime_parse(icalparser_string_line_generator,&d);
00268 
00269            printf("%s\n",icalcomponent_as_ical_string(c));
00270 
00271            icalcomponent_free(c);
00272            
00273        }
00274 
00275        free(buf);
00276 
00277        for(i=0; i<last; i++){
00278            free(array[i]);
00279        }
00280 
00281     } else if(opt.qp == 1){ 
00282        char str[4096];
00283        char conv[4096];
00284        
00285        memset(str,0,4096);
00286        
00287        while(!feof(f) && fgets(str,4096,f)!=0){
00288            size_t size;
00289            
00290            size = strlen(str);
00291            memset(conv,0,4096);
00292            decode_quoted_printable(conv,str,&size);
00293            
00294            conv[size] = '\0';
00295            printf("%s",conv);
00296            memset(str,0,4096);
00297            
00298        }
00299     } else if (opt.base64 == 1) {
00300        char str[4096];
00301        char conv[4096];
00302        
00303        memset(str,0,4096);
00304        
00305        while(!feof(f) && fgets(str,4096,f)!=0){
00306            size_t size;
00307            
00308            size = strlen(str);
00309            memset(conv,0,4096);
00310            decode_base64(conv,str,&size);
00311            
00312            conv[size] = '\0';
00313            printf("%s",conv);
00314            memset(str,0,4096);
00315            
00316        }
00317     }
00318 
00319     if (opt.sleep != 0){
00320        sleep(opt.sleep);
00321     }
00322 
00323     if(       opt.input_file != 0){
00324        free(opt.input_file);
00325     }
00326 
00327     icalmemory_free_ring();
00328 
00329     return 0;
00330 
00331 }
00332 
00333 
00334 
00335 
00336 
00337        
00338        
00339