<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>funcptr - development</title>
    <subtitle>An engineer&#x27;s technical notebook</subtitle>
    <link rel="self" type="application/atom+xml" href="https://funcptr.net/category/development/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://funcptr.net/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2018-05-21T14:00:00+00:00</updated>
    <id>https://funcptr.net/category/development/atom.xml</id>
    <entry xml:lang="en">
        <title>Python Packaging and Distribution</title>
        <published>2018-05-21T14:00:00+00:00</published>
        <updated>2018-05-21T14:00:00+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2018/python-packaging/"/>
        <id>https://funcptr.net/2018/python-packaging/</id>
        
        <content type="html" xml:base="https://funcptr.net/2018/python-packaging/">&lt;p&gt;There&#x27;s been many a discussion online related to a variety of tools related to
Python packaging and distribution. There is &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pip.pypa.io&quot;&gt;pip&lt;&#x2F;a&gt;, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.pipenv.org&quot;&gt;pipenv&lt;&#x2F;a&gt; and
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;poetry.eustace.io&quot;&gt;poetry&lt;&#x2F;a&gt; that have been the tools under discussion.&lt;&#x2F;p&gt;
&lt;p&gt;As an open source maintainer as part of the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pylonsproject.org&quot;&gt;Pylons Project&lt;&#x2F;a&gt;, while I would
love to be writing code I end up spending a lot of time dealing with user
questions around packaging&#x2F;distributing their source code using the software
I&#x27;ve helped build, and as we move forward myself and other maintainers were
wondering if we were actually helping users move forward in the best way
possible using best of breed tools.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-201805tooldiscussionpyramid-1&quot;&gt;&lt;a href=&quot;#fn-201805tooldiscussionpyramid&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As the Python community has moved from &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20180527214739&#x2F;http:&#x2F;&#x2F;setuptools.readthedocs.io&#x2F;en&#x2F;latest&#x2F;easy_install.html#easy-install&quot;&gt;easy_install&lt;&#x2F;a&gt; to pip, we too have
kept the documentation up to date. We went from &lt;code&gt;python setup.py develop&lt;&#x2F;code&gt; to
&lt;code&gt;pip install -e .&lt;&#x2F;code&gt; to create editable installs of local projects, and try to
let people know the pitfalls of using both easy_install and pip in the same
project (mostly with an answer that falls in line with: remove your virtual
environment and start over, just use pip).&lt;&#x2F;p&gt;
&lt;p&gt;As part of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;trypyramid.com&quot;&gt;Pyramid&lt;&#x2F;a&gt; we have developed and maintain various different
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;cookiecutter.readthedocs.io&#x2F;en&#x2F;latest&#x2F;&quot;&gt;cookiecutter&lt;&#x2F;a&gt; templates, and our goal is to attempt to provide templates
that are both useful, but also follow best practices that are being adopted
within the community at large so that newcomers can use their existing
skills&#x2F;knowledge and those that are starting with us walk away with a knowledge
and experience that applies not just to development of Pyramid applications,
but also applies to the broader community as a whole.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pip&quot;&gt;pip&lt;&#x2F;h2&gt;
&lt;p&gt;Pip is a great tool that has simplified installation of packages, it supports
using binary distributions named &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pypa&#x2F;wheel&quot;&gt;wheels&lt;&#x2F;a&gt; and has a way to easily install
software from the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&quot;&gt;Python Packaging Index&lt;&#x2F;a&gt;. It has a rather naive
dependency resolution process, but for the most part it works and works well.
It replaced easy_install as the tool to use for installing packages.&lt;&#x2F;p&gt;
&lt;p&gt;While you can use a &lt;code&gt;requirements.txt&lt;&#x2F;code&gt; file with pip to install a &quot;blessed&quot;
list of software there is no good way to &quot;lock&quot; the dependencies of
dependencies without manually adding it to the list of requirements. This ends
up making it very difficult to manage, and it is very difficult to know that
what has been tested is what the user is actually going to get because packages
may be updated at anytime, and re-creating the same exact environment is
difficult and fraught with errors.&lt;&#x2F;p&gt;
&lt;p&gt;This is where &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pypa&#x2F;pipfile&quot;&gt;Pipfile&lt;&#x2F;a&gt; is supposed to help. This is a project to add a
new, more descriptive requirements file, as well as allowing for a lockfile
that would lock not just your primary packages you have listed, but also all
dependencies of dependencies all the way down the tree. This helps with
reproducibility and allows for the same installation on two different systems
to have the exact same software&#x2F;dependencies installed.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pipenv&quot;&gt;pipenv&lt;&#x2F;h2&gt;
&lt;p&gt;While pip is a great tool, and with the Pipfile changes it would allow for
locking of dependencies, there is one more puzzle piece missing. When
installing packages while you can install them into the global namespace, the
recommended way is to install all packages for a particular tool&#x2F;project into a
virtual environment.&lt;&#x2F;p&gt;
&lt;p&gt;Normally you&#x27;d invoke &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;virtualenv.pypa.io&#x2F;en&#x2F;stable&#x2F;&quot;&gt;virtualenv&lt;&#x2F;a&gt;, to create this environment and then
you&#x27;d make sure to install all packages within it, thereby isolating it from
the rest of the system.&lt;&#x2F;p&gt;
&lt;p&gt;pipenv automates this for you, as well as using &lt;code&gt;Pipfile&lt;&#x2F;code&gt; it also supports
locking using &lt;code&gt;Pipfile.lock&lt;&#x2F;code&gt; and provides a bunch of tooling around
adding&#x2F;removing dependencies from a local project.&lt;&#x2F;p&gt;
&lt;p&gt;pipenv allows you to easily create an environment and manage dependencies, but
it makes no effort to solve the problem of distributing and building a package
that may be installed by third parties.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;poetry&quot;&gt;poetry&lt;&#x2F;h2&gt;
&lt;p&gt;Poetry is a similar project to pipenv, with a major difference being that it
was built to help with distributing&#x2F;developing applications and building a
distributable package that may then be installed using pip.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of using a &lt;code&gt;Pipfile&lt;&#x2F;code&gt; it uses a recently standardised &lt;code&gt;pyproject.toml&lt;&#x2F;code&gt;
file instead. Like pipenv it also supports locking, and it provides tooling
around adding&#x2F;removing dependencies as well as managing what versions are
required.&lt;&#x2F;p&gt;
&lt;p&gt;Ultimately those dependencies are going to end up as metadata in a
distributable package.&lt;&#x2F;p&gt;
&lt;p&gt;Poetry makes it easier to manage a software development project, whether that
is for an application using various libraries for internal use, or for
libraries that are going to be distributed to other developers.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-divide&quot;&gt;The divide&lt;&#x2F;h2&gt;
&lt;p&gt;This is where the divide really starts, while you can use pipenv with a
standard &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;setuptools.readthedocs.io&#x2F;en&#x2F;latest&#x2F;&quot;&gt;setuptools&lt;&#x2F;a&gt; project, any dependencies you add to the Pipfile
using pipenv&#x27;s tooling will not be listed as a dependencies for your project
when you distribute it, this either means you need to duplicate the list in
both &lt;code&gt;setup.py&lt;&#x2F;code&gt; as well as the &lt;code&gt;Pipfile&lt;&#x2F;code&gt;, or you have to add your current
project as an editable install within your &lt;code&gt;Pipfile&lt;&#x2F;code&gt; which means your &lt;code&gt;Pipfile&lt;&#x2F;code&gt;
is now not as easily distributable.&lt;&#x2F;p&gt;
&lt;p&gt;There are work-arounds that people have used, such as having &lt;code&gt;setup.py&lt;&#x2F;code&gt; read a
&lt;code&gt;requirements.txt&lt;&#x2F;code&gt;, so that you could have all your requirements listed in a
text file, and not in &lt;code&gt;setup.py&lt;&#x2F;code&gt;, but asking to do the same with a &lt;code&gt;Pipfile&lt;&#x2F;code&gt; in
pipenv was met with a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20201020045235&#x2F;https:&#x2F;&#x2F;github.com&#x2F;pypa&#x2F;pipenv&#x2F;issues&#x2F;209#issuecomment-332553113&quot;&gt;&quot;Do not do this.&quot;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;poetry explicitly allows you to add dependencies in one place, and those
dependency listings are then automatically inserted into the package metadata
that is created when you build your distributable package.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-two-use-cases&quot;&gt;The two use cases&lt;&#x2F;h2&gt;
&lt;p&gt;There are two competing use cases, one is the deployment of software packages
and being able to run them, but not as a developer, the other is a developer of
software packages that needs to define dependencies for the project to run.&lt;&#x2F;p&gt;
&lt;p&gt;pipenv solves the deployment case. If I was a user I could very simply grab a
known good &lt;code&gt;Pipfile.lock&lt;&#x2F;code&gt; and use pipenv to install a known good set of
software, this is great when I am deploying a project. It is the use case that
many in the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.pypa.io&#x2F;en&#x2F;latest&#x2F;&quot;&gt;Python Packaging Authority&lt;&#x2F;a&gt; also seem to be optimizing for.&lt;&#x2F;p&gt;
&lt;p&gt;The other use case is for developers that are building new software, either by
using a list of existing packages and deploying privately, or people developing
software for other developers to be published on the Python Packaging Index.&lt;&#x2F;p&gt;
&lt;p&gt;This latter group of people is under represented due to it likely being much
smaller, and existing tools like &lt;code&gt;setuptools&lt;&#x2F;code&gt; and &lt;code&gt;setup.py&lt;&#x2F;code&gt; already providing
a &quot;good enough&quot; experience. Innovation in this area is something that readily
needs to be improved upon to make it easier to create new libraries&#x2F;packages
that follow best practices. The amount of copy and pastes people have done for
adding a &lt;code&gt;setup.py&lt;&#x2F;code&gt; to their projects or to make something work is long. It&#x27;s
all a little bit of black magic, and there is a great many things that have
been carried over because of cargo cult programming.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;explicit-mentions-by-the-python-packaging-authority&quot;&gt;Explicit mentions by the Python Packaging Authority&lt;&#x2F;h2&gt;
&lt;p&gt;Reading the packaging guide on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;packaging.python.org&#x2F;tutorials&#x2F;managing-dependencies&#x2F;&quot;&gt;managing dependencies&lt;&#x2F;a&gt;, pipenv is the
recommended tool:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;This tutorial walks you through the use of Pipenv to manage dependencies for
an application. It will show you how to install and use the necessary tools
and make strong recommendations on best practices.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;this language, along with what &lt;code&gt;packaging.python.org&lt;&#x2F;code&gt; implies as a URL makes it
difficult as a project maintainer to recommend alternate tools, becuase even if
those tools are superior for the use case we are recommending them for it is
always going to lead to questions from users, such as:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Why are you not using pipenv, the official tool recommended by Python.org?&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;We get similar questions about &lt;code&gt;easy_install&lt;&#x2F;code&gt; vs &lt;code&gt;pip&lt;&#x2F;code&gt; all of the time, as
well as why people should switch, and we can point to various bits of
documentation that explains why pip is a better choice.&lt;&#x2F;p&gt;
&lt;p&gt;If we were to recommend an alternate the appeal to authority that &lt;code&gt;python.org&lt;&#x2F;code&gt;
implies is going to make it much more difficult, and the question will become
&quot;why is the Pylons Project not using recommended tooling?&quot;&lt;&#x2F;p&gt;
&lt;p&gt;poetry is listed as a footnote on that page, alongside &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jazzband&#x2F;pip-tools&quot;&gt;pip-tools&lt;&#x2F;a&gt; and
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ofek&#x2F;hatch&quot;&gt;hatch&lt;&#x2F;a&gt;, and is mentioned only for doing library development, with no
mention of other requirements that may make it a much better tool for
developing locally.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;deployment-is-not-development&quot;&gt;Deployment is not development&lt;&#x2F;h2&gt;
&lt;p&gt;If I am using pipenv with a non-installable project (no setup.py) I end up
having to figure out how to get the code, and the &lt;code&gt;Pipfile&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;Pipfile.lock&lt;&#x2F;code&gt; to
my environment I am deploying into. pipenv&#x27;s install provides a way to make
sure to only install if the &lt;code&gt;Pipfile.lock&lt;&#x2F;code&gt; is up to date or otherwise will fail
to continue. If you are using a local project though, and it uses &lt;code&gt;setup.py&lt;&#x2F;code&gt;
the only way that the &lt;code&gt;Pipfile.lock&lt;&#x2F;code&gt; will contain any sub-dependencies of your
&lt;code&gt;setup.py&lt;&#x2F;code&gt; project is if you install it as &lt;code&gt;editable&lt;&#x2F;code&gt;. Otherwise
sub-dependencies are not locked.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-201805subdepends-1&quot;&gt;&lt;a href=&quot;#fn-201805subdepends&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If I am using poetry I get an pip installable project, but it doesn&#x27;t contain
any hard pins or lock files. I&#x27;d have to distribute &lt;code&gt;pyproject.lock&lt;&#x2F;code&gt; as well as
my wheel. This gets me a little closer, but still no lock file that includes my
newly produced wheel, and has all of its dependencies locked.&lt;&#x2F;p&gt;
&lt;p&gt;The Python Packaging Authority based on Twitter conversations with its members
and the documentation on &lt;code&gt;packaging.python.org&lt;&#x2F;code&gt; suggest using pipenv for
development. pipenv is particular ill-suited for development if the goal is to
create a package to be deployed to production. With two locations to define
dependencies it leaves people scratching their heads as to which is canonical,
and if a dependency is added to &lt;code&gt;Pipfile&lt;&#x2F;code&gt; but not &lt;code&gt;setup.py&lt;&#x2F;code&gt; it may leave a
developer thinking their package is ready for distribution when in reality it
is missing a dependency that is required to run&#x2F;use said distribution.&lt;&#x2F;p&gt;
&lt;p&gt;At this point using both projects seems like a win-win. Use poetry to
build&#x2F;develop a package, then use pipenv in the integration phase to create a
&lt;code&gt;Pipfile.lock&lt;&#x2F;code&gt; that is used to deploy in production. This way you get the best
of both worlds. A great tool that can help you register entry points and
another that can help you with deploying a known good set of dependencies.&lt;&#x2F;p&gt;
&lt;p&gt;Interestingly, even the pipenv docs seem to agree that it is a deployment tool:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Specify your target Python version in your Pipfile’s [requires] section.
Ideally, you should only have one target Python version, as this is a
deployment tool.&lt;&#x2F;p&gt;
&lt;p&gt;-- &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.pipenv.org&#x2F;basics&#x2F;#general-recommendations-version-control&quot;&gt;Pipenv - General Recommendations &amp;amp; Version Control&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Use pipenv if you have a script that requires a couple of dependencies and
doesn&#x27;t need all of the extra overhead of packaging metadata&#x2F;packaging. Use
poetry if you want to build a distributable project that can easily be deployed
by others, and use both if you develop a project and need a known good
environment to deploy.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;in-summary&quot;&gt;In summary&lt;&#x2F;h2&gt;
&lt;p&gt;There will likely never be a time that one single tool is considered good
enough, and competition between tools is a way to keep advancing forward.
Packaging in the Python community for a long time has been difficult. Wheels
has made things a little better. pip has made management of installing new
packages easier and improved upon easy_install. Here&#x27;s to the next evolution.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Now, can we talk about standardising on &lt;code&gt;pyproject.toml&lt;&#x2F;code&gt; since that is already
where &quot;project&quot; metadata needs to go, might as well re-use the name instead of
having two different names&#x2F;files. Oh, and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0517&#x2F;&quot;&gt;PEP 517&lt;&#x2F;a&gt; can&#x27;t come soon enough
so that alternate tools like &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flit.readthedocs.io&#x2F;en&#x2F;latest&#x2F;&quot;&gt;flit&lt;&#x2F;a&gt; can be used instead of
setuptools&#x2F;&lt;code&gt;setup.py&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-201805tooldiscussionpyramid&quot;&gt;
&lt;p&gt;We created an issue named &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Pylons&#x2F;pyramid&#x2F;issues&#x2F;3270&quot;&gt;Support poetry,
flit, pipenv, or ...?&lt;&#x2F;a&gt; that attempts to go over the pros and cons of the
various tools and how we currently support our users in our documentation on
building projects using pyramid, including how to create a project that is
distributable.  Pyramid heavily uses &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;setuptools.readthedocs.io&#x2F;en&#x2F;latest&#x2F;pkg_resources.html&quot;&gt;&lt;code&gt;pkg_resources&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and entry
points.  The way to register the entry points is to have an installable
package. The framework is flexible enough that there is no requirement for
entry points, but at that point you are in territory where the default
tooling provided by the project will not work, and some of the convenience
tools&#x2F;functionality that Pyramid provides it&#x27;s users&#x2F;developers is not
available. &lt;a href=&quot;#fr-201805tooldiscussionpyramid-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-201805subdepends&quot;&gt;
&lt;p&gt;See documentation for &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.pipenv.org&#x2F;basics&#x2F;#editable-dependencies-e-g-e&quot;&gt;Editable Dependencies (e.g.  &lt;code&gt;-e   .&lt;&#x2F;code&gt;)&lt;&#x2F;a&gt; which as of this writing states: &lt;blockquote&gt;Sub-dependencies are &lt;strong&gt;not&lt;&#x2F;strong&gt; added to the Pipfile.lock if you leave the -e option out.&lt;&#x2F;blockquote&gt; &lt;a href=&quot;#fr-201805subdepends-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Build numbers in binaries using waf</title>
        <published>2014-06-10T17:32:03+00:00</published>
        <updated>2014-06-10T17:32:03+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2014/build-numbers-in-binaries-using-waf/"/>
        <id>https://funcptr.net/2014/build-numbers-in-binaries-using-waf/</id>
        
        <content type="html" xml:base="https://funcptr.net/2014/build-numbers-in-binaries-using-waf/">&lt;p&gt;My build system of choice these days for any C++ project is &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;waf.io&quot;&gt;waf&lt;&#x2F;a&gt;. One of
