gnuplot with iOS - ios

Does anyone here have experiences of using gnuplot with iOS? I want to develop a scientific computing app on iOS devices and want to use gnuplot as the plotting engine. Are there any good tutorials from which I can start with?

I have the same general question, a quick google search led me to the following app, which seems to use gnuplot.
https://apps.apple.com/us/app/icas-workstation-class-scientific/id394637176
I followed the trail to their website:
http://alsoftiphone.com/iCAS/
I contacted them about it, and I will follow up if/when I get a response.
The response:
Hello,
As you've aptly pointed out, there are some complications when embedding gnuplot in an app, particularly if you intend to dynamically create and dispose of it. That is, gnuplot (as of v4.4.0 which is what I used) doesn't explicitly release some memory that it allocates presumably because it assumes that it will be released when it is quit, which would be the case when it is used as a standalone application. This will of course result in memory leaks. Similarly, gnuplot doesn't explicitly close its output file descriptor (which is set to stdout as far as I know). And it also doesn't clear multiplot mode when the main function exits which is problematic because the next time you call gnuplot in an embedded situation, some global variables will reflect multiplot mode if called thereafter.
Fortunately for you, I've already identified them so you won't have to hunt them down like I did. Unfortunately, though, I didn't create a library for my projects but here are the relevant changes that you'll need to make to the gnuplot source code.
plot.c line #615, at the end of the else block for the "if (interactive && term != 0)" conditional block of the main() function (which you will also want to change to some appropriate entry function name):
// Free replot_line
if (replot_line != NULL)
{
free(replot_line);
replot_line = NULL;
}
plot.c line #680, before "return exit_status" at the end of the main() function:
// Free replot_line if it was allocated
if (replot_line != NULL)
free(replot_line);
// Clear multiplot mode, if it was active
if (multiplot)
term_end_multiplot();
// Close current file
if (gpoutfile)
fclose(gpoutfile);
The other issue is that gnuplot is written to use stdin and stdout so for my apps I replaced them with my own appropriate file descriptors to serve as the interface to/from gnuplot. That will be implementation specific, so I won't list my own particular changes but you'll basically need to look through the gnuplot source for instances of stdin and stdout and replace them as appropriate for your needs.
You'll also want to #define NO_GIH in config.h.
Other than that, you'll probably need to hard code the appropriate gnuplot terminal type for your app (I used SVG in my apps).
I hope this helps.
Best Regards,
Antonio Lagana

Related

How to get the path of a library from its handle (macOS / iOS)?

I have a handle to a dynamic library (from using dlopen()). Regardless of why, I don't have access to what the path supplied to dlopen() was, but need the path for another function. Thus, I need to be able to acquire the path to the library using its handle.
I've tried using dladdr(), as I have in other parts of my app, but on macOS / iOS you aren't able to use it to find the path of a library using the handle to the library it only works with a handle to a symbol in the library. I could try adding a "locator symbol" to the library, and accomplish things this way, but I'd prefer not to.
I also tried dlinfo() with RTLD_DI_LINKMAP, but this is apparently not available on macOS / iOS.
I'm surprised at how little information there is out there for this. Many of the solutions out there were not available on macOS / iOS. Others still, were only about getting the path of the current executable, and had nothing to do with the handle.
After a TON of searching, I finally came across some resources saying to iterate through all the loaded images using _dyld_image_count() and _dyld_get_image_name(). I initially decided against this, as it just didn't seem like an unreasonably slow way of doing things.
Eventually, I decided to go with iterating over all the loaded images, as it was the only actual solution I had come across. I googled for examples, and couldn't find any tutorials on the topic. However, I did come across an open source C++ library that implemented the functionality (found here).
I translated it to normal C, and got rid of some excess things (such as stripping the handle). During the testing process, I noticed that the library I wanted was always last in the list (my best guess is that it stores them in the order they were loaded, and since mine isn't a system library, it will be one of the last ones loaded). This guaranteed a slow performance (in reference to the computer - to a human it'd still be nearly instantaneous). So, I did a clever optimization that started the search at the end of the list, rather than the beginning.
This is the final code for my solution:
// NOT a thread safe way of doing things
NSString *pathFromHandle(void* handle)
{
// Since we know the image we want will always be near the end of the list, start there and go backwards
for (uint32_t i = (_dyld_image_count() - 1); i >= 0; i--)
{
const char* image_name = _dyld_get_image_name(i);
// Why dlopen doesn't effect _dyld stuff: if an image is already loaded, it returns the existing handle.
void* probe_handle = dlopen(image_name, RTLD_LAZY);
dlclose(probe_handle);
if (handle == probe_handle)
{
return [NSString stringWithUTF8String:image_name];
}
}
return NULL;
}
It's important to note that this solution is not thread safe, as _dyld_image_count() and _dyld_get_image_name() are inherently not thread safe. This means that any other thread could load / unload an image and have a negative impact on our search.
Additionally, the resource I used questioned why dlopen didn't have an effect on _dyld_image_count(). This is because if an image is already loaded, dlopen does not load a new instance of the image, but rather returns the existing handle for it.

