Announcement

Collapse
No announcement yet.

Some port help...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Some port help...

    Been doing wome work on a port and I just want to make sure I'm
    doing it right and I need some help on a few sections of the code.

    Code:
    unsigned char tab1[256] =
    {
           0x33,0x73,0x3b,0x26,0x63,0x23,0x6b,0x76,0x3e,0x7e,0x36,0x2b,0x6e,0x2e,0x66,0x7b,
           0xd3,0x93,0xdb,0x06,0x43,0x03,0x4b,0x96,0xde,0x9e,0xd6,0x0b,0x4e,0x0e,0x46,0x9b,
           0x57,0x17,0x5f,0x82,0xc7,0x87,0xcf,0x12,0x5a,0x1a,0x52,0x8f,0xca,0x8a,0xc2,0x1f,
           0xd9,0x99,0xd1,0x00,0x49,0x09,0x41,0x90,0xd8,0x98,0xd0,0x01,0x48,0x08,0x40,0x91,
           0x3d,0x7d,0x35,0x24,0x6d,0x2d,0x65,0x74,0x3c,0x7c,0x34,0x25,0x6c,0x2c,0x64,0x75,
           0xdd,0x9d,0xd5,0x04,0x4d,0x0d,0x45,0x94,0xdc,0x9c,0xd4,0x05,0xc,0x0c,0x44,0x95,
           0x59,0x19,0x51,0x80,0xc9,0x89,0xc1,0x10,0x58,0x18,0x50,0x81,0xc8,0x88,0xc0,0x11,
           0xd7,0x97,0xdf,0x02,0x47,0x07,0x4f,0x92,0xda,0x9a,0xd2,0x0f,0x4a,0x0a,0x42,0x9f,
           0x53,0x13,0x5b,0x86,0xc3,0x83,0xcb,0x16,0x5e,0x1e,0x56,0x8b,0xce,0x8e,0xc6,0x1b,
           0xb3,0xf3,0xbb,0xa6,0xe3,0xa3,0xeb,0xf6,0xbe,0xfe,0xb6,0xab,0xee,0xae,0xe6,0xfb,
           0x37,0x77,0x3f,0x22,0x67,0x27,0x6f,0x72,0x3a,0x7a,0x32,0x2f,0x6a,0x2a,0x62,0x7f,
           0xb9,0xf9,0xb1,0xa0,0xe9,0xa9,0xe1,0xf0,0xb8,0xf8,0xb0,0xa1,0xe8,0xa8,0xe0,0xf1,
           0x5d,0x1d,0x55,0x84,0xcd,0x8d,0xc5,0x14,0x5c,0x1c,0x54,0x85,0xcc,0x8c,0xc4,0x15,
           0xbd,0xfd,0xb5,0xa4,0xed,0xad,0xe5,0xf4,0xbc,0xfc,0xb4,0xa5,0xec,0xac,0xe4,0xf5,
           0x39,0x79,0x31,0x20,0x69,0x29,0x61,0x70,0x38,0x78,0x30,0x21,0x68,0x28,0x60,0x71,
           0xb7,0xf7,0xbf,0xa2,0xe7,0xa7,0xef,0xf2,0xba,0xfa,0xb2,0xaf,0xea,0xaa,0xe2,0xff
    };
    I translated that kinda roughly to:

    Code:
    GLOBAL tab1() AS BYTE
    
    FUNCTION filltab1() AS LONG
        DIM i&
    
        FOR i& = 0 TO (8? * 32?)
            REDIM tab1(i&)
            tab1(i&) = VAL(READ$(i&))
        NEXT
    
    DATA &H00, &H24, &H49, &H6d, &H92, &Hb6, &Hdb, &Hff, &H00, &H24, &H49, &H6d, &H92, &Hb6, &Hdb, &Hff
    DATA &Hd3, &H93, &Hdb, &H06, &H43, &H03, &H4b, &H96, &Hde, &H9e, &Hd6, &H0b, &H4e, &H0e, &H46, &H9b
    DATA &H57, &H17, &H5f, &H82, &Hc7, &H87, &Hcf, &H12, &H5a, &H1a, &H52, &H8f, &Hca, &H8a, &Hc2, &H1f
    DATA &Hd9, &H99, &Hd1, &H00, &H49, &H09, &H41, &H90, &Hd8, &H98, &Hd0, &H01, &H48, &H08, &H40, &H91
    DATA &H3d, &H7d, &H35, &H24, &H6d, &H2d, &H65, &H74, &H3c, &H7c, &H34, &H25, &H6c, &H2c, &H64, &H75
    DATA &Hdd, &H9d, &Hd5, &H04, &H4d, &H0d, &H45, &H94, &Hdc, &H9c, &Hd4, &H05, &Hc, &H0c, &H44, &H95
    DATA &H59, &H19, &H51, &H80, &Hc9, &H89, &Hc1, &H10, &H58, &H18, &H50, &H81, &Hc8, &H88, &Hc0, &H11
    DATA &Hd7, &H97, &Hdf, &H02, &H47, &H07, &H4f, &H92, &Hda, &H9a, &Hd2, &H0f, &H4a, &H0a, &H42, &H9f
    DATA &H53, &H13, &H5b, &H86, &Hc3, &H83, &Hcb, &H16, &H5e, &H1e, &H56, &H8b, &Hce, &H8e, &Hc6, &H1b
    DATA &Hb3, &Hf3, &Hbb, &Ha6, &He3, &Ha3, &Heb, &Hf6, &Hbe, &Hfe, &Hb6, &Hab, &Hee, &Hae, &He6, &Hfb
    DATA &H37, &H77, &H3f, &H22, &H67, &H27, &H6f, &H72, &H3a, &H7a, &H32, &H2f, &H6a, &H2a, &H62, &H7f
    DATA &Hb9, &Hf9, &Hb1, &Ha0, &He9, &Ha9, &He1, &Hf0, &Hb8, &Hf8, &Hb0, &Ha1, &He8, &Ha8, &He0, &Hf1
    DATA &H5d, &H1d, &H55, &H84, &Hcd, &H8d, &Hc5, &H14, &H5c, &H1c, &H54, &H85, &Hcc, &H8c, &Hc4, &H15
    DATA &Hbd, &Hfd, &Hb5, &Ha4, &Hed, &Had, &He5, &Hf4, &Hbc, &Hfc, &Hb4, &Ha5, &Hec, &Hac, &He4, &Hf5
    DATA &H39, &H79, &H31, &H20, &H69, &H29, &H61, &H70, &H38, &H78, &H30, &H21, &H68, &H28, &H60, &H71
    DATA &Hb7, &Hf7, &Hbf, &Ha2, &He7, &Ha7, &Hef, &Hf2, &Hba, &Hfa, &Hb2, &Haf, &Hea, &Haa, &He2, &Hff
    END FUNCTION
    Not sure if that's the right procedure.

    Another part of the code I need some help with in order to proceed is this:

    Code:
    void descramble(unsigned char *sec, unsigned char *key)
    {
    
           unsigned int t1, t2, t3, t4, t5, t6;
           unsigned char * end = sec + 0x800;
           t1 = key[0] ^ sec[0x54] | 0x100;
    
           t2 = key[1] ^ sec[0x55];
           t3 = (*((unsigned int *)(key + 2))) ^ (*((unsigned int *)(sec + 0x56)));
           t4 = t3 & 7;
           t3 = t3 * 2 + 8 - t4;
           sec += 0x80;
           t5 = 0;
           while(sec != end)
           {
                   t4 = tab2[t2] ^ tab3[t1];
                   t2 = t1 >> 1;
                   t1 = ((t1 & 1) << 8) ^ t4;
                   t4 = tab5[t4];
                   t6 = (((((((t3 >> 3) ^ t3) >> 1) ^ t3) >> 8) ^ t3) >> 5) & 0xff;
                   t3 = (t3 << 8) | t6;
                   t6 = tab4[t6];
                   t5 += t6 + t4;
                   * sec++= tab1[*sec] ^ (t5&0xff);
                   t5 >> = 8;
           }
    
    }
    It would probably look something like
    Code:
    FUNCTION Descramble(sec() AS BYTE, key() AS BYTE) AS LONG
        DIM t1 AS LONG, t2 AS LONG, t3 AS LONG, t4 AS LONG, t5 AS LONG, t6 AS LONG
    ...
    ...
    But then I'm not so sure about alot of the following since I keep
    getting error messages on most other stuff I try.
    Any experienced C/C++ - PB coder that are willing to give me a hand?


    /Dennis

    ------------------

  • #2
    Dennis,

    Don't redim tab1 is the middle of the loop, 'cause your tossing out the data you just read. Also, the data statements don't have the same values as the c code at the top.

    Russ Srole

    ------------------
    "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

    Comment


    • #3
      Or add the "preserve" keyword

      ------------------
      Paul Dwyer
      Network Engineer
      Aussie in Tokyo

      Comment


      • #4
        Thanks guys.

        Does anyone have any tips/ideas/etc for the later code:

        [CODE]
        void descramble(unsigned char *sec, unsigned char *key)
        {
        unsigned int t1, t2, t3, t4, t5, t6;
        unsigned char * end = sec + 0x800;
        t1 = key[0] ^ sec[0x54] | 0x100;
        t2 = key[1] ^ sec[0x55];
        t3 = (*((unsigned int *)(key + 2))) ^ (*((unsigned int *)(sec + 0x56)));
        t4 = t3 & 7;
        t3 = t3 * 2 + 8 - t4;
        sec += 0x80; t5 = 0;
        while(sec != end)
        {
        t4 = tab2[t2] ^ tab3[t1];
        t2 = t1 >> 1;
        t1 = ((t1 & 1) << 8) ^ t4;
        t4 = tab5[t4];
        t6 = (((((((t3 >> 3) ^ t3) >> 1) ^ t3) >> 8) ^ t3) >> 5) & 0xff;
        t3 = (t3 << 8) | t6;
        t6 = tab4[t6];
        t5 += t6 + t4;
        * sec++= tab1[*sec] ^ (t5&0xff);
        t5 >> = 8;
        }
        }



        ------------------

        Comment


        • #5
          Dennis,

          My C is a bit too rusty to do a complete translation on that subroutine, but I think the following may help:

          Any hex number denoted 0x[whatever] can be changed to &H[whatever]. Example: 0x800 can be changed to &H800 or the decimal equivalent of 2048.

          The >> and << operators are shift right and shift left operators. Though it does not operate exactly like the C/C++ shift operators, the SHIFT statement in the help file is what you need for these.

          The | operator is the OR operator, and PowerBASIC has an OR operator itself. Check the help file.

          I believe that the ^ operator is the exponent operator. If that is the case, PowerBASIC's is identical.

          The C & operator is equivalent to the PB AND operator. (Check Help file)

          I belive that var += something is equivalent to saying var=var+something.

          I belive unsigned int is equivalent to the PB DWORD data type.

          Hopefully to help you get started, here is how a few of the lines would translate:

          Code:
          t6 = (((((((t3 >> 3) ^ t3) >> 1) ^ t3) >> 8) ^ t3) >> 5) & 0xff;
          
          
          t6=t3
          SHIFT RIGHT t6,3
          t6=t6^t3
          SHIFT RIGHT t6,1
          t6=t6^t3
          SHIFT RIGHT t6,8
          t6=t6^t3
          SHIFT RIGHT t6,5
          t6=t6 AND &HFF
          
          
          t3 = (t3 << 8) | t6;
          
          
          SHIFT LEFT t3,8
          t3=t3 OR t6
          ------------------


          [This message has been edited by Adam J. Drake (edited December 17, 2003).]
          Adam Drake
          Drake Software

          Comment


          • #6
            Thanks Adam

            That took me a bit further.
            I played around a little with the info you gave me and this is
            what I came up with:

            Code:
            FUNCTION descramble(sec() AS BYTE, key() AS BYTE) AS LONG
                DIM t1 AS DWORD, t2 AS DWORD, t3 AS DWORD, t4 AS DWORD, t5 AS DWORD, t6 AS DWORD
                DIM ends() AS BYTE
            
                ends? = sec? + &H800
            
                t1 = key(0) ^ sec(&H54) OR &H100
                t2 = key(1) ^sec(&H55)
                't3 = (*((unsigned int *)(key + 2))) ^ (*((unsigned int *)(sec + 0x56)));
                t4 = t3 AND 7
                t3 = t3 * 2 + 8 - t4
                sec? = sec? + &H80
                t5 = 0
            
                DO WHILE sec? >= ends?
                    t4 = tab2(t2) ^ tab3(t1)
                    t2 = t1
                    SHIFT RIGHT t2,1
                    t1 = t1 AND 1
                    SHIFT LEFT 8
                    t1 = t1 ^t4
                    t4 = tab5(t4)
                    t6=t3
                    SHIFT RIGHT t6,3
                    t6=t6^t3
                    SHIFT RIGHT t6,1
                    t6=t6^t3
                    SHIFT RIGHT t6,8
                    t6=t6^t3
                    SHIFT RIGHT t6,5
                    t6=t6 AND &HFF
                    SHIFT LEFT t3,8
                    t3=t3 OR t6
                    t6 = CSStab4(t6)
                    t5 = t5 + (t6 + t4)
                    '* sec++= tab1[*sec] ^ (t5&0xff);
                    SHIFT RIGHT t5, 8
                LOOP
            END FUNCTION

            There are two lines I'm now sure how to port. (first t3 and the * sec at the bottom)
            And another thing I had some problems with was the sec() AS BYTE, key() AS BYTE.

            Only solution I could come up with was using sec? and key? (same with ends?)

            Other then that that part of the code compiles fine.
            Just need some more help with those few lines and to know if I'm declaring everything right or now.


            Thanks again for the great replies.


            /Dennis

            ------------------

            Comment


            • #7
              Dennis,

              It looks like key and sec are byte arrays. Our C++ expert is on vacation for a few weeks, so I can't verify that. If I remember correctly key[0], key[1] and sec[0x53] and the fact that they were passed as pointers (byref) char would indicate that they are byte arrays. Sorry I can't help on the first t3 - it's that sort of mangled thinking that keeps me from writing applications in C/C++. If someone wants to start a rant thread about those languages, I can contribute.

              Russ Srole

              ------------------
              "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

              Comment


              • #8
                sec isn't an array, it's a byte pointer.
                Code:
                    *sec++= tab1[*sec] ^ (t5&0xff);
                -->
                Code:
                    @sec = tab1(@sec) XOR (t5 AND &H0FF)
                    INCR sec
                ------------------
                Tom Hanlin
                PowerBASIC Staff

                Comment


                • #9
                  Dennis,

                  Please pay close attention to Tom's post and note my screw-up, ^ is not an exponent operator in C, it's XOR.

                  ------------------
                  Adam Drake
                  Drake Software

                  Comment

                  Working...
                  X