I'm facing a problem with write function not working properly if it placed inside a loop with out sleep.what i'm trying to achieve is to transfer files from local machine to a remote FTP server the following code works fine but the write function not transferring the bytes properly it transfer only of the half bytes, it works fine with sleep blocks the loop.
struct sockaddr_in
{
int16_t sin_family;
uint16_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr
{
uint32_t s_addr;
};
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
int isocket = socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("some ip goes here");
servaddr.sin_port = htons(21);
char buf[1024];
int MAX_LENGTH = 1024;
char readBuf[MAX_LENGTH];
long cmd;
int res = connect(isocket, (struct sockaddr_in *)&servaddr, sizeof(servaddr));
if(res < 0) {
NSLog(#"problem connecting to server");
}
recv(isocket, (void *)readBuf,MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf);
NSLog(#"Issuing command");
strcpy(buf, "USER foo\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0); //send username
recv(isocket, (void *)readBuf,MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "PASS *******\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//send password
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "CWD /httpdocs/testing\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//send directory
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "TYPE I\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//set transfer type
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
strcpy(buf, "PASV\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//set transfer mode to passive
recv(isocket, (void *)readBuf, MAX_LENGTH, 0);
NSLog(#"response: %s",readBuf); //read response
NSString *pasvResponse = [NSString stringWithFormat:#"%s",readBuf];
strcpy(buf, "STOR mmov.jpeg\r\n");
cmd = send(isocket, (void *)buf, strlen(buf), 0);//start tranfering files
pasvResponse = [pasvResponse stringByReplacingOccurrencesOfString:#"227 Entering Passive Mode (" withString:#""];
pasvResponse = [pasvResponse stringByReplacingOccurrencesOfString:#")." withString:#""];
pasvResponse = [pasvResponse stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *matches = [pasvResponse componentsSeparatedByString:#","];
NSString *ip = nil;
int port = 0;
if([matches count] > 2) {
ip = [NSString stringWithFormat:#"%#.%#.%#.%#",matches[0],matches[1],matches[2],matches[3]];
port = ([matches[4] intValue] * 256) + ([matches[5] intValue]);
}
if(port != 0 && ip != nil) {
struct sockaddr_in servaddr_pasv;
memset(&servaddr_pasv, 0, sizeof(servaddr_pasv));
int isocket_pasv = socket(AF_INET,SOCK_STREAM,0);
servaddr_pasv.sin_family = AF_INET;
servaddr_pasv.sin_addr.s_addr = inet_addr([ip UTF8String]);
servaddr_pasv.sin_port = htons(port);
int res_pasv = connect(isocket_pasv, (struct sockaddr_in *)&servaddr_pasv, sizeof(servaddr_pasv));
if(res_pasv < 0) {
NSLog(#"problem connecting to server passv");
}
NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:#"/Users/myMac/Desktop/limitation.jpeg"];
[input open];
int i= 0;
while(1) {
if([input hasBytesAvailable]) {
i++;
uint8_t buffer[1024];
long res = [input read:buffer maxLength:1024];
NSLog(#"Bytes Read: %ld",res);
if(res != 0) {
long bytesSent = write(isocket_pasv, buffer, sizeof(buffer));
NSLog(#"%d Bytes Transfered: %ld",i,bytesSent);
// sleep(1);
}
if(res == 0) {
break;
}
}
}
[input close];
NSLog(#"bytes write completed");
close(isocket_pasv);
}
close(isocket);
Any help Thanks in advance.
You can't guarantee you can offload your entire buffer in one go. You'll have to loop over write until all your data is transferred. You should also check for errors returned by write, it's likely to be the place where you catch all kinds of network related events as well as more typical O/S write errors.
I'm guessing it's the write call that doesn't send all you ask it to send? That's actually not unexpected. You simply have to send the remaining data in a new call to write.
If you want to write all data, use something like this:
int write_all(int fd, const void *buffer, const size_t bufsize)
{
size_t to_write = bufsize;
const void *ptr = buffer;
while (to_write > 0)
{
ssize_t written = write(fd, ptr, to_write);
if (written < 0)
return -1;
to_write -= written;
ptr = ((char *) ptr) + written;
}
return bufsize;
}
Caveat: I write the following from memory, haven't tested it.
Related
I am trying to stream AAC encoded audio data received as CMSampleBuffer. AudioConverterFillComplexBuffer returns 0 status code.
But after passing this data to my FFMPEG HLSWriter audio is not correctly saved (short truncated signals).
Below is the sample code.
static OSStatus inInputDataProc(AudioConverterRef inAudioConverter,
UInt32 *ioNumberDataPackets,
AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void *inUserData)
{
KFAACEncoder *encoder = (__bridge KFAACEncoder *)(inUserData);
UInt32 requestedPackets = *ioNumberDataPackets;
if(requestedPackets > encoder.cycledBuffer.size / 2)
{
//NSLog(#"PCM buffer isn't full enough!");
*ioNumberDataPackets = 0;
return -1;
}
static size_t staticBuffSize = 4096;
static void* staticBuff = nil;
if(!staticBuff)
{
staticBuff = malloc(staticBuffSize);
}
size_t outputBytesSize = requestedPackets * 2;
[encoder.cycledBuffer popToBuffer:staticBuff bytes: outputBytesSize];
ioData->mBuffers[0].mData = staticBuff;
ioData->mBuffers[0].mDataByteSize = (int)outputBytesSize;
*ioNumberDataPackets = ioData->mBuffers[0].mDataByteSize / 2;
return noErr;
}
- (void) encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
CFRetain(sampleBuffer);
dispatch_async(self.encoderQueue,
^{
if (!_audioConverter)
{
[self setupAACEncoderFromSampleBuffer:sampleBuffer];
}
CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CFRetain(blockBuffer);
size_t pcmBufferSize = 0;
void* pcmBuffer = nil;
OSStatus status = CMBlockBufferGetDataPointer(blockBuffer, 0, NULL, &pcmBufferSize, &pcmBuffer);
[_cycledBuffer push:pcmBuffer size:pcmBufferSize];
NSError *error = nil;
if (status != kCMBlockBufferNoErr)
{
error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
}
memset(_aacBuffer, 0, _aacBufferSize);
AudioBufferList outAudioBufferList = {0};
outAudioBufferList.mNumberBuffers = 1;
outAudioBufferList.mBuffers[0].mNumberChannels = 1;
outAudioBufferList.mBuffers[0].mDataByteSize = _aacBufferSize;
outAudioBufferList.mBuffers[0].mData = _aacBuffer;
AudioStreamPacketDescription *outPacketDescription = NULL;
UInt32 ioOutputDataPacketSize = 1;
status = AudioConverterFillComplexBuffer(_audioConverter,
inInputDataProc,
(__bridge void *)(self),
&ioOutputDataPacketSize,
&outAudioBufferList,
NULL);
NSData *data = nil;
if (status == 0)
{
NSData *rawAAC = [NSData dataWithBytes:outAudioBufferList.mBuffers[0].mData length:outAudioBufferList.mBuffers[0].mDataByteSize];
if (_addADTSHeader) {
NSData *adtsHeader = [self adtsDataForPacketLength:rawAAC.length];
NSMutableData *fullData = [NSMutableData dataWithData:adtsHeader];
[fullData appendData:rawAAC];
data = fullData;
} else {
data = rawAAC;
}
} else {
error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
}
if (self.delegate) {
KFFrame *frame = [[KFFrame alloc] initWithData:data pts:pts];
NSLog(#"Bytes of data %lu", (unsigned long)data.length);
dispatch_async(self.callbackQueue, ^{
[self.delegate encoder:self encodedFrame:frame];
});
}
CFRelease(sampleBuffer);
CFRelease(blockBuffer);
});
}
I hope someone can help me. I am new to Objective-c and OSX and I am trying to play audio data I am receiving via socket into my audio queue. I found out this link https://stackoverflow.com/a/30318859/4274654 which in away address my issue with circular buffer.
However when I try to run my project it returns
It returns an error (OSStatus) -10865. That is why the code logs " Error enabling AudioUnit output bus".
status = AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &one, sizeof(one));
Here is my code:
Test.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import "TPCircularBuffer.h"
#interface Test : Communicator
#property (nonatomic) AudioComponentInstance audioUnit;
#property (nonatomic) TPCircularBuffer circularBuffer;
-(TPCircularBuffer *) outputShouldUseCircularBuffer;
-(void) start;
#end
Test.m
#import "Test.h"
#define kOutputBus 0
#define kInputBus 1
#implementation Test{
BOOL stopped;
}
static OSStatus OutputRenderCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData){
Test *output = (__bridge Test*)inRefCon;
TPCircularBuffer *circularBuffer = [output outputShouldUseCircularBuffer];
if( !circularBuffer ){
SInt32 *left = (SInt32*)ioData->mBuffers[0].mData;
for(int i = 0; i < inNumberFrames; i++ ){
left[ i ] = 0.0f;
}
return noErr;
};
int32_t bytesToCopy = ioData->mBuffers[0].mDataByteSize;
SInt16* outputBuffer = ioData->mBuffers[0].mData;
uint32_t availableBytes;
SInt16 *sourceBuffer = TPCircularBufferTail(circularBuffer, &availableBytes);
int32_t amount = MIN(bytesToCopy,availableBytes);
memcpy(outputBuffer, sourceBuffer, amount);
TPCircularBufferConsume(circularBuffer,amount);
return noErr;
}
-(void) start
{
[self circularBuffer:&_circularBuffer withSize:24576*5];
stopped = NO;
[self setupAudioUnit];
// [super setup:#"http://localhost" port:5321];
}
-(void) setupAudioUnit
{
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
OSStatus status;
status = AudioComponentInstanceNew(comp, &_audioUnit);
if(status != noErr)
{
NSLog(#"Error creating AudioUnit instance");
}
// Enable input and output on AURemoteIO
// Input is enabled on the input scope of the input element
// Output is enabled on the output scope of the output element
UInt32 one = 1;
status = AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &one, sizeof(one));
if(status != noErr)
{
NSLog(#"Error enableling AudioUnit output bus");
}
// Explicitly set the input and output client formats
// sample rate = 44100, num channels = 1, format = 16 bit int point
AudioStreamBasicDescription audioFormat = [self getAudioDescription];
status = AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));
if(status != noErr)
{
NSLog(#"Error setting audio format");
}
AURenderCallbackStruct renderCallback;
renderCallback.inputProc = OutputRenderCallback;
renderCallback.inputProcRefCon = (__bridge void *)(self);
status = AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &renderCallback, sizeof(renderCallback));
if(status != noErr)
{
NSLog(#"Error setting rendering callback");
}
// Initialize the AURemoteIO instance
status = AudioUnitInitialize(_audioUnit);
if(status != noErr)
{
NSLog(#"Error initializing audio unit");
}
}
- (AudioStreamBasicDescription)getAudioDescription {
AudioStreamBasicDescription audioDescription = {0};
audioDescription.mFormatID = kAudioFormatLinearPCM;
audioDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
audioDescription.mChannelsPerFrame = 1;
audioDescription.mBytesPerPacket = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mFramesPerPacket = 1;
audioDescription.mBytesPerFrame = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mBitsPerChannel = 8 * sizeof(SInt16);
audioDescription.mSampleRate = 44100.0;
return audioDescription;
}
-(void)circularBuffer:(TPCircularBuffer *)circularBuffer withSize:(int)size {
TPCircularBufferInit(circularBuffer,size);
}
-(void)appendDataToCircularBuffer:(TPCircularBuffer*)circularBuffer
fromAudioBufferList:(AudioBufferList*)audioBufferList {
TPCircularBufferProduceBytes(circularBuffer,
audioBufferList->mBuffers[0].mData,
audioBufferList->mBuffers[0].mDataByteSize);
}
-(void)freeCircularBuffer:(TPCircularBuffer *)circularBuffer {
TPCircularBufferClear(circularBuffer);
TPCircularBufferCleanup(circularBuffer);
}
-(TPCircularBuffer *) outputShouldUseCircularBuffer
{
return &_circularBuffer;
}
-(void) stop
{
OSStatus status = AudioOutputUnitStop(_audioUnit);
if(status != noErr)
{
NSLog(#"Error stopping audio unit");
}
TPCircularBufferClear(&_circularBuffer);
_audioUnit = nil;
stopped = YES;
}
-(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event{
switch (event) {
case NSStreamEventOpenCompleted:
NSLog(#"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (stream == [super inputStream]) {
NSLog(#"NSStreamEventHasBytesAvailable");
uint8_t buffer[1024];
NSUInteger len;
while ([[super inputStream] hasBytesAvailable]) {
len = [[super inputStream] read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
//converting buffer to byte data
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
//NSLog(#"server overideddddd said: %#", output);
}
NSData *data0 = [[NSData alloc] initWithBytes:buffer length:len];
if (nil != data0) {
SInt16* byteData = (SInt16*)malloc(len);
memcpy(byteData, [data0 bytes], len);
double sum = 0.0;
for(int i = 0; i < len/2; i++) {
sum += byteData[i] * byteData[i];
}
Byte* soundData = (Byte*)malloc(len);
memcpy(soundData, [data0 bytes], len);
if(soundData)
{
AudioBufferList *theDataBuffer = (AudioBufferList*) malloc(sizeof(AudioBufferList) *1);
theDataBuffer->mNumberBuffers = 1;
theDataBuffer->mBuffers[0].mDataByteSize = (UInt32)len;
theDataBuffer->mBuffers[0].mNumberChannels = 1;
theDataBuffer->mBuffers[0].mData = (SInt16*)soundData;
NSLog(#"soundData here");
[self appendDataToCircularBuffer:&_circularBuffer fromAudioBufferList:theDataBuffer];
}
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(#"Can't connect to server");
break;
case NSStreamEventEndEncountered:
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
break;
default:
NSLog(#"Unknown event");
}
[super stream:stream handleEvent:event];
}
#end
I would highly appreciate if there is any one with an example of playing buffers returned from a socket server into audio queue so that I can be able to listen to sound as it comes from the socket server.
Thanks
Your code seems to be asking for a kAudioUnitSubType_VoiceProcessingIO audio unit. But kAudioUnitSubType_RemoteIO would be a more suitable iOS audio unit for just playing buffers of audio samples.
Also, your code does not seem to first select an appropriate audio session category and activate it before playing audio. See Apple's documentation for doing this: https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html
I am using rc4 algorithm to encrypt and decrypt a video. I am taking 64 bytes and encrypting.
as the same way I am doing the decryption also but my video is not playing.Anyone have idea about this???
- (void)encryptOnFly:(UInt8*)buffer bytesRead:(NSInteger)bytesRead
{
for(long long i=0;i<bytesRead;i++)
{
[self push:buffer[i]];
if([self isFull])
{
unsigned char *buf11=[self retrieveArray];
unsigned char buf1[64];
for(int j=0;j<64;j++)
{
buf1[j]=*buf11++;
}
NSData *data = [NSData dataWithBytes:buf1 length:sizeof(buf1)];
aesCrypt = [data RC4EncryptDecryptWithKey:#"ABCD" operation:kCCEncrypt];
// aesCrypt=[data EncryptAES:#"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf1[j]=*bytes++;
}
[[self fileDownloadOutputStream] write:buf1 maxLength:64];
[self empty];
}
}
}
this is the code to handle a file which is divisible by 64....
switch (type) {
case kCFStreamEventHasBytesAvailable:
[self handleBytesAvailable];
break;
case kCFStreamEventEndEncountered:
NSLog(#"REACHED :: -- > kCFStreamEventEndEncountered");
if (isEncryptionRequired)
{
if(pointer>-1)
{
remBytes=pointer+1;
int tmp=64-remBytes;
for(int k=0;k<tmp;k++)
{
if (pointer!=62)
{
if(![self isFull])
[self push:0];
}
else
if(![self isFull])
[self push:remBytes];
if ([self isFull])
{
unsigned char *buf11= [self retrieveArray];
unsigned char buf1[64];
for(int j=0;j<64;j++)
{
buf1[j]=*buf11++;
}
NSData *data = [NSData dataWithBytes:buf1 length:sizeof(buf1)];
aesCrypt = [data RC4EncryptDecryptWithKey:#"ABCD" operation:kCCEncrypt];
// aesCrypt=[data EncryptAES:#"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf1[j]=*bytes++;
}
[[self fileDownloadOutputStream] write:buf1 maxLength:16];
[self empty];
}}
}
else
{
unsigned char extrabuf=0;
[[self fileDownloadOutputStream] write:extrabuf maxLength:1];
///add
}
}
else
{
[[self fileDownloadOutputStream] write:buf maxLength:16];
}
[self handleStreamComplete];
break;
case kCFStreamEventErrorOccurred:
[self handleStreamError];
break;
default:
break;
}
this is the method to decrypt a file......
- (void)decryptFile:(NSString *)OMfileUrl
{
// NSLog(#"omurl::%#",OMfileUrl);
#try
{
__block NSURL *movieURL;
isOffline = YES;
{
const char *srcfile = [OMfileUrl cStringUsingEncoding:NSUTF8StringEncoding];
source = fopen(srcfile, "rb");
filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"assetforplay.mp4"];
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.filePath = filePath;
filePathCharArr = [filePath cStringUsingEncoding:NSUTF8StringEncoding];
remove([filePath cStringUsingEncoding:NSUTF8StringEncoding]);
if (source)
{
destination = fopen(filePathCharArr, "wb");
cancelDecryption = NO;
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
unsigned char temp[64];
long long totalBytes=0;
fseek(source, 0L, SEEK_END);
long long sz = ftell(source);
fseek(source, 0L, SEEK_SET);
int percentage=0,prevPercentage=0;
int xx=sz%64;
percentageLabel.hidden = NO;
if (xx==0)
{
NSData *data;
while (totalBytes<=(sz-64))
{
if(!cancelDecryption)
{
percentage =((double)totalBytes/(double)sz)*100;
if (percentage!=prevPercentage)
{
prevPercentage=percentage;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[percentageLabel setText:[NSString stringWithFormat:#"%i%%",percentage]];
}];
}
fread(buffer,1, 64, source);
// memcpy( buf, buffer, 16);
// memcpy( keyoffline, [delegate secretKey], 16 + 0 * 8);
// aes_set_key( &ctx, keyoffline, 128 + 0 * 64);
// aes_decrypt( &ctx, buf, buf );
data = [NSData dataWithBytes:buffer length:64];
aesCrypt = [data RC4EncryptDecryptWithKey:#"ABCD" operation:kCCDecrypt];
// aesCrypt=[data DecryptAES:#"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf[j]=*bytes++;
}
fwrite(buf, 1, 64, destination);
totalBytes=totalBytes+64;
}
else
{
percentageLabel.text=#"";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err;
if (![fileManager removeItemAtPath:filePath error:&err])
{
NSLog(#"Unable to delete the essence. Reason:%#",[err description]);
}
break;
}
}
fseek(source, -64, SEEK_END);
sz = ftell(source);
// fread(buffer, 1, 16, source);
// memcpy( buf, buffer, 16);
// memcpy( keyoffline, [delegate secretKey], 16 + 0 * 8);
// aes_set_key( &ctx, keyoffline, 128 + 0 * 64);
// aes_decrypt( &ctx, buf, buf );
data = [NSData dataWithBytes:buffer length:64];
aesCrypt = [data RC4EncryptDecryptWithKey:#"ABCD" operation:kCCDecrypt];
// aesCrypt=[data DecryptAES:#"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf[j]=*bytes++;
}
int buf1 = buf[63];
for(int j=0;j<buf1;j++)
{
temp[j] = buf[j];
}
fwrite(temp, 1, buf1, destination);
}
else
{
NSData *data;
while (totalBytes<=(sz-1))
{
if(!cancelDecryption)
{
percentage =((double)totalBytes/(double)sz)*100;
if (percentage!=prevPercentage)
{
prevPercentage=percentage;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[percentageLabel setText:[NSString stringWithFormat:#"%i%%",percentage]];
}];
}
fread(buffer,1, 64, source);
// memcpy( buf, buffer, 16);
// memcpy( keyoffline, [delegate secretKey], 16 + 0 * 8);
// aes_set_key( &ctx, keyoffline, 128 + 0 * 64);
// aes_decrypt( &ctx, buf, buf );
data = [NSData dataWithBytes:buffer length:64];
aesCrypt = [data RC4EncryptDecryptWithKey:#"ABCD" operation:kCCDecrypt];
// aesCrypt=[data DecryptAES:#"akey"];
const uint8_t *bytes = (const uint8_t*)[aesCrypt bytes];
for(int j=0;j<63;j++)
{
buf[j]=*bytes++;
}
fwrite(buf, 1, 64, destination);
totalBytes=totalBytes+64;
}
else
{
percentageLabel.text=#"";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err;
if (![fileManager removeItemAtPath:filePath error:&err])
{
NSLog(#"Unable to delete the essence. Reason:%#",[err description]);
}
break;
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
percentageLabel.text=#"";
if (!cancelDecryption)
{
percentageLabel.hidden = YES;
movieURL = [NSURL fileURLWithPath:filePath];
[self.mediaPanelViewCtrl setURL:movieURL];
}
});
});
}
}
}
#catch (NSException *exception)
{
NSLog(#"Exception in decrypt file::%#",[exception description]);
}
}
this is the method to do encryption and decryption using RC4....
- (NSData*)RC4EncryptDecryptWithKey:(NSString *)key operation:(CCOperation)operation
{
// convert to C string..
int keySize = [key length];
char keyPtr[keySize];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr
maxLength:sizeof(keyPtr)
encoding:NSUTF8StringEncoding];
// encode/decode
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength;
void *buffer = malloc(bufferSize);
size_t numBytesOut = 0;
CCCryptorStatus cryptStatus = CCCrypt(operation,
kCCAlgorithmRC4,
kCCOptionECBMode,
keyPtr,
kCCKeySizeMinRC4,
NULL,
[self bytes],
dataLength,
buffer,
bufferSize,
&numBytesOut);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer
length:numBytesOut
freeWhenDone:YES];
}
free(buffer);
return nil;
}
Can anyone provide me a tutorial / documentation on compressing and decompressing strings in memory in objective-c (for iPhone development).
I am looking at Objective-Zip, but it only seems to work by writing the compressed data to a file.
give you an example
#interface NSString (Gzip)
- (NSData *)compress;
#end
#implementation NSString (Gzip)
- (NSData *)compress
{
size_t len = [self length];
size_t bufLen = (len + 12) * 1.001;
u_char *buf = (u_char *)malloc(bufLen);
if (buf == NULL) {
NSLog(#"malloc error");
return nil;
}
int err = compress(buf, &bufLen, (u_char *)[[self dataUsingEncoding:NSUTF8StringEncoding] bytes], len);
if (err != Z_OK) {
NSLog(#"compress error");
free(buf);
return nil;
}
NSData *rtn = [[[NSData alloc] initWithBytes:buf length:bufLen] autorelease];
free(buf);
return rtn;
}
#end
- (void)readData{
int err;
int sock;
struct sockaddr_storage addr;
socklen_t addrLen;
uint8_t buffer[65536];
ssize_t bytesRead;
sock = CFSocketGetNative(self->_cfSocket);
addrLen = sizeof(addr);
bytesRead = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &addr, &addrLen);
if (bytesRead < 0) {
err = errno;
} else if (bytesRead == 0) {
err = EPIPE;
} else {
NSData * dataObj;
NSData * addrObj;
err = 0;
dataObj = [NSData dataWithBytes:buffer length:bytesRead];
addrObj = [NSData dataWithBytes:&addr length:addrLen ];
// Tell the delegate about the data.
NSLog(#"receive data");
if ( (self.delegate != nil) && [self.delegate respondsToSelector:#selector(socket:didReceiveData:fromAddress:)] ) {
[self.delegate socket:self didReceiveData:dataObj fromAddress:addrObj];
}
}
if (err != 0) {
NSLog(#"Did Receive Error");
}
}
addrObj is NSData, How do I extract IP address and port number from addrObj?
I think you don't need to use a NSData object to extract the address. I use the following code to extract the IP address and port from which I read:
- (void)readData
{
int sock = CFSocketGetNative(self.cfSocket);
struct sockaddr_storage address;
socklen_t len = sizeof(address);
uint8_t buffer[65536];
ssize_t bytesRead = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *) &address, &len);
int error = (bytesRead < 0) ? errno : 0;
NSLog(#"%zi bytes read from %s:%d...", bytesRead,
inet_ntoa(((struct sockaddr_in*)&address)->sin_addr),
((struct sockaddr_in*)&address)->sin_port);
...
}
Sample output:
2011-05-04 10:41:57.051,-[myClass readData],5 bytes read from 10.112.15.81:37259...
2011-05-04 10:41:57.052,-[myClass readData] read:
"Hello"