php-src icon indicating copy to clipboard operation
php-src copied to clipboard

The return value of mb_list_encodings() doesn't contain 'SJIS-win'

Open sji-il opened this issue 4 years ago • 34 comments

Description

The following code:

<?php
var_dump(mb_list_encodings());

https://3v4l.org/GH0Gj

Resulted in this output:

  • contains 'CP932' and 'SJIS', but no 'SJIS-win'

But I expected this output instead:

  • contains 'SJIS-win'

As of PHP 8.1, SJIS-win is an alias of CP932, thus the return value of mb_list_encodings() doesn't contain 'SJIS-win'.

This breaks BC.

Some libraries have functions which receive one or more encoding names as arguments, and they often validate the input by the return value of mb_list_encodings(). This should be reverted, or fixed, or at least documented as an incompatible change.

PHP Version

PHP 8.1.0

Operating System

No response

sji-il avatar Apr 06 '22 04:04 sji-il

https://gist.github.com/sj-i/6bacf1bef255c096b3cbb78bfc29aa28 I have experimented with this already. So the case about U+00A5 is justified by the implementation of Windows, via its 'best-fit' mapping.

References:

https://web.archive.org/web/20100212163537/http://blogs.msdn.com/michkap/archive/2004/12/14/284838.aspx#332622

Now through the masgic of "best fit" mappings (which I will cover another day), U+2089 also maps to 0x5c on cp 932. Its a one-way mapping, obviously. But its there.

https://web.archive.org/web/20100212163537/http://blogs.msdn.com/michkap/archive/2004/12/14/284838.aspx#332622

Look at http://microsoft.com/globaldev/reference/dbcs/932.htm which has the offical mapping for cp932. It only has the 0x4c to U+005c mapping, as the other (best fit) mapping is not documented; none of the best fit mappings are.

https://web.archive.org/web/20100130054656/http://blogs.msdn.com:80/michkap/archive/2005/09/17/469941.aspx

both U+00a5 and U+20a9 have one-way 'best fit' mappings to 0x5c on their respective code pages.

https://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/readme.txt https://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/bestfit932.txt

sj-i avatar Apr 09 '22 18:04 sj-i

@sj-i Hmm, OK, thanks.

alexdowad avatar Apr 09 '22 18:04 alexdowad

About U+203E, there should be another background...

Your finding of the WHATWG encoding standard would be a hint. https://encoding.spec.whatwg.org/#shift_jis

https://github.com/whatwg/encoding/commit/be441c878334cba53ee0be6d6640deb572605ee2 This was added in 2012. But W3C and WHATWG are standardization organizations, there must be at least one, probably two actual implementations that behave like this beforehand.

I'm currently searching for old implementations of browsers that handle encodings.

BTW, glibc iconv translates U+203E to 0x7E on converting Unicode texts to CP932. https://gist.github.com/sj-i/c003796281253c41b70a1d9b92de2ea3

http://www2d.biglobe.ne.jp/~msyk/cgi-bin/charcode/373.html Maybe this is the reason of the glibc implementation. (Note: The author of this forum post is the man who wrote the actual glibc code for the CP932 mapping)

glibc iconv adopts UCS normalization, so even conversions between SJIS family and EUC-JP family are done through Unicode. So additional one-way mappings are required for each encoding for convenience. I assume a similar reason is there for browser implementations.

sj-i avatar Apr 09 '22 19:04 sj-i

Leaving aside the historical truth, the fact that glibc iconv uses these one-way mappings for such practical reasons may in itself justify its implementation in PHP. This could fit nicely into the code comments^^

sj-i avatar Apr 09 '22 19:04 sj-i

Speaking of iconv, I reached out to the maintainer of GNU libiconv earlier today to see if he has any insight on the background of these extra one-way mappings from Unicode to CP932. Haven't heard back from him yet.

alexdowad avatar Apr 09 '22 19:04 alexdowad

I've also found the Twitter account of the original author of the patch that added support for CP932 to the glibc libiconv, and asked him the intent about U+203E. If I get a response, will share it here.

sj-i avatar Apr 09 '22 21:04 sj-i

@alexdowad https://twitter.com/tree3yama/status/1513172414460743683 I got the answer from Moriyama-san (who added support for CP932 to glibc iconv)!

Those one-way mappings in glibc iconv were added to allow practical conversion between legacy encodings. And from what I can find, several other systems, such as Java, have similar mappings for the same reason. I don't have time right now, but I can put together more detailed information if needed?

sj-i avatar Apr 11 '22 08:04 sj-i

@sj-i Thanks very much for this information. Just trying to process this...

The reply from Moriyama-san which you kindly linked to mentions SJIS → CP932, EUC-JP → CP932, and ISO-2022-JP → CP932 conversions. The implication is that in all 3 of the source encodings, something maps to U+203E. Then, since conversions from one legacy encoding to another go through Unicode, when converting Unicode → CP932, we convert U+203E to 0x7E so that the something mentioned above will map to CP932 0x7E.

