home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / Registration / des.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  14.0 KB  |  590 lines

  1. /* Sofware DES functions
  2.  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
  3.  * the 1977 public-domain program by Jim Gillogly
  4.  * Modified for additional speed - 6 December 1988 Phil Karn
  5.  * Modified for parameterized key schedules - Jan 1991 Phil Karn
  6.  * Callers now allocate a key schedule as follows:
  7.  *    kn = (char (*)[8])malloc(sizeof(char) * 8 * 16);
  8.  *    or
  9.  *    char kn[16][8];
  10.  */
  11. //#define    NULL    0
  12.  
  13. #ifdef    LITTLE_ENDIAN
  14. static long byteswap();
  15. #endif
  16. static void permute(),perminit(),spinit();
  17. static long f();
  18.  
  19. /* Tables defined in the Data Encryption Standard documents */
  20.  
  21. /* initial permutation IP */
  22. static char ip[] = {
  23.     58, 50, 42, 34, 26, 18, 10,  2,
  24.     60, 52, 44, 36, 28, 20, 12,  4,
  25.     62, 54, 46, 38, 30, 22, 14,  6,
  26.     64, 56, 48, 40, 32, 24, 16,  8,
  27.     57, 49, 41, 33, 25, 17,  9,  1,
  28.     59, 51, 43, 35, 27, 19, 11,  3,
  29.     61, 53, 45, 37, 29, 21, 13,  5,
  30.     63, 55, 47, 39, 31, 23, 15,  7
  31. };
  32.  
  33. /* final permutation IP^-1 */
  34. static char fp[] = {
  35.     40,  8, 48, 16, 56, 24, 64, 32,
  36.     39,  7, 47, 15, 55, 23, 63, 31,
  37.     38,  6, 46, 14, 54, 22, 62, 30,
  38.     37,  5, 45, 13, 53, 21, 61, 29,
  39.     36,  4, 44, 12, 52, 20, 60, 28,
  40.     35,  3, 43, 11, 51, 19, 59, 27,
  41.     34,  2, 42, 10, 50, 18, 58, 26,
  42.     33,  1, 41,  9, 49, 17, 57, 25
  43. };
  44.  
  45. /* expansion operation matrix
  46.  * This is for reference only; it is unused in the code
  47.  * as the f() function performs it implicitly for speed
  48.  */
  49. #ifdef notdef
  50. static char ei[] = {
  51.     32,  1,  2,  3,  4,  5,
  52.      4,  5,  6,  7,  8,  9,
  53.      8,  9, 10, 11, 12, 13,
  54.     12, 13, 14, 15, 16, 17,
  55.     16, 17, 18, 19, 20, 21,
  56.     20, 21, 22, 23, 24, 25,
  57.     24, 25, 26, 27, 28, 29,
  58.     28, 29, 30, 31, 32,  1 
  59. };
  60. #endif
  61.  
  62. /* permuted choice table (key) */
  63. static char pc1[] = {
  64.     57, 49, 41, 33, 25, 17,  9,
  65.      1, 58, 50, 42, 34, 26, 18,
  66.     10,  2, 59, 51, 43, 35, 27,
  67.     19, 11,  3, 60, 52, 44, 36,
  68.  
  69.     63, 55, 47, 39, 31, 23, 15,
  70.      7, 62, 54, 46, 38, 30, 22,
  71.     14,  6, 61, 53, 45, 37, 29,
  72.     21, 13,  5, 28, 20, 12,  4
  73. };
  74.  
  75. /* number left rotations of pc1 */
  76. static char totrot[] = {
  77.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  78. };
  79.  
  80. /* permuted choice key (table) */
  81. static char pc2[] = {
  82.     14, 17, 11, 24,  1,  5,
  83.      3, 28, 15,  6, 21, 10,
  84.     23, 19, 12,  4, 26,  8,
  85.     16,  7, 27, 20, 13,  2,
  86.     41, 52, 31, 37, 47, 55,
  87.     30, 40, 51, 45, 33, 48,
  88.     44, 49, 39, 56, 34, 53,
  89.     46, 42, 50, 36, 29, 32
  90. };
  91.  
  92. /* The (in)famous S-boxes */
  93. static char si[8][64] = {
  94.     /* S1 */
  95.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  96.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  97.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  98.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  99.  
  100.     /* S2 */
  101.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  102.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  103.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  104.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  105.  
  106.     /* S3 */
  107.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  108.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  109.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  110.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  111.  
  112.     /* S4 */
  113.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  114.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  115.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  116.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  117.  
  118.     /* S5 */
  119.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  120.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  121.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  122.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  123.  
  124.     /* S6 */
  125.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  126.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  127.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  128.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  129.  
  130.     /* S7 */
  131.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  132.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  133.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  134.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  135.  
  136.     /* S8 */
  137.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  138.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  139.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  140.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  141. };
  142.  
  143. /* 32-bit permutation function P used on the output of the S-boxes */
  144. static char p32i[] = {    
  145.     16,  7, 20, 21,
  146.     29, 12, 28, 17,
  147.      1, 15, 23, 26,
  148.      5, 18, 31, 10,
  149.      2,  8, 24, 14,
  150.     32, 27,  3,  9,
  151.     19, 13, 30,  6,
  152.     22, 11,  4, 25
  153. };
  154. /* End of DES-defined tables */
  155.  
  156. /* Lookup tables initialized once only at startup by desinit() */
  157. static long (*sp)[64];        /* Combined S and P boxes */
  158.  
  159. static char (*iperm)[16][8];    /* Initial and final permutations */
  160. static char (*fperm)[16][8];
  161.  
  162.  
  163. /* bit 0 is left-most in byte */
  164. static int bytebit[] = {
  165.     0200,0100,040,020,010,04,02,01
  166. };
  167.  
  168. static int nibblebit[] = {
  169.      010,04,02,01
  170. };
  171. static int desmode;
  172.  
  173. /* Allocate space and initialize DES lookup arrays
  174.  * mode == 0: standard Data Encryption Algorithm
  175.  * mode == 1: DEA without initial and final permutations for speed
  176.  */
  177. int
  178. desinit(mode)
  179. int mode;
  180. {
  181.     //char *malloc();
  182.  
  183.     if(sp != NULL){
  184.         /* Already initialized */
  185.         return 0;
  186.     }
  187.     desmode = mode;
  188.     
  189.     if((sp = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
  190.         return 0;
  191.     }
  192.     spinit();
  193.     if(mode == 1)    /* No permutations */
  194.         return 0;
  195.  
  196.     iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  197.     if(iperm == NULL){
  198.         free((char *)sp);
  199.         return -1;
  200.     }
  201.     perminit(iperm,ip);
  202.  
  203.     fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  204.     if(fperm == NULL){
  205.         free((char *)sp);
  206.         free((char *)iperm);
  207.         return -1;
  208.     }
  209.     perminit(fperm,fp);
  210.     
  211.     return 0;
  212. }
  213. /* Free up storage used by DES */
  214. void
  215. desdone()
  216. {
  217.     if(sp == NULL)
  218.         return;    /* Already done */
  219.  
  220.     free((char *)sp);
  221.     if(iperm != NULL)
  222.         free((char *)iperm);
  223.     if(fperm != NULL)
  224.         free((char *)fperm);
  225.  
  226.     sp = NULL;
  227.     iperm = NULL;
  228.     fperm = NULL;
  229. }
  230. /* Set key (initialize key schedule array) */
  231. int
  232. dessetkey(kn,key)
  233. char (*kn)[8];        /* Key schedule */
  234. char *key;        /* 64 bits (will use only 56) */
  235. {
  236.     char pc1m[56];        /* place to modify pc1 into */
  237.     char pcr[56];        /* place to rotate pc1 into */
  238.     register int i,j,l;
  239.     int m;
  240.  
  241.     if(kn == NULL){
  242.         return -1;
  243.     }
  244.  
  245.     /* Clear key schedule */
  246.     memset((char *)kn,0,16*8);
  247.  
  248.     for (j=0; j<56; j++) {        /* convert pc1 to bits of key */
  249.         l=pc1[j]-1;        /* integer bit location     */
  250.         m = l & 07;        /* find bit         */
  251.         pc1m[j]=(key[l>>3] &    /* find which key byte l is in */
  252.             bytebit[m])    /* and which bit of that byte */
  253.             ? 1 : 0;    /* and store 1-bit result */
  254.     }
  255.     for (i=0; i<16; i++) {        /* key chunk for each iteration */
  256.         for (j=0; j<56; j++)    /* rotate pc1 the right amount */
  257.             pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
  258.             /* rotate left and right halves independently */
  259.         for (j=0; j<48; j++){    /* select bits individually */
  260.             /* check bit that goes to kn[j] */
  261.             if (pcr[pc2[j]-1]){
  262.                 /* mask it in if it's there */
  263.                 l= j % 6;
  264.                 kn[i][j/6] |= bytebit[l] >> 2;
  265.             }
  266.         }
  267.     }
  268.     return 0;
  269. }
  270. /* In-place encryption of 64-bit block */
  271. int
  272. endes(kn,data)
  273. char (*kn)[8];        /* Key schedule */
  274. void *data;
  275. {
  276.     char    *block = data;
  277.     register long left,right;
  278.     register char *knp;
  279.     long work[2];         /* Working data storage */
  280.  
  281.     if(kn == NULL || block == NULL)
  282.         return -1;
  283.     permute(block,iperm,(char *)work);    /* Initial Permutation */
  284. #ifdef    LITTLE_ENDIAN
  285.     left = byteswap(work[0]);
  286.     right = byteswap(work[1]);
  287. #else
  288.     left = work[0];
  289.     right = work[1];
  290. #endif
  291.  
  292.     /* Do the 16 rounds.
  293.      * The rounds are numbered from 0 to 15. On even rounds
  294.      * the right half is fed to f() and the result exclusive-ORs
  295.      * the left half; on odd rounds the reverse is done.
  296.      */
  297.     knp = &kn[0][0];
  298.     left ^= f(right,knp);
  299.     knp += 8;
  300.     right ^= f(left,knp);
  301.     knp += 8;
  302.     left ^= f(right,knp);
  303.     knp += 8;
  304.     right ^= f(left,knp);
  305.     knp += 8;
  306.     left ^= f(right,knp);
  307.     knp += 8;
  308.     right ^= f(left,knp);
  309.     knp += 8;
  310.     left ^= f(right,knp);
  311.     knp += 8;
  312.     right ^= f(left,knp);
  313.     knp += 8;
  314.     left ^= f(right,knp);
  315.     knp += 8;
  316.     right ^= f(left,knp);
  317.     knp += 8;
  318.     left ^= f(right,knp);
  319.     knp += 8;
  320.     right ^= f(left,knp);
  321.     knp += 8;
  322.     left ^= f(right,knp);
  323.     knp += 8;
  324.     right ^= f(left,knp);
  325.     knp += 8;
  326.     left ^= f(right,knp);
  327.     knp += 8;
  328.     right ^= f(left,knp);
  329.  
  330.     /* Left/right half swap, plus byte swap if little-endian */
  331. #ifdef    LITTLE_ENDIAN
  332.     work[1] = byteswap(left);
  333.     work[0] = byteswap(right);
  334. #else
  335.     work[0] = right;
  336.     work[1] = left;
  337. #endif
  338.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  339.     return 0;
  340. }
  341. /* In-place decryption of 64-bit block. This function is the mirror
  342.  * image of encryption; exactly the same steps are taken, but in
  343.  * reverse order
  344.  */
  345. int
  346. dedes(kn,data)
  347. char (*kn)[8];        /* Key schedule */
  348. void *data;
  349. {
  350.     char    *block = data;
  351.     register long left,right;
  352.     register char *knp;
  353.     long work[2];    /* Working data storage */
  354.  
  355.     if(kn == NULL || block == NULL)
  356.         return -1;
  357.     permute(block,iperm,(char *)work);    /* Initial permutation */
  358.  
  359.     /* Left/right half swap, plus byte swap if little-endian */
  360. #ifdef    LITTLE_ENDIAN
  361.     right = byteswap(work[0]);
  362.     left = byteswap(work[1]);
  363. #else
  364.     right = work[0];
  365.     left = work[1];
  366. #endif
  367.     /* Do the 16 rounds in reverse order.
  368.      * The rounds are numbered from 15 to 0. On even rounds
  369.      * the right half is fed to f() and the result exclusive-ORs
  370.      * the left half; on odd rounds the reverse is done.
  371.      */
  372.     knp = &kn[15][0];
  373.     right ^= f(left,knp);
  374.     knp -= 8;
  375.     left ^= f(right,knp);
  376.     knp -= 8;
  377.     right ^= f(left,knp);
  378.     knp -= 8;
  379.     left ^= f(right,knp);
  380.     knp -= 8;
  381.     right ^= f(left,knp);
  382.     knp -= 8;
  383.     left ^= f(right,knp);
  384.     knp -= 8;
  385.     right ^= f(left,knp);
  386.     knp -= 8;
  387.     left ^= f(right,knp);
  388.     knp -= 8;
  389.     right ^= f(left,knp);
  390.     knp -= 8;
  391.     left ^= f(right,knp);
  392.     knp -= 8;
  393.     right ^= f(left,knp);
  394.     knp -= 8;
  395.     left ^= f(right,knp);
  396.     knp -= 8;
  397.     right ^= f(left,knp);
  398.     knp -= 8;
  399.     left ^= f(right,knp);
  400.     knp -= 8;
  401.     right ^= f(left,knp);
  402.     knp -= 8;
  403.     left ^= f(right,knp);
  404.  
  405. #ifdef    LITTLE_ENDIAN
  406.     work[0] = byteswap(left);
  407.     work[1] = byteswap(right);
  408. #else
  409.     work[0] = left;
  410.     work[1] = right;
  411. #endif
  412.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  413.     return 0;
  414. }
  415.  
  416. /* Permute inblock with perm */
  417. static void
  418. permute(inblock,perm,outblock)
  419. char *inblock, *outblock;        /* result into outblock,64 bits */
  420. char perm[16][16][8];            /* 2K bytes defining perm. */
  421. {
  422.     register char *ib, *ob;        /* ptr to input or output block */
  423.     register char *p, *q;
  424.     register int j;
  425.  
  426.     if(perm == NULL){
  427.         /* No permutation, just copy */
  428.         memcpy(outblock,inblock,8);
  429.         return;
  430.     }
  431.     /* Clear output block */
  432.     memset(outblock,'\0',8);
  433.  
  434.     ib = inblock;
  435.     for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */
  436.         ob = outblock;
  437.         p = perm[j][(*ib >> 4) & 0xf];
  438.         q = perm[j + 1][*ib & 0xf];
  439.         /* and each output byte, OR the masks together */
  440.         *ob++ |= *p++ | *q++;
  441.         *ob++ |= *p++ | *q++;
  442.         *ob++ |= *p++ | *q++;
  443.         *ob++ |= *p++ | *q++;
  444.         *ob++ |= *p++ | *q++;
  445.         *ob++ |= *p++ | *q++;
  446.         *ob++ |= *p++ | *q++;
  447.         *ob++ |= *p++ | *q++;
  448.     }
  449. }
  450.  
  451. /* The nonlinear function f(r,k), the heart of DES */
  452. static long
  453. f(r,subkey)
  454. register long r;    /* 32 bits */
  455. register char *subkey;    /* 48-bit key for this round */
  456. {
  457.     register long *spp;
  458.     register long rval,rt;
  459.     register int er;
  460.  
  461. #ifdef    TRACE
  462.     printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
  463.         r,
  464.         subkey[0], subkey[1], subkey[2],
  465.         subkey[3], subkey[4], subkey[5],
  466.         subkey[6], subkey[7]);
  467. #endif
  468.     /* Run E(R) ^ K through the combined S & P boxes.
  469.      * This code takes advantage of a convenient regularity in
  470.      * E, namely that each group of 6 bits in E(R) feeding
  471.      * a single S-box is a contiguous segment of R.
  472.      */
  473.     subkey += 7;
  474.  
  475.     /* Compute E(R) for each block of 6 bits, and run thru boxes */
  476.     er = ((int)r << 1) | ((r & 0x80000000) ? 1 : 0);
  477.     spp = &sp[7][0];
  478.     rval = spp[(er ^ *subkey--) & 0x3f];
  479.     spp -= 64;
  480.     rt = (unsigned long)r >> 3;
  481.     rval |= spp[((int)rt ^ *subkey--) & 0x3f];
  482.     spp -= 64;
  483.     rt >>= 4;
  484.     rval |= spp[((int)rt ^ *subkey--) & 0x3f];
  485.     spp -= 64;
  486.     rt >>= 4;
  487.     rval |= spp[((int)rt ^ *subkey--) & 0x3f];
  488.     spp -= 64;
  489.     rt >>= 4;
  490.     rval |= spp[((int)rt ^ *subkey--) & 0x3f];
  491.     spp -= 64;
  492.     rt >>= 4;
  493.     rval |= spp[((int)rt ^ *subkey--) & 0x3f];
  494.     spp -= 64;
  495.     rt >>= 4;
  496.     rval |= spp[((int)rt ^ *subkey--) & 0x3f];
  497.     spp -= 64;
  498.     rt >>= 4;
  499.     rt |= (r & 1) << 5;
  500.     rval |= spp[((int)rt ^ *subkey) & 0x3f];
  501. #ifdef    TRACE
  502.     printf(" %08lx\n",rval);
  503. #endif
  504.     return rval;
  505. }
  506. /* initialize a perm array */
  507. static void
  508. perminit(perm,p)
  509. char perm[16][16][8];            /* 64-bit, either init or final */
  510. char p[64];
  511. {
  512.     register int l, j, k;
  513.     int i,m;
  514.  
  515.     /* Clear the permutation array */
  516.     memset((char *)perm,0,16*16*8);
  517.  
  518.     for (i=0; i<16; i++)        /* each input nibble position */
  519.         for (j = 0; j < 16; j++)/* each possible input nibble */
  520.         for (k = 0; k < 64; k++)/* each output bit position */
  521.         {   l = p[k] - 1;    /* where does this bit come from*/
  522.             if ((l >> 2) != i)  /* does it come from input posn?*/
  523.             continue;    /* if not, bit k is 0     */
  524.             if (!(j & nibblebit[l & 3]))
  525.             continue;    /* any such bit in input? */
  526.             m = k & 07;    /* which bit is this in the byte*/
  527.             perm[i][j][k>>3] |= bytebit[m];
  528.         }
  529. }
  530.  
  531. /* Initialize the lookup table for the combined S and P boxes */
  532. static void
  533. spinit()
  534. {
  535.     char pbox[32];
  536.     int p,i,s,j,rowcol;
  537.     long val;
  538.  
  539.     /* Compute pbox, the inverse of p32i.
  540.      * This is easier to work with
  541.      */
  542.     for(p=0;p<32;p++){
  543.         for(i=0;i<32;i++){
  544.             if(p32i[i]-1 == p){
  545.                 pbox[p] = i;
  546.                 break;
  547.             }
  548.         }
  549.     }
  550.     for(s = 0; s < 8; s++){            /* For each S-box */
  551.         for(i=0; i<64; i++){        /* For each possible input */
  552.             val = 0;
  553.             /* The row number is formed from the first and last
  554.              * bits; the column number is from the middle 4
  555.              */
  556.             rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
  557.             for(j=0;j<4;j++){    /* For each output bit */
  558.                 if(si[s][rowcol] & (8 >> j)){
  559.                  val |= 1L << (31 - pbox[4*s + j]);
  560.                 }
  561.             }
  562.             sp[s][i] = val;
  563.  
  564. #ifdef    TRACE
  565.             printf("sp[%d][%2d] = %08lx\n",s,i,sp[s][i]);
  566. #endif
  567.         }
  568.     }
  569. }
  570. #ifdef    LITTLE_ENDIAN
  571. /* Byte swap a long */
  572. static long
  573. byteswap(x)
  574. unsigned long x;
  575. {
  576.     register char *cp,tmp;
  577.  
  578.     cp = (char *)&x;
  579.     tmp = cp[3];
  580.     cp[3] = cp[0];
  581.     cp[0] = tmp;
  582.  
  583.     tmp = cp[2];
  584.     cp[2] = cp[1];
  585.     cp[1] = tmp;
  586.  
  587.     return x;
  588. }
  589. #endif
  590.