2011-09-20 15 views
0

我对objective-c比较陌生,而且这个问题让我难以忍受一段时间,甚至不关心使用编码。我正在处理音频文件,这些文件具有适用于它们的字节替换编码的基本字节格式。为了让这些文件在我的应用程序中工作,我必须对它们进行解码,但是在我的尝试中失败了。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左右,我想这个操作应该只需要几秒钟,但是我的代码显然很糟糕。

+0

这可能不是什么大不了的事情,但是FWIW你应该避免像这样公开发布真正的关键材料。我假设你已经将它随机化了:) –

+0

这个问题显然是逻辑的,你可以给方法重新添加一些注释,而不是将前面的代码添加到那个代码中?这样我可能会帮助你。 –

回答

0
  • 分配一个NSMutableData对象,并使用databuffer填写...只是减少NSData的长度,最后再从中提取此NSMutableData对象的字节可能是无用的:你大概可以访问的databuffer直接字节......和停止时你达到len-8?

  • 但更重要的是,你应该只反您key阵列一次,并且可能是由编译时做英寸由于key数组是一个静态的硬编码数组(我们称之为LUT或Look-Up Table),inverseKey数组在每次运行程序时都会有相同的值,因此对它进行硬编码也会加速您的代码太。

最后,我鼓励你使用仪器和它的“性能”和基准测试工具,它会帮助你发现需要这么长的时间来执行自己的代码的部分,d会非常使它方便您在代码中找到正确的位置来优化事物。