00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef lint
00039 static char copyright[] =
00040 "@(#) Copyright (c) 2002\n\
00041 Netherlands Forensic Institute. All rights reserved.\n";
00042 #endif
00043
00044 #if defined(HAVE_CONFIG_H)
00045 #include <config.h>
00046 #endif
00047
00048 #include <assert.h>
00049 #include <memory.h>
00050 #include <stdio.h>
00051 #include <string.h>
00052
00053 #include "rdd.h"
00054 #include "rdd_internals.h"
00055 #include "writer.h"
00056 #include "filter.h"
00057 #include "filterset.h"
00058
00059 #define is_stream_filter(fltr) ((fltr)->block_size <= 0)
00060 #define is_block_filter(fltr) ((fltr)->block_size > 0)
00061
00062 int
00063 rdd_fset_init(RDD_FILTERSET *fset)
00064 {
00065 fset->head = 0;
00066 fset->tail = &fset->head;
00067
00068 return RDD_OK;
00069 }
00070
00071 int
00072 rdd_fset_add(RDD_FILTERSET *fset, const char *name, RDD_FILTER *f)
00073 {
00074 RDD_FSET_NODE *node = 0;
00075 char *filtername = 0;
00076 int rc = RDD_OK;
00077
00078 if (name == 0 || strlen(name) < 1 || f == 0) {
00079 return RDD_BADARG;
00080 }
00081
00082 rc = rdd_fset_get(fset, name, 0);
00083
00084 if (rc == RDD_OK) {
00085 return RDD_EEXISTS;
00086 } else if (rc != RDD_NOTFOUND) {
00087 return rc;
00088 }
00089
00090
00091
00092 if ((node = calloc(1, sizeof(*node))) == 0) {
00093 rc = RDD_NOMEM;
00094 goto error;
00095 }
00096 if ((filtername = malloc(strlen(name) + 1)) == 0) {
00097 rc = RDD_NOMEM;
00098 goto error;
00099 }
00100 strcpy(filtername, name);
00101
00102 node->name = filtername;
00103 node->filter = f;
00104 node->next = 0;
00105
00106
00107
00108 *(fset->tail) = node;
00109 fset->tail = &node->next;
00110
00111 return RDD_OK;
00112
00113 error:
00114 if (filtername != 0) {
00115 free(filtername);
00116 }
00117 if (node != 0) {
00118 free(node);
00119 }
00120 return rc;
00121 }
00122
00123 int
00124 rdd_fset_get(RDD_FILTERSET *fset, const char *name, RDD_FILTER **f)
00125 {
00126 RDD_FSET_NODE *node;
00127
00128
00129
00130 for (node = fset->head; node != 0; node = node->next) {
00131 if (strcmp(node->name, name) == 0) {
00132 if (f != 0) {
00133 *f = node->filter;
00134 }
00135 return RDD_OK;
00136 }
00137 }
00138
00139 if (f != 0) {
00140 *f = 0;
00141 }
00142 return RDD_NOTFOUND;
00143 }
00144
00145 int
00146 rdd_fset_open_cursor(RDD_FILTERSET *fset, RDD_FSET_CURSOR *c)
00147 {
00148 c->current = fset->head;
00149 return RDD_OK;
00150 }
00151
00152 int
00153 rdd_fset_cursor_next(RDD_FSET_CURSOR *c, RDD_FILTER **f)
00154 {
00155 if (c->current == 0) {
00156 if (f != 0) *f = 0;
00157 return RDD_NOTFOUND;
00158 }
00159
00160 if (f != 0) *f = c->current->filter;
00161 c->current = c->current->next;
00162 return RDD_OK;
00163 }
00164
00165 int
00166 rdd_fset_cursor_close(RDD_FSET_CURSOR *c)
00167 {
00168 c->current = 0;
00169 return RDD_OK;
00170 }
00171
00172 int
00173 rdd_fset_push(RDD_FILTERSET *fset, const unsigned char *buf, unsigned nbyte)
00174 {
00175 RDD_FSET_NODE *node;
00176 int rc;
00177
00178 for (node = fset->head; node != 0; node = node->next) {
00179 rc = rdd_filter_push(node->filter, buf, nbyte);
00180 if (rc != RDD_OK) {
00181 return rc;
00182 }
00183 }
00184
00185 return RDD_OK;
00186 }
00187
00188 int
00189 rdd_fset_close(RDD_FILTERSET *fset)
00190 {
00191 RDD_FSET_NODE *node;
00192 int rc;
00193
00194 for (node = fset->head; node != 0; node = node->next) {
00195 rc = rdd_filter_close(node->filter);
00196 if (rc != RDD_OK) {
00197 return rc;
00198 }
00199 }
00200
00201 return RDD_OK;
00202 }
00203
00204 int
00205 rdd_fset_clear(RDD_FILTERSET *fset)
00206 {
00207 RDD_FSET_NODE *node;
00208 RDD_FSET_NODE *next;
00209 int rc;
00210
00211 for (node = fset->head; node != 0; node = next) {
00212 next = node->next;
00213 free(node->name);
00214 node->name = 0;
00215 if ((rc = rdd_filter_free(node->filter)) != RDD_OK) {
00216 return rc;
00217 }
00218 free(node);
00219 }
00220
00221 memset(fset, 0, sizeof(*fset));
00222
00223 return RDD_OK;
00224 }