From doug@seismo.berkeley.edu Thu Oct 23 20:54:04 1997
From: Doug Neuhauser <doug@seismo.berkeley.edu>
Date: Thu, 23 Oct 1997 20:53:55 -0700
To: dietz@andreas.wr.usgs.gov, alex@andreas.wr.usgs.gov
Subject: bug in import_generic
Cc: doug@seismo.berkeley.edu, oppen@alum.wr.usgs.gov

Lynn and Alex,

I believe there is a bug in the unpacking code in MessageReceiver()
in import_generic.  If this is the code used for other binary import
program, they will all be in error.

Consider the following raw message fragments below.  In particular,
notice that the final character in the first message is an <esc> char:

msg 1:			... x y z <esc>
msg 2:			a b c ...

When packed into a message by binEscape, it generates:

quoted msg 1:		... x y z <esc> <esc>
quoted msg 2:		a b c ...

When encapsulated into an EW message, you get:

encapsulated msg 1:	... x y z <esc> <esc> <etx>
encapsulated msg 2:	<stx> a b c ...

When processing the message fragments above, the code correctly identifies
"<esc> <esc>" as a quoted escape string, and puts a single <esc> into the
unpacked message buffer, but then it incorrectly identifies the <etx> 
as a quoted <etx>, and puts <etx> into the unpakced message buffer.
It then encounters the <stx> and flags it as an unquoted <stx>, which is
an error.

I believe the following code in state 2:

               /* Is this the end?
                *******************/
                if (chr==endCharacter && lastChr != escape)   /* naked end character: end of the message is at hand */

should be:

               /* Is this the end?
                *******************/
                if (chr==endCharacter)   /* naked end character: end of the message is at hand */

At the top of the loop for state 2, if you ever find an <etx> (or
<stx>) it will be unquoted by definition.  You don't want to check
whether the previous character was an escape -- it is irrelevant as
long as you always process the next character immediately within the
loop when you find an <esc>. 

You need to think about what the invariant relations are for the
loop, and it becomes obvious.

- Doug N
