+ private function outFd($path, $flags) {
+ $dirn = dirname($path);
+ if (!is_dir($dirn) && !@mkdir($dirn, 0777, true)) {
+ throw new Exception;
+ }
+ if (!$fd = @fopen($path, "w")) {
+ throw new Exception;
+ }
+ switch ($flags & self::COMP_FILE_MASK) {
+ case self::COMP_GZ_FILE:
+ if (!@stream_filter_append($fd, "zlib.inflate")) {
+ throw new Exception;
+ }
+ break;
+ case self::COMP_BZ2_FILE:
+ if (!@stream_filter_append($fd, "bz2.decompress")) {
+ throw new Exception;
+ }
+ break;
+ }
+
+ }
+ private function readVerified($fd, $len) {
+ if ($len != strlen($data = fread($fd, $len))) {
+ throw new Exception("Unexpected EOF");
+ }
+ return $data;
+ }
+
+ private function readFormat($format, $fd, $len) {
+ if (false === ($data = @unpack($format, $this->readVerified($fd, $len)))) {
+ throw new Exception;
+ }
+ return $data;
+ }
+
+ private function readSingleFormat($format, $fd, $len) {
+ return current($this->readFormat($format, $fd, $len));
+ }
+
+ private function readStringBinary($fd) {
+ if (($length = $this->readSingleFormat("V", $fd, 4))) {
+ return $this->readVerified($this->fd, $length);
+ }
+ return null;
+ }
+
+ private function readSerializedBinary($fd) {
+ if (($length = $this->readSingleFormat("V", $fd, 4))) {
+ if (false === ($data = unserialize($this->readVerified($fd, $length)))) {
+ throw new Exception;
+ }
+ return $data;
+ }
+ return null;
+ }
+