Pasted as Plain Text [Remove this paste ]
Description: No description
URL: http://bcas.tv/paste/results/BYvBA529.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bitstream.h"
 
#undef WRITE_OUT
 
typedef struct {
	uint8_t Sync;
	uint8_t Type;
	uint8_t Length[2];
} TLV_Header_t;
 
static uint8_t buf[65536];
static uint64_t TLVIndex;
static uint8_t tmp[64];
static uint64_t PacketCountsPerCID[16] = { 0, };
 
static void MMTP_MPU(BitStream_t *bs, uint16_t CID, uint16_t PacketId)
{
	uint16_t PayloadLength;
	uint8_t FragmentType;
	bool HasTimeData;
	uint8_t DivisionIndex;
	bool HasAggregate;
	uint8_t DivisionNumberCounter;
	uint32_t MpuSequenceNumber;
 
	BS_PopU16(bs, &PayloadLength);
	BS_PopUInt(bs, 4, &FragmentType, sizeof(FragmentType));
	BS_PopUInt(bs, 1, &HasTimeData, sizeof(HasTimeData));
	BS_PopUInt(bs, 2, &DivisionIndex, sizeof(DivisionIndex));
	BS_PopUInt(bs, 1, &HasAggregate, sizeof(HasAggregate));
	BS_PopU8(bs, &DivisionNumberCounter);
	BS_PopU32(bs, &MpuSequenceNumber);
 
	printf("   MMTP_MPU PayloadLength=%d, DivisionIndex=%d, DivisionNumberCounter=%d\n", PayloadLength, DivisionIndex, DivisionNumberCounter);
 
}
 
static void MMTP_CA_Message(BitStream_t *bs)
{
	uint16_t MessageId;
	uint8_t version;
	uint32_t MessageLength;
 
	BS_PopU16(bs, &MessageId);
	BS_PopU8(bs, &version);
	BS_PopU32(bs, &MessageLength);
 
	printf("   MMTP_CA %04x, %08x\n", MessageId, MessageLength);
 
	// table
	BitStream_t SubBS;
	BS_GetSubStream(bs, &SubBS, BS_GetRemainingBytes(bs));
 
	while (!BS_Eof(&SubBS)) {
		uint8_t TableId;
		uint8_t TableVerson;
		uint16_t TableLength;
 
		BS_PopU8(&SubBS, &TableId);
		BS_PopU8(&SubBS, &TableVerson);
		BS_PopU16(&SubBS, &TableLength);
 
		printf("    MMTP_CA_Table %02x (%02x) - %04x\n", TableId, TableVerson, TableLength);
		BS_Skip(&SubBS, TableLength * 8);
	}
}
 
static void MMTP_PA_Message(BitStream_t *bs)
{
	int i;
	uint16_t MessageId;
	uint8_t version;
	uint32_t MessageLength;
 
	BS_PopU16(bs, &MessageId);
	BS_PopU8(bs, &version);
	BS_PopU32(bs, &MessageLength);
 
	uint8_t TableNumber;
	BS_PopU8(bs, &TableNumber);
 
	// repeat
	for (i = 0; i < TableNumber; i++)
		BS_Skip(bs, 8 + 8 + 16);
 
	BitStream_t SubBS;
	BS_GetSubStream(bs, &SubBS, BS_GetRemainingBytes(bs));
 
	printf("   MMTP_PA %04x, %08x\n", MessageId, MessageLength);
 
	// table
	while (!BS_Eof(&SubBS)) {
		uint8_t TableId;
		uint8_t TableVerson;
		uint16_t TableLength;
 
		BS_PopU8(&SubBS, &TableId);
		BS_PopU8(&SubBS, &TableVerson);
		BS_PopU16(&SubBS, &TableLength);
 
		printf("    MMTP_PA_Table %02x (%02x) - %04x\n", TableId, TableVerson, TableLength);
		BS_Skip(&SubBS, TableLength * 8);
	}
}
 
static void MMTP_ControlMessage(BitStream_t *bs, uint16_t PacketId)
{
	uint8_t DivisionIndex;
	bool HasLengthInformation;
	bool HasAggregate;
 
	BS_PopUInt(bs, 2, &DivisionIndex, sizeof(DivisionIndex));
	BS_Skip(bs, 4);
	BS_PopUInt(bs, 1, &HasLengthInformation, sizeof(HasLengthInformation));
	BS_PopUInt(bs, 1, &HasAggregate, sizeof(HasAggregate));
 
	uint8_t DivisionNumberCounter;
	BS_PopU8(bs, &DivisionNumberCounter);
 
	BitStream_t SubBS;
	BS_GetSubStream(bs, &SubBS, BS_GetRemainingBytes(bs));
 
	if (PacketId == 0x0000) {
		// PA Message
		MMTP_PA_Message(&SubBS);
 
	} else if (PacketId == 0x0001) {
		// CA Message
		MMTP_CA_Message(&SubBS);
	}
 
	return;
}
 
