Re: nibtools, GCR, G64

From: silverdr_at_wfmh.org.pl
Date: Fri, 31 May 2013 10:42:19 +0200
Message-Id: <3AD7F5CA-C9F3-4BF4-9DB6-ADF07F848145@wfmh.org.pl>
On 2013-05-31, at 03:16, Pete Rittwage wrote:

>> The question is what if we have something like:
>> 
>> buffer[i] = 0x1f;
>> buffer[i+1] = 0xf8;
>> 
>> or any of other possible two-byte combinations [*] that still constitute a
>> valid SYNC mark (10 consecutive bits set) but the first one is not equal
>> to 0xff?
>> 
>> * - 0x7f/0xfe, 0x07/0xfe, ... and so on.
> 
> I see what you are asking now, and you are absolutely correct.  There is a
> flaw here that I overlooked when I quickly hacked that routine together. 
> It can miss up to a perfectly constructed 14 bit sync.  :)

Precisely ;-)

> Extra credit for whomever on the list can fix it up nicely.  The code is
> open source, although only a couple people have ever stepped up with any
> help in 10 years.  :<

I can understand that. First factor is that this is a highly specific and specialised toolchain, not really for everyone and his dog, second that the underlying contexts are rather complex and not very easy to understand. Combine the two and the result is as you mentioned above.

This is yet unproven concept so might be completely off, but so far I did like this I search for SYNC in a function:

SYNC search over the buffer:
=============================
if (*byte0 == 0xff && *byte1 & 0xc0 == 0xc0)	// 8 bits in first byte, 2 bits in the second
	break;
else if (*byte0 & 0x7f == 0x7f && *byte1 & 0xe0 == 0xe0)	// 7 bits in first byte, 3 bits in the second
	break;
else if (*byte0 & 0x3f == 0x3f && *byte1 & 0xf0 == 0xf0)	// 6 bits in first byte, 4 bits in the second
	break;
else if (*byte0 & 0x1f == 0x1f && *byte1 & 0xf8 == 0xf8)	// 5 bits in first byte, 5 bits in the second
	break;
else if (*byte0 & 0x0f == 0x0f && *byte1 & 0xfc == 0xfc)	// 4 bits in first byte, 6 bits in the second
	break;
else if (*byte0 & 0x07 == 0x07 && *byte1 & 0xfe == 0xfe)	// 3 bits in first byte, 7 bits in the second
	break;
else if (*byte0 & 0x03 == 0x03 && *byte1 == 0xff)	// 2 bits in first byte, 8 bits in the second
	break;
else if (*byte0 & 0x01 == 0x01 && *byte1 == 0xff && *byte2 & 0x80 == 0x80)	// 1 bit in first byte, 8 bits in the second, 1 bit in the third
	break;

and I return pointer to the /first/ byte where the SYNC starts. This is of course much more complex than

while(!((buffer[i+j] == 0xff) && ((buffer[i+j+1] & 0x80) == 0x80))) { j++; bytes++; if(i+j>length) break; }

but should be able to catch all combinations rather than only those starting with 0xff.

Then in another place:

Before shift aligning (var names achanged for clarity):
========================================================

// We skip the first byte as it can't contain anything else than first part of SYNC
bufferPointer = firstSyncBytePointer + 1;

while (*bufferPointer = 0xff)	// and we skip all fully set SYNC bytes too
	bufferPointer++;

// Here we start shifting at the last SYNC/first non-SYNC byte
[...]

-- 
SD!
       Message was sent through the cbm-hackers mailing list
Received on 2013-05-31 09:00:04

Archive generated by hypermail 2.2.0.