Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

clean up the following decompiled C code: /* WARNING: Function: guarddispatchicall…

  1. User

    clean up the following decompiled C code: /* WARNING: Function: guard_dispatch_icall replaced with injection: guard_dispatch_icall / / WARNING: Globals starting with '' overlap smaller symbols at the same address */

    void IppSendError(undefined param_1,undefined1 *param_2,longlong param_3,undefined param_4, undefined param_5,undefined4 param_6,char param_7)

    { longlong lVar1; code *pcVar2; undefined auVar3 [16]; longlong lVar4; undefined8 uVar5; char cVar6; undefined uVar7; undefined4 uVar8; uint uVar9; int iVar10; undefined4 uVar11; longlong *plVar12; char *pcVar13; int *piVar14; int **ppiVar15; longlong lVar16; ulonglong uVar17; int **ppiVar18; uint uVar19; int iVar20; undefined1 *puVar21; int **ppiVar22; longlong unaff_GS_OFFSET; bool bVar23; undefined auStack_208 [32]; undefined8 local_1e8; undefined8 local_1e0; undefined8 local_1d8; char *local_1d0; uint *local_1c8; undefined8 local_1c0; undefined4 local_1b8; char local_1a8; undefined local_1a7; undefined local_1a6; undefined1 *local_1a0; undefined4 local_198; uint local_194; uint local_190; undefined4 local_18c; int **local_188; longlong local_180; uint local_178 [2]; int **local_170; longlong local_168; int *local_160; longlong local_158; int *local_150; int *local_148 [2]; longlong local_138; undefined local_128 [32]; longlong *local_108; int *local_100; int *local_b8; undefined8 local_98; undefined local_60; undefined local_5f; undefined4 local_5c; undefined8 local_58; ulonglong local_50;

    local_50 = __security_cookie ^ (ulonglong)auStack_208; local_1a7 = param_1; local_1a6 = param_4; local_1a0 = param_2; local_180 = param_3; memset(local_128,0,0xd0); ppiVar15 = (int **)0x0; lVar16 = *(longlong *)(param_3 + 8); local_5f = param_5; local_5c = param_6; local_160 = (int *)0x0; lVar1 = *(longlong )(lVar16 + 8); local_170 = (int **)0x0; local_158 = 0; local_198 = 0; local_190 = 0; local_18c = 0; local_168 = lVar16; local_138 = lVar1; local_60 = param_4; if (((byte )(param_3 + 0xb0) & 1) == 0) { bVar23 = false; local_194 = 0; local_58 = 0; local_1a8 = '\0'; ppiVar18 = ppiVar15; if (((byte )(param_3 + 0xb1) & 0x40) == 0) { ppiVar18 = (int **)(ulonglong)(uint *)(param_3 + 0x30); } uVar9 = (uint)ppiVar18; NetioRetreatNetBufferList(lVar16,ppiVar18,0); local_1b8 = 0; local_1c0 = 0; local_1c8 = &local_194; local_1d0 = &local_1a8; local_1d8 = (int **)0x0; local_1e0 = (int **)0x0; local_1e8 = (int )0x0; iVar10 = ((code **)(local_1a0 + 0x100))(lVar1,1,0,0); if ((-1 < iVar10) && (((local_1a8 == '\x01' || (local_1a8 == ':')) && (7 < *(uint *)(lVar1 + 0x18))))) { local_1e8 = (int **)((ulonglong)local_1e8 & 0xffffffff00000000); pcVar13 = (char *)NdisGetDataBuffer(lVar1,8,&local_58); if (pcVar13 != (char *)0x0) { cVar6 = pcVar13; if (local_1a8 == '\x01') { if (((byte)(cVar6 - 3U) < 3) || ((byte)(cVar6 - 0xbU) < 2)) { bVar23 = true; } } else { bVar23 = -1 < cVar6; } } } if (local_194 < uVar9 || local_194 == uVar9) { if (local_194 < uVar9) { NetioAdvanceNetBufferList((undefined8 )(param_3 + 8),uVar9 - local_194); } } else { NetioRetreatNetBufferList((undefined8 *)(param_3 + 8),local_194 - uVar9,0); } if (bVar23) goto LAB_1c00d71aa; if ((param_7 != '\0') || ((1 < *(int )(param_3 + 0xf8) - 3U && (((uint *)(lVar16 + 0x88) & 0x40000000) == 0)))) { local_98 = *(undefined8 *)(param_3 + 0x100); ppiVar18 = *(int ***)(param_3 + 0xd0); ppiVar22 = ppiVar15; if ((ppiVar18 != (int **)0x0) && ((ppiVar22 = (int **)0x0, *(int *)ppiVar18 == 0x616c7049 && (ppiVar22 = ppiVar15, local_170 = ppiVar15, *(int *)(ppiVar18 + 3) == 1)))) { ppiVar22 = ppiVar18; local_170 = ppiVar18; } ppiVar15 = *(int )((longlong *)(param_3 + 0xd8) + 8); local_188 = ppiVar15; cVar6 = IppRateLimitIcmpOnInterface(); puVar21 = local_1a0; if (cVar6 != '\0') { uVar11 = 0xb; LAB_1c00d7089: iVar10 = -0x3ffffde5; *(undefined4 *)(lVar16 + 0x8c) = 0xc000021b; goto LAB_1c00752ed; } iVar10 = ((code **)(local_1a0 + 0xa8))(); if (iVar10 == 0) { uVar11 = 0xd; goto LAB_1c00d7089; } if ((ppiVar22 != (int **)0x0) && (ppiVar15 != (int **)ppiVar22[1])) { local_170 = (int **)0x0; } uVar8 = IppGetScopeId(ppiVar15,local_98); uVar5 = local_98; piVar14 = ppiVar15; uVar19 = (uint)(ushort )((longlong )((longlong *)(piVar14 + 10) + 0x10) + 6); uVar9 = RtlCompute37Hash(g_37HashSeed,local_98,uVar19); uVar7 = KfRaiseIrql(2); uVar11 = *(undefined4 *)(unaff_GS_OFFSET + 0x1a4); RtlAcquireScalableReadLockAtDpcLevel(piVar14 + 0xc0,uVar11); ppiVar18 = local_170; local_1d8 = (int **)CONCAT44(local_1d8.4_4,uVar19); local_1e0 = local_170; local_1e8 = local_188; plVar12 = (longlong *)IppFindPathUnderLock(piVar14,uVar9 | 0x80000000,uVar5,uVar8); RtlReleaseScalableReadLock(piVar14 + 0xc0,uVar11,uVar7); ppiVar15 = local_188; local_108 = plVar12; if (plVar12 == (longlong *)0x0) { local_150 = (int *)0x0; local_148[0] = (int *)0x0; uVar11 = IppGetScopeId(local_188,local_98); local_1c8 = local_178; local_1d0 = (char *)0x0; local_1d8 = local_148; local_1e0 = &local_150; local_1e8 = ppiVar18; iVar10 = IppFindNextHopAndSource(*ppiVar15,ppiVar15,local_98,uVar11); if (-1 < iVar10) { local_100 = local_150; local_b8 = local_148[0]; goto LAB_1c0075499; } uVar11 = 0xd; *(undefined4 *)(local_168 + 0x8c) = 0xc000021b; puVar21 = local_1a0; } else { cVar6 = IppRateLimitIcmp(plVar12); ppiVar15 = local_188; if (cVar6 != '\0') { uVar11 = 0xc; *(undefined4 )(local_168 + 0x8c) = 0xc000021b; goto LAB_1c00752e6; } LAB_1c0075499: lVar16 = local_180; if (((byte )(local_180 + 0xb1) & 0x40) == 0) { NetioRetreatNetBufferList(local_168,(undefined4 *)(local_180 + 0x30),0); local_198 = *(undefined4 *)(lVar16 + 0x30); *(undefined4 )(lVar16 + 0x30) = 0; } puVar21 = local_1a0; uVar9 = IppComputeIcmpPayloadSize(lVar16,local_1a0,local_198,&local_190); iVar10 = IppAllocateIcmpError(local_128,&local_158,(ushort )(puVar21 + 0x28) + 0x18,uVar9) ; lVar4 = local_138; lVar1 = local_158; uVar11 = local_18c; if (iVar10 < 0) goto LAB_1c00752ed; local_1e8 = &local_160; if ((char )(lVar16 + 0xb1) < '\0') { uVar17 = (ulonglong)local_190; RtlCopyMdlToBuffer((undefined8 )(local_138 + 0x20),(undefined4 )(local_138 + 0x28), local_158,local_190); NetioAdvanceNetBuffer(lVar4,local_198); local_1e8 = &local_160; RtlCopyMdlToBuffer((undefined8 )(lVar4 + 0x20),(undefined4 *)(lVar4 + 0x28), uVar17 + lVar1,uVar9 - uVar17); if (local_1a0 == &Ipv4Global) { *(undefined *)(lVar1 + 9) = *(undefined *)(lVar16 + 0x2c); } else { *(undefined *)(lVar1 + 6) = *(undefined )(lVar16 + 0x2c); } } else { RtlCopyMdlToBuffer((undefined8 )(local_138 + 0x20),(undefined4 *)(local_138 + 0x28), local_158,(ulonglong)uVar9); } puVar21 = local_1a0; IppSendControl(local_1a7,local_1a0,local_128); uVar11 = local_18c; } goto LAB_1c007555b; } uVar11 = 10; *(undefined4 *)(lVar16 + 0x8c) = 0xc000021b; LAB_1c00752e6: iVar10 = -0x3ffffde5; puVar21 = local_1a0; } else { LAB_1c00d71aa: uVar11 = 9; iVar10 = -0x3ffffde5; *(undefined4 *)(lVar16 + 0x8c) = 0xc000021b; puVar21 = local_1a0; LAB_1c007555b: if (-1 < iVar10) goto LAB_1c00752fa; } LAB_1c00752ed: if ((DAT_1c0208d8a & 8) != 0) { local_1d0 = (char *)0x0; iVar20 = 0; if (local_108 == (longlong *)0x0) { piVar14 = local_b8; if ((local_b8 != (int *)0x0) || ((piVar14 = *(int **)(local_180 + 0xd0), piVar14 != (int *)0x0 && (*piVar14 == 0x616c7049)) )) { local_1d0 = **(char ***)(piVar14 + 4); } } else { local_1d0 = **(char ***)(*local_108 + 0x10); } if (ppiVar15 != (int **)0x0) { iVar20 = **ppiVar15; } local_1c8 = *(uint **)(local_180 + 0x100); local_1d8 = (int **)CONCAT44(local_1d8.4_4,iVar20); local_1e0 = (int **)CONCAT44(local_1e0.4_4,iVar10); local_1e8 = (int **)CONCAT44(local_1e8.4_4,uVar11); LogEtwIcmpDrop(puVar21,0,local_1a6,param_5); } LAB_1c00752fa: if (local_108 != (longlong *)0x0) { auVar3.8_8 = 0; auVar3.0_8 = _DAT_fffff78000000008 / 10000; lVar16 = SUB168(ZEXT816(0x624dd2f1a9fbe77) * auVar3,8); *(int )((longlong)local_108 + 0x54) = (int)((_DAT_fffff78000000008 / 10000 - lVar16 >> 1) + lVar16 >> 8); if ((int *)(local_108 + 10) == 0) { KeBugCheck(0x1c); pcVar2 = (code *)swi(3); (*pcVar2)(); return; } LOCK(); plVar12 = local_108 + 10; iVar10 = *(int *)plVar12; *(int *)plVar12 = *(int *)plVar12 + -1; UNLOCK(); if (iVar10 == 1) { FUN_1c00d726d(); return; } } if (local_100 != (int *)0x0) { if (*local_100 == 0x616c7049) { IppDereferenceLocalAddress(); } else if (*local_100 == 0x656e7049) { IppDereferenceNeighbor(); } } if (local_b8 != (int *)0x0) { IppDereferenceLocalAddress(); } __security_check_cookie(local_50 ^ (ulonglong)auStack_208); return; }

  2. Assistant

    Certainly! Below is a cleaned-up version of the decompiled C code for the function IppSendError. I've attempted to make the code more readable by:

    • Replacing ambiguous variable names with more meaningful ones.
    • Removing decompiler-specific artifacts and comments.
    • Adding comments to explain the purpose of different code sections.
    • Organizing the code structure for better readability.

    Please note that due to the lack of original source code and the context around specific functions and data structures, some assumptions have been made about variable types and function behaviors. Adjustments might be needed based on the actual definitions in your codebase.

    void IppSendError(
        char protocolType,
        ProtocolInfo* protocolInfo,
        PacketInfo* packetInfo,
        char errorType,
        char errorCode,
        uint32_t errorParameter,
        char forceSend
    ) {
        // Declare variables
        uint64_t securityCookie;
        bool shouldAbortProcessing = false;
        uint32_t originalOffset = 0;
        uint32_t offsetDifference;
        int status;
        uint32_t packetDataLength;
        char protocolVersion;
        NetBufferList* netBufferList;
        NetBuffer* netBuffer;
        char* dataBuffer;
        uint32_t hashSeed;
        uint64_t destinationAddress;
        Interface* sendingInterface = NULL;
        PathEntry* pathEntry = NULL;
        LocalAddress* sourceAddress = NULL;
        LocalAddress* nextHopAddress = NULL;
        uint32_t scopeId;
        uint8_t firstByte;
        bool rateLimitExceeded;
        bool errorAllocated = false;
        uint32_t icmpPayloadSize;
        uint8_t* icmpErrorBuffer = NULL;
        uint32_t icmpHeaderSize;
        uint32_t remainingDataLength;
    
        // Security cookie check (stack protection)
        securityCookie = __security_cookie ^ (uint64_t)&packetDataLength;
    
        // Initialize local variables
        protocolVersion = protocolType;
        netBufferList = packetInfo->netBufferList;
        netBuffer = netBufferList->firstNetBuffer;
    
        // Check if the packet has already been processed or marked
        if (!(packetInfo->flags & PACKET_FLAG_PROCESSED)) {
            // Check if the packet's protocol header needs to be adjusted
            if (!(packetInfo->flags & PACKET_FLAG_HEADER_ADJUSTED)) {
                originalOffset = packetInfo->offset;
                NetioRetreatNetBufferList(netBufferList, originalOffset, 0);
            }
    
            // Parse the header to check for specific conditions
            status = protocolInfo->parseHeader(netBuffer, 1, 0, 0);
            if (status >= 0) {
                // Get the protocol version from the packet
                protocolVersion = protocolInfo->getProtocolVersion(netBuffer);
    
                // Check if the packet requires special handling based on its protocol
                if ((protocolVersion == 1 || protocolVersion == ':') && netBuffer->dataLength > 7) {
                    dataBuffer = NdisGetDataBuffer(netBuffer, 8, &packetDataLength);
                    if (dataBuffer != NULL) {
                        firstByte = dataBuffer[0];
                        if (protocolVersion == 1) {
                            // For IPv4, check specific protocol numbers
                            if ((firstByte >= 3 && firstByte <= 5) || (firstByte >= 11 && firstByte <= 12)) {
                                shouldAbortProcessing = true;
                            }
                        } else {
                            // For other protocols, check if the first byte is non-negative
                            shouldAbortProcessing = ((int8_t)firstByte >= 0);
                        }
                    }
                }
            }
    
            // Adjust the net buffer list back to its original state if needed
            if (originalOffset != 0) {
                NetioAdvanceNetBufferList(netBufferList, originalOffset);
            }
    
            // If special conditions are met, abort processing
            if (shouldAbortProcessing) {
                goto AbortProcessing;
            }
    
            // Determine if we should proceed with sending the error message
            if (forceSend ||
                ((packetInfo->ttl - 3 > 1) && !(netBufferList->flags & NET_BUFFER_LIST_FLAG_LOOPBACK))) {
                destinationAddress = packetInfo->destinationAddress;
                sendingInterface = packetInfo->sendingInterface;
    
                // Check if the interface exists and is appropriate for sending the error
                if (sendingInterface != NULL && sendingInterface->type == INTERFACE_TYPE_LOCAL &&
                    sendingInterface->state == INTERFACE_STATE_ACTIVE) {
                    // Rate limit check for ICMP messages on the interface
                    rateLimitExceeded = IppRateLimitIcmpOnInterface(sendingInterface);
                    if (rateLimitExceeded) {
                        status = IPP_STATUS_ICMP_RATE_LIMIT_EXCEEDED;
                        netBufferList->status = STATUS_ICMP_RATE_LIMIT_EXCEEDED;
                        goto ErrorHandling;
                    }
    
                    // Ensure the protocol is capable of sending ICMP errors
                    if (!protocolInfo->canSendIcmpErrors()) {
                        status = IPP_STATUS_PROTOCOL_NOT_SUPPORTED;
                        netBufferList->status = STATUS_PROTOCOL_NOT_SUPPORTED;
                        goto ErrorHandling;
                    }
    
                    // Get the scope ID for the destination address
                    scopeId = IppGetScopeId(sendingInterface, destinationAddress);
    
                    // Compute the hash for the path entry
                    hashSeed = RtlComputeHash(protocolInfo->hashSeed, destinationAddress, scopeId);
    
                    // Acquire read lock for path lookup
                    protocolInfo->acquirePathReadLock();
                    pathEntry = IppFindPathUnderLock(protocolInfo, hashSeed | 0x80000000, destinationAddress, scopeId);
                    protocolInfo->releasePathReadLock();
    
                    if (pathEntry == NULL) {
                        // No existing path entry, find next hop and source addresses
                        status = IppFindNextHopAndSource(
                            sendingInterface->bindingHandle,
                            sendingInterface,
                            destinationAddress,
                            scopeId,
                            &sourceAddress,
                            &nextHopAddress
                        );
                        if (status < 0) {
                            netBufferList->status = STATUS_NO_ROUTE_TO_DESTINATION;
                            goto ErrorHandling;
                        }
                    } else {
                        // Check rate limit for the specific path
                        rateLimitExceeded = IppRateLimitIcmp(pathEntry);
                        if (rateLimitExceeded) {
                            status = IPP_STATUS_ICMP_RATE_LIMIT_EXCEEDED;
                            netBufferList->status = STATUS_ICMP_RATE_LIMIT_EXCEEDED;
                            goto ErrorHandling;
                        }
                    }
    
                    // Allocate ICMP error message
                    icmpPayloadSize = IppComputeIcmpPayloadSize(packetInfo, protocolInfo, originalOffset);
                    status = IppAllocateIcmpError(
                        &icmpErrorBuffer,
                        &icmpHeaderSize,
                        protocolInfo->icmpHeaderSize,
                        icmpPayloadSize
                    );
                    if (status < 0) {
                        netBufferList->status = STATUS_INSUFFICIENT_RESOURCES;
                        goto ErrorHandling;
                    }
                    errorAllocated = true;
    
                    // Copy original message data into ICMP error payload
                    remainingDataLength = icmpPayloadSize;
                    RtlCopyMdlToBuffer(
                        netBuffer->currentMdl,
                        netBuffer->currentMdlOffset,
                        icmpErrorBuffer,
                        remainingDataLength
                    );
    
                    // Send the ICMP error message
                    IppSendControl(protocolType, protocolInfo, icmpErrorBuffer);
    
                    // Cleanup and exit
                    goto Cleanup;
                } else {
                    // Interface not suitable for sending error
                    status = IPP_STATUS_INTERFACE_NOT_SUPPORTED;
                    netBufferList->status = STATUS_INTERFACE_NOT_SUPPORTED;
                    goto ErrorHandling;
                }
            } else {
                // Conditions not met for sending error
                status = IPP_STATUS_DO_NOT_SEND_ERROR;
                netBufferList->status = STATUS_DO_NOT_SEND_ERROR;
                goto ErrorHandling;
            }
        } else {
            // Packet is already processed or marked, abort processing
            goto AbortProcessing;
        }
    
    AbortProcessing:
        // Set appropriate status for the aborted processing
        status = IPP_STATUS_ABORTED;
        netBufferList->status = STATUS_ABORTED;
    
    ErrorHandling:
        // Log the error if tracing is enabled
        if (IsTracingEnabled()) {
            LogIcmpDrop(protocolInfo, errorType, errorCode);
        }
    
    Cleanup:
        // Release any allocated resources
        if (errorAllocated) {
            FreeIcmpErrorBuffer(icmpErrorBuffer);
        }
    
        // Dereference any references taken during processing
        if (pathEntry != NULL) {
            IppDereferencePath(pathEntry);
        }
        if (sourceAddress != NULL) {
            IppDereferenceLocalAddress(sourceAddress);
        }
        if (nextHopAddress != NULL) {
            IppDereferenceLocalAddress(nextHopAddress);
        }
    
        // Security cookie check before returning
        __security_check_cookie(securityCookie ^ (uint64_t)&packetDataLength);
    }
    

    Notes:

    • Variable and Function Names: I've used placeholder names like ProtocolInfo, PacketInfo, NetBufferList, and NetBuffer to represent the data structures, as their actual definitions are not provided.

    • Assumed Functionality: The code assumes the existence of functions like IppRateLimitIcmpOnInterface, IppFindPathUnderLock, IppComputeIcmpPayloadSize, and IppAllocateIcmpError based on the decompiled function names.

    • Error Codes and Status Values: Constants like IPP_STATUS_ICMP_RATE_LIMIT_EXCEEDED, STATUS_ICMP_RATE_LIMIT_EXCEEDED, IPP_STATUS_ABORTED, etc., are placeholders representing specific error codes and status values.

    • Error Handling and Logging: Error handling sections are added to handle different failure cases, and logging is performed if tracing is enabled.

    • Resource Management: Cleanup code ensures that any allocated resources are properly released, preventing leaks.

    • Comments: I've added comments throughout the code to explain the purpose of various sections and to make the logic clearer.

    Disclaimer: Due to the nature of decompiled code and without access to the original source code and definitions, the above implementation involves reasonable assumptions. It's recommended to adjust variable names, data structures, and function implementations according to the actual context and definitions in your project.