Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ccn_buf_encoder.cc |
| 3 | * Abstraction |
| 4 | * |
| 5 | * Created by Ilya on 7/29/11. |
| 6 | * Copyright 2011 __MyCompanyName__. All rights reserved. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <string.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/time.h> |
| 15 | #include "ccn_ccn.h" |
| 16 | #include "ccn_charbuf.h" |
| 17 | #include "ccn_coding.h" |
| 18 | #include "ccn_indexbuf.h" |
| 19 | |
| 20 | /** |
| 21 | * Append a ccnb start marker |
| 22 | * |
| 23 | * This forms the basic building block of ccnb-encoded data. |
| 24 | * @param c is the buffer to append to. |
| 25 | * @param val is the numval, intepreted according to tt (see enum ccn_tt). |
| 26 | * @param tt is the type field. |
| 27 | * @returns 0 for success or -1 for error. |
| 28 | */ |
| 29 | int |
| 30 | ccn_charbuf_append_tt(struct ccn_charbuf *c, size_t val, enum ccn_tt tt) |
| 31 | { |
| 32 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 33 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 34 | int n = 1; |
| 35 | p[0] = (CCN_TT_HBIT & ~CCN_CLOSE) | |
| 36 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 37 | (CCN_TT_MASK & tt); |
| 38 | val >>= (7-CCN_TT_BITS); |
| 39 | while (val != 0) { |
| 40 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CCN_CLOSE; |
| 41 | n++; |
| 42 | val >>= 7; |
| 43 | } |
| 44 | return(ccn_charbuf_append(c, p, n)); |
| 45 | } |