linuxfront-v2-logistics/js/base64.js

Overview

This module provides Base64 encoding and decoding for JavaScript strings, with full UTF-8 support. It defines a lightweight string builder, the Base64 object, and internal enumerators to handle multi-byte character conversion efficiently .

📜 License

Released under a permissive license by Fred Palmer (2008).

  • Permission to use, copy, modify, and distribute is granted, provided the copyright notice
    and this permission notice appear in all copies.
  • Provided “AS IS” without warranty of any kind .

Key Components

  • StringBuffer: Efficient string concatenation.
  • Base64: Main object with encode/decode methods and lookup codex.
  • Utf8EncodeEnumerator: Converts JS charCode to UTF-8 byte sequence.
  • Base64DecodeEnumerator: Parses Base64 text back into byte codes.

StringBuffer

A utility class to build strings without repeated concatenation overhead.

function StringBuffer() {
  this.buffer = [];
}

StringBuffer.prototype.append = function(string) {
  this.buffer.push(string);
  return this;
};

StringBuffer.prototype.toString = function() {
  return this.buffer.join("");
};
  • .append(string) pushes to the internal array.
  • .toString() joins and returns the accumulated string .

Base64 Object

Provides public methods and the encoding alphabet.

Property / Method Description
codex Base64 alphabet with padding character (=).
encode(input) Converts a UTF-8 string into a Base64-encoded string.
decode(input) Converts a Base64 string back into the original UTF-8 string.

Base64.encode(input)

  1. Initializes a StringBuffer and a Utf8EncodeEnumerator.
  2. Reads three bytes at a time, computes four 6-bit groups via bitwise ops.
  3. Maps each group to a character in codex, using = for padding.
  4. Returns the concatenated Base64 string .

Base64.decode(input)

  1. Sets up a Base64DecodeEnumerator over the input.
  2. Iterates, reconstructing original byte codes:
    • 1-byte (ASCII)
    • 2-byte UTF-8
    • 3-byte UTF-8
  3. Uses String.fromCharCode to form the decoded string.
  4. Returns the result .

Utf8EncodeEnumerator

An iterator that emits UTF-8 byte codes for a given JS string.

function Utf8EncodeEnumerator(input) {
  this._input = input;
  this._index = -1;
  this._buffer = [];
  this.current = NaN;
}

Utf8EncodeEnumerator.prototype.moveNext = function() {
  if (this._buffer.length > 0) {
    this.current = this._buffer.shift();
    return true;
  }
  if (this._index >= this._input.length - 1) {
    this.current = NaN;
    return false;
  }
  var charCode = this._input.charCodeAt(++this._index);
  // Normalize CRLF to LF
  if (charCode === 13 && this._input.charCodeAt(this._index + 1) === 10) {
    charCode = 10;
    this._index++;
  }
  if (charCode < 0x80) {
    this.current = charCode;
  } else if (charCode < 0x800) {
    this.current = (charCode >> 6) | 0xC0;
    this._buffer.push((charCode & 0x3F) | 0x80);
  } else {
    this.current = (charCode >> 12) | 0xE0;
    this._buffer.push(((charCode >> 6) & 0x3F) | 0x80);
    this._buffer.push((charCode & 0x3F) | 0x80);
  }
  return true;
};
  • Handles line endings and multi-byte UTF-8 sequences .

Base64DecodeEnumerator

Iterates over a Base64 string, emitting original byte codes.

function Base64DecodeEnumerator(input) {
  this._input = input;
  this._index = -1;
  this._buffer = [];
  this.current = 64;
}

Base64DecodeEnumerator.prototype.moveNext = function() {
  if (this._buffer.length > 0) {
    this.current = this._buffer.shift();
    return true;
  }
  if (this._index >= this._input.length - 1) {
    this.current = 64;
    return false;
  }
  var enc1 = Base64.codex.indexOf(this._input.charAt(++this._index));
  var enc2 = Base64.codex.indexOf(this._input.charAt(++this._index));
  var enc3 = Base64.codex.indexOf(this._input.charAt(++this._index));
  var enc4 = Base64.codex.indexOf(this._input.charAt(++this._index));
  var chr1 = (enc1 << 2) | (enc2 >> 4);
  var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  var chr3 = ((enc3 & 3) << 6) | enc4;
  this.current = chr1;
  if (enc3 !== 64) this._buffer.push(chr2);
  if (enc4 !== 64) this._buffer.push(chr3);
  return true;
};
  • Converts each 4-character chunk back into up to three bytes .

Usage Example 📝

// Encode a string
const original = "Hello, 世界";
const b64 = Base64.encode(original);
console.log(b64);  // "SGVsbG8sIOS4lueVjA=="

// Decode back
const decoded = Base64.decode(b64);
console.log(decoded); // "Hello, 世界"

Integration 🔗

  • Included in the label-printing page to enable client-side PDF embedding via data URIs .
  • This same utility is duplicated in:
    • new_2025_js/base64.js for the updated UI
    • linuxfront-v2/js/base64.js across legacy modules

Method Summary

Method Description
Base64.encode(input) UTF-8 encodes and then Base64-encodes the input string.
Base64.decode(input) Base64-decodes to bytes and reconstructs the UTF-8 string.
StringBuffer Micro-optimized string builder to reduce concatenation cost.
Utf8EncodeEnumerator Iterator that outputs UTF-8 byte codes from JS charCode.
Base64DecodeEnumerator Iterator that outputs byte codes from a Base64 string.

This module offers a self-contained, dependency-free solution for Base64 operations in browsers, ensuring consistent handling of multi-byte characters in a variety of client-side features.