Skip to content

Commit 8fbd171

Browse files
committed
GP-4285 Compressed SLEIGH
1 parent b380fd5 commit 8fbd171

File tree

207 files changed

+15220
-6042
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+15220
-6042
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MODULE FILE LICENSE: src/decompile/zlib zlib License

Ghidra/Features/Decompiler/buildNatives.gradle

+31-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ if (findProject(':Generic') == null) {
2626
* Define the "native build model" for building the decompiler executables.
2727
*/
2828
model {
29-
3029
// Define the source files that are compiled and linked to become the decompiler.
3130
// The decompiler source is a bit weird in that all the cpp and headers all live in
3231
// the same directory with other files that are not used by the decompiler.
@@ -174,6 +173,8 @@ model {
174173
include "slghpattern.cc"
175174
include "semantics.cc"
176175
include "context.cc"
176+
include "slaformat.cc"
177+
include "compression.cc"
177178
include "filemanage.cc"
178179
include "slgh_compile.cc"
179180

@@ -188,7 +189,27 @@ model {
188189
srcDir "src/decompile/cpp"
189190
}
190191
} // end cpp
192+
193+
if (isCurrentWindows()) {
194+
c {
195+
source {
196+
srcDir "src/decompile/zlib"
197+
include "*.c"
198+
}
199+
}
200+
}
191201
} // end sources (sleigh)
202+
203+
binaries {
204+
all{ b ->
205+
if (b.toolChain in Gcc || b.toolChain in Clang) {
206+
b.linker.args "-lz"
207+
}
208+
else {
209+
b.cppCompiler.define "LOCAL_ZLIB"
210+
}
211+
}
212+
} // end binaries.all (sleigh)
192213
} // end sleigh
193214

