function voucher_used_count($roll)
{
    global $g, $cpzone;
    $bitstring = voucher_read_used_db($roll);
    $max = strlen($bitstring) * 8;
    $used = 0;
    for ($i = 1; $i <= $max; $i++) {
        // check if ticket already used or not.
        $pos = $i >> 3;
        // divide by 8 -> octet
        $mask = 1 << $i % 8;
        // mask to test bit in octet
        if (ord($bitstring[$pos]) & $mask) {
            $used++;
        }
    }
    unset($bitstring);
    return $used;
}
 $voucherlck = lock("voucher{$cpzone}");
 if ($_POST['count'] != $rollent['count']) {
     $rollent['count'] = $_POST['count'];
     $len = ($rollent['count'] >> 3) + 1;
     // count / 8 +1
     $rollent['used'] = base64_encode(str_repeat("", $len));
     // 4 bitmask
     $rollent['active'] = array();
     voucher_write_used_db($rollent['number'], $rollent['used']);
     voucher_write_active_db($rollent['number'], array());
     // create empty DB
     voucher_log(LOG_INFO, sprintf(gettext('All %1$s vouchers from Roll %2$s marked unused'), $rollent['count'], $rollent['number']));
 } else {
     // existing roll has been modified but without changing the count
     // read active and used DB from ramdisk and store it in XML config
     $rollent['used'] = base64_encode(voucher_read_used_db($rollent['number']));
     $activent = array();
     $db = array();
     $active_vouchers = voucher_read_active_db($rollent['number'], $rollent['minutes']);
     foreach ($active_vouchers as $voucher => $line) {
         list($timestamp, $minutes) = explode(",", $line);
         $activent['voucher'] = $voucher;
         $activent['timestamp'] = $timestamp;
         $activent['minutes'] = $minutes;
         $db[] = $activent;
     }
     $rollent['active'] = $db;
 }
 unlock($voucherlck);
 if (isset($id) && $a_roll[$id]) {
     $a_roll[$id] = $rollent;