You are here

function apdqc_lock_base85_encode in Asynchronous Prefetch Database Query Cache 7

Convert a binary string to base85 ascii.

Parameters

string $binary: Binary string.

Return value

string Ascii base85 transformation of the binary string.

Related topics

1 call to apdqc_lock_base85_encode()
_apdqc_lock_id in ./apdqc.lock.db.inc
Helper function to get this request's unique id.
2 string references to 'apdqc_lock_base85_encode'
apdqc_admin_semaphore_table_need_update in ./apdqc.admin.inc
See if the semaphore table needs to be updated.
apdqc_schema_alter in ./apdqc.module
Implements hook_schema_alter().

File

./apdqc.lock.db.inc, line 455
A database-mediated implementation of a locking mechanism.

Code

function apdqc_lock_base85_encode($binary) {

  // Calculate padding.
  $padding = 4 - strlen($binary) % 4;
  if (strlen($binary) % 4 === 0) {
    $padding = 0;
  }

  // Append NULL character to make sure this is a 4 byte chunk.
  $binary .= str_repeat("\0", $padding);

  // Generate output.
  $output = '';
  foreach (unpack('N*', $binary) as $chunk) {

    // Fix bug with unpack returning negative numbers (32bit os).
    if ($chunk < 0) {

      // Chunk is now a float on 32bit systems.
      $chunk += 4294967296;
    }

    // Convert the 32bit integer into 5 "quintet" chunks.
    $int_ascii = intval($chunk / 52200625);
    $chunk -= $int_ascii * 52200625;
    $output .= chr($int_ascii + 33);
    $int_ascii = intval($chunk / 614125);
    $chunk -= $int_ascii * 614125;
    $output .= chr($int_ascii + 33);
    $int_ascii = intval($chunk / 7225);
    $chunk -= $int_ascii * 7225;
    $output .= chr($int_ascii + 33);
    $int_ascii = intval($chunk / 85);
    $chunk -= $int_ascii * 85;
    $output .= chr($int_ascii + 33);
    $output .= chr($chunk + 33);
  }

  // If we added some null bytes, remove them from the final string.
  if ($padding) {
    $output = preg_replace("/z\$/", '!!!!!', $output);
    $output = substr($output, 0, strlen($output) - $padding);
  }
  return $output;
}