]> git.wh0rd.org - tt-rss.git/blame - lib/phpqrcode/qrbitstream.php
strip_harmful_tags: remove data- attributes
[tt-rss.git] / lib / phpqrcode / qrbitstream.php
CommitLineData
fb70f26e
AD
1<?php\r
2/*\r
3 * PHP QR Code encoder\r
4 *\r
5 * Bitstream class\r
6 *\r
7 * Based on libqrencode C library distributed under LGPL 2.1\r
8 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>\r
9 *\r
10 * PHP QR Code is distributed under LGPL 3\r
11 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>\r
12 *\r
13 * This library is free software; you can redistribute it and/or\r
14 * modify it under the terms of the GNU Lesser General Public\r
15 * License as published by the Free Software Foundation; either\r
16 * version 3 of the License, or any later version.\r
17 *\r
18 * This library is distributed in the hope that it will be useful,\r
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
21 * Lesser General Public License for more details.\r
22 *\r
23 * You should have received a copy of the GNU Lesser General Public\r
24 * License along with this library; if not, write to the Free Software\r
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
26 */\r
27 \r
28 class QRbitstream {\r
29 \r
30 public $data = array();\r
31 \r
32 //----------------------------------------------------------------------\r
33 public function size()\r
34 {\r
35 return count($this->data);\r
36 }\r
37 \r
38 //----------------------------------------------------------------------\r
39 public function allocate($setLength)\r
40 {\r
41 $this->data = array_fill(0, $setLength, 0);\r
42 return 0;\r
43 }\r
44 \r
45 //----------------------------------------------------------------------\r
46 public static function newFromNum($bits, $num)\r
47 {\r
48 $bstream = new QRbitstream();\r
49 $bstream->allocate($bits);\r
50 \r
51 $mask = 1 << ($bits - 1);\r
52 for($i=0; $i<$bits; $i++) {\r
53 if($num & $mask) {\r
54 $bstream->data[$i] = 1;\r
55 } else {\r
56 $bstream->data[$i] = 0;\r
57 }\r
58 $mask = $mask >> 1;\r
59 }\r
60\r
61 return $bstream;\r
62 }\r
63 \r
64 //----------------------------------------------------------------------\r
65 public static function newFromBytes($size, $data)\r
66 {\r
67 $bstream = new QRbitstream();\r
68 $bstream->allocate($size * 8);\r
69 $p=0;\r
70\r
71 for($i=0; $i<$size; $i++) {\r
72 $mask = 0x80;\r
73 for($j=0; $j<8; $j++) {\r
74 if($data[$i] & $mask) {\r
75 $bstream->data[$p] = 1;\r
76 } else {\r
77 $bstream->data[$p] = 0;\r
78 }\r
79 $p++;\r
80 $mask = $mask >> 1;\r
81 }\r
82 }\r
83\r
84 return $bstream;\r
85 }\r
86 \r
87 //----------------------------------------------------------------------\r
88 public function append(QRbitstream $arg)\r
89 {\r
90 if (is_null($arg)) {\r
91 return -1;\r
92 }\r
93 \r
94 if($arg->size() == 0) {\r
95 return 0;\r
96 }\r
97 \r
98 if($this->size() == 0) {\r
99 $this->data = $arg->data;\r
100 return 0;\r
101 }\r
102 \r
103 $this->data = array_values(array_merge($this->data, $arg->data));\r
104\r
105 return 0;\r
106 }\r
107 \r
108 //----------------------------------------------------------------------\r
109 public function appendNum($bits, $num)\r
110 {\r
111 if ($bits == 0) \r
112 return 0;\r
113\r
114 $b = QRbitstream::newFromNum($bits, $num);\r
115 \r
116 if(is_null($b))\r
117 return -1;\r
118\r
119 $ret = $this->append($b);\r
120 unset($b);\r
121\r
122 return $ret;\r
123 }\r
124\r
125 //----------------------------------------------------------------------\r
126 public function appendBytes($size, $data)\r
127 {\r
128 if ($size == 0) \r
129 return 0;\r
130\r
131 $b = QRbitstream::newFromBytes($size, $data);\r
132 \r
133 if(is_null($b))\r
134 return -1;\r
135\r
136 $ret = $this->append($b);\r
137 unset($b);\r
138\r
139 return $ret;\r
140 }\r
141 \r
142 //----------------------------------------------------------------------\r
143 public function toByte()\r
144 {\r
145 \r
146 $size = $this->size();\r
147\r
148 if($size == 0) {\r
149 return array();\r
150 }\r
151 \r
152 $data = array_fill(0, (int)(($size + 7) / 8), 0);\r
153 $bytes = (int)($size / 8);\r
154\r
155 $p = 0;\r
156 \r
157 for($i=0; $i<$bytes; $i++) {\r
158 $v = 0;\r
159 for($j=0; $j<8; $j++) {\r
160 $v = $v << 1;\r
161 $v |= $this->data[$p];\r
162 $p++;\r
163 }\r
164 $data[$i] = $v;\r
165 }\r
166 \r
167 if($size & 7) {\r
168 $v = 0;\r
169 for($j=0; $j<($size & 7); $j++) {\r
170 $v = $v << 1;\r
171 $v |= $this->data[$p];\r
172 $p++;\r
173 }\r
174 $data[$bytes] = $v;\r
175 }\r
176\r
177 return $data;\r
178 }\r
179\r
180 }\r