#!/usr/bin/python
#
# Copyright 2015 Robert Kooima
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

#-------------------------------------------------------------------------------
# To encode "message" in the text of input.txt, producing output.txt
#
#     whitemark message < input.txt > output.txt
#
# To recover the message from output.txt
#
#     whitemark < output.txt

import sys

eol  = chr(10)
bit0 = chr(32)
bit1 = chr(160)

# Encode the given character string to a bit string of 8-bit values in
# 10-bit frames with a start bit of 1 and a stop bit of 0.

def tobits(data):
	bits = "";

	for c in data:
		b = ord(c);
		bits += bit1
		bits += bit1 if b & 128 else bit0
		bits += bit1 if b &  64 else bit0
		bits += bit1 if b &  32 else bit0
		bits += bit1 if b &  16 else bit0
		bits += bit1 if b &   8 else bit0
		bits += bit1 if b &   4 else bit0
		bits += bit1 if b &   2 else bit0
		bits += bit1 if b &   1 else bit0
		bits += bit0

	return bits

# Decode the given bit string assuming an 8-bit encoding within 10-bit frames.

def frombits(bits):
	data = ""

	for i in range(0, len(bits), 10):
		frame = bits[i:i+10]

		b  = 128 if len(frame) > 1 and frame[1] == bit1 else 0
		b +=  64 if len(frame) > 2 and frame[2] == bit1 else 0
		b +=  32 if len(frame) > 3 and frame[3] == bit1 else 0
		b +=  16 if len(frame) > 4 and frame[4] == bit1 else 0
		b +=   8 if len(frame) > 5 and frame[5] == bit1 else 0
		b +=   4 if len(frame) > 6 and frame[6] == bit1 else 0
		b +=   2 if len(frame) > 7 and frame[7] == bit1 else 0
		b +=   1 if len(frame) > 8 and frame[8] == bit1 else 0

		data += chr(b);

	return data;

# Separate the given bit string into 10-bit frames and confirm that each
# begins with a 1 start bit and ends with a 0 stop bit. This will indicate
# that the bit stream is synchronized for decoding.

def issynced(bits):
	for i in range(0, len(bits), 10):
		frame = bits[i:i+10]

		if len(frame) > 0 and bits[i + 0] != bit1:
			return False
		if len(frame) > 9 and bits[i + 9] != bit0:
			return False

	return True

# Decode the bit string embedded in the white space of stdin, recognizing
# char 32 as 0 and char 160 as 1. Ignore indentation whitespace.

def decode():
	text = False
	bits = ""

	while True:
		c = sys.stdin.read(1)

		if len(c) == 0:
			break

		elif text and c == bit0:
			bits += bit0
		elif text and c == bit1:
			bits += bit1
		elif text and c == eol:
			text = False
		elif not c.isspace():
			text = True

	return bits

# Copy stdin to stdout, encoding the given bit string in the white space.
# Encode 0 as char 32 and 1 as char 160.

def encode(bits):
	text = False
	i = 0;

	while True:
		c = sys.stdin.read(1)

		if len(c) == 0:
			break;

		elif text and c == bit0:
			c = bits[i % len(bits)]
			i += 1;
		elif text and c == bit1:
			c = bits[i % len(bits)]
			i += 1;
		elif text and c == eol:
			text = False
		elif not c.isspace():
			text = True

		sys.stdout.write(c)

if len(sys.argv) == 1:
	print frombits(decode())
else:
	encode(tobits(sys.argv[1]))

