diff options
| author | Charles Forsyth <charles.forsyth@gmail.com> | 2015-04-02 20:04:52 +0100 |
|---|---|---|
| committer | Charles Forsyth <charles.forsyth@gmail.com> | 2015-04-02 20:04:52 +0100 |
| commit | 3df564da98071d088879b6c15e0b02e0e7952668 (patch) | |
| tree | ea78e4cbbf3a0f42a97dadf8ad58c796cf734d8f | |
| parent | 779758698838199ce6b9ebd308692cd5375b02d7 (diff) | |
Rune16 conversion routines because Rune is 32 bits
| -rw-r--r-- | emu/Nt/devarch.c | 69 | ||||
| -rw-r--r-- | emu/Nt/devfs.c | 46 | ||||
| -rw-r--r-- | emu/Nt/ie-os.c | 36 | ||||
| -rw-r--r-- | emu/Nt/os.c | 36 | ||||
| -rw-r--r-- | emu/Nt/r16.c | 46 | ||||
| -rw-r--r-- | emu/Nt/r16.h | 8 | ||||
| -rw-r--r-- | emu/Nt/win.c | 4 |
7 files changed, 118 insertions, 127 deletions
diff --git a/emu/Nt/devarch.c b/emu/Nt/devarch.c index eea152fc..507f9f3f 100644 --- a/emu/Nt/devarch.c +++ b/emu/Nt/devarch.c @@ -13,7 +13,6 @@ #include "error.h" #include "r16.h" - enum{ Qdir, Qarchctl, @@ -38,8 +37,7 @@ struct Value { union { ulong w; vlong q; - Rune utf[1]; /* more allocated as required */ - char data[1]; + char data[1]; /* utf-8 */ }; }; @@ -66,8 +64,7 @@ static struct { static QLock reglock; -extern wchar_t *widen(char*); -static Value* getregistry(HKEY, Rune*, Rune*); +static Value* getregistry(HKEY, Rune16*, Rune16*); static int nprocs(void); static void @@ -78,7 +75,7 @@ archinit(void) v = getregistry(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", L"ProcessorNameString"); if(v != nil){ - snprint(arch.cpu, sizeof(arch.cpu), "%S", v->utf); + snprint(arch.cpu, sizeof(arch.cpu), "%s", v->data); if((p = strrchr(arch.cpu, ' ')) != nil) for(; p >= arch.cpu && *p == ' '; p--) *p = '\0'; @@ -86,7 +83,7 @@ archinit(void) }else{ v = getregistry(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", L"VendorIdentifier"); if(v != nil){ - snprint(arch.cpu, sizeof(arch.cpu), "%S", v->utf); + snprint(arch.cpu, sizeof(arch.cpu), "%s", v->data); free(v); }else snprint(arch.cpu, sizeof(arch.cpu), "unknown"); @@ -104,7 +101,7 @@ nprocs(void) { int n; char *p; - Rune *r; + Rune16 *r; Value *v; n = 0; for(;;){ @@ -207,18 +204,18 @@ archread(Chan* c, void* a, long n, vlong offset) #endif case REG_SZ: case REG_EXPAND_SZ: - if(v->utf[0]) - snprint(p, READSTR, "str %Q", v->utf); + if(v->data[0]) + snprint(p, READSTR, "str %q", v->data); n = readstr(offset, a, n, p); break; case REG_MULTI_SZ: l = snprint(p, READSTR, "str"); for(i=0;;){ - l += snprint(p+l, READSTR-l, " %Q", v->utf+i); - while(v->utf[i++] != 0){ + l += snprint(p+l, READSTR-l, " %q", v->data+i); + while(v->data[i++] != 0){ /* skip */ } - if(v->utf[i] == 0) + if(v->data[i] == 0) break; /* final terminator */ } n = readstr(offset, a, n, p); @@ -273,7 +270,7 @@ archwrite(Chan* c, void* a, long n, vlong offset) Value *v; int i; Cmdbuf *cb; - Rune *key, *item; + Rune16 *key, *item; if((ulong)c->qid.path != Qregquery) error(Eperm); @@ -339,22 +336,27 @@ Dev archdevtab = { static void regerr(int rc) { - Rune err[64]; + Rune16 err[64]; + char emsg[sizeof(err)*UTFmax+1]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, rc, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err, sizeof(err), 0); - errorf("%S", err); + runes16toutf(emsg, err, nelem(err)); + error(emsg); } static Value* -getregistry(HKEY root, Rune *keyname, Rune *name) +getregistry(HKEY root, Rune16 *keyname, Rune16 *name) { long res; HKEY key; DWORD dtype, n; + int i, l, nb; void* vp; + char *p; Value *val; + Rune16 *rb; qlock(®lock); if(waserror()){ @@ -372,13 +374,22 @@ getregistry(HKEY root, Rune *keyname, Rune *name) res = RegQueryValueEx(key, name, NULL, &dtype, NULL, &n); if(res != ERROR_SUCCESS) regerr(res); - val = smalloc(sizeof(Value)+n); + nb = n; + if(dtype == REG_SZ || dtype == REG_EXPAND_SZ || dtype == REG_MULTI_SZ){ + nb = n*UTFmax + 1; + rb = smalloc((n+2)*sizeof(Rune16)); + memset(rb, 0, (n+2)*sizeof(Rune16)); + }else + rb = nil; + if(waserror()){ + free(rb); + nexterror(); + } + val = smalloc(sizeof(Value)+nb); if(waserror()){ free(val); nexterror(); } - if(0) - fprint(2, "%S\\%S: %d %d\n", keyname, name, dtype, n); val->type = dtype; val->size = n; switch(dtype){ @@ -393,7 +404,7 @@ getregistry(HKEY root, Rune *keyname, Rune *name) case REG_SZ: case REG_EXPAND_SZ: case REG_MULTI_SZ: - vp = val->utf; + vp = rb; break; case REG_BINARY: case REG_NONE: @@ -407,6 +418,22 @@ getregistry(HKEY root, Rune *keyname, Rune *name) if(res != ERROR_SUCCESS) regerr(res); poperror(); + if(rb != nil){ + if(dtype == REG_MULTI_SZ){ + p = val->data; + for(i=0;;){ + l = runes16len(rb+i); + runes16toutf(p, rb+i, l); + i += l+1; + if(rb[i] == 0) + break; + p += strlen(p)+1; + } + }else + runes16toutf(val->data, rb, n); + free(rb); + } + poperror(); poperror(); RegCloseKey(key); poperror(); diff --git a/emu/Nt/devfs.c b/emu/Nt/devfs.c index 8493e01e..628d1ef9 100644 --- a/emu/Nt/devfs.c +++ b/emu/Nt/devfs.c @@ -208,7 +208,7 @@ winfilematch(char *path, WIN32_FIND_DATA *data) while(p > path && p[-1] != '\\') --p; wpath = widen(p); - r = (data->cFileName[0] == '.' && runeslen(data->cFileName) == 1) + r = (data->cFileName[0] == '.' && runes16len(data->cFileName) == 1) || runescmp(data->cFileName, wpath) == 0; free(wpath); return r; @@ -314,7 +314,7 @@ fsinit(void) } n = GetFullPathName(wpath, MAXROOT, wrootdir, &last); free(wpath); - runestoutf(rootdir, wrootdir, MAXROOT); + runes16toutf(rootdir, wrootdir, MAXROOT); if(n >= MAXROOT || n == 0) panic("illegal root path"); @@ -789,7 +789,7 @@ fsstat(Chan *c, uchar *buf, int n) data.ftLastWriteTime = wintime(time(0)); data.nFileSizeHigh = 0; data.nFileSizeLow = 0; - utftorunes(data.cFileName, ".", MAX_PATH); + utftorunes16(data.cFileName, ".", MAX_PATH); } else { HANDLE h = INVALID_HANDLE_VALUE; @@ -821,7 +821,7 @@ fsstat(Chan *c, uchar *buf, int n) } FindClose(h); } - utftorunes(data.cFileName, fslastelem(FS(c)->name), MAX_PATH); + utftorunes16(data.cFileName, fslastelem(FS(c)->name), MAX_PATH); } return fsdirset(buf, n, &data, path, c, 0); @@ -849,14 +849,14 @@ fswstat(Chan *c, uchar *buf, int n) error(Eshortstat); last = fspath(FS(c)->name, 0, path, FS(c)->spec); - utftorunes(wspath, path, MAX_PATH); + utftorunes16(wspath, path, MAX_PATH); if(fsisroot(c)){ if(dir.atime != ~0) data.ftLastAccessTime = wintime(dir.atime); if(dir.mtime != ~0) data.ftLastWriteTime = wintime(dir.mtime); - utftorunes(data.cFileName, ".", MAX_PATH); + utftorunes16(data.cFileName, ".", MAX_PATH); }else{ h = FindFirstFile(wspath, &data); if(h == INVALID_HANDLE_VALUE) @@ -953,7 +953,7 @@ fswstat(Chan *c, uchar *buf, int n) } ph = fswalkpath(ph, dir.name, 0); fspath(ph, 0, newpath, FS(c)->spec); - utftorunes(wsnewpath, newpath, MAX_PATH); + utftorunes16(wsnewpath, newpath, MAX_PATH); if(GetFileAttributes(wpath) != 0xffffffff && !winfileclash(newpath)) error("file already exists"); if(fsisroot(c)) @@ -1011,7 +1011,7 @@ fswstat(Chan *c, uchar *buf, int n) } ph = fswalkpath(ph, dir.name, 0); fspath(ph, 0, newpath, FS(c)->spec); - utftorunes(wsnewpath, newpath, MAX_PATH); + utftorunes16(wsnewpath, newpath, MAX_PATH); /* * can't rename if it is open: if this process has it open, close it temporarily. */ @@ -1059,7 +1059,7 @@ fsremove(Chan *c) if(fsisroot(c)) error(Eperm); p = fspath(FS(c)->name, 0, path, FS(c)->spec); - utftorunes(wspath, path, MAX_PATH); + utftorunes16(wspath, path, MAX_PATH); if(FS(c)->checksec){ *p = '\0'; seccheck(path, WMODE, FS(c)->srv); @@ -1296,7 +1296,7 @@ fsdirread(Chan *c, uchar *va, int count, vlong offset) FS(c)->offset = o; return 0; } - runestoutf(p, de->cFileName, &path[MAX_PATH]-p); + runes16toutf(p, de->cFileName, &path[MAX_PATH]-p); path[MAX_PATH-1] = '\0'; o += fsdirsize(de, path, c); } @@ -1311,7 +1311,7 @@ fsdirread(Chan *c, uchar *va, int count, vlong offset) if(de == nil) break; } - runestoutf(p, de->cFileName, &path[MAX_PATH]-p); + runes16toutf(p, de->cFileName, &path[MAX_PATH]-p); path[MAX_PATH-1] = '\0'; r = fsdirset(va+i, count-i, de, path, c, 1); if(r <= 0){ @@ -1659,12 +1659,12 @@ secstat(Dir *dir, char *file, Rune16 *srv) free(sd); if(ok){ dir->mode = st.mode; - n = runenlen(st.owner->name, runeslen(st.owner->name)); + n = runenlen(st.owner->name, runes16len(st.owner->name)); dir->uid = smalloc(n+1); - runestoutf(dir->uid, st.owner->name, n+1); - n = runenlen(st.group->name, runeslen(st.group->name)); + runes16toutf(dir->uid, st.owner->name, n+1); + n = runenlen(st.group->name, runes16len(st.group->name)); dir->gid = smalloc(n+1); - runestoutf(dir->gid, st.group->name, n+1); + runes16toutf(dir->gid, st.group->name, n+1); } return ok; } @@ -1688,7 +1688,7 @@ secsize(char *file, Rune16 *srv) if(sd != (void*)sdrock) free(sd); if(ok) - return runenlen(st.owner->name, runeslen(st.owner->name))+runenlen(st.group->name, runeslen(st.group->name)); + return runenlen(st.owner->name, runes16len(st.owner->name))+runenlen(st.group->name, runes16len(st.group->name)); return -1; } @@ -2063,7 +2063,7 @@ unametouser(Rune16 *srv, char *name) { Rune16 rname[MAX_PATH]; - utftorunes(rname, name, MAX_PATH); + utftorunes16(rname, name, MAX_PATH); return nametouser(srv, rname); } @@ -2108,10 +2108,10 @@ mkuser(SID *sid, int type, Rune16 *name, Rune16 *dom) u->type = type; u->name = nil; if(name != nil) - u->name = runesdup(name); + u->name = runes16dup(name); u->dom = nil; if(dom != nil) - u->dom = runesdup(dom); + u->dom = runes16dup(dom); u->next = users.u; users.u = u; @@ -2268,7 +2268,7 @@ filesrv(char *file) n = sizeof(uni); if(WNetGetUniversalName(vol, UNIVERSAL_NAME_INFO_LEVEL, uni, &n) != NO_ERROR) return nil; - runestoutf(mfile, ((UNIVERSAL_NAME_INFO*)uni)->lpUniversalName, MAX_PATH); + runes16toutf(mfile, ((UNIVERSAL_NAME_INFO*)uni)->lpUniversalName, MAX_PATH); file = mfile; } file += 2; @@ -2286,7 +2286,7 @@ filesrv(char *file) srv = malloc((n + 1) * sizeof(Rune16)); if(srv == nil) panic("filesrv: no memory"); - utftorunes(srv, uni, n+1); + utftorunes16(srv, uni, n+1); return srv; } @@ -2318,7 +2318,7 @@ fsacls(char *file) else p[1] = 0; } - utftorunes(wpath, path, MAX_PATH); + utftorunes16(wpath, path, MAX_PATH); if(!GetVolumeInformation(wpath, NULL, 0, NULL, NULL, &flags, NULL, 0)) return 0; @@ -2343,7 +2343,7 @@ domsrv(Rune16 *dom, Rune16 srv[MAX_PATH]) r = NetGetAnyDCName(NULL, dom, (LPBYTE*)&psrv); if(r == NERR_Success) { - n = runeslen(psrv); + n = runes16len(psrv); if(n >= MAX_PATH) n = MAX_PATH-1; memmove(srv, psrv, n*sizeof(Rune16)); diff --git a/emu/Nt/ie-os.c b/emu/Nt/ie-os.c index f97b4e32..3e2654e7 100644 --- a/emu/Nt/ie-os.c +++ b/emu/Nt/ie-os.c @@ -789,39 +789,3 @@ segflush(void *a, ulong n) { return 0; } - -wchar_t * -widen(char *s) -{ - int n; - wchar_t *ws; - - n = utflen(s) + 1; - ws = smalloc(n*sizeof(wchar_t)); - utftorunes(ws, s, n); - return ws; -} - - -char * -narrowen(wchar_t *ws) -{ - char *s; - int n; - - n = widebytes(ws); - s = smalloc(n); - runestoutf(s, ws, n); - return s; -} - - -int -widebytes(wchar_t *ws) -{ - int n = 0; - - while (*ws) - n += runelen(*ws++); - return n+1; -} diff --git a/emu/Nt/os.c b/emu/Nt/os.c index e5e0c57e..7ebe9f4b 100644 --- a/emu/Nt/os.c +++ b/emu/Nt/os.c @@ -713,39 +713,3 @@ segflush(void *a, ulong n) { return 0; } - -wchar_t * -widen(char *s) -{ - int n; - wchar_t *ws; - - n = utflen(s) + 1; - ws = smalloc(n*sizeof(wchar_t)); - utftorunes(ws, s, n); - return ws; -} - - -char * -narrowen(wchar_t *ws) -{ - char *s; - int n; - - n = widebytes(ws); - s = smalloc(n); - runestoutf(s, ws, n); - return s; -} - - -int -widebytes(wchar_t *ws) -{ - int n = 0; - - while (*ws) - n += runelen(*ws++); - return n+1; -} diff --git a/emu/Nt/r16.c b/emu/Nt/r16.c index f340f3e4..f8563b64 100644 --- a/emu/Nt/r16.c +++ b/emu/Nt/r16.c @@ -10,12 +10,12 @@ #include "r16.h" Rune16* -runesdup(Rune16 *r) +runes16dup(Rune16 *r) { int n; Rune16 *s; - n = runeslen(r) + 1; + n = runes16len(r) + 1; s = malloc(n * sizeof(Rune16)); if(s == nil) error(Enomem); @@ -24,7 +24,7 @@ runesdup(Rune16 *r) } int -runeslen(Rune16 *r) +runes16len(Rune16 *r) { int n; @@ -35,7 +35,7 @@ runeslen(Rune16 *r) } char* -runestoutf(char *p, Rune16 *r, int nc) +runes16toutf(char *p, Rune16 *r, int nc) { char *op, *ep; int n, c; @@ -60,7 +60,7 @@ runestoutf(char *p, Rune16 *r, int nc) } Rune16* -utftorunes(Rune16 *r, char *p, int nc) +utftorunes16(Rune16 *r, char *p, int nc) { Rune16 *or, *er; Rune rc; @@ -92,3 +92,39 @@ runescmp(Rune16 *s1, Rune16 *s2) return 0; } } + +wchar_t * +widen(char *s) +{ + int n; + wchar_t *ws; + + n = utflen(s) + 1; + ws = smalloc(n*sizeof(wchar_t)); + utftorunes16(ws, s, n); + return ws; +} + + +char * +narrowen(wchar_t *ws) +{ + char *s; + int n; + + n = widebytes(ws); + s = smalloc(n); + runes16toutf(s, ws, n); + return s; +} + + +int +widebytes(wchar_t *ws) +{ + int n = 0; + + while (*ws) + n += runelen(*ws++); + return n+1; +} diff --git a/emu/Nt/r16.h b/emu/Nt/r16.h index b245392d..fc991abb 100644 --- a/emu/Nt/r16.h +++ b/emu/Nt/r16.h @@ -3,8 +3,8 @@ typedef unsigned short Rune16; wchar_t *widen(char *s); char *narrowen(wchar_t *ws); int widebytes(wchar_t *ws); - int runeslen(Rune16*); - Rune16* runesdup(Rune*); - Rune16* utftorunes(Rune*, char*, int); - char* runestoutf(char*, Rune16*, int); + int runes16len(Rune16*); + Rune16* runes16dup(Rune*); + Rune16* utftorunes16(Rune*, char*, int); + char* runes16toutf(char*, Rune16*, int); int runescmp(Rune16*, Rune*); diff --git a/emu/Nt/win.c b/emu/Nt/win.c index 93c79c25..4eb56f45 100644 --- a/emu/Nt/win.c +++ b/emu/Nt/win.c @@ -35,7 +35,7 @@ extern ulong displaychan; -extern char* runestoutf(char*, Rune*, int); +extern char* runes16toutf(char*, Rune*, int); extern int bytesperline(Rectangle, int); extern int main(int argc, char **argv); static void dprint(char*, ...); @@ -706,7 +706,7 @@ clipreadunicode(HANDLE h) n = runenlen(p, runestrlen(p)+1); q = malloc(n); if(q != nil) - runestoutf(q, p, n); + runes16toutf(q, p, n); GlobalUnlock(h); if(q == nil) |
