Home > Blockchain >  Write manipulated buffer to new file
Write manipulated buffer to new file

Time:11-08

My program tries to decrypt the data of a file by writing the file to a buffer, manipulating the buffer data, then writing the buffer to a new output file. Currently this program does not manipulate the data inside the buffer, it just writes the buffer data to the output file as is. What would be the correct way to write the manipulated data from my buffer into a new file?

 /*-----------------------------------------
 *swaps the nibbles of a byte
 */
unsigned char nibbleSwap(unsigned char byte){
        return( (byte & 0x0f) << 4 | (byte & 0xf0) >>4);
}

 /*-----------------------------------------
 * swaps the bits of given data
 */
unsigned char bitSwap(unsigned char byte){
        unsigned char b = nibbleSwap(byte);
        b= (b & 0xcc) >>2 | (b & 0x33) <<2;
        b= (b & 0xaa) >>1 | (b & 0x55) <<1;
        return(b);
}

int main(int argc, char**argv){
        FILE * pFile;
        char * outputFile;
        unsigned char * buffer;
        long lSize;
        size_t result;

        //gets filename from cmd line
        printf("Author: Torin Costales \n");
        for (int x=1; x<argc; x  )
                printf("Input File: %s\n",  argv[x]);
        pFile = fopen(argv[1], "rb");
        if(pFile == NULL){
                printf("OPEN ERROR");
                return -1;
        }
        //file size
        fseek (pFile, 0, SEEK_END);
        lSize= ftell(pFile);
        rewind (pFile);

        //buffer allocation
        buffer = (unsigned char*) malloc (sizeof(unsigned char)*lSize);
        if(buffer == NULL) {
                printf("MEM ERROR");
                return -1;
        }
       
        //read data from file into the buffer
        result =fread(buffer, sizeof(unsigned char), lSize, pFile);
        if(result != lSize){
                printf("Read error");
                return -1;
        }
        //decrypt data, odd bytes nibbles are swapped, even bytes have bits reversed
        for(int x =0; x < sizeof(buffer); x  ){
                if(x % 2 == 1){
                        nibbleSwap(buffer[x]);
                }
                else
                        bitSwap(buffer[x]);
        }

        //make output file
        if(argc >=2){
                outputFile = argv[1];
                char appendix[] = ".d";
                strncat(outputFile, appendix, 2);
                pFile= fopen(outputFile, "wb");
                printf("Output File: %s\n", outputFile);
                fwrite(buffer, sizeof(unsigned char), lSize, pFile);
                fclose (pFile);
        }
        return 1;
}

CodePudding user response:

Try assigning the result of nibbleSwap and bitSwap calls to buffer[x], i.e. change your second for loop like so:

        for(int x =0; x < sizeof(buffer); x  ){
                if(x % 2 == 1){
                        buffer[x] = nibbleSwap(buffer[x]);
                }
                else
                        buffer[x] = bitSwap(buffer[x]);
        }
  • Related