Skip to content

Fix #3987: Arabic / Hebrew records read as logical text, stored visual - #4256

Merged
squid-protocol merged 1 commit into
mainfrom
fix/3987-bidi-visual-logical
Oct 3, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
fix/3987-bidi-visual-logical

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Resolves #3987.

What was broken

A 3270 had no BiDi engine, so the host kept Arabic and Hebrew in visual order: the characters left to right as the screen showed them. שלום is stored as the bytes of םולש. CobolRecords.text() decoded those bytes as they were. A browser treats every String as logical (reading order), so each right-to-left field came out backwards.

Changes

  • CobolRecords.text() returns logical order and putText() stores visual order back when the page is visual. sortKey() encodes the visual bytes, so keys still sort the way VSAM sorts them.
    • The transform uses java.text.Bidi from the JDK, so no new dependency. Right-to-left runs are reversed, numbers stay left to right, brackets are mirrored, and the shaped (presentation) forms of IBM420 are kept. A record that is read and written again is byte-exact.
    • CobolRecords.logical(s) / visual(s) are public, for record bytes that don't go through the codecs (COMMAREAs, hand-built records).
  • New setting data.bidi_layout: auto | logical | visual_ltr | visual_rtl.
    • visual_ltr: the record's first byte is the screen's leftmost character, IBM's host default.
    • visual_rtl: a screen-reverse terminal. Arabic is stored in reading order and numbers and Latin are reversed.
    • auto means visual_ltr under IBM420 / IBM424 (EBCDIC Arabic / Hebrew) and IBM864 / IBM862 (PC Arabic / Hebrew), and logical everywhere else. Windows-1255/1256 and ISO-8859-6/8 are logical by default. A bare visual value is rejected, because it doesn't say which side of the screen the first byte is on.
  • cp420 registered with codecs. Python ships cp424 but not cp420, so record_charset: cp420 / code_page: cp420 used to fail config validation. The table is the JDK IBM420 diff against cp037, built the same way as the national pages; bytes the JDK leaves unmapped stay undefined. cp420 and cp424 join EBCDIC_CODE_PAGES.
  • New porting rule (bidi_rules), added only on visual estates: keep Strings logical, never reverse one by hand, convert at a raw-bytes boundary with logical / visual, and flag a reference modification or INSPECT of such a field in a TODO.
  • dead_key_audit.py: allowlist bidi_layout and record_charset. Both are Data fields that target_as_dict writes with vars().

Limits

  • Visual to logical isn't always unique. On a left-to-right screen, 125 ديصر can be 125 رصيد or رصيد 125; both give the same screen and the same bytes. The reverse step uses the bidi algorithm in the screen's direction, which undoes itself for words and numbers. A fuzz run over realistic Arabic/Hebrew text with digits and punctuation found 47 of 200,000 strings that don't come back exactly. Every one is built around a cluster of punctuation sitting between a right-to-left letter and a digit. The Javadoc says so.
  • No unshaping (presentation forms to base letters). It would make the write lossy, since re-shaping isn't implemented, and browsers render the presentation forms correctly.
  • The deterministic port (det/cobolrt/Cobol.text) is not changed. Its program storage stays visual, as on the mainframe. The DTO boundary there is a follow-up.

Verification

  • test_bidi_layout.py: a Java probe on IBM424 / IBM420 / IBM037 / windows-1255, compiled under each of the four layouts. It covers the read and the write, numbers inside an Arabic run, mirrored brackets, byte-exact round trips (shaped forms included), Latin unchanged, sortKey on visual bytes, and the visual_rtl Hebrew-plus-number case. It also checks config validation, which estates get the rule, the cp420 codec, and that the Java page set matches java_target.VISUAL_BIDI_PAGES. I ran it locally with JDK 21's compiler module; it skips without javac.
  • tests/cobol_mainframe, tests/cics_crucible, codec tests: pass. The first run caught a raw «» in the template; test_generated_java_is_ascii passes after escaping it.
  • ruff / mypy / dead-key / ast-accuracy audits: nothing new. Format is clean.
  • No parsing logic is touched, so no crucible run was needed.

🤖 Generated with Claude Code

A 3270 had no BiDi engine, so the host kept right-to-left text in visual order
(as the screen showed it): `שלום` is stored as the bytes of `םולש`. The entity
codecs decoded it as-is, so a modern frontend -- which takes a String as
logical -- rendered every Arabic / Hebrew field backwards.

- CobolRecords.text() returns logical order and putText() stores visual order
  under a visual page; sortKey() orders by the visual bytes VSAM holds. The
  transform is java.text.Bidi (JDK, no dependency): RTL runs reversed, numbers
  kept left to right, brackets mirrored, shaped Arabic forms kept, so a record
  read and re-written is byte-exact. CobolRecords.logical / visual are public
  for record bytes outside the codecs.
- data.bidi_layout: auto | logical | visual_ltr | visual_rtl. visual_ltr is
  IBM's host default (the first byte at the screen's left); visual_rtl is a
  screen-reverse terminal (Arabic stored in reading order, numbers reversed).
  auto = visual_ltr under IBM420 / IBM424 / IBM864 / IBM862, else logical.
- cp420 (EBCDIC Arabic) registered with codecs, from the JDK's IBM420 table:
  record_charset / code_page cp420 failed config validation before. cp420 and
  cp424 join EBCDIC_CODE_PAGES.
- A porting rule on visual estates: keep Strings logical, never reverse by
  hand, flag reference modification / INSPECT of such a field.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 4ce83c7 into main Oct 3, 2026
38 checks passed
@squid-protocol
squid-protocol deleted the fix/3987-bidi-visual-logical branch October 3, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Globe: Arabic/Hebrew 3270 Screens Render Backwards (Visual vs Logical BiDi)

1 participant