Changeset 133 for trunk/src/sh_tiger0.c


Ignore:
Timestamp:
Oct 25, 2007, 12:19:05 AM (17 years ago)
Author:
rainer
Message:

Reentrant checksum/hash functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_tiger0.c

    r131 r133  
    9999 * implementation
    100100 */
    101 SL_TICKET tiger_fd = (-1);
    102101
    103102static sh_byte buffer[PRIV_MAX + 72];
     
    106105static
    107106word64 * sh_tiger_hash_val (const char * filename, TigerType what,
    108                             UINT64 Length, int timeout)
     107                            UINT64 Length, int timeout, word64 * res)
    109108#else
    110109static
    111110sh_word32 * sh_tiger_hash_val (const char * filename, TigerType what,
    112                                UINT64 Length, int timeout)
     111                               UINT64 Length, int timeout, sh_word32 * res)
    113112#endif
    114113{
     
    138137#if defined(TIGER_64_BIT)
    139138#define TIGER_CAST (word64*)
    140   static word64 res[3];
     139  /* word64 res[3]; */
    141140  res[0]= (word64) 0x0123456789ABCDEFLL;
    142141  res[1]= (word64) 0xFEDCBA9876543210LL;
     
    144143#else
    145144#define TIGER_CAST (sh_word32*)
    146   static sh_word32 res[6];
     145  /* sh_word32 res[6]; */
    147146  res[0]= (sh_word32) 0x89ABCDEF;
    148147  res[1]= (sh_word32) 0x01234567;
     
    155154  SL_ENTER(_("sh_tiger_hash_val"));
    156155
    157   if (what == TIGER_FD || what == TIGER_FILE)
     156  if (what >= TIGER_FILE)
    158157    {
    159       if (what == TIGER_FD)
     158      if (what > TIGER_FD)
    160159        {
    161           fd = tiger_fd;
    162           TPT((0,FIL__, __LINE__, _("msg=<TIGER_FD>, fd=<%ld>\n"), tiger_fd));
     160          fd = what;
     161          TPT((0,FIL__, __LINE__, _("msg=<TIGER_FD>, fd=<%ld>\n"), fd));
    163162        }
    164163      else
     
    171170      if (SL_ISERROR (fd))
    172171        {
    173           TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), tiger_fd));
     172          TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
    174173          tmp = sh_util_safe_name (filename);
    175174          (void) sl_get_euid(&euid);
     
    179178          SL_RETURN( NULL, _("sh_tiger_hash_val"));
    180179        }
    181      
     180
     181      SH_MUTEX_LOCK(mutex_skey); 
    182182#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
    183183    if ((lockflag == SL_FALSE) && (skey->mlock_failed == SL_FALSE))
     
    196196      }
    197197#endif
     198    SH_MUTEX_UNLOCK(mutex_skey);
    198199
    199200#ifdef TIGER_DBG
     
    254255
    255256        nblocks += blk;
     257        /* MAY_LOCK */
    256258        sh.statistics.bytes_hashed += tt;
    257259       
     
    331333          bbuf[j++] = (sh_byte) 0;
    332334        tiger_t(TIGER_CAST bbuf, 64, res);
     335        /* MAY_LOCK */
    333336        sh.statistics.bytes_hashed += 64;
    334337        ++nblocks; ncount = 0;
     
    361364    if (what == TIGER_FILE)
    362365      (void) sl_close (fd);
    363     else
    364       tiger_fd = (-1);
    365366
    366367    SL_RETURN( res, _("sh_tiger_hash_val"));
     
    807808   beginning at RESBLOCK.  */
    808809static int md5_stream(char * filename, void *resblock,
    809                       UINT64 Length, int timeout)
     810                      UINT64 Length, int timeout, SL_TICKET fd)
    810811{
    811812  /* Important: BLOCKSIZE must be a multiple of 64.  */
     
    830831  (void) md5Reset (&ctx);
    831832
    832   fd = tiger_fd;
    833 
    834833  if (SL_ISERROR (fd))
    835834    {
    836       TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), tiger_fd));
     835      TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
    837836      tmp = sh_util_safe_name (filename);
    838837      (void) sl_get_euid(&euid);
     
    932931static
    933932char * sh_tiger_md5_hash  (char * filename, TigerType what,
    934                            UINT64 Length, int timeout)
     933                           UINT64 Length, int timeout, char * out, size_t len)
    935934{
    936935  int cnt;
    937   static char out[KEY_LEN+1];
     936  char outbuf[KEY_LEN+1];
    938937  unsigned char md5buffer[16];
    939938
    940   if (what != TIGER_FD)
    941     {
    942       sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
    943                        MSG_E_SUBGEN, _("Not TIGER_FD"),
    944                        _("sh_tiger_md5_hash"));
    945       out[0] = '\0';
    946       return out;
    947     }
    948 
    949   (void) md5_stream (filename, md5buffer, Length, timeout);
     939  (void) md5_stream (filename, md5buffer, Length, timeout, what);
    950940
    951941  /*@-bufferoverflowhigh -usedef@*/
    952942  for (cnt = 0; cnt < 16; ++cnt)
    953     sprintf (&out[cnt*2], _("%02X"),                 /* known to fit  */
     943    sprintf (&outbuf[cnt*2], _("%02X"),                 /* known to fit  */
    954944             (unsigned int) md5buffer[cnt]);
    955945  /*@+bufferoverflowhigh +usedef@*/
    956946  for (cnt = 32; cnt < KEY_LEN; ++cnt)
    957     out[cnt] = '0';
    958   out[KEY_LEN] = '\0';
    959 
     947    outbuf[cnt] = '0';
     948  outbuf[KEY_LEN] = '\0';
     949
     950  sl_strlcpy(out, outbuf, len);
    960951  return out;
    961952}
     
    13281319   beginning at RESBLOCK.  */
    13291320static int sha1_stream(char * filename, void *resblock,
    1330                        UINT64 Length, int timeout)
     1321                       UINT64 Length, int timeout, SL_TICKET fd)
    13311322{
    13321323  /* Important: BLOCKSIZE must be a multiple of 64.  */
     
    13501341  (void) sha_init(&ctx);
    13511342
    1352   fd = tiger_fd;
    1353 
    13541343  if (SL_ISERROR (fd))
    13551344    {
    1356       TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), tiger_fd));
     1345      TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
    13571346      tmp = sh_util_safe_name (filename);
    13581347      (void) sl_get_euid(&euid);
     
    14571446
    14581447static char * sh_tiger_sha1_hash  (char * filename, TigerType what,
    1459                                    UINT64 Length, int timeout)
     1448                                   UINT64 Length, int timeout,
     1449                                   char * out, size_t len)
    14601450{
    14611451  int cnt = (int) Length;  /* fix compiler warning */
    1462   static char out[KEY_LEN+1];
     1452  char outbuf[KEY_LEN+1];
    14631453  unsigned char sha1buffer[20];
    14641454
    1465   if (what != TIGER_FD)
    1466     {
    1467       sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
    1468                        MSG_E_SUBGEN, _("Not TIGER_FD"),
    1469                        _("sh_tiger_sha1_hash"));
    1470       out[0] = '\0';
    1471       return out;
    1472     }
    1473 
    1474   (void) sha1_stream (filename, sha1buffer, Length, timeout);
     1455  (void) sha1_stream (filename, sha1buffer, Length, timeout, what);
    14751456
    14761457  /*@-bufferoverflowhigh -usedef@*/
    14771458  for (cnt = 0; cnt < 20; ++cnt)
    1478     sprintf (&out[cnt*2], _("%02X"),                 /* known to fit  */
     1459    sprintf (&outbuf[cnt*2], _("%02X"),              /* known to fit  */
    14791460             (unsigned int) sha1buffer[cnt]);
    14801461  /*@+bufferoverflowhigh +usedef@*/
    14811462  for (cnt = 40; cnt < KEY_LEN; ++cnt)
    1482     out[cnt] = '0';
    1483   out[KEY_LEN] = '\0';
    1484 
     1463    outbuf[cnt] = '0';
     1464  outbuf[KEY_LEN] = '\0';
     1465
     1466  sl_strlcpy(out, outbuf, len);
    14851467  return out;
    14861468}
     
    15231505
    15241506static char * sh_tiger_hash_internal (const char * filename, TigerType what,
    1525                                       UINT64 Length, int timeout);
     1507                                      UINT64 Length, int timeout,
     1508                                      char * out, size_t len);
    15261509
    15271510char * sh_tiger_hash (const char * filename, TigerType what,
    1528                       UINT64 Length)
    1529 {
    1530   return sh_tiger_hash_internal (filename, what, Length, 0);
     1511                      UINT64 Length, char * out, size_t len)
     1512{
     1513  return sh_tiger_hash_internal (filename, what, Length, 0, out,len);
    15311514}
    15321515
    15331516char * sh_tiger_generic_hash (char * filename, TigerType what,
    1534                               UINT64 Length, int timeout)
     1517                              UINT64 Length, int timeout,
     1518                              char * out, size_t len)
    15351519{
    15361520#ifdef USE_SHA1
    15371521  if (hash_type == 1)
    1538     return sh_tiger_sha1_hash    (filename, what, Length, timeout);
     1522    return sh_tiger_sha1_hash    (filename, what, Length, timeout, out, len);
    15391523#endif
    15401524#ifdef USE_MD5
    15411525  if (hash_type == 2)
    1542     return sh_tiger_md5_hash     (filename, what, Length, timeout);
    1543 #endif
    1544   return sh_tiger_hash_internal  (filename, what, Length, timeout);
     1526    return sh_tiger_md5_hash     (filename, what, Length, timeout, out, len);
     1527#endif
     1528  return sh_tiger_hash_internal  (filename, what, Length, timeout, out, len);
    15451529}
    15461530
     
    15491533 
    15501534static char * sh_tiger_hash_internal (const char * filename, TigerType what,
    1551                                       UINT64 Length, int timeout)
     1535                                      UINT64 Length, int timeout,
     1536                                      char * out, size_t len)
    15521537{
    15531538#if defined(TIGER_64_BIT)
    1554   word64 * res;
     1539  word64 res[3];
    15551540#else
    1556   sh_word32 * res;
    1557 #endif
    1558   static char out[KEY_LEN+1];
     1541  sh_word32 res[6];
     1542#endif
    15591543
    15601544  SL_ENTER( _("sh_tiger_hash_internal"));
    15611545
    1562   res = sh_tiger_hash_val (filename, what, Length, timeout);
    1563 
    1564   if (res != NULL)
     1546  SH_VALIDATE_GE(len, (KEY_LEN+1));
     1547
     1548  if (NULL != sh_tiger_hash_val (filename, what, Length, timeout, res))
    15651549    {
    15661550#if defined(TIGER_64_BIT)
    1567       sl_snprintf(out,
    1568                   sizeof(out),
     1551      sl_snprintf(out, len,
    15691552                  MYFORMAT,
    15701553                  (sh_word32)(res[0]>>32),
     
    15751558                  (sh_word32)(res[2]) );
    15761559#else
    1577       sl_snprintf(out,
    1578                   sizeof(out),
     1560      sl_snprintf(out, len,
    15791561                  MYFORMAT,
    15801562                  (sh_word32)(res[1]),
     
    15851567                  (sh_word32)(res[4]) );
    15861568#endif
    1587       out[sizeof(out)-1] = '\0';
     1569      out[len-1] = '\0';
    15881570      SL_RETURN( out, _("sh_tiger_hash_internal"));
    15891571
     
    16001582  char   outhash[48+6+1];
    16011583#if defined(TIGER_64_BIT)
    1602   word64 * res;
     1584  word64 res[3];
    16031585#else
    1604   sh_word32 * res;
     1586  sh_word32 res[6];
    16051587#endif
    16061588
    16071589  SL_ENTER(_("sh_tiger_hash_gpg"));
    16081590
    1609   res = sh_tiger_hash_val (filename, what, Length, 0);
    1610   if (res != NULL)
     1591  if (NULL != sh_tiger_hash_val (filename, what, Length, 0, res))
    16111592    {
    16121593#if defined(TIGER_64_BIT)
     
    16631644UINT32 * sh_tiger_hash_uint32 (char * filename,
    16641645                               TigerType what,
    1665                                UINT64 Length)
     1646                               UINT64 Length, UINT32 * out, size_t len)
    16661647{
    16671648#if defined(TIGER_64_BIT)
    1668   word64 * res;
     1649  word64 res[3];
    16691650#else
    1670   sh_word32 * res;
    1671 #endif
    1672 
    1673   static UINT32 out[6];
     1651  sh_word32 res[6];
     1652#endif
    16741653
    16751654  SL_ENTER(_("sh_tiger_hash_uint32"));
     1655
     1656  SH_VALIDATE_GE(len, 6);
    16761657
    16771658  out[0] = 0; out[1] = 0; out[2] = 0;
    16781659  out[3] = 0; out[4] = 0; out[5] = 0;
    16791660
    1680   res = sh_tiger_hash_val (filename,  what,  Length, 0);
    1681 
    1682   if (res != NULL)
     1661  if (NULL != sh_tiger_hash_val (filename,  what,  Length, 0, res))
    16831662    {
    16841663#if defined(TIGER_64_BIT)
Note: See TracChangeset for help on using the changeset viewer.