package com.bytedance.librarian.elf;

import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.librarian.elf.Elf;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ElfParser implements Elf, Closeable {
    public final int LIZ = 1179403647;
    public final FileChannel LIZIZ;

    public void close() {
        this.LIZIZ.close();
    }

    public final List<String> LIZ() {
        boolean z;
        Elf.Header elf64Header;
        ByteOrder byteOrder;
        Elf.DynamicStructure LIZ2;
        this.LIZIZ.position(0);
        ArrayList arrayList = new ArrayList();
        this.LIZIZ.position(0);
        ByteBuffer allocate = ByteBuffer.allocate(8);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        if (LIZIZ(allocate, 0) == 1179403647) {
            short LJ = LJ(allocate, 4);
            if (LJ(allocate, 5) == 2) {
                z = true;
            } else {
                z = false;
            }
            if (LJ == 1) {
                elf64Header = new Elf32Header(z, this);
            } else if (LJ == 2) {
                elf64Header = new Elf64Header(z, this);
            } else {
                throw new IllegalStateException("Invalid class type!");
            }
            ByteBuffer allocate2 = ByteBuffer.allocate(8);
            if (elf64Header.LIZ) {
                byteOrder = ByteOrder.BIG_ENDIAN;
            } else {
                byteOrder = ByteOrder.LITTLE_ENDIAN;
            }
            allocate2.order(byteOrder);
            long j = (long) elf64Header.LJFF;
            if (j == 65535) {
                j = elf64Header.LIZ().LIZ;
            }
            long j2 = 0;
            while (true) {
                if (j2 >= j) {
                    break;
                }
                Elf.ProgramHeader LIZ3 = elf64Header.LIZ(j2);
                if (LIZ3.LIZ == 2) {
                    long j3 = LIZ3.LIZIZ;
                    if (j3 != 0) {
                        ArrayList<Long> arrayList2 = new ArrayList<>();
                        long j4 = 0;
                        int i = 0;
                        do {
                            LIZ2 = elf64Header.LIZ(j3, i);
                            if (LIZ2.LIZ == 1) {
                                arrayList2.add(Long.valueOf(LIZ2.LIZIZ));
                            } else if (LIZ2.LIZ == 5) {
                                j4 = LIZ2.LIZIZ;
                            }
                            i++;
                        } while (LIZ2.LIZ != 0);
                        if (j4 != 0) {
                            long LIZ4 = LIZ(elf64Header, j, j4);
                            for (Long longValue : arrayList2) {
                                arrayList.add(LIZLLL(allocate2, longValue.longValue() + LIZ4));
                            }
                            return arrayList;
                        }
                        throw new IllegalStateException("String table offset not found!");
                    }
                } else {
                    j2++;
                }
            }
            return Collections.unmodifiableList(arrayList);
        }
        throw new IllegalArgumentException("Invalid ELF Magic!");
    }

    public ElfParser(File file) {
        MethodCollector.i(1772);
        if (file == null || !file.exists()) {
            IllegalArgumentException illegalArgumentException = new IllegalArgumentException("File is null or does not exist");
            MethodCollector.o(1772);
            throw illegalArgumentException;
        }
        this.LIZIZ = new FileInputStream(file).getChannel();
        MethodCollector.o(1772);
    }

    private short LJ(ByteBuffer byteBuffer, long j) {
        LIZ(byteBuffer, j, 1);
        return (short) (byteBuffer.get() & 255);
    }

    public final long LIZ(ByteBuffer byteBuffer, long j) {
        LIZ(byteBuffer, j, 8);
        return byteBuffer.getLong();
    }

    public final long LIZIZ(ByteBuffer byteBuffer, long j) {
        LIZ(byteBuffer, j, 4);
        return ((long) byteBuffer.getInt()) & 4294967295L;
    }

    public final int LIZJ(ByteBuffer byteBuffer, long j) {
        LIZ(byteBuffer, j, 2);
        return byteBuffer.getShort() & 65535;
    }

    private String LIZLLL(ByteBuffer byteBuffer, long j) {
        StringBuilder sb = new StringBuilder();
        while (true) {
            long j2 = 1 + j;
            short LJ = LJ(byteBuffer, j);
            if (LJ == 0) {
                return sb.toString();
            }
            sb.append((char) LJ);
            j = j2;
        }
    }

    private long LIZ(Elf.Header header, long j, long j2) {
        for (long j3 = 0; j3 < j; j3++) {
            Elf.ProgramHeader LIZ2 = header.LIZ(j3);
            if (LIZ2.LIZ == 1 && LIZ2.LIZJ <= j2 && j2 <= LIZ2.LIZJ + LIZ2.LIZLLL) {
                return (j2 - LIZ2.LIZJ) + LIZ2.LIZIZ;
            }
        }
        throw new IllegalStateException("Could not map vma to file offset!");
    }

    private void LIZ(ByteBuffer byteBuffer, long j, int i) {
        byteBuffer.position(0);
        byteBuffer.limit(i);
        long j2 = 0;
        while (j2 < ((long) i)) {
            int read = this.LIZIZ.read(byteBuffer, j + j2);
            if (read != -1) {
                j2 += (long) read;
            } else {
                throw new EOFException();
            }
        }
        byteBuffer.position(0);
    }
}
