From 07607b4bba9d387c371d472f92960ee96cefc7ba Mon Sep 17 00:00:00 2001 From: juha Date: Thu, 1 Nov 2018 08:39:09 +0000 Subject: [PATCH] LazUtils: Raise exception in UTF8To... functions, controlled by a global boolean. Issue #33686, patch from AlexeyT. git-svn-id: trunk@59416 - --- .../lazutils/asiancodepagefunctions.inc | 22 +++++++++---------- components/lazutils/lconvencoding.pas | 18 ++++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/components/lazutils/asiancodepagefunctions.inc b/components/lazutils/asiancodepagefunctions.inc index a2cc78d203..de2494842f 100644 --- a/components/lazutils/asiancodepagefunctions.inc +++ b/components/lazutils/asiancodepagefunctions.inc @@ -14,11 +14,9 @@ function DBCSToUTF8(const s: string; CodeP: integer): string; var - len: SizeInt; - Src: PChar; - Dest: PChar; - c: char; - l: Integer; + len, l: Integer; + Src, Dest: PChar; + c: char; code: word; begin if s = '' then @@ -189,13 +187,10 @@ end; function UTF8ToDBCS(const s: string; const UTF8CharConvFunc: TUnicodeToCharID): string; var - len: integer; - Src: PChar; - Dest: PChar; - c: char; + len, i, CharLen: integer; + Src, Dest: PChar; + c: char; Unicode: longword; - CharLen: integer; - i: integer; begin if s = '' then begin @@ -232,7 +227,10 @@ begin else Dest^ := chr(i); Inc(Dest); - end; + end + else + if ConvertEncodingFromUtf8RaisesException then + raise EConvertError.Create('Cannot convert UTF8 to DBCS code page'); end; until false; //SetLength(Result, Dest - PChar(Result)); diff --git a/components/lazutils/lconvencoding.pas b/components/lazutils/lconvencoding.pas index ae607aab70..bd3911074b 100644 --- a/components/lazutils/lconvencoding.pas +++ b/components/lazutils/lconvencoding.pas @@ -31,6 +31,9 @@ uses SysUtils, Classes, dos, LazUTF8 {$IFDEF EnableIconvEnc},iconvenc{$ENDIF}; +var + ConvertEncodingFromUtf8RaisesException: boolean = False; + //encoding names const EncodingUTF8 = 'utf8'; @@ -6998,16 +7001,12 @@ begin end; {$ENDIF} -function UTF8ToSingleByte(const s: string; - const UTF8CharConvFunc: TUnicodeToCharID): string; +function UTF8ToSingleByte(const s: string; const UTF8CharConvFunc: TUnicodeToCharID): string; var - len: Integer; - Src: PChar; - Dest: PChar; + len, i, CharLen: Integer; + Src, Dest: PChar; c: Char; Unicode: LongWord; - CharLen: integer; - i: integer; begin if s='' then begin Result:=''; @@ -7033,7 +7032,10 @@ begin if i>=0 then begin Dest^:=chr(i); inc(Dest); - end; + end + else + if ConvertEncodingFromUtf8RaisesException then + raise EConvertError.Create('Cannot convert UTF8 to single byte'); end; end; SetLength(Result,Dest-PChar(Result));