⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htvms_waisui.c

📁 用于linux和其他unix下面的
💻 C
📖 第 1 页 / 共 4 页
字号:
  padding = size - needed;  i = padding - 1;  for (i = padding - 1;i >= 0;i--)    { buf[i] = pad;    }  buf = writeCompressedInteger(num,buf + padding,len);  return(buf);}/*----------------------------------------------------------------------*/unsigned longwrittenCompressedIntSize(unsigned long num)/* return the number of bytes needed to represnet the value num in   compressed format.  curently limited to 4 bytes */{  if (num < CompressedInt1Byte)    return(1);  else if (num < CompressedInt2Byte)    return(2);  else if (num < CompressedInt3Byte)    return(3);  else    return(4);}/*----------------------------------------------------------------------*/char*writeTag(data_tag tag, char* buf, long* len)/* write out a data tag */{  return(writeCompressedInteger(tag,buf,len));}/*----------------------------------------------------------------------*/char*readTag(data_tag* tag, char* buf)/* read a data tag */{  return(readCompressedInteger(tag,buf));}/*----------------------------------------------------------------------*/unsigned longwrittenTagSize(data_tag tag){  return(writtenCompressedIntSize(tag));}/*----------------------------------------------------------------------*/data_tagpeekTag(char* buf)/* read a data tag without advancing the buffer */{  data_tag tag;  readTag(&tag,buf);  return(tag);}/*----------------------------------------------------------------------*/any*makeAny(unsigned long size, char* data){  any* a = (any*)s_malloc((size_t)sizeof(any));  a->size = size;  a->bytes = data;  return(a);}/*----------------------------------------------------------------------*/voidfreeAny(any* a)/* destroy an any and its associated data.  Assumes a->bytes was   allocated using the s_malloc family of libraries */{  if (a != NULL)    { if (a->bytes != NULL)	s_free(a->bytes);      s_free(a);    }}/*----------------------------------------------------------------------*/any*duplicateAny(any* a){  any* copy = NULL;  if (a == NULL)    return(NULL);  copy = (any*)s_malloc((size_t)sizeof(any));  copy->size = a->size;  if (a->bytes == NULL)    copy->bytes = NULL;  else    { copy->bytes = (char*)s_malloc((size_t)copy->size);      memcpy(copy->bytes,a->bytes,(size_t)copy->size);    }  return(copy);}/*----------------------------------------------------------------------*/char*writeAny(any* a, data_tag tag, char* buffer, long* len)/* write an any + tag and size info */{  char* buf = buffer;  if (a == NULL)		/* handle unspecified optional args */    return(buf);  /* write the tags */  buf = writeTag(tag,buf,len);  buf = writeCompressedInteger(a->size,buf,len);  /* write the bytes */  CHECK_FOR_SPACE_LEFT(a->size,len);  memcpy(buf,a->bytes,(size_t)a->size);  return(buf+a->size);}/*----------------------------------------------------------------------*/char *readAny(any** anAny, char* buffer)/* read an any + tag and size info */{  char *buf;  any* a;  data_tag tag;a=(any*)s_malloc((size_t)sizeof(any));  buf=buffer;  buf = readTag(&tag,buf);  buf = readCompressedInteger(&a->size,buf);  /* now simply copy the bytes */  a->bytes = (char*)s_malloc((size_t)a->size);  memcpy(a->bytes,buf,(size_t)a->size);  *anAny = a;  return(buf+a->size);}/*----------------------------------------------------------------------*/unsigned longwrittenAnySize(data_tag tag, any* a){  unsigned long size;  if (a == NULL)    return(0);  size = writtenTagSize(tag);  size += writtenCompressedIntSize(a->size);  size += a->size;  return(size);}/*----------------------------------------------------------------------*/any*stringToAny(char* s){  any* a = NULL;  if (s == NULL)    return(NULL);  a = (any*)s_malloc((size_t)sizeof(any));  a->size = strlen(s);  a->bytes = (char*)s_malloc((size_t)a->size);  memcpy(a->bytes,s,(size_t)a->size);  return(a);}/*----------------------------------------------------------------------*/char*anyToString(any* a){  char* s = NULL;  if (a == NULL)    return(NULL);  s = s_malloc((size_t)(a->size + 1));  memcpy(s,a->bytes,(size_t)a->size);  s[a->size] = '\0';  return(s);}/*----------------------------------------------------------------------*/char*writeString(char* s, data_tag tag, char* buffer, long* len)/* Write a C style string.  The terminating null is not written.   This function is not part of the Z39.50 spec.  It is provided   for the convienience of those wishing to pass C strings in   the place of an any. */{  char* buf = buffer;  any* data = NULL;  if (s == NULL)    return(buffer);		/* handle unused optional item before making an any */  data = (any*)s_malloc((size_t)sizeof(any));  data->size = strlen(s);  data->bytes = s;		/* save a copy here by not using stringToAny() */  buf = writeAny(data,tag,buf,len);  s_free(data);			/* don't use freeAny() since it will free s too */  return(buf);}/*----------------------------------------------------------------------*/char*readString(char** s, char* buffer)/* Read an any and convert it into a C style string.   This function is not part of the Z39.50 spec.  It is provided   for the convienience of those wishing to pass C strings in   the place of an any. */{  any* data = NULL;  char* buf = readAny(&data,buffer);  *s = anyToString(data);  freeAny(data);  return(buf);}/*----------------------------------------------------------------------*/unsigned longwrittenStringSize(data_tag tag, char* s){  unsigned long size;  if (s == NULL)   return(0);  size = writtenTagSize(tag);  size += writtenCompressedIntSize(size);  size += strlen(s);  return(size);}/*----------------------------------------------------------------------*/any*longToAny(long num)/* a convienience function */{  char s[40];  sprintf(s,"%ld",num);  return(stringToAny(s));}/*----------------------------------------------------------------------*/longanyToLong(any* a)/* a convienience function */{  long num;  char* str = NULL;  str = anyToString(a);  sscanf(str,"%ld",&num);	/* could check the result and return				   an error */  s_free(str);  return(num);}/*----------------------------------------------------------------------*/#define bitsPerByte	8bit_map*makeBitMap(unsigned long numBits, ...)/* construct and return a bitmap with numBits elements */{  va_list ap;  unsigned long i,j;  bit_map* bm = NULL;  LYva_start(ap,numBits);  bm = (bit_map*)s_malloc((size_t)sizeof(bit_map));  bm->size = (unsigned long)(ceil((double)numBits / bitsPerByte));  bm->bytes = (char*)s_malloc((size_t)bm->size);  /* fill up the bits */  for (i = 0; i < bm->size; i++) /* iterate over bytes */    { char byte = 0;      for (j = 0; j < bitsPerByte; j++) /* iterate over bits */	{ if ((i * bitsPerByte + j) < numBits)	    { boolean bit = false;	      bit = (boolean)va_arg(ap,boolean);	      if (bit)	        { byte = byte | (1 << (bitsPerByte - j - 1));	        }	    }	  }      bm->bytes[i] = byte;    }  va_end(ap);  return(bm);}/*----------------------------------------------------------------------*/voidfreeBitMap(bit_map* bm)/* destroy a bit map created by makeBitMap() */{  s_free(bm->bytes);  s_free(bm);}/*----------------------------------------------------------------------*//* use this routine to interpret a bit map.  pos specifies the bit   number.  bit 0 is the Leftmost bit of the first byte.   Could do bounds checking. */booleanbitAtPos(unsigned long pos, bit_map* bm){  if (pos > bm->size*bitsPerByte)    return false;  else    return((bm->bytes[(pos / bitsPerByte)] &	    (0x80>>(pos % bitsPerByte))) ?	   true : false);}/*----------------------------------------------------------------------*/char*writeBitMap(bit_map* bm, data_tag tag, char* buffer, long* len)/* write a bitmap + type and size info */{  return(writeAny((any*)bm,tag,buffer,len));}/*----------------------------------------------------------------------*/char*readBitMap(bit_map** bm, char* buffer)/* read a bitmap + type and size info */{    char *c;    c = readAny((any**)bm,buffer);    return(c);}/*----------------------------------------------------------------------*/char*writeByte(unsigned long byte, char* buf, long* len){  CHECK_FOR_SPACE_LEFT(1,len);  buf[0] = byte & 0xFF; /* we really only want the first byte */  return(buf + 1);}/*----------------------------------------------------------------------*/char*readByte(unsigned char* byte, char* buf){  *byte = buf[0];  return(buf + 1);}/*----------------------------------------------------------------------*/char*writeBoolean(boolean flag, char* buf, long* len){  return(writeByte(flag,buf,len));}/*----------------------------------------------------------------------*/char*readBoolean(boolean* flag, char* buffer){  unsigned char byte;  char* buf = readByte(&byte,buffer);  *flag = (byte == true) ? true : false;  return(buf);}/*----------------------------------------------------------------------*/char*writePDUType(pdu_type pduType, char* buf, long* len)/* PDUType is a single byte */{  return(writeBinaryInteger((long)pduType,(unsigned long)1,buf,len));}/*----------------------------------------------------------------------*/char*readPDUType(pdu_type* pduType, char* buf)/* PDUType is a single byte */{  return(readBinaryInteger((long*)pduType,(unsigned long)1,buf));}/*----------------------------------------------------------------------*/pdu_typepeekPDUType(char* buf)/* read the next pdu without advancing the buffer, Note that this   function is to be used on a buffer that is known to contain an   APDU.  The pdu_type is written HEADER_LEN bytes into the buffer */{  pdu_type pdu;  readPDUType(&pdu,buf + HEADER_LEN);  return(pdu);}/*----------------------------------------------------------------------*/#define BINARY_INTEGER_BYTES	sizeof(long) /* the number of bytes used by						a "binary integer" */char*writeBinaryInteger(long num, unsigned long size, char* buf, long* len)/* write out first size bytes of num - no type info  XXX should this take unsigned longs instead ???  */{  long i;  char byte;  if (size < 1 || size > BINARY_INTEGER_BYTES)    return(NULL);		/* error */  CHECK_FOR_SPACE_LEFT(size,len);  for (i = size - 1; i >= 0; i--)    { byte = (char)(num & 255);      buf[i] = byte;      num = num >> bitsPerByte; /* don't and here */    }  return(buf + size);}/*----------------------------------------------------------------------*/char*readBinaryInteger(long* num, unsigned long size, char* buf)/* read in first size bytes of num - no type info  XXX this should take unsigned longs instead !!! */{  unsigned long i;  unsigned char byte;  if (size < 1 || size > BINARY_INTEGER_BYTES)    return(buf);		/* error */  *num = 0;  for (i = 0; i < size; i++)    { byte = buf[i];      *num = *num << bitsPerByte;      *num += byte;    }  return(buf + size);}/*----------------------------------------------------------------------*/unsigned longwrittenCompressedBinIntSize(long num)/* return the number of bytes needed to represent the value num.   currently limited to max of 4 bytes   Only compresses for positive nums - negatives get whole 4 bytes */{  if (num < 0L)    return(4);  else if (num < 256L)		/* 2**8 */    return(1);  else if (num < 65536L)	/* 2**16 */    return(2);  else if (num < 16777216L)	/* 2**24 */    return(3);  else    return(4);}/*----------------------------------------------------------------------*/char*writeNum(long num, data_tag tag, char* buffer, long* len)/* write a binary integer + size and tag info */{  char* buf = buffer;  long size = writtenCompressedBinIntSize(num);  if (num == UNUSED)    return(buffer);  buf = writeTag(tag,buf,len);  buf = writeCompressedInteger(size,buf,len);  buf = writeBinaryInteger(num,(unsigned long)size,buf,len);  return(buf);}/*----------------------------------------------------------------------*/char*readNum(long* num, char* buffer)/* read a binary integer + size and tag info */{  char* buf = buffer;  data_tag tag;  unsigned long size;  unsigned long val;  buf = readTag(&tag,buf);  buf = readCompressedInteger(&val,buf);  size = (unsigned long)val;  buf = readBinaryInteger(num,size,buf);  return(buf);}/*----------------------------------------------------------------------*/unsigned longwrittenNumSize(data_tag tag, long num){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -