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

@sji-il, thank you for the report.

This change actually dates back to 2020; see e2459857af. Before that commit, SJIS-win was indeed treated as a separate encoding from CP932, but testing revealed that the behavior of SJIS-win and CP932 was exactly the same (i.e. all the same mappings).

I think the first question here is: Was there actually some difference between SJIS-win and CP932 which I missed? Perhaps more importantly, should there have been some difference?

If indeed there is no difference, do you still feel that mb_list_encodings should list the same legacy text encoding twice, under two different names? Or perhaps SJIS-win should be the primary name, and CP932 should be an alias?

Regarding the BC issue, I'm sorry that this was not documented as a backwards-incompatible change. I think this is actually a change from PHP 7.4 to 8.0, not from 8.0 to 8.1 (but please correct me if that is wrong).

alexdowad avatar Apr 06 '22 06:04 alexdowad

SJIS-win is only an alias of CP932 as of PHP 8.1.0 (https://3v4l.org/bF8G4). I think we should document this change, but not revert it.

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().

These libraries probably should also check mb_encoding_aliases() (something like https://www.php.net/manual/en/function.mb-list-encodings.php#122266).

cmb69 avatar Apr 06 '22 10:04 cmb69

@alexdowad

Was there actually some difference between SJIS-win and CP932

https://3v4l.org/AVllX This code behaves differently between SJIS-win and CP932 before 8.1.

should there have been some difference?

As there has been some difference already 20+ years... there should be, unfortunately. Yeah, I think I can hear the third sigh from you. Legacy Japanese encodings should all be burned by fire and buried in the dark, though it's not realistic. SJIS-win and CP932 are both used in Japan. Some articles recommend one, others recommend another. It is optimistic to say that keeping only one behavior is enough. Both could be depended on by a fair number of users.

mb_list_encodings should list the same legacy text encoding twice

Even if there is no difference, I would recommend keeping two names on the list to avoid BC breaks, unless the maintenance burden is too high. When the maintenance burden is so much problematic, changing it in a major release with deprecation beforehand is reasonable. If the behavior was newborn or clearly disadvantageous to users, I feel it could simply be fixed as a bug in a minor release though...

this is actually a change from PHP 7.4 to 8.0, not from 8.0 to 8.1

It's from PHP 8.1. https://3v4l.org/glAfa

sj-i avatar Apr 06 '22 20:04 sj-i

@sj-i, thank you for pointing out the difference in mappings between SJIS-win and CP932. I don't know where I messed up when testing mbstring's handling of these text encodings back in 2020, but in any case, the correction is appreciated. (As mentioned, when testing, it appeared that there was no difference in behavior.)

I would just like to ask: as far as you know, is U+203E the only codepoint which should be handled differently between SJIS-win and CP932? Or are there any others?

alexdowad avatar Apr 06 '22 20:04 alexdowad

(Commenting further on the sample code which @sj-i shared:) Hmm. This actually seems to be closely related to the same issue which you have commented on in #8281. (Whether SJIS 0x7E should be interpreted as U+007E or U+203E...)

It does appear that the legacy behavior of CP932 in mbstring was a bit illogical, in that when converting Unicode → CP932, it would convert U+007E to 0x7E (implying that 0x7E is being used to represent a tilde, not an overbar), but then when converting CP932 → Unicode, it would convert U+203E to 0x7E (implying that 0x7E represents an overbar, not a tilde).

Do you have any idea why this is the case?

SJIS-win and CP932 are both used in Japan. Some articles recommend one, others recommend another. It is optimistic to say that keeping only one behavior is enough. Both could be depended on by a fair number of users.

If these encodings are different, and both are used by even a small number of users, then we definitely need to support both of them. That is not under question. Currently I am still trying to understand clearly what the difference is.

alexdowad avatar Apr 06 '22 21:04 alexdowad

@alexdowad

Or are there any others?

I can't say sure, but this is the only one I'm aware of.

Do you have any idea why this is the case?

I'm not sure about this specific case. Tilde (0x7E in ASCII) is often used in programming languages for special purposes. So historically there is confusion about how 0x7E in legacy encodings should be converted to Unicode, and how its roundtrip should be treated.

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

@alexdowad https://3v4l.org/lQpnG I found that conversion from U+00A5 is also different between CP932 and SJIS-win via brute-forcing.

I've also asked the original author, and he said that SJIS-win is a compatibility implementation created for historical reasons to map U+00A5 and U+203E in the same way as eucJP-win, so that the differences with CP932 are intended and should be kept.

sj-i avatar Apr 07 '22 04:04 sj-i

I've also asked the original author, and he said that SJIS-win is a compatibility implementation created for historical reasons to map U+00A5 and U+203E in the same way as eucJP-win, so that the differences with CP932 are intended and should be kept.

Understood. 👍🏻

So it looks like the solution to this issue here is to restore SJIS-win. That will also make it 'reappear' in mb_list_encodings.

Will do that as soon as I have a bit of time to prepare a PR.

alexdowad avatar Apr 07 '22 04:04 alexdowad

Just did a first draft of the code to reintroduce SJIS-win (it's just a few lines). Still need to adjust the unit tests.

I also still need to re-examine the mappings for CP932, confirm if there have been any changes from legacy mbstring, and if so, raise each one for discussion here.

I think it would be helpful if we can include some explanation of why users might choose to use CP932 or SJIS-win in the code comments; this would help future maintainers and developers to understand why the code is the way it is.

@sj-i stated above:

SJIS-win and CP932 are both used in Japan. Some articles recommend one, others recommend another.

I've not yet read through the references provided by @zonuexe on #8281, so maybe this is already covered there, but can you explain any reasons why one might recommend the use of CP932, or conversely, might recommend SJIS-win?

alexdowad avatar Apr 07 '22 06:04 alexdowad

Patch is ready for review, just waiting to see if @sj-i or any other interested parties can provide any additional background information which can go into code comments.

alexdowad avatar Apr 07 '22 06:04 alexdowad

OK, I won't be available for a few more hours to take care of my 2 yo daughter, but I'll try to summarize the information I have after she slept (if no one else has done it by then) .

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

@alexdowad

I've not yet read through the references provided by @zonuexe on https://github.com/php/php-src/issues/8281, so maybe this is already covered there, but can you explain any reasons why one might recommend the use of CP932, or conversely, might recommend SJIS-win?

Unfortunately, these encodings are not named according to standard specifications. Both Java and Ruby have a different set of encoding names about for SJIS variants than PHP's mbstring.

mbstring originates from the PHP3 漢字パッチ(Kanji patch), which is based on a library called libkcc or "streamable kanji code filter and converter". I haven't seen the full code at that time, but there is evidence that sjis-win was added earlier in the first commit of libmbfl.

(Note: libkcc is not libkkc. Finding information about kcc on the web today is very difficult.)

One of the original authors, @moriyoshi, said that SJIS-win and CP932 needed to be separated in order to comply with "some specifications". They forget which spec they followed, but the important fact is that for 20 years many users have relied on the name SJIS-win and the conversion map.

zonuexe avatar Apr 07 '22 14:04 zonuexe

@zonuexe, these are interesting points, though what I was trying to ask was a bit different.

Let me try again. Let's imagine you are starting a new PHP-based software project right now. You need to ingest SJIS-encoded data provided by users, and convert to Unicode for processing. Maybe it's for CSV import. Would you use mb_convert_encoding with "SJIS", "SJIS-win", "CP932", or something else? Why?

Have you ever developed such software projects in the past? If so, which of mbstring's text encodings did you use? And most importantly, why? (I hope the answer will not be "I just flipped a coin". 😅 )

It may be that none of the programmers who are sharing in this discussion know why they should choose SJIS-win or CP932. It may be that almost none of the (presumably many) PHP users who have ever used mbstring for this purpose, over its entire history, have ever understood why they should choose SJIS-win over CP932, or vice versa. If so, well... it is what it is. But it would be helpful, and appreciated, if we can be clear about that.

I'm still hoping that somebody who is following this discussion does have reasons for choosing one variant or another.

alexdowad avatar Apr 07 '22 19:04 alexdowad

One more small comment... the BC issue is understood and appreciated. I'm not saying much about it, because there is not much to say about it, but it does not mean it is being ignored.

alexdowad avatar Apr 07 '22 20:04 alexdowad

OK, I don't know if this will fit nicely into the code comments, but let me explain my findings in the additional investigation. In my opinion, unfortunately, the two encodings are often chosen almost at random among users by coincidence or historical reasons, rather than by strong opinions.

As @zonuexe said, SJIS-win existed at the time of the first commit of libmbfl. libmbfl's predecessor was mbfilter, a program created by a Japanese company called HappySize and released as OSS for use with PHP. Although the implementation history at the time of the introduction of SJIS-win cannot be tracked anymore, what is important to this story is the initial implementation of libmbfl used with PHP's mbstring.

On December 8, 2002, SJIS-win and CP932 were aliases. The filename of the filter was mbfilter_cp932.c. At this point, U+00A5 and U+203E were mapped to FULLWIDTH YEN SIGN (0x818F) and FULLWIDTH MACRON (0x8150), respectively. https://github.com/moriyoshi/libmbfl/commit/6c6ea25dd04aecc4260f0231b8158cc36b5e66a3#diff-5eecf532cc3c80d6877f1356bbc4b084ccd049313ab175160a1f9370384d1362R59-R63 https://github.com/moriyoshi/libmbfl/commit/6c6ea25dd04aecc4260f0231b8158cc36b5e66a3#diff-5eecf532cc3c80d6877f1356bbc4b084ccd049313ab175160a1f9370384d1362R237-R240

(Previously I said that the 8.0 behavior is for "20+ years", it was wrong. Correctly it's about 10 years.)

On March 14, 2004, A statement was added to the PHP Manual https://github.com/php/doc-en/commit/cee35d96cdddb02c6d6cb627f2c7dedcb2638e7a#diff-d5b0e3b6642a9b9829d0fc664170f19ddb344fd869a5317934d22db6e8bf179cR346

For the CP932 codemap, use SJIS-WIN instead.

This phrase is still in the PHP manual, so the number of users who refer to the PHP manual would specify 'SJIS-win' because of this.

On the other hand, Japanese programmers often know that there are multiple variants of ShiftJIS, and that some characters can only be handled with a encoding called CP932 or Windows-31J. Some people may have "tried" one of them and it worked. At this point, these were just aliases, so the behavior was the same no matter what name they specified. They could use any name they like.

On March 2, 2010, a couple of fixes were made to libmbfl that changes the conversion of U+00A5 and U+203E to 0x5c and 0x7e for CP932, respectively, and separate SJIS-win as a compatible implementation. https://github.com/moriyoshi/libmbfl/commit/cb257bc380e9d93865590dfb8593c3524a9b48a6 https://github.com/moriyoshi/libmbfl/commit/46a83aafb0738e49a5479769b3f649ac45eab087

For the 10 years between the release of PHP 5.3.3 on July 22, 2010 and the release of PHP 8.1.0, CP932 and SJIS-win had different conversion tables for the two characters. And this difference is not specifically explained in the PHP manual. During this decade, many users may have unwittingly written programs that rely on a conversion table that they chose for no particular reason.

On October 30, 2011, another page in the PHP manual also specifically lists CP932 as a supported encoding. https://github.com/php/doc-en/commit/93c8f3c1305ca9d17a6e9f68d9e0438dec5dc165

This is still in the manual, too.

Depending on perspective, when users try to find out from the manual how to use CP932 with PHP, the PHP manual itself now supports two factions, CP932 and SJIS-win, incites warfare between them^^

Of course, there would be some programmers who will notice the difference between the two starting with PHP 5.3.3 and consciously choosing the encoding. Yes, don't forget about this faction: ....... The more serious and careful programmers know that there is no single right answer to the code conversion rules and that they should be used differently for different purposes.

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

Depending on how the document is generated, such as the conversion table used to convert a document from legacy encoding to Unicode, which code point is intended to represent which character may vary. When such a document is further converted to the same or another legacy encoding, the conversion method required will depend on the intended use. There are conversion tables with different policies for handling U+00A5 and U+203E, so we can choose which one we imagine would work best for our use case. That's all we can talk about if we try to choose an encoding reasonably.

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

Even if the behavior was about 10 years old instead of over 20, I would still recommend keeping both conversion tables and improving the PHP manual to make it a description that clearly states the differences and gives hints on how to choose one. Maybe @mumumu will write a good explanation :)

sj-i avatar Apr 07 '22 22:04 sj-i

@alexdowad The research by @sj-i clearly explained my vague understanding of the historical background.

Above all, the following phrase is simply expressed.

the two encodings are often chosen almost at random among users by coincidence or historical reasons, rather than by strong opinions.

I preferred CP932 as a common encoding name compatible with other languages, but I didn't know the subtle differences in behavior and it's essentially just a coin toss. :smile:

One more small comment... the BC issue is understood and appreciated. I'm not saying much about it, because there is not much to say about it, but it does not mean it is being ignored.

I don't think you're trying to ignore the effects of BC. Throughout the last few days of work, I understand that everyone is working hard to resolve these issues. Thank you!

zonuexe avatar Apr 08 '22 01:04 zonuexe

I have just been searching a bit more to see if it is possible to identify which specification @moriyoshi was following 10 years ago when he adjusted the mappings for CP932. Coming up empty so far.

I did find that for handling text on the web, the W3C and WHATWG publish a standard on handling of various text encodings. They refer to CP932 as "Shift_JIS", and their definition of "Shift_JIS" seems to agree with mbstring's definition of CP932. However, the first version of that standard was published in 2014... so it looks like it's not the one.

alexdowad avatar Apr 08 '22 06:04 alexdowad

@cmb69, I think you are a Windows man. I'm just wondering if mbstring's definition of CP932 might have been chosen to agree with the Win32 API. Presumably you have a Windows machine with VC++?

Are you able to build and run this program? I don't have a Windows machine to do it on. (Please note, I wrote this code from the Win32 API docs, but can't test it, so it may not work... 😬)

#include <stdio.h>
#include <stringapiset.h>

void TryConversion(int wchar)
{
    WCHAR instr[1];
    CHAR outstr[32];
    instr[0] = wchar;
    int num_chars = WideCharToMultiByte(932, 0, instr, 1, outstr, sizeof(outstr), NULL, NULL);
    if (num_chars == 0) {
        printf("ERROR\n");
        return;
    }
    for (int i = 0; i < num_chars; i++)
        printf("%x ", outstr[i] & 0xFF);
    printf("\n");
}

int main()
{
    TryConversion(0xA5);
    TryConversion(0x203E);
    return 0;
}

A web search suggests that in Visual Studio, when creating a new project for this, you would want to choose "Windows Desktop", choose "Windows Desktop Wizard", then in the wizard, pick "Console Application (.exe)" as the "Application Type".

alexdowad avatar Apr 09 '22 17:04 alexdowad