BIFFwriter.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * PHPExcel_Writer_Excel5_BIFFwriter
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel5
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. // Original file header of PEAR::Spreadsheet_Excel_Writer_BIFFwriter (used as the base for this class):
  28. // -----------------------------------------------------------------------------------------
  29. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  30. // *
  31. // * The majority of this is _NOT_ my code. I simply ported it from the
  32. // * PERL Spreadsheet::WriteExcel module.
  33. // *
  34. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  35. // * <jmcnamara@cpan.org>
  36. // *
  37. // * I _DO_ maintain this code, and John McNamara has nothing to do with the
  38. // * porting of this code to PHP. Any questions directly related to this
  39. // * class library should be directed to me.
  40. // *
  41. // * License Information:
  42. // *
  43. // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  44. // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  45. // *
  46. // * This library is free software; you can redistribute it and/or
  47. // * modify it under the terms of the GNU Lesser General Public
  48. // * License as published by the Free Software Foundation; either
  49. // * version 2.1 of the License, or (at your option) any later version.
  50. // *
  51. // * This library is distributed in the hope that it will be useful,
  52. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  54. // * Lesser General Public License for more details.
  55. // *
  56. // * You should have received a copy of the GNU Lesser General Public
  57. // * License along with this library; if not, write to the Free Software
  58. // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  59. // */
  60. class PHPExcel_Writer_Excel5_BIFFwriter
  61. {
  62. /**
  63. * The byte order of this architecture. 0 => little endian, 1 => big endian
  64. * @var integer
  65. */
  66. private static $byteOrder;
  67. /**
  68. * The string containing the data of the BIFF stream
  69. * @var string
  70. */
  71. public $_data;
  72. /**
  73. * The size of the data in bytes. Should be the same as strlen($this->_data)
  74. * @var integer
  75. */
  76. public $_datasize;
  77. /**
  78. * The maximum length for a BIFF record (excluding record header and length field). See addContinue()
  79. * @var integer
  80. * @see addContinue()
  81. */
  82. private $limit = 8224;
  83. /**
  84. * Constructor
  85. */
  86. public function __construct()
  87. {
  88. $this->_data = '';
  89. $this->_datasize = 0;
  90. // $this->limit = 8224;
  91. }
  92. /**
  93. * Determine the byte order and store it as class data to avoid
  94. * recalculating it for each call to new().
  95. *
  96. * @return int
  97. */
  98. public static function getByteOrder()
  99. {
  100. if (!isset(self::$byteOrder)) {
  101. // Check if "pack" gives the required IEEE 64bit float
  102. $teststr = pack("d", 1.2345);
  103. $number = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
  104. if ($number == $teststr) {
  105. $byte_order = 0; // Little Endian
  106. } elseif ($number == strrev($teststr)) {
  107. $byte_order = 1; // Big Endian
  108. } else {
  109. // Give up. I'll fix this in a later version.
  110. throw new PHPExcel_Writer_Exception("Required floating point format not supported on this platform.");
  111. }
  112. self::$byteOrder = $byte_order;
  113. }
  114. return self::$byteOrder;
  115. }
  116. /**
  117. * General storage function
  118. *
  119. * @param string $data binary data to append
  120. * @access private
  121. */
  122. protected function append($data)
  123. {
  124. if (strlen($data) - 4 > $this->limit) {
  125. $data = $this->addContinue($data);
  126. }
  127. $this->_data .= $data;
  128. $this->_datasize += strlen($data);
  129. }
  130. /**
  131. * General storage function like append, but returns string instead of modifying $this->_data
  132. *
  133. * @param string $data binary data to write
  134. * @return string
  135. */
  136. public function writeData($data)
  137. {
  138. if (strlen($data) - 4 > $this->limit) {
  139. $data = $this->addContinue($data);
  140. }
  141. $this->_datasize += strlen($data);
  142. return $data;
  143. }
  144. /**
  145. * Writes Excel BOF record to indicate the beginning of a stream or
  146. * sub-stream in the BIFF file.
  147. *
  148. * @param integer $type Type of BIFF file to write: 0x0005 Workbook,
  149. * 0x0010 Worksheet.
  150. * @access private
  151. */
  152. protected function storeBof($type)
  153. {
  154. $record = 0x0809; // Record identifier (BIFF5-BIFF8)
  155. $length = 0x0010;
  156. // by inspection of real files, MS Office Excel 2007 writes the following
  157. $unknown = pack("VV", 0x000100D1, 0x00000406);
  158. $build = 0x0DBB; // Excel 97
  159. $year = 0x07CC; // Excel 97
  160. $version = 0x0600; // BIFF8
  161. $header = pack("vv", $record, $length);
  162. $data = pack("vvvv", $version, $type, $build, $year);
  163. $this->append($header . $data . $unknown);
  164. }
  165. /**
  166. * Writes Excel EOF record to indicate the end of a BIFF stream.
  167. *
  168. * @access private
  169. */
  170. protected function storeEof()
  171. {
  172. $record = 0x000A; // Record identifier
  173. $length = 0x0000; // Number of bytes to follow
  174. $header = pack("vv", $record, $length);
  175. $this->append($header);
  176. }
  177. /**
  178. * Writes Excel EOF record to indicate the end of a BIFF stream.
  179. *
  180. * @access private
  181. */
  182. public function writeEof()
  183. {
  184. $record = 0x000A; // Record identifier
  185. $length = 0x0000; // Number of bytes to follow
  186. $header = pack("vv", $record, $length);
  187. return $this->writeData($header);
  188. }
  189. /**
  190. * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
  191. * Excel 97 the limit is 8228 bytes. Records that are longer than these limits
  192. * must be split up into CONTINUE blocks.
  193. *
  194. * This function takes a long BIFF record and inserts CONTINUE records as
  195. * necessary.
  196. *
  197. * @param string $data The original binary data to be written
  198. * @return string A very convenient string of continue blocks
  199. * @access private
  200. */
  201. private function addContinue($data)
  202. {
  203. $limit = $this->limit;
  204. $record = 0x003C; // Record identifier
  205. // The first 2080/8224 bytes remain intact. However, we have to change
  206. // the length field of the record.
  207. $tmp = substr($data, 0, 2) . pack("v", $limit) . substr($data, 4, $limit);
  208. $header = pack("vv", $record, $limit); // Headers for continue records
  209. // Retrieve chunks of 2080/8224 bytes +4 for the header.
  210. $data_length = strlen($data);
  211. for ($i = $limit + 4; $i < ($data_length - $limit); $i += $limit) {
  212. $tmp .= $header;
  213. $tmp .= substr($data, $i, $limit);
  214. }
  215. // Retrieve the last chunk of data
  216. $header = pack("vv", $record, strlen($data) - $i);
  217. $tmp .= $header;
  218. $tmp .= substr($data, $i, strlen($data) - $i);
  219. return $tmp;
  220. }
  221. }