package okio.internal;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000f\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010 \n\u0000\n\u0002\u0010$\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\t\n\u0002\u0010\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0010\u000e\n\u0002\b\u0004\u001a5\u0010\t\u001a\u00020\b2\u0006\u0010\u0001\u001a\u00020\u00002\u0006\u0010\u0003\u001a\u00020\u00022\u0014\b\u0002\u0010\u0007\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0004H\u0000¢\u0006\u0004\b\t\u0010\n\u001a)\u0010\u000e\u001a\u000e\u0012\u0004\u0012\u00020\u0000\u0012\u0004\u0012\u00020\u00050\r2\f\u0010\f\u001a\b\u0012\u0004\u0012\u00020\u00050\u000bH\u0002¢\u0006\u0004\b\u000e\u0010\u000f\u001a\u0013\u0010\u0011\u001a\u00020\u0005*\u00020\u0010H\u0000¢\u0006\u0004\b\u0011\u0010\u0012\u001a\u0013\u0010\u0014\u001a\u00020\u0013*\u00020\u0010H\u0002¢\u0006\u0004\b\u0014\u0010\u0015\u001a\u001b\u0010\u0017\u001a\u00020\u0013*\u00020\u00102\u0006\u0010\u0016\u001a\u00020\u0013H\u0002¢\u0006\u0004\b\u0017\u0010\u0018\u001a5\u0010\u001f\u001a\u00020\u001d*\u00020\u00102\u0006\u0010\u001a\u001a\u00020\u00192\u0018\u0010\u001e\u001a\u0014\u0012\u0004\u0012\u00020\u0019\u0012\u0004\u0012\u00020\u001c\u0012\u0004\u0012\u00020\u001d0\u001bH\u0002¢\u0006\u0004\b\u001f\u0010 \u001a\u0013\u0010!\u001a\u00020\u001d*\u00020\u0010H\u0000¢\u0006\u0004\b!\u0010\"\u001a\u001b\u0010%\u001a\u00020#*\u00020\u00102\u0006\u0010$\u001a\u00020#H\u0000¢\u0006\u0004\b%\u0010&\u001a\u001f\u0010'\u001a\u0004\u0018\u00010#*\u00020\u00102\b\u0010$\u001a\u0004\u0018\u00010#H\u0002¢\u0006\u0004\b'\u0010&\u001a!\u0010*\u001a\u0004\u0018\u00010\u001c2\u0006\u0010(\u001a\u00020\u00192\u0006\u0010)\u001a\u00020\u0019H\u0002¢\u0006\u0004\b*\u0010+\"\u0018\u0010/\u001a\u00020,*\u00020\u00198BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b-\u0010.¨\u00060"}, d2 = {"Lokio/Path;", "zipPath", "Lokio/FileSystem;", "fileSystem", "Lkotlin/Function1;", "Lokio/internal/ZipEntry;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "predicate", "Lokio/ZipFileSystem;", "d", "(Lokio/Path;Lokio/FileSystem;Lkotlin/jvm/functions/Function1;)Lokio/ZipFileSystem;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "entries", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "a", "(Ljava/util/List;)Ljava/util/Map;", "Lokio/BufferedSource;", "e", "(Lokio/BufferedSource;)Lokio/internal/ZipEntry;", "Lokio/internal/EocdRecord;", "f", "(Lokio/BufferedSource;)Lokio/internal/EocdRecord;", "regularRecord", "j", "(Lokio/BufferedSource;Lokio/internal/EocdRecord;)Lokio/internal/EocdRecord;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "extraSize", "Lkotlin/Function2;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "block", "g", "(Lokio/BufferedSource;ILkotlin/jvm/functions/Function2;)V", "k", "(Lokio/BufferedSource;)V", "Lokio/FileMetadata;", "basicMetadata", "h", "(Lokio/BufferedSource;Lokio/FileMetadata;)Lokio/FileMetadata;", "i", "date", "time", "b", "(II)Ljava/lang/Long;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "c", "(I)Ljava/lang/String;", "hex", "okio"}, k = 2, mv = {1, 9, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class ZipFilesKt {
    private static final java.util.Map a(java.util.List list) {
        okio.Path pathE = okio.Path.Companion.e(okio.Path.INSTANCE, "/", false, 1, null);
        java.util.Map mapO = kotlin.collections.MapsKt.o(new kotlin.Pair[]{kotlin.TuplesKt.a(pathE, new okio.internal.ZipEntry(pathE, true, null, 0L, 0L, 0L, 0, null, 0L, 508, null))});
        for (okio.internal.ZipEntry zipEntry : kotlin.collections.CollectionsKt.a1(list, new java.util.Comparator() { // from class: okio.internal.ZipFilesKt$buildIndex$$inlined$sortedBy$1
            @Override // java.util.Comparator
            public final int compare(java.lang.Object obj, java.lang.Object obj2) {
                return kotlin.comparisons.ComparisonsKt.d(((okio.internal.ZipEntry) obj).getCanonicalPath(), ((okio.internal.ZipEntry) obj2).getCanonicalPath());
            }
        })) {
            if (((okio.internal.ZipEntry) mapO.put(zipEntry.getCanonicalPath(), zipEntry)) == null) {
                while (true) {
                    okio.Path pathH = zipEntry.getCanonicalPath().h();
                    if (pathH == null) {
                        break;
                    }
                    okio.internal.ZipEntry zipEntry2 = (okio.internal.ZipEntry) mapO.get(pathH);
                    if (zipEntry2 != null) {
                        zipEntry2.getChildren().add(zipEntry.getCanonicalPath());
                        break;
                    }
                    okio.internal.ZipEntry zipEntry3 = new okio.internal.ZipEntry(pathH, true, null, 0L, 0L, 0L, 0, null, 0L, 508, null);
                    mapO.put(pathH, zipEntry3);
                    zipEntry3.getChildren().add(zipEntry.getCanonicalPath());
                    zipEntry = zipEntry3;
                }
            }
        }
        return mapO;
    }

    private static final java.lang.Long b(int i, int i2) {
        if (i2 == -1) {
            return null;
        }
        java.util.GregorianCalendar gregorianCalendar = new java.util.GregorianCalendar();
        gregorianCalendar.set(14, 0);
        gregorianCalendar.set(((i >> 9) & 127) + 1980, ((i >> 5) & 15) - 1, i & 31, (i2 >> 11) & 31, (i2 >> 5) & 63, (i2 & 31) << 1);
        return java.lang.Long.valueOf(gregorianCalendar.getTime().getTime());
    }

    private static final java.lang.String c(int i) {
        java.lang.StringBuilder sb = new java.lang.StringBuilder();
        sb.append("0x");
        java.lang.String string = java.lang.Integer.toString(i, kotlin.text.CharsKt.a(16));
        kotlin.jvm.internal.Intrinsics.k(string, "toString(...)");
        sb.append(string);
        return sb.toString();
    }

    public static final okio.ZipFileSystem d(okio.Path path, okio.FileSystem fileSystem, kotlin.jvm.functions.Function1 function1) {
        kotlin.jvm.internal.Intrinsics.l(path, "zipPath");
        kotlin.jvm.internal.Intrinsics.l(fileSystem, "fileSystem");
        kotlin.jvm.internal.Intrinsics.l(function1, "predicate");
        okio.FileHandle fileHandleO = fileSystem.o(path);
        try {
            long size = fileHandleO.size() - ((long) 22);
            if (size < 0) {
                throw new java.io.IOException("not a zip: size=" + fileHandleO.size());
            }
            long jMax = java.lang.Math.max(size - 65536, 0L);
            do {
                okio.BufferedSource bufferedSourceD = okio.Okio.d(fileHandleO.X(size));
                try {
                    if (bufferedSourceD.E2() == 101010256) {
                        okio.internal.EocdRecord eocdRecordF = f(bufferedSourceD);
                        java.lang.String strQ0 = bufferedSourceD.q0(eocdRecordF.getCommentByteCount());
                        bufferedSourceD.close();
                        long j = size - ((long) 20);
                        if (j > 0) {
                            okio.BufferedSource bufferedSourceD2 = okio.Okio.d(fileHandleO.X(j));
                            try {
                                if (bufferedSourceD2.E2() == 117853008) {
                                    int iE2 = bufferedSourceD2.E2();
                                    long jL0 = bufferedSourceD2.l0();
                                    if (bufferedSourceD2.E2() != 1 || iE2 != 0) {
                                        throw new java.io.IOException("unsupported zip: spanned");
                                    }
                                    okio.BufferedSource bufferedSourceD3 = okio.Okio.d(fileHandleO.X(jL0));
                                    try {
                                        int iE3 = bufferedSourceD3.E2();
                                        if (iE3 != 101075792) {
                                            throw new java.io.IOException("bad zip: expected " + c(101075792) + " but was " + c(iE3));
                                        }
                                        eocdRecordF = j(bufferedSourceD3, eocdRecordF);
                                        kotlin.Unit unit = kotlin.Unit.a;
                                        kotlin.io.CloseableKt.a(bufferedSourceD3, (java.lang.Throwable) null);
                                    } catch (java.lang.Throwable th) {
                                        try {
                                            throw th;
                                        } catch (java.lang.Throwable th2) {
                                            kotlin.io.CloseableKt.a(bufferedSourceD3, th);
                                            throw th2;
                                        }
                                    }
                                    try {
                                        throw th;
                                    } catch (java.lang.Throwable th3) {
                                        kotlin.io.CloseableKt.a(fileHandleO, th);
                                        throw th3;
                                    }
                                }
                                kotlin.Unit unit2 = kotlin.Unit.a;
                                kotlin.io.CloseableKt.a(bufferedSourceD2, (java.lang.Throwable) null);
                            } catch (java.lang.Throwable th4) {
                                try {
                                    throw th4;
                                } catch (java.lang.Throwable th5) {
                                    kotlin.io.CloseableKt.a(bufferedSourceD2, th4);
                                    throw th5;
                                }
                            }
                        }
                        java.util.ArrayList arrayList = new java.util.ArrayList();
                        okio.BufferedSource bufferedSourceD4 = okio.Okio.d(fileHandleO.X(eocdRecordF.getCentralDirectoryOffset()));
                        try {
                            long jC = eocdRecordF.getEntryCount();
                            for (long j2 = 0; j2 < jC; j2++) {
                                okio.internal.ZipEntry zipEntryE = e(bufferedSourceD4);
                                if (zipEntryE.getOffset() >= eocdRecordF.getCentralDirectoryOffset()) {
                                    throw new java.io.IOException("bad zip: local file header offset >= central directory offset");
                                }
                                if (((java.lang.Boolean) function1.invoke(zipEntryE)).booleanValue()) {
                                    arrayList.add(zipEntryE);
                                }
                            }
                            kotlin.Unit unit3 = kotlin.Unit.a;
                            kotlin.io.CloseableKt.a(bufferedSourceD4, (java.lang.Throwable) null);
                            okio.ZipFileSystem zipFileSystem = new okio.ZipFileSystem(path, fileSystem, a(arrayList), strQ0);
                            kotlin.io.CloseableKt.a(fileHandleO, (java.lang.Throwable) null);
                            return zipFileSystem;
                        } catch (java.lang.Throwable th6) {
                            try {
                                throw th6;
                            } catch (java.lang.Throwable th7) {
                                kotlin.io.CloseableKt.a(bufferedSourceD4, th6);
                                throw th7;
                            }
                        }
                    }
                    bufferedSourceD.close();
                    size--;
                } catch (java.lang.Throwable th8) {
                    bufferedSourceD.close();
                    throw th8;
                }
            } while (size >= jMax);
            throw new java.io.IOException("not a zip: end of central directory signature not found");
        } catch (java.lang.Throwable th9) {
            throw th9;
        }
    }

    public static final okio.internal.ZipEntry e(final okio.BufferedSource bufferedSource) throws java.io.IOException {
        kotlin.jvm.internal.Intrinsics.l(bufferedSource, "<this>");
        int iE2 = bufferedSource.E2();
        if (iE2 != 33639248) {
            throw new java.io.IOException("bad zip: expected " + c(33639248) + " but was " + c(iE2));
        }
        bufferedSource.skip(4L);
        short sJ0 = bufferedSource.j0();
        int i = sJ0 & 65535;
        if ((sJ0 & 1) != 0) {
            throw new java.io.IOException("unsupported zip: general purpose bit flag=" + c(i));
        }
        int iJ0 = bufferedSource.j0() & 65535;
        java.lang.Long lB = b(bufferedSource.j0() & 65535, bufferedSource.j0() & 65535);
        long jE2 = ((long) bufferedSource.E2()) & 4294967295L;
        final kotlin.jvm.internal.Ref.LongRef longRef = new kotlin.jvm.internal.Ref.LongRef();
        longRef.a = ((long) bufferedSource.E2()) & 4294967295L;
        final kotlin.jvm.internal.Ref.LongRef longRef2 = new kotlin.jvm.internal.Ref.LongRef();
        longRef2.a = ((long) bufferedSource.E2()) & 4294967295L;
        int iJ1 = bufferedSource.j0() & 65535;
        int iJ2 = bufferedSource.j0() & 65535;
        int iJ3 = bufferedSource.j0() & 65535;
        bufferedSource.skip(8L);
        final kotlin.jvm.internal.Ref.LongRef longRef3 = new kotlin.jvm.internal.Ref.LongRef();
        longRef3.a = ((long) bufferedSource.E2()) & 4294967295L;
        java.lang.String strQ0 = bufferedSource.q0(iJ1);
        if (kotlin.text.StringsKt.e0(strQ0, (char) 0, false, 2, (java.lang.Object) null)) {
            throw new java.io.IOException("bad zip: filename contains 0x00");
        }
        final long j = longRef2.a == 4294967295L ? 8 : 0L;
        if (longRef.a == 4294967295L) {
            j += (long) 8;
        }
        if (longRef3.a == 4294967295L) {
            j += (long) 8;
        }
        final kotlin.jvm.internal.Ref.BooleanRef booleanRef = new kotlin.jvm.internal.Ref.BooleanRef();
        g(bufferedSource, iJ2, new kotlin.jvm.functions.Function2<java.lang.Integer, java.lang.Long, kotlin.Unit>() { // from class: okio.internal.ZipFilesKt$readEntry$1
            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            {
                super(2);
            }

            public final void a(int i2, long j2) throws java.io.IOException {
                if (i2 == 1) {
                    kotlin.jvm.internal.Ref.BooleanRef booleanRef2 = booleanRef;
                    if (booleanRef2.a) {
                        throw new java.io.IOException("bad zip: zip64 extra repeated");
                    }
                    booleanRef2.a = true;
                    if (j2 < j) {
                        throw new java.io.IOException("bad zip: zip64 extra too short");
                    }
                    kotlin.jvm.internal.Ref.LongRef longRef4 = longRef2;
                    long jL0 = longRef4.a;
                    if (jL0 == 4294967295L) {
                        jL0 = bufferedSource.l0();
                    }
                    longRef4.a = jL0;
                    kotlin.jvm.internal.Ref.LongRef longRef5 = longRef;
                    longRef5.a = longRef5.a == 4294967295L ? bufferedSource.l0() : 0L;
                    kotlin.jvm.internal.Ref.LongRef longRef6 = longRef3;
                    longRef6.a = longRef6.a == 4294967295L ? bufferedSource.l0() : 0L;
                }
            }

            public /* bridge */ /* synthetic */ java.lang.Object invoke(java.lang.Object obj, java.lang.Object obj2) throws java.io.IOException {
                a(((java.lang.Number) obj).intValue(), ((java.lang.Number) obj2).longValue());
                return kotlin.Unit.a;
            }
        });
        if (j <= 0 || booleanRef.a) {
            return new okio.internal.ZipEntry(okio.Path.Companion.e(okio.Path.INSTANCE, "/", false, 1, null).j(strQ0), kotlin.text.StringsKt.J(strQ0, "/", false, 2, (java.lang.Object) null), bufferedSource.q0(iJ3), jE2, longRef.a, longRef2.a, iJ0, lB, longRef3.a);
        }
        throw new java.io.IOException("bad zip: zip64 extra required but absent");
    }

    private static final okio.internal.EocdRecord f(okio.BufferedSource bufferedSource) throws java.io.IOException {
        int iJ0 = bufferedSource.j0() & 65535;
        int iJ1 = bufferedSource.j0() & 65535;
        long jJ0 = bufferedSource.j0() & 65535;
        if (jJ0 != (bufferedSource.j0() & 65535) || iJ0 != 0 || iJ1 != 0) {
            throw new java.io.IOException("unsupported zip: spanned");
        }
        bufferedSource.skip(4L);
        return new okio.internal.EocdRecord(jJ0, 4294967295L & ((long) bufferedSource.E2()), bufferedSource.j0() & 65535);
    }

    private static final void g(okio.BufferedSource bufferedSource, int i, kotlin.jvm.functions.Function2 function2) throws java.io.IOException {
        long j = i;
        while (j != 0) {
            if (j < 4) {
                throw new java.io.IOException("bad zip: truncated header in extra field");
            }
            int iJ0 = bufferedSource.j0() & 65535;
            long jJ0 = ((long) bufferedSource.j0()) & okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_SHORT_MAX;
            long j2 = j - ((long) 4);
            if (j2 < jJ0) {
                throw new java.io.IOException("bad zip: truncated value in extra field");
            }
            bufferedSource.b2(jJ0);
            long size = bufferedSource.getBufferField().getSize();
            function2.invoke(java.lang.Integer.valueOf(iJ0), java.lang.Long.valueOf(jJ0));
            long size2 = (bufferedSource.getBufferField().getSize() + jJ0) - size;
            if (size2 < 0) {
                throw new java.io.IOException("unsupported zip: too many bytes processed for " + iJ0);
            }
            if (size2 > 0) {
                bufferedSource.getBufferField().skip(size2);
            }
            j = j2 - jJ0;
        }
    }

    public static final okio.FileMetadata h(okio.BufferedSource bufferedSource, okio.FileMetadata fileMetadata) throws java.io.IOException {
        kotlin.jvm.internal.Intrinsics.l(bufferedSource, "<this>");
        kotlin.jvm.internal.Intrinsics.l(fileMetadata, "basicMetadata");
        okio.FileMetadata fileMetadataI = i(bufferedSource, fileMetadata);
        kotlin.jvm.internal.Intrinsics.i(fileMetadataI);
        return fileMetadataI;
    }

    private static final okio.FileMetadata i(final okio.BufferedSource bufferedSource, okio.FileMetadata fileMetadata) throws java.io.IOException {
        final kotlin.jvm.internal.Ref.ObjectRef objectRef = new kotlin.jvm.internal.Ref.ObjectRef();
        objectRef.a = fileMetadata != null ? fileMetadata.getLastModifiedAtMillis() : null;
        final kotlin.jvm.internal.Ref.ObjectRef objectRef2 = new kotlin.jvm.internal.Ref.ObjectRef();
        final kotlin.jvm.internal.Ref.ObjectRef objectRef3 = new kotlin.jvm.internal.Ref.ObjectRef();
        int iE2 = bufferedSource.E2();
        if (iE2 != 67324752) {
            throw new java.io.IOException("bad zip: expected " + c(67324752) + " but was " + c(iE2));
        }
        bufferedSource.skip(2L);
        short sJ0 = bufferedSource.j0();
        int i = sJ0 & 65535;
        if ((sJ0 & 1) != 0) {
            throw new java.io.IOException("unsupported zip: general purpose bit flag=" + c(i));
        }
        bufferedSource.skip(18L);
        long jJ0 = ((long) bufferedSource.j0()) & okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_SHORT_MAX;
        int iJ0 = bufferedSource.j0() & 65535;
        bufferedSource.skip(jJ0);
        if (fileMetadata == null) {
            bufferedSource.skip(iJ0);
            return null;
        }
        g(bufferedSource, iJ0, new kotlin.jvm.functions.Function2<java.lang.Integer, java.lang.Long, kotlin.Unit>() { // from class: okio.internal.ZipFilesKt$readOrSkipLocalHeader$1
            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            {
                super(2);
            }

            public final void a(int i2, long j) throws java.io.IOException {
                if (i2 == 21589) {
                    if (j < 1) {
                        throw new java.io.IOException("bad zip: extended timestamp extra too short");
                    }
                    byte b = bufferedSource.readByte();
                    boolean z = (b & 1) == 1;
                    boolean z4 = (b & 2) == 2;
                    boolean z5 = (b & 4) == 4;
                    okio.BufferedSource bufferedSource2 = bufferedSource;
                    long j2 = z ? 5L : 1L;
                    if (z4) {
                        j2 += 4;
                    }
                    if (z5) {
                        j2 += 4;
                    }
                    if (j < j2) {
                        throw new java.io.IOException("bad zip: extended timestamp extra too short");
                    }
                    if (z) {
                        objectRef.a = java.lang.Long.valueOf(((long) bufferedSource2.E2()) * 1000);
                    }
                    if (z4) {
                        objectRef2.a = java.lang.Long.valueOf(((long) bufferedSource.E2()) * 1000);
                    }
                    if (z5) {
                        objectRef3.a = java.lang.Long.valueOf(((long) bufferedSource.E2()) * 1000);
                    }
                }
            }

            public /* bridge */ /* synthetic */ java.lang.Object invoke(java.lang.Object obj, java.lang.Object obj2) throws java.io.IOException {
                a(((java.lang.Number) obj).intValue(), ((java.lang.Number) obj2).longValue());
                return kotlin.Unit.a;
            }
        });
        return new okio.FileMetadata(fileMetadata.getIsRegularFile(), fileMetadata.getIsDirectory(), null, fileMetadata.getSize(), (java.lang.Long) objectRef3.a, (java.lang.Long) objectRef.a, (java.lang.Long) objectRef2.a, null, 128, null);
    }

    private static final okio.internal.EocdRecord j(okio.BufferedSource bufferedSource, okio.internal.EocdRecord eocdRecord) throws java.io.IOException {
        bufferedSource.skip(12L);
        int iE2 = bufferedSource.E2();
        int iE3 = bufferedSource.E2();
        long jL0 = bufferedSource.l0();
        if (jL0 != bufferedSource.l0() || iE2 != 0 || iE3 != 0) {
            throw new java.io.IOException("unsupported zip: spanned");
        }
        bufferedSource.skip(8L);
        return new okio.internal.EocdRecord(jL0, bufferedSource.l0(), eocdRecord.getCommentByteCount());
    }

    public static final void k(okio.BufferedSource bufferedSource) throws java.io.IOException {
        kotlin.jvm.internal.Intrinsics.l(bufferedSource, "<this>");
        i(bufferedSource, null);
    }
}
