簡體   English   中英

const unsigned char *到字符串或const char *的轉換

[英]const unsigned char* conversion to/from string or const char*

我迷失在指針的黑社會! 這是我的問題

這很奇怪,我只能控制其中一個功能,所以請不要說我需要重新設計。 正在使用android-ndkr7在Linux Ubuntu 11.04中進行編譯。 它是純本地應用程序(或服務),將在android手機上運行。 我正在使用Google測試來驗證我的課程/功能。 第一個函數(我的測試類)必須聲明未簽名的char *,它將其傳遞給第二個函數以用作輸出(crypt :: encryptBuffer),加密采用聲明的變量,為其分配內存,並將其傳遞給第三個函數是將值作為輸出放置到其中的位置。

加密

class Crypt
{
public:

   Crypt();
   ~Crypt();

   bool encryptBuffer(const unsigned char* inDecryptBuffer, const int inputSize, unsigned char** outEncryptBuffer, int*  pOutSize);

};

#endif

加密文件

#include "Crypt.h"
#include "pan/crypt.h"

static unsigned char HydraEncryptionKey[] = {0x17, 0x43, 0x9B, 0x55, 0x07, 0xAE, 0x73, 0xB1, 0x32, 0x10, 0xE0, 0x22, 0xD9, 0xC7, 0xF2, 0x3B};

bool AccCrypt::encryptBuffer(const unsigned char* inDecryptBuffer, const int inputSize, unsigned char** outEncryptBuffer, int*  pOutSize)
{
    int encryptedSize;
    pan::aes128_cbc enc(HydraEncryptionKey);

    // see how long the encrypted data will be and allocate space for the data
    encryptedSize = enc.output_len( inputSize );

    *outEncryptBuffer = (unsigned char*)malloc(encryptedSize + 4);

    enc.encrypt(inDecryptBuffer, *outEncryptBuffer, inputSize );
    return true;
}

CryptTest.cpp

#incude "Crypt.h"
#include <gtest/gtest.h>

#define CHECK_COND(X, a, b, c) { \
if(X) \
{ \
    printf("FAIL: %s\n", c); \
    printf("Press any key to continue");\
    getc(stdin);\
}\
else \
{ \
    printf("PASS: %s\n", c); \
}\
}

#define EXPECT_EQ(a,b,c)  CHECK_COND((a != b), a, b, c)

const char* decBuff = "something";
const int inputSize = 10;
unsigned char* encBuffTest = NULL;
int pOutsize = 0;

class cryptTester : public testing::Test
{
    protected:
    virtual void SetUp()
    {
        cryptTest = new Crypt();
        cryptTest->encryptBuffer((const unsigned char*)decBuff, inputSize, &encBuffTest, &pOutsize);
    }

    virtual void TearDown()
    {
    }

    Crypt* cryptTest;

};
TEST_F(AccCryptTest, decryptBuffer)
{
    int poutSize = 0;
    EXPECT_EQ(true, accCryptTest->decryptBuffer((const unsigned char*)encBuffTest, pOutsize, &outDecryptBuffTest, &poutSize), "decryptBuffer(valid, valid)");

}

這樣可以很好地編譯,但是當我在電話上運行它時,我遇到了分段錯誤。 我無法弄清楚這是哪里發生的,因為我無法從adb shell正確設置調試。

任何幫助,將不勝感激!

您的代碼似乎沒問題,也許錯誤在於encrypt方法中:

enc.encrypt(inDecryptBuffer, *outEncryptBuffer, inputSize );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM