Back to index

lightning-sunbird  0.9+nobinonly
ugen.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   jeroen.dobbelaere@acunia.com
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include "unicpriv.h"
00039 /*=================================================================================
00040 
00041 =================================================================================*/
00042 typedef  PRBool (*uSubGeneratorFunc) (PRUint16 in, unsigned char* out);
00043 /*=================================================================================
00044 
00045 =================================================================================*/
00046 
00047 typedef PRBool (*uGeneratorFunc) (
00048                                   uShiftTable    *shift,
00049                                   PRInt32*    state,
00050                                   PRUint16    in,
00051                                   unsigned char*  out,
00052                                   PRUint32     outbuflen,
00053                                   PRUint32*    outlen
00054                                   );
00055 
00056 MODULE_PRIVATE PRBool uGenerate(  
00057                                 uShiftTable    *shift,
00058                                 PRInt32*    state,
00059                                 PRUint16    in,
00060                                 unsigned char*  out,
00061                                 PRUint32     outbuflen,
00062                                 PRUint32*    outlen
00063                                 );
00064 
00065 #define uSubGennerator(sub,in,out) (* m_subgenerator[sub])((in),(out))
00066 
00067 PRIVATE PRBool uCheckAndGenAlways1Byte(
00068                                        uShiftTable   *shift,
00069                                        PRInt32*   state,
00070                                        PRUint16   in,
00071                                        unsigned char* out,
00072                                        PRUint32    outbuflen,
00073                                        PRUint32*   outlen
00074                                        );
00075 PRIVATE PRBool uCheckAndGenAlways2Byte(
00076                                        uShiftTable   *shift,
00077                                        PRInt32*   state,
00078                                        PRUint16   in,
00079                                        unsigned char* out,
00080                                        PRUint32    outbuflen,
00081                                        PRUint32*   outlen
00082                                        );
00083 PRIVATE PRBool uCheckAndGenAlways2ByteShiftGR(
00084                                               uShiftTable    *shift,
00085                                               PRInt32*    state,
00086                                               PRUint16    in,
00087                                               unsigned char*  out,
00088                                               PRUint32     outbuflen,
00089                                               PRUint32*    outlen
00090                                               );
00091 PRIVATE PRBool uCheckAndGenByTable(
00092                                    uShiftTable   *shift,
00093                                    PRInt32*   state,
00094                                    PRUint16   in,
00095                                    unsigned char* out,
00096                                    PRUint32    outbuflen,
00097                                    PRUint32*   outlen
00098                                    );
00099 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8F(
00100                                            uShiftTable   *shift,
00101                                            PRInt32*   state,
00102                                            PRUint16   in,
00103                                            unsigned char* out,
00104                                            PRUint32    outbuflen,
00105                                            PRUint32*   outlen
00106                                            );
00107 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA2(
00108                                              uShiftTable   *shift,
00109                                              PRInt32*   state,
00110                                              PRUint16   in,
00111                                              unsigned char* out,
00112                                              PRUint32    outbuflen,
00113                                              PRUint32*   outlen
00114                                              );
00115 
00116 PRIVATE PRBool uCheckAndGenAlways2ByteSwap(
00117                                            uShiftTable   *shift,
00118                                            PRInt32*   state,
00119                                            PRUint16   in,
00120                                            unsigned char* out,
00121                                            PRUint32    outbuflen,
00122                                            PRUint32*   outlen
00123                                            );
00124 
00125 PRIVATE PRBool uCheckAndGenAlways4Byte(
00126                                        uShiftTable   *shift,
00127                                        PRInt32*   state,
00128                                        PRUint16   in,
00129                                        unsigned char* out,
00130                                        PRUint32    outbuflen,
00131                                        PRUint32*   outlen
00132                                        );
00133 
00134 PRIVATE PRBool uCheckAndGenAlways4ByteSwap(
00135                                            uShiftTable   *shift,
00136                                            PRInt32*   state,
00137                                            PRUint16   in,
00138                                            unsigned char* out,
00139                                            PRUint32    outbuflen,
00140                                            PRUint32*   outlen
00141                                            );
00142 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA3(
00143                                              uShiftTable   *shift,
00144                                              PRInt32*   state,
00145                                              PRUint16   in,
00146                                              unsigned char* out,
00147                                              PRUint32    outbuflen,
00148                                              PRUint32*   outlen
00149                                              );
00150 
00151 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA4(
00152                                              uShiftTable   *shift,
00153                                              PRInt32*   state,
00154                                              PRUint16   in,
00155                                              unsigned char* out,
00156                                              PRUint32    outbuflen,
00157                                              PRUint32*   outlen
00158                                              );
00159 
00160 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA5(
00161                                              uShiftTable   *shift,
00162                                              PRInt32*   state,
00163                                              PRUint16   in,
00164                                              unsigned char* out,
00165                                              PRUint32    outbuflen,
00166                                              PRUint32*   outlen
00167                                              );
00168 
00169 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA6(
00170                                              uShiftTable   *shift,
00171                                              PRInt32*   state,
00172                                              PRUint16   in,
00173                                              unsigned char* out,
00174                                              PRUint32    outbuflen,
00175                                              PRUint32*   outlen
00176                                              );
00177 
00178 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA7(
00179                                              uShiftTable   *shift,
00180                                              PRInt32*   state,
00181                                              PRUint16   in,
00182                                              unsigned char* out,
00183                                              PRUint32    outbuflen,
00184                                              PRUint32*   outlen
00185                                              );
00186 PRIVATE PRBool uCheckAndGenAlways1ByteShiftGL(
00187                                               uShiftTable    *shift,
00188                                               PRInt32*    state,
00189                                               PRUint16    in,
00190                                               unsigned char*  out,
00191                                               PRUint32     outbuflen,
00192                                               PRUint32*    outlen
00193                                               );
00194 
00195 PRIVATE PRBool uCnGAlways8BytesDecomposedHangul(
00196                                               uShiftTable    *shift,
00197                                               PRInt32*    state,
00198                                               PRUint16    in,
00199                                               unsigned char*  out,
00200                                               PRUint32     outbuflen,
00201                                               PRUint32*    outlen
00202                                               );
00203 
00204 PRIVATE PRBool uCheckAndGenJohabHangul(
00205                                        uShiftTable   *shift,
00206                                        PRInt32*   state,
00207                                        PRUint16   in,
00208                                        unsigned char* out,
00209                                        PRUint32    outbuflen,
00210                                        PRUint32*   outlen
00211                                        );
00212 
00213 PRIVATE PRBool uCheckAndGenJohabSymbol(
00214                                        uShiftTable   *shift,
00215                                        PRInt32*   state,
00216                                        PRUint16   in,
00217                                        unsigned char* out,
00218                                        PRUint32    outbuflen,
00219                                        PRUint32*   outlen
00220                                        );
00221 
00222 
00223 PRIVATE PRBool uCheckAndGen4BytesGB18030(
00224                                          uShiftTable   *shift,
00225                                          PRInt32*   state,
00226                                          PRUint16   in,
00227                                          unsigned char* out,
00228                                          PRUint32    outbuflen,
00229                                          PRUint32*   outlen
00230                                          );
00231 
00232 PRIVATE PRBool uGenAlways2Byte(
00233                                PRUint16    in,
00234                                unsigned char* out
00235                                );
00236 PRIVATE PRBool uGenAlways2ByteShiftGR(
00237                                       PRUint16     in,
00238                                       unsigned char*  out
00239                                       );
00240 PRIVATE PRBool uGenAlways1Byte(
00241                                PRUint16    in,
00242                                unsigned char* out
00243                                );
00244 PRIVATE PRBool uGenAlways1BytePrefix8E(
00245                                        PRUint16    in,
00246                                        unsigned char* out
00247                                        );
00248 PRIVATE PRBool uGenAlways2ByteUTF8(
00249                                    PRUint16    in,
00250                                    unsigned char* out
00251                                    );
00252 PRIVATE PRBool uGenAlways3ByteUTF8(
00253                                    PRUint16    in,
00254                                    unsigned char* out
00255                                    );
00256                                    /*=================================================================================
00257                                    
00258 =================================================================================*/
00259 PRIVATE const uGeneratorFunc m_generator[uNumOfCharsetType] =
00260 {
00261     uCheckAndGenAlways1Byte,
00262     uCheckAndGenAlways2Byte,
00263     uCheckAndGenByTable,
00264     uCheckAndGenAlways2ByteShiftGR,
00265     uCheckAndGen2ByteGRPrefix8F,
00266     uCheckAndGen2ByteGRPrefix8EA2,
00267     uCheckAndGenAlways2ByteSwap,
00268     uCheckAndGenAlways4Byte,
00269     uCheckAndGenAlways4ByteSwap,
00270     uCheckAndGen2ByteGRPrefix8EA3,
00271     uCheckAndGen2ByteGRPrefix8EA4,
00272     uCheckAndGen2ByteGRPrefix8EA5,
00273     uCheckAndGen2ByteGRPrefix8EA6,
00274     uCheckAndGen2ByteGRPrefix8EA7,
00275     uCheckAndGenAlways1ByteShiftGL,
00276     uCnGAlways8BytesDecomposedHangul,
00277     uCheckAndGenJohabHangul,
00278     uCheckAndGenJohabSymbol,
00279     uCheckAndGen4BytesGB18030,
00280     uCheckAndGenAlways2Byte   /* place-holder for GR128 */
00281 };
00282 
00283 /*=================================================================================
00284 
00285 =================================================================================*/
00286 
00287 PRIVATE const uSubGeneratorFunc m_subgenerator[uNumOfCharType] =
00288 {
00289     uGenAlways1Byte,
00290     uGenAlways2Byte,
00291     uGenAlways2ByteShiftGR,
00292     uGenAlways1BytePrefix8E,
00293     uGenAlways2ByteUTF8,
00294     uGenAlways3ByteUTF8
00295         
00296 };
00297 /*=================================================================================
00298 
00299 =================================================================================*/
00300 MODULE_PRIVATE PRBool uGenerate(  
00301                                 uShiftTable    *shift,
00302                                 PRInt32*    state,
00303                                 PRUint16    in,
00304                                 unsigned char*  out,
00305                                 PRUint32     outbuflen,
00306                                 PRUint32*    outlen
00307                                 )
00308 {
00309     return (* m_generator[shift->classID]) (shift,state,in,out,outbuflen,outlen);
00310 }
00311 /*=================================================================================
00312 
00313 =================================================================================*/
00314 PRIVATE PRBool uGenAlways1Byte(
00315                                PRUint16    in,
00316                                unsigned char* out
00317                                )
00318 {
00319     out[0] = (unsigned char)in;
00320     return PR_TRUE;
00321 }
00322 
00323 /*=================================================================================
00324 
00325 =================================================================================*/
00326 PRIVATE PRBool uGenAlways2Byte(
00327                                PRUint16    in,
00328                                unsigned char* out
00329                                )
00330 {
00331     out[0] = (unsigned char)((in >> 8) & 0xff);
00332     out[1] = (unsigned char)(in & 0xff);
00333     return PR_TRUE;
00334 }
00335 /*=================================================================================
00336 
00337 =================================================================================*/
00338 PRIVATE PRBool uGenAlways2ByteShiftGR(
00339                                       PRUint16     in,
00340                                       unsigned char*  out
00341                                       )
00342 {
00343     out[0] = (unsigned char)(((in >> 8) & 0xff) | 0x80);
00344     out[1] = (unsigned char)((in & 0xff) | 0x80);
00345     return PR_TRUE;
00346 }
00347 /*=================================================================================
00348 
00349 =================================================================================*/
00350 PRIVATE PRBool uGenAlways1BytePrefix8E(
00351                                        PRUint16    in,
00352                                        unsigned char* out
00353                                        )
00354 {
00355     out[0] = 0x8E;
00356     out[1] = (unsigned char)(in  & 0xff);
00357     return PR_TRUE;
00358 }
00359 /*=================================================================================
00360 
00361 =================================================================================*/
00362 PRIVATE PRBool uGenAlways2ByteUTF8(
00363                                    PRUint16    in,
00364                                    unsigned char* out
00365                                    )
00366 {
00367     out[0] = (unsigned char)(0xC0 | (( in >> 6 ) & 0x1F));
00368     out[1] = (unsigned char)(0x80 | (( in      ) & 0x3F));
00369     return PR_TRUE;
00370 }
00371 
00372 /*=================================================================================
00373 
00374 =================================================================================*/
00375 PRIVATE PRBool uGenAlways3ByteUTF8(
00376                                    PRUint16    in,
00377                                    unsigned char* out
00378                                    )
00379 {
00380     out[0] = (unsigned char)(0xE0 | (( in >> 12 ) & 0x0F));
00381     out[1] = (unsigned char)(0x80 | (( in >> 6  ) & 0x3F));
00382     out[2] = (unsigned char)(0x80 | (( in       ) & 0x3F));
00383     return PR_TRUE;
00384 }
00385 /*=================================================================================
00386 
00387 =================================================================================*/
00388 PRIVATE PRBool uCheckAndGenAlways1Byte(
00389                                        uShiftTable   *shift,
00390                                        PRInt32*   state,
00391                                        PRUint16   in,
00392                                        unsigned char* out,
00393                                        PRUint32    outbuflen,
00394                                        PRUint32*   outlen
00395                                        )
00396 {
00397     /* Don't check inlen. The caller should ensure it is larger than 0 */
00398     /*  Oops, I don't agree. Code changed to check every time. [CATA] */
00399     if(outbuflen < 1)
00400         return PR_FALSE;
00401     else
00402     {
00403         *outlen = 1;
00404         out[0] = in & 0xff;
00405         return PR_TRUE;
00406     }
00407 }
00408 
00409 /*=================================================================================
00410 
00411 =================================================================================*/
00412 PRIVATE PRBool uCheckAndGenAlways2Byte(
00413                                        uShiftTable   *shift,
00414                                        PRInt32*   state,
00415                                        PRUint16   in,
00416                                        unsigned char* out,
00417                                        PRUint32    outbuflen,
00418                                        PRUint32*   outlen
00419                                        )
00420 {
00421     if(outbuflen < 2)
00422         return PR_FALSE;
00423     else
00424     {
00425         *outlen = 2;
00426         out[0] = ((in >> 8 ) & 0xff);
00427         out[1] = in  & 0xff;
00428         return PR_TRUE;
00429     }
00430 }
00431 /*=================================================================================
00432 
00433 =================================================================================*/
00434 PRIVATE PRBool uCheckAndGenAlways2ByteShiftGR(
00435                                               uShiftTable    *shift,
00436                                               PRInt32*    state,
00437                                               PRUint16    in,
00438                                               unsigned char*  out,
00439                                               PRUint32     outbuflen,
00440                                               PRUint32*    outlen
00441                                               )
00442 {
00443     if(outbuflen < 2)
00444         return PR_FALSE;
00445     else
00446     {
00447         *outlen = 2;
00448         out[0] = ((in >> 8 ) & 0xff) | 0x80;
00449         out[1] = (in  & 0xff)  | 0x80;
00450         return PR_TRUE;
00451     }
00452 }
00453 /*=================================================================================
00454 
00455 =================================================================================*/
00456 PRIVATE PRBool uCheckAndGenByTable(
00457                                    uShiftTable   *shift,
00458                                    PRInt32*   state,
00459                                    PRUint16   in,
00460                                    unsigned char* out,
00461                                    PRUint32    outbuflen,
00462                                    PRUint32*   outlen
00463                                    )
00464 {
00465     PRInt16 i;
00466     const uShiftCell* cell = &(shift->shiftcell[0]);
00467     PRInt16 itemnum = shift->numOfItem;
00468     unsigned char inH, inL;
00469     inH = (in >> 8) & 0xff;
00470     inL = (in & 0xff );
00471     for(i=0;i<itemnum;i++)
00472     {
00473         if( ( inL >=  cell[i].shiftout_MinLB) &&
00474             ( inL <=  cell[i].shiftout_MaxLB) &&
00475             ( inH >=  cell[i].shiftout_MinHB) &&
00476             ( inH <=  cell[i].shiftout_MaxHB) )
00477         {
00478             if(outbuflen < cell[i].reserveLen)
00479                 return PR_FALSE;
00480             else
00481             {
00482                 *outlen = cell[i].reserveLen;
00483                 return (uSubGennerator(cell[i].classID,in,out));
00484             }
00485         }
00486     }
00487     return PR_FALSE;
00488 }
00489 /*=================================================================================
00490 
00491 =================================================================================*/
00492 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8F( uShiftTable    *shift,
00493                                            PRInt32*   state,
00494                                            PRUint16   in,
00495                                            unsigned char* out,
00496                                            PRUint32    outbuflen,
00497                                            PRUint32*   outlen
00498                                            )
00499 {
00500     if(outbuflen < 3)
00501         return PR_FALSE;
00502     else
00503     {
00504         *outlen = 3;
00505         out[0] = 0x8F;
00506         out[1] = ((in >> 8 ) & 0xff) | 0x80;
00507         out[2] = (in  & 0xff)  | 0x80;
00508         return PR_TRUE;
00509     }
00510 }
00511 /*=================================================================================
00512 
00513 =================================================================================*/
00514 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA2( uShiftTable    *shift,
00515                                              PRInt32*   state,
00516                                              PRUint16   in,
00517                                              unsigned char* out,
00518                                              PRUint32    outbuflen,
00519                                              PRUint32*   outlen
00520                                              )
00521 {
00522     if(outbuflen < 4)
00523         return PR_FALSE;
00524     else
00525     {
00526         *outlen = 4;
00527         out[0] = 0x8E;
00528         out[1] = 0xA2;
00529         out[2] = ((in >> 8 ) & 0xff) | 0x80;
00530         out[3] = (in  & 0xff)  | 0x80;
00531         return PR_TRUE;
00532     }
00533 }
00534 
00535 
00536 /*=================================================================================
00537 
00538 =================================================================================*/
00539 PRIVATE PRBool uCheckAndGenAlways2ByteSwap(
00540                                            uShiftTable   *shift,
00541                                            PRInt32*   state,
00542                                            PRUint16   in,
00543                                            unsigned char* out,
00544                                            PRUint32    outbuflen,
00545                                            PRUint32*   outlen
00546                                            )
00547 {
00548     if(outbuflen < 2)
00549         return PR_FALSE;
00550     else
00551     {
00552         *outlen = 2;
00553         out[0] = in  & 0xff;
00554         out[1] = ((in >> 8 ) & 0xff);
00555         return PR_TRUE;
00556     }
00557 }
00558 /*=================================================================================
00559 
00560 =================================================================================*/
00561 PRIVATE PRBool uCheckAndGenAlways4Byte(
00562                                        uShiftTable   *shift,
00563                                        PRInt32*   state,
00564                                        PRUint16   in,
00565                                        unsigned char* out,
00566                                        PRUint32    outbuflen,
00567                                        PRUint32*   outlen
00568                                        )
00569 {
00570     if(outbuflen < 4)
00571         return PR_FALSE;
00572     else
00573     {
00574         *outlen = 4;
00575         out[0] = out[1] = 0x00;
00576         out[2] = ((in >> 8 ) & 0xff);
00577         out[3] = in  & 0xff;
00578         return PR_TRUE;
00579     }
00580 }
00581 /*=================================================================================
00582 
00583 =================================================================================*/
00584 PRIVATE PRBool uCheckAndGenAlways4ByteSwap(
00585                                            uShiftTable   *shift,
00586                                            PRInt32*   state,
00587                                            PRUint16   in,
00588                                            unsigned char* out,
00589                                            PRUint32    outbuflen,
00590                                            PRUint32*   outlen
00591                                            )
00592 {
00593     if(outbuflen < 4)
00594         return PR_FALSE;
00595     else
00596     {
00597         *outlen = 4;
00598         out[0] = ((in >> 8 ) & 0xff);
00599         out[1] = in  & 0xff;
00600         out[2] = out[3] = 0x00;
00601         return PR_TRUE;
00602     }
00603 }
00604 /*=================================================================================
00605 
00606 =================================================================================*/
00607 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA3( uShiftTable    *shift,
00608                                              PRInt32*   state,
00609                                              PRUint16   in,
00610                                              unsigned char* out,
00611                                              PRUint32    outbuflen,
00612                                              PRUint32*   outlen
00613                                              )
00614 {
00615     if(outbuflen < 4)
00616         return PR_FALSE;
00617     else
00618     {
00619         *outlen = 4;
00620         out[0] = 0x8E;
00621         out[1] = 0xA3;
00622         out[2] = ((in >> 8 ) & 0xff) | 0x80;
00623         out[3] = (in  & 0xff)  | 0x80;
00624         return PR_TRUE;
00625     }
00626 }
00627 /*=================================================================================
00628 
00629 =================================================================================*/
00630 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA4( uShiftTable    *shift,
00631                                              PRInt32*   state,
00632                                              PRUint16   in,
00633                                              unsigned char* out,
00634                                              PRUint32    outbuflen,
00635                                              PRUint32*   outlen
00636                                              )
00637 {
00638     if(outbuflen < 4)
00639         return PR_FALSE;
00640     else
00641     {
00642         *outlen = 4;
00643         out[0] = 0x8E;
00644         out[1] = 0xA4;
00645         out[2] = ((in >> 8 ) & 0xff) | 0x80;
00646         out[3] = (in  & 0xff)  | 0x80;
00647         return PR_TRUE;
00648     }
00649 }
00650 /*=================================================================================
00651 
00652 =================================================================================*/
00653 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA5( uShiftTable    *shift,
00654                                              PRInt32*   state,
00655                                              PRUint16   in,
00656                                              unsigned char* out,
00657                                              PRUint32    outbuflen,
00658                                              PRUint32*   outlen
00659                                              )
00660 {
00661     if(outbuflen < 4)
00662         return PR_FALSE;
00663     else
00664     {
00665         *outlen = 4;
00666         out[0] = 0x8E;
00667         out[1] = 0xA5;
00668         out[2] = ((in >> 8 ) & 0xff) | 0x80;
00669         out[3] = (in  & 0xff)  | 0x80;
00670         return PR_TRUE;
00671     }
00672 }
00673 /*=================================================================================
00674 
00675 =================================================================================*/
00676 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA6( uShiftTable    *shift,
00677                                              PRInt32*   state,
00678                                              PRUint16   in,
00679                                              unsigned char* out,
00680                                              PRUint32    outbuflen,
00681                                              PRUint32*   outlen
00682                                              )
00683 {
00684     if(outbuflen < 4)
00685         return PR_FALSE;
00686     else
00687     {
00688         *outlen = 4;
00689         out[0] = 0x8E;
00690         out[1] = 0xA6;
00691         out[2] = ((in >> 8 ) & 0xff) | 0x80;
00692         out[3] = (in  & 0xff)  | 0x80;
00693         return PR_TRUE;
00694     }
00695 }
00696 /*=================================================================================
00697 
00698 =================================================================================*/
00699 PRIVATE PRBool uCheckAndGen2ByteGRPrefix8EA7( uShiftTable    *shift,
00700                                              PRInt32*   state,
00701                                              PRUint16   in,
00702                                              unsigned char* out,
00703                                              PRUint32    outbuflen,
00704                                              PRUint32*   outlen
00705                                              )
00706 {
00707     if(outbuflen < 4)
00708         return PR_FALSE;
00709     else
00710     {
00711         *outlen = 4;
00712         out[0] = 0x8E;
00713         out[1] = 0xA7;
00714         out[2] = ((in >> 8 ) & 0xff) | 0x80;
00715         out[3] = (in  & 0xff)  | 0x80;
00716         return PR_TRUE;
00717     }
00718 }
00719 /*=================================================================================
00720 
00721 =================================================================================*/
00722 PRIVATE PRBool uCheckAndGenAlways1ByteShiftGL(
00723                                               uShiftTable    *shift,
00724                                               PRInt32*    state,
00725                                               PRUint16    in,
00726                                               unsigned char*  out,
00727                                               PRUint32     outbuflen,
00728                                               PRUint32*    outlen
00729                                               )
00730 {
00731     /* Don't check inlen. The caller should ensure it is larger than 0 */
00732     /*  Oops, I don't agree. Code changed to check every time. [CATA] */
00733     if(outbuflen < 1)
00734         return PR_FALSE;
00735     else
00736     {
00737         *outlen = 1;
00738         out[0] = in & 0x7f;
00739         return PR_TRUE;
00740     }
00741 }
00742 #define SBase 0xAC00
00743 #define LCount 19
00744 #define VCount 21
00745 #define TCount 28
00746 #define NCount (VCount * TCount)
00747 /*=================================================================================
00748 
00749 =================================================================================*/
00750 PRIVATE PRBool uCnGAlways8BytesDecomposedHangul(
00751                                               uShiftTable    *shift,
00752                                               PRInt32*    state,
00753                                               PRUint16    in,
00754                                               unsigned char*  out,
00755                                               PRUint32     outbuflen,
00756                                               PRUint32*    outlen
00757                                               )
00758 {
00759     static const PRUint8 lMap[LCount] = {
00760         0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xa9, 0xb1, 0xb2, 0xb3, 0xb5,
00761             0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe
00762     };
00763     
00764     static const PRUint8 tMap[TCount] = {
00765         0xd4, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa9, 0xaa, 
00766             0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb4, 0xb5, 
00767             0xb6, 0xb7, 0xb8, 0xba, 0xbb, 0xbc, 0xbd, 0xbe
00768     };
00769 
00770     PRUint16 SIndex, LIndex, VIndex, TIndex;
00771 
00772     if(outbuflen < 8)
00773         return PR_FALSE;
00774 
00775     /* the following line are copy from Unicode 2.0 page 3-13 */
00776     /* item 1 of Hangul Syllabel Decomposition */
00777     SIndex =  in - SBase;
00778     
00779     /* the following lines are copy from Unicode 2.0 page 3-14 */
00780     /* item 2 of Hangul Syllabel Decomposition w/ modification */
00781     LIndex = SIndex / NCount;
00782     VIndex = (SIndex % NCount) / TCount;
00783     TIndex = SIndex % TCount;
00784     
00785     /* 
00786      * A Hangul syllable not enumerated in KS X 1001 is represented
00787      * by a sequence of 8 bytes beginning with Hangul-filler
00788      * (0xA4D4 in EUC-KR and 0x2454 in ISO-2022-KR) followed by three 
00789      * Jamos (2 bytes each the first of which is 0xA4 in EUC-KR) making 
00790      * up the syllable.  ref. KS X 1001:1998 Annex 3
00791      */
00792     *outlen = 8;
00793     out[0] = out[2] = out[4] = out[6] = 0xa4;
00794     out[1] = 0xd4;
00795     out[3] = lMap[LIndex] ;
00796     out[5] = (VIndex + 0xbf);
00797     out[7] = tMap[TIndex];
00798 
00799     return PR_TRUE;
00800 }
00801 
00802 PRIVATE PRBool uCheckAndGenJohabHangul(
00803                                        uShiftTable   *shift,
00804                                        PRInt32*   state,
00805                                        PRUint16   in,
00806                                        unsigned char* out,
00807                                        PRUint32    outbuflen,
00808                                        PRUint32*   outlen
00809                                        )
00810 {
00811     if(outbuflen < 2)
00812         return PR_FALSE;
00813     else
00814     {
00815     /*
00816     See Table 4-45 (page 183) of CJKV Information Processing
00817     for detail explaination of the following table
00818         */
00819         /*
00820         static const PRUint8 lMap[LCount] = {
00821         2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
00822         };
00823         Therefore lMap[i] == i+2;
00824         */
00825         
00826         static const PRUint8 vMap[VCount] = {
00827             /* no 0,1,2 */
00828             3,4,5,6,7,            /* no 8,9   */
00829                 10,11,12,13,14,15,    /* no 16,17 */
00830                 18,19,20,21,22,23,    /* no 24,25 */
00831                 26,27,28,29
00832         };
00833         static const PRUint8 tMap[TCount] = {
00834             1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, /* no 18 */
00835                 19,20,21,22,23,24,25,26,27,28,29
00836         };
00837         PRUint16 SIndex, LIndex, VIndex, TIndex, ch;
00838         /* the following line are copy from Unicode 2.0 page 3-13 */
00839         /* item 1 of Hangul Syllabel Decomposition */
00840         SIndex =  in - SBase;
00841         
00842         /* the following lines are copy from Unicode 2.0 page 3-14 */
00843         /* item 2 of Hangul Syllabel Decomposition w/ modification */
00844         LIndex = SIndex / NCount;
00845         VIndex = (SIndex % NCount) / TCount;
00846         TIndex = SIndex % TCount;
00847         
00848         *outlen = 2;
00849         ch = 0x8000 | 
00850             ((LIndex+2)<<10) | 
00851             (vMap[VIndex]<<5)| 
00852             tMap[TIndex];
00853         out[0] = (ch >> 8);
00854         out[1] = ch & 0x00FF;
00855 #if 0
00856         printf("Johab Hangul %x %x in=%x L=%d V=%d T=%d\n", out[0], out[1], in, LIndex, VIndex, TIndex); 
00857 #endif 
00858         return PR_TRUE;
00859     }
00860 }
00861 PRIVATE PRBool uCheckAndGenJohabSymbol(
00862                                        uShiftTable   *shift,
00863                                        PRInt32*   state,
00864                                        PRUint16   in,
00865                                        unsigned char* out,
00866                                        PRUint32    outbuflen,
00867                                        PRUint32*   outlen
00868                                        )
00869 {
00870     if(outbuflen < 2)
00871         return PR_FALSE;
00872     else
00873     {
00874     /* The following code are based on the Perl code listed under
00875     * "ISO-2022-KR or EUC-KR to Johab Conversion" (page 1013)
00876     * in the book "CJKV Information Processing" by 
00877     * Ken Lunde <lunde@adobe.com>
00878     *
00879     * sub convert2johab($) { # Convert ISO-2022-KR or EUC-KR to Johab
00880     *  my @euc = unpack("C*", $_[0]);
00881     *  my ($fe_off, $hi_off, $lo_off) = (0,0,1);
00882     *  my @out = ();
00883     *  while(($hi, $lo) = splice(@euc, 0, 2)) {
00884     *    $hi &= 127; $lo &= 127;
00885     *    $fe_off = 21 if $hi == 73;
00886     *    $fe_off = 34 if $hi == 126;
00887     *    ($hi_off, $lo_off) = ($lo_off, $hi_off) if ($hi <74 or $hi >125);
00888     *    push(@out, ((($hi+$hi_off) >> 1)+ ($hi <74 ? 200:187)- $fe_off),
00889     *      $lo + ((($hi+$lo_off) & 1) ? ($lo > 110 ? 34:16):128));    
00890     *  }
00891     *  return pack("C*", @out);
00892         */
00893         
00894         unsigned char fe_off = 0;
00895         unsigned char hi_off = 0;
00896         unsigned char lo_off = 1;
00897         unsigned char hi = (in >> 8) & 0x7F;
00898         unsigned char lo = in & 0x7F;
00899         if(73 == hi)
00900             fe_off = 21;
00901         if(126 == hi)
00902             fe_off = 34;
00903         if( (hi < 74) || ( hi > 125) )
00904         {
00905             hi_off = 1;
00906             lo_off = 0;
00907         }
00908         *outlen = 2;
00909         out[0] =  ((hi+hi_off) >> 1) + ((hi<74) ? 200 : 187 ) - fe_off;
00910         out[1] =  lo + (((hi+lo_off) & 1) ? ((lo > 110) ? 34 : 16) : 
00911         128);
00912 #if 0
00913         printf("Johab Symbol %x %x in=%x\n", out[0], out[1], in); 
00914 #endif
00915         return PR_TRUE;
00916     }
00917 }
00918 PRIVATE PRBool uCheckAndGen4BytesGB18030(
00919                                          uShiftTable   *shift,
00920                                          PRInt32*   state,
00921                                          PRUint16   in,
00922                                          unsigned char* out,
00923                                          PRUint32    outbuflen,
00924                                          PRUint32*   outlen
00925                                          )
00926 {
00927     if(outbuflen < 4)
00928         return PR_FALSE;
00929     out[0] = (in / (10*126*10)) + 0x81;
00930     in %= (10*126*10);
00931     out[1] = (in / (10*126)) + 0x30;
00932     in %= (10*126);
00933     out[2] = (in / (10)) + 0x81;
00934     out[3] = (in % 10) + 0x30;
00935     *outlen = 4;
00936     return PR_TRUE;
00937 }