the things I always like havig is the build number included in the final
binary, so that with a simple &lt;code&gt;.&#x2F;binary --version&lt;&#x2F;code&gt; or even &lt;code&gt;.&#x2F;binary&lt;&#x2F;code&gt; the
version is printed that it was built from. This can make it much simpler to
debug any potential issues, especially if fixes may have already been made but
a bad binary was deployed.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setup-the-wscript&quot;&gt;Setup the wscript&lt;&#x2F;h2&gt;
&lt;p&gt;Make sure that your wscript somewhere near the top contains the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;APPNAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;myapp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;VERSION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;0.0.0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then in your &lt;code&gt;configure(cfg)&lt;&#x2F;code&gt; add the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cfg.env.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;VERSION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; VERSION&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cfg.env.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;APPNAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; APPNAME&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;git_version&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt; try_git_version&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; git_version:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cfg.env.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;VERSION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; git_version&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;try_git_version()&lt;&#x2F;code&gt; function is fairly simple and looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; try_git_version&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    import&lt;&#x2F;span&gt;&lt;span&gt; os&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    import&lt;&#x2F;span&gt;&lt;span&gt; sys&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    version&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    try&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        version&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; os.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;popen&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;git describe --always --dirty --long&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    except&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; e:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt; e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; version&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It runs &lt;code&gt;git describe --always --dirty --long&lt;&#x2F;code&gt; which will return something
along these lines: &lt;code&gt;401b85f-dirty&lt;&#x2F;code&gt;. If you have any annoted tags, it will
return the tag name as well.&lt;&#x2F;p&gt;
&lt;p&gt;If &lt;code&gt;git&lt;&#x2F;code&gt; is not installed, or it is not a valid &lt;code&gt;git&lt;&#x2F;code&gt; directory, then it will
simply return &lt;code&gt;None&lt;&#x2F;code&gt;. At that point all we have to go on is the &lt;code&gt;VERSION&lt;&#x2F;code&gt;
variable set at the top of the wscript.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have our configuration environment set up with the &lt;code&gt;VERSION&lt;&#x2F;code&gt; we
want to get that into a file that we can then include in our C++ source code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;create-a-build-version-h-in-file&quot;&gt;Create a &lt;code&gt;build_version.h.in&lt;&#x2F;code&gt; file&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;ifndef&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;font-style: italic;&quot;&gt; BUILD_VERSION_H_IN_941AD1F24D0A9D&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;font-style: italic;&quot;&gt; BUILD_VERSION_H_IN_941AD1F24D0A9D&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; VERSION[] &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;@VERSION@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;endif&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; &#x2F;* BUILD_VERSION_H_IN_941AD1F24D0A9D *&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;add-the-following-to-build-ctx&quot;&gt;Add the following to &lt;code&gt;build(ctx)&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;ctx&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;features&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;subst&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        source&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;build_version.h.in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        target&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;build_version.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        VERSION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; ctx.env&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;VERSION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This uses the substitution feature to transform &lt;code&gt;build_version.h.in&lt;&#x2F;code&gt; into
&lt;code&gt;build_version.h&lt;&#x2F;code&gt;, while inserting the version into the file.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;include-build-version-h-in-your-source-code&quot;&gt;Include &lt;code&gt;build_version.h&lt;&#x2F;code&gt; in your source code&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;build_version.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And add something along these lines to your &lt;code&gt;main()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;std::cerr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;Version: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; std::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will print out the &lt;code&gt;VERSION&lt;&#x2F;code&gt; that has been stored in &lt;code&gt;build_version.h&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;full-example&quot;&gt;Full example&lt;&#x2F;h2&gt;
&lt;p&gt;Check out my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;digitalresistor&#x2F;mdns-announce&quot;&gt;mdns-announce&lt;&#x2F;a&gt; project on Github for an example of how this is
implemented.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>User sessions, what data should be stored where?</title>
        <published>2013-08-25T13:05:00+00:00</published>
        <updated>2013-08-25T13:05:00+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2013/user-sessions-and-the-data-to-be-stored/"/>
        <id>https://funcptr.net/2013/user-sessions-and-the-data-to-be-stored/</id>
        
        <content type="html" xml:base="https://funcptr.net/2013/user-sessions-and-the-data-to-be-stored/">&lt;p&gt;&lt;em&gt;This article has been updated. For the old version please check
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20130826031727&#x2F;http:&#x2F;&#x2F;funcptr.net&#x2F;2013&#x2F;08&#x2F;25&#x2F;user-sessions,-what-data-should-be-stored-where-&#x2F;&quot;&gt;Archive.org&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A couple of days ago on &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;reddit.com&#x2F;&quot;&gt;reddit.com&lt;&#x2F;a&gt;&#x27;s &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;reddit.com&#x2F;r&#x2F;netsec&#x2F;&quot;&gt;&#x2F;r&#x2F;netsec&lt;&#x2F;a&gt; a poster by the name
of &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;danweber.blogspot.com&#x2F;&quot;&gt;Dan Weber&lt;&#x2F;a&gt; posted what he believed to be an attack on PHP sessions:
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;danweber.blogspot.com&#x2F;2013&#x2F;08&#x2F;hacking-php-sessions-by-running-out-of.html&quot;&gt;Hacking PHP sessions by running out of memory&lt;&#x2F;a&gt; &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;netsec&#x2F;comments&#x2F;1k7khy&#x2F;i_know_its_not_that_hard_but_i_think_i_found_a&#x2F;&quot;&gt;(reddit link)&lt;&#x2F;a&gt;. The way
the &quot;attack&quot; works is as follows:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new session&lt;&#x2F;li&gt;
&lt;li&gt;Assign some new data to the session, in this case a username which is used
to signify that the user is logged in.&lt;&#x2F;li&gt;
&lt;li&gt;Check to verify that the user should be logged in&lt;&#x2F;li&gt;
&lt;li&gt;If the user should not be logged in, destroy the session.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The &quot;attack&quot; would be to run the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;php.net&#x2F;&quot;&gt;PHP&lt;&#x2F;a&gt; script out of memory on number 3, since
once something is set on the session it is immediately stored, so even if the
user is not supposed to be logged in, they are now logged in since their
session says they are.&lt;&#x2F;p&gt;
&lt;p&gt;I wouldn&#x27;t necessarily call this a PHP hack, this is really just bad practice
in terms of programming, the logic should be reversed.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new session&lt;&#x2F;li&gt;
&lt;li&gt;Verify the user should be logged in&lt;&#x2F;li&gt;
&lt;li&gt;If so, set the session data&lt;&#x2F;li&gt;
&lt;li&gt;If the user should not be logged in, don&#x27;t set the session data&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;That would solve the problem at hand, and now there is no way for the user to
trick the PHP script into believing she is logged in when that is not the case.&lt;&#x2F;p&gt;
&lt;p&gt;However as the discussion went on on Reddit, it became even more clear that
there are no good resources on what you should store in the user session, and
what you shouldn&#x27;t store in the user session. Some of these things may seem
like common knowledge, but sadly this is something every single new person to
programming has to learn on their own.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;some-assumptions&quot;&gt;Some assumptions&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s get this out of the way, this is in no way limited to PHP, but it is the
one I will be using as an example. This can all apply to Ruby (&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;rubyonrails.org&quot;&gt;Ruby on
Rails&lt;&#x2F;a&gt;), Python (&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.pylonsproject.org&quot;&gt;Pyramid&lt;&#x2F;a&gt;) or many other frameworks.&lt;&#x2F;p&gt;
&lt;p&gt;The basic problem is that generally writing to the session is not an atomic
transaction based on the page accessed, so the assumption made in this article
is that when you write to the session it is instantly committed, and there is
no way to roll it back upon failure. If there was, our first example listed
wouldn&#x27;t be able to occur since upon running out of memory at step 3, the
session would have been rolled back and cleaned up.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-should-you-store-in-the-users-session&quot;&gt;What should you store in the users session?&lt;&#x2F;h2&gt;
&lt;p&gt;You should only really store anything in the session that if it were made
public it would do very little harm.&lt;&#x2F;p&gt;
&lt;p&gt;Really the list of items to be stored in a session are as follows:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;The users unique id (The ID that allows you to retrieve the users
information from storage)&lt;&#x2F;li&gt;
&lt;li&gt;Temporary state (i.e. Flash messages)&lt;&#x2F;li&gt;
&lt;li&gt;CSRF token&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;More importantly, don&#x27;t store permission bits, or group memberships, or
anything that is used to allow&#x2F;deny access to particular resources. You want to
store just enough information that upon a user accessing your site you are able
to retrieve the users information from storage, and based upon that information
from storage you then make decisions such as permissions&#x2F;group memberships.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-is-this-important&quot;&gt;Why is this important?&lt;&#x2F;h3&gt;
&lt;p&gt;One of the things that Dan Weber brought up in the Reddit post was storing the
users permission level and group membership in the session. If your code is
then relying on the session to always contain the right permission level, then
there is no way to expire someones access to the data.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;A user logs in&lt;&#x2F;li&gt;
&lt;li&gt;A user gets various permissions, and they are set in the session&lt;&#x2F;li&gt;
&lt;li&gt;The user has been fired from his job, and an administrator removes the
users permissions&lt;&#x2F;li&gt;
&lt;li&gt;Since the user is still logged in, the users permission bits are still
set, and he continues to have access to parts of the site&#x2F;information he
shouldn&#x27;t have access to.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;If instead on every page visit we simply pull out the users unique id and
verify the permissions upon access, as soon as the permissions are revoked by
the administrator the user no longer has access to the various resources.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;temporary-state&quot;&gt;Temporary state&lt;&#x2F;h3&gt;
&lt;p&gt;There has to be an easy way to remember something from page visit to page visit
that isn&#x27;t considered detrimental if the information gets lost. One of those
things is flash messages. Flash messages are generally used to provide the user
indication that something has changed, they are shown once and then never
again.&lt;&#x2F;p&gt;
&lt;p&gt;Storing these as session data makes sense. If the flash message gets set,
great, if it doesn&#x27;t get set, it doesn&#x27;t matter. Flash messages are simply a
notification tool, if the user misses them it isn&#x27;t important.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-you-shouldn-t-store-in-the-session&quot;&gt;What you shouldn&#x27;t store in the session&lt;&#x2F;h2&gt;
&lt;p&gt;Definitely don&#x27;t store any kind of permission bits, groups a user is a part of
or anything that would allow the user access that they normally would not be
able to access.&lt;&#x2F;p&gt;
&lt;p&gt;On each page access check what permissions the user has. While it may mean a
little more heavy lifting server side it provides extra security, and the means
to enforce changes in permissions instantly.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;good-secure-programming-practices&quot;&gt;Good secure programming practices&lt;&#x2F;h2&gt;
&lt;p&gt;Keep secure programming practices in mind at all times, always consider how the
information you are storing&#x2F;processing is accessed&#x2F;viewed&#x2F;administered. More
importantly think about the access controls that are in place, and how one
could expire access to a particular resource without requiring a co-operative
client.&lt;&#x2F;p&gt;
&lt;p&gt;The ordering of how variables are set, and when they are set are very
important.  &lt;code&gt;$_SESSION[&#x27;isadmin&#x27;] = True&lt;&#x2F;code&gt; at the top of a PHP script, and then
removing it by checking to see if the user is actually an administrator later
on in the script is a bad idea.&lt;&#x2F;p&gt;
&lt;p&gt;Always order your code so that if a failure does occur there is no chance that
a critical section of your code is executed by accident, or that information is
stored in a half-verified state. This is especially important for access
control.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Embedding ZeroMQ in the libev Event Loop</title>
        <published>2013-04-20T18:22:00+00:00</published>
        <updated>2013-04-20T18:22:00+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2013/embedding-zeromq-in-the-libev-event-loop/"/>
        <id>https://funcptr.net/2013/embedding-zeromq-in-the-libev-event-loop/</id>
        
        <content type="html" xml:base="https://funcptr.net/2013/embedding-zeromq-in-the-libev-event-loop/">&lt;p&gt;In a &lt;a href=&quot;&#x2F;2012&#x2F;zeromq-edge-triggered&#x2F;&quot;&gt;previous article on ZeroMQ&lt;&#x2F;a&gt; we went over how ZeroMQ is triggered when
you use the socket that ZeroMQ returns, in that article there was some
discussion of embedding ZeroMQ into another event loop. Let&#x27;s do that.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;software.schmorp.de&#x2F;pkg&#x2F;libev.html&quot;&gt;libev&lt;&#x2F;a&gt; is an absolutely fantastic library that helps make it easy to write
evented programs. Evented programs work by getting notified that an action has
happened, and acting upon it. Unlike threaded where multiple pieces of work are
being executed at the same time, in an evented system you move every item that
could block to an event loop, that then calls back into user code with a
notification to continue. If one event uses up more than its fair share of CPU
time because it is busy doing a long calculation, every single other event that
is waiting will never get notified.&lt;&#x2F;p&gt;
&lt;p&gt;Now, as previously discussed ZeroMQ is edge triggered, so embedding it into an
event loop that is level triggered doesn&#x27;t do us much good, because we will miss
certain ZeroMQ notifications.&lt;&#x2F;p&gt;
&lt;p&gt;One way to solve this problem is by looping over ZeroMQ&#x27;s event system until we
get back a notification that it no longer has anything else for us to process,
that would look something like this&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-20130420zeromqedge-1&quot;&gt;&lt;a href=&quot;#fn-20130420zeromqedge&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;size_t&lt;&#x2F;span&gt;&lt;span&gt; zevents_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(zevents);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;zmq_socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;do&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; We can read from the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Check to see if there is more to read ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    zmq_socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; while&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLOUT) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; We can write to the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; If neither of the above is true, then it was a false positive&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However if we are receiving information from ZeroMQ remote endpoints faster
than we can process them, we end up being stuck in that &lt;code&gt;do&lt;&#x2F;code&gt; ... &lt;code&gt;while&lt;&#x2F;code&gt; loop
forever. If we have other events we want to process, that isn&#x27;t entirely fair
since they will never ever get called again. Especially in a server application
where it may be servicing thousands of clients this is simply not acceptable.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;libev&quot;&gt;libev&lt;&#x2F;h2&gt;
&lt;p&gt;libev provides various different event notifications, to be able to get around
edge triggered notifications, and still provide fair round-robin for all events
we are going to have to build on top of multiple different events.&lt;&#x2F;p&gt;
&lt;p&gt;The events used will be:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pod.tst.eu&#x2F;http:&#x2F;&#x2F;cvs.schmorp.de&#x2F;libev&#x2F;ev.pod#code_ev_io_code_is_this_file_descrip&quot;&gt;&lt;code&gt;ev::io&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;: This one is pretty self explanatory, this is for getting notified
about input output changes. This is the one we are going to use on the
&lt;code&gt;ZMQ_FD&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pod.tst.eu&#x2F;http:&#x2F;&#x2F;cvs.schmorp.de&#x2F;libev&#x2F;ev.pod#code_ev_prepare_code_and_code_ev_che&quot;&gt;&lt;code&gt;ev::prepare&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pod.tst.eu&#x2F;http:&#x2F;&#x2F;cvs.schmorp.de&#x2F;libev&#x2F;ev.pod#code_ev_prepare_code_and_code_ev_che&quot;&gt;&lt;code&gt;ev::check&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;: These two are generally used together, they
can be used to change the event loop and or make modifications on the fly to
events that have been registered with the event loop.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pod.tst.eu&#x2F;http:&#x2F;&#x2F;cvs.schmorp.de&#x2F;libev&#x2F;ev.pod#code_ev_idle_code_when_you_ve_got_no&quot;&gt;&lt;code&gt;ev::idle&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;: This is an event that gets fired whenever the event loop has
nothing else to do, so no other events fired, this will fire.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;plan-of-attack&quot;&gt;Plan of attack&lt;&#x2F;h2&gt;
&lt;p&gt;Since the &lt;code&gt;prepare&lt;&#x2F;code&gt; and &lt;code&gt;check&lt;&#x2F;code&gt; run before and after the loop, we are going to
be using those to do most of the work. We use an &lt;code&gt;io&lt;&#x2F;code&gt; so that we can turn off
the &lt;code&gt;idle&lt;&#x2F;code&gt; when we can actually wait for a result from ZeroMQ&#x27;s file
descriptor, otherwise we use &lt;code&gt;idle&lt;&#x2F;code&gt; so that we will always get called once
every loop.&lt;&#x2F;p&gt;
&lt;p&gt;In the &lt;code&gt;prepare&lt;&#x2F;code&gt; watcher callback we do the following:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Check to see what events ZeroMQ has for us, and check what events the user
has requested.&lt;&#x2F;li&gt;
&lt;li&gt;If the ZeroMQ has an event for us that we want, and the user has requested
that event, we start the idle watcher.&lt;&#x2F;li&gt;
&lt;li&gt;If ZeroMQ has no events, we start the io watcher.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;In the &lt;code&gt;check&lt;&#x2F;code&gt; watcher callback we do the following:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Stop both the &lt;code&gt;io&lt;&#x2F;code&gt; and &lt;code&gt;idle&lt;&#x2F;code&gt; watchers, they were only there to make sure
that our &lt;code&gt;check&lt;&#x2F;code&gt; watcher was called.&lt;&#x2F;li&gt;
&lt;li&gt;See what event ZeroMQ has for us, and check that against what the user
wants. Depending on the event, call user defined function write() or user
defined function read().&lt;&#x2F;li&gt;
&lt;li&gt;If this was a spurious wake-up on the part of ZeroMQ we simply ignore it
and let libev go on to other events.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;We could make all of this work by simply using the &lt;code&gt;prepare&lt;&#x2F;code&gt;, &lt;code&gt;check&lt;&#x2F;code&gt; and
&lt;code&gt;idle&lt;&#x2F;code&gt; watchers, but that would mean libev would be busy-waiting for something
to happen on the ZeroMQ socket. The &lt;code&gt;io&lt;&#x2F;code&gt; watcher is required simply so in times
of nothing happening libev in its library can call into the kernels event
handling mechanism and go to sleep. We can&#x27;t use just the &lt;code&gt;io&lt;&#x2F;code&gt; watcher due to
the edge-triggered notification, because we&#x27;d miss all kinds of ZeroMQ
messages. So all four watchers are required, and play crucial parts in making
this work.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;let-s-get-down-to-code&quot;&gt;Let&#x27;s get down to code&lt;&#x2F;h2&gt;
&lt;p&gt;Below you will find example code, it is not complete. Do note that I am using
some C++11isms, error checking code may not be complete&#x2F;correct and in general
I don&#x27;t suggest you copy and paste this without reading and understanding what
it does.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;zmq_event&lt;&#x2F;code&gt; class is meant to be used as a base class, inherit from it, and
create the &lt;code&gt;write()&lt;&#x2F;code&gt; and &lt;code&gt;read()&lt;&#x2F;code&gt; functions. These functions will be called
when you are able to read from the ZeroMQ socket, or when you are able to write
to the ZeroMQ socket. You are guaranteed to be able to read one whole ZeroMQ
message, so if it is a multi-part message, do make sure to loop on
&lt;code&gt;ZMQ_SNDMORE&lt;&#x2F;code&gt; as required.&lt;&#x2F;p&gt;
&lt;p&gt;Upon instantiation it will automatically start being notified about events, we
start off with &lt;code&gt;ev::READ&lt;&#x2F;code&gt;. When your sub-class wants to write to ZeroMQ it
should put the messages to be written into a list somewhere, and set
&lt;code&gt;ev::READ | ev::WRITE&lt;&#x2F;code&gt; on &lt;code&gt;watcher_io&lt;&#x2F;code&gt;, by calling
&lt;code&gt;watcher_io.set(socket_fd, ev::READ | ev::WRITE)&lt;&#x2F;code&gt;. &lt;code&gt;write()&lt;&#x2F;code&gt; will then be
called, write a single message to ZeroMQ, and if necessary when finished
writing, unset &lt;code&gt;ev::WRITE&lt;&#x2F;code&gt; using &lt;code&gt;watcher_io.set(socket_fd, ev::READ)&lt;&#x2F;code&gt;. If you
are not finished writing, after writing that singular message you may return
and &lt;code&gt;write()&lt;&#x2F;code&gt; will be called again the next loop iteration. This way if you
have a lot of data to write you don&#x27;t starve the other events from receiving
their notifications.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;zmq-event-h&quot;&gt;&lt;code&gt;zmq_event.h&lt;&#x2F;code&gt;&lt;&#x2F;h4&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;ev++.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;zmq.hpp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt; zmq_event {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    public:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        zmq_event&lt;&#x2F;span&gt;&lt;span&gt;(zmq::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;context_t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; context&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; type&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; const&lt;&#x2F;span&gt;&lt;span&gt; std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; connect&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        virtual&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; ~zmq_event&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    protected:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; This gets fired before the event loop, to prepare&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; before&lt;&#x2F;span&gt;&lt;span&gt;(ev::prepare&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; prep&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; revents&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; This is fired after the event loop, but before any other type of events&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; after&lt;&#x2F;span&gt;&lt;span&gt;(ev::check&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; check&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; revents&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; We need to have a no-op function available for those events that we&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; want to add to the list, but should never fire an actual event&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        template&lt;&#x2F;span&gt;&lt;span&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;&#x2F;span&gt;&lt;span&gt; T&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            inline&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; noop&lt;&#x2F;span&gt;&lt;span&gt;(T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; w&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; revents&lt;&#x2F;span&gt;&lt;span&gt;) {};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Function we are going to call to write to the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        virtual&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; write&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Function we are going to call to read from the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        virtual&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; read&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Some helper function, one to start notifications&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; start_notify&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; And one to stop notifications.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; stop_notify&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Our event types&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ev::io      watcher_io;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ev::prepare watcher_prepare;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ev::check   watcher_check;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ev::idle    watcher_idle;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Our ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        zmq::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;socket_t&lt;&#x2F;span&gt;&lt;span&gt; socket;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        int&lt;&#x2F;span&gt;&lt;span&gt;           socket_fd &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;= -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h4 id=&quot;zmq-event-cc&quot;&gt;&lt;code&gt;zmq_event.cc&lt;&#x2F;code&gt;&lt;&#x2F;h4&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;stdexcept&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;zmq_event.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zmq_event::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;zmq_event&lt;&#x2F;span&gt;&lt;span&gt;(zmq::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;context_t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; context,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt; type,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; const&lt;&#x2F;span&gt;&lt;span&gt; std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; connect) :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; socket&lt;&#x2F;span&gt;&lt;span&gt;(context, type) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Get the file descriptor for the socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    size_t&lt;&#x2F;span&gt;&lt;span&gt; fd_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(_socket_fd);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_FD,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;socket_fd,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;fd_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Actually connect to the ZeroMQ endpoint, could replace this with a bind as well ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;bind&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;connect&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Set up all of our watchers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Have our IO watcher check for READ on the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(socket_fd, ev::READ);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; This watcher has a no-op callback&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event::noop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Set up our prepare watcher to call the before() function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_prepare&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event::before&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Set up the check watcher to call the after() function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_check&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event::after&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Set up our idle watcher, once again a no-op&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_idle&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zmq_event::noop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Tell libev to start notifying us!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    start_notify&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zmq_event::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;zmq_event&lt;&#x2F;span&gt;&lt;span&gt;() {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zmq_event::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(ev::prepare&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt; revents) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (EV_ERROR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; revents) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        throw&lt;&#x2F;span&gt;&lt;span&gt; std::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;runtime_error&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;libev error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Get any events that may be waiting&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    uint32_t&lt;&#x2F;span&gt;&lt;span&gt; zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    size_t&lt;&#x2F;span&gt;&lt;span&gt; zevents_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(zevents);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Lucky for us, getting the events available doesn&amp;#39;t invalidate the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; events, so that calling this in `before()` and in `after()` will&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; give us the same results.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Check what events exists, and check it against what event we want. We&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; &amp;quot;abuse&amp;quot; our watcher_io.events for this information.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; ((zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLOUT)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;events&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ev::WRITE)) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        watcher_idle&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; ((zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;events&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ev::READ)) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        watcher_idle&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; No events ready to be processed, we&amp;#39;ll just go watch some io&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zmq_event::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;after&lt;&#x2F;span&gt;&lt;span&gt;(ev::check&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt; revents) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (EV_ERROR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; revents) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        throw&lt;&#x2F;span&gt;&lt;span&gt; std::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;runtime_error&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;libev error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Stop both the idle and the io watcher, no point in calling the no-op callback&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; One of them will be reactived by before() on the next loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_idle&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Get the events&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    uint32_t&lt;&#x2F;span&gt;&lt;span&gt; zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    size_t&lt;&#x2F;span&gt;&lt;span&gt; zevents_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(zevents);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Check the events and call the users read&#x2F;write function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; ((zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;events&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ev::READ)) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; ((zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLOUT)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;watcher_io&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BAEBE2;&quot;&gt;events&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ev::WRITE)) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zmq_event::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start_notify&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_check&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_prepare&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;zmq_event::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;stop_notify&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_check&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    watcher_prepare&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;other-event-loops&quot;&gt;Other event loops&lt;&#x2F;h2&gt;
&lt;p&gt;libev is but just one of many event loops that exist out there, hopefully
this shows how it is possible to embed ZeroMQ into an event loop, thereby
making it easier to embed ZeroMQ into any other event loops.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-20130420zeromqedge&quot;&gt;
&lt;p&gt;This snippet was from my older article regarding
&lt;a href=&quot;&#x2F;2012&#x2F;zeromq-edge-triggered&#x2F;&quot;&gt;ZeroMQ edge triggered notifications&lt;&#x2F;a&gt;. I would highly suggest reading that
article for more information and even more background on what is going on. &lt;a href=&quot;#fr-20130420zeromqedge-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ZeroMQ - Edge Triggered Notification</title>
        <published>2012-09-10T19:01:50+00:00</published>
        <updated>2012-09-10T19:01:50+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2012/zeromq-edge-triggered/"/>
        <id>https://funcptr.net/2012/zeromq-edge-triggered/</id>
        
        <content type="html" xml:base="https://funcptr.net/2012/zeromq-edge-triggered/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.zeromq.org&quot;&gt;ZeroMQ&lt;&#x2F;a&gt; is an absolutely fantastic communications library that has allowed
me to very easily create networks&#x2F;connections between multiple different
applications to communicate in an extremely fast yet directed manner and allow
scaling of the connected entities with almost no second thought.&lt;&#x2F;p&gt;
&lt;p&gt;I have used ZeroMQ within Python and C++, and it has been fantastic leaving
that part of the communication layer to a library rather than writing something
similar myself. However, embedding ZeroMQ into an another event loop has proven
to have some quirks. They are documented in the API, but not so much by other
users&#x2F;examples found around the web, and caught me by surprise.&lt;&#x2F;p&gt;
&lt;p&gt;This post mainly goes over the issue, the difference between edge and level
triggered and how ZeroMQ works. I am hoping to spend some time documenting how
to embed ZeroMQ correctly inside &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;software.schmorp.de&#x2F;pkg&#x2F;libev.html&quot;&gt;libev&lt;&#x2F;a&gt; at a later date.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-quirk&quot;&gt;The quirk&lt;&#x2F;h2&gt;
&lt;p&gt;ZeroMQ allows you to get an underlying file descriptor (&lt;code&gt;ZMQ_FD&lt;&#x2F;code&gt;) for a ZeroMQ
socket using &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;api.zeromq.org&#x2F;2-2:zmq-getsockopt#toc21&quot;&gt;&lt;code&gt;getsockopt()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, this is not the actual ZeroMQ file descriptor
but a stand-in:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; fd &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;size_t&lt;&#x2F;span&gt;&lt;span&gt; fd_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(fd);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;zmq_socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_FD,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;fd,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;fd_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This file descriptor can then be passed on to &lt;code&gt;poll()&lt;&#x2F;code&gt;, &lt;code&gt;select()&lt;&#x2F;code&gt;, &lt;code&gt;kqueue()&lt;&#x2F;code&gt;
or whatever your event notification library of choice may be, the idea being
that if the file descriptor is notified to be available for reading then you
check the sockets &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;api.zeromq.org&#x2F;2-2:zmq-getsockopt#toc22&quot;&gt;&lt;code&gt;ZMQ_EVENTS&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and find out if you are allowed to read or
write to the ZeroMQ socket, or even possibly neither (false positive). The same
file descriptor is also used internally within the ZeroMQ library for
notification and thus we may receive an event notification that we can&#x27;t do
anything with.&lt;&#x2F;p&gt;
&lt;p&gt;As stated in the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;api.zeromq.org&#x2F;2-2:zmq-getsockopt#toc21&quot;&gt;ZeroMQ API guide for &lt;code&gt;getsockopt()&lt;&#x2F;code&gt; under &lt;code&gt;ZMQ_FD&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;the ØMQ library shall signal any pending events on the socket in an
&lt;em&gt;edge-triggered&lt;&#x2F;em&gt; fashion by making the file descriptor become ready for
reading.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;and further it states:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;The ability to read from the returned file descriptor does not necessarily
indicate that messages are available to be read from, or can be written to,
the underlying socket; applications must retrieve the actual event state with
a subsequent retrieval of the &lt;code&gt;ZMQ_EVENTS&lt;&#x2F;code&gt; option.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So far so good, so after we return from our event notification on the file
descriptor we simply ask ZeroMQ whether or not we can read or write (or
possibly both):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;size_t&lt;&#x2F;span&gt;&lt;span&gt; zevents_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(zevents);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;zmq_socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; We can read from the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLOUT) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; We can write to the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; If neither of the above is true, then it was a false positive&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now you are happily reading from ZeroMQ and using the data as you wish,
except you start to notice that sometimes it can take a little while for data
to show up, or that if the end-point you are connected to on ZeroMQ sends a lot
of data it is not delivered in a timely fashion. This is where the
documentation for edge triggered comes into play, and that is something that is
different about ZeroMQ compared to for example BSD sockets.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;edge-triggered-versus-level-triggered&quot;&gt;Edge triggered versus level triggered&lt;&#x2F;h2&gt;
&lt;p&gt;Edge and level triggered refer to what kind of signal a device will output in
an attempt to gain the attention of another device. Edge and level triggered
have their history in electronics and hardware, where they play a very crucial
role.&lt;&#x2F;p&gt;
&lt;p&gt;For both level and edge triggered assume the data is represented by this simple string:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000000111111111111111111111100000000011111111111000000000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The zeroes (0) are where no data is available, and the ones (1) are where data
is available for processing.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;level-triggered&quot;&gt;Level triggered&lt;&#x2F;h3&gt;
&lt;p&gt;Level triggered devices when they require attention, and until they no longer
require attention will signal with a high voltage (going back to early
electronics), and when they no longer require attention signal with a low
voltage.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data:  00000000001111111111111111111111000000000011111111111000000000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Block:      A               B              C         D&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+ 5v              ____________________           _________&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -    __________|                    |_________|         |__________&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  0v&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;State: 0          1                    0          1        0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At this point, once the signal turns to 1 in block B we can start processing
data, and until we are done processing data the signal will stay 1. Once we are
done processing the device will signal that we are done by setting the state to
0 in block C, and the process can repeat itself with block D.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;edge-triggered&quot;&gt;Edge triggered&lt;&#x2F;h3&gt;
&lt;p&gt;Edge triggered devices when they require attention will simply turn on the
signal for a short period of time and then turn it back off until all of the
requireding processing is complete at which point they will once again signal
that they need processing.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Data:  00000000001111111111111111111111100000000011111111111000000000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Block:      A     B              C                 D&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;+ 5v              __                              __&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  -    __________|  |____________________________|  |________________&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  0v&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;State: 0          1              0                1        0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this case when the trigger arrives you have to process all of the data
available. When block B arrives and we get 1 as our signal, we have to process
all of the data, we won&#x27;t know when we are done other than some other
notification scheme by the device. (Such as a special message that says we are
done). Then when we are done we can go back to listening to waiting for a
trigger.&lt;&#x2F;p&gt;
&lt;p&gt;If however in the time that we are processing the data from the trigger at B
receive more data to process we won&#x27;t get notified because we weren&#x27;t
officially done processing data from the device, and thus the device will not
notify us of the new data again until we have processed all of the data.&lt;&#x2F;p&gt;
&lt;p&gt;There are some devices that as soon as you start processing their data, you
re-enable the trigger, so if you receive new data while still processing the
old you will get a new notification.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;zeromq-uses-edge-triggered&quot;&gt;ZeroMQ uses edge triggered&lt;&#x2F;h2&gt;
&lt;p&gt;When multiple message arrive for the ZeroMQ socket, one notification is sent.
You check to see if the &lt;code&gt;ZMQ_EVENTS&lt;&#x2F;code&gt; actually contains &lt;code&gt;ZMQ_POLLIN&lt;&#x2F;code&gt; and then
read a single message from ZeroMQ. Content that you have processed the data
ZeroMQ has for you, you add the file descriptor back to the event notification
library and wait. However, you know you sent two messages, but all you got was
one. The next time you send data to the socket, it returns the second message,
and you have now send three but the third doesn&#x27;t seem to arrive.&lt;&#x2F;p&gt;
&lt;p&gt;This is where the edge triggered comes in. ZeroMQ only notifies you once, and
after it has notified you it won&#x27;t notify you again until it receives new
messages, &lt;em&gt;EVEN&lt;&#x2F;em&gt; if you have messages waiting for you. In BSD sockets when you
&lt;code&gt;recv()&lt;&#x2F;code&gt; data from the socket, if it is not all of the data it will simply tell
you, and until all of the data has been read that the socket has to offer it is
going to tell you (level triggered).&lt;&#x2F;p&gt;
&lt;p&gt;This means that when you get a notification from ZeroMQ you have to use a loop
to process all of the messages that are coming your way:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;size_t&lt;&#x2F;span&gt;&lt;span&gt; zevents_len &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(zevents);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;zmq_socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;do&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        &#x2F;&#x2F; We can read from the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Check to see if there is more to read ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;    zmq_socket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;getsockopt&lt;&#x2F;span&gt;&lt;span&gt;(ZMQ_EVENTS,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;zevents_len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; while&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLIN);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (zevents &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; ZMQ_POLLOUT) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; We can write to the ZeroMQ socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; If neither of the above is true, then it was a false positive&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The same is true if you need to send multiple messages, you will need to check
&lt;code&gt;ZMQ_EVENTS&lt;&#x2F;code&gt; for &lt;code&gt;ZMQ_POLLOUT&lt;&#x2F;code&gt; and send messages so long as that holds true, as
soon as it is no longer true you will need to wait until you get triggered
again by using the file descriptor in your event notification library.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;embedding-or-co-existing-with-another-event-loop&quot;&gt;Embedding or co-existing with another event loop&lt;&#x2F;h2&gt;
&lt;p&gt;Embedding ZeroMQ into a different event loop becomes more difficult when you
realise that it is edge triggered versus level triggered. If you want to allow
fair processing of data between events it becomes harder to accomplish with
ZeroMQ because you are required to process all of the data before going back to
your event loop or requires the setting up of different types of events
depending on the state that the ZeroMQ socket is in to make sure you process
all of the data and don&#x27;t forget any.&lt;&#x2F;p&gt;
&lt;p&gt;Edge triggered for the ZeroMQ file descriptor was a design decision which
unfortunately made the use of ZeroMQ sockets more difficult and different from
BSD sockets which makes it harder for application programmers to do the right
thing. There are plenty of questions on the internet regarding the proper usage
of &lt;code&gt;ZMQ_FD&lt;&#x2F;code&gt; with various different event notification schemes&#x2F;implementations.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>IPv6 -- getaddrinfo() and bind() ordering with V6ONLY</title>
        <published>2012-08-07T22:39:56+00:00</published>
        <updated>2012-08-07T22:39:56+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2012/ipv6-and-getaddrinfo/"/>
        <id>https://funcptr.net/2012/ipv6-and-getaddrinfo/</id>
        
        <content type="html" xml:base="https://funcptr.net/2012/ipv6-and-getaddrinfo/">&lt;p&gt;Recently I ran into an issue that took me a while to sort out, and it is
regarding inconsistent behaviour on various OS&#x27;s with regards to IPv6 sockets
(&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799&#x2F;basedefs&#x2F;sys_socket.h.html&quot;&gt;&lt;code&gt;AF_INET6&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-20120807afvspf-1&quot;&gt;&lt;a href=&quot;#fn-20120807afvspf&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;) and calling &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=bind&amp;amp;apropos=0&amp;amp;sektion=2&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;bind(2)&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; after getting the
results back from &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=getaddrinfo&amp;amp;apropos=0&amp;amp;sektion=3&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;getaddrinfo(3)&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A call to &lt;code&gt;getaddrinfo()&lt;&#x2F;code&gt; with the hints set to &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799&#x2F;basedefs&#x2F;sys_socket.h.html&quot;&gt;&lt;code&gt;AF_UNSPEC&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in &lt;code&gt;ai_family&lt;&#x2F;code&gt;
and &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=getaddrinfo&amp;amp;apropos=0&amp;amp;sektion=3&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;AI_PASSIVE&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in &lt;code&gt;ai_flags&lt;&#x2F;code&gt; will return to us 1 or more results that we
can &lt;code&gt;bind()&lt;&#x2F;code&gt; to. Sample code for that looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; addrinfo hints,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt;addrlist;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;memset&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(hints));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Ask for TCP&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hints.ai_socktype &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; SOCK_STREAM;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Any family works for us ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hints.ai_family &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; AF_UNSPEC;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Set some hints&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hints.ai_flags &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AI_PASSIVE    &lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; &#x2F;&#x2F; We want to use this with bind&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AI_ADDRCONFIG;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;  &#x2F;&#x2F; Only return IPv4 or IPv6 if they are configured&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; rv;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; ((rv &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; getaddrinfo&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;7020&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;addrlist&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    fprintf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(stderr,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;getaddrinfo: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; gai_strerror&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(rv))&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Use the list in *addrlist&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (addr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; addrlist; addr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;!=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; addr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; addr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;ai_next) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; use *addr as appropriate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Clean up the memory from getaddrinfo()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;freeaddrinfo&lt;&#x2F;span&gt;&lt;span&gt;(addrlist);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;On Linux there are two entries returned when the host it is run on has both
IPv4 and IPv6 enabled. An &lt;code&gt;AF_INET&lt;&#x2F;code&gt; which was followed by an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt;. Now,
it is not said that you are required to use all of the results that are
returned, but if you want to listen on all address families it is off course
suggested.&lt;&#x2F;p&gt;
&lt;p&gt;Following the steps below for each of the returned results should result in
having 1 or more different sockets that are bound to a single port.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create the socket()&lt;&#x2F;li&gt;
&lt;li&gt;Set any socket options you want (&lt;code&gt;SO_REUSEADDR&lt;&#x2F;code&gt; for example)&lt;&#x2F;li&gt;
&lt;li&gt;Then bind() the socket&lt;&#x2F;li&gt;
&lt;li&gt;After that call listen() (followed off course by accept() on the socket)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Only for some unknown reason (and &lt;code&gt;errno&lt;&#x2F;code&gt; is no help) &lt;code&gt;bind()&lt;&#x2F;code&gt; fails when you
get to the &lt;code&gt;AF_INET6&lt;&#x2F;code&gt;, which was returned second. Searching online as to why
the bind would fail doesn&#x27;t give you any good results and the thing that is
even worse is that if you run the same code on another platform such as
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&quot;&gt;FreeBSD&lt;&#x2F;a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;openindiana.org&quot;&gt;OpenIndiana&lt;&#x2F;a&gt; or &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20120808044613&#x2F;http:&#x2F;&#x2F;www.apple.com&#x2F;osx&#x2F;&quot;&gt;Mac OS X&lt;&#x2F;a&gt; no such failure exists. However
I started suspecting something was up when I started looking at the output from
&lt;code&gt;netstat -lan | grep 7020&lt;&#x2F;code&gt; on Mac OS X. Where 7020 is the port I passed into
&lt;code&gt;getaddrinfo()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tcp46      0      0  *.7020                 *.*  LISTEN     &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tcp4       0      0  *.7020                 *.*  LISTEN &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Wait a minute ... one of the sockets is on both IPv4 and on IPv6. Some more
time spent searching the internet I came across &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;tools.ietf.org&#x2F;html&#x2F;rfc3493#page-22&quot;&gt;RFC 3493 section 5.3&lt;&#x2F;a&gt;,
which is titled &quot;&lt;code&gt;IPV6_V6ONLY&lt;&#x2F;code&gt; option for &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; Sockets&quot;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;As stated in section &amp;lt;3.7 Compatibility with IPv4 Nodes&amp;gt;, &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; sockets
may be used for both IPv4 and IPv6 communications.  Some applications may
want to restrict their use of an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; socket to IPv6 communications
only.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This was going down the right route, so I changed my code so that in the steps
listed above in number 2 I added the following code if the socket type is
&lt;code&gt;AF_INET6&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;setsockopt&lt;&#x2F;span&gt;&lt;span&gt;(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;yes&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    close&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(sockfd)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    fprintf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(stderr,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;setsockopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; IPV6_V6ONLY&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; strerror&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(errno))&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    continue&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The RFC 3493 section 5.3 also states that this option should be turned off by
default, which means that all IPv6 sockets can also communicate over IPv4. Thus
technically setting the option manually in code the best way to fix the issue.
FreeBSD has had this feature turned on (as in IPv6 sockets can only communicate
with IPv6 and NOT IPv4) since 5.x.&lt;&#x2F;p&gt;
&lt;p&gt;The biggest issue is that the remaining operating systems (OS X and
OpenIndiana) don&#x27;t have the same behaviour as Linux which makes troubleshooting
this issue more difficult than it should be. The issue is that the RFC doesn&#x27;t
specify what exactly the operating should do when it encounters a request to
bind to the same port on IPv4 and IPv6. The only place where I have found this
documented is in &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.amazon.com&#x2F;Network-Programming-Jun-ichiro-itojun-Hagino&#x2F;dp&#x2F;1555583180&quot;&gt;&quot;IPv6 Network Programming&quot;&lt;&#x2F;a&gt; under &quot;Tips in IPv6
Programming&quot; chapter 4, section 4, appropriately titled &quot;bind(2) Ordering and
Conflicts&quot;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;If you get a &lt;code&gt;bind()&lt;&#x2F;code&gt; error when attempting to bind to an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; socket
please make sure that you set the socket option &lt;code&gt;IPV6_V6ONLY&lt;&#x2F;code&gt; on the &lt;code&gt;AF_INET6&lt;&#x2F;code&gt;
socket. The default as required by RFC 3493 is to have that option be off. The
default is wrong, and the RFC should have been more specific regarding what the
right behaviour is when attempting to bind on an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; socket when already
bound on an &lt;code&gt;AF_INET&lt;&#x2F;code&gt; while &lt;code&gt;IPV6_V6ONLY&lt;&#x2F;code&gt; is set to false.&lt;&#x2F;p&gt;
&lt;p&gt;The full code that I used for testing, along with a little bit more information
is available as a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;3231892&quot;&gt;gist on github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-20120807afvspf&quot;&gt;
&lt;p&gt;The old BSD style &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=socket&amp;amp;apropos=0&amp;amp;sektion=2&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;socket()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; called for defines
starting with &lt;code&gt;PF_&lt;&#x2F;code&gt; such as &lt;code&gt;PF_INET&lt;&#x2F;code&gt; and &lt;code&gt;PF_INET6&lt;&#x2F;code&gt; with the &lt;code&gt;PF&lt;&#x2F;code&gt; standing for
protocol family. POSIX starts them with &lt;code&gt;AF_&lt;&#x2F;code&gt;, and calls them an address
family. On almost every operating system &lt;code&gt;PF_INET&lt;&#x2F;code&gt; is the same as &lt;code&gt;AF_INET&lt;&#x2F;code&gt;. If
the define doesn&#x27;t exist you can always create it. &lt;a href=&quot;#fr-20120807afvspf-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>OpenSSL as a Filter (or non-blocking OpenSSL)</title>
        <published>2012-04-08T16:50:14+00:00</published>
        <updated>2012-04-08T16:50:14+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2012/openssl-as-a-filter/"/>
        <id>https://funcptr.net/2012/openssl-as-a-filter/</id>
        
        <content type="html" xml:base="https://funcptr.net/2012/openssl-as-a-filter/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;openssl.org&#x2F;&quot;&gt;OpenSSL&lt;&#x2F;a&gt; as a library at first glance is complicated, and then you realise
that a lot of the &lt;a rel=&quot;external&quot; title=&quot;ssl(3) documentation&quot; href=&quot;http:&#x2F;&#x2F;openssl.org&#x2F;docs&#x2F;ssl&#x2F;ssl.html&quot;&gt;documentation&lt;&#x2F;a&gt; seems to be incomplete or missing.
Generally the individual man pages for the various functions are not bad, and
they will give you relevant information to help you along, but it can be hard
to find.&lt;&#x2F;p&gt;
&lt;p&gt;The idea is to write a filter in such a way that OpenSSL can easily be disabled
or removed. We don&#x27;t want to rely on OpenSSL specific functionality in our
code, for instance in the future we may want to change to using &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;botan.randombit.net&#x2F;&quot;&gt;Botan&lt;&#x2F;a&gt; or
another SSL&#x2F;TLS library without having to change a lot of core functionality.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;possible-methods&quot;&gt;Possible methods&lt;&#x2F;h2&gt;
&lt;p&gt;There are various ways of doing non-blocking OpenSSL, the main one is to simply
set the underlying socket to non-blocking and pass it into OpenSSL, at that
point the functions &lt;code&gt;SSL_write()&lt;&#x2F;code&gt; and &lt;code&gt;SSL_read()&lt;&#x2F;code&gt; will enter various error
states that can be read by &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.openssl.org&#x2F;docs&#x2F;ssl&#x2F;SSL_get_error.html&quot;&gt;&lt;code&gt;SSL_get_error()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, more specifically the
function will return either &lt;code&gt;SSL_ERROR_WANT_READ&lt;&#x2F;code&gt; or &lt;code&gt;SSL_ERROR_WANT_WRITE&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The other method, the one used below, was described by &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;lists.schmorp.de&#x2F;pipermail&#x2F;libev&#x2F;2008q2&#x2F;000325.html&quot;&gt;Marc Lehmann in a post
to the libev mailling list&lt;&#x2F;a&gt; and uses &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.openssl.org&#x2F;docs&#x2F;crypto&#x2F;bio.html&quot;&gt;memory BIO&lt;&#x2F;a&gt; objects. Specifically
he linked to some Perl sample code; the OpenSSL &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;ue.tst.eu&#x2F;f7383715351b4427b6b9db660b6289f8.txt&quot;&gt;setup&lt;&#x2F;a&gt;, and the function
called for new incoming data&#x2F;data to be written: &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;ue.tst.eu&#x2F;2f4cfee5166db411bd85a40e06de6dbf.txt&quot;&gt;dotls function&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;The code is very new and might still be buggy, but it outlines the
principles: use a memory stream, which will avoid all issues with blocking in
openssl&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;It shows the idea pretty clearly but unless you know Perl it can be really
confusing as to what is going on.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-ssl-filter-implementation&quot;&gt;The SSL Filter implementation&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.openssl.org&#x2F;docs&#x2F;ssl&#x2F;SSL_CTX_new.html&quot;&gt;&lt;code&gt;SSL_CTX&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; that is passed in contains all of the initialisation that is
done for OpenSSL in general, which is not shown here. The &lt;code&gt;SSLFilter&lt;&#x2F;code&gt; is
created for each time you want to have a socket start doing SSL.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; openssl_filter.h&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt; SSLFilter {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    public:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        SSLFilter&lt;&#x2F;span&gt;&lt;span&gt;(SSL_CTX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; ctxt&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; nread&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; nwrite&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; aread&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; awrite&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        virtual&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; ~SSLFilter&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        void&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; update&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    private:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        bool&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; continue_ssl_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; function_return&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        SSL &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; ssl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        BIO &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; rbio;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        BIO &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; wbio;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; nread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; nwrite;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; aread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; awrite;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The class contains mainly various different bits of state, when you create the
filter you pass in a pointer to four different strings, they are used as
follows:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nread&lt;&#x2F;code&gt;: This contains data to be processed by the filter, from the network&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;nwrite&lt;&#x2F;code&gt;: This contains data that has been processed by the filter, and has
to be sent to the network&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;aread&lt;&#x2F;code&gt;: This contains data that has been processed by the filter, and is
ready to be processed by the application&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;awrite&lt;&#x2F;code&gt;: This contains data that the application wants to send out to the
network and is ready for processing by the filter.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The other private variables contain various different pieces of state and are
created by the filters constructors.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ssl&lt;&#x2F;code&gt;: Contains the state for the current SSL connection.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;rbio&lt;&#x2F;code&gt;: Contains the data that the SSL functions will read from, data put in
here is copied from &lt;code&gt;nread&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;wbio&lt;&#x2F;code&gt;: Contains the data that the SSL functions will write to, data from
this BIO is copied to &lt;code&gt;nwrite&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The implementation is the really interesting part.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; openssl_filter.cc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;stdexcept&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;openssl_filter.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  7&lt;&#x2F;span&gt;&lt;span&gt;SSLFilter::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;SSLFilter&lt;&#x2F;span&gt;&lt;span&gt;(SSL_CTX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; ctxt, &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  8&lt;&#x2F;span&gt;&lt;span&gt;                     std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; nread, &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  9&lt;&#x2F;span&gt;&lt;span&gt;                     std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; nwrite,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;                     std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; aread,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 11&lt;&#x2F;span&gt;&lt;span&gt;                     std::string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; awrite)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span&gt;                      :&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;                     nread&lt;&#x2F;span&gt;&lt;span&gt;(nread), &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 14&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;                     nwrite&lt;&#x2F;span&gt;&lt;span&gt;(nwrite), &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 15&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;                     aread&lt;&#x2F;span&gt;&lt;span&gt;(aread), &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;                     awrite&lt;&#x2F;span&gt;&lt;span&gt;(awrite) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 17&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 18&lt;&#x2F;span&gt;&lt;span&gt;    rbio &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; BIO_new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;BIO_s_mem&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 19&lt;&#x2F;span&gt;&lt;span&gt;    wbio &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; BIO_new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;BIO_s_mem&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 21&lt;&#x2F;span&gt;&lt;span&gt;    ssl &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; SSL_new&lt;&#x2F;span&gt;&lt;span&gt;(ctxt);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 22&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 23&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    SSL_set_accept_state&lt;&#x2F;span&gt;&lt;span&gt;(ssl);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 24&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    SSL_set_bio&lt;&#x2F;span&gt;&lt;span&gt;(ssl, rbio, wbio);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 25&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 26&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 27&lt;&#x2F;span&gt;&lt;span&gt;SSLFilter::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;SSLFilter&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 28&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    SSL_free&lt;&#x2F;span&gt;&lt;span&gt;(_ssl);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 29&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 30&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 31&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt; SSLFilter::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;update&lt;&#x2F;span&gt;&lt;span&gt;(Filter::FilterDirection) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; If we have data from the network to process, put it the memory BIO for OpenSSL&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 33&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;nread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;empty&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 34&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        int&lt;&#x2F;span&gt;&lt;span&gt; written &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; BIO_write&lt;&#x2F;span&gt;&lt;span&gt;(rbio,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; nread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; nread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 35&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (written &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; nread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;erase&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, written);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 36&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 37&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 38&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; If the application wants to write data out to the network, process it with SSL_write&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 39&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;awrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;empty&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 40&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        int&lt;&#x2F;span&gt;&lt;span&gt; written &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; SSL_write&lt;&#x2F;span&gt;&lt;span&gt;(ssl,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; awrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;c_str&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; awrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 41&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 42&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;continue_ssl_&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 43&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            throw&lt;&#x2F;span&gt;&lt;span&gt; std::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;runtime_error&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;An SSL error occured.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 44&lt;&#x2F;span&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 45&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 46&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (written &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; awrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;erase&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, written);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 47&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 48&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 49&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Read data for the application from the encrypted connection and place it in the string for the app to read&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 50&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    while&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 51&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        char *&lt;&#x2F;span&gt;&lt;span&gt;readto &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; char&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 52&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        int&lt;&#x2F;span&gt;&lt;span&gt; read &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; SSL_read&lt;&#x2F;span&gt;&lt;span&gt;(ssl, readto,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1024&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 53&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 54&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;continue_ssl_&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 55&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;            delete&lt;&#x2F;span&gt;&lt;span&gt; readto;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 56&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            throw&lt;&#x2F;span&gt;&lt;span&gt; std::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;runtime_error&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;An SSL error occured.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 57&lt;&#x2F;span&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 58&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 59&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (read &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 60&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;            size_t&lt;&#x2F;span&gt;&lt;span&gt; cur_size &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; aread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 61&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;            aread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;resize&lt;&#x2F;span&gt;&lt;span&gt;(cur_size &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; read);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 62&lt;&#x2F;span&gt;&lt;span&gt;            std::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span&gt;(readto, readto &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; read,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; aread&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;begin&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; cur_size);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 63&lt;&#x2F;span&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 64&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 65&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;        delete&lt;&#x2F;span&gt;&lt;span&gt; readto;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 66&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 67&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;static_cast&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;size_t&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(read)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1024&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ||&lt;&#x2F;span&gt;&lt;span&gt; written &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 68&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 69&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 70&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Read any data to be written to the network from the memory BIO and copy it to nwrite&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 71&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    while&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 72&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        char *&lt;&#x2F;span&gt;&lt;span&gt;readto &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; char&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 73&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        int&lt;&#x2F;span&gt;&lt;span&gt; read &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; BIO_read&lt;&#x2F;span&gt;&lt;span&gt;(wbio, readto,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1024&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 74&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 75&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (read &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 76&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;            size_t&lt;&#x2F;span&gt;&lt;span&gt; cur_size &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; nwrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 77&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;            nwrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;resize&lt;&#x2F;span&gt;&lt;span&gt;(cur_size &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; read);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 78&lt;&#x2F;span&gt;&lt;span&gt;            std::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span&gt;(readto, readto &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; read,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; nwrite&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;begin&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; cur_size);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 79&lt;&#x2F;span&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 80&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 81&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;        delete&lt;&#x2F;span&gt;&lt;span&gt; readto;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 82&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 83&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;static_cast&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;lt;size_t&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(read)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1024&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ||&lt;&#x2F;span&gt;&lt;span&gt; read &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 84&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 85&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 86&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 87&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span&gt; SSLFilter::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;continue_ssl_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; function_return&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 88&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    int&lt;&#x2F;span&gt;&lt;span&gt; err &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; SSL_get_error&lt;&#x2F;span&gt;&lt;span&gt;(ssl, function_return);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 89&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 90&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (err &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt; SSL_ERROR_NONE &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;||&lt;&#x2F;span&gt;&lt;span&gt; err &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt; SSL_ERROR_WANT_READ) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 91&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 92&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 93&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 94&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (err &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt; SSL_ERROR_SYSCALL) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 95&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        ERR_print_errors_fp&lt;&#x2F;span&gt;&lt;span&gt;(stderr);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 96&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        perror&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;syscall error: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 97&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 98&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 99&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (err &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt; SSL_ERROR_SSL) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        ERR_print_errors_fp&lt;&#x2F;span&gt;&lt;span&gt;(stderr);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;103&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;104&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;105&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;explanation-of-the-ssl-filter-class&quot;&gt;Explanation of the SSL Filter class&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;Line 7 - 25&lt;&#x2F;strong&gt;, we create the two memory BIO&#x27;s and then call &lt;code&gt;SSL_new()&lt;&#x2F;code&gt; using
the passed in &lt;code&gt;SSL_CTX&lt;&#x2F;code&gt;, set the accept set for SSL and then we hook up the two
BIO objects to the &lt;code&gt;ssl&lt;&#x2F;code&gt; object.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Line 27 - 29&lt;&#x2F;strong&gt;, All we do in the destructor is &lt;code&gt;SSL_free()&lt;&#x2F;code&gt; the SSL state,
which automatically takes care of freeing the two memory BIOs that were given
to it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Line 33 - 36&lt;&#x2F;strong&gt;, if &lt;code&gt;nread&lt;&#x2F;code&gt; is not empty we copy the data into &lt;code&gt;rbio&lt;&#x2F;code&gt;. &lt;code&gt;rbio&lt;&#x2F;code&gt;
is used by the SSL functions to read from (as if it were reading from a
socket). We read into the memory BIO as much as possible, technically this
should be everything, but it is possible it won&#x27;t read everything due to memory
constraints, next time &lt;code&gt;SSLFilter::update()&lt;&#x2F;code&gt; is called it will get emptied as
much as possible again.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Line 39 - 47&lt;&#x2F;strong&gt;, we see if there is anything that the app wants to write out
to the remote client, if there is we call &lt;code&gt;SSL_write()&lt;&#x2F;code&gt;. After the call to
&lt;code&gt;SSL_write()&lt;&#x2F;code&gt; we call &lt;code&gt;continue_ssl_()&lt;&#x2F;code&gt; to see if we can safely continue using
SSL.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Line 50 - 68&lt;&#x2F;strong&gt;, this is where try to read as much data from &lt;code&gt;SSL_read()&lt;&#x2F;code&gt; as
we can and place it in &lt;code&gt;aread&lt;&#x2F;code&gt;. This is data that has been decrypted by OpenSSL
and can be used by the application. After the call to &lt;code&gt;SSL_read()&lt;&#x2F;code&gt; we call
&lt;code&gt;continue_ssl_()&lt;&#x2F;code&gt; to see if we can safely continue using SSL.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Line 71 - 84&lt;&#x2F;strong&gt;, if OpenSSL has to write something to the network, either
directly due to something the app did (by filling &lt;code&gt;awrite&lt;&#x2F;code&gt;) or because of
something the remote client sent, it will place it in its memory buffer
(&lt;code&gt;wbio&lt;&#x2F;code&gt;), what we do here is drain that memory buffer and fill &lt;code&gt;nwrite&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Line 87 - 105&lt;&#x2F;strong&gt;, the &lt;code&gt;continue_ssl_()&lt;&#x2F;code&gt; function gets the SSL errors using
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.openssl.org&#x2F;docs&#x2F;ssl&#x2F;SSL_get_error.html&quot;&gt;&lt;code&gt;SSL_get_error&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and checks that either it is &lt;code&gt;SSL_ERROR_NONE&lt;&#x2F;code&gt; or
&lt;code&gt;SSL_ERROR_WANT_READ&lt;&#x2F;code&gt; both of which are acceptable errors, any other errors and
we check to see if it is an error due to a system call or due to an SSL error,
we print out the errors to standard error and return false. At that point the
calling code is free to go about its business as it pleases (most likely
throwing an error).&lt;&#x2F;p&gt;
&lt;h4 id=&quot;notes-regarding-this-sample-code&quot;&gt;Notes regarding this sample code ...&lt;&#x2F;h4&gt;
&lt;p&gt;The only thing missing in this example code is checking to see if the SSL
connection has gone into an &lt;code&gt;SSL_shutdown()&lt;&#x2F;code&gt; mode. This should be added so that
you cleanly shut down an SSL connection, but it is not absolutely required,
especially since it seems to be shaky as to whether or not it will work,
especially if you are using SSLv3 or SSLv2.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-to-use-the-ssl-filter&quot;&gt;How to use the SSL Filter&lt;&#x2F;h3&gt;
&lt;p&gt;Here are the steps to using this filter:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;At program startup set up the &lt;code&gt;SSL_CTX&lt;&#x2F;code&gt; structure as required, so that it
be used in the &lt;code&gt;SSLFilter&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Set up socket&#x2F;event loop and accept() new socket.&lt;&#x2F;li&gt;
&lt;li&gt;Create four new &lt;code&gt;std::string&lt;&#x2F;code&gt;&#x27;s and create a new SSLFilter, pass in the
&lt;code&gt;SSL_CTX&lt;&#x2F;code&gt; you created, and the four buffers.&lt;&#x2F;li&gt;
&lt;li&gt;Add the new socket to the event loop to wait for reading.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now upon receiving a read ready status from the event loop, the following should be done:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Read data into &lt;code&gt;nread&lt;&#x2F;code&gt; buffer.&lt;&#x2F;li&gt;
&lt;li&gt;call &lt;code&gt;SSLFilter::update()&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;See if &lt;code&gt;aread&lt;&#x2F;code&gt; is not empty, process the data as you please&lt;&#x2F;li&gt;
&lt;li&gt;Write data to &lt;code&gt;awrite&lt;&#x2F;code&gt; as needed, if data is written to &lt;code&gt;awrite&lt;&#x2F;code&gt;, call
&lt;code&gt;SSLFilter::update(WRITE)&lt;&#x2F;code&gt; to process the data&lt;&#x2F;li&gt;
&lt;li&gt;See if &lt;code&gt;nwrite&lt;&#x2F;code&gt; is not empty, if so add socket to event loop for write
readiness.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Once you receive a write ready status from the event loop, you should do the following:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Write data in &lt;code&gt;nwrite&lt;&#x2F;code&gt; to the socket.&lt;&#x2F;li&gt;
&lt;li&gt;If &lt;code&gt;nwrite&lt;&#x2F;code&gt; is empty, remove the socket from the event loop for writing (so
that you don&#x27;t have the event loop notifying you of the ability to write,
even-though you have nothing to write).&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now it becomes simple to add OpenSSL to any open socket, in a way that is easy
to refactor later and or disable depending on parameters passed in as arguments
to the program. If you set &lt;code&gt;nread&lt;&#x2F;code&gt; to the same string as &lt;code&gt;aread&lt;&#x2F;code&gt; and &lt;code&gt;nwrite&lt;&#x2F;code&gt;
to &lt;code&gt;awrite&lt;&#x2F;code&gt; and don&#x27;t create an SSLFilter you have bypassed the filter...&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Magic Properties on Google AppEngine</title>
        <published>2010-03-24T02:15:33+00:00</published>
        <updated>2010-03-24T02:15:33+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2010/magic-properties-on-google-appengine/"/>
        <id>https://funcptr.net/2010/magic-properties-on-google-appengine/</id>
        
        <content type="html" xml:base="https://funcptr.net/2010/magic-properties-on-google-appengine/">&lt;p&gt;Update to my previous &lt;a href=&quot;&#x2F;2010&#x2F;auto-updating-properties-in-google-appengine-models&#x2F;&quot;&gt;Google AppEngine auto-updating properties post&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In my last post I was talking about:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;[...] however there is still an issue, for now the MagicProperty is only
updated the first time the property it affects is set&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;I fixed that issue, it has a local cache that is added to the model, so if the
value does change, the cache would no longer be valid and the value is
recreated, otherwise the already computed value is returned. There was however
another issue I had to deal with, and that was the fact that the cache was not
being written to the datastore, this meant that upon getting the data back from
the datastore we recomputed it anyway. Now the programmer has to do some extra
work, they have to add a cache unindexed property to the model, and pass it
into the &lt;code&gt;MagicProperty&lt;&#x2F;code&gt; to be used as the caching variable. Within the
&lt;code&gt;save()&lt;&#x2F;code&gt; they have to prime the cache by setting a temporary variable equal to
the to be computed variable just in case the cache variables have not been
primed, this has not yet been documented other than in my SVN history.&lt;&#x2F;p&gt;
&lt;p&gt;We still don&#x27;t allow setting of the magic property, however upon returning from
the datastore it calls &lt;code&gt;make_value_from_datastore&lt;&#x2F;code&gt;, which returns a
&lt;code&gt;_MagicDatastore&lt;&#x2F;code&gt;, as long as we set with an instance of &lt;code&gt;_MagicDatastore&lt;&#x2F;code&gt; it will
allow it to go through, so we can get the value from the datastore without
having to recompute the value.&lt;&#x2F;p&gt;
&lt;p&gt;Read the code for an example on how to use this.&lt;&#x2F;p&gt;
&lt;p&gt;Code is added below, feel free to use it as you wish under the license that is
attached.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;###&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # Copyright (c) 2010 Delta Regeer;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # Permission to use, copy, modify, and distribute this software for any&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # purpose with or without fee is hereby granted, provided that the above&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  6&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # copyright notice and this permission notice appear in all copies.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot; AND THE AUTHOR DISCLAIMS ALL WARRANTIES&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;  9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 11&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 14&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 15&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;###&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 17&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; logging&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 19&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; hashlib&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; google.appengine.ext&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; db&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 21&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 22&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 23&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; MagicProperty&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;prop&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; magic_func=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; cache_prop=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; pass_instance=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *args&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **kw&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 24&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; magic_func:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 25&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # No pants required.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 26&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt; _MagicProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; cache_prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; pass_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;kw&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 27&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 28&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # We are putting some pants on the function.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 29&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; pants&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 30&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt; _MagicProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; cache_prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; pass_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;kw&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 31&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; pants&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 33&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFCB8B;&quot;&gt; _MagicDatastore&lt;&#x2F;span&gt;&lt;span&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 34&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;This is an internal class for _MagicProperty.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 35&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 36&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 37&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; val&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 38&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 39&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; retval&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 40&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 41&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 42&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFCB8B;&quot;&gt; _MagicProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;Property&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 43&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;MagicProperty which will modify output based on a function that it is given.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 44&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 45&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    This has several ways in which it may be called:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 46&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 47&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    In this example we create the model, no caching is done, so any data that is returned from the datastore&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 48&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    will be erased the first time that the MagicProperty is accessed and recomputed.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 49&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 50&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        class MagicTest(db.Model):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 51&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            title = db.StringProperty(required=True)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 52&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            chars = utils.MagicProperty(title, len, required=True)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 53&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 54&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        mytest = MagicTest(title=&amp;quot;It was for the good of the school!&amp;quot;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 55&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 56&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytest.title&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 57&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        It was for the good of the school!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 58&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytest.chars&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 59&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        34&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 60&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 61&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        mytest = MagicTest.all().get()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 62&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 63&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytest.title&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 64&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        Hello&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 65&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytest.chars		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 66&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        5	# Do note, this is recalculated the first time it is called, as long as title does not change it won&amp;#39;t be recomputed.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 67&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 68&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    In this example we create the model, and we also create a caching property so that even when we get values back &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 69&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    from the datastore we use the cached computed value rather than running the function again. Do note that this requires overriding put()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 70&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    to prime the cache as there is currently no way to specify that certain properties should be &amp;quot;saved&amp;quot; before others.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 71&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 72&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        class MagicTesting(db.Model):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 73&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            title = db.StringProperty(required=True)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 74&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            cache = db.UnindexedProperty()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 75&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            chars = utils.MagicProperty(title, len, cache_prop=cache, required=True)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 76&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 77&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            def put(self, *args, **kw):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 78&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;                prime_cache = self.chars&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 79&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;                super(MagicTesting).put(*args, **kw)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 81&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        mytesting = MagicTesting(title=&amp;quot;Get the pocket knife out of my boot.&amp;quot;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 82&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 83&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytesting.title&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 84&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        Get the pocket knife out of my boot.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 85&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytesting.chars&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 86&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        36&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 87&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 88&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        mytesting = MagicTest.all().get()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 89&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 90&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytesting.title&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 91&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        How are you?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 92&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        &amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;print mytesting.chars	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 93&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        12	# This is not recomputed so long as the title has not changed, however it uses more datastore space to store a hash.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 94&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 95&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 96&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    Inspired by: &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 97&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    http:&#x2F;&#x2F;appengine-cookbook.appspot.com&#x2F;recipe&#x2F;custom-model-properties-are-cute&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 98&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    http:&#x2F;&#x2F;code.google.com&#x2F;appengine&#x2F;articles&#x2F;extending_models.html&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt; 99&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    http:&#x2F;&#x2F;googleappengine.blogspot.com&#x2F;2009&#x2F;07&#x2F;writing-custom-property-classes.html&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;102&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; prop&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; magic_func&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; cache_prop&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; pass_instance&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *args&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **kw&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;103&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;104&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        Extra parameters you can give this initializer.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;105&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;106&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            prop		= Property to be acted upon&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;107&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            magic_func	= The function to be called when the property is accessed&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;108&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;            cache_prop	= The property that can hold our cache, I suggest it is an db.UnindexedProperty() since it just stores sha1 hashes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;109&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;110&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        super&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;_MagicProperty&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;kw&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;111&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; magic_func&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;112&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.magic_prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; prop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;113&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.magic_cache&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; cache_prop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;114&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.magic_pass&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pass_instance&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;115&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;116&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; get_cache_val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; model_instance&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;117&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.magic_cache&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; is not&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;118&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.magic_cache.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;__get__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;119&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; getattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;orig&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;120&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;121&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; set_cache_val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; model_instance&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;122&lt;&#x2F;span&gt;&lt;span&gt;        val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; hashlib.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;sha1&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;hexdigest&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;123&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;124&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.magic_cache&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; is not&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;125&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;            self&lt;&#x2F;span&gt;&lt;span&gt;.magic_cache.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;__set__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;126&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        setattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;orig&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;127&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;128&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; attr_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;129&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # In google.appengine.ex.db there is an explicit warning not to use this method, so we test for it first.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;130&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;._attr_name:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;131&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;_attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;132&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;133&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;134&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;135&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __get__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; model_instance&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;136&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; is&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;137&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;138&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;139&lt;&#x2F;span&gt;&lt;span&gt;        cur&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.magic_prop.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;__get__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;140&lt;&#x2F;span&gt;&lt;span&gt;        cur&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; cur.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;encode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;141&lt;&#x2F;span&gt;&lt;span&gt;        last&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;get_cache_val&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;142&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; last&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; hashlib.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;sha1&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;cur&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;hexdigest&lt;&#x2F;span&gt;&lt;span&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;143&lt;&#x2F;span&gt;&lt;span&gt;            logging.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;Cache hit: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; (cur)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;144&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; getattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;145&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;146&lt;&#x2F;span&gt;&lt;span&gt;        logging.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;Cache miss: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; (cur)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;147&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;148&lt;&#x2F;span&gt;&lt;span&gt;        magic_done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; = u&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;149&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.magic_pass:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;150&lt;&#x2F;span&gt;&lt;span&gt;            magic_done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;magic_func&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; cur&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;151&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;152&lt;&#x2F;span&gt;&lt;span&gt;            magic_done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;magic_func&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;cur&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;153&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;154&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # Set the attribute in the model&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;155&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        setattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; magic_done&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;156&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;set_cache_val&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; cur&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;157&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;158&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; magic_done&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;159&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;160&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;161&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __set__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; model_instance&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;162&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; isinstance&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; _MagicDatastore&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;163&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;            setattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; value.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;retval&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;164&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;165&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            raise&lt;&#x2F;span&gt;&lt;span&gt; db.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;DerivedPropertyError&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;MagicProperty is magic. Magic may not be modified.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;166&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;167&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; make_value_from_datastore&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: #4B6479;&quot;&gt;168&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt; _MagicDatastore&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Auto-updating Properties in Google AppEngine Models</title>
        <published>2010-02-21T04:03:42+00:00</published>
        <updated>2010-03-24T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2010/auto-updating-properties-in-google-appengine-models/"/>
        <id>https://funcptr.net/2010/auto-updating-properties-in-google-appengine-models/</id>
        
        <content type="html" xml:base="https://funcptr.net/2010/auto-updating-properties-in-google-appengine-models/">&lt;p&gt;EDIT: I have an updated version of this code available in a newer post, please
go to &lt;a href=&quot;&#x2F;2010&#x2F;magic-properties-on-google-appengine&#x2F;&quot;&gt;Magic Properties on Google AppEngine&lt;&#x2F;a&gt; for the updated information.
For the generic information that has not changed, and the idea behind the code
please continue below.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;In Google AppEngine a model may contain many different properties, from this
model you can generate a form using djangoforms. This is all good and well
until you have a required property on a model that needs to have part of it be
calculated based upon something the user types in. For example, a blog entry
generally has a &quot;slug&quot; which is generated from the title, now in Google
AppEngine&#x27;s models, if a property is required it has to be set at object
creation time.&lt;&#x2F;p&gt;
&lt;p&gt;Djangoforms has a &lt;code&gt;form.save(committed=False)&lt;&#x2F;code&gt; that allows one to get back an
unsaved &lt;code&gt;Model&lt;&#x2F;code&gt;, however since slug is not in the form (since it is auto
generated) it cannot be set at model creation time. Even adding a hidden field
to the form with the name of the property is not going to satisfy the model
since a value of &lt;code&gt;None&lt;&#x2F;code&gt; is considered to be empty, which means it does not meet
the &lt;code&gt;required=True&lt;&#x2F;code&gt; status.&lt;&#x2F;p&gt;
&lt;p&gt;After trying a multitude of different options I ran across an &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;code.google.com&#x2F;appengine&#x2F;articles&#x2F;extending_models.html&quot;&gt;article about
extending models&lt;&#x2F;a&gt; that specifically talked about adding custom properties
that were saved into the DB, and could be searched on, this got me digging and
I found &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;googleappengine.blogspot.com&#x2F;2009&#x2F;07&#x2F;writing-custom-property-classes.html&quot;&gt;a blog post on the Google AppEngine blogspot&lt;&#x2F;a&gt; about the exact same
thing, using yet again a different example. The one that got me on the right
track was the following post by &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20090517114229&#x2F;http:&#x2F;&#x2F;appengine-cookbook.appspot.com&#x2F;recipe&#x2F;custom-model-properties-are-cute&#x2F;&quot;&gt;Rodrigo Moraes on his custom model
properties&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I came up with the following fairly quickly after also taking a look at
&lt;code&gt;googleappengine.ext.db&lt;&#x2F;code&gt; within the Google AppEngine framework, however there is
still an issue, for now the &lt;code&gt;MagicProperty&lt;&#x2F;code&gt; is only updated the first time the
property it affects is set. I am not yet sure how to work around that, I will
have to do some more digging, maybe there is a way to get notified if another
property gets modified or updated.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; logging&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; google.appengine.ext&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; db&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; MagicProperty&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;prop&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; magic_func=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *args&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **kw&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; magic_func:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # No pants required.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt; _MagicProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;kw&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # We are putting some pants on the function.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; pants&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt; _MagicProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;kw&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; pants&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFCB8B;&quot;&gt; _MagicProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;Property&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;MagicProperty which will modify output based on a function that it is given.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    Inspired by: &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    http:&#x2F;&#x2F;appengine-cookbook.appspot.com&#x2F;recipe&#x2F;custom-model-properties-are-cute&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    http:&#x2F;&#x2F;code.google.com&#x2F;appengine&#x2F;articles&#x2F;extending_models.html&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    http:&#x2F;&#x2F;googleappengine.blogspot.com&#x2F;2009&#x2F;07&#x2F;writing-custom-property-classes.html&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; prop&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; magic_func&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *args&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **kw&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;        super&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;_MagicProperty&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;kw&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.magic_func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; magic_func&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.magic_prop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; prop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; attr_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # In google.appengine.ex.db there is an explicit warning not to use this method, so we test for it first.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;._attr_name:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;_attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __get__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; model_instance&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; is&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; TODO&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;: If the property that this one is functioning on is changed, we need a way to know that so that we recalculate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;        # Original __get__ in google.appengine.ext.db has getattr to retrieve the value from the model instance&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        magic_done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; getattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; magic_done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; is&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            magic_done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;magic_func&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.magic_prop.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;__get__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; class_instance&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;            # Set the attribute in the model&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;            setattr&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;model_instance&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8EACE3;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;_attr_name&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; magic_done&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; magic_done&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt; __set__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        raise&lt;&#x2F;span&gt;&lt;span&gt; db.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;DerivedPropertyError&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;MagicProperty is magic. Magic may not be modified.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The approach that the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20100208035715&#x2F;http:&#x2F;&#x2F;code.google.com&#x2F;appengine&#x2F;docs&#x2F;python&#x2F;datastore&#x2F;typesandpropertyclasses.html#DateTimeProperty&quot;&gt;DateTimeProperty&lt;&#x2F;a&gt; takes is one that I can&#x27;t use
because my class is dependant upon an external property, not whether or not a
value was set when the object was instantiated.&lt;&#x2F;p&gt;
&lt;p&gt;Google AppEngine is posing some rather interesting challenges. Although, for
slugs it may not be entirely bad that it can only be set once, as that means
title changes on the article won&#x27;t cause the slug to change possibly breaking
old links. This definitely requires more research and more thinking.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Property class on AppEngine does not support a tuple for choices</title>
        <published>2010-02-15T04:56:59+00:00</published>
        <updated>2010-02-15T04:56:59+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2010/property-class-on-google-appengine-not-supporting-a-tuple-of-choices/"/>
        <id>https://funcptr.net/2010/property-class-on-google-appengine-not-supporting-a-tuple-of-choices/</id>
        
        <content type="html" xml:base="https://funcptr.net/2010/property-class-on-google-appengine-not-supporting-a-tuple-of-choices/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;code.google.com&#x2F;appengine&#x2F;&quot;&gt;Google AppEngine&lt;&#x2F;a&gt; does not allow one to set the choices field of a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20100212221437&#x2F;http:&#x2F;&#x2F;code.google.com&#x2F;appengine&#x2F;docs&#x2F;python&#x2F;datastore&#x2F;propertyclass.html#Property&quot;&gt;model
property&lt;&#x2F;a&gt;&lt;&#x2F;a&gt; to a tuple; this workaround will have have a form give the
user the choices with the user friendly items but have the backend store the
non-user friendly version.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;# Set up our non-user friendly and user friendly choices&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;REDIRECT_CHOICES&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;301&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;301 - Permanent Redirect&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;302&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;302 - Temporary Redirect&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;403&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;403 - Access Denied&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;404&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;404 - File Not Found&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;500 - Internal Server Error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;# Sample model that has the required Property with the choices set to the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;# non-user friendly values&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFCB8B;&quot;&gt; Shorturl&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    uripath&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; db.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;StringProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;verbose_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;Path&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt; required&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    httpcode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; db.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;IntegerProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        verbose_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;HTTP code&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        required&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        default&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;301&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        choices&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; REDIRECT_CHOICES&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    location&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; db.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;StringProperty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;verbose_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;Location&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;# This is our djangoform that has the choices set as required so that the user&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;# gets the user friendly version and not just simply the numbers.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFCB8B;&quot;&gt; ShorturlForm&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;djangoforms&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;ModelForm&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    httpcode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; forms.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;IntegerField&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        widget&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;forms.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B2CCD6;&quot;&gt;Select&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;choices&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;REDIRECT_CHOICES&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        required&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF5874;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;        label&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;HTTP code&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFCB8B;&quot;&gt; Meta&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        model&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Shorturl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Given the above tuple it will set the choices in the model for Google AppEngine
and pass it to the field on the Django form allowing user friendly output. This
will allow the user to pick the right redirect choice without having to know
that various different redirect codes or have to look them up.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>addnumbers.asm updated</title>
        <published>2008-02-03T09:59:04+00:00</published>
        <updated>2008-02-03T09:59:04+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2008/addnumbers-asm-updated/"/>
        <id>https://funcptr.net/2008/addnumbers-asm-updated/</id>
        
        <content type="html" xml:base="https://funcptr.net/2008/addnumbers-asm-updated/">&lt;p&gt;If you have no clue what I am talking about, check out &lt;a href=&quot;&#x2F;2008&#x2F;introduction-to-intel-assembly&#x2F;&quot;&gt;my previous post&lt;&#x2F;a&gt;.
I have fixed the flaws, and now it will parse the commands it is handed on
the command line, and stick them into an integer. It will now print it to
stdout.&lt;&#x2F;p&gt;
&lt;p&gt;This has been an awesome learning experience for me. Especially with regards
to how to do recursion, how to debug a pure assembly program with gdb and
whatnot.&lt;&#x2F;p&gt;
&lt;p&gt;Here comes the code, as I said before, see my previous thread if you don&#x27;t
know what I am talking about, as well as instructions on how to compile the
program.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; File: addnumbers2.asm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Delta Regeer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; 2008-01-27&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Function:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	Add numbers together that are provided as arguments to the program in argv[1] and argv[2].&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Known limitations:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	This will hopefully be fixed in the next revision. Floating point numbers will not work.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	Any input that is larger than an integer will cause overflows, and thus will not work.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;section .data&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Define some strings that are going to be used throughout the program&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This string is to let the user know they failed to provide the proper amount of arguments.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;args	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;	&amp;quot;Program &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;addnumbers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0xa&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x9&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;addnumbers &amp;lt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &amp;lt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0xa&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x9&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;Arguments &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; and&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt; are required.&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0xa&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x9&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;Anything that will cause addition to overflow an &lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;147&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;483&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;647&lt;&#x2F;span&gt;&lt;span&gt;), will fail! :P&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0xa&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;largs	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;equ&lt;&#x2F;span&gt;&lt;span&gt;	$ - args&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This string contains part of the output that we are going to send to the terminal. The last two&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; bytes will be filled automatically by the program, before it is output to stdout.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;msg	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;	&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;Answer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;,	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lmsg	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;equ&lt;&#x2F;span&gt;&lt;span&gt;	$ - msg&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;num1	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;num2	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;section .bss&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This is where I am going to store the output of my conversion from an integer to a char&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;answer	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;resb&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	64&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;section .text&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;global start				&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Linker defined entry point. Mac OS X this is start. &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;global _start				&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; FreeBSD and others _start.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;_start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; 		; Set up the stack frame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Get argc, we check if it set to at least 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Put the base pointer into edx, so we can use that in &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;					; our dereferences coming up&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Add 8. We want to skip ebp and argc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	cmp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Check if we have at least 3 arguments to the program. &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;					; At least two arguments are required, and the 3rd one is &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;					; the name of the program&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jl&lt;&#x2F;span&gt;&lt;span&gt;	exit			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; If the value in ecx is less than 3, jump to exit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Set the index to 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;edx&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esi&lt;&#x2F;span&gt;&lt;span&gt; * &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the pointer to the character array into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push eax onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span&gt;	num1			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the pointer to num1 onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	call&lt;&#x2F;span&gt;&lt;span&gt;	ctoi			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Call my char to int function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Put the stack pointer back to where it was.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	inc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esi&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Increase the index&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;edx&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esi&lt;&#x2F;span&gt;&lt;span&gt; * &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the pointer to the character array into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push eax onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span&gt;	num2			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the pointer to num2 onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	call&lt;&#x2F;span&gt;&lt;span&gt;	ctoi			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Call my char to int function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Put the stack pointer back to where it was.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [num1]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move value stored in num1 into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [num2]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Add num2 to eax, this will now be stored in eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the new calculated number onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	call&lt;&#x2F;span&gt;&lt;span&gt;	itoa			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Convert the integer to a character array&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; lmsg		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the length of the string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span&gt;	msg			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the location of the string in memory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the file descriptor to write to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Clean up the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span&gt;	answer			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push answer onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	call&lt;&#x2F;span&gt;&lt;span&gt;	len			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Get it&amp;#39;s length&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edi&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the length onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span&gt;	answer			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the pointer to the character string onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the file descriptor to write to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Clean up the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jmp&lt;&#x2F;span&gt;&lt;span&gt;	done			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Program is done. Jump to done&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This label is jumped to when we want to exit the program and let the user know how&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; to run the program. Like for instance what paramaters to send the program.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; Call sys_write&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; largs		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the length of the string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; args		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the location of the string in memory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the file descriptor to write to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Clean up the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This is the label we jump to when we want to exit the program, we set the exit code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; to 0.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; Call sys_exit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the value to return to the operating system&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; We never return to this function, so no need to clean the stack.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;ctoi&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; char to i. We actually convert entire character array&amp;#39;s to integers.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; We get two paramaters on the stack. The first one we grab is the pointer to the place to store&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; the number. The second is the pointer to the character array.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the old base pointer onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Create a new base pointer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esi&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Store all the original registers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push edx, so that we can overwrite it&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; We get another storage space on the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; This is the number we are going to multiply by&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the pointer to the character array into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the pointer to the character array onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	call&lt;&#x2F;span&gt;&lt;span&gt;	len			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Call the string length versoin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Reclaim the space we lost when we pushed eax onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; This is where we are going to store the numbers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esi&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; This is the pointer to the character array&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	movzx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;di&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; move with extended zero edi.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Clean up edi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;	ctoi_loop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the value stored in ebx into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mul&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move it over a 10s place.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Move the new number back into ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	movzx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esi&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;edi&lt;&#x2F;span&gt;&lt;span&gt;]	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the character into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	movsx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;al&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; We just want the lower part of the character&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x30&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Subtract 0x30, ASCII 0 so that it is an actual number&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span&gt;	[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Add the new number to the old number that has been multiplied by 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	inc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edi&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Increase the counter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	loop&lt;&#x2F;span&gt;&lt;span&gt; ctoi_loop			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Loop into cx is 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Restore all the registers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Make esp the original base pointer again&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Pop the original base pointer into the register&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	ret&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; Return caller&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;itoa&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Recursive function. This is going to convert the integer to the character.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Setup a new stack frame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Save the registers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; eax is going to contain the integer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; This is our &amp;quot;stop&amp;quot; value as well as our value to divide with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span&gt;, answer		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Put a pointer to answer into ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push ebx on the field for our &amp;quot;stop&amp;quot; value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;	itoa_loop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	cmp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Compare eax, and ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jl&lt;&#x2F;span&gt;&lt;span&gt;	itoa_unroll		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Jump if eax is less than ebx (which is 10)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	xor&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;edx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Clear edx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	div&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Divide by ebx (10)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the remainder onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jmp&lt;&#x2F;span&gt;&lt;span&gt;	itoa_loop		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Jump back to the top of the loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;	itoa_unroll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	al&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x30&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Add 0x30 to the bottom part of eax to make it an ASCII char&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; al&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Move the ASCII char into the memory references by ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	inc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Increment ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Pop the next variable from the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	cmp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Compare if eax is ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jne&lt;&#x2F;span&gt;&lt;span&gt;	itoa_unroll		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; If they are not equal, we jump back to the unroll loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; else we are done, and we execute the next few commands&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0xa&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Add a newline character to the end of the character array&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	inc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Increment ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Add a null byte to ecx, so that when we pass it to our&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; len function it will properly give us a length&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Restore registers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	ret&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Returns the length of a string. The string has to be null terminated. Otherwise this function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; will fail miserably. &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Upon return. edi will contain the length of the string.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Save the previous stack pointer. We restore it on return&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;esp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; We setup a new stack frame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Save registers we are going to use. edi returns the length of the string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; 	ecx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span&gt;,  [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;]		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the pointer to eax; we want an offset of one, to jump over the return address&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	edi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Set the counter to 0. We are going to increment this each loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;	len_loop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Just a quick label to jump to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	movzx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;edi&lt;&#x2F;span&gt;&lt;span&gt;]	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the character to eax.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	movsx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;al&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move al to eax. al is part of eax.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	inc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	di&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Increase di.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	cmp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Compare eax to 0.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jnz&lt;&#x2F;span&gt;&lt;span&gt; 	len_loop		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; If it is not zero, we jump back to len_loop and repeat.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	dec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	di&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Remove one from the count&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Restore registers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Set esp back to what ebp used to be.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Restore the stack frame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	ret&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; Return to caller&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Introduction to Intel Assembly</title>
        <published>2008-01-27T03:42:44+00:00</published>
        <updated>2008-01-27T03:42:44+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2008/introduction-to-intel-assembly/"/>
        <id>https://funcptr.net/2008/introduction-to-intel-assembly/</id>
        
        <content type="html" xml:base="https://funcptr.net/2008/introduction-to-intel-assembly/">&lt;p&gt;This semester I am taking a class on Intel assembly, because I want more of an
insight into how the computer works, and it will allow me to better reverse
engineer new viruses and spyware. The class is also required if one is a
Software Engineering major, so that means I have to take it.&lt;&#x2F;p&gt;
&lt;p&gt;We had our first assignment. Sum two numbers and then output them to the
screen. We were supposed to write inline assembly using Visual Studio C++,
but if we are to do an assembly class, then we should learn how to write
assembly, not have some parts assembly and other parts the compiler. Sure
it makes it easy as you will get immediate access to the standard C
library, but if you want that, you can just link against it.&lt;&#x2F;p&gt;
&lt;p&gt;The following code examples were written on Mac OS X, and will work on
FreeBSD. Linux uses a different calling convention for its syscalls, and as
such this code will not run on Linux, unless it is modified. Do note, you
need an Intel Mac for this to work. This is Intel assembly.&lt;&#x2F;p&gt;
&lt;p&gt;Compile the code with (Mac OS X):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;nasm&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; macho addnumbers.asm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;ld&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; addnumbers addnumbers.o&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;or (FreeBSD):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;nasm&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; elf addnumbers.asm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;ld&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; addnumbers addnumbers.o&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then you can run it with:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;.&#x2F;addnumbers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see in the comments of the source code, there are still some
limitations, but the rest of the source code should be made readable by the
comments that are provided.&lt;&#x2F;p&gt;
&lt;p&gt;Porting to Linux: Please double check that all the syscall numbers are the
same. There are some differences between Linux and FreeBSD&#x2F;Mac OS X in that
regard.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; File: addnumbers.asm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Delta Regeer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; 2008-01-27&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Function:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	Add numbers together that are provided as arguments to the program in argv[1] and argv[2].&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Known limitations:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	As of right now, the numbers that are provided may not add up to anything more than 9.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	This will hopefully be fixed in the next revision. Floating point numbers will not work.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Todo:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	Write conversion routine, to convert a string of numbers into a real integer on which&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;;	math may be performed.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;section .data&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Define some strings that are going to be used throughout the program&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This string is to let the user know they failed to provide the proper amount of arguments.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;args	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;	&amp;quot;You failed to provide the proper amount of arguments&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0xa&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;largs	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;equ&lt;&#x2F;span&gt;&lt;span&gt;	$ - args&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; This string contains part of the output that we are going to send to the terminal. The last two&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; bytes will be filled automatically by the program, before it is output to stdout.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;msg	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;	&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;Answer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;,	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lmsg	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;equ&lt;&#x2F;span&gt;&lt;span&gt;	$ - msg&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;section .text&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;global start				&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Linker defined entry point. Mac OS X this is start. FreeBSD and others _start.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;global _start&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;_start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Start the program here.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; We don&amp;#39;t care about argc or argv[0]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; 	ecx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Get the first argument or argv[1]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jecxz&lt;&#x2F;span&gt;&lt;span&gt;	exit			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; If there was no argument. Exit. Let the user know why&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Change the number from a character to an actual dword&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dword&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;]	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the character into eax so we can manipulate it&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x30&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Remove 0x30 from the character. To make it an actual number, not an ASCII number.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	pop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ecx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; get the second argument or argv[2]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jecxz&lt;&#x2F;span&gt;&lt;span&gt;	exit			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; If there was no second argument. Exit. Let the user know why&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dword&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ecx&lt;&#x2F;span&gt;&lt;span&gt;]	&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Move the character into ebx so we can manipulate it&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	ebx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x30&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Remove 0x30 from the character. To make it an actual number, not an ASCII number.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;ebx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Add the two numbers together&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0x30&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Make it an ASCII number again&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[msg+lmsg-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Replace the null character in the msg with the answer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span&gt;	[msg+lmsg-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0xa&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; Add an newline character so that when it spits it out it is neatly formatted&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;				&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; Call sys_write&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; lmsg		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the length of the string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span&gt;	msg			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the location of the string in memory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the file descriptor to write to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Advance the stack pointer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	jmp&lt;&#x2F;span&gt;&lt;span&gt;	done			&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Program is done. Jump to done&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; Call sys_write&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; largs		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the length of the string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span&gt; args		&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;; Push the location of the string in memory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the file descriptor to write to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	esp&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;byte&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Advance esp past the part we were just at&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;				; sys_exit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;	dword&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0x1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;		; Push the value to return to the operating system&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	mov&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Move the syscall number into eax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	push&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;	eax&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Push the syscall onto the stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;	int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;	0x80&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;			; Interrupt 80, go to kernel&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;	; We never return to this function, so no need to clean the stack.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>X11 mouse button pressed</title>
        <published>2007-03-16T04:51:34+00:00</published>
        <updated>2007-03-16T04:51:34+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2007/x11-mouse-button-pressed/"/>
        <id>https://funcptr.net/2007/x11-mouse-button-pressed/</id>
        
        <content type="html" xml:base="https://funcptr.net/2007/x11-mouse-button-pressed/">&lt;p&gt;For a Rube Goldberg device I needed a way to know if a mouse button was
pressed on the root window of an X11 screen. Here is the code for it:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;X11&#x2F;Xlib.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;X11&#x2F;Xutil.h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Display &lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;dsp &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; XOpenDisplay&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;( NULL )&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; !&lt;&#x2F;span&gt;&lt;span&gt;dsp ){&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        int&lt;&#x2F;span&gt;&lt;span&gt; screenNumber &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; DefaultScreen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(dsp)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        long&lt;&#x2F;span&gt;&lt;span&gt; eventMask &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; StructureNotifyMask;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        XSelectInput&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;( dsp,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; DefaultRootWindow&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(dsp), eventMask )&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        XEvent evt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        eventMask &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; ButtonPressMask&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;ButtonReleaseMask;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;        XSelectInput&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(dsp,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; DefaultRootWindow&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(dsp), eventMask)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; &#x2F;&#x2F; override prev&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;        do&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;                XNextEvent&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;( dsp,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;evt )&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;   &#x2F;&#x2F; calls XFlush()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;                if&lt;&#x2F;span&gt;&lt;span&gt; ( (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;evt&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; ButtonRelease)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ||&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;evt&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C5E478;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; ButtonPress) )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;                exit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt; while&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt; );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Compile:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;gcc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -lX11 -L&#x2F;usr&#x2F;local&#x2F;include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; x11.c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; x11-mousebutton&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Upon a mouse button being pressed it will exit 2, so using that return value
in some script you can execute a command. For example, to open a CD rom
drive so a domino falls over.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
