@cosyte/dicom 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.5
4
+
5
+ ### Patch Changes
6
+
7
+ - c827734: `serializeDicom()` and `deidentify()` now write a value over 65,534 bytes in a VR with a 16-bit length field as `UN`, as PS3.5 section 6.2.2 requires, instead of throwing a `RangeError`.
8
+
9
+ Under the Explicit VR transfer syntaxes, VRs such as `CS`, `DS`, `LO`, `PN`, `UI` and `US` carry a 16-bit length field, and the element encoder put every length through it unchecked. A value the reader tolerated at the odd length FFFFH, padded to 65,536 bytes on write, made `serializeDicom()` throw a `RangeError` it does not document, and so did a multi-valued UID that `deidentify()` remapped past 65,534 bytes; inside a Sequence, `deidentify()` threw it itself while rebuilding the Item. Such a value is now written in the long form with VR `UN`, Little Endian under Explicit VR Big Endian too, and reads back byte for byte. A Private Creator, for which the same section forbids `UN`, is not rewritten.
10
+
3
11
  ## 0.1.4
4
12
 
5
13
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -11,7 +11,7 @@ var __export = (target, all) => {
11
11
  };
12
12
 
13
13
  // src/version.ts
14
- var VERSION = "0.1.4";
14
+ var VERSION = "0.1.5";
15
15
 
16
16
  // src/dictionary/index.ts
17
17
  var dictionary_exports = {};
@@ -14943,6 +14943,7 @@ function toDate(value, options) {
14943
14943
  return Number.isNaN(instant.getTime()) ? void 0 : instant;
14944
14944
  }
14945
14945
  var UNDEFINED_LENGTH4 = 4294967295;
14946
+ var MAX_SHORT_FORM_LENGTH = 65534;
14946
14947
  var NULL_PAD_VRS = /* @__PURE__ */ new Set(["UI", "OB", "OW", "OF", "OD", "OL", "OV", "UN"]);
14947
14948
  function padValue(value, vr) {
14948
14949
  if (value.length % 2 === 0) return buffer.Buffer.from(value);
@@ -14961,6 +14962,19 @@ function uint32(n, littleEndian) {
14961
14962
  else buf.writeUInt32BE(n, 0);
14962
14963
  return buf;
14963
14964
  }
14965
+ function littleEndianValue(value, vr, littleEndian) {
14966
+ const stride = BE_VR_STRIDE[vr];
14967
+ if (littleEndian || stride === 0) return value;
14968
+ const swapped = buffer.Buffer.from(value);
14969
+ const whole = swapped.subarray(0, swapped.length - swapped.length % stride);
14970
+ if (stride === 2) whole.swap16();
14971
+ else if (stride === 4) whole.swap32();
14972
+ else whole.swap64();
14973
+ return swapped;
14974
+ }
14975
+ function isPrivateCreator(group, element) {
14976
+ return group % 2 === 1 && element >= 16 && element <= 255;
14977
+ }
14964
14978
  function isFullSpanElement(el, encoding) {
14965
14979
  if (el.length === UNDEFINED_LENGTH4) return true;
14966
14980
  return el.vr === "SQ" && encoding !== "implicit";
@@ -14992,6 +15006,17 @@ function encodeDatasetElement(el, encoding, rawBytes = el.rawBytes) {
14992
15006
  value
14993
15007
  ]);
14994
15008
  }
15009
+ if (value.length > MAX_SHORT_FORM_LENGTH && !isPrivateCreator(group, element)) {
15010
+ return buffer.Buffer.concat([
15011
+ uint16(group, littleEndian),
15012
+ uint16(element, littleEndian),
15013
+ buffer.Buffer.from("UN", "ascii"),
15014
+ buffer.Buffer.from([0, 0]),
15015
+ // reserved (PS3.5 §7.1.2)
15016
+ uint32(value.length, littleEndian),
15017
+ littleEndianValue(value, el.vr, littleEndian)
15018
+ ]);
15019
+ }
14995
15020
  return buffer.Buffer.concat([
14996
15021
  uint16(group, littleEndian),
14997
15022
  uint16(element, littleEndian),