# SPDX-License-Identifier: GPL-2.0-only
# This file is part of Scapy RPC
# See https://scapy.net/ for more information
# Copyright (C) Gabriel Potter

# unknwnbase.idl 10.0.10240.0 (29 Jul 2015)

"""
RPC definitions for the following interfaces:
- IUnknown (v0.0): 00000000-0000-0000-C000-000000000046
- AsyncIUnknown (v0.0): 000e0000-0000-0000-C000-000000000046
- IClassFactory (v0.0): 00000001-0000-0000-C000-000000000046
This file is auto-generated by midl-to-scapy, do not modify.
"""

import uuid

from scapy.fields import StrFixedLenField
from scapy.layers.dcerpc import (
    NDRPacket,
    DceRpcOp,
    NDRConfStrLenField,
    NDRFullPointerField,
    NDRIntField,
    NDRPacketField,
    NDRShortField,
    NDRSignedIntField,
    register_com_interface,
)

IUNKNOWN_OPNUMS = {  # 0: Opnum0NotUsedOnWire,
    # 1: Opnum1NotUsedOnWire,
    # 2: Opnum2NotUsedOnWire
}
register_com_interface(
    name="IUnknown",
    uuid=uuid.UUID("00000000-0000-0000-C000-000000000046"),
    opnums=IUNKNOWN_OPNUMS,
)


class GUID(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRIntField("Data1", 0),
        NDRShortField("Data2", 0),
        NDRShortField("Data3", 0),
        StrFixedLenField("Data4", "", length=8),
    ]


class Begin_QueryInterface_Request(NDRPacket):
    fields_desc = [NDRPacketField("riid", GUID(), GUID)]


class Begin_QueryInterface_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class Finish_QueryInterface_Request(NDRPacket):
    fields_desc = []


class Finish_QueryInterface_Response(NDRPacket):
    fields_desc = [
        StrFixedLenField("ppvObject", "", length=0),
        NDRIntField("status", 0),
    ]


class Begin_AddRef_Request(NDRPacket):
    fields_desc = []


class Begin_AddRef_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class Finish_AddRef_Request(NDRPacket):
    fields_desc = []


class Finish_AddRef_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class Begin_Release_Request(NDRPacket):
    fields_desc = []


class Begin_Release_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class Finish_Release_Request(NDRPacket):
    fields_desc = []


class Finish_Release_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


ASYNCIUNKNOWN_OPNUMS = {  # 0: Opnum0NotUsedOnWire,
    # 1: Opnum1NotUsedOnWire,
    # 2: Opnum2NotUsedOnWire,
    3: DceRpcOp(Begin_QueryInterface_Request, Begin_QueryInterface_Response),
    4: DceRpcOp(Finish_QueryInterface_Request, Finish_QueryInterface_Response),
    5: DceRpcOp(Begin_AddRef_Request, Begin_AddRef_Response),
    6: DceRpcOp(Finish_AddRef_Request, Finish_AddRef_Response),
    7: DceRpcOp(Begin_Release_Request, Begin_Release_Response),
    8: DceRpcOp(Finish_Release_Request, Finish_Release_Response),
}
register_com_interface(
    name="AsyncIUnknown",
    uuid=uuid.UUID("000e0000-0000-0000-C000-000000000046"),
    opnums=ASYNCIUNKNOWN_OPNUMS,
)


class MInterfacePointer(NDRPacket):
    ALIGNMENT = (4, 4)
    DEPORTED_CONFORMANTS = ["abData"]
    fields_desc = [
        NDRIntField("ulCntData", None, size_of="abData"),
        NDRConfStrLenField(
            "abData", "", size_is=lambda pkt: pkt.ulCntData, conformant_in_struct=True
        ),
    ]


class CreateInstance_Request(NDRPacket):
    fields_desc = [
        NDRFullPointerField(
            NDRPacketField("pUnkOuter", MInterfacePointer(), MInterfacePointer)
        ),
        NDRPacketField("riid", GUID(), GUID),
    ]


class CreateInstance_Response(NDRPacket):
    fields_desc = [
        StrFixedLenField("ppvObject", "", length=0),
        NDRIntField("status", 0),
    ]


class RemoteCreateInstance_Request(NDRPacket):
    fields_desc = [NDRPacketField("riid", GUID(), GUID)]


class RemoteCreateInstance_Response(NDRPacket):
    fields_desc = [
        NDRFullPointerField(
            NDRPacketField("ppvObject", MInterfacePointer(), MInterfacePointer)
        ),
        NDRIntField("status", 0),
    ]


class LockServer_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("fLock", 0)]


class LockServer_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RemoteLockServer_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("fLock", 0)]


class RemoteLockServer_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


ICLASSFACTORY_OPNUMS = {  # 0: Opnum0NotUsedOnWire,
    # 1: Opnum1NotUsedOnWire,
    # 2: Opnum2NotUsedOnWire,
    3: DceRpcOp(CreateInstance_Request, CreateInstance_Response),
    4: DceRpcOp(RemoteCreateInstance_Request, RemoteCreateInstance_Response),
    5: DceRpcOp(LockServer_Request, LockServer_Response),
    6: DceRpcOp(RemoteLockServer_Request, RemoteLockServer_Response),
}
register_com_interface(
    name="IClassFactory",
    uuid=uuid.UUID("00000001-0000-0000-C000-000000000046"),
    opnums=ICLASSFACTORY_OPNUMS,
)
