pcode - Create content-obscured, executable files - MATLAB (original) (raw)
Create content-obscured, executable files
Syntax
Description
pcode([item](#btbo5ft-1-fun))
obfuscates the code in a .m
file or folder on the search path and produces P-code files with the extension .p
. For example, ifitem
is a .m
file namedmytest.m
, then the resulting file ismytest.p
. If item
is a folder, then all script or function files in that folder are obfuscated and saved in the current folder. A P-code file takes precedence over the corresponding .m
file for execution, even after modifications to the .m
file.
Note
Security Considerations: Thepcode
function produces MATLAB® program files in a proprietary, obfuscated code format. Consider combining multiple approaches to protect sensitive code or data. For more information, see Security Considerations to Protect Your Source Code.
pcode([item](#btbo5ft-1-fun),"-R2022a")
creates the P-code files using a more complex obfuscation algorithm. Files obfuscated in this way run only in MATLAB R2022a and later.
pcode([item](#btbo5ft-1-fun),"-R2007b")
creates the P-code files using the default, legacy algorithm.
pcode([item1,item2,...,itemN](#mw%5F8bbef2bd-12b3-423c-9e26-041de30d86fc))
creates P-code files from each .m
file or folder specified in a comma-separated list.
pcode(___,"-inplace")
creates the P-code files in the same folders as the inputs. Specify"-inplace"
after any of the input argument combinations in the previous syntaxes.
If the source file resides within a namespace or class folder, thenpcode
creates the same namespace or class structure to house the resulting P-code files.
Examples
Create P-Code File from a Single File
In a file named myfunc.m
in your current folder, define a function that returns the square root of a cubic polynomial.
function y = myfunc(x) y = sqrt(x.^3 + x.^2 + x + 1); end
Create a P-code file from myfunc.m
.
Confirm that MATLAB uses the P-code file when you callmyfunc
.
a = myfunc(3); which myfunc
_c:\myMATLABfiles_\myfunc.p
Create P-Code File Using More Complex Obfuscation Algorithm
In a file named myfunc.m
in your current folder, define a function that returns the square root of a cubic polynomial.
function y = myfunc(x) y = sqrt(x.^3 + x.^2 + x + 1); end
Create a P-code file from myfunc.m
using a more complex obfuscation algorithm. Files obfuscated in this way run only in MATLAB R2022a and later.
Confirm that MATLAB uses the P-code file when you callmyfunc
.
a = myfunc(3); which myfunc
_c:\myMATLABfiles_\myfunc.p
Create P-Code Files From Multiple Files
Convert selected files from the sparfun
folder to P-code files.
Create a temporary folder and define an existing path to .m
files.
tmp = tempname; mkdir(tmp) cd(tmp) filename = fullfile(matlabroot,"toolbox","matlab","sparfun","spr*.m");
Create the P-code files.
. .. sprand.p sprandn.p sprandsym.p sprank.p
The temporary folder now contains encoded P-code files.
Create P-Code Files for Class Files
Generate P-code files from input files that are part of a class. (You can apply the same procedure to files that are part of a namespace.)
Define classfolder
as an existing class folder that contains .m files.
classfolder = fullfile(cd,"@myClass")
classfolder = "/tmp/Bdoc24b_2855429_3621852/tpda1e2d8f/matlab-ex48377006/@myClass"
Create a temporary folder. This folder has no class structure at this time.
tmp = tempname; mkdir(tmp) cd(tmp) dir(tmp)
Create a P-code file for every .m
file in the path classfolder
. Because the input files are part of a class, MATLAB creates a folder structure so that the output file belongs to the same class.
pcode(classfolder) dir(tmp)
The P-code file resides in the same folder structure.
Create P-Code Files In Place
Generate P-code files in the same folder as the input files.
Copy several .m
files to a temporary folder.
filename = fullfile(matlabroot,"toolbox","matlab","sparfun","spr*.m"); tmp = tempname; mkdir(tmp) copyfile(filename,tmp) dir(tmp)
. .. sprand.m sprandn.m sprandsym.m sprank.m
Create P-code files in the same folder as the original .m
files.
pcode(tmp,"-inplace") dir(tmp)
. .. sprand.m sprand.p sprandn.m sprandn.p sprandsym.m sprandsym.p sprank.m sprank.p
Input Arguments
item
— .m
file or folder to obfuscate
character vector | string scalar
.m
file or folder to obfuscate, specified as a character vector or string scalar.
- An input argument that does not have a file extension and is not the name of a folder must be a function on the MATLAB path or in the current folder.
- When using the wildcard character
*
,pcode
ignores all files without a.m
extension. - The
pcode
function does not support live scripts or functions (.mlx
). - If
item
resides within a namespace or class folder, thenpcode
creates the same namespace or class structure to house the resulting P-code files.
item1,item2,...,itemN
— List of .m
files or folders to obfuscate
character vectors | string scalars
List of .m
files or folders to obfuscate, specified as a comma-separated list of character vectors or string scalars. The list can include a mix of files and folders.
More About
Creating P-Code Files from Related Files
In addition to your program, you can obfuscate other functions and scripts that your program is dependent upon. To determine the files required to run your program, use the matlab.codetools.requiredFilesAndProducts function.
Obfuscating Code
P-code files are an obfuscated, execute-only form of MATLAB code. You cannot open a P-code file in the MATLAB Editor or Live Editor.
Version History
Introduced before R2006a
R2022a: Encode with enhanced obfuscation
You can use the option "-R2022a", which creates P-code files using a more complex obfuscation algorithm. Files created with this option run only in MATLAB R2022a and later.
R2007b: Obfuscation algorithm redesigned
The pcode
algorithm was redesigned in MATLAB R2007b. If a P-code file was generated prior to R2007b, then it will not run in R2015b or later. Files generated in R2007b or later do not run in R2007a or earlier.