0

I am writing an Mp3 player. I am downloading mp3 files from server. Files are encrypted. Encrypt method of my customer is very easy. They just insert a single bit per X byte. Like 323 in code. But decryption process take very long time. And CPU usage is huge? Why? And what can I do?

_mappedData = [[NSMutableData dataWithMappedContentsOfFile:_cachedPath] mutableCopy];

  Singleton *singleton = [Singleton sharedSingleton];

  NSLog(@"Decryption started");

  if(singleton.playerMode == PLAYERMODELISTEN)
  {
     int i = 323;
     while (i < [_mappedData length]) {
         [_mappedData replaceBytesInRange:NSMakeRange(i, 1) withBytes:NULL length:0];
         //CFDataDeleteBytes((CFMutableDataRef)_mappedData, CFRangeMake(i, 1000));
         i += 323;
     }
  }


  [General dismissGlobalHUD];

  NSLog(@"Decryption completed");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Can Ürek
  • 641
  • 12
  • 24
  • Have you done any profiling to see what's taking all the time? – Wain Mar 29 '15 at 19:02
  • I try use instruments and catch some informations. But i can't found anything with that way. – Can Ürek Mar 29 '15 at 19:06
  • iOS or OSX? How large is the file? Why call `mutableCopy` on `NSMutableData`? – zaph Mar 29 '15 at 19:29
  • The point of mapping a file is not to read the entire file in at once or perhaps even all of it. But you are making changes to the entire file so the changes willl create read and writes to the file. File I/O is expensive time-wise. – zaph Mar 29 '15 at 19:32
  • iOS project. And when i was not use mutableCopy project is crashing. EXC_BAD_ACCESS – Can Ürek Mar 29 '15 at 19:33
  • So, why `NSMutableData dataWithMappedContentsOfFile` since this does not work. I suspect that `mutableCopy` makes a copy of the file in memory, probably not what you want. Perhaps you really want `modifiableDataWithMappedContentsOfFile:`. You need to use Instruments to see what is happening, time and memory allocations. – zaph Mar 29 '15 at 19:38
  • Have you any suggestion to solve that issue? – Can Ürek Mar 29 '15 at 19:40
  • I have no any memory usage problem in that issue. So modifiableDataWithMappedContentsOfFile not change anything for me. – Can Ürek Mar 29 '15 at 19:44
  • Also i need and i want makes a copy of the file in memory. Because i need decrypt file just when playing. – Can Ürek Mar 29 '15 at 21:24

1 Answers1

1

Finally i solve that issue myself.

NSMutableData *decryptedData = [[NSMutableData alloc] init];

  NSUInteger length = [_mappedData length];
  NSUInteger chunkSize = 500;
  NSUInteger offset = 0;

  do {
      NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
      NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[_mappedData bytes] + offset
                                           length:thisChunkSize
                                     freeWhenDone:NO];
      offset += (thisChunkSize + 1);

      [decryptedData appendData:chunk];

  } while (offset < length);

  _mappedData = decryptedData;

That's my solution. Working very fast.

Can Ürek
  • 641
  • 12
  • 24