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 #ifdef HAVE_CONFIG_H
00045 #include "config.h"
00046 #endif
00047
00048 #include <stdio.h>
00049 #include <stdlib.h>
00050 #include <string.h>
00051
00052 #include "rdd.h"
00053 #include "writer.h"
00054 #include "filter.h"
00055
00056 #define is_stream_filter(fltr) ((fltr)->ops->block == 0)
00057 #define is_block_filter(fltr) ((fltr)->ops->block != 0)
00058
00062 int
00063 rdd_new_filter(RDD_FILTER **self, RDD_FILTER_OPS *ops,
00064 unsigned statesize, unsigned blocksize)
00065 {
00066 RDD_FILTER *f = 0;
00067 unsigned char *state = 0;
00068
00069 if (self == 0 || ops == 0){
00070 return RDD_BADARG;
00071 }
00072
00073 if (ops->block != 0 && blocksize == 0){
00074 return RDD_BADARG;
00075 }
00076 if(ops->block == 0 && blocksize > 0){
00077 return RDD_BADARG;
00078 }
00079
00080 if ((f = calloc(1, sizeof(RDD_FILTER))) == 0) {
00081 return RDD_NOMEM;
00082 }
00083
00084 if ((state = calloc(1, statesize)) == 0) {
00085 free(f);
00086 return RDD_NOMEM;
00087 }
00088
00089 f->ops = ops;
00090 f->state = state;
00091 f->pos = 0;
00092 f->blocksize = blocksize;
00093
00094 *self = f;
00095 return RDD_OK;
00096 }
00097
00098 static int
00099 stream_filter_push(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00100 {
00101 RDD_FILTER_OPS *ops = f->ops;
00102
00103 return (*ops->input)(f, buf, nbyte);
00104 }
00105
00106 static int
00107 block_filter_push(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00108 {
00109 RDD_FILTER_OPS *ops = f->ops;
00110 unsigned todo;
00111 int rc;
00112
00113 while (nbyte > 0) {
00114 if (f->pos + nbyte > f->blocksize) {
00115 todo = f->blocksize - f->pos;
00116 } else {
00117 todo = nbyte;
00118 }
00119
00120 rc = (*ops->input)(f, buf, todo);
00121 if (rc != RDD_OK) {
00122 return rc;
00123 }
00124
00125 buf += todo;
00126 nbyte -= todo;
00127 f->pos += todo;
00128 if (f->pos >= f->blocksize) {
00129
00130
00131 rc = (*ops->block)(f, f->pos);
00132 if (rc != RDD_OK) {
00133 return rc;
00134 }
00135 f->pos = 0;
00136 }
00137 }
00138
00139 return RDD_OK;
00140 }
00141
00148 int
00149 rdd_filter_push(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00150 {
00151
00152 if (is_stream_filter(f)) {
00153 return stream_filter_push(f, buf, nbyte);
00154 } else {
00155 return block_filter_push(f, buf, nbyte);
00156 }
00157 }
00158
00159 int
00160 rdd_filter_close(RDD_FILTER *f)
00161 {
00162 RDD_FILTER_OPS *ops = f->ops;
00163 int rc;
00164
00165 if (is_block_filter(f) && f->pos > 0) {
00166 rc = (*ops->block)(f, f->pos);
00167 if (rc != RDD_OK) {
00168 return rc;
00169 }
00170 f->pos = 0;
00171 }
00172
00173 if (ops->close != 0) {
00174 return (*ops->close)(f);
00175 }
00176
00177 return RDD_OK;
00178 }
00179
00180 int
00181 rdd_filter_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte)
00182 {
00183 RDD_FILTER_OPS *ops = f->ops;
00184
00185 if (buf == 0) return RDD_BADARG;
00186
00187 if (ops->get_result == 0) return RDD_NOTFOUND;
00188
00189 return (*ops->get_result)(f, buf, nbyte);
00190 }
00191
00192 int
00193 rdd_filter_free(RDD_FILTER *f)
00194 {
00195 RDD_FILTER_OPS *ops = f->ops;
00196 int rc;
00197
00198 if (ops->free != 0) {
00199 rc = (*ops->free)(f);
00200 if (rc != RDD_OK) {
00201 return rc;
00202 }
00203 }
00204
00205 free(f->state);
00206 f->state = 0;
00207 free(f);
00208
00209 return RDD_OK;
00210 }