/*
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.jcodings.specific;

import org.jcodings.Config;
import org.jcodings.IntHolder;
import org.jcodings.ascii.AsciiTables;
import org.jcodings.unicode.FixedWidthUnicodeEncoding;

public final class UTF32BEEncoding extends FixedWidthUnicodeEncoding {

    protected UTF32BEEncoding() {
        super("UTF-32BE", 4);
    }

    @Override
    public boolean isNewLine(byte[]bytes, int p, int end) {
        if (p + 3 < end) {
            if (bytes[p + 3] == (byte)0x0a && bytes[p + 2] == 0 && bytes[p + 1] == 0 && bytes[p] == 0) return true;

            if (Config.USE_UNICODE_ALL_LINE_TERMINATORS) {
                if ((Config.USE_CRNL_AS_LINE_TERMINATOR && bytes[p + 3] == (byte)0x0d) ||
                   bytes[p + 3] == (byte)0x85 && bytes[p + 2] == 0 && bytes[p + 1] == 0 && bytes[p] == 0) return true;

                if (bytes[p + 2] == (byte)0x20 &&
                   (bytes[p + 3] == (byte)0x29 || bytes[p + 3] == (byte)0x28) &&
                    bytes[p + 1] == 0 && bytes[p] == 0) return true;
            } // USE_UNICODE_ALL_LINE_TERMINATORS
        }
        return false;
    }

    @Override
    public int mbcToCode(byte[]bytes, int p, int end) {
        return (((bytes[p] & 0xff) * 256 + (bytes[p + 1] & 0xff)) * 256 + (bytes[p + 2] & 0xff)) * 256 + (bytes[p + 3] & 0xff);
    }

    @Override
    public int codeToMbc(int code, byte[]bytes, int p) {
        int p_ = p;
        bytes[p_++] = (byte)((code & 0xff000000) >>> 24);
        bytes[p_++] = (byte)((code & 0xff0000)   >>> 16);
        bytes[p_++] = (byte)((code & 0xff00)     >>> 8);
        bytes[p_++] = (byte) (code & 0xff);
        return 4;
    }

    @Override
    public int mbcCaseFold(int flag, byte[]bytes, IntHolder pp, int end, byte[]fold) {
        int p = pp.value;
        int foldP = 0;
        if (isAscii(bytes[p + 3] & 0xff) && bytes[p + 2] == 0 && bytes[p + 1] == 0 && bytes[p] == 0) {
            fold[foldP++] = 0;
            fold[foldP++] = 0;

            if (Config.USE_UNICODE_CASE_FOLD_TURKISH_AZERI) {
                if ((flag & Config.CASE_FOLD_TURKISH_AZERI) != 0) {
                    if (bytes[p + 3] == (byte)0x49) {
                        fold[foldP++] = (byte)0x01;
                        fold[foldP] = (byte)0x31;
                        pp.value += 4;
                        return 4;
                    }
                }
            } // USE_UNICODE_CASE_FOLD_TURKISH_AZERI

            fold[foldP++] = 0;
            fold[foldP] = AsciiTables.ToLowerCaseTable[bytes[p + 3] & 0xff];
            pp.value += 4;
            return 4;
        } else {
            return super.mbcCaseFold(flag, bytes, pp, end, fold);
        }
    }

    public static UTF32BEEncoding INSTANCE = new UTF32BEEncoding();
}

@@ -1703,46 +1703,8 @@
 f ((
-Config.USE_CRNL_AS_LINE_TERMINATOR && 
 byte
@@ -1728,32 +1728,72 @@
 )0x0
-d) %7C%7C%0A                  
+b %7C%7C bytes%5Bp + 3%5D == (byte)0x0c %7C%7C bytes%5Bp + 3%5D == (byte)0x0d %7C%7C
  byt
@@ -1815,16 +1815,38 @@
 yte)0x85
+) %0A                   
  && byte

update isNewLine condition(port from MRI 2.6.6). see https://github.com/ruby/ruby/blob/v2_6_6/enc/utf_32be.c