At least in mbstring, SJIS 0x7E maps to U+203E. So mapping U+203E to CP932 0x7E means that SJIS 0x7E will convert to CP932 0x7E.

However, in both mbstring and iconv, EUC-JP 0x7E and ISO-2022-JP 0x7E do not map to U+203E. So I am still trying to figure out why Moriyama-san said this mapping is related to EUC-JP → CP932 and ISO-2022-JP → CP932 conversions...

alexdowad avatar Apr 11 '22 08:04 alexdowad

@alexdowad Ah, that point needs a little more context. To summarize, there is no direct relationship with the one-way mapping for this specific code point.

Moriyama-san was working in several OSS communities at the time to make each OSS's character code processing interoperable with each other.

There is a problem with practicality when a character that Japanese users generally regard as "the same" is changed to another character when converted via Unicode.

He proposed the addition of several additional one-way mappings for the then-current ISO 2022 JP and EUC-JP encodings, as well as for ShiftJIS and its derivatives. Examples of such efforts can be found, for example, in the following URLs

http://www.mysql.gr.jp/mysqlml/mysql/msg/12442 https://ja.osdn.net/projects/legacy-encoding/

Although not each of these efforts actually bore fruit, he chose to include ISO-2022-JP and EUC-JP in his mention of the topic because similar efforts were being made at the time for code points other than U+203E and U+00A5.

sj-i avatar Apr 11 '22 21:04 sj-i

@sj-i I am sorry I have delayed a bit in responding. Thanks for your exhaustive research.

I think we are almost ready to update both documentation for core developers (i.e. code comments) and the PHP manual here. To summarize @sj-i's findings, I think we can characterize mbstring's CP932 implementation as follows:

• It follows the "best fit" CP932 mappings from the Microsoft Windows API... • ...together with an extra mapping which improves the quality of conversions from SJIS to CP932. • It is also compatible with the WHATWG standard for "Shift_JIS".

Before we put that information in the PHP manual, we need to confirm that those statements are true. For example, @sj-i kindly checked the Win32 mappings for U+203E and U+00A5, but we haven't checked all Win32 "best fit" mappings from Unicode → CP932 and CP932 → Unicode.

I would also like to check all Shift_JIS mappings in the JavaScript Encoding API (i.e. WHATWG), to confirm whether we are truly compatible with them or not. I can do this when I have some time (probably in the next few days).

For core developers, we can add the following information:

• The 'extra mapping' is for U+203E. • mbstring's implementation of CP932 is also compatible with iconv's. • We can also include a small amount of historical background information.

Again, we need to check if the statement about iconv is true or not. If it is true, it is good information for core developers. But for users of PHP, I don't think most of them really care about compatibility with iconv. Hence, I don't think that statement needs to go into the PHP manual. (If you disagree, please mention.)

NOW! We have come a long way here, but are still have not 100% reached where we need to go.

The remaining piece of critical information which needs to be documented, both for core developers and for users of PHP, is why we have the so-called SJIS-win encoding. We already know that this encoding matches the original implementation of CP932 which was included in libmbfl when it was integrated into PHP. Further, when CP932 was adjusted, @moriyoshi retained the previous implementation as SJIS-win, presumably for backwards compatibility.

That is all well and good, but do we really want to tell our users that we are currently keeping SJIS-win purely for BC? If so, that implies that nobody should be using it for new, 'greenfield' development. But I think it is useful for more than just BC.

SJIS-win maps U+203E and U+00A5 to fullwidth, JIS X 0208 characters. For people who are converting arbitrary Unicode text to SJIS/CP932, that could help to avoid situations where a source text contains ¥, but after conversion, the user sees \ instead.

I think we can summarize the recommended uses of these mapping tables as follows:

• If you are converting SJIS text to CP932, use CP932. (Should we mention this? Is it something that people are actually likely to do?) • If you are exchanging CP932 data with Microsoft Windows software (such as Microsoft Office), especially if you receive data which was exported from MS Office and then emit data which will be imported back into MS Office, use CP932 for maximum compatibility. • If you will be processing SJIS/CP932 data in a web browser using JavaScript, use CP932 for maximum compatibility between conversions performed in the browser and those performed in PHP. • If you need to convert other, generic Unicode text to CP932, use SJIS-win.

Does that sound right?

alexdowad avatar Apr 14 '22 07:04 alexdowad

@alexdowad You have probably understood almost all the details necessary about this case, but I would like to emphasize the following point to be sure. It should be noted that what is important here is not conformance to any written standard or specific implementation of another product, but a bit vague customs and BC. Until 8.0, mbstring behaved differently from the current implementation. The reason behind this is not a formal standard, but an effort made for the convenience of users. Some code points had the same mappings in multiple OSS as it is customs from old ages. I've found yet another example in the JDK bug tracker. https://bugs.openjdk.java.net/browse/JDK-4361835

So, as we found, there are parts that are currently consistent with the MS best-fit mapping, the WHATWG specification, and the behavior of other converters. But even if there are differences with them in the implementation, I don't think they should be "fixed" to eliminate the differences, at least in minor versions, unless there is an obvious error that is to the detriment of the user.

