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