The Relogix translation

Note: This is an excerpt exactly as produced by Relogix, with no human intervention to clean up the code after translation. Relogix has automatically chosen the types of variables, detected the use of structures and pointers, created proper C function calls, and has placed the assembler comments in the translated source allowing for the re-arrangement of the code flow.

Note that MicroAPL currently offers Relogix/MF as a porting service. The output of the translator as shown here would be cleaned up by our engineers before delivery to the customer. Typical post-translation cleanup work includes removing endian dependencies from the code, improving the choice of variable names and slight changes to improve code readability.



    /** encoded data is written to stream in reversed order                   
      * Thus code X'71' (fixed huff x41=A) 01110001 --> 10001110              
      * this reversed code of 10001110 is written in bit chunks as            
      * thus if the current writing byte is ?????011 then written as          
      * 01110011 and then ?????100 where the next byte will fillin ?s          */
    ptr2 = huffllen;                             /* ptr fixed huffman table to be built  */
    i = 0x30;                                    /* value starting point  */
    for (j = 0; j < 144; j++)
        *ptr2++ = i++;                           /* table  */
    i = 0x90;                                    /* value starting point  */
    for (j = 0; j < 112; j++)
        *ptr2++ = i++;                           /* table  */
    i = 0x0;                                     /* value starting point  */
    for (j = 0; j < 24; j++)
        *ptr2++ = i++;                           /* table  */
    i = 0xc0;                                    /* value starting point  */
    for (j = 0; j < 8; j++)
        *ptr2++ = i++;                           /* table  */
    ptr2 = huffllen;                             /* ptr fixed huffman table to be built  */

    for (j = 0; j < 288; j++) {
        the_flags1 = 128;                        /* left to right  */
        the_flags2 = 1;                          /* right to left  */
        dword [0] = 0;                           /* use as workarea to reverse bits  */

        for (i = 8; i > 0; i--) {
            /* is bit 0 or 1
               zero */
            if (*ptr2 & the_flags1)
                dword [0] |= the_flags2;         /* reverse bit 1  */
            the_flags1 >>= 1;                    /* test next bit  */
            the_flags2 *= 2;                     /* to set next bit 
                                                     loop */
        }

        *ptr2++ = dword [0];                     /* fix up with reversed byte  */
    }

    qsam_open_file (&input, MODE_INPUT);

    /**OPEN (SNAPDCB,OUTPUT) */
    servretv = 0;
    servretc = 0;
    servreas = 0;
    bpx1opn (&pathnlen, pathname, fileoptn, filemode, &servretv, &servretc, &servreas);

    /* FD OR IF -1 THEN ERROR */
    if (servretv < 0)
        return;
    fd = servretv;                               /* SAVE FD  */
    do {
        buffrptr = buffrbgn;
        bitctr = 8;                              /* reset constant  */
        for (k = 0; k < 16; k++)
            bitbuckt [k] = 0;
        ptr2 = buffrptr;
        for (k = 0; k < 32; k++)
            dword [k] = 0;
        curroff = 0;
        nextoff = 0;
        totalchr = 0;
        memset (blnkdupl, 0, 258 * 4);
        memset (dashdupl, 0, 258 * 4);
        memset (stardupl, 0, 258 * 4);
        memset (equldupl, 0, 258 * 4);
        memset (zerodupl, 0, 258 * 4);
        memset (hbardupl, 0, 258 * 4);
        time_dec (dword, DATETYPE_YYYYMMDD);

        /** pd time-hhmmssthmiju0000 date-yyyymmdd */
        for (k = 0; k < 3; k++)
            dword [k] = cnvbin [dword [k]];      /* convert pd time to bin  */
        for (k = 0; k < 3; k++)
            dword [k + 9] = cnvbin [dword [k + 9]];
                                                 /* convert pd date to bin  */
        hhmmss = (dword [0] << 11) | (dword [1] * 32);
                                                 /* hh 
                                                     mm */
        hhmmss |= (dword [2] >> 1);              /* ss 
                                                     /2 */
        *((char *) &centfhdr.filetime + 1) = (unsigned short) hhmmss >> 8;
                                                 /* INTEL reversed bytes order  */
        *(char *) &centfhdr.filetime = hhmmss;   /* ...  */
        yymmdd = ((dword [9] + 9) * 1024) | (dword [10] * 32);
                                                 /* yy 
                                                     adjust year for zips
                                                     mm */
        yymmdd |= dword [11];                    /* dd  */
        *((char *) &centfhdr.filedate + 1) = (unsigned short) yymmdd >> 8;
                                                 /* INTEL reversed bytes order  */
        *(char *) &centfhdr.filedate = yymmdd;   /* ...  */
        *(long *) ptr2 = loclsign;
        _rlx_move_mem (&ptr2 [4], centfhdr.loclfhdr, LOCLFSIZ);
        ptr2 += LOCLFSIZ + 4;
        filenptr = ptr2;                         /* save ptr to current filename  */
        _rlx_move_mem (ptr2, ebcdic_string ("block"), 5);
        the_centfcn2 = (short) centfhdr.centfcnt & 0xffff;
                                                 /* update total file count  */
        _rlx_binary_to_packed (the_centfcn2, dword, 8);
        centfhdr.centfcnt = ++the_centfcn2;
        centfhdr.centfcn2 = the_centfcn2;
        _rlx_packed_to_zoned (&ptr2 [5], 7, &dword [4], 4);
        ptr2 [11] |= EBCDIC_0;
        ptr3 = ptr2;
        for (k = 0; k < 12; k++)
            ptr3 [k] = x0378859 [ptr3 [k]];
        _rlx_reverse_mem (dword, &ptr2 [5], 7);  /* remove leading zeroes  */

        if (memcmp (asczeros, &ptr2 [5], 6) == 0)
            i = 1;                               /* min len of file literal  */
        else if (memcmp (asczeros, &ptr2 [5], 5) == 0)
            i = 2;                               /* incr filename len  */
        else if (memcmp (asczeros, &ptr2 [5], 4) == 0)
            i = 3;                               /* incr filename len  */
        else if (memcmp (asczeros, &ptr2 [5], 3) == 0)
            i = 4;                               /* incr filename len  */
        else if (memcmp (asczeros, &ptr2 [5], 2) == 0)
            i = 5;                               /* incr filename len  */
        else if ((unsigned char) asczeros [0] == ptr2 [5])
            i = 6;                               /* incr filename len  */
        else
            i = 7;                               /* incr filename len  */

        ptr3 = &dword [--i];                     /* last valid char  */
        _rlx_reverse_mem (&ptr2 [5], &ptr3 [-i], i + 1);
        ptr3 = ptr2;                             /* save ptr to block???? filename for laterusage  */
        ptr4 = ptr2 - 4;                         /* calc ptr to filename len field 
                                                     backtrack */
        ptr2 += i + 6;                           /* ptr  */
        i += 6;                                  /* total file name len  */
        ptr4 [0] = i;                            /* new len  */
        *(char *) &centfhdr.filenlen = i;        /* central dir filenamelen  */
        xdirmap_ptr = dirptr;                    /* start of current saved dir info area  */
        xdirmap_ptr->xfiletim = centfhdr.filetime;
        xdirmap_ptr->xfiledte = centfhdr.filedate;
        xdirmap_ptr->xfilenln = centfhdr.filenlen;
        _rlx_reverse_mem (&xdirmap_ptr->xfilelfo, &centfhdr.fileloff, 4);
                                                 /* save total local filehdr offset  */
        _rlx_move_mem (xdirmap_ptr->xfilefnc, ptr3, i);
                                                 /* save filename len #  for later usage 
                                                     for ex of filename
                                                     ...
                                                     continue */
        buffrptr = ptr2;
        comprptr = ptr2;                         /* save ptr to start compressed data  */

        /** setup '01'b and '1'b fixed codes; last block */
        bitwritr (3, 3);                         /* bit sequence='011'b 
                                                     bit count - rightmost 3 bits
                                                     ... */