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