In framing for the HDLC protocol, assume the receiver receives the following string of bits:
[…] 011111010 […]
How can it realize whether the sequence “011111010” is real data, or the result of a bit stuffing operation from an original flag-like sequence “01111110”?
how can the receiver recognize the two different options?
Simply. There are no two options. The sequence
01111110 (0x7E)
is not allowed for the original data in the frame, i.e. it is reserved for the flags. So, while bit-stuffing, only data bit sequences are affected, but not flag bit sequences. Such any sequence the receiver sees with 5 ones in a row is definitely not a flag sequence, which has kept its successive 6 ones.-- (reference see below)
Note that any data bit sequence of pattern [x zeroes] [5 ones] [y zeroes] is stuffed to [x zeroes] [5 ones] [y+1 zeroes]. So you will receive e.g. a data sequence of
01111101
as011111001
, one of011111001
as0111110001
and so on. The receiver knows that this happens on sender-side and removes any additional zero after 5 ones at unstuffing.And when byte-stuffing any
0x7E
found in the data is replaced by the 2-byte0x7D5E
, whereby0x7D
serves as an escape character, before being put into the frame. Again, the flag sequences remain untouched.See K.R.Fall & W.R.Stevens (2012), TCP/IP Illustrated Volume 1: The Protocols, 2nd Ed., Addison-Wesley, pp.131 for the whole procedure: