#define  INCL_DOSERRORS

#include <os2.h>
#include "blkapifn.h"
#include "bdcalls.h"
#include "bdatt.h"

/*--------------------------------------------------------------*/
/* This is the translation table from  Arabic to Hindi numerics */
/*--------------------------------------------------------------*/
static char ArabicToHindiTable [256] = {
0X00,0X01,0X02,0X03,0X04,0X05,0X06,0X07,0X08,0X09,0X0A,0X0B,0X0C,0X0D,0X0E,0X0F,
0X10,0X11,0X12,0X13,0X14,0X15,0X16,0X17,0X18,0X19,0X1A,0X1B,0X1C,0X1D,0X1E,0X1F,
0X20,0X21,0X22,0X23,0X24,0X25,0X26,0X27,0X28,0X29,0X2A,0X2B,0X2C,0X2D,0X2E,0X2F,
0XB0,0XB1,0XB2,0XB3,0XB4,0XB5,0XB6,0XB7,0XB8,0XB9,0X3A,0X3B,0X3C,0X3D,0X3E,0X3F,
0X40,0X41,0X42,0X43,0X44,0X45,0X46,0X47,0X48,0X49,0X4A,0X4B,0X4C,0X4D,0X4E,0X4F,
0X50,0X51,0X52,0X53,0X54,0X55,0X56,0X57,0X58,0X59,0X5A,0X5B,0X5C,0X5D,0X5E,0X5F,
0X60,0X61,0X62,0X63,0X64,0X65,0X66,0X67,0X68,0X69,0X6A,0X6B,0X6C,0X6D,0X6E,0X6F,
0X70,0X71,0X72,0X73,0X74,0X75,0X76,0X77,0X78,0X79,0X7A,0X7B,0X7C,0X7D,0X7E,0X7F,
0X80,0X81,0X82,0X83,0X84,0X85,0X86,0X87,0X88,0X89,0X8A,0X8B,0X8C,0X8D,0X8E,0X8F,
0X90,0X91,0X92,0X93,0X94,0X95,0X96,0X97,0X98,0X99,0X9A,0X9B,0X9C,0X9D,0X9E,0X9F,
0XA0,0XA1,0XA2,0XA3,0XA4,0XA5,0XA6,0XA7,0XA8,0XA9,0XAA,0XAB,0XAC,0XAD,0XAE,0XAF,
0XB0,0XB1,0XB2,0XB3,0XB4,0XB5,0XB6,0XB7,0XB8,0XB9,0XBA,0XBB,0XBC,0XBD,0XBE,0XBF,
0XC0,0XC1,0XC2,0XC3,0XC4,0XC5,0XC6,0XC7,0XC8,0XC9,0XCA,0XCB,0XCC,0XCD,0XCE,0XCF,
0XD0,0XD1,0XD2,0XD3,0XD4,0XD5,0XD6,0XD7,0XD8,0XD9,0XDA,0XDB,0XDC,0XDD,0XDE,0XDF,
0XE0,0XE1,0XE2,0XE3,0XE4,0XE5,0XE6,0XE7,0XE8,0XE9,0XEA,0XEB,0XEC,0XED,0XEE,0XEF,
0XF0,0XF1,0XF2,0XF3,0XF4,0XF5,0XF6,0XF7,0XF8,0XF9,0XFA,0XFB,0XFC,0XFD,0XFE,0XFF,
};

