在Inno Setup中将字符串编码为Base64(Inno Setup的Unicode版本) [英] Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)

查看:504
本文介绍了在Inno Setup中将字符串编码为Base64(Inno Setup的Unicode版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我尝试使用Pascal函数EncodeStringBase64,假设Inno Setup可以访问Pascal标准库,但找不到它并提供了Unknown Identifier错误.

https://www.freepascal.org/docs-html/fcl/base64/encodestringbase64.html

我还发现此代码可以执行转换,但似乎仅限于AnsiStrings.

https://github.com/docker/toolbox/blob/master/windows/base64.iss

问题

理想情况下,我想使用标准库函数,有什么方法可以访问它?

如果没有,如果更改签名,使用AnsiStrings的代码是否可以安全地用于普通Unicode字符串?

我将为此进行测试,但是我担心我会测试大量用例,但这不能保证它实际上适用于每个字符,并且可能存在边缘情况.

解决方案

Base64编码的是字节,而不是字符(字符串).这也可能就是您发现的Encode64实现采用AnsiString的原因. AnsiString在Inno Setup Pascal脚本中通常(ab)用作 bytes 的动态数组.而string是一个个字符的数组.

如果要编码字符串,则首先必须将字符串表示为字节数组(以Base64编码的字符串的接收者所期望的方式),然后可以使用Encode64实现./p>

如果仅编码ASCII字符,则可以盲目地将string强制转换为AnsiString.如果使用非ASCII字符,则可能需要使用UTF-8之类的某种编码将UnicodeString转换为字节.

对于结果字符串,您可以将其从AnsiString强制转换为string,因为Base64仅使用ASCII字符(尽管更改函数签名以返回string也很有意义,因为它确实返回一个字符串,而不是字节数组.

对于ASCII输入,这可以做到:

 Base64 := string(Encode64(AnsiString(S)));
 


如果要使用标准"功能,则可以使用 CryptBinaryToString WinAPI函数.尽管这并不能解决上述问题,但由于该函数在输入端使用了一个字节数组(如预期的那样).


仅当使用Unicode Inno Setup(正确操作)时,以上内容才有所不同.如果您使用过Ansi Inno Setup(您不应该使用),则stringAnsiString.

Problem

I tried to use the Pascal function EncodeStringBase64, assuming Inno Setup had access to the Pascal standard library but it fails to find it and provides an Unknown Identifier error.

https://www.freepascal.org/docs-html/fcl/base64/encodestringbase64.html

I also found this code to carry out the conversion but it seems to be restricted to AnsiStrings.

https://github.com/docker/toolbox/blob/master/windows/base64.iss

Question

Ideally I'd like to use the standard library function, is there any way that I can access it?

If not, is the code using AnsiStrings safe to use on normal Unicode strings if I change the signature?

I'm going to carry out testing for it, but I'm worried that I'll test a good number of use cases, but this wouldn't guarantee that it's actually suitable for every character, and edge cases may exist.

解决方案

Base64 encodes bytes, not characters (strings). That's also probably the reason, why the Encode64 implementation, that you have found, takes AnsiString. AnsiString is commonly (ab)used in Inno Setup Pascal Script as a dynamic array of bytes. While string is an array of characters.

If you want to encode a string, you first have to represent the string as an array of bytes (in a way the recipient of the Base64-encoded string expects it) and then you can use your Encode64 implementation.

In case you encode ASCII characters only, you can just blindly cast string to AnsiString. If you use non-ascii characters, you will probably want to convert your UnicodeString to bytes using some encoding, like UTF-8.

As for the resulting string, you can just safely cast it from AnsiString to string, as Base64 uses ASCII characters only (though, it also makes sense to change function signature to return string, as it indeed returns a character string, not byte array).

So for an ASCII input, this will do:

Base64 := string(Encode64(AnsiString(S)));


If you want to use a "standard" function, you can use CryptBinaryToString WinAPI function. Though that won't spare you from solving the above, as the function takes an array of bytes on input (as expected).


The above makes a difference only, if you use Unicode Inno Setup (what you correctly do). Had you used Ansi Inno Setup (what you should not), string is AnsiString.

这篇关于在Inno Setup中将字符串编码为Base64(Inno Setup的Unicode版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