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
|
# File 'ext/postfix_status_line_core.c', line 318
static VALUE rb_postfix_status_line_parse(VALUE self, VALUE v_str, VALUE v_mask, VALUE v_hash, VALUE v_salt, VALUE v_parse_time, VALUE v_sha_algo) {
Check_Type(v_str, T_STRING);
char *str = RSTRING_PTR(v_str);
size_t len = RSTRING_LEN(v_str);
if (len < 1) {
return Qnil;
}
bool mask = rb_value_to_bool(v_mask);
bool parse_time = rb_value_to_bool(v_parse_time);
bool include_hash = false;
char *salt = NULL;
size_t salt_len = -1;
if (rb_value_to_bool(v_hash)) {
#ifdef HAVE_OPENSSL_SHA_H
include_hash = true;
if (!NIL_P(v_salt)) {
Check_Type(v_salt, T_STRING);
salt = RSTRING_PTR(v_salt);
salt_len = RSTRING_LEN(v_salt);
}
#else
rb_raise(rb_eArgError, "OpenSSL is not linked");
#endif // HAVE_OPENSSL_SHA_H
}
int sha_algo = 512;
if (!NIL_P(v_sha_algo)) {
#ifdef HAVE_OPENSSL_SHA_H
sha_algo = NUM2INT(v_sha_algo);
#else
rb_raise(rb_eArgError, "OpenSSL is not linked");
#endif // HAVE_OPENSSL_SHA_H
}
DIGEST_SHA digest_sha_func = NULL;
#ifdef HAVE_OPENSSL_SHA_H
switch (sha_algo) {
case 1:
digest_sha_func = digest_sha;
break;
case 224:
digest_sha_func = digest_sha224;
break;
case 256:
digest_sha_func = digest_sha256;
break;
case 384:
digest_sha_func = digest_sha384;
break;
case 512:
digest_sha_func = digest_sha512;
break;
default:
rb_raise(rb_eArgError, "Invalid SHA algorithm");
}
#endif // HAVE_OPENSSL_SHA_H
char buf[len + 1];
strncpy(buf, str, len);
buf[len] = '\0';
char *tm, *hostname, *process, *queue_id, *attrs;
if (!split_line1(buf, &tm, &hostname, &process, &queue_id, &attrs)) {
return Qnil;
}
VALUE hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("time"), rb_str_new2(tm));
rb_hash_aset(hash, rb_str_new2("hostname"), rb_str_new2(hostname));
rb_hash_aset(hash, rb_str_new2("process"), rb_str_new2(process));
rb_hash_aset(hash, rb_str_new2("queue_id"), rb_str_new2(queue_id));
if (parse_time) {
put_epoch(tm, hash);
}
split_line2(attrs, mask, hash, include_hash, salt, salt_len, digest_sha_func);
return hash;
}
|