hdf5.js 56 KB

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