Skip to content

Commit

Permalink
QR8bitByte: address latentcat#41 where some input strings are not cor…
Browse files Browse the repository at this point in the history
…rectly byte-encoded
  • Loading branch information
deed02392 committed Aug 14, 2022
1 parent 93a58b6 commit 6195ca5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"webpack-merge": "^4.2.2"
},
"dependencies": {
"utf8": "^3.0.0",
"yup": "^0.29.1"
},
"husky": {
Expand Down
32 changes: 4 additions & 28 deletions src/utils/qrcodeEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,18 @@
// http://www.denso-wave.com/qrcode/faqpatent-e.html
//
// ---------------------------------------------------------------------
import utf8 from 'utf8';

/* eslint-disable */
function QR8bitByte(data) {
this.mode = QRMode.MODE_8BIT_BYTE;
this.data = data;
this.data = utf8.encode(data);
this.parsedData = [];

// Added to support UTF-8 Characters
for (var i = 0, l = this.data.length; i < l; i++) {
var byteArray = [];
var code = this.data.charCodeAt(i);

if (code > 0x10000) {
byteArray[0] = 0xf0 | ((code & 0x1c0000) >>> 18);
byteArray[1] = 0x80 | ((code & 0x3f000) >>> 12);
byteArray[2] = 0x80 | ((code & 0xfc0) >>> 6);
byteArray[3] = 0x80 | (code & 0x3f);
} else if (code > 0x800) {
byteArray[0] = 0xe0 | ((code & 0xf000) >>> 12);
byteArray[1] = 0x80 | ((code & 0xfc0) >>> 6);
byteArray[2] = 0x80 | (code & 0x3f);
} else if (code > 0x80) {
byteArray[0] = 0xc0 | ((code & 0x7c0) >>> 6);
byteArray[1] = 0x80 | (code & 0x3f);
} else {
byteArray[0] = code;
}

this.parsedData.push(byteArray);
}

this.parsedData = Array.prototype.concat.apply([], this.parsedData);

if (this.parsedData.length != this.data.length) {
this.parsedData.unshift(191);
this.parsedData.unshift(187);
this.parsedData.unshift(239);
this.parsedData.push(code);
}
}

Expand Down

0 comments on commit 6195ca5

Please sign in to comment.