' File splitter: split data into chunks, join chunks back, verify
#COMPILE EXE
#DIM ALL

FUNCTION SplitData(BYVAL data AS STRING, BYVAL chunkSize AS LONG, c1 AS STRING, c2 AS STRING, c3 AS STRING, c4 AS STRING, c5 AS STRING, nc AS LONG) AS LONG
    LOCAL i  AS LONG
    LOCAL numChunks  AS LONG
    LOCAL startPos  AS LONG
    c1 = "": c2 = "": c3 = "": c4 = "": c5 = ""
    numChunks = (LEN(data) + chunkSize - 1) / chunkSize
    IF numChunks < 1 THEN numChunks = 1
    nc = numChunks
    IF numChunks >= 1 THEN c1 = MID$(data, 1, chunkSize)
    IF numChunks >= 2 THEN c2 = MID$(data, chunkSize + 1, chunkSize)
    IF numChunks >= 3 THEN c3 = MID$(data, chunkSize * 2 + 1, chunkSize)
    IF numChunks >= 4 THEN c4 = MID$(data, chunkSize * 3 + 1, chunkSize)
    IF numChunks >= 5 THEN c5 = MID$(data, chunkSize * 4 + 1, chunkSize)
    FUNCTION = numChunks
END FUNCTION

FUNCTION JoinTwo(BYVAL a AS STRING, BYVAL b AS STRING) AS STRING
    FUNCTION = a + b
END FUNCTION

FUNCTION SimpleChecksum(BYVAL data AS STRING) AS LONG
    LOCAL i  AS LONG
    LOCAL sum  AS LONG
    sum = 0
    FOR i = 1 TO LEN(data)
        sum = sum + ASC(MID$(data, i, 1))
    NEXT i
    FUNCTION = sum AND 65535
END FUNCTION

SUB PrintChunk(BYVAL idx AS LONG, BYVAL data AS STRING, BYVAL label AS STRING)
    PRINT label + " [" + FORMAT$(idx) + "]: len=" + FORMAT$(LEN(data)) + " cs=" + FORMAT$(SimpleChecksum(data))
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL original  AS STRING
    LOCAL c1  AS STRING
    LOCAL c2  AS STRING
    LOCAL c3  AS STRING
    LOCAL c4  AS STRING
    LOCAL c5  AS STRING
    LOCAL joined  AS STRING
    LOCAL partial  AS STRING
    LOCAL nc AS LONG
    PRINT "=== File Splitter ==="
    PRINT
    original = "This is a test of the file splitter utility for PBXB64."
    original = original + " It demonstrates splitting data into chunks and reassembling."
    original = original + " Each chunk has size and checksum for verification."
    original = original + " The join operation reconstructs the original data exactly."
    PRINT "Original (" + FORMAT$(LEN(original)) + " bytes):"
    PRINT original
    PRINT
    CALL SplitData(original, 30, c1, c2, c3, c4, c5, nc)
    PRINT "Split into " + FORMAT$(nc) + " chunks (30 bytes each):"
    CALL PrintChunk(1, c1, "Chunk")
    CALL PrintChunk(2, c2, "Chunk")
    CALL PrintChunk(3, c3, "Chunk")
    IF nc >= 4 THEN CALL PrintChunk(4, c4, "Chunk")
    IF nc >= 5 THEN CALL PrintChunk(5, c5, "Chunk")
    PRINT
    joined = JoinTwo(c1, c2)
    joined = JoinTwo(joined, c3)
    IF nc >= 4 THEN joined = JoinTwo(joined, c4)
    IF nc >= 5 THEN joined = JoinTwo(joined, c5)
    PRINT "Joined (" + FORMAT$(LEN(joined)) + " bytes):"
    PRINT joined
    PRINT
    IF joined = original THEN
        PRINT "Verification: PASS"
    ELSE
        PRINT "Verification: FAIL"
    END IF
    PRINT
    partial = "Short data test"
    CALL SplitData(partial, 50, c1, c2, c3, c4, c5, nc)
    PRINT "Single chunk (" + FORMAT$(nc) + " chunk, " + FORMAT$(LEN(c1)) + " bytes)"
    PRINT
    PRINT "File splitter tests complete."
    FUNCTION = 0
END FUNCTION
