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