static void MMTP_packet(BitStream_t *bs, uint16_t CID)
{
	bool HasPacketCounter;
	bool HasExtensionFlag;
	uint8_t PayloadType;
 
	BS_Skip(bs, 2); // version
	BS_PopUInt(bs, 1, &HasPacketCounter, sizeof(HasPacketCounter));
	BS_Skip(bs, 2 + 1);
	BS_PopUInt(bs, 1, &HasExtensionFlag, sizeof(HasExtensionFlag));
	BS_Skip(bs, 1 + 2);
	BS_PopUInt(bs, 6, &PayloadType, sizeof(PayloadType));
 
	uint16_t PacketId;
	uint32_t TimeStamp;
	uint32_t PacketSequenceNumber;
 
	BS_PopU16(bs, &PacketId);
	BS_PopU32(bs, &TimeStamp);
	BS_PopU32(bs, &PacketSequenceNumber);
 
	if (HasPacketCounter) {
		uint32_t PacketCounter;
		BS_PopU32(bs, &PacketCounter);
	}
	if (HasExtensionFlag) {
		uint16_t ExtensionType;
		uint16_t ExtensionLength;
 
		BS_PopU16(bs, &ExtensionType);
		BS_PopU16(bs, &ExtensionLength);
		BS_Skip(bs, ExtensionLength * 8);
	}
 
	printf("  MMTP ID:%04x PayLoadType:%02x TS:%08x SN:%08x\n", PacketId, PayloadType, TimeStamp, PacketSequenceNumber);
 
#ifdef WRITE_OUT
	const uint8_t *Payload = NULL;
	size_t Length;
	Payload = BS_GetCurrentPtr(bs);
	Length = BS_GetRemainingBytes(bs);
 
	sprintf(tmp, "%08llu-%04x-%04x.bin", TLVIndex, CID, PacketId);
	FILE *fp = fopen(tmp, "wb");
	fwrite(Payload, 1, Length, fp);
	fclose(fp);
#endif
 
	BitStream_t SubBS;
	BS_GetSubStream(bs, &SubBS, BS_GetRemainingBytes(bs));
 
	if (PayloadType == 0x00) {
		// MPU
		MMTP_MPU(&SubBS, PacketId, CID);
	} else if (PayloadType == 0x02) {
		// Control Message
		MMTP_ControlMessage(&SubBS, PacketId);
	}
}
 
int main(int argc, char **argv)
{
	FILE *fp;
 
	if (argc < 2) {
		printf("missing filename\n");
		return -1;
	}
 
	fp = fopen(argv[1], "rb");
 
	TLV_Header_t *pHeader = NULL;
 
	while (!feof(fp)) {
		if (!fread(buf, 1, 4, fp))
			break;
 
		pHeader = (TLV_Header_t *)buf;
		uint16_t Length = pHeader->Length[0] << 8 | pHeader->Length[1];
		if (pHeader->Sync != 0x7F) {
			printf("TLV desync at %llu, exiting\n", _ftelli64(fp));
			break;
		}
		printf("TLV_Header: %02x, Length: %04x\n", pHeader->Type, Length);
		fread(buf + 4, 1, Length, fp);
#if 0
		printf("[ ");
		for (int i = 0; i < Length + 4; i++)
			printf("%02x ", buf[i]);
		printf("]\n");
#endif
 
		if (pHeader->Type == 0x03) {
			// HCfB
			BitStream_t bs;
			uint16_t CID;
			uint8_t SequenceNumber;
			uint8_t HeaderType;
 
			BS_Init(&bs, buf + 4, Length);
			BS_PopUInt(&bs, 12, &CID, sizeof(CID));
			BS_PopUInt(&bs, 4, &SequenceNumber, sizeof(SequenceNumber));
			BS_PopU8(&bs, &HeaderType);
 
			static uint8_t SavedSequenceNumber[16] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
			if (SavedSequenceNumber[CID] == 0xff) {
				// first run
				SavedSequenceNumber[CID] = SequenceNumber;
			} else {
				uint8_t ExpectedSequenceNumber;
				ExpectedSequenceNumber = (SavedSequenceNumber[CID] + 1) & 0xf;
				SavedSequenceNumber[CID] = SequenceNumber;
 
				if (ExpectedSequenceNumber != SequenceNumber)
					printf(" HCfB Discontinuity (received %d, expected %d)\n", SequenceNumber, ExpectedSequenceNumber);
			}
 
			printf(" HCfB %04x, SN=%02x, HeaderType=%02x\n", CID, SequenceNumber, HeaderType);
 
			if (HeaderType == 0x61) {
				BitStream_t SubBS;
				BS_GetSubStream(&bs, &SubBS, BS_GetRemainingBytes(&bs));
				MMTP_packet(&SubBS, CID);
				PacketCountsPerCID[CID]++;
			}
		}
		TLVIndex++;
	}
 
	fclose(fp);
 
	return 0;
}