attrec.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifndef __attrec_h
  39. #define __attrec_h
  40. /*
  41. * Description (attrec.h)
  42. *
  43. * This file describes the encoding and decoding of attribute
  44. * records. Attribute records consist of a sequence of items
  45. * of the form:
  46. *
  47. * <tag><length><contents>
  48. *
  49. * The <tag> is an integer code which identifies a particular
  50. * attribute. The <length> is the integer length in bytes of
  51. * the <contents>. The encoding of the contents is determined
  52. * by the <tag>, and is application-specific.
  53. *
  54. * Primitive data types currently supported are unsigned
  55. * integers (USI) and null-terminated strings (NTS). The
  56. * encoding of USI values less than 128 is simply an octet
  57. * containing the value. For values 128 or greater, the first
  58. * octet is 0x80 plus the length of the value, in octets.
  59. * This octet is followed by the indicated number of octets,
  60. * containing the USI value, with the most significant bits in
  61. * the first octet, and the least significant bits in the last
  62. * octet.
  63. *
  64. * Examples of USI encoding:
  65. *
  66. * Value Encoding (each value is an octet)
  67. * 4 0x04
  68. * 127 0x7f
  69. * -1 (this is not a USI)
  70. * 128 0x81 0x80
  71. * 1023 0x82 0x03 0xff
  72. *
  73. * The encoding of a null-terminated string (NTS) is simply the
  74. * sequence of octets which comprise the string, including the
  75. * terminating null (0x00) octet. The terminating null octet is
  76. * the only null value in the string. The character set used to
  77. * encode the other string octets is ASCII.
  78. */
  79. #include "usi.h"
  80. NSPR_BEGIN_EXTERN_C
  81. /* Define a type to reference an attribute record */
  82. typedef unsigned char * ATR_t;
  83. /*
  84. * Description (USILENGTH)
  85. *
  86. * This macro returns the length of the USI encoding for a specified
  87. * unsigned integer value. The length is the number of octets
  88. * required. It will be greater than zero, and less than or equal
  89. * to USIALLOC(). This is a partial inline optimization of
  90. * USI_Length().
  91. */
  92. #define USILENGTH(val) (((USI_t)(val) <= 0x7f) ? 1 : USI_Length((USI_t)(val)))
  93. /*
  94. * Description (USIALLOC)
  95. *
  96. * This macro returns the maximum length of an unsigned integer
  97. * encoding.
  98. */
  99. #define USIALLOC() (5)
  100. /*
  101. * Description (USIENCODE)
  102. *
  103. * This macro encodes a USI value into a specified buffer. It
  104. * returns a pointer to the first octet after the encoding.
  105. * This is a partial inline optimization for USI_Encode().
  106. */
  107. #define USIENCODE(cp, val) (((USI_t)(val) <= 0x7f) ? (*(cp) = (val), (cp)+1) \
  108. : USI_Encode((cp), (val)))
  109. /*
  110. * Description (USIINSERT)
  111. *
  112. * This macro performs a variation of USIENCODE which always
  113. * generates the maximum-sized USI encoding, i.e. the number of
  114. * octets indicated by USIALLOC().
  115. */
  116. #define USIINSERT(cp, val) USI_Insert((ATR_t)(cp), (USI_t)(val))
  117. /*
  118. * Description (USIDECODE)
  119. *
  120. * This macro decodes a USI value from a specified buffer. It
  121. * returns a pointer to the first octet after the encoding.
  122. * This is a partial inline optimization for USI_Decode().
  123. */
  124. #define USIDECODE(cp, pval) \
  125. ((*(cp) & 0x80) ? USI_Decode((cp), (pval)) \
  126. : (((pval) ? (*(pval) = *(cp)) : 0), (cp)+1))
  127. /* Define a type to reference a null-terminated string */
  128. typedef unsigned char * NTS_t;
  129. /*
  130. * Decription (NTSLENGTH)
  131. *
  132. * Return the length, in octets, of a null-terminated string.
  133. * It includes the terminating null octet.
  134. */
  135. #define NTSLENGTH(nts) ((nts) ? strlen((char *)(nts)) + 1 : 1)
  136. /*
  137. * Description (NTSENCODE)
  138. *
  139. * This macro copies a null-terminated string to a specified
  140. * attribute record buffer. It returns a pointer to the octet
  141. * following the NTS in the buffer.
  142. */
  143. #define NTSENCODE(cp, nts) \
  144. ((ATR_t)memccpy((void *)(cp), \
  145. (void *)((nts) ? (NTS_t)(nts) : (NTS_t)""), \
  146. 0, NTSLENGTH(nts)))
  147. /*
  148. * Description (NTSDECODE)
  149. *
  150. * This macro decodes a null-terminated string in a specified
  151. * attribute record buffer into a dynamically allocated buffer.
  152. * It returns a pointer to the first octet after the NTS in the
  153. * attribute record buffer.
  154. */
  155. #define NTSDECODE(cp, pnts) NTS_Decode((cp), (pnts))
  156. /* Functions in attrec.c */
  157. extern int NTS_Length(NTS_t ntsp);
  158. extern ATR_t NTS_Decode(ATR_t cp, NTS_t * pnts);
  159. extern ATR_t NTS_Encode(ATR_t cp, NTS_t nts);
  160. extern ATR_t USI_Decode(ATR_t cp, USI_t * pval);
  161. extern ATR_t USI_Encode(ATR_t cp, USI_t val);
  162. extern ATR_t USI_Insert(ATR_t cp, USI_t val);
  163. extern int USI_Length(USI_t val);
  164. NSPR_END_EXTERN_C
  165. #endif /* __attrec_h */