2
$\begingroup$

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?

$\endgroup$
3
$\begingroup$

Think of a message (ending in) FLAG. You need to escape it, of course, so your message looks like: FLAG (rest) ESC escaped-FLAG FLAG

Now think of a message (ending in) ESC. If you do not escape it, your message looks like this: FLAG (rest) ESC FLAG

Now, “ESC FLAG” and “ESC escaped-FLAG” are undistinguishable. So, that last ESC needs to be escaped.

Now you can go backwards, again and again, until you find that, if one ESC has to be escaped, they all need to be escaped, from the first one.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.