gonzui


Format: Advanced Search

t2ex/t2ex_source/t2ex/fs/fimp/src/fimp_fat_enc.hbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12.
   10:  *    Modified by T-Engine Forum at 2013/03/08.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: /*
   16:  * This software package is available for use, modification, 
   17:  * and redistribution in accordance with the terms of the attached 
   18:  * T-License 2.x.
   19:  * If you want to redistribute the source code, you need to attach 
   20:  * the T-License 2.x document.
   21:  * There's no obligation to publish the content, and no obligation 
   22:  * to disclose it to the TRON Forum if you have modified the 
   23:  * software package.
   24:  * You can also distribute the modified source code. In this case, 
   25:  * please register the modification to T-Kernel traceability service.
   26:  * People can know the history of modifications by the service, 
   27:  * and can be sure that the version you have inherited some 
   28:  * modification of a particular version or not.
   29:  *
   30:  *    http://trace.tron.org/tk/?lang=en
   31:  *    http://trace.tron.org/tk/?lang=ja
   32:  *
   33:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   34:  * the portion of the software that is copyrighted by Ken Sakamura or 
   35:  * the TRON Forum does not infringe the copyrights of a third party.
   36:  * However, it does not make any warranty other than this.
   37:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   38:  * responsible for any consequences or damages caused directly or
   39:  * indirectly by the use of this software package.
   40:  *
   41:  * The source codes in bsd_source.tar.gz in this software package are 
   42:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   43:  * They need to be changed or redistributed according to the 
   44:  * representation of each source header.
   45:  */
   46: 
   47: /*
   48:  *      @(#)fimp_fat_enc.h
   49:  *
   50:  */
   51: 
   52: #ifndef __FIMP_FAT_ENC_H__
   53: #define __FIMP_FAT_ENC_H__
   54: 
   55: #include <t2ex/fs.h>
   56: #include <tk/tkernel.h>
   57: #include <string.h>
   58: #include <ctype.h>
   59: 
   60: /*
   61:  *  String operations
   62:  */
   63: #define fat_strncpy(dst,src,n)  (UB*)strncpy((B*)(dst),(const B*)(src),(size_t)(n))
   64: IMPORT  W        fat_strncasecmp(const UB *str1, const UB *str2, UW n);
   65: IMPORT  W        fat_strlen16(const UH *utf16str);
   66: 
   67: /*
   68:  *  Endian conversion
   69:  */
   70: #if     BIGENDIAN
   71: #define CEH(x)  swapH(x)
   72: #define CEW(x)  swapW(x)
   73: #else
   74: #define CEH(x)  (x)
   75: #define CEW(x)  (x)
   76: #endif
   77: 
   78: Inline  UH       swapH(UH x)
   79: {
   80:         return (x << 8) | (x >> 8);
   81: }
   82: Inline  UW       swapW(UW x)
   83: {
   84:         return (UW)swapH((UH)x) << 16 | swapH((UH)(x >> 16));
   85: }
   86: 
   87: /*
   88:  *  Missalign operations
   89:  */
   90: Inline  UH       GetMisalignH(UB d[2])
   91: {
   92: #if ALLOW_MISALIGN
   93:         return *(UH *)d;
   94: #else
   95: #if BIGENDIAN
   96:         return ((UH)d[0] << 8) | (UH)d[1];
   97: #else
   98:         return ((UH)d[1] << 8) | (UH)d[0];
   99: #endif
  100: #endif
  101: }
  102: 
  103: Inline  void     SetMisalignH(UB d[2], UH x)
  104: {
  105: #if ALLOW_MISALIGN
  106:         *(UH *)d = x;
  107: #else
  108: #if BIGENDIAN
  109:         d[0] = x >> 8;
  110:         d[1] = x;
  111: #else
  112:         d[1] = x >> 8;
  113:         d[0] = x;
  114: #endif
  115: #endif
  116: }
  117: 
  118: Inline  UW       GetMisalignW(UB d[4])
  119: {
  120: #if ALLOW_MISALIGN
  121:         return *(UW *)d;
  122: #else
  123: #if BIGENDIAN
  124:         return ((UW)d[0] << 24) | ((UW)d[1] << 16) | \
  125:                                 ((UW)d[2] << 8) | (UW) d[3];
  126: #else
  127:         return ((UW)d[3] << 24) | ((UW)d[2] << 16) | \
  128:                                 ((UW)d[1] << 8) | (UW) d[0];
  129: #endif
  130: #endif
  131: }
  132: 
  133: Inline  void     SetMisalignW(UB d[4], UW x)
  134: {
  135: #if ALLOW_MISALIGN
  136:         *(UW *)d = x;
  137: #else
  138: #if BIGENDIAN
  139:         d[0] = x >> 24;
  140:         d[1] = x >> 16;
  141:         d[2] = x >> 8;
  142:         d[3] = x;
  143: #else
  144:         d[3] = x >> 24;
  145:         d[2] = x >> 16;
  146:         d[1] = x >> 8;
  147:         d[0] = x;
  148: #endif
  149: #endif
  150: }
  151: 
  152: /*
  153:  *  Encode definitions
  154:  */
  155: #define UTF8_MAX                (6)
  156: #define ENC_CHAR_MAX            (4)
  157: 
  158: #define ASCII_MAX               (0x7f)
  159: 
  160: /* ASCII tables for character check */
  161: IMPORT const    UB fatEncAsciiKindTable[ASCII_MAX + 1];
  162: 
  163: #define AsciiToUP(ascii)        ((UB)toupper((ascii) & ASCII_MAX))
  164: #define AsciiToLOW(ascii)       ((UB)tolower((ascii) & ASCII_MAX))
  165: 
  166: #define getAsciiKind(ascii)     (fatEncAsciiKindTable[(ascii)] & 0xf)
  167: #define ASCII_LOW               (0)  /* Lower */
  168: #define ASCII_UP                (1)   /* Upper  */
  169: #define ASCII_NUM               (2)  /* Number        */
  170: #define ASCII_SYM               (3)  /* Symbol        */
  171: #define ASCII_CON               (4)  /* Control code */
  172: #define ASCII_KIND_MAX          (5)
  173: 
  174: #define AsciiIsLOW(ascii)       (getAsciiKind(ascii) == ASCII_LOW)
  175: #define AsciiIsUP(ascii)        (getAsciiKind(ascii) == ASCII_UP)
  176: #define AsciiIsNUM(ascii)       (getAsciiKind(ascii) == ASCII_NUM)
  177: #define AsciiIsSYM(ascii)       (getAsciiKind(ascii) == ASCII_SYM)
  178: 
  179: #define AsciiIsSFN(ascii)       ((fatEncAsciiKindTable[(ascii)] & 0x10) == 0x10)
  180: #define AsciiIsLFN(ascii)       ((fatEncAsciiKindTable[(ascii)] & 0x20) == 0x20)
  181: 
  182: 
  183: #define CharIsAscii(c)          ((0 < (c)) && ((c) <= ASCII_MAX))
  184: 
  185: #define Utf8IsAscii(utf8)       ((0 < (utf8)) && ((utf8) <= ASCII_MAX))
  186: 
  187: #define Utf16IsUpSurrogate(utf16)       \
  188:                         ((0xD800 <= (utf16)) && ((utf16) <= 0xDBFF))
  189: #define Utf16IsLowSurrogate(utf16)      \
  190:                         ((0xDC00 <= (utf16)) && ((utf16) <= 0xDFFF))
  191: #define Utf16ToUnicode(up, low)                 \
  192:                         (UW)((((up) - 0xd800) * 0x400) + ((low) - 0xdc00))
  193: 
  194: #define UnicodeIsAscii(unicode)         \
  195:                         ((0 < (unicode)) && ((unicode) <= ASCII_MAX))
  196: #define UnicodeIsNormal(unicode)        \
  197:                         ((0 < (unicode)) && ((unicode) < 0x10000))
  198: #define UnicodeIsSurrogate(unicode)     \
  199:                         ((0x10000 <= (unicode)) && ((unicode) < 0x20000))
  200: #define UnicodeIsInvalid(unicode)       \
  201:                         (((unicode) <= 0) || (0x20000 <= (unicode)))
  202: 
  203: #define UnicodeToUtf16UpSurrogate(unicode)      \
  204:                         (UH)(((unicode - 0x10000) / 0x400) + 0xd800)
  205: #define UnicodeToUtf16LowSurrogate(unicode)     \
  206:                         (UH)(((unicode - 0x10000) % 0x400) + 0xdc00)
  207: 
  208: /*
  209:  *  Encode functions
  210:  */
  211: IMPORT  W        fatEncUtf8ToUnicode(const UB utf8str[], UW *_unicode);
  212: 
  213: IMPORT  W        fatEncUnicodeToUtf8(UW unicode, UB utf8a[UTF8_MAX]);
  214: 
  215: IMPORT  W        fatEncUtf8strToUtf16str(const UB *utf8str,
  216:                 W utf8len, UH *utf16str, W utf16buflen, BOOL toUpper);
  217: 
  218: IMPORT  W        fatEncUtf16strToUtf8str(const UH *utf16str,
  219:                 W utf16len, UB *utf8str, W utf8buflen);
  220: 
  221: #ifndef FAT_ASCII_FN_ONLY
  222: 
  223: IMPORT  W        fatEncCP932ToUnicode(const UB cp932[], UW *unicode);
  224: IMPORT  W        fatEncUnicodeToCP932(UW unicode, UB cp932[ENC_CHAR_MAX]);
  225: 
  226: Inline  W        fatEncUnicodeToLocal(UW unicode, UB local[ENC_CHAR_MAX])
  227: {
  228:         return fatEncUnicodeToCP932(unicode, local);
  229: }
  230: 
  231: Inline  W        fatEncLocalToUnicode(const UB local[], UW *unicode)
  232: {
  233:         return fatEncCP932ToUnicode(local, unicode);
  234: }
  235: #endif  /* ~ FAT_ASCII_FN_ONLY */
  236: 
  237: #endif /* __FIMP_FAT_ENC_H__ */
  238: