<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Webdings;
        panose-1:5 3 1 2 1 5 9 6 7 3;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle18
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-NZ" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;mso-fareast-language:EN-US">It’s a shame you can’t do:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;mso-fareast-language:EN-US">Type<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;mso-fareast-language:EN-US"> String = ansistring ;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> delphi-bounces@listserver.123.net.nz [mailto:delphi-bounces@listserver.123.net.nz]
<b>On Behalf Of </b>Stefan Mueller<br>
<b>Sent:</b> Wednesday, 16 November 2016 4:48 pm<br>
<b>To:</b> 'NZ Borland Developers Group - Delphi List' <delphi@listserver.123.net.nz><br>
<b>Subject:</b> Re: [DUG] Are these good ideas?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E">Search your code for “length()” function –the length function on an ansistring has returned the size in bytes (8bit), now it returns the size in words
(16bit). Places where you write strings to files and streams are usually places that need a fix. Do that search and you should probably have like 99% of your unicode issues found. Switching over to Unicode isn’t that big of a drama.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E">As for the MD5 issue – there’s actually a bit more to it – you can easily fix it by typecasting to ansistring and you should then get the same md5
hashes back again like in older pre-unicode Delphi versions:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">function foo(s: String): Integer;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> var<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> s2: AnsiString;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> begin<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> s2 := AnsiString(s); //typecasting to ansistring without getting compiler warnings<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> Result := md5(@s2[1], length(s2));<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> End;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#17375E">…but the question is if you really want to keep limiting yourself to ansistring? Now that your program will be unicode enabled you might as well embrace
it fully and for example make your code work with chinese passwords too:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">function foo(s: String): Integer;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> var<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> data: TBytes;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> begin<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> data := TEncoding.UTF8.GetBytes(s);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> Result := md5(data, length(data));<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> End;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">Kind regards,</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">Stefan Müller</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">,<br>
R&D Manager</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#1F497D"><br>
<br>
</span><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:white;background:red">ORCL</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Calibri",sans-serif;color:#1F497D">
</span><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">Toolbox Ltd.</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Calibri",sans-serif;color:#1F497D">
<br>
</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">Auckland, New Zealand</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Calibri",sans-serif;color:#1F497D">
<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="font-size:9.0pt;font-family:Webdings;color:#777777"><br>
P</span><span lang="EN-US" style="font-size:9.0pt;font-family:"Arial",sans-serif;color:#777777"> Please consider the environment before printing this email<br>
<br>
This message is intended for the adresse named above and may contain privileged or confidential information.<br>
If you are not the intended recipient of this message you must not use, copy, distribute or disclose it to anyone.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma",sans-serif">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma",sans-serif">
<a href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a> [<a href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>]
<b>On Behalf Of </b>Jeremy Coulter<br>
<b>Sent:</b> Wednesday, 16 November 2016 3:34 p.m.<br>
<b>To:</b> NZ Borland Developers Group - Delphi List<br>
<b>Subject:</b> Re: [DUG] Are these good ideas?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US">you see my dilema ? agh....what a pain<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US">On Wed, Nov 16, 2016 at 3:21 PM, Jolyon Direnko-Smith <<a href="mailto:jsmith@deltics.co.nz" target="_blank">jsmith@deltics.co.nz</a>> wrote:<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US">If by your "code that is working" (vs code that isn't) refers to the two different methods being used to produce an MD5 Hash, then I think there is a bit of confusion about the problem (hard to say without actual code).
i.e. it is possible that *both* of your MD5 implementations are working absolutely correctly. The problem is the way that your data is being passed in/out. MD5 is not a string-specific hashing algorithm - it works on bytes, not characters (otherwise you
couldn't produce an MD5 digest for a binary file, for example).<br>
<br>
e.g. if you have a function that accepts a string but then treats it as a pointer to some bytes (which is what a "string" is, after all) and assumes that the memory referenced by that pointer occupies the number of bytes indicated by the length of the string:<br>
<br>
function Foo(aString: String): Integer;<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US"> var<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> i: Integer;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> ptr: PByte;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> len: Integer;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> begin<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> len := Length(aString);<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> ptr := PByte(aString);<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> for i := 1 to len do<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> begin<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> // Do something with each byte then move on to the next one...<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> Inc(ptr);<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> end;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> // blah blah<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> end;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">This works perfectly well for an ANSI String because an ANSI String is a sequence of ANSIChar and an ANSIChar is a single byte. But this code will compile without any warning on Delphi 2009 and later, where "String"
is now synonymous with a "UnicodeString" which is a sequence of WIDEChars. i.e. 2 bytes. But even without ANY warning or hints that anything is amiss, the function will now process only HALF the string that it is passed (and half of the bytes it does process
will themselves be 0 [zero] - i.e. nulls).<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><br>
When migrating string handling code from ANSI to Unicode strings, you have to look for these sorts of issues as well as intermixed ANSI and WIDE types. I suspect that this is what is going on in your case.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">The above function could be "fixed" by correcting the declaration to explicitly accept an ANSIString:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"> function Foo(aString: ANSIString): Integer;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Now the assumption in the implementation that each char is 1 byte will be correct, but you will start seeing warnings about possible data loss resulting from any implicit conversions from Unicode to ANSI Strings.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">NOTE: In the case of an MD5 hash, fixing the implementation to process UnicodeStrings correctly (processing all bytes in the string by process 2 x LENGTH bytes of the string) will not result in the same MD5 hash because
MD5 works on bytes, not chars. An ANSIString and it's Unicode equivalent may be visually "equal" when displayed, but the binary representation of them is different and so the MD5 digest will also be different.<br>
<br>
If the binary representation is significant to your algorithm (as it is with MD5) then what you really need is TWO versions of the above function. One for ANSIString (working on the basis of 1 byte per char) and a separate implementation for UnicodeString
(2 bytes per char).<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">You then need to make sure that you correctly convert to and pass strings in the appropriate encoding to each function as and when required.<o:p></o:p></span></p>
</div>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<div>
<div>
<div>
<p class="MsoNormal"><span lang="EN-US">On 16 November 2016 at 13:30, Jeremy Coulter <<a href="mailto:jscoulter@gmail.com" target="_blank">jscoulter@gmail.com</a>> wrote:<o:p></o:p></span></p>
</div>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0cm;margin-bottom:5.0pt">
<div>
<div>
<div>
<p class="MsoNormal"><span lang="EN-US">Hi Again.I am looking at a Plan B to my last email about converting some code.<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">What I am thinking is, I COULD just use the code I have that is working, and may be create a DLL and access it from my new application.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">However, the code is actually running a server that listens on a couple of ports and when "things" happen, it fires an event that is then captured by an event on the front end.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Can I run a service as above in a DLL and have events that I guess just do a pass through to the DLL calling application?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">The other thought I had was to write a COM DLL, but bit wary of that.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Anyone got any thoughts on the above?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Thanks, Jeremy<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US">_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz" target="_blank">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz" target="_blank">
delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<o:p></o:p></span></p>
</blockquote>
</div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<p class="MsoNormal"><span lang="EN-US"><br>
_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">
delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</div>
</body>
</html>