hdf5.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. // Experimental HDF5 JavaScript reader
  2. var hdf5 = hdf5 || {};
  3. var zip = zip || require('./zip');
  4. hdf5.File = class {
  5. static open(data) {
  6. const buffer = data instanceof Uint8Array ? data : data.peek();
  7. const reader = new hdf5.BinaryReader(buffer, 0);
  8. if (reader.match('\x89HDF\r\n\x1A\n')) {
  9. return new hdf5.File(reader);
  10. }
  11. return null;
  12. }
  13. constructor(reader) {
  14. // https://support.hdfgroup.org/HDF5/doc/H5.format.html
  15. this._globalHeap = new hdf5.GlobalHeap(reader);
  16. const version = reader.byte();
  17. switch (version) {
  18. case 0:
  19. case 1: {
  20. this._freeSpaceStorageVersion = reader.byte();
  21. this._rootGroupEntryVersion = reader.byte();
  22. reader.skip(1);
  23. this._sharedHeaderMessageVersionFormat = reader.byte();
  24. reader.initialize();
  25. reader.skip(1);
  26. this._groupLeafNodeK = reader.uint16(); // 0x04?
  27. this._groupInternalNodeK = reader.uint16(); // 0x10?
  28. reader.skip(4);
  29. if (version > 0) {
  30. this._indexedStorageInternalNodeK = reader.uint16();
  31. this.seek(2); // Reserved
  32. }
  33. this._baseAddress = reader.offset();
  34. reader.offset(); // Address of File Free space Info
  35. this._endOfFileAddress = reader.offset();
  36. reader.offset(); // Driver Information Block Address
  37. if (this._baseAddress != 0) {
  38. throw new hdf5.Error('Base address is not zero.');
  39. }
  40. const rootGroupEntry = new hdf5.SymbolTableEntry(reader);
  41. this._rootGroup = new hdf5.Group(reader, rootGroupEntry, null, this._globalHeap, '', '');
  42. break;
  43. }
  44. case 2:
  45. case 3: {
  46. reader.initialize();
  47. reader.byte();
  48. this._baseAddress = reader.offset();
  49. this._superBlockExtensionAddress = reader.offset();
  50. this._endOfFileAddress = reader.offset();
  51. const rootGroupObjectHeader = new hdf5.DataObjectHeader(reader.at(reader.offset()));
  52. this._rootGroup = new hdf5.Group(reader, null, rootGroupObjectHeader, this._globalHeap, '', '');
  53. break;
  54. }
  55. default:
  56. throw new hdf5.Error('Unsupported Superblock version ' + version + '.');
  57. }
  58. }
  59. get rootGroup() {
  60. return this._rootGroup;
  61. }
  62. };
  63. hdf5.Group = class {
  64. constructor(reader, entry, objectHeader, globalHeap, parentPath, name) {
  65. this._reader = reader;
  66. this._entry = entry;
  67. this._dataObjectHeader = objectHeader;
  68. this._globalHeap = globalHeap;
  69. this._name = name;
  70. this._path = parentPath == '/' ? (parentPath + name) : (parentPath + '/' + name);
  71. }
  72. get name() {
  73. return this._name;
  74. }
  75. get path() {
  76. return this._path;
  77. }
  78. group(path) {
  79. this._decodeGroups();
  80. if (this._groups.has(path)) {
  81. return this._groups.get(path);
  82. }
  83. const index = path.indexOf('/');
  84. if (index !== -1) {
  85. const group = this.group(path.substring(0, index));
  86. if (group) {
  87. return group.group(path.substring(index + 1));
  88. }
  89. }
  90. return null;
  91. }
  92. get groups() {
  93. this._decodeGroups();
  94. return this._groups;
  95. }
  96. get attributes() {
  97. this._decodeDataObject();
  98. return this._attributes;
  99. }
  100. get value() {
  101. this._decodeDataObject();
  102. return this._value;
  103. }
  104. _decodeDataObject() {
  105. if (!this._dataObjectHeader) {
  106. const reader = this._reader.at(this._entry.objectHeaderAddress);
  107. this._dataObjectHeader = new hdf5.DataObjectHeader(reader);
  108. }
  109. if (!this._attributes) {
  110. this._attributes = new Map();
  111. for (const attribute of this._dataObjectHeader.attributes) {
  112. const name = attribute.name;
  113. const value = attribute.decodeValue(this._globalHeap);
  114. this._attributes.set(name, value);
  115. }
  116. this._value = null;
  117. const datatype = this._dataObjectHeader.datatype;
  118. const dataspace = this._dataObjectHeader.dataspace;
  119. const dataLayout = this._dataObjectHeader.dataLayout;
  120. const filterPipeline = this._dataObjectHeader.filterPipeline;
  121. if (datatype && dataspace && dataLayout) {
  122. this._value = new hdf5.Variable(this._reader, this._globalHeap, datatype, dataspace, dataLayout, filterPipeline);
  123. }
  124. }
  125. }
  126. _decodeGroups() {
  127. if (!this._groups) {
  128. this._groups = new Map();
  129. if (this._entry) {
  130. if (this._entry.treeAddress || this._entry.heapAddress) {
  131. const heap = new hdf5.Heap(this._reader.at(this._entry.heapAddress));
  132. const tree = new hdf5.Tree(this._reader.at(this._entry.treeAddress));
  133. for (const node of tree.nodes) {
  134. for (const entry of node.entries) {
  135. const name = heap.getString(entry.linkNameOffset);
  136. const group = new hdf5.Group(this._reader, entry, null, this._globalHeap, this._path, name);
  137. this._groups.set(name, group);
  138. }
  139. }
  140. }
  141. }
  142. else {
  143. this._decodeDataObject();
  144. for (const link of this._dataObjectHeader.links) {
  145. if (Object.prototype.hasOwnProperty.call(link, 'objectHeaderAddress')) {
  146. const name = link.name;
  147. const objectHeader = new hdf5.DataObjectHeader(this._reader.at(link.objectHeaderAddress));
  148. const linkGroup = new hdf5.Group(this._reader, null, objectHeader, this._globalHeap, this._path, name);
  149. this._groups.set(name, linkGroup);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. };
  156. hdf5.Variable = class {
  157. constructor(reader, globalHeap, datatype, dataspace, dataLayout, filterPipeline) {
  158. this._reader = reader;
  159. this._globalHeap = globalHeap;
  160. this._datatype = datatype;
  161. this._dataspace = dataspace;
  162. this._dataLayout = dataLayout;
  163. this._filterPipeline = filterPipeline;
  164. }
  165. get type () {
  166. return this._datatype.type;
  167. }
  168. get littleEndian() {
  169. return this._datatype.littleEndian;
  170. }
  171. get shape() {
  172. return this._dataspace.shape;
  173. }
  174. get value() {
  175. const data = this.data;
  176. if (data) {
  177. const reader = new hdf5.BinaryReader(data);
  178. const array = this._dataspace.read(this._datatype, reader);
  179. return this._dataspace.decode(this._datatype, array, array, this._globalHeap);
  180. }
  181. return null;
  182. }
  183. get data() {
  184. switch (this._dataLayout.layoutClass) {
  185. case 1: // Contiguous
  186. if (this._dataLayout.address) {
  187. return this._reader.at(this._dataLayout.address).read(this._dataLayout.size);
  188. }
  189. break;
  190. case 2: { // Chunked
  191. const dimensionality = this._dataLayout.dimensionality;
  192. const tree = new hdf5.Tree(this._reader.at(this._dataLayout.address), dimensionality);
  193. const item_size = this._dataLayout.datasetElementSize;
  194. const chunk_shape = this._dataLayout.dimensionSizes;
  195. const data_shape = this._dataspace.shape;
  196. const chunk_size = chunk_shape.reduce((a, b) => a * b, 1);
  197. const data_size = data_shape.reduce((a, b) => a * b, 1);
  198. const max_dim = data_shape.length - 1;
  199. let data_stride = 1;
  200. const data_strides = data_shape.slice().reverse().map((d2) => {
  201. const s = data_stride;
  202. data_stride *= d2;
  203. return s;
  204. }).reverse();
  205. const data = new Uint8Array(data_size * item_size);
  206. for (const node of tree.nodes) {
  207. if (node.filterMask !== 0) {
  208. return null;
  209. }
  210. let chunk = node.data;
  211. if (this._filterPipeline) {
  212. for (const filter of this._filterPipeline.filters) {
  213. chunk = filter.decode(chunk);
  214. }
  215. }
  216. const chunk_offset = node.fields;
  217. var data_pos = chunk_offset.slice();
  218. var chunk_pos = data_pos.map(() => 0);
  219. for (let chunk_index = 0; chunk_index < chunk_size; chunk_index++) {
  220. for (let i = max_dim; i >= 0; i--) {
  221. if (chunk_pos[i] >= chunk_shape[i]) {
  222. chunk_pos[i] = 0;
  223. data_pos[i] = chunk_offset[i];
  224. if (i > 0) {
  225. chunk_pos[i - 1]++;
  226. data_pos[i - 1]++;
  227. }
  228. }
  229. else {
  230. break;
  231. }
  232. }
  233. let index = 0;
  234. let inbounds = true;
  235. const length = data_pos.length - 1;
  236. for (let i = 0; i < length; i++) {
  237. const pos = data_pos[i];
  238. inbounds = inbounds && pos < data_shape[i];
  239. index += pos * data_strides[i];
  240. }
  241. if (inbounds) {
  242. let chunk_offset = chunk_index * item_size;
  243. let target_offset = index * item_size;
  244. const target_end = target_offset + item_size;
  245. while (target_offset < target_end) {
  246. data[target_offset++] = chunk[chunk_offset++];
  247. }
  248. }
  249. chunk_pos[max_dim]++;
  250. data_pos[max_dim]++;
  251. }
  252. }
  253. return data;
  254. }
  255. default: {
  256. throw new hdf5.Error("Unsupported data layout class '" + this.layoutClass + "'.");
  257. }
  258. }
  259. return null;
  260. }
  261. };
  262. hdf5.BinaryReader = class {
  263. constructor(buffer, view, position, offset, offsetSize, lengthSize) {
  264. this._buffer = buffer;
  265. this._view = view || new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  266. this._position = position || 0;
  267. this._offset = offset || 0;
  268. this._offsetSize = offsetSize;
  269. this._lengthSize = lengthSize;
  270. }
  271. initialize() {
  272. this._offsetSize = this.byte();
  273. this._lengthSize = this.byte();
  274. }
  275. skip(offset) {
  276. this._offset += offset;
  277. if (this._position + this._offset > this._buffer.length) {
  278. throw new hdf5.Error('Expected ' + (this._position + this._offset - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
  279. }
  280. }
  281. read(length) {
  282. const offset = this._offset;
  283. this.skip(length);
  284. return this._buffer.subarray(this._position + offset, this._position + this._offset);
  285. }
  286. int8() {
  287. const offset = this._offset;
  288. this.skip(1);
  289. return this._view.getInt8(this._position + offset);
  290. }
  291. byte() {
  292. const offset = this._offset;
  293. this.skip(1);
  294. return this._view.getUint8(this._position + offset);
  295. }
  296. int16() {
  297. const offset = this._position + this._offset;
  298. this.skip(2);
  299. return this._view.getInt16(offset, true);
  300. }
  301. uint16() {
  302. const offset = this._position + this._offset;
  303. this.skip(2);
  304. return this._view.getUint16(offset, true);
  305. }
  306. int32() {
  307. const offset = this._position + this._offset;
  308. this.skip(4);
  309. return this._view.getInt32(offset, true);
  310. }
  311. uint32() {
  312. const offset = this._position + this._offset;
  313. this.skip(4);
  314. return this._view.getUint32(offset, true);
  315. }
  316. int64() {
  317. const offset = this._position + this._offset;
  318. this.skip(8);
  319. return this._view.getInt64(offset, true).toNumber();
  320. }
  321. uint64() {
  322. const offset = this._position + this._offset;
  323. this.skip(8);
  324. return this._view.getUint64(offset, true).toNumber();
  325. }
  326. uint(size) {
  327. switch (size) {
  328. case 0: return this.byte();
  329. case 1: return this.uint16();
  330. case 2: return this.uint32();
  331. case 3: return this.uint64();
  332. default: throw new hdf5.Error("Unsupported uint size '" + size + "'.");
  333. }
  334. }
  335. float16() {
  336. const offset = this._offset;
  337. this.skip(2);
  338. const value = this._view.getUint16(this._position + offset, true);
  339. // decode float16 value
  340. const s = (value & 0x8000) >> 15;
  341. const e = (value & 0x7C00) >> 10;
  342. const f = value & 0x03FF;
  343. if(e == 0) {
  344. return (s ? -1 : 1) * Math.pow(2, -14) * (f / Math.pow(2, 10));
  345. }
  346. else if (e == 0x1F) {
  347. return f ? NaN : ((s ? -1 : 1) * Infinity);
  348. }
  349. return (s ? -1 : 1) * Math.pow(2, e-15) * (1 + (f / Math.pow(2, 10)));
  350. }
  351. float32() {
  352. const offset = this._position + this._offset;
  353. this.skip(4);
  354. return this._view.getFloat32(offset, true);
  355. }
  356. float64() {
  357. const offset = this._position + this._offset;
  358. this.skip(8);
  359. return this._view.getFloat64(offset, true);
  360. }
  361. string(size, encoding) {
  362. if (!size || size == -1) {
  363. let position = this._position + this._offset;
  364. while (this._buffer[position] != 0) {
  365. position++;
  366. }
  367. size = position - this._position - this._offset + 1;
  368. }
  369. const data = this.read(size);
  370. return hdf5.BinaryReader.decode(data, encoding);
  371. }
  372. static decode(data, encoding) {
  373. let content = '';
  374. if (encoding == 'utf-8') {
  375. if (!hdf5.BinaryReader._utf8Decoder) {
  376. hdf5.BinaryReader._utf8Decoder = new TextDecoder('utf-8');
  377. }
  378. content = hdf5.BinaryReader._utf8Decoder.decode(data);
  379. }
  380. else {
  381. if (!hdf5.BinaryReader._asciiDecoder) {
  382. hdf5.BinaryReader._asciiDecoder = new TextDecoder('ascii');
  383. }
  384. content = hdf5.BinaryReader._asciiDecoder.decode(data);
  385. }
  386. return content.replace(/\0/g, '');
  387. }
  388. offset() {
  389. switch (this._offsetSize) {
  390. case 8: {
  391. const position = this._position + this._offset;
  392. this.skip(8);
  393. const value = this._view.getUint64(position, true);
  394. if (value.low === -1 && value.high === -1) {
  395. return undefined;
  396. }
  397. return value.toNumber();
  398. }
  399. case 4: {
  400. const value = this.uint32();
  401. if (value === 0xffffffff) {
  402. return undefined;
  403. }
  404. return value;
  405. }
  406. default: {
  407. throw new hdf5.Error('Unsupported offset size \'' + this._offsetSize + '\'.');
  408. }
  409. }
  410. }
  411. length() {
  412. switch (this._lengthSize) {
  413. case 8: {
  414. const position = this._position + this._offset;
  415. this.skip(8);
  416. const value = this._view.getUint64(position, true);
  417. if (value.low === -1 && value.high === -1) {
  418. return undefined;
  419. }
  420. return value.toNumber();
  421. }
  422. case 4: {
  423. const value = this.uint32();
  424. if (value === 0xffffffff) {
  425. return undefined;
  426. }
  427. return value;
  428. }
  429. default: {
  430. throw new hdf5.Error('Unsupported length size \'' + this._lengthSize + '\'.');
  431. }
  432. }
  433. }
  434. at(position) {
  435. return new hdf5.BinaryReader(this._buffer, this._view, position, 0, this._offsetSize, this._lengthSize);
  436. }
  437. clone() {
  438. return new hdf5.BinaryReader(this._buffer, this._view, this._position, this._offset, this._offsetSize, this._lengthSize);
  439. }
  440. align(mod) {
  441. if (this._offset % mod != 0) {
  442. this._offset = (Math.floor(this._offset / mod) + 1) * mod;
  443. }
  444. }
  445. match(text) {
  446. if (this._position + this._offset + text.length > this._buffer.length) {
  447. return false;
  448. }
  449. const offset = this._offset;
  450. const buffer = this.read(text.length);
  451. for (let i = 0; i < text.length; i++) {
  452. if (text.charCodeAt(i) != buffer[i]) {
  453. this._offset = offset;
  454. return false;
  455. }
  456. }
  457. return true;
  458. }
  459. get position() {
  460. return this._position + this._offset;
  461. }
  462. get size() {
  463. return this._buffer.length;
  464. }
  465. };
  466. hdf5.SymbolTableNode = class {
  467. constructor(reader) {
  468. if (!reader.match('SNOD')) {
  469. throw new hdf5.Error("Not a valid 'SNOD' block.");
  470. }
  471. const version = reader.byte();
  472. if (version == 1) {
  473. reader.skip(1);
  474. const entriesUsed = reader.uint16();
  475. this.entries = [];
  476. for (let i = 0; i < entriesUsed; i++) {
  477. const entry = new hdf5.SymbolTableEntry(reader);
  478. this.entries.push(entry);
  479. }
  480. }
  481. else {
  482. throw new hdf5.Error('Unsupported symbol table node version \'' + version + '\'.');
  483. }
  484. }
  485. };
  486. hdf5.SymbolTableEntry = class {
  487. constructor(reader) {
  488. this.linkNameOffset = reader.offset();
  489. this.objectHeaderAddress = reader.offset();
  490. const cacheType = reader.uint32();
  491. reader.skip(4); // Reserved
  492. switch (cacheType) {
  493. case 0:
  494. break;
  495. case 1: {
  496. const scratchReader = reader.clone();
  497. this.treeAddress = scratchReader.offset();
  498. this.heapAddress = scratchReader.offset();
  499. break;
  500. }
  501. default:
  502. throw new hdf5.Error('Unsupported cache type \'' + cacheType + '\'.');
  503. }
  504. reader.skip(16); // Scratch-pad space
  505. }
  506. };
  507. hdf5.DataObjectHeader = class {
  508. constructor(reader) {
  509. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#ObjectHeader
  510. this.attributes = [];
  511. this.links = [];
  512. this.continuations = [];
  513. reader.match('OHDR');
  514. const version = reader.byte();
  515. switch (version) {
  516. case 1: {
  517. reader.skip(1);
  518. const count = reader.uint16();
  519. reader.uint32();
  520. const objectHeaderSize = reader.uint32();
  521. reader.align(8);
  522. let end = reader.position + objectHeaderSize;
  523. for (let i = 0; i < count; i++) {
  524. const type = reader.uint16();
  525. const size = reader.uint16();
  526. const flags = reader.byte();
  527. reader.skip(3);
  528. reader.align(8);
  529. const next = this._readMessage(reader, type, size, flags);
  530. if ((!next || reader.position >= end) && this.continuations.length > 0) {
  531. const continuation = this.continuations.shift();
  532. reader = reader.at(continuation.offset);
  533. end = continuation.offset + continuation.length;
  534. }
  535. else {
  536. reader.align(8);
  537. }
  538. }
  539. break;
  540. }
  541. case 2: {
  542. const flags = reader.byte();
  543. if ((flags & 0x20) != 0) {
  544. reader.uint32(); // access time
  545. reader.uint32(); // modification time
  546. reader.uint32(); // change time
  547. reader.uint32(); // birth time
  548. }
  549. if ((flags & 0x10) != 0) {
  550. reader.uint16(); // max compact attributes
  551. reader.uint16(); // min compact attributes
  552. }
  553. const order = (flags & 0x04) != 0;
  554. const size = reader.uint(flags & 0x03);
  555. let next = true;
  556. let end = reader.position + size;
  557. while (next && reader.position < end) {
  558. const type = reader.byte();
  559. const size = reader.uint16();
  560. const flags = reader.byte();
  561. if (reader.position < end) {
  562. if (order) {
  563. reader.uint16(); // creation order
  564. }
  565. next = this._readMessage(reader, type, size, flags);
  566. }
  567. if ((!next || reader.position >= end) && this.continuations.length > 0) {
  568. const continuation = this.continuations.shift();
  569. reader = reader.at(continuation.offset);
  570. end = continuation.offset + continuation.length;
  571. if (!reader.match('OCHK')) {
  572. throw new hdf5.Error('Invalid continuation block signature.');
  573. }
  574. next = true;
  575. }
  576. }
  577. break;
  578. }
  579. default: {
  580. throw new hdf5.Error("Unsupported data object header version '" + version + "'.");
  581. }
  582. }
  583. }
  584. _readMessage(reader, type, size, flags) {
  585. switch(type) {
  586. case 0x0000: // NIL
  587. return false;
  588. case 0x0001: // Dataspace
  589. this.dataspace = (size != 4 || flags != 1) ? new hdf5.Dataspace(reader.clone()) : null;
  590. break;
  591. case 0x0002: // Link Info
  592. this.linkInfo = new hdf5.LinkInfo(reader.clone());
  593. break;
  594. case 0x0003: // Datatype
  595. this.datatype = new hdf5.Datatype(reader.clone());
  596. break;
  597. case 0x0004:
  598. case 0x0005: // Fill Value
  599. this.fillValue = new hdf5.FillValue(reader.clone(), type);
  600. break;
  601. case 0x0006: // Link
  602. this.links.push(new hdf5.Link(reader.clone()));
  603. break;
  604. case 0x0008: // Data Layout
  605. this.dataLayout = new hdf5.DataLayout(reader.clone());
  606. break;
  607. case 0x000A: // Group Info
  608. this.groupInfo = new hdf5.GroupInfo(reader.clone());
  609. break;
  610. case 0x000B: // Filter Pipeline
  611. this.filterPipeline = new hdf5.FilterPipeline(reader.clone());
  612. break;
  613. case 0x000C: // Attribute
  614. this.attributes.push(new hdf5.Attribute(reader.clone()));
  615. break;
  616. case 0x000D: // Object Comment Message
  617. this.comment = reader.string(-1, 'ascii');
  618. break;
  619. case 0x0010: // Object Header Continuation
  620. this.continuations.push(new hdf5.ObjectHeaderContinuation(reader.clone()));
  621. break;
  622. case 0x0011: // Symbol Table
  623. this.symbolTable = new hdf5.SymbolTable(reader.clone());
  624. break;
  625. case 0x000E: // Object Modification Time (Old)
  626. case 0x0012: // Object Modification Time
  627. this.objectModificationTime = new hdf5.ObjectModificationTime(reader.clone(), type);
  628. break;
  629. case 0x0015: // Attribute Info
  630. this.attributeInfo = new hdf5.AttributeInfo(reader.clone());
  631. break;
  632. default:
  633. throw new hdf5.Error('Unsupported message type \'' + type + '\'.');
  634. }
  635. reader.skip(size);
  636. return true;
  637. }
  638. };
  639. hdf5.Message = class {
  640. constructor(type, data, flags) {
  641. this._type = type;
  642. this._data = data;
  643. this._flags = flags;
  644. }
  645. };
  646. hdf5.Dataspace = class {
  647. constructor(reader) {
  648. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#DataspaceMessage
  649. this._sizes = [];
  650. const version = reader.byte();
  651. switch (version) {
  652. case 1:
  653. this._dimensions = reader.byte();
  654. this._flags = reader.byte();
  655. reader.skip(1);
  656. reader.skip(4);
  657. for (let i = 0; i < this._dimensions; i++) {
  658. this._sizes.push(reader.length());
  659. }
  660. if ((this._flags & 0x01) != 0) {
  661. this._maxSizes = [];
  662. for (let j = 0; j < this._dimensions; j++) {
  663. this._maxSizes.push(reader.length());
  664. if (this._maxSizes[j] != this._sizes[j]) {
  665. throw new hdf5.Error('Max size is not supported.');
  666. }
  667. }
  668. }
  669. if ((this._flags & 0x02) != 0) {
  670. throw new hdf5.Error('Permutation indices not supported.');
  671. }
  672. break;
  673. case 2:
  674. this._dimensions = reader.byte();
  675. this._flags = reader.byte();
  676. this._type = reader.byte(); // 0 scalar, 1 simple, 2 null
  677. for (let k = 0; k < this._dimensions; k++) {
  678. this._sizes.push(reader.length());
  679. }
  680. if ((this._flags & 0x01) != 0) {
  681. this._maxSizes = [];
  682. for (let l = 0; l < this._dimensions; l++) {
  683. this._maxSizes.push(reader.length());
  684. }
  685. }
  686. break;
  687. default:
  688. throw new hdf5.Error("Unsupported dataspace message version '" + version + "'.");
  689. }
  690. }
  691. get shape() {
  692. return this._sizes;
  693. }
  694. read(datatype, reader) {
  695. if (this._dimensions == 0) {
  696. return datatype.read(reader);
  697. }
  698. return this._readArray(datatype, reader, this._sizes, 0);
  699. }
  700. _readArray(datatype, reader, shape, dimension) {
  701. const array = [];
  702. const size = shape[dimension];
  703. if (dimension == shape.length - 1) {
  704. for (let i = 0; i < size; i++) {
  705. array.push(datatype.read(reader));
  706. }
  707. }
  708. else {
  709. for (let j = 0; j < size; j++) {
  710. array.push(this._readArray(datatype, reader, shape, dimension + 1));
  711. }
  712. }
  713. return array;
  714. }
  715. decode(datatype, data, globalHeap) {
  716. if (this._dimensions == 0) {
  717. return datatype.decode(data, globalHeap);
  718. }
  719. return this._decodeArray(datatype, data, globalHeap, this._sizes, 0);
  720. }
  721. _decodeArray(datatype, data, globalHeap, shape, dimension) {
  722. const size = shape[dimension];
  723. if (dimension == shape.length - 1) {
  724. for (let i = 0; i < size; i++) {
  725. data[i] = datatype.decode(data[i], globalHeap);
  726. }
  727. }
  728. else {
  729. for (let j = 0; j < size; j++) {
  730. data[j] = this._decodeArray(datatype, data[j], shape, dimension + 1);
  731. }
  732. }
  733. return data;
  734. }
  735. };
  736. hdf5.LinkInfo = class {
  737. constructor(reader) {
  738. const version = reader.byte();
  739. switch (version) {
  740. case 0: {
  741. const flags = reader.byte();
  742. if ((flags & 1) != 0) {
  743. this.maxCreationIndex = reader.uint64();
  744. }
  745. this.fractalHeapAddress = reader.offset();
  746. this.nameIndexTreeAddress = reader.offset();
  747. if ((flags & 2) != 0) {
  748. this.creationOrderIndexTreeAddress = reader.offset();
  749. }
  750. break;
  751. }
  752. default:
  753. throw new hdf5.Error("Unsupported link info message version '" + version + "'.");
  754. }
  755. }
  756. };
  757. hdf5.Datatype = class {
  758. constructor(reader) {
  759. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#DatatypeMessage
  760. const format = reader.byte();
  761. const version = format >> 4;
  762. this._class = format & 0xf;
  763. switch (version) {
  764. case 1:
  765. case 2: {
  766. this._flags = reader.byte() | reader.byte() << 8 | reader.byte() << 16;
  767. this._size = reader.uint32();
  768. switch (this._class) {
  769. case 0: { // fixed-Point
  770. this._bitOffset = reader.uint16();
  771. this._bitPrecision = reader.uint16();
  772. break;
  773. }
  774. case 8: { // enumerated
  775. this._base = new hdf5.Datatype(reader);
  776. this._names = [];
  777. this._values = [];
  778. const count = this._flags & 0xffff;
  779. for (let i = 0; i < count; i++) {
  780. const name = reader.clone().string(-1, 'ascii');
  781. this._names.push(name);
  782. reader.skip(Math.round((name.length + 1) / 8) * 8);
  783. }
  784. for (let i = 0; i < count; i++) {
  785. this._values.push(this._base.read(reader));
  786. }
  787. break;
  788. }
  789. default: {
  790. break;
  791. }
  792. }
  793. break;
  794. }
  795. default: {
  796. throw new hdf5.Error('Unsupported datatype version \'' + version + '\'.');
  797. }
  798. }
  799. }
  800. get type() {
  801. switch (this._class) {
  802. case 0: // fixed-point
  803. if ((this._flags & 0xfff6) === 0) {
  804. if ((this._flags && 0x08) !== 0) {
  805. switch (this._size) {
  806. case 1: return 'int8';
  807. case 2: return 'int16';
  808. case 4: return 'int32';
  809. case 8: return 'int64';
  810. default: throw new hdf5.Error("Unsupported int size '" + this._size + "'.");
  811. }
  812. }
  813. else {
  814. switch (this._size) {
  815. case 1: return 'uint8';
  816. case 2: return 'uint16';
  817. case 4: return 'uint32';
  818. case 8: return 'uint64';
  819. default: throw new hdf5.Error("Unsupported uint size '" + this._size + "'.");
  820. }
  821. }
  822. }
  823. break;
  824. case 1: // floating-point
  825. if (this._size == 2 && this._flags == 0x0f20) {
  826. return 'float16';
  827. }
  828. else if (this._size == 4 && this._flags == 0x1f20) {
  829. return 'float32';
  830. }
  831. else if (this._size == 8 && this._flags == 0x3f20) {
  832. return 'float64';
  833. }
  834. break;
  835. case 3: // string
  836. return 'string';
  837. case 5: // opaque
  838. return 'uint8[]';
  839. case 8: // enumerated
  840. if (this._base.type === 'int8' &&
  841. this._names.length === 2 && this._names[0] === 'FALSE' && this._names[1] === 'TRUE' &&
  842. this._values.length === 2 && this._values[0] === 0 && this._values[1] === 1) {
  843. return 'boolean';
  844. }
  845. break;
  846. case 9: // variable-length
  847. if ((this._flags & 0x0f) == 1) { // type
  848. return 'char[]';
  849. }
  850. break;
  851. default:
  852. break;
  853. }
  854. throw new hdf5.Error("Unsupported datatype class '" + this._class + "'.");
  855. }
  856. get littleEndian() {
  857. switch (this._class) {
  858. case 0: // fixed-point
  859. case 1: // floating-point
  860. return (this.flags & 0x01) == 0;
  861. default:
  862. return true;
  863. }
  864. }
  865. read(reader) {
  866. switch (this._class) {
  867. case 0: // fixed-point
  868. if (this._size == 1) {
  869. return ((this._flags & 0x8) != 0) ? reader.int8() : reader.byte();
  870. }
  871. else if (this._size == 2) {
  872. return ((this._flags & 0x8) != 0) ? reader.int16() : reader.uint16();
  873. }
  874. else if (this._size == 4) {
  875. return ((this._flags & 0x8) != 0) ? reader.int32() : reader.uint32();
  876. }
  877. else if (this._size == 8) {
  878. return ((this._flags & 0x8) != 0) ? reader.int64() : reader.uint64();
  879. }
  880. throw new hdf5.Error('Unsupported fixed-point datatype.');
  881. case 1: // floating-point
  882. if (this._size == 2 && this._flags == 0x0f20) {
  883. return reader.float16();
  884. }
  885. else if (this._size == 4 && this._flags == 0x1f20) {
  886. return reader.float32();
  887. }
  888. else if (this._size == 8 && this._flags == 0x3f20) {
  889. return reader.float64();
  890. }
  891. throw new hdf5.Error('Unsupported floating-point datatype.');
  892. case 3: // string
  893. switch ((this._flags >> 8) & 0x0f) { // character set
  894. case 0:
  895. return hdf5.BinaryReader.decode(reader.read(this._size), 'ascii');
  896. case 1:
  897. return hdf5.BinaryReader.decode(reader.read(this._size), 'utf-8');
  898. default:
  899. throw new hdf5.Error('Unsupported character encoding.');
  900. }
  901. case 5: // opaque
  902. return reader.read(this._size);
  903. case 8: // enumerated
  904. return reader.read(this._size);
  905. case 9: // variable-length
  906. return {
  907. length: reader.uint32(),
  908. globalHeapID: new hdf5.GlobalHeapID(reader)
  909. };
  910. default:
  911. throw new hdf5.Error('Unsupported datatype class \'' + this._class + '\'.');
  912. }
  913. }
  914. decode(data, globalHeap) {
  915. switch (this._class) {
  916. case 0: // fixed-point
  917. return data;
  918. case 1: // floating-point
  919. return data;
  920. case 3: // string
  921. return data;
  922. case 5: // opaque
  923. return data;
  924. case 8: // enumerated
  925. return data;
  926. case 9: { // variable-length
  927. const globalHeapObject = globalHeap.get(data.globalHeapID);
  928. if (globalHeapObject != null) {
  929. const characterSet = (this._flags >> 8) & 0x0f;
  930. switch (characterSet) {
  931. case 0:
  932. return hdf5.BinaryReader.decode(globalHeapObject.data, 'ascii');
  933. case 1:
  934. return hdf5.BinaryReader.decode(globalHeapObject.data, 'utf-8');
  935. default:
  936. throw new hdf5.Error('Unsupported character encoding.');
  937. }
  938. }
  939. break;
  940. }
  941. default:
  942. throw new hdf5.Error('Unsupported datatype class \'' + this._class + '\'.');
  943. }
  944. return null;
  945. }
  946. };
  947. hdf5.FillValue = class {
  948. constructor(reader, type) {
  949. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#FillValueMessage
  950. switch (type) {
  951. case 0x0004: {
  952. const size = reader.uint32();
  953. this.data = reader.read(size);
  954. break;
  955. }
  956. case 0x0005:
  957. default: {
  958. const version = reader.byte();
  959. switch (version) {
  960. case 1:
  961. case 2: {
  962. reader.byte();
  963. reader.byte();
  964. const valueDefined = reader.byte();
  965. if (version === 1 || valueDefined === 1) {
  966. const size = reader.uint32();
  967. this.data = reader.read(size);
  968. }
  969. break;
  970. }
  971. case 3: {
  972. const flags = reader.byte();
  973. if ((flags & 0x20) !== 0) {
  974. const size = reader.uint32();
  975. this.data = reader.read(size);
  976. }
  977. break;
  978. }
  979. default:
  980. throw new hdf5.Error('Unsupported fill value version \'' + version + '\'.');
  981. }
  982. break;
  983. }
  984. }
  985. }
  986. };
  987. hdf5.Link = class {
  988. constructor(reader) {
  989. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#FillValueMessage
  990. const version = reader.byte();
  991. switch (version) {
  992. case 1: {
  993. const flags = reader.byte();
  994. this.type = (flags & 0x08) != 0 ? reader.byte() : 0;
  995. if ((flags & 0x04) != 0) {
  996. this.creationOrder = reader.uint32();
  997. }
  998. const encoding = ((flags & 0x10) != 0 && reader.byte() == 1) ? 'utf-8' : 'ascii';
  999. this.name = reader.string(reader.uint(flags & 0x03), encoding);
  1000. switch (this.type) {
  1001. case 0: // hard link
  1002. this.objectHeaderAddress = reader.offset();
  1003. break;
  1004. case 1: // soft link
  1005. break;
  1006. default:
  1007. throw new hdf5.Error('Unsupported link message type \'' + this.type + '\'.');
  1008. }
  1009. break;
  1010. }
  1011. default:
  1012. throw new hdf5.Error('Unsupported link message version \'' + version + '\'.');
  1013. }
  1014. }
  1015. };
  1016. hdf5.DataLayout = class {
  1017. constructor(reader) {
  1018. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#LayoutMessage
  1019. const version = reader.byte();
  1020. switch (version) {
  1021. case 1:
  1022. case 2: {
  1023. this.dimensionality = reader.byte();
  1024. this.layoutClass = reader.byte();
  1025. reader.skip(5);
  1026. switch (this.layoutClass) {
  1027. case 1:
  1028. this.address = reader.offset();
  1029. this.dimensionSizes = [];
  1030. for (let i = 0; i < this.dimensionality - 1; i++) {
  1031. this.dimensionSizes.push(reader.int32());
  1032. }
  1033. break;
  1034. case 2: // Chunked
  1035. this.address = reader.offset();
  1036. this.dimensionSizes = [];
  1037. for (let i = 0; i < this.dimensionality - 1; i++) {
  1038. this.dimensionSizes.push(reader.int32());
  1039. }
  1040. this.datasetElementSize = reader.int32();
  1041. break;
  1042. default:
  1043. throw new hdf5.Error('Unsupported data layout class \'' + this.layoutClass + '\'.');
  1044. }
  1045. break;
  1046. }
  1047. case 3: {
  1048. this.layoutClass = reader.byte();
  1049. switch (this.layoutClass) {
  1050. case 0: // Compact
  1051. this.size = reader.uint16();
  1052. reader.skip(2);
  1053. this.address = reader.position;
  1054. break;
  1055. case 1: // Contiguous
  1056. this.address = reader.offset();
  1057. this.size = reader.length();
  1058. break;
  1059. case 2: // Chunked
  1060. this.dimensionality = reader.byte();
  1061. this.address = reader.offset();
  1062. this.dimensionSizes = [];
  1063. for (let i = 0; i < this.dimensionality - 1; i++) {
  1064. this.dimensionSizes.push(reader.int32());
  1065. }
  1066. this.datasetElementSize = reader.int32();
  1067. break;
  1068. default:
  1069. throw new hdf5.Error('Unsupported data layout class \'' + this.layoutClass + '\'.');
  1070. }
  1071. break;
  1072. }
  1073. default: {
  1074. throw new hdf5.Error('Unsupported data layout version \'' + version + '\'.');
  1075. }
  1076. }
  1077. }
  1078. };
  1079. hdf5.GroupInfo = class {
  1080. constructor(reader) {
  1081. const version = reader.byte();
  1082. switch (version) {
  1083. case 0: {
  1084. const flags = reader.byte();
  1085. if ((flags & 0x01) != 0) {
  1086. this.maxCompactLinks = reader.uint16();
  1087. this.minDenseLinks = reader.uint16();
  1088. }
  1089. if ((flags & 0x02) != 0) {
  1090. this.estimatedEntriesNumber = reader.uint16();
  1091. this.estimatedLinkNameLengthEntires = reader.uint16();
  1092. }
  1093. break;
  1094. }
  1095. default:
  1096. throw new hdf5.Error('Unsupported group info version \'' + version + '\'.');
  1097. }
  1098. }
  1099. };
  1100. hdf5.FilterPipeline = class {
  1101. constructor(reader) {
  1102. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#FilterMessage
  1103. const version = reader.byte();
  1104. switch (version) {
  1105. case 1: {
  1106. this.filters = [];
  1107. const numberOfFilters = reader.byte();
  1108. reader.skip(2);
  1109. reader.skip(4);
  1110. for (let i = 0; i < numberOfFilters; i++) {
  1111. this.filters.push(new hdf5.Filter(reader));
  1112. reader.align(8);
  1113. }
  1114. break;
  1115. }
  1116. default:
  1117. throw new hdf5.Error('Unsupported filter pipeline message version \'' + version + '\'.');
  1118. }
  1119. }
  1120. };
  1121. hdf5.Filter = class {
  1122. constructor(reader) {
  1123. this.id = reader.int16();
  1124. const nameLength = reader.int16();
  1125. this.flags = reader.int16();
  1126. const clientDataSize = reader.int16();
  1127. this.name = reader.string(nameLength, 'ascii');
  1128. this.clientData = reader.read(clientDataSize * 4);
  1129. }
  1130. decode(data) {
  1131. switch (this.id) {
  1132. case 1: { // gzip
  1133. const archive = zip.Archive.open(data);
  1134. return archive.entries.get('').peek();
  1135. }
  1136. default: {
  1137. throw new hdf5.Error("Unsupported filter '" + this.name + "'.");
  1138. }
  1139. }
  1140. }
  1141. };
  1142. hdf5.Attribute = class {
  1143. constructor(reader) {
  1144. const version = reader.byte();
  1145. switch (version) {
  1146. case 1: {
  1147. reader.skip(1);
  1148. const nameSize = reader.uint16();
  1149. const datatypeSize = reader.uint16();
  1150. const dataspaceSize = reader.uint16();
  1151. this.name = reader.string(nameSize, 'utf-8');
  1152. reader.align(8);
  1153. this._datatype = new hdf5.Datatype(reader.clone());
  1154. reader.skip(datatypeSize);
  1155. reader.align(8);
  1156. this._dataspace = new hdf5.Dataspace(reader.clone());
  1157. reader.skip(dataspaceSize);
  1158. reader.align(8);
  1159. this._data = this._dataspace.read(this._datatype, reader);
  1160. break;
  1161. }
  1162. case 3: {
  1163. reader.byte();
  1164. const nameSize = reader.uint16();
  1165. const datatypeSize = reader.uint16();
  1166. const dataspaceSize = reader.uint16();
  1167. const encoding = reader.byte() == 1 ? 'utf-8' : 'ascii';
  1168. this.name = reader.string(nameSize, encoding);
  1169. this._datatype = new hdf5.Datatype(reader.clone());
  1170. reader.skip(datatypeSize);
  1171. this._dataspace = new hdf5.Dataspace(reader.clone());
  1172. reader.skip(dataspaceSize);
  1173. this._data = this._dataspace.read(this._datatype, reader);
  1174. break;
  1175. }
  1176. default:
  1177. throw new hdf5.Error('Unsupported attribute message version \'' + version + '\'.');
  1178. }
  1179. }
  1180. decodeValue(globalHeap) {
  1181. if (this._data) {
  1182. return this._dataspace.decode(this._datatype, this._data, globalHeap);
  1183. }
  1184. return null;
  1185. }
  1186. };
  1187. hdf5.ObjectHeaderContinuation = class {
  1188. constructor(reader) {
  1189. this.offset = reader.offset();
  1190. this.length = reader.length();
  1191. }
  1192. };
  1193. hdf5.SymbolTable = class {
  1194. constructor(reader) {
  1195. this.treeAddress = reader.offset(); // hdf5.Tree pointer
  1196. this.heapAddress = reader.offset(); // hdf5.Heap pointer
  1197. }
  1198. };
  1199. hdf5.ObjectModificationTime = class {
  1200. constructor(reader, type) {
  1201. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#ModificationTimeMessage
  1202. switch (type) {
  1203. case 0x000E: {
  1204. this.year = reader.uint32();
  1205. this.month = reader.uint16();
  1206. this.day = reader.uint16();
  1207. this.hour = reader.uint16();
  1208. this.minute = reader.uint16();
  1209. this.second = reader.uint16();
  1210. reader.skip(2);
  1211. break;
  1212. }
  1213. case 0x0012: {
  1214. const version = reader.byte();
  1215. reader.skip(3);
  1216. switch (version) {
  1217. case 1:
  1218. this.timestamp = reader.uint32();
  1219. break;
  1220. default:
  1221. throw new hdf5.Error('Unsupported object modification time message version \'' + version + '\'.');
  1222. }
  1223. break;
  1224. }
  1225. default: {
  1226. throw new hdf5.Error('Unsupported object modification time message type \'' + type + '\'.');
  1227. }
  1228. }
  1229. }
  1230. };
  1231. hdf5.AttributeInfo = class {
  1232. constructor(reader) {
  1233. const version = reader.byte();
  1234. switch (version) {
  1235. case 0: {
  1236. const flags = reader.byte();
  1237. if ((flags & 1) != 0) {
  1238. this.maxCreationIndex = reader.uint64();
  1239. }
  1240. this.fractalHeapAddress = reader.offset();
  1241. this.attributeNameTreeAddress = reader.offset();
  1242. if ((flags & 2) != 0) {
  1243. this.attributeCreationOrderTreeAddress = reader.offset();
  1244. }
  1245. break;
  1246. }
  1247. default:
  1248. throw new hdf5.Error('Unsupported attribute info message version \'' + version + '\'.');
  1249. }
  1250. }
  1251. };
  1252. hdf5.Tree = class {
  1253. constructor(reader, dimensionality) {
  1254. // https://support.hdfgroup.org/HDF5/doc/H5.format.html#V1Btrees
  1255. if (!reader.match('TREE')) {
  1256. throw new hdf5.Error("Not a valid 'TREE' block.");
  1257. }
  1258. this.type = reader.byte();
  1259. this.level = reader.byte();
  1260. const entries = reader.uint16();
  1261. reader.offset(); // address of left sibling
  1262. reader.offset(); // address of right sibling
  1263. this.nodes = [];
  1264. switch (this.type) {
  1265. case 0: { // Group nodes
  1266. for (let i = 0; i < entries; i++) {
  1267. reader.length();
  1268. const childPointer = reader.offset();
  1269. if (this.level == 0) {
  1270. const node = new hdf5.SymbolTableNode(reader.at(childPointer));
  1271. this.nodes.push(node);
  1272. }
  1273. else {
  1274. const tree = new hdf5.Tree(reader.at(childPointer));
  1275. this.nodes.push(...tree.nodes);
  1276. }
  1277. }
  1278. break;
  1279. }
  1280. case 1: { // Raw data chunk nodes
  1281. for (let i = 0; i < entries; i++) {
  1282. const size = reader.int32();
  1283. const filterMask = reader.int32();
  1284. const fields = [];
  1285. for (let j = 0; j < dimensionality; j++) {
  1286. fields.push(reader.uint64());
  1287. }
  1288. const childPointer = reader.offset();
  1289. if (this.level == 0) {
  1290. const data = reader.at(childPointer).read(size);
  1291. this.nodes.push({ data: data, fields: fields, filterMask: filterMask });
  1292. }
  1293. else {
  1294. const tree = new hdf5.Tree(reader.at(childPointer), dimensionality);
  1295. this.nodes.push(...tree.nodes);
  1296. }
  1297. }
  1298. break;
  1299. }
  1300. default: {
  1301. throw new hdf5.Error('Unsupported B-Tree node type \'' + this.type + '\'.');
  1302. }
  1303. }
  1304. }
  1305. };
  1306. hdf5.Heap = class {
  1307. constructor(reader) {
  1308. this._reader = reader;
  1309. if (!reader.match('HEAP')) {
  1310. throw new hdf5.Error("Not a valid 'HEAP' block.");
  1311. }
  1312. const version = reader.byte();
  1313. switch (version) {
  1314. case 0: {
  1315. reader.skip(3);
  1316. this._dataSize = reader.length();
  1317. this._offsetToHeadOfFreeList = reader.length();
  1318. this._dataAddress = reader.offset();
  1319. break;
  1320. }
  1321. default: {
  1322. throw new hdf5.Error('Unsupported Local Heap version \'' + version + '\'.');
  1323. }
  1324. }
  1325. }
  1326. getString(offset) {
  1327. const reader = this._reader.at(this._dataAddress + offset);
  1328. return reader.string(-1, 'utf-8');
  1329. }
  1330. };
  1331. hdf5.GlobalHeap = class {
  1332. constructor(reader) {
  1333. this._reader = reader;
  1334. this._collections = new Map();
  1335. }
  1336. get(globalHeapID) {
  1337. const address = globalHeapID.address;
  1338. if (!this._collections.has(address)) {
  1339. this._collections.set(address, new hdf5.GlobalHeapCollection(this._reader.at(address)));
  1340. }
  1341. return this._collections.get(globalHeapID.address).getObject(globalHeapID.objectIndex);
  1342. }
  1343. };
  1344. hdf5.GlobalHeapCollection = class {
  1345. constructor(reader) {
  1346. const startPosition = reader.position;
  1347. if (!reader.match('GCOL')) {
  1348. throw new hdf5.Error("Not a valid 'GCOL' block.");
  1349. }
  1350. const version = reader.byte();
  1351. switch (version) {
  1352. case 1: {
  1353. reader.skip(3);
  1354. this._objects = new Map();
  1355. const size = reader.length();
  1356. const endPosition = startPosition + size;
  1357. while (reader.position < endPosition) {
  1358. const index = reader.uint16();
  1359. if (index == 0) {
  1360. break;
  1361. }
  1362. this._objects.set(index, new hdf5.GlobalHeapObject(reader));
  1363. reader.align(8);
  1364. }
  1365. break;
  1366. }
  1367. default: {
  1368. throw new hdf5.Error('Unsupported global heap collection version \'' + version + '\'.');
  1369. }
  1370. }
  1371. }
  1372. getObject(objectIndex) {
  1373. if (this._objects.has(objectIndex)) {
  1374. return this._objects.get(objectIndex);
  1375. }
  1376. return null;
  1377. }
  1378. };
  1379. hdf5.GlobalHeapObject = class {
  1380. constructor(reader) {
  1381. reader.uint16();
  1382. reader.skip(4);
  1383. const length = reader.length();
  1384. this.data = reader.read(length);
  1385. }
  1386. };
  1387. hdf5.GlobalHeapID = class {
  1388. constructor(reader) {
  1389. this.address = reader.offset();
  1390. this.objectIndex = reader.uint32();
  1391. }
  1392. };
  1393. hdf5.Error = class extends Error {
  1394. constructor(message) {
  1395. super(message);
  1396. this.name = 'HDF5 Error';
  1397. }
  1398. };
  1399. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  1400. module.exports.File = hdf5.File;
  1401. }