Sebastian Kuschel reports that pfd_closing can be called for a socket
[u/mdw/putty] / charset / sbcs.c
index a71e5d8..8e2a227 100644 (file)
@@ -16,30 +16,38 @@ void read_sbcs(charset_spec const *charset, long int input_chr,
               charset_state *state,
               void (*emit)(void *ctx, long int output), void *emitctx)
 {
-    wchar_t const *table = (wchar_t const *)charset->data;
+    const struct sbcs_data *sd = charset->data;
 
     UNUSEDARG(state);
 
-    emit(emitctx, table[input_chr]);
+    emit(emitctx, sd->sbcs2ucs[input_chr]);
 }
 
 void write_sbcs(charset_spec const *charset, long int input_chr,
                charset_state *state,
                void (*emit)(void *ctx, long int output), void *emitctx)
 {
-    wchar_t const *table = (wchar_t const *)charset->data;
-    int i;
+    const struct sbcs_data *sd = charset->data;
+    int i, j, k, c;
 
     UNUSEDARG(state);
 
     /*
-     * FIXME: this should work, but it's ludicrously inefficient.
-     * We should be using the ucs2sbcs table.
+     * Binary-search in the ucs2sbcs table.
      */
-    for (i = 0; i < 256; i++)
-       if (table[i] == input_chr) {
-           emit(emitctx, i);
+    i = -1;
+    j = sd->nvalid;
+    while (i+1 < j) {
+       k = (i+j)/2;
+       c = sd->ucs2sbcs[k];
+       if (input_chr < sd->sbcs2ucs[c])
+           j = k;
+       else if (input_chr > sd->sbcs2ucs[c])
+           i = k;
+       else {
+           emit(emitctx, c);
            return;
        }
+    }
     emit(emitctx, ERROR);
 }