LCOV - code coverage report
Current view: top level - src - ikey_dll.c (source / functions) Coverage Total Hit
Test: InChI Unit Test Coverage Lines: 0.0 % 202 0
Test Date: 2026-12-03 11:58:49 Functions: 0.0 % 4 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 158 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * SPDX-License-Identifier: MIT
       3                 :             :  * Copyright (c) 2024 IUPAC and InChI Trust
       4                 :             :  */
       5                 :             : 
       6                 :             : /* InChIKey: calculation of hash for InChI string. Uses truncated SHA-256 function. */
       7                 :             : 
       8                 :             : #ifdef _MSC_VER
       9                 :             : #if _MSC_VER > 1000
      10                 :             : #pragma warning( disable : 4996 )
      11                 :             : #endif
      12                 :             : #endif
      13                 :             : 
      14                 :             : #include <stdlib.h>
      15                 :             : #include <string.h>
      16                 :             : #include <ctype.h>
      17                 :             : 
      18                 :             : #include "sha2.h"
      19                 :             : #include "ikey_base26.h"
      20                 :             : 
      21                 :             : #include "mode.h"
      22                 :             : #include "inchi_api.h"
      23                 :             : #include "util.h"
      24                 :             : 
      25                 :             : #include "bcf_s.h"
      26                 :             : 
      27                 :             : /*    Local options */
      28                 :             : 
      29                 :             : #define INCHIKEY_DEBUG 0 /* 2 */
      30                 :             : 
      31                 :             : #define INCHIKEY_FLAG_OK 0
      32                 :             : #define INCHIKEY_NOT_VALID_FLAG 1
      33                 :             : 
      34                 :             : enum { MINOUTLENGTH = 256 };
      35                 :             : 
      36                 :             : 
      37                 :             : 
      38                 :             : /*    Local functions */
      39                 :             : 
      40                 :             : void fprint_digest( FILE* fw, const char *header, unsigned char *a );
      41                 :             : 
      42                 :             : 
      43                 :             : /*
      44                 :             :     EXPORTED FUNCTIONS
      45                 :             : */
      46                 :             : 
      47                 :             : 
      48                 :             : /****************************************************************************/
      49                 :             : EXPIMP_TEMPLATE INCHI_API
      50                 :           0 : int INCHI_DECL GetStdINCHIKeyFromStdINCHI( const char* szINCHISource,
      51                 :             :                                            char* szINCHIKey )
      52                 :             : {
      53         [ #  # ]:           0 :     if (strlen( szINCHISource ) < LEN_INCHI_STRING_PREFIX + 3)
      54                 :             :     {
      55                 :           0 :         return INCHIKEY_INVALID_STD_INCHI;
      56                 :             :     }
      57         [ #  # ]:           0 :     if (szINCHISource[LEN_INCHI_STRING_PREFIX + 1] != 'S')
      58                 :             :     {
      59                 :           0 :         return INCHIKEY_INVALID_STD_INCHI;
      60                 :             :     }
      61                 :             : 
      62                 :           0 :     return GetINCHIKeyFromINCHI( szINCHISource,
      63                 :             :                                  0, 0,
      64                 :             :                                  szINCHIKey,
      65                 :             :                                  (char *) NULL, (char *) NULL );
      66                 :             : }
      67                 :             : 
      68                 :             : 
      69                 :             : /****************************************************************************/
      70                 :             : EXPIMP_TEMPLATE INCHI_API
      71                 :           0 : int INCHI_DECL GetINCHIKeyFromINCHI( const char* szINCHISource,
      72                 :             :                                      const int xtra1,
      73                 :             :                                      const int xtra2,
      74                 :             :                                      char* szINCHIKey,
      75                 :             :                                      char* szXtra1,
      76                 :             :                                      char* szXtra2 )
      77                 :             : {
      78                 :           0 :     int ret = INCHIKEY_OK;
      79                 :           0 :     int ret1 = INCHIKEY_OK;
      80                 :             :     int cn;
      81                 :           0 :     size_t slen, i, j, jproto = 0, ncp, pos_slash1 = 0;
      82                 :           0 :     char *str = NULL, *smajor = NULL, *sminor = NULL,
      83                 :           0 :         *sproto = NULL, *stmp = NULL, tmp[MINOUTLENGTH];
      84                 :             :     unsigned char
      85                 :             :         digest_major[32], digest_minor[32];
      86                 :             : 
      87                 :           0 :     char flagstd = 'S', /* standard key */
      88                 :           0 :         flagnonstd = 'N', /* non-standard key */
      89                 :           0 :         flagexptl = 'B', /* experimental ('beta') key */
      90                 :           0 :         flagsubver = 'A', /* base InChI subversion */
      91                 :           0 :         flagproto = 'N'; /* no [de]protonization , by default */
      92                 :             :     int  nprotons;
      93                 :             :     /*
      94                 :             :     Protonization encoding:
      95                 :             :     N 0
      96                 :             :     O +1 P +2 Q +3 R +4 S +5 T +6 U +7 V +8 W +9 X +10 Y +11 Z +12
      97                 :             :     M -1 L-2 K -3 J -4 I -5 H -6 G -7 F -8 E -9 D -10 C -11 B -12
      98                 :             :     A < -12 or > +12
      99                 :             :     */
     100                 :             :     static const char *pplus = "OPQRSTUVWXYZ";
     101                 :             :     static const char *pminus = "MLKJIHGFEDCB";
     102                 :           0 :     int is_stdinchi = 0;    /* 0 -  non-standard,
     103                 :             :                                1    standard
     104                 :             :                               -1    experimental ('beta') */
     105                 :             : 
     106                 :           0 :     int is_molecular_inorganic = 0;
     107                 :             : 
     108                 :             : 
     109                 :             : 
     110         [ #  # ]:           0 :     if (NULL != szXtra1) /* Software version 1.06 added check to fix bug with NULL szXtra, thanks to WDI */
     111                 :             :     {
     112                 :           0 :         szXtra1[0] = '\0';
     113                 :             :     }
     114         [ #  # ]:           0 :     if (NULL != szXtra2)
     115                 :             :     {
     116                 :           0 :         szXtra2[0] = '\0';
     117                 :             :     }
     118                 :             : 
     119                 :             :     /* Check if input is a valid InChI string */
     120                 :             : 
     121                 :             :     /* .. non-empty */
     122         [ #  # ]:           0 :     if (szINCHISource == NULL)
     123                 :             :     {
     124                 :           0 :         return INCHIKEY_EMPTY_INPUT;
     125                 :             :     }
     126                 :             : 
     127                 :           0 :     slen = strlen( szINCHISource );
     128                 :             : 
     129                 :             :     /* .. has valid prefix */
     130         [ #  # ]:           0 :     if (slen < LEN_INCHI_STRING_PREFIX + 3)
     131                 :             :     {
     132                 :           0 :         return INCHIKEY_INVALID_INCHI_PREFIX;
     133                 :             :     }
     134         [ #  # ]:           0 :     if (memcmp( szINCHISource, INCHI_STRING_PREFIX, LEN_INCHI_STRING_PREFIX ))
     135                 :             :     {
     136                 :           0 :         return INCHIKEY_INVALID_INCHI_PREFIX;
     137                 :             :     }
     138                 :             : 
     139                 :             :     /* .. has InChI version 1 */
     140                 :             :     /* if (!isdigit(szINCHISource[LEN_INCHI_STRING_PREFIX]) )  */
     141         [ #  # ]:           0 :     if (szINCHISource[LEN_INCHI_STRING_PREFIX] != '1')
     142                 :           0 :         return INCHIKEY_INVALID_INCHI_PREFIX;
     143                 :             : 
     144                 :             :     /* Determine the InChI calculation-class designator.
     145                 :             :      * Historical InChI=1/... remains accepted as non-standard input.
     146                 :             :      */
     147                 :           0 :     pos_slash1 = LEN_INCHI_STRING_PREFIX + 1;
     148                 :             : 
     149         [ #  # ]:           0 :     if (szINCHISource[pos_slash1] == 'S')
     150                 :             :     {
     151                 :             :         /* Standard InChI */
     152                 :           0 :         is_stdinchi = 1;
     153                 :           0 :         pos_slash1++;
     154                 :             :     }
     155         [ #  # ]:           0 :     else if (szINCHISource[pos_slash1] == 'B')
     156                 :             :     {
     157                 :             :         /* Experimental ('beta') InChI */
     158                 :           0 :         is_stdinchi = -1;
     159                 :           0 :         pos_slash1++;
     160                 :             :     }
     161         [ #  # ]:           0 :     else if (szINCHISource[pos_slash1] == 'N')
     162                 :             :     {
     163                 :             :         /* Explicitly marked non-standard InChI */
     164                 :           0 :         is_stdinchi = 0;
     165                 :           0 :         pos_slash1++;
     166                 :             :     }
     167                 :             : 
     168                 :             :     /* Molecular Inorganics subversion designator */
     169         [ #  # ]:           0 :     if (szINCHISource[pos_slash1] == 'B')
     170                 :             :     {
     171                 :           0 :         is_molecular_inorganic = 1;
     172                 :           0 :         pos_slash1++;
     173                 :             :     }
     174                 :             : 
     175                 :             :     /* .. has trailing slash in the right place */
     176         [ #  # ]:           0 :     if (szINCHISource[pos_slash1] != '/')
     177                 :             :     {
     178                 :           0 :         return INCHIKEY_INVALID_INCHI_PREFIX;
     179                 :             :     }
     180                 :             : 
     181                 :             :     /* .. the rest of source string contains at least one a..Z0.9 or slash */
     182         [ #  # ]:           0 :     if (!isalnum( szINCHISource[pos_slash1 + 1] ))
     183                 :             :     {
     184                 :           0 :         int valid_empty_inchi = szINCHISource[pos_slash1 + 1] == '/';
     185                 :           0 :         int allowed_and_valid_err_inchi = 0;
     186         [ #  # ]:           0 :         if (!valid_empty_inchi)
     187                 :             :         {
     188                 :           0 :             allowed_and_valid_err_inchi = szINCHISource[pos_slash1 + 1] == '?';
     189         [ #  # ]:           0 :             if (!allowed_and_valid_err_inchi)
     190                 :             :             {
     191                 :           0 :                 return INCHIKEY_INVALID_INCHI;
     192                 :             :             }
     193                 :             :         }
     194                 :             :     }
     195                 :             : 
     196                 :             :     /* Ok. Will use a local copy of the source. */
     197                 :             : 
     198                 :           0 :     extract_inchi_substring( &str, szINCHISource, slen );
     199         [ #  # ]:           0 :     if (NULL == str)
     200                 :             :     {
     201                 :           0 :         ret = INCHIKEY_NOT_ENOUGH_MEMORY;
     202                 :           0 :         goto fin;
     203                 :             :     }
     204                 :           0 :     slen = strlen( str );
     205                 :             : 
     206                 :             : 
     207                 :             :     /* Make buffers. */
     208                 :           0 :     smajor = (char*) inchi_calloc( slen + 1, sizeof( char ) );
     209         [ #  # ]:           0 :     if (NULL == smajor)
     210                 :             :     {
     211                 :           0 :         ret = INCHIKEY_NOT_ENOUGH_MEMORY; goto fin;
     212                 :             :     }
     213                 :           0 :     sminor = (char*) inchi_calloc( 2 * slen + 2, sizeof( char ) ); /* we may double the length ... */
     214         [ #  # ]:           0 :     if (NULL == sminor)
     215                 :             :     {
     216                 :           0 :         ret = INCHIKEY_NOT_ENOUGH_MEMORY; goto fin;
     217                 :             :     }
     218                 :           0 :     stmp = (char*) inchi_calloc( slen + 1, sizeof( char ) );
     219         [ #  # ]:           0 :     if (NULL == stmp)
     220                 :             :     {
     221                 :           0 :         ret = INCHIKEY_NOT_ENOUGH_MEMORY; goto fin;
     222                 :             :     }
     223                 :           0 :     sproto = (char*) inchi_calloc( slen + 1, sizeof( char ) );
     224         [ #  # ]:           0 :     if (NULL == sproto)
     225                 :             :     {
     226                 :           0 :         ret = INCHIKEY_NOT_ENOUGH_MEMORY; goto fin;
     227                 :             :     }
     228                 :             : 
     229                 :           0 :     szINCHIKey[0] = '\0';
     230                 :             : 
     231                 :             :     /* Extract the major block */
     232                 :           0 :     smajor[0] = '\0';
     233         [ #  # ]:           0 :     for (j = pos_slash1 + 1; j < slen - 1; j++)
     234                 :             :     {
     235         [ #  # ]:           0 :         if (str[j] == '/')
     236                 :             :         {
     237                 :           0 :             cn = str[j + 1];
     238   [ #  #  #  # ]:           0 :             switch (cn)
     239                 :             :             {
     240                 :             :                 /* anything allowed from a major part */
     241                 :           0 :                 case 'c':
     242                 :             :                 case 'h':
     243                 :           0 :                 case 'q':   continue;
     244                 :             : 
     245                 :             :                 /* "/p"; protons now go to to special string, not to minor hash */
     246                 :           0 :                 case 'p':
     247                 :           0 :                     jproto = j;
     248                 :           0 :                     continue;
     249                 :             : 
     250                 :             :         /* "/f",  "/r" : may not occur in stdInChI */
     251                 :           0 :                 case 'f':
     252                 :             :                 case 'r':
     253         [ #  # ]:           0 :                     if (is_stdinchi==1)
     254                 :             :                     {
     255                 :           0 :                         ret = INCHIKEY_INVALID_STD_INCHI;
     256                 :           0 :                         goto fin;
     257                 :             :                     }
     258                 :           0 :                     break;
     259                 :             : 
     260                 :             :         /* anything allowed from a minor part */
     261                 :           0 :                 default:
     262                 :           0 :                     break;
     263                 :             :             }
     264                 :           0 :             break;
     265                 :             :         }
     266                 :             :     }
     267                 :           0 :     j++;
     268         [ #  # ]:           0 :     if (j == slen)
     269                 :             :     {
     270                 :           0 :         j++;
     271                 :             :     }
     272                 :             :     else
     273                 :             :     {
     274                 :           0 :         j--;
     275                 :             :     }
     276                 :             : 
     277         [ #  # ]:           0 :     if (jproto)
     278                 :             :     {
     279                 :           0 :         ncp = jproto - pos_slash1 - 1;
     280                 :             :     }
     281                 :             :     else
     282                 :             :     {
     283                 :           0 :         ncp = j - pos_slash1 - 1;
     284                 :             :     }
     285                 :             : 
     286                 :             : 
     287                 :             :     /* Hash only the InChI layers following the parsed version prefix. */
     288                 :           0 :     memcpy(smajor, str + pos_slash1 + 1, ncp * sizeof(str[0]));
     289                 :           0 :     smajor[ncp] = '\0';
     290                 :             : 
     291                 :             : 
     292                 :             :     /* Treat protonization */
     293         [ #  # ]:           0 :     if (jproto)
     294                 :             :     {
     295                 :             :         /* 2009-01-07 fix bug/typo: assigned incorrect length to the protonation segment of
     296                 :             :            source string ( was sproto[ncp]='\0'; should be sproto[lenproto]='\0'; )  */
     297                 :           0 :         int lenproto = j - (int) jproto;
     298         [ #  # ]:           0 :         if (lenproto < 3)
     299                 :             :         {
     300                 :             :             /* empty "/p", should not occur */
     301                 :           0 :             ret = INCHIKEY_INVALID_INCHI;
     302                 :           0 :             goto fin;
     303                 :             :         }
     304                 :             : 
     305                 :           0 :         memcpy(sproto, str + pos_slash1 + ncp + 1, lenproto * sizeof(str[0]));
     306                 :           0 :         sproto[lenproto] = '\0';
     307                 :             : 
     308                 :           0 :         nprotons = strtol( sproto + 2, NULL, 10 );
     309                 :             : 
     310         [ #  # ]:           0 :         if (nprotons > 0)
     311                 :             :         {
     312         [ #  # ]:           0 :             if (nprotons > 12)
     313                 :             :             {
     314                 :           0 :                 flagproto = 'A';
     315                 :             :             }
     316                 :             :             else
     317                 :             :             {
     318                 :           0 :                 flagproto = pplus[nprotons - 1];
     319                 :             :             }
     320                 :             :         }
     321         [ #  # ]:           0 :         else if (nprotons < 0)
     322                 :             :         {
     323         [ #  # ]:           0 :             if (nprotons < -12)
     324                 :             :             {
     325                 :           0 :                 flagproto = 'A';
     326                 :             :             }
     327                 :             :             else
     328                 :             :             {
     329                 :           0 :                 flagproto = pminus[-nprotons - 1];
     330                 :             :             }
     331                 :             :         }
     332                 :             :         else
     333                 :             :         {
     334                 :             :             /* should never occur */
     335                 :           0 :             ret = INCHIKEY_INVALID_STD_INCHI;
     336                 :           0 :             goto fin;
     337                 :             :         }
     338                 :             :     }
     339                 :             : 
     340                 :             :     /* Extract the minor block. */
     341                 :             : 
     342         [ #  # ]:           0 :     if (j != slen + 1)    /* check that something exists at right.*/
     343                 :             :     {
     344                 :           0 :         ncp = slen - j;
     345                 :           0 :         memcpy(sminor, str + j, (ncp) * sizeof(str[0]));
     346                 :           0 :         sminor[ncp] = '\0';
     347                 :             :     }
     348                 :             :     else
     349                 :             :     {
     350                 :           0 :         sminor[0] = '\0';
     351                 :             :     }
     352                 :             : 
     353                 :             : 
     354                 :             : #if INCHIKEY_DEBUG
     355                 :             :     ITRACE_( "Source:  {%-s}\n", str );
     356                 :             :     ITRACE_( "SMajor:  {%-s}\n", smajor );
     357                 :             :     ITRACE_( "SMinor:  {%-s}\n", sminor );
     358                 :             :     ITRACE_( "SProto:  {%-s}\n", sproto );
     359                 :             : #endif
     360                 :             : 
     361                 :             :     /* Compute and compose the InChIKey string. */
     362                 :             : 
     363                 :             :     /* Major hash sub-string. */
     364         [ #  # ]:           0 :     for (i = 0; i < 32; i++)
     365                 :             :     {
     366                 :           0 :         digest_major[i] = 0;
     367                 :             :     }
     368                 :             : 
     369                 :           0 :     sha2_csum( (unsigned char *) smajor, (int) strlen( smajor ), digest_major );
     370                 :             : 
     371                 :           0 :     sprintf(tmp, "%-.3s%-.3s%-.3s%-.3s%-.2s",
     372                 :             :         base26_triplet_1(digest_major), base26_triplet_2(digest_major),
     373                 :             :         base26_triplet_3(digest_major), base26_triplet_4(digest_major),
     374                 :             :         base26_dublet_for_bits_56_to_64(digest_major));
     375                 :           0 :     strcat(szINCHIKey, tmp);
     376                 :             : #if (INCHIKEY_DEBUG>1)
     377                 :             :     fprint_digest( stderr, "Major hash, full SHA-256", digest_major );
     378                 :             : #endif
     379                 :             : 
     380                 :             : 
     381                 :             :     /* Minor hash sub-string. */
     382         [ #  # ]:           0 :     for (i = 0; i < 32; i++)
     383                 :             :     {
     384                 :           0 :         digest_minor[i] = 0;
     385                 :             :     }
     386                 :           0 :     slen = strlen( sminor );
     387   [ #  #  #  # ]:           0 :     if (( slen > 0 ) && ( slen < 255 ))
     388                 :             :     {
     389                 :           0 :         strcpy(stmp, sminor);
     390                 :           0 :         strcpy(sminor + slen, stmp);
     391                 :             :     }
     392                 :             : 
     393                 :           0 :     sha2_csum( (unsigned char *) sminor, (int) strlen( sminor ), digest_minor );
     394                 :             : 
     395                 :             : #if (INCHIKEY_DEBUG>1)
     396                 :             :     fprint_digest( stderr, "Minor hash, full SHA-256", digest_minor );
     397                 :             : #endif
     398                 :             : 
     399                 :           0 :     strcat(szINCHIKey, "-");
     400                 :           0 :     sprintf(tmp, "%-.3s%-.3s%-.2s",
     401                 :             :         base26_triplet_1(digest_minor),
     402                 :             :         base26_triplet_2(digest_minor),
     403                 :             :         base26_dublet_for_bits_28_to_36(digest_minor));
     404                 :           0 :     strcat(szINCHIKey, tmp);
     405                 :             :     /* Append calculation-class flag */
     406                 :           0 :     slen = strlen(szINCHIKey);
     407         [ #  # ]:           0 :     if (is_stdinchi == 1)
     408                 :             :     {
     409                 :           0 :         szINCHIKey[slen] = flagstd;
     410                 :             :     }
     411         [ #  # ]:           0 :     else if (is_stdinchi == -1)
     412                 :             :     {
     413                 :           0 :         szINCHIKey[slen] = flagexptl;
     414                 :             :     }
     415                 :             :     else
     416                 :             :     {
     417                 :           0 :         szINCHIKey[slen] = flagnonstd;
     418                 :             :     }
     419                 :             : 
     420                 :             :     /* Append InChI subversion flag */
     421         [ #  # ]:           0 :     szINCHIKey[slen + 1] = is_molecular_inorganic ? 'B' : flagsubver;
     422                 :             : 
     423                 :             :     /* Append dash  */
     424                 :           0 :     szINCHIKey[slen + 2] = '-';
     425                 :             : 
     426                 :             :     /* Append protonization flag */
     427                 :           0 :     szINCHIKey[slen + 3] = flagproto;
     428                 :           0 :     szINCHIKey[slen + 4] = '\0';
     429                 :             : 
     430                 :             : 
     431                 :             : #if INCHIKEY_DEBUG
     432                 :             :     ITRACE_( "szINCHIKey:  {%-s}\n", szINCHIKey );
     433                 :             : #endif
     434                 :             : 
     435                 :             :     /* Hash extensions */
     436   [ #  #  #  # ]:           0 :     if (xtra1 && szXtra1)
     437                 :             :     {
     438                 :           0 :         get_xtra_hash_major_hex( digest_major, szXtra1 );
     439                 :             : #if INCHIKEY_DEBUG
     440                 :             :         fprintf( stderr, "XHash1=%-s\n", szXtra1 );
     441                 :             :         fprintf( stderr, "j=%-d\n", j );
     442                 :             : #endif
     443                 :             :     }
     444   [ #  #  #  # ]:           0 :     if (xtra2 && szXtra2)
     445                 :             :     {
     446                 :           0 :         get_xtra_hash_minor_hex( digest_minor, szXtra2 );
     447                 :             : #if INCHIKEY_DEBUG
     448                 :             :         fprintf( stderr, "XHash2=%-s\n", szXtra2 );
     449                 :             :         fprintf( stderr, "j=%-d\n", j );
     450                 :             : #endif
     451                 :             :     }
     452                 :             : 
     453                 :             : 
     454                 :           0 : fin:
     455                 :             :     /* djb-rwth: fixing GH issue #87 (thanks to Ricardo Rodriguez) and oss-fuzz issue #66746 */
     456         [ #  # ]:           0 :     if (NULL != str)
     457                 :             :     {
     458         [ #  # ]:           0 :         inchi_free( str );
     459                 :             :     }
     460         [ #  # ]:           0 :     if (NULL != smajor)
     461                 :             :     {
     462         [ #  # ]:           0 :         inchi_free( smajor );
     463                 :             :     }
     464         [ #  # ]:           0 :     if (NULL != sminor)
     465                 :             :     {
     466         [ #  # ]:           0 :         inchi_free( sminor );
     467                 :           0 :         sminor = NULL;
     468                 :             :     }
     469         [ #  # ]:           0 :     if (NULL != stmp)
     470                 :             :     {
     471         [ #  # ]:           0 :         inchi_free( stmp );
     472                 :             :     }
     473         [ #  # ]:           0 :     if (NULL != sproto)
     474                 :             :     {
     475         [ #  # ]:           0 :         inchi_free( sproto );
     476                 :             :     }
     477   [ #  #  #  # ]:           0 :     if (( ret == INCHIKEY_OK ) && ( ret1 != INCHIKEY_OK ))
     478                 :             :     {
     479                 :           0 :         ret = ret1;
     480                 :             :     }
     481                 :             : 
     482                 :           0 :     return ret;
     483                 :             : }
     484                 :             : 
     485                 :             : 
     486                 :             : /****************************************************************************
     487                 :             : Check if the string represents valid InChIKey.
     488                 :             : ****************************************************************************/
     489                 :           0 : EXPIMP_TEMPLATE INCHI_API int INCHI_DECL CheckINCHIKey( const char *szINCHIKey )
     490                 :             : {
     491                 :             :     size_t slen, j;
     492                 :             : 
     493                 :             : 
     494                 :           0 :     slen = strlen( szINCHIKey );
     495                 :             : 
     496                 :             : 
     497                 :             :     /* Proper length is 27  */
     498         [ #  # ]:           0 :     if (slen != 27)
     499                 :             :     {
     500                 :           0 :         return INCHIKEY_INVALID_LENGTH;
     501                 :             :     }
     502                 :             : 
     503                 :             :     /* Should have dash in 14-th position */
     504         [ #  # ]:           0 :     if (szINCHIKey[14] != '-')
     505                 :             :     {
     506                 :           0 :         return INCHIKEY_INVALID_LAYOUT;
     507                 :             :     }
     508                 :             :     /* Should have dash in 25-th position */
     509         [ #  # ]:           0 :     if (szINCHIKey[25] != '-')
     510                 :             :     {
     511                 :           0 :         return INCHIKEY_INVALID_LAYOUT;
     512                 :             :     }
     513                 :             : 
     514                 :             :     /* All other should be uppercase */
     515         [ #  # ]:           0 :     for (j = 0; j < 14; j++)
     516                 :             :     {
     517   [ #  #  #  # ]:           0 :         if (!isbase26( szINCHIKey[j] ))
     518                 :             :         {
     519                 :           0 :             return INCHIKEY_INVALID_LAYOUT; /* first block */
     520                 :             :         }
     521                 :             :     }
     522         [ #  # ]:           0 :     for (j = 15; j < 25; j++)
     523                 :             :     {
     524   [ #  #  #  # ]:           0 :         if (!isbase26( szINCHIKey[j] ))
     525                 :             :         {
     526                 :           0 :             return INCHIKEY_INVALID_LAYOUT; /* second block */
     527                 :             :         }
     528                 :             :     }
     529                 :             : 
     530   [ #  #  #  # ]:           0 :     if (!isbase26( szINCHIKey[26] ))
     531                 :             :     {
     532                 :           0 :         return INCHIKEY_INVALID_LAYOUT; /* (de)protonation flag */
     533                 :             :     }
     534                 :             : 
     535                 :             : 
     536                 :             :     /* No 'E' may appear in 0,3,6,and 9 positions of the 1st block ... */
     537         [ #  # ]:           0 :     for (j = 0; j < 10; j += 3)
     538                 :             :     {
     539         [ #  # ]:           0 :         if (szINCHIKey[j] == 'E')
     540                 :             :         {
     541                 :           0 :             return INCHIKEY_INVALID_LAYOUT;
     542                 :             :         }
     543                 :             :     }
     544                 :             :     /* ... and 0 and 3 pos. of the second block. */
     545         [ #  # ]:           0 :     for (j = 15; j < 19; j += 3)
     546                 :             :     {
     547         [ #  # ]:           0 :         if (szINCHIKey[j] == 'E')
     548                 :             :         {
     549                 :           0 :             return INCHIKEY_INVALID_LAYOUT;
     550                 :             :         }
     551                 :             :     }
     552                 :             : 
     553                 :             :     /* Check InChI subversion flag */
     554   [ #  #  #  # ]:           0 :     if (szINCHIKey[24] != 'A' && szINCHIKey[24] != 'B')
     555                 :             :     {
     556                 :           0 :         return INCHIKEY_INVALID_VERSION;
     557                 :             :     }
     558                 :             : 
     559                 :             :     /* Check calculation-class flag */
     560         [ #  # ]:           0 :     if (szINCHIKey[23] == 'S')
     561                 :             :     {
     562                 :           0 :         return INCHIKEY_VALID_STANDARD;
     563                 :             :     }
     564   [ #  #  #  # ]:           0 :     else if (szINCHIKey[23] == 'N' || szINCHIKey[23] == 'B')
     565                 :             :     {
     566                 :           0 :         return INCHIKEY_VALID_NON_STANDARD;
     567                 :             :     }
     568                 :             :     else
     569                 :             :     {
     570                 :           0 :         return INCHIKEY_INVALID_LAYOUT;
     571                 :             :     }
     572                 :             : 
     573                 :             : }
     574                 :             : 
     575                 :             : 
     576                 :             : /****************************************************************************/
     577                 :           0 : void fprint_digest( FILE* fw, const char *header, unsigned char *a )
     578                 :             : {
     579                 :           0 :     size_t i, bytelen = 32;
     580                 :           0 :     fprintf( fw, "%s\n", header );
     581         [ #  # ]:           0 :     for (i = 0; i < bytelen; i++)
     582                 :             :     {
     583                 :           0 :         fprintf( fw, "%02x ", a[i] );
     584                 :             :     }
     585                 :           0 :     fprintf( fw, "\n" );
     586                 :           0 : }
     587                 :             : 
     588                 :             : 
     589                 :             : /****************************************************************************/
     590                 :             : 
     591                 :             : #if ( defined( _WIN32 ) && defined( _MSC_VER ) && _MSC_VER >= 800 && defined(_USRDLL) && defined(BUILD_LINK_AS_DLL) )
     592                 :             :     /* Win32 & MS VC ++, compile and link as a DLL */
     593                 :             : /*********************************************************/
     594                 :             : /*   C calling conventions export from Win32 dll         */
     595                 :             : /*********************************************************/
     596                 :             : /* prototypes */
     597                 :             : #ifndef COMPILE_ALL_CPP
     598                 :             : #ifdef __cplusplus
     599                 :             : extern "C" {
     600                 :             : #endif
     601                 :             : #endif
     602                 :             : 
     603                 :             :     int cdecl_CheckINCHIKey( const char *szINCHIKey );
     604                 :             :     int cdecl_GetINCHIKeyFromINCHI( const char* szINCHISource,
     605                 :             :                                    const int xtra1, const int xtra2,
     606                 :             :                                    char* szINCHIKey, char* szXtra1, char* szXtra2 );
     607                 :             :     int cdecl_GetStdINCHIKeyFromStdINCHI( const char* szINCHISource, char* szINCHIKey );
     608                 :             : 
     609                 :             : #ifndef COMPILE_ALL_CPP
     610                 :             : #ifdef __cplusplus
     611                 :             : }
     612                 :             : #endif
     613                 :             : #endif
     614                 :             : 
     615                 :             : /* implementation */
     616                 :             : /* libinchi.def provides export without cdecl_ prefixes */
     617                 :             : 
     618                 :             : /****************************************************************************/
     619                 :             : int cdecl_GetStdINCHIKeyFromStdINCHI( const char* szINCHISource, char* szINCHIKey )
     620                 :             : {
     621                 :             :     return GetStdINCHIKeyFromStdINCHI( szINCHISource, szINCHIKey );
     622                 :             : }
     623                 :             : 
     624                 :             : 
     625                 :             : /****************************************************************************/
     626                 :             : int cdecl_CheckINCHIKey( const char *szINCHIKey )
     627                 :             : {
     628                 :             :     return CheckINCHIKey( szINCHIKey );
     629                 :             : }
     630                 :             : 
     631                 :             : 
     632                 :             : /****************************************************************************/
     633                 :             : int cdecl_GetINCHIKeyFromINCHI( const char* szINCHISource,
     634                 :             :                                const int xtra1, const int xtra2,
     635                 :             :                                char* szINCHIKey, char* szXtra1, char* szXtra2 )
     636                 :             : {
     637                 :             :     return GetINCHIKeyFromINCHI( szINCHISource, xtra1, xtra2, szINCHIKey, szXtra1, szXtra2 );
     638                 :             : }
     639                 :             : #endif
     640                 :             : 
     641                 :             : 
     642                 :             : 
     643                 :             : #if ( defined(__GNUC__) && __GNUC__ >= 3 && defined(__MINGW32__) && defined(_WIN32) )
     644                 :             : #include <windows.h>
     645                 :             : 
     646                 :             : 
     647                 :             : /****************************************************************************
     648                 :             :    Pacal calling conventions export from Win32 dll
     649                 :             : ****************************************************************************/
     650                 :             : #ifndef COMPILE_ALL_CPP
     651                 :             : #ifdef __cplusplus
     652                 :             : extern "C" {
     653                 :             : #endif
     654                 :             : #endif
     655                 :             : /* prototypes */
     656                 :             :     int PASCAL pasc_CheckINCHIKey( const char *szINCHIKey );
     657                 :             : #ifndef COMPILE_ALL_CPP
     658                 :             : #ifdef __cplusplus
     659                 :             : }
     660                 :             : #endif
     661                 :             : #endif
     662                 :             : 
     663                 :             : /* implementation */
     664                 :             : /* libinchi.def provides export without PASCAL pasc_ prefixes */
     665                 :             : 
     666                 :             : /****************************************************************************/
     667                 :             : int PASCAL pasc_CheckINCHIKey( const char *szINCHIKey )
     668                 :             : {
     669                 :             :     return CheckINCHIKey( szINCHIKey );
     670                 :             : }
     671                 :             : 
     672                 :             : #endif
        

Generated by: LCOV version 2.0-1