1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2007-2015, University of Amsterdam,
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <errno.h>
#include "md5.h"
#include "sha1/sha1.h"
#include "sha1/sha2.h"
static atom_t ATOM_algorithm;
static atom_t ATOM_close_parent;
static atom_t ATOM_md5;
static atom_t ATOM_sha1;
static atom_t ATOM_sha224;
static atom_t ATOM_sha256;
static atom_t ATOM_sha384;
static atom_t ATOM_sha512;
typedef enum hash_algorithm
{ ALGORITHM_MD5,
ALGORITHM_SHA1,
ALGORITHM_SHA224,
ALGORITHM_SHA256,
ALGORITHM_SHA384,
ALGORITHM_SHA512
} hash_algorithm;
/*******************************
* MD5 *
*******************************/
static int
unify_digest(term_t t, unsigned char *digest, size_t len)
{ char hex_output[len*2];
int di;
char *pi;
static char hexd[] = "0123456789abcdef";
for(pi=hex_output, di = 0; di < len; ++di)
{ *pi++ = hexd[(digest[di] >> 4) & 0x0f];
*pi++ = hexd[digest[di] & 0x0f];
}
return PL_unify_atom_nchars(t, len*2, hex_output);
}
/*******************************
* TYPES *
*******************************/
#define BUFSIZE SIO_BUFSIZE /* raw I/O buffer */
typedef struct hash_context
{ IOSTREAM *stream; /* Original stream */
IOSTREAM *hash_stream; /* Stream I'm handle of */
IOENC parent_encoding; /* original encoding */
int close_parent; /* close parent stream */
hash_algorithm algorithm;
unsigned long digest_size;
union
{ md5_state_t md5;
sha1_ctx sha1;
sha2_ctx sha2;
} state;
} hash_context;
static hash_context*
alloc_hash_context(IOSTREAM *s)
{ hash_context *ctx = PL_malloc(sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
ctx->stream = s;
return ctx;
}
static void
free_hash_context(hash_context *ctx)
{ if ( ctx->stream->upstream )
Sset_filter(ctx->stream, NULL);
PL_free(ctx);
}
/*******************************
* RANGE LIMITED INPUT *
*******************************/
static void
hash_append(hash_context *ctx, void *data, size_t size)
{ switch ( ctx->algorithm )
{ case ALGORITHM_MD5:
md5_append(&ctx->state.md5, data, size);
break;
case ALGORITHM_SHA1:
sha1_hash(data, size, &ctx->state.sha1);
break;
default:
sha2_hash(data, size, &ctx->state.sha2);
break;
}
}
static ssize_t /* range-limited read */
hash_read(void *handle, char *buf, size_t size)
{ hash_context *ctx = handle;
ssize_t rd;
if ( (rd = Sfread(buf, sizeof(char), size, ctx->stream)) >= 0 )
{ hash_append(ctx, buf, rd);
return rd;
}
return rd;
}
static ssize_t
hash_write(void *handle, char *buf, size_t size)
{ hash_context *ctx = handle;
size_t written = 0;
hash_append(ctx, buf, size);
while ( written < size )
{ ssize_t wr = Sfwrite(buf+written, sizeof(char), size, ctx->stream);
if ( wr >= 0 )
{ written += wr;
} else
return wr;
}
return size;
}
static int
hash_control(void *handle, int op, void *data)
{ hash_context *ctx = handle;
switch(op)
{ case SIO_SETENCODING:
return 0; /* allow switching encoding */
default:
if ( ctx->stream->functions->control )
return (*ctx->stream->functions->control)(ctx->stream->handle, op, data);
return -1;
}
}
static int
hash_close(void *handle)
{ int rc = 0;
hash_context *ctx = handle;
ctx->stream->encoding = ctx->parent_encoding;
if ( ctx->stream->upstream )
Sset_filter(ctx->stream, NULL);
if ( ctx->close_parent )
rc = Sclose(ctx->stream);
free_hash_context(ctx);
return rc;
}
static IOFUNCTIONS hash_functions =
{ hash_read,
hash_write,
NULL, /* seek */
hash_close,
hash_control, /* control */
NULL, /* seek64 */
};
/*******************************
* PROLOG CONNECTION *
*******************************/
static int
get_hash_algorithm(term_t t, hash_algorithm *ap)
{ atom_t a;
if ( PL_get_atom_ex(t, &a) )
{ hash_algorithm alg;
if ( a == ATOM_md5 )
alg = ALGORITHM_MD5;
else if ( a == ATOM_sha1 )
alg = ALGORITHM_SHA1;
else if ( a == ATOM_sha224 )
alg = ALGORITHM_SHA224;
else if ( a == ATOM_sha256 )
alg = ALGORITHM_SHA256;
else if ( a == ATOM_sha384 )
alg = ALGORITHM_SHA384;
else if ( a == ATOM_sha512 )
alg = ALGORITHM_SHA512;
else
return PL_domain_error("algorithm", t);
*ap = alg;
return TRUE;
}
return FALSE;
}
static unsigned long
digest_size(hash_algorithm algorithm)
{ switch(algorithm)
{ case ALGORITHM_MD5: return 16;
case ALGORITHM_SHA1: return SHA1_DIGEST_SIZE;
case ALGORITHM_SHA224: return SHA224_DIGEST_SIZE;
case ALGORITHM_SHA256: return SHA256_DIGEST_SIZE;
case ALGORITHM_SHA384: return SHA384_DIGEST_SIZE;
case ALGORITHM_SHA512: return SHA512_DIGEST_SIZE;
default: assert(0); return(0);
}
}
#define COPY_FLAGS (SIO_INPUT|SIO_OUTPUT| \
SIO_TEXT| \
SIO_REPXML|SIO_REPPL|\
SIO_RECORDPOS)
static foreign_t
pl_stream_hash_open(term_t org, term_t new, term_t options)
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
hash_context *ctx;
IOSTREAM *s, *s2;
hash_algorithm algorithm = ALGORITHM_SHA1;
int close_parent = TRUE;
while(PL_get_list_ex(tail, head, tail))
{ atom_t name;
size_t arity;
term_t arg = PL_new_term_ref();
if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
return PL_type_error("option", head);
_PL_get_arg(1, head, arg);
if ( name == ATOM_algorithm )
{ if ( !get_hash_algorithm(arg, &algorithm) )
return FALSE;
} else if ( name == ATOM_close_parent )
{ if ( !PL_get_bool_ex(arg, &close_parent) )
return FALSE;
}
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
if ( !PL_get_stream_handle(org, &s) )
return FALSE; /* Error */
ctx = alloc_hash_context(s);
ctx->close_parent = close_parent;
ctx->algorithm = algorithm;
ctx->digest_size = digest_size(algorithm);
switch ( algorithm )
{ case ALGORITHM_MD5:
md5_init(&ctx->state.md5);
break;
case ALGORITHM_SHA1:
sha1_begin(&ctx->state.sha1);
break;
default:
sha2_begin(ctx->digest_size, &ctx->state.sha2);
break;
}
if ( !(s2 = Snew(ctx,
(s->flags©_FLAGS)|SIO_FBUF,
&hash_functions)) )
{ PL_release_stream(s);
free_hash_context(ctx); /* no memory */
return FALSE;
}
s2->encoding = s->encoding;
ctx->parent_encoding = s->encoding;
s->encoding = ENC_OCTET;
ctx->hash_stream = s2;
if ( PL_unify_stream(new, s2) )
{ Sset_filter(s, s2);
PL_release_stream(s);
return TRUE;
} else
{ PL_release_stream(s);
return FALSE;
}
}
static foreign_t
pl_stream_hash(term_t stream, term_t hash)
{ IOSTREAM *s;
if ( PL_get_stream_handle(stream, &s) )
{ unsigned char hval[SHA2_MAX_DIGEST_SIZE];
hash_context *ctx = s->handle;
int rc;
if ( Sferror(s) || ((s->flags & SIO_OUTPUT) && Sflush(s) < 0) )
return PL_release_stream(s);
if ( s->functions != &hash_functions )
{ PL_release_stream(s);
return PL_domain_error("hash_stream", stream);
}
switch(ctx->algorithm)
{ case ALGORITHM_MD5:
md5_finish(&ctx->state.md5, (md5_byte_t*)hval);
break;
case ALGORITHM_SHA1:
sha1_end(hval, &ctx->state.sha1);
break;
default:
sha2_end(hval, &ctx->state.sha2);
break;
}
rc = unify_digest(hash, hval, ctx->digest_size);
PL_release_stream(s);
return rc;
}
return FALSE;
}
/*******************************
* INSTALL *
*******************************/
#define MKATOM(n) ATOM_ ## n = PL_new_atom(#n);
void
install_hashstream(void)
{ MKATOM(md5);
MKATOM(sha1); /* =160 */
MKATOM(sha224);
MKATOM(sha256);
MKATOM(sha384);
MKATOM(sha512);
MKATOM(algorithm);
MKATOM(close_parent);
PL_register_foreign("open_hash_stream", 3, pl_stream_hash_open, 0);
PL_register_foreign("stream_hash", 2, pl_stream_hash, 0);
}
|