/
lib.rs
1477 lines (1246 loc) · 53.3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QR Code generator library (Rust, no heap)
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/qr-code-generator-library
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
//! Generates QR Codes from text strings and byte arrays.
//!
//! This project aims to be the best, clearest QR Code generator library.
//! The primary goals are flexible options and absolute correctness.
//! Secondary goals are compact implementation size and good documentation comments.
//!
//! Home page with live JavaScript demo, extensive descriptions, and competitor comparisons:
//! [https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)
//!
//! # Features
//!
//! Core features:
//!
//! - Significantly shorter code but more documentation comments compared to competing libraries
//! - Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard
//! - Output format: Raw modules/pixels of the QR symbol
//! - Detects finder-like penalty patterns more accurately than other implementations
//! - Encodes numeric and special-alphanumeric text in less space than general text
//! - Open-source code under the permissive MIT License
//!
//! Manual parameters:
//!
//! - User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data
//! - User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one
//! - User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number
//! - User can create a list of data segments manually and add ECI segments
//!
//! More information about QR Code technology and this library's design can be found on the project home page.
//!
//! # Examples
//!
//! ```
//! extern crate qrcodegen_no_heap;
//! use qrcodegen_no_heap::Mask;
//! use qrcodegen_no_heap::QrCode;
//! use qrcodegen_no_heap::QrCodeEcc;
//! use qrcodegen_no_heap::Version;
//! ```
//!
//! Text data:
//!
//! ```
//! let mut outbuffer = vec![0u8; Version::MAX.buffer_len()];
//! let mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];
//! let qr = QrCode::encode_text("Hello, world!", &mut tempbuffer, &mut outbuffer,
//! QrCodeEcc::Medium, Version::MIN, Version:MAX, None, true).unwrap();
//! let svg = to_svg_string(&qr, 4); // See qrcodegen-demo
//! ```
//!
//! Binary data:
//!
//! ```
//! let mut outbuffer = vec![0u8; Version::MAX.buffer_len()];
//! let mut dataandtemp = vec![0u8; Version::MAX.buffer_len()];
//! dataandtemp[0] = 0xE3;
//! dataandtemp[1] = 0x81;
//! dataandtemp[2] = 0x82;
//! let qr = QrCode::encode_binary(&mut dataandtemp, 3, &mut outbuffer, QrCodeEcc::High,
//! Version::new(2), Version::new(7), Some(Mask::new(4)), false).unwrap();
//! for y in 0 .. qr.size() {
//! for x in 0 .. qr.size() {
//! (... paint qr.get_module(x, y) ...)
//! }
//! }
//! ```
#![no_std]
#![forbid(unsafe_code)]
use core::convert::TryFrom;
/*---- QrCode functionality ----*/
/// A QR Code symbol, which is a type of two-dimension barcode.
///
/// Invented by Denso Wave and described in the ISO/IEC 18004 standard.
///
/// Instances of this struct represent an immutable square grid of dark and light cells.
/// The impl provides static factory functions to create a QR Code from text or binary data.
/// The struct and impl cover the QR Code Model 2 specification, supporting all versions
/// (sizes) from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
///
/// Ways to create a QR Code object:
///
/// - High level: Take the payload data and call `QrCode::encode_text()` or `QrCode::encode_binary()`.
/// - Mid level: Custom-make the list of segments and call
/// `QrCode::encode_segments_to_codewords()` and then `QrCode::encode_codewords()`.
/// - Low level: Custom-make the array of data codeword bytes (including segment
/// headers and final padding, excluding error correction codewords), supply the
/// appropriate version number, and call the `QrCode::encode_codewords()` constructor.
///
/// (Note that all ways require supplying the desired error correction level and various byte buffers.)
pub struct QrCode<'a> {
// The width and height of this QR Code, measured in modules, between
// 21 and 177 (inclusive). This is equal to version * 4 + 17.
size: &'a mut u8,
// The modules of this QR Code (0 = light, 1 = dark), packed bitwise into bytes.
// Immutable after constructor finishes. Accessed through get_module().
modules: &'a mut [u8],
}
impl<'a> QrCode<'a> {
/*---- Static factory functions (high level) ----*/
/// Encodes the given text string to a QR Code, returning a wrapped `QrCode` if successful.
/// If the data is too long to fit in any version in the given range
/// at the given ECC level, then `Err` is returned.
///
/// The smallest possible QR Code version within the given range is automatically
/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result
/// may be higher than the ecl argument if it can be done without increasing the
/// version. The mask number is either between 0 to 7 (inclusive) to force that
/// mask, or `None` to automatically choose an appropriate mask (which may be slow).
///
/// About the slices, letting len = maxversion.buffer_len():
/// - Before calling the function:
/// - The slices tempbuffer and outbuffer each must have a length of at least len.
/// - If a slice is longer than len, then the function will not
/// read from or write to the suffix array[len .. array.len()].
/// - The initial values of both slices can be arbitrary
/// because the function always writes before reading.
/// - After the function returns, both slices have no guarantee on what values are stored.
///
/// If successful, the resulting QR Code may use numeric,
/// alphanumeric, or byte mode to encode the text.
///
/// In the most optimistic case, a QR Code at version 40 with low ECC
/// can hold any UTF-8 string up to 2953 bytes, or any alphanumeric string
/// up to 4296 characters, or any digit string up to 7089 characters.
/// These numbers represent the hard upper limit of the QR Code standard.
///
/// Please consult the QR Code specification for information on
/// data capacities per version, ECC level, and text encoding mode.
pub fn encode_text<'b>(text: &str, tempbuffer: &'b mut [u8], mut outbuffer: &'a mut [u8], ecl: QrCodeEcc,
minversion: Version, maxversion: Version, mask: Option<Mask>, boostecl: bool) -> Result<QrCode<'a>,DataTooLong> {
let minlen: usize = outbuffer.len().min(tempbuffer.len());
outbuffer = &mut outbuffer[ .. minlen];
let textlen: usize = text.len(); // In bytes
if textlen == 0 {
let (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(&[], outbuffer, ecl, minversion, maxversion, boostecl)?;
return Ok(Self::encode_codewords(outbuffer, datacodewordslen, tempbuffer, ecl, version, mask));
}
use QrSegmentMode::*;
let buflen: usize = outbuffer.len();
let seg: QrSegment = if QrSegment::is_numeric(text) && QrSegment::calc_buffer_size(Numeric, textlen).map_or(false, |x| x <= buflen) {
QrSegment::make_numeric(text, tempbuffer)
} else if QrSegment::is_alphanumeric(text) && QrSegment::calc_buffer_size(Alphanumeric, textlen).map_or(false, |x| x <= buflen) {
QrSegment::make_alphanumeric(text, tempbuffer)
} else if QrSegment::calc_buffer_size(Byte, textlen).map_or(false, |x| x <= buflen) {
QrSegment::make_bytes(text.as_bytes())
} else {
return Err(DataTooLong::SegmentTooLong);
};
let (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(&[seg], outbuffer, ecl, minversion, maxversion, boostecl)?;
Ok(Self::encode_codewords(outbuffer, datacodewordslen, tempbuffer, ecl, version, mask))
}
/// Encodes the given binary data to a QR Code, returning a wrapped `QrCode` if successful.
/// If the data is too long to fit in any version in the given range
/// at the given ECC level, then `Err` is returned.
///
/// The smallest possible QR Code version within the given range is automatically
/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result
/// may be higher than the ecl argument if it can be done without increasing the
/// version. The mask number is either between 0 to 7 (inclusive) to force that
/// mask, or `None` to automatically choose an appropriate mask (which may be slow).
///
/// About the slices, letting len = maxversion.buffer_len():
/// - Before calling the function:
/// - The slices dataandtempbuffer and outbuffer each must have a length of at least len.
/// - If a slice is longer than len, then the function will not
/// read from or write to the suffix array[len .. array.len()].
/// - The input slice range dataandtempbuffer[0 .. datalen] should normally be
/// valid UTF-8 text, but is not required by the QR Code standard.
/// - The initial values of dataandtempbuffer[datalen .. len] and outbuffer[0 .. len]
/// can be arbitrary because the function always writes before reading.
/// - After the function returns, both slices have no guarantee on what values are stored.
///
/// If successful, the resulting QR Code will use byte mode to encode the data.
///
/// In the most optimistic case, a QR Code at version 40 with low ECC can hold any byte
/// sequence up to length 2953. This is the hard upper limit of the QR Code standard.
///
/// Please consult the QR Code specification for information on
/// data capacities per version, ECC level, and text encoding mode.
pub fn encode_binary<'b>(dataandtempbuffer: &'b mut [u8], datalen: usize, mut outbuffer: &'a mut [u8], ecl: QrCodeEcc,
minversion: Version, maxversion: Version, mask: Option<Mask>, boostecl: bool) -> Result<QrCode<'a>,DataTooLong> {
assert!(datalen <= dataandtempbuffer.len(), "Invalid data length");
let minlen: usize = outbuffer.len().min(dataandtempbuffer.len());
outbuffer = &mut outbuffer[ .. minlen];
if QrSegment::calc_buffer_size(QrSegmentMode::Byte, datalen).map_or(true, |x| x > outbuffer.len()) {
return Err(DataTooLong::SegmentTooLong);
}
let seg: QrSegment = QrSegment::make_bytes(&dataandtempbuffer[ .. datalen]);
let (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(&[seg], outbuffer, ecl, minversion, maxversion, boostecl)?;
Ok(Self::encode_codewords(outbuffer, datacodewordslen, dataandtempbuffer, ecl, version, mask))
}
/*---- Static factory functions (mid level) ----*/
/// Returns an intermediate state representing the given segments
/// with the given encoding parameters being encoded into codewords.
///
/// The smallest possible QR Code version within the given range is automatically
/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result
/// may be higher than the ecl argument if it can be done without increasing the
/// version. The mask number is either between 0 to 7 (inclusive) to force that
/// mask, or `None` to automatically choose an appropriate mask (which may be slow).
///
/// This function exists to allow segments to use parts of a temporary buffer,
/// then have the segments be encoded to an output buffer, then invalidate all the segments,
/// and finally have the output buffer and temporary buffer be encoded to a QR Code.
pub fn encode_segments_to_codewords(segs: &[QrSegment], outbuffer: &'a mut [u8],
mut ecl: QrCodeEcc, minversion: Version, maxversion: Version, boostecl: bool)
-> Result<(usize,QrCodeEcc,Version),DataTooLong> {
assert!(minversion <= maxversion, "Invalid value");
assert!(outbuffer.len() >= QrCode::get_num_data_codewords(maxversion, ecl), "Invalid buffer length");
// Find the minimal version number to use
let mut version: Version = minversion;
let datausedbits: usize = loop {
let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8; // Number of data bits available
let dataused: Option<usize> = QrSegment::get_total_bits(segs, version);
if dataused.map_or(false, |n| n <= datacapacitybits) {
break dataused.unwrap(); // This version number is found to be suitable
} else if version >= maxversion { // All versions in the range could not fit the given data
return Err(match dataused {
None => DataTooLong::SegmentTooLong,
Some(n) => DataTooLong::DataOverCapacity(n, datacapacitybits),
});
} else {
version = Version::new(version.value() + 1);
}
};
// Increase the error correction level while the data still fits in the current version number
for &newecl in &[QrCodeEcc::Medium, QrCodeEcc::Quartile, QrCodeEcc::High] { // From low to high
if boostecl && datausedbits <= QrCode::get_num_data_codewords(version, newecl) * 8 {
ecl = newecl;
}
}
// Concatenate all segments to create the data bit string
let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;
let mut bb = BitBuffer::new(&mut outbuffer[ .. datacapacitybits/8]);
for seg in segs {
bb.append_bits(seg.mode.mode_bits(), 4);
bb.append_bits(u32::try_from(seg.numchars).unwrap(), seg.mode.num_char_count_bits(version));
for i in 0 .. seg.bitlength {
let bit: u8 = (seg.data[i >> 3] >> (7 - (i & 7))) & 1;
bb.append_bits(bit.into(), 1);
}
}
debug_assert_eq!(bb.length, datausedbits);
// Add terminator and pad up to a byte if applicable
let numzerobits: usize = core::cmp::min(4, datacapacitybits - bb.length);
bb.append_bits(0, u8::try_from(numzerobits).unwrap());
let numzerobits: usize = bb.length.wrapping_neg() & 7;
bb.append_bits(0, u8::try_from(numzerobits).unwrap());
debug_assert_eq!(bb.length % 8, 0);
// Pad with alternating bytes until data capacity is reached
for &padbyte in [0xEC, 0x11].iter().cycle() {
if bb.length >= datacapacitybits {
break;
}
bb.append_bits(padbyte, 8);
}
Ok((bb.length / 8, ecl, version))
}
/*---- Constructor (low level) ----*/
/// Creates a new QR Code with the given version number,
/// error correction level, data codeword bytes, and mask number.
///
/// This is a low-level API that most users should not use directly.
/// A mid-level API is the `encode_segments_to_codewords()` function.
pub fn encode_codewords<'b>(mut datacodewordsandoutbuffer: &'a mut [u8], datacodewordslen: usize, mut tempbuffer: &'b mut [u8],
ecl: QrCodeEcc, version: Version, mut msk: Option<Mask>) -> QrCode<'a> {
datacodewordsandoutbuffer = &mut datacodewordsandoutbuffer[ .. version.buffer_len()];
tempbuffer = &mut tempbuffer [ .. version.buffer_len()];
// Compute ECC
let rawcodewords: usize = QrCode::get_num_raw_data_modules(version) / 8;
assert!(datacodewordslen <= rawcodewords);
let (data, temp) = datacodewordsandoutbuffer.split_at_mut(datacodewordslen);
let allcodewords = Self::add_ecc_and_interleave(data, version, ecl, temp, tempbuffer);
// Draw modules
let mut result: QrCode = QrCode::<'a>::function_modules_marked(datacodewordsandoutbuffer, version);
result.draw_codewords(allcodewords);
result.draw_light_function_modules();
let funcmods: QrCode = QrCode::<'b>::function_modules_marked(tempbuffer, version); // Just a grid, not a real QR Code
// Do masking
if msk.is_none() { // Automatically choose best mask
let mut minpenalty = core::i32::MAX;
for i in 0u8 .. 8 {
let i = Mask::new(i);
result.apply_mask(&funcmods, i);
result.draw_format_bits(ecl, i);
let penalty: i32 = result.get_penalty_score();
if penalty < minpenalty {
msk = Some(i);
minpenalty = penalty;
}
result.apply_mask(&funcmods, i); // Undoes the mask due to XOR
}
}
let msk: Mask = msk.unwrap();
result.apply_mask(&funcmods, msk); // Apply the final choice of mask
result.draw_format_bits(ecl, msk); // Overwrite old format bits
result
}
/*---- Public methods ----*/
/// Returns this QR Code's version, in the range [1, 40].
pub fn version(&self) -> Version {
Version::new((*self.size - 17) / 4)
}
/// Returns this QR Code's size, in the range [21, 177].
pub fn size(&self) -> i32 {
i32::from(*self.size)
}
/// Returns this QR Code's error correction level.
pub fn error_correction_level(&self) -> QrCodeEcc {
let index =
usize::from(self.get_module_bounded(0, 8)) << 1 |
usize::from(self.get_module_bounded(1, 8)) << 0;
use QrCodeEcc::*;
[Medium, Low, High, Quartile][index]
}
/// Returns this QR Code's mask, in the range [0, 7].
pub fn mask(&self) -> Mask {
Mask::new(
u8::from(self.get_module_bounded(2, 8)) << 2 |
u8::from(self.get_module_bounded(3, 8)) << 1 |
u8::from(self.get_module_bounded(4, 8)) << 0)
}
/// Returns the color of the module (pixel) at the given coordinates,
/// which is `false` for light or `true` for dark.
///
/// The top left corner has the coordinates (x=0, y=0). If the given
/// coordinates are out of bounds, then `false` (light) is returned.
pub fn get_module(&self, x: i32, y: i32) -> bool {
let range = 0 .. self.size();
range.contains(&x) && range.contains(&y) && self.get_module_bounded(x as u8, y as u8)
}
// Returns the color of the module at the given coordinates, which must be in bounds.
fn get_module_bounded(&self, x: u8, y: u8) -> bool {
let range = 0 .. *self.size;
assert!(range.contains(&x) && range.contains(&y));
let index = usize::from(y) * usize::from(*self.size) + usize::from(x);
let byteindex: usize = index >> 3;
let bitindex: usize = index & 7;
get_bit(self.modules[byteindex].into(), bitindex as u8)
}
// Sets the color of the module at the given coordinates, doing nothing if out of bounds.
fn set_module_unbounded(&mut self, x: i32, y: i32, isdark: bool) {
let range = 0 .. self.size();
if range.contains(&x) && range.contains(&y) {
self.set_module_bounded(x as u8, y as u8, isdark);
}
}
// Sets the color of the module at the given coordinates, which must be in bounds.
fn set_module_bounded(&mut self, x: u8, y: u8, isdark: bool) {
let range = 0 .. *self.size;
assert!(range.contains(&x) && range.contains(&y));
let index = usize::from(y) * usize::from(*self.size) + usize::from(x);
let byteindex: usize = index >> 3;
let bitindex: usize = index & 7;
if isdark {
self.modules[byteindex] |= 1u8 << bitindex;
} else {
self.modules[byteindex] &= !(1u8 << bitindex);
}
}
/*---- Error correction code generation ----*/
// Appends error correction bytes to each block of the given data array, then interleaves
// bytes from the blocks, stores them in the output array, and returns a slice of resultbuf.
// temp is used as a temporary work area and will be clobbered by this function.
fn add_ecc_and_interleave<'b>(data: &[u8], ver: Version, ecl: QrCodeEcc, temp: &mut [u8], resultbuf: &'b mut [u8]) -> &'b [u8] {
assert_eq!(data.len(), QrCode::get_num_data_codewords(ver, ecl));
// Calculate parameter numbers
let numblocks: usize = QrCode::table_get(&NUM_ERROR_CORRECTION_BLOCKS, ver, ecl);
let blockecclen: usize = QrCode::table_get(&ECC_CODEWORDS_PER_BLOCK , ver, ecl);
let rawcodewords: usize = QrCode::get_num_raw_data_modules(ver) / 8;
let numshortblocks: usize = numblocks - rawcodewords % numblocks;
let shortblockdatalen: usize = rawcodewords / numblocks - blockecclen;
let result = &mut resultbuf[ .. rawcodewords];
// Split data into blocks, calculate ECC, and interleave
// (not concatenate) the bytes into a single sequence
let rs = ReedSolomonGenerator::new(blockecclen);
let mut dat: &[u8] = data;
let ecc: &mut [u8] = &mut temp[ .. blockecclen]; // Temporary storage
for i in 0 .. numblocks {
let datlen: usize = shortblockdatalen + usize::from(i >= numshortblocks);
rs.compute_remainder(&dat[ .. datlen], ecc);
let mut k: usize = i;
for j in 0 .. datlen { // Copy data
if j == shortblockdatalen {
k -= numshortblocks;
}
result[k] = dat[j];
k += numblocks;
}
let mut k: usize = data.len() + i;
for j in 0 .. blockecclen { // Copy ECC
result[k] = ecc[j];
k += numblocks;
}
dat = &dat[datlen .. ];
}
debug_assert_eq!(dat.len(), 0);
result
}
/*---- Drawing function modules ----*/
// Creates a QR Code grid with light modules for the given
// version's size, then marks every function module as dark.
fn function_modules_marked(outbuffer: &'a mut [u8], ver: Version) -> Self {
assert_eq!(outbuffer.len(), ver.buffer_len());
let parts: (&mut u8, &mut [u8]) = outbuffer.split_first_mut().unwrap();
let mut result = Self {
size: parts.0,
modules: parts.1,
};
let size: u8 = ver.value() * 4 + 17;
*result.size = size;
result.modules.fill(0);
// Fill horizontal and vertical timing patterns
result.fill_rectangle(6, 0, 1, size);
result.fill_rectangle(0, 6, size, 1);
// Fill 3 finder patterns (all corners except bottom right) and format bits
result.fill_rectangle(0, 0, 9, 9);
result.fill_rectangle(size - 8, 0, 8, 9);
result.fill_rectangle(0, size - 8, 9, 8);
// Fill numerous alignment patterns
let mut alignpatposbuf = [0u8; 7];
let alignpatpos: &[u8] = result.get_alignment_pattern_positions(&mut alignpatposbuf);
for (i, pos0) in alignpatpos.iter().enumerate() {
for (j, pos1) in alignpatpos.iter().enumerate() {
// Don't draw on the three finder corners
if !((i == 0 && j == 0) || (i == 0 && j == alignpatpos.len() - 1) || (i == alignpatpos.len() - 1 && j == 0)) {
result.fill_rectangle(pos0 - 2, pos1 - 2, 5, 5);
}
}
}
// Fill version blocks
if ver.value() >= 7 {
result.fill_rectangle(size - 11, 0, 3, 6);
result.fill_rectangle(0, size - 11, 6, 3);
}
result
}
// Draws light function modules and possibly some dark modules onto this QR Code, without changing
// non-function modules. This does not draw the format bits. This requires all function modules to be previously
// marked dark (namely by function_modules_marked()), because this may skip redrawing dark function modules.
fn draw_light_function_modules(&mut self) {
// Draw horizontal and vertical timing patterns
let size: u8 = *self.size;
for i in (7 .. size-7).step_by(2) {
self.set_module_bounded(6, i, false);
self.set_module_bounded(i, 6, false);
}
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
for dy in -4i32 ..= 4 {
for dx in -4i32 ..= 4 {
let dist: i32 = dx.abs().max(dy.abs());
if dist == 2 || dist == 4 {
self.set_module_unbounded(3 + dx, 3 + dy, false);
self.set_module_unbounded(i32::from(size) - 4 + dx, 3 + dy, false);
self.set_module_unbounded(3 + dx, i32::from(size) - 4 + dy, false);
}
}
}
// Draw numerous alignment patterns
let mut alignpatposbuf = [0u8; 7];
let alignpatpos: &[u8] = self.get_alignment_pattern_positions(&mut alignpatposbuf);
for (i, &pos0) in alignpatpos.iter().enumerate() {
for (j, &pos1) in alignpatpos.iter().enumerate() {
if (i == 0 && j == 0) || (i == 0 && j == alignpatpos.len() - 1) || (i == alignpatpos.len() - 1 && j == 0) {
continue; // Don't draw on the three finder corners
}
for dy in -1 ..= 1 {
for dx in -1 ..= 1 {
self.set_module_bounded((i32::from(pos0) + dx) as u8, (i32::from(pos1) + dy) as u8, dx == 0 && dy == 0);
}
}
}
}
// Draw version blocks
let ver = u32::from(self.version().value()); // uint6, in the range [7, 40]
if ver >= 7 {
// Calculate error correction code and pack bits
let bits: u32 = {
let mut rem: u32 = ver;
for _ in 0 .. 12 {
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
}
ver << 12 | rem // uint18
};
debug_assert_eq!(bits >> 18, 0);
// Draw two copies
for i in 0u8 .. 18 {
let bit: bool = get_bit(bits, i);
let a: u8 = size - 11 + i % 3;
let b: u8 = i / 3;
self.set_module_bounded(a, b, bit);
self.set_module_bounded(b, a, bit);
}
}
}
// Draws two copies of the format bits (with its own error correction code) based
// on the given mask and error correction level. This always draws all modules of
// the format bits, unlike draw_light_function_modules() which might skip dark modules.
fn draw_format_bits(&mut self, ecl: QrCodeEcc, mask: Mask) {
// Calculate error correction code and pack bits
let bits: u32 = {
// errcorrlvl is uint2, mask is uint3
let data = u32::from(ecl.format_bits() << 3 | mask.value());
let mut rem: u32 = data;
for _ in 0 .. 10 {
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
}
(data << 10 | rem) ^ 0x5412 // uint15
};
debug_assert_eq!(bits >> 15, 0);
// Draw first copy
for i in 0 .. 6 {
self.set_module_bounded(8, i, get_bit(bits, i));
}
self.set_module_bounded(8, 7, get_bit(bits, 6));
self.set_module_bounded(8, 8, get_bit(bits, 7));
self.set_module_bounded(7, 8, get_bit(bits, 8));
for i in 9 .. 15 {
self.set_module_bounded(14 - i, 8, get_bit(bits, i));
}
// Draw second copy
let size: u8 = *self.size;
for i in 0 .. 8 {
self.set_module_bounded(size - 1 - i, 8, get_bit(bits, i));
}
for i in 8 .. 15 {
self.set_module_bounded(8, size - 15 + i, get_bit(bits, i));
}
self.set_module_bounded(8, size - 8, true); // Always dark
}
// Sets every module in the range [left : left + width] * [top : top + height] to dark.
fn fill_rectangle(&mut self, left: u8, top: u8, width: u8, height: u8) {
for dy in 0 .. height {
for dx in 0 .. width {
self.set_module_bounded(left + dx, top + dy, true);
}
}
}
/*---- Drawing data modules and masking ----*/
// Draws the raw codewords (including data and ECC) onto this QR Code. This requires the initial state of
// the QR Code to be dark at function modules and light at codeword modules (including unused remainder bits).
fn draw_codewords(&mut self, data: &[u8]) {
assert_eq!(data.len(), QrCode::get_num_raw_data_modules(self.version()) / 8, "Illegal argument");
let size: i32 = self.size();
let mut i: usize = 0; // Bit index into the data
// Do the funny zigzag scan
let mut right: i32 = size - 1;
while right >= 1 { // Index of right column in each column pair
if right == 6 {
right = 5;
}
for vert in 0 .. size { // Vertical counter
for j in 0 .. 2 {
let x = (right - j) as u8; // Actual x coordinate
let upward: bool = (right + 1) & 2 == 0;
let y = (if upward { size - 1 - vert } else { vert }) as u8; // Actual y coordinate
if !self.get_module_bounded(x, y) && i < data.len() * 8 {
self.set_module_bounded(x, y, get_bit(data[i >> 3].into(), 7 - ((i as u8) & 7)));
i += 1;
}
// If this QR Code has any remainder bits (0 to 7), they were assigned as
// 0/false/light by the constructor and are left unchanged by this method
}
}
right -= 2;
}
debug_assert_eq!(i, data.len() * 8);
}
// XORs the codeword modules in this QR Code with the given mask pattern
// and given pattern of function modules. The codeword bits must be drawn
// before masking. Due to the arithmetic of XOR, calling apply_mask() with
// the same mask value a second time will undo the mask. A final well-formed
// QR Code needs exactly one (not zero, two, etc.) mask applied.
fn apply_mask(&mut self, functionmodules: &QrCode, mask: Mask) {
for y in 0 .. *self.size {
for x in 0 .. *self.size {
if functionmodules.get_module_bounded(x, y) {
continue;
}
let invert: bool = {
let x = i32::from(x);
let y = i32::from(y);
match mask.value() {
0 => (x + y) % 2 == 0,
1 => y % 2 == 0,
2 => x % 3 == 0,
3 => (x + y) % 3 == 0,
4 => (x / 3 + y / 2) % 2 == 0,
5 => x * y % 2 + x * y % 3 == 0,
6 => (x * y % 2 + x * y % 3) % 2 == 0,
7 => ((x + y) % 2 + x * y % 3) % 2 == 0,
_ => unreachable!(),
}
};
self.set_module_bounded(x, y,
self.get_module_bounded(x, y) ^ invert);
}
}
}
// Calculates and returns the penalty score based on state of this QR Code's current modules.
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
fn get_penalty_score(&self) -> i32 {
let mut result: i32 = 0;
let size: u8 = *self.size;
// Adjacent modules in row having same color, and finder-like patterns
for y in 0 .. size {
let mut runcolor = false;
let mut runx: i32 = 0;
let mut runhistory = FinderPenalty::new(size);
for x in 0 .. size {
if self.get_module_bounded(x, y) == runcolor {
runx += 1;
if runx == 5 {
result += PENALTY_N1;
} else if runx > 5 {
result += 1;
}
} else {
runhistory.add_history(runx);
if !runcolor {
result += runhistory.count_patterns() * PENALTY_N3;
}
runcolor = self.get_module_bounded(x, y);
runx = 1;
}
}
result += runhistory.terminate_and_count(runcolor, runx) * PENALTY_N3;
}
// Adjacent modules in column having same color, and finder-like patterns
for x in 0 .. size {
let mut runcolor = false;
let mut runy: i32 = 0;
let mut runhistory = FinderPenalty::new(size);
for y in 0 .. size {
if self.get_module_bounded(x, y) == runcolor {
runy += 1;
if runy == 5 {
result += PENALTY_N1;
} else if runy > 5 {
result += 1;
}
} else {
runhistory.add_history(runy);
if !runcolor {
result += runhistory.count_patterns() * PENALTY_N3;
}
runcolor = self.get_module_bounded(x, y);
runy = 1;
}
}
result += runhistory.terminate_and_count(runcolor, runy) * PENALTY_N3;
}
// 2*2 blocks of modules having same color
for y in 0 .. size-1 {
for x in 0 .. size-1 {
let color: bool = self.get_module_bounded(x, y);
if color == self.get_module_bounded(x + 1, y) &&
color == self.get_module_bounded(x, y + 1) &&
color == self.get_module_bounded(x + 1, y + 1) {
result += PENALTY_N2;
}
}
}
// Balance of dark and light modules
let dark = self.modules.iter().map(|x| x.count_ones()).sum::<u32>() as i32;
let total = i32::from(size) * i32::from(size); // Note that size is odd, so dark/total != 1/2
// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%
let k: i32 = ((dark * 20 - total * 10).abs() + total - 1) / total - 1;
debug_assert!(0 <= k && k <= 9);
result += k * PENALTY_N4;
debug_assert!(0 <= result && result <= 2568888); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
result
}
/*---- Private helper functions ----*/
// Calculates and stores an ascending list of positions of alignment patterns
// for this version number, returning a slice of resultbuf.
// Each position is in the range [0,177), and are used on both the x and y axes.
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
fn get_alignment_pattern_positions<'b>(&self, resultbuf: &'b mut [u8; 7]) -> &'b [u8] {
let ver: u8 = self.version().value();
if ver == 1 {
&resultbuf[ .. 0]
} else {
let numalign: u8 = ver / 7 + 2;
let step: u8 = if ver == 32 { 26 } else
{(ver * 4 + numalign * 2 + 1) / (numalign * 2 - 2) * 2};
let result = &mut resultbuf[ .. usize::from(numalign)];
for i in 0 .. numalign-1 {
result[usize::from(i)] = *self.size - 7 - i * step;
}
*result.last_mut().unwrap() = 6;
result.reverse();
result
}
}
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
fn get_num_raw_data_modules(ver: Version) -> usize {
let ver = usize::from(ver.value());
let mut result: usize = (16 * ver + 128) * ver + 64;
if ver >= 2 {
let numalign: usize = ver / 7 + 2;
result -= (25 * numalign - 10) * numalign - 55;
if ver >= 7 {
result -= 36;
}
}
debug_assert!((208 ..= 29648).contains(&result));
result
}
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
// QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
fn get_num_data_codewords(ver: Version, ecl: QrCodeEcc) -> usize {
QrCode::get_num_raw_data_modules(ver) / 8
- QrCode::table_get(&ECC_CODEWORDS_PER_BLOCK , ver, ecl)
* QrCode::table_get(&NUM_ERROR_CORRECTION_BLOCKS, ver, ecl)
}
// Returns an entry from the given table based on the given values.
fn table_get(table: &'static [[i8; 41]; 4], ver: Version, ecl: QrCodeEcc) -> usize {
table[ecl.ordinal()][usize::from(ver.value())] as usize
}
}
impl PartialEq for QrCode<'_> {
fn eq(&self, other: &QrCode<'_>) -> bool{
*self.size == *other.size &&
*self.modules == *other.modules
}
}
impl Eq for QrCode<'_> {}
/*---- Helper struct for add_ecc_and_interleave() ----*/
struct ReedSolomonGenerator {
// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].
divisor: [u8; 30],
// The degree of the divisor polynomial, in the range [1, 30].
degree: usize,
}
impl ReedSolomonGenerator {
// Creates a Reed-Solomon ECC generator polynomial for the given degree. This could be
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
fn new(degree: usize) -> Self {
let mut result = Self {
divisor: [0u8; 30],
degree: degree,
};
assert!((1 ..= result.divisor.len()).contains(°ree), "Degree out of range");
let divisor: &mut [u8] = &mut result.divisor[ .. degree];
divisor[degree - 1] = 1; // Start off with the monomial x^0
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
// and drop the highest monomial term which is always 1x^degree.
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
let mut root: u8 = 1;
for _ in 0 .. degree { // Unused variable i
// Multiply the current product by (x - r^i)
for j in 0 .. degree {
divisor[j] = Self::multiply(divisor[j], root);
if j + 1 < divisor.len() {
divisor[j] ^= divisor[j + 1];
}
}
root = Self::multiply(root, 0x02);
}
result
}
// Returns the Reed-Solomon error correction codeword for the given data polynomial and this divisor polynomial.
fn compute_remainder(&self, data: &[u8], result: &mut [u8]) {
assert_eq!(result.len(), self.degree);
result.fill(0);
for b in data { // Polynomial division
let factor: u8 = b ^ result[0];
result.copy_within(1 .. , 0);
result[result.len() - 1] = 0;
for (x, &y) in result.iter_mut().zip(self.divisor.iter()) {
*x ^= Self::multiply(y, factor);
}
}
}
// Returns the product of the two given field elements modulo GF(2^8/0x11D).
// All inputs are valid. This could be implemented as a 256*256 lookup table.
fn multiply(x: u8, y: u8) -> u8 {
// Russian peasant multiplication
let mut z: u8 = 0;
for i in (0 .. 8).rev() {
z = (z << 1) ^ ((z >> 7) * 0x1D);
z ^= ((y >> i) & 1) * x;
}
z
}
}
/*---- Helper struct for get_penalty_score() ----*/
struct FinderPenalty {
qr_size: i32,
run_history: [i32; 7],
}
impl FinderPenalty {
pub fn new(size: u8) -> Self {
Self {
qr_size: i32::from(size),
run_history: [0; 7],
}
}
// Pushes the given value to the front and drops the last value.
pub fn add_history(&mut self, mut currentrunlength: i32) {
if self.run_history[0] == 0 {
currentrunlength += self.qr_size; // Add light border to initial run
}
let len: usize = self.run_history.len();
self.run_history.copy_within(0 .. len-1, 1);
self.run_history[0] = currentrunlength;
}
// Can only be called immediately after a light run is added, and returns either 0, 1, or 2.
pub fn count_patterns(&self) -> i32 {
let rh = &self.run_history;
let n = rh[1];
debug_assert!(n <= self.qr_size * 3);
let core = n > 0 && rh[2] == n && rh[3] == n * 3 && rh[4] == n && rh[5] == n;
#[allow(unused_parens)]
( i32::from(core && rh[0] >= n * 4 && rh[6] >= n)
+ i32::from(core && rh[6] >= n * 4 && rh[0] >= n))
}
// Must be called at the end of a line (row or column) of modules.
pub fn terminate_and_count(mut self, currentruncolor: bool, mut currentrunlength: i32) -> i32 {
if currentruncolor { // Terminate dark run
self.add_history(currentrunlength);
currentrunlength = 0;
}
currentrunlength += self.qr_size; // Add light border to final run
self.add_history(currentrunlength);
self.count_patterns()
}
}
/*---- Constants and tables ----*/
// For use in get_penalty_score(), when evaluating which mask is best.
const PENALTY_N1: i32 = 3;
const PENALTY_N2: i32 = 3;
const PENALTY_N3: i32 = 40;
const PENALTY_N4: i32 = 10;
static ECC_CODEWORDS_PER_BLOCK: [[i8; 41]; 4] = [
// Version: (note that index 0 is for padding, and is set to an illegal value)
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
[-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], // Low