tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

prerrortable.c (5580B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /*
      6 
      7 Copyright 1987, 1988 by the Student Information Processing Board
      8    of the Massachusetts Institute of Technology
      9 
     10 Permission to use, copy, modify, and distribute this software
     11 and its documentation for any purpose and without fee is
     12 hereby granted, provided that the above copyright notice
     13 appear in all copies and that both that copyright notice and
     14 this permission notice appear in supporting documentation,
     15 and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
     16 used in advertising or publicity pertaining to distribution
     17 of the software without specific, written prior permission.
     18 M.I.T. and the M.I.T. S.I.P.B. make no representations about
     19 the suitability of this software for any purpose.  It is
     20 provided "as is" without express or implied warranty.
     21 
     22 */
     23 
     24 #include <string.h>
     25 #include <assert.h>
     26 #include <errno.h>
     27 #include "prmem.h"
     28 #include "prerror.h"
     29 
     30 #define ERRCODE_RANGE 8 /* # of bits to shift table number */
     31 #define BITS_PER_CHAR 6 /* # bits to shift per character in name */
     32 
     33 #ifdef NEED_SYS_ERRLIST
     34 extern char const* const sys_errlist[];
     35 extern const int sys_nerr;
     36 #endif
     37 
     38 /* List of error tables */
     39 struct PRErrorTableList {
     40  struct PRErrorTableList* next;
     41  const struct PRErrorTable* table;
     42  struct PRErrorCallbackTablePrivate* table_private;
     43 };
     44 static struct PRErrorTableList* Table_List = (struct PRErrorTableList*)NULL;
     45 
     46 /* Supported languages */
     47 static const char* default_languages[] = {"i-default", "en", 0};
     48 static const char* const* callback_languages = default_languages;
     49 
     50 /* Callback info */
     51 static struct PRErrorCallbackPrivate* callback_private = 0;
     52 static PRErrorCallbackLookupFn* callback_lookup = 0;
     53 static PRErrorCallbackNewTableFn* callback_newtable = 0;
     54 
     55 static const char char_set[] =
     56    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
     57 
     58 static const char* error_table_name(PRErrorCode num) {
     59  static char buf[6]; /* only used if internal code problems exist */
     60 
     61  long ch;
     62  int i;
     63  char* p;
     64 
     65  /* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */
     66  p = buf;
     67  num >>= ERRCODE_RANGE;
     68  /* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */
     69  num &= 077777777;
     70  /* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */
     71  for (i = 4; i >= 0; i--) {
     72    ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);
     73    if (ch != 0) {
     74      *p++ = char_set[ch - 1];
     75    }
     76  }
     77  *p = '\0';
     78  return (buf);
     79 }
     80 
     81 PR_IMPLEMENT(const char*)
     82 PR_ErrorToString(PRErrorCode code, PRLanguageCode language) {
     83  /* static buffer only used if code is using inconsistent error message
     84   * numbers, so just ignore the possible thread contention
     85   */
     86  static char buffer[25];
     87 
     88  const char* msg;
     89  int offset;
     90  PRErrorCode table_num;
     91  struct PRErrorTableList* et;
     92  int started = 0;
     93  char* cp;
     94 
     95  for (et = Table_List; et; et = et->next) {
     96    if (et->table->base <= code && et->table->base + et->table->n_msgs > code) {
     97      /* This is the right table */
     98      if (callback_lookup) {
     99        msg = callback_lookup(code, language, et->table, callback_private,
    100                              et->table_private);
    101        if (msg) {
    102          return msg;
    103        }
    104      }
    105 
    106      return (et->table->msgs[code - et->table->base].en_text);
    107    }
    108  }
    109 
    110  if (code >= 0 && code < 256) {
    111    return strerror(code);
    112  }
    113 
    114  offset = (int)(code & ((1 << ERRCODE_RANGE) - 1));
    115  table_num = code - offset;
    116  strcpy(buffer, "Unknown code ");
    117  if (table_num) {
    118    strcat(buffer, error_table_name(table_num));
    119    strcat(buffer, " ");
    120  }
    121  for (cp = buffer; *cp; cp++);
    122  if (offset >= 100) {
    123    *cp++ = (char)('0' + offset / 100);
    124    offset %= 100;
    125    started++;
    126  }
    127  if (started || offset >= 10) {
    128    *cp++ = (char)('0' + offset / 10);
    129    offset %= 10;
    130  }
    131  *cp++ = (char)('0' + offset);
    132  *cp = '\0';
    133  return (buffer);
    134 }
    135 
    136 PR_IMPLEMENT(const char*)
    137 PR_ErrorToName(PRErrorCode code) {
    138  struct PRErrorTableList* et;
    139 
    140  for (et = Table_List; et; et = et->next) {
    141    if (et->table->base <= code && et->table->base + et->table->n_msgs > code) {
    142      /* This is the right table */
    143      return (et->table->msgs[code - et->table->base].name);
    144    }
    145  }
    146 
    147  return 0;
    148 }
    149 
    150 PR_IMPLEMENT(const char* const*)
    151 PR_ErrorLanguages(void) { return callback_languages; }
    152 
    153 PR_IMPLEMENT(PRErrorCode)
    154 PR_ErrorInstallTable(const struct PRErrorTable* table) {
    155  struct PRErrorTableList* new_et;
    156 
    157  new_et = (struct PRErrorTableList*)PR_Malloc(sizeof(struct PRErrorTableList));
    158  if (!new_et) {
    159    return errno; /* oops */
    160  }
    161  new_et->table = table;
    162  if (callback_newtable) {
    163    new_et->table_private = callback_newtable(table, callback_private);
    164  } else {
    165    new_et->table_private = 0;
    166  }
    167  new_et->next = Table_List;
    168  Table_List = new_et;
    169  return 0;
    170 }
    171 
    172 PR_IMPLEMENT(void)
    173 PR_ErrorInstallCallback(const char* const* languages,
    174                        PRErrorCallbackLookupFn* lookup,
    175                        PRErrorCallbackNewTableFn* newtable,
    176                        struct PRErrorCallbackPrivate* cb_private) {
    177  struct PRErrorTableList* et;
    178 
    179  assert(strcmp(languages[0], "i-default") == 0);
    180  assert(strcmp(languages[1], "en") == 0);
    181 
    182  callback_languages = languages;
    183  callback_lookup = lookup;
    184  callback_newtable = newtable;
    185  callback_private = cb_private;
    186 
    187  if (callback_newtable) {
    188    for (et = Table_List; et; et = et->next) {
    189      et->table_private = callback_newtable(et->table, callback_private);
    190    }
    191  }
    192 }