Skip to content

fix(legacy): read a UN element that holds a sequence as Implicit VR LE (port of #496) - #658

Merged
wayfarer3130 merged 3 commits into
1.0-betafrom
fix/1.0-beta-port-496-un-sequence
Nov 25, 2026
Merged

wayfarer3130 merged 3 commits into
1.0-betafrom
fix/1.0-beta-port-496-un-sequence

Conversation

@wayfarer3130

@wayfarer3130 wayfarer3130 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This PR is the 1.0 port of #496 from 0.x.

Summary

PS3.5 §6.2.2 says that a UN element that holds a sequence uses Implicit VR Little Endian. An Explicit VR file can have such an element with a defined length. The value of that element starts with the item tag (FFFE,E000), which is the bytes FE FF 00 E0. This PR reads that value as an SQ in all 1.x read paths. This PR also writes a UN with item datasets as SQ.

Detection rule (read)

A reader reads an Explicit VR UN value as a sequence only when all of these conditions are true:

  • The dictionary VR of the tag is SQ, or the tag has no dictionary entry (for example a private tag).
  • The value has a defined length of 8 bytes or more.
  • The value starts with the item tag (FFFE,E000).
  • The first item length is FFFFFFFF, or the first item length is not more than the value length minus 8.

A value that fails the rule stays UN bytes, as on 1.0-beta. For example, a private value with the item tag and an item length that is too large stays UN. A standard tag with the dictionary VR OB, sent as UN, keeps its OB bytes.

The items are Implicit VR Little Endian, also in an Explicit VR Big Endian file.

Writer rule

Tag.write writes a UN as SQ with undefined length only when every value is an item dataset. An item dataset is an object whose keys are all tags, for example "00100010". A UN with { InlineBinary } or { BulkDataURI } stays UN.

Cause

  • The legacy readers (DicomMessage._readTag and AsyncDicomReader) read the value with the Explicit VR syntax of the file. A private tag stayed UN with binary data. A standard tag with a dictionary VR of SQ, for example (0018,100A), gave items with wrong values.
  • The event-stream reader (fromPart10Stream, and fromPart10, which uses it) had the same problem. The fix for issue Invalid tag in sequence : Unable to parse MR Dicom file in dcmjs. Cornerstone able to parse and works fine #363 covers only a UN with undefined length. A private tag gave UN with an empty object. A standard tag with a dictionary VR of SQ caused an error.
  • Tag.write in @dcmjs-org/core wrote a UN with item objects as a UN with length 0. The writer lost the items.

Changes

  • packages/core/src/utilities/unSequence.js (new): isUnSequenceValue(view, offset, length, vr) holds the detection rule. isItemDictLike(v) holds the item dataset check. The file uses the existing SEQUENCE_ITEM_TAG and isBinaryData. @dcmjs-org/core exports both functions. The three readers and the writer use this one copy.
  • packages/legacy/src/DicomMessage.js: _readTag keeps the VR from the stream, before the dictionary override. When that VR is UN and isUnSequenceValue is true, _readTag reads the value as SQ with Implicit VR LE. _readTag also sets the stream to little endian for the items, and restores the endian after the element.
  • packages/legacy/src/AsyncDicomReader.js: readTagHeader does the same check, and gives sequenceReadSyntax in the header. readSequence, read and readSingle read the items with that syntax through options.readSyntax. readSequence also passes the options correctly to readTagHeader. Before, it passed the syntax string as the options.
  • packages/parser/src/eventStream/fromPart10Stream.js: parseOneElement does the same check, and emits the sequence with the body syntax set to Implicit VR LE. withImplicitLE sets the syntax and restores it after the sequence. The JSDoc of parseOneElement names the §6.2.2 exception.
  • packages/parser/src/core/decodeCore.js: the AD-1 contract text names the §6.2.2 exception.
  • packages/parser/src/eventStream/emit.js: uses isItemDictLike from @dcmjs-org/core. The local copy is removed.
  • packages/core/src/Tag.js: Tag.write uses isItemDictLike for the writer rule.
  • docs/specs/naturalized-dataset.md: new user requirement NAT-U-BIN-6. NAT-U-BIN-1 points to NAT-U-BIN-6. New implementation requirement NAT-I-BIN-3.
  • test/helper/ps352UnSequenceFixture.js: fixture with the §6.2.2 bytes. The fixture can write an Explicit VR Big Endian file.
  • test/sync-async-parser-equivalency.test.js: new tests. The read tests use the sync reader, the async reader, fromPart10 and fromPart10Stream:
    • DicomMessage._readTag reads the element as SQ.
    • A standard tag (0018,100A) and a private tag (0009,1010) read as SQ in all readers.
    • A private tag in Explicit VR Big Endian reads as SQ in all readers, and the element after the sequence reads correctly.
    • A private UN with the item tag and an item length that is too large stays UN with the same bytes.
    • The standard tag (0042,0011) with the dictionary VR OB, sent as UN with the item tag, keeps its bytes.
    • A read, write and read round trip keeps the sequence. The header bytes are SQ and FFFFFFFF.
    • A UN with item datasets writes as SQ with undefined length, in Explicit VR LE and in Implicit VR LE.
    • A UN with { InlineBinary } or { BulkDataURI } writes as UN.
  • test/bundle-size.test.js: each ceiling is the measured ratio, rounded up to 0.001: 1.086 (dcmjs.js) and 1.069 (dcmjs.es.js). The measured ratios after the merge of 1.0-beta are 1.0858 and 1.0684.

Verified

  • Before the fix, on 1.0-beta, 6 of the 9 first tests in sync-async-parser-equivalency.test.js failed.
  • On the first commit of this PR, the 5 new tests for the review items failed: Big Endian, item length, dictionary VR OB, { InlineBinary } and { BulkDataURI }. After the fixes, all 13 tests of the §6.2.2 group pass.
  • After the merge of origin/1.0-beta: npx jest --runInBand on bundle-size, sync-async-parser-equivalency, SequenceOfItems, defined-length-sequence, fromPart10Stream, fromPart10 and lossless-read-write gives 248 passed and 1 skipped.
  • Before the merge, a wider set of reader and writer tests (streamEquivalence, crossSource, Part10Writer, NaturalizedListener, export, data-options, data-encoding and the files above) gave 417 passed and 1 skipped.
  • The full suite did not run locally, because a local full run hung before. CI runs the full suite.
  • eslint, prettier --check and pnpm run build pass.

Not ported from #496

  • The loglevel external in the ES build of rollup.config.mjs. This change is not part of the §6.2.2 fix, and it is out of scope for this port.
  • The 0.x data.test.js test of _readTag is in sync-async-parser-equivalency.test.js in this PR, with the other §6.2.2 tests.
  • The fixes for the Big Endian read and the detection rule are not in 0.x. A separate PR does the 0.x follow-up.

Maintainer edits

🤖 Generated with Claude Code

…E (port of #496)

PS3.5 §6.2.2: an Explicit VR element with VR UN and a defined length, whose
value starts with the item tag (FFFE,E000), holds a sequence in Implicit VR
Little Endian.

- legacy DicomMessage._readTag and AsyncDicomReader read such a value as SQ
  (shared isUnSequenceValue).
- parser fromPart10Stream (and fromPart10) emit it as a sequence.
- core Tag.write writes a UN whose values are all item objects as SQ with
  undefined length.
- bundle-size ceilings 1.085 and 1.067.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130 and others added 2 commits November 25, 2026 09:18
…tem check

- core: isUnSequenceValue (dictionary VR SQ or no entry, item tag,
  first item length undefined or within the value) and isItemDictLike,
  one copy for the legacy sync, legacy async and event-stream readers.
- legacy sync reader: read the items as little endian in an Explicit VR
  Big Endian body (the items were lost).
- Tag.write: a UN is written as SQ only when every value is an item
  dataset; { InlineBinary } and { BulkDataURI } stay UN.
- parser: JSDoc and AD-1 text name the §6.2.2 exception.
- docs: NAT-U-BIN-6 and NAT-I-BIN-3.
- tests: Explicit VR Big Endian, bad item length, dictionary VR OB,
  binary references on write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-496-un-sequence

# Conflicts:
#	test/bundle-size.test.js
#	test/sync-async-parser-equivalency.test.js
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 25, 2026 16:53
@wayfarer3130
wayfarer3130 merged commit 03220d8 into 1.0-beta Nov 25, 2026
4 checks passed
@wayfarer3130
wayfarer3130 deleted the fix/1.0-beta-port-496-un-sequence branch November 25, 2026 16:53
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.19 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant