version 1.1.1.1, 2012/02/21 16:42:02
|
version 1.1.1.2, 2012/10/09 09:13:23
|
Line 1
|
Line 1
|
/* |
/* |
* Copyright (C) 2008-2010 Daisuke Aoyama <aoyama@peach.ne.jp>. | * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>. |
* All rights reserved. |
* All rights reserved. |
* |
* |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
Line 51
|
Line 51
|
#include "istgt.h" |
#include "istgt.h" |
#include "istgt_misc.h" |
#include "istgt_misc.h" |
|
|
|
#if !defined(__GNUC__) |
|
#undef __attribute__ |
|
#define __attribute__(x) |
|
#endif |
|
|
|
static void fatal(const char *format, ...) __attribute__((__noreturn__, __format__(__printf__, 1, 2))); |
|
|
static void |
static void |
fatal(const char *format, ...) |
fatal(const char *format, ...) |
{ |
{ |
Line 439 istgt_fdump(FILE *fp, const char *label, const uint8_t
|
Line 446 istgt_fdump(FILE *fp, const char *label, const uint8_t
|
{ |
{ |
char tmpbuf[MAX_TMPBUF]; |
char tmpbuf[MAX_TMPBUF]; |
char buf8[8+1]; |
char buf8[8+1]; |
int total; | size_t total; |
int i; | size_t idx; |
|
|
fprintf(fp, "%s\n", label); |
fprintf(fp, "%s\n", label); |
|
|
memset(buf8, 0, sizeof buf8); |
memset(buf8, 0, sizeof buf8); |
total = 0; |
total = 0; |
for (i = 0; i < len; i++) { | for (idx = 0; idx < len; idx++) { |
if (i != 0 && i % 8 == 0) { | if (idx != 0 && idx % 8 == 0) { |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, |
"%s", buf8); | "%s", buf8); |
fprintf(fp, "%s\n", tmpbuf); |
fprintf(fp, "%s\n", tmpbuf); |
total = 0; |
total = 0; |
} |
} |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, |
"%2.2x ", buf[i] & 0xff); | "%2.2x ", buf[idx] & 0xff); |
buf8[i % 8] = isprint(buf[i]) ? buf[i] : '.'; | buf8[idx % 8] = isprint(buf[idx]) ? buf[idx] : '.'; |
} |
} |
for ( ; i % 8 != 0; i++) { | for ( ; idx % 8 != 0; idx++) { |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " "); |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " "); |
buf8[i % 8] = ' '; | buf8[idx % 8] = ' '; |
} |
} |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, "%s", buf8); |
total += snprintf(tmpbuf + total, sizeof tmpbuf - total, "%s", buf8); |
fprintf(fp, "%s\n", tmpbuf); |
fprintf(fp, "%s\n", tmpbuf); |
Line 507 istgt_gen_random(uint8_t *buf, size_t len)
|
Line 514 istgt_gen_random(uint8_t *buf, size_t len)
|
{ |
{ |
#ifdef USE_RANDOM |
#ifdef USE_RANDOM |
long l; |
long l; |
int i; | size_t idx; |
|
|
srandomdev(); |
srandomdev(); |
for (i = 0; i < len; i++) { | for (idx = 0; idx < len; idx++) { |
l = random(); |
l = random(); |
buf[i] = (uint8_t) l; | buf[idx] = (uint8_t) l; |
} |
} |
#else |
#else |
uint32_t r; |
uint32_t r; |
int i; | size_t idx; |
|
|
for (i = 0; i < len; i++) { | for (idx = 0; idx < len; idx++) { |
r = arc4random(); |
r = arc4random(); |
buf[i] = (uint8_t) r; | buf[idx] = (uint8_t) r; |
} |
} |
#endif /* USE_RANDOM */ |
#endif /* USE_RANDOM */ |
} |
} |
Line 529 int
|
Line 536 int
|
istgt_bin2hex(char *buf, size_t len, const uint8_t *data, size_t data_len) |
istgt_bin2hex(char *buf, size_t len, const uint8_t *data, size_t data_len) |
{ |
{ |
const char *digits = "0123456789ABCDEF"; |
const char *digits = "0123456789ABCDEF"; |
int total = 0; | size_t total = 0; |
int i; | size_t idx; |
|
|
if (len < 3) |
if (len < 3) |
return -1; |
return -1; |
Line 540 istgt_bin2hex(char *buf, size_t len, const uint8_t *da
|
Line 547 istgt_bin2hex(char *buf, size_t len, const uint8_t *da
|
total++; |
total++; |
buf[total] = '\0'; |
buf[total] = '\0'; |
|
|
for (i = 0; i < data_len; i++) { | for (idx = 0; idx < data_len; idx++) { |
if (total + 3 > len) { |
if (total + 3 > len) { |
buf[total] = '\0'; |
buf[total] = '\0'; |
return - 1; |
return - 1; |
} |
} |
buf[total] = digits[(data[i] >> 4) & 0x0fU]; | buf[total] = digits[(data[idx] >> 4) & 0x0fU]; |
total++; |
total++; |
buf[total] = digits[data[i] & 0x0fU]; | buf[total] = digits[data[idx] & 0x0fU]; |
total++; |
total++; |
} |
} |
buf[total] = '\0'; |
buf[total] = '\0'; |
Line 560 istgt_hex2bin(uint8_t *data, size_t data_len, const ch
|
Line 567 istgt_hex2bin(uint8_t *data, size_t data_len, const ch
|
const char *digits = "0123456789ABCDEF"; |
const char *digits = "0123456789ABCDEF"; |
const char *dp; |
const char *dp; |
const char *p; |
const char *p; |
int total = 0; | size_t total = 0; |
int n0, n1; |
int n0, n1; |
|
|
p = str; |
p = str; |