2012-03-15 208 views
0

我迷失在黑社會的指針!這是我的問題,const無符號字符*轉換爲/從字符串或const char *

這是非常古怪,我只能控制其中一個功能,所以請不要說我需要重新設計。 這正在使用android-ndkr7在Linux Ubuntu 11.04中進行編譯。它是純粹的原生應用程序(或服務),將運行在Android手機上。我正在使用谷歌測試來驗證我的課程/功能。 第一個函數(我的測試類)必須聲明unsigned char *,它將它傳遞給第二個函數以用作輸出(crypt :: encryptBuffer),encrypt接受聲明的變量,爲它分配內存並將其傳遞給第三個函數是將值放入其中作爲輸出的位置。

Crypt.h

class Crypt 
{ 
public: 

    Crypt(); 
    ~Crypt(); 

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

}; 

#endif 

Crypt.cpp

#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正確設置調試。

任何幫助,將不勝感激!

+0

您應該能夠調試這在完全支持的開發環境的舒適性(如在gdb或運行本地代碼你最喜歡的其他調試器,而不是手機)。一旦你發現這個問題,在NDK for Android下編譯時應該沒有區別。無論失敗與unsigned char和char無關。 – mah 2012-03-15 20:49:48

+0

這很明顯,但我建議在分配內存(malloc)和創建對象(新)之後測試指針。 – guga 2012-03-15 21:29:38

+0

-1不是真正的代碼。 'cryptTest = new Crypt();''cryptTest'是一個類名不應該編譯。浪費了人們的時間。 – 2012-03-15 23:55:44

回答

0

您的代碼似乎確定,也許錯誤是在encrypt方法:

enc.encrypt(inDecryptBuffer, *outEncryptBuffer, inputSize); 
相關問題