home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / WAIS / ir / wmessage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  3.1 KB  |  126 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.    3.26.90
  5. */
  6.  
  7. /* This file is for reading and writing the wais packet header.
  8.  * Morris@think.com
  9.  */
  10.  
  11. /* to do:
  12.  *  add check sum
  13.  *  what do you do when checksum is wrong?
  14.  */
  15.  
  16. #include <string.h>
  17. #include "wmessage.h"
  18. #include "ustubs.h"
  19. #include "cutil.h"
  20.  
  21. /*---------------------------------------------------------------------*/
  22.  
  23. void 
  24. readWAISPacketHeader(msgBuffer,header_struct)
  25. char* msgBuffer;
  26. WAISMessage *header_struct;
  27. {
  28.   /* msgBuffer is a string containing at least HEADER_LENGTH bytes. */
  29.             
  30.   memmove(header_struct->msg_len,msgBuffer,(size_t)10); 
  31.   header_struct->msg_type = char_downcase((unsigned long)msgBuffer[10]);
  32.   header_struct->hdr_vers = char_downcase((unsigned long)msgBuffer[11]);
  33.   memmove(header_struct->server,(void*)(msgBuffer + 12),(size_t)10);
  34.   header_struct->compression = char_downcase((unsigned long)msgBuffer[22]);
  35.   header_struct->encoding = char_downcase((unsigned long)msgBuffer[23]);
  36.   header_struct->msg_checksum = char_downcase((unsigned long)msgBuffer[24]);
  37. }
  38.  
  39. /*---------------------------------------------------------------------*/
  40.  
  41. long
  42. getWAISPacketLength(header)
  43. WAISMessage* header;
  44. /* interpret the length field, this is necessary since the lenght in the
  45.    message is not null terminated, so atol() may get confused.
  46.  */
  47.   char lenBuf[11];
  48.   memmove(lenBuf,header->msg_len,(size_t)10);
  49.   lenBuf[10] = '\0';
  50.   return(atol(lenBuf));
  51. }
  52.  
  53. /*---------------------------------------------------------------------*/
  54.  
  55. #ifdef NOTUSEDYET
  56.  
  57. static char checkSum _AP((char* string,long len));
  58.  
  59. static char
  60. checkSum(string,len)
  61. char* string;
  62. long len;
  63. /* XXX the problem with this routine is that it can generate 
  64.    non-ascii values.  Since these values are not being hexized,
  65.    they can (and will) hang up some communication channels.
  66.    */
  67. {
  68.   register long i;
  69.   register char chSum = '\0';
  70.       
  71.   for (i = 0; i < len; i++)
  72.     chSum = chSum ^ string[i];
  73.         
  74.   return(chSum);
  75. }    
  76. #endif /* def NOTUSEDYET */
  77.  
  78. /* this modifies the header argument.  See wais-message.h for the different
  79.  * options for the arguments.
  80.  */
  81.  
  82. void
  83. writeWAISPacketHeader(header,
  84.               dataLen,
  85.               type,
  86.               server,
  87.               compression,
  88.               encoding,
  89.               version)
  90. char* header;
  91. long dataLen;
  92. long type;
  93. char* server;
  94. long compression;
  95. long encoding;
  96. long version;
  97. /* Puts together the new wais before-the-z39-packet header. */
  98. {
  99.   char lengthBuf[11];
  100.   char serverBuf[11];
  101.  
  102.   long serverLen = strlen(server);
  103.   if (serverLen > 10)
  104.     serverLen = 10;
  105.  
  106.   sprintf(lengthBuf, "%010ld", dataLen);  
  107.   strncpy(header,lengthBuf,10);
  108.  
  109.   header[10] = type & 0xFF; 
  110.   header[11] = version & 0xFF;
  111.  
  112.   strncpy(serverBuf,server,serverLen);       
  113.   strncpy((char*)(header + 12),serverBuf,serverLen);
  114.  
  115.   header[22] = compression & 0xFF;    
  116.   header[23] = encoding & 0xFF;    
  117.   header[24] = '0'; /* checkSum(header + HEADER_LENGTH,dataLen);   XXX the result must be ascii */    
  118. }              
  119.               
  120. /*---------------------------------------------------------------------*/
  121.  
  122.  
  123.  
  124.  
  125.