Matching Buffers to COBOL REDEFINE Structures
The Problem
When a COBOL copybook contains REDEFINES clauses, the same memory region is described by multiple competing field layouts. A single raw buffer can be interpreted in several structurally valid ways, but only one interpretation reflects the data the sending program actually wrote.
01 ACCOUNT-RECORD.
05 ACCT-TYPE PIC X(1).
05 ACCT-DATA.
10 PERSONAL-INFO.
15 FULL-NAME PIC X(30).
15 DATE-OF-BIRTH PIC 9(8).
10 BUSINESS-INFO REDEFINES PERSONAL-INFO.
15 COMPANY-NAME PIC X(25).
15 TAX-ID PIC X(13).
10 TRUST-INFO REDEFINES PERSONAL-INFO.
15 TRUST-NAME PIC X(30).
15 TRUSTEE-CODE PIC 9(8).
The challenge is: given a raw byte buffer received from the mainframe, how do you determine which REDEFINE branch was actually populated, so you can produce a correct, meaningful JSON output?
📘 Key insight
With n independent REDEFINE groups in a copybook, the total number of interpretation paths grows as the Cartesian product of all branch options. A copybook with just 4 REDEFINE groups of 3 branches each yields 3⁴ = 81 possible interpretations.
The Three Strategies
- Strategy 1: Explicit Selection. You know the buffer structure in advance and can specify exactly which REDEFINE branches to select at design time.
- Strategy 2: Design-Time Discovery. You enumerate, at design time, only the REDEFINE paths that are actually used in production, rather than testing all permutations.
- Strategy 3: Runtime Matching. You attempt to match the buffer against candidate REDEFINE paths at runtime, iterating until a valid interpretation is found.
Strategy 1: Explicit Selection
In this approach, you already know, before processing any data, which REDEFINE branch applies. This knowledge typically comes from system documentation, a discriminator field in the record, or coordination with the sending application team.
How It Works
Analyze Copybook → Lock REDEFINE Choices → Configure Parser → Deserialize Buffer → Output JSON
The parser or converter is configured with a fixed set of REDEFINE branch selections. Every incoming buffer is then interpreted using this single, predetermined layout. There is no ambiguity at runtime: the structure is fully resolved before the first byte is processed.
Characteristics
- Pro: Zero runtime overhead; the structure is resolved once at startup.
- Pro: 100% accuracy when the assumption is correct.
- Pro: Simplest implementation and easiest to test.
- Con: Requires intimate knowledge of the data source.
- Con: Breaks silently if the sender starts using a different branch.
- Con: Not viable when a single stream carries multiple record variants.
Strategy 2: Design-Time Discovery
Rather than assuming a single branch, you investigate, before deployment, which REDEFINE paths actually appear in real production data. You might analyze sample files, consult with mainframe developers, or run profiling against historical buffers. The result is a curated list of active paths, much smaller than the full Cartesian product of all possible branch permutations.
How It Works
Analyze Copybook → Profile Real Data → Curate Active Paths → Build Matching Rules → Deserialize
This typically involves examining discriminator values, field-level heuristics (for example, "if bytes 12–14 are numeric, this is layout B"), or using domain knowledge to prune impossible combinations. The result is a decision map that covers all realistic scenarios without resorting to brute-force permutation. For example, in the copybook above, ACCT-TYPE = 'P' could select PERSONAL-INFO and ACCT-TYPE = 'B' could select BUSINESS-INFO.
❗️ Important
The quality of this strategy depends entirely on how thoroughly you profile the real data. Missing an edge-case path means those records will either fail or be misinterpreted.
Characteristics
- Pro: Handles multiple record layouts within a single data stream.
- Pro: Greatly reduces the search space compared to full permutation.
- Pro: High accuracy when profiling is thorough.
- Con: Significant upfront analysis effort; may require access to mainframe subject-matter experts.
- Con: The curated path list must be maintained as copybooks evolve.
- Con: Risk of blind spots if sample data isn't representative.
Strategy 3: Runtime Matching
In this fully automated approach, the parser generates candidate REDEFINE interpretations and attempts to deserialize the buffer against each one at runtime. It evaluates each attempt using validation heuristics (numeric field validity, string plausibility, packed-decimal integrity, field-level constraints) and selects the interpretation that best fits.
How It Works
Receive Buffer → Generate Candidates → Try Each Path → Score & Validate → Best Match → JSON
The matching logic may check packed decimal sign nibbles, validate date fields, evaluate whether alphanumeric fields contain printable characters, or score how many fields pass their PIC clause constraints. The first candidate that passes all validation checks, or the highest-scoring one, wins.
Conceptual pseudocode (not OpenLegacy code or configuration):
function matchBuffer(buffer, copybook):
candidates = generateRedefinePaths(copybook)
bestScore = -1
bestResult = null
for path in candidates:
result = tryDeserialize(buffer, path)
score = validateFields(result)
if score > bestScore:
bestScore = score
bestResult = result
return bestResult.toJSON()
❗️ Combinatorial warning
Without pruning, the number of candidate paths can grow exponentially. A copybook with 5 REDEFINE groups each having 3 options creates 3⁵ = 243 candidate paths per record. For high-throughput streams, this can become a serious bottleneck.
Characteristics
- Pro: No upfront analysis; works with any copybook.
- Pro: Self-adapting; handles new record variants without reconfiguration.
- Pro: Maximum flexibility for unknown or evolving data streams.
- Con: Highest runtime cost; each record may require multiple deserialization attempts.
- Con: Accuracy depends on the quality of validation heuristics.
- Con: Risk of false positives when two layouts produce superficially valid results.
Comparison Matrix
| Dimension | Strategy 1: Explicit | Strategy 2: Design-Time | Strategy 3: Runtime |
|---|---|---|---|
| Design-Time Effort | Low: select branches once | Medium: profile and curate paths | Low: no upfront analysis needed |
| Runtime Performance | Optimal: single-pass parse | Near-optimal: conditional branch, no iteration | Variable: multiple parse attempts per record |
| Result Accuracy | 100% when the assumption holds | High: bounded by profiling quality | Heuristic: bounded by validation logic |
| Adaptability | Rigid: single layout only | Moderate: requires re-profiling | High: self-adapting |
| Maintenance | Minimal | Ongoing: path list updates | Minimal: heuristics are generic |
| Risk Profile | Silent failure if the assumption breaks | Gaps if profiling misses edge cases | False positives from ambiguous layouts |
Deep Dive: Design-Time Effort
Strategy 1: Minimal
The developer reads the copybook, selects the relevant REDEFINE branch, and configures the parser. This is often a matter of minutes and doesn't require access to sample data. However, it implicitly assumes the developer has domain knowledge or documentation about which branch the sender uses.
Strategy 2: Moderate to Significant
This strategy front-loads work into a discovery phase. The developer must obtain representative sample data (ideally production snapshots), analyze discriminator patterns, and build a mapping table. For complex copybooks with nested REDEFINES, this can take days and may require collaboration with mainframe application teams who understand the business logic governing which layouts are used.
The payoff is a well-documented, validated mapping that provides near-certain accuracy with near-zero runtime cost. However, every time the copybook changes or a new record variant enters the stream, the mapping must be revisited.
Strategy 3: Minimal (Shifted Effort)
The most complex runtime strategy requires the least upfront effort from the developer. The matching engine is a reusable component: once built, it can be pointed at any copybook. The effort is shifted from per-copybook design work to one-time engine development. However, tuning the validation heuristics to produce accurate results across diverse copybooks can be a significant engineering investment.
Deep Dive: Runtime Performance
Throughput Impact
For high-volume mainframe data pipelines (millions of records per batch), the performance difference between strategies is material.
| Metric | Explicit | Design-Time | Runtime |
|---|---|---|---|
| Parse ops / record | 1 | 1 (+ condition eval) | 1 … n (worst case = all paths) |
| Branching cost | None | O(1) lookup or simple if/else | Validation scoring per attempt |
| Memory overhead | Single layout in memory | All active layouts loaded | All permutations (or lazy generation) |
| Scalability | Linear with volume | Linear with volume | Linear × paths with volume |
📘 Optimization note
Strategy 3 performance can be improved with early-exit optimizations: if the first validation check fails (for example, an expected numeric field contains non-numeric data), that candidate path is immediately discarded without completing the full deserialization. Ordering candidates so that the most common layouts come first can also reduce the average number of attempts.
Deep Dive: Result Accuracy
What Can Go Wrong
Strategy 1 is binary: if the assumption is correct, you get correct JSON every time. If the assumption is wrong (for instance, the sender starts writing a different REDEFINE branch), every record is silently misinterpreted. There's no fallback and often no error signal, because the buffer is the same length regardless of which branch was used.
Strategy 2 offers strong accuracy for known paths. The risk lies in the unknown: a newly introduced record variant, a rare branch that wasn't present in the profiling sample, or a copybook update that adds branches. These edge cases will either cause a match failure (detectable) or a misclassification (harder to catch).
Strategy 3 faces the most nuanced accuracy challenges. Consider two REDEFINE branches where one defines PIC X(10) and the other defines PIC 9(10) over the same bytes. If the data happens to be a 10-digit number, both branches will validate successfully. The heuristic must resolve this ambiguity, and it won't always choose correctly. Accuracy is fundamentally bounded by how distinguishable the REDEFINE branches are from each other at the byte level.
❗️ The ambiguity problem
COBOL REDEFINES were not designed with self-describing data in mind. Unlike tagged unions or discriminated types in modern languages, a REDEFINE carries no tag bits. The same byte sequence is always valid under the original definition. Disambiguation is a best-effort exercise unless an explicit discriminator field exists.
Recommendation
No single strategy is universally best. The right choice depends on your constraints:
- Best for accuracy: Strategy 1 + 2. Combine explicit selection where possible, with design-time discovery as a safety net for multi-variant streams.
- Best for performance: Strategy 1. Single-pass parsing with no overhead. Suited to high-throughput pipelines with well-understood data.
- Best for flexibility: Strategy 3. Self-adapting, no configuration. Best when you can't access mainframe subject-matter experts or the data is unpredictable.
Decision Tree
Use this flow to select the right strategy for your situation:
- Do you know which REDEFINE branch the sender uses?
- Yes, always the same branch: Strategy 1 (Explicit Selection).
- No, or it varies by record: continue to question 2.
- Can you access sample production data and/or mainframe subject-matter experts?
- Yes: Strategy 2 (Design-Time Discovery).
- No: continue to question 3.
- Is the throughput requirement high (>100K records/sec)?
- No: Strategy 3 (Runtime Matching).
- Yes: Strategy 3 with aggressive caching and early-exit optimization, or invest in Strategy 2 despite the effort.
📘 Hybrid approach
In practice, many production systems combine strategies. Use Strategy 1 for well-known, stable copybooks. Use Strategy 2 for critical business feeds where accuracy is paramount. Fall back to Strategy 3 for ad-hoc or exploratory data integration work.