home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libi18n / ugen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.0 KB  |  383 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include "intlpriv.h"
  19. #include "ugen.h"
  20. #include "xp.h"
  21. /*=================================================================================
  22.  
  23. =================================================================================*/
  24. typedef  XP_Bool (*uSubGeneratorFunc) (uint16 in, unsigned char* out);
  25. /*=================================================================================
  26.  
  27. =================================================================================*/
  28.  
  29. typedef XP_Bool (*uGeneratorFunc) (
  30.         uShiftTable             *shift,
  31.         int32*                state,
  32.         uint16                in,
  33.         unsigned char*        out,
  34.         uint16                 outbuflen,
  35.         uint16*                outlen
  36. );
  37.  
  38. MODULE_PRIVATE XP_Bool uGenerate(        
  39.         uShiftTable             *shift,
  40.         int32*                state,
  41.         uint16                in,
  42.         unsigned char*        out,
  43.         uint16                 outbuflen,
  44.         uint16*                outlen
  45. );
  46.  
  47. #define uSubGennerator(sub,in,out)    (* m_subgenerator[sub])((in),(out))
  48.  
  49. PRIVATE XP_Bool uCheckAndGenAlways1Byte(
  50.         uShiftTable             *shift,
  51.         int32*                state,
  52.         uint16                in,
  53.         unsigned char*        out,
  54.         uint16                 outbuflen,
  55.         uint16*                outlen
  56. );
  57. PRIVATE XP_Bool uCheckAndGenAlways2Byte(
  58.         uShiftTable             *shift,
  59.         int32*                state,
  60.         uint16                in,
  61.         unsigned char*        out,
  62.         uint16                 outbuflen,
  63.         uint16*                outlen
  64. );
  65. PRIVATE XP_Bool uCheckAndGenAlways2ByteShiftGR(
  66.         uShiftTable             *shift,
  67.         int32*                state,
  68.         uint16                in,
  69.         unsigned char*        out,
  70.         uint16                 outbuflen,
  71.         uint16*                outlen
  72. );
  73. PRIVATE XP_Bool uCheckAndGenByTable(
  74.         uShiftTable             *shift,
  75.         int32*                state,
  76.         uint16                in,
  77.         unsigned char*        out,
  78.         uint16                 outbuflen,
  79.         uint16*                outlen
  80. );
  81. PRIVATE XP_Bool uCheckAndGen2ByteGRPrefix8F(
  82.         uShiftTable             *shift,
  83.         int32*                state,
  84.         uint16                in,
  85.         unsigned char*        out,
  86.         uint16                 outbuflen,
  87.         uint16*                outlen
  88. );
  89. PRIVATE XP_Bool uCheckAndGen2ByteGRPrefix8EA2(
  90.         uShiftTable             *shift,
  91.         int32*                state,
  92.         uint16                in,
  93.         unsigned char*        out,
  94.         uint16                 outbuflen,
  95.         uint16*                outlen
  96. );
  97.  
  98.  
  99. PRIVATE XP_Bool uGenAlways2Byte(
  100.         uint16                 in,
  101.         unsigned char*        out
  102. );
  103. PRIVATE XP_Bool uGenAlways2ByteShiftGR(
  104.         uint16                 in,
  105.         unsigned char*        out
  106. );
  107. PRIVATE XP_Bool uGenAlways1Byte(
  108.         uint16                 in,
  109.         unsigned char*        out
  110. );
  111. PRIVATE XP_Bool uGenAlways1BytePrefix8E(
  112.         uint16                 in,
  113.         unsigned char*        out
  114. );
  115. PRIVATE XP_Bool uGenAlways2ByteUTF8(
  116.         uint16                 in,
  117.         unsigned char*        out
  118. );
  119. PRIVATE XP_Bool uGenAlways3ByteUTF8(
  120.         uint16                 in,
  121.         unsigned char*        out
  122. );
  123. /*=================================================================================
  124.  
  125. =================================================================================*/
  126. PRIVATE uGeneratorFunc m_generator[uNumOfCharsetType] =
  127. {
  128.     uCheckAndGenAlways1Byte,
  129.     uCheckAndGenAlways2Byte,
  130.     uCheckAndGenByTable,
  131.     uCheckAndGenAlways2ByteShiftGR,
  132.     uCheckAndGen2ByteGRPrefix8F,
  133.     uCheckAndGen2ByteGRPrefix8EA2,
  134. };
  135.  
  136. /*=================================================================================
  137.  
  138. =================================================================================*/
  139.  
  140. PRIVATE uSubGeneratorFunc m_subgenerator[uNumOfCharType] =
  141. {
  142.     uGenAlways1Byte,
  143.     uGenAlways2Byte,
  144.     uGenAlways2ByteShiftGR,
  145.     uGenAlways1BytePrefix8E,
  146.     uGenAlways2ByteUTF8,
  147.     uGenAlways3ByteUTF8
  148.  
  149. };
  150. /*=================================================================================
  151.  
  152. =================================================================================*/
  153. MODULE_PRIVATE XP_Bool uGenerate(        
  154.         uShiftTable             *shift,
  155.         int32*                state,
  156.         uint16                in,
  157.         unsigned char*        out,
  158.         uint16                 outbuflen,
  159.         uint16*                outlen
  160. )
  161. {
  162.     return (* m_generator[shift->classID]) (shift,state,in,out,outbuflen,outlen);
  163. }
  164. /*=================================================================================
  165.  
  166. =================================================================================*/
  167. PRIVATE XP_Bool uGenAlways1Byte(
  168.         uint16                 in,
  169.         unsigned char*        out
  170. )
  171. {
  172.     out[0] = (unsigned char)in;
  173.     return TRUE;
  174. }
  175.  
  176. /*=================================================================================
  177.  
  178. =================================================================================*/
  179. PRIVATE XP_Bool uGenAlways2Byte(
  180.         uint16                 in,
  181.         unsigned char*        out
  182. )
  183. {
  184.     out[0] = (unsigned char)((in >> 8) & 0xff);
  185.     out[1] = (unsigned char)(in & 0xff);
  186.     return TRUE;
  187. }
  188. /*=================================================================================
  189.  
  190. =================================================================================*/
  191. PRIVATE XP_Bool uGenAlways2ByteShiftGR(
  192.         uint16                 in,
  193.         unsigned char*        out
  194. )
  195. {
  196.     out[0] = (unsigned char)(((in >> 8) & 0xff) | 0x80);
  197.     out[1] = (unsigned char)((in & 0xff) | 0x80);
  198.     return TRUE;
  199. }
  200. /*=================================================================================
  201.  
  202. =================================================================================*/
  203. PRIVATE XP_Bool uGenAlways1BytePrefix8E(
  204.         uint16                 in,
  205.         unsigned char*        out
  206. )
  207. {
  208.     out[0] = 0x8E;
  209.     out[1] = (unsigned char)(in  & 0xff);
  210.     return TRUE;
  211. }
  212. /*=================================================================================
  213.  
  214. =================================================================================*/
  215. PRIVATE XP_Bool uGenAlways2ByteUTF8(
  216.         uint16                 in,
  217.         unsigned char*        out
  218. )
  219. {
  220.     out[0] = (unsigned char)(0xC0 | (( in >> 6 ) & 0x1F));
  221.     out[1] = (unsigned char)(0x80 | (( in      ) & 0x3F));
  222.     return TRUE;
  223. }
  224.  
  225. /*=================================================================================
  226.  
  227. =================================================================================*/
  228. PRIVATE XP_Bool uGenAlways3ByteUTF8(
  229.         uint16                 in,
  230.         unsigned char*        out
  231. )
  232. {
  233.     out[0] = (unsigned char)(0xE0 | (( in >> 12 ) & 0x0F));
  234.     out[1] = (unsigned char)(0x80 | (( in >> 6  ) & 0x3F));
  235.     out[2] = (unsigned char)(0x80 | (( in       ) & 0x3F));
  236.     return TRUE;
  237. }
  238. /*=================================================================================
  239.  
  240. =================================================================================*/
  241. PRIVATE XP_Bool uCheckAndGenAlways1Byte(
  242.         uShiftTable             *shift,
  243.         int32*                state,
  244.         uint16                in,
  245.         unsigned char*        out,
  246.         uint16                 outbuflen,
  247.         uint16*                outlen
  248. )
  249. {
  250.     /*    Don't check inlen. The caller should ensure it is larger than 0 */
  251.     *outlen = 1;
  252.     out[0] = in & 0xff;
  253.     return TRUE;
  254. }
  255.  
  256. /*=================================================================================
  257.  
  258. =================================================================================*/
  259. PRIVATE XP_Bool uCheckAndGenAlways2Byte(
  260.         uShiftTable             *shift,
  261.         int32*                state,
  262.         uint16                in,
  263.         unsigned char*        out,
  264.         uint16                 outbuflen,
  265.         uint16*                outlen
  266. )
  267. {
  268.     if(outbuflen < 2)
  269.         return FALSE;
  270.     else
  271.     {
  272.         *outlen = 2;
  273.         out[0] = ((in >> 8 ) & 0xff);
  274.         out[1] = in  & 0xff;
  275.         return TRUE;
  276.     }
  277. }
  278. /*=================================================================================
  279.  
  280. =================================================================================*/
  281. PRIVATE XP_Bool uCheckAndGenAlways2ByteShiftGR(
  282.         uShiftTable             *shift,
  283.         int32*                state,
  284.         uint16                in,
  285.         unsigned char*        out,
  286.         uint16                 outbuflen,
  287.         uint16*                outlen
  288. )
  289. {
  290.     if(outbuflen < 2)
  291.         return FALSE;
  292.     else
  293.     {
  294.         *outlen = 2;
  295.         out[0] = ((in >> 8 ) & 0xff) | 0x80;
  296.         out[1] = (in  & 0xff)  | 0x80;
  297.         return TRUE;
  298.     }
  299. }
  300. /*=================================================================================
  301.  
  302. =================================================================================*/
  303. PRIVATE XP_Bool uCheckAndGenByTable(
  304.         uShiftTable             *shift,
  305.         int32*                state,
  306.         uint16                in,
  307.         unsigned char*        out,
  308.         uint16                 outbuflen,
  309.         uint16*                outlen
  310. )
  311. {
  312.     int16 i;
  313.     uShiftCell* cell = &(shift->shiftcell[0]);
  314.     int16 itemnum = shift->numOfItem;
  315.     unsigned char inH, inL;
  316.     inH =    (in >> 8) & 0xff;
  317.     inL = (in & 0xff );
  318.     for(i=0;i<itemnum;i++)
  319.     {
  320.         if( ( inL >=  cell[i].shiftout.MinLB) &&
  321.             ( inL <=  cell[i].shiftout.MaxLB) &&
  322.             ( inH >=  cell[i].shiftout.MinHB) &&
  323.             ( inH <=  cell[i].shiftout.MaxHB)    )
  324.         {
  325.             if(outbuflen < cell[i].reserveLen)
  326.                 return FALSE;
  327.             else
  328.             {
  329.                 *outlen = cell[i].reserveLen;
  330.                 return (uSubGennerator(cell[i].classID,in,out));
  331.             }
  332.         }
  333.     }
  334.     return FALSE;
  335. }
  336. /*=================================================================================
  337.  
  338. =================================================================================*/
  339. PRIVATE XP_Bool uCheckAndGen2ByteGRPrefix8F( uShiftTable             *shift,
  340.         int32*                state,
  341.         uint16                in,
  342.         unsigned char*        out,
  343.         uint16                 outbuflen,
  344.         uint16*                outlen
  345. )
  346. {
  347.     if(outbuflen < 3)
  348.         return FALSE;
  349.     else
  350.     {
  351.         *outlen = 3;
  352.         out[0] = 0x8F;
  353.         out[1] = ((in >> 8 ) & 0xff) | 0x80;
  354.         out[2] = (in  & 0xff)  | 0x80;
  355.         return TRUE;
  356.     }
  357. }
  358. /*=================================================================================
  359.  
  360. =================================================================================*/
  361. PRIVATE XP_Bool uCheckAndGen2ByteGRPrefix8EA2( uShiftTable             *shift,
  362.         int32*                state,
  363.         uint16                in,
  364.         unsigned char*        out,
  365.         uint16                 outbuflen,
  366.         uint16*                outlen
  367. )
  368. {
  369.     if(outbuflen < 4)
  370.         return FALSE;
  371.     else
  372.     {
  373.         *outlen = 4;
  374.         out[0] = 0x8E;
  375.         out[1] = 0xA2;
  376.         out[2] = ((in >> 8 ) & 0xff) | 0x80;
  377.         out[3] = (in  & 0xff)  | 0x80;
  378.         return TRUE;
  379.     }
  380. }
  381.  
  382.  
  383.