What is the recommended way to make & load a library?

I want to make a small "library" to be used by my future maxima scripts, but I am not quite sure on how to proceed (I use wxMaxima). Maxima's documentation covers the save(), load() and loadFile() functions, yet does not provide examples. Therefore, I am not sure whether I am using the proper/best way or not. My current solution, which is based on this post, stores my library in the *.lisp format.
As a simple example, let's say that my library defines the cosSin(x) function. I open a new session and define this function as
(%i0) cosSin(x) := cos(x) * sin(x);
I then save it to a lisp file located in the /tmp/ directory.
(%i1) save("/tmp/lib.lisp");
I then open a new instance of maxima and load the library
(%i0) loadfile("/tmp/lib.lisp");
The cosSin(x) is now defined and can be called
(%i1) cosSin(%pi/4)
(%o1) 1/2
However, I noticed that a substantial number of the libraries shipped with maxima are of *.mac format: the /usr/share/maxima/5.37.2/share/ directory contains 428 *.mac files and 516 *.lisp files. Is it a better format? How would I generate such files?
More generally, what are the different ways a library can be saved and loaded? What is the recommended approach?
Usually people put the functions they need in a file name something.mac and then load("something.mac"); loads the functions into Maxima.
A file can contain any number of functions. A file can load other files, so if you have somethingA.mac and somethingB.mac, then you can have another file that just says load("somethingA.mac"); load("somethingB.mac");.
One can also create Lisp files and load them too, but it is not required to write functions in Lisp.
Unless you are specifically interested in writing Lisp functions, my advice is to write your functions in the Maxima language and put them in a file, using an ordinary text editor. Also, I recommend that you don't use save to save the functions to a file as Lisp code; just type the functions into a file, as Maxima code, with a plain text editor.
Take a look at the files in share to get a feeling for how other people have gone about it. I am looking right now at share/contrib/ggf.mac and I see it has a lengthy comment header describing its purpose -- such comments are always a good idea.
For principiants, like me,
Menu Edit:configure:Startup commands
Copy all the functions you have verified in the first box (this will write your wxmaxima-init.mac in the location indicated below)
Restart Wxmaxima.
Now you can access the functions whitout any load() command

Is it possible to edit and recompile an iOS Binary?

I have an application and posted to Cydia recently. It has been cracked by someone else and posted it in torrent sites. I have a binary checksum verification mechanism inside and they were able to create a new checksum file based on the changes they have made to the binary. They have edited two functions and decompiled it and posted it to torrents.
I saw that it's possible to see the actual implementation of functions and classes. But in order to edit the functions they have to find the address of that function and edit it via HEX EDITOR. I don’t want to make it "unhackable", but I really want to find out how they hack.
How can I edit a function in an iOS binary and re-compile it? For example I have a following method in one of my classes.
- (id) getSomething {
return #"Something";
}
I want to edit the return value of this function. Is that possible?
Usually, you don't "re-compile" it. Just feed the file to IDA, look for strings, function calls or whatever you are looking for and then use a hex editor or similar to edit the file on assembly level. In most cases it's enough to simply change a conditional jump into an unconditional jump or a nop (no operation). If you want to change return values, you have to put a little more effort into it, but in my experience you either edit the char sequence right inside the binary file, if it's specified as a constant or initial value - or you just write a completely new function and "copy" the assembler code of it into the original file. You just have to make sure your new function does not take more space than the original - or everything's getting a lot more complex.
I hope that's what you were asking for, otherwise just tell us which app you are talking about and we can look deeper into it :)

lua script error checking

