LCOV - code coverage report
Current view: top level - src - ichi_io.c (source / functions) Coverage Total Hit
Test: InChI Unit Test Coverage Lines: 42.5 % 614 261
Test Date: 2026-12-03 11:58:49 Functions: 51.4 % 37 19
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 27.9 % 470 131

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * SPDX-License-Identifier: MIT
       3                 :             :  * Copyright (c) 2024 IUPAC and InChI Trust
       4                 :             :  */
       5                 :             : 
       6                 :             : #include <string.h>
       7                 :             : #include <stdlib.h>
       8                 :             : #include <ctype.h>
       9                 :             : #include <stdarg.h>
      10                 :             : #include <time.h>
      11                 :             : 
      12                 :             : #include "mode.h"
      13                 :             : #include "ichi_io.h"
      14                 :             : #include "ichicomp.h"
      15                 :             : #include "ichidrp.h"
      16                 :             : #include "strutil.h"
      17                 :             : #include "util.h"
      18                 :             : 
      19                 :             : #include "bcf_s.h"
      20                 :             : 
      21                 :             : #ifndef INCHI_ADD_STR_LEN
      22                 :             : #define INCHI_ADD_STR_LEN   32768
      23                 :             : #endif
      24                 :             : 
      25                 :             : #ifdef TARGET_LIB_FOR_WINCHI
      26                 :             : extern void(*FWPRINT) (const char* format, va_list argptr);
      27                 :             : extern void(*FWPUSH) (const char* s);
      28                 :             : 
      29                 :             : #endif
      30                 :             : 
      31                 :             : 
      32                 :             : /* Internal functions */
      33                 :             : 
      34                 :             : int inchi_ios_str_getc(INCHI_IOSTREAM* ios);
      35                 :             : char* inchi_ios_str_gets(char* szLine, int len, INCHI_IOSTREAM* ios);
      36                 :             : char* inchi_ios_str_getsTab(char* szLine, int len, INCHI_IOSTREAM* ios);
      37                 :             : int GetMaxPrintfLength(const char* lpszFormat, va_list argList);
      38                 :             : char* inchi_fgetsTab(char* szLine, int len, FILE* f);
      39                 :             : int inchi_vfprintf(FILE* f, const char* lpszFormat, va_list argList);
      40                 :             : 
      41                 :             : 
      42                 :             : /*
      43                 :             :     INCHI_IOSTREAM OPERATIONS
      44                 :             : */
      45                 :             : 
      46                 :             : 
      47                 :             : /****************************************************************************
      48                 :             :  Init INCHI_IOSTREAM
      49                 :             : ****************************************************************************/
      50                 :        1210 : void inchi_ios_init(INCHI_IOSTREAM* ios, int io_type, FILE* f)
      51                 :             : {
      52                 :        1210 :     memset(ios, 0, sizeof(*ios)); /* djb-rwth: memset_s C11/Annex K variant? */
      53         [ +  + ]:        1210 :     switch (io_type)
      54                 :             :     {
      55                 :          14 :     case INCHI_IOS_TYPE_FILE:
      56                 :          14 :         ios->type = INCHI_IOS_TYPE_FILE;
      57                 :          14 :         break;
      58                 :        1196 :     case INCHI_IOS_TYPE_STRING:
      59                 :             :     default:
      60                 :        1196 :         ios->type = INCHI_IOS_TYPE_STRING;
      61                 :        1196 :         break;
      62                 :             :     }
      63                 :        1210 :     ios->f = f;
      64                 :        1210 :     return;
      65                 :             : }
      66                 :             : 
      67                 :             : 
      68                 :             : /****************************************************************************
      69                 :             :  Make a copy of INCHI_IOSTREAM
      70                 :             : ****************************************************************************/
      71                 :           0 : int inchi_ios_create_copy(INCHI_IOSTREAM* ios, INCHI_IOSTREAM* ios0)
      72                 :             : {
      73         [ #  # ]:           0 :     if (ios) /* djb-rwth: fixing a NULL pointer dereference */
      74                 :             :     {
      75                 :           0 :         memset(ios, 0, sizeof(*ios)); /* djb-rwth: memset_s C11/Annex K variant? */
      76                 :           0 :         ios->type = ios0->type;
      77                 :             : 
      78         [ #  # ]:           0 :         if (ios->type == INCHI_IOS_TYPE_STRING)
      79                 :             :         {
      80         [ #  # ]:           0 :             if (ios->s.pStr)
      81                 :             :             {
      82         [ #  # ]:           0 :                 inchi_free(ios->s.pStr);
      83                 :             :             }
      84                 :           0 :             ios->s.pStr = (char*)inchi_calloc(ios0->s.nAllocatedLength, sizeof(char));
      85         [ #  # ]:           0 :             if (ios->s.pStr)
      86                 :             :             {
      87                 :           0 :                 ios->s.nUsedLength = ios0->s.nUsedLength;
      88                 :           0 :                 ios->s.nPtr = ios0->s.nPtr;
      89                 :             :             }
      90                 :             :             else
      91                 :             :             {
      92                 :           0 :                 return -1; /* no memory */
      93                 :             :             }
      94                 :             :         }
      95                 :           0 :         ios->f = ios0->f;
      96                 :             :     }
      97                 :             : 
      98                 :           0 :     return 0;
      99                 :             : }
     100                 :             : 
     101                 :             : 
     102                 :             : /****************************************************************************
     103                 :             :     If INCHI_IOSTREAM type is INCHI_IOS_TYPE_STRING
     104                 :             :     and associated file exists, flush the string buffer
     105                 :             :     to that file, then free the buffer.
     106                 :             :     If INCHI_IOSTREAM type is INCHI_IOS_TYPE_FILE,
     107                 :             :     just flush the file.
     108                 :             : ****************************************************************************/
     109                 :           0 : void inchi_ios_flush(INCHI_IOSTREAM* ios)
     110                 :             : {
     111                 :             : 
     112         [ #  # ]:           0 :     if (ios->type == INCHI_IOS_TYPE_STRING)
     113                 :             :     {
     114         [ #  # ]:           0 :         if (ios->s.pStr)
     115                 :             :         {
     116         [ #  # ]:           0 :             if (ios->s.nUsedLength > 0)
     117                 :             :             {
     118         [ #  # ]:           0 :                 if (ios->f)
     119                 :             :                 {
     120                 :           0 :                     fprintf(ios->f, "%-s", ios->s.pStr);
     121                 :           0 :                     fflush(ios->f);
     122                 :             :                 }
     123         [ #  # ]:           0 :                 inchi_free(ios->s.pStr);
     124                 :           0 :                 ios->s.pStr = NULL;
     125                 :           0 :                 ios->s.nUsedLength = ios->s.nAllocatedLength = ios->s.nPtr = 0;
     126                 :             :             }
     127                 :             :         }
     128                 :             :     }
     129                 :             : 
     130         [ #  # ]:           0 :     else if (ios->type == INCHI_IOS_TYPE_FILE)
     131                 :             :     {
     132                 :             :         /* output to plain file: just flush it. */
     133                 :           0 :         fflush(ios->f);
     134                 :             :     }
     135                 :             : 
     136                 :           0 :     return;
     137                 :             : }
     138                 :             : 
     139                 :             : 
     140                 :             : /****************************************************************************
     141                 :             :     If INCHI_IOSTREAM type is INCHI_IOS_TYPE_STRING,
     142                 :             :     flush INCHI_IOSTREAM string buffer to associated file
     143                 :             :     (if non-NULL) echoing to another file (supplied as
     144                 :             :     parameter; typically, stderr); then free buffer.
     145                 :             :     If INCHI_IOSTREAM type is INCHI_IOS_TYPE_FILE,
     146                 :             :     just flush the both files.
     147                 :             : ****************************************************************************/
     148                 :           0 : void inchi_ios_flush2(INCHI_IOSTREAM* ios, FILE* f2)
     149                 :             : {
     150                 :             : 
     151         [ #  # ]:           0 :     if (ios->type == INCHI_IOS_TYPE_STRING)
     152                 :             :     {
     153         [ #  # ]:           0 :         if (ios->s.pStr)
     154                 :             :         {
     155         [ #  # ]:           0 :             if (ios->s.nUsedLength > 0)
     156                 :             :             {
     157         [ #  # ]:           0 :                 if (ios->f)
     158                 :             :                 {
     159                 :           0 :                     fprintf(ios->f, "%-s", ios->s.pStr);
     160                 :           0 :                     fflush(ios->f);
     161                 :             :                 }
     162         [ #  # ]:           0 :                 if (f2 != ios->f)
     163                 :             :                 {
     164                 :           0 :                     fprintf(f2, "%-s", ios->s.pStr);
     165                 :             :                 }
     166                 :             : 
     167         [ #  # ]:           0 :                 inchi_free(ios->s.pStr);
     168                 :           0 :                 ios->s.pStr = NULL;
     169                 :           0 :                 ios->s.nUsedLength = ios->s.nAllocatedLength = ios->s.nPtr = 0;
     170                 :             :             }
     171                 :             :         }
     172                 :             :     }
     173                 :             : 
     174         [ #  # ]:           0 :     else if (ios->type == INCHI_IOS_TYPE_FILE)
     175                 :             :     {
     176                 :             :         /* Output to plain file: just flush it. */
     177   [ #  #  #  #  :           0 :         if ((ios->f) && (ios->f != stderr) && (ios->f != stdout))
                   #  # ]
     178                 :             :         {
     179                 :           0 :             fflush(ios->f);
     180                 :             :         }
     181   [ #  #  #  #  :           0 :         if (f2 && f2 != stderr && f2 != stdout)
                   #  # ]
     182                 :             :         {
     183                 :           0 :             fflush(f2);
     184                 :             :         }
     185                 :             :     }
     186                 :             : 
     187                 :           0 :     return;
     188                 :             : }
     189                 :             : 
     190                 :             : 
     191                 :             : /****************************************************************************
     192                 :             :     Close INCHI_IOSTREAM: free string buffer and close associated file.
     193                 :             : ****************************************************************************/
     194                 :         590 : void inchi_ios_close(INCHI_IOSTREAM* ios)
     195                 :             : {
     196         [ -  + ]:         590 :     if (NULL == ios)
     197                 :             :     {
     198                 :           0 :         return;
     199                 :             :     }
     200         [ +  + ]:         590 :     if (ios->s.pStr)
     201                 :             :     {
     202         [ +  - ]:         139 :         inchi_free(ios->s.pStr);
     203                 :             :     }
     204                 :         590 :     ios->s.pStr = NULL;
     205                 :         590 :     ios->s.nUsedLength = ios->s.nAllocatedLength = ios->s.nPtr = 0;
     206                 :             : 
     207   [ -  +  -  -  :         590 :     if (NULL != ios->f && stdout != ios->f && stderr != ios->f && stdin != ios->f)
             -  -  -  - ]
     208                 :             :     {
     209                 :           0 :         fclose(ios->f);
     210                 :             :     }
     211                 :             : 
     212                 :         590 :     return;
     213                 :             : }
     214                 :             : 
     215                 :             : 
     216                 :             : /****************************************************************************
     217                 :             :     Reset INCHI_IOSTREAM: set string buffer ptr to NULL
     218                 :             :     (but do _not_ free memory)and close associated file.
     219                 :             : ****************************************************************************/
     220                 :          24 : void inchi_ios_reset(INCHI_IOSTREAM* ios)
     221                 :             : {
     222                 :          24 :     ios->s.pStr = NULL;
     223                 :          24 :     ios->s.nUsedLength = ios->s.nAllocatedLength = ios->s.nPtr = 0;
     224   [ -  +  -  -  :          24 :     if (NULL != ios->f && stdout != ios->f && stderr != ios->f && stdin != ios->f)
             -  -  -  - ]
     225                 :             :     {
     226                 :           0 :         fclose(ios->f);
     227                 :             :     }
     228                 :             : 
     229                 :          24 :     return;
     230                 :             : }
     231                 :             : 
     232                 :             : 
     233                 :             : /****************************************************************************
     234                 :             :     Reset INCHI_IOSTREAM: set string buffer ptr to NULL
     235                 :             :     (after freeing memory) but do not close associated file.
     236                 :             : ****************************************************************************/
     237                 :           0 : void inchi_ios_free_str(INCHI_IOSTREAM* ios)
     238                 :             : {
     239         [ #  # ]:           0 :     if (NULL == ios)
     240                 :             :     {
     241                 :           0 :         return;
     242                 :             :     }
     243         [ #  # ]:           0 :     if (ios->s.pStr) /* && ios->s.nAllocatedLength)*/
     244                 :             :     {
     245         [ #  # ]:           0 :         inchi_free(ios->s.pStr);
     246                 :             :     }
     247                 :           0 :     ios->s.pStr = NULL;
     248                 :           0 :     ios->s.nUsedLength = 0;
     249                 :           0 :     ios->s.nAllocatedLength = 0;
     250                 :           0 :     ios->s.nPtr = 0;
     251                 :             : 
     252                 :           0 :     return;
     253                 :             : }
     254                 :             : 
     255                 :             : 
     256                 :             : /****************************************************************************
     257                 :             :     [str] getc()
     258                 :             : ****************************************************************************/
     259                 :      525205 : int inchi_ios_str_getc(INCHI_IOSTREAM* ios)
     260                 :             : {
     261                 :             :     int c;
     262         [ +  - ]:      525205 :     if (ios->type == INCHI_IOS_TYPE_STRING)
     263                 :             :     {
     264                 :             :         /* GH #146: stop at '\0'; nUsedLength may count it, never step past */
     265   [ +  +  +  + ]:      525205 :         if (ios->s.nPtr < ios->s.nUsedLength && ios->s.pStr[ios->s.nPtr])
     266                 :             :         {
     267                 :      525196 :             return (int)ios->s.pStr[ios->s.nPtr++];
     268                 :             :         }
     269                 :           9 :         return EOF;
     270                 :             :     }
     271                 :             : 
     272         [ #  # ]:           0 :     else if (ios->type == INCHI_IOS_TYPE_FILE)
     273                 :             :     {
     274                 :           0 :         c = fgetc(ios->f);
     275         [ #  # ]:           0 :         if (ferror(ios->f))
     276                 :             :         {
     277                 :           0 :             c = EOF;
     278                 :             :         }
     279                 :           0 :         return c;
     280                 :             :     }
     281                 :             : 
     282                 :             :     /* error */
     283                 :           0 :     return EOF;
     284                 :             : }
     285                 :             : 
     286                 :             : 
     287                 :             : /****************************************************************************
     288                 :             :     [str] gets()
     289                 :             : ****************************************************************************/
     290                 :           0 : char* inchi_ios_str_gets(char* szLine, int len, INCHI_IOSTREAM* f)
     291                 :             : {
     292                 :           0 :     int  length = 0, c = 0;
     293         [ #  # ]:           0 :     if (--len < 0)
     294                 :             :     {
     295                 :           0 :         return NULL;
     296                 :             :     }
     297   [ #  #  #  # ]:           0 :     while (length < len && EOF != (c = inchi_ios_str_getc(f)))
     298                 :             :     {
     299                 :           0 :         szLine[length++] = (char)c;
     300         [ #  # ]:           0 :         if (c == '\n')
     301                 :             :         {
     302                 :           0 :             break;
     303                 :             :         }
     304                 :             :     }
     305   [ #  #  #  # ]:           0 :     if (!length && EOF == c)
     306                 :             :     {
     307                 :           0 :         return NULL;
     308                 :             :     }
     309                 :           0 :     szLine[length] = '\0';
     310                 :             : 
     311                 :           0 :     return szLine;
     312                 :             : }
     313                 :             : 
     314                 :             : /****************************************************************************
     315                 :             :     Read up to tab or LF but not more than len chars;
     316                 :             :     if empty line, read further until a non-empty line;
     317                 :             :     remove leading and trailing white spaces;
     318                 :             :     keep zero termination
     319                 :             : ****************************************************************************/
     320                 :           7 : char* inchi_ios_str_getsTab(char* szLine, int len, INCHI_IOSTREAM* f)
     321                 :             : {
     322                 :           7 :     int  length = 0, c = 0;
     323         [ -  + ]:           7 :     if (--len < 0)
     324                 :             :     {
     325                 :           0 :         return NULL;
     326                 :             :     }
     327   [ +  -  +  + ]:         265 :     while (length < len && EOF != (c = inchi_ios_str_getc(f)))
     328                 :             :     {
     329         [ -  + ]:         258 :         if (c == '\t')
     330                 :             :         {
     331                 :           0 :             c = '\n';
     332                 :             :         }
     333                 :         258 :         szLine[length++] = (char)c;
     334         [ -  + ]:         258 :         if (c == '\n')
     335                 :             :         {
     336                 :           0 :             break;
     337                 :             :         }
     338                 :             :     }
     339   [ +  +  +  - ]:           7 :     if (!length && EOF == c)
     340                 :             :     {
     341                 :           3 :         return NULL;
     342                 :             :     }
     343                 :           4 :     szLine[length] = '\0';
     344                 :             : 
     345                 :           4 :     return szLine;
     346                 :             : }
     347                 :             : 
     348                 :             : 
     349                 :             : /****************************************************************************
     350                 :             :     gets()
     351                 :             : ****************************************************************************/
     352                 :           0 : int inchi_ios_gets(char* szLine,
     353                 :             :     int len,
     354                 :             :     INCHI_IOSTREAM* f,
     355                 :             :     int* bTooLongLine)
     356                 :             : {
     357                 :             :     int  length;
     358                 :             :     char* p;
     359                 :             :     do
     360                 :             :     {
     361                 :           0 :         p = inchi_ios_str_gets(szLine, len - 1, f);
     362         [ #  # ]:           0 :         if (!p)
     363                 :             :         {
     364                 :           0 :             *bTooLongLine = 0;
     365                 :           0 :             return -1; /* end of file or cannot read */
     366                 :             :         }
     367                 :           0 :         szLine[len - 1] = '\0';
     368                 :             :         /*
     369                 :             :         *bTooLongLine = !strchr( szLine, '\n' );
     370                 :             :         */
     371                 :           0 :         p = strchr(szLine, '\n');
     372   [ #  #  #  # ]:           0 :         *bTooLongLine = (!p && ((int)strlen(szLine)) == len - 2);
     373                 :           0 :         lrtrim(szLine, &length);
     374         [ #  # ]:           0 :     } while (!length);
     375                 :             : 
     376                 :           0 :     return length;
     377                 :             : }
     378                 :             : 
     379                 :             : 
     380                 :             : /****************************************************************************
     381                 :             :     Read up to tab or LF but not more than len chars;
     382                 :             :     if empty line, read further until a non-empty line;
     383                 :             :     remove leading and trailing white spaces;
     384                 :             :     keep zero termination
     385                 :             : ****************************************************************************/
     386                 :           7 : int inchi_ios_getsTab(char* szLine,
     387                 :             :     int len,
     388                 :             :     INCHI_IOSTREAM* f,
     389                 :             :     int* bTooLongLine)
     390                 :             : {
     391                 :             :     int  length;
     392                 :             :     char* p;
     393                 :             :     do
     394                 :             :     {
     395                 :             : 
     396                 :           7 :         p = inchi_ios_str_getsTab(szLine, len - 1, f);
     397         [ +  + ]:           7 :         if (!p)
     398                 :             :         {
     399                 :           3 :             *bTooLongLine = 0;
     400                 :           3 :             return -1; /* end of file or cannot read */
     401                 :             :         }
     402                 :           4 :         szLine[len - 1] = '\0';
     403                 :             :         /*
     404                 :             :         *bTooLongLine = !strchr( szLine, '\n' );
     405                 :             :         */
     406                 :           4 :         p = strchr(szLine, '\n');
     407   [ +  -  -  + ]:           4 :         *bTooLongLine = (!p && ((int)strlen(szLine)) == len - 2);
     408                 :           4 :         lrtrim(szLine, &length);
     409                 :             : 
     410         [ -  + ]:           4 :     } while (!length);
     411                 :             : 
     412                 :           4 :     return length;
     413                 :             : }
     414                 :             : 
     415                 :             : 
     416                 :             : /****************************************************************************/
     417                 :           0 : int inchi_ios_getsTab1(char* szLine,
     418                 :             :     int len,
     419                 :             :     INCHI_IOSTREAM* f,
     420                 :             :     int* bTooLongLine)
     421                 :             : {
     422                 :             :     int  length;
     423                 :             :     char* p;
     424                 :             : 
     425                 :           0 :     p = inchi_ios_str_getsTab(szLine, len - 1, f);
     426         [ #  # ]:           0 :     if (!p)
     427                 :             :     {
     428                 :           0 :         *bTooLongLine = 0;
     429                 :           0 :         return -1; /* end of file or cannot read */
     430                 :             :     }
     431                 :           0 :     szLine[len - 1] = '\0';
     432                 :           0 :     p = strchr(szLine, '\n');
     433   [ #  #  #  # ]:           0 :     *bTooLongLine = (!p && ((int)strlen(szLine)) == len - 2);
     434                 :           0 :     lrtrim(szLine, &length);
     435                 :             : 
     436                 :           0 :     return length;
     437                 :             : }
     438                 :             : 
     439                 :             : 
     440                 :             : /****************************************************************************
     441                 :             :  General procedure for printing to INCHI_IOSTREAM
     442                 :             : ****************************************************************************/
     443                 :        4051 : int inchi_ios_print(INCHI_IOSTREAM* ios, const char* lpszFormat, ...)
     444                 :             : {
     445                 :        4051 :     int ret = 0, ret2 = 0;
     446                 :             :     va_list argList;
     447                 :             : 
     448         [ -  + ]:        4051 :     if (!ios)
     449                 :             :     {
     450                 :           0 :         return -1;
     451                 :             :     }
     452                 :             : 
     453         [ +  - ]:        4051 :     if (ios->type == INCHI_IOS_TYPE_STRING)
     454                 :             :     {
     455                 :             :         /* output to string buffer */
     456                 :             :         int max_len;
     457                 :        4051 :         my_va_start(argList, lpszFormat);
     458                 :        4051 :         max_len = GetMaxPrintfLength(lpszFormat, argList);
     459                 :        4051 :         va_end(argList);
     460         [ +  - ]:        4051 :         if (max_len >= 0)
     461                 :             :         {
     462                 :             :             /* djb-rwth: fixing oss-fuzz issue #30152 */
     463                 :        4051 :             int  nAddLength = inchi_max(INCHI_ADD_STR_LEN, max_len + 1);
     464                 :        4051 :             long long new_str_len = (long long)ios->s.nAllocatedLength + (long long)nAddLength;
     465         [ -  + ]:        4051 :             if (ios->s.nAllocatedLength - ios->s.nUsedLength <= max_len)
     466                 :             :             {
     467                 :             :                 /* enlarge output string */
     468                 :           0 :                 char* new_str = (char*)inchi_calloc(new_str_len, sizeof(char)); /* djb-rwth: cast operators added */
     469         [ #  # ]:           0 :                 if (new_str)
     470                 :             :                 {
     471         [ #  # ]:           0 :                     if (ios->s.pStr)
     472                 :             :                     {
     473         [ #  # ]:           0 :                         if (ios->s.nUsedLength > 0)
     474                 :             :                         {
     475                 :           0 :                             memcpy(new_str, ios->s.pStr, sizeof(new_str[0]) * ios->s.nUsedLength);
     476                 :             :                         }
     477         [ #  # ]:           0 :                         inchi_free(ios->s.pStr);
     478                 :             :                     }
     479                 :           0 :                     ios->s.pStr = new_str;
     480                 :           0 :                     ios->s.nAllocatedLength += nAddLength;
     481                 :             :                 }
     482                 :             :                 else
     483                 :             :                 {
     484                 :           0 :                     return -1; /* failed */
     485                 :             :                 }
     486                 :             :             }
     487                 :             :             /* output */
     488                 :        4051 :             my_va_start(argList, lpszFormat);
     489                 :        4051 :             ret = vsprintf(ios->s.pStr + ios->s.nUsedLength, lpszFormat, argList); /* djb-rwth: not using vsnprintf due to variable length argument */
     490                 :        4051 :             va_end(argList);
     491         [ +  - ]:        4051 :             if (ret >= 0)
     492                 :             :             {
     493                 :        4051 :                 ios->s.nUsedLength += ret;
     494                 :             :             }
     495                 :             : #ifdef TARGET_LIB_FOR_WINCHI
     496                 :             : #if 0
     497                 :             :             if (FWPRINT)
     498                 :             :             {
     499                 :             :                 my_va_start(argList, lpszFormat);
     500                 :             :                 FWPRINT(lpszFormat, argList);
     501                 :             :                 va_end(argList);
     502                 :             :             }
     503                 :             : #endif
     504                 :             : #endif
     505                 :        4051 :             return ret;
     506                 :             :         }
     507                 :           0 :         return -1;
     508                 :             :     }
     509                 :             : 
     510         [ #  # ]:           0 :     else if (ios->type == INCHI_IOS_TYPE_FILE)
     511                 :             :     {
     512                 :             :         /* output to file */
     513         [ #  # ]:           0 :         if (ios->f)
     514                 :             :         {
     515                 :           0 :             my_va_start(argList, lpszFormat);
     516                 :           0 :             ret = vfprintf(ios->f, lpszFormat, argList);
     517                 :           0 :             va_end(argList);
     518                 :             :         }
     519                 :             :         else
     520                 :             :         {
     521                 :           0 :             my_va_start(argList, lpszFormat);
     522                 :           0 :             ret2 = vfprintf(stdout, lpszFormat, argList);
     523                 :           0 :             va_end(argList);
     524                 :             :         }
     525                 :             : #ifdef TARGET_LIB_FOR_WINCHI
     526                 :             :         if (FWPRINT)
     527                 :             :         {
     528                 :             :             my_va_start(argList, lpszFormat);
     529                 :             :             FWPRINT(lpszFormat, argList);
     530                 :             :             va_end(argList);
     531                 :             :         }
     532                 :             : #endif
     533         [ #  # ]:           0 :         return ret ? ret : ret2;
     534                 :             :     }
     535                 :             : 
     536                 :             :     /* no output */
     537                 :           0 :     return 0;
     538                 :             : }
     539                 :             : 
     540                 :             : 
     541                 :             : /****************************************************************************/
     542                 :           0 : int push_to_winchi_text_window(INCHI_IOSTREAM* ios)
     543                 :             : /*, const char* lpszFormat, ... ) */
     544                 :             : {
     545                 :             : #ifndef TARGET_LIB_FOR_WINCHI
     546                 :           0 :     return -1;
     547                 :             : #else
     548                 :             :     if (ios->type != INCHI_IOS_TYPE_STRING)
     549                 :             :     {
     550                 :             :         return -1;
     551                 :             :     }
     552                 :             :     if (!ios || !ios->s.pStr)
     553                 :             :     {
     554                 :             :         return -1;
     555                 :             :     }
     556                 :             :     if (!FWPUSH)
     557                 :             :     {
     558                 :             :         return -1;
     559                 :             :     }
     560                 :             : 
     561                 :             :     FWPUSH(ios->s.pStr);
     562                 :             : 
     563                 :             :     return 0;
     564                 :             : #endif
     565                 :             : }
     566                 :             : 
     567                 :             : /****************************************************************************
     568                 :             :     This function's output should not be displayed in the output pane
     569                 :             : ****************************************************************************/
     570                 :        1306 : int inchi_ios_print_nodisplay( INCHI_IOSTREAM * ios,
     571                 :             :                                const char* lpszFormat,
     572                 :             :                                ... )
     573                 :             : {
     574                 :             :     va_list argList;
     575                 :             : 
     576         [ -  + ]:        1306 :     if (!ios)
     577                 :             :     {
     578                 :           0 :         return -1;
     579                 :             :     }
     580                 :             : 
     581         [ +  - ]:        1306 :     if (ios->type == INCHI_IOS_TYPE_STRING)
     582                 :             :     {
     583                 :             :         /* output to string buffer */
     584                 :        1306 :         int ret = 0, max_len;
     585                 :        1306 :         my_va_start( argList, lpszFormat );
     586                 :        1306 :         max_len = GetMaxPrintfLength( lpszFormat, argList );
     587                 :        1306 :         va_end( argList );
     588         [ +  - ]:        1306 :         if (max_len >= 0)
     589                 :             :         {
     590                 :             :             /* djb-rwth: fixing oss-fuzz issue #30163 */
     591                 :        1306 :             int  nAddLength = inchi_max(INCHI_ADD_STR_LEN, max_len);
     592                 :        1306 :             long long new_str_len = (long long)ios->s.nAllocatedLength + (long long)nAddLength;
     593         [ +  + ]:        1306 :             if (ios->s.nAllocatedLength - ios->s.nUsedLength <= max_len)
     594                 :             :             {
     595                 :             :                 /* enlarge output string */
     596                 :         201 :                 char* new_str = (char*)inchi_calloc(new_str_len, sizeof(new_str[0])); /* djb-rwth: cast operators added */
     597         [ +  - ]:         201 :                 if (new_str)
     598                 :             :                 {
     599         [ -  + ]:         201 :                     if (ios->s.pStr)
     600                 :             :                     {
     601         [ #  # ]:           0 :                         if (ios->s.nUsedLength > 0)
     602                 :             :                         {
     603                 :           0 :                             memcpy( new_str, ios->s.pStr, sizeof( new_str[0] )*ios->s.nUsedLength );
     604                 :             :                         }
     605         [ #  # ]:           0 :                         inchi_free( ios->s.pStr );
     606                 :             :                     }
     607                 :         201 :                     ios->s.pStr = new_str;
     608                 :         201 :                     ios->s.nAllocatedLength += nAddLength;
     609                 :             :                 }
     610                 :             :                 else
     611                 :             :                 {
     612                 :           0 :                     return -1; /* failed */
     613                 :             :                 }
     614                 :             :             }
     615                 :             :             /* output */
     616                 :             :             /* djb-rwth: fixing oss-fuzz issue #67676 */
     617                 :        1306 :             my_va_start(argList, lpszFormat);
     618                 :        1306 :             size_t remaining_length = (size_t)(ios->s.nAllocatedLength - ios->s.nUsedLength);
     619                 :        1306 :             ret = vsnprintf(ios->s.pStr + ios->s.nUsedLength, remaining_length, lpszFormat, argList);
     620                 :        1306 :             va_end(argList);
     621                 :             : 
     622                 :             :             /* djb-rwth: if vsnprintf fails, try again with + 1 */
     623   [ +  -  -  + ]:        1306 :             if (ret < 0 || (size_t)ret >= remaining_length)
     624                 :             :             {
     625                 :             :                 long long str_length_extension;
     626         [ #  # ]:           0 :                 if (ret >= 0)
     627                 :             :                 {
     628                 :           0 :                     str_length_extension = (long long)ret + 1;
     629                 :             :                 }
     630                 :             :                 else
     631                 :             :                 {
     632                 :           0 :                     str_length_extension = (long long)max_len + 1;
     633                 :             :                 }
     634                 :           0 :                 long long new_str_length = (long long)ios->s.nAllocatedLength + str_length_extension;
     635                 :           0 :                 char* new_str = (char*)inchi_calloc(new_str_length, sizeof(new_str[0]));
     636         [ #  # ]:           0 :                 if (!new_str)
     637                 :             :                 {
     638                 :           0 :                     return -1;
     639                 :             :                 }
     640         [ #  # ]:           0 :                 if (ios->s.pStr)
     641                 :             :                 {
     642         [ #  # ]:           0 :                     if (ios->s.nUsedLength > 0)
     643                 :             :                     {
     644                 :           0 :                         memcpy(new_str, ios->s.pStr, sizeof(new_str[0]) * ios->s.nUsedLength);
     645                 :             :                     }
     646         [ #  # ]:           0 :                     inchi_free(ios->s.pStr);
     647                 :             :                 }
     648                 :           0 :                 ios->s.pStr = new_str;
     649                 :           0 :                 ios->s.nAllocatedLength += (int)str_length_extension;
     650                 :             : 
     651                 :             :                 // try printing again
     652                 :           0 :                 my_va_start(argList, lpszFormat);
     653                 :           0 :                 size_t remainingLength2 = (size_t)(ios->s.nAllocatedLength - ios->s.nUsedLength);
     654                 :           0 :                 ret = vsnprintf(ios->s.pStr + ios->s.nUsedLength, remainingLength2, lpszFormat, argList);
     655                 :           0 :                 va_end(argList);
     656                 :             :             }
     657                 :             : 
     658         [ +  - ]:        1306 :             if (ret >= 0)
     659                 :             :             {
     660                 :        1306 :                 ios->s.nUsedLength += ret;
     661                 :             :             }
     662                 :        1306 :             return ret;
     663                 :             :         }
     664                 :           0 :         return -1;
     665                 :             :     }
     666                 :             : 
     667         [ #  # ]:           0 :     else if (ios->type == INCHI_IOS_TYPE_FILE)
     668                 :             :     {
     669                 :           0 :         my_va_start( argList, lpszFormat );
     670                 :           0 :         inchi_print_nodisplay( ios->f, lpszFormat, argList );
     671                 :           0 :         va_end( argList );
     672                 :             :     }
     673                 :             : 
     674                 :             :     /* no output */
     675                 :           0 :     return 0;
     676                 :             : }
     677                 :             : 
     678                 :             : 
     679                 :             : /****************************************************************************
     680                 :             :     This function's flushes previously hidden output and resets string stream
     681                 :             :     returns n chars on success, otherwise -1
     682                 :             : ****************************************************************************/
     683                 :           0 : int inchi_ios_flush_not_displayed(INCHI_IOSTREAM* ios)
     684                 :             : {
     685                 :           0 :     char* obuf = NULL;
     686                 :             :     int ret;
     687                 :             : 
     688         [ #  # ]:           0 :     if (!ios)
     689                 :             :     {
     690                 :           0 :         return -1;
     691                 :             :     }
     692                 :             : 
     693                 :           0 :     obuf = (char*)inchi_calloc((long long)ios->s.nUsedLength + 1, sizeof(char)); /* djb-rwth: cast operator added */
     694                 :             : 
     695         [ #  # ]:           0 :     if (!obuf)
     696                 :             :     {
     697                 :           0 :         return -1;
     698                 :             :     }
     699                 :             : 
     700                 :           0 :     strcpy(obuf, ios->s.pStr);
     701                 :           0 :     ios->s.nUsedLength = 0;
     702                 :           0 :     ret = inchi_ios_print(ios, "%s", obuf);
     703         [ #  # ]:           0 :     inchi_free(obuf);
     704                 :             : 
     705                 :           0 :     return ret;
     706                 :             : }
     707                 :             : 
     708                 :             : 
     709                 :             : /****************************************************************************
     710                 :             :  Print to string buffer or to file+stderr
     711                 :             : ****************************************************************************/
     712                 :         498 : int inchi_ios_eprint(INCHI_IOSTREAM* ios, const char* lpszFormat, ...)
     713                 :             : {
     714                 :         498 :     int ret = 0, ret2 = 0;
     715                 :             :     va_list argList;
     716                 :             : 
     717         [ +  + ]:         498 :     if (!ios)
     718                 :             :     {
     719                 :           2 :         return -1;
     720                 :             :     }
     721                 :             : 
     722         [ +  - ]:         496 :     if (ios->type == INCHI_IOS_TYPE_STRING)
     723                 :             :         /* was #if ( defined(TARGET_API_LIB) || defined(INCHI_STANDALONE_EXE) ) */
     724                 :             :     {
     725                 :             :         /* output to string buffer */
     726                 :         496 :         int max_len, nAddLength = 0;
     727                 :         496 :         char* new_str = NULL;
     728                 :             : 
     729                 :         496 :         my_va_start(argList, lpszFormat);
     730                 :         496 :         max_len = GetMaxPrintfLength(lpszFormat, argList);
     731                 :         496 :         va_end(argList);
     732                 :             : 
     733         [ +  - ]:         496 :         if (max_len >= 0)
     734                 :             :         {
     735         [ +  + ]:         496 :             if (ios->s.nAllocatedLength - ios->s.nUsedLength <= max_len)
     736                 :             :             {
     737                 :             :                 /* enlarge output string */
     738                 :          76 :                 nAddLength = inchi_max(INCHI_ADD_STR_LEN, max_len + 1);
     739                 :          76 :                 new_str = (char*)inchi_calloc((long long)ios->s.nAllocatedLength + (long long)nAddLength, sizeof(new_str[0])); /* djb-rwth: cast operators added */
     740         [ +  - ]:          76 :                 if (new_str)
     741                 :             :                 {
     742         [ -  + ]:          76 :                     if (ios->s.pStr)
     743                 :             :                     {
     744         [ #  # ]:           0 :                         if (ios->s.nUsedLength > 0)
     745                 :             :                         {
     746                 :           0 :                             memcpy(new_str, ios->s.pStr, sizeof(new_str[0]) * ios->s.nUsedLength);
     747                 :             :                         }
     748         [ #  # ]:           0 :                         inchi_free(ios->s.pStr);
     749                 :             :                     }
     750                 :          76 :                     ios->s.pStr = new_str;
     751                 :          76 :                     ios->s.nAllocatedLength += nAddLength;
     752                 :             :                 }
     753                 :             :                 else
     754                 :             :                 {
     755                 :           0 :                     return -1; /* failed */
     756                 :             :                 }
     757                 :             :             }
     758                 :             : 
     759                 :             :             /* output */
     760                 :         496 :             my_va_start(argList, lpszFormat);
     761                 :         496 :             ret = vsprintf(ios->s.pStr + ios->s.nUsedLength, lpszFormat, argList);
     762                 :         496 :             va_end(argList);
     763         [ +  - ]:         496 :             if (ret >= 0)
     764                 :             :             {
     765                 :         496 :                 ios->s.nUsedLength += ret;
     766                 :             :             }
     767                 :         496 :             return ret;
     768                 :             :         }
     769                 :           0 :         return -1;
     770                 :             :     }
     771                 :             : 
     772         [ #  # ]:           0 :     else if (ios->type == INCHI_IOS_TYPE_FILE)
     773                 :             :     {
     774         [ #  # ]:           0 :         if (ios->f)
     775                 :             :         {
     776                 :             :             /* output to plain file */
     777                 :           0 :             my_va_start(argList, lpszFormat);
     778                 :           0 :             ret = inchi_vfprintf(ios->f, lpszFormat, argList);
     779                 :           0 :             va_end(argList);
     780                 :             :             /*  No output to stderr from within dll or GUI program */
     781                 :             : #if ( !defined(TARGET_API_LIB) && !defined(TARGET_LIB_FOR_WINCHI) )
     782                 :             :             if (ios->f != stderr)
     783                 :             :             {
     784                 :             :                 my_va_start(argList, lpszFormat);
     785                 :             :                 ret2 = vfprintf(stderr, lpszFormat, argList);
     786                 :             :                 va_end(argList);
     787                 :             :             }
     788                 :             : #endif
     789         [ #  # ]:           0 :             return ret ? ret : ret2;
     790                 :             :         }
     791                 :             :     }
     792                 :             : 
     793                 :             :     /* no output */
     794                 :           0 :     return 0;
     795                 :             : }
     796                 :             : 
     797                 :             : 
     798                 :             : /*    PLAIN FILE OPERATIONS */
     799                 :             : 
     800                 :             : 
     801                 :             : /****************************************************************************
     802                 :             :  Print to file, echoing to stderr
     803                 :             : ****************************************************************************/
     804                 :           0 : int inchi_fprintf(FILE* f, const char* lpszFormat, ...)
     805                 :             : {
     806                 :           0 :     int ret = 0, ret2 = 0;
     807                 :             :     va_list argList;
     808         [ #  # ]:           0 :     if (f)
     809                 :             :     {
     810                 :           0 :         my_va_start(argList, lpszFormat);
     811                 :           0 :         ret = inchi_vfprintf(f, lpszFormat, argList);
     812                 :           0 :         va_end(argList);
     813                 :             :         /*  No output to stderr from within dll or GUI program */
     814                 :             : #if ( !defined(TARGET_API_LIB) && !defined(TARGET_LIB_FOR_WINCHI) )
     815                 :             :         if (f != stderr)
     816                 :             :         {
     817                 :             :             my_va_start(argList, lpszFormat);
     818                 :             :             ret2 = vfprintf(stderr, lpszFormat, argList);
     819                 :             :             va_end(argList);
     820                 :             :         }
     821                 :             : #endif
     822         [ #  # ]:           0 :         return ret ? ret : ret2;
     823                 :             :     }
     824                 :             : 
     825                 :           0 :     return 0;
     826                 :             : }
     827                 :             : 
     828                 :             : 
     829                 :             : /****************************************************************************
     830                 :             :  Print to file
     831                 :             : ****************************************************************************/
     832                 :           0 : int inchi_vfprintf(FILE* f, const char* lpszFormat, va_list argList)
     833                 :             : {
     834                 :           0 :     int ret = 0;
     835   [ #  #  #  # ]:           0 :     if (lpszFormat && lpszFormat[0]) /* djb-rwth: condition added as lpszFormat == 0 may lead to undefined ret value */
     836                 :             :     {
     837   [ #  #  #  # ]:           0 :         if (f == stderr && '\r' == lpszFormat[strlen(lpszFormat) - 1])
     838                 :             :         {
     839                 :             : #define CONSOLE_LINE_LEN 80
     840                 :             : #ifndef COMPILE_ANSI_ONLY
     841                 :             :             char szLine[CONSOLE_LINE_LEN];
     842                 :             : 
     843                 :             :             ret = _vsnprintf(szLine, CONSOLE_LINE_LEN - 1, lpszFormat, argList);
     844                 :             : 
     845                 :             :             if (ret < 0)
     846                 :             :             {
     847                 :             :                 /*  output is longer than the console line */
     848                 :             :                 /* Fixed bug: (CONSOLE_LINE_LEN-4) --> (CONSOLE_LINE_LEN-4-1) 11-22-08 IPl */
     849                 :             :                 strcpy(szLine + CONSOLE_LINE_LEN - 5, "...\r");
     850                 :             :             }
     851                 :             : 
     852                 :             :             fputs(szLine, f);
     853                 :             : #else
     854                 :           0 :             ret = vfprintf(f, lpszFormat, argList);
     855                 :             : #endif
     856                 :             : #undef CONSOLE_LINE_LEN
     857                 :             :         }
     858                 :             :         else
     859                 :             :         {
     860                 :           0 :             ret = vfprintf(f, lpszFormat, argList);
     861                 :             :         }
     862                 :             :     }
     863                 :             : 
     864                 :           0 :     return ret;
     865                 :             : }
     866                 :             : 
     867                 :             : 
     868                 :             : /****************************************************************************
     869                 :             :     This function's output should not be displayed in the output pane
     870                 :             : ****************************************************************************/
     871                 :           0 : int inchi_print_nodisplay(FILE* f, const char* lpszFormat, ...)
     872                 :             : {
     873                 :           0 :     int ret = 0;
     874                 :             :     va_list argList;
     875                 :             :     FILE* fi;
     876                 :             : 
     877         [ #  # ]:           0 :     if (f)
     878                 :             :     {
     879                 :           0 :         fi = f;
     880                 :             :     }
     881                 :             :     else
     882                 :             :     {
     883                 :           0 :         fi = stdout;
     884                 :             :     }
     885                 :             : 
     886                 :           0 :     my_va_start(argList, lpszFormat);
     887                 :           0 :     ret = vfprintf(fi, lpszFormat, argList);
     888                 :           0 :     va_end(argList);
     889                 :           0 :     return ret;
     890                 :             : }
     891                 :             : 
     892                 :             : 
     893                 :             : #if ( FIX_READ_LONG_LINE_BUG == 1 )
     894                 :             : 
     895                 :             : 
     896                 :             : /****************************************************************************/
     897                 :           0 : int inchi_fgetsLfTab(char* szLine, int len, FILE* f)
     898                 :             : {
     899                 :             :     int  length;
     900                 :             :     char* p;
     901                 :             :     char szSkip[256];
     902                 :           0 :     int  bTooLongLine = 0;
     903                 :             :     do
     904                 :             :     {
     905                 :           0 :         p = inchi_fgetsTab(szLine, len, f);
     906         [ #  # ]:           0 :         if (!p)
     907                 :             :         {
     908                 :           0 :             return -1; /* end of file or cannot read */
     909                 :             :         }
     910   [ #  #  #  # ]:           0 :         bTooLongLine = ((int)strlen(szLine) == len - 1 && szLine[len - 2] != '\n');
     911                 :           0 :         lrtrim(szLine, &length);
     912         [ #  # ]:           0 :     } while (!length);
     913         [ #  # ]:           0 :     if (bTooLongLine)
     914                 :             :     {
     915         [ #  # ]:           0 :         while ((p = inchi_fgetsTab(szSkip, sizeof(szSkip) - 1, f))) /* djb-rwth: ignoring LLVM warning: function returning value */
     916                 :             :         {
     917         [ #  # ]:           0 :             if (strchr(szSkip, '\n'))
     918                 :           0 :                 break;
     919                 :             :         }
     920                 :             :     }
     921                 :             : 
     922                 :           0 :     return length;
     923                 :             : }
     924                 :             : 
     925                 :             : 
     926                 :             : #else
     927                 :             : 
     928                 :             : 
     929                 :             : /****************************************************************************/
     930                 :             : int inchi_fgetsLfTab(char* szLine, int len, FILE* f)
     931                 :             : {
     932                 :             :     int  length;
     933                 :             :     char* p;
     934                 :             :     char szSkip[256];
     935                 :             :     int  bTooLongLine = 0;
     936                 :             :     do
     937                 :             :     {
     938                 :             :         p = inchi_fgetsTab(szLine, len - 1, f);
     939                 :             :         if (!p)
     940                 :             :         {
     941                 :             :             return -1; /* end of file or cannot read */
     942                 :             :         }
     943                 :             :         szLine[len - 1] = '\0';
     944                 :             :         /*
     945                 :             :         bTooLongLine = !strchr( szLine, '\n' );
     946                 :             :         */
     947                 :             :         bTooLongLine = (!p && ((int)strlen(szLine)) == len - 2);
     948                 :             :         lrtrim(szLine, &length);
     949                 :             :     } while (!length);
     950                 :             :     if (bTooLongLine)
     951                 :             :     {
     952                 :             :         while (p = inchi_fgetsTab(szSkip, sizeof(szSkip) - 1, f))
     953                 :             :         {
     954                 :             :             szSkip[sizeof(szSkip) - 1] = '\0';
     955                 :             :             if (strchr(szSkip, '\n'))
     956                 :             :                 break;
     957                 :             :         }
     958                 :             :     }
     959                 :             :     return length;
     960                 :             : }
     961                 :             : #endif
     962                 :             : 
     963                 :             : 
     964                 :             : /****************************************************************************
     965                 :             :     Read up to tab or LF but not more than len chars;
     966                 :             :     if empty line, read further until a non-empty line;
     967                 :             :     remove leading and trailing white spaces;
     968                 :             :     keep zero termination
     969                 :             : ****************************************************************************/
     970                 :           0 : char* inchi_fgetsTab(char* szLine, int len, FILE* f)
     971                 :             : {
     972                 :           0 :     int  length = 0, c = 0;
     973                 :           0 :     len--;
     974   [ #  #  #  # ]:           0 :     while (length < len && EOF != (c = fgetc(f)))
     975                 :             :     {
     976         [ #  # ]:           0 :         if (c == '\t')
     977                 :             :         {
     978                 :           0 :             c = '\n';
     979                 :             :         }
     980                 :           0 :         szLine[length++] = (char)c;
     981         [ #  # ]:           0 :         if (c == '\n')
     982                 :             :         {
     983                 :           0 :             break;
     984                 :             :         }
     985                 :             :     }
     986   [ #  #  #  # ]:           0 :     if (!length && EOF == c)
     987                 :             :     {
     988                 :           0 :         return NULL;
     989                 :             :     }
     990                 :           0 :     szLine[length] = '\0';
     991                 :             : 
     992                 :           0 :     return szLine;
     993                 :             : }
     994                 :             : 
     995                 :             : 
     996                 :             : /****************************************************************************
     997                 :             :     Read up to LF but not more than line_len bytes;
     998                 :             :     if input line is too long, quietly ignore the rest of the line
     999                 :             : ****************************************************************************/
    1000                 :        1973 : char* inchi_fgetsLf(char* line, int line_len, INCHI_IOSTREAM* inp_stream)
    1001                 :             : {
    1002                 :        1973 :     char* p = NULL, * q;
    1003                 :        1973 :     FILE* finp = NULL;
    1004                 :             : 
    1005         [ -  + ]:        1973 :     if (inp_stream->type == INCHI_IOS_TYPE_FILE)
    1006                 :             :     {
    1007                 :             :         /* Read from file */
    1008                 :           0 :         finp = inp_stream->f;
    1009                 :           0 :         memset(line, 0, line_len); /* djb-rwth: memset_s C11/Annex K variant? */
    1010         [ #  # ]:           0 :         if (NULL != (p = fgets(line, line_len, finp)) &&
    1011         [ #  # ]:           0 :             NULL == strchr(p, '\n'))
    1012                 :             :         {
    1013                 :             :             char temp[64];
    1014                 :             :             /* bypass up to '\n' or up to end of file whichever comes first */
    1015   [ #  #  #  # ]:           0 :             while (NULL != fgets(temp, sizeof(temp), finp) && NULL == strchr(temp, '\n'))
    1016                 :             :             {
    1017                 :             :                 ;
    1018                 :             :             }
    1019                 :             :         }
    1020                 :             :     }
    1021         [ +  - ]:        1973 :     else if (inp_stream->type == INCHI_IOS_TYPE_STRING)
    1022                 :             :     {
    1023                 :             :         /* Read from supplied string representing Molfile */
    1024                 :        1973 :         memset(line, 0, line_len); /* djb-rwth: memset_s C11/Annex K variant? */
    1025         [ +  + ]:        1973 :         if (NULL != (p = inchi_sgets(line, line_len, inp_stream)) &&
    1026         [ +  + ]:        1809 :             NULL == strchr(p, '\n'))
    1027                 :             :         {
    1028                 :             :             char temp[64];
    1029                 :             :             /* bypass up to '\n' or up to end of file whichever comes first */
    1030   [ -  +  -  - ]:           4 :             while (NULL != inchi_sgets(temp, sizeof(temp), inp_stream) && NULL == strchr(temp, '\n'))
    1031                 :             :             {
    1032                 :             :                 ;
    1033                 :             :             }
    1034                 :             :         }
    1035                 :             :     }
    1036                 :             :     else
    1037                 :             :     {
    1038                 :             :         ;
    1039                 :             :     }
    1040                 :             : 
    1041         [ +  + ]:        1973 :     if (p)
    1042                 :             :     {
    1043         [ -  + ]:        1809 :         if ((q = strchr(line, '\r'))) /* djb-rwth: addressing LLVM warning */
    1044                 :             :         {
    1045                 :             :             /*  fix CR CR LF line terminator. */
    1046                 :           0 :             q[0] = '\n';
    1047                 :           0 :             q[1] = '\0';
    1048                 :             :         }
    1049                 :             :     }
    1050                 :             : 
    1051                 :        1973 :     return p;
    1052                 :             : }
    1053                 :             : 
    1054                 :             : 
    1055                 :             : /****************************************************************************
    1056                 :             :     Estimate printf string length.
    1057                 :             : 
    1058                 :             :     The code is based on Microsoft Knowledge Base article Q127038:
    1059                 :             :     "FIX: CString::Format Gives Assertion Failed, Access Violation"
    1060                 :             :     (Related to Microsoft Visual C++, 32-bit Editions, versions 2.0, 2.1)
    1061                 :             : ****************************************************************************/
    1062                 :             : 
    1063                 :             : #define FORCE_ANSI      0x10000
    1064                 :             : #define FORCE_UNICODE   0x20000
    1065                 :             : 
    1066                 :             : /****************************************************************************
    1067                 :             :  Formatting (using wsprintf style formatting)
    1068                 :             : ****************************************************************************/
    1069                 :      558628 : int GetMaxPrintfLength(const char* lpszFormat, va_list argList)
    1070                 :             : {
    1071                 :             :     /*ASSERT(AfxIsValidString(lpszFormat, FALSE));*/
    1072                 :             :     const char* lpsz;
    1073                 :             :     int nMaxLen, nWidth, nPrecision, nModifier, nItemLen;
    1074                 :             : 
    1075                 :      558628 :     nMaxLen = 0;
    1076                 :             :     /* make a guess at the maximum length of the resulting string */
    1077         [ +  + ]:     1135653 :     for (lpsz = lpszFormat; *lpsz; lpsz++)
    1078                 :             :     {
    1079                 :             :         /* moved from below for C syntax reason - 2024-09-01 DT */
    1080                 :             :         /* djb-rwth: return values needed for va_arg; djb-rwth: ignoring LLVM warnings: function returning value */
    1081                 :             :         int ivarg;
    1082                 :             :         double dvarg;
    1083                 :             :         void* ivvarg;
    1084                 :             :         int* ipvarg;
    1085                 :             : 
    1086                 :             :         /* handle '%' character, but watch out for '%%' */
    1087   [ +  +  -  + ]:      577025 :         if (*lpsz != '%' || *(++lpsz) == '%')
    1088                 :             :         {
    1089                 :       17809 :             nMaxLen += 1;
    1090                 :       17809 :             continue;
    1091                 :             :         }
    1092                 :             : 
    1093                 :      559216 :         nItemLen = 0;
    1094                 :             : 
    1095                 :             :         /*  handle '%' character with format */
    1096                 :      559216 :         nWidth = 0;
    1097         [ +  - ]:      560003 :         for (; *lpsz; lpsz++)
    1098                 :             :         {
    1099                 :             :             /* check for valid flags */
    1100         [ -  + ]:      560003 :             if (*lpsz == '#')
    1101                 :             :             {
    1102                 :           0 :                 nMaxLen += 2;   /* for '0x' */
    1103                 :             :             }
    1104         [ -  + ]:      560003 :             else if (*lpsz == '*')
    1105                 :             :             {
    1106                 :           0 :                 nWidth = va_arg(argList, int);
    1107                 :             :             }
    1108   [ +  +  +  +  :      560003 :             else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0'
                   +  - ]
    1109         [ +  - ]:      559216 :                 || *lpsz == ' ')
    1110                 :             :             {
    1111                 :             :                 ;
    1112                 :             :             }
    1113                 :             :             else /* hit non-flag character */
    1114                 :             :             {
    1115                 :      559216 :                 break;
    1116                 :             :             }
    1117                 :             :         }
    1118                 :             :         /* get width and skip it */
    1119         [ +  - ]:      559216 :         if (nWidth == 0)
    1120                 :             :         {
    1121                 :             :             /* width indicated by */
    1122                 :      559216 :             nWidth = atoi(lpsz);
    1123   [ +  -  -  + ]:      559216 :             for (; *lpsz && isdigit(*lpsz); lpsz++)
    1124                 :             :             {
    1125                 :             :                 ;
    1126                 :             :             }
    1127                 :             :         }
    1128                 :             :         /*ASSERT(nWidth >= 0);*/
    1129         [ -  + ]:      559216 :         if (nWidth < 0)
    1130                 :             :         {
    1131                 :           0 :             goto exit_error; /* instead of exception */
    1132                 :             :         }
    1133                 :             : 
    1134                 :      559216 :         nPrecision = 0;
    1135         [ -  + ]:      559216 :         if (*lpsz == '.')
    1136                 :             :         {
    1137                 :             :             /* skip past '.' separator (width.precision)*/
    1138                 :           0 :             lpsz++;
    1139                 :             : 
    1140                 :             :             /* get precision and skip it*/
    1141         [ #  # ]:           0 :             if (*lpsz == '*')
    1142                 :             :             {
    1143                 :           0 :                 nPrecision = va_arg(argList, int);
    1144                 :           0 :                 lpsz++;
    1145                 :             :             }
    1146                 :             :             else
    1147                 :             :             {
    1148                 :           0 :                 nPrecision = atoi(lpsz);
    1149   [ #  #  #  # ]:           0 :                 for (; *lpsz && isdigit(*lpsz); lpsz++)
    1150                 :             :                 {
    1151                 :             :                     ;
    1152                 :             :                 }
    1153                 :             :             }
    1154         [ #  # ]:           0 :             if (nPrecision < 0)
    1155                 :             :             {
    1156                 :           0 :                 goto exit_error; /* instead of exception */
    1157                 :             :             }
    1158                 :             :         }
    1159                 :             : 
    1160                 :             :         /* should be on type modifier or specifier */
    1161                 :      559216 :         nModifier = 0;
    1162   [ -  +  -  + ]:      559216 :         switch (*lpsz)
    1163                 :             :         {
    1164                 :             :             /* modifiers that affect size */
    1165                 :           0 :         case 'h':
    1166         [ #  # ]:           0 :             switch (lpsz[1])
    1167                 :             :             {
    1168                 :           0 :             case 'd':
    1169                 :             :             case 'i':
    1170                 :             :             case 'o':
    1171                 :             :             case 'x':
    1172                 :             :             case 'X':
    1173                 :             :             case 'u':
    1174                 :             :                 /* short unsigned, short double, etc. -- added to the original MS example */
    1175                 :             :                 /* ignore the fact that these modifiers do affect size */
    1176                 :           0 :                 lpsz++;
    1177                 :           0 :                 break;
    1178                 :           0 :             default:
    1179                 :           0 :                 nModifier = FORCE_ANSI;
    1180                 :           0 :                 lpsz++;
    1181                 :           0 :                 break;
    1182                 :             :             }
    1183                 :           0 :             break;
    1184                 :          55 :         case 'l':
    1185         [ +  - ]:          55 :             switch (lpsz[1])
    1186                 :             :             {
    1187                 :          55 :             case 'd':
    1188                 :             :             case 'i':
    1189                 :             :             case 'o':
    1190                 :             :             case 'x':
    1191                 :             :             case 'X':
    1192                 :             :             case 'u':
    1193                 :             :             case 'f': /* long float -- post ANSI C */
    1194                 :             :                 /* long unsigned, long double, etc. -- added to the original MS example */
    1195                 :             :                 /* ignore the fact that these modifiers do affect size */
    1196                 :          55 :                 lpsz++;
    1197                 :          55 :                 break;
    1198                 :           0 :             default:
    1199                 :             :                 /*
    1200                 :             :                 nModifier = FORCE_UNICODE;
    1201                 :             :                 lpsz ++;
    1202                 :             :                 break;
    1203                 :             :                 */
    1204                 :           0 :                 goto exit_error;  /* no UNICODE, please */
    1205                 :             :             }
    1206                 :          55 :             break;
    1207                 :             :             /* modifiers that do not affect size */
    1208                 :           0 :         case 'F':
    1209                 :             :         case 'N':
    1210                 :             :         case 'L':
    1211                 :           0 :             lpsz++;
    1212                 :           0 :             break;
    1213                 :             :         }
    1214                 :             : 
    1215                 :             :         /* now should be on specifier */
    1216                 :             : 
    1217   [ +  -  -  +  :      559216 :         switch (*lpsz | nModifier)
                -  -  + ]
    1218                 :             :         {
    1219                 :             :             /* single characters*/
    1220                 :      524938 :         case 'c':
    1221                 :             :         case 'C':
    1222                 :      524938 :             nItemLen = 2;
    1223                 :      524938 :             ivarg = va_arg(argList, int); /* djb-rwth: int return value; ignoring LLVM warning */
    1224                 :      524938 :             break;
    1225                 :           0 :         case 'c' | FORCE_ANSI:
    1226                 :             :         case 'C' | FORCE_ANSI:
    1227                 :           0 :             nItemLen = 2;
    1228                 :           0 :             ivarg = va_arg(argList, int); /* djb-rwth: int return value; ignoring LLVM warning */
    1229                 :           0 :             break;
    1230                 :           0 :         case 'c' | FORCE_UNICODE:
    1231                 :             :         case 'C' | FORCE_UNICODE:
    1232                 :           0 :             goto exit_error;  /* no UNICODE, please */
    1233                 :             :             /*
    1234                 :             :             nItemLen = 2;
    1235                 :             :             va_arg(argList, wchar_t);
    1236                 :             :             break;
    1237                 :             :             */
    1238                 :             : 
    1239                 :             :             /* strings*/
    1240                 :       33466 :         case 's':
    1241                 :             :         case 'S':
    1242                 :       33466 :             nItemLen = (int)strlen(va_arg(argList, char*));
    1243                 :       33466 :             nItemLen = inchi_max(1, nItemLen);
    1244                 :       33466 :             break;
    1245                 :           0 :         case 's' | FORCE_ANSI:
    1246                 :             :         case 'S' | FORCE_ANSI:
    1247                 :           0 :             nItemLen = (int)strlen(va_arg(argList, char*));
    1248                 :           0 :             nItemLen = inchi_max(1, nItemLen);
    1249                 :           0 :             break;
    1250                 :             : 
    1251                 :           0 :         case 's' | FORCE_UNICODE:
    1252                 :             :         case 'S' | FORCE_UNICODE:
    1253                 :           0 :             goto exit_error;  /* no UNICODE, please */
    1254                 :             :             /*
    1255                 :             :             nItemLen = wcslen(va_arg(argList, wchar_t*));
    1256                 :             :             nItemLen = inchi_max(1, nItemLen);
    1257                 :             :             break;
    1258                 :             :             */
    1259                 :             :         }
    1260                 :             : 
    1261                 :             :         /* adjust nItemLen for strings */
    1262         [ +  + ]:      559216 :         if (nItemLen != 0)
    1263                 :             :         {
    1264                 :      558404 :             nItemLen = inchi_max(nItemLen, nWidth);
    1265         [ -  + ]:      558404 :             if (nPrecision != 0)
    1266                 :             :             {
    1267                 :           0 :                 nItemLen = inchi_min(nItemLen, nPrecision);
    1268                 :             :             }
    1269                 :             :         }
    1270                 :             :         else
    1271                 :             :         {
    1272   [ +  -  -  -  :         812 :             switch (*lpsz)
                      - ]
    1273                 :             :             {
    1274                 :             :                 /* integers */
    1275                 :         812 :             case 'd':
    1276                 :             :             case 'i':
    1277                 :             :             case 'u':
    1278                 :             :             case 'x':
    1279                 :             :             case 'X':
    1280                 :             :             case 'o':
    1281                 :         812 :                 ivarg = va_arg(argList, int); /* djb-rwth: int return value; ignoring LLVM warning */
    1282                 :         812 :                 nItemLen = 32;
    1283                 :         812 :                 nItemLen = inchi_max(nItemLen, nWidth + nPrecision);
    1284                 :         812 :                 break;
    1285                 :             : 
    1286                 :           0 :             case 'e':
    1287                 :             :             case 'f':
    1288                 :             :             case 'g':
    1289                 :             :             case 'G':
    1290                 :           0 :                 dvarg = va_arg(argList, double); /* djb-rwth: double return value; ignoring LLVM warning */
    1291                 :           0 :                 nItemLen = 32;
    1292                 :           0 :                 nItemLen = inchi_max(nItemLen, nWidth + nPrecision);
    1293                 :           0 :                 break;
    1294                 :             : 
    1295                 :           0 :             case 'p':
    1296                 :           0 :                 ivvarg = va_arg(argList, void*); /* djb-rwth: void* return value; ignoring LLVM warning */
    1297                 :           0 :                 nItemLen = 32;
    1298                 :           0 :                 nItemLen = inchi_max(nItemLen, nWidth + nPrecision);
    1299                 :           0 :                 break;
    1300                 :             : 
    1301                 :             :                 /* no output */
    1302                 :           0 :             case 'n':
    1303                 :           0 :                 ipvarg = va_arg(argList, int*); /* djb-rwth: int* return value; ignoring LLVM warning */
    1304                 :           0 :                 break;
    1305                 :             : 
    1306                 :           0 :             default:
    1307                 :             :                 /*ASSERT(FALSE);*/  /* unknown formatting option*/
    1308                 :           0 :                 goto exit_error; /* instead of exception */
    1309                 :             :             }
    1310                 :             :         }
    1311                 :             : 
    1312                 :             :         /* adjust nMaxLen for output nItemLen */
    1313                 :      559216 :         nMaxLen += nItemLen;
    1314                 :             :     }
    1315                 :      558628 :     return nMaxLen;
    1316                 :             : 
    1317                 :           0 : exit_error:
    1318                 :           0 :     return -1; /* wrong format */
    1319                 :             : }
    1320                 :             : 
    1321                 :             : 
    1322                 :             : /****************************************************************************
    1323                 :             :     Get at most n-1 chars, plus a null, then advance input's start.
    1324                 :             :     Return emulates fgets()
    1325                 :             : ****************************************************************************/
    1326                 :        1977 : char* inchi_sgets(char* s, int n, INCHI_IOSTREAM* ios)
    1327                 :             : {
    1328                 :        1977 :     int c = 0;
    1329                 :             :     char* p;
    1330                 :             :     char* inp;
    1331                 :             : 
    1332                 :        1977 :     inp = ios->s.pStr + ios->s.nPtr;
    1333                 :             : 
    1334         [ -  + ]:        1977 :     if (n <= 0)
    1335                 :             :     {
    1336                 :           0 :         return NULL;
    1337                 :             :     }
    1338                 :             : 
    1339         [ -  + ]:        1977 :     if (NULL == inp)
    1340                 :             :     {
    1341                 :             :         /* like EOF */
    1342                 :           0 :         return NULL; /* djb-rwth: addressing coverity ID #499480 -- inp can be NULL */
    1343                 :             :     }
    1344                 :             : 
    1345                 :        1977 :     p = s;
    1346                 :             : 
    1347                 :             :     /*
    1348                 :             :     if ( *inp == '\0' )
    1349                 :             :         s = '\0';
    1350                 :             :     else
    1351                 :             :     */
    1352                 :             : 
    1353   [ +  -  +  + ]:       60188 :     while (--n > 0 && (c = *inp++))
    1354                 :             :     {
    1355                 :       60016 :         ios->s.nPtr++;
    1356         [ +  + ]:       60016 :         if ((*p++ = c) == '\n')
    1357                 :             :         {
    1358                 :        1805 :             break;
    1359                 :             :         }
    1360                 :             :     }
    1361                 :        1977 :     *p = '\0';
    1362                 :             : 
    1363                 :             :     /* printf("\n*** {%-s}",s); */
    1364                 :             : 
    1365         [ +  + ]:         172 :     return (c == '\0' && p == s)
    1366                 :             :         ? NULL /* like EOF reached */
    1367         [ +  + ]:        2149 :         : s;
    1368                 :             : }
    1369                 :             : 
    1370                 :             : 
    1371                 :             : /****************************************************************************
    1372                 :             :  Init expandable buffer of type INCHI_IOS_STRING
    1373                 :             : ****************************************************************************/
    1374                 :         240 : int inchi_strbuf_init(INCHI_IOS_STRING* buf, int start_size, int incr_size)
    1375                 :             : {
    1376                 :         240 :     char* new_str = NULL;
    1377                 :         240 :     memset(buf, 0, sizeof(*buf)); /* djb-rwth: memset_s C11/Annex K variant? */
    1378                 :             : 
    1379         [ -  + ]:         240 :     if (start_size <= 0)
    1380                 :             :     {
    1381                 :           0 :         start_size = INCHI_STRBUF_INITIAL_SIZE;
    1382                 :             :     }
    1383         [ -  + ]:         240 :     if (incr_size <= 0)
    1384                 :             :     {
    1385                 :           0 :         incr_size = INCHI_STRBUF_SIZE_INCREMENT;
    1386                 :             :     }
    1387                 :             : 
    1388                 :         240 :     new_str = (char*)inchi_calloc(start_size, sizeof(char));
    1389                 :             : 
    1390         [ -  + ]:         240 :     if (!new_str)
    1391                 :             :     {
    1392                 :           0 :         return -1;
    1393                 :             :     }
    1394                 :             : 
    1395                 :         240 :     buf->pStr = new_str;
    1396                 :         240 :     buf->nAllocatedLength = start_size;
    1397                 :         240 :     buf->nPtr = incr_size;
    1398                 :             : 
    1399                 :         240 :     return start_size;
    1400                 :             : }
    1401                 :             : 
    1402                 :             : 
    1403                 :             : /****************************************************************************
    1404                 :             :     Reset INCHI_IOS_STRING object holding an expandable buffer string
    1405                 :             :     (place '\0' at the start and do _not_ free memory).
    1406                 :             : ****************************************************************************/
    1407                 :       38477 : void inchi_strbuf_reset(INCHI_IOS_STRING* buf)
    1408                 :             : {
    1409         [ -  + ]:       38477 :     if (!buf)
    1410                 :             :     {
    1411                 :           0 :         return;
    1412                 :             :     }
    1413         [ +  + ]:       38477 :     if (buf->pStr)
    1414                 :             :     {
    1415                 :       38055 :         buf->pStr[0] = '\0';
    1416                 :             :     }
    1417                 :             : 
    1418                 :       38477 :     buf->nUsedLength = buf->nPtr = 0;
    1419                 :             : }
    1420                 :             : 
    1421                 :             : 
    1422                 :             : /****************************************************************************
    1423                 :             : Close INCHI_IOS_STRING object holding an expandable buffer string,
    1424                 :             :     free previously allocated sring memory
    1425                 :             : ****************************************************************************/
    1426                 :         483 : void inchi_strbuf_close(INCHI_IOS_STRING* buf)
    1427                 :             : {
    1428         [ -  + ]:         483 :     if (!buf)
    1429                 :             :     {
    1430                 :           0 :         return;
    1431                 :             :     }
    1432         [ +  - ]:         483 :     if (buf->pStr)
    1433                 :             :     {
    1434         [ +  - ]:         483 :         inchi_free(buf->pStr);
    1435                 :             :     }
    1436                 :             : 
    1437                 :         483 :     memset(buf, 0, sizeof(*buf)); /* djb-rwth: memset_s C11/Annex K variant? */
    1438                 :             : }
    1439                 :             : 
    1440                 :             : 
    1441                 :             : /****************************************************************************/
    1442                 :          15 : int inchi_strbuf_create_copy(INCHI_IOS_STRING* buf2, INCHI_IOS_STRING* buf)
    1443                 :             : {
    1444                 :          15 :     char* new_str = NULL;
    1445                 :             : 
    1446                 :          15 :     new_str = (char*)inchi_calloc(buf->nAllocatedLength, sizeof(char));
    1447                 :          15 :     buf2->pStr = new_str;
    1448         [ -  + ]:          15 :     if (!new_str)
    1449                 :             :     {
    1450                 :           0 :         return -1;
    1451                 :             :     }
    1452                 :          15 :     buf2->nAllocatedLength = buf->nAllocatedLength;
    1453                 :          15 :     buf2->nUsedLength = buf->nUsedLength;
    1454                 :          15 :     buf2->nPtr = buf->nPtr;
    1455                 :             : 
    1456                 :          15 :     return 0;
    1457                 :             : }
    1458                 :             : 
    1459                 :             : 
    1460                 :             : /****************************************************************************
    1461                 :             :  Check size and if necessary expand string buffer in INCHI_IOS_STRING
    1462                 :             : ****************************************************************************/
    1463                 :      553943 : int inchi_strbuf_update(INCHI_IOS_STRING* buf, int new_addition_size)
    1464                 :             : {
    1465                 :             :     int requsted_len;
    1466                 :             : 
    1467         [ -  + ]:      553943 :     if (!buf)
    1468                 :             :     {
    1469                 :           0 :         return -1;
    1470                 :             :     }
    1471                 :             : 
    1472         [ -  + ]:      553943 :     if (new_addition_size <= 0)
    1473                 :             :     {
    1474                 :           0 :         return buf->nAllocatedLength;
    1475                 :             :     }
    1476                 :             : 
    1477                 :      553943 :     requsted_len = buf->nUsedLength + new_addition_size;
    1478                 :             : 
    1479         [ +  + ]:      553943 :     if (requsted_len >= buf->nAllocatedLength)
    1480                 :             :     {
    1481                 :             :         /* Expand */
    1482                 :        6140 :         int  nAddLength = inchi_max(buf->nPtr, new_addition_size);
    1483                 :             :         /* buf->nPtr stores size increment for this buffer */
    1484                 :             :         char* new_str =
    1485                 :        6140 :             (char*)inchi_calloc((long long)buf->nAllocatedLength + (long long)nAddLength,
    1486                 :             :                 sizeof(new_str[0])); /* djb-rwth: cast operators added */
    1487         [ -  + ]:        6140 :         if (!new_str)
    1488                 :             :         {
    1489                 :           0 :             return -1; /* failed */
    1490                 :             :         }
    1491         [ +  + ]:        6140 :         if (buf->pStr)
    1492                 :             :         {
    1493         [ +  - ]:        5794 :             if (buf->nUsedLength > 0)
    1494                 :             :             {
    1495                 :        5794 :                 memcpy(new_str, buf->pStr, sizeof(new_str[0]) * buf->nUsedLength);
    1496                 :             :             }
    1497         [ +  - ]:        5794 :             inchi_free(buf->pStr);
    1498                 :             :         }
    1499                 :        6140 :         buf->pStr = new_str;
    1500                 :        6140 :         buf->nAllocatedLength += nAddLength;
    1501                 :             :     }
    1502                 :             : 
    1503                 :      553943 :     return buf->nAllocatedLength;
    1504                 :             : }
    1505                 :             : 
    1506                 :             : 
    1507                 :             : /****************************************************************************
    1508                 :             :  Add to the end of string in INCHI_IOS_STRING object,
    1509                 :             :     expanding buffer if necessary
    1510                 :             : ****************************************************************************/
    1511                 :      552775 : int inchi_strbuf_printf(INCHI_IOS_STRING* buf, const char* lpszFormat, ...)
    1512                 :             : {
    1513                 :      552775 :     int ret = 0, max_len;
    1514                 :             :     va_list argList;
    1515                 :             : 
    1516         [ -  + ]:      552775 :     if (!buf)
    1517                 :             :     {
    1518                 :           0 :         return -1;
    1519                 :             :     }
    1520                 :             : 
    1521                 :      552775 :     my_va_start(argList, lpszFormat);
    1522                 :      552775 :     max_len = GetMaxPrintfLength(lpszFormat, argList);
    1523                 :      552775 :     va_end(argList);
    1524         [ -  + ]:      552775 :     if (max_len < 0)
    1525                 :             :     {
    1526                 :           0 :         return 0;
    1527                 :             :     }
    1528                 :             : 
    1529                 :      552775 :     inchi_strbuf_update(buf, max_len);
    1530                 :             : 
    1531                 :      552775 :     my_va_start(argList, lpszFormat);
    1532                 :      552775 :     ret = vsprintf(buf->pStr + buf->nUsedLength, lpszFormat, argList);
    1533                 :      552775 :     va_end(argList);
    1534         [ +  - ]:      552775 :     if (ret >= 0)
    1535                 :             :     {
    1536                 :      552775 :         buf->nUsedLength += ret;
    1537                 :             :     }
    1538                 :             : 
    1539                 :      552775 :     return ret;
    1540                 :             : }
    1541                 :             : 
    1542                 :             : 
    1543                 :             : /****************************************************************************
    1544                 :             :     Print to string in INCHI_IOS_STRING object
    1545                 :             :     from specified position 'npos', expanding buffer if necessary.
    1546                 :             :     NB: be careful, intentionally no checks on where is 'npos'!
    1547                 :             : ****************************************************************************/
    1548                 :           0 : int inchi_strbuf_printf_from(INCHI_IOS_STRING* buf,
    1549                 :             :     int npos,
    1550                 :             :     const char* lpszFormat, ...)
    1551                 :             : {
    1552                 :           0 :     int ret = 0, max_len;
    1553                 :             :     va_list argList;
    1554                 :             : 
    1555         [ #  # ]:           0 :     if (!buf)
    1556                 :             :     {
    1557                 :           0 :         return -1;
    1558                 :             :     }
    1559                 :             : 
    1560                 :           0 :     my_va_start(argList, lpszFormat);
    1561                 :           0 :     max_len = GetMaxPrintfLength(lpszFormat, argList);
    1562                 :           0 :     va_end(argList);
    1563         [ #  # ]:           0 :     if (max_len < 0)
    1564                 :             :     {
    1565                 :           0 :         return 0;
    1566                 :             :     }
    1567                 :             : 
    1568                 :           0 :     max_len += npos;
    1569                 :             : 
    1570                 :           0 :     inchi_strbuf_update(buf, max_len);
    1571                 :             : 
    1572                 :           0 :     my_va_start(argList, lpszFormat);
    1573                 :           0 :     ret = vsprintf(buf->pStr + npos, lpszFormat, argList);
    1574                 :           0 :     va_end(argList);
    1575         [ #  # ]:           0 :     if (ret >= 0)
    1576                 :             :     {
    1577                 :           0 :         buf->nUsedLength = npos + ret;
    1578                 :             :     }
    1579                 :             : 
    1580                 :           0 :     return ret;
    1581                 :             : }
    1582                 :             : 
    1583                 :             : 
    1584                 :             : /****************************************************************************
    1585                 :             :     Reads the next line to growing str buf.
    1586                 :             :     Returns n of read chars, -1 at end of file or at error.
    1587                 :             : *****************************************************************************/
    1588                 :           0 : int inchi_strbuf_getline(INCHI_IOS_STRING* buf,
    1589                 :             :     FILE* f,
    1590                 :             :     int crlf2lf,
    1591                 :             :     int preserve_lf)
    1592                 :             : {
    1593                 :             :     int c;
    1594                 :           0 :     inchi_strbuf_reset(buf);
    1595                 :             : 
    1596                 :             :     while (1)
    1597                 :             :     {
    1598                 :           0 :         c = fgetc(f);
    1599         [ #  # ]:           0 :         if (ferror(f))
    1600                 :             :         {
    1601                 :           0 :             return -1;
    1602                 :             :         }
    1603         [ #  # ]:           0 :         if (c == EOF)
    1604                 :             :         {
    1605                 :           0 :             return -1;
    1606                 :             :         }
    1607                 :           0 :         inchi_strbuf_printf(buf, "%c", c);
    1608         [ #  # ]:           0 :         if (c == '\n')
    1609                 :             :         {
    1610                 :           0 :             break;
    1611                 :             :         }
    1612                 :             :     }
    1613                 :             : 
    1614         [ #  # ]:           0 :     if (crlf2lf)
    1615                 :             :     {
    1616         [ #  # ]:           0 :         if (buf->nUsedLength > 2)
    1617                 :             :         {
    1618         [ #  # ]:           0 :             if (buf->pStr[buf->nUsedLength - 2] == '\r')
    1619                 :             :             {
    1620                 :           0 :                 buf->pStr[buf->nUsedLength - 2] = '\n';
    1621                 :           0 :                 buf->pStr[--buf->nUsedLength] = '\0';
    1622                 :             :             }
    1623                 :             :         }
    1624                 :             :     }
    1625         [ #  # ]:           0 :     if (!preserve_lf)
    1626                 :             :     {
    1627                 :           0 :         buf->pStr[--buf->nUsedLength] = '\0';
    1628                 :             :     }
    1629                 :             : 
    1630                 :           0 :     return buf->nUsedLength;
    1631                 :             : }
    1632                 :             : 
    1633                 :             : 
    1634                 :             : 
    1635                 :             : /****************************************************************************
    1636                 :             :     Adds the next line to growing str buf (does not reset buf before adding).
    1637                 :             :     Returns n of read chars, -1 at end of file or at error.
    1638                 :             : ****************************************************************************/
    1639                 :       17093 : int inchi_strbuf_addline(INCHI_IOS_STRING* buf,
    1640                 :             :     INCHI_IOSTREAM* inp_stream,
    1641                 :             :     int crlf2lf,
    1642                 :             :     int preserve_lf)
    1643                 :             : {
    1644                 :             :     int c;
    1645                 :             : 
    1646                 :             :     while (1)
    1647                 :             :     {
    1648                 :      524940 :         c = inchi_ios_str_getc(inp_stream);
    1649         [ +  + ]:      524940 :         if (c == EOF)
    1650                 :             :         {
    1651                 :           2 :             return -1;
    1652                 :             :         }
    1653                 :      524938 :         inchi_strbuf_printf(buf, "%c", c);
    1654         [ +  + ]:      524938 :         if (c == '\n')
    1655                 :             :         {
    1656                 :       17091 :             break;
    1657                 :             :         }
    1658                 :             :     }
    1659         [ +  - ]:       17091 :     if (crlf2lf)
    1660                 :             :     {
    1661         [ +  - ]:       17091 :         if (buf->nUsedLength > 2)
    1662                 :             :         {
    1663         [ -  + ]:       17091 :             if (buf->pStr[buf->nUsedLength - 2] == '\r')
    1664                 :             :             {
    1665                 :           0 :                 buf->pStr[buf->nUsedLength - 2] = '\n';
    1666                 :           0 :                 buf->pStr[--buf->nUsedLength] = '\0';
    1667                 :             :             }
    1668                 :             :         }
    1669                 :             :     }
    1670         [ +  - ]:       17091 :     if (!preserve_lf)
    1671                 :             :     {
    1672                 :       17091 :         buf->pStr[--buf->nUsedLength] = '\0';
    1673                 :             :     }
    1674                 :             : 
    1675                 :       17091 :     return buf->nUsedLength;
    1676                 :             : }
    1677                 :             : 
    1678                 :             : 
    1679                 :             : #if ( defined(_WIN32) && defined(_DEBUG) && defined(_MSC_VER) )
    1680                 :             : #include <windows.h>
    1681                 :             : 
    1682                 :             : 
    1683                 :             : /****************************************************************************/
    1684                 :             : /* djb-rwth: placed as a global variable to avoid function buffer issues */
    1685                 :             : char it_buffer[32767];
    1686                 :             : int _inchi_trace(const char* format, ...)
    1687                 :             : {
    1688                 :             :     /*
    1689                 :             :     TCHAR buffer[32767];
    1690                 :             :     char buffer[32767];
    1691                 :             :     */
    1692                 :             :     int ret;
    1693                 :             : 
    1694                 :             :     va_list argptr;
    1695                 :             :     va_start(argptr, format);
    1696                 :             :     /*wvsprintf(buffer, format, argptr);*/
    1697                 :             :     ret = vsprintf(it_buffer, format, argptr);
    1698                 :             :     va_end(argptr);
    1699                 :             :     OutputDebugString(it_buffer);
    1700                 :             :     return 1;
    1701                 :             : }
    1702                 :             : #else
    1703                 :           0 : int _inchi_trace(const char* format, ...)
    1704                 :             : {
    1705                 :           0 :     return 1;
    1706                 :             : }
    1707                 :             : #endif
    1708                 :             : 
    1709                 :             : 
    1710                 :             : /****************************************************************************
    1711                 :             : Output structure (compound) header for current record
    1712                 :             : ****************************************************************************/
    1713                 :           0 : int Output_RecordInfo(INCHI_IOSTREAM* out_file,
    1714                 :             :     int              num_input_struct,
    1715                 :             :     int              bNoStructLabels,
    1716                 :             :     const char* szSdfLabel,
    1717                 :             :     const char* szSdfValue,
    1718                 :             :     unsigned long    lSdfId,
    1719                 :             :     char* pLF,
    1720                 :             :     char* pTAB)
    1721                 :             : {
    1722         [ #  # ]:           0 :     if (bNoStructLabels)
    1723                 :             :     {
    1724                 :           0 :         return 0;
    1725                 :             :     }
    1726                 :             : 
    1727                 :             : #ifdef TARGET_LIB_FOR_WINCHI
    1728                 :             :     /*out_file->s.nUsedLength = 0;
    1729                 :             :     inchi_ios_reset( out_file );*/
    1730                 :             : #endif
    1731   [ #  #  #  #  :           0 :     if (!(szSdfLabel && szSdfLabel[0]) && !(szSdfValue && szSdfValue[0]))
             #  #  #  # ]
    1732                 :             :     {
    1733                 :           0 :         inchi_ios_print_nodisplay(out_file, "%sStructure: %d", pLF, num_input_struct);
    1734                 :           0 :         inchi_ios_print_nodisplay(out_file, "%s", pTAB);
    1735                 :             :     }
    1736                 :             :     else
    1737                 :             :     {
    1738   [ #  #  #  #  :           0 :         inchi_ios_print_nodisplay(out_file, "%sStructure: %d.%s%s%s%s",
             #  #  #  # ]
    1739                 :             :             pLF, num_input_struct,
    1740   [ #  #  #  #  :           0 :             SDF_LBL_VAL(szSdfLabel, szSdfValue));
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1741                 :             : 
    1742         [ #  # ]:           0 :         if (lSdfId)
    1743                 :             :         {
    1744                 :           0 :             (out_file->s.nUsedLength)--;
    1745                 :           0 :             inchi_ios_print_nodisplay(out_file, ":%lu", lSdfId);
    1746                 :             :         }
    1747                 :           0 :         inchi_ios_print_nodisplay(out_file, "%s", pTAB);
    1748                 :             :     }
    1749                 :             : 
    1750                 :           0 :     return 0;
    1751                 :             : }
        

Generated by: LCOV version 2.0-1