blob: cc3455136680fea7d48a7b3b3a2458b0756dc86c [file] [log] [blame]
Alexander Afanasyev6b997c52011-08-08 12:55:25 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
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;
8 *
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>
19 */
20
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -070021/*
22 * ccn_buf_encoder.cc
23 * Abstraction
24 *
25 * Created by Ilya on 7/29/11.
26 * Copyright 2011 __MyCompanyName__. All rights reserved.
27 *
28 */
29
30#include <string.h>
31#include <stdarg.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <sys/time.h>
Alexander Afanasyev43e70252011-08-08 18:48:18 -070035#include "ccn.h"
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -070036#include "ccn_charbuf.h"
37#include "ccn_coding.h"
38#include "ccn_indexbuf.h"
39
40/**
41 * Append a ccnb start marker
42 *
43 * This forms the basic building block of ccnb-encoded data.
44 * @param c is the buffer to append to.
45 * @param val is the numval, intepreted according to tt (see enum ccn_tt).
46 * @param tt is the type field.
47 * @returns 0 for success or -1 for error.
48 */
49int
50ccn_charbuf_append_tt(struct ccn_charbuf *c, size_t val, enum ccn_tt tt)
51{
52 unsigned char buf[1+8*((sizeof(val)+6)/7)];
53 unsigned char *p = &(buf[sizeof(buf)-1]);
54 int n = 1;
55 p[0] = (CCN_TT_HBIT & ~CCN_CLOSE) |
56 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
57 (CCN_TT_MASK & tt);
58 val >>= (7-CCN_TT_BITS);
59 while (val != 0) {
60 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CCN_CLOSE;
61 n++;
62 val >>= 7;
63 }
64 return(ccn_charbuf_append(c, p, n));
65}
Ilya Moiseenko7e254b72011-08-04 19:06:10 -070066
67
68/**
69 * Encode and sign a ContentObject.
70 * @param buf is the output buffer where encoded object is written.
71 * @param Name is the ccnb-encoded name from ccn_name_init and friends.
72 * @param SignedInfo is the ccnb-encoded info from ccn_signed_info_create.
73 * @param data pintes to the raw data to be encoded.
74 * @param size is the size, in bytes, of the raw data to be encoded.
75 * @param digest_algorithm may be NULL for default.
76 * @param private_key is the private key to use for signing.
77 * @returns 0 for success or -1 for error.
78 */
79int
80ccn_encode_ContentObject(struct ccn_charbuf *buf,
81 const struct ccn_charbuf *Name,
82 /*const struct ccn_charbuf *SignedInfo,*/
83 const void *data,
84 size_t size
85 /*const char *digest_algorithm,
86 const struct ccn_pkey *private_key*/
87 )
88{
89 int res = 0;
90 //struct ccn_sigc *sig_ctx;
91 //struct ccn_signature *signature;
92 //size_t signature_size;
93 struct ccn_charbuf *content_header;
94 size_t closer_start;
95
96 content_header = ccn_charbuf_create();
97 res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG);
98 if (size != 0)
99 res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB);
100 closer_start = content_header->length;
101 res |= ccn_charbuf_append_closer(content_header);
102 if (res < 0)
103 return(-1);
104 //sig_ctx = ccn_sigc_create();
105 //if (sig_ctx == NULL)
106 // return(-1);
107 //if (0 != ccn_sigc_init(sig_ctx, digest_algorithm))
108 // return(-1);
109 //if (0 != ccn_sigc_update(sig_ctx, Name->buf, Name->length))
110 // return(-1);
111 //if (0 != ccn_sigc_update(sig_ctx, SignedInfo->buf, SignedInfo->length))
112 // return(-1);
113 //if (0 != ccn_sigc_update(sig_ctx, content_header->buf, closer_start))
114 // return(-1);
115 //if (0 != ccn_sigc_update(sig_ctx, data, size))
116 // return(-1);
117 //if (0 != ccn_sigc_update(sig_ctx, content_header->buf + closer_start,
118 // content_header->length - closer_start))
119 // return(-1);
120 //signature = calloc(1, ccn_sigc_signature_max_size(sig_ctx, private_key));
121 //if (signature == NULL)
122 // return(-1);
123 //res = ccn_sigc_final(sig_ctx, signature, &signature_size, private_key);
124 //if (0 != res) {
125 // free(signature);
126 // return(-1);
127 //}
128 //ccn_sigc_destroy(&sig_ctx);
129 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG);
130 //res |= ccn_encode_Signature(buf, digest_algorithm,
131 // NULL, 0, signature, signature_size);
132 res |= ccn_charbuf_append_charbuf(buf, Name);
133 //res |= ccn_charbuf_append_charbuf(buf, SignedInfo);
134 res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size);
135 res |= ccn_charbuf_append_closer(buf);
136 //free(signature);
137 ccn_charbuf_destroy(&content_header);
138 return(res == 0 ? 0 : -1);
139}
140
141/**
142 * Append a tagged BLOB
143 *
144 * This is a ccnb-encoded element with containing the BLOB as content
145 * @param c is the buffer to append to.
146 * @param dtag is the element's dtab
147 * @param data points to the binary data
148 * @param size is the size of the data, in bytes
149 * @returns 0 for success or -1 for error.
150 */
151int
152ccnb_append_tagged_blob(struct ccn_charbuf *c,
153 enum ccn_dtag dtag,
154 const void *data,
155 size_t size)
156{
157 int res;
158
159 res = ccn_charbuf_append_tt(c, dtag, CCN_DTAG);
160 if (size != 0) {
161 res |= ccn_charbuf_append_tt(c, size, CCN_BLOB);
162 res |= ccn_charbuf_append(c, data, size);
163 }
164 res |= ccn_charbuf_append_closer(c);
165 return(res == 0 ? 0 : -1);
166}