if ($i == $corpseitemcount - 1) {
             $itemend = $mercitemstart - 1;
         } else {
             $itemend = strpos($RawCharData, 'JM', $itemstart);
             // this will lose the last item
         }
         if ($itemend === false || $itemend >= $mercitemstart) {
             break;
         } else {
             $itemstart = item_decode($itemstart, $itemend, $RawCharData, $CharData, $CorpseItemList, $mercitemend, $Level);
         }
     }
 }
 // Merc Item List
 if ($mercitemstart + 2 != $mercitemend) {
     $mercitemcount = hexdec(padhex($CharData[$mercitemstart + 5]) . padhex($CharData[$mercitemstart + 4]));
     $MercItemList = array();
     $itemstart = $mercitemstart + 8;
     for ($i = 0; $i < $mercitemcount; $i++) {
         $itemend = strpos($RawCharData, 'JM', $itemstart);
         if ($itemend === false || $itemend >= $mercitemend) {
             $itemend = $mercitemend;
         }
         $itemstart = item_decode($itemstart, $itemend, $RawCharData, $CharData, $MercItemList, $mercitemend, $MercLevel);
     }
 } else {
     $mercitemcount = 0;
 }
 // Iron Golem - Expansion only
 if ($Expansion) {
     $hasirongolem = $CharData[$mercitemend + 2];
function item_decode($startpos, $endpos, $rawdata, $data, &$items, $mercend, $level)
{
    $item = array();
    $binitem = "";
    for ($i = $endpos - 1; $i >= $startpos; $i--) {
        if ($i < 0) {
            break;
        }
        $binitem .= sprintf("%08b", hexdec(padhex($data[$i])));
    }
    //  $item['binary'] = $binitem;
    $itemlength = strlen($binitem);
    $item['isquest'] = substr($binitem, $itemlength - 1, 1);
    $item['isidentified'] = substr($binitem, $itemlength - 5, 1);
    $item['isdisabled'] = substr($binitem, $itemlength - 9, 1);
    $item['issocketed'] = substr($binitem, $itemlength - 12, 1);
    $item['foundthisrun'] = substr($binitem, $itemlength - 14, 1);
    $item['illegal'] = substr($binitem, $itemlength - 15, 1);
    $item['isear'] = substr($binitem, $itemlength - 17, 1);
    $item['isnewbie'] = substr($binitem, $itemlength - 18, 1);
    $item['issimple'] = substr($binitem, $itemlength - 22, 1);
    $item['isethereal'] = substr($binitem, $itemlength - 23, 1);
    $item['ispersonalized'] = substr($binitem, $itemlength - 25, 1);
    $item['noreadgems'] = substr($binitem, $itemlength - 26, 1);
    $item['hasruneword'] = substr($binitem, $itemlength - 27, 1);
    $item['version'] = bindec(substr($binitem, $itemlength - 42, 10));
    $item['location'] = bindec(substr($binitem, $itemlength - 45, 3));
    $item['position'] = bindec(substr($binitem, $itemlength - 49, 4));
    $item['column'] = bindec(substr($binitem, $itemlength - 53, 4));
    $item['row'] = bindec(substr($binitem, $itemlength - 57, 3));
    $item['storedin'] = bindec(substr($binitem, $itemlength - 60, 3));
    $item['stacked'] = 0;
    $item['magiccount'] = 0;
    $item['itemsinsockets'] = 0;
    $item['magiccolor'] = '4850b8';
    $socketstart = $endpos + 2;
    if ($item['isear']) {
        global $classes;
        global $Hardcore;
        $item['type'] = "ear";
        $item['magiccolor'] = 'ffffff';
        $item['earclass'] = $classes[bindec(substr($binitem, $itemlength - 63, 3))];
        $item['earlevel'] = bindec(substr($binitem, $itemlength - 70, 7));
        $earname = $letter = "";
        $letters = 0;
        $offset = $itemlength - 77;
        while ($letters < 18 && $letter != chr(0)) {
            $letter = chr(bindec(substr($binitem, $offset, 7)));
            $earname = $earname . $letter;
            $letters++;
            $offset -= 7;
        }
        $item['earname'] = substr($earname, 0, -1);
        $item['issocketed'] = 0;
        $item['magicproperty0'] = $item['earclass'];
        $item['magicproperty1'] = "Level " . $item['earlevel'];
        if ($Hardcore) {
            $item['magicproperty2'] = "Hardcore";
            $item['magiccount'] = 3;
        } else {
            $item['magiccount'] = 2;
        }
    } else {
        $item['type'] = item_name(chr(bindec(substr($binitem, $itemlength - 68, 8))) . chr(bindec(substr($binitem, $itemlength - 76, 8))) . chr(bindec(substr($binitem, $itemlength - 84, 8))), $item['description'], $item['stacked']);
        //    $offset = $itemlength-85; // for simple items
        $offset = $itemlength - 93;
        if ($item['issimple'] == 0) {
            if ($item['noreadgems'] == 0) {
                $item['itemsinsockets'] = bindec(substr($binitem, $offset - 2, 3));
                $offset -= 3;
            }
            $item['id'] = str_pad(dechex(bindec(substr($binitem, $offset - 31, 32))), 8, '0', STR_PAD_LEFT);
            $offset -= 32;
            $item['level'] = bindec(substr($binitem, $offset - 6, 7));
            $offset -= 7;
            $item['quality'] = bindec(substr($binitem, $offset - 3, 4));
            $offset -= 4;
            // graphic
            if (substr($binitem, $offset, 1) == 1) {
                // bool
                $item['ringpicture'] = bindec(substr($binitem, $offset - 3, 3));
                $offset -= 3;
            }
            $offset--;
            // class info
            if (substr($binitem, $offset, 1) == 1) {
                // bool
                $item['class'] = bindec(substr($binitem, $offset - 11, 11));
                $offset -= 11;
            }
            $offset--;
            // high or low quality item
            if ($item['quality'] == 1 || $item['quality'] == 3) {
                $item['quality2'] = bindec(substr($binitem, $offset - 2, 3));
                $offset -= 3;
            }
            // normal quality item
            if ($item['quality'] == 2) {
                //        if ($item['description'] == "char" && substr($binitem, $offset, 1) == 1) {
                //          $item['charmaffix'] = bindec(substr($binitem, $offset-11, 11));
                if ($item['description'] == "char") {
                    $item['charmaffix'] = bindec(substr($binitem, $offset - 11, 12));
                    $offset -= 12;
                }
                if ($item['description'] == "book" || $item['description'] == "scro") {
                    $item['spellid'] = bindec(substr($binitem, $offset - 4, 5));
                    $offset -= 5;
                }
                if ($item['description'] == "body") {
                    // not that I have an item that will make it here...
                    $item['monsterid'] == bindec(substr($binitem, $offset - 9, 10));
                    $offset -= 10;
                }
            }
            // magic item
            if ($item['quality'] == 4) {
                $item['magicprefix'] = bindec(substr($binitem, $offset - 10, 11));
                $item['magicsuffix'] = bindec(substr($binitem, $offset - 21, 11));
                $offset -= 22;
            }
            // set item
            if ($item['quality'] == 5) {
                $item['setid'] = bindec(substr($binitem, $offset - 11, 12));
                $offset -= 12;
            }
            // rare or crafted item
            if ($item['quality'] == 6 || $item['quality'] == 8) {
                $item['rareword1'] = bindec(substr($binitem, $offset - 7, 8));
                $item['rareword2'] = bindec(substr($binitem, $offset - 15, 8));
                $offset -= 16;
                if (substr($binitem, $offset, 1) == 1) {
                    $item['rareprefix1'] = bindec(substr($binitem, $offset - 11, 11));
                    $offset -= 11;
                }
                $offset--;
                if (substr($binitem, $offset, 1) == 1) {
                    $item['raresuffix1'] = bindec(substr($binitem, $offset - 11, 11));
                    $offset -= 11;
                }
                $offset--;
                if (substr($binitem, $offset, 1) == 1) {
                    $item['rareprefix2'] = bindec(substr($binitem, $offset - 11, 11));
                    $offset -= 11;
                }
                $offset--;
                if (substr($binitem, $offset, 1) == 1) {
                    $item['raresuffix2'] = bindec(substr($binitem, $offset - 11, 11));
                    $offset -= 11;
                }
                $offset--;
                if (substr($binitem, $offset, 1) == 1) {
                    $item['rareprefix3'] = bindec(substr($binitem, $offset - 11, 11));
                    $offset -= 11;
                }
                $offset--;
                if (substr($binitem, $offset, 1) == 1) {
                    $item['raresuffix3'] = bindec(substr($binitem, $offset - 11, 11));
                    $offset -= 11;
                }
                $offset--;
            }
            // unique item
            if ($item['quality'] == 7) {
                $item['uniqueid'] = bindec(substr($binitem, $offset - 11, 12));
                $offset -= 12;
            }
            // tempered items?
            if ($item['quality'] == 9) {
                $item['rareword1'] = bindec(substr($binitem, $offset - 7, 8));
                $item['rareword2'] = bindec(substr($binitem, $offset - 15, 8));
                $offset -= 16;
            }
            // rune word
            if ($item['hasruneword'] == 1) {
                $item['runeword'] = runeword_decode(bindec(substr($binitem, $offset - 15, 16)));
                $offset -= 16;
            }
            // personalized
            if ($item['ispersonalized'] == 1) {
                $itemname = $letter = "";
                $letters = 0;
                while ($letters < 15 && $letter != chr(0)) {
                    $letter = chr(bindec(substr($binitem, $offset - 6, 7)));
                    $itemname = $itemname . $letter;
                    $letters++;
                    $offset -= 7;
                }
                $item['itemname'] = substr($itemname, 0, -1);
            }
        }
        if ($item['description'] == "gold") {
            $item['goldflag'] = substr($binitem, $offset, 1);
            $offset--;
            if ($item['goldflag']) {
                $item['gold'] = bindec(substr($binitem, $offset - 11, 12));
                $offset -= 12;
            } else {
                $item['gold'] = bindec(substr($binitem, $offset - 31, 32));
                $offset -= 32;
            }
        }
        // Time Stamp Flag
        if (substr($binitem, $offset, 1) == 1) {
            $item['rand1'] = str_pad(dechex(bindec(substr($binitem, $offset - 31, 32))), 8, '0', STR_PAD_LEFT);
            $item['rand2'] = str_pad(dechex(bindec(substr($binitem, $offset - 63, 32))), 8, '0', STR_PAD_LEFT);
            $item['rand3'] = str_pad(dechex(bindec(substr($binitem, $offset - 95, 32))), 8, '0', STR_PAD_LEFT);
            $offset -= 96;
        }
        $offset--;
        if ($item['issimple'] == 0) {
            // defense rating (base)
            if ($item['description'] == "armo" || $item['description'] == "shld") {
                $item['defense'] = bindec(substr($binitem, $offset - 10, 11)) - 10;
                $offset -= 11;
            }
            // durability
            if ($item['description'] == "armo" || $item['description'] == "shld" || $item['description'] == "weap") {
                $item['maxdurability'] = bindec(substr($binitem, $offset - 7, 8));
                $offset -= 8;
                if ($item['maxdurability'] > 0) {
                    $item['curdurability'] = bindec(substr($binitem, $offset - 8, 9));
                    $offset -= 9;
                }
            }
            // quantity
            if ($item['stacked']) {
                $item['quantity'] = bindec(substr($binitem, $offset - 8, 9));
                $offset -= 9;
            }
            // number of sockets
            if ($item['issocketed']) {
                $item['socketcount'] = bindec(substr($binitem, $offset - 3, 4));
                $offset -= 4;
            }
            // number of magic property lists for set items
            if ($item['quality'] == 5) {
                $item['setlists'] = bindec(substr($binitem, $offset - 4, 5));
                $offset -= 5;
            }
            $magiccount = 0;
            $magicproperty = '';
            $magicid = bindec(substr($binitem, $offset - 8, 9));
            while ($magicid != 511 || $offset < 0) {
                $newoffset = magic_properties($magicid, $binitem, $offset - 9, $magicproperty, $level);
                if ($newoffset == $offset - 9) {
                    break;
                }
                $offset = $newoffset;
                //        $item["magicproperty".$magiccount] = $magicid ." ". $magicproperty;
                $item["magicproperty" . $magiccount] = $magicproperty;
                $magicid = bindec(substr($binitem, $offset - 8, 9));
                if ($magiccount > 20) {
                    break;
                }
                $magiccount++;
            }
            $offset -= 9;
            $lastmagicproperty = '';
            if ($item['isethereal']) {
                $lastmagicproperty = "Ethereal (Cannot be Repaired), ";
            }
            if ($item['issocketed']) {
                $lastmagicproperty .= "Socketed (" . $item['socketcount'] . "), ";
            }
            if ($lastmagicproperty != "") {
                $item["magicproperty" . $magiccount] = substr($lastmagicproperty, 0, -2);
                $magiccount++;
            }
            $item['magiccount'] = $magiccount;
        }
        if ($item['hasruneword'] == 1) {
            //      $magiccount = 0;
            $magicproperty = '';
            $magicid = bindec(substr($binitem, $offset - 8, 9));
            while ($magicid != 511 || $offset < 0) {
                $newoffset = magic_properties($magicid, $binitem, $offset - 9, $magicproperty, $level);
                if ($newoffset == $offset - 9) {
                    break;
                }
                $offset = $newoffset;
                //        $item["magicproperty".$magiccount] = $magicid ." ". $magicproperty;
                $item["magicproperty" . $magiccount] = $magicproperty;
                $magicid = bindec(substr($binitem, $offset - 8, 9));
                if ($magiccount > 20) {
                    break;
                }
                $magiccount++;
            }
            $item['magiccount'] = $magiccount;
        }
        if ($item['issocketed'] == 1) {
            for ($i = 0; $i < $item['itemsinsockets']; $i++) {
                $socketitem = array();
                $socketend = strpos($rawdata, 'JM', $socketstart);
                // if there is a golem, this might break for the merc
                if ($socketend === false) {
                    $socketend = $mercend;
                }
                $socketstart = item_decode($socketstart, $socketend, $rawdata, $data, $socketitem, $mercend, $level);
                $socketstart = $socketend + 2;
                $item["socket" . $i] = array_merge($socketitem[0]);
            }
        }
    }
    array_push($items, $item);
    //  return ($endpos + 2);
    return $socketstart;
}