/*--------------------------------------------------------------*/
/* This is the translation table from Hindi To Arabic  numerics */
/*--------------------------------------------------------------*/
static char HindiToArabicTable [256] = {
0X00,0X01,0X02,0X03,0X04,0X05,0X06,0X07,0X08,0X09,0X0A,0X0B,0X0C,0X0D,0X0E,0X0F,
0X10,0X11,0X12,0X13,0X14,0X15,0X16,0X17,0X18,0X19,0X1A,0X1B,0X1C,0X1D,0X1E,0X1F,
0X20,0X21,0X22,0X23,0X24,0X25,0X26,0X27,0X28,0X29,0X2A,0X2B,0X2C,0X2D,0X2E,0X2F,
0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39,0X3A,0X3B,0X3C,0X3D,0X3E,0X3F,
0X40,0X41,0X42,0X43,0X44,0X45,0X46,0X47,0X48,0X49,0X4A,0X4B,0X4C,0X4D,0X4E,0X4F,
0X50,0X51,0X52,0X53,0X54,0X55,0X56,0X57,0X58,0X59,0X5A,0X5B,0X5C,0X5D,0X5E,0X5F,
0X60,0X61,0X62,0X63,0X64,0X65,0X66,0X67,0X68,0X69,0X6A,0X6B,0X6C,0X6D,0X6E,0X6F,
0X70,0X71,0X72,0X73,0X74,0X75,0X76,0X77,0X78,0X79,0X7A,0X7B,0X7C,0X7D,0X7E,0X7F,
0X80,0X81,0X82,0X83,0X84,0X85,0X86,0X87,0X88,0X89,0X8A,0X8B,0X8C,0X8D,0X8E,0X8F,
0X90,0X91,0X92,0X93,0X94,0X95,0X96,0X97,0X98,0X99,0X9A,0X9B,0X9C,0X9D,0X9E,0X9F,
0XA0,0XA1,0XA2,0XA3,0XA4,0XA5,0XA6,0XA7,0XA8,0XA9,0XAA,0XAB,0XAC,0XAD,0XAE,0XAF,
0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39,0XBA,0XBB,0XBC,0XBD,0XBE,0XBF,
0XC0,0XC1,0XC2,0XC3,0XC4,0XC5,0XC6,0XC7,0XC8,0XC9,0XCA,0XCB,0XCC,0XCD,0XCE,0XCF,
0XD0,0XD1,0XD2,0XD3,0XD4,0XD5,0XD6,0XD7,0XD8,0XD9,0XDA,0XDB,0XDC,0XDD,0XDE,0XDF,
0XE0,0XE1,0XE2,0XE3,0XE4,0XE5,0XE6,0XE7,0XE8,0XE9,0XEA,0XEB,0XEC,0XED,0XEE,0XEF,
0XF0,0XF1,0XF2,0XF3,0XF4,0XF5,0XF6,0XF7,0XF8,0XF9,0XFA,0XFB,0XFC,0XFD,0XFE,0XFF,
};
                       /*---------------------------*/
                       /* END OF TRANSLATION TABLES */
                       /*---------------------------*/


/***********************START OF SPECIFICATIONS******************************/
/*                                                                          */
/* SUBROUTINE  NAME:  NLSCONVERTBIDINUMERICS                                */
/*                                                                          */
/*  COPYRIGHT: (c) IBM Corp. 1989                                           */
/*                                                                          */
/* DESCRIPTIVE NAME:  Convert numerics in a bidirectional string to Arabic  */
/*                    or Hindi according to Effect and BiDiAttributes       */
/*                                                                          */
/* FUNCTION: Conversion from Arabic to Hindi takes place                    */
/*           if Effect is 0 and the numeral shapes in BiDiAttributes        */
/*           denote Hindi numerics. If Effect is 0 and the numeral shapes   */
/*           denote Arabic numerics, the source string is copied to the     */
/*           target as is. If Effect is 1 , the source is converted from    */
/*           Hindi to Arabic regardless of the numeral shapes in the        */
/*           BiDiAttributes.The converted string is placed in Target.       */
/*                                                                          */
/* ENTRY POINT: NlsConvertBidiNumerics                                      */
/*    LINKAGE : CALL FAR (dynamic API link)                                 */
/*                                                                          */
/* INPUT:   (BiDiAtts, Effect, Source,Target,Length,Increment)              */
/*          BiDiAtts  DWORD    -Bidirectional attributes (Arabic/Hindi      */
/*                              numerics is considered)                     */
/*          Reserved  DWORD    -reseved and should = zero                   */
/*          @Source   CHAR     -pointer to the source string                */
/*          @Target   CHAR     -pointer to the target string                */
/*          Length    DWORD    -length of the string                        */
/*          Increment DWORD    -cell size                                   */
/*                                                                          */
/* EXIT-NORMAL: RC = 0                                                      */
/*                                                                          */
/* EXIT-ERROR:  RC = ERROR_BIDI_INVLALID_LENGTH        -invalid length      */
/*              RC = ERROR_BIDI_INVALID_INCREMENT      -invalid increment   */
/*              RC = ERROR_BIDI_INVALID_CSD            -invalid CSD         */
/*              RC = ERROR_BIDI_INVALID_ORIENTATION    -invalid orientation */
/*              RC = ERROR_BIDI_INVALID_NUM_SHAPE      -invalid numerics    */
/*              RC = ERROR_BIDI_INVALID_LEVEL          -invalid level       */
/*              RC = ERROR_BIDI_INVALID_TYPE_SUPPORT   -invalid support     */
/*              RC = ERROR_BIDI_INVALID_RESERVED       -invalid reserved    */
/*                                                                          */
/* INTERNAL REFERENCES:                                                     */
/*   LOCAL DATA DEFINITION:                                                 */
/*       CHAR     @tableptr             - pointer to the translation table  */
/*                NumShape              - numeral shapes of BiDiAtts        */
/*       DWORD    RC                    - return code from API              */
/*                j                     - loop counter for attributes within*/
/*                                        one cell                          */
/*                i                     - loop counter for bytes within the */
/*                                        source and target                 */
/*       WORD   ArabicToHindiConversion - flag indicating whether numerics  */
/*                                        conversion is from Arabic to      */
/*                                        Hindi or not.                     */
/*       WORD   HindiToArabicConversion - flag indicating whether numerics  */
/*                                        conversion is from Hindi to       */
/*                                        Arabic or not.                    */
/*                                                                          */
/*                                                                          */
/*   GLOBAL DATA DEFINITION:    NONE                                        */
/*   ROUTINES:                  ValidNumerics -performs error checking on   */
/*                                             the numerics field of the    */
/*                                             BiDiAtts                     */
/*                              RCLenInc      -performs error checking on   */
/*                                             Length and Increment         */
/*                              GetByte       -Gets a specific byte of      */
/*                                             BiDiAtts                     */
/* EXTERNAL REFERENCES:                                                     */
/*   ROUTINES:                                                              */
/*                                                                          */
/*                                                                          */
/****************************END OF SPECIFICATIONS***************************/
USHORT far pascal _loadds NlsConvertBidiNumerics ( ULONG BidiAtts,
                                                   LONG Reserved,
                                                   PUCHAR Source ,
                                                   PUCHAR Target ,
                                                   ULONG Length  ,
                                                   ULONG Increment )

