Advertisement
bejiitas_wrath

DES.C

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