GP-797 Corrected python script info parse - ignore certification header

if present
This commit is contained in:
ghidra1 2021-03-23 13:38:14 -04:00
parent c6de9dc493
commit 0f235d029c
4 changed files with 88 additions and 13 deletions

View file

@ -148,4 +148,31 @@ public abstract class GhidraScriptProvider
return scriptName; return scriptName;
} }
/**
* Return the start of certification header line if this file type is
* subject to certification.
* @return start of certification header or null if not supported
*/
protected String getCertifyHeaderStart() {
return null;
}
/**
* Return the prefix for each certification header bofy line if
* this file is subject to certification
* @return certification heaber body prefix or null if not supported
*/
protected String getCertificationBodyPrefix() {
return null;
}
/**
* Return the end of certification header line if this file type is
* subject to certification.
* @return end of certification header or null if not supported
*/
protected String getCertifyHeaderEnd() {
return null;
}
} }

View file

@ -164,6 +164,16 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return "//"; return "//";
} }
@Override
protected String getCertifyHeaderStart() {
return "/* ###";
}
@Override
protected String getCertifyHeaderEnd() {
return "*/";
}
/** /**
* *
* Fix script name for search in script directories, such as Java package parts in the name and inner class names. * Fix script name for search in script directories, such as Java package parts in the name and inner class names.

View file

@ -20,7 +20,6 @@ import static ghidra.util.HTMLUtilities.*;
import java.io.*; import java.io.*;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Pattern;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
@ -47,9 +46,6 @@ public class ScriptInfo {
static final String AT_MENUPATH = "@menupath"; static final String AT_MENUPATH = "@menupath";
static final String AT_TOOLBAR = "@toolbar"; static final String AT_TOOLBAR = "@toolbar";
private static final Pattern DOCUMENTATION_START = Pattern.compile("/\\*");
private static final Pattern DOCUMENTATION_END = Pattern.compile("\\*/");
// omit from METADATA to avoid pre-populating in new scripts // omit from METADATA to avoid pre-populating in new scripts
private static final String AT_IMPORTPACKAGE = "@importpackage"; private static final String AT_IMPORTPACKAGE = "@importpackage";
@ -186,6 +182,16 @@ public class ScriptInfo {
init(); init();
String commentPrefix = provider.getCommentCharacter();
// Note that skipping certification header presumes that the header
// is intact with an appropriate start and end
String certifyHeaderStart = provider.getCertifyHeaderStart();
String certifyHeaderEnd = provider.getCertifyHeaderEnd();
String certifyHeaderBodyPrefix = provider.getCertificationBodyPrefix();
boolean allowCertifyHeader = (certifyHeaderStart != null);
boolean skipCertifyHeader = false;
BufferedReader reader = null; BufferedReader reader = null;
try { try {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
@ -197,16 +203,34 @@ public class ScriptInfo {
break; break;
} }
if (DOCUMENTATION_START.matcher(line).find()) { if (allowCertifyHeader) {
while (line != null && !DOCUMENTATION_END.matcher(line).find()) { // Skip past certification header if found
line = reader.readLine(); if (skipCertifyHeader) {
String trimLine = line.trim();
if (trimLine.startsWith(certifyHeaderEnd)) {
allowCertifyHeader = false;
skipCertifyHeader = false;
continue;
}
if (certifyHeaderBodyPrefix == null ||
trimLine.startsWith(certifyHeaderBodyPrefix)) {
continue; // skip certification header body
}
// broken certification header - unexpected line
Msg.error(this,
"Script contains invalid certification header: " + getName());
allowCertifyHeader = false;
skipCertifyHeader = false;
}
else if (line.startsWith(certifyHeaderStart)) {
skipCertifyHeader = true;
continue;
} }
continue;
} }
String commentPrefix = provider.getCommentCharacter();
if (line.startsWith(commentPrefix)) { if (line.startsWith(commentPrefix)) {
allowCertifyHeader = false;
line = line.substring(commentPrefix.length()).trim(); line = line.substring(commentPrefix.length()).trim();
if (line.startsWith("@")) { if (line.startsWith("@")) {

View file

@ -1,6 +1,5 @@
/* ### /* ###
* IP: GHIDRA * IP: GHIDRA
* REVIEWED: YES
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,12 +15,12 @@
*/ */
package ghidra.python; package ghidra.python;
import java.io.*;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
import ghidra.app.script.GhidraScript; import ghidra.app.script.GhidraScript;
import ghidra.app.script.GhidraScriptProvider; import ghidra.app.script.GhidraScriptProvider;
import java.io.*;
public class PythonScriptProvider extends GhidraScriptProvider { public class PythonScriptProvider extends GhidraScriptProvider {
@Override @Override
@ -39,6 +38,21 @@ public class PythonScriptProvider extends GhidraScriptProvider {
return "#"; return "#";
} }
@Override
protected String getCertifyHeaderStart() {
return "## ###";
}
@Override
protected String getCertifyHeaderEnd() {
return "##";
}
@Override
protected String getCertificationBodyPrefix() {
return "#";
}
@Override @Override
public String getDescription() { public String getDescription() {
return "Python"; return "Python";