/*
 * beacon_compatibility.c -- Stub implementations of the Beacon API
 * Step 05: Minimal stubs so the loader compiles and links.
 *
 * BeaconPrintf and BeaconOutput produce visible output.
 * Everything else is a no-op stub that returns 0/NULL.
 * Full implementations come in a later step.
 *
 * This file is compiled as part of the LOADER -- it is NOT a BOF.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "beacon_compatibility.h"

/* ================================================================
 * Global output buffer variables
 *
 * In a real C2 the output would be buffered and sent back to the
 * team server.  Here we just declare the vars so the loader links.
 * ================================================================ */
char*  beacon_compatibility_output      = NULL;
int    beacon_compatibility_size        = 0;
int    beacon_compatibility_offset      = 0;

/* ================================================================
 * InternalFunctions table
 *
 * Declared extern in beacon_compatibility.h.  Populated by the
 * loader (COFFLoader.c) at startup.  Defined here as a global
 * so both translation units share the same storage.
 * ================================================================ */
unsigned char* InternalFunctions[30][2] = { {0} };

/* ================================================================
 * Output API -- these produce visible output for debugging
 * ================================================================ */

void BeaconPrintf(int type, char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

void BeaconOutput(int type, char* data, int len) {
    if (data && len > 0) {
        fwrite(data, 1, len, stdout);
    }
}

/* ================================================================
 * Data Parsing API -- empty stubs
 * ================================================================ */

void BeaconDataParse(datap* parser, char* buffer, int size) {
    if (parser) {
        parser->original = buffer;
        parser->buffer   = buffer;
        parser->length   = size;
        parser->size     = size;
    }
}

int BeaconDataInt(datap* parser) {
    return 0;
}

short BeaconDataShort(datap* parser) {
    return 0;
}

int BeaconDataLength(datap* parser) {
    return 0;
}

char* BeaconDataExtract(datap* parser, int* size) {
    if (size) *size = 0;
    return NULL;
}

/* ================================================================
 * Format Buffer API -- empty stubs
 * ================================================================ */

void BeaconFormatAlloc(formatp* format, int maxsz) {
    return;
}

void BeaconFormatReset(formatp* format) {
    return;
}

void BeaconFormatFree(formatp* format) {
    return;
}

void BeaconFormatAppend(formatp* format, char* text, int len) {
    return;
}

void BeaconFormatPrintf(formatp* format, char* fmt, ...) {
    return;
}

char* BeaconFormatToString(formatp* format, int* size) {
    if (size) *size = 0;
    return NULL;
}

void BeaconFormatInt(formatp* format, int value) {
    return;
}

/* ================================================================
 * Token / Process API -- empty stubs
 * ================================================================ */

void BeaconUseToken(HANDLE token) {
    return;
}

void BeaconRevertToken(void) {
    return;
}

BOOL BeaconIsAdmin(void) {
    return FALSE;
}

void BeaconGetSpawnTo(BOOL x86, char* buffer, int length) {
    return;
}

BOOL BeaconSpawnTemporaryProcess(BOOL x86, BOOL ignoreToken,
        STARTUPINFOA* si, PROCESS_INFORMATION* pi) {
    return FALSE;
}

void BeaconInjectProcess(HANDLE hProc, int pid,
        char* payload, int p_len, int offset,
        char* arg, int a_len) {
    return;
}

void BeaconInjectTemporaryProcess(PROCESS_INFORMATION* pi,
        char* payload, int p_len, int offset,
        char* arg, int a_len) {
    return;
}

void BeaconCleanupProcess(PROCESS_INFORMATION* pi) {
    return;
}

/* ================================================================
 * Utility -- empty stub
 * ================================================================ */

BOOL toWideChar(char* src, wchar_t* dst, int max) {
    return FALSE;
}

/* ================================================================
 * Output retrieval -- stub returns NULL
 * ================================================================ */

char* BeaconGetOutputData(int* outsize) {
    if (outsize) *outsize = 0;
    return NULL;
}
