Sunday, April 29, 2012

Simple ipv4 dot notation parser I wrote once.

ip.c an ipv4 dot notation parser.
#include 



/*



This is a routine designed to parse the ipv4 dot notation.



Given a string with a dot notation value between

0.0.0.0 and 255.255.255.255

make sure this is a valid string of that format.



*/



static char ** string;

       char * str;



int

parseNum(){



       int value = 0;



       if (*string && isdigit(*string[0])){



               value = value*10+*string[0]-'0';



               str = *string;

               str++;

               string = &str;



               if (*string && isdigit(*string[0])){



                       value = value*10+*string[0]-'0';



                       str = *string;

                       str++;

                       string = &str;



                       if (*string && isdigit(*string[0])){



                               value = value*10+*string[0]-'0';



                               str = *string;

                               str++;

                               string = &str;



                       }

               }

       } else {

               return 0;

       }



       if (value < 256)

               return 1;

       else

               return 0;

}



int

parseDot(){



       if (*string && *string[0] == '.') {



               str = *string;

               str++;

               string = &str;



               return 1;

       } else

               return 0;



}



int

endOfString(){



       if (*string && *string[0] == 0)

               return 1;

       else

               return 0;



}

int

parseDotNotationV4 (char * stringtoparse){



       string = &stringtoparse;



       if (parseNum()){

        if (parseDot()){

         if (parseNum()){

          if (parseDot()){

           if (parseNum()){

            if (parseDot()){

             if (parseNum()){

               if (endOfString()){

                // Good dot notation value

                return 1;

               }

             }

            }

           }

          }

         }

        }

       }



       // failure

       return 0;

}



printfParseDotNotationV4(char * string){



       int returnVal = parseDotNotationV4(string);



       printf("%s %d\n", string, returnVal);



       return returnVal;

}



int main (){



       int i,j,k,l;



       char string[1024];



       printfParseDotNotationV4("255255.255.255");

       printfParseDotNotationV4("2");

       printfParseDotNotationV4("255.255.255.255.6");

       printfParseDotNotationV4("255.255.256.255");



       for(i=0; i<256; i=i+4){

        for(j=0; j<256; j=j+4){

         for(k=0; k<256; k=k+4){

          for(l=0; l<256; l=l+4){

               sprintf(string, "%d.%d.%d.%d", i, j, k, l);

               if (!parseDotNotationV4(string))

                       printf("%d failed when it should have been
 valid.", string);

          }

         }

        }

       }



       return 0;

}

No comments:

Post a Comment