blob: 808d5bec2f8e8cce10b8f788f7e74b000bf18d12 [file] [log] [blame]
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -07001/*
2 * ccn_coding.h
3 * Abstraction
4 *
5 * Created by Ilya on 7/29/11.
6 * Copyright 2011 __MyCompanyName__. All rights reserved.
7 *
8 */
9
10/**
11 * @file ccn/coding.h
12 *
13 * Details of the ccn binary wire encoding.
14 *
15 * Part of the CCNx C Library.
16 *
17 * Copyright (C) 2008-2010 Palo Alto Research Center, Inc.
18 *
19 * This library is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU Lesser General Public License version 2.1
21 * as published by the Free Software Foundation.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details. You should have received
26 * a copy of the GNU Lesser General Public License along with this library;
27 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
28 * Fifth Floor, Boston, MA 02110-1301 USA.
29 */
30
31#ifndef CCN_CODING_DEFINED
32#define CCN_CODING_DEFINED
33
34#include <sys/types.h>
35#include <stddef.h>
36
37#define CCN_TT_BITS 3
38#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
39#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
40#define CCN_TT_HBIT ((unsigned char)(1 << 7))
41
42/**
43 * Type tag for a ccnb start marker.
44 */
45enum ccn_tt {
46 CCN_EXT, /**< starts composite extension - numval is subtype */
47 CCN_TAG, /**< starts composite - numval is tagnamelen-1 */
48 CCN_DTAG, /**< starts composite - numval is tagdict index (enum ccn_dtag) */
49 CCN_ATTR, /**< attribute - numval is attrnamelen-1, value follows */
50 CCN_DATTR, /**< attribute numval is attrdict index */
51 CCN_BLOB, /**< opaque binary data - numval is byte count */
52 CCN_UDATA, /**< UTF-8 encoded character data - numval is byte count */
53 CCN_NO_TOKEN /**< should not occur in encoding */
54};
55
56/** CCN_CLOSE terminates composites */
57#define CCN_CLOSE ((unsigned char)(0))
58
59enum ccn_ext_subtype {
60 /* skip smallest values for now */
61 CCN_PROCESSING_INSTRUCTIONS = 16 /* <?name:U value:U?> */
62};
63
64/**
65 * DTAG identifies ccnb-encoded elements.
66 * c.f. tagname.csvdict
67 * See the gen_enum_dtag script for help updating these.
68 */
69enum ccn_dtag {
70 CCN_DTAG_Any = 13,
71 CCN_DTAG_Name = 14,
72 CCN_DTAG_Component = 15,
73 CCN_DTAG_Certificate = 16,
74 CCN_DTAG_Collection = 17,
75 CCN_DTAG_CompleteName = 18,
76 CCN_DTAG_Content = 19,
77 CCN_DTAG_SignedInfo = 20,
78 CCN_DTAG_ContentDigest = 21,
79 CCN_DTAG_ContentHash = 22,
80 CCN_DTAG_Count = 24,
81 CCN_DTAG_Header = 25,
82 CCN_DTAG_Interest = 26, /* 20090915 */
83 CCN_DTAG_Key = 27,
84 CCN_DTAG_KeyLocator = 28,
85 CCN_DTAG_KeyName = 29,
86 CCN_DTAG_Length = 30,
87 CCN_DTAG_Link = 31,
88 CCN_DTAG_LinkAuthenticator = 32,
89 CCN_DTAG_NameComponentCount = 33, /* DeprecatedInInterest */
90 CCN_DTAG_RootDigest = 36,
91 CCN_DTAG_Signature = 37,
92 CCN_DTAG_Start = 38,
93 CCN_DTAG_Timestamp = 39,
94 CCN_DTAG_Type = 40,
95 CCN_DTAG_Nonce = 41,
96 CCN_DTAG_Scope = 42,
97 CCN_DTAG_Exclude = 43,
98 CCN_DTAG_Bloom = 44,
99 CCN_DTAG_BloomSeed = 45,
100 CCN_DTAG_AnswerOriginKind = 47,
101 CCN_DTAG_InterestLifetime = 48,
102 CCN_DTAG_Witness = 53,
103 CCN_DTAG_SignatureBits = 54,
104 CCN_DTAG_DigestAlgorithm = 55,
105 CCN_DTAG_BlockSize = 56,
106 CCN_DTAG_FreshnessSeconds = 58,
107 CCN_DTAG_FinalBlockID = 59,
108 CCN_DTAG_PublisherPublicKeyDigest = 60,
109 CCN_DTAG_PublisherCertificateDigest = 61,
110 CCN_DTAG_PublisherIssuerKeyDigest = 62,
111 CCN_DTAG_PublisherIssuerCertificateDigest = 63,
112 CCN_DTAG_ContentObject = 64, /* 20090915 */
113 CCN_DTAG_WrappedKey = 65,
114 CCN_DTAG_WrappingKeyIdentifier = 66,
115 CCN_DTAG_WrapAlgorithm = 67,
116 CCN_DTAG_KeyAlgorithm = 68,
117 CCN_DTAG_Label = 69,
118 CCN_DTAG_EncryptedKey = 70,
119 CCN_DTAG_EncryptedNonceKey = 71,
120 CCN_DTAG_WrappingKeyName = 72,
121 CCN_DTAG_Action = 73,
122 CCN_DTAG_FaceID = 74,
123 CCN_DTAG_IPProto = 75,
124 CCN_DTAG_Host = 76,
125 CCN_DTAG_Port = 77,
126 CCN_DTAG_MulticastInterface = 78,
127 CCN_DTAG_ForwardingFlags = 79,
128 CCN_DTAG_FaceInstance = 80,
129 CCN_DTAG_ForwardingEntry = 81,
130 CCN_DTAG_MulticastTTL = 82,
131 CCN_DTAG_MinSuffixComponents = 83,
132 CCN_DTAG_MaxSuffixComponents = 84,
133 CCN_DTAG_ChildSelector = 85,
134 CCN_DTAG_RepositoryInfo = 86,
135 CCN_DTAG_Version = 87,
136 CCN_DTAG_RepositoryVersion = 88,
137 CCN_DTAG_GlobalPrefix = 89,
138 CCN_DTAG_LocalName = 90,
139 CCN_DTAG_Policy = 91,
140 CCN_DTAG_Namespace = 92,
141 CCN_DTAG_GlobalPrefixName = 93,
142 CCN_DTAG_PolicyVersion = 94,
143 CCN_DTAG_KeyValueSet = 95,
144 CCN_DTAG_KeyValuePair = 96,
145 CCN_DTAG_IntegerValue = 97,
146 CCN_DTAG_DecimalValue = 98,
147 CCN_DTAG_StringValue = 99,
148 CCN_DTAG_BinaryValue = 100,
149 CCN_DTAG_NameValue = 101,
150 CCN_DTAG_Entry = 102,
151 CCN_DTAG_ACL = 103,
152 CCN_DTAG_ParameterizedName = 104,
153 CCN_DTAG_Prefix = 105,
154 CCN_DTAG_Suffix = 106,
155 CCN_DTAG_Root = 107,
156 CCN_DTAG_ProfileName = 108,
157 CCN_DTAG_Parameters = 109,
158 CCN_DTAG_InfoString = 110,
159 CCN_DTAG_StatusResponse = 112,
160 CCN_DTAG_StatusCode = 113,
161 CCN_DTAG_StatusText = 114,
162 CCN_DTAG_SequenceNumber = 256,
163 CCN_DTAG_CCNProtocolDataUnit = 17702112
164};
165
166struct ccn_dict_entry {
167 int index; /**< matches enum ccn_dtag above */
168 const char *name; /**< textual name of dtag */
169};
170
171struct ccn_dict {
172 int count; /**< Count of elements in the table */
173 const struct ccn_dict_entry *dict; /**< the table entries */
174};
175
176/**
177 * Table for translating from DTAGs to names and vice versa.
178 */
179extern const struct ccn_dict ccn_dtag_dict; /* matches enum ccn_dtag above */
180
181struct ccn_skeleton_decoder { /* initialize to all 0 */
182 ssize_t index; /**< Number of bytes processed */
183 int state; /**< Decoder state */
184 int nest; /**< Element nesting */
185 size_t numval; /**< Current numval, meaning depends on state */
186 size_t token_index; /**< Starting index of most-recent token */
187 size_t element_index; /**< Starting index of most-recent element */
188};
189
190/**
191 * The decoder state is one of these, possibly with some
192 * additional bits set for internal use. A complete parse
193 * ends up in state 0 or an error state. Not all possible
194 * error states are listed here.
195 */
196enum ccn_decoder_state {
197 CCN_DSTATE_INITIAL = 0,
198 CCN_DSTATE_NEWTOKEN,
199 CCN_DSTATE_NUMVAL,
200 CCN_DSTATE_UDATA,
201 CCN_DSTATE_TAGNAME,
202 CCN_DSTATE_ATTRNAME,
203 CCN_DSTATE_BLOB,
204 /* All error states are negative */
205 CCN_DSTATE_ERR_OVERFLOW = -1,
206 CCN_DSTATE_ERR_ATTR = -2,
207 CCN_DSTATE_ERR_CODING = -3,
208 CCN_DSTATE_ERR_NEST = -4,
209 CCN_DSTATE_ERR_BUG = -5
210};
211
212/**
213 * If the CCN_DSTATE_PAUSE bit is set in the decoder state,
214 * the decoder will return just after recognizing each token.
215 * In this instance, use CCN_GET_TT_FROM_DSTATE() to extract
216 * the token type from the decoder state;
217 * CCN_CLOSE will be reported as CCN_NO_TOKEN.
218 * The pause bit persists, so the end test should take that into account
219 * by using the CCN_FINAL_DSTATE macro instead of testing for state 0.
220 */
221#define CCN_DSTATE_PAUSE (1 << 15)
222#define CCN_GET_TT_FROM_DSTATE(state) (CCN_TT_MASK & ((state) >> 16))
223#define CCN_FINAL_DSTATE(state) (((state) & (CCN_DSTATE_PAUSE-1)) == 0)
224
225ssize_t ccn_skeleton_decode(struct ccn_skeleton_decoder *d,
226 const unsigned char *p,
227 size_t n);
228
229#endif
230