… | |
… | |
189 | For example: |
189 | For example: |
190 | |
190 | |
191 | [ASN_UNIVERSAL, ASN_INTEGER32, 0, 177] # the integer 177 |
191 | [ASN_UNIVERSAL, ASN_INTEGER32, 0, 177] # the integer 177 |
192 | [ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "john"] # the string "john" |
192 | [ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "john"] # the string "john" |
193 | [ASN_UNIVERSAL, ASN_OID, 0, "1.3.6.133"] # some OID |
193 | [ASN_UNIVERSAL, ASN_OID, 0, "1.3.6.133"] # some OID |
194 | [ASN_UNIVERSAL, ASN_SEQUENCE, 1, [ [ASN_UNIVERSAL... # a sequencE |
194 | [ASN_UNIVERSAL, ASN_SEQUENCE, 1, [ [ASN_UNIVERSAL... # a sequence |
195 | |
195 | |
196 | To avoid non-descriptive hardcoded array index numbers, this module |
196 | To avoid non-descriptive hardcoded array index numbers, this module |
197 | defines symbolic constants to access these members: C<BER_CLASS>, |
197 | defines symbolic constants to access these members: C<BER_CLASS>, |
198 | C<BER_TAG>, C<BER_CONSTRUCTED> and C<BER_DATA>. |
198 | C<BER_TAG>, C<BER_CONSTRUCTED> and C<BER_DATA>. |
199 | |
199 | |
… | |
… | |
218 | specific applications (for example, the SNMP C<Unsigned32> type is in this |
218 | specific applications (for example, the SNMP C<Unsigned32> type is in this |
219 | namespace), a special-purpose context namespace (C<ASN_CONTEXT>, used e.g. |
219 | namespace), a special-purpose context namespace (C<ASN_CONTEXT>, used e.g. |
220 | for C<CHOICE>) and a private namespace (C<ASN_PRIVATE>). |
220 | for C<CHOICE>) and a private namespace (C<ASN_PRIVATE>). |
221 | |
221 | |
222 | The meaning of the I<TAG> depends on the namespace, and defines a |
222 | The meaning of the I<TAG> depends on the namespace, and defines a |
223 | (partial) interpretation of the data value. For example, right now, SNMP |
223 | (partial) interpretation of the data value. For example, SNMP defines |
224 | application namespace knowledge ix hardcoded into this module, so it |
224 | extra tags in the C<ASN_APPLICATION> namespace, and to take full advantage |
225 | knows that SNMP C<Unsigned32> values need to be decoded into actual perl |
225 | of these, you need to tell this module how to handle those via profiles. |
226 | integers. |
|
|
227 | |
226 | |
228 | The most common tags in the C<ASN_UNIVERSAL> namespace are |
227 | The most common tags in the C<ASN_UNIVERSAL> namespace are |
229 | C<ASN_INTEGER32>, C<ASN_BIT_STRING>, C<ASN_NULL>, C<ASN_OCTET_STRING>, |
228 | C<ASN_INTEGER32>, C<ASN_BIT_STRING>, C<ASN_NULL>, C<ASN_OCTET_STRING>, |
230 | C<ASN_OBJECT_IDENTIFIER>, C<ASN_SEQUENCE>, C<ASN_SET> and |
229 | C<ASN_OBJECT_IDENTIFIER>, C<ASN_SEQUENCE>, C<ASN_SET> and |
231 | C<ASN_IA5_STRING>. |
230 | C<ASN_IA5_STRING>. |
232 | |
231 | |
233 | The most common tags in SNMP's C<ASN_APPLICATION> namespace |
232 | The most common tags in SNMP's C<ASN_APPLICATION> namespace are |
234 | are C<SNMP_IPADDRESS>, C<SNMP_COUNTER32>, C<SNMP_UNSIGNED32>, |
233 | C<SNMP_COUNTER32>, C<SNMP_UNSIGNED32>, C<SNMP_TIMETICKS> and |
235 | C<SNMP_TIMETICKS>, C<SNMP_OPAQUE> and C<SNMP_COUNTER64>. |
234 | C<SNMP_COUNTER64>. |
236 | |
235 | |
237 | The I<CONSTRUCTED> flag is really just a boolean - if it is false, the |
236 | The I<CONSTRUCTED> flag is really just a boolean - if it is false, |
238 | the value is "primitive" and contains no subvalues, kind of like a |
237 | the value is "primitive" and contains no subvalues, kind of like a |
239 | non-reference perl scalar. IF it is true, then the value is "constructed" |
238 | non-reference perl scalar. If it is true, then the value is "constructed" |
240 | which just means it contains a list of subvalues which this module will |
239 | which just means it contains a list of subvalues which this module will |
241 | en-/decode as BER tuples themselves. |
240 | en-/decode as BER tuples themselves. |
242 | |
241 | |
243 | The I<DATA> value is either a reference to an array of further tuples (if |
242 | The I<DATA> value is either a reference to an array of further tuples (if |
244 | the value is I<CONSTRUCTED>), some decoded representation of the value, |
243 | the value is I<CONSTRUCTED>), some decoded representation of the value, |
… | |
… | |
253 | |
252 | |
254 | =head2 DECODING AND ENCODING |
253 | =head2 DECODING AND ENCODING |
255 | |
254 | |
256 | =over |
255 | =over |
257 | |
256 | |
258 | =item $tuple = ber_decoded $bindata |
257 | =item $tuple = ber_decoded $bindata[, $profile] |
259 | |
258 | |
260 | Decodes binary BER data in C<$bindata> and returns the resulting BER |
259 | Decodes binary BER data in C<$bindata> and returns the resulting BER |
261 | tuple. Croaks on any decoding error, so the returned C<$tuple> is always |
260 | tuple. Croaks on any decoding error, so the returned C<$tuple> is always |
262 | valid. |
261 | valid. |
263 | |
262 | |
|
|
263 | How tags are interpreted is defined by the second argument, which must |
|
|
264 | be a C<Convert::BER::XS::Profile> object. If it is missing, the default |
|
|
265 | profile will be used (C<$Convert::BER::XS::DEFAULT_PROFILE>). |
|
|
266 | |
|
|
267 | In addition to rolling your own, this module provides a |
|
|
268 | C<$Convert::BER::XS::SNMP_PROFILE> that knows about the additional SNMP |
|
|
269 | types. |
|
|
270 | |
264 | =item $bindata = ber_encode $tuple |
271 | =item $bindata = ber_encode $tuple[, $profile] |
265 | |
272 | |
266 | Encodes the BER tuple into a BER/DER data structure. |
273 | Encodes the BER tuple into a BER/DER data structure. AS with |
|
|
274 | Cyber_decode>, an optional profile can be given. |
267 | |
275 | |
268 | =back |
276 | =back |
269 | |
277 | |
270 | =head2 HELPER FUNCTIONS |
278 | =head2 HELPER FUNCTIONS |
271 | |
279 | |
272 | Working with a 4-tuple for every value can be annoying. Or, rather, I<is> |
280 | Working with a 4-tuple for every value can be annoying. Or, rather, I<is> |
273 | annoying. To reduce this a bit, this module defines a number of helper |
281 | annoying. To reduce this a bit, this module defines a number of helper |
274 | functions, both to match BER tuples and to conmstruct BER tuples: |
282 | functions, both to match BER tuples and to construct BER tuples: |
275 | |
283 | |
276 | =head3 MATCH HELPERS |
284 | =head3 MATCH HELPERS |
277 | |
285 | |
278 | Thse functions accept a BER tuple as first argument and either paertially |
286 | These functions accept a BER tuple as first argument and either partially |
279 | or fully match it. They often come in two forms, one which exactly matches |
287 | or fully match it. They often come in two forms, one which exactly matches |
280 | a value, and one which only matches the type and returns the value. |
288 | a value, and one which only matches the type and returns the value. |
281 | |
289 | |
282 | They do check whether valid tuples are passed in and croak otherwise. As |
290 | They do check whether valid tuples are passed in and croak otherwise. As |
283 | a ease-of-use exception, they usually also accept C<undef> instead of a |
291 | a ease-of-use exception, they usually also accept C<undef> instead of a |
284 | tuple reference. in which case they silently fail to match. |
292 | tuple reference, in which case they silently fail to match. |
285 | |
293 | |
286 | =over |
294 | =over |
287 | |
295 | |
288 | =item $bool = ber_is $tuple, $class, $tag, $constructed, $data |
296 | =item $bool = ber_is $tuple, $class, $tag, $constructed, $data |
289 | |
297 | |
290 | This takes a BER C<$tuple> and matches its elements agains the privded |
298 | This takes a BER C<$tuple> and matches its elements against the provided |
291 | values, all of which are optional - values that are either missing or |
299 | values, all of which are optional - values that are either missing or |
292 | C<undef> will be ignored, the others will be matched exactly (e.g. as if |
300 | C<undef> will be ignored, the others will be matched exactly (e.g. as if |
293 | you used C<==> or C<eq> (for C<$data>)). |
301 | you used C<==> or C<eq> (for C<$data>)). |
294 | |
302 | |
295 | Some examples: |
303 | Some examples: |