| 1 | //! Contains all functions required to encode any string as a `QRCode` |
| 2 | |
| 3 | #![deny(unsafe_code)] |
| 4 | #![warn(missing_docs)] |
| 5 | |
| 6 | use crate::compact::CompactQR; |
| 7 | use crate::ecl::ECL; |
| 8 | use crate::hardcode; |
| 9 | use crate::version::Version; |
| 10 | |
| 11 | /// Enum for the 3 encoding mode |
| 12 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 13 | pub enum Mode { |
| 14 | /// Numeric mode (0-9 only) |
| 15 | Numeric, |
| 16 | /// Alphanumeric mode (0-9, A-Z, $%*./:+-?.= [space]) |
| 17 | Alphanumeric, |
| 18 | /// Byte mode (any) |
| 19 | Byte, |
| 20 | } |
| 21 | |
| 22 | /// Encodes the string according the mode and version |
| 23 | pub fn encode(input: &[u8], ecl: ECL, mode: Mode, version: Version) -> CompactQR { |
| 24 | let cci_bits = hardcode::cci_bits(version, mode); |
| 25 | |
| 26 | let mut compact = CompactQR::from_version(version); |
| 27 | |
| 28 | match mode { |
| 29 | Mode::Numeric => encode_numeric(&mut compact, input, cci_bits), |
| 30 | Mode::Alphanumeric => encode_alphanumeric(&mut compact, input, cci_bits), |
| 31 | Mode::Byte => encode_byte(&mut compact, input, cci_bits), |
| 32 | }; |
| 33 | |
| 34 | let data_bits = hardcode::data_bits(version, ecl); |
| 35 | |
| 36 | add_terminator(&mut compact, data_bits); |
| 37 | pad_to_8(&mut compact); |
| 38 | compact.fill(); |
| 39 | |
| 40 | compact |
| 41 | } |
| 42 | |
| 43 | /// Find the best encoding (Numeric -> Alnum -> Byte) |
| 44 | pub fn best_encoding(input: &[u8]) -> Mode { |
| 45 | fn try_encode_numeric(input: &[u8], i: usize) -> Mode { |
| 46 | for &c in input.iter().skip(i) { |
| 47 | if !c.is_ascii_digit() { |
| 48 | return try_encode_alphanumeric(input, i); |
| 49 | } |
| 50 | } |
| 51 | Mode::Numeric |
| 52 | } |
| 53 | |
| 54 | fn try_encode_alphanumeric(input: &[u8], i: usize) -> Mode { |
| 55 | for &c in input.iter().skip(i) { |
| 56 | if !is_qr_alphanumeric(c) { |
| 57 | return Mode::Byte; |
| 58 | } |
| 59 | } |
| 60 | Mode::Alphanumeric |
| 61 | } |
| 62 | |
| 63 | try_encode_numeric(input, 0) |
| 64 | } |
| 65 | |
| 66 | /// Encodes numeric strings (i.e. "123456789"), referring to 8.4.2 of the spec. |
| 67 | pub(crate) fn encode_numeric(compact: &mut CompactQR, input: &[u8], cci_bits: usize) { |
| 68 | #[derive(Clone, Copy)] |
| 69 | enum NumericEncoding { |
| 70 | Single, |
| 71 | Double, |
| 72 | Triple, |
| 73 | } |
| 74 | |
| 75 | fn encode_number(compact: &mut CompactQR, number: usize, encoding: NumericEncoding) { |
| 76 | match encoding { |
| 77 | NumericEncoding::Single => compact.push_bits(number, 4), |
| 78 | NumericEncoding::Double => compact.push_bits(number, 7), |
| 79 | NumericEncoding::Triple => compact.push_bits(number, 10), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | compact.push_bits(0b0001, 4); |
| 84 | compact.push_bits(input.len(), cci_bits); |
| 85 | |
| 86 | let mut i = 0; |
| 87 | let len = input.len() - input.len() % 3; |
| 88 | |
| 89 | while i < len { |
| 90 | let number = ascii_to_digit(input[i]) * 100 |
| 91 | + ascii_to_digit(input[i + 1]) * 10 |
| 92 | + ascii_to_digit(input[i + 2]); |
| 93 | |
| 94 | encode_number(compact, number, NumericEncoding::Triple); |
| 95 | i += 3; |
| 96 | } |
| 97 | |
| 98 | // If the length is a multiple of 3, we are done |
| 99 | if len == input.len() { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | let mut number = 0; |
| 104 | while i < input.len() { |
| 105 | number *= 10; |
| 106 | number += ascii_to_digit(input[i]); |
| 107 | i += 1; |
| 108 | } |
| 109 | |
| 110 | let encoding = match i % 3 { |
| 111 | 1 => NumericEncoding::Single, |
| 112 | 2 => NumericEncoding::Double, |
| 113 | _ => unreachable!("i % 3 can only be 1 or 2"), |
| 114 | }; |
| 115 | |
| 116 | encode_number(compact, number, encoding); |
| 117 | } |
| 118 | |
| 119 | /// Encodes alphanumeric strings (i.e. "FAST-QR123"), referring to 8.4.3 of the spec. |
| 120 | pub(crate) fn encode_alphanumeric(compact: &mut CompactQR, input: &[u8], cci_bits: usize) { |
| 121 | compact.push_bits(0b0010, 4); |
| 122 | compact.push_bits(input.len(), cci_bits); |
| 123 | |
| 124 | let even_size = input.len() - input.len() % 2; |
| 125 | for chunk in input.chunks_exact(2) { |
| 126 | let a = ascii_to_alphanumeric(chunk[0]); |
| 127 | let b = ascii_to_alphanumeric(chunk[1]); |
| 128 | compact.push_bits(a * 45 + b, 11); |
| 129 | } |
| 130 | if even_size != input.len() { |
| 131 | compact.push_bits(ascii_to_alphanumeric(*input.last().unwrap()), 6); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Encodes any string (i.e. "<https://fast-qr.com/🚀>"), referring to 8.4.4 of the spec. |
| 136 | pub(crate) fn encode_byte(compact: &mut CompactQR, input: &[u8], cci_bits: usize) { |
| 137 | compact.push_bits(0b0100, 4); |
| 138 | compact.push_bits(input.len(), cci_bits); |
| 139 | compact.push_u8_slice(input); |
| 140 | } |
| 141 | |
| 142 | /// Adds needed terminator padding, terminating the data `BitString`, referring to 8.4.8 of the spec. |
| 143 | fn add_terminator(compact: &mut CompactQR, data_bits: usize) { |
| 144 | let len = data_bits - compact.len(); |
| 145 | let len = core::cmp::min(len, 4); |
| 146 | |
| 147 | compact.push_bits(0, len); |
| 148 | } |
| 149 | |
| 150 | /// Adds the padding to make the length of the `BitString` a multiple of 8, referring to 8.4.9 of the spec. |
| 151 | fn pad_to_8(compact: &mut CompactQR) { |
| 152 | let len = (8 - compact.len() % 8) % 8; |
| 153 | compact.push_bits(0, len); |
| 154 | } |
| 155 | |
| 156 | /// Converts ascii number to it's value in usize \ |
| 157 | /// "5" -> 5 |
| 158 | fn ascii_to_digit(c: u8) -> usize { |
| 159 | assert!( |
| 160 | c.is_ascii_digit(), |
| 161 | "Unexpected character '{}' in Numeric mode", |
| 162 | c as char |
| 163 | ); |
| 164 | (c - b'0') as usize |
| 165 | } |
| 166 | |
| 167 | /// Converts ascii alnum to it's numeric value, characters included in `AlphaNumeric` are: \ |
| 168 | /// 0-9, A-Z, $%*./:+-?.= [space] \ |
| 169 | /// referring to 7.1 of the spec. |
| 170 | pub(crate) fn ascii_to_alphanumeric(c: u8) -> usize { |
| 171 | match c { |
| 172 | b'0'..=b'9' => (c - b'0') as usize, |
| 173 | b'A'..=b'Z' => (c - b'A') as usize + 10, |
| 174 | b' ' => 36, |
| 175 | b'$' => 37, |
| 176 | b'%' => 38, |
| 177 | b'*' => 39, |
| 178 | b'+' => 40, |
| 179 | b'-' => 41, |
| 180 | b'.' => 42, |
| 181 | b'/' => 43, |
| 182 | b':' => 44, |
| 183 | _ => panic!("Unexpected character '{}' in Alphanumeric mode", c as char), |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /// Checks if character c is alphanumeric: 0-9, A-Z, $%*./:+-?.= [space] \ |
| 188 | /// referring to 7.1 of the spec. |
| 189 | const fn is_qr_alphanumeric(c: u8) -> bool { |
| 190 | matches!(c, |
| 191 | b'A'..=b'Z' |
| 192 | | b'0'..=b'9' |
| 193 | | b' ' |
| 194 | | b'$' |
| 195 | | b'%' |
| 196 | | b'*' |
| 197 | | b'+' |
| 198 | | b'-' |
| 199 | | b'.' |
| 200 | | b'/' |
| 201 | | b':') |
| 202 | } |