package com.facebook.soloader;

import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedByInterruptException;
import kotlin.UShort;
import okhttp3.internal.ws.WebSocketProtocol;
/* loaded from: classes2.dex */
public final class MinElf {
    public static final int DT_NEEDED = 1;
    public static final int DT_NULL = 0;
    public static final int DT_STRTAB = 5;
    public static final int ELF_MAGIC = 1179403647;
    public static final int PN_XNUM = 65535;
    public static final int PT_DYNAMIC = 2;
    public static final int PT_LOAD = 1;
    private static final String TAG = "MinElf";

    /* loaded from: classes2.dex */
    public enum ISA {
        NOT_SO("not_so"),
        X86("x86"),
        ARM("armeabi-v7a"),
        X86_64("x86_64"),
        AARCH64("arm64-v8a"),
        OTHERS("others");
        
        private final String value;

        @Override // java.lang.Enum
        public String toString() {
            return this.value;
        }

        ISA(String str) {
            this.value = str;
        }
    }

    public static String[] extract_DT_NEEDED(File file) throws IOException {
        ElfFileChannel elfFileChannel = new ElfFileChannel(file);
        try {
            String[] extract_DT_NEEDED = extract_DT_NEEDED(elfFileChannel);
            elfFileChannel.close();
            return extract_DT_NEEDED;
        } catch (Throwable th) {
            try {
                elfFileChannel.close();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    private static String[] extract_DT_NEEDED_with_retries(ElfFileChannel elfFileChannel) throws IOException {
        int i = 0;
        while (true) {
            try {
                return extract_DT_NEEDED_no_retries(elfFileChannel);
            } catch (ClosedByInterruptException e) {
                i++;
                if (i > 4) {
                    throw e;
                }
                Thread.interrupted();
                Log.e(TAG, "retrying extract_DT_NEEDED due to ClosedByInterruptException", e);
                elfFileChannel.openChannel();
            }
        }
    }

    public static String[] extract_DT_NEEDED(ElfByteChannel elfByteChannel) throws IOException {
        if (elfByteChannel instanceof ElfFileChannel) {
            return extract_DT_NEEDED_with_retries((ElfFileChannel) elfByteChannel);
        }
        return extract_DT_NEEDED_no_retries(elfByteChannel);
    }

    private static String[] extract_DT_NEEDED_no_retries(ElfByteChannel elfByteChannel) throws IOException {
        long j;
        String str;
        long j2;
        long j3;
        long j4;
        String str2;
        long j5;
        long j6;
        long j7;
        long j8;
        long j9;
        long j10;
        ByteBuffer allocate = ByteBuffer.allocate(8);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        long j11 = getu32(elfByteChannel, allocate, 0L);
        if (j11 != 1179403647) {
            throw new ElfError("file is not ELF: 0x" + Long.toHexString(j11));
        }
        boolean z = getu8(elfByteChannel, allocate, 4L) == 1;
        if (getu8(elfByteChannel, allocate, 5L) == 2) {
            allocate.order(ByteOrder.BIG_ENDIAN);
        }
        long j12 = z ? getu32(elfByteChannel, allocate, 28L) : get64(elfByteChannel, allocate, 32L);
        long j13 = z ? getu16(elfByteChannel, allocate, 44L) : getu16(elfByteChannel, allocate, 56L);
        int i = getu16(elfByteChannel, allocate, z ? 42L : 54L);
        if (j13 == WebSocketProtocol.PAYLOAD_SHORT_MAX) {
            long j14 = z ? getu32(elfByteChannel, allocate, 32L) : get64(elfByteChannel, allocate, 40L);
            if (z) {
                j13 = getu32(elfByteChannel, allocate, j14 + 28);
            } else {
                j13 = getu32(elfByteChannel, allocate, j14 + 44);
            }
        }
        long j15 = j12;
        long j16 = 0;
        while (true) {
            if (j16 >= j13) {
                j = 0;
                break;
            }
            if (z) {
                j10 = getu32(elfByteChannel, allocate, j15);
            } else {
                j10 = getu32(elfByteChannel, allocate, j15);
            }
            if (j10 != 2) {
                j15 += i;
                j16++;
            } else if (z) {
                j = getu32(elfByteChannel, allocate, j15 + 4);
            } else {
                j = get64(elfByteChannel, allocate, j15 + 8);
            }
        }
        if (j == 0) {
            throw new ElfError("ELF file does not contain dynamic linking information");
        }
        long j17 = j;
        int i2 = 0;
        long j18 = 0;
        while (true) {
            long j19 = z ? getu32(elfByteChannel, allocate, j17) : get64(elfByteChannel, allocate, j17);
            if (j19 == 1) {
                if (i2 == Integer.MAX_VALUE) {
                    throw new ElfError("malformed DT_NEEDED section");
                }
                i2++;
                str = "malformed DT_NEEDED section";
            } else if (j19 == 5) {
                str = "malformed DT_NEEDED section";
                j18 = z ? getu32(elfByteChannel, allocate, j17 + 4) : get64(elfByteChannel, allocate, j17 + 8);
            } else {
                str = "malformed DT_NEEDED section";
            }
            j17 += z ? 8L : 16L;
            if (j19 == 0) {
                if (j18 == 0) {
                    throw new ElfError("Dynamic section string-table not found");
                }
                long j20 = j12;
                int i3 = 0;
                while (true) {
                    if (i3 >= j13) {
                        j2 = j;
                        j3 = 0;
                        j4 = 0;
                        break;
                    }
                    if (z) {
                        j5 = getu32(elfByteChannel, allocate, j20);
                    } else {
                        j5 = getu32(elfByteChannel, allocate, j20);
                    }
                    if (j5 == 1) {
                        if (z) {
                            j6 = j13;
                            j7 = getu32(elfByteChannel, allocate, j20 + 8);
                        } else {
                            j6 = j13;
                            j7 = get64(elfByteChannel, allocate, j20 + 16);
                        }
                        if (z) {
                            j2 = j;
                            j8 = getu32(elfByteChannel, allocate, j20 + 20);
                        } else {
                            j2 = j;
                            j8 = get64(elfByteChannel, allocate, j20 + 40);
                        }
                        if (j7 <= j18 && j18 < j8 + j7) {
                            if (z) {
                                j9 = getu32(elfByteChannel, allocate, j20 + 4);
                            } else {
                                j9 = get64(elfByteChannel, allocate, j20 + 8);
                            }
                            j4 = j9 + (j18 - j7);
                            j3 = 0;
                        }
                    } else {
                        j6 = j13;
                        j2 = j;
                    }
                    j20 += i;
                    i3++;
                    j13 = j6;
                    j = j2;
                }
                if (j4 == j3) {
                    throw new ElfError("did not find file offset of DT_STRTAB table");
                }
                String[] strArr = new String[i2];
                long j21 = j2;
                int i4 = 0;
                while (true) {
                    long j22 = z ? getu32(elfByteChannel, allocate, j21) : get64(elfByteChannel, allocate, j21);
                    if (j22 == 1) {
                        strArr[i4] = getSz(elfByteChannel, allocate, (z ? getu32(elfByteChannel, allocate, j21 + 4) : get64(elfByteChannel, allocate, j21 + 8)) + j4);
                        if (i4 == Integer.MAX_VALUE) {
                            throw new ElfError(str);
                        }
                        i4++;
                        str2 = str;
                    } else {
                        str2 = str;
                    }
                    j21 += z ? 8L : 16L;
                    if (j22 == 0) {
                        if (i4 == i2) {
                            return strArr;
                        }
                        throw new ElfError(str2);
                    }
                    str = str2;
                }
            }
        }
    }

    private static String getSz(ElfByteChannel elfByteChannel, ByteBuffer byteBuffer, long j) throws IOException {
        StringBuilder sb = new StringBuilder();
        while (true) {
            long j2 = 1 + j;
            short u8Var = getu8(elfByteChannel, byteBuffer, j);
            if (u8Var != 0) {
                sb.append((char) u8Var);
                j = j2;
            } else {
                return sb.toString();
            }
        }
    }

    private static void read(ElfByteChannel elfByteChannel, ByteBuffer byteBuffer, int i, long j) throws IOException {
        int read;
        byteBuffer.position(0);
        byteBuffer.limit(i);
        while (byteBuffer.remaining() > 0 && (read = elfByteChannel.read(byteBuffer, j)) != -1) {
            j += read;
        }
        if (byteBuffer.remaining() > 0) {
            throw new ElfError("ELF file truncated");
        }
        byteBuffer.position(0);
    }

    private static long get64(ElfByteChannel elfByteChannel, ByteBuffer byteBuffer, long j) throws IOException {
        read(elfByteChannel, byteBuffer, 8, j);
        return byteBuffer.getLong();
    }

    private static long getu32(ElfByteChannel elfByteChannel, ByteBuffer byteBuffer, long j) throws IOException {
        read(elfByteChannel, byteBuffer, 4, j);
        return byteBuffer.getInt() & 4294967295L;
    }

    private static int getu16(ElfByteChannel elfByteChannel, ByteBuffer byteBuffer, long j) throws IOException {
        read(elfByteChannel, byteBuffer, 2, j);
        return byteBuffer.getShort() & UShort.MAX_VALUE;
    }

    private static short getu8(ElfByteChannel elfByteChannel, ByteBuffer byteBuffer, long j) throws IOException {
        read(elfByteChannel, byteBuffer, 1, j);
        return (short) (byteBuffer.get() & 255);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static class ElfError extends RuntimeException {
        ElfError(String str) {
            super(str);
        }
    }
}
