Nugget
Loading...
Searching...
No Matches
sjis-encode.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
33
34// Unicode -> Shift-JIS encoding, the inverse of support/sjis_conv.cc's decoder.
35// Shares the same data as the forward table (sjis-table.h), inverted into
36// sjis-encode-table.h. Usable from both the host and the MIPS targets.
37
38namespace Sjis {
39
40// Returns the Shift-JIS encoding of a Unicode codepoint, or 0 if it has no
41// mapping. A result <= 0xff is a single byte; otherwise it is two bytes, high
42// byte first. (U+0000 also returns 0, which is harmless for string use.)
43static inline uint16_t unicodeToSjis(uint16_t unicode) {
44 unsigned lo = 0, hi = sizeof(c_unicodeToSjisConvTable) / sizeof(c_unicodeToSjisConvTable[0]);
45 while (lo < hi) {
46 unsigned mid = (lo + hi) / 2;
47 uint16_t u = c_unicodeToSjisConvTable[mid].unicode;
48 if (u == unicode) return c_unicodeToSjisConvTable[mid].sjis;
49 if (u < unicode) {
50 lo = mid + 1;
51 } else {
52 hi = mid;
53 }
54 }
55 return 0;
56}
57
58// utf8Decode() lives in common/util/utf8-decode.h (same Sjis namespace) so the
59// lightweight encoders can share it without pulling in the conversion table.
60
61// Encodes a UTF-8 C string to Shift-JIS. Writes up to dstSize bytes and returns
62// the number written. Codepoints with no Shift-JIS mapping are emitted as '?'.
63// The output is not NUL-terminated.
64static inline uint32_t utf8ToSjis(uint8_t* dst, uint32_t dstSize, const char* src) {
65 uint32_t srcLen = 0;
66 while (src[srcLen]) srcLen++;
67 uint32_t in = 0, out = 0;
68 while (in < srcLen && out < dstSize) {
69 uint16_t cp = utf8Decode(src, srcLen, &in);
70 uint16_t sjis = unicodeToSjis(cp);
71 if (sjis == 0 && cp != 0) sjis = '?';
72 if (sjis > 0xff) {
73 if (out + 2 > dstSize) break;
74 dst[out++] = sjis >> 8;
75 dst[out++] = sjis & 0xff;
76 } else {
77 dst[out++] = sjis & 0xff;
78 }
79 }
80 return out;
81}
82
83// Encodes a UTF-8 string to Shift-JIS using the BIOS save-title convention:
84// printable ASCII is promoted to its fullwidth form (space -> U+3000,
85// 0x21..0x7e -> U+ff01..U+ff5e) so it renders in the manager's fullwidth font,
86// while any non-ASCII codepoint (e.g. Japanese) is encoded directly. This is
87// what memory card save titles want. Writes up to dstSize bytes, returns the
88// number written; output is not NUL-terminated.
89static inline uint32_t utf8ToSjisTitle(uint8_t* dst, uint32_t dstSize, const char* src) {
90 uint32_t srcLen = 0;
91 while (src[srcLen]) srcLen++;
92 uint32_t in = 0, out = 0;
93 while (in < srcLen && out < dstSize) {
94 uint16_t cp = utf8Decode(src, srcLen, &in);
95 if (cp == ' ') {
96 cp = 0x3000;
97 } else if (cp >= 0x21 && cp <= 0x7e) {
98 cp = 0xff00 + (cp - 0x20);
99 }
100 uint16_t sjis = unicodeToSjis(cp);
101 if (sjis == 0 && cp != 0) sjis = '?';
102 if (sjis > 0xff) {
103 if (out + 2 > dstSize) break;
104 dst[out++] = sjis >> 8;
105 dst[out++] = sjis & 0xff;
106 } else {
107 dst[out++] = sjis & 0xff;
108 }
109 }
110 return out;
111}
112
113} // namespace Sjis
int32_t hi
Definition cpu.c:154
int32_t lo
Definition cpu.c:154
uint32_t out
Definition cpu.c:62
char in[50]
Definition memcpy.c:74
Definition sjis-decode.hh:38
uint16_t utf8Decode(const char *str, uint32_t length, uint32_t *index)
Definition utf8-decode.cc:29
uint16_t unicode
Definition sjis-encode-table.h:11
uint16_t sjis
Definition sjis-encode-table.h:12
static const char * src
Definition syscalls.h:80
void uint32_t(classId, spec)