PHP 8.2.30
Preview: Pointer.js Size: 4.09 KB
/home/byroehnu/.trash/node_modules11/restructure/src/Pointer.js

import * as utils from './utils.js';
import {Base} from './Base.js';

export class Pointer extends Base {
  constructor(offsetType, type, options = {}) {
    super();
    this.offsetType = offsetType;
    this.type = type;
    this.options = options;
    if (this.type === 'void') { this.type = null; }
    if (this.options.type == null) { this.options.type = 'local'; }
    if (this.options.allowNull == null) { this.options.allowNull = true; }
    if (this.options.nullValue == null) { this.options.nullValue = 0; }
    if (this.options.lazy == null) { this.options.lazy = false; }
    if (this.options.relativeTo) {
      if (typeof this.options.relativeTo !== 'function') {
        throw new Error('relativeTo option must be a function');
      }
      this.relativeToGetter = options.relativeTo;
    }
  }

  decode(stream, ctx) {
    const offset = this.offsetType.decode(stream, ctx);

    // handle NULL pointers
    if ((offset === this.options.nullValue) && this.options.allowNull) {
      return null;
    }

    let relative;
    switch (this.options.type) {
      case 'local':     relative = ctx._startOffset; break;
      case 'immediate': relative = stream.pos - this.offsetType.size(); break;
      case 'parent':    relative = ctx.parent._startOffset; break;
      default:
        var c = ctx;
        while (c.parent) {
          c = c.parent;
        }

        relative = c._startOffset || 0;
    }

    if (this.options.relativeTo) {
      relative += this.relativeToGetter(ctx);
    }

    const ptr = offset + relative;

    if (this.type != null) {
      let val = null;
      const decodeValue = () => {
        if (val != null) { return val; }

        const { pos } = stream;
        stream.pos = ptr;
        val = this.type.decode(stream, ctx);
        stream.pos = pos;
        return val;
      };

      // If this is a lazy pointer, define a getter to decode only when needed.
      // This obviously only works when the pointer is contained by a Struct.
      if (this.options.lazy) {
        return new utils.PropertyDescriptor({
          get: decodeValue});
      }

      return decodeValue();
    } else {
      return ptr;
    }
  }

  size(val, ctx) {
    const parent = ctx;
    switch (this.options.type) {
      case 'local': case 'immediate':
        break;
      case 'parent':
        ctx = ctx.parent;
        break;
      default: // global
        while (ctx.parent) {
          ctx = ctx.parent;
        }
    }

    let { type } = this;
    if (type == null) {
      if (!(val instanceof VoidPointer)) {
        throw new Error("Must be a VoidPointer");
      }

      ({ type } = val);
      val = val.value;
    }

    if (val && ctx) {
      // Must be written as two separate lines rather than += in case `type.size` mutates ctx.pointerSize.
      let size = type.size(val, parent);
      ctx.pointerSize += size;
    }

    return this.offsetType.size();
  }

  encode(stream, val, ctx) {
    let relative;
    const parent = ctx;
    if ((val == null)) {
      this.offsetType.encode(stream, this.options.nullValue);
      return;
    }

    switch (this.options.type) {
      case 'local':
        relative = ctx.startOffset;
        break;
      case 'immediate':
        relative = stream.pos + this.offsetType.size(val, parent);
        break;
      case 'parent':
        ctx = ctx.parent;
        relative = ctx.startOffset;
        break;
      default: // global
        relative = 0;
        while (ctx.parent) {
          ctx = ctx.parent;
        }
    }

    if (this.options.relativeTo) {
      relative += this.relativeToGetter(parent.val);
    }

    this.offsetType.encode(stream, ctx.pointerOffset - relative);

    let { type } = this;
    if (type == null) {
      if (!(val instanceof VoidPointer)) {
        throw new Error("Must be a VoidPointer");
      }

      ({ type } = val);
      val = val.value;
    }

    ctx.pointers.push({
      type,
      val,
      parent
    });

    return ctx.pointerOffset += type.size(val, parent);
  }
}

// A pointer whose type is determined at decode time
export class VoidPointer {
  constructor(type, value) {
    this.type = type;
    this.value = value;
  }
}

Directory Contents

Dirs: 0 × Files: 17

Name Size Perms Modified Actions
2.43 KB lrw-r--r-- 2026-02-14 19:19:14
Edit Download
412 B lrw-r--r-- 2026-02-14 19:19:14
Edit Download
741 B lrw-r--r-- 2026-02-14 19:19:14
Edit Download
398 B lrw-r--r-- 2026-02-14 19:19:16
Edit Download
795 B lrw-r--r-- 2026-02-14 19:19:16
Edit Download
1.85 KB lrw-r--r-- 2026-02-14 19:19:16
Edit Download
3.13 KB lrw-r--r-- 2026-02-14 19:19:16
Edit Download
533 B lrw-r--r-- 2026-02-14 19:19:16
Edit Download
1.62 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
2.23 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
920 B lrw-r--r-- 2026-02-14 19:19:18
Edit Download
4.09 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
519 B lrw-r--r-- 2026-02-14 19:19:18
Edit Download
3.71 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
2.33 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
723 B lrw-r--r-- 2026-02-14 19:19:18
Edit Download
3.01 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).