function biffvector = biffread(filename)
%BIFFREAD read biff vector from xls file

%   $Revision: 1.1 $  $Date: 2000/04/21 19:58:21 $

% try to read the biff file using fread
% if the first byte is a 9, this is an old style (pre excel 95) file.
% otherwise, check if byte 513 is 9, if so, this is a storage file
% and we can just start at byte 513.
% if neither of these are true, try to open the file using the
% storage library.
fid = fopen(filename,'r','l'); % always use little endian for read
if fid < 0
    error(['Unable to read XLS file ' filename '.  Can''t open file.']);
end
biffvector = fread(fid, inf, 'uchar');
fclose(fid);
if length(biffvector) == 0
    error(['Unable to read XLS file ' filename '.  File is empty.']);
end
if biffvector(1) == 9
    % this is a pre excel95 biff file, use it as is
    biffvector = uint8(biffvector);
elseif biffvector(513) == 9 & length(biffvector > 512)
    % this is a structured storage file where the 1st record is a biff record,
    % throw away the 1st 512 bytes
    biffvector = uint8(biffvector(513:end));
else
    error(['Unable to read XLS file ' filename '.  File is not in recognized format.']);
end
