How to open a .p file in matlab ?

I have a file with extension .img whick is of around 70MB and a .p file to view them
I need to open / view the images using matlab.
Please help me out on this

Answers (3)

Walter Roberson
Walter Roberson on 10 Jan 2019
.p files are MATLAB functions with source code you cannot read. You can call them as functions by mentioning their base name without the .p extension
result = NameOfPFileHere('NameOfimgFileHere.img');

8 Comments

how we can change or modify .p file
You cannot read or modify a p-file. This is the purpose of this file format.
To change or modify EXAMPLE.p
filename = 'EXAMPLE.p';
[fid, msg] = fopen(filename, 'w+');
if fid < 0; error('Could not open "%s" because "%s"', filename, msg);
fseek(fid, 0, 'eof');
filelen = ftell(fid);
randompos = randi([0 filelen-1]);
randombyte = randi([0 255], 'uint8');
fseek(fid, randompos, 'bof');
fwrite(fid, randombyte);
fclose(fid);
This will write a random byte at a random position inside the .p file.
There is a fairly high probability that the .p file will not function afterwards. Ability to write to or change the .p file does not mean that you can do so sensibly
Do I have to change the name(Example) of file only in this code?
To use this code with your .p file, all you have to do is insert the name of the .p file in the line
filename = 'EXAMPLE.p';
such as
filename = 'PutTheNameOfTheFileToChangeHere.p';
IMPORTANT
Make sure you have a back-up of the file !! This code will almost certainly make the .p file unusable.
basename = 'Example';
td = tempdir();
addpath(td)
m_filename = fullfile(td, basename + ".m")
m_filename = "/tmp/Example.m"
fid = fopen(m_filename, 'w');
cleanMe = onCleanup(@() delete(m_filename));
fprintf(fid, 'function %s\ndisp("We are inside %s")\n;end\n', basename, m_filename);
fclose(fid)
ans = 0
clear(basename);
which(basename)
/tmp/Example.m
H = str2func(basename);
functions(H)
ans = struct with fields:
function: 'Example' type: 'simple' file: '/tmp/Example.m'
H();
We are inside /tmp/Example.m
pcode('-INPLACE', basename);
cleanMe2 = onCleanup(@() delete(p_filename));
clear(basename);
which(basename)
/tmp/Example.p
H = str2func(basename);
functions(H)
ans = struct with fields:
function: 'Example' type: 'simple' file: '/tmp/Example.p'
H();
We are inside /tmp/Example.m
p_filename = fullfile(td, basename + ".p")
p_filename = "/tmp/Example.p"
[fid, msg] = fopen(p_filename, 'r+');
if fid < 0; error('Could not open "%s" because "%s"', filename, msg); end
fseek(fid, 0, 'eof');
filelen = ftell(fid)
filelen = 103
randompos = randi([0 filelen-1]);
randombyte = randi([0 255], 'uint8');
fseek(fid, randompos, 'bof');
fwrite(fid, randombyte);
fclose(fid);
clear(basename)
which(basename)
/tmp/Example.p
H = str2func(basename);
functions(H)
ans = struct with fields:
function: 'Example' type: 'simple' file: '/tmp/Example.p'
H();
corrupt P-file.
The above demonstrates creating a .m file, making sure it executes properly. Then pcode'ing the .m file and verifying that the .p file has priority (notice that the file field of the functions() report changes), and verifying that the .p executes. Then modifying the .p file, and showing that the modified .p file does not execute.
The .p file was successfully modified -- but the result was something that could not be used.
@Sarib Malik: Before you get too confused: Walter's code does no do, what you try to do. It is not possible to manipulate the contents of a P-file in a meaningful way.
Suppose that you had a letter in English, and you found someone to translate the letter to Linear B, and then once in Linear B they converted it into Morse Code For Linear B, and gave you back a file of that Morse Code For Linear B.
Now, under that circumstance could you look at the contents of the file? Yes -- and you would see a bunch of dashes and dots. Could you modify the file? Yes -- you could change any of the dashes and dots or even insert more dashes and dots. But would the modified file be useful?? When you do not yourself know either Linear B nor Morse Code for Linear B ?
You can read .p code files. You can change .p code files. However, no tools are available to be able to deal with the meaning stored in the file.

Sign in to comment.

