package io.netty.handler.codec.dns;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.handler.codec.UnsupportedMessageTypeException;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest;
import io.netty.util.internal.StringUtil;

/* JADX INFO: loaded from: classes.dex */
public class DefaultDnsRecordEncoder implements DnsRecordEncoder {
    private static final int PREFIX_MASK = 7;

    public static int calculateEcsAddressLength(int i2, int i3) {
        return (i2 >>> 3) + (i3 != 0 ? 1 : 0);
    }

    private void encodeOptEcsRecord(DnsOptEcsRecord dnsOptEcsRecord, ByteBuf byteBuf) {
        encodeRecord0(dnsOptEcsRecord, byteBuf);
        int iSourcePrefixLength = dnsOptEcsRecord.sourcePrefixLength();
        int iScopePrefixLength = dnsOptEcsRecord.scopePrefixLength();
        int i2 = iSourcePrefixLength & 7;
        byte[] bArrAddress = dnsOptEcsRecord.address();
        int length = bArrAddress.length << 3;
        if (length < iSourcePrefixLength || iSourcePrefixLength < 0) {
            throw new IllegalArgumentException(iSourcePrefixLength + ": " + iSourcePrefixLength + " (expected: 0 >= " + length + ')');
        }
        short sAddressNumber = (short) (bArrAddress.length == 4 ? InternetProtocolFamily.IPv4 : InternetProtocolFamily.IPv6).addressNumber();
        int iCalculateEcsAddressLength = calculateEcsAddressLength(iSourcePrefixLength, i2);
        int i3 = iCalculateEcsAddressLength + 8;
        byteBuf.writeShort(i3);
        byteBuf.writeShort(8);
        byteBuf.writeShort(i3 - 4);
        byteBuf.writeShort(sAddressNumber);
        byteBuf.writeByte(iSourcePrefixLength);
        byteBuf.writeByte(iScopePrefixLength);
        if (i2 <= 0) {
            byteBuf.writeBytes(bArrAddress, 0, iCalculateEcsAddressLength);
            return;
        }
        int i4 = iCalculateEcsAddressLength - 1;
        byteBuf.writeBytes(bArrAddress, 0, i4);
        byteBuf.writeByte(padWithZeros(bArrAddress[i4], i2));
    }

    private void encodeOptPseudoRecord(DnsOptPseudoRecord dnsOptPseudoRecord, ByteBuf byteBuf) {
        encodeRecord0(dnsOptPseudoRecord, byteBuf);
        byteBuf.writeShort(0);
    }

    private void encodePtrRecord(DnsPtrRecord dnsPtrRecord, ByteBuf byteBuf) {
        encodeRecord0(dnsPtrRecord, byteBuf);
        encodeName(dnsPtrRecord.hostname(), byteBuf);
    }

    private void encodeRawRecord(DnsRawRecord dnsRawRecord, ByteBuf byteBuf) {
        encodeRecord0(dnsRawRecord, byteBuf);
        ByteBuf byteBufContent = dnsRawRecord.content();
        int i2 = byteBufContent.readableBytes();
        byteBuf.writeShort(i2);
        byteBuf.writeBytes(byteBufContent, byteBufContent.readerIndex(), i2);
    }

    private void encodeRecord0(DnsRecord dnsRecord, ByteBuf byteBuf) {
        encodeName(dnsRecord.name(), byteBuf);
        byteBuf.writeShort(dnsRecord.type().intValue());
        byteBuf.writeShort(dnsRecord.dnsClass());
        byteBuf.writeInt((int) dnsRecord.timeToLive());
    }

    private static byte padWithZeros(byte b2, int i2) {
        int i3;
        switch (i2) {
            case 0:
                return (byte) 0;
            case 1:
                i3 = b2 & DefaultBinaryMemcacheRequest.REQUEST_MAGIC_BYTE;
                break;
            case 2:
                i3 = b2 & 192;
                break;
            case 3:
                i3 = b2 & 224;
                break;
            case 4:
                i3 = b2 & 240;
                break;
            case 5:
                i3 = b2 & 248;
                break;
            case 6:
                i3 = b2 & 252;
                break;
            case 7:
                i3 = b2 & 254;
                break;
            case 8:
                return b2;
            default:
                throw new IllegalArgumentException(a.l("lowOrderBitsToPreserve: ", i2));
        }
        return (byte) i3;
    }

    public void encodeName(String str, ByteBuf byteBuf) {
        if (DefaultDnsRecordDecoder.ROOT.equals(str)) {
            byteBuf.writeByte(0);
            return;
        }
        for (String str2 : str.split("\\.")) {
            int length = str2.length();
            if (length == 0) {
                break;
            }
            byteBuf.writeByte(length);
            ByteBufUtil.writeAscii(byteBuf, str2);
        }
        byteBuf.writeByte(0);
    }

    @Override // io.netty.handler.codec.dns.DnsRecordEncoder
    public final void encodeQuestion(DnsQuestion dnsQuestion, ByteBuf byteBuf) {
        encodeName(dnsQuestion.name(), byteBuf);
        byteBuf.writeShort(dnsQuestion.type().intValue());
        byteBuf.writeShort(dnsQuestion.dnsClass());
    }

    @Override // io.netty.handler.codec.dns.DnsRecordEncoder
    public void encodeRecord(DnsRecord dnsRecord, ByteBuf byteBuf) {
        if (dnsRecord instanceof DnsQuestion) {
            encodeQuestion((DnsQuestion) dnsRecord, byteBuf);
            return;
        }
        if (dnsRecord instanceof DnsPtrRecord) {
            encodePtrRecord((DnsPtrRecord) dnsRecord, byteBuf);
            return;
        }
        if (dnsRecord instanceof DnsOptEcsRecord) {
            encodeOptEcsRecord((DnsOptEcsRecord) dnsRecord, byteBuf);
        } else if (dnsRecord instanceof DnsOptPseudoRecord) {
            encodeOptPseudoRecord((DnsOptPseudoRecord) dnsRecord, byteBuf);
        } else {
            if (!(dnsRecord instanceof DnsRawRecord)) {
                throw new UnsupportedMessageTypeException(StringUtil.simpleClassName(dnsRecord));
            }
            encodeRawRecord((DnsRawRecord) dnsRecord, byteBuf);
        }
    }
}
