]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #614 in SNORT/snort3 from crc_lzma to master
authorMichael Altizer (mialtize) <mialtize@cisco.com>
Thu, 8 Sep 2016 17:46:12 +0000 (13:46 -0400)
committerMichael Altizer (mialtize) <mialtize@cisco.com>
Thu, 8 Sep 2016 17:46:12 +0000 (13:46 -0400)
Squashed commit of the following:

commit 733e5c3c4f5fd7c5eab4b079db98fb36f1d0216a
Author: Russ Combs <rucombs@cisco.com>
Date:   Tue Sep 6 18:37:28 2016 -0400

    refactor file_decomp.h so that pdf and swf headers aren't required
    ensure consistent fd_session_t regardless of lzma support for snort and extras
    reorganize fd_session_t to reduce void space
    don't install internal swf and pdf headers

src/decompress/CMakeLists.txt
src/decompress/Makefile.am
src/decompress/file_decomp.cc
src/decompress/file_decomp.h
src/decompress/file_decomp_pdf.cc
src/decompress/file_decomp_pdf.h
src/decompress/file_decomp_swf.cc
src/decompress/file_decomp_swf.h

index 06fc68789e3cbd2380c38ff41ee96d9e7908986f..7d71ea75a0c7625da02aa60614ddd0e849ff5e97 100644 (file)
@@ -1,15 +1,15 @@
 
 set( DECOMPRESS_INCLUDES
     file_decomp.h
-    file_decomp_pdf.h
-    file_decomp_swf.h
 )
 
 add_library (decompress STATIC
     ${DECOMPRESS_INCLUDES}
     file_decomp.cc
     file_decomp_pdf.cc
+    file_decomp_pdf.h
     file_decomp_swf.cc
+    file_decomp_swf.h
 )
 
 target_link_libraries(decompress
index 9a5bd82ed038403f57d792e3b508000bf1a13a5e..7d34b79c188c2e33064cd0e3c21d2286236b733d 100644 (file)
@@ -4,12 +4,12 @@ noinst_LIBRARIES = libdecompress.a
 x_includedir = $(pkgincludedir)/decompress
 
 x_include_HEADERS = \
-file_decomp.h \
-file_decomp_pdf.h \
-file_decomp_swf.h
+file_decomp.h
 
 libdecompress_a_SOURCES = \
 file_decomp.cc \
 file_decomp_pdf.cc \
-file_decomp_swf.cc
+file_decomp_pdf.h \
+file_decomp_swf.cc \
+file_decomp_swf.h
 
index e658ff3f868e380e62a0d2d47ae58de5abc21fca..420e245a7611917ed229069aac4dcd20b4b63b01 100644 (file)
 
 // file_decomp.cc author Ed Borgoyn <eborgoyn@sourcefire.com>
 
+#include "file_decomp.h"
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include "file_decomp.h"
+#ifdef HAVE_LZMA
+#include <lzma.h>
+#endif
+
+#include <zlib.h>
+
 #include "main/snort_types.h"
 #include "utils/util.h"
 #include "detection/detection_util.h"
-#include "decompress/file_decomp_pdf.h"
-#include "decompress/file_decomp_swf.h"
+
+#include "file_decomp_pdf.h"
+#include "file_decomp_swf.h"
 
 #ifdef UNIT_TEST
 #include "catch/catch.hpp"
@@ -413,6 +421,21 @@ fd_status_t File_Decomp_StopFree(fd_session_p_t SessionPtr)
 
 void File_Decomp_Free(fd_session_p_t SessionPtr)
 {
+    assert(SessionPtr);
+
+    switch ( SessionPtr->File_Type )
+    {
+    case FILE_TYPE_SWF:
+        assert(SessionPtr->SWF);
+        snort_free(SessionPtr->SWF);
+        break;
+
+    case FILE_TYPE_PDF:
+        assert(SessionPtr->PDF);
+        snort_free(SessionPtr->PDF);
+        break;
+    }
+
     delete SessionPtr;
 }
 
index 66724655320dc7561ce879a05bbfb888b1bd1e77..5af2fbb69e1e4c46ece8d0e48d3c87e773cbabdb 100644 (file)
 
 // File_Decomp global typedefs (used in child objects)
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
 #include <stdint.h>
 #include <string.h>
 
 #include "main/snort_types.h"
 
 /* Function return codes used internally and with caller */
-typedef enum fd_status
+enum fd_status_t
 {
     File_Decomp_DecompError = -2,  /* Error from decompression */
     File_Decomp_Error = -1,        /* Error from decompression */
@@ -43,27 +39,16 @@ typedef enum fd_status
     File_Decomp_BlockOut = 3,      /* Blocked due to lack of output space */
     File_Decomp_BlockIn = 4,       /* Blocked due to lack in input data */
     File_Decomp_Eof = 5            /* End of file located */
-} fd_status_t;
+};
 
