2012-03-15 214 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); 
相关问题