-
-
Save jverkoey/2985830 to your computer and use it in GitHub Desktop.
const char* cstr = [string UTF8String]; | |
void* anon = mmap(0, sizeof(char) * (string.length + 1), PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, 0, 0); | |
if (anon == MAP_FAILED) { | |
NSLog(@"Failed to map memory."); | |
return; | |
} | |
strcpy(anon, cstr); | |
// How do I get a file descriptor for use here? | |
markdownin = fdopen(fd, "r"); |
- Use
shm_open
to obtain file descriptor. - Use file descriptor from
shm_open
inmmap
. - Use
fdopen
to obtainFILE
pointer from file descriptor.
Here's the latest. Still not working sadly and I'm not sure what the culprit is.
const char* path = "/string1524";
int stringSize = sizeof(char) * (string.length + 1);
int fd = shm_open(path, O_RDWR | O_CREAT);
ftruncate(fd, stringSize);
void* anon = mmap(0, stringSize, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
if (anon == MAP_FAILED) {
NSLog(@"Failed to map memory.");
}
strcpy(anon, cstr);
// This returns a FILE*, but when trying to read from it I get garbage data.
markdownin = fdopen(fd, "r");
Unfortunately I also in result had no success in making it work on OS X / iOS.
However I found alternative solution which even isn't an ugly hack – http://developer.apple.com/library/ios/#documentation/System/Conceptual/ManPages_iPhoneOS/man3/fropen.3.html
You can just specify your function to read stream.
There is example here https://redmine.openinfosecfoundation.org/attachments/105/0001-fmemopen-wrapper-added-fix-compilation-problems-on-m.patch
I used this code linked to by poTomek on twitter and it worked beautifully:
https://github.com/ingenuitas/python-tesseract/blob/master/fmemopen.c
Looks very similar to the example given in your links :)
http://jverkoey.github.com/fmemopen/
Thanks gang!
As this stands I'm able to map the memory, but I don't have a file descriptor that I can use between mmap and f*open methods so that I can create a FILE pointer.