·6 min read
Georgian text turned into mojibake, and I repaired it with a reversed cp1252 table
My Georgian task titles on the kanban board had turned into strings like დღე, and no amount of squinting at the PowerShell scripts would tell me which run had done it. The text was not lost. Every byte was still in the file, just decoded by the wrong table and then saved again as if it were correct. So instead of hunting the writer, I wrote a function that reads the damage backwards.
- Project
- DailyBrief
- Date
- Aug 2026
- Stack
- Electron, Node.js, PowerShell 5.1
- Commit
- 5a3f7b2
What the damage looks like
DailyBrief is an Electron shell around PowerShell scripts that call Claude and write JSON files. The app is bilingual in practice: I write task titles in Georgian. An earlier version of those scripts was less careful about encoding. It read UTF-8 files with a cp1252 assumption and wrote them back as UTF-8, which is the classic mojibake path.
Georgian letters live in the range U+10D0 to U+10FF. In UTF-8 each one is three bytes, starting with 0xE1, then a byte in the 0x83 or 0x82 neighbourhood, then a continuation byte. I checked the real bytes for the word დღე:
e18393 e183a6 e18394Decode those bytes as Windows-1252 and you get დღე. The middle byte 0x83 becomes ƒ (U+0192), and 0x93 becomes a curly quote. Write that string out as UTF-8 and the original information is now spread over twice as many characters, but nothing was dropped.
Why Latin-1 is not enough
My first instinct was the standard trick: take each character's code point as a byte (Latin-1), then decode as UTF-8. It fails here. Windows-1252 differs from Latin-1 in exactly one region, bytes 0x80 to 0x9F, where cp1252 puts printable characters like ƒ, “ and € instead of control codes. Those are the bytes Georgian UTF-8 is full of. A code point such as U+0192 is above 0xFF, so a Latin-1 mapping cannot turn it back into a byte at all.
The fix needs cp1252's own table, reversed. This is the table from src/text.js, which started life in main.js in this commit:
const CP1252_REVERSE = {
0x20AC: 0x80, 0x201A: 0x82, 0x0192: 0x83, 0x201E: 0x84, 0x2026: 0x85,
0x2020: 0x86, 0x2021: 0x87, 0x02C6: 0x88, 0x2030: 0x89, 0x0160: 0x8A,
0x2039: 0x8B, 0x0152: 0x8C, 0x017D: 0x8E, 0x2018: 0x91, 0x2019: 0x92,
0x201C: 0x93, 0x201D: 0x94, 0x2022: 0x95, 0x2013: 0x96, 0x2014: 0x97,
0x02DC: 0x98, 0x2122: 0x99, 0x0161: 0x9A, 0x203A: 0x9B, 0x0153: 0x9C,
0x017E: 0x9E, 0x0178: 0x9F
};
function cp1252Bytes(s) {
const out = Buffer.alloc(s.length);
for (let i = 0; i < s.length; i++) {
const c = s.charCodeAt(i);
const b = c <= 0xFF ? c : CP1252_REVERSE[c];
if (b === undefined) return null;
out[i] = b;
}
return out;
}Note what is missing: 0x81, 0x8D, 0x8F, 0x90 and 0x9D. Windows-1252 leaves those undefined, and that matters in a moment.
Not breaking text that was fine
A repair function that runs on every string in every file must not touch healthy text. Three guards in fixMojibake handle that.
First, it only looks at runs of two or more non-ASCII characters (/[^\x00-\x7F]{2,}/g), so plain English is never inspected. Second, if a run already contains a Georgian letter, it is returned as is. Third, if any character in the run cannot be mapped back to a cp1252 byte, the run is not mojibake, so it is left alone.
The last check is the one I like most. After reversing, the bytes are decoded as UTF-8, and the result is accepted only if it is shorter than the input and has no replacement character inside it:
let fixed = bytes.toString('utf8');
fixed = fixed.replace(/\uFFFD+$/, '');
return (fixed.length < run.length && fixed.indexOf('\uFFFD') === -1) ? fixed : run;Shorter output is the proof. Real mojibake of a three-byte letter is three characters long and decodes to one. If the decode does not shrink the run, whatever was there was never UTF-8 to begin with.
The dropped byte
The comment in the code records something I would not have guessed: the old corrupter did not only misdecode, it also dropped bytes it could not map, for example 0x90. That leaves an incomplete UTF-8 sequence at the end of a run, and decoding it yields a trailing U+FFFD. The code strips trailing replacement characters and accepts that the last letter of such a string is unrecoverable. Interior replacement characters mean the run is real garbage, and it is returned untouched. I decided that losing one final letter was better than keeping a whole mangled word.
Healing on read
The repair lives where the data is read, not in a one-off migration. readJson strips the byte order mark that PowerShell 5.1 writes, parses, and walks the object with deepFixMojibake. That walker returns the same object reference when nothing changed, so a caller can detect a repair with fixed !== obj and write the file back only then:
const fixed = deepFixMojibake(obj);
if (fixed !== obj) {
try { fs.writeFileSync(file, JSON.stringify(fixed, null, 2), 'utf8'); } catch (e) { /* repair still returned below */ }
}The write-back matters because the PowerShell scripts read the same files. If only the renderer saw clean text, the next run would feed the model garbage again. The same call exists for the SQLite key-value store: a value is repaired and saved back with kvSet when it changed.
Testing it
I did not want to trust a table I typed by hand, so the test file builds the corruption independently, with a second copy of the mapping going the forward direction, and asserts the round trip on დღიური ბრიფინგი. It also checks that clean Georgian, plain ASCII, an empty string and a single character come back untouched, and that the no-change case returns the identical reference. When I ran the function on the corrupted form of დღე in Node just now, it printed დღე going in and დღე coming out.
What I took from it
Encoding bugs are cheap to fix at the source and expensive to fix afterwards, but the data from before the fix still has to live somewhere. Because the damage was reversible, a small deterministic function was enough. The real work was making it conservative: only touch runs that provably decode as UTF-8, and only report a change when one happened. A repair that sometimes damages good text is worse than the bug it fixes.
#encoding#utf-8#electron#powershell#georgian
Keep reading
Have a project in mind?