Is it possible to check if a lua script contains errors without executing it? I have fallowing code:
if(luaL_loadbuffer(L, data, size, name))
{
fprintf (stderr, "%s", lua_tostring (L, -1));
lua_pop (L, 1);
}
if(lua_pcall(L, 0, 0, 0))
{
fprintf (stderr, "%s", lua_tostring (L, -1));
lua_pop (L, 1);
}
But if the script contains errors it passes first if and it is executed. I want to know if it contains errors when I load it, not when I execute it. Is this possible?
You can use the LUA Compiler. It will only compile your file to bytecode without executing it.
Your program will also have the advantage the run faster if it is compiled.
You can even use the -p option to only perform a syntax checking, according to the linked man page :
-p load files but do not generate any output file. Used mainly for syntax checking or testing precompiled chunks: corrupted files will probably generate errors when loaded. For a thourough integrity test, use -t.
(This was originally meant as a reply to the first comment to Krtek's question, but I ran out of space there and to be honest it works as an answer just fine.)
Functions are essentially values, and thus a named function is actually a variable of that name. Variables, by their very definition, can change as a script is executed. Hell, someone might accidentally redefine one of those functions. Is that bad? To sum my thoughts up: depending on the script, parameters passed and/or actual implementations of those pre-defined functions you speak of (one might unset itself or others, for example), it is not possible to guarantee things work unless you are willing to narrow down some of your demands. Lua is too dynamic for what you are looking for. :)
If you want a flawless test: create a dummy environment with all bells and whistles in place, and see if it crashes anywhere along the way (loading, executing, etc). This is basically a sort of unit test, and as such would be pretty heavy.
If you want a basic check to see if a script has a valid syntax: Krtek gave an answer for that already. I am quite sure (but not 100%) that the lua equivalent is to loadfile or loadstring, and the respective C equivalent is to try and lua_load() the code, each of which convert readable script to bytecode which you would already need to do before you could actually execute the code in your normal all-is-well usecase. (And if that contained function definitions, those would need to be executed later on for the code inside those to execute.)
However, these are the extent of your options with regards to pre-empting errors before they actually happen. Lua is a very dynamic language, and what is a great strength also makes for a weakness when you want to prove correctness. There are simply too many variables involved for a perfect solution.
In general it is not possible, as Lua is a dynamic language, and most of errors happen in runtime.
If you want to check for syntax errors, use luac -p option. I use it as a part of my pre-commit hook, for example.
Other common errors are triggering by misusing the global variables. You may analyze output of luac -l to catch these cases. See here: http://lua-users.org/wiki/DetectingUndefinedVariables.
If you want something more advanced, there are several more-or-less functional static analysis tools for Lua code. Start with LuaInspect.
In any case, you are advised to write unit tests instead of just relying on static code checks. Less pain, more gain.

Don't make me manually abort a LaTeX compile when there's an error

As suggested here, latexmk is a handy way to continually compile your document whenever the source changes. But often when you're working on a document you'll end up with errors and then latex will panic and wait for user input before continuing. That can get very annoying, especially recently when I hacked up something to compile latex directly from an etherpad document, which saves continuously as you type.
Is there a setting for latex or latexmk to make it just abort with an error message if it can't compile? Or, if necessary, how would I set up some kind of Expect script to auto-dismiss LaTeX's complaints?
(I had thought pdflatex's option -halt-on-error would do the trick but apparently not.)
Bonus question: Skim on Mac OSX is a nice pdf viewer that autorefreshes when the pdf changes (unlike Preview), except that whenever there's a latex error it makes you reconfirm that you want autorefreshing. Texniscope doesn't have this problem, but I had to ditch Texniscope for other reasons. Is there a way to make Skim always autorefresh, or is there another viewer that gets this right?
ADDED: Mini-tutorial on latexmk based on the answer to this question:
Get latexmk here: http://www.phys.psu.edu/~collins/software/latexmk-jcc/
Add the following to your ~/.latexmkrc file:
$pdflatex = 'pdflatex -interaction=nonstopmode';
(For OS X with Skim)
$pdf_previewer = "open -a /Applications/Skim.app";
While editing your source file, foo.tex, run the following in a terminal:
latexmk -pvc -pdf foo.tex
Use Skim or another realtime pdf viewer to view foo.pdf. For Skim, just look at the “Sync” tab in Skim’s preferences and set it up for your editor.
Voila! Hitting save on foo.tex will now cause foo.pdf to refresh without touching a thing.
With MikTeX, pdflatex has this command-line option:
-interaction=MODE Set the interaction mode; MODE must be one
of: batchmode, nonstopmode, scrollmode,
errorstopmode.
Edit suggested by #9999years:
Those values are equivalent to a set of LaTeX \commands that provide the same functionality.
From TeX usage tips:
The modes make TeX behave in the following way:
errorstopmode stops on all errors, whether they are about errors in the
source code or non-existent files.
scrollmode doesn't stop on errors in the source but requests input when a
more serious error like like a missing file occurs.
In the somewhat misnamed nonstopmode, TeX does not request input after
serious errors but stops altogether.
batchmode prevents all output in addition to that (intended for use in
automated scripts). In all cases, all errors are written to the log file
(yourtexfile.log).
You can also put \nonstopmode or \batchmode at the very beginning of your tex file; then it'll work with any TeX version, not just pdflatex. For more info on these and related commands see the very good reference on (raw) TeX commands by David Bausum. Especially the command from the debugging family could be of interest here.
Another possible hack is simply to use:
yes x | latexmk source.tex
You could always create an alias for 'yes x | latexmk' if you're going to use this option lots. The main advantage of this that I can see above the other suggestions is that it is very quick for when you occasionally want latexmk to behave like this.
Mehmet
There is also a \batchmode command may do the work.

Resources