Skip to content

fix/bill un sq implicit vr ps35622 - #496

Merged
wayfarer3130 merged 12 commits into
masterfrom
fix/bill-un-sq-implicit-vr-ps35622
Nov 24, 2026
Merged

wayfarer3130 merged 12 commits into
masterfrom
fix/bill-un-sq-implicit-vr-ps35622

Conversation

@wayfarer3130

Copy link
Copy Markdown
Contributor

This is a copy of the code from #492 with some additional changes

jimOnAir and others added 7 commits April 23, 2026 14:04
…r PS3.5 §6.2.2

When an Explicit VR file encodes a sequence element with VR=UN and a
defined length, DICOM PS3.5 §6.2.2 requires that its items be parsed
using Implicit VR Little Endian regardless of the file's transfer syntax.

Without this fix, dcmjs tried to interpret the item bytes as Explicit VR,
emitted 'Invalid vr type ( - using UN', stored the value as [{}], and then
on write inflated the element length to ~843 MB (the 843 MB inflation bug
reported for files containing UDISequence (0018,100A)).

The fix records the literal wire VR before any dictionary override
(wireVrType), then after reading the length checks whether the payload
starts with the FFFE item-delimiter group bytes (FE FF in LE). If so,
the element is re-typed as SQ and the recursive item read is forced to
IMPLICIT_LITTLE_ENDIAN.