194215
} // end components
@@ -223,7 +244,15 @@ model {
223244
b.cppCompiler.define "WIN64"
224245
b.cppCompiler.define "_WIN64"
225246
}
226-
}
247+
}
248+
b.cCompiler.args "/W3"
249+
b.cCompiler.args "/O2"
250+
b.cCompiler.args "/Oy" // Omit frame pointer
251+
b.cCompiler.define "_CRT_SECURE_NO_DEPRECATE"
252+
b.cCompiler.define "_CRT_NONSTDC_NO_DEPRECATE"
253+
b.cCompiler.define "WIN64"
254+
b.cCompiler.define "ZLIB_WINAPI"
255+
b.cCompiler.define "NO_GZIP"
227256
}
228257
else if (b.toolChain in Clang) {
229258
b.cppCompiler.args "-std=c++11"

Ghidra/Features/Decompiler/certification.manifest

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
##MODULE IP: Modified Nuvola Icons - LGPL 2.1
55
##MODULE IP: Oxygen Icons - LGPL 3.0
66
##MODULE IP: Tango Icons - Public Domain
7+
##MODULE IP: zlib License
78
Module.manifest||GHIDRA||||END|
89
data/decompiler.theme.properties||GHIDRA||||END|
910
src/decompile/.cproject||GHIDRA||||END|
@@ -67,6 +68,7 @@ src/decompile/datatests/twodim.xml||GHIDRA||||END|
6768
src/decompile/datatests/union_datatype.xml||GHIDRA||||END|
6869
src/decompile/datatests/varcross.xml||GHIDRA||||END|
6970
src/decompile/datatests/wayoffarray.xml||GHIDRA||||END|
71+
src/decompile/zlib/README.txt||GHIDRA||||END|
7072
src/main/doc/commonprofile.xsl||GHIDRA||||END|
7173
src/main/doc/cspec.xml||GHIDRA||||END|
7274
src/main/doc/cspec_html.xsl||GHIDRA||||END|

Ghidra/Features/Decompiler/src/decompile/cpp/Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ YACC=bison
5050
# libraries
5151
#INCLUDES=-I$(BFDHOME)/include
5252
INCLUDES=
53-
BFDLIB=-lbfd -lz
53+
BFDLIB=-lbfd
5454

55-
LNK=
55+
LNK=-lz
5656

5757
# Source files
5858
ALL_SOURCE= $(wildcard *.cc)
@@ -87,7 +87,7 @@ DECCORE=capability architecture options graph cover block cast typeop database c
8787
printlanguage printc printjava memstate opbehavior paramid signature $(COREEXT_NAMES)
8888
# Files used for any project that use the sleigh decoder
8989
SLEIGH= sleigh pcodeparse pcodecompile sleighbase slghsymbol \
90-
slghpatexpress slghpattern semantics context filemanage
90+
slghpatexpress slghpattern semantics context slaformat compression filemanage
9191
# Additional files for the GHIDRA specific build
9292
GHIDRA= ghidra_arch inject_ghidra ghidra_translate loadimage_ghidra \
9393
typegrp_ghidra database_ghidra ghidra_context cpool_ghidra \
@@ -260,10 +260,10 @@ test: ghidra_test_dbg
260260
./ghidra_test_dbg
261261

262262
ghidra_dbg: $(GHIDRA_DBG_OBJS)
263-
$(CXX) $(DBG_CXXFLAGS) $(ADDITIONAL_FLAGS) $(MAKE_STATIC) $(ARCH_TYPE) -o ghidra_dbg $(GHIDRA_DBG_OBJS) $(LNK)
263+
$(CXX) $(DBG_CXXFLAGS) $(ADDITIONAL_FLAGS) $(MAKE_STATIC) $(ARCH_TYPE) -o ghidra_dbg $(GHIDRA_DBG_OBJS)
264264

265265
ghidra_opt: $(GHIDRA_OPT_OBJS)
266-
$(CXX) $(OPT_CXXFLAGS) $(ADDITIONAL_FLAGS) $(MAKE_STATIC) $(ARCH_TYPE) -o ghidra_opt $(GHIDRA_OPT_OBJS) $(LNK)
266+
$(CXX) $(OPT_CXXFLAGS) $(ADDITIONAL_FLAGS) $(MAKE_STATIC) $(ARCH_TYPE) -o ghidra_opt $(GHIDRA_OPT_OBJS)
267267

268268
sleigh_dbg: $(SLEIGH_DBG_OBJS)
269269
$(CXX) $(DBG_CXXFLAGS) $(ADDITIONAL_FLAGS) $(MAKE_STATIC) $(ARCH_TYPE) -o sleigh_dbg $(SLEIGH_DBG_OBJS) $(LNK)

Ghidra/Features/Decompiler/src/decompile/cpp/address.hh

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ public:
9898
void encode(Encoder &encoder) const; ///< Encode \b this to a stream
9999
void encode(Encoder &encoder,int4 size) const; ///< Encode \b this and a size to a stream
100100

101-
/// Restore an address from parsed XML
101+
/// Decode an address from a stream
102102
static Address decode(Decoder &decoder);
103103

104-
/// Restore an address and size from parsed XML
104+
/// Decode an address and size from a stream
105105
static Address decode(Decoder &decoder,int4 &size);
106106
};
107107

@@ -221,7 +221,7 @@ class RangeProperties {
221221
bool seenLast; ///< End of the range is actively specified
222222
public:
223223
RangeProperties(void) { first = 0; last = 0; isRegister = false; seenLast = false; }
224-
void decode(Decoder &decoder); ///< Restore \b this from an XML stream
224+
void decode(Decoder &decoder); ///< Decode \b this from a stream
225225
};
226226

227227
/// \brief A disjoint set of Ranges, possibly across multiple address spaces