The long-standing behavior of PHP's mbstring itself is now part of the customs that our users rely on.

The advice we can surely give to future core developers, in general, is as follows

  • In many cases, there is no standard to follow for mapping between Japanese legacy encodings and Unicode.
  • In some cases, it is not clear which way of mapping is the "correct" one.
  • Mappings that have existed for a long time may have users relying on any part of them.
  • If the mapping is to be changed, there must be enough benefit to balance the possible BC breaks.
  • If it is not an obvious bug, it should not be fixed in a minor release.
  • See https://github.com/php/php-src/issues/8308 for more detail :)

nobody should be using it for new, 'greenfield' development.

I don't think anybody should use legacy encodings in their 'greenfield' development ^^ Joking aside, for the recommendations you listed, I think the following one should be fixed.

If you need to convert other, generic Unicode text to CP932, use SJIS-win.

"If you need to convert U+00A5 (YEN SIGN) and U+203E (OVERLINE) from Unicode to full-width, use SJIS-win."

I think it is appropriate to explain the behavior just as it is.

sj-i avatar Apr 16 '22 23:04 sj-i

@sj-i These are good points and are appreciated. In general, I will certainly try to follow your advice.

Regarding the specific issue under discussion here (SJIS-win vs. CP932), I must honestly say that this discussion has left a lot of question marks in my mind. How we got to the present state of affairs is now fairly clear. The general principles which @sj-i has set forth regarding future development of mbstring, at least as regards legacy Japanese encodings, are clear (and I do agree with all of them). A lot of things have become clear, largely due to @sj-i's research.

The remaining "question marks" are all related to the way forward from here. If I had known in 2020 that there was a difference in behavior between SJIS-win and CP932, I would not have merged these encodings at that time. But I didn't know, and I did merge them, and so here we are in 2022, trying to decide what to do next.

Normally, when a change like this is made unintentionally or due to a misunderstanding, the default response is to roll it back. And we might still go that way. But, there are some unusual things about this situation, which are making it hard for me to feel sure about what to do:

  • PHP 8.1 was released 6 months ago, but at present, we do not have any evidence that any PHP application in production has been broken by the merge of SJIS-win and CP932. Certainly, it is possible that some application, somewhere, might have been broken, and perhaps the developers have already patched it, without reporting the breakage. I wouldn't want to ignore that possibility; but still, for now, we do not have any evidence of actual problems being caused.

  • @zonuexe and @sj-i are obviously both very experienced, knowledgeable, and accomplished Japanese PHP developers, but it seems that neither of these esteemed developers has any reason for preferring SJIS-win or CP932. Certainly, reasons could be invented ex post facto to justify the request to restore SJIS-win; but when this GH issue was opened, neither had a reason.

It is revealing that both gentlemen could experience many years of PHP development without ever finding it necessary to pay attention to the difference between SJIS-win and CP932. Of course, there may be other Japanese PHP developers out there who have practical reasons to care about the difference; but at the moment, we do not have any evidence of such.

  • From a (very limited, incomplete) survey of Japanese-language blog posts on the subject, it did not appear that any of the authors had clear reasons for preferring SJIS-win or CP932. It seems likely that most existing users have just chosen randomly, or by "superstition" (copying what others do, without understanding the reasons).

  • It doesn't seem that other software development platforms have found it necessary or desirable to support two different variants of CP932.

Another point: To me, the maintenance burden of keeping two different CP932 variants in mbstring is not a great concern. My concern is more for the users. I think it is a disservice to PHP users if we keep two different features, which work almost the same, but just slightly different, and nobody can provide any reasonable explanation of why things should be that way or how the users can choose which one to use. (The PHP platform may have had such inconsistencies in the past, but they are being gradually smoothed out with each successive release.)

Thought experiment: If we were not working on PHP here, but on Python, Ruby, JavaScript, or some other platform which only supports one variant of CP932 in its standard library, would you recommend that a second variant of CP932 should be added? If not, that suggests there might not be a logical or practical reason to have two variants.

If indeed, there is no practical reason to have two different CP932 variants, it is possible that we could still roll the change back for PHP 8.1 (to avoid BC break), and perform the simplification in a future release. Please note, I am just mentioning this as an idea and am still open to all possible outcomes here.

At this point, it would be very helpful if other core developers can share their views.

Finally, I would like to say that the issue raised in #8281 may be more straightforward than this one, and it is possible we might be able to sort it out more quickly.

alexdowad avatar Apr 17 '22 12:04 alexdowad

If I had known in 2020 that there was a difference in behavior between SJIS-win and CP932, I would not have merged these encodings at that time. But I didn't know, and I did merge them, and so here we are in 2022, trying to decide what to do next.

It might be reasonable to revert for now, and to merge these encodings for PHP 9.

cmb69 avatar Apr 18 '22 11:04 cmb69

This has been resolved.

alexdowad avatar Nov 14 '22 13:11 alexdowad