# encode/v1 — Encode/decode text

Encodes and decodes text in classic ASCII-safe formats, using Go's standard
library (`encoding/base64`, `encoding/base32`, `encoding/hex`, `net/url`,
`html`) plus two hand-rolled algorithms too simple/old to be in any
stdlib: ROT13 and the classic Unix-to-Unix (`uuencode`) encoding. Same
rationale as this API's other modules — an LLM asked to "base64-encode
this" or "decode this ROT13" produces a plausible-looking but frequently
wrong guess; this endpoint returns the actual value.

Supported `format` values: `rot13`, `base64`, `base64url`, `base32`,
`hex`, `uuencode`, `urlcomponent`, `urlform`, `html`.

- `urlcomponent` — RFC 3986 percent-encoding for a URL path/query
  component (`url.PathEscape`/`PathUnescape`): a space becomes `%20`.
- `urlform` — `application/x-www-form-urlencoded`
  (`url.QueryEscape`/`QueryUnescape`): a space becomes `+`. Use this one
  for form bodies and classic query strings, `urlcomponent` for a path
  segment or a component you'll place inside a query string yourself.
- `html` — HTML entity encoding (`html.EscapeString`/`UnescapeString`):
  escapes `< > & ' "`. This is generic text-context escaping, not
  attribute- or script-context-aware — it does not by itself make text
  safe to interpolate into every HTML context (e.g. inside a `<script>`
  block or an unquoted attribute).

### `POST /v1/encode/encode`

**Request**

```json
{ "format": "base64", "input": "hello" }
```

- `format` (required) — one of the formats above.
- `input` (required) — plain UTF-8 text to encode.

**Response**

```json
{ "format": "base64", "output": "aGVsbG8=" }
```

`output` is always plain ASCII.

### `POST /v1/encode/decode`

**Request**

```json
{ "format": "base64", "input": "aGVsbG8=" }
```

**Response**

```json
{ "format": "base64", "output": "hello", "encoding": "text" }
```

`encoding` is `"text"` when the decoded bytes are valid UTF-8 (`output` is
that text), or `"base64"` when they aren't (`output` is then the raw
decoded bytes, re-encoded as base64 — returning them as a plain JSON
string would silently corrupt them, since JSON strings must be UTF-8).

**Error response**

```json
{ "error": "unsupported format \"foo\": expected one of rot13, base64, base64url, base32, hex, uuencode, urlcomponent, urlform, html" }
```

## Notes

- `rot13` is self-inverse — `/encode` and `/decode` do the same thing.
- `uuencode` implements the core 3-bytes-to-4-chars algorithm (45 decoded
  bytes per line, matching Python's `binascii.b2a_uu`/`a2b_uu`), not the
  full file-transfer framing (`begin <mode> <filename>` / `` ` `` / `end`)
  — this endpoint transforms bytes, not files, same as `base64`/`hex`
  below it don't add MIME headers either.
- `html` decoding never errors — an unrecognized entity (`&notarealentity;`)
  is left unchanged rather than rejected, matching browser behavior.

## Out of scope

Base85/ascii85, quoted-printable, and other less common text encodings —
ask if you need one added.

## Files

- `encode.go` — module wiring (routes, HTTP handlers)
- `codec.go` — pure encode/decode logic
- `codec_test.go` — known-vector, round-trip, and error tests
