patternMinor
Why escape and escape in byte stuffing?
Viewed 0 times
whystuffingbyteandescape
Problem
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,
this can be byte stuffed as,
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?
if the original message was for example,
X FLAG ESC FLAG FLAG ESC ESC FLAG X Xthis can be byte stuffed as,
FLAG X ESC FLAG ESC ESC FLAG ESC FLAG ESC ESC ESC FLAG X X FLAGwhere 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?
Solution
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.
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.
Context
StackExchange Computer Science Q#33684, answer score: 4
Revisions (0)
No revisions yet.