[
  {
    "path": "Readme.markdown",
    "content": "QR Code generator library\n=========================\n\n\nIntroduction\n------------\n\nThis project aims to be the best, clearest QR Code generator library in multiple languages. The primary goals are flexible options and absolute correctness. Secondary goals are compact implementation size and good documentation comments.\n\nHome 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)\n\n\nFeatures\n--------\n\nCore features:\n\n* Available in 6 programming languages, all with nearly equal functionality: Java, TypeScript/JavaScript, Python, Rust, C++, C\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nOptional advanced features (Java only):\n\n* Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes\n* Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\nThe code below is in Java, but the other language ports are designed with essentially the same API naming and behavior.\n\n```java\nimport java.awt.image.BufferedImage;\nimport java.io.File;\nimport java.util.List;\nimport javax.imageio.ImageIO;\nimport io.nayuki.qrcodegen.*;\n\n// Simple operation\nQrCode qr0 = QrCode.encodeText(\"Hello, world!\", QrCode.Ecc.MEDIUM);\nBufferedImage img = toImage(qr0, 4, 10);  // See QrCodeGeneratorDemo\nImageIO.write(img, \"png\", new File(\"qr-code.png\"));\n\n// Manual operation\nList<QrSegment> segs = QrSegment.makeSegments(\"3141592653589793238462643383\");\nQrCode qr1 = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);\nfor (int y = 0; y < qr1.size; y++) {\n    for (int x = 0; x < qr1.size; x++) {\n        (... paint qr1.getModule(x, y) ...)\n    }\n}\n```\n\n\nLicense\n-------\n\nCopyright © 2025 Project Nayuki. (MIT License)  \n[https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\n* The above copyright notice and this permission notice shall be included in\n  all copies or substantial portions of the Software.\n\n* The Software is provided \"as is\", without warranty of any kind, express or\n  implied, including but not limited to the warranties of merchantability,\n  fitness for a particular purpose and noninfringement. In no event shall the\n  authors or copyright holders be liable for any claim, damages or other\n  liability, whether in an action of contract, tort or otherwise, arising from,\n  out of or in connection with the Software or the use or other dealings in the\n  Software.\n"
  },
  {
    "path": "c/Makefile",
    "content": "# \n# Makefile for QR Code generator (C)\n# \n# Copyright (c) Project Nayuki. (MIT License)\n# https://www.nayuki.io/page/qr-code-generator-library\n# \n# Permission is hereby granted, free of charge, to any person obtaining a copy of\n# this software and associated documentation files (the \"Software\"), to deal in\n# the Software without restriction, including without limitation the rights to\n# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n# the Software, and to permit persons to whom the Software is furnished to do so,\n# subject to the following conditions:\n# - The above copyright notice and this permission notice shall be included in\n#   all copies or substantial portions of the Software.\n# - The Software is provided \"as is\", without warranty of any kind, express or\n#   implied, including but not limited to the warranties of merchantability,\n#   fitness for a particular purpose and noninfringement. In no event shall the\n#   authors or copyright holders be liable for any claim, damages or other\n#   liability, whether in an action of contract, tort or otherwise, arising from,\n#   out of or in connection with the Software or the use or other dealings in the\n#   Software.\n# \n\n\n# ---- Configuration options ----\n\n# External/implicit variables:\n# - CC: The C compiler, such as gcc or clang.\n# - CFLAGS: Any extra user-specified compiler flags (can be blank).\n\n# Recommended compiler flags:\nCFLAGS += -std=c99 -O\n\n# Extra flags for diagnostics:\n# CFLAGS += -g -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -fsanitize=undefined,address\n\n\n# ---- Controlling make ----\n\n# Clear default suffix rules\n.SUFFIXES:\n\n# Don't delete object files\n.SECONDARY:\n\n# Stuff concerning goals\n.DEFAULT_GOAL = all\n.PHONY: all clean\n\n\n# ---- Targets to build ----\n\nLIB = qrcodegen\nLIBFILE = lib$(LIB).a\nLIBOBJ = qrcodegen.o\nMAINS = qrcodegen-demo qrcodegen-test\n\n# Build all binaries\nall: $(LIBFILE) $(MAINS)\n\n# Delete build output\nclean:\n\trm -f -- $(LIBOBJ) $(LIBFILE) $(MAINS:=.o) $(MAINS)\n\trm -rf .deps\n\n# Executable files\n%: %.o $(LIBFILE)\n\t$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -L . -l $(LIB)\n\n# Special executable\nqrcodegen-test: qrcodegen-test.c $(LIBOBJ:%.o=%.c)\n\t$(CC) $(CFLAGS) $(LDFLAGS) -DQRCODEGEN_TEST -o $@ $^\n\n# The library\n$(LIBFILE): $(LIBOBJ)\n\t$(AR) -crs $@ -- $^\n\n# Object files\n%.o: %.c .deps/timestamp\n\t$(CC) $(CFLAGS) -c -o $@ -MMD -MF .deps/$*.d $<\n\n# Have a place to store header dependencies automatically generated by compiler\n.deps/timestamp:\n\tmkdir -p .deps\n\ttouch .deps/timestamp\n\n# Make use of said dependencies if available\n-include .deps/*.d\n"
  },
  {
    "path": "c/Readme.markdown",
    "content": "QR Code generator library - C\n=============================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Completely avoids heap allocation (`malloc()`), instead relying on suitably sized buffers from the caller and fixed-size stack allocations\n* Coded carefully to prevent memory corruption, integer overflow, platform-dependent inconsistencies, and undefined behavior; tested rigorously to confirm safety\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```c\n#include <stdbool.h>\n#include <stdint.h>\n#include \"qrcodegen.h\"\n\n// Text data\nuint8_t qr0[qrcodegen_BUFFER_LEN_MAX];\nuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\nbool ok = qrcodegen_encodeText(\"Hello, world!\",\n    tempBuffer, qr0, qrcodegen_Ecc_MEDIUM,\n    qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX,\n    qrcodegen_Mask_AUTO, true);\nif (!ok)\n    return;\n\nint size = qrcodegen_getSize(qr0);\nfor (int y = 0; y < size; y++) {\n    for (int x = 0; x < size; x++) {\n        (... paint qrcodegen_getModule(qr0, x, y) ...)\n    }\n}\n\n// Binary data\nuint8_t dataAndTemp[qrcodegen_BUFFER_LEN_FOR_VERSION(7)]\n    = {0xE3, 0x81, 0x82};\nuint8_t qr1[qrcodegen_BUFFER_LEN_FOR_VERSION(7)];\nok = qrcodegen_encodeBinary(dataAndTemp, 3, qr1,\n    qrcodegen_Ecc_HIGH, 2, 7, qrcodegen_Mask_4, false);\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/c/qrcodegen-demo.c .\n"
  },
  {
    "path": "c/qrcodegen-demo.c",
    "content": "/* \n * QR Code generator demo (C)\n * \n * Run this command-line program with no arguments. The program\n * computes a demonstration QR Codes and print it to the console.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"qrcodegen.h\"\n\n\n// Function prototypes\nstatic void doBasicDemo(void);\nstatic void doVarietyDemo(void);\nstatic void doSegmentDemo(void);\nstatic void doMaskDemo(void);\nstatic void printQr(const uint8_t qrcode[]);\n\n\n// The main application program.\nint main(void) {\n\tdoBasicDemo();\n\tdoVarietyDemo();\n\tdoSegmentDemo();\n\tdoMaskDemo();\n\treturn EXIT_SUCCESS;\n}\n\n\n\n/*---- Demo suite ----*/\n\n// Creates a single QR Code, then prints it to the console.\nstatic void doBasicDemo(void) {\n\tconst char *text = \"Hello, world!\";                // User-supplied text\n\tenum qrcodegen_Ecc errCorLvl = qrcodegen_Ecc_LOW;  // Error correction level\n\t\n\t// Make and print the QR Code symbol\n\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\tbool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, errCorLvl,\n\t\tqrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\tif (ok)\n\t\tprintQr(qrcode);\n}\n\n\n// Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.\nstatic void doVarietyDemo(void) {\n\t{  // Numeric mode encoding (3.33 bits per digit)\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok = qrcodegen_encodeText(\"314159265358979323846264338327950288419716939937510\", tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t}\n\t\n\t{  // Alphanumeric mode encoding (5.5 bits per character)\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok = qrcodegen_encodeText(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_HIGH, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t}\n\t\n\t{  // Unicode text as UTF-8\n\t\tconst char *text = \"\\xE3\\x81\\x93\\xE3\\x82\\x93\\xE3\\x81\\xAB\\xE3\\x81\\xA1wa\\xE3\\x80\\x81\"\n\t\t\t\"\\xE4\\xB8\\x96\\xE7\\x95\\x8C\\xEF\\xBC\\x81\\x20\\xCE\\xB1\\xCE\\xB2\\xCE\\xB3\\xCE\\xB4\";\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok = qrcodegen_encodeText(text, tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_QUARTILE, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t}\n\t\n\t{  // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\t\tconst char *text =\n\t\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \"\n\t\t\t\"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \"\n\t\t\t\"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \"\n\t\t\t\"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \"\n\t\t\t\"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \"\n\t\t\t\"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \"\n\t\t\t\"a White Rabbit with pink eyes ran close by her.\";\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok = qrcodegen_encodeText(text, tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_HIGH, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t}\n}\n\n\n// Creates QR Codes with manually specified segments for better compactness.\nstatic void doSegmentDemo(void) {\n\t{  // Illustration \"silver\"\n\t\tconst char *silver0 = \"THE SQUARE ROOT OF 2 IS 1.\";\n\t\tconst char *silver1 = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok;\n\t\t{\n\t\t\tchar *concat = calloc(strlen(silver0) + strlen(silver1) + 1, sizeof(char));\n\t\t\tif (concat == NULL) {\n\t\t\t\tperror(\"calloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tstrcat(concat, silver0);\n\t\t\tstrcat(concat, silver1);\n\t\t\tok = qrcodegen_encodeText(concat, tempBuffer, qrcode, qrcodegen_Ecc_LOW,\n\t\t\t\tqrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\t\tif (ok)\n\t\t\t\tprintQr(qrcode);\n\t\t\tfree(concat);\n\t\t}\n\t\t{\n\t\t\tuint8_t *segBuf0 = malloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, strlen(silver0)) * sizeof(uint8_t));\n\t\t\tuint8_t *segBuf1 = malloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, strlen(silver1)) * sizeof(uint8_t));\n\t\t\tif (segBuf0 == NULL || segBuf1 == NULL) {\n\t\t\t\tperror(\"malloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t\tqrcodegen_makeAlphanumeric(silver0, segBuf0),\n\t\t\t\tqrcodegen_makeNumeric(silver1, segBuf1),\n\t\t\t};\n\t\t\tok = qrcodegen_encodeSegments(segs, sizeof(segs) / sizeof(segs[0]), qrcodegen_Ecc_LOW, tempBuffer, qrcode);\n\t\t\tfree(segBuf0);\n\t\t\tfree(segBuf1);\n\t\t\tif (ok)\n\t\t\t\tprintQr(qrcode);\n\t\t}\n\t}\n\t\n\t{  // Illustration \"golden\"\n\t\tconst char *golden0 = \"Golden ratio \\xCF\\x86 = 1.\";\n\t\tconst char *golden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\t\tconst char *golden2 = \"......\";\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok;\n\t\t{\n\t\t\tchar *concat = calloc(strlen(golden0) + strlen(golden1) + strlen(golden2) + 1, sizeof(char));\n\t\t\tif (concat == NULL) {\n\t\t\t\tperror(\"calloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tstrcat(concat, golden0);\n\t\t\tstrcat(concat, golden1);\n\t\t\tstrcat(concat, golden2);\n\t\t\tok = qrcodegen_encodeText(concat, tempBuffer, qrcode, qrcodegen_Ecc_LOW,\n\t\t\t\tqrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\t\tif (ok)\n\t\t\t\tprintQr(qrcode);\n\t\t\tfree(concat);\n\t\t}\n\t\t{\n\t\t\tuint8_t *bytes = malloc(strlen(golden0) * sizeof(uint8_t));\n\t\t\tif (bytes == NULL) {\n\t\t\t\tperror(\"malloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tfor (size_t i = 0, len = strlen(golden0); i < len; i++)\n\t\t\t\tbytes[i] = (uint8_t)golden0[i];\n\t\t\tuint8_t *segBuf0 = malloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_BYTE, strlen(golden0)) * sizeof(uint8_t));\n\t\t\tuint8_t *segBuf1 = malloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, strlen(golden1)) * sizeof(uint8_t));\n\t\t\tuint8_t *segBuf2 = malloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, strlen(golden2)) * sizeof(uint8_t));\n\t\t\tif (segBuf0 == NULL || segBuf1 == NULL || segBuf2 == NULL) {\n\t\t\t\tperror(\"malloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t\tqrcodegen_makeBytes(bytes, strlen(golden0), segBuf0),\n\t\t\t\tqrcodegen_makeNumeric(golden1, segBuf1),\n\t\t\t\tqrcodegen_makeAlphanumeric(golden2, segBuf2),\n\t\t\t};\n\t\t\tfree(bytes);\n\t\t\tok = qrcodegen_encodeSegments(segs, sizeof(segs) / sizeof(segs[0]), qrcodegen_Ecc_LOW, tempBuffer, qrcode);\n\t\t\tfree(segBuf0);\n\t\t\tfree(segBuf1);\n\t\t\tfree(segBuf2);\n\t\t\tif (ok)\n\t\t\t\tprintQr(qrcode);\n\t\t}\n\t}\n\t\n\t{  // Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok;\n\t\t{\n\t\t\tconst char *madoka =  // Encoded in UTF-8\n\t\t\t\t\"\\xE3\\x80\\x8C\\xE9\\xAD\\x94\\xE6\\xB3\\x95\\xE5\"\n\t\t\t\t\"\\xB0\\x91\\xE5\\xA5\\xB3\\xE3\\x81\\xBE\\xE3\\x81\"\n\t\t\t\t\"\\xA9\\xE3\\x81\\x8B\\xE2\\x98\\x86\\xE3\\x83\\x9E\"\n\t\t\t\t\"\\xE3\\x82\\xAE\\xE3\\x82\\xAB\\xE3\\x80\\x8D\\xE3\"\n\t\t\t\t\"\\x81\\xA3\\xE3\\x81\\xA6\\xE3\\x80\\x81\\xE3\\x80\"\n\t\t\t\t\"\\x80\\xD0\\x98\\xD0\\x90\\xD0\\x98\\xE3\\x80\\x80\"\n\t\t\t\t\"\\xEF\\xBD\\x84\\xEF\\xBD\\x85\\xEF\\xBD\\x93\\xEF\"\n\t\t\t\t\"\\xBD\\x95\\xE3\\x80\\x80\\xCE\\xBA\\xCE\\xB1\\xEF\"\n\t\t\t\t\"\\xBC\\x9F\";\n\t\t\tok = qrcodegen_encodeText(madoka, tempBuffer, qrcode, qrcodegen_Ecc_LOW,\n\t\t\t\tqrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\t\tif (ok)\n\t\t\t\tprintQr(qrcode);\n\t\t}\n\t\t{\n\t\t\tconst int kanjiChars[] = {  // Kanji mode encoding (13 bits per character)\n\t\t\t\t0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,\n\t\t\t\t0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,\n\t\t\t\t0x018D, 0x018A, 0x0036, 0x0141, 0x0144,\n\t\t\t\t0x0001, 0x0000, 0x0249, 0x0240, 0x0249,\n\t\t\t\t0x0000, 0x0104, 0x0105, 0x0113, 0x0115,\n\t\t\t\t0x0000, 0x0208, 0x01FF, 0x0008,\n\t\t\t};\n\t\t\tsize_t len = sizeof(kanjiChars) / sizeof(kanjiChars[0]);\n\t\t\tuint8_t *segBuf = calloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_KANJI, len), sizeof(uint8_t));\n\t\t\tif (segBuf == NULL) {\n\t\t\t\tperror(\"calloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tstruct qrcodegen_Segment seg;\n\t\t\tseg.mode = qrcodegen_Mode_KANJI;\n\t\t\tseg.numChars = (int)len;\n\t\t\tseg.bitLength = 0;\n\t\t\tfor (size_t i = 0; i < len; i++) {\n\t\t\t\tfor (int j = 12; j >= 0; j--, seg.bitLength++)\n\t\t\t\t\tsegBuf[seg.bitLength >> 3] |= ((kanjiChars[i] >> j) & 1) << (7 - (seg.bitLength & 7));\n\t\t\t}\n\t\t\tseg.data = segBuf;\n\t\t\tok = qrcodegen_encodeSegments(&seg, 1, qrcodegen_Ecc_LOW, tempBuffer, qrcode);\n\t\t\tfree(segBuf);\n\t\t\tif (ok)\n\t\t\t\tprintQr(qrcode);\n\t\t}\n\t}\n}\n\n\n// Creates QR Codes with the same size and contents but different mask patterns.\nstatic void doMaskDemo(void) {\n\t{  // Project Nayuki URL\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok;\n\t\t\n\t\tok = qrcodegen_encodeText(\"https://www.nayuki.io/\", tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_HIGH, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t\t\n\t\tok = qrcodegen_encodeText(\"https://www.nayuki.io/\", tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_HIGH, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_3, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t}\n\t\n\t{  // Chinese text as UTF-8\n\t\tconst char *text =\n\t\t\t\"\\xE7\\xB6\\xAD\\xE5\\x9F\\xBA\\xE7\\x99\\xBE\\xE7\\xA7\\x91\\xEF\\xBC\\x88\\x57\\x69\\x6B\\x69\\x70\"\n\t\t\t\"\\x65\\x64\\x69\\x61\\xEF\\xBC\\x8C\\xE8\\x81\\x86\\xE8\\x81\\xBD\\x69\\x2F\\xCB\\x8C\\x77\\xC9\\xAA\"\n\t\t\t\"\\x6B\\xE1\\xB5\\xBB\\xCB\\x88\\x70\\x69\\xCB\\x90\\x64\\x69\\x2E\\xC9\\x99\\x2F\\xEF\\xBC\\x89\\xE6\"\n\t\t\t\"\\x98\\xAF\\xE4\\xB8\\x80\\xE5\\x80\\x8B\\xE8\\x87\\xAA\\xE7\\x94\\xB1\\xE5\\x85\\xA7\\xE5\\xAE\\xB9\"\n\t\t\t\"\\xE3\\x80\\x81\\xE5\\x85\\xAC\\xE9\\x96\\x8B\\xE7\\xB7\\xA8\\xE8\\xBC\\xAF\\xE4\\xB8\\x94\\xE5\\xA4\"\n\t\t\t\"\\x9A\\xE8\\xAA\\x9E\\xE8\\xA8\\x80\\xE7\\x9A\\x84\\xE7\\xB6\\xB2\\xE8\\xB7\\xAF\\xE7\\x99\\xBE\\xE7\"\n\t\t\t\"\\xA7\\x91\\xE5\\x85\\xA8\\xE6\\x9B\\xB8\\xE5\\x8D\\x94\\xE4\\xBD\\x9C\\xE8\\xA8\\x88\\xE7\\x95\\xAB\";\n\t\tuint8_t qrcode[qrcodegen_BUFFER_LEN_MAX];\n\t\tuint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];\n\t\tbool ok;\n\t\t\n\t\tok = qrcodegen_encodeText(text, tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_0, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t\t\n\t\tok = qrcodegen_encodeText(text, tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_1, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t\t\n\t\tok = qrcodegen_encodeText(text, tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_5, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t\t\n\t\tok = qrcodegen_encodeText(text, tempBuffer, qrcode,\n\t\t\tqrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_7, true);\n\t\tif (ok)\n\t\t\tprintQr(qrcode);\n\t}\n}\n\n\n\n/*---- Utilities ----*/\n\n// Prints the given QR Code to the console.\nstatic void printQr(const uint8_t qrcode[]) {\n\tint size = qrcodegen_getSize(qrcode);\n\tint border = 4;\n\tfor (int y = -border; y < size + border; y++) {\n\t\tfor (int x = -border; x < size + border; x++) {\n\t\t\tfputs((qrcodegen_getModule(qrcode, x, y) ? \"##\" : \"  \"), stdout);\n\t\t}\n\t\tfputs(\"\\n\", stdout);\n\t}\n\tfputs(\"\\n\", stdout);\n}\n"
  },
  {
    "path": "c/qrcodegen-test.c",
    "content": "/* \n * QR Code generator test suite (C)\n * \n * When compiling this program, the library qrcodegen.c needs QRCODEGEN_TEST\n * to be defined. Run this command line program with no arguments.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#include <assert.h>\n#include <limits.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <time.h>\n#include \"qrcodegen.h\"\n\n#define ARRAY_LENGTH(name)  (sizeof(name) / sizeof(name[0]))\n\n\n// Global variables\nstatic int numTestCases = 0;\n\n\n// Prototypes of private functions under test\nextern const int8_t ECC_CODEWORDS_PER_BLOCK[4][41];\nextern const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];\nvoid appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);\nvoid addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);\nint getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);\nint getNumRawDataModules(int version);\nvoid reedSolomonComputeDivisor(int degree, uint8_t result[]);\nvoid reedSolomonComputeRemainder(const uint8_t data[], int dataLen, const uint8_t generator[], int degree, uint8_t result[]);\nuint8_t reedSolomonMultiply(uint8_t x, uint8_t y);\nvoid initializeFunctionModules(int version, uint8_t qrcode[]);\nint getAlignmentPatternPositions(int version, uint8_t result[7]);\nbool getModuleBounded(const uint8_t qrcode[], int x, int y);\nvoid setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark);\nvoid setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark);\nint calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars);\nint getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version);\n\n\n/*---- Test cases ----*/\n\nstatic void testAppendBitsToBuffer(void) {\n\t{\n\t\tuint8_t buf[1] = {0};\n\t\tint bitLen = 0;\n\t\tappendBitsToBuffer(0, 0, buf, &bitLen);\n\t\tassert(bitLen == 0);\n\t\tassert(buf[0] == 0);\n\t\tappendBitsToBuffer(1, 1, buf, &bitLen);\n\t\tassert(bitLen == 1);\n\t\tassert(buf[0] == 0x80);\n\t\tappendBitsToBuffer(0, 1, buf, &bitLen);\n\t\tassert(bitLen == 2);\n\t\tassert(buf[0] == 0x80);\n\t\tappendBitsToBuffer(5, 3, buf, &bitLen);\n\t\tassert(bitLen == 5);\n\t\tassert(buf[0] == 0xA8);\n\t\tappendBitsToBuffer(6, 3, buf, &bitLen);\n\t\tassert(bitLen == 8);\n\t\tassert(buf[0] == 0xAE);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[6] = {0};\n\t\tint bitLen = 0;\n\t\tappendBitsToBuffer(16942, 16, buf, &bitLen);\n\t\tassert(bitLen == 16);\n\t\tassert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x00 && buf[3] == 0x00 && buf[4] == 0x00 && buf[5] == 0x00);\n\t\tappendBitsToBuffer(10, 7, buf, &bitLen);\n\t\tassert(bitLen == 23);\n\t\tassert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x14 && buf[3] == 0x00 && buf[4] == 0x00 && buf[5] == 0x00);\n\t\tappendBitsToBuffer(15, 4, buf, &bitLen);\n\t\tassert(bitLen == 27);\n\t\tassert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x15 && buf[3] == 0xE0 && buf[4] == 0x00 && buf[5] == 0x00);\n\t\tappendBitsToBuffer(26664, 15, buf, &bitLen);\n\t\tassert(bitLen == 42);\n\t\tassert(buf[0] == 0x42 && buf[1] == 0x2E && buf[2] == 0x15 && buf[3] == 0xFA && buf[4] == 0x0A && buf[5] == 0x00);\n\t\tnumTestCases++;\n\t}\n}\n\n\n// Ported from the Java version of the code.\nstatic uint8_t *addEccAndInterleaveReference(const uint8_t *data, int version, enum qrcodegen_Ecc ecl) {\n\t// Calculate parameter numbers\n\tsize_t numBlocks = (size_t)NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version];\n\tsize_t blockEccLen = (size_t)ECC_CODEWORDS_PER_BLOCK[(int)ecl][version];\n\tsize_t rawCodewords = (size_t)getNumRawDataModules(version) / 8;\n\tsize_t numShortBlocks = numBlocks - rawCodewords % numBlocks;\n\tsize_t shortBlockLen = rawCodewords / numBlocks;\n\t\n\t// Split data into blocks and append ECC to each block\n\tuint8_t **blocks = malloc(numBlocks * sizeof(uint8_t*));\n\tuint8_t *generator = malloc(blockEccLen * sizeof(uint8_t));\n\tif (blocks == NULL || generator == NULL) {\n\t\tperror(\"malloc\");\n\t\texit(EXIT_FAILURE);\n\t}\n\treedSolomonComputeDivisor((int)blockEccLen, generator);\n\tfor (size_t i = 0, k = 0; i < numBlocks; i++) {\n\t\tuint8_t *block = malloc((shortBlockLen + 1) * sizeof(uint8_t));\n\t\tif (block == NULL) {\n\t\t\tperror(\"malloc\");\n\t\t\texit(EXIT_FAILURE);\n\t\t}\n\t\tsize_t datLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1);\n\t\tmemcpy(block, &data[k], datLen * sizeof(uint8_t));\n\t\treedSolomonComputeRemainder(&data[k], (int)datLen, generator, (int)blockEccLen, &block[shortBlockLen + 1 - blockEccLen]);\n\t\tk += datLen;\n\t\tblocks[i] = block;\n\t}\n\tfree(generator);\n\t\n\t// Interleave (not concatenate) the bytes from every block into a single sequence\n\tuint8_t *result = malloc(rawCodewords * sizeof(uint8_t));\n\tif (result == NULL) {\n\t\tperror(\"malloc\");\n\t\texit(EXIT_FAILURE);\n\t}\n\tfor (size_t i = 0, k = 0; i < shortBlockLen + 1; i++) {\n\t\tfor (size_t j = 0; j < numBlocks; j++) {\n\t\t\t// Skip the padding byte in short blocks\n\t\t\tif (i != shortBlockLen - blockEccLen || j >= numShortBlocks) {\n\t\t\t\tresult[k] = blocks[j][i];\n\t\t\t\tk++;\n\t\t\t}\n\t\t}\n\t}\n\tfor (size_t i = 0; i < numBlocks; i++)\n\t\tfree(blocks[i]);\n\tfree(blocks);\n\treturn result;\n}\n\n\nstatic void testAddEccAndInterleave(void) {\n\tfor (int version = 1; version <= 40; version++) {\n\t\tfor (int ecl = 0; ecl < 4; ecl++) {\n\t\t\tsize_t dataLen = (size_t)getNumDataCodewords(version, (enum qrcodegen_Ecc)ecl);\n\t\t\tuint8_t *pureData = malloc(dataLen * sizeof(uint8_t));\n\t\t\tif (pureData == NULL) {\n\t\t\t\tperror(\"malloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tfor (size_t i = 0; i < dataLen; i++)\n\t\t\t\tpureData[i] = (uint8_t)(rand() % 256);\n\t\t\tuint8_t *expectOutput = addEccAndInterleaveReference(pureData, version, (enum qrcodegen_Ecc)ecl);\n\t\t\t\n\t\t\tsize_t dataAndEccLen = (size_t)getNumRawDataModules(version) / 8;\n\t\t\tuint8_t *paddedData = malloc(dataAndEccLen * sizeof(uint8_t));\n\t\t\tif (paddedData == NULL) {\n\t\t\t\tperror(\"malloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\tmemcpy(paddedData, pureData, dataLen * sizeof(uint8_t));\n\t\t\tuint8_t *actualOutput = malloc(dataAndEccLen * sizeof(uint8_t));\n\t\t\tif (actualOutput == NULL) {\n\t\t\t\tperror(\"malloc\");\n\t\t\t\texit(EXIT_FAILURE);\n\t\t\t}\n\t\t\taddEccAndInterleave(paddedData, version, (enum qrcodegen_Ecc)ecl, actualOutput);\n\t\t\t\n\t\t\tassert(memcmp(actualOutput, expectOutput, dataAndEccLen * sizeof(uint8_t)) == 0);\n\t\t\tfree(pureData);\n\t\t\tfree(expectOutput);\n\t\t\tfree(paddedData);\n\t\t\tfree(actualOutput);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n}\n\n\nstatic void testGetNumDataCodewords(void) {\n\tconst int cases[][3] = {\n\t\t{ 3, 1,   44},\n\t\t{ 3, 2,   34},\n\t\t{ 3, 3,   26},\n\t\t{ 6, 0,  136},\n\t\t{ 7, 0,  156},\n\t\t{ 9, 0,  232},\n\t\t{ 9, 1,  182},\n\t\t{12, 3,  158},\n\t\t{15, 0,  523},\n\t\t{16, 2,  325},\n\t\t{19, 3,  341},\n\t\t{21, 0,  932},\n\t\t{22, 0, 1006},\n\t\t{22, 1,  782},\n\t\t{22, 3,  442},\n\t\t{24, 0, 1174},\n\t\t{24, 3,  514},\n\t\t{28, 0, 1531},\n\t\t{30, 3,  745},\n\t\t{32, 3,  845},\n\t\t{33, 0, 2071},\n\t\t{33, 3,  901},\n\t\t{35, 0, 2306},\n\t\t{35, 1, 1812},\n\t\t{35, 2, 1286},\n\t\t{36, 3, 1054},\n\t\t{37, 3, 1096},\n\t\t{39, 1, 2216},\n\t\t{40, 1, 2334},\n\t};\n\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\tconst int *tc = cases[i];\n\t\tassert(getNumDataCodewords(tc[0], (enum qrcodegen_Ecc)tc[1]) == tc[2]);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testGetNumRawDataModules(void) {\n\tconst int cases[][2] = {\n\t\t{ 1,   208},\n\t\t{ 2,   359},\n\t\t{ 3,   567},\n\t\t{ 6,  1383},\n\t\t{ 7,  1568},\n\t\t{12,  3728},\n\t\t{15,  5243},\n\t\t{18,  7211},\n\t\t{22, 10068},\n\t\t{26, 13652},\n\t\t{32, 19723},\n\t\t{37, 25568},\n\t\t{40, 29648},\n\t};\n\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\tconst int *tc = cases[i];\n\t\tassert(getNumRawDataModules(tc[0]) == tc[1]);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testReedSolomonComputeDivisor(void) {\n\tuint8_t generator[30];\n\t\n\treedSolomonComputeDivisor(1, generator);\n\tassert(generator[0] == 0x01);\n\tnumTestCases++;\n\t\n\treedSolomonComputeDivisor(2, generator);\n\tassert(generator[0] == 0x03);\n\tassert(generator[1] == 0x02);\n\tnumTestCases++;\n\t\n\treedSolomonComputeDivisor(5, generator);\n\tassert(generator[0] == 0x1F);\n\tassert(generator[1] == 0xC6);\n\tassert(generator[2] == 0x3F);\n\tassert(generator[3] == 0x93);\n\tassert(generator[4] == 0x74);\n\tnumTestCases++;\n\t\n\treedSolomonComputeDivisor(30, generator);\n\tassert(generator[ 0] == 0xD4);\n\tassert(generator[ 1] == 0xF6);\n\tassert(generator[ 5] == 0xC0);\n\tassert(generator[12] == 0x16);\n\tassert(generator[13] == 0xD9);\n\tassert(generator[20] == 0x12);\n\tassert(generator[27] == 0x6A);\n\tassert(generator[29] == 0x96);\n\tnumTestCases++;\n}\n\n\nstatic void testReedSolomonComputeRemainder(void) {\n\t{\n\t\tuint8_t data[1];\n\t\tuint8_t generator[3];\n\t\tuint8_t remainder[ARRAY_LENGTH(generator)];\n\t\treedSolomonComputeDivisor(ARRAY_LENGTH(generator), generator);\n\t\treedSolomonComputeRemainder(data, 0, generator, ARRAY_LENGTH(generator), remainder);\n\t\tassert(remainder[0] == 0);\n\t\tassert(remainder[1] == 0);\n\t\tassert(remainder[2] == 0);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t data[2] = {0, 1};\n\t\tuint8_t generator[4];\n\t\tuint8_t remainder[ARRAY_LENGTH(generator)];\n\t\treedSolomonComputeDivisor(ARRAY_LENGTH(generator), generator);\n\t\treedSolomonComputeRemainder(data, ARRAY_LENGTH(data), generator, ARRAY_LENGTH(generator), remainder);\n\t\tassert(remainder[0] == generator[0]);\n\t\tassert(remainder[1] == generator[1]);\n\t\tassert(remainder[2] == generator[2]);\n\t\tassert(remainder[3] == generator[3]);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t data[5] = {0x03, 0x3A, 0x60, 0x12, 0xC7};\n\t\tuint8_t generator[5];\n\t\tuint8_t remainder[ARRAY_LENGTH(generator)];\n\t\treedSolomonComputeDivisor(ARRAY_LENGTH(generator), generator);\n\t\treedSolomonComputeRemainder(data, ARRAY_LENGTH(data), generator, ARRAY_LENGTH(generator), remainder);\n\t\tassert(remainder[0] == 0xCB);\n\t\tassert(remainder[1] == 0x36);\n\t\tassert(remainder[2] == 0x16);\n\t\tassert(remainder[3] == 0xFA);\n\t\tassert(remainder[4] == 0x9D);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t data[43] = {\n\t\t\t0x38, 0x71, 0xDB, 0xF9, 0xD7, 0x28, 0xF6, 0x8E, 0xFE, 0x5E,\n\t\t\t0xE6, 0x7D, 0x7D, 0xB2, 0xA5, 0x58, 0xBC, 0x28, 0x23, 0x53,\n\t\t\t0x14, 0xD5, 0x61, 0xC0, 0x20, 0x6C, 0xDE, 0xDE, 0xFC, 0x79,\n\t\t\t0xB0, 0x8B, 0x78, 0x6B, 0x49, 0xD0, 0x1A, 0xAD, 0xF3, 0xEF,\n\t\t\t0x52, 0x7D, 0x9A,\n\t\t};\n\t\tuint8_t generator[30];\n\t\tuint8_t remainder[ARRAY_LENGTH(generator)];\n\t\treedSolomonComputeDivisor(ARRAY_LENGTH(generator), generator);\n\t\treedSolomonComputeRemainder(data, ARRAY_LENGTH(data), generator, ARRAY_LENGTH(generator), remainder);\n\t\tassert(remainder[ 0] == 0xCE);\n\t\tassert(remainder[ 1] == 0xF0);\n\t\tassert(remainder[ 2] == 0x31);\n\t\tassert(remainder[ 3] == 0xDE);\n\t\tassert(remainder[ 8] == 0xE1);\n\t\tassert(remainder[12] == 0xCA);\n\t\tassert(remainder[17] == 0xE3);\n\t\tassert(remainder[19] == 0x85);\n\t\tassert(remainder[20] == 0x50);\n\t\tassert(remainder[24] == 0xBE);\n\t\tassert(remainder[29] == 0xB3);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testReedSolomonMultiply(void) {\n\tconst uint8_t cases[][3] = {\n\t\t{0x00, 0x00, 0x00},\n\t\t{0x01, 0x01, 0x01},\n\t\t{0x02, 0x02, 0x04},\n\t\t{0x00, 0x6E, 0x00},\n\t\t{0xB2, 0xDD, 0xE6},\n\t\t{0x41, 0x11, 0x25},\n\t\t{0xB0, 0x1F, 0x11},\n\t\t{0x05, 0x75, 0xBC},\n\t\t{0x52, 0xB5, 0xAE},\n\t\t{0xA8, 0x20, 0xA4},\n\t\t{0x0E, 0x44, 0x9F},\n\t\t{0xD4, 0x13, 0xA0},\n\t\t{0x31, 0x10, 0x37},\n\t\t{0x6C, 0x58, 0xCB},\n\t\t{0xB6, 0x75, 0x3E},\n\t\t{0xFF, 0xFF, 0xE2},\n\t};\n\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\tconst uint8_t *tc = cases[i];\n\t\tassert(reedSolomonMultiply(tc[0], tc[1]) == tc[2]);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testInitializeFunctionModulesEtc(void) {\n\tfor (int ver = 1; ver <= 40; ver++) {\n\t\tuint8_t *qrcode = malloc((size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t));\n\t\tif (qrcode == NULL) {\n\t\t\tperror(\"malloc\");\n\t\t\texit(EXIT_FAILURE);\n\t\t}\n\t\tinitializeFunctionModules(ver, qrcode);\n\t\t\n\t\tint size = qrcodegen_getSize(qrcode);\n\t\tif (ver == 1)\n\t\t\tassert(size == 21);\n\t\telse if (ver == 40)\n\t\t\tassert(size == 177);\n\t\telse\n\t\t\tassert(size == ver * 4 + 17);\n\t\t\n\t\tbool hasLight = false;\n\t\tbool hasDark = false;\n\t\tfor (int y = 0; y < size; y++) {\n\t\t\tfor (int x = 0; x < size; x++) {\n\t\t\t\tbool color = qrcodegen_getModule(qrcode, x, y);\n\t\t\t\tif (color)\n\t\t\t\t\thasDark = true;\n\t\t\t\telse\n\t\t\t\t\thasLight = true;\n\t\t\t}\n\t\t}\n\t\tassert(hasLight && hasDark);\n\t\tfree(qrcode);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testGetAlignmentPatternPositions(void) {\n\tconst int cases[][9] = {\n\t\t{ 1, 0,  -1,  -1,  -1,  -1,  -1,  -1,  -1},\n\t\t{ 2, 2,   6,  18,  -1,  -1,  -1,  -1,  -1},\n\t\t{ 3, 2,   6,  22,  -1,  -1,  -1,  -1,  -1},\n\t\t{ 6, 2,   6,  34,  -1,  -1,  -1,  -1,  -1},\n\t\t{ 7, 3,   6,  22,  38,  -1,  -1,  -1,  -1},\n\t\t{ 8, 3,   6,  24,  42,  -1,  -1,  -1,  -1},\n\t\t{16, 4,   6,  26,  50,  74,  -1,  -1,  -1},\n\t\t{25, 5,   6,  32,  58,  84, 110,  -1,  -1},\n\t\t{32, 6,   6,  34,  60,  86, 112, 138,  -1},\n\t\t{33, 6,   6,  30,  58,  86, 114, 142,  -1},\n\t\t{39, 7,   6,  26,  54,  82, 110, 138, 166},\n\t\t{40, 7,   6,  30,  58,  86, 114, 142, 170},\n\t};\n\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\tconst int *tc = cases[i];\n\t\tuint8_t pos[7];\n\t\tint num = getAlignmentPatternPositions(tc[0], pos);\n\t\tassert(num == tc[1]);\n\t\tfor (int j = 0; j < num; j++)\n\t\t\tassert(pos[j] == tc[2 + j]);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testGetSetModule(void) {\n\tuint8_t qrcode[qrcodegen_BUFFER_LEN_FOR_VERSION(23)];\n\tinitializeFunctionModules(23, qrcode);\n\tint size = qrcodegen_getSize(qrcode);\n\t\n\tfor (int y = 0; y < size; y++) {  // Clear all to light\n\t\tfor (int x = 0; x < size; x++)\n\t\t\tsetModuleBounded(qrcode, x, y, false);\n\t}\n\tfor (int y = 0; y < size; y++) {  // Check all light\n\t\tfor (int x = 0; x < size; x++)\n\t\t\tassert(qrcodegen_getModule(qrcode, x, y) == false);\n\t}\n\tfor (int y = 0; y < size; y++) {  // Set all to dark\n\t\tfor (int x = 0; x < size; x++)\n\t\t\tsetModuleBounded(qrcode, x, y, true);\n\t}\n\tfor (int y = 0; y < size; y++) {  // Check all dark\n\t\tfor (int x = 0; x < size; x++)\n\t\t\tassert(qrcodegen_getModule(qrcode, x, y) == true);\n\t}\n\t\n\t// Set some out of bounds modules to light\n\tsetModuleUnbounded(qrcode, -1, -1, false);\n\tsetModuleUnbounded(qrcode, -1, 0, false);\n\tsetModuleUnbounded(qrcode, 0, -1, false);\n\tsetModuleUnbounded(qrcode, size, 5, false);\n\tsetModuleUnbounded(qrcode, 72, size, false);\n\tsetModuleUnbounded(qrcode, size, size, false);\n\tfor (int y = 0; y < size; y++) {  // Check all dark\n\t\tfor (int x = 0; x < size; x++)\n\t\t\tassert(qrcodegen_getModule(qrcode, x, y) == true);\n\t}\n\t\n\t// Set some modules to light\n\tsetModuleBounded(qrcode, 3, 8, false);\n\tsetModuleBounded(qrcode, 61, 49, false);\n\tfor (int y = 0; y < size; y++) {  // Check most dark\n\t\tfor (int x = 0; x < size; x++) {\n\t\t\tbool light = (x == 3 && y == 8) || (x == 61 && y == 49);\n\t\t\tassert(qrcodegen_getModule(qrcode, x, y) != light);\n\t\t}\n\t}\n\tnumTestCases++;\n}\n\n\nstatic void testGetSetModuleRandomly(void) {\n\tuint8_t qrcode[qrcodegen_BUFFER_LEN_FOR_VERSION(1)];\n\tinitializeFunctionModules(1, qrcode);\n\tint size = qrcodegen_getSize(qrcode);\n\t\n\tbool modules[21][21];\n\tfor (int y = 0; y < size; y++) {\n\t\tfor (int x = 0; x < size; x++)\n\t\t\tmodules[y][x] = qrcodegen_getModule(qrcode, x, y);\n\t}\n\t\n\tlong trials = 100000;\n\tfor (long i = 0; i < trials; i++) {\n\t\tint x = rand() % (size * 2) - size / 2;\n\t\tint y = rand() % (size * 2) - size / 2;\n\t\tbool isInBounds = 0 <= x && x < size && 0 <= y && y < size;\n\t\tbool oldColor = isInBounds && modules[y][x];\n\t\tif (isInBounds)\n\t\t\tassert(getModuleBounded(qrcode, x, y) == oldColor);\n\t\tassert(qrcodegen_getModule(qrcode, x, y) == oldColor);\n\t\t\n\t\tbool newColor = rand() % 2 == 0;\n\t\tif (isInBounds)\n\t\t\tmodules[y][x] = newColor;\n\t\tif (isInBounds && rand() % 2 == 0)\n\t\t\tsetModuleBounded(qrcode, x, y, newColor);\n\t\telse\n\t\t\tsetModuleUnbounded(qrcode, x, y, newColor);\n\t}\n\tnumTestCases++;\n}\n\n\nstatic void testIsAlphanumeric(void) {\n\tstruct TestCase {\n\t\tbool answer;\n\t\tconst char *text;\n\t};\n\tconst struct TestCase cases[] = {\n\t\t{true, \"\"},\n\t\t{true, \"0\"},\n\t\t{true, \"A\"},\n\t\t{false, \"a\"},\n\t\t{true, \" \"},\n\t\t{true, \".\"},\n\t\t{true, \"*\"},\n\t\t{false, \",\"},\n\t\t{false, \"|\"},\n\t\t{false, \"@\"},\n\t\t{true, \"XYZ\"},\n\t\t{false, \"XYZ!\"},\n\t\t{true, \"79068\"},\n\t\t{true, \"+123 ABC$\"},\n\t\t{false, \"\\x01\"},\n\t\t{false, \"\\x7F\"},\n\t\t{false, \"\\x80\"},\n\t\t{false, \"\\xC0\"},\n\t\t{false, \"\\xFF\"},\n\t};\n\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\tassert(qrcodegen_isAlphanumeric(cases[i].text) == cases[i].answer);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testIsNumeric(void) {\n\tstruct TestCase {\n\t\tbool answer;\n\t\tconst char *text;\n\t};\n\tconst struct TestCase cases[] = {\n\t\t{true, \"\"},\n\t\t{true, \"0\"},\n\t\t{false, \"A\"},\n\t\t{false, \"a\"},\n\t\t{false, \" \"},\n\t\t{false, \".\"},\n\t\t{false, \"*\"},\n\t\t{false, \",\"},\n\t\t{false, \"|\"},\n\t\t{false, \"@\"},\n\t\t{false, \"XYZ\"},\n\t\t{false, \"XYZ!\"},\n\t\t{true, \"79068\"},\n\t\t{false, \"+123 ABC$\"},\n\t\t{false, \"\\x01\"},\n\t\t{false, \"\\x7F\"},\n\t\t{false, \"\\x80\"},\n\t\t{false, \"\\xC0\"},\n\t\t{false, \"\\xFF\"},\n\t};\n\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\tassert(qrcodegen_isNumeric(cases[i].text) == cases[i].answer);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testCalcSegmentBufferSize(void) {\n\t{\n\t\tconst size_t cases[][2] = {\n\t\t\t{0, 0},\n\t\t\t{1, 1},\n\t\t\t{2, 1},\n\t\t\t{3, 2},\n\t\t\t{4, 2},\n\t\t\t{5, 3},\n\t\t\t{6, 3},\n\t\t\t{1472, 614},\n\t\t\t{2097, 874},\n\t\t\t{5326, 2220},\n\t\t\t{9828, 4095},\n\t\t\t{9829, 4096},\n\t\t\t{9830, 4096},\n\t\t\t{9831, SIZE_MAX},\n\t\t\t{9832, SIZE_MAX},\n\t\t\t{12000, SIZE_MAX},\n\t\t\t{28453, SIZE_MAX},\n\t\t\t{55555, SIZE_MAX},\n\t\t\t{SIZE_MAX / 6, SIZE_MAX},\n\t\t\t{SIZE_MAX / 4, SIZE_MAX},\n\t\t\t{SIZE_MAX / 2, SIZE_MAX},\n\t\t\t{SIZE_MAX / 1, SIZE_MAX},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\t\tassert(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, cases[i][0]) == cases[i][1]);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tconst size_t cases[][2] = {\n\t\t\t{0, 0},\n\t\t\t{1, 1},\n\t\t\t{2, 2},\n\t\t\t{3, 3},\n\t\t\t{4, 3},\n\t\t\t{5, 4},\n\t\t\t{6, 5},\n\t\t\t{1472, 1012},\n\t\t\t{2097, 1442},\n\t\t\t{5326, 3662},\n\t\t\t{5955, 4095},\n\t\t\t{5956, 4095},\n\t\t\t{5957, 4096},\n\t\t\t{5958, SIZE_MAX},\n\t\t\t{5959, SIZE_MAX},\n\t\t\t{12000, SIZE_MAX},\n\t\t\t{28453, SIZE_MAX},\n\t\t\t{55555, SIZE_MAX},\n\t\t\t{SIZE_MAX / 10, SIZE_MAX},\n\t\t\t{SIZE_MAX / 8, SIZE_MAX},\n\t\t\t{SIZE_MAX / 5, SIZE_MAX},\n\t\t\t{SIZE_MAX / 2, SIZE_MAX},\n\t\t\t{SIZE_MAX / 1, SIZE_MAX},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\t\tassert(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, cases[i][0]) == cases[i][1]);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tconst size_t cases[][2] = {\n\t\t\t{0, 0},\n\t\t\t{1, 1},\n\t\t\t{2, 2},\n\t\t\t{3, 3},\n\t\t\t{1472, 1472},\n\t\t\t{2097, 2097},\n\t\t\t{4094, 4094},\n\t\t\t{4095, 4095},\n\t\t\t{4096, SIZE_MAX},\n\t\t\t{4097, SIZE_MAX},\n\t\t\t{5957, SIZE_MAX},\n\t\t\t{12000, SIZE_MAX},\n\t\t\t{28453, SIZE_MAX},\n\t\t\t{55555, SIZE_MAX},\n\t\t\t{SIZE_MAX / 16 + 1, SIZE_MAX},\n\t\t\t{SIZE_MAX / 14, SIZE_MAX},\n\t\t\t{SIZE_MAX / 9, SIZE_MAX},\n\t\t\t{SIZE_MAX / 7, SIZE_MAX},\n\t\t\t{SIZE_MAX / 4, SIZE_MAX},\n\t\t\t{SIZE_MAX / 3, SIZE_MAX},\n\t\t\t{SIZE_MAX / 2, SIZE_MAX},\n\t\t\t{SIZE_MAX / 1, SIZE_MAX},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\t\tassert(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_BYTE, cases[i][0]) == cases[i][1]);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tconst size_t cases[][2] = {\n\t\t\t{0, 0},\n\t\t\t{1, 2},\n\t\t\t{2, 4},\n\t\t\t{3, 5},\n\t\t\t{1472, 2392},\n\t\t\t{2097, 3408},\n\t\t\t{2519, 4094},\n\t\t\t{2520, 4095},\n\t\t\t{2521, SIZE_MAX},\n\t\t\t{5957, SIZE_MAX},\n\t\t\t{2522, SIZE_MAX},\n\t\t\t{12000, SIZE_MAX},\n\t\t\t{28453, SIZE_MAX},\n\t\t\t{55555, SIZE_MAX},\n\t\t\t{SIZE_MAX / 13 + 1, SIZE_MAX},\n\t\t\t{SIZE_MAX / 12, SIZE_MAX},\n\t\t\t{SIZE_MAX / 9, SIZE_MAX},\n\t\t\t{SIZE_MAX / 4, SIZE_MAX},\n\t\t\t{SIZE_MAX / 3, SIZE_MAX},\n\t\t\t{SIZE_MAX / 2, SIZE_MAX},\n\t\t\t{SIZE_MAX / 1, SIZE_MAX},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {\n\t\t\tassert(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_KANJI, cases[i][0]) == cases[i][1]);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tassert(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ECI, 0) == 3);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testCalcSegmentBitLength(void) {\n\tstruct TestCase {\n\t\tsize_t numChars;\n\t\tint result;\n\t};\n\t{\n\t\tconst struct TestCase CASES[] = {\n\t\t\t{0, 0},\n\t\t\t{1, 4},\n\t\t\t{2, 7},\n\t\t\t{3, 10},\n\t\t\t{4, 14},\n\t\t\t{5, 17},\n\t\t\t{6, 20},\n\t\t\t{1472, 4907},\n\t\t\t{2097, 6990},\n\t\t\t{5326, 17754},\n\t\t\t{9828, 32760},\n\t\t\t{9829, 32764},\n\t\t\t{9830, 32767},\n\t\t\t{9831, -1},\n\t\t\t{9832, -1},\n\t\t\t{12000, -1},\n\t\t\t{28453, -1},\n\t\t\t{SIZE_MAX / 6, -1},\n\t\t\t{SIZE_MAX / 3, -1},\n\t\t\t{SIZE_MAX / 2, -1},\n\t\t\t{SIZE_MAX / 1, -1},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(CASES); i++) {\n\t\t\tassert(calcSegmentBitLength(qrcodegen_Mode_NUMERIC, CASES[i].numChars) == CASES[i].result);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tconst struct TestCase CASES[] = {\n\t\t\t{0, 0},\n\t\t\t{1, 6},\n\t\t\t{2, 11},\n\t\t\t{3, 17},\n\t\t\t{4, 22},\n\t\t\t{5, 28},\n\t\t\t{6, 33},\n\t\t\t{1472, 8096},\n\t\t\t{2097, 11534},\n\t\t\t{5326, 29293},\n\t\t\t{5955, 32753},\n\t\t\t{5956, 32758},\n\t\t\t{5957, 32764},\n\t\t\t{5958, -1},\n\t\t\t{5959, -1},\n\t\t\t{12000, -1},\n\t\t\t{28453, -1},\n\t\t\t{SIZE_MAX / 10, -1},\n\t\t\t{SIZE_MAX / 5, -1},\n\t\t\t{SIZE_MAX / 2, -1},\n\t\t\t{SIZE_MAX / 1, -1},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(CASES); i++) {\n\t\t\tassert(calcSegmentBitLength(qrcodegen_Mode_ALPHANUMERIC, CASES[i].numChars) == CASES[i].result);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tconst struct TestCase CASES[] = {\n\t\t\t{0, 0},\n\t\t\t{1, 8},\n\t\t\t{2, 16},\n\t\t\t{3, 24},\n\t\t\t{1472, 11776},\n\t\t\t{2097, 16776},\n\t\t\t{4094, 32752},\n\t\t\t{4095, 32760},\n\t\t\t{4096, -1},\n\t\t\t{4097, -1},\n\t\t\t{5957, -1},\n\t\t\t{12000, -1},\n\t\t\t{28453, -1},\n\t\t\t{SIZE_MAX / 15, -1},\n\t\t\t{SIZE_MAX / 12, -1},\n\t\t\t{SIZE_MAX / 7, -1},\n\t\t\t{SIZE_MAX / 3, -1},\n\t\t\t{SIZE_MAX / 1, -1},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(CASES); i++) {\n\t\t\tassert(calcSegmentBitLength(qrcodegen_Mode_BYTE, CASES[i].numChars) == CASES[i].result);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tconst struct TestCase CASES[] = {\n\t\t\t{0, 0},\n\t\t\t{1, 13},\n\t\t\t{2, 26},\n\t\t\t{3, 39},\n\t\t\t{1472, 19136},\n\t\t\t{2097, 27261},\n\t\t\t{2519, 32747},\n\t\t\t{2520, 32760},\n\t\t\t{2521, -1},\n\t\t\t{5957, -1},\n\t\t\t{2522, -1},\n\t\t\t{12000, -1},\n\t\t\t{28453, -1},\n\t\t\t{SIZE_MAX / 25, -1},\n\t\t\t{SIZE_MAX / 20, -1},\n\t\t\t{SIZE_MAX / 11, -1},\n\t\t\t{SIZE_MAX / 4, -1},\n\t\t\t{SIZE_MAX / 2, -1},\n\t\t\t{SIZE_MAX / 1, -1},\n\t\t};\n\t\tfor (size_t i = 0; i < ARRAY_LENGTH(CASES); i++) {\n\t\t\tassert(calcSegmentBitLength(qrcodegen_Mode_KANJI, CASES[i].numChars) == CASES[i].result);\n\t\t\tnumTestCases++;\n\t\t}\n\t}\n\t{\n\t\tassert(calcSegmentBitLength(qrcodegen_Mode_ECI, 0) == 24);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testMakeBytes(void) {\n\t{\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeBytes(NULL, 0, NULL);\n\t\tassert(seg.mode == qrcodegen_Mode_BYTE);\n\t\tassert(seg.numChars == 0);\n\t\tassert(seg.bitLength == 0);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tconst uint8_t data[] = {0x00};\n\t\tuint8_t buf[1];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeBytes(data, 1, buf);\n\t\tassert(seg.numChars == 1);\n\t\tassert(seg.bitLength == 8);\n\t\tassert(seg.data[0] == 0x00);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tconst uint8_t data[] = {0xEF, 0xBB, 0xBF};\n\t\tuint8_t buf[3];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeBytes(data, 3, buf);\n\t\tassert(seg.numChars == 3);\n\t\tassert(seg.bitLength == 24);\n\t\tassert(seg.data[0] == 0xEF);\n\t\tassert(seg.data[1] == 0xBB);\n\t\tassert(seg.data[2] == 0xBF);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testMakeNumeric(void) {\n\t{\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeNumeric(\"\", NULL);\n\t\tassert(seg.mode == qrcodegen_Mode_NUMERIC);\n\t\tassert(seg.numChars == 0);\n\t\tassert(seg.bitLength == 0);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[1];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeNumeric(\"9\", buf);\n\t\tassert(seg.numChars == 1);\n\t\tassert(seg.bitLength == 4);\n\t\tassert(seg.data[0] == 0x90);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[1];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeNumeric(\"81\", buf);\n\t\tassert(seg.numChars == 2);\n\t\tassert(seg.bitLength == 7);\n\t\tassert(seg.data[0] == 0xA2);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[2];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeNumeric(\"673\", buf);\n\t\tassert(seg.numChars == 3);\n\t\tassert(seg.bitLength == 10);\n\t\tassert(seg.data[0] == 0xA8);\n\t\tassert(seg.data[1] == 0x40);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[5];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeNumeric(\"3141592653\", buf);\n\t\tassert(seg.numChars == 10);\n\t\tassert(seg.bitLength == 34);\n\t\tassert(seg.data[0] == 0x4E);\n\t\tassert(seg.data[1] == 0x89);\n\t\tassert(seg.data[2] == 0xF4);\n\t\tassert(seg.data[3] == 0x24);\n\t\tassert(seg.data[4] == 0xC0);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testMakeAlphanumeric(void) {\n\t{\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeAlphanumeric(\"\", NULL);\n\t\tassert(seg.mode == qrcodegen_Mode_ALPHANUMERIC);\n\t\tassert(seg.numChars == 0);\n\t\tassert(seg.bitLength == 0);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[1];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeAlphanumeric(\"A\", buf);\n\t\tassert(seg.numChars == 1);\n\t\tassert(seg.bitLength == 6);\n\t\tassert(seg.data[0] == 0x28);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[2];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeAlphanumeric(\"%:\", buf);\n\t\tassert(seg.numChars == 2);\n\t\tassert(seg.bitLength == 11);\n\t\tassert(seg.data[0] == 0xDB);\n\t\tassert(seg.data[1] == 0x40);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[3];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeAlphanumeric(\"Q R\", buf);\n\t\tassert(seg.numChars == 3);\n\t\tassert(seg.bitLength == 17);\n\t\tassert(seg.data[0] == 0x96);\n\t\tassert(seg.data[1] == 0xCD);\n\t\tassert(seg.data[2] == 0x80);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testMakeEci(void) {\n\t{\n\t\tuint8_t buf[1];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeEci(127, buf);\n\t\tassert(seg.mode == qrcodegen_Mode_ECI);\n\t\tassert(seg.numChars == 0);\n\t\tassert(seg.bitLength == 8);\n\t\tassert(seg.data[0] == 0x7F);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[2];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeEci(10345, buf);\n\t\tassert(seg.numChars == 0);\n\t\tassert(seg.bitLength == 16);\n\t\tassert(seg.data[0] == 0xA8);\n\t\tassert(seg.data[1] == 0x69);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tuint8_t buf[3];\n\t\tstruct qrcodegen_Segment seg = qrcodegen_makeEci(999999, buf);\n\t\tassert(seg.numChars == 0);\n\t\tassert(seg.bitLength == 24);\n\t\tassert(seg.data[0] == 0xCF);\n\t\tassert(seg.data[1] == 0x42);\n\t\tassert(seg.data[2] == 0x3F);\n\t\tnumTestCases++;\n\t}\n}\n\n\nstatic void testGetTotalBits(void) {\n\t{\n\t\tassert(getTotalBits(NULL, 0, 1) == 0);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(NULL, 0, 40) == 0);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t{qrcodegen_Mode_BYTE, 3, NULL, 24},\n\t\t};\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 2) == 36);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 10) == 44);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 39) == 44);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t{qrcodegen_Mode_ECI, 0, NULL, 8},\n\t\t\t{qrcodegen_Mode_NUMERIC, 7, NULL, 24},\n\t\t\t{qrcodegen_Mode_ALPHANUMERIC, 1, NULL, 6},\n\t\t\t{qrcodegen_Mode_KANJI, 4, NULL, 52},\n\t\t};\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 9) == 133);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 21) == 139);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 27) == 145);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t{qrcodegen_Mode_BYTE, 4093, NULL, 32744},\n\t\t};\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 1) == -1);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 10) == 32764);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 27) == 32764);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t{qrcodegen_Mode_NUMERIC, 2047, NULL, 6824},\n\t\t\t{qrcodegen_Mode_NUMERIC, 2047, NULL, 6824},\n\t\t\t{qrcodegen_Mode_NUMERIC, 2047, NULL, 6824},\n\t\t\t{qrcodegen_Mode_NUMERIC, 2047, NULL, 6824},\n\t\t\t{qrcodegen_Mode_NUMERIC, 1617, NULL, 5390},\n\t\t};\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 1) == -1);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 10) == 32766);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 27) == -1);\n\t\tnumTestCases++;\n\t}\n\t{\n\t\tstruct qrcodegen_Segment segs[] = {\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_KANJI, 255, NULL, 3315},\n\t\t\t{qrcodegen_Mode_ALPHANUMERIC, 511, NULL, 2811},\n\t\t};\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 9) == 32767);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 26) == -1);\n\t\tnumTestCases++;\n\t\tassert(getTotalBits(segs, ARRAY_LENGTH(segs), 40) == -1);\n\t\tnumTestCases++;\n\t}\n}\n\n\n/*---- Main runner ----*/\n\nint main(void) {\n\tsrand((unsigned int)time(NULL));\n\ttestAppendBitsToBuffer();\n\ttestAddEccAndInterleave();\n\ttestGetNumDataCodewords();\n\ttestGetNumRawDataModules();\n\ttestReedSolomonComputeDivisor();\n\ttestReedSolomonComputeRemainder();\n\ttestReedSolomonMultiply();\n\ttestInitializeFunctionModulesEtc();\n\ttestGetAlignmentPatternPositions();\n\ttestGetSetModule();\n\ttestGetSetModuleRandomly();\n\ttestIsAlphanumeric();\n\ttestIsNumeric();\n\ttestCalcSegmentBufferSize();\n\ttestCalcSegmentBitLength();\n\ttestMakeBytes();\n\ttestMakeNumeric();\n\ttestMakeAlphanumeric();\n\ttestMakeEci();\n\ttestGetTotalBits();\n\tprintf(\"All %d test cases passed\\n\", numTestCases);\n\treturn EXIT_SUCCESS;\n}\n"
  },
  {
    "path": "c/qrcodegen.c",
    "content": "/* \n * QR Code generator library (C)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#include <assert.h>\n#include <limits.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"qrcodegen.h\"\n\n#ifndef QRCODEGEN_TEST\n\t#define testable static  // Keep functions private\n#else\n\t#define testable  // Expose private functions\n#endif\n\n\n/*---- Forward declarations for private functions ----*/\n\n// Regarding all public and private functions defined in this source file:\n// - They require all pointer/array arguments to be not null unless the array length is zero.\n// - They only read input scalar/array arguments, write to output pointer/array\n//   arguments, and return scalar values; they are \"pure\" functions.\n// - They don't read mutable global variables or write to any global variables.\n// - They don't perform I/O, read the clock, print to console, etc.\n// - They allocate a small and constant amount of stack memory.\n// - They don't allocate or free any memory on the heap.\n// - They don't recurse or mutually recurse. All the code\n//   could be inlined into the top-level public functions.\n// - They run in at most quadratic time with respect to input arguments.\n//   Most functions run in linear time, and some in constant time.\n//   There are no unbounded loops or non-obvious termination conditions.\n// - They are completely thread-safe if the caller does not give the\n//   same writable buffer to concurrent calls to these functions.\n\ntestable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);\n\ntestable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);\ntestable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);\ntestable int getNumRawDataModules(int ver);\n\ntestable void reedSolomonComputeDivisor(int degree, uint8_t result[]);\ntestable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,\n\tconst uint8_t generator[], int degree, uint8_t result[]);\ntestable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y);\n\ntestable void initializeFunctionModules(int version, uint8_t qrcode[]);\nstatic void drawLightFunctionModules(uint8_t qrcode[], int version);\nstatic void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]);\ntestable int getAlignmentPatternPositions(int version, uint8_t result[7]);\nstatic void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]);\n\nstatic void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]);\nstatic void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask);\nstatic long getPenaltyScore(const uint8_t qrcode[]);\nstatic int finderPenaltyCountPatterns(const int runHistory[7], int qrsize);\nstatic int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int runHistory[7], int qrsize);\nstatic void finderPenaltyAddHistory(int currentRunLength, int runHistory[7], int qrsize);\n\ntestable bool getModuleBounded(const uint8_t qrcode[], int x, int y);\ntestable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark);\ntestable void setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark);\nstatic bool getBit(int x, int i);\n\ntestable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars);\ntestable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version);\nstatic int numCharCountBits(enum qrcodegen_Mode mode, int version);\n\n\n\n/*---- Private tables of constants ----*/\n\n// The set of all legal characters in alphanumeric mode, where each character\n// value maps to the index in the string. For checking text and encoding segments.\nstatic const char *ALPHANUMERIC_CHARSET = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\n// Sentinel value for use in only some functions.\n#define LENGTH_OVERFLOW -1\n\n// For generating error correction codes.\ntestable const int8_t ECC_CODEWORDS_PER_BLOCK[4][41] = {\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t{-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\n\t{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},  // Medium\n\t{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // Quartile\n\t{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // High\n};\n\n#define qrcodegen_REED_SOLOMON_DEGREE_MAX 30  // Based on the table above\n\n// For generating error correction codes.\ntestable const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41] = {\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25},  // Low\n\t{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49},  // Medium\n\t{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68},  // Quartile\n\t{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81},  // High\n};\n\n// For automatic mask pattern selection.\nstatic const int PENALTY_N1 =  3;\nstatic const int PENALTY_N2 =  3;\nstatic const int PENALTY_N3 = 40;\nstatic const int PENALTY_N4 = 10;\n\n\n\n/*---- High-level QR Code encoding functions ----*/\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],\n\t\tenum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {\n\t\n\tsize_t textLen = strlen(text);\n\tif (textLen == 0)\n\t\treturn qrcodegen_encodeSegmentsAdvanced(NULL, 0, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);\n\tsize_t bufLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);\n\t\n\tstruct qrcodegen_Segment seg;\n\tif (qrcodegen_isNumeric(text)) {\n\t\tif (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, textLen) > bufLen)\n\t\t\tgoto fail;\n\t\tseg = qrcodegen_makeNumeric(text, tempBuffer);\n\t} else if (qrcodegen_isAlphanumeric(text)) {\n\t\tif (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, textLen) > bufLen)\n\t\t\tgoto fail;\n\t\tseg = qrcodegen_makeAlphanumeric(text, tempBuffer);\n\t} else {\n\t\tif (textLen > bufLen)\n\t\t\tgoto fail;\n\t\tfor (size_t i = 0; i < textLen; i++)\n\t\t\ttempBuffer[i] = (uint8_t)text[i];\n\t\tseg.mode = qrcodegen_Mode_BYTE;\n\t\tseg.bitLength = calcSegmentBitLength(seg.mode, textLen);\n\t\tif (seg.bitLength == LENGTH_OVERFLOW)\n\t\t\tgoto fail;\n\t\tseg.numChars = (int)textLen;\n\t\tseg.data = tempBuffer;\n\t}\n\treturn qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);\n\t\nfail:\n\tqrcode[0] = 0;  // Set size to invalid value for safety\n\treturn false;\n}\n\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],\n\t\tenum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {\n\t\n\tstruct qrcodegen_Segment seg;\n\tseg.mode = qrcodegen_Mode_BYTE;\n\tseg.bitLength = calcSegmentBitLength(seg.mode, dataLen);\n\tif (seg.bitLength == LENGTH_OVERFLOW) {\n\t\tqrcode[0] = 0;  // Set size to invalid value for safety\n\t\treturn false;\n\t}\n\tseg.numChars = (int)dataLen;\n\tseg.data = dataAndTemp;\n\treturn qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, dataAndTemp, qrcode);\n}\n\n\n// Appends the given number of low-order bits of the given value to the given byte-based\n// bit buffer, increasing the bit length. Requires 0 <= numBits <= 16 and val < 2^numBits.\ntestable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {\n\tassert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0);\n\tfor (int i = numBits - 1; i >= 0; i--, (*bitLen)++)\n\t\tbuffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7));\n}\n\n\n\n/*---- Low-level QR Code encoding functions ----*/\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len,\n\t\tenum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]) {\n\treturn qrcodegen_encodeSegmentsAdvanced(segs, len, ecl,\n\t\tqrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true, tempBuffer, qrcode);\n}\n\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,\n\t\tint minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]) {\n\tassert(segs != NULL || len == 0);\n\tassert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);\n\tassert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);\n\t\n\t// Find the minimal version number to use\n\tint version, dataUsedBits;\n\tfor (version = minVersion; ; version++) {\n\t\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\tdataUsedBits = getTotalBits(segs, len, version);\n\t\tif (dataUsedBits != LENGTH_OVERFLOW && dataUsedBits <= dataCapacityBits)\n\t\t\tbreak;  // This version number is found to be suitable\n\t\tif (version >= maxVersion) {  // All versions in the range could not fit the given data\n\t\t\tqrcode[0] = 0;  // Set size to invalid value for safety\n\t\t\treturn false;\n\t\t}\n\t}\n\tassert(dataUsedBits != LENGTH_OVERFLOW);\n\t\n\t// Increase the error correction level while the data still fits in the current version number\n\tfor (int i = (int)qrcodegen_Ecc_MEDIUM; i <= (int)qrcodegen_Ecc_HIGH; i++) {  // From low to high\n\t\tif (boostEcl && dataUsedBits <= getNumDataCodewords(version, (enum qrcodegen_Ecc)i) * 8)\n\t\t\tecl = (enum qrcodegen_Ecc)i;\n\t}\n\t\n\t// Concatenate all segments to create the data bit string\n\tmemset(qrcode, 0, (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0]));\n\tint bitLen = 0;\n\tfor (size_t i = 0; i < len; i++) {\n\t\tconst struct qrcodegen_Segment *seg = &segs[i];\n\t\tappendBitsToBuffer((unsigned int)seg->mode, 4, qrcode, &bitLen);\n\t\tappendBitsToBuffer((unsigned int)seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen);\n\t\tfor (int j = 0; j < seg->bitLength; j++) {\n\t\t\tint bit = (seg->data[j >> 3] >> (7 - (j & 7))) & 1;\n\t\t\tappendBitsToBuffer((unsigned int)bit, 1, qrcode, &bitLen);\n\t\t}\n\t}\n\tassert(bitLen == dataUsedBits);\n\t\n\t// Add terminator and pad up to a byte if applicable\n\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;\n\tassert(bitLen <= dataCapacityBits);\n\tint terminatorBits = dataCapacityBits - bitLen;\n\tif (terminatorBits > 4)\n\t\tterminatorBits = 4;\n\tappendBitsToBuffer(0, terminatorBits, qrcode, &bitLen);\n\tappendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen);\n\tassert(bitLen % 8 == 0);\n\t\n\t// Pad with alternating bytes until data capacity is reached\n\tfor (uint8_t padByte = 0xEC; bitLen < dataCapacityBits; padByte ^= 0xEC ^ 0x11)\n\t\tappendBitsToBuffer(padByte, 8, qrcode, &bitLen);\n\t\n\t// Compute ECC, draw modules\n\taddEccAndInterleave(qrcode, version, ecl, tempBuffer);\n\tinitializeFunctionModules(version, qrcode);\n\tdrawCodewords(tempBuffer, getNumRawDataModules(version) / 8, qrcode);\n\tdrawLightFunctionModules(qrcode, version);\n\tinitializeFunctionModules(version, tempBuffer);\n\t\n\t// Do masking\n\tif (mask == qrcodegen_Mask_AUTO) {  // Automatically choose best mask\n\t\tlong minPenalty = LONG_MAX;\n\t\tfor (int i = 0; i < 8; i++) {\n\t\t\tenum qrcodegen_Mask msk = (enum qrcodegen_Mask)i;\n\t\t\tapplyMask(tempBuffer, qrcode, msk);\n\t\t\tdrawFormatBits(ecl, msk, qrcode);\n\t\t\tlong penalty = getPenaltyScore(qrcode);\n\t\t\tif (penalty < minPenalty) {\n\t\t\t\tmask = msk;\n\t\t\t\tminPenalty = penalty;\n\t\t\t}\n\t\t\tapplyMask(tempBuffer, qrcode, msk);  // Undoes the mask due to XOR\n\t\t}\n\t}\n\tassert(0 <= (int)mask && (int)mask <= 7);\n\tapplyMask(tempBuffer, qrcode, mask);  // Apply the final choice of mask\n\tdrawFormatBits(ecl, mask, qrcode);  // Overwrite old format bits\n\treturn true;\n}\n\n\n\n/*---- Error correction code generation functions ----*/\n\n// Appends error correction bytes to each block of the given data array, then interleaves\n// bytes from the blocks and stores them in the result array. data[0 : dataLen] contains\n// the input data. data[dataLen : rawCodewords] is used as a temporary work area and will\n// be clobbered by this function. The final answer is stored in result[0 : rawCodewords].\ntestable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]) {\n\t// Calculate parameter numbers\n\tassert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);\n\tint numBlocks = NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version];\n\tint blockEccLen = ECC_CODEWORDS_PER_BLOCK  [(int)ecl][version];\n\tint rawCodewords = getNumRawDataModules(version) / 8;\n\tint dataLen = getNumDataCodewords(version, ecl);\n\tint numShortBlocks = numBlocks - rawCodewords % numBlocks;\n\tint shortBlockDataLen = rawCodewords / numBlocks - blockEccLen;\n\t\n\t// Split data into blocks, calculate ECC, and interleave\n\t// (not concatenate) the bytes into a single sequence\n\tuint8_t rsdiv[qrcodegen_REED_SOLOMON_DEGREE_MAX];\n\treedSolomonComputeDivisor(blockEccLen, rsdiv);\n\tconst uint8_t *dat = data;\n\tfor (int i = 0; i < numBlocks; i++) {\n\t\tint datLen = shortBlockDataLen + (i < numShortBlocks ? 0 : 1);\n\t\tuint8_t *ecc = &data[dataLen];  // Temporary storage\n\t\treedSolomonComputeRemainder(dat, datLen, rsdiv, blockEccLen, ecc);\n\t\tfor (int j = 0, k = i; j < datLen; j++, k += numBlocks) {  // Copy data\n\t\t\tif (j == shortBlockDataLen)\n\t\t\t\tk -= numShortBlocks;\n\t\t\tresult[k] = dat[j];\n\t\t}\n\t\tfor (int j = 0, k = dataLen + i; j < blockEccLen; j++, k += numBlocks)  // Copy ECC\n\t\t\tresult[k] = ecc[j];\n\t\tdat += datLen;\n\t}\n}\n\n\n// Returns the number of 8-bit codewords that can be used for storing data (not ECC),\n// for the given version number and error correction level. The result is in the range [9, 2956].\ntestable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {\n\tint v = version, e = (int)ecl;\n\tassert(0 <= e && e < 4);\n\treturn getNumRawDataModules(v) / 8\n\t\t- ECC_CODEWORDS_PER_BLOCK    [e][v]\n\t\t* NUM_ERROR_CORRECTION_BLOCKS[e][v];\n}\n\n\n// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\ntestable int getNumRawDataModules(int ver) {\n\tassert(qrcodegen_VERSION_MIN <= ver && ver <= qrcodegen_VERSION_MAX);\n\tint result = (16 * ver + 128) * ver + 64;\n\tif (ver >= 2) {\n\t\tint numAlign = ver / 7 + 2;\n\t\tresult -= (25 * numAlign - 10) * numAlign - 55;\n\t\tif (ver >= 7)\n\t\t\tresult -= 36;\n\t}\n\tassert(208 <= result && result <= 29648);\n\treturn result;\n}\n\n\n\n/*---- Reed-Solomon ECC generator functions ----*/\n\n// Computes a Reed-Solomon ECC generator polynomial for the given degree, storing in result[0 : degree].\n// This could be implemented as a lookup table over all possible parameter values, instead of as an algorithm.\ntestable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {\n\tassert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);\n\t// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.\n\tmemset(result, 0, (size_t)degree * sizeof(result[0]));\n\tresult[degree - 1] = 1;  // Start off with the monomial x^0\n\t\n\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t// drop the highest monomial term which is always 1x^degree.\n\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\tuint8_t root = 1;\n\tfor (int i = 0; i < degree; i++) {\n\t\t// Multiply the current product by (x - r^i)\n\t\tfor (int j = 0; j < degree; j++) {\n\t\t\tresult[j] = reedSolomonMultiply(result[j], root);\n\t\t\tif (j + 1 < degree)\n\t\t\t\tresult[j] ^= result[j + 1];\n\t\t}\n\t\troot = reedSolomonMultiply(root, 0x02);\n\t}\n}\n\n\n// Computes the Reed-Solomon error correction codeword for the given data and divisor polynomials.\n// The remainder when data[0 : dataLen] is divided by divisor[0 : degree] is stored in result[0 : degree].\n// All polynomials are in big endian, and the generator has an implicit leading 1 term.\ntestable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,\n\t\tconst uint8_t generator[], int degree, uint8_t result[]) {\n\tassert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);\n\tmemset(result, 0, (size_t)degree * sizeof(result[0]));\n\tfor (int i = 0; i < dataLen; i++) {  // Polynomial division\n\t\tuint8_t factor = data[i] ^ result[0];\n\t\tmemmove(&result[0], &result[1], (size_t)(degree - 1) * sizeof(result[0]));\n\t\tresult[degree - 1] = 0;\n\t\tfor (int j = 0; j < degree; j++)\n\t\t\tresult[j] ^= reedSolomonMultiply(generator[j], factor);\n\t}\n}\n\n#undef qrcodegen_REED_SOLOMON_DEGREE_MAX\n\n\n// Returns the product of the two given field elements modulo GF(2^8/0x11D).\n// All inputs are valid. This could be implemented as a 256*256 lookup table.\ntestable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {\n\t// Russian peasant multiplication\n\tuint8_t z = 0;\n\tfor (int i = 7; i >= 0; i--) {\n\t\tz = (uint8_t)((z << 1) ^ ((z >> 7) * 0x11D));\n\t\tz ^= ((y >> i) & 1) * x;\n\t}\n\treturn z;\n}\n\n\n\n/*---- Drawing function modules ----*/\n\n// Clears the given QR Code grid with light modules for the given\n// version's size, then marks every function module as dark.\ntestable void initializeFunctionModules(int version, uint8_t qrcode[]) {\n\t// Initialize QR Code\n\tint qrsize = version * 4 + 17;\n\tmemset(qrcode, 0, (size_t)((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));\n\tqrcode[0] = (uint8_t)qrsize;\n\t\n\t// Fill horizontal and vertical timing patterns\n\tfillRectangle(6, 0, 1, qrsize, qrcode);\n\tfillRectangle(0, 6, qrsize, 1, qrcode);\n\t\n\t// Fill 3 finder patterns (all corners except bottom right) and format bits\n\tfillRectangle(0, 0, 9, 9, qrcode);\n\tfillRectangle(qrsize - 8, 0, 8, 9, qrcode);\n\tfillRectangle(0, qrsize - 8, 9, 8, qrcode);\n\t\n\t// Fill numerous alignment patterns\n\tuint8_t alignPatPos[7];\n\tint numAlign = getAlignmentPatternPositions(version, alignPatPos);\n\tfor (int i = 0; i < numAlign; i++) {\n\t\tfor (int j = 0; j < numAlign; j++) {\n\t\t\t// Don't draw on the three finder corners\n\t\t\tif (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))\n\t\t\t\tfillRectangle(alignPatPos[i] - 2, alignPatPos[j] - 2, 5, 5, qrcode);\n\t\t}\n\t}\n\t\n\t// Fill version blocks\n\tif (version >= 7) {\n\t\tfillRectangle(qrsize - 11, 0, 3, 6, qrcode);\n\t\tfillRectangle(0, qrsize - 11, 6, 3, qrcode);\n\t}\n}\n\n\n// Draws light function modules and possibly some dark modules onto the given QR Code, without changing\n// non-function modules. This does not draw the format bits. This requires all function modules to be previously\n// marked dark (namely by initializeFunctionModules()), because this may skip redrawing dark function modules.\nstatic void drawLightFunctionModules(uint8_t qrcode[], int version) {\n\t// Draw horizontal and vertical timing patterns\n\tint qrsize = qrcodegen_getSize(qrcode);\n\tfor (int i = 7; i < qrsize - 7; i += 2) {\n\t\tsetModuleBounded(qrcode, 6, i, false);\n\t\tsetModuleBounded(qrcode, i, 6, false);\n\t}\n\t\n\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\tfor (int dy = -4; dy <= 4; dy++) {\n\t\tfor (int dx = -4; dx <= 4; dx++) {\n\t\t\tint dist = abs(dx);\n\t\t\tif (abs(dy) > dist)\n\t\t\t\tdist = abs(dy);\n\t\t\tif (dist == 2 || dist == 4) {\n\t\t\t\tsetModuleUnbounded(qrcode, 3 + dx, 3 + dy, false);\n\t\t\t\tsetModuleUnbounded(qrcode, qrsize - 4 + dx, 3 + dy, false);\n\t\t\t\tsetModuleUnbounded(qrcode, 3 + dx, qrsize - 4 + dy, false);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// Draw numerous alignment patterns\n\tuint8_t alignPatPos[7];\n\tint numAlign = getAlignmentPatternPositions(version, alignPatPos);\n\tfor (int i = 0; i < numAlign; i++) {\n\t\tfor (int j = 0; j < numAlign; j++) {\n\t\t\tif ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))\n\t\t\t\tcontinue;  // Don't draw on the three finder corners\n\t\t\tfor (int dy = -1; dy <= 1; dy++) {\n\t\t\t\tfor (int dx = -1; dx <= 1; dx++)\n\t\t\t\t\tsetModuleBounded(qrcode, alignPatPos[i] + dx, alignPatPos[j] + dy, dx == 0 && dy == 0);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// Draw version blocks\n\tif (version >= 7) {\n\t\t// Calculate error correction code and pack bits\n\t\tint rem = version;  // version is uint6, in the range [7, 40]\n\t\tfor (int i = 0; i < 12; i++)\n\t\t\trem = (rem << 1) ^ ((rem >> 11) * 0x1F25);\n\t\tlong bits = (long)version << 12 | rem;  // uint18\n\t\tassert(bits >> 18 == 0);\n\t\t\n\t\t// Draw two copies\n\t\tfor (int i = 0; i < 6; i++) {\n\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\tint k = qrsize - 11 + j;\n\t\t\t\tsetModuleBounded(qrcode, k, i, (bits & 1) != 0);\n\t\t\t\tsetModuleBounded(qrcode, i, k, (bits & 1) != 0);\n\t\t\t\tbits >>= 1;\n\t\t\t}\n\t\t}\n\t}\n}\n\n\n// Draws two copies of the format bits (with its own error correction code) based\n// on the given mask and error correction level. This always draws all modules of\n// the format bits, unlike drawLightFunctionModules() which might skip dark modules.\nstatic void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]) {\n\t// Calculate error correction code and pack bits\n\tassert(0 <= (int)mask && (int)mask <= 7);\n\tstatic const int table[] = {1, 0, 3, 2};\n\tint data = table[(int)ecl] << 3 | (int)mask;  // errCorrLvl is uint2, mask is uint3\n\tint rem = data;\n\tfor (int i = 0; i < 10; i++)\n\t\trem = (rem << 1) ^ ((rem >> 9) * 0x537);\n\tint bits = (data << 10 | rem) ^ 0x5412;  // uint15\n\tassert(bits >> 15 == 0);\n\t\n\t// Draw first copy\n\tfor (int i = 0; i <= 5; i++)\n\t\tsetModuleBounded(qrcode, 8, i, getBit(bits, i));\n\tsetModuleBounded(qrcode, 8, 7, getBit(bits, 6));\n\tsetModuleBounded(qrcode, 8, 8, getBit(bits, 7));\n\tsetModuleBounded(qrcode, 7, 8, getBit(bits, 8));\n\tfor (int i = 9; i < 15; i++)\n\t\tsetModuleBounded(qrcode, 14 - i, 8, getBit(bits, i));\n\t\n\t// Draw second copy\n\tint qrsize = qrcodegen_getSize(qrcode);\n\tfor (int i = 0; i < 8; i++)\n\t\tsetModuleBounded(qrcode, qrsize - 1 - i, 8, getBit(bits, i));\n\tfor (int i = 8; i < 15; i++)\n\t\tsetModuleBounded(qrcode, 8, qrsize - 15 + i, getBit(bits, i));\n\tsetModuleBounded(qrcode, 8, qrsize - 8, true);  // Always dark\n}\n\n\n// Calculates and stores an ascending list of positions of alignment patterns\n// for this version number, returning the length of the list (in the range [0,7]).\n// Each position is in the range [0,177), and are used on both the x and y axes.\n// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.\ntestable int getAlignmentPatternPositions(int version, uint8_t result[7]) {\n\tif (version == 1)\n\t\treturn 0;\n\tint numAlign = version / 7 + 2;\n\tint step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;\n\tfor (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)\n\t\tresult[i] = (uint8_t)pos;\n\tresult[0] = 6;\n\treturn numAlign;\n}\n\n\n// Sets every module in the range [left : left + width] * [top : top + height] to dark.\nstatic void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]) {\n\tfor (int dy = 0; dy < height; dy++) {\n\t\tfor (int dx = 0; dx < width; dx++)\n\t\t\tsetModuleBounded(qrcode, left + dx, top + dy, true);\n\t}\n}\n\n\n\n/*---- Drawing data modules and masking ----*/\n\n// Draws the raw codewords (including data and ECC) onto the given QR Code. This requires the initial state of\n// the QR Code to be dark at function modules and light at codeword modules (including unused remainder bits).\nstatic void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]) {\n\tint qrsize = qrcodegen_getSize(qrcode);\n\tint i = 0;  // Bit index into the data\n\t// Do the funny zigzag scan\n\tfor (int right = qrsize - 1; right >= 1; right -= 2) {  // Index of right column in each column pair\n\t\tif (right == 6)\n\t\t\tright = 5;\n\t\tfor (int vert = 0; vert < qrsize; vert++) {  // Vertical counter\n\t\t\tfor (int j = 0; j < 2; j++) {\n\t\t\t\tint x = right - j;  // Actual x coordinate\n\t\t\t\tbool upward = ((right + 1) & 2) == 0;\n\t\t\t\tint y = upward ? qrsize - 1 - vert : vert;  // Actual y coordinate\n\t\t\t\tif (!getModuleBounded(qrcode, x, y) && i < dataLen * 8) {\n\t\t\t\t\tbool dark = getBit(data[i >> 3], 7 - (i & 7));\n\t\t\t\t\tsetModuleBounded(qrcode, x, y, dark);\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\t// If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t// 0/false/light by the constructor and are left unchanged by this method\n\t\t\t}\n\t\t}\n\t}\n\tassert(i == dataLen * 8);\n}\n\n\n// XORs the codeword modules in this QR Code with the given mask pattern\n// and given pattern of function modules. The codeword bits must be drawn\n// before masking. Due to the arithmetic of XOR, calling applyMask() with\n// the same mask value a second time will undo the mask. A final well-formed\n// QR Code needs exactly one (not zero, two, etc.) mask applied.\nstatic void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask) {\n\tassert(0 <= (int)mask && (int)mask <= 7);  // Disallows qrcodegen_Mask_AUTO\n\tint qrsize = qrcodegen_getSize(qrcode);\n\tfor (int y = 0; y < qrsize; y++) {\n\t\tfor (int x = 0; x < qrsize; x++) {\n\t\t\tif (getModuleBounded(functionModules, x, y))\n\t\t\t\tcontinue;\n\t\t\tbool invert;\n\t\t\tswitch ((int)mask) {\n\t\t\t\tcase 0:  invert = (x + y) % 2 == 0;                    break;\n\t\t\t\tcase 1:  invert = y % 2 == 0;                          break;\n\t\t\t\tcase 2:  invert = x % 3 == 0;                          break;\n\t\t\t\tcase 3:  invert = (x + y) % 3 == 0;                    break;\n\t\t\t\tcase 4:  invert = (x / 3 + y / 2) % 2 == 0;            break;\n\t\t\t\tcase 5:  invert = x * y % 2 + x * y % 3 == 0;          break;\n\t\t\t\tcase 6:  invert = (x * y % 2 + x * y % 3) % 2 == 0;    break;\n\t\t\t\tcase 7:  invert = ((x + y) % 2 + x * y % 3) % 2 == 0;  break;\n\t\t\t\tdefault:  assert(false);  return;\n\t\t\t}\n\t\t\tbool val = getModuleBounded(qrcode, x, y);\n\t\t\tsetModuleBounded(qrcode, x, y, val ^ invert);\n\t\t}\n\t}\n}\n\n\n// Calculates and returns the penalty score based on state of the given QR Code's current modules.\n// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\nstatic long getPenaltyScore(const uint8_t qrcode[]) {\n\tint qrsize = qrcodegen_getSize(qrcode);\n\tlong result = 0;\n\t\n\t// Adjacent modules in row having same color, and finder-like patterns\n\tfor (int y = 0; y < qrsize; y++) {\n\t\tbool runColor = false;\n\t\tint runX = 0;\n\t\tint runHistory[7] = {0};\n\t\tfor (int x = 0; x < qrsize; x++) {\n\t\t\tif (getModuleBounded(qrcode, x, y) == runColor) {\n\t\t\t\trunX++;\n\t\t\t\tif (runX == 5)\n\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\telse if (runX > 5)\n\t\t\t\t\tresult++;\n\t\t\t} else {\n\t\t\t\tfinderPenaltyAddHistory(runX, runHistory, qrsize);\n\t\t\t\tif (!runColor)\n\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;\n\t\t\t\trunColor = getModuleBounded(qrcode, x, y);\n\t\t\t\trunX = 1;\n\t\t\t}\n\t\t}\n\t\tresult += finderPenaltyTerminateAndCount(runColor, runX, runHistory, qrsize) * PENALTY_N3;\n\t}\n\t// Adjacent modules in column having same color, and finder-like patterns\n\tfor (int x = 0; x < qrsize; x++) {\n\t\tbool runColor = false;\n\t\tint runY = 0;\n\t\tint runHistory[7] = {0};\n\t\tfor (int y = 0; y < qrsize; y++) {\n\t\t\tif (getModuleBounded(qrcode, x, y) == runColor) {\n\t\t\t\trunY++;\n\t\t\t\tif (runY == 5)\n\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\telse if (runY > 5)\n\t\t\t\t\tresult++;\n\t\t\t} else {\n\t\t\t\tfinderPenaltyAddHistory(runY, runHistory, qrsize);\n\t\t\t\tif (!runColor)\n\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;\n\t\t\t\trunColor = getModuleBounded(qrcode, x, y);\n\t\t\t\trunY = 1;\n\t\t\t}\n\t\t}\n\t\tresult += finderPenaltyTerminateAndCount(runColor, runY, runHistory, qrsize) * PENALTY_N3;\n\t}\n\t\n\t// 2*2 blocks of modules having same color\n\tfor (int y = 0; y < qrsize - 1; y++) {\n\t\tfor (int x = 0; x < qrsize - 1; x++) {\n\t\t\tbool  color = getModuleBounded(qrcode, x, y);\n\t\t\tif (  color == getModuleBounded(qrcode, x + 1, y) &&\n\t\t\t      color == getModuleBounded(qrcode, x, y + 1) &&\n\t\t\t      color == getModuleBounded(qrcode, x + 1, y + 1))\n\t\t\t\tresult += PENALTY_N2;\n\t\t}\n\t}\n\t\n\t// Balance of dark and light modules\n\tint dark = 0;\n\tfor (int y = 0; y < qrsize; y++) {\n\t\tfor (int x = 0; x < qrsize; x++) {\n\t\t\tif (getModuleBounded(qrcode, x, y))\n\t\t\t\tdark++;\n\t\t}\n\t}\n\tint total = qrsize * qrsize;  // Note that size is odd, so dark/total != 1/2\n\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\tint k = (int)((labs(dark * 20L - total * 10L) + total - 1) / total) - 1;\n\tassert(0 <= k && k <= 9);\n\tresult += k * PENALTY_N4;\n\tassert(0 <= result && result <= 2568888L);  // Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\treturn result;\n}\n\n\n// Can only be called immediately after a light run is added, and\n// returns either 0, 1, or 2. A helper function for getPenaltyScore().\nstatic int finderPenaltyCountPatterns(const int runHistory[7], int qrsize) {\n\tint n = runHistory[1];\n\tassert(n <= qrsize * 3);  (void)qrsize;\n\tbool core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;\n\t// The maximum QR Code size is 177, hence the dark run length n <= 177.\n\t// Arithmetic is promoted to int, so n*4 will not overflow.\n\treturn (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)\n\t     + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);\n}\n\n\n// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().\nstatic int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int runHistory[7], int qrsize) {\n\tif (currentRunColor) {  // Terminate dark run\n\t\tfinderPenaltyAddHistory(currentRunLength, runHistory, qrsize);\n\t\tcurrentRunLength = 0;\n\t}\n\tcurrentRunLength += qrsize;  // Add light border to final run\n\tfinderPenaltyAddHistory(currentRunLength, runHistory, qrsize);\n\treturn finderPenaltyCountPatterns(runHistory, qrsize);\n}\n\n\n// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().\nstatic void finderPenaltyAddHistory(int currentRunLength, int runHistory[7], int qrsize) {\n\tif (runHistory[0] == 0)\n\t\tcurrentRunLength += qrsize;  // Add light border to initial run\n\tmemmove(&runHistory[1], &runHistory[0], 6 * sizeof(runHistory[0]));\n\trunHistory[0] = currentRunLength;\n}\n\n\n\n/*---- Basic QR Code information ----*/\n\n// Public function - see documentation comment in header file.\nint qrcodegen_getSize(const uint8_t qrcode[]) {\n\tassert(qrcode != NULL);\n\tint result = qrcode[0];\n\tassert((qrcodegen_VERSION_MIN * 4 + 17) <= result\n\t\t&& result <= (qrcodegen_VERSION_MAX * 4 + 17));\n\treturn result;\n}\n\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) {\n\tassert(qrcode != NULL);\n\tint qrsize = qrcode[0];\n\treturn (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModuleBounded(qrcode, x, y);\n}\n\n\n// Returns the color of the module at the given coordinates, which must be in bounds.\ntestable bool getModuleBounded(const uint8_t qrcode[], int x, int y) {\n\tint qrsize = qrcode[0];\n\tassert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);\n\tint index = y * qrsize + x;\n\treturn getBit(qrcode[(index >> 3) + 1], index & 7);\n}\n\n\n// Sets the color of the module at the given coordinates, which must be in bounds.\ntestable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark) {\n\tint qrsize = qrcode[0];\n\tassert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);\n\tint index = y * qrsize + x;\n\tint bitIndex = index & 7;\n\tint byteIndex = (index >> 3) + 1;\n\tif (isDark)\n\t\tqrcode[byteIndex] |= 1 << bitIndex;\n\telse\n\t\tqrcode[byteIndex] &= (1 << bitIndex) ^ 0xFF;\n}\n\n\n// Sets the color of the module at the given coordinates, doing nothing if out of bounds.\ntestable void setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark) {\n\tint qrsize = qrcode[0];\n\tif (0 <= x && x < qrsize && 0 <= y && y < qrsize)\n\t\tsetModuleBounded(qrcode, x, y, isDark);\n}\n\n\n// Returns true iff the i'th bit of x is set to 1. Requires x >= 0 and 0 <= i <= 14.\nstatic bool getBit(int x, int i) {\n\treturn ((x >> i) & 1) != 0;\n}\n\n\n\n/*---- Segment handling ----*/\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_isNumeric(const char *text) {\n\tassert(text != NULL);\n\tfor (; *text != '\\0'; text++) {\n\t\tif (*text < '0' || *text > '9')\n\t\t\treturn false;\n\t}\n\treturn true;\n}\n\n\n// Public function - see documentation comment in header file.\nbool qrcodegen_isAlphanumeric(const char *text) {\n\tassert(text != NULL);\n\tfor (; *text != '\\0'; text++) {\n\t\tif (strchr(ALPHANUMERIC_CHARSET, *text) == NULL)\n\t\t\treturn false;\n\t}\n\treturn true;\n}\n\n\n// Public function - see documentation comment in header file.\nsize_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars) {\n\tint temp = calcSegmentBitLength(mode, numChars);\n\tif (temp == LENGTH_OVERFLOW)\n\t\treturn SIZE_MAX;\n\tassert(0 <= temp && temp <= INT16_MAX);\n\treturn ((size_t)temp + 7) / 8;\n}\n\n\n// Returns the number of data bits needed to represent a segment\n// containing the given number of characters using the given mode. Notes:\n// - Returns LENGTH_OVERFLOW on failure, i.e. numChars > INT16_MAX\n//   or the number of needed bits exceeds INT16_MAX (i.e. 32767).\n// - Otherwise, all valid results are in the range [0, INT16_MAX].\n// - For byte mode, numChars measures the number of bytes, not Unicode code points.\n// - For ECI mode, numChars must be 0, and the worst-case number of bits is returned.\n//   An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.\ntestable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {\n\t// All calculations are designed to avoid overflow on all platforms\n\tif (numChars > (unsigned int)INT16_MAX)\n\t\treturn LENGTH_OVERFLOW;\n\tlong result = (long)numChars;\n\tif (mode == qrcodegen_Mode_NUMERIC)\n\t\tresult = (result * 10 + 2) / 3;  // ceil(10/3 * n)\n\telse if (mode == qrcodegen_Mode_ALPHANUMERIC)\n\t\tresult = (result * 11 + 1) / 2;  // ceil(11/2 * n)\n\telse if (mode == qrcodegen_Mode_BYTE)\n\t\tresult *= 8;\n\telse if (mode == qrcodegen_Mode_KANJI)\n\t\tresult *= 13;\n\telse if (mode == qrcodegen_Mode_ECI && numChars == 0)\n\t\tresult = 3 * 8;\n\telse {  // Invalid argument\n\t\tassert(false);\n\t\treturn LENGTH_OVERFLOW;\n\t}\n\tassert(result >= 0);\n\tif (result > INT16_MAX)\n\t\treturn LENGTH_OVERFLOW;\n\treturn (int)result;\n}\n\n\n// Public function - see documentation comment in header file.\nstruct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]) {\n\tassert(data != NULL || len == 0);\n\tstruct qrcodegen_Segment result;\n\tresult.mode = qrcodegen_Mode_BYTE;\n\tresult.bitLength = calcSegmentBitLength(result.mode, len);\n\tassert(result.bitLength != LENGTH_OVERFLOW);\n\tresult.numChars = (int)len;\n\tif (len > 0)\n\t\tmemcpy(buf, data, len * sizeof(buf[0]));\n\tresult.data = buf;\n\treturn result;\n}\n\n\n// Public function - see documentation comment in header file.\nstruct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]) {\n\tassert(digits != NULL);\n\tstruct qrcodegen_Segment result;\n\tsize_t len = strlen(digits);\n\tresult.mode = qrcodegen_Mode_NUMERIC;\n\tint bitLen = calcSegmentBitLength(result.mode, len);\n\tassert(bitLen != LENGTH_OVERFLOW);\n\tresult.numChars = (int)len;\n\tif (bitLen > 0)\n\t\tmemset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));\n\tresult.bitLength = 0;\n\t\n\tunsigned int accumData = 0;\n\tint accumCount = 0;\n\tfor (; *digits != '\\0'; digits++) {\n\t\tchar c = *digits;\n\t\tassert('0' <= c && c <= '9');\n\t\taccumData = accumData * 10 + (unsigned int)(c - '0');\n\t\taccumCount++;\n\t\tif (accumCount == 3) {\n\t\t\tappendBitsToBuffer(accumData, 10, buf, &result.bitLength);\n\t\t\taccumData = 0;\n\t\t\taccumCount = 0;\n\t\t}\n\t}\n\tif (accumCount > 0)  // 1 or 2 digits remaining\n\t\tappendBitsToBuffer(accumData, accumCount * 3 + 1, buf, &result.bitLength);\n\tassert(result.bitLength == bitLen);\n\tresult.data = buf;\n\treturn result;\n}\n\n\n// Public function - see documentation comment in header file.\nstruct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]) {\n\tassert(text != NULL);\n\tstruct qrcodegen_Segment result;\n\tsize_t len = strlen(text);\n\tresult.mode = qrcodegen_Mode_ALPHANUMERIC;\n\tint bitLen = calcSegmentBitLength(result.mode, len);\n\tassert(bitLen != LENGTH_OVERFLOW);\n\tresult.numChars = (int)len;\n\tif (bitLen > 0)\n\t\tmemset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));\n\tresult.bitLength = 0;\n\t\n\tunsigned int accumData = 0;\n\tint accumCount = 0;\n\tfor (; *text != '\\0'; text++) {\n\t\tconst char *temp = strchr(ALPHANUMERIC_CHARSET, *text);\n\t\tassert(temp != NULL);\n\t\taccumData = accumData * 45 + (unsigned int)(temp - ALPHANUMERIC_CHARSET);\n\t\taccumCount++;\n\t\tif (accumCount == 2) {\n\t\t\tappendBitsToBuffer(accumData, 11, buf, &result.bitLength);\n\t\t\taccumData = 0;\n\t\t\taccumCount = 0;\n\t\t}\n\t}\n\tif (accumCount > 0)  // 1 character remaining\n\t\tappendBitsToBuffer(accumData, 6, buf, &result.bitLength);\n\tassert(result.bitLength == bitLen);\n\tresult.data = buf;\n\treturn result;\n}\n\n\n// Public function - see documentation comment in header file.\nstruct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {\n\tstruct qrcodegen_Segment result;\n\tresult.mode = qrcodegen_Mode_ECI;\n\tresult.numChars = 0;\n\tresult.bitLength = 0;\n\tif (assignVal < 0)\n\t\tassert(false);\n\telse if (assignVal < (1 << 7)) {\n\t\tmemset(buf, 0, 1 * sizeof(buf[0]));\n\t\tappendBitsToBuffer((unsigned int)assignVal, 8, buf, &result.bitLength);\n\t} else if (assignVal < (1 << 14)) {\n\t\tmemset(buf, 0, 2 * sizeof(buf[0]));\n\t\tappendBitsToBuffer(2, 2, buf, &result.bitLength);\n\t\tappendBitsToBuffer((unsigned int)assignVal, 14, buf, &result.bitLength);\n\t} else if (assignVal < 1000000L) {\n\t\tmemset(buf, 0, 3 * sizeof(buf[0]));\n\t\tappendBitsToBuffer(6, 3, buf, &result.bitLength);\n\t\tappendBitsToBuffer((unsigned int)(assignVal >> 10), 11, buf, &result.bitLength);\n\t\tappendBitsToBuffer((unsigned int)(assignVal & 0x3FF), 10, buf, &result.bitLength);\n\t} else\n\t\tassert(false);\n\tresult.data = buf;\n\treturn result;\n}\n\n\n// Calculates the number of bits needed to encode the given segments at the given version.\n// Returns a non-negative number if successful. Otherwise returns LENGTH_OVERFLOW if a segment\n// has too many characters to fit its length field, or the total bits exceeds INT16_MAX.\ntestable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) {\n\tassert(segs != NULL || len == 0);\n\tlong result = 0;\n\tfor (size_t i = 0; i < len; i++) {\n\t\tint numChars  = segs[i].numChars;\n\t\tint bitLength = segs[i].bitLength;\n\t\tassert(0 <= numChars  && numChars  <= INT16_MAX);\n\t\tassert(0 <= bitLength && bitLength <= INT16_MAX);\n\t\tint ccbits = numCharCountBits(segs[i].mode, version);\n\t\tassert(0 <= ccbits && ccbits <= 16);\n\t\tif (numChars >= (1L << ccbits))\n\t\t\treturn LENGTH_OVERFLOW;  // The segment's length doesn't fit the field's bit width\n\t\tresult += 4L + ccbits + bitLength;\n\t\tif (result > INT16_MAX)\n\t\t\treturn LENGTH_OVERFLOW;  // The sum might overflow an int type\n\t}\n\tassert(0 <= result && result <= INT16_MAX);\n\treturn (int)result;\n}\n\n\n// Returns the bit width of the character count field for a segment in the given mode\n// in a QR Code at the given version number. The result is in the range [0, 16].\nstatic int numCharCountBits(enum qrcodegen_Mode mode, int version) {\n\tassert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);\n\tint i = (version + 7) / 17;\n\tswitch (mode) {\n\t\tcase qrcodegen_Mode_NUMERIC     : { static const int temp[] = {10, 12, 14}; return temp[i]; }\n\t\tcase qrcodegen_Mode_ALPHANUMERIC: { static const int temp[] = { 9, 11, 13}; return temp[i]; }\n\t\tcase qrcodegen_Mode_BYTE        : { static const int temp[] = { 8, 16, 16}; return temp[i]; }\n\t\tcase qrcodegen_Mode_KANJI       : { static const int temp[] = { 8, 10, 12}; return temp[i]; }\n\t\tcase qrcodegen_Mode_ECI         : return 0;\n\t\tdefault:  assert(false);  return -1;  // Dummy value\n\t}\n}\n\n\n#undef LENGTH_OVERFLOW\n"
  },
  {
    "path": "c/qrcodegen.h",
    "content": "/* \n * QR Code generator library (C)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#pragma once\n\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdint.h>\n\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n\n/* \n * This library creates QR Code symbols, which is a type of two-dimension barcode.\n * Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n * A QR Code structure is an immutable square grid of dark and light cells.\n * The library provides functions to create a QR Code from text or binary data.\n * The library covers the QR Code Model 2 specification, supporting all versions (sizes)\n * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.\n * \n * Ways to create a QR Code object:\n * - High level: Take the payload data and call qrcodegen_encodeText() or qrcodegen_encodeBinary().\n * - Low level: Custom-make the list of segments and call\n *   qrcodegen_encodeSegments() or qrcodegen_encodeSegmentsAdvanced().\n * (Note that all ways require supplying the desired error correction level and various byte buffers.)\n */\n\n\n/*---- Enum and struct types----*/\n\n/* \n * The error correction level in a QR Code symbol.\n */\nenum qrcodegen_Ecc {\n\t// Must be declared in ascending order of error protection\n\t// so that an internal qrcodegen function works properly\n\tqrcodegen_Ecc_LOW = 0 ,  // The QR Code can tolerate about  7% erroneous codewords\n\tqrcodegen_Ecc_MEDIUM  ,  // The QR Code can tolerate about 15% erroneous codewords\n\tqrcodegen_Ecc_QUARTILE,  // The QR Code can tolerate about 25% erroneous codewords\n\tqrcodegen_Ecc_HIGH    ,  // The QR Code can tolerate about 30% erroneous codewords\n};\n\n\n/* \n * The mask pattern used in a QR Code symbol.\n */\nenum qrcodegen_Mask {\n\t// A special value to tell the QR Code encoder to\n\t// automatically select an appropriate mask pattern\n\tqrcodegen_Mask_AUTO = -1,\n\t// The eight actual mask patterns\n\tqrcodegen_Mask_0 = 0,\n\tqrcodegen_Mask_1,\n\tqrcodegen_Mask_2,\n\tqrcodegen_Mask_3,\n\tqrcodegen_Mask_4,\n\tqrcodegen_Mask_5,\n\tqrcodegen_Mask_6,\n\tqrcodegen_Mask_7,\n};\n\n\n/* \n * Describes how a segment's data bits are interpreted.\n */\nenum qrcodegen_Mode {\n\tqrcodegen_Mode_NUMERIC      = 0x1,\n\tqrcodegen_Mode_ALPHANUMERIC = 0x2,\n\tqrcodegen_Mode_BYTE         = 0x4,\n\tqrcodegen_Mode_KANJI        = 0x8,\n\tqrcodegen_Mode_ECI          = 0x7,\n};\n\n\n/* \n * A segment of character/binary/control data in a QR Code symbol.\n * The mid-level way to create a segment is to take the payload data\n * and call a factory function such as qrcodegen_makeNumeric().\n * The low-level way to create a segment is to custom-make the bit buffer\n * and initialize a qrcodegen_Segment struct with appropriate values.\n * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n * Any segment longer than this is meaningless for the purpose of generating QR Codes.\n * Moreover, the maximum allowed bit length is 32767 because\n * the largest QR Code (version 40) has 31329 modules.\n */\nstruct qrcodegen_Segment {\n\t// The mode indicator of this segment.\n\tenum qrcodegen_Mode mode;\n\t\n\t// The length of this segment's unencoded data. Measured in characters for\n\t// numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t// Always zero or positive. Not the same as the data's bit length.\n\tint numChars;\n\t\n\t// The data bits of this segment, packed in bitwise big endian.\n\t// Can be null if the bit length is zero.\n\tuint8_t *data;\n\t\n\t// The number of valid data bits used in the buffer. Requires\n\t// 0 <= bitLength <= 32767, and bitLength <= (capacity of data array) * 8.\n\t// The character count (numChars) must agree with the mode and the bit buffer length.\n\tint bitLength;\n};\n\n\n\n/*---- Macro constants and functions ----*/\n\n#define qrcodegen_VERSION_MIN   1  // The minimum version number supported in the QR Code Model 2 standard\n#define qrcodegen_VERSION_MAX  40  // The maximum version number supported in the QR Code Model 2 standard\n\n// Calculates the number of bytes needed to store any QR Code up to and including the given version number,\n// as a compile-time constant. For example, 'uint8_t buffer[qrcodegen_BUFFER_LEN_FOR_VERSION(25)];'\n// can store any single QR Code from version 1 to 25 (inclusive). The result fits in an int (or int16).\n// Requires qrcodegen_VERSION_MIN <= n <= qrcodegen_VERSION_MAX.\n#define qrcodegen_BUFFER_LEN_FOR_VERSION(n)  ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8 + 1)\n\n// The worst-case number of bytes needed to store one QR Code, up to and including\n// version 40. This value equals 3918, which is just under 4 kilobytes.\n// Use this more convenient value to avoid calculating tighter memory bounds for buffers.\n#define qrcodegen_BUFFER_LEN_MAX  qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX)\n\n\n\n/*---- Functions (high level) to generate QR Codes ----*/\n\n/* \n * Encodes the given text string to a QR Code, returning true if successful.\n * If the data is too long to fit in any version in the given range\n * at the given ECC level, then false is returned.\n * \n * The input text must be encoded in UTF-8 and contain no NULs.\n * Requires 1 <= minVersion <= maxVersion <= 40.\n * \n * The smallest possible QR Code version within the given range is automatically\n * chosen for the output. Iff boostEcl is true, then the ECC level of the result\n * may be higher than the ecl argument if it can be done without increasing the\n * version. The mask is either between qrcodegen_Mask_0 to 7 to force that mask, or\n * qrcodegen_Mask_AUTO to automatically choose an appropriate mask (which may be slow).\n * \n * About the arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion):\n * - Before calling the function:\n *   - The array ranges tempBuffer[0 : len] and qrcode[0 : len] must allow\n *     reading and writing; hence each array must have a length of at least len.\n *   - The two ranges must not overlap (aliasing).\n *   - The initial state of both ranges can be uninitialized\n *     because the function always writes before reading.\n * - After the function returns:\n *   - Both ranges have no guarantee on which elements are initialized and what values are stored.\n *   - tempBuffer contains no useful data and should be treated as entirely uninitialized.\n *   - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().\n * \n * If successful, the resulting QR Code may use numeric,\n * alphanumeric, or byte mode to encode the text.\n * \n * In the most optimistic case, a QR Code at version 40 with low ECC\n * can hold any UTF-8 string up to 2953 bytes, or any alphanumeric string\n * up to 4296 characters, or any digit string up to 7089 characters.\n * These numbers represent the hard upper limit of the QR Code standard.\n * \n * Please consult the QR Code specification for information on\n * data capacities per version, ECC level, and text encoding mode.\n */\nbool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],\n\tenum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl);\n\n\n/* \n * Encodes the given binary data to a QR Code, returning true if successful.\n * If the data is too long to fit in any version in the given range\n * at the given ECC level, then false is returned.\n * \n * Requires 1 <= minVersion <= maxVersion <= 40.\n * \n * The smallest possible QR Code version within the given range is automatically\n * chosen for the output. Iff boostEcl is true, then the ECC level of the result\n * may be higher than the ecl argument if it can be done without increasing the\n * version. The mask is either between qrcodegen_Mask_0 to 7 to force that mask, or\n * qrcodegen_Mask_AUTO to automatically choose an appropriate mask (which may be slow).\n * \n * About the arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion):\n * - Before calling the function:\n *   - The array ranges dataAndTemp[0 : len] and qrcode[0 : len] must allow\n *     reading and writing; hence each array must have a length of at least len.\n *   - The two ranges must not overlap (aliasing).\n *   - The input array range dataAndTemp[0 : dataLen] should normally be\n *     valid UTF-8 text, but is not required by the QR Code standard.\n *   - The initial state of dataAndTemp[dataLen : len] and qrcode[0 : len]\n *     can be uninitialized because the function always writes before reading.\n * - After the function returns:\n *   - Both ranges have no guarantee on which elements are initialized and what values are stored.\n *   - dataAndTemp contains no useful data and should be treated as entirely uninitialized.\n *   - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().\n * \n * If successful, the resulting QR Code will use byte mode to encode the data.\n * \n * In the most optimistic case, a QR Code at version 40 with low ECC can hold any byte\n * sequence up to length 2953. This is the hard upper limit of the QR Code standard.\n * \n * Please consult the QR Code specification for information on\n * data capacities per version, ECC level, and text encoding mode.\n */\nbool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],\n\tenum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl);\n\n\n/*---- Functions (low level) to generate QR Codes ----*/\n\n/* \n * Encodes the given segments to a QR Code, returning true if successful.\n * If the data is too long to fit in any version at the given ECC level,\n * then false is returned.\n * \n * The smallest possible QR Code version is automatically chosen for\n * the output. The ECC level of the result may be higher than the\n * ecl argument if it can be done without increasing the version.\n * \n * About the byte arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX):\n * - Before calling the function:\n *   - The array ranges tempBuffer[0 : len] and qrcode[0 : len] must allow\n *     reading and writing; hence each array must have a length of at least len.\n *   - The two ranges must not overlap (aliasing).\n *   - The initial state of both ranges can be uninitialized\n *     because the function always writes before reading.\n *   - The input array segs can contain segments whose data buffers overlap with tempBuffer.\n * - After the function returns:\n *   - Both ranges have no guarantee on which elements are initialized and what values are stored.\n *   - tempBuffer contains no useful data and should be treated as entirely uninitialized.\n *   - Any segment whose data buffer overlaps with tempBuffer[0 : len]\n *     must be treated as having invalid values in that array.\n *   - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().\n * \n * Please consult the QR Code specification for information on\n * data capacities per version, ECC level, and text encoding mode.\n * \n * This function allows the user to create a custom sequence of segments that switches\n * between modes (such as alphanumeric and byte) to encode text in less space.\n * This is a low-level API; the high-level API is qrcodegen_encodeText() and qrcodegen_encodeBinary().\n */\nbool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len,\n\tenum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]);\n\n\n/* \n * Encodes the given segments to a QR Code, returning true if successful.\n * If the data is too long to fit in any version in the given range\n * at the given ECC level, then false is returned.\n * \n * Requires 1 <= minVersion <= maxVersion <= 40.\n * \n * The smallest possible QR Code version within the given range is automatically\n * chosen for the output. Iff boostEcl is true, then the ECC level of the result\n * may be higher than the ecl argument if it can be done without increasing the\n * version. The mask is either between qrcodegen_Mask_0 to 7 to force that mask, or\n * qrcodegen_Mask_AUTO to automatically choose an appropriate mask (which may be slow).\n * \n * About the byte arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX):\n * - Before calling the function:\n *   - The array ranges tempBuffer[0 : len] and qrcode[0 : len] must allow\n *     reading and writing; hence each array must have a length of at least len.\n *   - The two ranges must not overlap (aliasing).\n *   - The initial state of both ranges can be uninitialized\n *     because the function always writes before reading.\n *   - The input array segs can contain segments whose data buffers overlap with tempBuffer.\n * - After the function returns:\n *   - Both ranges have no guarantee on which elements are initialized and what values are stored.\n *   - tempBuffer contains no useful data and should be treated as entirely uninitialized.\n *   - Any segment whose data buffer overlaps with tempBuffer[0 : len]\n *     must be treated as having invalid values in that array.\n *   - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().\n * \n * Please consult the QR Code specification for information on\n * data capacities per version, ECC level, and text encoding mode.\n * \n * This function allows the user to create a custom sequence of segments that switches\n * between modes (such as alphanumeric and byte) to encode text in less space.\n * This is a low-level API; the high-level API is qrcodegen_encodeText() and qrcodegen_encodeBinary().\n */\nbool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,\n\tint minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]);\n\n\n/* \n * Tests whether the given string can be encoded as a segment in numeric mode.\n * A string is encodable iff each character is in the range 0 to 9.\n */\nbool qrcodegen_isNumeric(const char *text);\n\n\n/* \n * Tests whether the given string can be encoded as a segment in alphanumeric mode.\n * A string is encodable iff each character is in the following set: 0 to 9, A to Z\n * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n */\nbool qrcodegen_isAlphanumeric(const char *text);\n\n\n/* \n * Returns the number of bytes (uint8_t) needed for the data buffer of a segment\n * containing the given number of characters using the given mode. Notes:\n * - Returns SIZE_MAX on failure, i.e. numChars > INT16_MAX or the internal\n *   calculation of the number of needed bits exceeds INT16_MAX (i.e. 32767).\n * - Otherwise, all valid results are in the range [0, ceil(INT16_MAX / 8)], i.e. at most 4096.\n * - It is okay for the user to allocate more bytes for the buffer than needed.\n * - For byte mode, numChars measures the number of bytes, not Unicode code points.\n * - For ECI mode, numChars must be 0, and the worst-case number of bytes is returned.\n *   An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.\n */\nsize_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars);\n\n\n/* \n * Returns a segment representing the given binary data encoded in\n * byte mode. All input byte arrays are acceptable. Any text string\n * can be converted to UTF-8 bytes and encoded as a byte mode segment.\n */\nstruct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]);\n\n\n/* \n * Returns a segment representing the given string of decimal digits encoded in numeric mode.\n */\nstruct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]);\n\n\n/* \n * Returns a segment representing the given text string encoded in alphanumeric mode.\n * The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n * dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n */\nstruct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]);\n\n\n/* \n * Returns a segment representing an Extended Channel Interpretation\n * (ECI) designator with the given assignment value.\n */\nstruct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]);\n\n\n/*---- Functions to extract raw data from QR Codes ----*/\n\n/* \n * Returns the side length of the given QR Code, assuming that encoding succeeded.\n * The result is in the range [21, 177]. Note that the length of the array buffer\n * is related to the side length - every 'uint8_t qrcode[]' must have length at least\n * qrcodegen_BUFFER_LEN_FOR_VERSION(version), which equals ceil(size^2 / 8 + 1).\n */\nint qrcodegen_getSize(const uint8_t qrcode[]);\n\n\n/* \n * Returns the color of the module (pixel) at the given coordinates, which is false\n * for light or true for dark. The top left corner has the coordinates (x=0, y=0).\n * If the given coordinates are out of bounds, then false (light) is returned.\n */\nbool qrcodegen_getModule(const uint8_t qrcode[], int x, int y);\n\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "cpp/Makefile",
    "content": "# \n# Makefile for QR Code generator (C++)\n# \n# Copyright (c) Project Nayuki. (MIT License)\n# https://www.nayuki.io/page/qr-code-generator-library\n# \n# Permission is hereby granted, free of charge, to any person obtaining a copy of\n# this software and associated documentation files (the \"Software\"), to deal in\n# the Software without restriction, including without limitation the rights to\n# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n# the Software, and to permit persons to whom the Software is furnished to do so,\n# subject to the following conditions:\n# - The above copyright notice and this permission notice shall be included in\n#   all copies or substantial portions of the Software.\n# - The Software is provided \"as is\", without warranty of any kind, express or\n#   implied, including but not limited to the warranties of merchantability,\n#   fitness for a particular purpose and noninfringement. In no event shall the\n#   authors or copyright holders be liable for any claim, damages or other\n#   liability, whether in an action of contract, tort or otherwise, arising from,\n#   out of or in connection with the Software or the use or other dealings in the\n#   Software.\n# \n\n\n# ---- Configuration options ----\n\n# External/implicit variables:\n# - CXX: The C++ compiler, such as g++ or clang++.\n# - CXXFLAGS: Any extra user-specified compiler flags (can be blank).\n\n# Recommended compiler flags:\nCXXFLAGS += -std=c++11 -O\n\n# Extra flags for diagnostics:\n# CXXFLAGS += -g -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -fsanitize=undefined,address\n\n\n# ---- Controlling make ----\n\n# Clear default suffix rules\n.SUFFIXES:\n\n# Don't delete object files\n.SECONDARY:\n\n# Stuff concerning goals\n.DEFAULT_GOAL = all\n.PHONY: all clean\n\n\n# ---- Targets to build ----\n\nLIB = qrcodegencpp\nLIBFILE = lib$(LIB).a\nLIBOBJ = qrcodegen.o\nMAINS = QrCodeGeneratorDemo\n\n# Build all binaries\nall: $(LIBFILE) $(MAINS)\n\n# Delete build output\nclean:\n\trm -f -- $(LIBOBJ) $(LIBFILE) $(MAINS:=.o) $(MAINS)\n\trm -rf .deps\n\n# Executable files\n%: %.o $(LIBFILE)\n\t$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $< -L . -l $(LIB)\n\n# The library\n$(LIBFILE): $(LIBOBJ)\n\t$(AR) -crs $@ -- $^\n\n# Object files\n%.o: %.cpp .deps/timestamp\n\t$(CXX) $(CXXFLAGS) -c -o $@ -MMD -MF .deps/$*.d $<\n\n# Have a place to store header dependencies automatically generated by compiler\n.deps/timestamp:\n\tmkdir -p .deps\n\ttouch .deps/timestamp\n\n# Make use of said dependencies if available\n-include .deps/*.d\n"
  },
  {
    "path": "cpp/QrCodeGeneratorDemo.cpp",
    "content": "/* \n * QR Code generator demo (C++)\n * \n * Run this command-line program with no arguments. The program computes a bunch of demonstration\n * QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#include <climits>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <sstream>\n#include <string>\n#include <vector>\n#include \"qrcodegen.hpp\"\n\nusing std::uint8_t;\nusing qrcodegen::QrCode;\nusing qrcodegen::QrSegment;\n\n\n// Function prototypes\nstatic void doBasicDemo();\nstatic void doVarietyDemo();\nstatic void doSegmentDemo();\nstatic void doMaskDemo();\nstatic std::string toSvgString(const QrCode &qr, int border);\nstatic void printQr(const QrCode &qr);\n\n\n// The main application program.\nint main() {\n\tdoBasicDemo();\n\tdoVarietyDemo();\n\tdoSegmentDemo();\n\tdoMaskDemo();\n\treturn EXIT_SUCCESS;\n}\n\n\n\n/*---- Demo suite ----*/\n\n// Creates a single QR Code, then prints it to the console.\nstatic void doBasicDemo() {\n\tconst char *text = \"Hello, world!\";              // User-supplied text\n\tconst QrCode::Ecc errCorLvl = QrCode::Ecc::LOW;  // Error correction level\n\t\n\t// Make and print the QR Code symbol\n\tconst QrCode qr = QrCode::encodeText(text, errCorLvl);\n\tprintQr(qr);\n\tstd::cout << toSvgString(qr, 4) << std::endl;\n}\n\n\n// Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.\nstatic void doVarietyDemo() {\n\t// Numeric mode encoding (3.33 bits per digit)\n\tconst QrCode qr0 = QrCode::encodeText(\"314159265358979323846264338327950288419716939937510\", QrCode::Ecc::MEDIUM);\n\tprintQr(qr0);\n\t\n\t// Alphanumeric mode encoding (5.5 bits per character)\n\tconst QrCode qr1 = QrCode::encodeText(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", QrCode::Ecc::HIGH);\n\tprintQr(qr1);\n\t\n\t// Unicode text as UTF-8\n\tconst QrCode qr2 = QrCode::encodeText(\"\\xE3\\x81\\x93\\xE3\\x82\\x93\\xE3\\x81\\xAB\\xE3\\x81\\xA1wa\\xE3\\x80\\x81\"\n\t\t\"\\xE4\\xB8\\x96\\xE7\\x95\\x8C\\xEF\\xBC\\x81\\x20\\xCE\\xB1\\xCE\\xB2\\xCE\\xB3\\xCE\\xB4\", QrCode::Ecc::QUARTILE);\n\tprintQr(qr2);\n\t\n\t// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\tconst QrCode qr3 = QrCode::encodeText(\n\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \"\n\t\t\"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \"\n\t\t\"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \"\n\t\t\"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \"\n\t\t\"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \"\n\t\t\"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \"\n\t\t\"a White Rabbit with pink eyes ran close by her.\", QrCode::Ecc::HIGH);\n\tprintQr(qr3);\n}\n\n\n// Creates QR Codes with manually specified segments for better compactness.\nstatic void doSegmentDemo() {\n\t// Illustration \"silver\"\n\tconst char *silver0 = \"THE SQUARE ROOT OF 2 IS 1.\";\n\tconst char *silver1 = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\tconst QrCode qr0 = QrCode::encodeText(\n\t\t(std::string(silver0) + silver1).c_str(),\n\t\tQrCode::Ecc::LOW);\n\tprintQr(qr0);\n\t\n\tconst QrCode qr1 = QrCode::encodeSegments(\n\t\t{QrSegment::makeAlphanumeric(silver0), QrSegment::makeNumeric(silver1)},\n\t\tQrCode::Ecc::LOW);\n\tprintQr(qr1);\n\t\n\t// Illustration \"golden\"\n\tconst char *golden0 = \"Golden ratio \\xCF\\x86 = 1.\";\n\tconst char *golden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\tconst char *golden2 = \"......\";\n\tconst QrCode qr2 = QrCode::encodeText(\n\t\t(std::string(golden0) + golden1 + golden2).c_str(),\n\t\tQrCode::Ecc::LOW);\n\tprintQr(qr2);\n\t\n\tstd::vector<uint8_t> bytes(golden0, golden0 + std::strlen(golden0));\n\tconst QrCode qr3 = QrCode::encodeSegments(\n\t\t{QrSegment::makeBytes(bytes), QrSegment::makeNumeric(golden1), QrSegment::makeAlphanumeric(golden2)},\n\t\tQrCode::Ecc::LOW);\n\tprintQr(qr3);\n\t\n\t// Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\tconst char *madoka =  // Encoded in UTF-8\n\t\t\"\\xE3\\x80\\x8C\\xE9\\xAD\\x94\\xE6\\xB3\\x95\\xE5\"\n\t\t\"\\xB0\\x91\\xE5\\xA5\\xB3\\xE3\\x81\\xBE\\xE3\\x81\"\n\t\t\"\\xA9\\xE3\\x81\\x8B\\xE2\\x98\\x86\\xE3\\x83\\x9E\"\n\t\t\"\\xE3\\x82\\xAE\\xE3\\x82\\xAB\\xE3\\x80\\x8D\\xE3\"\n\t\t\"\\x81\\xA3\\xE3\\x81\\xA6\\xE3\\x80\\x81\\xE3\\x80\"\n\t\t\"\\x80\\xD0\\x98\\xD0\\x90\\xD0\\x98\\xE3\\x80\\x80\"\n\t\t\"\\xEF\\xBD\\x84\\xEF\\xBD\\x85\\xEF\\xBD\\x93\\xEF\"\n\t\t\"\\xBD\\x95\\xE3\\x80\\x80\\xCE\\xBA\\xCE\\xB1\\xEF\"\n\t\t\"\\xBC\\x9F\";\n\tconst QrCode qr4 = QrCode::encodeText(madoka, QrCode::Ecc::LOW);\n\tprintQr(qr4);\n\t\n\tconst std::vector<int> kanjiChars{  // Kanji mode encoding (13 bits per character)\n\t\t0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,\n\t\t0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,\n\t\t0x018D, 0x018A, 0x0036, 0x0141, 0x0144,\n\t\t0x0001, 0x0000, 0x0249, 0x0240, 0x0249,\n\t\t0x0000, 0x0104, 0x0105, 0x0113, 0x0115,\n\t\t0x0000, 0x0208, 0x01FF, 0x0008,\n\t};\n\tqrcodegen::BitBuffer bb;\n\tfor (int c : kanjiChars)\n\t\tbb.appendBits(static_cast<std::uint32_t>(c), 13);\n\tconst QrCode qr5 = QrCode::encodeSegments(\n\t\t{QrSegment(QrSegment::Mode::KANJI, static_cast<int>(kanjiChars.size()), bb)},\n\t\tQrCode::Ecc::LOW);\n\tprintQr(qr5);\n}\n\n\n// Creates QR Codes with the same size and contents but different mask patterns.\nstatic void doMaskDemo() {\n\t// Project Nayuki URL\n\tstd::vector<QrSegment> segs0 = QrSegment::makeSegments(\"https://www.nayuki.io/\");\n\tprintQr(QrCode::encodeSegments(segs0, QrCode::Ecc::HIGH, QrCode::MIN_VERSION, QrCode::MAX_VERSION, -1, true));  // Automatic mask\n\tprintQr(QrCode::encodeSegments(segs0, QrCode::Ecc::HIGH, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 3, true));  // Force mask 3\n\t\n\t// Chinese text as UTF-8\n\tstd::vector<QrSegment> segs1 = QrSegment::makeSegments(\n\t\t\"\\xE7\\xB6\\xAD\\xE5\\x9F\\xBA\\xE7\\x99\\xBE\\xE7\\xA7\\x91\\xEF\\xBC\\x88\\x57\\x69\\x6B\\x69\\x70\"\n\t\t\"\\x65\\x64\\x69\\x61\\xEF\\xBC\\x8C\\xE8\\x81\\x86\\xE8\\x81\\xBD\\x69\\x2F\\xCB\\x8C\\x77\\xC9\\xAA\"\n\t\t\"\\x6B\\xE1\\xB5\\xBB\\xCB\\x88\\x70\\x69\\xCB\\x90\\x64\\x69\\x2E\\xC9\\x99\\x2F\\xEF\\xBC\\x89\\xE6\"\n\t\t\"\\x98\\xAF\\xE4\\xB8\\x80\\xE5\\x80\\x8B\\xE8\\x87\\xAA\\xE7\\x94\\xB1\\xE5\\x85\\xA7\\xE5\\xAE\\xB9\"\n\t\t\"\\xE3\\x80\\x81\\xE5\\x85\\xAC\\xE9\\x96\\x8B\\xE7\\xB7\\xA8\\xE8\\xBC\\xAF\\xE4\\xB8\\x94\\xE5\\xA4\"\n\t\t\"\\x9A\\xE8\\xAA\\x9E\\xE8\\xA8\\x80\\xE7\\x9A\\x84\\xE7\\xB6\\xB2\\xE8\\xB7\\xAF\\xE7\\x99\\xBE\\xE7\"\n\t\t\"\\xA7\\x91\\xE5\\x85\\xA8\\xE6\\x9B\\xB8\\xE5\\x8D\\x94\\xE4\\xBD\\x9C\\xE8\\xA8\\x88\\xE7\\x95\\xAB\");\n\tprintQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 0, true));  // Force mask 0\n\tprintQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 1, true));  // Force mask 1\n\tprintQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 5, true));  // Force mask 5\n\tprintQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 7, true));  // Force mask 7\n}\n\n\n\n/*---- Utilities ----*/\n\n// Returns a string of SVG code for an image depicting the given QR Code, with the given number\n// of border modules. The string always uses Unix newlines (\\n), regardless of the platform.\nstatic std::string toSvgString(const QrCode &qr, int border) {\n\tif (border < 0)\n\t\tthrow std::domain_error(\"Border must be non-negative\");\n\tif (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize())\n\t\tthrow std::overflow_error(\"Border too large\");\n\t\n\tstd::ostringstream sb;\n\tsb << \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";\n\tsb << \"<!DOCTYPE svg PUBLIC \\\"-//W3C//DTD SVG 1.1//EN\\\" \\\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\\">\\n\";\n\tsb << \"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" version=\\\"1.1\\\" viewBox=\\\"0 0 \";\n\tsb << (qr.getSize() + border * 2) << \" \" << (qr.getSize() + border * 2) << \"\\\" stroke=\\\"none\\\">\\n\";\n\tsb << \"\\t<rect width=\\\"100%\\\" height=\\\"100%\\\" fill=\\\"#FFFFFF\\\"/>\\n\";\n\tsb << \"\\t<path d=\\\"\";\n\tfor (int y = 0; y < qr.getSize(); y++) {\n\t\tfor (int x = 0; x < qr.getSize(); x++) {\n\t\t\tif (qr.getModule(x, y)) {\n\t\t\t\tif (x != 0 || y != 0)\n\t\t\t\t\tsb << \" \";\n\t\t\t\tsb << \"M\" << (x + border) << \",\" << (y + border) << \"h1v1h-1z\";\n\t\t\t}\n\t\t}\n\t}\n\tsb << \"\\\" fill=\\\"#000000\\\"/>\\n\";\n\tsb << \"</svg>\\n\";\n\treturn sb.str();\n}\n\n\n// Prints the given QrCode object to the console.\nstatic void printQr(const QrCode &qr) {\n\tint border = 4;\n\tfor (int y = -border; y < qr.getSize() + border; y++) {\n\t\tfor (int x = -border; x < qr.getSize() + border; x++) {\n\t\t\tstd::cout << (qr.getModule(x, y) ? \"##\" : \"  \");\n\t\t}\n\t\tstd::cout << std::endl;\n\t}\n\tstd::cout << std::endl;\n}\n"
  },
  {
    "path": "cpp/Readme.markdown",
    "content": "QR Code generator library - C++\n===============================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Coded carefully to prevent memory corruption, integer overflow, platform-dependent inconsistencies, and undefined behavior; tested rigorously to confirm safety\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```c++\n#include <string>\n#include <vector>\n#include \"QrCode.hpp\"\nusing namespace qrcodegen;\n\n// Simple operation\nQrCode qr0 = QrCode::encodeText(\"Hello, world!\", QrCode::Ecc::MEDIUM);\nstd::string svg = toSvgString(qr0, 4);  // See QrCodeGeneratorDemo\n\n// Manual operation\nstd::vector<QrSegment> segs =\n    QrSegment::makeSegments(\"3141592653589793238462643383\");\nQrCode qr1 = QrCode::encodeSegments(\n    segs, QrCode::Ecc::HIGH, 5, 5, 2, false);\nfor (int y = 0; y < qr1.getSize(); y++) {\n    for (int x = 0; x < qr1.getSize(); x++) {\n        (... paint qr1.getModule(x, y) ...)\n    }\n}\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/cpp/QrCodeGeneratorDemo.cpp .\n"
  },
  {
    "path": "cpp/qrcodegen.cpp",
    "content": "/* \n * QR Code generator library (C++)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#include <algorithm>\n#include <cassert>\n#include <climits>\n#include <cstddef>\n#include <cstdlib>\n#include <cstring>\n#include <sstream>\n#include <utility>\n#include \"qrcodegen.hpp\"\n\nusing std::int8_t;\nusing std::uint8_t;\nusing std::size_t;\nusing std::vector;\n\n\nnamespace qrcodegen {\n\n/*---- Class QrSegment ----*/\n\nQrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :\n\t\tmodeBits(mode) {\n\tnumBitsCharCount[0] = cc0;\n\tnumBitsCharCount[1] = cc1;\n\tnumBitsCharCount[2] = cc2;\n}\n\n\nint QrSegment::Mode::getModeBits() const {\n\treturn modeBits;\n}\n\n\nint QrSegment::Mode::numCharCountBits(int ver) const {\n\treturn numBitsCharCount[(ver + 7) / 17];\n}\n\n\nconst QrSegment::Mode QrSegment::Mode::NUMERIC     (0x1, 10, 12, 14);\nconst QrSegment::Mode QrSegment::Mode::ALPHANUMERIC(0x2,  9, 11, 13);\nconst QrSegment::Mode QrSegment::Mode::BYTE        (0x4,  8, 16, 16);\nconst QrSegment::Mode QrSegment::Mode::KANJI       (0x8,  8, 10, 12);\nconst QrSegment::Mode QrSegment::Mode::ECI         (0x7,  0,  0,  0);\n\n\nQrSegment QrSegment::makeBytes(const vector<uint8_t> &data) {\n\tif (data.size() > static_cast<unsigned int>(INT_MAX))\n\t\tthrow std::length_error(\"Data too long\");\n\tBitBuffer bb;\n\tfor (uint8_t b : data)\n\t\tbb.appendBits(b, 8);\n\treturn QrSegment(Mode::BYTE, static_cast<int>(data.size()), std::move(bb));\n}\n\n\nQrSegment QrSegment::makeNumeric(const char *digits) {\n\tBitBuffer bb;\n\tint accumData = 0;\n\tint accumCount = 0;\n\tint charCount = 0;\n\tfor (; *digits != '\\0'; digits++, charCount++) {\n\t\tchar c = *digits;\n\t\tif (c < '0' || c > '9')\n\t\t\tthrow std::domain_error(\"String contains non-numeric characters\");\n\t\taccumData = accumData * 10 + (c - '0');\n\t\taccumCount++;\n\t\tif (accumCount == 3) {\n\t\t\tbb.appendBits(static_cast<uint32_t>(accumData), 10);\n\t\t\taccumData = 0;\n\t\t\taccumCount = 0;\n\t\t}\n\t}\n\tif (accumCount > 0)  // 1 or 2 digits remaining\n\t\tbb.appendBits(static_cast<uint32_t>(accumData), accumCount * 3 + 1);\n\treturn QrSegment(Mode::NUMERIC, charCount, std::move(bb));\n}\n\n\nQrSegment QrSegment::makeAlphanumeric(const char *text) {\n\tBitBuffer bb;\n\tint accumData = 0;\n\tint accumCount = 0;\n\tint charCount = 0;\n\tfor (; *text != '\\0'; text++, charCount++) {\n\t\tconst char *temp = std::strchr(ALPHANUMERIC_CHARSET, *text);\n\t\tif (temp == nullptr)\n\t\t\tthrow std::domain_error(\"String contains unencodable characters in alphanumeric mode\");\n\t\taccumData = accumData * 45 + static_cast<int>(temp - ALPHANUMERIC_CHARSET);\n\t\taccumCount++;\n\t\tif (accumCount == 2) {\n\t\t\tbb.appendBits(static_cast<uint32_t>(accumData), 11);\n\t\t\taccumData = 0;\n\t\t\taccumCount = 0;\n\t\t}\n\t}\n\tif (accumCount > 0)  // 1 character remaining\n\t\tbb.appendBits(static_cast<uint32_t>(accumData), 6);\n\treturn QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));\n}\n\n\nvector<QrSegment> QrSegment::makeSegments(const char *text) {\n\t// Select the most efficient segment encoding automatically\n\tvector<QrSegment> result;\n\tif (*text == '\\0');  // Leave result empty\n\telse if (isNumeric(text))\n\t\tresult.push_back(makeNumeric(text));\n\telse if (isAlphanumeric(text))\n\t\tresult.push_back(makeAlphanumeric(text));\n\telse {\n\t\tvector<uint8_t> bytes;\n\t\tfor (; *text != '\\0'; text++)\n\t\t\tbytes.push_back(static_cast<uint8_t>(*text));\n\t\tresult.push_back(makeBytes(bytes));\n\t}\n\treturn result;\n}\n\n\nQrSegment QrSegment::makeEci(long assignVal) {\n\tBitBuffer bb;\n\tif (assignVal < 0)\n\t\tthrow std::domain_error(\"ECI assignment value out of range\");\n\telse if (assignVal < (1 << 7))\n\t\tbb.appendBits(static_cast<uint32_t>(assignVal), 8);\n\telse if (assignVal < (1 << 14)) {\n\t\tbb.appendBits(2, 2);\n\t\tbb.appendBits(static_cast<uint32_t>(assignVal), 14);\n\t} else if (assignVal < 1000000L) {\n\t\tbb.appendBits(6, 3);\n\t\tbb.appendBits(static_cast<uint32_t>(assignVal), 21);\n\t} else\n\t\tthrow std::domain_error(\"ECI assignment value out of range\");\n\treturn QrSegment(Mode::ECI, 0, std::move(bb));\n}\n\n\nQrSegment::QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt) :\n\t\tmode(&md),\n\t\tnumChars(numCh),\n\t\tdata(dt) {\n\tif (numCh < 0)\n\t\tthrow std::domain_error(\"Invalid value\");\n}\n\n\nQrSegment::QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt) :\n\t\tmode(&md),\n\t\tnumChars(numCh),\n\t\tdata(std::move(dt)) {\n\tif (numCh < 0)\n\t\tthrow std::domain_error(\"Invalid value\");\n}\n\n\nint QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {\n\tint result = 0;\n\tfor (const QrSegment &seg : segs) {\n\t\tint ccbits = seg.mode->numCharCountBits(version);\n\t\tif (seg.numChars >= (1L << ccbits))\n\t\t\treturn -1;  // The segment's length doesn't fit the field's bit width\n\t\tif (4 + ccbits > INT_MAX - result)\n\t\t\treturn -1;  // The sum will overflow an int type\n\t\tresult += 4 + ccbits;\n\t\tif (seg.data.size() > static_cast<unsigned int>(INT_MAX - result))\n\t\t\treturn -1;  // The sum will overflow an int type\n\t\tresult += static_cast<int>(seg.data.size());\n\t}\n\treturn result;\n}\n\n\nbool QrSegment::isNumeric(const char *text) {\n\tfor (; *text != '\\0'; text++) {\n\t\tchar c = *text;\n\t\tif (c < '0' || c > '9')\n\t\t\treturn false;\n\t}\n\treturn true;\n}\n\n\nbool QrSegment::isAlphanumeric(const char *text) {\n\tfor (; *text != '\\0'; text++) {\n\t\tif (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)\n\t\t\treturn false;\n\t}\n\treturn true;\n}\n\n\nconst QrSegment::Mode &QrSegment::getMode() const {\n\treturn *mode;\n}\n\n\nint QrSegment::getNumChars() const {\n\treturn numChars;\n}\n\n\nconst std::vector<bool> &QrSegment::getData() const {\n\treturn data;\n}\n\n\nconst char *QrSegment::ALPHANUMERIC_CHARSET = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\n\n\n/*---- Class QrCode ----*/\n\nint QrCode::getFormatBits(Ecc ecl) {\n\tswitch (ecl) {\n\t\tcase Ecc::LOW     :  return 1;\n\t\tcase Ecc::MEDIUM  :  return 0;\n\t\tcase Ecc::QUARTILE:  return 3;\n\t\tcase Ecc::HIGH    :  return 2;\n\t\tdefault:  throw std::logic_error(\"Unreachable\");\n\t}\n}\n\n\nQrCode QrCode::encodeText(const char *text, Ecc ecl) {\n\tvector<QrSegment> segs = QrSegment::makeSegments(text);\n\treturn encodeSegments(segs, ecl);\n}\n\n\nQrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) {\n\tvector<QrSegment> segs{QrSegment::makeBytes(data)};\n\treturn encodeSegments(segs, ecl);\n}\n\n\nQrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,\n\t\tint minVersion, int maxVersion, int mask, bool boostEcl) {\n\tif (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)\n\t\tthrow std::invalid_argument(\"Invalid value\");\n\t\n\t// Find the minimal version number to use\n\tint version, dataUsedBits;\n\tfor (version = minVersion; ; version++) {\n\t\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\tdataUsedBits = QrSegment::getTotalBits(segs, version);\n\t\tif (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)\n\t\t\tbreak;  // This version number is found to be suitable\n\t\tif (version >= maxVersion) {  // All versions in the range could not fit the given data\n\t\t\tstd::ostringstream sb;\n\t\t\tif (dataUsedBits == -1)\n\t\t\t\tsb << \"Segment too long\";\n\t\t\telse {\n\t\t\t\tsb << \"Data length = \" << dataUsedBits << \" bits, \";\n\t\t\t\tsb << \"Max capacity = \" << dataCapacityBits << \" bits\";\n\t\t\t}\n\t\t\tthrow data_too_long(sb.str());\n\t\t}\n\t}\n\tassert(dataUsedBits != -1);\n\t\n\t// Increase the error correction level while the data still fits in the current version number\n\tfor (Ecc newEcl : {Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) {  // From low to high\n\t\tif (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)\n\t\t\tecl = newEcl;\n\t}\n\t\n\t// Concatenate all segments to create the data bit string\n\tBitBuffer bb;\n\tfor (const QrSegment &seg : segs) {\n\t\tbb.appendBits(static_cast<uint32_t>(seg.getMode().getModeBits()), 4);\n\t\tbb.appendBits(static_cast<uint32_t>(seg.getNumChars()), seg.getMode().numCharCountBits(version));\n\t\tbb.insert(bb.end(), seg.getData().begin(), seg.getData().end());\n\t}\n\tassert(bb.size() == static_cast<unsigned int>(dataUsedBits));\n\t\n\t// Add terminator and pad up to a byte if applicable\n\tsize_t dataCapacityBits = static_cast<size_t>(getNumDataCodewords(version, ecl)) * 8;\n\tassert(bb.size() <= dataCapacityBits);\n\tbb.appendBits(0, std::min(4, static_cast<int>(dataCapacityBits - bb.size())));\n\tbb.appendBits(0, (8 - static_cast<int>(bb.size() % 8)) % 8);\n\tassert(bb.size() % 8 == 0);\n\t\n\t// Pad with alternating bytes until data capacity is reached\n\tfor (uint8_t padByte = 0xEC; bb.size() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)\n\t\tbb.appendBits(padByte, 8);\n\t\n\t// Pack bits into bytes in big endian\n\tvector<uint8_t> dataCodewords(bb.size() / 8);\n\tfor (size_t i = 0; i < bb.size(); i++)\n\t\tdataCodewords.at(i >> 3) |= (bb.at(i) ? 1 : 0) << (7 - (i & 7));\n\t\n\t// Create the QR Code object\n\treturn QrCode(version, ecl, dataCodewords, mask);\n}\n\n\nQrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int msk) :\n\t\t// Initialize fields and check arguments\n\t\tversion(ver),\n\t\terrorCorrectionLevel(ecl) {\n\tif (ver < MIN_VERSION || ver > MAX_VERSION)\n\t\tthrow std::domain_error(\"Version value out of range\");\n\tif (msk < -1 || msk > 7)\n\t\tthrow std::domain_error(\"Mask value out of range\");\n\tsize = ver * 4 + 17;\n\tsize_t sz = static_cast<size_t>(size);\n\tmodules    = vector<vector<bool> >(sz, vector<bool>(sz));  // Initially all light\n\tisFunction = vector<vector<bool> >(sz, vector<bool>(sz));\n\t\n\t// Compute ECC, draw modules\n\tdrawFunctionPatterns();\n\tconst vector<uint8_t> allCodewords = addEccAndInterleave(dataCodewords);\n\tdrawCodewords(allCodewords);\n\t\n\t// Do masking\n\tif (msk == -1) {  // Automatically choose best mask\n\t\tlong minPenalty = LONG_MAX;\n\t\tfor (int i = 0; i < 8; i++) {\n\t\t\tapplyMask(i);\n\t\t\tdrawFormatBits(i);\n\t\t\tlong penalty = getPenaltyScore();\n\t\t\tif (penalty < minPenalty) {\n\t\t\t\tmsk = i;\n\t\t\t\tminPenalty = penalty;\n\t\t\t}\n\t\t\tapplyMask(i);  // Undoes the mask due to XOR\n\t\t}\n\t}\n\tassert(0 <= msk && msk <= 7);\n\tmask = msk;\n\tapplyMask(msk);  // Apply the final choice of mask\n\tdrawFormatBits(msk);  // Overwrite old format bits\n\t\n\tisFunction.clear();\n\tisFunction.shrink_to_fit();\n}\n\n\nint QrCode::getVersion() const {\n\treturn version;\n}\n\n\nint QrCode::getSize() const {\n\treturn size;\n}\n\n\nQrCode::Ecc QrCode::getErrorCorrectionLevel() const {\n\treturn errorCorrectionLevel;\n}\n\n\nint QrCode::getMask() const {\n\treturn mask;\n}\n\n\nbool QrCode::getModule(int x, int y) const {\n\treturn 0 <= x && x < size && 0 <= y && y < size && module(x, y);\n}\n\n\nvoid QrCode::drawFunctionPatterns() {\n\t// Draw horizontal and vertical timing patterns\n\tfor (int i = 0; i < size; i++) {\n\t\tsetFunctionModule(6, i, i % 2 == 0);\n\t\tsetFunctionModule(i, 6, i % 2 == 0);\n\t}\n\t\n\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\tdrawFinderPattern(3, 3);\n\tdrawFinderPattern(size - 4, 3);\n\tdrawFinderPattern(3, size - 4);\n\t\n\t// Draw numerous alignment patterns\n\tconst vector<int> alignPatPos = getAlignmentPatternPositions();\n\tsize_t numAlign = alignPatPos.size();\n\tfor (size_t i = 0; i < numAlign; i++) {\n\t\tfor (size_t j = 0; j < numAlign; j++) {\n\t\t\t// Don't draw on the three finder corners\n\t\t\tif (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))\n\t\t\t\tdrawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));\n\t\t}\n\t}\n\t\n\t// Draw configuration data\n\tdrawFormatBits(0);  // Dummy mask value; overwritten later in the constructor\n\tdrawVersion();\n}\n\n\nvoid QrCode::drawFormatBits(int msk) {\n\t// Calculate error correction code and pack bits\n\tint data = getFormatBits(errorCorrectionLevel) << 3 | msk;  // errCorrLvl is uint2, msk is uint3\n\tint rem = data;\n\tfor (int i = 0; i < 10; i++)\n\t\trem = (rem << 1) ^ ((rem >> 9) * 0x537);\n\tint bits = (data << 10 | rem) ^ 0x5412;  // uint15\n\tassert(bits >> 15 == 0);\n\t\n\t// Draw first copy\n\tfor (int i = 0; i <= 5; i++)\n\t\tsetFunctionModule(8, i, getBit(bits, i));\n\tsetFunctionModule(8, 7, getBit(bits, 6));\n\tsetFunctionModule(8, 8, getBit(bits, 7));\n\tsetFunctionModule(7, 8, getBit(bits, 8));\n\tfor (int i = 9; i < 15; i++)\n\t\tsetFunctionModule(14 - i, 8, getBit(bits, i));\n\t\n\t// Draw second copy\n\tfor (int i = 0; i < 8; i++)\n\t\tsetFunctionModule(size - 1 - i, 8, getBit(bits, i));\n\tfor (int i = 8; i < 15; i++)\n\t\tsetFunctionModule(8, size - 15 + i, getBit(bits, i));\n\tsetFunctionModule(8, size - 8, true);  // Always dark\n}\n\n\nvoid QrCode::drawVersion() {\n\tif (version < 7)\n\t\treturn;\n\t\n\t// Calculate error correction code and pack bits\n\tint rem = version;  // version is uint6, in the range [7, 40]\n\tfor (int i = 0; i < 12; i++)\n\t\trem = (rem << 1) ^ ((rem >> 11) * 0x1F25);\n\tlong bits = static_cast<long>(version) << 12 | rem;  // uint18\n\tassert(bits >> 18 == 0);\n\t\n\t// Draw two copies\n\tfor (int i = 0; i < 18; i++) {\n\t\tbool bit = getBit(bits, i);\n\t\tint a = size - 11 + i % 3;\n\t\tint b = i / 3;\n\t\tsetFunctionModule(a, b, bit);\n\t\tsetFunctionModule(b, a, bit);\n\t}\n}\n\n\nvoid QrCode::drawFinderPattern(int x, int y) {\n\tfor (int dy = -4; dy <= 4; dy++) {\n\t\tfor (int dx = -4; dx <= 4; dx++) {\n\t\t\tint dist = std::max(std::abs(dx), std::abs(dy));  // Chebyshev/infinity norm\n\t\t\tint xx = x + dx, yy = y + dy;\n\t\t\tif (0 <= xx && xx < size && 0 <= yy && yy < size)\n\t\t\t\tsetFunctionModule(xx, yy, dist != 2 && dist != 4);\n\t\t}\n\t}\n}\n\n\nvoid QrCode::drawAlignmentPattern(int x, int y) {\n\tfor (int dy = -2; dy <= 2; dy++) {\n\t\tfor (int dx = -2; dx <= 2; dx++)\n\t\t\tsetFunctionModule(x + dx, y + dy, std::max(std::abs(dx), std::abs(dy)) != 1);\n\t}\n}\n\n\nvoid QrCode::setFunctionModule(int x, int y, bool isDark) {\n\tsize_t ux = static_cast<size_t>(x);\n\tsize_t uy = static_cast<size_t>(y);\n\tmodules   .at(uy).at(ux) = isDark;\n\tisFunction.at(uy).at(ux) = true;\n}\n\n\nbool QrCode::module(int x, int y) const {\n\treturn modules.at(static_cast<size_t>(y)).at(static_cast<size_t>(x));\n}\n\n\nvector<uint8_t> QrCode::addEccAndInterleave(const vector<uint8_t> &data) const {\n\tif (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))\n\t\tthrow std::invalid_argument(\"Invalid argument\");\n\t\n\t// Calculate parameter numbers\n\tint numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version];\n\tint blockEccLen = ECC_CODEWORDS_PER_BLOCK  [static_cast<int>(errorCorrectionLevel)][version];\n\tint rawCodewords = getNumRawDataModules(version) / 8;\n\tint numShortBlocks = numBlocks - rawCodewords % numBlocks;\n\tint shortBlockLen = rawCodewords / numBlocks;\n\t\n\t// Split data into blocks and append ECC to each block\n\tvector<vector<uint8_t> > blocks;\n\tconst vector<uint8_t> rsDiv = reedSolomonComputeDivisor(blockEccLen);\n\tfor (int i = 0, k = 0; i < numBlocks; i++) {\n\t\tvector<uint8_t> dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));\n\t\tk += static_cast<int>(dat.size());\n\t\tconst vector<uint8_t> ecc = reedSolomonComputeRemainder(dat, rsDiv);\n\t\tif (i < numShortBlocks)\n\t\t\tdat.push_back(0);\n\t\tdat.insert(dat.end(), ecc.cbegin(), ecc.cend());\n\t\tblocks.push_back(std::move(dat));\n\t}\n\t\n\t// Interleave (not concatenate) the bytes from every block into a single sequence\n\tvector<uint8_t> result;\n\tfor (size_t i = 0; i < blocks.at(0).size(); i++) {\n\t\tfor (size_t j = 0; j < blocks.size(); j++) {\n\t\t\t// Skip the padding byte in short blocks\n\t\t\tif (i != static_cast<unsigned int>(shortBlockLen - blockEccLen) || j >= static_cast<unsigned int>(numShortBlocks))\n\t\t\t\tresult.push_back(blocks.at(j).at(i));\n\t\t}\n\t}\n\tassert(result.size() == static_cast<unsigned int>(rawCodewords));\n\treturn result;\n}\n\n\nvoid QrCode::drawCodewords(const vector<uint8_t> &data) {\n\tif (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))\n\t\tthrow std::invalid_argument(\"Invalid argument\");\n\t\n\tsize_t i = 0;  // Bit index into the data\n\t// Do the funny zigzag scan\n\tfor (int right = size - 1; right >= 1; right -= 2) {  // Index of right column in each column pair\n\t\tif (right == 6)\n\t\t\tright = 5;\n\t\tfor (int vert = 0; vert < size; vert++) {  // Vertical counter\n\t\t\tfor (int j = 0; j < 2; j++) {\n\t\t\t\tsize_t x = static_cast<size_t>(right - j);  // Actual x coordinate\n\t\t\t\tbool upward = ((right + 1) & 2) == 0;\n\t\t\t\tsize_t y = static_cast<size_t>(upward ? size - 1 - vert : vert);  // Actual y coordinate\n\t\t\t\tif (!isFunction.at(y).at(x) && i < data.size() * 8) {\n\t\t\t\t\tmodules.at(y).at(x) = getBit(data.at(i >> 3), 7 - static_cast<int>(i & 7));\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\t// If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t// 0/false/light by the constructor and are left unchanged by this method\n\t\t\t}\n\t\t}\n\t}\n\tassert(i == data.size() * 8);\n}\n\n\nvoid QrCode::applyMask(int msk) {\n\tif (msk < 0 || msk > 7)\n\t\tthrow std::domain_error(\"Mask value out of range\");\n\tsize_t sz = static_cast<size_t>(size);\n\tfor (size_t y = 0; y < sz; y++) {\n\t\tfor (size_t x = 0; x < sz; x++) {\n\t\t\tbool invert;\n\t\t\tswitch (msk) {\n\t\t\t\tcase 0:  invert = (x + y) % 2 == 0;                    break;\n\t\t\t\tcase 1:  invert = y % 2 == 0;                          break;\n\t\t\t\tcase 2:  invert = x % 3 == 0;                          break;\n\t\t\t\tcase 3:  invert = (x + y) % 3 == 0;                    break;\n\t\t\t\tcase 4:  invert = (x / 3 + y / 2) % 2 == 0;            break;\n\t\t\t\tcase 5:  invert = x * y % 2 + x * y % 3 == 0;          break;\n\t\t\t\tcase 6:  invert = (x * y % 2 + x * y % 3) % 2 == 0;    break;\n\t\t\t\tcase 7:  invert = ((x + y) % 2 + x * y % 3) % 2 == 0;  break;\n\t\t\t\tdefault:  throw std::logic_error(\"Unreachable\");\n\t\t\t}\n\t\t\tmodules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));\n\t\t}\n\t}\n}\n\n\nlong QrCode::getPenaltyScore() const {\n\tlong result = 0;\n\t\n\t// Adjacent modules in row having same color, and finder-like patterns\n\tfor (int y = 0; y < size; y++) {\n\t\tbool runColor = false;\n\t\tint runX = 0;\n\t\tstd::array<int,7> runHistory = {};\n\t\tfor (int x = 0; x < size; x++) {\n\t\t\tif (module(x, y) == runColor) {\n\t\t\t\trunX++;\n\t\t\t\tif (runX == 5)\n\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\telse if (runX > 5)\n\t\t\t\t\tresult++;\n\t\t\t} else {\n\t\t\t\tfinderPenaltyAddHistory(runX, runHistory);\n\t\t\t\tif (!runColor)\n\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;\n\t\t\t\trunColor = module(x, y);\n\t\t\t\trunX = 1;\n\t\t\t}\n\t\t}\n\t\tresult += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;\n\t}\n\t// Adjacent modules in column having same color, and finder-like patterns\n\tfor (int x = 0; x < size; x++) {\n\t\tbool runColor = false;\n\t\tint runY = 0;\n\t\tstd::array<int,7> runHistory = {};\n\t\tfor (int y = 0; y < size; y++) {\n\t\t\tif (module(x, y) == runColor) {\n\t\t\t\trunY++;\n\t\t\t\tif (runY == 5)\n\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\telse if (runY > 5)\n\t\t\t\t\tresult++;\n\t\t\t} else {\n\t\t\t\tfinderPenaltyAddHistory(runY, runHistory);\n\t\t\t\tif (!runColor)\n\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;\n\t\t\t\trunColor = module(x, y);\n\t\t\t\trunY = 1;\n\t\t\t}\n\t\t}\n\t\tresult += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;\n\t}\n\t\n\t// 2*2 blocks of modules having same color\n\tfor (int y = 0; y < size - 1; y++) {\n\t\tfor (int x = 0; x < size - 1; x++) {\n\t\t\tbool  color = module(x, y);\n\t\t\tif (  color == module(x + 1, y) &&\n\t\t\t      color == module(x, y + 1) &&\n\t\t\t      color == module(x + 1, y + 1))\n\t\t\t\tresult += PENALTY_N2;\n\t\t}\n\t}\n\t\n\t// Balance of dark and light modules\n\tint dark = 0;\n\tfor (const vector<bool> &row : modules) {\n\t\tfor (bool color : row) {\n\t\t\tif (color)\n\t\t\t\tdark++;\n\t\t}\n\t}\n\tint total = size * size;  // Note that size is odd, so dark/total != 1/2\n\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\tint k = static_cast<int>((std::abs(dark * 20L - total * 10L) + total - 1) / total) - 1;\n\tassert(0 <= k && k <= 9);\n\tresult += k * PENALTY_N4;\n\tassert(0 <= result && result <= 2568888L);  // Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\treturn result;\n}\n\n\nvector<int> QrCode::getAlignmentPatternPositions() const {\n\tif (version == 1)\n\t\treturn vector<int>();\n\telse {\n\t\tint numAlign = version / 7 + 2;\n\t\tint step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;\n\t\tvector<int> result;\n\t\tfor (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)\n\t\t\tresult.insert(result.begin(), pos);\n\t\tresult.insert(result.begin(), 6);\n\t\treturn result;\n\t}\n}\n\n\nint QrCode::getNumRawDataModules(int ver) {\n\tif (ver < MIN_VERSION || ver > MAX_VERSION)\n\t\tthrow std::domain_error(\"Version number out of range\");\n\tint result = (16 * ver + 128) * ver + 64;\n\tif (ver >= 2) {\n\t\tint numAlign = ver / 7 + 2;\n\t\tresult -= (25 * numAlign - 10) * numAlign - 55;\n\t\tif (ver >= 7)\n\t\t\tresult -= 36;\n\t}\n\tassert(208 <= result && result <= 29648);\n\treturn result;\n}\n\n\nint QrCode::getNumDataCodewords(int ver, Ecc ecl) {\n\treturn getNumRawDataModules(ver) / 8\n\t\t- ECC_CODEWORDS_PER_BLOCK    [static_cast<int>(ecl)][ver]\n\t\t* NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];\n}\n\n\nvector<uint8_t> QrCode::reedSolomonComputeDivisor(int degree) {\n\tif (degree < 1 || degree > 255)\n\t\tthrow std::domain_error(\"Degree out of range\");\n\t// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.\n\tvector<uint8_t> result(static_cast<size_t>(degree));\n\tresult.at(result.size() - 1) = 1;  // Start off with the monomial x^0\n\t\n\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t// and drop the highest monomial term which is always 1x^degree.\n\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\tuint8_t root = 1;\n\tfor (int i = 0; i < degree; i++) {\n\t\t// Multiply the current product by (x - r^i)\n\t\tfor (size_t j = 0; j < result.size(); j++) {\n\t\t\tresult.at(j) = reedSolomonMultiply(result.at(j), root);\n\t\t\tif (j + 1 < result.size())\n\t\t\t\tresult.at(j) ^= result.at(j + 1);\n\t\t}\n\t\troot = reedSolomonMultiply(root, 0x02);\n\t}\n\treturn result;\n}\n\n\nvector<uint8_t> QrCode::reedSolomonComputeRemainder(const vector<uint8_t> &data, const vector<uint8_t> &divisor) {\n\tvector<uint8_t> result(divisor.size());\n\tfor (uint8_t b : data) {  // Polynomial division\n\t\tuint8_t factor = b ^ result.at(0);\n\t\tresult.erase(result.begin());\n\t\tresult.push_back(0);\n\t\tfor (size_t i = 0; i < result.size(); i++)\n\t\t\tresult.at(i) ^= reedSolomonMultiply(divisor.at(i), factor);\n\t}\n\treturn result;\n}\n\n\nuint8_t QrCode::reedSolomonMultiply(uint8_t x, uint8_t y) {\n\t// Russian peasant multiplication\n\tint z = 0;\n\tfor (int i = 7; i >= 0; i--) {\n\t\tz = (z << 1) ^ ((z >> 7) * 0x11D);\n\t\tz ^= ((y >> i) & 1) * x;\n\t}\n\tassert(z >> 8 == 0);\n\treturn static_cast<uint8_t>(z);\n}\n\n\nint QrCode::finderPenaltyCountPatterns(const std::array<int,7> &runHistory) const {\n\tint n = runHistory.at(1);\n\tassert(n <= size * 3);\n\tbool core = n > 0 && runHistory.at(2) == n && runHistory.at(3) == n * 3 && runHistory.at(4) == n && runHistory.at(5) == n;\n\treturn (core && runHistory.at(0) >= n * 4 && runHistory.at(6) >= n ? 1 : 0)\n\t     + (core && runHistory.at(6) >= n * 4 && runHistory.at(0) >= n ? 1 : 0);\n}\n\n\nint QrCode::finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int,7> &runHistory) const {\n\tif (currentRunColor) {  // Terminate dark run\n\t\tfinderPenaltyAddHistory(currentRunLength, runHistory);\n\t\tcurrentRunLength = 0;\n\t}\n\tcurrentRunLength += size;  // Add light border to final run\n\tfinderPenaltyAddHistory(currentRunLength, runHistory);\n\treturn finderPenaltyCountPatterns(runHistory);\n}\n\n\nvoid QrCode::finderPenaltyAddHistory(int currentRunLength, std::array<int,7> &runHistory) const {\n\tif (runHistory.at(0) == 0)\n\t\tcurrentRunLength += size;  // Add light border to initial run\n\tstd::copy_backward(runHistory.cbegin(), runHistory.cend() - 1, runHistory.end());\n\trunHistory.at(0) = currentRunLength;\n}\n\n\nbool QrCode::getBit(long x, int i) {\n\treturn ((x >> i) & 1) != 0;\n}\n\n\n/*---- Tables of constants ----*/\n\nconst int QrCode::PENALTY_N1 =  3;\nconst int QrCode::PENALTY_N2 =  3;\nconst int QrCode::PENALTY_N3 = 40;\nconst int QrCode::PENALTY_N4 = 10;\n\n\nconst int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t{-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\n\t{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},  // Medium\n\t{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // Quartile\n\t{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // High\n};\n\nconst int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25},  // Low\n\t{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49},  // Medium\n\t{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68},  // Quartile\n\t{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81},  // High\n};\n\n\ndata_too_long::data_too_long(const std::string &msg) :\n\tstd::length_error(msg) {}\n\n\n\n/*---- Class BitBuffer ----*/\n\nBitBuffer::BitBuffer()\n\t: std::vector<bool>() {}\n\n\nvoid BitBuffer::appendBits(std::uint32_t val, int len) {\n\tif (len < 0 || len > 31 || val >> len != 0)\n\t\tthrow std::domain_error(\"Value out of range\");\n\tfor (int i = len - 1; i >= 0; i--)  // Append bit by bit\n\t\tthis->push_back(((val >> i) & 1) != 0);\n}\n\n}\n"
  },
  {
    "path": "cpp/qrcodegen.hpp",
    "content": "/* \n * QR Code generator library (C++)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n#pragma once\n\n#include <array>\n#include <cstdint>\n#include <stdexcept>\n#include <string>\n#include <vector>\n\n\nnamespace qrcodegen {\n\n/* \n * A segment of character/binary/control data in a QR Code symbol.\n * Instances of this class are immutable.\n * The mid-level way to create a segment is to take the payload data\n * and call a static factory function such as QrSegment::makeNumeric().\n * The low-level way to create a segment is to custom-make the bit buffer\n * and call the QrSegment() constructor with appropriate values.\n * This segment class imposes no length restrictions, but QR Codes have restrictions.\n * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n * Any segment longer than this is meaningless for the purpose of generating QR Codes.\n */\nclass QrSegment final {\n\t\n\t/*---- Public helper enumeration ----*/\n\t\n\t/* \n\t * Describes how a segment's data bits are interpreted. Immutable.\n\t */\n\tpublic: class Mode final {\n\t\t\n\t\t/*-- Constants --*/\n\t\t\n\t\tpublic: static const Mode NUMERIC;\n\t\tpublic: static const Mode ALPHANUMERIC;\n\t\tpublic: static const Mode BYTE;\n\t\tpublic: static const Mode KANJI;\n\t\tpublic: static const Mode ECI;\n\t\t\n\t\t\n\t\t/*-- Fields --*/\n\t\t\n\t\t// The mode indicator bits, which is a uint4 value (range 0 to 15).\n\t\tprivate: int modeBits;\n\t\t\n\t\t// Number of character count bits for three different version ranges.\n\t\tprivate: int numBitsCharCount[3];\n\t\t\n\t\t\n\t\t/*-- Constructor --*/\n\t\t\n\t\tprivate: Mode(int mode, int cc0, int cc1, int cc2);\n\t\t\n\t\t\n\t\t/*-- Methods --*/\n\t\t\n\t\t/* \n\t\t * (Package-private) Returns the mode indicator bits, which is an unsigned 4-bit value (range 0 to 15).\n\t\t */\n\t\tpublic: int getModeBits() const;\n\t\t\n\t\t/* \n\t\t * (Package-private) Returns the bit width of the character count field for a segment in\n\t\t * this mode in a QR Code at the given version number. The result is in the range [0, 16].\n\t\t */\n\t\tpublic: int numCharCountBits(int ver) const;\n\t\t\n\t};\n\t\n\t\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/* \n\t * Returns a segment representing the given binary data encoded in\n\t * byte mode. All input byte vectors are acceptable. Any text string\n\t * can be converted to UTF-8 bytes and encoded as a byte mode segment.\n\t */\n\tpublic: static QrSegment makeBytes(const std::vector<std::uint8_t> &data);\n\t\n\t\n\t/* \n\t * Returns a segment representing the given string of decimal digits encoded in numeric mode.\n\t */\n\tpublic: static QrSegment makeNumeric(const char *digits);\n\t\n\t\n\t/* \n\t * Returns a segment representing the given text string encoded in alphanumeric mode.\n\t * The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t * dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t */\n\tpublic: static QrSegment makeAlphanumeric(const char *text);\n\t\n\t\n\t/* \n\t * Returns a list of zero or more segments to represent the given text string. The result\n\t * may use various segment modes and switch modes to optimize the length of the bit stream.\n\t */\n\tpublic: static std::vector<QrSegment> makeSegments(const char *text);\n\t\n\t\n\t/* \n\t * Returns a segment representing an Extended Channel Interpretation\n\t * (ECI) designator with the given assignment value.\n\t */\n\tpublic: static QrSegment makeEci(long assignVal);\n\t\n\t\n\t/*---- Public static helper functions ----*/\n\t\n\t/* \n\t * Tests whether the given string can be encoded as a segment in numeric mode.\n\t * A string is encodable iff each character is in the range 0 to 9.\n\t */\n\tpublic: static bool isNumeric(const char *text);\n\t\n\t\n\t/* \n\t * Tests whether the given string can be encoded as a segment in alphanumeric mode.\n\t * A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t */\n\tpublic: static bool isAlphanumeric(const char *text);\n\t\n\t\n\t\n\t/*---- Instance fields ----*/\n\t\n\t/* The mode indicator of this segment. Accessed through getMode(). */\n\tprivate: const Mode *mode;\n\t\n\t/* The length of this segment's unencoded data. Measured in characters for\n\t * numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t * Always zero or positive. Not the same as the data's bit length.\n\t * Accessed through getNumChars(). */\n\tprivate: int numChars;\n\t\n\t/* The data bits of this segment. Accessed through getData(). */\n\tprivate: std::vector<bool> data;\n\t\n\t\n\t/*---- Constructors (low level) ----*/\n\t\n\t/* \n\t * Creates a new QR Code segment with the given attributes and data.\n\t * The character count (numCh) must agree with the mode and the bit buffer length,\n\t * but the constraint isn't checked. The given bit buffer is copied and stored.\n\t */\n\tpublic: QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);\n\t\n\t\n\t/* \n\t * Creates a new QR Code segment with the given parameters and data.\n\t * The character count (numCh) must agree with the mode and the bit buffer length,\n\t * but the constraint isn't checked. The given bit buffer is moved and stored.\n\t */\n\tpublic: QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);\n\t\n\t\n\t/*---- Methods ----*/\n\t\n\t/* \n\t * Returns the mode field of this segment.\n\t */\n\tpublic: const Mode &getMode() const;\n\t\n\t\n\t/* \n\t * Returns the character count field of this segment.\n\t */\n\tpublic: int getNumChars() const;\n\t\n\t\n\t/* \n\t * Returns the data bits of this segment.\n\t */\n\tpublic: const std::vector<bool> &getData() const;\n\t\n\t\n\t// (Package-private) Calculates the number of bits needed to encode the given segments at\n\t// the given version. Returns a non-negative number if successful. Otherwise returns -1 if a\n\t// segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.\n\tpublic: static int getTotalBits(const std::vector<QrSegment> &segs, int version);\n\t\n\t\n\t/*---- Private constant ----*/\n\t\n\t/* The set of all legal characters in alphanumeric mode, where\n\t * each character value maps to the index in the string. */\n\tprivate: static const char *ALPHANUMERIC_CHARSET;\n\t\n};\n\n\n\n/* \n * A QR Code symbol, which is a type of two-dimension barcode.\n * Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n * Instances of this class represent an immutable square grid of dark and light cells.\n * The class provides static factory functions to create a QR Code from text or binary data.\n * The class covers the QR Code Model 2 specification, supporting all versions (sizes)\n * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.\n * \n * Ways to create a QR Code object:\n * - High level: Take the payload data and call QrCode::encodeText() or QrCode::encodeBinary().\n * - Mid level: Custom-make the list of segments and call QrCode::encodeSegments().\n * - Low level: Custom-make the array of data codeword bytes (including\n *   segment headers and final padding, excluding error correction codewords),\n *   supply the appropriate version number, and call the QrCode() constructor.\n * (Note that all ways require supplying the desired error correction level.)\n */\nclass QrCode final {\n\t\n\t/*---- Public helper enumeration ----*/\n\t\n\t/* \n\t * The error correction level in a QR Code symbol.\n\t */\n\tpublic: enum class Ecc {\n\t\tLOW = 0 ,  // The QR Code can tolerate about  7% erroneous codewords\n\t\tMEDIUM  ,  // The QR Code can tolerate about 15% erroneous codewords\n\t\tQUARTILE,  // The QR Code can tolerate about 25% erroneous codewords\n\t\tHIGH    ,  // The QR Code can tolerate about 30% erroneous codewords\n\t};\n\t\n\t\n\t// Returns a value in the range 0 to 3 (unsigned 2-bit integer).\n\tprivate: static int getFormatBits(Ecc ecl);\n\t\n\t\n\t\n\t/*---- Static factory functions (high level) ----*/\n\t\n\t/* \n\t * Returns a QR Code representing the given Unicode text string at the given error correction level.\n\t * As a conservative upper bound, this function is guaranteed to succeed for strings that have 2953 or fewer\n\t * UTF-8 code units (not Unicode code points) if the low error correction level is used. The smallest possible\n\t * QR Code version is automatically chosen for the output. The ECC level of the result may be higher than\n\t * the ecl argument if it can be done without increasing the version.\n\t */\n\tpublic: static QrCode encodeText(const char *text, Ecc ecl);\n\t\n\t\n\t/* \n\t * Returns a QR Code representing the given binary data at the given error correction level.\n\t * This function always encodes using the binary segment mode, not any text mode. The maximum number of\n\t * bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.\n\t * The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t */\n\tpublic: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);\n\t\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/* \n\t * Returns a QR Code representing the given segments with the given encoding parameters.\n\t * The smallest possible QR Code version within the given range is automatically\n\t * chosen for the output. Iff boostEcl is true, then the ECC level of the result\n\t * may be higher than the ecl argument if it can be done without increasing the\n\t * version. The mask number is either between 0 to 7 (inclusive) to force that\n\t * mask, or -1 to automatically choose an appropriate mask (which may be slow).\n\t * This function allows the user to create a custom sequence of segments that switches\n\t * between modes (such as alphanumeric and byte) to encode text in less space.\n\t * This is a mid-level API; the high-level API is encodeText() and encodeBinary().\n\t */\n\tpublic: static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,\n\t\tint minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true);  // All optional parameters\n\t\n\t\n\t\n\t/*---- Instance fields ----*/\n\t\n\t// Immutable scalar parameters:\n\t\n\t/* The version number of this QR Code, which is between 1 and 40 (inclusive).\n\t * This determines the size of this barcode. */\n\tprivate: int version;\n\t\n\t/* The width and height of this QR Code, measured in modules, between\n\t * 21 and 177 (inclusive). This is equal to version * 4 + 17. */\n\tprivate: int size;\n\t\n\t/* The error correction level used in this QR Code. */\n\tprivate: Ecc errorCorrectionLevel;\n\t\n\t/* The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).\n\t * Even if a QR Code is created with automatic masking requested (mask = -1),\n\t * the resulting object still has a mask value between 0 and 7. */\n\tprivate: int mask;\n\t\n\t// Private grids of modules/pixels, with dimensions of size*size:\n\t\n\t// The modules of this QR Code (false = light, true = dark).\n\t// Immutable after constructor finishes. Accessed through getModule().\n\tprivate: std::vector<std::vector<bool> > modules;\n\t\n\t// Indicates function modules that are not subjected to masking. Discarded when constructor finishes.\n\tprivate: std::vector<std::vector<bool> > isFunction;\n\t\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/* \n\t * Creates a new QR Code with the given version number,\n\t * error correction level, data codeword bytes, and mask number.\n\t * This is a low-level API that most users should not use directly.\n\t * A mid-level API is the encodeSegments() function.\n\t */\n\tpublic: QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int msk);\n\t\n\t\n\t\n\t/*---- Public instance methods ----*/\n\t\n\t/* \n\t * Returns this QR Code's version, in the range [1, 40].\n\t */\n\tpublic: int getVersion() const;\n\t\n\t\n\t/* \n\t * Returns this QR Code's size, in the range [21, 177].\n\t */\n\tpublic: int getSize() const;\n\t\n\t\n\t/* \n\t * Returns this QR Code's error correction level.\n\t */\n\tpublic: Ecc getErrorCorrectionLevel() const;\n\t\n\t\n\t/* \n\t * Returns this QR Code's mask, in the range [0, 7].\n\t */\n\tpublic: int getMask() const;\n\t\n\t\n\t/* \n\t * Returns the color of the module (pixel) at the given coordinates, which is false\n\t * for light or true for dark. The top left corner has the coordinates (x=0, y=0).\n\t * If the given coordinates are out of bounds, then false (light) is returned.\n\t */\n\tpublic: bool getModule(int x, int y) const;\n\t\n\t\n\t\n\t/*---- Private helper methods for constructor: Drawing function modules ----*/\n\t\n\t// Reads this object's version field, and draws and marks all function modules.\n\tprivate: void drawFunctionPatterns();\n\t\n\t\n\t// Draws two copies of the format bits (with its own error correction code)\n\t// based on the given mask and this object's error correction level field.\n\tprivate: void drawFormatBits(int msk);\n\t\n\t\n\t// Draws two copies of the version bits (with its own error correction code),\n\t// based on this object's version field, iff 7 <= version <= 40.\n\tprivate: void drawVersion();\n\t\n\t\n\t// Draws a 9*9 finder pattern including the border separator,\n\t// with the center module at (x, y). Modules can be out of bounds.\n\tprivate: void drawFinderPattern(int x, int y);\n\t\n\t\n\t// Draws a 5*5 alignment pattern, with the center module\n\t// at (x, y). All modules must be in bounds.\n\tprivate: void drawAlignmentPattern(int x, int y);\n\t\n\t\n\t// Sets the color of a module and marks it as a function module.\n\t// Only used by the constructor. Coordinates must be in bounds.\n\tprivate: void setFunctionModule(int x, int y, bool isDark);\n\t\n\t\n\t// Returns the color of the module at the given coordinates, which must be in range.\n\tprivate: bool module(int x, int y) const;\n\t\n\t\n\t/*---- Private helper methods for constructor: Codewords and masking ----*/\n\t\n\t// Returns a new byte string representing the given data with the appropriate error correction\n\t// codewords appended to it, based on this object's version and error correction level.\n\tprivate: std::vector<std::uint8_t> addEccAndInterleave(const std::vector<std::uint8_t> &data) const;\n\t\n\t\n\t// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire\n\t// data area of this QR Code. Function modules need to be marked off before this is called.\n\tprivate: void drawCodewords(const std::vector<std::uint8_t> &data);\n\t\n\t\n\t// XORs the codeword modules in this QR Code with the given mask pattern.\n\t// The function modules must be marked and the codeword bits must be drawn\n\t// before masking. Due to the arithmetic of XOR, calling applyMask() with\n\t// the same mask value a second time will undo the mask. A final well-formed\n\t// QR Code needs exactly one (not zero, two, etc.) mask applied.\n\tprivate: void applyMask(int msk);\n\t\n\t\n\t// Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\n\tprivate: long getPenaltyScore() const;\n\t\n\t\n\t\n\t/*---- Private helper functions ----*/\n\t\n\t// Returns an ascending list of positions of alignment patterns for this version number.\n\t// Each position is in the range [0,177), and are used on both the x and y axes.\n\t// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.\n\tprivate: std::vector<int> getAlignmentPatternPositions() const;\n\t\n\t\n\t// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\n\tprivate: static int getNumRawDataModules(int ver);\n\t\n\t\n\t// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t// QR Code of the given version number and error correction level, with remainder bits discarded.\n\t// This stateless pure function could be implemented as a (40*4)-cell lookup table.\n\tprivate: static int getNumDataCodewords(int ver, Ecc ecl);\n\t\n\t\n\t// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be\n\t// implemented as a lookup table over all possible parameter values, instead of as an algorithm.\n\tprivate: static std::vector<std::uint8_t> reedSolomonComputeDivisor(int degree);\n\t\n\t\n\t// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.\n\tprivate: static std::vector<std::uint8_t> reedSolomonComputeRemainder(const std::vector<std::uint8_t> &data, const std::vector<std::uint8_t> &divisor);\n\t\n\t\n\t// Returns the product of the two given field elements modulo GF(2^8/0x11D).\n\t// All inputs are valid. This could be implemented as a 256*256 lookup table.\n\tprivate: static std::uint8_t reedSolomonMultiply(std::uint8_t x, std::uint8_t y);\n\t\n\t\n\t// Can only be called immediately after a light run is added, and\n\t// returns either 0, 1, or 2. A helper function for getPenaltyScore().\n\tprivate: int finderPenaltyCountPatterns(const std::array<int,7> &runHistory) const;\n\t\n\t\n\t// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().\n\tprivate: int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int,7> &runHistory) const;\n\t\n\t\n\t// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().\n\tprivate: void finderPenaltyAddHistory(int currentRunLength, std::array<int,7> &runHistory) const;\n\t\n\t\n\t// Returns true iff the i'th bit of x is set to 1.\n\tprivate: static bool getBit(long x, int i);\n\t\n\t\n\t/*---- Constants and tables ----*/\n\t\n\t// The minimum version number supported in the QR Code Model 2 standard.\n\tpublic: static constexpr int MIN_VERSION =  1;\n\t\n\t// The maximum version number supported in the QR Code Model 2 standard.\n\tpublic: static constexpr int MAX_VERSION = 40;\n\t\n\t\n\t// For use in getPenaltyScore(), when evaluating which mask is best.\n\tprivate: static const int PENALTY_N1;\n\tprivate: static const int PENALTY_N2;\n\tprivate: static const int PENALTY_N3;\n\tprivate: static const int PENALTY_N4;\n\t\n\t\n\tprivate: static const std::int8_t ECC_CODEWORDS_PER_BLOCK[4][41];\n\tprivate: static const std::int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];\n\t\n};\n\n\n\n/*---- Public exception class ----*/\n\n/* \n * Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:\n * - Decrease the error correction level if it was greater than Ecc::LOW.\n * - If the encodeSegments() function was called with a maxVersion argument, then increase\n *   it if it was less than QrCode::MAX_VERSION. (This advice does not apply to the other\n *   factory functions because they search all versions up to QrCode::MAX_VERSION.)\n * - Split the text data into better or optimal segments in order to reduce the number of bits required.\n * - Change the text or binary data to be shorter.\n * - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).\n * - Propagate the error upward to the caller/user.\n */\nclass data_too_long : public std::length_error {\n\t\n\tpublic: explicit data_too_long(const std::string &msg);\n\t\n};\n\n\n\n/* \n * An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.\n */\nclass BitBuffer final : public std::vector<bool> {\n\t\n\t/*---- Constructor ----*/\n\t\n\t// Creates an empty bit buffer (length 0).\n\tpublic: BitBuffer();\n\t\n\t\n\t\n\t/*---- Method ----*/\n\t\n\t// Appends the given number of low-order bits of the given value\n\t// to this buffer. Requires 0 <= len <= 31 and val < 2^len.\n\tpublic: void appendBits(std::uint32_t val, int len);\n\t\n};\n\n}\n"
  },
  {
    "path": "java/QrCodeGeneratorDemo.java",
    "content": "/* \n * QR Code generator demo (Java)\n * \n * Run this command-line program with no arguments. The program creates/overwrites a bunch of\n * PNG and SVG files in the current working directory to demonstrate the creation of QR Codes.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\nimport java.awt.image.BufferedImage;\nimport java.io.File;\nimport java.io.IOException;\nimport java.nio.charset.StandardCharsets;\nimport java.nio.file.Files;\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Objects;\nimport javax.imageio.ImageIO;\nimport io.nayuki.qrcodegen.QrCode;\nimport io.nayuki.qrcodegen.QrSegment;\nimport io.nayuki.qrcodegen.QrSegmentAdvanced;\n\n\npublic final class QrCodeGeneratorDemo {\n\t\n\t// The main application program.\n\tpublic static void main(String[] args) throws IOException {\n\t\tdoBasicDemo();\n\t\tdoVarietyDemo();\n\t\tdoSegmentDemo();\n\t\tdoMaskDemo();\n\t}\n\t\n\t\n\t\n\t/*---- Demo suite ----*/\n\t\n\t// Creates a single QR Code, then writes it to a PNG file and an SVG file.\n\tprivate static void doBasicDemo() throws IOException {\n\t\tString text = \"Hello, world!\";          // User-supplied Unicode text\n\t\tQrCode.Ecc errCorLvl = QrCode.Ecc.LOW;  // Error correction level\n\t\t\n\t\tQrCode qr = QrCode.encodeText(text, errCorLvl);  // Make the QR Code symbol\n\t\t\n\t\tBufferedImage img = toImage(qr, 10, 4);          // Convert to bitmap image\n\t\tFile imgFile = new File(\"hello-world-QR.png\");   // File path for output\n\t\tImageIO.write(img, \"png\", imgFile);              // Write image to file\n\t\t\n\t\tString svg = toSvgString(qr, 4, \"#FFFFFF\", \"#000000\");  // Convert to SVG XML code\n\t\tFile svgFile = new File(\"hello-world-QR.svg\");          // File path for output\n\t\tFiles.write(svgFile.toPath(),                           // Write image to file\n\t\t\tsvg.getBytes(StandardCharsets.UTF_8));\n\t}\n\t\n\t\n\t// Creates a variety of QR Codes that exercise different features of the library, and writes each one to file.\n\tprivate static void doVarietyDemo() throws IOException {\n\t\tQrCode qr;\n\t\t\n\t\t// Numeric mode encoding (3.33 bits per digit)\n\t\tqr = QrCode.encodeText(\"314159265358979323846264338327950288419716939937510\", QrCode.Ecc.MEDIUM);\n\t\twritePng(toImage(qr, 13, 1), \"pi-digits-QR.png\");\n\t\t\n\t\t// Alphanumeric mode encoding (5.5 bits per character)\n\t\tqr = QrCode.encodeText(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", QrCode.Ecc.HIGH);\n\t\twritePng(toImage(qr, 10, 2), \"alphanumeric-QR.png\");\n\t\t\n\t\t// Unicode text as UTF-8\n\t\tqr = QrCode.encodeText(\"こんにちwa、世界！ αβγδ\", QrCode.Ecc.QUARTILE);\n\t\twritePng(toImage(qr, 10, 3), \"unicode-QR.png\");\n\t\t\n\t\t// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\t\tqr = QrCode.encodeText(\n\t\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \"\n\t\t\t+ \"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \"\n\t\t\t+ \"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \"\n\t\t\t+ \"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \"\n\t\t\t+ \"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \"\n\t\t\t+ \"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \"\n\t\t\t+ \"a White Rabbit with pink eyes ran close by her.\", QrCode.Ecc.HIGH);\n\t\twritePng(toImage(qr, 6, 10), \"alice-wonderland-QR.png\");\n\t}\n\t\n\t\n\t// Creates QR Codes with manually specified segments for better compactness.\n\tprivate static void doSegmentDemo() throws IOException {\n\t\tQrCode qr;\n\t\tList<QrSegment> segs;\n\t\t\n\t\t// Illustration \"silver\"\n\t\tString silver0 = \"THE SQUARE ROOT OF 2 IS 1.\";\n\t\tString silver1 = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\t\tqr = QrCode.encodeText(silver0 + silver1, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 10, 3), \"sqrt2-monolithic-QR.png\");\n\t\t\n\t\tsegs = Arrays.asList(\n\t\t\tQrSegment.makeAlphanumeric(silver0),\n\t\t\tQrSegment.makeNumeric(silver1));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 10, 3), \"sqrt2-segmented-QR.png\");\n\t\t\n\t\t// Illustration \"golden\"\n\t\tString golden0 = \"Golden ratio φ = 1.\";\n\t\tString golden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\t\tString golden2 = \"......\";\n\t\tqr = QrCode.encodeText(golden0 + golden1 + golden2, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 8, 5), \"phi-monolithic-QR.png\");\n\t\t\n\t\tsegs = Arrays.asList(\n\t\t\tQrSegment.makeBytes(golden0.getBytes(StandardCharsets.UTF_8)),\n\t\t\tQrSegment.makeNumeric(golden1),\n\t\t\tQrSegment.makeAlphanumeric(golden2));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 8, 5), \"phi-segmented-QR.png\");\n\t\t\n\t\t// Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\t\tString madoka = \"「魔法少女まどか☆マギカ」って、　ИАИ　ｄｅｓｕ　κα？\";\n\t\tqr = QrCode.encodeText(madoka, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 9, 4, 0xFFFFE0, 0x303080), \"madoka-utf8-QR.png\");\n\t\t\n\t\tsegs = Arrays.asList(QrSegmentAdvanced.makeKanji(madoka));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 9, 4, 0xE0F0FF, 0x404040), \"madoka-kanji-QR.png\");\n\t}\n\t\n\t\n\t// Creates QR Codes with the same size and contents but different mask patterns.\n\tprivate static void doMaskDemo() throws IOException {\n\t\tQrCode qr;\n\t\tList<QrSegment> segs;\n\t\t\n\t\t// Project Nayuki URL\n\t\tsegs = QrSegment.makeSegments(\"https://www.nayuki.io/\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true);  // Automatic mask\n\t\twritePng(toImage(qr, 8, 6, 0xE0FFE0, 0x206020), \"project-nayuki-automask-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true);  // Force mask 3\n\t\twritePng(toImage(qr, 8, 6, 0xFFE0E0, 0x602020), \"project-nayuki-mask3-QR.png\");\n\t\t\n\t\t// Chinese text as UTF-8\n\t\tsegs = QrSegment.makeSegments(\"維基百科（Wikipedia，聆聽i/ˌwɪkᵻˈpiːdi.ə/）是一個自由內容、公開編輯且多語言的網路百科全書協作計畫\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 0, true);  // Force mask 0\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask0-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 1, true);  // Force mask 1\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask1-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 5, true);  // Force mask 5\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask5-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 7, true);  // Force mask 7\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask7-QR.png\");\n\t}\n\t\n\t\n\t\n\t/*---- Utilities ----*/\n\t\n\tprivate static BufferedImage toImage(QrCode qr, int scale, int border) {\n\t\treturn toImage(qr, scale, border, 0xFFFFFF, 0x000000);\n\t}\n\t\n\t\n\t/**\n\t * Returns a raster image depicting the specified QR Code, with\n\t * the specified module scale, border modules, and module colors.\n\t * <p>For example, scale=10 and border=4 means to pad the QR Code with 4 light border\n\t * modules on all four sides, and use 10&#xD7;10 pixels to represent each module.\n\t * @param qr the QR Code to render (not {@code null})\n\t * @param scale the side length (measured in pixels, must be positive) of each module\n\t * @param border the number of border modules to add, which must be non-negative\n\t * @param lightColor the color to use for light modules, in 0xRRGGBB format\n\t * @param darkColor the color to use for dark modules, in 0xRRGGBB format\n\t * @return a new image representing the QR Code, with padding and scaling\n\t * @throws NullPointerException if the QR Code is {@code null}\n\t * @throws IllegalArgumentException if the scale or border is out of range, or if\n\t * {scale, border, size} cause the image dimensions to exceed Integer.MAX_VALUE\n\t */\n\tprivate static BufferedImage toImage(QrCode qr, int scale, int border, int lightColor, int darkColor) {\n\t\tObjects.requireNonNull(qr);\n\t\tif (scale <= 0 || border < 0)\n\t\t\tthrow new IllegalArgumentException(\"Value out of range\");\n\t\tif (border > Integer.MAX_VALUE / 2 || qr.size + border * 2L > Integer.MAX_VALUE / scale)\n\t\t\tthrow new IllegalArgumentException(\"Scale or border too large\");\n\t\t\n\t\tBufferedImage result = new BufferedImage((qr.size + border * 2) * scale, (qr.size + border * 2) * scale, BufferedImage.TYPE_INT_RGB);\n\t\tfor (int y = 0; y < result.getHeight(); y++) {\n\t\t\tfor (int x = 0; x < result.getWidth(); x++) {\n\t\t\t\tboolean color = qr.getModule(x / scale - border, y / scale - border);\n\t\t\t\tresult.setRGB(x, y, color ? darkColor : lightColor);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Helper function to reduce code duplication.\n\tprivate static void writePng(BufferedImage img, String filepath) throws IOException {\n\t\tImageIO.write(img, \"png\", new File(filepath));\n\t}\n\t\n\t\n\t/**\n\t * Returns a string of SVG code for an image depicting the specified QR Code, with the specified\n\t * number of border modules. The string always uses Unix newlines (\\n), regardless of the platform.\n\t * @param qr the QR Code to render (not {@code null})\n\t * @param border the number of border modules to add, which must be non-negative\n\t * @param lightColor the color to use for light modules, in any format supported by CSS, not {@code null}\n\t * @param darkColor the color to use for dark modules, in any format supported by CSS, not {@code null}\n\t * @return a string representing the QR Code as an SVG XML document\n\t * @throws NullPointerException if any object is {@code null}\n\t * @throws IllegalArgumentException if the border is negative\n\t */\n\tprivate static String toSvgString(QrCode qr, int border, String lightColor, String darkColor) {\n\t\tObjects.requireNonNull(qr);\n\t\tObjects.requireNonNull(lightColor);\n\t\tObjects.requireNonNull(darkColor);\n\t\tif (border < 0)\n\t\t\tthrow new IllegalArgumentException(\"Border must be non-negative\");\n\t\tlong brd = border;\n\t\tStringBuilder sb = new StringBuilder()\n\t\t\t.append(\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\")\n\t\t\t.append(\"<!DOCTYPE svg PUBLIC \\\"-//W3C//DTD SVG 1.1//EN\\\" \\\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\\">\\n\")\n\t\t\t.append(String.format(\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" version=\\\"1.1\\\" viewBox=\\\"0 0 %1$d %1$d\\\" stroke=\\\"none\\\">\\n\",\n\t\t\t\tqr.size + brd * 2))\n\t\t\t.append(\"\\t<rect width=\\\"100%\\\" height=\\\"100%\\\" fill=\\\"\" + lightColor + \"\\\"/>\\n\")\n\t\t\t.append(\"\\t<path d=\\\"\");\n\t\tfor (int y = 0; y < qr.size; y++) {\n\t\t\tfor (int x = 0; x < qr.size; x++) {\n\t\t\t\tif (qr.getModule(x, y)) {\n\t\t\t\t\tif (x != 0 || y != 0)\n\t\t\t\t\t\tsb.append(\" \");\n\t\t\t\t\tsb.append(String.format(\"M%d,%dh1v1h-1z\", x + brd, y + brd));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn sb\n\t\t\t.append(\"\\\" fill=\\\"\" + darkColor + \"\\\"/>\\n\")\n\t\t\t.append(\"</svg>\\n\")\n\t\t\t.toString();\n\t}\n\t\n}\n"
  },
  {
    "path": "java/Readme.markdown",
    "content": "QR Code generator library - Java\n================================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nOptional advanced features:\n\n* Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes\n* Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```java\nimport java.awt.image.BufferedImage;\nimport java.io.File;\nimport java.util.List;\nimport javax.imageio.ImageIO;\nimport io.nayuki.qrcodegen.*;\n\n// Simple operation\nQrCode qr0 = QrCode.encodeText(\"Hello, world!\", QrCode.Ecc.MEDIUM);\nBufferedImage img = toImage(qr0, 4, 10);  // See QrCodeGeneratorDemo\nImageIO.write(img, \"png\", new File(\"qr-code.png\"));\n\n// Manual operation\nList<QrSegment> segs = QrSegment.makeSegments(\"3141592653589793238462643383\");\nQrCode qr1 = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);\nfor (int y = 0; y < qr1.size; y++) {\n    for (int x = 0; x < qr1.size; x++) {\n        (... paint qr1.getModule(x, y) ...)\n    }\n}\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/java/QrCodeGeneratorDemo.java .\n"
  },
  {
    "path": "java/pom.xml",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n\t<modelVersion>4.0.0</modelVersion>\n\t\n\t<groupId>io.nayuki</groupId>\n\t<artifactId>qrcodegen</artifactId>\n\t<version>1.8.0</version>\n\t<packaging>jar</packaging>\n\t<properties>\n\t\t<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n\t</properties>\n\t\n\t\n\t<build>\n\t\t<plugins>\n\t\t\t\n\t\t\t<plugin>\n\t\t\t\t<groupId>org.apache.maven.plugins</groupId>\n\t\t\t\t<artifactId>maven-compiler-plugin</artifactId>\n\t\t\t\t<version>3.8.1</version>\n\t\t\t\t<executions>\n\t\t\t\t\t<execution>\n\t\t\t\t\t\t<id>default-compile</id>\n\t\t\t\t\t\t<configuration>\n\t\t\t\t\t\t\t<release>9</release>\n\t\t\t\t\t\t</configuration>\n\t\t\t\t\t</execution>\n\t\t\t\t\t<execution>\n\t\t\t\t\t\t<id>java8-compile</id>\n\t\t\t\t\t\t<goals>\n\t\t\t\t\t\t\t<goal>compile</goal>\n\t\t\t\t\t\t</goals>\n\t\t\t\t\t\t<configuration>\n\t\t\t\t\t\t\t<source>1.8</source>\n\t\t\t\t\t\t\t<target>1.8</target>\n\t\t\t\t\t\t\t<excludes>\n\t\t\t\t\t\t\t\t<exclude>module-info.java</exclude>\n\t\t\t\t\t\t\t</excludes>\n\t\t\t\t\t\t</configuration>\n\t\t\t\t\t</execution>\n\t\t\t\t</executions>\n\t\t\t</plugin>\n\t\t\t\n\t\t\t<plugin>\n\t\t\t\t<groupId>org.apache.maven.plugins</groupId>\n\t\t\t\t<artifactId>maven-release-plugin</artifactId>\n\t\t\t\t<configuration>\n\t\t\t\t\t<checkModificationExcludes>\n\t\t\t\t\t\t<checkModificationExclude>java/pom.xml</checkModificationExclude>\n\t\t\t\t\t</checkModificationExcludes>\n\t\t\t\t</configuration>\n\t\t\t</plugin>\n\t\t\t\n\t\t\t<plugin>\n\t\t\t\t<groupId>org.apache.maven.plugins</groupId>\n\t\t\t\t<artifactId>maven-source-plugin</artifactId>\n\t\t\t\t<version>2.2.1</version>\n\t\t\t\t<executions>\n\t\t\t\t\t<execution>\n\t\t\t\t\t\t<id>attach-sources</id>\n\t\t\t\t\t\t<goals>\n\t\t\t\t\t\t\t<goal>jar-no-fork</goal>\n\t\t\t\t\t\t</goals>\n\t\t\t\t\t</execution>\n\t\t\t\t</executions>\n\t\t\t</plugin>\n\t\t\t\n\t\t\t<plugin>\n\t\t\t\t<groupId>org.apache.maven.plugins</groupId>\n\t\t\t\t<artifactId>maven-javadoc-plugin</artifactId>\n\t\t\t\t<version>3.1.1</version>\n\t\t\t\t<executions>\n\t\t\t\t\t<execution>\n\t\t\t\t\t\t<id>attach-javadocs</id>\n\t\t\t\t\t\t<goals>\n\t\t\t\t\t\t\t<goal>jar</goal>\n\t\t\t\t\t\t</goals>\n\t\t\t\t\t</execution>\n\t\t\t\t</executions>\n\t\t\t</plugin>\n\t\t\t\n\t\t\t<plugin>\n\t\t\t\t<groupId>org.apache.maven.plugins</groupId>\n\t\t\t\t<artifactId>maven-gpg-plugin</artifactId>\n\t\t\t\t<version>1.5</version>\n\t\t\t\t<executions>\n\t\t\t\t\t<execution>\n\t\t\t\t\t\t<id>sign-artifacts</id>\n\t\t\t\t\t\t<phase>verify</phase>\n\t\t\t\t\t\t<goals>\n\t\t\t\t\t\t\t<goal>sign</goal>\n\t\t\t\t\t\t</goals>\n\t\t\t\t\t</execution>\n\t\t\t\t</executions>\n\t\t\t</plugin>\n\t\t\t\n\t\t</plugins>\n\t</build>\n\t\n\t\n\t<name>QR Code generator library</name>\n\t<description>High quality QR Code generator library</description>\n\t<url>https://www.nayuki.io/page/qr-code-generator-library</url>\n\t<inceptionYear>2016</inceptionYear>\n\t<licenses>\n\t\t<license>\n\t\t\t<name>The MIT License</name>\n\t\t\t<url>https://opensource.org/licenses/MIT</url>\n\t\t\t<distribution>repo</distribution>\n\t\t</license>\n\t</licenses>\n\t<developers>\n\t\t<developer>\n\t\t\t<name>Project Nayuki</name>\n\t\t\t<email>me@nayuki.io</email>\n\t\t\t<url>https://www.nayuki.io/</url>\n\t\t</developer>\n\t</developers>\n\t\n\t<scm>\n\t\t<connection>scm:git:git://github.com/nayuki/QR-Code-generator.git</connection>\n\t\t<developerConnection>scm:git:ssh://github.com:nayuki/QR-Code-generator.git</developerConnection>\n\t\t<url>https://github.com/nayuki/QR-Code-generator/tree/master/java</url>\n\t</scm>\n\t<distributionManagement>\n\t\t<snapshotRepository>\n\t\t\t<id>ossrh</id>\n\t\t\t<url>https://oss.sonatype.org/content/repositories/snapshots</url>\n\t\t</snapshotRepository>\n\t\t<repository>\n\t\t\t<id>ossrh</id>\n\t\t\t<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>\n\t\t</repository>\n\t</distributionManagement>\n</project>\n"
  },
  {
    "path": "java/src/main/java/io/nayuki/qrcodegen/BitBuffer.java",
    "content": "/* \n * QR Code generator library (Java)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.qrcodegen;\n\nimport java.util.BitSet;\nimport java.util.Objects;\n\n\n/**\n * An appendable sequence of bits (0s and 1s). Mainly used by {@link QrSegment}.\n */\npublic final class BitBuffer implements Cloneable {\n\t\n\t/*---- Fields ----*/\n\t\n\tprivate BitSet data;\n\t\n\tprivate int bitLength;  // Non-negative\n\t\n\t\n\t\n\t/*---- Constructor ----*/\n\t\n\t/**\n\t * Constructs an empty bit buffer (length 0).\n\t */\n\tpublic BitBuffer() {\n\t\tdata = new BitSet();\n\t\tbitLength = 0;\n\t}\n\t\n\t\n\t\n\t/*---- Methods ----*/\n\t\n\t/**\n\t * Returns the length of this sequence, which is a non-negative value.\n\t * @return the length of this sequence\n\t */\n\tpublic int bitLength() {\n\t\tassert bitLength >= 0;\n\t\treturn bitLength;\n\t}\n\t\n\t\n\t/**\n\t * Returns the bit at the specified index, yielding 0 or 1.\n\t * @param index the index to get the bit at\n\t * @return the bit at the specified index\n\t * @throws IndexOutOfBoundsException if index &lt; 0 or index &#x2265; bitLength\n\t */\n\tpublic int getBit(int index) {\n\t\tif (index < 0 || index >= bitLength)\n\t\t\tthrow new IndexOutOfBoundsException();\n\t\treturn data.get(index) ? 1 : 0;\n\t}\n\t\n\t\n\t/**\n\t * Appends the specified number of low-order bits of the specified value to this\n\t * buffer. Requires 0 &#x2264; len &#x2264; 31 and 0 &#x2264; val &lt; 2<sup>len</sup>.\n\t * @param val the value to append\n\t * @param len the number of low-order bits in the value to take\n\t * @throws IllegalArgumentException if the value or number of bits is out of range\n\t * @throws IllegalStateException if appending the data\n\t * would make bitLength exceed Integer.MAX_VALUE\n\t */\n\tpublic void appendBits(int val, int len) {\n\t\tif (len < 0 || len > 31 || val >>> len != 0)\n\t\t\tthrow new IllegalArgumentException(\"Value out of range\");\n\t\tif (Integer.MAX_VALUE - bitLength < len)\n\t\t\tthrow new IllegalStateException(\"Maximum length reached\");\n\t\tfor (int i = len - 1; i >= 0; i--, bitLength++)  // Append bit by bit\n\t\t\tdata.set(bitLength, QrCode.getBit(val, i));\n\t}\n\t\n\t\n\t/**\n\t * Appends the content of the specified bit buffer to this buffer.\n\t * @param bb the bit buffer whose data to append (not {@code null})\n\t * @throws NullPointerException if the bit buffer is {@code null}\n\t * @throws IllegalStateException if appending the data\n\t * would make bitLength exceed Integer.MAX_VALUE\n\t */\n\tpublic void appendData(BitBuffer bb) {\n\t\tObjects.requireNonNull(bb);\n\t\tif (Integer.MAX_VALUE - bitLength < bb.bitLength)\n\t\t\tthrow new IllegalStateException(\"Maximum length reached\");\n\t\tfor (int i = 0; i < bb.bitLength; i++, bitLength++)  // Append bit by bit\n\t\t\tdata.set(bitLength, bb.data.get(i));\n\t}\n\t\n\t\n\t/**\n\t * Returns a new copy of this buffer.\n\t * @return a new copy of this buffer (not {@code null})\n\t */\n\tpublic BitBuffer clone() {\n\t\ttry {\n\t\t\tBitBuffer result = (BitBuffer)super.clone();\n\t\t\tresult.data = (BitSet)result.data.clone();\n\t\t\treturn result;\n\t\t} catch (CloneNotSupportedException e) {\n\t\t\tthrow new AssertionError(e);\n\t\t}\n\t}\n\t\n}\n"
  },
  {
    "path": "java/src/main/java/io/nayuki/qrcodegen/DataTooLongException.java",
    "content": "/* \n * QR Code generator library (Java)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.qrcodegen;\n\n\n/**\n * Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:\n * <ul>\n *   <li><p>Decrease the error correction level if it was greater than {@code Ecc.LOW}.</p></li>\n *   <li><p>If the advanced {@code encodeSegments()} function with 6 arguments or the\n *     {@code makeSegmentsOptimally()} function was called, then increase the maxVersion argument\n *     if it was less than {@link QrCode#MAX_VERSION}. (This advice does not apply to the other\n *     factory functions because they search all versions up to {@code QrCode.MAX_VERSION}.)</p></li>\n *   <li><p>Split the text data into better or optimal segments in order to reduce the number of\n *     bits required. (See {@link QrSegmentAdvanced#makeSegmentsOptimally(CharSequence,QrCode.Ecc,int,int)\n *     QrSegmentAdvanced.makeSegmentsOptimally()}.)</p></li>\n *   <li><p>Change the text or binary data to be shorter.</p></li>\n *   <li><p>Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).</p></li>\n *   <li><p>Propagate the error upward to the caller/user.</p></li>\n * </ul>\n * @see QrCode#encodeText(CharSequence, QrCode.Ecc)\n * @see QrCode#encodeBinary(byte[], QrCode.Ecc)\n * @see QrCode#encodeSegments(java.util.List, QrCode.Ecc)\n * @see QrCode#encodeSegments(java.util.List, QrCode.Ecc, int, int, int, boolean)\n * @see QrSegmentAdvanced#makeSegmentsOptimally(CharSequence, QrCode.Ecc, int, int)\n */\npublic class DataTooLongException extends IllegalArgumentException {\n\t\n\tpublic DataTooLongException() {}\n\t\n\t\n\tpublic DataTooLongException(String msg) {\n\t\tsuper(msg);\n\t}\n\t\n}\n"
  },
  {
    "path": "java/src/main/java/io/nayuki/qrcodegen/QrCode.java",
    "content": "/* \n * QR Code generator library (Java)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.qrcodegen;\n\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Objects;\n\n\n/**\n * A QR Code symbol, which is a type of two-dimension barcode.\n * Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n * <p>Instances of this class represent an immutable square grid of dark and light cells.\n * The class provides static factory functions to create a QR Code from text or binary data.\n * The class covers the QR Code Model 2 specification, supporting all versions (sizes)\n * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.</p>\n * <p>Ways to create a QR Code object:</p>\n * <ul>\n *   <li><p>High level: Take the payload data and call {@link QrCode#encodeText(CharSequence,Ecc)}\n *     or {@link QrCode#encodeBinary(byte[],Ecc)}.</p></li>\n *   <li><p>Mid level: Custom-make the list of {@link QrSegment segments}\n *     and call {@link QrCode#encodeSegments(List,Ecc)} or\n *     {@link QrCode#encodeSegments(List,Ecc,int,int,int,boolean)}</p></li>\n *   <li><p>Low level: Custom-make the array of data codeword bytes (including segment headers and\n *     final padding, excluding error correction codewords), supply the appropriate version number,\n *     and call the {@link QrCode#QrCode(int,Ecc,byte[],int) constructor}.</p></li>\n * </ul>\n * <p>(Note that all ways require supplying the desired error correction level.)</p>\n * @see QrSegment\n */\npublic final class QrCode {\n\t\n\t/*---- Static factory functions (high level) ----*/\n\t\n\t/**\n\t * Returns a QR Code representing the specified Unicode text string at the specified error correction level.\n\t * As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer\n\t * Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible\n\t * QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the\n\t * ecl argument if it can be done without increasing the version.\n\t * @param text the text to be encoded (not {@code null}), which can be any Unicode string\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @return a QR Code (not {@code null}) representing the text\n\t * @throws NullPointerException if the text or error correction level is {@code null}\n\t * @throws DataTooLongException if the text fails to fit in the\n\t * largest version QR Code at the ECL, which means it is too long\n\t */\n\tpublic static QrCode encodeText(CharSequence text, Ecc ecl) {\n\t\tObjects.requireNonNull(text);\n\t\tObjects.requireNonNull(ecl);\n\t\tList<QrSegment> segs = QrSegment.makeSegments(text);\n\t\treturn encodeSegments(segs, ecl);\n\t}\n\t\n\t\n\t/**\n\t * Returns a QR Code representing the specified binary data at the specified error correction level.\n\t * This function always encodes using the binary segment mode, not any text mode. The maximum number of\n\t * bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.\n\t * The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t * @param data the binary data to encode (not {@code null})\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @return a QR Code (not {@code null}) representing the data\n\t * @throws NullPointerException if the data or error correction level is {@code null}\n\t * @throws DataTooLongException if the data fails to fit in the\n\t * largest version QR Code at the ECL, which means it is too long\n\t */\n\tpublic static QrCode encodeBinary(byte[] data, Ecc ecl) {\n\t\tObjects.requireNonNull(data);\n\t\tObjects.requireNonNull(ecl);\n\t\tQrSegment seg = QrSegment.makeBytes(data);\n\t\treturn encodeSegments(Arrays.asList(seg), ecl);\n\t}\n\t\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/**\n\t * Returns a QR Code representing the specified segments at the specified error correction\n\t * level. The smallest possible QR Code version is automatically chosen for the output. The ECC level\n\t * of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t * <p>This function allows the user to create a custom sequence of segments that switches\n\t * between modes (such as alphanumeric and byte) to encode text in less space.\n\t * This is a mid-level API; the high-level API is {@link #encodeText(CharSequence,Ecc)}\n\t * and {@link #encodeBinary(byte[],Ecc)}.</p>\n\t * @param segs the segments to encode\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @return a QR Code (not {@code null}) representing the segments\n\t * @throws NullPointerException if the list of segments, any segment, or the error correction level is {@code null}\n\t * @throws DataTooLongException if the segments fail to fit in the\n\t * largest version QR Code at the ECL, which means they are too long\n\t */\n\tpublic static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl) {\n\t\treturn encodeSegments(segs, ecl, MIN_VERSION, MAX_VERSION, -1, true);\n\t}\n\t\n\t\n\t/**\n\t * Returns a QR Code representing the specified segments with the specified encoding parameters.\n\t * The smallest possible QR Code version within the specified range is automatically\n\t * chosen for the output. Iff boostEcl is {@code true}, then the ECC level of the\n\t * result may be higher than the ecl argument if it can be done without increasing\n\t * the version. The mask number is either between 0 to 7 (inclusive) to force that\n\t * mask, or &#x2212;1 to automatically choose an appropriate mask (which may be slow).\n\t * <p>This function allows the user to create a custom sequence of segments that switches\n\t * between modes (such as alphanumeric and byte) to encode text in less space.\n\t * This is a mid-level API; the high-level API is {@link #encodeText(CharSequence,Ecc)}\n\t * and {@link #encodeBinary(byte[],Ecc)}.</p>\n\t * @param segs the segments to encode\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @param minVersion the minimum allowed version of the QR Code (at least 1)\n\t * @param maxVersion the maximum allowed version of the QR Code (at most 40)\n\t * @param mask the mask number to use (between 0 and 7 (inclusive)), or &#x2212;1 for automatic mask\n\t * @param boostEcl increases the ECC level as long as it doesn't increase the version number\n\t * @return a QR Code (not {@code null}) representing the segments\n\t * @throws NullPointerException if the list of segments, any segment, or the error correction level is {@code null}\n\t * @throws IllegalArgumentException if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40\n\t * or &#x2212;1 &#x2264; mask &#x2264; 7 is violated\n\t * @throws DataTooLongException if the segments fail to fit in\n\t * the maxVersion QR Code at the ECL, which means they are too long\n\t */\n\tpublic static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl, int minVersion, int maxVersion, int mask, boolean boostEcl) {\n\t\tObjects.requireNonNull(segs);\n\t\tObjects.requireNonNull(ecl);\n\t\tif (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)\n\t\t\tthrow new IllegalArgumentException(\"Invalid value\");\n\t\t\n\t\t// Find the minimal version number to use\n\t\tint version, dataUsedBits;\n\t\tfor (version = minVersion; ; version++) {\n\t\t\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\t\tdataUsedBits = QrSegment.getTotalBits(segs, version);\n\t\t\tif (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)\n\t\t\t\tbreak;  // This version number is found to be suitable\n\t\t\tif (version >= maxVersion) {  // All versions in the range could not fit the given data\n\t\t\t\tString msg = \"Segment too long\";\n\t\t\t\tif (dataUsedBits != -1)\n\t\t\t\t\tmsg = String.format(\"Data length = %d bits, Max capacity = %d bits\", dataUsedBits, dataCapacityBits);\n\t\t\t\tthrow new DataTooLongException(msg);\n\t\t\t}\n\t\t}\n\t\tassert dataUsedBits != -1;\n\t\t\n\t\t// Increase the error correction level while the data still fits in the current version number\n\t\tfor (Ecc newEcl : Ecc.values()) {  // From low to high\n\t\t\tif (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)\n\t\t\t\tecl = newEcl;\n\t\t}\n\t\t\n\t\t// Concatenate all segments to create the data bit string\n\t\tBitBuffer bb = new BitBuffer();\n\t\tfor (QrSegment seg : segs) {\n\t\t\tbb.appendBits(seg.mode.modeBits, 4);\n\t\t\tbb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));\n\t\t\tbb.appendData(seg.data);\n\t\t}\n\t\tassert bb.bitLength() == dataUsedBits;\n\t\t\n\t\t// Add terminator and pad up to a byte if applicable\n\t\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;\n\t\tassert bb.bitLength() <= dataCapacityBits;\n\t\tbb.appendBits(0, Math.min(4, dataCapacityBits - bb.bitLength()));\n\t\tbb.appendBits(0, (8 - bb.bitLength() % 8) % 8);\n\t\tassert bb.bitLength() % 8 == 0;\n\t\t\n\t\t// Pad with alternating bytes until data capacity is reached\n\t\tfor (int padByte = 0xEC; bb.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)\n\t\t\tbb.appendBits(padByte, 8);\n\t\t\n\t\t// Pack bits into bytes in big endian\n\t\tbyte[] dataCodewords = new byte[bb.bitLength() / 8];\n\t\tfor (int i = 0; i < bb.bitLength(); i++)\n\t\t\tdataCodewords[i >>> 3] |= bb.getBit(i) << (7 - (i & 7));\n\t\t\n\t\t// Create the QR Code object\n\t\treturn new QrCode(version, ecl, dataCodewords, mask);\n\t}\n\t\n\t\n\t\n\t/*---- Instance fields ----*/\n\t\n\t// Public immutable scalar parameters:\n\t\n\t/** The version number of this QR Code, which is between 1 and 40 (inclusive).\n\t * This determines the size of this barcode. */\n\tpublic final int version;\n\t\n\t/** The width and height of this QR Code, measured in modules, between\n\t * 21 and 177 (inclusive). This is equal to version &#xD7; 4 + 17. */\n\tpublic final int size;\n\t\n\t/** The error correction level used in this QR Code, which is not {@code null}. */\n\tpublic final Ecc errorCorrectionLevel;\n\t\n\t/** The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).\n\t * <p>Even if a QR Code is created with automatic masking requested (mask =\n\t * &#x2212;1), the resulting object still has a mask value between 0 and 7. */\n\tpublic final int mask;\n\t\n\t// Private grids of modules/pixels, with dimensions of size*size:\n\t\n\t// The modules of this QR Code (false = light, true = dark).\n\t// Immutable after constructor finishes. Accessed through getModule().\n\tprivate boolean[][] modules;\n\t\n\t// Indicates function modules that are not subjected to masking. Discarded when constructor finishes.\n\tprivate boolean[][] isFunction;\n\t\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/**\n\t * Constructs a QR Code with the specified version number,\n\t * error correction level, data codeword bytes, and mask number.\n\t * <p>This is a low-level API that most users should not use directly. A mid-level\n\t * API is the {@link #encodeSegments(List,Ecc,int,int,int,boolean)} function.</p>\n\t * @param ver the version number to use, which must be in the range 1 to 40 (inclusive)\n\t * @param ecl the error correction level to use\n\t * @param dataCodewords the bytes representing segments to encode (without ECC)\n\t * @param msk the mask pattern to use, which is either &#x2212;1 for automatic choice or from 0 to 7 for fixed choice\n\t * @throws NullPointerException if the byte array or error correction level is {@code null}\n\t * @throws IllegalArgumentException if the version or mask value is out of range,\n\t * or if the data is the wrong length for the specified version and error correction level\n\t */\n\tpublic QrCode(int ver, Ecc ecl, byte[] dataCodewords, int msk) {\n\t\t// Check arguments and initialize fields\n\t\tif (ver < MIN_VERSION || ver > MAX_VERSION)\n\t\t\tthrow new IllegalArgumentException(\"Version value out of range\");\n\t\tif (msk < -1 || msk > 7)\n\t\t\tthrow new IllegalArgumentException(\"Mask value out of range\");\n\t\tversion = ver;\n\t\tsize = ver * 4 + 17;\n\t\terrorCorrectionLevel = Objects.requireNonNull(ecl);\n\t\tObjects.requireNonNull(dataCodewords);\n\t\tmodules    = new boolean[size][size];  // Initially all light\n\t\tisFunction = new boolean[size][size];\n\t\t\n\t\t// Compute ECC, draw modules, do masking\n\t\tdrawFunctionPatterns();\n\t\tbyte[] allCodewords = addEccAndInterleave(dataCodewords);\n\t\tdrawCodewords(allCodewords);\n\t\t\n\t\t// Do masking\n\t\tif (msk == -1) {  // Automatically choose best mask\n\t\t\tint minPenalty = Integer.MAX_VALUE;\n\t\t\tfor (int i = 0; i < 8; i++) {\n\t\t\t\tapplyMask(i);\n\t\t\t\tdrawFormatBits(i);\n\t\t\t\tint penalty = getPenaltyScore();\n\t\t\t\tif (penalty < minPenalty) {\n\t\t\t\t\tmsk = i;\n\t\t\t\t\tminPenalty = penalty;\n\t\t\t\t}\n\t\t\t\tapplyMask(i);  // Undoes the mask due to XOR\n\t\t\t}\n\t\t}\n\t\tassert 0 <= msk && msk <= 7;\n\t\tmask = msk;\n\t\tapplyMask(msk);  // Apply the final choice of mask\n\t\tdrawFormatBits(msk);  // Overwrite old format bits\n\t\t\n\t\tisFunction = null;\n\t}\n\t\n\t\n\t\n\t/*---- Public instance methods ----*/\n\t\n\t/**\n\t * Returns the color of the module (pixel) at the specified coordinates, which is {@code false}\n\t * for light or {@code true} for dark. The top left corner has the coordinates (x=0, y=0).\n\t * If the specified coordinates are out of bounds, then {@code false} (light) is returned.\n\t * @param x the x coordinate, where 0 is the left edge and size&#x2212;1 is the right edge\n\t * @param y the y coordinate, where 0 is the top edge and size&#x2212;1 is the bottom edge\n\t * @return {@code true} if the coordinates are in bounds and the module\n\t * at that location is dark, or {@code false} (light) otherwise\n\t */\n\tpublic boolean getModule(int x, int y) {\n\t\treturn 0 <= x && x < size && 0 <= y && y < size && modules[y][x];\n\t}\n\t\n\t\n\t\n\t/*---- Private helper methods for constructor: Drawing function modules ----*/\n\t\n\t// Reads this object's version field, and draws and marks all function modules.\n\tprivate void drawFunctionPatterns() {\n\t\t// Draw horizontal and vertical timing patterns\n\t\tfor (int i = 0; i < size; i++) {\n\t\t\tsetFunctionModule(6, i, i % 2 == 0);\n\t\t\tsetFunctionModule(i, 6, i % 2 == 0);\n\t\t}\n\t\t\n\t\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\t\tdrawFinderPattern(3, 3);\n\t\tdrawFinderPattern(size - 4, 3);\n\t\tdrawFinderPattern(3, size - 4);\n\t\t\n\t\t// Draw numerous alignment patterns\n\t\tint[] alignPatPos = getAlignmentPatternPositions();\n\t\tint numAlign = alignPatPos.length;\n\t\tfor (int i = 0; i < numAlign; i++) {\n\t\t\tfor (int j = 0; j < numAlign; j++) {\n\t\t\t\t// Don't draw on the three finder corners\n\t\t\t\tif (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))\n\t\t\t\t\tdrawAlignmentPattern(alignPatPos[i], alignPatPos[j]);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Draw configuration data\n\t\tdrawFormatBits(0);  // Dummy mask value; overwritten later in the constructor\n\t\tdrawVersion();\n\t}\n\t\n\t\n\t// Draws two copies of the format bits (with its own error correction code)\n\t// based on the given mask and this object's error correction level field.\n\tprivate void drawFormatBits(int msk) {\n\t\t// Calculate error correction code and pack bits\n\t\tint data = errorCorrectionLevel.formatBits << 3 | msk;  // errCorrLvl is uint2, mask is uint3\n\t\tint rem = data;\n\t\tfor (int i = 0; i < 10; i++)\n\t\t\trem = (rem << 1) ^ ((rem >>> 9) * 0x537);\n\t\tint bits = (data << 10 | rem) ^ 0x5412;  // uint15\n\t\tassert bits >>> 15 == 0;\n\t\t\n\t\t// Draw first copy\n\t\tfor (int i = 0; i <= 5; i++)\n\t\t\tsetFunctionModule(8, i, getBit(bits, i));\n\t\tsetFunctionModule(8, 7, getBit(bits, 6));\n\t\tsetFunctionModule(8, 8, getBit(bits, 7));\n\t\tsetFunctionModule(7, 8, getBit(bits, 8));\n\t\tfor (int i = 9; i < 15; i++)\n\t\t\tsetFunctionModule(14 - i, 8, getBit(bits, i));\n\t\t\n\t\t// Draw second copy\n\t\tfor (int i = 0; i < 8; i++)\n\t\t\tsetFunctionModule(size - 1 - i, 8, getBit(bits, i));\n\t\tfor (int i = 8; i < 15; i++)\n\t\t\tsetFunctionModule(8, size - 15 + i, getBit(bits, i));\n\t\tsetFunctionModule(8, size - 8, true);  // Always dark\n\t}\n\t\n\t\n\t// Draws two copies of the version bits (with its own error correction code),\n\t// based on this object's version field, iff 7 <= version <= 40.\n\tprivate void drawVersion() {\n\t\tif (version < 7)\n\t\t\treturn;\n\t\t\n\t\t// Calculate error correction code and pack bits\n\t\tint rem = version;  // version is uint6, in the range [7, 40]\n\t\tfor (int i = 0; i < 12; i++)\n\t\t\trem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);\n\t\tint bits = version << 12 | rem;  // uint18\n\t\tassert bits >>> 18 == 0;\n\t\t\n\t\t// Draw two copies\n\t\tfor (int i = 0; i < 18; i++) {\n\t\t\tboolean bit = getBit(bits, i);\n\t\t\tint a = size - 11 + i % 3;\n\t\t\tint b = i / 3;\n\t\t\tsetFunctionModule(a, b, bit);\n\t\t\tsetFunctionModule(b, a, bit);\n\t\t}\n\t}\n\t\n\t\n\t// Draws a 9*9 finder pattern including the border separator,\n\t// with the center module at (x, y). Modules can be out of bounds.\n\tprivate void drawFinderPattern(int x, int y) {\n\t\tfor (int dy = -4; dy <= 4; dy++) {\n\t\t\tfor (int dx = -4; dx <= 4; dx++) {\n\t\t\t\tint dist = Math.max(Math.abs(dx), Math.abs(dy));  // Chebyshev/infinity norm\n\t\t\t\tint xx = x + dx, yy = y + dy;\n\t\t\t\tif (0 <= xx && xx < size && 0 <= yy && yy < size)\n\t\t\t\t\tsetFunctionModule(xx, yy, dist != 2 && dist != 4);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Draws a 5*5 alignment pattern, with the center module\n\t// at (x, y). All modules must be in bounds.\n\tprivate void drawAlignmentPattern(int x, int y) {\n\t\tfor (int dy = -2; dy <= 2; dy++) {\n\t\t\tfor (int dx = -2; dx <= 2; dx++)\n\t\t\t\tsetFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);\n\t\t}\n\t}\n\t\n\t\n\t// Sets the color of a module and marks it as a function module.\n\t// Only used by the constructor. Coordinates must be in bounds.\n\tprivate void setFunctionModule(int x, int y, boolean isDark) {\n\t\tmodules[y][x] = isDark;\n\t\tisFunction[y][x] = true;\n\t}\n\t\n\t\n\t/*---- Private helper methods for constructor: Codewords and masking ----*/\n\t\n\t// Returns a new byte string representing the given data with the appropriate error correction\n\t// codewords appended to it, based on this object's version and error correction level.\n\tprivate byte[] addEccAndInterleave(byte[] data) {\n\t\tObjects.requireNonNull(data);\n\t\tif (data.length != getNumDataCodewords(version, errorCorrectionLevel))\n\t\t\tthrow new IllegalArgumentException();\n\t\t\n\t\t// Calculate parameter numbers\n\t\tint numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal()][version];\n\t\tint blockEccLen = ECC_CODEWORDS_PER_BLOCK  [errorCorrectionLevel.ordinal()][version];\n\t\tint rawCodewords = getNumRawDataModules(version) / 8;\n\t\tint numShortBlocks = numBlocks - rawCodewords % numBlocks;\n\t\tint shortBlockLen = rawCodewords / numBlocks;\n\t\t\n\t\t// Split data into blocks and append ECC to each block\n\t\tbyte[][] blocks = new byte[numBlocks][];\n\t\tbyte[] rsDiv = reedSolomonComputeDivisor(blockEccLen);\n\t\tfor (int i = 0, k = 0; i < numBlocks; i++) {\n\t\t\tbyte[] dat = Arrays.copyOfRange(data, k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));\n\t\t\tk += dat.length;\n\t\t\tbyte[] block = Arrays.copyOf(dat, shortBlockLen + 1);\n\t\t\tbyte[] ecc = reedSolomonComputeRemainder(dat, rsDiv);\n\t\t\tSystem.arraycopy(ecc, 0, block, block.length - blockEccLen, ecc.length);\n\t\t\tblocks[i] = block;\n\t\t}\n\t\t\n\t\t// Interleave (not concatenate) the bytes from every block into a single sequence\n\t\tbyte[] result = new byte[rawCodewords];\n\t\tfor (int i = 0, k = 0; i < blocks[0].length; i++) {\n\t\t\tfor (int j = 0; j < blocks.length; j++) {\n\t\t\t\t// Skip the padding byte in short blocks\n\t\t\t\tif (i != shortBlockLen - blockEccLen || j >= numShortBlocks) {\n\t\t\t\t\tresult[k] = blocks[j][i];\n\t\t\t\t\tk++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire\n\t// data area of this QR Code. Function modules need to be marked off before this is called.\n\tprivate void drawCodewords(byte[] data) {\n\t\tObjects.requireNonNull(data);\n\t\tif (data.length != getNumRawDataModules(version) / 8)\n\t\t\tthrow new IllegalArgumentException();\n\t\t\n\t\tint i = 0;  // Bit index into the data\n\t\t// Do the funny zigzag scan\n\t\tfor (int right = size - 1; right >= 1; right -= 2) {  // Index of right column in each column pair\n\t\t\tif (right == 6)\n\t\t\t\tright = 5;\n\t\t\tfor (int vert = 0; vert < size; vert++) {  // Vertical counter\n\t\t\t\tfor (int j = 0; j < 2; j++) {\n\t\t\t\t\tint x = right - j;  // Actual x coordinate\n\t\t\t\t\tboolean upward = ((right + 1) & 2) == 0;\n\t\t\t\t\tint y = upward ? size - 1 - vert : vert;  // Actual y coordinate\n\t\t\t\t\tif (!isFunction[y][x] && i < data.length * 8) {\n\t\t\t\t\t\tmodules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));\n\t\t\t\t\t\ti++;\n\t\t\t\t\t}\n\t\t\t\t\t// If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t\t// 0/false/light by the constructor and are left unchanged by this method\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tassert i == data.length * 8;\n\t}\n\t\n\t\n\t// XORs the codeword modules in this QR Code with the given mask pattern.\n\t// The function modules must be marked and the codeword bits must be drawn\n\t// before masking. Due to the arithmetic of XOR, calling applyMask() with\n\t// the same mask value a second time will undo the mask. A final well-formed\n\t// QR Code needs exactly one (not zero, two, etc.) mask applied.\n\tprivate void applyMask(int msk) {\n\t\tif (msk < 0 || msk > 7)\n\t\t\tthrow new IllegalArgumentException(\"Mask value out of range\");\n\t\tfor (int y = 0; y < size; y++) {\n\t\t\tfor (int x = 0; x < size; x++) {\n\t\t\t\tboolean invert;\n\t\t\t\tswitch (msk) {\n\t\t\t\t\tcase 0:  invert = (x + y) % 2 == 0;                    break;\n\t\t\t\t\tcase 1:  invert = y % 2 == 0;                          break;\n\t\t\t\t\tcase 2:  invert = x % 3 == 0;                          break;\n\t\t\t\t\tcase 3:  invert = (x + y) % 3 == 0;                    break;\n\t\t\t\t\tcase 4:  invert = (x / 3 + y / 2) % 2 == 0;            break;\n\t\t\t\t\tcase 5:  invert = x * y % 2 + x * y % 3 == 0;          break;\n\t\t\t\t\tcase 6:  invert = (x * y % 2 + x * y % 3) % 2 == 0;    break;\n\t\t\t\t\tcase 7:  invert = ((x + y) % 2 + x * y % 3) % 2 == 0;  break;\n\t\t\t\t\tdefault:  throw new AssertionError();\n\t\t\t\t}\n\t\t\t\tmodules[y][x] ^= invert & !isFunction[y][x];\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\n\tprivate int getPenaltyScore() {\n\t\tint result = 0;\n\t\t\n\t\t// Adjacent modules in row having same color, and finder-like patterns\n\t\tfor (int y = 0; y < size; y++) {\n\t\t\tboolean runColor = false;\n\t\t\tint runX = 0;\n\t\t\tint[] runHistory = new int[7];\n\t\t\tfor (int x = 0; x < size; x++) {\n\t\t\t\tif (modules[y][x] == runColor) {\n\t\t\t\t\trunX++;\n\t\t\t\t\tif (runX == 5)\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\telse if (runX > 5)\n\t\t\t\t\t\tresult++;\n\t\t\t\t} else {\n\t\t\t\t\tfinderPenaltyAddHistory(runX, runHistory);\n\t\t\t\t\tif (!runColor)\n\t\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;\n\t\t\t\t\trunColor = modules[y][x];\n\t\t\t\t\trunX = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;\n\t\t}\n\t\t// Adjacent modules in column having same color, and finder-like patterns\n\t\tfor (int x = 0; x < size; x++) {\n\t\t\tboolean runColor = false;\n\t\t\tint runY = 0;\n\t\t\tint[] runHistory = new int[7];\n\t\t\tfor (int y = 0; y < size; y++) {\n\t\t\t\tif (modules[y][x] == runColor) {\n\t\t\t\t\trunY++;\n\t\t\t\t\tif (runY == 5)\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\telse if (runY > 5)\n\t\t\t\t\t\tresult++;\n\t\t\t\t} else {\n\t\t\t\t\tfinderPenaltyAddHistory(runY, runHistory);\n\t\t\t\t\tif (!runColor)\n\t\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;\n\t\t\t\t\trunColor = modules[y][x];\n\t\t\t\t\trunY = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;\n\t\t}\n\t\t\n\t\t// 2*2 blocks of modules having same color\n\t\tfor (int y = 0; y < size - 1; y++) {\n\t\t\tfor (int x = 0; x < size - 1; x++) {\n\t\t\t\tboolean color = modules[y][x];\n\t\t\t\tif (  color == modules[y][x + 1] &&\n\t\t\t\t      color == modules[y + 1][x] &&\n\t\t\t\t      color == modules[y + 1][x + 1])\n\t\t\t\t\tresult += PENALTY_N2;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Balance of dark and light modules\n\t\tint dark = 0;\n\t\tfor (boolean[] row : modules) {\n\t\t\tfor (boolean color : row) {\n\t\t\t\tif (color)\n\t\t\t\t\tdark++;\n\t\t\t}\n\t\t}\n\t\tint total = size * size;  // Note that size is odd, so dark/total != 1/2\n\t\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\t\tint k = (Math.abs(dark * 20 - total * 10) + total - 1) / total - 1;\n\t\tassert 0 <= k && k <= 9;\n\t\tresult += k * PENALTY_N4;\n\t\tassert 0 <= result && result <= 2568888;  // Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\t\treturn result;\n\t}\n\t\n\t\n\t\n\t/*---- Private helper functions ----*/\n\t\n\t// Returns an ascending list of positions of alignment patterns for this version number.\n\t// Each position is in the range [0,177), and are used on both the x and y axes.\n\t// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.\n\tprivate int[] getAlignmentPatternPositions() {\n\t\tif (version == 1)\n\t\t\treturn new int[]{};\n\t\telse {\n\t\t\tint numAlign = version / 7 + 2;\n\t\t\tint step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;\n\t\t\tint[] result = new int[numAlign];\n\t\t\tresult[0] = 6;\n\t\t\tfor (int i = result.length - 1, pos = size - 7; i >= 1; i--, pos -= step)\n\t\t\t\tresult[i] = pos;\n\t\t\treturn result;\n\t\t}\n\t}\n\t\n\t\n\t// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\n\tprivate static int getNumRawDataModules(int ver) {\n\t\tif (ver < MIN_VERSION || ver > MAX_VERSION)\n\t\t\tthrow new IllegalArgumentException(\"Version number out of range\");\n\t\t\n\t\tint size = ver * 4 + 17;\n\t\tint result = size * size;   // Number of modules in the whole QR Code square\n\t\tresult -= 8 * 8 * 3;        // Subtract the three finders with separators\n\t\tresult -= 15 * 2 + 1;       // Subtract the format information and dark module\n\t\tresult -= (size - 16) * 2;  // Subtract the timing patterns (excluding finders)\n\t\t// The five lines above are equivalent to: int result = (16 * ver + 128) * ver + 64;\n\t\tif (ver >= 2) {\n\t\t\tint numAlign = ver / 7 + 2;\n\t\t\tresult -= (numAlign - 1) * (numAlign - 1) * 25;  // Subtract alignment patterns not overlapping with timing patterns\n\t\t\tresult -= (numAlign - 2) * 2 * 20;  // Subtract alignment patterns that overlap with timing patterns\n\t\t\t// The two lines above are equivalent to: result -= (25 * numAlign - 10) * numAlign - 55;\n\t\t\tif (ver >= 7)\n\t\t\t\tresult -= 6 * 3 * 2;  // Subtract version information\n\t\t}\n\t\tassert 208 <= result && result <= 29648;\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be\n\t// implemented as a lookup table over all possible parameter values, instead of as an algorithm.\n\tprivate static byte[] reedSolomonComputeDivisor(int degree) {\n\t\tif (degree < 1 || degree > 255)\n\t\t\tthrow new IllegalArgumentException(\"Degree out of range\");\n\t\t// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t\t// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.\n\t\tbyte[] result = new byte[degree];\n\t\tresult[degree - 1] = 1;  // Start off with the monomial x^0\n\t\t\n\t\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t\t// and drop the highest monomial term which is always 1x^degree.\n\t\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\t\tint root = 1;\n\t\tfor (int i = 0; i < degree; i++) {\n\t\t\t// Multiply the current product by (x - r^i)\n\t\t\tfor (int j = 0; j < result.length; j++) {\n\t\t\t\tresult[j] = (byte)reedSolomonMultiply(result[j] & 0xFF, root);\n\t\t\t\tif (j + 1 < result.length)\n\t\t\t\t\tresult[j] ^= result[j + 1];\n\t\t\t}\n\t\t\troot = reedSolomonMultiply(root, 0x02);\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.\n\tprivate static byte[] reedSolomonComputeRemainder(byte[] data, byte[] divisor) {\n\t\tObjects.requireNonNull(data);\n\t\tObjects.requireNonNull(divisor);\n\t\tbyte[] result = new byte[divisor.length];\n\t\tfor (byte b : data) {  // Polynomial division\n\t\t\tint factor = (b ^ result[0]) & 0xFF;\n\t\t\tSystem.arraycopy(result, 1, result, 0, result.length - 1);\n\t\t\tresult[result.length - 1] = 0;\n\t\t\tfor (int i = 0; i < result.length; i++)\n\t\t\t\tresult[i] ^= reedSolomonMultiply(divisor[i] & 0xFF, factor);\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result\n\t// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.\n\tprivate static int reedSolomonMultiply(int x, int y) {\n\t\tassert x >> 8 == 0 && y >> 8 == 0;\n\t\t// Russian peasant multiplication\n\t\tint z = 0;\n\t\tfor (int i = 7; i >= 0; i--) {\n\t\t\tz = (z << 1) ^ ((z >>> 7) * 0x11D);\n\t\t\tz ^= ((y >>> i) & 1) * x;\n\t\t}\n\t\tassert z >>> 8 == 0;\n\t\treturn z;\n\t}\n\t\n\t\n\t// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t// QR Code of the given version number and error correction level, with remainder bits discarded.\n\t// This stateless pure function could be implemented as a (40*4)-cell lookup table.\n\tstatic int getNumDataCodewords(int ver, Ecc ecl) {\n\t\treturn getNumRawDataModules(ver) / 8\n\t\t\t- ECC_CODEWORDS_PER_BLOCK    [ecl.ordinal()][ver]\n\t\t\t* NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver];\n\t}\n\t\n\t\n\t// Can only be called immediately after a light run is added, and\n\t// returns either 0, 1, or 2. A helper function for getPenaltyScore().\n\tprivate int finderPenaltyCountPatterns(int[] runHistory) {\n\t\tint n = runHistory[1];\n\t\tassert n <= size * 3;\n\t\tboolean core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;\n\t\treturn (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)\n\t\t     + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);\n\t}\n\t\n\t\n\t// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().\n\tprivate int finderPenaltyTerminateAndCount(boolean currentRunColor, int currentRunLength, int[] runHistory) {\n\t\tif (currentRunColor) {  // Terminate dark run\n\t\t\tfinderPenaltyAddHistory(currentRunLength, runHistory);\n\t\t\tcurrentRunLength = 0;\n\t\t}\n\t\tcurrentRunLength += size;  // Add light border to final run\n\t\tfinderPenaltyAddHistory(currentRunLength, runHistory);\n\t\treturn finderPenaltyCountPatterns(runHistory);\n\t}\n\t\n\t\n\t// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().\n\tprivate void finderPenaltyAddHistory(int currentRunLength, int[] runHistory) {\n\t\tif (runHistory[0] == 0)\n\t\t\tcurrentRunLength += size;  // Add light border to initial run\n\t\tSystem.arraycopy(runHistory, 0, runHistory, 1, runHistory.length - 1);\n\t\trunHistory[0] = currentRunLength;\n\t}\n\t\n\t\n\t// Returns true iff the i'th bit of x is set to 1.\n\tstatic boolean getBit(int x, int i) {\n\t\treturn ((x >>> i) & 1) != 0;\n\t}\n\t\n\t\n\t/*---- Constants and tables ----*/\n\t\n\t/** The minimum version number  (1) supported in the QR Code Model 2 standard. */\n\tpublic static final int MIN_VERSION =  1;\n\t\n\t/** The maximum version number (40) supported in the QR Code Model 2 standard. */\n\tpublic static final int MAX_VERSION = 40;\n\t\n\t\n\t// For use in getPenaltyScore(), when evaluating which mask is best.\n\tprivate static final int PENALTY_N1 =  3;\n\tprivate static final int PENALTY_N2 =  3;\n\tprivate static final int PENALTY_N3 = 40;\n\tprivate static final int PENALTY_N4 = 10;\n\t\n\t\n\tprivate static final byte[][] ECC_CODEWORDS_PER_BLOCK = {\n\t\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t//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\n\t\t{-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\n\t\t{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},  // Medium\n\t\t{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // Quartile\n\t\t{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // High\n\t};\n\t\n\tprivate static final byte[][] NUM_ERROR_CORRECTION_BLOCKS = {\n\t\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t//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\n\t\t{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25},  // Low\n\t\t{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49},  // Medium\n\t\t{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68},  // Quartile\n\t\t{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81},  // High\n\t};\n\t\n\t\n\t\n\t/*---- Public helper enumeration ----*/\n\t\n\t/**\n\t * The error correction level in a QR Code symbol.\n\t */\n\tpublic enum Ecc {\n\t\t// Must be declared in ascending order of error protection\n\t\t// so that the implicit ordinal() and values() work properly\n\t\t/** The QR Code can tolerate about  7% erroneous codewords. */ LOW(1),\n\t\t/** The QR Code can tolerate about 15% erroneous codewords. */ MEDIUM(0),\n\t\t/** The QR Code can tolerate about 25% erroneous codewords. */ QUARTILE(3),\n\t\t/** The QR Code can tolerate about 30% erroneous codewords. */ HIGH(2);\n\t\t\n\t\t// In the range 0 to 3 (unsigned 2-bit integer).\n\t\tfinal int formatBits;\n\t\t\n\t\t// Constructor.\n\t\tprivate Ecc(int fb) {\n\t\t\tformatBits = fb;\n\t\t}\n\t}\n\t\n}\n"
  },
  {
    "path": "java/src/main/java/io/nayuki/qrcodegen/QrSegment.java",
    "content": "/* \n * QR Code generator library (Java)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.qrcodegen;\n\nimport java.nio.charset.StandardCharsets;\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.Objects;\nimport java.util.regex.Pattern;\n\n\n/**\n * A segment of character/binary/control data in a QR Code symbol.\n * Instances of this class are immutable.\n * <p>The mid-level way to create a segment is to take the payload data and call a\n * static factory function such as {@link QrSegment#makeNumeric(CharSequence)}. The low-level\n * way to create a segment is to custom-make the bit buffer and call the {@link\n * QrSegment#QrSegment(Mode,int,BitBuffer) constructor} with appropriate values.</p>\n * <p>This segment class imposes no length restrictions, but QR Codes have restrictions.\n * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n * Any segment longer than this is meaningless for the purpose of generating QR Codes.\n * This class can represent kanji mode segments, but provides no help in encoding them\n * - see {@link QrSegmentAdvanced} for full kanji support.</p>\n */\npublic final class QrSegment {\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/**\n\t * Returns a segment representing the specified binary data\n\t * encoded in byte mode. All input byte arrays are acceptable.\n\t * <p>Any text string can be converted to UTF-8 bytes ({@code\n\t * s.getBytes(StandardCharsets.UTF_8)}) and encoded as a byte mode segment.</p>\n\t * @param data the binary data (not {@code null})\n\t * @return a segment (not {@code null}) containing the data\n\t * @throws NullPointerException if the array is {@code null}\n\t */\n\tpublic static QrSegment makeBytes(byte[] data) {\n\t\tObjects.requireNonNull(data);\n\t\tBitBuffer bb = new BitBuffer();\n\t\tfor (byte b : data)\n\t\t\tbb.appendBits(b & 0xFF, 8);\n\t\treturn new QrSegment(Mode.BYTE, data.length, bb);\n\t}\n\t\n\t\n\t/**\n\t * Returns a segment representing the specified string of decimal digits encoded in numeric mode.\n\t * @param digits the text (not {@code null}), with only digits from 0 to 9 allowed\n\t * @return a segment (not {@code null}) containing the text\n\t * @throws NullPointerException if the string is {@code null}\n\t * @throws IllegalArgumentException if the string contains non-digit characters\n\t */\n\tpublic static QrSegment makeNumeric(CharSequence digits) {\n\t\tObjects.requireNonNull(digits);\n\t\tif (!isNumeric(digits))\n\t\t\tthrow new IllegalArgumentException(\"String contains non-numeric characters\");\n\t\t\n\t\tBitBuffer bb = new BitBuffer();\n\t\tfor (int i = 0; i < digits.length(); ) {  // Consume up to 3 digits per iteration\n\t\t\tint n = Math.min(digits.length() - i, 3);\n\t\t\tbb.appendBits(Integer.parseInt(digits.subSequence(i, i + n).toString()), n * 3 + 1);\n\t\t\ti += n;\n\t\t}\n\t\treturn new QrSegment(Mode.NUMERIC, digits.length(), bb);\n\t}\n\t\n\t\n\t/**\n\t * Returns a segment representing the specified text string encoded in alphanumeric mode.\n\t * The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t * dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t * @param text the text (not {@code null}), with only certain characters allowed\n\t * @return a segment (not {@code null}) containing the text\n\t * @throws NullPointerException if the string is {@code null}\n\t * @throws IllegalArgumentException if the string contains non-encodable characters\n\t */\n\tpublic static QrSegment makeAlphanumeric(CharSequence text) {\n\t\tObjects.requireNonNull(text);\n\t\tif (!isAlphanumeric(text))\n\t\t\tthrow new IllegalArgumentException(\"String contains unencodable characters in alphanumeric mode\");\n\t\t\n\t\tBitBuffer bb = new BitBuffer();\n\t\tint i;\n\t\tfor (i = 0; i <= text.length() - 2; i += 2) {  // Process groups of 2\n\t\t\tint temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;\n\t\t\ttemp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));\n\t\t\tbb.appendBits(temp, 11);\n\t\t}\n\t\tif (i < text.length())  // 1 character remaining\n\t\t\tbb.appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6);\n\t\treturn new QrSegment(Mode.ALPHANUMERIC, text.length(), bb);\n\t}\n\t\n\t\n\t/**\n\t * Returns a list of zero or more segments to represent the specified Unicode text string.\n\t * The result may use various segment modes and switch modes to optimize the length of the bit stream.\n\t * @param text the text to be encoded, which can be any Unicode string\n\t * @return a new mutable list (not {@code null}) of segments (not {@code null}) containing the text\n\t * @throws NullPointerException if the text is {@code null}\n\t */\n\tpublic static List<QrSegment> makeSegments(CharSequence text) {\n\t\tObjects.requireNonNull(text);\n\t\t\n\t\t// Select the most efficient segment encoding automatically\n\t\tList<QrSegment> result = new ArrayList<>();\n\t\tif (text.equals(\"\"));  // Leave result empty\n\t\telse if (isNumeric(text))\n\t\t\tresult.add(makeNumeric(text));\n\t\telse if (isAlphanumeric(text))\n\t\t\tresult.add(makeAlphanumeric(text));\n\t\telse\n\t\t\tresult.add(makeBytes(text.toString().getBytes(StandardCharsets.UTF_8)));\n\t\treturn result;\n\t}\n\t\n\t\n\t/**\n\t * Returns a segment representing an Extended Channel Interpretation\n\t * (ECI) designator with the specified assignment value.\n\t * @param assignVal the ECI assignment number (see the AIM ECI specification)\n\t * @return a segment (not {@code null}) containing the data\n\t * @throws IllegalArgumentException if the value is outside the range [0, 10<sup>6</sup>)\n\t */\n\tpublic static QrSegment makeEci(int assignVal) {\n\t\tBitBuffer bb = new BitBuffer();\n\t\tif (assignVal < 0)\n\t\t\tthrow new IllegalArgumentException(\"ECI assignment value out of range\");\n\t\telse if (assignVal < (1 << 7))\n\t\t\tbb.appendBits(assignVal, 8);\n\t\telse if (assignVal < (1 << 14)) {\n\t\t\tbb.appendBits(0b10, 2);\n\t\t\tbb.appendBits(assignVal, 14);\n\t\t} else if (assignVal < 1_000_000) {\n\t\t\tbb.appendBits(0b110, 3);\n\t\t\tbb.appendBits(assignVal, 21);\n\t\t} else\n\t\t\tthrow new IllegalArgumentException(\"ECI assignment value out of range\");\n\t\treturn new QrSegment(Mode.ECI, 0, bb);\n\t}\n\t\n\t\n\t/**\n\t * Tests whether the specified string can be encoded as a segment in numeric mode.\n\t * A string is encodable iff each character is in the range 0 to 9.\n\t * @param text the string to test for encodability (not {@code null})\n\t * @return {@code true} iff each character is in the range 0 to 9.\n\t * @throws NullPointerException if the string is {@code null}\n\t * @see #makeNumeric(CharSequence)\n\t */\n\tpublic static boolean isNumeric(CharSequence text) {\n\t\treturn NUMERIC_REGEX.matcher(text).matches();\n\t}\n\t\n\t\n\t/**\n\t * Tests whether the specified string can be encoded as a segment in alphanumeric mode.\n\t * A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t * @param text the string to test for encodability (not {@code null})\n\t * @return {@code true} iff each character is in the alphanumeric mode character set\n\t * @throws NullPointerException if the string is {@code null}\n\t * @see #makeAlphanumeric(CharSequence)\n\t */\n\tpublic static boolean isAlphanumeric(CharSequence text) {\n\t\treturn ALPHANUMERIC_REGEX.matcher(text).matches();\n\t}\n\t\n\t\n\t\n\t/*---- Instance fields ----*/\n\t\n\t/** The mode indicator of this segment. Not {@code null}. */\n\tpublic final Mode mode;\n\t\n\t/** The length of this segment's unencoded data. Measured in characters for\n\t * numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t * Always zero or positive. Not the same as the data's bit length. */\n\tpublic final int numChars;\n\t\n\t// The data bits of this segment. Not null. Accessed through getData().\n\tfinal BitBuffer data;\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/**\n\t * Constructs a QR Code segment with the specified attributes and data.\n\t * The character count (numCh) must agree with the mode and the bit buffer length,\n\t * but the constraint isn't checked. The specified bit buffer is cloned and stored.\n\t * @param md the mode (not {@code null})\n\t * @param numCh the data length in characters or bytes, which is non-negative\n\t * @param data the data bits (not {@code null})\n\t * @throws NullPointerException if the mode or data is {@code null}\n\t * @throws IllegalArgumentException if the character count is negative\n\t */\n\tpublic QrSegment(Mode md, int numCh, BitBuffer data) {\n\t\tmode = Objects.requireNonNull(md);\n\t\tObjects.requireNonNull(data);\n\t\tif (numCh < 0)\n\t\t\tthrow new IllegalArgumentException(\"Invalid value\");\n\t\tnumChars = numCh;\n\t\tthis.data = data.clone();  // Make defensive copy\n\t}\n\t\n\t\n\t/*---- Methods ----*/\n\t\n\t/**\n\t * Returns the data bits of this segment.\n\t * @return a new copy of the data bits (not {@code null})\n\t */\n\tpublic BitBuffer getData() {\n\t\treturn data.clone();  // Make defensive copy\n\t}\n\t\n\t\n\t// Calculates the number of bits needed to encode the given segments at the given version.\n\t// Returns a non-negative number if successful. Otherwise returns -1 if a segment has too\n\t// many characters to fit its length field, or the total bits exceeds Integer.MAX_VALUE.\n\tstatic int getTotalBits(List<QrSegment> segs, int version) {\n\t\tObjects.requireNonNull(segs);\n\t\tlong result = 0;\n\t\tfor (QrSegment seg : segs) {\n\t\t\tObjects.requireNonNull(seg);\n\t\t\tint ccbits = seg.mode.numCharCountBits(version);\n\t\t\tif (seg.numChars >= (1 << ccbits))\n\t\t\t\treturn -1;  // The segment's length doesn't fit the field's bit width\n\t\t\tresult += 4L + ccbits + seg.data.bitLength();\n\t\t\tif (result > Integer.MAX_VALUE)\n\t\t\t\treturn -1;  // The sum will overflow an int type\n\t\t}\n\t\treturn (int)result;\n\t}\n\t\n\t\n\t/*---- Constants ----*/\n\t\n\t// Describes precisely all strings that are encodable in numeric mode.\n\tprivate static final Pattern NUMERIC_REGEX = Pattern.compile(\"[0-9]*\");\n\t\n\t// Describes precisely all strings that are encodable in alphanumeric mode.\n\tprivate static final Pattern ALPHANUMERIC_REGEX = Pattern.compile(\"[A-Z0-9 $%*+./:-]*\");\n\t\n\t// The set of all legal characters in alphanumeric mode, where\n\t// each character value maps to the index in the string.\n\tstatic final String ALPHANUMERIC_CHARSET = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\t\n\t\n\t\n\t/*---- Public helper enumeration ----*/\n\t\n\t/**\n\t * Describes how a segment's data bits are interpreted.\n\t */\n\tpublic enum Mode {\n\t\t\n\t\t/*-- Constants --*/\n\t\t\n\t\tNUMERIC     (0x1, 10, 12, 14),\n\t\tALPHANUMERIC(0x2,  9, 11, 13),\n\t\tBYTE        (0x4,  8, 16, 16),\n\t\tKANJI       (0x8,  8, 10, 12),\n\t\tECI         (0x7,  0,  0,  0);\n\t\t\n\t\t\n\t\t/*-- Fields --*/\n\t\t\n\t\t// The mode indicator bits, which is a uint4 value (range 0 to 15).\n\t\tfinal int modeBits;\n\t\t\n\t\t// Number of character count bits for three different version ranges.\n\t\tprivate final int[] numBitsCharCount;\n\t\t\n\t\t\n\t\t/*-- Constructor --*/\n\t\t\n\t\tprivate Mode(int mode, int... ccbits) {\n\t\t\tmodeBits = mode;\n\t\t\tnumBitsCharCount = ccbits;\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Method --*/\n\t\t\n\t\t// Returns the bit width of the character count field for a segment in this mode\n\t\t// in a QR Code at the given version number. The result is in the range [0, 16].\n\t\tint numCharCountBits(int ver) {\n\t\t\tassert QrCode.MIN_VERSION <= ver && ver <= QrCode.MAX_VERSION;\n\t\t\treturn numBitsCharCount[(ver + 7) / 17];\n\t\t}\n\t\t\n\t}\n\t\n}\n"
  },
  {
    "path": "java/src/main/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java",
    "content": "/* \n * QR Code generator library - Optional advanced logic (Java)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.qrcodegen;\n\nimport java.nio.charset.StandardCharsets;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.Base64;\nimport java.util.List;\nimport java.util.Objects;\nimport io.nayuki.qrcodegen.QrSegment.Mode;\n\n\n/**\n * Splits text into optimal segments and encodes kanji segments.\n * Provides static functions only; not instantiable.\n * @see QrSegment\n * @see QrCode\n */\npublic final class QrSegmentAdvanced {\n\t\n\t/*---- Optimal list of segments encoder ----*/\n\t\n\t/**\n\t * Returns a list of zero or more segments to represent the specified Unicode text string.\n\t * The resulting list optimally minimizes the total encoded bit length, subjected to the constraints\n\t * in the specified {error correction level, minimum version number, maximum version number}.\n\t * <p>This function can utilize all four text encoding modes: numeric, alphanumeric, byte (UTF-8),\n\t * and kanji. This can be considered as a sophisticated but slower replacement for {@link\n\t * QrSegment#makeSegments(CharSequence)}. This requires more input parameters because it searches a\n\t * range of versions, like {@link QrCode#encodeSegments(List,QrCode.Ecc,int,int,int,boolean)}.</p>\n\t * @param text the text to be encoded (not {@code null}), which can be any Unicode string\n\t * @param ecl the error correction level to use (not {@code null})\n\t * @param minVersion the minimum allowed version of the QR Code (at least 1)\n\t * @param maxVersion the maximum allowed version of the QR Code (at most 40)\n\t * @return a new mutable list (not {@code null}) of segments (not {@code null})\n\t * containing the text, minimizing the bit length with respect to the constraints\n\t * @throws NullPointerException if the text or error correction level is {@code null}\n\t * @throws IllegalArgumentException if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40 is violated\n\t * @throws DataTooLongException if the text fails to fit in the maxVersion QR Code at the ECL\n\t */\n\tpublic static List<QrSegment> makeSegmentsOptimally(CharSequence text, QrCode.Ecc ecl, int minVersion, int maxVersion) {\n\t\t// Check arguments\n\t\tObjects.requireNonNull(text);\n\t\tObjects.requireNonNull(ecl);\n\t\tif (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION))\n\t\t\tthrow new IllegalArgumentException(\"Invalid value\");\n\t\t\n\t\t// Iterate through version numbers, and make tentative segments\n\t\tList<QrSegment> segs = null;\n\t\tint[] codePoints = toCodePoints(text);\n\t\tfor (int version = minVersion; ; version++) {\n\t\t\tif (version == minVersion || version == 10 || version == 27)\n\t\t\t\tsegs = makeSegmentsOptimally(codePoints, version);\n\t\t\tassert segs != null;\n\t\t\t\n\t\t\t// Check if the segments fit\n\t\t\tint dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\t\tint dataUsedBits = QrSegment.getTotalBits(segs, version);\n\t\t\tif (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)\n\t\t\t\treturn segs;  // This version number is found to be suitable\n\t\t\tif (version >= maxVersion) {  // All versions in the range could not fit the given text\n\t\t\t\tString msg = \"Segment too long\";\n\t\t\t\tif (dataUsedBits != -1)\n\t\t\t\t\tmsg = String.format(\"Data length = %d bits, Max capacity = %d bits\", dataUsedBits, dataCapacityBits);\n\t\t\t\tthrow new DataTooLongException(msg);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Returns a new list of segments that is optimal for the given text at the given version number.\n\tprivate static List<QrSegment> makeSegmentsOptimally(int[] codePoints, int version) {\n\t\tif (codePoints.length == 0)\n\t\t\treturn new ArrayList<>();\n\t\tMode[] charModes = computeCharacterModes(codePoints, version);\n\t\treturn splitIntoSegments(codePoints, charModes);\n\t}\n\t\n\t\n\t// Returns a new array representing the optimal mode per code point based on the given text and version.\n\tprivate static Mode[] computeCharacterModes(int[] codePoints, int version) {\n\t\tif (codePoints.length == 0)\n\t\t\tthrow new IllegalArgumentException();\n\t\tif (codePoints.length > 7089)  // Upper bound is the number of characters that fit in QR Code version 40, low error correction, numeric mode\n\t\t\tthrow new DataTooLongException(\"String too long\");\n\t\tfinal Mode[] modeTypes = {Mode.BYTE, Mode.ALPHANUMERIC, Mode.NUMERIC, Mode.KANJI};  // Do not modify\n\t\tfinal int numModes = modeTypes.length;\n\t\t\n\t\t// Segment header sizes, measured in 1/6 bits\n\t\tfinal int[] headCosts = new int[numModes];\n\t\tfor (int i = 0; i < numModes; i++) {\n\t\t\theadCosts[i] = (4 + modeTypes[i].numCharCountBits(version)) * 6;\n\t\t\tassert 0 <= headCosts[i] && headCosts[i] <= (4 + 16) * 6;\n\t\t}\n\t\t\n\t\t// charModes[i][j] represents the mode to encode the code point at\n\t\t// index i such that the final segment ends in modeTypes[j] and the\n\t\t// total number of bits is minimized over all possible choices\n\t\tMode[][] charModes = new Mode[codePoints.length][numModes];\n\t\t\n\t\t// At the beginning of each iteration of the loop below,\n\t\t// prevCosts[j] is the exact minimum number of 1/6 bits needed to\n\t\t// encode the entire string prefix of length i, and end in modeTypes[j]\n\t\tint[] prevCosts = headCosts.clone();\n\t\t\n\t\t// Calculate costs using dynamic programming\n\t\tfor (int i = 0; i < codePoints.length; i++) {\n\t\t\tint c = codePoints[i];\n\t\t\tint[] curCosts = new int[numModes];\n\t\t\t{  // Always extend a byte mode segment\n\t\t\t\tcurCosts[0] = prevCosts[0] + countUtf8Bytes(c) * 8 * 6;\n\t\t\t\tcharModes[i][0] = modeTypes[0];\n\t\t\t}\n\t\t\t// Extend a segment if possible\n\t\t\tif (QrSegment.ALPHANUMERIC_CHARSET.indexOf(c) != -1) {  // Is alphanumeric\n\t\t\t\tcurCosts[1] = prevCosts[1] + 33;  // 5.5 bits per alphanumeric char\n\t\t\t\tcharModes[i][1] = modeTypes[1];\n\t\t\t}\n\t\t\tif ('0' <= c && c <= '9') {  // Is numeric\n\t\t\t\tcurCosts[2] = prevCosts[2] + 20;  // 3.33 bits per digit\n\t\t\t\tcharModes[i][2] = modeTypes[2];\n\t\t\t}\n\t\t\tif (isKanji(c)) {\n\t\t\t\tcurCosts[3] = prevCosts[3] + 78;  // 13 bits per Shift JIS char\n\t\t\t\tcharModes[i][3] = modeTypes[3];\n\t\t\t}\n\t\t\t\n\t\t\t// Start new segment at the end to switch modes\n\t\t\tfor (int j = 0; j < numModes; j++) {  // To mode\n\t\t\t\tfor (int k = 0; k < numModes; k++) {  // From mode\n\t\t\t\t\tint newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j];\n\t\t\t\t\tif (charModes[i][k] != null && (charModes[i][j] == null || newCost < curCosts[j])) {\n\t\t\t\t\t\tcurCosts[j] = newCost;\n\t\t\t\t\t\tcharModes[i][j] = modeTypes[k];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// A non-tight upper bound is when each of 7089 characters switches to\n\t\t\t// byte mode (4-bit header + 16-bit count) and requires 4 bytes in UTF-8\n\t\t\tfor (int cost : curCosts)\n\t\t\t\tassert 0 <= cost && cost <= (4 + 16 + 32) * 6 * 7089;\n\t\t\tprevCosts = curCosts;\n\t\t}\n\t\t\n\t\t// Find optimal ending mode\n\t\tMode curMode = null;\n\t\tfor (int i = 0, minCost = 0; i < numModes; i++) {\n\t\t\tif (curMode == null || prevCosts[i] < minCost) {\n\t\t\t\tminCost = prevCosts[i];\n\t\t\t\tcurMode = modeTypes[i];\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Get optimal mode for each code point by tracing backwards\n\t\tMode[] result = new Mode[charModes.length];\n\t\tfor (int i = result.length - 1; i >= 0; i--) {\n\t\t\tfor (int j = 0; j < numModes; j++) {\n\t\t\t\tif (modeTypes[j] == curMode) {\n\t\t\t\t\tcurMode = charModes[i][j];\n\t\t\t\t\tresult[i] = curMode;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns a new list of segments based on the given text and modes, such that\n\t// consecutive code points in the same mode are put into the same segment.\n\tprivate static List<QrSegment> splitIntoSegments(int[] codePoints, Mode[] charModes) {\n\t\tif (codePoints.length == 0)\n\t\t\tthrow new IllegalArgumentException();\n\t\tList<QrSegment> result = new ArrayList<>();\n\t\t\n\t\t// Accumulate run of modes\n\t\tMode curMode = charModes[0];\n\t\tint start = 0;\n\t\tfor (int i = 1; ; i++) {\n\t\t\tif (i < codePoints.length && charModes[i] == curMode)\n\t\t\t\tcontinue;\n\t\t\tString s = new String(codePoints, start, i - start);\n\t\t\tif (curMode == Mode.BYTE)\n\t\t\t\tresult.add(QrSegment.makeBytes(s.getBytes(StandardCharsets.UTF_8)));\n\t\t\telse if (curMode == Mode.NUMERIC)\n\t\t\t\tresult.add(QrSegment.makeNumeric(s));\n\t\t\telse if (curMode == Mode.ALPHANUMERIC)\n\t\t\t\tresult.add(QrSegment.makeAlphanumeric(s));\n\t\t\telse if (curMode == Mode.KANJI)\n\t\t\t\tresult.add(makeKanji(s));\n\t\t\telse\n\t\t\t\tthrow new AssertionError();\n\t\t\tif (i >= codePoints.length)\n\t\t\t\treturn result;\n\t\t\tcurMode = charModes[i];\n\t\t\tstart = i;\n\t\t}\n\t}\n\t\n\t\n\t// Returns a new array of Unicode code points (effectively\n\t// UTF-32 / UCS-4) representing the given UTF-16 string.\n\tprivate static int[] toCodePoints(CharSequence s) {\n\t\tint[] result = s.codePoints().toArray();\n\t\tfor (int c : result) {\n\t\t\tif (Character.isSurrogate((char)c))\n\t\t\t\tthrow new IllegalArgumentException(\"Invalid UTF-16 string\");\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns the number of UTF-8 bytes needed to encode the given Unicode code point.\n\tprivate static int countUtf8Bytes(int cp) {\n\t\tif      (cp <        0) throw new IllegalArgumentException(\"Invalid code point\");\n\t\telse if (cp <     0x80) return 1;\n\t\telse if (cp <    0x800) return 2;\n\t\telse if (cp <  0x10000) return 3;\n\t\telse if (cp < 0x110000) return 4;\n\t\telse                    throw new IllegalArgumentException(\"Invalid code point\");\n\t}\n\t\n\t\n\t\n\t/*---- Kanji mode segment encoder ----*/\n\t\n\t/**\n\t * Returns a segment representing the specified text string encoded in kanji mode.\n\t * Broadly speaking, the set of encodable characters are {kanji used in Japan,\n\t * hiragana, katakana, East Asian punctuation, full-width ASCII, Greek, Cyrillic}.\n\t * Examples of non-encodable characters include {ordinary ASCII, half-width katakana,\n\t * more extensive Chinese hanzi}.\n\t * @param text the text (not {@code null}), with only certain characters allowed\n\t * @return a segment (not {@code null}) containing the text\n\t * @throws NullPointerException if the string is {@code null}\n\t * @throws IllegalArgumentException if the string contains non-encodable characters\n\t * @see #isEncodableAsKanji(CharSequence)\n\t */\n\tpublic static QrSegment makeKanji(CharSequence text) {\n\t\tObjects.requireNonNull(text);\n\t\tBitBuffer bb = new BitBuffer();\n\t\ttext.chars().forEachOrdered(c -> {\n\t\t\tint val = UNICODE_TO_QR_KANJI[c];\n\t\t\tif (val == -1)\n\t\t\t\tthrow new IllegalArgumentException(\"String contains non-kanji-mode characters\");\n\t\t\tbb.appendBits(val, 13);\n\t\t});\n\t\treturn new QrSegment(Mode.KANJI, text.length(), bb);\n\t}\n\t\n\t\n\t/**\n\t * Tests whether the specified string can be encoded as a segment in kanji mode.\n\t * Broadly speaking, the set of encodable characters are {kanji used in Japan,\n\t * hiragana, katakana, East Asian punctuation, full-width ASCII, Greek, Cyrillic}.\n\t * Examples of non-encodable characters include {ordinary ASCII, half-width katakana,\n\t * more extensive Chinese hanzi}.\n\t * @param text the string to test for encodability (not {@code null})\n\t * @return {@code true} iff each character is in the kanji mode character set\n\t * @throws NullPointerException if the string is {@code null}\n\t * @see #makeKanji(CharSequence)\n\t */\n\tpublic static boolean isEncodableAsKanji(CharSequence text) {\n\t\tObjects.requireNonNull(text);\n\t\treturn text.chars().allMatch(\n\t\t\tc -> isKanji((char)c));\n\t}\n\t\n\t\n\tprivate static boolean isKanji(int c) {\n\t\treturn c < UNICODE_TO_QR_KANJI.length && UNICODE_TO_QR_KANJI[c] != -1;\n\t}\n\t\n\t\n\t// Data derived from ftp://ftp.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT\n\tprivate static final String PACKED_QR_KANJI_TO_UNICODE =\n\t\t\"MAAwATAC/wz/DjD7/xr/G/8f/wEwmzCcALT/QACo/z7/4/8/MP0w/jCdMJ4wA07dMAUwBjAHMPwgFSAQ/w8AXDAcIBb/XCAmICUgGCAZIBwgHf8I/wkwFDAV/zv/Pf9b/10wCDAJMAowCzAMMA0wDjAPMBAwEf8LIhIAsQDX//8A9/8dImD/HP8eImYiZyIeIjQmQiZA\" +\n\t\t\"ALAgMiAzIQP/5f8EAKIAo/8F/wP/Bv8K/yAApyYGJgUlyyXPJc4lxyXGJaEloCWzJbIlvSW8IDswEiGSIZAhkSGTMBP/////////////////////////////IggiCyKGIocigiKDIioiKf////////////////////8iJyIoAKwh0iHUIgAiA///////////////////\" +\n\t\t\"//////////8iICKlIxIiAiIHImEiUiJqImsiGiI9Ih0iNSIrIiz//////////////////yErIDAmbyZtJmogICAhALb//////////yXv/////////////////////////////////////////////////xD/Ef8S/xP/FP8V/xb/F/8Y/xn///////////////////8h\" +\n\t\t\"/yL/I/8k/yX/Jv8n/yj/Kf8q/yv/LP8t/y7/L/8w/zH/Mv8z/zT/Nf82/zf/OP85/zr///////////////////9B/0L/Q/9E/0X/Rv9H/0j/Sf9K/0v/TP9N/07/T/9Q/1H/Uv9T/1T/Vf9W/1f/WP9Z/1r//////////zBBMEIwQzBEMEUwRjBHMEgwSTBKMEswTDBN\" +\n\t\t\"ME4wTzBQMFEwUjBTMFQwVTBWMFcwWDBZMFowWzBcMF0wXjBfMGAwYTBiMGMwZDBlMGYwZzBoMGkwajBrMGwwbTBuMG8wcDBxMHIwczB0MHUwdjB3MHgweTB6MHswfDB9MH4wfzCAMIEwgjCDMIQwhTCGMIcwiDCJMIowizCMMI0wjjCPMJAwkTCSMJP/////////////\" +\n\t\t\"////////////////////////MKEwojCjMKQwpTCmMKcwqDCpMKowqzCsMK0wrjCvMLAwsTCyMLMwtDC1MLYwtzC4MLkwujC7MLwwvTC+ML8wwDDBMMIwwzDEMMUwxjDHMMgwyTDKMMswzDDNMM4wzzDQMNEw0jDTMNQw1TDWMNcw2DDZMNow2zDcMN0w3jDf//8w4DDh\" +\n\t\t\"MOIw4zDkMOUw5jDnMOgw6TDqMOsw7DDtMO4w7zDwMPEw8jDzMPQw9TD2/////////////////////wORA5IDkwOUA5UDlgOXA5gDmQOaA5sDnAOdA54DnwOgA6EDowOkA6UDpgOnA6gDqf////////////////////8DsQOyA7MDtAO1A7YDtwO4A7kDugO7A7wDvQO+\" +\n\t\t\"A78DwAPBA8MDxAPFA8YDxwPIA8n/////////////////////////////////////////////////////////////////////////////////////////////////////////////BBAEEQQSBBMEFAQVBAEEFgQXBBgEGQQaBBsEHAQdBB4EHwQgBCEEIgQjBCQEJQQm\" +\n\t\t\"BCcEKAQpBCoEKwQsBC0ELgQv////////////////////////////////////////BDAEMQQyBDMENAQ1BFEENgQ3BDgEOQQ6BDsEPAQ9//8EPgQ/BEAEQQRCBEMERARFBEYERwRIBEkESgRLBEwETQROBE///////////////////////////////////yUAJQIlDCUQ\" +\n\t\t\"JRglFCUcJSwlJCU0JTwlASUDJQ8lEyUbJRclIyUzJSslOyVLJSAlLyUoJTclPyUdJTAlJSU4JUL/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"/////////////////////////////////////06cVRZaA5Y/VMBhG2MoWfaQIoR1gxx6UGCqY+FuJWXthGaCppv1aJNXJ2WhYnFbm1nQhnuY9H1ifb6bjmIWfJ+It1uJXrVjCWaXaEiVx5eNZ09O5U8KT01PnVBJVvJZN1nUWgFcCWDfYQ9hcGYTaQVwunVPdXB5+32t\" +\n\t\t\"fe+Aw4QOiGOLApBVkHpTO06VTqVX34CykMF4704AWPFuopA4ejKDKIKLnC9RQVNwVL1U4VbgWftfFZjybeuA5IUt////////lmKWcJagl/tUC1PzW4dwz3+9j8KW6FNvnVx6uk4ReJOB/G4mVhhVBGsdhRqcO1nlU6ltZnTclY9WQk6RkEuW8oNPmQxT4VW2WzBfcWYg\" +\n\t\t\"ZvNoBGw4bPNtKXRbdsh6Tpg0gvGIW4pgku1tsnWrdsqZxWCmiwGNipWyaY5TrVGG//9XElgwWURbtF72YChjqWP0bL9vFHCOcRRxWXHVcz9+AYJ2gtGFl5BgkludG1hpZbxsWnUlUflZLlllX4Bf3GK8ZfpqKmsna7Rzi3/BiVadLJ0OnsRcoWyWg3tRBFxLYbaBxmh2\" +\n\t\t\"cmFOWU/6U3hgaW4pek+X804LUxZO7k9VTz1PoU9zUqBT71YJWQ9awVu2W+F50WaHZ5xntmtMbLNwa3PCeY15vno8e4eCsYLbgwSDd4Pvg9OHZoqyVimMqI/mkE6XHoaKT8Rc6GIRcll1O4Hlgr2G/ozAlsWZE5nVTstPGonjVt5YSljKXvtf62AqYJRgYmHQYhJi0GU5\" +\n\t\t\"////////m0FmZmiwbXdwcHVMdoZ9dYKlh/mVi5aOjJ1R8VK+WRZUs1uzXRZhaGmCba94jYTLiFeKcpOnmrhtbJmohtlXo2f/hs6SDlKDVodUBF7TYuFkuWg8aDhru3NyeLp6a4maidKNa48DkO2Vo5aUl2lbZlyzaX2YTZhOY5t7IGor//9qf2i2nA1vX1JyVZ1gcGLs\" +\n\t\t\"bTtuB27RhFuJEI9EThScOVP2aRtqOpeEaCpRXHrDhLKR3JOMVludKGgigwWEMXylUgiCxXTmTn5Pg1GgW9JSClLYUudd+1WaWCpZ5luMW5hb215yXnlgo2EfYWNhvmPbZWJn0WhTaPprPmtTbFdvIm+Xb0V0sHUYduN3C3r/e6F8IX3pfzZ/8ICdgmaDnomzisyMq5CE\" +\n\t\t\"lFGVk5WRlaKWZZfTmSiCGE44VCtcuF3Mc6l2THc8XKl/640LlsGYEZhUmFhPAU8OU3FVnFZoV/pZR1sJW8RckF4MXn5fzGPuZzpl12XiZx9oy2jE////////al9eMGvFbBdsfXV/eUhbY3oAfQBfvYmPihiMtI13jsyPHZjimg6bPE6AUH1RAFmTW5xiL2KAZOxrOnKg\" +\n\t\t\"dZF5R3+ph/uKvItwY6yDypegVAlUA1WraFRqWIpweCdndZ7NU3RbooEahlCQBk4YTkVOx08RU8pUOFuuXxNgJWVR//9nPWxCbHJs43B4dAN6dnquewh9Gnz+fWZl53JbU7tcRV3oYtJi4GMZbiCGWooxjd2S+G8BeaabWk6oTqtOrE+bT6BQ0VFHevZRcVH2U1RTIVN/\" +\n\t\t\"U+tVrFiDXOFfN19KYC9gUGBtYx9lWWpLbMFywnLtd++A+IEFggiFTpD3k+GX/5lXmlpO8FHdXC1mgWltXEBm8ml1c4loUHyBUMVS5FdHXf6TJmWkayNrPXQ0eYF5vXtLfcqCuYPMiH+JX4s5j9GR0VQfkoBOXVA2U+VTOnLXc5Z36YLmjq+ZxpnImdJRd2Eahl5VsHp6\" +\n\t\t\"UHZb05BHloVOMmrbkedcUVxI////////Y5h6n2yTl3SPYXqqcYqWiHyCaBd+cGhRk2xS8lQbhauKE3+kjs2Q4VNmiIh5QU/CUL5SEVFEVVNXLXPqV4tZUV9iX4RgdWF2YWdhqWOyZDplbGZvaEJuE3Vmej18+31MfZl+S39rgw6DSobNigiKY4tmjv2YGp2PgriPzpvo\" +\n\t\t\"//9Sh2IfZINvwJaZaEFQkWsgbHpvVHp0fVCIQIojZwhO9lA5UCZQZVF8UjhSY1WnVw9YBVrMXvphsmH4YvNjcmkcailyfXKscy54FHhvfXl3DICpiYuLGYzijtKQY5N1lnqYVZoTnnhRQ1OfU7Nee18mbhtukHOEc/59Q4I3igCK+pZQTk5QC1PkVHxW+lnRW2Rd8V6r\" +\n\t\t\"XydiOGVFZ69uVnLQfMqItIChgOGD8IZOioeN6JI3lseYZ58TTpROkk8NU0hUSVQ+Wi9fjF+hYJ9op2qOdFp4gYqeiqSLd5GQTl6byU6kT3xPr1AZUBZRSVFsUp9SuVL+U5pT41QR////////VA5ViVdRV6JZfVtUW11bj13lXedd9154XoNeml63XxhgUmFMYpdi2GOn\" +\n\t\t\"ZTtmAmZDZvRnbWghaJdpy2xfbSptaW4vbp11MnaHeGx6P3zgfQV9GH1efbGAFYADgK+AsYFUgY+CKoNSiEyIYYsbjKKM/JDKkXWScXg/kvyVpJZN//+YBZmZmtidO1JbUqtT91QIWNVi92/gjGqPX565UUtSO1RKVv16QJF3nWCe0nNEbwmBcHURX/1g2pqoctuPvGtk\" +\n\t\t\"mANOylbwV2RYvlpaYGhhx2YPZgZoOWixbfd11X06gm6bQk6bT1BTyVUGXW9d5l3uZ/tsmXRzeAKKUJOWiN9XUF6nYytQtVCsUY1nAFTJWF5Zu1uwX2liTWOhaD1rc24IcH2Rx3KAeBV4JnltZY59MIPciMGPCZabUmRXKGdQf2qMoVG0V0KWKlg6aYqAtFSyXQ5X/HiV\" +\n\t\t\"nfpPXFJKVItkPmYoZxRn9XqEe1Z9IpMvaFybrXs5UxlRilI3////////W99i9mSuZOZnLWu6hamW0XaQm9ZjTJMGm6t2v2ZSTglQmFPCXHFg6GSSZWNoX3Hmc8p1I3uXfoKGlYuDjNuReJkQZaxmq2uLTtVO1E86T39SOlP4U/JV41bbWOtZy1nJWf9bUFxNXgJeK1/X\" +\n\t\t\"YB1jB2UvW1xlr2W9ZehnnWti//9re2wPc0V5SXnBfPh9GX0rgKKBAoHziZaKXoppimaKjIrujMeM3JbMmPxrb06LTzxPjVFQW1db+mFIYwFmQmshbstsu3I+dL111HjBeTqADIAzgeqElI+ebFCef18Pi1idK3r6jvhbjZbrTgNT8Vf3WTFayVukYIluf28Gdb6M6luf\" +\n\t\t\"hQB74FByZ/SCnVxhhUp+HoIOUZlcBGNojWZlnHFueT59F4AFix2OypBuhseQqlAfUvpcOmdTcHxyNZFMkciTK4LlW8JfMWD5TjtT1luIYktnMWuKculz4HougWuNo5FSmZZRElPXVGpb/2OIajl9rJcAVtpTzlRo////////W5dcMV3eT+5hAWL+bTJ5wHnLfUJ+TX/S\" +\n\t\t\"ge2CH4SQiEaJcouQjnSPL5AxkUuRbJbGkZxOwE9PUUVTQV+TYg5n1GxBbgtzY34mkc2Sg1PUWRlbv23ReV1+LnybWH5xn1H6iFOP8E/KXPtmJXeseuOCHJn/UcZfqmXsaW9riW3z//9ulm9kdv59FF3hkHWRh5gGUeZSHWJAZpFm2W4aXrZ90n9yZviFr4X3ivhSqVPZ\" +\n\t\t\"WXNej1+QYFWS5JZkULdRH1LdUyBTR1PsVOhVRlUxVhdZaFm+WjxbtVwGXA9cEVwaXoReil7gX3Bif2KEYttjjGN3ZgdmDGYtZnZnfmiiah9qNWy8bYhuCW5YcTxxJnFndcd3AXhdeQF5ZXnweuB7EXynfTmAloPWhIuFSYhdiPOKH4o8ilSKc4xhjN6RpJJmk36UGJac\" +\n\t\t\"l5hOCk4ITh5OV1GXUnBXzlg0WMxbIl44YMVk/mdhZ1ZtRHK2dXN6Y4S4i3KRuJMgVjFX9Jj+////////Yu1pDWuWce1+VIB3gnKJ5pjfh1WPsVw7TzhP4U+1VQdaIFvdW+lfw2FOYy9lsGZLaO5pm214bfF1M3W5dx95XnnmfTOB44KvhaqJqoo6jquPm5Aykd2XB066\" +\n\t\t\"TsFSA1h1WOxcC3UaXD2BTooKj8WWY5dteyWKz5gIkWJW81Oo//+QF1Q5V4JeJWOobDRwindhfIt/4IhwkEKRVJMQkxiWj3RemsRdB11pZXBnoo2olttjbmdJaRmDxZgXlsCI/m+EZHpb+E4WcCx1XWYvUcRSNlLiWdNfgWAnYhBlP2V0Zh9mdGjyaBZrY24FcnJ1H3bb\" +\n\t\t\"fL6AVljwiP2Jf4qgipOKy5AdkZKXUpdZZYl6DoEGlrteLWDcYhplpWYUZ5B383pNfE1+PoEKjKyNZI3hjl94qVIHYtljpWRCYpiKLXqDe8CKrJbqfXaCDIdJTtlRSFNDU2Bbo1wCXBZd3WImYkdksGgTaDRsyW1FbRdn029ccU5xfWXLen97rX3a////////fkp/qIF6\" +\n\t\t\"ghuCOYWmim6Mzo31kHiQd5KtkpGVg5uuUk1VhG84cTZRaHmFflWBs3zOVkxYUVyoY6pm/mb9aVpy2XWPdY55DnlWed98l30gfUSGB4o0ljuQYZ8gUOdSdVPMU+JQCVWqWO5ZT3I9W4tcZFMdYONg82NcY4NjP2O7//9kzWXpZvld42nNaf1vFXHlTol16Xb4epN8333P\" +\n\t\t\"fZyAYYNJg1iEbIS8hfuIxY1wkAGQbZOXlxyaElDPWJdhjoHThTWNCJAgT8NQdFJHU3Ngb2NJZ19uLI2zkB9P11xejMplz32aU1KIllF2Y8NbWFtrXApkDWdRkFxO1lkaWSpscIpRVT5YFVmlYPBiU2fBgjVpVZZAmcSaKE9TWAZb/oAQXLFeL1+FYCBhS2I0Zv9s8G7e\" +\n\t\t\"gM6Bf4LUiIuMuJAAkC6Wip7bm9tO41PwWSd7LJGNmEyd+W7dcCdTU1VEW4ViWGKeYtNsom/vdCKKF5Q4b8GK/oM4UeeG+FPq////////U+lPRpBUj7BZaoExXf166o+/aNqMN3L4nEhqPYqwTjlTWFYGV2ZixWOiZeZrTm3hbltwrXfteu97qn27gD2AxobLipWTW1bj\" +\n\t\t\"WMdfPmWtZpZqgGu1dTeKx1Akd+VXMF8bYGVmemxgdfR6Gn9ugfSHGJBFmbN7yXVcevl7UYTE//+QEHnpepKDNlrhd0BOLU7yW5lf4GK9Zjxn8WzohmuId4o7kU6S85nQahdwJnMqgueEV4yvTgFRRlHLVYtb9V4WXjNegV8UXzVfa1+0YfJjEWaiZx1vbnJSdTp3OoB0\" +\n\t\t\"gTmBeId2ir+K3I2FjfOSmpV3mAKc5VLFY1d29GcVbIhzzYzDk66Wc20lWJxpDmnMj/2TmnXbkBpYWmgCY7Rp+09Dbyxn2I+7hSZ9tJNUaT9vcFdqWPdbLH0scipUCpHjnbROrU9OUFxQdVJDjJ5USFgkW5peHV6VXq1e918fYIxitWM6Y9Bor2xAeId5jnoLfeCCR4oC\" +\n\t\t\"iuaORJAT////////kLiRLZHYnw5s5WRYZOJldW70doR7G5Bpk9FuulTyX7lkpI9Nj+2SRFF4WGtZKVxVXpdt+36PdRyMvI7imFtwuU8da79vsXUwlvtRTlQQWDVYV1msXGBfkmWXZ1xuIXZ7g9+M7ZAUkP2TTXgleDpSql6mVx9ZdGASUBJRWlGs//9RzVIAVRBYVFhY\" +\n\t\t\"WVdblVz2XYtgvGKVZC1ncWhDaLxo33bXbdhub22bcG9xyF9Tddh5d3tJe1R7UnzWfXFSMIRjhWmF5IoOiwSMRo4PkAOQD5QZlnaYLZowldhQzVLVVAxYAlwOYadknm0ed7N65YD0hASQU5KFXOCdB1M/X5dfs22ccnl3Y3m/e+Rr0nLsiq1oA2phUfh6gWk0XEqc9oLr\" +\n\t\t\"W8WRSXAeVnhcb2DHZWZsjIxakEGYE1RRZseSDVlIkKNRhU5NUeqFmYsOcFhjepNLaWKZtH4EdXdTV2lgjt+W42xdToxcPF8Qj+lTAozRgImGeV7/ZeVOc1Fl////////WYJcP5fuTvtZil/Nio1v4XmweWJb54RxcytxsV50X/Vje2SaccN8mE5DXvxOS1fcVqJgqW/D\" +\n\t\t\"fQ2A/YEzgb+PsomXhqRd9GKKZK2Jh2d3bOJtPnQ2eDRaRn91gq2ZrE/zXsNi3WOSZVdnb3bDckyAzIC6jymRTVANV/lakmiF//9pc3Fkcv2Mt1jyjOCWapAZh3955HfnhClPL1JlU1pizWfPbMp2fXuUfJWCNoWEj+tm3W8gcgZ+G4OrmcGeplH9e7F4cnu4gId7SGro\" +\n\t\t\"XmGAjHVRdWBRa5Jibox2epGXmupPEH9wYpx7T5WlnOlWelhZhuSWvE80UiRTSlPNU9teBmQsZZFnf2w+bE5ySHKvc+11VH5BgiyF6Yype8SRxnFpmBKY72M9Zml1anbkeNCFQ4buUypTUVQmWYNeh198YLJiSWJ5YqtlkGvUbMx1snaueJF52H3Lf3eApYirirmMu5B/\" +\n\t\t\"l16Y22oLfDhQmVw+X65nh2vYdDV3CX+O////////nztnynoXUzl1i5rtX2aBnYPxgJhfPF/FdWJ7RpA8aGdZ61qbfRB2fossT/VfamoZbDdvAnTieWiIaIpVjHle32PPdcV50oLXkyiS8oSchu2cLVTBX2xljG1ccBWMp4zTmDtlT3T2Tg1O2FfgWStaZlvMUaheA16c\" +\n\t\t\"YBZidmV3//9lp2ZubW5yNnsmgVCBmoKZi1yMoIzmjXSWHJZET65kq2tmgh6EYYVqkOhcAWlTmKiEeoVXTw9Sb1+pXkVnDXmPgXmJB4mGbfVfF2JVbLhOz3Jpm5JSBlQ7VnRYs2GkYm5xGllufIl83n0blvBlh4BeThlPdVF1WEBeY15zXwpnxE4mhT2ViZZbfHOYAVD7\" +\n\t\t\"WMF2VninUiV3pYURe4ZQT1kJckd7x33oj7qP1JBNT79SyVopXwGXrU/dgheS6lcDY1VraXUriNyPFHpCUt9Yk2FVYgpmrmvNfD+D6VAjT/hTBVRGWDFZSVudXPBc710pXpZisWNnZT5luWcL////////bNVs4XD5eDJ+K4DegrOEDITshwKJEooqjEqQppLSmP2c851s\" +\n\t\t\"Tk9OoVCNUlZXSlmoXj1f2F/ZYj9mtGcbZ9Bo0lGSfSGAqoGoiwCMjIy/kn6WMlQgmCxTF1DVU1xYqGSyZzRyZ3dmekaR5lLDbKFrhlgAXkxZVGcsf/tR4XbG//9kaXjom1Seu1fLWblmJ2eaa85U6WnZXlWBnGeVm6pn/pxSaF1Opk/jU8hiuWcrbKuPxE+tfm2ev04H\" +\n\t\t\"YWJugG8rhRNUc2cqm0Vd83uVXKxbxoccbkqE0XoUgQhZmXyNbBF3IFLZWSJxIXJfd9uXJ51haQtaf1oYUaVUDVR9Zg5234/3kpic9Fnqcl1uxVFNaMl9v33sl2KeumR4aiGDAlmEW19r23MbdvJ9soAXhJlRMmcontl27mdiUv+ZBVwkYjt8foywVU9gtn0LlYBTAU5f\" +\n\t\t\"UbZZHHI6gDaRzl8ld+JThF95fQSFrIozjo2XVmfzha6UU2EJYQhsuXZS////////iu2POFUvT1FRKlLHU8tbpV59YKBhgmPWZwln2m5nbYxzNnM3dTF5UIjVipiQSpCRkPWWxIeNWRVOiE9ZTg6KiY8/mBBQrV58WZZbuV64Y9pj+mTBZtxpSmnYbQtutnGUdSh6r3+K\" +\n\t\t\"gACESYTJiYGLIY4KkGWWfZkKYX5ikWsy//9sg210f8x//G3Af4WHuoj4Z2WDsZg8lvdtG31hhD2Rak5xU3VdUGsEb+uFzYYtiadSKVQPXGVnTmiodAZ0g3XiiM+I4ZHMluKWeF+Lc4d6y4ROY6B1ZVKJbUFunHQJdVl4a3ySloZ63J+NT7ZhbmXFhlxOhk6uUNpOIVHM\" +\n\t\t\"W+5lmWiBbbxzH3ZCd616HHzngm+K0pB8kc+WdZgYUpt90VArU5hnl23LcdB0M4HojyqWo5xXnp90YFhBbZl9L5heTuRPNk+LUbdSsV26YBxzsnk8gtOSNJa3lvaXCp6Xn2Jmpmt0UhdSo3DIiMJeyWBLYZBvI3FJfD599IBv////////hO6QI5MsVEKbb2rTcImMwo3v\" +\n\t\t\"lzJStFpBXspfBGcXaXxplG1qbw9yYnL8e+2AAYB+h0uQzlFtnpN5hICLkzKK1lAtVIyKcWtqjMSBB2DRZ6Cd8k6ZTpicEIprhcGFaGkAbn54l4FV////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"/////////////////////////////18MThBOFU4qTjFONk48Tj9OQk5WTlhOgk6FjGtOioISXw1Ojk6eTp9OoE6iTrBOs062Ts5OzU7ETsZOwk7XTt5O7U7fTvdPCU9aTzBPW09dT1dPR092T4hPj0+YT3tPaU9wT5FPb0+GT5ZRGE/UT99Pzk/YT9tP0U/aT9BP5E/l\" +\n\t\t\"UBpQKFAUUCpQJVAFTxxP9lAhUClQLE/+T+9QEVAGUENQR2cDUFVQUFBIUFpQVlBsUHhQgFCaUIVQtFCy////////UMlQylCzUMJQ1lDeUOVQ7VDjUO5Q+VD1UQlRAVECURZRFVEUURpRIVE6UTdRPFE7UT9RQFFSUUxRVFFievhRaVFqUW5RgFGCVthRjFGJUY9RkVGT\" +\n\t\t\"UZVRllGkUaZRolGpUapRq1GzUbFRslGwUbVRvVHFUclR21HghlVR6VHt//9R8FH1Uf5SBFILUhRSDlInUipSLlIzUjlST1JEUktSTFJeUlRSalJ0UmlSc1J/Un1SjVKUUpJScVKIUpGPqI+nUqxSrVK8UrVSwVLNUtdS3lLjUuaY7VLgUvNS9VL4UvlTBlMIdThTDVMQ\" +\n\t\t\"Uw9TFVMaUyNTL1MxUzNTOFNAU0ZTRU4XU0lTTVHWU15TaVNuWRhTe1N3U4JTllOgU6ZTpVOuU7BTtlPDfBKW2VPfZvxx7lPuU+hT7VP6VAFUPVRAVCxULVQ8VC5UNlQpVB1UTlSPVHVUjlRfVHFUd1RwVJJUe1SAVHZUhFSQVIZUx1SiVLhUpVSsVMRUyFSo////////\" +\n\t\t\"VKtUwlSkVL5UvFTYVOVU5lUPVRRU/VTuVO1U+lTiVTlVQFVjVUxVLlVcVUVVVlVXVThVM1VdVZlVgFSvVYpVn1V7VX5VmFWeVa5VfFWDValVh1WoVdpVxVXfVcRV3FXkVdRWFFX3VhZV/lX9VhtV+VZOVlBx31Y0VjZWMlY4//9Wa1ZkVi9WbFZqVoZWgFaKVqBWlFaP\" +\n\t\t\"VqVWrla2VrRWwla8VsFWw1bAVshWzlbRVtNW11buVvlXAFb/VwRXCVcIVwtXDVcTVxhXFlXHVxxXJlc3VzhXTlc7V0BXT1dpV8BXiFdhV39XiVeTV6BXs1ekV6pXsFfDV8ZX1FfSV9NYClfWV+NYC1gZWB1YclghWGJYS1hwa8BYUlg9WHlYhVi5WJ9Yq1i6WN5Yu1i4\" +\n\t\t\"WK5YxVjTWNFY11jZWNhY5VjcWORY31jvWPpY+Vj7WPxY/VkCWQpZEFkbaKZZJVksWS1ZMlk4WT560llVWVBZTllaWVhZYllgWWdZbFlp////////WXhZgVmdT15Pq1mjWbJZxlnoWdxZjVnZWdpaJVofWhFaHFoJWhpaQFpsWklaNVo2WmJaalqaWrxavlrLWsJavVrj\" +\n\t\t\"Wtda5lrpWtZa+lr7WwxbC1sWWzJa0FsqWzZbPltDW0VbQFtRW1VbWltbW2VbaVtwW3NbdVt4ZYhbeluA//9bg1umW7hbw1vHW8lb1FvQW+Rb5lviW95b5VvrW/Bb9lvzXAVcB1wIXA1cE1wgXCJcKFw4XDlcQVxGXE5cU1xQXE9bcVxsXG5OYlx2XHlcjFyRXJRZm1yr\" +\n\t\t\"XLtctly8XLdcxVy+XMdc2VzpXP1c+lztXYxc6l0LXRVdF11cXR9dG10RXRRdIl0aXRldGF1MXVJdTl1LXWxdc112XYddhF2CXaJdnV2sXa5dvV2QXbddvF3JXc1d013SXdZd213rXfJd9V4LXhpeGV4RXhteNl43XkReQ15AXk5eV15UXl9eYl5kXkdedV52XnqevF5/\" +\n\t\t\"XqBewV7CXshe0F7P////////XtZe417dXtpe217iXuFe6F7pXuxe8V7zXvBe9F74Xv5fA18JX11fXF8LXxFfFl8pXy1fOF9BX0hfTF9OXy9fUV9WX1dfWV9hX21fc193X4Nfgl9/X4pfiF+RX4dfnl+ZX5hfoF+oX61fvF/WX/tf5F/4X/Ff3WCzX/9gIWBg//9gGWAQ\" +\n\t\t\"YClgDmAxYBtgFWArYCZgD2A6YFpgQWBqYHdgX2BKYEZgTWBjYENgZGBCYGxga2BZYIFgjWDnYINgmmCEYJtglmCXYJJgp2CLYOFguGDgYNNgtF/wYL1gxmC1YNhhTWEVYQZg9mD3YQBg9GD6YQNhIWD7YPFhDWEOYUdhPmEoYSdhSmE/YTxhLGE0YT1hQmFEYXNhd2FY\" +\n\t\t\"YVlhWmFrYXRhb2FlYXFhX2FdYVNhdWGZYZZhh2GsYZRhmmGKYZFhq2GuYcxhymHJYfdhyGHDYcZhumHLf3lhzWHmYeNh9mH6YfRh/2H9Yfxh/mIAYghiCWINYgxiFGIb////////Yh5iIWIqYi5iMGIyYjNiQWJOYl5iY2JbYmBiaGJ8YoJiiWJ+YpJik2KWYtRig2KU\" +\n\t\t\"Ytdi0WK7Ys9i/2LGZNRiyGLcYsxiymLCYsdim2LJYwxi7mLxYydjAmMIYu9i9WNQYz5jTWQcY09jlmOOY4Bjq2N2Y6Njj2OJY59jtWNr//9jaWO+Y+ljwGPGY+NjyWPSY/ZjxGQWZDRkBmQTZCZkNmUdZBdkKGQPZGdkb2R2ZE5lKmSVZJNkpWSpZIhkvGTaZNJkxWTH\" +\n\t\t\"ZLtk2GTCZPFk54IJZOBk4WKsZONk72UsZPZk9GTyZPplAGT9ZRhlHGUFZSRlI2UrZTRlNWU3ZTZlOHVLZUhlVmVVZU1lWGVeZV1lcmV4ZYJlg4uKZZtln2WrZbdlw2XGZcFlxGXMZdJl22XZZeBl4WXxZ3JmCmYDZftnc2Y1ZjZmNGYcZk9mRGZJZkFmXmZdZmRmZ2Zo\" +\n\t\t\"Zl9mYmZwZoNmiGaOZolmhGaYZp1mwWa5Zslmvma8////////ZsRmuGbWZtpm4GY/ZuZm6WbwZvVm92cPZxZnHmcmZyeXOGcuZz9nNmdBZzhnN2dGZ15nYGdZZ2NnZGeJZ3BnqWd8Z2pnjGeLZ6ZnoWeFZ7dn72e0Z+xns2fpZ7hn5GfeZ91n4mfuZ7lnzmfGZ+dqnGge\" +\n\t\t\"aEZoKWhAaE1oMmhO//9os2graFloY2h3aH9on2iPaK1olGidaJtog2quaLlodGi1aKBoumkPaI1ofmkBaMppCGjYaSJpJmjhaQxozWjUaOdo1Wk2aRJpBGjXaONpJWj5aOBo72koaSppGmkjaSFoxml5aXdpXGl4aWtpVGl+aW5pOWl0aT1pWWkwaWFpXmldaYFpammy\" +\n\t\t\"aa5p0Gm/acFp02m+ac5b6GnKad1pu2nDaadqLmmRaaBpnGmVabRp3mnoagJqG2n/awpp+WnyaedqBWmxah5p7WoUaetqCmoSasFqI2oTakRqDGpyajZqeGpHamJqWWpmakhqOGoiapBqjWqgaoRqomqj////////apeGF2q7asNqwmq4arNqrGreatFq32qqatpq6mr7\" +\n\t\t\"awWGFmr6axJrFpsxax9rOGs3dtxrOZjua0drQ2tJa1BrWWtUa1trX2tha3hreWt/a4BrhGuDa41rmGuVa55rpGuqa6trr2uya7Frs2u3a7xrxmvLa9Nr32vsa+tr82vv//+evmwIbBNsFGwbbCRsI2xebFVsYmxqbIJsjWyabIFsm2x+bGhsc2ySbJBsxGzxbNNsvWzX\" +\n\t\t\"bMVs3WyubLFsvmy6bNts72zZbOptH4hNbTZtK209bThtGW01bTNtEm0MbWNtk21kbVpteW1ZbY5tlW/kbYVt+W4VbgpttW3HbeZtuG3Gbext3m3Mbeht0m3Fbfpt2W3kbdVt6m3ubi1ubm4ubhlucm5fbj5uI25rbitudm5Nbh9uQ246bk5uJG7/bh1uOG6CbqpumG7J\" +\n\t\t\"brdu0269bq9uxG6ybtRu1W6PbqVuwm6fb0FvEXBMbuxu+G7+bz9u8m8xbu9vMm7M////////bz5vE273b4Zvem94b4FvgG9vb1tv829tb4JvfG9Yb45vkW/Cb2Zvs2+jb6FvpG+5b8Zvqm/fb9Vv7G/Ub9hv8W/ub9twCXALb/pwEXABcA9v/nAbcBpvdHAdcBhwH3Aw\" +\n\t\t\"cD5wMnBRcGNwmXCScK9w8XCscLhws3CucN9wy3Dd//9w2XEJcP1xHHEZcWVxVXGIcWZxYnFMcVZxbHGPcftxhHGVcahxrHHXcblxvnHScclx1HHOceBx7HHncfVx/HH5cf9yDXIQchtyKHItcixyMHIycjtyPHI/ckByRnJLclhydHJ+coJygXKHcpJylnKicqdyuXKy\" +\n\t\t\"csNyxnLEcs5y0nLicuBy4XL5cvdQD3MXcwpzHHMWcx1zNHMvcylzJXM+c05zT57Yc1dzanNoc3BzeHN1c3tzenPIc7NzznO7c8Bz5XPuc950onQFdG90JXP4dDJ0OnRVdD90X3RZdEF0XHRpdHB0Y3RqdHZ0fnSLdJ50p3TKdM901HPx////////dOB043TndOl07nTy\" +\n\t\t\"dPB08XT4dPd1BHUDdQV1DHUOdQ11FXUTdR51JnUsdTx1RHVNdUp1SXVbdUZ1WnVpdWR1Z3VrdW11eHV2dYZ1h3V0dYp1iXWCdZR1mnWddaV1o3XCdbN1w3W1db11uHW8dbF1zXXKddJ12XXjdd51/nX///91/HYBdfB1+nXydfN2C3YNdgl2H3YndiB2IXYidiR2NHYw\" +\n\t\t\"djt2R3ZIdkZ2XHZYdmF2YnZodml2anZndmx2cHZydnZ2eHZ8doB2g3aIdot2jnaWdpN2mXaadrB2tHa4drl2unbCds121nbSdt524Xbldud26oYvdvt3CHcHdwR3KXckdx53JXcmdxt3N3c4d0d3Wndod2t3W3dld393fnd5d453i3eRd6B3nnewd7Z3uXe/d7x3vXe7\" +\n\t\t\"d8d3zXfXd9p33Hfjd+53/HgMeBJ5JnggeSp4RXiOeHR4hnh8eJp4jHijeLV4qniveNF4xnjLeNR4vni8eMV4ynjs////////eOd42nj9ePR5B3kSeRF5GXkseSt5QHlgeVd5X3laeVV5U3l6eX95inmdeaefS3mqea55s3m5ebp5yXnVeed57HnheeN6CHoNehh6GXog\" +\n\t\t\"eh95gHoxejt6Pno3ekN6V3pJemF6Ynppn516cHp5en16iHqXepV6mHqWeql6yHqw//96tnrFesR6v5CDesd6ynrNes961XrTetl62nrdeuF64nrmeu168HsCew97CnsGezN7GHsZex57NXsoezZ7UHt6ewR7TXsLe0x7RXt1e2V7dHtne3B7cXtse257nXuYe597jXuc\" +\n\t\t\"e5p7i3uSe497XXuZe8t7wXvMe897tHvGe9176XwRfBR75nvlfGB8AHwHfBN783v3fBd8DXv2fCN8J3wqfB98N3wrfD18THxDfFR8T3xAfFB8WHxffGR8VnxlfGx8dXyDfJB8pHytfKJ8q3yhfKh8s3yyfLF8rny5fL18wHzFfMJ82HzSfNx84ps7fO988nz0fPZ8+n0G\" +\n\t\t\"////////fQJ9HH0VfQp9RX1LfS59Mn0/fTV9Rn1zfVZ9Tn1yfWh9bn1PfWN9k32JfVt9j319fZt9un2ufaN9tX3Hfb19q349faJ9r33cfbh9n32wfdh93X3kfd59+33yfeF+BX4KfiN+IX4SfjF+H34Jfgt+In5GfmZ+O341fjl+Q343//9+Mn46fmd+XX5Wfl5+WX5a\" +\n\t\t\"fnl+an5pfnx+e36DfdV+fY+ufn9+iH6Jfox+kn6QfpN+lH6Wfo5+m36cfzh/On9Ff0x/TX9Of1B/UX9Vf1R/WH9ff2B/aH9pf2d/eH+Cf4Z/g3+If4d/jH+Uf55/nX+af6N/r3+yf7l/rn+2f7iLcX/Ff8Z/yn/Vf9R/4X/mf+l/83/5mNyABoAEgAuAEoAYgBmAHIAh\" +\n\t\t\"gCiAP4A7gEqARoBSgFiAWoBfgGKAaIBzgHKAcIB2gHmAfYB/gISAhoCFgJuAk4CagK1RkICsgNuA5YDZgN2AxIDagNaBCYDvgPGBG4EpgSOBL4FL////////louBRoE+gVOBUYD8gXGBboFlgWaBdIGDgYiBioGAgYKBoIGVgaSBo4FfgZOBqYGwgbWBvoG4gb2BwIHC\" +\n\t\t\"gbqByYHNgdGB2YHYgciB2oHfgeCB54H6gfuB/oIBggKCBYIHggqCDYIQghaCKYIrgjiCM4JAglmCWIJdglqCX4Jk//+CYoJogmqCa4IugnGCd4J4gn6CjYKSgquCn4K7gqyC4YLjgt+C0oL0gvOC+oOTgwOC+4L5gt6DBoLcgwmC2YM1gzSDFoMygzGDQIM5g1CDRYMv\" +\n\t\t\"gyuDF4MYg4WDmoOqg5+DooOWgyODjoOHg4qDfIO1g3ODdYOgg4mDqIP0hBOD64POg/2EA4PYhAuDwYP3hAeD4IPyhA2EIoQgg72EOIUGg/uEbYQqhDyFWoSEhHeEa4SthG6EgoRphEaELIRvhHmENYTKhGKEuYS/hJ+E2YTNhLuE2oTQhMGExoTWhKGFIYT/hPSFF4UY\" +\n\t\t\"hSyFH4UVhRSE/IVAhWOFWIVI////////hUGGAoVLhVWFgIWkhYiFkYWKhaiFbYWUhZuF6oWHhZyFd4V+hZCFyYW6hc+FuYXQhdWF3YXlhdyF+YYKhhOGC4X+hfqGBoYihhqGMIY/hk1OVYZUhl+GZ4ZxhpOGo4aphqqGi4aMhraGr4bEhsaGsIbJiCOGq4bUht6G6Ybs\" +\n\t\t\"//+G34bbhu+HEocGhwiHAIcDhvuHEYcJhw2G+YcKhzSHP4c3hzuHJYcphxqHYIdfh3iHTIdOh3SHV4doh26HWYdTh2OHaogFh6KHn4eCh6+Hy4e9h8CH0JbWh6uHxIezh8eHxoe7h++H8ofgiA+IDYf+h/aH94gOh9KIEYgWiBWIIoghiDGINog5iCeIO4hEiEKIUohZ\" +\n\t\t\"iF6IYohriIGIfoieiHWIfYi1iHKIgoiXiJKIroiZiKKIjYikiLCIv4ixiMOIxIjUiNiI2YjdiPmJAoj8iPSI6IjyiQSJDIkKiROJQ4keiSWJKokriUGJRIk7iTaJOIlMiR2JYIle////////iWaJZIltiWqJb4l0iXeJfomDiYiJiomTiZiJoYmpiaaJrImvibKJuom9\" +\n\t\t\"ib+JwInaidyJ3YnnifSJ+IoDihaKEIoMihuKHYolijaKQYpbilKKRopIinyKbYpsimKKhYqCioSKqIqhipGKpYqmipqKo4rEis2KworaiuuK84rn//+K5IrxixSK4IriiveK3orbiwyLB4saiuGLFosQixeLIIszl6uLJosriz6LKItBi0yLT4tOi0mLVotbi1qLa4tf\" +\n\t\t\"i2yLb4t0i32LgIuMi46LkouTi5aLmYuajDqMQYw/jEiMTIxOjFCMVYxijGyMeIx6jIKMiYyFjIqMjYyOjJSMfIyYYh2MrYyqjL2MsoyzjK6MtozIjMGM5IzjjNqM/Yz6jPuNBI0FjQqNB40PjQ2NEJ9OjROMzY0UjRaNZ41tjXGNc42BjZmNwo2+jbqNz43ajdaNzI3b\" +\n\t\t\"jcuN6o3rjd+N4438jgiOCY3/jh2OHo4Qjh+OQo41jjCONI5K////////jkeOSY5MjlCOSI5ZjmSOYI4qjmOOVY52jnKOfI6BjoeOhY6EjouOio6TjpGOlI6ZjqqOoY6sjrCOxo6xjr6OxY7IjsuO247jjvyO+47rjv6PCo8FjxWPEo8ZjxOPHI8fjxuPDI8mjzOPO485\" +\n\t\t\"j0WPQo8+j0yPSY9Gj06PV49c//+PYo9jj2SPnI+fj6OPrY+vj7eP2o/lj+KP6o/vkIeP9JAFj/mP+pARkBWQIZANkB6QFpALkCeQNpA1kDmP+JBPkFCQUZBSkA6QSZA+kFaQWJBekGiQb5B2lqiQcpCCkH2QgZCAkIqQiZCPkKiQr5CxkLWQ4pDkYkiQ25ECkRKRGZEy\" +\n\t\t\"kTCRSpFWkViRY5FlkWmRc5FykYuRiZGCkaKRq5GvkaqRtZG0kbqRwJHBkcmRy5HQkdaR35HhkduR/JH1kfaSHpH/khSSLJIVkhGSXpJXkkWSSZJkkkiSlZI/kkuSUJKckpaSk5KbklqSz5K5kreS6ZMPkvqTRJMu////////kxmTIpMakyOTOpM1kzuTXJNgk3yTbpNW\" +\n\t\t\"k7CTrJOtk5STuZPWk9eT6JPlk9iTw5Pdk9CTyJPklBqUFJQTlAOUB5QQlDaUK5Q1lCGUOpRBlFKURJRblGCUYpRelGqSKZRwlHWUd5R9lFqUfJR+lIGUf5WClYeVipWUlZaVmJWZ//+VoJWolaeVrZW8lbuVuZW+lcpv9pXDlc2VzJXVldSV1pXcleGV5ZXiliGWKJYu\" +\n\t\t\"li+WQpZMlk+WS5Z3llyWXpZdll+WZpZylmyWjZaYlpWWl5aqlqeWsZaylrCWtJa2lriWuZbOlsuWyZbNiU2W3JcNltWW+ZcElwaXCJcTlw6XEZcPlxaXGZcklyqXMJc5lz2XPpdEl0aXSJdCl0mXXJdgl2SXZpdoUtKXa5dxl3mXhZd8l4GXepeGl4uXj5eQl5yXqJem\" +\n\t\t\"l6OXs5e0l8OXxpfIl8uX3Jftn0+X8nrfl/aX9ZgPmAyYOJgkmCGYN5g9mEaYT5hLmGuYb5hw////////mHGYdJhzmKqYr5ixmLaYxJjDmMaY6ZjrmQOZCZkSmRSZGJkhmR2ZHpkkmSCZLJkumT2ZPplCmUmZRZlQmUuZUZlSmUyZVZmXmZiZpZmtma6ZvJnfmduZ3ZnY\" +\n\t\t\"mdGZ7ZnumfGZ8pn7mfiaAZoPmgWZ4poZmiuaN5pFmkKaQJpD//+aPppVmk2aW5pXml+aYpplmmSaaZprmmqarZqwmryawJrPmtGa05rUmt6a35rimuOa5prvmuua7pr0mvGa95r7mwabGJsamx+bIpsjmyWbJ5somymbKpsumy+bMptEm0ObT5tNm06bUZtYm3Sbk5uD\" +\n\t\t\"m5GblpuXm5+boJuom7SbwJvKm7mbxpvPm9Gb0pvjm+Kb5JvUm+GcOpvym/Gb8JwVnBScCZwTnAycBpwInBKcCpwEnC6cG5wlnCScIZwwnEecMpxGnD6cWpxgnGecdpx4nOec7JzwnQmdCJzrnQOdBp0qnSadr50jnR+dRJ0VnRKdQZ0/nT6dRp1I////////nV2dXp1k\" +\n\t\t\"nVGdUJ1ZnXKdiZ2Hnaudb516nZqdpJ2pnbKdxJ3BnbuduJ26ncadz53Cndmd0534nead7Z3vnf2eGp4bnh6edZ55nn2egZ6InouejJ6SnpWekZ6dnqWeqZ64nqqerZdhnsyezp7PntCe1J7cnt6e3Z7gnuWe6J7v//+e9J72nvee+Z77nvye/Z8Hnwh2t58VnyGfLJ8+\" +\n\t\t\"n0qfUp9Un2OfX59gn2GfZp9nn2yfap93n3Kfdp+Vn5yfoFgvaceQWXRkUdxxmf//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"/////////////////////////////////////////////w==\";\n\t\n\t\n\tprivate static short[] UNICODE_TO_QR_KANJI = new short[1 << 16];\n\t\n\tstatic {  // Unpack the Shift JIS table into a more computation-friendly form\n\t\tArrays.fill(UNICODE_TO_QR_KANJI, (short)-1);\n\t\tbyte[] bytes = Base64.getDecoder().decode(PACKED_QR_KANJI_TO_UNICODE);\n\t\tfor (int i = 0; i < bytes.length; i += 2) {\n\t\t\tchar c = (char)(((bytes[i] & 0xFF) << 8) | (bytes[i + 1] & 0xFF));\n\t\t\tif (c == 0xFFFF)\n\t\t\t\tcontinue;\n\t\t\tassert UNICODE_TO_QR_KANJI[c] == -1;\n\t\t\tUNICODE_TO_QR_KANJI[c] = (short)(i / 2);\n\t\t}\n\t}\n\t\n\t\n\t\n\t/*---- Miscellaneous ----*/\n\t\n\tprivate QrSegmentAdvanced() {}  // Not instantiable\n\t\n}\n"
  },
  {
    "path": "java/src/main/java/io/nayuki/qrcodegen/package-info.java",
    "content": "/**\n * Generates QR Codes from text strings and byte arrays.\n * \n * <p>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.</p>\n * <p>Home page with live JavaScript demo, extensive descriptions, and competitor comparisons: <a href=\"https://www.nayuki.io/page/qr-code-generator-library\">https://www.nayuki.io/page/qr-code-generator-library</a></p>\n * \n * <h2>Features</h2>\n * <p>Core features:</p>\n * <ul>\n *   <li><p>Significantly shorter code but more documentation comments compared to competing libraries</p></li>\n *   <li><p>Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard</p></li>\n *   <li><p>Output format: Raw modules/pixels of the QR symbol</p></li>\n *   <li><p>Detects finder-like penalty patterns more accurately than other implementations</p></li>\n *   <li><p>Encodes numeric and special-alphanumeric text in less space than general text</p></li>\n *   <li><p>Open-source code under the permissive MIT License</p></li>\n * </ul>\n * <p>Manual parameters:</p>\n * <ul>\n *   <li><p>User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data</p></li>\n *   <li><p>User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one</p></li>\n *   <li><p>User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number</p></li>\n *   <li><p>User can create a list of data segments manually and add ECI segments</p></li>\n * </ul>\n * <p>Optional advanced features:</p>\n * <ul>\n *   <li><p>Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes</p></li>\n *   <li><p>Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts</p></li>\n * </ul>\n * <p>More information about QR Code technology and this library's design can be found on the project home page.</p>\n * \n * <h2>Examples</h2>\n * <p>Simple operation:</p>\n * <pre style=\"margin-left:2em\">import java.awt.image.BufferedImage;\n *import java.io.File;\n *import javax.imageio.ImageIO;\n *import io.nayuki.qrcodegen.*;\n *\n *QrCode qr = QrCode.encodeText(\"Hello, world!\", QrCode.Ecc.MEDIUM);\n *BufferedImage img = toImage(qr, 4, 10);  // See QrCodeGeneratorDemo\n *ImageIO.write(img, \"png\", new File(\"qr-code.png\"));</pre>\n * <p>Manual operation:</p>\n * <pre style=\"margin-left:2em\">import java.util.List;\n *import io.nayuki.qrcodegen.*;\n *\n *List&lt;QrSegment&gt; segs = QrSegment.makeSegments(\"3141592653589793238462643383\");\n *QrCode qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);\n *for (int y = 0; y &lt; qr.size; y++) {\n *    for (int x = 0; x &lt; qr.size; x++) {\n *        (... paint qr.getModule(x, y) ...)\n *    }\n *}</pre>\n */\npackage io.nayuki.qrcodegen;\n"
  },
  {
    "path": "java/src/main/java/module-info.java",
    "content": "/* \n * QR Code generator library (Java)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n\nmodule io.nayuki.qrcodegen {\n\t\n\texports io.nayuki.qrcodegen;\n\t\n}\n"
  },
  {
    "path": "java-fast/Readme.markdown",
    "content": "QR Code generator library - Java, fast\n======================================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page for this fast library with design explanation and benchmarks: https://www.nayuki.io/page/fast-qr-code-generator-library\n\nHome page for the main project with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Approximately 1.5× to 10× faster than other Java implementation\n* Shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes\n* Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```java\nimport java.awt.image.BufferedImage;\nimport java.io.File;\nimport java.util.List;\nimport javax.imageio.ImageIO;\nimport io.nayuki.fastqrcodegen.*;\n\n// Simple operation\nQrCode qr0 = QrCode.encodeText(\"Hello, world!\", QrCode.Ecc.MEDIUM);\nBufferedImage img = toImage(qr0, 4, 10);  // See QrCodeGeneratorDemo\nImageIO.write(img, \"png\", new File(\"qr-code.png\"));\n\n// Manual operation\nList<QrSegment> segs = QrSegment.makeSegments(\"3141592653589793238462643383\");\nQrCode qr1 = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);\nfor (int y = 0; y < qr1.size; y++) {\n    for (int x = 0; x < qr1.size; x++) {\n        (... paint qr1.getModule(x, y) ...)\n    }\n}\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/java-fast/io/nayuki/fastqrcodegen/QrCodeGeneratorDemo.java .\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/BitBuffer.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.util.Arrays;\nimport java.util.Objects;\n\n\n// An appendable sequence of bits (0s and 1s), mainly used by QrSegment.\nfinal class BitBuffer {\n\t\n\t/*---- Fields ----*/\n\t\n\tint[] data;  // In each 32-bit word, bits are filled from top down.\n\t\n\tint bitLength;  // Always non-negative.\n\t\n\t\n\t\n\t/*---- Constructor ----*/\n\t\n\t// Creates an empty bit buffer.\n\tpublic BitBuffer() {\n\t\tdata = new int[64];\n\t\tbitLength = 0;\n\t}\n\t\n\t\n\t\n\t/*---- Methods ----*/\n\t\n\t// Returns the bit at the given index, yielding 0 or 1.\n\tpublic int getBit(int index) {\n\t\tif (index < 0 || index >= bitLength)\n\t\t\tthrow new IndexOutOfBoundsException();\n\t\treturn (data[index >>> 5] >>> ~index) & 1;\n\t}\n\t\n\t\n\t// Returns a new array representing this buffer's bits packed into\n\t// bytes in big endian. The current bit length must be a multiple of 8.\n\tpublic byte[] getBytes() {\n\t\tif (bitLength % 8 != 0)\n\t\t\tthrow new IllegalStateException(\"Data is not a whole number of bytes\");\n\t\tbyte[] result = new byte[bitLength / 8];\n\t\tfor (int i = 0; i < result.length; i++)\n\t\t\tresult[i] = (byte)(data[i >>> 2] >>> (~i << 3));\n\t\treturn result;\n\t}\n\t\n\t\n\t// Appends the given number of low-order bits of the given value\n\t// to this buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.\n\tpublic void appendBits(int val, int len) {\n\t\tif (len < 0 || len > 31 || val >>> len != 0)\n\t\t\tthrow new IllegalArgumentException(\"Value out of range\");\n\t\tif (len > Integer.MAX_VALUE - bitLength)\n\t\t\tthrow new IllegalStateException(\"Maximum length reached\");\n\t\t\n\t\tif (bitLength + len + 1 > data.length << 5)\n\t\t\tdata = Arrays.copyOf(data, data.length * 2);\n\t\tassert bitLength + len <= data.length << 5;\n\t\t\n\t\tint remain = 32 - (bitLength & 0x1F);\n\t\tassert 1 <= remain && remain <= 32;\n\t\tif (remain < len) {\n\t\t\tdata[bitLength >>> 5] |= val >>> (len - remain);\n\t\t\tbitLength += remain;\n\t\t\tassert (bitLength & 0x1F) == 0;\n\t\t\tlen -= remain;\n\t\t\tval &= (1 << len) - 1;\n\t\t\tremain = 32;\n\t\t}\n\t\tdata[bitLength >>> 5] |= val << (remain - len);\n\t\tbitLength += len;\n\t}\n\t\n\t\n\t// Appends to this buffer the sequence of bits represented by the given\n\t// word array and given bit length. Requires 0 <= len <= 32 * vals.length.\n\tpublic void appendBits(int[] vals, int len) {\n\t\tObjects.requireNonNull(vals);\n\t\tif (len == 0)\n\t\t\treturn;\n\t\tif (len < 0 || len > vals.length * 32L)\n\t\t\tthrow new IllegalArgumentException(\"Value out of range\");\n\t\tint wholeWords = len / 32;\n\t\tint tailBits = len % 32;\n\t\tif (tailBits > 0 && vals[wholeWords] << tailBits != 0)\n\t\t\tthrow new IllegalArgumentException(\"Last word must have low bits clear\");\n\t\tif (len > Integer.MAX_VALUE - bitLength)\n\t\t\tthrow new IllegalStateException(\"Maximum length reached\");\n\t\t\n\t\twhile (bitLength + len > data.length * 32)\n\t\t\tdata = Arrays.copyOf(data, data.length * 2);\n\t\t\n\t\tint shift = bitLength % 32;\n\t\tif (shift == 0) {\n\t\t\tSystem.arraycopy(vals, 0, data, bitLength / 32, (len + 31) / 32);\n\t\t\tbitLength += len;\n\t\t} else {\n\t\t\tfor (int i = 0; i < wholeWords; i++) {\n\t\t\t\tint word = vals[i];\n\t\t\t\tdata[bitLength >>> 5] |= word >>> shift;\n\t\t\t\tbitLength += 32;\n\t\t\t\tdata[bitLength >>> 5] = word << (32 - shift);\n\t\t\t}\n\t\t\tif (tailBits > 0)\n\t\t\t\tappendBits(vals[wholeWords] >>> (32 - tailBits), tailBits);\n\t\t}\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/DataTooLongException.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\n\n/**\n * Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:\n * <ul>\n *   <li><p>Decrease the error correction level if it was greater than {@code Ecc.LOW}.</p></li>\n *   <li><p>If the advanced {@code encodeSegments()} function with 6 arguments or the\n *     {@code makeSegmentsOptimally()} function was called, then increase the maxVersion argument\n *     if it was less than {@link QrCode#MAX_VERSION}. (This advice does not apply to the other\n *     factory functions because they search all versions up to {@code QrCode.MAX_VERSION}.)</p></li>\n *   <li><p>Split the text data into better or optimal segments in order to reduce the number of\n *     bits required. (See {@link QrSegmentAdvanced#makeSegmentsOptimally(String,QrCode.Ecc,int,int)\n *     QrSegmentAdvanced.makeSegmentsOptimally()}.)</p></li>\n *   <li><p>Change the text or binary data to be shorter.</p></li>\n *   <li><p>Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).</p></li>\n *   <li><p>Propagate the error upward to the caller/user.</p></li>\n * </ul>\n * @see QrCode#encodeText(String, QrCode.Ecc)\n * @see QrCode#encodeBinary(byte[], QrCode.Ecc)\n * @see QrCode#encodeSegments(java.util.List, QrCode.Ecc)\n * @see QrCode#encodeSegments(java.util.List, QrCode.Ecc, int, int, int, boolean)\n * @see QrSegmentAdvanced#makeSegmentsOptimally(String, QrCode.Ecc, int, int)\n */\npublic class DataTooLongException extends IllegalArgumentException {\n\t\n\tpublic DataTooLongException() {}\n\t\n\t\n\tpublic DataTooLongException(String msg) {\n\t\tsuper(msg);\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/Memoizer.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.lang.ref.SoftReference;\nimport java.util.HashSet;\nimport java.util.Map;\nimport java.util.Set;\nimport java.util.concurrent.ConcurrentHashMap;\nimport java.util.function.Function;\n\n\n// A thread-safe cache based on soft references.\nfinal class Memoizer<T,R> {\n\t\n\tprivate final Function<T,R> function;\n\tMap<T,SoftReference<R>> cache = new ConcurrentHashMap<>();\n\tprivate Set<T> pending = new HashSet<>();\n\t\n\t\n\t// Creates a memoizer based on the given function that takes one input to compute an output.\n\tpublic Memoizer(Function<T,R> func) {\n\t\tfunction = func;\n\t}\n\t\n\t\n\t// Computes function.apply(arg) or returns a cached copy of a previous call.\n\tpublic R get(T arg) {\n\t\t// Non-blocking fast path\n\t\t{\n\t\t\tSoftReference<R> ref = cache.get(arg);\n\t\t\tif (ref != null) {\n\t\t\t\tR result = ref.get();\n\t\t\t\tif (result != null)\n\t\t\t\t\treturn result;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Sequential slow path\n\t\twhile (true) {\n\t\t\tsynchronized(this) {\n\t\t\t\tSoftReference<R> ref = cache.get(arg);\n\t\t\t\tif (ref != null) {\n\t\t\t\t\tR result = ref.get();\n\t\t\t\t\tif (result != null)\n\t\t\t\t\t\treturn result;\n\t\t\t\t\tcache.remove(arg);\n\t\t\t\t}\n\t\t\t\tassert !cache.containsKey(arg);\n\t\t\t\t\n\t\t\t\tif (pending.add(arg))\n\t\t\t\t\tbreak;\n\t\t\t\t\n\t\t\t\ttry {\n\t\t\t\t\tthis.wait();\n\t\t\t\t} catch (InterruptedException e) {\n\t\t\t\t\tthrow new RuntimeException(e);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\ttry {\n\t\t\tR result = function.apply(arg);\n\t\t\tcache.put(arg, new SoftReference<>(result));\n\t\t\treturn result;\n\t\t} finally {\n\t\t\tsynchronized(this) {\n\t\t\t\tpending.remove(arg);\n\t\t\t\tthis.notifyAll();\n\t\t\t}\n\t\t}\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/QrCode.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Objects;\n\n\n/**\n * A QR Code symbol, which is a type of two-dimension barcode.\n * Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n * <p>Instances of this class represent an immutable square grid of dark and light cells.\n * The class provides static factory functions to create a QR Code from text or binary data.\n * The class covers the QR Code Model 2 specification, supporting all versions (sizes)\n * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.</p>\n * <p>Ways to create a QR Code object:</p>\n * <ul>\n *   <li><p>High level: Take the payload data and call {@link QrCode#encodeText(String,Ecc)}\n *     or {@link QrCode#encodeBinary(byte[],Ecc)}.</p></li>\n *   <li><p>Mid level: Custom-make the list of {@link QrSegment segments}\n *     and call {@link QrCode#encodeSegments(List,Ecc)} or\n *     {@link QrCode#encodeSegments(List,Ecc,int,int,int,boolean)}</p></li>\n *   <li><p>Low level: Custom-make the array of data codeword bytes (including segment headers and\n *     final padding, excluding error correction codewords), supply the appropriate version number,\n *     and call the {@link QrCode#QrCode(int,Ecc,byte[],int) constructor}.</p></li>\n * </ul>\n * <p>(Note that all ways require supplying the desired error correction level.)</p>\n * @see QrSegment\n */\npublic final class QrCode {\n\t\n\t/*---- Static factory functions (high level) ----*/\n\t\n\t/**\n\t * Returns a QR Code representing the specified Unicode text string at the specified error correction level.\n\t * As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer\n\t * Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible\n\t * QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the\n\t * ecl argument if it can be done without increasing the version.\n\t * @param text the text to be encoded (not {@code null}), which can be any Unicode string\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @return a QR Code (not {@code null}) representing the text\n\t * @throws NullPointerException if the text or error correction level is {@code null}\n\t * @throws DataTooLongException if the text fails to fit in the\n\t * largest version QR Code at the ECL, which means it is too long\n\t */\n\tpublic static QrCode encodeText(String text, Ecc ecl) {\n\t\tObjects.requireNonNull(text);\n\t\tObjects.requireNonNull(ecl);\n\t\tList<QrSegment> segs = QrSegment.makeSegments(text);\n\t\treturn encodeSegments(segs, ecl);\n\t}\n\t\n\t\n\t/**\n\t * Returns a QR Code representing the specified binary data at the specified error correction level.\n\t * This function always encodes using the binary segment mode, not any text mode. The maximum number of\n\t * bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.\n\t * The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t * @param data the binary data to encode (not {@code null})\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @return a QR Code (not {@code null}) representing the data\n\t * @throws NullPointerException if the data or error correction level is {@code null}\n\t * @throws DataTooLongException if the data fails to fit in the\n\t * largest version QR Code at the ECL, which means it is too long\n\t */\n\tpublic static QrCode encodeBinary(byte[] data, Ecc ecl) {\n\t\tObjects.requireNonNull(data);\n\t\tObjects.requireNonNull(ecl);\n\t\tQrSegment seg = QrSegment.makeBytes(data);\n\t\treturn encodeSegments(Arrays.asList(seg), ecl);\n\t}\n\t\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/**\n\t * Returns a QR Code representing the specified segments at the specified error correction\n\t * level. The smallest possible QR Code version is automatically chosen for the output. The ECC level\n\t * of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t * <p>This function allows the user to create a custom sequence of segments that switches\n\t * between modes (such as alphanumeric and byte) to encode text in less space.\n\t * This is a mid-level API; the high-level API is {@link #encodeText(String,Ecc)}\n\t * and {@link #encodeBinary(byte[],Ecc)}.</p>\n\t * @param segs the segments to encode\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @return a QR Code (not {@code null}) representing the segments\n\t * @throws NullPointerException if the list of segments, any segment, or the error correction level is {@code null}\n\t * @throws DataTooLongException if the segments fail to fit in the\n\t * largest version QR Code at the ECL, which means they are too long\n\t */\n\tpublic static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl) {\n\t\treturn encodeSegments(segs, ecl, MIN_VERSION, MAX_VERSION, -1, true);\n\t}\n\t\n\t\n\t/**\n\t * Returns a QR Code representing the specified segments with the specified encoding parameters.\n\t * The smallest possible QR Code version within the specified range is automatically\n\t * chosen for the output. Iff boostEcl is {@code true}, then the ECC level of the\n\t * result may be higher than the ecl argument if it can be done without increasing\n\t * the version. The mask number is either between 0 to 7 (inclusive) to force that\n\t * mask, or &#x2212;1 to automatically choose an appropriate mask (which may be slow).\n\t * <p>This function allows the user to create a custom sequence of segments that switches\n\t * between modes (such as alphanumeric and byte) to encode text in less space.\n\t * This is a mid-level API; the high-level API is {@link #encodeText(String,Ecc)}\n\t * and {@link #encodeBinary(byte[],Ecc)}.</p>\n\t * @param segs the segments to encode\n\t * @param ecl the error correction level to use (not {@code null}) (boostable)\n\t * @param minVersion the minimum allowed version of the QR Code (at least 1)\n\t * @param maxVersion the maximum allowed version of the QR Code (at most 40)\n\t * @param mask the mask number to use (between 0 and 7 (inclusive)), or &#x2212;1 for automatic mask\n\t * @param boostEcl increases the ECC level as long as it doesn't increase the version number\n\t * @return a QR Code (not {@code null}) representing the segments\n\t * @throws NullPointerException if the list of segments, any segment, or the error correction level is {@code null}\n\t * @throws IllegalArgumentException if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40\n\t * or &#x2212;1 &#x2264; mask &#x2264; 7 is violated\n\t * @throws DataTooLongException if the segments fail to fit in\n\t * the maxVersion QR Code at the ECL, which means they are too long\n\t */\n\tpublic static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl, int minVersion, int maxVersion, int mask, boolean boostEcl) {\n\t\tObjects.requireNonNull(segs);\n\t\tObjects.requireNonNull(ecl);\n\t\tif (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)\n\t\t\tthrow new IllegalArgumentException(\"Invalid value\");\n\t\t\n\t\t// Find the minimal version number to use\n\t\tint version, dataUsedBits;\n\t\tfor (version = minVersion; ; version++) {\n\t\t\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\t\tdataUsedBits = QrSegment.getTotalBits(segs, version);\n\t\t\tif (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)\n\t\t\t\tbreak;  // This version number is found to be suitable\n\t\t\tif (version >= maxVersion) {  // All versions in the range could not fit the given data\n\t\t\t\tString msg = \"Segment too long\";\n\t\t\t\tif (dataUsedBits != -1)\n\t\t\t\t\tmsg = String.format(\"Data length = %d bits, Max capacity = %d bits\", dataUsedBits, dataCapacityBits);\n\t\t\t\tthrow new DataTooLongException(msg);\n\t\t\t}\n\t\t}\n\t\tassert dataUsedBits != -1;\n\t\t\n\t\t// Increase the error correction level while the data still fits in the current version number\n\t\tfor (Ecc newEcl : Ecc.values()) {  // From low to high\n\t\t\tif (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)\n\t\t\t\tecl = newEcl;\n\t\t}\n\t\t\n\t\t// Concatenate all segments to create the data bit string\n\t\tBitBuffer bb = new BitBuffer();\n\t\tfor (QrSegment seg : segs) {\n\t\t\tbb.appendBits(seg.mode.modeBits, 4);\n\t\t\tbb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));\n\t\t\tbb.appendBits(seg.data, seg.bitLength);\n\t\t}\n\t\tassert bb.bitLength == dataUsedBits;\n\t\t\n\t\t// Add terminator and pad up to a byte if applicable\n\t\tint dataCapacityBits = getNumDataCodewords(version, ecl) * 8;\n\t\tassert bb.bitLength <= dataCapacityBits;\n\t\tbb.appendBits(0, Math.min(4, dataCapacityBits - bb.bitLength));\n\t\tbb.appendBits(0, (8 - bb.bitLength % 8) % 8);\n\t\tassert bb.bitLength % 8 == 0;\n\t\t\n\t\t// Pad with alternating bytes until data capacity is reached\n\t\tfor (int padByte = 0xEC; bb.bitLength < dataCapacityBits; padByte ^= 0xEC ^ 0x11)\n\t\t\tbb.appendBits(padByte, 8);\n\t\t\n\t\t// Create the QR Code object\n\t\treturn new QrCode(version, ecl, bb.getBytes(), mask);\n\t}\n\t\n\t\n\t\n\t/*---- Instance fields ----*/\n\t\n\t// Public immutable scalar parameters:\n\t\n\t/** The version number of this QR Code, which is between 1 and 40 (inclusive).\n\t * This determines the size of this barcode. */\n\tpublic final int version;\n\t\n\t/** The width and height of this QR Code, measured in modules, between\n\t * 21 and 177 (inclusive). This is equal to version &#xD7; 4 + 17. */\n\tpublic final int size;\n\t\n\t/** The error correction level used in this QR Code, which is not {@code null}. */\n\tpublic final Ecc errorCorrectionLevel;\n\t\n\t/** The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).\n\t * <p>Even if a QR Code is created with automatic masking requested (mask =\n\t * &#x2212;1), the resulting object still has a mask value between 0 and 7. */\n\tpublic final int mask;\n\t\n\t// Private grid of modules of this QR Code, packed tightly into bits.\n\t// Immutable after constructor finishes. Accessed through getModule().\n\tprivate final int[] modules;\n\t\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/**\n\t * Constructs a QR Code with the specified version number,\n\t * error correction level, data codeword bytes, and mask number.\n\t * <p>This is a low-level API that most users should not use directly. A mid-level\n\t * API is the {@link #encodeSegments(List,Ecc,int,int,int,boolean)} function.</p>\n\t * @param ver the version number to use, which must be in the range 1 to 40 (inclusive)\n\t * @param ecl the error correction level to use\n\t * @param dataCodewords the bytes representing segments to encode (without ECC)\n\t * @param msk the mask pattern to use, which is either &#x2212;1 for automatic choice or from 0 to 7 for fixed choice\n\t * @throws NullPointerException if the byte array or error correction level is {@code null}\n\t * @throws IllegalArgumentException if the version or mask value is out of range,\n\t * or if the data is the wrong length for the specified version and error correction level\n\t */\n\tpublic QrCode(int ver, Ecc ecl, byte[] dataCodewords, int msk) {\n\t\t// Check arguments and initialize fields\n\t\tif (ver < MIN_VERSION || ver > MAX_VERSION)\n\t\t\tthrow new IllegalArgumentException(\"Version value out of range\");\n\t\tif (msk < -1 || msk > 7)\n\t\t\tthrow new IllegalArgumentException(\"Mask value out of range\");\n\t\tversion = ver;\n\t\tsize = ver * 4 + 17;\n\t\terrorCorrectionLevel = Objects.requireNonNull(ecl);\n\t\tObjects.requireNonNull(dataCodewords);\n\t\t\n\t\tQrTemplate tpl = QrTemplate.MEMOIZER.get(ver);\n\t\tmodules = tpl.template.clone();\n\t\t\n\t\t// Compute ECC, draw modules, do masking\n\t\tbyte[] allCodewords = addEccAndInterleave(dataCodewords);\n\t\tdrawCodewords(tpl.dataOutputBitIndexes, allCodewords);\n\t\tmask = handleConstructorMasking(tpl.masks, msk);\n\t}\n\t\n\t\n\t\n\t/*---- Public instance methods ----*/\n\t\n\t/**\n\t * Returns the color of the module (pixel) at the specified coordinates, which is {@code false}\n\t * for light or {@code true} for dark. The top left corner has the coordinates (x=0, y=0).\n\t * If the specified coordinates are out of bounds, then {@code false} (light) is returned.\n\t * @param x the x coordinate, where 0 is the left edge and size&#x2212;1 is the right edge\n\t * @param y the y coordinate, where 0 is the top edge and size&#x2212;1 is the bottom edge\n\t * @return {@code true} if the coordinates are in bounds and the module\n\t * at that location is dark, or {@code false} (light) otherwise\n\t */\n\tpublic boolean getModule(int x, int y) {\n\t\tif (0 <= x && x < size && 0 <= y && y < size) {\n\t\t\tint i = y * size + x;\n\t\t\treturn getBit(modules[i >>> 5], i) != 0;\n\t\t} else\n\t\t\treturn false;\n\t}\n\t\n\t\n\t\n\t/*---- Private helper methods for constructor: Drawing function modules ----*/\n\t\n\t// Draws two copies of the format bits (with its own error correction code)\n\t// based on the given mask and this object's error correction level field.\n\tprivate void drawFormatBits(int msk) {\n\t\t// Calculate error correction code and pack bits\n\t\tint data = errorCorrectionLevel.formatBits << 3 | msk;  // errCorrLvl is uint2, mask is uint3\n\t\tint rem = data;\n\t\tfor (int i = 0; i < 10; i++)\n\t\t\trem = (rem << 1) ^ ((rem >>> 9) * 0x537);\n\t\tint bits = (data << 10 | rem) ^ 0x5412;  // uint15\n\t\tassert bits >>> 15 == 0;\n\t\t\n\t\t// Draw first copy\n\t\tfor (int i = 0; i <= 5; i++)\n\t\t\tsetModule(8, i, getBit(bits, i));\n\t\tsetModule(8, 7, getBit(bits, 6));\n\t\tsetModule(8, 8, getBit(bits, 7));\n\t\tsetModule(7, 8, getBit(bits, 8));\n\t\tfor (int i = 9; i < 15; i++)\n\t\t\tsetModule(14 - i, 8, getBit(bits, i));\n\t\t\n\t\t// Draw second copy\n\t\tfor (int i = 0; i < 8; i++)\n\t\t\tsetModule(size - 1 - i, 8, getBit(bits, i));\n\t\tfor (int i = 8; i < 15; i++)\n\t\t\tsetModule(8, size - 15 + i, getBit(bits, i));\n\t\tsetModule(8, size - 8, 1);  // Always dark\n\t}\n\t\n\t\n\t// Sets the module at the given coordinates to the given color.\n\t// Only used by the constructor. Coordinates must be in bounds.\n\tprivate void setModule(int x, int y, int dark) {\n\t\tassert 0 <= x && x < size;\n\t\tassert 0 <= y && y < size;\n\t\tassert dark == 0 || dark == 1;\n\t\tint i = y * size + x;\n\t\tmodules[i >>> 5] &= ~(1 << i);\n\t\tmodules[i >>> 5] |= dark << i;\n\t}\n\t\n\t\n\t/*---- Private helper methods for constructor: Codewords and masking ----*/\n\t\n\t// Returns a new byte string representing the given data with the appropriate error correction\n\t// codewords appended to it, based on this object's version and error correction level.\n\tprivate byte[] addEccAndInterleave(byte[] data) {\n\t\tObjects.requireNonNull(data);\n\t\tif (data.length != getNumDataCodewords(version, errorCorrectionLevel))\n\t\t\tthrow new IllegalArgumentException();\n\t\t\n\t\t// Calculate parameter numbers\n\t\tint numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal()][version];\n\t\tint blockEccLen = ECC_CODEWORDS_PER_BLOCK  [errorCorrectionLevel.ordinal()][version];\n\t\tint rawCodewords = QrTemplate.getNumRawDataModules(version) / 8;\n\t\tint numShortBlocks = numBlocks - rawCodewords % numBlocks;\n\t\tint shortBlockDataLen = rawCodewords / numBlocks - blockEccLen;\n\t\t\n\t\t// Split data into blocks, calculate ECC, and interleave\n\t\t// (not concatenate) the bytes into a single sequence\n\t\tbyte[] result = new byte[rawCodewords];\n\t\tReedSolomonGenerator rs = ReedSolomonGenerator.MEMOIZER.get(blockEccLen);\n\t\tbyte[] ecc = new byte[blockEccLen];  // Temporary storage per iteration\n\t\tfor (int i = 0, k = 0; i < numBlocks; i++) {\n\t\t\tint datLen = shortBlockDataLen + (i < numShortBlocks ? 0 : 1);\n\t\t\trs.getRemainder(data, k, datLen, ecc);\n\t\t\tfor (int j = 0, l = i; j < datLen; j++, k++, l += numBlocks) {  // Copy data\n\t\t\t\tif (j == shortBlockDataLen)\n\t\t\t\t\tl -= numShortBlocks;\n\t\t\t\tresult[l] = data[k];\n\t\t\t}\n\t\t\tfor (int j = 0, l = data.length + i; j < blockEccLen; j++, l += numBlocks)  // Copy ECC\n\t\t\t\tresult[l] = ecc[j];\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Draws the given sequence of 8-bit codewords (data and error correction)\n\t// onto the entire data area of this QR Code, based on the given bit indexes.\n\tprivate void drawCodewords(int[] dataOutputBitIndexes, byte[] allCodewords) {\n\t\tObjects.requireNonNull(dataOutputBitIndexes);\n\t\tObjects.requireNonNull(allCodewords);\n\t\tif (allCodewords.length * 8 != dataOutputBitIndexes.length)\n\t\t\tthrow new IllegalArgumentException();\n\t\tfor (int i = 0; i < dataOutputBitIndexes.length; i++) {\n\t\t\tint j = dataOutputBitIndexes[i];\n\t\t\tint bit = getBit(allCodewords[i >>> 3], ~i & 7);\n\t\t\tmodules[j >>> 5] |= bit << j;\n\t\t}\n\t}\n\t\n\t\n\t// XORs the codeword modules in this QR Code with the given mask pattern.\n\t// The function modules must be marked and the codeword bits must be drawn\n\t// before masking. Due to the arithmetic of XOR, calling applyMask() with\n\t// the same mask value a second time will undo the mask. A final well-formed\n\t// QR Code needs exactly one (not zero, two, etc.) mask applied.\n\tprivate void applyMask(int[] msk) {\n\t\tif (msk.length != modules.length)\n\t\t\tthrow new IllegalArgumentException();\n\t\tfor (int i = 0; i < msk.length; i++)\n\t\t\tmodules[i] ^= msk[i];\n\t}\n\t\n\t\n\t// A messy helper function for the constructor. This QR Code must be in an unmasked state when this\n\t// method is called. The 'mask' argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.\n\t// This method applies and returns the actual mask chosen, from 0 to 7.\n\tprivate int handleConstructorMasking(int[][] masks, int msk) {\n\t\tif (msk == -1) {  // Automatically choose best mask\n\t\t\tint minPenalty = Integer.MAX_VALUE;\n\t\t\tfor (int i = 0; i < 8; i++) {\n\t\t\t\tapplyMask(masks[i]);\n\t\t\t\tdrawFormatBits(i);\n\t\t\t\tint penalty = getPenaltyScore();\n\t\t\t\tif (penalty < minPenalty) {\n\t\t\t\t\tmsk = i;\n\t\t\t\t\tminPenalty = penalty;\n\t\t\t\t}\n\t\t\t\tapplyMask(masks[i]);  // Undoes the mask due to XOR\n\t\t\t}\n\t\t}\n\t\tassert 0 <= msk && msk <= 7;\n\t\tapplyMask(masks[msk]);  // Apply the final choice of mask\n\t\tdrawFormatBits(msk);  // Overwrite old format bits\n\t\treturn msk;  // The caller shall assign this value to the final-declared field\n\t}\n\t\n\t\n\t// Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\n\tprivate int getPenaltyScore() {\n\t\tint result = 0;\n\t\tint dark = 0;\n\t\tint[] runHistory = new int[7];\n\t\t\n\t\t// Iterate over adjacent pairs of rows\n\t\tfor (int index = 0, downIndex = size, end = size * size; index < end; ) {\n\t\t\tint runColor = 0;\n\t\t\tint runX = 0;\n\t\t\tArrays.fill(runHistory, 0);\n\t\t\tint curRow = 0;\n\t\t\tint nextRow = 0;\n\t\t\tfor (int x = 0; x < size; x++, index++, downIndex++) {\n\t\t\t\tint c = getBit(modules[index >>> 5], index);\n\t\t\t\tif (c == runColor) {\n\t\t\t\t\trunX++;\n\t\t\t\t\tif (runX == 5)\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\telse if (runX > 5)\n\t\t\t\t\t\tresult++;\n\t\t\t\t} else {\n\t\t\t\t\tfinderPenaltyAddHistory(runX, runHistory);\n\t\t\t\t\tif (runColor == 0)\n\t\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;\n\t\t\t\t\trunColor = c;\n\t\t\t\t\trunX = 1;\n\t\t\t\t}\n\t\t\t\tdark += c;\n\t\t\t\tif (downIndex < end) {\n\t\t\t\t\tcurRow = ((curRow << 1) | c) & 3;\n\t\t\t\t\tnextRow = ((nextRow << 1) | getBit(modules[downIndex >>> 5], downIndex)) & 3;\n\t\t\t\t\t// 2*2 blocks of modules having same color\n\t\t\t\t\tif (x >= 1 && (curRow == 0 || curRow == 3) && curRow == nextRow)\n\t\t\t\t\t\tresult += PENALTY_N2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;\n\t\t}\n\t\t\n\t\t// Iterate over single columns\n\t\tfor (int x = 0; x < size; x++) {\n\t\t\tint runColor = 0;\n\t\t\tint runY = 0;\n\t\t\tArrays.fill(runHistory, 0);\n\t\t\tfor (int y = 0, index = x; y < size; y++, index += size) {\n\t\t\t\tint c = getBit(modules[index >>> 5], index);\n\t\t\t\tif (c == runColor) {\n\t\t\t\t\trunY++;\n\t\t\t\t\tif (runY == 5)\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\telse if (runY > 5)\n\t\t\t\t\t\tresult++;\n\t\t\t\t} else {\n\t\t\t\t\tfinderPenaltyAddHistory(runY, runHistory);\n\t\t\t\t\tif (runColor == 0)\n\t\t\t\t\t\tresult += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;\n\t\t\t\t\trunColor = c;\n\t\t\t\t\trunY = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;\n\t\t}\n\t\t\n\t\t// Balance of dark and light modules\n\t\tint total = size * size;  // Note that size is odd, so dark/total != 1/2\n\t\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\t\tint k = (Math.abs(dark * 20 - total * 10) + total - 1) / total - 1;\n\t\tresult += k * PENALTY_N4;\n\t\treturn result;\n\t}\n\t\n\t\n\t\n\t/*---- Private helper functions ----*/\n\t\n\t// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t// QR Code of the given version number and error correction level, with remainder bits discarded.\n\t// This stateless pure function could be implemented as a (40*4)-cell lookup table.\n\tstatic int getNumDataCodewords(int ver, Ecc ecl) {\n\t\treturn QrTemplate.getNumRawDataModules(ver) / 8\n\t\t\t- ECC_CODEWORDS_PER_BLOCK    [ecl.ordinal()][ver]\n\t\t\t* NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver];\n\t}\n\t\n\t\n\t// Can only be called immediately after a light run is added, and\n\t// returns either 0, 1, or 2. A helper function for getPenaltyScore().\n\tprivate int finderPenaltyCountPatterns(int[] runHistory) {\n\t\tint n = runHistory[1];\n\t\tassert n <= size * 3;\n\t\tboolean core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;\n\t\treturn (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)\n\t\t     + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);\n\t}\n\t\n\t\n\t// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().\n\tprivate int finderPenaltyTerminateAndCount(int currentRunColor, int currentRunLength, int[] runHistory) {\n\t\tif (currentRunColor == 1) {  // Terminate dark run\n\t\t\tfinderPenaltyAddHistory(currentRunLength, runHistory);\n\t\t\tcurrentRunLength = 0;\n\t\t}\n\t\tcurrentRunLength += size;  // Add light border to final run\n\t\tfinderPenaltyAddHistory(currentRunLength, runHistory);\n\t\treturn finderPenaltyCountPatterns(runHistory);\n\t}\n\t\n\t\n\t// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().\n\tprivate void finderPenaltyAddHistory(int currentRunLength, int[] runHistory) {\n\t\tif (runHistory[0] == 0)\n\t\t\tcurrentRunLength += size;  // Add light border to initial run\n\t\tSystem.arraycopy(runHistory, 0, runHistory, 1, runHistory.length - 1);\n\t\trunHistory[0] = currentRunLength;\n\t}\n\t\n\t\n\t// Returns 0 or 1 based on the (i mod 32)'th bit of x.\n\tstatic int getBit(int x, int i) {\n\t\treturn (x >>> i) & 1;\n\t}\n\t\n\t\n\t/*---- Constants and tables ----*/\n\t\n\t/** The minimum version number  (1) supported in the QR Code Model 2 standard. */\n\tpublic static final int MIN_VERSION =  1;\n\t\n\t/** The maximum version number (40) supported in the QR Code Model 2 standard. */\n\tpublic static final int MAX_VERSION = 40;\n\t\n\t\n\t// For use in getPenaltyScore(), when evaluating which mask is best.\n\tprivate static final int PENALTY_N1 =  3;\n\tprivate static final int PENALTY_N2 =  3;\n\tprivate static final int PENALTY_N3 = 40;\n\tprivate static final int PENALTY_N4 = 10;\n\t\n\t\n\tprivate static final byte[][] ECC_CODEWORDS_PER_BLOCK = {\n\t\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t//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\n\t\t{-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\n\t\t{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},  // Medium\n\t\t{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // Quartile\n\t\t{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},  // High\n\t};\n\t\n\tprivate static final byte[][] NUM_ERROR_CORRECTION_BLOCKS = {\n\t\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t//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\n\t\t{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25},  // Low\n\t\t{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49},  // Medium\n\t\t{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68},  // Quartile\n\t\t{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81},  // High\n\t};\n\t\n\t\n\t\n\t/*---- Public helper enumeration ----*/\n\t\n\t/**\n\t * The error correction level in a QR Code symbol.\n\t */\n\tpublic enum Ecc {\n\t\t// Must be declared in ascending order of error protection\n\t\t// so that the implicit ordinal() and values() work properly\n\t\t/** The QR Code can tolerate about  7% erroneous codewords. */ LOW(1),\n\t\t/** The QR Code can tolerate about 15% erroneous codewords. */ MEDIUM(0),\n\t\t/** The QR Code can tolerate about 25% erroneous codewords. */ QUARTILE(3),\n\t\t/** The QR Code can tolerate about 30% erroneous codewords. */ HIGH(2);\n\t\t\n\t\t// In the range 0 to 3 (unsigned 2-bit integer).\n\t\tfinal int formatBits;\n\t\t\n\t\t// Constructor.\n\t\tprivate Ecc(int fb) {\n\t\t\tformatBits = fb;\n\t\t}\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/QrCodeGeneratorDemo.java",
    "content": "/* \n * Fast QR Code generator demo\n * \n * Run this command-line program with no arguments. The program creates/overwrites a bunch of\n * PNG and SVG files in the current working directory to demonstrate the creation of QR Codes.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.awt.image.BufferedImage;\nimport java.io.File;\nimport java.io.IOException;\nimport java.nio.charset.StandardCharsets;\nimport java.nio.file.Files;\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Objects;\nimport javax.imageio.ImageIO;\n\n\npublic final class QrCodeGeneratorDemo {\n\t\n\t// The main application program.\n\tpublic static void main(String[] args) throws IOException {\n\t\tdoBasicDemo();\n\t\tdoVarietyDemo();\n\t\tdoSegmentDemo();\n\t\tdoMaskDemo();\n\t}\n\t\n\t\n\t\n\t/*---- Demo suite ----*/\n\t\n\t// Creates a single QR Code, then writes it to a PNG file and an SVG file.\n\tprivate static void doBasicDemo() throws IOException {\n\t\tString text = \"Hello, world!\";          // User-supplied Unicode text\n\t\tQrCode.Ecc errCorLvl = QrCode.Ecc.LOW;  // Error correction level\n\t\t\n\t\tQrCode qr = QrCode.encodeText(text, errCorLvl);  // Make the QR Code symbol\n\t\t\n\t\tBufferedImage img = toImage(qr, 10, 4);          // Convert to bitmap image\n\t\tFile imgFile = new File(\"hello-world-QR.png\");   // File path for output\n\t\tImageIO.write(img, \"png\", imgFile);              // Write image to file\n\t\t\n\t\tString svg = toSvgString(qr, 4, \"#FFFFFF\", \"#000000\");  // Convert to SVG XML code\n\t\tFile svgFile = new File(\"hello-world-QR.svg\");          // File path for output\n\t\tFiles.write(svgFile.toPath(),                           // Write image to file\n\t\t\tsvg.getBytes(StandardCharsets.UTF_8));\n\t}\n\t\n\t\n\t// Creates a variety of QR Codes that exercise different features of the library, and writes each one to file.\n\tprivate static void doVarietyDemo() throws IOException {\n\t\tQrCode qr;\n\t\t\n\t\t// Numeric mode encoding (3.33 bits per digit)\n\t\tqr = QrCode.encodeText(\"314159265358979323846264338327950288419716939937510\", QrCode.Ecc.MEDIUM);\n\t\twritePng(toImage(qr, 13, 1), \"pi-digits-QR.png\");\n\t\t\n\t\t// Alphanumeric mode encoding (5.5 bits per character)\n\t\tqr = QrCode.encodeText(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", QrCode.Ecc.HIGH);\n\t\twritePng(toImage(qr, 10, 2), \"alphanumeric-QR.png\");\n\t\t\n\t\t// Unicode text as UTF-8\n\t\tqr = QrCode.encodeText(\"こんにちwa、世界！ αβγδ\", QrCode.Ecc.QUARTILE);\n\t\twritePng(toImage(qr, 10, 3), \"unicode-QR.png\");\n\t\t\n\t\t// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\t\tqr = QrCode.encodeText(\n\t\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \"\n\t\t\t+ \"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \"\n\t\t\t+ \"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \"\n\t\t\t+ \"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \"\n\t\t\t+ \"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \"\n\t\t\t+ \"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \"\n\t\t\t+ \"a White Rabbit with pink eyes ran close by her.\", QrCode.Ecc.HIGH);\n\t\twritePng(toImage(qr, 6, 10), \"alice-wonderland-QR.png\");\n\t}\n\t\n\t\n\t// Creates QR Codes with manually specified segments for better compactness.\n\tprivate static void doSegmentDemo() throws IOException {\n\t\tQrCode qr;\n\t\tList<QrSegment> segs;\n\t\t\n\t\t// Illustration \"silver\"\n\t\tString silver0 = \"THE SQUARE ROOT OF 2 IS 1.\";\n\t\tString silver1 = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\t\tqr = QrCode.encodeText(silver0 + silver1, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 10, 3), \"sqrt2-monolithic-QR.png\");\n\t\t\n\t\tsegs = Arrays.asList(\n\t\t\tQrSegment.makeAlphanumeric(silver0),\n\t\t\tQrSegment.makeNumeric(silver1));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 10, 3), \"sqrt2-segmented-QR.png\");\n\t\t\n\t\t// Illustration \"golden\"\n\t\tString golden0 = \"Golden ratio φ = 1.\";\n\t\tString golden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\t\tString golden2 = \"......\";\n\t\tqr = QrCode.encodeText(golden0 + golden1 + golden2, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 8, 5), \"phi-monolithic-QR.png\");\n\t\t\n\t\tsegs = Arrays.asList(\n\t\t\tQrSegment.makeBytes(golden0.getBytes(StandardCharsets.UTF_8)),\n\t\t\tQrSegment.makeNumeric(golden1),\n\t\t\tQrSegment.makeAlphanumeric(golden2));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 8, 5), \"phi-segmented-QR.png\");\n\t\t\n\t\t// Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\t\tString madoka = \"「魔法少女まどか☆マギカ」って、　ИАИ　ｄｅｓｕ　κα？\";\n\t\tqr = QrCode.encodeText(madoka, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 9, 4, 0xFFFFE0, 0x303080), \"madoka-utf8-QR.png\");\n\t\t\n\t\tsegs = Arrays.asList(QrSegmentAdvanced.makeKanji(madoka));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\twritePng(toImage(qr, 9, 4, 0xE0F0FF, 0x404040), \"madoka-kanji-QR.png\");\n\t}\n\t\n\t\n\t// Creates QR Codes with the same size and contents but different mask patterns.\n\tprivate static void doMaskDemo() throws IOException {\n\t\tQrCode qr;\n\t\tList<QrSegment> segs;\n\t\t\n\t\t// Project Nayuki URL\n\t\tsegs = QrSegment.makeSegments(\"https://www.nayuki.io/\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true);  // Automatic mask\n\t\twritePng(toImage(qr, 8, 6, 0xE0FFE0, 0x206020), \"project-nayuki-automask-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true);  // Force mask 3\n\t\twritePng(toImage(qr, 8, 6, 0xFFE0E0, 0x602020), \"project-nayuki-mask3-QR.png\");\n\t\t\n\t\t// Chinese text as UTF-8\n\t\tsegs = QrSegment.makeSegments(\"維基百科（Wikipedia，聆聽i/ˌwɪkᵻˈpiːdi.ə/）是一個自由內容、公開編輯且多語言的網路百科全書協作計畫\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 0, true);  // Force mask 0\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask0-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 1, true);  // Force mask 1\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask1-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 5, true);  // Force mask 5\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask5-QR.png\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 7, true);  // Force mask 7\n\t\twritePng(toImage(qr, 10, 3), \"unicode-mask7-QR.png\");\n\t}\n\t\n\t\n\t\n\t/*---- Utilities ----*/\n\t\n\tprivate static BufferedImage toImage(QrCode qr, int scale, int border) {\n\t\treturn toImage(qr, scale, border, 0xFFFFFF, 0x000000);\n\t}\n\t\n\t\n\t/**\n\t * Returns a raster image depicting the specified QR Code, with\n\t * the specified module scale, border modules, and module colors.\n\t * <p>For example, scale=10 and border=4 means to pad the QR Code with 4 light border\n\t * modules on all four sides, and use 10&#xD7;10 pixels to represent each module.\n\t * @param qr the QR Code to render (not {@code null})\n\t * @param scale the side length (measured in pixels, must be positive) of each module\n\t * @param border the number of border modules to add, which must be non-negative\n\t * @param lightColor the color to use for light modules, in 0xRRGGBB format\n\t * @param darkColor the color to use for dark modules, in 0xRRGGBB format\n\t * @return a new image representing the QR Code, with padding and scaling\n\t * @throws NullPointerException if the QR Code is {@code null}\n\t * @throws IllegalArgumentException if the scale or border is out of range, or if\n\t * {scale, border, size} cause the image dimensions to exceed Integer.MAX_VALUE\n\t */\n\tprivate static BufferedImage toImage(QrCode qr, int scale, int border, int lightColor, int darkColor) {\n\t\tObjects.requireNonNull(qr);\n\t\tif (scale <= 0 || border < 0)\n\t\t\tthrow new IllegalArgumentException(\"Value out of range\");\n\t\tif (border > Integer.MAX_VALUE / 2 || qr.size + border * 2L > Integer.MAX_VALUE / scale)\n\t\t\tthrow new IllegalArgumentException(\"Scale or border too large\");\n\t\t\n\t\tBufferedImage result = new BufferedImage((qr.size + border * 2) * scale, (qr.size + border * 2) * scale, BufferedImage.TYPE_INT_RGB);\n\t\tfor (int y = 0; y < result.getHeight(); y++) {\n\t\t\tfor (int x = 0; x < result.getWidth(); x++) {\n\t\t\t\tboolean color = qr.getModule(x / scale - border, y / scale - border);\n\t\t\t\tresult.setRGB(x, y, color ? darkColor : lightColor);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Helper function to reduce code duplication.\n\tprivate static void writePng(BufferedImage img, String filepath) throws IOException {\n\t\tImageIO.write(img, \"png\", new File(filepath));\n\t}\n\t\n\t\n\t/**\n\t * Returns a string of SVG code for an image depicting the specified QR Code, with the specified\n\t * number of border modules. The string always uses Unix newlines (\\n), regardless of the platform.\n\t * @param qr the QR Code to render (not {@code null})\n\t * @param border the number of border modules to add, which must be non-negative\n\t * @param lightColor the color to use for light modules, in any format supported by CSS, not {@code null}\n\t * @param darkColor the color to use for dark modules, in any format supported by CSS, not {@code null}\n\t * @return a string representing the QR Code as an SVG XML document\n\t * @throws NullPointerException if any object is {@code null}\n\t * @throws IllegalArgumentException if the border is negative\n\t */\n\tprivate static String toSvgString(QrCode qr, int border, String lightColor, String darkColor) {\n\t\tObjects.requireNonNull(qr);\n\t\tObjects.requireNonNull(lightColor);\n\t\tObjects.requireNonNull(darkColor);\n\t\tif (border < 0)\n\t\t\tthrow new IllegalArgumentException(\"Border must be non-negative\");\n\t\tlong brd = border;\n\t\tStringBuilder sb = new StringBuilder()\n\t\t\t.append(\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\")\n\t\t\t.append(\"<!DOCTYPE svg PUBLIC \\\"-//W3C//DTD SVG 1.1//EN\\\" \\\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\\">\\n\")\n\t\t\t.append(String.format(\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" version=\\\"1.1\\\" viewBox=\\\"0 0 %1$d %1$d\\\" stroke=\\\"none\\\">\\n\",\n\t\t\t\tqr.size + brd * 2))\n\t\t\t.append(\"\\t<rect width=\\\"100%\\\" height=\\\"100%\\\" fill=\\\"\" + lightColor + \"\\\"/>\\n\")\n\t\t\t.append(\"\\t<path d=\\\"\");\n\t\tfor (int y = 0; y < qr.size; y++) {\n\t\t\tfor (int x = 0; x < qr.size; x++) {\n\t\t\t\tif (qr.getModule(x, y)) {\n\t\t\t\t\tif (x != 0 || y != 0)\n\t\t\t\t\t\tsb.append(\" \");\n\t\t\t\t\tsb.append(String.format(\"M%d,%dh1v1h-1z\", x + brd, y + brd));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn sb\n\t\t\t.append(\"\\\" fill=\\\"\" + darkColor + \"\\\"/>\\n\")\n\t\t\t.append(\"</svg>\\n\")\n\t\t\t.toString();\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/QrSegment.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.nio.charset.StandardCharsets;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Objects;\n\n\n/**\n * A segment of character/binary/control data in a QR Code symbol.\n * Instances of this class are immutable.\n * <p>The mid-level way to create a segment is to take the payload data and call a\n * static factory function such as {@link QrSegment#makeNumeric(String)}. The low-level\n * way to create a segment is to custom-make the bit buffer and call the {@link\n * QrSegment#QrSegment(Mode,int,int[],int) constructor} with appropriate values.</p>\n * <p>This segment class imposes no length restrictions, but QR Codes have restrictions.\n * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n * Any segment longer than this is meaningless for the purpose of generating QR Codes.\n * This class can represent kanji mode segments, but provides no help in encoding them\n * - see {@link QrSegmentAdvanced} for full kanji support.</p>\n */\npublic final class QrSegment {\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/**\n\t * Returns a segment representing the specified binary data\n\t * encoded in byte mode. All input byte arrays are acceptable.\n\t * <p>Any text string can be converted to UTF-8 bytes ({@code\n\t * s.getBytes(StandardCharsets.UTF_8)}) and encoded as a byte mode segment.</p>\n\t * @param data the binary data (not {@code null})\n\t * @return a segment (not {@code null}) containing the data\n\t * @throws NullPointerException if the array is {@code null}\n\t */\n\tpublic static QrSegment makeBytes(byte[] data) {\n\t\tObjects.requireNonNull(data);\n\t\tif (data.length * 8L > Integer.MAX_VALUE)\n\t\t\tthrow new IllegalArgumentException(\"Data too long\");\n\t\tint[] bits = new int[(data.length + 3) / 4];\n\t\tfor (int i = 0; i < data.length; i++)\n\t\t\tbits[i >>> 2] |= (data[i] & 0xFF) << (~i << 3);\n\t\treturn new QrSegment(Mode.BYTE, data.length, bits, data.length * 8);\n\t}\n\t\n\t\n\t/**\n\t * Returns a segment representing the specified string of decimal digits encoded in numeric mode.\n\t * @param digits the text (not {@code null}), with only digits from 0 to 9 allowed\n\t * @return a segment (not {@code null}) containing the text\n\t * @throws NullPointerException if the string is {@code null}\n\t * @throws IllegalArgumentException if the string contains non-digit characters\n\t */\n\tpublic static QrSegment makeNumeric(String digits) {\n\t\tObjects.requireNonNull(digits);\n\t\tBitBuffer bb = new BitBuffer();\n\t\tint accumData = 0;\n\t\tint accumCount = 0;\n\t\tfor (int i = 0; i < digits.length(); i++) {\n\t\t\tchar c = digits.charAt(i);\n\t\t\tif (c < '0' || c > '9')\n\t\t\t\tthrow new IllegalArgumentException(\"String contains non-numeric characters\");\n\t\t\taccumData = accumData * 10 + (c - '0');\n\t\t\taccumCount++;\n\t\t\tif (accumCount == 3) {\n\t\t\t\tbb.appendBits(accumData, 10);\n\t\t\t\taccumData = 0;\n\t\t\t\taccumCount = 0;\n\t\t\t}\n\t\t}\n\t\tif (accumCount > 0)  // 1 or 2 digits remaining\n\t\t\tbb.appendBits(accumData, accumCount * 3 + 1);\n\t\treturn new QrSegment(Mode.NUMERIC, digits.length(), bb.data, bb.bitLength);\n\t}\n\t\n\t\n\t/**\n\t * Returns a segment representing the specified text string encoded in alphanumeric mode.\n\t * The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t * dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t * @param text the text (not {@code null}), with only certain characters allowed\n\t * @return a segment (not {@code null}) containing the text\n\t * @throws NullPointerException if the string is {@code null}\n\t * @throws IllegalArgumentException if the string contains non-encodable characters\n\t */\n\tpublic static QrSegment makeAlphanumeric(String text) {\n\t\tObjects.requireNonNull(text);\n\t\tBitBuffer bb = new BitBuffer();\n\t\tint accumData = 0;\n\t\tint accumCount = 0;\n\t\tfor (int i = 0; i < text.length(); i++) {\n\t\t\tchar c = text.charAt(i);\n\t\t\tif (c >= ALPHANUMERIC_MAP.length || ALPHANUMERIC_MAP[c] == -1)\n\t\t\t\tthrow new IllegalArgumentException(\"String contains unencodable characters in alphanumeric mode\");\n\t\t\taccumData = accumData * 45 + ALPHANUMERIC_MAP[c];\n\t\t\taccumCount++;\n\t\t\tif (accumCount == 2) {\n\t\t\t\tbb.appendBits(accumData, 11);\n\t\t\t\taccumData = 0;\n\t\t\t\taccumCount = 0;\n\t\t\t}\n\t\t}\n\t\tif (accumCount > 0)  // 1 character remaining\n\t\t\tbb.appendBits(accumData, 6);\n\t\treturn new QrSegment(Mode.ALPHANUMERIC, text.length(), bb.data, bb.bitLength);\n\t}\n\t\n\t\n\t/**\n\t * Returns a list of zero or more segments to represent the specified Unicode text string.\n\t * The result may use various segment modes and switch modes to optimize the length of the bit stream.\n\t * @param text the text to be encoded, which can be any Unicode string\n\t * @return a new mutable list (not {@code null}) of segments (not {@code null}) containing the text\n\t * @throws NullPointerException if the text is {@code null}\n\t */\n\tpublic static List<QrSegment> makeSegments(String text) {\n\t\tObjects.requireNonNull(text);\n\t\t\n\t\t// Select the most efficient segment encoding automatically\n\t\tList<QrSegment> result = new ArrayList<>();\n\t\tif (text.equals(\"\"));  // Leave result empty\n\t\telse if (isNumeric(text))\n\t\t\tresult.add(makeNumeric(text));\n\t\telse if (isAlphanumeric(text))\n\t\t\tresult.add(makeAlphanumeric(text));\n\t\telse\n\t\t\tresult.add(makeBytes(text.getBytes(StandardCharsets.UTF_8)));\n\t\treturn result;\n\t}\n\t\n\t\n\t/**\n\t * Returns a segment representing an Extended Channel Interpretation\n\t * (ECI) designator with the specified assignment value.\n\t * @param assignVal the ECI assignment number (see the AIM ECI specification)\n\t * @return a segment (not {@code null}) containing the data\n\t * @throws IllegalArgumentException if the value is outside the range [0, 10<sup>6</sup>)\n\t */\n\tpublic static QrSegment makeEci(int assignVal) {\n\t\tBitBuffer bb = new BitBuffer();\n\t\tif (assignVal < 0)\n\t\t\tthrow new IllegalArgumentException(\"ECI assignment value out of range\");\n\t\telse if (assignVal < (1 << 7))\n\t\t\tbb.appendBits(assignVal, 8);\n\t\telse if (assignVal < (1 << 14)) {\n\t\t\tbb.appendBits(2, 2);\n\t\t\tbb.appendBits(assignVal, 14);\n\t\t} else if (assignVal < 1_000_000) {\n\t\t\tbb.appendBits(6, 3);\n\t\t\tbb.appendBits(assignVal, 21);\n\t\t} else\n\t\t\tthrow new IllegalArgumentException(\"ECI assignment value out of range\");\n\t\treturn new QrSegment(Mode.ECI, 0, bb.data, bb.bitLength);\n\t}\n\t\n\t\n\t/**\n\t * Tests whether the specified string can be encoded as a segment in numeric mode.\n\t * A string is encodable iff each character is in the range 0 to 9.\n\t * @param text the string to test for encodability (not {@code null})\n\t * @return {@code true} iff each character is in the range 0 to 9.\n\t * @throws NullPointerException if the string is {@code null}\n\t * @see #makeNumeric(String)\n\t */\n\tpublic static boolean isNumeric(String text) {\n\t\tfor (int i = 0; i < text.length(); i++) {\n\t\t\tchar c = text.charAt(i);\n\t\t\tif (c < '0' || c > '9')\n\t\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\t\n\t\n\t/**\n\t * Tests whether the specified string can be encoded as a segment in alphanumeric mode.\n\t * A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t * @param text the string to test for encodability (not {@code null})\n\t * @return {@code true} iff each character is in the alphanumeric mode character set\n\t * @throws NullPointerException if the string is {@code null}\n\t * @see #makeAlphanumeric(String)\n\t */\n\tpublic static boolean isAlphanumeric(String text) {\n\t\tfor (int i = 0; i < text.length(); i++) {\n\t\t\tchar c = text.charAt(i);\n\t\t\tif (c >= ALPHANUMERIC_MAP.length || ALPHANUMERIC_MAP[c] == -1)\n\t\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\t\n\t\n\t\n\t/*---- Instance fields ----*/\n\t\n\t/** The mode indicator of this segment. Not {@code null}. */\n\tpublic final Mode mode;\n\t\n\t/** The length of this segment's unencoded data. Measured in characters for\n\t * numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t * Always zero or positive. Not the same as the data's bit length. */\n\tpublic final int numChars;\n\t\n\t// The data bits of this segment. Not null.\n\tfinal int[] data;\n\t\n\t// Requires 0 <= bitLength <= data.length * 32.\n\tfinal int bitLength;\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/**\n\t * Constructs a QR Code segment with the specified attributes and data.\n\t * The character count (numCh) must agree with the mode and the bit buffer length,\n\t * but the constraint isn't checked. The specified bit buffer is cloned and stored.\n\t * @param md the mode (not {@code null})\n\t * @param numCh the data length in characters or bytes, which is non-negative\n\t * @param data the data bits (not {@code null})\n\t * @param bitLen the number of valid prefix bits in the data array\n\t * @throws NullPointerException if the mode or data is {@code null}\n\t * @throws IllegalArgumentException if the character count is negative\n\t */\n\tpublic QrSegment(Mode md, int numCh, int[] data, int bitLen) {\n\t\tmode = Objects.requireNonNull(md);\n\t\tthis.data = Objects.requireNonNull(data);\n\t\tif (numCh < 0 || bitLen < 0 || bitLen > data.length * 32L)\n\t\t\tthrow new IllegalArgumentException(\"Invalid value\");\n\t\tnumChars = numCh;\n\t\tbitLength = bitLen;\n\t}\n\t\n\t\n\t// Calculates the number of bits needed to encode the given segments at the given version.\n\t// Returns a non-negative number if successful. Otherwise returns -1 if a segment has too\n\t// many characters to fit its length field, or the total bits exceeds Integer.MAX_VALUE.\n\tstatic int getTotalBits(List<QrSegment> segs, int version) {\n\t\tObjects.requireNonNull(segs);\n\t\tlong result = 0;\n\t\tfor (QrSegment seg : segs) {\n\t\t\tObjects.requireNonNull(seg);\n\t\t\tint ccbits = seg.mode.numCharCountBits(version);\n\t\t\tif (seg.numChars >= (1 << ccbits))\n\t\t\t\treturn -1;  // The segment's length doesn't fit the field's bit width\n\t\t\tresult += 4L + ccbits + seg.bitLength;\n\t\t\tif (result > Integer.MAX_VALUE)\n\t\t\t\treturn -1;  // The sum will overflow an int type\n\t\t}\n\t\treturn (int)result;\n\t}\n\t\n\t\n\t/*---- Constants ----*/\n\t\n\tstatic final int[] ALPHANUMERIC_MAP;\n\t\n\tstatic {\n\t\tfinal String ALPHANUMERIC_CHARSET = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\t\tint maxCh = -1;\n\t\tfor (int i = 0; i < ALPHANUMERIC_CHARSET.length(); i++)\n\t\t\tmaxCh = Math.max(ALPHANUMERIC_CHARSET.charAt(i), maxCh);\n\t\tALPHANUMERIC_MAP = new int[maxCh + 1];\n\t\tArrays.fill(ALPHANUMERIC_MAP, -1);\n\t\tfor (int i = 0; i < ALPHANUMERIC_CHARSET.length(); i++)\n\t\t\tALPHANUMERIC_MAP[ALPHANUMERIC_CHARSET.charAt(i)] = i;\n\t}\n\t\n\t\n\t\n\t/*---- Public helper enumeration ----*/\n\t\n\t/**\n\t * Describes how a segment's data bits are interpreted.\n\t */\n\tpublic enum Mode {\n\t\t\n\t\t/*-- Constants --*/\n\t\t\n\t\tNUMERIC     (0x1, 10, 12, 14),\n\t\tALPHANUMERIC(0x2,  9, 11, 13),\n\t\tBYTE        (0x4,  8, 16, 16),\n\t\tKANJI       (0x8,  8, 10, 12),\n\t\tECI         (0x7,  0,  0,  0);\n\t\t\n\t\t\n\t\t/*-- Fields --*/\n\t\t\n\t\t// The mode indicator bits, which is a uint4 value (range 0 to 15).\n\t\tfinal int modeBits;\n\t\t\n\t\t// Number of character count bits for three different version ranges.\n\t\tprivate final int[] numBitsCharCount;\n\t\t\n\t\t\n\t\t/*-- Constructor --*/\n\t\t\n\t\tprivate Mode(int mode, int... ccbits) {\n\t\t\tmodeBits = mode;\n\t\t\tnumBitsCharCount = ccbits;\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Method --*/\n\t\t\n\t\t// Returns the bit width of the character count field for a segment in this mode\n\t\t// in a QR Code at the given version number. The result is in the range [0, 16].\n\t\tint numCharCountBits(int ver) {\n\t\t\tassert QrCode.MIN_VERSION <= ver && ver <= QrCode.MAX_VERSION;\n\t\t\treturn numBitsCharCount[(ver + 7) / 17];\n\t\t}\n\t\t\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/QrSegmentAdvanced.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.nio.charset.StandardCharsets;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.Base64;\nimport java.util.List;\nimport java.util.Objects;\nimport io.nayuki.fastqrcodegen.QrSegment.Mode;\n\n\n/**\n * Splits text into optimal segments and encodes kanji segments.\n * Provides static functions only; not instantiable.\n * @see QrSegment\n * @see QrCode\n */\npublic final class QrSegmentAdvanced {\n\t\n\t/*---- Optimal list of segments encoder ----*/\n\t\n\t/**\n\t * Returns a list of zero or more segments to represent the specified Unicode text string.\n\t * The resulting list optimally minimizes the total encoded bit length, subjected to the constraints\n\t * in the specified {error correction level, minimum version number, maximum version number}.\n\t * <p>This function can utilize all four text encoding modes: numeric, alphanumeric, byte (UTF-8),\n\t * and kanji. This can be considered as a sophisticated but slower replacement for {@link\n\t * QrSegment#makeSegments(String)}. This requires more input parameters because it searches a\n\t * range of versions, like {@link QrCode#encodeSegments(List,QrCode.Ecc,int,int,int,boolean)}.</p>\n\t * @param text the text to be encoded (not {@code null}), which can be any Unicode string\n\t * @param ecl the error correction level to use (not {@code null})\n\t * @param minVersion the minimum allowed version of the QR Code (at least 1)\n\t * @param maxVersion the maximum allowed version of the QR Code (at most 40)\n\t * @return a new mutable list (not {@code null}) of segments (not {@code null})\n\t * containing the text, minimizing the bit length with respect to the constraints\n\t * @throws NullPointerException if the text or error correction level is {@code null}\n\t * @throws IllegalArgumentException if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40 is violated\n\t * @throws DataTooLongException if the text fails to fit in the maxVersion QR Code at the ECL\n\t */\n\tpublic static List<QrSegment> makeSegmentsOptimally(String text, QrCode.Ecc ecl, int minVersion, int maxVersion) {\n\t\t// Check arguments\n\t\tObjects.requireNonNull(text);\n\t\tObjects.requireNonNull(ecl);\n\t\tif (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION))\n\t\t\tthrow new IllegalArgumentException(\"Invalid value\");\n\t\t\n\t\t// Iterate through version numbers, and make tentative segments\n\t\tList<QrSegment> segs = null;\n\t\tint[] codePoints = toCodePoints(text);\n\t\tfor (int version = minVersion; ; version++) {\n\t\t\tif (version == minVersion || version == 10 || version == 27)\n\t\t\t\tsegs = makeSegmentsOptimally(codePoints, version);\n\t\t\tassert segs != null;\n\t\t\t\n\t\t\t// Check if the segments fit\n\t\t\tint dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\t\tint dataUsedBits = QrSegment.getTotalBits(segs, version);\n\t\t\tif (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)\n\t\t\t\treturn segs;  // This version number is found to be suitable\n\t\t\tif (version >= maxVersion) {  // All versions in the range could not fit the given text\n\t\t\t\tString msg = \"Segment too long\";\n\t\t\t\tif (dataUsedBits != -1)\n\t\t\t\t\tmsg = String.format(\"Data length = %d bits, Max capacity = %d bits\", dataUsedBits, dataCapacityBits);\n\t\t\t\tthrow new DataTooLongException(msg);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Returns a new list of segments that is optimal for the given text at the given version number.\n\tprivate static List<QrSegment> makeSegmentsOptimally(int[] codePoints, int version) {\n\t\tif (codePoints.length == 0)\n\t\t\treturn new ArrayList<>();\n\t\tMode[] charModes = computeCharacterModes(codePoints, version);\n\t\treturn splitIntoSegments(codePoints, charModes);\n\t}\n\t\n\t\n\t// Returns a new array representing the optimal mode per code point based on the given text and version.\n\tprivate static Mode[] computeCharacterModes(int[] codePoints, int version) {\n\t\tif (codePoints.length == 0)\n\t\t\tthrow new IllegalArgumentException();\n\t\tfinal Mode[] modeTypes = {Mode.BYTE, Mode.ALPHANUMERIC, Mode.NUMERIC, Mode.KANJI};  // Do not modify\n\t\tfinal int numModes = modeTypes.length;\n\t\t\n\t\t// Segment header sizes, measured in 1/6 bits\n\t\tfinal int[] headCosts = new int[numModes];\n\t\tfor (int i = 0; i < numModes; i++)\n\t\t\theadCosts[i] = (4 + modeTypes[i].numCharCountBits(version)) * 6;\n\t\t\n\t\t// charModes[i][j] represents the mode to encode the code point at\n\t\t// index i such that the final segment ends in modeTypes[j] and the\n\t\t// total number of bits is minimized over all possible choices\n\t\tMode[][] charModes = new Mode[codePoints.length][numModes];\n\t\t\n\t\t// At the beginning of each iteration of the loop below,\n\t\t// prevCosts[j] is the exact minimum number of 1/6 bits needed to\n\t\t// encode the entire string prefix of length i, and end in modeTypes[j]\n\t\tint[] prevCosts = headCosts.clone();\n\t\t\n\t\t// Calculate costs using dynamic programming\n\t\tfor (int i = 0; i < codePoints.length; i++) {\n\t\t\tint c = codePoints[i];\n\t\t\tint[] curCosts = new int[numModes];\n\t\t\t{  // Always extend a byte mode segment\n\t\t\t\tcurCosts[0] = prevCosts[0] + countUtf8Bytes(c) * 8 * 6;\n\t\t\t\tcharModes[i][0] = modeTypes[0];\n\t\t\t}\n\t\t\t// Extend a segment if possible\n\t\t\tif (QrSegment.ALPHANUMERIC_MAP[c] != -1) {  // Is alphanumeric\n\t\t\t\tcurCosts[1] = prevCosts[1] + 33;  // 5.5 bits per alphanumeric char\n\t\t\t\tcharModes[i][1] = modeTypes[1];\n\t\t\t}\n\t\t\tif ('0' <= c && c <= '9') {  // Is numeric\n\t\t\t\tcurCosts[2] = prevCosts[2] + 20;  // 3.33 bits per digit\n\t\t\t\tcharModes[i][2] = modeTypes[2];\n\t\t\t}\n\t\t\tif (isKanji(c)) {\n\t\t\t\tcurCosts[3] = prevCosts[3] + 78;  // 13 bits per Shift JIS char\n\t\t\t\tcharModes[i][3] = modeTypes[3];\n\t\t\t}\n\t\t\t\n\t\t\t// Start new segment at the end to switch modes\n\t\t\tfor (int j = 0; j < numModes; j++) {  // To mode\n\t\t\t\tfor (int k = 0; k < numModes; k++) {  // From mode\n\t\t\t\t\tint newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j];\n\t\t\t\t\tif (charModes[i][k] != null && (charModes[i][j] == null || newCost < curCosts[j])) {\n\t\t\t\t\t\tcurCosts[j] = newCost;\n\t\t\t\t\t\tcharModes[i][j] = modeTypes[k];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tprevCosts = curCosts;\n\t\t}\n\t\t\n\t\t// Find optimal ending mode\n\t\tMode curMode = null;\n\t\tfor (int i = 0, minCost = 0; i < numModes; i++) {\n\t\t\tif (curMode == null || prevCosts[i] < minCost) {\n\t\t\t\tminCost = prevCosts[i];\n\t\t\t\tcurMode = modeTypes[i];\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Get optimal mode for each code point by tracing backwards\n\t\tMode[] result = new Mode[charModes.length];\n\t\tfor (int i = result.length - 1; i >= 0; i--) {\n\t\t\tfor (int j = 0; j < numModes; j++) {\n\t\t\t\tif (modeTypes[j] == curMode) {\n\t\t\t\t\tcurMode = charModes[i][j];\n\t\t\t\t\tresult[i] = curMode;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns a new list of segments based on the given text and modes, such that\n\t// consecutive code points in the same mode are put into the same segment.\n\tprivate static List<QrSegment> splitIntoSegments(int[] codePoints, Mode[] charModes) {\n\t\tif (codePoints.length == 0)\n\t\t\tthrow new IllegalArgumentException();\n\t\tList<QrSegment> result = new ArrayList<>();\n\t\t\n\t\t// Accumulate run of modes\n\t\tMode curMode = charModes[0];\n\t\tint start = 0;\n\t\tfor (int i = 1; ; i++) {\n\t\t\tif (i < codePoints.length && charModes[i] == curMode)\n\t\t\t\tcontinue;\n\t\t\tString s = new String(codePoints, start, i - start);\n\t\t\tif (curMode == Mode.BYTE)\n\t\t\t\tresult.add(QrSegment.makeBytes(s.getBytes(StandardCharsets.UTF_8)));\n\t\t\telse if (curMode == Mode.NUMERIC)\n\t\t\t\tresult.add(QrSegment.makeNumeric(s));\n\t\t\telse if (curMode == Mode.ALPHANUMERIC)\n\t\t\t\tresult.add(QrSegment.makeAlphanumeric(s));\n\t\t\telse if (curMode == Mode.KANJI)\n\t\t\t\tresult.add(makeKanji(s));\n\t\t\telse\n\t\t\t\tthrow new AssertionError();\n\t\t\tif (i >= codePoints.length)\n\t\t\t\treturn result;\n\t\t\tcurMode = charModes[i];\n\t\t\tstart = i;\n\t\t}\n\t}\n\t\n\t\n\t// Returns a new array of Unicode code points (effectively\n\t// UTF-32 / UCS-4) representing the given UTF-16 string.\n\tprivate static int[] toCodePoints(String s) {\n\t\tint[] result = s.codePoints().toArray();\n\t\tfor (int c : result) {\n\t\t\tif (Character.isSurrogate((char)c))\n\t\t\t\tthrow new IllegalArgumentException(\"Invalid UTF-16 string\");\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns the number of UTF-8 bytes needed to encode the given Unicode code point.\n\tprivate static int countUtf8Bytes(int cp) {\n\t\tif      (cp <        0) throw new IllegalArgumentException(\"Invalid code point\");\n\t\telse if (cp <     0x80) return 1;\n\t\telse if (cp <    0x800) return 2;\n\t\telse if (cp <  0x10000) return 3;\n\t\telse if (cp < 0x110000) return 4;\n\t\telse                    throw new IllegalArgumentException(\"Invalid code point\");\n\t}\n\t\n\t\n\t\n\t/*---- Kanji mode segment encoder ----*/\n\t\n\t/**\n\t * Returns a segment representing the specified text string encoded in kanji mode.\n\t * Broadly speaking, the set of encodable characters are {kanji used in Japan,\n\t * hiragana, katakana, East Asian punctuation, full-width ASCII, Greek, Cyrillic}.\n\t * Examples of non-encodable characters include {ordinary ASCII, half-width katakana,\n\t * more extensive Chinese hanzi}.\n\t * @param text the text (not {@code null}), with only certain characters allowed\n\t * @return a segment (not {@code null}) containing the text\n\t * @throws NullPointerException if the string is {@code null}\n\t * @throws IllegalArgumentException if the string contains non-encodable characters\n\t * @see #isEncodableAsKanji(String)\n\t */\n\tpublic static QrSegment makeKanji(String text) {\n\t\tObjects.requireNonNull(text);\n\t\tBitBuffer bb = new BitBuffer();\n\t\ttext.chars().forEachOrdered(c -> {\n\t\t\tint val = UNICODE_TO_QR_KANJI[c];\n\t\t\tif (val == -1)\n\t\t\t\tthrow new IllegalArgumentException(\"String contains non-kanji-mode characters\");\n\t\t\tbb.appendBits(val, 13);\n\t\t});\n\t\treturn new QrSegment(Mode.KANJI, text.length(), bb.data, bb.bitLength);\n\t}\n\t\n\t\n\t/**\n\t * Tests whether the specified string can be encoded as a segment in kanji mode.\n\t * Broadly speaking, the set of encodable characters are {kanji used in Japan,\n\t * hiragana, katakana, East Asian punctuation, full-width ASCII, Greek, Cyrillic}.\n\t * Examples of non-encodable characters include {ordinary ASCII, half-width katakana,\n\t * more extensive Chinese hanzi}.\n\t * @param text the string to test for encodability (not {@code null})\n\t * @return {@code true} iff each character is in the kanji mode character set\n\t * @throws NullPointerException if the string is {@code null}\n\t * @see #makeKanji(String)\n\t */\n\tpublic static boolean isEncodableAsKanji(String text) {\n\t\tObjects.requireNonNull(text);\n\t\treturn text.chars().allMatch(\n\t\t\tc -> isKanji((char)c));\n\t}\n\t\n\t\n\tprivate static boolean isKanji(int c) {\n\t\treturn c < UNICODE_TO_QR_KANJI.length && UNICODE_TO_QR_KANJI[c] != -1;\n\t}\n\t\n\t\n\t// Data derived from ftp://ftp.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT\n\tprivate static final String PACKED_QR_KANJI_TO_UNICODE =\n\t\t\"MAAwATAC/wz/DjD7/xr/G/8f/wEwmzCcALT/QACo/z7/4/8/MP0w/jCdMJ4wA07dMAUwBjAHMPwgFSAQ/w8AXDAcIBb/XCAmICUgGCAZIBwgHf8I/wkwFDAV/zv/Pf9b/10wCDAJMAowCzAMMA0wDjAPMBAwEf8LIhIAsQDX//8A9/8dImD/HP8eImYiZyIeIjQmQiZA\" +\n\t\t\"ALAgMiAzIQP/5f8EAKIAo/8F/wP/Bv8K/yAApyYGJgUlyyXPJc4lxyXGJaEloCWzJbIlvSW8IDswEiGSIZAhkSGTMBP/////////////////////////////IggiCyKGIocigiKDIioiKf////////////////////8iJyIoAKwh0iHUIgAiA///////////////////\" +\n\t\t\"//////////8iICKlIxIiAiIHImEiUiJqImsiGiI9Ih0iNSIrIiz//////////////////yErIDAmbyZtJmogICAhALb//////////yXv/////////////////////////////////////////////////xD/Ef8S/xP/FP8V/xb/F/8Y/xn///////////////////8h\" +\n\t\t\"/yL/I/8k/yX/Jv8n/yj/Kf8q/yv/LP8t/y7/L/8w/zH/Mv8z/zT/Nf82/zf/OP85/zr///////////////////9B/0L/Q/9E/0X/Rv9H/0j/Sf9K/0v/TP9N/07/T/9Q/1H/Uv9T/1T/Vf9W/1f/WP9Z/1r//////////zBBMEIwQzBEMEUwRjBHMEgwSTBKMEswTDBN\" +\n\t\t\"ME4wTzBQMFEwUjBTMFQwVTBWMFcwWDBZMFowWzBcMF0wXjBfMGAwYTBiMGMwZDBlMGYwZzBoMGkwajBrMGwwbTBuMG8wcDBxMHIwczB0MHUwdjB3MHgweTB6MHswfDB9MH4wfzCAMIEwgjCDMIQwhTCGMIcwiDCJMIowizCMMI0wjjCPMJAwkTCSMJP/////////////\" +\n\t\t\"////////////////////////MKEwojCjMKQwpTCmMKcwqDCpMKowqzCsMK0wrjCvMLAwsTCyMLMwtDC1MLYwtzC4MLkwujC7MLwwvTC+ML8wwDDBMMIwwzDEMMUwxjDHMMgwyTDKMMswzDDNMM4wzzDQMNEw0jDTMNQw1TDWMNcw2DDZMNow2zDcMN0w3jDf//8w4DDh\" +\n\t\t\"MOIw4zDkMOUw5jDnMOgw6TDqMOsw7DDtMO4w7zDwMPEw8jDzMPQw9TD2/////////////////////wORA5IDkwOUA5UDlgOXA5gDmQOaA5sDnAOdA54DnwOgA6EDowOkA6UDpgOnA6gDqf////////////////////8DsQOyA7MDtAO1A7YDtwO4A7kDugO7A7wDvQO+\" +\n\t\t\"A78DwAPBA8MDxAPFA8YDxwPIA8n/////////////////////////////////////////////////////////////////////////////////////////////////////////////BBAEEQQSBBMEFAQVBAEEFgQXBBgEGQQaBBsEHAQdBB4EHwQgBCEEIgQjBCQEJQQm\" +\n\t\t\"BCcEKAQpBCoEKwQsBC0ELgQv////////////////////////////////////////BDAEMQQyBDMENAQ1BFEENgQ3BDgEOQQ6BDsEPAQ9//8EPgQ/BEAEQQRCBEMERARFBEYERwRIBEkESgRLBEwETQROBE///////////////////////////////////yUAJQIlDCUQ\" +\n\t\t\"JRglFCUcJSwlJCU0JTwlASUDJQ8lEyUbJRclIyUzJSslOyVLJSAlLyUoJTclPyUdJTAlJSU4JUL/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"/////////////////////////////////////06cVRZaA5Y/VMBhG2MoWfaQIoR1gxx6UGCqY+FuJWXthGaCppv1aJNXJ2WhYnFbm1nQhnuY9H1ifb6bjmIWfJ+It1uJXrVjCWaXaEiVx5eNZ09O5U8KT01PnVBJVvJZN1nUWgFcCWDfYQ9hcGYTaQVwunVPdXB5+32t\" +\n\t\t\"fe+Aw4QOiGOLApBVkHpTO06VTqVX34CykMF4704AWPFuopA4ejKDKIKLnC9RQVNwVL1U4VbgWftfFZjybeuA5IUt////////lmKWcJagl/tUC1PzW4dwz3+9j8KW6FNvnVx6uk4ReJOB/G4mVhhVBGsdhRqcO1nlU6ltZnTclY9WQk6RkEuW8oNPmQxT4VW2WzBfcWYg\" +\n\t\t\"ZvNoBGw4bPNtKXRbdsh6Tpg0gvGIW4pgku1tsnWrdsqZxWCmiwGNipWyaY5TrVGG//9XElgwWURbtF72YChjqWP0bL9vFHCOcRRxWXHVcz9+AYJ2gtGFl5BgkludG1hpZbxsWnUlUflZLlllX4Bf3GK8ZfpqKmsna7Rzi3/BiVadLJ0OnsRcoWyWg3tRBFxLYbaBxmh2\" +\n\t\t\"cmFOWU/6U3hgaW4pek+X804LUxZO7k9VTz1PoU9zUqBT71YJWQ9awVu2W+F50WaHZ5xntmtMbLNwa3PCeY15vno8e4eCsYLbgwSDd4Pvg9OHZoqyVimMqI/mkE6XHoaKT8Rc6GIRcll1O4Hlgr2G/ozAlsWZE5nVTstPGonjVt5YSljKXvtf62AqYJRgYmHQYhJi0GU5\" +\n\t\t\"////////m0FmZmiwbXdwcHVMdoZ9dYKlh/mVi5aOjJ1R8VK+WRZUs1uzXRZhaGmCba94jYTLiFeKcpOnmrhtbJmohtlXo2f/hs6SDlKDVodUBF7TYuFkuWg8aDhru3NyeLp6a4maidKNa48DkO2Vo5aUl2lbZlyzaX2YTZhOY5t7IGor//9qf2i2nA1vX1JyVZ1gcGLs\" +\n\t\t\"bTtuB27RhFuJEI9EThScOVP2aRtqOpeEaCpRXHrDhLKR3JOMVludKGgigwWEMXylUgiCxXTmTn5Pg1GgW9JSClLYUudd+1WaWCpZ5luMW5hb215yXnlgo2EfYWNhvmPbZWJn0WhTaPprPmtTbFdvIm+Xb0V0sHUYduN3C3r/e6F8IX3pfzZ/8ICdgmaDnomzisyMq5CE\" +\n\t\t\"lFGVk5WRlaKWZZfTmSiCGE44VCtcuF3Mc6l2THc8XKl/640LlsGYEZhUmFhPAU8OU3FVnFZoV/pZR1sJW8RckF4MXn5fzGPuZzpl12XiZx9oy2jE////////al9eMGvFbBdsfXV/eUhbY3oAfQBfvYmPihiMtI13jsyPHZjimg6bPE6AUH1RAFmTW5xiL2KAZOxrOnKg\" +\n\t\t\"dZF5R3+ph/uKvItwY6yDypegVAlUA1WraFRqWIpweCdndZ7NU3RbooEahlCQBk4YTkVOx08RU8pUOFuuXxNgJWVR//9nPWxCbHJs43B4dAN6dnquewh9Gnz+fWZl53JbU7tcRV3oYtJi4GMZbiCGWooxjd2S+G8BeaabWk6oTqtOrE+bT6BQ0VFHevZRcVH2U1RTIVN/\" +\n\t\t\"U+tVrFiDXOFfN19KYC9gUGBtYx9lWWpLbMFywnLtd++A+IEFggiFTpD3k+GX/5lXmlpO8FHdXC1mgWltXEBm8ml1c4loUHyBUMVS5FdHXf6TJmWkayNrPXQ0eYF5vXtLfcqCuYPMiH+JX4s5j9GR0VQfkoBOXVA2U+VTOnLXc5Z36YLmjq+ZxpnImdJRd2Eahl5VsHp6\" +\n\t\t\"UHZb05BHloVOMmrbkedcUVxI////////Y5h6n2yTl3SPYXqqcYqWiHyCaBd+cGhRk2xS8lQbhauKE3+kjs2Q4VNmiIh5QU/CUL5SEVFEVVNXLXPqV4tZUV9iX4RgdWF2YWdhqWOyZDplbGZvaEJuE3Vmej18+31MfZl+S39rgw6DSobNigiKY4tmjv2YGp2PgriPzpvo\" +\n\t\t\"//9Sh2IfZINvwJaZaEFQkWsgbHpvVHp0fVCIQIojZwhO9lA5UCZQZVF8UjhSY1WnVw9YBVrMXvphsmH4YvNjcmkcailyfXKscy54FHhvfXl3DICpiYuLGYzijtKQY5N1lnqYVZoTnnhRQ1OfU7Nee18mbhtukHOEc/59Q4I3igCK+pZQTk5QC1PkVHxW+lnRW2Rd8V6r\" +\n\t\t\"XydiOGVFZ69uVnLQfMqItIChgOGD8IZOioeN6JI3lseYZ58TTpROkk8NU0hUSVQ+Wi9fjF+hYJ9op2qOdFp4gYqeiqSLd5GQTl6byU6kT3xPr1AZUBZRSVFsUp9SuVL+U5pT41QR////////VA5ViVdRV6JZfVtUW11bj13lXedd9154XoNeml63XxhgUmFMYpdi2GOn\" +\n\t\t\"ZTtmAmZDZvRnbWghaJdpy2xfbSptaW4vbp11MnaHeGx6P3zgfQV9GH1efbGAFYADgK+AsYFUgY+CKoNSiEyIYYsbjKKM/JDKkXWScXg/kvyVpJZN//+YBZmZmtidO1JbUqtT91QIWNVi92/gjGqPX565UUtSO1RKVv16QJF3nWCe0nNEbwmBcHURX/1g2pqoctuPvGtk\" +\n\t\t\"mANOylbwV2RYvlpaYGhhx2YPZgZoOWixbfd11X06gm6bQk6bT1BTyVUGXW9d5l3uZ/tsmXRzeAKKUJOWiN9XUF6nYytQtVCsUY1nAFTJWF5Zu1uwX2liTWOhaD1rc24IcH2Rx3KAeBV4JnltZY59MIPciMGPCZabUmRXKGdQf2qMoVG0V0KWKlg6aYqAtFSyXQ5X/HiV\" +\n\t\t\"nfpPXFJKVItkPmYoZxRn9XqEe1Z9IpMvaFybrXs5UxlRilI3////////W99i9mSuZOZnLWu6hamW0XaQm9ZjTJMGm6t2v2ZSTglQmFPCXHFg6GSSZWNoX3Hmc8p1I3uXfoKGlYuDjNuReJkQZaxmq2uLTtVO1E86T39SOlP4U/JV41bbWOtZy1nJWf9bUFxNXgJeK1/X\" +\n\t\t\"YB1jB2UvW1xlr2W9ZehnnWti//9re2wPc0V5SXnBfPh9GX0rgKKBAoHziZaKXoppimaKjIrujMeM3JbMmPxrb06LTzxPjVFQW1db+mFIYwFmQmshbstsu3I+dL111HjBeTqADIAzgeqElI+ebFCef18Pi1idK3r6jvhbjZbrTgNT8Vf3WTFayVukYIluf28Gdb6M6luf\" +\n\t\t\"hQB74FByZ/SCnVxhhUp+HoIOUZlcBGNojWZlnHFueT59F4AFix2OypBuhseQqlAfUvpcOmdTcHxyNZFMkciTK4LlW8JfMWD5TjtT1luIYktnMWuKculz4HougWuNo5FSmZZRElPXVGpb/2OIajl9rJcAVtpTzlRo////////W5dcMV3eT+5hAWL+bTJ5wHnLfUJ+TX/S\" +\n\t\t\"ge2CH4SQiEaJcouQjnSPL5AxkUuRbJbGkZxOwE9PUUVTQV+TYg5n1GxBbgtzY34mkc2Sg1PUWRlbv23ReV1+LnybWH5xn1H6iFOP8E/KXPtmJXeseuOCHJn/UcZfqmXsaW9riW3z//9ulm9kdv59FF3hkHWRh5gGUeZSHWJAZpFm2W4aXrZ90n9yZviFr4X3ivhSqVPZ\" +\n\t\t\"WXNej1+QYFWS5JZkULdRH1LdUyBTR1PsVOhVRlUxVhdZaFm+WjxbtVwGXA9cEVwaXoReil7gX3Bif2KEYttjjGN3ZgdmDGYtZnZnfmiiah9qNWy8bYhuCW5YcTxxJnFndcd3AXhdeQF5ZXnweuB7EXynfTmAloPWhIuFSYhdiPOKH4o8ilSKc4xhjN6RpJJmk36UGJac\" +\n\t\t\"l5hOCk4ITh5OV1GXUnBXzlg0WMxbIl44YMVk/mdhZ1ZtRHK2dXN6Y4S4i3KRuJMgVjFX9Jj+////////Yu1pDWuWce1+VIB3gnKJ5pjfh1WPsVw7TzhP4U+1VQdaIFvdW+lfw2FOYy9lsGZLaO5pm214bfF1M3W5dx95XnnmfTOB44KvhaqJqoo6jquPm5Aykd2XB066\" +\n\t\t\"TsFSA1h1WOxcC3UaXD2BTooKj8WWY5dteyWKz5gIkWJW81Oo//+QF1Q5V4JeJWOobDRwindhfIt/4IhwkEKRVJMQkxiWj3RemsRdB11pZXBnoo2olttjbmdJaRmDxZgXlsCI/m+EZHpb+E4WcCx1XWYvUcRSNlLiWdNfgWAnYhBlP2V0Zh9mdGjyaBZrY24FcnJ1H3bb\" +\n\t\t\"fL6AVljwiP2Jf4qgipOKy5AdkZKXUpdZZYl6DoEGlrteLWDcYhplpWYUZ5B383pNfE1+PoEKjKyNZI3hjl94qVIHYtljpWRCYpiKLXqDe8CKrJbqfXaCDIdJTtlRSFNDU2Bbo1wCXBZd3WImYkdksGgTaDRsyW1FbRdn029ccU5xfWXLen97rX3a////////fkp/qIF6\" +\n\t\t\"ghuCOYWmim6Mzo31kHiQd5KtkpGVg5uuUk1VhG84cTZRaHmFflWBs3zOVkxYUVyoY6pm/mb9aVpy2XWPdY55DnlWed98l30gfUSGB4o0ljuQYZ8gUOdSdVPMU+JQCVWqWO5ZT3I9W4tcZFMdYONg82NcY4NjP2O7//9kzWXpZvld42nNaf1vFXHlTol16Xb4epN8333P\" +\n\t\t\"fZyAYYNJg1iEbIS8hfuIxY1wkAGQbZOXlxyaElDPWJdhjoHThTWNCJAgT8NQdFJHU3Ngb2NJZ19uLI2zkB9P11xejMplz32aU1KIllF2Y8NbWFtrXApkDWdRkFxO1lkaWSpscIpRVT5YFVmlYPBiU2fBgjVpVZZAmcSaKE9TWAZb/oAQXLFeL1+FYCBhS2I0Zv9s8G7e\" +\n\t\t\"gM6Bf4LUiIuMuJAAkC6Wip7bm9tO41PwWSd7LJGNmEyd+W7dcCdTU1VEW4ViWGKeYtNsom/vdCKKF5Q4b8GK/oM4UeeG+FPq////////U+lPRpBUj7BZaoExXf166o+/aNqMN3L4nEhqPYqwTjlTWFYGV2ZixWOiZeZrTm3hbltwrXfteu97qn27gD2AxobLipWTW1bj\" +\n\t\t\"WMdfPmWtZpZqgGu1dTeKx1Akd+VXMF8bYGVmemxgdfR6Gn9ugfSHGJBFmbN7yXVcevl7UYTE//+QEHnpepKDNlrhd0BOLU7yW5lf4GK9Zjxn8WzohmuId4o7kU6S85nQahdwJnMqgueEV4yvTgFRRlHLVYtb9V4WXjNegV8UXzVfa1+0YfJjEWaiZx1vbnJSdTp3OoB0\" +\n\t\t\"gTmBeId2ir+K3I2FjfOSmpV3mAKc5VLFY1d29GcVbIhzzYzDk66Wc20lWJxpDmnMj/2TmnXbkBpYWmgCY7Rp+09Dbyxn2I+7hSZ9tJNUaT9vcFdqWPdbLH0scipUCpHjnbROrU9OUFxQdVJDjJ5USFgkW5peHV6VXq1e918fYIxitWM6Y9Bor2xAeId5jnoLfeCCR4oC\" +\n\t\t\"iuaORJAT////////kLiRLZHYnw5s5WRYZOJldW70doR7G5Bpk9FuulTyX7lkpI9Nj+2SRFF4WGtZKVxVXpdt+36PdRyMvI7imFtwuU8da79vsXUwlvtRTlQQWDVYV1msXGBfkmWXZ1xuIXZ7g9+M7ZAUkP2TTXgleDpSql6mVx9ZdGASUBJRWlGs//9RzVIAVRBYVFhY\" +\n\t\t\"WVdblVz2XYtgvGKVZC1ncWhDaLxo33bXbdhub22bcG9xyF9Tddh5d3tJe1R7UnzWfXFSMIRjhWmF5IoOiwSMRo4PkAOQD5QZlnaYLZowldhQzVLVVAxYAlwOYadknm0ed7N65YD0hASQU5KFXOCdB1M/X5dfs22ccnl3Y3m/e+Rr0nLsiq1oA2phUfh6gWk0XEqc9oLr\" +\n\t\t\"W8WRSXAeVnhcb2DHZWZsjIxakEGYE1RRZseSDVlIkKNRhU5NUeqFmYsOcFhjepNLaWKZtH4EdXdTV2lgjt+W42xdToxcPF8Qj+lTAozRgImGeV7/ZeVOc1Fl////////WYJcP5fuTvtZil/Nio1v4XmweWJb54RxcytxsV50X/Vje2SaccN8mE5DXvxOS1fcVqJgqW/D\" +\n\t\t\"fQ2A/YEzgb+PsomXhqRd9GKKZK2Jh2d3bOJtPnQ2eDRaRn91gq2ZrE/zXsNi3WOSZVdnb3bDckyAzIC6jymRTVANV/lakmiF//9pc3Fkcv2Mt1jyjOCWapAZh3955HfnhClPL1JlU1pizWfPbMp2fXuUfJWCNoWEj+tm3W8gcgZ+G4OrmcGeplH9e7F4cnu4gId7SGro\" +\n\t\t\"XmGAjHVRdWBRa5Jibox2epGXmupPEH9wYpx7T5WlnOlWelhZhuSWvE80UiRTSlPNU9teBmQsZZFnf2w+bE5ySHKvc+11VH5BgiyF6Yype8SRxnFpmBKY72M9Zml1anbkeNCFQ4buUypTUVQmWYNeh198YLJiSWJ5YqtlkGvUbMx1snaueJF52H3Lf3eApYirirmMu5B/\" +\n\t\t\"l16Y22oLfDhQmVw+X65nh2vYdDV3CX+O////////nztnynoXUzl1i5rtX2aBnYPxgJhfPF/FdWJ7RpA8aGdZ61qbfRB2fossT/VfamoZbDdvAnTieWiIaIpVjHle32PPdcV50oLXkyiS8oSchu2cLVTBX2xljG1ccBWMp4zTmDtlT3T2Tg1O2FfgWStaZlvMUaheA16c\" +\n\t\t\"YBZidmV3//9lp2ZubW5yNnsmgVCBmoKZi1yMoIzmjXSWHJZET65kq2tmgh6EYYVqkOhcAWlTmKiEeoVXTw9Sb1+pXkVnDXmPgXmJB4mGbfVfF2JVbLhOz3Jpm5JSBlQ7VnRYs2GkYm5xGllufIl83n0blvBlh4BeThlPdVF1WEBeY15zXwpnxE4mhT2ViZZbfHOYAVD7\" +\n\t\t\"WMF2VninUiV3pYURe4ZQT1kJckd7x33oj7qP1JBNT79SyVopXwGXrU/dgheS6lcDY1VraXUriNyPFHpCUt9Yk2FVYgpmrmvNfD+D6VAjT/hTBVRGWDFZSVudXPBc710pXpZisWNnZT5luWcL////////bNVs4XD5eDJ+K4DegrOEDITshwKJEooqjEqQppLSmP2c851s\" +\n\t\t\"Tk9OoVCNUlZXSlmoXj1f2F/ZYj9mtGcbZ9Bo0lGSfSGAqoGoiwCMjIy/kn6WMlQgmCxTF1DVU1xYqGSyZzRyZ3dmekaR5lLDbKFrhlgAXkxZVGcsf/tR4XbG//9kaXjom1Seu1fLWblmJ2eaa85U6WnZXlWBnGeVm6pn/pxSaF1Opk/jU8hiuWcrbKuPxE+tfm2ev04H\" +\n\t\t\"YWJugG8rhRNUc2cqm0Vd83uVXKxbxoccbkqE0XoUgQhZmXyNbBF3IFLZWSJxIXJfd9uXJ51haQtaf1oYUaVUDVR9Zg5234/3kpic9Fnqcl1uxVFNaMl9v33sl2KeumR4aiGDAlmEW19r23MbdvJ9soAXhJlRMmcontl27mdiUv+ZBVwkYjt8foywVU9gtn0LlYBTAU5f\" +\n\t\t\"UbZZHHI6gDaRzl8ld+JThF95fQSFrIozjo2XVmfzha6UU2EJYQhsuXZS////////iu2POFUvT1FRKlLHU8tbpV59YKBhgmPWZwln2m5nbYxzNnM3dTF5UIjVipiQSpCRkPWWxIeNWRVOiE9ZTg6KiY8/mBBQrV58WZZbuV64Y9pj+mTBZtxpSmnYbQtutnGUdSh6r3+K\" +\n\t\t\"gACESYTJiYGLIY4KkGWWfZkKYX5ikWsy//9sg210f8x//G3Af4WHuoj4Z2WDsZg8lvdtG31hhD2Rak5xU3VdUGsEb+uFzYYtiadSKVQPXGVnTmiodAZ0g3XiiM+I4ZHMluKWeF+Lc4d6y4ROY6B1ZVKJbUFunHQJdVl4a3ySloZ63J+NT7ZhbmXFhlxOhk6uUNpOIVHM\" +\n\t\t\"W+5lmWiBbbxzH3ZCd616HHzngm+K0pB8kc+WdZgYUpt90VArU5hnl23LcdB0M4HojyqWo5xXnp90YFhBbZl9L5heTuRPNk+LUbdSsV26YBxzsnk8gtOSNJa3lvaXCp6Xn2Jmpmt0UhdSo3DIiMJeyWBLYZBvI3FJfD599IBv////////hO6QI5MsVEKbb2rTcImMwo3v\" +\n\t\t\"lzJStFpBXspfBGcXaXxplG1qbw9yYnL8e+2AAYB+h0uQzlFtnpN5hICLkzKK1lAtVIyKcWtqjMSBB2DRZ6Cd8k6ZTpicEIprhcGFaGkAbn54l4FV////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"/////////////////////////////18MThBOFU4qTjFONk48Tj9OQk5WTlhOgk6FjGtOioISXw1Ojk6eTp9OoE6iTrBOs062Ts5OzU7ETsZOwk7XTt5O7U7fTvdPCU9aTzBPW09dT1dPR092T4hPj0+YT3tPaU9wT5FPb0+GT5ZRGE/UT99Pzk/YT9tP0U/aT9BP5E/l\" +\n\t\t\"UBpQKFAUUCpQJVAFTxxP9lAhUClQLE/+T+9QEVAGUENQR2cDUFVQUFBIUFpQVlBsUHhQgFCaUIVQtFCy////////UMlQylCzUMJQ1lDeUOVQ7VDjUO5Q+VD1UQlRAVECURZRFVEUURpRIVE6UTdRPFE7UT9RQFFSUUxRVFFievhRaVFqUW5RgFGCVthRjFGJUY9RkVGT\" +\n\t\t\"UZVRllGkUaZRolGpUapRq1GzUbFRslGwUbVRvVHFUclR21HghlVR6VHt//9R8FH1Uf5SBFILUhRSDlInUipSLlIzUjlST1JEUktSTFJeUlRSalJ0UmlSc1J/Un1SjVKUUpJScVKIUpGPqI+nUqxSrVK8UrVSwVLNUtdS3lLjUuaY7VLgUvNS9VL4UvlTBlMIdThTDVMQ\" +\n\t\t\"Uw9TFVMaUyNTL1MxUzNTOFNAU0ZTRU4XU0lTTVHWU15TaVNuWRhTe1N3U4JTllOgU6ZTpVOuU7BTtlPDfBKW2VPfZvxx7lPuU+hT7VP6VAFUPVRAVCxULVQ8VC5UNlQpVB1UTlSPVHVUjlRfVHFUd1RwVJJUe1SAVHZUhFSQVIZUx1SiVLhUpVSsVMRUyFSo////////\" +\n\t\t\"VKtUwlSkVL5UvFTYVOVU5lUPVRRU/VTuVO1U+lTiVTlVQFVjVUxVLlVcVUVVVlVXVThVM1VdVZlVgFSvVYpVn1V7VX5VmFWeVa5VfFWDValVh1WoVdpVxVXfVcRV3FXkVdRWFFX3VhZV/lX9VhtV+VZOVlBx31Y0VjZWMlY4//9Wa1ZkVi9WbFZqVoZWgFaKVqBWlFaP\" +\n\t\t\"VqVWrla2VrRWwla8VsFWw1bAVshWzlbRVtNW11buVvlXAFb/VwRXCVcIVwtXDVcTVxhXFlXHVxxXJlc3VzhXTlc7V0BXT1dpV8BXiFdhV39XiVeTV6BXs1ekV6pXsFfDV8ZX1FfSV9NYClfWV+NYC1gZWB1YclghWGJYS1hwa8BYUlg9WHlYhVi5WJ9Yq1i6WN5Yu1i4\" +\n\t\t\"WK5YxVjTWNFY11jZWNhY5VjcWORY31jvWPpY+Vj7WPxY/VkCWQpZEFkbaKZZJVksWS1ZMlk4WT560llVWVBZTllaWVhZYllgWWdZbFlp////////WXhZgVmdT15Pq1mjWbJZxlnoWdxZjVnZWdpaJVofWhFaHFoJWhpaQFpsWklaNVo2WmJaalqaWrxavlrLWsJavVrj\" +\n\t\t\"Wtda5lrpWtZa+lr7WwxbC1sWWzJa0FsqWzZbPltDW0VbQFtRW1VbWltbW2VbaVtwW3NbdVt4ZYhbeluA//9bg1umW7hbw1vHW8lb1FvQW+Rb5lviW95b5VvrW/Bb9lvzXAVcB1wIXA1cE1wgXCJcKFw4XDlcQVxGXE5cU1xQXE9bcVxsXG5OYlx2XHlcjFyRXJRZm1yr\" +\n\t\t\"XLtctly8XLdcxVy+XMdc2VzpXP1c+lztXYxc6l0LXRVdF11cXR9dG10RXRRdIl0aXRldGF1MXVJdTl1LXWxdc112XYddhF2CXaJdnV2sXa5dvV2QXbddvF3JXc1d013SXdZd213rXfJd9V4LXhpeGV4RXhteNl43XkReQ15AXk5eV15UXl9eYl5kXkdedV52XnqevF5/\" +\n\t\t\"XqBewV7CXshe0F7P////////XtZe417dXtpe217iXuFe6F7pXuxe8V7zXvBe9F74Xv5fA18JX11fXF8LXxFfFl8pXy1fOF9BX0hfTF9OXy9fUV9WX1dfWV9hX21fc193X4Nfgl9/X4pfiF+RX4dfnl+ZX5hfoF+oX61fvF/WX/tf5F/4X/Ff3WCzX/9gIWBg//9gGWAQ\" +\n\t\t\"YClgDmAxYBtgFWArYCZgD2A6YFpgQWBqYHdgX2BKYEZgTWBjYENgZGBCYGxga2BZYIFgjWDnYINgmmCEYJtglmCXYJJgp2CLYOFguGDgYNNgtF/wYL1gxmC1YNhhTWEVYQZg9mD3YQBg9GD6YQNhIWD7YPFhDWEOYUdhPmEoYSdhSmE/YTxhLGE0YT1hQmFEYXNhd2FY\" +\n\t\t\"YVlhWmFrYXRhb2FlYXFhX2FdYVNhdWGZYZZhh2GsYZRhmmGKYZFhq2GuYcxhymHJYfdhyGHDYcZhumHLf3lhzWHmYeNh9mH6YfRh/2H9Yfxh/mIAYghiCWINYgxiFGIb////////Yh5iIWIqYi5iMGIyYjNiQWJOYl5iY2JbYmBiaGJ8YoJiiWJ+YpJik2KWYtRig2KU\" +\n\t\t\"Ytdi0WK7Ys9i/2LGZNRiyGLcYsxiymLCYsdim2LJYwxi7mLxYydjAmMIYu9i9WNQYz5jTWQcY09jlmOOY4Bjq2N2Y6Njj2OJY59jtWNr//9jaWO+Y+ljwGPGY+NjyWPSY/ZjxGQWZDRkBmQTZCZkNmUdZBdkKGQPZGdkb2R2ZE5lKmSVZJNkpWSpZIhkvGTaZNJkxWTH\" +\n\t\t\"ZLtk2GTCZPFk54IJZOBk4WKsZONk72UsZPZk9GTyZPplAGT9ZRhlHGUFZSRlI2UrZTRlNWU3ZTZlOHVLZUhlVmVVZU1lWGVeZV1lcmV4ZYJlg4uKZZtln2WrZbdlw2XGZcFlxGXMZdJl22XZZeBl4WXxZ3JmCmYDZftnc2Y1ZjZmNGYcZk9mRGZJZkFmXmZdZmRmZ2Zo\" +\n\t\t\"Zl9mYmZwZoNmiGaOZolmhGaYZp1mwWa5Zslmvma8////////ZsRmuGbWZtpm4GY/ZuZm6WbwZvVm92cPZxZnHmcmZyeXOGcuZz9nNmdBZzhnN2dGZ15nYGdZZ2NnZGeJZ3BnqWd8Z2pnjGeLZ6ZnoWeFZ7dn72e0Z+xns2fpZ7hn5GfeZ91n4mfuZ7lnzmfGZ+dqnGge\" +\n\t\t\"aEZoKWhAaE1oMmhO//9os2graFloY2h3aH9on2iPaK1olGidaJtog2quaLlodGi1aKBoumkPaI1ofmkBaMppCGjYaSJpJmjhaQxozWjUaOdo1Wk2aRJpBGjXaONpJWj5aOBo72koaSppGmkjaSFoxml5aXdpXGl4aWtpVGl+aW5pOWl0aT1pWWkwaWFpXmldaYFpammy\" +\n\t\t\"aa5p0Gm/acFp02m+ac5b6GnKad1pu2nDaadqLmmRaaBpnGmVabRp3mnoagJqG2n/awpp+WnyaedqBWmxah5p7WoUaetqCmoSasFqI2oTakRqDGpyajZqeGpHamJqWWpmakhqOGoiapBqjWqgaoRqomqj////////apeGF2q7asNqwmq4arNqrGreatFq32qqatpq6mr7\" +\n\t\t\"awWGFmr6axJrFpsxax9rOGs3dtxrOZjua0drQ2tJa1BrWWtUa1trX2tha3hreWt/a4BrhGuDa41rmGuVa55rpGuqa6trr2uya7Frs2u3a7xrxmvLa9Nr32vsa+tr82vv//+evmwIbBNsFGwbbCRsI2xebFVsYmxqbIJsjWyabIFsm2x+bGhsc2ySbJBsxGzxbNNsvWzX\" +\n\t\t\"bMVs3WyubLFsvmy6bNts72zZbOptH4hNbTZtK209bThtGW01bTNtEm0MbWNtk21kbVpteW1ZbY5tlW/kbYVt+W4VbgpttW3HbeZtuG3Gbext3m3Mbeht0m3Fbfpt2W3kbdVt6m3ubi1ubm4ubhlucm5fbj5uI25rbitudm5Nbh9uQ246bk5uJG7/bh1uOG6CbqpumG7J\" +\n\t\t\"brdu0269bq9uxG6ybtRu1W6PbqVuwm6fb0FvEXBMbuxu+G7+bz9u8m8xbu9vMm7M////////bz5vE273b4Zvem94b4FvgG9vb1tv829tb4JvfG9Yb45vkW/Cb2Zvs2+jb6FvpG+5b8Zvqm/fb9Vv7G/Ub9hv8W/ub9twCXALb/pwEXABcA9v/nAbcBpvdHAdcBhwH3Aw\" +\n\t\t\"cD5wMnBRcGNwmXCScK9w8XCscLhws3CucN9wy3Dd//9w2XEJcP1xHHEZcWVxVXGIcWZxYnFMcVZxbHGPcftxhHGVcahxrHHXcblxvnHScclx1HHOceBx7HHncfVx/HH5cf9yDXIQchtyKHItcixyMHIycjtyPHI/ckByRnJLclhydHJ+coJygXKHcpJylnKicqdyuXKy\" +\n\t\t\"csNyxnLEcs5y0nLicuBy4XL5cvdQD3MXcwpzHHMWcx1zNHMvcylzJXM+c05zT57Yc1dzanNoc3BzeHN1c3tzenPIc7NzznO7c8Bz5XPuc950onQFdG90JXP4dDJ0OnRVdD90X3RZdEF0XHRpdHB0Y3RqdHZ0fnSLdJ50p3TKdM901HPx////////dOB043TndOl07nTy\" +\n\t\t\"dPB08XT4dPd1BHUDdQV1DHUOdQ11FXUTdR51JnUsdTx1RHVNdUp1SXVbdUZ1WnVpdWR1Z3VrdW11eHV2dYZ1h3V0dYp1iXWCdZR1mnWddaV1o3XCdbN1w3W1db11uHW8dbF1zXXKddJ12XXjdd51/nX///91/HYBdfB1+nXydfN2C3YNdgl2H3YndiB2IXYidiR2NHYw\" +\n\t\t\"djt2R3ZIdkZ2XHZYdmF2YnZodml2anZndmx2cHZydnZ2eHZ8doB2g3aIdot2jnaWdpN2mXaadrB2tHa4drl2unbCds121nbSdt524Xbldud26oYvdvt3CHcHdwR3KXckdx53JXcmdxt3N3c4d0d3Wndod2t3W3dld393fnd5d453i3eRd6B3nnewd7Z3uXe/d7x3vXe7\" +\n\t\t\"d8d3zXfXd9p33Hfjd+53/HgMeBJ5JnggeSp4RXiOeHR4hnh8eJp4jHijeLV4qniveNF4xnjLeNR4vni8eMV4ynjs////////eOd42nj9ePR5B3kSeRF5GXkseSt5QHlgeVd5X3laeVV5U3l6eX95inmdeaefS3mqea55s3m5ebp5yXnVeed57HnheeN6CHoNehh6GXog\" +\n\t\t\"eh95gHoxejt6Pno3ekN6V3pJemF6Ynppn516cHp5en16iHqXepV6mHqWeql6yHqw//96tnrFesR6v5CDesd6ynrNes961XrTetl62nrdeuF64nrmeu168HsCew97CnsGezN7GHsZex57NXsoezZ7UHt6ewR7TXsLe0x7RXt1e2V7dHtne3B7cXtse257nXuYe597jXuc\" +\n\t\t\"e5p7i3uSe497XXuZe8t7wXvMe897tHvGe9176XwRfBR75nvlfGB8AHwHfBN783v3fBd8DXv2fCN8J3wqfB98N3wrfD18THxDfFR8T3xAfFB8WHxffGR8VnxlfGx8dXyDfJB8pHytfKJ8q3yhfKh8s3yyfLF8rny5fL18wHzFfMJ82HzSfNx84ps7fO988nz0fPZ8+n0G\" +\n\t\t\"////////fQJ9HH0VfQp9RX1LfS59Mn0/fTV9Rn1zfVZ9Tn1yfWh9bn1PfWN9k32JfVt9j319fZt9un2ufaN9tX3Hfb19q349faJ9r33cfbh9n32wfdh93X3kfd59+33yfeF+BX4KfiN+IX4SfjF+H34Jfgt+In5GfmZ+O341fjl+Q343//9+Mn46fmd+XX5Wfl5+WX5a\" +\n\t\t\"fnl+an5pfnx+e36DfdV+fY+ufn9+iH6Jfox+kn6QfpN+lH6Wfo5+m36cfzh/On9Ff0x/TX9Of1B/UX9Vf1R/WH9ff2B/aH9pf2d/eH+Cf4Z/g3+If4d/jH+Uf55/nX+af6N/r3+yf7l/rn+2f7iLcX/Ff8Z/yn/Vf9R/4X/mf+l/83/5mNyABoAEgAuAEoAYgBmAHIAh\" +\n\t\t\"gCiAP4A7gEqARoBSgFiAWoBfgGKAaIBzgHKAcIB2gHmAfYB/gISAhoCFgJuAk4CagK1RkICsgNuA5YDZgN2AxIDagNaBCYDvgPGBG4EpgSOBL4FL////////louBRoE+gVOBUYD8gXGBboFlgWaBdIGDgYiBioGAgYKBoIGVgaSBo4FfgZOBqYGwgbWBvoG4gb2BwIHC\" +\n\t\t\"gbqByYHNgdGB2YHYgciB2oHfgeCB54H6gfuB/oIBggKCBYIHggqCDYIQghaCKYIrgjiCM4JAglmCWIJdglqCX4Jk//+CYoJogmqCa4IugnGCd4J4gn6CjYKSgquCn4K7gqyC4YLjgt+C0oL0gvOC+oOTgwOC+4L5gt6DBoLcgwmC2YM1gzSDFoMygzGDQIM5g1CDRYMv\" +\n\t\t\"gyuDF4MYg4WDmoOqg5+DooOWgyODjoOHg4qDfIO1g3ODdYOgg4mDqIP0hBOD64POg/2EA4PYhAuDwYP3hAeD4IPyhA2EIoQgg72EOIUGg/uEbYQqhDyFWoSEhHeEa4SthG6EgoRphEaELIRvhHmENYTKhGKEuYS/hJ+E2YTNhLuE2oTQhMGExoTWhKGFIYT/hPSFF4UY\" +\n\t\t\"hSyFH4UVhRSE/IVAhWOFWIVI////////hUGGAoVLhVWFgIWkhYiFkYWKhaiFbYWUhZuF6oWHhZyFd4V+hZCFyYW6hc+FuYXQhdWF3YXlhdyF+YYKhhOGC4X+hfqGBoYihhqGMIY/hk1OVYZUhl+GZ4ZxhpOGo4aphqqGi4aMhraGr4bEhsaGsIbJiCOGq4bUht6G6Ybs\" +\n\t\t\"//+G34bbhu+HEocGhwiHAIcDhvuHEYcJhw2G+YcKhzSHP4c3hzuHJYcphxqHYIdfh3iHTIdOh3SHV4doh26HWYdTh2OHaogFh6KHn4eCh6+Hy4e9h8CH0JbWh6uHxIezh8eHxoe7h++H8ofgiA+IDYf+h/aH94gOh9KIEYgWiBWIIoghiDGINog5iCeIO4hEiEKIUohZ\" +\n\t\t\"iF6IYohriIGIfoieiHWIfYi1iHKIgoiXiJKIroiZiKKIjYikiLCIv4ixiMOIxIjUiNiI2YjdiPmJAoj8iPSI6IjyiQSJDIkKiROJQ4keiSWJKokriUGJRIk7iTaJOIlMiR2JYIle////////iWaJZIltiWqJb4l0iXeJfomDiYiJiomTiZiJoYmpiaaJrImvibKJuom9\" +\n\t\t\"ib+JwInaidyJ3YnnifSJ+IoDihaKEIoMihuKHYolijaKQYpbilKKRopIinyKbYpsimKKhYqCioSKqIqhipGKpYqmipqKo4rEis2KworaiuuK84rn//+K5IrxixSK4IriiveK3orbiwyLB4saiuGLFosQixeLIIszl6uLJosriz6LKItBi0yLT4tOi0mLVotbi1qLa4tf\" +\n\t\t\"i2yLb4t0i32LgIuMi46LkouTi5aLmYuajDqMQYw/jEiMTIxOjFCMVYxijGyMeIx6jIKMiYyFjIqMjYyOjJSMfIyYYh2MrYyqjL2MsoyzjK6MtozIjMGM5IzjjNqM/Yz6jPuNBI0FjQqNB40PjQ2NEJ9OjROMzY0UjRaNZ41tjXGNc42BjZmNwo2+jbqNz43ajdaNzI3b\" +\n\t\t\"jcuN6o3rjd+N4438jgiOCY3/jh2OHo4Qjh+OQo41jjCONI5K////////jkeOSY5MjlCOSI5ZjmSOYI4qjmOOVY52jnKOfI6BjoeOhY6EjouOio6TjpGOlI6ZjqqOoY6sjrCOxo6xjr6OxY7IjsuO247jjvyO+47rjv6PCo8FjxWPEo8ZjxOPHI8fjxuPDI8mjzOPO485\" +\n\t\t\"j0WPQo8+j0yPSY9Gj06PV49c//+PYo9jj2SPnI+fj6OPrY+vj7eP2o/lj+KP6o/vkIeP9JAFj/mP+pARkBWQIZANkB6QFpALkCeQNpA1kDmP+JBPkFCQUZBSkA6QSZA+kFaQWJBekGiQb5B2lqiQcpCCkH2QgZCAkIqQiZCPkKiQr5CxkLWQ4pDkYkiQ25ECkRKRGZEy\" +\n\t\t\"kTCRSpFWkViRY5FlkWmRc5FykYuRiZGCkaKRq5GvkaqRtZG0kbqRwJHBkcmRy5HQkdaR35HhkduR/JH1kfaSHpH/khSSLJIVkhGSXpJXkkWSSZJkkkiSlZI/kkuSUJKckpaSk5KbklqSz5K5kreS6ZMPkvqTRJMu////////kxmTIpMakyOTOpM1kzuTXJNgk3yTbpNW\" +\n\t\t\"k7CTrJOtk5STuZPWk9eT6JPlk9iTw5Pdk9CTyJPklBqUFJQTlAOUB5QQlDaUK5Q1lCGUOpRBlFKURJRblGCUYpRelGqSKZRwlHWUd5R9lFqUfJR+lIGUf5WClYeVipWUlZaVmJWZ//+VoJWolaeVrZW8lbuVuZW+lcpv9pXDlc2VzJXVldSV1pXcleGV5ZXiliGWKJYu\" +\n\t\t\"li+WQpZMlk+WS5Z3llyWXpZdll+WZpZylmyWjZaYlpWWl5aqlqeWsZaylrCWtJa2lriWuZbOlsuWyZbNiU2W3JcNltWW+ZcElwaXCJcTlw6XEZcPlxaXGZcklyqXMJc5lz2XPpdEl0aXSJdCl0mXXJdgl2SXZpdoUtKXa5dxl3mXhZd8l4GXepeGl4uXj5eQl5yXqJem\" +\n\t\t\"l6OXs5e0l8OXxpfIl8uX3Jftn0+X8nrfl/aX9ZgPmAyYOJgkmCGYN5g9mEaYT5hLmGuYb5hw////////mHGYdJhzmKqYr5ixmLaYxJjDmMaY6ZjrmQOZCZkSmRSZGJkhmR2ZHpkkmSCZLJkumT2ZPplCmUmZRZlQmUuZUZlSmUyZVZmXmZiZpZmtma6ZvJnfmduZ3ZnY\" +\n\t\t\"mdGZ7ZnumfGZ8pn7mfiaAZoPmgWZ4poZmiuaN5pFmkKaQJpD//+aPppVmk2aW5pXml+aYpplmmSaaZprmmqarZqwmryawJrPmtGa05rUmt6a35rimuOa5prvmuua7pr0mvGa95r7mwabGJsamx+bIpsjmyWbJ5somymbKpsumy+bMptEm0ObT5tNm06bUZtYm3Sbk5uD\" +\n\t\t\"m5GblpuXm5+boJuom7SbwJvKm7mbxpvPm9Gb0pvjm+Kb5JvUm+GcOpvym/Gb8JwVnBScCZwTnAycBpwInBKcCpwEnC6cG5wlnCScIZwwnEecMpxGnD6cWpxgnGecdpx4nOec7JzwnQmdCJzrnQOdBp0qnSadr50jnR+dRJ0VnRKdQZ0/nT6dRp1I////////nV2dXp1k\" +\n\t\t\"nVGdUJ1ZnXKdiZ2Hnaudb516nZqdpJ2pnbKdxJ3BnbuduJ26ncadz53Cndmd0534nead7Z3vnf2eGp4bnh6edZ55nn2egZ6InouejJ6SnpWekZ6dnqWeqZ64nqqerZdhnsyezp7PntCe1J7cnt6e3Z7gnuWe6J7v//+e9J72nvee+Z77nvye/Z8Hnwh2t58VnyGfLJ8+\" +\n\t\t\"n0qfUp9Un2OfX59gn2GfZp9nn2yfap93n3Kfdp+Vn5yfoFgvaceQWXRkUdxxmf//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\" +\n\t\t\"/////////////////////////////////////////////w==\";\n\t\n\t\n\tprivate static short[] UNICODE_TO_QR_KANJI = new short[1 << 16];\n\t\n\tstatic {  // Unpack the Shift JIS table into a more computation-friendly form\n\t\tArrays.fill(UNICODE_TO_QR_KANJI, (short)-1);\n\t\tbyte[] bytes = Base64.getDecoder().decode(PACKED_QR_KANJI_TO_UNICODE);\n\t\tfor (int i = 0; i < bytes.length; i += 2) {\n\t\t\tchar c = (char)(((bytes[i] & 0xFF) << 8) | (bytes[i + 1] & 0xFF));\n\t\t\tif (c == 0xFFFF)\n\t\t\t\tcontinue;\n\t\t\tassert UNICODE_TO_QR_KANJI[c] == -1;\n\t\t\tUNICODE_TO_QR_KANJI[c] = (short)(i / 2);\n\t\t}\n\t}\n\t\n\t\n\t\n\t/*---- Miscellaneous ----*/\n\t\n\tprivate QrSegmentAdvanced() {}  // Not instantiable\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/QrTemplate.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\n\n// Stores the parts of a QR Code that depend only on the version number,\n// and does not depend on the data or error correction level or mask.\nfinal class QrTemplate {\n\t\n\t// Use this memoizer to get instances of this class.\n\tpublic static final Memoizer<Integer,QrTemplate> MEMOIZER\n\t\t= new Memoizer<>(QrTemplate::new);\n\t\n\t\n\tprivate final int version;  // In the range [1, 40].\n\tprivate final int size;  // Derived from version.\n\t\n\tfinal int[] template;  // Length and values depend on version.\n\tfinal int[][] masks;  // masks.length == 8, and masks[i].length == template.length.\n\tfinal int[] dataOutputBitIndexes;  // Length and values depend on version.\n\t\n\t// Indicates function modules that are not subjected to masking. Discarded when constructor finishes.\n\t// Otherwise when the constructor is running, isFunction.length == template.length.\n\tprivate int[] isFunction;\n\t\n\t\n\t// Creates a QR Code template for the given version number.\n\tprivate QrTemplate(int ver) {\n\t\tif (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)\n\t\t\tthrow new IllegalArgumentException(\"Version out of range\");\n\t\tversion = ver;\n\t\tsize = version * 4 + 17;\n\t\ttemplate = new int[(size * size + 31) / 32];\n\t\tisFunction = new int[template.length];\n\t\t\n\t\tdrawFunctionPatterns();  // Reads and writes fields\n\t\tmasks = generateMasks();  // Reads fields, returns array\n\t\tdataOutputBitIndexes = generateZigzagScan();  // Reads fields, returns array\n\t\tisFunction = null;\n\t}\n\t\n\t\n\t// Reads this object's version field, and draws and marks all function modules.\n\tprivate void drawFunctionPatterns() {\n\t\t// Draw horizontal and vertical timing patterns\n\t\tfor (int i = 0; i < size; i++) {\n\t\t\tdarkenFunctionModule(6, i, ~i & 1);\n\t\t\tdarkenFunctionModule(i, 6, ~i & 1);\n\t\t}\n\t\t\n\t\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\t\tdrawFinderPattern(3, 3);\n\t\tdrawFinderPattern(size - 4, 3);\n\t\tdrawFinderPattern(3, size - 4);\n\t\t\n\t\t// Draw numerous alignment patterns\n\t\tint[] alignPatPos = getAlignmentPatternPositions();\n\t\tint numAlign = alignPatPos.length;\n\t\tfor (int i = 0; i < numAlign; i++) {\n\t\t\tfor (int j = 0; j < numAlign; j++) {\n\t\t\t\t// Don't draw on the three finder corners\n\t\t\t\tif (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))\n\t\t\t\t\tdrawAlignmentPattern(alignPatPos[i], alignPatPos[j]);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Draw configuration data\n\t\tdrawDummyFormatBits();\n\t\tdrawVersion();\n\t}\n\t\n\t\n\t// Draws two blank copies of the format bits.\n\tprivate void drawDummyFormatBits() {\n\t\t// Draw first copy\n\t\tfor (int i = 0; i <= 5; i++)\n\t\t\tdarkenFunctionModule(8, i, 0);\n\t\tdarkenFunctionModule(8, 7, 0);\n\t\tdarkenFunctionModule(8, 8, 0);\n\t\tdarkenFunctionModule(7, 8, 0);\n\t\tfor (int i = 9; i < 15; i++)\n\t\t\tdarkenFunctionModule(14 - i, 8, 0);\n\t\t\n\t\t// Draw second copy\n\t\tfor (int i = 0; i < 8; i++)\n\t\t\tdarkenFunctionModule(size - 1 - i, 8, 0);\n\t\tfor (int i = 8; i < 15; i++)\n\t\t\tdarkenFunctionModule(8, size - 15 + i, 0);\n\t\tdarkenFunctionModule(8, size - 8, 1);  // Always dark\n\t}\n\t\n\t\n\t// Draws two copies of the version bits (with its own error correction code),\n\t// based on this object's version field, iff 7 <= version <= 40.\n\tprivate void drawVersion() {\n\t\tif (version < 7)\n\t\t\treturn;\n\t\t\n\t\t// Calculate error correction code and pack bits\n\t\tint rem = version;  // version is uint6, in the range [7, 40]\n\t\tfor (int i = 0; i < 12; i++)\n\t\t\trem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);\n\t\tint bits = version << 12 | rem;  // uint18\n\t\tassert bits >>> 18 == 0;\n\t\t\n\t\t// Draw two copies\n\t\tfor (int i = 0; i < 18; i++) {\n\t\t\tint bit = QrCode.getBit(bits, i);\n\t\t\tint a = size - 11 + i % 3;\n\t\t\tint b = i / 3;\n\t\t\tdarkenFunctionModule(a, b, bit);\n\t\t\tdarkenFunctionModule(b, a, bit);\n\t\t}\n\t}\n\t\n\t\n\t// Draws a 9*9 finder pattern including the border separator,\n\t// with the center module at (x, y). Modules can be out of bounds.\n\tprivate void drawFinderPattern(int x, int y) {\n\t\tfor (int dy = -4; dy <= 4; dy++) {\n\t\t\tfor (int dx = -4; dx <= 4; dx++) {\n\t\t\t\tint dist = Math.max(Math.abs(dx), Math.abs(dy));  // Chebyshev/infinity norm\n\t\t\t\tint xx = x + dx, yy = y + dy;\n\t\t\t\tif (0 <= xx && xx < size && 0 <= yy && yy < size)\n\t\t\t\t\tdarkenFunctionModule(xx, yy, (dist != 2 && dist != 4) ? 1 : 0);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Draws a 5*5 alignment pattern, with the center module\n\t// at (x, y). All modules must be in bounds.\n\tprivate void drawAlignmentPattern(int x, int y) {\n\t\tfor (int dy = -2; dy <= 2; dy++) {\n\t\t\tfor (int dx = -2; dx <= 2; dx++)\n\t\t\t\tdarkenFunctionModule(x + dx, y + dy, Math.abs(Math.max(Math.abs(dx), Math.abs(dy)) - 1));\n\t\t}\n\t}\n\t\n\t\n\t// Computes and returns a new array of masks, based on this object's various fields.\n\tprivate int[][] generateMasks() {\n\t\tint[][] result = new int[8][template.length];\n\t\tfor (int mask = 0; mask < result.length; mask++) {\n\t\t\tint[] maskModules = result[mask];\n\t\t\tfor (int y = 0, i = 0; y < size; y++) {\n\t\t\t\tfor (int x = 0; x < size; x++, i++) {\n\t\t\t\t\tboolean invert;\n\t\t\t\t\tswitch (mask) {\n\t\t\t\t\t\tcase 0:  invert = (x + y) % 2 == 0;                    break;\n\t\t\t\t\t\tcase 1:  invert = y % 2 == 0;                          break;\n\t\t\t\t\t\tcase 2:  invert = x % 3 == 0;                          break;\n\t\t\t\t\t\tcase 3:  invert = (x + y) % 3 == 0;                    break;\n\t\t\t\t\t\tcase 4:  invert = (x / 3 + y / 2) % 2 == 0;            break;\n\t\t\t\t\t\tcase 5:  invert = x * y % 2 + x * y % 3 == 0;          break;\n\t\t\t\t\t\tcase 6:  invert = (x * y % 2 + x * y % 3) % 2 == 0;    break;\n\t\t\t\t\t\tcase 7:  invert = ((x + y) % 2 + x * y % 3) % 2 == 0;  break;\n\t\t\t\t\t\tdefault:  throw new AssertionError();\n\t\t\t\t\t}\n\t\t\t\t\tint bit = (invert ? 1 : 0) & ~getModule(isFunction, x, y);\n\t\t\t\t\tmaskModules[i >>> 5] |= bit << i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\t// Computes and returns an array of bit indexes, based on this object's various fields.\n\tprivate int[] generateZigzagScan() {\n\t\tint[] result = new int[getNumRawDataModules(version) / 8 * 8];\n\t\tint i = 0;  // Bit index into the data\n\t\tfor (int right = size - 1; right >= 1; right -= 2) {  // Index of right column in each column pair\n\t\t\tif (right == 6)\n\t\t\t\tright = 5;\n\t\t\tfor (int vert = 0; vert < size; vert++) {  // Vertical counter\n\t\t\t\tfor (int j = 0; j < 2; j++) {\n\t\t\t\t\tint x = right - j;  // Actual x coordinate\n\t\t\t\t\tboolean upward = ((right + 1) & 2) == 0;\n\t\t\t\t\tint y = upward ? size - 1 - vert : vert;  // Actual y coordinate\n\t\t\t\t\tif (getModule(isFunction, x, y) == 0 && i < result.length) {\n\t\t\t\t\t\tresult[i] = y * size + x;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tassert i == result.length;\n\t\treturn result;\n\t}\n\t\n\t\n\t// Returns the value of the bit at the given coordinates in the given grid.\n\tprivate int getModule(int[] grid, int x, int y) {\n\t\tassert 0 <= x && x < size;\n\t\tassert 0 <= y && y < size;\n\t\tint i = y * size + x;\n\t\treturn QrCode.getBit(grid[i >>> 5], i);\n\t}\n\t\n\t\n\t// Marks the module at the given coordinates as a function module.\n\t// Also either sets that module dark or keeps its color unchanged.\n\tprivate void darkenFunctionModule(int x, int y, int enable) {\n\t\tassert 0 <= x && x < size;\n\t\tassert 0 <= y && y < size;\n\t\tassert enable == 0 || enable == 1;\n\t\tint i = y * size + x;\n\t\ttemplate[i >>> 5] |= enable << i;\n\t\tisFunction[i >>> 5] |= 1 << i;\n\t}\n\t\n\t\n\t// Returns an ascending list of positions of alignment patterns for this version number.\n\t// Each position is in the range [0,177), and are used on both the x and y axes.\n\t// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.\n\tprivate int[] getAlignmentPatternPositions() {\n\t\tif (version == 1)\n\t\t\treturn new int[]{};\n\t\telse {\n\t\t\tint numAlign = version / 7 + 2;\n\t\t\tint step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;\n\t\t\tint[] result = new int[numAlign];\n\t\t\tresult[0] = 6;\n\t\t\tfor (int i = result.length - 1, pos = size - 7; i >= 1; i--, pos -= step)\n\t\t\t\tresult[i] = pos;\n\t\t\treturn result;\n\t\t}\n\t}\n\t\n\t\n\t// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\n\tstatic int getNumRawDataModules(int ver) {\n\t\tif (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)\n\t\t\tthrow new IllegalArgumentException(\"Version number out of range\");\n\t\tint result = (16 * ver + 128) * ver + 64;\n\t\tif (ver >= 2) {\n\t\t\tint numAlign = ver / 7 + 2;\n\t\t\tresult -= (25 * numAlign - 10) * numAlign - 55;\n\t\t\tif (ver >= 7)\n\t\t\t\tresult -= 36;\n\t\t}\n\t\treturn result;\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java",
    "content": "/* \n * Fast QR Code generator library\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/fast-qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\npackage io.nayuki.fastqrcodegen;\n\nimport java.util.Arrays;\nimport java.util.Objects;\n\n\n// Computes Reed-Solomon error correction codewords for given data codewords.\nfinal class ReedSolomonGenerator {\n\t\n\t// Use this memoizer to get instances of this class.\n\tpublic static final Memoizer<Integer,ReedSolomonGenerator> MEMOIZER\n\t\t= new Memoizer<>(ReedSolomonGenerator::new);\n\t\n\t\n\t// A table of size 256 * degree, where polynomialMultiply[i][j] = multiply(i, coefficients[j]).\n\t// 'coefficients' is the temporary array computed in the constructor.\n\tprivate byte[][] polynomialMultiply;\n\t\n\t\n\t// Creates a Reed-Solomon ECC generator polynomial for the given degree.\n\tprivate ReedSolomonGenerator(int degree) {\n\t\tif (degree < 1 || degree > 255)\n\t\t\tthrow new IllegalArgumentException(\"Degree out of range\");\n\t\t\n\t\t// The divisor polynomial, whose coefficients are stored from highest to lowest power.\n\t\t// For example, x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.\n\t\tbyte[] coefficients = new byte[degree];\n\t\tcoefficients[degree - 1] = 1;  // Start off with the monomial x^0\n\t\t\n\t\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t\t// and drop the highest monomial term which is always 1x^degree.\n\t\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\t\tint root = 1;\n\t\tfor (int i = 0; i < degree; i++) {\n\t\t\t// Multiply the current product by (x - r^i)\n\t\t\tfor (int j = 0; j < coefficients.length; j++) {\n\t\t\t\tcoefficients[j] = (byte)multiply(coefficients[j] & 0xFF, root);\n\t\t\t\tif (j + 1 < coefficients.length)\n\t\t\t\t\tcoefficients[j] ^= coefficients[j + 1];\n\t\t\t}\n\t\t\troot = multiply(root, 0x02);\n\t\t}\n\t\t\n\t\tpolynomialMultiply = new byte[256][degree];\n\t\tfor (int i = 0; i < polynomialMultiply.length; i++) {\n\t\t\tfor (int j = 0; j < degree; j++)\n\t\t\t\tpolynomialMultiply[i][j] = (byte)multiply(i, coefficients[j] & 0xFF);\n\t\t}\n\t}\n\t\n\t\n\t// Returns the error correction codeword for the given data polynomial and this divisor polynomial.\n\tpublic void getRemainder(byte[] data, int dataOff, int dataLen, byte[] result) {\n\t\tObjects.requireNonNull(data);\n\t\tObjects.requireNonNull(result);\n\t\tint degree = polynomialMultiply[0].length;\n\t\tassert result.length == degree;\n\t\t\n\t\tArrays.fill(result, (byte)0);\n\t\tfor (int i = dataOff, dataEnd = dataOff + dataLen; i < dataEnd; i++) {  // Polynomial division\n\t\t\tbyte[] table = polynomialMultiply[(data[i] ^ result[0]) & 0xFF];\n\t\t\tfor (int j = 0; j < degree - 1; j++)\n\t\t\t\tresult[j] = (byte)(result[j + 1] ^ table[j]);\n\t\t\tresult[degree - 1] = table[degree - 1];\n\t\t}\n\t}\n\t\n\t\n\t// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result\n\t// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.\n\tprivate static int multiply(int x, int y) {\n\t\tassert x >> 8 == 0 && y >> 8 == 0;\n\t\t// Russian peasant multiplication\n\t\tint z = 0;\n\t\tfor (int i = 7; i >= 0; i--) {\n\t\t\tz = (z << 1) ^ ((z >>> 7) * 0x11D);\n\t\t\tz ^= ((y >>> i) & 1) * x;\n\t\t}\n\t\tassert z >>> 8 == 0;\n\t\treturn z;\n\t}\n\t\n}\n"
  },
  {
    "path": "java-fast/io/nayuki/fastqrcodegen/package-info.java",
    "content": "/**\n * Generates QR Codes from text strings and byte arrays.\n * \n * <p>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.</p>\n * <p>Home page for this fast library with design explanation and benchmarks: <a href=\"https://www.nayuki.io/page/fast-qr-code-generator-library\">https://www.nayuki.io/page/fast-qr-code-generator-library</a></p>\n * <p>Home page for the main project with live JavaScript demo, extensive descriptions, and competitor comparisons: <a href=\"https://www.nayuki.io/page/qr-code-generator-library\">https://www.nayuki.io/page/qr-code-generator-library</a></p>\n * \n * <h2>Features</h2>\n * <p>Core features:</p>\n * <ul>\n *   <li><p>Approximately 1.5× to 10× faster than other Java implementation</p></li>\n *   <li><p>Shorter code but more documentation comments compared to competing libraries</p></li>\n *   <li><p>Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard</p></li>\n *   <li><p>Output format: Raw modules/pixels of the QR symbol</p></li>\n *   <li><p>Detects finder-like penalty patterns more accurately than other implementations</p></li>\n *   <li><p>Encodes numeric and special-alphanumeric text in less space than general text</p></li>\n *   <li><p>Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes</p></li>\n *   <li><p>Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts</p></li>\n *   <li><p>Open-source code under the permissive MIT License</p></li>\n * </ul>\n * <p>Manual parameters:</p>\n * <ul>\n *   <li><p>User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data</p></li>\n *   <li><p>User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one</p></li>\n *   <li><p>User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number</p></li>\n *   <li><p>User can create a list of data segments manually and add ECI segments</p></li>\n * </ul>\n * <p>More information about QR Code technology and this library's design can be found on the project home page.</p>\n * \n * <h2>Examples</h2>\n * <p>Simple operation:</p>\n * <pre style=\"margin-left:2em\">import java.awt.image.BufferedImage;\n *import java.io.File;\n *import javax.imageio.ImageIO;\n *import io.nayuki.fastqrcodegen.*;\n *\n *QrCode qr = QrCode.encodeText(\"Hello, world!\", QrCode.Ecc.MEDIUM);\n *BufferedImage img = toImage(qr, 4, 10);  // See QrCodeGeneratorDemo\n *ImageIO.write(img, \"png\", new File(\"qr-code.png\"));</pre>\n * <p>Manual operation:</p>\n * <pre style=\"margin-left:2em\">import java.util.List;\n *import io.nayuki.fastqrcodegen.*;\n *\n *List&lt;QrSegment&gt; segs = QrSegment.makeSegments(\"3141592653589793238462643383\");\n *QrCode qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);\n *for (int y = 0; y &lt; qr.size; y++) {\n *    for (int x = 0; x &lt; qr.size; x++) {\n *        (... paint qr.getModule(x, y) ...)\n *    }\n *}</pre>\n */\npackage io.nayuki.fastqrcodegen;\n"
  },
  {
    "path": "python/Readme.markdown",
    "content": "QR Code generator library - Python\n==================================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```python\nfrom qrcodegen import *\n\n# Simple operation\nqr0 = QrCode.encode_text(\"Hello, world!\", QrCode.Ecc.MEDIUM)\nsvg = to_svg_str(qr0, 4)  # See qrcodegen-demo\n\n# Manual operation\nsegs = QrSegment.make_segments(\"3141592653589793238462643383\")\nqr1 = QrCode.encode_segments(segs, QrCode.Ecc.HIGH, 5, 5, 2, False)\nfor y in range(qr1.get_size()):\n    for x in range(qr1.get_size()):\n        (... paint qr1.get_module(x, y) ...)\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/python/qrcodegen-demo.py .\n"
  },
  {
    "path": "python/qrcodegen-demo.py",
    "content": "# \n# QR Code generator demo (Python)\n# \n# Run this command-line program with no arguments. The program computes a bunch of demonstration\n# QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.\n# \n# Copyright (c) Project Nayuki. (MIT License)\n# https://www.nayuki.io/page/qr-code-generator-library\n# \n# Permission is hereby granted, free of charge, to any person obtaining a copy of\n# this software and associated documentation files (the \"Software\"), to deal in\n# the Software without restriction, including without limitation the rights to\n# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n# the Software, and to permit persons to whom the Software is furnished to do so,\n# subject to the following conditions:\n# - The above copyright notice and this permission notice shall be included in\n#   all copies or substantial portions of the Software.\n# - The Software is provided \"as is\", without warranty of any kind, express or\n#   implied, including but not limited to the warranties of merchantability,\n#   fitness for a particular purpose and noninfringement. In no event shall the\n#   authors or copyright holders be liable for any claim, damages or other\n#   liability, whether in an action of contract, tort or otherwise, arising from,\n#   out of or in connection with the Software or the use or other dealings in the\n#   Software.\n# \n\nfrom __future__ import annotations\nfrom qrcodegen import QrCode, QrSegment\n\n\ndef main() -> None:\n\t\"\"\"The main application program.\"\"\"\n\tdo_basic_demo()\n\tdo_variety_demo()\n\tdo_segment_demo()\n\tdo_mask_demo()\n\n\n\n# ---- Demo suite ----\n\ndef do_basic_demo() -> None:\n\t\"\"\"Creates a single QR Code, then prints it to the console.\"\"\"\n\ttext = \"Hello, world!\"      # User-supplied Unicode text\n\terrcorlvl = QrCode.Ecc.LOW  # Error correction level\n\t\n\t# Make and print the QR Code symbol\n\tqr = QrCode.encode_text(text, errcorlvl)\n\tprint_qr(qr)\n\tprint(to_svg_str(qr, 4))\n\n\ndef do_variety_demo() -> None:\n\t\"\"\"Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.\"\"\"\n\t\n\t# Numeric mode encoding (3.33 bits per digit)\n\tqr = QrCode.encode_text(\"314159265358979323846264338327950288419716939937510\", QrCode.Ecc.MEDIUM)\n\tprint_qr(qr)\n\t\n\t# Alphanumeric mode encoding (5.5 bits per character)\n\tqr = QrCode.encode_text(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", QrCode.Ecc.HIGH)\n\tprint_qr(qr)\n\t\n\t# Unicode text as UTF-8\n\tqr = QrCode.encode_text(\"\\u3053\\u3093\\u306B\\u3061\\u0077\\u0061\\u3001\\u4E16\\u754C\\uFF01\\u0020\\u03B1\\u03B2\\u03B3\\u03B4\", QrCode.Ecc.QUARTILE)\n\tprint_qr(qr)\n\t\n\t# Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\tqr = QrCode.encode_text(\n\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \"\n\t\t\"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \"\n\t\t\"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \"\n\t\t\"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \"\n\t\t\"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \"\n\t\t\"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \"\n\t\t\"a White Rabbit with pink eyes ran close by her.\", QrCode.Ecc.HIGH)\n\tprint_qr(qr)\n\n\ndef do_segment_demo() -> None:\n\t\"\"\"Creates QR Codes with manually specified segments for better compactness.\"\"\"\n\t\n\t# Illustration \"silver\"\n\tsilver0 = \"THE SQUARE ROOT OF 2 IS 1.\"\n\tsilver1 = \"41421356237309504880168872420969807856967187537694807317667973799\"\n\tqr = QrCode.encode_text(silver0 + silver1, QrCode.Ecc.LOW)\n\tprint_qr(qr)\n\t\n\tsegs = [\n\t\tQrSegment.make_alphanumeric(silver0),\n\t\tQrSegment.make_numeric(silver1)]\n\tqr = QrCode.encode_segments(segs, QrCode.Ecc.LOW)\n\tprint_qr(qr)\n\t\n\t# Illustration \"golden\"\n\tgolden0 = \"Golden ratio \\u03C6 = 1.\"\n\tgolden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\"\n\tgolden2 = \"......\"\n\tqr = QrCode.encode_text(golden0 + golden1 + golden2, QrCode.Ecc.LOW)\n\tprint_qr(qr)\n\t\n\tsegs = [\n\t\tQrSegment.make_bytes(golden0.encode(\"UTF-8\")),\n\t\tQrSegment.make_numeric(golden1),\n\t\tQrSegment.make_alphanumeric(golden2)]\n\tqr = QrCode.encode_segments(segs, QrCode.Ecc.LOW)\n\tprint_qr(qr)\n\t\n\t# Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\tmadoka = \"\\u300C\\u9B54\\u6CD5\\u5C11\\u5973\\u307E\\u3069\\u304B\\u2606\\u30DE\\u30AE\\u30AB\\u300D\\u3063\\u3066\\u3001\\u3000\\u0418\\u0410\\u0418\\u3000\\uFF44\\uFF45\\uFF53\\uFF55\\u3000\\u03BA\\u03B1\\uFF1F\"\n\tqr = QrCode.encode_text(madoka, QrCode.Ecc.LOW)\n\tprint_qr(qr)\n\t\n\tkanjicharbits = [  # Kanji mode encoding (13 bits per character)\n\t\t0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1,\n\t\t1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\n\t\t0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,\n\t\t0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1,\n\t\t0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,\n\t\t0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,\n\t\t0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1,\n\t\t0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1,\n\t\t0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,\n\t\t0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,\n\t\t0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1,\n\t\t0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0,\n\t\t0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1,\n\t\t0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0,\n\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,\n\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,\n\t\t0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,\n\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,\n\t\t0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,\n\t\t0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,\n\t\t0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1,\n\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n\t\t0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n\t\t0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n\t]\n\tsegs = [QrSegment(QrSegment.Mode.KANJI, len(kanjicharbits) // 13, kanjicharbits)]\n\tqr = QrCode.encode_segments(segs, QrCode.Ecc.LOW)\n\tprint_qr(qr)\n\n\ndef do_mask_demo() -> None:\n\t\"\"\"Creates QR Codes with the same size and contents but different mask patterns.\"\"\"\n\t\n\t# Project Nayuki URL\n\tsegs = QrSegment.make_segments(\"https://www.nayuki.io/\")\n\tprint_qr(QrCode.encode_segments(segs, QrCode.Ecc.HIGH, mask=-1))  # Automatic mask\n\tprint_qr(QrCode.encode_segments(segs, QrCode.Ecc.HIGH, mask=3))  # Force mask 3\n\t\n\t# Chinese text as UTF-8\n\tsegs = QrSegment.make_segments(\n\t\t\"\\u7DAD\\u57FA\\u767E\\u79D1\\uFF08\\u0057\\u0069\\u006B\\u0069\\u0070\\u0065\\u0064\\u0069\\u0061\\uFF0C\"\n\t\t\"\\u8046\\u807D\\u0069\\u002F\\u02CC\\u0077\\u026A\\u006B\\u1D7B\\u02C8\\u0070\\u0069\\u02D0\\u0064\\u0069\"\n\t\t\"\\u002E\\u0259\\u002F\\uFF09\\u662F\\u4E00\\u500B\\u81EA\\u7531\\u5167\\u5BB9\\u3001\\u516C\\u958B\\u7DE8\"\n\t\t\"\\u8F2F\\u4E14\\u591A\\u8A9E\\u8A00\\u7684\\u7DB2\\u8DEF\\u767E\\u79D1\\u5168\\u66F8\\u5354\\u4F5C\\u8A08\"\n\t\t\"\\u756B\")\n\tprint_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=0))  # Force mask 0\n\tprint_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=1))  # Force mask 1\n\tprint_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=5))  # Force mask 5\n\tprint_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=7))  # Force mask 7\n\n\n\n# ---- Utilities ----\n\ndef to_svg_str(qr: QrCode, border: int) -> str:\n\t\"\"\"Returns a string of SVG code for an image depicting the given QR Code, with the given number\n\tof border modules. The string always uses Unix newlines (\\n), regardless of the platform.\"\"\"\n\tif border < 0:\n\t\traise ValueError(\"Border must be non-negative\")\n\tparts: list[str] = []\n\tfor y in range(qr.get_size()):\n\t\tfor x in range(qr.get_size()):\n\t\t\tif qr.get_module(x, y):\n\t\t\t\tparts.append(f\"M{x+border},{y+border}h1v1h-1z\")\n\treturn f\"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 {qr.get_size()+border*2} {qr.get_size()+border*2}\" stroke=\"none\">\n\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n\t<path d=\"{\" \".join(parts)}\" fill=\"#000000\"/>\n</svg>\n\"\"\"\n\n\ndef print_qr(qrcode: QrCode) -> None:\n\t\"\"\"Prints the given QrCode object to the console.\"\"\"\n\tborder = 4\n\tfor y in range(-border, qrcode.get_size() + border):\n\t\tfor x in range(-border, qrcode.get_size() + border):\n\t\t\tprint(\"\\u2588 \"[1 if qrcode.get_module(x,y) else 0] * 2, end=\"\")\n\t\tprint()\n\tprint()\n\n\n# Run the main program\nif __name__ == \"__main__\":\n\tmain()\n"
  },
  {
    "path": "python/qrcodegen.py",
    "content": "# \n# QR Code generator library (Python)\n# \n# Copyright (c) Project Nayuki. (MIT License)\n# https://www.nayuki.io/page/qr-code-generator-library\n# \n# Permission is hereby granted, free of charge, to any person obtaining a copy of\n# this software and associated documentation files (the \"Software\"), to deal in\n# the Software without restriction, including without limitation the rights to\n# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n# the Software, and to permit persons to whom the Software is furnished to do so,\n# subject to the following conditions:\n# - The above copyright notice and this permission notice shall be included in\n#   all copies or substantial portions of the Software.\n# - The Software is provided \"as is\", without warranty of any kind, express or\n#   implied, including but not limited to the warranties of merchantability,\n#   fitness for a particular purpose and noninfringement. In no event shall the\n#   authors or copyright holders be liable for any claim, damages or other\n#   liability, whether in an action of contract, tort or otherwise, arising from,\n#   out of or in connection with the Software or the use or other dealings in the\n#   Software.\n# \n\nfrom __future__ import annotations\nimport collections, itertools, re\nfrom collections.abc import Sequence\nfrom typing import Optional, Union\n\n\n# ---- QR Code symbol class ----\n\nclass QrCode:\n\t\"\"\"A QR Code symbol, which is a type of two-dimension barcode.\n\tInvented by Denso Wave and described in the ISO/IEC 18004 standard.\n\tInstances of this class represent an immutable square grid of dark and light cells.\n\tThe class provides static factory functions to create a QR Code from text or binary data.\n\tThe class covers the QR Code Model 2 specification, supporting all versions (sizes)\n\tfrom 1 to 40, all 4 error correction levels, and 4 character encoding modes.\n\t\n\tWays to create a QR Code object:\n\t- High level: Take the payload data and call QrCode.encode_text() or QrCode.encode_binary().\n\t- Mid level: Custom-make the list of segments and call QrCode.encode_segments().\n\t- Low level: Custom-make the array of data codeword bytes (including\n\t  segment headers and final padding, excluding error correction codewords),\n\t  supply the appropriate version number, and call the QrCode() constructor.\n\t(Note that all ways require supplying the desired error correction level.)\"\"\"\n\t\n\t# ---- Static factory functions (high level) ----\n\t\n\t@staticmethod\n\tdef encode_text(text: str, ecl: QrCode.Ecc) -> QrCode:\n\t\t\"\"\"Returns a QR Code representing the given Unicode text string at the given error correction level.\n\t\tAs a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer\n\t\tUnicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible\n\t\tQR Code version is automatically chosen for the output. The ECC level of the result may be higher than the\n\t\tecl argument if it can be done without increasing the version.\"\"\"\n\t\tsegs: list[QrSegment] = QrSegment.make_segments(text)\n\t\treturn QrCode.encode_segments(segs, ecl)\n\t\n\t\n\t@staticmethod\n\tdef encode_binary(data: Union[bytes,Sequence[int]], ecl: QrCode.Ecc) -> QrCode:\n\t\t\"\"\"Returns a QR Code representing the given binary data at the given error correction level.\n\t\tThis function always encodes using the binary segment mode, not any text mode. The maximum number of\n\t\tbytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.\n\t\tThe ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.\"\"\"\n\t\treturn QrCode.encode_segments([QrSegment.make_bytes(data)], ecl)\n\t\n\t\n\t# ---- Static factory functions (mid level) ----\n\t\n\t@staticmethod\n\tdef encode_segments(segs: Sequence[QrSegment], ecl: QrCode.Ecc, minversion: int = 1, maxversion: int = 40, mask: int = -1, boostecl: bool = True) -> QrCode:\n\t\t\"\"\"Returns a QR Code representing the given segments with the given encoding parameters.\n\t\tThe smallest possible QR Code version within the given range is automatically\n\t\tchosen for the output. Iff boostecl is true, then the ECC level of the result\n\t\tmay be higher than the ecl argument if it can be done without increasing the\n\t\tversion. The mask number is either between 0 to 7 (inclusive) to force that\n\t\tmask, or -1 to automatically choose an appropriate mask (which may be slow).\n\t\tThis function allows the user to create a custom sequence of segments that switches\n\t\tbetween modes (such as alphanumeric and byte) to encode text in less space.\n\t\tThis is a mid-level API; the high-level API is encode_text() and encode_binary().\"\"\"\n\t\t\n\t\tif not (QrCode.MIN_VERSION <= minversion <= maxversion <= QrCode.MAX_VERSION) or not (-1 <= mask <= 7):\n\t\t\traise ValueError(\"Invalid value\")\n\t\t\n\t\t# Find the minimal version number to use\n\t\tfor version in range(minversion, maxversion + 1):\n\t\t\tdatacapacitybits: int = QrCode._get_num_data_codewords(version, ecl) * 8  # Number of data bits available\n\t\t\tdatausedbits: Optional[int] = QrSegment.get_total_bits(segs, version)\n\t\t\tif (datausedbits is not None) and (datausedbits <= datacapacitybits):\n\t\t\t\tbreak  # This version number is found to be suitable\n\t\t\tif version >= maxversion:  # All versions in the range could not fit the given data\n\t\t\t\tmsg: str = \"Segment too long\"\n\t\t\t\tif datausedbits is not None:\n\t\t\t\t\tmsg = f\"Data length = {datausedbits} bits, Max capacity = {datacapacitybits} bits\"\n\t\t\t\traise DataTooLongError(msg)\n\t\tassert datausedbits is not None\n\t\t\n\t\t# Increase the error correction level while the data still fits in the current version number\n\t\tfor newecl in (QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH):  # From low to high\n\t\t\tif boostecl and (datausedbits <= QrCode._get_num_data_codewords(version, newecl) * 8):\n\t\t\t\tecl = newecl\n\t\t\n\t\t# Concatenate all segments to create the data bit string\n\t\tbb = _BitBuffer()\n\t\tfor seg in segs:\n\t\t\tbb.append_bits(seg.get_mode().get_mode_bits(), 4)\n\t\t\tbb.append_bits(seg.get_num_chars(), seg.get_mode().num_char_count_bits(version))\n\t\t\tbb.extend(seg._bitdata)\n\t\tassert len(bb) == datausedbits\n\t\t\n\t\t# Add terminator and pad up to a byte if applicable\n\t\tdatacapacitybits = QrCode._get_num_data_codewords(version, ecl) * 8\n\t\tassert len(bb) <= datacapacitybits\n\t\tbb.append_bits(0, min(4, datacapacitybits - len(bb)))\n\t\tbb.append_bits(0, -len(bb) % 8)  # Note: Python's modulo on negative numbers behaves better than C family languages\n\t\tassert len(bb) % 8 == 0\n\t\t\n\t\t# Pad with alternating bytes until data capacity is reached\n\t\tfor padbyte in itertools.cycle((0xEC, 0x11)):\n\t\t\tif len(bb) >= datacapacitybits:\n\t\t\t\tbreak\n\t\t\tbb.append_bits(padbyte, 8)\n\t\t\n\t\t# Pack bits into bytes in big endian\n\t\tdatacodewords = bytearray([0] * (len(bb) // 8))\n\t\tfor (i, bit) in enumerate(bb):\n\t\t\tdatacodewords[i >> 3] |= bit << (7 - (i & 7))\n\t\t\n\t\t# Create the QR Code object\n\t\treturn QrCode(version, ecl, datacodewords, mask)\n\t\n\t\n\t# ---- Private fields ----\n\t\n\t# The version number of this QR Code, which is between 1 and 40 (inclusive).\n\t# This determines the size of this barcode.\n\t_version: int\n\t\n\t# The width and height of this QR Code, measured in modules, between\n\t# 21 and 177 (inclusive). This is equal to version * 4 + 17.\n\t_size: int\n\t\n\t# The error correction level used in this QR Code.\n\t_errcorlvl: QrCode.Ecc\n\t\n\t# The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).\n\t# Even if a QR Code is created with automatic masking requested (mask = -1),\n\t# the resulting object still has a mask value between 0 and 7.\n\t_mask: int\n\t\n\t# The modules of this QR Code (False = light, True = dark).\n\t# Immutable after constructor finishes. Accessed through get_module().\n\t_modules: list[list[bool]]\n\t\n\t# Indicates function modules that are not subjected to masking. Discarded when constructor finishes.\n\t_isfunction: list[list[bool]]\n\t\n\t\n\t# ---- Constructor (low level) ----\n\t\n\tdef __init__(self, version: int, errcorlvl: QrCode.Ecc, datacodewords: Union[bytes,Sequence[int]], msk: int) -> None:\n\t\t\"\"\"Creates a new QR Code with the given version number,\n\t\terror correction level, data codeword bytes, and mask number.\n\t\tThis is a low-level API that most users should not use directly.\n\t\tA mid-level API is the encode_segments() function.\"\"\"\n\t\t\n\t\t# Check scalar arguments and set fields\n\t\tif not (QrCode.MIN_VERSION <= version <= QrCode.MAX_VERSION):\n\t\t\traise ValueError(\"Version value out of range\")\n\t\tif not (-1 <= msk <= 7):\n\t\t\traise ValueError(\"Mask value out of range\")\n\t\t\n\t\tself._version = version\n\t\tself._size = version * 4 + 17\n\t\tself._errcorlvl = errcorlvl\n\t\t\n\t\t# Initialize both grids to be size*size arrays of Boolean false\n\t\tself._modules    = [[False] * self._size for _ in range(self._size)]  # Initially all light\n\t\tself._isfunction = [[False] * self._size for _ in range(self._size)]\n\t\t\n\t\t# Compute ECC, draw modules\n\t\tself._draw_function_patterns()\n\t\tallcodewords: bytes = self._add_ecc_and_interleave(bytearray(datacodewords))\n\t\tself._draw_codewords(allcodewords)\n\t\t\n\t\t# Do masking\n\t\tif msk == -1:  # Automatically choose best mask\n\t\t\tminpenalty: int = 1 << 32\n\t\t\tfor i in range(8):\n\t\t\t\tself._apply_mask(i)\n\t\t\t\tself._draw_format_bits(i)\n\t\t\t\tpenalty = self._get_penalty_score()\n\t\t\t\tif penalty < minpenalty:\n\t\t\t\t\tmsk = i\n\t\t\t\t\tminpenalty = penalty\n\t\t\t\tself._apply_mask(i)  # Undoes the mask due to XOR\n\t\tassert 0 <= msk <= 7\n\t\tself._mask = msk\n\t\tself._apply_mask(msk)  # Apply the final choice of mask\n\t\tself._draw_format_bits(msk)  # Overwrite old format bits\n\t\t\n\t\tdel self._isfunction\n\t\n\t\n\t# ---- Accessor methods ----\n\t\n\tdef get_version(self) -> int:\n\t\t\"\"\"Returns this QR Code's version number, in the range [1, 40].\"\"\"\n\t\treturn self._version\n\t\n\tdef get_size(self) -> int:\n\t\t\"\"\"Returns this QR Code's size, in the range [21, 177].\"\"\"\n\t\treturn self._size\n\t\n\tdef get_error_correction_level(self) -> QrCode.Ecc:\n\t\t\"\"\"Returns this QR Code's error correction level.\"\"\"\n\t\treturn self._errcorlvl\n\t\n\tdef get_mask(self) -> int:\n\t\t\"\"\"Returns this QR Code's mask, in the range [0, 7].\"\"\"\n\t\treturn self._mask\n\t\n\tdef get_module(self, x: int, y: int) -> bool:\n\t\t\"\"\"Returns the color of the module (pixel) at the given coordinates, which is False\n\t\tfor light or True for dark. The top left corner has the coordinates (x=0, y=0).\n\t\tIf the given coordinates are out of bounds, then False (light) is returned.\"\"\"\n\t\treturn (0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x]\n\t\n\t\n\t# ---- Private helper methods for constructor: Drawing function modules ----\n\t\n\tdef _draw_function_patterns(self) -> None:\n\t\t\"\"\"Reads this object's version field, and draws and marks all function modules.\"\"\"\n\t\t# Draw horizontal and vertical timing patterns\n\t\tfor i in range(self._size):\n\t\t\tself._set_function_module(6, i, i % 2 == 0)\n\t\t\tself._set_function_module(i, 6, i % 2 == 0)\n\t\t\n\t\t# Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\t\tself._draw_finder_pattern(3, 3)\n\t\tself._draw_finder_pattern(self._size - 4, 3)\n\t\tself._draw_finder_pattern(3, self._size - 4)\n\t\t\n\t\t# Draw numerous alignment patterns\n\t\talignpatpos: list[int] = self._get_alignment_pattern_positions()\n\t\tnumalign: int = len(alignpatpos)\n\t\tskips: Sequence[tuple[int,int]] = ((0, 0), (0, numalign - 1), (numalign - 1, 0))\n\t\tfor i in range(numalign):\n\t\t\tfor j in range(numalign):\n\t\t\t\tif (i, j) not in skips:  # Don't draw on the three finder corners\n\t\t\t\t\tself._draw_alignment_pattern(alignpatpos[i], alignpatpos[j])\n\t\t\n\t\t# Draw configuration data\n\t\tself._draw_format_bits(0)  # Dummy mask value; overwritten later in the constructor\n\t\tself._draw_version()\n\t\n\t\n\tdef _draw_format_bits(self, mask: int) -> None:\n\t\t\"\"\"Draws two copies of the format bits (with its own error correction code)\n\t\tbased on the given mask and this object's error correction level field.\"\"\"\n\t\t# Calculate error correction code and pack bits\n\t\tdata: int = self._errcorlvl.formatbits << 3 | mask  # errCorrLvl is uint2, mask is uint3\n\t\trem: int = data\n\t\tfor _ in range(10):\n\t\t\trem = (rem << 1) ^ ((rem >> 9) * 0x537)\n\t\tbits: int = (data << 10 | rem) ^ 0x5412  # uint15\n\t\tassert bits >> 15 == 0\n\t\t\n\t\t# Draw first copy\n\t\tfor i in range(0, 6):\n\t\t\tself._set_function_module(8, i, _get_bit(bits, i))\n\t\tself._set_function_module(8, 7, _get_bit(bits, 6))\n\t\tself._set_function_module(8, 8, _get_bit(bits, 7))\n\t\tself._set_function_module(7, 8, _get_bit(bits, 8))\n\t\tfor i in range(9, 15):\n\t\t\tself._set_function_module(14 - i, 8, _get_bit(bits, i))\n\t\t\n\t\t# Draw second copy\n\t\tfor i in range(0, 8):\n\t\t\tself._set_function_module(self._size - 1 - i, 8, _get_bit(bits, i))\n\t\tfor i in range(8, 15):\n\t\t\tself._set_function_module(8, self._size - 15 + i, _get_bit(bits, i))\n\t\tself._set_function_module(8, self._size - 8, True)  # Always dark\n\t\n\t\n\tdef _draw_version(self) -> None:\n\t\t\"\"\"Draws two copies of the version bits (with its own error correction code),\n\t\tbased on this object's version field, iff 7 <= version <= 40.\"\"\"\n\t\tif self._version < 7:\n\t\t\treturn\n\t\t\n\t\t# Calculate error correction code and pack bits\n\t\trem: int = self._version  # version is uint6, in the range [7, 40]\n\t\tfor _ in range(12):\n\t\t\trem = (rem << 1) ^ ((rem >> 11) * 0x1F25)\n\t\tbits: int = self._version << 12 | rem  # uint18\n\t\tassert bits >> 18 == 0\n\t\t\n\t\t# Draw two copies\n\t\tfor i in range(18):\n\t\t\tbit: bool = _get_bit(bits, i)\n\t\t\ta: int = self._size - 11 + i % 3\n\t\t\tb: int = i // 3\n\t\t\tself._set_function_module(a, b, bit)\n\t\t\tself._set_function_module(b, a, bit)\n\t\n\t\n\tdef _draw_finder_pattern(self, x: int, y: int) -> None:\n\t\t\"\"\"Draws a 9*9 finder pattern including the border separator,\n\t\twith the center module at (x, y). Modules can be out of bounds.\"\"\"\n\t\tfor dy in range(-4, 5):\n\t\t\tfor dx in range(-4, 5):\n\t\t\t\txx, yy = x + dx, y + dy\n\t\t\t\tif (0 <= xx < self._size) and (0 <= yy < self._size):\n\t\t\t\t\t# Chebyshev/infinity norm\n\t\t\t\t\tself._set_function_module(xx, yy, max(abs(dx), abs(dy)) not in (2, 4))\n\t\n\t\n\tdef _draw_alignment_pattern(self, x: int, y: int) -> None:\n\t\t\"\"\"Draws a 5*5 alignment pattern, with the center module\n\t\tat (x, y). All modules must be in bounds.\"\"\"\n\t\tfor dy in range(-2, 3):\n\t\t\tfor dx in range(-2, 3):\n\t\t\t\tself._set_function_module(x + dx, y + dy, max(abs(dx), abs(dy)) != 1)\n\t\n\t\n\tdef _set_function_module(self, x: int, y: int, isdark: bool) -> None:\n\t\t\"\"\"Sets the color of a module and marks it as a function module.\n\t\tOnly used by the constructor. Coordinates must be in bounds.\"\"\"\n\t\tassert type(isdark) is bool\n\t\tself._modules[y][x] = isdark\n\t\tself._isfunction[y][x] = True\n\t\n\t\n\t# ---- Private helper methods for constructor: Codewords and masking ----\n\t\n\tdef _add_ecc_and_interleave(self, data: bytearray) -> bytes:\n\t\t\"\"\"Returns a new byte string representing the given data with the appropriate error correction\n\t\tcodewords appended to it, based on this object's version and error correction level.\"\"\"\n\t\tversion: int = self._version\n\t\tassert len(data) == QrCode._get_num_data_codewords(version, self._errcorlvl)\n\t\t\n\t\t# Calculate parameter numbers\n\t\tnumblocks: int = QrCode._NUM_ERROR_CORRECTION_BLOCKS[self._errcorlvl.ordinal][version]\n\t\tblockecclen: int = QrCode._ECC_CODEWORDS_PER_BLOCK  [self._errcorlvl.ordinal][version]\n\t\trawcodewords: int = QrCode._get_num_raw_data_modules(version) // 8\n\t\tnumshortblocks: int = numblocks - rawcodewords % numblocks\n\t\tshortblocklen: int = rawcodewords // numblocks\n\t\t\n\t\t# Split data into blocks and append ECC to each block\n\t\tblocks: list[bytes] = []\n\t\trsdiv: bytes = QrCode._reed_solomon_compute_divisor(blockecclen)\n\t\tk: int = 0\n\t\tfor i in range(numblocks):\n\t\t\tdat: bytearray = data[k : k + shortblocklen - blockecclen + (0 if i < numshortblocks else 1)]\n\t\t\tk += len(dat)\n\t\t\tecc: bytes = QrCode._reed_solomon_compute_remainder(dat, rsdiv)\n\t\t\tif i < numshortblocks:\n\t\t\t\tdat.append(0)\n\t\t\tblocks.append(dat + ecc)\n\t\tassert k == len(data)\n\t\t\n\t\t# Interleave (not concatenate) the bytes from every block into a single sequence\n\t\tresult = bytearray()\n\t\tfor i in range(len(blocks[0])):\n\t\t\tfor (j, blk) in enumerate(blocks):\n\t\t\t\t# Skip the padding byte in short blocks\n\t\t\t\tif (i != shortblocklen - blockecclen) or (j >= numshortblocks):\n\t\t\t\t\tresult.append(blk[i])\n\t\tassert len(result) == rawcodewords\n\t\treturn result\n\t\n\t\n\tdef _draw_codewords(self, data: bytes) -> None:\n\t\t\"\"\"Draws the given sequence of 8-bit codewords (data and error correction) onto the entire\n\t\tdata area of this QR Code. Function modules need to be marked off before this is called.\"\"\"\n\t\tassert len(data) == QrCode._get_num_raw_data_modules(self._version) // 8\n\t\t\n\t\ti: int = 0  # Bit index into the data\n\t\t# Do the funny zigzag scan\n\t\tfor right in range(self._size - 1, 0, -2):  # Index of right column in each column pair\n\t\t\tif right <= 6:\n\t\t\t\tright -= 1\n\t\t\tfor vert in range(self._size):  # Vertical counter\n\t\t\t\tfor j in range(2):\n\t\t\t\t\tx: int = right - j  # Actual x coordinate\n\t\t\t\t\tupward: bool = (right + 1) & 2 == 0\n\t\t\t\t\ty: int = (self._size - 1 - vert) if upward else vert  # Actual y coordinate\n\t\t\t\t\tif (not self._isfunction[y][x]) and (i < len(data) * 8):\n\t\t\t\t\t\tself._modules[y][x] = _get_bit(data[i >> 3], 7 - (i & 7))\n\t\t\t\t\t\ti += 1\n\t\t\t\t\t# If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t\t# 0/false/light by the constructor and are left unchanged by this method\n\t\tassert i == len(data) * 8\n\t\n\t\n\tdef _apply_mask(self, mask: int) -> None:\n\t\t\"\"\"XORs the codeword modules in this QR Code with the given mask pattern.\n\t\tThe function modules must be marked and the codeword bits must be drawn\n\t\tbefore masking. Due to the arithmetic of XOR, calling _apply_mask() with\n\t\tthe same mask value a second time will undo the mask. A final well-formed\n\t\tQR Code needs exactly one (not zero, two, etc.) mask applied.\"\"\"\n\t\tif not (0 <= mask <= 7):\n\t\t\traise ValueError(\"Mask value out of range\")\n\t\tmasker: collections.abc.Callable[[int,int],int] = QrCode._MASK_PATTERNS[mask]\n\t\tfor y in range(self._size):\n\t\t\tfor x in range(self._size):\n\t\t\t\tself._modules[y][x] ^= (masker(x, y) == 0) and (not self._isfunction[y][x])\n\t\n\t\n\tdef _get_penalty_score(self) -> int:\n\t\t\"\"\"Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t\tThis is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\"\"\"\n\t\tresult: int = 0\n\t\tsize: int = self._size\n\t\tmodules: list[list[bool]] = self._modules\n\t\t\n\t\t# Adjacent modules in row having same color, and finder-like patterns\n\t\tfor y in range(size):\n\t\t\truncolor: bool = False\n\t\t\trunx: int = 0\n\t\t\trunhistory = collections.deque([0] * 7, 7)\n\t\t\tfor x in range(size):\n\t\t\t\tif modules[y][x] == runcolor:\n\t\t\t\t\trunx += 1\n\t\t\t\t\tif runx == 5:\n\t\t\t\t\t\tresult += QrCode._PENALTY_N1\n\t\t\t\t\telif runx > 5:\n\t\t\t\t\t\tresult += 1\n\t\t\t\telse:\n\t\t\t\t\tself._finder_penalty_add_history(runx, runhistory)\n\t\t\t\t\tif not runcolor:\n\t\t\t\t\t\tresult += self._finder_penalty_count_patterns(runhistory) * QrCode._PENALTY_N3\n\t\t\t\t\truncolor = modules[y][x]\n\t\t\t\t\trunx = 1\n\t\t\tresult += self._finder_penalty_terminate_and_count(runcolor, runx, runhistory) * QrCode._PENALTY_N3\n\t\t# Adjacent modules in column having same color, and finder-like patterns\n\t\tfor x in range(size):\n\t\t\truncolor = False\n\t\t\truny: int = 0\n\t\t\trunhistory = collections.deque([0] * 7, 7)\n\t\t\tfor y in range(size):\n\t\t\t\tif modules[y][x] == runcolor:\n\t\t\t\t\truny += 1\n\t\t\t\t\tif runy == 5:\n\t\t\t\t\t\tresult += QrCode._PENALTY_N1\n\t\t\t\t\telif runy > 5:\n\t\t\t\t\t\tresult += 1\n\t\t\t\telse:\n\t\t\t\t\tself._finder_penalty_add_history(runy, runhistory)\n\t\t\t\t\tif not runcolor:\n\t\t\t\t\t\tresult += self._finder_penalty_count_patterns(runhistory) * QrCode._PENALTY_N3\n\t\t\t\t\truncolor = modules[y][x]\n\t\t\t\t\truny = 1\n\t\t\tresult += self._finder_penalty_terminate_and_count(runcolor, runy, runhistory) * QrCode._PENALTY_N3\n\t\t\n\t\t# 2*2 blocks of modules having same color\n\t\tfor y in range(size - 1):\n\t\t\tfor x in range(size - 1):\n\t\t\t\tif modules[y][x] == modules[y][x + 1] == modules[y + 1][x] == modules[y + 1][x + 1]:\n\t\t\t\t\tresult += QrCode._PENALTY_N2\n\t\t\n\t\t# Balance of dark and light modules\n\t\tdark: int = sum((1 if cell else 0) for row in modules for cell in row)\n\t\ttotal: int = size**2  # Note that size is odd, so dark/total != 1/2\n\t\t# Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\t\tk: int = (abs(dark * 20 - total * 10) + total - 1) // total - 1\n\t\tassert 0 <= k <= 9\n\t\tresult += k * QrCode._PENALTY_N4\n\t\tassert 0 <= result <= 2568888  # Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\t\treturn result\n\t\n\t\n\t# ---- Private helper functions ----\n\t\n\tdef _get_alignment_pattern_positions(self) -> list[int]:\n\t\t\"\"\"Returns an ascending list of positions of alignment patterns for this version number.\n\t\tEach position is in the range [0,177), and are used on both the x and y axes.\n\t\tThis could be implemented as lookup table of 40 variable-length lists of integers.\"\"\"\n\t\tif self._version == 1:\n\t\t\treturn []\n\t\telse:\n\t\t\tnumalign: int = self._version // 7 + 2\n\t\t\tstep: int = (self._version * 8 + numalign * 3 + 5) // (numalign * 4 - 4) * 2\n\t\t\tresult: list[int] = [(self._size - 7 - i * step) for i in range(numalign - 1)] + [6]\n\t\t\treturn list(reversed(result))\n\t\n\t\n\t@staticmethod\n\tdef _get_num_raw_data_modules(ver: int) -> int:\n\t\t\"\"\"Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t\tall function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t\tThe result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\"\"\"\n\t\tif not (QrCode.MIN_VERSION <= ver <= QrCode.MAX_VERSION):\n\t\t\traise ValueError(\"Version number out of range\")\n\t\tresult: int = (16 * ver + 128) * ver + 64\n\t\tif ver >= 2:\n\t\t\tnumalign: int = ver // 7 + 2\n\t\t\tresult -= (25 * numalign - 10) * numalign - 55\n\t\t\tif ver >= 7:\n\t\t\t\tresult -= 36\n\t\tassert 208 <= result <= 29648\n\t\treturn result\n\t\n\t\n\t@staticmethod\n\tdef _get_num_data_codewords(ver: int, ecl: QrCode.Ecc) -> int:\n\t\t\"\"\"Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t\tQR Code of the given version number and error correction level, with remainder bits discarded.\n\t\tThis stateless pure function could be implemented as a (40*4)-cell lookup table.\"\"\"\n\t\treturn QrCode._get_num_raw_data_modules(ver) // 8 \\\n\t\t\t- QrCode._ECC_CODEWORDS_PER_BLOCK    [ecl.ordinal][ver] \\\n\t\t\t* QrCode._NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]\n\t\n\t\n\t@staticmethod\n\tdef _reed_solomon_compute_divisor(degree: int) -> bytes:\n\t\t\"\"\"Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be\n\t\timplemented as a lookup table over all possible parameter values, instead of as an algorithm.\"\"\"\n\t\tif not (1 <= degree <= 255):\n\t\t\traise ValueError(\"Degree out of range\")\n\t\t# Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t\t# For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].\n\t\tresult = bytearray([0] * (degree - 1) + [1])  # Start off with the monomial x^0\n\t\t\n\t\t# Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t\t# and drop the highest monomial term which is always 1x^degree.\n\t\t# Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\t\troot: int = 1\n\t\tfor _ in range(degree):  # Unused variable i\n\t\t\t# Multiply the current product by (x - r^i)\n\t\t\tfor j in range(degree):\n\t\t\t\tresult[j] = QrCode._reed_solomon_multiply(result[j], root)\n\t\t\t\tif j + 1 < degree:\n\t\t\t\t\tresult[j] ^= result[j + 1]\n\t\t\troot = QrCode._reed_solomon_multiply(root, 0x02)\n\t\treturn result\n\t\n\t\n\t@staticmethod\n\tdef _reed_solomon_compute_remainder(data: bytes, divisor: bytes) -> bytes:\n\t\t\"\"\"Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.\"\"\"\n\t\tresult = bytearray([0] * len(divisor))\n\t\tfor b in data:  # Polynomial division\n\t\t\tfactor: int = b ^ result.pop(0)\n\t\t\tresult.append(0)\n\t\t\tfor (i, coef) in enumerate(divisor):\n\t\t\t\tresult[i] ^= QrCode._reed_solomon_multiply(coef, factor)\n\t\treturn result\n\t\n\t\n\t@staticmethod\n\tdef _reed_solomon_multiply(x: int, y: int) -> int:\n\t\t\"\"\"Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result\n\t\tare unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.\"\"\"\n\t\tif (x >> 8 != 0) or (y >> 8 != 0):\n\t\t\traise ValueError(\"Byte out of range\")\n\t\t# Russian peasant multiplication\n\t\tz: int = 0\n\t\tfor i in reversed(range(8)):\n\t\t\tz = (z << 1) ^ ((z >> 7) * 0x11D)\n\t\t\tz ^= ((y >> i) & 1) * x\n\t\tassert z >> 8 == 0\n\t\treturn z\n\t\n\t\n\tdef _finder_penalty_count_patterns(self, runhistory: collections.deque[int]) -> int:\n\t\t\"\"\"Can only be called immediately after a light run is added, and\n\t\treturns either 0, 1, or 2. A helper function for _get_penalty_score().\"\"\"\n\t\tn: int = runhistory[1]\n\t\tassert n <= self._size * 3\n\t\tcore: bool = n > 0 and (runhistory[2] == runhistory[4] == runhistory[5] == n) and runhistory[3] == n * 3\n\t\treturn (1 if (core and runhistory[0] >= n * 4 and runhistory[6] >= n) else 0) \\\n\t\t     + (1 if (core and runhistory[6] >= n * 4 and runhistory[0] >= n) else 0)\n\t\n\t\n\tdef _finder_penalty_terminate_and_count(self, currentruncolor: bool, currentrunlength: int, runhistory: collections.deque[int]) -> int:\n\t\t\"\"\"Must be called at the end of a line (row or column) of modules. A helper function for _get_penalty_score().\"\"\"\n\t\tif currentruncolor:  # Terminate dark run\n\t\t\tself._finder_penalty_add_history(currentrunlength, runhistory)\n\t\t\tcurrentrunlength = 0\n\t\tcurrentrunlength += self._size  # Add light border to final run\n\t\tself._finder_penalty_add_history(currentrunlength, runhistory)\n\t\treturn self._finder_penalty_count_patterns(runhistory)\n\t\n\t\n\tdef _finder_penalty_add_history(self, currentrunlength: int, runhistory: collections.deque[int]) -> None:\n\t\tif runhistory[0] == 0:\n\t\t\tcurrentrunlength += self._size  # Add light border to initial run\n\t\trunhistory.appendleft(currentrunlength)\n\t\n\t\n\t# ---- Constants and tables ----\n\t\n\tMIN_VERSION: int =  1  # The minimum version number supported in the QR Code Model 2 standard\n\tMAX_VERSION: int = 40  # The maximum version number supported in the QR Code Model 2 standard\n\t\n\t# For use in _get_penalty_score(), when evaluating which mask is best.\n\t_PENALTY_N1: int =  3\n\t_PENALTY_N2: int =  3\n\t_PENALTY_N3: int = 40\n\t_PENALTY_N4: int = 10\n\t\n\t_ECC_CODEWORDS_PER_BLOCK: Sequence[Sequence[int]] = (\n\t\t# Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t# 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\n\t\t(-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\n\t\t(-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28),  # Medium\n\t\t(-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30),  # Quartile\n\t\t(-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30))  # High\n\t\n\t_NUM_ERROR_CORRECTION_BLOCKS: Sequence[Sequence[int]] = (\n\t\t# Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t# 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\n\t\t(-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25),  # Low\n\t\t(-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49),  # Medium\n\t\t(-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68),  # Quartile\n\t\t(-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81))  # High\n\t\n\t_MASK_PATTERNS: Sequence[collections.abc.Callable[[int,int],int]] = (\n\t\t(lambda x, y:  (x + y) % 2                  ),\n\t\t(lambda x, y:  y % 2                        ),\n\t\t(lambda x, y:  x % 3                        ),\n\t\t(lambda x, y:  (x + y) % 3                  ),\n\t\t(lambda x, y:  (x // 3 + y // 2) % 2        ),\n\t\t(lambda x, y:  x * y % 2 + x * y % 3        ),\n\t\t(lambda x, y:  (x * y % 2 + x * y % 3) % 2  ),\n\t\t(lambda x, y:  ((x + y) % 2 + x * y % 3) % 2),\n\t)\n\t\n\t\n\t# ---- Public helper enumeration ----\n\t\n\tclass Ecc:\n\t\tordinal: int  # (Public) In the range 0 to 3 (unsigned 2-bit integer)\n\t\tformatbits: int  # (Package-private) In the range 0 to 3 (unsigned 2-bit integer)\n\t\t\n\t\t\"\"\"The error correction level in a QR Code symbol. Immutable.\"\"\"\n\t\t# Private constructor\n\t\tdef __init__(self, i: int, fb: int) -> None:\n\t\t\tself.ordinal = i\n\t\t\tself.formatbits = fb\n\t\t\n\t\t# Placeholders\n\t\tLOW     : QrCode.Ecc\n\t\tMEDIUM  : QrCode.Ecc\n\t\tQUARTILE: QrCode.Ecc\n\t\tHIGH    : QrCode.Ecc\n\t\n\t# Public constants. Create them outside the class.\n\tEcc.LOW      = Ecc(0, 1)  # The QR Code can tolerate about  7% erroneous codewords\n\tEcc.MEDIUM   = Ecc(1, 0)  # The QR Code can tolerate about 15% erroneous codewords\n\tEcc.QUARTILE = Ecc(2, 3)  # The QR Code can tolerate about 25% erroneous codewords\n\tEcc.HIGH     = Ecc(3, 2)  # The QR Code can tolerate about 30% erroneous codewords\n\n\n\n# ---- Data segment class ----\n\nclass QrSegment:\n\t\"\"\"A segment of character/binary/control data in a QR Code symbol.\n\tInstances of this class are immutable.\n\tThe mid-level way to create a segment is to take the payload data\n\tand call a static factory function such as QrSegment.make_numeric().\n\tThe low-level way to create a segment is to custom-make the bit buffer\n\tand call the QrSegment() constructor with appropriate values.\n\tThis segment class imposes no length restrictions, but QR Codes have restrictions.\n\tEven in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n\tAny segment longer than this is meaningless for the purpose of generating QR Codes.\"\"\"\n\t\n\t# ---- Static factory functions (mid level) ----\n\t\n\t@staticmethod\n\tdef make_bytes(data: Union[bytes,Sequence[int]]) -> QrSegment:\n\t\t\"\"\"Returns a segment representing the given binary data encoded in byte mode.\n\t\tAll input byte lists are acceptable. Any text string can be converted to\n\t\tUTF-8 bytes (s.encode(\"UTF-8\")) and encoded as a byte mode segment.\"\"\"\n\t\tbb = _BitBuffer()\n\t\tfor b in data:\n\t\t\tbb.append_bits(b, 8)\n\t\treturn QrSegment(QrSegment.Mode.BYTE, len(data), bb)\n\t\n\t\n\t@staticmethod\n\tdef make_numeric(digits: str) -> QrSegment:\n\t\t\"\"\"Returns a segment representing the given string of decimal digits encoded in numeric mode.\"\"\"\n\t\tif not QrSegment.is_numeric(digits):\n\t\t\traise ValueError(\"String contains non-numeric characters\")\n\t\tbb = _BitBuffer()\n\t\ti: int = 0\n\t\twhile i < len(digits):  # Consume up to 3 digits per iteration\n\t\t\tn: int = min(len(digits) - i, 3)\n\t\t\tbb.append_bits(int(digits[i : i + n]), n * 3 + 1)\n\t\t\ti += n\n\t\treturn QrSegment(QrSegment.Mode.NUMERIC, len(digits), bb)\n\t\n\t\n\t@staticmethod\n\tdef make_alphanumeric(text: str) -> QrSegment:\n\t\t\"\"\"Returns a segment representing the given text string encoded in alphanumeric mode.\n\t\tThe characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t\tdollar, percent, asterisk, plus, hyphen, period, slash, colon.\"\"\"\n\t\tif not QrSegment.is_alphanumeric(text):\n\t\t\traise ValueError(\"String contains unencodable characters in alphanumeric mode\")\n\t\tbb = _BitBuffer()\n\t\tfor i in range(0, len(text) - 1, 2):  # Process groups of 2\n\t\t\ttemp: int = QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[i]] * 45\n\t\t\ttemp += QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[i + 1]]\n\t\t\tbb.append_bits(temp, 11)\n\t\tif len(text) % 2 > 0:  # 1 character remaining\n\t\t\tbb.append_bits(QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[-1]], 6)\n\t\treturn QrSegment(QrSegment.Mode.ALPHANUMERIC, len(text), bb)\n\t\n\t\n\t@staticmethod\n\tdef make_segments(text: str) -> list[QrSegment]:\n\t\t\"\"\"Returns a new mutable list of zero or more segments to represent the given Unicode text string.\n\t\tThe result may use various segment modes and switch modes to optimize the length of the bit stream.\"\"\"\n\t\t\n\t\t# Select the most efficient segment encoding automatically\n\t\tif text == \"\":\n\t\t\treturn []\n\t\telif QrSegment.is_numeric(text):\n\t\t\treturn [QrSegment.make_numeric(text)]\n\t\telif QrSegment.is_alphanumeric(text):\n\t\t\treturn [QrSegment.make_alphanumeric(text)]\n\t\telse:\n\t\t\treturn [QrSegment.make_bytes(text.encode(\"UTF-8\"))]\n\t\n\t\n\t@staticmethod\n\tdef make_eci(assignval: int) -> QrSegment:\n\t\t\"\"\"Returns a segment representing an Extended Channel Interpretation\n\t\t(ECI) designator with the given assignment value.\"\"\"\n\t\tbb = _BitBuffer()\n\t\tif assignval < 0:\n\t\t\traise ValueError(\"ECI assignment value out of range\")\n\t\telif assignval < (1 << 7):\n\t\t\tbb.append_bits(assignval, 8)\n\t\telif assignval < (1 << 14):\n\t\t\tbb.append_bits(0b10, 2)\n\t\t\tbb.append_bits(assignval, 14)\n\t\telif assignval < 1000000:\n\t\t\tbb.append_bits(0b110, 3)\n\t\t\tbb.append_bits(assignval, 21)\n\t\telse:\n\t\t\traise ValueError(\"ECI assignment value out of range\")\n\t\treturn QrSegment(QrSegment.Mode.ECI, 0, bb)\n\t\n\t\n\t# Tests whether the given string can be encoded as a segment in numeric mode.\n\t# A string is encodable iff each character is in the range 0 to 9.\n\t@staticmethod\n\tdef is_numeric(text: str) -> bool:\n\t\treturn QrSegment._NUMERIC_REGEX.fullmatch(text) is not None\n\t\n\t\n\t# Tests whether the given string can be encoded as a segment in alphanumeric mode.\n\t# A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t# (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t@staticmethod\n\tdef is_alphanumeric(text: str) -> bool:\n\t\treturn QrSegment._ALPHANUMERIC_REGEX.fullmatch(text) is not None\n\t\n\t\n\t# ---- Private fields ----\n\t\n\t# The mode indicator of this segment. Accessed through get_mode().\n\t_mode: QrSegment.Mode\n\t\n\t# The length of this segment's unencoded data. Measured in characters for\n\t# numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t# Always zero or positive. Not the same as the data's bit length.\n\t# Accessed through get_num_chars().\n\t_numchars: int\n\t\n\t# The data bits of this segment. Accessed through get_data().\n\t_bitdata: list[int]\n\t\n\t\n\t# ---- Constructor (low level) ----\n\t\n\tdef __init__(self, mode: QrSegment.Mode, numch: int, bitdata: Sequence[int]) -> None:\n\t\t\"\"\"Creates a new QR Code segment with the given attributes and data.\n\t\tThe character count (numch) must agree with the mode and the bit buffer length,\n\t\tbut the constraint isn't checked. The given bit buffer is cloned and stored.\"\"\"\n\t\tif numch < 0:\n\t\t\traise ValueError()\n\t\tself._mode = mode\n\t\tself._numchars = numch\n\t\tself._bitdata = list(bitdata)  # Make defensive copy\n\t\n\t\n\t# ---- Accessor methods ----\n\t\n\tdef get_mode(self) -> QrSegment.Mode:\n\t\t\"\"\"Returns the mode field of this segment.\"\"\"\n\t\treturn self._mode\n\t\n\tdef get_num_chars(self) -> int:\n\t\t\"\"\"Returns the character count field of this segment.\"\"\"\n\t\treturn self._numchars\n\t\n\tdef get_data(self) -> list[int]:\n\t\t\"\"\"Returns a new copy of the data bits of this segment.\"\"\"\n\t\treturn list(self._bitdata)  # Make defensive copy\n\t\n\t\n\t# Package-private function\n\t@staticmethod\n\tdef get_total_bits(segs: Sequence[QrSegment], version: int) -> Optional[int]:\n\t\t\"\"\"Calculates the number of bits needed to encode the given segments at\n\t\tthe given version. Returns a non-negative number if successful. Otherwise\n\t\treturns None if a segment has too many characters to fit its length field.\"\"\"\n\t\tresult = 0\n\t\tfor seg in segs:\n\t\t\tccbits: int = seg.get_mode().num_char_count_bits(version)\n\t\t\tif seg.get_num_chars() >= (1 << ccbits):\n\t\t\t\treturn None  # The segment's length doesn't fit the field's bit width\n\t\t\tresult += 4 + ccbits + len(seg._bitdata)\n\t\treturn result\n\t\n\t\n\t# ---- Constants ----\n\t\n\t# Describes precisely all strings that are encodable in numeric mode.\n\t_NUMERIC_REGEX: re.Pattern[str] = re.compile(r\"[0-9]*\")\n\t\n\t# Describes precisely all strings that are encodable in alphanumeric mode.\n\t_ALPHANUMERIC_REGEX: re.Pattern[str] = re.compile(r\"[A-Z0-9 $%*+./:-]*\")\n\t\n\t# Dictionary of \"0\"->0, \"A\"->10, \"$\"->37, etc.\n\t_ALPHANUMERIC_ENCODING_TABLE: dict[str,int] = {ch: i for (i, ch) in enumerate(\"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\")}\n\t\n\t\n\t# ---- Public helper enumeration ----\n\t\n\tclass Mode:\n\t\t\"\"\"Describes how a segment's data bits are interpreted. Immutable.\"\"\"\n\t\t\n\t\t_modebits: int  # The mode indicator bits, which is a uint4 value (range 0 to 15)\n\t\t_charcounts: tuple[int,int,int]  # Number of character count bits for three different version ranges\n\t\t\n\t\t# Private constructor\n\t\tdef __init__(self, modebits: int, charcounts: tuple[int,int,int]):\n\t\t\tself._modebits = modebits\n\t\t\tself._charcounts = charcounts\n\t\t\n\t\t# Package-private method\n\t\tdef get_mode_bits(self) -> int:\n\t\t\t\"\"\"Returns an unsigned 4-bit integer value (range 0 to 15) representing the mode indicator bits for this mode object.\"\"\"\n\t\t\treturn self._modebits\n\t\t\n\t\t# Package-private method\n\t\tdef num_char_count_bits(self, ver: int) -> int:\n\t\t\t\"\"\"Returns the bit width of the character count field for a segment in this mode\n\t\t\tin a QR Code at the given version number. The result is in the range [0, 16].\"\"\"\n\t\t\treturn self._charcounts[(ver + 7) // 17]\n\t\t\n\t\t# Placeholders\n\t\tNUMERIC     : QrSegment.Mode\n\t\tALPHANUMERIC: QrSegment.Mode\n\t\tBYTE        : QrSegment.Mode\n\t\tKANJI       : QrSegment.Mode\n\t\tECI         : QrSegment.Mode\n\t\n\t# Public constants. Create them outside the class.\n\tMode.NUMERIC      = Mode(0x1, (10, 12, 14))\n\tMode.ALPHANUMERIC = Mode(0x2, ( 9, 11, 13))\n\tMode.BYTE         = Mode(0x4, ( 8, 16, 16))\n\tMode.KANJI        = Mode(0x8, ( 8, 10, 12))\n\tMode.ECI          = Mode(0x7, ( 0,  0,  0))\n\n\n\n# ---- Private helper class ----\n\nclass _BitBuffer(list[int]):\n\t\"\"\"An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.\"\"\"\n\t\n\tdef append_bits(self, val: int, n: int) -> None:\n\t\t\"\"\"Appends the given number of low-order bits of the given\n\t\tvalue to this buffer. Requires n >= 0 and 0 <= val < 2^n.\"\"\"\n\t\tif (n < 0) or (val >> n != 0):\n\t\t\traise ValueError(\"Value out of range\")\n\t\tself.extend(((val >> i) & 1) for i in reversed(range(n)))\n\n\ndef _get_bit(x: int, i: int) -> bool:\n\t\"\"\"Returns true iff the i'th bit of x is set to 1.\"\"\"\n\treturn (x >> i) & 1 != 0\n\n\n\nclass DataTooLongError(ValueError):\n\t\"\"\"Raised when the supplied data does not fit any QR Code version. Ways to handle this exception include:\n\t- Decrease the error correction level if it was greater than Ecc.LOW.\n\t- If the encode_segments() function was called with a maxversion argument, then increase\n\t  it if it was less than QrCode.MAX_VERSION. (This advice does not apply to the other\n\t  factory functions because they search all versions up to QrCode.MAX_VERSION.)\n\t- Split the text data into better or optimal segments in order to reduce the number of bits required.\n\t- Change the text or binary data to be shorter.\n\t- Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).\n\t- Propagate the error upward to the caller/user.\"\"\"\n\tpass\n"
  },
  {
    "path": "python/setup.py",
    "content": "# \n# QR Code generator Distutils script (Python)\n# \n# Copyright (c) Project Nayuki. (MIT License)\n# https://www.nayuki.io/page/qr-code-generator-library\n# \n# Permission is hereby granted, free of charge, to any person obtaining a copy of\n# this software and associated documentation files (the \"Software\"), to deal in\n# the Software without restriction, including without limitation the rights to\n# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n# the Software, and to permit persons to whom the Software is furnished to do so,\n# subject to the following conditions:\n# - The above copyright notice and this permission notice shall be included in\n#   all copies or substantial portions of the Software.\n# - The Software is provided \"as is\", without warranty of any kind, express or\n#   implied, including but not limited to the warranties of merchantability,\n#   fitness for a particular purpose and noninfringement. In no event shall the\n#   authors or copyright holders be liable for any claim, damages or other\n#   liability, whether in an action of contract, tort or otherwise, arising from,\n#   out of or in connection with the Software or the use or other dealings in the\n#   Software.\n# \n\nimport setuptools\n\n\nsetuptools.setup(\n\tname = \"qrcodegen\",\n\tdescription = \"High quality QR Code generator library for Python\",\n\tversion = \"1.8.0\",\n\tplatforms = \"OS Independent\",\n\tpython_requires = '>=3',\n\tlicense = \"MIT License\",\n\t\n\tauthor = \"Project Nayuki\",\n\tauthor_email = \"me@nayuki.io\",\n\turl = \"https://www.nayuki.io/page/qr-code-generator-library\",\n\t\n\tclassifiers = [\n\t\t\"Development Status :: 5 - Production/Stable\",\n\t\t\"Intended Audience :: Developers\",\n\t\t\"Intended Audience :: Information Technology\",\n\t\t\"License :: OSI Approved :: MIT License\",\n\t\t\"Operating System :: OS Independent\",\n\t\t\"Programming Language :: Python\",\n\t\t\"Programming Language :: Python :: 3\",\n\t\t\"Topic :: Multimedia :: Graphics\",\n\t\t\"Topic :: Software Development :: Libraries :: Python Modules\",\n\t],\n\t\n\tlong_description = \"\"\"=========================\nQR Code generator library\n=========================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n::\n\n    from qrcodegen import *\n    \n    # Simple operation\n    qr0 = QrCode.encode_text(\"Hello, world!\", QrCode.Ecc.MEDIUM)\n    svg = to_svg_str(qr0, 4)  # See qrcodegen-demo\n    \n    # Manual operation\n    segs = QrSegment.make_segments(\"3141592653589793238462643383\")\n    qr1 = QrCode.encode_segments(segs, QrCode.Ecc.HIGH, 5, 5, 2, False)\n    for y in range(qr1.get_size()):\n        for x in range(qr1.get_size()):\n            (... paint qr1.get_module(x, y) ...)\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/python/qrcodegen-demo.py .\"\"\",\n\t\n\tpy_modules = [\"qrcodegen\"],\n)\n"
  },
  {
    "path": "rust/Cargo.toml",
    "content": "[package]\nname = \"qrcodegen\"\nversion = \"1.8.0\"\nauthors = [\"Project Nayuki\"]\ndescription = \"High-quality QR Code generator library\"\nhomepage = \"https://www.nayuki.io/page/qr-code-generator-library\"\nrepository = \"https://github.com/nayuki/QR-Code-generator\"\nreadme = \"Readme.markdown\"\nkeywords = [\"qr-code\", \"barcode\", \"encoder\", \"image\"]\ncategories = [\"encoding\", \"multimedia::images\"]\nlicense = \"MIT\"\nexclude = [\"examples/*\"]\n"
  },
  {
    "path": "rust/Readme.markdown",
    "content": "QR Code generator library - Rust\n================================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```rust\nextern crate qrcodegen;\nuse qrcodegen::Mask;\nuse qrcodegen::QrCode;\nuse qrcodegen::QrCodeEcc;\nuse qrcodegen::QrSegment;\nuse qrcodegen::Version;\n\n// Simple operation\nlet qr = QrCode::encode_text(\"Hello, world!\",\n    QrCodeEcc::Medium).unwrap();\nlet svg = to_svg_string(&qr, 4);  // See qrcodegen-demo\n\n// Manual operation\nlet text: &str = \"3141592653589793238462643383\";\nlet segs = QrSegment::make_segments(text);\nlet qr = QrCode::encode_segments_advanced(&segs,\n    QrCodeEcc::High, Version::new(5), Version::new(5),\n    Some(Mask::new(2)), false).unwrap();\nfor y in 0 .. qr.size() {\n    for x in 0 .. qr.size() {\n        (... paint qr.get_module(x, y) ...)\n    }\n}\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/rust/examples/qrcodegen-demo.rs .\n"
  },
  {
    "path": "rust/examples/qrcodegen-demo.rs",
    "content": "/* \n * QR Code generator demo (Rust)\n * \n * Run this command-line program with no arguments. The program computes a bunch of demonstration\n * QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\nextern crate qrcodegen;\nuse qrcodegen::Mask;\nuse qrcodegen::QrCode;\nuse qrcodegen::QrCodeEcc;\nuse qrcodegen::QrSegment;\nuse qrcodegen::Version;\n\n\n// The main application program.\nfn main() {\n\tdo_basic_demo();\n\tdo_variety_demo();\n\tdo_segment_demo();\n\tdo_mask_demo();\n}\n\n\n\n/*---- Demo suite ----*/\n\n// Creates a single QR Code, then prints it to the console.\nfn do_basic_demo() {\n\tlet text: &'static str = \"Hello, world!\";   // User-supplied Unicode text\n\tlet errcorlvl: QrCodeEcc = QrCodeEcc::Low;  // Error correction level\n\t\n\t// Make and print the QR Code symbol\n\tlet qr: QrCode = QrCode::encode_text(text, errcorlvl).unwrap();\n\tprint_qr(&qr);\n\tprintln!(\"{}\", to_svg_string(&qr, 4));\n}\n\n\n// Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.\nfn do_variety_demo() {\n\t// Numeric mode encoding (3.33 bits per digit)\n\tlet qr = QrCode::encode_text(\"314159265358979323846264338327950288419716939937510\", QrCodeEcc::Medium).unwrap();\n\tprint_qr(&qr);\n\t\n\t// Alphanumeric mode encoding (5.5 bits per character)\n\tlet qr = QrCode::encode_text(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", QrCodeEcc::High).unwrap();\n\tprint_qr(&qr);\n\t\n\t// Unicode text as UTF-8\n\tlet qr = QrCode::encode_text(\"こんにちwa、世界！ αβγδ\", QrCodeEcc::Quartile).unwrap();\n\tprint_qr(&qr);\n\t\n\t// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\tlet qr = QrCode::encode_text(concat!(\n\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \",\n\t\t\"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \",\n\t\t\"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \",\n\t\t\"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \",\n\t\t\"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \",\n\t\t\"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \",\n\t\t\"a White Rabbit with pink eyes ran close by her.\"), QrCodeEcc::High).unwrap();\n\tprint_qr(&qr);\n}\n\n\n// Creates QR Codes with manually specified segments for better compactness.\nfn do_segment_demo() {\n\t// Illustration \"silver\"\n\tlet silver0 = \"THE SQUARE ROOT OF 2 IS 1.\";\n\tlet silver1 = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\tlet qr = QrCode::encode_text(&[silver0, silver1].concat(), QrCodeEcc::Low).unwrap();\n\tprint_qr(&qr);\n\t\n\tlet segs = vec![\n\t\tQrSegment::make_alphanumeric(silver0),\n\t\tQrSegment::make_numeric(silver1),\n\t];\n\tlet qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();\n\tprint_qr(&qr);\n\t\n\t// Illustration \"golden\"\n\tlet golden0 = \"Golden ratio φ = 1.\";\n\tlet golden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\tlet golden2 = \"......\";\n\tlet qr = QrCode::encode_text(&[golden0, golden1, golden2].concat(), QrCodeEcc::Low).unwrap();\n\tprint_qr(&qr);\n\t\n\tlet segs = vec![\n\t\tQrSegment::make_bytes(golden0.as_bytes()),\n\t\tQrSegment::make_numeric(golden1),\n\t\tQrSegment::make_alphanumeric(golden2),\n\t];\n\tlet qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();\n\tprint_qr(&qr);\n\t\n\t// Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\tlet madoka = \"「魔法少女まどか☆マギカ」って、　ИАИ　ｄｅｓｕ　κα？\";\n\tlet qr = QrCode::encode_text(madoka, QrCodeEcc::Low).unwrap();\n\tprint_qr(&qr);\n\t\n\tlet kanjichars: Vec<u32> = vec![  // Kanji mode encoding (13 bits per character)\n\t\t0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,\n\t\t0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,\n\t\t0x018D, 0x018A, 0x0036, 0x0141, 0x0144,\n\t\t0x0001, 0x0000, 0x0249, 0x0240, 0x0249,\n\t\t0x0000, 0x0104, 0x0105, 0x0113, 0x0115,\n\t\t0x0000, 0x0208, 0x01FF, 0x0008,\n\t];\n\tlet mut bb = qrcodegen::BitBuffer(Vec::new());\n\tfor &c in &kanjichars {\n\t\tbb.append_bits(c, 13);\n\t}\n\tlet segs = vec![\n\t\tQrSegment::new(qrcodegen::QrSegmentMode::Kanji, kanjichars.len(), bb.0),\n\t];\n\tlet qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();\n\tprint_qr(&qr);\n}\n\n\n// Creates QR Codes with the same size and contents but different mask patterns.\nfn do_mask_demo() {\n\t// Project Nayuki URL\n\tlet segs = QrSegment::make_segments(\"https://www.nayuki.io/\");\n\tlet qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, None, true).unwrap();  // Automatic mask\n\tprint_qr(&qr);\n\tlet qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, Some(Mask::new(3)), true).unwrap();  // Force mask 3\n\tprint_qr(&qr);\n\t\n\t// Chinese text as UTF-8\n\tlet segs = QrSegment::make_segments(\"維基百科（Wikipedia，聆聽i/ˌwɪkᵻˈpiːdi.ə/）是一個自由內容、公開編輯且多語言的網路百科全書協作計畫\");\n\tlet qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(0)), true).unwrap();  // Force mask 0\n\tprint_qr(&qr);\n\tlet qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(1)), true).unwrap();  // Force mask 1\n\tprint_qr(&qr);\n\tlet qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(5)), true).unwrap();  // Force mask 5\n\tprint_qr(&qr);\n\tlet qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(7)), true).unwrap();  // Force mask 7\n\tprint_qr(&qr);\n}\n\n\n\n/*---- Utilities ----*/\n\n// Returns a string of SVG code for an image depicting\n// the given QR Code, with the given number of border modules.\n// The string always uses Unix newlines (\\n), regardless of the platform.\nfn to_svg_string(qr: &QrCode, border: i32) -> String {\n\tassert!(border >= 0, \"Border must be non-negative\");\n\tlet mut result = String::new();\n\tresult += \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";\n\tresult += \"<!DOCTYPE svg PUBLIC \\\"-//W3C//DTD SVG 1.1//EN\\\" \\\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\\">\\n\";\n\tlet dimension = qr.size().checked_add(border.checked_mul(2).unwrap()).unwrap();\n\tresult += &format!(\n\t\t\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" version=\\\"1.1\\\" viewBox=\\\"0 0 {0} {0}\\\" stroke=\\\"none\\\">\\n\", dimension);\n\tresult += \"\\t<rect width=\\\"100%\\\" height=\\\"100%\\\" fill=\\\"#FFFFFF\\\"/>\\n\";\n\tresult += \"\\t<path d=\\\"\";\n\tfor y in 0 .. qr.size() {\n\t\tfor x in 0 .. qr.size() {\n\t\t\tif qr.get_module(x, y) {\n\t\t\t\tif x != 0 || y != 0 {\n\t\t\t\t\tresult += \" \";\n\t\t\t\t}\n\t\t\t\tresult += &format!(\"M{},{}h1v1h-1z\", x + border, y + border);\n\t\t\t}\n\t\t}\n\t}\n\tresult += \"\\\" fill=\\\"#000000\\\"/>\\n\";\n\tresult += \"</svg>\\n\";\n\tresult\n}\n\n\n// Prints the given QrCode object to the console.\nfn print_qr(qr: &QrCode) {\n\tlet border: i32 = 4;\n\tfor y in -border .. qr.size() + border {\n\t\tfor x in -border .. qr.size() + border {\n\t\t\tlet c: char = if qr.get_module(x, y) { '█' } else { ' ' };\n\t\t\tprint!(\"{0}{0}\", c);\n\t\t}\n\t\tprintln!();\n\t}\n\tprintln!();\n}\n"
  },
  {
    "path": "rust/src/lib.rs",
    "content": "/* \n * QR Code generator library (Rust)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n\n//! Generates QR Codes from text strings and byte arrays.\n//! \n//! This project aims to be the best, clearest QR Code generator library.\n//! The primary goals are flexible options and absolute correctness.\n//! Secondary goals are compact implementation size and good documentation comments.\n//! \n//! Home page with live JavaScript demo, extensive descriptions, and competitor comparisons:\n//! [https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)\n//! \n//! # Features\n//! \n//! Core features:\n//! \n//! - Significantly shorter code but more documentation comments compared to competing libraries\n//! - Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n//! - Output format: Raw modules/pixels of the QR symbol\n//! - Detects finder-like penalty patterns more accurately than other implementations\n//! - Encodes numeric and special-alphanumeric text in less space than general text\n//! - Open-source code under the permissive MIT License\n//! \n//! Manual parameters:\n//! \n//! - User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n//! - User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n//! - User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n//! - User can create a list of data segments manually and add ECI segments\n//! \n//! More information about QR Code technology and this library's design can be found on the project home page.\n//! \n//! # Examples\n//! \n//! ```\n//! extern crate qrcodegen;\n//! use qrcodegen::Mask;\n//! use qrcodegen::QrCode;\n//! use qrcodegen::QrCodeEcc;\n//! use qrcodegen::QrSegment;\n//! use qrcodegen::Version;\n//! ```\n//! \n//! Simple operation:\n//! \n//! ```\n//! let qr = QrCode::encode_text(\"Hello, world!\",\n//!     QrCodeEcc::Medium).unwrap();\n//! let svg = to_svg_string(&qr, 4);  // See qrcodegen-demo\n//! ```\n//! \n//! Manual operation:\n//! \n//! ```\n//! let text: &str = \"3141592653589793238462643383\";\n//! let segs = QrSegment::make_segments(text);\n//! let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High,\n//!     Version::new(5), Version::new(5), Some(Mask::new(2)), false).unwrap();\n//! for y in 0 .. qr.size() {\n//!     for x in 0 .. qr.size() {\n//!         (... paint qr.get_module(x, y) ...)\n//!     }\n//! }\n//! ```\n\n\n#![forbid(unsafe_code)]\nuse std::convert::TryFrom;\n\n\n/*---- QrCode functionality ----*/\n\n/// A QR Code symbol, which is a type of two-dimension barcode.\n/// \n/// Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n/// \n/// Instances of this struct represent an immutable square grid of dark and light cells.\n/// The impl provides static factory functions to create a QR Code from text or binary data.\n/// The struct and impl cover the QR Code Model 2 specification, supporting all versions\n/// (sizes) from 1 to 40, all 4 error correction levels, and 4 character encoding modes.\n/// \n/// Ways to create a QR Code object:\n/// \n/// - High level: Take the payload data and call `QrCode::encode_text()` or `QrCode::encode_binary()`.\n/// - Mid level: Custom-make the list of segments and call\n///   `QrCode::encode_segments()` or `QrCode::encode_segments_advanced()`.\n/// - Low level: Custom-make the array of data codeword bytes (including segment\n///   headers and final padding, excluding error correction codewords), supply the\n///   appropriate version number, and call the `QrCode::encode_codewords()` constructor.\n/// \n/// (Note that all ways require supplying the desired error correction level.)\n#[derive(Clone, PartialEq, Eq)]\npub struct QrCode {\n\t\n\t// Scalar parameters:\n\t\n\t// The version number of this QR Code, which is between 1 and 40 (inclusive).\n\t// This determines the size of this barcode.\n\tversion: Version,\n\t\n\t// The width and height of this QR Code, measured in modules, between\n\t// 21 and 177 (inclusive). This is equal to version * 4 + 17.\n\tsize: i32,\n\t\n\t// The error correction level used in this QR Code.\n\terrorcorrectionlevel: QrCodeEcc,\n\t\n\t// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).\n\t// Even if a QR Code is created with automatic masking requested (mask = None),\n\t// the resulting object still has a mask value between 0 and 7.\n\tmask: Mask,\n\t\n\t// Grids of modules/pixels, with dimensions of size*size:\n\t\n\t// The modules of this QR Code (false = light, true = dark).\n\t// Immutable after constructor finishes. Accessed through get_module().\n\tmodules: Vec<bool>,\n\t\n\t// Indicates function modules that are not subjected to masking. Discarded when constructor finishes.\n\tisfunction: Vec<bool>,\n\t\n}\n\n\nimpl QrCode {\n\t\n\t/*---- Static factory functions (high level) ----*/\n\t\n\t/// Returns a QR Code representing the given Unicode text string at the given error correction level.\n\t/// \n\t/// As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer Unicode\n\t/// code points (not UTF-8 code units) if the low error correction level is used. The smallest possible\n\t/// QR Code version is automatically chosen for the output. The ECC level of the result may be higher than\n\t/// the ecl argument if it can be done without increasing the version.\n\t/// \n\t/// Returns a wrapped `QrCode` if successful, or `Err` if the\n\t/// data is too long to fit in any version at the given ECC level.\n\tpub fn encode_text(text: &str, ecl: QrCodeEcc) -> Result<Self,DataTooLong> {\n\t\tlet segs: Vec<QrSegment> = QrSegment::make_segments(text);\n\t\tQrCode::encode_segments(&segs, ecl)\n\t}\n\t\n\t\n\t/// Returns a QR Code representing the given binary data at the given error correction level.\n\t/// \n\t/// This function always encodes using the binary segment mode, not any text mode. The maximum number of\n\t/// bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.\n\t/// The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t/// \n\t/// Returns a wrapped `QrCode` if successful, or `Err` if the\n\t/// data is too long to fit in any version at the given ECC level.\n\tpub fn encode_binary(data: &[u8], ecl: QrCodeEcc) -> Result<Self,DataTooLong> {\n\t\tlet segs: [QrSegment; 1] = [QrSegment::make_bytes(data)];\n\t\tQrCode::encode_segments(&segs, ecl)\n\t}\n\t\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/// Returns a QR Code representing the given segments at the given error correction level.\n\t/// \n\t/// The smallest possible QR Code version is automatically chosen for the output. The ECC level\n\t/// of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t/// \n\t/// This function allows the user to create a custom sequence of segments that switches\n\t/// between modes (such as alphanumeric and byte) to encode text in less space.\n\t/// This is a mid-level API; the high-level API is `encode_text()` and `encode_binary()`.\n\t/// \n\t/// Returns a wrapped `QrCode` if successful, or `Err` if the\n\t/// data is too long to fit in any version at the given ECC level.\n\tpub fn encode_segments(segs: &[QrSegment], ecl: QrCodeEcc) -> Result<Self,DataTooLong> {\n\t\tQrCode::encode_segments_advanced(segs, ecl, Version::MIN, Version::MAX, None, true)\n\t}\n\t\n\t\n\t/// Returns a QR Code representing the given segments with the given encoding parameters.\n\t/// \n\t/// The smallest possible QR Code version within the given range is automatically\n\t/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result\n\t/// may be higher than the ecl argument if it can be done without increasing the\n\t/// version. The mask number is either between 0 to 7 (inclusive) to force that\n\t/// mask, or `None` to automatically choose an appropriate mask (which may be slow).\n\t/// \n\t/// This function allows the user to create a custom sequence of segments that switches\n\t/// between modes (such as alphanumeric and byte) to encode text in less space.\n\t/// This is a mid-level API; the high-level API is `encode_text()` and `encode_binary()`.\n\t/// \n\t/// Returns a wrapped `QrCode` if successful, or `Err` if the data is too\n\t/// long to fit in any version in the given range at the given ECC level.\n\tpub fn encode_segments_advanced(segs: &[QrSegment], mut ecl: QrCodeEcc,\n\t\t\tminversion: Version, maxversion: Version, mask: Option<Mask>, boostecl: bool)\n\t\t\t-> Result<Self,DataTooLong> {\n\t\t\n\t\tassert!(minversion <= maxversion, \"Invalid value\");\n\t\t\n\t\t// Find the minimal version number to use\n\t\tlet mut version: Version = minversion;\n\t\tlet datausedbits: usize = loop {\n\t\t\tlet datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;  // Number of data bits available\n\t\t\tlet dataused: Option<usize> = QrSegment::get_total_bits(segs, version);\n\t\t\tif dataused.map_or(false, |n| n <= datacapacitybits) {\n\t\t\t\tbreak dataused.unwrap();  // This version number is found to be suitable\n\t\t\t} else if version >= maxversion {  // All versions in the range could not fit the given data\n\t\t\t\treturn Err(match dataused {\n\t\t\t\t\tNone => DataTooLong::SegmentTooLong,\n\t\t\t\t\tSome(n) => DataTooLong::DataOverCapacity(n, datacapacitybits),\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tversion = Version::new(version.value() + 1);\n\t\t\t}\n\t\t};\n\t\t\n\t\t// Increase the error correction level while the data still fits in the current version number\n\t\tfor &newecl in &[QrCodeEcc::Medium, QrCodeEcc::Quartile, QrCodeEcc::High] {  // From low to high\n\t\t\tif boostecl && datausedbits <= QrCode::get_num_data_codewords(version, newecl) * 8 {\n\t\t\t\tecl = newecl;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Concatenate all segments to create the data bit string\n\t\tlet mut bb = BitBuffer(Vec::new());\n\t\tfor seg in segs {\n\t\t\tbb.append_bits(seg.mode.mode_bits(), 4);\n\t\t\tbb.append_bits(u32::try_from(seg.numchars).unwrap(), seg.mode.num_char_count_bits(version));\n\t\t\tbb.0.extend_from_slice(&seg.data);\n\t\t}\n\t\tdebug_assert_eq!(bb.0.len(), datausedbits);\n\t\t\n\t\t// Add terminator and pad up to a byte if applicable\n\t\tlet datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;\n\t\tdebug_assert!(bb.0.len() <= datacapacitybits);\n\t\tlet numzerobits: usize = std::cmp::min(4, datacapacitybits - bb.0.len());\n\t\tbb.append_bits(0, u8::try_from(numzerobits).unwrap());\n\t\tlet numzerobits: usize = bb.0.len().wrapping_neg() & 7;\n\t\tbb.append_bits(0, u8::try_from(numzerobits).unwrap());\n\t\tdebug_assert_eq!(bb.0.len() % 8, 0);\n\t\t\n\t\t// Pad with alternating bytes until data capacity is reached\n\t\tfor &padbyte in [0xEC, 0x11].iter().cycle() {\n\t\t\tif bb.0.len() >= datacapacitybits {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbb.append_bits(padbyte, 8);\n\t\t}\n\t\t\n\t\t// Pack bits into bytes in big endian\n\t\tlet mut datacodewords = vec![0u8; bb.0.len() / 8];\n\t\tfor (i, &bit) in bb.0.iter().enumerate() {\n\t\t\tdatacodewords[i >> 3] |= u8::from(bit) << (7 - (i & 7));\n\t\t}\n\t\t\n\t\t// Create the QR Code object\n\t\tOk(QrCode::encode_codewords(version, ecl, &datacodewords, mask))\n\t}\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/// Creates a new QR Code with the given version number,\n\t/// error correction level, data codeword bytes, and mask number.\n\t/// \n\t/// This is a low-level API that most users should not use directly.\n\t/// A mid-level API is the `encode_segments()` function.\n\tpub fn encode_codewords(ver: Version, ecl: QrCodeEcc, datacodewords: &[u8], mut msk: Option<Mask>) -> Self {\n\t\t// Initialize fields\n\t\tlet size = usize::from(ver.value()) * 4 + 17;\n\t\tlet mut result = Self {\n\t\t\tversion: ver,\n\t\t\tsize: size as i32,\n\t\t\tmask: Mask::new(0),  // Dummy value\n\t\t\terrorcorrectionlevel: ecl,\n\t\t\tmodules   : vec![false; size * size],  // Initially all light\n\t\t\tisfunction: vec![false; size * size],\n\t\t};\n\t\t\n\t\t// Compute ECC, draw modules\n\t\tresult.draw_function_patterns();\n\t\tlet allcodewords: Vec<u8> = result.add_ecc_and_interleave(datacodewords);\n\t\tresult.draw_codewords(&allcodewords);\n\t\t\n\t\t// Do masking\n\t\tif msk.is_none() {  // Automatically choose best mask\n\t\t\tlet mut minpenalty = std::i32::MAX;\n\t\t\tfor i in 0u8 .. 8 {\n\t\t\t\tlet i = Mask::new(i);\n\t\t\t\tresult.apply_mask(i);\n\t\t\t\tresult.draw_format_bits(i);\n\t\t\t\tlet penalty: i32 = result.get_penalty_score();\n\t\t\t\tif penalty < minpenalty {\n\t\t\t\t\tmsk = Some(i);\n\t\t\t\t\tminpenalty = penalty;\n\t\t\t\t}\n\t\t\t\tresult.apply_mask(i);  // Undoes the mask due to XOR\n\t\t\t}\n\t\t}\n\t\tlet msk: Mask = msk.unwrap();\n\t\tresult.mask = msk;\n\t\tresult.apply_mask(msk);  // Apply the final choice of mask\n\t\tresult.draw_format_bits(msk);  // Overwrite old format bits\n\t\t\n\t\tresult.isfunction.clear();\n\t\tresult.isfunction.shrink_to_fit();\n\t\tresult\n\t}\n\t\n\t\n\t/*---- Public methods ----*/\n\t\n\t/// Returns this QR Code's version, in the range [1, 40].\n\tpub fn version(&self) -> Version {\n\t\tself.version\n\t}\n\t\n\t\n\t/// Returns this QR Code's size, in the range [21, 177].\n\tpub fn size(&self) -> i32 {\n\t\tself.size\n\t}\n\t\n\t\n\t/// Returns this QR Code's error correction level.\n\tpub fn error_correction_level(&self) -> QrCodeEcc {\n\t\tself.errorcorrectionlevel\n\t}\n\t\n\t\n\t/// Returns this QR Code's mask, in the range [0, 7].\n\tpub fn mask(&self) -> Mask {\n\t\tself.mask\n\t}\n\t\n\t\n\t/// Returns the color of the module (pixel) at the given coordinates,\n\t/// which is `false` for light or `true` for dark.\n\t/// \n\t/// The top left corner has the coordinates (x=0, y=0). If the given\n\t/// coordinates are out of bounds, then `false` (light) is returned.\n\tpub fn get_module(&self, x: i32, y: i32) -> bool {\n\t\t(0 .. self.size).contains(&x) && (0 .. self.size).contains(&y) && self.module(x, y)\n\t}\n\t\n\t\n\t// Returns the color of the module at the given coordinates, which must be in bounds.\n\tfn module(&self, x: i32, y: i32) -> bool {\n\t\tself.modules[(y * self.size + x) as usize]\n\t}\n\t\n\t\n\t// Returns a mutable reference to the module's color at the given coordinates, which must be in bounds.\n\tfn module_mut(&mut self, x: i32, y: i32) -> &mut bool {\n\t\t&mut self.modules[(y * self.size + x) as usize]\n\t}\n\t\n\t\n\t/*---- Private helper methods for constructor: Drawing function modules ----*/\n\t\n\t// Reads this object's version field, and draws and marks all function modules.\n\tfn draw_function_patterns(&mut self) {\n\t\t// Draw horizontal and vertical timing patterns\n\t\tlet size: i32 = self.size;\n\t\tfor i in 0 .. size {\n\t\t\tself.set_function_module(6, i, i % 2 == 0);\n\t\t\tself.set_function_module(i, 6, i % 2 == 0);\n\t\t}\n\t\t\n\t\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\t\tself.draw_finder_pattern(3, 3);\n\t\tself.draw_finder_pattern(size - 4, 3);\n\t\tself.draw_finder_pattern(3, size - 4);\n\t\t\n\t\t// Draw numerous alignment patterns\n\t\tlet alignpatpos: Vec<i32> = self.get_alignment_pattern_positions();\n\t\tlet numalign: usize = alignpatpos.len();\n\t\tfor i in 0 .. numalign {\n\t\t\tfor j in 0 .. numalign {\n\t\t\t\t// Don't draw on the three finder corners\n\t\t\t\tif !(i == 0 && j == 0 || i == 0 && j == numalign - 1 || i == numalign - 1 && j == 0) {\n\t\t\t\t\tself.draw_alignment_pattern(alignpatpos[i], alignpatpos[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Draw configuration data\n\t\tself.draw_format_bits(Mask::new(0));  // Dummy mask value; overwritten later in the constructor\n\t\tself.draw_version();\n\t}\n\t\n\t\n\t// Draws two copies of the format bits (with its own error correction code)\n\t// based on the given mask and this object's error correction level field.\n\tfn draw_format_bits(&mut self, mask: Mask) {\n\t\t// Calculate error correction code and pack bits\n\t\tlet bits: u32 = {\n\t\t\t// errcorrlvl is uint2, mask is uint3\n\t\t\tlet data = u32::from(self.errorcorrectionlevel.format_bits() << 3 | mask.value());\n\t\t\tlet mut rem: u32 = data;\n\t\t\tfor _ in 0 .. 10 {\n\t\t\t\trem = (rem << 1) ^ ((rem >> 9) * 0x537);\n\t\t\t}\n\t\t\t(data << 10 | rem) ^ 0x5412  // uint15\n\t\t};\n\t\tdebug_assert_eq!(bits >> 15, 0);\n\t\t\n\t\t// Draw first copy\n\t\tfor i in 0 .. 6 {\n\t\t\tself.set_function_module(8, i, get_bit(bits, i));\n\t\t}\n\t\tself.set_function_module(8, 7, get_bit(bits, 6));\n\t\tself.set_function_module(8, 8, get_bit(bits, 7));\n\t\tself.set_function_module(7, 8, get_bit(bits, 8));\n\t\tfor i in 9 .. 15 {\n\t\t\tself.set_function_module(14 - i, 8, get_bit(bits, i));\n\t\t}\n\t\t\n\t\t// Draw second copy\n\t\tlet size: i32 = self.size;\n\t\tfor i in 0 .. 8 {\n\t\t\tself.set_function_module(size - 1 - i, 8, get_bit(bits, i));\n\t\t}\n\t\tfor i in 8 .. 15 {\n\t\t\tself.set_function_module(8, size - 15 + i, get_bit(bits, i));\n\t\t}\n\t\tself.set_function_module(8, size - 8, true);  // Always dark\n\t}\n\t\n\t\n\t// Draws two copies of the version bits (with its own error correction code),\n\t// based on this object's version field, iff 7 <= version <= 40.\n\tfn draw_version(&mut self) {\n\t\tif self.version.value() < 7 {\n\t\t\treturn;\n\t\t}\n\t\t\n\t\t// Calculate error correction code and pack bits\n\t\tlet bits: u32 = {\n\t\t\tlet data = u32::from(self.version.value());  // uint6, in the range [7, 40]\n\t\t\tlet mut rem: u32 = data;\n\t\t\tfor _ in 0 .. 12 {\n\t\t\t\trem = (rem << 1) ^ ((rem >> 11) * 0x1F25);\n\t\t\t}\n\t\t\tdata << 12 | rem  // uint18\n\t\t};\n\t\tdebug_assert_eq!(bits >> 18, 0);\n\t\t\n\t\t// Draw two copies\n\t\tfor i in 0 .. 18 {\n\t\t\tlet bit: bool = get_bit(bits, i);\n\t\t\tlet a: i32 = self.size - 11 + i % 3;\n\t\t\tlet b: i32 = i / 3;\n\t\t\tself.set_function_module(a, b, bit);\n\t\t\tself.set_function_module(b, a, bit);\n\t\t}\n\t}\n\t\n\t\n\t// Draws a 9*9 finder pattern including the border separator,\n\t// with the center module at (x, y). Modules can be out of bounds.\n\tfn draw_finder_pattern(&mut self, x: i32, y: i32) {\n\t\tfor dy in -4 ..= 4 {\n\t\t\tfor dx in -4 ..= 4 {\n\t\t\t\tlet xx: i32 = x + dx;\n\t\t\t\tlet yy: i32 = y + dy;\n\t\t\t\tif (0 .. self.size).contains(&xx) && (0 .. self.size).contains(&yy) {\n\t\t\t\t\tlet dist: i32 = std::cmp::max(dx.abs(), dy.abs());  // Chebyshev/infinity norm\n\t\t\t\t\tself.set_function_module(xx, yy, dist != 2 && dist != 4);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Draws a 5*5 alignment pattern, with the center module\n\t// at (x, y). All modules must be in bounds.\n\tfn draw_alignment_pattern(&mut self, x: i32, y: i32) {\n\t\tfor dy in -2 ..= 2 {\n\t\t\tfor dx in -2 ..= 2 {\n\t\t\t\tself.set_function_module(x + dx, y + dy, std::cmp::max(dx.abs(), dy.abs()) != 1);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Sets the color of a module and marks it as a function module.\n\t// Only used by the constructor. Coordinates must be in bounds.\n\tfn set_function_module(&mut self, x: i32, y: i32, isdark: bool) {\n\t\t*self.module_mut(x, y) = isdark;\n\t\tself.isfunction[(y * self.size + x) as usize] = true;\n\t}\n\t\n\t\n\t/*---- Private helper methods for constructor: Codewords and masking ----*/\n\t\n\t// Returns a new byte string representing the given data with the appropriate error correction\n\t// codewords appended to it, based on this object's version and error correction level.\n\tfn add_ecc_and_interleave(&self, data: &[u8]) -> Vec<u8> {\n\t\tlet ver: Version = self.version;\n\t\tlet ecl: QrCodeEcc = self.errorcorrectionlevel;\n\t\tassert_eq!(data.len(), QrCode::get_num_data_codewords(ver, ecl), \"Illegal argument\");\n\t\t\n\t\t// Calculate parameter numbers\n\t\tlet numblocks: usize = QrCode::table_get(&NUM_ERROR_CORRECTION_BLOCKS, ver, ecl);\n\t\tlet blockecclen: usize = QrCode::table_get(&ECC_CODEWORDS_PER_BLOCK  , ver, ecl);\n\t\tlet rawcodewords: usize = QrCode::get_num_raw_data_modules(ver) / 8;\n\t\tlet numshortblocks: usize = numblocks - rawcodewords % numblocks;\n\t\tlet shortblocklen: usize = rawcodewords / numblocks;\n\t\t\n\t\t// Split data into blocks and append ECC to each block\n\t\tlet mut blocks = Vec::<Vec<u8>>::with_capacity(numblocks);\n\t\tlet rsdiv: Vec<u8> = QrCode::reed_solomon_compute_divisor(blockecclen);\n\t\tlet mut k: usize = 0;\n\t\tfor i in 0 .. numblocks {\n\t\t\tlet datlen: usize = shortblocklen - blockecclen + usize::from(i >= numshortblocks);\n\t\t\tlet mut dat = data[k .. k+datlen].to_vec();\n\t\t\tk += datlen;\n\t\t\tlet ecc: Vec<u8> = QrCode::reed_solomon_compute_remainder(&dat, &rsdiv);\n\t\t\tif i < numshortblocks {\n\t\t\t\tdat.push(0);\n\t\t\t}\n\t\t\tdat.extend_from_slice(&ecc);\n\t\t\tblocks.push(dat);\n\t\t}\n\t\t\n\t\t// Interleave (not concatenate) the bytes from every block into a single sequence\n\t\tlet mut result = Vec::<u8>::with_capacity(rawcodewords);\n\t\tfor i in 0 ..= shortblocklen {\n\t\t\tfor (j, block) in blocks.iter().enumerate() {\n\t\t\t\t// Skip the padding byte in short blocks\n\t\t\t\tif i != shortblocklen - blockecclen || j >= numshortblocks {\n\t\t\t\t\tresult.push(block[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tresult\n\t}\n\t\n\t\n\t// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire\n\t// data area of this QR Code. Function modules need to be marked off before this is called.\n\tfn draw_codewords(&mut self, data: &[u8]) {\n\t\tassert_eq!(data.len(), QrCode::get_num_raw_data_modules(self.version) / 8, \"Illegal argument\");\n\t\t\n\t\tlet mut i: usize = 0;  // Bit index into the data\n\t\t// Do the funny zigzag scan\n\t\tlet mut right: i32 = self.size - 1;\n\t\twhile right >= 1 {  // Index of right column in each column pair\n\t\t\tif right == 6 {\n\t\t\t\tright = 5;\n\t\t\t}\n\t\t\tfor vert in 0 .. self.size {  // Vertical counter\n\t\t\t\tfor j in 0 .. 2 {\n\t\t\t\t\tlet x: i32 = right - j;  // Actual x coordinate\n\t\t\t\t\tlet upward: bool = (right + 1) & 2 == 0;\n\t\t\t\t\tlet y: i32 = if upward { self.size - 1 - vert } else { vert };  // Actual y coordinate\n\t\t\t\t\tif !self.isfunction[(y * self.size + x) as usize] && i < data.len() * 8 {\n\t\t\t\t\t\t*self.module_mut(x, y) = get_bit(u32::from(data[i >> 3]), 7 - ((i as i32) & 7));\n\t\t\t\t\t\ti += 1;\n\t\t\t\t\t}\n\t\t\t\t\t// If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t\t// 0/false/light by the constructor and are left unchanged by this method\n\t\t\t\t}\n\t\t\t}\n\t\t\tright -= 2;\n\t\t}\n\t\tdebug_assert_eq!(i, data.len() * 8);\n\t}\n\t\n\t\n\t// XORs the codeword modules in this QR Code with the given mask pattern.\n\t// The function modules must be marked and the codeword bits must be drawn\n\t// before masking. Due to the arithmetic of XOR, calling apply_mask() with\n\t// the same mask value a second time will undo the mask. A final well-formed\n\t// QR Code needs exactly one (not zero, two, etc.) mask applied.\n\tfn apply_mask(&mut self, mask: Mask) {\n\t\tfor y in 0 .. self.size {\n\t\t\tfor x in 0 .. self.size {\n\t\t\t\tlet invert: bool = match mask.value() {\n\t\t\t\t\t0 => (x + y) % 2 == 0,\n\t\t\t\t\t1 => y % 2 == 0,\n\t\t\t\t\t2 => x % 3 == 0,\n\t\t\t\t\t3 => (x + y) % 3 == 0,\n\t\t\t\t\t4 => (x / 3 + y / 2) % 2 == 0,\n\t\t\t\t\t5 => x * y % 2 + x * y % 3 == 0,\n\t\t\t\t\t6 => (x * y % 2 + x * y % 3) % 2 == 0,\n\t\t\t\t\t7 => ((x + y) % 2 + x * y % 3) % 2 == 0,\n\t\t\t\t\t_ => unreachable!(),\n\t\t\t\t};\n\t\t\t\t*self.module_mut(x, y) ^= invert & !self.isfunction[(y * self.size + x) as usize];\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\n\tfn get_penalty_score(&self) -> i32 {\n\t\tlet mut result: i32 = 0;\n\t\tlet size: i32 = self.size;\n\t\t\n\t\t// Adjacent modules in row having same color, and finder-like patterns\n\t\tfor y in 0 .. size {\n\t\t\tlet mut runcolor = false;\n\t\t\tlet mut runx: i32 = 0;\n\t\t\tlet mut runhistory = FinderPenalty::new(size);\n\t\t\tfor x in 0 .. size {\n\t\t\t\tif self.module(x, y) == runcolor {\n\t\t\t\t\trunx += 1;\n\t\t\t\t\tif runx == 5 {\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\t} else if runx > 5 {\n\t\t\t\t\t\tresult += 1;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\trunhistory.add_history(runx);\n\t\t\t\t\tif !runcolor {\n\t\t\t\t\t\tresult += runhistory.count_patterns() * PENALTY_N3;\n\t\t\t\t\t}\n\t\t\t\t\truncolor = self.module(x, y);\n\t\t\t\t\trunx = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += runhistory.terminate_and_count(runcolor, runx) * PENALTY_N3;\n\t\t}\n\t\t// Adjacent modules in column having same color, and finder-like patterns\n\t\tfor x in 0 .. size {\n\t\t\tlet mut runcolor = false;\n\t\t\tlet mut runy: i32 = 0;\n\t\t\tlet mut runhistory = FinderPenalty::new(size);\n\t\t\tfor y in 0 .. size {\n\t\t\t\tif self.module(x, y) == runcolor {\n\t\t\t\t\truny += 1;\n\t\t\t\t\tif runy == 5 {\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\t} else if runy > 5 {\n\t\t\t\t\t\tresult += 1;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\trunhistory.add_history(runy);\n\t\t\t\t\tif !runcolor {\n\t\t\t\t\t\tresult += runhistory.count_patterns() * PENALTY_N3;\n\t\t\t\t\t}\n\t\t\t\t\truncolor = self.module(x, y);\n\t\t\t\t\truny = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += runhistory.terminate_and_count(runcolor, runy) * PENALTY_N3;\n\t\t}\n\t\t\n\t\t// 2*2 blocks of modules having same color\n\t\tfor y in 0 .. size-1 {\n\t\t\tfor x in 0 .. size-1 {\n\t\t\t\tlet color: bool = self.module(x, y);\n\t\t\t\tif color == self.module(x + 1, y) &&\n\t\t\t\t   color == self.module(x, y + 1) &&\n\t\t\t\t   color == self.module(x + 1, y + 1) {\n\t\t\t\t\tresult += PENALTY_N2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Balance of dark and light modules\n\t\tlet dark: i32 = self.modules.iter().copied().map(i32::from).sum();\n\t\tlet total: i32 = size * size;  // Note that size is odd, so dark/total != 1/2\n\t\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\t\tlet k: i32 = ((dark * 20 - total * 10).abs() + total - 1) / total - 1;\n\t\tdebug_assert!(0 <= k && k <= 9);\n\t\tresult += k * PENALTY_N4;\n\t\tdebug_assert!(0 <= result && result <= 2568888);  // Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\t\tresult\n\t}\n\t\n\t\n\t/*---- Private helper functions ----*/\n\t\n\t// Returns an ascending list of positions of alignment patterns for this version number.\n\t// Each position is in the range [0,177), and are used on both the x and y axes.\n\t// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.\n\tfn get_alignment_pattern_positions(&self) -> Vec<i32> {\n\t\tlet ver = i32::from(self.version.value());\n\t\tif ver == 1 {\n\t\t\tvec![]\n\t\t} else {\n\t\t\tlet numalign: i32 = ver / 7 + 2;\n\t\t\tlet step: i32 = (ver * 8 + numalign * 3 + 5) / (numalign * 4 - 4) * 2;\n\t\t\tlet mut result: Vec<i32> = (0 .. numalign-1).map(\n\t\t\t\t|i| self.size - 7 - i * step).collect();\n\t\t\tresult.push(6);\n\t\t\tresult.reverse();\n\t\t\tresult\n\t\t}\n\t}\n\t\n\t\n\t// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\n\tfn get_num_raw_data_modules(ver: Version) -> usize {\n\t\tlet ver = usize::from(ver.value());\n\t\tlet mut result: usize = (16 * ver + 128) * ver + 64;\n\t\tif ver >= 2 {\n\t\t\tlet numalign: usize = ver / 7 + 2;\n\t\t\tresult -= (25 * numalign - 10) * numalign - 55;\n\t\t\tif ver >= 7 {\n\t\t\t\tresult -= 36;\n\t\t\t}\n\t\t}\n\t\tdebug_assert!((208 ..= 29648).contains(&result));\n\t\tresult\n\t}\n\t\n\t\n\t// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t// QR Code of the given version number and error correction level, with remainder bits discarded.\n\t// This stateless pure function could be implemented as a (40*4)-cell lookup table.\n\tfn get_num_data_codewords(ver: Version, ecl: QrCodeEcc) -> usize {\n\t\tQrCode::get_num_raw_data_modules(ver) / 8\n\t\t\t- QrCode::table_get(&ECC_CODEWORDS_PER_BLOCK    , ver, ecl)\n\t\t\t* QrCode::table_get(&NUM_ERROR_CORRECTION_BLOCKS, ver, ecl)\n\t}\n\t\n\t\n\t// Returns an entry from the given table based on the given values.\n\tfn table_get(table: &'static [[i8; 41]; 4], ver: Version, ecl: QrCodeEcc) -> usize {\n\t\ttable[ecl.ordinal()][usize::from(ver.value())] as usize\n\t}\n\t\n\t\n\t// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be\n\t// implemented as a lookup table over all possible parameter values, instead of as an algorithm.\n\tfn reed_solomon_compute_divisor(degree: usize) -> Vec<u8> {\n\t\tassert!((1 ..= 255).contains(&degree), \"Degree out of range\");\n\t\t// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t\t// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].\n\t\tlet mut result = vec![0u8; degree - 1];\n\t\tresult.push(1);  // Start off with the monomial x^0\n\t\t\n\t\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t\t// and drop the highest monomial term which is always 1x^degree.\n\t\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\t\tlet mut root: u8 = 1;\n\t\tfor _ in 0 .. degree {  // Unused variable i\n\t\t\t// Multiply the current product by (x - r^i)\n\t\t\tfor j in 0 .. degree {\n\t\t\t\tresult[j] = QrCode::reed_solomon_multiply(result[j], root);\n\t\t\t\tif j + 1 < result.len() {\n\t\t\t\t\tresult[j] ^= result[j + 1];\n\t\t\t\t}\n\t\t\t}\n\t\t\troot = QrCode::reed_solomon_multiply(root, 0x02);\n\t\t}\n\t\tresult\n\t}\n\t\n\t\n\t// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.\n\tfn reed_solomon_compute_remainder(data: &[u8], divisor: &[u8]) -> Vec<u8> {\n\t\tlet mut result = vec![0u8; divisor.len()];\n\t\tfor b in data {  // Polynomial division\n\t\t\tlet factor: u8 = b ^ result.remove(0);\n\t\t\tresult.push(0);\n\t\t\tfor (x, &y) in result.iter_mut().zip(divisor.iter()) {\n\t\t\t\t*x ^= QrCode::reed_solomon_multiply(y, factor);\n\t\t\t}\n\t\t}\n\t\tresult\n\t}\n\t\n\t\n\t// Returns the product of the two given field elements modulo GF(2^8/0x11D).\n\t// All inputs are valid. This could be implemented as a 256*256 lookup table.\n\tfn reed_solomon_multiply(x: u8, y: u8) -> u8 {\n\t\t// Russian peasant multiplication\n\t\tlet mut z: u8 = 0;\n\t\tfor i in (0 .. 8).rev() {\n\t\t\tz = (z << 1) ^ ((z >> 7) * 0x1D);\n\t\t\tz ^= ((y >> i) & 1) * x;\n\t\t}\n\t\tz\n\t}\n\t\n}\n\n\n/*---- Helper struct for get_penalty_score() ----*/\n\nstruct FinderPenalty {\n\tqr_size: i32,\n\trun_history: [i32; 7],\n}\n\n\nimpl FinderPenalty {\n\t\n\tpub fn new(size: i32) -> Self {\n\t\tSelf {\n\t\t\tqr_size: size,\n\t\t\trun_history: [0i32; 7],\n\t\t}\n\t}\n\t\n\t\n\t// Pushes the given value to the front and drops the last value.\n\tpub fn add_history(&mut self, mut currentrunlength: i32) {\n\t\tif self.run_history[0] == 0 {\n\t\t\tcurrentrunlength += self.qr_size;  // Add light border to initial run\n\t\t}\n\t\tlet rh = &mut self.run_history;\n\t\tfor i in (0 .. rh.len()-1).rev() {\n\t\t\trh[i + 1] = rh[i];\n\t\t}\n\t\trh[0] = currentrunlength;\n\t}\n\t\n\t\n\t// Can only be called immediately after a light run is added, and returns either 0, 1, or 2.\n\tpub fn count_patterns(&self) -> i32 {\n\t\tlet rh = &self.run_history;\n\t\tlet n = rh[1];\n\t\tdebug_assert!(n <= self.qr_size * 3);\n\t\tlet core = n > 0 && rh[2] == n && rh[3] == n * 3 && rh[4] == n && rh[5] == n;\n\t\t#[allow(unused_parens)]\n\t\t( i32::from(core && rh[0] >= n * 4 && rh[6] >= n)\n\t\t+ i32::from(core && rh[6] >= n * 4 && rh[0] >= n))\n\t}\n\t\n\t\n\t// Must be called at the end of a line (row or column) of modules.\n\tpub fn terminate_and_count(mut self, currentruncolor: bool, mut currentrunlength: i32) -> i32 {\n\t\tif currentruncolor {  // Terminate dark run\n\t\t\tself.add_history(currentrunlength);\n\t\t\tcurrentrunlength = 0;\n\t\t}\n\t\tcurrentrunlength += self.qr_size;  // Add light border to final run\n\t\tself.add_history(currentrunlength);\n\t\tself.count_patterns()\n\t}\n\t\n}\n\n\n/*---- Constants and tables ----*/\n\n// For use in get_penalty_score(), when evaluating which mask is best.\nconst PENALTY_N1: i32 =  3;\nconst PENALTY_N2: i32 =  3;\nconst PENALTY_N3: i32 = 40;\nconst PENALTY_N4: i32 = 10;\n\n\nstatic ECC_CODEWORDS_PER_BLOCK: [[i8; 41]; 4] = [\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t[-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\n\t[-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],  // Medium\n\t[-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],  // Quartile\n\t[-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],  // High\n];\n\nstatic NUM_ERROR_CORRECTION_BLOCKS: [[i8; 41]; 4] = [\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t[-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],  // Low\n\t[-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],  // Medium\n\t[-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],  // Quartile\n\t[-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],  // High\n];\n\n\n\n/*---- QrCodeEcc functionality ----*/\n\n/// The error correction level in a QR Code symbol.\n#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]\npub enum QrCodeEcc {\n\t/// The QR Code can tolerate about  7% erroneous codewords.\n\tLow     ,\n\t/// The QR Code can tolerate about 15% erroneous codewords.\n\tMedium  ,\n\t/// The QR Code can tolerate about 25% erroneous codewords.\n\tQuartile,\n\t/// The QR Code can tolerate about 30% erroneous codewords.\n\tHigh    ,\n}\n\n\nimpl QrCodeEcc {\n\t\n\t// Returns an unsigned 2-bit integer (in the range 0 to 3).\n\tfn ordinal(self) -> usize {\n\t\tuse QrCodeEcc::*;\n\t\tmatch self {\n\t\t\tLow      => 0,\n\t\t\tMedium   => 1,\n\t\t\tQuartile => 2,\n\t\t\tHigh     => 3,\n\t\t}\n\t}\n\t\n\t\n\t// Returns an unsigned 2-bit integer (in the range 0 to 3).\n\tfn format_bits(self) -> u8 {\n\t\tuse QrCodeEcc::*;\n\t\tmatch self {\n\t\t\tLow      => 1,\n\t\t\tMedium   => 0,\n\t\t\tQuartile => 3,\n\t\t\tHigh     => 2,\n\t\t}\n\t}\n\t\n}\n\n\n\n/*---- QrSegment functionality ----*/\n\n/// A segment of character/binary/control data in a QR Code symbol.\n/// \n/// Instances of this struct are immutable.\n/// \n/// The mid-level way to create a segment is to take the payload data\n/// and call a static factory function such as `QrSegment::make_numeric()`.\n/// The low-level way to create a segment is to custom-make the bit buffer\n/// and call the `QrSegment::new()` constructor with appropriate values.\n/// \n/// This segment struct imposes no length restrictions, but QR Codes have restrictions.\n/// Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n/// Any segment longer than this is meaningless for the purpose of generating QR Codes.\n#[derive(Clone, PartialEq, Eq)]\npub struct QrSegment {\n\t\n\t// The mode indicator of this segment. Accessed through mode().\n\tmode: QrSegmentMode,\n\t\n\t// The length of this segment's unencoded data. Measured in characters for\n\t// numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t// Not the same as the data's bit length. Accessed through num_chars().\n\tnumchars: usize,\n\t\n\t// The data bits of this segment. Accessed through data().\n\tdata: Vec<bool>,\n\t\n}\n\n\nimpl QrSegment {\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/// Returns a segment representing the given binary data encoded in byte mode.\n\t/// \n\t/// All input byte slices are acceptable.\n\t/// \n\t/// Any text string can be converted to UTF-8 bytes and encoded as a byte mode segment.\n\tpub fn make_bytes(data: &[u8]) -> Self {\n\t\tlet mut bb = BitBuffer(Vec::with_capacity(data.len().checked_mul(8).unwrap()));\n\t\tfor &b in data {\n\t\t\tbb.append_bits(u32::from(b), 8);\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Byte, data.len(), bb.0)\n\t}\n\t\n\t\n\t/// Returns a segment representing the given string of decimal digits encoded in numeric mode.\n\t/// \n\t/// Panics if the string contains non-digit characters.\n\tpub fn make_numeric(text: &str) -> Self {\n\t\tassert!(text.bytes().all(|b| (b'0' ..= b'9').contains(&b)), \"String contains non-numeric characters\");\n\t\tlet mut bb = BitBuffer(Vec::with_capacity(\n\t\t\ttext.len().checked_mul(3).unwrap().checked_add(text.len().div_ceil(3)).unwrap()));\n\t\tfor chunk in text.as_bytes().chunks(3) {\n\t\t\tlet data: u32 = chunk.iter().fold(0u32,\n\t\t\t\t|acc, &b| acc * 10 + u32::from(b - b'0'));\n\t\t\tbb.append_bits(data, (chunk.len() as u8) * 3 + 1);\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Numeric, text.len(), bb.0)\n\t}\n\t\n\t\n\t/// Returns a segment representing the given text string encoded in alphanumeric mode.\n\t/// \n\t/// The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t/// dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t/// \n\t/// Panics if the string contains non-encodable characters.\n\tpub fn make_alphanumeric(text: &str) -> Self {\n\t\tlet mut bb = BitBuffer(Vec::with_capacity(\n\t\t\ttext.len().checked_mul(5).unwrap().checked_add(text.len().div_ceil(2)).unwrap()));\n\t\tfor chunk in text.as_bytes().chunks(2) {\n\t\t\tlet data: u32 = chunk.iter().fold(0u32, |acc, &b| acc * 45 + u32::try_from(\n\t\t\t\tALPHANUMERIC_CHARSET.find(char::from(b)).expect(\"String contains unencodable characters in alphanumeric mode\")).unwrap());\n\t\t\tbb.append_bits(data, (chunk.len() as u8) * 5 + 1);\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Alphanumeric, text.len(), bb.0)\n\t}\n\t\n\t\n\t/// Returns a list of zero or more segments to represent the given Unicode text string.\n\t/// \n\t/// The result may use various segment modes and switch\n\t/// modes to optimize the length of the bit stream.\n\tpub fn make_segments(text: &str) -> Vec<Self> {\n\t\tif text.is_empty() {\n\t\t\tvec![]\n\t\t} else {\n\t\t\tvec![\n\t\t\t\tif QrSegment::is_numeric(text) {\n\t\t\t\t\tQrSegment::make_numeric(text)\n\t\t\t\t} else if QrSegment::is_alphanumeric(text) {\n\t\t\t\t\tQrSegment::make_alphanumeric(text)\n\t\t\t\t} else {\n\t\t\t\t\tQrSegment::make_bytes(text.as_bytes())\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t}\n\t\n\t\n\t/// Returns a segment representing an Extended Channel Interpretation\n\t/// (ECI) designator with the given assignment value.\n\tpub fn make_eci(assignval: u32) -> Self {\n\t\tlet mut bb = BitBuffer(Vec::with_capacity(24));\n\t\tif assignval < (1 << 7) {\n\t\t\tbb.append_bits(assignval, 8);\n\t\t} else if assignval < (1 << 14) {\n\t\t\tbb.append_bits(0b10, 2);\n\t\t\tbb.append_bits(assignval, 14);\n\t\t} else if assignval < 1_000_000 {\n\t\t\tbb.append_bits(0b110, 3);\n\t\t\tbb.append_bits(assignval, 21);\n\t\t} else {\n\t\t\tpanic!(\"ECI assignment value out of range\");\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Eci, 0, bb.0)\n\t}\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/// Creates a new QR Code segment with the given attributes and data.\n\t/// \n\t/// The character count (numchars) must agree with the mode and\n\t/// the bit buffer length, but the constraint isn't checked.\n\tpub fn new(mode: QrSegmentMode, numchars: usize, data: Vec<bool>) -> Self {\n\t\tSelf { mode, numchars, data }\n\t}\n\t\n\t\n\t/*---- Instance field getters ----*/\n\t\n\t/// Returns the mode indicator of this segment.\n\tpub fn mode(&self) -> QrSegmentMode {\n\t\tself.mode\n\t}\n\t\n\t\n\t/// Returns the character count field of this segment.\n\tpub fn num_chars(&self) -> usize {\n\t\tself.numchars\n\t}\n\t\n\t\n\t/// Returns the data bits of this segment.\n\tpub fn data(&self) -> &Vec<bool> {\n\t\t&self.data\n\t}\n\t\n\t\n\t/*---- Other static functions ----*/\n\t\n\t// Calculates and returns the number of bits needed to encode the given\n\t// segments at the given version. The result is None if a segment has too many\n\t// characters to fit its length field, or the total bits exceeds usize::MAX.\n\tfn get_total_bits(segs: &[Self], version: Version) -> Option<usize> {\n\t\tlet mut result: usize = 0;\n\t\tfor seg in segs {\n\t\t\tlet ccbits: u8 = seg.mode.num_char_count_bits(version);\n\t\t\t// ccbits can be as large as 16, but usize can be as small as 16\n\t\t\tif let Some(limit) = 1usize.checked_shl(ccbits.into()) {\n\t\t\t\tif seg.numchars >= limit {\n\t\t\t\t\treturn None;  // The segment's length doesn't fit the field's bit width\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult = result.checked_add(4 + usize::from(ccbits))?;\n\t\t\tresult = result.checked_add(seg.data.len())?;\n\t\t}\n\t\tSome(result)\n\t}\n\t\n\t\n\t/// Tests whether the given string can be encoded as a segment in numeric mode.\n\t/// \n\t/// A string is encodable iff each character is in the range 0 to 9.\n\tpub fn is_numeric(text: &str) -> bool {\n\t\ttext.chars().all(|c| ('0' ..= '9').contains(&c))\n\t}\n\t\n\t\n\t/// Tests whether the given string can be encoded as a segment in alphanumeric mode.\n\t/// \n\t/// A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t/// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\tpub fn is_alphanumeric(text: &str) -> bool {\n\t\ttext.chars().all(|c| ALPHANUMERIC_CHARSET.contains(c))\n\t}\n\t\n}\n\n\n// The set of all legal characters in alphanumeric mode,\n// where each character value maps to the index in the string.\nstatic ALPHANUMERIC_CHARSET: &str = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\n\n\n/*---- QrSegmentMode functionality ----*/\n\n/// Describes how a segment's data bits are interpreted.\n#[derive(Clone, Copy, PartialEq, Eq, Debug)]\npub enum QrSegmentMode {\n\tNumeric,\n\tAlphanumeric,\n\tByte,\n\tKanji,\n\tEci,\n}\n\n\nimpl QrSegmentMode {\n\t\n\t// Returns an unsigned 4-bit integer value (range 0 to 15)\n\t// representing the mode indicator bits for this mode object.\n\tfn mode_bits(self) -> u32 {\n\t\tuse QrSegmentMode::*;\n\t\tmatch self {\n\t\t\tNumeric      => 0x1,\n\t\t\tAlphanumeric => 0x2,\n\t\t\tByte         => 0x4,\n\t\t\tKanji        => 0x8,\n\t\t\tEci          => 0x7,\n\t\t}\n\t}\n\t\n\t\n\t// Returns the bit width of the character count field for a segment in this mode\n\t// in a QR Code at the given version number. The result is in the range [0, 16].\n\tfn num_char_count_bits(self, ver: Version) -> u8 {\n\t\tuse QrSegmentMode::*;\n\t\t(match self {\n\t\t\tNumeric      => [10, 12, 14],\n\t\t\tAlphanumeric => [ 9, 11, 13],\n\t\t\tByte         => [ 8, 16, 16],\n\t\t\tKanji        => [ 8, 10, 12],\n\t\t\tEci          => [ 0,  0,  0],\n\t\t})[usize::from((ver.value() + 7) / 17)]\n\t}\n\t\n}\n\n\n\n/*---- Bit buffer functionality ----*/\n\n/// An appendable sequence of bits (0s and 1s).\n/// \n/// Mainly used by QrSegment.\npub struct BitBuffer(pub Vec<bool>);\n\n\nimpl BitBuffer {\n\t/// Appends the given number of low-order bits of the given value to this buffer.\n\t/// \n\t/// Requires len &#x2264; 31 and val &lt; 2<sup>len</sup>.\n\tpub fn append_bits(&mut self, val: u32, len: u8) {\n\t\tassert!(len <= 31 && val >> len == 0, \"Value out of range\");\n\t\tself.0.extend((0 .. i32::from(len)).rev().map(|i| get_bit(val, i)));  // Append bit by bit\n\t}\n}\n\n\n\n/*---- Miscellaneous values ----*/\n\n/// The error type when the supplied data does not fit any QR Code version.\n///\n/// Ways to handle this exception include:\n/// \n/// - Decrease the error correction level if it was greater than `QrCodeEcc::Low`.\n/// - If the `encode_segments_advanced()` function was called, then increase the maxversion\n///   argument if it was less than `Version::MAX`. (This advice does not apply to the\n///   other factory functions because they search all versions up to `Version::MAX`.)\n/// - Split the text data into better or optimal segments in order to reduce the number of bits required.\n/// - Change the text or binary data to be shorter.\n/// - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).\n/// - Propagate the error upward to the caller/user.\n#[derive(Debug, Clone)]\npub enum DataTooLong {\n\tSegmentTooLong,\n\tDataOverCapacity(usize, usize),\n}\n\nimpl std::error::Error for DataTooLong {}\n\nimpl std::fmt::Display for DataTooLong {\n\tfn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {\n\t\tmatch *self {\n\t\t\tSelf::SegmentTooLong => write!(f, \"Segment too long\"),\n\t\t\tSelf::DataOverCapacity(datalen, maxcapacity) =>\n\t\t\t\twrite!(f, \"Data length = {} bits, Max capacity = {} bits\", datalen, maxcapacity),\n\t\t}\n\t}\n}\n\n\n/// A number between 1 and 40 (inclusive).\n#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]\npub struct Version(u8);\n\nimpl Version {\n\t/// The minimum version number supported in the QR Code Model 2 standard.\n\tpub const MIN: Version = Version( 1);\n\t\n\t/// The maximum version number supported in the QR Code Model 2 standard.\n\tpub const MAX: Version = Version(40);\n\t\n\t/// Creates a version object from the given number.\n\t/// \n\t/// Panics if the number is outside the range [1, 40].\n\tpub const fn new(ver: u8) -> Self {\n\t\tassert!(Version::MIN.value() <= ver && ver <= Version::MAX.value(), \"Version number out of range\");\n\t\tSelf(ver)\n\t}\n\t\n\t/// Returns the value, which is in the range [1, 40].\n\tpub const fn value(self) -> u8 {\n\t\tself.0\n\t}\n}\n\n\n/// A number between 0 and 7 (inclusive).\n#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]\npub struct Mask(u8);\n\nimpl Mask {\n\t/// Creates a mask object from the given number.\n\t/// \n\t/// Panics if the number is outside the range [0, 7].\n\tpub const fn new(mask: u8) -> Self {\n\t\tassert!(mask <= 7, \"Mask value out of range\");\n\t\tSelf(mask)\n\t}\n\t\n\t/// Returns the value, which is in the range [0, 7].\n\tpub const fn value(self) -> u8 {\n\t\tself.0\n\t}\n}\n\n\n// Returns true iff the i'th bit of x is set to 1.\nfn get_bit(x: u32, i: i32) -> bool {\n\t(x >> i) & 1 != 0\n}\n"
  },
  {
    "path": "rust-no-heap/Cargo.toml",
    "content": "[package]\nname = \"qrcodegen-no-heap\"\nversion = \"1.8.1\"\nauthors = [\"Project Nayuki\"]\ndescription = \"High-quality QR Code generator library\"\nhomepage = \"https://www.nayuki.io/page/qr-code-generator-library\"\nrepository = \"https://github.com/nayuki/QR-Code-generator\"\nreadme = \"Readme.markdown\"\nkeywords = [\"qr-code\", \"barcode\", \"encoder\", \"image\"]\ncategories = [\"encoding\", \"multimedia::images\"]\nlicense = \"MIT\"\nexclude = [\"examples/*\"]\n"
  },
  {
    "path": "rust-no-heap/Readme.markdown",
    "content": "QR Code generator library - Rust, no heap\n=========================================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Completely avoids heap allocation (e.g. `std::vec::Vec`), instead relying on suitably sized buffers from the caller and fixed-size stack allocations\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```rust\nextern crate qrcodegen_no_heap;\nuse qrcodegen_no_heap::Mask;\nuse qrcodegen_no_heap::QrCode;\nuse qrcodegen_no_heap::QrCodeEcc;\nuse qrcodegen_no_heap::Version;\n\n// Text data\nlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\nlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\nlet qr = QrCode::encode_text(\"Hello, world!\",\n    &mut tempbuffer, &mut outbuffer, QrCodeEcc::Medium,\n    Version::MIN, Version::MAX, None, true).unwrap();\nlet svg = to_svg_string(&qr, 4);  // See qrcodegen-demo\n\n// Binary data\nlet mut outbuffer   = vec![0u8; Version::MAX.buffer_len()];\nlet mut dataandtemp = vec![0u8; Version::MAX.buffer_len()];\ndataandtemp[0] = 0xE3;\ndataandtemp[1] = 0x81;\ndataandtemp[2] = 0x82;\nlet qr = QrCode::encode_binary(&mut dataandtemp, 3,\n    &mut outbuffer, QrCodeEcc::High,\n    Version::new(2), Version::new(7),\n    Some(Mask::new(4)), false).unwrap();\nfor y in 0 .. qr.size() {\n    for x in 0 .. qr.size() {\n        (... paint qr.get_module(x, y) ...)\n    }\n}\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/rust-no-heap/examples/qrcodegen-demo.rs .\n"
  },
  {
    "path": "rust-no-heap/examples/qrcodegen-demo.rs",
    "content": "/* \n * QR Code generator demo (Rust, no heap)\n * \n * Run this command-line program with no arguments. The program computes a bunch of demonstration\n * QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\nextern crate qrcodegen_no_heap;\nuse qrcodegen_no_heap::Mask;\nuse qrcodegen_no_heap::QrCode;\nuse qrcodegen_no_heap::QrCodeEcc;\nuse qrcodegen_no_heap::QrSegment;\nuse qrcodegen_no_heap::QrSegmentMode;\nuse qrcodegen_no_heap::Version;\n\n\n// The main application program.\nfn main() {\n\tdo_basic_demo();\n\tdo_variety_demo();\n\tdo_segment_demo();\n\tdo_mask_demo();\n}\n\n\n\n/*---- Demo suite ----*/\n\n// Creates a single QR Code, then prints it to the console.\nfn do_basic_demo() {\n\tlet text: &'static str = \"Hello, world!\";   // User-supplied Unicode text\n\tlet errcorlvl: QrCodeEcc = QrCodeEcc::Low;  // Error correction level\n\t\n\t// Make and print the QR Code symbol\n\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\tlet qr: QrCode = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer,\n\t\terrcorlvl, Version::MIN, Version::MAX, None, true).unwrap();\n\t// Note: qr has a reference to outbuffer, so outbuffer needs to outlive qr\n\tstd::mem::drop(tempbuffer);  // Optional, because tempbuffer is only needed during encode_text()\n\tprint_qr(&qr);\n\tprintln!(\"{}\", to_svg_string(&qr, 4));\n}\n\n\n// Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.\nfn do_variety_demo() {\n\t{  // Numeric mode encoding (3.33 bits per digit)\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(\"314159265358979323846264338327950288419716939937510\",\n\t\t\t&mut tempbuffer, &mut outbuffer, QrCodeEcc::Medium, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t}\n\t\n\t{  // Alphanumeric mode encoding (5.5 bits per character)\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\",\n\t\t\t&mut tempbuffer, &mut outbuffer, QrCodeEcc::High, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t}\n\t\n\t{  // Unicode text as UTF-8\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(\"こんにちwa、世界！ αβγδ\",\n\t\t\t&mut tempbuffer, &mut outbuffer, QrCodeEcc::Quartile, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t}\n\t\n\t{  // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\t\tlet text = concat!(\n\t\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \",\n\t\t\t\"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \",\n\t\t\t\"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \",\n\t\t\t\"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \",\n\t\t\t\"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \",\n\t\t\t\"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \",\n\t\t\t\"a White Rabbit with pink eyes ran close by her.\");\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer,\n\t\t\tQrCodeEcc::High, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t}\n}\n\n\n// Creates QR Codes with manually specified segments for better compactness.\nfn do_segment_demo() {\n\t{  // Illustration \"silver\"\n\t\tlet silver0 = \"THE SQUARE ROOT OF 2 IS 1.\";\n\t\tlet silver1 = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(&[silver0, silver1].concat(), &mut tempbuffer, &mut outbuffer,\n\t\t\tQrCodeEcc::Low, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t\t\n\t\tlet (tempbuf0, tempbuf1) = tempbuffer.split_at_mut(QrSegment::calc_buffer_size(QrSegmentMode::Alphanumeric, silver0.len()).unwrap());\n\t\tlet segs = [\n\t\t\tQrSegment::make_alphanumeric(silver0, tempbuf0),\n\t\t\tQrSegment::make_numeric(silver1, tempbuf1),\n\t\t];\n\t\tlet (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(\n\t\t\t&segs, &mut outbuffer, QrCodeEcc::Low, Version::MIN, Version::MAX, true).unwrap();\n\t\tstd::mem::drop(segs);  // Implied, because segs has references to tempbuffer, but tempbuffer will be reused in encode_codewords()\n\t\tlet qr = QrCode::encode_codewords(&mut outbuffer, datacodewordslen, &mut tempbuffer, ecl, version, None);\n\t\tprint_qr(&qr);\n\t}\n\t\n\t{  // Illustration \"golden\"\n\t\tlet golden0 = \"Golden ratio φ = 1.\";\n\t\tlet golden1 = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\t\tlet golden2 = \"......\";\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(&[golden0, golden1, golden2].concat(), &mut tempbuffer, &mut outbuffer,\n\t\t\tQrCodeEcc::Low, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t\t\n\t\tlet (tempbuf1, tempbuf2) = tempbuffer.split_at_mut(QrSegment::calc_buffer_size(QrSegmentMode::Numeric, golden1.len()).unwrap());\n\t\tlet segs = [\n\t\t\tQrSegment::make_bytes(golden0.as_bytes()),\n\t\t\tQrSegment::make_numeric(golden1, tempbuf1),\n\t\t\tQrSegment::make_alphanumeric(golden2, tempbuf2),\n\t\t];\n\t\tlet (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(\n\t\t\t&segs, &mut outbuffer, QrCodeEcc::Low, Version::MIN, Version::MAX, true).unwrap();\n\t\tlet qr = QrCode::encode_codewords(&mut outbuffer, datacodewordslen, &mut tempbuffer, ecl, version, None);\n\t\tprint_qr(&qr);\n\t}\n\t\n\t{  // Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\t\tlet madoka = \"「魔法少女まどか☆マギカ」って、　ИАИ　ｄｅｓｕ　κα？\";\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet qr = QrCode::encode_text(madoka, &mut outbuffer, &mut tempbuffer,\n\t\t\tQrCodeEcc::Low, Version::MIN, Version::MAX, None, true).unwrap();\n\t\tprint_qr(&qr);\n\t\t\n\t\tlet kanjichars: Vec<u32> = vec![  // Kanji mode encoding (13 bits per character)\n\t\t\t0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,\n\t\t\t0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,\n\t\t\t0x018D, 0x018A, 0x0036, 0x0141, 0x0144,\n\t\t\t0x0001, 0x0000, 0x0249, 0x0240, 0x0249,\n\t\t\t0x0000, 0x0104, 0x0105, 0x0113, 0x0115,\n\t\t\t0x0000, 0x0208, 0x01FF, 0x0008,\n\t\t];\n\t\tlet mut bb = qrcodegen_no_heap::BitBuffer::new(&mut tempbuffer);\n\t\tfor &c in &kanjichars {\n\t\t\tbb.append_bits(c, 13);\n\t\t}\n\t\tlet segs = [\n\t\t\t{\n\t\t\t\tlet bitlen = bb.len();\n\t\t\t\tQrSegment::new(qrcodegen_no_heap::QrSegmentMode::Kanji, kanjichars.len(), &tempbuffer, bitlen)\n\t\t\t},\n\t\t];\n\t\tlet (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(\n\t\t\t&segs, &mut outbuffer, QrCodeEcc::Low, Version::MIN, Version::MAX, true).unwrap();\n\t\tlet qr = QrCode::encode_codewords(&mut outbuffer, datacodewordslen, &mut tempbuffer, ecl, version, None);\n\t\tprint_qr(&qr);\n\t}\n}\n\n\n// Creates QR Codes with the same size and contents but different mask patterns.\nfn do_mask_demo() {\n\t{  // Project Nayuki URL\n\t\tlet text = \"https://www.nayuki.io/\";\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\t\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer, QrCodeEcc::High,\n\t\t\tVersion::MIN, Version::MAX, None, true).unwrap();  // Automatic mask\n\t\tprint_qr(&qr);\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer, QrCodeEcc::High,\n\t\t\tVersion::MIN, Version::MAX, Some(Mask::new(3)), true).unwrap();  // Force mask 3\n\t\tprint_qr(&qr);\n\t}\n\t\n\t{  // Chinese text as UTF-8\n\t\tlet text = \"維基百科（Wikipedia，聆聽i/ˌwɪkᵻˈpiːdi.ə/）是一個自由內容、公開編輯且多語言的網路百科全書協作計畫\";\n\t\tlet mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n\t\tlet mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n\t\t\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer, QrCodeEcc::Medium,\n\t\t\tVersion::MIN, Version::MAX, Some(Mask::new(0)), true).unwrap();  // Force mask 0\n\t\tprint_qr(&qr);\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer, QrCodeEcc::Medium,\n\t\t\tVersion::MIN, Version::MAX, Some(Mask::new(1)), true).unwrap();  // Force mask 1\n\t\tprint_qr(&qr);\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer, QrCodeEcc::Medium,\n\t\t\tVersion::MIN, Version::MAX, Some(Mask::new(5)), true).unwrap();  // Force mask 5\n\t\tprint_qr(&qr);\n\t\tlet qr = QrCode::encode_text(text, &mut tempbuffer, &mut outbuffer, QrCodeEcc::Medium,\n\t\t\tVersion::MIN, Version::MAX, Some(Mask::new(7)), true).unwrap();  // Force mask 7\n\t\tprint_qr(&qr);\n\t}\n}\n\n\n\n/*---- Utilities ----*/\n\n// Returns a string of SVG code for an image depicting\n// the given QR Code, with the given number of border modules.\n// The string always uses Unix newlines (\\n), regardless of the platform.\nfn to_svg_string(qr: &QrCode, border: i32) -> String {\n\tassert!(border >= 0, \"Border must be non-negative\");\n\tlet mut result = String::new();\n\tresult += \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";\n\tresult += \"<!DOCTYPE svg PUBLIC \\\"-//W3C//DTD SVG 1.1//EN\\\" \\\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\\">\\n\";\n\tlet dimension = qr.size().checked_add(border.checked_mul(2).unwrap()).unwrap();\n\tresult += &format!(\n\t\t\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" version=\\\"1.1\\\" viewBox=\\\"0 0 {0} {0}\\\" stroke=\\\"none\\\">\\n\", dimension);\n\tresult += \"\\t<rect width=\\\"100%\\\" height=\\\"100%\\\" fill=\\\"#FFFFFF\\\"/>\\n\";\n\tresult += \"\\t<path d=\\\"\";\n\tfor y in 0 .. qr.size() {\n\t\tfor x in 0 .. qr.size() {\n\t\t\tif qr.get_module(x, y) {\n\t\t\t\tif x != 0 || y != 0 {\n\t\t\t\t\tresult += \" \";\n\t\t\t\t}\n\t\t\t\tresult += &format!(\"M{},{}h1v1h-1z\", x + border, y + border);\n\t\t\t}\n\t\t}\n\t}\n\tresult += \"\\\" fill=\\\"#000000\\\"/>\\n\";\n\tresult += \"</svg>\\n\";\n\tresult\n}\n\n\n// Prints the given QrCode object to the console.\nfn print_qr(qr: &QrCode) {\n\tlet border: i32 = 4;\n\tfor y in -border .. qr.size() + border {\n\t\tfor x in -border .. qr.size() + border {\n\t\t\tlet c: char = if qr.get_module(x, y) { '█' } else { ' ' };\n\t\t\tprint!(\"{0}{0}\", c);\n\t\t}\n\t\tprintln!();\n\t}\n\tprintln!();\n}\n"
  },
  {
    "path": "rust-no-heap/src/lib.rs",
    "content": "/* \n * QR Code generator library (Rust, no heap)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n\n//! Generates QR Codes from text strings and byte arrays.\n//! \n//! This project aims to be the best, clearest QR Code generator library.\n//! The primary goals are flexible options and absolute correctness.\n//! Secondary goals are compact implementation size and good documentation comments.\n//! \n//! Home page with live JavaScript demo, extensive descriptions, and competitor comparisons:\n//! [https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)\n//! \n//! # Features\n//! \n//! Core features:\n//! \n//! - Significantly shorter code but more documentation comments compared to competing libraries\n//! - Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n//! - Output format: Raw modules/pixels of the QR symbol\n//! - Detects finder-like penalty patterns more accurately than other implementations\n//! - Encodes numeric and special-alphanumeric text in less space than general text\n//! - Open-source code under the permissive MIT License\n//! \n//! Manual parameters:\n//! \n//! - User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n//! - User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n//! - User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n//! - User can create a list of data segments manually and add ECI segments\n//! \n//! More information about QR Code technology and this library's design can be found on the project home page.\n//! \n//! # Examples\n//! \n//! ```\n//! extern crate qrcodegen_no_heap;\n//! use qrcodegen_no_heap::Mask;\n//! use qrcodegen_no_heap::QrCode;\n//! use qrcodegen_no_heap::QrCodeEcc;\n//! use qrcodegen_no_heap::Version;\n//! ```\n//! \n//! Text data:\n//! \n//! ```\n//! let mut outbuffer  = vec![0u8; Version::MAX.buffer_len()];\n//! let mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];\n//! let qr = QrCode::encode_text(\"Hello, world!\", &mut tempbuffer, &mut outbuffer,\n//!     QrCodeEcc::Medium, Version::MIN, Version:MAX, None, true).unwrap();\n//! let svg = to_svg_string(&qr, 4);  // See qrcodegen-demo\n//! ```\n//! \n//! Binary data:\n//! \n//! ```\n//! let mut outbuffer   = vec![0u8; Version::MAX.buffer_len()];\n//! let mut dataandtemp = vec![0u8; Version::MAX.buffer_len()];\n//! dataandtemp[0] = 0xE3;\n//! dataandtemp[1] = 0x81;\n//! dataandtemp[2] = 0x82;\n//! let qr = QrCode::encode_binary(&mut dataandtemp, 3, &mut outbuffer, QrCodeEcc::High,\n//!     Version::new(2), Version::new(7), Some(Mask::new(4)), false).unwrap();\n//! for y in 0 .. qr.size() {\n//!     for x in 0 .. qr.size() {\n//!         (... paint qr.get_module(x, y) ...)\n//!     }\n//! }\n//! ```\n\n\n#![no_std]\n#![forbid(unsafe_code)]\nuse core::convert::TryFrom;\n\n\n/*---- QrCode functionality ----*/\n\n/// A QR Code symbol, which is a type of two-dimension barcode.\n/// \n/// Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n/// \n/// Instances of this struct represent an immutable square grid of dark and light cells.\n/// The impl provides static factory functions to create a QR Code from text or binary data.\n/// The struct and impl cover the QR Code Model 2 specification, supporting all versions\n/// (sizes) from 1 to 40, all 4 error correction levels, and 4 character encoding modes.\n/// \n/// Ways to create a QR Code object:\n/// \n/// - High level: Take the payload data and call `QrCode::encode_text()` or `QrCode::encode_binary()`.\n/// - Mid level: Custom-make the list of segments and call\n///   `QrCode::encode_segments_to_codewords()` and then `QrCode::encode_codewords()`.\n/// - Low level: Custom-make the array of data codeword bytes (including segment\n///   headers and final padding, excluding error correction codewords), supply the\n///   appropriate version number, and call the `QrCode::encode_codewords()` constructor.\n/// \n/// (Note that all ways require supplying the desired error correction level and various byte buffers.)\npub struct QrCode<'a> {\n\t\n\t// The width and height of this QR Code, measured in modules, between\n\t// 21 and 177 (inclusive). This is equal to version * 4 + 17.\n\tsize: &'a mut u8,\n\t\n\t// The modules of this QR Code (0 = light, 1 = dark), packed bitwise into bytes.\n\t// Immutable after constructor finishes. Accessed through get_module().\n\tmodules: &'a mut [u8],\n\t\n}\n\n\nimpl<'a> QrCode<'a> {\n\t\n\t/*---- Static factory functions (high level) ----*/\n\t\n\t/// Encodes the given text string to a QR Code, returning a wrapped `QrCode` if successful.\n\t/// If the data is too long to fit in any version in the given range\n\t/// at the given ECC level, then `Err` is returned.\n\t/// \n\t/// The smallest possible QR Code version within the given range is automatically\n\t/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result\n\t/// may be higher than the ecl argument if it can be done without increasing the\n\t/// version. The mask number is either between 0 to 7 (inclusive) to force that\n\t/// mask, or `None` to automatically choose an appropriate mask (which may be slow).\n\t/// \n\t/// About the slices, letting len = maxversion.buffer_len():\n\t/// - Before calling the function:\n\t///   - The slices tempbuffer and outbuffer each must have a length of at least len.\n\t///   - If a slice is longer than len, then the function will not\n\t///     read from or write to the suffix array[len .. array.len()].\n\t///   - The initial values of both slices can be arbitrary\n\t///     because the function always writes before reading.\n\t/// - After the function returns, both slices have no guarantee on what values are stored.\n\t/// \n\t/// If successful, the resulting QR Code may use numeric,\n\t/// alphanumeric, or byte mode to encode the text.\n\t/// \n\t/// In the most optimistic case, a QR Code at version 40 with low ECC\n\t/// can hold any UTF-8 string up to 2953 bytes, or any alphanumeric string\n\t/// up to 4296 characters, or any digit string up to 7089 characters.\n\t/// These numbers represent the hard upper limit of the QR Code standard.\n\t/// \n\t/// Please consult the QR Code specification for information on\n\t/// data capacities per version, ECC level, and text encoding mode.\n\tpub fn encode_text<'b>(text: &str, tempbuffer: &'b mut [u8], mut outbuffer: &'a mut [u8], ecl: QrCodeEcc,\n\t\t\tminversion: Version, maxversion: Version, mask: Option<Mask>, boostecl: bool) -> Result<QrCode<'a>,DataTooLong> {\n\t\t\n\t\tlet minlen: usize = outbuffer.len().min(tempbuffer.len());\n\t\toutbuffer = &mut outbuffer[ .. minlen];\n\t\t\n\t\tlet textlen: usize = text.len();  // In bytes\n\t\tif textlen == 0 {\n\t\t\tlet (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(&[], outbuffer, ecl, minversion, maxversion, boostecl)?;\n\t\t\treturn Ok(Self::encode_codewords(outbuffer, datacodewordslen, tempbuffer, ecl, version, mask));\n\t\t}\n\t\t\n\t\tuse QrSegmentMode::*;\n\t\tlet buflen: usize = outbuffer.len();\n\t\tlet seg: QrSegment = if QrSegment::is_numeric(text) && QrSegment::calc_buffer_size(Numeric, textlen).map_or(false, |x| x <= buflen) {\n\t\t\tQrSegment::make_numeric(text, tempbuffer)\n\t\t} else if QrSegment::is_alphanumeric(text) && QrSegment::calc_buffer_size(Alphanumeric, textlen).map_or(false, |x| x <= buflen) {\n\t\t\tQrSegment::make_alphanumeric(text, tempbuffer)\n\t\t} else if QrSegment::calc_buffer_size(Byte, textlen).map_or(false, |x| x <= buflen) {\n\t\t\tQrSegment::make_bytes(text.as_bytes())\n\t\t} else {\n\t\t\treturn Err(DataTooLong::SegmentTooLong);\n\t\t};\n\t\tlet (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(&[seg], outbuffer, ecl, minversion, maxversion, boostecl)?;\n\t\tOk(Self::encode_codewords(outbuffer, datacodewordslen, tempbuffer, ecl, version, mask))\n\t}\n\t\n\t\n\t/// Encodes the given binary data to a QR Code, returning a wrapped `QrCode` if successful.\n\t/// If the data is too long to fit in any version in the given range\n\t/// at the given ECC level, then `Err` is returned.\n\t/// \n\t/// The smallest possible QR Code version within the given range is automatically\n\t/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result\n\t/// may be higher than the ecl argument if it can be done without increasing the\n\t/// version. The mask number is either between 0 to 7 (inclusive) to force that\n\t/// mask, or `None` to automatically choose an appropriate mask (which may be slow).\n\t/// \n\t/// About the slices, letting len = maxversion.buffer_len():\n\t/// - Before calling the function:\n\t///   - The slices dataandtempbuffer and outbuffer each must have a length of at least len.\n\t///   - If a slice is longer than len, then the function will not\n\t///     read from or write to the suffix array[len .. array.len()].\n\t///   - The input slice range dataandtempbuffer[0 .. datalen] should normally be\n\t///     valid UTF-8 text, but is not required by the QR Code standard.\n\t///   - The initial values of dataandtempbuffer[datalen .. len] and outbuffer[0 .. len]\n\t///     can be arbitrary because the function always writes before reading.\n\t/// - After the function returns, both slices have no guarantee on what values are stored.\n\t/// \n\t/// If successful, the resulting QR Code will use byte mode to encode the data.\n\t/// \n\t/// In the most optimistic case, a QR Code at version 40 with low ECC can hold any byte\n\t/// sequence up to length 2953. This is the hard upper limit of the QR Code standard.\n\t/// \n\t/// Please consult the QR Code specification for information on\n\t/// data capacities per version, ECC level, and text encoding mode.\n\tpub fn encode_binary<'b>(dataandtempbuffer: &'b mut [u8], datalen: usize, mut outbuffer: &'a mut [u8], ecl: QrCodeEcc,\n\t\t\tminversion: Version, maxversion: Version, mask: Option<Mask>, boostecl: bool) -> Result<QrCode<'a>,DataTooLong> {\n\t\t\n\t\tassert!(datalen <= dataandtempbuffer.len(), \"Invalid data length\");\n\t\tlet minlen: usize = outbuffer.len().min(dataandtempbuffer.len());\n\t\toutbuffer = &mut outbuffer[ .. minlen];\n\t\t\n\t\tif QrSegment::calc_buffer_size(QrSegmentMode::Byte, datalen).map_or(true, |x| x > outbuffer.len()) {\n\t\t\treturn Err(DataTooLong::SegmentTooLong);\n\t\t}\n\t\tlet seg: QrSegment = QrSegment::make_bytes(&dataandtempbuffer[ .. datalen]);\n\t\tlet (datacodewordslen, ecl, version) = QrCode::encode_segments_to_codewords(&[seg], outbuffer, ecl, minversion, maxversion, boostecl)?;\n\t\tOk(Self::encode_codewords(outbuffer, datacodewordslen, dataandtempbuffer, ecl, version, mask))\n\t}\n\t\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/// Returns an intermediate state representing the given segments\n\t/// with the given encoding parameters being encoded into codewords.\n\t/// \n\t/// The smallest possible QR Code version within the given range is automatically\n\t/// chosen for the output. Iff boostecl is `true`, then the ECC level of the result\n\t/// may be higher than the ecl argument if it can be done without increasing the\n\t/// version. The mask number is either between 0 to 7 (inclusive) to force that\n\t/// mask, or `None` to automatically choose an appropriate mask (which may be slow).\n\t/// \n\t/// This function exists to allow segments to use parts of a temporary buffer,\n\t/// then have the segments be encoded to an output buffer, then invalidate all the segments,\n\t/// and finally have the output buffer and temporary buffer be encoded to a QR Code.\n\tpub fn encode_segments_to_codewords(segs: &[QrSegment], outbuffer: &'a mut [u8],\n\t\t\tmut ecl: QrCodeEcc, minversion: Version, maxversion: Version, boostecl: bool)\n\t\t\t-> Result<(usize,QrCodeEcc,Version),DataTooLong> {\n\t\t\n\t\tassert!(minversion <= maxversion, \"Invalid value\");\n\t\tassert!(outbuffer.len() >= QrCode::get_num_data_codewords(maxversion, ecl), \"Invalid buffer length\");\n\t\t\n\t\t// Find the minimal version number to use\n\t\tlet mut version: Version = minversion;\n\t\tlet datausedbits: usize = loop {\n\t\t\tlet datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;  // Number of data bits available\n\t\t\tlet dataused: Option<usize> = QrSegment::get_total_bits(segs, version);\n\t\t\tif dataused.map_or(false, |n| n <= datacapacitybits) {\n\t\t\t\tbreak dataused.unwrap();  // This version number is found to be suitable\n\t\t\t} else if version >= maxversion {  // All versions in the range could not fit the given data\n\t\t\t\treturn Err(match dataused {\n\t\t\t\t\tNone => DataTooLong::SegmentTooLong,\n\t\t\t\t\tSome(n) => DataTooLong::DataOverCapacity(n, datacapacitybits),\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tversion = Version::new(version.value() + 1);\n\t\t\t}\n\t\t};\n\t\t\n\t\t// Increase the error correction level while the data still fits in the current version number\n\t\tfor &newecl in &[QrCodeEcc::Medium, QrCodeEcc::Quartile, QrCodeEcc::High] {  // From low to high\n\t\t\tif boostecl && datausedbits <= QrCode::get_num_data_codewords(version, newecl) * 8 {\n\t\t\t\tecl = newecl;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Concatenate all segments to create the data bit string\n\t\tlet datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;\n\t\tlet mut bb = BitBuffer::new(&mut outbuffer[ .. datacapacitybits/8]);\n\t\tfor seg in segs {\n\t\t\tbb.append_bits(seg.mode.mode_bits(), 4);\n\t\t\tbb.append_bits(u32::try_from(seg.numchars).unwrap(), seg.mode.num_char_count_bits(version));\n\t\t\tfor i in 0 .. seg.bitlength {\n\t\t\t\tlet bit: u8 = (seg.data[i >> 3] >> (7 - (i & 7))) & 1;\n\t\t\t\tbb.append_bits(bit.into(), 1);\n\t\t\t}\n\t\t}\n\t\tdebug_assert_eq!(bb.length, datausedbits);\n\t\t\n\t\t// Add terminator and pad up to a byte if applicable\n\t\tlet numzerobits: usize = core::cmp::min(4, datacapacitybits - bb.length);\n\t\tbb.append_bits(0, u8::try_from(numzerobits).unwrap());\n\t\tlet numzerobits: usize = bb.length.wrapping_neg() & 7;\n\t\tbb.append_bits(0, u8::try_from(numzerobits).unwrap());\n\t\tdebug_assert_eq!(bb.length % 8, 0);\n\t\t\n\t\t// Pad with alternating bytes until data capacity is reached\n\t\tfor &padbyte in [0xEC, 0x11].iter().cycle() {\n\t\t\tif bb.length >= datacapacitybits {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbb.append_bits(padbyte, 8);\n\t\t}\n\t\tOk((bb.length / 8, ecl, version))\n\t}\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/// Creates a new QR Code with the given version number,\n\t/// error correction level, data codeword bytes, and mask number.\n\t/// \n\t/// This is a low-level API that most users should not use directly.\n\t/// A mid-level API is the `encode_segments_to_codewords()` function.\n\tpub fn encode_codewords<'b>(mut datacodewordsandoutbuffer: &'a mut [u8], datacodewordslen: usize, mut tempbuffer: &'b mut [u8],\n\t\t\tecl: QrCodeEcc, version: Version, mut msk: Option<Mask>) -> QrCode<'a> {\n\t\t\n\t\tdatacodewordsandoutbuffer = &mut datacodewordsandoutbuffer[ .. version.buffer_len()];\n\t\ttempbuffer                = &mut tempbuffer               [ .. version.buffer_len()];\n\t\t\n\t\t// Compute ECC\n\t\tlet rawcodewords: usize = QrCode::get_num_raw_data_modules(version) / 8;\n\t\tassert!(datacodewordslen <= rawcodewords);\n\t\tlet (data, temp) = datacodewordsandoutbuffer.split_at_mut(datacodewordslen);\n\t\tlet allcodewords = Self::add_ecc_and_interleave(data, version, ecl, temp, tempbuffer);\n\t\t\n\t\t// Draw modules\n\t\tlet mut result: QrCode = QrCode::<'a>::function_modules_marked(datacodewordsandoutbuffer, version);\n\t\tresult.draw_codewords(allcodewords);\n\t\tresult.draw_light_function_modules();\n\t\tlet funcmods: QrCode = QrCode::<'b>::function_modules_marked(tempbuffer, version);  // Just a grid, not a real QR Code\n\t\t\n\t\t// Do masking\n\t\tif msk.is_none() {  // Automatically choose best mask\n\t\t\tlet mut minpenalty = core::i32::MAX;\n\t\t\tfor i in 0u8 .. 8 {\n\t\t\t\tlet i = Mask::new(i);\n\t\t\t\tresult.apply_mask(&funcmods, i);\n\t\t\t\tresult.draw_format_bits(ecl, i);\n\t\t\t\tlet penalty: i32 = result.get_penalty_score();\n\t\t\t\tif penalty < minpenalty {\n\t\t\t\t\tmsk = Some(i);\n\t\t\t\t\tminpenalty = penalty;\n\t\t\t\t}\n\t\t\t\tresult.apply_mask(&funcmods, i);  // Undoes the mask due to XOR\n\t\t\t}\n\t\t}\n\t\tlet msk: Mask = msk.unwrap();\n\t\tresult.apply_mask(&funcmods, msk);  // Apply the final choice of mask\n\t\tresult.draw_format_bits(ecl, msk);  // Overwrite old format bits\n\t\tresult\n\t}\n\t\n\t\n\t/*---- Public methods ----*/\n\t\n\t/// Returns this QR Code's version, in the range [1, 40].\n\tpub fn version(&self) -> Version {\n\t\tVersion::new((*self.size - 17) / 4)\n\t}\n\t\n\t\n\t/// Returns this QR Code's size, in the range [21, 177].\n\tpub fn size(&self) -> i32 {\n\t\ti32::from(*self.size)\n\t}\n\t\n\t\n\t/// Returns this QR Code's error correction level.\n\tpub fn error_correction_level(&self) -> QrCodeEcc {\n\t\tlet index =\n\t\t\tusize::from(self.get_module_bounded(0, 8)) << 1 |\n\t\t\tusize::from(self.get_module_bounded(1, 8)) << 0;\n\t\tuse QrCodeEcc::*;\n\t\t[Medium, Low, High, Quartile][index]\n\t}\n\t\n\t\n\t/// Returns this QR Code's mask, in the range [0, 7].\n\tpub fn mask(&self) -> Mask {\n\t\tMask::new(\n\t\t\tu8::from(self.get_module_bounded(2, 8)) << 2 |\n\t\t\tu8::from(self.get_module_bounded(3, 8)) << 1 |\n\t\t\tu8::from(self.get_module_bounded(4, 8)) << 0)\n\t}\n\t\n\t\n\t/// Returns the color of the module (pixel) at the given coordinates,\n\t/// which is `false` for light or `true` for dark.\n\t/// \n\t/// The top left corner has the coordinates (x=0, y=0). If the given\n\t/// coordinates are out of bounds, then `false` (light) is returned.\n\tpub fn get_module(&self, x: i32, y: i32) -> bool {\n\t\tlet range = 0 .. self.size();\n\t\trange.contains(&x) && range.contains(&y) && self.get_module_bounded(x as u8, y as u8)\n\t}\n\t\n\t\n\t// Returns the color of the module at the given coordinates, which must be in bounds.\n\tfn get_module_bounded(&self, x: u8, y: u8) -> bool {\n\t\tlet range = 0 .. *self.size;\n\t\tassert!(range.contains(&x) && range.contains(&y));\n\t\tlet index = usize::from(y) * usize::from(*self.size) + usize::from(x);\n\t\tlet byteindex: usize = index >> 3;\n\t\tlet bitindex: usize = index & 7;\n\t\tget_bit(self.modules[byteindex].into(), bitindex as u8)\n\t}\n\t\n\t\n\t// Sets the color of the module at the given coordinates, doing nothing if out of bounds.\n\tfn set_module_unbounded(&mut self, x: i32, y: i32, isdark: bool) {\n\t\tlet range = 0 .. self.size();\n\t\tif range.contains(&x) && range.contains(&y) {\n\t\t\tself.set_module_bounded(x as u8, y as u8, isdark);\n\t\t}\n\t}\n\t\n\t\n\t// Sets the color of the module at the given coordinates, which must be in bounds.\n\tfn set_module_bounded(&mut self, x: u8, y: u8, isdark: bool) {\n\t\tlet range = 0 .. *self.size;\n\t\tassert!(range.contains(&x) && range.contains(&y));\n\t\tlet index = usize::from(y) * usize::from(*self.size) + usize::from(x);\n\t\tlet byteindex: usize = index >> 3;\n\t\tlet bitindex: usize = index & 7;\n\t\tif isdark {\n\t\t\tself.modules[byteindex] |= 1u8 << bitindex;\n\t\t} else {\n\t\t\tself.modules[byteindex] &= !(1u8 << bitindex);\n\t\t}\n\t}\n\t\n\t\n\t/*---- Error correction code generation ----*/\n\t\n\t// Appends error correction bytes to each block of the given data array, then interleaves\n\t// bytes from the blocks, stores them in the output array, and returns a slice of resultbuf.\n\t// temp is used as a temporary work area and will be clobbered by this function.\n\tfn add_ecc_and_interleave<'b>(data: &[u8], ver: Version, ecl: QrCodeEcc, temp: &mut [u8], resultbuf: &'b mut [u8]) -> &'b [u8] {\n\t\tassert_eq!(data.len(), QrCode::get_num_data_codewords(ver, ecl));\n\t\t\n\t\t// Calculate parameter numbers\n\t\tlet numblocks: usize = QrCode::table_get(&NUM_ERROR_CORRECTION_BLOCKS, ver, ecl);\n\t\tlet blockecclen: usize = QrCode::table_get(&ECC_CODEWORDS_PER_BLOCK  , ver, ecl);\n\t\tlet rawcodewords: usize = QrCode::get_num_raw_data_modules(ver) / 8;\n\t\tlet numshortblocks: usize = numblocks - rawcodewords % numblocks;\n\t\tlet shortblockdatalen: usize = rawcodewords / numblocks - blockecclen;\n\t\tlet result = &mut resultbuf[ .. rawcodewords];\n\t\t\n\t\t// Split data into blocks, calculate ECC, and interleave\n\t\t// (not concatenate) the bytes into a single sequence\n\t\tlet rs = ReedSolomonGenerator::new(blockecclen);\n\t\tlet mut dat: &[u8] = data;\n\t\tlet ecc: &mut [u8] = &mut temp[ .. blockecclen];  // Temporary storage\n\t\tfor i in 0 .. numblocks {\n\t\t\tlet datlen: usize = shortblockdatalen + usize::from(i >= numshortblocks);\n\t\t\trs.compute_remainder(&dat[ .. datlen], ecc);\n\t\t\tlet mut k: usize = i;\n\t\t\tfor j in 0 .. datlen {  // Copy data\n\t\t\t\tif j == shortblockdatalen {\n\t\t\t\t\tk -= numshortblocks;\n\t\t\t\t}\n\t\t\t\tresult[k] = dat[j];\n\t\t\t\tk += numblocks;\n\t\t\t}\n\t\t\tlet mut k: usize = data.len() + i;\n\t\t\tfor j in 0 .. blockecclen {  // Copy ECC\n\t\t\t\tresult[k] = ecc[j];\n\t\t\t\tk += numblocks;\n\t\t\t}\n\t\t\tdat = &dat[datlen .. ];\n\t\t}\n\t\tdebug_assert_eq!(dat.len(), 0);\n\t\tresult\n\t}\n\t\n\t\n\t/*---- Drawing function modules ----*/\n\t\n\t// Creates a QR Code grid with light modules for the given\n\t// version's size, then marks every function module as dark.\n\tfn function_modules_marked(outbuffer: &'a mut [u8], ver: Version) -> Self {\n\t\tassert_eq!(outbuffer.len(), ver.buffer_len());\n\t\tlet parts: (&mut u8, &mut [u8]) = outbuffer.split_first_mut().unwrap();\n\t\tlet mut result = Self {\n\t\t\tsize: parts.0,\n\t\t\tmodules: parts.1,\n\t\t};\n\t\tlet size: u8 = ver.value() * 4 + 17;\n\t\t*result.size = size;\n\t\tresult.modules.fill(0);\n\t\t\n\t\t// Fill horizontal and vertical timing patterns\n\t\tresult.fill_rectangle(6, 0, 1, size);\n\t\tresult.fill_rectangle(0, 6, size, 1);\n\t\t\n\t\t// Fill 3 finder patterns (all corners except bottom right) and format bits\n\t\tresult.fill_rectangle(0, 0, 9, 9);\n\t\tresult.fill_rectangle(size - 8, 0, 8, 9);\n\t\tresult.fill_rectangle(0, size - 8, 9, 8);\n\t\t\n\t\t// Fill numerous alignment patterns\n\t\tlet mut alignpatposbuf = [0u8; 7];\n\t\tlet alignpatpos: &[u8] = result.get_alignment_pattern_positions(&mut alignpatposbuf);\n\t\tfor (i, pos0) in alignpatpos.iter().enumerate() {\n\t\t\tfor (j, pos1) in alignpatpos.iter().enumerate() {\n\t\t\t\t// Don't draw on the three finder corners\n\t\t\t\tif !((i == 0 && j == 0) || (i == 0 && j == alignpatpos.len() - 1) || (i == alignpatpos.len() - 1 && j == 0)) {\n\t\t\t\t\tresult.fill_rectangle(pos0 - 2, pos1 - 2, 5, 5);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Fill version blocks\n\t\tif ver.value() >= 7 {\n\t\t\tresult.fill_rectangle(size - 11, 0, 3, 6);\n\t\t\tresult.fill_rectangle(0, size - 11, 6, 3);\n\t\t}\n\t\t\n\t\tresult\n\t}\n\t\n\t\n\t// Draws light function modules and possibly some dark modules onto this QR Code, without changing\n\t// non-function modules. This does not draw the format bits. This requires all function modules to be previously\n\t// marked dark (namely by function_modules_marked()), because this may skip redrawing dark function modules.\n\tfn draw_light_function_modules(&mut self) {\n\t\t// Draw horizontal and vertical timing patterns\n\t\tlet size: u8 = *self.size;\n\t\tfor i in (7 .. size-7).step_by(2) {\n\t\t\tself.set_module_bounded(6, i, false);\n\t\t\tself.set_module_bounded(i, 6, false);\n\t\t}\n\t\t\n\t\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\t\tfor dy in -4i32 ..= 4 {\n\t\t\tfor dx in -4i32 ..= 4 {\n\t\t\t\tlet dist: i32 = dx.abs().max(dy.abs());\n\t\t\t\tif dist == 2 || dist == 4 {\n\t\t\t\t\tself.set_module_unbounded(3 + dx, 3 + dy, false);\n\t\t\t\t\tself.set_module_unbounded(i32::from(size) - 4 + dx, 3 + dy, false);\n\t\t\t\t\tself.set_module_unbounded(3 + dx, i32::from(size) - 4 + dy, false);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Draw numerous alignment patterns\n\t\tlet mut alignpatposbuf = [0u8; 7];\n\t\tlet alignpatpos: &[u8] = self.get_alignment_pattern_positions(&mut alignpatposbuf);\n\t\tfor (i, &pos0) in alignpatpos.iter().enumerate() {\n\t\t\tfor (j, &pos1) in alignpatpos.iter().enumerate() {\n\t\t\t\tif (i == 0 && j == 0) || (i == 0 && j == alignpatpos.len() - 1) || (i == alignpatpos.len() - 1 && j == 0) {\n\t\t\t\t\tcontinue;  // Don't draw on the three finder corners\n\t\t\t\t}\n\t\t\t\tfor dy in -1 ..= 1 {\n\t\t\t\t\tfor dx in -1 ..= 1 {\n\t\t\t\t\t\tself.set_module_bounded((i32::from(pos0) + dx) as u8, (i32::from(pos1) + dy) as u8, dx == 0 && dy == 0);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Draw version blocks\n\t\tlet ver = u32::from(self.version().value());  // uint6, in the range [7, 40]\n\t\tif ver >= 7 {\n\t\t\t// Calculate error correction code and pack bits\n\t\t\tlet bits: u32 = {\n\t\t\t\tlet mut rem: u32 = ver;\n\t\t\t\tfor _ in 0 .. 12 {\n\t\t\t\t\trem = (rem << 1) ^ ((rem >> 11) * 0x1F25);\n\t\t\t\t}\n\t\t\t\tver << 12 | rem  // uint18\n\t\t\t};\n\t\t\tdebug_assert_eq!(bits >> 18, 0);\n\t\t\t\n\t\t\t// Draw two copies\n\t\t\tfor i in 0u8 .. 18 {\n\t\t\t\tlet bit: bool = get_bit(bits, i);\n\t\t\t\tlet a: u8 = size - 11 + i % 3;\n\t\t\t\tlet b: u8 = i / 3;\n\t\t\t\tself.set_module_bounded(a, b, bit);\n\t\t\t\tself.set_module_bounded(b, a, bit);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Draws two copies of the format bits (with its own error correction code) based\n\t// on the given mask and error correction level. This always draws all modules of\n\t// the format bits, unlike draw_light_function_modules() which might skip dark modules.\n\tfn draw_format_bits(&mut self, ecl: QrCodeEcc, mask: Mask) {\n\t\t// Calculate error correction code and pack bits\n\t\tlet bits: u32 = {\n\t\t\t// errcorrlvl is uint2, mask is uint3\n\t\t\tlet data = u32::from(ecl.format_bits() << 3 | mask.value());\n\t\t\tlet mut rem: u32 = data;\n\t\t\tfor _ in 0 .. 10 {\n\t\t\t\trem = (rem << 1) ^ ((rem >> 9) * 0x537);\n\t\t\t}\n\t\t\t(data << 10 | rem) ^ 0x5412  // uint15\n\t\t};\n\t\tdebug_assert_eq!(bits >> 15, 0);\n\t\t\n\t\t// Draw first copy\n\t\tfor i in 0 .. 6 {\n\t\t\tself.set_module_bounded(8, i, get_bit(bits, i));\n\t\t}\n\t\tself.set_module_bounded(8, 7, get_bit(bits, 6));\n\t\tself.set_module_bounded(8, 8, get_bit(bits, 7));\n\t\tself.set_module_bounded(7, 8, get_bit(bits, 8));\n\t\tfor i in 9 .. 15 {\n\t\t\tself.set_module_bounded(14 - i, 8, get_bit(bits, i));\n\t\t}\n\t\t\n\t\t// Draw second copy\n\t\tlet size: u8 = *self.size;\n\t\tfor i in 0 .. 8 {\n\t\t\tself.set_module_bounded(size - 1 - i, 8, get_bit(bits, i));\n\t\t}\n\t\tfor i in 8 .. 15 {\n\t\t\tself.set_module_bounded(8, size - 15 + i, get_bit(bits, i));\n\t\t}\n\t\tself.set_module_bounded(8, size - 8, true);  // Always dark\n\t}\n\t\n\t\n\t// Sets every module in the range [left : left + width] * [top : top + height] to dark.\n\tfn fill_rectangle(&mut self, left: u8, top: u8, width: u8, height: u8) {\n\t\tfor dy in 0 .. height {\n\t\t\tfor dx in 0 .. width {\n\t\t\t\tself.set_module_bounded(left + dx, top + dy, true);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t/*---- Drawing data modules and masking ----*/\n\t\n\t// Draws the raw codewords (including data and ECC) onto this QR Code. This requires the initial state of\n\t// the QR Code to be dark at function modules and light at codeword modules (including unused remainder bits).\n\tfn draw_codewords(&mut self, data: &[u8]) {\n\t\tassert_eq!(data.len(), QrCode::get_num_raw_data_modules(self.version()) / 8, \"Illegal argument\");\n\t\t\n\t\tlet size: i32 = self.size();\n\t\tlet mut i: usize = 0;  // Bit index into the data\n\t\t// Do the funny zigzag scan\n\t\tlet mut right: i32 = size - 1;\n\t\twhile right >= 1 {  // Index of right column in each column pair\n\t\t\tif right == 6 {\n\t\t\t\tright = 5;\n\t\t\t}\n\t\t\tfor vert in 0 .. size {  // Vertical counter\n\t\t\t\tfor j in 0 .. 2 {\n\t\t\t\t\tlet x = (right - j) as u8;  // Actual x coordinate\n\t\t\t\t\tlet upward: bool = (right + 1) & 2 == 0;\n\t\t\t\t\tlet y = (if upward { size - 1 - vert } else { vert }) as u8;  // Actual y coordinate\n\t\t\t\t\tif !self.get_module_bounded(x, y) && i < data.len() * 8 {\n\t\t\t\t\t\tself.set_module_bounded(x, y, get_bit(data[i >> 3].into(), 7 - ((i as u8) & 7)));\n\t\t\t\t\t\ti += 1;\n\t\t\t\t\t}\n\t\t\t\t\t// If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t\t// 0/false/light by the constructor and are left unchanged by this method\n\t\t\t\t}\n\t\t\t}\n\t\t\tright -= 2;\n\t\t}\n\t\tdebug_assert_eq!(i, data.len() * 8);\n\t}\n\t\n\t\n\t// XORs the codeword modules in this QR Code with the given mask pattern\n\t// and given pattern of function modules. The codeword bits must be drawn\n\t// before masking. Due to the arithmetic of XOR, calling apply_mask() with\n\t// the same mask value a second time will undo the mask. A final well-formed\n\t// QR Code needs exactly one (not zero, two, etc.) mask applied.\n\tfn apply_mask(&mut self, functionmodules: &QrCode, mask: Mask) {\n\t\tfor y in 0 .. *self.size {\n\t\t\tfor x in 0 .. *self.size {\n\t\t\t\tif functionmodules.get_module_bounded(x, y) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tlet invert: bool = {\n\t\t\t\t\tlet x = i32::from(x);\n\t\t\t\t\tlet y = i32::from(y);\n\t\t\t\t\tmatch mask.value() {\n\t\t\t\t\t\t0 => (x + y) % 2 == 0,\n\t\t\t\t\t\t1 => y % 2 == 0,\n\t\t\t\t\t\t2 => x % 3 == 0,\n\t\t\t\t\t\t3 => (x + y) % 3 == 0,\n\t\t\t\t\t\t4 => (x / 3 + y / 2) % 2 == 0,\n\t\t\t\t\t\t5 => x * y % 2 + x * y % 3 == 0,\n\t\t\t\t\t\t6 => (x * y % 2 + x * y % 3) % 2 == 0,\n\t\t\t\t\t\t7 => ((x + y) % 2 + x * y % 3) % 2 == 0,\n\t\t\t\t\t\t_ => unreachable!(),\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t\tself.set_module_bounded(x, y,\n\t\t\t\t\tself.get_module_bounded(x, y) ^ invert);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\n\tfn get_penalty_score(&self) -> i32 {\n\t\tlet mut result: i32 = 0;\n\t\tlet size: u8 = *self.size;\n\t\t\n\t\t// Adjacent modules in row having same color, and finder-like patterns\n\t\tfor y in 0 .. size {\n\t\t\tlet mut runcolor = false;\n\t\t\tlet mut runx: i32 = 0;\n\t\t\tlet mut runhistory = FinderPenalty::new(size);\n\t\t\tfor x in 0 .. size {\n\t\t\t\tif self.get_module_bounded(x, y) == runcolor {\n\t\t\t\t\trunx += 1;\n\t\t\t\t\tif runx == 5 {\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\t} else if runx > 5 {\n\t\t\t\t\t\tresult += 1;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\trunhistory.add_history(runx);\n\t\t\t\t\tif !runcolor {\n\t\t\t\t\t\tresult += runhistory.count_patterns() * PENALTY_N3;\n\t\t\t\t\t}\n\t\t\t\t\truncolor = self.get_module_bounded(x, y);\n\t\t\t\t\trunx = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += runhistory.terminate_and_count(runcolor, runx) * PENALTY_N3;\n\t\t}\n\t\t// Adjacent modules in column having same color, and finder-like patterns\n\t\tfor x in 0 .. size {\n\t\t\tlet mut runcolor = false;\n\t\t\tlet mut runy: i32 = 0;\n\t\t\tlet mut runhistory = FinderPenalty::new(size);\n\t\t\tfor y in 0 .. size {\n\t\t\t\tif self.get_module_bounded(x, y) == runcolor {\n\t\t\t\t\truny += 1;\n\t\t\t\t\tif runy == 5 {\n\t\t\t\t\t\tresult += PENALTY_N1;\n\t\t\t\t\t} else if runy > 5 {\n\t\t\t\t\t\tresult += 1;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\trunhistory.add_history(runy);\n\t\t\t\t\tif !runcolor {\n\t\t\t\t\t\tresult += runhistory.count_patterns() * PENALTY_N3;\n\t\t\t\t\t}\n\t\t\t\t\truncolor = self.get_module_bounded(x, y);\n\t\t\t\t\truny = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult += runhistory.terminate_and_count(runcolor, runy) * PENALTY_N3;\n\t\t}\n\t\t\n\t\t// 2*2 blocks of modules having same color\n\t\tfor y in 0 .. size-1 {\n\t\t\tfor x in 0 .. size-1 {\n\t\t\t\tlet color: bool = self.get_module_bounded(x, y);\n\t\t\t\tif color == self.get_module_bounded(x + 1, y) &&\n\t\t\t\t   color == self.get_module_bounded(x, y + 1) &&\n\t\t\t\t   color == self.get_module_bounded(x + 1, y + 1) {\n\t\t\t\t\tresult += PENALTY_N2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Balance of dark and light modules\n\t\tlet dark = self.modules.iter().map(|x| x.count_ones()).sum::<u32>() as i32;\n\t\tlet total = i32::from(size) * i32::from(size);  // Note that size is odd, so dark/total != 1/2\n\t\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\t\tlet k: i32 = ((dark * 20 - total * 10).abs() + total - 1) / total - 1;\n\t\tdebug_assert!(0 <= k && k <= 9);\n\t\tresult += k * PENALTY_N4;\n\t\tdebug_assert!(0 <= result && result <= 2568888);  // Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\t\tresult\n\t}\n\t\n\t\n\t/*---- Private helper functions ----*/\n\t\n\t// Calculates and stores an ascending list of positions of alignment patterns\n\t// for this version number, returning a slice of resultbuf.\n\t// Each position is in the range [0,177), and are used on both the x and y axes.\n\t// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.\n\tfn get_alignment_pattern_positions<'b>(&self, resultbuf: &'b mut [u8; 7]) -> &'b [u8] {\n\t\tlet ver: u8 = self.version().value();\n\t\tif ver == 1 {\n\t\t\t&resultbuf[ .. 0]\n\t\t} else {\n\t\t\tlet numalign: u8 = ver / 7 + 2;\n\t\t\tlet step = u8::try_from((i32::from(ver) * 8 + i32::from(numalign) * 3 + 5)\n\t\t\t\t/ (i32::from(numalign) * 4 - 4) * 2).unwrap();\n\t\t\tlet result = &mut resultbuf[ .. usize::from(numalign)];\n\t\t\tfor i in 0 .. numalign-1 {\n\t\t\t\tresult[usize::from(i)] = *self.size - 7 - i * step;\n\t\t\t}\n\t\t\t*result.last_mut().unwrap() = 6;\n\t\t\tresult.reverse();\n\t\t\tresult\n\t\t}\n\t}\n\t\n\t\n\t// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\n\tfn get_num_raw_data_modules(ver: Version) -> usize {\n\t\tlet ver = usize::from(ver.value());\n\t\tlet mut result: usize = (16 * ver + 128) * ver + 64;\n\t\tif ver >= 2 {\n\t\t\tlet numalign: usize = ver / 7 + 2;\n\t\t\tresult -= (25 * numalign - 10) * numalign - 55;\n\t\t\tif ver >= 7 {\n\t\t\t\tresult -= 36;\n\t\t\t}\n\t\t}\n\t\tdebug_assert!((208 ..= 29648).contains(&result));\n\t\tresult\n\t}\n\t\n\t\n\t// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t// QR Code of the given version number and error correction level, with remainder bits discarded.\n\t// This stateless pure function could be implemented as a (40*4)-cell lookup table.\n\tfn get_num_data_codewords(ver: Version, ecl: QrCodeEcc) -> usize {\n\t\tQrCode::get_num_raw_data_modules(ver) / 8\n\t\t\t- QrCode::table_get(&ECC_CODEWORDS_PER_BLOCK    , ver, ecl)\n\t\t\t* QrCode::table_get(&NUM_ERROR_CORRECTION_BLOCKS, ver, ecl)\n\t}\n\t\n\t\n\t// Returns an entry from the given table based on the given values.\n\tfn table_get(table: &'static [[i8; 41]; 4], ver: Version, ecl: QrCodeEcc) -> usize {\n\t\ttable[ecl.ordinal()][usize::from(ver.value())] as usize\n\t}\n\t\n}\n\n\nimpl PartialEq for QrCode<'_> {\n\tfn eq(&self, other: &QrCode<'_>) -> bool{\n\t\t*self.size    == *other.size    &&\n\t\t*self.modules == *other.modules\n\t}\n}\n\nimpl Eq for QrCode<'_> {}\n\n\n/*---- Helper struct for add_ecc_and_interleave() ----*/\n\nstruct ReedSolomonGenerator {\n\t\n\t// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].\n\tdivisor: [u8; 30],\n\t\n\t// The degree of the divisor polynomial, in the range [1, 30].\n\tdegree: usize,\n\t\n}\n\n\nimpl ReedSolomonGenerator {\n\t\n\t// Creates a Reed-Solomon ECC generator polynomial for the given degree. This could be\n\t// implemented as a lookup table over all possible parameter values, instead of as an algorithm.\n\tfn new(degree: usize) -> Self {\n\t\tlet mut result = Self {\n\t\t\tdivisor: [0u8; 30],\n\t\t\tdegree: degree,\n\t\t};\n\t\tassert!((1 ..= result.divisor.len()).contains(&degree), \"Degree out of range\");\n\t\tlet divisor: &mut [u8] = &mut result.divisor[ .. degree];\n\t\tdivisor[degree - 1] = 1;  // Start off with the monomial x^0\n\t\t\n\t\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t\t// and drop the highest monomial term which is always 1x^degree.\n\t\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\t\tlet mut root: u8 = 1;\n\t\tfor _ in 0 .. degree {  // Unused variable i\n\t\t\t// Multiply the current product by (x - r^i)\n\t\t\tfor j in 0 .. degree {\n\t\t\t\tdivisor[j] = Self::multiply(divisor[j], root);\n\t\t\t\tif j + 1 < divisor.len() {\n\t\t\t\t\tdivisor[j] ^= divisor[j + 1];\n\t\t\t\t}\n\t\t\t}\n\t\t\troot = Self::multiply(root, 0x02);\n\t\t}\n\t\tresult\n\t}\n\t\n\t\n\t// Returns the Reed-Solomon error correction codeword for the given data polynomial and this divisor polynomial.\n\tfn compute_remainder(&self, data: &[u8], result: &mut [u8]) {\n\t\tassert_eq!(result.len(), self.degree);\n\t\tresult.fill(0);\n\t\tfor b in data {  // Polynomial division\n\t\t\tlet factor: u8 = b ^ result[0];\n\t\t\tresult.copy_within(1 .. , 0);\n\t\t\tresult[result.len() - 1] = 0;\n\t\t\tfor (x, &y) in result.iter_mut().zip(self.divisor.iter()) {\n\t\t\t\t*x ^= Self::multiply(y, factor);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Returns the product of the two given field elements modulo GF(2^8/0x11D).\n\t// All inputs are valid. This could be implemented as a 256*256 lookup table.\n\tfn multiply(x: u8, y: u8) -> u8 {\n\t\t// Russian peasant multiplication\n\t\tlet mut z: u8 = 0;\n\t\tfor i in (0 .. 8).rev() {\n\t\t\tz = (z << 1) ^ ((z >> 7) * 0x1D);\n\t\t\tz ^= ((y >> i) & 1) * x;\n\t\t}\n\t\tz\n\t}\n\t\n}\n\n\n/*---- Helper struct for get_penalty_score() ----*/\n\nstruct FinderPenalty {\n\tqr_size: i32,\n\trun_history: [i32; 7],\n}\n\n\nimpl FinderPenalty {\n\t\n\tpub fn new(size: u8) -> Self {\n\t\tSelf {\n\t\t\tqr_size: i32::from(size),\n\t\t\trun_history: [0; 7],\n\t\t}\n\t}\n\t\n\t\n\t// Pushes the given value to the front and drops the last value.\n\tpub fn add_history(&mut self, mut currentrunlength: i32) {\n\t\tif self.run_history[0] == 0 {\n\t\t\tcurrentrunlength += self.qr_size;  // Add light border to initial run\n\t\t}\n\t\tlet len: usize = self.run_history.len();\n\t\tself.run_history.copy_within(0 .. len-1, 1);\n\t\tself.run_history[0] = currentrunlength;\n\t}\n\t\n\t\n\t// Can only be called immediately after a light run is added, and returns either 0, 1, or 2.\n\tpub fn count_patterns(&self) -> i32 {\n\t\tlet rh = &self.run_history;\n\t\tlet n = rh[1];\n\t\tdebug_assert!(n <= self.qr_size * 3);\n\t\tlet core = n > 0 && rh[2] == n && rh[3] == n * 3 && rh[4] == n && rh[5] == n;\n\t\t#[allow(unused_parens)]\n\t\t( i32::from(core && rh[0] >= n * 4 && rh[6] >= n)\n\t\t+ i32::from(core && rh[6] >= n * 4 && rh[0] >= n))\n\t}\n\t\n\t\n\t// Must be called at the end of a line (row or column) of modules.\n\tpub fn terminate_and_count(mut self, currentruncolor: bool, mut currentrunlength: i32) -> i32 {\n\t\tif currentruncolor {  // Terminate dark run\n\t\t\tself.add_history(currentrunlength);\n\t\t\tcurrentrunlength = 0;\n\t\t}\n\t\tcurrentrunlength += self.qr_size;  // Add light border to final run\n\t\tself.add_history(currentrunlength);\n\t\tself.count_patterns()\n\t}\n\t\n}\n\n\n/*---- Constants and tables ----*/\n\n// For use in get_penalty_score(), when evaluating which mask is best.\nconst PENALTY_N1: i32 =  3;\nconst PENALTY_N2: i32 =  3;\nconst PENALTY_N3: i32 = 40;\nconst PENALTY_N4: i32 = 10;\n\n\nstatic ECC_CODEWORDS_PER_BLOCK: [[i8; 41]; 4] = [\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t[-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\n\t[-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],  // Medium\n\t[-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],  // Quartile\n\t[-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],  // High\n];\n\nstatic NUM_ERROR_CORRECTION_BLOCKS: [[i8; 41]; 4] = [\n\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t//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\n\t[-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],  // Low\n\t[-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],  // Medium\n\t[-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],  // Quartile\n\t[-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],  // High\n];\n\n\n\n/*---- QrCodeEcc functionality ----*/\n\n/// The error correction level in a QR Code symbol.\n#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]\npub enum QrCodeEcc {\n\t/// The QR Code can tolerate about  7% erroneous codewords.\n\tLow     ,\n\t/// The QR Code can tolerate about 15% erroneous codewords.\n\tMedium  ,\n\t/// The QR Code can tolerate about 25% erroneous codewords.\n\tQuartile,\n\t/// The QR Code can tolerate about 30% erroneous codewords.\n\tHigh    ,\n}\n\n\nimpl QrCodeEcc {\n\t\n\t// Returns an unsigned 2-bit integer (in the range 0 to 3).\n\tfn ordinal(self) -> usize {\n\t\tuse QrCodeEcc::*;\n\t\tmatch self {\n\t\t\tLow      => 0,\n\t\t\tMedium   => 1,\n\t\t\tQuartile => 2,\n\t\t\tHigh     => 3,\n\t\t}\n\t}\n\t\n\t\n\t// Returns an unsigned 2-bit integer (in the range 0 to 3).\n\tfn format_bits(self) -> u8 {\n\t\tuse QrCodeEcc::*;\n\t\tmatch self {\n\t\t\tLow      => 1,\n\t\t\tMedium   => 0,\n\t\t\tQuartile => 3,\n\t\t\tHigh     => 2,\n\t\t}\n\t}\n\t\n}\n\n\n\n/*---- QrSegment functionality ----*/\n\n/// A segment of character/binary/control data in a QR Code symbol.\n/// \n/// Instances of this struct are immutable.\n/// \n/// The mid-level way to create a segment is to take the payload data\n/// and call a static factory function such as `QrSegment::make_numeric()`.\n/// The low-level way to create a segment is to custom-make the bit buffer\n/// and call the `QrSegment::new()` constructor with appropriate values.\n/// \n/// This segment struct imposes no length restrictions, but QR Codes have restrictions.\n/// Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n/// Any segment longer than this is meaningless for the purpose of generating QR Codes.\npub struct QrSegment<'a> {\n\t\n\t// The mode indicator of this segment. Accessed through mode().\n\tmode: QrSegmentMode,\n\t\n\t// The length of this segment's unencoded data. Measured in characters for\n\t// numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t// Not the same as the data's bit length. Accessed through num_chars().\n\tnumchars: usize,\n\t\n\t// The data bits of this segment, packed in bitwise big endian.\n\tdata: &'a [u8],\n\t\n\t// The number of valid data bits used in the buffer. Requires bitlength <= data.len() * 8.\n\t// The character count (numchars) must agree with the mode and the bit buffer length.\n\tbitlength: usize,\n\t\n}\n\n\nimpl<'a> QrSegment<'a> {\n\t\n\t/*---- Static factory functions (mid level) ----*/\n\t\n\t/// Returns a segment representing the given binary data encoded in byte mode.\n\t/// \n\t/// All input byte slices are acceptable.\n\t/// \n\t/// Any text string can be converted to UTF-8 bytes and encoded as a byte mode segment.\n\tpub fn make_bytes(data: &'a [u8]) -> Self {\n\t\tQrSegment::new(QrSegmentMode::Byte, data.len(), data, data.len().checked_mul(8).unwrap())\n\t}\n\t\n\t\n\t/// Returns a segment representing the given string of decimal digits encoded in numeric mode.\n\t/// \n\t/// Panics if the string contains non-digit characters.\n\tpub fn make_numeric(text: &str, buf: &'a mut [u8]) -> Self {\n\t\tassert!(text.bytes().all(|b| (b'0' ..= b'9').contains(&b)), \"String contains non-numeric characters\");\n\t\tlet mut bb = BitBuffer::new(buf);\n\t\tfor chunk in text.as_bytes().chunks(3) {\n\t\t\tlet data: u32 = chunk.iter().fold(0u32,\n\t\t\t\t|acc, &b| acc * 10 + u32::from(b - b'0'));\n\t\t\tbb.append_bits(data, (chunk.len() as u8) * 3 + 1);\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Numeric, text.len(), bb.data, bb.length)\n\t}\n\t\n\t\n\t/// Returns a segment representing the given text string encoded in alphanumeric mode.\n\t/// \n\t/// The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t/// dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t/// \n\t/// Panics if the string contains non-encodable characters.\n\tpub fn make_alphanumeric(text: &str, buf: &'a mut [u8]) -> Self {\n\t\tlet mut bb = BitBuffer::new(buf);\n\t\tfor chunk in text.as_bytes().chunks(2) {\n\t\t\tlet data: u32 = chunk.iter().fold(0u32, |acc, &b| acc * 45 + u32::try_from(\n\t\t\t\tALPHANUMERIC_CHARSET.find(char::from(b)).expect(\"String contains unencodable characters in alphanumeric mode\")).unwrap());\n\t\t\tbb.append_bits(data, (chunk.len() as u8) * 5 + 1);\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Alphanumeric, text.len(), bb.data, bb.length)\n\t}\n\t\n\t\n\t/// Returns a segment representing an Extended Channel Interpretation\n\t/// (ECI) designator with the given assignment value.\n\tpub fn make_eci(assignval: u32, buf: &'a mut [u8]) -> Self {\n\t\tlet mut bb = BitBuffer::new(buf);\n\t\tif assignval < (1 << 7) {\n\t\t\tbb.append_bits(assignval, 8);\n\t\t} else if assignval < (1 << 14) {\n\t\t\tbb.append_bits(0b10, 2);\n\t\t\tbb.append_bits(assignval, 14);\n\t\t} else if assignval < 1_000_000 {\n\t\t\tbb.append_bits(0b110, 3);\n\t\t\tbb.append_bits(assignval, 21);\n\t\t} else {\n\t\t\tpanic!(\"ECI assignment value out of range\");\n\t\t}\n\t\tQrSegment::new(QrSegmentMode::Eci, 0, bb.data, bb.length)\n\t}\n\t\n\t\n\t/*---- Constructor (low level) ----*/\n\t\n\t/// Creates a new QR Code segment with the given attributes and data.\n\t/// \n\t/// The character count (numchars) must agree with the mode and\n\t/// the bit buffer length, but the constraint isn't checked.\n\tpub fn new(mode: QrSegmentMode, numchars: usize, data: &'a [u8], bitlength: usize) -> Self {\n\t\tassert!(bitlength == 0 || (bitlength - 1) / 8 < data.len());\n\t\tSelf { mode, numchars, data, bitlength }\n\t}\n\t\n\t\n\t/*---- Instance field getters ----*/\n\t\n\t/// Returns the mode indicator of this segment.\n\tpub fn mode(&self) -> QrSegmentMode {\n\t\tself.mode\n\t}\n\t\n\t\n\t/// Returns the character count field of this segment.\n\tpub fn num_chars(&self) -> usize {\n\t\tself.numchars\n\t}\n\t\n\t\n\t/*---- Other static functions ----*/\n\t\n\t/// Returns the number of bytes needed for the data buffer of a segment\n\t/// containing the given number of characters using the given mode, or None if the\n\t/// internal calculation of the number of needed bits exceeds usize::MAX. Notes:\n\t/// \n\t/// - It is okay for the user to allocate more bytes for the buffer than needed.\n\t/// - For byte mode, numchars measures the number of bytes, not Unicode code points.\n\t/// - For ECI mode, numchars must be 0, and the worst-case number of bytes is returned.\n\t///   An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.\n\tpub fn calc_buffer_size(mode: QrSegmentMode, numchars: usize) -> Option<usize> {\n\t\tlet temp = Self::calc_bit_length(mode, numchars)?;\n\t\tSome(temp / 8 + usize::from(temp % 8 != 0))  // ceil(temp / 8)\n\t}\n\t\n\t\n\t// Returns the number of data bits needed to represent a segment\n\t// containing the given number of characters using the given mode,\n\t// or None if the the number of needed bits exceeds usize::MAX. Notes:\n\t// - For byte mode, numchars measures the number of bytes, not Unicode code points.\n\t// - For ECI mode, numchars must be 0, and the worst-case number of bits is returned.\n\t//   An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.\n\tfn calc_bit_length(mode: QrSegmentMode, numchars: usize) -> Option<usize> {\n\t\t// Returns ceil((numer / denom) * numchars)\n\t\tlet mul_frac_ceil = |numer: usize, denom: usize|\n\t\t\tSome(numchars)\n\t\t\t\t.and_then(|x| x.checked_mul(numer))\n\t\t\t\t.and_then(|x| x.checked_add(denom - 1))\n\t\t\t\t.map(|x| x / denom);\n\t\t\n\t\tuse QrSegmentMode::*;\n\t\tmatch mode {\n\t\t\tNumeric      => mul_frac_ceil(10, 3),\n\t\t\tAlphanumeric => mul_frac_ceil(11, 2),\n\t\t\tByte         => mul_frac_ceil( 8, 1),\n\t\t\tKanji        => mul_frac_ceil(13, 1),\n\t\t\tEci => {\n\t\t\t\tassert_eq!(numchars, 0);\n\t\t\t\tSome(3 * 8)\n\t\t\t},\n\t\t}\n\t}\n\t\n\t\n\t// Calculates and returns the number of bits needed to encode the given\n\t// segments at the given version. The result is None if a segment has too many\n\t// characters to fit its length field, or the total bits exceeds usize::MAX.\n\tfn get_total_bits(segs: &[Self], version: Version) -> Option<usize> {\n\t\tlet mut result: usize = 0;\n\t\tfor seg in segs {\n\t\t\tlet ccbits: u8 = seg.mode.num_char_count_bits(version);\n\t\t\t// ccbits can be as large as 16, but usize can be as small as 16\n\t\t\tif let Some(limit) = 1usize.checked_shl(ccbits.into()) {\n\t\t\t\tif seg.numchars >= limit {\n\t\t\t\t\treturn None;  // The segment's length doesn't fit the field's bit width\n\t\t\t\t}\n\t\t\t}\n\t\t\tresult = result.checked_add(4 + usize::from(ccbits))?;\n\t\t\tresult = result.checked_add(seg.bitlength)?;\n\t\t}\n\t\tSome(result)\n\t}\n\t\n\t\n\t/// Tests whether the given string can be encoded as a segment in numeric mode.\n\t/// A string is encodable iff each character is in the range 0 to 9.\n\tpub fn is_numeric(text: &str) -> bool {\n\t\ttext.chars().all(|c| ('0' ..= '9').contains(&c))\n\t}\n\t\n\t\n\t/// Tests whether the given string can be encoded as a segment in alphanumeric mode.\n\t/// A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t/// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\tpub fn is_alphanumeric(text: &str) -> bool {\n\t\ttext.chars().all(|c| ALPHANUMERIC_CHARSET.contains(c))\n\t}\n\t\n}\n\n\n// The set of all legal characters in alphanumeric mode,\n// where each character value maps to the index in the string.\nstatic ALPHANUMERIC_CHARSET: &str = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\n\n\n/*---- QrSegmentMode functionality ----*/\n\n/// Describes how a segment's data bits are interpreted.\n#[derive(Clone, Copy, PartialEq, Eq, Debug)]\npub enum QrSegmentMode {\n\tNumeric,\n\tAlphanumeric,\n\tByte,\n\tKanji,\n\tEci,\n}\n\n\nimpl QrSegmentMode {\n\t\n\t// Returns an unsigned 4-bit integer value (range 0 to 15)\n\t// representing the mode indicator bits for this mode object.\n\tfn mode_bits(self) -> u32 {\n\t\tuse QrSegmentMode::*;\n\t\tmatch self {\n\t\t\tNumeric      => 0x1,\n\t\t\tAlphanumeric => 0x2,\n\t\t\tByte         => 0x4,\n\t\t\tKanji        => 0x8,\n\t\t\tEci          => 0x7,\n\t\t}\n\t}\n\t\n\t\n\t// Returns the bit width of the character count field for a segment in this mode\n\t// in a QR Code at the given version number. The result is in the range [0, 16].\n\tfn num_char_count_bits(self, ver: Version) -> u8 {\n\t\tuse QrSegmentMode::*;\n\t\t(match self {\n\t\t\tNumeric      => [10, 12, 14],\n\t\t\tAlphanumeric => [ 9, 11, 13],\n\t\t\tByte         => [ 8, 16, 16],\n\t\t\tKanji        => [ 8, 10, 12],\n\t\t\tEci          => [ 0,  0,  0],\n\t\t})[usize::from((ver.value() + 7) / 17)]\n\t}\n\t\n}\n\n\n/*---- BitBuffer functionality ----*/\n\n/// An appendable sequence of bits (0s and 1s).\n/// \n/// Mainly used by QrSegment.\npub struct BitBuffer<'a> {\n\t\n\tdata: &'a mut [u8],\n\t\n\tlength: usize,\n\t\n}\n\n\nimpl<'a> BitBuffer<'a> {\n\t\n\t// Creates a bit buffer based on the given byte array.\n\tpub fn new(buffer: &'a mut [u8]) -> Self {\n\t\tSelf {\n\t\t\tdata: buffer,\n\t\t\tlength: 0,\n\t\t}\n\t}\n\t\n\t\n\t// Returns the length of this bit buffer, in bits.\n\tpub fn len(&self) -> usize {\n\t\tself.length\n\t}\n\t\n\t\n\t// Appends the given number of low-order bits of the given value to this byte-based\n\t// bit buffer, increasing the bit length. Requires 0 <= numBits <= 31 and val < 2^numBits.\n\tpub fn append_bits(&mut self, val: u32, len: u8) {\n\t\tassert!(len <= 31 && val >> len == 0);\n\t\tassert!(usize::from(len) <= usize::MAX - self.length);\n\t\tfor i in (0 .. len).rev() {\n\t\t\tlet index: usize = self.length >> 3;\n\t\t\tlet shift: u8 = 7 - ((self.length as u8) & 7);\n\t\t\tlet bit: u8 = ((val >> i) as u8) & 1;\n\t\t\tif shift == 7 {\n\t\t\t\tself.data[index] = bit << shift;\n\t\t\t} else {\n\t\t\t\tself.data[index] |= bit << shift;\n\t\t\t}\n\t\t\tself.length += 1;\n\t\t}\n\t}\n\t\n}\n\n\n\n/*---- Miscellaneous values ----*/\n\n/// The error type when the supplied data does not fit any QR Code version.\n///\n/// Ways to handle this exception include:\n/// \n/// - Decrease the error correction level if it was greater than `QrCodeEcc::Low`.\n/// - Increase the maxversion argument if it was less than `Version::MAX`.\n/// - Split the text data into better or optimal segments in order to reduce the number of bits required.\n/// - Change the text or binary data to be shorter.\n/// - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).\n/// - Propagate the error upward to the caller/user.\n#[derive(Debug, Clone)]\npub enum DataTooLong {\n\tSegmentTooLong,\n\tDataOverCapacity(usize, usize),\n}\n\nimpl core::fmt::Display for DataTooLong {\n\tfn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {\n\t\tmatch *self {\n\t\t\tSelf::SegmentTooLong => write!(f, \"Segment too long\"),\n\t\t\tSelf::DataOverCapacity(datalen, maxcapacity) =>\n\t\t\t\twrite!(f, \"Data length = {} bits, Max capacity = {} bits\", datalen, maxcapacity),\n\t\t}\n\t}\n}\n\n\n/// A number between 1 and 40 (inclusive).\n#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]\npub struct Version(u8);\n\nimpl Version {\n\t/// The minimum version number supported in the QR Code Model 2 standard.\n\tpub const MIN: Version = Version( 1);\n\t\n\t/// The maximum version number supported in the QR Code Model 2 standard.\n\tpub const MAX: Version = Version(40);\n\t\n\t/// Creates a version object from the given number.\n\t/// \n\t/// Panics if the number is outside the range [1, 40].\n\tpub const fn new(ver: u8) -> Self {\n\t\tassert!(Version::MIN.value() <= ver && ver <= Version::MAX.value(), \"Version number out of range\");\n\t\tSelf(ver)\n\t}\n\t\n\t/// Returns the value, which is in the range [1, 40].\n\tpub const fn value(self) -> u8 {\n\t\tself.0\n\t}\n\t\n\t/// Returns the minimum length required for the output and temporary\n\t/// buffers when creating a QR Code of this version number.\n\tpub const fn buffer_len(self) -> usize {\n\t\tlet sidelen = (self.0 as usize) * 4 + 17;\n\t\t(sidelen * sidelen + 7) / 8 + 1\n\t}\n}\n\n\n/// A number between 0 and 7 (inclusive).\n#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]\npub struct Mask(u8);\n\nimpl Mask {\n\t/// Creates a mask object from the given number.\n\t/// \n\t/// Panics if the number is outside the range [0, 7].\n\tpub const fn new(mask: u8) -> Self {\n\t\tassert!(mask <= 7, \"Mask value out of range\");\n\t\tSelf(mask)\n\t}\n\t\n\t/// Returns the value, which is in the range [0, 7].\n\tpub const fn value(self) -> u8 {\n\t\tself.0\n\t}\n}\n\n\n// Returns true iff the i'th bit of x is set to 1.\nfn get_bit(x: u32, i: u8) -> bool {\n\t(x >> i) & 1 != 0\n}\n"
  },
  {
    "path": "typescript-javascript/Readme.markdown",
    "content": "QR Code generator library - TypeScript\n======================================\n\n\nIntroduction\n------------\n\nThis 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.\n\nHome page with live JavaScript demo, extensive descriptions, and competitor comparisons: https://www.nayuki.io/page/qr-code-generator-library\n\n\nFeatures\n--------\n\nCore features:\n\n* Significantly shorter code but more documentation comments compared to competing libraries\n* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard\n* Output format: Raw modules/pixels of the QR symbol\n* Detects finder-like penalty patterns more accurately than other implementations\n* Encodes numeric and special-alphanumeric text in less space than general text\n* Open-source code under the permissive MIT License\n\nManual parameters:\n\n* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data\n* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one\n* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number\n* User can create a list of data segments manually and add ECI segments\n\nMore information about QR Code technology and this library's design can be found on the project home page.\n\n\nExamples\n--------\n\n```typescript\n// Name abbreviated for the sake of these examples here\nconst QRC = qrcodegen.QrCode;\n\n// Simple operation\nconst qr0 = QRC.encodeText(\"Hello, world!\", QRC.Ecc.MEDIUM);\nconst svg = toSvgString(qr0, 4);  // See qrcodegen-input-demo\n\n// Manual operation\nconst segs = qrcodegen.QrSegment.makeSegments(\"3141592653589793238462643383\");\nconst qr1 = QRC.encodeSegments(segs, QRC.Ecc.HIGH, 5, 5, 2, false);\nfor (let y = 0; y < qr1.size; y++) {\n    for (let x = 0; x < qr1.size; x++) {\n        (... paint qr1.getModule(x, y) ...)\n    }\n}\n```\n\nMore complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/typescript-javascript/qrcodegen-output-demo.ts .\n"
  },
  {
    "path": "typescript-javascript/build.sh",
    "content": "# \n# Build script for QR Code generator (TypeScript)\n# \n# Copyright (c) Project Nayuki. (MIT License)\n# https://www.nayuki.io/page/qr-code-generator-library\n# \n# Permission is hereby granted, free of charge, to any person obtaining a copy of\n# this software and associated documentation files (the \"Software\"), to deal in\n# the Software without restriction, including without limitation the rights to\n# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n# the Software, and to permit persons to whom the Software is furnished to do so,\n# subject to the following conditions:\n# - The above copyright notice and this permission notice shall be included in\n#   all copies or substantial portions of the Software.\n# - The Software is provided \"as is\", without warranty of any kind, express or\n#   implied, including but not limited to the warranties of merchantability,\n#   fitness for a particular purpose and noninfringement. In no event shall the\n#   authors or copyright holders be liable for any claim, damages or other\n#   liability, whether in an action of contract, tort or otherwise, arising from,\n#   out of or in connection with the Software or the use or other dealings in the\n#   Software.\n# \n\ntsc --strict --lib DOM,DOM.Iterable,ES6 --target ES6 qrcodegen.ts qrcodegen-input-demo.ts\ntsc --strict --lib DOM,DOM.Iterable,ES6 --target ES6 qrcodegen.ts qrcodegen-output-demo.ts\n"
  },
  {
    "path": "typescript-javascript/qrcodegen-input-demo.html",
    "content": "<!--\n  - QR Code generator input demo (HTML+JavaScript)\n  - \n  - Copyright (c) Project Nayuki. (MIT License)\n  - https://www.nayuki.io/page/qr-code-generator-library\n  - \n  - Permission is hereby granted, free of charge, to any person obtaining a copy of\n  - this software and associated documentation files (the \"Software\"), to deal in\n  - the Software without restriction, including without limitation the rights to\n  - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n  - the Software, and to permit persons to whom the Software is furnished to do so,\n  - subject to the following conditions:\n  - * The above copyright notice and this permission notice shall be included in\n  -   all copies or substantial portions of the Software.\n  - * The Software is provided \"as is\", without warranty of any kind, express or\n  -   implied, including but not limited to the warranties of merchantability,\n  -   fitness for a particular purpose and noninfringement. In no event shall the\n  -   authors or copyright holders be liable for any claim, damages or other\n  -   liability, whether in an action of contract, tort or otherwise, arising from,\n  -   out of or in connection with the Software or the use or other dealings in the\n  -   Software.\n  -->\n<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t\t<title>QR Code generator input demo (JavaScript)</title>\n\t\t<style type=\"text/css\">\n\t\t\thtml {\n\t\t\t\tfont-family: sans-serif;\n\t\t\t}\n\t\t\th1 {\n\t\t\t\ttext-align: center;\n\t\t\t}\n\t\t\ttable {\n\t\t\t\tborder-collapse: collapse;\n\t\t\t}\n\t\t\ttd {\n\t\t\t\tvertical-align: top;\n\t\t\t\tpadding-top: 0.2em;\n\t\t\t\tpadding-bottom: 0.2em;\n\t\t\t}\n\t\t\ttd:first-child {\n\t\t\t\twhite-space: pre;\n\t\t\t\tpadding-right: 0.5em;\n\t\t\t}\n\t\t\tinput, textarea {\n\t\t\t\tfont-size: inherit;\n\t\t\t\tfont-family: inherit;\n\t\t\t}\n\t\t\tinput[type=radio], input[type=checkbox] {\n\t\t\t\tmargin: 0em 0.2em 0em 0em;\n\t\t\t\tpadding: 0em;\n\t\t\t}\n\t\t\tlabel + label {\n\t\t\t\tmargin-left: 0.8em;\n\t\t\t}\n\t\t\thr {\n\t\t\t\tmargin: 2em 0em;\n\t\t\t\tborder: none;\n\t\t\t\tborder-top: 0.1em solid #A0A0A0;\n\t\t\t}\n\t\t</style>\n\t</head>\n\t\n\t<body>\n\t\t<h1>QR Code generator input demo (JavaScript)</h1>\n\t\t<div id=\"loading\">\n\t\t\t<p>Loading application...</p>\n\t\t\t<p>(Are the JavaScript files missing?)</p>\n\t\t\t<p>(The JavaScript code needs to be compiled from the TypeScript code.)</p>\n\t\t</div>\n\t\t<form id=\"loaded\" hidden=\"hidden\" onsubmit=\"event.preventDefault();\">\n\t\t\t<table>\n\t\t\t\t<tbody>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td><strong>Text string:</strong></td>\n\t\t\t\t\t\t<td><textarea placeholder=\"Enter your text to be put into the QR Code\" id=\"text-input\" style=\"width:30em; height:5em\"></textarea></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td><strong>QR Code:</strong><br/><a id=\"download\">(download)</a></td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<canvas id=\"qrcode-canvas\" style=\"padding:1em; background-color:#E8E8E8\"></canvas>\n\t\t\t\t\t\t\t<svg id=\"qrcode-svg\" style=\"width:30em; height:30em; padding:1em; background-color:#E8E8E8\">\n\t\t\t\t\t\t\t\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\" stroke-width=\"0\"></rect>\n\t\t\t\t\t\t\t\t<path d=\"\" fill=\"#000000\" stroke-width=\"0\"></path>\n\t\t\t\t\t\t\t</svg>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td><strong>Error correction:</strong></td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<label><input type=\"radio\" name=\"errcorlvl\" id=\"errcorlvl-low\" checked=\"checked\">Low</label>\n\t\t\t\t\t\t\t<label><input type=\"radio\" name=\"errcorlvl\" id=\"errcorlvl-medium\">Medium</label>\n\t\t\t\t\t\t\t<label><input type=\"radio\" name=\"errcorlvl\" id=\"errcorlvl-quartile\">Quartile</label>\n\t\t\t\t\t\t\t<label><input type=\"radio\" name=\"errcorlvl\" id=\"errcorlvl-high\">High</label>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Output format:</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<label><input type=\"radio\" name=\"output-format\" id=\"output-format-bitmap\" checked=\"checked\">Bitmap</label>\n\t\t\t\t\t\t\t<label><input type=\"radio\" name=\"output-format\" id=\"output-format-vector\">Vector</label>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Border:</td>\n\t\t\t\t\t\t<td><input type=\"number\" value=\"4\" min=\"0\" max=\"100\" step=\"1\" id=\"border-input\" style=\"width:4em\"> modules</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr id=\"scale-row\">\n\t\t\t\t\t\t<td>Scale:</td>\n\t\t\t\t\t\t<td><input type=\"number\" value=\"8\" min=\"1\" max=\"30\" step=\"1\" id=\"scale-input\" style=\"width:4em\"> pixels per module</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Colors:</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\tLight = <input type=\"text\" value=\"#FFFFFF\" id=\"light-color-input\" style=\"width:6em\">,\n\t\t\t\t\t\t\tdark = <input type=\"text\" value=\"#000000\" id=\"dark-color-input\" style=\"width:6em\">\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Version range:</td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\tMinimum = <input type=\"number\" value=\"1\"  min=\"1\" max=\"40\" step=\"1\" id=\"version-min-input\" style=\"width:4em\" oninput=\"app.handleVersionMinMax('min');\">,\n\t\t\t\t\t\t\tmaximum = <input type=\"number\" value=\"40\" min=\"1\" max=\"40\" step=\"1\" id=\"version-max-input\" style=\"width:4em\" oninput=\"app.handleVersionMinMax('max');\">\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Mask pattern:</td>\n\t\t\t\t\t\t<td><input type=\"number\" value=\"-1\" min=\"-1\" max=\"7\" step=\"1\" id=\"mask-input\" style=\"width:4em\"> (−1 for automatic, 0 to 7 for manual)</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Boost ECC:</td>\n\t\t\t\t\t\t<td><label><input type=\"checkbox\" checked=\"checked\" id=\"boost-ecc-input\">Increase <abbr title=\"error-correcting code\">ECC</abbr> level within same version</label></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Statistics:</td>\n\t\t\t\t\t\t<td id=\"statistics-output\" style=\"white-space:pre\"></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</tbody>\n\t\t\t</table>\n\t\t</form>\n\t\t<script type=\"application/javascript\" src=\"qrcodegen.js\"></script>\n\t\t<script type=\"application/javascript\" src=\"qrcodegen-input-demo.js\"></script>\n\t\t\n\t\t<hr>\n\t\t<p>Copyright © Project Nayuki – <a href=\"https://www.nayuki.io/page/qr-code-generator-library\">https://www.nayuki.io/page/qr-code-generator-library</a></p>\n\t</body>\n</html>\n"
  },
  {
    "path": "typescript-javascript/qrcodegen-input-demo.ts",
    "content": "/* \n * QR Code generator input demo (TypeScript)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n\"use strict\";\n\n\nnamespace app {\n\t\n\tfunction initialize(): void {\n\t\tgetElem(\"loading\").hidden = true;\n\t\tgetElem(\"loaded\").hidden = false;\n\t\tlet elems = document.querySelectorAll(\"input[type=number], input[type=text], textarea\");\n\t\tfor (let el of elems) {\n\t\t\tif (el.id.indexOf(\"version-\") != 0)\n\t\t\t\t(el as any).oninput = redrawQrCode;\n\t\t}\n\t\telems = document.querySelectorAll(\"input[type=radio], input[type=checkbox]\");\n\t\tfor (let el of elems)\n\t\t\t(el as HTMLInputElement).onchange = redrawQrCode;\n\t\tredrawQrCode();\n\t}\n\t\n\t\n\tfunction redrawQrCode(): void {\n\t\t// Show/hide rows based on bitmap/vector image output\n\t\tconst bitmapOutput: boolean = getInput(\"output-format-bitmap\").checked;\n\t\tconst scaleRow : HTMLElement = getElem(\"scale-row\");\n\t\tscaleRow.hidden = !bitmapOutput;\n\t\tlet download = getElem(\"download\") as HTMLAnchorElement;\n\t\tdownload.download = \"qr-code.\" + (bitmapOutput ? \"png\" : \"svg\");\n\t\tdownload.removeAttribute(\"href\");\n\t\t\n\t\t// Reset output images in case of early termination\n\t\tconst canvas = getElem(\"qrcode-canvas\") as HTMLCanvasElement;\n\t\tconst svg = (document.getElementById(\"qrcode-svg\") as Element) as SVGElement;\n\t\tcanvas.hidden = true;\n\t\tsvg.style.display = \"none\";\n\t\t\n\t\t// Returns a QrCode.Ecc object based on the radio buttons in the HTML form.\n\t\tfunction getInputErrorCorrectionLevel(): qrcodegen.QrCode.Ecc {\n\t\t\tif (getInput(\"errcorlvl-medium\").checked)\n\t\t\t\treturn qrcodegen.QrCode.Ecc.MEDIUM;\n\t\t\telse if (getInput(\"errcorlvl-quartile\").checked)\n\t\t\t\treturn qrcodegen.QrCode.Ecc.QUARTILE;\n\t\t\telse if (getInput(\"errcorlvl-high\").checked)\n\t\t\t\treturn qrcodegen.QrCode.Ecc.HIGH;\n\t\t\telse  // In case no radio button is depressed\n\t\t\t\treturn qrcodegen.QrCode.Ecc.LOW;\n\t\t}\n\t\t\n\t\t// Get form inputs and compute QR Code\n\t\tconst ecl: qrcodegen.QrCode.Ecc = getInputErrorCorrectionLevel();\n\t\tconst text: string = (getElem(\"text-input\") as HTMLTextAreaElement).value;\n\t\tconst segs: Array<qrcodegen.QrSegment> = qrcodegen.QrSegment.makeSegments(text);\n\t\tconst minVer: number = parseInt(getInput(\"version-min-input\").value, 10);\n\t\tconst maxVer: number = parseInt(getInput(\"version-max-input\").value, 10);\n\t\tconst mask: number = parseInt(getInput(\"mask-input\").value, 10);\n\t\tconst boostEcc: boolean = getInput(\"boost-ecc-input\").checked;\n\t\tconst qr: qrcodegen.QrCode = qrcodegen.QrCode.encodeSegments(segs, ecl, minVer, maxVer, mask, boostEcc);\n\t\t\n\t\t// Draw image output\n\t\tconst border: number = parseInt(getInput(\"border-input\").value, 10);\n\t\tconst lightColor: string = getInput(\"light-color-input\").value;\n\t\tconst darkColor : string = getInput(\"dark-color-input\" ).value;\n\t\tif (border < 0 || border > 100)\n\t\t\treturn;\n\t\tif (bitmapOutput) {\n\t\t\tconst scale: number = parseInt(getInput(\"scale-input\").value, 10);\n\t\t\tif (scale <= 0 || scale > 30)\n\t\t\t\treturn;\n\t\t\tdrawCanvas(qr, scale, border, lightColor, darkColor, canvas);\n\t\t\tcanvas.hidden = false;\n\t\t\tdownload.href = canvas.toDataURL(\"image/png\");\n\t\t} else {\n\t\t\tconst code: string = toSvgString(qr, border, lightColor, darkColor);\n\t\t\tconst viewBox: string = (/ viewBox=\"([^\"]*)\"/.exec(code) as RegExpExecArray)[1];\n\t\t\tconst pathD: string = (/ d=\"([^\"]*)\"/.exec(code) as RegExpExecArray)[1];\n\t\t\tsvg.setAttribute(\"viewBox\", viewBox);\n\t\t\t(svg.querySelector(\"path\") as Element).setAttribute(\"d\", pathD);\n\t\t\t(svg.querySelector(\"rect\") as Element).setAttribute(\"fill\", lightColor);\n\t\t\t(svg.querySelector(\"path\") as Element).setAttribute(\"fill\", darkColor);\n\t\t\tsvg.style.removeProperty(\"display\");\n\t\t\tdownload.href = \"data:application/svg+xml,\" + encodeURIComponent(code);\n\t\t}\n\t\t\n\t\t// Returns a string to describe the given list of segments.\n\t\tfunction describeSegments(segs: Array<qrcodegen.QrSegment>): string {\n\t\t\tif (segs.length == 0)\n\t\t\t\treturn \"none\";\n\t\t\telse if (segs.length == 1) {\n\t\t\t\tconst mode: qrcodegen.QrSegment.Mode = segs[0].mode;\n\t\t\t\tconst Mode = qrcodegen.QrSegment.Mode;\n\t\t\t\tif (mode == Mode.NUMERIC     )  return \"numeric\";\n\t\t\t\tif (mode == Mode.ALPHANUMERIC)  return \"alphanumeric\";\n\t\t\t\tif (mode == Mode.BYTE        )  return \"byte\";\n\t\t\t\tif (mode == Mode.KANJI       )  return \"kanji\";\n\t\t\t\treturn \"unknown\";\n\t\t\t} else\n\t\t\t\treturn \"multiple\";\n\t\t}\n\t\t\n\t\t// Returns the number of Unicode code points in the given UTF-16 string.\n\t\tfunction countUnicodeChars(str: string): number {\n\t\t\tlet result: number = 0;\n\t\t\tfor (const ch of str) {\n\t\t\t\tconst cc = ch.codePointAt(0) as number;\n\t\t\t\tif (0xD800 <= cc && cc < 0xE000)\n\t\t\t\t\tthrow new RangeError(\"Invalid UTF-16 string\");\n\t\t\t\tresult++;\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t// Show the QR Code symbol's statistics as a string\n\t\tgetElem(\"statistics-output\").textContent = `QR Code version = ${qr.version}, ` +\n\t\t\t`mask pattern = ${qr.mask}, ` +\n\t\t\t`character count = ${countUnicodeChars(text)},\\n` +\n\t\t\t`encoding mode = ${describeSegments(segs)}, ` +\n\t\t\t`error correction = level ${\"LMQH\".charAt(qr.errorCorrectionLevel.ordinal)}, ` +\n\t\t\t`data bits = ${qrcodegen.QrSegment.getTotalBits(segs, qr.version) as number}.`;\n\t}\n\t\n\t\n\t// Draws the given QR Code, with the given module scale and border modules, onto the given HTML\n\t// canvas element. The canvas's width and height is resized to (qr.size + border * 2) * scale.\n\t// The drawn image is purely dark and light, and fully opaque.\n\t// The scale must be a positive integer and the border must be a non-negative integer.\n\tfunction drawCanvas(qr: qrcodegen.QrCode, scale: number, border: number, lightColor: string, darkColor: string, canvas: HTMLCanvasElement): void {\n\t\tif (scale <= 0 || border < 0)\n\t\t\tthrow new RangeError(\"Value out of range\");\n\t\tconst width: number = (qr.size + border * 2) * scale;\n\t\tcanvas.width = width;\n\t\tcanvas.height = width;\n\t\tlet ctx = canvas.getContext(\"2d\") as CanvasRenderingContext2D;\n\t\tfor (let y = -border; y < qr.size + border; y++) {\n\t\t\tfor (let x = -border; x < qr.size + border; x++) {\n\t\t\t\tctx.fillStyle = qr.getModule(x, y) ? darkColor : lightColor;\n\t\t\t\tctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\t// Returns a string of SVG code for an image depicting the given QR Code, with the given number\n\t// of border modules. The string always uses Unix newlines (\\n), regardless of the platform.\n\tfunction toSvgString(qr: qrcodegen.QrCode, border: number, lightColor: string, darkColor: string): string {\n\t\tif (border < 0)\n\t\t\tthrow new RangeError(\"Border must be non-negative\");\n\t\tlet parts: Array<string> = [];\n\t\tfor (let y = 0; y < qr.size; y++) {\n\t\t\tfor (let x = 0; x < qr.size; x++) {\n\t\t\t\tif (qr.getModule(x, y))\n\t\t\t\t\tparts.push(`M${x + border},${y + border}h1v1h-1z`);\n\t\t\t}\n\t\t}\n\t\treturn `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ${qr.size + border * 2} ${qr.size + border * 2}\" stroke=\"none\">\n\t<rect width=\"100%\" height=\"100%\" fill=\"${lightColor}\"/>\n\t<path d=\"${parts.join(\" \")}\" fill=\"${darkColor}\"/>\n</svg>\n`;\n\t}\n\t\n\t\n\texport function handleVersionMinMax(which: \"min\"|\"max\"): void {\n\t\tconst minElem: HTMLInputElement = getInput(\"version-min-input\");\n\t\tconst maxElem: HTMLInputElement = getInput(\"version-max-input\");\n\t\tlet minVal: number = parseInt(minElem.value, 10);\n\t\tlet maxVal: number = parseInt(maxElem.value, 10);\n\t\tminVal = Math.max(Math.min(minVal, qrcodegen.QrCode.MAX_VERSION), qrcodegen.QrCode.MIN_VERSION);\n\t\tmaxVal = Math.max(Math.min(maxVal, qrcodegen.QrCode.MAX_VERSION), qrcodegen.QrCode.MIN_VERSION);\n\t\tif (which == \"min\" && minVal > maxVal)\n\t\t\tmaxVal = minVal;\n\t\telse if (which == \"max\" && maxVal < minVal)\n\t\t\tminVal = maxVal;\n\t\tminElem.value = minVal.toString();\n\t\tmaxElem.value = maxVal.toString();\n\t\tredrawQrCode();\n\t}\n\t\n\t\n\tfunction getElem(id: string): HTMLElement {\n\t\tconst result: HTMLElement|null = document.getElementById(id);\n\t\tif (result instanceof HTMLElement)\n\t\t\treturn result;\n\t\tthrow new Error(\"Assertion error\");\n\t}\n\t\n\t\n\tfunction getInput(id: string): HTMLInputElement {\n\t\tconst result: HTMLElement = getElem(id);\n\t\tif (result instanceof HTMLInputElement)\n\t\t\treturn result;\n\t\tthrow new Error(\"Assertion error\");\n\t}\n\t\n\t\n\tinitialize();\n}\n"
  },
  {
    "path": "typescript-javascript/qrcodegen-output-demo.html",
    "content": "<!--\n  - QR Code generator output demo (HTML+JavaScript)\n  - \n  - Copyright (c) Project Nayuki. (MIT License)\n  - https://www.nayuki.io/page/qr-code-generator-library\n  - \n  - Permission is hereby granted, free of charge, to any person obtaining a copy of\n  - this software and associated documentation files (the \"Software\"), to deal in\n  - the Software without restriction, including without limitation the rights to\n  - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n  - the Software, and to permit persons to whom the Software is furnished to do so,\n  - subject to the following conditions:\n  - * The above copyright notice and this permission notice shall be included in\n  -   all copies or substantial portions of the Software.\n  - * The Software is provided \"as is\", without warranty of any kind, express or\n  -   implied, including but not limited to the warranties of merchantability,\n  -   fitness for a particular purpose and noninfringement. In no event shall the\n  -   authors or copyright holders be liable for any claim, damages or other\n  -   liability, whether in an action of contract, tort or otherwise, arising from,\n  -   out of or in connection with the Software or the use or other dealings in the\n  -   Software.\n  -->\n<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t\t<title>QR Code generator output demo (JavaScript)</title>\n\t\t<style type=\"text/css\">\n\t\t\thtml {\n\t\t\t\tfont-family: sans-serif;\n\t\t\t}\n\t\t\th1 {\n\t\t\t\ttext-align: center;\n\t\t\t}\n\t\t\t#output p {\n\t\t\t\tmargin-top: 0.5em;\n\t\t\t\tmargin-bottom: 0.5em;\n\t\t\t}\n\t\t\t#output canvas {\n\t\t\t\tdisplay: block;\n\t\t\t\tmargin-bottom: 1.5em;\n\t\t\t\tborder: 0.2em solid #D0D0D0;\n\t\t\t\tborder-radius: 0.4em;\n\t\t\t}\n\t\t\thr {\n\t\t\t\tmargin: 2em 0em;\n\t\t\t\tborder: none;\n\t\t\t\tborder-top: 0.1em solid #A0A0A0;\n\t\t\t}\n\t\t</style>\n\t</head>\n\t\n\t<body>\n\t\t<h1>QR Code generator output demo (JavaScript)</h1>\n\t\t<div id=\"output\">\n\t\t\t<p>Loading application...</p>\n\t\t\t<p>(Are the JavaScript files missing?)</p>\n\t\t\t<p>(The JavaScript code needs to be compiled from the TypeScript code.)</p>\n\t\t</div>\n\t\t<script type=\"application/javascript\" src=\"qrcodegen.js\"></script>\n\t\t<script type=\"application/javascript\" src=\"qrcodegen-output-demo.js\"></script>\n\t\t<hr>\n\t\t<p>Copyright © Project Nayuki – <a href=\"https://www.nayuki.io/page/qr-code-generator-library\">https://www.nayuki.io/page/qr-code-generator-library</a></p>\n\t</body>\n</html>\n"
  },
  {
    "path": "typescript-javascript/qrcodegen-output-demo.ts",
    "content": "/* \n * QR Code generator output demo (TypeScript)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n\"use strict\";\n\n\nnamespace app {\n\t\n\tlet outputElem = document.getElementById(\"output\") as HTMLElement;\n\t\n\t\n\t// The main application program.\n\tfunction main(): void {\n\t\twhile (outputElem.firstChild !== null)\n\t\t\toutputElem.removeChild(outputElem.firstChild);\n\t\tdoBasicDemo();\n\t\tdoVarietyDemo();\n\t\tdoSegmentDemo();\n\t\tdoMaskDemo();\n\t}\n\t\n\t\n\t// Creates a single QR Code, then appends it to the document.\n\tfunction doBasicDemo(): void {\n\t\tappendHeading(\"Basic\");\n\t\tconst text: string = \"Hello, world!\";  // User-supplied Unicode text\n\t\tconst errCorLvl: qrcodegen.QrCode.Ecc = qrcodegen.QrCode.Ecc.LOW;  // Error correction level\n\t\tconst qr: qrcodegen.QrCode = qrcodegen.QrCode.encodeText(text, errCorLvl);  // Make the QR Code symbol\n\t\tdrawCanvas(qr, 10, 4, \"#FFFFFF\", \"#000000\", appendCanvas(\"hello-world-QR\"));  // Draw it on screen\n\t}\n\t\n\t\n\t// Creates a variety of QR Codes that exercise different features of the library, and appends each one to the document.\n\tfunction doVarietyDemo(): void {\n\t\tappendHeading(\"Variety\");\n\t\tlet qr: qrcodegen.QrCode;\n\t\tconst QrCode = qrcodegen.QrCode;  // Abbreviation\n\t\t\n\t\t// Numeric mode encoding (3.33 bits per digit)\n\t\tqr = QrCode.encodeText(\"314159265358979323846264338327950288419716939937510\", QrCode.Ecc.MEDIUM);\n\t\tdrawCanvas(qr, 13, 1, \"#FFFFFF\", \"#000000\", appendCanvas(\"pi-digits-QR\"));\n\t\t\n\t\t// Alphanumeric mode encoding (5.5 bits per character)\n\t\tqr = QrCode.encodeText(\"DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/\", QrCode.Ecc.HIGH);\n\t\tdrawCanvas(qr, 10, 2, \"#FFFFFF\", \"#000000\", appendCanvas(\"alphanumeric-QR\"));\n\t\t\n\t\t// Unicode text as UTF-8\n\t\tqr = QrCode.encodeText(\"\\u3053\\u3093\\u306B\\u3061wa\\u3001\\u4E16\\u754C\\uFF01 \\u03B1\\u03B2\\u03B3\\u03B4\", QrCode.Ecc.QUARTILE);\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"unicode-QR\"));\n\t\t\n\t\t// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)\n\t\tqr = QrCode.encodeText(\n\t\t\t\"Alice was beginning to get very tired of sitting by her sister on the bank, \"\n\t\t\t+ \"and of having nothing to do: once or twice she had peeped into the book her sister was reading, \"\n\t\t\t+ \"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice \"\n\t\t\t+ \"'without pictures or conversations?' So she was considering in her own mind (as well as she could, \"\n\t\t\t+ \"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a \"\n\t\t\t+ \"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly \"\n\t\t\t+ \"a White Rabbit with pink eyes ran close by her.\", QrCode.Ecc.HIGH);\n\t\tdrawCanvas(qr, 6, 10, \"#FFFFFF\", \"#000000\", appendCanvas(\"alice-wonderland-QR\"));\n\t}\n\t\n\t\n\t// Creates QR Codes with manually specified segments for better compactness.\n\tfunction doSegmentDemo(): void {\n\t\tappendHeading(\"Segment\");\n\t\tlet qr: qrcodegen.QrCode;\n\t\tlet segs: Array<qrcodegen.QrSegment>;\n\t\tconst QrCode = qrcodegen.QrCode;  // Abbreviation\n\t\tconst QrSegment = qrcodegen.QrSegment;  // Abbreviation\n\t\t\n\t\t// Illustration \"silver\"\n\t\tconst silver0: string = \"THE SQUARE ROOT OF 2 IS 1.\";\n\t\tconst silver1: string = \"41421356237309504880168872420969807856967187537694807317667973799\";\n\t\tqr = QrCode.encodeText(silver0 + silver1, QrCode.Ecc.LOW);\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"sqrt2-monolithic-QR\"));\n\t\t\n\t\tsegs = [\n\t\t\tQrSegment.makeAlphanumeric(silver0),\n\t\t\tQrSegment.makeNumeric(silver1)];\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"sqrt2-segmented-QR\"));\n\t\t\n\t\t// Illustration \"golden\"\n\t\tconst golden0: string = \"Golden ratio \\u03C6 = 1.\";\n\t\tconst golden1: string = \"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374\";\n\t\tconst golden2: string = \"......\";\n\t\tqr = QrCode.encodeText(golden0 + golden1 + golden2, QrCode.Ecc.LOW);\n\t\tdrawCanvas(qr, 8, 5, \"#FFFFFF\", \"#000000\", appendCanvas(\"phi-monolithic-QR\"));\n\t\t\n\t\tsegs = [\n\t\t\tQrSegment.makeBytes(toUtf8ByteArray(golden0)),\n\t\t\tQrSegment.makeNumeric(golden1),\n\t\t\tQrSegment.makeAlphanumeric(golden2)];\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\tdrawCanvas(qr, 8, 5, \"#FFFFFF\", \"#000000\", appendCanvas(\"phi-segmented-QR\"));\n\t\t\n\t\t// Illustration \"Madoka\": kanji, kana, Cyrillic, full-width Latin, Greek characters\n\t\tconst madoka: string = \"\\u300C\\u9B54\\u6CD5\\u5C11\\u5973\\u307E\\u3069\\u304B\\u2606\\u30DE\\u30AE\\u30AB\\u300D\\u3063\\u3066\\u3001\\u3000\\u0418\\u0410\\u0418\\u3000\\uFF44\\uFF45\\uFF53\\uFF55\\u3000\\u03BA\\u03B1\\uFF1F\";\n\t\tqr = QrCode.encodeText(madoka, QrCode.Ecc.LOW);\n\t\tdrawCanvas(qr, 9, 4, \"#FFFFE0\", \"#303080\", appendCanvas(\"madoka-utf8-QR\"));\n\t\t\n\t\tconst kanjiCharBits: Array<number> = [  // Kanji mode encoding (13 bits per character)\n\t\t\t0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1,\n\t\t\t1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\n\t\t\t0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,\n\t\t\t0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1,\n\t\t\t0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,\n\t\t\t0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,\n\t\t\t0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1,\n\t\t\t0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1,\n\t\t\t0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,\n\t\t\t0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,\n\t\t\t0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1,\n\t\t\t0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0,\n\t\t\t0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0,\n\t\t\t0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1,\n\t\t\t0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0,\n\t\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,\n\t\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n\t\t\t0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,\n\t\t\t0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,\n\t\t\t0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,\n\t\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n\t\t\t0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,\n\t\t\t0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,\n\t\t\t0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,\n\t\t\t0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1,\n\t\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n\t\t\t0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n\t\t\t0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n\t\t\t0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n\t\t];\n\t\tsegs = [new QrSegment(QrSegment.Mode.KANJI, kanjiCharBits.length / 13, kanjiCharBits)];\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);\n\t\tdrawCanvas(qr, 9, 4, \"#E0F0FF\", \"#404040\", appendCanvas(\"madoka-kanji-QR\"));\n\t}\n\t\n\t\n\t// Creates QR Codes with the same size and contents but different mask patterns.\n\tfunction doMaskDemo(): void {\n\t\tappendHeading(\"Mask\");\n\t\tlet qr: qrcodegen.QrCode;\n\t\tlet segs: Array<qrcodegen.QrSegment>;\n\t\tconst QrCode = qrcodegen.QrCode;  // Abbreviation\n\t\t\n\t\t// Project Nayuki URL\n\t\tsegs = qrcodegen.QrSegment.makeSegments(\"https://www.nayuki.io/\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true);  // Automatic mask\n\t\tdrawCanvas(qr, 8, 6, \"#E0FFE0\", \"#206020\", appendCanvas(\"project-nayuki-automask-QR\"));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true);  // Force mask 3\n\t\tdrawCanvas(qr, 8, 6, \"#FFE0E0\", \"#602020\", appendCanvas(\"project-nayuki-mask3-QR\"));\n\t\t\n\t\t// Chinese text as UTF-8\n\t\tsegs = qrcodegen.QrSegment.makeSegments(\"\\u7DAD\\u57FA\\u767E\\u79D1\\uFF08Wikipedia\\uFF0C\\u8046\\u807Di/\\u02CCw\\u026Ak\\u1D7B\\u02C8pi\\u02D0di.\\u0259/\\uFF09\\u662F\\u4E00\"\n\t\t\t+ \"\\u500B\\u81EA\\u7531\\u5167\\u5BB9\\u3001\\u516C\\u958B\\u7DE8\\u8F2F\\u4E14\\u591A\\u8A9E\\u8A00\\u7684\\u7DB2\\u8DEF\\u767E\\u79D1\\u5168\\u66F8\\u5354\\u4F5C\\u8A08\\u756B\");\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 0, true);  // Force mask 0\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"unicode-mask0-QR\"));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 1, true);  // Force mask 1\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"unicode-mask1-QR\"));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 5, true);  // Force mask 5\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"unicode-mask5-QR\"));\n\t\tqr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 7, true);  // Force mask 7\n\t\tdrawCanvas(qr, 10, 3, \"#FFFFFF\", \"#000000\", appendCanvas(\"unicode-mask7-QR\"));\n\t}\n\t\n\t\n\tfunction appendHeading(text: string): void {\n\t\tlet h2 = outputElem.appendChild(document.createElement(\"h2\"));\n\t\th2.textContent = text;\n\t}\n\t\n\t\n\tfunction appendCanvas(caption: string): HTMLCanvasElement {\n\t\tlet p = outputElem.appendChild(document.createElement(\"p\"));\n\t\tp.textContent = caption + \":\";\n\t\tlet result = document.createElement(\"canvas\");\n\t\toutputElem.appendChild(result);\n\t\treturn result;\n\t}\n\t\n\t\n\t// Draws the given QR Code, with the given module scale and border modules, onto the given HTML\n\t// canvas element. The canvas's width and height is resized to (qr.size + border * 2) * scale.\n\t// The drawn image is purely dark and light, and fully opaque.\n\t// The scale must be a positive integer and the border must be a non-negative integer.\n\tfunction drawCanvas(qr: qrcodegen.QrCode, scale: number, border: number, lightColor: string, darkColor: string, canvas: HTMLCanvasElement): void {\n\t\tif (scale <= 0 || border < 0)\n\t\t\tthrow new RangeError(\"Value out of range\");\n\t\tconst width: number = (qr.size + border * 2) * scale;\n\t\tcanvas.width = width;\n\t\tcanvas.height = width;\n\t\tlet ctx = canvas.getContext(\"2d\") as CanvasRenderingContext2D;\n\t\tfor (let y = -border; y < qr.size + border; y++) {\n\t\t\tfor (let x = -border; x < qr.size + border; x++) {\n\t\t\t\tctx.fillStyle = qr.getModule(x, y) ? darkColor : lightColor;\n\t\t\t\tctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t\n\tfunction toUtf8ByteArray(str: string): Array<number> {\n\t\tstr = encodeURI(str);\n\t\tlet result: Array<number> = [];\n\t\tfor (let i = 0; i < str.length; i++) {\n\t\t\tif (str.charAt(i) != \"%\")\n\t\t\t\tresult.push(str.charCodeAt(i));\n\t\t\telse {\n\t\t\t\tresult.push(parseInt(str.substring(i + 1, i + 3), 16));\n\t\t\t\ti += 2;\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\n\t\n\tmain();\n\t\n}\n"
  },
  {
    "path": "typescript-javascript/qrcodegen.ts",
    "content": "/* \n * QR Code generator library (TypeScript)\n * \n * Copyright (c) Project Nayuki. (MIT License)\n * https://www.nayuki.io/page/qr-code-generator-library\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy of\n * this software and associated documentation files (the \"Software\"), to deal in\n * the Software without restriction, including without limitation the rights to\n * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n * the Software, and to permit persons to whom the Software is furnished to do so,\n * subject to the following conditions:\n * - The above copyright notice and this permission notice shall be included in\n *   all copies or substantial portions of the Software.\n * - The Software is provided \"as is\", without warranty of any kind, express or\n *   implied, including but not limited to the warranties of merchantability,\n *   fitness for a particular purpose and noninfringement. In no event shall the\n *   authors or copyright holders be liable for any claim, damages or other\n *   liability, whether in an action of contract, tort or otherwise, arising from,\n *   out of or in connection with the Software or the use or other dealings in the\n *   Software.\n */\n\n\"use strict\";\n\n\nnamespace qrcodegen {\n\t\n\ttype bit  = number;\n\ttype byte = number;\n\ttype int  = number;\n\t\n\t\n\t/*---- QR Code symbol class ----*/\n\t\n\t/* \n\t * A QR Code symbol, which is a type of two-dimension barcode.\n\t * Invented by Denso Wave and described in the ISO/IEC 18004 standard.\n\t * Instances of this class represent an immutable square grid of dark and light cells.\n\t * The class provides static factory functions to create a QR Code from text or binary data.\n\t * The class covers the QR Code Model 2 specification, supporting all versions (sizes)\n\t * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.\n\t * \n\t * Ways to create a QR Code object:\n\t * - High level: Take the payload data and call QrCode.encodeText() or QrCode.encodeBinary().\n\t * - Mid level: Custom-make the list of segments and call QrCode.encodeSegments().\n\t * - Low level: Custom-make the array of data codeword bytes (including\n\t *   segment headers and final padding, excluding error correction codewords),\n\t *   supply the appropriate version number, and call the QrCode() constructor.\n\t * (Note that all ways require supplying the desired error correction level.)\n\t */\n\texport class QrCode {\n\t\t\n\t\t/*-- Static factory functions (high level) --*/\n\t\t\n\t\t// Returns a QR Code representing the given Unicode text string at the given error correction level.\n\t\t// As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer\n\t\t// Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible\n\t\t// QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the\n\t\t// ecl argument if it can be done without increasing the version.\n\t\tpublic static encodeText(text: string, ecl: QrCode.Ecc): QrCode {\n\t\t\tconst segs: Array<QrSegment> = qrcodegen.QrSegment.makeSegments(text);\n\t\t\treturn QrCode.encodeSegments(segs, ecl);\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a QR Code representing the given binary data at the given error correction level.\n\t\t// This function always encodes using the binary segment mode, not any text mode. The maximum number of\n\t\t// bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.\n\t\t// The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.\n\t\tpublic static encodeBinary(data: Readonly<Array<byte>>, ecl: QrCode.Ecc): QrCode {\n\t\t\tconst seg: QrSegment = qrcodegen.QrSegment.makeBytes(data);\n\t\t\treturn QrCode.encodeSegments([seg], ecl);\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Static factory functions (mid level) --*/\n\t\t\n\t\t// Returns a QR Code representing the given segments with the given encoding parameters.\n\t\t// The smallest possible QR Code version within the given range is automatically\n\t\t// chosen for the output. Iff boostEcl is true, then the ECC level of the result\n\t\t// may be higher than the ecl argument if it can be done without increasing the\n\t\t// version. The mask number is either between 0 to 7 (inclusive) to force that\n\t\t// mask, or -1 to automatically choose an appropriate mask (which may be slow).\n\t\t// This function allows the user to create a custom sequence of segments that switches\n\t\t// between modes (such as alphanumeric and byte) to encode text in less space.\n\t\t// This is a mid-level API; the high-level API is encodeText() and encodeBinary().\n\t\tpublic static encodeSegments(segs: Readonly<Array<QrSegment>>, ecl: QrCode.Ecc,\n\t\t\t\tminVersion: int = 1, maxVersion: int = 40,\n\t\t\t\tmask: int = -1, boostEcl: boolean = true): QrCode {\n\t\t\t\n\t\t\tif (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION)\n\t\t\t\t\t|| mask < -1 || mask > 7)\n\t\t\t\tthrow new RangeError(\"Invalid value\");\n\t\t\t\n\t\t\t// Find the minimal version number to use\n\t\t\tlet version: int;\n\t\t\tlet dataUsedBits: int;\n\t\t\tfor (version = minVersion; ; version++) {\n\t\t\t\tconst dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8;  // Number of data bits available\n\t\t\t\tconst usedBits: number = QrSegment.getTotalBits(segs, version);\n\t\t\t\tif (usedBits <= dataCapacityBits) {\n\t\t\t\t\tdataUsedBits = usedBits;\n\t\t\t\t\tbreak;  // This version number is found to be suitable\n\t\t\t\t}\n\t\t\t\tif (version >= maxVersion)  // All versions in the range could not fit the given data\n\t\t\t\t\tthrow new RangeError(\"Data too long\");\n\t\t\t}\n\t\t\t\n\t\t\t// Increase the error correction level while the data still fits in the current version number\n\t\t\tfor (const newEcl of [QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH]) {  // From low to high\n\t\t\t\tif (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8)\n\t\t\t\t\tecl = newEcl;\n\t\t\t}\n\t\t\t\n\t\t\t// Concatenate all segments to create the data bit string\n\t\t\tlet bb: Array<bit> = []\n\t\t\tfor (const seg of segs) {\n\t\t\t\tappendBits(seg.mode.modeBits, 4, bb);\n\t\t\t\tappendBits(seg.numChars, seg.mode.numCharCountBits(version), bb);\n\t\t\t\tfor (const b of seg.getData())\n\t\t\t\t\tbb.push(b);\n\t\t\t}\n\t\t\tassert(bb.length == dataUsedBits);\n\t\t\t\n\t\t\t// Add terminator and pad up to a byte if applicable\n\t\t\tconst dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8;\n\t\t\tassert(bb.length <= dataCapacityBits);\n\t\t\tappendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);\n\t\t\tappendBits(0, (8 - bb.length % 8) % 8, bb);\n\t\t\tassert(bb.length % 8 == 0);\n\t\t\t\n\t\t\t// Pad with alternating bytes until data capacity is reached\n\t\t\tfor (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11)\n\t\t\t\tappendBits(padByte, 8, bb);\n\t\t\t\n\t\t\t// Pack bits into bytes in big endian\n\t\t\tlet dataCodewords: Array<byte> = [];\n\t\t\twhile (dataCodewords.length * 8 < bb.length)\n\t\t\t\tdataCodewords.push(0);\n\t\t\tbb.forEach((b: bit, i: int) =>\n\t\t\t\tdataCodewords[i >>> 3] |= b << (7 - (i & 7)));\n\t\t\t\n\t\t\t// Create the QR Code object\n\t\t\treturn new QrCode(version, ecl, dataCodewords, mask);\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Fields --*/\n\t\t\n\t\t// The width and height of this QR Code, measured in modules, between\n\t\t// 21 and 177 (inclusive). This is equal to version * 4 + 17.\n\t\tpublic readonly size: int;\n\t\t\n\t\t// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).\n\t\t// Even if a QR Code is created with automatic masking requested (mask = -1),\n\t\t// the resulting object still has a mask value between 0 and 7.\n\t\tpublic readonly mask: int;\n\t\t\n\t\t// The modules of this QR Code (false = light, true = dark).\n\t\t// Immutable after constructor finishes. Accessed through getModule().\n\t\tprivate readonly modules   : Array<Array<boolean>> = [];\n\t\t\n\t\t// Indicates function modules that are not subjected to masking. Discarded when constructor finishes.\n\t\tprivate readonly isFunction: Array<Array<boolean>> = [];\n\t\t\n\t\t\n\t\t/*-- Constructor (low level) and fields --*/\n\t\t\n\t\t// Creates a new QR Code with the given version number,\n\t\t// error correction level, data codeword bytes, and mask number.\n\t\t// This is a low-level API that most users should not use directly.\n\t\t// A mid-level API is the encodeSegments() function.\n\t\tpublic constructor(\n\t\t\t\t// The version number of this QR Code, which is between 1 and 40 (inclusive).\n\t\t\t\t// This determines the size of this barcode.\n\t\t\t\tpublic readonly version: int,\n\t\t\t\t\n\t\t\t\t// The error correction level used in this QR Code.\n\t\t\t\tpublic readonly errorCorrectionLevel: QrCode.Ecc,\n\t\t\t\t\n\t\t\t\tdataCodewords: Readonly<Array<byte>>,\n\t\t\t\t\n\t\t\t\tmsk: int) {\n\t\t\t\n\t\t\t// Check scalar arguments\n\t\t\tif (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)\n\t\t\t\tthrow new RangeError(\"Version value out of range\");\n\t\t\tif (msk < -1 || msk > 7)\n\t\t\t\tthrow new RangeError(\"Mask value out of range\");\n\t\t\tthis.size = version * 4 + 17;\n\t\t\t\n\t\t\t// Initialize both grids to be size*size arrays of Boolean false\n\t\t\tlet row: Array<boolean> = [];\n\t\t\tfor (let i = 0; i < this.size; i++)\n\t\t\t\trow.push(false);\n\t\t\tfor (let i = 0; i < this.size; i++) {\n\t\t\t\tthis.modules   .push(row.slice());  // Initially all light\n\t\t\t\tthis.isFunction.push(row.slice());\n\t\t\t}\n\t\t\t\n\t\t\t// Compute ECC, draw modules\n\t\t\tthis.drawFunctionPatterns();\n\t\t\tconst allCodewords: Array<byte> = this.addEccAndInterleave(dataCodewords);\n\t\t\tthis.drawCodewords(allCodewords);\n\t\t\t\n\t\t\t// Do masking\n\t\t\tif (msk == -1) {  // Automatically choose best mask\n\t\t\t\tlet minPenalty: int = 1000000000;\n\t\t\t\tfor (let i = 0; i < 8; i++) {\n\t\t\t\t\tthis.applyMask(i);\n\t\t\t\t\tthis.drawFormatBits(i);\n\t\t\t\t\tconst penalty: int = this.getPenaltyScore();\n\t\t\t\t\tif (penalty < minPenalty) {\n\t\t\t\t\t\tmsk = i;\n\t\t\t\t\t\tminPenalty = penalty;\n\t\t\t\t\t}\n\t\t\t\t\tthis.applyMask(i);  // Undoes the mask due to XOR\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(0 <= msk && msk <= 7);\n\t\t\tthis.mask = msk;\n\t\t\tthis.applyMask(msk);  // Apply the final choice of mask\n\t\t\tthis.drawFormatBits(msk);  // Overwrite old format bits\n\t\t\t\n\t\t\tthis.isFunction = [];\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Accessor methods --*/\n\t\t\n\t\t// Returns the color of the module (pixel) at the given coordinates, which is false\n\t\t// for light or true for dark. The top left corner has the coordinates (x=0, y=0).\n\t\t// If the given coordinates are out of bounds, then false (light) is returned.\n\t\tpublic getModule(x: int, y: int): boolean {\n\t\t\treturn 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Private helper methods for constructor: Drawing function modules --*/\n\t\t\n\t\t// Reads this object's version field, and draws and marks all function modules.\n\t\tprivate drawFunctionPatterns(): void {\n\t\t\t// Draw horizontal and vertical timing patterns\n\t\t\tfor (let i = 0; i < this.size; i++) {\n\t\t\t\tthis.setFunctionModule(6, i, i % 2 == 0);\n\t\t\t\tthis.setFunctionModule(i, 6, i % 2 == 0);\n\t\t\t}\n\t\t\t\n\t\t\t// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)\n\t\t\tthis.drawFinderPattern(3, 3);\n\t\t\tthis.drawFinderPattern(this.size - 4, 3);\n\t\t\tthis.drawFinderPattern(3, this.size - 4);\n\t\t\t\n\t\t\t// Draw numerous alignment patterns\n\t\t\tconst alignPatPos: Array<int> = this.getAlignmentPatternPositions();\n\t\t\tconst numAlign: int = alignPatPos.length;\n\t\t\tfor (let i = 0; i < numAlign; i++) {\n\t\t\t\tfor (let j = 0; j < numAlign; j++) {\n\t\t\t\t\t// Don't draw on the three finder corners\n\t\t\t\t\tif (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))\n\t\t\t\t\t\tthis.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// Draw configuration data\n\t\t\tthis.drawFormatBits(0);  // Dummy mask value; overwritten later in the constructor\n\t\t\tthis.drawVersion();\n\t\t}\n\t\t\n\t\t\n\t\t// Draws two copies of the format bits (with its own error correction code)\n\t\t// based on the given mask and this object's error correction level field.\n\t\tprivate drawFormatBits(mask: int): void {\n\t\t\t// Calculate error correction code and pack bits\n\t\t\tconst data: int = this.errorCorrectionLevel.formatBits << 3 | mask;  // errCorrLvl is uint2, mask is uint3\n\t\t\tlet rem: int = data;\n\t\t\tfor (let i = 0; i < 10; i++)\n\t\t\t\trem = (rem << 1) ^ ((rem >>> 9) * 0x537);\n\t\t\tconst bits = (data << 10 | rem) ^ 0x5412;  // uint15\n\t\t\tassert(bits >>> 15 == 0);\n\t\t\t\n\t\t\t// Draw first copy\n\t\t\tfor (let i = 0; i <= 5; i++)\n\t\t\t\tthis.setFunctionModule(8, i, getBit(bits, i));\n\t\t\tthis.setFunctionModule(8, 7, getBit(bits, 6));\n\t\t\tthis.setFunctionModule(8, 8, getBit(bits, 7));\n\t\t\tthis.setFunctionModule(7, 8, getBit(bits, 8));\n\t\t\tfor (let i = 9; i < 15; i++)\n\t\t\t\tthis.setFunctionModule(14 - i, 8, getBit(bits, i));\n\t\t\t\n\t\t\t// Draw second copy\n\t\t\tfor (let i = 0; i < 8; i++)\n\t\t\t\tthis.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));\n\t\t\tfor (let i = 8; i < 15; i++)\n\t\t\t\tthis.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));\n\t\t\tthis.setFunctionModule(8, this.size - 8, true);  // Always dark\n\t\t}\n\t\t\n\t\t\n\t\t// Draws two copies of the version bits (with its own error correction code),\n\t\t// based on this object's version field, iff 7 <= version <= 40.\n\t\tprivate drawVersion(): void {\n\t\t\tif (this.version < 7)\n\t\t\t\treturn;\n\t\t\t\n\t\t\t// Calculate error correction code and pack bits\n\t\t\tlet rem: int = this.version;  // version is uint6, in the range [7, 40]\n\t\t\tfor (let i = 0; i < 12; i++)\n\t\t\t\trem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);\n\t\t\tconst bits: int = this.version << 12 | rem;  // uint18\n\t\t\tassert(bits >>> 18 == 0);\n\t\t\t\n\t\t\t// Draw two copies\n\t\t\tfor (let i = 0; i < 18; i++) {\n\t\t\t\tconst color: boolean = getBit(bits, i);\n\t\t\t\tconst a: int = this.size - 11 + i % 3;\n\t\t\t\tconst b: int = Math.floor(i / 3);\n\t\t\t\tthis.setFunctionModule(a, b, color);\n\t\t\t\tthis.setFunctionModule(b, a, color);\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// Draws a 9*9 finder pattern including the border separator,\n\t\t// with the center module at (x, y). Modules can be out of bounds.\n\t\tprivate drawFinderPattern(x: int, y: int): void {\n\t\t\tfor (let dy = -4; dy <= 4; dy++) {\n\t\t\t\tfor (let dx = -4; dx <= 4; dx++) {\n\t\t\t\t\tconst dist: int = Math.max(Math.abs(dx), Math.abs(dy));  // Chebyshev/infinity norm\n\t\t\t\t\tconst xx: int = x + dx;\n\t\t\t\t\tconst yy: int = y + dy;\n\t\t\t\t\tif (0 <= xx && xx < this.size && 0 <= yy && yy < this.size)\n\t\t\t\t\t\tthis.setFunctionModule(xx, yy, dist != 2 && dist != 4);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// Draws a 5*5 alignment pattern, with the center module\n\t\t// at (x, y). All modules must be in bounds.\n\t\tprivate drawAlignmentPattern(x: int, y: int): void {\n\t\t\tfor (let dy = -2; dy <= 2; dy++) {\n\t\t\t\tfor (let dx = -2; dx <= 2; dx++)\n\t\t\t\t\tthis.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// Sets the color of a module and marks it as a function module.\n\t\t// Only used by the constructor. Coordinates must be in bounds.\n\t\tprivate setFunctionModule(x: int, y: int, isDark: boolean): void {\n\t\t\tthis.modules[y][x] = isDark;\n\t\t\tthis.isFunction[y][x] = true;\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Private helper methods for constructor: Codewords and masking --*/\n\t\t\n\t\t// Returns a new byte string representing the given data with the appropriate error correction\n\t\t// codewords appended to it, based on this object's version and error correction level.\n\t\tprivate addEccAndInterleave(data: Readonly<Array<byte>>): Array<byte> {\n\t\t\tconst ver: int = this.version;\n\t\t\tconst ecl: QrCode.Ecc = this.errorCorrectionLevel;\n\t\t\tif (data.length != QrCode.getNumDataCodewords(ver, ecl))\n\t\t\t\tthrow new RangeError(\"Invalid argument\");\n\t\t\t\n\t\t\t// Calculate parameter numbers\n\t\t\tconst numBlocks: int = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];\n\t\t\tconst blockEccLen: int = QrCode.ECC_CODEWORDS_PER_BLOCK  [ecl.ordinal][ver];\n\t\t\tconst rawCodewords: int = Math.floor(QrCode.getNumRawDataModules(ver) / 8);\n\t\t\tconst numShortBlocks: int = numBlocks - rawCodewords % numBlocks;\n\t\t\tconst shortBlockLen: int = Math.floor(rawCodewords / numBlocks);\n\t\t\t\n\t\t\t// Split data into blocks and append ECC to each block\n\t\t\tlet blocks: Array<Array<byte>> = [];\n\t\t\tconst rsDiv: Array<byte> = QrCode.reedSolomonComputeDivisor(blockEccLen);\n\t\t\tfor (let i = 0, k = 0; i < numBlocks; i++) {\n\t\t\t\tlet dat: Array<byte> = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));\n\t\t\t\tk += dat.length;\n\t\t\t\tconst ecc: Array<byte> = QrCode.reedSolomonComputeRemainder(dat, rsDiv);\n\t\t\t\tif (i < numShortBlocks)\n\t\t\t\t\tdat.push(0);\n\t\t\t\tblocks.push(dat.concat(ecc));\n\t\t\t}\n\t\t\t\n\t\t\t// Interleave (not concatenate) the bytes from every block into a single sequence\n\t\t\tlet result: Array<byte> = [];\n\t\t\tfor (let i = 0; i < blocks[0].length; i++) {\n\t\t\t\tblocks.forEach((block, j) => {\n\t\t\t\t\t// Skip the padding byte in short blocks\n\t\t\t\t\tif (i != shortBlockLen - blockEccLen || j >= numShortBlocks)\n\t\t\t\t\t\tresult.push(block[i]);\n\t\t\t\t});\n\t\t\t}\n\t\t\tassert(result.length == rawCodewords);\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire\n\t\t// data area of this QR Code. Function modules need to be marked off before this is called.\n\t\tprivate drawCodewords(data: Readonly<Array<byte>>): void {\n\t\t\tif (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8))\n\t\t\t\tthrow new RangeError(\"Invalid argument\");\n\t\t\tlet i: int = 0;  // Bit index into the data\n\t\t\t// Do the funny zigzag scan\n\t\t\tfor (let right = this.size - 1; right >= 1; right -= 2) {  // Index of right column in each column pair\n\t\t\t\tif (right == 6)\n\t\t\t\t\tright = 5;\n\t\t\t\tfor (let vert = 0; vert < this.size; vert++) {  // Vertical counter\n\t\t\t\t\tfor (let j = 0; j < 2; j++) {\n\t\t\t\t\t\tconst x: int = right - j;  // Actual x coordinate\n\t\t\t\t\t\tconst upward: boolean = ((right + 1) & 2) == 0;\n\t\t\t\t\t\tconst y: int = upward ? this.size - 1 - vert : vert;  // Actual y coordinate\n\t\t\t\t\t\tif (!this.isFunction[y][x] && i < data.length * 8) {\n\t\t\t\t\t\t\tthis.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));\n\t\t\t\t\t\t\ti++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// If this QR Code has any remainder bits (0 to 7), they were assigned as\n\t\t\t\t\t\t// 0/false/light by the constructor and are left unchanged by this method\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(i == data.length * 8);\n\t\t}\n\t\t\n\t\t\n\t\t// XORs the codeword modules in this QR Code with the given mask pattern.\n\t\t// The function modules must be marked and the codeword bits must be drawn\n\t\t// before masking. Due to the arithmetic of XOR, calling applyMask() with\n\t\t// the same mask value a second time will undo the mask. A final well-formed\n\t\t// QR Code needs exactly one (not zero, two, etc.) mask applied.\n\t\tprivate applyMask(mask: int): void {\n\t\t\tif (mask < 0 || mask > 7)\n\t\t\t\tthrow new RangeError(\"Mask value out of range\");\n\t\t\tfor (let y = 0; y < this.size; y++) {\n\t\t\t\tfor (let x = 0; x < this.size; x++) {\n\t\t\t\t\tlet invert: boolean;\n\t\t\t\t\tswitch (mask) {\n\t\t\t\t\t\tcase 0:  invert = (x + y) % 2 == 0;                                  break;\n\t\t\t\t\t\tcase 1:  invert = y % 2 == 0;                                        break;\n\t\t\t\t\t\tcase 2:  invert = x % 3 == 0;                                        break;\n\t\t\t\t\t\tcase 3:  invert = (x + y) % 3 == 0;                                  break;\n\t\t\t\t\t\tcase 4:  invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;  break;\n\t\t\t\t\t\tcase 5:  invert = x * y % 2 + x * y % 3 == 0;                        break;\n\t\t\t\t\t\tcase 6:  invert = (x * y % 2 + x * y % 3) % 2 == 0;                  break;\n\t\t\t\t\t\tcase 7:  invert = ((x + y) % 2 + x * y % 3) % 2 == 0;                break;\n\t\t\t\t\t\tdefault:  throw new Error(\"Unreachable\");\n\t\t\t\t\t}\n\t\t\t\t\tif (!this.isFunction[y][x] && invert)\n\t\t\t\t\t\tthis.modules[y][x] = !this.modules[y][x];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// Calculates and returns the penalty score based on state of this QR Code's current modules.\n\t\t// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.\n\t\tprivate getPenaltyScore(): int {\n\t\t\tlet result: int = 0;\n\t\t\t\n\t\t\t// Adjacent modules in row having same color, and finder-like patterns\n\t\t\tfor (let y = 0; y < this.size; y++) {\n\t\t\t\tlet runColor = false;\n\t\t\t\tlet runX = 0;\n\t\t\t\tlet runHistory = [0,0,0,0,0,0,0];\n\t\t\t\tfor (let x = 0; x < this.size; x++) {\n\t\t\t\t\tif (this.modules[y][x] == runColor) {\n\t\t\t\t\t\trunX++;\n\t\t\t\t\t\tif (runX == 5)\n\t\t\t\t\t\t\tresult += QrCode.PENALTY_N1;\n\t\t\t\t\t\telse if (runX > 5)\n\t\t\t\t\t\t\tresult++;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthis.finderPenaltyAddHistory(runX, runHistory);\n\t\t\t\t\t\tif (!runColor)\n\t\t\t\t\t\t\tresult += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;\n\t\t\t\t\t\trunColor = this.modules[y][x];\n\t\t\t\t\t\trunX = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tresult += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * QrCode.PENALTY_N3;\n\t\t\t}\n\t\t\t// Adjacent modules in column having same color, and finder-like patterns\n\t\t\tfor (let x = 0; x < this.size; x++) {\n\t\t\t\tlet runColor = false;\n\t\t\t\tlet runY = 0;\n\t\t\t\tlet runHistory = [0,0,0,0,0,0,0];\n\t\t\t\tfor (let y = 0; y < this.size; y++) {\n\t\t\t\t\tif (this.modules[y][x] == runColor) {\n\t\t\t\t\t\trunY++;\n\t\t\t\t\t\tif (runY == 5)\n\t\t\t\t\t\t\tresult += QrCode.PENALTY_N1;\n\t\t\t\t\t\telse if (runY > 5)\n\t\t\t\t\t\t\tresult++;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthis.finderPenaltyAddHistory(runY, runHistory);\n\t\t\t\t\t\tif (!runColor)\n\t\t\t\t\t\t\tresult += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;\n\t\t\t\t\t\trunColor = this.modules[y][x];\n\t\t\t\t\t\trunY = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tresult += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * QrCode.PENALTY_N3;\n\t\t\t}\n\t\t\t\n\t\t\t// 2*2 blocks of modules having same color\n\t\t\tfor (let y = 0; y < this.size - 1; y++) {\n\t\t\t\tfor (let x = 0; x < this.size - 1; x++) {\n\t\t\t\t\tconst color: boolean = this.modules[y][x];\n\t\t\t\t\tif (  color == this.modules[y][x + 1] &&\n\t\t\t\t\t      color == this.modules[y + 1][x] &&\n\t\t\t\t\t      color == this.modules[y + 1][x + 1])\n\t\t\t\t\t\tresult += QrCode.PENALTY_N2;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// Balance of dark and light modules\n\t\t\tlet dark: int = 0;\n\t\t\tfor (const row of this.modules)\n\t\t\t\tdark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);\n\t\t\tconst total: int = this.size * this.size;  // Note that size is odd, so dark/total != 1/2\n\t\t\t// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%\n\t\t\tconst k: int = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;\n\t\t\tassert(0 <= k && k <= 9);\n\t\t\tresult += k * QrCode.PENALTY_N4;\n\t\t\tassert(0 <= result && result <= 2568888);  // Non-tight upper bound based on default values of PENALTY_N1, ..., N4\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Private helper functions --*/\n\t\t\n\t\t// Returns an ascending list of positions of alignment patterns for this version number.\n\t\t// Each position is in the range [0,177), and are used on both the x and y axes.\n\t\t// This could be implemented as lookup table of 40 variable-length lists of integers.\n\t\tprivate getAlignmentPatternPositions(): Array<int> {\n\t\t\tif (this.version == 1)\n\t\t\t\treturn [];\n\t\t\telse {\n\t\t\t\tconst numAlign: int = Math.floor(this.version / 7) + 2;\n\t\t\t\tconst step: int = Math.floor((this.version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4)) * 2;\n\t\t\t\tlet result: Array<int> = [6];\n\t\t\t\tfor (let pos = this.size - 7; result.length < numAlign; pos -= step)\n\t\t\t\t\tresult.splice(1, 0, pos);\n\t\t\t\treturn result;\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// Returns the number of data bits that can be stored in a QR Code of the given version number, after\n\t\t// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.\n\t\t// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.\n\t\tprivate static getNumRawDataModules(ver: int): int {\n\t\t\tif (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)\n\t\t\t\tthrow new RangeError(\"Version number out of range\");\n\t\t\tlet result: int = (16 * ver + 128) * ver + 64;\n\t\t\tif (ver >= 2) {\n\t\t\t\tconst numAlign: int = Math.floor(ver / 7) + 2;\n\t\t\t\tresult -= (25 * numAlign - 10) * numAlign - 55;\n\t\t\t\tif (ver >= 7)\n\t\t\t\t\tresult -= 36;\n\t\t\t}\n\t\t\tassert(208 <= result && result <= 29648);\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any\n\t\t// QR Code of the given version number and error correction level, with remainder bits discarded.\n\t\t// This stateless pure function could be implemented as a (40*4)-cell lookup table.\n\t\tprivate static getNumDataCodewords(ver: int, ecl: QrCode.Ecc): int {\n\t\t\treturn Math.floor(QrCode.getNumRawDataModules(ver) / 8) -\n\t\t\t\tQrCode.ECC_CODEWORDS_PER_BLOCK    [ecl.ordinal][ver] *\n\t\t\t\tQrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be\n\t\t// implemented as a lookup table over all possible parameter values, instead of as an algorithm.\n\t\tprivate static reedSolomonComputeDivisor(degree: int): Array<byte> {\n\t\t\tif (degree < 1 || degree > 255)\n\t\t\t\tthrow new RangeError(\"Degree out of range\");\n\t\t\t// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.\n\t\t\t// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].\n\t\t\tlet result: Array<byte> = [];\n\t\t\tfor (let i = 0; i < degree - 1; i++)\n\t\t\t\tresult.push(0);\n\t\t\tresult.push(1);  // Start off with the monomial x^0\n\t\t\t\n\t\t\t// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),\n\t\t\t// and drop the highest monomial term which is always 1x^degree.\n\t\t\t// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).\n\t\t\tlet root = 1;\n\t\t\tfor (let i = 0; i < degree; i++) {\n\t\t\t\t// Multiply the current product by (x - r^i)\n\t\t\t\tfor (let j = 0; j < result.length; j++) {\n\t\t\t\t\tresult[j] = QrCode.reedSolomonMultiply(result[j], root);\n\t\t\t\t\tif (j + 1 < result.length)\n\t\t\t\t\t\tresult[j] ^= result[j + 1];\n\t\t\t\t}\n\t\t\t\troot = QrCode.reedSolomonMultiply(root, 0x02);\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.\n\t\tprivate static reedSolomonComputeRemainder(data: Readonly<Array<byte>>, divisor: Readonly<Array<byte>>): Array<byte> {\n\t\t\tlet result: Array<byte> = divisor.map(_ => 0);\n\t\t\tfor (const b of data) {  // Polynomial division\n\t\t\t\tconst factor: byte = b ^ (result.shift() as byte);\n\t\t\t\tresult.push(0);\n\t\t\t\tdivisor.forEach((coef, i) =>\n\t\t\t\t\tresult[i] ^= QrCode.reedSolomonMultiply(coef, factor));\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result\n\t\t// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.\n\t\tprivate static reedSolomonMultiply(x: byte, y: byte): byte {\n\t\t\tif (x >>> 8 != 0 || y >>> 8 != 0)\n\t\t\t\tthrow new RangeError(\"Byte out of range\");\n\t\t\t// Russian peasant multiplication\n\t\t\tlet z: int = 0;\n\t\t\tfor (let i = 7; i >= 0; i--) {\n\t\t\t\tz = (z << 1) ^ ((z >>> 7) * 0x11D);\n\t\t\t\tz ^= ((y >>> i) & 1) * x;\n\t\t\t}\n\t\t\tassert(z >>> 8 == 0);\n\t\t\treturn z as byte;\n\t\t}\n\t\t\n\t\t\n\t\t// Can only be called immediately after a light run is added, and\n\t\t// returns either 0, 1, or 2. A helper function for getPenaltyScore().\n\t\tprivate finderPenaltyCountPatterns(runHistory: Readonly<Array<int>>): int {\n\t\t\tconst n: int = runHistory[1];\n\t\t\tassert(n <= this.size * 3);\n\t\t\tconst core: boolean = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;\n\t\t\treturn (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)\n\t\t\t     + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);\n\t\t}\n\t\t\n\t\t\n\t\t// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().\n\t\tprivate finderPenaltyTerminateAndCount(currentRunColor: boolean, currentRunLength: int, runHistory: Array<int>): int {\n\t\t\tif (currentRunColor) {  // Terminate dark run\n\t\t\t\tthis.finderPenaltyAddHistory(currentRunLength, runHistory);\n\t\t\t\tcurrentRunLength = 0;\n\t\t\t}\n\t\t\tcurrentRunLength += this.size;  // Add light border to final run\n\t\t\tthis.finderPenaltyAddHistory(currentRunLength, runHistory);\n\t\t\treturn this.finderPenaltyCountPatterns(runHistory);\n\t\t}\n\t\t\n\t\t\n\t\t// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().\n\t\tprivate finderPenaltyAddHistory(currentRunLength: int, runHistory: Array<int>): void {\n\t\t\tif (runHistory[0] == 0)\n\t\t\t\tcurrentRunLength += this.size;  // Add light border to initial run\n\t\t\trunHistory.pop();\n\t\t\trunHistory.unshift(currentRunLength);\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Constants and tables --*/\n\t\t\n\t\t// The minimum version number supported in the QR Code Model 2 standard.\n\t\tpublic static readonly MIN_VERSION: int =  1;\n\t\t// The maximum version number supported in the QR Code Model 2 standard.\n\t\tpublic static readonly MAX_VERSION: int = 40;\n\t\t\n\t\t// For use in getPenaltyScore(), when evaluating which mask is best.\n\t\tprivate static readonly PENALTY_N1: int =  3;\n\t\tprivate static readonly PENALTY_N2: int =  3;\n\t\tprivate static readonly PENALTY_N3: int = 40;\n\t\tprivate static readonly PENALTY_N4: int = 10;\n\t\t\n\t\tprivate static readonly ECC_CODEWORDS_PER_BLOCK: Array<Array<int>> = [\n\t\t\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t\t//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\n\t\t\t[-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\n\t\t\t[-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],  // Medium\n\t\t\t[-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],  // Quartile\n\t\t\t[-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],  // High\n\t\t];\n\t\t\n\t\tprivate static readonly NUM_ERROR_CORRECTION_BLOCKS: Array<Array<int>> = [\n\t\t\t// Version: (note that index 0 is for padding, and is set to an illegal value)\n\t\t\t//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\n\t\t\t[-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],  // Low\n\t\t\t[-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],  // Medium\n\t\t\t[-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],  // Quartile\n\t\t\t[-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],  // High\n\t\t];\n\t\t\n\t}\n\t\n\t\n\t// Appends the given number of low-order bits of the given value\n\t// to the given buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.\n\tfunction appendBits(val: int, len: int, bb: Array<bit>): void {\n\t\tif (len < 0 || len > 31 || val >>> len != 0)\n\t\t\tthrow new RangeError(\"Value out of range\");\n\t\tfor (let i = len - 1; i >= 0; i--)  // Append bit by bit\n\t\t\tbb.push((val >>> i) & 1);\n\t}\n\t\n\t\n\t// Returns true iff the i'th bit of x is set to 1.\n\tfunction getBit(x: int, i: int): boolean {\n\t\treturn ((x >>> i) & 1) != 0;\n\t}\n\t\n\t\n\t// Throws an exception if the given condition is false.\n\tfunction assert(cond: boolean): void {\n\t\tif (!cond)\n\t\t\tthrow new Error(\"Assertion error\");\n\t}\n\t\n\t\n\t\n\t/*---- Data segment class ----*/\n\t\n\t/* \n\t * A segment of character/binary/control data in a QR Code symbol.\n\t * Instances of this class are immutable.\n\t * The mid-level way to create a segment is to take the payload data\n\t * and call a static factory function such as QrSegment.makeNumeric().\n\t * The low-level way to create a segment is to custom-make the bit buffer\n\t * and call the QrSegment() constructor with appropriate values.\n\t * This segment class imposes no length restrictions, but QR Codes have restrictions.\n\t * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.\n\t * Any segment longer than this is meaningless for the purpose of generating QR Codes.\n\t */\n\texport class QrSegment {\n\t\t\n\t\t/*-- Static factory functions (mid level) --*/\n\t\t\n\t\t// Returns a segment representing the given binary data encoded in\n\t\t// byte mode. All input byte arrays are acceptable. Any text string\n\t\t// can be converted to UTF-8 bytes and encoded as a byte mode segment.\n\t\tpublic static makeBytes(data: Readonly<Array<byte>>): QrSegment {\n\t\t\tlet bb: Array<bit> = []\n\t\t\tfor (const b of data)\n\t\t\t\tappendBits(b, 8, bb);\n\t\t\treturn new QrSegment(QrSegment.Mode.BYTE, data.length, bb);\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a segment representing the given string of decimal digits encoded in numeric mode.\n\t\tpublic static makeNumeric(digits: string): QrSegment {\n\t\t\tif (!QrSegment.isNumeric(digits))\n\t\t\t\tthrow new RangeError(\"String contains non-numeric characters\");\n\t\t\tlet bb: Array<bit> = []\n\t\t\tfor (let i = 0; i < digits.length; ) {  // Consume up to 3 digits per iteration\n\t\t\t\tconst n: int = Math.min(digits.length - i, 3);\n\t\t\t\tappendBits(parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);\n\t\t\t\ti += n;\n\t\t\t}\n\t\t\treturn new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb);\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a segment representing the given text string encoded in alphanumeric mode.\n\t\t// The characters allowed are: 0 to 9, A to Z (uppercase only), space,\n\t\t// dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t\tpublic static makeAlphanumeric(text: string): QrSegment {\n\t\t\tif (!QrSegment.isAlphanumeric(text))\n\t\t\t\tthrow new RangeError(\"String contains unencodable characters in alphanumeric mode\");\n\t\t\tlet bb: Array<bit> = []\n\t\t\tlet i: int;\n\t\t\tfor (i = 0; i + 2 <= text.length; i += 2) {  // Process groups of 2\n\t\t\t\tlet temp: int = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;\n\t\t\t\ttemp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));\n\t\t\t\tappendBits(temp, 11, bb);\n\t\t\t}\n\t\t\tif (i < text.length)  // 1 character remaining\n\t\t\t\tappendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);\n\t\t\treturn new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length, bb);\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a new mutable list of zero or more segments to represent the given Unicode text string.\n\t\t// The result may use various segment modes and switch modes to optimize the length of the bit stream.\n\t\tpublic static makeSegments(text: string): Array<QrSegment> {\n\t\t\t// Select the most efficient segment encoding automatically\n\t\t\tif (text == \"\")\n\t\t\t\treturn [];\n\t\t\telse if (QrSegment.isNumeric(text))\n\t\t\t\treturn [QrSegment.makeNumeric(text)];\n\t\t\telse if (QrSegment.isAlphanumeric(text))\n\t\t\t\treturn [QrSegment.makeAlphanumeric(text)];\n\t\t\telse\n\t\t\t\treturn [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))];\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a segment representing an Extended Channel Interpretation\n\t\t// (ECI) designator with the given assignment value.\n\t\tpublic static makeEci(assignVal: int): QrSegment {\n\t\t\tlet bb: Array<bit> = []\n\t\t\tif (assignVal < 0)\n\t\t\t\tthrow new RangeError(\"ECI assignment value out of range\");\n\t\t\telse if (assignVal < (1 << 7))\n\t\t\t\tappendBits(assignVal, 8, bb);\n\t\t\telse if (assignVal < (1 << 14)) {\n\t\t\t\tappendBits(0b10, 2, bb);\n\t\t\t\tappendBits(assignVal, 14, bb);\n\t\t\t} else if (assignVal < 1000000) {\n\t\t\t\tappendBits(0b110, 3, bb);\n\t\t\t\tappendBits(assignVal, 21, bb);\n\t\t\t} else\n\t\t\t\tthrow new RangeError(\"ECI assignment value out of range\");\n\t\t\treturn new QrSegment(QrSegment.Mode.ECI, 0, bb);\n\t\t}\n\t\t\n\t\t\n\t\t// Tests whether the given string can be encoded as a segment in numeric mode.\n\t\t// A string is encodable iff each character is in the range 0 to 9.\n\t\tpublic static isNumeric(text: string): boolean {\n\t\t\treturn QrSegment.NUMERIC_REGEX.test(text);\n\t\t}\n\t\t\n\t\t\n\t\t// Tests whether the given string can be encoded as a segment in alphanumeric mode.\n\t\t// A string is encodable iff each character is in the following set: 0 to 9, A to Z\n\t\t// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.\n\t\tpublic static isAlphanumeric(text: string): boolean {\n\t\t\treturn QrSegment.ALPHANUMERIC_REGEX.test(text);\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Constructor (low level) and fields --*/\n\t\t\n\t\t// Creates a new QR Code segment with the given attributes and data.\n\t\t// The character count (numChars) must agree with the mode and the bit buffer length,\n\t\t// but the constraint isn't checked. The given bit buffer is cloned and stored.\n\t\tpublic constructor(\n\t\t\t\t// The mode indicator of this segment.\n\t\t\t\tpublic readonly mode: QrSegment.Mode,\n\t\t\t\t\n\t\t\t\t// The length of this segment's unencoded data. Measured in characters for\n\t\t\t\t// numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.\n\t\t\t\t// Always zero or positive. Not the same as the data's bit length.\n\t\t\t\tpublic readonly numChars: int,\n\t\t\t\t\n\t\t\t\t// The data bits of this segment. Accessed through getData().\n\t\t\t\tprivate readonly bitData: Array<bit>) {\n\t\t\t\n\t\t\tif (numChars < 0)\n\t\t\t\tthrow new RangeError(\"Invalid argument\");\n\t\t\tthis.bitData = bitData.slice();  // Make defensive copy\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Methods --*/\n\t\t\n\t\t// Returns a new copy of the data bits of this segment.\n\t\tpublic getData(): Array<bit> {\n\t\t\treturn this.bitData.slice();  // Make defensive copy\n\t\t}\n\t\t\n\t\t\n\t\t// (Package-private) Calculates and returns the number of bits needed to encode the given segments at\n\t\t// the given version. The result is infinity if a segment has too many characters to fit its length field.\n\t\tpublic static getTotalBits(segs: Readonly<Array<QrSegment>>, version: int): number {\n\t\t\tlet result: number = 0;\n\t\t\tfor (const seg of segs) {\n\t\t\t\tconst ccbits: int = seg.mode.numCharCountBits(version);\n\t\t\t\tif (seg.numChars >= (1 << ccbits))\n\t\t\t\t\treturn Infinity;  // The segment's length doesn't fit the field's bit width\n\t\t\t\tresult += 4 + ccbits + seg.bitData.length;\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t// Returns a new array of bytes representing the given string encoded in UTF-8.\n\t\tprivate static toUtf8ByteArray(str: string): Array<byte> {\n\t\t\tstr = encodeURI(str);\n\t\t\tlet result: Array<byte> = [];\n\t\t\tfor (let i = 0; i < str.length; i++) {\n\t\t\t\tif (str.charAt(i) != \"%\")\n\t\t\t\t\tresult.push(str.charCodeAt(i));\n\t\t\t\telse {\n\t\t\t\t\tresult.push(parseInt(str.substring(i + 1, i + 3), 16));\n\t\t\t\t\ti += 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\t\t\n\t\t\n\t\t/*-- Constants --*/\n\t\t\n\t\t// Describes precisely all strings that are encodable in numeric mode.\n\t\tprivate static readonly NUMERIC_REGEX: RegExp = /^[0-9]*$/;\n\t\t\n\t\t// Describes precisely all strings that are encodable in alphanumeric mode.\n\t\tprivate static readonly ALPHANUMERIC_REGEX: RegExp = /^[A-Z0-9 $%*+.\\/:-]*$/;\n\t\t\n\t\t// The set of all legal characters in alphanumeric mode,\n\t\t// where each character value maps to the index in the string.\n\t\tprivate static readonly ALPHANUMERIC_CHARSET: string = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:\";\n\t\t\n\t}\n\t\n}\n\n\n\n/*---- Public helper enumeration ----*/\n\nnamespace qrcodegen.QrCode {\n\t\n\ttype int = number;\n\t\n\t\n\t/* \n\t * The error correction level in a QR Code symbol. Immutable.\n\t */\n\texport class Ecc {\n\t\t\n\t\t/*-- Constants --*/\n\t\t\n\t\tpublic static readonly LOW      = new Ecc(0, 1);  // The QR Code can tolerate about  7% erroneous codewords\n\t\tpublic static readonly MEDIUM   = new Ecc(1, 0);  // The QR Code can tolerate about 15% erroneous codewords\n\t\tpublic static readonly QUARTILE = new Ecc(2, 3);  // The QR Code can tolerate about 25% erroneous codewords\n\t\tpublic static readonly HIGH     = new Ecc(3, 2);  // The QR Code can tolerate about 30% erroneous codewords\n\t\t\n\t\t\n\t\t/*-- Constructor and fields --*/\n\t\t\n\t\tprivate constructor(\n\t\t\t// In the range 0 to 3 (unsigned 2-bit integer).\n\t\t\tpublic readonly ordinal: int,\n\t\t\t// (Package-private) In the range 0 to 3 (unsigned 2-bit integer).\n\t\t\tpublic readonly formatBits: int) {}\n\t\t\n\t}\n}\n\n\n\n/*---- Public helper enumeration ----*/\n\nnamespace qrcodegen.QrSegment {\n\t\n\ttype int = number;\n\t\n\t\n\t/* \n\t * Describes how a segment's data bits are interpreted. Immutable.\n\t */\n\texport class Mode {\n\t\t\n\t\t/*-- Constants --*/\n\t\t\n\t\tpublic static readonly NUMERIC      = new Mode(0x1, [10, 12, 14]);\n\t\tpublic static readonly ALPHANUMERIC = new Mode(0x2, [ 9, 11, 13]);\n\t\tpublic static readonly BYTE         = new Mode(0x4, [ 8, 16, 16]);\n\t\tpublic static readonly KANJI        = new Mode(0x8, [ 8, 10, 12]);\n\t\tpublic static readonly ECI          = new Mode(0x7, [ 0,  0,  0]);\n\t\t\n\t\t\n\t\t/*-- Constructor and fields --*/\n\t\t\n\t\tprivate constructor(\n\t\t\t// The mode indicator bits, which is a uint4 value (range 0 to 15).\n\t\t\tpublic readonly modeBits: int,\n\t\t\t// Number of character count bits for three different version ranges.\n\t\t\tprivate readonly numBitsCharCount: [int,int,int]) {}\n\t\t\n\t\t\n\t\t/*-- Method --*/\n\t\t\n\t\t// (Package-private) Returns the bit width of the character count field for a segment in\n\t\t// this mode in a QR Code at the given version number. The result is in the range [0, 16].\n\t\tpublic numCharCountBits(ver: int): int {\n\t\t\treturn this.numBitsCharCount[Math.floor((ver + 7) / 17)];\n\t\t}\n\t\t\n\t}\n}\n"
  }
]