displaying hex sent from socket connection - objective-c

im using LXsocket as my networking provider
i have setup a server and client for testing. i also have wireshark in place to see whats being sent
client:
socket = [[LXSocket alloc] init];
if ([socket connect: host port: port] == NO)
{
//cant connect!
[socket release];
NSLog(#"cannot create socket");
return FALSE;
}
//connect hex string being sent
const char connectByteArray[] = {
0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
};
//send bytes this works and the packets shown in wireshark reflect the byte array above
[socket sendBytes:connectByteArray length:sizeof(connectByteArray)];
//receive bytes
id *object = [socket readBytesWithLength:sizeof(connectByteArray)];
Server:
LXSocket* serverSocket = [[LXSocket alloc] init];
[serverSocket bindToPort: 1234];
[serverSocket listen: 5];
LXSocket *socket = [serverSocket accept];
const char connectByteArray[] = {
0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
};
[socket sendBytes:connectByteArray length:sizeof(connectByteArray)];
my problem is that i cannot get cocoa to export the byte array properly. i have tried many different format tags without any luck. how can i get this to log the proper byte array, ie. 01:00:07:7f:00:00:01:00:03:40 ????
NSLog(#"return data %X",object);
NSLog(#"return data %f",object);
NSLog(#"return data %e",object);
NSLog(#"return data %llu",object);
NSLog(#"return data %hu",object);
2011-08-24 08:24:44.507 Chameleon[5574:207] return data 4C053B0
2011-08-24 08:24:44.508 Chameleon[5574:207] return data 0.000000
2011-08-24 08:24:44.509 Chameleon[5574:207] return data 2.125934e-313
2011-08-24 08:24:44.510 Chameleon[5574:207] return data 43029386160
2011-08-24 08:24:44.511 Chameleon[5574:207] return data 21424
#import "LXSocket.h"
#import <sys/socket.h>
#import <sys/types.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>
#implementation LXSocket
+ (id)socket {
return [[[LXSocket alloc] init] autorelease];
}
- (id)initWithCSocket:(SOCKET)_socket {
if ((self = [super init])) {
m_socket = _socket;
}
return self;
}
- (id)init {
if ((self = [super init])) {
m_socket = socket(AF_INET, SOCK_STREAM, 0);
}
return self;
}
- (BOOL)bindToPort:(unsigned)port {
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(port);
if (bind(m_socket, (struct sockaddr *) &service, sizeof(service)) == -1) {
NSLog(#"bind() failed -- %s\n", strerror(errno));
close(m_socket);
return NO;
} else return YES;
}
- (BOOL)listen:(unsigned)limit {
if (listen(m_socket, limit) == -1) {
printf("listen(): Error listening on socket\n");
return NO;
} else return YES;
}
- (LXSocket*)accept {
SOCKET AcceptSocket;
while (YES) {
AcceptSocket = -1;
while (AcceptSocket == -1) {
AcceptSocket = accept(m_socket, NULL, NULL);
}
//printf("Server: Client Connected!\n");
//m_socket = AcceptSocket;
return [[LXSocket alloc] initWithCSocket: AcceptSocket];
}
return nil;
}
- (BOOL)connect:(NSString*)host port:(unsigned)port {
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr([host UTF8String]);
clientService.sin_port = htons(port);
if (connect(m_socket, (struct sockaddr *) &clientService, sizeof(clientService)) == -1) {
printf("Client: connect() - Failed to connect.\n");
return NO;
} else return YES;
}
- (BOOL)sendBytes:(const void*)bytes length:(unsigned)len {
int sent = send(m_socket, bytes, len, 0);
return sent == len;
}
- (void*)readBytesWithLength:(unsigned)len {
void* buffer = malloc(len);
int received = recv(m_socket, buffer, len, 0);
if (received <= 0) {
free(buffer);
return NULL;
}
if (received < len) {
unsigned remaining = len;
char* ptr = buffer;
do {
remaining -= received;
ptr += received;
received = recv(m_socket, ptr, remaining, 0);
if (received <= 0) {
free(buffer);
return NULL;
}
} while (received < remaining);
}
return buffer;
}
- (BOOL)sendData:(NSData*)data {
const void* ptr = [data bytes];
BOOL succeeded = NO;
if ([self sendInt: [data length]])
if ([self sendBytes: ptr length: [data length]]) succeeded = YES;
//free(ptr);
return succeeded;
}
- (NSData*)readData{
unsigned len = [self readInt];
if (len > 0) {
void* ptr = [self readBytesWithLength: len];
if (ptr != NULL) {
NSData* data = [NSData dataWithBytes: ptr length: len];
free(ptr);
return data;
}
}
return nil;
}
- (BOOL)sendObject:(id)object {
NSMutableData* data = [[NSMutableData alloc] init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
[archiver encodeRootObject: object];
[archiver finishEncoding];
BOOL succeeded = [self sendData: data];
[archiver release];
[data release];
return succeeded;
}
- (id)readObject {
NSData* data = [self readData];
if (data != nil) {
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
id object = [[[unarchiver decodeObject] retain] autorelease];
[unarchiver release];
return object;
}
return nil;
}
- (BOOL)sendInt:(int)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendDouble:(double)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendInt64:(long long)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (int)readInt {
void* buffer = [self readBytesWithLength: sizeof(int)];
if (buffer == NULL) return 0;
int n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (double)readDouble {
void* buffer = [self readBytesWithLength: sizeof(double)];
if (buffer == NULL) return 0;
double n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (long long)readInt64 {
void* buffer = [self readBytesWithLength: sizeof(long long)];
if (buffer == NULL) return 0;
long long n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (BOOL)sendShort:(short)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (short)readShort {
void* buffer = [self readBytesWithLength: sizeof(short)];
if (buffer == NULL) return 0;
short n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (BOOL)sendLong:(long)n {
return [self sendBytes: &n length: sizeof(n)];
}
- (BOOL)sendUnsingedLongLong:(unsigned long long)n {
return YES;//return [self sendBytes: &n length: sizeof(n)];
}
- (long)readLong {
void* buffer = [self readBytesWithLength: sizeof(long)];
if (buffer == NULL) return 0;
long n;
memcpy(&n, buffer, sizeof(n));
free(buffer);
return n;
}
- (void)sendString:(NSString*)string {
const char* s = [string UTF8String];
int len = strlen(s);
[self sendInt: len];
[self sendBytes: s length: len + 1];
}
- (NSString*)readString {
int len = [self readInt];
char* s = [self readBytesWithLength: len + 1];
NSString* string = [NSString stringWithUTF8String: s];
free(s);
return string;
}
- (NSString*)resolveHostName:(NSString*)hostName {
struct hostent* host = gethostbyname([hostName UTF8String]);
if (host == NULL) {
printf("Error resolving host name: %d\n", strerror(errno));
return nil;
} else {
struct in_addr* addr = (struct in_addr*) host->h_addr_list[0];
char* ip = inet_ntoa(*addr);
return [NSString stringWithUTF8String: ip];
}
}
- (void)dealloc {
close(m_socket);
[super dealloc];
}
#end
Some further results (remember original hex string being passed is 0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40)(0100077f000001000340):
id *object = [socket readBytesWithLength:10];
NSLog(#"return data %X",*object); = return data 7F070001
NSLog(#"return data %llu",*object); = return data 45080838145
double object = [socket readInt64];
NSLog(#"return data %X",*object); = return data F0700010
NSLog(#"return data %llu",*object); = return data 4823355235012444176 (hex: 0x42F00007F0700010)

You probably have to write a special function to print in this way because you need to loop through the characters:
- (NSString *)bytesToString:(char *)bytes length:(int)length {
// Each byte is 2 hex characters, plus we need a ':' between each byte
NSMutableString *result = [NSMutableString stringWithCapacity:2*length + length - 1];
for (int i = 0; i < length; i++) {
if (i < length - 1) {
[result appendFormat:#"%02X:", bytes[i]];
}
else {
[result appendFormat:#"%02X", bytes[i]];
}
}
return result; // Result is auto-released
}
With this function defined, you should be able to log as follows:
NSLog(#"return data %#",[self bytesToString:object length:sizeof(connectByteArray)]);

Related

CMSampleBuffer from Replay Kit after encoding to Mpeg-TS Video is normal, Audio is corrupted

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);
});
}

How to play pcm audio buffer from a socket server using audio unit circular buffer

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 have a issue on encryption and decryption using rc4 algorithm in ios

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;
}

How to Use BOOL in IBACTION IOS

i need to connect an imap email server with below given -(BOOL) method
how can i call this method in IBACTION when connect button clicked?
- (BOOL) connectToHost: (NSString*) hostname
{
socket_ = socket(AF_INET, SOCK_STREAM, 0);
if (socket_ < 0) {
NSLog(#"socket");
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(993);
struct hostent* host = gethostbyname([hostname UTF8String]);
unsigned int** ptr = (unsigned int **) host->h_addr_list;
while (*ptr != NULL) {
addr.sin_addr.s_addr = *(*ptr);
if (connect(socket_, (struct sockaddr *) &addr, sizeof(addr)) == 0) {
break;
}
ptr++;
}
if (*ptr == NULL) {
NSLog(#"connect");
}
return [[self readLine] isEqualToString: #"* OK"];
}
... call it from another method?
- (IBAction) connectButtonClicked:(id)sender {
[self connectToHost:#"Your host name goes here, variable _or_ constant! :D"];
}

How do I extract IP address and port number from NSData

- (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"