unit type_fixes; {$MODE OBJFPC} interface uses classes {$IFNDEF WIN32}, BaseUNIX{TSize} {$ELSE}, Windows{TSize} {$ENDIF} ; type TWideString = WideString; TUTF8String = UTF8String; TBoolean = Boolean; TUINT16 = Word; TUINT32 = DWord; TINT32 = Integer; // ??? TInteger = Integer; TINT16 = ShortInt; TINT64 = Int64; TCardinal = Cardinal; TByte = Byte; TByteArray = array of TByte; TUINT8 = Byte; TINT8 = ShortInt; TString = String; TStringArray = array of TString; TFOURCC = type TUINT32; // array[0..3] of Char; {$IFDEF WIN32} TSize = Cardinal; // FIXME. {$ELSE} TSize = BaseUNIX.TSize; {$ENDIF} // 2D graphics: TCoordinate = Longint; // C 'long'. // Coordinate of point between pixmap points. TPointP = ^TPoint; {$PACKRECORDS C} TPoint = record // note that this should be the same size as WIN32 record 'POINT'. X : TCoordinate; Y : TCoordinate; end; TPointArray = array of TPoint; TAngle = Single; { in Radians. } TFixed16_16 = type TUINT32; TFixed32 = TFixed16_16; TLongDateTime = type INT64; procedure SkipJunk(aStream : TStream; aCount : TINT64); function CopyFrom(Source: TStream; Destination : TStream): Int64; function FOURCCToStr(aValue : TFOURCC) : String; function FOURCCFromStr(const aValue : String) : TFOURCC; inline; procedure ToHostByteOrderFromBigEndianM(var aValue : TUINT16); inline; procedure ToBigEndianFromHostByteOrderM(var aValue : TUINT16); inline; procedure ToHostByteOrderFromBigEndianM(var aValue : TUINT32); inline; procedure ToBigEndianFromHostByteOrderM(var aValue : TUINT32); inline; procedure ToHostByteOrderFromBigEndianM(var aValue : TINT16); inline; procedure ToBigEndianFromHostByteOrderM(var aValue : TINT16); inline; procedure ToHostByteOrderFromBigEndianM(var aValue : TINT32); inline; procedure ToBigEndianFromHostByteOrderM(var aValue : TINT32); inline; procedure ToHostByteOrderFromBigEndianM(var aValue : TINT64); inline; procedure ToBigEndianFromHostByteOrderM(var aValue : TINT64); inline; type TUINT32Array = packed array of TUINT32; TUINT16Array = packed array of TUINT16; TUINT8Array = packed array of TUINT8; procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT32Array; aCount : Cardinal); procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT16Array; aCount : Cardinal); procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT8Array; aCount : Cardinal); procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT32); procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT16); implementation uses sockets; procedure SkipJunk(aStream : TStream; aCount : TINT64); begin // TODO if stream is not seekable, just read. aStream.Seek(aCount, soCurrent); end; function CopyFrom(Source: TStream; Destination : TStream): Int64; var i : Int64; readCount : Int64; writtenCount : Int64; buffer : array[0..1023] of byte; begin CopyFrom := 0; repeat i := sizeof(Buffer); readCount := Source.Read(buffer,i); if readCount = 0 then Break; writtenCount := Destination.Write(buffer, readCount); if writtenCount <> readCount then raise EWriteError.Create('error'); CopyFrom := CopyFrom+i; until readCount = 0; end; function FOURCCToStr(aValue : TFOURCC) : String; begin Result := Chr((aValue and $FF)) + Chr((aValue shr 8) and $FF) + Chr((aValue shr 16) and $FF) + Chr((aValue shr 24) and $FF); end; {$ASSERTIONS ON} function FOURCCFromStr(const aValue : String) : TFOURCC; inline; begin assert(Length(aValue) = 4); Result := ord(aValue[1]) or (ord(aValue[2]) shl 8) or (ord(aValue[3]) shl 16) or (ord(aValue[4]) shl 24); end; procedure ToHostByteOrderFromBigEndianM(var aValue : TUINT16); inline; begin aValue := ntohs(aValue); end; procedure ToBigEndianFromHostByteOrderM(var aValue : TUINT16); inline; begin aValue := htons(aValue); end; procedure ToHostByteOrderFromBigEndianM(var aValue : TUINT32); inline; begin aValue := ntohl(aValue); end; procedure ToBigEndianFromHostByteOrderM(var aValue : TUINT32); inline; begin aValue := htonl(aValue); end; // FIXME: procedure ToHostByteOrderFromBigEndianM(var aValue : TINT16); inline; begin aValue := ntohs(aValue); end; procedure ToBigEndianFromHostByteOrderM(var aValue : TINT16); inline; begin aValue := htons(aValue); end; procedure ToHostByteOrderFromBigEndianM(var aValue : TINT32); inline; begin aValue := ntohl(aValue); end; procedure ToBigEndianFromHostByteOrderM(var aValue : TINT32); inline; begin aValue := htonl(aValue); end; // FIXME!!! procedure ToHostByteOrderFromBigEndianM(var aValue : TINT64); inline; begin aValue := ntohl(aValue); end; procedure ToBigEndianFromHostByteOrderM(var aValue : TINT64); inline; begin aValue := htonl(aValue); end; procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT32Array; aCount : Cardinal); var i : Integer; begin SetLength(aDestination, aCount); aStream.ReadBuffer(aDestination[0], aCount * SizeOf(aDestination[0])); if aCount > 0 then for i := 0 to aCount - 1 do ToHostByteOrderFromBigEndianM(aDestination[i]); end; procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT16Array; aCount : Cardinal); var i : Integer; begin SetLength(aDestination, aCount); aStream.ReadBuffer(aDestination[0], aCount * SizeOf(aDestination[0])); if aCount > 0 then for i := 0 to aCount - 1 do ToHostByteOrderFromBigEndianM(aDestination[i]); end; procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT8Array; aCount : Cardinal); begin SetLength(aDestination, aCount); aStream.ReadBuffer(aDestination[0], aCount * SizeOf(aDestination[0])); end; procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT32); begin aStream.ReadBuffer(aDestination, Sizeof(aDestination)); ToHostByteOrderFromBigEndianM(aDestination); end; procedure ReadBigEndian(aStream : TStream; var aDestination : TUINT16); begin aStream.ReadBuffer(aDestination, Sizeof(aDestination)); ToHostByteOrderFromBigEndianM(aDestination); end; initialization {$ASSERTIONS ON} assert(Sizeof(TINT8) = 1); assert(Sizeof(TUINT8) = 1); end.