Byte Stuffing Program in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

When transmitting data at the physical layer, bits are sent synchronously from the source to the destination. The data link layer packs these bits into frames. Particularly in computer networks and telecommunications, frames are the basic building blocks of digital transmission.

The packets from the network layer are taken and encapsulated into frames by the data-link layer. A point-to-point connection called framing uses a wire to send data as a stream of bits between two computers or other devices.

In variable-size framing at the data link layer, which requires defining the end of the frame as well as the beginning of the following frame to differentiate; we must specify a method for separating frames. One such method is Byte stuffing where an 8-bit flag ('F') is inserted at the beginning and end of the frame to differentiate it from the next. Therefore, whenever a flag sequence ('F') is encountered, it denotes the start or end of a frame.

This article explains in detail how a Byte stuffing program in C works, including code examples and the mechanism.

Introduction: What is Byte Stuffing?

Byte stuffing (or character stuffing) is a method for converting a message formed of a sequence of bytes that may contain reserved values such as frame delimiters into another byte sequence that does not contain the reserved values.

When there is a character with the same pattern as the flag, a byte usually called the escape character [ESC], with a preset bit pattern is appended to the data part of the frame. When the ESC character is encountered, the receiver removes it from the data section and interprets the following character as data rather than a flag.

The issue arises when the text contains one or more escape characters followed by a flag. To fix this problem, the escape characters that are part of the text are indicated by another escape character, i.e., if the escape character is part of the text, an extra one is added to signify that the second one is part of the text.

How does the Byte Stuffing Program in C works?

The physical layer's stream of bits is split into data frames at the Data Link layer. The data frames can be of constant or variable length. In variable-length framing, the size of each frame to be sent can vary.

In order to distinguish between one frame and the next, a pattern of bits is utilized as a delimiter. However, if the pattern appears in the message, then mechanisms must be included to ensure that this circumstance is prevented.

Every byte in the message is prefixed with an escape character [ESC], which follows the same pattern as the flag byte. A second ESC byte is inserted before the message byte if the ESC sequence is present there. When a receiver encounters the ESC, it removes it from the data section and considers the next character to be data rather than a delimiting flag.

What happens if there are one or more escape characters followed by a flag in the data?

The receiver discards the escape character but retains the flag, which is misinterpreted as the end of the frame. To resolve this issue, the escape characters that are a part of the data must also be identified by another escape character.

A data link frame has the following parts −

1. Frame Header − It includes the source and the destination addresses of the frame.

2. Payload field − It includes the intended message. In Byte stuffing, it is a variable sequence of data bytes.

3. Trailer − It contains the error detection and error correction bits.

4. Flags − Flags are frame delimiters that indicate the beginning and end of a frame. In Byte stuffing, a flag is 1- byte denoting a protocol-dependent unique character.

BYTE STUFFING MECHANISM

Four examples of byte sequences before and after stuffing:

PAYLOAD FIELD

Example of Byte Stuffing Program in C

To further comprehend the mechanism, let's examine the Byte stuffing program in C and output-

C code:

Input:

Output:

Conclusion

  • Byte stuffing is also known as character stuffing or character-oriented framing.
  • Byte stuffing actually operates on bytes whereas bit stuffing operates on bits.
  • In Byte stuffing, a special byte known as ESC (Escape Character) with a specified pattern is added to the data section of the data stream or frame when there is a message or character with the same pattern as the flag byte.
  • Byte stuffing is adding one extra byte when there is an ESC or flag in the text.