CFBinaryPropertyList.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. namespace CFPropertyList;
  3. abstract class CFBinaryPropertyList
  4. {
  5. protected $content = NULL;
  6. protected $pos = 0;
  7. protected $uniqueTable = Array();
  8. protected $countObjects = 0;
  9. protected $stringSize = 0;
  10. protected $intSize = 0;
  11. protected $miscSize = 0;
  12. protected $objectRefs = 0;
  13. protected $writtenObjectCount = 0;
  14. protected $objectTable = Array();
  15. protected $objectRefSize = 0;
  16. protected $offsets = Array();
  17. protected function readBinaryNullType($length)
  18. {
  19. switch ($length) {
  20. case 0:
  21. return 0;
  22. case 8:
  23. return new CFBoolean(false);
  24. case 9:
  25. return new CFBoolean(true);
  26. case 15:
  27. return 15;
  28. }
  29. throw new PListException("unknown null type: $length");
  30. }
  31. protected static function make64Int($hi, $lo)
  32. {
  33. if (PHP_INT_SIZE > 4) return (((int)$hi) << 32) | ((int)$lo);
  34. $lo = sprintf("%u", $lo);
  35. if (function_exists("gmp_mul")) return gmp_strval(gmp_add(gmp_mul($hi, "4294967296"), $lo));
  36. if (function_exists("bcmul")) return bcadd(bcmul($hi, "4294967296"), $lo);
  37. if (class_exists('Math_BigInteger')) {
  38. $bi = new \Math_BigInteger($hi);
  39. return $bi->multiply(new \Math_BigInteger("4294967296"))->add(new \Math_BigInteger($lo))->toString();
  40. }
  41. throw new PListException("either gmp or bc has to be installed, or the Math_BigInteger has to be available!");
  42. }
  43. protected function readBinaryInt($length)
  44. {
  45. if ($length > 3) throw new PListException("Integer greater than 8 bytes: $length");
  46. $nbytes = 1 << $length;
  47. $val = null;
  48. if (strlen($buff = substr($this->content, $this->pos, $nbytes)) != $nbytes) throw IOException::readError("");
  49. $this->pos += $nbytes;
  50. switch ($length) {
  51. case 0:
  52. $val = unpack("C", $buff);
  53. $val = $val[1];
  54. break;
  55. case 1:
  56. $val = unpack("n", $buff);
  57. $val = $val[1];
  58. break;
  59. case 2:
  60. $val = unpack("N", $buff);
  61. $val = $val[1];
  62. break;
  63. case 3:
  64. $words = unpack("Nhighword/Nlowword", $buff);
  65. $val = self::make64Int($words['highword'], $words['lowword']);
  66. break;
  67. }
  68. return new CFNumber($val);
  69. }
  70. protected function readBinaryReal($length)
  71. {
  72. if ($length > 3) throw new PListException("Real greater than 8 bytes: $length");
  73. $nbytes = 1 << $length;
  74. $val = null;
  75. if (strlen($buff = substr($this->content, $this->pos, $nbytes)) != $nbytes) throw IOException::readError("");
  76. $this->pos += $nbytes;
  77. switch ($length) {
  78. case 0:
  79. case 1:
  80. $x = $length + 1;
  81. throw new PListException("got {$x} byte float, must be an error!");
  82. case 2:
  83. $val = unpack("f", strrev($buff));
  84. $val = $val[1];
  85. break;
  86. case 3:
  87. $val = unpack("d", strrev($buff));
  88. $val = $val[1];
  89. break;
  90. }
  91. return new CFNumber($val);
  92. }
  93. protected function readBinaryDate($length)
  94. {
  95. if ($length > 3) throw new PListException("Date greater than 8 bytes: $length");
  96. $nbytes = 1 << $length;
  97. $val = null;
  98. if (strlen($buff = substr($this->content, $this->pos, $nbytes)) != $nbytes) throw IOException::readError("");
  99. $this->pos += $nbytes;
  100. switch ($length) {
  101. case 0:
  102. case 1:
  103. $x = $length + 1;
  104. throw new PListException("{$x} byte CFdate, error");
  105. case 2:
  106. $val = unpack("f", strrev($buff));
  107. $val = $val[1];
  108. break;
  109. case 3:
  110. $val = unpack("d", strrev($buff));
  111. $val = $val[1];
  112. break;
  113. }
  114. return new CFDate($val, CFDate::TIMESTAMP_APPLE);
  115. }
  116. protected function readBinaryData($length)
  117. {
  118. if ($length == 0) $buff = "";
  119. else {
  120. $buff = substr($this->content, $this->pos, $length);
  121. if (strlen($buff) != $length) throw IOException::readError("");
  122. $this->pos += $length;
  123. }
  124. return new CFData($buff, false);
  125. }
  126. protected function readBinaryString($length)
  127. {
  128. if ($length == 0) $buff = "";
  129. else {
  130. if (strlen($buff = substr($this->content, $this->pos, $length)) != $length) throw IOException::readError("");
  131. $this->pos += $length;
  132. }
  133. if (!isset($this->uniqueTable[$buff])) $this->uniqueTable[$buff] = true;
  134. return new CFString($buff);
  135. }
  136. public static function convertCharset($string, $fromCharset, $toCharset = 'UTF-8')
  137. {
  138. if (function_exists('mb_convert_encoding')) return mb_convert_encoding($string, $toCharset, $fromCharset);
  139. if (function_exists('iconv')) return iconv($fromCharset, $toCharset, $string);
  140. if (function_exists('recode_string')) return recode_string($fromCharset . '..' . $toCharset, $string);
  141. throw new PListException('neither iconv nor mbstring supported. how are we supposed to work on strings here?');
  142. }
  143. public static function charsetStrlen($string, $charset = "UTF-8")
  144. {
  145. if (function_exists('mb_strlen')) return mb_strlen($string, $charset);
  146. if (function_exists('iconv_strlen')) return iconv_strlen($string, $charset);
  147. throw new PListException('neither iconv nor mbstring supported. how are we supposed to work on strings here?');
  148. }
  149. protected function readBinaryUnicodeString($length)
  150. {
  151. if (strlen($buff = substr($this->content, $this->pos, 2 * $length)) != 2 * $length) throw IOException::readError("");
  152. $this->pos += 2 * $length;
  153. if (!isset($this->uniqueTable[$buff])) $this->uniqueTable[$buff] = true;
  154. return new CFString(self::convertCharset($buff, "UTF-16BE", "UTF-8"));
  155. }
  156. protected function readBinaryArray($length)
  157. {
  158. $ary = new CFArray();
  159. if ($length != 0) {
  160. if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) throw IOException::readError("");
  161. $this->pos += $length * $this->objectRefSize;
  162. $objects = self::unpackWithSize($this->objectRefSize, $buff);
  163. for ($i = 0; $i < $length; ++$i) {
  164. $object = $this->readBinaryObjectAt($objects[$i + 1] + 1);
  165. $ary->add($object);
  166. }
  167. }
  168. return $ary;
  169. }
  170. protected function readBinaryDict($length)
  171. {
  172. $dict = new CFDictionary();
  173. if ($length != 0) {
  174. if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) throw IOException::readError("");
  175. $this->pos += $length * $this->objectRefSize;
  176. $keys = self::unpackWithSize($this->objectRefSize, $buff);
  177. if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) throw IOException::readError("");
  178. $this->pos += $length * $this->objectRefSize;
  179. $objects = self::unpackWithSize($this->objectRefSize, $buff);
  180. for ($i = 0; $i < $length; ++$i) {
  181. $key = $this->readBinaryObjectAt($keys[$i + 1] + 1);
  182. $object = $this->readBinaryObjectAt($objects[$i + 1] + 1);
  183. $dict->add($key->getValue(), $object);
  184. }
  185. }
  186. return $dict;
  187. }
  188. function readBinaryObject()
  189. {
  190. if (strlen($buff = substr($this->content, $this->pos, 1)) != 1) throw IOException::readError("");
  191. $this->pos++;
  192. $object_length = unpack("C*", $buff);
  193. $object_length = $object_length[1] & 0xF;
  194. $buff = unpack("H*", $buff);
  195. $buff = $buff[1];
  196. $object_type = substr($buff, 0, 1);
  197. if ($object_type != "0" && $object_length == 15) {
  198. $object_length = $this->readBinaryObject($this->objectRefSize);
  199. $object_length = $object_length->getValue();
  200. }
  201. $retval = null;
  202. switch ($object_type) {
  203. case '0':
  204. $retval = $this->readBinaryNullType($object_length);
  205. break;
  206. case '1':
  207. $retval = $this->readBinaryInt($object_length);
  208. break;
  209. case '2':
  210. $retval = $this->readBinaryReal($object_length);
  211. break;
  212. case '3':
  213. $retval = $this->readBinaryDate($object_length);
  214. break;
  215. case '4':
  216. $retval = $this->readBinaryData($object_length);
  217. break;
  218. case '5':
  219. $retval = $this->readBinaryString($object_length);
  220. break;
  221. case '6':
  222. $retval = $this->readBinaryUnicodeString($object_length);
  223. break;
  224. case '8':
  225. $num = $this->readBinaryInt($object_length);
  226. $retval = new CFUid($num->getValue());
  227. break;
  228. case 'a':
  229. $retval = $this->readBinaryArray($object_length);
  230. break;
  231. case 'd':
  232. $retval = $this->readBinaryDict($object_length);
  233. break;
  234. }
  235. return $retval;
  236. }
  237. function readBinaryObjectAt($pos)
  238. {
  239. $this->pos = $this->offsets[$pos];
  240. return $this->readBinaryObject();
  241. }
  242. public function parseBinaryString()
  243. {
  244. $this->uniqueTable = Array();
  245. $this->countObjects = 0;
  246. $this->stringSize = 0;
  247. $this->intSize = 0;
  248. $this->miscSize = 0;
  249. $this->objectRefs = 0;
  250. $this->writtenObjectCount = 0;
  251. $this->objectTable = Array();
  252. $this->objectRefSize = 0;
  253. $this->offsets = Array();
  254. $buff = substr($this->content, -32);
  255. if (strlen($buff) < 32) {
  256. throw new PListException('Error in PList format: content is less than at least necessary 32 bytes!');
  257. }
  258. $infos = unpack("x6/Coffset_size/Cobject_ref_size/x4/Nnumber_of_objects/x4/Ntop_object/x4/Ntable_offset", $buff);
  259. $coded_offset_table = substr($this->content, $infos['table_offset'], $infos['number_of_objects'] * $infos['offset_size']);
  260. if (strlen($coded_offset_table) != $infos['number_of_objects'] * $infos['offset_size']) throw IOException::readError("");
  261. $this->countObjects = $infos['number_of_objects'];
  262. $formats = Array("", "C*", "n*", NULL, "N*");
  263. if ($infos['offset_size'] == 3) {# since PHP does not support parenthesis in pack/unpack expressions,
  264. # "(H6)*"does not work and we have to work round this by repeating the
  265. # expression as often as it fits in the string
  266. $this->offsets = array(NULL);
  267. while ($coded_offset_table) {
  268. $str = unpack("H6", $coded_offset_table);
  269. $this->offsets[] = hexdec($str[1]);
  270. $coded_offset_table = substr($coded_offset_table, 3);
  271. }
  272. } else $this->offsets = unpack($formats[$infos['offset_size']], $coded_offset_table);
  273. $this->uniqueTable = Array();
  274. $this->objectRefSize = $infos['object_ref_size'];
  275. $top = $this->readBinaryObjectAt($infos['top_object'] + 1);
  276. $this->add($top);
  277. }
  278. function readBinaryStream($stream)
  279. {
  280. if (($str = stream_get_contents($stream)) === false || empty($str)) {
  281. throw new PListException("Error reading stream!");
  282. }
  283. $this->parseBinary($str);
  284. }
  285. function parseBinary($content = NULL)
  286. {
  287. if ($content !== NULL) {
  288. $this->content = $content;
  289. }
  290. if (empty($this->content)) {
  291. throw new PListException("Content may not be empty!");
  292. }
  293. if (substr($this->content, 0, 8) != 'bplist00') {
  294. throw new PListException("Invalid binary string!");
  295. }
  296. $this->pos = 0;
  297. $this->parseBinaryString();
  298. }
  299. function readBinary($file)
  300. {
  301. if (!($fd = fopen($file, "rb"))) {
  302. throw new IOException("Could not open file {$file}!");
  303. }
  304. $this->readBinaryStream($fd);
  305. fclose($fd);
  306. }
  307. public static function bytesSizeInt($int)
  308. {
  309. $nbytes = 0;
  310. if ($int > 0xE) $nbytes += 2;
  311. if ($int > 0xFF) $nbytes += 1;
  312. if ($int > 0xFFFF) $nbytes += 2;
  313. return $nbytes;
  314. }
  315. public static function bytesInt($int)
  316. {
  317. $nbytes = 1;
  318. if ($int > 0xFF) $nbytes += 1;
  319. if ($int > 0xFFFF) $nbytes += 2;
  320. if ($int > 0xFFFFFFFF) $nbytes += 4;
  321. if ($int < 0) $nbytes += 7;
  322. return $nbytes + 1;
  323. }
  324. public static function packItWithSize($nbytes, $int)
  325. {
  326. $formats = Array("C", "n", "N", "N");
  327. $format = $formats[$nbytes - 1];
  328. if ($nbytes == 3) return substr(pack($format, $int), -3);
  329. return pack($format, $int);
  330. }
  331. public static function unpackWithSize($nbytes, $buff)
  332. {
  333. $formats = Array("C*", "n*", "N*", "N*");
  334. $format = $formats[$nbytes - 1];
  335. if ($nbytes == 3) $buff = "\0" . implode("\0", str_split($buff, 3));
  336. return unpack($format, $buff);
  337. }
  338. public static function bytesNeeded($count_objects)
  339. {
  340. $nbytes = 0;
  341. while ($count_objects >= 1) {
  342. $nbytes++;
  343. $count_objects /= 256;
  344. }
  345. return $nbytes;
  346. }
  347. public static function intBytes($int)
  348. {
  349. $intbytes = "";
  350. if ($int > 0xFFFF) $intbytes = "\x12" . pack("N", $int);
  351. elseif ($int > 0xFF) $intbytes = "\x11" . pack("n", $int);
  352. else $intbytes = "\x10" . pack("C", $int);
  353. return $intbytes;
  354. }
  355. public static function typeBytes($type, $type_len)
  356. {
  357. $optional_int = "";
  358. if ($type_len < 15) $type .= sprintf("%x", $type_len);
  359. else {
  360. $type .= "f";
  361. $optional_int = self::intBytes($type_len);
  362. }
  363. return pack("H*", $type) . $optional_int;
  364. }
  365. protected function uniqueAndCountValues($value)
  366. {
  367. if ($value instanceof CFNumber) {
  368. $val = $value->getValue();
  369. if (intval($val) == $val && !is_float($val) && strpos($val, '.') === false) $this->intSize += self::bytesInt($val);
  370. else $this->miscSize += 9;
  371. $this->countObjects++;
  372. return;
  373. } elseif ($value instanceof CFDate) {
  374. $this->miscSize += 9;
  375. $this->countObjects++;
  376. return;
  377. } elseif ($value instanceof CFBoolean) {
  378. $this->countObjects++;
  379. $this->miscSize += 1;
  380. return;
  381. } elseif ($value instanceof CFArray) {
  382. $cnt = 0;
  383. foreach ($value as $v) {
  384. ++$cnt;
  385. $this->uniqueAndCountValues($v);
  386. $this->objectRefs++;
  387. }
  388. $this->countObjects++;
  389. $this->intSize += self::bytesSizeInt($cnt);
  390. $this->miscSize++;
  391. return;
  392. } elseif ($value instanceof CFDictionary) {
  393. $cnt = 0;
  394. foreach ($value as $k => $v) {
  395. ++$cnt;
  396. if (!isset($this->uniqueTable[$k])) {
  397. $this->uniqueTable[$k] = 0;
  398. $len = self::binaryStrlen($k);
  399. $this->stringSize += $len + 1;
  400. $this->intSize += self::bytesSizeInt(self::charsetStrlen($k, 'UTF-8'));
  401. }
  402. $this->objectRefs += 2;
  403. $this->uniqueTable[$k]++;
  404. $this->uniqueAndCountValues($v);
  405. }
  406. $this->countObjects++;
  407. $this->miscSize++;
  408. $this->intSize += self::bytesSizeInt($cnt);
  409. return;
  410. } elseif ($value instanceOf CFData) {
  411. $val = $value->getValue();
  412. $len = strlen($val);
  413. $this->intSize += self::bytesSizeInt($len);
  414. $this->miscSize += $len + 1;
  415. $this->countObjects++;
  416. return;
  417. } else $val = $value->getValue();
  418. if (!isset($this->uniqueTable[$val])) {
  419. $this->uniqueTable[$val] = 0;
  420. $len = self::binaryStrlen($val);
  421. $this->stringSize += $len + 1;
  422. $this->intSize += self::bytesSizeInt(self::charsetStrlen($val, 'UTF-8'));
  423. }
  424. $this->uniqueTable[$val]++;
  425. }
  426. public function toBinary()
  427. {
  428. $this->uniqueTable = Array();
  429. $this->countObjects = 0;
  430. $this->stringSize = 0;
  431. $this->intSize = 0;
  432. $this->miscSize = 0;
  433. $this->objectRefs = 0;
  434. $this->writtenObjectCount = 0;
  435. $this->objectTable = Array();
  436. $this->objectRefSize = 0;
  437. $this->offsets = Array();
  438. $binary_str = "bplist00";
  439. $value = $this->getValue(true);
  440. $this->uniqueAndCountValues($value);
  441. $this->countObjects += count($this->uniqueTable);
  442. $this->objectRefSize = self::bytesNeeded($this->countObjects);
  443. $file_size = $this->stringSize + $this->intSize + $this->miscSize + $this->objectRefs * $this->objectRefSize + 40;
  444. $offset_size = self::bytesNeeded($file_size);
  445. $table_offset = $file_size - 32;
  446. $this->objectTable = Array();
  447. $this->writtenObjectCount = 0;
  448. $this->uniqueTable = Array();
  449. $value->toBinary($this);
  450. $object_offset = 8;
  451. $offsets = Array();
  452. for ($i = 0; $i < count($this->objectTable); ++$i) {
  453. $binary_str .= $this->objectTable[$i];
  454. $offsets[$i] = $object_offset;
  455. $object_offset += strlen($this->objectTable[$i]);
  456. }
  457. for ($i = 0; $i < count($offsets); ++$i) {
  458. $binary_str .= self::packItWithSize($offset_size, $offsets[$i]);
  459. }
  460. $binary_str .= pack("x6CC", $offset_size, $this->objectRefSize);
  461. $binary_str .= pack("x4N", $this->countObjects);
  462. $binary_str .= pack("x4N", 0);
  463. $binary_str .= pack("x4N", $table_offset);
  464. return $binary_str;
  465. }
  466. protected static function binaryStrlen($val)
  467. {
  468. for ($i = 0; $i < strlen($val); ++$i) {
  469. if (ord($val{$i}) >= 128) {
  470. $val = self::convertCharset($val, 'UTF-8', 'UTF-16BE');
  471. return strlen($val);
  472. }
  473. }
  474. return strlen($val);
  475. }
  476. public function stringToBinary($val)
  477. {
  478. $saved_object_count = -1;
  479. if (!isset($this->uniqueTable[$val])) {
  480. $saved_object_count = $this->writtenObjectCount++;
  481. $this->uniqueTable[$val] = $saved_object_count;
  482. $utf16 = false;
  483. for ($i = 0; $i < strlen($val); ++$i) {
  484. if (ord($val{$i}) >= 128) {
  485. $utf16 = true;
  486. break;
  487. }
  488. }
  489. if ($utf16) {
  490. $bdata = self::typeBytes("6", mb_strlen($val, 'UTF-8'));
  491. $val = self::convertCharset($val, 'UTF-8', 'UTF-16BE');
  492. $this->objectTable[$saved_object_count] = $bdata . $val;
  493. } else {
  494. $bdata = self::typeBytes("5", strlen($val));
  495. $this->objectTable[$saved_object_count] = $bdata . $val;
  496. }
  497. } else $saved_object_count = $this->uniqueTable[$val];
  498. return $saved_object_count;
  499. }
  500. protected function intToBinary($value)
  501. {
  502. $nbytes = 0;
  503. if ($value > 0xFF) $nbytes = 1;
  504. if ($value > 0xFFFF) $nbytes += 1;
  505. if ($value > 0xFFFFFFFF) $nbytes += 1;
  506. if ($value < 0) $nbytes = 3;
  507. $bdata = self::typeBytes("1", $nbytes);
  508. $buff = "";
  509. if ($nbytes < 3) {
  510. if ($nbytes == 0) $fmt = "C";
  511. elseif ($nbytes == 1) $fmt = "n";
  512. else $fmt = "N";
  513. $buff = pack($fmt, $value);
  514. } else {
  515. if (PHP_INT_SIZE > 4) {
  516. $high_word = $value >> 32;
  517. $low_word = $value & 0xFFFFFFFF;
  518. } else {
  519. if ($value < 0) $high_word = 0xFFFFFFFF;
  520. else $high_word = 0;
  521. $low_word = $value;
  522. }
  523. $buff = pack("N", $high_word) . pack("N", $low_word);
  524. }
  525. return $bdata . $buff;
  526. }
  527. protected function realToBinary($val)
  528. {
  529. $bdata = self::typeBytes("2", 3);
  530. return $bdata . strrev(pack("d", (float)$val));
  531. }
  532. public function uidToBinary($value)
  533. {
  534. $saved_object_count = $this->writtenObjectCount++;
  535. $val = "";
  536. $nbytes = 0;
  537. if ($value > 0xFF) $nbytes = 1;
  538. if ($value > 0xFFFF) $nbytes += 1;
  539. if ($value > 0xFFFFFFFF) $nbytes += 1;
  540. if ($value < 0) $nbytes = 3;
  541. $bdata = self::typeBytes("1000", $nbytes);
  542. $buff = "";
  543. if ($nbytes < 3) {
  544. if ($nbytes == 0) $fmt = "C";
  545. elseif ($nbytes == 1) $fmt = "n";
  546. else $fmt = "N";
  547. $buff = pack($fmt, $value);
  548. } else {
  549. if (PHP_INT_SIZE > 4) {
  550. $high_word = $value >> 32;
  551. $low_word = $value & 0xFFFFFFFF;
  552. } else {
  553. if ($value < 0) $high_word = 0xFFFFFFFF;
  554. else $high_word = 0;
  555. $low_word = $value;
  556. }
  557. $buff = pack("N", $high_word) . pack("N", $low_word);
  558. }
  559. $val = $bdata . $buff;
  560. $this->objectTable[$saved_object_count] = $val;
  561. return $saved_object_count;
  562. }
  563. public function numToBinary($value)
  564. {
  565. $saved_object_count = $this->writtenObjectCount++;
  566. $val = "";
  567. if (intval($value) == $value && !is_float($value) && strpos($value, '.') === false) $val = $this->intToBinary($value);
  568. else $val = $this->realToBinary($value);
  569. $this->objectTable[$saved_object_count] = $val;
  570. return $saved_object_count;
  571. }
  572. public function dateToBinary($val)
  573. {
  574. $saved_object_count = $this->writtenObjectCount++;
  575. $hour = gmdate("H", $val);
  576. $min = gmdate("i", $val);
  577. $sec = gmdate("s", $val);
  578. $mday = gmdate("j", $val);
  579. $mon = gmdate("n", $val);
  580. $year = gmdate("Y", $val);
  581. $val = gmmktime($hour, $min, $sec, $mon, $mday, $year) - CFDate::DATE_DIFF_APPLE_UNIX;
  582. $bdata = self::typeBytes("3", 3);
  583. $this->objectTable[$saved_object_count] = $bdata . strrev(pack("d", $val));
  584. return $saved_object_count;
  585. }
  586. public function boolToBinary($val)
  587. {
  588. $saved_object_count = $this->writtenObjectCount++;
  589. $this->objectTable[$saved_object_count] = $val ? "\x9" : "\x8";
  590. return $saved_object_count;
  591. }
  592. public function dataToBinary($val)
  593. {
  594. $saved_object_count = $this->writtenObjectCount++;
  595. $bdata = self::typeBytes("4", strlen($val));
  596. $this->objectTable[$saved_object_count] = $bdata . $val;
  597. return $saved_object_count;
  598. }
  599. public function arrayToBinary($val)
  600. {
  601. $saved_object_count = $this->writtenObjectCount++;
  602. $bdata = self::typeBytes("a", count($val->getValue()));
  603. foreach ($val as $v) {
  604. $bval = $v->toBinary($this);
  605. $bdata .= self::packItWithSize($this->objectRefSize, $bval);
  606. }
  607. $this->objectTable[$saved_object_count] = $bdata;
  608. return $saved_object_count;
  609. }
  610. public function dictToBinary($val)
  611. {
  612. $saved_object_count = $this->writtenObjectCount++;
  613. $bdata = self::typeBytes("d", count($val->getValue()));
  614. foreach ($val as $k => $v) {
  615. $str = new CFString($k);
  616. $key = $str->toBinary($this);
  617. $bdata .= self::packItWithSize($this->objectRefSize, $key);
  618. }
  619. foreach ($val as $k => $v) {
  620. $bval = $v->toBinary($this);
  621. $bdata .= self::packItWithSize($this->objectRefSize, $bval);
  622. }
  623. $this->objectTable[$saved_object_count] = $bdata;
  624. return $saved_object_count;
  625. }
  626. }
  627. ?>