I am relatively new to objective-c, and this problem has had me stumped for some time to the point where I don't even care to use encoding. I am working with audio files that have a basic form of byte for byte replacement encoding applied to them. In order for these files to work in my application, I must decode them, but have been unsuccessful at my attempts.
To decode the files, I was supplied a key that looks like the following:
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
};
The file has a custom header and some appended data at the end which I must ignore, so I do the following after opening an encoded file and put into a byte array:
[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];
I am able to access bytes like the following:
UInt8 firstByte = audioBytes[0];
UInt8 secondByte = audioBytes[1];
etc...
My attempt at decoding the data looks something like the following:
Byte *decodedData;
NSMutableData *audioDataToPlay = [[[NSMutableData alloc] init] autorelease];
UInt8 currentByte;
for(int x=0; x<[audioData length]; x++){
currentByte = audioBytes[x];
Byte *bytes = (Byte*) ¤tByte;
decodedData = [self unreplace:bytes];
//Hopefully unencoded data...
[audioDataToPlay appendBytes:decodedData length:sizeof(decodedData)];
}
Unreplace function looks like this:
+(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;
}
I'm sure this code has some major problems. It was my attempt at porting C# code to Objective-C. It seems like the bytes are being replaced, but it is extremely slow. It gets to around 100,000+ bytes after 10 minutes and will eventually crash due to running out of memory. I know the malloc needs to be freed at some point. Each file ranges from around 3MB to 10MB, and I'd imagine the operation should only take a few seconds, but my code is obviously bad.
Allocating an
NSMutableData
object and fill it withdatabuffer
… just to reduce the NSData length and finally then extract the bytes from thisNSMutableData
object is probably useless: you probably can access the bytes ofdatabuffer
directly… and stop when you reach len-8?But more important, you should only inverse your
key
array once, and probably do in by compile time. As thekey
array is a static, hardcoded array (we call it a LUT or Look-Up Table for what matters), the inverseKey array will always have the same values each time you run your program, so hardcoding it too will accelerate your code too.
Finally, I encourage you to use Instruments and its "Performance" and benchmarking tools, it will help you finding the portion of your code that takes all that time to execute itself, an d it will make it very easy for you to spot the right place in the code to optimize things.
精彩评论