Jan
Jan on 6 Sep 2021
Edited: Jan on 6 Sep 2021
You cannot "open" a P-file in a meaningful way. Modifications are not possible.
If you want to change the code of a P-coded function, you have to ask the author to do this for you. Maybe he is willing to share the original M-file with you, maybe he is not.
P-coding includes an encryption. Of course any encryption can be hacked, but this would be illegal.
[EDITED] To clarify the last sentence:
  • According to the documentation, P-coding is an "obfuscation", not an "encryption".
  • Following Bruno's arguments in the comments below, it is legal in Europe to decompile P-code.
  • MathWorks does not want the algorithm of P-coding to made public and it would be a drawback for many programmers, who have tried to protect their codes. So even if it is legal, there would be good reasons not to publish corresponding instructions.
  • Legal, illegal, allowed, not wanted, useful for one person but a drawback for many - sigh.

4 Comments

Bruno Luong
Bruno Luong on 6 Sep 2021
Edited: Bruno Luong on 6 Sep 2021
It seems if you live in Europe, you are legally allowed to hack the encryption to correct error, from the source (EDIT fix wrong link)
Article 5 of the Directive provides exceptions to the exclusive rights above, talking of reverse engineering in general. Such rights are not subject to the authorisation of the author when these acts are necessary to enable the legitimate purchaser to use the computer program in a manner consistent with its intended purpose, including correcting errors.
Dear @Bruno Luong, The documentation of pcode contains this carefully formulated warning:
The pcode function obfuscates your program files and does not encrypt them, thus P-files should not be considered secure. P-coding files to protect your intellectual property is not recommended.
This means, that my wording "hacking" and "encryption" was not accurate. As a computer scientist I'd prefer a strong(er) encryption and the current P-format is not satisfying.
I am very curious and my interest in encryption methods is not compatible to MathWork's interest in keeping the details concealed. The author of the P-file mentioned in the question does not want the source code to be accessible. I respect this intent.
Bruno Luong
Bruno Luong on 6 Sep 2021
Edited: Bruno Luong on 6 Sep 2021
From the from the source :
"Article 4 of the Directive sets out the exclusive rights of the author, including the rights to translation, adaptation, arrangement, any other transformation and reproduction of the program. "
"Decompilation is a type of reverse engineering of software, namely, converting executable, computer-readable code (known as object code) into a human-readable code (thus recreating the source code through a higher-level programming language). "
Therefore transforming p-code (object code) to m-code (source code) would be considered and decompilation and perfectly legally allowed in EU for fixing error purpose, if this is Sarib Malik intention, even without author authorisation.
Jan
Jan on 6 Sep 2021
Edited: Jan on 6 Sep 2021
@Bruno Luong: Thanks for the explanations. Following your legal arguments, the Sarib Malik is allowed to decompile P-code inside Europe. It was not my intention to allege, that the he has any illegal intentions.
I'm not in the position to offer assistence for such a decompilation, maybe for legal reasons, maybe following the common sense, maybe it is my respect for the intention of MathWorks and many programmers who want to keep their codes hidden, maybe I like to avoid getting or making troubles beyond the chances to win the lawsuit, maybe I do not have running code. I would not hesitate to decompile P-files, if I'm the author of the source codes. But a revealing of the algorithm in a public forum would mean loosing a tool (with limited power) for all Matlab users. And, of course, the Matlab team would not like me anymore ;-)
I've added a corresponding explanation to my answer.

Sign in to comment.

KSSV
KSSV on 10 Jan 2019
You cannot open a .p file...you can only use it......

4 Comments

You can examine the bytes of the .p file but they probably will not mean much.
how we can change or modify .p file?
@Sarib Malik: Did you read the answers and comments? Let me repeat the message:
You cannot modify a P-file in a way, which allows to use it afterwards.
P-coding is the method in Matlab to hide the source code and to prevent further modifications. This is useful e.g. to keep an intellectual property private. For this purpose the P-files are encrypted. While you still can modify bytes of encrypted files how ever you want, you cannot expect that the result is still meaningful.
I tired to open the .p file in Notepad but its not showing but now the 'Open with' option for the .p file is changed from 'Unknown Application' to 'Notepad' and its creating a problem now. So, do you know how to restore back to 'Unknown Application' ?

Sign in to comment.

Products

Release

R2015a

Asked:

on 10 Jan 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!