blob: 34bd95dc5286faa0963440c0ea8345ff34176afc [file] [log] [blame]
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -07001/*
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 */
29int
30ccn_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}
Ilya Moiseenko7e254b72011-08-04 19:06:10 -070046
47
48/**
49 * Encode and sign a ContentObject.
50 * @param buf is the output buffer where encoded object is written.
51 * @param Name is the ccnb-encoded name from ccn_name_init and friends.
52 * @param SignedInfo is the ccnb-encoded info from ccn_signed_info_create.
53 * @param data pintes to the raw data to be encoded.
54 * @param size is the size, in bytes, of the raw data to be encoded.
55 * @param digest_algorithm may be NULL for default.
56 * @param private_key is the private key to use for signing.
57 * @returns 0 for success or -1 for error.
58 */
59int
60ccn_encode_ContentObject(struct ccn_charbuf *buf,
61 const struct ccn_charbuf *Name,
62 /*const struct ccn_charbuf *SignedInfo,*/
63 const void *data,
64 size_t size
65 /*const char *digest_algorithm,
66 const struct ccn_pkey *private_key*/
67 )
68{
69 int res = 0;
70 //struct ccn_sigc *sig_ctx;
71 //struct ccn_signature *signature;
72 //size_t signature_size;
73 struct ccn_charbuf *content_header;
74 size_t closer_start;
75
76 content_header = ccn_charbuf_create();
77 res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG);
78 if (size != 0)
79 res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB);
80 closer_start = content_header->length;
81 res |= ccn_charbuf_append_closer(content_header);
82 if (res < 0)
83 return(-1);
84 //sig_ctx = ccn_sigc_create();
85 //if (sig_ctx == NULL)
86 // return(-1);
87 //if (0 != ccn_sigc_init(sig_ctx, digest_algorithm))
88 // return(-1);
89 //if (0 != ccn_sigc_update(sig_ctx, Name->buf, Name->length))
90 // return(-1);
91 //if (0 != ccn_sigc_update(sig_ctx, SignedInfo->buf, SignedInfo->length))
92 // return(-1);
93 //if (0 != ccn_sigc_update(sig_ctx, content_header->buf, closer_start))
94 // return(-1);
95 //if (0 != ccn_sigc_update(sig_ctx, data, size))
96 // return(-1);
97 //if (0 != ccn_sigc_update(sig_ctx, content_header->buf + closer_start,
98 // content_header->length - closer_start))
99 // return(-1);
100 //signature = calloc(1, ccn_sigc_signature_max_size(sig_ctx, private_key));
101 //if (signature == NULL)
102 // return(-1);
103 //res = ccn_sigc_final(sig_ctx, signature, &signature_size, private_key);
104 //if (0 != res) {
105 // free(signature);
106 // return(-1);
107 //}
108 //ccn_sigc_destroy(&sig_ctx);
109 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG);
110 //res |= ccn_encode_Signature(buf, digest_algorithm,
111 // NULL, 0, signature, signature_size);
112 res |= ccn_charbuf_append_charbuf(buf, Name);
113 //res |= ccn_charbuf_append_charbuf(buf, SignedInfo);
114 res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size);
115 res |= ccn_charbuf_append_closer(buf);
116 //free(signature);
117 ccn_charbuf_destroy(&content_header);
118 return(res == 0 ? 0 : -1);
119}
120
121/**
122 * Append a tagged BLOB
123 *
124 * This is a ccnb-encoded element with containing the BLOB as content
125 * @param c is the buffer to append to.
126 * @param dtag is the element's dtab
127 * @param data points to the binary data
128 * @param size is the size of the data, in bytes
129 * @returns 0 for success or -1 for error.
130 */
131int
132ccnb_append_tagged_blob(struct ccn_charbuf *c,
133 enum ccn_dtag dtag,
134 const void *data,
135 size_t size)
136{
137 int res;
138
139 res = ccn_charbuf_append_tt(c, dtag, CCN_DTAG);
140 if (size != 0) {
141 res |= ccn_charbuf_append_tt(c, size, CCN_BLOB);
142 res |= ccn_charbuf_append(c, data, size);
143 }
144 res |= ccn_charbuf_append_closer(c);
145 return(res == 0 ? 0 : -1);
146}