【问题标题】:Objective-C Decoding Byte Array Based On KeyObjective-C 基于键的解码字节数组
【发布时间】:2011-11-21 09:24:58
【问题描述】:

我对objective-c 比较陌生,这个问题让我困惑了一段时间,以至于我什至不关心使用编码。我正在处理应用了字节替换编码的基本字节形式的音频文件。为了让这些文件在我的应用程序中工作,我必须对它们进行解码,但我的尝试没有成功。

为了解码文件,我得到了一个如下所示的密钥:

static const short key[256] = {
      2,  93,   6, 134,   8, 200,  79, 236, 155, 242,
      4, 241,  59, 143, 153, 196, 118,  20, 105, 109,
    209, 149,  74, 177, 201,  81,  17,  62,  27, 183,
    103,  90, 220,   1, 224, 211, 207,  34,  24, 182,
     58,  91, 204,  73, 214,  65, 131,  75,  33,  80,
     50, 146, 139,  86, 254, 219,  76, 138, 179,  96,
    184, 166, 212, 178,  16, 193, 186, 150,  22,  40,
     19, 151, 120,  35,  26, 218, 221, 133, 127, 190,
    245, 225, 164,  47, 124,  95,  21, 255, 123, 237,
    162,  97, 115, 234,  46, 206, 185, 216,  85, 240,
     66, 229,  13,  43, 102, 154, 169,  92, 253,  54,
     44, 192, 126,  61, 247,  56, 194, 167,  10,  36,
    248, 223, 238, 121, 217,  14, 137, 147,  49, 152,
    141,  23,  25, 114, 246, 168,  55,  57, 181,   5,
    215,  60,  87, 100, 210, 163, 122, 113,  28,  68,
     53, 144, 135, 180,  38,  12, 157,  31, 202, 112,
    161, 239,  29,  98, 233, 230, 125, 111, 227,  52,
    189, 174,  30,  78,  88,  39, 213, 232,   7,  41,
    199,  15, 208,  94, 106, 145,  64, 191,  71, 132,
    173,   3, 205, 171, 101, 110, 172, 244, 249, 188,
    130, 235, 222, 195, 230,  18,  32, 250,  72, 170,
    198, 156, 251,  63, 117, 136, 252,  70, 158,  82,
    142, 176, 175, 107,  45, 119, 116,  83,  89,  69,
     42, 231,   0, 128,  37, 228,  84,  48,  99, 148,
    197, 243, 226, 129,  77,  67, 187, 108, 159,  11,
    165, 160,  51,   9, 104, 140
};

该文件有一个自定义标题和一些我必须忽略的附加数据,因此我在打开编码文件并放入字节数组后执行以下操作:

[file seekToFileOffset:128];
databuffer = [file readDataToEndOfFile];

NSMutableData *audioData =
      [[[NSMutableData alloc] initWithData:databuffer] autorelease];
[audioData setLength:[audioData length]-8];

//Put encoded data into byte array
Byte *audioBytes = (Byte *)malloc([audioData length]);
[audioData getBytes:audioBytes];

我可以访问如下字节:

UInt8 firstByte = audioBytes[0];
UInt8 secondByte = audioBytes[1];
etc...

我对数据的解码尝试如下所示:

Byte *decodedData;
NSMutableData *audioDataToPlay = [[[NSMutableData alloc] init] autorelease];
UInt8 currentByte;            

for(int x=0; x<[audioData length]; x++){
    currentByte = audioBytes[x];
    Byte *bytes = (Byte*) &currentByte;

    decodedData = [self unreplace:bytes];

    //Hopefully unencoded data...
    [audioDataToPlay appendBytes:decodedData length:sizeof(decodedData)];
}

Unreplace 函数如下所示:

+(Byte *)unreplace:(Byte *)bytes{
    int size = sizeof(key);

    Byte *inverseKey = (Byte *)malloc(size);

    for(int position = 0; position < size; position++)
    {
        for(int index=0; index < size; index++)
        {
            if(key[index] == position)
            {
                inverseKey[position] = index;
                break;
            }
        }
    }

    size = sizeof(bytes);
    Byte *unreplaced = (Byte *)malloc(size);

    for(int index=0; index <size; index++)
    {
        unreplaced[index] = inverseKey[bytes[index]];
    }
    return unreplaced;
}

我确信这段代码存在一些重大问题。这是我将 C# 代码移植到 Objective-C 的尝试。似乎正在替换字节,但速度非常慢。它在 10 分钟后达到大约 100,000+ 字节,最终由于内存不足而崩溃。我知道 malloc 需要在某个时候被释放。每个文件的大小从 3MB 到 10MB 不等,我想这个操作应该只需要几秒钟,但我的代码显然很糟糕。

【问题讨论】:

  • 这里可能没什么大不了的,但是 FWIW 您可能应该避免像这样公开发布实际的关键材料。我假设你已经将它随机化了 :)
  • 问题显然是逻辑问题,您能否在 unreplace 方法和之前的代码块中添加一些 cmets ?这样我也许能帮到你。

标签: objective-c ios encoding bytearray decoding


【解决方案1】:
  • 分配一个NSMutableData 对象并用databuffer 填充它...只是为了减少NSData 长度,最后然后从这个NSMutableData 对象中提取字节可能没用:您可能可以访问@ 的字节987654324@直接……到len-8就停下来?

  • 但更重要的是,您应该只反转一次 key 数组,并且可能在编译时完成。由于key 数组是一个静态的硬编码数组(我们称其为 LUT 或查找表),因此每次运行程序时 inverseKey 数组将始终具有相同的值,因此硬编码也将加速你的代码也是。

最后,我鼓励您使用 Instruments 及其“性能”和基准测试工具,它将帮助您找到需要花费所有时间来执行自身的代码部分,并且它会让您非常轻松地在代码中找到合适的地方来优化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2011-06-08
    相关资源
    最近更新 更多