memcpy( dest, file->data+startOffset, bytesRead );
if ( bytesRead ) { memcpy( dest, file->data+startOffset, bytesRead ); #ifdef DEBUG_dveditz file->reads++; #endif }
PRUint32 bufio_Read(BufioFile* file, char* dest, PRUint32 count){    PRInt32     startOffset;    PRInt32     endOffset;    PRInt32     leftover;    PRUint32    bytesCopied;    PRUint32    bytesRead;    PRUint32    retcount = 0;    /* sanity check arguments */    if ( !file || !dest || count == 0 || file->fpos >= file->fsize )        return 0;    /* Adjust amount to read if we're near EOF */    if ( (file->fpos + count) > (PRUint32)file->fsize )        count = file->fsize - file->fpos;    /* figure out how much of the data we want is already buffered */    startOffset = file->fpos - file->datastart;    endOffset = startOffset + count;    if ( startOffset >= 0 && startOffset < file->datasize )    {        /* The beginning of what we want is in the buffer  */        /* so copy as much as is available of what we want */        if ( endOffset <= file->datasize )            bytesCopied = count;        else            bytesCopied = file->datasize - startOffset;        memcpy( dest, file->data + startOffset, bytesCopied );        retcount = bytesCopied;        file->fpos += bytesCopied;        /* Was that all we wanted, or do we need to get more? */        leftover = count - bytesCopied;        PR_ASSERT( leftover >= 0 );     /* negative left? something's wrong! */        if ( leftover )        {            /* need data that's not in the buffer */            /* if what's left fits in a buffer then load the buffer with the */            /* new area before giving the data, otherwise just read right    */            /* into the user's dest buffer */            if ( _bufio_loadBuf( file, leftover ) )            {                startOffset = file->fpos - file->datastart;                /* we may not have been able to load as much as we wanted */                if ( startOffset+leftover <= file->datasize )                    bytesRead = leftover;                else                    bytesRead = file->datasize - startOffset;                memcpy( dest+bytesCopied, file->data+startOffset, bytesRead );                file->fpos += bytesRead;                retcount += bytesRead;            }            else             {                /* we need more than we could load into a buffer, so */                /* skip buffering and just read the data directly    */                if ( fseek( file->fd, file->fpos, SEEK_SET ) == 0 )                {                    bytesRead = fread(dest+bytesCopied, 1, leftover, file->fd);                    file->fpos += bytesRead;                    retcount += bytesRead;                }                else                 {                    /* XXX seek failed, couldn't load more data -- help! */                    /* should we call PR_SetError() ? */                }            }        }    }    else    {        /* range doesn't start in the loaded buffer but it might end there */        if ( endOffset > 0 && endOffset <= file->datasize )            bytesCopied = endOffset;        else            bytesCopied = 0;        leftover = count - bytesCopied;        if ( bytesCopied )        {            /* the tail end of the range we want is already buffered */            /* first copy the buffered data to the dest area         */            memcpy( dest+leftover, file->data, bytesCopied );        }                    /* now pick up the part that's not already in the buffer */        if ( _bufio_loadBuf( file, leftover ) )        {            /* we were able to load some data */            startOffset = file->fpos - file->datastart;            /* we may not have been able to read as much as we wanted */            if ( startOffset+leftover <= file->datasize )                bytesRead = leftover;            else                bytesRead = file->datasize - startOffset;            memcpy( dest, file->data+startOffset, bytesRead );        }        else        {            /* leftover data doesn't fit so skip buffering */            if ( fseek( file->fd, file->fpos, SEEK_SET ) == 0 )                bytesRead = fread(dest, 1, leftover, file->fd);            else                bytesRead = 0;        }        /* if we couldn't read all the leftover, don't tell caller */        /* about the tail end we copied from the first buffer      */        if ( bytesRead == (PRUint32)leftover )            retcount = bytesCopied + bytesRead;        else            retcount = bytesRead;        file->fpos += retcount;    }    return retcount;}