home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / HyperCard / CRCValue XFCN 1.0.2 / CRCValue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  2.3 KB  |  102 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     CRCValue XFCN
  4.     version 1.0.2
  5.     
  6.     Written by: Paul Celestin
  7.     
  8.     Copyright © 1993-1996 Celestin Company, Inc.
  9.     
  10.     This XFCN returns a 16 bit CRC of a specific piece of text.
  11.     
  12.     Requires one parameter: source text.
  13.     
  14.     931204 - 1.0.0 - initial release
  15.     951215 - 1.0.1 - updated for CW7
  16.     960704 - 1.0.2 - updated for CW9
  17.  
  18. ---------------------------------------------------------------------- */
  19.  
  20. #include <A4Stuff.h>
  21. #include <HyperXCmd.h>
  22.  
  23. #define PARAMETER_NUMS        1
  24. #define PARAMETER_TEXT        "\pRequires one parameter: source text."
  25. #define BYTEMASK            0xFF
  26. #define CRC_CONSTANT        0x1021
  27. #define WORDMASK            0xFFFF
  28. #define WORDBIT                0x10000
  29.  
  30.  
  31. /* ----------------------------------------------------------------------
  32. prototypes
  33. ---------------------------------------------------------------------- */
  34. void DoIt(XCmdPtr paramPtr);
  35. void CalcCRC(unsigned short v);
  36.  
  37.  
  38. /* ----------------------------------------------------------------------
  39. globals
  40. ---------------------------------------------------------------------- */
  41.  
  42. unsigned short CRC = 0;
  43.  
  44.  
  45. /* ----------------------------------------------------------------------
  46. main
  47. ---------------------------------------------------------------------- */
  48. pascal void main(XCmdPtr paramPtr)
  49. {
  50.     Str255 copyright = "\pCopyright © 1993-1996 Celestin Company, Inc.";
  51.     long oldA4 = SetCurrentA4();
  52.     if (paramPtr->paramCount != PARAMETER_NUMS)
  53.     {
  54.         paramPtr->returnValue =
  55.             PasToZero(paramPtr,PARAMETER_TEXT);
  56.     }
  57.     else
  58.     {
  59.         DoIt( paramPtr );
  60.     }
  61.     SetA4(oldA4);
  62. }
  63.  
  64. /* ----------------------------------------------------------------------
  65. DoIt
  66. ---------------------------------------------------------------------- */
  67. void DoIt(XCmdPtr paramPtr)
  68. {
  69.     char        *p;
  70.     Str255        myString;
  71.  
  72.     MoveHHi(paramPtr->params[0]);
  73.     p = *(paramPtr->params[0]);
  74.     while (*p)
  75.     {
  76.         CalcCRC(*p++);
  77.     }
  78.     NumToStr(paramPtr,CRC,myString);
  79.     paramPtr->returnValue = PasToZero(paramPtr,myString);
  80.  
  81. }
  82.  
  83.  
  84. /* ----------------------------------------------------------------------
  85. CalcCRC
  86. ---------------------------------------------------------------------- */
  87. void CalcCRC(unsigned short v)
  88. {
  89.     register int i;
  90.     register unsigned short temp = CRC;
  91.  
  92.     for (i = 0; i < 8; i++)
  93.     {
  94.         v <<= 1;
  95.         if ((temp <<= 1) & WORDBIT)
  96.             temp = (temp & WORDMASK) ^ CRC_CONSTANT;
  97.         temp ^= (v >> 8);
  98.         v &= BYTEMASK;
  99.     }
  100.     CRC = temp;
  101. }
  102.