-typedef enum file_compression_type
+enum file_compression_type_t
 {
     FILE_COMPRESSION_TYPE_NONE,
     FILE_COMPRESSION_TYPE_DEFLATE,
     FILE_COMPRESSION_TYPE_ZLIB,
     FILE_COMPRESSION_TYPE_LZMA,
     FILE_COMPRESSION_TYPE_MAX
-} file_compression_type_t;
-
-typedef struct fd_session_s* fd_session_p_t, fd_session_t;
-
-// FIXIT-L this should be unravelled so that these internal includes are not necessary
-#include "decompress/file_decomp_pdf.h"
-#include "decompress/file_decomp_swf.h"
-#include <zlib.h>
-
-#ifdef HAVE_LZMA
-#include <lzma.h>
-#endif
+};
 
 /* Potential decompression modes, passed in at initalization time. */
 #define FILE_SWF_LZMA_BIT    (0x00000001)
@@ -90,62 +75,66 @@ enum FileDecompError
 };
 
 /* Private Types */
-typedef enum file_type
+enum file_type_t
 {
     FILE_TYPE_NONE,
     FILE_TYPE_SWF,
     FILE_TYPE_PDF,
     FILE_TYPE_MAX
-} file_type_t;
+};
 
-typedef enum states
+enum fd_states_t
 {
     STATE_NEW,        /* Session created */
     STATE_READY,      /* Session created and ready for content, no file/decomp selected */
     STATE_ACTIVE,     /* Decompressor inited and ready for content */
     STATE_COMPLETE    /* Decompression completed */
-} fd_states_t;
+};
 
 /* Primary file decompression session state context */
-struct fd_session_s
+struct fd_session_t
 {
-    uint8_t* Next_In;   /* next input byte */
-    uint32_t Avail_In;  /* number of bytes available at next_in */
-    uint32_t Total_In;  /* total number of input bytes read so far */
-
-    uint8_t* Next_Out;  /* next output byte should be put there */
-    uint32_t Avail_Out; /* remaining free space at next_out */
-    uint32_t Total_Out; /* total number of bytes output so far */
-
-    /* Internal buffer setup by _Init().  App can overide. */
-    uint8_t* Buffer;    /* pointer to decompresiion buffer */
-    uint32_t Buffer_Len; /* length of decompression buffer */
+    // FIXIT-L replace with abstract base class pointer used for
+    // PDF or SWF subclass and eliminate switches on File_Type
+    union
+    {
+        struct fd_PDF_t* PDF;
+        struct fd_SWF_t* SWF;
+    };
 
-    /* Configuration settings */
-    uint32_t Compr_Depth;
-    uint32_t Decompr_Depth;
-    uint32_t Modes;     /* Bit mapped set of potential file/algo modes */
+    uint8_t* Next_In;    // next input byte
+    uint8_t* Next_Out;   // next output byte should be put there
+    uint8_t* Buffer;     // pointer to decompresiion buffer
 
-    /* Alerting callback */
+    // Alerting callback
     void (* Alert_Callback)(void* Context, int Event);
     void* Alert_Context;
 
-    /* Internal State */
-    uint8_t File_Type;   /* Active file type */
-    uint8_t Decomp_Type; /* Active decompression type */
-    uint8_t Sig_State;   /* Sig search state machine */
-    uint8_t State;       /* main state machine */
+    uint32_t Avail_In;   // number of bytes available at next_in
+    uint32_t Total_In;   // total number of input bytes read so far
 
-    union
-    {
-        fd_PDF_t PDF;
-        fd_SWF_t SWF;
-    } Decomp_State;
+    uint32_t Avail_Out;  // remaining free space at next_out
+    uint32_t Total_Out;  // total number of bytes output so far
 
-    /* Specific event indicated by DecomprError return */
-    int Error_Event;
+    uint32_t Buffer_Len;  // length of decompression buffer
+
+    // Configuration settings
+    uint32_t Compr_Depth;
+    uint32_t Decompr_Depth;
+    uint32_t Modes;      // Bit mapped set of potential file/algo modes
+
+    int Error_Event;     // Specific event indicated by DecomprError return
+
+    // Internal State
+    uint8_t File_Type;   // Active file type
+    uint8_t Decomp_Type; // Active decompression type
+    uint8_t Sig_State;   // Sig search state machine
+    uint8_t State;       // main state machine
 };
 
