[futurebasic] Offsets within handles

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 1999 : Group Archive : Group : All Groups

From: "Michael Evans" <mikonic@...>
Date: Mon, 15 Nov 1999 09:58:02 -0500
Hi.

Much of what I do revolves around reading parts of image files for chunks of
data which contain a number of sequential fields. The format for each field
contains a field identifier followed by a dataSize (two-byte) word
describing the number of bytes of data to follow.

So I read the data chunk into a handle and then walk the handle looking for
a particular field. (These data chunks are never bigger than 5KB and most
often are less than 1K. The data in the fields range in size from 0 bytes
(no data in this field) to 2000 bytes)

Everything works ok if the dataSize word is correct or too small.

But every once in a while a file is not written correctly and the dataSize
word is too big, which can cause me to read beyond the end of the data chunk
handle, for example if the the last field's dataSize word is too big.

I have found that reading beyond the end of a valid handle can, in fact,
cause all sorts of problems with FB II, the debugger and the Finder and/or
the System file... i.e. that reading certain random memory locations is not
always benign...

So, I have written the FN below to check that the calculated handle offset
of the last data byte in each field is within the data chunk handle.

Q1: Should I lock and unlock the handle within this FN?

Q2: Can this be made faster? (or does it matter?)

CLEAR LOCAL
LOCAL FN HandleOffsetIsValid%(aHandle&, anOffset&)
  DIM offsetIsValid%, hStart&, hEnd&, hSize&

  LONG IF aHandle&
    hSize& = FN GETHANDLESIZE(aHandle&)
    LONG IF hSize& > 0
      hStart& = [aHandle&] + 0: hEnd&   = hStart& + hSize& -1
      LONG IF (anOffset& => hStart&) AND (anOffset& <= hEnd&)
        offsetIsValid% = _true
      XELSE
        offsetIsValid% = _false
      END IF
    XELSE
      offsetIsValid% = _false
    END IF
  XELSE
    offsetIsValid% = _false
  END IF

END FN = offsetIsValid%

Now, I would call FN HandleOffsetIsValid% somthing like this:

LONG IF ChunkHandle&
  fieldStartOffset& = [ChunkHandle&] + an offset......
  recordID%         = PEEK WORD (fieldStartOffset&)'0 and 1
  fieldNum%         = PEEK      (fieldStartOffset& + 2)'2
  dataSize%         = PEEK WORD (fieldStartOffset& + 3)'3 and 4
  LONG IF (datasize% => 0) AND (datasize% <= 2000)
    dataStartOffset&  = fieldStartOffset& + 5
    dataEndOffset&    = dataStartOffset&  + datasize% -1

    LONG IF FN HandleOffsetIsValid%(ChunkHandle&, dataEndOffset&)
      'dataEndOffset& is within ChunkHandle&
      'read the data here
    XELSE
      'dataEndOffset& is outside of ChunkHandle&
      'bail out
    END IF
  XELSE
    'invalid data size
    'bail out
  END IF
XELSE
  'invalid handle
  'bail out
END IF



Cheers,

----------------------------------------------------------
Michael Evans
Manager of Software Development * Photo Systems, Inc.
3301 Wood Valley Road, NW * Atlanta, GA, 30327-1515
Voice: (404) 846-9386
Fax: (404) 240-0878 * Cell: (404) 229-3930
E-mail: evans@... * michael_evans@...
----------------------------------------------------------