iPhone - 最终确定 Apple 的模糊 "VerificationController.m"

标签 iphone objective-c ios ipad

我正在尝试实现新的 VerificationController.m Apple 发布的用于解决应用内购买欺诈问题的类。

作为 Apple 发布的所有内容,这是一份更加模糊、不完整且解释不当的文档,其中包含许多无法被所有人规避/理解的空白和未知数。

我正在尝试实现它,但在代码的末尾我们看到了这四种方法:

- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
#warning Replace this method.
    return nil;
}

- (NSString *)decodeBase64:(NSString *)input length:(NSInteger *)length
{
#warning Replace this method.
    return nil;
}

#warning Implement this function.
char* base64_encode(const void* buf, size_t size)
{ return NULL; }

#warning Implement this function.
void * base64_decode(const char* s, size_t * data_len)
{ return NULL; }

你可以看到 Apple 懒得在代码末尾实现 C 函数。由于我的 C/C++ 能力很差,我发现我需要在 C/C++ 中实现这两个函数,并且它们必须返回 char 和 void (???)。其他人已经在 SO 上发布了执行此操作的例程,但他们要么在 Objective-C 中,要么不返回 chars 和 void (??)。

注意:这是我遇到的另一个问题:如果 Apple 以这种形式使用方法,它怎么会返回 void?

uint8_t *purchase_info_bytes = base64_decode([purchase_info_string cStringUsingEncoding:NSASCIIStringEncoding],                                                 &purchase_info_length);

它不应该返回 uint8_t 吗?

注意 2:我遇到的另一个问题是 apple 说 base64_encode 是必需的,但他们提供的代码没有使用它。我认为他们在吸毒,或者我的 C/C++ 知识真的很糟糕。

所以,回到我的第一个问题。有人可以发布/指向一个可以完成声明方法 base64_encode 和 base64_decode 要求的工作的方法吗?请不要发布与 Apple 强加的这些要求不兼容的 objective-c 方法。

谢谢。

最佳答案

这个解决方案应该非常简单,其中包括填充缺失信息的所有方法。在沙箱中经过测试并正常运行。

//  single base64 character conversion
static int POS(char c)
{
    if (c>='A' && c<='Z') return c - 'A';
    if (c>='a' && c<='z') return c - 'a' + 26;
    if (c>='0' && c<='9') return c - '0' + 52;
    if (c == '+') return 62;
    if (c == '/') return 63;
    if (c == '=') return -1;

    [NSException raise:@"invalid BASE64 encoding" format:@"Invalid BASE64 encoding"];
    return 0;
}

- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
    return [NSString stringWithUTF8String:base64_encode(input, (size_t)length)];
}

- (NSString *)decodeBase64:(NSString *)input length:(NSInteger *)length
{
    size_t retLen;
    uint8_t *retStr = base64_decode([input UTF8String], &retLen);
    if (length)
        *length = (NSInteger)retLen;
    NSString *st = [[[NSString alloc] initWithBytes:retStr
                                             length:retLen
                                           encoding:NSUTF8StringEncoding] autorelease];
    free(retStr);    // If base64_decode returns dynamically allocated memory
    return st;
}

char* base64_encode(const void* buf, size_t size)
{
    static const char base64[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    char* str = (char*) malloc((size+3)*4/3 + 1);

    char* p = str;
    unsigned char* q = (unsigned char*) buf;
    size_t i = 0;

    while(i < size) {
        int c = q[i++];
        c *= 256;
        if (i < size) c += q[i];
        i++;

        c *= 256;
        if (i < size) c += q[i];
        i++;

        *p++ = base64[(c & 0x00fc0000) >> 18];
        *p++ = base64[(c & 0x0003f000) >> 12];

        if (i > size + 1)
            *p++ = '=';
        else
            *p++ = base64[(c & 0x00000fc0) >> 6];

        if (i > size)
            *p++ = '=';
        else
            *p++ = base64[c & 0x0000003f];
    }

    *p = 0;

    return str;
}

void* base64_decode(const char* s, size_t* data_len_ptr)
{
    size_t len = strlen(s);

    if (len % 4)
        [NSException raise:@"Invalid input in base64_decode" format:@"%d is an invalid length for an input string for BASE64 decoding", len];

    unsigned char* data = (unsigned char*) malloc(len/4*3);

    int n[4];
    unsigned char* q = (unsigned char*) data;

    for(const char*p=s; *p; )
    {
        n[0] = POS(*p++);
        n[1] = POS(*p++);
        n[2] = POS(*p++);
        n[3] = POS(*p++);

        if (n[0]==-1 || n[1]==-1)
            [NSException raise:@"Invalid input in base64_decode" format:@"Invalid BASE64 encoding"];

        if (n[2]==-1 && n[3]!=-1)
            [NSException raise:@"Invalid input in base64_decode" format:@"Invalid BASE64 encoding"];

        q[0] = (n[0] << 2) + (n[1] >> 4);
        if (n[2] != -1) q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
        if (n[3] != -1) q[2] = ((n[2] & 3) << 6) + n[3];
        q += 3;
    }

    // make sure that data_len_ptr is not null
    if (!data_len_ptr)
        [NSException raise:@"Invalid input in base64_decode" format:@"Invalid destination for output string length"];

    *data_len_ptr = q-data - (n[2]==-1) - (n[3]==-1);

    return data;
}

关于iPhone - 最终确定 Apple 的模糊 "VerificationController.m",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11633577/

相关文章:

iphone - iPhone 中的数据保护 API 未加密

iphone - 如何在 iOS 中重用代码(IBAction)?

iPhone 从服务器调用 python 函数

iphone - 塔防游戏中的多线程寻路

objective-c - GUI 的 NSThread 计时问题

ios - NSTimer 和服务器响应

ios - 无声推送通知

objective-c - 如何在 NSOutlineView 中制作带有渐变背景的标题行?

ios - 在 appdelegate 中设置初始 viewcontroller - swift

ios - 使用 iOS 版本 11.3.1 创建 iPad 模拟器