attrec.h 4.5 KB

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