Thread: copying buffers

  1. #1
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299

    copying buffers

    blowfish requires data come in 64 bit blocks


    now should i use a loop to copy the data or memcpy() ???


    Code:
    // NAME: blf_padData 
    //    pads data into 64-bit blocks
    //    void*        data    buffer you want to pad
    //    unsigned int  len    length in bits(uchar)
    //    int  &NumofBlocks    place to store num of 64 bit blocks
    //////////////////////////////////////////////////////////////
    unsigned long* blf_padData( void* data, const unsigned int len, int &NumofBlocks){
        unsigned int w,x,y,z;    //counters
        unsigned char* d;        //pointer to cast data into;
        unsigned long* pdata = 0;//pointer to the padded data.
        unsigned long  temp;
            
        if (!data)
            return NULL;
        
        d = (unsigned char*)data;
        
        //get the size of the buffer needed
        NumofBlocks = len/8;    //64-bit blocks
        if (0 != len%8)         //if it doesnt fit perfect then add a block
            NumofBlocks++;                             
        
        //make the buffer
        pdata = (unsigned long*) malloc( 8 * NumofBlocks);    //8 = sizeof(unsigned long)*2  64-bit blocks
            w = 0;
        if (!pdata)
            return NULL;
        
        //copy all of data into pdata               
        for(x = 0, z = 0; x < NumofBlocks; x++){
            for(int loopc = 0; loopc < 2; loopc++){
                temp = 0x00000000;     
                for(y = 0; y < 4; y++){
                    temp <<= 8;        
                    if(z < len)
                        temp = temp | d[z];                 
                    z++;                                                        
                }
                pdata[w] = temp;
                w++;
            }
        }
        
        //memset(pdata, 0, 8*NumofBlocks);
        //memcpy( pdata, data, len);
        
        return pdata;
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    memcpy should be faster.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    std::copy is the preferred way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in 16 and 24-bit audio data into (32-bit) integer buffers
    By theblindwatchma in forum C Programming
    Replies: 2
    Last Post: 04-13-2008, 11:12 PM
  2. Deep and Shallow Copying
    By peckitt99 in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2007, 09:37 PM
  3. Copying buffers?
    By fireonyx in forum C Programming
    Replies: 7
    Last Post: 03-08-2006, 10:20 AM
  4. Copy to Buffers
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-05-2004, 06:50 PM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM