Exemplo n.º 1
0
void subtract_file(char *name)
{

        FILE *file;
        char *buffer;
        unsigned long fileLen;

        //Open file
        file = fopen(name, "rb");
        if (!file)
        {
                fprintf(stderr, "Unable to open file %s\n", name);
                return 0;
        }

        //Get file length
        fseek(file, 0, SEEK_END);
        fileLen=ftell(file);
        fseek(file, 0, SEEK_SET);

        //Allocate memory
        buffer=(char *)malloc(fileLen+1);
        if (!buffer)
        {
                fprintf(stderr, "Memory error!");
                                fclose(file);
                return 0;
        }

        //Read file contents into buffer
        fread(buffer, fileLen, 1, file);
        fclose(file);

        printf("shuffle file size %lu \n", fileLen);

        int times = 0;
        unsigned long seed = nSeed;
        for (times = 0; times < timeslast; times++) {
          printf("shuffle file %lu \n", times);
          sub_buffer(buffer, fileLen);
          nSeed  = seed;
          nSeed2 = seed;
        }
        write_sub_buffer(name, buffer, fileLen);
        // dump_buffer_hex(buffer, fileLen);

        free(buffer);
}
Exemplo n.º 2
0
int
crystal_router( CrRouter *crr )
{
    L4C(__log4c_category_trace(GlobLogCat, "crystal_router(%x) -- begin", crr));
    L4C(log_buffer_content("crystal_router() -- begin: output buffer",
                       &crr->out_buf, (CURRENT|OLD)));
    L4C(log_buffer_content("crystal_router() -- begin: input buffer",
                       &crr->in_buf, (CURRENT|OLD)));

    int rec_bytes, d, resp, chan, read_bytes;
    CrBuffer combuf;

    resp = 0;
    compress(&crr->in_buf);
    resp += copy_to_buf(&crr->in_buf, &crr->out_buf, crr->procnum,
                        crr->write_msg_func, crr->userdata);

    for (d = 0; d < crr->doc; d++)
    {
        L4C(__log4c_category_trace(GlobLogCat,
                               "  crystal_router():\tdimension %d", d));

        chan = 1<<d;
        if ( d > 0 )
            resp += compress(&crr->out_buf);

        crb_clear(&crr->send_buf);
        if ( crr->read_msg_func != NULL )
        (*crr->read_msg_func)(&crr->send_buf, d, crr->userdata);
        resp += check_buffer(&crr->send_buf, &crr->out_buf, d, crr->procnum);

        sub_buffer(&combuf, &crr->out_buf);

        L4C(log_buffer_content("crystal_router() - send buffer",
                               &crr->send_buf, (CURRENT|OLD)));

        read_bytes = cishift(combuf.start_ptr, combuf.buf_size, chan,
                            crr->send_buf.start_ptr, crr->send_buf.bytes, chan,
                            crr->comm);

        if (read_bytes < 0)
        {
            // TODO: This seems to be wrong ??!!??!!
            rec_bytes = rescue_buf(combuf.start_ptr, combuf.buf_size);
            resp = -1;
        }
        else
            rec_bytes = read_bytes;
        combuf.bytes = rec_bytes;
        combuf.buf_ptr = combuf.start_ptr + rec_bytes;

        resp += copy_to_buf(&crr->in_buf, &combuf, crr->procnum,
                            crr->write_msg_func, crr->userdata);

        // absorb combuf
        crr->out_buf.bytes  += rec_bytes;
        crr->out_buf.buf_ptr = crr->out_buf.start_ptr + crr->out_buf.bytes;
    }
    crr->in_buf.cur_pos = crr->in_buf.bytes;
    compress(&crr->out_buf);

    L4C(log_buffer_content("crystal_router() -- end: output buffer",
                       &crr->out_buf, (CURRENT|OLD)));
    L4C(log_buffer_content("crystal_router() -- end: input buffer",
                       &crr->in_buf, (CURRENT|OLD)));
    L4C(__log4c_category_trace(GlobLogCat, "crystal_router() -- end"));

    return (resp < 0) ? -1 : 0;
}