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 <string.h>
00050
00051 #include "rdd.h"
00052 #include "rdd_internals.h"
00053
00054 #ifdef HAVE_OPENSSL
00055 #include <openssl/md5.h>
00056 #endif
00057
00058 #include "writer.h"
00059 #include "filter.h"
00060
00061 typedef struct _RDD_MD5_STREAM_FILTER {
00062 MD5_CTX md5_state;
00063 unsigned char result[MD5_DIGEST_LENGTH];
00064 } RDD_MD5_STREAM_FILTER;
00065
00066 static int md5_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte);
00067 static int md5_close(RDD_FILTER *f);
00068 static int md5_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte);
00069
00070 static RDD_FILTER_OPS md5_ops = {
00071 md5_input,
00072 0,
00073 md5_close,
00074 md5_get_result,
00075 0
00076 };
00077
00078 int
00079 rdd_new_md5_streamfilter(RDD_FILTER **self)
00080 {
00081 RDD_FILTER *f;
00082 RDD_MD5_STREAM_FILTER *state;
00083 int rc;
00084
00085 if (self == 0) {
00086 return RDD_BADARG;
00087 }
00088
00089 rc = rdd_new_filter(&f, &md5_ops, sizeof(RDD_MD5_STREAM_FILTER), 0);
00090 if (rc != RDD_OK) {
00091 return rc;
00092 }
00093 state = (RDD_MD5_STREAM_FILTER *) f->state;
00094
00095 MD5_Init(&state->md5_state);
00096
00097 *self = f;
00098 return RDD_OK;
00099 }
00100
00101 static int
00102 md5_input(RDD_FILTER *f, const unsigned char *buf, unsigned nbyte)
00103 {
00104
00105
00106 RDD_MD5_STREAM_FILTER *state = (RDD_MD5_STREAM_FILTER *) f->state;
00107
00108 MD5_Update(&state->md5_state, buf, nbyte);
00109
00110 return RDD_OK;
00111 }
00112
00113 static int
00114 md5_close(RDD_FILTER *f)
00115 {
00116 if (f == 0) {
00117 return RDD_BADARG;
00118 }
00119
00120 RDD_MD5_STREAM_FILTER *state = (RDD_MD5_STREAM_FILTER *) f->state;
00121
00122 MD5_Final(state->result, &state->md5_state);
00123
00124 return RDD_OK;
00125 }
00126
00127 static int
00128 md5_get_result(RDD_FILTER *f, unsigned char *buf, unsigned nbyte)
00129 {
00130 if (f == 0) {
00131 return RDD_BADARG;
00132 }
00133
00134 RDD_MD5_STREAM_FILTER *state = (RDD_MD5_STREAM_FILTER *) f->state;
00135
00136 if (nbyte < MD5_DIGEST_LENGTH) return RDD_ESPACE;
00137
00138 memcpy(buf, state->result, MD5_DIGEST_LENGTH);
00139
00140 return RDD_OK;
00141 }