+// FIXIT-L don't obfuscate pointers
+typedef fd_session_t* fd_session_p_t;
+
 /* Macros */
 
 /* Macros used to sync my decompression context with that
index e2b6d7cf2c75ca4397d4f2a1a7d45a0df79c62df..a7d0399a5c7b7cf0f8edb9b7c47a0520105a3e67 100644 (file)
@@ -18,7 +18,6 @@
 
 // file_decomp_pdf.cc author Ed Borgoyn <eborgoyn@sourcefire.com>
 
-#include "file_decomp.h"
 #include "file_decomp_pdf.h"
 
 #ifdef HAVE_CONFIG_H
@@ -31,6 +30,7 @@
 #include <zlib.h>
 
 #include "main/thread.h"
+#include "utils/util.h"
 
 #ifdef UNIT_TEST
 #include "catch/catch.hpp"
@@ -172,7 +172,7 @@ static inline void Process_One_Filter(fd_session_p_t SessionPtr, uint8_t* Token,
         {
             /* Found our first matching, supported filter type */
             SessionPtr->Decomp_Type = Comp_Type;
-            SessionPtr->Decomp_State.PDF.Decomp_Type = Comp_Type;
+            SessionPtr->PDF->Decomp_Type = Comp_Type;
         }
     }
     else
@@ -198,7 +198,7 @@ static fd_status_t Process_Filter_Spec(fd_session_p_t SessionPtr)
     int Index;
 
     fd_status_t Ret_Code = File_Decomp_OK;
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
 
     /* Assume the 'no compression' result */
     SessionPtr->Decomp_Type = FILE_COMPRESSION_TYPE_NONE;
