Skip to content

Commit

Permalink
Removed use of value_string for decompression return code (#10)
Browse files Browse the repository at this point in the history
As return errors can be negative on an error, we can't use value_string for that. Moved into a plain field with custom formatting.
  • Loading branch information
Martin Gallo committed Nov 17, 2016
1 parent d090f28 commit 778a49c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 71 deletions.
6 changes: 3 additions & 3 deletions src/packet-sapdiag.c
Original file line number Diff line number Diff line change
Expand Up @@ -2606,7 +2606,7 @@ dissect_sapdiag(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data
/* Check the return code and add a expert info warning if an error occurred. The dissector continues trying to add
adding the payload, however the returned size should be 0. */
if (rt < 0){
expert_add_info_format(pinfo, compression_header, &ei_sapdiag_invalid_decompresssion, "Decompression of payload failed with return code %d (%s)", rt, val_to_str(rt, decompress_return_code_vals, "Unknown"));
expert_add_info_format(pinfo, compression_header, &ei_sapdiag_invalid_decompresssion, "Decompression of payload failed with return code %d (%s)", rt, decompress_error_string(rt));
}

/* Check the length returned for the compression routine. If differs with the reported, use the actual one and add
Expand All @@ -2616,7 +2616,7 @@ dissect_sapdiag(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data
}

/* Add the return code to the tree */
proto_tree_add_int(compression_header_tree, hf_sapdiag_decompress_return_code, tvb, payload_offset, 8, rt);
proto_tree_add_int_format_value(compression_header_tree, hf_sapdiag_decompress_return_code, tvb, payload_offset, 8, rt, "%d (%s)", rt, decompress_error_string(rt));

if (uncompress_length != 0){
/* Now re-setup the tvb buffer to have the new data */
Expand Down Expand Up @@ -2720,7 +2720,7 @@ proto_register_sapdiag(void)
{ &hf_sapdiag_special,
{ "Special", "sapdiag.header.compression.special", FT_UINT8, BASE_HEX, NULL, 0x0, "SAP Diag Special", HFILL }},
{ &hf_sapdiag_decompress_return_code,
{ "Decompress Return Code", "sapdiag.header.compression.returncode", FT_INT8, BASE_DEC, VALS(decompress_return_code_vals), 0x0, "SAP Diag Decompression routine return code", HFILL }},
{ "Decompress Return Code", "sapdiag.header.compression.returncode", FT_INT8, BASE_DEC, NULL, 0x0, "SAP Diag Decompression routine return code", HFILL }},
/* SAPDiag Messages */
{ &hf_sapdiag_item,
{ "Item", "sapdiag.item", FT_NONE, BASE_NONE, NULL, 0x0, "SAP Diag Item", HFILL }},
Expand Down
7 changes: 4 additions & 3 deletions src/packet-saprfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ dissect_saprfc_tables_compressed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *
* occurred. The dissector continues trying to add the payload,
* however the returned size should be 0. */
if (rt < 0){
expert_add_info_format(pinfo, compression_header, &ei_saprfc_invalid_decompresssion, "Decompression of payload failed with return code %d (%s)", rt, val_to_str(rt, decompress_return_code_vals, "Unknown"));
expert_add_info_format(pinfo, compression_header, &ei_saprfc_invalid_decompresssion, "Decompression of payload failed with return code %d (%s)", rt, decompress_error_string(rt));
}

/* Check the length returned for the compression routine. If differs
Expand All @@ -477,7 +477,8 @@ dissect_saprfc_tables_compressed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *
expert_add_info_format(pinfo, rl, &ei_saprfc_invalid_decompress_length, "The uncompressed payload length (%d) differs with the reported length (%d)", uncompress_length, reported_length);
}

/* TODO: Add the return code to the tree */
/* Add the return code to the tree */
proto_tree_add_int_format_value(compression_header_tree, hf_saprfc_table_return_code, tvb, offset, 8, rt, "%d (%s)", rt, decompress_error_string(rt));

if (uncompress_length != 0){
/* Now re-setup the tvb buffer to have the new data */
Expand Down Expand Up @@ -1217,7 +1218,7 @@ proto_register_saprfc(void)
{ &hf_saprfc_table_special,
{ "Special", "saprfc.table.compression.special", FT_UINT8, BASE_HEX, NULL, 0x0, "SAP RFC Table Special", HFILL }},
{ &hf_saprfc_table_return_code,
{ "Decompress Return Code", "saprfc.table.compression.returncode", FT_UINT8, BASE_DEC, VALS(decompress_return_code_vals), 0x0, "SAP RFC Decompression routine return code", HFILL }},
{ "Decompress Return Code", "saprfc.table.compression.returncode", FT_INT8, BASE_DEC, NULL, 0x0, "SAP RFC Decompression routine return code", HFILL }},
{ &hf_saprfc_table_content,
{ "Content", "saprfc.table.content", FT_NONE, BASE_NONE, NULL, 0x0, "SAP RFC Table Content", HFILL }},

Expand Down
87 changes: 53 additions & 34 deletions src/sapdecompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <assert.h>
#include <string.h>

#include <glib.h>

#include "sapdecompress.h"

#include "hpa101saptype.h"
Expand All @@ -32,42 +34,46 @@
#include "hpa107cslzh.h"
#include "hpa105CsObjInt.h"

/* #define DEBUG # Enable this macro if you want some debugging information on the (de)compression functions */
/* #define DEBUG_TRACE # Enable this macro if you want detailed debugging information (hexdumps) on the (de)compression functions */
/* Enable this macro if you want some debugging information on the (de)compression functions */
/* #define DEBUG */
/* Enable this macro if you want detailed debugging information (hexdumps) on the (de)compression functions */
/* #define DEBUG_TRACE */

/* Returns an error strings for compression library return codes */
const char *error_string(int return_code){
const char *decompress_error_string(int return_code){
switch (return_code){
case CS_IEND_OF_STREAM: return ("end of data (internal)");
case CS_IEND_OUTBUFFER: return ("end of output buffer");
case CS_IEND_INBUFFER: return ("end of input buffer");
case CS_E_OUT_BUFFER_LEN: return ("invalid output length");
case CS_E_IN_BUFFER_LEN: return ("invalid input length");
case CS_E_NOSAVINGS: return ("no savings");
case CS_E_INVALID_SUMLEN: return ("invalid len of stream");
case CS_E_IN_EQU_OUT: return ("inbuf == outbuf");
case CS_E_INVALID_ADDR: return ("inbuf == NULL,outbuf == NULL");
case CS_E_FATAL: return ("internal error !");
case CS_E_BOTH_ZERO: return ("inlen = outlen = 0");
case CS_E_UNKNOWN_ALG: return ("unknown algorithm");
case CS_E_UNKNOWN_TYPE: return ("unknown type");
/* for decompress */
case CS_E_FILENOTCOMPRESSED: return ("input not compressed");
case CS_E_MAXBITS_TOO_BIG: return ("maxbits to large");
case CS_E_BAD_HUF_TREE: return ("bad hufman tree");
case CS_E_NO_STACKMEM: return ("no stack memory in decomp");
case CS_E_INVALIDCODE: return ("invalid code");
case CS_E_BADLENGTH: return ("bad lengths");
case CS_E_STACK_OVERFLOW: return ("stack overflow in decomp");
case CS_E_STACK_UNDERFLOW: return ("stack underflow in decomp");
/* only Windows */
case CS_NOT_INITIALIZED: return ("storage not allocated");
/* non error return codes */
case CS_END_INBUFFER: return ("end of input buffer");
case CS_END_OUTBUFFER: return ("end of output buffer");
case CS_END_OF_STREAM: return ("end of data");
/* unknown error */
default: return ("unknown error");
case CS_IEND_OF_STREAM: return ("CS_IEND_OF_STREAM: end of data (internal)");
case CS_IEND_OUTBUFFER: return ("CS_IEND_OUTBUFFER: end of output buffer");
case CS_IEND_INBUFFER: return ("CS_IEND_INBUFFER: end of input buffer");
case CS_E_OUT_BUFFER_LEN: return ("CS_E_OUT_BUFFER_LEN: invalid output length");
case CS_E_IN_BUFFER_LEN: return ("CS_E_IN_BUFFER_LEN: invalid input length");
case CS_E_NOSAVINGS: return ("CS_E_NOSAVINGS: no savings");
case CS_E_INVALID_SUMLEN: return ("CS_E_INVALID_SUMLEN: invalid len of stream");
case CS_E_IN_EQU_OUT: return ("CS_E_IN_EQU_OUT: inbuf == outbuf");
case CS_E_INVALID_ADDR: return ("CS_E_INVALID_ADDR: inbuf == NULL,outbuf == NULL");
case CS_E_FATAL: return ("CS_E_FATAL: internal error !");
case CS_E_BOTH_ZERO: return ("CS_E_BOTH_ZERO: inlen = outlen = 0");
case CS_E_UNKNOWN_ALG: return ("CS_E_UNKNOWN_ALG: unknown algorithm");
case CS_E_UNKNOWN_TYPE: return ("CS_E_UNKNOWN_TYPE: unknown type");
/* for decompress */
case CS_E_FILENOTCOMPRESSED: return ("CS_E_FILENOTCOMPRESSED: input not compressed");
case CS_E_MAXBITS_TOO_BIG: return ("CS_E_MAXBITS_TOO_BIG: maxbits to large");
case CS_E_BAD_HUF_TREE: return ("CS_E_BAD_HUF_TREE: bad hufman tree");
case CS_E_NO_STACKMEM: return ("CS_E_NO_STACKMEM: no stack memory in decomp");
case CS_E_INVALIDCODE: return ("CS_E_INVALIDCODE: invalid code");
case CS_E_BADLENGTH: return ("CS_E_BADLENGTH: bad lengths");
case CS_E_STACK_OVERFLOW: return ("CS_E_STACK_OVERFLOW: stack overflow in decomp");
case CS_E_STACK_UNDERFLOW: return ("CS_E_STACK_UNDERFLOW: stack underflow in decomp");
/* only Windows */
case CS_NOT_INITIALIZED: return ("CS_NOT_INITIALIZED: storage not allocated");
/* non error return codes */
case CS_END_INBUFFER: return ("CS_END_INBUFFER: end of input buffer");
case CS_END_OUTBUFFER: return ("CS_END_OUTBUFFER: end of output buffer");
case CS_END_OF_STREAM: return ("CS_END_OF_STREAM: end of data");
/* custom error */
case CS_E_MEMORY_ERROR: return ("CS_E_MEMORY_ERROR: custom memory error");
/* unknown error */
default: return ("unknown error");
}
}

Expand Down Expand Up @@ -171,7 +177,7 @@ int decompress_packet (const guint8 *in, gint in_length, guint8 *out, guint *out
rt = csObject.CsDecompr(bufin_pos, bufin_rest, bufout_pos, bufout_rest, 0, &bytes_read, &bytes_decompressed);

#ifdef DEBUG
printf("sapdecompress.cpp: Return code %d (%s) (%d bytes read, %d bytes decompressed)\n", rt, error_string(rt), bytes_read, bytes_decompressed);
printf("sapdecompress.cpp: Return code %d (%s) (%d bytes read, %d bytes decompressed)\n", rt, decompress_error_string(rt), bytes_read, bytes_decompressed);
#endif

/* Successful decompression, we've finished with the stream */
Expand Down Expand Up @@ -218,3 +224,16 @@ int decompress_packet (const guint8 *in, gint in_length, guint8 *out, guint *out
return (rt);
};


/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
* :indentSize=8:tabSize=8:noTabs=false:
*/
34 changes: 3 additions & 31 deletions src/sapdecompress.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#ifndef __PACKET_SAPDECOMPRESS_H__
#define __PACKET_SAPDECOMPRESS_H__

#include <epan/value_string.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
Expand All @@ -32,36 +30,10 @@ extern "C" {
#define CS_E_MEMORY_ERROR -99

/* SAP Decompression routine return codes */
static const value_string decompress_return_code_vals[] = {
{ 3, "CS_END_INBUFFER (End of input buffer)" },
{ 2, "CS_END_OUTBUFFER (End of output buffer)" },
{ 1, "CS_END_OF_STREAM (End of data)" },
{ 0, "CS_OK" },
{ -1, "CS_IEND_OF_STREAM (End of data (internal) )" },
{ -2, "CS_IEND_OUTBUFFER (End of output buffer)" },
{ -3, "CS_IEND_INBUFFER (End of input buffer)" },
{ -10, "CS_E_OUT_BUFFER_LEN (Invalid output length)" },
{ -11, "CS_E_IN_BUFFER_LEN (Invalid input length)" },
{ -12, "CS_E_NOSAVINGS" },
{ -13, "CS_E_INVALID_SUMLEN (Invalid len of stream)" },
{ -14, "CS_E_IN_EQU_OUT (inbuf == outbuf)" },
{ -15, "CS_E_INVALID_ADDR (inbuf == NULL,outbuf == NULL)" },
{ -19, "CS_E_FATAL (Internal Error !)" },
{ -20, "CS_E_BOTH_ZERO (inlen = outlen = 0)" },
{ -21, "CS_E_UNKNOWN_ALG (unknown algorithm)" },
{ -22, "CS_E_UNKNOWN_TYPE (unknown type)" },
{ -50, "CS_E_FILENOTCOMPRESSED (Input not compressed)" },
{ -51, "CS_E_MAXBITS_TOO_BIG (maxbits to large)" },
{ -52, "CS_E_BAD_HUF_TREE (bad hufman tree)" },
{ -53, "CS_E_NO_STACKMEM (no stack memory in decomp)" },
{ -54, "CS_E_INVALIDCODE (invalid code)" },
{ -55, "CS_E_BADLENGTH (bad lengths)" },
{ -60, "CS_E_STACK_OVERFLOW (stack overflow in decomp)" },
{ -61, "CS_E_STACK_UNDERFLOW (stack underflow in decomp)" },
{CS_E_MEMORY_ERROR, "CS_E_MEMORY_ERROR (custom error error)" }
};
const char *decompress_error_string(int return_code);

int decompress_packet (const guint8 *in, gint in_length, guint8 *out, guint *out_length);
/* SAP Decompression routine */
int decompress_packet(const guint8 *in, gint in_length, guint8 *out, guint *out_length);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 778a49c

Please sign in to comment.