Technical Articles
How to store a binary file as function module
Many information is stored in different files with different formats. This information is often important for the customer and there is the question, how to transport this to a customer. For this case I code a program, called BinFile2ABAP, which offers this possibility. With BinFile2ABAP you can store each file in an ABAP function module.
It converts the file to an ABAP source code as a function module, form or report. With the call the file is saved on the presentation server and executed. BinFile2ABAP can also create base64 encoded JSON and PowerShell files from any file too. You can use this JSON file in the context of your SAPUI5 development, e.g. to simulate backend databinding, via OData and the SAP Gateway, as local databinding.
With this kind of information you can extend your possibilities. You can integrate each type of file you want in your ABAP program, e.g. Adobe Flash and Portable Document Format (PDF), Compiled HTML Help (CHM), all image, sound and video formats etc.
BinFile2ABAP is free and you can download it from here.
The process to generate ABAP code is very easy. Choose the binary file you want, press the button Create Code and save the file to disc. Transfer the code in your ABAP editor via clipboard or upload – that’s all.
You can choose the line width, 16, 32, 64 or 125 bytes, this means, 32, 64, 128 or 250 characters in a line. It is dependence from the file size, because the ABAP interpreter allows max. 65535 lines to concatenate. With 125 bytes and 65535 lines you get a file size limit from 8’191’875 bytes. It is possible to create an ABAP source with more lines, but you can not compile it.
The process to generate JSON code is also very easy. Choose the binary file you want, press the button Create Code and save the file to clipboard or disc – that’s all. You can find an example how to use it here.
2018/04/02: Update is available
- Change variable arrangements in the ABAP sources.
- Add the possibility to create PowerShell file.
- Update to a new compiler version
2016/11/20: Update is available
- Change variable names in the ABAP sources to another prefix.
- Update to a new compiler version.
2016/04/01: Update is available
- Add now a few information as comment in the source about the file, like name, size, last modification date and time and MD5 hash
- Add now Authority-Check stub in the ABAP sources
2015/11/22: Update is available
- Now two versions are available, x86 and x64.
- Checked with Windows 10 x64.
2015/02/15: Update is available
- Add the possibility to create JSON file with base64 encoded content.
- Add the possibility to create function modules with an export interface.
- Add the possibility to create function modules in ABAP in Eclipse (AiE) style.
- New GUI design with tabs.
- Delete the possibility to create ABAP code with more than 65535 lines, because it has no practical significance.
Hi Stefan
İt seems quite interesting, so this converts everything into a function module
And then loads it into SAP for use, is that the way it works?
So basically makes an ABAP version of the library
And also is this for Microsoft only or for all types of libraries
e.g image, video, etc..
Hello Solen,
thanks for your reply.
Cheers
Stefan
Hi Stefan,
Very informative and useful document.
Regards,
Ankita
Hello Ankita,
thanks for the reply.
You find an additional document below, which describes the using of BinFile2ABAP on a practical example. The document is outdated, but it is a good example.
Cheers
Stefan
Hi, the URL is broken.
Hello Sander,
thanks for your reply and your hint. You find the PDF document now inside the post.
Cheers
Stefan
Very interesting Stefen.
Thanks,
Syam
Hi Stefan,
when should you use this, instead of uploading a file to the sap file server? Or load it as Mime object into SAP?
Best,
Sander
Hello Sander,
thanks for your reply.
BinFile2ABAP use another way, it stores the binary file direct as ABAP code.
It is just a different way than the MIME-Repository.E.g. you could use it, if you want to deliver only ABAP code and no other transport objects. It has in comparsion to the MIME-Repository no other important advantages - it is an alternative.
Cheers
Stefan
Taking an arbitrary file and storing it in an area where it's executable sounds quite dodgy to me. I would advise against this at all time as it may lead to security issues.
Please use the mime repository or BDS instead for the use cases mentioned.
Hello Jan,
thank you for your reply.
I don't understand your security reasons, because you can store a binary file in the MIME repository or in a function module but what is the difference for the content of the binary file? I think nothing. The container of the binary file is from the perspective of security no difference.
Cheers
Stefan
Hi Stefan,
The difference between Mime repo and a function module is that the first is that Mime space is not-executable, while a function module is. Because you are injecting something that should not be executable into executable space, it might eventually be used as an attack vector.
Hope this clarifies?
Cheers,
Jan
Hello Jan,
thank you for your reply.
I must confess that I still not understand your arguments.
You can execute the content of the MIME repository also easily, here an example:
Let us determine, it is no difference where the content is stored. As I show in my example above, you can use the same method with the MIME repositoy.
I think it is a question of the use case to decide if an "injection" of something should be execute on the frontend server or not. You can't say generally that should not be executable.
So last but not least in my eyes your argument is that an eventually attacker can use his chance with the command cl_gui_frontend_services=>execute, indenpendently where the content is stored. But this risk is always present, it is no special problem of this method.
Let us look to one of my use cases:
I embed a PDF in a function module and if the user press a button on the screen the file is downloaded and executed with the local installed Acrobat viewer.
In this function module is my user name, the creation date and the date of the last modification date stored. If something is suspicious with the content of the file the local security system of the frontend server will detect and prevent this activities. The chance to attack a frontend server on this way is considered optimistic near by null, also the backend server - and why should I attack a backend server where my function module is executed, it is going easier.
Therefore, it is all SAP standard, no tricks and I don't understand your security reason arguments furthermore.
Cheers
Stefan
Thank you for your very elaborate reply Stefan.
It's just that pushing data into a location where it's directly executable is simply not a practice I could recommend. So I'm afraid we're going to have to agree to disagree on the topic
Have a great weekend!
Hello Jan,
thank you for your reply.
This argument I can understand well, but it was important for me to emphasize that the container of the binary data does not matter.
Cheers
Stefan
Few concerns:
Please don't be offended, but, I feel that this mechanism is an overkill, when there is a standard and straightforward solution available.
Thanks,
Juwin
Hello Juwin,
thank your for your reply.
To your concerns:
To your questions:
I am not offended about your arguments and your opinion, you have a different perspective and environment. This forum is a platform to exchange these thoughts, it is important for me to present my ideas and it is important for you and each other here to say, hey, good idea but...
Last but not least I want to say, BinFile2ABAP creates standard code and it uses no tricks. However I must confess that the method is really not mainstream.
Cheers
Stefan
I am not sure if I understand what this program does. Can you please explain?
It takes any file and generate ABAP source code (of function module) with hardcoded binary data of that file? What next? Source code is to be manualy copied to SAP?
And why hardcode binary data to source code? Why not use database (Z* table with binary data column)?
Hello Tomas,
thank you for your reply.
The program does exactly what you describe. It takes any file and generates an ABAP source code with the binary data of the file. The generated code must be manually copied to SAP, via clipboard or import. With this way you have only one access to ABAP to get the data, no access to the database or anything else.
Cheers
Stefan