@@ -281,7 +281,7 @@ static fd_status_t Process_Filter_Spec(fd_session_p_t SessionPtr)
 
 static inline void Init_Parser(fd_session_p_t SessionPtr)
 {
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
     /* The parser starts in the P_COMMENT state we start
        parsing the file just after the signature is located
        and the signature is syntactially a comment. */
@@ -337,7 +337,7 @@ static inline fd_PDF_Parse_Stack_p_t Get_Previous_State(fd_PDF_Parse_p_t p)
 static inline fd_status_t Handle_State_DICT_OBJECT(fd_session_p_t SessionPtr, uint8_t c)
 {
     char Filter_Tok[] = TOK_DICT_FILT;
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
 
     /* enter with c being an EOL from the ind obj state */
     if ( p->State != P_DICT_OBJECT )
@@ -548,7 +548,7 @@ static inline fd_status_t Handle_State_IND_OBJ(fd_session_p_t SessionPtr, uint8_
     static THREAD_LOCAL uint8_t Stream_Token[] = { TOK_STRM_OPEN };
     static THREAD_LOCAL uint8_t Stream_End_Token[] = { TOK_STRM_CLOSE };
 
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
 
     /* Upon initial entry, setup state context */
     if ( p->State != P_IND_OBJ )
@@ -729,7 +729,7 @@ static inline fd_status_t Handle_State_XREF(fd_session_p_t SessionPtr, uint8_t c
 {
     static THREAD_LOCAL const uint8_t* Xref_Tok = (uint8_t*)TOK_XRF_STARTXREF;
     uint8_t Xref_End_Tok[] = { TOK_XRF_END };
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
 
     if ( p->State != P_XREF )
     {
@@ -788,7 +788,7 @@ static inline fd_status_t Handle_State_XREF(fd_session_p_t SessionPtr, uint8_t c
 
 static inline fd_status_t Handle_State_START(fd_session_p_t SessionPtr, uint8_t c)
 {
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
     /* Skip any whitespace.  This will include
        the LF as part of a <CRLF> EOL token. */
     if ( IS_WHITESPACE(c) )
@@ -832,7 +832,7 @@ static inline fd_status_t Handle_State_START(fd_session_p_t SessionPtr, uint8_t
 /* Parse file until input blocked or stream located. */
 static fd_status_t Locate_Stream_Beginning(fd_session_p_t SessionPtr)
 {
-    fd_PDF_Parse_p_t p = &(SessionPtr->Decomp_State.PDF.Parse);
+    fd_PDF_Parse_p_t p = &(SessionPtr->PDF->Parse);
     fd_status_t Ret_Code = File_Decomp_OK;
     uint8_t c;
 
@@ -907,7 +907,7 @@ static fd_status_t Locate_Stream_Beginning(fd_session_p_t SessionPtr)
 
 static fd_status_t Init_Stream(fd_session_p_t SessionPtr)
 {
-    fd_PDF_p_t StPtr = &(SessionPtr->Decomp_State.PDF);
+    fd_PDF_p_t StPtr = SessionPtr->PDF;
 
     switch ( StPtr->Decomp_Type )
     {
@@ -942,7 +942,7 @@ static fd_status_t Init_Stream(fd_session_p_t SessionPtr)
 
 static fd_status_t Decomp_Stream(fd_session_p_t SessionPtr)
 {
-    fd_PDF_p_t StPtr = &(SessionPtr->Decomp_State.PDF);
+    fd_PDF_p_t StPtr = SessionPtr->PDF;
 
     /* No reason to decompress if there's no input or
        room for output. */
@@ -989,10 +989,10 @@ static fd_status_t Decomp_Stream(fd_session_p_t SessionPtr)
 static fd_status_t Close_Stream(fd_session_p_t SessionPtr)
 {
     /* Put the parser state back where it was interrupted */
-    if ( Pop_State(&(SessionPtr->Decomp_State.PDF.Parse) ) == File_Decomp_Error )
+    if ( Pop_State(&(SessionPtr->PDF->Parse) ) == File_Decomp_Error )
         return( File_Decomp_Error );
 
-    SessionPtr->Decomp_State.PDF.State = PDF_STATE_LOCATE_STREAM;
+    SessionPtr->PDF->State = PDF_STATE_LOCATE_STREAM;
 
     return( File_Decomp_OK );
 }
@@ -1005,7 +1005,7 @@ fd_status_t File_Decomp_End_PDF(fd_session_p_t SessionPtr)
     if ( SessionPtr == NULL )
         return( File_Decomp_Error );
 
-    StPtr = &(SessionPtr->Decomp_State.PDF);
+    StPtr = SessionPtr->PDF;
 
     if ( (StPtr->State != PDF_STATE_INIT_STREAM) &&
         (StPtr->State != PDF_STATE_PROCESS_STREAM) )
@@ -1038,12 +1038,12 @@ fd_status_t File_Decomp_End_PDF(fd_session_p_t SessionPtr)
 /* From caller, initialize PDF state machine. */
 fd_status_t File_Decomp_Init_PDF(fd_session_p_t SessionPtr)
 {
-    fd_PDF_p_t StPtr;
-
     if ( SessionPtr == NULL )
         return( File_Decomp_Error );
 
-    StPtr = &(SessionPtr->Decomp_State.PDF);
+    SessionPtr->PDF = (fd_PDF_t*)snort_calloc(sizeof(fd_PDF_t));
+
+    fd_PDF_p_t StPtr = SessionPtr->PDF;
 
     Init_Parser(SessionPtr);
 
@@ -1066,7 +1066,7 @@ fd_status_t File_Decomp_PDF(fd_session_p_t SessionPtr)
     /* Process all data until blocked */
     while ( 1 )
     {
-        switch ( SessionPtr->Decomp_State.PDF.State )
+        switch ( SessionPtr->PDF->State )
         {
         case ( PDF_STATE_LOCATE_STREAM ):
         {
@@ -1090,7 +1090,7 @@ fd_status_t File_Decomp_PDF(fd_session_p_t SessionPtr)
             }
             else
             {
-                SessionPtr->Decomp_State.PDF.State = PDF_STATE_INIT_STREAM;
+                SessionPtr->PDF->State = PDF_STATE_INIT_STREAM;
                 /* If we've located the beginning of stream, set new state
                    and fall into next state */
             }
@@ -1109,7 +1109,7 @@ fd_status_t File_Decomp_PDF(fd_session_p_t SessionPtr)
                 break;
             }
 
-            SessionPtr->Decomp_State.PDF.State = PDF_STATE_PROCESS_STREAM;
+            SessionPtr->PDF->State = PDF_STATE_PROCESS_STREAM;
             /* INTENTIONAL FALL-THROUGH INTO PDF_STATE_PROCESS_STREAM CASE. */
         }
 
@@ -1176,8 +1176,10 @@ TEST_CASE("File_Decomp_PDF-not_pdf-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
+    p_s->PDF = (fd_PDF_t*)snort_calloc(sizeof(fd_PDF_t));
     p_s->File_Type = FILE_TYPE_SWF;
     REQUIRE(File_Decomp_PDF(p_s) == File_Decomp_Error);
+    snort_free(p_s->PDF);
 }
 
 TEST_CASE("File_Decomp_PDF-bad_state-error", "[file_decomp]")
@@ -1185,9 +1187,11 @@ TEST_CASE("File_Decomp_PDF-bad_state-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
+    p_s->PDF = (fd_PDF_t*)snort_calloc(sizeof(fd_PDF_t));
     p_s->File_Type = FILE_TYPE_PDF;
-    p_s->Decomp_State.PDF.State = PDF_STATE_NEW;
+    p_s->PDF->State = PDF_STATE_NEW;
     REQUIRE(File_Decomp_PDF(p_s) == File_Decomp_Error);
+    snort_free(p_s->PDF);
 }
 
 TEST_CASE("File_Decomp_End_PDF-bad_type-error", "[file_decomp]")
@@ -1195,9 +1199,11 @@ TEST_CASE("File_Decomp_End_PDF-bad_type-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
-    p_s->Decomp_State.PDF.Decomp_Type = FILE_COMPRESSION_TYPE_LZMA;
-    p_s->Decomp_State.PDF.State = PDF_STATE_PROCESS_STREAM;
+    p_s->PDF = (fd_PDF_t*)snort_calloc(sizeof(fd_PDF_t));
+    p_s->PDF->Decomp_Type = FILE_COMPRESSION_TYPE_LZMA;
+    p_s->PDF->State = PDF_STATE_PROCESS_STREAM;
     REQUIRE(File_Decomp_End_PDF(p_s) == File_Decomp_Error);
+    snort_free(p_s->PDF);
 }
 
 #endif
index 410e0826925bd91cca8b01bb99d289f206bb85b6..1129661284e81980d7ddff6c144ef77b7246c26f 100644 (file)
 #ifndef FILE_DECOMP_PDF_H
 #define FILE_DECOMP_PDF_H
 
+#include <stdint.h>
 #include <zlib.h>
 
+#include "file_decomp.h"
+
 #define ELEM_BUF_LEN        (12)
 #define FILTER_SPEC_BUF_LEN (40)
 #define PARSE_STACK_LEN     (12)
 /* FIXIT-L Other than the API prototypes, the other parts of this header should
    be private to file_decomp_pdf. */
 
-typedef enum pdf_states
+enum fd_PDF_States
 {
     PDF_STATE_NEW,
     PDF_STATE_LOCATE_STREAM,     /* Found sig bytes, looking for dictionary & stream*/
     PDF_STATE_INIT_STREAM,       /* Init stream */
     PDF_STATE_PROCESS_STREAM     /* Processing stream */
-} fd_PDF_States;
+};
 
-typedef struct fd_PDF_Parse_Stack_s
+struct fd_PDF_Parse_Stack_t
 {
     uint8_t State;
     uint8_t Sub_State;
-} fd_PDF_Parse_Stack_t, * fd_PDF_Parse_Stack_p_t;
+};
 
-typedef struct fd_PDF_Parse_s
+struct fd_PDF_Parse_t
 {
     uint8_t Dict_Nesting_Cnt;
     uint8_t Elem_Buf[ELEM_BUF_LEN];
@@ -58,14 +61,14 @@ typedef struct fd_PDF_Parse_s
     uint32_t Gen_Number;
     uint8_t Sub_State;
     uint8_t State;
-} fd_PDF_Parse_t, * fd_PDF_Parse_p_t;
+};
 
-typedef struct fd_PDF_Deflate_s
+struct fd_PDF_Deflate_t
 {
     z_stream StreamDeflate;
-} fd_PDF_Deflate_t;
+};
 
-typedef struct fd_PDF_s
+struct fd_PDF_t
 {
     union
     {
@@ -74,7 +77,12 @@ typedef struct fd_PDF_s
     fd_PDF_Parse_t Parse;
     uint8_t Decomp_Type;
     uint8_t State;
-} fd_PDF_t, * fd_PDF_p_t;
+};
+
+// FIXIT-L don't obfuscate pointers
+typedef fd_PDF_Parse_Stack_t* fd_PDF_Parse_Stack_p_t;
+typedef fd_PDF_Parse_t* fd_PDF_Parse_p_t;
+typedef fd_PDF_t* fd_PDF_p_t;
 
 /* API Functions */
 
index 6a173ca995f85744d1f1abb9505a430cde5707da..d120258420934516c47755a3cb38a269fecacdc4 100644 (file)
@@ -18,7 +18,6 @@
 
 // file_decomp_swf.cc author Ed Borgoyn <eborgoyn@sourcefire.com>
 
-#include "file_decomp.h"
 #include "file_decomp_swf.h"
 
 #ifdef HAVE_CONFIG_H
@@ -32,6 +31,8 @@
 #include <lzma.h>
 #endif
 
+#include "utils/util.h"
+
 #ifdef UNIT_TEST
 #include "catch/catch.hpp"
 #endif
 static fd_status_t File_Decomp_Process_LZMA_Header(fd_session_p_t SessionPtr)
 {
     uint8_t LZMA_Header[LZMA_HEADER_LEN];
-    uint8_t* SWF_Header = SessionPtr->Decomp_State.SWF.Header_Bytes;
+    uint8_t* SWF_Header = SessionPtr->SWF->Header_Bytes;
     uint64_t LZMA_Uncomp_Len;
     uint32_t SWF_Uncomp_Len;
     int idx;
 
     lzma_ret l_ret;
-    lzma_stream* l_s = &(SessionPtr->Decomp_State.SWF.StreamLZMA);
+    lzma_stream* l_s = &(SessionPtr->SWF->StreamLZMA);
 
     SWF_Uncomp_Len = 0;
     /* Read little-endian into value */
@@ -109,7 +110,7 @@ static fd_status_t Decomp(fd_session_p_t SessionPtr)
     case FILE_COMPRESSION_TYPE_ZLIB:
     {
         int z_ret;
-        z_stream* z_s = &(SessionPtr->Decomp_State.SWF.StreamZLIB);
+        z_stream* z_s = &(SessionPtr->SWF->StreamZLIB);
 
         SYNC_IN(z_s)
 
@@ -134,7 +135,7 @@ static fd_status_t Decomp(fd_session_p_t SessionPtr)
     case FILE_COMPRESSION_TYPE_LZMA:
     {
         lzma_ret l_ret;
-        lzma_stream* l_s = &(SessionPtr->Decomp_State.SWF.StreamLZMA);
+        lzma_stream* l_s = &(SessionPtr->SWF->StreamLZMA);
 
         SYNC_IN(l_s)
 
@@ -173,7 +174,7 @@ fd_status_t File_Decomp_End_SWF(fd_session_p_t SessionPtr)
     case FILE_COMPRESSION_TYPE_ZLIB:
     {
         int z_ret;
-        z_stream* z_s = &(SessionPtr->Decomp_State.SWF.StreamZLIB);
+        z_stream* z_s = &(SessionPtr->SWF->StreamZLIB);
 
         z_ret = inflateEnd(z_s);
 
@@ -188,7 +189,7 @@ fd_status_t File_Decomp_End_SWF(fd_session_p_t SessionPtr)
 #ifdef HAVE_LZMA
     case FILE_COMPRESSION_TYPE_LZMA:
     {
-        lzma_stream* l_s = &(SessionPtr->Decomp_State.SWF.StreamLZMA);
+        lzma_stream* l_s = &(SessionPtr->SWF->StreamLZMA);
 
         lzma_end(l_s);
 
@@ -207,10 +208,12 @@ fd_status_t File_Decomp_Init_SWF(fd_session_p_t SessionPtr)
     if ( SessionPtr == NULL )
         return( File_Decomp_Error );
 
+    SessionPtr->SWF = (fd_SWF_t*)snort_calloc(sizeof(fd_SWF_t));
+
     /* Indicate the we need to look for the remainder of the
        uncompressed header. */
-    SessionPtr->Decomp_State.SWF.State = SWF_STATE_GET_HEADER;
-    SessionPtr->Decomp_State.SWF.Header_Cnt = 0;
+    SessionPtr->SWF->State = SWF_STATE_GET_HEADER;
+    SessionPtr->SWF->Header_Cnt = 0;
 
     switch ( SessionPtr->Decomp_Type )
     {
@@ -219,10 +222,10 @@ fd_status_t File_Decomp_Init_SWF(fd_session_p_t SessionPtr)
         int z_ret;
         z_stream* z_s;
 
-        SessionPtr->Decomp_State.SWF.Header_Len =
+        SessionPtr->SWF->Header_Len =
             SWF_VER_LEN + SWF_UCL_LEN;
 
-        z_s = &(SessionPtr->Decomp_State.SWF.StreamZLIB);
+        z_s = &(SessionPtr->SWF->StreamZLIB);
 
         memset( (char*)z_s, 0, sizeof(z_stream));
 
@@ -246,10 +249,10 @@ fd_status_t File_Decomp_Init_SWF(fd_session_p_t SessionPtr)
         lzma_ret l_ret;
         lzma_stream* l_s;
 
-        SessionPtr->Decomp_State.SWF.Header_Len =
+        SessionPtr->SWF->Header_Len =
             SWF_VER_LEN + SWF_UCL_LEN + SWF_LZMA_CML_LEN + SWF_LZMA_PRP_LEN;
 
-        l_s = &(SessionPtr->Decomp_State.SWF.StreamLZMA);
+        l_s = &(SessionPtr->SWF->StreamLZMA);
 
         memset( (char*)l_s, 0, sizeof(lzma_stream));
 
@@ -281,14 +284,14 @@ fd_status_t File_Decomp_SWF(fd_session_p_t SessionPtr)
         return( File_Decomp_Error );
 
     /* Are we still looking for the balance of the uncompressed header? */
-    switch ( SessionPtr->Decomp_State.SWF.State )
+    switch ( SessionPtr->SWF->State )
     {
     case ( SWF_STATE_GET_HEADER ):
     {
-        uint8_t* Cnt_Ptr = &(SessionPtr->Decomp_State.SWF.Header_Cnt);      // For convenience
-        uint8_t* Len_Ptr = &(SessionPtr->Decomp_State.SWF.Header_Len);      // For convenience
+        uint8_t& Cnt_Ptr = SessionPtr->SWF->Header_Cnt;
+        uint8_t& Len_Ptr = SessionPtr->SWF->Header_Len;
 
-        while ( *Len_Ptr > *Cnt_Ptr )
+        while ( Len_Ptr > Cnt_Ptr )
         {
             if ( SessionPtr->Avail_In == 0 )
                 return( File_Decomp_BlockIn );
@@ -296,14 +299,14 @@ fd_status_t File_Decomp_SWF(fd_session_p_t SessionPtr)
             if ( SessionPtr->Avail_Out == 0 )
                 return( File_Decomp_BlockOut );
 
-            SessionPtr->Decomp_State.SWF.Header_Bytes[*Cnt_Ptr] =
+            SessionPtr->SWF->Header_Bytes[Cnt_Ptr] =
                 *(SessionPtr->Next_In);
 
             (void)Move_1(SessionPtr);
-            *Cnt_Ptr += 1;
+            ++Cnt_Ptr;
         }
 
-        SessionPtr->Decomp_State.SWF.State = SWF_STATE_PROC_HEADER;
+        SessionPtr->SWF->State = SWF_STATE_PROC_HEADER;
         /* INTENTIONAL FALL-THROUGH INTO SWF_STATE_PROC_HEADER CASE. */
     }
     case ( SWF_STATE_PROC_HEADER ):
@@ -317,7 +320,7 @@ fd_status_t File_Decomp_SWF(fd_session_p_t SessionPtr)
         }
 #endif
 
-        SessionPtr->Decomp_State.SWF.State = SWF_STATE_DATA;
+        SessionPtr->SWF->State = SWF_STATE_DATA;
         /* INTENTIONAL FALL-THROUGH INTO SWF_STATE_DATA CASE. */
     }
     case ( SWF_STATE_DATA ):
@@ -363,8 +366,10 @@ TEST_CASE("File_Decomp_SWF-not_swf-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
+    p_s->SWF = (fd_SWF_t*)snort_calloc(sizeof(fd_SWF_t));
     p_s->File_Type = FILE_TYPE_PDF;
     REQUIRE(File_Decomp_SWF(p_s) == File_Decomp_Error);
+    snort_free(p_s->SWF);
 }
 
 TEST_CASE("File_Decomp_SWF-bad_state-error", "[file_decomp]")
@@ -372,9 +377,11 @@ TEST_CASE("File_Decomp_SWF-bad_state-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
+    p_s->SWF = (fd_SWF_t*)snort_calloc(sizeof(fd_SWF_t));
     p_s->File_Type = FILE_TYPE_SWF;
-    p_s->Decomp_State.SWF.State = SWF_STATE_NEW;
+    p_s->SWF->State = SWF_STATE_NEW;
     REQUIRE(File_Decomp_SWF(p_s) == File_Decomp_Error);
+    snort_free(p_s->SWF);
 }
 
 TEST_CASE("File_Decomp_Init_SWF-bad_type-error", "[file_decomp]")
@@ -382,8 +389,10 @@ TEST_CASE("File_Decomp_Init_SWF-bad_type-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
+    p_s->SWF = (fd_SWF_t*)snort_calloc(sizeof(fd_SWF_t));
     p_s->Decomp_Type = FILE_COMPRESSION_TYPE_DEFLATE;
     REQUIRE(File_Decomp_Init_SWF(p_s) == File_Decomp_Error);
+    snort_free(p_s->SWF);
 }
 
 TEST_CASE("File_Decomp_End_SWF-bad_type-error", "[file_decomp]")
@@ -391,8 +400,10 @@ TEST_CASE("File_Decomp_End_SWF-bad_type-error", "[file_decomp]")
     fd_session_p_t p_s;
 
     REQUIRE((p_s = File_Decomp_New()) != (fd_session_p_t)NULL);
+    p_s->SWF = (fd_SWF_t*)snort_calloc(sizeof(fd_SWF_t));
     p_s->Decomp_Type = FILE_COMPRESSION_TYPE_DEFLATE;
     REQUIRE(File_Decomp_End_SWF(p_s) == File_Decomp_Error);
+    snort_free(p_s->SWF);
 }
 
 #endif
index b74ac74b2c1c9ecaec91017831be51e531dbc589..e29db56daf9eddb55a98385924414da0a5ae9942 100644 (file)
 #include "config.h"
 #endif
 
+#include <stdint.h>
 #include <zlib.h>
+
 #ifdef HAVE_LZMA
 #include <lzma.h>
 #endif
 
+#include "file_decomp.h"
+
 /* FIXIT-L Other than the API prototypes, the other parts of this header should
    be private to file_decomp_swf. */
 
 
 /* Types */
 
-typedef enum swf_states
+enum fd_SWF_States
 {
     SWF_STATE_NEW,
     SWF_STATE_GET_HEADER,     /* Found sig bytes, looking for end of uncomp header */
     SWF_STATE_PROC_HEADER,    /* Found header bytes, now process the header */
     SWF_STATE_DATA            /* Done with header, looking for start of data */
-} fd_SWF_States;
+};
 
-typedef struct fd_SWF_s
+struct fd_SWF_t
 {
     z_stream StreamZLIB;
 #ifdef HAVE_LZMA
@@ -72,7 +76,7 @@ typedef struct fd_SWF_s
     uint8_t State;
     uint8_t Header_Len;
     uint8_t Header_Cnt;
-} fd_SWF_t;
+};
 
 /* API Functions */