Denoting the two special bytes as FLAG and ESC, and any other as X, where FLAG denotes the start and end of the message and ESC is used to escape any flags within the original message, if the original message was for example,
X FLAG ESC FLAG FLAG ESC ESC FLAG X X
this can be byte stuffed as,
FLAG X ESC FLAG ESC ESC FLAG ESC FLAG ESC ESC ESC FLAG X X FLAG
where the added bytes are in bold. I don't think it is necessary to escape the escapes. In the receiving side, only FLAG comming after ESC together will be changed to FLAG and when any other bytes come after ESC, that ESC will be ESC as is. For example, if you see the ESC byte at the 5th byte of the received message, since ESC comes after ESC, the 5th ESC will be interpreted as ESC and nothing more. I thought about this, and I think it also works this way. But every resources I can find including my networks book says that an escape should be escaped as well as the flags when originally contained in the message. Why so?