Table of Contents
IronForgeCyber
Courses
Resources
Blog
Log In


IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. television
9 min read

TeLeVision

Problem Description

We will write a seeking algorithm that takes in the file data.bin, which contains a specialized array that has been serialized. The algorithm will read the array according to its rules. The flag can be printed out by printing the first 8, middle 8, and last 8 characters of the string made using the below constraints.

You have been provided data.bin, which contains the serialized array. The array is a TLV (Tag-Length-Value) array, where each array contains a Tag, Length, and Value. Each element takes the following form:

| T | L |  Value  |
| 0 | 1 | 2 | ... |

The parts of the array entry are:

  • Tag: An identifier used to classify the incoming data type.
  • Length: The length of the value.
  • Value: The value of the data.

You should store all the array entries with prime number tags in a string. No delimiters should be used in between entries.

The flag is generated by printing the first 8, middle 8, and last 8 characters of the string created. Hint: The string is even in length.

The flag will be represented in ASCII. Remember to surround your data in flag{} braces.

Stuck? Open one hint at a time before reading the walkthrough.

Hint 1
Each entry is a tag, a length, then length bytes of value. Read the tag and the length first, then exactly that many bytes.
Hint 2
Keep only the values whose tag is prime, appended to one big string with no separators.
Hint 3
To test for a prime, you only need to try divisors up to sqrt(n).
Hint 4
The flag is the first 8, the middle 8 and the last 8 characters. The string's length is even, so the middle 8 start 4 before its center.

Walkthrough

You'll notice that this writeup won't define a struct. In this case, we don't need to. Because we only need to store the value of the items that satisfy a condition (being prime), we can store the values we find.

Reading the Data

For this challenge, we need to read the data as normal:

  1. Open the file and ensure it's properly opened
  2. Initialize the container
  3. Read the data using an infinite while loop

First, we open the file. Nothing new here.

c
1FILE *fp = fopen("data.bin", "rb");
2if (fp == NULL)
3{

Then, we initialize the container. The instructions said to store the entries in a string, so I made a large string. It's infeasible to predict the number of elements for two reasons:

  1. The size of each ArrayItem is based on length
  2. We don't know which of the tags are prime

I chose an arbitrarily large number (10000). The file is only 8.6 kilobytes, so this number is more than large enough.

c
char *string = malloc(10000 * sizeof(char));

Now, we read the data. Since we don't know the number of elements to read, we use an infinite while loop. Rather than tracking the number of elements read, we track the index we are at in the string so we know where to write for each iteration.

When we read the data, we must check if a number is prime at the time of reading. If the number is prime, we should copy it from the variable into the string, starting at index.

c
1while (1)
2{
3    uint8_t tag;
4    uint8_t length;

Defining the isPrime function is simple. We simply can check if the number is divisible by any number between 2 and the number itself. To optimize this further, you only need to check between 2 and sqrt(n). You'll need the math package for this.

c
1int isPrime(uint8_t tag)
2{
3    if (tag < 2)
4        

Surprisingly, this is all the processing we need for this section. We can move on to printing the flag.

Printing the Flag

Printing the flag is a bit difficult. We need the string's first, middle, and last 8 characters. We're provided the hint that the string has an even length so that we can use this to our advantage. Let's think about the format of a small string:

0  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
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
|----- First 8 ------|     |----- Middle 8 -----|    |------ Final 8 ------|

How can we print these out?

  • For the first 8, we can iterate starting at index 0 and go 8 elements.
  • For the last 8, we can count backward 8 from the end of the string and start there.
  • For the middle 8, we need to find the middle of the string, move 8/2 to the left, and then iterate 8 elements.

Here's how we can make this work. I chose to use a variable for the characters per section (cps) to have more expandable code.

c
1uint8_t cps = 8;
2
3printf("flag{");
4for (uint8_t

Running this gets the flag:

$ gcc -o solve solve.c && ./solve && rm solveflag{FORGED_TLV_TELEVISION_42}

Full Solution

Here is the full solution:

solve.c
1#include <stdint.h>
2#include <stdio.h>
4 printf("File not found.\n");
5 exit(EXIT_FAILURE);
6}
5 char *value;
6
7 uint8_t bytes = fread(&tag, sizeof(tag), 1, fp);
8 if (bytes == 0)
9 break;
10
11 fread(&length, sizeof(length), 1, fp);
12
13 value = malloc(length * sizeof(char));
14 fread(value, length, 1, fp);
15
16 if (isPrime(tag))
17 {
18 for (uint8_t i = 0; i < length; ++i)
19 string[index + i] = value[i];
20 index += length;
21 }
22 free(value);
23}
return
0
;
5
6 for (uint8_t i = 2; i <= tag / 2; ++i)
7 if (tag % i == 0)
8 return 0;
9
10 return 1;
11}
i
=
0
;
i
<
cps
;
++
i
)
5 printf("%c", string[i]);
6for (uint8_t i = 0; i <= 7; ++i)
7 printf("%c", string[strlen(string) / 2 - cps / 2 + i]);
8for (int16_t i = cps; i > 0; --i)
9 printf("%c", string[strlen(string) - i]);
10printf("}\n");
3
#
include
<stdlib.h>
4#include <string.h>
5
6int isPrime(uint8_t tag)
7{
8 if (tag < 2)
9 return 0;
10
11 for (uint8_t i = 2; i <= tag / 2; ++i)
12 if (tag % i == 0)
13 return 0;
14
15 return 1;
16}
17
18int main()
19{
20 FILE *fp = fopen("data.bin", "rb");
21 if (fp == NULL)
22 {
23 printf("Error opening file\n");
24 exit(EXIT_FAILURE);
25 }
26
27 char *string = malloc(10000 * sizeof(char));
28
29 uint32_t index = 0;
30 while (1)
31 {
32 uint8_t tag;
33 uint8_t length;
34 char *value;
35
36 uint8_t bytes = fread(&tag, sizeof(tag), 1, fp);
37 if (bytes == 0)
38 break;
39
40 fread(&length, sizeof(length), 1, fp);
41
42 value = malloc(length * sizeof(char));
43 fread(value, length, 1, fp);
44
45 if (isPrime(tag))
46 {
47 for (uint8_t i = 0; i < length; ++i)
48 string[index + i] = value[i];
49 index += length;
50 }
51 free(value);
52 }
53
54 string[index] = '\0';
55
56 uint8_t cps = 8;
57
58 printf("flag{");
59 for (uint8_t i = 0; i < cps; ++i)
60 printf("%c", string[i]);
61 for (uint8_t i = 0; i <= 7; ++i)
62 printf("%c", string[strlen(string) / 2 - cps / 2 + i]);
63 for (int16_t i = cps; i > 0; --i)
64 printf("%c", string[strlen(string) - i]);
65 printf("}\n");
66
67 return 0;
68}