locamation-im: Fix encoding

Add strings with proto_tree_add_item or tvb_get_string_enc;
avoid using tvb_get_raw_bytes_as_string.

Use UTF-8 as the encoding to future-proof, according to
Locomation.

Use tvb_find_line_end() to split the lines, which does almost
all the needed logic and simplifies the code.

Fix #18632
This commit is contained in:
John Thacker 2022-11-16 19:42:53 -05:00
parent 4c4b3ec3a9
commit b00725c945
1 changed files with 24 additions and 33 deletions

View File

@ -139,32 +139,26 @@ static const value_string company_pid_vals[] = {
* ########################################################################
*/
static void add_split_lines(tvbuff_t *tvb, int tvb_offset, proto_tree *tree, int hf, char *src_buf, int src_buf_size, gboolean line_numbers) {
char line_buf[ETH_FRAME_LEN + 1];
static void add_split_lines(packet_info *pinfo, tvbuff_t *tvb, int tvb_offset, proto_tree *tree, int hf, gboolean line_numbers) {
char *line_start = src_buf;
char *line_end = src_buf;
int line_start_index = 0;
char *line;
int line_nr = 1;
while ((line_start_index <= (src_buf_size - 1)) && (line_end <= &src_buf[src_buf_size - 1]) && (line_start != NULL)) {
line_end = strchr(line_start, '\n');
gboolean found_line_end = (line_end != NULL);
int line_end_index = found_line_end ? (int)(line_end - src_buf) : src_buf_size;
int line_length = line_end_index - line_start_index;
int offset = tvb_offset;
int next_offset;
int len;
while (tvb_offset_exists(tvb, offset)) {
len = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
if (len == -1) {
break;
}
int line_number_length = 0;
if (line_numbers) {
line_number_length = snprintf(line_buf, sizeof(line_buf), " %2d:", line_nr++);
line = tvb_get_string_enc(pinfo->pool, tvb, offset, len, ENC_UTF_8);
proto_tree_add_string_format_value(tree, hf, tvb, offset, len, line, " %2d:%s", line_nr++, line);
} else {
proto_tree_add_item(tree, hf, tvb, offset, len, ENC_UTF_8);
}
int total_line_length = tvb_get_raw_bytes_as_string(tvb, tvb_offset + line_start_index, &line_buf[line_number_length], line_length + 1);
if (found_line_end) {
total_line_length++;
}
proto_tree_add_string(tree, hf, tvb, tvb_offset + line_start_index, total_line_length, line_buf);
line_start = line_end + 1;
line_start_index = line_end_index + 1;
offset = next_offset;
}
}
@ -265,24 +259,22 @@ static int dissect_calibration(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tr
tvb_offset += item_size;
/* Name */
char name_buf[ETH_FRAME_LEN + 1];
int name_length = tvb_get_raw_bytes_as_string(tvb, tvb_offset, name_buf, sizeof(name_buf));
proto_item *name_item = proto_tree_add_string(calibration_item_subtree, hf_calibration_name, tvb, tvb_offset, name_length, name_buf);
int name_length = tvb_reported_length_remaining(tvb, tvb_offset);
proto_item *name_item = proto_tree_add_item(calibration_item_subtree, hf_calibration_name, tvb, tvb_offset, name_length, ENC_UTF_8);
/* Name - Lines */
proto_tree *name_item_subtree = proto_item_add_subtree(name_item, hst_calibration_lines);
add_split_lines(tvb, tvb_offset, name_item_subtree, hf_calibration_name_line, name_buf, name_length, FALSE);
add_split_lines(pinfo, tvb, tvb_offset, name_item_subtree, hf_calibration_name_line, FALSE);
} else {
/* Chunk Packet */
/* Chunk */
char chunk_buf[ETH_FRAME_LEN + 1];
int chunk_length = tvb_get_raw_bytes_as_string(tvb, tvb_offset, chunk_buf, sizeof(chunk_buf));
proto_item *chunk_item = proto_tree_add_string(calibration_item_subtree, hf_calibration_chunk, tvb, tvb_offset, chunk_length, chunk_buf);
int chunk_length = tvb_reported_length_remaining(tvb, tvb_offset);
proto_item *chunk_item = proto_tree_add_item(calibration_item_subtree, hf_calibration_chunk, tvb, tvb_offset, chunk_length, ENC_UTF_8);
/* Chunk - Lines */
proto_tree *chunk_item_subtree = proto_item_add_subtree(chunk_item, hst_calibration_lines);
add_split_lines(tvb, tvb_offset, chunk_item_subtree, hf_calibration_chunk_line, chunk_buf, chunk_length, FALSE);
add_split_lines(pinfo, tvb, tvb_offset, chunk_item_subtree, hf_calibration_chunk_line, FALSE);
}
return tvb_captured_length(tvb);
@ -324,13 +316,12 @@ static int dissect_ident(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, vo
proto_tree *ident_item_subtree = proto_item_add_subtree(ident_item, hst_protocol_ident);
/* Contents */
char contents_buf[ETH_FRAME_LEN + 1];
int contents_length = tvb_get_raw_bytes_as_string(tvb, 0, contents_buf, sizeof(contents_buf));
proto_item *contents_item = proto_tree_add_string(ident_item_subtree, hf_ident_contents, tvb, 0, contents_length, contents_buf);
int contents_length = tvb_reported_length_remaining(tvb, 0);
proto_item *contents_item = proto_tree_add_item(ident_item_subtree, hf_ident_contents, tvb, 0, contents_length, ENC_UTF_8);
/* Contents - Lines */
proto_tree *contents_item_subtree = proto_item_add_subtree(contents_item, hst_ident_lines);
add_split_lines(tvb, 0, contents_item_subtree, hf_ident_contents_line, contents_buf, contents_length, FALSE);
add_split_lines(pinfo, tvb, 0, contents_item_subtree, hf_ident_contents_line, FALSE);
return tvb_captured_length(tvb);
}