Ghidra/Features/Decompiler/src/decompile/cpp/architecture.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public:
236236
void setPrototype(const PrototypePieces &pieces); ///< Set the prototype for a particular function
237237
void setPrintLanguage(const string &nm); ///< Establish a particular output language
238238
void globalify(void); ///< Mark \e all spaces as global
239-
void decodeFlowOverride(Decoder &decoder); ///< Set flow overrides from XML
239+
void decodeFlowOverride(Decoder &decoder); ///< Decode flow overrides from a stream
240240
virtual ~Architecture(void); ///< Destructor
241241

242242
/// \brief Get a string describing \b this architecture

Ghidra/Features/Decompiler/src/decompile/cpp/block.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private:
132132
// the result of the condition being false
133133
static void replaceEdgeMap(vector<BlockEdge> &vec); ///< Update block references in edges with copy map
134134
void addInEdge(FlowBlock *b,uint4 lab); ///< Add an edge coming into \b this
135-
void decodeNextInEdge(Decoder &decoder,BlockMap &resolver); ///< Restore the next input edge from XML
135+
void decodeNextInEdge(Decoder &decoder,BlockMap &resolver); ///< Decode the next input edge from stream
136136
void halfDeleteInEdge(int4 slot); ///< Delete the \e in half of an edge, correcting indices
137137
void halfDeleteOutEdge(int4 slot); ///< Delete the \e out half of an edge, correcting indices
138138
void removeInEdge(int4 slot); ///< Remove an incoming edge

Ghidra/Features/Decompiler/src/decompile/cpp/comment.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public:
6868
int4 getUniq(void) const { return uniq; } ///< Get the sub-sorting index
6969
const string &getText(void) const { return text; } ///< Get the body of the comment
7070
void encode(Encoder &encoder) const; ///< Encode the comment to a stream
71-
void decode(Decoder &decoder); ///< Restore the comment from XML
71+
void decode(Decoder &decoder); ///< Decode the comment from a stream
7272
static uint4 encodeCommentType(const string &name); ///< Convert name string to comment property
7373
static string decodeCommentType(uint4 val); ///< Convert comment property to string
7474
};
@@ -146,7 +146,7 @@ public:
146146
/// \param encoder is the stream encoder
147147
virtual void encode(Encoder &encoder) const=0;
148148