Adds a regression test that builds a hand-crafted Explicit VR buffer with
UDISequence (0018,100A) encoded as UN and verifies the inner
UniqueDeviceIdentifier (0018,1009) value is correctly recovered.
…f-8 conversion(#493)

The sync reader sets the character set to utf-8 to match the internal conversion it has performed.
…3 to 7.29.4 via bumping to preset-env 7.29.5 (#495)

* build(deps): bump @babel/plugin-transform-modules-systemjs

Bumps [@babel/plugin-transform-modules-systemjs](https://github.com/babel/babel/tree/HEAD/packages/babel-plugin-transform-modules-systemjs) from 7.23.3 to 7.29.4.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.29.4/packages/babel-plugin-transform-modules-systemjs)

---
updated-dependencies:
- dependency-name: "@babel/plugin-transform-modules-systemjs"
  dependency-version: 7.29.4
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>

* build(deps-dev): bump @babel/preset-env to 7.29.5

Pins @babel/preset-env to an exact 7.29.5 in package.json (was
^7.16.11, resolving to 7.23.8 in the lockfile) and refreshes
yarn.lock. This supersedes the Dependabot indirect bump of
@babel/plugin-transform-modules-systemjs from 7.23.3 to 7.29.4 by
updating the parent that pulls it in, so the new plugin version is
picked up naturally without a resolutions override.

Co-authored-by: Cursor <[email protected]>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Bill Wallace <[email protected]>
Co-authored-by: Cursor <[email protected]>
* feat(SplitDataView): Add get / set support for BigUint64

* feat(BufferStream): Add read / write support for BigUint64

* feat(ValueRepresentation): Add support for VR type "UV" (Unsigned 64 bit Very Long)

* feat(writeBufferStream-test): Add basic read / write tests for BigUint64

Admits further tests, in fact, leaves much to be desired

* fix: Formatting and add UV to binary VRs

* format

---------

Co-authored-by: Bill Wallace <[email protected]>
@netlify

netlify Bot commented May 13, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dcmjs2 ready!

Name Link
🔨 Latest commit c13ebf9
🔍 Latest deploy log https://app.netlify.com/projects/dcmjs2/deploys/6a04a4217309bd0008ddd31e
😎 Deploy Preview https://deploy-preview-496--dcmjs2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@jimOnAir

Copy link
Copy Markdown

Hello.
Discussed it with Copilot. Here are his findings:


1. The byte-peek guard (FE FF) is not required by the standard

Both DicomMessage.js and AsyncDicomReader.js gate the UN→SQ promotion on:

stream.peekUint8(0) === 0xfe && stream.peekUint8(1) === 0xff

PS3.5 §6.2.2 does not require a content sniff. The rule is:

"…if the Value Representation is UN and Value Length is not FFFFFFFF… the Value shall be read using Implicit VR Little Endian…"

The peek creates two problems:

  • A legitimate UN binary blob whose first bytes happen to be FE FF will be silently mis-promoted to SQ.
  • A compliant PS3.5 §6.2.2 sequence that does not start with FE FF (e.g. after any kind of stream offset issue, or a future edge case) will be silently skipped.

The conformant check is just:

wireVrType === "UN" && length !== UNDEFINED_LENGTH && length !== 0

2. length > 0 excludes valid empty sequences

An empty UN-encoded sequence (length = 0) is valid per the standard and should be promoted to SQ with an empty items array. The current length > 0 guard leaves it decoded as UN, which would corrupt a round-trip (read as UN [], written back as UN).


3. Minor: explicit !implicit guard asymmetry

AsyncDicomReader.js correctly adds !implicit as an explicit condition. In DicomMessage.js the block is structurally inside the else (explicit) branch so it's functionally guarded, but adding !implicit explicitly would make the intent defensive and symmetrical across both parsers.


4. Test gap — unknown tag path

The fixture uses (0018,100A) which is in the dictionary as SQ, so the dictionary-override path runs first. It would be good to add a test using a private/unknown tag (e.g. (0009,0010)) encoded as UN with a defined length and a valid item payload, to confirm the fix also works for tags with no dictionary entry.


Overall the structure is solid — the wireVrType tracking and async propagation are exactly right. The main ask is dropping the content sniff and letting the standard's length-based rule drive the promotion.

@igor-nanox

Copy link
Copy Markdown

Any chance of this to be released? It would be extremely helpful. Thanks in advance!

wayfarer3130 and others added 2 commits November 24, 2026 15:24
The ES build now imports loglevel instead of inlining it, so one
loglevel instance can be shared with the host application and other
libraries. The UMD builds keep loglevel bundled for <script> users.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@netlify

netlify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dcmjs2 ready!

Name Link
🔨 Latest commit 98ea145
🔍 Latest deploy log https://app.netlify.com/projects/dcmjs2/deploys/6b06130fa1688e72d3473cfa
😎 Deploy Preview https://deploy-preview-496--dcmjs2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

wayfarer3130 and others added 2 commits November 24, 2026 15:42
- Check all 4 bytes of the item tag FE FF 00 E0 (FFFE,E000) and a length
  of at least 8 in one shared isUnSequenceValue() used by both parsers.
  A UN value that fails the check stays UN.
- Use the same !implicit condition in the sync and async parsers.
- Test a private UN sequence, a FE FF value that stays UN, and a sync
  write round trip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A UN element with item objects as Value (for example, from a naturalized
or JSON dataset) now goes through the SQ writer: VR SQ in Explicit VR,
a normal sequence in Implicit VR, and undefined length for the sequence
and the items. Before, the write threw. A UN with binary values stays UN.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130

Copy link
Copy Markdown
Contributor Author

I approved this change on #492 as a maintainer edit of #492. #492 is closed, because I could not push to its branch. #496 has no additional approval, and I will merge #496 directly.

#496 contains the commit of #492 (42de140) and these maintainer edits:

  • 3e907e4: the parser checks all 4 bytes of the item tag (FE FF 00 E0) before it reads a UN value as a sequence. The sync and the async parser use the same check.
  • 98ea145: the writer writes a UN element whose value is a list of items as SQ, with undefined length.
  • 1540264: the ES build has loglevel as an external, so that one loglevel instance can be shared by several components. The UMD builds keep loglevel bundled.
  • Tests for a private tag, for a value that starts with FE FF but is not a sequence, and for a write and read round trip.

pnpm test passes (376 tests). eslint and format:check also pass.

@wayfarer3130
wayfarer3130 merged commit da9a306 into master Nov 24, 2026
20 checks passed
@wayfarer3130
wayfarer3130 deleted the fix/bill-un-sq-implicit-vr-ps35622 branch November 24, 2026 22:46
@github-project-automation github-project-automation Bot moved this from Todo to Done in dcmjs roadmap Nov 24, 2026
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.53.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

wayfarer3130 added a commit that referenced this pull request Nov 25, 2026
…E (port of #496) (#658)

* fix(legacy): read a UN element that holds a sequence as Implicit VR LE (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]>

* fix: narrow the PS3.5 §6.2.2 UN sequence rule; BE sync read; writer item 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]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants