Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame^] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 6b997c5 | 2011-08-08 12:55:25 -0700 | [diff] [blame] | 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 Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sys/time.h> |
Alexander Afanasyev | 43e7025 | 2011-08-08 18:48:18 -0700 | [diff] [blame] | 26 | #include "ccn.h" |
Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 27 | #include "ccn_charbuf.h" |
| 28 | #include "ccn_coding.h" |
| 29 | #include "ccn_indexbuf.h" |
| 30 | |
| 31 | /** |
Ilya Moiseenko | 7e254b7 | 2011-08-04 19:06:10 -0700 | [diff] [blame] | 32 | * Encode and sign a ContentObject. |
| 33 | * @param buf is the output buffer where encoded object is written. |
| 34 | * @param Name is the ccnb-encoded name from ccn_name_init and friends. |
| 35 | * @param SignedInfo is the ccnb-encoded info from ccn_signed_info_create. |
| 36 | * @param data pintes to the raw data to be encoded. |
| 37 | * @param size is the size, in bytes, of the raw data to be encoded. |
| 38 | * @param digest_algorithm may be NULL for default. |
| 39 | * @param private_key is the private key to use for signing. |
| 40 | * @returns 0 for success or -1 for error. |
| 41 | */ |
| 42 | int |
| 43 | ccn_encode_ContentObject(struct ccn_charbuf *buf, |
| 44 | const struct ccn_charbuf *Name, |
| 45 | /*const struct ccn_charbuf *SignedInfo,*/ |
| 46 | const void *data, |
| 47 | size_t size |
| 48 | /*const char *digest_algorithm, |
| 49 | const struct ccn_pkey *private_key*/ |
| 50 | ) |
| 51 | { |
| 52 | int res = 0; |
Ilya Moiseenko | 7e254b7 | 2011-08-04 19:06:10 -0700 | [diff] [blame] | 53 | |
Ilya Moiseenko | 7e254b7 | 2011-08-04 19:06:10 -0700 | [diff] [blame] | 54 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG); |
| 55 | //res |= ccn_encode_Signature(buf, digest_algorithm, |
| 56 | // NULL, 0, signature, signature_size); |
| 57 | res |= ccn_charbuf_append_charbuf(buf, Name); |
| 58 | //res |= ccn_charbuf_append_charbuf(buf, SignedInfo); |
| 59 | res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size); |
| 60 | res |= ccn_charbuf_append_closer(buf); |
| 61 | //free(signature); |
| 62 | ccn_charbuf_destroy(&content_header); |
| 63 | return(res == 0 ? 0 : -1); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Append a tagged BLOB |
| 68 | * |
| 69 | * This is a ccnb-encoded element with containing the BLOB as content |
| 70 | * @param c is the buffer to append to. |
| 71 | * @param dtag is the element's dtab |
| 72 | * @param data points to the binary data |
| 73 | * @param size is the size of the data, in bytes |
| 74 | * @returns 0 for success or -1 for error. |
| 75 | */ |
| 76 | int |
| 77 | ccnb_append_tagged_blob(struct ccn_charbuf *c, |
| 78 | enum ccn_dtag dtag, |
| 79 | const void *data, |
| 80 | size_t size) |
| 81 | { |
| 82 | int res; |
| 83 | |
| 84 | res = ccn_charbuf_append_tt(c, dtag, CCN_DTAG); |
| 85 | if (size != 0) { |
| 86 | res |= ccn_charbuf_append_tt(c, size, CCN_BLOB); |
| 87 | res |= ccn_charbuf_append(c, data, size); |
| 88 | } |
| 89 | res |= ccn_charbuf_append_closer(c); |
| 90 | return(res == 0 ? 0 : -1); |
| 91 | } |