mardi 13 novembre 2018

Generating a raw bit stream as an output to pipe in another software

So, I have been struggling with the following problem: I have a text file with millions of random numbers. They are pure text. I want to pipe it out to the input of another software that was supposed to get a bit stream (Dieharder is the software). It works with the /dev/urandom, like:

cat /dev/urandom | ./dieharder <options>

I've created a C routine like that to generate the binary raw output stream. It Converts the the ASCII code to decimal (like 0x0F = 15), and stream it out to stdout:

int main(int argc, char *argv[])
{
    FILE *fpr;
    FILE *fpw;
    unsigned int i = 0;
    size_t size=0;
    char c;       
    unsigned int *buffer; 
    char *filename;
    buffer = (unsigned int *) malloc(100);
    filename = (char *) malloc(128);

    if (argc != 2) 
    {
        scanf("%s", filename);
        fpr = fopen(filename, "rb");
    }
    else 
    {
        filename = (char *) realloc(filename, strlen(argv[1] + 1));
        strcpy(filename, argv[1]);
        fpr = fopen(filename, "rb");
    }
    if (fpr == NULL)
    {
        return -1;
    }
    while (c = fgetc(fpr) != EOF)
    {
        size++;
    }
    rewind(fpr);
    buffer = (int *) realloc(buffer, (size)*sizeof(int));
    memset(buffer, 0, sizeof(buffer));
    i=0;
    c = fgetc(fpr);
    while (c != EOF)
    {
       if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
       {
            buffer[i] = (unsigned int) ASCIItoDec(c);
       }
       else if (c == ' ')   
       { 
            buffer[i] = ' ';
       }
       c = fgetc(fpr);
       i++;
    }   
    fclose(fpr);
    i=0;
    //streaming output
    fwrite(buffer, size*sizeof(buffer), 1, stdout);
    return 0;   
}

The Dieharder software exits informing EOF was reached with out performing any analysis. What could possibly be wrong?




Aucun commentaire:

Enregistrer un commentaire