attrec.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #ifndef __attrec_h
  13. #define __attrec_h
  14. /*
  15. * Description (attrec.h)
  16. *
  17. * This file describes the encoding and decoding of attribute
  18. * records. Attribute records consist of a sequence of items
  19. * of the form:
  20. *
  21. * <tag><length><contents>
  22. *
  23. * The <tag> is an integer code which identifies a particular
  24. * attribute. The <length> is the integer length in bytes of
  25. * the <contents>. The encoding of the contents is determined
  26. * by the <tag>, and is application-specific.
  27. *
  28. * Primitive data types currently supported are unsigned
  29. * integers (USI) and null-terminated strings (NTS). The
  30. * encoding of USI values less than 128 is simply an octet
  31. * containing the value. For values 128 or greater, the first
  32. * octet is 0x80 plus the length of the value, in octets.
  33. * This octet is followed by the indicated number of octets,
  34. * containing the USI value, with the most significant bits in
  35. * the first octet, and the least significant bits in the last
  36. * octet.
  37. *
  38. * Examples of USI encoding:
  39. *
  40. * Value Encoding (each value is an octet)
  41. * 4 0x04
  42. * 127 0x7f
  43. * -1 (this is not a USI)
  44. * 128 0x81 0x80
  45. * 1023 0x82 0x03 0xff
  46. *
  47. * The encoding of a null-terminated string (NTS) is simply the
  48. * sequence of octets which comprise the string, including the
  49. * terminating null (0x00) octet. The terminating null octet is
  50. * the only null value in the string. The character set used to
  51. * encode the other string octets is ASCII.
  52. */
  53. #include "usi.h"
  54. NSPR_BEGIN_EXTERN_C
  55. /* Define a type to reference an attribute record */
  56. typedef unsigned char * ATR_t;
  57. /*
  58. * Description (USILENGTH)
  59. *
  60. * This macro returns the length of the USI encoding for a specified
  61. * unsigned integer value. The length is the number of octets
  62. * required. It will be greater than zero, and less than or equal
  63. * to USIALLOC(). This is a partial inline optimization of
  64. * USI_Length().
  65. */
  66. #define USILENGTH(val) (((USI_t)(val) <= 0x7f) ? 1 : USI_Length((USI_t)(val)))
  67. /*
  68. * Description (USIALLOC)
  69. *
  70. * This macro returns the maximum length of an unsigned integer
  71. * encoding.
  72. */
  73. #define USIALLOC() (5)
  74. /*
  75. * Description (USIENCODE)
  76. *
  77. * This macro encodes a USI value into a specified buffer. It
  78. * returns a pointer to the first octet after the encoding.
  79. * This is a partial inline optimization for USI_Encode().
  80. */
  81. #define USIENCODE(cp, val) (((USI_t)(val) <= 0x7f) ? (*(cp) = (val), (cp)+1) \
  82. : USI_Encode((cp), (val)))
  83. /*
  84. * Description (USIINSERT)
  85. *
  86. * This macro performs a variation of USIENCODE which always
  87. * generates the maximum-sized USI encoding, i.e. the number of
  88. * octets indicated by USIALLOC().
  89. */
  90. #define USIINSERT(cp, val) USI_Insert((ATR_t)(cp), (USI_t)(val))
  91. /*
  92. * Description (USIDECODE)
  93. *
  94. * This macro decodes a USI value from a specified buffer. It
  95. * returns a pointer to the first octet after the encoding.
  96. * This is a partial inline optimization for USI_Decode().
  97. */
  98. #define USIDECODE(cp, pval) \
  99. ((*(cp) & 0x80) ? USI_Decode((cp), (pval)) \
  100. : (((pval) ? (*(pval) = *(cp)) : 0), (cp)+1))
  101. /* Define a type to reference a null-terminated string */
  102. typedef unsigned char * NTS_t;
  103. /*
  104. * Decription (NTSLENGTH)
  105. *
  106. * Return the length, in octets, of a null-terminated string.
  107. * It includes the terminating null octet.
  108. */
  109. #define NTSLENGTH(nts) ((nts) ? strlen((char *)(nts)) + 1 : 1)
  110. /*
  111. * Description (NTSENCODE)
  112. *
  113. * This macro copies a null-terminated string to a specified
  114. * attribute record buffer. It returns a pointer to the octet
  115. * following the NTS in the buffer.
  116. */
  117. #define NTSENCODE(cp, nts) \
  118. ((ATR_t)memccpy((void *)(cp), \
  119. (void *)((nts) ? (NTS_t)(nts) : (NTS_t)""), \
  120. 0, NTSLENGTH(nts)))
  121. /*
  122. * Description (NTSDECODE)
  123. *
  124. * This macro decodes a null-terminated string in a specified
  125. * attribute record buffer into a dynamically allocated buffer.
  126. * It returns a pointer to the first octet after the NTS in the
  127. * attribute record buffer.
  128. */
  129. #define NTSDECODE(cp, pnts) NTS_Decode((cp), (pnts))
  130. /* Functions in attrec.c */
  131. extern int NTS_Length(NTS_t ntsp);
  132. extern ATR_t NTS_Decode(ATR_t cp, NTS_t * pnts);
  133. extern ATR_t NTS_Encode(ATR_t cp, NTS_t nts);
  134. extern ATR_t USI_Decode(ATR_t cp, USI_t * pval);
  135. extern ATR_t USI_Encode(ATR_t cp, USI_t val);
  136. extern ATR_t USI_Insert(ATR_t cp, USI_t val);
  137. extern int USI_Length(USI_t val);
  138. NSPR_END_EXTERN_C
  139. #endif /* __attrec_h */