attrec.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #ifndef __attrec_h
  42. #define __attrec_h
  43. /*
  44. * Description (attrec.h)
  45. *
  46. * This file describes the encoding and decoding of attribute
  47. * records. Attribute records consist of a sequence of items
  48. * of the form:
  49. *
  50. * <tag><length><contents>
  51. *
  52. * The <tag> is an integer code which identifies a particular
  53. * attribute. The <length> is the integer length in bytes of
  54. * the <contents>. The encoding of the contents is determined
  55. * by the <tag>, and is application-specific.
  56. *
  57. * Primitive data types currently supported are unsigned
  58. * integers (USI) and null-terminated strings (NTS). The
  59. * encoding of USI values less than 128 is simply an octet
  60. * containing the value. For values 128 or greater, the first
  61. * octet is 0x80 plus the length of the value, in octets.
  62. * This octet is followed by the indicated number of octets,
  63. * containing the USI value, with the most significant bits in
  64. * the first octet, and the least significant bits in the last
  65. * octet.
  66. *
  67. * Examples of USI encoding:
  68. *
  69. * Value Encoding (each value is an octet)
  70. * 4 0x04
  71. * 127 0x7f
  72. * -1 (this is not a USI)
  73. * 128 0x81 0x80
  74. * 1023 0x82 0x03 0xff
  75. *
  76. * The encoding of a null-terminated string (NTS) is simply the
  77. * sequence of octets which comprise the string, including the
  78. * terminating null (0x00) octet. The terminating null octet is
  79. * the only null value in the string. The character set used to
  80. * encode the other string octets is ASCII.
  81. */
  82. #include "usi.h"
  83. NSPR_BEGIN_EXTERN_C
  84. /* Define a type to reference an attribute record */
  85. typedef unsigned char * ATR_t;
  86. /*
  87. * Description (USILENGTH)
  88. *
  89. * This macro returns the length of the USI encoding for a specified
  90. * unsigned integer value. The length is the number of octets
  91. * required. It will be greater than zero, and less than or equal
  92. * to USIALLOC(). This is a partial inline optimization of
  93. * USI_Length().
  94. */
  95. #define USILENGTH(val) (((USI_t)(val) <= 0x7f) ? 1 : USI_Length((USI_t)(val)))
  96. /*
  97. * Description (USIALLOC)
  98. *
  99. * This macro returns the maximum length of an unsigned integer
  100. * encoding.
  101. */
  102. #define USIALLOC() (5)
  103. /*
  104. * Description (USIENCODE)
  105. *
  106. * This macro encodes a USI value into a specified buffer. It
  107. * returns a pointer to the first octet after the encoding.
  108. * This is a partial inline optimization for USI_Encode().
  109. */
  110. #define USIENCODE(cp, val) (((USI_t)(val) <= 0x7f) ? (*(cp) = (val), (cp)+1) \
  111. : USI_Encode((cp), (val)))
  112. /*
  113. * Description (USIINSERT)
  114. *
  115. * This macro performs a variation of USIENCODE which always
  116. * generates the maximum-sized USI encoding, i.e. the number of
  117. * octets indicated by USIALLOC().
  118. */
  119. #define USIINSERT(cp, val) USI_Insert((ATR_t)(cp), (USI_t)(val))
  120. /*
  121. * Description (USIDECODE)
  122. *
  123. * This macro decodes a USI value from a specified buffer. It
  124. * returns a pointer to the first octet after the encoding.
  125. * This is a partial inline optimization for USI_Decode().
  126. */
  127. #define USIDECODE(cp, pval) \
  128. ((*(cp) & 0x80) ? USI_Decode((cp), (pval)) \
  129. : (((pval) ? (*(pval) = *(cp)) : 0), (cp)+1))
  130. /* Define a type to reference a null-terminated string */
  131. typedef unsigned char * NTS_t;
  132. /*
  133. * Decription (NTSLENGTH)
  134. *
  135. * Return the length, in octets, of a null-terminated string.
  136. * It includes the terminating null octet.
  137. */
  138. #define NTSLENGTH(nts) ((nts) ? strlen((char *)(nts)) + 1 : 1)
  139. /*
  140. * Description (NTSENCODE)
  141. *
  142. * This macro copies a null-terminated string to a specified
  143. * attribute record buffer. It returns a pointer to the octet
  144. * following the NTS in the buffer.
  145. */
  146. #define NTSENCODE(cp, nts) \
  147. ((ATR_t)memccpy((void *)(cp), \
  148. (void *)((nts) ? (NTS_t)(nts) : (NTS_t)""), \
  149. 0, NTSLENGTH(nts)))
  150. /*
  151. * Description (NTSDECODE)
  152. *
  153. * This macro decodes a null-terminated string in a specified
  154. * attribute record buffer into a dynamically allocated buffer.
  155. * It returns a pointer to the first octet after the NTS in the
  156. * attribute record buffer.
  157. */
  158. #define NTSDECODE(cp, pnts) NTS_Decode((cp), (pnts))
  159. /* Functions in attrec.c */
  160. extern int NTS_Length(NTS_t ntsp);
  161. extern ATR_t NTS_Decode(ATR_t cp, NTS_t * pnts);
  162. extern ATR_t NTS_Encode(ATR_t cp, NTS_t nts);
  163. extern ATR_t USI_Decode(ATR_t cp, USI_t * pval);
  164. extern ATR_t USI_Encode(ATR_t cp, USI_t val);
  165. extern ATR_t USI_Insert(ATR_t cp, USI_t val);
  166. extern int USI_Length(USI_t val);
  167. NSPR_END_EXTERN_C
  168. #endif /* __attrec_h */