/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.html;

public class MhtPart {

    public String contentType;

    public String contentEncoding;

    public String contentLocation;

    public java.util.HashMap<String, String> attrs = new java.util.HashMap<String, String>();

    public byte[] data;

    public String stringData;

    public MhtPart(String src) {
        int i = 0;
        int crlf = 0;
        for (i = 0; i < src.length(); i++) {
            if (((int)src.charAt(i)) == 0xD) {
                crlf++;
                if (((i + 1) < src.length()) && ((int)src.charAt(i + 1)) == 0xA) 
                    i++;
            }
            else if (((int)src.charAt(i)) == 0xA) 
                crlf++;
            else 
                crlf = 0;
            if (crlf >= 2) 
                break;
        }
        if ((crlf < 2) || i >= src.length()) 
            return;
        String head = src.substring(0, 0 + i).trim();
        int j;
        while (head.length() > 0) {
            if (!head.startsWith("Content-")) 
                break;
            head = head.substring(8);
            j = head.indexOf("Content-");
            String rec = (j < 0 ? head : head.substring(0, 0 + j).trim());
            if (rec.startsWith("Location:")) 
                contentLocation = rec.substring(9).trim();
            else if (rec.startsWith("Transfer-Encoding:")) 
                contentEncoding = rec.substring("Transfer-Encoding:".length()).trim();
            else if (rec.startsWith("Type:")) 
                contentType = rec.substring(5).trim();
            if (j < 0) 
                break;
            head = head.substring(j);
        }
        if (com.pullenti.unisharp.Utils.stringsCompare(contentEncoding, "base64", true) == 0) 
            data = java.util.Base64.getDecoder().decode((src.substring(i).trim()).replace("\r", "").replace("\n", ""));
        else if (com.pullenti.unisharp.Utils.stringsCompare(contentEncoding, "quoted-printable", true) == 0) {
            StringBuilder tmp = new StringBuilder();
            for (j = i; j < src.length(); j++) {
                if (src.charAt(j) != '=') 
                    tmp.append(src.charAt(j));
                else {
                    if (((j + 2) < src.length()) && _toInt(src.charAt(j + 1)) >= 0 && _toInt(src.charAt(j + 2)) >= 0) {
                        int k = (_toInt(src.charAt(j + 1)) * 16) + _toInt(src.charAt(j + 2));
                        tmp.append((char)k);
                        j += 2;
                        continue;
                    }
                    for (++j; j < src.length(); j++) {
                        if (src.charAt(j) != ' ') 
                            break;
                    }
                    for (; j < src.length(); j++) {
                        if (((int)src.charAt(j)) != 0xD && ((int)src.charAt(j)) != 0xA) 
                            break;
                    }
                    j--;
                }
            }
            stringData = tmp.toString();
        }
    }

    private static int _toInt(char ch) {
        if (ch >= '0' && ch <= '9') 
            return ((int)ch) - ((int)'0');
        if (ch >= 'A' && ch <= 'F') 
            return 10 + ((((int)ch) - ((int)'A')));
        if (ch >= 'a' && ch <= 'f') 
            return 10 + ((((int)ch) - ((int)'a')));
        return -1;
    }

    public static java.util.ArrayList<MhtPart> parseAllStr(String all, int i0, String bound, boolean first) {
        java.util.ArrayList<MhtPart> res = new java.util.ArrayList<MhtPart>();
        int beg = 0;
        for (int i = i0; i < all.length(); i++) {
            int j;
            for (j = 0; j < bound.length(); j++) {
                if (bound.charAt(j) != all.charAt(i + j)) 
                    break;
            }
            if (j < bound.length()) 
                continue;
            if (beg > 0) {
                MhtPart pa = new MhtPart(all.substring(beg, beg + i - beg - 2).trim());
                if (pa.contentLocation != null) 
                    res.add(pa);
                if (first) 
                    break;
            }
            i += bound.length();
            for (; i < all.length(); i++) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(all.charAt(i))) 
                    break;
            }
            beg = i;
        }
        return res;
    }

    public static java.util.ArrayList<MhtPart> parseAll(com.pullenti.unisharp.FileStream fs, byte[] bound) {
        return null;
    }
    public MhtPart() {
    }
}
