home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / filesbbs / os2 / plnk065.arj / PLNK065.ZIP / pilot-link.0.6.5 / libsock / utils.c < prev   
Encoding:
C/C++ Source or Header  |  1997-05-23  |  2.5 KB  |  141 lines

  1. /* utils.c:  misc. stuff for dealing with packets.
  2.  *
  3.  * (c) 1996, D. Jeff Dionne.
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12.  
  13. #ifdef NeXT
  14. # include <stdlib.h>
  15. # include <string.h>
  16. # include <assert.h>
  17. #endif
  18.  
  19. #include "pi-source.h"
  20. #include "pi-socket.h"
  21.  
  22. /* this routine ruthlessly stolen verbatim from Brian J. Swetland */
  23.  
  24. int crc16(unsigned char *ptr, int count)
  25. {
  26.   int crc, i;
  27.  
  28.   crc = 0;
  29.   while(--count >= 0) {
  30.     crc = crc ^ (int)*ptr++ << 8;
  31.     for(i = 0; i < 8; ++i)
  32.       if(crc & 0x8000)
  33.     crc = crc << 1 ^ 0x1021;
  34.       else
  35.     crc = crc << 1;
  36.   }
  37.   return (crc & 0xFFFF);
  38. }
  39.  
  40. #ifndef HAVE_STRDUP
  41. char * strdup(const char *string)
  42. {
  43.     size_t length;
  44.     char *result;
  45.  
  46.     assert(string != NULL);
  47.  
  48.     length = strlen(string) + 1;
  49.     result = malloc(length);
  50.  
  51.     if (result == NULL)
  52.     return NULL;
  53.  
  54.     memcpy(result, string, length);
  55.  
  56.     return result;
  57. }
  58. #endif
  59.  
  60. char * printlong (unsigned long val)
  61. {
  62.   static char buf[5];
  63.   set_long(buf, val);
  64.   buf[4] = 0;
  65.   return buf;
  66. }
  67.  
  68. unsigned long makelong (char * c)
  69. {
  70.   char c2[4];
  71.   int l = strlen(c);
  72.   if (l>=4)
  73.     return get_long(c);
  74.   memset(c2, ' ', 4);
  75.   memcpy(c2, c, l);
  76.   return get_long(c2);
  77. }
  78.  
  79. void dumpline (const unsigned char *buf, int len, int addr)
  80. {
  81.   int i;
  82.  
  83.   fprintf (stderr,"%.4x  ",addr);
  84.  
  85.   for (i=0;i<16;i++) {
  86.  
  87.     if (i<len) fprintf (stderr,"%.2x ",0xff & (unsigned int)buf[i]);
  88.     else fprintf (stderr,"   ");
  89.   }
  90.  
  91.   fprintf (stderr,"  ");
  92.  
  93.   for (i=0;i<len;i++) {
  94.     if (isprint(buf[i]) && (buf[i]>=32) && (buf[i]<=126)) fprintf (stderr,"%c",buf[i]);
  95.     else fprintf(stderr,".");
  96.   }
  97.   fprintf(stderr,"\n");
  98. }
  99.  
  100. void dumpdata (const unsigned char * buf, int len) {
  101.   int i;
  102.   for(i=0;i<len;i+=16) {
  103.     dumpline(buf+i, ((len-i)>16) ? 16 : len-i, i);
  104.   }
  105. }
  106.  
  107. double get_float(void * buffer) {
  108.     unsigned char * buf = buffer;
  109.  
  110.     /* Load values */
  111.     unsigned long frac = get_long(buf);
  112.     int exp = get_sshort(buf+4);
  113.     int sign = get_byte(buf+6);
  114.     
  115.     return ldexp(sign ? (double)frac : -(double)frac, exp);
  116. }
  117.  
  118. void set_float(void * buffer, double value) {
  119.     unsigned char * buf = buffer;
  120.     
  121.     unsigned long frac;
  122.     int exp, sign;
  123.     
  124.     /* Take absolute */
  125.     if (value < 0) {
  126.         sign=0;
  127.         value = -value;
  128.     } else
  129.         sign=0xFF;
  130.     
  131.     /* Convert mantissa to 32-bit integer, and take exponent */
  132.     frac = ldexp(frexp(value, &exp), 32);
  133.     exp -= 32;
  134.     
  135.     /* Store values in buffer */
  136.     set_long(buf, frac);
  137.     set_sshort(buf+4, exp);
  138.     set_byte(buf+6, sign);
  139.     set_byte(buf+7, 0);
  140. }
  141.