| /* |
| * Test app for Rot-47 "encryption". |
| * |
| * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| * Use of this source code is governed by a BSD-style license that can be |
| * found in the LICENSE file. |
| */ |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| |
| /* rot47 is defined over the alphabet ['!'..'~'] */ |
| #define ROT_MIN '!' |
| #define ROT_MAX '~' |
| #define ROT_SIZE (ROT_MAX - ROT_MIN + 1) |
| #define ROT_HALF (ROT_SIZE / 2) |
| |
| static char *rotate_encrypt(const char *key, const char *value) |
| { |
| char *rv = strdup(value); |
| size_t i, len = strlen(rv); |
| |
| for (i = 0; i < len; ++i) { |
| int ch; |
| |
| if (rv[i] < ROT_MIN || rv[i] > ROT_MAX) |
| continue; |
| |
| /* Temporary storage in an int to avoid overflowing a char. */ |
| ch = rv[i] + ROT_HALF; |
| if (ch > ROT_MAX) { |
| rv[i] = ch - ROT_SIZE; |
| } else { |
| rv[i] = ch; |
| } |
| } |
| |
| return rv; |
| } |
| |
| int |
| main(int argc, char *argv[]) |
| { |
| char line[1024]; |
| |
| while (fgets(line, sizeof(line), stdin) != NULL) { |
| char *s = rotate_encrypt(NULL, line); |
| fputs(s, stdout); |
| free(s); |
| } |
| return 0; |
| } |