{
   unsigned long i,j;
   USHORT  RC;
   char    far * tableptr;
   char    NumShape;

   /******************** Start of Error Checking ****************************/

   /*********** This check is modified after the DCR changes 14/1/1991 *****/

   if (Reserved !=0)
      return ERROR_BIDI_INVALID_RESERVED;
   /************************************************************************/
                                /* checking on length and increment */
   if ((RC=(USHORT)RCLenInc(Length,Increment))!=0)
      return RC;

                                /* check on numerics field of BiDiAttributes */
   if(!ValidNumerics((char)(GetByte((char *)&BidiAtts,1) & 0xf0)) )
                         return ERROR_BIDI_INVALID_NUM_SHAPE;

   /******************** End  of Error Checking ****************************/

   NumShape =GetByte ((char *)&BidiAtts,1)& 0xf0;

   switch (NumShape) {
     case PASSTHRU:
        j=1;                                       /* source string == target */
        for (i=0;(i<Length)&&(j==1);i++ )
           if (Source [i]!=Target [i])
              j=0;                                 /* source string <> target */

        if (!j)                            /* source and target are different */
           if (Source <Target )            /* source starts before target     */
              for (i=Length;i>0 ;i-- )     /* copy backwards                  */
                 Target [i-1]=Source [i-1];
           else                            /* target starts before/at target  */
              for (i=0;i<Length ;i++ )     /* copy from start of string to end*/
                 Target [i]=Source [i];
     break;
     case HINDI:
         if (Source <Target )                   /*backward conversion     */
            for (i=Length;i>0 ; i--)
              {
                for (j=0;j<Increment-1 ;j++,i-- ) /*copy attributes as is   */
                  Target [i-1] = Source [i-1];
                                                /*convert Source [i-1]     */
                Target [i-1] = *(ArabicToHindiTable+Source [i-1]);
              }
         else                                   /*convert from start of   */
            for (i=0;i<Length ; )               /* string to end          */
              {
                                                /*convert Source [i]       */
                Target [i] = *(ArabicToHindiTable+Source [i]);
                i++;
                for (j=0;j<Increment-1 ;j++,i++ ) /*copy attributes as is   */
                  Target [i] = Source [i];
              }
     break;
     case ARABIC:
           tableptr = HindiToArabicTable; /* pointer to H/A translation table */
         if (Source <Target )                   /*backward conversion     */
            for (i=Length;i>0 ; i--)
              {
                for (j=0;j<Increment-1 ;j++,i-- ) /*copy attributes as is   */
                  Target [i-1] = Source [i-1];
                                                /*convert Source [i-1]     */
                Target [i-1] = *(HindiToArabicTable+Source [i-1]);
              }
         else                                   /*convert from start of   */
            for (i=0;i<Length ; )               /* string to end          */
              {
                                                /*convert Source [i]       */
                Target [i] = *(HindiToArabicTable+Source [i]);
                i++;
                for (j=0;j<Increment-1 ;j++,i++ ) /*copy attributes as is   */
                  Target [i] = Source [i];
              }
     break;
   } /* endswitch */
  return 0;
}
