view hrutil/tw5b-dump.c @ 585:3c6bf0d26ee7 default tip

TW-TS-005 reader: fix maximum line length bug TW-TS-005 section 4.1 states: The maximum allowed length of each line is 80 characters, not including the OS-specific newline encoding. The implementation of this line length limit in the TW-TS-005 hex file reader function in the present suite was wrong, such that lines of the full maximum length could not be read. Fix it. Note that this bug affects comment lines too, not just actual RTP payloads. Neither Annex A nor Annex B features an RTP payload format that goes to the maximum of 40 bytes, but if a comment line goes to the maximum allowed length of 80 characters not including the terminating newline, the bug will be triggered, necessitating the present fix.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 25 Feb 2025 07:49:28 +0000
parents 707d6f7a54dc
children
line wrap: on
line source

/*
 * This program reads a TW-TS-005 Annex B hexadecimal file
 * and dumps all frames in human-readable form.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../libgsmhr1/tw_gsmhr.h"
#include "../libtest/tw5reader.h"

main(argc, argv)
	char **argv;
{
	FILE *hexf;
	unsigned lineno;
	uint8_t frame[TWTS005_MAX_FRAME];
	unsigned frame_len, ft;
	int16_t params[GSMHR_NUM_PARAMS];
	int rc;

	if (argc != 2) {
		fprintf(stderr, "usage: %s hex-file\n", argv[0]);
		exit(1);
	}
	hexf = fopen(argv[1], "r");
	if (!hexf) {
		perror(argv[1]);
		exit(1);
	}
	lineno = 0;
	for (;;) {
		rc = twts005_read_frame(hexf, &lineno, frame, &frame_len);
		if (rc < 0) {
			fprintf(stderr, "%s line %u: not valid TW-TS-005\n",
				argv[1], lineno);
			exit(1);
		}
		if (!rc)
			break;
		switch (frame_len) {
		case 0:
			printf("line %u: NULL frame\n", lineno);
			break;
		case 1:
			if (frame[0] & 0x80)
				goto invalid;
			ft = frame[0] >> 4;
			switch (ft) {
			case 1:
				printf("line %u: Invalid_SID frame\n", lineno);
				break;
			case 7:
				printf("line %u: No_Data frame\n", lineno);
				break;
			default:
				goto invalid;
			}
			printf("  ToC=%02X (DTXd=%u UFI=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, (frame[0] >> 1) & 1,
				frame[0] & 1);
			break;
		case GSMHR_FRAME_LEN_RPF:
			printf("line %u: TS 101 318 frame\n", lineno);
			gsmhr_unpack_ts101318(frame, params);
			print_frame_params(params);
			break;
		case GSMHR_FRAME_LEN_5993:
			if (frame[0] & 0x80)
				goto invalid;
			ft = frame[0] >> 4;
			switch (ft) {
			case 0:
				printf("line %u: good speech frame\n", lineno);
				break;
			case 2:
				printf("line %u: good SID frame\n", lineno);
				break;
			case 6:
				printf("line %u: bad speech frame\n", lineno);
				break;
			default:
				goto invalid;
			}
			printf("  ToC=%02X (DTXd=%u UFI=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, (frame[0] >> 1) & 1,
				frame[0] & 1);
			gsmhr_unpack_ts101318(frame + 1, params);
			print_frame_params(params);
			break;
		default:
		invalid:
			fprintf(stderr,
				"%s line %u: not a valid GSM-HR frame\n",
				argv[1], lineno);
			exit(1);
		}
	}
	exit(0);
}