[
  {
    "path": ".clang-format",
    "content": "---\nLanguage: Cpp\nBasedOnStyle: Mozilla\nColumnLimit: 116\nSpacesInParentheses: true\nAlwaysBreakAfterReturnType: None\nAlwaysBreakAfterDefinitionReturnType: None\nSpaceBeforeCpp11BracedList: true\nBreakBeforeBinaryOperators: All\nCpp11BracedListStyle: true\nAllowShortBlocksOnASingleLine: Always\nBreakBeforeBraces: Custom\nBraceWrapping:\n  AfterClass: true\n  AfterControlStatement: Never\n  AfterFunction: true\n  AfterStruct: true\n  AfterEnum: true\n  SplitEmptyFunction: false\n  SplitEmptyRecord: false\n  SplitEmptyNamespace: false\nBreakConstructorInitializers: BeforeColon\n...\n"
  },
  {
    "path": ".gitattributes",
    "content": "* text=true\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "name: ci\n\non: [push, pull_request]\n\njobs:\n  build:\n    runs-on: ${{ matrix.os }}\n\n    strategy:\n      matrix:\n        os: [macos-latest, ubuntu-22.04, ubuntu-24.04]\n        crypto: [auto]\n        include:\n          - crypto: nettle\n            os: ubuntu-22.04\n        \n\n    steps:\n    - uses: actions/checkout@v2\n\n    - name: \"setup linux build environment\"\n      if: ${{ startsWith(matrix.os, 'ubuntu') }}\n      run: sudo apt update && sudo apt install -y protobuf-compiler libprotobuf-dev libutempter-dev autoconf automake nettle-dev\n\n    - name: \"setup macos build environment\"\n      if: ${{ startsWith(matrix.os, 'macos') }}\n      run: brew install protobuf automake\n\n    - name: \"generate build scripts\"\n      run: ./autogen.sh\n\n    - name: ${{ format('configure (crypto {0})', matrix.crypto) }}\n      run: ./configure --enable-compile-warnings=error --enable-examples ${{ (matrix.crypto != 'auto' && format('--with-crypto-library={0}', matrix.crypto)) || '' }}\n\n    - name: \"build\"\n      run: make V=1\n\n    - name: \"test\"\n      run: make V=1 check\n"
  },
  {
    "path": ".github/workflows/cifuzz.yml",
    "content": "name: CIFuzz\non: [pull_request]\njobs:\n  Fuzzing:\n    runs-on: ubuntu-latest\n    steps:\n    - name: Build Fuzzers\n      id: build\n      uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master\n      with:\n        oss-fuzz-project-name: 'mosh'\n        dry-run: false\n        language: c++\n    - name: Run Fuzzers\n      uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master\n      with:\n        oss-fuzz-project-name: 'mosh'\n        fuzz-seconds: 300\n        dry-run: false\n        language: c++\n    - name: Upload Crash\n      uses: actions/upload-artifact@v4\n      if: failure() && steps.build.outcome == 'success'\n      with:\n        name: artifacts\n        path: ./out/artifacts\n"
  },
  {
    "path": ".github/workflows/clang-format.yml",
    "content": "name: clang-format-lint\n\non: [pull_request]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    steps:\n    - uses: actions/checkout@v2\n    - uses: DoozyX/clang-format-lint-action@v0.16.2\n      with:\n        source: './src'\n        style: file\n        clangFormatVersion: 14\n"
  },
  {
    "path": ".github/workflows/release.yml",
    "content": "name: Release\n\non:\n  push:\n    tags:\n      # This syntax is globs, not regex, so it's matching any tag that\n      # contains the prefix \"mosh-\" and the 3 version elements.\n      - \"mosh-*.*.*\"\n\njobs:\n  macports-cache:\n    runs-on: macos-12\n\n    steps:\n    - uses: actions/checkout@v2\n\n    - name: \"Install gtar wrapper\"\n      run: |\n        sudo mv /usr/local/bin/gtar /usr/local/bin/gtar.orig\n        sudo cp macosx/gtar /usr/local/bin/gtar\n        sudo chmod +x /usr/local/bin/gtar\n\n    - name: Cache macports\n      id: cache-macports\n      uses: actions/cache@v3\n      env:\n        cache-name: cache-macports\n      with:\n        path: /opt/local\n        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('macosx/port-deps.sh') }}\n        restore-keys: |\n          ${{ runner.os }}-build-${{ env.cache-name }}-\n          ${{ runner.os }}-build-\n          ${{ runner.os }}-\n\n    - name: \"setup MacPorts environment\"\n      run: |\n        curl -L \"https://github.com/macports/macports-base/releases/download/v2.7.2/MacPorts-2.7.2-12-Monterey.pkg\" --output macports.pkg\n        sudo installer -pkg macports.pkg -target /\n        ./macosx/port-deps.sh deps\n\n  release:\n    needs: macports-cache\n    runs-on: ${{ matrix.os }}\n\n    strategy:\n      matrix:\n        os: [ubuntu-22.04, macos-12]\n\n    steps:\n    - uses: actions/checkout@v2\n\n    - name: \"ensure version bumped\"\n      run: |\n        expected_tag=$(echo ${{ github.ref }} | cut -d'/' -f3)\n        have_tag=\"mosh-$(sed -n 's/AC_INIT(\\[[^]]*\\], \\[\\([^]]*\\)\\].*/\\1/p' <configure.ac)\"\n        echo \"Expected tag <$expected_tag>, got <$have_tag>\"\n        if [[ \"$expected_tag\" != \"$have_tag\" ]]; then exit 1; fi\n\n    - name: \"setup linux build environment\"\n      if: ${{ startsWith(matrix.os, 'ubuntu') }}\n      run: sudo apt update && sudo apt install -y protobuf-compiler libprotobuf-dev libutempter-dev autoconf automake nettle-dev\n\n    - name: \"Install gtar wrapper\"\n      if: ${{ startsWith(matrix.os, 'macos') }}\n      run: |\n        sudo mv /usr/local/bin/gtar /usr/local/bin/gtar.orig\n        sudo cp macosx/gtar /usr/local/bin/gtar\n        sudo chmod +x /usr/local/bin/gtar\n\n    - name: Restore macports cache\n      id: cache-macports\n      if: ${{ startsWith(matrix.os, 'macos') }}\n      uses: actions/cache@v3\n      env:\n        cache-name: cache-macports\n      with:\n        path: /opt/local\n        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('macosx/port-deps.sh') }}\n        restore-keys: |\n          ${{ runner.os }}-build-${{ env.cache-name }}-\n          ${{ runner.os }}-build-\n          ${{ runner.os }}-\n\n    - name: \"describe macos build environment\"\n      if: ${{ startsWith(matrix.os, 'macos') }}\n      run: |\n        BUILD_TAG=$(echo ${{ github.ref }} | cut -d'/' -f3)\n        mkdir macosx/build-report &&\n        pushd macosx/build-report &&\n        ../port-deps.sh describe &&\n        ../osx-xcode.sh describe &&\n        tar -cjf \"../${BUILD_TAG}-osx-build-report.tbz\" . &&\n        popd\n\n    - name: \"generate build scripts\"\n      run: PATH=/opt/local/bin:$PATH ./autogen.sh\n\n    - name: \"configure\"\n      if: ${{ startsWith(matrix.os, 'ubuntu') }}\n      run: ./configure --enable-compile-warnings=error --enable-examples\n\n    - name: \"build\"\n      if: ${{ startsWith(matrix.os, 'ubuntu') }}\n      run: make V=1\n\n    - name: \"test\"\n      if: ${{ startsWith(matrix.os, 'ubuntu') }}\n      run: make V=1 distcheck -j\n\n    - name: \"unshallow git repository for git describe\"\n      if: ${{ startsWith(matrix.os, 'macos') }}\n      run: git fetch --tags --unshallow -f\n\n    - name: \"macOS package build\"\n      if: ${{ startsWith(matrix.os, 'macos') }}\n      run: |\n        pushd macosx &&\n        env ZERO_AR_DATE=1 MACOSX_DEPLOYMENT_TARGET=11.0 PATH=/opt/local/bin:$PATH ./build.sh &&\n        popd\n\n    - name: \"Upload Release\"\n      # v1 aka v0.1.14 as of 2022-07-05; pinned to avoid potential code injection\n      uses: softprops/action-gh-release@1e07f4398721186383de40550babbdf2b84acfc5\n      with:\n        # Action always creates releases in \"draft\" mode, and requires\n        # a maintainer to publish them\n        draft: True\n        prerelease: ${{ contains(github.ref, 'rc') }}\n        generate_release_notes: True\n        files: |\n          mosh-*.tar.gz\n          macosx/*.pkg\n          macosx/*-osx-build-report.tbz\n"
  },
  {
    "path": ".gitignore",
    "content": "*.a\n*.o\n*.pb.cc\n*.pb.h\n*~\n.deps\nMakefile\nMakefile.in\n.cproject\n.project\ncompile_commands.json\naminclude_static.am\n\n# Coverage artifacts\n*.gcda\n*.gcno\n*-coverage.info\n*-coverage\n\n/INSTALL\n/aclocal.m4\n/ar-lib\n/autom4te.cache\n/compile\nsrc/include/config.h\nsrc/include/config.h.in\nsrc/include/config.h.in~\n/config.cache\n/config.log\n/config.status\n/configure\n/depcomp\n/install-sh\n/missing\n/mosh-*.tar.gz\nsrc/include/stamp-h1\n/test-driver\n/VERSION\n/VERSION.stamp\n/scripts/mosh\nsrc/include/version.h\n"
  },
  {
    "path": "AUTHORS",
    "content": "Keith Winstein <mosh-devel@mit.edu>\nAnders Kaseorg\nQuentin Smith\nRichard Tibbetts\nKeegan McAllister\nJohn Hood\n"
  },
  {
    "path": "COPYING",
    "content": "                    GNU GENERAL PUBLIC LICENSE\n                       Version 3, 29 June 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n                            Preamble\n\n  The GNU General Public License is a free, copyleft license for\nsoftware and other kinds of works.\n\n  The licenses for most software and other practical works are designed\nto take away your freedom to share and change the works.  By contrast,\nthe GNU General Public License is intended to guarantee your freedom to\nshare and change all versions of a program--to make sure it remains free\nsoftware for all its users.  We, the Free Software Foundation, use the\nGNU General Public License for most of our software; it applies also to\nany other work released this way by its authors.  You can apply it to\nyour programs, too.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthem if you wish), that you receive source code or can get it if you\nwant it, that you can change the software or use pieces of it in new\nfree programs, and that you know you can do these things.\n\n  To protect your rights, we need to prevent others from denying you\nthese rights or asking you to surrender the rights.  Therefore, you have\ncertain responsibilities if you distribute copies of the software, or if\nyou modify it: responsibilities to respect the freedom of others.\n\n  For example, if you distribute copies of such a program, whether\ngratis or for a fee, you must pass on to the recipients the same\nfreedoms that you received.  You must make sure that they, too, receive\nor can get the source code.  And you must show them these terms so they\nknow their rights.\n\n  Developers that use the GNU GPL protect your rights with two steps:\n(1) assert copyright on the software, and (2) offer you this License\ngiving you legal permission to copy, distribute and/or modify it.\n\n  For the developers' and authors' protection, the GPL clearly explains\nthat there is no warranty for this free software.  For both users' and\nauthors' sake, the GPL requires that modified versions be marked as\nchanged, so that their problems will not be attributed erroneously to\nauthors of previous versions.\n\n  Some devices are designed to deny users access to install or run\nmodified versions of the software inside them, although the manufacturer\ncan do so.  This is fundamentally incompatible with the aim of\nprotecting users' freedom to change the software.  The systematic\npattern of such abuse occurs in the area of products for individuals to\nuse, which is precisely where it is most unacceptable.  Therefore, we\nhave designed this version of the GPL to prohibit the practice for those\nproducts.  If such problems arise substantially in other domains, we\nstand ready to extend this provision to those domains in future versions\nof the GPL, as needed to protect the freedom of users.\n\n  Finally, every program is threatened constantly by software patents.\nStates should not allow patents to restrict development and use of\nsoftware on general-purpose computers, but in those that do, we wish to\navoid the special danger that patents applied to a free program could\nmake it effectively proprietary.  To prevent this, the GPL assures that\npatents cannot be used to render the program non-free.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\n                       TERMS AND CONDITIONS\n\n  0. Definitions.\n\n  \"This License\" refers to version 3 of the GNU General Public License.\n\n  \"Copyright\" also means copyright-like laws that apply to other kinds of\nworks, such as semiconductor masks.\n\n  \"The Program\" refers to any copyrightable work licensed under this\nLicense.  Each licensee is addressed as \"you\".  \"Licensees\" and\n\"recipients\" may be individuals or organizations.\n\n  To \"modify\" a work means to copy from or adapt all or part of the work\nin a fashion requiring copyright permission, other than the making of an\nexact copy.  The resulting work is called a \"modified version\" of the\nearlier work or a work \"based on\" the earlier work.\n\n  A \"covered work\" means either the unmodified Program or a work based\non the Program.\n\n  To \"propagate\" a work means to do anything with it that, without\npermission, would make you directly or secondarily liable for\ninfringement under applicable copyright law, except executing it on a\ncomputer or modifying a private copy.  Propagation includes copying,\ndistribution (with or without modification), making available to the\npublic, and in some countries other activities as well.\n\n  To \"convey\" a work means any kind of propagation that enables other\nparties to make or receive copies.  Mere interaction with a user through\na computer network, with no transfer of a copy, is not conveying.\n\n  An interactive user interface displays \"Appropriate Legal Notices\"\nto the extent that it includes a convenient and prominently visible\nfeature that (1) displays an appropriate copyright notice, and (2)\ntells the user that there is no warranty for the work (except to the\nextent that warranties are provided), that licensees may convey the\nwork under this License, and how to view a copy of this License.  If\nthe interface presents a list of user commands or options, such as a\nmenu, a prominent item in the list meets this criterion.\n\n  1. Source Code.\n\n  The \"source code\" for a work means the preferred form of the work\nfor making modifications to it.  \"Object code\" means any non-source\nform of a work.\n\n  A \"Standard Interface\" means an interface that either is an official\nstandard defined by a recognized standards body, or, in the case of\ninterfaces specified for a particular programming language, one that\nis widely used among developers working in that language.\n\n  The \"System Libraries\" of an executable work include anything, other\nthan the work as a whole, that (a) is included in the normal form of\npackaging a Major Component, but which is not part of that Major\nComponent, and (b) serves only to enable use of the work with that\nMajor Component, or to implement a Standard Interface for which an\nimplementation is available to the public in source code form.  A\n\"Major Component\", in this context, means a major essential component\n(kernel, window system, and so on) of the specific operating system\n(if any) on which the executable work runs, or a compiler used to\nproduce the work, or an object code interpreter used to run it.\n\n  The \"Corresponding Source\" for a work in object code form means all\nthe source code needed to generate, install, and (for an executable\nwork) run the object code and to modify the work, including scripts to\ncontrol those activities.  However, it does not include the work's\nSystem Libraries, or general-purpose tools or generally available free\nprograms which are used unmodified in performing those activities but\nwhich are not part of the work.  For example, Corresponding Source\nincludes interface definition files associated with source files for\nthe work, and the source code for shared libraries and dynamically\nlinked subprograms that the work is specifically designed to require,\nsuch as by intimate data communication or control flow between those\nsubprograms and other parts of the work.\n\n  The Corresponding Source need not include anything that users\ncan regenerate automatically from other parts of the Corresponding\nSource.\n\n  The Corresponding Source for a work in source code form is that\nsame work.\n\n  2. Basic Permissions.\n\n  All rights granted under this License are granted for the term of\ncopyright on the Program, and are irrevocable provided the stated\nconditions are met.  This License explicitly affirms your unlimited\npermission to run the unmodified Program.  The output from running a\ncovered work is covered by this License only if the output, given its\ncontent, constitutes a covered work.  This License acknowledges your\nrights of fair use or other equivalent, as provided by copyright law.\n\n  You may make, run and propagate covered works that you do not\nconvey, without conditions so long as your license otherwise remains\nin force.  You may convey covered works to others for the sole purpose\nof having them make modifications exclusively for you, or provide you\nwith facilities for running those works, provided that you comply with\nthe terms of this License in conveying all material for which you do\nnot control copyright.  Those thus making or running the covered works\nfor you must do so exclusively on your behalf, under your direction\nand control, on terms that prohibit them from making any copies of\nyour copyrighted material outside their relationship with you.\n\n  Conveying under any other circumstances is permitted solely under\nthe conditions stated below.  Sublicensing is not allowed; section 10\nmakes it unnecessary.\n\n  3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\n  No covered work shall be deemed part of an effective technological\nmeasure under any applicable law fulfilling obligations under article\n11 of the WIPO copyright treaty adopted on 20 December 1996, or\nsimilar laws prohibiting or restricting circumvention of such\nmeasures.\n\n  When you convey a covered work, you waive any legal power to forbid\ncircumvention of technological measures to the extent such circumvention\nis effected by exercising rights under this License with respect to\nthe covered work, and you disclaim any intention to limit operation or\nmodification of the work as a means of enforcing, against the work's\nusers, your or third parties' legal rights to forbid circumvention of\ntechnological measures.\n\n  4. Conveying Verbatim Copies.\n\n  You may convey verbatim copies of the Program's source code as you\nreceive it, in any medium, provided that you conspicuously and\nappropriately publish on each copy an appropriate copyright notice;\nkeep intact all notices stating that this License and any\nnon-permissive terms added in accord with section 7 apply to the code;\nkeep intact all notices of the absence of any warranty; and give all\nrecipients a copy of this License along with the Program.\n\n  You may charge any price or no price for each copy that you convey,\nand you may offer support or warranty protection for a fee.\n\n  5. Conveying Modified Source Versions.\n\n  You may convey a work based on the Program, or the modifications to\nproduce it from the Program, in the form of source code under the\nterms of section 4, provided that you also meet all of these conditions:\n\n    a) The work must carry prominent notices stating that you modified\n    it, and giving a relevant date.\n\n    b) The work must carry prominent notices stating that it is\n    released under this License and any conditions added under section\n    7.  This requirement modifies the requirement in section 4 to\n    \"keep intact all notices\".\n\n    c) You must license the entire work, as a whole, under this\n    License to anyone who comes into possession of a copy.  This\n    License will therefore apply, along with any applicable section 7\n    additional terms, to the whole of the work, and all its parts,\n    regardless of how they are packaged.  This License gives no\n    permission to license the work in any other way, but it does not\n    invalidate such permission if you have separately received it.\n\n    d) If the work has interactive user interfaces, each must display\n    Appropriate Legal Notices; however, if the Program has interactive\n    interfaces that do not display Appropriate Legal Notices, your\n    work need not make them do so.\n\n  A compilation of a covered work with other separate and independent\nworks, which are not by their nature extensions of the covered work,\nand which are not combined with it such as to form a larger program,\nin or on a volume of a storage or distribution medium, is called an\n\"aggregate\" if the compilation and its resulting copyright are not\nused to limit the access or legal rights of the compilation's users\nbeyond what the individual works permit.  Inclusion of a covered work\nin an aggregate does not cause this License to apply to the other\nparts of the aggregate.\n\n  6. Conveying Non-Source Forms.\n\n  You may convey a covered work in object code form under the terms\nof sections 4 and 5, provided that you also convey the\nmachine-readable Corresponding Source under the terms of this License,\nin one of these ways:\n\n    a) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by the\n    Corresponding Source fixed on a durable physical medium\n    customarily used for software interchange.\n\n    b) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by a\n    written offer, valid for at least three years and valid for as\n    long as you offer spare parts or customer support for that product\n    model, to give anyone who possesses the object code either (1) a\n    copy of the Corresponding Source for all the software in the\n    product that is covered by this License, on a durable physical\n    medium customarily used for software interchange, for a price no\n    more than your reasonable cost of physically performing this\n    conveying of source, or (2) access to copy the\n    Corresponding Source from a network server at no charge.\n\n    c) Convey individual copies of the object code with a copy of the\n    written offer to provide the Corresponding Source.  This\n    alternative is allowed only occasionally and noncommercially, and\n    only if you received the object code with such an offer, in accord\n    with subsection 6b.\n\n    d) Convey the object code by offering access from a designated\n    place (gratis or for a charge), and offer equivalent access to the\n    Corresponding Source in the same way through the same place at no\n    further charge.  You need not require recipients to copy the\n    Corresponding Source along with the object code.  If the place to\n    copy the object code is a network server, the Corresponding Source\n    may be on a different server (operated by you or a third party)\n    that supports equivalent copying facilities, provided you maintain\n    clear directions next to the object code saying where to find the\n    Corresponding Source.  Regardless of what server hosts the\n    Corresponding Source, you remain obligated to ensure that it is\n    available for as long as needed to satisfy these requirements.\n\n    e) Convey the object code using peer-to-peer transmission, provided\n    you inform other peers where the object code and Corresponding\n    Source of the work are being offered to the general public at no\n    charge under subsection 6d.\n\n  A separable portion of the object code, whose source code is excluded\nfrom the Corresponding Source as a System Library, need not be\nincluded in conveying the object code work.\n\n  A \"User Product\" is either (1) a \"consumer product\", which means any\ntangible personal property which is normally used for personal, family,\nor household purposes, or (2) anything designed or sold for incorporation\ninto a dwelling.  In determining whether a product is a consumer product,\ndoubtful cases shall be resolved in favor of coverage.  For a particular\nproduct received by a particular user, \"normally used\" refers to a\ntypical or common use of that class of product, regardless of the status\nof the particular user or of the way in which the particular user\nactually uses, or expects or is expected to use, the product.  A product\nis a consumer product regardless of whether the product has substantial\ncommercial, industrial or non-consumer uses, unless such uses represent\nthe only significant mode of use of the product.\n\n  \"Installation Information\" for a User Product means any methods,\nprocedures, authorization keys, or other information required to install\nand execute modified versions of a covered work in that User Product from\na modified version of its Corresponding Source.  The information must\nsuffice to ensure that the continued functioning of the modified object\ncode is in no case prevented or interfered with solely because\nmodification has been made.\n\n  If you convey an object code work under this section in, or with, or\nspecifically for use in, a User Product, and the conveying occurs as\npart of a transaction in which the right of possession and use of the\nUser Product is transferred to the recipient in perpetuity or for a\nfixed term (regardless of how the transaction is characterized), the\nCorresponding Source conveyed under this section must be accompanied\nby the Installation Information.  But this requirement does not apply\nif neither you nor any third party retains the ability to install\nmodified object code on the User Product (for example, the work has\nbeen installed in ROM).\n\n  The requirement to provide Installation Information does not include a\nrequirement to continue to provide support service, warranty, or updates\nfor a work that has been modified or installed by the recipient, or for\nthe User Product in which it has been modified or installed.  Access to a\nnetwork may be denied when the modification itself materially and\nadversely affects the operation of the network or violates the rules and\nprotocols for communication across the network.\n\n  Corresponding Source conveyed, and Installation Information provided,\nin accord with this section must be in a format that is publicly\ndocumented (and with an implementation available to the public in\nsource code form), and must require no special password or key for\nunpacking, reading or copying.\n\n  7. Additional Terms.\n\n  \"Additional permissions\" are terms that supplement the terms of this\nLicense by making exceptions from one or more of its conditions.\nAdditional permissions that are applicable to the entire Program shall\nbe treated as though they were included in this License, to the extent\nthat they are valid under applicable law.  If additional permissions\napply only to part of the Program, that part may be used separately\nunder those permissions, but the entire Program remains governed by\nthis License without regard to the additional permissions.\n\n  When you convey a copy of a covered work, you may at your option\nremove any additional permissions from that copy, or from any part of\nit.  (Additional permissions may be written to require their own\nremoval in certain cases when you modify the work.)  You may place\nadditional permissions on material, added by you to a covered work,\nfor which you have or can give appropriate copyright permission.\n\n  Notwithstanding any other provision of this License, for material you\nadd to a covered work, you may (if authorized by the copyright holders of\nthat material) supplement the terms of this License with terms:\n\n    a) Disclaiming warranty or limiting liability differently from the\n    terms of sections 15 and 16 of this License; or\n\n    b) Requiring preservation of specified reasonable legal notices or\n    author attributions in that material or in the Appropriate Legal\n    Notices displayed by works containing it; or\n\n    c) Prohibiting misrepresentation of the origin of that material, or\n    requiring that modified versions of such material be marked in\n    reasonable ways as different from the original version; or\n\n    d) Limiting the use for publicity purposes of names of licensors or\n    authors of the material; or\n\n    e) Declining to grant rights under trademark law for use of some\n    trade names, trademarks, or service marks; or\n\n    f) Requiring indemnification of licensors and authors of that\n    material by anyone who conveys the material (or modified versions of\n    it) with contractual assumptions of liability to the recipient, for\n    any liability that these contractual assumptions directly impose on\n    those licensors and authors.\n\n  All other non-permissive additional terms are considered \"further\nrestrictions\" within the meaning of section 10.  If the Program as you\nreceived it, or any part of it, contains a notice stating that it is\ngoverned by this License along with a term that is a further\nrestriction, you may remove that term.  If a license document contains\na further restriction but permits relicensing or conveying under this\nLicense, you may add to a covered work material governed by the terms\nof that license document, provided that the further restriction does\nnot survive such relicensing or conveying.\n\n  If you add terms to a covered work in accord with this section, you\nmust place, in the relevant source files, a statement of the\nadditional terms that apply to those files, or a notice indicating\nwhere to find the applicable terms.\n\n  Additional terms, permissive or non-permissive, may be stated in the\nform of a separately written license, or stated as exceptions;\nthe above requirements apply either way.\n\n  8. Termination.\n\n  You may not propagate or modify a covered work except as expressly\nprovided under this License.  Any attempt otherwise to propagate or\nmodify it is void, and will automatically terminate your rights under\nthis License (including any patent licenses granted under the third\nparagraph of section 11).\n\n  However, if you cease all violation of this License, then your\nlicense from a particular copyright holder is reinstated (a)\nprovisionally, unless and until the copyright holder explicitly and\nfinally terminates your license, and (b) permanently, if the copyright\nholder fails to notify you of the violation by some reasonable means\nprior to 60 days after the cessation.\n\n  Moreover, your license from a particular copyright holder is\nreinstated permanently if the copyright holder notifies you of the\nviolation by some reasonable means, this is the first time you have\nreceived notice of violation of this License (for any work) from that\ncopyright holder, and you cure the violation prior to 30 days after\nyour receipt of the notice.\n\n  Termination of your rights under this section does not terminate the\nlicenses of parties who have received copies or rights from you under\nthis License.  If your rights have been terminated and not permanently\nreinstated, you do not qualify to receive new licenses for the same\nmaterial under section 10.\n\n  9. Acceptance Not Required for Having Copies.\n\n  You are not required to accept this License in order to receive or\nrun a copy of the Program.  Ancillary propagation of a covered work\noccurring solely as a consequence of using peer-to-peer transmission\nto receive a copy likewise does not require acceptance.  However,\nnothing other than this License grants you permission to propagate or\nmodify any covered work.  These actions infringe copyright if you do\nnot accept this License.  Therefore, by modifying or propagating a\ncovered work, you indicate your acceptance of this License to do so.\n\n  10. Automatic Licensing of Downstream Recipients.\n\n  Each time you convey a covered work, the recipient automatically\nreceives a license from the original licensors, to run, modify and\npropagate that work, subject to this License.  You are not responsible\nfor enforcing compliance by third parties with this License.\n\n  An \"entity transaction\" is a transaction transferring control of an\norganization, or substantially all assets of one, or subdividing an\norganization, or merging organizations.  If propagation of a covered\nwork results from an entity transaction, each party to that\ntransaction who receives a copy of the work also receives whatever\nlicenses to the work the party's predecessor in interest had or could\ngive under the previous paragraph, plus a right to possession of the\nCorresponding Source of the work from the predecessor in interest, if\nthe predecessor has it or can get it with reasonable efforts.\n\n  You may not impose any further restrictions on the exercise of the\nrights granted or affirmed under this License.  For example, you may\nnot impose a license fee, royalty, or other charge for exercise of\nrights granted under this License, and you may not initiate litigation\n(including a cross-claim or counterclaim in a lawsuit) alleging that\nany patent claim is infringed by making, using, selling, offering for\nsale, or importing the Program or any portion of it.\n\n  11. Patents.\n\n  A \"contributor\" is a copyright holder who authorizes use under this\nLicense of the Program or a work on which the Program is based.  The\nwork thus licensed is called the contributor's \"contributor version\".\n\n  A contributor's \"essential patent claims\" are all patent claims\nowned or controlled by the contributor, whether already acquired or\nhereafter acquired, that would be infringed by some manner, permitted\nby this License, of making, using, or selling its contributor version,\nbut do not include claims that would be infringed only as a\nconsequence of further modification of the contributor version.  For\npurposes of this definition, \"control\" includes the right to grant\npatent sublicenses in a manner consistent with the requirements of\nthis License.\n\n  Each contributor grants you a non-exclusive, worldwide, royalty-free\npatent license under the contributor's essential patent claims, to\nmake, use, sell, offer for sale, import and otherwise run, modify and\npropagate the contents of its contributor version.\n\n  In the following three paragraphs, a \"patent license\" is any express\nagreement or commitment, however denominated, not to enforce a patent\n(such as an express permission to practice a patent or covenant not to\nsue for patent infringement).  To \"grant\" such a patent license to a\nparty means to make such an agreement or commitment not to enforce a\npatent against the party.\n\n  If you convey a covered work, knowingly relying on a patent license,\nand the Corresponding Source of the work is not available for anyone\nto copy, free of charge and under the terms of this License, through a\npublicly available network server or other readily accessible means,\nthen you must either (1) cause the Corresponding Source to be so\navailable, or (2) arrange to deprive yourself of the benefit of the\npatent license for this particular work, or (3) arrange, in a manner\nconsistent with the requirements of this License, to extend the patent\nlicense to downstream recipients.  \"Knowingly relying\" means you have\nactual knowledge that, but for the patent license, your conveying the\ncovered work in a country, or your recipient's use of the covered work\nin a country, would infringe one or more identifiable patents in that\ncountry that you have reason to believe are valid.\n\n  If, pursuant to or in connection with a single transaction or\narrangement, you convey, or propagate by procuring conveyance of, a\ncovered work, and grant a patent license to some of the parties\nreceiving the covered work authorizing them to use, propagate, modify\nor convey a specific copy of the covered work, then the patent license\nyou grant is automatically extended to all recipients of the covered\nwork and works based on it.\n\n  A patent license is \"discriminatory\" if it does not include within\nthe scope of its coverage, prohibits the exercise of, or is\nconditioned on the non-exercise of one or more of the rights that are\nspecifically granted under this License.  You may not convey a covered\nwork if you are a party to an arrangement with a third party that is\nin the business of distributing software, under which you make payment\nto the third party based on the extent of your activity of conveying\nthe work, and under which the third party grants, to any of the\nparties who would receive the covered work from you, a discriminatory\npatent license (a) in connection with copies of the covered work\nconveyed by you (or copies made from those copies), or (b) primarily\nfor and in connection with specific products or compilations that\ncontain the covered work, unless you entered into that arrangement,\nor that patent license was granted, prior to 28 March 2007.\n\n  Nothing in this License shall be construed as excluding or limiting\nany implied license or other defenses to infringement that may\notherwise be available to you under applicable patent law.\n\n  12. No Surrender of Others' Freedom.\n\n  If conditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot convey a\ncovered work so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you may\nnot convey it at all.  For example, if you agree to terms that obligate you\nto collect a royalty for further conveying from those to whom you convey\nthe Program, the only way you could satisfy both those terms and this\nLicense would be to refrain entirely from conveying the Program.\n\n  13. Use with the GNU Affero General Public License.\n\n  Notwithstanding any other provision of this License, you have\npermission to link or combine any covered work with a work licensed\nunder version 3 of the GNU Affero General Public License into a single\ncombined work, and to convey the resulting work.  The terms of this\nLicense will continue to apply to the part which is the covered work,\nbut the special requirements of the GNU Affero General Public License,\nsection 13, concerning interaction through a network will apply to the\ncombination as such.\n\n  14. Revised Versions of this License.\n\n  The Free Software Foundation may publish revised and/or new versions of\nthe GNU General Public License from time to time.  Such new versions will\nbe similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\n  Each version is given a distinguishing version number.  If the\nProgram specifies that a certain numbered version of the GNU General\nPublic License \"or any later version\" applies to it, you have the\noption of following the terms and conditions either of that numbered\nversion or of any later version published by the Free Software\nFoundation.  If the Program does not specify a version number of the\nGNU General Public License, you may choose any version ever published\nby the Free Software Foundation.\n\n  If the Program specifies that a proxy can decide which future\nversions of the GNU General Public License can be used, that proxy's\npublic statement of acceptance of a version permanently authorizes you\nto choose that version for the Program.\n\n  Later license versions may give you additional or different\npermissions.  However, no additional obligations are imposed on any\nauthor or copyright holder as a result of your choosing to follow a\nlater version.\n\n  15. Disclaimer of Warranty.\n\n  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\nAPPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\nHOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\nOF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\nIS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\nALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n  16. Limitation of Liability.\n\n  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\nTHE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\nGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\nUSE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\nDATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\nPARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\nEVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\nSUCH DAMAGES.\n\n  17. Interpretation of Sections 15 and 16.\n\n  If the disclaimer of warranty and limitation of liability provided\nabove cannot be given local legal effect according to their terms,\nreviewing courts shall apply local law that most closely approximates\nan absolute waiver of all civil liability in connection with the\nProgram, unless a warranty or assumption of liability accompanies a\ncopy of the Program in return for a fee.\n\n                     END OF TERMS AND CONDITIONS\n\n            How to Apply These Terms to Your New Programs\n\n  If you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\n  To do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nstate the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    <one line to give the program's name and a brief idea of what it does.>\n    Copyright (C) <year>  <name of author>\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\nAlso add information on how to contact you by electronic and paper mail.\n\n  If the program does terminal interaction, make it output a short\nnotice like this when it starts in an interactive mode:\n\n    <program>  Copyright (C) <year>  <name of author>\n    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n    This is free software, and you are welcome to redistribute it\n    under certain conditions; type `show c' for details.\n\nThe hypothetical commands `show w' and `show c' should show the appropriate\nparts of the General Public License.  Of course, your program's commands\nmight be different; for a GUI interface, you would use an \"about box\".\n\n  You should also get your employer (if you work as a programmer) or school,\nif any, to sign a \"copyright disclaimer\" for the program, if necessary.\nFor more information on this, and how to apply and follow the GNU GPL, see\n<http://www.gnu.org/licenses/>.\n\n  The GNU General Public License does not permit incorporating your program\ninto proprietary programs.  If your program is a subroutine library, you\nmay consider it more useful to permit linking proprietary applications with\nthe library.  If this is what you want to do, use the GNU Lesser General\nPublic License instead of this License.  But first, please read\n<http://www.gnu.org/philosophy/why-not-lgpl.html>.\n"
  },
  {
    "path": "COPYING.iOS",
    "content": "The Mosh developers are aware that the terms of service that apply to\napps distributed via Apple's App Store services may conflict with\nrights granted under Mosh's license, the GNU General Public License,\nversion 3. The Mosh copyright holders do not wish this conflict to\nprevent the otherwise-compliant distribution of Mosh-derived apps via\nthe App Store. Therefore, we have committed not to pursue any license\nviolation that results solely from the conflict between the GPLv3 and\nthe Apple App Store terms of service. In other words, as long as you\ncomply with the GPL in all other respects, including its requirements\nto provide users with source code and the text of the license, we will\nnot object to your distribution of Mosh through the App Store.\n"
  },
  {
    "path": "ChangeLog",
    "content": "2017-07-21 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.3.2 released.\n\n\t* Platform support:\n\t\t* Explicitly enable binding to both IPv4 and IPv6 addresses.\n\t\t  (Giel van Schijndel)\n\t\t* Restore perl 5.8.8 support for RHEL5.  (Alexander Chernyakhovsky)\n\t\t* Make tests detect UTF-8 locale with a helper executable.  (John Hood)\n\t\t* Don't print /etc/motd on IllumOS.  (John Hood)\n\t\t* Print {,/var}/run/motd.dynamic on Ubuntu.  (John Hood)\n\t\t* Fix build on Haiku. (Adrien Destugues)\n\t\t* Disable unicode-later-combining.test for tmux 2.4.\n\t\t  This fixes build failures.  (John Hood)\n\n\t* Bug fixes:\n\t\t* In tests, explicitly set 80x24 tmux window, for newer versions\n\t\t  of tmux.  (John Hood)\n\t\t* Work around JuiceSSH rendering bug.  (John Hood)\n\t\t* Do not move cursor for SCROLL UP and SCROLL DOWN--\n\t\t  fixes an issue with tmux 2.4.  (John Hood)\n\n2017-03-25 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.3.0 released.\n\n\t* New features:\n\t\t* Change website URLs from http://mosh.mit.edu to\n\t\t  https://mosh.org.  (Keith Winstein)\n\t\t* Add --no-ssh-pty option for Dropbear compatibility and\n\t\t  other issues.\n\t\t* Switch to semantic versioning, making this version 1.3.0\n\t\t  instead of 1.2.7.\n\n\t* Platform support:\n\t\t* Added nonce-incrementing test.  (Keith Winstein)\n\t\t* Add build-source-package.sh for Debian.  (Keith Winstein)\n\t\t* Fix CPPFLAGS handling possibly causing curses detection\n\t\t  failure.  (John Hood)\n\t\t* Add an Appveyor/Cygwin CI build.\n\t\t* Improve warning-flags detection for 'make distcheck'.  (John Hood)\n\t\t* Improve robustness of regression tests.  (John Hood)\n\t\t* Support OpenBSD pledge() sandboxing.  (John Hood)\n\t\t* Use backward-compatible name for AES in\n\t\t  AppleCommonCrypto, fixing builds with older OS X SDKs.  (John Hood)\n\t\t* Detect clock_gettime() and CLOCK_MONOTONIC carefully,\n\t\t  fixing OS X 10.12 + Xcode 7.3 builds.  (John Hood)\n\t\t* Support older versions of Perl, back to 5.10, fixing\n\t\t  RHEL 5 builds. (Anders Kaseorg)\n\t\t* Add a Travis OS X CI and release build.  (John Hood)\n\t\t* Add --help and --version, enabling Automake's\n\t\t 'std-options' checks.  (Anders Kaseorg)\n\t\t* Add a simple smoke test not requiring tmux, to help\n\t\t  validate builds on older platforms including RHEL 5. (Anders Kaseorg)\n\t\t* Check for presence of clock_gettime() for OS X, where\n\t\t  the symbol may not be resolved on older OS X versions.  (John\n\t\t  Hood)\n\t\t* Fix a memory alignment issue in OCB with ARM/Neon. (Carlos Cabanero)\n\t\t* Mosh now runs correctly on Bash for Windows with Windows 10\n\t\t  Insider builds 15002 and higher. (No change in Mosh)\n\t\t* Other minor platform compatibility fixes for Mosh\n\t\t  sources and tests.  (John Hood)\n\n\t* Bug fixes:\n\t\t* Work around a pty buffering issue causing failed\n\t\t  connections on FreeBSD 11, or with Dropbear.  (John Hood)\n\t\t* Restore '-p 0' option for OS-selected UDP port bindings.  (John Hood)\n\t\t* Shell hygiene fixes, including better quoting of\n\t\t  pathnames.  (Anders Kaseorg)\n\t\t* Fix typos in project docs.  (Jakub Wilk)\n\t\t* Fix excess newlines on mosh client startup/shutdown.  (John Hood)\n\t\t* Exit gracefully, closing session, on pty write or ioctl failure.  (John Hood)\n\t\t* Fix two bugs that caused mosh-server to consume\n\t\t  excessive CPU in certain circumstances.  (John Hood)\n\t\t* Fix bug that caused text copied from mosh-client to\n\t\t  paste as long lines joined by spaces.  (John Hood)\n\t\t* Documentation improvements. (chenxiaoqino, Ashish Gupta)\n\t\t* Use getuid(), not geteuid(), for correct getpw* lookups.  (John Hood)\n\n2016-08-10 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.2.6 released.\n\n\t* New features:\n\t\t* Add Travis CI builds for Linux and Mac.  (Anders Kaseorg, others)\n\t\t* Add a --local option to run without ssh.  (John Hood)\n\t\t* Mosh now returns exitstatus reflecting connection success.\n\t\t  (John Hood)\n\t\t* Add an end-to-end test suite and many tests.  (John Hood)\n\t\t* Implement timeouts and signals to help address orphaned sessions.\n\t\t  (John Hood)\n\t\t* Major rework of Mosh's display differencing/rendering\n\t\t  code with much improved performance for slow machines.  (John Hood)\n\t\t* Implement ANSI back/forward tab (CSI CBT, CSI CHT).\n\t\t  (John Hood)\n\t\t* Do not start user shell until network session starts.\n\t\t  (John Hood)\n\t\t* Add options for more flexible specification of IPv4/IPv6\n\t\t  hostname resolution.  (John Hood)\n\t\t* Improved bash completion.  (Steve Dignam, HIGUCHI Yuta)\n\t\t* Add options for different methods of resolving the remote host\n\t\t  address, allowing operation without SshProxyCommand.  (John Hood)\n\n\t* Platform support:\n\t\t* Add configurable support for Apple Common Crypto and\n\t\t  Nettle, in place of OpenSSL.  Implement base64 locally.\n\t\t  (John Hood)\n\t\t* Workaround Cygwin select() bug.  (John Hood)\n\t\t* Updates to Debian packaging.  (Anders Kaseorg, Keith Winstein)\n\t\t* Workaround a glibc-2.22 issue causing segfaults on Debian Sid.\n\t\t  (John Hood with help from many others)\n\t\t* Prefer c++ to g++, for systems like FreeBSD where g++ is not usable.\n\t\t  (John Hood)\n\t\t* Fixes for Illumos Hipster 20151003.  (John Hood)\n\t\t* Disable -Werror for protobuf code, to resolve a new gcc6 warning.\n\t\t  (John Hood)\n\t\t* Link test for -fstack-protector-all on an embedded platform.\n\t\t  (Baruch Siach)\n\t\t* Resolve issue with bswap64() on FreeBSD-CURRENT with libc++-3.8.0.\n\t\t  (John Hood)\n\t\t* Fix issue with RECVTOS error message on client on FreeBSD.\n\t\t  (John Hood)\n\n\t* Bug fixes:\n\t\t* Remove an assertion causing aborts on Unicode fallback found by\n\t\t  fuzzing with afl.  (Keith Winstein)\n\t\t* Fix a server hang with XON/XOFF on BSD systems.  (John Hood)\n\t\t* Fix a typeahead-prediction bug that caused display corruption on\n\t\t  urxvt.  (John Hood)\n\n2015-07-12 Keith Winstein <mosh-devel@mit.edu>\n\n       * Version 1.2.5 released.\n\n       * New features:\n               * Bind to a specific IP address with --bind-server. (Philipp\n\t\t Haselwarter)\n               * MOSH_ESCAPE_KEY configures escape character.  (Timo\n\t\t J. Rinne)\n               * Support non-roaming IPv6. (Anders Kaseorg)\n               * Implement XTerm mouse mode. (Barosl LEE, Andrew Chin,\n\t\t Louis Kruger)\n               * Report Git revision along with version if available.\n\t\t (John Hood)\n\n       * Platform support:\n               * Add pselect() emulation. (Jérémie Courrèges-Anglas)\n               * OpenBSD, OS X: Fix be64toh-related issues. (Jérémie\n\t\t Courrèges-Anglas)\n               * ARM Neon: fix gcc4.8 compiling problem(Pasi Sjöholm)\n               * NaCl: Conditionally rename main to mosh_main. (Richard\n\t\t Woodbury)\n               * FreeBSD: Token pasting, forkpty(), ARM fixes. (John Hood)\n               * AIX: Implement CTTY grabbing when TIOCSCTTY is missing\n                 (Anton Lundin)\n\t       * OS X: Broaden build support to cover OS X 10.5 through\n\t\t 10.10. (John Hood)\n\t       * Debian: Improve bash-completion install and\n\t\t functionality. (Suggested by Gabriel Filion, John Hood)\n\n       * Bug fixes:\n               * Automake/autoconf workarounds.  (Anders Kaseorg)\n               * mosh-server: Allow startup without PTY.  (Keith Winstein)\n               * network.cc: Properly close old fd on Socket assignment\n\t\t operator. (Thanks to Igor Bukanov)\n               * mosh-server:  Allow startup with zero-window-size PTY.\n                 (Igor Bukanov)\n               * AddrInfo: Fix error message generation when node == NULL\n                 (Anders Kaseorg)\n               * Timestamp: Prevent integer overflow on Darwin PPC 32-bit\n                 (Anders Kaseorg)\n               * scripts/mosh: Fix hang when remote closes the connection\n                 (Anders Kaseorg)\n               * Fix issues with parsing of 256-color SGR sequences.\n                 (John Hood)\n               * Numerous code hygiene, Coverity, and Clang static analyzer\n                 fixes.  (Anders Kaseorg, Geoffrey Thomas, John Hood)\n\n2013-03-27 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.2.4 released.\n\n\t* New features:\n\t        * Support port ranges with -p LOWPORT:HIGHPORT (Luke Mewburn)\n\t        * Ctrl-^ Ctrl-Z suspends mosh client (Nikolai Zeldovich)\n\t        * mm:ss display of lost-contact times (Kevin Ballard)\n\t        * Show infobar with control chars when Ctrl-^ is typed\n\t        * Put terminal in altscreen mode (Anders Kaseorg)\n\t        * Tell automake/Debian pkg about our checks (Anders Kaseorg)\n\n\t* Platform support:\n\t        * OS X: Script to build a universal package (Peter Iannucci)\n\t        * FreeBSD: Fix build problems (Jimmy Olgeni)\n\t        * AIX: port by Anton Lundin\n\t        * Solaris with system curses (Anton Lundin)\n\t        * Cygwin and others: eliminate use of IO::Pty (Anton Lundin)\n\n\t* Bug fixes:\n\t        * Fix bug (introduced in 1.2.3) where server stays around\n\t          if process quits while client is detached\n\t        * Clean up spurious entries from detached sessions warning\n\t\t* Fix freeze when connectivity is one-directional for hours\n\t          (reported by Axel Beckert)\n\t        * Don't wipe title until a new one is set (sqweek)\n\t        * Eliminate memory leaks and cppcheck warnings (Anders Kaseorg)\n\n2012-10-19 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.2.3 released.\n\n\t* Security improvements:\n\t\t* Use OpenSSL AES implementation\n\t\t* Update AES-OCB implementation (Keegan McAllister)\n\t\t* Don't let bad server dictate IP (Felix Groebert)\n\n\t* New features:\n\t\t* Client hops ports to survive challenging client-side firewall\n\t\t* Server stops sending to save client power (Daniel Drown)\n\t\t* Set DiffServ code point and ECN-capable (Dave Täht)\n\t\t* Slow down if explicit congestion notification received\n\t\t* Warn about unattached Mosh sessions on login\n\t\t* Compatible with KDE konsole (uses BEL to terminate OSC)\n\t\t* Improved heuristic about color of predicted characters\n\n\t* Bug fixes:\n\t\t* Improved performance on systems with expensive time\n\t\t* No longer choke on \"ffff::\" address for hosts with IPv6\n\t\t* More conservative MTU and datagram sizing\n\n\t* Platform support:\n\t\t* Build on Solaris and IllumOS (Timo Sirainen, Ira Cooper)\n\t\t* Build on ARM with gcc 4.7 (Alexander Chernyakhovsky)\n\n\t* Licensing changes:\n\t\t* Allow distribution on Apple App Stores\n\t\t* Allow linking with OpenSSL\n\n2012-06-12 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.2.2 released.\n\n\t* Remove warning on out-of-order/duplicated datagrams\n\n\t* Add \"experimental\" prediction mode\n\n2012-05-25 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.2.1 released.\n\n\t* Improve performance on lossy links.\n\n\t* New diagnostic message when link is dead in only one direction.\n\n\t* Use less CPU when link is down. (Keegan McAllister)\n\n\t* Use much less CPU when application or mosh-server sends\n\t  large repeat counts (resolves CVE-2012-2385, reported by\n\t  Timo Juhani Lindfors).\n\n\t* Use less memory when mosh-server is malicious.\n\n\t* Fix vttest regression re: wrapping and tabs.\n\n\t* Enable roundtrip verifier of terminal emulator correctness\n\t  when verbose.\n\n\t* Remove skalibs as a dependency. (Keegan McAllister)\n\n\t* Remove use of poll() and OS X poll workaround. (Keegan McAllister)\n\n\t* Include bash_completion file. (ejeffrey)\n\n\t* Include UFW firewall profile. (Fumihito YOSHIDA)\n\n\t* Warning on out-of-order/duplicated datagrams\n\t  (or failed nonce increment).\n\n\t* Clearer error message on invalid port number.\n\n\t* Cleanups to quit scenario when firewalled.\n\n2012-04-26 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.2 released.\n\n\t* Remove Boost as a dependency (Keegan McAllister)\n\n\t* Add support for FreeBSD, Cygwin, RHEL/CentOS 5, OS X 10.5 on PPC.\n\n\t* More verbose and helpful diagnostics. Server now has -v flag.\n\n\t* Client now has --ssh= flag to set SSH options (like port)\n\n\t* Remove skalibs as a dependency on Debian/Ubuntu (Keegan McAllister)\n\n\t* Now passes locale-related env vars over the connection\n\n\t* Fix startup script to no longer hang on some Macs (Jay Freeman)\n\n\t* Fix terminal emulation and argument parsing on FreeBSD\n\n\t* Fix alignment problems on ARM\n\n\t* Now prints message of the day\n\n\t* Use binary hardening flags where available (Keegan McAllister)\n\n\t* Responsiveness and CPU-usage improvements\n\n\t* Several terminal-emulation improvements and bug fixes\n\n2012-04-03 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.1.3 released.\n\n\t* Builds on armel, armhf, and kFreeBSD.\n\n\t* Fixes bug causing occasional missing/extra wraparound copy-and-paste.\n\n\t* Eliminates valgrind complaint over unused winsize fields.\n\n\t* Close connection after four petabytes (per OCB recommendation).\n\n2012-03-28 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.1.2 released.\n\n\t* Fixes to build on OS X 10.5 with older gcc and -lpoll (Quentin Smith)\n\n\t* Add --with-utempter and --without-utempter per gentoo request\n\t  (Michael Weber)\n\n\t* configure now requires ncurses headers (and others) to be installed\n\n\t* Consolidate locale routines to help Android port (Keegan McAllister)\n\n2012-03-27 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.1.1 released.\n\n\t* Fix spec file and build failure on Fedora.\n\n\t* Print out error message properly on fatal_assert().\n\n\t* Support for machines without posix_memalign().\n\n2012-03-22 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.1 released (stable release).\n\n\t* Allows user to specify remote command to execute.\n\n\t* Only advertises 256 colors when user's terminal has 256 colors.\n\n\t* Add chaff to datagrams to frustrate statistical analysis of length\n\n\t* Cosmetic fixes to terminal handling\n\n\t* Improved startup script (Anders Kaseorg)\n\n2012-03-16 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.0.2 released.\n\n\t* Uses xterm-256color and supports 256-color escape sequences.\n\n\t* Posterizes to 8 colors unless the user has a 256-color terminal.\n\n\t* Handles terminals without BCE.\n\n\t* Starts login shell.\n\n2012-03-14 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.0.1 released.\n\n\t* Roughly 40% less CPU usage.\n\n2012-03-12 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 1.0 released.\n\n\t* mosh now supports --version option and prints no-warranty message.\n\n2012-03-10 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.98 released.\n\n\t* Add option to select server-side UDP port.\n\n\t* Restrict default UDP port range to 60000..61000.\n\n\t* Use TERM / terminfo to decide when to send ECH sequence.\n\n\t* Now works properly inside tmux.\n\n2012-03-07 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.97 released.\n\n\t* Merged Mac OS X port (thanks to Quentin Smith and Anders Kaseorg)\n\n\t* Server will quit if no connection within first 60 seconds\n\n\t* Startup script no longer requires threaded Perl\n\n\t* Add --enable-compile-warnings=error to ./configure\n\n\t* Fix some flicker issues with adaptive prediction mode\n\n2012-02-26 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.96 released.\n\n\t* Compress all instructions with zlib\n\n2012-02-25 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.95 released.\n\n\t* Make echo acknowledgment reliable to reduce spurious mispredict detections.\n\n\t* Require two dropped heartbeats before showing blue bar of lost contact\n\n\t* Print newline before MOSH CONNECT string to make more robust if intermediate keys hit\n\n\t* Disable ControlMaster in initial SSH connection so proxy is always used\n\n\t* Make retransmissions occur at frame rate (vs. every 3 seconds) for limited time after loss\n\n2012-02-15 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.94 released.\n\n\t* Make man pages\n\n\t* Replace C++11 features (lambda and auto) with boost equivalents\n\t(Anders Kaseorg)\n\n\t* Now builds with g++ or clang (with libstdc++)\n\n2012-02-13 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.93 released.\n\n\t* Make utmp entries\n\n2012-02-13 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.92 released.\n\n\t* Allows user to select prediction mode (always/never/adaptive)\n\n\t* Fix bug in server startup on slow hosts\n\n\t* Better prediction when deleting at line ending\n\n2012-02-09 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.91 released.\n\n\t* Startup script support SSH options and aliases (Anders Kaseorg)\n\n\t* End use of Path MTU discovery and allow IP fragmentation because,\n\te.g., Cisco VPN has MTU of 1200 and does not pass ICMP too-big.\n\n\t* Better exception and error handling.\n\n\t* Restrict predictive local echo display to long-delay links\n\t(>60 ms RTT) or for temporarily after a \"glitch.\" Otherwise simply\n\tshow server echos, while maintaining prediction overlay in the\n\tbackground in case it is needed.\n\n2012-02-07 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.9b released.\n\t* Improvements to build system (Anders Kaseorg)\n\n2012-02-06 Keith Winstein <mosh-devel@mit.edu>\n\n\t* Version 0.9 released.\n"
  },
  {
    "path": "Makefile.am",
    "content": "# Work around inconsistency in AX_CODE_COVERAGE defining\n# AM_DISTCHECK_CONFIGURE_FLAGS only in some branches\nif !CODE_COVERAGE_ENABLED\nAM_DISTCHECK_CONFIGURE_FLAGS =\nendif\n\ninclude $(top_srcdir)/aminclude_static.am\n\nACLOCAL_AMFLAGS = -I m4\nSUBDIRS = scripts src man conf\nEXTRA_DIST = autogen.sh ocb-license.html README.md COPYING.iOS\nBUILT_SOURCES = VERSION.stamp\nAM_DISTCHECK_CONFIGURE_FLAGS += --enable-compile-warnings=distcheck --enable-examples --enable-syslog\n\n# AX_CODE_COVERAGE configuration\n\n# Remove everything outside of this repository\nCODE_COVERAGE_IGNORE_PATTERN = \"/usr/include/*\" \"/usr/lib/*\" \"*/src/tests/*\" \"*/src/protobufs/*\"\n\n# Ensure coverage is relative to the top of the repository\nCODE_COVERAGE_DIRECTORY = $(abs_top_builddir)\n\n# non-Automake defines\nCPPCHECK_FLAGS = --enable=all --template=gcc --force # -j8 disables unused function checking.\nCLANG_SCAN_BUILD = scan-build\nBEAR = bear\nOCLINT_JCD = oclint-json-compilation-database\nOCLINT_OPTIONS = -enable-global-analysis -max-priority-2=1000 -max-priority-3=1000 \\\n\t-rc LONG_LINE=160 \\\n\t-rc LONG_VARIABLE_NAME=40 \\\n\t-rc SHORT_VARIABLE_NAME=1 \\\n\t-rc MINIMUM_CASES_IN_SWITCH=2\n\n.PHONY:\tVERSION.stamp\n\nVERSION.stamp:\n\t@echo @PACKAGE_STRING@ > VERSION.dist\n\t@set -e; if git describe --dirty --always > VERSION.git 2>&1 && \\\n\t\t[ -z `git rev-parse --show-prefix` ]; then \\\n\t\tif ! diff -q VERSION.git VERSION.stamp > /dev/null 2>&1; then \\\n\t\t\tmv -f VERSION.git VERSION.stamp; \\\n\t\tfi; \\\n\telif ! diff -q VERSION.dist VERSION.stamp > /dev/null 2>&1; then \\\n\t\tmv -f VERSION.dist VERSION.stamp; \\\n\tfi\n\t@rm -f VERSION.dist VERSION.git\n\nclean-local:\n\t@rm -rf VERSION.stamp cov-int mosh-coverity.txz compile_commands.json\n\n# Linters and static checkers, for development only.  Not included in\n# build dependencies, and outside of Automake processing.\ncppcheck:\n\tcppcheck $(CPPCHECK_FLAGS) -include src/include/config.h -I src/include \\\n\t\t-I src/crypto -I src/frontend -I src/network -I src/protobufs \\\n\t\t-I src/statesync -I src/terminal -I src/util \\\n\t\t-I /usr/include -I /usr/include/google/protobuf -I/usr/include/openssl \\\n\t\tsrc\n\n# Coverity.\ncov-build:\n\t$(MAKE) clean\n\trm -rf cov-int\n\tcov-build --dir cov-int $(MAKE) check TESTS=\n\ttar -caf mosh-coverity.txz cov-int\n\n# These two rules are for Bear + OCLint.\n# Don't *run* the tests, prediction-unicode.test generates arguments\n# with illegal UTF-8 that make Bear unhappy.\ncompile_commands.json:\n\t$(MAKE) clean\n\tbear $(MAKE) check TESTS=\noclint: compile_commands.json\n\t$(OCLINT_JCD) -e src/protobufs -- $(OCLINT_OPTIONS)\n\n# Clang's scan-build static checker.\nscan-build:\n\t$(MAKE) clean\n\t$(CLANG_SCAN_BUILD) $(MAKE) check TESTS=\n"
  },
  {
    "path": "NEWS",
    "content": "2012-04-26\n----------\n\n* Version 1.2 released\n\n2012-03-22\n----------\n\n* Version 1.1 released\n\n2012-03-12\n----------\n\n* Version 1.0 released\n\n2012-03-07 Release candidate (0.97):\n------------------------------------\n\n* Now builds on Mac OS X as well as GNU/Linux\n\n2012-02-06 Initial release (0.9):\n---------------------------------\n\n* Supports intermittent connectivity\n* Supports client and server IP roaming\n* Local echo supports typing and the left and right arrow keys\n"
  },
  {
    "path": "README.md",
    "content": "[![ci](https://github.com/mobile-shell/mosh/actions/workflows/ci.yml/badge.svg)](https://github.com/mobile-shell/mosh/actions/workflows/ci.yml)\n\nMosh: the mobile shell\n======================\n\nMosh is a remote terminal application that supports intermittent\nconnectivity, allows roaming, and provides speculative local echo\nand line editing of user keystrokes.\n\nIt aims to support the typical interactive uses of SSH, plus:\n\n   * Mosh keeps the session alive if the client goes to sleep and\n     wakes up later, or temporarily loses its Internet connection.\n\n   * Mosh allows the client and server to \"roam\" and change IP\n     addresses, while keeping the connection alive. Unlike SSH, Mosh\n     can be used while switching between Wi-Fi networks or from Wi-Fi\n     to cellular data to wired Ethernet.\n\n   * The Mosh client runs a predictive model of the server's behavior\n     in the background and tries to guess intelligently how each\n     keystroke will affect the screen state. When it is confident in\n     its predictions, it will show them to the user while waiting for\n     confirmation from the server. Most typing and uses of the left-\n     and right-arrow keys can be echoed immediately.\n\n     As a result, Mosh is usable on high-latency links, e.g. on a\n     cellular data connection or spotty Wi-Fi. In distinction from\n     previous attempts at local echo modes in other protocols, Mosh\n     works properly with full-screen applications such as emacs, vi,\n     alpine, and irssi, and automatically recovers from occasional\n     prediction errors within an RTT. On high-latency links, Mosh\n     underlines its predictions while they are outstanding and removes\n     the underline when they are confirmed by the server.\n\nMosh does not support X forwarding or the non-interactive uses of SSH,\nincluding port forwarding.\n\nOther features\n--------------\n\n   * Mosh adjusts its frame rate so as not to fill up network queues\n     on slow links, so \"Control-C\" always works within an RTT to halt\n     a runaway process.\n\n   * Mosh warns the user when it has not heard from the server\n     in a while.\n\n   * Mosh supports lossy links that lose a significant fraction\n     of their packets.\n\n   * Mosh handles some Unicode edge cases better than SSH and existing\n     terminal emulators by themselves, but requires a UTF-8\n     environment to run.\n\n   * Mosh leverages SSH to set up the connection and authenticate\n     users. Mosh does not contain any privileged (root) code.\n\nGetting Mosh\n------------\n\n  [The Mosh web site](https://mosh.org/#getting) has information about\n  packages for many operating systems, as well as instructions for building\n  from source.\n\n  Note that `mosh-client` receives an AES session key as an environment\n  variable.  If you are porting Mosh to a new operating system, please make\n  sure that a running process's environment variables are not readable by other\n  users.  We have confirmed that this is the case on GNU/Linux, OS X, and\n  FreeBSD.\n\nUsage\n-----\n\n  The `mosh-client` binary must exist on the user's machine, and the\n  `mosh-server` binary on the remote host.\n\n  The user runs:\n\n    $ mosh [user@]host\n\n  If the `mosh-client` or `mosh-server` binaries live outside the user's\n  `$PATH`, `mosh` accepts the arguments `--client=PATH` and `--server=PATH` to\n  select alternate locations. More options are documented in the mosh(1) manual\n  page.\n\n  There are [more examples](https://mosh.org/#usage) and a\n  [FAQ](https://mosh.org/#faq) on the Mosh web site.\n\nHow it works\n------------\n\n  The `mosh` program will SSH to `user@host` to establish the connection.\n  SSH may prompt the user for a password or use public-key\n  authentication to log in.\n\n  From this point, `mosh` runs the `mosh-server` process (as the user)\n  on the server machine. The server process listens on a high UDP port\n  and sends its port number and an AES-128 secret key back to the\n  client over SSH. The SSH connection is then shut down and the\n  terminal session begins over UDP.\n\n  If the client changes IP addresses, the server will begin sending\n  to the client on the new IP address within a few seconds.\n\n  To function, Mosh requires UDP datagrams to be passed between client\n  and server. By default, `mosh` uses a port number between 60000 and\n  61000, but the user can select a particular port with the -p option.\n  Please note that the -p option has no effect on the port used by SSH.\n\nAdvice to distributors\n----------------------\n\nA note on compiler flags: Mosh is security-sensitive code. When making\nautomated builds for a binary package, we recommend passing the option\n`--enable-compile-warnings=error` to `./configure`. On GNU/Linux with\n`g++` or `clang++`, the package should compile cleanly with\n`-Werror`. Please report a bug if it doesn't.\n\nWhere available, Mosh builds with a variety of binary hardening flags\nsuch as `-fstack-protector-all`, `-D_FORTIFY_SOURCE=2`, etc.  These\nprovide proactive security against the possibility of a memory\ncorruption bug in Mosh or one of the libraries it uses.  For a full\nlist of flags, search for `HARDEN` in `configure.ac`.  The `configure`\nscript detects which flags are supported by your compiler, and enables\nthem automatically.  To disable this detection, pass\n`--disable-hardening` to `./configure`.  Please report a bug if you\nhave trouble with the default settings; we would like as many users as\npossible to be running a configuration as secure as possible.\n\nMosh ships with a default optimization setting of `-O2`. Some\ndistributors have asked about changing this to `-Os` (which causes a\ncompiler to prefer space optimizations to time optimizations). We have\nbenchmarked with the included `src/examples/benchmark` program to test\nthis. The results are that `-O2` is 40% faster than `-Os` with g++ 4.6\non GNU/Linux, and 16% faster than `-Os` with clang++ 3.1 on Mac OS\nX. In both cases, `-Os` did produce a smaller binary (by up to 40%,\nsaving almost 200 kilobytes on disk). While Mosh is not especially CPU\nintensive and mostly sits idle when the user is not typing, we think\nthe results suggest that `-O2` (the default) is preferable.\n\nOur Debian and Fedora packaging presents Mosh as a single package.\nMosh has a Perl dependency that is only required for client use.  For\nsome platforms, it may make sense to have separate mosh-server and\nmosh-client packages to allow mosh-server usage without Perl.\n\nNotes for developers\n--------------------\n\nTo start contributing to Mosh, install the following dependencies:\n\nDebian, Windows Subsystem for Linux:\n\n```\n$ sudo apt install -y build-essential protobuf-compiler \\\n    libprotobuf-dev pkg-config libutempter-dev zlib1g-dev libncurses5-dev \\\n    libssl-dev bash-completion tmux less\n```\n\nFedora, RHEL:\n\n```\n$ sudo dnf group install development-tools\n$ sudo dnf install automake protobuf-compiler protobuf-devel libutempter-devel \\\n    zlib-ng-compat-devel ncurses-devel openssl-devel bash-completion tmux less \\\n    perl-diagnostics\n```\n\nMacOS:\n\n```\n$ brew install protobuf automake\n```\n\nOnce you have forked the repository, run the following to build and test Mosh:\n\n```\n$ ./autogen.sh\n$ ./configure\n$ make\n$ make check\n```\n\nMosh supports producing code coverage reports by tests, but this feature is\ndisabled by default. To enable it, make sure `lcov` is installed on your\nsystem. Then, configure and run tests:\n\n```\n$ ./configure --enable-code-coverage\n$ make check-code-coverage\n```\n\nThis will run all tests and produce a coverage report in HTML form that can be\nopened with your favorite browser. Ideally, newly added code should strive for\n90% (or better) incremental test coverage.\n\nMore info\n---------\n\n  * Mosh Web site:\n\n    <https://mosh.org>\n\n  * `mosh-devel@mit.edu` mailing list:\n\n    <https://mailman.mit.edu/mailman/listinfo/mosh-devel>\n\n  * `mosh-users@mit.edu` mailing list:\n\n    <https://mailman.mit.edu/mailman/listinfo/mosh-users>\n\n  * `#mosh` channel on [Libera Chat](https://libera.chat/)\n\n    https://web.libera.chat/#mosh\n"
  },
  {
    "path": "THANKS",
    "content": "* Hari Balakrishnan, who advised this work and came up with the name.\n\n* Paul Williams (www.vt100.net), whose reverse-engineered vt500 state\n  diagram is the basis for the Mosh parser.\n\n* The anonymous users who contributed session logs for tuning and\n  measuring Mosh's predictive echo.\n\n* Nickolai Zeldovich for helpful comments on the Mosh research paper.\n\n* Richard Stallman for helpful discussion about the capabilities of\n  the SUPDUP Local Editing Protocol.\n\n* Nelson Elhage\n\n* Christine Spang\n\n* Stefie Tellex\n\n* Joseph Sokol-Margolis\n\n* Waseem Daher\n\n* Bill McCloskey\n\n* Austin Roach\n\n* Greg Hudson\n\n* Karl Ramm\n\n* Alexander Chernyakhovsky\n\n* Peter Iannucci\n\n* Evan Broder\n\n* Neha Narula\n\n* Katrina LaCurts\n\n* Ramesh Chandra\n\n* Peter Jeremy\n\n* Ed Schouten\n\n* Ryan Steinmetz\n\n* Jay Freeman\n\n* Dave Täht\n\n* Larry Doolittle\n\n* Daniel Drown\n\n* Timo Juhani Lindfors\n\n* Timo Sirainen\n\n* Ira Cooper\n\n* Felix Gröbert\n\n* Luke Mewburn\n\n* Anton Lundin\n\n* Philipp Haselwarter\n\n* Timo J. Rinne\n\n* Barosl Lee\n\n* Andrew Chin\n\n* Louis Kruger\n\n* Jérémie Courrèges-Anglas\n\n* Pasi Sjöholm\n\n* Richard Woodbury\n\n* Igor Bukanov\n\n* Geoffrey Thomas\n\n* Steve Dignam\n\n* HIGUCHI Yuta\n\n* Baruch Siach\n\n* Adrien Destugues\n\n* Giel van Schijndel\n"
  },
  {
    "path": "autogen.sh",
    "content": "#!/bin/sh\n\nexec autoreconf -fi\n"
  },
  {
    "path": "build-package.sh",
    "content": "#!/bin/sh\n\ngbp buildpackage --git-upstream-branch=master --git-upstream-tree=branch\n"
  },
  {
    "path": "build-source-package.sh",
    "content": "#!/bin/sh\n\ngbp buildpackage --git-upstream-branch=master --git-upstream-tree=branch -S\n"
  },
  {
    "path": "conf/Makefile.am",
    "content": "completionsdir = @completions@\ndist_completions_DATA =\nnobase_dist_sysconf_DATA =\n\nif INSTALL_UFW\n  nobase_dist_sysconf_DATA += ufw/applications.d/mosh\nendif\n\nif INSTALL_COMPLETION\n  dist_completions_DATA += bash-completion/completions/mosh\nendif\n"
  },
  {
    "path": "conf/bash-completion/completions/mosh",
    "content": "# Bash completions for Mosh, the mobile shell.         -*- shell-script -*-\n\n__mosh_init_completion()\n{\n    if declare -F _init_completions >/dev/null 2>&1; then\n        _init_completion\n    else\n        COMPREPLY=()\n        _get_comp_words_by_ref cur prev words cword\n    fi\n}\n\n_mosh () {\n    local cur\n\n    __mosh_init_completion || return\n\n    local simple_flags=\"-a -b -4 -6 -p\"\n    local flags=\"--client= --server= --predict= --family= --port= \n    --bind-server= --ssh= --no-init --help --version\"\n\n    if [[ \"$cur\" == --* && \"$COMP_CWORD\" == 1 ]]; then\n        COMPREPLY=($(compgen -W \"$flags\" -- \"$cur\"))\n    elif [[ \"$cur\" == -* && \"$COMP_CWORD\" == 1 ]]; then\n        COMPREPLY=($(compgen -W \"$simple_flags\" -- \"$cur\"))\n    else\n        _known_hosts_real -a \"$cur\"\n    fi\n}\n\ncomplete -o nospace -F _mosh mosh\n"
  },
  {
    "path": "conf/ufw/applications.d/mosh",
    "content": "[mosh]\ntitle=Mosh (mobile shell)\ndescription=Mobile shell that supports roaming and intelligent local echo\nports=60000:61000/udp\n"
  },
  {
    "path": "configure.ac",
    "content": "#                                               -*- Autoconf -*-\n# Process this file with autoconf to produce a configure script.\n\nAC_PREREQ([2.61])\nAC_INIT([mosh], [1.4.0], [mosh-devel@mit.edu])\nAM_INIT_AUTOMAKE([foreign std-options -Wall -Werror -Wno-portability])\nm4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])\nAC_CONFIG_SRCDIR([src/frontend/mosh-client.cc])\nAC_CONFIG_MACRO_DIR([m4])\nAC_CONFIG_HEADERS([src/include/config.h])\nAC_LANG([C++])\nAX_CODE_COVERAGE\n\n# Checks for programs.\nAC_PROG_CC([cc gcc clang])\nAC_PROG_CXX([c++ g++ clang++])\nAC_PROG_RANLIB\nAC_PATH_PROG([PROTOC], [protoc], [])\nAS_IF([test x\"$PROTOC\" = x],\n  [AC_MSG_ERROR([cannot find protoc, the Protocol Buffers compiler])])\n\n# automake 1.12 seems to require this, but automake 1.11 doesn't recognize it\nm4_ifdef([AM_PROG_AR], [AM_PROG_AR])\n\n# Protobuf transitively requires at least C++14, get ahead of the\n# curve and require at least C++17.\nAX_CXX_COMPILE_STDCXX([17])\n\nWARNING_CXXFLAGS=\"\"\nPICKY_CXXFLAGS=\"\"\nDISTCHECK_CXXFLAGS=\"\"\nAC_ARG_ENABLE([compile-warnings],\n  [AS_HELP_STRING([--enable-compile-warnings@<:@=no/yes/maximum/error/distcheck@:>@],\n     [Turn on compiler warnings])],\n  [case \"$enableval\" in\n     no)\n       ;;\n     '' | yes)\n       WARNING_CXXFLAGS=\"-Wall\"\n       ;;\n     maximum)\n       WARNING_CXXFLAGS=\"-Wall\"\n       PICKY_CXXFLAGS=\"-Wextra -Wno-long-long -Weffc++ -Wmissing-declarations\"\n       ;;\n     error)\n       # remove -Wno-c++17-extensions once protocolbuffers/protobuf#9181 is\n       # resolved\n       # remove -Wno-unused-parameter once\n       # protocolbuffers/protobuf#10357 is resolved       \n       WARNING_CXXFLAGS=\"-Wall -Werror -Wno-c++17-extensions\"\n       PICKY_CXXFLAGS=\"-Wextra -Wno-long-long -Weffc++ -Wmissing-declarations -Wno-unused-parameter\"\n       ;;\n     distcheck)\n       WARNING_CXXFLAGS=\"-Wall -Werror\"\n       PICKY_CXXFLAGS=\"-Wextra -Wno-long-long -Weffc++ -Wmissing-declarations\"\n       AX_CHECK_COMPILE_FLAG([-Wno-error=unused-parameter],\n         [PICKY_CXXFLAGS=\"$PICKY_CXXFLAGS -Wno-error=unused-parameter\"], [], [-Werror])\n       AX_CHECK_COMPILE_FLAG([-Wno-error=c++11-extensions],\n         [PICKY_CXXFLAGS=\"$PICKY_CXXFLAGS -Wno-error=c++11-extensions\"], [], [-Werror])\n       AX_CHECK_COMPILE_FLAG([-Wno-error=deprecated-declarations],\n         [PICKY_CXXFLAGS=\"$PICKY_CXXFLAGS -Wno-error=deprecated-declarations\"], [], [-Werror])\n       AX_CHECK_COMPILE_FLAG([-Wno-error=nested-anon-types],\n         [PICKY_CXXFLAGS=\"$PICKY_CXXFLAGS -Wno-error=nested-anon-types\"], [], [-Werror])\n       ;;\n     *)\n       AC_MSG_ERROR([Unknown argument '$enableval' to --enable-compile-warnings])\n       ;;\n   esac],\n  [WARNING_CXXFLAGS=\"-Wall\"])\nAC_SUBST([WARNING_CXXFLAGS])\nAC_SUBST([PICKY_CXXFLAGS])\n\n# Check for fuzzing support before the flag wrapper, because if\n# requested and missing the functionality is entirely nonexistent.\nMISC_CXXFLAGS=\"\"\nAC_ARG_ENABLE([fuzzing],\n  [AS_HELP_STRING([--enable-fuzzing],\n    [Enable compiler and linker options to enable fuzz testing @<:@no/yes/libfuzzer@:>@])],\n  [fuzzing=\"$enableval\"\n   case \"$enableval\" in\n    no)\n      ;;\n    '' | yes | libfuzzer)\n      AX_CHECK_COMPILE_FLAG([-fsanitize=fuzzer],\n        [FUZZING_CFLAGS=\"$FUZZING_CFLAGS -fsanitize=fuzzer\"], [\n        AC_MSG_ERROR([Fuzzing requested, but compiler support not present])], [-Werror])\n      ;;\n    *)\n     AC_MSG_ERROR([\"Unknown argument '$enableval' to --enable-fuzzing])\n     ;;\n  esac],\n  [fuzzing=\"no\"])\nAC_SUBST([FUZZING_CFLAGS])\nAC_SUBST([MISC_CXXFLAGS])\nAM_CONDITIONAL([ENABLE_FUZZING], [test x\"$fuzzing\" != xno])\n\nAC_ARG_ENABLE([asan],\n  [AS_HELP_STRING([--enable-asan],\n    [Enable compiler and linker options to enable AddressSanitizer @<:@no@:>@])],\n  [asan=\"$enableval\"],\n  [asan=\"no\"])\n\nAS_IF([test x\"$asan\" != x\"no\"], [\n  AX_CHECK_COMPILE_FLAG([-fsanitize=address,leak],\n  [MISC_CXXFLAGS=\"$MISC_CXXFLAGS -fsanitize=address,leak\"], [\n  AC_MSG_ERROR([ASAN requested, but compiler support not present])], [-Werror])\n])\nAC_SUBST([MISC_CXXFLAGS])\n\n# We want to check for compiler flag support, but there is no way to make\n# clang's \"argument unused\" warning fatal.  So we invoke the compiler through a\n# wrapper script that greps for this message.\n\nsaved_CXX=\"$CXX\"\nsaved_LD=\"$LD\"\nflag_wrap=\"$srcdir/scripts/wrap-compiler-for-flag-check\"\nCXX=\"$flag_wrap $CXX\"\nLD=\"$flag_wrap $LD\"\n\nAC_DEFUN([check_cxx_flag],\n  [AX_CHECK_COMPILE_FLAG([$1], [$2], [$3], [-Werror $4])])\nAC_DEFUN([check_link_flag],\n  [AX_CHECK_LINK_FLAG([$1], [$2], [$3], [-Werror $4])])\n\nAC_ARG_ENABLE([hardening],\n  [AS_HELP_STRING([--enable-hardening],\n    [Enable compiler and linker options to frustrate memory corruption exploits @<:@yes@:>@])],\n  [hardening=\"$enableval\"],\n  [hardening=\"yes\"])\n\nHARDEN_CFLAGS=\"\"\nHARDEN_LDFLAGS=\"\"\nAS_IF([test x\"$hardening\" != x\"no\"], [\n  check_cxx_flag([-fno-strict-overflow], [HARDEN_CFLAGS=\"$HARDEN_CFLAGS -fno-strict-overflow\"])\n\n  # This one will likely succeed, even on platforms where it does nothing.\n  check_cxx_flag([-D_FORTIFY_SOURCE=2], [HARDEN_CFLAGS=\"$HARDEN_CFLAGS -D_FORTIFY_SOURCE=2\"])\n\n  check_link_flag([-fstack-protector-all],\n   [HARDEN_CFLAGS=\"$HARDEN_CFLAGS -fstack-protector-all\"\n    check_cxx_flag([-Wstack-protector], [HARDEN_CFLAGS=\"$HARDEN_CFLAGS -Wstack-protector\"],\n      [], [-fstack-protector-all])\n    check_cxx_flag([--param ssp-buffer-size=1], [HARDEN_CFLAGS=\"$HARDEN_CFLAGS --param ssp-buffer-size=1\"],\n      [], [-fstack-protector-all])])\n\n  check_cxx_flag([-fPIE],\n   [check_link_flag([-fPIE -pie],\n     [HARDEN_CFLAGS=\"$HARDEN_CFLAGS -fPIE\"\n      HARDEN_LDFLAGS=\"$HARDEN_LDFLAGS -pie\"],\n     [check_link_flag([-fPIE -Wl,-pie],\n       [HARDEN_CFLAGS=\"$HARDEN_CFLAGS -fPIE\"\n        HARDEN_LDFLAGS=\"$HARDEN_LDFLAGS -Wl,-pie\"])])])\n\n  check_link_flag([-Wl,-z,relro],\n   [HARDEN_LDFLAGS=\"$HARDEN_LDFLAGS -Wl,-z,relro\"\n    check_link_flag([-Wl,-z,now], [HARDEN_LDFLAGS=\"$HARDEN_LDFLAGS -Wl,-z,now\"])])])\nAC_SUBST([HARDEN_CFLAGS])\nAC_SUBST([HARDEN_LDFLAGS])\n\n# Also check for a few non-hardening-related flags.\nAX_CHECK_COMPILE_FLAG([-fno-default-inline],\n  [MISC_CXXFLAGS=\"$MISC_CXXFLAGS -fno-default-inline\"], [], [-Werror])\nAX_CHECK_COMPILE_FLAG([-pipe],\n  [MISC_CXXFLAGS=\"$MISC_CXXFLAGS -pipe\"], [], [-Werror])\nAC_SUBST([MISC_CXXFLAGS])\n\n# End of flag tests.\nCXX=\"$saved_CXX\"\nLD=\"$saved_LD\"\n\nAC_ARG_ENABLE([client],\n  [AS_HELP_STRING([--enable-client], [Build the mosh-client program @<:@yes@:>@])],\n  [build_client=\"$enableval\"],\n  [build_client=\"yes\"])\nAM_CONDITIONAL([BUILD_CLIENT], [test x\"$build_client\" != xno])\n\nAC_ARG_ENABLE([server],\n  [AS_HELP_STRING([--enable-server], [Build the mosh-server program @<:@yes@:>@])],\n  [build_server=\"$enableval\"],\n  [build_server=\"yes\"])\nAM_CONDITIONAL([BUILD_SERVER], [test x\"$build_server\" != xno])\n\nAC_ARG_ENABLE([examples],\n  [AS_HELP_STRING([--enable-examples], [Build the miscellaneous programs in src/examples @<:@no@:>@])],\n  [build_examples=\"$enableval\"],\n  [build_examples=\"no\"])\nAM_CONDITIONAL([BUILD_EXAMPLES], [test x\"$build_examples\" != xno])\n\nAC_ARG_ENABLE([ufw],\n  [AS_HELP_STRING([--enable-ufw], [Install firewall profile for ufw (Uncomplicated Firewall) @<:@no@:>@])],\n  [install_ufw=\"$enableval\"],\n  [install_ufw=\"no\"])\nAM_CONDITIONAL([INSTALL_UFW], [test x\"$install_ufw\" != xno])\n\nAC_ARG_ENABLE([completion],\n  [AS_HELP_STRING([--enable-completion], [Install bash_completion rule @<:@no@:>@])],\n  [install_completion=\"$enableval\"],\n  [install_completion=\"no\"])\nAM_CONDITIONAL([INSTALL_COMPLETION], [test x\"$install_completion\" != xno])\n\nAC_ARG_ENABLE([syslog],\n  [AS_HELP_STRING([--enable-syslog], [Enable connection logging in mosh-server @<:@no@:>@])],\n  [enable_syslog=\"$enableval\"],\n  [enable_syslog=\"no\"])\nAS_IF([test x\"$enable_syslog\" != xno],\n  [AC_CHECK_HEADERS([syslog.h],\n    [AC_DEFINE([HAVE_SYSLOG], [1], [Define if syslog is available.])],\n    [AS_IF([test x\"$enable_syslog\" = xcheck],\n      [AC_MSG_WARN([Unable to find syslog.h.])],\n      [AC_MSG_ERROR([--enable-syslog was given but syslog.h was not found.])])])])\n\n# Checks for libraries.\nAC_ARG_ENABLE([static-libraries],\n  [AS_HELP_STRING([--enable-static-libraries], [Enable all static linking options below @<:@no@:>@])])\n\nAC_ARG_ENABLE([static-libstdc++],\n  [AS_HELP_STRING([--enable-static-libstdc++], [Link libstdc++ statically @<:@no@:>@])],\n  [], [enable_static_libstdc__=\"$enable_static_libraries\"])\nAS_IF([test \"$enable_static_libstdc__\" = yes],\n  [LDFLAGS=\"$LDFLAGS -static-libstdc++\"])\n\nAC_ARG_ENABLE([static-libgcc],\n  [AS_HELP_STRING([--enable-static-libgcc], [Link libgcc statically @<:@no@:>@])],\n  [], [enable_static_libgcc=\"$enable_static_libraries\"])\nAS_IF([test \"$enable_static_libgcc\" = yes],\n  [LDFLAGS=\"$LDFLAGS -static-libgcc\"])\n\nAC_ARG_WITH([utempter],\n  [AS_HELP_STRING([--with-utempter], [write utmp entries using libutempter @<:@check@:>@])],\n  [with_utempter=\"$withval\"],\n  [with_utempter=\"check\"])\nAC_ARG_ENABLE([static-utempter],\n  [AS_HELP_STRING([--enable-static-utempter], [Link utempter statically @<:@no@:>@])],\n  [], [enable_static_utempter=\"$enable_static_libraries\"])\nAS_IF([test x\"$with_utempter\" != xno],\n  [AC_CHECK_LIB([utempter], [utempter_remove_record],\n    [UTEMPTER_LIBS=-lutempter\n     AS_IF([test \"$enable_static_utempter\" = yes],\n       [UTEMPTER_LIBS=\"-Wl,-Bstatic $UTEMPTER_LIBS -Wl,-Bdynamic\"])\n     LIBS=\"$UTEMPTER_LIBS $LIBS\"\n     AC_DEFINE([HAVE_UTEMPTER], [1], [Define if libutempter is available.])],\n    [AS_IF([test x\"$with_utempter\" = xcheck],\n      [AC_MSG_WARN([Unable to find libutempter; utmp entries will not be made.])],\n      [AC_MSG_ERROR([--with-utempter was given but libutempter was not found.])])])])\n\nAC_ARG_ENABLE([static-zlib],\n  [AS_HELP_STRING([--enable-static-zlib], [Link zlib statically @<:@no@:>@])],\n  [], [enable_static_zlib=\"$enable_static_libraries\"])\nAC_CHECK_LIB([z], [compress],\n  [ZLIB_LIBS=-lz\n   AS_IF([test \"$enable_static_zlib\" = yes],\n     [ZLIB_LIBS=\"-Wl,-Bstatic $ZLIB_LIBS -Wl,-Bdynamic\"])\n   LIBS=\"$ZLIB_LIBS $LIBS\"],\n  [AC_MSG_ERROR([Unable to find zlib.])])\n\nAC_SEARCH_LIBS([socket], [socket network])\nAC_SEARCH_LIBS([inet_addr], [nsl])\n\nAC_SEARCH_LIBS([clock_gettime], [rt])\n\n# Checks for header files.\nAC_CHECK_HEADERS(m4_normalize([\n  fcntl.h\n  langinfo.h\n  limits.h\n  locale.h\n  netdb.h\n  netinet/in.h\n  stddef.h\n  stdint.h\n  inttypes.h\n  stdlib.h\n  string.h\n  sys/ioctl.h\n  sys/random.h\n  sys/resource.h\n  sys/socket.h\n  sys/stat.h\n  sys/time.h\n  termios.h\n  unistd.h\n  wchar.h\n  wctype.h\n  ]), [], [AC_MSG_ERROR([Missing required header file.])])\n\nAC_CHECK_HEADERS([pty.h util.h libutil.h paths.h])\nAC_CHECK_HEADERS([endian.h sys/endian.h])\nAC_CHECK_HEADERS([utmpx.h])\nAC_CHECK_HEADERS([termio.h])\nAC_CHECK_HEADERS([sys/uio.h])\nAC_CHECK_HEADERS([memory tr1/memory])\n\n# Checks for typedefs, structures, and compiler characteristics.\nAC_C_INLINE\nAC_TYPE_INT64_T\nAC_TYPE_PID_T\nAC_C_RESTRICT\nAC_TYPE_SIZE_T\nAC_TYPE_SSIZE_T\nAC_TYPE_UINT16_T\nAC_TYPE_UINT32_T\nAC_TYPE_UINT64_T\nAC_TYPE_UINT8_T\nAC_TYPE_UINTPTR_T\n\n# Checks for library functions.\nAC_CHECK_FUNCS(m4_normalize([\n  getentropy\n  getrandom\n  gettimeofday\n  posix_memalign\n  cfmakeraw\n  pselect\n  pledge\n  ]))\n\n# Start by trying to find the needed tinfo parts by pkg-config\nPKG_CHECK_MODULES([TINFO], [tinfo],\n  [AC_DEFINE([HAVE_CURSES_H], [1], [Define to 1 if <curses.h> is present])],\n  [PKG_CHECK_MODULES([TINFO], [ncurses],\n    [AC_DEFINE([HAVE_CURSES_H], [1], [Define to 1 if <curses.h> is present])],\n    [AX_CHECK_LIBRARY([TINFO], [curses.h], [tinfo],\n      [AC_DEFINE([HAVE_CURSES_H], [1], [Define to 1 if <curses.h> is present])\n        AC_SUBST([TINFO_CFLAGS], [\"$TINFO_CPPFLAGS\"])\n        AC_SUBST([TINFO_LIBS], [\"$TINFO_LDFLAGS -ltinfo\"])],)])])\n\n# Then try to find it in a specific install dir\nAC_ARG_WITH(curses, [AS_HELP_STRING([--with-curses=DIR], [Where curses is installed])],\n    [if test $withval != yes; then\n        cv_curses=$withval\n    fi\n    if test x$cv_curses != x/usr; then\n        CURSES_LDFLAGS=\"-L${cv_curses}/lib\"\n        CURSES_CPPFLAGS=\"-I${cv_curses}/include\"\n    fi])\n\nif test \"x$ax_cv_have_TINFO\" = xno ; then\n    # save away old LDFLAGS/CPPFLAGS and check for curses with cv_curses\n    old_LDFLAGS=\"$LDFLAGS\"\n    old_CPPFLAGS=\"$CPPFLAGS\"\n    LDFLAGS=\"$LDFLAGS $CURSES_LDFLAGS\"\n    CPPFLAGS=\"$CPPFLAGS $CURSES_CPPFLAGS\"\n\n    AX_WITH_CURSES\n    # restore old LDFLAGS/CPPFLAGS\n    LDFLAGS=\"$old_LDFLAGS\"\n    CPPFLAGS=\"$old_CPPFLAGS\"\n\n    if test \"x$ax_cv_curses_which\" = xno ; then\n        AC_MSG_ERROR([requires either tinfo, NcursesW or Ncurses library])\n    else\n        AC_SUBST([TINFO_LIBS], [\"$CURSES_LDFLAGS $CURSES_LIB\"])\n        AC_SUBST([TINFO_CFLAGS], [\"$CURSES_CPPFLAGS\"])\n    fi\nfi\n\nAC_ARG_ENABLE([static-curses],\n  [AS_HELP_STRING([--enable-static-curses], [Link curses statically @<:@no@:>@])],\n  [], [enable_static_curses=\"$enable_static_libraries\"])\nAS_IF([test \"$enable_static_curses\" = yes],\n  [TINFO_LIBS=\"-Wl,-Bstatic $TINFO_LIBS -Wl,-Bdynamic\"])\n\ndnl Default to OpenSSL, or OS X crypto library if found\nAC_CHECK_HEADERS([CommonCrypto/CommonCrypto.h],\n  [default_crypto_library=\"apple-common-crypto\"],\n  [default_crypto_library=\"openssl\"]\n)\n\ndnl Allow user to select over the default.\nAC_ARG_WITH(\n  [crypto-library],\n  [AS_HELP_STRING([--with-crypto-library=library], [build with the given crypto library, TYPE=openssl|nettle|apple-common-crypto @<:@default=openssl@:>@])],\n  [\n    case \"${withval}\" in\n      openssl|openssl-with-internal-ocb|openssl-with-openssl-ocb|nettle|apple-common-crypto) ;;\n      *) AC_MSG_ERROR([bad value ${withval} for --with-crypto-library]) ;;\n    esac\n  ],\n  [with_crypto_library=\"$default_crypto_library\"]\n)\n\ndnl Checks for chosen crypto library\nPKG_CHECK_MODULES([OpenSSL], [openssl],\n  [have_openssl=yes\n    AC_CHECK_LIB([crypto], [AES_encrypt], [have_deprecated_openssl_aes=yes])\n    AC_CHECK_LIB([crypto], [EVP_aes_128_ocb], [have_evp_aes_ocb=yes])],\n  [:])\nPKG_CHECK_MODULES([Nettle], [nettle], [have_nettle=yes], [:])\nAS_CASE([$with_crypto_library],\n  [openssl*],\n    [AS_IF([test \"x$have_openssl\" != xyes],\n       [AC_MSG_ERROR([OpenSSL crypto library not found])])\n     AC_DEFINE([USE_OPENSSL_AES], [1], [Use OpenSSL library])\n     AC_SUBST([CRYPTO_CFLAGS], [\"$OpenSSL_CFLAGS\"])\n     AC_SUBST([CRYPTO_LIBS], [\"$OpenSSL_LIBS -lcrypto\"])])\ncase \"${with_crypto_library}\" in\n  openssl|openssl-with-internal-ocb)\n    AS_IF([test \"x$have_deprecated_openssl_aes\" != xyes],\n      [AC_MSG_ERROR([found OpenSSL without AES support])])\n    AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [false])\n    human_readable_cryptography_description='internal OCB, OpenSSL AES'\n    ;;\n  openssl-with-openssl-ocb)\n    AS_IF([test \"x$have_evp_aes_ocb\" != xyes],\n      [AC_MSG_ERROR([found OpenSSL without AES-OCB support])])\n    AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [true])\n    human_readable_cryptography_description='OpenSSL OCB, OpenSSL AES'\n    ;;\n  nettle)\n    AS_IF([test \"x$have_nettle\" != xyes],\n      [AC_MSG_ERROR([Nettle crypto library not found])])\n    AC_DEFINE([USE_NETTLE_AES], [1], [Use Nettle library])\n    AC_SUBST([CRYPTO_CFLAGS], [\"$Nettle_CFLAGS\"])\n    AC_SUBST([CRYPTO_LIBS], [\"$Nettle_LIBS\"])\n    AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [false])\n    human_readable_cryptography_description='internal OCB, Nettle AES'\n    ;;\n  apple-common-crypto)\n    AS_IF([test \"x$ac_cv_header_CommonCrypto_CommonCrypto_h\" != xyes],\n      [AC_MSG_ERROR([Apple Common Crypto header not found])])\n    AC_DEFINE([USE_APPLE_COMMON_CRYPTO_AES], [1], [Use Apple Common Crypto library])\n    AM_CONDITIONAL([USE_AES_OCB_FROM_OPENSSL], [false])\n    human_readable_cryptography_description='internal OCB, Apple Common Crypto AES'\n    ;;\nesac\n\nAC_ARG_ENABLE([static-crypto],\n  [AS_HELP_STRING([--enable-static-crypto], [Link crypto library statically @<:@no@:>@])],\n  [], [enable_static_crypto=\"$enable_static_libraries\"])\nAS_IF([test \"$enable_static_crypto\" = yes],\n  [CRYPTO_LIBS=\"-Wl,-Bstatic $CRYPTO_LIBS -Wl,-Bdynamic\"])\n\nAC_CHECK_DECL([forkpty],\n  [AC_DEFINE([FORKPTY_IN_LIBUTIL], [1],\n     [Define if libutil.h necessary for forkpty().])],\n  , [[#include <sys/types.h>\n#include <libutil.h>]])\n\nAC_SEARCH_LIBS([forkpty], [util bsd], [\n  AC_DEFINE([HAVE_FORKPTY],, [Define if you have forkpty().])\n])\n\nAC_MSG_CHECKING([whether FD_ISSET() argument is const])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/select.h>\nclass T {\npublic:\n  fd_set fds;\n  bool Fun( void ) const { return FD_ISSET( 0, &fds ); } };]],\n[[T x = T(); return x.Fun();]])],\n  [AC_DEFINE([FD_ISSET_IS_CONST], [1],\n     [Define if FD_ISSET() fd_set argument is const.])\n   AC_MSG_RESULT([yes])],\n  [AC_MSG_RESULT([no])])\n\nAC_MSG_CHECKING([whether std::shared_ptr is available])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <memory>\nclass T {\npublic:\n  std::shared_ptr<int> Fun( void ) { return std::shared_ptr<int>( new int ( 0 ) ); } };]],\n[[T x; return !!x.Fun();]])],\n  [AC_DEFINE([HAVE_STD_SHARED_PTR], [1],\n     [Define if std::shared_ptr is available.])\n   AC_MSG_RESULT([yes])],\n  [AC_MSG_RESULT([no])])\n\nAC_MSG_CHECKING([whether std::tr1::shared_ptr is available])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <tr1/memory>\nclass T {\npublic:\n  std::tr1::shared_ptr<int> Fun( void ) { return std::tr1::shared_ptr<int>( new int ( 0 ) ); } };]],\n[[T x; return !!x.Fun();]])],\n  [AC_DEFINE([HAVE_STD_TR1_SHARED_PTR], [1],\n     [Define if std::tr1::shared_ptr is available.])\n   AC_MSG_RESULT([yes])],\n  [AC_MSG_RESULT([no])])\n\nAC_MSG_CHECKING([whether clock_gettime() is supported])\nAC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <time.h>\nstruct timespec ts;\n]], [[return clock_gettime(CLOCK_MONOTONIC, &ts);]])],\n  [AC_DEFINE([HAVE_CLOCK_GETTIME], [1],\n     [Define if clock_gettime() is available.])\n   AC_MSG_RESULT([yes])],\n  [AC_MSG_RESULT([no])])\n\nAC_CHECK_DECL([mach_absolute_time],\n  [AC_DEFINE([HAVE_MACH_ABSOLUTE_TIME], [1],\n     [Define if mach_absolute_time is available.])],\n  , [[#include <mach/mach_time.h>]])\n\n\nAC_CHECK_DECLS([__builtin_ctz])\n\nAC_CHECK_DECLS([ffs], [], [],\n  [[#include <strings.h>]])\n\nAC_CHECK_DECLS([be64toh, betoh64, bswap64, __builtin_bswap64], [], [],\n  [[#if defined(HAVE_ENDIAN_H)\n#include <endian.h>\n#elif defined(HAVE_SYS_ENDIAN_H)\n#include <sys/types.h>\n#include <sys/endian.h>\n#endif]])\n\nAS_IF([test x\"$ac_cv_have_decl_be64toh\" != xyes &&\n       test x\"$ac_cv_have_decl_betoh64\" != xyes],\n  [AC_CHECK_DECL([OSSwapHostToBigInt64],\n     [AC_DEFINE([HAVE_OSX_SWAP], [1],\n        [Define if OSSwapHostToBigInt64 and friends exist.])],\n     [AC_MSG_WARN([Unable to find byte swapping functions; using built-in routines.])],\n     [[#include <libkern/OSByteOrder.h>]])])\n\nAC_CHECK_DECL([IP_MTU_DISCOVER],\n  [AC_DEFINE([HAVE_IP_MTU_DISCOVER], [1],\n     [Define if IP_MTU_DISCOVER is a valid sockopt.])],\n  , [[#include <netinet/in.h>]])\n\nAC_CHECK_DECL([IP_RECVTOS],\n  [AC_DEFINE([HAVE_IP_RECVTOS], [1],\n     [Define if IP_RECVTOS is a valid sockopt.])],\n  , [[#include <netinet/in.h>]])\n\nAC_CHECK_DECL([__STDC_ISO_10646__],\n  [],\n  [AC_MSG_WARN([C library doesn't advertise wchar_t is Unicode (OS X works anyway with workaround).])],\n  [[#include <wchar.h>]])\n\nAC_CHECK_DECL([IUTF8],\n  [AC_DEFINE([HAVE_IUTF8], [1],\n     [Define if IUTF8 is a defined termios mode.])],\n  [AC_MSG_WARN([No IUTF8 termios mode; character-erase of multibyte character sequence probably does not work properly in canonical mode on this platform.])],\n  [[#include <termios.h>]])\n\n# Checks for protobuf\nPKG_CHECK_MODULES([protobuf], [protobuf])\n\n# On Debian Sid 2016-03-16ish at least, \"pkgconfig --libs protobuf\"\n# gives us the bogus \"-lprotobuf -pthread -lpthread\"; remove\n# \"-lpthread\" which misconfigures compile, causing a segfault in\n# mosh-server.\nAS_IF([echo \"$protobuf_LIBS\" | grep -q -- -pthread],\n  [protobuf_LIBS=\"`echo $protobuf_LIBS | sed 's/-lpthread//g'`\"])\n\nAC_ARG_ENABLE([static-protobuf],\n  [AS_HELP_STRING([--enable-static-protobuf], [Link protobuf statically @<:@no@:>@])],\n  [], [enable_static_protobuf=\"$enable_static_libraries\"])\nAS_IF([test \"$enable_static_protobuf\" = yes],\n  [protobuf_LIBS=\"-Wl,-Bstatic $protobuf_LIBS -Wl,-Bdynamic\"],\n  [AS_IF([test \"$enable_static_libstdc__\" = yes],\n     [AC_MSG_ERROR([--enable-static-libstdc++ requires --enable-static-protobuf])])\n   AS_IF([test \"$enable_static_libgcc\" = yes],\n     [AC_MSG_ERROR([--enable-static-libgcc requires --enable-static-protobuf])])])\n\nAC_MSG_CHECKING([whether protoc matches protobuf])\ncat > conftest.proto <<'EOF'\nsyntax = \"proto2\";\noption optimize_for = LITE_RUNTIME;\nEOF\nAS_IF([$PROTOC --cpp_out=. conftest.proto],\n  [old_CPPFLAGS=\"$CPPFLAGS\"\n   CPPFLAGS=\"$CPPFLAGS $protobuf_CFLAGS\"\n   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include \"conftest.pb.h\"]], [[]])],\n    [AC_MSG_RESULT([yes])],\n    [AC_MSG_RESULT([no])\n     cat conftest.err\n     AC_MSG_FAILURE([Could not build output generated by protoc ($PROTOC).\nCheck that protoc matches the installed protobuf headers and libraries.])])\n   CPPFLAGS=\"$old_CPPFLAGS\"],\n  [AC_MSG_RESULT([no])\n   AC_MSG_ERROR([Could not run protoc ($PROTOC).])])\n\n# Bash completion needs to ask where it goes if >= 2.0 is installed.\nAS_IF([test \"$install_completion\" != no],\n  [PKG_CHECK_MODULES([BASH_COMPLETION], [bash-completion >= 2.0],\n     [if test \"$prefix\" = \"NONE\"; then\n        completions=\"`pkg-config --variable=completionsdir bash-completion`\"\n      else\n        completions=\"`pkg-config --define-variable=prefix=$prefix --variable=completionsdir bash-completion`\"\n      fi],\n     [completions=\"${sysconfdir}/bash_completion.d\"])\n   AC_SUBST([completions])])\n\nAC_CONFIG_FILES([\n  Makefile\n  src/Makefile\n  src/crypto/Makefile\n  src/frontend/Makefile\n  src/fuzz/Makefile\n  src/include/Makefile\n  src/network/Makefile\n  src/protobufs/Makefile\n  src/statesync/Makefile\n  src/terminal/Makefile\n  src/util/Makefile\n  scripts/Makefile\n  src/examples/Makefile\n  src/tests/Makefile\n  man/Makefile\n  conf/Makefile\n])\nAC_OUTPUT\n\nAC_MSG_NOTICE([ === Configuration results ===])\nAC_MSG_NOTICE([Version:             $PACKAGE_VERSION])\nAC_MSG_NOTICE([c++ compiler:        $CXX])\nAC_MSG_NOTICE([Warning CXXFLAGS:    $WARNING_CXXFLAGS])\nAC_MSG_NOTICE([Picky CXXFLAGS:      $PICKY_CXXFLAGS])\nAC_MSG_NOTICE([Harden CFLAGS:       $HARDEN_CFLAGS])\nAC_MSG_NOTICE([Cryptography:        $human_readable_cryptography_description])\nAC_MSG_NOTICE([ =============================])\n"
  },
  {
    "path": "debian/changelog",
    "content": "mosh (1.4.0-2) UNRELEASED; urgency=medium\n\n  * Remove Benjamin Barenblat from Uploaders.\n\n -- Benjamin Barenblat <bbaren@debian.org>  Mon, 19 Jan 2026 19:38:42 -0500\n\nmosh (1.4.0-1) unstable; urgency=medium\n\n  * New upstream release.\n\n -- Benjamin Barenblat <bbaren@debian.org>  Fri, 11 Nov 2022 22:33:06 -0500\n\nmosh (1.3.2.95rc2-1) experimental; urgency=medium\n\n  * New upstream release candidate.\n\n -- Benjamin Barenblat <bbaren@debian.org>  Wed, 26 Oct 2022 20:37:12 -0400\n\nmosh (1.3.2-2) unstable; urgency=medium\n\n  * Fix \"mosh FTBFS with gcc 7\" <remove default constructors>\n    (Closes: #871030)\n\n -- Keith Winstein <keithw@mit.edu>  Thu, 24 Aug 2017 15:04:25 -0700\n\nmosh (1.3.2-1) unstable; urgency=medium\n\n  * Version 1.3.2 released to unstable.\n  * Fixes Debian revision inversion of 1.3.1-rc3-1 > 1.3.1-1\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Fri, 21 Jul 2017 14:41:44 -0700\n\nmosh (1.3.1-1) unstable; urgency=medium\n\n  * Version 1.3.1 released to unstable.\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Thu, 20 Jul 2017 17:32:58 -0400\n\nmosh (1.3.1-rc3-1) experimental; urgency=medium\n\n  * Platform support:\n    * Explicitly enable binding to both IPv4 and IPv6 addresses.\n      (Giel van Schijndel)\n\n  * Bug fixes:\n    * In tests, explicitly set 80x24 tmux window, for newer versions\n      of tmux.  (John Hood)\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Wed, 19 Jul 2017 22:27:12 -0400\n\nmosh (1.3.1~rc2-1) experimental; urgency=medium\n\n  * Build fix.\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Fri, 19 May 2017 19:25:51 -0400\n\nmosh (1.3.1~rc1-1) experimental; urgency=medium\n\n  * Platform support:\n    * Restore perl 5.8.8 support for RHEL5.  (Alexander Chernyakhovsky)\n    * Make tests detect UTF-8 locale with a helper executable.  (John Hood)\n    * Don't print /etc/motd on IllumOS.  (John Hood)\n    * Print {,/var}/run/motd.dynamic on Ubuntu.  (John Hood)\n    * Fix build on Haiku. (Adrien Destugues)\n    * Disable unicode-later-combining.test for tmux 2.4.\n      This fixes build failures.  (John Hood)\n\n  * Bug fixes:\n    * Work around JuiceSSH rendering bug.  (John Hood)\n    * Do not move cursor for SCROLL UP and SCROLL DOWN--\n      fixes an issue with tmux 2.4.  (John Hood)\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Thu, 18 May 2017 00:52:01 -0400\n\nmosh (1.3.0-1) unstable; urgency=medium\n\n  * Version 1.3.0 released to unstable.\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Sat, 25 Mar 2017 12:55:31 -0700\n\nmosh (1.3.0~rc3-1) experimental; urgency=medium\n\n  * Workaround for Debian bug #817236 on buildds\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Fri, 03 Mar 2017 02:21:02 -0800\n\nmosh (1.3.0~rc2-1) unstable; urgency=medium\n\n  * New features:\n    * Change website URLs from http://mosh.mit.edu to\n      https://mosh.org.  (Keith Winstein)\n    * Add --no-ssh-pty option for Dropbear compatibility and\n      other issues.\n    * Switch to semantic versioning, making this version 1.3.0\n      instead of 1.2.7.\n\n  * Platform support:\n    * Added nonce-incrementing test.  (Keith Winstein)\n    * Add build-source-package.sh for Debian.  (Keith Winstein)\n    * Fix CPPFLAGS handling possibly causing curses detection\n      failure.  (John Hood)\n    * Add an Appveyor/Cygwin CI build.\n    * Improve warning-flags detection for 'make distcheck'.  (John Hood)\n    * Improve robustness of regression tests.  (John Hood)\n    * Support OpenBSD pledge() sandboxing.  (John Hood)\n    * Use backward-compatible name for AES in\n      AppleCommonCrypto, fixing builds with older OS X SDKs.  (John Hood)\n    * Detect clock_gettime() and CLOCK_MONOTONIC carefully,\n      fixing OS X 10.12 + Xcode 7.3 builds.  (John Hood)\n    * Support older versions of Perl, back to 5.10, fixing\n      RHEL 5 builds. (Anders Kaseorg)\n    * Add a Travis OS X CI and release build.  (John Hood)\n    * Add --help and --version, enabling Automake's\n     'std-options' checks.  (Anders Kaseorg)\n    * Add a simple smoke test not requiring tmux, to help\n      validate builds on older platforms including RHEL 5. (Anders Kaseorg)\n    * Check for presence of clock_gettime() for OS X, where\n      the symbol may not be resolved on older OS X versions.  (John\n      Hood)\n    * Fix a memory alignment issue in OCB with ARM/Neon. (Carlos Cabanero)\n    * Mosh now runs correctly on Bash for Windows with Windows 10\n      Insider builds 15002 and higher. (No change in Mosh)\n    * Other minor platform compatibility fixes for Mosh\n      sources and tests.  (John Hood)\n\n  * Bug fixes:\n    * Work around a pty buffering issue causing failed\n      connections on FreeBSD 11, or with Dropbear.  (John Hood)\n    * Restore '-p 0' option for OS-selected UDP port bindings.  (John Hood)\n    * Shell hygiene fixes, including better quoting of\n      pathnames.  (Anders Kaseorg)\n    * Fix typos in project docs.  (Jakub Wilk)\n    * Fix excess newlines on mosh client startup/shutdown.  (John Hood)\n    * Exit gracefully, closing session, on pty write or\n      ioctl failure.  (John Hood)\n    * Fix two bugs that caused mosh-server to consume\n      excessive CPU in certain circumstances.  (John Hood)\n    * Fix bug that caused text copied from mosh-client to\n      paste as long lines joined by spaces.  (John Hood)\n    * Documentation improvements. (chenxiaoqino, Ashish Gupta)\n    * Use getuid(), not geteuid(), for correct getpw* lookups.  (John Hood)\n\n -- Keith Winstein <keithw@cs.stanford.edu>  Mon, 06 Feb 2017 23:38:41 -0800\n\nmosh (1.2.6-1) unstable; urgency=low\n\n  * Version 1.2.6 released.\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 10 Aug 2016 01:02:31 -0700\n\nmosh (1.2.5.95rc1-1) unstable; urgency=low\n\n  * Version 1.2.6 released.\n\n  * New features:\n    * Add Travis CI builds for Linux and Mac.  (Anders Kaseorg, others)\n    * Add a --local option to run without ssh.  (John Hood)\n    * Mosh now returns exitstatus reflecting connection success.\n      (John Hood)\n    * Add a end-to-end test suite and many tests.  (John Hood)\n    * Implement timeouts and signals to help address orphaned sessions.\n      (John Hood)\n    * Major rework of Mosh's display differencing/rendering\n      code with much improved performance for slow machines.  (John Hood)\n    * Implement ANSI back/forward tab (CSI CBT, CSI CHT).\n      (John Hood)\n    * Do not start user shell until network session starts.\n      (John Hood)\n    * Add options for more flexible specification of IPv4/IPv6\n      hostname resolution.  (John Hood)\n    * Improved bash completion.  (Steve Dignam, HIGUCHI Yuta)\n    * Add options for different methods of resolving the remote host\n      address, allowing operation without SshProxyCommand.  (John Hood)\n\n  * Platform support:\n    * Add configurable support for Apple Common Crypto and\n      Nettle, in place of OpenSSL.  Implement base64 locally.\n      (John Hood)\n    * Workaround Cygwin select() bug.  (John Hood)\n    * Updates to Debian packaging.  (Anders Kaseorg, Keith Winstein)\n    * Workaround a glibc-2.22 issue causing segfaults on Debian Sid.\n      (John Hood with help from many others)\n    * Prefer c++ to g++, for systems like FreeBSD where g++ is not usable.\n      (John Hood)\n    * Fixes for Illumos Hipster 20151003.  (John Hood)\n    * Disable -Werror for protobuf code, to resolve a new gcc6 warning.\n      (John Hood)\n    * Link test for -fstack-protector-all on an embedded platform.\n      (Baruch Siach)\n    * Resolve issue with bswap64() on FreeBSD-CURRENT with libc++-3.8.0.\n      (John Hood)\n    * Fix issue with RECVTOS error message on client on FreeBSD.\n      (John Hood)\n\n  * Bug fixes:\n    * Remove an assertion causing aborts on Unicode fallback found by\n      fuzzing with afl.  (Keith Winstein)\n    * Fix a server hang with XON/XOFF on BSD systems.  (John Hood)\n    * Fix a typeahead-prediction bug that caused display corruption on\n      urxvt.  (John Hood)\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 23 May 2016 20:43:00 -0400\n\nmosh (1.2.5-2) unstable; urgency=medium\n\n  * debian/rules: Work around protobuf/glibc build problem\n    causing mosh-server segfault. (Closes: #817929) (John Hood)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 23 Mar 2016 22:05:34 -0700\n\nmosh (1.2.5-1.1) unstable; urgency=medium\n\n  * Non-maintainer upload.\n  * debian/mosh.maintscript: Fix /etc/bash_completion.d/mosh removal, thanks\n    to for the patch Jakub Wilk <jwilk@debian.org> (Closes: #803253)\n\n -- Laurent Bigonville <bigon@debian.org>  Sun, 31 Jan 2016 16:19:09 +0100\n\nmosh (1.2.5-1) unstable; urgency=low\n\n  * Version 1.2.5 released.\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 12 Jul 2015 12:42:00 -0400\n\nmosh (1.2.4.95rc2-1) unstable; urgency=low\n\n  * Version 1.2.5 release candidate.\n\n  * New features:\n    * Bind to a specific IP address with --bind-server. (Philipp\n      Haselwarter)\n    * MOSH_ESCAPE_KEY configures escape character.  (Timo\n      J. Rinne)\n    * Support non-roaming IPv6. (Anders Kaseorg)\n    * Implement XTerm mouse mode. (Barosl LEE, Andrew Chin,\n      Louis Kruger)\n    * Report Git revision along with version if available.\n      (John Hood)\n\n  * Platform support:\n    * Add pselect() emulation. (Jérémie Courrèges-Anglas)\n    * OpenBSD, OS X: Fix be64toh-related issues. (Jérémie\n      Courrèges-Anglas)\n    * ARM Neon: fix gcc4.8 compiling problem(Pasi Sjöholm)\n    * NaCl: Conditionally rename main to mosh_main. (Richard\n      Woodbury)\n    * FreeBSD: Token pasting, forkpty(), ARM fixes. (John Hood)\n    * AIX: Implement CTTY grabbing when TIOCSCTTY is missing\n      (Anton Lundin)\n    * OS X: Broaden build support to cover OS X 10.5 through\n      10.10. (John Hood)\n    * Debian: Improve bash-completion install and\n      functionality. (Suggested by Gabriel Filion, John Hood)\n\n  * Bug fixes:\n    * Automake/autoconf workarounds.  (Anders Kaseorg)\n    * mosh-server: Allow startup without PTY.  (Keith Winstein)\n    * network.cc: Properly close old fd on Socket assignment\n      operator. (Thanks to Igor Bukanov)\n    * mosh-server:  Allow startup with zero-window-size PTY.\n      (Igor Bukanov)\n    * AddrInfo: Fix error message generation when node == NULL\n      (Anders Kaseorg)\n    * Timestamp: Prevent integer overflow on Darwin PPC 32-bit\n      (Anders Kaseorg)\n    * scripts/mosh: Fix hang when remote closes the connection\n      (Anders Kaseorg)\n    * Fix issues with parsing of 256-color SGR sequences.\n      (John Hood)\n    * Numerous code hygiene, Coverity, and Clang static analyzer\n      fixes.  (Anders Kaseorg, Geoffrey Thomas, John Hood)\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 08 Jun 2015 22:45:00 -0400\n\nmosh (1.2.4a-1) unstable; urgency=low\n\n  * Eliminate redundant ocb.cc test (fixes build warning on ARM/MIPS/s390)\n  * Add explicit cast to int (fixes build warning on older gcc)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 27 Mar 2013 18:32:33 -0400\n\nmosh (1.2.4-1) unstable; urgency=low\n\n  * Version 1.2.4 released.\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 27 Mar 2013 00:17:39 -0400\n\nmosh (1.2.3.95rc1-1) unstable; urgency=low\n\n  * Version 1.2.4 release candidate\n\n  * New features:\n    * Support port ranges with -p LOWPORT:HIGHPORT (Luke Mewburn)\n    * Ctrl-^ Ctrl-Z suspends mosh client (Nikolai Zeldovich)\n    * mm:ss display of lost-contact times (Kevin Ballard)\n    * Show infobar with control chars when Ctrl-^ is typed\n    * Put terminal in altscreen mode (Anders Kaseorg)\n    * Tell automake/Debian pkg about our checks (Anders Kaseorg)\n\n  * Platform support:\n    * OS X: Script to build a universal package (Peter Iannucci)\n    * FreeBSD: Fix build problems (Jimmy Olgeni)\n    * AIX: port by Anton Lundin\n    * Solaris with system curses (Anton Lundin)\n    * Cygwin and others: eliminate use of IO::Pty (Anton Lundin)\n\n  * Bug fixes:\n    * Fix bug (introduced in 1.2.3) where server stays around\n      if process quits while client is detached\n    * Clean up spurious entries from detached sessions warning\n    * Fix freeze when connectivity is one-directional for hours\n      (reported by Axel Beckert)\n    * Don't wipe title until a new one is set (sqweek)\n    * Eliminate memory leaks and cppcheck warnings (Anders Kaseorg)\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 10 Mar 2013 17:46:37 -0400\n\nmosh (1.2.3-1) unstable; urgency=low\n\n  * Version 1.2.3 released.\n\n  * Update AES-OCB implementation (Keegan McAllister)\n\n  * More conservative MTU and datagram sizing\n\n -- Keith Winstein <keithw@mit.edu>  Fri, 19 Oct 2012 15:21:54 -0400\n\nmosh (1.2.2.95rc1-1) unstable; urgency=low\n\n  * Security improvements:\n    * Use OpenSSL AES implementation\n    * Don't let bad server dictate IP (Felix Groebert)\n\n  * New features:\n    * Client hops ports to survive challenging client-side firewall\n    * Server stops sending to save client power (Daniel Drown)\n    * Set DiffServ code point and ECN-capable (Dave Täht)\n    * Slow down if explicit congestion notification received\n    * Warn about unattached Mosh sessions on login\n    * Compatible with KDE konsole (uses BEL to terminate OSC)\n    * Improved heuristic about color of predicted characters\n\n  * Bug fixes:\n    * Improved performance on systems with expensive time\n    * No longer choke on \"ffff::\" address for hosts with IPv6\n\n  * Platform support:\n    * Build on Solaris and IllumOS (Timo Sirainen, Ira Cooper)\n    * Build on ARM with gcc 4.7 (Alexander Chernyakhovsky)\n\n  * Licensing changes:\n    * Allow distribution on Apple App Stores\n    * Allow linking with OpenSSL\n\n -- Keith Winstein <keithw@mit.edu>  Fri, 05 Oct 2012 19:45:51 -0400\n\nmosh (1.2.2-1) unstable; urgency=low\n\n  * Version 1.2.2 released.\n\n  * Remove warning on out-of-order/duplicated datagrams\n\n  * Add \"experimental\" prediction mode\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 12 Jun 2012 14:52:17 -0400\n\nmosh (1.2.1-1) unstable; urgency=low\n\n  * Version 1.2.1 released.\n\n  * Warning on out-of-order/duplicated datagrams (or failed nonce increment)\n\n  * Clearer error message on invalid port number\n\n -- Keith Winstein <keithw@mit.edu>  Fri, 25 May 2012 18:03:31 -0400\n\nmosh (1.2.0.97-1) unstable; urgency=low\n\n  * Cosmetic cleanups to quit sequence when server is firewalled\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 23 May 2012 12:16:47 -0400\n\nmosh (1.2.0.96-1) unstable; urgency=low\n\n  * Include bash_completion file (ejeffrey)\n\n  * Include UFW firewall profile (Fumihito YOSHIDA)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 23 May 2012 07:05:54 -0400\n\nmosh (1.2.0.95-1) unstable; urgency=low\n\n  * Improve performance on lossy links.\n\n  * New diagnostic message when link is dead in only one direction.\n\n  * Use less CPU when link is down. (Keegan McAllister)\n\n  * Use much less CPU when application or mosh-server sends\n    large repeat counts (resolves CVE-2012-2385, reported by\n    Timo Juhani Lindfors).\n\n  * Use less memory when mosh-server is malicious.\n\n  * Fix vttest regression re: wrapping and tabs.\n\n  * Enable roundtrip verifier of terminal emulator correctness\n    when verbose.\n\n  * Remove skalibs as a dependency. (Keegan McAllister)\n\n  * Remove use of poll() and OS X poll workaround. (Keegan McAllister)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 23 May 2012 03:36:13 -0400\n\nmosh (1.2-1) unstable; urgency=low\n\n  * Version 1.2 released.\n\n -- Keith Winstein <keithw@mit.edu>  Thu, 26 Apr 2012 01:35:59 -0400\n\nmosh (1.1.99b-1) unstable; urgency=low\n\n  * Update THANKS and copyright information\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 25 Apr 2012 02:53:21 -0400\n\nmosh (1.1.99a-1) unstable; urgency=low\n\n  * Fix using broken system poll on OS X 10.6\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 25 Apr 2012 02:30:44 -0400\n\nmosh (1.1.99-1) unstable; urgency=low\n\n  * Fix build problems on FreeBSD 7 and 8\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 24 Apr 2012 23:52:22 -0400\n\nmosh (1.1.98-1) unstable; urgency=low\n\n  * Fix build problems on Cygwin\n\n  * Fix build problems on Debian armel/armhf\n\n  * Fix alignment problems on ARM\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 24 Apr 2012 19:00:03 -0400\n\nmosh (1.1.97-1) unstable; urgency=low\n\n  * Revert use of protobufs' zero-copy gzip stream\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 23 Apr 2012 22:53:44 -0400\n\nmosh (1.1.96-1) unstable; urgency=low\n\n  * Include binary hardening check script in GNU-style tar.gz\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 23 Apr 2012 20:21:14 -0400\n\nmosh (1.1.95-1) unstable; urgency=low\n\n  * Eliminate PPC32 dependence on posix_memalign()\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 22 Apr 2012 23:18:13 -0400\n\nmosh (1.1.94e-1) unstable; urgency=low\n\n  * Improve responsiveness on fast links\n\n  * Include poll implementation for Mac OS X\n\n  * Use protobufs' zero-copy gzip stream\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 22 Apr 2012 16:59:25 -0400\n\nmosh (1.1.94d-1) unstable; urgency=low\n\n  * More helpful locale-related error messages\n\n  * Improve prediction transparency\n\n  * Honor .hushlogin\n\n  * Set PWD to deal with home directories that are symlinks\n\n  * Back off overlay frame rate when all timing tests have fired (saves CPU)\n\n  * -v flag now makes server more verbose\n\n -- Keith Winstein <keithw@mit.edu>  Thu, 19 Apr 2012 02:44:48 -0400\n\nmosh (1.1.94c-1) unstable; urgency=low\n\n  * Further improved Debian rule to disable conflicting stack protector flags\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 17 Apr 2012 04:17:08 -0400\n\nmosh (1.1.94b-1) unstable; urgency=low\n\n  * Improved Debian rule to disable conflicting stack protector flags\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 17 Apr 2012 03:24:23 -0400\n\nmosh (1.1.94a-1) unstable; urgency=low\n\n  * Bump base version\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 17 Apr 2012 02:36:30 -0400\n\nmosh (1.1.94-2) unstable; urgency=low\n\n  * Disable conflicting Debian/Ubuntu stack protector flags\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 17 Apr 2012 02:15:41 -0400\n\nmosh (1.1.94-1) unstable; urgency=low\n\n  * Release pre-candidate for mosh 1.2\n\n  * Remove Boost as a dependency (Keegan McAllister)\n\n  * More friendly error messages in some common cases\n\n  * Now passes locale-related env vars over the connection\n\n  * Remove skalibs as a depenency on Debian/Ubuntu (Keegan McAllister)\n\n  * Fix startup script to no longer hang on some Macs (Jay Freeman)\n\n  * Fix argument parsing on FreeBSD (Daniel O'Connor)\n\n  * Use binary hardening flags where available (Keegan McAllister)\n\n  * Several improvements to terminal emulation correctness\n\n  * Now prints message of the day (motd)\n\n  * Allows core dumps of subsidiary applications if desired\n\n  * Cleaner execution of subsidiary shell (Jay Freeman)\n\n  * Fix leaking utmp entries (Ed Schouten)\n\n  * Add support for Cygwin (Joshua Pollack)\n\n  * Add support for RHEL/CentOS 5 (Reini Urban)\n\n  * Use less CPU when server's network is down\n\n  * Disable posterization of 256colors to 8 ANSI colors\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 16 Apr 2012 21:16:28 -0400\n\nmosh (1.1.3a-1) unstable; urgency=low\n\n  * Support 16-color escape sequences (Anders Kaseorg)\n\n  * Fix some wraparound glitches\n\n  * Fix escape sequences when server run on BSD and OS X (Peter Jeremy)\n\n  * FreeBSD support (Ben Kaduk)\n\n  * Fixes to build on iOS (Peter Iannucci)\n\n  * Add some tests (Keegan McAllister)\n\n  * Support OS X iTerm with lowercase \"utf-8\"\n\n  * Avoid unnecessary link with -lncurses when possible (Anders Kaseorg)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 11 Apr 2012 03:36:49 -0400\n\nmosh (1.1.3-1) unstable; urgency=low\n\n  * Version 1.1.3 released.\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 03 Apr 2012 12:33:46 -0400\n\nmosh (1.1.2c-1) unstable; urgency=low\n\n  * Fix bug causing occasional missing (or spurious) wraparound copy-and-paste\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 02 Apr 2012 19:08:19 -0400\n\nmosh (1.1.2b-1) unstable; urgency=low\n\n  * Close connection after four petabytes (per OCB recommendation)\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 31 Mar 2012 17:25:10 -0400\n\nmosh (1.1.2a-1) unstable; urgency=low\n\n  * Fixes to build on armel and armhf (Keegan McAllister)\n\n  * Fix to build on FreeBSD, with warning about broken tty multibyte delete (reported by Christoph Egger)\n\n  * Keep sane values in unused wx_xpixel/wx_ypixel values of struct winsize.\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 31 Mar 2012 15:51:31 -0400\n\nmosh (1.1.2-1) unstable; urgency=low\n\n  * Version 1.1.2 released.\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 28 Mar 2012 18:02:28 +0200\n\nmosh (1.1.1a-1) unstable; urgency=low\n\n  * Fixes to build on OS X 10.5 with older gcc and -lpoll (Quentin Smith)\n\n  * Add --with-utempter and --without-utempter per gentoo req (Michael Weber)\n\n  * configure now requires ncurses headers (and others) to be installed\n\n  * Consolidate locale routines to help Android port (Keegan McAllister)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 28 Mar 2012 15:16:51 +0200\n\nmosh (1.1.1-1) unstable; urgency=low\n\n  * Version 1.1.1 released.\n\n  * Fix spec file and build failure on Fedora.\n\n  * Print out error message properly on fatal_assert().\n\n  * Support for machines without posix_memalign().\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 27 Mar 2012 01:18:26 +0200\n\nmosh (1.1-1) unstable; urgency=low\n\n  * Version 1.1 released\n\n -- Keith Winstein <keithw@mit.edu>  Thu, 22 Mar 2012 18:40:59 -0400\n\nmosh (1.0.9d-1) unstable; urgency=low\n\n  * Fix bug preventing -p argument from working\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 21 Mar 2012 22:46:51 -0400\n\nmosh (1.0.9c-1) unstable; urgency=low\n\n  * Improved building on Mac OS X (Anders Kaseorg)\n\n  * PRNG (for chaff) now reads from /dev/urandom (Keegan McAllister)\n\n  * Startup script cleanups and fix shell quoting (Anders Kaseorg)\n\n  * Disables core dumps (containing session key) (Keegan McAllister)\n\n  * Improved terminal shutdown and other cosmetic fixes\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 21 Mar 2012 19:23:41 -0400\n\nmosh (1.0.9b-1) unstable; urgency=low\n\n  * More efficient repaint when scrolling top part of window\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 18 Mar 2012 16:37:30 -0400\n\nmosh (1.0.9a-1) unstable; urgency=low\n\n  * Add chaff to datagrams to frustrate statistical analysis of length\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 18 Mar 2012 14:00:11 -0400\n\nmosh (1.0.9-1) unstable; urgency=low\n\n  * Release candidate for version 1.1.\n\n  * Allows user to specify remote command to execute.\n\n  * Only advertises 256 colors when user's terminal has 256 colors.\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 18 Mar 2012 06:06:43 -0400\n\nmosh (1.0.2-1) unstable; urgency=low\n\n  * Version 1.02 released.\n\n  * Uses xterm-256color and supports 256-color escape sequences.\n\n  * Posterizes to 8 colors unless the user has a 256-color terminal.\n\n  * Handles terminals without BCE.\n\n  * Starts login shell.\n\n -- Keith Winstein <keithw@mit.edu>  Fri, 16 Mar 2012 17:57:45 -0400\n\nmosh (1.0.1-1) unstable; urgency=low\n\n  * Version 1.01 released.\n\n  * Roughly 40% less CPU usage.\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 14 Mar 2012 04:55:11 -0400\n\nmosh (1.0-1) unstable; urgency=low\n\n  * Version 1.0 released.\n\n  * mosh now supports --version option and prints no-warranty message.\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 12 Mar 2012 04:51:43 -0400\n\nmosh (0.98c-1) unstable; urgency=low\n\n  * Fix pointer comparison (Anders Kaseorg)\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 10 Mar 2012 16:50:35 -0500\n\nmosh (0.98b-1) unstable; urgency=low\n\n  * Update README.md\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 10 Mar 2012 14:05:12 -0500\n\nmosh (0.98a-1) unstable; urgency=low\n\n  * Small cleanup to generated terminal output when predictions active\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 10 Mar 2012 06:40:24 -0500\n\nmosh (0.98-1) unstable; urgency=low\n\n  * Version 0.98 released.\n\n  * Add option to select server-side UDP port.\n\n  * Restrict default UDP port range to 60000..61000.\n\n  * Use TERM / terminfo to decide when to send ECH sequence.\n\n  * Now works properly inside tmux.\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 10 Mar 2012 05:45:34 -0500\n\nmosh (0.97-1) unstable; urgency=low\n\n  * Merged Mac OS X port (thanks to Quentin Smith and Anders Kaseorg)\n\n  * Server will quit if no connection within first 60 seconds\n\n  * Startup script no longer requires threaded Perl\n\n  * Add --enable-compile-warnings=error to ./configure\n\n  * Fix some flicker issues with adaptive prediction mode\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 07 Mar 2012 04:17:19 -0500\n\nmosh (0.96a-3) unstable; urgency=low\n\n  * Update copyright file to correct pkg.m4 is GPL-2+ not GPL-2\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 03 Mar 2012 19:06:43 -0500\n\nmosh (0.96a-2) unstable; urgency=low\n\n  * Update copyright file to include public domain files and other licenses\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 03 Mar 2012 14:05:15 -0500\n\nmosh (0.96a-1) unstable; urgency=low\n\n  * Version 0.96a released.\n\n  * Fix build problem on i386\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 26 Feb 2012 13:49:44 -0500\n\nmosh (0.96-1) unstable; urgency=low\n\n  * Version 0.96 released.\n\n  * Compress all instructions with zlib\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 26 Feb 2012 04:40:46 -0500\n\nmosh (0.95-1) unstable; urgency=low\n\n  * Version 0.95 released.\n\n  * Make echo acknowledgment reliable to reduce spurious mispredict detections.\n\n  * Require two dropped heartbeats before showing blue bar of lost contact\n\n  * Print newline before MOSH CONNECT string to make more robust\n\n  * Disable ControlMaster in initial SSH connection so proxy is always used\n\n  * Make retransmissions occur at frame rate for limited time after loss\n\n -- Keith Winstein <keithw@mit.edu>  Sat, 25 Feb 2012 15:15:59 -0500\n\nmosh (0.94c-1) unstable; urgency=low\n\n  * Move to protobuf (from protobuf-lite) to make build on Ubuntu 10.04\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 20 Feb 2012 00:39:12 -0500\n\nmosh (0.94b-1) unstable; urgency=low\n\n  * Relax autoconf dependency\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 19 Feb 2012 22:55:53 -0500\n\nmosh (0.94a-1) unstable; urgency=low\n\n  * Relax debhelper dependency to make build on Ubuntu 10.04 LTS lucid\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 19 Feb 2012 22:03:03 -0500\n\nmosh (0.94-1) unstable; urgency=low\n\n  * Version 0.94 released.\n\n  * Make man pages.\n\n  * Replace C++11 features (lambda and auto) with boost equivalents\n\t        (Anders Kaseorg)\n\n  * Now builds with g++ or clang (with libstdc++)\n\n -- Keith Winstein <keithw@mit.edu>  Wed, 15 Feb 2012 13:56:11 -0500\n\nmosh (0.93-1) unstable; urgency=low\n\n  * Version 0.93 released.\n\n  * Make utmp entries.\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 13 Feb 2012 05:44:35 -0500\n\nmosh (0.92-1) unstable; urgency=low\n\n  * Version 0.92 released.\n\n  * Allows user to select prediction mode (always/never/adaptive)\n\n  * Fix bug in server startup on slow hosts\n\n  * Better prediction when deleting at line ending\n\n -- Keith Winstein <keithw@mit.edu>  Mon, 13 Feb 2012 03:53:31 -0500\n\nmosh (0.91a-0ubuntu1) oneiric; urgency=low\n\n  * Add missing dependencies to debian/control\n\n -- Keith Winstein <keithw@mit.edu>  Thu, 09 Feb 2012 09:34:18 -0500\n\nmosh (0.91-0ubuntu1) oneiric; urgency=low\n\n  * Version 0.91 released.\n\n  * Startup script support SSH options and aliases (Anders Kaseorg)\n\n  * End use of Path MTU discovery and allow IP fragmentation because,\n    e.g., Cisco VPN has MTU of 1200 and does not pass ICMP too-big.\n\n  * Better exception and error handling.\n\n  * Restrict predictive local echo display to long-delay links\n    (>60 ms RTT) or for temporarily after a \"glitch.\" Otherwise simply\n    show server echos, while maintaining prediction overlay in the\n    background in case it is needed.\n\n -- Keith Winstein <keithw@mit.edu>  Thu, 09 Feb 2012 03:05:45 -0500\n\nmosh (0.9b-0ubuntu1) oneiric; urgency=low\n\n  * Merged Anders Kaseorg fixes to build system\n\n -- Keith Winstein <keithw@mit.edu>  Tue, 07 Feb 2012 17:27:42 -0500\n\nmosh (0.9a-0ubuntu1) oneiric; urgency=low\n\n  * Edit README\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 05 Feb 2012 03:50:53 -0500\n\nmosh (0.9-0ubuntu1) oneiric; urgency=low\n\n  * Initial release\n\n -- Keith Winstein <keithw@mit.edu>  Sun, 05 Feb 2012 03:50:53 -0500\n"
  },
  {
    "path": "debian/control",
    "content": "Source: mosh\nSection: net\nPriority: optional\nMaintainer: Keith Winstein <keithw@mit.edu>\nUploaders: Alex Chernyakhovsky <achernya@debian.org>\nBuild-Depends: debhelper-compat (= 12), protobuf-compiler, libprotobuf-dev, pkg-config, libutempter-dev, zlib1g-dev, libncurses5-dev, libssl-dev, bash-completion, locales <!nocheck>, tmux <!nocheck>, less <!nocheck>\nRules-Requires-Root: binary-targets\nStandards-Version: 4.6.1\nHomepage: https://mosh.org\nVcs-Git: https://github.com/mobile-shell/mosh.git\nVcs-Browser: https://github.com/mobile-shell/mosh\n\nPackage: mosh\nArchitecture: any\nPre-Depends: dpkg (>= 1.15.7.2)\nDepends: ${shlibs:Depends}, ${misc:Depends}, openssh-client\nRecommends: libio-socket-ip-perl\nDescription: Mobile shell that supports roaming and intelligent local echo\n Mosh is a remote terminal application that supports:\n   - intermittent network connectivity,\n   - roaming to different IP address without dropping the connection, and\n   - intelligent local echo and line editing to reduce the effects\n     of \"network lag\" on high-latency connections.\n"
  },
  {
    "path": "debian/copyright",
    "content": "Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\nUpstream-Name: mosh\nSource: https://github.com/mobile-shell/mosh\n\nFiles: *\nCopyright: 2012 Keith Winstein <mosh-devel@mit.edu>\nLicense: GPL-3+ with OpenSSL exception\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU General Public License as published by\n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n .\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n GNU General Public License for more details.\n .\n You should have received a copy of the GNU General Public License\n along with this program.  If not, see <http://www.gnu.org/licenses/>.\n .\n In addition, as a special exception, the copyright holders give\n permission to link the code of portions of this program with the\n OpenSSL library under certain conditions as described in each\n individual source file, and distribute linked combinations including\n the two.\n .\n You must obey the GNU General Public License in all respects for all\n of the code used other than OpenSSL. If you modify file(s) with this\n exception, you may extend this exception to your version of the\n file(s), but you are not obligated to do so. If you do not wish to do\n so, delete this exception statement from your version. If you delete\n this exception statement from all source files in the program, then\n also delete it here.\n .\n On Debian systems, the complete text of the GNU General\n Public License version 3 can be found in \"/usr/share/common-licenses/GPL-3\".\n\nFiles: m4/pkg.m4\nCopyright: 2004 Scott James Remnant <scott@netsplit.com>\nLicense: GPL-2+\n This program is free software; you can redistribute it and/or modify\n it under the terms of the GNU General Public License as published by\n the Free Software Foundation; either version 2 of the License, or\n (at your option) any later version.\n .\n This program is distributed in the hope that it will be useful, but\n WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n General Public License for more details.\n .\n You should have received a copy of the GNU General Public License\n along with this program; if not, see <http://www.gnu.org/licenses/>.\n .\n As a special exception to the GNU General Public License, if you\n distribute this file as part of a program that contains a\n configuration script generated by Autoconf, you may include it under\n the same distribution terms that you use for the rest of that program.\n .\n On Debian systems, the complete text of the GNU General\n Public License version 2 can be found in \"/usr/share/common-licenses/GPL-2\"\n\nFiles: m4/ax_check_library.m4\nCopyright: 2010 Diego Elio Petteno` <flameeyes@gmail.com>\nLicense: GPL-3+ with Autoconf exception\n\nFiles: m4/ax_check_compile_flag.m4 m4/ax_check_link_flag.m4\nCopyright: 2008 Guido U. Draheim <guidod@gmx.de>\n           2011 Maarten Bosmans <mkbosmans@gmail.com>\nLicense: GPL-3+ with Autoconf exception\n\nLicense: GPL-3+ with Autoconf exception\n This program is free software: you can redistribute it and/or modify it\n under the terms of the GNU General Public License as published by the\n Free Software Foundation, either version 3 of the License, or (at your\n option) any later version.\n .\n This program is distributed in the hope that it will be useful, but\n WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n Public License for more details.\n .\n You should have received a copy of the GNU General Public License along\n with this program. If not, see <http://www.gnu.org/licenses/>.\n .\n As a special exception, the respective Autoconf Macro's copyright owner\n gives unlimited permission to copy, distribute and modify the configure\n scripts that are the output of Autoconf when processing the Macro. You\n need not follow the terms of the GNU General Public License when using\n or distributing such scripts, even though portions of the text of the\n Macro appear in them. The GNU General Public License (GPL) does govern\n all other use of the material that constitutes the Autoconf Macro.\n .\n This special exception to the GPL applies to versions of the Autoconf\n Macro released by the Autoconf Archive. When you make and distribute a\n modified version of the Autoconf Macro, you may extend this special\n exception to the GPL to apply to your modified version as well.\n .\n On Debian systems, the complete text of the GNU General\n Public License version 3 can be found in \"/usr/share/common-licenses/GPL-3\".\n\nFiles: src/crypto/ocb_internal.cc src/crypto/ocb_openssl.cc\nCopyright: 2011 Ted Krovetz\n           2022 Google LLC\nLicense: ISC\n Permission to use, copy, modify, and/or distribute this software for any\n purpose with or without fee is hereby granted, provided that the above\n copyright notice and this permission notice appear in all copies.\n .\n THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n .\n Phillip Rogaway holds patents relevant to OCB. See the following for\n his patent grant: http://www.cs.ucdavis.edu/~rogaway/ocb/grant.htm\n (also found in ocb-license.html in the Mosh source distribution).\n\nFiles: src/crypto/ae.h\nCopyright: none\nLicense: public-domain\n This file is in the public domain. It is provided \"as is\", without\n warranty of any kind. Use at your own risk.\n .\n Comments are welcome: Ted Krovetz <ted@krovetz>.\n"
  },
  {
    "path": "debian/docs",
    "content": "README.md\n"
  },
  {
    "path": "debian/mosh.maintscript",
    "content": "rm_conffile /etc/bash_completion.d/mosh 1.2.5~\n"
  },
  {
    "path": "debian/rules",
    "content": "#!/usr/bin/make -f\n# -*- makefile -*-\n# Sample debian/rules that uses debhelper.\n# This file was originally written by Joey Hess and Craig Small.\n# As a special exception, when this file is copied by dh-make into a\n# dh-make output file, you may use that output file without restriction.\n# This special exception was added by Craig Small in version 0.37 of dh-make.\n\n# Uncomment this to turn on verbose mode.\n#export DH_VERBOSE=1\n\n# Through Autoconf we set hardening flags that are actually more aggressive\n# than the Ubuntu defaults, but they conflict with same.\nexport DEB_BUILD_MAINT_OPTIONS = hardening=-stackprotector\n-include /usr/share/dpkg/buildflags.mk\n\n%:\n\tdh $@ --with autoreconf\n\noverride_dh_auto_configure:\n\tdh_auto_configure -- \\\n\t\t--disable-silent-rules \\\n\t\t--enable-ufw \\\n\t\t--enable-completion \\\n\t\t--enable-compile-warnings=error\n\noverride_dh_perl:\n\t# mosh only uses Perl modules in perl-base.\n\tdh_perl -d\n"
  },
  {
    "path": "debian/source/format",
    "content": "3.0 (quilt)\n"
  },
  {
    "path": "debian/watch",
    "content": "version=4\nopts=\"filenamemangle=s%(?:.*?)?v?(\\d[\\d.]*)\\.tar\\.gz%@PACKAGE@-$1.tar.gz%\" \\\n    https://github.com/mobile-shell/mosh/tags \\\n    .*/mosh-(\\d[\\d\\.]+)\\.tar\\.gz debian uupdate\n"
  },
  {
    "path": "fedora/mosh.spec",
    "content": "Name:\t\tmosh\nVersion:\t1.2.5\nRelease:\t1%{?dist}\nSummary:\tMobile shell that supports roaming and intelligent local echo\n\nLicense:\tGPLv3+\nGroup:\t\tApplications/Internet\nURL:\t\thttps://mosh.org/\nSource0:\thttps://github.com/downloads/keithw/mosh/mosh-%{version}.tar.gz\n\nBuildRequires:\tprotobuf-compiler\nBuildRequires:\tprotobuf-devel\nBuildRequires:\tlibutempter-devel\nBuildRequires:\tzlib-devel\nBuildRequires:\tncurses-devel\nBuildRequires:\topenssl-devel\nRequires:\topenssh-clients\nRequires:\topenssl\nRequires:\tperl-IO-Socket-IP\n\n%description\nMosh is a remote terminal application that supports:\n  - intermittent network connectivity,\n  - roaming to different IP address without dropping the connection, and\n  - intelligent local echo and line editing to reduce the effects\n    of \"network lag\" on high-latency connections.\n\n\n%prep\n%setup -q\n\n\n%build\n# Use upstream's more aggressive hardening instead of Fedora's defaults\nexport CFLAGS=\"-g -O2\" CXXFLAGS=\"-g -O2\"\n%configure --enable-compile-warnings=error\nmake %{?_smp_mflags}\n\n\n%install\nmake install DESTDIR=$RPM_BUILD_ROOT\n\n\n%files\n%doc README.md COPYING ChangeLog\n%{_bindir}/mosh\n%{_bindir}/mosh-client\n%{_bindir}/mosh-server\n%{_mandir}/man1/mosh.1.gz\n%{_mandir}/man1/mosh-client.1.gz\n%{_mandir}/man1/mosh-server.1.gz\n\n\n%changelog\n* Sun Jul 12 2015 John Hood <cgull@glup.org> - 1.2.5-1\n- Update to mosh 1.2.5\n\n* Fri Jun 26 2015 John Hood <cgull@glup.org> - 1.2.4.95rc2-1\n- Update to mosh 1.2.4.95rc2\n\n* Mon Jun 08 2015 John Hood <cgull@glup.org> - 1.2.4.95rc1-1\n- Update to mosh 1.2.4.95rc1\n\n* Wed Mar 27 2013 Alexander Chernyakhovsky <achernya@mit.edu> - 1.2.4-1\n- Update to mosh 1.2.4\n\n* Sun Mar 10 2013 Alexander Chernyakhovsky <achernya@mit.edu> - 1.2.3-3\n- Rebuilt for Protobuf API change from 2.4.1 to 2.5.0\n\n* Thu Feb 14 2013 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 1.2.3-2\n- Rebuilt for https://fedoraproject.org/wiki/Fedora_19_Mass_Rebuild\n\n* Fri Oct 19 2012 Alexander Chernyakhovsky <achernya@mit.edu> - 1.2.3-1\n- Update to mosh 1.2.3\n\n* Fri Jul 20 2012 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 1.2.2-2\n- Rebuilt for https://fedoraproject.org/wiki/Fedora_18_Mass_Rebuild\n\n* Wed Jun 13 2012 Alexander Chernyakhovsky <achernya@mit.edu> - 1.2.2-1\n- Update to mosh 1.2.2\n\n* Sat Apr 28 2012 Alexander Chernyakhovsky <achernya@mit.edu> - 1.2-2\n- Add -g and -O2 CFLAGS\n\n* Fri Apr 27 2012 Alexander Chernyakhovsky <achernya@mit.edu> - 1.2-1\n- Update to mosh 1.2.\n\n* Mon Mar 26 2012 Alexander Chernyakhovsky <achernya@mit.edu> - 1.1.1-1\n- Update to mosh 1.1.1.\n\n* Wed Mar 21 2012 Alexander Chernyakhovsky <achernya@mit.edu> - 1.1-1\n- Initial packaging for mosh.\n"
  },
  {
    "path": "m4/ax_ac_append_to_file.m4",
    "content": "# ===========================================================================\n#   https://www.gnu.org/software/autoconf-archive/ax_ac_append_to_file.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_AC_APPEND_TO_FILE([FILE],[DATA])\n#\n# DESCRIPTION\n#\n#   Appends the specified data to the specified Autoconf is run. If you want\n#   to append to a file when configure is run use AX_APPEND_TO_FILE instead.\n#\n# LICENSE\n#\n#   Copyright (c) 2009 Allan Caffee <allan.caffee@gmail.com>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 10\n\nAC_DEFUN([AX_AC_APPEND_TO_FILE],[\nAC_REQUIRE([AX_FILE_ESCAPES])\nm4_esyscmd(\nAX_FILE_ESCAPES\n[\nprintf \"%s\" \"$2\" >> \"$1\"\n])\n])\n"
  },
  {
    "path": "m4/ax_ac_print_to_file.m4",
    "content": "# ===========================================================================\n#   https://www.gnu.org/software/autoconf-archive/ax_ac_print_to_file.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_AC_PRINT_TO_FILE([FILE],[DATA])\n#\n# DESCRIPTION\n#\n#   Writes the specified data to the specified file when Autoconf is run. If\n#   you want to print to a file when configure is run use AX_PRINT_TO_FILE\n#   instead.\n#\n# LICENSE\n#\n#   Copyright (c) 2009 Allan Caffee <allan.caffee@gmail.com>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 10\n\nAC_DEFUN([AX_AC_PRINT_TO_FILE],[\nm4_esyscmd(\nAC_REQUIRE([AX_FILE_ESCAPES])\n[\nprintf \"%s\" \"$2\" > \"$1\"\n])\n])\n"
  },
  {
    "path": "m4/ax_add_am_macro_static.m4",
    "content": "# ===========================================================================\n#  https://www.gnu.org/software/autoconf-archive/ax_add_am_macro_static.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_ADD_AM_MACRO_STATIC([RULE])\n#\n# DESCRIPTION\n#\n#   Adds the specified rule to $AMINCLUDE.\n#\n# LICENSE\n#\n#   Copyright (c) 2009 Tom Howard <tomhoward@users.sf.net>\n#   Copyright (c) 2009 Allan Caffee <allan.caffee@gmail.com>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 8\n\nAC_DEFUN([AX_ADD_AM_MACRO_STATIC],[\n  AC_REQUIRE([AX_AM_MACROS_STATIC])\n  AX_AC_APPEND_TO_FILE(AMINCLUDE_STATIC,[$1])\n])\n"
  },
  {
    "path": "m4/ax_am_macros_static.m4",
    "content": "# ===========================================================================\n#   https://www.gnu.org/software/autoconf-archive/ax_am_macros_static.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_AM_MACROS_STATIC\n#\n# DESCRIPTION\n#\n#   Adds support for macros that create Automake rules. You must manually\n#   add the following line\n#\n#     include $(top_srcdir)/aminclude_static.am\n#\n#   to your Makefile.am files.\n#\n# LICENSE\n#\n#   Copyright (c) 2009 Tom Howard <tomhoward@users.sf.net>\n#   Copyright (c) 2009 Allan Caffee <allan.caffee@gmail.com>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 11\n\nAC_DEFUN([AMINCLUDE_STATIC],[aminclude_static.am])\n\nAC_DEFUN([AX_AM_MACROS_STATIC],\n[\nAX_AC_PRINT_TO_FILE(AMINCLUDE_STATIC,[\n# ]AMINCLUDE_STATIC[ generated automatically by Autoconf\n# from AX_AM_MACROS_STATIC on ]m4_esyscmd([LC_ALL=C date])[\n])\n])\n"
  },
  {
    "path": "m4/ax_check_compile_flag.m4",
    "content": "# ===========================================================================\n#   http://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS])\n#\n# DESCRIPTION\n#\n#   Check whether the given FLAG works with the current language's compiler\n#   or gives an error.  (Warnings, however, are ignored)\n#\n#   ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on\n#   success/failure.\n#\n#   If EXTRA-FLAGS is defined, it is added to the current language's default\n#   flags (e.g. CFLAGS) when the check is done.  The check is thus made with\n#   the flags: \"CFLAGS EXTRA-FLAGS FLAG\".  This can for example be used to\n#   force the compiler to issue an error when a bad flag is given.\n#\n#   NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this\n#   macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>\n#   Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>\n#\n#   This program is free software: you can redistribute it and/or modify it\n#   under the terms of the GNU General Public License as published by the\n#   Free Software Foundation, either version 3 of the License, or (at your\n#   option) any later version.\n#\n#   This program is distributed in the hope that it will be useful, but\n#   WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n#   Public License for more details.\n#\n#   You should have received a copy of the GNU General Public License along\n#   with this program. If not, see <http://www.gnu.org/licenses/>.\n#\n#   As a special exception, the respective Autoconf Macro's copyright owner\n#   gives unlimited permission to copy, distribute and modify the configure\n#   scripts that are the output of Autoconf when processing the Macro. You\n#   need not follow the terms of the GNU General Public License when using\n#   or distributing such scripts, even though portions of the text of the\n#   Macro appear in them. The GNU General Public License (GPL) does govern\n#   all other use of the material that constitutes the Autoconf Macro.\n#\n#   This special exception to the GPL applies to versions of the Autoconf\n#   Macro released by the Autoconf Archive. When you make and distribute a\n#   modified version of the Autoconf Macro, you may extend this special\n#   exception to the GPL to apply to your modified version as well.\n\n#serial 2\n\nAC_DEFUN([AX_CHECK_COMPILE_FLAG],\n[AC_PREREQ(2.59)dnl for _AC_LANG_PREFIX\nAS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl\nAC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [\n  ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS\n  _AC_LANG_PREFIX[]FLAGS=\"$[]_AC_LANG_PREFIX[]FLAGS $4 $1\"\n  AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],\n    [AS_VAR_SET(CACHEVAR,[yes])],\n    [AS_VAR_SET(CACHEVAR,[no])])\n  _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])\nAS_IF([test x\"AS_VAR_GET(CACHEVAR)\" = xyes],\n  [m4_default([$2], :)],\n  [m4_default([$3], :)])\nAS_VAR_POPDEF([CACHEVAR])dnl\n])dnl AX_CHECK_COMPILE_FLAGS\n"
  },
  {
    "path": "m4/ax_check_gnu_make.m4",
    "content": "# ===========================================================================\n#    https://www.gnu.org/software/autoconf-archive/ax_check_gnu_make.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CHECK_GNU_MAKE([run-if-true],[run-if-false])\n#\n# DESCRIPTION\n#\n#   This macro searches for a GNU version of make. If a match is found:\n#\n#     * The makefile variable `ifGNUmake' is set to the empty string, otherwise\n#       it is set to \"#\". This is useful for including a special features in a\n#       Makefile, which cannot be handled by other versions of make.\n#     * The makefile variable `ifnGNUmake' is set to #, otherwise\n#       it is set to the empty string. This is useful for including a special\n#       features in a Makefile, which can be handled\n#       by other versions of make or to specify else like clause.\n#     * The variable `_cv_gnu_make_command` is set to the command to invoke\n#       GNU make if it exists, the empty string otherwise.\n#     * The variable `ax_cv_gnu_make_command` is set to the command to invoke\n#       GNU make by copying `_cv_gnu_make_command`, otherwise it is unset.\n#     * If GNU Make is found, its version is extracted from the output of\n#       `make --version` as the last field of a record of space-separated\n#       columns and saved into the variable `ax_check_gnu_make_version`.\n#     * Additionally if GNU Make is found, run shell code run-if-true\n#       else run shell code run-if-false.\n#\n#   Here is an example of its use:\n#\n#   Makefile.in might contain:\n#\n#     # A failsafe way of putting a dependency rule into a makefile\n#     $(DEPEND):\n#             $(CC) -MM $(srcdir)/*.c > $(DEPEND)\n#\n#     @ifGNUmake@ ifeq ($(DEPEND),$(wildcard $(DEPEND)))\n#     @ifGNUmake@ include $(DEPEND)\n#     @ifGNUmake@ else\n#     fallback code\n#     @ifGNUmake@ endif\n#\n#   Then configure.in would normally contain:\n#\n#     AX_CHECK_GNU_MAKE()\n#     AC_OUTPUT(Makefile)\n#\n#   Then perhaps to cause gnu make to override any other make, we could do\n#   something like this (note that GNU make always looks for GNUmakefile\n#   first):\n#\n#     if  ! test x$_cv_gnu_make_command = x ; then\n#             mv Makefile GNUmakefile\n#             echo .DEFAULT: > Makefile ;\n#             echo \\  $_cv_gnu_make_command \\$@ >> Makefile;\n#     fi\n#\n#   Then, if any (well almost any) other make is called, and GNU make also\n#   exists, then the other make wraps the GNU make.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 John Darrington <j.darrington@elvis.murdoch.edu.au>\n#   Copyright (c) 2015 Enrico M. Crisostomo <enrico.m.crisostomo@gmail.com>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 12\n\nAC_DEFUN([AX_CHECK_GNU_MAKE],dnl\n  [AC_PROG_AWK\n  AC_CACHE_CHECK([for GNU make],[_cv_gnu_make_command],[dnl\n    _cv_gnu_make_command=\"\" ;\ndnl Search all the common names for GNU make\n    for a in \"$MAKE\" make gmake gnumake ; do\n      if test -z \"$a\" ; then continue ; fi ;\n      if \"$a\" --version 2> /dev/null | grep GNU 2>&1 > /dev/null ; then\n        _cv_gnu_make_command=$a ;\n        AX_CHECK_GNU_MAKE_HEADLINE=$(\"$a\" --version 2> /dev/null | grep \"GNU Make\")\n        ax_check_gnu_make_version=$(echo ${AX_CHECK_GNU_MAKE_HEADLINE} | ${AWK} -F \" \" '{ print $(NF); }')\n        break ;\n      fi\n    done ;])\ndnl If there was a GNU version, then set @ifGNUmake@ to the empty string, '#' otherwise\n  AS_VAR_IF([_cv_gnu_make_command], [\"\"], [AS_VAR_SET([ifGNUmake], [\"#\"])],   [AS_VAR_SET([ifGNUmake], [\"\"])])\n  AS_VAR_IF([_cv_gnu_make_command], [\"\"], [AS_VAR_SET([ifnGNUmake], [\"\"])],   [AS_VAR_SET([ifnGNUmake], [\"#\"])])\n  AS_VAR_IF([_cv_gnu_make_command], [\"\"], [AS_UNSET(ax_cv_gnu_make_command)], [AS_VAR_SET([ax_cv_gnu_make_command], [${_cv_gnu_make_command}])])\n  AS_VAR_IF([_cv_gnu_make_command], [\"\"],[$2],[$1])\n  AC_SUBST([ifGNUmake])\n  AC_SUBST([ifnGNUmake])\n])\n"
  },
  {
    "path": "m4/ax_check_library.m4",
    "content": "# ===========================================================================\n#     http://www.gnu.org/software/autoconf-archive/ax_check_library.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CHECK_LIBRARY(VARIABLE-PREFIX, HEADER-FILE, LIBRARY-FILE,\n#                    [ACTION-IF-FOUND], [ACTION-IF-NOT_FOUND])\n#\n# DESCRIPTION\n#\n#   Provides a generic test for a given library, similar in concept to the\n#   PKG_CHECK_MODULES macro used by pkg-config.\n#\n#   Most simplest libraries can be checked against simply through the\n#   presence of a header file and a library to link to. This macro allows to\n#   wrap around the test so that it doesn't have to be recreated each time.\n#\n#   Rather than define --with-$LIBRARY arguments, it uses variables in the\n#   same way that PKG_CHECK_MODULES does. It doesn't, though, use the same\n#   names, since you shouldn't provide a value for LIBS or CFLAGS but rather\n#   for LDFLAGS and CPPFLAGS, to tell the linker and compiler where to find\n#   libraries and headers respectively.\n#\n#   If the library is find, HAVE_PREFIX is defined, and in all cases\n#   PREFIX_LDFLAGS and PREFIX_CPPFLAGS are substituted.\n#\n#   Example:\n#\n#     AX_CHECK_LIBRARY([LIBEVENT], [event.h], [event], [],\n#                      [AC_MSG_ERROR([Unable to find libevent])])\n#\n# LICENSE\n#\n#   Copyright (c) 2010 Diego Elio Petteno` <flameeyes@gmail.com>\n#\n#   This program is free software: you can redistribute it and/or modify it\n#   under the terms of the GNU General Public License as published by the\n#   Free Software Foundation, either version 3 of the License, or (at your\n#   option) any later version.\n#\n#   This program is distributed in the hope that it will be useful, but\n#   WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n#   Public License for more details.\n#\n#   You should have received a copy of the GNU General Public License along\n#   with this program. If not, see <http://www.gnu.org/licenses/>.\n#\n#   As a special exception, the respective Autoconf Macro's copyright owner\n#   gives unlimited permission to copy, distribute and modify the configure\n#   scripts that are the output of Autoconf when processing the Macro. You\n#   need not follow the terms of the GNU General Public License when using\n#   or distributing such scripts, even though portions of the text of the\n#   Macro appear in them. The GNU General Public License (GPL) does govern\n#   all other use of the material that constitutes the Autoconf Macro.\n#\n#   This special exception to the GPL applies to versions of the Autoconf\n#   Macro released by the Autoconf Archive. When you make and distribute a\n#   modified version of the Autoconf Macro, you may extend this special\n#   exception to the GPL to apply to your modified version as well.\n\n#serial 4\n\nAC_DEFUN([AX_CHECK_LIBRARY], [\n  AC_ARG_VAR($1[_CPPFLAGS], [C preprocessor flags for ]$1[ headers])\n  AC_ARG_VAR($1[_LDFLAGS], [linker flags for ]$1[ libraries])\n\n  AC_CACHE_VAL(AS_TR_SH([ax_cv_have_]$1),\n    [save_CPPFLAGS=\"$CPPFLAGS\"\n     save_LDFLAGS=\"$LDFLAGS\"\n     save_LIBS=\"$LIBS\"\n\n     AS_IF([test \"x$]$1[_CPPFLAGS\" != \"x\"],\n       [CPPFLAGS=\"$CPPFLAGS $]$1[_CPPFLAGS\"])\n\n     AS_IF([test \"x$]$1[_LDFLAGS\" != \"x\"],\n       [LDFLAGS=\"$LDFLAGS $]$1[_LDFLAGS\"])\n\n     AC_CHECK_HEADER($2, [\n       AC_CHECK_LIB($3, [main],\n         [AS_TR_SH([ax_cv_have_]$1)=yes],\n         [AS_TR_SH([ax_cv_have_]$1)=no])\n     ], [AS_TR_SH([ax_cv_have_]$1)=no])\n\n     CPPFLAGS=\"$save_CPPFLAGS\"\n     LDFLAGS=\"$save_LDFLAGS\"\n     LIBS=\"$save_LIBS\"\n    ])\n\n  AS_IF([test \"$]AS_TR_SH([ax_cv_have_]$1)[\" = \"yes\"],\n    AC_DEFINE([HAVE_]$1, [1], [Define to 1 if ]$1[ is found])\n    [$4],\n    [$5])\n])\n"
  },
  {
    "path": "m4/ax_check_link_flag.m4",
    "content": "# ===========================================================================\n#    http://www.gnu.org/software/autoconf-archive/ax_check_link_flag.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CHECK_LINK_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS])\n#\n# DESCRIPTION\n#\n#   Check whether the given FLAG works with the linker or gives an error.\n#   (Warnings, however, are ignored)\n#\n#   ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on\n#   success/failure.\n#\n#   If EXTRA-FLAGS is defined, it is added to the linker's default flags\n#   when the check is done.  The check is thus made with the flags: \"LDFLAGS\n#   EXTRA-FLAGS FLAG\".  This can for example be used to force the linker to\n#   issue an error when a bad flag is given.\n#\n#   NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this\n#   macro in sync with AX_CHECK_{PREPROC,COMPILE}_FLAG.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>\n#   Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>\n#\n#   This program is free software: you can redistribute it and/or modify it\n#   under the terms of the GNU General Public License as published by the\n#   Free Software Foundation, either version 3 of the License, or (at your\n#   option) any later version.\n#\n#   This program is distributed in the hope that it will be useful, but\n#   WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n#   Public License for more details.\n#\n#   You should have received a copy of the GNU General Public License along\n#   with this program. If not, see <http://www.gnu.org/licenses/>.\n#\n#   As a special exception, the respective Autoconf Macro's copyright owner\n#   gives unlimited permission to copy, distribute and modify the configure\n#   scripts that are the output of Autoconf when processing the Macro. You\n#   need not follow the terms of the GNU General Public License when using\n#   or distributing such scripts, even though portions of the text of the\n#   Macro appear in them. The GNU General Public License (GPL) does govern\n#   all other use of the material that constitutes the Autoconf Macro.\n#\n#   This special exception to the GPL applies to versions of the Autoconf\n#   Macro released by the Autoconf Archive. When you make and distribute a\n#   modified version of the Autoconf Macro, you may extend this special\n#   exception to the GPL to apply to your modified version as well.\n\n#serial 2\n\nAC_DEFUN([AX_CHECK_LINK_FLAG],\n[AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_ldflags_$4_$1])dnl\nAC_CACHE_CHECK([whether the linker accepts $1], CACHEVAR, [\n  ax_check_save_flags=$LDFLAGS\n  LDFLAGS=\"$LDFLAGS $4 $1\"\n  AC_LINK_IFELSE([AC_LANG_PROGRAM()],\n    [AS_VAR_SET(CACHEVAR,[yes])],\n    [AS_VAR_SET(CACHEVAR,[no])])\n  LDFLAGS=$ax_check_save_flags])\nAS_IF([test x\"AS_VAR_GET(CACHEVAR)\" = xyes],\n  [m4_default([$2], :)],\n  [m4_default([$3], :)])\nAS_VAR_POPDEF([CACHEVAR])dnl\n])dnl AX_CHECK_LINK_FLAGS\n"
  },
  {
    "path": "m4/ax_code_coverage.m4",
    "content": "# ===========================================================================\n#     https://www.gnu.org/software/autoconf-archive/ax_code_coverage.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CODE_COVERAGE()\n#\n# DESCRIPTION\n#\n#   Defines CODE_COVERAGE_CPPFLAGS, CODE_COVERAGE_CFLAGS,\n#   CODE_COVERAGE_CXXFLAGS and CODE_COVERAGE_LIBS which should be included\n#   in the CPPFLAGS, CFLAGS CXXFLAGS and LIBS/LIBADD variables of every\n#   build target (program or library) which should be built with code\n#   coverage support. Also add rules using AX_ADD_AM_MACRO_STATIC; and\n#   $enable_code_coverage which can be used in subsequent configure output.\n#   CODE_COVERAGE_ENABLED is defined and substituted, and corresponds to the\n#   value of the --enable-code-coverage option, which defaults to being\n#   disabled.\n#\n#   Test also for gcov program and create GCOV variable that could be\n#   substituted.\n#\n#   Note that all optimization flags in CFLAGS must be disabled when code\n#   coverage is enabled.\n#\n#   Usage example:\n#\n#   configure.ac:\n#\n#     AX_CODE_COVERAGE\n#\n#   Makefile.am:\n#\n#     include $(top_srcdir)/aminclude_static.am\n#\n#     my_program_LIBS = ... $(CODE_COVERAGE_LIBS) ...\n#     my_program_CPPFLAGS = ... $(CODE_COVERAGE_CPPFLAGS) ...\n#     my_program_CFLAGS = ... $(CODE_COVERAGE_CFLAGS) ...\n#     my_program_CXXFLAGS = ... $(CODE_COVERAGE_CXXFLAGS) ...\n#\n#     clean-local: code-coverage-clean\n#     distclean-local: code-coverage-dist-clean\n#\n#   This results in a \"check-code-coverage\" rule being added to any\n#   Makefile.am which do \"include $(top_srcdir)/aminclude_static.am\"\n#   (assuming the module has been configured with --enable-code-coverage).\n#   Running `make check-code-coverage` in that directory will run the\n#   module's test suite (`make check`) and build a code coverage report\n#   detailing the code which was touched, then print the URI for the report.\n#\n#   This code was derived from Makefile.decl in GLib, originally licensed\n#   under LGPLv2.1+.\n#\n# LICENSE\n#\n#   Copyright (c) 2012, 2016 Philip Withnall\n#   Copyright (c) 2012 Xan Lopez\n#   Copyright (c) 2012 Christian Persch\n#   Copyright (c) 2012 Paolo Borelli\n#   Copyright (c) 2012 Dan Winship\n#   Copyright (c) 2015,2018 Bastien ROUCARIES\n#\n#   This library is free software; you can redistribute it and/or modify it\n#   under the terms of the GNU Lesser General Public License as published by\n#   the Free Software Foundation; either version 2.1 of the License, or (at\n#   your option) any later version.\n#\n#   This library is distributed in the hope that it will be useful, but\n#   WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser\n#   General Public License for more details.\n#\n#   You should have received a copy of the GNU Lesser General Public License\n#   along with this program. If not, see <https://www.gnu.org/licenses/>.\n\n#serial 34\n\nm4_define(_AX_CODE_COVERAGE_RULES,[\nAX_ADD_AM_MACRO_STATIC([\n# Code coverage\n#\n# Optional:\n#  - CODE_COVERAGE_DIRECTORY: Top-level directory for code coverage reporting.\n#    Multiple directories may be specified, separated by whitespace.\n#    (Default: \\$(top_builddir))\n#  - CODE_COVERAGE_OUTPUT_FILE: Filename and path for the .info file generated\n#    by lcov for code coverage. (Default:\n#    \\$(PACKAGE_NAME)-\\$(PACKAGE_VERSION)-coverage.info)\n#  - CODE_COVERAGE_OUTPUT_DIRECTORY: Directory for generated code coverage\n#    reports to be created. (Default:\n#    \\$(PACKAGE_NAME)-\\$(PACKAGE_VERSION)-coverage)\n#  - CODE_COVERAGE_BRANCH_COVERAGE: Set to 1 to enforce branch coverage,\n#    set to 0 to disable it and leave empty to stay with the default.\n#    (Default: empty)\n#  - CODE_COVERAGE_LCOV_SHOPTS_DEFAULT: Extra options shared between both lcov\n#    instances. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)\n#  - CODE_COVERAGE_LCOV_SHOPTS: Extra options to shared between both lcov\n#    instances. (Default: $CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)\n#  - CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH: --gcov-tool pathtogcov\n#  - CODE_COVERAGE_LCOV_OPTIONS_DEFAULT: Extra options to pass to the\n#    collecting lcov instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)\n#  - CODE_COVERAGE_LCOV_OPTIONS: Extra options to pass to the collecting lcov\n#    instance. (Default: $CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)\n#  - CODE_COVERAGE_LCOV_RMOPTS_DEFAULT: Extra options to pass to the filtering\n#    lcov instance. (Default: empty)\n#  - CODE_COVERAGE_LCOV_RMOPTS: Extra options to pass to the filtering lcov\n#    instance. (Default: $CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)\n#  - CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT: Extra options to pass to the\n#    genhtml instance. (Default: based on $CODE_COVERAGE_BRANCH_COVERAGE)\n#  - CODE_COVERAGE_GENHTML_OPTIONS: Extra options to pass to the genhtml\n#    instance. (Default: $CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)\n#  - CODE_COVERAGE_IGNORE_PATTERN: Extra glob pattern of files to ignore\n#\n# The generated report will be titled using the \\$(PACKAGE_NAME) and\n# \\$(PACKAGE_VERSION). In order to add the current git hash to the title,\n# use the git-version-gen script, available online.\n# Optional variables\n# run only on top dir\nif CODE_COVERAGE_ENABLED\n ifeq (\\$(abs_builddir), \\$(abs_top_builddir))\nCODE_COVERAGE_DIRECTORY ?= \\$(top_builddir)\nCODE_COVERAGE_OUTPUT_FILE ?= \\$(PACKAGE_NAME)-\\$(PACKAGE_VERSION)-coverage.info\nCODE_COVERAGE_OUTPUT_DIRECTORY ?= \\$(PACKAGE_NAME)-\\$(PACKAGE_VERSION)-coverage\n\nCODE_COVERAGE_BRANCH_COVERAGE ?=\nCODE_COVERAGE_LCOV_SHOPTS_DEFAULT ?= \\$(if \\$(CODE_COVERAGE_BRANCH_COVERAGE),\\\n--rc lcov_branch_coverage=\\$(CODE_COVERAGE_BRANCH_COVERAGE))\nCODE_COVERAGE_LCOV_SHOPTS ?= \\$(CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)\nCODE_COVERAGE_LCOV_OPTIONS_GCOVPATH ?= --gcov-tool \\\"\\$(GCOV)\\\"\nCODE_COVERAGE_LCOV_OPTIONS_DEFAULT ?= \\$(CODE_COVERAGE_LCOV_OPTIONS_GCOVPATH)\nCODE_COVERAGE_LCOV_OPTIONS ?= \\$(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)\nCODE_COVERAGE_LCOV_RMOPTS_DEFAULT ?=\nCODE_COVERAGE_LCOV_RMOPTS ?= \\$(CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)\nCODE_COVERAGE_GENHTML_OPTIONS_DEFAULT ?=\\\n\\$(if \\$(CODE_COVERAGE_BRANCH_COVERAGE),\\\n--rc genhtml_branch_coverage=\\$(CODE_COVERAGE_BRANCH_COVERAGE))\nCODE_COVERAGE_GENHTML_OPTIONS ?= \\$(CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)\nCODE_COVERAGE_IGNORE_PATTERN ?=\n\nGITIGNOREFILES := \\$(GITIGNOREFILES) \\$(CODE_COVERAGE_OUTPUT_FILE) \\$(CODE_COVERAGE_OUTPUT_DIRECTORY)\ncode_coverage_v_lcov_cap = \\$(code_coverage_v_lcov_cap_\\$(V))\ncode_coverage_v_lcov_cap_ = \\$(code_coverage_v_lcov_cap_\\$(AM_DEFAULT_VERBOSITY))\ncode_coverage_v_lcov_cap_0 = @echo \\\"  LCOV   --capture\\\" \\$(CODE_COVERAGE_OUTPUT_FILE);\ncode_coverage_v_lcov_ign = \\$(code_coverage_v_lcov_ign_\\$(V))\ncode_coverage_v_lcov_ign_ = \\$(code_coverage_v_lcov_ign_\\$(AM_DEFAULT_VERBOSITY))\ncode_coverage_v_lcov_ign_0 = @echo \\\"  LCOV   --remove /tmp/*\\\" \\$(CODE_COVERAGE_IGNORE_PATTERN);\ncode_coverage_v_genhtml = \\$(code_coverage_v_genhtml_\\$(V))\ncode_coverage_v_genhtml_ = \\$(code_coverage_v_genhtml_\\$(AM_DEFAULT_VERBOSITY))\ncode_coverage_v_genhtml_0 = @echo \\\"  GEN   \\\" \\\"\\$(CODE_COVERAGE_OUTPUT_DIRECTORY)\\\";\ncode_coverage_quiet = \\$(code_coverage_quiet_\\$(V))\ncode_coverage_quiet_ = \\$(code_coverage_quiet_\\$(AM_DEFAULT_VERBOSITY))\ncode_coverage_quiet_0 = --quiet\n\n# sanitizes the test-name: replaces with underscores: dashes and dots\ncode_coverage_sanitize = \\$(subst -,_,\\$(subst .,_,\\$(1)))\n\n# Use recursive makes in order to ignore errors during check\ncheck-code-coverage:\n\t-\\$(AM_V_at)\\$(MAKE) \\$(AM_MAKEFLAGS) -k check\n\t\\$(AM_V_at)\\$(MAKE) \\$(AM_MAKEFLAGS) code-coverage-capture\n\n# Capture code coverage data\ncode-coverage-capture: code-coverage-capture-hook\n\t\\$(code_coverage_v_lcov_cap)\\$(LCOV) \\$(code_coverage_quiet) \\$(addprefix --directory ,\\$(CODE_COVERAGE_DIRECTORY)) --capture --output-file \\\"\\$(CODE_COVERAGE_OUTPUT_FILE).tmp\\\" --test-name \\\"\\$(call code_coverage_sanitize,\\$(PACKAGE_NAME)-\\$(PACKAGE_VERSION))\\\" --no-checksum --compat-libtool \\$(CODE_COVERAGE_LCOV_SHOPTS) \\$(CODE_COVERAGE_LCOV_OPTIONS)\n\t\\$(code_coverage_v_lcov_ign)\\$(LCOV) \\$(code_coverage_quiet) \\$(addprefix --directory ,\\$(CODE_COVERAGE_DIRECTORY)) --remove \\\"\\$(CODE_COVERAGE_OUTPUT_FILE).tmp\\\" \\\"/tmp/*\\\" \\$(CODE_COVERAGE_IGNORE_PATTERN) --output-file \\\"\\$(CODE_COVERAGE_OUTPUT_FILE)\\\" \\$(CODE_COVERAGE_LCOV_SHOPTS) \\$(CODE_COVERAGE_LCOV_RMOPTS)\n\t-@rm -f \\\"\\$(CODE_COVERAGE_OUTPUT_FILE).tmp\\\"\n\t\\$(code_coverage_v_genhtml)LANG=C \\$(GENHTML) \\$(code_coverage_quiet) \\$(addprefix --prefix ,\\$(CODE_COVERAGE_DIRECTORY)) --output-directory \\\"\\$(CODE_COVERAGE_OUTPUT_DIRECTORY)\\\" --title \\\"\\$(PACKAGE_NAME)-\\$(PACKAGE_VERSION) Code Coverage\\\" --legend --show-details \\\"\\$(CODE_COVERAGE_OUTPUT_FILE)\\\" \\$(CODE_COVERAGE_GENHTML_OPTIONS)\n\t@echo \\\"file://\\$(abs_builddir)/\\$(CODE_COVERAGE_OUTPUT_DIRECTORY)/index.html\\\"\n\ncode-coverage-clean:\n\t-\\$(LCOV) --directory \\$(top_builddir) -z\n\t-rm -rf \\\"\\$(CODE_COVERAGE_OUTPUT_FILE)\\\" \\\"\\$(CODE_COVERAGE_OUTPUT_FILE).tmp\\\" \\\"\\$(CODE_COVERAGE_OUTPUT_DIRECTORY)\\\"\n\t-find . \\\\( -name \\\"*.gcda\\\" -o -name \\\"*.gcno\\\" -o -name \\\"*.gcov\\\" \\\\) -delete\n\ncode-coverage-dist-clean:\n\nA][M_DISTCHECK_CONFIGURE_FLAGS := \\$(A][M_DISTCHECK_CONFIGURE_FLAGS) --disable-code-coverage\n else # ifneq (\\$(abs_builddir), \\$(abs_top_builddir))\ncheck-code-coverage:\n\ncode-coverage-capture: code-coverage-capture-hook\n\ncode-coverage-clean:\n\ncode-coverage-dist-clean:\n endif # ifeq (\\$(abs_builddir), \\$(abs_top_builddir))\nelse #! CODE_COVERAGE_ENABLED\n# Use recursive makes in order to ignore errors during check\ncheck-code-coverage:\n\t@echo \\\"Need to reconfigure with --enable-code-coverage\\\"\n# Capture code coverage data\ncode-coverage-capture: code-coverage-capture-hook\n\t@echo \\\"Need to reconfigure with --enable-code-coverage\\\"\n\ncode-coverage-clean:\n\ncode-coverage-dist-clean:\n\nendif #CODE_COVERAGE_ENABLED\n# Hook rule executed before code-coverage-capture, overridable by the user\ncode-coverage-capture-hook:\n\n.PHONY: check-code-coverage code-coverage-capture code-coverage-dist-clean code-coverage-clean code-coverage-capture-hook\n])\n])\n\nAC_DEFUN([_AX_CODE_COVERAGE_ENABLED],[\n\tAX_CHECK_GNU_MAKE([],[AC_MSG_ERROR([not using GNU make that is needed for coverage])])\n\tAC_REQUIRE([AX_ADD_AM_MACRO_STATIC])\n\t# check for gcov\n\tAC_CHECK_TOOL([GCOV],\n\t\t  [$_AX_CODE_COVERAGE_GCOV_PROG_WITH],\n\t\t  [:])\n\tAS_IF([test \"X$GCOV\" = \"X:\"],\n\t      [AC_MSG_ERROR([gcov is needed to do coverage])])\n\tAC_SUBST([GCOV])\n\n\tdnl Check if gcc is being used\n\tAS_IF([ test \"$GCC\" = \"no\" ], [\n\t\tAC_MSG_ERROR([not compiling with gcc, which is required for gcov code coverage])\n\t      ])\n\n\tAC_CHECK_PROG([LCOV], [lcov], [lcov])\n\tAC_CHECK_PROG([GENHTML], [genhtml], [genhtml])\n\n\tAS_IF([ test x\"$LCOV\" = x ], [\n\t\tAC_MSG_ERROR([To enable code coverage reporting you must have lcov installed])\n\t      ])\n\n\tAS_IF([ test x\"$GENHTML\" = x ], [\n\t\tAC_MSG_ERROR([Could not find genhtml from the lcov package])\n\t])\n\n\tdnl Build the code coverage flags\n\tdnl Define CODE_COVERAGE_LDFLAGS for backwards compatibility\n\tCODE_COVERAGE_CPPFLAGS=\"-DNDEBUG\"\n\tCODE_COVERAGE_CFLAGS=\"-O0 -g -fprofile-arcs -ftest-coverage\"\n\tCODE_COVERAGE_CXXFLAGS=\"-O0 -g -fprofile-arcs -ftest-coverage\"\n\tCODE_COVERAGE_LIBS=\"-lgcov\"\n\n\tAC_SUBST([CODE_COVERAGE_CPPFLAGS])\n\tAC_SUBST([CODE_COVERAGE_CFLAGS])\n\tAC_SUBST([CODE_COVERAGE_CXXFLAGS])\n\tAC_SUBST([CODE_COVERAGE_LIBS])\n])\n\nAC_DEFUN([AX_CODE_COVERAGE],[\n\tdnl Check for --enable-code-coverage\n\n\t# allow to override gcov location\n\tAC_ARG_WITH([gcov],\n\t  [AS_HELP_STRING([--with-gcov[=GCOV]], [use given GCOV for coverage (GCOV=gcov).])],\n\t  [_AX_CODE_COVERAGE_GCOV_PROG_WITH=$with_gcov],\n\t  [_AX_CODE_COVERAGE_GCOV_PROG_WITH=gcov])\n\n\tAC_MSG_CHECKING([whether to build with code coverage support])\n\tAC_ARG_ENABLE([code-coverage],\n\t  AS_HELP_STRING([--enable-code-coverage],\n\t  [Whether to enable code coverage support]),,\n\t  enable_code_coverage=no)\n\n\tAM_CONDITIONAL([CODE_COVERAGE_ENABLED], [test \"x$enable_code_coverage\" = xyes])\n\tAC_SUBST([CODE_COVERAGE_ENABLED], [$enable_code_coverage])\n\tAC_MSG_RESULT($enable_code_coverage)\n\n\tAS_IF([ test \"x$enable_code_coverage\" = xyes ], [\n\t\t_AX_CODE_COVERAGE_ENABLED\n\t      ])\n\n\t_AX_CODE_COVERAGE_RULES\n])\n"
  },
  {
    "path": "m4/ax_cxx_compile_stdcxx.m4",
    "content": "# ===========================================================================\n#  https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])\n#\n# DESCRIPTION\n#\n#   Check for baseline language coverage in the compiler for the specified\n#   version of the C++ standard.  If necessary, add switches to CXX and\n#   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)\n#   or '14' (for the C++14 standard).\n#\n#   The second argument, if specified, indicates whether you insist on an\n#   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.\n#   -std=c++11).  If neither is specified, you get whatever works, with\n#   preference for an extended mode.\n#\n#   The third argument, if specified 'mandatory' or if left unspecified,\n#   indicates that baseline support for the specified C++ standard is\n#   required and that the macro should error out if no mode with that\n#   support is found.  If specified 'optional', then configuration proceeds\n#   regardless, after defining HAVE_CXX${VERSION} if and only if a\n#   supporting mode is found.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>\n#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>\n#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>\n#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>\n#   Copyright (c) 2015 Paul Norman <penorman@mac.com>\n#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>\n#   Copyright (c) 2016, 2018 Krzesimir Nowak <qdlacz@gmail.com>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved.  This file is offered as-is, without any\n#   warranty.\n\n#serial 10\n\ndnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro\ndnl  (serial version number 13).\n\nAC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl\n  m4_if([$1], [11], [ax_cxx_compile_alternatives=\"11 0x\"],\n        [$1], [14], [ax_cxx_compile_alternatives=\"14 1y\"],\n        [$1], [17], [ax_cxx_compile_alternatives=\"17 1z\"],\n        [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$2], [], [],\n        [$2], [ext], [],\n        [$2], [noext], [],\n        [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [optional], [ax_cxx_compile_cxx$1_required=false],\n        [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])\n  AC_LANG_PUSH([C++])dnl\n  ac_success=no\n\n  m4_if([$2], [noext], [], [dnl\n  if test x$ac_success = xno; then\n    for alternative in ${ax_cxx_compile_alternatives}; do\n      switch=\"-std=gnu++${alternative}\"\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n\n  m4_if([$2], [ext], [], [dnl\n  if test x$ac_success = xno; then\n    dnl HP's aCC needs +std=c++11 according to:\n    dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf\n    dnl Cray's crayCC needs \"-h std=c++11\"\n    for alternative in ${ax_cxx_compile_alternatives}; do\n      for switch in -std=c++${alternative} +std=c++${alternative} \"-h std=c++${alternative}\"; do\n        cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n        AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                       $cachevar,\n          [ac_save_CXX=\"$CXX\"\n           CXX=\"$CXX $switch\"\n           AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n            [eval $cachevar=yes],\n            [eval $cachevar=no])\n           CXX=\"$ac_save_CXX\"])\n        if eval test x\\$$cachevar = xyes; then\n          CXX=\"$CXX $switch\"\n          if test -n \"$CXXCPP\" ; then\n            CXXCPP=\"$CXXCPP $switch\"\n          fi\n          ac_success=yes\n          break\n        fi\n      done\n      if test x$ac_success = xyes; then\n        break\n      fi\n    done\n  fi])\n  AC_LANG_POP([C++])\n  if test x$ax_cxx_compile_cxx$1_required = xtrue; then\n    if test x$ac_success = xno; then\n      AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])\n    fi\n  fi\n  if test x$ac_success = xno; then\n    HAVE_CXX$1=0\n    AC_MSG_NOTICE([No compiler with C++$1 support was found])\n  else\n    HAVE_CXX$1=1\n    AC_DEFINE(HAVE_CXX$1,1,\n              [define if the compiler supports basic C++$1 syntax])\n  fi\n  AC_SUBST(HAVE_CXX$1)\n])\n\n\ndnl  Test body for checking C++11 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n)\n\n\ndnl  Test body for checking C++14 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_14\n)\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_14\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_17\n)\n\ndnl  Tests for new features in C++11\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[\n\n// If the compiler admits that it is not ready for C++11, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201103L\n\n#error \"This is not a C++11 compiler\"\n\n#else\n\nnamespace cxx11\n{\n\n  namespace test_static_assert\n  {\n\n    template <typename T>\n    struct check\n    {\n      static_assert(sizeof(int) <= sizeof(T), \"not big enough\");\n    };\n\n  }\n\n  namespace test_final_override\n  {\n\n    struct Base\n    {\n      virtual void f() {}\n    };\n\n    struct Derived : public Base\n    {\n      virtual void f() override {}\n    };\n\n  }\n\n  namespace test_double_right_angle_brackets\n  {\n\n    template < typename T >\n    struct check {};\n\n    typedef check<void> single_type;\n    typedef check<check<void>> double_type;\n    typedef check<check<check<void>>> triple_type;\n    typedef check<check<check<check<void>>>> quadruple_type;\n\n  }\n\n  namespace test_decltype\n  {\n\n    int\n    f()\n    {\n      int a = 1;\n      decltype(a) b = 2;\n      return a + b;\n    }\n\n  }\n\n  namespace test_type_deduction\n  {\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static const bool value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static const bool value = true;\n    };\n\n    template < typename T1, typename T2 >\n    auto\n    add(T1 a1, T2 a2) -> decltype(a1 + a2)\n    {\n      return a1 + a2;\n    }\n\n    int\n    test(const int c, volatile int v)\n    {\n      static_assert(is_same<int, decltype(0)>::value == true, \"\");\n      static_assert(is_same<int, decltype(c)>::value == false, \"\");\n      static_assert(is_same<int, decltype(v)>::value == false, \"\");\n      auto ac = c;\n      auto av = v;\n      auto sumi = ac + av + 'x';\n      auto sumf = ac + av + 1.0;\n      static_assert(is_same<int, decltype(ac)>::value == true, \"\");\n      static_assert(is_same<int, decltype(av)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumi)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumf)>::value == false, \"\");\n      static_assert(is_same<int, decltype(add(c, v))>::value == true, \"\");\n      return (sumf > 0.0) ? sumi : add(c, v);\n    }\n\n  }\n\n  namespace test_noexcept\n  {\n\n    int f() { return 0; }\n    int g() noexcept { return 0; }\n\n    static_assert(noexcept(f()) == false, \"\");\n    static_assert(noexcept(g()) == true, \"\");\n\n  }\n\n  namespace test_constexpr\n  {\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept\n    {\n      return *s ? strlen_c_r(s + 1, acc + 1) : acc;\n    }\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c(const CharT *const s) noexcept\n    {\n      return strlen_c_r(s, 0UL);\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"1\") == 1UL, \"\");\n    static_assert(strlen_c(\"example\") == 7UL, \"\");\n    static_assert(strlen_c(\"another\\0example\") == 7UL, \"\");\n\n  }\n\n  namespace test_rvalue_references\n  {\n\n    template < int N >\n    struct answer\n    {\n      static constexpr int value = N;\n    };\n\n    answer<1> f(int&)       { return answer<1>(); }\n    answer<2> f(const int&) { return answer<2>(); }\n    answer<3> f(int&&)      { return answer<3>(); }\n\n    void\n    test()\n    {\n      int i = 0;\n      const int c = 0;\n      static_assert(decltype(f(i))::value == 1, \"\");\n      static_assert(decltype(f(c))::value == 2, \"\");\n      static_assert(decltype(f(0))::value == 3, \"\");\n    }\n\n  }\n\n  namespace test_uniform_initialization\n  {\n\n    struct test\n    {\n      static const int zero {};\n      static const int one {1};\n    };\n\n    static_assert(test::zero == 0, \"\");\n    static_assert(test::one == 1, \"\");\n\n  }\n\n  namespace test_lambdas\n  {\n\n    void\n    test1()\n    {\n      auto lambda1 = [](){};\n      auto lambda2 = lambda1;\n      lambda1();\n      lambda2();\n    }\n\n    int\n    test2()\n    {\n      auto a = [](int i, int j){ return i + j; }(1, 2);\n      auto b = []() -> int { return '0'; }();\n      auto c = [=](){ return a + b; }();\n      auto d = [&](){ return c; }();\n      auto e = [a, &b](int x) mutable {\n        const auto identity = [](int y){ return y; };\n        for (auto i = 0; i < a; ++i)\n          a += b--;\n        return x + identity(a + b);\n      }(0);\n      return a + b + c + d + e;\n    }\n\n    int\n    test3()\n    {\n      const auto nullary = [](){ return 0; };\n      const auto unary = [](int x){ return x; };\n      using nullary_t = decltype(nullary);\n      using unary_t = decltype(unary);\n      const auto higher1st = [](nullary_t f){ return f(); };\n      const auto higher2nd = [unary](nullary_t f1){\n        return [unary, f1](unary_t f2){ return f2(unary(f1())); };\n      };\n      return higher1st(nullary) + higher2nd(nullary)(unary);\n    }\n\n  }\n\n  namespace test_variadic_templates\n  {\n\n    template <int...>\n    struct sum;\n\n    template <int N0, int... N1toN>\n    struct sum<N0, N1toN...>\n    {\n      static constexpr auto value = N0 + sum<N1toN...>::value;\n    };\n\n    template <>\n    struct sum<>\n    {\n      static constexpr auto value = 0;\n    };\n\n    static_assert(sum<>::value == 0, \"\");\n    static_assert(sum<1>::value == 1, \"\");\n    static_assert(sum<23>::value == 23, \"\");\n    static_assert(sum<1, 2>::value == 3, \"\");\n    static_assert(sum<5, 5, 11>::value == 21, \"\");\n    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, \"\");\n\n  }\n\n  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae\n  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function\n  // because of this.\n  namespace test_template_alias_sfinae\n  {\n\n    struct foo {};\n\n    template<typename T>\n    using member = typename T::member_type;\n\n    template<typename T>\n    void func(...) {}\n\n    template<typename T>\n    void func(member<T>*) {}\n\n    void test();\n\n    void test() { func<foo>(0); }\n\n  }\n\n}  // namespace cxx11\n\n#endif  // __cplusplus >= 201103L\n\n]])\n\n\ndnl  Tests for new features in C++14\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[\n\n// If the compiler admits that it is not ready for C++14, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201402L\n\n#error \"This is not a C++14 compiler\"\n\n#else\n\nnamespace cxx14\n{\n\n  namespace test_polymorphic_lambdas\n  {\n\n    int\n    test()\n    {\n      const auto lambda = [](auto&&... args){\n        const auto istiny = [](auto x){\n          return (sizeof(x) == 1UL) ? 1 : 0;\n        };\n        const int aretiny[] = { istiny(args)... };\n        return aretiny[0];\n      };\n      return lambda(1, 1L, 1.0f, '1');\n    }\n\n  }\n\n  namespace test_binary_literals\n  {\n\n    constexpr auto ivii = 0b0000000000101010;\n    static_assert(ivii == 42, \"wrong value\");\n\n  }\n\n  namespace test_generalized_constexpr\n  {\n\n    template < typename CharT >\n    constexpr unsigned long\n    strlen_c(const CharT *const s) noexcept\n    {\n      auto length = 0UL;\n      for (auto p = s; *p; ++p)\n        ++length;\n      return length;\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"x\") == 1UL, \"\");\n    static_assert(strlen_c(\"test\") == 4UL, \"\");\n    static_assert(strlen_c(\"another\\0test\") == 7UL, \"\");\n\n  }\n\n  namespace test_lambda_init_capture\n  {\n\n    int\n    test()\n    {\n      auto x = 0;\n      const auto lambda1 = [a = x](int b){ return a + b; };\n      const auto lambda2 = [a = lambda1(x)](){ return a; };\n      return lambda2();\n    }\n\n  }\n\n  namespace test_digit_separators\n  {\n\n    constexpr auto ten_million = 100'000'000;\n    static_assert(ten_million == 100000000, \"\");\n\n  }\n\n  namespace test_return_type_deduction\n  {\n\n    auto f(int& x) { return x; }\n    decltype(auto) g(int& x) { return x; }\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static constexpr auto value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static constexpr auto value = true;\n    };\n\n    int\n    test()\n    {\n      auto x = 0;\n      static_assert(is_same<int, decltype(f(x))>::value, \"\");\n      static_assert(is_same<int&, decltype(g(x))>::value, \"\");\n      return x;\n    }\n\n  }\n\n}  // namespace cxx14\n\n#endif  // __cplusplus >= 201402L\n\n]])\n\n\ndnl  Tests for new features in C++17\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[\n\n// If the compiler admits that it is not ready for C++17, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201703L\n\n#error \"This is not a C++17 compiler\"\n\n#else\n\n#include <initializer_list>\n#include <utility>\n#include <type_traits>\n\nnamespace cxx17\n{\n\n  namespace test_constexpr_lambdas\n  {\n\n    constexpr int foo = [](){return 42;}();\n\n  }\n\n  namespace test::nested_namespace::definitions\n  {\n\n  }\n\n  namespace test_fold_expression\n  {\n\n    template<typename... Args>\n    int multiply(Args... args)\n    {\n      return (args * ... * 1);\n    }\n\n    template<typename... Args>\n    bool all(Args... args)\n    {\n      return (args && ...);\n    }\n\n  }\n\n  namespace test_extended_static_assert\n  {\n\n    static_assert (true);\n\n  }\n\n  namespace test_auto_brace_init_list\n  {\n\n    auto foo = {5};\n    auto bar {5};\n\n    static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);\n    static_assert(std::is_same<int, decltype(bar)>::value);\n  }\n\n  namespace test_typename_in_template_template_parameter\n  {\n\n    template<template<typename> typename X> struct D;\n\n  }\n\n  namespace test_fallthrough_nodiscard_maybe_unused_attributes\n  {\n\n    int f1()\n    {\n      return 42;\n    }\n\n    [[nodiscard]] int f2()\n    {\n      [[maybe_unused]] auto unused = f1();\n\n      switch (f1())\n      {\n      case 17:\n        f1();\n        [[fallthrough]];\n      case 42:\n        f1();\n      }\n      return f1();\n    }\n\n  }\n\n  namespace test_extended_aggregate_initialization\n  {\n\n    struct base1\n    {\n      int b1, b2 = 42;\n    };\n\n    struct base2\n    {\n      base2() {\n        b3 = 42;\n      }\n      int b3;\n    };\n\n    struct derived : base1, base2\n    {\n        int d;\n    };\n\n    derived d1 {{1, 2}, {}, 4};  // full initialization\n    derived d2 {{}, {}, 4};      // value-initialized bases\n\n  }\n\n  namespace test_general_range_based_for_loop\n  {\n\n    struct iter\n    {\n      int i;\n\n      int& operator* ()\n      {\n        return i;\n      }\n\n      const int& operator* () const\n      {\n        return i;\n      }\n\n      iter& operator++()\n      {\n        ++i;\n        return *this;\n      }\n    };\n\n    struct sentinel\n    {\n      int i;\n    };\n\n    bool operator== (const iter& i, const sentinel& s)\n    {\n      return i.i == s.i;\n    }\n\n    bool operator!= (const iter& i, const sentinel& s)\n    {\n      return !(i == s);\n    }\n\n    struct range\n    {\n      iter begin() const\n      {\n        return {0};\n      }\n\n      sentinel end() const\n      {\n        return {5};\n      }\n    };\n\n    void f()\n    {\n      range r {};\n\n      for (auto i : r)\n      {\n        [[maybe_unused]] auto v = i;\n      }\n    }\n\n  }\n\n  namespace test_lambda_capture_asterisk_this_by_value\n  {\n\n    struct t\n    {\n      int i;\n      int foo()\n      {\n        return [*this]()\n        {\n          return i;\n        }();\n      }\n    };\n\n  }\n\n  namespace test_enum_class_construction\n  {\n\n    enum class byte : unsigned char\n    {};\n\n    byte foo {42};\n\n  }\n\n  namespace test_constexpr_if\n  {\n\n    template <bool cond>\n    int f ()\n    {\n      if constexpr(cond)\n      {\n        return 13;\n      }\n      else\n      {\n        return 42;\n      }\n    }\n\n  }\n\n  namespace test_selection_statement_with_initializer\n  {\n\n    int f()\n    {\n      return 13;\n    }\n\n    int f2()\n    {\n      if (auto i = f(); i > 0)\n      {\n        return 3;\n      }\n\n      switch (auto i = f(); i + 4)\n      {\n      case 17:\n        return 2;\n\n      default:\n        return 1;\n      }\n    }\n\n  }\n\n  namespace test_template_argument_deduction_for_class_templates\n  {\n\n    template <typename T1, typename T2>\n    struct pair\n    {\n      pair (T1 p1, T2 p2)\n        : m1 {p1},\n          m2 {p2}\n      {}\n\n      T1 m1;\n      T2 m2;\n    };\n\n    void f()\n    {\n      [[maybe_unused]] auto p = pair{13, 42u};\n    }\n\n  }\n\n  namespace test_non_type_auto_template_parameters\n  {\n\n    template <auto n>\n    struct B\n    {};\n\n    B<5> b1;\n    B<'a'> b2;\n\n  }\n\n  namespace test_structured_bindings\n  {\n\n    int arr[2] = { 1, 2 };\n    std::pair<int, int> pr = { 1, 2 };\n\n    auto f1() -> int(&)[2]\n    {\n      return arr;\n    }\n\n    auto f2() -> std::pair<int, int>&\n    {\n      return pr;\n    }\n\n    struct S\n    {\n      int x1 : 2;\n      volatile double y1;\n    };\n\n    S f3()\n    {\n      return {};\n    }\n\n    auto [ x1, y1 ] = f1();\n    auto& [ xr1, yr1 ] = f1();\n    auto [ x2, y2 ] = f2();\n    auto& [ xr2, yr2 ] = f2();\n    const auto [ x3, y3 ] = f3();\n\n  }\n\n  namespace test_exception_spec_type_system\n  {\n\n    struct Good {};\n    struct Bad {};\n\n    void g1() noexcept;\n    void g2();\n\n    template<typename T>\n    Bad\n    f(T*, T*);\n\n    template<typename T1, typename T2>\n    Good\n    f(T1*, T2*);\n\n    static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);\n\n  }\n\n  namespace test_inline_variables\n  {\n\n    template<class T> void f(T)\n    {}\n\n    template<class T> inline T g(T)\n    {\n      return T{};\n    }\n\n    template<> inline void f<>(int)\n    {}\n\n    template<> int g<>(int)\n    {\n      return 5;\n    }\n\n  }\n\n}  // namespace cxx17\n\n#endif  // __cplusplus < 201703L\n\n]])\n"
  },
  {
    "path": "m4/ax_file_escapes.m4",
    "content": "# ===========================================================================\n#     https://www.gnu.org/software/autoconf-archive/ax_file_escapes.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_FILE_ESCAPES\n#\n# DESCRIPTION\n#\n#   Writes the specified data to the specified file.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Tom Howard <tomhoward@users.sf.net>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 8\n\nAC_DEFUN([AX_FILE_ESCAPES],[\nAX_DOLLAR=\"\\$\"\nAX_SRB=\"\\\\135\"\nAX_SLB=\"\\\\133\"\nAX_BS=\"\\\\\\\\\"\nAX_DQ=\"\\\"\"\n])\n"
  },
  {
    "path": "m4/ax_require_defined.m4",
    "content": "# ===========================================================================\n#    https://www.gnu.org/software/autoconf-archive/ax_require_defined.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_REQUIRE_DEFINED(MACRO)\n#\n# DESCRIPTION\n#\n#   AX_REQUIRE_DEFINED is a simple helper for making sure other macros have\n#   been defined and thus are available for use.  This avoids random issues\n#   where a macro isn't expanded.  Instead the configure script emits a\n#   non-fatal:\n#\n#     ./configure: line 1673: AX_CFLAGS_WARN_ALL: command not found\n#\n#   It's like AC_REQUIRE except it doesn't expand the required macro.\n#\n#   Here's an example:\n#\n#     AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG])\n#\n# LICENSE\n#\n#   Copyright (c) 2014 Mike Frysinger <vapier@gentoo.org>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 2\n\nAC_DEFUN([AX_REQUIRE_DEFINED], [dnl\n  m4_ifndef([$1], [m4_fatal([macro ]$1[ is not defined; is a m4 file missing?])])\n])dnl AX_REQUIRE_DEFINED\n"
  },
  {
    "path": "m4/ax_with_curses.m4",
    "content": "# ===========================================================================\n#      http://www.gnu.org/software/autoconf-archive/ax_with_curses.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_WITH_CURSES\n#\n# DESCRIPTION\n#\n#   This macro checks whether a SysV or X/Open-compatible Curses library is\n#   present, along with the associated header file.  The NcursesW\n#   (wide-character) library is searched for first, followed by Ncurses,\n#   then the system-default plain Curses.  The first library found is the\n#   one returned.\n#\n#   The following options are understood: --with-ncursesw, --with-ncurses,\n#   --without-ncursesw, --without-ncurses.  The \"--with\" options force the\n#   macro to use that particular library, terminating with an error if not\n#   found.  The \"--without\" options simply skip the check for that library.\n#   The effect on the search pattern is:\n#\n#     (no options)                           - NcursesW, Ncurses, Curses\n#     --with-ncurses     --with-ncursesw     - NcursesW only [*]\n#     --without-ncurses  --with-ncursesw     - NcursesW only [*]\n#                        --with-ncursesw     - NcursesW only [*]\n#     --with-ncurses     --without-ncursesw  - Ncurses only [*]\n#     --with-ncurses                         - NcursesW, Ncurses [**]\n#     --without-ncurses  --without-ncursesw  - Curses only\n#                        --without-ncursesw  - Ncurses, Curses\n#     --without-ncurses                      - NcursesW, Curses\n#\n#   [*]  If the library is not found, abort the configure script.\n#\n#   [**] If the second library (Ncurses) is not found, abort configure.\n#\n#   The following preprocessor symbols may be defined by this macro if the\n#   appropriate conditions are met:\n#\n#     HAVE_CURSES             - if any SysV or X/Open Curses library found\n#     HAVE_CURSES_ENHANCED    - if library supports X/Open Enhanced functions\n#     HAVE_CURSES_COLOR       - if library supports color (enhanced functions)\n#     HAVE_CURSES_OBSOLETE    - if library supports certain obsolete features\n#     HAVE_NCURSESW           - if NcursesW (wide char) library is to be used\n#     HAVE_NCURSES            - if the Ncurses library is to be used\n#\n#     HAVE_CURSES_H           - if <curses.h> is present and should be used\n#     HAVE_NCURSESW_H         - if <ncursesw.h> should be used\n#     HAVE_NCURSES_H          - if <ncurses.h> should be used\n#     HAVE_NCURSESW_CURSES_H  - if <ncursesw/curses.h> should be used\n#     HAVE_NCURSES_CURSES_H   - if <ncurses/curses.h> should be used\n#\n#   (These preprocessor symbols are discussed later in this document.)\n#\n#   The following output variable is defined by this macro; it is precious\n#   and may be overridden on the ./configure command line:\n#\n#     CURSES_LIB  - library to add to xxx_LDADD\n#\n#   The library listed in CURSES_LIB is NOT added to LIBS by default. You\n#   need to add CURSES_LIB to the appropriate xxx_LDADD line in your\n#   Makefile.am.  For example:\n#\n#     prog_LDADD = @CURSES_LIB@\n#\n#   If CURSES_LIB is set on the configure command line (such as by running\n#   \"./configure CURSES_LIB=-lmycurses\"), then the only header searched for\n#   is <curses.h>.  The user may use the CPPFLAGS precious variable to\n#   override the standard #include search path.  If the user needs to\n#   specify an alternative path for a library (such as for a non-standard\n#   NcurseW), the user should use the LDFLAGS variable.\n#\n#   The following shell variables may be defined by this macro:\n#\n#     ax_cv_curses           - set to \"yes\" if any Curses library found\n#     ax_cv_curses_enhanced  - set to \"yes\" if Enhanced functions present\n#     ax_cv_curses_color     - set to \"yes\" if color functions present\n#     ax_cv_curses_obsolete  - set to \"yes\" if obsolete features present\n#\n#     ax_cv_ncursesw      - set to \"yes\" if NcursesW library found\n#     ax_cv_ncurses       - set to \"yes\" if Ncurses library found\n#     ax_cv_plaincurses   - set to \"yes\" if plain Curses library found\n#     ax_cv_curses_which  - set to \"ncursesw\", \"ncurses\", \"plaincurses\" or \"no\"\n#\n#   These variables can be used in your configure.ac to determine the level\n#   of support you need from the Curses library.  For example, if you must\n#   have either Ncurses or NcursesW, you could include:\n#\n#     AX_WITH_CURSES\n#     if test \"x$ax_cv_ncursesw\" != xyes && test \"x$ax_cv_ncurses\" != xyes; then\n#         AX_MSG_ERROR([requires either NcursesW or Ncurses library])\n#     fi\n#\n#   If any Curses library will do (but one must be present and must support\n#   color), you could use:\n#\n#     AX_WITH_CURSES\n#     if test \"x$ax_cv_curses\" != xyes || test \"x$ax_cv_curses_color\" != xyes; then\n#         AC_MSG_ERROR([requires an X/Open-compatible Curses library with color])\n#     fi\n#\n#   Certain preprocessor symbols and shell variables defined by this macro\n#   can be used to determine various features of the Curses library.  In\n#   particular, HAVE_CURSES and ax_cv_curses are defined if the Curses\n#   library found conforms to the traditional SysV and/or X/Open Base Curses\n#   definition.  Any working Curses library conforms to this level.\n#\n#   HAVE_CURSES_ENHANCED and ax_cv_curses_enhanced are defined if the\n#   library supports the X/Open Enhanced Curses definition.  In particular,\n#   the wide-character types attr_t, cchar_t and wint_t, the functions\n#   wattr_set() and wget_wch() and the macros WA_NORMAL and _XOPEN_CURSES\n#   are checked.  The Ncurses library does NOT conform to this definition,\n#   although NcursesW does.\n#\n#   HAVE_CURSES_COLOR and ax_cv_curses_color are defined if the library\n#   supports color functions and macros such as COLOR_PAIR, A_COLOR,\n#   COLOR_WHITE, COLOR_RED and init_pair().  These are NOT part of the\n#   X/Open Base Curses definition, but are part of the Enhanced set of\n#   functions.  The Ncurses library DOES support these functions, as does\n#   NcursesW.\n#\n#   HAVE_CURSES_OBSOLETE and ax_cv_curses_obsolete are defined if the\n#   library supports certain features present in SysV and BSD Curses but not\n#   defined in the X/Open definition.  In particular, the functions\n#   getattrs(), getcurx() and getmaxx() are checked.\n#\n#   To use the HAVE_xxx_H preprocessor symbols, insert the following into\n#   your system.h (or equivalent) header file:\n#\n#     #if defined HAVE_NCURSESW_CURSES_H\n#     #  include <ncursesw/curses.h>\n#     #elif defined HAVE_NCURSESW_H\n#     #  include <ncursesw.h>\n#     #elif defined HAVE_NCURSES_CURSES_H\n#     #  include <ncurses/curses.h>\n#     #elif defined HAVE_NCURSES_H\n#     #  include <ncurses.h>\n#     #elif defined HAVE_CURSES_H\n#     #  include <curses.h>\n#     #else\n#     #  error \"SysV or X/Open-compatible Curses header file required\"\n#     #endif\n#\n#   For previous users of this macro: you should not need to change anything\n#   in your configure.ac or Makefile.am, as the previous (serial 10)\n#   semantics are still valid.  However, you should update your system.h (or\n#   equivalent) header file to the fragment shown above. You are encouraged\n#   also to make use of the extended functionality provided by this version\n#   of AX_WITH_CURSES, as well as in the additional macros\n#   AX_WITH_CURSES_PANEL, AX_WITH_CURSES_MENU and AX_WITH_CURSES_FORM.\n#\n# LICENSE\n#\n#   Copyright (c) 2009 Mark Pulford <mark@kyne.com.au>\n#   Copyright (c) 2009 Damian Pietras <daper@daper.net>\n#   Copyright (c) 2012 Reuben Thomas <rrt@sc3d.org>\n#   Copyright (c) 2011 John Zaitseff <J.Zaitseff@zap.org.au>\n#\n#   This program is free software: you can redistribute it and/or modify it\n#   under the terms of the GNU General Public License as published by the\n#   Free Software Foundation, either version 3 of the License, or (at your\n#   option) any later version.\n#\n#   This program is distributed in the hope that it will be useful, but\n#   WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n#   Public License for more details.\n#\n#   You should have received a copy of the GNU General Public License along\n#   with this program. If not, see <http://www.gnu.org/licenses/>.\n#\n#   As a special exception, the respective Autoconf Macro's copyright owner\n#   gives unlimited permission to copy, distribute and modify the configure\n#   scripts that are the output of Autoconf when processing the Macro. You\n#   need not follow the terms of the GNU General Public License when using\n#   or distributing such scripts, even though portions of the text of the\n#   Macro appear in them. The GNU General Public License (GPL) does govern\n#   all other use of the material that constitutes the Autoconf Macro.\n#\n#   This special exception to the GPL applies to versions of the Autoconf\n#   Macro released by the Autoconf Archive. When you make and distribute a\n#   modified version of the Autoconf Macro, you may extend this special\n#   exception to the GPL to apply to your modified version as well.\n\n#serial 13\n\nAU_ALIAS([MP_WITH_CURSES], [AX_WITH_CURSES])\nAC_DEFUN([AX_WITH_CURSES], [\n    AC_ARG_VAR([CURSES_LIB], [linker library for Curses, e.g. -lcurses])\n    AC_ARG_WITH([ncurses], [AS_HELP_STRING([--with-ncurses],\n        [force the use of Ncurses or NcursesW])],\n        [], [with_ncurses=check])\n    AC_ARG_WITH([ncursesw], [AS_HELP_STRING([--without-ncursesw],\n        [do not use NcursesW (wide character support)])],\n        [], [with_ncursesw=check])\n\n    ax_saved_LIBS=$LIBS\n    AS_IF([test \"x$with_ncurses\" = xyes || test \"x$with_ncursesw\" = xyes],\n        [ax_with_plaincurses=no], [ax_with_plaincurses=check])\n\n    ax_cv_curses_which=no\n\n    # Test for NcursesW\n\n    AS_IF([test \"x$CURSES_LIB\" = x && test \"x$with_ncursesw\" != xno], [\n        LIBS=\"$ax_saved_LIBS -lncursesw\"\n\n        AC_CACHE_CHECK([for NcursesW wide-character library], [ax_cv_ncursesw], [\n            AC_LINK_IFELSE([AC_LANG_CALL([], [initscr])],\n                [ax_cv_ncursesw=yes], [ax_cv_ncursesw=no])\n        ])\n        AS_IF([test \"x$ax_cv_ncursesw\" = xno && test \"x$with_ncursesw\" = xyes], [\n            AC_MSG_ERROR([--with-ncursesw specified but could not find NcursesW library])\n        ])\n\n        AS_IF([test \"x$ax_cv_ncursesw\" = xyes], [\n            ax_cv_curses=yes\n            ax_cv_curses_which=ncursesw\n            CURSES_LIB=\"-lncursesw\"\n            AC_DEFINE([HAVE_NCURSESW], [1], [Define to 1 if the NcursesW library is present])\n            AC_DEFINE([HAVE_CURSES],   [1], [Define to 1 if a SysV or X/Open compatible Curses library is present])\n\n            AC_CACHE_CHECK([for working ncursesw/curses.h], [ax_cv_header_ncursesw_curses_h], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@define _XOPEN_SOURCE_EXTENDED 1\n                        @%:@include <ncursesw/curses.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        chtype c = COLOR_PAIR(1) & A_COLOR;\n                        attr_t d = WA_NORMAL;\n                        cchar_t e;\n                        wint_t f;\n                        int g = getattrs(stdscr);\n                        int h = getcurx(stdscr) + getmaxx(stdscr);\n                        initscr();\n                        init_pair(1, COLOR_WHITE, COLOR_RED);\n                        wattr_set(stdscr, d, 0, NULL);\n                        wget_wch(stdscr, &f);\n                    ]])],\n                    [ax_cv_header_ncursesw_curses_h=yes],\n                    [ax_cv_header_ncursesw_curses_h=no])\n            ])\n            AS_IF([test \"x$ax_cv_header_ncursesw_curses_h\" = xyes], [\n                ax_cv_curses_enhanced=yes\n                ax_cv_curses_color=yes\n                ax_cv_curses_obsolete=yes\n                AC_DEFINE([HAVE_CURSES_ENHANCED],   [1], [Define to 1 if library supports X/Open Enhanced functions])\n                AC_DEFINE([HAVE_CURSES_COLOR],      [1], [Define to 1 if library supports color (enhanced functions)])\n                AC_DEFINE([HAVE_CURSES_OBSOLETE],   [1], [Define to 1 if library supports certain obsolete features])\n                AC_DEFINE([HAVE_NCURSESW_CURSES_H], [1], [Define to 1 if <ncursesw/curses.h> is present])\n            ])\n\n            AC_CACHE_CHECK([for working ncursesw.h], [ax_cv_header_ncursesw_h], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@define _XOPEN_SOURCE_EXTENDED 1\n                        @%:@include <ncursesw.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        chtype c = COLOR_PAIR(1) & A_COLOR;\n                        attr_t d = WA_NORMAL;\n                        cchar_t e;\n                        wint_t f;\n                        int g = getattrs(stdscr);\n                        int h = getcurx(stdscr) + getmaxx(stdscr);\n                        initscr();\n                        init_pair(1, COLOR_WHITE, COLOR_RED);\n                        wattr_set(stdscr, d, 0, NULL);\n                        wget_wch(stdscr, &f);\n                    ]])],\n                    [ax_cv_header_ncursesw_h=yes],\n                    [ax_cv_header_ncursesw_h=no])\n            ])\n            AS_IF([test \"x$ax_cv_header_ncursesw_h\" = xyes], [\n                ax_cv_curses_enhanced=yes\n                ax_cv_curses_color=yes\n                ax_cv_curses_obsolete=yes\n                AC_DEFINE([HAVE_CURSES_ENHANCED], [1], [Define to 1 if library supports X/Open Enhanced functions])\n                AC_DEFINE([HAVE_CURSES_COLOR],    [1], [Define to 1 if library supports color (enhanced functions)])\n                AC_DEFINE([HAVE_CURSES_OBSOLETE], [1], [Define to 1 if library supports certain obsolete features])\n                AC_DEFINE([HAVE_NCURSESW_H],      [1], [Define to 1 if <ncursesw.h> is present])\n            ])\n\n            AC_CACHE_CHECK([for working ncurses.h], [ax_cv_header_ncurses_h_with_ncursesw], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@define _XOPEN_SOURCE_EXTENDED 1\n                        @%:@include <ncurses.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        chtype c = COLOR_PAIR(1) & A_COLOR;\n                        attr_t d = WA_NORMAL;\n                        cchar_t e;\n                        wint_t f;\n                        int g = getattrs(stdscr);\n                        int h = getcurx(stdscr) + getmaxx(stdscr);\n                        initscr();\n                        init_pair(1, COLOR_WHITE, COLOR_RED);\n                        wattr_set(stdscr, d, 0, NULL);\n                        wget_wch(stdscr, &f);\n                    ]])],\n                    [ax_cv_header_ncurses_h_with_ncursesw=yes],\n                    [ax_cv_header_ncurses_h_with_ncursesw=no])\n            ])\n            AS_IF([test \"x$ax_cv_header_ncurses_h_with_ncursesw\" = xyes], [\n                ax_cv_curses_enhanced=yes\n                ax_cv_curses_color=yes\n                ax_cv_curses_obsolete=yes\n                AC_DEFINE([HAVE_CURSES_ENHANCED], [1], [Define to 1 if library supports X/Open Enhanced functions])\n                AC_DEFINE([HAVE_CURSES_COLOR],    [1], [Define to 1 if library supports color (enhanced functions)])\n                AC_DEFINE([HAVE_CURSES_OBSOLETE], [1], [Define to 1 if library supports certain obsolete features])\n                AC_DEFINE([HAVE_NCURSES_H],       [1], [Define to 1 if <ncurses.h> is present])\n            ])\n\n            AS_IF([test \"x$ax_cv_header_ncursesw_curses_h\" = xno && test \"x$ax_cv_header_ncursesw_h\" = xno && test \"x$ax_cv_header_ncurses_h_with_ncursesw\" = xno], [\n                AC_MSG_WARN([could not find a working ncursesw/curses.h, ncursesw.h or ncurses.h])\n            ])\n        ])\n    ])\n\n    # Test for Ncurses\n\n    AS_IF([test \"x$CURSES_LIB\" = x && test \"x$with_ncurses\" != xno && test \"x$ax_cv_curses_which\" = xno], [\n        LIBS=\"$ax_saved_LIBS -lncurses\"\n\n        AC_CACHE_CHECK([for Ncurses library], [ax_cv_ncurses], [\n            AC_LINK_IFELSE([AC_LANG_CALL([], [initscr])],\n                [ax_cv_ncurses=yes], [ax_cv_ncurses=no])\n        ])\n        AS_IF([test \"x$ax_cv_ncurses\" = xno && test \"x$with_ncurses\" = xyes], [\n            AC_MSG_ERROR([--with-ncurses specified but could not find Ncurses library])\n        ])\n\n        AS_IF([test \"x$ax_cv_ncurses\" = xyes], [\n            ax_cv_curses=yes\n            ax_cv_curses_which=ncurses\n            CURSES_LIB=\"-lncurses\"\n            AC_DEFINE([HAVE_NCURSES], [1], [Define to 1 if the Ncurses library is present])\n            AC_DEFINE([HAVE_CURSES],  [1], [Define to 1 if a SysV or X/Open compatible Curses library is present])\n\n            AC_CACHE_CHECK([for working ncurses/curses.h], [ax_cv_header_ncurses_curses_h], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@include <ncurses/curses.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        chtype c = COLOR_PAIR(1) & A_COLOR;\n                        int g = getattrs(stdscr);\n                        int h = getcurx(stdscr) + getmaxx(stdscr);\n                        initscr();\n                        init_pair(1, COLOR_WHITE, COLOR_RED);\n                    ]])],\n                    [ax_cv_header_ncurses_curses_h=yes],\n                    [ax_cv_header_ncurses_curses_h=no])\n            ])\n            AS_IF([test \"x$ax_cv_header_ncurses_curses_h\" = xyes], [\n                ax_cv_curses_color=yes\n                ax_cv_curses_obsolete=yes\n                AC_DEFINE([HAVE_CURSES_COLOR],     [1], [Define to 1 if library supports color (enhanced functions)])\n                AC_DEFINE([HAVE_CURSES_OBSOLETE],  [1], [Define to 1 if library supports certain obsolete features])\n                AC_DEFINE([HAVE_NCURSES_CURSES_H], [1], [Define to 1 if <ncurses/curses.h> is present])\n            ])\n\n            AC_CACHE_CHECK([for working ncurses.h], [ax_cv_header_ncurses_h], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@include <ncurses.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        chtype c = COLOR_PAIR(1) & A_COLOR;\n                        int g = getattrs(stdscr);\n                        int h = getcurx(stdscr) + getmaxx(stdscr);\n                        initscr();\n                        init_pair(1, COLOR_WHITE, COLOR_RED);\n                    ]])],\n                    [ax_cv_header_ncurses_h=yes],\n                    [ax_cv_header_ncurses_h=no])\n            ])\n            AS_IF([test \"x$ax_cv_header_ncurses_h\" = xyes], [\n                ax_cv_curses_color=yes\n                ax_cv_curses_obsolete=yes\n                AC_DEFINE([HAVE_CURSES_COLOR],    [1], [Define to 1 if library supports color (enhanced functions)])\n                AC_DEFINE([HAVE_CURSES_OBSOLETE], [1], [Define to 1 if library supports certain obsolete features])\n                AC_DEFINE([HAVE_NCURSES_H],       [1], [Define to 1 if <ncurses.h> is present])\n            ])\n\n            AS_IF([test \"x$ax_cv_header_ncurses_curses_h\" = xno && test \"x$ax_cv_header_ncurses_h\" = xno], [\n                AC_MSG_WARN([could not find a working ncurses/curses.h or ncurses.h])\n            ])\n        ])\n    ])\n\n    # Test for plain Curses (or if CURSES_LIB was set by user)\n\n    AS_IF([test \"x$with_plaincurses\" != xno && test \"x$ax_cv_curses_which\" = xno], [\n        AS_IF([test \"x$CURSES_LIB\" != x], [\n            LIBS=\"$ax_saved_LIBS $CURSES_LIB\"\n        ], [\n            LIBS=\"$ax_saved_LIBS -lcurses\"\n        ])\n\n        AC_CACHE_CHECK([for Curses library], [ax_cv_plaincurses], [\n            AC_LINK_IFELSE([AC_LANG_CALL([], [initscr])],\n                [ax_cv_plaincurses=yes], [ax_cv_plaincurses=no])\n        ])\n\n        AS_IF([test \"x$ax_cv_plaincurses\" = xyes], [\n            ax_cv_curses=yes\n            ax_cv_curses_which=plaincurses\n            AS_IF([test \"x$CURSES_LIB\" = x], [\n                CURSES_LIB=\"-lcurses\"\n            ])\n            AC_DEFINE([HAVE_CURSES], [1], [Define to 1 if a SysV or X/Open compatible Curses library is present])\n\n            # Check for base conformance (and header file)\n\n            AC_CACHE_CHECK([for working curses.h], [ax_cv_header_curses_h], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@include <curses.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        initscr();\n                    ]])],\n                    [ax_cv_header_curses_h=yes],\n                    [ax_cv_header_curses_h=no])\n            ])\n            AS_IF([test \"x$ax_cv_header_curses_h\" = xyes], [\n                AC_DEFINE([HAVE_CURSES_H], [1], [Define to 1 if <curses.h> is present])\n\n                # Check for X/Open Enhanced conformance\n\n                AC_CACHE_CHECK([for X/Open Enhanced Curses conformance], [ax_cv_plaincurses_enhanced], [\n                    AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                            @%:@define _XOPEN_SOURCE_EXTENDED 1\n                            @%:@include <curses.h>\n                            @%:@ifndef _XOPEN_CURSES\n                            @%:@error \"this Curses library is not enhanced\"\n                            \"this Curses library is not enhanced\"\n                            @%:@endif\n                        ]], [[\n                            chtype a = A_BOLD;\n                            int b = KEY_LEFT;\n                            chtype c = COLOR_PAIR(1) & A_COLOR;\n                            attr_t d = WA_NORMAL;\n                            cchar_t e;\n                            wint_t f;\n                            initscr();\n                            init_pair(1, COLOR_WHITE, COLOR_RED);\n                            wattr_set(stdscr, d, 0, NULL);\n                            wget_wch(stdscr, &f);\n                        ]])],\n                        [ax_cv_plaincurses_enhanced=yes],\n                        [ax_cv_plaincurses_enhanced=no])\n                ])\n                AS_IF([test \"x$ax_cv_plaincurses_enhanced\" = xyes], [\n                    ax_cv_curses_enhanced=yes\n                    ax_cv_curses_color=yes\n                    AC_DEFINE([HAVE_CURSES_ENHANCED], [1], [Define to 1 if library supports X/Open Enhanced functions])\n                    AC_DEFINE([HAVE_CURSES_COLOR],    [1], [Define to 1 if library supports color (enhanced functions)])\n                ])\n\n                # Check for color functions\n\n                AC_CACHE_CHECK([for Curses color functions], [ax_cv_plaincurses_color], [\n                    AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@define _XOPEN_SOURCE_EXTENDED 1\n                        @%:@include <curses.h>\n                        ]], [[\n                            chtype a = A_BOLD;\n                            int b = KEY_LEFT;\n                            chtype c = COLOR_PAIR(1) & A_COLOR;\n                            initscr();\n                            init_pair(1, COLOR_WHITE, COLOR_RED);\n                        ]])],\n                        [ax_cv_plaincurses_color=yes],\n                        [ax_cv_plaincurses_color=no])\n                ])\n                AS_IF([test \"x$ax_cv_plaincurses_color\" = xyes], [\n                    ax_cv_curses_color=yes\n                    AC_DEFINE([HAVE_CURSES_COLOR], [1], [Define to 1 if library supports color (enhanced functions)])\n                ])\n\n                # Check for obsolete functions\n\n                AC_CACHE_CHECK([for obsolete Curses functions], [ax_cv_plaincurses_obsolete], [\n                AC_LINK_IFELSE([AC_LANG_PROGRAM([[\n                        @%:@include <curses.h>\n                    ]], [[\n                        chtype a = A_BOLD;\n                        int b = KEY_LEFT;\n                        int g = getattrs(stdscr);\n                        int h = getcurx(stdscr) + getmaxx(stdscr);\n                        initscr();\n                    ]])],\n                    [ax_cv_plaincurses_obsolete=yes],\n                    [ax_cv_plaincurses_obsolete=no])\n                ])\n                AS_IF([test \"x$ax_cv_plaincurses_obsolete\" = xyes], [\n                    ax_cv_curses_obsolete=yes\n                    AC_DEFINE([HAVE_CURSES_OBSOLETE], [1], [Define to 1 if library supports certain obsolete features])\n                ])\n            ])\n\n            AS_IF([test \"x$ax_cv_header_curses_h\" = xno], [\n                AC_MSG_WARN([could not find a working curses.h])\n            ])\n        ])\n    ])\n\n    AS_IF([test \"x$ax_cv_curses\"          != xyes], [ax_cv_curses=no])\n    AS_IF([test \"x$ax_cv_curses_enhanced\" != xyes], [ax_cv_curses_enhanced=no])\n    AS_IF([test \"x$ax_cv_curses_color\"    != xyes], [ax_cv_curses_color=no])\n    AS_IF([test \"x$ax_cv_curses_obsolete\" != xyes], [ax_cv_curses_obsolete=no])\n\n    LIBS=$ax_saved_LIBS\n])dnl\n"
  },
  {
    "path": "m4/pkg.m4",
    "content": "# pkg.m4 - Macros to locate and utilise pkg-config.            -*- Autoconf -*-\n# serial 1 (pkg-config-0.24)\n# \n# Copyright © 2004 Scott James Remnant <scott@netsplit.com>.\n#\n# This program is free software; you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 2 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n# General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, write to the Free Software\n# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n#\n# As a special exception to the GNU General Public License, if you\n# distribute this file as part of a program that contains a\n# configuration script generated by Autoconf, you may include it under\n# the same distribution terms that you use for the rest of that program.\n\n# PKG_PROG_PKG_CONFIG([MIN-VERSION])\n# ----------------------------------\nAC_DEFUN([PKG_PROG_PKG_CONFIG],\n[m4_pattern_forbid([^_?PKG_[A-Z_]+$])\nm4_pattern_allow([^PKG_CONFIG(_(PATH|LIBDIR|SYSROOT_DIR|ALLOW_SYSTEM_(CFLAGS|LIBS)))?$])\nm4_pattern_allow([^PKG_CONFIG_(DISABLE_UNINSTALLED|TOP_BUILD_DIR|DEBUG_SPEW)$])\nAC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])\nAC_ARG_VAR([PKG_CONFIG_PATH], [directories to add to pkg-config's search path])\nAC_ARG_VAR([PKG_CONFIG_LIBDIR], [path overriding pkg-config's built-in search path])\n\nif test \"x$ac_cv_env_PKG_CONFIG_set\" != \"xset\"; then\n\tAC_PATH_TOOL([PKG_CONFIG], [pkg-config])\nfi\nif test -n \"$PKG_CONFIG\"; then\n\t_pkg_min_version=m4_default([$1], [0.9.0])\n\tAC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version])\n\tif $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then\n\t\tAC_MSG_RESULT([yes])\n\telse\n\t\tAC_MSG_RESULT([no])\n\t\tPKG_CONFIG=\"\"\n\tfi\nfi[]dnl\n])# PKG_PROG_PKG_CONFIG\n\n# PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])\n#\n# Check to see whether a particular set of modules exists.  Similar\n# to PKG_CHECK_MODULES(), but does not set variables or print errors.\n#\n# Please remember that m4 expands AC_REQUIRE([PKG_PROG_PKG_CONFIG])\n# only at the first occurence in configure.ac, so if the first place\n# it's called might be skipped (such as if it is within an \"if\", you\n# have to call PKG_CHECK_EXISTS manually\n# --------------------------------------------------------------\nAC_DEFUN([PKG_CHECK_EXISTS],\n[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl\nif test -n \"$PKG_CONFIG\" && \\\n    AC_RUN_LOG([$PKG_CONFIG --exists --print-errors \"$1\"]); then\n  m4_default([$2], [:])\nm4_ifvaln([$3], [else\n  $3])dnl\nfi])\n\n# _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])\n# ---------------------------------------------\nm4_define([_PKG_CONFIG],\n[if test -n \"$$1\"; then\n    pkg_cv_[]$1=\"$$1\"\n elif test -n \"$PKG_CONFIG\"; then\n    PKG_CHECK_EXISTS([$3],\n                     [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 \"$3\" 2>/dev/null`\n\t\t      test \"x$?\" != \"x0\" && pkg_failed=yes ],\n\t\t     [pkg_failed=yes])\n else\n    pkg_failed=untried\nfi[]dnl\n])# _PKG_CONFIG\n\n# _PKG_SHORT_ERRORS_SUPPORTED\n# -----------------------------\nAC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED],\n[AC_REQUIRE([PKG_PROG_PKG_CONFIG])\nif $PKG_CONFIG --atleast-pkgconfig-version 0.20; then\n        _pkg_short_errors_supported=yes\nelse\n        _pkg_short_errors_supported=no\nfi[]dnl\n])# _PKG_SHORT_ERRORS_SUPPORTED\n\n\n# PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],\n# [ACTION-IF-NOT-FOUND])\n#\n#\n# Note that if there is a possibility the first call to\n# PKG_CHECK_MODULES might not happen, you should be sure to include an\n# explicit call to PKG_PROG_PKG_CONFIG in your configure.ac\n#\n#\n# --------------------------------------------------------------\nAC_DEFUN([PKG_CHECK_MODULES],\n[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl\nAC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl\nAC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl\n\npkg_failed=no\nAC_MSG_CHECKING([for $1])\n\n_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])\n_PKG_CONFIG([$1][_LIBS], [libs], [$2])\n\nm4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS\nand $1[]_LIBS to avoid the need to call pkg-config.\nSee the pkg-config man page for more details.])\n\nif test $pkg_failed = yes; then\n   \tAC_MSG_RESULT([no])\n        _PKG_SHORT_ERRORS_SUPPORTED\n        if test $_pkg_short_errors_supported = yes; then\n\t        $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs \"$2\" 2>&1`\n        else \n\t        $1[]_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs \"$2\" 2>&1`\n        fi\n\t# Put the nasty error message in config.log where it belongs\n\techo \"$$1[]_PKG_ERRORS\" >&AS_MESSAGE_LOG_FD\n\n\tm4_default([$4], [AC_MSG_ERROR(\n[Package requirements ($2) were not met:\n\n$$1_PKG_ERRORS\n\nConsider adjusting the PKG_CONFIG_PATH environment variable if you\ninstalled software in a non-standard prefix.\n\n_PKG_TEXT])[]dnl\n        ])\nelif test $pkg_failed = untried; then\n     \tAC_MSG_RESULT([no])\n\tm4_default([$4], [AC_MSG_FAILURE(\n[The pkg-config script could not be found or is too old.  Make sure it\nis in your PATH or set the PKG_CONFIG environment variable to the full\npath to pkg-config.\n\n_PKG_TEXT\n\nTo get pkg-config, see <http://pkg-config.freedesktop.org/>.])[]dnl\n        ])\nelse\n\t$1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS\n\t$1[]_LIBS=$pkg_cv_[]$1[]_LIBS\n        AC_MSG_RESULT([yes])\n\t$3\nfi[]dnl\n])# PKG_CHECK_MODULES\n"
  },
  {
    "path": "macosx/.gitignore",
    "content": "/mosh-package.pmdoc\n/Mosh*.pkg\n/prefix*/\n/Distribution\n/Resources/\n"
  },
  {
    "path": "macosx/Distribution.in",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<installer-script minSpecVersion=\"1.000000\" authoringTool=\"com.apple.PackageMaker\" authoringToolVersion=\"3.0.6\" authoringToolBuild=\"201\">\n    <title>@PACKAGE_VERSION@</title>\n    <options customize=\"never\" allow-external-scripts=\"no\"/>\n    <domains enable_anywhere=\"true\"/>\n    <readme file=\"ReadMe\"/>\n    <license file=\"License\"/>\n    <choices-outline>\n        <line choice=\"choice0\"/>\n    </choices-outline>\n    <choice id=\"choice0\" title=\"Default\" customLocation=\"/usr\">\n        <pkg-ref id=\"edu.mit.mosh.mosh.pkg\"/>\n    </choice>\n    <pkg-ref id=\"edu.mit.mosh.mosh.pkg\" installKBytes=\"2204\" version=\"@PACKAGE_VERSION@\" auth=\"Root\">#edu.mit.mosh.mosh.pkg</pkg-ref>\n</installer-script>\n"
  },
  {
    "path": "macosx/brew-deps.sh",
    "content": "#!/bin/sh\n\n#\n# Install Homebrew dependencies\n#\n# This script handles build dependencies other than those provided by\n# MacOS and Xcode, for a Mosh build using macosx/build.sh or the\n# native autoconf/automake build for CI.  It is intended to be used by\n# a build system, and should be agnostic to any particular system.\n#\n# Similar scripts could be developed for MacPorts, direct dependency\n# builds, etc.\n#\n\n#\n# Install and/or configure the system used to provide dependencies.\n#\ninstall()\n{\n    # Straight from https://brew.sh\n    if ! brew --version > /dev/null 2>&1; then\n\t/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"\n    fi\n}\n\n#\n# Install up-to-date build dependencies required for a development or\n# CI build.  These dependencies only need to provide runtime\n# dependencies for the build system, support for things like previous\n# OS versions and fat binaries is not needed.\n#\ndeps()\n{\n    brew update\n    brew update\n    brew upgrade\n    brew reinstall tmux\n    brew reinstall protobuf\n}\n\n#\n# Install build dependencies required for the MacOS package build.\n# Runtime dependencies are required to support the targeted OS X\n# version, static libraries, and fat binaries for the package build.\n#\n# This reinstalls protobuf with --universal --build-bottle to get a\n# fat library that will run on any machine.  (This takes about 15\n# minutes on current Travis infrastructure.)\n#\npackage_deps()\n{\n    deps\n    brew rm protobuf\n    brew install protobuf --universal --build-bottle\n}\n\n#\n# Describe the dependencies installed and used as best as possible.\n#\ndescribe()\n{\n    brew --version > brew-version.txt\n    brew info --json=v1 --installed > brew-info.json\n}\n\n#\n# Do something.\n#\nset -e\n\"$@\"\n"
  },
  {
    "path": "macosx/build.sh",
    "content": "#!/bin/bash\n\n#\n# This script is known to work on:\n# OS X 10.5.8, Xcode 3.1.2, SDK 10.5, MacPorts 2.3.3\n# OS X 10.9.5, Xcode 5.1.1, SDK 10.9, MacPorts 2.3.2\n# OS X 10.10.3, XCode 6.3.2, SDK 10.10, Homebrew 0.9.5/8da6986\n#\n# You may need to set PATH to include the location of your\n# PackageMaker binary, if your system is old enough to need that.\n# Setting MACOSX_DEPLOYMENT_TARGET will select an SDK as usual.\n#\n# If you are using Homebrew, you should install protobuf (and any\n# other future Homebrew dependencies) with\n# `--universal --build-bottle`.\n# The first option should be fairly obvious; the second has the side\n# effect of disabling Homebrew's overzealous processor optimization\n# with (effectively) `-march=native`.\n#\n\nset -e\n\nprotobuf_LIBS=$(l=libprotobuf.a; for i in /opt/local/lib /usr/local/lib; do  if [ -f $i/$l ]; then echo $i/$l; fi; done)\nif [ -z \"$protobuf_LIBS\" ]; then echo \"Can't find libprotobuf.a\"; exit 1; fi\nexport protobuf_LIBS\nif ! pkg-config --cflags protobuf > /dev/null 2>&1; then\n    protobuf_CFLAGS=-I$(for i in /opt /usr; do d=$i/local/include; if [ -d $d/google/protobuf ]; then echo $d; fi; done)\n    if [ \"$protobuf_CFLAGS\" = \"-I\" ]; then echo \"Can't find protobuf includes\"; exit 1; fi\n    export protobuf_CFLAGS\nfi\n\necho \"Building into prefix...\"\n\n\n#\n# XXX This script abuses Configure's --prefix argument badly.  It uses\n# it as a $DESTDIR, but --prefix can also affect paths in generated\n# objects.  That is not *currently* a problem in mosh.\n#\nPREFIX=\"$(pwd)/prefix\"\n\nHOST=\"x86_64-apple-macosx${MACOSX_DEPLOYMENT_TARGET}\"\nARCH_TRIPLES=\"x86_64-apple-macosx arm64-apple-macos\"\n\npushd .. > /dev/null\n\nif [ ! -f configure ];\nthen\n    echo \"Running autogen.\"\n    PATH=/opt/local/bin:$PATH ./autogen.sh\nfi\n\n#\n# Build archs one by one.\n#\nfor triple in $ARCH_TRIPLES; do\n    arch=$(echo $triple | cut -d- -f1)\n    echo \"Building for ${arch}...\"\n    prefix=\"${PREFIX}_${arch}\"\n    rm -rf \"${prefix}\"\n    mkdir \"${prefix}\"\n    if ./configure --prefix=\"${prefix}/local\" --build=\"${triple}${MACOSX_DEPLOYMENT_TARGET}\"\\\n\t\t   --host=\"${HOST}\" \\\n\t\t   CC=\"cc -arch ${arch}\" CPP=\"cc -arch ${arch} -E\" CXX=\"c++ -arch ${arch}\" \\\n\t\t   TINFO_LIBS=-lncurses &&\n\t    make clean &&\n\t    make install -j8 V=1 &&\n\t    rm -f \"${prefix}/etc\"\n    then\n\t# mosh-client built with Xcode 3.1.2 bus-errors if the binary is stripped.\n\t# strip \"${prefix}/local/bin/mosh-client\" \"${prefix}/local/bin/mosh-server\"\n\tBUILT_ARCHS=\"$BUILT_ARCHS $arch\"\n    fi\ndone\n\nif [ -z \"$BUILT_ARCHS\" ]; then\n    echo \"No architectures built successfully\"\n    exit 1\nfi\n\necho \"Building universal binaries for archs ${BUILT_ARCHS}...\"\n\n\nrm -rf \"$PREFIX\"\n# Copy one architecture to get all files into place.\nfor arch in $BUILT_ARCHS; do\n    cp -Rp \"${PREFIX}_${arch}\" \"${PREFIX}\"\n    break\ndone\n\n# Build fat binaries\n# XXX will break with spaces in pathname\nfor prog in local/bin/mosh-client local/bin/mosh-server; do\n    archprogs=()\n    for arch in $BUILT_ARCHS; do\n\tarchprogs+=(\"${PREFIX}_${arch}/$prog\")\n    done\n    lipo -create \"${archprogs[@]}\" -output \"${PREFIX}/$prog\"\ndone\n\nperl -wlpi -e 's{#!/usr/bin/env perl}{#!/usr/bin/perl}' \"$PREFIX/local/bin/mosh\"\n\npopd > /dev/null\n\nPACKAGE_VERSION=$(cat ../VERSION.stamp)\n\nOUTFILE=\"$PACKAGE_VERSION.pkg\"\n\nrm -f \"$OUTFILE\"\n\nif which -s pkgbuild; then\n    # To replace PackageMaker, you:\n    # * make a bare package with the build products\n    # * essentially take the Distribution file that PackageMaker generated and\n    #   use it as the --distribution input file for productbuild\n    echo \"Preprocessing package description...\"\n    PKGID=edu.mit.mosh.mosh.pkg\n    for file in Distribution; do\n\tsed -e \"s/@PACKAGE_VERSION@/${PACKAGE_VERSION}/g\" ${file}.in > ${file}\n    done\n    echo \"Running pkgbuild/productbuild...\"\n    mkdir -p Resources/en.lproj\n    cp -p copying.rtf Resources/en.lproj/License\n    cp -p readme.rtf Resources/en.lproj/Readme\n    pkgbuild --root \"$PREFIX\" --identifier $PKGID $PKGID\n    productbuild --distribution Distribution \\\n\t\t --resources Resources \\\n\t\t --package-path . \\\n\t\t \"$OUTFILE\"\n    echo \"Cleaning up...\"\n    rm -rf $PKGID\nelse\n    echo \"Preprocessing package description...\"\n    INDIR=mosh-package.pmdoc.in\n    OUTDIR=mosh-package.pmdoc\n    mkdir -p \"$OUTDIR\"\n    pushd \"$INDIR\" > /dev/null\n    for file in *\n    do\n\tsed -e 's/$PACKAGE_VERSION/'\"$PACKAGE_VERSION\"'/g' \"$file\" > \"../$OUTDIR/$file\"\n    done\n    popd > /dev/null\n    echo \"Running PackageMaker...\"\n    env PATH=\"/Applications/PackageMaker.app/Contents/MacOS:/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS:$PATH\" PackageMaker -d mosh-package.pmdoc -o \"$OUTFILE\" -i edu.mit.mosh.mosh.pkg\n    echo \"Cleaning up...\"\n    rm -rf \"$OUTDIR\"\nfi\n\n\nif [ -f \"$OUTFILE\" ];\nthen\n    echo \"Successfully built $OUTFILE with archs ${BUILT_ARCHS}.\"\nelse\n    echo \"There was an error building $OUTFILE.\"\n    false\nfi\n"
  },
  {
    "path": "macosx/copying.rtf",
    "content": "{\\rtf1\\ansi\\ansicpg1252\\cocoartf1138\\cocoasubrtf320\n{\\fonttbl\\f0\\fnil\\fcharset0 LucidaGrande;}\n{\\colortbl;\\red255\\green255\\blue255;}\n\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\n\n\\f0\\fs26 \\cf0                     GNU GENERAL PUBLIC LICENSE\\\n                       Version 3, 29 June 2007\\\n\\\n Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\\\n Everyone is permitted to copy and distribute verbatim copies\\\n of this license document, but changing it is not allowed.\\\n\\\n                            Preamble\\\n\\\n  The GNU General Public License is a free, copyleft license for software and other kinds of works.\\\n\\\n  The licenses for most software and other practical works are designed to take away your freedom to share and change the works.  By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.  We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors.  You can apply it to your programs, too.\\\n\\\n  When we speak of free software, we are referring to freedom, not price.  Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.\\\n\\\n  To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights.  Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others.\\\n\\\n  For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received.  You must make sure that they, too, receive or can get the source code.  And you must show them these terms so they know their rights.\\\n\\\n  Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it.\\\n\\\n  For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software.  For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions.\\\n\\\n  Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so.  This is fundamentally incompatible with the aim of protecting users' freedom to change the software.  The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable.  Therefore, we have designed this version of the GPL to prohibit the practice for those products.  If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users.\\\n\\\n  Finally, every program is threatened constantly by software patents.  States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary.  To prevent this, the GPL assures that patents cannot be used to render the program non-free.\\\n\\\n  The precise terms and conditions for copying, distribution and modification follow.\\\n\\\n                       TERMS AND CONDITIONS\\\n\\\n  0. Definitions.\\\n\\\n  \"This License\" refers to version 3 of the GNU General Public License.\\\n\\\n  \"Copyright\" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.\\\n\\\n  \"The Program\" refers to any copyrightable work licensed under this License.  Each licensee is addressed as \"you\".  \"Licensees\" and \"recipients\" may be individuals or organizations.\\\n\\\n  To \"modify\" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy.  The resulting work is called a \"modified version\" of the earlier work or a work \"based on\" the earlier work.\\\n\\\n  A \"covered work\" means either the unmodified Program or a work based on the Program.\\\n\\\n  To \"propagate\" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy.  Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.\\\n\\\n  To \"convey\" a work means any kind of propagation that enables other parties to make or receive copies.  Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.\\\n\\\n  An interactive user interface displays \"Appropriate Legal Notices\" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License.  If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.\\\n\\\n  1. Source Code.\\\n\\\n  The \"source code\" for a work means the preferred form of the work for making modifications to it.  \"Object code\" means any non-source form of a work.\\\n\\\n  A \"Standard Interface\" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.\\\n\\\n  The \"System Libraries\" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form.  A \"Major Component\", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.\\\n\\\n  The \"Corresponding Source\" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities.  However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work.  For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.\\\n\\\n  The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.\\\n\\\n  The Corresponding Source for a work in source code form is that same work.\\\n\\\n  2. Basic Permissions.\\\n\\\n  All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met.  This License explicitly affirms your unlimited permission to run the unmodified Program.  The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work.  This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.\\\n\\\n  You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force.  You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright.  Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.\\\n\\\n  Conveying under any other circumstances is permitted solely under the conditions stated below.  Sublicensing is not allowed; section 10 makes it unnecessary.\\\n\\\n  3. Protecting Users' Legal Rights From Anti-Circumvention Law.\\\n\\\n  No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.\\\n\\\n  When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.\\\n\\\n  4. Conveying Verbatim Copies.\\\n\\\n  You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.\\\n\\\n  You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.\\\n\\\n  5. Conveying Modified Source Versions.\\\n\\\n  You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:\\\n\\\n    a) The work must carry prominent notices stating that you modified it, and giving a relevant date.\\\n\\\n    b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7.  This requirement modifies the requirement in section 4 to \"keep intact all notices\".\\\n\\\n    c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy.  This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged.  This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.\\\n\\\n    d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.\\\n\\\n  A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an \"aggregate\" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit.  Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.\\\n\\\n  6. Conveying Non-Source Forms.\\\n\\\n  You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:\\\n\\\n    a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.\\\n\\\n    b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.\\\n\\\n    c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source.  This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.\\\n\\\n    d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge.  You need not require recipients to copy the Corresponding Source along with the object code.  If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source.  Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.\\\n\\\n    e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.\\\n\\\n  A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.\\\n\\\n  A \"User Product\" is either (1) a \"consumer product\", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling.  In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage.  For a particular product received by a particular user, \"normally used\" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product.  A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.\\\n\\\n  \"Installation Information\" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source.  The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.\\\n\\\n  If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information.  But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).\\\n\\\n  The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed.  Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.\\\n\\\n  Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.\\\n\\\n  7. Additional Terms.\\\n\\\n  \"Additional permissions\" are terms that supplement the terms of this License by making exceptions from one or more of its conditions.  Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law.  If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.\\\n\\\n  When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it.  (Additional permissions may be written to require their own removal in certain cases when you modify the work.)  You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.\\\n\\\n  Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:\\\n\\\n    a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or\\\n\\\n    b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or\\\n\\\n    c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or\\\n\\\n    d) Limiting the use for publicity purposes of names of licensors or authors of the material; or\\\n\\\n    e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or\\\n\\\n    f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.\\\n\\\n  All other non-permissive additional terms are considered \"further restrictions\" within the meaning of section 10.  If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term.  If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.\\\n\\\n  If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.\\\n\\\n  Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.\\\n\\\n  8. Termination.\\\n\\\n  You may not propagate or modify a covered work except as expressly provided under this License.  Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).\\\n\\\n  However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.\\\n\\\n  Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.\\\n\\\n  Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License.  If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.\\\n\\\n  9. Acceptance Not Required for Having Copies.\\\n\\\n  You are not required to accept this License in order to receive or run a copy of the Program.  Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance.  However, nothing other than this License grants you permission to propagate or modify any covered work.  These actions infringe copyright if you do not accept this License.  Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.\\\n\\\n  10. Automatic Licensing of Downstream Recipients.\\\n\\\n  Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License.  You are not responsible for enforcing compliance by third parties with this License.\\\n\\\n  An \"entity transaction\" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations.  If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.\\\n\\\n  You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License.  For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.\\\n\\\n  11. Patents.\\\n\\\n  A \"contributor\" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based.  The work thus licensed is called the contributor's \"contributor version\".\\\n\\\n  A contributor's \"essential patent claims\" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version.  For purposes of this definition, \"control\" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.\\\n\\\n  Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.\\\n\\\n  In the following three paragraphs, a \"patent license\" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement).  To \"grant\" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.\\\n\\\n  If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients.  \"Knowingly relying\" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.\\\n\\\n  If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.\\\n\\\n  A patent license is \"discriminatory\" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License.  You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.\\\n\\\n  Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.\\\n\\\n  12. No Surrender of Others' Freedom.\\\n\\\n  If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License.  If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all.  For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.\\\n\\\n  13. Use with the GNU Affero General Public License.\\\n\\\n  Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work.  The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.\\\n\\\n  14. Revised Versions of this License.\\\n\\\n  The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time.  Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.\\\n\\\n  Each version is given a distinguishing version number.  If the Program specifies that a certain numbered version of the GNU General Public License \"or any later version\" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation.  If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.\\\n\\\n  If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.\\\n\\\n  Later license versions may give you additional or different permissions.  However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.\\\n\\\n  15. Disclaimer of Warranty.\\\n\\\n  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\\\n\\\n  16. Limitation of Liability.\\\n\\\n  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\\\n\\\n  17. Interpretation of Sections 15 and 16.\\\n\\\n  If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.\\\n\\\n                     END OF TERMS AND CONDITIONS\\\n\\\n            How to Apply These Terms to Your New Programs\\\n\\\n  If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.\\\n\\\n  To do so, attach the following notices to the program.  It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the \"copyright\" line and a pointer to where the full notice is found.\\\n\\\n    <one line to give the program's name and a brief idea of what it does.>\\\n    Copyright (C) <year>  <name of author>\\\n\\\n    This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\\\n\\\n    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\\\n\\\n    You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.\\\n\\\nAlso add information on how to contact you by electronic and paper mail.\\\n\\\n  If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:\\\n\\\n    <program>  Copyright (C) <year>  <name of author> This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.  This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.\\\n\\\nThe hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License.  Of course, your program's commands might be different; for a GUI interface, you would use an \"about box\".\\\n\\\n  You should also get your employer (if you work as a programmer) or school, if any, to sign a \"copyright disclaimer\" for the program, if necessary.  For more information on this, and how to apply and follow the GNU GPL, see <http://www.gnu.org/licenses/>.\\\n\\\nThe GNU General Public License does not permit incorporating your program into proprietary programs.  If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library.  If this is what you want to do, use the GNU Lesser General Public License instead of this License.  But first, please read <http://www.gnu.org/philosophy/why-not-lgpl.html>.}\n"
  },
  {
    "path": "macosx/gtar",
    "content": "#!/bin/sh\nexec sudo /usr/local/bin/gtar.orig \"$@\"\n"
  },
  {
    "path": "macosx/mosh-package.pmdoc.in/01prefix-contents.xml",
    "content": "<pkg-contents spec=\"1.12\">\n  <f n=\"prefix\" o=\"root\" g=\"wheel\" p=\"16877\" pt=\"prefix\" m=\"false\" t=\"file\">\n    <f n=\"local\" o=\"root\" g=\"wheel\" p=\"16877\">\n      <f n=\"bin\" o=\"root\" g=\"wheel\" p=\"16877\">\n        <f n=\"mosh\" o=\"root\" g=\"wheel\" p=\"33261\">\n          <mod>group</mod>\n          <mod>owner</mod>\n        </f>\n        <f n=\"mosh-client\" o=\"root\" g=\"wheel\" p=\"33261\">\n          <mod>group</mod>\n          <mod>owner</mod>\n        </f>\n        <f n=\"mosh-server\" o=\"root\" g=\"wheel\" p=\"33261\">\n          <mod>group</mod>\n          <mod>owner</mod>\n        </f>\n        <mod>group</mod>\n        <mod>owner</mod>\n      </f>\n      <f n=\"share\" o=\"root\" g=\"wheel\" p=\"16877\">\n        <f n=\"man\" o=\"root\" g=\"wheel\" p=\"16877\">\n          <f n=\"man1\" o=\"root\" g=\"wheel\" p=\"16877\">\n            <f n=\"mosh-client.1\" o=\"root\" g=\"wheel\" p=\"33188\">\n              <mod>group</mod>\n              <mod>owner</mod>\n            </f>\n            <f n=\"mosh-server.1\" o=\"root\" g=\"wheel\" p=\"33188\">\n              <mod>group</mod>\n              <mod>owner</mod>\n            </f>\n            <f n=\"mosh.1\" o=\"root\" g=\"wheel\" p=\"33188\">\n              <mod>group</mod>\n              <mod>owner</mod>\n            </f>\n            <mod>group</mod>\n            <mod>owner</mod>\n          </f>\n          <mod>group</mod>\n          <mod>owner</mod>\n        </f>\n        <mod>group</mod>\n        <mod>owner</mod>\n      </f>\n      <mod>group</mod>\n      <mod>owner</mod>\n    </f>\n    <mod>group</mod>\n    <mod>owner</mod>\n  </f>\n</pkg-contents>\n"
  },
  {
    "path": "macosx/mosh-package.pmdoc.in/01prefix.xml",
    "content": "<pkgref spec=\"1.12\" uuid=\"38E2F0D0-8573-4E7B-8A50-A86056DD0EE8\">\n    <config>\n        <identifier>edu.mit.mosh.mosh.pkg</identifier>\n        <version>$PACKAGE_VERSION</version>\n        <description></description>\n        <post-install type=\"none\"/>\n        <requireAuthorization/>\n        <installFrom relative=\"true\" mod=\"true\">prefix</installFrom>\n        <installTo mod=\"true\">/usr</installTo>\n        <flags>\n            <followSymbolicLinks/>\n        </flags>\n        <packageStore type=\"internal\">\n        </packageStore>\n        <mod>installTo</mod>\n        <mod>relocatable</mod>\n        <mod>installFrom.path</mod>\n        <mod>identifier</mod>\n        <mod>parent</mod>\n        <mod>includeRoot</mod>\n        <mod>installTo.path</mod>\n        <mod>installFrom.isRelativeType</mod>\n        <mod>version</mod>\n    </config>\n    <contents>\n        <file-list>01prefix-contents.xml</file-list>\n        <filter>/\\.gitignore$</filter>\n        <filter>/\\.DS_Store$</filter>\n    </contents>\n</pkgref>\n"
  },
  {
    "path": "macosx/mosh-package.pmdoc.in/index.xml",
    "content": "<pkmkdoc spec=\"1.12\">\n    <properties>\n        <title>Mosh $PACKAGE_VERSION</title>\n        <build>Mosh.pkg</build>\n        <organization>edu.mit.mosh</organization>\n        <userSees ui=\"easy\"/>\n        <min-target os=\"3\"/>\n        <domain anywhere=\"true\"/>\n    </properties>\n    <distribution>\n        <versions min-spec=\"1.000000\"/>\n        <scripts></scripts>\n    </distribution>\n    <description>Mosh is a remote terminal application that supports intermittent connectivity, allows roaming, and provides speculative local echo and line editing of user keystrokes.</description>\n    <contents>\n        <choice title=\"Default\" id=\"choice0\" starts_selected=\"true\" starts_enabled=\"true\" starts_hidden=\"false\">\n            <customLoc>/</customLoc>\n            <pkgref id=\"edu.mit.mosh.mosh.pkg\"/>\n        </choice>\n    </contents>\n    <resources bg-scale=\"none\" bg-align=\"topleft\">\n        <locale lang=\"en\">\n            <resource relative=\"true\" mod=\"true\" type=\"license\">copying.rtf</resource>\n            <resource relative=\"true\" mod=\"true\" type=\"readme\">readme.rtf</resource>\n        </locale>\n    </resources>\n    <flags/>\n    <item type=\"file\">01prefix.xml</item>\n    <mod>properties.title</mod>\n    <mod>properties.userDomain</mod>\n    <mod>description</mod>\n</pkmkdoc>\n"
  },
  {
    "path": "macosx/osx-xcode.sh",
    "content": "#!/bin/sh\n\n#\n# OS X and Xcode support script.\n#\n\n#\n# Describe the OS X and Xcode installation, patches, etc as best as possible.\n#\n# Beware: System Profiler dumps significant private and security information.\n#\ndescribe()\n{\n    # Most of the XML in this report is plist files, which can be read more easily with plutil -p\n    pkgutil --pkgs-plist > packages-plist.xml\n    mkdir package-info-plist/\n    for i in $(pkgutil --pkgs); do pkgutil --pkg-info-plist=$i > package-info-plist/$i.xml; done\n    xcodebuild -version > xcodebuild-version.txt\n    # CLT info can be found in package-info-plist/com.apple.pkg.CLTools_Executables.xml\n    xcode-select --print-path > xcode-path.txt\n    # System Profiler's XML can be read more easily with plutil -p, or\n    # opened with the System Profiler GUI.\n    system_profiler -xml -detailLevel full > system-profile.spx 2>/dev/null\n}\n\n#\n# Do something.\n#\nset -e\n\"$@\"\n"
  },
  {
    "path": "macosx/port-deps.sh",
    "content": "describe() {\n    /opt/local/bin/port version > port-version.txt\n    /opt/local/bin/port -v -q installed > port-packages.txt\n}\n\ndeps() {\n    # Set up macports to respect the deployment target before building universal packages\n    echo \"macosx_deployment_target 11.0\" | sudo tee -a /opt/local/etc/macports/macports.conf\n    sudo /opt/local/bin/port install protobuf3-cpp +universal\n    sudo /opt/local/bin/port install ncurses +universal\n    sudo /opt/local/bin/port install pkgconfig\n    sudo /opt/local/bin/port install autoconf automake\n}\n\nset -e\n\"$@\"\n"
  },
  {
    "path": "macosx/readme.rtf",
    "content": "{\\rtf1\\ansi\\ansicpg1252\\cocoartf1138\\cocoasubrtf320\n{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;\\f1\\fmodern\\fcharset0 Courier;}\n{\\colortbl;\\red255\\green255\\blue255;\\red38\\green38\\blue38;\\red246\\green246\\blue246;\\red51\\green108\\blue190;\n}\n{\\*\\listtable{\\list\\listtemplateid1\\listhybrid{\\listlevel\\levelnfc23\\levelnfcn23\\leveljc0\\leveljcn0\\levelfollow0\\levelstartat1\\levelspace360\\levelindent0{\\*\\levelmarker \\{disc\\}}{\\leveltext\\leveltemplateid1\\'01\\uc0\\u8226 ;}{\\levelnumbers;}\\fi-360\\li720\\lin720 }{\\listname ;}\\listid1}\n{\\list\\listtemplateid2\\listhybrid{\\listlevel\\levelnfc23\\levelnfcn23\\leveljc0\\leveljcn0\\levelfollow0\\levelstartat1\\levelspace360\\levelindent0{\\*\\levelmarker \\{disc\\}}{\\leveltext\\leveltemplateid101\\'01\\uc0\\u8226 ;}{\\levelnumbers;}\\fi-360\\li720\\lin720 }{\\listname ;}\\listid2}\n{\\list\\listtemplateid3\\listhybrid{\\listlevel\\levelnfc23\\levelnfcn23\\leveljc0\\leveljcn0\\levelfollow0\\levelstartat1\\levelspace360\\levelindent0{\\*\\levelmarker \\{disc\\}}{\\leveltext\\leveltemplateid201\\'01\\uc0\\u8226 ;}{\\levelnumbers;}\\fi-360\\li720\\lin720 }{\\listname ;}\\listid3}}\n{\\*\\listoverridetable{\\listoverride\\listid1\\listoverridecount0\\ls1}{\\listoverride\\listid2\\listoverridecount0\\ls2}{\\listoverride\\listid3\\listoverridecount0\\ls3}}\n\\deftab720\n\\pard\\pardeftab720\\sa200\n\n\\f0\\b\\fs56 \\cf0 Mosh: the mobile shell\\\n\\pard\\pardeftab720\\sa300\n\n\\b0\\fs28 \\cf2 Mosh is a remote terminal application that supports intermittent connectivity, allows roaming, and provides speculative local echo and line editing of user keystrokes.\\\nIt aims to support the typical interactive uses of SSH, plus:\\\n\\pard\\tx220\\tx720\\pardeftab720\\li720\\fi-720\n\\ls1\\ilvl0\\cf2 {\\listtext\t\\'95\t}Mosh keeps the session alive if the client goes to sleep and wakes up later, or temporarily loses its Internet connection.\\\n{\\listtext\t\\'95\t}Mosh allows the client and server to \"roam\" and change IP addresses, while keeping the connection alive. Unlike SSH, Mosh can be used while switching between Wi-Fi networks or from Wi-Fi to cellular data to wired Ethernet.\\\n\\pard\\tx220\\tx720\\pardeftab720\\li720\\fi-720\\sa300\n\\ls1\\ilvl0\\cf2 {\\listtext\t\\'95\t}The Mosh client runs a predictive model of the server's behavior in the background and tries to guess intelligently how each keystroke will affect the screen state. When it is confident in its predictions, it will show them to the user while waiting for confirmation from the server. Most typing and uses of the left- and right-arrow keys can be echoed immediately.\\uc0\\u8232 As a result, Mosh is usable on high-latency links, e.g. on a cellular data connection or spotty Wi-Fi. In distinction from previous attempts at local echo modes in other protocols, Mosh works properly with full-screen applications such as emacs, vi, alpine, and irssi, and automatically recovers from occasional prediction errors within an RTT. On high-latency links, Mosh underlines its predictions while they are outstanding and removes the underline when they are confirmed by the server.\\\n\\pard\\pardeftab720\\sa300\n\\cf2 Mosh does not support X forwarding or the non-interactive uses of SSH, including port forwarding.\\\n\\pard\\pardeftab720\\sa200\n\n\\b\\fs48 \\cf0 Other features\\\n\\pard\\tx220\\tx720\\pardeftab720\\li720\\fi-720\n\\ls2\\ilvl0\n\\b0\\fs28 \\cf2 {\\listtext\t\\'95\t}Mosh adjusts its frame rate so as not to fill up network queues on slow links, so \"Control-C\" always works within an RTT to halt a runaway process.\\\n{\\listtext\t\\'95\t}Mosh warns the user when it has not heard from the server in a while.\\\n{\\listtext\t\\'95\t}Mosh supports lossy links that lose a significant fraction of their packets.\\\n{\\listtext\t\\'95\t}Mosh handles some Unicode edge cases better than SSH and existing terminal emulators by themselves, but requires a UTF-8 environment to run.\\\n{\\listtext\t\\'95\t}Mosh leverages SSH to set up the connection and authenticate users. Mosh does not contain any privileged (root) code.\\\n\\\n\\pard\\pardeftab720\\sa200\n\n\\b\\fs48 \\cf0 Usage\\\n\\pard\\pardeftab720\\sa300\n\n\\b0\\fs28 \\cf2 The \n\\f1\\fs24 \\cb3 mosh-client\n\\f0\\fs28 \\cb1  binary must be installed on the user's machine, and the \n\\f1\\fs24 \\cb3 mosh-server\n\\f0\\fs28 \\cb1  binary on the remote host.\\\nThe user runs:\\\n\\pard\\pardeftab720\n\n\\f1\\fs24 \\cf2 \\cb3 $ mosh [user@]host\\\n\\pard\\pardeftab720\\sa300\n\n\\f0\\fs28 \\cf2 \\cb1 A command may also be specified, for example:\\\n\\pard\\pardeftab720\n\n\\f1\\fs24 \\cf2 \\cb3 $ mosh host -- screen -r\\\n\\pard\\pardeftab720\\sa300\n\n\\f0\\fs28 \\cf2 \\cb1 If the \n\\f1\\fs24 \\cb3 mosh-client\n\\f0\\fs28 \\cb1  or \n\\f1\\fs24 \\cb3 mosh-server\n\\f0\\fs28 \\cb1  binaries are installed outside the user's PATH, \n\\f1\\fs24 \\cb3 mosh\n\\f0\\fs28 \\cb1  accepts the arguments \n\\f1\\fs24 \\cb3 --client=PATH\n\\f0\\fs28 \\cb1  and \n\\f1\\fs24 \\cb3 --server=PATH\n\\f0\\fs28 \\cb1  to select alternate locations. More options are documented in the mosh(1) manual page.\\\nMosh supports 256-color mode as long as the user's own terminal does. Generally this means the \n\\f1\\fs24 \\cb3 TERM\n\\f0\\fs28 \\cb1  environment variable must be set to \n\\f1\\fs24 \\cb3 xterm-256color\n\\f0\\fs28 \\cb1  or \n\\f1\\fs24 \\cb3 screen-256color-bce\n\\f0\\fs28 \\cb1  before running \n\\f1\\fs24 \\cb3 mosh\n\\f0\\fs28 \\cb1 .\\\n\\pard\\pardeftab720\\sa200\n\n\\b\\fs48 \\cf0 How it works\\\n\\pard\\pardeftab720\\sa300\n\n\\b0\\fs28 \\cf2 The \n\\f1\\fs24 \\cb3 mosh\n\\f0\\fs28 \\cb1  program will SSH to \n\\f1\\fs24 \\cb3 user@host\n\\f0\\fs28 \\cb1  to establish the connection. SSH may prompt the user for a password or use public-key authentication to log in.\\\nFrom this point, \n\\f1\\fs24 \\cb3 mosh\n\\f0\\fs28 \\cb1  runs the \n\\f1\\fs24 \\cb3 mosh-server\n\\f0\\fs28 \\cb1  process (as the user) on the server machine. The server process listens on a high UDP port and sends its port number and an AES-128 secret key back to the client over SSH. The SSH connection is then shut down and the terminal session begins over UDP.\\\nIf the client changes IP addresses, the server will begin sending to the client on the new IP address within a few seconds.\\\nTo function, Mosh requires UDP datagrams to be passed between client and server. By default, \n\\f1\\fs24 \\cb3 mosh\n\\f0\\fs28 \\cb1  uses a port number between 60000 and 61000, but the user can select a particular port with the -p option. Please note that the -p option has no effect on the port used by SSH.\\\n\\pard\\pardeftab720\\sa200\n\n\\b\\fs48 \\cf0 More info\\\n\\pard\\tx220\\tx720\\pardeftab720\\li720\\fi-720\\sa300\n\\ls3\\ilvl0\n\\b0\\fs28 \\cf2 {\\listtext\t\\'95\t}Mosh Web site:\\uc0\\u8232 {\\field{\\*\\fldinst{HYPERLINK \"https://mosh.org/\"}}{\\fldrslt \\cf4 https://mosh.org}}\\\n\\pard\\tx220\\tx720\\pardeftab720\\li720\\fi-720\\sa300\n\\ls3\\ilvl0\n\\f1\\fs24 \\cf2 \\cb3 {\\listtext\t\\'95\t}mosh-devel@mit.edu\n\\f0\\fs28 \\cb1  mailing list:\\uc0\\u8232 {\\field{\\*\\fldinst{HYPERLINK \"http://mailman.mit.edu/mailman/listinfo/mosh-devel\"}}{\\fldrslt \\cf4 http://mailman.mit.edu/mailman/listinfo/mosh-devel}}\\\n\\ls3\\ilvl0\n\\f1\\fs24 \\cb3 {\\listtext\t\\'95\t}mosh-users@mit.edu\n\\f0\\fs28 \\cb1  mailing list:\\uc0\\u8232 {\\field{\\*\\fldinst{HYPERLINK \"http://mailman.mit.edu/mailman/listinfo/mosh-users\"}}{\\fldrslt \\cf4 http://mailman.mit.edu/mailman/listinfo/mosh-users}}}\n"
  },
  {
    "path": "man/Makefile.am",
    "content": "dist_man_MANS =\n\nif BUILD_CLIENT\n  dist_man_MANS += mosh.1 mosh-client.1\nendif\n\nif BUILD_SERVER\n  dist_man_MANS += mosh-server.1\nendif\n"
  },
  {
    "path": "man/mosh-client.1",
    "content": ".\\\"                                      Hey, EMACS: -*- nroff -*-\n.\\\" First parameter, NAME, should be all caps\n.\\\" Second parameter, SECTION, should be 1-8, maybe w/ subsection\n.\\\" other parameters are allowed: see man(7), man(1)\n.TH MOSH 1 \"February 2012\"\n.\\\" Please adjust this date whenever revising the manpage.\n.\\\"\n.\\\" Some roff macros, for reference:\n.\\\" .nh        disable hyphenation\n.\\\" .hy        enable hyphenation\n.\\\" .ad l      left justify\n.\\\" .ad b      justify to both left and right margins\n.\\\" .nf        disable filling\n.\\\" .fi        enable filling\n.\\\" .br        insert line break\n.\\\" .sp <n>    insert n+1 empty lines\n.\\\" for manpage-specific macros, see man(7)\n.SH NAME\nmosh-client \\- client-side helper for mosh\n.SH SYNOPSIS\nMOSH_KEY=KEY\n.B mosh-client \n[\\-v]\nIP PORT\n.br\n.B mosh-client \n\\-c\n.br\n.SH DESCRIPTION\n\\fBmosh-client\\fP is a helper program for the \n.BR mosh (1)\nremote terminal application.\n\n\\fBmosh\\fP itself is a setup script that establishes an SSH\nconnection, runs the server-side helper \\fBmosh-server\\fP,\nand collects the server's port number and session key.\n\n\\fBmosh\\fP then executes \\fBmosh-client\\fP with the server's IP\naddress, port, and session key. \\fBmosh-client\\fP runs for\nthe lifetime of the connection.\n\nThe 22-byte base64 session key given by \\fBmosh-server\\fP is supplied\nin the MOSH_KEY environment variable. This represents a 128-bit AES\nkey that protects the integrity and confidentiality of the session.\n\nFor constructing new setup wrappers for remote execution facilities\nother than SSH, it may be necessary to invoke \\fBmosh-client\\fP\ndirectly.\n\nWith the \\-c option, \\fBmosh-client\\fP instead prints the number of colors\nof the terminal given by the TERM environment variable.\n\nThe \\-v option will print some debugging information on standard\nerror.  More instances of this flag will result in more debugging\ninformation.  If standard error is not redirected from the terminal,\nthe display will be corrupted and quickly become unusable.\n\n.SH ENVIRONMENT VARIABLES\n\n.TP\n.B MOSH_KEY\nThis variable must be set, and must contain a Base64-encoded cryptographic key from\n.BR mosh-server (1).\n\n.TP\n.B MOSH_ESCAPE_KEY\nSee\n.BR mosh (1).\n\n.TP\n.B MOSH_PREDICTION_DISPLAY\nControls local echo as described in\n.BR mosh (1).\n\n.TP\n.B MOSH_PREDICTION_OVERWRITE\nControls local echo insert/overwrite as described in\n.BR mosh (1).\n\n.TP\n.B MOSH_TITLE_NOPREFIX\nSee\n.BR mosh (1).\n\n\n.SH SEE ALSO\n.BR mosh (1),\n.BR mosh-server (1).\n\nProject home page:\n.I https://mosh.org\n\n.br\n.SH AUTHOR\nmosh was written by Keith Winstein <mosh-devel@mit.edu>.\n.SH BUGS\nPlease report bugs to \\fImosh-devel@mit.edu\\fP. Users may also subscribe\nto the\n.nh\n.I mosh-users@mit.edu\n.hy\nmailing list, at\n.br\n.nh\n.I http://mailman.mit.edu/mailman/listinfo/mosh-users\n.hy\n.\n"
  },
  {
    "path": "man/mosh-server.1",
    "content": ".\\\"                                      Hey, EMACS: -*- nroff -*-\n.\\\" First parameter, NAME, should be all caps\n.\\\" Second parameter, SECTION, should be 1-8, maybe w/ subsection\n.\\\" other parameters are allowed: see man(7), man(1)\n.TH MOSH 1 \"October 2012\"\n.\\\" Please adjust this date whenever revising the manpage.\n.\\\"\n.\\\" Some roff macros, for reference:\n.\\\" .nh        disable hyphenation\n.\\\" .hy        enable hyphenation\n.\\\" .ad l      left justify\n.\\\" .ad b      justify to both left and right margins\n.\\\" .nf        disable filling\n.\\\" .fi        enable filling\n.\\\" .br        insert line break\n.\\\" .sp <n>    insert n+1 empty lines\n.\\\" for manpage-specific macros, see man(7)\n.SH NAME\nmosh-server \\- server-side helper for mosh\n.SH SYNOPSIS\n.B mosh-server\nnew\n[\\-s]\n[\\-v]\n[\\-i \\fIIP\\fP]\n[\\-p \\fIPORT\\fP[:\\fIPORT2\\fP]]\n[\\-c \\fICOLORS\\fP]\n[\\-\\- command...]\n.br\n.SH DESCRIPTION\n\\fBmosh-server\\fP is a helper program for the \n.BR mosh(1)\nremote terminal application.\n\n\\fBmosh-server\\fP binds to a high UDP port and chooses an encryption\nkey to protect the session. It prints both on standard output,\ndetaches from the terminal, and waits for the \\fBmosh-client\\fP to\nestablish a connection. It will exit if no client has contacted\nit within 60 seconds.\n\nBy default, \\fBmosh-server\\fP binds to a port between 60000 and\n61000 and executes the user's login shell.\n\nOn platforms with \\fButempter\\fP, \\fBmosh-server\\fP maintains an entry\nin the\n.BR utmp(5)\nfile to indicate its process ID, whether the session is connected,\nand the client's current IP address.\n\n\\fBmosh-server\\fP exits when the client terminates the connection.\n\n.SH OPTIONS\n\nThe argument \"new\" must be first on the command line to use\ncommand-line options.\n\n.TP\n.B \\-s\nbind to the local interface used for an incoming SSH connection, given\nin the \\fBSSH_CONNECTION\\fP environment variable (for multihomed\nhosts)\n\n.TP\n.B \\-v\nPrint some debugging information even after detaching.  More instances\nof this flag will result in more debugging information.\n\n.TP\n.B \\-i \\fIIP\\fP\nIP address of the local interface to bind (for multihomed hosts)\n\n.TP\n.B \\-p \\fIPORT\\fP[:\\fIPORT2\\fP]\nUDP port number or port-range to bind.  \\fB\\-p 0\\fP will let the\noperating system pick an available UDP port.\n\n.TP\n.B \\-c \\fICOLORS\\fP\nNumber of colors to advertise to applications through TERM (e.g. 8, 256)\n\n.TP\n.B \\-l \\fINAME=VALUE\\fP\nLocale-related environment variable to try as part of a fallback\nenvironment, if the startup environment does not specify a character\nset of UTF-8.\n\n.SH ENVIRONMENT VARIABLES\nThese variables allow server-side configuration of Mosh's behavior.\nThey may be set by administrators in system login/rc files,\n/etc/login.conf, or similar mechanisms, or users in their shell's\nlogin/rc files.  \\fBmosh-server\\fP passes these variables to the login\nsession and shell that it starts, but changing them there will have no\neffect.\n\n.TP\n.B MOSH_SERVER_NETWORK_TMOUT\nIf this variable is set to a positive integer number, it specifies how\nlong (in seconds) \\fBmosh-server\\fP will wait to receive an update from the\nclient before exiting.  Since \\fPmosh\\fP is very useful for mobile\nclients with intermittent operation and connectivity, we suggest\nsetting this variable to a high value, such as 604800 (one week) or\n2592000 (30 days).  Otherwise, \\fBmosh-server\\fP will wait\nindefinitely for a client to reappear.  This variable is somewhat\nsimilar to the \\fBTMOUT\\fP variable found in many Bourne shells.\nHowever, it is not a login-session inactivity timeout; it only applies\nto network connectivity.\n\n.TP\n.B MOSH_SERVER_SIGNAL_TMOUT\nIf this variable is set to a positive integer number, it specifies how\nlong (in seconds) \\fBmosh-server\\fP will ignore SIGUSR1 while waiting\nto receive an update from the client.  Otherwise, \\fBSIGUSR1\\fP will\nalways terminate \\fBmosh-server\\fP.  Users and administrators may\nimplement scripts to clean up disconnected Mosh sessions.  With this\nvariable set, a user or administrator can issue\n\n.nf\n$ pkill -SIGUSR1 mosh-server\n.fi\n\nto kill disconnected sessions without killing connected login\nsessions.\n\n.SH EXAMPLE\n\n.nf\n$ mosh-server\n\nMOSH CONNECT 60001 UAkFedSsVJs2LfMeKyQB5g\n\nmosh-server (mosh 1.1)\n[...] (copyright notice omitted)\n\n[mosh-server detached, pid = 20443]\n.fi\n\n.SH SEE ALSO\n.BR mosh (1),\n.BR mosh-client (1).\n\nProject home page:\n.I https://mosh.org\n\n.br\n.SH AUTHOR\nmosh was written by Keith Winstein <mosh-devel@mit.edu>.\n.SH BUGS\nPlease report bugs to \\fImosh-devel@mit.edu\\fP. Users may also subscribe\nto the\n.nh\n.I mosh-users@mit.edu\n.hy\nmailing list, at\n.br\n.nh\n.I http://mailman.mit.edu/mailman/listinfo/mosh-users\n.hy\n.\n"
  },
  {
    "path": "man/mosh.1",
    "content": ".\\\"                                      Hey, EMACS: -*- nroff -*-\n.\\\" First parameter, NAME, should be all caps\n.\\\" Second parameter, SECTION, should be 1-8, maybe w/ subsection\n.\\\" other parameters are allowed: see man(7), man(1)\n.TH MOSH 1 \"April 2013\"\n.\\\" Please adjust this date whenever revising the manpage.\n.\\\"\n.\\\" Some roff macros, for reference:\n.\\\" .nh        disable hyphenation\n.\\\" .hy        enable hyphenation\n.\\\" .ad l      left justify\n.\\\" .ad b      justify to both left and right margins\n.\\\" .nf        disable filling\n.\\\" .fi        enable filling\n.\\\" .br        insert line break\n.\\\" .sp <n>    insert n+1 empty lines\n.\\\" for manpage-specific macros, see man(7)\n.SH NAME\nmosh \\- mobile shell with roaming and intelligent local echo\n.SH SYNOPSIS\n.B mosh\n.RI [ options ]\n[--]\n[user@]host\n[command...]\n.br\n.SH DESCRIPTION\n\\fBmosh\\fP (mobile shell) is a remote terminal application that\nsupports intermittent connectivity, allows roaming, and provides\nspeculative local echo and line editing of user keystrokes.\n\nCompared with \\fBssh\\fP, \\fBmosh\\fP is more robust \\(em its\nconnections stay up across sleeps and changes in the client's IP\naddress \\(em and more responsive, because the protocol is tolerant of\npacket loss and the client can echo most keystrokes immediately,\nwithout waiting for a network round-trip.\n\n\\fBmosh\\fP uses \\fBssh\\fP to establish a connection to the remote host\nand authenticate with existing means (e.g., public-key authentication\nor a password). \\fBmosh\\fP executes the unprivileged \\fBmosh-server\\fP\nhelper program on the server, then closes the SSH connection and\nstarts the \\fBmosh-client\\fP, which establishes a long-lived datagram\nconnection over UDP.\n\nTo improve responsiveness, \\fBmosh\\fP runs a predictive model of the\nserver's behavior in the background, trying to guess the effect of\neach keystroke on the screen. It makes predictions for normal typing,\nbackspace, and the left- and right-arrow keys. When it is confident,\n\\fBmosh\\fP displays the predictions without waiting for the\nserver. The predictive model must prove itself anew on each row of the\nterminal and after each control character, so \\fBmosh\\fP avoids\nechoing passwords or non-echoing editor commands.\n\nBy default, \\fBmosh\\fP shows its predictions only on high-latency\nconnections and to smooth out network glitches. (On longer-latency\nlinks, the predicted cells are underlined until confirmed by the\nserver.) Occasional echo mistakes are corrected within a network\nround-trip and do not cause lasting effect.\n\n\\fBmosh\\fP does not support X forwarding or the non-interactive uses\nof SSH, including port forwarding or sshfs. \\fBmosh\\fP works through\ntypical client-side network address translators but requires UDP to\npass between client and server. By default, \\fBmosh\\fP uses the ports\nbetween 60000 and 61000, but allows the user to request a particular\nUDP port instead.\n\nCurrently, \\fBmosh\\fP has limited support for IPv6, dual-stack\nnetworks, and servers with multiple addresses.  At session start, it\nwill select a single IPv4 or IPv6 server address to connect to for the\nlifetime of the session.\n\n\\fBmosh\\fP will do its best to arrange a UTF-8 character set locale on\nthe client and server. The client must have locale-related environment\nvariables that specify UTF-8. \\fBmosh\\fP will pass these client\nvariables to the \\fBmosh-server\\fP on its command line, but in most\ncases they will not need to be used. \\fBmosh-server\\fP first attempts\nto use its own locale-related environment variables, which come from\nthe system default configuration (sometimes /etc/default/locale) or\nfrom having been passed over the SSH connection. But if these\nvariables don't call for the use of UTF-8, \\fBmosh-server\\fP will\napply the locale-related environment variables from the client and try\nagain.\n\n.SH OPTIONS\nOptions named \\fB \\-\\-experimental-*\\fP are subject to change or\nremoval in future versions of Mosh; their design or function is not\nyet final.\n\n.TP\n.B \\fIcommand\\fP\nCommand to run on remote host. By default, \\fBmosh\\fP executes a login shell.\n\n.TP\n.B \\-\\-client=\\fIPATH\\fP\npath to client helper on local machine (default: \"mosh-client\")\n\n.TP\n.B \\-\\-server=\\fICOMMAND\\fP\ncommand to run server helper on remote machine (default: \"mosh-server\")\n\nThe server helper is unprivileged and can be installed in the user's\nhome directory.\n\nThis option can be used to set environment variables for the server by\nusing the\n.BR env (1)\ncommand to wrap the actual server command.  See\n.BR mosh-server (1)\nfor available environment variables.\n\n.TP\n.B \\-\\-ssh=\\fICOMMAND\\fP\nOpenSSH command to remotely execute mosh-server on remote machine (default: \"ssh\")\n\nAn alternate ssh port can be specified with, \\fIe.g.\\fP, \\-\\-ssh=\"ssh \\-p 2222\".\n\n.TP\n.B \\-\\-ssh-pty\\fP\n.B \\-\\-no-ssh-pty\\fP\nEnable or disable ssh's use of a pty when connecting to a remote host.\nThe default is enabled.\n\n.TP\n.B \\-\\-predict=\\fIWHEN\\fP\nControls use of speculative local echo. WHEN defaults to `adaptive'\n(show predictions on slower links and to smooth out network glitches)\nand can also be `always` or `never'.\n\nThe MOSH_PREDICTION_DISPLAY environment variable controls this setting\npermanently and can adopt the same three values.\n\nEven on `always', \\fBmosh\\fP will only show predictions when it is\nconfident. This generally means a previous prediction on the same row\nof the terminal has been confirmed by the server, without any\nintervening control character keystrokes.\n\n.TP\n.B \\-a\nSynonym for \\-\\-predict=always\n\n.TP\n.B \\-n\nSynonym for \\-\\-predict=never\n\n.TP\n.B \\-\\-predict\\-overwrite\\fP\nWhen prediction is enabled, do not insert speculative local echo\nbefore existing text, but overwrite it instead.\n\nThe MOSH_PREDICTION_OVERWRITE environment variable also enables this\nif its value is 'yes'.\n\n.TP\n.B \\-o\nSynonym for \\-\\-predict\\-overwrite\n\n.TP\n.B \\-\\-family=inet\nOnly use IPv4 for the SSH connection and Mosh session.\n\n.TP\n.B \\-\\-family=inet6\nOnly use IPv6 for the SSH connection and Mosh session.  This and the\nfollowing modes require Perl's IO::Socket::IP or IO::Socket::INET6\nmodules.\n\n.TP\n.B \\-\\-family=auto\nAutodetect IPv4 or IPv6 for hosts that only have addresses\nin a single family.  Hosts with both IPv4 and IPv6 addresses will\nraise an error, and require re-invocation of \\fBmosh\\fP with another\n\\fB\\-\\-family\\fP option.\n\n.TP\n.B \\-\\-family=all\nChoose an address from all available IPv4 or IPv6 address, even for\ndual-stack hosts.  This is the most convenient option, but requires\ndual-stack connectivity, and Mosh 1.2.5 or later on the server, when\nroaming with dual-stack servers.\n\n.TP\n.B \\-\\-family=prefer-inet\nSimilar to \\fB\\-\\-family=all\\fP, but attempt connects to the IPv4\naddresses first.  This is the default.\n\n.TP\n.B \\-\\-family=prefer-inet6\nSimilar to \\fB\\-\\-family=all\\fP, but attempt connects to the IPv6\naddresses first.\n\n.TP\n.B \\-4\nSynonym for \\-\\-family=inet\n\n.TP\n.B \\-6\nSynonym for \\-\\-family=inet6\n\n.TP\n.B \\-p \\fIPORT\\fP[:\\fIPORT2\\fP], \\-\\-port=\\fIPORT\\fP[:\\fIPORT2\\fP]\nUse a particular server-side UDP port or port range,\nfor example, if this is the\nonly port that is forwarded through a firewall to the\nserver. With \\fB\\-p 0\\fP, the server will let the operating system pick an\navailable UDP port. Otherwise, \\fBmosh\\fP will choose a port between 60000 and\n61000. Please note that this option does not affect the server-side\nport used by SSH.\n\n.TP\n.B \\-\\-bind\\-server={ssh|any|\\fIIP\\fP}\nControl the IP address that the \\fBmosh-server\\fP binds to.\n\nThe default is `ssh', in which case the server will reply from the IP\naddress that the SSH connection came from (as found in the\n\\fBSSH_CONNECTION\\fP environment variable). This is useful for\nmultihomed servers.\n\nWith \\-\\-bind\\-server=any, the server will reply on the default interface\nand will not bind to a particular IP address. This can be useful if\nthe connection is made through \\fBsslh\\fP or another tool that makes\nthe SSH connection appear to come from localhost.\n\nWith \\-\\-bind\\-server=\\fIIP\\fP, the server will attempt to bind to the\nspecified IP address.\n\n.TP\n.B \\-\\-no\\-init\nDo not send the \\fBsmcup\\fP initialization string and \\fBrmcup\\fP\ndeinitialization string to the client's terminal. On many terminals\nthis disables alternate screen mode.\n\n.TP\n.B \\-\\-local\nInvoke \\fBmosh-server\\fP locally, without using \\fBssh\\fP.  This\noption requires the \\fBhost\\fP argument to be a local, numeric\nIPv4/IPv6 address.  This option is useful for testing.\n\n.TP\n.B \\-\\-experimental\\-remote\\-ip={proxy|local|remote}\nSelect the method used to discover the IP address that the\n\\fBmosh-client\\fP connects to.\n\nThe default is \\fBproxy\\fP, which uses SSH's\n.B \\-\\-ssh\\-proxy\\-command\noption to generate and report the exact address that \\fBssh\\fP uses to\nconnect to the remote host.  This option is generally the most\ncompatible with hosts and other options configured in \\fBssh\\fP\nconfiguration files.  However, this may not work for some\nconfigurations, or for environments where a \\fBssh\\fP bastion host\nforwards to a remote machine.  It only works with \\fBOpenSSH\\fP.\n\nWith \\fBremote\\fP, the server's\n.B SSH_CONNECTION\nenvironment variable will be used.  This is useful for environments\nwhere \\fBssh\\fP forwarding is used, or the\n.B \\-\\-ssh\\-proxy\\-command\noption is used for other purposes.\n\nWith \\fBlocal\\fP, Mosh resolves the hostname given on its command\nline, and uses that address for both \\fBssh\\fP and Mosh connections.\nThis option ignores any configuration in\n.B ssh_config\nfor the same hostname.\n\n.SH ESCAPE SEQUENCES\n\nThe default escape character used by Mosh is ASCII RS (decimal 30).\nThis is typically typed as \\fBCtrl-^\\fP or \\fBCtrl-Shift-6\\fP, on US\nEnglish keyboards.  Users of non-English keyboards may find it\ndifficult or impossible to type the default escape character, and may\nneed to change the escape character.  See the description of\nMOSH_ESCAPE_KEY, below.  In this description, the configured escape\ncharacter is represented as \\fBEsc\\fP.\n\nThere are two slightly different modes for escape sequences, depending\nwhether the escape character is printable or not.\n\nIf the escape character is a printable character, it must be prefixed\nwith a newline, similar to \\fBOpenSSH\\fP.  To send the escape character\nitself, type it twice.  If the escape character is set to \\fB~\\fP,\n\\fBmosh\\fP will behave much like \\fBOpenSSH\\fP.\n\nIf the escape character is a non-printable control character, no\nprefix is used and the escape character is recognized at any time.  To\nsend the escape character itself, type the escape character, then its\ncorresponding ASCII character (for \\fBCtrl-^\\fP you would type \\fB^\\fP,\nfor \\fBCtrl-B\\fP you would type \\fBB\\fP).\n\nThe escape sequence to shut down the connection is\n\\fBEsc .\\fP. The sequence \\fBEsc Ctrl-Z\\fP suspends the client.\nAny other sequence passes both characters through to the server.\n\n.SH ENVIRONMENT VARIABLES\nThese variables are not actually interpreted by\n.BR mosh (1)\nitself, but are passed through to\n.BR mosh-server (1).\nThey are described here for ease of use.\n\n.TP\n.B MOSH_ESCAPE_KEY\nWhen set, this configures the escape character used for local\ncommands.  The escape character may be set to any ASCII character in\nthe range 1-127.  The variable must be set with a single literal ASCII\ncharacter.  Control characters are set with the actual ASCII\ncontrol character, not with a printable representation such as \"^B\".\n\n.TP\n.B MOSH_PREDICTION_DISPLAY\nControls local echo as described above.  The command-line flag\noverrides this variable.\n\n.TP\n.B MOSH_TITLE_NOPREFIX\nWhen set, inhibits prepending \"[mosh]\" to window title.\n\n.SH SEE ALSO\n.BR mosh-client (1),\n.BR mosh-server (1).\n\nProject home page:\n.I https://mosh.org\n\n.br\n.SH AUTHOR\nmosh was written by Keith Winstein <mosh-devel@mit.edu>.\n.SH BUGS\nPlease report bugs to \\fImosh-devel@mit.edu\\fP. Users may also subscribe\nto the\n.nh\n.I mosh-users@mit.edu\n.hy\nmailing list, at\n.br\n.nh\n.I http://mailman.mit.edu/mailman/listinfo/mosh-users\n.hy\n.\n"
  },
  {
    "path": "ocb-license.html",
    "content": "<TITLE>OCB - An Authenticated-Encryption Scheme - GPL Patent Grant - Rogaway</TITLE>\n\n<body bgcolor=\"#FFFFFF\">\n<H2><a name=\"ocb-grant\"> <font face=\"Arial, Helvetica, sans-serif\" size=\"6\" color=\"#FF0000\">OCB: \n  Patent Grant for GNU GPL</font> </a> </H2>\n\nWhereas I, Phillip Rogaway (hereinafter \"Inventor\") have sought \npatent protection for certain technology \n(hereinafter \"Patented Technology\"), \nand Inventor wishes to aid the Free Software Foundation in achieving its goals, \nand Inventor wishes to increase public awareness of Patented Technology, \nInventor hereby grants a fully paid-up, nonexclusive, \nroyalty-free license to \npractice any patents claiming priority to the \npatent applications below (\"the Patents\") \nif practiced by\nsoftware distributed \nunder the terms of any version of \nthe GNU General Public License as published by the Free Software Foundation, \n59 Temple Place, Suite 330, Boston, MA 02111. \nInventor reserves all other rights, including without limitation\nlicensing for software not distributed under the GNU General Public License. \n\n<h4>The patents:</h4>\n\n\n<ul>\n<li> <a href=\"http://appft1.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&amp;Sect2=HITOFF&amp;p=1&amp;u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&amp;r=2&amp;f=G&amp;l=50&amp;co1=AND&amp;d=PG01&amp;s1=rogaway.IN.&amp;OS=IN/rogaway&amp;RS=IN/rogaway\">\n09/918,615</a>  -\nMethod and Apparatus for Facilitating Efficient Authenticated Encryption.\n\n<li> <a href=\"http://appft1.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&amp;Sect2=HITOFF&amp;p=1&amp;u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&amp;r=3&amp;f=G&amp;l=50&amp;co1=AND&amp;d=PG01&amp;s1=rogaway.IN.&amp;OS=IN/rogaway&amp;RS=IN/rogaway\">\n09/948,084</a> - \nMethod and Apparatus for Realizing a Parallelizable Variable-Input-Length \nPseudorandom Function. \n</ul>\n\n<hr>\n\n<p><b>June 12, 2012</b>: Phillip Rogaway licensed the distribution of OCB\nin Mosh under the GPL with the OpenSSL linking exception and iOS\nwaiver contained in the <code>COPYING.iOS</code> file.</p>\n\n<blockquote>\n  \"Mosh with the two GPL exemptions you specify in the attached note\n  is freely licensed to use for any OCB-related IP that I own.\"\n</blockquote>\n"
  },
  {
    "path": "scripts/Makefile.am",
    "content": "EXTRA_DIST = wrap-compiler-for-flag-check mosh.pl\nif BUILD_CLIENT\n  bin_SCRIPTS = mosh\nendif\nCLEANFILES = $(bin_SCRIPTS)\n\nmosh:\tmosh.pl ../VERSION.stamp Makefile\n\tperl -Mdiagnostics -c $(srcdir)/mosh.pl\n\t@sed -e \"s/\\@VERSION\\@/`cat ../VERSION.stamp`/\" -e \"s/\\@PACKAGE_STRING\\@/@PACKAGE_STRING@/\" $(srcdir)/mosh.pl > mosh\n\t@chmod a+x mosh\n"
  },
  {
    "path": "scripts/mosh.pl",
    "content": "#!/usr/bin/env perl\n\n#   Mosh: the mobile shell\n#   Copyright 2012 Keith Winstein\n#\n#   This program is free software: you can redistribute it and/or modify\n#   it under the terms of the GNU General Public License as published by\n#   the Free Software Foundation, either version 3 of the License, or\n#   (at your option) any later version.\n#\n#   This program is distributed in the hope that it will be useful,\n#   but WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n#   GNU General Public License for more details.\n#\n#   You should have received a copy of the GNU General Public License\n#   along with this program.  If not, see <http://www.gnu.org/licenses/>.\n#\n#   In addition, as a special exception, the copyright holders give\n#   permission to link the code of portions of this program with the\n#   OpenSSL library under certain conditions as described in each\n#   individual source file, and distribute linked combinations including\n#   the two.\n#\n#   You must obey the GNU General Public License in all respects for all\n#   of the code used other than OpenSSL. If you modify file(s) with this\n#   exception, you may extend this exception to your version of the\n#   file(s), but you are not obligated to do so. If you do not wish to do\n#   so, delete this exception statement from your version. If you delete\n#   this exception statement from all source files in the program, then\n#   also delete it here.\n\nuse 5.8.8;\n\nuse warnings;\nuse strict;\nuse Getopt::Long;\nuse IO::Socket;\nuse Text::ParseWords;\nuse Socket qw(IPPROTO_TCP);\nuse Errno qw(EINTR);\nuse POSIX qw(_exit);\n\nBEGIN {\n  my @gai_reqs = qw( getaddrinfo getnameinfo AI_CANONNAME AI_NUMERICHOST NI_NUMERICHOST );\n  eval { Socket->import( @gai_reqs ); 1; }\n    || (eval { require Socket::GetAddrInfo; 1; }\n        && (eval { Socket::GetAddrInfo->import( ':newapi', @gai_reqs ); 1; }\n            || eval { Socket::GetAddrInfo->import( '0.22', @gai_reqs ); 1; }))\n    || die \"$0 error: requires Perl 5.14 or Socket::GetAddrInfo.\\n\";\n}\n\nmy $have_ipv6 = eval {\n  require IO::Socket::IP;\n  IO::Socket::IP->import('-register');\n  1;\n} || eval {\n  require IO::Socket::INET6;\n  1;\n};\n\n$|=1;\n\nmy $client = 'mosh-client';\nmy $server = 'mosh-server';\n\nmy $predict = undef;\n\nmy $overwrite = 0;\n\nmy $bind_ip = undef;\n\nmy $use_remote_ip = 'proxy';\n\nmy $family = 'prefer-inet';\nmy $port_request = undef;\n\nmy @ssh = ('ssh');\n\nmy $term_init = 1;\n\nmy $localhost = undef;\n\nmy $ssh_pty = 1;\n\nmy $help = undef;\nmy $version = undef;\n\nmy @cmdline = @ARGV;\n\nmy $usage =\nqq{Usage: $0 [options] [--] [user@]host [command...]\n        --client=PATH        mosh client on local machine\n                                (default: \"mosh-client\")\n        --server=COMMAND     mosh server on remote machine\n                                (default: \"mosh-server\")\n\n        --predict=adaptive      local echo for slower links [default]\n-a      --predict=always        use local echo even on fast links\n-n      --predict=never         never use local echo\n        --predict=experimental  aggressively echo even when incorrect\n\n-o      --predict-overwrite     prediction overwrites instead of inserting\n\n-4      --family=inet        use IPv4 only\n-6      --family=inet6       use IPv6 only\n        --family=auto        autodetect network type for single-family hosts only\n        --family=all         try all network types\n        --family=prefer-inet use all network types, but try IPv4 first [default]\n        --family=prefer-inet6 use all network types, but try IPv6 first\n-p PORT[:PORT2]\n        --port=PORT[:PORT2]  server-side UDP port or range\n                                (No effect on server-side SSH port)\n        --bind-server={ssh|any|IP}  ask the server to reply from an IP address\n                                       (default: \"ssh\")\n\n        --ssh=COMMAND        ssh command to run when setting up session\n                                (example: \"ssh -p 2222\")\n                                (default: \"ssh\")\n\n        --no-ssh-pty         do not allocate a pseudo tty on ssh connection\n\n        --no-init            do not send terminal initialization string\n\n        --local              run mosh-server locally without using ssh\n\n        --experimental-remote-ip=(local|remote|proxy)  select the method for\n                             discovering the remote IP address to use for mosh\n                             (default: \"proxy\")\n\n        --help               this message\n        --version            version and copyright information\n\nPlease report bugs to mosh-devel\\@mit.edu.\nMosh home page: https://mosh.org\\n};\n\nmy $version_message = '@PACKAGE_STRING@ [build @VERSION@]' . qq{\nCopyright 2012 Keith Winstein <mosh-devel\\@mit.edu>\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\\n};\n\nsub predict_check {\n  my ( $predict, $env_set ) = @_;\n\n  if ( not exists { adaptive => 0, always => 0,\n\t\t    never => 0, experimental => 0 }->{ $predict } ) {\n    my $explanation = $env_set ? \" (MOSH_PREDICTION_DISPLAY in environment)\" : \"\";\n    print STDERR qq{$0: Unknown mode \\\"$predict\\\"$explanation.\\n\\n};\n\n    die $usage;\n  }\n}\n\nGetOptions( 'client=s' => \\$client,\n\t    'server=s' => \\$server,\n\t    'predict=s' => \\$predict,\n\t    'predict-overwrite|o!' => \\$overwrite,\n\t    'port=s' => \\$port_request,\n\t    'a' => sub { $predict = 'always' },\n\t    'n' => sub { $predict = 'never' },\n\t    'family=s' => \\$family,\n\t    '4' => sub { $family = 'inet' },\n\t    '6' => sub { $family = 'inet6' },\n\t    'p=s' => \\$port_request,\n\t    'ssh=s' => sub { @ssh = shellwords($_[1]); },\n\t    'ssh-pty!' => \\$ssh_pty,\n\t    'init!' => \\$term_init,\n\t    'local' => \\$localhost,\n\t    'help' => \\$help,\n\t    'version' => \\$version,\n\t    'fake-proxy!' => \\my $fake_proxy,\n\t    'bind-server=s' => \\$bind_ip,\n\t    'experimental-remote-ip=s' => \\$use_remote_ip) or die $usage;\n\nif ( defined $help ) {\n    print $usage;\n    exit;\n}\nif ( defined $version ) {\n    print $version_message;\n    exit;\n}\n\nif ( defined $predict ) {\n  predict_check( $predict, 0 );\n} elsif ( defined $ENV{ 'MOSH_PREDICTION_DISPLAY' } ) {\n  $predict = $ENV{ 'MOSH_PREDICTION_DISPLAY' };\n  predict_check( $predict, 1 );\n} else {\n  $predict = 'adaptive';\n  predict_check( $predict, 0 );\n}\n\nif ( not grep { $_ eq $use_remote_ip } qw { local remote proxy } ) {\n  die \"Unknown parameter $use_remote_ip\";\n}\n\n$family = lc( $family );\n# Handle IPv4-only Perl installs.\nif (!$have_ipv6) {\n  # Report failure if IPv6 needed and not available.\n  if (defined($family) && $family eq \"inet6\") {\n    die \"$0: IPv6 sockets not available in this Perl install\\n\";\n  }\n  # Force IPv4.\n  $family = \"inet\";\n}\nif ( $overwrite ) {\n    $ENV{ \"MOSH_PREDICTION_OVERWRITE\" } = \"yes\";\n}\n\nif ( defined $port_request ) {\n  if ( $port_request =~ m{^(\\d+)(:(\\d+))?$} ) {\n    my ( $low, $clause, $high ) = ( $1, $2, $3 );\n    # good port or port-range\n    if ( $low < 0 or $low > 65535 ) {\n      die \"$0: Server-side (low) port ($low) must be within valid range [0..65535].\\n\";\n    }\n    if ( defined $high ) {\n      if ( $high <= 0 or $high > 65535 ) {\n\tdie \"$0: Server-side high port ($high) must be within valid range [1..65535].\\n\";\n      }\n      if ( $low == 0 ) {\n\tdie \"$0: Server-side port ranges may not be used with starting port 0 ($port_request).\\n\";\n      }\n      if ( $low > $high ) {\n\tdie \"$0: Server-side port range ($port_request): low port greater than high port.\\n\";\n      }\n    }\n  } else {\n    die \"$0: Server-side port or range ($port_request) is not valid.\\n\";\n  }\n}\n\ndelete $ENV{ 'MOSH_PREDICTION_DISPLAY' };\n\nmy $userhost;\nmy @command;\nmy @bind_arguments;\n\nif ( ! defined $fake_proxy ) {\n  if ( scalar @ARGV < 1 ) {\n    die $usage;\n  }\n  $userhost = shift;\n  @command = @ARGV;\n  if ( not defined $bind_ip or $bind_ip =~ m{^ssh$}i ) {\n    if ( not defined $localhost ) {\n      push @bind_arguments, '-s';\n    } else {\n      push @bind_arguments, ('-i', \"$userhost\");\n    }\n  } elsif ( $bind_ip =~ m{^any$}i ) {\n    # do nothing\n  } else {\n    push @bind_arguments, ('-i', \"$bind_ip\");\n  }\n} else {\n  my ( $host, $port ) = @ARGV;\n\n  my @res = resolvename( $host, $port, $family );\n\n  # Now try and connect to something.\n  my $err;\n  my $sock;\n  my $addr_string;\n  my $service;\n  for my $ai ( @res ) {\n    ( $err, $addr_string, $service ) = getnameinfo( $ai->{addr}, NI_NUMERICHOST );\n    next if $err;\n    if ( $sock = IO::Socket->new( Domain => $ai->{family},\n\t\t\t\t  Family => $ai->{family},\n\t\t\t\t  PeerHost => $addr_string,\n\t\t\t\t  PeerPort => $port,\n\t\t\t\t  Proto => 'tcp' )) {\n      print STDERR 'MOSH IP ', $addr_string, \"\\n\";\n      last;\n    } else {\n      $err = $@;\n    }\n  }\n  die \"$0: Could not connect to ${host}, last tried ${addr_string}: ${err}\\n\" if !$sock;\n\n  # Act like netcat\n  binmode($sock);\n  binmode(STDIN);\n  binmode(STDOUT);\n\n  sub cat {\n    my ( $from, $to ) = @_;\n    while ( my $n = $from->sysread( my $buf, 4096 ) ) {\n      next if ( $n == -1 && $! == EINTR );\n      $n >= 0 or last;\n      $to->write( $buf ) or last;\n    }\n  }\n\n  defined( my $pid = fork ) or die \"$0: fork: $!\\n\";\n  if ( $pid == 0 ) {\n    close STDIN;\n    cat $sock, \\*STDOUT; $sock->shutdown( 0 );\n    _exit 0;\n  }\n  $SIG{ 'HUP' } = 'IGNORE';\n  close STDOUT;\n  cat \\*STDIN, $sock; $sock->shutdown( 1 );\n  close STDIN;\n  waitpid $pid, 0;\n  exit;\n}\n\n# Count colors\nopen COLORCOUNT, '-|', $client, ('-c') or die \"Can't count colors: $!\\n\";\nmy $colors = \"\";\n{\n  local $/ = undef;\n  $colors = <COLORCOUNT>;\n}\nclose COLORCOUNT or die;\n\nchomp $colors;\n\nif ( (not defined $colors)\n    or $colors !~ m{^[0-9]+$}\n    or $colors < 0 ) {\n  $colors = 0;\n}\n\n$ENV{ 'MOSH_CLIENT_PID' } = $$; # We don't support this, but it's useful for test and debug.\n\n# If we are using a locally-resolved address, we have to get it before we fork,\n# so both parent and child get it.\nmy $ip;\nif ( $use_remote_ip eq 'local' ) {\n  # \"parse\" the host from what the user gave us\n  my ($user, $host) = $userhost =~ /^((?:.*@)?)(.*)$/;\n  # get list of addresses\n  my @res = resolvename( $host, 22, $family );\n  # Use only the first address as the Mosh IP\n  my $hostaddr = $res[0];\n  if ( !defined $hostaddr ) {\n    die( \"could not find address for $host\" );\n  }\n  my ( $err, $addr_string, $service ) = getnameinfo( $hostaddr->{addr}, NI_NUMERICHOST );\n  if ( $err ) {\n    die( \"could not use address for $host\" );\n  }\n  $ip = $addr_string;\n  $userhost = \"$user$ip\";\n}\n\nmy $pid = open(my $pipe, \"-|\");\ndie \"$0: fork: $!\\n\" unless ( defined $pid );\nif ( $pid == 0 ) { # child\n  open(STDERR, \">&STDOUT\") or die;\n\n  my @sshopts = ( '-n' );\n  if ($ssh_pty) {\n      push @sshopts, '-tt';\n  }\n\n  my $ssh_connection = \"\";\n  if ( $use_remote_ip eq 'remote' ) {\n    # Ask the server for its IP.  The user's shell may not be\n    # Posix-compatible so invoke sh explicitly.\n    $ssh_connection = \"sh -c \" .\n      shell_quote ( '[ -n \"$SSH_CONNECTION\" ] && printf \"\\nMOSH SSH_CONNECTION %s\\n\" \"$SSH_CONNECTION\"' ) .\n      \" ; \";\n    # Only with 'remote', we may need to tell SSH which protocol to use.\n    if ( $family eq 'inet' ) {\n      push @sshopts, '-4';\n    } elsif ( $family eq 'inet6' ) {\n      push @sshopts, '-6';\n    }\n  }\n  my @server = ( 'new' );\n\n  push @server, ( '-c', $colors );\n\n  push @server, @bind_arguments;\n\n  if ( defined $port_request ) {\n    push @server, ( '-p', $port_request );\n  }\n\n  for ( &locale_vars ) {\n    push @server, ( '-l', $_ );\n  }\n\n  if ( scalar @command > 0 ) {\n    push @server, '--', @command;\n  }\n\n  if ( defined( $localhost )) {\n    delete $ENV{ 'SSH_CONNECTION' };\n    chdir; # $HOME\n    print \"MOSH IP ${userhost}\\n\";\n    exec( \"$server \" . shell_quote( @server ) );\n    die \"Cannot exec $server: $!\\n\";\n  }\n  if ( $use_remote_ip eq 'proxy' ) {\n    # Non-standard shells and broken shrc files cause the ssh\n    # proxy to break mysteriously.\n    $ENV{ 'SHELL' } = '/bin/sh';\n    my $quoted_proxy_command = shell_quote( $0, \"--family=$family\" );\n    push @sshopts, ( '-S', 'none', '-o', \"ProxyCommand=$quoted_proxy_command --fake-proxy -- %h %p\" );\n  }\n  my @exec_argv = ( @ssh, @sshopts, $userhost, '--', $ssh_connection . \"$server \" . shell_quote( @server ) );\n  exec @exec_argv;\n  die \"Cannot exec ssh: $!\\n\";\n} else { # parent\n  my ( $sship, $port, $key );\n  my $bad_udp_port_warning = 0;\n  LINE: while ( <$pipe> ) {\n    chomp;\n    if ( m{^MOSH IP } ) {\n      if ( defined $ip ) {\n\tdie \"$0 error: detected attempt to redefine MOSH IP.\\n\";\n      }\n      ( $ip ) = m{^MOSH IP (\\S+)\\s*$} or die \"Bad MOSH IP string: $_\\n\";\n    } elsif ( m{^MOSH SSH_CONNECTION } ) {\n      my @words = split;\n      if ( scalar @words == 6 ) {\n\t$sship = $words[4];\n      } else {\n\tdie \"Bad MOSH SSH_CONNECTION string: $_\\n\";\n      }\n    } elsif ( m{^MOSH CONNECT } ) {\n      if ( ( $port, $key ) = m{^MOSH CONNECT (\\d+?) ([A-Za-z0-9/+]{22})\\s*$} ) {\n\tlast LINE;\n      } else {\n\tdie \"Bad MOSH CONNECT string: $_\\n\";\n      }\n    } else {\n      if ( defined $port_request and $port_request =~ m{:} and m{Bad UDP port} ) {\n\t$bad_udp_port_warning = 1;\n      }\n      print \"$_\\n\";\n    }\n  }\n  close $pipe;\n  waitpid $pid, 0;\n\n  if ( not defined $ip ) {\n    if ( defined $sship ) {\n      warn \"$0: Using remote IP address ${sship} from \\$SSH_CONNECTION for hostname ${userhost}\\n\";\n      $ip = $sship;\n    } else {\n      die \"$0: Did not find remote IP address (is SSH ProxyCommand disabled?).\\n\";\n    }\n  }\n\n  if ( not defined $key or not defined $port ) {\n    if ( $bad_udp_port_warning ) {\n      die \"$0: Server does not support UDP port range option.\\n\";\n    }\n    die \"$0: Did not find mosh server startup message. (Have you installed mosh on your server?)\\n\";\n  }\n\n  # Now start real mosh client\n  $ENV{ 'MOSH_KEY' } = $key;\n  $ENV{ 'MOSH_PREDICTION_DISPLAY' } = $predict;\n  $ENV{ 'MOSH_NO_TERM_INIT' } = '1' if !$term_init;\n  exec {$client} (\"$client\", \"-# @cmdline |\", $ip, $port);\n}\n\nsub shell_quote { join ' ', map {(my $a = $_) =~ s/'/'\\\\''/g; \"'$a'\"} @_ }\n\nsub locale_vars {\n  my @names = qw[LANG LANGUAGE LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT LC_IDENTIFICATION LC_ALL];\n\n  my @assignments;\n\n  for ( @names ) {\n    if ( defined $ENV{ $_ } ) {\n      push @assignments, $_ . q{=} . $ENV{ $_ };\n    }\n  }\n\n  return @assignments;\n}\n\nsub resolvename {\n  my ( $host, $port, $family ) = @_;\n  my $err;\n  my @res;\n  my $af;\n\n  # If the user selected a specific family, parse it.\n  if ( defined( $family ) && ( $family eq 'inet' || $family eq 'inet6' )) {\n      # Choose an address family, or cause pain.\n      my $afstr = 'AF_' . uc( $family );\n      $af = eval { IO::Socket->$afstr } or die \"$0: Invalid family $family\\n\";\n  }\n\n  # First try the address as a numeric.\n  my %hints = ( flags => AI_NUMERICHOST,\n\t\tsocktype => SOCK_STREAM,\n\t\tprotocol => IPPROTO_TCP );\n  if ( defined( $af )) {\n    $hints{family} = $af;\n  }\n  ( $err, @res ) = getaddrinfo( $host, $port, \\%hints );\n  if ( $err ) {\n    # Get canonical name for this host.\n    $hints{flags} = AI_CANONNAME;\n    ( $err, @res ) = getaddrinfo( $host, $port, \\%hints );\n    die \"$0: could not get canonical name for $host: ${err}\\n\" if $err;\n    # Then get final resolution of the canonical name.\n    delete $hints{flags};\n    my @newres;\n    ( $err, @newres ) = getaddrinfo( $res[0]{canonname}, $port, \\%hints );\n    die \"$0: could not resolve canonical name ${res[0]{canonname}} for ${host}: ${err}\\n\" if $err;\n    @res = @newres;\n  }\n\n  if ( defined( $af )) {\n    # If v4 or v6 was specified, reduce the host list.\n    @res = grep {$_->{family} == $af} @res;\n  } elsif ( $family =~ /^prefer-/ ) {\n    # If prefer-* was specified, reorder the host list to put that family first.\n    my $prefer_afstr = 'AF_' . uc( ($family =~ /prefer-(.*)/)[0] );\n    my $prefer_af = eval { IO::Socket->$prefer_afstr } or die \"$0: Invalid preferred family $family\\n\";\n    @res = (grep({$_->{family} == $prefer_af} @res), grep({$_->{family} != $prefer_af} @res));\n  } elsif ( $family ne 'all' ) {\n    # If v4/v6/all were not specified, verify that this host only has one address family available.\n    for my $ai ( @res ) {\n      if ( !defined( $af )) {\n\t$af = $ai->{family};\n      } else {\n\tdie \"$0: host has both IPv4 and IPv6 addresses, use --family to specify behavior\\n\"\n\t    if $af != $ai->{family};\n      }\n    }\n  }\n  return @res;\n}\n"
  },
  {
    "path": "scripts/wrap-compiler-for-flag-check",
    "content": "#!/bin/sh\n\n# There is no way to make clang's \"argument unused\" warning fatal.  So when\n# configure checks for supported flags, it runs $CXX and $LD via this\n# wrapper.\n#\n# Ideally the search string would also include 'clang: ' but this output might\n# depend on clang's argv[0].\n\nif out=$(\"$@\" 2>&1); then\n  echo \"$out\"\n  if echo \"$out\" | grep 'warning: argument unused' >/dev/null; then\n    echo \"$0: found clang warning\"\n    exit 1\n  else\n    exit 0\n  fi\nelse\n  code=$?\n  echo \"$out\"\n  exit $code\nfi\n"
  },
  {
    "path": "src/Makefile.am",
    "content": "SUBDIRS = include protobufs util crypto terminal network statesync frontend examples tests fuzz\n"
  },
  {
    "path": "src/crypto/Makefile.am",
    "content": "AM_CPPFLAGS = -I$(top_srcdir)/ $(CRYPTO_CFLAGS)\nAM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\n\nnoinst_LIBRARIES = libmoshcrypto.a\n\nOCB_SRCS = ae.h\nif USE_AES_OCB_FROM_OPENSSL\nOCB_SRCS += ocb_openssl.cc\nelse\nOCB_SRCS += ocb_internal.cc\nendif\n\nlibmoshcrypto_a_SOURCES = \\\n\t$(OCB_SRCS) \\\n\tbase64.cc \\\n\tbase64.h \\\n\tbyteorder.h \\\n\tcrypto.cc \\\n\tcrypto.h \\\n\tprng.h\n"
  },
  {
    "path": "src/crypto/ae.h",
    "content": "/* ---------------------------------------------------------------------------\n *\n * AEAD API 0.12 - 13 July 2011\n *\n * This file gives an interface appropriate for many authenticated\n * encryption with associated data (AEAD) implementations. It does not try\n * to accommodate all possible options or limitations that an implementation\n * might have -- you should consult the documentation of your chosen\n * implementation to find things like RFC 5116 constants, alignment\n * requirements, whether the incremental interface is supported, etc.\n *\n * This file is in the public domain. It is provided \"as is\", without\n * warranty of any kind. Use at your own risk.\n *\n * Comments are welcome: Ted Krovetz <ted@krovetz>.\n *\n * ------------------------------------------------------------------------ */\n\n#ifndef _AE_H_\n#define _AE_H_\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* --------------------------------------------------------------------------\n *\n * Constants\n *\n * ----------------------------------------------------------------------- */\n\n/* Return status codes: Negative return values indicate an error occurred.\n * For full explanations of error values, consult the implementation's\n * documentation.                                                          */\n#define AE_SUCCESS ( 0 )        /* Indicates successful completion of call  */\n#define AE_INVALID ( -1 )       /* Indicates bad tag during decryption      */\n#define AE_NOT_SUPPORTED ( -2 ) /* Indicates unsupported option requested   */\n\n/* Flags: When data can be processed \"incrementally\", these flags are used\n * to indicate whether the submitted data is the last or not.               */\n#define AE_FINALIZE ( 1 ) /* This is the last of data                  */\n#define AE_PENDING ( 0 )  /* More data of is coming                    */\n\n/* --------------------------------------------------------------------------\n *\n * AEAD opaque structure definition\n *\n * ----------------------------------------------------------------------- */\n\ntypedef struct _ae_ctx ae_ctx;\n\n/* --------------------------------------------------------------------------\n *\n * Data Structure Routines\n *\n * ----------------------------------------------------------------------- */\n\nae_ctx* ae_allocate( void* misc ); /* Allocate ae_ctx, set optional ptr   */\nvoid ae_free( ae_ctx* ctx );       /* Deallocate ae_ctx struct            */\nint ae_clear( ae_ctx* ctx );       /* Undo initialization                 */\nint ae_ctx_sizeof( void );         /* Return sizeof(ae_ctx)               */\n/* ae_allocate() allocates an ae_ctx structure, but does not initialize it.\n * ae_free() deallocates an ae_ctx structure, but does not zeroize it.\n * ae_clear() zeroes sensitive values associated with an ae_ctx structure\n * and deallocates any auxiliary structures allocated during ae_init().\n * ae_ctx_sizeof() returns sizeof(ae_ctx), to aid in any static allocations.\n */\n\n/* --------------------------------------------------------------------------\n *\n * AEAD Routines\n *\n * ----------------------------------------------------------------------- */\n\nint ae_init( ae_ctx* ctx, const void* key, int key_len, int nonce_len, int tag_len );\n/* --------------------------------------------------------------------------\n *\n * Initialize an ae_ctx context structure.\n *\n * Parameters:\n *  ctx       - Pointer to an ae_ctx structure to be initialized\n *  key       - Pointer to user-supplied key\n *  key_len   - Length of key supplied, in bytes\n *  nonce_len - Length of nonces to be used for this key, in bytes\n *  tag_len   - Length of tags to be produced for this key, in bytes\n *\n * Returns:\n *  AE_SUCCESS       - Success. Ctx ready for use.\n *  AE_NOT_SUPPORTED - An unsupported length was supplied. Ctx is untouched.\n *  Otherwise        - Error. Check implementation documentation for codes.\n *\n * ----------------------------------------------------------------------- */\n\nint ae_encrypt( ae_ctx* ctx,\n                const void* nonce,\n                const void* pt,\n                int pt_len,\n                const void* ad,\n                int ad_len,\n                void* ct,\n                void* tag,\n                int final );\n/* --------------------------------------------------------------------------\n *\n * Encrypt plaintext; provide for authentication of ciphertext/associated data.\n *\n * Parameters:\n *  ctx    - Pointer to an ae_ctx structure initialized by ae_init.\n *  nonce  - Pointer to a nonce_len (defined in ae_init) byte nonce.\n *  pt     - Pointer to plaintext bytes to be encrypted.\n *  pt_len - number of bytes pointed to by pt.\n *  ad     - Pointer to associated data.\n *  ad_len - number of bytes pointed to by ad.\n *  ct     - Pointer to buffer to receive ciphertext encryption.\n *  tag    - Pointer to receive authentication tag; or NULL\n *           if tag is to be bundled into the ciphertext.\n *  final  - Non-zero if this call completes the plaintext being encrypted.\n *\n * If nonce!=NULL then a message is being initiated. If final!=0\n * then a message is being finalized. If final==0 or nonce==NULL\n * then the incremental interface is being used. If nonce!=NULL and\n * ad_len<0, then use same ad as last message.\n *\n * Returns:\n *  non-negative     - Number of bytes written to ct.\n *  AE_NOT_SUPPORTED - Usage mode unsupported (eg, incremental and/or sticky).\n *  Otherwise        - Error. Check implementation documentation for codes.\n *\n * ----------------------------------------------------------------------- */\n\nint ae_decrypt( ae_ctx* ctx,\n                const void* nonce,\n                const void* ct,\n                int ct_len,\n                const void* ad,\n                int ad_len,\n                void* pt,\n                const void* tag,\n                int final );\n/* --------------------------------------------------------------------------\n *\n * Decrypt ciphertext; provide authenticity of plaintext and associated data.\n *\n * Parameters:\n *  ctx    - Pointer to an ae_ctx structure initialized by ae_init.\n *  nonce  - Pointer to a nonce_len (defined in ae_init) byte nonce.\n *  ct     - Pointer to ciphertext bytes to be decrypted.\n *  ct_len - number of bytes pointed to by ct.\n *  ad     - Pointer to associated data.\n *  ad_len - number of bytes pointed to by ad.\n *  pt     - Pointer to buffer to receive plaintext decryption.\n *  tag    - Pointer to tag_len (defined in ae_init) bytes; or NULL\n *           if tag is bundled into the ciphertext. Non-NULL tag is only\n *           read when final is non-zero.\n *  final  - Non-zero if this call completes the ciphertext being decrypted.\n *\n * If nonce!=NULL then \"ct\" points to the start of a ciphertext. If final!=0\n * then \"in\" points to the final piece of ciphertext. If final==0 or nonce==\n * NULL then the incremental interface is being used. If nonce!=NULL and\n * ad_len<0, then use same ad as last message.\n *\n * Returns:\n *  non-negative     - Number of bytes written to pt.\n *  AE_INVALID       - Authentication failure.\n *  AE_NOT_SUPPORTED - Usage mode unsupported (eg, incremental and/or sticky).\n *  Otherwise        - Error. Check implementation documentation for codes.\n *\n * NOTE !!! NOTE !!! -- The ciphertext should be assumed possibly inauthentic\n *                      until it has been completely written and it is\n *                      verified that this routine did not return AE_INVALID.\n *\n * ----------------------------------------------------------------------- */\n\n#ifdef __cplusplus\n} /* closing brace for extern \"C\" */\n#endif\n\n#endif /* _AE_H_ */\n"
  },
  {
    "path": "src/crypto/base64.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdlib>\n#include <cstring>\n\n#include \"src/crypto/base64.h\"\n#include \"src/util/fatal_assert.h\"\n\nstatic const char table[] = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\nstatic const unsigned char reverse[] = {\n  // clang-format off\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,\n  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,\n  0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,\n  0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n  // clang-format on\n};\n\n/* Reverse maps from an ASCII char to a base64 sixbit value.  Returns > 0x3f on failure. */\nstatic unsigned char base64_char_to_sixbit( unsigned char c )\n{\n  return reverse[c];\n}\n\nbool base64_decode( const char* b64, const size_t b64_len, uint8_t* raw, size_t* raw_len )\n{\n  fatal_assert( b64_len == 24 ); /* only useful for Mosh keys */\n  fatal_assert( *raw_len == 16 );\n\n  uint32_t bytes = 0;\n  for ( int i = 0; i < 22; i++ ) {\n    unsigned char sixbit = base64_char_to_sixbit( *( b64++ ) );\n    if ( sixbit > 0x3f ) {\n      return false;\n    }\n    bytes <<= 6;\n    bytes |= sixbit;\n    /* write groups of 3 */\n    if ( i % 4 == 3 ) {\n      raw[0] = bytes >> 16;\n      raw[1] = bytes >> 8;\n      raw[2] = bytes;\n      raw += 3;\n      bytes = 0;\n    }\n  }\n  /* last byte of output */\n  *raw = bytes >> 4;\n  if ( b64[0] != '=' || b64[1] != '=' ) {\n    return false;\n  }\n  return true;\n}\n\nvoid base64_encode( const uint8_t* raw, const size_t raw_len, char* b64, const size_t b64_len )\n{\n  fatal_assert( b64_len == 24 ); /* only useful for Mosh keys */\n  fatal_assert( raw_len == 16 );\n\n  /* first 15 bytes of input */\n  for ( int i = 0; i < 5; i++ ) {\n    uint32_t bytes = ( raw[0] << 16 ) | ( raw[1] << 8 ) | raw[2];\n    b64[0] = table[( bytes >> 18 ) & 0x3f];\n    b64[1] = table[( bytes >> 12 ) & 0x3f];\n    b64[2] = table[( bytes >> 6 ) & 0x3f];\n    b64[3] = table[(bytes)&0x3f];\n    raw += 3;\n    b64 += 4;\n  }\n\n  /* last byte of input, last 4 of output */\n  uint8_t lastchar = *raw;\n  b64[0] = table[( lastchar >> 2 ) & 0x3f];\n  b64[1] = table[( lastchar << 4 ) & 0x3f];\n  b64[2] = '=';\n  b64[3] = '=';\n}\n"
  },
  {
    "path": "src/crypto/base64.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdint>\n\nbool base64_decode( const char* b64, const size_t b64_len, uint8_t* raw, size_t* raw_len );\n\nvoid base64_encode( const uint8_t* raw, const size_t raw_len, char* b64, const size_t b64_len );\n"
  },
  {
    "path": "src/crypto/byteorder.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef BYTEORDER_HPP\n#define BYTEORDER_HPP\n\n#include \"src/include/config.h\"\n\n#if HAVE_DECL_BE64TOH || HAVE_DECL_BETOH64\n\n#if defined( HAVE_ENDIAN_H )\n#include <endian.h>\n#elif defined( HAVE_SYS_ENDIAN_H )\n#include <sys/endian.h>\n#include <sys/types.h>\n#endif\n\n#if !HAVE_DECL_BE64TOH && HAVE_DECL_BETOH64\n#define be64toh betoh64\n#define be16toh betoh16\n#endif\n\n#elif HAVE_OSX_SWAP\n#include <libkern/OSByteOrder.h>\n#define htobe64 OSSwapHostToBigInt64\n#define be64toh OSSwapBigToHostInt64\n#define htobe16 OSSwapHostToBigInt16\n#define be16toh OSSwapBigToHostInt16\n\n#else\n\n/* Use our fallback implementation, which is correct for any endianness. */\n\n#include <cstdint>\n\n/* Make sure they aren't macros */\n#undef htobe64\n#undef be64toh\n#undef htobe16\n#undef be16toh\n\n/* Use unions rather than casts, to comply with strict aliasing rules. */\n\ninline uint64_t htobe64( uint64_t x )\n{\n  uint8_t xs[8] = { static_cast<uint8_t>( ( x >> 56 ) & 0xFF ),\n                    static_cast<uint8_t>( ( x >> 48 ) & 0xFF ),\n                    static_cast<uint8_t>( ( x >> 40 ) & 0xFF ),\n                    static_cast<uint8_t>( ( x >> 32 ) & 0xFF ),\n                    static_cast<uint8_t>( ( x >> 24 ) & 0xFF ),\n                    static_cast<uint8_t>( ( x >> 16 ) & 0xFF ),\n                    static_cast<uint8_t>( ( x >> 8 ) & 0xFF ),\n                    static_cast<uint8_t>( (x)&0xFF ) };\n  union {\n    const uint8_t* p8;\n    const uint64_t* p64;\n  } u;\n  u.p8 = xs;\n  return *u.p64;\n}\n\ninline uint64_t be64toh( uint64_t x )\n{\n  union {\n    const uint8_t* p8;\n    const uint64_t* p64;\n  } u;\n  u.p64 = &x;\n  return ( uint64_t( u.p8[0] ) << 56 ) | ( uint64_t( u.p8[1] ) << 48 ) | ( uint64_t( u.p8[2] ) << 40 )\n         | ( uint64_t( u.p8[3] ) << 32 ) | ( uint64_t( u.p8[4] ) << 24 ) | ( uint64_t( u.p8[5] ) << 16 )\n         | ( uint64_t( u.p8[6] ) << 8 ) | ( uint64_t( u.p8[7] ) );\n}\n\ninline uint16_t htobe16( uint16_t x )\n{\n  uint8_t xs[2] = { static_cast<uint8_t>( ( x >> 8 ) & 0xFF ), static_cast<uint8_t>( (x)&0xFF ) };\n  union {\n    const uint8_t* p8;\n    const uint16_t* p16;\n  } u;\n  u.p8 = xs;\n  return *u.p16;\n}\n\ninline uint16_t be16toh( uint16_t x )\n{\n  union {\n    const uint8_t* p8;\n    const uint16_t* p16;\n  } u;\n  u.p16 = &x;\n  return ( uint16_t( u.p8[0] ) << 8 ) | ( uint16_t( u.p8[1] ) );\n}\n\n#endif\n\n#endif\n"
  },
  {
    "path": "src/crypto/crypto.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n#include <cerrno>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <fstream>\n\n#include <sys/resource.h>\n\n#include \"src/crypto/base64.h\"\n#include \"src/crypto/byteorder.h\"\n#include \"src/crypto/crypto.h\"\n#include \"src/crypto/prng.h\"\n#include \"src/util/fatal_assert.h\"\n\nusing namespace Crypto;\n\nlong int myatoi( const char* str )\n{\n  char* end;\n\n  errno = 0;\n  long int ret = strtol( str, &end, 10 );\n\n  if ( ( errno != 0 ) || ( end != str + strlen( str ) ) ) {\n    throw CryptoException( \"Bad integer.\" );\n  }\n\n  return ret;\n}\n\nuint64_t Crypto::unique( void )\n{\n  static uint64_t counter = 0;\n  uint64_t rv = counter++;\n  if ( counter == 0 ) {\n    throw CryptoException( \"Counter wrapped\", true );\n  }\n  return rv;\n}\n\nAlignedBuffer::AlignedBuffer( size_t len, const char* data ) : m_len( len ), m_allocated( NULL ), m_data( NULL )\n{\n  size_t alloc_len = len ? len : 1;\n#if defined( HAVE_POSIX_MEMALIGN )\n  if ( ( 0 != posix_memalign( &m_allocated, 16, alloc_len ) ) || ( m_allocated == NULL ) ) {\n    throw std::bad_alloc();\n  }\n  m_data = (char*)m_allocated;\n\n#else\n  /* malloc() a region 15 bytes larger than we need, and find\n     the aligned offset within. */\n  m_allocated = malloc( 15 + alloc_len );\n  if ( m_allocated == NULL ) {\n    throw std::bad_alloc();\n  }\n\n  uintptr_t iptr = (uintptr_t)m_allocated;\n  if ( iptr & 0xF ) {\n    iptr += 16 - ( iptr & 0xF );\n  }\n  assert( !( iptr & 0xF ) );\n  assert( iptr >= (uintptr_t)m_allocated );\n  assert( iptr <= ( 15 + (uintptr_t)m_allocated ) );\n\n  m_data = (char*)iptr;\n\n#endif /* !defined(HAVE_POSIX_MEMALIGN) */\n\n  if ( data ) {\n    memcpy( m_data, data, len );\n  }\n}\n\nBase64Key::Base64Key( std::string printable_key )\n{\n  if ( printable_key.length() != 22 ) {\n    throw CryptoException( \"Key must be 22 letters long.\" );\n  }\n\n  std::string base64 = printable_key + \"==\";\n\n  size_t len = 16;\n  if ( !base64_decode( base64.data(), 24, key, &len ) ) {\n    throw CryptoException( \"Key must be well-formed base64.\" );\n  }\n\n  if ( len != 16 ) {\n    throw CryptoException( \"Key must represent 16 octets.\" );\n  }\n\n  /* to catch changes after the first 128 bits */\n  if ( printable_key != this->printable_key() ) {\n    throw CryptoException( \"Base64 key was not encoded 128-bit key.\" );\n  }\n}\n\nBase64Key::Base64Key()\n{\n  PRNG().fill( key, sizeof( key ) );\n}\n\nBase64Key::Base64Key( PRNG& prng )\n{\n  prng.fill( key, sizeof( key ) );\n}\n\nstd::string Base64Key::printable_key( void ) const\n{\n  char base64[24];\n\n  base64_encode( key, 16, base64, 24 );\n\n  if ( ( base64[23] != '=' ) || ( base64[22] != '=' ) ) {\n    throw CryptoException( std::string( \"Unexpected output from base64_encode: \" ) + std::string( base64, 24 ) );\n  }\n\n  base64[22] = 0;\n  return std::string( base64 );\n}\n\nSession::Session( Base64Key s_key )\n  : key( s_key ), ctx_buf( ae_ctx_sizeof() ), ctx( (ae_ctx*)ctx_buf.data() ), blocks_encrypted( 0 ),\n    plaintext_buffer( RECEIVE_MTU ), ciphertext_buffer( RECEIVE_MTU ), nonce_buffer( Nonce::NONCE_LEN )\n{\n  if ( AE_SUCCESS != ae_init( ctx, key.data(), 16, 12, 16 ) ) {\n    throw CryptoException( \"Could not initialize AES-OCB context.\" );\n  }\n}\n\nSession::~Session()\n{\n  fatal_assert( ae_clear( ctx ) == AE_SUCCESS );\n}\n\nNonce::Nonce( uint64_t val )\n{\n  uint64_t val_net = htobe64( val );\n\n  memset( bytes, 0, 4 );\n  memcpy( bytes + 4, &val_net, 8 );\n}\n\nuint64_t Nonce::val( void ) const\n{\n  uint64_t ret;\n  memcpy( &ret, bytes + 4, 8 );\n  return be64toh( ret );\n}\n\nNonce::Nonce( const char* s_bytes, size_t len )\n{\n  if ( len != 8 ) {\n    throw CryptoException( \"Nonce representation must be 8 octets long.\" );\n  }\n\n  memset( bytes, 0, 4 );\n  memcpy( bytes + 4, s_bytes, 8 );\n}\n\nconst std::string Session::encrypt( const Message& plaintext )\n{\n  const size_t pt_len = plaintext.text.size();\n  const int ciphertext_len = pt_len + 16;\n\n  assert( (size_t)ciphertext_len <= ciphertext_buffer.len() );\n  assert( pt_len <= plaintext_buffer.len() );\n\n  memcpy( plaintext_buffer.data(), plaintext.text.data(), pt_len );\n  memcpy( nonce_buffer.data(), plaintext.nonce.data(), Nonce::NONCE_LEN );\n\n  if ( ciphertext_len\n       != ae_encrypt( ctx,                      /* ctx */\n                      nonce_buffer.data(),      /* nonce */\n                      plaintext_buffer.data(),  /* pt */\n                      pt_len,                   /* pt_len */\n                      NULL,                     /* ad */\n                      0,                        /* ad_len */\n                      ciphertext_buffer.data(), /* ct */\n                      NULL,                     /* tag */\n                      AE_FINALIZE ) ) {         /* final */\n    throw CryptoException( \"ae_encrypt() returned error.\" );\n  }\n\n  blocks_encrypted += pt_len >> 4;\n  if ( pt_len & 0xF ) {\n    /* partial block */\n    blocks_encrypted++;\n  }\n\n  /* \"Both the privacy and the authenticity properties of OCB degrade as\n      per s^2 / 2^128, where s is the total number of blocks that the\n      adversary acquires.... In order to ensure that s^2 / 2^128 remains\n      small, a given key should be used to encrypt at most 2^48 blocks (2^55\n      bits or 4 petabytes)\"\n\n     -- http://tools.ietf.org/html/draft-krovetz-ocb-03\n\n     We deem it unlikely that a legitimate user will send 4 PB through a Mosh\n     session.  If it happens, we simply kill the session.  The server and\n     client use the same key, so we actually need to die after 2^47 blocks.\n  */\n  if ( blocks_encrypted >> 47 ) {\n    throw CryptoException( \"Encrypted 2^47 blocks.\", true );\n  }\n\n  std::string text( ciphertext_buffer.data(), ciphertext_len );\n\n  return plaintext.nonce.cc_str() + text;\n}\n\nconst Message Session::decrypt( const char* str, size_t len )\n{\n  if ( len < 24 ) {\n    throw CryptoException( \"Ciphertext must contain nonce and tag.\" );\n  }\n\n  int body_len = len - 8;\n  int pt_len = body_len - 16;\n\n  if ( pt_len < 0 ) { /* super-assertion that pt_len does not equal AE_INVALID */\n    fprintf( stderr, \"BUG.\\n\" );\n    exit( 1 );\n  }\n\n  assert( (size_t)body_len <= ciphertext_buffer.len() );\n  assert( (size_t)pt_len <= plaintext_buffer.len() );\n\n  Nonce nonce( str, 8 );\n  memcpy( ciphertext_buffer.data(), str + 8, body_len );\n  memcpy( nonce_buffer.data(), nonce.data(), Nonce::NONCE_LEN );\n\n  if ( pt_len\n       != ae_decrypt( ctx,                      /* ctx */\n                      nonce_buffer.data(),      /* nonce */\n                      ciphertext_buffer.data(), /* ct */\n                      body_len,                 /* ct_len */\n                      NULL,                     /* ad */\n                      0,                        /* ad_len */\n                      plaintext_buffer.data(),  /* pt */\n                      NULL,                     /* tag */\n                      AE_FINALIZE ) ) {         /* final */\n    throw CryptoException( \"Packet failed integrity check.\" );\n  }\n\n  const Message ret( nonce, std::string( plaintext_buffer.data(), pt_len ) );\n\n  return ret;\n}\n\nstatic rlim_t saved_core_rlimit;\n\n/* Disable dumping core, as a precaution to avoid saving sensitive data\n   to disk. */\nvoid Crypto::disable_dumping_core( void )\n{\n  struct rlimit limit;\n  if ( 0 != getrlimit( RLIMIT_CORE, &limit ) ) {\n    /* We don't throw CryptoException because this is called very early\n       in main(), outside of 'try'. */\n    perror( \"getrlimit(RLIMIT_CORE)\" );\n    exit( 1 );\n  }\n\n  saved_core_rlimit = limit.rlim_cur;\n  limit.rlim_cur = 0;\n  if ( 0 != setrlimit( RLIMIT_CORE, &limit ) ) {\n    perror( \"setrlimit(RLIMIT_CORE)\" );\n    exit( 1 );\n  }\n}\n\nvoid Crypto::reenable_dumping_core( void )\n{\n  /* Silent failure is safe. */\n  struct rlimit limit;\n  if ( 0 == getrlimit( RLIMIT_CORE, &limit ) ) {\n    limit.rlim_cur = saved_core_rlimit;\n    setrlimit( RLIMIT_CORE, &limit );\n  }\n}\n"
  },
  {
    "path": "src/crypto/crypto.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef CRYPTO_HPP\n#define CRYPTO_HPP\n\n#include \"src/crypto/ae.h\"\n\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <exception>\n#include <string>\n\nlong int myatoi( const char* str );\n\nclass PRNG;\n\nnamespace Crypto {\nclass CryptoException : public std::exception\n{\npublic:\n  std::string text;\n  bool fatal;\n  CryptoException( std::string s_text, bool s_fatal = false ) : text( s_text ), fatal( s_fatal ) {};\n  const char* what() const throw() { return text.c_str(); }\n  ~CryptoException() throw() {}\n};\n\n/*\n * OCB (and other algorithms) require a source of nonce/sequence\n * numbers that never repeats its output.  Enforce that with this\n * function.\n */\nuint64_t unique( void );\n\n/* 16-byte-aligned buffer, with length. */\nclass AlignedBuffer\n{\nprivate:\n  size_t m_len;\n  void* m_allocated;\n  char* m_data;\n\npublic:\n  AlignedBuffer( size_t len, const char* data = NULL );\n\n  ~AlignedBuffer() { free( m_allocated ); }\n\n  char* data( void ) const { return m_data; }\n  size_t len( void ) const { return m_len; }\n\nprivate:\n  /* Not implemented */\n  AlignedBuffer( const AlignedBuffer& );\n  AlignedBuffer& operator=( const AlignedBuffer& );\n};\n\nclass Base64Key\n{\nprivate:\n  unsigned char key[16];\n\npublic:\n  Base64Key(); /* random key */\n  Base64Key( PRNG& prng );\n  Base64Key( std::string printable_key );\n  std::string printable_key( void ) const;\n  unsigned char* data( void ) { return key; }\n};\n\nclass Nonce\n{\npublic:\n  static const int NONCE_LEN = 12;\n\nprivate:\n  char bytes[NONCE_LEN];\n\npublic:\n  Nonce( uint64_t val );\n  Nonce( const char* s_bytes, size_t len );\n\n  std::string cc_str( void ) const { return std::string( bytes + 4, 8 ); }\n  const char* data( void ) const { return bytes; }\n  uint64_t val( void ) const;\n};\n\nclass Message\n{\npublic:\n  const Nonce nonce;\n  const std::string text;\n\n  Message( const char* nonce_bytes, size_t nonce_len, const char* text_bytes, size_t text_len )\n    : nonce( nonce_bytes, nonce_len ), text( text_bytes, text_len )\n  {}\n\n  Message( const Nonce& s_nonce, const std::string& s_text ) : nonce( s_nonce ), text( s_text ) {}\n};\n\nclass Session\n{\nprivate:\n  Base64Key key;\n  AlignedBuffer ctx_buf;\n  ae_ctx* ctx;\n  uint64_t blocks_encrypted;\n\n  AlignedBuffer plaintext_buffer;\n  AlignedBuffer ciphertext_buffer;\n  AlignedBuffer nonce_buffer;\n\npublic:\n  static const int RECEIVE_MTU = 2048;\n  /* Overhead (not counting the nonce, which is handled by network transport) */\n  static const int ADDED_BYTES = 16 /* final OCB block */;\n\n  Session( Base64Key s_key );\n  ~Session();\n\n  const std::string encrypt( const Message& plaintext );\n  const Message decrypt( const char* str, size_t len );\n  const Message decrypt( const std::string& ciphertext ) { return decrypt( ciphertext.data(), ciphertext.size() ); }\n\n  Session( const Session& );\n  Session& operator=( const Session& );\n};\n\nvoid disable_dumping_core( void );\nvoid reenable_dumping_core( void );\n}\n\n#endif\n"
  },
  {
    "path": "src/crypto/ocb_internal.cc",
    "content": "/*------------------------------------------------------------------------\n/ OCB Version 3 Reference Code (Optimized C)     Last modified 08-SEP-2012\n/-------------------------------------------------------------------------\n/ Copyright (c) 2012 Ted Krovetz.\n/ Copyright 2022 Google LLC\n/\n/ Permission to use, copy, modify, and/or distribute this software for any\n/ purpose with or without fee is hereby granted, provided that the above\n/ copyright notice and this permission notice appear in all copies.\n/\n/ THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n/ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n/ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n/ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n/ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n/ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n/ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n/\n/ Phillip Rogaway holds patents relevant to OCB. See the following for\n/ his patent grant: http://www.cs.ucdavis.edu/~rogaway/ocb/grant.htm\n/\n/ Special thanks to Keegan McAllister for suggesting several good improvements\n/\n/ Comments are welcome: Ted Krovetz <ted@krovetz.net> - Dedicated to Laurel K\n/------------------------------------------------------------------------- */\n\n// clang-format off\n\n#include \"src/include/config.h\"\n\n/* This module implements the ae.h interface for OpenSSL, Apple Common\n/  Crypto, and Nettle.                                                     */\n#if !defined(USE_OPENSSL_AES) && !defined(USE_APPLE_COMMON_CRYPTO_AES) && \\\n    !defined(USE_NETTLE_AES)\n#error ocb_internal.cc only works with OpenSSL, Apple Common Crypto, or Nettle\n#endif\n\n/* ----------------------------------------------------------------------- */\n/* Usage notes                                                             */\n/* ----------------------------------------------------------------------- */\n\n/* - When AE_PENDING is passed as the 'final' parameter of any function,\n/    the length parameters must be a multiple of (BPI*16).\n/  - When available, SSE or AltiVec registers are used to manipulate data.\n/    So, when on machines with these facilities, all pointers passed to\n/    any function should be 16-byte aligned.\n/  - Plaintext and ciphertext pointers may be equal (ie, plaintext gets\n/    encrypted in-place), but no other pair of pointers may be equal.\n/  - This code assumes all x86 processors have SSE2 and SSSE3 instructions\n/    when compiling under MSVC. If untrue, alter the #define.\n/  - This code is tested for C99 and recent versions of GCC and MSVC.      */\n\n/* ----------------------------------------------------------------------- */\n/* User configuration options                                              */\n/* ----------------------------------------------------------------------- */\n\n/* Set the AES key length to use and length of authentication tag to produce.\n/  Setting either to 0 requires the value be set at runtime via ae_init().\n/  Some optimizations occur for each when set to a fixed value.            */\n#define OCB_KEY_LEN         16  /* 0, 16, 24 or 32. 0 means set in ae_init */\n#define OCB_TAG_LEN         16  /* 0 to 16. 0 means set in ae_init         */\n\n/* This implementation has built-in support for multiple AES APIs. Set any\n/  one of the following to non-zero to specify which to use.               */\n#if 0\n#define USE_APPLE_COMMON_CRYPTO_AES       0\n#define USE_NETTLE_AES       0\n#define USE_OPENSSL_AES      1  /* http://openssl.org                      */\n#endif\n\n/* During encryption and decryption, various \"L values\" are required.\n/  The L values can be precomputed during initialization (requiring extra\n/  space in ae_ctx), generated as needed (slightly slowing encryption and\n/  decryption), or some combination of the two. L_TABLE_SZ specifies how many\n/  L values to precompute. L_TABLE_SZ must be at least 3. L_TABLE_SZ*16 bytes\n/  are used for L values in ae_ctx. Plaintext and ciphertexts shorter than\n/  2^L_TABLE_SZ blocks need no L values calculated dynamically.            */\n#define L_TABLE_SZ          16\n\n/* Set L_TABLE_SZ_IS_ENOUGH non-zero iff you know that all plaintexts\n/  will be shorter than 2^(L_TABLE_SZ+4) bytes in length. This results\n/  in better performance.                                                  */\n#define L_TABLE_SZ_IS_ENOUGH 1\n\n/* ----------------------------------------------------------------------- */\n/* Includes and compiler specific definitions                              */\n/* ----------------------------------------------------------------------- */\n\n#include \"src/crypto/ae.h\"\n#include \"src/crypto/crypto.h\"\n#include \"src/util/fatal_assert.h\"\n#include <cstdlib>\n#include <cstring>\n#if defined(HAVE_STRINGS_H)\n#include <strings.h>\n#endif\n#if defined(HAVE_ENDIAN_H)\n#include <endian.h>\n#elif defined(HAVE_SYS_ENDIAN_H)\n#include <sys/types.h>\n#include <sys/endian.h>\n#endif\n\n#include <new>\n\n/* Define standard sized integers                                          */\n#if defined(_MSC_VER) && (_MSC_VER < 1600)\n\ttypedef unsigned __int8  uint8_t;\n\ttypedef unsigned __int32 uint32_t;\n\ttypedef unsigned __int64 uint64_t;\n\ttypedef          __int64 int64_t;\n#else\n\t#include <stdint.h>\n#endif\n\n/* Compiler-specific intrinsics and fixes: bswap64, ntz                    */\n#if _MSC_VER\n\t#define inline __inline        /* MSVC doesn't recognize \"inline\" in C */\n\t#define restrict __restrict  /* MSVC doesn't recognize \"restrict\" in C */\n    #define __SSE2__   (_M_IX86 || _M_AMD64 || _M_X64)    /* Assume SSE2  */\n    #define __SSSE3__  (_M_IX86 || _M_AMD64 || _M_X64)    /* Assume SSSE3 */\n\t#include <intrin.h>\n\t#pragma intrinsic(_byteswap_uint64, _BitScanForward, memcpy)\n#elif __GNUC__\n\t#ifndef inline\n\t#define inline __inline__            /* No \"inline\" in GCC ansi C mode */\n\t#endif\n\t#ifndef restrict\n\t#define restrict __restrict__      /* No \"restrict\" in GCC ansi C mode */\n\t#endif\n#endif\n\n#if _MSC_VER\n\t#define bswap64(x) _byteswap_uint64(x)\n#elif HAVE_DECL_BSWAP64\n\t/* nothing */\n#elif HAVE_DECL___BUILTIN_BSWAP64\n\t#define bswap64(x) __builtin_bswap64(x)           /* GCC 4.3+ */\n#else\n\t#define bswap32(x)                                              \\\n\t   ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) | \\\n\t\t(((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))\n\n\t static inline uint64_t bswap64(uint64_t x) {\n\t\tunion { uint64_t u64; uint32_t u32[2]; } in, out;\n\t\tin.u64 = x;\n\t\tout.u32[0] = bswap32(in.u32[1]);\n\t\tout.u32[1] = bswap32(in.u32[0]);\n\t\treturn out.u64;\n\t}\n#endif\n\n#if _MSC_VER\n\tstatic inline unsigned ntz(unsigned x) {_BitScanForward(&x,x);return x;}\n#elif HAVE_DECL___BUILTIN_CTZ\n\t#define ntz(x)     __builtin_ctz((unsigned)(x))   /* GCC 3.4+ */\n#elif HAVE_DECL_FFS\n\t#define ntz(x)     (ffs(x) - 1)\n#else\n\t#if (L_TABLE_SZ <= 9) && (L_TABLE_SZ_IS_ENOUGH)   /* < 2^13 byte texts */\n\tstatic inline unsigned ntz(unsigned x) {\n\t\tstatic const unsigned char tz_table[] = {0,\n\t\t2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,7,\n\t\t2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,8,\n\t\t2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,7,\n\t\t2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2};\n\t\treturn tz_table[x/4];\n\t}\n\t#else       /* From http://supertech.csail.mit.edu/papers/debruijn.pdf */\n\tstatic inline unsigned ntz(unsigned x) {\n\t\tstatic const unsigned char tz_table[32] =\n\t\t{ 0,  1, 28,  2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17,  4, 8,\n\t\t 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18,  6, 11,  5, 10, 9};\n\t\treturn tz_table[((uint32_t)((x & -x) * 0x077CB531u)) >> 27];\n\t}\n\t#endif\n#endif\n\n/* ----------------------------------------------------------------------- */\n/* Define blocks and operations -- Patch if incorrect on your compiler.    */\n/* ----------------------------------------------------------------------- */\n\n#if __SSE2__\n    #include <xmmintrin.h>              /* SSE instructions and _mm_malloc */\n    #include <emmintrin.h>              /* SSE2 instructions               */\n    typedef __m128i block;\n    #define xor_block(x,y)        _mm_xor_si128(x,y)\n    #define zero_block()          _mm_setzero_si128()\n    #define unequal_blocks(x,y) \\\n    \t\t\t\t\t   (_mm_movemask_epi8(_mm_cmpeq_epi8(x,y)) != 0xffff)\n\t#if __SSSE3__\n    #include <tmmintrin.h>              /* SSSE3 instructions              */\n    #define swap_if_le(b) \\\n      _mm_shuffle_epi8(b,_mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15))\n\t#else\n    static inline block swap_if_le(block b) {\n\t\tblock a = _mm_shuffle_epi32  (b, _MM_SHUFFLE(0,1,2,3));\n\t\ta = _mm_shufflehi_epi16(a, _MM_SHUFFLE(2,3,0,1));\n\t\ta = _mm_shufflelo_epi16(a, _MM_SHUFFLE(2,3,0,1));\n\t\treturn _mm_xor_si128(_mm_srli_epi16(a,8), _mm_slli_epi16(a,8));\n    }\n\t#endif\n\tstatic inline block gen_offset(uint64_t KtopStr[3], unsigned bot) {\n\t\tblock hi = _mm_load_si128((__m128i *)(KtopStr+0));   /* hi = B A */\n\t\tblock lo = _mm_loadu_si128((__m128i *)(KtopStr+1));  /* lo = C B */\n\t\t__m128i lshift = _mm_cvtsi32_si128(bot);\n\t\t__m128i rshift = _mm_cvtsi32_si128(64-bot);\n\t\tlo = _mm_xor_si128(_mm_sll_epi64(hi,lshift),_mm_srl_epi64(lo,rshift));\n\t\t#if __SSSE3__\n\t\treturn _mm_shuffle_epi8(lo,_mm_set_epi8(8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7));\n\t\t#else\n\t\treturn swap_if_le(_mm_shuffle_epi32(lo, _MM_SHUFFLE(1,0,3,2)));\n\t\t#endif\n\t}\n\tstatic inline block double_block(block bl) {\n\t\tconst __m128i mask = _mm_set_epi32(135,1,1,1);\n\t\t__m128i tmp = _mm_srai_epi32(bl, 31);\n\t\ttmp = _mm_and_si128(tmp, mask);\n\t\ttmp = _mm_shuffle_epi32(tmp, _MM_SHUFFLE(2,1,0,3));\n\t\tbl = _mm_slli_epi32(bl, 1);\n\t\treturn _mm_xor_si128(bl,tmp);\n\t}\n#elif __ALTIVEC__ && _CALL_ELF != 2\n    #include <altivec.h>\n    typedef vector unsigned block;\n    #define xor_block(x,y)         vec_xor(x,y)\n    #define zero_block()           vec_splat_u32(0)\n    #define unequal_blocks(x,y)    vec_any_ne(x,y)\n    #define swap_if_le(b)          (b)\n\t#if __PPC64__\n\tstatic block gen_offset(uint64_t KtopStr[3], unsigned bot) {\n\t\tunion {uint64_t u64[2]; block bl;} rval;\n\t\trval.u64[0] = (KtopStr[0] << bot) | (KtopStr[1] >> (64-bot));\n\t\trval.u64[1] = (KtopStr[1] << bot) | (KtopStr[2] >> (64-bot));\n        return rval.bl;\n\t}\n\t#else\n\t/* Special handling: Shifts are mod 32, and no 64-bit types */\n\tstatic block gen_offset(uint64_t KtopStr[3], unsigned bot) {\n\t\tconst vector unsigned k32 = {32,32,32,32};\n\t\tvector unsigned hi = *(vector unsigned *)(KtopStr+0);\n\t\tvector unsigned lo = *(vector unsigned *)(KtopStr+2);\n\t\tvector unsigned bot_vec;\n\t\tif (bot < 32) {\n\t\t\tlo = vec_sld(hi,lo,4);\n\t\t} else {\n\t\t\tvector unsigned t = vec_sld(hi,lo,4);\n\t\t\tlo = vec_sld(hi,lo,8);\n\t\t\thi = t;\n\t\t\tbot = bot - 32;\n\t\t}\n\t\tif (bot == 0) return hi;\n\t\t*(unsigned *)&bot_vec = bot;\n\t\tvector unsigned lshift = vec_splat(bot_vec,0);\n\t\tvector unsigned rshift = vec_sub(k32,lshift);\n\t\thi = vec_sl(hi,lshift);\n\t\tlo = vec_sr(lo,rshift);\n\t\treturn vec_xor(hi,lo);\n\t}\n\t#endif\n\tstatic inline block double_block(block b) {\n\t\tconst vector unsigned char mask = {135,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};\n\t\tconst vector unsigned char perm = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};\n\t\tconst vector unsigned char shift7  = vec_splat_u8(7);\n\t\tconst vector unsigned char shift1  = vec_splat_u8(1);\n\t\tvector unsigned char c = (vector unsigned char)b;\n\t\tvector unsigned char t = vec_sra(c,shift7);\n\t\tt = vec_and(t,mask);\n\t\tt = vec_perm(t,t,perm);\n\t\tc = vec_sl(c,shift1);\n\t\treturn (block)vec_xor(c,t);\n\t}\n#elif __ARM_NEON__\n    #include <arm_neon.h>\n    typedef int8x16_t block;      /* Yay! Endian-neutral reads! */\n    #define xor_block(x,y)             veorq_s8(x,y)\n    #define zero_block()               vdupq_n_s8(0)\n    static inline int unequal_blocks(block a, block b) {\n\t\tint64x2_t t=veorq_s64((int64x2_t)a,(int64x2_t)b);\n\t\treturn (vgetq_lane_s64(t,0)|vgetq_lane_s64(t,1))!=0;\n    }\n    #define swap_if_le(b)          (b)  /* Using endian-neutral int8x16_t */\n\t/* KtopStr is reg correct by 64 bits, return mem correct */\n\tstatic block gen_offset(uint64_t KtopStr[3], unsigned bot) {\n\t\tconst union { unsigned x; unsigned char endian; } little = { 1 };\n\t\tconst int64x2_t k64 = {-64,-64};\n\t\tuint64x2_t hi, lo;\n\t\tmemcpy(&hi, KtopStr, sizeof(hi));\n\t\tmemcpy(&lo, KtopStr+1, sizeof(lo));\n\t\tint64x2_t ls = vdupq_n_s64(bot);\n\t\tint64x2_t rs = vqaddq_s64(k64,ls);\n\t\tblock rval = (block)veorq_u64(vshlq_u64(hi,ls),vshlq_u64(lo,rs));\n\t\tif (little.endian)\n\t\t\trval = vrev64q_s8(rval);\n\t\treturn rval;\n\t}\n\tstatic inline block double_block(block b)\n\t{\n\t\tconst block mask = {-121,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};\n\t\tblock tmp = vshrq_n_s8(b,7);\n\t\ttmp = vandq_s8(tmp, mask);\n\t\ttmp = vextq_s8(tmp, tmp, 1);  /* Rotate high byte to end */\n\t\tb = vshlq_n_s8(b,1);\n\t\treturn veorq_s8(tmp,b);\n\t}\n#else\n    typedef struct { uint64_t l,r; } block;\n    static inline block xor_block(block x, block y) {\n    \tx.l^=y.l; x.r^=y.r; return x;\n    }\n    static inline block zero_block(void) { const block t = {0,0}; return t; }\n    #define unequal_blocks(x, y)         ((((x).l^(y).l)|((x).r^(y).r)) != 0)\n    static inline block swap_if_le(block b) {\n\t\tconst union { unsigned x; unsigned char endian; } little = { 1 };\n    \tif (little.endian) {\n    \t\tblock r;\n    \t\tr.l = bswap64(b.l);\n    \t\tr.r = bswap64(b.r);\n    \t\treturn r;\n    \t} else\n    \t\treturn b;\n    }\n\n\t/* KtopStr is reg correct by 64 bits, return mem correct */\n\tstatic block gen_offset(uint64_t KtopStr[3], unsigned bot) {\n        block rval;\n        if (bot != 0) {\n\t\t\trval.l = (KtopStr[0] << bot) | (KtopStr[1] >> (64-bot));\n\t\t\trval.r = (KtopStr[1] << bot) | (KtopStr[2] >> (64-bot));\n\t\t} else {\n\t\t\trval.l = KtopStr[0];\n\t\t\trval.r = KtopStr[1];\n\t\t}\n        return swap_if_le(rval);\n\t}\n\n\t#if __GNUC__ && !__clang__ && __arm__\n\tstatic inline block double_block(block b) {\n\t\t__asm__ (\"adds %1,%1,%1\\n\\t\"\n\t\t\t\t \"adcs %H1,%H1,%H1\\n\\t\"\n\t\t\t\t \"adcs %0,%0,%0\\n\\t\"\n\t\t\t\t \"adcs %H0,%H0,%H0\\n\\t\"\n\t\t\t\t \"it cs\\n\\t\"\n\t\t\t\t \"eorcs %1,%1,#135\"\n\t\t: \"+r\"(b.l), \"+r\"(b.r) : : \"cc\");\n\t\treturn b;\n\t}\n\t#else\n\tstatic inline block double_block(block b) {\n\t\tuint64_t t = (uint64_t)((int64_t)b.l >> 63);\n\t\tb.l = (b.l + b.l) ^ (b.r >> 63);\n\t\tb.r = (b.r + b.r) ^ (t & 135);\n\t\treturn b;\n\t}\n\t#endif\n\n#endif\n\n/* ----------------------------------------------------------------------- */\n/* AES                                                                     */\n/* ----------------------------------------------------------------------- */\n\n/*---------------*/\n#if USE_OPENSSL_AES\n/*---------------*/\n\n#include <openssl/evp.h>                            /* http://openssl.org/ */\n\nnamespace ocb_aes {\n\ntypedef EVP_CIPHER_CTX KEY;\n\nenum { BLOCK_SIZE = 16 };\n\nstatic KEY *KEY_new() {\n\tKEY *key = EVP_CIPHER_CTX_new();\n\tif (key == NULL) {\n\t\tthrow std::bad_alloc();\n\t}\n\treturn key;\n}\n\nstatic void KEY_delete(KEY *key) { EVP_CIPHER_CTX_free(key); }\n\nstatic void set_encrypt_key(const unsigned char *user_key, int bits, KEY *key) {\n\t// Do not copy and paste this code! It is far too low-level to be\n\t// general-purpose. If you're looking for an example of using AEAD\n\t// through OpenSSL's EVP_CIPHER API, have a look at ocb_openssl.cc\n\t// instead.\n\t//\n\t// This function and the others in this section replicate the behavior of\n\t// OpenSSL's deprecated AES_* primitives. Those primitives implemented AES\n\t// without any block cipher mode--that is, in ECB mode. Normally, using ECB\n\t// mode anywhere would be questionable, but it's safe here because it's\n\t// being used to implement a higher-level cryptographic mode (OCB mode),\n\t// which is in turn used by Mosh.\n\n\tfatal_assert(bits == 128);\n\tif (EVP_EncryptInit_ex(key, EVP_aes_128_ecb(), /*impl=*/NULL, user_key, /*iv=*/NULL) != 1 ||\n\t\t\tEVP_CIPHER_CTX_set_padding(key, false) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not initialize AES encryption context.\");\n\t}\n}\n\nstatic void set_decrypt_key(const unsigned char *user_key, int bits, KEY *key) {\n\t// Do not copy and paste this code! See notes in set_encrypt_key.\n\tfatal_assert(bits == 128);\n\tif (EVP_DecryptInit_ex(key, EVP_aes_128_ecb(), /*impl=*/NULL, user_key, /*iv=*/NULL) != 1 ||\n\t\t\tEVP_CIPHER_CTX_set_padding(key, false) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not initialize AES decryption context.\");\n\t}\n}\n\nstatic void encrypt(const unsigned char *in, unsigned char *out, KEY *key) {\n\t// Even though the functions in this section use ECB mode (which is\n\t// stateless), OpenSSL still requires calls to EncryptInit and\n\t// EncryptFinal. Since ECB mode has no IV and they key is unchanged,\n\t// every parameter to this function can be NULL (which OpenSSL\n\t// interprets as \"don't change this\").\n\tif (EVP_EncryptInit_ex(key, /*type=*/NULL, /*impl=*/NULL, /*key=*/NULL, /*iv=*/NULL) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not start AES encryption operation.\");\n\t}\n\n\tint len;\n\tif (EVP_EncryptUpdate(key, out, &len, in, BLOCK_SIZE) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not AES-encrypt block.\");\n\t}\n\n\tint total_len = len;\n\tif (EVP_EncryptFinal_ex(key, out + total_len, &len) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not finish AES encryption operation.\");\n\t}\n\ttotal_len += len;\n\tfatal_assert(total_len == BLOCK_SIZE);\n}\n\nstatic void decrypt(const unsigned char *in, unsigned char *out, KEY *key) {\n\t// See notes in encrypt about EncryptInit and EncryptFinal; the same\n\t// notes apply to DecryptInit and DecryptFinal here.\n\tif (EVP_DecryptInit_ex(key, /*type=*/NULL, /*impl=*/NULL, /*key=*/NULL, /*iv=*/NULL) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not start AES decryption operation.\");\n\t}\n\n\tint len;\n\tif (EVP_DecryptUpdate(key, out, &len, in, BLOCK_SIZE) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not AES-decrypt block.\");\n\t}\n\n\tint total_len = len;\n\tif (EVP_DecryptFinal_ex(key, out + total_len, &len) != 1) {\n\t\tthrow Crypto::CryptoException(\"Could not finish AES decryption operation.\");\n\t}\n\ttotal_len += len;\n\tfatal_assert(total_len == BLOCK_SIZE);\n}\n\n/* How to ECB encrypt an array of blocks, in place                         */\nstatic void ecb_encrypt_blks(block *blks, unsigned nblks, KEY *key) {\n\twhile (nblks) {\n\t\t--nblks;\n\t\tencrypt(reinterpret_cast<unsigned char *>(blks+nblks), reinterpret_cast<unsigned char *>(blks+nblks), key);\n\t}\n}\n\nstatic void ecb_decrypt_blks(block *blks, unsigned nblks, KEY *key) {\n\twhile (nblks) {\n\t\t--nblks;\n\t\tdecrypt(reinterpret_cast<unsigned char *>(blks+nblks), reinterpret_cast<unsigned char *>(blks+nblks), key);\n\t}\n}\n\n}  // namespace ocb_aes\n\n#define BPI 4  /* Number of blocks in buffer per ECB call */\n\n/*-------------------*/\n#elif USE_APPLE_COMMON_CRYPTO_AES\n/*-------------------*/\n\n#include <CommonCrypto/CommonCryptor.h>\n\nnamespace ocb_aes {\n\ntypedef struct {\n\tCCCryptorRef ref;\n\tuint8_t b[4096];\n} KEY;\n\nstatic KEY *KEY_new() { return new KEY; }\n\nstatic void KEY_delete(KEY *key) { delete key; }\n\nstatic void set_encrypt_key(const unsigned char *handle, const int bits, KEY *key)\n{\n\tCCCryptorStatus rv = CCCryptorCreateFromData(\n\t\tkCCEncrypt,\n\t\tkCCAlgorithmAES128,\n\t\tkCCOptionECBMode,\n\t\thandle,\n\t\tbits / 8,\n\t\tNULL,\n\t\t&(key->b),\n\t\tsizeof (key->b),\n\t\t&(key->ref),\n\t\tNULL);\n\n\tfatal_assert(rv == kCCSuccess);\n}\nstatic void set_decrypt_key(const unsigned char *handle, const int bits, KEY *key)\n{\n\tCCCryptorStatus rv = CCCryptorCreateFromData(\n\t\tkCCDecrypt,\n\t\tkCCAlgorithmAES128,\n\t\tkCCOptionECBMode,\n\t\thandle,\n\t\tbits / 8,\n\t\tNULL,\n\t\t&(key->b),\n\t\tsizeof (key->b),\n\t\t&(key->ref),\n\t\tNULL);\n\n\tfatal_assert(rv == kCCSuccess);\n}\nstatic void encrypt(unsigned char *src, unsigned char *dst, KEY *key) {\n\tsize_t dataOutMoved;\n\tCCCryptorStatus rv = CCCryptorUpdate(\n\t\tkey->ref,\n\t\t(const void *)src,\n\t\tkCCBlockSizeAES128,\n\t\t(void *)dst,\n\t\tkCCBlockSizeAES128,\n\t\t&dataOutMoved);\n\tfatal_assert(rv == kCCSuccess);\n\tfatal_assert(dataOutMoved == kCCBlockSizeAES128);\n}\n#if 0\n/* unused */\nstatic void decrypt(unsigned char *src, unsigned char *dst, KEY *key) {\n\tencrypt(src, dst, key);\n}\n#endif\nstatic void ecb_encrypt_blks(block *blks, unsigned nblks, KEY *key) {\n\tconst size_t dataSize = kCCBlockSizeAES128 * nblks;\n\tsize_t dataOutMoved;\n\tCCCryptorStatus rv = CCCryptorUpdate(\n\t\tkey->ref,\n\t\t(const void *)blks,\n\t\tdataSize,\n\t\t(void *)blks,\n\t\tdataSize,\n\t\t&dataOutMoved);\n\tfatal_assert(rv == kCCSuccess);\n\tfatal_assert(dataOutMoved == dataSize);\n}\nstatic void ecb_decrypt_blks(block *blks, unsigned nblks, KEY *key) {\n\tecb_encrypt_blks(blks, nblks, key);\n}\n\n}  // namespace ocb_aes\n\n#define BPI 4  /* Number of blocks in buffer per ECB call */\n\n/*-------------------*/\n#elif USE_NETTLE_AES\n/*-------------------*/\n\n#include <nettle/aes.h>\n\nnamespace ocb_aes {\n\ntypedef struct aes128_ctx KEY;\n\nstatic KEY *KEY_new() { return new KEY; }\n\nstatic void KEY_delete(KEY *key) { delete key; }\n\nstatic void set_encrypt_key(const unsigned char *handle, const int bits, KEY *key)\n{\n\tfatal_assert(bits == 128);\n\tnettle_aes128_set_encrypt_key(key, (const uint8_t *)handle);\n}\nstatic void set_decrypt_key(const unsigned char *handle, const int bits, KEY *key)\n{\n\tfatal_assert(bits == 128);\n\tnettle_aes128_set_decrypt_key(key, (const uint8_t *)handle);\n}\nstatic void encrypt(unsigned char *src, unsigned char *dst, KEY *key) {\n\tnettle_aes128_encrypt(key, AES_BLOCK_SIZE, dst, src);\n}\n#if 0\n/* unused */\nstatic void decrypt(unsigned char *src, unsigned char *dst, KEY *key) {\n\tnettle_aes128_decrypt(key, AES_BLOCK_SIZE, dst, src);\n}\n#endif\nstatic void ecb_encrypt_blks(block *blks, unsigned nblks, KEY *key) {\n\tnettle_aes128_encrypt(key, nblks * AES_BLOCK_SIZE, (unsigned char*)blks, (unsigned char*)blks);\n}\nstatic void ecb_decrypt_blks(block *blks, unsigned nblks, KEY *key) {\n\tnettle_aes128_decrypt(key, nblks * AES_BLOCK_SIZE, (unsigned char*)blks, (unsigned char*)blks);\n}\n\n}  // namespace ocb_aes\n\n#define BPI 4  /* Number of blocks in buffer per ECB call */\n\n#else\n#error \"No AES implementation selected.\"\n#endif\n\n/* ----------------------------------------------------------------------- */\n/* Define OCB context structure.                                           */\n/* ----------------------------------------------------------------------- */\n\n/*------------------------------------------------------------------------\n/ Each item in the OCB context is stored either \"memory correct\" or\n/ \"register correct\". On big-endian machines, this is identical. On\n/ little-endian machines, one must choose whether the byte-string\n/ is in the correct order when it resides in memory or in registers.\n/ It must be register correct whenever it is to be manipulated\n/ arithmetically, but must be memory correct whenever it interacts\n/ with the plaintext or ciphertext.\n/------------------------------------------------------------------------- */\n\nstruct _ae_ctx {\n    block offset;                          /* Memory correct               */\n    block checksum;                        /* Memory correct               */\n    block Lstar;                           /* Memory correct               */\n    block Ldollar;                         /* Memory correct               */\n    block L[L_TABLE_SZ];                   /* Memory correct               */\n    block ad_checksum;                     /* Memory correct               */\n    block ad_offset;                       /* Memory correct               */\n    block cached_Top;                      /* Memory correct               */\n\tuint64_t KtopStr[3];                   /* Register correct, each item  */\n    uint32_t ad_blocks_processed;\n    uint32_t blocks_processed;\n    ocb_aes::KEY *decrypt_key;\n    ocb_aes::KEY *encrypt_key;\n    #if (OCB_TAG_LEN == 0)\n    unsigned tag_len;\n    #endif\n};\n\n/* ----------------------------------------------------------------------- */\n/* L table lookup (or on-the-fly generation)                               */\n/* ----------------------------------------------------------------------- */\n\n#if L_TABLE_SZ_IS_ENOUGH\n#define getL(_ctx, _tz) ((_ctx)->L[_tz])\n#else\nstatic block getL(const ae_ctx *ctx, unsigned tz)\n{\n    if (tz < L_TABLE_SZ)\n        return ctx->L[tz];\n    else {\n        unsigned i;\n        /* Bring L[MAX] into registers, make it register correct */\n        block rval = swap_if_le(ctx->L[L_TABLE_SZ-1]);\n        rval = double_block(rval);\n        for (i=L_TABLE_SZ; i < tz; i++)\n            rval = double_block(rval);\n        return swap_if_le(rval);             /* To memory correct */\n    }\n}\n#endif\n\n/* ----------------------------------------------------------------------- */\n/* Public functions                                                        */\n/* ----------------------------------------------------------------------- */\n\n/* 32-bit SSE2 and Altivec systems need to be forced to allocate memory\n   on 16-byte alignments. (I believe all major 64-bit systems do already.) */\n\n/* Mosh uses its own AlignedBuffer class, not ae_allocate() or ae_free(). */\n\n/* ----------------------------------------------------------------------- */\n\nint ae_clear (ae_ctx *ctx) /* Zero ae_ctx and undo initialization          */\n{\n\tocb_aes::KEY_delete(ctx->encrypt_key);\n\tocb_aes::KEY_delete(ctx->decrypt_key);\n\tmemset(ctx, 0, sizeof(ae_ctx));\n\treturn AE_SUCCESS;\n}\n\nint ae_ctx_sizeof(void) { return (int) sizeof(ae_ctx); }\n\n/* ----------------------------------------------------------------------- */\n\nint ae_init(ae_ctx *ctx, const void *key, int key_len, int nonce_len, int tag_len)\n{\n    unsigned i;\n    block tmp_blk;\n\n    if (nonce_len != 12)\n    \treturn AE_NOT_SUPPORTED;\n\n    ctx->decrypt_key = ocb_aes::KEY_new();\n    ctx->encrypt_key = ocb_aes::KEY_new();\n\n    /* Initialize encryption & decryption keys */\n    #if (OCB_KEY_LEN > 0)\n    key_len = OCB_KEY_LEN;\n    #endif\n    ocb_aes::set_encrypt_key(reinterpret_cast<const unsigned char *>(key), key_len*8, ctx->encrypt_key);\n    ocb_aes::set_decrypt_key(reinterpret_cast<const unsigned char *>(key), static_cast<int>(key_len*8), ctx->decrypt_key);\n\n    /* Zero things that need zeroing */\n    ctx->cached_Top = ctx->ad_checksum = zero_block();\n    ctx->ad_blocks_processed = 0;\n\n    /* Compute key-dependent values */\n    ocb_aes::encrypt(reinterpret_cast<unsigned char *>(&ctx->cached_Top),\n                            reinterpret_cast<unsigned char *>(&ctx->Lstar), ctx->encrypt_key);\n    tmp_blk = swap_if_le(ctx->Lstar);\n    tmp_blk = double_block(tmp_blk);\n    ctx->Ldollar = swap_if_le(tmp_blk);\n    tmp_blk = double_block(tmp_blk);\n    ctx->L[0] = swap_if_le(tmp_blk);\n    for (i = 1; i < L_TABLE_SZ; i++) {\n\t\ttmp_blk = double_block(tmp_blk);\n    \tctx->L[i] = swap_if_le(tmp_blk);\n    }\n\n    #if (OCB_TAG_LEN == 0)\n    \tctx->tag_len = tag_len;\n    #else\n    \t(void) tag_len;  /* Suppress var not used error */\n    #endif\n\n    return AE_SUCCESS;\n}\n\n/* ----------------------------------------------------------------------- */\n\nstatic block gen_offset_from_nonce(ae_ctx *ctx, const void *nonce)\n{\n\tconst union { unsigned x; unsigned char endian; } little = { 1 };\n\tunion { uint32_t u32[4]; uint8_t u8[16]; block bl; } tmp;\n\tunsigned idx;\n\n\t/* Replace cached nonce Top if needed */\n\ttmp.u32[0] = (little.endian?0x01000000:0x00000001);\n\ttmp.u32[1] = ((uint32_t *)nonce)[0];\n\ttmp.u32[2] = ((uint32_t *)nonce)[1];\n\ttmp.u32[3] = ((uint32_t *)nonce)[2];\n\tidx = (unsigned)(tmp.u8[15] & 0x3f);   /* Get low 6 bits of nonce  */\n\ttmp.u8[15] = tmp.u8[15] & 0xc0;        /* Zero low 6 bits of nonce */\n\tif ( unequal_blocks(tmp.bl,ctx->cached_Top) )   { /* Cached?       */\n\t\tctx->cached_Top = tmp.bl;          /* Update cache, KtopStr    */\n\t\tocb_aes::encrypt(tmp.u8, (unsigned char *)&ctx->KtopStr, ctx->encrypt_key);\n\t\tif (little.endian) {               /* Make Register Correct    */\n\t\t\tctx->KtopStr[0] = bswap64(ctx->KtopStr[0]);\n\t\t\tctx->KtopStr[1] = bswap64(ctx->KtopStr[1]);\n\t\t}\n\t\tctx->KtopStr[2] = ctx->KtopStr[0] ^\n\t\t\t\t\t\t (ctx->KtopStr[0] << 8) ^ (ctx->KtopStr[1] >> 56);\n\t}\n\treturn gen_offset(ctx->KtopStr, idx);\n}\n\nstatic void process_ad(ae_ctx *ctx, const void *ad, int ad_len, int final)\n{\n\tunion { uint32_t u32[4]; uint8_t u8[16]; block bl; } tmp;\n    block ad_offset, ad_checksum;\n    const block *  adp = (block *)ad;\n\tunsigned i,k,tz,remaining;\n\n    ad_offset = ctx->ad_offset;\n    ad_checksum = ctx->ad_checksum;\n    i = ad_len/(BPI*16);\n    if (i) {\n\t\tunsigned ad_block_num = ctx->ad_blocks_processed;\n\t\tdo {\n\t\t\tblock ta[BPI], oa[BPI];\n\t\t\tad_block_num += BPI;\n\t\t\ttz = ntz(ad_block_num);\n\t\t\toa[0] = xor_block(ad_offset, ctx->L[0]);\n\t\t\tta[0] = xor_block(oa[0], adp[0]);\n\t\t\toa[1] = xor_block(oa[0], ctx->L[1]);\n\t\t\tta[1] = xor_block(oa[1], adp[1]);\n\t\t\toa[2] = xor_block(ad_offset, ctx->L[1]);\n\t\t\tta[2] = xor_block(oa[2], adp[2]);\n\t\t\t#if BPI == 4\n\t\t\t\tad_offset = xor_block(oa[2], getL(ctx, tz));\n\t\t\t\tta[3] = xor_block(ad_offset, adp[3]);\n\t\t\t#elif BPI == 8\n\t\t\t\toa[3] = xor_block(oa[2], ctx->L[2]);\n\t\t\t\tta[3] = xor_block(oa[3], adp[3]);\n\t\t\t\toa[4] = xor_block(oa[1], ctx->L[2]);\n\t\t\t\tta[4] = xor_block(oa[4], adp[4]);\n\t\t\t\toa[5] = xor_block(oa[0], ctx->L[2]);\n\t\t\t\tta[5] = xor_block(oa[5], adp[5]);\n\t\t\t\toa[6] = xor_block(ad_offset, ctx->L[2]);\n\t\t\t\tta[6] = xor_block(oa[6], adp[6]);\n\t\t\t\tad_offset = xor_block(oa[6], getL(ctx, tz));\n\t\t\t\tta[7] = xor_block(ad_offset, adp[7]);\n\t\t\t#endif\n\t\t\tocb_aes::ecb_encrypt_blks(ta, BPI, ctx->encrypt_key);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[0]);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[1]);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[2]);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[3]);\n\t\t\t#if (BPI == 8)\n\t\t\tad_checksum = xor_block(ad_checksum, ta[4]);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[5]);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[6]);\n\t\t\tad_checksum = xor_block(ad_checksum, ta[7]);\n\t\t\t#endif\n\t\t\tadp += BPI;\n\t\t} while (--i);\n\t\tctx->ad_blocks_processed = ad_block_num;\n\t\tctx->ad_offset = ad_offset;\n\t\tctx->ad_checksum = ad_checksum;\n\t}\n\n    if (final) {\n\t\tblock ta[BPI];\n\n        /* Process remaining associated data, compute its tag contribution */\n        remaining = ((unsigned)ad_len) % (BPI*16);\n        if (remaining) {\n\t\t\tk=0;\n\t\t\t#if (BPI == 8)\n\t\t\tif (remaining >= 64) {\n\t\t\t\ttmp.bl = xor_block(ad_offset, ctx->L[0]);\n\t\t\t\tta[0] = xor_block(tmp.bl, adp[0]);\n\t\t\t\ttmp.bl = xor_block(tmp.bl, ctx->L[1]);\n\t\t\t\tta[1] = xor_block(tmp.bl, adp[1]);\n\t\t\t\tad_offset = xor_block(ad_offset, ctx->L[1]);\n\t\t\t\tta[2] = xor_block(ad_offset, adp[2]);\n\t\t\t\tad_offset = xor_block(ad_offset, ctx->L[2]);\n\t\t\t\tta[3] = xor_block(ad_offset, adp[3]);\n\t\t\t\tremaining -= 64;\n\t\t\t\tk=4;\n\t\t\t}\n\t\t\t#endif\n\t\t\tif (remaining >= 32) {\n\t\t\t\tad_offset = xor_block(ad_offset, ctx->L[0]);\n\t\t\t\tta[k] = xor_block(ad_offset, adp[k]);\n\t\t\t\tad_offset = xor_block(ad_offset, getL(ctx, ntz(k+2)));\n\t\t\t\tta[k+1] = xor_block(ad_offset, adp[k+1]);\n\t\t\t\tremaining -= 32;\n\t\t\t\tk+=2;\n\t\t\t}\n\t\t\tif (remaining >= 16) {\n\t\t\t\tad_offset = xor_block(ad_offset, ctx->L[0]);\n\t\t\t\tta[k] = xor_block(ad_offset, adp[k]);\n\t\t\t\tremaining = remaining - 16;\n\t\t\t\t++k;\n\t\t\t}\n\t\t\tif (remaining) {\n\t\t\t\tad_offset = xor_block(ad_offset,ctx->Lstar);\n\t\t\t\ttmp.bl = zero_block();\n\t\t\t\tmemcpy(tmp.u8, adp+k, remaining);\n\t\t\t\ttmp.u8[remaining] = (unsigned char)0x80u;\n\t\t\t\tta[k] = xor_block(ad_offset, tmp.bl);\n\t\t\t\t++k;\n\t\t\t}\n\t\t\tocb_aes::ecb_encrypt_blks(ta, k, ctx->encrypt_key);\n\t\t\tswitch (k) {\n\t\t\t\t#if (BPI == 8)\n\t\t\t\tcase 8: ad_checksum = xor_block(ad_checksum, ta[7]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\tcase 7: ad_checksum = xor_block(ad_checksum, ta[6]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\tcase 6: ad_checksum = xor_block(ad_checksum, ta[5]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\tcase 5: ad_checksum = xor_block(ad_checksum, ta[4]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\t#endif\n\t\t\t\tcase 4: ad_checksum = xor_block(ad_checksum, ta[3]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\tcase 3: ad_checksum = xor_block(ad_checksum, ta[2]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\tcase 2: ad_checksum = xor_block(ad_checksum, ta[1]);\n\t\t\t\t\t/* fallthrough */\n\t\t\t\tcase 1: ad_checksum = xor_block(ad_checksum, ta[0]);\n\t\t\t}\n\t\t\tctx->ad_checksum = ad_checksum;\n\t\t}\n\t}\n}\n\n/* ----------------------------------------------------------------------- */\n\nint ae_encrypt(ae_ctx     *  ctx,\n               const void *  nonce,\n               const void *pt,\n               int         pt_len,\n               const void *ad,\n               int         ad_len,\n               void       *ct,\n               void       *tag,\n               int         final)\n{\n\tunion { uint32_t u32[4]; uint8_t u8[16]; block bl; } tmp;\n    block offset, checksum;\n    unsigned i, k;\n    block       * ctp = (block *)ct;\n    const block * ptp = (block *)pt;\n\n    /* Non-null nonce means start of new message, init per-message values */\n    if (nonce) {\n        ctx->offset = gen_offset_from_nonce(ctx, nonce);\n        ctx->ad_offset = ctx->checksum   = zero_block();\n        ctx->ad_blocks_processed = ctx->blocks_processed    = 0;\n        if (ad_len >= 0)\n        \tctx->ad_checksum = zero_block();\n    }\n\n\t/* Process associated data */\n\tif (ad_len > 0)\n\t\tprocess_ad(ctx, ad, ad_len, final);\n\n\t/* Encrypt plaintext data BPI blocks at a time */\n    offset = ctx->offset;\n    checksum  = ctx->checksum;\n    i = pt_len/(BPI*16);\n    if (i) {\n    \tblock oa[BPI];\n    \tunsigned block_num = ctx->blocks_processed;\n    \toa[BPI-1] = offset;\n\t\tdo {\n\t\t\tblock ta[BPI];\n\t\t\tblock_num += BPI;\n\t\t\toa[0] = xor_block(oa[BPI-1], ctx->L[0]);\n\t\t\tta[0] = xor_block(oa[0], ptp[0]);\n\t\t\tchecksum = xor_block(checksum, ptp[0]);\n\t\t\toa[1] = xor_block(oa[0], ctx->L[1]);\n\t\t\tta[1] = xor_block(oa[1], ptp[1]);\n\t\t\tchecksum = xor_block(checksum, ptp[1]);\n\t\t\toa[2] = xor_block(oa[1], ctx->L[0]);\n\t\t\tta[2] = xor_block(oa[2], ptp[2]);\n\t\t\tchecksum = xor_block(checksum, ptp[2]);\n\t\t\t#if BPI == 4\n\t\t\t\toa[3] = xor_block(oa[2], getL(ctx, ntz(block_num)));\n\t\t\t\tta[3] = xor_block(oa[3], ptp[3]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[3]);\n\t\t\t#elif BPI == 8\n\t\t\t\toa[3] = xor_block(oa[2], ctx->L[2]);\n\t\t\t\tta[3] = xor_block(oa[3], ptp[3]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[3]);\n\t\t\t\toa[4] = xor_block(oa[1], ctx->L[2]);\n\t\t\t\tta[4] = xor_block(oa[4], ptp[4]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[4]);\n\t\t\t\toa[5] = xor_block(oa[0], ctx->L[2]);\n\t\t\t\tta[5] = xor_block(oa[5], ptp[5]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[5]);\n\t\t\t\toa[6] = xor_block(oa[7], ctx->L[2]);\n\t\t\t\tta[6] = xor_block(oa[6], ptp[6]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[6]);\n\t\t\t\toa[7] = xor_block(oa[6], getL(ctx, ntz(block_num)));\n\t\t\t\tta[7] = xor_block(oa[7], ptp[7]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[7]);\n\t\t\t#endif\n\t\t\tocb_aes::ecb_encrypt_blks(ta, BPI, ctx->encrypt_key);\n\t\t\tctp[0] = xor_block(ta[0], oa[0]);\n\t\t\tctp[1] = xor_block(ta[1], oa[1]);\n\t\t\tctp[2] = xor_block(ta[2], oa[2]);\n\t\t\tctp[3] = xor_block(ta[3], oa[3]);\n\t\t\t#if (BPI == 8)\n\t\t\tctp[4] = xor_block(ta[4], oa[4]);\n\t\t\tctp[5] = xor_block(ta[5], oa[5]);\n\t\t\tctp[6] = xor_block(ta[6], oa[6]);\n\t\t\tctp[7] = xor_block(ta[7], oa[7]);\n\t\t\t#endif\n\t\t\tptp += BPI;\n\t\t\tctp += BPI;\n\t\t} while (--i);\n    \tctx->offset = offset = oa[BPI-1];\n\t    ctx->blocks_processed = block_num;\n\t\tctx->checksum = checksum;\n    }\n\n    if (final) {\n\t\tblock ta[BPI+1], oa[BPI];\n\n        /* Process remaining plaintext and compute its tag contribution    */\n        unsigned remaining = ((unsigned)pt_len) % (BPI*16);\n        k = 0;                      /* How many blocks in ta[] need ECBing */\n        if (remaining) {\n\t\t\t#if (BPI == 8)\n\t\t\tif (remaining >= 64) {\n\t\t\t\toa[0] = xor_block(offset, ctx->L[0]);\n\t\t\t\tta[0] = xor_block(oa[0], ptp[0]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[0]);\n\t\t\t\toa[1] = xor_block(oa[0], ctx->L[1]);\n\t\t\t\tta[1] = xor_block(oa[1], ptp[1]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[1]);\n\t\t\t\toa[2] = xor_block(oa[1], ctx->L[0]);\n\t\t\t\tta[2] = xor_block(oa[2], ptp[2]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[2]);\n\t\t\t\toffset = oa[3] = xor_block(oa[2], ctx->L[2]);\n\t\t\t\tta[3] = xor_block(offset, ptp[3]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[3]);\n\t\t\t\tremaining -= 64;\n\t\t\t\tk = 4;\n\t\t\t}\n\t\t\t#endif\n\t\t\tif (remaining >= 32) {\n\t\t\t\toa[k] = xor_block(offset, ctx->L[0]);\n\t\t\t\tta[k] = xor_block(oa[k], ptp[k]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[k]);\n\t\t\t\toffset = oa[k+1] = xor_block(oa[k], ctx->L[1]);\n\t\t\t\tta[k+1] = xor_block(offset, ptp[k+1]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[k+1]);\n\t\t\t\tremaining -= 32;\n\t\t\t\tk+=2;\n\t\t\t}\n\t\t\tif (remaining >= 16) {\n\t\t\t\toffset = oa[k] = xor_block(offset, ctx->L[0]);\n\t\t\t\tta[k] = xor_block(offset, ptp[k]);\n\t\t\t\tchecksum = xor_block(checksum, ptp[k]);\n\t\t\t\tremaining -= 16;\n\t\t\t\t++k;\n\t\t\t}\n\t\t\tif (remaining) {\n\t\t\t\ttmp.bl = zero_block();\n\t\t\t\tmemcpy(tmp.u8, ptp+k, remaining);\n\t\t\t\ttmp.u8[remaining] = (unsigned char)0x80u;\n\t\t\t\tchecksum = xor_block(checksum, tmp.bl);\n\t\t\t\tta[k] = offset = xor_block(offset,ctx->Lstar);\n\t\t\t\t++k;\n\t\t\t}\n\t\t}\n        offset = xor_block(offset, ctx->Ldollar);      /* Part of tag gen */\n        ta[k] = xor_block(offset, checksum);           /* Part of tag gen */\n\t\tocb_aes::ecb_encrypt_blks(ta, k + 1, ctx->encrypt_key);\n\t\toffset = xor_block(ta[k], ctx->ad_checksum);   /* Part of tag gen */\n\t\tif (remaining) {\n\t\t\t--k;\n\t\t\ttmp.bl = xor_block(tmp.bl, ta[k]);\n\t\t\tmemcpy(ctp+k, tmp.u8, remaining);\n\t\t}\n\t\tswitch (k) {\n\t\t\t#if (BPI == 8)\n\t\t\tcase 7: ctp[6] = xor_block(ta[6], oa[6]);\n\t\t\t\t/* fallthrough */\n\t\t\tcase 6: ctp[5] = xor_block(ta[5], oa[5]);\n\t\t\t\t/* fallthrough */\n\t\t\tcase 5: ctp[4] = xor_block(ta[4], oa[4]);\n\t\t\t\t/* fallthrough */\n\t\t\tcase 4: ctp[3] = xor_block(ta[3], oa[3]);\n\t\t\t\t/* fallthrough */\n\t\t\t#endif\n\t\t\tcase 3: ctp[2] = xor_block(ta[2], oa[2]);\n\t\t\t\t/* fallthrough */\n\t\t\tcase 2: ctp[1] = xor_block(ta[1], oa[1]);\n\t\t\t\t/* fallthrough */\n\t\t\tcase 1: ctp[0] = xor_block(ta[0], oa[0]);\n\t\t}\n\n        /* Tag is placed at the correct location\n         */\n        if (tag) {\n\t\t\t#if (OCB_TAG_LEN == 16)\n            \t*(block *)tag = offset;\n\t\t\t#elif (OCB_TAG_LEN > 0)\n\t            memcpy((char *)tag, &offset, OCB_TAG_LEN);\n\t\t\t#else\n\t            memcpy((char *)tag, &offset, ctx->tag_len);\n\t        #endif\n        } else {\n\t\t\t#if (OCB_TAG_LEN > 0)\n\t            memcpy((char *)ct + pt_len, &offset, OCB_TAG_LEN);\n            \tpt_len += OCB_TAG_LEN;\n\t\t\t#else\n\t            memcpy((char *)ct + pt_len, &offset, ctx->tag_len);\n            \tpt_len += ctx->tag_len;\n\t        #endif\n        }\n    }\n    return (int) pt_len;\n}\n\n/* ----------------------------------------------------------------------- */\n\n/* Compare two regions of memory, taking a constant amount of time for a\n   given buffer size -- under certain assumptions about the compiler\n   and machine, of course.\n\n   Use this to avoid timing side-channel attacks.\n\n   Returns 0 for memory regions with equal contents; non-zero otherwise. */\nstatic int constant_time_memcmp(const void *av, const void *bv, size_t n) {\n    const uint8_t *a = (const uint8_t *) av;\n    const uint8_t *b = (const uint8_t *) bv;\n    uint8_t result = 0;\n    size_t i;\n\n    for (i=0; i<n; i++) {\n        result |= *a ^ *b;\n        a++;\n        b++;\n    }\n\n    return (int) result;\n}\n\nint ae_decrypt(ae_ctx     *ctx,\n               const void *nonce,\n               const void *ct,\n               int         ct_len,\n               const void *ad,\n               int         ad_len,\n               void       *pt,\n               const void *tag,\n               int         final)\n{\n\tunion { uint32_t u32[4]; uint8_t u8[16]; block bl; } tmp;\n    block offset, checksum;\n    unsigned i, k;\n    block       *ctp = (block *)ct;\n    block       *ptp = (block *)pt;\n\n\t/* Reduce ct_len tag bundled in ct */\n\tif ((final) && (!tag))\n\t\t#if (OCB_TAG_LEN > 0)\n\t\t\tct_len -= OCB_TAG_LEN;\n\t\t#else\n\t\t\tct_len -= ctx->tag_len;\n\t\t#endif\n\n    /* Non-null nonce means start of new message, init per-message values */\n    if (nonce) {\n        ctx->offset = gen_offset_from_nonce(ctx, nonce);\n        ctx->ad_offset = ctx->checksum   = zero_block();\n        ctx->ad_blocks_processed = ctx->blocks_processed    = 0;\n        if (ad_len >= 0)\n        \tctx->ad_checksum = zero_block();\n    }\n\n\t/* Process associated data */\n\tif (ad_len > 0)\n\t\tprocess_ad(ctx, ad, ad_len, final);\n\n\t/* Encrypt plaintext data BPI blocks at a time */\n    offset = ctx->offset;\n    checksum  = ctx->checksum;\n    i = ct_len/(BPI*16);\n    if (i) {\n    \tblock oa[BPI];\n    \tunsigned block_num = ctx->blocks_processed;\n    \toa[BPI-1] = offset;\n\t\tdo {\n\t\t\tblock ta[BPI];\n\t\t\tblock_num += BPI;\n\t\t\toa[0] = xor_block(oa[BPI-1], ctx->L[0]);\n\t\t\tta[0] = xor_block(oa[0], ctp[0]);\n\t\t\toa[1] = xor_block(oa[0], ctx->L[1]);\n\t\t\tta[1] = xor_block(oa[1], ctp[1]);\n\t\t\toa[2] = xor_block(oa[1], ctx->L[0]);\n\t\t\tta[2] = xor_block(oa[2], ctp[2]);\n\t\t\t#if BPI == 4\n\t\t\t\toa[3] = xor_block(oa[2], getL(ctx, ntz(block_num)));\n\t\t\t\tta[3] = xor_block(oa[3], ctp[3]);\n\t\t\t#elif BPI == 8\n\t\t\t\toa[3] = xor_block(oa[2], ctx->L[2]);\n\t\t\t\tta[3] = xor_block(oa[3], ctp[3]);\n\t\t\t\toa[4] = xor_block(oa[1], ctx->L[2]);\n\t\t\t\tta[4] = xor_block(oa[4], ctp[4]);\n\t\t\t\toa[5] = xor_block(oa[0], ctx->L[2]);\n\t\t\t\tta[5] = xor_block(oa[5], ctp[5]);\n\t\t\t\toa[6] = xor_block(oa[7], ctx->L[2]);\n\t\t\t\tta[6] = xor_block(oa[6], ctp[6]);\n\t\t\t\toa[7] = xor_block(oa[6], getL(ctx, ntz(block_num)));\n\t\t\t\tta[7] = xor_block(oa[7], ctp[7]);\n\t\t\t#endif\n\t\t\tocb_aes::ecb_decrypt_blks(ta,BPI,ctx->decrypt_key);\n\t\t\tptp[0] = xor_block(ta[0], oa[0]);\n\t\t\tchecksum = xor_block(checksum, ptp[0]);\n\t\t\tptp[1] = xor_block(ta[1], oa[1]);\n\t\t\tchecksum = xor_block(checksum, ptp[1]);\n\t\t\tptp[2] = xor_block(ta[2], oa[2]);\n\t\t\tchecksum = xor_block(checksum, ptp[2]);\n\t\t\tptp[3] = xor_block(ta[3], oa[3]);\n\t\t\tchecksum = xor_block(checksum, ptp[3]);\n\t\t\t#if (BPI == 8)\n\t\t\tptp[4] = xor_block(ta[4], oa[4]);\n\t\t\tchecksum = xor_block(checksum, ptp[4]);\n\t\t\tptp[5] = xor_block(ta[5], oa[5]);\n\t\t\tchecksum = xor_block(checksum, ptp[5]);\n\t\t\tptp[6] = xor_block(ta[6], oa[6]);\n\t\t\tchecksum = xor_block(checksum, ptp[6]);\n\t\t\tptp[7] = xor_block(ta[7], oa[7]);\n\t\t\tchecksum = xor_block(checksum, ptp[7]);\n\t\t\t#endif\n\t\t\tptp += BPI;\n\t\t\tctp += BPI;\n\t\t} while (--i);\n    \tctx->offset = offset = oa[BPI-1];\n\t    ctx->blocks_processed = block_num;\n\t\tctx->checksum = checksum;\n    }\n\n    if (final) {\n\t\tblock ta[BPI+1], oa[BPI];\n\n        /* Process remaining plaintext and compute its tag contribution    */\n        unsigned remaining = ((unsigned)ct_len) % (BPI*16);\n        k = 0;                      /* How many blocks in ta[] need ECBing */\n        if (remaining) {\n\t\t\t#if (BPI == 8)\n\t\t\tif (remaining >= 64) {\n\t\t\t\toa[0] = xor_block(offset, ctx->L[0]);\n\t\t\t\tta[0] = xor_block(oa[0], ctp[0]);\n\t\t\t\toa[1] = xor_block(oa[0], ctx->L[1]);\n\t\t\t\tta[1] = xor_block(oa[1], ctp[1]);\n\t\t\t\toa[2] = xor_block(oa[1], ctx->L[0]);\n\t\t\t\tta[2] = xor_block(oa[2], ctp[2]);\n\t\t\t\toffset = oa[3] = xor_block(oa[2], ctx->L[2]);\n\t\t\t\tta[3] = xor_block(offset, ctp[3]);\n\t\t\t\tremaining -= 64;\n\t\t\t\tk = 4;\n\t\t\t}\n\t\t\t#endif\n\t\t\tif (remaining >= 32) {\n\t\t\t\toa[k] = xor_block(offset, ctx->L[0]);\n\t\t\t\tta[k] = xor_block(oa[k], ctp[k]);\n\t\t\t\toffset = oa[k+1] = xor_block(oa[k], ctx->L[1]);\n\t\t\t\tta[k+1] = xor_block(offset, ctp[k+1]);\n\t\t\t\tremaining -= 32;\n\t\t\t\tk+=2;\n\t\t\t}\n\t\t\tif (remaining >= 16) {\n\t\t\t\toffset = oa[k] = xor_block(offset, ctx->L[0]);\n\t\t\t\tta[k] = xor_block(offset, ctp[k]);\n\t\t\t\tremaining -= 16;\n\t\t\t\t++k;\n\t\t\t}\n\t\t\tif (remaining) {\n\t\t\t\tblock pad;\n\t\t\t\toffset = xor_block(offset,ctx->Lstar);\n\t\t\t\tocb_aes::encrypt(reinterpret_cast<unsigned char *>(&offset), tmp.u8, ctx->encrypt_key);\n\t\t\t\tpad = tmp.bl;\n\t\t\t\tmemcpy(tmp.u8,ctp+k,remaining);\n\t\t\t\ttmp.bl = xor_block(tmp.bl, pad);\n\t\t\t\ttmp.u8[remaining] = (unsigned char)0x80u;\n\t\t\t\tmemcpy(ptp+k, tmp.u8, remaining);\n\t\t\t\tchecksum = xor_block(checksum, tmp.bl);\n\t\t\t}\n\t\t}\n\t\tocb_aes::ecb_decrypt_blks(ta,k,ctx->decrypt_key);\n\t\tswitch (k) {\n\t\t\t#if (BPI == 8)\n\t\t\tcase 7: ptp[6] = xor_block(ta[6], oa[6]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[6]);\n\t\t\t\t    /* fallthrough */\n\t\t\tcase 6: ptp[5] = xor_block(ta[5], oa[5]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[5]);\n\t\t\t\t    /* fallthrough */\n\t\t\tcase 5: ptp[4] = xor_block(ta[4], oa[4]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[4]);\n\t\t\t\t    /* fallthrough */\n\t\t\tcase 4: ptp[3] = xor_block(ta[3], oa[3]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[3]);\n\t\t\t\t    /* fallthrough */\n\t\t\t#endif\n\t\t\tcase 3: ptp[2] = xor_block(ta[2], oa[2]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[2]);\n\t\t\t\t    /* fallthrough */\n\t\t\tcase 2: ptp[1] = xor_block(ta[1], oa[1]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[1]);\n\t\t\t\t    /* fallthrough */\n\t\t\tcase 1: ptp[0] = xor_block(ta[0], oa[0]);\n\t\t\t\t    checksum = xor_block(checksum, ptp[0]);\n\t\t}\n\n\t\t/* Calculate expected tag */\n        offset = xor_block(offset, ctx->Ldollar);\n        tmp.bl = xor_block(offset, checksum);\n\t\tocb_aes::encrypt(tmp.u8, tmp.u8, ctx->encrypt_key);\n\t\ttmp.bl = xor_block(tmp.bl, ctx->ad_checksum); /* Full tag */\n\n\t\t/* Compare with proposed tag, change ct_len if invalid */\n\t\tif ((OCB_TAG_LEN == 16) && tag) {\n\t\t\tif (unequal_blocks(tmp.bl, *(block *)tag))\n\t\t\t\tct_len = AE_INVALID;\n\t\t} else {\n\t\t\t#if (OCB_TAG_LEN > 0)\n\t\t\t\tint len = OCB_TAG_LEN;\n\t\t\t#else\n\t\t\t\tint len = ctx->tag_len;\n\t\t\t#endif\n\t\t\tif (tag) {\n\t\t\t\tif (constant_time_memcmp(tag,tmp.u8,len) != 0)\n\t\t\t\t\tct_len = AE_INVALID;\n\t\t\t} else {\n\t\t\t\tif (constant_time_memcmp((char *)ct + ct_len,tmp.u8,len) != 0)\n\t\t\t\t\tct_len = AE_INVALID;\n\t\t\t}\n\t\t}\n    }\n    return ct_len;\n }\n\n/* ----------------------------------------------------------------------- */\n/* Simple test program                                                     */\n/* ----------------------------------------------------------------------- */\n\n#if defined(OCB_TEST_PROGRAM)\n\n#include <stdio.h>\n#include <time.h>\n\n#if __GNUC__\n\t#define ALIGN(n) __attribute__ ((aligned(n)))\n#elif _MSC_VER\n\t#define ALIGN(n) __declspec(align(n))\n#else /* Not GNU/Microsoft: delete alignment uses.     */\n\t#define ALIGN(n)\n#endif\n\nstatic void pbuf(void *p, unsigned len, const void *s)\n{\n    unsigned i;\n    if (s)\n        printf(\"%s\", (char *)s);\n    for (i = 0; i < len; i++)\n        printf(\"%02X\", (unsigned)(((unsigned char *)p)[i]));\n    printf(\"\\n\");\n}\n\nstatic void vectors(ae_ctx *ctx, int len)\n{\n    ALIGN(16) uint8_t pt[128];\n    ALIGN(16) uint8_t ct[144];\n    ALIGN(16) uint8_t nonce[] = {0,1,2,3,4,5,6,7,8,9,10,11};\n    int i;\n    for (i=0; i < 128; i++) pt[i] = i;\n    i = ae_encrypt(ctx,nonce,pt,len,pt,len,ct,NULL,AE_FINALIZE);\n    printf(\"P=%d,A=%d: \",len,len); pbuf(ct, i, NULL);\n    i = ae_encrypt(ctx,nonce,pt,0,pt,len,ct,NULL,AE_FINALIZE);\n    printf(\"P=%d,A=%d: \",0,len); pbuf(ct, i, NULL);\n    i = ae_encrypt(ctx,nonce,pt,len,pt,0,ct,NULL,AE_FINALIZE);\n    printf(\"P=%d,A=%d: \",len,0); pbuf(ct, i, NULL);\n}\n\nstatic void validate()\n{\n    ALIGN(16) uint8_t pt[1024];\n    ALIGN(16) uint8_t ct[1024];\n    ALIGN(16) uint8_t tag[16];\n    ALIGN(16) uint8_t nonce[12] = {0,};\n    ALIGN(16) uint8_t key[32] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};\n    ALIGN(16) uint8_t valid[] = {0xB2,0xB4,0x1C,0xBF,0x9B,0x05,0x03,0x7D,\n                                 0xA7,0xF1,0x6C,0x24,0xA3,0x5C,0x1C,0x94};\n    ae_ctx ctx;\n    uint8_t *val_buf, *next;\n    int i, len;\n\n    val_buf = (uint8_t *)malloc(22400 + 16);\n    next = val_buf = (uint8_t *)(((size_t)val_buf + 16) & ~((size_t)15));\n\n    if (0) {\n\t\tae_init(&ctx, key, 16, 12, 16);\n\t\t/* pbuf(&ctx, sizeof(ctx), \"CTX: \"); */\n\t\tvectors(&ctx,0);\n\t\tvectors(&ctx,8);\n\t\tvectors(&ctx,16);\n\t\tvectors(&ctx,24);\n\t\tvectors(&ctx,32);\n\t\tvectors(&ctx,40);\n    }\n\n    memset(key,0,32);\n    memset(pt,0,128);\n    ae_init(&ctx, key, 16, 12, 16);\n\n    /* RFC Vector test */\n    for (i = 0; i < 128; i++) {\n        int first = ((i/3)/(BPI*16))*(BPI*16);\n        int second = first;\n        int third = i - (first + second);\n\n        nonce[11] = i;\n\n        if (0) {\n            ae_encrypt(&ctx,nonce,pt,i,pt,i,ct,NULL,AE_FINALIZE);\n            memcpy(next,ct,(size_t)i+16);\n            next = next+i+16;\n\n            ae_encrypt(&ctx,nonce,pt,i,pt,0,ct,NULL,AE_FINALIZE);\n            memcpy(next,ct,(size_t)i+16);\n            next = next+i+16;\n\n            ae_encrypt(&ctx,nonce,pt,0,pt,i,ct,NULL,AE_FINALIZE);\n            memcpy(next,ct,16);\n            next = next+16;\n        } else {\n            ae_encrypt(&ctx,nonce,pt,first,pt,first,ct,NULL,AE_PENDING);\n            ae_encrypt(&ctx,NULL,pt+first,second,pt+first,second,ct+first,NULL,AE_PENDING);\n            ae_encrypt(&ctx,NULL,pt+first+second,third,pt+first+second,third,ct+first+second,NULL,AE_FINALIZE);\n            memcpy(next,ct,(size_t)i+16);\n            next = next+i+16;\n\n            ae_encrypt(&ctx,nonce,pt,first,pt,0,ct,NULL,AE_PENDING);\n            ae_encrypt(&ctx,NULL,pt+first,second,pt,0,ct+first,NULL,AE_PENDING);\n            ae_encrypt(&ctx,NULL,pt+first+second,third,pt,0,ct+first+second,NULL,AE_FINALIZE);\n            memcpy(next,ct,(size_t)i+16);\n            next = next+i+16;\n\n            ae_encrypt(&ctx,nonce,pt,0,pt,first,ct,NULL,AE_PENDING);\n            ae_encrypt(&ctx,NULL,pt,0,pt+first,second,ct,NULL,AE_PENDING);\n            ae_encrypt(&ctx,NULL,pt,0,pt+first+second,third,ct,NULL,AE_FINALIZE);\n            memcpy(next,ct,16);\n            next = next+16;\n        }\n\n    }\n    nonce[11] = 0;\n    ae_encrypt(&ctx,nonce,NULL,0,val_buf,next-val_buf,ct,tag,AE_FINALIZE);\n    pbuf(tag,16,0);\n    if (memcmp(valid,tag,16) == 0)\n    \tprintf(\"Vectors: PASS\\n\");\n    else\n    \tprintf(\"Vectors: FAIL\\n\");\n\n\n    /* Encrypt/Decrypt test */\n    for (i = 0; i < 128; i++) {\n        int first = ((i/3)/(BPI*16))*(BPI*16);\n        int second = first;\n        int third = i - (first + second);\n\n        nonce[11] = i%128;\n\n        if (1) {\n            len = ae_encrypt(&ctx,nonce,val_buf,i,val_buf,i,ct,tag,AE_FINALIZE);\n            len = ae_encrypt(&ctx,nonce,val_buf,i,val_buf,-1,ct,tag,AE_FINALIZE);\n            len = ae_decrypt(&ctx,nonce,ct,len,val_buf,-1,pt,tag,AE_FINALIZE);\n            if (len == -1) { printf(\"Authentication error: %d\\n\", i); return; }\n            if (len != i) { printf(\"Length error: %d\\n\", i); return; }\n            if (memcmp(val_buf,pt,i)) { printf(\"Decrypt error: %d\\n\", i); return; }\n        } else {\n            len = ae_encrypt(&ctx,nonce,val_buf,i,val_buf,i,ct,NULL,AE_FINALIZE);\n            ae_decrypt(&ctx,nonce,ct,first,val_buf,first,pt,NULL,AE_PENDING);\n            ae_decrypt(&ctx,NULL,ct+first,second,val_buf+first,second,pt+first,NULL,AE_PENDING);\n            len = ae_decrypt(&ctx,NULL,ct+first+second,len-(first+second),val_buf+first+second,third,pt+first+second,NULL,AE_FINALIZE);\n            if (len == -1) { printf(\"Authentication error: %d\\n\", i); return; }\n            if (memcmp(val_buf,pt,i)) { printf(\"Decrypt error: %d\\n\", i); return; }\n        }\n\n    }\n    printf(\"Decrypt: PASS\\n\");\n}\n\nint main()\n{\n    validate();\n    return 0;\n}\n#endif\n\n#if USE_OPENSSL_AES\nchar infoString[] = \"OCB3 (OpenSSL)\";\n#endif\n"
  },
  {
    "path": "src/crypto/ocb_openssl.cc",
    "content": "#include \"src/crypto/ae.h\"\n#include \"src/include/config.h\"\n\n#include <cstring>\n#include <openssl/crypto.h>\n#include <openssl/evp.h>\n\nstruct _ae_ctx\n{\n  EVP_CIPHER_CTX* enc_ctx;\n  EVP_CIPHER_CTX* dec_ctx;\n  int tag_len;\n};\n\nint ae_clear( ae_ctx* ctx )\n{\n  EVP_CIPHER_CTX_free( ctx->enc_ctx );\n  EVP_CIPHER_CTX_free( ctx->dec_ctx );\n  OPENSSL_cleanse( ctx, sizeof( *ctx ) );\n  return AE_SUCCESS;\n}\n\nint ae_ctx_sizeof()\n{\n  return sizeof( _ae_ctx );\n}\n\n// If direction is 1, initializes encryption. If 0, initializes\n// decryption. See the documentation of EVP_CipherInit_ex\nstatic int ae_evp_cipher_init( EVP_CIPHER_CTX** in_ctx,\n                               int direction,\n                               const unsigned char* key,\n                               int nonce_len,\n                               int tag_len )\n{\n  // Create an OpenSSL EVP context. It does not yet have any specific\n  // cipher associated with it.\n  if ( !( *in_ctx = EVP_CIPHER_CTX_new() ) ) {\n    return -3;\n  }\n  EVP_CIPHER_CTX* ctx = *in_ctx;\n  // Although OCB-AES has the same initialization process between\n  // encryption and decryption, an EVP_CIPHER_CTX must be initialized\n  // for a specific direction.\n  if ( EVP_CipherInit_ex( ctx,\n                          EVP_aes_128_ocb(),\n                          /*impl=*/NULL,\n                          /*key=*/key,\n                          /*iv=*/NULL,\n                          direction )\n       != 1 ) {\n    return -3;\n  }\n  // Attempt to set the nonce length. If it fails, the length must not\n  // be supported. However, that should have been handled by the\n  // pre-condition check above.\n  if ( EVP_CIPHER_CTX_ctrl( ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce_len, NULL ) != 1 ) {\n    return -3;\n  }\n  // A NULL tag length means that EVP_CTRL_AEAD_SET_TAG is only being\n  // used to set the length\n  if ( EVP_CIPHER_CTX_ctrl( ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, NULL ) != 1 ) {\n    return -3;\n  }\n  return AE_SUCCESS;\n}\n\nint ae_init( ae_ctx* ctx, const void* key, int key_len, int nonce_len, int tag_len )\n{\n  // Pre-condition: Only nonces of length 12 are supported. The\n  // documentation of `ae_init` in ae.h specifies that `ctx` is\n  // untouched if an invalid configuration is requested. Delegating\n  // this to OpenSSL would happen too late; `ctx` has already been\n  // modified.\n  if ( nonce_len != 12 ) {\n    return AE_NOT_SUPPORTED;\n  }\n  // Pre-condition: Only AES-128 is supported.\n  if ( key_len != 16 ) {\n    return AE_NOT_SUPPORTED;\n  }\n  int r = AE_SUCCESS;\n  if ( ( r = ae_evp_cipher_init(\n           &ctx->enc_ctx, 1, reinterpret_cast<const unsigned char*>( key ), nonce_len, tag_len ) )\n       != AE_SUCCESS ) {\n    return r;\n  }\n  if ( ( r = ae_evp_cipher_init(\n           &ctx->dec_ctx, 0, reinterpret_cast<const unsigned char*>( key ), nonce_len, tag_len ) )\n       != AE_SUCCESS ) {\n    return r;\n  }\n  ctx->tag_len = tag_len;\n  return AE_SUCCESS;\n}\n\nint ae_encrypt( ae_ctx* ctx,\n                const void* nonce_ptr,\n                const void* pt_ptr,\n                int pt_len,\n                const void* ad_ptr,\n                int ad_len,\n                void* ct_ptr,\n                void* tag,\n                int final )\n{\n  const unsigned char* nonce = reinterpret_cast<const unsigned char*>( nonce_ptr );\n  const unsigned char* pt = reinterpret_cast<const unsigned char*>( pt_ptr );\n  const unsigned char* ad = reinterpret_cast<const unsigned char*>( ad_ptr );\n  unsigned char* ct = reinterpret_cast<unsigned char*>( ct_ptr );\n  // Streaming mode is not supported; nonce must always be provided.\n  if ( final != AE_FINALIZE ) {\n    return AE_NOT_SUPPORTED;\n  }\n  if ( nonce == NULL ) {\n    return AE_NOT_SUPPORTED;\n  }\n  if ( EVP_EncryptInit_ex( ctx->enc_ctx,\n                           /*type=*/NULL,\n                           /*impl=*/NULL,\n                           /*key=*/NULL,\n                           nonce )\n       != 1 ) {\n    return -3;\n  }\n  int len = 0;\n  if ( ad != NULL && ad_len > 0 && EVP_EncryptUpdate( ctx->enc_ctx, /*out=*/NULL, &len, ad, ad_len ) != 1 ) {\n    return -3;\n  }\n  len = 0;\n  if ( pt != NULL && pt_len > 0 && EVP_EncryptUpdate( ctx->enc_ctx, ct, &len, pt, pt_len ) != 1 ) {\n    return -3;\n  }\n  int ciphertext_len = len;\n  if ( EVP_EncryptFinal_ex( ctx->enc_ctx, ct + ciphertext_len, &len ) != 1 ) {\n    return -3;\n  }\n  ciphertext_len += len;\n  // If `tag` is provided, the authentication tag goes\n  // there. Otherwise, it is appended after the ciphertext.\n  void* tag_location = tag != NULL ? tag : ct + ciphertext_len;\n  if ( EVP_CIPHER_CTX_ctrl( ctx->enc_ctx, EVP_CTRL_AEAD_GET_TAG, ctx->tag_len, tag_location ) != 1 ) {\n    return -3;\n  }\n  if ( tag == NULL ) {\n    ciphertext_len += ctx->tag_len;\n  }\n  return ciphertext_len;\n}\n\nint ae_decrypt( ae_ctx* ctx,\n                const void* nonce_ptr,\n                const void* ct_ptr,\n                int ct_len,\n                const void* ad_ptr,\n                int ad_len,\n                void* pt_ptr,\n                const void* tag,\n                int final )\n{\n  const unsigned char* nonce = reinterpret_cast<const unsigned char*>( nonce_ptr );\n  const unsigned char* ct = reinterpret_cast<const unsigned char*>( ct_ptr );\n  const unsigned char* ad = reinterpret_cast<const unsigned char*>( ad_ptr );\n  unsigned char* pt = reinterpret_cast<unsigned char*>( pt_ptr );\n  if ( ct_len < ctx->tag_len ) {\n    return AE_INVALID;\n  }\n  // If an external tag is not provided, then the tag is assumed to be\n  // the final bytes of the cipher text. Subtract it off now so the\n  // plaintext does not accidentally try to decrypt the AEAD tag (and\n  // then cause an authentication failure).\n  if ( tag == NULL ) {\n    ct_len -= ctx->tag_len;\n  }\n  // Like encryption, nonce must always be provided and streaming is not supported.\n  if ( final != AE_FINALIZE ) {\n    return AE_NOT_SUPPORTED;\n  }\n  if ( nonce == NULL ) {\n    return AE_NOT_SUPPORTED;\n  }\n  if ( EVP_DecryptInit_ex( ctx->dec_ctx,\n                           /*type=*/NULL,\n                           /*impl=*/NULL,\n                           /*key=*/NULL,\n                           nonce )\n       != 1 ) {\n    return -3;\n  }\n  int len = 0;\n  if ( ad != NULL && ad_len > 0 && EVP_DecryptUpdate( ctx->dec_ctx, /*out=*/NULL, &len, ad, ad_len ) != 1 ) {\n    return -3;\n  }\n  len = 0;\n  if ( ct != NULL && ct_len > 0 && EVP_DecryptUpdate( ctx->dec_ctx, pt, &len, ct, ct_len ) != 1 ) {\n    return -3;\n  }\n  int plaintext_len = len;\n  // If `tag` is provided, the authentication is read from\n  // there. Otherwise, it's the last bytes of the ciphertext. (This is\n  // a convention, not a requirement of OCB mode).\n  const void* tag_location = tag != NULL ? tag : ct + ct_len;\n  if ( EVP_CIPHER_CTX_ctrl( ctx->dec_ctx, EVP_CTRL_AEAD_SET_TAG, ctx->tag_len, (void*)tag_location ) != 1 ) {\n    return -3;\n  }\n  if ( EVP_DecryptFinal_ex( ctx->dec_ctx, pt + plaintext_len, &len ) != 1 ) {\n    return AE_INVALID;\n  }\n  plaintext_len += len;\n  return plaintext_len;\n}\n"
  },
  {
    "path": "src/crypto/prng.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PRNG_HPP\n#define PRNG_HPP\n\n#include \"config.h\"\n\n#if !defined( HAVE_GETENTROPY ) && !defined( HAVE_GETRANDOM )\n#define HAVE_URANDOM 1\n#else\n#undef HAVE_URANDOM\n#endif\n\n#include <unistd.h>\n#ifdef HAVE_SYS_RANDOM_H\n#include <sys/random.h>\n#endif\n\n#include <algorithm>\n#include <cstdint>\n#include <fstream>\n#include <string>\n\n#include \"src/crypto/crypto.h\"\n\n/* Read random bytes from /dev/urandom.\n\n   We rely on stdio buffering for efficiency. */\n\n#ifdef HAVE_URANDOM\nstatic const char rdev[] = \"/dev/urandom\";\n#endif\n\nusing namespace Crypto;\n\nclass PRNG\n{\nprivate:\n#ifdef HAVE_URANDOM\n  std::ifstream randfile;\n#endif\n\n  /* unimplemented to satisfy -Weffc++ */\n  PRNG( const PRNG& );\n  PRNG& operator=( const PRNG& );\n\npublic:\n  PRNG()\n#ifdef HAVE_URANDOM\n    : randfile( rdev, std::ifstream::in | std::ifstream::binary )\n#endif\n  {}\n\n  void fill( void* dest, size_t size )\n  {\n    if ( 0 == size ) {\n      return;\n    }\n\n#if defined( HAVE_GETRANDOM )\n    if ( getrandom( dest, size, 0 ) != static_cast<ssize_t>( size ) ) {\n      throw CryptoException( \"getrandom fell short\" );\n    }\n#elif defined( HAVE_GETENTROPY )\n    // getentropy() can only read up to 256 bytes at a time :(.\n    const size_t max_read = 256;\n    while ( size ) {\n      size_t this_size = std::min( max_read, size );\n      if ( getentropy( dest, this_size ) ) {\n        throw CryptoException( \"getentropy fell short\" );\n      }\n      size -= this_size;\n      dest = static_cast<char*>( dest ) + this_size;\n    }\n#else\n    randfile.read( static_cast<char*>( dest ), size );\n    if ( !randfile ) {\n      throw CryptoException( \"Could not read from \" + std::string( rdev ) );\n    }\n#endif\n  }\n\n  uint8_t uint8()\n  {\n    uint8_t x;\n    fill( &x, 1 );\n    return x;\n  }\n\n  uint32_t uint32()\n  {\n    uint32_t x;\n    fill( &x, 4 );\n    return x;\n  }\n\n  uint64_t uint64()\n  {\n    uint64_t x;\n    fill( &x, 8 );\n    return x;\n  }\n};\n\n#endif\n"
  },
  {
    "path": "src/examples/.gitignore",
    "content": "/decrypt\n/encrypt\n/ntester\n/parse\n/termemu\n/benchmark\n"
  },
  {
    "path": "src/examples/Makefile.am",
    "content": "AM_CXXFLAGS = -I$(top_srcdir)/ $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS)\nAM_LDFLAGS  = $(HARDEN_LDFLAGS)\n\nif BUILD_EXAMPLES\n  noinst_PROGRAMS = encrypt decrypt ntester parse termemu benchmark\nendif\n\nencrypt_SOURCES = encrypt.cc\nencrypt_CPPFLAGS = -I$(srcdir)/../crypto\nencrypt_LDADD = ../crypto/libmoshcrypto.a $(CRYPTO_LIBS)\n\ndecrypt_SOURCES = decrypt.cc\ndecrypt_CPPFLAGS = -I$(srcdir)/../crypto\ndecrypt_LDADD = ../crypto/libmoshcrypto.a $(CRYPTO_LIBS)\n\nparse_SOURCES = parse.cc\nparse_CPPFLAGS = -I$(srcdir)/../terminal -I$(srcdir)/../util\nparse_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a\n\ntermemu_SOURCES = termemu.cc\ntermemu_CPPFLAGS = -I$(srcdir)/../terminal -I$(srcdir)/../util -I$(srcdir)/../statesync -I../protobufs\ntermemu_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a ../statesync/libmoshstatesync.a ../protobufs/libmoshprotos.a $(TINFO_LIBS) $(protobuf_LIBS)\n\nntester_SOURCES = ntester.cc\nntester_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I../protobufs $(protobuf_CFLAGS)\nntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a ../util/libmoshutil.a -lm $(protobuf_LIBS)  $(CRYPTO_LIBS)\n\nbenchmark_SOURCES = benchmark.cc\nbenchmark_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I../protobufs -I$(srcdir)/../frontend -I$(srcdir)/../crypto -I$(srcdir)/../network $(protobuf_CFLAGS)\nbenchmark_LDADD = ../frontend/terminaloverlay.o ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../protobufs/libmoshprotos.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../util/libmoshutil.a $(STDDJB_LDFLAGS) -lm $(TINFO_LIBS) $(protobuf_LIBS) $(CRYPTO_LIBS)\n"
  },
  {
    "path": "src/examples/benchmark.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cerrno>\n#include <climits>\n#include <clocale>\n#include <csignal>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <exception>\n\n#include <pwd.h>\n#include <sys/ioctl.h>\n#include <sys/types.h>\n#include <unistd.h>\n\n#if HAVE_PTY_H\n#include <pty.h>\n#elif HAVE_UTIL_H\n#include <util.h>\n#endif\n\n#include \"src/frontend/terminaloverlay.h\"\n#include \"src/statesync/completeterminal.h\"\n#include \"src/statesync/user.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/locale_utils.h\"\n#include \"src/util/swrite.h\"\n\nconst int ITERATIONS = 100000;\n\nusing namespace Terminal;\n\nint main( int argc, char** argv )\n{\n  try {\n    int fbmod = 0;\n    int width = 80, height = 24;\n    int iterations = ITERATIONS;\n    if ( argc > 1 ) {\n      iterations = atoi( argv[1] );\n      if ( iterations < 1 || iterations > 1000000000 ) {\n        fprintf( stderr, \"bogus iteration count\\n\" );\n        exit( 1 );\n      }\n    }\n    if ( argc > 3 ) {\n      width = atoi( argv[2] );\n      height = atoi( argv[3] );\n      if ( width < 1 || width > 1000 || height < 1 || height > 1000 ) {\n        fprintf( stderr, \"bogus window size\\n\" );\n        exit( 1 );\n      }\n    }\n    Framebuffer local_framebuffers[2] = { Framebuffer( width, height ), Framebuffer( width, height ) };\n    Framebuffer* local_framebuffer = &( local_framebuffers[fbmod] );\n    Framebuffer* new_state = &( local_framebuffers[!fbmod] );\n    Overlay::OverlayManager overlays;\n    Display display( true );\n    Complete local_terminal( width, height );\n\n    /* Adopt native locale */\n    set_native_locale();\n    fatal_assert( is_utf8_locale() );\n\n    for ( int i = 0; i < iterations; i++ ) {\n      /* type a character */\n      overlays.get_prediction_engine().new_user_byte( i + 'x', *local_framebuffer );\n\n      /* fetch target state */\n      *new_state = local_terminal.get_fb();\n\n      /* apply local overlays */\n      overlays.apply( *new_state );\n\n      /* calculate minimal difference from where we are */\n      const std::string diff( display.new_frame( false, *local_framebuffer, *new_state ) );\n\n      /* make sure to use diff */\n      if ( diff.size() > INT_MAX ) {\n        exit( 1 );\n      }\n\n      fbmod = !fbmod;\n      local_framebuffer = &( local_framebuffers[fbmod] );\n      new_state = &( local_framebuffers[!fbmod] );\n    }\n  } catch ( const std::exception& e ) {\n    fprintf( stderr, \"Exception caught: %s\\n\", e.what() );\n    return 1;\n  }\n  return 0;\n}\n"
  },
  {
    "path": "src/examples/decrypt.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n\n#include \"src/crypto/crypto.h\"\n\nusing namespace Crypto;\n\nint main( int argc, char* argv[] )\n{\n  if ( argc != 2 ) {\n    fprintf( stderr, \"Usage: %s KEY\\n\", argv[0] );\n    return 1;\n  }\n\n  try {\n    Base64Key key( argv[1] );\n    Session session( key );\n\n    /* Read input */\n    std::ostringstream input;\n    input << std::cin.rdbuf();\n\n    /* Decrypt message */\n\n    Message message = session.decrypt( input.str() );\n\n    fprintf( stderr, \"Nonce = %ld\\n\", (long)message.nonce.val() );\n    std::cout << message.text;\n  } catch ( const CryptoException& e ) {\n    std::cerr << e.what() << std::endl;\n    exit( 1 );\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "src/examples/encrypt.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n\n#include \"src/crypto/crypto.h\"\n\nusing namespace Crypto;\n\nint main( int argc, char* argv[] )\n{\n  if ( argc != 2 ) {\n    fprintf( stderr, \"Usage: %s NONCE\\n\", argv[0] );\n    return 1;\n  }\n\n  try {\n    Base64Key key;\n    Session session( key );\n    Nonce nonce( myatoi( argv[1] ) );\n\n    /* Read input */\n    std::ostringstream input;\n    input << std::cin.rdbuf();\n\n    /* Encrypt message */\n\n    std::string ciphertext = session.encrypt( Message( nonce, input.str() ) );\n\n    std::cerr << \"Key: \" << key.printable_key() << std::endl;\n\n    std::cout << ciphertext;\n  } catch ( const CryptoException& e ) {\n    std::cerr << e.what() << std::endl;\n    exit( 1 );\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "src/examples/ntester.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <memory>\n\n#include <termios.h>\n#include <unistd.h>\n\n#include \"src/network/networktransport-impl.h\"\n#include \"src/statesync/user.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/pty_compat.h\"\n#include \"src/util/select.h\"\n\nusing namespace Network;\n\nint main( int argc, char* argv[] )\n{\n  bool server = true;\n  char* key;\n  char* ip;\n  char* port;\n\n  UserStream me, remote;\n  using NetworkPointer = std::shared_ptr<Transport<UserStream, UserStream>>;\n  Transport<UserStream, UserStream>* raw_n;\n  try {\n    if ( argc > 1 ) {\n      server = false;\n      /* client */\n\n      key = argv[1];\n      ip = argv[2];\n      port = argv[3];\n\n      raw_n = new Transport<UserStream, UserStream>( me, remote, key, ip, port );\n    } else {\n      raw_n = new Transport<UserStream, UserStream>( me, remote, NULL, NULL );\n    }\n  } catch ( const std::exception& e ) {\n    fprintf( stderr, \"Fatal startup error: %s\\n\", e.what() );\n    exit( 1 );\n  }\n  NetworkPointer n( raw_n );\n  fprintf( stderr, \"Port bound is %s, key is %s\\n\", n->port().c_str(), n->get_key().c_str() );\n\n  if ( server ) {\n    Select& sel = Select::get_instance();\n    uint64_t last_num = n->get_remote_state_num();\n    while ( true ) {\n      try {\n        sel.clear_fds();\n        std::vector<int> fd_list( n->fds() );\n        assert( fd_list.size() == 1 ); /* servers don't hop */\n        int network_fd = fd_list.back();\n        sel.add_fd( network_fd );\n        if ( sel.select( n->wait_time() ) < 0 ) {\n          perror( \"select\" );\n          exit( 1 );\n        }\n\n        n->tick();\n\n        if ( sel.read( network_fd ) ) {\n          n->recv();\n\n          if ( n->get_remote_state_num() != last_num ) {\n            fprintf(\n              stderr, \"[%d=>%d %s]\", (int)last_num, (int)n->get_remote_state_num(), n->get_remote_diff().c_str() );\n            last_num = n->get_remote_state_num();\n          }\n        }\n      } catch ( const std::exception& e ) {\n        fprintf( stderr, \"Server error: %s\\n\", e.what() );\n      }\n    }\n  } else {\n    struct termios saved_termios;\n    struct termios the_termios;\n\n    if ( tcgetattr( STDIN_FILENO, &the_termios ) < 0 ) {\n      perror( \"tcgetattr\" );\n      exit( 1 );\n    }\n\n    saved_termios = the_termios;\n\n    cfmakeraw( &the_termios );\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &the_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n\n    Select& sel = Select::get_instance();\n\n    while ( true ) {\n      sel.clear_fds();\n      sel.add_fd( STDIN_FILENO );\n\n      std::vector<int> fd_list( n->fds() );\n      for ( std::vector<int>::const_iterator it = fd_list.begin(); it != fd_list.end(); it++ ) {\n        sel.add_fd( *it );\n      }\n\n      try {\n        if ( sel.select( n->wait_time() ) < 0 ) {\n          perror( \"select\" );\n        }\n\n        n->tick();\n\n        if ( sel.read( STDIN_FILENO ) ) {\n          char x;\n          fatal_assert( read( STDIN_FILENO, &x, 1 ) == 1 );\n          n->get_current_state().push_back( Parser::UserByte( x ) );\n        }\n\n        bool network_ready_to_read = false;\n        for ( std::vector<int>::const_iterator it = fd_list.begin(); it != fd_list.end(); it++ ) {\n          if ( sel.read( *it ) ) {\n            /* packet received from the network */\n            /* we only read one socket each run */\n            network_ready_to_read = true;\n          }\n        }\n\n        if ( network_ready_to_read ) {\n          n->recv();\n        }\n      } catch ( const std::exception& e ) {\n        fprintf( stderr, \"Client error: %s\\n\", e.what() );\n      }\n    }\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n  }\n}\n"
  },
  {
    "path": "src/examples/parse.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cassert>\n#include <cerrno>\n#include <clocale>\n#include <csignal>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cwchar>\n#include <cwctype>\n#include <typeinfo>\n\n#include <termios.h>\n#include <unistd.h>\n\n#if HAVE_PTY_H\n#include <pty.h>\n#elif HAVE_UTIL_H\n#include <util.h>\n#elif HAVE_LIBUTIL_H\n#include <libutil.h>\n#endif\n\n#include \"src/terminal/parser.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/locale_utils.h\"\n#include \"src/util/pty_compat.h\"\n#include \"src/util/select.h\"\n#include \"src/util/swrite.h\"\n\nconst size_t buf_size = 1024;\n\nstatic void emulate_terminal( int fd );\nstatic int copy( int src, int dest );\nstatic int vt_parser( int fd, Parser::UTF8Parser* parser );\n\nint main( int argc __attribute__( ( unused ) ), char* argv[] __attribute__( ( unused ) ), char* envp[] )\n{\n  int master;\n  struct termios saved_termios, raw_termios, child_termios;\n\n  set_native_locale();\n  fatal_assert( is_utf8_locale() );\n\n  if ( tcgetattr( STDIN_FILENO, &saved_termios ) < 0 ) {\n    perror( \"tcgetattr\" );\n    exit( 1 );\n  }\n\n  child_termios = saved_termios;\n\n#ifdef HAVE_IUTF8\n  if ( !( child_termios.c_iflag & IUTF8 ) ) {\n    fprintf( stderr, \"Warning: Locale is UTF-8 but termios IUTF8 flag not set. Setting IUTF8 flag.\\n\" );\n    child_termios.c_iflag |= IUTF8;\n  }\n#else\n  fprintf( stderr,\n           \"Warning: termios IUTF8 flag not defined. Character-erase of multibyte character sequence probably does \"\n           \"not work properly on this platform.\\n\" );\n#endif /* HAVE_IUTF8 */\n\n  pid_t child = forkpty( &master, NULL, &child_termios, NULL );\n\n  if ( child == -1 ) {\n    perror( \"forkpty\" );\n    exit( 1 );\n  }\n\n  if ( child == 0 ) {\n    /* child */\n    char* my_argv[2];\n    my_argv[0] = strdup( \"/bin/bash\" );\n    assert( my_argv[0] );\n\n    my_argv[1] = NULL;\n\n    if ( execve( \"/bin/bash\", my_argv, envp ) < 0 ) {\n      perror( \"execve\" );\n      exit( 1 );\n    }\n    exit( 0 );\n  } else {\n    /* parent */\n    raw_termios = saved_termios;\n\n    cfmakeraw( &raw_termios );\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &raw_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n\n    emulate_terminal( master );\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n  }\n\n  return 0;\n}\n\nstatic void emulate_terminal( int fd )\n{\n  Parser::UTF8Parser parser;\n\n  Select& sel = Select::get_instance();\n  sel.add_fd( STDIN_FILENO );\n  sel.add_fd( fd );\n\n  while ( 1 ) {\n    int active_fds = sel.select( -1 );\n    if ( active_fds <= 0 ) {\n      perror( \"select\" );\n      return;\n    }\n\n    if ( sel.read( STDIN_FILENO ) ) {\n      if ( copy( STDIN_FILENO, fd ) < 0 ) {\n        return;\n      }\n    } else if ( sel.read( fd ) ) {\n      if ( vt_parser( fd, &parser ) < 0 ) {\n        return;\n      }\n    } else {\n      fprintf( stderr, \"select mysteriously woken up\\n\" );\n    }\n  }\n}\n\nstatic int copy( int src, int dest )\n{\n  char buf[buf_size];\n\n  ssize_t bytes_read = read( src, buf, buf_size );\n  if ( bytes_read == 0 ) { /* EOF */\n    return -1;\n  } else if ( bytes_read < 0 ) {\n    perror( \"read\" );\n    return -1;\n  }\n\n  return swrite( dest, buf, bytes_read );\n}\n\nstatic int vt_parser( int fd, Parser::UTF8Parser* parser )\n{\n  char buf[buf_size];\n\n  /* fill buffer if possible */\n  ssize_t bytes_read = read( fd, buf, buf_size );\n  if ( bytes_read == 0 ) { /* EOF */\n    return -1;\n  } else if ( bytes_read < 0 ) {\n    perror( \"read\" );\n    return -1;\n  }\n\n  /* feed to parser */\n  Parser::Actions actions;\n  for ( int i = 0; i < bytes_read; i++ ) {\n    parser->input( buf[i], actions );\n    for ( Parser::Actions::iterator j = actions.begin(); j != actions.end(); j++ ) {\n\n      assert( *j );\n      Parser::Action& act = **j;\n\n      if ( act.char_present ) {\n        if ( iswprint( act.ch ) ) {\n          printf( \"%s(0x%02x=%lc) \", act.name().c_str(), (unsigned int)act.ch, (wint_t)act.ch );\n        } else {\n          printf( \"%s(0x%02x) \", act.name().c_str(), (unsigned int)act.ch );\n        }\n      } else {\n        printf( \"[%s] \", act.name().c_str() );\n      }\n\n      fflush( stdout );\n    }\n    actions.clear();\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "src/examples/termemu.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cassert>\n#include <cerrno>\n#include <clocale>\n#include <csignal>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cwchar>\n#include <cwctype>\n#include <exception>\n#include <typeinfo>\n\n#include <fcntl.h>\n#include <pwd.h>\n#include <sys/ioctl.h>\n#include <sys/stat.h>\n#include <sys/time.h>\n#include <sys/types.h>\n#include <termios.h>\n#include <unistd.h>\n\n#if HAVE_PTY_H\n#include <pty.h>\n#elif HAVE_UTIL_H\n#include <util.h>\n#elif HAVE_LIBUTIL_H\n#include <libutil.h>\n#endif\n\n#include \"src/statesync/completeterminal.h\"\n#include \"src/terminal/parser.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/locale_utils.h\"\n#include \"src/util/pty_compat.h\"\n#include \"src/util/select.h\"\n#include \"src/util/swrite.h\"\n\nconst size_t buf_size = 16384;\n\nstatic void emulate_terminal( int fd );\n\nint main( int argc, char* argv[] )\n{\n  int master;\n  struct termios saved_termios, raw_termios, child_termios;\n\n  set_native_locale();\n  fatal_assert( is_utf8_locale() );\n\n  if ( tcgetattr( STDIN_FILENO, &saved_termios ) < 0 ) {\n    perror( \"tcgetattr\" );\n    exit( 1 );\n  }\n\n  child_termios = saved_termios;\n\n#ifdef HAVE_IUTF8\n  if ( !( child_termios.c_iflag & IUTF8 ) ) {\n    fprintf( stderr, \"Warning: Locale is UTF-8 but termios IUTF8 flag not set. Setting IUTF8 flag.\\n\" );\n    child_termios.c_iflag |= IUTF8;\n  }\n#else\n  fprintf( stderr,\n           \"Warning: termios IUTF8 flag not defined. Character-erase of multibyte character sequence probably does \"\n           \"not work properly on this platform.\\n\" );\n#endif /* HAVE_IUTF8 */\n\n  pid_t child = forkpty( &master, NULL, &child_termios, NULL );\n\n  if ( child == -1 ) {\n    perror( \"forkpty\" );\n    exit( 1 );\n  }\n\n  if ( child == 0 ) {\n    /* child */\n    if ( setenv( \"TERM\", \"xterm-256color\", true ) < 0 ) {\n      perror( \"setenv\" );\n      exit( 1 );\n    }\n\n    /* ask ncurses to send UTF-8 instead of ISO 2022 for line-drawing chars */\n    if ( setenv( \"NCURSES_NO_UTF8_ACS\", \"1\", true ) < 0 ) {\n      perror( \"setenv\" );\n      exit( 1 );\n    }\n\n    char* my_argv[2];\n\n    if ( argc > 1 ) {\n      argv++;\n    } else {\n      /* get shell name */\n      my_argv[0] = getenv( \"SHELL\" );\n      if ( my_argv[0] == NULL || *my_argv[0] == '\\0' ) {\n        struct passwd* pw = getpwuid( getuid() );\n        if ( pw == NULL ) {\n          perror( \"getpwuid\" );\n          exit( 1 );\n        }\n        my_argv[0] = strdup( pw->pw_shell );\n      }\n      assert( my_argv[0] );\n      my_argv[1] = NULL;\n      argv = my_argv;\n    }\n    if ( execvp( argv[0], argv ) < 0 ) {\n      perror( \"execve\" );\n      exit( 1 );\n    }\n    exit( 0 );\n  } else {\n    /* parent */\n    raw_termios = saved_termios;\n\n    cfmakeraw( &raw_termios );\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &raw_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n\n    try {\n      emulate_terminal( master );\n    } catch ( const std::exception& e ) {\n      fprintf( stderr, \"\\r\\nException caught: %s\\r\\n\", e.what() );\n    }\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n  }\n\n  printf( \"[stm is exiting.]\\n\" );\n\n  return 0;\n}\n\n/* Print a frame if the last frame was more than 1/50 seconds ago */\nstatic bool tick( Terminal::Framebuffer& state, Terminal::Framebuffer& new_frame, const Terminal::Display& display )\n{\n  static bool initialized = false;\n  static struct timeval last_time;\n\n  struct timeval this_time;\n\n  if ( gettimeofday( &this_time, NULL ) < 0 ) {\n    perror( \"gettimeofday\" );\n  }\n\n  double diff = ( this_time.tv_sec - last_time.tv_sec ) + .000001 * ( this_time.tv_usec - last_time.tv_usec );\n\n  if ( ( !initialized ) || ( diff >= 0.02 ) ) {\n    std::string update = display.new_frame( initialized, state, new_frame );\n    swrite( STDOUT_FILENO, update.c_str() );\n    state = new_frame;\n\n    initialized = true;\n    last_time = this_time;\n\n    return true;\n  }\n\n  return false;\n}\n\n/* This is the main loop.\n\n   1. New bytes from the user get applied to the terminal emulator\n      as \"UserByte\" actions.\n\n   2. New bytes from the host get sent to the Parser, and then\n      those actions are applied to the terminal.\n\n   3. Resize events (from a SIGWINCH signal) get turned into\n      \"Resize\" actions and applied to the terminal.\n\n   At every event from select(), we run the tick() function to\n   possibly print a new frame (if we haven't printed one in the\n   last 1/50 second). The new frames are \"differential\" -- they\n   assume the previous frame was sent to the real terminal.\n*/\n\nstatic void emulate_terminal( int fd )\n{\n  /* get current window size */\n  struct winsize window_size;\n  if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {\n    perror( \"ioctl TIOCGWINSZ\" );\n    return;\n  }\n\n  /* tell child process */\n  if ( ioctl( fd, TIOCSWINSZ, &window_size ) < 0 ) {\n    perror( \"ioctl TIOCSWINSZ\" );\n    return;\n  }\n\n  /* open parser and terminal */\n  Terminal::Complete complete( window_size.ws_col, window_size.ws_row );\n  Terminal::Framebuffer state( window_size.ws_col, window_size.ws_row );\n\n  /* open display */\n  Terminal::Display display( true ); /* use TERM to initialize */\n\n  Select& sel = Select::get_instance();\n  sel.add_fd( STDIN_FILENO );\n  sel.add_fd( fd );\n  sel.add_signal( SIGWINCH );\n\n  swrite( STDOUT_FILENO, display.open().c_str() );\n\n  int timeout = -1;\n\n  while ( 1 ) {\n    int active_fds = sel.select( timeout );\n    if ( active_fds < 0 ) {\n      perror( \"select\" );\n      break;\n    }\n\n    if ( sel.read( STDIN_FILENO ) ) {\n      /* input from user */\n      char buf[buf_size];\n\n      /* fill buffer if possible */\n      ssize_t bytes_read = read( STDIN_FILENO, buf, buf_size );\n      if ( bytes_read == 0 ) { /* EOF */\n        return;\n      } else if ( bytes_read < 0 ) {\n        perror( \"read\" );\n        return;\n      }\n\n      std::string terminal_to_host;\n\n      for ( int i = 0; i < bytes_read; i++ ) {\n        terminal_to_host += complete.act( Parser::UserByte( buf[i] ) );\n      }\n\n      if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {\n        break;\n      }\n    } else if ( sel.read( fd ) ) {\n      /* input from host */\n      char buf[buf_size];\n\n      /* fill buffer if possible */\n      ssize_t bytes_read = read( fd, buf, buf_size );\n      if ( bytes_read == 0 ) { /* EOF */\n        return;\n      } else if ( bytes_read < 0 ) {\n        perror( \"read\" );\n        return;\n      }\n\n      std::string terminal_to_host = complete.act( std::string( buf, bytes_read ) );\n      if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {\n        break;\n      }\n    } else if ( sel.signal( SIGWINCH ) ) {\n      /* get new size */\n      if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {\n        perror( \"ioctl TIOCGWINSZ\" );\n        return;\n      }\n\n      /* tell emulator */\n      complete.act( Parser::Resize( window_size.ws_col, window_size.ws_row ) );\n\n      /* tell child process */\n      if ( ioctl( fd, TIOCSWINSZ, &window_size ) < 0 ) {\n        perror( \"ioctl TIOCSWINSZ\" );\n        return;\n      }\n    }\n\n    Terminal::Framebuffer new_frame( complete.get_fb() );\n\n    if ( tick( state, new_frame, display ) ) { /* there was a frame */\n      timeout = -1;\n    } else {\n      timeout = 20;\n    }\n  }\n\n  std::string update = display.new_frame( true, state, complete.get_fb() );\n  swrite( STDOUT_FILENO, update.c_str() );\n\n  swrite( STDOUT_FILENO, display.close().c_str() );\n}\n"
  },
  {
    "path": "src/frontend/.gitignore",
    "content": "/mosh-client\n/mosh-server\n"
  },
  {
    "path": "src/frontend/Makefile.am",
    "content": "AM_CPPFLAGS = -I$(top_srcdir)/ $(TINFO_CFLAGS) $(protobuf_CFLAGS) $(CRYPTO_CFLAGS)\nAM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\nAM_LDFLAGS  = $(HARDEN_LDFLAGS) $(CODE_COVERAGE_LIBS)\nLDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lm $(TINFO_LIBS) $(protobuf_LIBS) $(CRYPTO_LIBS)\n\nmosh_server_LDADD = $(LDADD)\n\nbin_PROGRAMS =\n\nif BUILD_CLIENT\n  bin_PROGRAMS += mosh-client\nendif\n\nif BUILD_SERVER\n  bin_PROGRAMS += mosh-server\nendif\n\nmosh_client_SOURCES = mosh-client.cc stmclient.cc stmclient.h terminaloverlay.cc terminaloverlay.h\nmosh_server_SOURCES = mosh-server.cc\n"
  },
  {
    "path": "src/frontend/mosh-client.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n#include \"src/include/version.h\"\n\n#include <cstdlib>\n\n#include <unistd.h>\n\n#include \"src/crypto/crypto.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/locale_utils.h\"\n#include \"stmclient.h\"\n\n/* These need to be included last because of conflicting defines. */\n/*\n * stmclient.h includes termios.h, and that will break termio/termios pull in on Solaris.\n * The solution is to include termio.h also.\n * But Mac OS X doesn't have termio.h, so this needs a guard.\n */\n#ifdef HAVE_TERMIO_H\n#include <termio.h>\n#endif\n\n#if defined HAVE_NCURSESW_CURSES_H\n#include <ncursesw/curses.h>\n#include <ncursesw/term.h>\n#elif defined HAVE_NCURSESW_H\n#include <ncursesw.h>\n#include <term.h>\n#elif defined HAVE_NCURSES_CURSES_H\n#include <ncurses/curses.h>\n#include <ncurses/term.h>\n#elif defined HAVE_NCURSES_H\n#include <ncurses.h>\n#include <term.h>\n#elif defined HAVE_CURSES_H\n#include <curses.h>\n#include <term.h>\n#else\n#error \"SysV or X/Open-compatible Curses header file required\"\n#endif\n\nstatic void print_version( FILE* file )\n{\n  fputs( \"mosh-client (\" PACKAGE_STRING \") [build \" BUILD_VERSION \"]\\n\"\n         \"Copyright 2012 Keith Winstein <mosh-devel@mit.edu>\\n\"\n         \"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\\n\"\n         \"This is free software: you are free to change and redistribute it.\\n\"\n         \"There is NO WARRANTY, to the extent permitted by law.\\n\",\n         file );\n}\n\nstatic void print_usage( FILE* file, const char* argv0 )\n{\n  print_version( file );\n  fprintf( file,\n           \"\\nUsage: %s [-# 'ARGS'] IP PORT\\n\"\n           \"       %s -c\\n\",\n           argv0,\n           argv0 );\n}\n\nstatic void print_colorcount( void )\n{\n  /* check colors */\n  setupterm( (char*)0, 1, (int*)0 );\n\n  char colors_name[] = \"colors\";\n  int color_val = tigetnum( colors_name );\n  if ( color_val == -2 ) {\n    fprintf( stderr, \"Invalid terminfo numeric capability: %s\\n\", colors_name );\n  }\n\n  printf( \"%d\\n\", color_val );\n}\n\nint main( int argc, char* argv[] )\n{\n  unsigned int verbose = 0;\n  /* For security, make sure we don't dump core */\n  Crypto::disable_dumping_core();\n\n  /* Detect edge case */\n  fatal_assert( argc > 0 );\n\n  /* Get arguments */\n  for ( int i = 1; i < argc; i++ ) {\n    if ( 0 == strcmp( argv[i], \"--help\" ) ) {\n      print_usage( stdout, argv[0] );\n      exit( 0 );\n    }\n    if ( 0 == strcmp( argv[i], \"--version\" ) ) {\n      print_version( stdout );\n      exit( 0 );\n    }\n  }\n\n  int opt;\n  while ( ( opt = getopt( argc, argv, \"#:cv\" ) ) != -1 ) {\n    switch ( opt ) {\n      case '#':\n        // Ignore the original arguments to mosh wrapper\n        break;\n      case 'c':\n        print_colorcount();\n        exit( 0 );\n        break;\n      case 'v':\n        verbose++;\n        break;\n      default:\n        print_usage( stderr, argv[0] );\n        exit( 1 );\n        break;\n    }\n  }\n\n  char *ip, *desired_port;\n\n  if ( argc - optind != 2 ) {\n    print_usage( stderr, argv[0] );\n    exit( 1 );\n  }\n\n  ip = argv[optind];\n  desired_port = argv[optind + 1];\n\n  /* Sanity-check arguments */\n  if ( desired_port && ( strspn( desired_port, \"0123456789\" ) != strlen( desired_port ) ) ) {\n    fprintf( stderr, \"%s: Bad UDP port (%s)\\n\\n\", argv[0], desired_port );\n    print_usage( stderr, argv[0] );\n    exit( 1 );\n  }\n\n  /* Read key from environment */\n  char* env_key = getenv( \"MOSH_KEY\" );\n  if ( env_key == NULL ) {\n    fputs( \"MOSH_KEY environment variable not found.\\n\", stderr );\n    exit( 1 );\n  }\n\n  /* Read prediction preference */\n  char* predict_mode = getenv( \"MOSH_PREDICTION_DISPLAY\" );\n  /* can be NULL */\n\n  /* Read prediction insertion preference */\n  char* predict_overwrite = getenv( \"MOSH_PREDICTION_OVERWRITE\" );\n  /* can be NULL */\n\n  std::string key( env_key );\n\n  if ( unsetenv( \"MOSH_KEY\" ) < 0 ) {\n    perror( \"unsetenv\" );\n    exit( 1 );\n  }\n\n  /* Adopt native locale */\n  set_native_locale();\n\n  bool success = false;\n  try {\n    STMClient client( ip, desired_port, key.c_str(), predict_mode, verbose, predict_overwrite );\n    client.init();\n\n    try {\n      success = client.main();\n    } catch ( ... ) {\n      client.shutdown();\n      throw;\n    }\n\n    client.shutdown();\n  } catch ( const Network::NetworkException& e ) {\n    fprintf( stderr, \"Network exception: %s\\r\\n\", e.what() );\n    success = false;\n  } catch ( const Crypto::CryptoException& e ) {\n    fprintf( stderr, \"Crypto exception: %s\\r\\n\", e.what() );\n    success = false;\n  } catch ( const std::exception& e ) {\n    fprintf( stderr, \"Error: %s\\r\\n\", e.what() );\n    success = false;\n  }\n\n  printf( \"[mosh is exiting.]\\n\" );\n\n  return !success;\n}\n"
  },
  {
    "path": "src/frontend/mosh-server.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n#include \"src/include/version.h\"\n\n#include <cerrno>\n#include <clocale>\n#include <csignal>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <sstream>\n#include <typeinfo>\n\n#include <err.h>\n#include <fcntl.h>\n#include <inttypes.h>\n#include <netdb.h>\n#include <pwd.h>\n#include <strings.h>\n#include <sys/ioctl.h>\n#include <sys/socket.h>\n#include <sys/stat.h>\n#include <sys/types.h>\n#include <termios.h>\n#include <unistd.h>\n#ifdef HAVE_UTEMPTER\n#include <utempter.h>\n#endif\n#ifdef HAVE_SYSLOG\n#include <syslog.h>\n#endif\n\n#ifdef HAVE_UTMPX_H\n#include <utmpx.h>\n#endif\n\n#ifdef HAVE_PATHS_H\n#include <paths.h>\n#endif\n\n#if HAVE_PTY_H\n#include <pty.h>\n#elif HAVE_UTIL_H\n#include <util.h>\n#endif\n\n#if FORKPTY_IN_LIBUTIL\n#include <libutil.h>\n#endif\n\n#include \"src/statesync/completeterminal.h\"\n#include \"src/statesync/user.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/locale_utils.h\"\n#include \"src/util/pty_compat.h\"\n#include \"src/util/select.h\"\n#include \"src/util/swrite.h\"\n#include \"src/util/timestamp.h\"\n\n#ifndef _PATH_BSHELL\n#define _PATH_BSHELL \"/bin/sh\"\n#endif\n\n#include \"src/network/networktransport-impl.h\"\n\nusing ServerConnection = Network::Transport<Terminal::Complete, Network::UserStream>;\n\nstatic void serve( int host_fd,\n                   int pipe_fd,\n                   Terminal::Complete& terminal,\n                   ServerConnection& network,\n                   long network_timeout,\n                   long network_signaled_timeout );\n\nstatic int run_server( const char* desired_ip,\n                       const char* desired_port,\n                       const std::string& command_path,\n                       char* command_argv[],\n                       const int colors,\n                       unsigned int verbose,\n                       bool with_motd );\n\nstatic void print_version( FILE* file )\n{\n  fputs( \"mosh-server (\" PACKAGE_STRING \") [build \" BUILD_VERSION \"]\\n\"\n         \"Copyright 2012 Keith Winstein <mosh-devel@mit.edu>\\n\"\n         \"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\\n\"\n         \"This is free software: you are free to change and redistribute it.\\n\"\n         \"There is NO WARRANTY, to the extent permitted by law.\\n\",\n         file );\n}\n\nstatic void print_usage( FILE* stream, const char* argv0 )\n{\n  fprintf( stream,\n           \"Usage: %s new [-s] [-v] [-i LOCALADDR] [-p PORT[:PORT2]] [-c COLORS] [-l NAME=VALUE] [-- COMMAND...]\\n\",\n           argv0 );\n}\n\nstatic bool print_motd( const char* filename );\nstatic void chdir_homedir( void );\nstatic bool motd_hushed( void );\nstatic void warn_unattached( const std::string& ignore_entry );\n\n/* Simple spinloop */\nstatic void spin( void )\n{\n  static unsigned int spincount = 0;\n  spincount++;\n\n  if ( spincount > 10 ) {\n    struct timespec req;\n    req.tv_sec = 0;\n    req.tv_nsec = 100000000; /* 0.1 sec */\n    nanosleep( &req, NULL );\n    freeze_timestamp();\n  }\n}\n\nstatic std::string get_SSH_IP( void )\n{\n  const char* SSH_CONNECTION = getenv( \"SSH_CONNECTION\" );\n  if ( !SSH_CONNECTION ) { /* Older sshds don't set this */\n    fputs( \"Warning: SSH_CONNECTION not found; binding to any interface.\\n\", stderr );\n    return std::string( \"\" );\n  }\n  std::istringstream ss( SSH_CONNECTION );\n  std::string dummy, local_interface_IP;\n  ss >> dummy >> dummy >> local_interface_IP;\n  if ( !ss ) {\n    fputs( \"Warning: Could not parse SSH_CONNECTION; binding to any interface.\\n\", stderr );\n    return std::string( \"\" );\n  }\n\n  /* Strip IPv6 prefix. */\n  const char IPv6_prefix[] = \"::ffff:\";\n\n  if ( ( local_interface_IP.length() > strlen( IPv6_prefix ) )\n       && ( 0 == strncasecmp( local_interface_IP.c_str(), IPv6_prefix, strlen( IPv6_prefix ) ) ) ) {\n    return local_interface_IP.substr( strlen( IPv6_prefix ) );\n  }\n\n  return local_interface_IP;\n}\n\nint main( int argc, char* argv[] )\n{\n  /* For security, make sure we don't dump core */\n  Crypto::disable_dumping_core();\n\n  /* Detect edge case */\n  fatal_assert( argc > 0 );\n\n  const char* desired_ip = NULL;\n  std::string desired_ip_str;\n  const char* desired_port = NULL;\n  std::string command_path;\n  char** command_argv = NULL;\n  int colors = 0;\n  unsigned int verbose = 0; /* don't close stdin/stdout/stderr */\n  /* Will cause mosh-server not to correctly detach on old versions of sshd. */\n  std::list<std::string> locale_vars;\n\n  /* strip off command */\n  for ( int i = 1; i < argc; i++ ) {\n    if ( 0 == strcmp( argv[i], \"--help\" ) || 0 == strcmp( argv[i], \"-h\" ) ) {\n      print_usage( stdout, argv[0] );\n      exit( 0 );\n    }\n    if ( 0 == strcmp( argv[i], \"--version\" ) ) {\n      print_version( stdout );\n      exit( 0 );\n    }\n    if ( 0 == strcmp( argv[i], \"--\" ) ) { /* -- is mandatory */\n      if ( i != argc - 1 ) {\n        command_argv = argv + i + 1;\n      }\n      argc = i; /* rest of options before -- */\n      break;\n    }\n  }\n\n  /* Parse new command-line syntax */\n  if ( ( argc >= 2 ) && ( strcmp( argv[1], \"new\" ) == 0 ) ) {\n    /* new option syntax */\n    int opt;\n    while ( ( opt = getopt( argc - 1, argv + 1, \"@:i:p:c:svl:\" ) ) != -1 ) {\n      switch ( opt ) {\n          /*\n           * This undocumented option does nothing but eat its argument.\n           * Useful in scripting where you prepend something to a\n           * mosh-server argv, and might end up with something like\n           * \"mosh-server new -v new -c 256\", now you can say\n           * \"mosh-server new -v -@ new -c 256\" to discard the second\n           * \"new\".\n           */\n        case '@':\n          break;\n        case 'i':\n          desired_ip = optarg;\n          break;\n        case 'p':\n          desired_port = optarg;\n          break;\n        case 's':\n          desired_ip = NULL;\n          desired_ip_str = get_SSH_IP();\n          if ( !desired_ip_str.empty() ) {\n            desired_ip = desired_ip_str.c_str();\n            fatal_assert( desired_ip );\n          }\n          break;\n        case 'c':\n          try {\n            colors = myatoi( optarg );\n          } catch ( const CryptoException& ) {\n            fprintf( stderr, \"%s: Bad number of colors (%s)\\n\", argv[0], optarg );\n            print_usage( stderr, argv[0] );\n            exit( 1 );\n          }\n          break;\n        case 'v':\n          verbose++;\n          break;\n        case 'l':\n          locale_vars.push_back( std::string( optarg ) );\n          break;\n        default:\n          /* don't die on unknown options */\n          print_usage( stderr, argv[0] );\n          break;\n      }\n    }\n  } else if ( argc == 1 ) {\n    /* legacy argument parsing for older client wrapper script */\n    /* do nothing */\n  } else if ( argc == 2 ) {\n    desired_ip = argv[1];\n  } else if ( argc == 3 ) {\n    desired_ip = argv[1];\n    desired_port = argv[2];\n  } else {\n    print_usage( stderr, argv[0] );\n    exit( 1 );\n  }\n\n  /* Sanity-check arguments */\n  int dpl, dph;\n  if ( desired_port && !Connection::parse_portrange( desired_port, dpl, dph ) ) {\n    fprintf( stderr, \"%s: Bad UDP port range (%s)\\n\", argv[0], desired_port );\n    print_usage( stderr, argv[0] );\n    exit( 1 );\n  }\n\n  bool with_motd = false;\n\n#ifdef HAVE_SYSLOG\n  openlog( argv[0], LOG_PID | LOG_NDELAY, LOG_AUTH );\n#endif\n\n  /* Get shell */\n  char* my_argv[2];\n  std::string shell_name;\n  if ( !command_argv ) {\n    /* get shell name */\n    const char* shell = getenv( \"SHELL\" );\n    if ( shell == NULL ) {\n      struct passwd* pw = getpwuid( getuid() );\n      if ( pw == NULL ) {\n        perror( \"getpwuid\" );\n        exit( 1 );\n      }\n      shell = pw->pw_shell;\n    }\n\n    std::string shell_path( shell );\n    if ( shell_path.empty() ) { /* empty shell means Bourne shell */\n      shell_path = _PATH_BSHELL;\n    }\n\n    command_path = shell_path;\n\n    size_t shell_slash( shell_path.rfind( '/' ) );\n    if ( shell_slash == std::string::npos ) {\n      shell_name = shell_path;\n    } else {\n      shell_name = shell_path.substr( shell_slash + 1 );\n    }\n\n    /* prepend '-' to make login shell */\n    shell_name = '-' + shell_name;\n\n    my_argv[0] = const_cast<char*>( shell_name.c_str() );\n    my_argv[1] = NULL;\n    command_argv = my_argv;\n\n    with_motd = true;\n  }\n\n  if ( command_path.empty() ) {\n    command_path = command_argv[0];\n  }\n\n  /* Adopt implementation locale */\n  set_native_locale();\n  if ( !is_utf8_locale() ) {\n    /* save details for diagnostic */\n    LocaleVar native_ctype = get_ctype();\n    std::string native_charset( locale_charset() );\n\n    /* apply locale-related environment variables from client */\n    clear_locale_variables();\n    for ( std::list<std::string>::const_iterator i = locale_vars.begin(); i != locale_vars.end(); i++ ) {\n      char* env_string = strdup( i->c_str() );\n      fatal_assert( env_string );\n      if ( 0 != putenv( env_string ) ) {\n        perror( \"putenv\" );\n      }\n    }\n\n    /* check again */\n    set_native_locale();\n    if ( !is_utf8_locale() ) {\n      LocaleVar client_ctype = get_ctype();\n      std::string client_charset( locale_charset() );\n\n      fprintf( stderr,\n               \"mosh-server needs a UTF-8 native locale to run.\\n\\n\"\n               \"Unfortunately, the local environment (%s) specifies\\n\"\n               \"the character set \\\"%s\\\",\\n\\n\"\n               \"The client-supplied environment (%s) specifies\\n\"\n               \"the character set \\\"%s\\\".\\n\\n\",\n               native_ctype.str().c_str(),\n               native_charset.c_str(),\n               client_ctype.str().c_str(),\n               client_charset.c_str() );\n      int unused __attribute( ( unused ) ) = system( \"locale\" );\n      exit( 1 );\n    }\n  }\n\n  try {\n    return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd );\n  } catch ( const Network::NetworkException& e ) {\n    fprintf( stderr, \"Network exception: %s\\n\", e.what() );\n    return 1;\n  } catch ( const Crypto::CryptoException& e ) {\n    fprintf( stderr, \"Crypto exception: %s\\n\", e.what() );\n    return 1;\n  }\n}\n\nstatic int run_server( const char* desired_ip,\n                       const char* desired_port,\n                       const std::string& command_path,\n                       char* command_argv[],\n                       const int colors,\n                       unsigned int verbose,\n                       bool with_motd )\n{\n  /* get network idle timeout */\n  long network_timeout = 0;\n  char* timeout_envar = getenv( \"MOSH_SERVER_NETWORK_TMOUT\" );\n  if ( timeout_envar && *timeout_envar ) {\n    errno = 0;\n    char* endptr;\n    network_timeout = strtol( timeout_envar, &endptr, 10 );\n    if ( *endptr != '\\0' || ( network_timeout == 0 && errno == EINVAL ) ) {\n      fputs( \"MOSH_SERVER_NETWORK_TMOUT not a valid integer, ignoring\\n\", stderr );\n    } else if ( network_timeout < 0 ) {\n      fputs( \"MOSH_SERVER_NETWORK_TMOUT is negative, ignoring\\n\", stderr );\n      network_timeout = 0;\n    }\n  }\n  /* get network signaled idle timeout */\n  long network_signaled_timeout = 0;\n  char* signal_envar = getenv( \"MOSH_SERVER_SIGNAL_TMOUT\" );\n  if ( signal_envar && *signal_envar ) {\n    errno = 0;\n    char* endptr;\n    network_signaled_timeout = strtol( signal_envar, &endptr, 10 );\n    if ( *endptr != '\\0' || ( network_signaled_timeout == 0 && errno == EINVAL ) ) {\n      fputs( \"MOSH_SERVER_SIGNAL_TMOUT not a valid integer, ignoring\\n\", stderr );\n    } else if ( network_signaled_timeout < 0 ) {\n      fputs( \"MOSH_SERVER_SIGNAL_TMOUT is negative, ignoring\\n\", stderr );\n      network_signaled_timeout = 0;\n    }\n  }\n  /* get initial window size */\n  struct winsize window_size;\n  if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 || window_size.ws_col == 0 || window_size.ws_row == 0 ) {\n    /* Fill in sensible defaults. */\n    /* They will be overwritten by client on first connection. */\n    memset( &window_size, 0, sizeof( window_size ) );\n    window_size.ws_col = 80;\n    window_size.ws_row = 24;\n  }\n\n  /* open parser and terminal */\n  Terminal::Complete terminal( window_size.ws_col, window_size.ws_row );\n\n  /* open network */\n  Network::UserStream blank;\n  using NetworkPointer = std::shared_ptr<ServerConnection>;\n  NetworkPointer network( new ServerConnection( terminal, blank, desired_ip, desired_port ) );\n\n  network->set_verbose( verbose );\n  Select::set_verbose( verbose );\n\n  /*\n   * If mosh-server is run on a pty, then typeahead may echo and break mosh.pl's\n   * detection of the MOSH CONNECT message.  Print it on a new line to bodge\n   * around that.\n   */\n  if ( isatty( STDIN_FILENO ) ) {\n    puts( \"\\r\\n\" );\n  }\n  printf( \"MOSH CONNECT %s %s\\n\", network->port().c_str(), network->get_key().c_str() );\n\n  /* don't let signals kill us */\n  struct sigaction sa;\n  sa.sa_handler = SIG_IGN;\n  sa.sa_flags = 0;\n  fatal_assert( 0 == sigfillset( &sa.sa_mask ) );\n  fatal_assert( 0 == sigaction( SIGHUP, &sa, NULL ) );\n  fatal_assert( 0 == sigaction( SIGPIPE, &sa, NULL ) );\n\n  /* detach from terminal */\n  fflush( NULL );\n  pid_t the_pid = fork();\n  if ( the_pid < 0 ) {\n    perror( \"fork\" );\n  } else if ( the_pid > 0 ) {\n    fputs( \"\\nmosh-server (\" PACKAGE_STRING \") [build \" BUILD_VERSION \"]\\n\"\n           \"Copyright 2012 Keith Winstein <mosh-devel@mit.edu>\\n\"\n           \"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\\n\"\n           \"This is free software: you are free to change and redistribute it.\\n\"\n           \"There is NO WARRANTY, to the extent permitted by law.\\n\\n\",\n           stderr );\n\n    fprintf( stderr, \"[mosh-server detached, pid = %d]\\n\", static_cast<int>( the_pid ) );\n#ifndef HAVE_IUTF8\n    fputs( \"\\nWarning: termios IUTF8 flag not defined.\\n\"\n           \"Character-erase of multibyte character sequence\\n\"\n           \"probably does not work properly on this platform.\\n\",\n           stderr );\n#endif /* HAVE_IUTF8 */\n\n    fflush( NULL );\n    if ( isatty( STDOUT_FILENO ) ) {\n      tcdrain( STDOUT_FILENO );\n    }\n    if ( isatty( STDERR_FILENO ) ) {\n      tcdrain( STDERR_FILENO );\n    }\n    exit( 0 );\n  }\n\n  int master;\n\n  /* close file descriptors */\n  if ( verbose == 0 ) {\n    /* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */\n    int nullfd;\n\n    nullfd = open( \"/dev/null\", O_RDWR );\n    if ( nullfd == -1 ) {\n      perror( \"open\" );\n      exit( 1 );\n    }\n\n    if ( dup2( nullfd, STDIN_FILENO ) < 0 || dup2( nullfd, STDOUT_FILENO ) < 0\n         || dup2( nullfd, STDERR_FILENO ) < 0 ) {\n      perror( \"dup2\" );\n      exit( 1 );\n    }\n\n    if ( close( nullfd ) < 0 ) {\n      perror( \"close\" );\n      exit( 1 );\n    }\n  }\n\n  char utmp_entry[64] = { 0 };\n  snprintf( utmp_entry, 64, \"mosh [%ld]\", static_cast<long int>( getpid() ) );\n\n  /* Fork child process */\n  int pipes[2];\n  int success = pipe( pipes );\n  if ( success == -1 ) {\n    perror( \"pipe\" );\n    exit( 1 );\n  }\n  pid_t child = forkpty( &master, NULL, NULL, &window_size );\n\n  if ( child == -1 ) {\n    perror( \"forkpty\" );\n    exit( 1 );\n  }\n\n  if ( child == 0 ) {\n    /* child */\n    if ( close( pipes[1] ) < 0 ) {\n      perror( \"child write pipe close\" );\n      exit( 1 );\n    }\n\n    /* reenable signals */\n    struct sigaction sa;\n    sa.sa_handler = SIG_DFL;\n    sa.sa_flags = 0;\n    fatal_assert( 0 == sigfillset( &sa.sa_mask ) );\n    fatal_assert( 0 == sigaction( SIGHUP, &sa, NULL ) );\n    fatal_assert( 0 == sigaction( SIGPIPE, &sa, NULL ) );\n\n#ifdef HAVE_SYSLOG\n    closelog();\n#endif\n\n    /* close server-related file descriptors */\n    network.reset();\n\n    /* set IUTF8 if available */\n#ifdef HAVE_IUTF8\n    struct termios child_termios;\n    if ( tcgetattr( STDIN_FILENO, &child_termios ) < 0 ) {\n      perror( \"tcgetattr\" );\n      exit( 1 );\n    }\n\n    child_termios.c_iflag |= IUTF8;\n\n    if ( tcsetattr( STDIN_FILENO, TCSANOW, &child_termios ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n#endif /* HAVE_IUTF8 */\n\n    /* set TERM */\n    const char default_term[] = \"xterm\";\n    const char color_term[] = \"xterm-256color\";\n\n    if ( setenv( \"TERM\", ( colors == 256 ) ? color_term : default_term, true ) < 0 ) {\n      perror( \"setenv\" );\n      exit( 1 );\n    }\n\n    /* ask ncurses to send UTF-8 instead of ISO 2022 for line-drawing chars */\n    if ( setenv( \"NCURSES_NO_UTF8_ACS\", \"1\", true ) < 0 ) {\n      perror( \"setenv\" );\n      exit( 1 );\n    }\n\n    /* clear STY environment variable so GNU screen regards us as top level */\n    if ( unsetenv( \"STY\" ) < 0 ) {\n      perror( \"unsetenv\" );\n      exit( 1 );\n    }\n\n    chdir_homedir();\n\n    if ( with_motd && ( !motd_hushed() ) ) {\n      // On illumos motd is printed by /etc/profile.\n#ifndef __sun\n      // For Ubuntu, try and print one of {,/var}/run/motd.dynamic.\n      // This file is only updated when pam_motd is run, but when\n      // mosh-server is run in the usual way with ssh via the script,\n      // this always happens.\n      // XXX Hackish knowledge of Ubuntu PAM configuration.\n      // But this seems less awful than build-time detection with autoconf.\n      if ( !print_motd( \"/run/motd.dynamic\" ) ) {\n        print_motd( \"/var/run/motd.dynamic\" );\n      }\n      // Always print traditional /etc/motd.\n      print_motd( \"/etc/motd\" );\n#endif\n      warn_unattached( utmp_entry );\n    }\n\n    /* Wait for parent to release us. */\n    char linebuf[81];\n    // -1, errno == EINTR -- retry\n    // otherwise, give up\n    while ( read( pipes[0], linebuf, sizeof( linebuf ) ) == -1 ) {\n      if ( errno != EINTR ) {\n        err( 1, \"parent signal\" );\n      }\n    }\n\n    Crypto::reenable_dumping_core();\n\n    if ( execvp( command_path.c_str(), command_argv ) < 0 ) {\n      warn( \"execvp: %s\", command_path.c_str() );\n      sleep( 3 );\n      exit( 1 );\n    }\n\n    if ( close( pipes[0] ) < 0 ) {\n      perror( \"child read pipe close\" );\n      exit( 1 );\n    }\n  } else {\n    /* parent */\n    if ( close( pipes[0] ) < 0 ) {\n      perror( \"parent read pipe close\" );\n      exit( 1 );\n    }\n\n    /* Drop unnecessary privileges */\n#ifdef HAVE_PLEDGE\n    /* OpenBSD pledge() syscall */\n    if ( pledge( \"stdio inet tty\", NULL ) ) {\n      perror( \"pledge() failed\" );\n      exit( 1 );\n    }\n#endif\n\n#ifdef HAVE_UTEMPTER\n    /* make utmp entry */\n    utempter_add_record( master, utmp_entry );\n#endif\n\n    try {\n      serve( master, pipes[1], terminal, *network, network_timeout, network_signaled_timeout );\n    } catch ( const Network::NetworkException& e ) {\n      fprintf( stderr, \"Network exception: %s\\n\", e.what() );\n    } catch ( const Crypto::CryptoException& e ) {\n      fprintf( stderr, \"Crypto exception: %s\\n\", e.what() );\n    }\n\n#ifdef HAVE_UTEMPTER\n    utempter_remove_record( master );\n#endif\n\n    if ( close( master ) < 0 ) {\n      perror( \"close\" );\n      exit( 1 );\n    }\n  }\n\n  fputs( \"\\n[mosh-server is exiting.]\\n\", stdout );\n\n  return 0;\n}\n\nstatic void serve( int host_fd,\n                   int pipe_fd,\n                   Terminal::Complete& terminal,\n                   ServerConnection& network,\n                   long network_timeout,\n                   long network_signaled_timeout )\n{\n  /* scale timeouts */\n  const uint64_t network_timeout_ms = static_cast<uint64_t>( network_timeout ) * 1000;\n  const uint64_t network_signaled_timeout_ms = static_cast<uint64_t>( network_signaled_timeout ) * 1000;\n  /* prepare to poll for events */\n  Select& sel = Select::get_instance();\n  sel.add_signal( SIGTERM );\n  sel.add_signal( SIGINT );\n  sel.add_signal( SIGUSR1 );\n\n  uint64_t last_remote_num = network.get_remote_state_num();\n\n#ifdef HAVE_UTEMPTER\n  bool connected_utmp = false;\n#endif\n#if defined( HAVE_SYSLOG ) || defined( HAVE_UTEMPTER )\n  bool force_connection_change_evt = false;\n  Addr saved_addr;\n  socklen_t saved_addr_len = 0;\n#endif\n\n#ifdef HAVE_SYSLOG\n  struct passwd* pw = getpwuid( getuid() );\n  if ( pw == NULL ) {\n    throw NetworkException( std::string( \"serve: getpwuid: \" ) + strerror( errno ), 0 );\n  }\n  syslog( LOG_INFO, \"user %s session begin\", pw->pw_name );\n#endif\n\n  bool child_released = false;\n\n  while ( true ) {\n    try {\n      static const uint64_t timeout_if_no_client = 60000;\n      int timeout = INT_MAX;\n      uint64_t now = Network::timestamp();\n\n      timeout = std::min( timeout, network.wait_time() );\n      timeout = std::min( timeout, terminal.wait_time( now ) );\n      if ( ( !network.get_remote_state_num() ) || network.shutdown_in_progress() ) {\n        timeout = std::min( timeout, 5000 );\n      }\n      /*\n       * The server goes completely asleep if it has no remote peer.\n       * We may want to wake up sooner.\n       */\n      if ( network_timeout_ms ) {\n        int64_t network_sleep = network_timeout_ms - ( now - network.get_latest_remote_state().timestamp );\n        if ( network_sleep < 0 ) {\n          network_sleep = 0;\n        } else if ( network_sleep > INT_MAX ) {\n          /* 24 days might be too soon.  That's OK. */\n          network_sleep = INT_MAX;\n        }\n        timeout = std::min( timeout, static_cast<int>( network_sleep ) );\n      }\n\n      /* poll for events */\n      sel.clear_fds();\n      std::vector<int> fd_list( network.fds() );\n      assert( fd_list.size() == 1 ); /* servers don't hop */\n      int network_fd = fd_list.back();\n      sel.add_fd( network_fd );\n      if ( !network.shutdown_in_progress() ) {\n        sel.add_fd( host_fd );\n      }\n\n      int active_fds = sel.select( timeout );\n      if ( active_fds < 0 ) {\n        perror( \"select\" );\n        break;\n      }\n\n      now = Network::timestamp();\n      uint64_t time_since_remote_state = now - network.get_latest_remote_state().timestamp;\n      std::string terminal_to_host;\n\n      if ( sel.read( network_fd ) ) {\n        /* packet received from the network */\n        network.recv();\n\n        /* is new user input available for the terminal? */\n        if ( network.get_remote_state_num() != last_remote_num ) {\n          last_remote_num = network.get_remote_state_num();\n\n          Network::UserStream us;\n          us.apply_string( network.get_remote_diff() );\n          /* apply userstream to terminal */\n          for ( size_t i = 0; i < us.size(); i++ ) {\n            const Parser::Action& action = us.get_action( i );\n            if ( typeid( action ) == typeid( Parser::Resize ) ) {\n              /* apply only the last consecutive Resize action */\n              if ( i < us.size() - 1 ) {\n                const Parser::Action& next = us.get_action( i + 1 );\n                if ( typeid( next ) == typeid( Parser::Resize ) ) {\n                  continue;\n                }\n              }\n              /* tell child process of resize */\n              const Parser::Resize& res = static_cast<const Parser::Resize&>( action );\n              struct winsize window_size;\n              if ( ioctl( host_fd, TIOCGWINSZ, &window_size ) < 0 ) {\n                perror( \"ioctl TIOCGWINSZ\" );\n                network.start_shutdown();\n              }\n              window_size.ws_col = res.width;\n              window_size.ws_row = res.height;\n              if ( ioctl( host_fd, TIOCSWINSZ, &window_size ) < 0 ) {\n                perror( \"ioctl TIOCSWINSZ\" );\n                network.start_shutdown();\n              }\n            }\n            terminal_to_host += terminal.act( action );\n          }\n\n          if ( !us.empty() ) {\n            /* register input frame number for future echo ack */\n            terminal.register_input_frame( last_remote_num, now );\n          }\n\n          /* update client with new state of terminal */\n          if ( !network.shutdown_in_progress() ) {\n            network.set_current_state( terminal );\n          }\n#if defined( HAVE_SYSLOG ) || defined( HAVE_UTEMPTER )\n#ifdef HAVE_UTEMPTER\n          if ( !connected_utmp ) {\n            force_connection_change_evt = true;\n          } else {\n            force_connection_change_evt = false;\n          }\n#else\n          force_connection_change_evt = false;\n#endif\n\n          /**\n           * - HAVE_UTEMPTER - update utmp entry if we have become \"connected\"\n           * - HAVE_SYSLOG - log connection information to syslog\n           **/\n          if ( ( force_connection_change_evt ) || saved_addr_len != network.get_remote_addr_len()\n               || memcmp( &saved_addr, &network.get_remote_addr(), saved_addr_len ) != 0 ) {\n\n            saved_addr = network.get_remote_addr();\n            saved_addr_len = network.get_remote_addr_len();\n\n            char host[NI_MAXHOST];\n            int errcode\n              = getnameinfo( &saved_addr.sa, saved_addr_len, host, sizeof( host ), NULL, 0, NI_NUMERICHOST );\n            if ( errcode != 0 ) {\n              throw NetworkException( std::string( \"serve: getnameinfo: \" ) + gai_strerror( errcode ), 0 );\n            }\n\n#ifdef HAVE_UTEMPTER\n            utempter_remove_record( host_fd );\n            char tmp[64 + NI_MAXHOST];\n            snprintf( tmp, 64 + NI_MAXHOST, \"%s via mosh [%ld]\", host, static_cast<long int>( getpid() ) );\n            utempter_add_record( host_fd, tmp );\n\n            connected_utmp = true;\n#endif\n\n#ifdef HAVE_SYSLOG\n            syslog( LOG_INFO, \"user %s connected from host: %s\", pw->pw_name, host );\n#endif\n          }\n#endif\n\n          /* Tell child to start login session. */\n          if ( !child_released ) {\n            if ( close( pipe_fd ) < 0 ) {\n              err( 1, \"child release\" );\n            }\n            child_released = true;\n          }\n        }\n      }\n\n      if ( ( !network.shutdown_in_progress() ) && sel.read( host_fd ) ) {\n        /* input from the host needs to be fed to the terminal */\n        const int buf_size = 16384;\n        char buf[buf_size];\n\n        /* fill buffer if possible */\n        ssize_t bytes_read = read( host_fd, buf, buf_size );\n\n        /* If the pty slave is closed, reading from the master can fail with\n           EIO (see #264).  So we treat errors on read() like EOF. */\n        if ( bytes_read <= 0 ) {\n          network.start_shutdown();\n        } else {\n          terminal_to_host += terminal.act( std::string( buf, bytes_read ) );\n\n          /* update client with new state of terminal */\n          network.set_current_state( terminal );\n        }\n      }\n\n      /* write user input and terminal writeback to the host */\n      if ( swrite( host_fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {\n        network.start_shutdown();\n      }\n\n      bool idle_shutdown = false;\n      if ( network_timeout_ms && network_timeout_ms <= time_since_remote_state ) {\n        idle_shutdown = true;\n        fprintf( stderr,\n                 \"Network idle for %llu seconds.\\n\",\n                 static_cast<unsigned long long>( time_since_remote_state / 1000 ) );\n      }\n      if ( sel.signal( SIGUSR1 )\n           && ( !network_signaled_timeout_ms || network_signaled_timeout_ms <= time_since_remote_state ) ) {\n        idle_shutdown = true;\n        fprintf( stderr,\n                 \"Network idle for %llu seconds when SIGUSR1 received\\n\",\n                 static_cast<unsigned long long>( time_since_remote_state / 1000 ) );\n      }\n\n      if ( sel.any_signal() || idle_shutdown ) {\n        /* shutdown signal */\n        if ( network.has_remote_addr() && ( !network.shutdown_in_progress() ) ) {\n          network.start_shutdown();\n        } else {\n          break;\n        }\n      }\n\n      /* quit if our shutdown has been acknowledged */\n      if ( network.shutdown_in_progress() && network.shutdown_acknowledged() ) {\n        break;\n      }\n\n      /* quit after shutdown acknowledgement timeout */\n      if ( network.shutdown_in_progress() && network.shutdown_ack_timed_out() ) {\n        break;\n      }\n\n      /* quit if we received and acknowledged a shutdown request */\n      if ( network.counterparty_shutdown_ack_sent() ) {\n        break;\n      }\n\n#ifdef HAVE_UTEMPTER\n      /* update utmp if has been more than 30 seconds since heard from client */\n      if ( connected_utmp && time_since_remote_state > 30000 ) {\n        utempter_remove_record( host_fd );\n\n        char tmp[64];\n        snprintf( tmp, 64, \"mosh [%ld]\", static_cast<long int>( getpid() ) );\n        utempter_add_record( host_fd, tmp );\n\n        connected_utmp = false;\n      }\n#endif\n\n      if ( terminal.set_echo_ack( now ) && !network.shutdown_in_progress() ) {\n        /* update client with new echo ack */\n        network.set_current_state( terminal );\n      }\n\n      if ( !network.get_remote_state_num() && time_since_remote_state >= timeout_if_no_client ) {\n        fprintf( stderr,\n                 \"No connection within %llu seconds.\\n\",\n                 static_cast<unsigned long long>( timeout_if_no_client / 1000 ) );\n        break;\n      }\n\n      network.tick();\n    } catch ( const Network::NetworkException& e ) {\n      fprintf( stderr, \"%s\\n\", e.what() );\n      spin();\n    } catch ( const Crypto::CryptoException& e ) {\n      if ( e.fatal ) {\n        throw;\n      } else {\n        fprintf( stderr, \"Crypto exception: %s\\n\", e.what() );\n      }\n    }\n  }\n#ifdef HAVE_SYSLOG\n  syslog( LOG_INFO, \"user %s session end\", pw->pw_name );\n#endif\n}\n\n/* Print the motd from a given file, if available */\nstatic bool print_motd( const char* filename )\n{\n  FILE* motd = fopen( filename, \"r\" );\n  if ( !motd ) {\n    return false;\n  }\n\n  const int BUFSIZE = 256;\n\n  char buffer[BUFSIZE];\n  while ( 1 ) {\n    size_t bytes_read = fread( buffer, 1, BUFSIZE, motd );\n    if ( bytes_read == 0 ) {\n      break; /* don't report error */\n    }\n    size_t bytes_written = fwrite( buffer, 1, bytes_read, stdout );\n    if ( bytes_written == 0 ) {\n      break;\n    }\n  }\n\n  fclose( motd );\n  return true;\n}\n\nstatic void chdir_homedir( void )\n{\n  const char* home = getenv( \"HOME\" );\n  if ( home == NULL ) {\n    struct passwd* pw = getpwuid( getuid() );\n    if ( pw == NULL ) {\n      perror( \"getpwuid\" );\n      return; /* non-fatal */\n    }\n    home = pw->pw_dir;\n  }\n\n  if ( chdir( home ) < 0 ) {\n    perror( \"chdir\" );\n  }\n\n  if ( setenv( \"PWD\", home, 1 ) < 0 ) {\n    perror( \"setenv\" );\n  }\n}\n\nstatic bool motd_hushed( void )\n{\n  /* must be in home directory already */\n  struct stat buf;\n  return 0 == lstat( \".hushlogin\", &buf );\n}\n\n#ifdef HAVE_UTMPX_H\nstatic bool device_exists( const char* ut_line )\n{\n  std::string device_name = std::string( \"/dev/\" ) + std::string( ut_line );\n  struct stat buf;\n  return 0 == lstat( device_name.c_str(), &buf );\n}\n#endif\n\nstatic void warn_unattached( const std::string& ignore_entry )\n{\n#ifdef HAVE_UTMPX_H\n  /* get username */\n  const struct passwd* pw = getpwuid( getuid() );\n  if ( pw == NULL ) {\n    perror( \"getpwuid\" );\n    /* non-fatal */\n    return;\n  }\n\n  const std::string username( pw->pw_name );\n\n  /* look for unattached sessions */\n  std::vector<std::string> unattached_mosh_servers;\n\n  while ( struct utmpx* entry = getutxent() ) {\n    if ( ( entry->ut_type == USER_PROCESS ) && ( username == std::string( entry->ut_user ) ) ) {\n      /* does line show unattached mosh session */\n      std::string text( entry->ut_host );\n      if ( ( text.size() >= 5 ) && ( text.substr( 0, 5 ) == \"mosh \" ) && ( text[text.size() - 1] == ']' )\n           && ( text != ignore_entry ) && device_exists( entry->ut_line ) ) {\n        unattached_mosh_servers.push_back( text );\n      }\n    }\n  }\n\n  /* print out warning if necessary */\n  if ( unattached_mosh_servers.empty() ) {\n    return;\n  } else if ( unattached_mosh_servers.size() == 1 ) {\n    printf( \"\\033[37;44mMosh: You have a detached Mosh session on this server (%s).\\033[m\\n\\n\",\n            unattached_mosh_servers.front().c_str() );\n  } else {\n    std::string pid_string;\n\n    for ( std::vector<std::string>::const_iterator it = unattached_mosh_servers.begin();\n          it != unattached_mosh_servers.end();\n          it++ ) {\n      pid_string += \"        - \" + *it + \"\\n\";\n    }\n\n    printf( \"\\033[37;44mMosh: You have %d detached Mosh sessions on this server, with PIDs:\\n%s\\033[m\\n\",\n            (int)unattached_mosh_servers.size(),\n            pid_string.c_str() );\n  }\n#endif /* HAVE_UTMPX_H */\n}\n"
  },
  {
    "path": "src/frontend/stmclient.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cerrno>\n#include <clocale>\n#include <csignal>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n\n#include <err.h>\n#include <pwd.h>\n#include <sys/ioctl.h>\n#include <sys/types.h>\n#include <unistd.h>\n\n#if HAVE_PTY_H\n#include <pty.h>\n#elif HAVE_UTIL_H\n#include <util.h>\n#endif\n\n#include \"src/statesync/completeterminal.h\"\n#include \"src/statesync/user.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/locale_utils.h\"\n#include \"src/util/pty_compat.h\"\n#include \"src/util/select.h\"\n#include \"src/util/swrite.h\"\n#include \"src/util/timestamp.h\"\n#include \"stmclient.h\"\n\n#include \"src/network/networktransport-impl.h\"\n\nvoid STMClient::resume( void )\n{\n  /* Restore termios state */\n  if ( tcsetattr( STDIN_FILENO, TCSANOW, &raw_termios ) < 0 ) {\n    perror( \"tcsetattr\" );\n    exit( 1 );\n  }\n\n  /* Put terminal in application-cursor-key mode */\n  swrite( STDOUT_FILENO, display.open().c_str() );\n\n  /* Flag that outer terminal state is unknown */\n  repaint_requested = true;\n}\n\nvoid STMClient::init( void )\n{\n  if ( !is_utf8_locale() ) {\n    LocaleVar native_ctype = get_ctype();\n    std::string native_charset( locale_charset() );\n\n    fprintf( stderr,\n             \"mosh-client needs a UTF-8 native locale to run.\\n\\n\"\n             \"Unfortunately, the client's environment (%s) specifies\\n\"\n             \"the character set \\\"%s\\\".\\n\\n\",\n             native_ctype.str().c_str(),\n             native_charset.c_str() );\n    int unused __attribute( ( unused ) ) = system( \"locale\" );\n    exit( 1 );\n  }\n\n  /* Verify terminal configuration */\n  if ( tcgetattr( STDIN_FILENO, &saved_termios ) < 0 ) {\n    perror( \"tcgetattr\" );\n    exit( 1 );\n  }\n\n  /* Put terminal driver in raw mode */\n  raw_termios = saved_termios;\n\n#ifdef HAVE_IUTF8\n  if ( !( raw_termios.c_iflag & IUTF8 ) ) {\n    //    fprintf( stderr, \"Warning: Locale is UTF-8 but termios IUTF8 flag not set. Setting IUTF8 flag.\\n\" );\n    /* Probably not really necessary since we are putting terminal driver into raw mode anyway. */\n    raw_termios.c_iflag |= IUTF8;\n  }\n#endif /* HAVE_IUTF8 */\n\n  cfmakeraw( &raw_termios );\n\n  if ( tcsetattr( STDIN_FILENO, TCSANOW, &raw_termios ) < 0 ) {\n    perror( \"tcsetattr\" );\n    exit( 1 );\n  }\n\n  /* Put terminal in application-cursor-key mode */\n  swrite( STDOUT_FILENO, display.open().c_str() );\n\n  /* Add our name to window title */\n  if ( !getenv( \"MOSH_TITLE_NOPREFIX\" ) ) {\n    overlays.set_title_prefix( std::wstring( L\"[mosh] \" ) );\n  }\n\n  /* Set terminal escape key. */\n  const char* escape_key_env;\n  if ( ( escape_key_env = getenv( \"MOSH_ESCAPE_KEY\" ) ) != NULL ) {\n    if ( strlen( escape_key_env ) == 1 ) {\n      escape_key = (int)escape_key_env[0];\n      if ( escape_key > 0 && escape_key < 128 ) {\n        if ( escape_key < 32 ) {\n          /* If escape is ctrl-something, pass it with repeating the key without ctrl. */\n          escape_pass_key = escape_key + (int)'@';\n        } else {\n          /* If escape is something else, pass it with repeating the key itself. */\n          escape_pass_key = escape_key;\n        }\n        if ( escape_pass_key >= 'A' && escape_pass_key <= 'Z' ) {\n          /* If escape pass is an upper case character, define optional version\n             as lower case of the same. */\n          escape_pass_key2 = escape_pass_key + (int)'a' - (int)'A';\n        } else {\n          escape_pass_key2 = escape_pass_key;\n        }\n      } else {\n        escape_key = 0x1E;\n        escape_pass_key = '^';\n        escape_pass_key2 = '^';\n      }\n    } else if ( strlen( escape_key_env ) == 0 ) {\n      escape_key = -1;\n    } else {\n      escape_key = 0x1E;\n      escape_pass_key = '^';\n      escape_pass_key2 = '^';\n    }\n  } else {\n    escape_key = 0x1E;\n    escape_pass_key = '^';\n    escape_pass_key2 = '^';\n  }\n\n  /* There are so many better ways to shoot oneself into leg than\n     setting escape key to Ctrl-C, Ctrl-D, NewLine, Ctrl-L or CarriageReturn\n     that we just won't allow that. */\n  if ( escape_key == 0x03 || escape_key == 0x04 || escape_key == 0x0A || escape_key == 0x0C\n       || escape_key == 0x0D ) {\n    escape_key = 0x1E;\n    escape_pass_key = '^';\n    escape_pass_key2 = '^';\n  }\n\n  /* Adjust escape help differently if escape is a control character. */\n  if ( escape_key > 0 ) {\n    char escape_pass_name_buf[16];\n    char escape_key_name_buf[16];\n    snprintf( escape_pass_name_buf, sizeof escape_pass_name_buf, \"\\\"%c\\\"\", escape_pass_key );\n    if ( escape_key < 32 ) {\n      snprintf( escape_key_name_buf, sizeof escape_key_name_buf, \"Ctrl-%c\", escape_pass_key );\n      escape_requires_lf = false;\n    } else {\n      snprintf( escape_key_name_buf, sizeof escape_key_name_buf, \"\\\"%c\\\"\", escape_key );\n      escape_requires_lf = true;\n    }\n    std::string tmp;\n    tmp = std::string( escape_pass_name_buf );\n    std::wstring escape_pass_name = std::wstring( tmp.begin(), tmp.end() );\n    tmp = std::string( escape_key_name_buf );\n    std::wstring escape_key_name = std::wstring( tmp.begin(), tmp.end() );\n    escape_key_help\n      = L\"Commands: Ctrl-Z suspends, \\\".\\\" quits, \" + escape_pass_name + L\" gives literal \" + escape_key_name;\n    overlays.get_notification_engine().set_escape_key_string( tmp );\n  }\n  wchar_t tmp[128];\n  swprintf( tmp, 128, L\"Nothing received from server on UDP port %s.\", port.c_str() );\n  connecting_notification = std::wstring( tmp );\n}\n\nvoid STMClient::shutdown( void )\n{\n  /* Restore screen state */\n  overlays.get_notification_engine().set_notification_string( std::wstring( L\"\" ) );\n  overlays.get_notification_engine().server_heard( timestamp() );\n  overlays.set_title_prefix( std::wstring( L\"\" ) );\n  output_new_frame();\n\n  /* Restore terminal and terminal-driver state */\n  swrite( STDOUT_FILENO, display.close().c_str() );\n\n  if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) {\n    perror( \"tcsetattr\" );\n    exit( 1 );\n  }\n\n  if ( still_connecting() ) {\n    fprintf( stderr,\n             \"\\nmosh did not make a successful connection to %s:%s.\\n\"\n             \"Please verify that UDP port %s is not firewalled and can reach the server.\\n\\n\"\n             \"(By default, mosh uses a UDP port between 60000 and 61000. The -p option\\n\"\n             \"selects a specific UDP port number.)\\n\",\n             ip.c_str(),\n             port.c_str(),\n             port.c_str() );\n  } else if ( network && !clean_shutdown ) {\n    fputs( \"\\n\\nmosh did not shut down cleanly. Please note that the\\n\"\n           \"mosh-server process may still be running on the server.\\n\",\n           stderr );\n  }\n}\n\nvoid STMClient::main_init( void )\n{\n  Select& sel = Select::get_instance();\n  sel.add_signal( SIGWINCH );\n  sel.add_signal( SIGTERM );\n  sel.add_signal( SIGINT );\n  sel.add_signal( SIGHUP );\n  sel.add_signal( SIGPIPE );\n  sel.add_signal( SIGCONT );\n\n  /* get initial window size */\n  if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {\n    perror( \"ioctl TIOCGWINSZ\" );\n    return;\n  }\n\n  /* local state */\n  local_framebuffer = Terminal::Framebuffer( window_size.ws_col, window_size.ws_row );\n  new_state = Terminal::Framebuffer( 1, 1 );\n\n  /* initialize screen */\n  std::string init = display.new_frame( false, local_framebuffer, local_framebuffer );\n  swrite( STDOUT_FILENO, init.data(), init.size() );\n\n  /* open network */\n  Network::UserStream blank;\n  Terminal::Complete local_terminal( window_size.ws_col, window_size.ws_row );\n  network = NetworkPointer( new NetworkType( blank, local_terminal, key.c_str(), ip.c_str(), port.c_str() ) );\n\n  network->set_send_delay( 1 ); /* minimal delay on outgoing keystrokes */\n\n  /* tell server the size of the terminal */\n  network->get_current_state().push_back( Parser::Resize( window_size.ws_col, window_size.ws_row ) );\n\n  /* be noisy as necessary */\n  network->set_verbose( verbose );\n  Select::set_verbose( verbose );\n}\n\nvoid STMClient::output_new_frame( void )\n{\n  if ( !network ) { /* clean shutdown even when not initialized */\n    return;\n  }\n\n  /* fetch target state */\n  new_state = network->get_latest_remote_state().state.get_fb();\n\n  /* apply local overlays */\n  overlays.apply( new_state );\n\n  /* calculate minimal difference from where we are */\n  const std::string diff( display.new_frame( !repaint_requested, local_framebuffer, new_state ) );\n  swrite( STDOUT_FILENO, diff.data(), diff.size() );\n\n  repaint_requested = false;\n\n  local_framebuffer = new_state;\n}\n\nvoid STMClient::process_network_input( void )\n{\n  network->recv();\n\n  /* Now give hints to the overlays */\n  overlays.get_notification_engine().server_heard( network->get_latest_remote_state().timestamp );\n  overlays.get_notification_engine().server_acked( network->get_sent_state_acked_timestamp() );\n\n  overlays.get_prediction_engine().set_local_frame_acked( network->get_sent_state_acked() );\n  overlays.get_prediction_engine().set_send_interval( network->send_interval() );\n  overlays.get_prediction_engine().set_local_frame_late_acked(\n    network->get_latest_remote_state().state.get_echo_ack() );\n}\n\nbool STMClient::process_user_input( int fd )\n{\n  const int buf_size = 16384;\n  char buf[buf_size];\n\n  /* fill buffer if possible */\n  ssize_t bytes_read = read( fd, buf, buf_size );\n  if ( bytes_read == 0 ) { /* EOF */\n    return false;\n  } else if ( bytes_read < 0 ) {\n    perror( \"read\" );\n    return false;\n  }\n\n  NetworkType& net = *network;\n\n  if ( net.shutdown_in_progress() ) {\n    return true;\n  }\n  overlays.get_prediction_engine().set_local_frame_sent( net.get_sent_state_last() );\n\n  /* Don't predict for bulk data. */\n  bool paste = bytes_read > 100;\n  if ( paste ) {\n    overlays.get_prediction_engine().reset();\n  }\n\n  for ( int i = 0; i < bytes_read; i++ ) {\n    char the_byte = buf[i];\n\n    if ( !paste ) {\n      overlays.get_prediction_engine().new_user_byte( the_byte, local_framebuffer );\n    }\n\n    if ( quit_sequence_started ) {\n      if ( the_byte == '.' ) { /* Quit sequence is Ctrl-^ . */\n        if ( net.has_remote_addr() && ( !net.shutdown_in_progress() ) ) {\n          overlays.get_notification_engine().set_notification_string( std::wstring( L\"Exiting on user request...\" ),\n                                                                      true );\n          net.start_shutdown();\n          return true;\n        }\n        return false;\n      } else if ( the_byte == 0x1a ) { /* Suspend sequence is escape_key Ctrl-Z */\n        /* Restore terminal and terminal-driver state */\n        swrite( STDOUT_FILENO, display.close().c_str() );\n\n        if ( tcsetattr( STDIN_FILENO, TCSANOW, &saved_termios ) < 0 ) {\n          perror( \"tcsetattr\" );\n          exit( 1 );\n        }\n\n        fputs( \"\\n\\033[37;44m[mosh is suspended.]\\033[m\\n\", stdout );\n\n        fflush( NULL );\n\n        /* actually suspend */\n        kill( 0, SIGSTOP );\n\n        resume();\n      } else if ( ( the_byte == escape_pass_key ) || ( the_byte == escape_pass_key2 ) ) {\n        /* Emulation sequence to type escape_key is escape_key +\n           escape_pass_key (that is escape key without Ctrl) */\n        net.get_current_state().push_back( Parser::UserByte( escape_key ) );\n      } else {\n        /* Escape key followed by anything other than . and ^ gets sent literally */\n        net.get_current_state().push_back( Parser::UserByte( escape_key ) );\n        net.get_current_state().push_back( Parser::UserByte( the_byte ) );\n      }\n\n      quit_sequence_started = false;\n\n      if ( overlays.get_notification_engine().get_notification_string() == escape_key_help ) {\n        overlays.get_notification_engine().set_notification_string( L\"\" );\n      }\n\n      continue;\n    }\n\n    quit_sequence_started\n      = ( escape_key > 0 ) && ( the_byte == escape_key ) && ( lf_entered || ( !escape_requires_lf ) );\n    if ( quit_sequence_started ) {\n      lf_entered = false;\n      overlays.get_notification_engine().set_notification_string( escape_key_help, true, false );\n      continue;\n    }\n\n    lf_entered = ( ( the_byte == 0x0A )\n                   || ( the_byte == 0x0D ) ); /* LineFeed, Ctrl-J, '\\n' or CarriageReturn, Ctrl-M, '\\r' */\n\n    if ( the_byte == 0x0C ) { /* Ctrl-L */\n      repaint_requested = true;\n    }\n\n    net.get_current_state().push_back( Parser::UserByte( the_byte ) );\n  }\n\n  return true;\n}\n\nbool STMClient::process_resize( void )\n{\n  /* get new size */\n  if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {\n    perror( \"ioctl TIOCGWINSZ\" );\n    return false;\n  }\n\n  /* tell remote emulator */\n  Parser::Resize res( window_size.ws_col, window_size.ws_row );\n\n  if ( !network->shutdown_in_progress() ) {\n    network->get_current_state().push_back( res );\n  }\n\n  /* note remote emulator will probably reply with its own Resize to adjust our state */\n\n  /* tell prediction engine */\n  overlays.get_prediction_engine().reset();\n\n  return true;\n}\n\nbool STMClient::main( void )\n{\n  /* initialize signal handling and structures */\n  main_init();\n\n  /* Drop unnecessary privileges */\n#ifdef HAVE_PLEDGE\n  /* OpenBSD pledge() syscall */\n  if ( pledge( \"stdio inet tty\", NULL ) ) {\n    perror( \"pledge() failed\" );\n    exit( 1 );\n  }\n#endif\n\n  /* prepare to poll for events */\n  Select& sel = Select::get_instance();\n\n  while ( 1 ) {\n    try {\n      output_new_frame();\n\n      int wait_time = std::min( network->wait_time(), overlays.wait_time() );\n\n      /* Handle startup \"Connecting...\" message */\n      if ( still_connecting() ) {\n        wait_time = std::min( 250, wait_time );\n      }\n\n      /* poll for events */\n      /* network->fd() can in theory change over time */\n      sel.clear_fds();\n      std::vector<int> fd_list( network->fds() );\n      for ( std::vector<int>::const_iterator it = fd_list.begin(); it != fd_list.end(); it++ ) {\n        sel.add_fd( *it );\n      }\n      sel.add_fd( STDIN_FILENO );\n\n      int active_fds = sel.select( wait_time );\n      if ( active_fds < 0 ) {\n        perror( \"select\" );\n        break;\n      }\n\n      bool network_ready_to_read = false;\n\n      for ( std::vector<int>::const_iterator it = fd_list.begin(); it != fd_list.end(); it++ ) {\n        if ( sel.read( *it ) ) {\n          /* packet received from the network */\n          /* we only read one socket each run */\n          network_ready_to_read = true;\n        }\n      }\n\n      if ( network_ready_to_read ) {\n        process_network_input();\n      }\n\n      if ( sel.read( STDIN_FILENO )\n           && !process_user_input( STDIN_FILENO ) ) { /* input from the user needs to be fed to the network */\n        if ( !network->has_remote_addr() ) {\n          break;\n        } else if ( !network->shutdown_in_progress() ) {\n          overlays.get_notification_engine().set_notification_string( std::wstring( L\"Exiting...\" ), true );\n          network->start_shutdown();\n        }\n      }\n\n      if ( sel.signal( SIGWINCH ) && !process_resize() ) { /* resize */\n        return false;\n      }\n\n      if ( sel.signal( SIGCONT ) ) {\n        resume();\n      }\n\n      if ( sel.signal( SIGTERM ) || sel.signal( SIGINT ) || sel.signal( SIGHUP ) || sel.signal( SIGPIPE ) ) {\n        /* shutdown signal */\n        if ( !network->has_remote_addr() ) {\n          break;\n        } else if ( !network->shutdown_in_progress() ) {\n          overlays.get_notification_engine().set_notification_string(\n            std::wstring( L\"Signal received, shutting down...\" ), true );\n          network->start_shutdown();\n        }\n      }\n\n      /* quit if our shutdown has been acknowledged */\n      if ( network->shutdown_in_progress() && network->shutdown_acknowledged() ) {\n        clean_shutdown = true;\n        break;\n      }\n\n      /* quit after shutdown acknowledgement timeout */\n      if ( network->shutdown_in_progress() && network->shutdown_ack_timed_out() ) {\n        break;\n      }\n\n      /* quit if we received and acknowledged a shutdown request */\n      if ( network->counterparty_shutdown_ack_sent() ) {\n        clean_shutdown = true;\n        break;\n      }\n\n      /* write diagnostic message if can't reach server */\n      if ( still_connecting() && ( !network->shutdown_in_progress() )\n           && ( timestamp() - network->get_latest_remote_state().timestamp > 250 ) ) {\n        if ( timestamp() - network->get_latest_remote_state().timestamp > 15000 ) {\n          if ( !network->shutdown_in_progress() ) {\n            overlays.get_notification_engine().set_notification_string(\n              std::wstring( L\"Timed out waiting for server...\" ), true );\n            network->start_shutdown();\n          }\n        } else {\n          overlays.get_notification_engine().set_notification_string( connecting_notification );\n        }\n      } else if ( ( network->get_remote_state_num() != 0 )\n                  && ( overlays.get_notification_engine().get_notification_string() == connecting_notification ) ) {\n        overlays.get_notification_engine().set_notification_string( L\"\" );\n      }\n\n      network->tick();\n\n      std::string& send_error = network->get_send_error();\n      if ( !send_error.empty() ) {\n        overlays.get_notification_engine().set_network_error( send_error );\n        send_error.clear();\n      } else {\n        overlays.get_notification_engine().clear_network_error();\n      }\n    } catch ( const Network::NetworkException& e ) {\n      if ( !network->shutdown_in_progress() ) {\n        overlays.get_notification_engine().set_network_error( e.what() );\n      }\n\n      struct timespec req;\n      req.tv_sec = 0;\n      req.tv_nsec = 200000000; /* 0.2 sec */\n      nanosleep( &req, NULL );\n      freeze_timestamp();\n    } catch ( const Crypto::CryptoException& e ) {\n      if ( e.fatal ) {\n        throw;\n      } else {\n        wchar_t tmp[128];\n        swprintf( tmp, 128, L\"Crypto exception: %s\", e.what() );\n        overlays.get_notification_engine().set_notification_string( std::wstring( tmp ) );\n      }\n    }\n  }\n  return clean_shutdown;\n}\n"
  },
  {
    "path": "src/frontend/stmclient.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef STM_CLIENT_HPP\n#define STM_CLIENT_HPP\n\n#include <memory>\n#include <string>\n\n#include <sys/ioctl.h>\n#include <termios.h>\n\n#include \"src/frontend/terminaloverlay.h\"\n#include \"src/network/networktransport.h\"\n#include \"src/statesync/completeterminal.h\"\n#include \"src/statesync/user.h\"\n\nclass STMClient\n{\nprivate:\n  std::string ip;\n  std::string port;\n  std::string key;\n\n  int escape_key;\n  int escape_pass_key;\n  int escape_pass_key2;\n  bool escape_requires_lf;\n  std::wstring escape_key_help;\n\n  struct termios saved_termios, raw_termios;\n\n  struct winsize window_size;\n\n  Terminal::Framebuffer local_framebuffer, new_state;\n  Overlay::OverlayManager overlays;\n  using NetworkType = Network::Transport<Network::UserStream, Terminal::Complete>;\n  using NetworkPointer = std::shared_ptr<NetworkType>;\n  NetworkPointer network;\n  Terminal::Display display;\n\n  std::wstring connecting_notification;\n  bool repaint_requested, lf_entered, quit_sequence_started;\n  bool clean_shutdown;\n  unsigned int verbose;\n\n  void main_init( void );\n  void process_network_input( void );\n  bool process_user_input( int fd );\n  bool process_resize( void );\n\n  void output_new_frame( void );\n\n  bool still_connecting( void ) const\n  {\n    /* Initially, network == NULL */\n    return network && ( network->get_remote_state_num() == 0 );\n  }\n\n  void resume( void ); /* restore state after SIGCONT */\n\npublic:\n  STMClient( const char* s_ip,\n             const char* s_port,\n             const char* s_key,\n             const char* predict_mode,\n             unsigned int s_verbose,\n             const char* predict_overwrite )\n    : ip( s_ip ? s_ip : \"\" ), port( s_port ? s_port : \"\" ), key( s_key ? s_key : \"\" ), escape_key( 0x1E ),\n      escape_pass_key( '^' ), escape_pass_key2( '^' ), escape_requires_lf( false ), escape_key_help( L\"?\" ),\n      saved_termios(), raw_termios(), window_size(), local_framebuffer( 1, 1 ), new_state( 1, 1 ), overlays(),\n      network(), display( true ) /* use TERM environment var to initialize display */, connecting_notification(),\n      repaint_requested( false ), lf_entered( false ), quit_sequence_started( false ), clean_shutdown( false ),\n      verbose( s_verbose )\n  {\n    if ( predict_mode ) {\n      if ( !strcmp( predict_mode, \"always\" ) ) {\n        overlays.get_prediction_engine().set_display_preference( Overlay::PredictionEngine::Always );\n      } else if ( !strcmp( predict_mode, \"never\" ) ) {\n        overlays.get_prediction_engine().set_display_preference( Overlay::PredictionEngine::Never );\n      } else if ( !strcmp( predict_mode, \"adaptive\" ) ) {\n        overlays.get_prediction_engine().set_display_preference( Overlay::PredictionEngine::Adaptive );\n      } else if ( !strcmp( predict_mode, \"experimental\" ) ) {\n        overlays.get_prediction_engine().set_display_preference( Overlay::PredictionEngine::Experimental );\n      } else {\n        fprintf( stderr, \"Unknown prediction mode %s.\\n\", predict_mode );\n        exit( 1 );\n      }\n    }\n    if ( predict_overwrite && !strcmp( predict_overwrite, \"yes\" ) ) {\n      overlays.get_prediction_engine().set_predict_overwrite( true );\n    }\n  }\n\n  void init( void );\n  void shutdown( void );\n  bool main( void );\n\n  /* unused */\n  STMClient( const STMClient& );\n  STMClient& operator=( const STMClient& );\n};\n\n#endif\n"
  },
  {
    "path": "src/frontend/terminaloverlay.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <algorithm>\n#include <climits>\n#include <cwchar>\n#include <list>\n#include <typeinfo>\n\n#include \"src/frontend/terminaloverlay.h\"\n\nusing namespace Overlay;\n\nvoid ConditionalOverlayCell::apply( Framebuffer& fb, uint64_t confirmed_epoch, int row, bool flag ) const\n{\n  if ( ( !active ) || ( row >= fb.ds.get_height() ) || ( col >= fb.ds.get_width() ) ) {\n    return;\n  }\n\n  if ( tentative( confirmed_epoch ) ) {\n    return;\n  }\n\n  if ( replacement.is_blank() && fb.get_cell( row, col )->is_blank() ) {\n    flag = false;\n  }\n\n  if ( unknown ) {\n    if ( flag && ( col != fb.ds.get_width() - 1 ) ) {\n      fb.get_mutable_cell( row, col )->get_renditions().set_attribute( Renditions::underlined, true );\n    }\n    return;\n  }\n\n  if ( *fb.get_cell( row, col ) != replacement ) {\n    *( fb.get_mutable_cell( row, col ) ) = replacement;\n    if ( flag ) {\n      fb.get_mutable_cell( row, col )->get_renditions().set_attribute( Renditions::underlined, true );\n    }\n  }\n}\n\nValidity ConditionalOverlayCell::get_validity( const Framebuffer& fb,\n                                               int row,\n                                               uint64_t early_ack __attribute__( ( unused ) ),\n                                               uint64_t late_ack ) const\n{\n  if ( !active ) {\n    return Inactive;\n  }\n\n  if ( ( row >= fb.ds.get_height() ) || ( col >= fb.ds.get_width() ) ) {\n    return IncorrectOrExpired;\n  }\n\n  const Cell& current = *( fb.get_cell( row, col ) );\n\n  /* see if it hasn't been updated yet */\n  if ( late_ack < expiration_frame ) {\n    return Pending;\n  }\n\n  if ( unknown ) {\n    return CorrectNoCredit;\n  }\n\n  if ( replacement.is_blank() ) { /* too easy for this to trigger falsely */\n    return CorrectNoCredit;\n  }\n\n  if ( current.contents_match( replacement ) ) {\n    std::vector<Cell>::const_iterator it = original_contents.begin();\n    for ( ; it != original_contents.end(); it++ ) {\n      if ( it->contents_match( replacement ) )\n        break;\n    }\n    if ( it == original_contents.end() ) {\n      return Correct;\n    }\n    return CorrectNoCredit;\n  }\n  return IncorrectOrExpired;\n}\n\nValidity ConditionalCursorMove::get_validity( const Framebuffer& fb,\n                                              uint64_t early_ack __attribute( ( unused ) ),\n                                              uint64_t late_ack ) const\n{\n  if ( !active ) {\n    return Inactive;\n  }\n\n  if ( ( row >= fb.ds.get_height() ) || ( col >= fb.ds.get_width() ) ) {\n    //    fprintf( stderr, \"Crazy cursor (%d,%d)!\\n\", row, col );\n    return IncorrectOrExpired;\n  }\n\n  if ( late_ack >= expiration_frame ) {\n    if ( ( fb.ds.get_cursor_col() == col ) && ( fb.ds.get_cursor_row() == row ) ) {\n      return Correct;\n    }\n    return IncorrectOrExpired;\n  }\n\n  return Pending;\n}\n\nvoid ConditionalCursorMove::apply( Framebuffer& fb, uint64_t confirmed_epoch ) const\n{\n  if ( !active ) {\n    return;\n  }\n\n  if ( tentative( confirmed_epoch ) ) {\n    return;\n  }\n\n  assert( row < fb.ds.get_height() );\n  assert( col < fb.ds.get_width() );\n  assert( !fb.ds.origin_mode );\n\n  fb.ds.move_row( row, false );\n  fb.ds.move_col( col, false, false );\n}\n\nNotificationEngine::NotificationEngine()\n  : last_word_from_server( timestamp() ), last_acked_state( timestamp() ), escape_key_string(), message(),\n    message_is_network_error( false ), message_expiration( -1 ), show_quit_keystroke( true )\n{}\n\nstatic std::string human_readable_duration( int num_seconds, const std::string& seconds_abbr )\n{\n  char tmp[128];\n  if ( num_seconds < 60 ) {\n    snprintf( tmp, 128, \"%d %s\", num_seconds, seconds_abbr.c_str() );\n  } else if ( num_seconds < 3600 ) {\n    snprintf( tmp, 128, \"%d:%02d\", num_seconds / 60, num_seconds % 60 );\n  } else {\n    snprintf( tmp, 128, \"%d:%02d:%02d\", num_seconds / 3600, ( num_seconds / 60 ) % 60, num_seconds % 60 );\n  }\n  return tmp;\n}\n\nvoid NotificationEngine::apply( Framebuffer& fb ) const\n{\n  uint64_t now = timestamp();\n\n  bool time_expired = need_countup( now );\n\n  if ( message.empty() && !time_expired ) {\n    return;\n  }\n\n  assert( fb.ds.get_width() > 0 );\n  assert( fb.ds.get_height() > 0 );\n\n  /* hide cursor if necessary */\n  if ( fb.ds.get_cursor_row() == 0 ) {\n    fb.ds.cursor_visible = false;\n  }\n\n  /* draw bar across top of screen */\n  Cell notification_bar( 0 );\n  notification_bar.get_renditions().set_foreground_color( 7 );\n  notification_bar.get_renditions().set_background_color( 4 );\n  notification_bar.append( 0x20 );\n\n  for ( int i = 0; i < fb.ds.get_width(); i++ ) {\n    *( fb.get_mutable_cell( 0, i ) ) = notification_bar;\n  }\n\n  /* write message */\n  wchar_t tmp[128];\n\n  /* We want to prefer the \"last contact\" message if we simply haven't\n     heard from the server in a while, but print the \"last reply\" message\n     if the problem is uplink-only. */\n\n  double since_heard = (double)( now - last_word_from_server ) / 1000.0;\n  double since_ack = (double)( now - last_acked_state ) / 1000.0;\n  const char server_message[] = \"contact\";\n  const char reply_message[] = \"reply\";\n\n  double time_elapsed = since_heard;\n  const char* explanation = server_message;\n\n  if ( reply_late( now ) && ( !server_late( now ) ) ) {\n    time_elapsed = since_ack;\n    explanation = reply_message;\n  }\n\n  const static char blank[] = \"\";\n\n  const char* keystroke_str = show_quit_keystroke ? escape_key_string.c_str() : blank;\n\n  if ( message.empty() && ( !time_expired ) ) {\n    return;\n  }\n  if ( message.empty() && time_expired ) {\n    swprintf( tmp,\n              128,\n              L\"mosh: Last %s %s ago.%s\",\n              explanation,\n              human_readable_duration( static_cast<int>( time_elapsed ), \"seconds\" ).c_str(),\n              keystroke_str );\n  } else if ( ( !message.empty() ) && ( !time_expired ) ) {\n    swprintf( tmp, 128, L\"mosh: %ls%s\", message.c_str(), keystroke_str );\n  } else {\n    swprintf( tmp,\n              128,\n              L\"mosh: %ls (%s without %s.)%s\",\n              message.c_str(),\n              human_readable_duration( static_cast<int>( time_elapsed ), \"s\" ).c_str(),\n              explanation,\n              keystroke_str );\n  }\n\n  std::wstring string_to_draw( tmp );\n\n  int overlay_col = 0;\n\n  Cell* combining_cell = fb.get_mutable_cell( 0, 0 );\n\n  /* We unfortunately duplicate the terminal's logic for how to render a Unicode sequence into graphemes */\n  for ( std::wstring::const_iterator i = string_to_draw.begin(); i != string_to_draw.end(); i++ ) {\n    if ( overlay_col >= fb.ds.get_width() ) {\n      break;\n    }\n\n    wchar_t ch = *i;\n    int chwidth = ch == L'\\0' ? -1 : wcwidth( ch );\n    Cell* this_cell = 0;\n\n    switch ( chwidth ) {\n      case 1: /* normal character */\n      case 2: /* wide character */\n        this_cell = fb.get_mutable_cell( 0, overlay_col );\n        fb.reset_cell( this_cell );\n        this_cell->get_renditions().set_attribute( Renditions::bold, true );\n        this_cell->get_renditions().set_foreground_color( 7 );\n        this_cell->get_renditions().set_background_color( 4 );\n\n        this_cell->append( ch );\n        this_cell->set_wide( chwidth == 2 );\n        combining_cell = this_cell;\n\n        overlay_col += chwidth;\n        break;\n      case 0: /* combining character */\n        if ( !combining_cell ) {\n          break;\n        }\n\n        if ( combining_cell->empty() ) {\n          assert( !combining_cell->get_wide() );\n          combining_cell->set_fallback( true );\n          overlay_col++;\n        }\n\n        if ( !combining_cell->full() ) {\n          combining_cell->append( ch );\n        }\n        break;\n      case -1: /* unprintable character */\n        break;\n      default:\n        assert( !\"unexpected character width from wcwidth()\" );\n    }\n  }\n}\n\nvoid NotificationEngine::adjust_message( void )\n{\n  if ( timestamp() >= message_expiration ) {\n    message.clear();\n  }\n}\n\nint NotificationEngine::wait_time( void ) const\n{\n  uint64_t next_expiry = INT_MAX;\n\n  uint64_t now = timestamp();\n\n  next_expiry = std::min( next_expiry, message_expiration - now );\n\n  if ( need_countup( now ) ) {\n    uint64_t countup_interval = 1000;\n    if ( ( now - last_word_from_server ) > 60000 ) {\n      /* If we've been disconnected for 60 seconds, save power by updating the\n         display less often.  See #243. */\n      countup_interval = Network::ACK_INTERVAL;\n    }\n    next_expiry = std::min( next_expiry, countup_interval );\n  }\n\n  return next_expiry;\n}\n\nvoid OverlayManager::apply( Framebuffer& fb )\n{\n  predictions.cull( fb );\n  predictions.apply( fb );\n  notifications.adjust_message();\n  notifications.apply( fb );\n  title.apply( fb );\n}\n\nvoid TitleEngine::set_prefix( const std::wstring& s )\n{\n  prefix = Terminal::Framebuffer::title_type( s.begin(), s.end() );\n}\n\nvoid ConditionalOverlayRow::apply( Framebuffer& fb, uint64_t confirmed_epoch, bool flag ) const\n{\n  for ( overlay_cells_type::const_iterator it = overlay_cells.begin(); it != overlay_cells.end(); it++ ) {\n    it->apply( fb, confirmed_epoch, row_num, flag );\n  }\n}\n\nvoid PredictionEngine::apply( Framebuffer& fb ) const\n{\n  if ( ( display_preference == Never )\n       || !( srtt_trigger || glitch_trigger || ( display_preference == Always )\n             || ( display_preference == Experimental ) ) ) {\n    return;\n  }\n\n  for ( cursors_type::const_iterator it = cursors.begin(); it != cursors.end(); it++ ) {\n    it->apply( fb, confirmed_epoch );\n  }\n\n  for ( overlays_type::const_iterator it = overlays.begin(); it != overlays.end(); it++ ) {\n    it->apply( fb, confirmed_epoch, flagging );\n  }\n}\n\nvoid PredictionEngine::kill_epoch( uint64_t epoch, const Framebuffer& fb )\n{\n  for ( cursors_type::iterator it = cursors.begin(); it != cursors.end(); ) {\n    cursors_type::iterator it_next = it;\n    it_next++;\n    if ( it->tentative( epoch - 1 ) ) {\n      cursors.erase( it );\n    }\n    it = it_next;\n  }\n\n  cursors.push_back( ConditionalCursorMove(\n    local_frame_sent + 1, fb.ds.get_cursor_row(), fb.ds.get_cursor_col(), prediction_epoch ) );\n  cursor().active = true;\n\n  for ( overlays_type::iterator i = overlays.begin(); i != overlays.end(); i++ ) {\n    for ( overlay_cells_type::iterator j = i->overlay_cells.begin(); j != i->overlay_cells.end(); j++ ) {\n      if ( j->tentative( epoch - 1 ) ) {\n        j->reset();\n      }\n    }\n  }\n\n  become_tentative();\n}\n\nvoid PredictionEngine::reset( void )\n{\n  cursors.clear();\n  overlays.clear();\n  become_tentative();\n\n  //  fprintf( stderr, \"RESETTING\\n\" );\n}\n\nvoid PredictionEngine::init_cursor( const Framebuffer& fb )\n{\n  if ( cursors.empty() ) {\n    /* initialize new cursor prediction */\n\n    cursors.push_back( ConditionalCursorMove(\n      local_frame_sent + 1, fb.ds.get_cursor_row(), fb.ds.get_cursor_col(), prediction_epoch ) );\n\n    cursor().active = true;\n  } else if ( cursor().tentative_until_epoch != prediction_epoch ) {\n    cursors.push_back(\n      ConditionalCursorMove( local_frame_sent + 1, cursor().row, cursor().col, prediction_epoch ) );\n\n    cursor().active = true;\n  }\n}\n\nvoid PredictionEngine::cull( const Framebuffer& fb )\n{\n  if ( display_preference == Never ) {\n    return;\n  }\n\n  if ( ( last_height != fb.ds.get_height() ) || ( last_width != fb.ds.get_width() ) ) {\n    last_height = fb.ds.get_height();\n    last_width = fb.ds.get_width();\n    reset();\n  }\n\n  uint64_t now = timestamp();\n\n  /* control srtt_trigger with hysteresis */\n  if ( send_interval > SRTT_TRIGGER_HIGH ) {\n    srtt_trigger = true;\n  } else if ( srtt_trigger && ( send_interval <= SRTT_TRIGGER_LOW ) /* 20 ms is current minimum value */\n              && ( !active() ) ) { /* only turn off when no predictions being shown */\n    srtt_trigger = false;\n  }\n\n  /* control underlining with hysteresis */\n  if ( send_interval > FLAG_TRIGGER_HIGH ) {\n    flagging = true;\n  } else if ( send_interval <= FLAG_TRIGGER_LOW ) {\n    flagging = false;\n  }\n\n  /* really big glitches also activate underlining */\n  if ( glitch_trigger > GLITCH_REPAIR_COUNT ) {\n    flagging = true;\n  }\n\n  /* go through cell predictions */\n\n  overlays_type::iterator i = overlays.begin();\n  while ( i != overlays.end() ) {\n    overlays_type::iterator inext = i;\n    inext++;\n    if ( ( i->row_num < 0 ) || ( i->row_num >= fb.ds.get_height() ) ) {\n      overlays.erase( i );\n      i = inext;\n      continue;\n    }\n\n    for ( overlay_cells_type::iterator j = i->overlay_cells.begin(); j != i->overlay_cells.end(); j++ ) {\n      switch ( j->get_validity( fb, i->row_num, local_frame_acked, local_frame_late_acked ) ) {\n        case IncorrectOrExpired:\n          if ( j->tentative( confirmed_epoch ) ) {\n\n            /*\n            fprintf( stderr, \"Bad tentative prediction in row %d, col %d (think %lc, actually %lc)\\n\",\n                     i->row_num, j->col,\n                     j->replacement.debug_contents(),\n                     fb.get_cell( i->row_num, j->col )->debug_contents()\n                     );\n            */\n\n            if ( display_preference == Experimental ) {\n              j->reset();\n            } else {\n              kill_epoch( j->tentative_until_epoch, fb );\n            }\n            /*\n            if ( j->display_time != uint64_t(-1) ) {\n              fprintf( stderr, \"TIMING %ld - %ld (TENT)\\n\", time(NULL), now - j->display_time );\n            }\n            */\n          } else {\n            /*\n            fprintf( stderr, \"[%d=>%d] Killing prediction in row %d, col %d (think %lc, actually %lc)\\n\",\n                     (int)local_frame_acked, (int)j->expiration_frame,\n                     i->row_num, j->col,\n                     j->replacement.debug_contents(),\n                     fb.get_cell( i->row_num, j->col )->debug_contents() );\n            */\n            /*\n            if ( j->display_time != uint64_t(-1) ) {\n              fprintf( stderr, \"TIMING %ld - %ld\\n\", time(NULL), now - j->display_time );\n            }\n            */\n\n            if ( display_preference == Experimental ) {\n              j->reset();\n            } else {\n              reset();\n              return;\n            }\n          }\n          break;\n        case Correct:\n          /*\n          if ( j->display_time != uint64_t(-1) ) {\n            fprintf( stderr, \"TIMING %ld + %ld\\n\", now, now - j->display_time );\n          }\n          */\n\n          if ( j->tentative_until_epoch > confirmed_epoch ) {\n            confirmed_epoch = j->tentative_until_epoch;\n\n            /*\n            fprintf( stderr, \"%lc in (%d,%d) confirms epoch %lu (predicting in epoch %lu)\\n\",\n                     j->replacement.debug_contents(), i->row_num, j->col,\n                     confirmed_epoch, prediction_epoch );\n            */\n          }\n\n          /* When predictions come in quickly, slowly take away the glitch trigger. */\n          if ( now - j->prediction_time < GLITCH_THRESHOLD\n               && ( glitch_trigger > 0 && now - GLITCH_REPAIR_MININTERVAL >= last_quick_confirmation ) ) {\n            glitch_trigger--;\n            last_quick_confirmation = now;\n          }\n\n          /* match rest of row to the actual renditions */\n          {\n            const Renditions& actual_renditions = fb.get_cell( i->row_num, j->col )->get_renditions();\n            for ( overlay_cells_type::iterator k = j; k != i->overlay_cells.end(); k++ ) {\n              k->replacement.get_renditions() = actual_renditions;\n            }\n          }\n\n          /* fallthrough */\n        case CorrectNoCredit:\n          j->reset();\n\n          break;\n        case Pending:\n          /* When a prediction takes a long time to be confirmed, we\n             activate the predictions even if SRTT is low */\n          if ( ( now - j->prediction_time ) >= GLITCH_FLAG_THRESHOLD ) {\n            glitch_trigger = GLITCH_REPAIR_COUNT * 2; /* display and underline */\n          } else if ( ( ( now - j->prediction_time ) >= GLITCH_THRESHOLD )\n                      && ( glitch_trigger < GLITCH_REPAIR_COUNT ) ) {\n            glitch_trigger = GLITCH_REPAIR_COUNT; /* just display */\n          }\n\n          break;\n        default:\n          break;\n      }\n    }\n\n    i = inext;\n  }\n\n  /* go through cursor predictions */\n  if ( !cursors.empty()\n       && cursor().get_validity( fb, local_frame_acked, local_frame_late_acked ) == IncorrectOrExpired ) {\n    /*\n      fprintf( stderr, \"Sadly, we're predicting (%d,%d) vs. (%d,%d) [tau: %ld, expiration_time=%ld, now=%ld]\\n\",\n      cursor().row, cursor().col,\n      fb.ds.get_cursor_row(),\n      fb.ds.get_cursor_col(),\n      cursor().tentative_until_epoch,\n      cursor().expiration_time,\n      now );\n    */\n    if ( display_preference == Experimental ) {\n      cursors.clear();\n    } else {\n      reset();\n      return;\n    }\n  }\n\n  /* NB: switching from list to another STL container could break this code.\n     So we don't use the cursors_type typedef. */\n  for ( std::list<ConditionalCursorMove>::iterator it = cursors.begin(); it != cursors.end(); ) {\n    if ( it->get_validity( fb, local_frame_acked, local_frame_late_acked ) != Pending ) {\n      it = cursors.erase( it );\n    } else {\n      it++;\n    }\n  }\n}\n\nConditionalOverlayRow& PredictionEngine::get_or_make_row( int row_num, int num_cols )\n{\n  overlays_type::iterator it;\n\n  for ( it = overlays.begin(); it != overlays.end(); it++ ) {\n    if ( it->row_num == row_num ) {\n      break;\n    }\n  }\n\n  if ( it != overlays.end() ) {\n    return *it;\n  }\n  /* make row */\n  ConditionalOverlayRow r( row_num );\n  r.overlay_cells.reserve( num_cols );\n  for ( int i = 0; i < num_cols; i++ ) {\n    r.overlay_cells.push_back( ConditionalOverlayCell( 0, i, prediction_epoch ) );\n    assert( r.overlay_cells[i].col == i );\n  }\n  overlays.push_back( r );\n  return overlays.back();\n}\n\nvoid PredictionEngine::new_user_byte( char the_byte, const Framebuffer& fb )\n{\n  if ( display_preference == Never ) {\n    return;\n  }\n  if ( display_preference == Experimental ) {\n    prediction_epoch = confirmed_epoch;\n  }\n\n  cull( fb );\n\n  uint64_t now = timestamp();\n\n  /* translate application-mode cursor control function to ANSI cursor control sequence */\n  if ( ( last_byte == 0x1b ) && ( the_byte == 'O' ) ) {\n    the_byte = '[';\n  }\n  last_byte = the_byte;\n\n  Parser::Actions actions;\n  parser.input( the_byte, actions );\n\n  for ( Parser::Actions::iterator it = actions.begin(); it != actions.end(); it++ ) {\n    Parser::Action& act = **it;\n\n    /*\n    fprintf( stderr, \"Action: %s (%lc)\\n\",\n             act->name().c_str(), act->char_present ? act->ch : L'_' );\n    */\n\n    const std::type_info& type_act = typeid( act );\n    if ( type_act == typeid( Parser::Print ) ) {\n      /* make new prediction */\n\n      init_cursor( fb );\n\n      assert( act.char_present );\n\n      wchar_t ch = act.ch;\n      /* XXX handle wide characters */\n\n      if ( ch == 0x7f ) { /* backspace */\n        //\tfprintf( stderr, \"Backspace.\\n\" );\n        ConditionalOverlayRow& the_row = get_or_make_row( cursor().row, fb.ds.get_width() );\n\n        if ( cursor().col > 0 ) {\n          cursor().col--;\n          cursor().expire( local_frame_sent + 1, now );\n\n          if ( predict_overwrite ) {\n            ConditionalOverlayCell& cell = the_row.overlay_cells[cursor().col];\n            cell.reset_with_orig();\n            cell.active = true;\n            cell.tentative_until_epoch = prediction_epoch;\n            cell.expire( local_frame_sent + 1, now );\n            const Cell orig_cell = *fb.get_cell();\n            cell.original_contents.push_back( orig_cell );\n            cell.replacement = orig_cell;\n            cell.replacement.clear();\n            cell.replacement.append( ' ' );\n          } else {\n            for ( int i = cursor().col; i < fb.ds.get_width(); i++ ) {\n              ConditionalOverlayCell& cell = the_row.overlay_cells[i];\n\n              cell.reset_with_orig();\n              cell.active = true;\n              cell.tentative_until_epoch = prediction_epoch;\n              cell.expire( local_frame_sent + 1, now );\n              cell.original_contents.push_back( *fb.get_cell( cursor().row, i ) );\n\n              if ( i + 2 < fb.ds.get_width() ) {\n                ConditionalOverlayCell& next_cell = the_row.overlay_cells[i + 1];\n                const Cell* next_cell_actual = fb.get_cell( cursor().row, i + 1 );\n\n                if ( next_cell.active ) {\n                  if ( next_cell.unknown ) {\n                    cell.unknown = true;\n                  } else {\n                    cell.unknown = false;\n                    cell.replacement = next_cell.replacement;\n                  }\n                } else {\n                  cell.unknown = false;\n                  cell.replacement = *next_cell_actual;\n                }\n              } else {\n                cell.unknown = true;\n              }\n            }\n          }\n        }\n      } else if ( ( ch < 0x20 ) || ( wcwidth( ch ) != 1 ) ) {\n        /* unknown print */\n        become_tentative();\n        //\tfprintf( stderr, \"Unknown print 0x%x\\n\", ch );\n      } else {\n        assert( cursor().row >= 0 );\n        assert( cursor().col >= 0 );\n        assert( cursor().row < fb.ds.get_height() );\n        assert( cursor().col < fb.ds.get_width() );\n\n        ConditionalOverlayRow& the_row = get_or_make_row( cursor().row, fb.ds.get_width() );\n\n        if ( cursor().col + 1 >= fb.ds.get_width() ) {\n          /* prediction in the last column is tricky */\n          /* e.g., emacs will show wrap character, shell will just put the character there */\n          become_tentative();\n        }\n\n        /* do the insert */\n        int rightmost_column = predict_overwrite ? cursor().col : fb.ds.get_width() - 1;\n        for ( int i = rightmost_column; i > cursor().col; i-- ) {\n          ConditionalOverlayCell& cell = the_row.overlay_cells[i];\n          cell.reset_with_orig();\n          cell.active = true;\n          cell.tentative_until_epoch = prediction_epoch;\n          cell.expire( local_frame_sent + 1, now );\n          cell.original_contents.push_back( *fb.get_cell( cursor().row, i ) );\n\n          ConditionalOverlayCell& prev_cell = the_row.overlay_cells[i - 1];\n          const Cell* prev_cell_actual = fb.get_cell( cursor().row, i - 1 );\n\n          if ( i == fb.ds.get_width() - 1 ) {\n            cell.unknown = true;\n          } else if ( prev_cell.active ) {\n            if ( prev_cell.unknown ) {\n              cell.unknown = true;\n            } else {\n              cell.unknown = false;\n              cell.replacement = prev_cell.replacement;\n            }\n          } else {\n            cell.unknown = false;\n            cell.replacement = *prev_cell_actual;\n          }\n        }\n\n        ConditionalOverlayCell& cell = the_row.overlay_cells[cursor().col];\n        cell.reset_with_orig();\n        cell.active = true;\n        cell.tentative_until_epoch = prediction_epoch;\n        cell.expire( local_frame_sent + 1, now );\n        cell.replacement.get_renditions() = fb.ds.get_renditions();\n\n        /* heuristic: match renditions of character to the left */\n        if ( cursor().col > 0 ) {\n          ConditionalOverlayCell& prev_cell = the_row.overlay_cells[cursor().col - 1];\n          const Cell* prev_cell_actual = fb.get_cell( cursor().row, cursor().col - 1 );\n          if ( prev_cell.active && ( !prev_cell.unknown ) ) {\n            cell.replacement.get_renditions() = prev_cell.replacement.get_renditions();\n          } else {\n            cell.replacement.get_renditions() = prev_cell_actual->get_renditions();\n          }\n        }\n\n        cell.replacement.clear();\n        cell.replacement.append( ch );\n        cell.original_contents.push_back( *fb.get_cell( cursor().row, cursor().col ) );\n\n        /*\n        fprintf( stderr, \"[%d=>%d] Predicting %lc in row %d, col %d [tue: %lu]\\n\",\n                 (int)local_frame_acked, (int)cell.expiration_frame,\n                 ch, cursor().row, cursor().col,\n                 cell.tentative_until_epoch );\n        */\n\n        cursor().expire( local_frame_sent + 1, now );\n\n        /* do we need to wrap? */\n        if ( cursor().col < fb.ds.get_width() - 1 ) {\n          cursor().col++;\n        } else {\n          become_tentative();\n          newline_carriage_return( fb );\n        }\n      }\n    } else if ( type_act == typeid( Parser::Execute ) ) {\n      if ( act.char_present && ( act.ch == 0x0d ) /* CR */ ) {\n        become_tentative();\n        newline_carriage_return( fb );\n      } else {\n        //\tfprintf( stderr, \"Execute 0x%x\\n\", act.ch );\n        become_tentative();\n      }\n    } else if ( type_act == typeid( Parser::Esc_Dispatch ) ) {\n      //      fprintf( stderr, \"Escape sequence\\n\" );\n      become_tentative();\n    } else if ( type_act == typeid( Parser::CSI_Dispatch ) ) {\n      if ( act.char_present && ( act.ch == L'C' ) ) { /* right arrow */\n        init_cursor( fb );\n        if ( cursor().col < fb.ds.get_width() - 1 ) {\n          cursor().col++;\n          cursor().expire( local_frame_sent + 1, now );\n        }\n      } else if ( act.char_present && ( act.ch == L'D' ) ) { /* left arrow */\n        init_cursor( fb );\n\n        if ( cursor().col > 0 ) {\n          cursor().col--;\n          cursor().expire( local_frame_sent + 1, now );\n        }\n      } else {\n        //\tfprintf( stderr, \"CSI sequence %lc\\n\", act.ch );\n        become_tentative();\n      }\n    }\n  }\n}\n\nvoid PredictionEngine::newline_carriage_return( const Framebuffer& fb )\n{\n  uint64_t now = timestamp();\n  init_cursor( fb );\n  cursor().col = 0;\n  if ( cursor().row == fb.ds.get_height() - 1 ) {\n    /* Don't try to predict scroll until we have versioned cell predictions */\n    /*\n    for ( overlays_type::iterator i = overlays.begin();\n          i != overlays.end();\n          i++ ) {\n      i->row_num--;\n      for ( overlay_cells_type::iterator j = i->overlay_cells.begin();\n            j != i->overlay_cells.end();\n            j++ ) {\n        if ( j->active ) {\n          j->expire( local_frame_sent + 1, now );\n        }\n      }\n    }\n    */\n\n    /* make blank prediction for last row */\n    ConditionalOverlayRow& the_row = get_or_make_row( cursor().row, fb.ds.get_width() );\n    for ( overlay_cells_type::iterator j = the_row.overlay_cells.begin(); j != the_row.overlay_cells.end(); j++ ) {\n      j->active = true;\n      j->tentative_until_epoch = prediction_epoch;\n      j->expire( local_frame_sent + 1, now );\n      j->replacement.clear();\n    }\n  } else {\n    cursor().row++;\n  }\n}\n\nvoid PredictionEngine::become_tentative( void )\n{\n  if ( display_preference != Experimental ) {\n    prediction_epoch++;\n  }\n\n  /*\n  fprintf( stderr, \"Now tentative in epoch %lu (confirmed=%lu)\\n\",\n           prediction_epoch, confirmed_epoch );\n  */\n}\n\nbool PredictionEngine::active( void ) const\n{\n  if ( !cursors.empty() ) {\n    return true;\n  }\n\n  for ( overlays_type::const_iterator i = overlays.begin(); i != overlays.end(); i++ ) {\n    for ( overlay_cells_type::const_iterator j = i->overlay_cells.begin(); j != i->overlay_cells.end(); j++ ) {\n      if ( j->active ) {\n        return true;\n      }\n    }\n  }\n\n  return false;\n}\n"
  },
  {
    "path": "src/frontend/terminaloverlay.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TERMINAL_OVERLAY_HPP\n#define TERMINAL_OVERLAY_HPP\n\n#include \"src/network/network.h\"\n#include \"src/network/transportsender.h\"\n#include \"src/terminal/parser.h\"\n#include \"src/terminal/terminalframebuffer.h\"\n\n#include <climits>\n#include <vector>\n\nnamespace Overlay {\nusing namespace Terminal;\nusing namespace Network;\n\nenum Validity\n{\n  Pending,\n  Correct,\n  CorrectNoCredit,\n  IncorrectOrExpired,\n  Inactive\n};\n\nclass ConditionalOverlay\n{\npublic:\n  uint64_t expiration_frame;\n  int col;\n  bool active;                    /* represents a prediction at all */\n  uint64_t tentative_until_epoch; /* when to show */\n  uint64_t prediction_time;       /* used to find long-pending predictions */\n\n  ConditionalOverlay( uint64_t s_exp, int s_col, uint64_t s_tentative )\n    : expiration_frame( s_exp ), col( s_col ), active( false ), tentative_until_epoch( s_tentative ),\n      prediction_time( uint64_t( -1 ) )\n  {}\n\n  virtual ~ConditionalOverlay() {}\n\n  bool tentative( uint64_t confirmed_epoch ) const { return tentative_until_epoch > confirmed_epoch; }\n  void reset( void )\n  {\n    expiration_frame = tentative_until_epoch = -1;\n    active = false;\n  }\n  void expire( uint64_t s_exp, uint64_t now )\n  {\n    expiration_frame = s_exp;\n    prediction_time = now;\n  }\n};\n\nclass ConditionalCursorMove : public ConditionalOverlay\n{\npublic:\n  int row;\n\n  void apply( Framebuffer& fb, uint64_t confirmed_epoch ) const;\n\n  Validity get_validity( const Framebuffer& fb, uint64_t early_ack, uint64_t late_ack ) const;\n\n  ConditionalCursorMove( uint64_t s_exp, int s_row, int s_col, uint64_t s_tentative )\n    : ConditionalOverlay( s_exp, s_col, s_tentative ), row( s_row )\n  {}\n};\n\nclass ConditionalOverlayCell : public ConditionalOverlay\n{\npublic:\n  Cell replacement;\n  bool unknown;\n\n  std::vector<Cell> original_contents; /* we don't give credit for correct predictions\n                                          that match the original contents */\n\n  void apply( Framebuffer& fb, uint64_t confirmed_epoch, int row, bool flag ) const;\n  Validity get_validity( const Framebuffer& fb, int row, uint64_t early_ack, uint64_t late_ack ) const;\n\n  ConditionalOverlayCell( uint64_t s_exp, int s_col, uint64_t s_tentative )\n    : ConditionalOverlay( s_exp, s_col, s_tentative ), replacement( 0 ), unknown( false ), original_contents()\n  {}\n\n  void reset( void )\n  {\n    unknown = false;\n    original_contents.clear();\n    ConditionalOverlay::reset();\n  }\n  void reset_with_orig( void )\n  {\n    if ( ( !active ) || unknown ) {\n      reset();\n      return;\n    }\n\n    original_contents.push_back( replacement );\n    ConditionalOverlay::reset();\n  }\n};\n\nclass ConditionalOverlayRow\n{\npublic:\n  int row_num;\n\n  using overlay_cells_type = std::vector<ConditionalOverlayCell>;\n  overlay_cells_type overlay_cells;\n\n  void apply( Framebuffer& fb, uint64_t confirmed_epoch, bool flag ) const;\n\n  ConditionalOverlayRow( int s_row_num ) : row_num( s_row_num ), overlay_cells() {}\n};\n\n/* the various overlays */\nclass NotificationEngine\n{\nprivate:\n  uint64_t last_word_from_server;\n  uint64_t last_acked_state;\n  std::string escape_key_string;\n  std::wstring message;\n  bool message_is_network_error;\n  uint64_t message_expiration;\n  bool show_quit_keystroke;\n\n  bool server_late( uint64_t ts ) const { return ( ts - last_word_from_server ) > 6500; }\n  bool reply_late( uint64_t ts ) const { return ( ts - last_acked_state ) > 10000; }\n  bool need_countup( uint64_t ts ) const { return server_late( ts ) || reply_late( ts ); }\n\npublic:\n  void adjust_message( void );\n  void apply( Framebuffer& fb ) const;\n  const std::wstring& get_notification_string( void ) const { return message; }\n  void server_heard( uint64_t s_last_word ) { last_word_from_server = s_last_word; }\n  void server_acked( uint64_t s_last_acked ) { last_acked_state = s_last_acked; }\n  int wait_time( void ) const;\n\n  void set_notification_string( const std::wstring& s_message,\n                                bool permanent = false,\n                                bool s_show_quit_keystroke = true )\n  {\n    message = s_message;\n    if ( permanent ) {\n      message_expiration = -1;\n    } else {\n      message_expiration = timestamp() + 1000;\n    }\n    message_is_network_error = false;\n    show_quit_keystroke = s_show_quit_keystroke;\n  }\n\n  void set_escape_key_string( const std::string& s_name )\n  {\n    char tmp[128];\n    snprintf( tmp, sizeof tmp, \" [To quit: %s .]\", s_name.c_str() );\n    escape_key_string = tmp;\n  }\n\n  void set_network_error( const std::string& s )\n  {\n    wchar_t tmp[128];\n    swprintf( tmp, 128, L\"%s\", s.c_str() );\n\n    message = tmp;\n    message_is_network_error = true;\n    message_expiration = timestamp() + Network::ACK_INTERVAL + 100;\n  }\n\n  void clear_network_error()\n  {\n    if ( message_is_network_error ) {\n      message_expiration = std::min( message_expiration, timestamp() + 1000 );\n    }\n  }\n\n  NotificationEngine();\n};\n\nclass PredictionEngine\n{\nprivate:\n  static const uint64_t SRTT_TRIGGER_LOW = 20;  /* <= ms cures SRTT trigger to show predictions */\n  static const uint64_t SRTT_TRIGGER_HIGH = 30; /* > ms starts SRTT trigger */\n\n  static const uint64_t FLAG_TRIGGER_LOW = 50;  /* <= ms cures flagging */\n  static const uint64_t FLAG_TRIGGER_HIGH = 80; /* > ms starts flagging */\n\n  static const uint64_t GLITCH_THRESHOLD = 250;          /* prediction outstanding this long is glitch */\n  static const uint64_t GLITCH_REPAIR_COUNT = 10;        /* non-glitches required to cure glitch trigger */\n  static const uint64_t GLITCH_REPAIR_MININTERVAL = 150; /* required time in between non-glitches */\n\n  static const uint64_t GLITCH_FLAG_THRESHOLD = 5000; /* prediction outstanding this long => underline */\n\n  char last_byte;\n  Parser::UTF8Parser parser;\n\n  using overlays_type = std::list<ConditionalOverlayRow>;\n  overlays_type overlays;\n\n  using cursors_type = std::list<ConditionalCursorMove>;\n  cursors_type cursors;\n\n  using overlay_cells_type = ConditionalOverlayRow::overlay_cells_type;\n\n  uint64_t local_frame_sent, local_frame_acked, local_frame_late_acked;\n\n  ConditionalOverlayRow& get_or_make_row( int row_num, int num_cols );\n\n  uint64_t prediction_epoch;\n  uint64_t confirmed_epoch;\n\n  void become_tentative( void );\n\n  void newline_carriage_return( const Framebuffer& fb );\n\n  bool flagging;               /* whether we are underlining predictions */\n  bool srtt_trigger;           /* show predictions because of slow round trip time */\n  unsigned int glitch_trigger; /* show predictions temporarily because of long-pending prediction */\n  uint64_t last_quick_confirmation;\n\n  ConditionalCursorMove& cursor( void )\n  {\n    assert( !cursors.empty() );\n    return cursors.back();\n  }\n\n  void kill_epoch( uint64_t epoch, const Framebuffer& fb );\n\n  void init_cursor( const Framebuffer& fb );\n\n  unsigned int send_interval;\n\n  int last_height, last_width;\n\npublic:\n  enum DisplayPreference\n  {\n    Always,\n    Never,\n    Adaptive,\n    Experimental\n  };\n\nprivate:\n  DisplayPreference display_preference;\n  bool predict_overwrite;\n\n  bool active( void ) const;\n\n  bool timing_tests_necessary( void ) const\n  {\n    /* Are there any timing-based triggers that haven't fired yet? */\n    return !( glitch_trigger && flagging );\n  }\n\npublic:\n  void set_display_preference( DisplayPreference s_pref ) { display_preference = s_pref; }\n  void set_predict_overwrite( bool overwrite ) { predict_overwrite = overwrite; }\n\n  void apply( Framebuffer& fb ) const;\n  void new_user_byte( char the_byte, const Framebuffer& fb );\n  void cull( const Framebuffer& fb );\n\n  void reset( void );\n\n  void set_local_frame_sent( uint64_t x ) { local_frame_sent = x; }\n  void set_local_frame_acked( uint64_t x ) { local_frame_acked = x; }\n  void set_local_frame_late_acked( uint64_t x ) { local_frame_late_acked = x; }\n\n  void set_send_interval( unsigned int x ) { send_interval = x; }\n\n  int wait_time( void ) const { return ( timing_tests_necessary() && active() ) ? 50 : INT_MAX; }\n\n  PredictionEngine( void )\n    : last_byte( 0 ), parser(), overlays(), cursors(), local_frame_sent( 0 ), local_frame_acked( 0 ),\n      local_frame_late_acked( 0 ), prediction_epoch( 1 ), confirmed_epoch( 0 ), flagging( false ),\n      srtt_trigger( false ), glitch_trigger( 0 ), last_quick_confirmation( 0 ), send_interval( 250 ),\n      last_height( 0 ), last_width( 0 ), display_preference( Adaptive ), predict_overwrite( false )\n  {}\n};\n\nclass TitleEngine\n{\nprivate:\n  Terminal::Framebuffer::title_type prefix;\n\npublic:\n  void apply( Framebuffer& fb ) const { fb.prefix_window_title( prefix ); }\n  TitleEngine() : prefix() {}\n  void set_prefix( const std::wstring& s );\n};\n\n/* the overlay manager */\nclass OverlayManager\n{\nprivate:\n  NotificationEngine notifications;\n  PredictionEngine predictions;\n  TitleEngine title;\n\npublic:\n  void apply( Framebuffer& fb );\n\n  NotificationEngine& get_notification_engine( void ) { return notifications; }\n  PredictionEngine& get_prediction_engine( void ) { return predictions; }\n\n  void set_title_prefix( const std::wstring& s ) { title.set_prefix( s ); }\n\n  OverlayManager() : notifications(), predictions(), title() {}\n\n  int wait_time( void ) const { return std::min( notifications.wait_time(), predictions.wait_time() ); }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/fuzz/Makefile.am",
    "content": "AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS) $(FUZZING_CFLAGS)\n\nif ENABLE_FUZZING\n  noinst_PROGRAMS = terminal_parser_fuzzer terminal_fuzzer\nendif\n\nterminal_parser_fuzzer_CPPFLAGS = -I$(top_srcdir)/\nterminal_parser_fuzzer_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a\nterminal_parser_fuzzer_SOURCES = terminal_parser_fuzzer.cc\n\nterminal_fuzzer_CPPFLAGS = -I$(top_srcdir)/\nterminal_fuzzer_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a ../statesync/libmoshstatesync.a ../protobufs/libmoshprotos.a $(TINFO_LIBS) $(protobuf_LIBS)\nterminal_fuzzer_SOURCES = terminal_fuzzer.cc\n"
  },
  {
    "path": "src/fuzz/terminal_corpus/3a52ce780950d4d969792a2559cd519d7ee8c727",
    "content": "."
  },
  {
    "path": "src/fuzz/terminal_corpus/433f367f36f48f78570c2013fef7a4f4b52b7c0c",
    "content": "&\u000e:#"
  },
  {
    "path": "src/fuzz/terminal_corpus/7c4d33785daa5c2370201ffa236b427aa37c9996",
    "content": "&"
  },
  {
    "path": "src/fuzz/terminal_corpus/9a78211436f6d425ec38f5c4e02270801f3524f8",
    "content": "@"
  },
  {
    "path": "src/fuzz/terminal_corpus/a70a7fcfa8e88039504b6a798314285419f51e16",
    "content": "\n\n@"
  },
  {
    "path": "src/fuzz/terminal_corpus/adc83b19e793491b1c6ea0fd8b46cd9f32e592fc",
    "content": "\n"
  },
  {
    "path": "src/fuzz/terminal_corpus/e0d3c08cb28736844512c52dc05fa4e4efd91490",
    "content": "\n@"
  },
  {
    "path": "src/fuzz/terminal_corpus/f195c020a28dfc5f2fb6af256b524ddcd93756ed",
    "content": ""
  },
  {
    "path": "src/fuzz/terminal_fuzzer.cc",
    "content": "#include <cstddef>\n#include <cstdint>\n\n#include \"src/statesync/completeterminal.h\"\n#include \"src/terminal/parser.h\"\n\nextern \"C\" int LLVMFuzzerTestOneInput( const uint8_t* data, size_t size )\n{\n  Terminal::Display display( false );\n  Terminal::Complete complete( 80, 24 );\n  Terminal::Framebuffer state( 80, 24 );\n  for ( size_t i = 0; i < size; i++ ) {\n    complete.act( Parser::UserByte( data[i] ) );\n  }\n  display.new_frame( true, state, complete.get_fb() );\n\n  return 0;\n}\n"
  },
  {
    "path": "src/fuzz/terminal_parser_corpus/7164cb6ab7e834fa6145bcf283e94b981313980d",
    "content": "\n\n"
  },
  {
    "path": "src/fuzz/terminal_parser_corpus/71853c6197a6a7f222db0f1978c7cb232b87c5ee",
    "content": "\n\n"
  },
  {
    "path": "src/fuzz/terminal_parser_corpus/a09fd95888cb80e1dcea4cc9dbd7d76928909927",
    "content": "\n"
  },
  {
    "path": "src/fuzz/terminal_parser_corpus/adc83b19e793491b1c6ea0fd8b46cd9f32e592fc",
    "content": "\n"
  },
  {
    "path": "src/fuzz/terminal_parser_corpus/b67f23988e8274fcf6150a18dacb5ab3db49520d",
    "content": "\n"
  },
  {
    "path": "src/fuzz/terminal_parser_fuzzer.cc",
    "content": "#include <cstddef>\n#include <cstdint>\n\n#include \"src/terminal/parser.h\"\n\nextern \"C\" int LLVMFuzzerTestOneInput( const uint8_t* data, size_t size )\n{\n  Parser::UTF8Parser parser;\n  Parser::Actions result;\n\n  for ( size_t i = 0; i < size; i++ ) {\n    parser.input( data[i], result );\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "src/include/Makefile.am",
    "content": "BUILT_SOURCES = version.h\nnodist_INCLUDES = version.h\n\nclean-local:\n\t@rm -rf version.h\n\nversion.h:\t../../VERSION.stamp\n\t@test -s $<\n\t@printf '#define BUILD_VERSION \"%s\"\\n' \"$$(cat $<)\" > $@.new\n\t@set -e; if ! diff -q $@ $@.new > /dev/null 2>&1; then \\\n\t\tmv -f $@.new $@; \\\n\tfi\n\t@rm -f $@.new\n\n"
  },
  {
    "path": "src/network/Makefile.am",
    "content": "AM_CPPFLAGS = -I$(top_srcdir)/ $(protobuf_CFLAGS)\nAM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\n\nnoinst_LIBRARIES = libmoshnetwork.a\n\nlibmoshnetwork_a_SOURCES = network.cc network.h networktransport-impl.h networktransport.h transportfragment.cc transportfragment.h transportsender-impl.h transportsender.h transportstate.h compressor.cc compressor.h\n"
  },
  {
    "path": "src/network/compressor.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <zlib.h>\n\n#include \"compressor.h\"\n#include \"src/util/dos_assert.h\"\n\nusing namespace Network;\n\nstd::string Compressor::compress_str( const std::string& input )\n{\n  long unsigned int len = BUFFER_SIZE;\n  dos_assert( Z_OK\n              == compress( buffer, &len, reinterpret_cast<const unsigned char*>( input.data() ), input.size() ) );\n  return std::string( reinterpret_cast<char*>( buffer ), len );\n}\n\nstd::string Compressor::uncompress_str( const std::string& input )\n{\n  long unsigned int len = BUFFER_SIZE;\n  dos_assert( Z_OK\n              == uncompress( buffer, &len, reinterpret_cast<const unsigned char*>( input.data() ), input.size() ) );\n  return std::string( reinterpret_cast<char*>( buffer ), len );\n}\n\n/* construct on first use */\nCompressor& Network::get_compressor( void )\n{\n  static Compressor the_compressor;\n  return the_compressor;\n}\n"
  },
  {
    "path": "src/network/compressor.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef COMPRESSOR_H\n#define COMPRESSOR_H\n\n#include <string>\n\nnamespace Network {\nclass Compressor\n{\nprivate:\n  static const int BUFFER_SIZE = 2048 * 2048; /* effective limit on terminal size */\n\n  unsigned char buffer[BUFFER_SIZE];\n\npublic:\n  Compressor() : buffer() {}\n  ~Compressor() {}\n\n  std::string compress_str( const std::string& input );\n  std::string uncompress_str( const std::string& input );\n\n  /* unused */\n  Compressor( const Compressor& );\n  Compressor& operator=( const Compressor& );\n};\n\nCompressor& get_compressor( void );\n}\n\n#endif\n"
  },
  {
    "path": "src/network/network.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cassert>\n#include <cerrno>\n#include <cstring>\n\n#include <sys/socket.h>\n#include <sys/types.h>\n#ifdef HAVE_SYS_UIO_H\n#include <sys/uio.h>\n#endif\n#include <netdb.h>\n#include <netinet/in.h>\n#include <unistd.h>\n\n#include \"src/crypto/byteorder.h\"\n#include \"src/crypto/crypto.h\"\n#include \"src/network/network.h\"\n#include \"src/util/dos_assert.h\"\n#include \"src/util/fatal_assert.h\"\n\n#include \"src/util/timestamp.h\"\n\n#ifndef MSG_DONTWAIT\n#define MSG_DONTWAIT MSG_NONBLOCK\n#endif\n\n#ifndef AI_NUMERICSERV\n#define AI_NUMERICSERV 0\n#endif\n\nusing namespace Network;\nusing namespace Crypto;\n\nconst uint64_t DIRECTION_MASK = uint64_t( 1 ) << 63;\nconst uint64_t SEQUENCE_MASK = uint64_t( -1 ) ^ DIRECTION_MASK;\n\n/* Read in packet */\nPacket::Packet( const Message& message )\n  : seq( message.nonce.val() & SEQUENCE_MASK ),\n    direction( ( message.nonce.val() & DIRECTION_MASK ) ? TO_CLIENT : TO_SERVER ), timestamp( -1 ),\n    timestamp_reply( -1 ), payload()\n{\n  dos_assert( message.text.size() >= 2 * sizeof( uint16_t ) );\n\n  const uint16_t* data = (uint16_t*)message.text.data();\n  timestamp = be16toh( data[0] );\n  timestamp_reply = be16toh( data[1] );\n\n  payload = std::string( message.text.begin() + 2 * sizeof( uint16_t ), message.text.end() );\n}\n\n/* Output from packet */\nMessage Packet::toMessage( void )\n{\n  uint64_t direction_seq = ( uint64_t( direction == TO_CLIENT ) << 63 ) | ( seq & SEQUENCE_MASK );\n\n  uint16_t ts_net[2]\n    = { static_cast<uint16_t>( htobe16( timestamp ) ), static_cast<uint16_t>( htobe16( timestamp_reply ) ) };\n\n  std::string timestamps = std::string( (char*)ts_net, 2 * sizeof( uint16_t ) );\n\n  return Message( Nonce( direction_seq ), timestamps + payload );\n}\n\nPacket Connection::new_packet( const std::string& s_payload )\n{\n  uint16_t outgoing_timestamp_reply = -1;\n\n  uint64_t now = timestamp();\n\n  if ( now - saved_timestamp_received_at < 1000 ) { /* we have a recent received timestamp */\n    /* send \"corrected\" timestamp advanced by how long we held it */\n    outgoing_timestamp_reply = saved_timestamp + ( now - saved_timestamp_received_at );\n    saved_timestamp = -1;\n    saved_timestamp_received_at = 0;\n  }\n\n  Packet p( direction, timestamp16(), outgoing_timestamp_reply, s_payload );\n\n  return p;\n}\n\nvoid Connection::hop_port( void )\n{\n  assert( !server );\n\n  setup();\n  assert( remote_addr_len != 0 );\n  socks.push_back( Socket( remote_addr.sa.sa_family ) );\n\n  prune_sockets();\n}\n\nvoid Connection::prune_sockets( void )\n{\n  /* don't keep old sockets if the new socket has been working for long enough */\n  if ( socks.size() > 1 ) {\n    if ( timestamp() - last_port_choice > MAX_OLD_SOCKET_AGE ) {\n      int num_to_kill = socks.size() - 1;\n      for ( int i = 0; i < num_to_kill; i++ ) {\n        socks.pop_front();\n      }\n    }\n  } else {\n    return;\n  }\n\n  /* make sure we don't have too many receive sockets open */\n  if ( socks.size() > MAX_PORTS_OPEN ) {\n    int num_to_kill = socks.size() - MAX_PORTS_OPEN;\n    for ( int i = 0; i < num_to_kill; i++ ) {\n      socks.pop_front();\n    }\n  }\n}\n\nConnection::Socket::Socket( int family ) : _fd( socket( family, SOCK_DGRAM, 0 ) )\n{\n  if ( _fd < 0 ) {\n    throw NetworkException( \"socket\", errno );\n  }\n\n  /* Disable path MTU discovery */\n#ifdef HAVE_IP_MTU_DISCOVER\n  int flag = IP_PMTUDISC_DONT;\n  if ( setsockopt( _fd, IPPROTO_IP, IP_MTU_DISCOVER, &flag, sizeof flag ) < 0 ) {\n    throw NetworkException( \"setsockopt\", errno );\n  }\n#endif\n\n  //  int dscp = 0x92; /* OS X does not have IPTOS_DSCP_AF42 constant */\n  int dscp = 0x02; /* ECN-capable transport only */\n  if ( setsockopt( _fd, IPPROTO_IP, IP_TOS, &dscp, sizeof dscp ) < 0 ) {\n    //    perror( \"setsockopt( IP_TOS )\" );\n  }\n\n  /* request explicit congestion notification on received datagrams */\n#ifdef HAVE_IP_RECVTOS\n  int tosflag = true;\n  if ( setsockopt( _fd, IPPROTO_IP, IP_RECVTOS, &tosflag, sizeof tosflag ) < 0\n       && family == IPPROTO_IP ) { /* FreeBSD disallows this option on IPv6 sockets. */\n    perror( \"setsockopt( IP_RECVTOS )\" );\n  }\n#endif\n}\n\nvoid Connection::setup( void )\n{\n  last_port_choice = timestamp();\n}\n\nconst std::vector<int> Connection::fds( void ) const\n{\n  std::vector<int> ret;\n\n  for ( std::deque<Socket>::const_iterator it = socks.begin(); it != socks.end(); it++ ) {\n    ret.push_back( it->fd() );\n  }\n\n  return ret;\n}\n\nvoid Connection::set_MTU( int family )\n{\n  switch ( family ) {\n    case AF_INET:\n      MTU = DEFAULT_IPV4_MTU - IPV4_HEADER_LEN;\n      break;\n    case AF_INET6:\n      MTU = DEFAULT_IPV6_MTU - IPV6_HEADER_LEN;\n      break;\n    default:\n      throw NetworkException( \"Unknown address family\", 0 );\n  }\n}\n\nclass AddrInfo\n{\npublic:\n  struct addrinfo* res;\n  AddrInfo( const char* node, const char* service, const struct addrinfo* hints ) : res( NULL )\n  {\n    int errcode = getaddrinfo( node, service, hints, &res );\n    if ( errcode != 0 ) {\n      throw NetworkException( std::string( \"Bad IP address (\" ) + ( node != NULL ? node : \"(null)\" )\n                                + \"): \" + gai_strerror( errcode ),\n                              0 );\n    }\n  }\n  ~AddrInfo() { freeaddrinfo( res ); }\n\nprivate:\n  AddrInfo( const AddrInfo& );\n  AddrInfo& operator=( const AddrInfo& );\n};\n\nConnection::Connection( const char* desired_ip, const char* desired_port ) /* server */\n  : socks(), has_remote_addr( false ), remote_addr(), remote_addr_len( 0 ), server( true ), MTU( DEFAULT_SEND_MTU ),\n    key(), session( key ), direction( TO_CLIENT ), saved_timestamp( -1 ), saved_timestamp_received_at( 0 ),\n    expected_receiver_seq( 0 ), last_heard( -1 ), last_port_choice( -1 ), last_roundtrip_success( -1 ),\n    RTT_hit( false ), SRTT( 1000 ), RTTVAR( 500 ), send_error()\n{\n  setup();\n\n  /* The mosh wrapper always gives an IP request, in order\n     to deal with multihomed servers. The port is optional. */\n\n  /* If an IP request is given, we try to bind to that IP, but we also\n     try INADDR_ANY. If a port request is given, we bind only to that port. */\n\n  /* convert port numbers */\n  int desired_port_low = -1;\n  int desired_port_high = -1;\n\n  if ( desired_port && !parse_portrange( desired_port, desired_port_low, desired_port_high ) ) {\n    throw NetworkException( \"Invalid port range\", 0 );\n  }\n\n  /* try to bind to desired IP first */\n  if ( desired_ip ) {\n    try {\n      if ( try_bind( desired_ip, desired_port_low, desired_port_high ) ) {\n        return;\n      }\n    } catch ( const NetworkException& e ) {\n      fprintf( stderr, \"Error binding to IP %s: %s\\n\", desired_ip, e.what() );\n    }\n  }\n\n  /* now try any local interface */\n  try {\n    if ( try_bind( NULL, desired_port_low, desired_port_high ) ) {\n      return;\n    }\n  } catch ( const NetworkException& e ) {\n    fprintf( stderr, \"Error binding to any interface: %s\\n\", e.what() );\n    throw; /* this time it's fatal */\n  }\n\n  throw NetworkException( \"Could not bind\", errno );\n}\n\nbool Connection::try_bind( const char* addr, int port_low, int port_high )\n{\n  struct addrinfo hints;\n  memset( &hints, 0, sizeof( hints ) );\n  hints.ai_family = AF_UNSPEC;\n  hints.ai_socktype = SOCK_DGRAM;\n  hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV;\n  AddrInfo ai( addr, \"0\", &hints );\n\n  Addr local_addr;\n  socklen_t local_addr_len = ai.res->ai_addrlen;\n  memcpy( &local_addr.sa, ai.res->ai_addr, local_addr_len );\n\n  int search_low = PORT_RANGE_LOW, search_high = PORT_RANGE_HIGH;\n\n  if ( port_low != -1 ) { /* low port preference */\n    search_low = port_low;\n  }\n  if ( port_high != -1 ) { /* high port preference */\n    search_high = port_high;\n  }\n\n  socks.push_back( Socket( local_addr.sa.sa_family ) );\n  for ( int i = search_low; i <= search_high; i++ ) {\n    switch ( local_addr.sa.sa_family ) {\n      case AF_INET:\n        local_addr.sin.sin_port = htons( i );\n        break;\n      case AF_INET6:\n        local_addr.sin6.sin6_port = htons( i );\n        break;\n      default:\n        throw NetworkException( \"Unknown address family\", 0 );\n    }\n\n    if ( local_addr.sa.sa_family == AF_INET6\n         && memcmp( &local_addr.sin6.sin6_addr, &in6addr_any, sizeof( in6addr_any ) ) == 0 ) {\n      const int off = 0;\n      if ( setsockopt( sock(), IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof( off ) ) ) {\n        perror( \"setsockopt( IPV6_V6ONLY, off )\" );\n      }\n    }\n\n    if ( ::bind( sock(), &local_addr.sa, local_addr_len ) == 0 ) {\n      set_MTU( local_addr.sa.sa_family );\n      return true;\n    } // else fallthrough to below code, on last iteration.\n  }\n  int saved_errno = errno;\n  socks.pop_back();\n  char host[NI_MAXHOST], serv[NI_MAXSERV];\n  int errcode = getnameinfo( &local_addr.sa,\n                             local_addr_len,\n                             host,\n                             sizeof( host ),\n                             serv,\n                             sizeof( serv ),\n                             NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV );\n  if ( errcode != 0 ) {\n    throw NetworkException( std::string( \"bind: getnameinfo: \" ) + gai_strerror( errcode ), 0 );\n  }\n  fprintf( stderr, \"Failed binding to %s:%s\\n\", host, serv );\n  throw NetworkException( \"bind\", saved_errno );\n}\n\nConnection::Connection( const char* key_str, const char* ip, const char* port ) /* client */\n  : socks(), has_remote_addr( false ), remote_addr(), remote_addr_len( 0 ), server( false ),\n    MTU( DEFAULT_SEND_MTU ), key( key_str ), session( key ), direction( TO_SERVER ), saved_timestamp( -1 ),\n    saved_timestamp_received_at( 0 ), expected_receiver_seq( 0 ), last_heard( -1 ), last_port_choice( -1 ),\n    last_roundtrip_success( -1 ), RTT_hit( false ), SRTT( 1000 ), RTTVAR( 500 ), send_error()\n{\n  setup();\n\n  /* associate socket with remote host and port */\n  struct addrinfo hints;\n  memset( &hints, 0, sizeof( hints ) );\n  hints.ai_family = AF_UNSPEC;\n  hints.ai_socktype = SOCK_DGRAM;\n  hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;\n  AddrInfo ai( ip, port, &hints );\n  fatal_assert( static_cast<size_t>( ai.res->ai_addrlen ) <= sizeof( remote_addr ) );\n  remote_addr_len = ai.res->ai_addrlen;\n  memcpy( &remote_addr.sa, ai.res->ai_addr, remote_addr_len );\n\n  has_remote_addr = true;\n\n  socks.push_back( Socket( remote_addr.sa.sa_family ) );\n\n  set_MTU( remote_addr.sa.sa_family );\n}\n\nvoid Connection::send( const std::string& s )\n{\n  if ( !has_remote_addr ) {\n    return;\n  }\n\n  Packet px = new_packet( s );\n\n  std::string p = session.encrypt( px.toMessage() );\n\n  ssize_t bytes_sent = sendto( sock(), p.data(), p.size(), MSG_DONTWAIT, &remote_addr.sa, remote_addr_len );\n\n  if ( bytes_sent != static_cast<ssize_t>( p.size() ) ) {\n    /* Make sendto() failure available to the frontend. */\n    send_error = \"sendto: \";\n    send_error += strerror( errno );\n\n    if ( errno == EMSGSIZE ) {\n      MTU = DEFAULT_SEND_MTU; /* payload MTU of last resort */\n    }\n  }\n\n  uint64_t now = timestamp();\n  if ( server ) {\n    if ( now - last_heard > SERVER_ASSOCIATION_TIMEOUT ) {\n      has_remote_addr = false;\n      fprintf( stderr, \"Server now detached from client.\\n\" );\n    }\n  } else { /* client */\n    if ( ( now - last_port_choice > PORT_HOP_INTERVAL ) && ( now - last_roundtrip_success > PORT_HOP_INTERVAL ) ) {\n      hop_port();\n    }\n  }\n}\n\nstd::string Connection::recv( void )\n{\n  assert( !socks.empty() );\n  for ( std::deque<Socket>::const_iterator it = socks.begin(); it != socks.end(); it++ ) {\n    std::string payload;\n    try {\n      payload = recv_one( it->fd() );\n    } catch ( NetworkException& e ) {\n      if ( ( e.the_errno == EAGAIN ) || ( e.the_errno == EWOULDBLOCK ) ) {\n        continue;\n      } else {\n        throw;\n      }\n    }\n\n    /* succeeded */\n    prune_sockets();\n    return payload;\n  }\n  throw NetworkException( \"No packet received\" );\n}\n\nstd::string Connection::recv_one( int sock_to_recv )\n{\n  /* receive source address, ECN, and payload in msghdr structure */\n  Addr packet_remote_addr;\n  struct msghdr header;\n  struct iovec msg_iovec;\n\n  char msg_payload[Session::RECEIVE_MTU];\n  char msg_control[Session::RECEIVE_MTU];\n\n  /* receive source address */\n  header.msg_name = &packet_remote_addr;\n  header.msg_namelen = sizeof packet_remote_addr;\n\n  /* receive payload */\n  msg_iovec.iov_base = msg_payload;\n  msg_iovec.iov_len = sizeof msg_payload;\n  header.msg_iov = &msg_iovec;\n  header.msg_iovlen = 1;\n\n  /* receive explicit congestion notification */\n  header.msg_control = msg_control;\n  header.msg_controllen = sizeof msg_control;\n\n  /* receive flags */\n  header.msg_flags = 0;\n\n  ssize_t received_len = recvmsg( sock_to_recv, &header, MSG_DONTWAIT );\n\n  if ( received_len < 0 ) {\n    throw NetworkException( \"recvmsg\", errno );\n  }\n\n  if ( header.msg_flags & MSG_TRUNC ) {\n    throw NetworkException( \"Received oversize datagram\", errno );\n  }\n\n  /* receive ECN */\n  bool congestion_experienced = false;\n\n  struct cmsghdr* ecn_hdr = CMSG_FIRSTHDR( &header );\n  if ( ecn_hdr && ecn_hdr->cmsg_level == IPPROTO_IP\n       && ( ecn_hdr->cmsg_type == IP_TOS\n#ifdef IP_RECVTOS\n            || ecn_hdr->cmsg_type == IP_RECVTOS\n#endif\n            ) ) {\n    /* got one */\n    uint8_t* ecn_octet_p = (uint8_t*)CMSG_DATA( ecn_hdr );\n    assert( ecn_octet_p );\n\n    congestion_experienced = ( *ecn_octet_p & 0x03 ) == 0x03;\n  }\n\n  Packet p( session.decrypt( msg_payload, received_len ) );\n\n  dos_assert( p.direction == ( server ? TO_SERVER : TO_CLIENT ) ); /* prevent malicious playback to sender */\n\n  if ( p.seq\n       < expected_receiver_seq ) { /* don't use (but do return) out-of-order packets for timestamp or targeting */\n    return p.payload;\n  }\n  expected_receiver_seq = p.seq + 1; /* this is security-sensitive because a replay attack could otherwise\n                                        screw up the timestamp and targeting */\n\n  if ( p.timestamp != uint16_t( -1 ) ) {\n    saved_timestamp = p.timestamp;\n    saved_timestamp_received_at = timestamp();\n\n    if ( congestion_experienced ) {\n      /* signal counterparty to slow down */\n      /* this will gradually slow the counterparty down to the minimum frame rate */\n      saved_timestamp -= CONGESTION_TIMESTAMP_PENALTY;\n      if ( server ) {\n        fprintf( stderr, \"Received explicit congestion notification.\\n\" );\n      }\n    }\n  }\n\n  if ( p.timestamp_reply != uint16_t( -1 ) ) {\n    uint16_t now = timestamp16();\n    double R = timestamp_diff( now, p.timestamp_reply );\n\n    if ( R < 5000 ) {   /* ignore large values, e.g. server was Ctrl-Zed */\n      if ( !RTT_hit ) { /* first measurement */\n        SRTT = R;\n        RTTVAR = R / 2;\n        RTT_hit = true;\n      } else {\n        const double alpha = 1.0 / 8.0;\n        const double beta = 1.0 / 4.0;\n\n        RTTVAR = ( 1 - beta ) * RTTVAR + ( beta * fabs( SRTT - R ) );\n        SRTT = ( 1 - alpha ) * SRTT + ( alpha * R );\n      }\n    }\n  }\n\n  /* auto-adjust to remote host */\n  has_remote_addr = true;\n  last_heard = timestamp();\n\n  if ( server && /* only client can roam */\n       ( remote_addr_len != header.msg_namelen\n         || memcmp( &remote_addr, &packet_remote_addr, remote_addr_len ) != 0 ) ) {\n    remote_addr = packet_remote_addr;\n    remote_addr_len = header.msg_namelen;\n    char host[NI_MAXHOST], serv[NI_MAXSERV];\n    int errcode = getnameinfo( &remote_addr.sa,\n                               remote_addr_len,\n                               host,\n                               sizeof( host ),\n                               serv,\n                               sizeof( serv ),\n                               NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV );\n    if ( errcode != 0 ) {\n      throw NetworkException( std::string( \"recv_one: getnameinfo: \" ) + gai_strerror( errcode ), 0 );\n    }\n    fprintf( stderr, \"Server now attached to client at %s:%s\\n\", host, serv );\n  }\n  return p.payload;\n}\n\nstd::string Connection::port( void ) const\n{\n  Addr local_addr;\n  socklen_t addrlen = sizeof( local_addr );\n\n  if ( getsockname( sock(), &local_addr.sa, &addrlen ) < 0 ) {\n    throw NetworkException( \"getsockname\", errno );\n  }\n\n  char serv[NI_MAXSERV];\n  int errcode = getnameinfo( &local_addr.sa, addrlen, NULL, 0, serv, sizeof( serv ), NI_DGRAM | NI_NUMERICSERV );\n  if ( errcode != 0 ) {\n    throw NetworkException( std::string( \"port: getnameinfo: \" ) + gai_strerror( errcode ), 0 );\n  }\n\n  return std::string( serv );\n}\n\nuint64_t Network::timestamp( void )\n{\n  return frozen_timestamp();\n}\n\nuint16_t Network::timestamp16( void )\n{\n  uint16_t ts = timestamp() % 65536;\n  if ( ts == uint16_t( -1 ) ) {\n    ts++;\n  }\n  return ts;\n}\n\nuint16_t Network::timestamp_diff( uint16_t tsnew, uint16_t tsold )\n{\n  int diff = tsnew - tsold;\n  if ( diff < 0 ) {\n    diff += 65536;\n  }\n\n  assert( diff >= 0 );\n  assert( diff <= 65535 );\n\n  return diff;\n}\n\nuint64_t Connection::timeout( void ) const\n{\n  uint64_t RTO = lrint( ceil( SRTT + 4 * RTTVAR ) );\n  if ( RTO < MIN_RTO ) {\n    RTO = MIN_RTO;\n  } else if ( RTO > MAX_RTO ) {\n    RTO = MAX_RTO;\n  }\n  return RTO;\n}\n\nConnection::Socket::~Socket()\n{\n  fatal_assert( close( _fd ) == 0 );\n}\n\nConnection::Socket::Socket( const Socket& other ) : _fd( dup( other._fd ) )\n{\n  if ( _fd < 0 ) {\n    throw NetworkException( \"socket\", errno );\n  }\n}\n\nConnection::Socket& Connection::Socket::operator=( const Socket& other )\n{\n  if ( dup2( other._fd, _fd ) < 0 ) {\n    throw NetworkException( \"socket\", errno );\n  }\n\n  return *this;\n}\n\nbool Connection::parse_portrange( const char* desired_port, int& desired_port_low, int& desired_port_high )\n{\n  /* parse \"port\" or \"portlow:porthigh\" */\n  desired_port_low = desired_port_high = 0;\n  char* end;\n  long value;\n\n  /* parse first (only?) port */\n  errno = 0;\n  value = strtol( desired_port, &end, 10 );\n  if ( ( errno != 0 ) || ( *end != '\\0' && *end != ':' ) ) {\n    fprintf( stderr, \"Invalid (low) port number (%s)\\n\", desired_port );\n    return false;\n  }\n  if ( ( value < 0 ) || ( value > 65535 ) ) {\n    fprintf( stderr, \"(Low) port number %ld outside valid range [0..65535]\\n\", value );\n    return false;\n  }\n\n  desired_port_low = (int)value;\n  if ( *end == '\\0' ) { /* not a port range */\n    desired_port_high = desired_port_low;\n    return true;\n  }\n  /* port range; parse high port */\n  const char* cp = end + 1;\n  errno = 0;\n  value = strtol( cp, &end, 10 );\n  if ( ( errno != 0 ) || ( *end != '\\0' ) ) {\n    fprintf( stderr, \"Invalid high port number (%s)\\n\", cp );\n    return false;\n  }\n  if ( ( value < 0 ) || ( value > 65535 ) ) {\n    fprintf( stderr, \"High port number %ld outside valid range [0..65535]\\n\", value );\n    return false;\n  }\n\n  desired_port_high = (int)value;\n  if ( desired_port_low > desired_port_high ) {\n    fprintf( stderr, \"Low port %d greater than high port %d\\n\", desired_port_low, desired_port_high );\n    return false;\n  }\n\n  if ( desired_port_low == 0 ) {\n    fprintf( stderr, \"Low port 0 incompatible with port ranges\\n\" );\n    return false;\n  }\n\n  return true;\n}\n"
  },
  {
    "path": "src/network/network.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef NETWORK_HPP\n#define NETWORK_HPP\n\n#include <cassert>\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <deque>\n#include <exception>\n#include <string>\n#include <vector>\n\n#include <netinet/in.h>\n#include <sys/socket.h>\n\n#include \"src/crypto/crypto.h\"\n\nusing namespace Crypto;\n\nnamespace Network {\nstatic const unsigned int MOSH_PROTOCOL_VERSION = 2; /* bumped for echo-ack */\n\nuint64_t timestamp( void );\nuint16_t timestamp16( void );\nuint16_t timestamp_diff( uint16_t tsnew, uint16_t tsold );\n\nclass NetworkException : public std::exception\n{\npublic:\n  std::string function;\n  int the_errno;\n\nprivate:\n  std::string my_what;\n\npublic:\n  NetworkException( std::string s_function = \"<none>\", int s_errno = 0 )\n    : function( s_function ), the_errno( s_errno ), my_what( function + \": \" + strerror( the_errno ) )\n  {}\n  const char* what() const throw() { return my_what.c_str(); }\n  ~NetworkException() throw() {}\n};\n\nenum Direction\n{\n  TO_SERVER = 0,\n  TO_CLIENT = 1\n};\n\nclass Packet\n{\npublic:\n  const uint64_t seq;\n  Direction direction;\n  uint16_t timestamp, timestamp_reply;\n  std::string payload;\n\n  Packet( Direction s_direction, uint16_t s_timestamp, uint16_t s_timestamp_reply, const std::string& s_payload )\n    : seq( Crypto::unique() ), direction( s_direction ), timestamp( s_timestamp ),\n      timestamp_reply( s_timestamp_reply ), payload( s_payload )\n  {}\n\n  Packet( const Message& message );\n\n  Message toMessage( void );\n};\n\nunion Addr {\n  struct sockaddr sa;\n  struct sockaddr_in sin;\n  struct sockaddr_in6 sin6;\n  struct sockaddr_storage ss;\n};\n\nclass Connection\n{\nprivate:\n  /*\n   * For IPv4, guess the typical (minimum) header length;\n   * fragmentation is not dangerous, just inefficient.\n   */\n  static const int IPV4_HEADER_LEN = 20 /* base IP header */\n                                     + 8 /* UDP */;\n  /*\n   * For IPv6, we don't want to ever have MTU issues, so make a\n   * conservative guess about header size.\n   */\n  static const int IPV6_HEADER_LEN = 40   /* base IPv6 header */\n                                     + 16 /* 2 minimum-sized extension headers */\n                                     + 8 /* UDP */;\n  /* Application datagram MTU. For constructors and fallback. */\n  static const int DEFAULT_SEND_MTU = 500;\n  /*\n   * IPv4 MTU. Don't use full Ethernet-derived MTU,\n   * mobile networks have high tunneling overhead.\n   *\n   * As of July 2016, VPN traffic over Amtrak Acela wifi seems to be\n   * dropped if tunnelled packets are 1320 bytes or larger.  Use a\n   * 1280-byte IPv4 MTU for now.\n   *\n   * We may have to implement ICMP-less PMTUD (RFC 4821) eventually.\n   */\n  static const int DEFAULT_IPV4_MTU = 1280;\n  /* IPv6 MTU. Use the guaranteed minimum to avoid fragmentation. */\n  static const int DEFAULT_IPV6_MTU = 1280;\n\n  static const uint64_t MIN_RTO = 50;   /* ms */\n  static const uint64_t MAX_RTO = 1000; /* ms */\n\n  static const int PORT_RANGE_LOW = 60001;\n  static const int PORT_RANGE_HIGH = 60999;\n\n  static const unsigned int SERVER_ASSOCIATION_TIMEOUT = 40000;\n  static const unsigned int PORT_HOP_INTERVAL = 10000;\n\n  static const unsigned int MAX_PORTS_OPEN = 10;\n  static const unsigned int MAX_OLD_SOCKET_AGE = 60000;\n\n  static const int CONGESTION_TIMESTAMP_PENALTY = 500; /* ms */\n\n  bool try_bind( const char* addr, int port_low, int port_high );\n\n  class Socket\n  {\n  private:\n    int _fd;\n\n  public:\n    int fd( void ) const { return _fd; }\n    Socket( int family );\n    ~Socket();\n\n    Socket( const Socket& other );\n    Socket& operator=( const Socket& other );\n  };\n\n  std::deque<Socket> socks;\n  bool has_remote_addr;\n  Addr remote_addr;\n  socklen_t remote_addr_len;\n\n  bool server;\n\n  int MTU; /* application datagram MTU */\n\n  Base64Key key;\n  Session session;\n\n  void setup( void );\n\n  Direction direction;\n  uint16_t saved_timestamp;\n  uint64_t saved_timestamp_received_at;\n  uint64_t expected_receiver_seq;\n\n  uint64_t last_heard;\n  uint64_t last_port_choice;\n  uint64_t last_roundtrip_success; /* transport layer needs to tell us this */\n\n  bool RTT_hit;\n  double SRTT;\n  double RTTVAR;\n\n  /* Error from send()/sendto(). */\n  std::string send_error;\n\n  Packet new_packet( const std::string& s_payload );\n\n  void hop_port( void );\n\n  int sock( void ) const\n  {\n    assert( !socks.empty() );\n    return socks.back().fd();\n  }\n\n  void prune_sockets( void );\n\n  std::string recv_one( int sock_to_recv );\n\n  void set_MTU( int family );\n\npublic:\n  /* Network transport overhead. */\n  static const int ADDED_BYTES = 8 /* seqno/nonce */ + 4 /* timestamps */;\n\n  Connection( const char* desired_ip, const char* desired_port );      /* server */\n  Connection( const char* key_str, const char* ip, const char* port ); /* client */\n\n  void send( const std::string& s );\n  std::string recv( void );\n  const std::vector<int> fds( void ) const;\n  int get_MTU( void ) const { return MTU; }\n\n  std::string port( void ) const;\n  std::string get_key( void ) const { return key.printable_key(); }\n  bool get_has_remote_addr( void ) const { return has_remote_addr; }\n\n  uint64_t timeout( void ) const;\n  double get_SRTT( void ) const { return SRTT; }\n\n  const Addr& get_remote_addr( void ) const { return remote_addr; }\n  socklen_t get_remote_addr_len( void ) const { return remote_addr_len; }\n\n  std::string& get_send_error( void ) { return send_error; }\n\n  void set_last_roundtrip_success( uint64_t s_success ) { last_roundtrip_success = s_success; }\n\n  static bool parse_portrange( const char* desired_port_range, int& desired_port_low, int& desired_port_high );\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/network/networktransport-impl.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef NETWORK_TRANSPORT_IMPL_HPP\n#define NETWORK_TRANSPORT_IMPL_HPP\n\n#include \"src/network/networktransport.h\"\n\n#include \"transportsender-impl.h\"\n\nusing namespace Network;\n\ntemplate<class MyState, class RemoteState>\nTransport<MyState, RemoteState>::Transport( MyState& initial_state,\n                                            RemoteState& initial_remote,\n                                            const char* desired_ip,\n                                            const char* desired_port )\n  : connection( desired_ip, desired_port ), sender( &connection, initial_state ),\n    received_states( 1, TimestampedState<RemoteState>( timestamp(), 0, initial_remote ) ),\n    receiver_quench_timer( 0 ), last_receiver_state( initial_remote ), fragments(), verbose( 0 )\n{\n  /* server */\n}\n\ntemplate<class MyState, class RemoteState>\nTransport<MyState, RemoteState>::Transport( MyState& initial_state,\n                                            RemoteState& initial_remote,\n                                            const char* key_str,\n                                            const char* ip,\n                                            const char* port )\n  : connection( key_str, ip, port ), sender( &connection, initial_state ),\n    received_states( 1, TimestampedState<RemoteState>( timestamp(), 0, initial_remote ) ),\n    receiver_quench_timer( 0 ), last_receiver_state( initial_remote ), fragments(), verbose( 0 )\n{\n  /* client */\n}\n\ntemplate<class MyState, class RemoteState>\nvoid Transport<MyState, RemoteState>::recv( void )\n{\n  std::string s( connection.recv() );\n  Fragment frag( s );\n\n  if ( fragments.add_fragment( frag ) ) { /* complete packet */\n    Instruction inst = fragments.get_assembly();\n\n    if ( inst.protocol_version() != MOSH_PROTOCOL_VERSION ) {\n      throw NetworkException( \"mosh protocol version mismatch\", 0 );\n    }\n\n    sender.process_acknowledgment_through( inst.ack_num() );\n\n    /* inform network layer of roundtrip (end-to-end-to-end) connectivity */\n    connection.set_last_roundtrip_success( sender.get_sent_state_acked_timestamp() );\n\n    /* first, make sure we don't already have the new state */\n    for ( typename std::list<TimestampedState<RemoteState>>::iterator i = received_states.begin();\n          i != received_states.end();\n          i++ ) {\n      if ( inst.new_num() == i->num ) {\n        return;\n      }\n    }\n\n    /* now, make sure we do have the old state */\n    bool found = 0;\n    typename std::list<TimestampedState<RemoteState>>::iterator reference_state = received_states.begin();\n    while ( reference_state != received_states.end() ) {\n      if ( inst.old_num() == reference_state->num ) {\n        found = true;\n        break;\n      }\n      reference_state++;\n    }\n\n    if ( !found ) {\n      //    fprintf( stderr, \"Ignoring out-of-order packet. Reference state %d has been discarded or hasn't yet been\n      //    received.\\n\", int(inst.old_num) );\n      return; /* this is security-sensitive and part of how we enforce idempotency */\n    }\n\n    /* Do not accept state if our queue is full */\n    /* This is better than dropping states from the middle of the\n       queue (as sender does), because we don't want to ACK a state\n       and then discard it later. */\n\n    process_throwaway_until( inst.throwaway_num() );\n\n    if ( received_states.size() > 1024 ) { /* limit on state queue */\n      uint64_t now = timestamp();\n      if ( now < receiver_quench_timer ) { /* deny letting state grow further */\n        if ( verbose ) {\n          fprintf(\n            stderr,\n            \"[%u] Receiver queue full, discarding %d (malicious sender or long-unidirectional connectivity?)\\n\",\n            (unsigned int)( timestamp() % 100000 ),\n            (int)inst.new_num() );\n        }\n        return;\n      } else {\n        receiver_quench_timer = now + 15000;\n      }\n    }\n\n    /* apply diff to reference state */\n    TimestampedState<RemoteState> new_state = *reference_state;\n    new_state.timestamp = timestamp();\n    new_state.num = inst.new_num();\n\n    if ( !inst.diff().empty() ) {\n      new_state.state.apply_string( inst.diff() );\n    }\n\n    /* Insert new state in sorted place */\n    for ( typename std::list<TimestampedState<RemoteState>>::iterator i = received_states.begin();\n          i != received_states.end();\n          i++ ) {\n      if ( i->num > new_state.num ) {\n        received_states.insert( i, new_state );\n        if ( verbose ) {\n          fprintf( stderr,\n                   \"[%u] Received OUT-OF-ORDER state %d [ack %d]\\n\",\n                   (unsigned int)( timestamp() % 100000 ),\n                   (int)new_state.num,\n                   (int)inst.ack_num() );\n        }\n        return;\n      }\n    }\n    if ( verbose ) {\n      fprintf( stderr,\n               \"[%u] Received state %d [coming from %d, ack %d]\\n\",\n               (unsigned int)( timestamp() % 100000 ),\n               (int)new_state.num,\n               (int)inst.old_num(),\n               (int)inst.ack_num() );\n    }\n    received_states.push_back( new_state );\n    sender.set_ack_num( received_states.back().num );\n\n    sender.remote_heard( new_state.timestamp );\n    if ( !inst.diff().empty() ) {\n      sender.set_data_ack();\n    }\n  }\n}\n\n/* The sender uses throwaway_num to tell us the earliest received state that we need to keep around */\ntemplate<class MyState, class RemoteState>\nvoid Transport<MyState, RemoteState>::process_throwaway_until( uint64_t throwaway_num )\n{\n  typename std::list<TimestampedState<RemoteState>>::iterator i = received_states.begin();\n  while ( i != received_states.end() ) {\n    typename std::list<TimestampedState<RemoteState>>::iterator inext = i;\n    inext++;\n    if ( i->num < throwaway_num ) {\n      received_states.erase( i );\n    }\n    i = inext;\n  }\n\n  fatal_assert( received_states.size() > 0 );\n}\n\ntemplate<class MyState, class RemoteState>\nstd::string Transport<MyState, RemoteState>::get_remote_diff( void )\n{\n  /* find diff between last receiver state and current remote state, then rationalize states */\n\n  std::string ret( received_states.back().state.diff_from( last_receiver_state ) );\n\n  const RemoteState* oldest_receiver_state = &received_states.front().state;\n\n  for ( typename std::list<TimestampedState<RemoteState>>::reverse_iterator i = received_states.rbegin();\n        i != received_states.rend();\n        i++ ) {\n    i->state.subtract( oldest_receiver_state );\n  }\n\n  last_receiver_state = received_states.back().state;\n\n  return ret;\n}\n\n#endif\n"
  },
  {
    "path": "src/network/networktransport.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef NETWORK_TRANSPORT_HPP\n#define NETWORK_TRANSPORT_HPP\n\n#include <csignal>\n#include <ctime>\n#include <list>\n#include <string>\n#include <vector>\n\n#include \"src/network/network.h\"\n#include \"src/network/transportsender.h\"\n#include \"transportfragment.h\"\n\nnamespace Network {\ntemplate<class MyState, class RemoteState>\nclass Transport\n{\nprivate:\n  /* the underlying, encrypted network connection */\n  Connection connection;\n\n  /* sender side */\n  TransportSender<MyState> sender;\n\n  /* helper methods for recv() */\n  void process_throwaway_until( uint64_t throwaway_num );\n\n  /* simple receiver */\n  std::list<TimestampedState<RemoteState>> received_states;\n  uint64_t receiver_quench_timer;\n  RemoteState last_receiver_state; /* the state we were in when user last queried state */\n  FragmentAssembly fragments;\n  unsigned int verbose;\n\npublic:\n  Transport( MyState& initial_state,\n             RemoteState& initial_remote,\n             const char* desired_ip,\n             const char* desired_port );\n  Transport( MyState& initial_state,\n             RemoteState& initial_remote,\n             const char* key_str,\n             const char* ip,\n             const char* port );\n\n  /* Send data or an ack if necessary. */\n  void tick( void ) { sender.tick(); }\n\n  /* Returns the number of ms to wait until next possible event. */\n  int wait_time( void ) { return sender.wait_time(); }\n\n  /* Blocks waiting for a packet. */\n  void recv( void );\n\n  /* Find diff between last receiver state and current remote state, then rationalize states. */\n  std::string get_remote_diff( void );\n\n  /* Shut down other side of connection. */\n  /* Illegal to change current_state after this. */\n  void start_shutdown( void ) { sender.start_shutdown(); }\n  bool shutdown_in_progress( void ) const { return sender.get_shutdown_in_progress(); }\n  bool shutdown_acknowledged( void ) const { return sender.get_shutdown_acknowledged(); }\n  bool shutdown_ack_timed_out( void ) const { return sender.shutdown_ack_timed_out(); }\n  bool has_remote_addr( void ) const { return connection.get_has_remote_addr(); }\n\n  /* Other side has requested shutdown and we have sent one ACK */\n  bool counterparty_shutdown_ack_sent( void ) const { return sender.get_counterparty_shutdown_acknowledged(); }\n\n  std::string port( void ) const { return connection.port(); }\n  std::string get_key( void ) const { return connection.get_key(); }\n\n  MyState& get_current_state( void ) { return sender.get_current_state(); }\n  void set_current_state( const MyState& x ) { sender.set_current_state( x ); }\n\n  uint64_t get_remote_state_num( void ) const { return received_states.back().num; }\n\n  const TimestampedState<RemoteState>& get_latest_remote_state( void ) const { return received_states.back(); }\n\n  const std::vector<int> fds( void ) const { return connection.fds(); }\n\n  void set_verbose( unsigned int s_verbose )\n  {\n    sender.set_verbose( s_verbose );\n    verbose = s_verbose;\n  }\n\n  void set_send_delay( int new_delay ) { sender.set_send_delay( new_delay ); }\n\n  uint64_t get_sent_state_acked_timestamp( void ) const { return sender.get_sent_state_acked_timestamp(); }\n  uint64_t get_sent_state_acked( void ) const { return sender.get_sent_state_acked(); }\n  uint64_t get_sent_state_last( void ) const { return sender.get_sent_state_last(); }\n\n  unsigned int send_interval( void ) const { return sender.send_interval(); }\n\n  const Addr& get_remote_addr( void ) const { return connection.get_remote_addr(); }\n  socklen_t get_remote_addr_len( void ) const { return connection.get_remote_addr_len(); }\n\n  std::string& get_send_error( void ) { return connection.get_send_error(); }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/network/transportfragment.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n\n#include \"compressor.h\"\n#include \"src/crypto/byteorder.h\"\n#include \"src/protobufs/transportinstruction.pb.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"transportfragment.h\"\n\nusing namespace Network;\nusing namespace TransportBuffers;\n\nstatic std::string network_order_string( uint16_t host_order )\n{\n  uint16_t net_int = htobe16( host_order );\n  return std::string( (char*)&net_int, sizeof( net_int ) );\n}\n\nstatic std::string network_order_string( uint64_t host_order )\n{\n  uint64_t net_int = htobe64( host_order );\n  return std::string( (char*)&net_int, sizeof( net_int ) );\n}\n\nstd::string Fragment::tostring( void )\n{\n  assert( initialized );\n\n  std::string ret;\n\n  ret += network_order_string( id );\n\n  fatal_assert(\n    !( fragment_num & 0x8000 ) ); /* effective limit on size of a terminal screen change or buffered user input */\n  uint16_t combined_fragment_num = ( final << 15 ) | fragment_num;\n  ret += network_order_string( combined_fragment_num );\n\n  assert( ret.size() == frag_header_len );\n\n  ret += contents;\n\n  return ret;\n}\n\nFragment::Fragment( const std::string& x )\n  : id( -1 ), fragment_num( -1 ), final( false ), initialized( true ), contents()\n{\n  fatal_assert( x.size() >= frag_header_len );\n  contents = std::string( x.begin() + frag_header_len, x.end() );\n\n  uint64_t data64;\n  uint16_t* data16 = (uint16_t*)x.data();\n  memcpy( &data64, x.data(), sizeof( data64 ) );\n  id = be64toh( data64 );\n  fragment_num = be16toh( data16[4] );\n  final = ( fragment_num & 0x8000 ) >> 15;\n  fragment_num &= 0x7FFF;\n}\n\nbool FragmentAssembly::add_fragment( Fragment& frag )\n{\n  /* see if this is a totally new packet */\n  if ( current_id != frag.id ) {\n    fragments.clear();\n    fragments.resize( frag.fragment_num + 1 );\n    fragments.at( frag.fragment_num ) = frag;\n    fragments_arrived = 1;\n    fragments_total = -1; /* unknown */\n    current_id = frag.id;\n  } else { /* not a new packet */\n    /* see if we already have this fragment */\n    if ( ( fragments.size() > frag.fragment_num ) && ( fragments.at( frag.fragment_num ).initialized ) ) {\n      /* make sure new version is same as what we already have */\n      assert( fragments.at( frag.fragment_num ) == frag );\n    } else {\n      if ( (int)fragments.size() < frag.fragment_num + 1 ) {\n        fragments.resize( frag.fragment_num + 1 );\n      }\n      fragments.at( frag.fragment_num ) = frag;\n      fragments_arrived++;\n    }\n  }\n\n  if ( frag.final ) {\n    fragments_total = frag.fragment_num + 1;\n    assert( (int)fragments.size() <= fragments_total );\n    fragments.resize( fragments_total );\n  }\n\n  if ( fragments_total != -1 ) {\n    assert( fragments_arrived <= fragments_total );\n  }\n\n  /* see if we're done */\n  return fragments_arrived == fragments_total;\n}\n\nInstruction FragmentAssembly::get_assembly( void )\n{\n  assert( fragments_arrived == fragments_total );\n\n  std::string encoded;\n\n  for ( int i = 0; i < fragments_total; i++ ) {\n    assert( fragments.at( i ).initialized );\n    encoded += fragments.at( i ).contents;\n  }\n\n  Instruction ret;\n  fatal_assert( ret.ParseFromString( get_compressor().uncompress_str( encoded ) ) );\n\n  fragments.clear();\n  fragments_arrived = 0;\n  fragments_total = -1;\n\n  return ret;\n}\n\nbool Fragment::operator==( const Fragment& x ) const\n{\n  return ( id == x.id ) && ( fragment_num == x.fragment_num ) && ( final == x.final )\n         && ( initialized == x.initialized ) && ( contents == x.contents );\n}\n\nstd::vector<Fragment> Fragmenter::make_fragments( const Instruction& inst, size_t MTU )\n{\n  MTU -= Fragment::frag_header_len;\n  if ( ( inst.old_num() != last_instruction.old_num() ) || ( inst.new_num() != last_instruction.new_num() )\n       || ( inst.ack_num() != last_instruction.ack_num() )\n       || ( inst.throwaway_num() != last_instruction.throwaway_num() )\n       || ( inst.chaff() != last_instruction.chaff() )\n       || ( inst.protocol_version() != last_instruction.protocol_version() ) || ( last_MTU != MTU ) ) {\n    next_instruction_id++;\n  }\n\n  if ( ( inst.old_num() == last_instruction.old_num() ) && ( inst.new_num() == last_instruction.new_num() ) ) {\n    assert( inst.diff() == last_instruction.diff() );\n  }\n\n  last_instruction = inst;\n  last_MTU = MTU;\n\n  std::string payload = get_compressor().compress_str( inst.SerializeAsString() );\n  uint16_t fragment_num = 0;\n  std::vector<Fragment> ret;\n\n  while ( !payload.empty() ) {\n    std::string this_fragment;\n    bool final = false;\n\n    if ( payload.size() > MTU ) {\n      this_fragment = std::string( payload.begin(), payload.begin() + MTU );\n      payload = std::string( payload.begin() + MTU, payload.end() );\n    } else {\n      this_fragment = payload;\n      payload.clear();\n      final = true;\n    }\n\n    ret.push_back( Fragment( next_instruction_id, fragment_num++, final, this_fragment ) );\n  }\n\n  return ret;\n}\n"
  },
  {
    "path": "src/network/transportfragment.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TRANSPORT_FRAGMENT_HPP\n#define TRANSPORT_FRAGMENT_HPP\n\n#include <cstdint>\n#include <string>\n#include <vector>\n\n#include \"src/protobufs/transportinstruction.pb.h\"\n\nnamespace Network {\nusing namespace TransportBuffers;\n\nclass Fragment\n{\npublic:\n  static const size_t frag_header_len = sizeof( uint64_t ) + sizeof( uint16_t );\n\n  uint64_t id;\n  uint16_t fragment_num;\n  bool final;\n\n  bool initialized;\n\n  std::string contents;\n\n  Fragment() : id( -1 ), fragment_num( -1 ), final( false ), initialized( false ), contents() {}\n\n  Fragment( uint64_t s_id, uint16_t s_fragment_num, bool s_final, const std::string& s_contents )\n    : id( s_id ), fragment_num( s_fragment_num ), final( s_final ), initialized( true ), contents( s_contents )\n  {}\n\n  Fragment( const std::string& x );\n\n  std::string tostring( void );\n\n  bool operator==( const Fragment& x ) const;\n};\n\nclass FragmentAssembly\n{\nprivate:\n  std::vector<Fragment> fragments;\n  uint64_t current_id;\n  int fragments_arrived, fragments_total;\n\npublic:\n  FragmentAssembly() : fragments(), current_id( -1 ), fragments_arrived( 0 ), fragments_total( -1 ) {}\n  bool add_fragment( Fragment& inst );\n  Instruction get_assembly( void );\n};\n\nclass Fragmenter\n{\nprivate:\n  uint64_t next_instruction_id;\n  Instruction last_instruction;\n  size_t last_MTU;\n\npublic:\n  Fragmenter() : next_instruction_id( 0 ), last_instruction(), last_MTU( -1 )\n  {\n    last_instruction.set_old_num( -1 );\n    last_instruction.set_new_num( -1 );\n  }\n  std::vector<Fragment> make_fragments( const Instruction& inst, size_t MTU );\n  uint64_t last_ack_sent( void ) const { return last_instruction.ack_num(); }\n};\n\n}\n\n#endif\n"
  },
  {
    "path": "src/network/transportsender-impl.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TRANSPORT_SENDER_IMPL_HPP\n#define TRANSPORT_SENDER_IMPL_HPP\n\n#include <algorithm>\n#include <climits>\n#include <cstdio>\n#include <cstdlib>\n#include <ctime>\n#include <list>\n\n#include \"src/network/transportfragment.h\"\n#include \"src/network/transportsender.h\"\n#include \"src/util/fatal_assert.h\"\n\nusing namespace Network;\n\ntemplate<class MyState>\nTransportSender<MyState>::TransportSender( Connection* s_connection, MyState& initial_state )\n  : connection( s_connection ), current_state( initial_state ),\n    sent_states( 1, TimestampedState<MyState>( timestamp(), 0, initial_state ) ),\n    assumed_receiver_state( sent_states.begin() ), fragmenter(), next_ack_time( timestamp() ),\n    next_send_time( timestamp() ), verbose( 0 ), shutdown_in_progress( false ), shutdown_tries( 0 ),\n    shutdown_start( -1 ), ack_num( 0 ), pending_data_ack( false ), SEND_MINDELAY( 8 ), last_heard( 0 ), prng(),\n    mindelay_clock( -1 )\n{}\n\n/* Try to send roughly two frames per RTT, bounded by limits on frame rate */\ntemplate<class MyState>\nunsigned int TransportSender<MyState>::send_interval( void ) const\n{\n  int SEND_INTERVAL = lrint( ceil( connection->get_SRTT() / 2.0 ) );\n  if ( SEND_INTERVAL < SEND_INTERVAL_MIN ) {\n    SEND_INTERVAL = SEND_INTERVAL_MIN;\n  } else if ( SEND_INTERVAL > SEND_INTERVAL_MAX ) {\n    SEND_INTERVAL = SEND_INTERVAL_MAX;\n  }\n\n  return SEND_INTERVAL;\n}\n\n/* Housekeeping routine to calculate next send and ack times */\ntemplate<class MyState>\nvoid TransportSender<MyState>::calculate_timers( void )\n{\n  uint64_t now = timestamp();\n\n  /* Update assumed receiver state */\n  update_assumed_receiver_state();\n\n  /* Cut out common prefix of all states */\n  rationalize_states();\n\n  if ( pending_data_ack && ( next_ack_time > now + ACK_DELAY ) ) {\n    next_ack_time = now + ACK_DELAY;\n  }\n\n  if ( !( current_state == sent_states.back().state ) ) {\n    if ( mindelay_clock == uint64_t( -1 ) ) {\n      mindelay_clock = now;\n    }\n\n    next_send_time = std::max( mindelay_clock + SEND_MINDELAY, sent_states.back().timestamp + send_interval() );\n  } else if ( !( current_state == assumed_receiver_state->state ) && ( last_heard + ACTIVE_RETRY_TIMEOUT > now ) ) {\n    next_send_time = sent_states.back().timestamp + send_interval();\n    if ( mindelay_clock != uint64_t( -1 ) ) {\n      next_send_time = std::max( next_send_time, mindelay_clock + SEND_MINDELAY );\n    }\n  } else if ( !( current_state == sent_states.front().state ) && ( last_heard + ACTIVE_RETRY_TIMEOUT > now ) ) {\n    next_send_time = sent_states.back().timestamp + connection->timeout() + ACK_DELAY;\n  } else {\n    next_send_time = uint64_t( -1 );\n  }\n\n  /* speed up shutdown sequence */\n  if ( shutdown_in_progress || ( ack_num == uint64_t( -1 ) ) ) {\n    next_ack_time = sent_states.back().timestamp + send_interval();\n  }\n}\n\n/* How many ms to wait until next event */\ntemplate<class MyState>\nint TransportSender<MyState>::wait_time( void )\n{\n  calculate_timers();\n\n  uint64_t next_wakeup = next_ack_time;\n  if ( next_send_time < next_wakeup ) {\n    next_wakeup = next_send_time;\n  }\n\n  uint64_t now = timestamp();\n\n  if ( !connection->get_has_remote_addr() ) {\n    return INT_MAX;\n  }\n\n  if ( next_wakeup > now ) {\n    return next_wakeup - now;\n  } else {\n    return 0;\n  }\n}\n\n/* Send data or an empty ack if necessary */\ntemplate<class MyState>\nvoid TransportSender<MyState>::tick( void )\n{\n  calculate_timers(); /* updates assumed receiver state and rationalizes */\n\n  if ( !connection->get_has_remote_addr() ) {\n    return;\n  }\n\n  uint64_t now = timestamp();\n\n  if ( ( now < next_ack_time ) && ( now < next_send_time ) ) {\n    return;\n  }\n\n  /* Determine if a new diff or empty ack needs to be sent */\n\n  std::string diff = current_state.diff_from( assumed_receiver_state->state );\n\n  attempt_prospective_resend_optimization( diff );\n\n  if ( verbose ) {\n    /* verify diff has round-trip identity (modulo Unicode fallback rendering) */\n    MyState newstate( assumed_receiver_state->state );\n    newstate.apply_string( diff );\n    if ( current_state.compare( newstate ) ) {\n      fprintf( stderr, \"Warning, round-trip Instruction verification failed!\\n\" );\n    }\n    /* Also verify that both the original frame and generated frame have the same initial diff. */\n    std::string current_diff( current_state.init_diff() );\n    std::string new_diff( newstate.init_diff() );\n    if ( current_diff != new_diff ) {\n      fprintf( stderr, \"Warning, target state Instruction verification failed!\\n\" );\n    }\n  }\n\n  if ( diff.empty() ) {\n    if ( ( now >= next_ack_time ) ) {\n      send_empty_ack();\n      mindelay_clock = uint64_t( -1 );\n    }\n    if ( ( now >= next_send_time ) ) {\n      next_send_time = uint64_t( -1 );\n      mindelay_clock = uint64_t( -1 );\n    }\n  } else if ( ( now >= next_send_time ) || ( now >= next_ack_time ) ) {\n    /* Send diffs or ack */\n    send_to_receiver( diff );\n    mindelay_clock = uint64_t( -1 );\n  }\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::send_empty_ack( void )\n{\n  uint64_t now = timestamp();\n\n  assert( now >= next_ack_time );\n\n  uint64_t new_num = sent_states.back().num + 1;\n\n  /* special case for shutdown sequence */\n  if ( shutdown_in_progress ) {\n    new_num = uint64_t( -1 );\n  }\n\n  //  sent_states.push_back( TimestampedState<MyState>( sent_states.back().timestamp, new_num, current_state ) );\n  add_sent_state( now, new_num, current_state );\n  send_in_fragments( \"\", new_num );\n\n  next_ack_time = now + ACK_INTERVAL;\n  next_send_time = uint64_t( -1 );\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::add_sent_state( uint64_t the_timestamp, uint64_t num, MyState& state )\n{\n  sent_states.push_back( TimestampedState<MyState>( the_timestamp, num, state ) );\n  if ( sent_states.size() > 32 ) { /* limit on state queue */\n    typename sent_states_type::iterator last = sent_states.end();\n    for ( int i = 0; i < 16; i++ ) {\n      last--;\n    }\n    sent_states.erase( last ); /* erase state from middle of queue */\n  }\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::send_to_receiver( const std::string& diff )\n{\n  uint64_t new_num;\n  if ( current_state == sent_states.back().state ) { /* previously sent */\n    new_num = sent_states.back().num;\n  } else { /* new state */\n    new_num = sent_states.back().num + 1;\n  }\n\n  /* special case for shutdown sequence */\n  if ( shutdown_in_progress ) {\n    new_num = uint64_t( -1 );\n  }\n\n  if ( new_num == sent_states.back().num ) {\n    sent_states.back().timestamp = timestamp();\n  } else {\n    add_sent_state( timestamp(), new_num, current_state );\n  }\n\n  send_in_fragments( diff, new_num ); // Can throw NetworkException\n\n  /* successfully sent, probably */\n  /* (\"probably\" because the FIRST size-exceeded datagram doesn't get an error) */\n  assumed_receiver_state = sent_states.end();\n  assumed_receiver_state--;\n  next_ack_time = timestamp() + ACK_INTERVAL;\n  next_send_time = uint64_t( -1 );\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::update_assumed_receiver_state( void )\n{\n  uint64_t now = timestamp();\n\n  /* start from what is known and give benefit of the doubt to unacknowledged states\n     transmitted recently enough ago */\n  assumed_receiver_state = sent_states.begin();\n\n  typename std::list<TimestampedState<MyState>>::iterator i = sent_states.begin();\n  i++;\n\n  while ( i != sent_states.end() ) {\n    assert( now >= i->timestamp );\n\n    if ( uint64_t( now - i->timestamp ) < connection->timeout() + ACK_DELAY ) {\n      assumed_receiver_state = i;\n    } else {\n      return;\n    }\n\n    i++;\n  }\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::rationalize_states( void )\n{\n  const MyState* known_receiver_state = &sent_states.front().state;\n\n  current_state.subtract( known_receiver_state );\n\n  for ( typename std::list<TimestampedState<MyState>>::reverse_iterator i = sent_states.rbegin();\n        i != sent_states.rend();\n        i++ ) {\n    i->state.subtract( known_receiver_state );\n  }\n}\n\ntemplate<class MyState>\nconst std::string TransportSender<MyState>::make_chaff( void )\n{\n  const size_t CHAFF_MAX = 16;\n  const size_t chaff_len = prng.uint8() % ( CHAFF_MAX + 1 );\n\n  char chaff[CHAFF_MAX];\n  prng.fill( chaff, chaff_len );\n  return std::string( chaff, chaff_len );\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::send_in_fragments( const std::string& diff, uint64_t new_num )\n{\n  Instruction inst;\n\n  inst.set_protocol_version( MOSH_PROTOCOL_VERSION );\n  inst.set_old_num( assumed_receiver_state->num );\n  inst.set_new_num( new_num );\n  inst.set_ack_num( ack_num );\n  inst.set_throwaway_num( sent_states.front().num );\n  inst.set_diff( diff );\n  inst.set_chaff( make_chaff() );\n\n  if ( new_num == uint64_t( -1 ) ) {\n    shutdown_tries++;\n  }\n\n  std::vector<Fragment> fragments = fragmenter.make_fragments(\n    inst, connection->get_MTU() - Network::Connection::ADDED_BYTES - Crypto::Session::ADDED_BYTES );\n  for ( std::vector<Fragment>::iterator i = fragments.begin(); i != fragments.end(); i++ ) {\n    connection->send( i->tostring() );\n\n    if ( verbose ) {\n      fprintf(\n        stderr,\n        \"[%u] Sent [%d=>%d] id %d, frag %d ack=%d, throwaway=%d, len=%d, frame rate=%.2f, timeout=%d, srtt=%.1f\\n\",\n        (unsigned int)( timestamp() % 100000 ),\n        (int)inst.old_num(),\n        (int)inst.new_num(),\n        (int)i->id,\n        (int)i->fragment_num,\n        (int)inst.ack_num(),\n        (int)inst.throwaway_num(),\n        (int)i->contents.size(),\n        1000.0 / (double)send_interval(),\n        (int)connection->timeout(),\n        connection->get_SRTT() );\n    }\n  }\n\n  pending_data_ack = false;\n}\n\ntemplate<class MyState>\nvoid TransportSender<MyState>::process_acknowledgment_through( uint64_t ack_num )\n{\n  /* Ignore ack if we have culled the state it's acknowledging */\n\n  typename sent_states_type::iterator i;\n  for ( i = sent_states.begin(); i != sent_states.end(); i++ ) {\n    if ( i->num == ack_num ) {\n      break;\n    }\n  }\n\n  if ( i != sent_states.end() ) {\n    for ( i = sent_states.begin(); i != sent_states.end(); ) {\n      typename sent_states_type::iterator i_next = i;\n      i_next++;\n      if ( i->num < ack_num ) {\n        sent_states.erase( i );\n      }\n      i = i_next;\n    }\n  }\n  assert( !sent_states.empty() );\n}\n\n/* give up on getting acknowledgement for shutdown */\ntemplate<class MyState>\nbool TransportSender<MyState>::shutdown_ack_timed_out( void ) const\n{\n  if ( shutdown_in_progress ) {\n    if ( shutdown_tries >= SHUTDOWN_RETRIES ) {\n      return true;\n    } else if ( timestamp() - shutdown_start >= uint64_t( ACTIVE_RETRY_TIMEOUT ) ) {\n      return true;\n    }\n  }\n\n  return false;\n}\n\n/* Executed upon entry to new receiver state */\ntemplate<class MyState>\nvoid TransportSender<MyState>::set_ack_num( uint64_t s_ack_num )\n{\n  ack_num = s_ack_num;\n}\n\n/* Investigate diff against known receiver state instead */\n/* Mutates proposed_diff */\ntemplate<class MyState>\nvoid TransportSender<MyState>::attempt_prospective_resend_optimization( std::string& proposed_diff )\n{\n  if ( assumed_receiver_state == sent_states.begin() ) {\n    return;\n  }\n\n  std::string resend_diff = current_state.diff_from( sent_states.front().state );\n\n  /* We do a prophylactic resend if it would make the diff shorter,\n     or if it would lengthen it by no more than 100 bytes and still be\n     less than 1000 bytes. */\n\n  if ( ( resend_diff.size() <= proposed_diff.size() )\n       || ( ( resend_diff.size() < 1000 ) && ( resend_diff.size() - proposed_diff.size() < 100 ) ) ) {\n    assumed_receiver_state = sent_states.begin();\n    proposed_diff = resend_diff;\n  }\n}\n\n#endif\n"
  },
  {
    "path": "src/network/transportsender.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TRANSPORT_SENDER_HPP\n#define TRANSPORT_SENDER_HPP\n\n#include <list>\n#include <string>\n\n#include \"src/crypto/prng.h\"\n#include \"src/network/network.h\"\n#include \"src/protobufs/transportinstruction.pb.h\"\n#include \"transportfragment.h\"\n#include \"transportstate.h\"\n\nnamespace Network {\nusing namespace TransportBuffers;\n\n/* timing parameters */\nconst int SEND_INTERVAL_MIN = 20;       /* ms between frames */\nconst int SEND_INTERVAL_MAX = 250;      /* ms between frames */\nconst int ACK_INTERVAL = 3000;          /* ms between empty acks */\nconst int ACK_DELAY = 100;              /* ms before delayed ack */\nconst int SHUTDOWN_RETRIES = 16;        /* number of shutdown packets to send before giving up */\nconst int ACTIVE_RETRY_TIMEOUT = 10000; /* attempt to resend at frame rate */\n\ntemplate<class MyState>\nclass TransportSender\n{\nprivate:\n  /* helper methods for tick() */\n  void update_assumed_receiver_state( void );\n  void attempt_prospective_resend_optimization( std::string& proposed_diff );\n  void rationalize_states( void );\n  void send_to_receiver( const std::string& diff );\n  void send_empty_ack( void );\n  void send_in_fragments( const std::string& diff, uint64_t new_num );\n  void add_sent_state( uint64_t the_timestamp, uint64_t num, MyState& state );\n\n  /* state of sender */\n  Connection* connection;\n\n  MyState current_state;\n\n  using sent_states_type = std::list<TimestampedState<MyState>>;\n  sent_states_type sent_states;\n  /* first element: known, acknowledged receiver state */\n  /* last element: last sent state */\n\n  /* somewhere in the middle: the assumed state of the receiver */\n  typename sent_states_type::iterator assumed_receiver_state;\n\n  /* for fragment creation */\n  Fragmenter fragmenter;\n\n  /* timing state */\n  uint64_t next_ack_time;\n  uint64_t next_send_time;\n\n  void calculate_timers( void );\n\n  unsigned int verbose;\n  bool shutdown_in_progress;\n  int shutdown_tries;\n  uint64_t shutdown_start;\n\n  /* information about receiver state */\n  uint64_t ack_num;\n  bool pending_data_ack;\n\n  unsigned int SEND_MINDELAY; /* ms to collect all input */\n\n  uint64_t last_heard; /* last time received new state */\n\n  /* chaff to disguise instruction length */\n  PRNG prng;\n  const std::string make_chaff( void );\n\n  uint64_t mindelay_clock; /* time of first pending change to current state */\n\npublic:\n  /* constructor */\n  TransportSender( Connection* s_connection, MyState& initial_state );\n\n  /* Send data or an ack if necessary */\n  void tick( void );\n\n  /* Returns the number of ms to wait until next possible event. */\n  int wait_time( void );\n\n  /* Executed upon receipt of ack */\n  void process_acknowledgment_through( uint64_t ack_num );\n\n  /* Executed upon entry to new receiver state */\n  void set_ack_num( uint64_t s_ack_num );\n\n  /* Accelerate reply ack */\n  void set_data_ack( void ) { pending_data_ack = true; }\n\n  /* Received something */\n  void remote_heard( uint64_t ts ) { last_heard = ts; }\n\n  /* Starts shutdown sequence */\n  void start_shutdown( void )\n  {\n    if ( !shutdown_in_progress ) {\n      shutdown_start = timestamp();\n      shutdown_in_progress = true;\n    }\n  }\n\n  /* Misc. getters and setters */\n  /* Cannot modify current_state while shutdown in progress */\n  MyState& get_current_state( void )\n  {\n    assert( !shutdown_in_progress );\n    return current_state;\n  }\n  void set_current_state( const MyState& x )\n  {\n    assert( !shutdown_in_progress );\n    current_state = x;\n    current_state.reset_input();\n  }\n  void set_verbose( unsigned int s_verbose ) { verbose = s_verbose; }\n\n  bool get_shutdown_in_progress( void ) const { return shutdown_in_progress; }\n  bool get_shutdown_acknowledged( void ) const { return sent_states.front().num == uint64_t( -1 ); }\n  bool get_counterparty_shutdown_acknowledged( void ) const { return fragmenter.last_ack_sent() == uint64_t( -1 ); }\n  uint64_t get_sent_state_acked_timestamp( void ) const { return sent_states.front().timestamp; }\n  uint64_t get_sent_state_acked( void ) const { return sent_states.front().num; }\n  uint64_t get_sent_state_last( void ) const { return sent_states.back().num; }\n\n  bool shutdown_ack_timed_out( void ) const;\n\n  void set_send_delay( int new_delay ) { SEND_MINDELAY = new_delay; }\n\n  unsigned int send_interval( void ) const;\n\n  /* nonexistent methods to satisfy -Weffc++ */\n  TransportSender( const TransportSender& x );\n  TransportSender& operator=( const TransportSender& x );\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/network/transportstate.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TRANSPORT_STATE_HPP\n#define TRANSPORT_STATE_HPP\n\nnamespace Network {\ntemplate<class State>\nclass TimestampedState\n{\npublic:\n  uint64_t timestamp;\n  uint64_t num;\n  State state;\n\n  TimestampedState( uint64_t s_timestamp, uint64_t s_num, const State& s_state )\n    : timestamp( s_timestamp ), num( s_num ), state( s_state )\n  {}\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/protobufs/Makefile.am",
    "content": "source = userinput.proto hostinput.proto transportinstruction.proto\n\nAM_CPPFLAGS = $(protobuf_CFLAGS)\nAM_CXXFLAGS = $(WARNING_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS) -Wno-error\n\nSUFFIXES = .proto .pb.cc\n\n.proto.pb.cc:\n\t$(AM_V_GEN)$(PROTOC) --cpp_out=. -I$(srcdir) $<\n\nnoinst_LIBRARIES = libmoshprotos.a\n\nlibmoshprotos_a_SOURCES = $(source)\nnodist_libmoshprotos_a_SOURCES = $(source:.proto=.pb.cc) $(source:.proto=.pb.h)\n\nBUILT_SOURCES = $(source:.proto=.pb.cc)\nCLEANFILES = $(source:.proto=.pb.cc) $(source:.proto=.pb.h)\n"
  },
  {
    "path": "src/protobufs/hostinput.proto",
    "content": "syntax = \"proto2\";\n\noption optimize_for = LITE_RUNTIME;\n\npackage HostBuffers;\n\nmessage HostMessage {\n  repeated Instruction instruction = 1;\n}\n\nmessage Instruction {\n  extensions 2 to max;\n}\n\nmessage HostBytes {\n  optional bytes hoststring = 4;\n}\n\nmessage ResizeMessage {\n  optional int32 width = 5;\n  optional int32 height = 6;\n}\n\nmessage EchoAck {\n  optional uint64 echo_ack_num = 8;\n}\n\nextend Instruction {\n  optional HostBytes hostbytes = 2;\n  optional ResizeMessage resize = 3;\n  optional EchoAck echoack = 7;\n}\n"
  },
  {
    "path": "src/protobufs/transportinstruction.proto",
    "content": "syntax = \"proto2\";\n\noption optimize_for = LITE_RUNTIME;\n\npackage TransportBuffers;\n\nmessage Instruction {\n  optional uint32 protocol_version = 1;\n\n  optional uint64 old_num = 2;\n  optional uint64 new_num = 3;\n  optional uint64 ack_num = 4;\n  optional uint64 throwaway_num = 5;\n\n  optional bytes diff = 6;\n\n  optional bytes chaff = 7;\n}\n"
  },
  {
    "path": "src/protobufs/userinput.proto",
    "content": "syntax = \"proto2\";\n\noption optimize_for = LITE_RUNTIME;\n\npackage ClientBuffers;\n\nmessage UserMessage {\n  repeated Instruction instruction = 1;\n}\n\nmessage Instruction {\n  extensions 2 to max;\n}\n\nmessage Keystroke {\n  optional bytes keys = 4;\n}\n\nmessage ResizeMessage {\n  optional int32 width = 5;\n  optional int32 height = 6;\n}\n\nextend Instruction {\n  optional Keystroke keystroke = 2;\n  optional ResizeMessage resize = 3;\n}\n"
  },
  {
    "path": "src/statesync/Makefile.am",
    "content": "AM_CPPFLAGS = -I$(top_srcdir)/ $(protobuf_CFLAGS)\nAM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\n\nnoinst_LIBRARIES = libmoshstatesync.a\n\nlibmoshstatesync_a_SOURCES = completeterminal.cc completeterminal.h user.cc user.h\n"
  },
  {
    "path": "src/statesync/completeterminal.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <climits>\n\n#include \"src/protobufs/hostinput.pb.h\"\n#include \"src/statesync/completeterminal.h\"\n#include \"src/util/fatal_assert.h\"\n\nusing namespace std;\nusing namespace Parser;\nusing namespace Terminal;\nusing namespace HostBuffers;\n\nstring Complete::act( const string& str )\n{\n  for ( unsigned int i = 0; i < str.size(); i++ ) {\n    /* parse octet into up to three actions */\n    parser.input( str[i], actions );\n\n    /* apply actions to terminal and delete them */\n    for ( Actions::iterator it = actions.begin(); it != actions.end(); it++ ) {\n      Action& act = **it;\n      act.act_on_terminal( &terminal );\n    }\n    actions.clear();\n  }\n\n  return terminal.read_octets_to_host();\n}\n\nstring Complete::act( const Action& act )\n{\n  /* apply action to terminal */\n  act.act_on_terminal( &terminal );\n  return terminal.read_octets_to_host();\n}\n\n/* interface for Network::Transport */\nstring Complete::diff_from( const Complete& existing ) const\n{\n  HostBuffers::HostMessage output;\n\n  if ( existing.get_echo_ack() != get_echo_ack() ) {\n    assert( get_echo_ack() >= existing.get_echo_ack() );\n    Instruction* new_echo = output.add_instruction();\n    new_echo->MutableExtension( echoack )->set_echo_ack_num( get_echo_ack() );\n  }\n\n  if ( !( existing.get_fb() == get_fb() ) ) {\n    if ( ( existing.get_fb().ds.get_width() != terminal.get_fb().ds.get_width() )\n         || ( existing.get_fb().ds.get_height() != terminal.get_fb().ds.get_height() ) ) {\n      Instruction* new_res = output.add_instruction();\n      new_res->MutableExtension( resize )->set_width( terminal.get_fb().ds.get_width() );\n      new_res->MutableExtension( resize )->set_height( terminal.get_fb().ds.get_height() );\n    }\n    string update = display.new_frame( true, existing.get_fb(), terminal.get_fb() );\n    if ( !update.empty() ) {\n      Instruction* new_inst = output.add_instruction();\n      new_inst->MutableExtension( hostbytes )->set_hoststring( update );\n    }\n  }\n\n  return output.SerializeAsString();\n}\n\nstring Complete::init_diff( void ) const\n{\n  return diff_from( Complete( get_fb().ds.get_width(), get_fb().ds.get_height() ) );\n}\n\nvoid Complete::apply_string( const string& diff )\n{\n  HostBuffers::HostMessage input;\n  fatal_assert( input.ParseFromString( diff ) );\n\n  for ( int i = 0; i < input.instruction_size(); i++ ) {\n    if ( input.instruction( i ).HasExtension( hostbytes ) ) {\n      string terminal_to_host = act( input.instruction( i ).GetExtension( hostbytes ).hoststring() );\n      assert( terminal_to_host.empty() ); /* server never interrogates client terminal */\n    } else if ( input.instruction( i ).HasExtension( resize ) ) {\n      act( Resize( input.instruction( i ).GetExtension( resize ).width(),\n                   input.instruction( i ).GetExtension( resize ).height() ) );\n    } else if ( input.instruction( i ).HasExtension( echoack ) ) {\n      uint64_t inst_echo_ack_num = input.instruction( i ).GetExtension( echoack ).echo_ack_num();\n      assert( inst_echo_ack_num >= echo_ack );\n      echo_ack = inst_echo_ack_num;\n    }\n  }\n}\n\nbool Complete::operator==( Complete const& x ) const\n{\n  //  assert( parser == x.parser ); /* parser state is irrelevant for us */\n  return ( terminal == x.terminal ) && ( echo_ack == x.echo_ack );\n}\n\nbool Complete::set_echo_ack( uint64_t now )\n{\n  bool ret = false;\n  uint64_t newest_echo_ack = 0;\n\n  for ( input_history_type::const_iterator i = input_history.begin(); i != input_history.end(); i++ ) {\n    if ( i->second <= now - ECHO_TIMEOUT ) {\n      newest_echo_ack = i->first;\n    }\n  }\n\n  for ( input_history_type::iterator i = input_history.begin(); i != input_history.end(); ) {\n    input_history_type::iterator i_next = i;\n    i_next++;\n    if ( i->first < newest_echo_ack ) {\n      input_history.erase( i );\n    }\n    i = i_next;\n  }\n\n  if ( echo_ack != newest_echo_ack ) {\n    ret = true;\n  }\n\n  echo_ack = newest_echo_ack;\n\n  return ret;\n}\n\nvoid Complete::register_input_frame( uint64_t n, uint64_t now )\n{\n  input_history.push_back( std::make_pair( n, now ) );\n}\n\nint Complete::wait_time( uint64_t now ) const\n{\n  if ( input_history.size() < 2 ) {\n    return INT_MAX;\n  }\n\n  input_history_type::const_iterator it = input_history.begin();\n  it++;\n\n  uint64_t next_echo_ack_time = it->second + ECHO_TIMEOUT;\n  if ( next_echo_ack_time <= now ) {\n    return 0;\n  }\n  return next_echo_ack_time - now;\n}\n\nbool Complete::compare( const Complete& other ) const\n{\n  bool ret = false;\n  const Framebuffer& fb = terminal.get_fb();\n  const Framebuffer& other_fb = other.terminal.get_fb();\n  const int height = fb.ds.get_height();\n  const int other_height = other_fb.ds.get_height();\n  const int width = fb.ds.get_width();\n  const int other_width = other_fb.ds.get_width();\n\n  if ( height != other_height || width != other_width ) {\n    fprintf( stderr, \"Framebuffer size (%dx%d, %dx%d) differs.\\n\", width, height, other_width, other_height );\n    return true;\n  }\n\n  for ( int y = 0; y < height; y++ ) {\n    for ( int x = 0; x < width; x++ ) {\n      if ( fb.get_cell( y, x )->compare( *other_fb.get_cell( y, x ) ) ) {\n        fprintf( stderr, \"Cell (%d, %d) differs.\\n\", y, x );\n        ret = true;\n      }\n    }\n  }\n\n  if ( ( fb.ds.get_cursor_row() != other_fb.ds.get_cursor_row() )\n       || ( fb.ds.get_cursor_col() != other_fb.ds.get_cursor_col() ) ) {\n    fprintf( stderr,\n             \"Cursor mismatch: (%d, %d) vs. (%d, %d).\\n\",\n             fb.ds.get_cursor_row(),\n             fb.ds.get_cursor_col(),\n             other_fb.ds.get_cursor_row(),\n             other_fb.ds.get_cursor_col() );\n    ret = true;\n  }\n  /* XXX should compare other terminal state too (mouse mode, bell. etc.) */\n\n  return ret;\n}\n"
  },
  {
    "path": "src/statesync/completeterminal.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef COMPLETE_TERMINAL_HPP\n#define COMPLETE_TERMINAL_HPP\n\n#include <cstdint>\n#include <list>\n\n#include \"src/terminal/parser.h\"\n#include \"src/terminal/terminal.h\"\n\n/* This class represents the complete terminal -- a UTF8Parser feeding Actions to an Emulator. */\n\nnamespace Terminal {\nclass Complete\n{\nprivate:\n  Parser::UTF8Parser parser;\n  Terminal::Emulator terminal;\n  Terminal::Display display;\n\n  // Only used locally by act(), but kept here as a performance optimization,\n  // to avoid construction/destruction.  It must always be empty\n  // outside calls to act() to keep horrible things from happening.\n  Parser::Actions actions;\n\n  using input_history_type = std::list<std::pair<uint64_t, uint64_t>>;\n  input_history_type input_history;\n  uint64_t echo_ack;\n\n  static const int ECHO_TIMEOUT = 50; /* for late ack */\n\npublic:\n  Complete( size_t width, size_t height )\n    : parser(), terminal( width, height ), display( false ), actions(), input_history(), echo_ack( 0 )\n  {}\n\n  std::string act( const std::string& str );\n  std::string act( const Parser::Action& act );\n\n  const Framebuffer& get_fb( void ) const { return terminal.get_fb(); }\n  void reset_input( void ) { parser.reset_input(); }\n  uint64_t get_echo_ack( void ) const { return echo_ack; }\n  bool set_echo_ack( uint64_t now );\n  void register_input_frame( uint64_t n, uint64_t now );\n  int wait_time( uint64_t now ) const;\n\n  /* interface for Network::Transport */\n  void subtract( const Complete* ) const {}\n  std::string diff_from( const Complete& existing ) const;\n  std::string init_diff( void ) const;\n  void apply_string( const std::string& diff );\n  bool operator==( const Complete& x ) const;\n\n  bool compare( const Complete& other ) const;\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/statesync/user.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n#include <typeinfo>\n\n#include \"src/protobufs/userinput.pb.h\"\n#include \"src/statesync/user.h\"\n#include \"src/util/fatal_assert.h\"\n\nusing namespace Parser;\nusing namespace Network;\nusing namespace ClientBuffers;\n\nvoid UserStream::subtract( const UserStream* prefix )\n{\n  // if we are subtracting ourself from ourself, just clear the std::deque\n  if ( this == prefix ) {\n    actions.clear();\n    return;\n  }\n  for ( std::deque<UserEvent>::const_iterator i = prefix->actions.begin(); i != prefix->actions.end(); i++ ) {\n    assert( this != prefix );\n    assert( !actions.empty() );\n    assert( *i == actions.front() );\n    actions.pop_front();\n  }\n}\n\nstd::string UserStream::diff_from( const UserStream& existing ) const\n{\n  std::deque<UserEvent>::const_iterator my_it = actions.begin();\n\n  for ( std::deque<UserEvent>::const_iterator i = existing.actions.begin(); i != existing.actions.end(); i++ ) {\n    assert( my_it != actions.end() );\n    assert( *i == *my_it );\n    my_it++;\n  }\n\n  ClientBuffers::UserMessage output;\n\n  while ( my_it != actions.end() ) {\n    switch ( my_it->type ) {\n      case UserByteType: {\n        char the_byte = my_it->userbyte.c;\n        /* can we combine this with a previous Keystroke? */\n        if ( ( output.instruction_size() > 0 )\n             && ( output.instruction( output.instruction_size() - 1 ).HasExtension( keystroke ) ) ) {\n          output.mutable_instruction( output.instruction_size() - 1 )\n            ->MutableExtension( keystroke )\n            ->mutable_keys()\n            ->append( std::string( &the_byte, 1 ) );\n        } else {\n          Instruction* new_inst = output.add_instruction();\n          new_inst->MutableExtension( keystroke )->set_keys( &the_byte, 1 );\n        }\n      } break;\n      case ResizeType: {\n        Instruction* new_inst = output.add_instruction();\n        new_inst->MutableExtension( resize )->set_width( my_it->resize.width );\n        new_inst->MutableExtension( resize )->set_height( my_it->resize.height );\n      } break;\n      default:\n        assert( !\"unexpected event type\" );\n        break;\n    }\n\n    my_it++;\n  }\n\n  return output.SerializeAsString();\n}\n\nvoid UserStream::apply_string( const std::string& diff )\n{\n  ClientBuffers::UserMessage input;\n  fatal_assert( input.ParseFromString( diff ) );\n\n  for ( int i = 0; i < input.instruction_size(); i++ ) {\n    if ( input.instruction( i ).HasExtension( keystroke ) ) {\n      std::string the_bytes = input.instruction( i ).GetExtension( keystroke ).keys();\n      for ( unsigned int loc = 0; loc < the_bytes.size(); loc++ ) {\n        actions.push_back( UserEvent( UserByte( the_bytes.at( loc ) ) ) );\n      }\n    } else if ( input.instruction( i ).HasExtension( resize ) ) {\n      actions.push_back( UserEvent( Resize( input.instruction( i ).GetExtension( resize ).width(),\n                                            input.instruction( i ).GetExtension( resize ).height() ) ) );\n    }\n  }\n}\n\nconst Parser::Action& UserStream::get_action( unsigned int i ) const\n{\n  switch ( actions[i].type ) {\n    case UserByteType:\n      return actions[i].userbyte;\n    case ResizeType:\n      return actions[i].resize;\n    default:\n      assert( !\"unexpected action type\" );\n      static const Parser::Ignore nothing = Parser::Ignore();\n      return nothing;\n  }\n}\n"
  },
  {
    "path": "src/statesync/user.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef USER_HPP\n#define USER_HPP\n\n#include <cassert>\n#include <deque>\n#include <list>\n#include <string>\n\n#include \"src/terminal/parseraction.h\"\n\nnamespace Network {\nenum UserEventType\n{\n  UserByteType = 0,\n  ResizeType = 1\n};\n\nclass UserEvent\n{\npublic:\n  UserEventType type;\n  Parser::UserByte userbyte;\n  Parser::Resize resize;\n\n  UserEvent( const Parser::UserByte& s_userbyte ) : type( UserByteType ), userbyte( s_userbyte ), resize( -1, -1 )\n  {}\n  UserEvent( const Parser::Resize& s_resize ) : type( ResizeType ), userbyte( 0 ), resize( s_resize ) {}\n\nprivate:\n  UserEvent();\n\npublic:\n  bool operator==( const UserEvent& x ) const\n  {\n    return ( type == x.type ) && ( userbyte == x.userbyte ) && ( resize == x.resize );\n  }\n};\n\nclass UserStream\n{\nprivate:\n  std::deque<UserEvent> actions;\n\npublic:\n  UserStream() : actions() {}\n\n  void push_back( const Parser::UserByte& s_userbyte ) { actions.push_back( UserEvent( s_userbyte ) ); }\n  void push_back( const Parser::Resize& s_resize ) { actions.push_back( UserEvent( s_resize ) ); }\n\n  bool empty( void ) const { return actions.empty(); }\n  size_t size( void ) const { return actions.size(); }\n  const Parser::Action& get_action( unsigned int i ) const;\n\n  /* interface for Network::Transport */\n  void subtract( const UserStream* prefix );\n  std::string diff_from( const UserStream& existing ) const;\n  std::string init_diff( void ) const { return diff_from( UserStream() ); };\n  void apply_string( const std::string& diff );\n  bool operator==( const UserStream& x ) const { return actions == x.actions; }\n\n  bool compare( const UserStream& ) { return false; }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/Makefile.am",
    "content": "AM_CPPFLAGS = -I$(top_srcdir)/ $(TINFO_CFLAGS)\nAM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\n\nnoinst_LIBRARIES = libmoshterminal.a\n\nlibmoshterminal_a_SOURCES = parseraction.cc parseraction.h parser.cc parser.h parserstate.cc parserstatefamily.h parserstate.h parsertransition.h terminal.cc terminaldispatcher.cc terminaldispatcher.h terminaldisplay.cc terminaldisplayinit.cc terminaldisplay.h terminalframebuffer.cc terminalframebuffer.h terminalfunctions.cc terminal.h terminaluserinput.cc terminaluserinput.h\n"
  },
  {
    "path": "src/terminal/parser.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n#include <cerrno>\n#include <cstdint>\n#include <cwchar>\n#include <typeinfo>\n\n#include \"src/terminal/parser.h\"\n\nconst Parser::StateFamily Parser::family;\n\nstatic void append_or_delete( Parser::ActionPointer act, Parser::Actions& vec )\n{\n  assert( act );\n\n  if ( !act->ignore() ) {\n    vec.push_back( act );\n  }\n}\n\nvoid Parser::Parser::input( wchar_t ch, Actions& ret )\n{\n  Transition tx = state->input( ch );\n\n  if ( tx.next_state != NULL ) {\n    append_or_delete( state->exit(), ret );\n  }\n\n  append_or_delete( tx.action, ret );\n\n  if ( tx.next_state != NULL ) {\n    append_or_delete( tx.next_state->enter(), ret );\n    state = tx.next_state;\n  }\n}\n\nParser::UTF8Parser::UTF8Parser() : parser(), buf_len( 0 )\n{\n  assert( BUF_SIZE >= (size_t)MB_CUR_MAX );\n  buf[0] = '\\0';\n}\n\nvoid Parser::UTF8Parser::input( char c, Actions& ret )\n{\n  assert( buf_len < BUF_SIZE );\n\n  /* 1-byte UTF-8 character, aka ASCII?  Cheat. */\n  if ( buf_len == 0 && static_cast<unsigned char>( c ) <= 0x7f ) {\n    parser.input( static_cast<wchar_t>( c ), ret );\n    return;\n  }\n\n  buf[buf_len++] = c;\n\n  /* This function will only work in a UTF-8 locale. */\n  wchar_t pwc;\n  mbstate_t ps = mbstate_t();\n\n  size_t total_bytes_parsed = 0;\n  size_t orig_buf_len = buf_len;\n\n  /* this routine is somewhat complicated in order to comply with\n     Unicode 6.0, section 3.9, \"Best Practices for using U+FFFD\" */\n\n  while ( total_bytes_parsed != orig_buf_len ) {\n    assert( total_bytes_parsed < orig_buf_len );\n    assert( buf_len > 0 );\n    size_t bytes_parsed = mbrtowc( &pwc, buf, buf_len, &ps );\n\n    /* this returns 0 when n = 0! */\n\n    if ( bytes_parsed == 0 ) {\n      /* character was NUL, accept and clear buffer */\n      assert( buf_len == 1 );\n      buf_len = 0;\n      pwc = L'\\0';\n      bytes_parsed = 1;\n    } else if ( bytes_parsed == (size_t)-1 ) {\n      /* invalid sequence, use replacement character and try again with last char */\n      assert( errno == EILSEQ );\n      if ( buf_len > 1 ) {\n        buf[0] = buf[buf_len - 1];\n        bytes_parsed = buf_len - 1;\n        buf_len = 1;\n      } else {\n        buf_len = 0;\n        bytes_parsed = 1;\n      }\n      pwc = (wchar_t)0xFFFD;\n    } else if ( bytes_parsed == (size_t)-2 ) {\n      /* can't parse incomplete multibyte character */\n      total_bytes_parsed += buf_len;\n      continue;\n    } else {\n      /* parsed into pwc, accept */\n      assert( bytes_parsed <= buf_len );\n      memmove( buf, buf + bytes_parsed, buf_len - bytes_parsed );\n      buf_len = buf_len - bytes_parsed;\n    }\n\n    /* Cast to unsigned for checks, because some\n       platforms (e.g. ARM) use uint32_t as wchar_t,\n       causing compiler warning on \"pwc > 0\" check. */\n    const uint32_t pwcheck = pwc;\n\n    if ( pwcheck > 0x10FFFF ) { /* outside Unicode range */\n      pwc = (wchar_t)0xFFFD;\n    }\n\n    if ( ( pwcheck >= 0xD800 ) && ( pwcheck <= 0xDFFF ) ) { /* surrogate code point */\n      /*\n        OS X unfortunately allows these sequences without EILSEQ, but\n        they are ill-formed UTF-8 and we shouldn't repeat them to the\n        user's terminal.\n      */\n      pwc = (wchar_t)0xFFFD;\n    }\n\n    parser.input( pwc, ret );\n\n    total_bytes_parsed += bytes_parsed;\n  }\n}\n\nParser::Parser::Parser( const Parser& other ) : state( other.state ) {}\n\nParser::Parser& Parser::Parser::operator=( const Parser& other )\n{\n  state = other.state;\n  return *this;\n}\n"
  },
  {
    "path": "src/terminal/parser.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PARSER_HPP\n#define PARSER_HPP\n\n/* Based on Paul Williams's parser,\n   http://www.vt100.net/emu/dec_ansi_parser */\n\n#include <cstring>\n#include <cwchar>\n\n#include \"parserstate.h\"\n#include \"parserstatefamily.h\"\n#include \"parsertransition.h\"\n#include \"src/terminal/parseraction.h\"\n\nnamespace Parser {\nextern const StateFamily family;\n\nclass Parser\n{\nprivate:\n  State const* state;\n\npublic:\n  Parser() : state( &family.s_Ground ) {}\n\n  Parser( const Parser& other );\n  Parser& operator=( const Parser& );\n  ~Parser() {}\n\n  void input( wchar_t ch, Actions& actions );\n\n  void reset_input( void ) { state = &family.s_Ground; }\n};\n\nstatic const size_t BUF_SIZE = 8;\n\nclass UTF8Parser\n{\nprivate:\n  Parser parser;\n\n  char buf[BUF_SIZE];\n  size_t buf_len;\n\npublic:\n  UTF8Parser();\n\n  void input( char c, Actions& actions );\n\n  void reset_input( void )\n  {\n    parser.reset_input();\n    buf[0] = '\\0';\n    buf_len = 0;\n  }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/parseraction.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n#include <cwctype>\n\n#include \"src/terminal/parseraction.h\"\n#include \"src/terminal/terminal.h\"\n\nusing namespace Parser;\n\nvoid Print::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->print( this );\n}\n\nvoid Execute::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->execute( this );\n}\n\nvoid Clear::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->dispatch.clear( this );\n}\n\nvoid Param::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->dispatch.newparamchar( this );\n}\n\nvoid Collect::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->dispatch.collect( this );\n}\n\nvoid CSI_Dispatch::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->CSI_dispatch( this );\n}\n\nvoid Esc_Dispatch::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->Esc_dispatch( this );\n}\n\nvoid OSC_Put::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->dispatch.OSC_put( this );\n}\n\nvoid OSC_Start::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->dispatch.OSC_start( this );\n}\n\nvoid OSC_End::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->OSC_end( this );\n}\n\nvoid UserByte::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->dispatch.terminal_to_host.append( emu->user.input( this, emu->fb.ds.application_mode_cursor_keys ) );\n}\n\nvoid Resize::act_on_terminal( Terminal::Emulator* emu ) const\n{\n  emu->resize( width, height );\n}\n"
  },
  {
    "path": "src/terminal/parseraction.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PARSERACTION_HPP\n#define PARSERACTION_HPP\n\n#include <memory>\n#include <string>\n#include <vector>\n\nnamespace Terminal {\nclass Emulator;\n}\n\nnamespace Parser {\nclass Action\n{\npublic:\n  wchar_t ch;\n  bool char_present;\n\n  virtual std::string name( void ) = 0;\n\n  virtual void act_on_terminal( Terminal::Emulator* ) const {};\n\n  virtual bool ignore() const { return false; }\n\n  Action() : ch( -1 ), char_present( false ) {};\n  virtual ~Action() {};\n};\n\nusing ActionPointer = std::shared_ptr<Action>;\nusing Actions = std::vector<ActionPointer>;\n\nclass Ignore : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Ignore\" ); }\n  bool ignore() const { return true; }\n};\nclass Print : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Print\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass Execute : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Execute\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass Clear : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Clear\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass Collect : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Collect\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass Param : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Param\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass Esc_Dispatch : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Esc_Dispatch\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass CSI_Dispatch : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"CSI_Dispatch\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass Hook : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Hook\" ); }\n};\nclass Put : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Put\" ); }\n};\nclass Unhook : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"Unhook\" ); }\n};\nclass OSC_Start : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"OSC_Start\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass OSC_Put : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"OSC_Put\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\nclass OSC_End : public Action\n{\npublic:\n  std::string name( void ) { return std::string( \"OSC_End\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n};\n\nclass UserByte : public Action\n{\n  /* user keystroke -- not part of the host-source state machine*/\npublic:\n  char c; /* The user-source byte. We don't try to interpret the charset */\n\n  std::string name( void ) { return std::string( \"UserByte\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n\n  UserByte( int s_c ) : c( s_c ) {}\n\n  bool operator==( const UserByte& other ) const { return c == other.c; }\n};\n\nclass Resize : public Action\n{\n  /* resize event -- not part of the host-source state machine*/\npublic:\n  size_t width, height;\n\n  std::string name( void ) { return std::string( \"Resize\" ); }\n  void act_on_terminal( Terminal::Emulator* emu ) const;\n\n  Resize( size_t s_width, size_t s_height ) : width( s_width ), height( s_height ) {}\n\n  bool operator==( const Resize& other ) const { return ( width == other.width ) && ( height == other.height ); }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/parserstate.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <memory>\n\n#include \"parserstate.h\"\n#include \"parserstatefamily.h\"\n\nusing namespace Parser;\n\nTransition State::anywhere_rule( wchar_t ch ) const\n{\n  if ( ( ch == 0x18 ) || ( ch == 0x1A ) || ( ( 0x80 <= ch ) && ( ch <= 0x8F ) )\n       || ( ( 0x91 <= ch ) && ( ch <= 0x97 ) ) || ( ch == 0x99 ) || ( ch == 0x9A ) ) {\n    return Transition( std::make_shared<Execute>(), &family->s_Ground );\n  } else if ( ch == 0x9C ) {\n    return Transition( &family->s_Ground );\n  } else if ( ch == 0x1B ) {\n    return Transition( &family->s_Escape );\n  } else if ( ( ch == 0x98 ) || ( ch == 0x9E ) || ( ch == 0x9F ) ) {\n    return Transition( &family->s_SOS_PM_APC_String );\n  } else if ( ch == 0x90 ) {\n    return Transition( &family->s_DCS_Entry );\n  } else if ( ch == 0x9D ) {\n    return Transition( &family->s_OSC_String );\n  } else if ( ch == 0x9B ) {\n    return Transition( &family->s_CSI_Entry );\n  }\n\n  return Transition( (State*)NULL, ActionPointer() ); /* don't allocate an Ignore action */\n}\n\nTransition State::input( wchar_t ch ) const\n{\n  /* Check for immediate transitions. */\n  Transition anywhere = anywhere_rule( ch );\n  if ( anywhere.next_state ) {\n    anywhere.action->char_present = true;\n    anywhere.action->ch = ch;\n    return anywhere;\n  }\n  /* Normal X.364 state machine. */\n  /* Parse high Unicode codepoints like 'A'. */\n  Transition ret = this->input_state_rule( ch >= 0xA0 ? 0x41 : ch );\n  ret.action->char_present = true;\n  ret.action->ch = ch;\n  return ret;\n}\n\nstatic bool C0_prime( wchar_t ch )\n{\n  return ( ch <= 0x17 ) || ( ch == 0x19 ) || ( ( 0x1C <= ch ) && ( ch <= 0x1F ) );\n}\n\nstatic bool GLGR( wchar_t ch )\n{\n  return ( ( 0x20 <= ch ) && ( ch <= 0x7F ) )     /* GL area */\n         || ( ( 0xA0 <= ch ) && ( ch <= 0xFF ) ); /* GR area */\n}\n\nTransition Ground::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( GLGR( ch ) ) {\n    return Transition( std::make_shared<Print>() );\n  }\n\n  return Transition();\n}\n\nActionPointer Escape::enter( void ) const\n{\n  return std::make_shared<Clear>();\n}\n\nTransition Escape::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_Escape_Intermediate );\n  }\n\n  if ( ( ( 0x30 <= ch ) && ( ch <= 0x4F ) ) || ( ( 0x51 <= ch ) && ( ch <= 0x57 ) ) || ( ch == 0x59 )\n       || ( ch == 0x5A ) || ( ch == 0x5C ) || ( ( 0x60 <= ch ) && ( ch <= 0x7E ) ) ) {\n    return Transition( std::make_shared<Esc_Dispatch>(), &family->s_Ground );\n  }\n\n  if ( ch == 0x5B ) {\n    return Transition( &family->s_CSI_Entry );\n  }\n\n  if ( ch == 0x5D ) {\n    return Transition( &family->s_OSC_String );\n  }\n\n  if ( ch == 0x50 ) {\n    return Transition( &family->s_DCS_Entry );\n  }\n\n  if ( ( ch == 0x58 ) || ( ch == 0x5E ) || ( ch == 0x5F ) ) {\n    return Transition( &family->s_SOS_PM_APC_String );\n  }\n\n  return Transition();\n}\n\nTransition Escape_Intermediate::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>() );\n  }\n\n  if ( ( 0x30 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( std::make_shared<Esc_Dispatch>(), &family->s_Ground );\n  }\n\n  return Transition();\n}\n\nActionPointer CSI_Entry::enter( void ) const\n{\n  return std::make_shared<Clear>();\n}\n\nTransition CSI_Entry::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( std::make_shared<CSI_Dispatch>(), &family->s_Ground );\n  }\n\n  if ( ( ( 0x30 <= ch ) && ( ch <= 0x39 ) ) || ( ch == 0x3B ) ) {\n    return Transition( std::make_shared<Param>(), &family->s_CSI_Param );\n  }\n\n  if ( ( 0x3C <= ch ) && ( ch <= 0x3F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_CSI_Param );\n  }\n\n  if ( ch == 0x3A ) {\n    return Transition( &family->s_CSI_Ignore );\n  }\n\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_CSI_Intermediate );\n  }\n\n  return Transition();\n}\n\nTransition CSI_Param::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( ( ( 0x30 <= ch ) && ( ch <= 0x39 ) ) || ( ch == 0x3B ) ) {\n    return Transition( std::make_shared<Param>() );\n  }\n\n  if ( ( ch == 0x3A ) || ( ( 0x3C <= ch ) && ( ch <= 0x3F ) ) ) {\n    return Transition( &family->s_CSI_Ignore );\n  }\n\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_CSI_Intermediate );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( std::make_shared<CSI_Dispatch>(), &family->s_Ground );\n  }\n\n  return Transition();\n}\n\nTransition CSI_Intermediate::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>() );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( std::make_shared<CSI_Dispatch>(), &family->s_Ground );\n  }\n\n  if ( ( 0x30 <= ch ) && ( ch <= 0x3F ) ) {\n    return Transition( &family->s_CSI_Ignore );\n  }\n\n  return Transition();\n}\n\nTransition CSI_Ignore::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) ) {\n    return Transition( std::make_shared<Execute>() );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( &family->s_Ground );\n  }\n\n  return Transition();\n}\n\nActionPointer DCS_Entry::enter( void ) const\n{\n  return std::make_shared<Clear>();\n}\n\nTransition DCS_Entry::input_state_rule( wchar_t ch ) const\n{\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_DCS_Intermediate );\n  }\n\n  if ( ch == 0x3A ) {\n    return Transition( &family->s_DCS_Ignore );\n  }\n\n  if ( ( ( 0x30 <= ch ) && ( ch <= 0x39 ) ) || ( ch == 0x3B ) ) {\n    return Transition( std::make_shared<Param>(), &family->s_DCS_Param );\n  }\n\n  if ( ( 0x3C <= ch ) && ( ch <= 0x3F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_DCS_Param );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( &family->s_DCS_Passthrough );\n  }\n\n  return Transition();\n}\n\nTransition DCS_Param::input_state_rule( wchar_t ch ) const\n{\n  if ( ( ( 0x30 <= ch ) && ( ch <= 0x39 ) ) || ( ch == 0x3B ) ) {\n    return Transition( std::make_shared<Param>() );\n  }\n\n  if ( ( ch == 0x3A ) || ( ( 0x3C <= ch ) && ( ch <= 0x3F ) ) ) {\n    return Transition( &family->s_DCS_Ignore );\n  }\n\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>(), &family->s_DCS_Intermediate );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( &family->s_DCS_Passthrough );\n  }\n\n  return Transition();\n}\n\nTransition DCS_Intermediate::input_state_rule( wchar_t ch ) const\n{\n  if ( ( 0x20 <= ch ) && ( ch <= 0x2F ) ) {\n    return Transition( std::make_shared<Collect>() );\n  }\n\n  if ( ( 0x40 <= ch ) && ( ch <= 0x7E ) ) {\n    return Transition( &family->s_DCS_Passthrough );\n  }\n\n  if ( ( 0x30 <= ch ) && ( ch <= 0x3F ) ) {\n    return Transition( &family->s_DCS_Ignore );\n  }\n\n  return Transition();\n}\n\nActionPointer DCS_Passthrough::enter( void ) const\n{\n  return std::make_shared<Hook>();\n}\n\nActionPointer DCS_Passthrough::exit( void ) const\n{\n  return std::make_shared<Unhook>();\n}\n\nTransition DCS_Passthrough::input_state_rule( wchar_t ch ) const\n{\n  if ( C0_prime( ch ) || ( ( 0x20 <= ch ) && ( ch <= 0x7E ) ) ) {\n    return Transition( std::make_shared<Put>() );\n  }\n\n  if ( ch == 0x9C ) {\n    return Transition( &family->s_Ground );\n  }\n\n  return Transition();\n}\n\nTransition DCS_Ignore::input_state_rule( wchar_t ch ) const\n{\n  if ( ch == 0x9C ) {\n    return Transition( &family->s_Ground );\n  }\n\n  return Transition();\n}\n\nActionPointer OSC_String::enter( void ) const\n{\n  return std::make_shared<OSC_Start>();\n}\n\nActionPointer OSC_String::exit( void ) const\n{\n  return std::make_shared<OSC_End>();\n}\n\nTransition OSC_String::input_state_rule( wchar_t ch ) const\n{\n  if ( ( 0x20 <= ch ) && ( ch <= 0x7F ) ) {\n    return Transition( std::make_shared<OSC_Put>() );\n  }\n\n  if ( ( ch == 0x9C ) || ( ch == 0x07 ) ) { /* 0x07 is xterm non-ANSI variant */\n    return Transition( &family->s_Ground );\n  }\n\n  return Transition();\n}\n\nTransition SOS_PM_APC_String::input_state_rule( wchar_t ch ) const\n{\n  if ( ch == 0x9C ) {\n    return Transition( &family->s_Ground );\n  }\n\n  return Transition();\n}\n"
  },
  {
    "path": "src/terminal/parserstate.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PARSERSTATE_HPP\n#define PARSERSTATE_HPP\n\n#include \"parsertransition.h\"\n\nnamespace Parser {\nclass StateFamily;\n\nclass State\n{\nprotected:\n  virtual Transition input_state_rule( wchar_t ch ) const = 0;\n  StateFamily* family;\n\nprivate:\n  Transition anywhere_rule( wchar_t ch ) const;\n\npublic:\n  void setfamily( StateFamily* s_family ) { family = s_family; }\n  Transition input( wchar_t ch ) const;\n  virtual ActionPointer enter( void ) const { return std::make_shared<Ignore>(); }\n  virtual ActionPointer exit( void ) const { return std::make_shared<Ignore>(); }\n\n  State() : family( NULL ) {};\n  virtual ~State() {};\n\n  State( const State& );\n  State& operator=( const State& );\n};\n\nclass Ground : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\n\nclass Escape : public State\n{\n  ActionPointer enter( void ) const;\n  Transition input_state_rule( wchar_t ch ) const;\n};\n\nclass Escape_Intermediate : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\n\nclass CSI_Entry : public State\n{\n  ActionPointer enter( void ) const;\n  Transition input_state_rule( wchar_t ch ) const;\n};\nclass CSI_Param : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\nclass CSI_Intermediate : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\nclass CSI_Ignore : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\n\nclass DCS_Entry : public State\n{\n  ActionPointer enter( void ) const;\n  Transition input_state_rule( wchar_t ch ) const;\n};\nclass DCS_Param : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\nclass DCS_Intermediate : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\nclass DCS_Passthrough : public State\n{\n  ActionPointer enter( void ) const;\n  Transition input_state_rule( wchar_t ch ) const;\n  ActionPointer exit( void ) const;\n};\nclass DCS_Ignore : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\n\nclass OSC_String : public State\n{\n  ActionPointer enter( void ) const;\n  Transition input_state_rule( wchar_t ch ) const;\n  ActionPointer exit( void ) const;\n};\nclass SOS_PM_APC_String : public State\n{\n  Transition input_state_rule( wchar_t ch ) const;\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/parserstatefamily.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PARSERSTATEFAMILY_HPP\n#define PARSERSTATEFAMILY_HPP\n\n#include \"parserstate.h\"\n\nnamespace Parser {\nclass StateFamily\n{\npublic:\n  Ground s_Ground;\n\n  Escape s_Escape;\n  Escape_Intermediate s_Escape_Intermediate;\n\n  CSI_Entry s_CSI_Entry;\n  CSI_Param s_CSI_Param;\n  CSI_Intermediate s_CSI_Intermediate;\n  CSI_Ignore s_CSI_Ignore;\n\n  DCS_Entry s_DCS_Entry;\n  DCS_Param s_DCS_Param;\n  DCS_Intermediate s_DCS_Intermediate;\n  DCS_Passthrough s_DCS_Passthrough;\n  DCS_Ignore s_DCS_Ignore;\n\n  OSC_String s_OSC_String;\n  SOS_PM_APC_String s_SOS_PM_APC_String;\n\n  StateFamily()\n    : s_Ground(), s_Escape(), s_Escape_Intermediate(), s_CSI_Entry(), s_CSI_Param(), s_CSI_Intermediate(),\n      s_CSI_Ignore(), s_DCS_Entry(), s_DCS_Param(), s_DCS_Intermediate(), s_DCS_Passthrough(), s_DCS_Ignore(),\n      s_OSC_String(), s_SOS_PM_APC_String()\n  {\n    s_Ground.setfamily( this );\n    s_Escape.setfamily( this );\n    s_Escape_Intermediate.setfamily( this );\n    s_CSI_Entry.setfamily( this );\n    s_CSI_Param.setfamily( this );\n    s_CSI_Intermediate.setfamily( this );\n    s_CSI_Ignore.setfamily( this );\n    s_DCS_Entry.setfamily( this );\n    s_DCS_Param.setfamily( this );\n    s_DCS_Intermediate.setfamily( this );\n    s_DCS_Passthrough.setfamily( this );\n    s_DCS_Ignore.setfamily( this );\n    s_OSC_String.setfamily( this );\n    s_SOS_PM_APC_String.setfamily( this );\n  }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/parsertransition.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PARSERTRANSITION_HPP\n#define PARSERTRANSITION_HPP\n\n#include <cstdlib>\n\n#include \"src/terminal/parseraction.h\"\n\nnamespace Parser {\nclass State;\n\nclass Transition\n{\npublic:\n  // Transition is only a courier for an Action; it should\n  // never create/delete one on its own.\n  ActionPointer action;\n  State* next_state;\n\n  Transition( const Transition& x ) : action( x.action ), next_state( x.next_state ) {}\n  Transition& operator=( const Transition& t )\n  {\n    action = t.action;\n    next_state = t.next_state;\n\n    return *this;\n  }\n  Transition( ActionPointer s_action = std::make_shared<Ignore>(), State* s_next_state = NULL )\n    : action( s_action ), next_state( s_next_state )\n  {}\n\n  // This is only ever used in the 1-argument form;\n  // we use this instead of an initializer to\n  // tell Coverity the object never owns *action.\n  Transition( State* s_next_state, ActionPointer s_action = std::make_shared<Ignore>() )\n    : action( s_action ), next_state( s_next_state )\n  {}\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/terminal.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n#include <cstdlib>\n#include <cstring>\n#include <typeinfo>\n\n#include <unistd.h>\n\n#include \"src/terminal/terminal.h\"\n\nusing namespace Terminal;\n\nEmulator::Emulator( size_t s_width, size_t s_height ) : fb( s_width, s_height ), dispatch(), user() {}\n\nstd::string Emulator::read_octets_to_host( void )\n{\n  std::string ret = dispatch.terminal_to_host;\n  dispatch.terminal_to_host.clear();\n  return ret;\n}\n\nvoid Emulator::execute( const Parser::Execute* act )\n{\n  dispatch.dispatch( CONTROL, act, &fb );\n}\n\nvoid Emulator::print( const Parser::Print* act )\n{\n  assert( act->char_present );\n\n  const wchar_t ch = act->ch;\n\n  /*\n   * Check for printing ISO 8859-1 first, it's a cheap way to detect\n   * some common narrow characters.\n   */\n  const int chwidth = ch == L'\\0' ? -1 : ( Cell::isprint_iso8859_1( ch ) ? 1 : wcwidth( ch ) );\n\n  Cell* this_cell = fb.get_mutable_cell();\n\n  switch ( chwidth ) {\n    case 1: /* normal character */\n    case 2: /* wide character */\n      if ( fb.ds.auto_wrap_mode && fb.ds.next_print_will_wrap ) {\n        fb.get_mutable_row( -1 )->set_wrap( true );\n        fb.ds.move_col( 0 );\n        fb.move_rows_autoscroll( 1 );\n        this_cell = NULL;\n      } else if ( fb.ds.auto_wrap_mode && ( chwidth == 2 )\n                  && ( fb.ds.get_cursor_col() == fb.ds.get_width() - 1 ) ) {\n        /* wrap 2-cell chars if no room, even without will-wrap flag */\n        fb.reset_cell( this_cell );\n        fb.get_mutable_row( -1 )->set_wrap( false );\n        /* There doesn't seem to be a consistent way to get the\n           downstream terminal emulator to set the wrap-around\n           copy-and-paste flag on a row that ends with an empty cell\n           because a wide char was wrapped to the next line. */\n        fb.ds.move_col( 0 );\n        fb.move_rows_autoscroll( 1 );\n        this_cell = NULL;\n      }\n\n      if ( fb.ds.insert_mode ) {\n        for ( int i = 0; i < chwidth; i++ ) {\n          fb.insert_cell( fb.ds.get_cursor_row(), fb.ds.get_cursor_col() );\n        }\n        this_cell = NULL;\n      }\n\n      if ( !this_cell ) {\n        this_cell = fb.get_mutable_cell();\n      }\n\n      fb.reset_cell( this_cell );\n      this_cell->append( ch );\n      this_cell->set_wide( chwidth == 2 ); /* chwidth had better be 1 or 2 here */\n      fb.apply_renditions_to_cell( this_cell );\n\n      if ( chwidth == 2 && fb.ds.get_cursor_col() + 1 < fb.ds.get_width() ) { /* erase overlapped cell */\n        fb.reset_cell( fb.get_mutable_cell( fb.ds.get_cursor_row(), fb.ds.get_cursor_col() + 1 ) );\n      }\n\n      fb.ds.move_col( chwidth, true, true );\n\n      break;\n    case 0: /* combining character */\n    {\n      Cell* combining_cell = fb.get_combining_cell(); /* can be null if we were resized */\n      if ( combining_cell == NULL ) {                 /* character is now offscreen */\n        break;\n      }\n\n      if ( combining_cell->empty() ) {\n        /* cell starts with combining character */\n        /* ... but isn't necessarily the target for a new\n           base character [e.g. start of line], if the\n           combining character has been cleared with\n           a sequence like ED (\"J\") or EL (\"K\") */\n        assert( !combining_cell->get_wide() );\n        combining_cell->set_fallback( true );\n        fb.ds.move_col( 1, true, true );\n      }\n      if ( !combining_cell->full() ) {\n        combining_cell->append( ch );\n      }\n    } break;\n    case -1: /* unprintable character */\n      break;\n    default:\n      assert( !\"unexpected character width from wcwidth()\" );\n      break;\n  }\n}\n\nvoid Emulator::CSI_dispatch( const Parser::CSI_Dispatch* act )\n{\n  dispatch.dispatch( CSI, act, &fb );\n}\n\nvoid Emulator::OSC_end( const Parser::OSC_End* act )\n{\n  dispatch.OSC_dispatch( act, &fb );\n}\n\nvoid Emulator::Esc_dispatch( const Parser::Esc_Dispatch* act )\n{\n  /* handle 7-bit ESC-encoding of C1 control characters */\n  if ( ( dispatch.get_dispatch_chars().size() == 0 ) && ( 0x40 <= act->ch ) && ( act->ch <= 0x5F ) ) {\n    Parser::Esc_Dispatch act2 = *act;\n    act2.ch += 0x40;\n    dispatch.dispatch( CONTROL, &act2, &fb );\n  } else {\n    dispatch.dispatch( ESCAPE, act, &fb );\n  }\n}\n\nvoid Emulator::resize( size_t s_width, size_t s_height )\n{\n  fb.resize( s_width, s_height );\n}\n\nbool Emulator::operator==( Emulator const& x ) const\n{\n  /* dispatcher and user are irrelevant for us */\n  return fb == x.fb;\n}\n"
  },
  {
    "path": "src/terminal/terminal.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TERMINAL_CPP\n#define TERMINAL_CPP\n\n#include <cstdio>\n#include <cwchar>\n#include <deque>\n#include <vector>\n\n#include \"src/terminal/parseraction.h\"\n#include \"src/terminal/terminalframebuffer.h\"\n#include \"terminaldispatcher.h\"\n#include \"terminaldisplay.h\"\n#include \"terminaluserinput.h\"\n\nnamespace Terminal {\nclass Emulator\n{\n  friend void Parser::Print::act_on_terminal( Emulator* ) const;\n  friend void Parser::Execute::act_on_terminal( Emulator* ) const;\n  friend void Parser::Clear::act_on_terminal( Emulator* ) const;\n  friend void Parser::Param::act_on_terminal( Emulator* ) const;\n  friend void Parser::Collect::act_on_terminal( Emulator* ) const;\n  friend void Parser::CSI_Dispatch::act_on_terminal( Emulator* ) const;\n  friend void Parser::Esc_Dispatch::act_on_terminal( Emulator* ) const;\n  friend void Parser::OSC_Start::act_on_terminal( Emulator* ) const;\n  friend void Parser::OSC_Put::act_on_terminal( Emulator* ) const;\n  friend void Parser::OSC_End::act_on_terminal( Emulator* ) const;\n\n  friend void Parser::UserByte::act_on_terminal( Emulator* ) const;\n  friend void Parser::Resize::act_on_terminal( Emulator* ) const;\n\nprivate:\n  Framebuffer fb;\n  Dispatcher dispatch;\n  UserInput user;\n\n  /* action methods */\n  void print( const Parser::Print* act );\n  void execute( const Parser::Execute* act );\n  void CSI_dispatch( const Parser::CSI_Dispatch* act );\n  void Esc_dispatch( const Parser::Esc_Dispatch* act );\n  void OSC_end( const Parser::OSC_End* act );\n  void resize( size_t s_width, size_t s_height );\n\npublic:\n  Emulator( size_t s_width, size_t s_height );\n\n  std::string read_octets_to_host( void );\n\n  const Framebuffer& get_fb( void ) const { return fb; }\n\n  bool operator==( Emulator const& x ) const;\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/terminaldispatcher.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n#include <cerrno>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n\n#include \"src/terminal/parseraction.h\"\n#include \"src/terminal/terminalframebuffer.h\"\n#include \"terminaldispatcher.h\"\n\nusing namespace Terminal;\n\nstatic const size_t MAXIMUM_CLIPBOARD_SIZE = 16 * 1024;\n\nDispatcher::Dispatcher()\n  : params(), parsed_params(), parsed( false ), dispatch_chars(), OSC_string(), terminal_to_host()\n{}\n\nvoid Dispatcher::newparamchar( const Parser::Param* act )\n{\n  assert( act->char_present );\n  assert( ( act->ch == ';' ) || ( ( act->ch >= '0' ) && ( act->ch <= '9' ) ) );\n  if ( params.length() < 100 ) {\n    /* enough for 16 five-char params plus 15 semicolons */\n    params.push_back( act->ch );\n  }\n  parsed = false;\n}\n\nvoid Dispatcher::collect( const Parser::Collect* act )\n{\n  assert( act->char_present );\n  if ( ( dispatch_chars.length() < 8 ) /* never should need more than 2 */\n       && ( act->ch <= 255 ) ) {       /* ignore non-8-bit */\n    dispatch_chars.push_back( act->ch );\n  }\n}\n\nvoid Dispatcher::clear( const Parser::Clear* act __attribute( ( unused ) ) )\n{\n  params.clear();\n  dispatch_chars.clear();\n  parsed = false;\n}\n\nvoid Dispatcher::parse_params( void )\n{\n  if ( parsed ) {\n    return;\n  }\n\n  parsed_params.clear();\n  const char* str = params.c_str();\n  const char* segment_begin = str;\n\n  while ( 1 ) {\n    const char* segment_end = strchr( segment_begin, ';' );\n    if ( segment_end == NULL ) {\n      break;\n    }\n\n    errno = 0;\n    char* endptr;\n    long val = strtol( segment_begin, &endptr, 10 );\n    if ( endptr == segment_begin ) {\n      val = -1;\n    }\n\n    if ( val > PARAM_MAX || errno == ERANGE ) {\n      val = -1;\n      errno = 0;\n    }\n\n    if ( errno == 0 || segment_begin == endptr ) {\n      parsed_params.push_back( val );\n    }\n\n    segment_begin = segment_end + 1;\n  }\n\n  /* get last param */\n  errno = 0;\n  char* endptr;\n  long val = strtol( segment_begin, &endptr, 10 );\n  if ( endptr == segment_begin ) {\n    val = -1;\n  }\n\n  if ( val > PARAM_MAX || errno == ERANGE ) {\n    val = -1;\n    errno = 0;\n  }\n\n  if ( errno == 0 || segment_begin == endptr ) {\n    parsed_params.push_back( val );\n  }\n\n  parsed = true;\n}\n\nint Dispatcher::getparam( size_t N, int defaultval )\n{\n  int ret = defaultval;\n  if ( !parsed ) {\n    parse_params();\n  }\n\n  if ( parsed_params.size() > N ) {\n    ret = parsed_params[N];\n  }\n\n  if ( ret < 1 )\n    ret = defaultval;\n\n  return ret;\n}\n\nint Dispatcher::param_count( void )\n{\n  if ( !parsed ) {\n    parse_params();\n  }\n\n  return parsed_params.size();\n}\n\nstd::string Dispatcher::str( void )\n{\n  char assum[64];\n  snprintf( assum, 64, \"[dispatch=\\\"%s\\\" params=\\\"%s\\\"]\", dispatch_chars.c_str(), params.c_str() );\n  return std::string( assum );\n}\n\n/* construct on first use to avoid static initialization order crash */\nDispatchRegistry& Terminal::get_global_dispatch_registry( void )\n{\n  static DispatchRegistry global_dispatch_registry;\n  return global_dispatch_registry;\n}\n\nstatic void register_function( Function_Type type, const std::string& dispatch_chars, Function f )\n{\n  switch ( type ) {\n    case ESCAPE:\n      get_global_dispatch_registry().escape.insert( dispatch_map_t::value_type( dispatch_chars, f ) );\n      break;\n    case CSI:\n      get_global_dispatch_registry().CSI.insert( dispatch_map_t::value_type( dispatch_chars, f ) );\n      break;\n    case CONTROL:\n      get_global_dispatch_registry().control.insert( dispatch_map_t::value_type( dispatch_chars, f ) );\n      break;\n  }\n}\n\nFunction::Function( Function_Type type,\n                    const std::string& dispatch_chars,\n                    void ( *s_function )( Framebuffer*, Dispatcher* ),\n                    bool s_clears_wrap_state )\n  : function( s_function ), clears_wrap_state( s_clears_wrap_state )\n{\n  register_function( type, dispatch_chars, *this );\n}\n\nvoid Dispatcher::dispatch( Function_Type type, const Parser::Action* act, Framebuffer* fb )\n{\n  /* add final char to dispatch key */\n  if ( ( type == ESCAPE ) || ( type == CSI ) ) {\n    assert( act->char_present );\n    Parser::Collect act2;\n    act2.char_present = true;\n    act2.ch = act->ch;\n    collect( &act2 );\n  }\n\n  dispatch_map_t* map = NULL;\n  switch ( type ) {\n    case ESCAPE:\n      map = &get_global_dispatch_registry().escape;\n      break;\n    case CSI:\n      map = &get_global_dispatch_registry().CSI;\n      break;\n    case CONTROL:\n      map = &get_global_dispatch_registry().control;\n      break;\n  }\n\n  std::string key = dispatch_chars;\n  if ( type == CONTROL ) {\n    assert( act->ch <= 255 );\n    char ctrlstr[2] = { (char)act->ch, 0 };\n    key = std::string( ctrlstr, 1 );\n  }\n\n  dispatch_map_t::const_iterator i = map->find( key );\n  if ( i == map->end() ) {\n    /* unknown function */\n    fb->ds.next_print_will_wrap = false;\n    return;\n  }\n  if ( i->second.clears_wrap_state ) {\n    fb->ds.next_print_will_wrap = false;\n  }\n  i->second.function( fb, this );\n}\n\nvoid Dispatcher::OSC_put( const Parser::OSC_Put* act )\n{\n  assert( act->char_present );\n  if ( OSC_string.size() < MAXIMUM_CLIPBOARD_SIZE ) {\n    OSC_string.push_back( act->ch );\n  }\n}\n\nvoid Dispatcher::OSC_start( const Parser::OSC_Start* act __attribute( ( unused ) ) )\n{\n  OSC_string.clear();\n}\n\nbool Dispatcher::operator==( const Dispatcher& x ) const\n{\n  return ( params == x.params ) && ( parsed_params == x.parsed_params ) && ( parsed == x.parsed )\n         && ( dispatch_chars == x.dispatch_chars ) && ( OSC_string == x.OSC_string )\n         && ( terminal_to_host == x.terminal_to_host );\n}\n"
  },
  {
    "path": "src/terminal/terminaldispatcher.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TERMINALDISPATCHER_HPP\n#define TERMINALDISPATCHER_HPP\n\n#include <map>\n#include <string>\n#include <vector>\n\nnamespace Parser {\nclass Action;\nclass Param;\nclass Collect;\nclass Clear;\nclass Esc_Dispatch;\nclass CSI_Dispatch;\nclass Execute;\nclass OSC_Start;\nclass OSC_Put;\nclass OSC_End;\n}\n\nnamespace Terminal {\nclass Framebuffer;\nclass Dispatcher;\n\nenum Function_Type\n{\n  ESCAPE,\n  CSI,\n  CONTROL\n};\n\nclass Function\n{\npublic:\n  Function() : function( NULL ), clears_wrap_state( true ) {}\n  Function( Function_Type type,\n            const std::string& dispatch_chars,\n            void ( *s_function )( Framebuffer*, Dispatcher* ),\n            bool s_clears_wrap_state = true );\n  void ( *function )( Framebuffer*, Dispatcher* );\n  bool clears_wrap_state;\n};\n\nusing dispatch_map_t = std::map<std::string, Function>;\n\nclass DispatchRegistry\n{\npublic:\n  dispatch_map_t escape;\n  dispatch_map_t CSI;\n  dispatch_map_t control;\n\n  DispatchRegistry() : escape(), CSI(), control() {}\n};\n\nDispatchRegistry& get_global_dispatch_registry( void );\n\nclass Dispatcher\n{\nprivate:\n  std::string params;\n  std::vector<int> parsed_params;\n  bool parsed;\n\n  std::string dispatch_chars;\n  std::vector<wchar_t> OSC_string; /* only used to set the window title */\n\n  void parse_params( void );\n\npublic:\n  static const int PARAM_MAX = 65535;\n  /* prevent evil escape sequences from causing long loops */\n\n  std::string terminal_to_host; /* this is the reply string */\n\n  Dispatcher();\n  int getparam( size_t N, int defaultval );\n  int param_count( void );\n\n  void newparamchar( const Parser::Param* act );\n  void collect( const Parser::Collect* act );\n  void clear( const Parser::Clear* act );\n\n  std::string str( void );\n\n  void dispatch( Function_Type type, const Parser::Action* act, Framebuffer* fb );\n  std::string get_dispatch_chars( void ) const { return dispatch_chars; }\n  std::vector<wchar_t> get_OSC_string( void ) const { return OSC_string; }\n\n  void OSC_put( const Parser::OSC_Put* act );\n  void OSC_start( const Parser::OSC_Start* act );\n  void OSC_dispatch( const Parser::OSC_End* act, Framebuffer* fb );\n\n  bool operator==( const Dispatcher& x ) const;\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/terminaldisplay.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n\n#include \"src/terminal/terminalframebuffer.h\"\n#include \"terminaldisplay.h\"\n\nusing namespace Terminal;\n\n/* Print a new \"frame\" to the terminal, using ANSI/ECMA-48 escape codes. */\n\nstatic const Renditions& initial_rendition( void )\n{\n  const static Renditions blank = Renditions( 0 );\n  return blank;\n}\n\nstd::string Display::open() const\n{\n  return std::string( smcup ? smcup : \"\" ) + std::string( \"\\033[?1h\" );\n}\n\nstd::string Display::close() const\n{\n  return std::string( \"\\033[?1l\\033[0m\\033[?25h\"\n                      \"\\033[?1003l\\033[?1002l\\033[?1001l\\033[?1000l\"\n                      \"\\033[?1015l\\033[?1006l\\033[?1005l\" )\n         + std::string( rmcup ? rmcup : \"\" );\n}\n\nstd::string Display::new_frame( bool initialized, const Framebuffer& last, const Framebuffer& f ) const\n{\n  FrameState frame( last );\n\n  char tmp[64];\n\n  /* has bell been rung? */\n  if ( f.get_bell_count() != frame.last_frame.get_bell_count() ) {\n    frame.append( '\\007' );\n  }\n  using title_type = Terminal::Framebuffer::title_type;\n\n  /* has icon name or window title changed? */\n  if ( has_title && f.is_title_initialized()\n       && ( ( !initialized ) || ( f.get_icon_name() != frame.last_frame.get_icon_name() )\n            || ( f.get_window_title() != frame.last_frame.get_window_title() ) ) ) {\n    /* set icon name and window title */\n    if ( f.get_icon_name() == f.get_window_title() ) {\n      /* write combined Icon Name and Window Title */\n      frame.append( \"\\033]0;\" );\n      const title_type& window_title( f.get_window_title() );\n      for ( title_type::const_iterator i = window_title.begin(); i != window_title.end(); i++ ) {\n        frame.append( *i );\n      }\n      frame.append( '\\007' );\n      /* ST is more correct, but BEL more widely supported */\n    } else {\n      /* write Icon Name */\n      frame.append( \"\\033]1;\" );\n      const title_type& icon_name( f.get_icon_name() );\n      for ( title_type::const_iterator i = icon_name.begin(); i != icon_name.end(); i++ ) {\n        frame.append( *i );\n      }\n      frame.append( '\\007' );\n\n      frame.append( \"\\033]2;\" );\n      const title_type& window_title( f.get_window_title() );\n      for ( title_type::const_iterator i = window_title.begin(); i != window_title.end(); i++ ) {\n        frame.append( *i );\n      }\n      frame.append( '\\007' );\n    }\n  }\n\n  /* has clipboard changed? */\n  if ( f.get_clipboard() != frame.last_frame.get_clipboard() ) {\n    frame.append( \"\\033]52;c;\" );\n    const title_type& clipboard( f.get_clipboard() );\n    for ( title_type::const_iterator i = clipboard.begin(); i != clipboard.end(); i++ ) {\n      frame.append( *i );\n    }\n    frame.append( '\\007' );\n  }\n\n  /* has reverse video state changed? */\n  if ( ( !initialized ) || ( f.ds.reverse_video != frame.last_frame.ds.reverse_video ) ) {\n    /* set reverse video */\n    snprintf( tmp, 64, \"\\033[?5%c\", ( f.ds.reverse_video ? 'h' : 'l' ) );\n    frame.append( tmp );\n  }\n\n  /* has size changed? */\n  if ( ( !initialized ) || ( f.ds.get_width() != frame.last_frame.ds.get_width() )\n       || ( f.ds.get_height() != frame.last_frame.ds.get_height() ) ) {\n    /* reset scrolling region */\n    frame.append( \"\\033[r\" );\n\n    /* clear screen */\n    frame.append( \"\\033[0m\\033[H\\033[2J\" );\n    initialized = false;\n    frame.cursor_x = frame.cursor_y = 0;\n    frame.current_rendition = initial_rendition();\n  } else {\n    frame.cursor_x = frame.last_frame.ds.get_cursor_col();\n    frame.cursor_y = frame.last_frame.ds.get_cursor_row();\n    frame.current_rendition = frame.last_frame.ds.get_renditions();\n  }\n\n  /* is cursor visibility initialized? */\n  if ( !initialized ) {\n    frame.cursor_visible = false;\n    frame.append( \"\\033[?25l\" );\n  }\n\n  int frame_y = 0;\n  Framebuffer::row_pointer blank_row;\n  Framebuffer::rows_type rows( frame.last_frame.get_rows() );\n  /* Extend rows if we've gotten a resize and new is wider than old */\n  if ( frame.last_frame.ds.get_width() < f.ds.get_width() ) {\n    for ( Framebuffer::rows_type::iterator p = rows.begin(); p != rows.end(); p++ ) {\n      *p = std::make_shared<Row>( **p );\n      ( *p )->cells.resize( f.ds.get_width(), Cell( f.ds.get_background_rendition() ) );\n    }\n  }\n  /* Add rows if we've gotten a resize and new is taller than old */\n  if ( static_cast<int>( rows.size() ) < f.ds.get_height() ) {\n    // get a proper blank row\n    const size_t w = f.ds.get_width();\n    const color_type c = 0;\n    blank_row = std::make_shared<Row>( w, c );\n    rows.resize( f.ds.get_height(), blank_row );\n  }\n\n  /* shortcut -- has display moved up by a certain number of lines? */\n  if ( initialized ) {\n    int lines_scrolled = 0;\n    int scroll_height = 0;\n\n    for ( int row = 0; row < f.ds.get_height(); row++ ) {\n      const Row* new_row = f.get_row( 0 );\n      const Row* old_row = &*rows.at( row );\n      if ( !( new_row == old_row || *new_row == *old_row ) ) {\n        continue;\n      }\n      /* if row 0, we're looking at ourselves and probably didn't scroll */\n      if ( row == 0 ) {\n        break;\n      }\n      /* found a scroll */\n      lines_scrolled = row;\n      scroll_height = 1;\n\n      /* how big is the region that was scrolled? */\n      for ( int region_height = 1; lines_scrolled + region_height < f.ds.get_height(); region_height++ ) {\n        if ( *f.get_row( region_height ) == *rows.at( lines_scrolled + region_height ) ) {\n          scroll_height = region_height + 1;\n        } else {\n          break;\n        }\n      }\n\n      break;\n    }\n\n    if ( scroll_height ) {\n      frame_y = scroll_height;\n\n      if ( lines_scrolled ) {\n        /* Now we need a proper blank row. */\n        if ( blank_row.get() == NULL ) {\n          const size_t w = f.ds.get_width();\n          const color_type c = 0;\n          blank_row = std::make_shared<Row>( w, c );\n        }\n        frame.update_rendition( initial_rendition(), true );\n\n        int top_margin = 0;\n        int bottom_margin = top_margin + lines_scrolled + scroll_height - 1;\n\n        assert( bottom_margin < f.ds.get_height() );\n\n        /* Common case:  if we're already on the bottom line and we're scrolling the whole\n         * screen, just do a CR and LFs.\n         */\n        if ( scroll_height + lines_scrolled == f.ds.get_height() && frame.cursor_y + 1 == f.ds.get_height() ) {\n          frame.append( '\\r' );\n          frame.append( lines_scrolled, '\\n' );\n          frame.cursor_x = 0;\n        } else {\n          /* set scrolling region */\n          snprintf( tmp, 64, \"\\033[%d;%dr\", top_margin + 1, bottom_margin + 1 );\n          frame.append( tmp );\n\n          /* go to bottom of scrolling region */\n          frame.cursor_x = frame.cursor_y = -1;\n          frame.append_silent_move( bottom_margin, 0 );\n\n          /* scroll */\n          frame.append( lines_scrolled, '\\n' );\n\n          /* reset scrolling region */\n          frame.append( \"\\033[r\" );\n          /* invalidate cursor position after unsetting scrolling region */\n          frame.cursor_x = frame.cursor_y = -1;\n        }\n\n        /* do the move in our local index */\n        for ( int i = top_margin; i <= bottom_margin; i++ ) {\n          if ( i + lines_scrolled <= bottom_margin ) {\n            rows.at( i ) = rows.at( i + lines_scrolled );\n          } else {\n            rows.at( i ) = blank_row;\n          }\n        }\n      }\n    }\n  }\n\n  /* Now update the display, row by row */\n  bool wrap = false;\n  for ( ; frame_y < f.ds.get_height(); frame_y++ ) {\n    wrap = put_row( initialized, frame, f, frame_y, *rows.at( frame_y ), wrap );\n  }\n\n  /* has cursor location changed? */\n  if ( ( !initialized ) || ( f.ds.get_cursor_row() != frame.cursor_y )\n       || ( f.ds.get_cursor_col() != frame.cursor_x ) ) {\n    frame.append_move( f.ds.get_cursor_row(), f.ds.get_cursor_col() );\n  }\n\n  /* has cursor visibility changed? */\n  if ( ( !initialized ) || ( f.ds.cursor_visible != frame.cursor_visible ) ) {\n    if ( f.ds.cursor_visible ) {\n      frame.append( \"\\033[?25h\" );\n    } else {\n      frame.append( \"\\033[?25l\" );\n    }\n  }\n\n  /* have renditions changed? */\n  frame.update_rendition( f.ds.get_renditions(), !initialized );\n\n  /* has bracketed paste mode changed? */\n  if ( ( !initialized ) || ( f.ds.bracketed_paste != frame.last_frame.ds.bracketed_paste ) ) {\n    frame.append( f.ds.bracketed_paste ? \"\\033[?2004h\" : \"\\033[?2004l\" );\n  }\n\n  /* has mouse reporting mode changed? */\n  if ( ( !initialized ) || ( f.ds.mouse_reporting_mode != frame.last_frame.ds.mouse_reporting_mode ) ) {\n    if ( f.ds.mouse_reporting_mode == DrawState::MOUSE_REPORTING_NONE ) {\n      frame.append( \"\\033[?1003l\" );\n      frame.append( \"\\033[?1002l\" );\n      frame.append( \"\\033[?1001l\" );\n      frame.append( \"\\033[?1000l\" );\n    } else {\n      if ( frame.last_frame.ds.mouse_reporting_mode != DrawState::MOUSE_REPORTING_NONE ) {\n        snprintf( tmp, sizeof( tmp ), \"\\033[?%dl\", frame.last_frame.ds.mouse_reporting_mode );\n        frame.append( tmp );\n      }\n      snprintf( tmp, sizeof( tmp ), \"\\033[?%dh\", f.ds.mouse_reporting_mode );\n      frame.append( tmp );\n    }\n  }\n\n  /* has mouse focus mode changed? */\n  if ( ( !initialized ) || ( f.ds.mouse_focus_event != frame.last_frame.ds.mouse_focus_event ) ) {\n    frame.append( f.ds.mouse_focus_event ? \"\\033[?1004h\" : \"\\033[?1004l\" );\n  }\n\n  /* has mouse encoding mode changed? */\n  if ( ( !initialized ) || ( f.ds.mouse_encoding_mode != frame.last_frame.ds.mouse_encoding_mode ) ) {\n    if ( f.ds.mouse_encoding_mode == DrawState::MOUSE_ENCODING_DEFAULT ) {\n      frame.append( \"\\033[?1015l\" );\n      frame.append( \"\\033[?1006l\" );\n      frame.append( \"\\033[?1005l\" );\n    } else {\n      if ( frame.last_frame.ds.mouse_encoding_mode != DrawState::MOUSE_ENCODING_DEFAULT ) {\n        snprintf( tmp, sizeof( tmp ), \"\\033[?%dl\", frame.last_frame.ds.mouse_encoding_mode );\n        frame.append( tmp );\n      }\n      snprintf( tmp, sizeof( tmp ), \"\\033[?%dh\", f.ds.mouse_encoding_mode );\n      frame.append( tmp );\n    }\n  }\n\n  return frame.str;\n}\n\nbool Display::put_row( bool initialized,\n                       FrameState& frame,\n                       const Framebuffer& f,\n                       int frame_y,\n                       const Row& old_row,\n                       bool wrap ) const\n{\n  char tmp[64];\n  int frame_x = 0;\n\n  const Row& row = *f.get_row( frame_y );\n  const Row::cells_type& cells = row.cells;\n  const Row::cells_type& old_cells = old_row.cells;\n\n  /* If we're forced to write the first column because of wrap, go ahead and do so. */\n  if ( wrap ) {\n    const Cell& cell = cells.at( 0 );\n    frame.update_rendition( cell.get_renditions() );\n    frame.append_cell( cell );\n    frame_x += cell.get_width();\n    frame.cursor_x += cell.get_width();\n  }\n\n  /* If rows are the same object, we don't need to do anything at all. */\n  if ( initialized && &row == &old_row ) {\n    return false;\n  }\n\n  const bool wrap_this = row.get_wrap();\n  const int row_width = f.ds.get_width();\n  int clear_count = 0;\n  bool wrote_last_cell = false;\n  Renditions blank_renditions = initial_rendition();\n\n  /* iterate for every cell */\n  while ( frame_x < row_width ) {\n\n    const Cell& cell = cells.at( frame_x );\n\n    /* Does cell need to be drawn?  Skip all this. */\n    if ( initialized && !clear_count && ( cell == old_cells.at( frame_x ) ) ) {\n      frame_x += cell.get_width();\n      continue;\n    }\n\n    /* Slurp up all the empty cells */\n    if ( cell.empty() ) {\n      if ( !clear_count ) {\n        blank_renditions = cell.get_renditions();\n      }\n      if ( cell.get_renditions() == blank_renditions ) {\n        /* Remember run of blank cells */\n        clear_count++;\n        frame_x++;\n        continue;\n      }\n    }\n\n    /* Clear or write cells within the row (not to end). */\n    if ( clear_count ) {\n      /* Move to the right position. */\n      frame.append_silent_move( frame_y, frame_x - clear_count );\n      frame.update_rendition( blank_renditions );\n      bool can_use_erase = has_bce || ( frame.current_rendition == initial_rendition() );\n      if ( can_use_erase && has_ech && clear_count > 4 ) {\n        snprintf( tmp, 64, \"\\033[%dX\", clear_count );\n        frame.append( tmp );\n      } else {\n        frame.append( clear_count, ' ' );\n        frame.cursor_x = frame_x;\n      }\n      clear_count = 0;\n      // If the current character is *another* empty cell in a different rendition,\n      // we restart counting and continue here\n      if ( cell.empty() ) {\n        blank_renditions = cell.get_renditions();\n        clear_count = 1;\n        frame_x++;\n        continue;\n      }\n    }\n\n    /* Now draw a character cell. */\n    /* Move to the right position. */\n    const int cell_width = cell.get_width();\n    /* If we are about to print the last character in a wrapping row,\n       trash the cursor position to force explicit positioning.  We do\n       this because our input terminal state may have the cursor on\n       the autowrap column (\"column 81\"), but our output terminal\n       states always snap the cursor to the true last column (\"column\n       80\"), and we want to be able to apply the diff to either, for\n       verification. */\n    if ( wrap_this && frame_x + cell_width >= row_width ) {\n      frame.cursor_x = frame.cursor_y = -1;\n    }\n    frame.append_silent_move( frame_y, frame_x );\n    frame.update_rendition( cell.get_renditions() );\n    frame.append_cell( cell );\n    frame_x += cell_width;\n    frame.cursor_x += cell_width;\n    if ( frame_x >= row_width ) {\n      wrote_last_cell = true;\n    }\n  }\n\n  /* End of line. */\n\n  /* Clear or write empty cells at EOL. */\n  if ( clear_count ) {\n    /* Move to the right position. */\n    frame.append_silent_move( frame_y, frame_x - clear_count );\n    frame.update_rendition( blank_renditions );\n\n    bool can_use_erase = has_bce || ( frame.current_rendition == initial_rendition() );\n    if ( can_use_erase && !wrap_this ) {\n      frame.append( \"\\033[K\" );\n    } else {\n      frame.append( clear_count, ' ' );\n      frame.cursor_x = frame_x;\n      wrote_last_cell = true;\n    }\n  }\n\n  if ( !( wrote_last_cell && ( frame_y < f.ds.get_height() - 1 ) ) ) {\n    return false;\n  }\n  /* To hint that a word-select should group the end of one line\n     with the beginning of the next, we let the real cursor\n     actually wrap around in cases where it wrapped around for us. */\n  if ( wrap_this ) {\n    /* Update our cursor, and ask for wrap on the next row. */\n    frame.cursor_x = 0;\n    frame.cursor_y++;\n    return true;\n  }\n  /* Resort to CR/LF and update our cursor. */\n  frame.append( \"\\r\\n\" );\n  frame.cursor_x = 0;\n  frame.cursor_y++;\n  return false;\n}\n\nFrameState::FrameState( const Framebuffer& s_last )\n  : str(), cursor_x( 0 ), cursor_y( 0 ), current_rendition( 0 ), cursor_visible( s_last.ds.cursor_visible ),\n    last_frame( s_last )\n{\n  /* Preallocate for better performance.  Make a guess-- doesn't matter for correctness */\n  str.reserve( last_frame.ds.get_width() * last_frame.ds.get_height() * 4 );\n}\n\nvoid FrameState::append_silent_move( int y, int x )\n{\n  if ( cursor_x == x && cursor_y == y )\n    return;\n  /* turn off cursor if necessary before moving cursor */\n  if ( cursor_visible ) {\n    append( \"\\033[?25l\" );\n    cursor_visible = false;\n  }\n  append_move( y, x );\n}\n\nvoid FrameState::append_move( int y, int x )\n{\n  const int last_x = cursor_x;\n  const int last_y = cursor_y;\n  cursor_x = x;\n  cursor_y = y;\n  // Only optimize if cursor pos is known\n  if ( last_x != -1 && last_y != -1 ) {\n    // Can we use CR and/or LF?  They're cheap and easier to trace.\n    if ( x == 0 && y - last_y >= 0 && y - last_y < 5 ) {\n      if ( last_x != 0 ) {\n        append( '\\r' );\n      }\n      append( y - last_y, '\\n' );\n      return;\n    }\n    // Backspaces are good too.\n    if ( y == last_y && x - last_x < 0 && x - last_x > -5 ) {\n      append( last_x - x, '\\b' );\n      return;\n    }\n    // More optimizations are possible.\n  }\n  char tmp[64];\n  snprintf( tmp, 64, \"\\033[%d;%dH\", y + 1, x + 1 );\n  append( tmp );\n}\n\nvoid FrameState::update_rendition( const Renditions& r, bool force )\n{\n  if ( force || !( current_rendition == r ) ) {\n    /* print renditions */\n    append_string( r.sgr() );\n    current_rendition = r;\n  }\n}\n"
  },
  {
    "path": "src/terminal/terminaldisplay.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TERMINALDISPLAY_HPP\n#define TERMINALDISPLAY_HPP\n\n#include \"src/terminal/terminalframebuffer.h\"\n\nnamespace Terminal {\n/* variables used within a new_frame */\nclass FrameState\n{\npublic:\n  std::string str;\n\n  int cursor_x, cursor_y;\n  Renditions current_rendition;\n  bool cursor_visible;\n\n  const Framebuffer& last_frame;\n\n  FrameState( const Framebuffer& s_last );\n\n  void append( char c ) { str.append( 1, c ); }\n  void append( size_t s, char c ) { str.append( s, c ); }\n  void append( wchar_t wc ) { Cell::append_to_str( str, wc ); }\n  void append( const char* s ) { str.append( s ); }\n  void append_string( const std::string& append ) { str.append( append ); }\n\n  void append_cell( const Cell& cell ) { cell.print_grapheme( str ); }\n  void append_silent_move( int y, int x );\n  void append_move( int y, int x );\n  void update_rendition( const Renditions& r, bool force = false );\n};\n\nclass Display\n{\nprivate:\n  bool has_ech; /* erase character is part of vt200 but not supported by tmux\n                   (or by \"screen\" terminfo entry, which is what tmux advertises) */\n\n  bool has_bce; /* erases result in cell filled with background color */\n\n  bool has_title; /* supports window title and icon name */\n\n  const char *smcup, *rmcup; /* enter and exit alternate screen mode */\n\n  bool put_row( bool initialized,\n                FrameState& frame,\n                const Framebuffer& f,\n                int frame_y,\n                const Row& old_row,\n                bool wrap ) const;\n\npublic:\n  std::string open() const;\n  std::string close() const;\n\n  std::string new_frame( bool initialized, const Framebuffer& last, const Framebuffer& f ) const;\n\n  Display( bool use_environment );\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/terminaldisplayinit.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n/* This is in its own file because otherwise the ncurses #defines\n   alias our own variable names. */\n\n#include \"src/include/config.h\"\n#include \"terminaldisplay.h\"\n\n#include <stdexcept>\n#include <string>\n\n#if defined HAVE_NCURSESW_CURSES_H\n#include <ncursesw/curses.h>\n#include <ncursesw/term.h>\n#elif defined HAVE_NCURSESW_H\n#include <ncursesw.h>\n#include <term.h>\n#elif defined HAVE_NCURSES_CURSES_H\n#include <ncurses/curses.h>\n#include <ncurses/term.h>\n#elif defined HAVE_NCURSES_H\n#include <ncurses.h>\n#include <term.h>\n#elif defined HAVE_CURSES_H\n#include <curses.h>\n#include <term.h>\n#else\n#error \"SysV or X/Open-compatible Curses header file required\"\n#endif\n#include <cstdlib>\n#include <cstring>\n\nusing namespace Terminal;\n\nstatic bool ti_flag( const char* capname )\n{\n  int val = tigetflag( const_cast<char*>( capname ) );\n  if ( val == -1 ) {\n    throw std::invalid_argument( std::string( \"Invalid terminfo boolean capability \" ) + capname );\n  }\n  return val;\n}\n\nstatic const char* ti_str( const char* capname )\n{\n  const char* val = tigetstr( const_cast<char*>( capname ) );\n  if ( val == (const char*)-1 ) {\n    throw std::invalid_argument( std::string( \"Invalid terminfo string capability \" ) + capname );\n  }\n  return val;\n}\n\nDisplay::Display( bool use_environment )\n  : has_ech( true ), has_bce( true ), has_title( true ), smcup( NULL ), rmcup( NULL )\n{\n  if ( use_environment ) {\n    int errret = -2;\n    int ret = setupterm( (char*)0, 1, &errret );\n\n    if ( ret != OK ) {\n      switch ( errret ) {\n        case 1:\n          throw std::runtime_error( \"Terminal is hardcopy and cannot be used by curses applications.\" );\n          break;\n        case 0:\n          throw std::runtime_error( \"Unknown terminal type.\" );\n          break;\n        case -1:\n          throw std::runtime_error( \"Terminfo database could not be found.\" );\n          break;\n        default:\n          throw std::runtime_error( \"Unknown terminfo error.\" );\n          break;\n      }\n    }\n\n    /* check for ECH */\n    has_ech = ti_str( \"ech\" );\n\n    /* check for BCE */\n    has_bce = ti_flag( \"bce\" );\n\n    /* Check if we can set the window title and icon name.  terminfo does not\n       have reliable information on this, so we hardcode a whitelist of\n       terminal type prefixes. */\n    static const char* const title_term_types[]\n      = { \"xterm\", \"rxvt\", \"kterm\", \"Eterm\", \"alacritty\", \"screen\", \"tmux\" };\n\n    has_title = false;\n    const char* term_type = getenv( \"TERM\" );\n    if ( term_type ) {\n      for ( size_t i = 0; i < sizeof( title_term_types ) / sizeof( const char* ); i++ ) {\n        if ( 0 == strncmp( term_type, title_term_types[i], strlen( title_term_types[i] ) ) ) {\n          has_title = true;\n          break;\n        }\n      }\n    }\n\n    if ( !getenv( \"MOSH_NO_TERM_INIT\" ) ) {\n      smcup = ti_str( \"smcup\" );\n      rmcup = ti_str( \"rmcup\" );\n    }\n  }\n}\n"
  },
  {
    "path": "src/terminal/terminalframebuffer.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n#include <cstdio>\n#include <cstdlib>\n\n#include \"src/terminal/terminalframebuffer.h\"\n\nusing namespace Terminal;\n\nCell::Cell( color_type background_color )\n  : contents(), renditions( background_color ), wide( false ), fallback( false ), wrap( false )\n{}\n\nvoid Cell::reset( color_type background_color )\n{\n  contents.clear();\n  renditions = Renditions( background_color );\n  wide = false;\n  fallback = false;\n  wrap = false;\n}\n\nvoid DrawState::reinitialize_tabs( unsigned int start )\n{\n  assert( default_tabs );\n  for ( unsigned int i = start; i < tabs.size(); i++ ) {\n    tabs[i] = ( ( i % 8 ) == 0 );\n  }\n}\n\nDrawState::DrawState( int s_width, int s_height )\n  : width( s_width ), height( s_height ), cursor_col( 0 ), cursor_row( 0 ), combining_char_col( 0 ),\n    combining_char_row( 0 ), default_tabs( true ), tabs( s_width ), scrolling_region_top_row( 0 ),\n    scrolling_region_bottom_row( height - 1 ), renditions( 0 ), save(), next_print_will_wrap( false ),\n    origin_mode( false ), auto_wrap_mode( true ), insert_mode( false ), cursor_visible( true ),\n    reverse_video( false ), bracketed_paste( false ), mouse_reporting_mode( MOUSE_REPORTING_NONE ),\n    mouse_focus_event( false ), mouse_alternate_scroll( false ), mouse_encoding_mode( MOUSE_ENCODING_DEFAULT ),\n    application_mode_cursor_keys( false )\n{\n  reinitialize_tabs( 0 );\n}\n\nFramebuffer::Framebuffer( int s_width, int s_height )\n  : rows(), icon_name(), window_title(), clipboard(), bell_count( 0 ), title_initialized( false ),\n    ds( s_width, s_height )\n{\n  assert( s_height > 0 );\n  assert( s_width > 0 );\n  const size_t w = s_width;\n  const color_type c = 0;\n  rows = rows_type( s_height, row_pointer( std::make_shared<Row>( w, c ) ) );\n}\n\nFramebuffer::Framebuffer( const Framebuffer& other )\n  : rows( other.rows ), icon_name( other.icon_name ), window_title( other.window_title ),\n    clipboard( other.clipboard ), bell_count( other.bell_count ), title_initialized( other.title_initialized ),\n    ds( other.ds )\n{}\n\nFramebuffer& Framebuffer::operator=( const Framebuffer& other )\n{\n  if ( this != &other ) {\n    rows = other.rows;\n    icon_name = other.icon_name;\n    window_title = other.window_title;\n    clipboard = other.clipboard;\n    bell_count = other.bell_count;\n    title_initialized = other.title_initialized;\n    ds = other.ds;\n  }\n  return *this;\n}\n\nvoid Framebuffer::scroll( int N )\n{\n  if ( N >= 0 ) {\n    delete_line( ds.get_scrolling_region_top_row(), N );\n  } else {\n    insert_line( ds.get_scrolling_region_top_row(), -N );\n  }\n}\n\nvoid DrawState::new_grapheme( void )\n{\n  combining_char_col = cursor_col;\n  combining_char_row = cursor_row;\n}\n\nvoid DrawState::snap_cursor_to_border( void )\n{\n  if ( cursor_row < limit_top() )\n    cursor_row = limit_top();\n  if ( cursor_row > limit_bottom() )\n    cursor_row = limit_bottom();\n  if ( cursor_col < 0 )\n    cursor_col = 0;\n  if ( cursor_col >= width )\n    cursor_col = width - 1;\n}\n\nvoid DrawState::move_row( int N, bool relative )\n{\n  if ( relative ) {\n    cursor_row += N;\n  } else {\n    cursor_row = N + limit_top();\n  }\n\n  snap_cursor_to_border();\n  new_grapheme();\n  next_print_will_wrap = false;\n}\n\nvoid DrawState::move_col( int N, bool relative, bool implicit )\n{\n  if ( implicit ) {\n    new_grapheme();\n  }\n\n  if ( relative ) {\n    cursor_col += N;\n  } else {\n    cursor_col = N;\n  }\n\n  if ( implicit ) {\n    next_print_will_wrap = ( cursor_col >= width );\n  }\n\n  snap_cursor_to_border();\n  if ( !implicit ) {\n    new_grapheme();\n    next_print_will_wrap = false;\n  }\n}\n\nvoid Framebuffer::move_rows_autoscroll( int rows )\n{\n  /* don't scroll if outside the scrolling region */\n  if ( ( ds.get_cursor_row() < ds.get_scrolling_region_top_row() )\n       || ( ds.get_cursor_row() > ds.get_scrolling_region_bottom_row() ) ) {\n    ds.move_row( rows, true );\n    return;\n  }\n\n  if ( ds.get_cursor_row() + rows > ds.get_scrolling_region_bottom_row() ) {\n    int N = ds.get_cursor_row() + rows - ds.get_scrolling_region_bottom_row();\n    scroll( N );\n    ds.move_row( -N, true );\n  } else if ( ds.get_cursor_row() + rows < ds.get_scrolling_region_top_row() ) {\n    int N = ds.get_cursor_row() + rows - ds.get_scrolling_region_top_row();\n    scroll( N );\n    ds.move_row( -N, true );\n  }\n\n  ds.move_row( rows, true );\n}\n\nCell* Framebuffer::get_combining_cell( void )\n{\n  if ( ( ds.get_combining_char_col() < 0 ) || ( ds.get_combining_char_row() < 0 )\n       || ( ds.get_combining_char_col() >= ds.get_width() )\n       || ( ds.get_combining_char_row() >= ds.get_height() ) ) {\n    return NULL;\n  } /* can happen if a resize came in between */\n\n  return get_mutable_cell( ds.get_combining_char_row(), ds.get_combining_char_col() );\n}\n\nvoid DrawState::set_tab( void )\n{\n  tabs[cursor_col] = true;\n}\n\nvoid DrawState::clear_tab( int col )\n{\n  tabs[col] = false;\n}\n\nint DrawState::get_next_tab( int count ) const\n{\n  if ( count >= 0 ) {\n    for ( int i = cursor_col + 1; i < width; i++ ) {\n      if ( tabs[i] && --count == 0 ) {\n        return i;\n      }\n    }\n    return -1;\n  }\n  for ( int i = cursor_col - 1; i > 0; i-- ) {\n    if ( tabs[i] && ++count == 0 ) {\n      return i;\n    }\n  }\n  return 0;\n}\n\nvoid DrawState::set_scrolling_region( int top, int bottom )\n{\n  if ( height < 1 ) {\n    return;\n  }\n\n  scrolling_region_top_row = top;\n  scrolling_region_bottom_row = bottom;\n\n  if ( scrolling_region_top_row < 0 )\n    scrolling_region_top_row = 0;\n  if ( scrolling_region_bottom_row >= height )\n    scrolling_region_bottom_row = height - 1;\n\n  if ( scrolling_region_bottom_row < scrolling_region_top_row )\n    scrolling_region_bottom_row = scrolling_region_top_row;\n  /* real rule requires TWO-line scrolling region */\n\n  if ( origin_mode ) {\n    snap_cursor_to_border();\n    new_grapheme();\n  }\n}\n\nint DrawState::limit_top( void ) const\n{\n  return origin_mode ? scrolling_region_top_row : 0;\n}\n\nint DrawState::limit_bottom( void ) const\n{\n  return origin_mode ? scrolling_region_bottom_row : height - 1;\n}\n\nvoid Framebuffer::apply_renditions_to_cell( Cell* cell )\n{\n  if ( !cell ) {\n    cell = get_mutable_cell();\n  }\n  cell->set_renditions( ds.get_renditions() );\n}\n\nSavedCursor::SavedCursor()\n  : cursor_col( 0 ), cursor_row( 0 ), renditions( 0 ), auto_wrap_mode( true ), origin_mode( false )\n{}\n\nvoid DrawState::save_cursor( void )\n{\n  save.cursor_col = cursor_col;\n  save.cursor_row = cursor_row;\n  save.renditions = renditions;\n  save.auto_wrap_mode = auto_wrap_mode;\n  save.origin_mode = origin_mode;\n}\n\nvoid DrawState::restore_cursor( void )\n{\n  cursor_col = save.cursor_col;\n  cursor_row = save.cursor_row;\n  renditions = save.renditions;\n  auto_wrap_mode = save.auto_wrap_mode;\n  origin_mode = save.origin_mode;\n\n  snap_cursor_to_border(); /* we could have resized in between */\n  new_grapheme();\n}\n\nvoid Framebuffer::insert_line( int before_row, int count )\n{\n  if ( ( before_row < ds.get_scrolling_region_top_row() )\n       || ( before_row > ds.get_scrolling_region_bottom_row() + 1 ) ) {\n    return;\n  }\n\n  int scroll = ds.get_scrolling_region_bottom_row() + 1 - before_row;\n  if ( count < scroll ) {\n    scroll = count;\n  }\n\n  if ( scroll == 0 ) {\n    return;\n  }\n\n  // delete old rows\n  rows_type::iterator start = rows.begin() + ds.get_scrolling_region_bottom_row() + 1 - scroll;\n  rows.erase( start, start + scroll );\n  // insert new rows\n  start = rows.begin() + before_row;\n  rows.insert( start, scroll, newrow() );\n}\n\nvoid Framebuffer::delete_line( int row, int count )\n{\n  if ( ( row < ds.get_scrolling_region_top_row() ) || ( row > ds.get_scrolling_region_bottom_row() ) ) {\n    return;\n  }\n\n  int scroll = ds.get_scrolling_region_bottom_row() + 1 - row;\n  if ( count < scroll ) {\n    scroll = count;\n  }\n\n  if ( scroll == 0 ) {\n    return;\n  }\n\n  // delete old rows\n  rows_type::iterator start = rows.begin() + row;\n  rows.erase( start, start + scroll );\n  // insert a block of dummy rows\n  start = rows.begin() + ds.get_scrolling_region_bottom_row() + 1 - scroll;\n  rows.insert( start, scroll, newrow() );\n}\n\nRow::Row( const size_t s_width, const color_type background_color )\n  : cells( s_width, Cell( background_color ) ), gen( get_gen() )\n{}\n\nuint64_t Row::get_gen() const\n{\n  static uint64_t gen_counter = 0;\n  return gen_counter++;\n}\n\nvoid Row::insert_cell( int col, color_type background_color )\n{\n  cells.insert( cells.begin() + col, Cell( background_color ) );\n  cells.pop_back();\n}\n\nvoid Row::delete_cell( int col, color_type background_color )\n{\n  cells.push_back( Cell( background_color ) );\n  cells.erase( cells.begin() + col );\n}\n\nvoid Framebuffer::insert_cell( int row, int col )\n{\n  get_mutable_row( row )->insert_cell( col, ds.get_background_rendition() );\n}\n\nvoid Framebuffer::delete_cell( int row, int col )\n{\n  get_mutable_row( row )->delete_cell( col, ds.get_background_rendition() );\n}\n\nvoid Framebuffer::reset( void )\n{\n  int width = ds.get_width(), height = ds.get_height();\n  ds = DrawState( width, height );\n  rows = rows_type( height, newrow() );\n  window_title.clear();\n  clipboard.clear();\n  /* do not reset bell_count */\n}\n\nvoid Framebuffer::soft_reset( void )\n{\n  ds.insert_mode = false;\n  ds.origin_mode = false;\n  ds.cursor_visible = true; /* per xterm and gnome-terminal */\n  ds.application_mode_cursor_keys = false;\n  ds.set_scrolling_region( 0, ds.get_height() - 1 );\n  ds.add_rendition( 0 );\n  ds.clear_saved_cursor();\n}\n\nvoid Framebuffer::resize( int s_width, int s_height )\n{\n  assert( s_width > 0 );\n  assert( s_height > 0 );\n\n  int oldheight = ds.get_height();\n  int oldwidth = ds.get_width();\n  ds.resize( s_width, s_height );\n\n  row_pointer blankrow( newrow() );\n  if ( oldheight != s_height ) {\n    rows.resize( s_height, blankrow );\n  }\n  if ( oldwidth == s_width ) {\n    return;\n  }\n  for ( rows_type::iterator i = rows.begin(); i != rows.end() && *i != blankrow; i++ ) {\n    *i = std::make_shared<Row>( **i );\n    ( *i )->set_wrap( false );\n    ( *i )->cells.resize( s_width, Cell( ds.get_background_rendition() ) );\n  }\n}\n\nvoid DrawState::resize( int s_width, int s_height )\n{\n  if ( ( width != s_width ) || ( height != s_height ) ) {\n    /* reset entire scrolling region on any resize */\n    /* xterm and rxvt-unicode do this. gnome-terminal only\n       resets scrolling region if it has to become smaller in resize */\n    scrolling_region_top_row = 0;\n    scrolling_region_bottom_row = s_height - 1;\n  }\n\n  tabs.resize( s_width );\n  if ( default_tabs ) {\n    reinitialize_tabs( width );\n  }\n\n  width = s_width;\n  height = s_height;\n\n  snap_cursor_to_border();\n\n  /* saved cursor will be snapped to border on restore */\n\n  /* invalidate combining char cell if necessary */\n  if ( ( combining_char_col >= width ) || ( combining_char_row >= height ) ) {\n    combining_char_col = combining_char_row = -1;\n  }\n}\n\nRenditions::Renditions( color_type s_background )\n  : foreground_color( 0 ), background_color( s_background ), attributes( 0 )\n{}\n\n/* This routine cannot be used to set a color beyond the 16-color set. */\nvoid Renditions::set_rendition( color_type num )\n{\n  if ( num == 0 ) {\n    clear_attributes();\n    foreground_color = background_color = 0;\n    return;\n  }\n\n  if ( num == 39 ) {\n    foreground_color = 0;\n    return;\n  } else if ( num == 49 ) {\n    background_color = 0;\n    return;\n  }\n\n  if ( ( 30 <= num ) && ( num <= 37 ) ) { /* foreground color in 8-color set */\n    foreground_color = num;\n    return;\n  } else if ( ( 40 <= num ) && ( num <= 47 ) ) { /* background color in 8-color set */\n    background_color = num;\n    return;\n  } else if ( ( 90 <= num ) && ( num <= 97 ) ) { /* foreground color in 16-color set */\n    foreground_color = num - 90 + 38;\n    return;\n  } else if ( ( 100 <= num ) && ( num <= 107 ) ) { /* background color in 16-color set */\n    background_color = num - 100 + 48;\n    return;\n  }\n\n  bool value = num < 9;\n  switch ( num ) {\n    case 1:\n    case 22:\n      set_attribute( bold, value );\n      break;\n    case 3:\n    case 23:\n      set_attribute( italic, value );\n      break;\n    case 4:\n    case 24:\n      set_attribute( underlined, value );\n      break;\n    case 5:\n    case 25:\n      set_attribute( blink, value );\n      break;\n    case 7:\n    case 27:\n      set_attribute( inverse, value );\n      break;\n    case 8:\n    case 28:\n      set_attribute( invisible, value );\n      break;\n    default:\n      break; /* ignore unknown rendition */\n  }\n}\n\nvoid Renditions::set_foreground_color( int num )\n{\n  if ( ( 0 <= num ) && ( num <= 255 ) ) {\n    foreground_color = 30 + num;\n  } else if ( is_true_color( num ) ) {\n    foreground_color = num;\n  }\n}\n\nvoid Renditions::set_background_color( int num )\n{\n  if ( ( 0 <= num ) && ( num <= 255 ) ) {\n    background_color = 40 + num;\n  } else if ( is_true_color( num ) ) {\n    background_color = num;\n  }\n}\n\nstd::string Renditions::sgr( void ) const\n{\n  std::string ret;\n  char col[64];\n\n  ret.append( \"\\033[0\" );\n  if ( get_attribute( bold ) )\n    ret.append( \";1\" );\n  if ( get_attribute( italic ) )\n    ret.append( \";3\" );\n  if ( get_attribute( underlined ) )\n    ret.append( \";4\" );\n  if ( get_attribute( blink ) )\n    ret.append( \";5\" );\n  if ( get_attribute( inverse ) )\n    ret.append( \";7\" );\n  if ( get_attribute( invisible ) )\n    ret.append( \";8\" );\n\n  if ( foreground_color ) {\n    // Since foreground_color is a 25-bit field, it is promoted to an int when\n    // manipulated. (See [conv.prom] in various C++ standards, e.g.,\n    // https://timsong-cpp.github.io/cppwp/n4659/conv.prom#5.) The correct\n    // printf format specifier is thus %d.\n    if ( is_true_color( foreground_color ) ) {\n      snprintf( col,\n                sizeof( col ),\n                \";38;2;%d;%d;%d\",\n                ( foreground_color >> 16 ) & 0xff,\n                ( foreground_color >> 8 ) & 0xff,\n                foreground_color & 0xff );\n    } else if ( foreground_color > 37 ) { /* use 256-color set */\n      snprintf( col, sizeof( col ), \";38;5;%d\", foreground_color - 30 );\n    } else { /* ANSI foreground color */\n      // Unfortunately, some versions of GCC (notably including GCC 9.3) give\n      // -Wformat warnings when relying on [conv.prom] to promote\n      // foreground_color in calls to printf. Explicitly promote it to silence\n      // the warning.\n      int fg = foreground_color;\n      snprintf( col, sizeof( col ), \";%d\", fg );\n    }\n    ret.append( col );\n  }\n  if ( background_color ) {\n    // See comment above about bit-field promotion; it applies here as well.\n    if ( is_true_color( background_color ) ) {\n      snprintf( col,\n                sizeof( col ),\n                \";48;2;%d;%d;%d\",\n                ( background_color >> 16 ) & 0xff,\n                ( background_color >> 8 ) & 0xff,\n                background_color & 0xff );\n    } else if ( background_color > 47 ) { /* use 256-color set */\n      snprintf( col, sizeof( col ), \";48;5;%d\", background_color - 40 );\n    } else { /* ANSI background color */\n      // See comment above about explicit promotion; it applies here as well.\n      int bg = background_color;\n      snprintf( col, sizeof( col ), \";%d\", bg );\n    }\n    ret.append( col );\n  }\n  ret.append( \"m\" );\n\n  return ret;\n}\n\nvoid Row::reset( color_type background_color )\n{\n  gen = get_gen();\n  for ( cells_type::iterator i = cells.begin(); i != cells.end(); i++ ) {\n    i->reset( background_color );\n  }\n}\n\nvoid Framebuffer::prefix_window_title( const title_type& s )\n{\n  if ( icon_name == window_title ) {\n    /* preserve equivalence */\n    icon_name.insert( icon_name.begin(), s.begin(), s.end() );\n  }\n  window_title.insert( window_title.begin(), s.begin(), s.end() );\n}\n\nstd::string Cell::debug_contents( void ) const\n{\n  if ( contents.empty() ) {\n    return \"'_' ()\";\n  }\n  std::string chars( 1, '\\'' );\n  print_grapheme( chars );\n  chars.append( \"' [\" );\n  const char* lazycomma = \"\";\n  char buf[64];\n  for ( content_type::const_iterator i = contents.begin(); i < contents.end(); i++ ) {\n\n    snprintf( buf, sizeof buf, \"%s0x%02x\", lazycomma, static_cast<uint8_t>( *i ) );\n    chars.append( buf );\n    lazycomma = \", \";\n  }\n  chars.append( \"]\" );\n  return chars;\n}\n\nbool Cell::compare( const Cell& other ) const\n{\n  bool ret = false;\n\n  std::string grapheme, other_grapheme;\n\n  print_grapheme( grapheme );\n  other.print_grapheme( other_grapheme );\n\n  if ( grapheme != other_grapheme ) {\n    ret = true;\n    fprintf( stderr, \"Graphemes: '%s' vs. '%s'\\n\", grapheme.c_str(), other_grapheme.c_str() );\n  }\n\n  if ( !contents_match( other ) ) {\n    // ret = true;\n    fprintf( stderr,\n             \"Contents: %s (%ld) vs. %s (%ld)\\n\",\n             debug_contents().c_str(),\n             static_cast<long int>( contents.size() ),\n             other.debug_contents().c_str(),\n             static_cast<long int>( other.contents.size() ) );\n  }\n\n  if ( fallback != other.fallback ) {\n    // ret = true;\n    // Since fallback is a 1-bit field, it is promoted to an int when\n    // manipulated. (See [conv.prom] in various C++ standards, e.g.,\n    // https://timsong-cpp.github.io/cppwp/n4659/conv.prom#5.) The correct\n    // printf format specifier is thus %d.\n    fprintf( stderr, \"fallback: %d vs. %d\\n\", fallback, other.fallback );\n  }\n\n  if ( wide != other.wide ) {\n    ret = true;\n    // See comment above about bit-field promotion; it applies here as well.\n    fprintf( stderr, \"width: %d vs. %d\\n\", wide, other.wide );\n  }\n\n  if ( !( renditions == other.renditions ) ) {\n    ret = true;\n    fprintf( stderr, \"renditions differ\\n\" );\n  }\n\n  if ( wrap != other.wrap ) {\n    ret = true;\n    // See comment above about bit-field promotion; it applies here as well.\n    fprintf( stderr, \"wrap: %d vs. %d\\n\", wrap, other.wrap );\n  }\n\n  return ret;\n}\n"
  },
  {
    "path": "src/terminal/terminalframebuffer.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TERMINALFB_HPP\n#define TERMINALFB_HPP\n\n#include <cassert>\n#include <climits>\n#include <cstdint>\n#include <deque>\n#include <list>\n#include <memory>\n#include <string>\n#include <vector>\n\n/* Terminal framebuffer */\n\nnamespace Terminal {\nusing color_type = uint32_t;\n\nclass Renditions\n{\npublic:\n  typedef enum\n  {\n    bold,\n    faint,\n    italic,\n    underlined,\n    blink,\n    inverse,\n    invisible,\n    SIZE\n  } attribute_type;\n\nprivate:\n  static const uint64_t true_color_mask = 0x1000000;\n  uint64_t foreground_color : 25;\n  uint64_t background_color : 25;\n  uint64_t attributes : 8;\n\npublic:\n  Renditions( color_type s_background );\n  void set_foreground_color( int num );\n  void set_background_color( int num );\n  void set_rendition( color_type num );\n  std::string sgr( void ) const;\n\n  static unsigned int make_true_color( unsigned int r, unsigned int g, unsigned int b )\n  {\n    return true_color_mask | ( r << 16 ) | ( g << 8 ) | b;\n  }\n\n  static bool is_true_color( unsigned int color ) { return ( color & true_color_mask ) != 0; }\n\n  // unsigned int get_foreground_rendition() const { return foreground_color; }\n  unsigned int get_background_rendition() const { return background_color; }\n\n  bool operator==( const Renditions& x ) const\n  {\n    return ( attributes == x.attributes ) && ( foreground_color == x.foreground_color )\n           && ( background_color == x.background_color );\n  }\n  void set_attribute( attribute_type attr, bool val )\n  {\n    attributes = val ? ( attributes | ( 1 << attr ) ) : ( attributes & ~( 1 << attr ) );\n  }\n  bool get_attribute( attribute_type attr ) const { return attributes & ( 1 << attr ); }\n  void clear_attributes() { attributes = 0; }\n};\n\nclass Cell\n{\nprivate:\n  typedef std::string content_type; /* can be std::string, std::vector<uint8_t>, or __gnu_cxx::__vstring */\n  content_type contents;\n  Renditions renditions;\n  unsigned int wide : 1;     /* 0 = narrow, 1 = wide */\n  unsigned int fallback : 1; /* first character is combining character */\n  unsigned int wrap : 1;\n\nprivate:\n  Cell();\n\npublic:\n  Cell( color_type background_color );\n\n  void reset( color_type background_color );\n\n  bool operator==( const Cell& x ) const\n  {\n    return ( ( contents == x.contents ) && ( fallback == x.fallback ) && ( wide == x.wide )\n             && ( renditions == x.renditions ) && ( wrap == x.wrap ) );\n  }\n\n  bool operator!=( const Cell& x ) const { return !operator==( x ); }\n\n  /* Accessors for contents field */\n  std::string debug_contents( void ) const;\n\n  bool empty( void ) const { return contents.empty(); }\n  /* 32 seems like a reasonable limit on combining characters */\n  bool full( void ) const { return contents.size() >= 32; }\n  void clear( void ) { contents.clear(); }\n\n  bool is_blank( void ) const\n  {\n    // XXX fix.\n    return ( contents.empty() || contents == \" \" || contents == \"\\xC2\\xA0\" );\n  }\n\n  bool contents_match( const Cell& other ) const\n  {\n    return ( is_blank() && other.is_blank() ) || ( contents == other.contents );\n  }\n\n  bool compare( const Cell& other ) const;\n\n  // Is this a printing ISO 8859-1 character?\n  static bool isprint_iso8859_1( const wchar_t c )\n  {\n    return ( c <= 0xff && c >= 0xa0 ) || ( c <= 0x7e && c >= 0x20 );\n  }\n\n  static void append_to_str( std::string& dest, const wchar_t c )\n  {\n    /* ASCII?  Cheat. */\n    if ( static_cast<uint32_t>( c ) <= 0x7f ) {\n      dest.push_back( static_cast<char>( c ) );\n      return;\n    }\n    static mbstate_t ps = mbstate_t();\n    char tmp[MB_LEN_MAX];\n    size_t ignore = wcrtomb( NULL, 0, &ps );\n    (void)ignore;\n    size_t len = wcrtomb( tmp, c, &ps );\n    dest.append( tmp, len );\n  }\n\n  void append( const wchar_t c )\n  {\n    /* ASCII?  Cheat. */\n    if ( static_cast<uint32_t>( c ) <= 0x7f ) {\n      contents.push_back( static_cast<char>( c ) );\n      return;\n    }\n    static mbstate_t ps = mbstate_t();\n    char tmp[MB_LEN_MAX];\n    size_t ignore = wcrtomb( NULL, 0, &ps );\n    (void)ignore;\n    size_t len = wcrtomb( tmp, c, &ps );\n    contents.insert( contents.end(), tmp, tmp + len );\n  }\n\n  void print_grapheme( std::string& output ) const\n  {\n    if ( contents.empty() ) {\n      output.append( 1, ' ' );\n      return;\n    }\n    /*\n     * cells that begin with combining character get combiner\n     * attached to no-break space\n     */\n    if ( fallback ) {\n      output.append( \"\\xC2\\xA0\" );\n    }\n    output.append( contents );\n  }\n\n  /* Other accessors */\n  const Renditions& get_renditions( void ) const { return renditions; }\n  Renditions& get_renditions( void ) { return renditions; }\n  void set_renditions( const Renditions& r ) { renditions = r; }\n  bool get_wide( void ) const { return wide; }\n  void set_wide( bool w ) { wide = w; }\n  unsigned int get_width( void ) const { return wide + 1; }\n  bool get_fallback( void ) const { return fallback; }\n  void set_fallback( bool f ) { fallback = f; }\n  bool get_wrap( void ) const { return wrap; }\n  void set_wrap( bool f ) { wrap = f; }\n};\n\nclass Row\n{\npublic:\n  typedef std::vector<Cell> cells_type;\n  cells_type cells;\n  // gen is a generation counter.  It can be used to quickly rule\n  // out the possibility of two rows being identical; this is useful\n  // in scrolling.\n  uint64_t gen;\n\nprivate:\n  Row();\n\npublic:\n  Row( const size_t s_width, const color_type background_color );\n\n  void insert_cell( int col, color_type background_color );\n  void delete_cell( int col, color_type background_color );\n\n  void reset( color_type background_color );\n\n  bool operator==( const Row& x ) const { return ( gen == x.gen && cells == x.cells ); }\n\n  bool get_wrap( void ) const { return cells.back().get_wrap(); }\n  void set_wrap( bool w ) { cells.back().set_wrap( w ); }\n\n  uint64_t get_gen() const;\n};\n\nclass SavedCursor\n{\npublic:\n  int cursor_col, cursor_row;\n  Renditions renditions;\n  /* not implemented: character set shift state */\n  bool auto_wrap_mode;\n  bool origin_mode;\n  /* not implemented: state of selective erase */\n\n  SavedCursor();\n};\n\nclass DrawState\n{\nprivate:\n  int width, height;\n\n  void new_grapheme( void );\n  void snap_cursor_to_border( void );\n\n  int cursor_col, cursor_row;\n  int combining_char_col, combining_char_row;\n\n  bool default_tabs;\n  std::vector<bool> tabs;\n\n  void reinitialize_tabs( unsigned int start );\n\n  int scrolling_region_top_row, scrolling_region_bottom_row;\n\n  Renditions renditions;\n\n  SavedCursor save;\n\npublic:\n  bool next_print_will_wrap;\n  bool origin_mode;\n  bool auto_wrap_mode;\n  bool insert_mode;\n  bool cursor_visible;\n  bool reverse_video;\n  bool bracketed_paste;\n\n  enum MouseReportingMode\n  {\n    MOUSE_REPORTING_NONE = 0,\n    MOUSE_REPORTING_X10 = 9,\n    MOUSE_REPORTING_VT220 = 1000,\n    MOUSE_REPORTING_VT220_HILIGHT = 1001,\n    MOUSE_REPORTING_BTN_EVENT = 1002,\n    MOUSE_REPORTING_ANY_EVENT = 1003\n  } mouse_reporting_mode;\n\n  bool mouse_focus_event;      // 1004\n  bool mouse_alternate_scroll; // 1007\n\n  enum MouseEncodingMode\n  {\n    MOUSE_ENCODING_DEFAULT = 0,\n    MOUSE_ENCODING_UTF8 = 1005,\n    MOUSE_ENCODING_SGR = 1006,\n    MOUSE_ENCODING_URXVT = 1015\n  } mouse_encoding_mode;\n\n  bool application_mode_cursor_keys;\n\n  /* bold, etc. */\n\n  void move_row( int N, bool relative = false );\n  void move_col( int N, bool relative = false, bool implicit = false );\n\n  int get_cursor_col( void ) const { return cursor_col; }\n  int get_cursor_row( void ) const { return cursor_row; }\n  int get_combining_char_col( void ) const { return combining_char_col; }\n  int get_combining_char_row( void ) const { return combining_char_row; }\n  int get_width( void ) const { return width; }\n  int get_height( void ) const { return height; }\n\n  void set_tab( void );\n  void clear_tab( int col );\n  void clear_default_tabs( void ) { default_tabs = false; }\n  /* Default tabs can't be restored without resetting the draw state. */\n  int get_next_tab( int count ) const;\n\n  void set_scrolling_region( int top, int bottom );\n\n  int get_scrolling_region_top_row( void ) const { return scrolling_region_top_row; }\n  int get_scrolling_region_bottom_row( void ) const { return scrolling_region_bottom_row; }\n\n  int limit_top( void ) const;\n  int limit_bottom( void ) const;\n\n  void set_foreground_color( int x ) { renditions.set_foreground_color( x ); }\n  void set_background_color( int x ) { renditions.set_background_color( x ); }\n  void add_rendition( color_type x ) { renditions.set_rendition( x ); }\n  const Renditions& get_renditions( void ) const { return renditions; }\n  Renditions& get_renditions( void ) { return renditions; }\n  int get_background_rendition( void ) const { return renditions.get_background_rendition(); }\n\n  void save_cursor( void );\n  void restore_cursor( void );\n  void clear_saved_cursor( void ) { save = SavedCursor(); }\n\n  void resize( int s_width, int s_height );\n\n  DrawState( int s_width, int s_height );\n\n  bool operator==( const DrawState& x ) const\n  {\n    /* only compare fields that affect display */\n    return ( width == x.width ) && ( height == x.height ) && ( cursor_col == x.cursor_col )\n           && ( cursor_row == x.cursor_row ) && ( cursor_visible == x.cursor_visible )\n           && ( reverse_video == x.reverse_video ) && ( renditions == x.renditions )\n           && ( bracketed_paste == x.bracketed_paste ) && ( mouse_reporting_mode == x.mouse_reporting_mode )\n           && ( mouse_focus_event == x.mouse_focus_event ) && ( mouse_alternate_scroll == x.mouse_alternate_scroll )\n           && ( mouse_encoding_mode == x.mouse_encoding_mode );\n  }\n};\n\nclass Framebuffer\n{\n  // To minimize copying of rows and cells, we use shared_ptr to\n  // share unchanged rows between multiple Framebuffers.  If we\n  // write to a row in a Framebuffer and it is shared with other\n  // owners, we copy it first.  The shared_ptr naturally manages the\n  // usage of the actual rows themselves.\n  //\n  // We gain a couple of free extras by doing this:\n  //\n  // * A quick check for equality between rows in different\n  // Framebuffers is to simply compare the pointer values.  If they\n  // are equal, then the rows are obviously identical.\n  // * If no row is shared, the frame has not been modified.\npublic:\n  typedef std::vector<wchar_t> title_type;\n  typedef std::shared_ptr<Row> row_pointer;\n  typedef std::vector<row_pointer> rows_type; /* can be either std::vector or std::deque */\n\nprivate:\n  rows_type rows;\n  title_type icon_name;\n  title_type window_title;\n  title_type clipboard;\n  unsigned int bell_count;\n  bool title_initialized; /* true if the window title has been set via an OSC */\n\n  row_pointer newrow( void )\n  {\n    const size_t w = ds.get_width();\n    const color_type c = ds.get_background_rendition();\n    return std::make_shared<Row>( w, c );\n  }\n\npublic:\n  Framebuffer( int s_width, int s_height );\n  Framebuffer( const Framebuffer& other );\n  Framebuffer& operator=( const Framebuffer& other );\n  DrawState ds;\n\n  const rows_type& get_rows() const { return rows; }\n\n  void scroll( int N );\n  void move_rows_autoscroll( int rows );\n\n  inline const Row* get_row( int row ) const\n  {\n    if ( row == -1 )\n      row = ds.get_cursor_row();\n\n    return rows.at( row ).get();\n  }\n\n  inline const Cell* get_cell( int row = -1, int col = -1 ) const\n  {\n    if ( row == -1 )\n      row = ds.get_cursor_row();\n    if ( col == -1 )\n      col = ds.get_cursor_col();\n\n    return &rows.at( row )->cells.at( col );\n  }\n\n  Row* get_mutable_row( int row )\n  {\n    if ( row == -1 )\n      row = ds.get_cursor_row();\n    row_pointer& mutable_row = rows.at( row );\n    // If the row is shared, copy it. This is only safe because mosh isn't\n    // multi-threaded.\n    if ( mutable_row.use_count() > 1 ) {\n      mutable_row = std::make_shared<Row>( *mutable_row );\n    }\n    return mutable_row.get();\n  }\n\n  Cell* get_mutable_cell( int row = -1, int col = -1 )\n  {\n    if ( row == -1 )\n      row = ds.get_cursor_row();\n    if ( col == -1 )\n      col = ds.get_cursor_col();\n\n    return &get_mutable_row( row )->cells.at( col );\n  }\n\n  Cell* get_combining_cell( void );\n\n  void apply_renditions_to_cell( Cell* cell );\n\n  void insert_line( int before_row, int count );\n  void delete_line( int row, int count );\n\n  void insert_cell( int row, int col );\n  void delete_cell( int row, int col );\n\n  void reset( void );\n  void soft_reset( void );\n\n  void set_title_initialized( void ) { title_initialized = true; }\n  bool is_title_initialized( void ) const { return title_initialized; }\n  void set_icon_name( const title_type& s ) { icon_name = s; }\n  void set_window_title( const title_type& s ) { window_title = s; }\n  void set_clipboard( const title_type& s ) { clipboard = s; }\n  const title_type& get_icon_name( void ) const { return icon_name; }\n  const title_type& get_window_title( void ) const { return window_title; }\n  const title_type& get_clipboard( void ) const { return clipboard; }\n\n  void prefix_window_title( const title_type& s );\n\n  void resize( int s_width, int s_height );\n\n  void reset_cell( Cell* c ) { c->reset( ds.get_background_rendition() ); }\n  void reset_row( Row* r ) { r->reset( ds.get_background_rendition() ); }\n\n  void ring_bell( void ) { bell_count++; }\n  unsigned int get_bell_count( void ) const { return bell_count; }\n\n  bool operator==( const Framebuffer& x ) const\n  {\n    return ( rows == x.rows ) && ( window_title == x.window_title ) && ( clipboard == x.clipboard )\n           && ( bell_count == x.bell_count ) && ( ds == x.ds );\n  }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/terminal/terminalfunctions.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <algorithm>\n#include <cstdio>\n#include <string>\n\n#include <unistd.h>\n\n#include \"src/terminal/parseraction.h\"\n#include \"src/terminal/terminalframebuffer.h\"\n#include \"terminaldispatcher.h\"\n\nusing namespace Terminal;\n\n/* Terminal functions -- routines activated by CSI, escape or a control char */\n\nstatic void clearline( Framebuffer* fb, int row, int start, int end )\n{\n  for ( int col = start; col <= end; col++ ) {\n    fb->reset_cell( fb->get_mutable_cell( row, col ) );\n  }\n}\n\n/* erase in line */\nstatic void CSI_EL( Framebuffer* fb, Dispatcher* dispatch )\n{\n  switch ( dispatch->getparam( 0, 0 ) ) {\n    case 0: /* default: active position to end of line, inclusive */\n      clearline( fb, -1, fb->ds.get_cursor_col(), fb->ds.get_width() - 1 );\n      break;\n    case 1: /* start of screen to active position, inclusive */\n      clearline( fb, -1, 0, fb->ds.get_cursor_col() );\n      break;\n    case 2: /* all of line */\n      fb->reset_row( fb->get_mutable_row( -1 ) );\n      break;\n    default:\n      break;\n  }\n}\n\nstatic Function func_CSI_EL( CSI, \"K\", CSI_EL );\n\n/* erase in display */\nstatic void CSI_ED( Framebuffer* fb, Dispatcher* dispatch )\n{\n  switch ( dispatch->getparam( 0, 0 ) ) {\n    case 0: /* active position to end of screen, inclusive */\n      clearline( fb, -1, fb->ds.get_cursor_col(), fb->ds.get_width() - 1 );\n      for ( int y = fb->ds.get_cursor_row() + 1; y < fb->ds.get_height(); y++ ) {\n        fb->reset_row( fb->get_mutable_row( y ) );\n      }\n      break;\n    case 1: /* start of screen to active position, inclusive */\n      for ( int y = 0; y < fb->ds.get_cursor_row(); y++ ) {\n        fb->reset_row( fb->get_mutable_row( y ) );\n      }\n      clearline( fb, -1, 0, fb->ds.get_cursor_col() );\n      break;\n    case 2: /* entire screen */\n      for ( int y = 0; y < fb->ds.get_height(); y++ ) {\n        fb->reset_row( fb->get_mutable_row( y ) );\n      }\n      break;\n    default:\n      break;\n  }\n}\n\nstatic Function func_CSI_ED( CSI, \"J\", CSI_ED );\n\n/* cursor movement -- relative and absolute */\nstatic void CSI_cursormove( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int num = dispatch->getparam( 0, 1 );\n\n  switch ( dispatch->get_dispatch_chars()[0] ) {\n    case 'A':\n      fb->ds.move_row( -num, true );\n      break;\n    case 'B':\n      fb->ds.move_row( num, true );\n      break;\n    case 'C':\n      fb->ds.move_col( num, true );\n      break;\n    case 'D':\n      fb->ds.move_col( -num, true );\n      break;\n    case 'H':\n    case 'f':\n      fb->ds.move_row( dispatch->getparam( 0, 1 ) - 1 );\n      fb->ds.move_col( dispatch->getparam( 1, 1 ) - 1 );\n      break;\n    default:\n      break;\n  }\n}\n\nstatic Function func_CSI_cursormove_A( CSI, \"A\", CSI_cursormove );\nstatic Function func_CSI_cursormove_B( CSI, \"B\", CSI_cursormove );\nstatic Function func_CSI_cursormove_C( CSI, \"C\", CSI_cursormove );\nstatic Function func_CSI_cursormove_D( CSI, \"D\", CSI_cursormove );\nstatic Function func_CSI_cursormove_H( CSI, \"H\", CSI_cursormove );\nstatic Function func_CSI_cursormove_f( CSI, \"f\", CSI_cursormove );\n\n/* device attributes */\nstatic void CSI_DA( Framebuffer* fb __attribute( ( unused ) ), Dispatcher* dispatch )\n{\n  dispatch->terminal_to_host.append( \"\\033[?62c\" ); /* plain vt220 */\n}\n\nstatic Function func_CSI_DA( CSI, \"c\", CSI_DA );\n\n/* secondary device attributes */\nstatic void CSI_SDA( Framebuffer* fb __attribute( ( unused ) ), Dispatcher* dispatch )\n{\n  dispatch->terminal_to_host.append( \"\\033[>1;10;0c\" ); /* plain vt220 */\n}\n\nstatic Function func_CSI_SDA( CSI, \">c\", CSI_SDA );\n\n/* screen alignment diagnostic */\nstatic void Esc_DECALN( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  for ( int y = 0; y < fb->ds.get_height(); y++ ) {\n    for ( int x = 0; x < fb->ds.get_width(); x++ ) {\n      fb->reset_cell( fb->get_mutable_cell( y, x ) );\n      fb->get_mutable_cell( y, x )->append( 'E' );\n    }\n  }\n}\n\nstatic Function func_Esc_DECALN( ESCAPE, \"#8\", Esc_DECALN );\n\n/* line feed */\nstatic void Ctrl_LF( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->move_rows_autoscroll( 1 );\n}\n\n/* same procedure for index, vertical tab, and form feed control codes */\nstatic Function func_Ctrl_LF( CONTROL, \"\\x0a\", Ctrl_LF );\nstatic Function func_Ctrl_IND( CONTROL, \"\\x84\", Ctrl_LF );\nstatic Function func_Ctrl_VT( CONTROL, \"\\x0b\", Ctrl_LF );\nstatic Function func_Ctrl_FF( CONTROL, \"\\x0c\", Ctrl_LF );\n\n/* carriage return */\nstatic void Ctrl_CR( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ds.move_col( 0 );\n}\n\nstatic Function func_Ctrl_CR( CONTROL, \"\\x0d\", Ctrl_CR );\n\n/* backspace */\nstatic void Ctrl_BS( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ds.move_col( -1, true );\n}\n\nstatic Function func_Ctrl_BS( CONTROL, \"\\x08\", Ctrl_BS );\n\n/* reverse index -- like a backwards line feed */\nstatic void Ctrl_RI( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->move_rows_autoscroll( -1 );\n}\n\nstatic Function func_Ctrl_RI( CONTROL, \"\\x8D\", Ctrl_RI );\n\n/* newline */\nstatic void Ctrl_NEL( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ds.move_col( 0 );\n  fb->move_rows_autoscroll( 1 );\n}\n\nstatic Function func_Ctrl_NEL( CONTROL, \"\\x85\", Ctrl_NEL );\n\n/* horizontal tab */\nstatic void HT_n( Framebuffer* fb, size_t count )\n{\n  int col = fb->ds.get_next_tab( count );\n  if ( col == -1 ) { /* no tabs, go to end of line */\n    col = fb->ds.get_width() - 1;\n  }\n\n  /* A horizontal tab is the only operation that preserves but\n     does not set the wrap state. It also starts a new grapheme. */\n\n  bool wrap_state_save = fb->ds.next_print_will_wrap;\n  fb->ds.move_col( col, false );\n  fb->ds.next_print_will_wrap = wrap_state_save;\n}\n\nstatic void Ctrl_HT( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  HT_n( fb, 1 );\n}\nstatic Function func_Ctrl_HT( CONTROL, \"\\x09\", Ctrl_HT, false );\n\nstatic void CSI_CxT( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int param = dispatch->getparam( 0, 1 );\n  if ( dispatch->get_dispatch_chars()[0] == 'Z' ) {\n    param = -param;\n  }\n  if ( param == 0 ) {\n    return;\n  }\n  HT_n( fb, param );\n}\n\nstatic Function func_CSI_CHT( CSI, \"I\", CSI_CxT, false );\nstatic Function func_CSI_CBT( CSI, \"Z\", CSI_CxT, false );\n\n/* horizontal tab set */\nstatic void Ctrl_HTS( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ds.set_tab();\n}\n\nstatic Function func_Ctrl_HTS( CONTROL, \"\\x88\", Ctrl_HTS );\n\n/* tabulation clear */\nstatic void CSI_TBC( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int param = dispatch->getparam( 0, 0 );\n  switch ( param ) {\n    case 0: /* clear this tab stop */\n      fb->ds.clear_tab( fb->ds.get_cursor_col() );\n      break;\n    case 3: /* clear all tab stops */\n      fb->ds.clear_default_tabs();\n      for ( int x = 0; x < fb->ds.get_width(); x++ ) {\n        fb->ds.clear_tab( x );\n      }\n      break;\n    default:\n      break;\n  }\n}\n\n/* TBC preserves wrap state */\nstatic Function func_CSI_TBC( CSI, \"g\", CSI_TBC, false );\n\nstatic bool* get_DEC_mode( int param, Framebuffer* fb )\n{\n  switch ( param ) {\n    case 1: /* cursor key mode */\n      return &( fb->ds.application_mode_cursor_keys );\n    case 3: /* 80/132. Ignore but clear screen. */\n      /* clear screen */\n      fb->ds.move_row( 0 );\n      fb->ds.move_col( 0 );\n      for ( int y = 0; y < fb->ds.get_height(); y++ ) {\n        fb->reset_row( fb->get_mutable_row( y ) );\n      }\n      return NULL;\n    case 5: /* reverse video */\n      return &( fb->ds.reverse_video );\n    case 6: /* origin */\n      fb->ds.move_row( 0 );\n      fb->ds.move_col( 0 );\n      return &( fb->ds.origin_mode );\n    case 7: /* auto wrap */\n      return &( fb->ds.auto_wrap_mode );\n    case 25:\n      return &( fb->ds.cursor_visible );\n    case 1004: /* xterm mouse focus event */\n      return &( fb->ds.mouse_focus_event );\n    case 1007: /* xterm mouse alternate scroll */\n      return &( fb->ds.mouse_alternate_scroll );\n    case 2004: /* bracketed paste */\n      return &( fb->ds.bracketed_paste );\n    default:\n      break;\n  }\n  return NULL;\n}\n\n/* helper for CSI_DECSM and CSI_DECRM */\nstatic void set_if_available( bool* mode, bool value )\n{\n  if ( mode ) {\n    *mode = value;\n  }\n}\n\n/* set private mode */\nstatic void CSI_DECSM( Framebuffer* fb, Dispatcher* dispatch )\n{\n  for ( int i = 0; i < dispatch->param_count(); i++ ) {\n    int param = dispatch->getparam( i, 0 );\n    if ( param == 9 || ( param >= 1000 && param <= 1003 ) ) {\n      fb->ds.mouse_reporting_mode = (Terminal::DrawState::MouseReportingMode)param;\n    } else if ( param == 1005 || param == 1006 || param == 1015 ) {\n      fb->ds.mouse_encoding_mode = (Terminal::DrawState::MouseEncodingMode)param;\n    } else {\n      set_if_available( get_DEC_mode( param, fb ), true );\n    }\n  }\n}\n\n/* clear private mode */\nstatic void CSI_DECRM( Framebuffer* fb, Dispatcher* dispatch )\n{\n  for ( int i = 0; i < dispatch->param_count(); i++ ) {\n    int param = dispatch->getparam( i, 0 );\n    if ( param == 9 || ( param >= 1000 && param <= 1003 ) ) {\n      fb->ds.mouse_reporting_mode = Terminal::DrawState::MOUSE_REPORTING_NONE;\n    } else if ( param == 1005 || param == 1006 || param == 1015 ) {\n      fb->ds.mouse_encoding_mode = Terminal::DrawState::MOUSE_ENCODING_DEFAULT;\n    } else {\n      set_if_available( get_DEC_mode( param, fb ), false );\n    }\n  }\n}\n\n/* These functions don't clear wrap state. */\nstatic Function func_CSI_DECSM( CSI, \"?h\", CSI_DECSM, false );\nstatic Function func_CSI_DECRM( CSI, \"?l\", CSI_DECRM, false );\n\nstatic bool* get_ANSI_mode( int param, Framebuffer* fb )\n{\n  if ( param == 4 ) { /* insert/replace mode */\n    return &( fb->ds.insert_mode );\n  }\n  return NULL;\n}\n\n/* set mode */\nstatic void CSI_SM( Framebuffer* fb, Dispatcher* dispatch )\n{\n  for ( int i = 0; i < dispatch->param_count(); i++ ) {\n    bool* mode = get_ANSI_mode( dispatch->getparam( i, 0 ), fb );\n    if ( mode ) {\n      *mode = true;\n    }\n  }\n}\n\n/* clear mode */\nstatic void CSI_RM( Framebuffer* fb, Dispatcher* dispatch )\n{\n  for ( int i = 0; i < dispatch->param_count(); i++ ) {\n    bool* mode = get_ANSI_mode( dispatch->getparam( i, 0 ), fb );\n    if ( mode ) {\n      *mode = false;\n    }\n  }\n}\n\nstatic Function func_CSI_SM( CSI, \"h\", CSI_SM );\nstatic Function func_CSI_RM( CSI, \"l\", CSI_RM );\n\n/* set top and bottom margins */\nstatic void CSI_DECSTBM( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int top = dispatch->getparam( 0, 1 );\n  int bottom = dispatch->getparam( 1, fb->ds.get_height() );\n\n  if ( ( bottom <= top ) || ( top > fb->ds.get_height() ) || ( top == 0 && bottom == 1 ) ) {\n    return; /* invalid, xterm ignores */\n  }\n\n  fb->ds.set_scrolling_region( top - 1, bottom - 1 );\n  fb->ds.move_row( 0 );\n  fb->ds.move_col( 0 );\n}\n\nstatic Function func_CSI_DECSTMB( CSI, \"r\", CSI_DECSTBM );\n\n/* terminal bell */\nstatic void Ctrl_BEL( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ring_bell();\n}\n\nstatic Function func_Ctrl_BEL( CONTROL, \"\\x07\", Ctrl_BEL );\n\n/* select graphics rendition -- e.g., bold, blinking, etc. */\nstatic void CSI_SGR( Framebuffer* fb, Dispatcher* dispatch )\n{\n  for ( int i = 0; i < dispatch->param_count(); i++ ) {\n    int rendition = dispatch->getparam( i, 0 );\n    /* We need to special-case the handling of [34]8 ; 5 ; Ps,\n       because Ps of 0 in that case does not mean reset to default, even\n       though it means that otherwise (as usually renditions are applied\n       in order). */\n    if ( ( rendition == 38 || rendition == 48 ) && ( dispatch->param_count() - i >= 3 )\n         && ( dispatch->getparam( i + 1, -1 ) == 5 ) ) {\n      ( rendition == 38 ) ? fb->ds.set_foreground_color( dispatch->getparam( i + 2, 0 ) )\n                          : fb->ds.set_background_color( dispatch->getparam( i + 2, 0 ) );\n      i += 2;\n      continue;\n    }\n\n    /* True color support: ESC[ ... [34]8;2;<r>;<g>;<b> ... m */\n    if ( ( rendition == 38 || rendition == 48 ) && ( dispatch->param_count() - i >= 5 )\n         && ( dispatch->getparam( i + 1, -1 ) == 2 ) ) {\n      unsigned int red = dispatch->getparam( i + 2, 0 );\n      unsigned int green = dispatch->getparam( i + 3, 0 );\n      unsigned int blue = dispatch->getparam( i + 4, 0 );\n      unsigned int color;\n\n      color = Renditions::make_true_color( red, green, blue );\n\n      if ( rendition == 38 ) {\n        fb->ds.set_foreground_color( color );\n      } else {\n        fb->ds.set_background_color( color );\n      }\n      i += 4;\n      continue;\n    }\n\n    fb->ds.add_rendition( rendition );\n  }\n}\n\nstatic Function func_CSI_SGR( CSI, \"m\", CSI_SGR, false ); /* changing renditions doesn't clear wrap flag */\n\n/* save and restore cursor */\nstatic void Esc_DECSC( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ds.save_cursor();\n}\n\nstatic void Esc_DECRC( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->ds.restore_cursor();\n}\n\nstatic Function func_Esc_DECSC( ESCAPE, \"7\", Esc_DECSC );\nstatic Function func_Esc_DECRC( ESCAPE, \"8\", Esc_DECRC );\n\n/* device status report -- e.g., cursor position (used by resize) */\nstatic void CSI_DSR( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int param = dispatch->getparam( 0, 0 );\n\n  switch ( param ) {\n    case 5: /* device status report requested */\n      dispatch->terminal_to_host.append( \"\\033[0n\" );\n      break;\n    case 6: /* report of active position requested */\n      char cpr[32];\n      snprintf( cpr, 32, \"\\033[%d;%dR\", fb->ds.get_cursor_row() + 1, fb->ds.get_cursor_col() + 1 );\n      dispatch->terminal_to_host.append( cpr );\n      break;\n    default:\n      break;\n  }\n}\n\nstatic Function func_CSI_DSR( CSI, \"n\", CSI_DSR );\n\n/* insert line */\nstatic void CSI_IL( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int lines = dispatch->getparam( 0, 1 );\n\n  fb->insert_line( fb->ds.get_cursor_row(), lines );\n\n  /* vt220 manual and Ecma-48 say to move to first column */\n  /* but xterm and gnome-terminal don't */\n  fb->ds.move_col( 0 );\n}\n\nstatic Function func_CSI_IL( CSI, \"L\", CSI_IL );\n\n/* delete line */\nstatic void CSI_DL( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int lines = dispatch->getparam( 0, 1 );\n\n  fb->delete_line( fb->ds.get_cursor_row(), lines );\n\n  /* same story -- xterm and gnome-terminal don't\n     move to first column */\n  fb->ds.move_col( 0 );\n}\n\nstatic Function func_CSI_DL( CSI, \"M\", CSI_DL );\n\n/* insert characters */\nstatic void CSI_ICH( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int cells = dispatch->getparam( 0, 1 );\n\n  for ( int i = 0; i < cells; i++ ) {\n    fb->insert_cell( fb->ds.get_cursor_row(), fb->ds.get_cursor_col() );\n  }\n}\n\nstatic Function func_CSI_ICH( CSI, \"@\", CSI_ICH );\n\n/* delete character */\nstatic void CSI_DCH( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int cells = dispatch->getparam( 0, 1 );\n\n  for ( int i = 0; i < cells; i++ ) {\n    fb->delete_cell( fb->ds.get_cursor_row(), fb->ds.get_cursor_col() );\n  }\n}\n\nstatic Function func_CSI_DCH( CSI, \"P\", CSI_DCH );\n\n/* line position absolute */\nstatic void CSI_VPA( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int row = dispatch->getparam( 0, 1 );\n  fb->ds.move_row( row - 1 );\n}\n\nstatic Function func_CSI_VPA( CSI, \"d\", CSI_VPA );\n\n/* character position absolute */\nstatic void CSI_HPA( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int col = dispatch->getparam( 0, 1 );\n  fb->ds.move_col( col - 1 );\n}\n\nstatic Function func_CSI_CHA( CSI, \"G\", CSI_HPA );    /* ECMA-48 name: CHA */\nstatic Function func_CSI_HPA( CSI, \"\\x60\", CSI_HPA ); /* ECMA-48 name: HPA */\n\n/* erase character */\nstatic void CSI_ECH( Framebuffer* fb, Dispatcher* dispatch )\n{\n  int num = dispatch->getparam( 0, 1 );\n  int limit = fb->ds.get_cursor_col() + num - 1;\n  if ( limit >= fb->ds.get_width() ) {\n    limit = fb->ds.get_width() - 1;\n  }\n\n  clearline( fb, -1, fb->ds.get_cursor_col(), limit );\n}\n\nstatic Function func_CSI_ECH( CSI, \"X\", CSI_ECH );\n\n/* reset to initial state */\nstatic void Esc_RIS( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->reset();\n}\n\nstatic Function func_Esc_RIS( ESCAPE, \"c\", Esc_RIS );\n\n/* soft reset */\nstatic void CSI_DECSTR( Framebuffer* fb, Dispatcher* dispatch __attribute( ( unused ) ) )\n{\n  fb->soft_reset();\n}\n\nstatic Function func_CSI_DECSTR( CSI, \"!p\", CSI_DECSTR );\n\n/* xterm uses an Operating System Command to set the window title */\nvoid Dispatcher::OSC_dispatch( const Parser::OSC_End* act __attribute( ( unused ) ), Framebuffer* fb )\n{\n  /* handle osc copy clipboard sequence 52;c; */\n  if ( OSC_string.size() >= 5 && OSC_string[0] == L'5' && OSC_string[1] == L'2' && OSC_string[2] == L';'\n       && OSC_string[3] == L'c' && OSC_string[4] == L';' ) {\n    Terminal::Framebuffer::title_type clipboard( OSC_string.begin() + 5, OSC_string.end() );\n    fb->set_clipboard( clipboard );\n    /* handle osc terminal title sequence */\n  } else if ( OSC_string.size() >= 1 ) {\n    long cmd_num = -1;\n    int offset = 0;\n    if ( OSC_string[0] == L';' ) {\n      /* OSC of the form \"\\033];<title>\\007\" */\n      cmd_num = 0; /* treat it as as a zero */\n      offset = 1;\n    } else if ( ( OSC_string.size() >= 2 ) && ( OSC_string[1] == L';' ) ) {\n      /* OSC of the form \"\\033]X;<title>\\007\" where X can be:\n       * 0: set icon name and window title\n       * 1: set icon name\n       * 2: set window title */\n      cmd_num = OSC_string[0] - L'0';\n      offset = 2;\n    }\n    bool set_icon = cmd_num == 0 || cmd_num == 1;\n    bool set_title = cmd_num == 0 || cmd_num == 2;\n    if ( set_icon || set_title ) {\n      fb->set_title_initialized();\n      int title_length = std::min( OSC_string.size(), (size_t)256 );\n      Terminal::Framebuffer::title_type newtitle( OSC_string.begin() + offset, OSC_string.begin() + title_length );\n      if ( set_icon ) {\n        fb->set_icon_name( newtitle );\n      }\n      if ( set_title ) {\n        fb->set_window_title( newtitle );\n      }\n    }\n  }\n}\n\n/* scroll down or terminfo indn */\nstatic void CSI_SD( Framebuffer* fb, Dispatcher* dispatch )\n{\n  fb->scroll( dispatch->getparam( 0, 1 ) );\n}\n\nstatic Function func_CSI_SD( CSI, \"S\", CSI_SD );\n\n/* scroll up or terminfo rin */\nstatic void CSI_SU( Framebuffer* fb, Dispatcher* dispatch )\n{\n  fb->scroll( -dispatch->getparam( 0, 1 ) );\n}\n\nstatic Function func_CSI_SU( CSI, \"T\", CSI_SU );\n"
  },
  {
    "path": "src/terminal/terminaluserinput.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cassert>\n\n#include \"terminaluserinput.h\"\n\nusing namespace Terminal;\n\nstd::string UserInput::input( const Parser::UserByte* act, bool application_mode_cursor_keys )\n{\n  /* The user will always be in application mode. If stm is not in\n     application mode, convert user's cursor control function to an\n     ANSI cursor control sequence */\n\n  /* We need to look ahead one byte in the SS3 state to see if\n     the next byte will be A, B, C, or D (cursor control keys). */\n\n  /* This doesn't handle the 8-bit SS3 C1 control, which would be\n     two octets in UTF-8. Fortunately nobody seems to send this. */\n\n  switch ( state ) {\n    case Ground:\n      if ( act->c == 0x1b ) { /* ESC */\n        state = ESC;\n      }\n      return std::string( &act->c, 1 );\n\n    case ESC:\n      if ( act->c == 'O' ) { /* ESC O = 7-bit SS3 */\n        state = SS3;\n        return std::string();\n      }\n      state = Ground;\n      return std::string( &act->c, 1 );\n\n    case SS3:\n      state = Ground;\n      if ( ( !application_mode_cursor_keys ) && ( act->c >= 'A' ) && ( act->c <= 'D' ) ) {\n        char translated_cursor[2] = { '[', act->c };\n        return std::string( translated_cursor, 2 );\n      } else {\n        char original_cursor[2] = { 'O', act->c };\n        return std::string( original_cursor, 2 );\n      }\n\n    default:\n      assert( !\"unexpected state\" );\n      state = Ground;\n      return std::string();\n  }\n}\n"
  },
  {
    "path": "src/terminal/terminaluserinput.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TERMINALUSERINPUT_HPP\n#define TERMINALUSERINPUT_HPP\n\n#include \"src/terminal/parseraction.h\"\n#include <string>\n\nnamespace Terminal {\nclass UserInput\n{\npublic:\n  enum UserInputState\n  {\n    Ground,\n    ESC,\n    SS3\n  };\n\nprivate:\n  UserInputState state;\n\npublic:\n  UserInput() : state( Ground ) {}\n\n  std::string input( const Parser::UserByte* act, bool application_mode_cursor_keys );\n\n  bool operator==( const UserInput& x ) const { return state == x.state; }\n};\n}\n\n#endif\n"
  },
  {
    "path": "src/tests/.gitignore",
    "content": "/base64\n/base64_vector.cc\n/ocb-aes\n/encrypt-decrypt\n/nonce-incr\n/inpty\n/is-utf8-locale\n/*.d/\n*.log\n*.trs\n"
  },
  {
    "path": "src/tests/Makefile.am",
    "content": "EXTRA_DIST = \\\n\thold-stdin print-exitstatus \\\n\te2e-test e2e-test-server \\\n\te2e-test-subrs \\\n\tmosh-client mosh-server \\\n\tlocal.test \\\n\t$(displaytests) \\\n\temulation-attributes.test\n\nAM_CXXFLAGS = -I$(top_srcdir)/ $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\nAM_LDFLAGS  = $(HARDEN_LDFLAGS) $(CODE_COVERAGE_LIBS)\n\ndisplaytests = \\\n\te2e-success.test \\\n\te2e-failure.test \\\n\temulation-ascii-iso-8859.test \\\n\temulation-80th-column.test \\\n\temulation-attributes-vt100.test \\\n\temulation-attributes-16color.test \\\n\temulation-attributes-256color8.test \\\n\temulation-attributes-256color248.test \\\n\temulation-attributes-truecolor.test \\\n\temulation-attributes-bce.test \\\n\temulation-back-tab.test \\\n\temulation-cursor-motion.test \\\n\temulation-multiline-scroll.test \\\n\temulation-scroll.test \\\n\temulation-wrap-across-frames.test \\\n\tnetwork-no-diff.test \\\n\tprediction-unicode.test \\\n\tpty-deadlock.test \\\n\trepeat.test \\\n\trepeat-with-input.test \\\n\tserver-network-timeout.test \\\n\tserver-signal-timeout.test \\\n\twindow-resize.test \\\n\tunicode-combine-fallback-assert.test \\\n\tunicode-later-combining.test \\\n\twindow-resize.test\n\ncheck_PROGRAMS = ocb-aes encrypt-decrypt base64 nonce-incr inpty is-utf8-locale\nTESTS = ocb-aes encrypt-decrypt base64 nonce-incr local.test $(displaytests)\nXFAIL_TESTS = \\\n\te2e-failure.test \\\n\temulation-attributes-256color8.test\n\nbase64_vector.cc: $(srcdir)/genbase64.pl\n\t$(AM_V_GEN)echo '#include \"base64_vector.h\"' > base64_vector.cc || rm base64_vector.cc\n\t$(AM_V_GEN)perl $(srcdir)/genbase64.pl >> base64_vector.cc || rm base64_vector.cc\n\nocb_aes_SOURCES = ocb-aes.cc test_utils.cc test_utils.h\nocb_aes_CPPFLAGS = -I$(srcdir)/../crypto -I$(srcdir)/../util $(CRYPTO_CFLAGS)\nocb_aes_LDADD = ../crypto/libmoshcrypto.a ../util/libmoshutil.a $(CRYPTO_LIBS)\n\nencrypt_decrypt_SOURCES = encrypt-decrypt.cc test_utils.cc test_utils.h\nencrypt_decrypt_CPPFLAGS = -I$(srcdir)/../crypto -I$(srcdir)/../util\nencrypt_decrypt_LDADD = ../crypto/libmoshcrypto.a ../util/libmoshutil.a $(CRYPTO_LIBS)\n\nbase64_SOURCES = base64.cc base64_vector.cc base64_vector.h genbase64.pl\nbase64_CPPFLAGS = $(ocb_aes_CPPFLAGS)\nbase64_LDADD = $(ocb_aes_LDADD)\n\nnonce_incr_SOURCES = nonce-incr.cc\nnonce_incr_CPPFLAGS = -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(srcdir)/../util $(CRYPTO_CFLAGS)\nnonce_incr_LDADD = ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../util/libmoshutil.a $(CRYPTO_LIBS)\n\ninpty_SOURCES = inpty.cc\ninpty_CPPFLAGS = -I$(srcdir)/../util\ninpty_LDADD = ../util/libmoshutil.a\n\nis_utf8_locale_SOURCES = is-utf8-locale.cc\nis_utf8_locale_CPPFLAGS = -I$(srcdir)/../util\nis_utf8_locale_LDADD = ../util/libmoshutil.a $(LIBUTIL)\n\nclean-local: clean-local-check\n.PHONY: clean-local-check\nclean-local-check:\n\t-for i in $(displaytests); do rm -rf $$i.d/; done\n\nCLEANFILES = base64_vector.cc\n"
  },
  {
    "path": "src/tests/README.md",
    "content": "# Mosh Tests\n\n## ocb-aes\n\nThis is a unit test for the OCB-AES encryption used in mosh, including\nRogaway's OCB implementation and some of mosh's surrounding C++\nsupport code.\n\n## encrypt-decrypt\n\nThis is a simple functional test of mosh's implementation of encrypted\nmessages.\n\n## base64\n\nThis tests Mosh's homegrown base64 functionality.  The associated\n`genbase64.pl` script is used to independently generate validated test\nvectors.\n\n## e2e-test\n\nThis is a test framework for end-to-end testing of mosh.  It uses tmux\nto invoke mosh in a nicely stable interactive pty, and also uses\ntmux's `capture-pane` command to get a dump of the terminal screen\nthat mosh-client has drawn, neatly getting around Mosh's somewhat\nnon-deterministic display redraw.\n\nThere are four essential parts to the framework:\n\n* your test script\n* `e2e-test`\n* `e2e-test-server`\n* `e2e-test-subrs`\n\nThe test script has two roles: when invoked without arguments, it is a\nwrapper script for the overall test, and when invoked with an\nargument, it performs a testing-related action.  In wrapper mode, it\ninvokes e2e-test with action arguments, which are used to invoke the\ntest script for actions at appropriate points by e2e-test.  These\nprovide a suite of behaviors that you can use to test various mosh\nbehaviors.\n\n`e2e-test` is the heart of the framework.  It runs actions as\nrequested, logs their output, compares and/or validates their results,\nand generates the final result (exitstatus, mostly) for the Automake\ntesting framework used by the mosh build.  For test execution, it runs\nan action in an interactive session, in a tmux `screen`, to exercise\nsome behavior.  The action can optionally be run in a mosh session, or\ndirectly in tmux (doing both and comparing the result is a useful way\nto test complex terminal emulation behaviors).  The action generally\nwrites some output to the terminal that can later be verified by\nanother action.  Optionally, a client action can generate tty input or\notherwise exercise mosh in some fashion (this capability is untested,\nbut it's a useful place to use `expect` or other interactive\nsimulations).  The action is run by `e2e-test-server`, which is a\nrelatively small wrapper script to capture errors, and capture the\ntmux screen.\n\nThere are several different categories of actions:\n\n### Execution\n\n`baseline` is an action that almost all tests will use.  This invokes\nthe test script inside mosh, where it can generate some output, and\nthen captures the client-side tmux display with `tmux capture-pane`.\n\n`direct` is the same as the above, except that mosh is not used--\n`e2e-wrapper-script` and the test script are invoked directly inside\ntmux.\n\n`variant` can be used to provide a slightly different action from\n`baseline`.\n\n### Verification\n\n`verify` compares captures from the `baseline` and `direct` test\nactions, which are expected to be identical.\n\n`same` compares captures from the `baseline` and `variant` test\nactions, which are expected to be identical.\n\n`different` compares captures from the `baseline` and `variant` test\nactions, which are expected to be different.\n\n`post` is a catchall script hook which allows custom verification\nacions to be coded.\n\n### Client wrappers\n\n`tmux` injects a wrapper command into the test command before tmux.\nIf this is not run, a default command called `hold-stdin` is run\ninstead.  These commands are expected to hold tmux's stdin open,\npossibly injecting tmux commands, while the test runs.  See\n`window-resize.test` for an example of this that manipulates tmux\nstate.  Alternately, this could use expect or something similar.\n\n`client` simply injects a wrapper command into the (long) test command\nbetween tmux and mosh.  It's expected to interact with its wrapped\ncommand line as `expect` might do.  This is not actually tested yet.\n\n### Flags\n\n`mosh-args`, `client-args` and `server-args` inject extra arguments\ninto the invocations of the respective commands.\n\n## Logging and error reporting\n\nEach execution action is run, and recorded in\n`<testname>.test.d/<action>.*`. `<action>.exitstatus` is the\nexitstatus from the server wrapper.  `<action>.tmux.log` is the output\nof tmux for the entire test run for that action; `<action>.capture` is\na capture of the Mosh client screen after the test action is complete,\ngenerated with `tmux capture-pane`.\n\nIn accordance with GNU Automake's test framework, the test should\nreturn these exit status values:\n\n* 0 test success\n* 1 test failure\n* 77 test skipped (tmux or ssh is unavailable if needed)\n* 99 hard error\n\nThese values are also used internally between the various scripts;\nerrors are conveyed out to the build test framework.\n\n\n## Sample tests\n\nA few tests have been implemented so far to test the framework itself,\nand to provide examples for further development.\n\n`e2e-success` is a simple test that executes `baseline` and `direct`\nwith the same stimulus (simply clearing the screen), and expects to\nsee identical results.\n\n`e2e-failure` is similar to `e2e-success`, but expects to see\ndifferent results from `baseline` and `variant`.  Since it uses the\nsame stimulus for the two execution action, it fails.  A more\nrealistic test might be to have `variant` execute some escape sequence\nthat is absent from `baseline`; this would verify that the escape\nsequence actually does something.\n\n`emulation-back-tab` tests an escape sequence that mosh does not\nsupport.  It expects the test to produce the output that would be\ngenerated if the escape sequence were implemented.  If it gets output\nas expected when the escape sequence is *not* implemented, the test\nfails.  But if the output does not match one of these two cases, the\ntest returns an error.  This is an example of error handling within\nthe test framework.\n\n`unicode-later-combining` demonstrates mosh's handling of a Unicode\nedge case, a combining character drawn without a printing character in\nthe same cell.  It verifies the output in the `post` action; since\nthere are a couple of different Unicode renderings that are reasonable\nin this case, a regex that covers both is used.  It also implements an\nunused `variant` action that draws blank-space+combiner in a correct\nfashion.\n\n## Notes\n\nThe shell command `printf` is generally used in place of\n`echo` in this framework, because of its more precisely-specified and\nportable behavior.  But beware, even `printf` varies between systems--\nGNU printf, for example, implements `\\e`, which is a non-POSIX\nextension unavailable in BSD implementations\n\nIt's fairly simple to test each of these scripts independently, but\nthe entire chain is a bit prone to behaving oddly in hard-to-debug\nways.  `set -x` is your friend here.\n\nThe test scripts are a bit fragile about timeouts.  They will\ngenerally run correctly on an unloaded machine without the `make -j`\nflag.  Using `make -j` is obviously very convenient for development,\nand it works fine on faster machines, but I don't recommend it for\nautomated testing.\n"
  },
  {
    "path": "src/tests/base64.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n/* Test suite for the OCB-AES reference implementation included with Mosh.\n\n   This tests cryptographic primitives implemented by others.  It uses the\n   same interfaces and indeed the same compiled object code as the Mosh\n   client and server.  It does not particularly test any code written for\n   the Mosh project. */\n\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n\n#include \"base64_vector.h\"\n#include \"src/crypto/base64.h\"\n#include \"src/crypto/crypto.h\"\n#include \"src/crypto/prng.h\"\n#include \"src/util/fatal_assert.h\"\n// #include \"test_utils.h\"\n\n#define KEY_LEN 16\n#define NONCE_LEN 12\n#define TAG_LEN 16\n\nbool verbose = false;\n\nstatic void test_base64( void )\n{\n  /* run through a test vector */\n  char encoded[25];\n  uint8_t decoded[16];\n  size_t b64_len = 24;\n  size_t raw_len = 16;\n  for ( base64_test_row* row = static_base64_vector; *row->native != '\\0'; row++ ) {\n    memset( encoded, '\\0', sizeof encoded );\n    memset( decoded, '\\0', sizeof decoded );\n\n    base64_encode( static_cast<const uint8_t*>( row->native ), raw_len, encoded, b64_len );\n    fatal_assert( b64_len == 24 );\n    fatal_assert( !memcmp( row->encoded, encoded, sizeof encoded ) );\n\n    fatal_assert( base64_decode( row->encoded, b64_len, decoded, &raw_len ) );\n    fatal_assert( raw_len == 16 );\n    fatal_assert( !memcmp( row->native, decoded, sizeof decoded ) );\n  }\n  if ( verbose ) {\n    printf( \"validation PASSED\\n\" );\n  }\n  /* try 0..255 in the last byte; make sure the final two characters are output properly */\n  uint8_t source[16];\n  memset( source, '\\0', sizeof source );\n  for ( int i = 0; i < 256; i++ ) {\n    source[15] = i;\n    base64_encode( source, raw_len, encoded, b64_len );\n    fatal_assert( b64_len == 24 );\n\n    fatal_assert( base64_decode( encoded, b64_len, decoded, &raw_len ) );\n    fatal_assert( raw_len == 16 );\n    fatal_assert( !memcmp( source, decoded, sizeof decoded ) );\n  }\n  if ( verbose ) {\n    printf( \"last-byte PASSED\\n\" );\n  }\n\n  /* randomly try keys */\n  PRNG prng;\n  for ( int i = 0; i < ( 1 << 17 ); i++ ) {\n    Base64Key key1( prng );\n    Base64Key key2( key1.printable_key() );\n    fatal_assert( key1.printable_key() == key2.printable_key() && !memcmp( key1.data(), key2.data(), 16 ) );\n  }\n  if ( verbose ) {\n    printf( \"random PASSED\\n\" );\n  }\n\n  /* test bad keys */\n  const char* bad_keys[] = {\n    \"\",\n    \"AAAAAAAAAAAAAAAAAAAAAA\",\n    \"AAAAAAAAAAAAAAAAAAAAAA=\",\n    \"AAAAAAAAAAAAAAAAAAAAA==\",\n    \"AAAAAAAAAAAAAAAAAAAAAAA==\",\n    \"AAAAAAAAAAAAAAAAAAAAAAAA==\",\n    \"AAAAAAAAAAAAAAAAAAAAAA~=\",\n    \"AAAAAAAAAAAAAAAAAAAAAA=~\",\n    \"~AAAAAAAAAAAAAAAAAAAAA==\",\n    \"AAAAAAAAAAAAAAAAAAAA~A==\",\n    \"AAAAAAAAAAAAAAAAAAAAA~==\",\n    \"AAAAAAAAAA~AAAAAAAAAAA==\",\n    \"AAAAAAAAAA==\",\n    NULL,\n  };\n  for ( const char** key = bad_keys; *key != NULL; key++ ) {\n    b64_len = 24;\n    raw_len = 16;\n    fatal_assert( !base64_decode( *key, b64_len, decoded, &raw_len ) );\n  }\n  if ( verbose ) {\n    printf( \"bad-keys PASSED\\n\" );\n  }\n}\n\nint main( int argc, char* argv[] )\n{\n  if ( argc >= 2 && strcmp( argv[1], \"-v\" ) == 0 ) {\n    verbose = true;\n  }\n\n  try {\n    test_base64();\n  } catch ( const std::exception& e ) {\n    fprintf( stderr, \"Error: %s\\r\\n\", e.what() );\n    return 1;\n  }\n  return 0;\n}\n"
  },
  {
    "path": "src/tests/base64_vector.h",
    "content": "struct base64_test_row\n{\n  const unsigned char native[17];\n  const char encoded[25];\n};\ntypedef base64_test_row* base64_test_vector;\nextern base64_test_row static_base64_vector[];\n"
  },
  {
    "path": "src/tests/e2e-failure.test",
    "content": "#!/bin/sh\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline variant different\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf \"\\033[H\\033[J\"\n}\n\ncase $1 in\n    baseline|variant)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/e2e-success.test",
    "content": "#!/bin/sh\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline direct variant verify same\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf \"\\033[H\\033[J\"\n}\n\ncase $1 in\n    baseline|direct|variant)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/e2e-test",
    "content": "#!/bin/sh\n\n. \"$(dirname \"$0\")/e2e-test-subrs\"\n\n#\n# Validate that mosh produces expected output, using screen captures\n# in tmux.\n#\n\nlog()\n{\n    printf \"$@\"\n}\n\nerror()\n{\n    printf \"$@\" >&2\n}\n\ndump_logs()\n{\n    dir=$1\n    shift\n    testname=$(basename \"$dir\" .d)\n    for logfile in $dir/*.tmux.log; do\n\tprintf \"travis_fold:start:%s-%s\\n\" \"$testname\" \"$(basename \"$logfile\")\"\n\tcat \"$logfile\"\n\tprintf \"travis_fold:end:%s-%s\\n\" \"$testname\" \"$(basename \"$logfile\")\"\n    done\n}\n\ntest_success()\n{\n    exit 0\n}\ntest_failure()\n{\n    error \"$@\"\n    exit 1\n}\ntest_skipped()\n{\n    error \"$@\"\n    exit 77\n}\ntest_error()\n{\n    error \"$@\"\n    exit 99\n}\ntest_exitstatus()\n{\n    status=$1\n    shift\n    error \"$@\"\n    exit \"$status\"\n}\n\n\nssh_localhost_check()\n{\n    ssh localhost :\n    if [ $? -ne 0 ]; then\n\terror \"ssh to localhost failed\\n\"\n\treturn 1\n    fi\n    return 0\n}\n\n# These two functions are wrappers for mosh-client/mosh-server to turn\n# on verbosity and log stderr.\nmosh_client()\n{\n    if [ -z \"$MOSH_CLIENT\" ] || [ -z \"$MOSH_E2E_TEST\" ]; then\n\ttest_error \"mosh_client: variables missing\\n\"\n    fi\n    exec 2> \"${MOSH_E2E_TEST}.client.stderr\"\n    exec \"$MOSH_CLIENT\" $MOSH_CLIENT_ARGS \"$@\"\n}\n\nmosh_server()\n{\n    if [ -z \"$MOSH_SERVER\" ] || [ -z \"$MOSH_E2E_TEST\" ]; then\n\ttest_error \"mosh_server: variables missing\\n\"\n    fi\n    exec 2> \"${MOSH_E2E_TEST}.server.stderr\"\n    exec \"$MOSH_SERVER\" new -vv $MOSH_SERVER_ARGS -@ \"$@\"\n}\n\n# main\n# Set up environment\nif [ -z \"$srcdir\" ]; then\n    export srcdir=$PWD\nelse\n    srcdir=\"$(cd \"$srcdir\" && pwd)\"\n    if [ $? -ne 0 ]; then\n\terror \"can't cd to srcdir: %s\\n\" \"$srcdir\"\n\texit 99\n    fi\nfi\n\n# Wrappers.\ncase \"$(basename \"$0\")\" in\n    mosh-client)\n\tmosh_client \"$@\"\n\texit\n\t;;\n    mosh-server)\n\tmosh_server \"$@\"\n\texit\n\t;;\n    *)\n\t;;\nesac\n\nif [ $# -lt 2 ]; then\n    test_error \"not enough args\\n\"\nfi\n\n# Get arguments (only one so far)\ntest_name=$1\nshift\ntest_args=$@\n# XXX could use AM testsubdir macro instead\ntest_dir=$(basename \"${test_name}\").d\ntest_script=\"${test_name}\"\n\ntests_dir=$(dirname \"${test_name}\")\nif ! set_locale \"${tests_dir}\"; then\n    test_error \"e2e-test: no usable locale\\n\"\nfi\n\n# XXX this ignores $TMPDIR, because it results in an overlong pathname on OS X\ntmux_socket=\"/tmp/.tmux-mosh-test-$$\"\n\n# need version 1.8 for capture-pane\nif ! tmux_check 1 8; then\n    test_skipped \"tmux unavailable\\n\"\nfi\n\nrm -rf \"${test_dir}\"\nmkdir \"${test_dir}\"\n\n\non_exit() {\n    rv=$?\n    if test $rv -ne 0; then\n\tdump_logs \"$test_dir\" $test_args\n    fi\n    exit $rv\n}\ntrap on_exit EXIT\n\n# Set up tests to run.\nserver_tests=\ncompare_tests=\nfor i in $test_args; do\n    case $i in\n\tbaseline|direct|variant)\n\t    server_tests=\"$server_tests $i\";;\n\tverify|same|different)\n\t    compare_tests=\"$compare_tests $i\";;\n\ttmux)\n\t    tmux=1;;\n\tclient)\n\t    client=1;;\n\tserver)\n\t    server=1;;\n\tpost)\n\t    post=1;;\n\tmosh-args)\n\t    mosh_args=$(\"${test_script}\" mosh-args);;\n\tclient-args)\n\t    MOSH_CLIENT_ARGS=$(\"${test_script}\" client-args)\n\t    export MOSH_CLIENT_ARGS;;\n\tserver-args)\n\t    MOSH_SERVER_ARGS=$(\"${test_script}\" server-args)\n\t    export MOSH_SERVER_ARGS;;\n\t*)\n\t    error 'unknown test type argument %s\\n' \"$i\"\n\t    exit 99\n\t    ;;\n    esac\ndone\n\n# Run test(s).\nclient_wrapper=\nif [ -n \"$client\" ]; then\n    client_wrapper=\"${test_script} client\"\nfi\n\nserver_wrapper=\"\\\"${srcdir}/e2e-test-server\\\"\"\nif [ -n \"$server\" ]; then\n    server_wrapper=\"\\\"${srcdir}/${test_script}\\\" server\"\nfi\ntmux_stdin=\"${srcdir}/hold-stdin\"\nif [ -n \"$tmux\" ];  then\n    tmux_stdin=\"${test_script} tmux\"\nfi\n\nln -fs \"${tmux_socket}\" \"${test_dir}/tmux-socket\"\n\nfor run in $server_tests; do\n    log \"Running server test %s.\\n\" \"$run\"\n    # These three variables are for the benefit of the mosh-client and mosh-server wrappers.\n    export MOSH_CLIENT=\"$PWD/../frontend/mosh-client\"\n    export MOSH_SERVER=\"$PWD/../frontend/mosh-server\"\n    export MOSH_E2E_TEST=\"$PWD/${test_dir}/${run}\"\n    # XXX need to quote special chars in server pathname here somehow\n    sut=\"../../scripts/mosh --client=${srcdir}/mosh-client --server=${srcdir}/mosh-server --local --bind-server=127.0.0.1 ${mosh_args} 127.0.0.1\"\n    if [ \"$run\" = \"direct\" ]; then\n\tsut=\"\"\n    fi\n    # Actually execute code under test\n    # XXX tmux 1.8 requires shell command as a single arg; once we move to 2.0, undo these quotes\n    # tmux <= 2.5 ignore -x/-y, but the client sets the session to 80x24.\n    # tmux from 2017-05-27 and later should default to an 80x24 session,\n    # but do use -x/-y on control-master clients.\n    ${tmux_stdin} tmux -f /dev/null -S \"${tmux_socket}\" -C new-session -x 80 -y 24 \"${srcdir}/print-exitstatus ${client_wrapper} ${sut} ${server_wrapper} \\\"${PWD}/${test_dir}/${run}\\\" \\\"${PWD}/${test_script} ${run}\\\"\" > \"${test_dir}/${run}.tmux.log\"\n    rv=$?\n    rm -f \"${tmux_socket}\" \"${test_dir}/tmux-socket\"\n    if [ $rv -ne 0 ]; then\n\ttest_error \"tmux failure on test %s\\n\" \"$run\"\n    fi\n    # Check for mosh failures\n    if ! grep -q \"@@@ exitstatus: 0 @@@\" \"${test_dir}/${run}.tmux.log\"; then\n\ttest_error \"mosh-client had non-zero exitstatus\\n\"\n    fi\n\n    # Check for server harness failures\n    if [ -z \"$server\" ]; then\n\tif [ ! -s \"${test_dir}/${run}.capture\" ] \\\n\t       || [ ! -s \"${test_dir}/${run}.exitstatus\" ]; then\n\t    test_error \"server harness failure on test %s\\n\" \"$run\"\n\tfi\n\tread -r server_rv < \"${test_dir}/${run}.exitstatus\"\n\tif [ \"$server_rv\" -ne 0 ]; then\n\t    test_error \"server harness exited with status %s\\n\" \"$server_rv\"\n\tfi\n    fi\n    if [ \"${run}\" != \"direct\" ]; then\n\t# Check for \"round-trip\" failures\n\tif grep -q \"round-trip Instruction verification failed\" \"${test_dir}/${run}.server.stderr\"; then\n\t    test_error \"Round-trip Instruction verification failed on server during %s\\n\" \"$run\"\n\tfi\n\t# Check for 0-timeout select() issue\n\tif egrep -q \"(polls, rate limiting|consecutive polls)\" \"${test_dir}/${run}.server.stderr\"; then\n\t    if [ \"osx\" != \"${TRAVIS_OS_NAME}\" ]; then\n\t\ttest_error \"select() with zero timeout called too often on server during %s\\n\" \"$run\"\n\t    fi\n\tfi\n\t# Check for assert()\n\tif egrep -q \"assertion.*failed\" \"${test_dir}/${run}.server.stderr\"; then\n\t    test_error \"assertion during %s\\n\" \"$run\"\n\tfi\n    fi\n    # XXX We'd also like to check for \"target state Instruction\n    # verification failed\", a new check, but tmux's lack of BCE\n    # support forces mosh to clear lines with spaces and change a\n    # framebuffer in a way that causes this to fire spuriously.\ndone\n\nfor compare in $compare_tests; do\n    log \"Running server comparison %s.\\n\" \"$compare\"\n    # Compare captures\n    if [ \"$compare\" = verify ]; then\n\ttest1=\"direct\"\n\ttest2=\"baseline\"\n    else\n\ttest1=\"baseline\"\n\ttest2=\"variant\"\n    fi\n    if diff -q \"${test_dir}/${test1}.capture\" \"${test_dir}/${test2}.capture\"; then\n\tdiffer=n\n    else\n\tdiffer=y\n    fi\n    if [ \"$compare\" = different ]; then\n\tdesired=y\n\tbadresult=same\n    else\n\tdesired=n\n\tbadresult=different\n    fi\n    if [ $differ != $desired ]; then\n\ttest_failure \"Output is %s between tests %s and %s\\n\" \"$badresult\" \"$test1\" \"$test2\"\n    fi\ndone\n\n# Run a post script (usually a custom validation of results)\nif [ -n \"$post\" ]; then\n    \"${test_script}\" post\n    status=$?\n    if [ $status -ne 0 ]; then\n\ttest_exitstatus $status \"Post test failed with exitstatus %d\\n\" $status\n    fi\nfi\n"
  },
  {
    "path": "src/tests/e2e-test-server",
    "content": "#!/bin/sh\n\n#\n# Harness script for Mosh tests, server side.  Runs test script and\n# then captures screen with `tmux capture-pane`.  Captures exitstatus\n# of both and returns appropriate errors.\n#\n\n# If MOSH_E2E_WAIT is set, then the test will wait for another tmux\n# client to attach to the test session before starting, and will wait\n# for other tmux clients to detach before exiting.\nwait_for_clients()\n{\n    if [ -z \"$MOSH_E2E_WAIT\" ]; then\n\treturn\n    fi\n    expected=$1\n    while true; do \n\tn=$(tmux list-clients -F . | wc -l)\n\tif [ $expected -eq 1 ]; then\n\t    if [ $n -eq 1 ]; then\n\t\treturn\n\t    fi\n\telif [ $n -ne 1 ]; then\n\t    return\n\tfi\n\tsleep 1\n    done\n}\n\nexport MOSH_SERVER_PID=$PPID\n\nif [ $# -lt 2 ]; then\n    printf \"not enough args\\n\" >&2\n    exit 99\nfi\ntestname=$1\nshift\nrm -f \"$testname.capture\" \"$testname.exitstatus\"\ntrap \":\" TERM HUP QUIT # If the session closes on us, let the test we're running drive.\non_exit() {\n    rv=$?\n    echo $rv > \"$testname.exitstatus\"\n    exit $rv\n}\ntrap on_exit EXIT\n# check for tmux\nif [ -z \"$TMUX_PANE\" ]; then\n    printf \"not running under tmux\\n\" >&2\n    exit 99\nfi\nwait_for_clients 2\n# run harnessed command\neval \"$@\"\ntestret=$?\n# Capture mosh-server runtime if possible.\nruntime=$(ps -o time= $PPID 2>/dev/null)\nif [ $? -ne 0 ]; then # Cygwin...\n    runtime=-\nfi\n# Wait for tmux client screen to become up to date.\nsleep 1\nprintf \"@@@ server complete @@@\" >&2\nwait_for_clients 1\ni=0\nwhile [ $i -lt 60 ]; do\n    if grep -q \"@@@ server complete @@@\" \"$testname.tmux.log\"; then\n\tbreak\n    fi\n    i=$((i+1))\n    sleep 1\ndone\nif [ $i -ge 60 ]; then\n    printf \"wait for tmux client update failed, erroring test\\n\" >&2\n    exit 99\nfi\n# capture screen\nif ! tmux capture-pane -et \"$TMUX_PANE\"; then\n    printf \"tmux capture-pane failed, erroring test\\n\" >&2\n    exit 99\nfi\nif ! tmux save-buffer \"$testname.capture\"; then\n    printf \"tmux save-buffer failed, erroring test\\n\" >&2\n    exit 99\nfi\n# Dump runtime into tmux log.\nprintf \"@@@ runtime %s @@@\\n\" \"$runtime\"\n# return useful exitstatus from harnessed command\nif [ $testret -ne 0 ]; then\n    exit 1\nfi\nexit 0\n"
  },
  {
    "path": "src/tests/e2e-test-subrs",
    "content": "#!/bin/sh\n\n#\n# This is a library of subroutines mostly intended for test scripts.\n#\n\nfail()\n{\n    printf \"$@\" 2>&1\n    exit 99\n}\n\nskip()\n{\n    printf \"$@\" 2>&1\n    exit 77\n}\n\nsleepf()\n{\n    (sleep .1 || sleep 1) > /dev/null 2>&1\n}\n\nseq_function()\n{\n    if [ $# -lt 1 ] || [ $# -gt 3 ]; then\n\techo \"bad args\" >&2\n    fi\n    first=$1\n    incr=1\n    last=0\n    case $# in\n\t3)\n\t    incr=$2\n\t    last=$3\n\t    ;;\n\t2)\n\t    last=$2\n\t    ;;\n\t1)\n\t    ;;\n    esac\n    while :; do\n\tprintf '%d\\n' \"$first\"\n\tfirst=$(( first + incr ))\n\tif [ \"$first\" -gt \"$last\" ]; then\n\t    break\n\tfi\n    done\n}\n\nif ! seq 1 > /dev/null 2>&1; then\n    seq()\n    {\n\tseq_function \"$@\"\n    }\nfi\n\nchr()\n{\n    printf '%b' \"\\\\0$(printf %03o \"$1\")\"\n}\n\n# If the locale is not set to a UTF-8 locale, set it to en_US.UTF-8\n# or C.UTF-8.\nset_locale()\n{\n    # Test for a usable locale.\n    if ./is-utf8-locale 2> /dev/null; then\n\treturn 0\n    fi\n    # Attempt to find/set a usable locale.\n    unset LANG LC_CTYPE LC_COLLATE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME LC_ALL\n    for i in en_US.UTF-8 en_US.utf8 C.UTF-8; do\n\tif env LC_ALL=$i ./is-utf8-locale 2> /dev/null; then\n\t    export LC_ALL=$i\n\t    return 0\n\tfi\n    done\n    # Fail.\n    return 1\n}\n\n# Given two versions, return success if the first version is less than\n# or equal to the second version.\nversionlte()\n{\n    [  \"$1\" = \"$(printf \"$1\\n$2\\n\" | sort -V | head -n1)\" ]\n}\nversionlt() {\n    if [ \"$1\" = \"$2\" ]; then\n\treturn 1\n    fi\n    versionlte \"$1\" \"$2\"\n}\n\n# Tmux check.\ntmux_check()\n{\n    need_major=\"$1\"; shift\n    need_minor=\"$1\"; shift\n    # OpenBSD tmux does not have '-V'.\n    if [ \"$(uname -s)\" = \"OpenBSD\" ]; then\n\topenbsd_major=\"$(uname -r)\"\n\topenbsd_major=\"${openbsd_major%%.*}\"\n\tif [ \"${openbsd_major}\" -ge 6 ]; then\n\t    return 0\n\tfi\n    fi\n    version=$(tmux -V)\n    if [ $? != 0 ]; then\n\terror \"tmux unavailable\\n\"\n\treturn 1\n    fi\n    if [ \"$version\" = \"tmux master\" ]; then\n\treturn 0\n    fi\n    version=${version##tmux }\n\n    if versionlt \"$version\" \"$need_major.$need_minor\"; then\n\tprintf \"tmux version %s too old\\n\" \"$version\" >&2\n\treturn 1\n    fi\n    # Finally, check that tmux actually works to some degree.\n    #\n    # Use a different socket name.  On Cygwin, this tmux server is\n    # slow to exit, and the actual test tmux can attach to it, causing\n    # problems with missing environment variables.\n    tmux_check_socket=$(mktemp -d /tmp/mosh-tmux-check.XXXXXXXX)\n    tmux -f /dev/null -S \"${tmux_check_socket}/s\" -C new-session true\n    rv=$?\n    rm ${tmux_check_socket}/s\n    rmdir ${tmux_check_socket}\n    return $rv\n}\n"
  },
  {
    "path": "src/tests/emulation-80th-column.test",
    "content": "#!/bin/sh\n\n#\n# This test validates the ancient VT100 behavior of positioning the\n# cursor at column 80 (and not wrapping) after 80 characters are\n# output, and behaving accordingly with subsequent cursor motion\n# commands (CR+LF in this state should not result in an extra blank\n# line).\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline post\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    # We need to control CR and LF individually for this test.\n    TERMIO=$(stty -g)\n    trap 'stty \"$TERMIO\"' EXIT\n    stty raw\n    printf '\\033[H\\033[J'\n    for lines in $(seq 1 25); do\n\tfor tencols in $(seq 1 8); do\n\t    printf \"EEEEEEEEEE\"\n\tdone\n\tprintf \"\\r\\n\"\n    done\n}\n\npost()\n{\n    # If hidden 80th column is working properly, then the lines\n    # will have no blank lines in between and we should see 23\n    # of them.\n    if [ \"$(grep -c \"EEEEEEEEEE\" \"$(basename \"$0\").d/baseline.capture\")\" -ne 23 ]; then\n\texit 1\n    fi\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-ascii-iso-8859.test",
    "content": "#!/bin/sh\n\n#\n# This validates display of ASCII and ISO-8859-1 characters.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline direct verify\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf '\\033[H\\033[J'\n\n    cat <<'EOF'\n20   21 ! 22 \" 23 # 24 $ 25 % 26 & 27 ' 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f / \n30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ? \n40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O \n50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W 58 X 59 Y 5a Z 5b [ 5c \\ 5d ] 5e ^ 5f _ \n60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o \n70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ \na0   a1 ¡ a2 ¢ a3 £ a4 ¤ a5 ¥ a6 ¦ a7 § a8 ¨ a9 © aa ª ab « ac ¬ ad ­ ae ® af ¯ \nb0 ° b1 ± b2 ² b3 ³ b4 ´ b5 µ b6 ¶ b7 · b8 ¸ b9 ¹ ba º bb » bc ¼ bd ½ be ¾ bf ¿ \nc0 À c1 Á c2 Â c3 Ã c4 Ä c5 Å c6 Æ c7 Ç c8 È c9 É ca Ê cb Ë cc Ì cd Í ce Î cf Ï \nd0 Ð d1 Ñ d2 Ò d3 Ó d4 Ô d5 Õ d6 Ö d7 × d8 Ø d9 Ù da Ú db Û dc Ü dd Ý de Þ df ß \ne0 à e1 á e2 â e3 ã e4 ä e5 å e6 æ e7 ç e8 è e9 é ea ê eb ë ec ì ed í ee î ef ï \nf0 ð f1 ñ f2 ò f3 ó f4 ô f5 õ f6 ö f7 ÷ f8 ø f9 ù fa ú fb û fc ü fd ý fe þ ff ÿ \nEOF\n} \n\ncase $1 in\n    baseline|direct)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-attributes.test",
    "content": "#!/bin/sh\n\n#\n# This validates VT100, 16-color, 256-color and true color attributes\n# against tmux.  It is not run directly, but as subtests based on the\n# executable's name for vt100, 16color, 256color8, 256color248 and\n# truecolor.\n# This is because Mosh internally represents the first 8 values of the\n# 256color space as though they were the 16-color values they are\n# equivalent to.  tmux does not filter this out on its redisplay, so\n# compares on these values fail though they are visually identical.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Need 2.3 for true-color support (2.2 may work also)\nif [ \"$(basename \"$0\")\" = emulation-attributes-truecolor.test ] &&\n   ! tmux_check 2 3; then\n    printf \"tmux does not support true color\\n\" >&2\n    exit 77\nfi\n# Need 2.4 for BCE support\nif [ \"$(basename \"$0\")\" = emulation-attributes-bce.test ] &&\n   ! tmux_check 2 4; then\n    printf \"tmux does not support BCE\\n\" >&2\n    exit 77\nfi\n\n# tmux 3.3a has a behavior change in how BCE is handled, which has\n# been reverted: https://github.com/tmux/tmux/issues/3339\nif [ \"$(basename \"$0\")\" = emulation-attributes-bce.test ] &&\n   tmux_check 3 3a && ! tmux_check 3 3b; then\n    printf \"tmux 3.3a has incompatible BCE behavior\\n\" >&2\n    exit 77\nfi\n\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline direct verify\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\ntest_true_color()\n{\n    s=$(printf \"\\033[0\")\n    for i in $@; do\n        s=\"$s;$i\"\n    done\n    s=\"${s}m\"\n\n    for attr in $(seq 0 76); do\n        r=$((255-(attr*255/76)))\n        g=$((attr*510/76))\n        b=$((attr*255/76))\n        if [ $g -gt 255 ]; then\n            g=$((510-g))\n        fi\n        invr=$((255-r))\n        invg=$((255-g))\n        invb=$((255-b))\n        c=\"E\"\n\n        printf \"%s\" \"$s\"\n        printf \"\\033[48;2;%d;%d;%dm\" $r $g $b;\n        printf \"\\033[38;2;%d;%d;%dm\" $invr $invg $invb\n        printf \"%s\\033[m\" \"$c\"\n    done\n    printf \"\\n\"\n}\n\nbaseline()\n{\n    # Strip our name to the last dash-separated word before the .test suffix.\n    testname=$(basename \"$1\")\n    testname=${testname%%.test}\n    testname=${testname##*-}\n\n    printf '\\033[H\\033[J'\n\n\n    case $testname in\n\t# Traditional ancient VT100 attributes.\n\tvt100)\n\t    for attr in 0 1 4 5 7; do\n\t\tprintf '\\033[%dmE\\033[m ' $attr\n\t    done\n\t    ;;\n\t# 16-color attributes.\n\t16color)\n\t    for attr in $(seq 30 37) $(seq 39 47) 49; do\n\t\tprintf '\\033[%dmE\\033[m ' \"$attr\"\n\t    done\n\t    ;;\n\t# First 8 256-color attributes.  Comparing mosh and tmux fails.\n\t256color8)\n\t    for attr in $(seq 0 7); do\n\t\tprintf '\\033[38;5;%dmE\\033[m ' \"$attr\"\n\t\tprintf '\\033[48;5;%dmM\\033[m ' \"$attr\"\n\t    done\n\t    ;;\n\t# Last 248 256-color attributes.\n\t256color248)\n\t    for attr in $(seq 8 255); do\n\t\tprintf '\\033[38;5;%dmE\\033[m ' \"$attr\"\n\t\tprintf '\\033[48;5;%dmM\\033[m ' \"$attr\"\n\t    done\n\t    ;;\n        # True color.\n        # See https://gist.github.com/XVilka/8346728 for the test case\n        truecolor)\n            echo \"Normal:\"\n            test_true_color\n            echo \"Bold:\"\n            test_true_color 1\n            echo \"Italic:\"\n            test_true_color 3\n            echo \"Underline:\"\n            test_true_color 4\n            echo \"Blink:\"\n            test_true_color 5\n            echo \"Inverse:\"\n            test_true_color 7\n            echo \"Invisible:\"\n            test_true_color 8\n            echo \"Bold, italic and underline:\"\n            test_true_color 1 3 4\n            ;;\n\t# BCE in combination with various color modes.\n\tbce)\n\t    # True color.\n\t    printf '\\033[48;2;255;0;255m\\033[H\\033[JTrue color\\n'\n\t    printf '\\033[48;5;32m\\033[J256 color\\n'\n\t    printf '\\033[42m\\033[J16 color\\n'\n\t    printf '\\033[0mdone\\n'\n\t    ;;\n\t*)\n\t    fail \"unknown test name %s\\n\" \"$1\"\n\t    ;;\n    esac\n\n    printf '\\033[mend\\n'\n} \n\ncase $1 in\n    baseline|direct)\n\tbaseline \"$0\";;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-back-tab.test",
    "content": "#!/bin/sh\n\n#\n# This test is for issue 539 on github.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline post\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf 'hello, wurld\\033[Zo\\n'\n    printf 'hello, wurld\\033[2Zo\\n'\n    printf 'hello, wurld\\033[99Z9\\n'\n    printf 'hello, wurld\\033[It\\n'\n    printf '\\033[99I#\\n'\n}\n\npost()\n{\n    # Basic previously-failing case.\n    if grep -q 'hello, wurldo' \"$(basename \"$0\").d/baseline.capture\"; then\n\texit 1\n    fi\n    if ! grep -q 'hello, world' \"$(basename \"$0\").d/baseline.capture\"; then\n\texit 99\n    fi\n    # New test cases for new code.\n    if ! grep -q 'oello, wurld' \"$(basename \"$0\").d/baseline.capture\" ||\n\t    ! grep -q '9ello, wurld' \"$(basename \"$0\").d/baseline.capture\" ||\n\t    ! grep -q 'hello, wurld    t' \"$(basename \"$0\").d/baseline.capture\" ||\n\t    ! grep -E -q '^ {79}#$' \"$(basename \"$0\").d/baseline.capture\"; then\n\texit 1\n    fi\n    exit 0\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-cursor-motion.test",
    "content": "#!/bin/sh\n\n#\n# This test exercises a particular optimization involving small cursor\n# motions in Mosh.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline direct verify\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf '\\033[H\\033[J'\n\n    while read -r x y text; do\n\tprintf '\\033[%d;%dH%s' \"$y\" \"$x\" \"$text\"\n\tsleepf\n    done <<EOF\n1 1 A\n10 1 B\n1 2 C\n1 4 D\n10 4 E\n1 7 F\n1 11 G\n10 11 H\n1 16 I\n2 16 J\n1 22 K\n60 23 L\n59 23 M\n57 23 N\n54 23 O\n50 23 P\n45 23 Q\n39 23 R\n32 23 S\n1 24 done\nEOF\n} \n\ncase $1 in\n    baseline|direct)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-multiline-scroll.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test for a crash seen in my development\n# of performance code for Mosh, involving insert/delete line.\n# It does insert/delete line from 0 to 2 more than the window height\n# 24 in this test environment).\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf '\\033[H\\033[J'\n    for dir in L M; do\n\tfor i in $(seq 0 2) $(seq 22 26); do\n\t    printf '%d\\r' \"$i\"\n\t    printf '\\033[%d%s' \"$i\" \"$dir\"\n\t    # On the one hand, we'd like to test that this works\n\t    # properly on both client and server, which requires\n\t    # delays so that each iteration percolates to the client\n\t    # by itself.  On the other hand, that makes the test take\n\t    # a long time.  Compromise on .1 second.\n\t    sleepf\n\tdone\n    done\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-scroll.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test for a bug in Mosh: it would move the\n# cursor for the SCROLL UP and SCROLL DOWN commands, though it should\n# not.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline post\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    # Clear\n    printf '\\033[H\\033[J'\n    # Fill with sample text\n    for i in $(seq 1 24); do\n\tprintf '\\ntext %s' \"$i\"\n    done\n    # Scroll up 4 lines\n    printf '\\033[4S'\n    # Then down 2\n    printf '\\033[2T'\n\n    # The cursor should not have moved and this should print on the\n    # last line, and not overprint 'line 24'\n    printf '\\rBad line'\n    # Overprint on line 24\n    printf '\\033[24;1HLast line'\n    # and line 1\n    printf '\\033[HFirst line\\n'\n\n}\n\npost()\n{\n    local capture\n    capture=\"$(basename \"$0\").d/baseline.capture\"\n    # 'Bad line' should have been overwritten\n    if grep -q '^Bad line$' \"$capture\"; then\n\texit 1\n    fi\n    # The first four lines should have scrolled off\n    if grep -q '^text [1-4]$' \"$capture\"; then\n\texit 1\n    fi\n    # The last line should not have scrolled off or been overwritten\n    if ! grep -q '^text 24$' \"$capture\"; then\n\texit 1\n    fi\n    # 20 lines of the original text should remain\n    if [ \"$(grep -c '^text' \"$capture\")\" -ne 20 ]; then\n\texit 1\n    fi\n    exit 0\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/emulation-wrap-across-frames.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test for a bug seen where mosh-server's\n# round-trip verification failed if text was filled to column 80 on\n# frame N and then wrapped to the next line on frame N+1, because the\n# wrap flag used to be in the Cell class and caused miscompares\n# between cells.  It got moved to the Rows class where it makes more\n# sense.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline direct verify\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf '\\033[H\\033[J'\n\n    for x in $(seq 1 10); do\n\tprintf \"abcdXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1234\"\n\tsleepf\n\tprintf \"ABCDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5678\"\n\tsleepf\n    done\n    printf '\\n'\n} \n\ncase $1 in\n    baseline|direct)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/encrypt-decrypt.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n/* Tests the Mosh crypto layer by encrypting and decrypting a bunch of random\n   messages, interspersed with some random bad ciphertexts which we need to\n   reject. */\n\n#include <cstdio>\n\n#define __STDC_FORMAT_MACROS\n#include <cinttypes>\n\n#include \"src/crypto/crypto.h\"\n#include \"src/crypto/prng.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"test_utils.h\"\n\nusing namespace Crypto;\n\nPRNG prng;\n\nconst size_t MESSAGE_SIZE_MAX = ( 2048 - 16 );\nconst size_t MESSAGES_PER_SESSION = 256;\nconst size_t NUM_SESSIONS = 64;\n\nbool verbose = false;\n\n#define NONCE_FMT \"%016\" PRIx64\n\nstatic std::string random_payload( void )\n{\n  const size_t len = prng.uint32() % MESSAGE_SIZE_MAX;\n  char buf[MESSAGE_SIZE_MAX];\n  prng.fill( buf, len );\n\n  std::string payload( buf, len );\n  return payload;\n}\n\nstatic void test_bad_decrypt( Session& decryption_session )\n{\n  std::string bad_ct = random_payload();\n\n  bool got_exn = false;\n  try {\n    decryption_session.decrypt( bad_ct );\n  } catch ( const CryptoException& e ) {\n    got_exn = true;\n\n    /* The \"bad decrypt\" exception needs to be non-fatal, otherwise we are\n       vulnerable to an easy DoS. */\n    fatal_assert( !e.fatal );\n  }\n\n  if ( verbose ) {\n    hexdump( bad_ct, \"bad ct\" );\n  }\n  fatal_assert( got_exn );\n}\n\n/* Generate a single key and initial nonce, then perform some encryptions. */\nstatic void test_one_session( void )\n{\n  Base64Key key;\n  Session encryption_session( key );\n  Session decryption_session( key );\n\n  uint64_t nonce_int = prng.uint64();\n\n  if ( verbose ) {\n    hexdump( key.data(), 16, \"key\" );\n  }\n\n  for ( size_t i = 0; i < MESSAGES_PER_SESSION; i++ ) {\n    Nonce nonce( nonce_int );\n    fatal_assert( nonce.val() == nonce_int );\n\n    std::string plaintext = random_payload();\n    if ( verbose ) {\n      printf( DUMP_NAME_FMT NONCE_FMT \"\\n\", \"nonce\", nonce_int );\n      hexdump( plaintext, \"pt\" );\n    }\n\n    std::string ciphertext = encryption_session.encrypt( Message( nonce, plaintext ) );\n    if ( verbose ) {\n      hexdump( ciphertext, \"ct\" );\n    }\n\n    Message decrypted = decryption_session.decrypt( ciphertext );\n    if ( verbose ) {\n      printf( DUMP_NAME_FMT NONCE_FMT \"\\n\", \"dec nonce\", decrypted.nonce.val() );\n      hexdump( decrypted.text, \"dec pt\" );\n    }\n\n    fatal_assert( decrypted.nonce.val() == nonce_int );\n    fatal_assert( decrypted.text == plaintext );\n\n    nonce_int++;\n\n    if ( !( prng.uint8() % 16 ) ) {\n      test_bad_decrypt( decryption_session );\n    }\n\n    if ( verbose ) {\n      printf( \"\\n\" );\n    }\n  }\n}\n\nint main( int argc, char* argv[] )\n{\n  if ( argc >= 2 && strcmp( argv[1], \"-v\" ) == 0 ) {\n    verbose = true;\n  }\n\n  for ( size_t i = 0; i < NUM_SESSIONS; i++ ) {\n    try {\n      test_one_session();\n    } catch ( const CryptoException& e ) {\n      fprintf( stderr, \"Crypto exception: %s\\r\\n\", e.what() );\n      return 1;\n    }\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "src/tests/genbase64.pl",
    "content": "#!/usr/bin/perl\n\nuse strict;\nuse warnings;\nuse MIME::Base64;\n\nmy @vectors = (\n    \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n    \"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\",\n    \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xff\",\n    \"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\",\n    \"\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\",\n    \"\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\",\n    \"\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\\xaa\",\n    \"\\x01\\x02\\x04\\x08\\x10\\x20\\x40\\x80\\xfe\\xfd\\xfb\\xf7\\xef\\xdf\\xbf\\x7f\",\n    );\n\nprint 'base64_test_row static_base64_vector[] = {' . \"\\n\";\nfor my $v (@vectors) {\n    print '    { \"';\n    my @chars = split '', $v;\n    for my $c (@chars) {\n\tprintf \"\\\\x%02x\", ord($c);\n    }\n    print '\", \"' . encode_base64($v, \"\") . '\" },' . \"\\n\";\n}\nprint '    { \"\", \"\" }' . \"\\n\";\nprint \"};\\n\";\n"
  },
  {
    "path": "src/tests/hold-stdin",
    "content": "#!/usr/bin/env perl\n\n#\n# The sole function of this script is to provide a stdin that doesn't\n# read data or return EOF to its children.  If there's a clean,\n# portable, not-Perl way to do this, then that should replace this.\n#\nuse warnings;\nuse strict;\n\nmy $pid = open(my $fh, \"|-\", @ARGV) or die;\nwaitpid($pid, 0) == $pid or die;\nmy $rc;\nif ($? == 0) {\n    $rc = 0;\n} elsif ($? >= 256) {\n    $rc = $? >> 8;\n} else {\n    $rc = ($? & 127) | 128;\n}\nexit $rc;\n"
  },
  {
    "path": "src/tests/inpty.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cerrno>\n#include <csignal>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n\n#include <sys/types.h>\n#include <sys/wait.h>\n#include <termios.h>\n#include <unistd.h>\n\n#if HAVE_PTY_H\n#include <pty.h>\n#elif HAVE_UTIL_H\n#include <util.h>\n#endif\n\n#if FORKPTY_IN_LIBUTIL\n#include <libutil.h>\n#endif\n\n#include \"src/util/pty_compat.h\"\n#include \"src/util/swrite.h\"\n\nint main( int argc, char* argv[] )\n{\n  if ( argc < 2 ) {\n    fprintf( stderr, \"usage: inpty COMMAND [ARGS...]\\n\" );\n    return 1;\n  }\n\n  struct winsize winsize;\n  memset( &winsize, 0, sizeof( winsize ) );\n  winsize.ws_col = 80;\n  winsize.ws_row = 24;\n\n  int saved_stderr = dup( STDERR_FILENO );\n  if ( saved_stderr < 0 ) {\n    perror( \"dup\" );\n    return 1;\n  }\n\n  int master;\n  pid_t child = forkpty( &master, NULL, NULL, &winsize );\n  if ( child == -1 ) {\n    perror( \"forkpty\" );\n    /* The Debian and Ubuntu build systems fail to set up a working\n     * /dev/ptmx (https://bugs.debian.org/817236).  There is not much\n     * we can do about that except skip the test.  In the future when\n     * this is fixed, we should turn this into an failure.\n     */\n    return 77;\n  } else if ( child == 0 ) {\n    if ( dup2( saved_stderr, STDERR_FILENO ) < 0 ) {\n      perror( \"dup2\" );\n      exit( 1 );\n    }\n    if ( close( saved_stderr ) < 0 ) {\n      perror( \"close\" );\n      exit( 1 );\n    }\n    if ( execvp( argv[1], argv + 1 ) < 0 ) {\n      perror( \"execve\" );\n      exit( 1 );\n    }\n    exit( 0 );\n  }\n\n  while ( 1 ) {\n    char buf[1024];\n    ssize_t bytes_read = read( master, buf, sizeof( buf ) );\n    if ( bytes_read == 0 || ( bytes_read < 0 && errno == EIO ) ) { /* EOF */\n      break;\n    } else if ( bytes_read < 0 ) {\n      perror( \"read\" );\n      return 1;\n    }\n    swrite( STDOUT_FILENO, buf, bytes_read );\n  }\n\n  int wstatus;\n  if ( waitpid( child, &wstatus, 0 ) < 0 ) {\n    perror( \"waitpid\" );\n    return 1;\n  }\n\n  if ( WIFSIGNALED( wstatus ) ) {\n    fprintf( stderr, \"inpty: child exited with signal %d\\n\", WTERMSIG( wstatus ) );\n    raise( WTERMSIG( wstatus ) );\n    return -1;\n  } else {\n    return WIFEXITED( wstatus ) ? WEXITSTATUS( wstatus ) : -1;\n  }\n}\n"
  },
  {
    "path": "src/tests/is-utf8-locale.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n\n#include \"src/util/locale_utils.h\"\n\nint main( int argc __attribute__( ( unused ) ), char** argv __attribute__( ( unused ) ) )\n{\n  set_native_locale();\n  if ( !is_utf8_locale() ) {\n    fprintf( stderr, \"not a UTF-8 locale\\n\" );\n    return 1;\n  }\n  return 0;\n}\n"
  },
  {
    "path": "src/tests/local.test",
    "content": "#!/bin/sh\n\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nif ! set_locale; then\n    echo \"$0: no usable locale\" >&2\n    exit 99\nfi\n\nset -eu\nout=$(\n    TERM=xterm \\\n        ./inpty \\\n        ../../scripts/mosh \\\n        --client=\"../frontend/mosh-client\" \\\n        --server=\"$PWD/../frontend/mosh-server\" \\\n        --local --bind-server=127.0.0.1 127.0.0.1 \\\n        -- printf 'he%s\\n' llo)\ncase \"$out\" in\n    *hello*) exit 0;;\n    *) exit 1;;\nesac\n"
  },
  {
    "path": "src/tests/network-no-diff.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test to ensure that Mosh does not spin\n# on updates that do not actually change the framebuffer.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline post\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    # Generate updates that don't change the screen\n    i=0\n    while [ $i -lt 10 ] && printf 'x\\b' && sleep 1; do\n\ti=$((i + 1))\n    done\n}\n\npost()\n{\n    # Extract server run time.\n    runtime=$(sed -E -n 's/.*@@@ runtime: (.*) @@@.*/\\1/p' \"$(basename \"$0\").d/baseline.tmux.log\")\n\n    # If this system can't actually report runtime, bail.\n    if [ -z \"$runtime\" ] || [ \"$runtime\" -eq \"-\" ]; then\n\texit 0\n    fi\n\n    # If we ran for more than one second, fail.\n    seconds=${runtime##*:}\n    bigger=${runtime%:*}\n    onesec=$(echo \"$seconds >= 1\" | bc)\n    if [ \"$onesec\" -eq 1 ] || [ \"$bigger\" -ne 0 ]; then\n\texit 1\n    fi\n    exit 0\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/nonce-incr.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n/* Tests that the Mosh network layer seems to be using unique nonces */\n\n#include <cstdlib>\n#include <iostream>\n#include <set>\n\n#include \"src/network/network.h\"\n\nint main()\n{\n  std::set<uint64_t> nonces;\n  const unsigned int NUM_EXAMPLES = 1000000;\n\n  for ( unsigned int i = 0; i < NUM_EXAMPLES; i++ ) {\n    Network::Packet packet( Network::TO_CLIENT, 0, 0, \"test\" );\n    nonces.insert( packet.toMessage().nonce.val() );\n  }\n\n  for ( unsigned int i = 0; i < NUM_EXAMPLES; i++ ) {\n    Network::Packet packet( Network::TO_SERVER, 0, 0, \"test\" );\n    nonces.insert( packet.toMessage().nonce.val() );\n  }\n\n  for ( unsigned int i = 0; i < NUM_EXAMPLES; i++ ) {\n    {\n      Network::Packet packet( Network::TO_SERVER, 0, 0, \"test\" );\n      nonces.insert( packet.toMessage().nonce.val() );\n    }\n\n    {\n      Network::Packet packet( Network::TO_CLIENT, 0, 0, \"test\" );\n      nonces.insert( packet.toMessage().nonce.val() );\n    }\n  }\n\n  if ( nonces.size() == 4 * NUM_EXAMPLES ) {\n    return EXIT_SUCCESS;\n  }\n\n  return EXIT_FAILURE;\n}\n"
  },
  {
    "path": "src/tests/ocb-aes.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n/* Test suite for the OCB-AES reference implementation included with Mosh.\n\n   This tests cryptographic primitives implemented by others.  It uses the\n   same interfaces and indeed the same compiled object code as the Mosh\n   client and server.  It does not particularly test any code written for\n   the Mosh project. */\n\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <memory>\n\n#include \"src/crypto/ae.h\"\n#include \"src/crypto/crypto.h\"\n#include \"src/crypto/prng.h\"\n#include \"src/util/fatal_assert.h\"\n#include \"test_utils.h\"\n\n#define KEY_LEN 16\n#define NONCE_LEN 12\n#define TAG_LEN 16\n\nusing Crypto::AlignedBuffer;\n\nbool verbose = false;\n\nstatic bool equal( const AlignedBuffer& a, const AlignedBuffer& b )\n{\n  return ( a.len() == b.len() ) && !memcmp( a.data(), b.data(), a.len() );\n}\n\nusing AlignedPointer = std::shared_ptr<AlignedBuffer>;\n\nstatic AlignedBuffer* get_ctx( const AlignedBuffer& key )\n{\n  AlignedBuffer* ctx_buf = new AlignedBuffer( ae_ctx_sizeof() );\n  fatal_assert( ctx_buf );\n  fatal_assert( AE_SUCCESS == ae_init( (ae_ctx*)ctx_buf->data(), key.data(), key.len(), NONCE_LEN, TAG_LEN ) );\n  return ctx_buf;\n}\n\nstatic void scrap_ctx( AlignedBuffer& ctx_buf )\n{\n  fatal_assert( AE_SUCCESS == ae_clear( (ae_ctx*)ctx_buf.data() ) );\n}\n\nstatic void test_encrypt( const AlignedBuffer& key,\n                          const AlignedBuffer& nonce,\n                          const AlignedBuffer& plaintext,\n                          const AlignedBuffer& assoc,\n                          const AlignedBuffer& expected_ciphertext )\n{\n  AlignedPointer ctx_buf( get_ctx( key ) );\n  ae_ctx* ctx = (ae_ctx*)ctx_buf->data();\n\n  AlignedBuffer observed_ciphertext( plaintext.len() + TAG_LEN );\n\n  const int ret = ae_encrypt( ctx,\n                              nonce.data(),\n                              plaintext.data(),\n                              plaintext.len(),\n                              assoc.data(),\n                              assoc.len(),\n                              observed_ciphertext.data(),\n                              NULL,\n                              AE_FINALIZE );\n\n  if ( verbose ) {\n    printf( \"ret %d\\n\", ret );\n    hexdump( observed_ciphertext, \"obs ct\" );\n  }\n\n  fatal_assert( ret == int( expected_ciphertext.len() ) );\n  fatal_assert( equal( expected_ciphertext, observed_ciphertext ) );\n\n  scrap_ctx( *ctx_buf );\n}\n\nstatic void test_decrypt( const AlignedBuffer& key,\n                          const AlignedBuffer& nonce,\n                          const AlignedBuffer& ciphertext,\n                          const AlignedBuffer& assoc,\n                          const AlignedBuffer& expected_plaintext,\n                          bool valid )\n{\n  AlignedPointer ctx_buf( get_ctx( key ) );\n  ae_ctx* ctx = (ae_ctx*)ctx_buf->data();\n\n  AlignedBuffer observed_plaintext( ciphertext.len() - TAG_LEN );\n\n  const int ret = ae_decrypt( ctx,\n                              nonce.data(),\n                              ciphertext.data(),\n                              ciphertext.len(),\n                              assoc.data(),\n                              assoc.len(),\n                              observed_plaintext.data(),\n                              NULL,\n                              AE_FINALIZE );\n\n  if ( verbose ) {\n    printf( \"ret %d\\n\", ret );\n  }\n\n  if ( valid ) {\n    if ( verbose ) {\n      hexdump( observed_plaintext, \"obs pt\" );\n    }\n    fatal_assert( ret == int( expected_plaintext.len() ) );\n    fatal_assert( equal( expected_plaintext, observed_plaintext ) );\n  } else {\n    fatal_assert( ret == AE_INVALID );\n  }\n\n  scrap_ctx( *ctx_buf );\n}\n\nstatic void test_vector( const char* key_p,\n                         const char* nonce_p,\n                         size_t assoc_len,\n                         const char* assoc_p,\n                         size_t plaintext_len,\n                         const char* plaintext_p,\n                         size_t ciphertext_len,\n                         const char* ciphertext_p )\n{\n\n  AlignedBuffer key( KEY_LEN, key_p );\n  AlignedBuffer nonce( NONCE_LEN, nonce_p );\n  AlignedBuffer plaintext( plaintext_len, plaintext_p );\n  AlignedBuffer assoc( assoc_len, assoc_p );\n  AlignedBuffer ciphertext( ciphertext_len, ciphertext_p );\n\n  if ( verbose ) {\n    hexdump( key, \"key\" );\n    hexdump( nonce, \"nonce\" );\n    hexdump( assoc, \"assoc\" );\n    hexdump( plaintext, \"exp pt\" );\n    hexdump( ciphertext, \"exp ct\" );\n  }\n\n  test_encrypt( key, nonce, plaintext, assoc, ciphertext );\n\n  test_decrypt( key, nonce, ciphertext, assoc, plaintext, true );\n\n  /* Try some bad ciphertexts and make sure they don't validate. */\n  PRNG prng;\n  for ( size_t i = 0; i < 64; i++ ) {\n    AlignedBuffer bad_ct( ciphertext.len(), ciphertext.data() );\n    ( (uint8_t*)bad_ct.data() )[prng.uint32() % bad_ct.len()] ^= ( 1 << ( prng.uint8() % 8 ) );\n    test_decrypt( key, nonce, bad_ct, assoc, plaintext, false );\n  }\n\n  if ( verbose ) {\n    printf( \"PASSED\\n\\n\" );\n  }\n}\n\n#define TEST_VECTOR( _key, _nonce, _assoc, _pt, _ct )                                                              \\\n  test_vector( _key, _nonce, sizeof( _assoc ) - 1, _assoc, sizeof( _pt ) - 1, _pt, sizeof( _ct ) - 1, _ct )\n\nstatic void test_all_vectors( void )\n{\n  /* Test vectors from http://tools.ietf.org/html/draft-krovetz-ocb-03#appendix-A */\n\n  const char ietf_key[] = \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\";\n  const char ietf_nonce[] = \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\";\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\" /* associated data */\n               ,\n               \"\" /* plaintext */\n               /* ciphertext including tag */\n               ,\n               \"\\x19\\x7B\\x9C\\x3C\\x44\\x1D\\x3C\\x83\\xEA\\xFB\\x2B\\xEF\\x63\\x3B\\x91\\x82\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\",\n               \"\\x92\\xB6\\x57\\x13\\x0A\\x74\\xB8\\x5A\\x16\\xDC\\x76\\xA4\\x6D\\x47\\xE1\\xEA\"\n               \"\\xD5\\x37\\x20\\x9E\\x8A\\x96\\xD1\\x4E\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\",\n               \"\",\n               \"\\x98\\xB9\\x15\\x52\\xC8\\xC0\\x09\\x18\\x50\\x44\\xE3\\x0A\\x6E\\xB2\\xFE\\x21\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\",\n               \"\\x92\\xB6\\x57\\x13\\x0A\\x74\\xB8\\x5A\\x97\\x1E\\xFF\\xCA\\xE1\\x9A\\xD4\\x71\"\n               \"\\x6F\\x88\\xE8\\x7B\\x87\\x1F\\xBE\\xED\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\x77\\x6C\\x99\\x24\\xD6\\x72\\x3A\\x1F\\xC4\\x52\\x45\\x32\\xAC\\x3E\\x5B\\xEB\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\",\n               \"\",\n               \"\\x7D\\xDB\\x8E\\x6C\\xEA\\x68\\x14\\x86\\x62\\x12\\x50\\x96\\x19\\xB1\\x9C\\xC6\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\x13\\xCC\\x8B\\x74\\x78\\x07\\x12\\x1A\\x4C\\xBB\\x3E\\x4B\\xD6\\xB4\\x56\\xAF\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xFC\\xFC\\xEE\\x7A\\x2A\\x8D\\x4D\\x48\\x5F\\xA9\\x4F\\xC3\\xF3\\x88\\x20\\xF1\"\n               \"\\xDC\\x3F\\x3D\\x1F\\xD4\\xE5\\x5E\\x1C\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\",\n               \"\\x28\\x20\\x26\\xDA\\x30\\x68\\xBC\\x9F\\xA1\\x18\\x68\\x1D\\x55\\x9F\\x10\\xF6\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xFC\\xFC\\xEE\\x7A\\x2A\\x8D\\x4D\\x48\\x6E\\xF2\\xF5\\x25\\x87\\xFD\\xA0\\xED\"\n               \"\\x97\\xDC\\x7E\\xED\\xE2\\x41\\xDF\\x68\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xFC\\xFC\\xEE\\x7A\\x2A\\x8D\\x4D\\x48\\x5F\\xA9\\x4F\\xC3\\xF3\\x88\\x20\\xF1\"\n               \"\\xDC\\x3F\\x3D\\x1F\\xD4\\xE5\\x5E\\x1C\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\",\n               \"\\x28\\x20\\x26\\xDA\\x30\\x68\\xBC\\x9F\\xA1\\x18\\x68\\x1D\\x55\\x9F\\x10\\xF6\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xFC\\xFC\\xEE\\x7A\\x2A\\x8D\\x4D\\x48\\x6E\\xF2\\xF5\\x25\\x87\\xFD\\xA0\\xED\"\n               \"\\x97\\xDC\\x7E\\xED\\xE2\\x41\\xDF\\x68\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xCE\\xAA\\xB9\\xB0\\x5D\\xF7\\x71\\xA6\\x57\\x14\\x9D\\x53\\x77\\x34\\x63\\xCB\"\n               \"\\xB2\\xA0\\x40\\xDD\\x3B\\xD5\\x16\\x43\\x72\\xD7\\x6D\\x7B\\xB6\\x82\\x42\\x40\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\",\n               \"\",\n               \"\\xE1\\xE0\\x72\\x63\\x3B\\xAD\\xE5\\x1A\\x60\\xE8\\x59\\x51\\xD9\\xC4\\x2A\\x1B\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xCE\\xAA\\xB9\\xB0\\x5D\\xF7\\x71\\xA6\\x57\\x14\\x9D\\x53\\x77\\x34\\x63\\xCB\"\n               \"\\x4A\\x3B\\xAE\\x82\\x44\\x65\\xCF\\xDA\\xF8\\xC4\\x1F\\xC5\\x0C\\x7D\\xF9\\xD9\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\"\n               \"\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\"\n               \"\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xCE\\xAA\\xB9\\xB0\\x5D\\xF7\\x71\\xA6\\x57\\x14\\x9D\\x53\\x77\\x34\\x63\\xCB\"\n               \"\\x68\\xC6\\x57\\x78\\xB0\\x58\\xA6\\x35\\x65\\x9C\\x62\\x32\\x11\\xDE\\xEA\\x0D\"\n               \"\\xE3\\x0D\\x2C\\x38\\x18\\x79\\xF4\\xC8\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\"\n               \"\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\",\n               \"\",\n               \"\\x7A\\xEB\\x7A\\x69\\xA1\\x68\\x7D\\xD0\\x82\\xCA\\x27\\xB0\\xD9\\xA3\\x70\\x96\" );\n\n  TEST_VECTOR( ietf_key,\n               ietf_nonce,\n               \"\",\n               \"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n               \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\"\n               \"\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\",\n               \"\\xBE\\xA5\\xE8\\x79\\x8D\\xBE\\x71\\x10\\x03\\x1C\\x14\\x4D\\xA0\\xB2\\x61\\x22\"\n               \"\\xCE\\xAA\\xB9\\xB0\\x5D\\xF7\\x71\\xA6\\x57\\x14\\x9D\\x53\\x77\\x34\\x63\\xCB\"\n               \"\\x68\\xC6\\x57\\x78\\xB0\\x58\\xA6\\x35\\x06\\x0C\\x84\\x67\\xF4\\xAB\\xAB\\x5E\"\n               \"\\x8B\\x3C\\x20\\x67\\xA2\\xE1\\x15\\xDC\" );\n\n  /* Some big texts.  These were originally encrypted using this program;\n     they are regression tests. */\n\n  TEST_VECTOR( \"\\x06\\xF8\\x9F\\x69\\xDA\\x49\\xDA\\xD7\\x68\\x48\\xFF\\xB3\\x60\\xB6\\x8F\\x00\",\n               \"\\xDC\\xBF\\x85\\x18\\x23\\xD9\\x67\\x85\\x45\\x59\\x6F\\xAD\",\n               \"\",\n               \"\\xC4\\x8E\\x1F\\x04\\x10\\x2F\\xA5\\x58\\x68\\x42\\x62\\xF3\\x1B\\xE7\\x63\\xA7\"\n               \"\\x77\\x89\\x64\\x16\\xE6\\xB0\\xF7\\xFA\\xFE\\xF0\\xB9\\x50\\x22\\xDC\\xCE\\x78\"\n               \"\\xA5\\x01\\xA4\\x2D\\xA2\\x0F\\x50\\xEA\\x9A\\xAE\\x23\\x60\\x1C\\xC9\\x11\\x84\"\n               \"\\x5F\\xD0\\x0A\\x88\\x99\\xCD\\xF1\\x1B\\x7C\\xF9\\x71\\xC2\\xD8\\xE3\\x7B\\xB1\"\n               \"\\xBC\\x91\\xCE\\x10\\xDA\\xED\\xBF\\xA5\\xDF\\x96\\x69\\x7A\\xFA\\xCA\\x9A\\x91\"\n               \"\\x40\\xF9\\x8F\\xF1\\xFE\\x5A\\x2B\\x21\\x17\\xA5\\xAE\\x97\\xBD\\x0B\\x68\\xE0\"\n               \"\\x58\\x7A\\xB4\\x10\\x89\\xDE\\x30\\x12\\xB7\\x14\\xEB\\xE8\\xFA\\xA5\\x87\\x7A\"\n               \"\\x7E\\xEA\\xD0\\x34\\x0B\\xAF\\x73\\x0B\\x1C\\xA7\\xC5\\xCB\\x84\\x15\\x5F\\x45\"\n               \"\\x53\\x8E\\x27\\xD7\\xA0\\x7D\\xAF\\x99\\x05\\x6E\\x9F\\xF4\\xCD\\xE5\\xA1\\x11\"\n               \"\\x43\\xBD\\x7A\\x24\\xA3\\xB7\\xB6\\x92\\x7C\\xB7\\xD8\\x6F\\x17\\x78\\xB3\\x30\"\n               \"\\x53\\x6E\\x46\\x14\\x11\\x86\\xF6\\x70\\x2E\\x25\\x8E\\x54\\xF8\\x70\\x2F\\x47\"\n               \"\\x2B\\xF8\\xF6\\xC3\\xEE\\xFB\\x9B\\x7D\\xF0\\x73\\xAD\\x24\\xC0\\xE9\\x58\\xA8\"\n               \"\\x79\\xDC\\x92\\x25\\x6D\\xF3\\x9D\\xA8\\x8F\\x07\\xA3\\xE7\\xAB\\xF4\\xBC\\x32\"\n               \"\\x29\\xB6\\xEF\\xDB\\x20\\x42\\x51\\xCF\\xD5\\x57\\xDA\\x14\\xC7\\x2A\\xB3\\x19\"\n               \"\\xDE\\x31\\xF7\\x77\\x95\\x8D\\x33\\x57\\x1D\\xEB\\x9A\\x80\\x98\\x4D\\x95\\x07\"\n               \"\\xEB\\x38\\x42\\xEB\\xE0\\xAB\\x2D\\xF3\\x9E\\x16\\xFA\\x6D\\x06\\x62\\x41\\x58\"\n               \"\\x81\\xBF\\x3D\\x01\\x1A\\xF7\\xBA\\x53\\xCE\\x60\\xEF\\x86\\xC9\\x11\\xED\\x89\"\n               \"\\x5B\\xCF\\x98\\xB4\\xD3\\xD4\\xAC\\xB9\\x71\\x69\\xEE\\xF6\\xA7\\x77\\x4E\\x46\"\n               \"\\x0F\\xF7\\x1D\\x7D\\x36\\x4E\\x86\\x15\\xCD\\xF3\\xD3\\x5C\\xAA\\x97\\x74\\xCC\"\n               \"\\x5D\\xD6\\xF0\\x5C\\x62\\x27\\x0D\\x4B\\x01\\xDD\\x8B\\xE6\\x2C\\x72\\x99\\xBA\"\n               \"\\x5B\\x95\\x87\\x0F\\xB9\\x9A\\xBE\\xA2\\x42\\x5E\\x7D\\x1B\\x19\\x08\\xBC\\xEF\"\n               \"\\x66\\x24\\x2F\\xB3\\xFC\\xE7\\xDA\\xB1\\x79\\x51\\xE9\\x3B\\x45\\x66\\xDC\\xD8\"\n               \"\\x0F\\xA7\\x56\\xBF\\x56\\x15\\x4B\\x15\\xCC\\x3E\\x9F\\x44\\xB9\\xED\\xC2\\x0A\"\n               \"\\x7C\\xA9\\x6D\\x87\\xCD\\xE2\\x00\\x9F\\x2F\\x2C\\xF0\\xBC\\x1B\\xB0\\x27\\xE8\"\n               \"\\x46\\x3E\\x06\\x1E\\xD5\\xB6\\xB4\\x82\\xF7\\x88\\xB7\\x48\\xF3\\x58\\xB0\\x23\"\n               \"\\x6D\\xC2\\xD3\\x85\\x9D\\x1F\\xE5\\x30\\xFC\\xC8\\x46\\x62\\xD1\\xE1\\xAE\\x3B\"\n               \"\\x3A\\xA9\\x57\\xE0\\xD7\\x8F\\x7E\\x4D\\x59\\x7D\\x7A\\xEB\\xBF\\xD3\\x10\\x00\"\n               \"\\xA0\\xE4\\x76\\x76\\xE3\\xA8\\x14\\xD0\\x03\\xBF\\x1F\\x6A\\xF9\\x11\\xCE\\x98\"\n               \"\\x2C\\x2A\\x86\\x25\\x77\\x85\\x14\\x76\\xD4\\x51\\xAB\\xC7\\x3A\\xA7\\xE1\\xF7\"\n               \"\\x23\\xF7\\x2B\\xA3\\xBA\\xE4\\x0B\\xA4\\x81\\x9A\\x83\\x98\\x69\\xC3\\x1C\\x8A\"\n               \"\\xBD\\x26\\x12\\x36\\x22\\x9D\\xCE\\x85\\x5D\\xA3\\xA0\\xDF\\x66\\xD0\\x59\\xF6\"\n               \"\\x47\\xF2\\xC5\\x37\\xF1\\x62\\x0D\\x0C\\x45\\x5B\\xE5\\xFE\\x3C\\x8D\\x28\\x75\",\n               \"\\xa1\\xd8\\xa0\\xe0\\x75\\x5c\\xb4\\xf4\\xab\\x59\\x6d\\x14\\xfc\\x2e\\x75\\x54\"\n               \"\\xa3\\x35\\x4f\\x57\\x69\\x48\\x7a\\x46\\x17\\x5f\\xd9\\x34\\x50\\xf9\\x35\\xe5\"\n               \"\\x6f\\xee\\x27\\xdb\\x28\\x0f\\x06\\x0b\\xaf\\xd5\\x50\\x4e\\x20\\x78\\x35\\xd6\"\n               \"\\x4d\\xa0\\x18\\xe8\\x6c\\x5b\\x07\\xbb\\xb6\\xd0\\x3f\\x4a\\x0e\\x14\\x32\\xaf\"\n               \"\\x0d\\x6a\\x5d\\xb6\\xe4\\x36\\xb0\\x1c\\xae\\x2e\\x75\\x85\\x19\\x53\\x9d\\xf2\"\n               \"\\xc6\\xc6\\xf5\\x29\\x4a\\x5d\\x73\\xd6\\xcd\\x3a\\xec\\x38\\x15\\x7b\\x1f\\x3d\"\n               \"\\x19\\x8f\\x7f\\x85\\x53\\x75\\xfe\\x6c\\x8a\\x5d\\x4f\\x05\\x3f\\x16\\x5e\\x73\"\n               \"\\x9c\\xbe\\xbe\\xdb\\xe8\\xb2\\x3e\\xea\\x0f\\x2e\\x2c\\xf3\\x2c\\x9c\\x56\\x5b\"\n               \"\\xf7\\xf2\\xed\\x87\\x07\\xfb\\x87\\x74\\x09\\xda\\xb8\\xad\\x57\\xc0\\xa0\\x79\"\n               \"\\xc6\\x69\\x1c\\x79\\x08\\x69\\x35\\x18\\xc4\\x54\\xca\\x6b\\xed\\x89\\xa0\\x27\"\n               \"\\x32\\x19\\x1f\\xaf\\xff\\x12\\x1c\\x1b\\x08\\xa6\\x9f\\x0e\\xe3\\x01\\x98\\x77\"\n               \"\\xdb\\x75\\xdf\\x87\\x67\\x6d\\xd9\\xb4\\x23\\x39\\xd0\\x81\\x54\\xf8\\x89\\x18\"\n               \"\\x5c\\xbd\\xb7\\x4d\\xf4\\xb1\\x8b\\x37\\x2e\\x9b\\x20\\x29\\x9f\\x95\\x9e\\xdd\"\n               \"\\x73\\x2c\\xdb\\x37\\xe5\\x7d\\x9f\\x8a\\x79\\xff\\x8f\\x5b\\x99\\x10\\xe1\\xe9\"\n               \"\\x1d\\xcb\\x1f\\x17\\xef\\x7d\\xb7\\xd1\\x50\\x23\\x4a\\x38\\x92\\xfb\\xbe\\x1c\"\n               \"\\xff\\x23\\x68\\x3a\\xb9\\xc8\\xbc\\xa0\\xab\\xa7\\xbe\\xc8\\x84\\xce\\x66\\xa7\"\n               \"\\x11\\x17\\xc6\\x48\\x91\\x4d\\x77\\xe1\\x64\\xc4\\x26\\x08\\xcd\\xb4\\xea\\x10\"\n               \"\\x42\\x60\\x5a\\x7c\\x5a\\x72\\xba\\xb4\\x93\\xf7\\x02\\xa1\\xd4\\x44\\xdb\\x1a\"\n               \"\\x4a\\xc3\\xb1\\xea\\x69\\x74\\xea\\x18\\xb3\\x5a\\x27\\x09\\x6f\\x5b\\x1f\\x30\"\n               \"\\xe2\\xeb\\x2a\\x37\\x5e\\xd2\\xe2\\x23\\xec\\xbc\\xcb\\xcb\\x65\\x41\\xed\\x0e\"\n               \"\\x23\\xda\\x71\\x45\\xf3\\x3f\\x7d\\x44\\x73\\xd7\\x14\\x4d\\xab\\x03\\xf7\\x7d\"\n               \"\\x24\\x33\\x9d\\x25\\x83\\x1f\\x3d\\xf3\\xc9\\x34\\x42\\x6c\\x2f\\x61\\xa2\\x83\"\n               \"\\xa0\\x0f\\xb6\\x28\\xe8\\x27\\xf0\\x91\\xf6\\xc5\\x7e\\xa1\\x74\\x74\\xaf\\xa7\"\n               \"\\x7f\\xba\\x99\\x29\\xb2\\x91\\x9f\\xf0\\xf7\\x71\\x1f\\xf0\\x0b\\xd6\\xd9\\xf9\"\n               \"\\x72\\xaa\\x04\\x09\\x5d\\x75\\x76\\x5f\\x18\\xc8\\xd5\\xd5\\x84\\x1a\\x40\\x3f\"\n               \"\\xe5\\x38\\xe4\\x38\\xdf\\xf9\\x0d\\x79\\xdc\\x71\\xc8\\xa8\\xef\\x86\\x4e\\x26\"\n               \"\\xf4\\xc2\\x46\\xf4\\xe0\\x64\\xd6\\x5e\\xfb\\x1c\\x47\\x58\\x3d\\x87\\x7c\\xba\"\n               \"\\xa3\\xf5\\x98\\xdc\\xd5\\xdf\\xaf\\x62\\x96\\xee\\x4e\\x39\\x32\\x4c\\x6d\\xd2\"\n               \"\\x0a\\x6f\\xf8\\x34\\x98\\x76\\xae\\x21\\xa1\\x41\\x3d\\x96\\xfc\\x52\\xdb\\xdd\"\n               \"\\xf3\\x9a\\xb1\\xf3\\x78\\x3d\\xb8\\x2f\\xae\\xae\\x7d\\xe0\\x4b\\xb2\\xdf\\x2b\"\n               \"\\x12\\xac\\xee\\xfb\\xf8\\x54\\x60\\xea\\x74\\xd5\\xde\\x43\\x7b\\x38\\x45\\x5f\"\n               \"\\xa0\\xb8\\xe8\\xcc\\x8a\\xe3\\xcc\\x0c\\x92\\xe6\\xb1\\xb0\\xe2\\xc1\\x99\\xca\"\n               \"\\x1b\\xa1\\xac\\x6e\\x7a\\x8a\\xa0\\x20\\x3d\\xeb\\x29\\x8b\\xf4\\x55\\x41\\x62\" );\n\n  TEST_VECTOR( \"\\x7A\\x54\\x0D\\x3E\\x56\\x38\\xF7\\xC6\\xCF\\xAB\\xF9\\x56\\xDC\\xCA\\x14\\x23\",\n               \"\\x9B\\x0E\\xC1\\x15\\xD5\\xE6\\xC9\\xAB\\xE6\\x88\\x2A\\x18\",\n               \"\",\n               \"\\x52\\xDB\\xA7\\x44\\x2B\\x1C\\x9C\\x24\\x4D\\xF3\\xA1\\xE4\\x53\\x7B\\x9B\\xB2\"\n               \"\\x25\\xC5\\xA3\\x81\\x42\\x23\\xA9\\xB4\\x12\\xF8\\xFC\\xE4\\xF6\\x8E\\x20\\xD4\"\n               \"\\x59\\x7B\\x39\\x2D\\x5D\\x7C\\x6E\\xB7\\x51\\x02\\x90\\x7A\\x8E\\xAA\\x30\\xD0\"\n               \"\\xEB\\xDF\\x70\\x09\\x5A\\xEC\\xFB\\xD4\\xDB\\x0B\\xE9\\x1B\\x79\\xAF\\x40\\xA3\"\n               \"\\xC9\\xD1\\x3F\\x0E\\x7E\\x9B\\xA8\\xF2\\x4E\\xA2\\xAE\\x81\\xE7\\x8F\\xE9\\x4A\"\n               \"\\xF8\\x84\\x38\\x20\\x9B\\xCB\\x0D\\x9E\\x46\\x87\\xCC\\x0C\\x71\\xF2\\x4C\\xE5\"\n               \"\\x57\\x47\\xFB\\x52\\x1D\\x0C\\x92\\xCD\\x9B\\xD0\\xA3\\xA3\\xFE\\x44\\x4F\\x2A\"\n               \"\\x2D\\x7D\\xC8\\x48\\x87\\x71\\x8A\\x83\\x83\\x43\\x52\\xD9\\x87\\x72\\x0A\\x1F\"\n               \"\\x72\\x7D\\xE0\\x0D\\xE4\\xBA\\x38\\x2D\\xAB\\x89\\x85\\x34\\xFE\\x30\\xE9\\x47\"\n               \"\\x99\\x69\\x96\\x37\\x3B\\x88\\x0D\\x08\\x5B\\x41\\xF0\\x55\\x3A\\x90\\xB6\\x5D\"\n               \"\\xC9\\xB8\\xFA\\x22\\xEB\\x54\\x67\\x1B\\x43\\x3B\\x2A\\x13\\xDF\\x76\\x71\\xBF\"\n               \"\\x8F\\xBA\\xF6\\x5F\\xE0\\xAE\\x84\\x80\\xD3\\x9A\\x9E\\xFE\\x35\\x29\\x0C\\x44\"\n               \"\\xB8\\x3B\\xC8\\x58\\x3F\\x51\\x18\\xF0\\x8E\\x3F\\xE0\\xFE\\xFF\\x55\\xE1\\xFD\"\n               \"\\x79\\xED\\x71\\x6D\\xB9\\xBE\\xF0\\x76\\x25\\x4E\\x6F\\xA2\\x49\\x5B\\xEC\\x20\"\n               \"\\x61\\x95\\x65\\x67\\xBF\\xA3\\x73\\x18\\x65\\x7C\\x2F\\x94\\x29\\x3B\\x57\\x7D\"\n               \"\\x78\\x6A\\xBD\\x22\\x2B\\x17\\x5E\\xDA\\x2B\\x4E\\xC3\\xA5\\xF0\\x9A\\xD9\\x1C\"\n               \"\\xCF\\xC4\\xDD\\x7E\\x07\\xB4\\xE6\\x50\\xD0\\x9D\\xAF\\x86\\x9D\\x3F\\xFE\\x70\"\n               \"\\x64\\x18\\xBA\\x32\\x90\\xE5\\x63\\xA0\\x08\\x3D\\x1D\\xCE\\xB2\\x77\\x31\\x59\"\n               \"\\xD4\\xB4\\x03\\x2C\\xE2\\x15\\x11\\x17\\xD1\\xBA\\x5E\\x1E\\x7D\\x38\\x09\\xCF\"\n               \"\\x25\\x3E\\x3D\\x89\\x7E\\x13\\xAE\\x11\\xFA\\xCD\\x46\\x23\\x86\\xC5\\x78\\xC8\"\n               \"\\x1A\\xE8\\x8F\\x9A\\x9C\\xCE\\xBE\\x01\\x5D\\x70\\x56\\xA8\\x84\\x07\\x07\\x2A\"\n               \"\\x1C\\xD4\\x33\\x8B\\x2F\\x01\\x0F\\xD0\\xCC\\xCD\\x29\\xEC\\x73\\x6E\\xDE\\xB2\"\n               \"\\xBC\\xA7\\x32\\x6A\\x12\\x93\\x13\\xCD\\xE0\\xA9\\x55\\x97\\x36\\x6B\\xDA\\xC5\"\n               \"\\xC5\\xD2\\x44\\x80\\xBD\\x00\\x37\\xD7\\x21\\x80\\x27\\x1F\\x2E\\x02\\x76\\x1A\"\n               \"\\x57\\xA5\\x5F\\xCE\\x8B\\x20\\x6A\\x6A\\x0F\\x70\\x9B\\xDB\\x07\\x64\\x96\\x7B\"\n               \"\\x43\\xFD\\xCA\\xBF\\x9D\\x52\\xEB\\x24\\x7B\\x1B\\xEE\\x43\\x10\\x2C\\xDB\\x92\"\n               \"\\xA1\\xA8\\xA9\\xF7\\x2F\\xD2\\x39\\xA8\\x85\\x9C\\xFE\\x2C\\x2A\\xCF\\x52\\x73\"\n               \"\\xFB\\xCA\\x20\\xAD\\xC9\\xDD\\xFC\\x4A\\x91\\x39\\x6C\\x7C\\x84\\x67\\xC5\\xE4\"\n               \"\\x9B\\x3E\\x3D\\x6B\\x56\\x3B\\x2B\\xDC\\x8A\\x46\\xF6\\x7C\\x36\\xF9\\x27\\x29\"\n               \"\\x37\\x38\\x7C\\x9D\\xA0\\x6E\\x5D\\x4C\\xE5\\xB2\\x6F\\x0C\\xDC\\xEF\\xFE\\x35\"\n               \"\\xFE\\x3D\\x56\\x40\\x7F\\xBD\\x4D\\xDD\\x40\\x79\\xDD\\xA7\\x0A\\x7B\\xA2\\xCE\"\n               \"\\x22\\x38\\x94\\xEA\\x90\\xF5\\x95\\xB6\\xE6\\x6F\\x14\\xFB\\xA2\",\n               \"\\xec\\xa9\\xcc\\x30\\x66\\x6c\\x04\\x16\\x21\\x8d\\xc8\\x15\\x47\\xa2\\x18\\xcf\"\n               \"\\x19\\x90\\x4f\\x82\\x27\\x25\\xa2\\x1e\\xfa\\x1c\\xe4\\x58\\x78\\x43\\x52\\x4c\"\n               \"\\xac\\x24\\xde\\xcb\\xad\\x80\\x05\\x7a\\xeb\\x2d\\xc0\\x33\\x05\\x31\\x25\\x44\"\n               \"\\xd7\\x11\\xa1\\xf2\\xcb\\x09\\x6f\\xf0\\x14\\x3c\\x3f\\xf2\\xc7\\x79\\xfb\\x3f\"\n               \"\\xb0\\x0a\\x65\\xae\\xd7\\xe5\\x5d\\x35\\x0c\\xb4\\x69\\x7a\\x89\\x6b\\xa9\\xdc\"\n               \"\\x02\\x69\\x96\\xd2\\x9f\\xe7\\x3c\\x99\\xd4\\xd3\\x55\\x97\\xc4\\x59\\xad\\xc4\"\n               \"\\x0c\\x7b\\xf8\\x47\\x1c\\xbe\\x36\\x2a\\x53\\x6b\\xb1\\x21\\x5f\\xc1\\x6e\\xca\"\n               \"\\x0a\\x4f\\x16\\x3a\\xf0\\xd4\\x12\\xa6\\xf8\\x68\\x9f\\x12\\xad\\x36\\x4c\\xd8\"\n               \"\\x5a\\x5b\\x17\\xb8\\xbd\\xc7\\x2c\\x48\\x51\\x7d\\x45\\x74\\x00\\xb0\\x02\\xe9\"\n               \"\\x1b\\xd6\\x0c\\x41\\xa7\\x5d\\x65\\xca\\x68\\xa7\\x3e\\x3c\\xf5\\xaa\\x9b\\xbd\"\n               \"\\x25\\x98\\x00\\xd8\\x4d\\xbc\\xd1\\x7a\\x25\\x34\\x92\\x24\\xa4\\x84\\x62\\x63\"\n               \"\\x2c\\x40\\xa5\\x58\\x81\\x90\\xf1\\x0f\\x75\\xaa\\x70\\xe4\\x4e\\x0f\\xa3\\x03\"\n               \"\\x90\\xd1\\x07\\x18\\x0a\\x50\\x9a\\x3e\\x28\\x1f\\x33\\xb7\\x11\\xed\\x3c\\x2c\"\n               \"\\x40\\xc8\\xd7\\xe3\\x12\\xdc\\xef\\x94\\x93\\x7b\\x11\\xc3\\x24\\x51\\x61\\xbf\"\n               \"\\x8b\\xa4\\xa4\\x5c\\x85\\x0d\\x50\\x49\\x45\\x69\\xbe\\x5b\\x36\\x90\\x84\\x30\"\n               \"\\x66\\x67\\x76\\x3d\\xcc\\x02\\x8b\\x9f\\xb7\\x90\\x57\\xef\\xe1\\x21\\x34\\x65\"\n               \"\\x3e\\xca\\xbf\\x70\\x1d\\x76\\x63\\xbf\\xae\\x1f\\xb2\\x55\\xf0\\x87\\x3e\\x42\"\n               \"\\xf9\\x71\\x28\\x02\\x06\\x9e\\xf7\\x6a\\x47\\x3b\\xda\\x38\\x54\\x66\\xd9\\xaf\"\n               \"\\xba\\x7b\\xec\\xbf\\xe3\\x52\\x63\\x02\\x8b\\xa7\\xad\\x1d\\x76\\x16\\xa2\\x20\"\n               \"\\x38\\xec\\x40\\xb7\\xc8\\x35\\x6b\\xc2\\x80\\x9d\\x20\\x02\\xc6\\x34\\xdb\\x65\"\n               \"\\xd8\\x27\\x0b\\xc5\\x2d\\x85\\xe4\\xdc\\x85\\xae\\x10\\x36\\x01\\xdb\\x4b\\xaf\"\n               \"\\x44\\x79\\xea\\x23\\x21\\xa0\\x83\\xa3\\x91\\xf5\\xc5\\x16\\x9b\\xeb\\x43\\x92\"\n               \"\\x1f\\x88\\xd2\\x00\\x60\\x40\\xe9\\x52\\x0b\\x39\\x86\\x3b\\x9e\\x3b\\x9a\\x4a\"\n               \"\\x31\\xdf\\xb6\\x57\\x78\\x38\\xcf\\x77\\x7c\\x0c\\xf4\\x14\\x90\\x25\\xed\\x27\"\n               \"\\xd2\\x86\\x20\\x4c\\x1a\\x52\\xeb\\xbe\\x1e\\xac\\x2b\\xce\\xb7\\x72\\x86\\x87\"\n               \"\\xfd\\xac\\x11\\x90\\xc5\\xea\\x96\\xcb\\xdc\\x89\\xe9\\x77\\xf0\\x83\\xc3\\xa7\"\n               \"\\xa7\\xd1\\xe1\\xc9\\x7e\\x89\\xb3\\x4e\\xf1\\x12\\xa3\\x9c\\xfe\\x66\\xcc\\x5d\"\n               \"\\xcf\\x1d\\x5a\\x11\\x21\\x2f\\x10\\x66\\x37\\x5f\\xd7\\x35\\xeb\\x09\\x62\\x99\"\n               \"\\xa6\\xf8\\xc7\\xc7\\xef\\xd3\\xf0\\x56\\x2b\\xa7\\x14\\x65\\x6a\\xce\\xa9\\x68\"\n               \"\\xe7\\xa4\\x89\\xb4\\x1e\\x16\\x99\\xbf\\x8d\\x2d\\x5e\\x67\\xb4\\x3a\\x0b\\xf3\"\n               \"\\x37\\x14\\x1e\\x5d\\xc6\\xb4\\xb5\\x9e\\xa5\\x69\\xa4\\xaf\\xcc\\x0f\\x46\\xe9\"\n               \"\\xd5\\xbb\\x10\\x49\\x07\\x0d\\x92\\x42\\x0c\\x04\\xb9\\xdf\\xa4\\xb5\\xef\\xcc\"\n               \"\\x05\\x81\\x3f\\xc1\\x21\\x12\\x2c\\x33\\xb7\\x79\\xfd\\x5d\\x8a\" );\n}\n\n/* http://tools.ietf.org/html/draft-krovetz-ocb-03#appendix-A\n   also specifies an iterative test algorithm, which we implement here. */\n\nstatic void test_iterative( void )\n{\n  /* Key is always all zeros */\n  AlignedBuffer key( KEY_LEN );\n  memset( key.data(), 0, KEY_LEN );\n\n  AlignedPointer ctx_buf( get_ctx( key ) );\n  ae_ctx* ctx = (ae_ctx*)ctx_buf->data();\n\n  AlignedBuffer nonce( NONCE_LEN );\n  memset( nonce.data(), 0, NONCE_LEN );\n\n  /* The loop below fills this buffer in order.\n     Each iteration adds 2*i + 3*TAG_LEN bytes. */\n  AlignedBuffer accumulator( 22400 );\n  uint8_t* acc = (uint8_t*)accumulator.data();\n\n  for ( size_t i = 0; i < 128; i++ ) {\n    /* i bytes of zeros */\n    AlignedBuffer s( i );\n    memset( s.data(), 0, s.len() );\n\n    /* Nonce is 11 bytes of zeros followed by 1 byte i */\n    ( (uint8_t*)nonce.data() )[11] = i;\n\n    /* We can't write directly to acc because it might not be aligned. */\n    AlignedBuffer out( s.len() + TAG_LEN );\n\n    /* OCB-ENCRYPT(K,N,S,S) */\n    fatal_assert(\n      0 <= ae_encrypt( ctx, nonce.data(), s.data(), s.len(), s.data(), s.len(), out.data(), NULL, AE_FINALIZE ) );\n    memcpy( acc, out.data(), s.len() + TAG_LEN );\n    acc += s.len() + TAG_LEN;\n\n    /* OCB-ENCRYPT(K,N,<empty string>,S) */\n    fatal_assert( 0 <= ae_encrypt( ctx, nonce.data(), s.data(), s.len(), NULL, 0, out.data(), NULL, AE_FINALIZE ) );\n    memcpy( acc, out.data(), s.len() + TAG_LEN );\n    acc += s.len() + TAG_LEN;\n\n    /* OCB-ENCRYPT(K,N,S,<empty string>) */\n    fatal_assert( 0 <= ae_encrypt( ctx, nonce.data(), NULL, 0, s.data(), s.len(), out.data(), NULL, AE_FINALIZE ) );\n    memcpy( acc, out.data(), TAG_LEN );\n    acc += TAG_LEN;\n  }\n\n  /* OCB-ENCRYPT(K,N,C,<empty string>) */\n  AlignedBuffer out( TAG_LEN );\n  memset( nonce.data(), 0, NONCE_LEN );\n  fatal_assert(\n    0 <= ae_encrypt(\n      ctx, nonce.data(), NULL, 0, accumulator.data(), accumulator.len(), out.data(), NULL, AE_FINALIZE ) );\n\n  /* Check this final tag against the known value */\n  AlignedBuffer correct( TAG_LEN, \"\\xB2\\xB4\\x1C\\xBF\\x9B\\x05\\x03\\x7D\\xA7\\xF1\\x6C\\x24\\xA3\\x5C\\x1C\\x94\" );\n  fatal_assert( equal( out, correct ) );\n\n  if ( verbose ) {\n    printf( \"iterative PASSED\\n\\n\" );\n  }\n  scrap_ctx( *ctx_buf );\n}\n\nint main( int argc, char* argv[] )\n{\n  if ( argc >= 2 && strcmp( argv[1], \"-v\" ) == 0 ) {\n    verbose = true;\n  }\n\n  try {\n    test_all_vectors();\n    test_iterative();\n  } catch ( const std::exception& e ) {\n    fprintf( stderr, \"Error: %s\\r\\n\", e.what() );\n    return 1;\n  }\n  return 0;\n}\n"
  },
  {
    "path": "src/tests/prediction-unicode.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test for a bug seen where mosh-client's\n# prediction code would sometimes show \"gück\" when \"glück\" was typed.\n# mosh-client would output a predicted Unicode input character\n# first as an 8-bit character containing the lowest 8 bits of the\n# Unicode code point, then redraw it correctly with its UTF-8 sequence\n# when the prediction is validated.  For many accented Latin\n# characters, the 8-bit character is an illegal UTF-8 code sequence.\n# Most terminal emulators will output the Unicode replacement\n# character, which is only visible until validation.  urxvt, however,\n# draws no character and does not change the cursor location on an\n# illegal UTF-8 sequence, causing this bug to be visible as ongoing\n# display corruption.  A subset of wide characters (including CJK)\n# will show display corruption with all terminal emulators, because a\n# narrow replacement character will be drawn when a wide character\n# should have been.\n#\n# tmux draws a replacement character for invalid UTF-8, and we\n# depend on that in this test.\n#\n# Another similar failing case is typing \"faĩl\".  In this case the \"ĩ\"\n# would be predicted as \")\" before being replaced by the\n# correct character.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" tmux baseline mosh-args post\n    exit\nfi\n\ntmux_commands()\n{\n    for x in $(seq 1 5); do\n\tfor y in $(seq 1 5); do \n\t    for i in \"gl\" ü \"ck fa\" ĩ \"l \"; do\n\t\tprintf \"send-keys '%s'\\n\" \"$i\"\n\t\tsleepf\n\t    done\n\tdone\n\tprintf \"send-keys 0x0d\\n\"\n    done\n    printf \"send-keys 0x0d\\n\"\n    sleep 1\n    printf \"send-keys 0x04\\n\"\n    # Unreliable on Cygwin under load, it seems.\n    sleep 1\n    printf \"send-keys 0x04\\n\"\n    # This will get killed by SIGPIPE.\n    while printf \"show-options\\n\" && sleep 1; do\n\t:\n    done\n} \n\ntmux_stdin()\n{\n    tmux_commands | \"$@\"\n    exit\n}\n\nbaseline()\n{\n    # Just receive and toss input in canonical mode.\n    cat > /dev/null\n}\n\npost()\n{\n    # Look for bad output: ')' or \\374\n    (\n\tunset LC_ALL\n\tunset LC_CTYPE\n\tunset LANGUAGE\n\t! env LANG=C egrep -q \"%output %0 (\\)|$(printf \\\\374))\" \"$(basename \"$0\").d/baseline.tmux.log\"\n\t# Implicit exitcode return.\n    )\n    return $?\n}\n\ncase $1 in\n    tmux)\n\tshift;\n\ttmux_stdin \"$@\";;\n    baseline)\n\tbaseline;;\n    mosh-args)\n\tprintf \"%s\\n\" \"--predict=always\";;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/print-exitstatus",
    "content": "#!/usr/bin/env perl\n\n#\n# Print exitstatus on stderr.\n#\nuse warnings;\nuse strict;\n\nmy $rc = system(@ARGV);\nif ($? == -1) {\n    die \"system failed: %!\\n\";\n}\nif ($? == 0) {\n    $rc = 0;\n} elsif ($? >= 256) {\n    $rc = $? >> 8;\n} else {\n    $rc = ($? & 127) | 128;\n}\nprint STDERR \"@@@ exitstatus: ${rc} @@@\\n\";\n# Now look for it in log file.\nmy $grepfilename = $ENV{'MOSH_E2E_TEST'} . \".tmux.log\";\nfor my $i (1..600) {\n    open(my $grepfile, \"<\", $grepfilename) or die;\n    while (<$grepfile>) {\n\tchomp;\n\t/@@@ exitstatus: .* @@@/ && goto gotit;\n    }\n    close($grepfile);\n    sleep .1;\n}\n gotit:\nexit $rc;\n"
  },
  {
    "path": "src/tests/pty-deadlock.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test for a BSD pty bug in Mosh.  On\n# FreeBSD/OpenBSD/OS X, a pty master can block on read() after\n# select() has informed us that there is data available, if a ^S is\n# written to the pty master between the select() and the read().\n#\n# Unfortunately, everything attached to the pty gets stuck when this\n# happens.  If this tests fails, you will need to do some manual\n# cleanup with kill -9.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" tmux baseline post\n    exit\nfi\n\ntmux_commands()\n{\n    # An interactive shell is waiting for us in the mosh session.\n    # Start test...\n    sleep 2\n    printf \"send-keys 0x0d\\n\"\n    sleep 5\n    # Stop output...\n    printf \"send-keys 0x13\\n\"\n    sleep 10\n    # Restart output...\n    printf \"send-keys 0x11\\n\"\n    sleep 10\n    # And stop the test script, so it produces its exit messge.\n    printf \"send-keys 0x0d\\n\"\n    # This will get killed by SIGPIPE.\n    while printf \"show-options\\n\" && sleep 1; do\n\t:\n    done\n}\n\ntmux_stdin()\n{\n    tmux_commands | \"$@\"\n    exit\n}\n\nbaseline()\n{\n    # Make a lot of noise on stdout to keep mosh busy, and exit\n    # with a distinctive message when we get a CR.  Exit after 10s in any case.\n    blat=$(for i in $(seq 1 100); do\n\t       printf 'a\\nb\\nc\\nd\\ne\\nf\\ng\\nh\\ni\\nj\\nk\\nl\\nm\\nn\\no\\np\\nq\\nr\\ns\\nt\\nu\\nv\\nw\\nx\\ny\\nz\\n'\n\t   done)\n    read -r x\n    while printf '%s' \"$blat\"; do\n\t:\n    done &\n    printpid=$!\n    (sleep 120; kill $$ $printpid) &\n    killpid=$!\n    read -r x\n    kill $printpid\n    # Try and make sure the printer stops writing before the following printf\n    sleep 1\n    printf \"\\n=== normal exit ===\\n\"\n    # Let tty queues drain, so the exit message gets to mosh-client\n    # before we exit\n    sleep 4\n    # Kill the killer and exit normally.\n    kill $killpid\n}\n\npost()\n{\n    if grep -q '=== normal exit ===' \"$(basename \"$0\").d/baseline.capture\"; then\n\texit 0\n    fi\n    exit 1\n}\n\ncase $1 in\n    tmux)\n\tshift;\n\ttmux_stdin \"$@\";;\n    baseline)\n\tbaseline;;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/repeat.test",
    "content": "#!/bin/sh\n\n#\n# Run mosh many times.  If run as repeat-with-stdin.test, also send\n# input constantly, to try and trigger a bug where mosh-server would\n# exit on OS X if input was received after the client session had\n# exited.\n#\n\n# 100 iterations runs in comparable time to other tests.\n: ${REPEAT_TEST_LOOPCOUNT:=100}\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    do_tmux=\n    case $(basename \"$0\" .test) in\n\trepeat-with-input)\n\t    do_tmux=tmux;;\n    esac\n    e2e-test \"$0\" baseline client server ${do_tmux}\n    exit\nfi\n\n# Run mosh repeatedly\nclient()\n{\n    for i in $(seq 1 $REPEAT_TEST_LOOPCOUNT); do\n\t(sleep 15; kill $$) &\n\tkillpid=$!\n\tif ! \"$@\"; then\n\t    printf \"### iteration %d failed\\n\" \"$i\"\n\t    kill $killpid\n\t    exit 1\n\tfi\n\tkill $killpid\n    done\n}\n\n# e2e-test-server is slow because of its screen capture; this simple\n# wrapper is faster.\nserver()\n{\n    shift\n    eval \"$@\"\n}\n\n# Constantly send keyboard input.\ntmux_commands()\n{\n    while printf \"send-keys 0x0d\\n\" && sleepf; do\n\t:\n    done\n}\n\ntmux_stdin()\n{\n    tmux_commands | \"$@\"\n    exit\n}\n\nbaseline()\n{\n    printf \"@@@ done\\n\"\n}\n\npost()\n{\n    if [ \"$(grep -c \"@@@ done\" \"$(basename \"$0\").d/baseline.tmux.log\")\" -lt $REPEAT_TEST_LOOPCOUNT ]; then\n\texit 1\n    fi\n}\n\ncase $1 in\n    tmux)\n\tshift;\n\ttmux_stdin \"$@\";;\n    baseline)\n\tbaseline;;\n    client)\n\tshift\n\tclient \"$@\";;\n    server)\n\tshift\n\tserver \"$@\";;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/server-network-timeout.test",
    "content": "#!/bin/sh\n\n#\n# This test checks for operation of the MOSH_SERVER_NETWORK_TMOUT variable.\n# It does this by\n# * setting the variable\n# * killing the client (and its network traffic)\n# * waiting server-side, for the server to die\n# If it is killed, the test is successful.\n# If it survives that long and the server is still around, the test fails.\n# The client waits a bit longer than the server so that status can be collected\n# properly.\n#\n\nTIMEOUT=10\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" client baseline\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\n\nclient()\n{\n    case \"$myname\" in\n\tserver-network-timeout)\n\t    export MOSH_SERVER_NETWORK_TMOUT=$TIMEOUT;;\n\tserver-signal-timeout)\n\t    export MOSH_SERVER_SIGNAL_TMOUT=$TIMEOUT;;\n\t*)\n\t    fail \"unexpected test name %s\\n\" \"$myname\"\n    esac\n    shift\n    # Print this early, because the server is expected to die before\n    # the normal time this is sent by e2e-test-server.\n    printf \"@@@ server complete @@@\\n\" >&2\n    # Run mosh SUT.\n    eval \"$@\"\n    # The client may be murdered.  We need to expect that...\n    retval=$?\n    case $retval in\n\t0|1)\n\t    fail \"mosh-client had a normal exit\\n\";; # test condition failed\n\t9|137|265)\n\t    # Aha, signal 9.  Wait.\n\t    sleep $(( TIMEOUT + 12 ))\n\t    exit 0\n\t    ;;\n\t*)\n\t    fail \"unknown client wrapper failure, retval=%d\\n\" $retval\n\t    ;;\n    esac\n    fail \"client wrapper shouldnt get here\\n\"\n}\nbaseline()\n{\n    # check for our wonderful variable\n    if [ -z \"$MOSH_SERVER_NETWORK_TMOUT\" ] && [ -z \"$MOSH_SERVER_SIGNAL_TMOUT\" ]; then\n\tenv\n\tfail \"Variable unset\\n\"\n    fi\n    # check for our client\n    if [ -z \"$MOSH_CLIENT_PID\" ]; then\n\tenv\n\tfail \"Client pid unavailable\\n\"\n    fi\n    if ! kill -0 \"$MOSH_CLIENT_PID\"; then\n\tfail \"mosh client is unexpectedly missing\\n\"\n    fi\n\n    # Set up for good return and cleanup on being killed\n    trap \"echo got killed >&2; sleep 1; exit 0\" HUP TERM\n    sleep 1\n    \n    # Kill the client, to stop network traffic.\n    kill -KILL \"$MOSH_CLIENT_PID\"\n    case \"$myname\" in\n\tserver-network-timeout)\n\t    # Just wait.  This is the hardest part.\n\t    sleep $(( TIMEOUT + 7 ))\n\t    ;;\n\tserver-signal-timeout)\n\t    # Wait for the timeout to expire.\n\t    sleep $(( TIMEOUT + 2 ))\n\t    # Tell the server to go away.\n\t    kill -USR1 \"$MOSH_SERVER_PID\"\n\t    sleep 5\n\t    ;;\n\t*)\n\t    fail \"unexpected test name %s\\n\" \"$myname\"\n    esac\n    # If we're still alive and the server is too, the test failed.\n    # XXX the server is getting killed and we're getting here anyway.\n    # Exit with error only if server is still around.\n    sleep 1\n    ! kill -0 \"$MOSH_SERVER_PID\"\n    exit\n}\n\nmyname=\"$(basename \"$0\" .test)\"\n\ncase $1 in\n    baseline|variant)\n\tbaseline;;\n    client)\n\tclient \"$@\";;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/test_utils.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n\n#include \"test_utils.h\"\n\nvoid hexdump( const void* buf, size_t len, const char* name )\n{\n  const unsigned char* data = (const unsigned char*)buf;\n  printf( DUMP_NAME_FMT, name );\n  for ( size_t i = 0; i < len; i++ ) {\n    // Although data[i] is an unsigned char, it will be promoted to a signed int\n    // when passed as an argument. Explicitly cast it back to an unsigned type\n    // so it can be printed in hex.\n    printf( \"%02x\", static_cast<unsigned>( data[i] ) );\n  }\n  printf( \"\\n\" );\n}\n\nvoid hexdump( const Crypto::AlignedBuffer& buf, const char* name )\n{\n  hexdump( buf.data(), buf.len(), name );\n}\n\nvoid hexdump( const std::string& buf, const char* name )\n{\n  hexdump( buf.data(), buf.size(), name );\n}\n"
  },
  {
    "path": "src/tests/test_utils.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TEST_UTILS_HPP\n#define TEST_UTILS_HPP\n\n#include <string>\n\n#include \"src/crypto/crypto.h\"\n\n#define DUMP_NAME_FMT \"%-10s \"\n\nvoid hexdump( const void* buf, size_t len, const char* name );\nvoid hexdump( const Crypto::AlignedBuffer& buf, const char* name );\nvoid hexdump( const std::string& buf, const char* name );\n\n#endif\n"
  },
  {
    "path": "src/tests/unicode-combine-fallback-assert.test",
    "content": "#!/bin/sh\n\n#\n# This test is for Github issue 667\n# https://github.com/mobile-shell/mosh/issues/667\n# where mosh will assert when a combining character is printed on a\n# just-cleared cell.\n#\n# It just sends the offending output to mosh and expects it to not die.\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf '0\\033[1J\\xcc\\xb4'\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/unicode-later-combining.test",
    "content": "#!/bin/sh\n\n#\n# This test is for the first Unicode issue described on the Mosh web\n# page, a combining character drawn on a cell after returning the\n# cursor to that cell.\n#\n# We print 'COMBINING CIRCUMFLEX ACCENT' (U+0302) onto an unused cell.\n# We expect Mosh to output U+0020, U+0302 for that character cell (or\n# possibly U+00A0, U+0302).\n#\n# XXX This test is fragile because it depends on tmux's unicode rendering.\n# The baseline and variant tests produce different (but valid) outputs\n# that are visually identical.  The variant test is not run or validated.\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" baseline post\n    exit\nfi\n\n# OK, we have arguments, we're one of the test hooks.\nif [ $# -ne 1 ]; then\n    fail \"bad arguments %s\\n\" \"$@\"\nfi\n\nbaseline()\n{\n    printf 'abc\\n\\314\\202\\ndef\\n'\n}\n\nvariant()\n{\n    printf 'abc\\n \\314\\202\\ndef\\n'\n}\n\npost()\n{\n    export LANG=C\n    if [ \"$(tmux -V)\" = \"tmux 2.4\" ]; then\n\tskip \"tmux 2.4 unicode combining bug breaks this test\\n\"\n    fi\n    if grep -q \"$(printf '^\\302\\240\\314\\202$')\" \"$(basename \"$0\").d/baseline.capture\"; then\n\texit 0\n    fi\n    exit 1\n}\n\ncase $1 in\n    baseline)\n\tbaseline;;\n    variant)\n\tvariant;;\n    post)\n\tpost;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/tests/window-resize.test",
    "content": "#!/bin/sh\n\n#\n# This is a regression test for window resizing in Mosh.  Making it\n# happen is a little kludgy: we have to create other panes in tmux and\n# resize them.  But it works!\n#\n\n# shellcheck source=e2e-test-subrs\n. \"$(dirname \"$0\")/e2e-test-subrs\"\nPATH=$PATH:.:$srcdir\n# Top-level wrapper.\nif [ $# -eq 0 ]; then\n    e2e-test \"$0\" tmux baseline\n    exit\nfi\n\ntmux_resize_commands()\n{\n    hv=$1\n    shrink=$2\n    grow=$3\n    # Split the window into two panes.\n    printf \"split-window -%s\\n\" \"${hv}\"\n    # Shrink the pane we created\n    for i in $(seq 1 10); do\n\tsleepf\n\tprintf \"resize-pane -%s\\n\" \"${shrink}\"\n    done\n    # And grow it\n    for i in $(seq 1 10); do\n\tsleepf\n\tprintf \"resize-pane -%s\\n\" \"${grow}\"\n    done\n    sleep 1\n    # Remove the pane we created.\n    printf \"kill-pane\\n\"\n}\n\ntmux_commands()\n{\n    # An interactive shell is waiting for us in the mosh session.\n    # Start a full screen application that will redraw on window\n    # resize.\n    printf \"send-keys 'less \\\"%s\\\"' 0x0d\\n\" \"${srcdir}/e2e-test\"\n    sleep 1\n    # we control the horizontal...\n    tmux_resize_commands v D U\n    sleep 1\n    # and the vertical.\n    tmux_resize_commands h L R\n    sleep 1\n    # Exit less.\n    printf \"send-keys 'q'\\n\"\n    sleep 1\n    # Exit mosh session shell.\n    printf \"send-keys 'exit 0' 0x0d\\n\"\n    # need to sleep extra long here, to let child commands complete,\n    # and not have tmux exit prematurely\n    sleep 5\n}\n\ntmux_stdin()\n{\n    tmux_commands | \"$@\"\n    exit\n}\n\nbaseline()\n{\n    # Just start an interactive shell and wait.  The tmux action drives.\n    sh\n}\n\ncase $1 in\n    tmux)\n\tshift;\n\ttmux_stdin \"$@\";;\n    baseline)\n\tbaseline;;\n    *)\n\tfail \"unknown test argument %s\\n\" \"$1\";;\nesac\n"
  },
  {
    "path": "src/util/Makefile.am",
    "content": "AM_CXXFLAGS = -I$(top_srcdir)/ $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) $(HARDEN_CFLAGS) $(MISC_CXXFLAGS) $(CODE_COVERAGE_CXXFLAGS)\n\nnoinst_LIBRARIES = libmoshutil.a\n\nlibmoshutil_a_SOURCES = locale_utils.cc locale_utils.h swrite.cc swrite.h dos_assert.h fatal_assert.h select.h select.cc timestamp.h timestamp.cc pty_compat.cc pty_compat.h\n"
  },
  {
    "path": "src/util/dos_assert.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef DOS_ASSERT_HPP\n#define DOS_ASSERT_HPP\n\n#include <cstdio>\n#include <cstdlib>\n\n#include \"src/crypto/crypto.h\"\n\nstatic void dos_detected( const char* expression, const char* file, int line, const char* function )\n{\n  char buffer[2048];\n  snprintf( buffer,\n            2048,\n            \"Illegal counterparty input (possible denial of service) in function %s at %s:%d, failed test: %s\\n\",\n            function,\n            file,\n            line,\n            expression );\n  throw Crypto::CryptoException( buffer );\n}\n\n#define dos_assert( expr ) ( ( expr ) ? (void)0 : dos_detected( #expr, __FILE__, __LINE__, __func__ ) )\n\n#endif\n"
  },
  {
    "path": "src/util/fatal_assert.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef FATAL_ASSERT_HPP\n#define FATAL_ASSERT_HPP\n\n#include <cstdio>\n#include <cstdlib>\n\nstatic void fatal_error( const char* expression, const char* file, int line, const char* function )\n{\n  fprintf( stderr,\n           \"Fatal assertion failure in function %s at %s:%d\\nFailed test: %s\\n\",\n           function,\n           file,\n           line,\n           expression );\n  abort();\n}\n\n#define fatal_assert( expr ) ( ( expr ) ? (void)0 : fatal_error( #expr, __FILE__, __LINE__, __func__ ) )\n\n#endif\n"
  },
  {
    "path": "src/util/locale_utils.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include <cerrno>\n#include <clocale>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <string>\n\n#if HAVE_LANGINFO_H\n#include <langinfo.h>\n#endif\n\n#include \"src/util/locale_utils.h\"\n\nconst std::string LocaleVar::str( void ) const\n{\n  if ( name.empty() ) {\n    return std::string( \"[no charset variables]\" );\n  }\n  return name + \"=\" + value;\n}\n\nconst LocaleVar get_ctype( void )\n{\n  /* Reimplement the search logic, just for diagnostics */\n  if ( const char* all = getenv( \"LC_ALL\" ) ) {\n    return LocaleVar( \"LC_ALL\", all );\n  } else if ( const char* ctype = getenv( \"LC_CTYPE\" ) ) {\n    return LocaleVar( \"LC_CTYPE\", ctype );\n  } else if ( const char* lang = getenv( \"LANG\" ) ) {\n    return LocaleVar( \"LANG\", lang );\n  }\n  return LocaleVar( \"\", \"\" );\n}\n\nconst char* locale_charset( void )\n{\n  static const char ASCII_name[] = \"US-ASCII\";\n\n  /* Produce more pleasant name of US-ASCII */\n  const char* ret = nl_langinfo( CODESET );\n\n  if ( strcmp( ret, \"ANSI_X3.4-1968\" ) == 0 ) {\n    ret = ASCII_name;\n  }\n\n  return ret;\n}\n\nbool is_utf8_locale( void )\n{\n  /* Verify locale calls for UTF-8 */\n  if ( strcmp( locale_charset(), \"UTF-8\" ) != 0 && strcmp( locale_charset(), \"utf-8\" ) != 0 ) {\n    return false;\n  }\n  return true;\n}\n\nvoid set_native_locale( void )\n{\n  /* Adopt native locale */\n  if ( NULL == setlocale( LC_ALL, \"\" ) ) {\n    int saved_errno = errno;\n    if ( saved_errno == ENOENT ) {\n      LocaleVar ctype( get_ctype() );\n      fprintf( stderr, \"The locale requested by %s isn't available here.\\n\", ctype.str().c_str() );\n      if ( !ctype.name.empty() ) {\n        fprintf( stderr, \"Running `locale-gen %s' may be necessary.\\n\\n\", ctype.value.c_str() );\n      }\n    } else {\n      errno = saved_errno;\n      perror( \"setlocale\" );\n    }\n  }\n}\n\nvoid clear_locale_variables( void )\n{\n  unsetenv( \"LANG\" );\n  unsetenv( \"LANGUAGE\" );\n  unsetenv( \"LC_CTYPE\" );\n  unsetenv( \"LC_NUMERIC\" );\n  unsetenv( \"LC_TIME\" );\n  unsetenv( \"LC_COLLATE\" );\n  unsetenv( \"LC_MONETARY\" );\n  unsetenv( \"LC_MESSAGES\" );\n  unsetenv( \"LC_PAPER\" );\n  unsetenv( \"LC_NAME\" );\n  unsetenv( \"LC_ADDRESS\" );\n  unsetenv( \"LC_TELEPHONE\" );\n  unsetenv( \"LC_MEASUREMENT\" );\n  unsetenv( \"LC_IDENTIFICATION\" );\n  unsetenv( \"LC_ALL\" );\n}\n"
  },
  {
    "path": "src/util/locale_utils.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef LOCALE_UTILS_HPP\n#define LOCALE_UTILS_HPP\n\n#include <string>\n\nclass LocaleVar\n{\npublic:\n  const std::string name, value;\n  LocaleVar( const char* s_name, const char* s_value ) : name( s_name ), value( s_value ) {}\n  const std::string str( void ) const;\n};\n\nconst LocaleVar get_ctype( void );\nconst char* locale_charset( void );\nbool is_utf8_locale( void );\nvoid set_native_locale( void );\nvoid clear_locale_variables( void );\n\n#endif\n"
  },
  {
    "path": "src/util/pty_compat.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#if !defined( HAVE_FORKPTY ) || !defined( HAVE_CFMAKERAW )\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n\n#include <fcntl.h>\n#include <sys/ioctl.h>\n#include <sys/stropts.h>\n#include <termios.h>\n#include <unistd.h>\n\n#include \"src/util/pty_compat.h\"\n\n#ifndef HAVE_FORKPTY\npid_t my_forkpty( int* amaster, char* name, const struct termios* termp, const struct winsize* winp )\n{\n  /* For Solaris and AIX */\n  int master, slave;\n  char* slave_name;\n  pid_t pid;\n\n#ifdef _AIX\n#define PTY_DEVICE \"/dev/ptc\"\n#else\n#define PTY_DEVICE \"/dev/ptmx\"\n#endif\n\n  master = open( PTY_DEVICE, O_RDWR | O_NOCTTY );\n  if ( master < 0 ) {\n    perror( \"open(\" PTY_DEVICE \")\" );\n    return -1;\n  }\n\n  if ( grantpt( master ) < 0 ) {\n    perror( \"grantpt\" );\n    close( master );\n    return -1;\n  }\n\n  if ( unlockpt( master ) < 0 ) {\n    perror( \"unlockpt\" );\n    close( master );\n    return -1;\n  }\n\n  slave_name = ptsname( master );\n  if ( slave_name == NULL ) {\n    perror( \"ptsname\" );\n    close( master );\n    return -1;\n  }\n\n  slave = open( slave_name, O_RDWR | O_NOCTTY );\n  if ( slave < 0 ) {\n    perror( \"open(slave)\" );\n    close( master );\n    return -1;\n  }\n\n#ifndef _AIX\n  if ( ioctl( slave, I_PUSH, \"ptem\" ) < 0 || ioctl( slave, I_PUSH, \"ldterm\" ) < 0 ) {\n    perror( \"ioctl(I_PUSH)\" );\n    close( slave );\n    close( master );\n    return -1;\n  }\n#endif\n\n  if ( amaster != NULL )\n    *amaster = master;\n\n  if ( name != NULL )\n    strcpy( name, slave_name );\n\n  if ( termp != NULL ) {\n    if ( tcsetattr( slave, TCSAFLUSH, termp ) < 0 ) {\n      perror( \"tcsetattr\" );\n      exit( 1 );\n    }\n  }\n\n  // we need to set initial window size, or TIOCGWINSZ fails\n  struct winsize w;\n  w.ws_row = 25;\n  w.ws_col = 80;\n  w.ws_xpixel = 0;\n  w.ws_ypixel = 0;\n  if ( ioctl( slave, TIOCSWINSZ, &w ) < 0 ) {\n    perror( \"ioctl TIOCSWINSZ\" );\n    exit( 1 );\n  }\n  if ( winp != NULL ) {\n    if ( ioctl( slave, TIOCGWINSZ, winp ) < 0 ) {\n      perror( \"ioctl TIOCGWINSZ\" );\n      exit( 1 );\n    }\n  }\n\n  pid = fork();\n  switch ( pid ) {\n    case -1: /* Error */\n      perror( \"fork()\" );\n      return -1;\n    case 0: /* Child */\n      if ( setsid() < 0 )\n        perror( \"setsid\" );\n#ifdef TIOCSCTTY\n      if ( ioctl( slave, TIOCSCTTY, NULL ) < 0 ) {\n        perror( \"ioctl\" );\n        return -1;\n      }\n#else\n      {\n        int dummy_fd;\n        dummy_fd = open( slave_name, O_RDWR );\n        if ( dummy_fd < 0 ) {\n          perror( \"open(slave_name)\" );\n          return -1;\n        }\n        close( dummy_fd );\n      }\n#endif /* TIOCSCTTY */\n      close( master );\n      dup2( slave, STDIN_FILENO );\n      dup2( slave, STDOUT_FILENO );\n      dup2( slave, STDERR_FILENO );\n      return 0;\n    default: /* Parent */\n      close( slave );\n      return pid;\n  }\n}\n#endif\n\n#ifndef HAVE_CFMAKERAW\nvoid my_cfmakeraw( struct termios* termios_p )\n{\n  termios_p->c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON );\n  termios_p->c_oflag &= ~OPOST;\n  termios_p->c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN );\n  termios_p->c_cflag &= ~( CSIZE | PARENB );\n  termios_p->c_cflag |= CS8;\n\n  termios_p->c_cc[VMIN] = 1;  // read() is satisfied after 1 char\n  termios_p->c_cc[VTIME] = 0; // No timer\n}\n#endif\n#endif\n"
  },
  {
    "path": "src/util/pty_compat.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef PTY_COMPAT_HPP\n#define PTY_COMPAT_HPP\n\n#include \"src/include/config.h\"\n\n#ifndef HAVE_FORKPTY\n#define forkpty my_forkpty\n#endif\n#ifndef HAVE_CFMAKERAW\n#define cfmakeraw my_cfmakeraw\n#endif\n\npid_t my_forkpty( int* amaster, char* name, const struct termios* termp, const struct winsize* winp );\n\nvoid my_cfmakeraw( struct termios* termios_p );\n\n#endif\n"
  },
  {
    "path": "src/util/select.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/util/select.h\"\n\nfd_set Select::dummy_fd_set;\n\nsigset_t Select::dummy_sigset;\n\nunsigned int Select::verbose = 0;\n\nvoid Select::handle_signal( int signum )\n{\n  fatal_assert( signum >= 0 );\n  fatal_assert( signum <= MAX_SIGNAL_NUMBER );\n\n  Select& sel = get_instance();\n  sel.got_signal[signum] = 1;\n}\n"
  },
  {
    "path": "src/util/select.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef SELECT_HPP\n#define SELECT_HPP\n\n#include <cassert>\n#include <cerrno>\n#include <csignal>\n#include <cstring>\n\n#include <sys/select.h>\n\n#include \"src/util/fatal_assert.h\"\n#include \"src/util/timestamp.h\"\n\n/* Convenience wrapper for pselect(2).\n\n   Any signals blocked by calling sigprocmask() outside this code will still be\n   received during Select::select().  So don't do that. */\n\nclass Select\n{\npublic:\n  static Select& get_instance( void )\n  {\n    /* COFU may or may not be thread-safe, depending on compiler */\n    static Select instance;\n    return instance;\n  }\n\nprivate:\n  Select()\n    : max_fd( -1 ),\n      /* These initializations are not used; they are just here to appease -Weffc++. */\n      all_fds( dummy_fd_set ), read_fds( dummy_fd_set ), empty_sigset( dummy_sigset ), consecutive_polls( 0 )\n  {\n    FD_ZERO( &all_fds );\n    FD_ZERO( &read_fds );\n\n    clear_got_signal();\n    fatal_assert( 0 == sigemptyset( &empty_sigset ) );\n  }\n\n  void clear_got_signal( void )\n  {\n    for ( volatile sig_atomic_t* p = got_signal; p < got_signal + sizeof( got_signal ) / sizeof( *got_signal );\n          p++ ) {\n      *p = 0;\n    }\n  }\n\n  /* not implemented */\n  Select( const Select& );\n  Select& operator=( const Select& );\n\npublic:\n  void add_fd( int fd )\n  {\n    if ( fd > max_fd ) {\n      max_fd = fd;\n    }\n    FD_SET( fd, &all_fds );\n  }\n\n  void clear_fds( void ) { FD_ZERO( &all_fds ); }\n\n  static void add_signal( int signum )\n  {\n    fatal_assert( signum >= 0 );\n    fatal_assert( signum <= MAX_SIGNAL_NUMBER );\n\n    /* Block the signal so we don't get it outside of pselect(). */\n    sigset_t to_block;\n    fatal_assert( 0 == sigemptyset( &to_block ) );\n    fatal_assert( 0 == sigaddset( &to_block, signum ) );\n    fatal_assert( 0 == sigprocmask( SIG_BLOCK, &to_block, NULL ) );\n\n    /* Register a handler, which will only be called when pselect()\n       is interrupted by a (possibly queued) signal. */\n    struct sigaction sa;\n    sa.sa_flags = SA_RESTART;\n    sa.sa_handler = &handle_signal;\n    fatal_assert( 0 == sigfillset( &sa.sa_mask ) );\n    fatal_assert( 0 == sigaction( signum, &sa, NULL ) );\n  }\n\n  /* timeout unit: milliseconds; negative timeout means wait forever */\n  int select( int timeout )\n  {\n    memcpy( &read_fds, &all_fds, sizeof( read_fds ) );\n    clear_got_signal();\n\n    /* Rate-limit and warn about polls. */\n    if ( verbose > 1 && timeout == 0 ) {\n      fprintf( stderr, \"%s: got poll (timeout 0)\\n\", __func__ );\n    }\n    if ( timeout == 0 && ++consecutive_polls >= MAX_POLLS ) {\n      if ( verbose > 1 && consecutive_polls == MAX_POLLS ) {\n        fprintf( stderr, \"%s: got %d polls, rate limiting.\\n\", __func__, MAX_POLLS );\n      }\n      timeout = 1;\n    } else if ( timeout != 0 && consecutive_polls ) {\n      if ( verbose > 1 && consecutive_polls >= MAX_POLLS ) {\n        fprintf( stderr, \"%s: got %d consecutive polls\\n\", __func__, consecutive_polls );\n      }\n      consecutive_polls = 0;\n    }\n\n#ifdef HAVE_PSELECT\n    struct timespec ts;\n    struct timespec* tsp = NULL;\n\n    if ( timeout >= 0 ) {\n      ts.tv_sec = timeout / 1000;\n      ts.tv_nsec = 1000000 * ( long( timeout ) % 1000 );\n      tsp = &ts;\n    }\n\n    int ret = ::pselect( max_fd + 1, &read_fds, NULL, NULL, tsp, &empty_sigset );\n#else\n    struct timeval tv;\n    struct timeval* tvp = NULL;\n    sigset_t old_sigset;\n\n    if ( timeout >= 0 ) {\n      tv.tv_sec = timeout / 1000;\n      tv.tv_usec = 1000 * ( long( timeout ) % 1000 );\n      tvp = &tv;\n    }\n\n    int ret = sigprocmask( SIG_SETMASK, &empty_sigset, &old_sigset );\n    if ( ret != -1 ) {\n      ret = ::select( max_fd + 1, &read_fds, NULL, NULL, tvp );\n      sigprocmask( SIG_SETMASK, &old_sigset, NULL );\n    }\n#endif\n\n    if ( ret == 0 || ( ret == -1 && errno == EINTR ) ) {\n      /* Look for and report Cygwin select() bug. */\n      if ( ret == 0 ) {\n        for ( int fd = 0; fd <= max_fd; fd++ ) {\n          if ( FD_ISSET( fd, &read_fds ) ) {\n            fprintf( stderr, \"select(): nfds = 0 but read fd %d is set\\n\", fd );\n          }\n        }\n      }\n      /* The user should process events as usual. */\n      FD_ZERO( &read_fds );\n      ret = 0;\n    }\n\n    freeze_timestamp();\n\n    return ret;\n  }\n\n  bool read( int fd )\n#if FD_ISSET_IS_CONST\n    const\n#endif\n  {\n    assert( FD_ISSET( fd, &all_fds ) );\n    return FD_ISSET( fd, &read_fds );\n  }\n\n  /* This method consumes a signal notification. */\n  bool signal( int signum )\n  {\n    fatal_assert( signum >= 0 );\n    fatal_assert( signum <= MAX_SIGNAL_NUMBER );\n    /* XXX This requires a guard against concurrent signals. */\n    bool rv = got_signal[signum];\n    got_signal[signum] = 0;\n    return rv;\n  }\n\n  /* This method does not consume signal notifications. */\n  bool any_signal( void ) const\n  {\n    bool rv = false;\n    for ( int i = 0; i < MAX_SIGNAL_NUMBER; i++ ) {\n      rv |= got_signal[i];\n    }\n    return rv;\n  }\n\n  static void set_verbose( unsigned int s_verbose )\n  {\n    verbose = s_verbose;\n  }\n\nprivate:\n  static const int MAX_SIGNAL_NUMBER = 64;\n  /* Number of 0-timeout selects after which we begin to think\n   * something's wrong. */\n  static const int MAX_POLLS = 10;\n\n  static void handle_signal( int signum );\n\n  int max_fd;\n\n  /* We assume writes to got_signal are atomic, though we also try to mask out\n     concurrent signal handlers. */\n  volatile sig_atomic_t got_signal[MAX_SIGNAL_NUMBER + 1];\n\n  fd_set all_fds, read_fds;\n\n  sigset_t empty_sigset;\n\n  static fd_set dummy_fd_set;\n  static sigset_t dummy_sigset;\n  int consecutive_polls;\n  static unsigned int verbose;\n};\n\n#endif\n"
  },
  {
    "path": "src/util/swrite.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include <cstdio>\n#include <cstring>\n\n#include <unistd.h>\n\n#include \"src/util/swrite.h\"\n\nint swrite( int fd, const char* str, ssize_t len )\n{\n  ssize_t total_bytes_written = 0;\n  ssize_t bytes_to_write = ( len >= 0 ) ? len : (ssize_t)strlen( str );\n  while ( total_bytes_written < bytes_to_write ) {\n    ssize_t bytes_written = write( fd, str + total_bytes_written, bytes_to_write - total_bytes_written );\n    if ( bytes_written <= 0 ) {\n      perror( \"write\" );\n      return -1;\n    }\n    total_bytes_written += bytes_written;\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "src/util/swrite.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef SWRITE_HPP\n#define SWRITE_HPP\n\nint swrite( int fd, const char* str, ssize_t len = -1 );\n\n#endif\n"
  },
  {
    "path": "src/util/timestamp.cc",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#include \"src/include/config.h\"\n\n#include \"src/util/timestamp.h\"\n\n#include <cerrno>\n\n#if HAVE_CLOCK_GETTIME\n#include <ctime>\n#endif\n#if HAVE_MACH_ABSOLUTE_TIME\n#include <mach/error.h>\n#include <mach/mach_time.h>\n#endif\n#if HAVE_GETTIMEOFDAY\n#include <cstdio>\n#include <sys/time.h>\n#endif\n\n// On Apple systems CLOCK_MONOTONIC is unfortunately able to go\n// backwards in time. This breaks mosh when system is returning from\n// suspend as described in ticket #1014. To avoid this issue prefer\n// CLOCK_MONOTONIC_RAW on Apple systems when available.\n#if defined( __APPLE__ ) && defined( CLOCK_MONOTONIC_RAW )\n#define CLOCKTYPE CLOCK_MONOTONIC_RAW\n#else\n#define CLOCKTYPE CLOCK_MONOTONIC\n#endif\n\nstatic uint64_t millis_cache = -1;\n\nuint64_t frozen_timestamp( void )\n{\n  if ( millis_cache == uint64_t( -1 ) ) {\n    freeze_timestamp();\n  }\n\n  return millis_cache;\n}\n\nvoid freeze_timestamp( void )\n{\n  // Try all our clock sources till we get something.  This could\n  // break if a source only sometimes works in a given process.\n#if HAVE_CLOCK_GETTIME\n  // Preferred clock source-- portable, monotonic, (should be)\n  // adjusted after system sleep\n  struct timespec tp;\n\n  if (\n#if defined( __APPLE__ ) && defined( __MACH__ )\n    // Check for presence, for OS X SDK >= 10.12 and runtime < 10.12\n    &clock_gettime != NULL &&\n#endif\n    clock_gettime( CLOCKTYPE, &tp ) == 0 ) {\n    uint64_t millis = tp.tv_nsec / 1000000;\n    millis += uint64_t( tp.tv_sec ) * 1000;\n\n    millis_cache = millis;\n    return;\n  }\n#endif\n#if HAVE_MACH_ABSOLUTE_TIME\n  // Monotonic, not adjusted after system sleep.  OS X 10.12 has\n  // mach_continuous_time(), but also has clock_gettime().\n  static mach_timebase_info_data_t s_timebase_info;\n  static double absolute_to_millis = 0.0;\n\n  if ( absolute_to_millis == 0.0 ) {\n    if ( ERR_SUCCESS == mach_timebase_info( &s_timebase_info ) ) {\n      absolute_to_millis = 1e-6 * s_timebase_info.numer / s_timebase_info.denom;\n    } else\n      absolute_to_millis = -1.0;\n  }\n\n  // NB: mach_absolute_time() returns \"absolute time units\"\n  // We need to apply a conversion to get milliseconds.\n  if ( absolute_to_millis > 0.0 ) {\n    millis_cache = mach_absolute_time() * absolute_to_millis;\n    return;\n  }\n#endif\n#if HAVE_GETTIMEOFDAY\n  // Not monotonic.\n  // NOTE: If time steps backwards, timeouts may be confused.\n  struct timeval tv;\n  if ( gettimeofday( &tv, NULL ) ) {\n    perror( \"gettimeofday\" );\n  } else {\n    uint64_t millis = tv.tv_usec / 1000;\n    millis += uint64_t( tv.tv_sec ) * 1000;\n\n    millis_cache = millis;\n    return;\n  }\n#else\n#error \"gettimeofday() unavailable-- required as timer of last resort\"\n#endif\n}\n"
  },
  {
    "path": "src/util/timestamp.h",
    "content": "/*\n    Mosh: the mobile shell\n    Copyright 2012 Keith Winstein\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n    In addition, as a special exception, the copyright holders give\n    permission to link the code of portions of this program with the\n    OpenSSL library under certain conditions as described in each\n    individual source file, and distribute linked combinations including\n    the two.\n\n    You must obey the GNU General Public License in all respects for all\n    of the code used other than OpenSSL. If you modify file(s) with this\n    exception, you may extend this exception to your version of the\n    file(s), but you are not obligated to do so. If you do not wish to do\n    so, delete this exception statement from your version. If you delete\n    this exception statement from all source files in the program, then\n    also delete it here.\n*/\n\n#ifndef TIMESTAMP_HPP\n#define TIMESTAMP_HPP\n\n#include <cstdint>\n\nvoid freeze_timestamp( void );\nuint64_t frozen_timestamp( void );\n\n#endif\n"
  }
]