Search code examples
reverse-engineeringglibc

__ctype_b_loc what is its purpose?


I'm trying to understand a piece of code which uses __ctype_b_loc(), problem is that I don't know what is the purpose of this function.

So far, I found it is defined in the ctype.h. I also found its prototype and an implementation. Still I have no idea of what this function is for.

Can someone enlight me?


Solution

  • After a fair research, I think I can answer myself this question.

    unsigned short int** __ctype_b_loc (void)

    is a function which returns a pointer to a 'traits' table containing some flags related with the characteristics of each single character.

    Here's the enum with the flags:

    From ctype.h

    enum
    {
      _ISupper = _ISbit (0),        /* UPPERCASE.  */
      _ISlower = _ISbit (1),        /* lowercase.  */
      _ISalpha = _ISbit (2),        /* Alphabetic.  */
      _ISdigit = _ISbit (3),        /* Numeric.  */
      _ISxdigit = _ISbit (4),       /* Hexadecimal numeric.  */
      _ISspace = _ISbit (5),        /* Whitespace.  */
      _ISprint = _ISbit (6),        /* Printing.  */
      _ISgraph = _ISbit (7),        /* Graphical.  */
      _ISblank = _ISbit (8),        /* Blank (usually SPC and TAB).  */
      _IScntrl = _ISbit (9),        /* Control character.  */
      _ISpunct = _ISbit (10),       /* Punctuation.  */
      _ISalnum = _ISbit (11)        /* Alphanumeric.  */
    };
    

    To make an example, if you make a lookup to the table __ctype_b_loc() returns for the character whose ascii code is 0x30 ('0') you will have 0x08d8.

    0x08d8=0000 1000 1101 1000 (Alphanumeric, Graphical, Printing, Hexadecimal, Numeric)
    

    The table is connected with the localchar of the locale installed on the machine, so the example might not be accurate, compared with results you may have on your system.