Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/alloc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { invalidParameter } from './util/errors.js';

/**
* Creates a new byteArray of the same type (Uint8Array or Buffer) of the specified length.
* @param byteArray the underlying byteArray (either Uint8Array or Buffer)
Expand All @@ -10,5 +12,5 @@ export default function alloc (byteArray, length) {
} else if (byteArray instanceof Uint8Array) {
return new Uint8Array(length);
}
throw 'dicomParser.alloc: unknown type for byteArray';
throw new TypeError(invalidParameter('byteArray', 'should be of type Uint8Array or Buffer'));
}
50 changes: 26 additions & 24 deletions src/bigEndianByteArrayParser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { overflowErrorMessage, underflowErrorMessage } from './util/errors.js';

/**
* Internal helper functions for parsing different types from a big-endian byte array
*/
Expand All @@ -9,16 +11,16 @@ export default {
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed unsigned int 16
* @throws error if buffer overread would occur
* @returns {number} the parsed unsigned int 16
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readUint16 (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readUint16: position cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}
if (position + 2 > byteArray.length) {
throw 'bigEndianByteArrayParser.readUint16: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}

return (byteArray[position] << 8) + byteArray[position + 1];
Expand All @@ -30,16 +32,16 @@ export default {
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed signed int 16
* @throws error if buffer overread would occur
* @returns {number} the parsed signed int 16
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readInt16 (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readInt16: position cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}
if (position + 2 > byteArray.length) {
throw 'bigEndianByteArrayParser.readInt16: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}
var int16 = (byteArray[position] << 8) + byteArray[position + 1];
// fix sign
Expand All @@ -56,17 +58,17 @@ export default {
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed unsigned int 32
* @throws error if buffer overread would occur
* @returns {number} the parsed unsigned int 32
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readUint32 (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readUint32: position cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}

if (position + 4 > byteArray.length) {
throw 'bigEndianByteArrayParser.readUint32: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}

var uint32 = (256 * (256 * (256 * byteArray[position] +
Expand All @@ -82,17 +84,17 @@ export default {
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed signed int 32
* @throws error if buffer overread would occur
* @returns {number} the parsed signed int 32
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readInt32 (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readInt32: position cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}

if (position + 4 > byteArray.length) {
throw 'bigEndianByteArrayParser.readInt32: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}

var int32 = ((byteArray[position] << 24) +
Expand All @@ -108,17 +110,17 @@ export default {
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed 32-bit float
* @throws error if buffer overread would occur
* @returns {number} the parsed 32-bit float
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readFloat (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readFloat: position cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}

if (position + 4 > byteArray.length) {
throw 'bigEndianByteArrayParser.readFloat: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}

// I am sure there is a better way than this but this should be safe
Expand All @@ -139,17 +141,17 @@ export default {
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed 64-bit float
* @throws error if buffer overread would occur
* @returns {number} the parsed 64-bit float
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readDouble (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readDouble: position cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}

if (position + 8 > byteArray.length) {
throw 'bigEndianByteArrayParser.readDouble: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}

// I am sure there is a better way than this but this should be safe
Expand Down
8 changes: 5 additions & 3 deletions src/byteArrayParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Internal helper functions common to parsing byte arrays of any type
*/

import { overflowErrorMessage, underflowErrorMessage } from './util/errors.js';

/**
* Reads a string of 8-bit characters from an array of bytes and advances
* the position by length bytes. A null terminator will end the string
Expand All @@ -11,16 +13,16 @@
* @param position the position in the byte array to read from
* @param length the maximum number of bytes to parse
* @returns {string} the parsed string
* @throws error if buffer overread would occur
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
export function readFixedString (byteArray, position, length) {
if (length < 0) {
throw 'dicomParser.readFixedString - length cannot be less than 0';
throw new RangeError(underflowErrorMessage);
}

if (position + length > byteArray.length) {
throw 'dicomParser.readFixedString: attempt to read past end of buffer';
throw new RangeError(overflowErrorMessage);
}

var result = '';
Expand Down
65 changes: 35 additions & 30 deletions src/byteStream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sharedCopy from './sharedCopy.js';
import { readFixedString } from './byteArrayParser.js';
import { invalidParameter, missingParameter } from './util/errors.js';

/**
*
Expand All @@ -11,63 +12,67 @@ import { readFixedString } from './byteArrayParser.js';
*
* */

/**
* Constructor for ByteStream objects.
* @param byteArrayParser a parser for parsing the byte array
* @param byteArray a Uint8Array containing the byte stream
* @param position (optional) the position to start reading from. 0 if not specified
* @constructor
* @throws will throw an error if the byteArrayParser parameter is not present
* @throws will throw an error if the byteArray parameter is not present or invalid
* @throws will throw an error if the position parameter is not inside the byte array
*/
export default class ByteStream {
constructor (byteArrayParser, byteArray, position) {
/**
* Constructor for ByteStream objects.
* @param byteArrayParser a parser for parsing the byte array
* @param {Uint8Array | Buffer} byteArray a Uint8Array containing the byte stream
* @param {number} position (optional) the position to start reading from. 0 if not specified
* @constructor
* @throws {TypeError} missing or invalid `byteArrayParser` or `byteArray` parameter
* @throws {RangeError} incorrect start `position` parameter
*/
constructor (byteArrayParser, byteArray, position = 0) {
if (byteArrayParser === undefined) {
throw 'dicomParser.ByteStream: missing required parameter \'byteArrayParser\'';
throw new TypeError(missingParameter('byteArrayParser'));
}
if (byteArray === undefined) {
throw 'dicomParser.ByteStream: missing required parameter \'byteArray\'';
throw new TypeError(missingParameter('byteArray'));
}
if ((byteArray instanceof Uint8Array) === false &&
((typeof Buffer === 'undefined') ||
(byteArray instanceof Buffer) === false)) {
throw 'dicomParser.ByteStream: parameter byteArray is not of type Uint8Array or Buffer';
throw new TypeError(invalidParameter('byteArray', 'should be of type Uint8Array or Buffer'));
}
if (position < 0) {
throw 'dicomParser.ByteStream: parameter \'position\' cannot be less than 0';
throw new RangeError(`ByteStream start position should be a positive integer or 0, got ${position.toString(10)}`);
}
if (position >= byteArray.length) {
throw 'dicomParser.ByteStream: parameter \'position\' cannot be greater than or equal to \'byteArray\' length';
throw new RangeError('ByteStream start position should be a positive integer strictly smaller than byteArray' +
`length (${byteArray.length}), got ${position.toString(10)}`);
}
this.byteArrayParser = byteArrayParser;
this.byteArray = byteArray;
this.position = position ? position : 0;

this.position = position;
this.warnings = []; // array of string warnings encountered while parsing
}

/**
* Safely seeks through the byte stream. Will throw an exception if an attempt
* is made to seek outside of the byte array.
* @param offset the number of bytes to add to the position
* @throws error if seek would cause position to be outside of the byteArray
* Safely seeks through the byte stream.
* @param {number} offset the number of bytes to add to the position
* @throws {RangeError} seek would cause position to be outside the byteArray
*/
seek (offset) {
if (this.position + offset < 0) {
throw 'dicomParser.ByteStream.prototype.seek: cannot seek to position < 0';
const newPosition = this.position + offset;

if (newPosition < 0) {
throw new RangeError(
`ByteStream cannot seek outside the byteArray tried to read position ${newPosition.toString(10)}`
);
}
this.position += offset;
this.position = newPosition;
}

/**
* Returns a new ByteStream object from the current position and of the requested number of bytes
* @param numBytes the length of the byte array for the ByteStream to contain
* @returns {dicomParser.ByteStream}
* @throws error if buffer overread would occur
* @throws {RangeError} error if buffer overflow would occur
*/
readByteStream (numBytes) {
if (this.position + numBytes > this.byteArray.length) {
throw 'dicomParser.ByteStream.prototype.readByteStream: readByteStream - buffer overread';
throw new RangeError('Attempted to read stream past buffer end');
}
var byteArrayView = sharedCopy(this.byteArray, this.position, numBytes);

Expand All @@ -76,7 +81,7 @@ export default class ByteStream {
return new ByteStream(this.byteArrayParser, byteArrayView);
}

getSize() {
getSize () {
return this.byteArray.length;
}

Expand All @@ -86,7 +91,7 @@ export default class ByteStream {
* the position by 2 bytes
*
* @returns {*} the parsed unsigned int 16
* @throws error if buffer overread would occur
* @throws {RangeError} tried to read outside the buffer
*/
readUint16 () {
var result = this.byteArrayParser.readUint16(this.byteArray, this.position);
Expand All @@ -101,7 +106,7 @@ export default class ByteStream {
* the position by 2 bytes
*
* @returns {*} the parse unsigned int 32
* @throws error if buffer overread would occur
* @throws {RangeError} tried to read outside the buffer
*/
readUint32 () {
var result = this.byteArrayParser.readUint32(this.byteArray, this.position);
Expand All @@ -117,7 +122,7 @@ export default class ByteStream {
* but will not effect advancement of the position.
* @param length the maximum number of bytes to parse
* @returns {string} the parsed string
* @throws error if buffer overread would occur
* @throws {RangeError} tried to read outside the buffer
*/
readFixedString (length) {
var result = readFixedString(this.byteArray, this.position, length);
Expand Down
5 changes: 4 additions & 1 deletion src/findAndSetUNElementLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
* Internal helper functions for parsing DICOM elements
*/

import { missingParameter } from './util/errors.js';

/**
* reads from the byte stream until it finds the magic number for the Sequence Delimitation
* Item item and then sets the length of the element
* @param byteStream
* @param element
* @throws {TypeError} missing required parameter `byteStream`
*/
export default function findAndSetUNElementLength (byteStream, element) {
if (byteStream === undefined) {
throw 'dicomParser.findAndSetUNElementLength: missing required parameter \'byteStream\'';
throw new TypeError(missingParameter('byteStream'));
}

// group, element, length
Expand Down
Loading