mardi 10 avril 2018

Encryption Program, Floating Point Exception in C, infinite loop

I'm writing an implementation of a Linear Congruential Generator to that can be used to create a sequence of “random” numbers. The class shown below is the cipher.c class that:

  1. Reads characters from the standard input stream using the standard library function getchar().

  2. Determines whether each line encodes a valid cipher direction, key pair and data.

  3. For each valid line use the described cipher algorithm either encrypt or decrypt the data. Send the result to the standard output stream. If the line contains an error, then skip to the end of the line, print an error message, and read the next line.

Format for cipher input is:

enter image description here

Example : e126,25,Byte

I believe most of the logic is correct, however I keep getting an infinite loop when I run in command line, with no output. When I adjust, I keep getting a Floating point exception: 8. I am not sure at all why this is occurring.

#include "lcg.h"
#include <stdio.h>

int main(){

char c;
char ans;
long encryptFlag = 0;
unsigned long m = 0;
unsigned long lcg_c = 0;
struct LinearCongruentialGenerator lcg_temp;
unsigned long nextNumber = 0;
int inputLineNumber = 0;
long flag = 0;
long printTillNow = 0;

while((c = getchar()))
{

    inputLineNumber++;

  /* If c == e set encrypt flag */
    if(c == 'e')
  {
        encryptFlag = 1;
    }

  /* If c == d set decript flag */
    else if(c == 'd')
  {
        encryptFlag = 0;
    }

  /* Else print out error code */
    else
  {
        printf("%5d) Error 24\n", inputLineNumber);
        while(c!='\n')
     {
            c = getchar();
       }
        continue;
    }

  /* get next char in line */
    c = getchar();

    if(c == ',')
  {
        printf("%5d) Error 32\n", inputLineNumber);

        while(c!='\n')
     {
            c = getchar();
        }
        continue;

    }

    while(c != ',')
  {
        m = m*10;
        m += c - '0';
        c = getchar();
    }

    c = getchar();

    while(c != ',')
  {
        lcg_c *= 10;
        lcg_c += c - '0';
        c = getchar();
    }

    /* printf("%d %d ",m, lcg_c );*/
    lcg_temp = makeLCG(m, lcg_c);


    if(lcg_temp.a == 0)
  {
        printf("%5d) Error 48\n", inputLineNumber);
        while(c!='\n')
     {
            c = getchar();
        }
        continue;
    }


    /* printf("%lu %lu\n", m, lcg_c);*/
    c = getchar();

    while(c != '\n')
  {
        if(c < 32 || c > 126)
     {
            flag = 1;

            while(c!='\n')
        {
                c = getchar();
            }
            break;

        }


        nextNumber = getNextRandomValue(&lcg_temp);
        ans = c ^ (nextNumber % 128);

        if(printTillNow == 0)
     {
            printTillNow = 1;
            printf("%5d) ", inputLineNumber);
        }

        if(ans < 32)
     {
            printf("*%c", '?'+ans);
        }

        else if(ans == 127)
     {
            printf("*!");
        }

        else if(ans == '*')
     {
            printf("**");
        }

        else
     {
            printf("%c", ans);
        }

        c = getchar();
    }

    if(flag == 1)
  {
        printf("%5d) Error \n", inputLineNumber);
    }
    else
  {
        printf("\n");
    }
}

return 0;
}




Aucun commentaire:

Enregistrer un commentaire