// The MIT License (MIT)
//
// Copyright (C) 2026 Scott Duensing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// obfuscate.h -- Release build name obfuscation
//
// Replaces form and control names with generated tokens (C1, C2, ...)
// across the compiled module AND the raw .frm text resources. This
// hinders casual decompilation by removing meaningful identifiers
// from event handler names (e.g., BtnHello_Click -> C3_Click).

#ifndef DVXBASIC_OBFUSCATE_H
#define DVXBASIC_OBFUSCATE_H

#include "../runtime/vm.h"

// One .frm text after obfuscation (data is newly-allocated).
typedef struct {
    uint8_t *data;
    int32_t  len;
} BasObfFrmT;

// Obfuscate form/control names in the module and all .frm texts.
//
// Reads original names from the Begin declarations in each .frm,
// generates C1..Cn (skipping any name that is also a control type or a
// property key, since bytecode cannot tell those apart), then rewrites:
//   - The .frm text (Begin-line names and property values naming a
//     control, stripping the trailing BASIC code section after the
//     outer form closes)
//   - Module string constants matching any original name, and
//     <OrigName>_<Event> handler names used as SetEvent targets
//   - Procedure names matching <OrigName>_<Event>
//   - formVarInfo entries keyed by form name
//
// frmTexts / frmLens: input .frm buffers (one per form).
// outFrms: caller-allocated array of frmCount entries; function fills
// in .data (malloc'd, caller frees) and .len for each.
void basObfuscateNames(BasModuleT *mod, const char **frmTexts, const int32_t *frmLens, int32_t frmCount, BasObfFrmT *outFrms);


// Strip comments from .frm text.
//
// Removes both whole-line comments (lines whose first non-whitespace
// token is `'` or `REM`) and trailing comments (everything from the
// first unquoted `'` to end of line). Pure-whitespace lines are
// dropped. Quoted string literals are preserved as-is.
//
// srcLen: input length; outBuf: caller-allocated buffer of outCap bytes.
// Returns the number of bytes written.
int32_t basStripFrmComments(const char *src, int32_t srcLen, uint8_t *outBuf, int32_t outCap);

#endif // DVXBASIC_OBFUSCATE_H
