00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <fcntl.h>
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <sys/mman.h>
00024 #include <limits.h>
00025 #include <stddef.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <unistd.h>
00030
00031 #include <ccn/coding.h>
00032 #include <ccn/ccn.h>
00033
00034 struct fstate {
00035 char *prefix;
00036 int segnum;
00037 };
00038
00039 static char *
00040 segment_prefix(char *path)
00041 {
00042 char *s, *d, *r;
00043
00044 s = strrchr(path, '/');
00045 if (s == NULL) s = path;
00046 d = strrchr(s, '.');
00047 if (d == NULL) d = s + strlen(s);
00048 r = calloc(1, 1 + d - path);
00049 memcpy(r, path, d - path);
00050 return (r);
00051 }
00052
00053 static int
00054 process_test(unsigned char *data, size_t n, struct fstate *perfilestate)
00055 {
00056 struct ccn_skeleton_decoder skel_decoder = {0};
00057 struct ccn_skeleton_decoder *d = &skel_decoder;
00058 struct ccn_parsed_ContentObject content;
00059 struct ccn_indexbuf *comps = ccn_indexbuf_create();
00060 const unsigned char * content_value;
00061 size_t content_length;
00062 int res = 0;
00063 size_t s;
00064 unsigned int i;
00065
00066 retry:
00067 s = ccn_skeleton_decode(d, data, n);
00068 if (d->state < 0) {
00069 res = 1;
00070 fprintf(stderr, "error state %d after %d of %d chars\n",
00071 (int)d->state, (int)s, (int)n);
00072 }
00073 else if (s == 0) {
00074 fprintf(stderr, "nothing to do\n");
00075 } else {
00076 if (s < n) {
00077 content_value = NULL;
00078 content_length = 0;
00079 if (ccn_parse_ContentObject(data, s, &content, comps) != 0) {
00080 fprintf(stderr, "unable to parse content object\n");
00081 res = 1;
00082 }
00083 else if (ccn_content_get_value(data, s, &content, &content_value, &content_length) != 0) {
00084 fprintf(stderr, "unable to retrieve content value\n");
00085 res = 1;
00086 }
00087 for (i = 0; i < content_length; i++) {
00088 if (i % 16 == 0) printf("\n%08x ", i);
00089 printf(" %02x", content_value[i]);
00090 }
00091 printf("\n");
00092 data += s;
00093 n -= s;
00094 goto retry;
00095 }
00096 }
00097
00098 if (!CCN_FINAL_DSTATE(d->state)) {
00099 res = 1;
00100 fprintf(stderr, "incomplete state %d after %d of %d chars\n",
00101 (int)d->state, (int)s, (int)n);
00102 } else {
00103 content_value = NULL;
00104 content_length = 0;
00105 if (ccn_parse_ContentObject(data, s, &content, comps) != 0) {
00106 fprintf(stderr, "unable to parse content object\n");
00107 res = 1;
00108 }
00109 else if (ccn_content_get_value(data, s, &content, &content_value, &content_length) != 0) {
00110 fprintf(stderr, "unable to retrieve content value\n");
00111 res = 1;
00112 }
00113 for (i = 0; i < content_length; i++) {
00114 if (i % 16 == 0) printf("\n%08x ", i);
00115 printf(" %02x", content_value[i]);
00116 }
00117 printf("\n\n");
00118 }
00119 return(res);
00120 }
00121
00122 static int
00123 process_fd(int fd, struct fstate *perfilestate)
00124 {
00125 unsigned char *buf;
00126 ssize_t len;
00127 struct stat s;
00128 int res = 0;
00129
00130 res = fstat(fd, &s);
00131 len = s.st_size;
00132 buf = (unsigned char *)mmap((void *)NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
00133 if (buf == (void *)-1) return (1);
00134 fprintf(stderr, " <!-- input is %6lu bytes -->\n", (unsigned long)len);
00135 res |= process_test(buf, len, perfilestate);
00136 munmap((void *)buf, len);
00137 return(res);
00138 }
00139
00140
00141 static int
00142 process_file(char *path, struct fstate *perfilestate)
00143 {
00144 int fd;
00145 int res = 0;
00146
00147 fd = open(path, O_RDONLY);
00148 if (-1 == fd) {
00149 perror(path);
00150 return(1);
00151 }
00152
00153 perfilestate->segnum = 0;
00154 if (perfilestate->prefix != NULL) free(perfilestate->prefix);
00155 perfilestate->prefix = segment_prefix(path);
00156
00157 res = process_fd(fd, perfilestate);
00158 if (fd > 0)
00159 close(fd);
00160 return(res);
00161 }
00162
00163 int
00164 main(int argc, char *argv[])
00165 {
00166 int i;
00167 int res = 0;
00168 struct fstate perfilestate = {0};
00169
00170 for (i = 1; argv[i] != 0; i++) {
00171 fprintf(stderr, "<!-- Processing %s -->\n", argv[i]);
00172 res |= process_file(argv[i], &perfilestate);
00173 }
00174 return(res);
00175 }