149-
/// \brief Restore all comments from a \<commentdb> element
149+
/// \brief Decode all comments from a \<commentdb> element
150150
///
151151
/// \param decoder is the stream decoder
152152
virtual void decode(Decoder &decoder)=0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "compression.hh"
17+
18+
namespace ghidra {
19+
20+
/// The compression \b level ranges from 1-9 from faster/least compression to slower/most compression.
21+
/// Use a \b level of 0 for no compression and -1 for the \e default compression level.
22+
/// \param level is the compression level
23+
Compress::Compress(int4 level)
24+
25+
{
26+
compStream.zalloc = Z_NULL;
27+
compStream.zfree = Z_NULL;
28+
compStream.opaque = Z_NULL;
29+
int4 ret = deflateInit(&compStream, level);
30+
if (ret != Z_OK)
31+
throw LowlevelError("Could not initialize deflate stream state");
32+
}
33+
34+
Compress::~Compress(void)
35+
36+
{
37+
deflateEnd(&compStream);
38+
}
39+
40+
/// Return the number of bytes of output space still available. Output may be limited by the amount
41+
/// of space in the output buffer or the amount of data available in the current input buffer.
42+
/// \param buffer is where compressed bytes are stored
43+
/// \param sz is the size, in bytes, of the buffer
44+
/// \param finish is set to \b true if this is the final buffer to add to the stream
45+
/// \return the number of output bytes still available
46+
int4 Compress::deflate(uint1 *buffer,int4 sz,bool finish)
47+
48+
{
49+
int flush = finish ? Z_FINISH : Z_NO_FLUSH;
50+
compStream.avail_out = sz;
51+
compStream.next_out = buffer;
52+
53+
int ret = ::deflate(&compStream, flush);
54+
if (ret == Z_STREAM_ERROR)
55+
throw LowlevelError("Error compressing stream");
56+
return compStream.avail_out;
57+
}
58+
59+
Decompress::Decompress(void)
60+
61+
{
62+
streamFinished = false;
63+
compStream.zalloc = Z_NULL;
64+
compStream.zfree = Z_NULL;
65+
compStream.opaque = Z_NULL;
66+
compStream.avail_in = 0;
67+
compStream.next_in = Z_NULL;
68+
int ret = inflateInit(&compStream);
69+
if (ret != Z_OK)
70+
throw LowlevelError("Could not initialize inflate stream state");
71+
}
72+
73+
/// Return the number of bytes of output space still available. Output may be limited by the amount
74+
/// of space in the output buffer or the amount of data available in the current input buffer.
75+
/// \param buffer is where uncompressed bytes are stored
76+
/// \param sz is the size, in bytes, of the buffer
77+
/// \return the number of output bytes still available
78+
int4 Decompress::inflate(uint1 *buffer,int4 sz)
79+
80+
{
81+
compStream.avail_out = sz;
82+
compStream.next_out = buffer;
83+
84+
int ret = ::inflate(&compStream, Z_NO_FLUSH);
85+
switch (ret) {
86+
case Z_NEED_DICT:
87+
case Z_DATA_ERROR:
88+
case Z_MEM_ERROR:
89+
case Z_STREAM_ERROR:
90+
throw LowlevelError("Error decompressing stream");
91+
case Z_STREAM_END:
92+
streamFinished = true;
93+
break;
94+
default:
95+
break;
96+
}
97+
98+
return compStream.avail_out;
99+
}
100+
101+
Decompress::~Decompress(void)
102+
103+
{
104+
inflateEnd(&compStream);
105+
}
106+
107+
const int4 CompressBuffer::IN_BUFFER_SIZE = 4096;
108+
const int4 CompressBuffer::OUT_BUFFER_SIZE = 4096;
109+
110+
/// \param s is the backing output stream
111+
/// \param level is the level of compression
112+
CompressBuffer::CompressBuffer(ostream &s,int4 level)
113+
: outStream(s), compressor(level)
114+
{
115+
inBuffer = new uint1[IN_BUFFER_SIZE];
116+
outBuffer = new uint1[OUT_BUFFER_SIZE];
117+
setp((char *)inBuffer,(char *)inBuffer + IN_BUFFER_SIZE-1);
118+
}
119+
120+
CompressBuffer::~CompressBuffer(void)
121+
122+
{
123+
delete [] inBuffer;
124+
delete [] outBuffer;
125+
}
126+
127+
/// The compressor is called repeatedly and its output is written to the backing stream
128+
/// until the compressor can no longer fill the \e output buffer.
129+
/// \param lastBuffer is \b true if this is the final set of bytes to add to the compressed stream
130+
void CompressBuffer::flushInput(bool lastBuffer)
131+
132+
{
133+
int len = pptr() - pbase();
134+
compressor.input((uint1 *)pbase(),len);
135+
int4 outAvail;
136+
do {
137+
outAvail = OUT_BUFFER_SIZE;
138+
outAvail = compressor.deflate(outBuffer,outAvail,lastBuffer);
139+
outStream.write((char *)outBuffer,OUT_BUFFER_SIZE-outAvail);
140+
} while(outAvail == 0);
141+
pbump(-len);
142+
}
143+
144+
/// \param c is the final character filling the buffer
145+
/// \return the written character
146+
int CompressBuffer::overflow(int c)
147+
148+
{
149+
if (c != EOF) {
150+
*pptr() = c;
151+
pbump(1);
152+
}
153+
flushInput(false);
154+
return c;
155+
}
156+
157+
/// \return 0 for success
158+
int CompressBuffer::sync(void)
159+
160+
{
161+
flushInput(true);
162+
return 0;
163+
}
164+
165+
}

0 commit comments

Comments
 (0)