ArticleslgStudy

computer science

Polyglot (computing)

Polyglot (computing) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Polyglot (computing) rather than just read about it. In short: In computing, a polyglot is a computer program or script (or other file) written in a valid form of multiple programming languages or file formats. The name was coined by analogy to multilingualism.

Key takeaways

  • Polyglot (computing) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Polyglot (computing) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Polyglot (computing) from memory before moving on to harder problems.

Reference excerpt

In computing, a polyglot is a computer program or script (or other file) written in a valid form of multiple programming languages or file formats. The name was coined by analogy to multilingualism. A polyglot file is composed by combining syntax from two or more different formats. When the file formats are to be compiled or interpreted as source code, the file can be said to be a polyglot program, though file formats and source code syntax are both fundamentally streams of bytes, and exploiting this commonality is key to the development of polyglots. Polyglot files have limited practical applications in compatibility, but can also present a security risk when used to bypass validation or to exploit a vulnerability.

History

Polyglot programs have been crafted as challenges and curios in hacker culture since at least the early 1990s. A notable early example, named simply polyglot was published on the Usenet group rec.puzzles in 1991, supporting eight languages, though this was inspired by even earlier programs. In 2000, a polyglot program was named a winner in the International Obfuscated C Code Contest. In the 21st century, polyglot programs and files gained attention as a covert channel mechanism for propagation of malware. Polyglot files have limited practical applications in compatibility.

Construction A polyglot is composed by combining syntax from two or more different formats, leveraging various syntactic constructs that are either common between the formats, or constructs that are language specific but carrying different meaning in each language. A file is a valid polyglot if it can be successfully interpreted by multiple interpreting programs. For example, a PDF-Zip polyglot might be opened as both a valid PDF document and decompressed as a valid zip archive. To maintain validity across interpreting programs, one must ensure that constructs specific to one interpreter are not interpreted by another, and vice versa. This is often accomplished by hiding language-specific constructs in segments interpreted as comments or plain text of the other format.

Examples

C, PHP, and Bash Two commonly used techniques for constructing a polyglot program are to make use of languages that use different characters for comments, and to redefine various tokens as others in different languages. These are demonstrated in this public domain polyglot written in ANSI C, PHP and bash:

Note the following:

A hash sign marks a preprocessor statement in C, but is a comment in both bash and PHP. "//" is a comment in both PHP and C and the root directory in bash. Shell redirection is used to eliminate undesirable outputs. Even on commented out lines, the "<?php" and "?>" PHP indicators still have effect. The statement "function main()" is valid in both PHP and bash; C #defines are used to convert it into "int main(void)" at compile time. Comment indicators can be combined to perform various operations. "if (($x))" is a valid statement in both bash and PHP. printf is a bash shell builtin which is identical to the C printf except for its omission of brackets (which the C preprocessor adds if this is compiled with a C compiler). The final three lines are only used by bash, to call the main function. In PHP the main function is defined but not called and in C there is no need to explicitly call the main function.

SNOBOL4, Win32Forth, PureBasicv4.x, and REBOL The following is written simultaneously in SNOBOL4, Win32Forth, PureBasicv4.x, and REBOL:

MS-DOS batch file and Perl The following file runs as an MS-DOS batch file, then re-runs itself in Perl:

This allows creating Perl scripts that can be run on MS-DOS systems with minimal effort. Note that there is no requirement for a file to perform exactly the same function in the different interpreters.

JavaScript and Python This example makes use of the difference between String.prototype.replace in JavaScript and str.replace in Python.

Types

Polyglot types include:

stacks, where multiple files are concatenated with each other parasites where a secondary file format is hidden within comment fields in a primary file format zippers where two files are mutually arranged within each others' comments cavities, where a secondary file format is hidden within null-padded areas of the primary file.

Benefits

Polyglot markup Polyglot markup has been proposed as a useful combination of the benefits of HTML5 and XHTML. Such documents can be parsed as either HTML (which is SGML-compatible) or XML, and will produce the same DOM structure either way. For example, in order for an HTML5 document to meet these criteria, the two requirements are that it must have an HTML5 doctype, and be written in well-formed XHTML. The same document can then be served as either HTML or XHTML, depending on browser support and MIME type. As expressed by the html-polyglot recommendation, to write a polyglot HTML5 document, the following key points should be observed:

Processing instructions and the XML declaration are both forbidden in polyglot markup Specifying a document’s character encoding The DOCTYPE Namespaces Element syntax (i.e. End tags are not optional. Use self-closing tags for void elements.) Element content Text (i.e. pre and textarea should not start with newline character) Attributes (i.e. Values must be quoted) Named entity references (i.e. Only amp, lt, gt, apos, quot) Comments (i.e. Use <!-- syntax -->) Scripting and styling polyglot markup The most basic possible polyglot markup document would therefore look like this:

In a polyglot markup document non-void elements (such as script, p, div) cannot be self-closing even if they are empty, as this is not valid HTML. For example, to add an empty textarea to a page, one cannot use <textarea/>, but has to use <textarea></textarea> instead.

Composing formats The DICOM medical imaging format was designed to allow polyglotting with TIFF files, allowing efficient storage of the same image data in a file that can be interpreted by either DICOM or TIFF viewers.

Compatibility The Python 2 and Python 3 programming languages were not designed to be compatible with each other, but there is sufficient commonality of syntax that a polyglot Python program can be written than runs in both versions.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Polyglot (computing)

Start with the simplest possible case. Write down what Polyglot (computing) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Polyglot (computing) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Polyglot (computing) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Polyglot (computing)

In research
Polyglot (computing) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Polyglot (computing) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Polyglot (computing) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer file formats, Computer programming, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for Polyglot (computing) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Polyglot (computing) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Polyglot (computing) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Polyglot (computing) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Polyglot (computing) in simple terms?

In computing, a polyglot is a computer program or script (or other file) written in a valid form of multiple programming languages or file formats. The name was coined by analogy to multilingualism.

Why does Polyglot (computing) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Polyglot (computing)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Polyglot (computing).

Tags

  • Computer file formats
  • Computer programming
  • Source code
  • Steganography

Keep exploring