0

mmapを使用してファイルを読み込み、暗号化してから、暗号化を出力ファイルに書き込もうとしています。mmapでもこれを実行しようとしていますが、コードを実行すると、「無効な引数」が原因でマップを解除できなかったことがわかります。

//Open files initialy and obtain a handle to the file.
inputFile = open(inFileName, O_RDONLY, S_IREAD);
outputFile = open(outFileName, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IWRITE);

//Allocate buffers for encrption.
from = (unsigned char*)malloc(blockSize);
to = (unsigned char*)malloc(blockSize);

mmapWriteBuff = (unsigned char*)malloc(blockSize);
mmapReadBuff = (unsigned char*)malloc(blockSize);

memset(to, 0, blockSize);
memset(from, 0, blockSize);
memset(mmapWriteBuff, 0, blockSize);
memset(mmapReadBuff, 0, blockSize);

//Make sure we have permission to read the file provided.
setFilePermissions(inFileName, PERMISSION_MODE);
setFilePermissions(outFileName, PERMISSION_MODE);

if(encriptParam)
{
    printf("*Encripting file: %s *\n", inFileName);

do//Go through the entire file.
{   
    if(memParam)
    {
            currAmt = lseek(inputFile, blockSize, SEEK_SET);
        mmapReadBuff = mmap(0, blockSize, PROT_READ, MAP_SHARED, inputFile, 0);

        /*
         *This is how you encrypt an input char* buffer "from", of length "len"
         *onto output buffer "to", using key "key".  Jyst pass "iv" and "&n" as
         *shown, and don't forget to actually tell the function to BF_ENCRYPT.
         */

        BF_cfb64_encrypt(mmapReadBuff, mmapWriteBuff, blockSize, &key, iv, &n, BF_ENCRYPT);

        if(currAmt < blockSize)
        {
            writeAmt = lseek(outputFile, currAmt, SEEK_SET);
            mmapWriteBuff = mmap(0, currAmt, PROT_WRITE,  MAP_SHARED, outputFile, 0);                   
            if(errno == EINVAL)
            {
                perror("MMAP failed to start write buffer: ");

                exit(MMAP_IO_ERROR);
            }
        }
        else
        {
            writeAmt = lseek(outputFile, blockSize, SEEK_SET);
            mmapWriteBuff = mmap(0, blockSize, PROT_WRITE, MAP_SHARED, outputFile, 0);                  
            if(errno == EINVAL)
            {
                perror("MMAP failed to start write buffer: ");

                exit(MMAP_IO_ERROR);
            }
        }

        mmapWriteBuff = to;
    }
    else
    {
        currAmt = read(inputFile, from, blockSize);

        /*
         *This is how you encrypt an input char* buffer "from", of length "len"              *onto output buffer "to", using key "key".  Jyst pass "iv" and "n" as
         *shown, and don't forget to actually tell the function to BF_ENCRYT.
        */

        BF_cfb64_encrypt(from, to, blockSize, &key, iv, &n, BF_ENCRYPT);

        if(currAmt < blockSize)
        {
            writeAmt = write(outputFile, to, currAmt);
        }
        else
        {
            writeAmt = write(outputFile, to, blockSize);
        }
    }

    if(memParam)
    {
        //if(currAmt < blockSize)
        //{
        //  if(munmap(mmapWriteBuff, currAmt) == -1)
        //  {
        //      perror("MMAP failed to unmap itself: ");
        //      
        //      exit(MMAP_IO_ERROR);
        //  }
        //  
        //  if(munmap(mmapReadBuff, currAmt) == -1)
        //  {
        //      perror("MMAP failed to unmap itself: ");
        //      
        //      exit(MMAP_IO_ERROR);
        //  }
        //}
        //else
        //{

            if(munmap(mmapReadBuff, blockSize) == -1)
            {
                perror("MMAP Read Buffer failed to unmap itself: ");

                exit(MMAP_IO_ERROR);
            }

            if(munmap(mmapWriteBuff, blockSize) == -1)
            {
                perror("MMAP Write Buffer failed to unmap itself: ");                       
                exit(MMAP_IO_ERROR);
            }


        //}

    }

    memset(to, 0, strlen((char *)to));
    memset(from, 0, strlen((char *)from));
    memset(mmapReadBuff, 0, strlen((char*)mmapReadBuff));
    memset(mmapWriteBuff, 0, strlen((char*)mmapWriteBuff));
    }
    while(currAmt > 0);

    printf("*Saving file: %s *\n", outFileName);

}

4

1 に答える 1

0

一般的に言えば、フラグoutputFileなしでファイル記述子を設定してみることをお勧めします。フラグをO_WRONLY使用するだけでは不十分です。O_WRONLYmmap()

したがって、これを変更する必要がある場合があります。

outputFile = open(outFileName, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IWRITE);

これに:

outputFile = open(outFileName, O_APPEND | O_CREAT | O_TRUNC | O_RDWR, S_IWRITE);

私は の専門家ではありmmap()ませんが、ファイル記述子を開くときに読み取りと書き込みの両方のアクセス許可を付与する必要があることは知っています。

編集:mmap()また、すべての呼び出しを(int*)次のように キャストしたい:

mmapReadBuff = (int*)mmap(0, blockSize, PROT_READ, MAP_SHARED, inputFile, 0);
于 2012-09-30T08:11:32.107 に答える