Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

608 B
Raw Blame
import { logError } from 'backend/logger'

/**
 * Decodes a buffer to a UTF-8 string.
 * Any leading continuation bytes are skipped to get a clean string start.
 *
 * @param buffer The buffer to decode
 */
export function decodeUTF8(buffer: Buffer): string {
  try {
    let skip = 0
    // If bit 7 is set and bit 6 is cleared, this is a continuation byte.
    while (skip < 4 && skip < buffer.length && (buffer[skip] & 0xc0) === 0x80) {
      skip++
    }
    return buffer.subarray(skip).toString('utf-8')
  } catch (error) {
    logError(`Error decoding buffer as UTF-8: ${error}`)
    throw error
  }
}