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 <ctype.h>
8 : : #include <limits.h>
9 : : #include <locale.h>
10 : :
11 : : /* #define CHECK_WIN32_VC_HEAP */
12 : :
13 : : #include "mode.h"
14 : :
15 : : #if ( READ_INCHI_STRING == 1 )
16 : :
17 : : #include "ichierr.h"
18 : : #include "ichi.h"
19 : : #include "ichitime.h"
20 : : #include "ichidrp.h"
21 : : #include "inpdef.h"
22 : : #include "util.h"
23 : : #include "strutil.h"
24 : : #include "ichi_io.h"
25 : :
26 : : /* reverse InChI */
27 : : #include "ichimain.h"
28 : : #include "extr_ct.h"
29 : : #include "ichitaut.h"
30 : : #include "ichister.h"
31 : : #include "strutil.h"
32 : : #include "ichisize.h"
33 : : #include "ichiring.h"
34 : : #include "ichinorm.h"
35 : : #include "ichierr.h"
36 : : #include "ichicant.h"
37 : :
38 : : #include "ichirvrs.h"
39 : : #include "mol_fmt.h"
40 : :
41 : :
42 : : /* */
43 : : #if ( defined(TARGET_API_LIB) || defined(TARGET_EXE_STANDALONE) )
44 : : #include "inchi_api.h"
45 : : #endif
46 : : /* */
47 : :
48 : : #include "bcf_s.h"
49 : :
50 : : #define SEGM_LINE_ADD 128
51 : :
52 : : typedef struct tagOneLinkedBond
53 : : {
54 : : AT_NUMB neigh; /* canonical number of a neighbor */
55 : : AT_NUMB prev; /* position of the previous neighbor in the list */
56 : : } ONE_LINKED_BOND;
57 : :
58 : : typedef struct tagLinkedBonds
59 : : {
60 : : ONE_LINKED_BOND* pBond;
61 : : int len;
62 : : int len_alloc;
63 : : }LINKED_BONDS;
64 : : #define LINKED_BOND_ADD 128
65 : :
66 : : typedef enum tagModeProtonIsoExchgH
67 : : {
68 : : MODE_PIXH_UNDEFINED, /* 0 */
69 : : MODE_PIXH_ADD_TO_FIRST, /* 1 */
70 : : MODE_PIXH_ADD_TO_EACH, /* 2 */
71 : : MODE_PIXH_ADD_A_PIXH_COMPONENT, /* 3 */
72 : : MODE_PIXH_KEEP_TOTALS /* 4 */
73 : : } MODE_PIXH;
74 : :
75 : : #define INCHI_INPUT_CLASS_MASK 0x03
76 : : #define INCHI_INPUT_CLASS_NONSTANDARD 0x00
77 : : #define INCHI_INPUT_CLASS_STANDARD 0x01
78 : : #define INCHI_INPUT_CLASS_BETA 0x02
79 : : #define INCHI_INPUT_MI_MARKER 0x04
80 : :
81 : :
82 : : /* Local prototypes */
83 : : int InChILine2Data(INCHI_IOSTREAM* pInp,
84 : : SEGM_LINE* pLine,
85 : : char** pStr,
86 : : int* pState,
87 : : int* nErr,
88 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
89 : : int nNumComponents[INCHI_NUM][TAUT_NUM],
90 : : REM_PROTONS nNumProtons[INCHI_NUM][TAUT_NUM],
91 : : int s[INCHI_NUM][TAUT_NUM][2],
92 : : int bReadCoord,
93 : : int bInchi2Struct,
94 : : INCHI_MODE nMode,
95 : : int* bStdFormat,
96 : : int* input_has_save_opt,
97 : : unsigned char* input_save_opt_bits,
98 : : OAD_Polymer** ppolymer,
99 : : OAD_V3000** pv3000);
100 : :
101 : : static int GetInChIFormulaNumH(INChI* pInChI, int* nNumH);
102 : : static int GetInChINumH(INChI* pInChI, int* nNumH);
103 : : static int GetInChIIsoH(INChI* pInChI, int nNumIsotopicH[NUM_H_ISOTOPES]);
104 : :
105 : : static int getInChIChar(INCHI_IOSTREAM* pInp);
106 : : static int AddInChIChar(INCHI_IOSTREAM* pInp, SEGM_LINE* Line, const char* pszToken);
107 : : static int AddLinkedBond(AT_NUMB at1, AT_NUMB at2, AT_NUMB num_at, LINKED_BONDS* pLB);
108 : : static int bInChIHasReconnectedMetal(INChI* pInChI);
109 : : static int SetProtonsAndXchgIsoH(int bInChI2Structure,
110 : : int bReqSplitOutputInChI,
111 : : int bReqProtonsForEachComponent,
112 : : int bReqNonTaut, int bReqStereo,
113 : : int num_components[INCHI_NUM],
114 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
115 : : InpInChI* OneInput);
116 : : #if ( FIX_DALKE_BUGS == 1 )
117 : : static int SetHillFormFromInChI(InpInChI* OneInput);
118 : : #endif
119 : :
120 : : static int nGetInChISegment(INCHI_IOSTREAM* pInp, SEGM_LINE* Line, const char* pszToken);
121 : :
122 : : static int CopySegment(INChI* pInChITo, INChI* pInChIFrom, int StereoType,
123 : : int bIsotopicTo, int bIsotopicFrom);
124 : : static int nFillOutProtonMobileH(INChI* pInChI);
125 : : static int nProtonCopyIsotopicInfo(INChI* pInChI_to, INChI* pInChI_from);
126 : : static int CopyAtomNumbers(INChI* pInChI_To, int bIsoTo, INChI* pInChI_From, int bIsoFrom);
127 : :
128 : : static int ParseSegmentFormula(const char* str, int bMobileH, INChI* pInpInChI[],
129 : : int nNumComponents[], int* na_total);
130 : : static int ParseSegmentConnections(const char* str, int bMobileH, INChI** pInpInChI,
131 : : int* pnNumComponents, int* pbAbc, int* nb_total);
132 : : static int ParseSegmentMobileH(const char* str, int bMobileH, INChI* pInpInChI[],
133 : : int pnNumComponents[], int* pbAbc);
134 : : static int ParseSegmentCharge(const char* str, int bMobileH, INChI* pInpInChI[],
135 : : int nNumComponents[]);
136 : : static int ParseSegmentProtons(const char* str, int bMobileH,
137 : : REM_PROTONS nNumProtons[], int nNumComponents[]);
138 : : static int ParseSegmentPolymer(const char* str, int bMobileH,
139 : : REM_PROTONS nNumProtons[], int nNumComponents[],
140 : : int na_total, int nb_total,
141 : : int bInchi2Struct,
142 : : OAD_Polymer** ppPolymer, OAD_V3000** ppV3000);
143 : : static int ParseSegmentSp2(const char* str, int bMobileH, INChI* pInpInChI[],
144 : : int nNumComponents[], int state, int* pbAbc);
145 : : static int ParseSegmentSp3(const char* str, int bMobileH, INChI* pInpInChI[],
146 : : int ppnNumComponents[], int state, int* pbAbc);
147 : : static int SegmentSp3CreateEmpty(const char* str, int bMobileH, INChI* pInpInChI[],
148 : : int nNumComponents, int state, int* pbAbc);
149 : : static int SegmentSp3StoreStereoCenters(int* pbAbc, const char* pStart, const char* pEnd,
150 : : int pInChI_iComponent_nNumberOfAtoms,
151 : : INChI_Stereo* PStereo_0);
152 : : static int SegmentSp3ProcessAbbreviation(int* mpy_component, int iComponent, int nNumComponents,
153 : : int val, const char* q, int state, int* pbAbc,
154 : : int bMobileH, int nCpyType,
155 : : INChI* pInChI, INChI* pInpInChI_ALT_TAUT_bMobileH);
156 : : static int SegmentSp3CopyMultiplierCovered(int mpy_component, int iComponent,
157 : : INChI* pInpInChI, int bIso, int nCpyType);
158 : : static int ParseSegmentSp3m(const char* str, int bMobileH, INChI* pInpInChI[],
159 : : int nNumComponents[], int state);
160 : : static int bIsSp3LayerNotEmpty(INChI* pInpInChI[], int bMobileH,
161 : : int bIso, int nNumComponents);
162 : : static int ParseSegmentSp3s(const char* str, int bMobileH, INChI* pInpInChI[],
163 : : int s[TAUT_NUM][2], int ppnNumComponents[], int state);
164 : : static int ParseSegmentIsoAtoms(const char* str, int bMobileH, INChI* pInpInChI[],
165 : : int nNumComponents[], int state, int* pbAbc);
166 : : static int ParseSegmentIsoExchgH(const char* str, int bMobileH,
167 : : REM_PROTONS nNumProtons[],
168 : : int nNumComponents[], int state, int* pbAbc);
169 : : static int ParseSegmentPerm(const char* str, int bMobileH, INChI* pInpInChI[],
170 : : int ppnNumComponents[], int state, int* pbAbc);
171 : : #if ( FIX_ISO_FIXEDH_BUG_READ == 1 )
172 : : static int bIsoMayBeArranged(int bInchi2Struct, int iso_diff[NUM_H_ISOTOPES],
173 : : REM_PROTONS nNumProtons[INCHI_NUM][TAUT_NUM],
174 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM], int nNumComponents[INCHI_NUM][TAUT_NUM], int iINChI);
175 : : #endif
176 : :
177 : : static int ReadInChILine(INCHI_IOSTREAM* pInp,
178 : : SEGM_LINE* pLine,
179 : : char** pStr,
180 : : int* pState,
181 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
182 : : int nNumComponents[INCHI_NUM][TAUT_NUM],
183 : : REM_PROTONS nNumProtons[INCHI_NUM][TAUT_NUM],
184 : : int s[INCHI_NUM][TAUT_NUM][2],
185 : : int* input_is_stdinchi,
186 : : int* input_has_save_opt,
187 : : unsigned char* input_save_opt_bits,
188 : : int bInchi2Struct,
189 : : OAD_Polymer** ppolymer,
190 : : OAD_V3000** pv3000);
191 : :
192 : : int ReadInChICoord(INCHI_IOSTREAM* pInp,
193 : : SEGM_LINE* pLine,
194 : : int* pState,
195 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
196 : : int nNumComponents[INCHI_NUM][TAUT_NUM]);
197 : :
198 : : static int OutputInChIAsRequested(struct tagCANON_GLOBALS* pCG,
199 : : INCHI_IOSTREAM* pOut,
200 : : INCHI_IOSTREAM* pLog,
201 : : ICHICONST INPUT_PARMS* ip_inp,
202 : : STRUCT_DATA* sd_inp,
203 : : InpInChI* OneInput,
204 : : int num_components[INCHI_NUM],
205 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
206 : : long num_inp,
207 : : unsigned char save_opt_bits);
208 : :
209 : : static int ParseAuxSegmentVersion(const char* str,
210 : : int bMobileH,
211 : : INChI* pInpInChI[],
212 : : int ppnNumComponents[],
213 : : int state);
214 : :
215 : : static int ParseAuxSegmentNumbers(const char* str,
216 : : int bMobileH,
217 : : INChI* pInpInChI[],
218 : : int ppnNumComponents[],
219 : : int state,
220 : : int* pbAbc);
221 : :
222 : : static int ParseAuxSegmentAtomEqu(const char* str,
223 : : int bMobileH,
224 : : INChI* pInpInChI[],
225 : : int ppnNumComponents[],
226 : : int state);
227 : :
228 : : static int ParseAuxSegmentGroupEqu(const char* str,
229 : : int bMobileH,
230 : : INChI* pInpInChI[],
231 : : int ppnNumComponents[],
232 : : int state);
233 : :
234 : : static int ParseAuxSegmentSp3Inv(const char* str,
235 : : int bMobileH,
236 : : INChI* pInpInChI[],
237 : : int ppnNumComponents[],
238 : : int state);
239 : :
240 : : static int ParseAuxSegmentSp3InvNumbers(const char* str,
241 : : int bMobileH,
242 : : INChI* pInpInChI[],
243 : : int ppnNumComponents[],
244 : : int state);
245 : :
246 : : static int ParseAuxSegmentReverseCRV(const char* str, int state);
247 : :
248 : : static int ParseAuxSegmentReverseAtoms(const char* str, int state);
249 : :
250 : : static int ParseAuxSegmentReverseBonds(const char* str, int state);
251 : :
252 : : static int ParseAuxSegmentReverseXYZ(const char* str,
253 : : XYZ_COORD** ppXYZ,
254 : : int state);
255 : :
256 : : static int AddAuxSegmentCoord(int nRet,
257 : : XYZ_COORD* pXYZ,
258 : : int nLenXYZ,
259 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
260 : : int nNumComponents[INCHI_NUM][TAUT_NUM]);
261 : :
262 : :
263 : : static void getInchiStateReadErr(int stat, char* szMsg);
264 : :
265 : : static const char* getInchiErrName(int nErr);
266 : :
267 : : static int extract_from_inchi_string(char* sinchi, InpInChI* OneInput);
268 : :
269 : : const char* ParseSegmentReadDelimitedNumbers(const char* str, const char* pEnd,
270 : : INT_ARRAY* numlist, char c_delim,
271 : : char c_stop, int* ret);
272 : :
273 : : #define SEG_END '/'
274 : : /* the following 2 definitions are used to allow tab-delimited InChI input - 2008-11-17 DT */
275 : : #define INCHI_INP_EOL(X) ((X)=='\n' || (X)=='\r' || (X)=='\t')
276 : : /*#define INCHI_TOKEN "/\n\r\t"*/
277 : : #define INCHI_TOKEN "/\n\r\t\\"
278 : :
279 : : typedef enum tagInChI_STATE
280 : : {
281 : : /* M */
282 : : IST_MOBILE_H_FORMULA, /* 0 */
283 : : IST_MOBILE_H_CONNECTIONS, /* 1 */
284 : : IST_MOBILE_H, /* 2 */
285 : : IST_MOBILE_H_CHARGE, /* 3 */
286 : : IST_MOBILE_H_PROTONS, /* 4 */
287 : : IST_MOBILE_H_SP2, /* 5 */
288 : : IST_MOBILE_H_SP3, /* 6 */
289 : : IST_MOBILE_H_SP3_M, /* 7 */
290 : : IST_MOBILE_H_SP3_S, /* 8 */
291 : :
292 : : /* Fork */
293 : : IST_MOBILE_H_ISO_LAYER_FORK, /* 9 */
294 : :
295 : : /* MI */
296 : : IST_MOBILE_H_ISO_ATOMS, /* 10 */
297 : : IST_MOBILE_H_ISO_EXCH_H, /* 11 */
298 : : IST_MOBILE_H_ISO_SP2, /* 12 */
299 : : IST_MOBILE_H_ISO_SP3, /* 13 */
300 : : IST_MOBILE_H_ISO_SP3_M, /* 14 */
301 : : IST_MOBILE_H_ISO_SP3_S, /* 15 */
302 : :
303 : : /* Fork */
304 : : IST_FIXED_H_LAYER_FORK, /* 16 */
305 : :
306 : : /* F */
307 : : IST_FIXED_H_FORMULA, /* 17 */
308 : : IST_FIXED_H, /* 18 */
309 : : IST_FIXED_H_CHARGE, /* 19 */
310 : : IST_FIXED_H_SP2, /* 20 */
311 : : IST_FIXED_H_SP3, /* 21 */
312 : : IST_FIXED_H_SP3_M, /* 22 */
313 : : IST_FIXED_H_SP3_S, /* 23 */
314 : : IST_FIXED_H_PERMUTATION, /* 24 */
315 : :
316 : : /* Fork */
317 : : IST_FIXED_H_ISO_LAYER_FORK, /* 25 */
318 : :
319 : : /* FI */
320 : : IST_FIXED_H_ISO_ATOMS, /* 26 */
321 : : IST_FIXED_H_ISO_LAYER, /* 27 */
322 : : IST_FIXED_H_ISO_SP2, /* 28 */
323 : : IST_FIXED_H_ISO_SP3, /* 29 */
324 : : IST_FIXED_H_ISO_SP3_M, /* 30 */
325 : : IST_FIXED_H_ISO_SP3_S, /* 31 */
326 : : IST_FIXED_H_ISO_PERMUTATION, /* 32 */
327 : :
328 : : /* Reconnected */
329 : : IST_RECONNECTED_LAYER_FORK, /* 33 */
330 : : IST_RECONNECTED_FORMULA, /* 34 */
331 : :
332 : : /* Other reading errors */
333 : : IST_MATERIAL_BALANCE_ERROR, /* 35 */
334 : :
335 : : /* */
336 : : IST_MOBILE_H_POLYMER, /* 36 */
337 : :
338 : : IST_END = -1
339 : : }INCHI_STATE;
340 : :
341 : :
342 : : #define IST_HAPPENED_IN_RECMET 100
343 : :
344 : :
345 : : typedef struct tagInchiReadErrMsg
346 : : {
347 : : int stat;
348 : : const char* msg;
349 : : } INCHI_READ_ERR_MSG;
350 : :
351 : : ICHICONST INCHI_READ_ERR_MSG irErrMsg[] =
352 : : {
353 : : /* M */
354 : : { IST_MOBILE_H_FORMULA, "MOBILE_H_FORMULA" },
355 : : { IST_MOBILE_H_CONNECTIONS, "MOBILE_H_CONNECTIONS" },
356 : : { IST_MOBILE_H, "MOBILE_H" },
357 : : { IST_MOBILE_H_CHARGE, "MOBILE_H_CHARGE" },
358 : : { IST_MOBILE_H_PROTONS, "MOBILE_H_PROTONS" },
359 : : { IST_MOBILE_H_SP2, "MOBILE_H_SP2" },
360 : : { IST_MOBILE_H_SP3, "MOBILE_H_SP3" },
361 : : { IST_MOBILE_H_SP3_M, "MOBILE_H_SP3_/m" },
362 : : { IST_MOBILE_H_SP3_S, "MOBILE_H_SP3_/s" },
363 : :
364 : : /* Fork */
365 : : { IST_MOBILE_H_ISO_LAYER_FORK, "MOBILE_H_ISO_LAYER_FORK" },
366 : :
367 : : /* MI */
368 : : { IST_MOBILE_H_ISO_ATOMS, "MOBILE_H_ISO_ATOMS" },
369 : : { IST_MOBILE_H_ISO_EXCH_H, "MOBILE_H_ISO_EXCH_H" },
370 : : { IST_MOBILE_H_ISO_SP2, "MOBILE_H_ISO_SP2" },
371 : : { IST_MOBILE_H_ISO_SP3, "MOBILE_H_ISO_SP3" },
372 : : { IST_MOBILE_H_ISO_SP3_M, "MOBILE_H_ISO_SP3_/m" },
373 : : { IST_MOBILE_H_ISO_SP3_S, "MOBILE_H_ISO_SP3_/s" },
374 : :
375 : : /* Fork */
376 : : { IST_FIXED_H_LAYER_FORK, "FIXED_H_LAYER_FORK" },
377 : :
378 : : /* F */
379 : : { IST_FIXED_H_FORMULA, "FIXED_H_FORMULA" },
380 : : { IST_FIXED_H, "FIXED_H" },
381 : : { IST_FIXED_H_CHARGE, "FIXED_H_CHARGE" },
382 : : { IST_FIXED_H_SP2, "FIXED_H_SP2" },
383 : : { IST_FIXED_H_SP3, "FIXED_H_SP3" },
384 : : { IST_FIXED_H_SP3_M, "FIXED_H_SP3_/m" },
385 : : { IST_FIXED_H_SP3_S, "FIXED_H_SP3_/s" },
386 : : { IST_FIXED_H_PERMUTATION, "FIXED_H_PERMUTATION" },
387 : :
388 : : /* Fork */
389 : : { IST_FIXED_H_ISO_LAYER_FORK, "FIXED_H_ISO_LAYER_FORK" },
390 : :
391 : : /* FI */
392 : : { IST_FIXED_H_ISO_ATOMS, "FIXED_H_ISO_ATOMS" },
393 : : { IST_FIXED_H_ISO_LAYER, "FIXED_H_ISO_LAYER" },
394 : : { IST_FIXED_H_ISO_SP2, "FIXED_H_ISO_SP2" },
395 : : { IST_FIXED_H_ISO_SP3, "FIXED_H_ISO_SP3" },
396 : : { IST_FIXED_H_ISO_SP3_M, "FIXED_H_ISO_SP3_m" },
397 : : { IST_FIXED_H_ISO_SP3_S, "FIXED_H_ISO_SP3_s" },
398 : : { IST_FIXED_H_ISO_PERMUTATION, "FIXED_H_ISO_PERMUTATION" },
399 : :
400 : : /* Reconnected */
401 : : { IST_RECONNECTED_LAYER_FORK, "RECONNECTED_LAYER_FORK" },
402 : : { IST_RECONNECTED_FORMULA, "RECONNECTED_FORMULA" },
403 : :
404 : : { IST_MATERIAL_BALANCE_ERROR, "MATERIAL_BALANCE" },
405 : :
406 : : { IST_MOBILE_H_POLYMER, "POLYMER_LAYER" },
407 : :
408 : : { IST_END, "Unknown Error" }
409 : : };
410 : :
411 : :
412 : :
413 : : typedef enum tagCopySegmentType
414 : : {
415 : : CPY_SP2,
416 : : CPY_SP3,
417 : : CPY_SP3_M,
418 : : CPY_SP3_S,
419 : : CPY_ISO_AT
420 : : } COPY_SEG_TYPE;
421 : :
422 : : #define NSTRLEN 524288
423 : : #define MAX_MSG_LEN 512
424 : : #define MAX_MSG_BUF_LEN 128
425 : :
426 : :
427 : : void PrepareSaveOptBits(INPUT_PARMS* ip,
428 : : INCHI_IOSTREAM* pLog,
429 : : const long num_inp,
430 : : const char* szCurHdr,
431 : : int input_has_save_opt,
432 : : unsigned char input_save_opt_bits,
433 : : unsigned char* save_opt_bits);
434 : :
435 : : void TreatErrorsInReadInChIString(int nReadStatus,
436 : : int nErr,
437 : : int pState,
438 : : INPUT_PARMS* ip,
439 : : INCHI_IOSTREAM* pOut,
440 : : INCHI_IOSTREAM* pLog,
441 : : long* num_inp,
442 : : long* num_errors,
443 : : long* num_processed,
444 : : char** pstrHdr,
445 : : char** pszCurHdr,
446 : : InpInChI* pOneInput);
447 : :
448 : : int ConvertInChI2Struct(ICHICONST INPUT_PARMS* ip_inp,
449 : : INPUT_PARMS* ip,
450 : : InpInChI* pOneInput,
451 : : inp_ATOM** at,
452 : : int* num_at,
453 : : OAD_Polymer** polymer,
454 : : OAD_V3000** v3000,
455 : : INCHI_IOSTREAM* pOut,
456 : : INCHI_IOSTREAM* pLog,
457 : : STRUCT_DATA* sd,
458 : : int num_components[INCHI_NUM],
459 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
460 : : char** pszCurHdr,
461 : : char* szMsg,
462 : : int nMsgLen,
463 : : char szMessage[MAX_MSG_LEN],
464 : : int nInitLenMessage,
465 : : int nMessageLen,
466 : : int input_is_stdinchi,
467 : : int bHasSomeReconnected,
468 : : int bHasSomeFixedH,
469 : : int bHasMetal,
470 : : int nModeFlagsStereo,
471 : : int bTautFlags,
472 : : int bReqNonTaut,
473 : : unsigned long WarningFlags[2][2],
474 : : long num_inp,
475 : : long* num_errors,
476 : : unsigned char save_opt_bits,
477 : : inchiTime* pulTStart,
478 : : long* ulProcessingTime,
479 : : struct tagINCHI_CLOCK* ic,
480 : : struct tagCANON_GLOBALS* pCG);
481 : :
482 : : int ConvertInChI2InChI(INPUT_PARMS* ip,
483 : : InpInChI* pOneInput,
484 : : INCHI_IOSTREAM* pOut,
485 : : INCHI_IOSTREAM* pLog,
486 : : STRUCT_DATA* sd,
487 : : int num_components[INCHI_NUM],
488 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
489 : : char** pszCurHdr,
490 : : long num_inp,
491 : : long* num_errors,
492 : : unsigned char save_opt_bits,
493 : : inchiTime* pulTStart,
494 : : long* ulProcessingTime,
495 : : struct tagINCHI_CLOCK* ic,
496 : : struct tagCANON_GLOBALS* pCG);
497 : :
498 : : int DetectAndExposePolymerInternals(INCHI_IOSTREAM* is);
499 : :
500 : : int DetectHiddenPolymerStuff(char* tmpstr, int tmpstrlen,
501 : : int* ninsert, int* insert_pos,
502 : : int insert_lead_offset, int* nstars);
503 : :
504 : :
505 : :
506 : : /****************************************************************************/
507 : 9 : void getInchiStateReadErr(int stat, char* szMsg)
508 : : /* const char *getInchiStateReadErr(int stat) */
509 : : {
510 : 9 : int i, bRecMet = 0;
511 : :
512 [ - + ]: 9 : if (stat >= IST_HAPPENED_IN_RECMET)
513 : : {
514 : 0 : bRecMet = 1;
515 : 0 : stat -= IST_HAPPENED_IN_RECMET;
516 : : }
517 [ + - + + ]: 52 : for (i = 0; 0 <= irErrMsg[i].stat && stat != irErrMsg[i].stat; i++)
518 : : {
519 : : ;
520 : : }
521 [ - + ]: 9 : sprintf(szMsg,
522 : : #if ( FIX_DALKE_BUGS == 1 )
523 : : "%s%.100s",
524 : : #else
525 : : "%s%s",
526 : : #endif
527 : 9 : irErrMsg[i].msg, bRecMet ? ", Reconnected layer" : "");
528 : :
529 : 9 : }
530 : :
531 : :
532 : : /****************************************************************************/
533 : 9 : const char* getInchiErrName(int nErr)
534 : : {
535 [ - - + - : 9 : switch (nErr)
- ]
536 : : {
537 : 0 : case RI_ERR_ALLOC:
538 : 0 : return "Allocation failed";
539 : 0 : case RI_ERR_PROGR:
540 : 0 : return "Program error";
541 : 9 : case RI_ERR_SYNTAX:
542 : 9 : return "Syntax error";
543 : 0 : case RI_ERR_EOL:
544 : 0 : return "End of line";
545 : : }
546 : 0 : return "Unknown error";
547 : : }
548 : :
549 : :
550 : : #if ( FIX_DALKE_BUGS == 1 )
551 : :
552 : :
553 : : /****************************************************************************/
554 : 16 : int SetHillFormFromInChI(InpInChI* OneInput)
555 : : {
556 : : int iINChI, iTaut, iComp, num_diff;
557 : : INChI* pINChI;
558 : : char* szHillFormulaOld;
559 [ + + ]: 48 : for (iINChI = 0, num_diff = 0; iINChI < INCHI_NUM; iINChI++)
560 : : {
561 [ + + ]: 96 : for (iTaut = TAUT_NON; iTaut < TAUT_NUM; iTaut++)
562 : : {
563 [ + + ]: 83 : for (iComp = 0; iComp < OneInput->nNumComponents[iINChI][iTaut]; iComp++)
564 : : {
565 : 19 : pINChI = &OneInput->pInpInChI[iINChI][iTaut][iComp];
566 [ + - + - : 19 : if (!pINChI->nNumberOfAtoms || pINChI->bDeleted || !pINChI->szHillFormula || !pINChI->szHillFormula[0])
+ - - + ]
567 : : {
568 : 0 : continue;
569 : : }
570 : 19 : szHillFormulaOld = pINChI->szHillFormula;
571 : 19 : pINChI->szHillFormula = AllocateAndFillHillFormula(pINChI);
572 [ + - + - : 19 : num_diff += !pINChI->szHillFormula || !pINChI->szHillFormula[0] || strcmp(pINChI->szHillFormula, szHillFormulaOld);
- + ]
573 [ + - ]: 19 : inchi_free(szHillFormulaOld);
574 : : }
575 : : }
576 : : }
577 : :
578 : 16 : return num_diff;
579 : : }
580 : : #endif
581 : :
582 : :
583 : : /****************************************************************************
584 : : Main entry point
585 : : ****************************************************************************/
586 : 28 : int ReadWriteInChI(INCHI_CLOCK* ic,
587 : : struct tagCANON_GLOBALS* pCG,
588 : : INCHI_IOSTREAM* pInp,
589 : : INCHI_IOSTREAM* pOut,
590 : : INCHI_IOSTREAM* pLog,
591 : : INPUT_PARMS* ip_inp,
592 : : STRUCT_DATA* sd_inp,
593 : : /* the following are InChI library-specific parameters */
594 : : inp_ATOM** at,
595 : : int* num_at,
596 : : int* num_bonds,
597 : : OAD_Polymer** polymer,
598 : : OAD_V3000** v3000,
599 : : /* end of InChI library-specific parameters */
600 : : char* szMsg,
601 : : int nMsgLen,
602 : : unsigned long WarningFlags[2][2])
603 : : {
604 : : InpInChI OneInput;
605 : 28 : char* strHdr = NULL;
606 : 28 : char* szCurHdr = NULL;
607 : : int num_components[INCHI_NUM];
608 [ + + ]: 30 : int bReqNonTaut = (0 != ((ip_inp->nMode & REQ_MODE_BASIC) &&
609 [ + - ]: 2 : (ip_inp->nMode & REQ_MODE_TAUT)));
610 : : /*
611 : : int bReqRecmet = (0 != ((ip->bTautFlags & TG_FLAG_RECONNECT_COORD) &&
612 : : (ip->bTautFlags & TG_FLAG_DISCONNECT_COORD)));
613 : : */
614 : 28 : int bReqStereo = (0 != (ip_inp->nMode & REQ_MODE_STEREO));
615 : 28 : int bHasSomeReconnected = 0, bHasSomeFixedH = 0, bHasMetal = 0;
616 : 28 : int nModeFlagsStereo = 0, bTautFlags = 0; /* InChI creation flags modifications derived from current InChI */
617 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM];
618 : :
619 : : NORM_CANON_FLAGS ncFlags;
620 : 28 : NORM_CANON_FLAGS* pncFlags = &ncFlags;
621 : : INPUT_PARMS ip_cur, * ip;
622 : : STRUCT_DATA sd_cur, * sd;
623 : :
624 : : int pState; /* djb-rwth: removing redundant variables */
625 : 28 : int bReqProtonsForEachComponent = 0;
626 : 28 : int bReqSplitOutputInChI = 0;
627 : : SEGM_LINE Line;
628 : 28 : SEGM_LINE* pLine = &Line;
629 : 28 : long ulProcessingTime = 0;
630 : : inchiTime ulTStart;
631 : 28 : long num_processed = 0, num_errors = 0;
632 : : int bPlainTabbedOutput; /* djb-rwth: ignoring LLVM warning: variable used */
633 : : const char* pTAB; /* djb-rwth: ignoring LLVM warning: variable used */
634 : :
635 : 28 : long num_inp = 0;
636 : :
637 : 28 : int read_inchi_ok = 0;
638 : 28 : int end_of_data_reached = 0;
639 : 28 : int treat_save_opt = 0;
640 : 28 : int input_is_stdinchi = 0;
641 : 28 : int input_has_save_opt = 0;
642 : 28 : unsigned char input_save_opt_bits = 0;
643 : 28 : unsigned char save_opt_bits = 0;
644 : :
645 : 28 : const int bInChI2Structure = 0 != (ip_inp->bReadInChIOptions & READ_INCHI_TO_STRUCTURE);
646 : 28 : const int bInChI2InChI = 0 != (ip_inp->bReadInChIOptions & READ_INCHI_OUTPUT_INCHI);
647 : 28 : const int bReadCoord = bInChI2Structure;
648 : :
649 : 28 : int nMessageLen = MAX_MSG_LEN;
650 : : char szMessage[MAX_MSG_LEN];
651 : : int nInitLenMessage;
652 : 28 : int invalid_opt = 0;
653 : : int j, nErr, iINChI;
654 : :
655 : 28 : int treat_mismatch_as_error = ip_inp->bINChIOutputOptions2 & INCHI_OUT_MISMATCH_AS_ERROR;
656 : 28 : int output_error_inchi = ip_inp->bINChIOutputOptions2 & INCHI_OUT_INCHI_GEN_ERROR;
657 : :
658 : 28 : int ret = 0;
659 : 28 : int nReadStatus = RI_ERR_EOL;
660 : :
661 : 28 : INCHI_IOSTREAM* pRealOut = pOut;
662 : : /* temporary output buffer pTmpOut may be used */
663 : : /* locally instead of legal pOut to capture */
664 : : /* InChI string which may then be recognised */
665 : : /* as erratic (whence should not be finally */
666 : : /* printed) */
667 : : INCHI_IOSTREAM tmpoutputstr;
668 : 28 : INCHI_IOSTREAM* pTmpOut = &tmpoutputstr;
669 [ + + ]: 28 : if (bInChI2Structure)
670 : : {
671 : 4 : inchi_ios_init(pTmpOut, INCHI_IOS_TYPE_STRING, NULL);
672 [ - + ]: 4 : if (pTmpOut->s.pStr)
673 : 0 : pRealOut = pTmpOut;
674 : : }
675 : :
676 : 28 : memset(szMessage, 0, sizeof(szMessage)); /* djb-rwth: memset_s C11/Annex K variant? */
677 : 28 : memset(&OneInput, 0, sizeof(OneInput)); /* djb-rwth: memset_s C11/Annex K variant? */
678 : 28 : memset(pLine, 0, sizeof(pLine[0])); /* djb-rwth: memset_s C11/Annex K variant? */
679 [ + - ]: 28 : if (szMsg)
680 : 28 : szMsg[0] = '\0';
681 : :
682 : 28 : OneInput.polymer = NULL; /* v. 1.05 added */
683 : 28 : OneInput.v3000 = NULL;
684 : :
685 : :
686 : : /* Read input, InChI by InChI*/
687 [ + - ]: 28 : while (nReadStatus != RI_ERR_EOF)
688 : : {
689 [ + + ]: 84 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
690 : : {
691 [ + + ]: 168 : for (j = 0; j < TAUT_NUM; j++)
692 : : {
693 [ - + ]: 112 : if (OneInput.nNumProtons[iINChI][j].pNumProtons)
694 : : {
695 [ # # ]: 0 : inchi_free(OneInput.nNumProtons[iINChI][j].pNumProtons);
696 : 0 : OneInput.nNumProtons[iINChI][j].pNumProtons = NULL;
697 : : }
698 : : }
699 : : }
700 : :
701 : 28 : memset(&OneInput, 0, sizeof(OneInput)); /* djb-rwth: memset_s C11/Annex K variant? */
702 : 28 : memset(pncFlags, 0, sizeof(*pncFlags)); /* djb-rwth: memset_s C11/Annex K variant? */
703 : : /* djb-rwth: removing redundant code */
704 : 28 : ip_cur = *ip_inp;
705 : 28 : ip = &ip_cur;
706 : 28 : sd_cur = *sd_inp;
707 : 28 : sd = &sd_cur;
708 : :
709 : 28 : bReqSplitOutputInChI =
710 : 28 : 0 != (ip->bReadInChIOptions & READ_INCHI_SPLIT_OUTPUT);
711 : 28 : bReqProtonsForEachComponent =
712 [ - + ]: 28 : bReqSplitOutputInChI &&
713 [ # # ]: 0 : 0 != (READ_INCHI_KEEP_BALANCE_P & ip->bReadInChIOptions);
714 : 28 : bPlainTabbedOutput =
715 : 28 : 0 != (ip->bINChIOutputOptions & INCHI_OUT_TABBED_OUTPUT);
716 : :
717 : 28 : pTAB =
718 : : #if ( defined(TARGET_API_LIB) || defined(TARGET_LIB_FOR_WINCHI) )
719 : : "\n";
720 : : #else
721 : : bPlainTabbedOutput ? "\t" : "\n";
722 : : #endif
723 : :
724 : :
725 [ + + ]: 28 : if (bInChI2Structure)
726 : : {
727 : 4 : bReqStereo = 1;
728 : 4 : bReqSplitOutputInChI = 1;
729 : 4 : bReqProtonsForEachComponent = bReqNonTaut;
730 : 4 : ip->bTautFlags |= (TG_FLAG_DISCONNECT_COORD | TG_FLAG_RECONNECT_COORD);
731 : 4 : ip->nMode |= (REQ_MODE_BASIC | REQ_MODE_TAUT | REQ_MODE_STEREO | REQ_MODE_ISO_STEREO | REQ_MODE_ISO);
732 : : /* bReqRecmet = 1; */
733 : : #if ( bRELEASE_VERSION == 1 )
734 : 4 : bReqNonTaut = 1; /* bReqNonTaut=0 ignores Fixed-H layer in input InChI, for testing only */
735 : : #endif
736 : : /* polymer stuff added */
737 [ + - ]: 4 : if (pInp->type == INCHI_IOS_TYPE_STRING)
738 : : {
739 : : int res; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
740 : :
741 [ - + ]: 4 : if (ip_inp->lMolfileNumber)
742 : : /* get here from inchi-1 main emulation mode */
743 : 0 : num_inp = ip_inp->lMolfileNumber - 1;
744 : :
745 : 4 : res = DetectAndExposePolymerInternals(pInp); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
746 : : /* proceed silently for now, errs mist be uncovered further on conversion */
747 : : }
748 : : }
749 : :
750 : 28 : InchiTimeGet(&ulTStart);
751 : :
752 : : /* Read InChI string */
753 : 28 : nReadStatus = InChILine2Data(pInp, pLine, &strHdr, &pState, &nErr,
754 : : OneInput.pInpInChI, OneInput.nNumComponents,
755 : : OneInput.nNumProtons, OneInput.s,
756 : : bReadCoord, bInChI2Structure, ip_inp->nMode,
757 : : &input_is_stdinchi, &input_has_save_opt,
758 : : &input_save_opt_bits,
759 : : &OneInput.polymer, &OneInput.v3000);
760 : :
761 : 28 : ulProcessingTime += InchiTimeElapsed(ic, &ulTStart);
762 : :
763 : : #if 0
764 : : if (!bInChI2Structure && OneInput.polymer)
765 : : {
766 : : /* inchi2inchi for polymers: for now skip */
767 : : pState == IST_MOBILE_H_POLYMER;
768 : : nReadStatus = -2;
769 : : }
770 : : #endif
771 : :
772 : :
773 [ + - + + ]: 28 : end_of_data_reached = nReadStatus == RI_ERR_EOL || nReadStatus == RI_ERR_EOF;
774 : :
775 [ + + ]: 28 : if (!bInChI2Structure)
776 : : {
777 : : /* i2i, disable polymer related */
778 [ - + ]: 24 : if (OneInput.polymer)
779 : : {
780 : 0 : pState = IST_MOBILE_H_POLYMER;
781 : 0 : nErr = RI_ERR_PROGR;
782 : : }
783 : : }
784 : :
785 [ + + + - ]: 28 : read_inchi_ok = end_of_data_reached && !nErr;
786 : :
787 : : /* then uncommented... commented out 2020-07-10 (potentially dangerous change, needs testing (TODO: i2s for InChI=1// )*/
788 [ + + ]: 47 : read_inchi_ok = read_inchi_ok &&
789 [ + - ]: 19 : (OneInput.nNumComponents[INCHI_BAS][TAUT_YES] + OneInput.nNumComponents[INCHI_BAS][TAUT_NON]);
790 : :
791 : : #if ALLOW_EMPTY_INCHI_AS_INPUT!=1
792 : : /* no empty InChI allowed */
793 : : read_inchi_ok = read_inchi_ok &&
794 : : (OneInput.nNumComponents[INCHI_BAS][TAUT_YES] +
795 : : OneInput.nNumComponents[INCHI_BAS][TAUT_NON]);
796 : : #endif
797 : : #ifdef TARGET_EXE_STANDALONE
798 : : /* inchi-1: we currently disable conversion of polymeric InChI (inchi2struct)
799 : : to anything but SDF (would it be necessary in the future?) */
800 : : if (OneInput.polymer &&
801 : : bInChI2Structure &&
802 : : !(ip_inp->bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY))
803 : : {
804 : : read_inchi_ok = 0;
805 : : nErr = RI_ERR_PROGR;
806 : : }
807 : : #endif
808 : :
809 [ + + ]: 28 : if (!read_inchi_ok)
810 : : {
811 : 9 : TreatErrorsInReadInChIString(nReadStatus, nErr, pState, ip_inp, pRealOut, pLog,
812 : : &num_inp, &num_errors, &num_processed,
813 : : &strHdr, &szCurHdr, &OneInput);
814 [ - + - - ]: 9 : if (nReadStatus == RI_ERR_SYNTAX || nReadStatus == RI_ERR_PROGR)
815 : : {
816 : 9 : ret = nReadStatus;
817 : : }
818 : : }
819 : : else
820 : : {
821 : : /* InChI has been successfully read */
822 : 19 : ret = 0;
823 : 19 : num_inp++;
824 : 19 : ip->lMolfileNumber = num_inp;
825 : 19 : bHasSomeReconnected = 0;
826 : 19 : bHasSomeFixedH = 0;
827 : :
828 [ - + ]: 19 : if (pRealOut == pTmpOut)
829 : : {
830 : 0 : inchi_ios_reset(pRealOut);
831 : : /*inchi_ios_close( pRealOut );
832 : : inchi_ios_init( pRealOut, INCHI_IOS_TYPE_STRING, NULL );*/
833 : : }
834 : : /* inchi_ios_print( pRealOut, ""); */
835 : :
836 : : /* Does not allow conversion of a non-standard input to Standard output. */
837 [ + + ]: 19 : if (ip_inp->bINChIOutputOptions & INCHI_OUT_STDINCHI)
838 : : {
839 [ + + ]: 14 : if ((input_is_stdinchi & INCHI_INPUT_CLASS_MASK) ==
840 : : INCHI_INPUT_CLASS_NONSTANDARD)
841 : : {
842 : : /* Input InChI is non-standard. */
843 : 3 : ip->bINChIOutputOptions &= ~INCHI_OUT_STDINCHI;
844 : : /*
845 : : if (szCurHdr && szCurHdr[0])
846 : : inchi_ios_eprint( pLog, "Warning: forced conversion to non-standard InChI for non-std input, %s\n", szCurHdr );
847 : : else
848 : : inchi_ios_eprint( pLog, "Warning: forced conversion to non-standard InChI for non-std input, Structure %ld\n", num_inp );
849 : : */
850 : : }
851 : : }
852 : :
853 : : /*
854 : : * Preserve the input calculation-class designator and subversion
855 : : * designator when an InChI is converted or restored to a structure.
856 : : */
857 [ + + + - ]: 19 : if (bInChI2InChI || bInChI2Structure)
858 : : {
859 : 19 : ip->bReadInChIOptions &= ~(READ_INCHI_INPUT_BETA | READ_INCHI_INPUT_MI);
860 : :
861 [ + + ]: 19 : if ((input_is_stdinchi & INCHI_INPUT_CLASS_MASK) == INCHI_INPUT_CLASS_BETA)
862 : : {
863 : 4 : ip->bReadInChIOptions |= READ_INCHI_INPUT_BETA;
864 : : }
865 : :
866 [ + + ]: 19 : if (input_is_stdinchi & INCHI_INPUT_MI_MARKER)
867 : : {
868 : 6 : ip->bReadInChIOptions |= READ_INCHI_INPUT_MI;
869 : : }
870 : : }
871 : :
872 : 19 : treat_save_opt = ip->bINChIOutputOptions & INCHI_OUT_SAVEOPT;
873 : :
874 [ - + ]: 19 : if (treat_save_opt)
875 : : {
876 : 0 : PrepareSaveOptBits(ip, pLog, num_inp, szCurHdr,
877 : : input_has_save_opt,
878 : : input_save_opt_bits,
879 : : &save_opt_bits);
880 : : }
881 : :
882 : : #ifndef TARGET_API_LIB
883 : : /*
884 : : inchi_ios_eprint(stderr, "%ld: %s\r", num_inp, strHdr? strHdr : "");
885 : : inchi_ios_eprint(pLog, "%ld: %s\n", num_inp, strHdr? strHdr : "");
886 : : */
887 : :
888 : : if (!ip->bNoStructLabels &&
889 : : !(bInChI2Structure && (ip->bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY))
890 : : )
891 : : {
892 : : /* Added 2nd item: Do not output this extra line into the output SDfile. 2008-11-17 DCh */
893 : : if (strHdr && strstr(strHdr, "Structure:"))
894 : : {
895 : : inchi_ios_print(pRealOut, "%s%s", strHdr, pTAB); /* output header */
896 : : #if ( FIX_DALKE_BUGS == 1 )
897 : : #else
898 : : sprintf(szMessage, "%s (%ld)", strHdr ? strHdr : "", num_inp);
899 : : #endif
900 : : }
901 : : else
902 : : {
903 : : if (bInChI2Structure) inchi_ios_print(pRealOut, "Structure: %ld. (%s)%s",
904 : : num_inp, strHdr ? strHdr : "No struct name",
905 : : pTAB); /* output header */
906 : : #if ( FIX_DALKE_BUGS == 1 )
907 : : #else
908 : : sprintf(szMessage, "Structure: %ld. (%s)%s", num_inp, strHdr ? strHdr : "No struct name", pTAB);
909 : : #endif
910 : : }
911 : : if (strHdr && strHdr[0])
912 : : {
913 : : strncpy(ip->szSdfDataHeader, strHdr, sizeof(ip->szSdfDataHeader));
914 : : ip->szSdfDataHeader[sizeof(ip->szSdfDataHeader) - 1] = '\0';
915 : : ip->pSdfLabel = NULL;
916 : : ip->pSdfValue = ip->szSdfDataHeader;
917 : : }
918 : : else
919 : : {
920 : : ip->pSdfValue = NULL;
921 : : ip->szSdfDataHeader[0] = '\0';
922 : : }
923 : : }
924 : :
925 : : #if ( FIX_DALKE_BUGS == 1 )
926 : : sprintf(szMessage, "%ld: %.400s", num_inp, strHdr ? strHdr : "");
927 : : #else
928 : : sprintf(szMessage, "%ld: %s", num_inp, strHdr ? strHdr : "");
929 : : #endif
930 : : #endif
931 : :
932 : 19 : nInitLenMessage = (int)strlen(szMessage);
933 [ - + ]: 19 : if (strHdr)
934 : : {
935 : 0 : szCurHdr = strHdr;
936 : 0 : strHdr = NULL;
937 : : }
938 [ - + - - : 19 : if (szCurHdr && ip && ip->first_struct_number > 0)
- - ]
939 : : {
940 : : /* Check whether the structure should be skipped */
941 : : static const char szStruct[] = "Structure:";
942 : 0 : char* pStrNum = strstr(szCurHdr, szStruct);
943 : : long cur_struct_number;
944 [ # # ]: 0 : if (pStrNum)
945 : : {
946 : 0 : pStrNum += sizeof(szStruct) - 1; /* -1 takes care of the string terminal zero */
947 : 0 : cur_struct_number = inchi_strtol(pStrNum, NULL, 10);
948 [ # # ]: 0 : if (cur_struct_number)
949 : : {
950 : 0 : OneInput.num_inp = cur_struct_number;
951 : : }
952 : : /* Process request to bypass first several InChIs */
953 [ # # # # ]: 0 : if (cur_struct_number > 0 && cur_struct_number < ip->first_struct_number)
954 : : {
955 : :
956 : : #if ( !defined(TARGET_API_LIB) && !defined(TARGET_EXE_STANDALONE) )
957 : : inchi_fprintf(stderr, "Skipping %s\r", szMessage);
958 : : #endif
959 : 0 : FreeInpInChI(&OneInput);
960 [ # # ]: 0 : if (szCurHdr)
961 : : {
962 [ # # ]: 0 : inchi_free(szCurHdr);
963 : 0 : szCurHdr = NULL;
964 : : }
965 : : INCHI_HEAPCHK
966 : 0 : continue;
967 : : }
968 : : }
969 : : }
970 : :
971 : 19 : num_processed++;
972 : :
973 : : /* In case of splitting InChI into separate components */
974 : : /* decide whether to keep /p in each component or */
975 : : /* output /p and /i/h as a separate component */
976 : : /* Note: if InChI is not to be splitted DO NOT create */
977 : : /* a separate component for /p, /i/h: it would be a bug*/
978 : :
979 : 19 : InchiTimeGet(&ulTStart);
980 : :
981 : : INCHI_HEAPCHK
982 : :
983 : 19 : ret = SetProtonsAndXchgIsoH(bInChI2Structure, bReqSplitOutputInChI,
984 : : bReqProtonsForEachComponent, bReqNonTaut, bReqStereo,
985 : : num_components, nModeProtonIsoExchgH, &OneInput);
986 : : INCHI_HEAPCHK
987 : :
988 [ - + ]: 19 : if (ret < 0)
989 : : {
990 : 0 : num_errors++;
991 : 0 : goto exit_error;
992 : : }
993 : :
994 : 19 : sd->num_components[INCHI_BAS] = num_components[INCHI_BAS];
995 : 19 : sd->num_components[INCHI_REC] = num_components[INCHI_REC];
996 : :
997 : : /* Do we have reconnected InChI ? */
998 [ + - ]: 19 : if ((OneInput.nNumComponents[INCHI_REC][TAUT_YES] ||
999 [ - + ]: 19 : OneInput.nNumComponents[INCHI_REC][TAUT_NON]) &&
1000 [ # # ]: 0 : (ip->bTautFlags & TG_FLAG_RECONNECT_COORD) &&
1001 [ # # ]: 0 : (ip->bTautFlags & TG_FLAG_DISCONNECT_COORD)
1002 : : )
1003 : : {
1004 : : /* needed for InChI string output to include reconnected InChI */
1005 : 0 : sd->bTautFlagsDone[0] |= TG_FLAG_DISCONNECT_COORD_DONE;
1006 : 0 : bHasSomeReconnected = 1;
1007 : : }
1008 : :
1009 : : /* Do we have fixed H InChI ? */
1010 [ + + ]: 19 : if (bReqNonTaut &&
1011 : : /*OneInput.nNumComponents[bHasSomeReconnected?INCHI_REC:INCHI_BAS][TAUT_NON]*/
1012 [ + - ]: 5 : (OneInput.nNumComponents[INCHI_REC][TAUT_NON] ||
1013 [ - + ]: 5 : OneInput.nNumComponents[INCHI_BAS][TAUT_NON]))
1014 : : {
1015 : 0 : bHasSomeFixedH = 1;
1016 : : }
1017 : :
1018 : 19 : ulProcessingTime += InchiTimeElapsed(ic, &ulTStart);
1019 : :
1020 [ + + + - : 22 : invalid_opt = (!bInChI2Structure && !bInChI2InChI) ||
+ + ]
1021 [ - + ]: 3 : (bInChI2Structure && bInChI2InChI);
1022 : :
1023 [ - + ]: 19 : if (invalid_opt)
1024 : : {
1025 : 0 : inchi_ios_eprint(pLog, "\nWrong command line options: expected Inch2Struct or Inchi2Inchi\n", num_inp);
1026 : 0 : break;
1027 : : }
1028 : :
1029 : : /* InChI --> Structure */
1030 [ + + ]: 19 : if (bInChI2Structure)
1031 : : {
1032 : : char* result_string;
1033 : :
1034 [ + + + - ]: 3 : if (OneInput.polymer && OneInput.polymer->n)
1035 : : {
1036 : 1 : OneInput.valid_polymer = 1;
1037 : : }
1038 : :
1039 : 3 : ret = ConvertInChI2Struct(ip_inp, ip, &OneInput,
1040 : : at, num_at,
1041 : : polymer, v3000,
1042 : : pRealOut, pLog,
1043 : : sd, num_components, nModeProtonIsoExchgH,
1044 : : &szCurHdr, szMsg, nMsgLen, szMessage,
1045 : : nInitLenMessage, nMessageLen,
1046 : : input_is_stdinchi, bHasSomeReconnected,
1047 : : bHasSomeFixedH, bHasMetal,
1048 : : nModeFlagsStereo, bTautFlags, bReqNonTaut,
1049 : : WarningFlags, num_inp, &num_errors,
1050 : : save_opt_bits, &ulTStart, &ulProcessingTime,
1051 : : ic, pCG);
1052 : :
1053 : 3 : result_string = pRealOut->s.pStr;
1054 [ - + ]: 3 : if (ret == 0) /* no problems */
1055 : : {
1056 : : ;
1057 : : }
1058 [ # # ]: 0 : else if (ret > 0)
1059 : : {
1060 : : /* error */
1061 [ # # ]: 0 : if (output_error_inchi)
1062 : : {
1063 : : /* emit error string */
1064 : 0 : inchi_ios_eprint(pRealOut, "InChI creation Error!\n");
1065 : : }
1066 : : }
1067 [ # # ]: 0 : else if (ret < 0)
1068 : : {
1069 : : /* error or a mismatch possibly treated as error */
1070 [ # # ]: 0 : if (ret != RI_ERR_MISMATCH)
1071 : : {
1072 : 0 : goto exit_error;
1073 : : }
1074 : : else
1075 : : {
1076 [ # # ]: 0 : if (!treat_mismatch_as_error)
1077 : : {
1078 : : /* ignore mismatch, print InChI as usual */
1079 [ # # ]: 0 : if (result_string)
1080 : : {
1081 : 0 : inchi_ios_eprint(pRealOut, "%-s\n", result_string);
1082 : : }
1083 : : }
1084 : : else
1085 : : {
1086 : : /* concider mismatch an error */
1087 [ # # ]: 0 : if (!output_error_inchi)
1088 : : {
1089 : : ; /* print nothing for now*/
1090 : : }
1091 : : else
1092 : : {
1093 : : /* emit err string instead of InChI */
1094 [ # # ]: 0 : if (result_string)
1095 : : {
1096 : : /* try to preserve header if any */
1097 : 0 : char* pi = strstr(result_string, "InChI=");
1098 [ # # ]: 0 : if (pi)
1099 : : {
1100 : 0 : int np = pi - result_string;
1101 [ # # ]: 0 : if (np)
1102 : 0 : result_string[np - 1] = '\0';
1103 : 0 : inchi_ios_eprint(pRealOut, "%-s\n", result_string);
1104 : : }
1105 : : }
1106 : 0 : inchi_ios_eprint(pRealOut, "InChICreationError!\n"); /* emit err string */
1107 : : }
1108 : : }
1109 : 0 : FreeInpInChI(&OneInput);
1110 [ # # ]: 0 : if (strHdr)
1111 : : {
1112 [ # # ]: 0 : inchi_free(strHdr);
1113 : 0 : strHdr = NULL;
1114 : : }
1115 [ # # ]: 0 : if (szCurHdr)
1116 : : {
1117 [ # # ]: 0 : inchi_free(szCurHdr);
1118 : 0 : szCurHdr = NULL;
1119 : : }
1120 : : INCHI_HEAPCHK
1121 : 0 : continue;
1122 : : }
1123 : : }
1124 : : } /* ( bInChI2Structure ) */
1125 : :
1126 : :
1127 : : /* InChI --> InChI */
1128 [ + - ]: 16 : else if (bInChI2InChI)
1129 : : {
1130 : 16 : ret = ConvertInChI2InChI(ip, &OneInput, pRealOut, pLog, sd,
1131 : : num_components, nModeProtonIsoExchgH,
1132 : : &szCurHdr, num_inp, &num_errors,
1133 : : save_opt_bits, &ulTStart,
1134 : : &ulProcessingTime, ic, pCG);
1135 : : }
1136 : :
1137 [ + - ]: 19 : if (nReadStatus == RI_ERR_EOF)
1138 : : #if ( FIX_ONE_LINE_INCHI_INPUT_CONVERSION_ISSUE==1 && defined(TARGET_EXE_STANDALONE) && !defined(TARGET_API_LIB) )
1139 : : {
1140 : : inchi_ios_flush(pRealOut);
1141 : : inchi_ios_flush2(pLog, stderr);
1142 : : break;
1143 : : }
1144 : : #else
1145 : 19 : break;
1146 : : #endif
1147 : : } /* InChI has been successfully read */
1148 : :
1149 : :
1150 : : #ifdef TARGET_EXE_STANDALONE
1151 : : #ifndef TARGET_API_LIB
1152 : : inchi_ios_flush(pRealOut);
1153 : : inchi_ios_flush2(pLog, stderr);
1154 : : #endif
1155 : : #endif
1156 : : #ifdef TARGET_API_LIB
1157 : 9 : break; /* exit after the 1st structure */
1158 : : #endif
1159 : : } /* while */
1160 : :
1161 : 0 : exit_error:
1162 : 28 : FreeInpInChI(&OneInput);
1163 [ - + ]: 28 : if (strHdr)
1164 : : {
1165 [ # # ]: 0 : inchi_free(strHdr);
1166 : 0 : strHdr = NULL;
1167 : : }
1168 [ + - ]: 28 : if (pLine->str)
1169 : : {
1170 [ + - ]: 28 : inchi_free(pLine->str);
1171 : : }
1172 [ - + ]: 28 : if (szCurHdr)
1173 : : {
1174 [ # # ]: 0 : inchi_free(szCurHdr);
1175 : 0 : szCurHdr = NULL;
1176 : : }
1177 : :
1178 : : INCHI_HEAPCHK
1179 : :
1180 [ + - ]: 28 : if (sd_inp)
1181 : : {
1182 : 28 : sd_inp->ulStructTime = ulProcessingTime;
1183 : 28 : sd_inp->fPtrStart = num_processed;
1184 : 28 : sd_inp->fPtrEnd = num_errors;
1185 : : }
1186 [ - + ]: 28 : if (pRealOut == pTmpOut)
1187 : : {
1188 : 0 : inchi_ios_close(pTmpOut);
1189 : : }
1190 : :
1191 : 28 : return ret;
1192 : : }
1193 : :
1194 : :
1195 : : /****************************************************************************/
1196 : 13 : int OutputInChIAsRequested(struct tagCANON_GLOBALS* pCG,
1197 : : INCHI_IOSTREAM* pOut,
1198 : : INCHI_IOSTREAM* pLog,
1199 : : ICHICONST INPUT_PARMS* ip_inp,
1200 : : STRUCT_DATA* sd_inp,
1201 : : InpInChI* OneInput,
1202 : : int num_components[INCHI_NUM],
1203 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
1204 : : long num_inp,
1205 : : unsigned char save_opt_bits)
1206 : : {
1207 : 13 : int j, k, k1, k2, ret2 = 0, iINChI, iINChI1; /* djb-rwth: removing redundant variables */
1208 : : PINChI2* pINChI[INCHI_NUM], * newPTR1;
1209 : : PINChI_Aux2* pINChI_Aux[INCHI_NUM], * newPTR2;
1210 : : int bReqNonTaut;
1211 : : int bHasSomeReconnected;
1212 : :
1213 : : INPUT_PARMS ip_local;
1214 : : STRUCT_DATA sd_local;
1215 : 13 : INPUT_PARMS* ip = &ip_local;
1216 : 13 : STRUCT_DATA* sd = &sd_local;
1217 : : NORM_CANON_FLAGS ncFlags;
1218 : 13 : NORM_CANON_FLAGS* pncFlags = &ncFlags;
1219 : : int nRet1, bSortPrintINChIFlags;
1220 : : int bReqSplitOutputInChI;
1221 : : int nNumOutputComponents;
1222 : :
1223 : : INCHI_IOS_STRING temp_string_container;
1224 : 13 : INCHI_IOS_STRING* strbuf = &temp_string_container;
1225 : 13 : memset(strbuf, 0, sizeof(*strbuf)); /* djb-rwth: memset_s C11/Annex K variant? */
1226 : :
1227 [ - + ]: 13 : if (0 >= inchi_strbuf_init(strbuf, INCHI_STRBUF_INITIAL_SIZE, INCHI_STRBUF_SIZE_INCREMENT))
1228 : : {
1229 : 0 : ret2 = RI_ERR_ALLOC;
1230 : 0 : goto exit_error;
1231 : : }
1232 : :
1233 : 13 : nRet1 = 0;
1234 : 13 : k1 = k2 = 0;
1235 : 13 : memset(pncFlags, 0, sizeof(*pncFlags)); /* djb-rwth: memset_s C11/Annex K variant? */
1236 : 13 : memset(pINChI, 0, sizeof(pINChI)); /* djb-rwth: memset_s C11/Annex K variant? */
1237 : 13 : memset(pINChI_Aux, 0, sizeof(pINChI_Aux)); /* djb-rwth: memset_s C11/Annex K variant? */
1238 : :
1239 : 13 : *ip = *ip_inp;
1240 : 13 : *sd = *sd_inp;
1241 : 13 : bHasSomeReconnected = 0;
1242 : 13 : bSortPrintINChIFlags = 0;
1243 : : /* djb-rwth: removing redundant code */
1244 : 13 : bReqNonTaut = (0 != (ip->nMode & REQ_MODE_BASIC));
1245 : 13 : bReqSplitOutputInChI = (0 != (ip->bReadInChIOptions & READ_INCHI_SPLIT_OUTPUT));
1246 : :
1247 : : INCHI_HEAPCHK
1248 : :
1249 [ + - ]: 13 : if (num_components[INCHI_BAS])
1250 : : {
1251 : : /* djb-rwth: MYREALLOC2( PINChI2, PINChI_Aux2, pINChI[INCHI_BAS], pINChI_Aux[INCHI_BAS], num_components[INCHI_BAS], (long long)num_components[INCHI_BAS], k1 ); has been replaced and the whole block rewritten to address memory leaks and reading from freed memory locations */
1252 : :
1253 : : do {
1254 : : if ((num_components[INCHI_BAS]) <= ((long long)num_components[INCHI_BAS]))
1255 : : {
1256 : 13 : newPTR1 = (PINChI2*)inchi_calloc(((long long)num_components[INCHI_BAS]) + 1, sizeof(PINChI2));
1257 : 13 : newPTR2 = (PINChI_Aux2*)inchi_calloc(((long long)num_components[INCHI_BAS]) + 1, sizeof(PINChI_Aux2));
1258 [ + - + - ]: 13 : if (newPTR1 && newPTR2) {
1259 [ - + - - ]: 13 : if ((pINChI[INCHI_BAS]) && (num_components[INCHI_BAS]) > 0)
1260 : 0 : memcpy(newPTR1, (pINChI[INCHI_BAS]), (num_components[INCHI_BAS]) * sizeof(PINChI2));
1261 [ - + - - ]: 13 : if ((pINChI_Aux[INCHI_BAS]) && (num_components[INCHI_BAS]) > 0)
1262 : 0 : memcpy(newPTR2, (pINChI_Aux[INCHI_BAS]), (num_components[INCHI_BAS]) * sizeof(PINChI_Aux2));
1263 [ - + ]: 13 : if (pINChI[INCHI_BAS])
1264 [ # # ]: 0 : inchi_free(pINChI[INCHI_BAS]);
1265 [ - + ]: 13 : if (pINChI_Aux[INCHI_BAS])
1266 [ # # ]: 0 : inchi_free(pINChI_Aux[INCHI_BAS]);
1267 : 13 : pINChI[INCHI_BAS] = newPTR1;
1268 : 13 : pINChI_Aux[INCHI_BAS] = newPTR2;
1269 : 13 : num_components[INCHI_BAS] = (long long)num_components[INCHI_BAS];
1270 : 13 : k1 = 0;
1271 : : }
1272 : : else
1273 : : {
1274 [ # # ]: 0 : inchi_free(newPTR1);
1275 [ # # ]: 0 : inchi_free(newPTR2);
1276 : 0 : k1 = 1;
1277 : : }
1278 : : }
1279 : : else { k1 = 0; }
1280 : : } while (0);
1281 : : }
1282 : :
1283 [ - + ]: 13 : if (num_components[INCHI_REC])
1284 : : {
1285 : : /* djb-rwth: MYREALLOC2( PINChI2, PINChI_Aux2, pINChI[INCHI_REC], pINChI_Aux[INCHI_REC], num_components[INCHI_REC], (long long)num_components[INCHI_REC], k2 ); has been replaced and the whole block rewritten to address memory leaks and reading from freed memory locations */
1286 : :
1287 : : do {
1288 : : if ((num_components[INCHI_REC]) <= ((long long)num_components[INCHI_REC]))
1289 : : {
1290 : 0 : newPTR1 = (PINChI2*)inchi_calloc(((long long)num_components[INCHI_REC]) + 1, sizeof(PINChI2));
1291 : 0 : newPTR2 = (PINChI_Aux2*)inchi_calloc(((long long)num_components[INCHI_REC]) + 1, sizeof(PINChI_Aux2));
1292 [ # # # # ]: 0 : if (newPTR1 && newPTR2) {
1293 [ # # # # ]: 0 : if ((pINChI[INCHI_REC]) && (num_components[INCHI_REC]) > 0)
1294 : 0 : memcpy(newPTR1, (pINChI[INCHI_REC]), (num_components[INCHI_REC]) * sizeof(PINChI2));
1295 [ # # # # ]: 0 : if ((pINChI_Aux[INCHI_REC]) && (num_components[INCHI_REC]) > 0)
1296 : 0 : memcpy(newPTR2, (pINChI_Aux[INCHI_REC]), (num_components[INCHI_REC]) * sizeof(PINChI_Aux2));
1297 [ # # ]: 0 : if (pINChI[INCHI_REC])
1298 [ # # ]: 0 : inchi_free(pINChI[INCHI_REC]);
1299 [ # # ]: 0 : if (pINChI_Aux[INCHI_REC])
1300 [ # # ]: 0 : inchi_free(pINChI_Aux[INCHI_REC]);
1301 : 0 : pINChI[INCHI_REC] = newPTR1;
1302 : 0 : pINChI_Aux[INCHI_REC] = newPTR2;
1303 : 0 : num_components[INCHI_REC] = (long long)num_components[INCHI_REC];
1304 : 0 : k2 = 0;
1305 : : }
1306 : : else
1307 : : {
1308 [ # # ]: 0 : inchi_free(newPTR1);
1309 [ # # ]: 0 : inchi_free(newPTR2);
1310 : 0 : k2 = 1;
1311 : : }
1312 : : }
1313 : : else { k2 = 0; }
1314 : : } while (0);
1315 : : }
1316 : :
1317 : :
1318 : : INCHI_HEAPCHK
1319 : :
1320 [ + - - + ]: 13 : if (k1 || k2 /*|| !pStr*/)
1321 : : {
1322 : : /* djb-rwth: avoiding memory leak */
1323 : 0 : free(pINChI[INCHI_BAS]);
1324 : 0 : free(pINChI_Aux[INCHI_BAS]);
1325 : 0 : free(pINChI[INCHI_REC]);
1326 : 0 : free(pINChI_Aux[INCHI_REC]);
1327 : 0 : ret2 = RI_ERR_ALLOC;
1328 : 0 : goto exit_error;
1329 : : }
1330 : :
1331 [ - + ]: 13 : if (num_components[INCHI_REC] &&
1332 [ # # ]: 0 : (ip->bTautFlags & TG_FLAG_RECONNECT_COORD) &&
1333 [ # # ]: 0 : (ip->bTautFlags & TG_FLAG_DISCONNECT_COORD))
1334 : : {
1335 : 0 : sd->bTautFlagsDone[0] |= TG_FLAG_DISCONNECT_COORD_DONE;
1336 : 0 : bHasSomeReconnected = 1;
1337 : : }
1338 : :
1339 [ + + ]: 39 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
1340 : : {
1341 [ + + ]: 78 : for (j = 0; j < TAUT_NUM; j++)
1342 : : {
1343 [ + + + + : 52 : if (bReqNonTaut || (j != TAUT_NON && OneInput->pInpInChI[iINChI][j])) /* djb-rwth: addressing LLVM warning */
+ + ]
1344 : : {
1345 [ + + ]: 37 : for (k = 0; k < num_components[iINChI]; k++)
1346 : : {
1347 : : /* allocate InChI & AuxInfo */
1348 [ - + ]: 18 : if (!(pINChI[iINChI][k][j] = (INChI*)inchi_calloc(1, sizeof(INChI))))
1349 : : {
1350 : : /* djb-rwth: avoiding memory leak */
1351 : 0 : free(pINChI[INCHI_BAS]);
1352 : 0 : free(pINChI_Aux[INCHI_BAS]);
1353 : 0 : free(pINChI[INCHI_REC]);
1354 : 0 : free(pINChI_Aux[INCHI_REC]);
1355 : 0 : ret2 = RI_ERR_ALLOC;
1356 : 0 : goto exit_error;
1357 : : }
1358 [ - + ]: 18 : if (!(pINChI_Aux[iINChI][k][j] = (INChI_Aux*)inchi_calloc(1, sizeof(INChI_Aux))))
1359 : : {
1360 : : /* djb-rwth: avoiding memory leak */
1361 : 0 : free(pINChI[INCHI_BAS]);
1362 : 0 : free(pINChI_Aux[INCHI_BAS]);
1363 : 0 : free(pINChI[INCHI_REC]);
1364 : 0 : free(pINChI_Aux[INCHI_REC]);
1365 : 0 : ret2 = RI_ERR_ALLOC;
1366 : 0 : goto exit_error;
1367 : : }
1368 : : /* copy InChI & AuxInfo */
1369 [ + + ]: 18 : if (k < OneInput->nNumComponents[iINChI][j])
1370 : : {
1371 : :
1372 : : /* copy InChI */
1373 : 16 : *pINChI[iINChI][k][j] = OneInput->pInpInChI[iINChI][j][k];
1374 : 16 : memset(&OneInput->pInpInChI[iINChI][j][k], 0, sizeof(OneInput->pInpInChI[iINChI][j][k])); /* djb-rwth: memset_s C11/Annex K variant? */
1375 : : INCHI_HEAPCHK
1376 : : /* take care of protons in AuxInfo */
1377 : :
1378 [ - + - - ]: 16 : if (nModeProtonIsoExchgH[iINChI] == MODE_PIXH_ADD_TO_EACH && j == TAUT_YES)
1379 : : {
1380 : 0 : pINChI_Aux[iINChI][k][j]->nNumRemovedProtons =
1381 : 0 : OneInput->nNumProtons[iINChI][j].pNumProtons[k].nNumRemovedProtons;
1382 [ # # ]: 0 : for (k1 = 0; k1 < NUM_H_ISOTOPES; k1++)
1383 : : {
1384 : 0 : pINChI_Aux[iINChI][k][j]->nNumRemovedIsotopicH[k1] =
1385 : 0 : OneInput->nNumProtons[iINChI][j].pNumProtons[k].nNumRemovedIsotopicH[k1];
1386 : : }
1387 : : INCHI_HEAPCHK
1388 : : }
1389 [ + + - + ]: 16 : else if ((!k && nModeProtonIsoExchgH[iINChI] == MODE_PIXH_ADD_TO_FIRST) ||
1390 [ + - ]: 3 : (k + 1 == OneInput->nNumComponents[iINChI][j] &&
1391 [ - + ]: 3 : nModeProtonIsoExchgH[iINChI] == MODE_PIXH_ADD_A_PIXH_COMPONENT)) /* djb-rwth: addressing LLVM warnings */
1392 : : {
1393 : : /* add protons and exchangeable isotopic H to the first component's AuxInfo */
1394 : 13 : pINChI_Aux[iINChI][k][j]->nNumRemovedProtons = OneInput->nNumProtons[iINChI][j].nNumRemovedProtons;
1395 [ + + ]: 52 : for (k1 = 0; k1 < NUM_H_ISOTOPES; k1++)
1396 : : {
1397 : 39 : pINChI_Aux[iINChI][k][j]->nNumRemovedIsotopicH[k1] =
1398 : 39 : OneInput->nNumProtons[iINChI][j].nNumRemovedIsotopicH[k1];
1399 : : }
1400 : : INCHI_HEAPCHK
1401 : : }
1402 : : else
1403 : : {
1404 : 3 : pINChI_Aux[iINChI][k][j]->bDeleted = pINChI[iINChI][k][j]->bDeleted;
1405 : : }
1406 : :
1407 [ + - + - : 16 : if (j == TAUT_YES && pINChI[iINChI][k][j] && pINChI[iINChI][k][j]->nNumberOfAtoms &&
+ - ]
1408 [ + - ]: 16 : !pINChI[iINChI][k][j]->nNum_H_fixed)
1409 : : {
1410 : : /* serializer crashes if it is not allocated */
1411 : 16 : pINChI[iINChI][k][j]->nNum_H_fixed = (S_CHAR*)inchi_calloc((long long)pINChI[iINChI][k][j]->nNumberOfAtoms + 1, sizeof(pINChI[0][0][0]->nNum_H_fixed[0])); /* djb-rwth: cast operator added */
1412 : : }
1413 : :
1414 [ + - - + ]: 16 : if (j == TAUT_YES && k < OneInput->nNumComponents[iINChI][TAUT_NON] &&
1415 [ # # # # ]: 0 : pINChI[iINChI][k][j] && pINChI[iINChI][k][j]->nNumberOfAtoms &&
1416 [ # # # # : 0 : pINChI[iINChI][k][TAUT_NON] && pINChI[iINChI][k][TAUT_NON]->nNumberOfAtoms &&
# # ]
1417 : 0 : !CompareReversedINChI(pINChI[iINChI][k][j], pINChI[iINChI][k][TAUT_NON], NULL, NULL))
1418 : : {
1419 : 0 : pINChI[iINChI][k][TAUT_NON]->nNumberOfAtoms = 0; /* eliminate non-taut equal to taut */
1420 : : }
1421 : : }
1422 : : else
1423 : : {
1424 : : /* extra component, usually it is a Mobile H component */
1425 : : /* corresponding to a free proton component in Fixed H */
1426 : 2 : pINChI[iINChI][k][j]->bDeleted = 1;
1427 : 2 : pINChI_Aux[iINChI][k][j]->bDeleted = 1;
1428 : : }
1429 : : } /* k */
1430 : : } /* if ( bReqNonTaut || j != TAUT_NON && OneInput->pInpInChI[iINChI][j] ) */
1431 : :
1432 [ + + ]: 52 : if (OneInput->pInpInChI[iINChI][j])
1433 : : {
1434 : : INCHI_HEAPCHK
1435 [ + - ]: 13 : inchi_free(OneInput->pInpInChI[iINChI][j]);
1436 : 13 : OneInput->pInpInChI[iINChI][j] = NULL;
1437 : : }
1438 : : } /* j */
1439 : : } /* iINChI */
1440 : :
1441 [ - + ]: 13 : if (bReqSplitOutputInChI)
1442 : : {
1443 [ # # ]: 0 : if (bHasSomeReconnected)
1444 : : {
1445 : 0 : iINChI1 = INCHI_REC; /* only reconnected */
1446 : : /* djb-rwth: removing redundant code */
1447 : 0 : sd->num_components[INCHI_BAS] = sd->num_components[INCHI_REC];
1448 : : }
1449 : : else
1450 : : {
1451 : 0 : iINChI1 = 0; /* only disconnected */
1452 : : /* djb-rwth: removing redundant code */
1453 : : }
1454 : 0 : sd->num_components[INCHI_REC] = 0; /* treat reconnected as connected */
1455 : 0 : nNumOutputComponents = sd->num_components[INCHI_BAS];
1456 : : }
1457 : : else
1458 : : {
1459 : 13 : iINChI1 = 0;
1460 : : /* djb-rwth: removing redundant code */
1461 : 13 : nNumOutputComponents = 1;
1462 : : }
1463 : :
1464 [ - + + - : 26 : for (k1 = 0, k2 = (bReqSplitOutputInChI ? k1 + 1 : nNumOutputComponents); k1 < k2 && k1 < nNumOutputComponents; k1 = k2, k2++)
+ + ]
1465 : : {
1466 : :
1467 [ - + ]: 13 : if (bReqSplitOutputInChI)
1468 : : {
1469 : 0 : sd->num_components[INCHI_BAS] = 1;
1470 : 0 : sd->num_components[INCHI_REC] = 0;
1471 : : /* additional data */
1472 : 0 : sd->num_non_taut[INCHI_BAS] =
1473 : 0 : sd->num_taut[INCHI_BAS] =
1474 : 0 : sd->num_non_taut[INCHI_REC] =
1475 : 0 : sd->num_taut[INCHI_REC] = 0;
1476 : 0 : iINChI = iINChI1;
1477 [ # # # # ]: 0 : for (j = 0; j < TAUT_NUM && sd->num_components[iINChI]; j++)
1478 : : {
1479 [ # # ]: 0 : for (k = k1; k < k2; k++)
1480 : : {
1481 : : /* find where the current processed structure is located */
1482 [ # # # # ]: 0 : int cur_is_in_non_taut = (pINChI[iINChI][k][TAUT_NON] && pINChI[iINChI][k][TAUT_NON]->nNumberOfAtoms > 0);
1483 [ # # # # ]: 0 : int cur_is_in_taut = (pINChI[iINChI][k][TAUT_YES] && pINChI[iINChI][k][TAUT_YES]->nNumberOfAtoms > 0);
1484 [ # # # # : 0 : int cur_is_non_taut = (cur_is_in_non_taut && 0 == pINChI[iINChI][k][TAUT_NON]->lenTautomer) ||
# # ]
1485 [ # # ]: 0 : (cur_is_in_taut && 0 == pINChI[iINChI][k][TAUT_YES]->lenTautomer); /* djb-rwth: addressing LLVM warnings */
1486 [ # # # # ]: 0 : int cur_is_taut = cur_is_in_taut && 0 < pINChI[iINChI][k][TAUT_YES]->lenTautomer;
1487 [ # # ]: 0 : if (cur_is_non_taut + cur_is_taut)
1488 : : {
1489 : : /* count tautomeric and non-tautomeric components of the structures */
1490 : : /*
1491 : : int j1 = cur_is_in_non_taut? TAUT_NON:TAUT_YES;
1492 : : int j2 = cur_is_in_taut? TAUT_YES:TAUT_NON;
1493 : : */
1494 : 0 : sd->num_non_taut[INCHI_BAS] += cur_is_non_taut;
1495 : 0 : sd->num_taut[INCHI_BAS] += cur_is_taut;
1496 : : }
1497 : : }
1498 : : }
1499 : : INCHI_HEAPCHK
1500 : : }
1501 : : else
1502 : : {
1503 : 13 : sd->num_components[INCHI_BAS] = inchi_max(OneInput->nNumComponents[INCHI_BAS][TAUT_YES],
1504 : : OneInput->nNumComponents[INCHI_BAS][TAUT_NON]);
1505 : 13 : sd->num_components[INCHI_REC] = inchi_max(OneInput->nNumComponents[INCHI_REC][TAUT_YES],
1506 : : OneInput->nNumComponents[INCHI_REC][TAUT_NON]);
1507 : : /* additional data needed for SortAndPrintINChI() */
1508 [ + + ]: 39 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
1509 : : {
1510 : 26 : sd->num_non_taut[iINChI] =
1511 : 26 : sd->num_taut[iINChI] = 0;
1512 [ + + + + ]: 52 : for (j = 0; j < TAUT_NUM && sd->num_components[iINChI]; j++)
1513 : : {
1514 [ + + ]: 52 : for (k = k1; k < k2; k++)
1515 : : {
1516 : : /* find where the current processed structure is located */
1517 [ + + - + ]: 26 : int cur_is_in_non_taut = (pINChI[iINChI][k][TAUT_NON] && pINChI[iINChI][k][TAUT_NON]->nNumberOfAtoms > 0);
1518 [ + - + - ]: 26 : int cur_is_in_taut = (pINChI[iINChI][k][TAUT_YES] && pINChI[iINChI][k][TAUT_YES]->nNumberOfAtoms > 0);
1519 [ - + - - : 52 : int cur_is_non_taut = (cur_is_in_non_taut && 0 == pINChI[iINChI][k][TAUT_NON]->lenTautomer) ||
+ - ]
1520 [ + + ]: 26 : (cur_is_in_taut && 0 == pINChI[iINChI][k][TAUT_YES]->lenTautomer); /* djb-rwth: addressing LLVM warnings */
1521 [ + - + + ]: 26 : int cur_is_taut = cur_is_in_taut && 0 < pINChI[iINChI][k][TAUT_YES]->lenTautomer;
1522 [ + - ]: 26 : if (cur_is_non_taut + cur_is_taut)
1523 : : {
1524 : : /* count tautomeric and non-tautomeric components of the structures */
1525 : : /*
1526 : : int j1 = cur_is_in_non_taut? TAUT_NON:TAUT_YES;
1527 : : int j2 = cur_is_in_taut? TAUT_YES:TAUT_NON;
1528 : : */
1529 : 26 : sd->num_non_taut[iINChI] += cur_is_non_taut;
1530 : 26 : sd->num_taut[iINChI] += cur_is_taut;
1531 : : }
1532 : : }
1533 : : }
1534 : : }
1535 : : INCHI_HEAPCHK
1536 : : }
1537 [ - + ]: 13 : if (bReqSplitOutputInChI)
1538 : : {
1539 : : /* output components one by one (for splitting input InChI into components) */
1540 : : PINChI2* pInChI_2[INCHI_NUM];
1541 : : PINChI_Aux2* pInChI_Aux_2[INCHI_NUM];
1542 : : INChI* pInChI_1[1][2];
1543 : : INChI_Aux* pInChI_Aux_1[1][2];
1544 : 0 : memset(pInChI_2, 0, sizeof(pInChI_2)); /* djb-rwth: memset_s C11/Annex K variant? */
1545 : 0 : memset(pInChI_Aux_2, 0, sizeof(pInChI_Aux_2)); /* djb-rwth: memset_s C11/Annex K variant? */
1546 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
1547 : : {
1548 : 0 : pInChI_1[0][j] = pINChI[iINChI1][k1][j];
1549 : 0 : pInChI_Aux_1[0][j] = pINChI_Aux[iINChI1][k1][j];
1550 : : }
1551 : 0 : pInChI_2[INCHI_BAS] = pInChI_1;
1552 : 0 : pInChI_Aux_2[INCHI_BAS] = pInChI_Aux_1;
1553 : : /* make sure purely reconnected InChI is marked as ReChI, not InChI */
1554 [ # # # # ]: 0 : if (bHasSomeReconnected &&
1555 [ # # ]: 0 : (bInChIHasReconnectedMetal(pInChI_1[0][TAUT_YES]) ||
1556 : 0 : bInChIHasReconnectedMetal(pInChI_1[0][TAUT_NON])))
1557 : : {
1558 : 0 : bSortPrintINChIFlags = FLAG_SORT_PRINT_ReChI_PREFIX;
1559 : : }
1560 : : else
1561 : : {
1562 : 0 : bSortPrintINChIFlags = 0;
1563 : : }
1564 : : INCHI_HEAPCHK
1565 : 0 : nRet1 = SortAndPrintINChI(pCG, pOut, strbuf, pLog, ip,
1566 : : NULL /*orig_inp_data*/,
1567 : : NULL /*prep_inp_data*/,
1568 : : NULL /*composite_norm_data*/,
1569 : : NULL /*pOrigStruct*/,
1570 : 0 : sd->num_components, sd->num_non_taut,
1571 : 0 : sd->num_taut, sd->bTautFlags,
1572 : 0 : sd->bTautFlagsDone, pncFlags, num_inp,
1573 : : pInChI_2, pInChI_Aux_2,
1574 : : &bSortPrintINChIFlags, save_opt_bits);
1575 : : INCHI_HEAPCHK
1576 : : }
1577 : : else
1578 : : {
1579 : : INCHI_HEAPCHK
1580 : :
1581 : 13 : bSortPrintINChIFlags = 0;
1582 : 13 : nRet1 = SortAndPrintINChI(pCG, pOut, strbuf, pLog, ip,
1583 : : NULL /*orig_inp_data*/, NULL /*prep_inp_data*/,
1584 : : NULL /*composite_norm_data*/, NULL /*pOrigStruct*/,
1585 : 13 : sd->num_components, sd->num_non_taut, sd->num_taut,
1586 : 13 : sd->bTautFlags, sd->bTautFlagsDone, pncFlags, num_inp,
1587 : : pINChI, pINChI_Aux, &bSortPrintINChIFlags,
1588 : : save_opt_bits);
1589 : : INCHI_HEAPCHK
1590 : : }
1591 [ + - + - ]: 13 : if (nRet1 == _IS_FATAL || nRet1 == _IS_ERROR)
1592 : : {
1593 : : break;
1594 : : }
1595 : : }
1596 : :
1597 : : INCHI_HEAPCHK
1598 : 13 : FreeAllINChIArrays(pINChI, pINChI_Aux, num_components);
1599 : : INCHI_HEAPCHK
1600 : :
1601 [ + + ]: 39 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
1602 : : {
1603 [ + + ]: 78 : for (j = 0; j < TAUT_NUM; j++)
1604 : : {
1605 [ - + ]: 52 : if (OneInput->nNumProtons[iINChI][j].pNumProtons)
1606 : : {
1607 [ # # ]: 0 : inchi_free(OneInput->nNumProtons[iINChI][j].pNumProtons);
1608 : 0 : OneInput->nNumProtons[iINChI][j].pNumProtons = NULL;
1609 : : }
1610 : : }
1611 : : }
1612 : :
1613 : : INCHI_HEAPCHK
1614 : :
1615 [ + - + - ]: 13 : if (nRet1 == _IS_FATAL || nRet1 == _IS_ERROR)
1616 : : {
1617 : 0 : ret2 = RI_ERR_PROGR;
1618 : : }
1619 : :
1620 : 13 : exit_error:
1621 : :
1622 : 13 : inchi_strbuf_close(strbuf);
1623 : :
1624 : 13 : return ret2;
1625 : : }
1626 : :
1627 : :
1628 : : /****************************************************************************/
1629 : 0 : int GetNumNeighborsFromInchi(INChI* pInChI, AT_NUMB nAtNumber)
1630 : : {
1631 : : int i, j, n_vertex, n_neigh, nNumNeigh, bTautAtom, nNumH, nTotNumNeigh, num_atoms;
1632 : : AT_NUMB taut_at_number;
1633 : 0 : nAtNumber -= 1;
1634 : 0 : nNumNeigh = 0; /* number of bonds */
1635 : 0 : bTautAtom = 0; /* 1 if atom belongs to a Mobile-H group */
1636 : 0 : nNumH = 0; /* number of terminal neighbors H */
1637 : 0 : num_atoms = 0; /* djb-rwth: initialisation with pInChI below */
1638 : :
1639 [ # # ]: 0 : if (pInChI) /* djb-rwth: fixing a NULL pointer dereference */
1640 : : {
1641 : 0 : num_atoms = pInChI->nNumberOfAtoms;
1642 : : /* from RestoreAtomConnectionsSetStereo() */
1643 : : /* Connection table structure:
1644 : : Vert(1) [, Neigh(11), Neigh(12),...], Vert(2) [, Neigh(2,1), Neigh(2,2),...] ...
1645 : : where Neigh(i,1) < Neigh(i,2) <... < Vert(i);
1646 : : Vert(i) < Vert(i+1)
1647 : : */
1648 [ # # ]: 0 : for (i = 1, n_vertex = pInChI->nConnTable[0] - 1; i < pInChI->lenConnTable; i++)
1649 : : {
1650 [ # # ]: 0 : if ((n_neigh = pInChI->nConnTable[i] - 1) < n_vertex)
1651 : : {
1652 : : /* vertex - neighbor connection */
1653 [ # # # # ]: 0 : nNumNeigh += (nAtNumber == n_vertex || nAtNumber == n_neigh);
1654 : : }
1655 : : else
1656 : : {/* n_neigh is the next vertex */
1657 [ # # ]: 0 : if ((n_vertex = n_neigh) >= num_atoms)
1658 : : {
1659 : 0 : return RI_ERR_PROGR;
1660 : : }
1661 : : }
1662 : : }
1663 : : }
1664 : :
1665 : :
1666 : : /* is atom tautomeric, from GetTgroupInfoFromInChI() */
1667 [ # # # # : 0 : if (pInChI && pInChI->lenTautomer > 1 && pInChI->nTautomer && pInChI->nTautomer[0] > 0)
# # # # ]
1668 : : {
1669 : : int itg, len_tg;
1670 : 0 : int tot_len_tg = pInChI->lenTautomer - T_GROUP_HDR_LEN * pInChI->nTautomer[0] - 1; /* number of endpoints */
1671 : 0 : j = 1; /* index in pInChI->nTautomer[] */
1672 : 0 : i = 0; /* index in ti->nEndpointAtomNumber[] */
1673 [ # # ]: 0 : for (itg = 0; itg < pInChI->nTautomer[0]; itg++)
1674 : : {
1675 : 0 : len_tg = pInChI->nTautomer[j]; /* t-group length not including pInChI->nTautomer[j] */
1676 : 0 : j += T_GROUP_HDR_LEN; /* skip t-group header */
1677 : 0 : len_tg -= T_GROUP_HDR_LEN - 1;
1678 [ # # ]: 0 : for (; 0 < len_tg--; j++, i++)
1679 : : {
1680 : 0 : taut_at_number = pInChI->nTautomer[j] - 1; /* Mobile-H group atom number */
1681 : 0 : bTautAtom += (taut_at_number == nAtNumber);
1682 : : }
1683 : : }
1684 [ # # ]: 0 : if (i != tot_len_tg)
1685 : : {
1686 : 0 : return RI_ERR_PROGR;
1687 : : }
1688 : : }
1689 : : /* count hydrogen neighbors */
1690 [ # # # # ]: 0 : if (pInChI && pInChI->nNum_H) /* djb-rwth: condition added for fixing a NULL pointer dereference */
1691 : : {
1692 : 0 : nNumH = pInChI->nNum_H[nAtNumber];
1693 : : }
1694 : : /* conclusion: if not tautomeric then return positive number, otherwise add 1000 */
1695 : 0 : nTotNumNeigh = nNumNeigh + nNumH;
1696 [ # # ]: 0 : if (bTautAtom)
1697 : : {
1698 : 0 : nTotNumNeigh += 1000;
1699 : : }
1700 : 0 : return nTotNumNeigh;
1701 : :
1702 : : }
1703 : :
1704 : :
1705 : : /****************************************************************************/
1706 : 6 : int CountStereoTypes(INChI* pInChI,
1707 : : int* num_known_SB,
1708 : : int* num_known_SC,
1709 : : int* num_unk_und_SB,
1710 : : int* num_unk_und_SC,
1711 : : int* num_SC_PIII,
1712 : : int* num_SC_AsIII)
1713 : : {
1714 : : INChI_Stereo* Stereo;
1715 : : int i, ret;
1716 : : AT_NUMB nAtNumber;
1717 : : U_CHAR el_number;
1718 : :
1719 [ + - + + ]: 6 : if (!pInChI->nNumberOfAtoms || pInChI->bDeleted)
1720 : : {
1721 : 3 : return 0; /* no InChI */
1722 : : }
1723 : 6 : Stereo = (pInChI->StereoIsotopic &&
1724 : 0 : (pInChI->StereoIsotopic->nNumberOfStereoBonds +
1725 [ - + - - ]: 6 : pInChI->StereoIsotopic->nNumberOfStereoCenters)) ? pInChI->StereoIsotopic :
1726 : 3 : (pInChI->Stereo &&
1727 : 0 : (pInChI->Stereo->nNumberOfStereoBonds +
1728 [ - + - - ]: 3 : pInChI->Stereo->nNumberOfStereoCenters)) ? pInChI->Stereo : NULL;
1729 [ + - ]: 3 : if (!Stereo)
1730 : : {
1731 : 3 : return 1; /* No Stereo */
1732 : : }
1733 : :
1734 : : /* count SB and cumulenes */
1735 [ # # ]: 0 : for (i = 0; i < Stereo->nNumberOfStereoBonds; i++)
1736 : : {
1737 [ # # # # ]: 0 : if (ATOM_PARITY_WELL_DEF(Stereo->b_parity[i]))
1738 : : {
1739 : 0 : (*num_known_SB)++;
1740 : : }
1741 : : else
1742 : : {
1743 : 0 : (*num_unk_und_SB)++;
1744 : : }
1745 : : }
1746 : : /* count SC and allenes */
1747 [ # # ]: 0 : for (i = 0; i < Stereo->nNumberOfStereoCenters; i++)
1748 : : {
1749 [ # # # # ]: 0 : if (!(nAtNumber = Stereo->nNumber[i]) || nAtNumber > pInChI->nNumberOfAtoms)
1750 : : {
1751 : 0 : return RI_ERR_PROGR; /* wrong data, should never happen */
1752 : : }
1753 [ # # # # ]: 0 : if (ATOM_PARITY_WELL_DEF(Stereo->t_parity[i]))
1754 : : {
1755 : 0 : (*num_known_SC)++;
1756 : : }
1757 : : else
1758 : : {
1759 : 0 : (*num_unk_und_SC)++;
1760 : : }
1761 : 0 : el_number = pInChI->nAtom[nAtNumber - 1];
1762 [ # # # # ]: 0 : if (el_number != EL_NUMBER_P && el_number != EL_NUMBER_AS)
1763 : : {
1764 : 0 : continue;
1765 : : }
1766 : 0 : ret = GetNumNeighborsFromInchi(pInChI, nAtNumber);
1767 [ # # ]: 0 : if (ret < 0)
1768 : : {
1769 : 0 : return ret;
1770 : : }
1771 [ # # ]: 0 : if (3 == ret)
1772 : : {
1773 : 0 : *num_SC_PIII += (EL_NUMBER_P == el_number);
1774 : 0 : *num_SC_AsIII += (EL_NUMBER_AS == el_number);
1775 : : }
1776 : : }
1777 : :
1778 : 0 : return 2; /* Has Stereo */
1779 : : }
1780 : :
1781 : :
1782 : : /****************************************************************************/
1783 : 6 : int bInpInchiComponentExists(InpInChI* pOneInput,
1784 : : int iInChI,
1785 : : int bMobileH,
1786 : : int k)
1787 : : {
1788 [ - + - - : 6 : if ((INCHI_BAS != iInChI && iInChI != INCHI_REC) ||
+ - ]
1789 [ + - - + ]: 6 : (TAUT_NON != bMobileH && TAUT_YES != bMobileH) || k < 0) /* djb-rwth: addressing LLVM warnings */
1790 : : {
1791 : 0 : return 0;
1792 : : }
1793 : :
1794 : 12 : return (k < pOneInput->nNumComponents[iInChI][bMobileH] &&
1795 [ + - ]: 6 : pOneInput->pInpInChI[iInChI][bMobileH] &&
1796 [ + - + - ]: 18 : pOneInput->pInpInChI[iInChI][bMobileH][k].nNumberOfAtoms > 0 &&
1797 [ + + ]: 6 : !pOneInput->pInpInChI[iInChI][bMobileH][k].bDeleted);
1798 : : }
1799 : :
1800 : :
1801 : : /****************************************************************************/
1802 : 0 : int bInpInchiComponentDeleted(InpInChI* pOneInput,
1803 : : int iInChI,
1804 : : int bMobileH,
1805 : : int k)
1806 : : {
1807 [ # # # # : 0 : if ((INCHI_BAS != iInChI && iInChI != INCHI_REC) ||
# # ]
1808 [ # # # # ]: 0 : (TAUT_NON != bMobileH && TAUT_YES != bMobileH) || k < 0) /* djb-rwth: addressing LLVM warnings */
1809 : : {
1810 : 0 : return 0;
1811 : : }
1812 : :
1813 : 0 : return (k < pOneInput->nNumComponents[iInChI][bMobileH] &&
1814 [ # # ]: 0 : pOneInput->pInpInChI[iInChI][bMobileH] &&
1815 [ # # # # ]: 0 : pOneInput->pInpInChI[iInChI][bMobileH][k].nNumberOfAtoms > 0 &&
1816 [ # # ]: 0 : pOneInput->pInpInChI[iInChI][bMobileH][k].bDeleted);
1817 : : }
1818 : :
1819 : :
1820 : : /****************************************************************************/
1821 : 8 : int bRevInchiComponentExists(StrFromINChI* pStruct,
1822 : : int iInChI,
1823 : : int bMobileH,
1824 : : int k)
1825 : : {
1826 [ + - + - : 8 : if (!pStruct || /*!pStruct->at2 ||*/ !pStruct->num_atoms ||
+ + ]
1827 [ + - + - ]: 8 : (INCHI_BAS != iInChI && iInChI != INCHI_REC) ||
1828 [ + - - + ]: 8 : (TAUT_NON != bMobileH && TAUT_YES != bMobileH) || k < 0) /* djb-rwth: addressing LLVM warnings */
1829 : : {
1830 : 0 : return 0;
1831 : : }
1832 : :
1833 : 11 : return (k < pStruct->RevInChI.num_components[iInChI] &&
1834 [ + - ]: 3 : pStruct->RevInChI.pINChI[iInChI] &&
1835 [ + - ]: 3 : pStruct->RevInChI.pINChI[iInChI][k][bMobileH] &&
1836 [ + + + - ]: 14 : pStruct->RevInChI.pINChI[iInChI][k][bMobileH]->nNumberOfAtoms > 0 &&
1837 [ + + ]: 3 : !pStruct->RevInChI.pINChI[iInChI][k][bMobileH]->bDeleted);
1838 : : }
1839 : :
1840 : :
1841 : : /****************************************************************************/
1842 : 0 : int bRevInchiComponentDeleted(StrFromINChI* pStruct,
1843 : : int iInChI,
1844 : : int bMobileH,
1845 : : int k)
1846 : : {
1847 [ # # # # : 0 : if (!pStruct || /*!pStruct->at2 ||*/ !pStruct->num_atoms ||
# # ]
1848 [ # # # # ]: 0 : (INCHI_BAS != iInChI && iInChI != INCHI_REC) ||
1849 [ # # # # ]: 0 : (TAUT_NON != bMobileH && TAUT_YES != bMobileH) || k < 0) /* djb-rwth: addressing LLVM warnings */
1850 : : {
1851 : 0 : return 0;
1852 : : }
1853 : :
1854 : 0 : return (k < pStruct->RevInChI.num_components[iInChI] &&
1855 [ # # ]: 0 : pStruct->RevInChI.pINChI[iInChI] &&
1856 [ # # ]: 0 : pStruct->RevInChI.pINChI[iInChI][k][bMobileH] &&
1857 [ # # # # ]: 0 : pStruct->RevInChI.pINChI[iInChI][k][bMobileH]->nNumberOfAtoms > 0 &&
1858 [ # # ]: 0 : pStruct->RevInChI.pINChI[iInChI][k][bMobileH]->bDeleted);
1859 : : }
1860 : :
1861 : :
1862 : : /****************************************************************************/
1863 : 3 : int DetectInpInchiCreationOptions(InpInChI* pOneInput,
1864 : : int* bHasReconnected,
1865 : : int* bHasMetal,
1866 : : int* bHasFixedH,
1867 : : int* nModeFlagsStereo,
1868 : : int* bTautFlagsStereo)
1869 : : {
1870 : 3 : int ret = 0, bHasStereo;
1871 : 3 : int nModeFlagsValue = 0, bTautFlagsValue; /* stereo flags */
1872 : : int iInChI, iMobileH, bIso, k, max_components, num_components;
1873 : : INChI* pInChI;
1874 : : int num_known_SB /*Stereo Bonds & Cumulenes >C==C==C==C< */;
1875 : : int num_known_SC /* Stereo Centers & Allenes >C=C=C< */;
1876 : : int num_unk_und_SB, num_unk_und_SC;
1877 : : int num_SC_PIII, num_SC_AsIII; /* has Phosphine or Arsine stereo center(s) */
1878 : :
1879 : 3 : *bHasReconnected = *bHasFixedH = *nModeFlagsStereo = *bTautFlagsStereo = 0;
1880 : 3 : nModeFlagsValue = bTautFlagsValue = bHasStereo = 0;
1881 : 3 : num_known_SB = num_known_SC = num_unk_und_SB = num_unk_und_SC = num_SC_PIII = num_SC_AsIII = 0;
1882 : 3 : *bHasMetal = 0;
1883 : :
1884 [ + + ]: 9 : for (iInChI = 0; iInChI < INCHI_NUM; iInChI++)
1885 : : {
1886 [ + + ]: 18 : for (iMobileH = 0; iMobileH < TAUT_NUM; iMobileH++)
1887 : : {
1888 [ + - + + ]: 36 : for (bIso = 1; !nModeFlagsValue && 0 <= bIso; bIso--)
1889 : : {
1890 [ - - - + ]: 24 : switch (pOneInput->s[iInChI][iMobileH][bIso])
1891 : : {
1892 : 0 : case 1: /* SABS */
1893 : 0 : nModeFlagsValue |= REQ_MODE_STEREO | REQ_MODE_ISO_STEREO;
1894 : 0 : break;
1895 : 0 : case 2:
1896 : 0 : nModeFlagsValue |= REQ_MODE_STEREO | REQ_MODE_ISO_STEREO | REQ_MODE_RELATIVE_STEREO;
1897 : 0 : break;
1898 : 0 : case 3:
1899 : 0 : nModeFlagsValue |= REQ_MODE_STEREO | REQ_MODE_ISO_STEREO | REQ_MODE_RACEMIC_STEREO;
1900 : : }
1901 : : }
1902 : :
1903 : 24 : max_components = pOneInput->pInpInChI[iInChI][iMobileH] ?
1904 [ + + ]: 12 : pOneInput->nNumComponents[iInChI][iMobileH] : 0;
1905 : :
1906 [ + + ]: 18 : for (k = num_components = 0; k < max_components; k++)
1907 : : {
1908 : 6 : pInChI = pOneInput->pInpInChI[iInChI][iMobileH] + k;
1909 : 6 : ret = CountStereoTypes(pInChI,
1910 : : &num_known_SB, &num_known_SC,
1911 : : &num_unk_und_SB, &num_unk_und_SC,
1912 : : &num_SC_PIII, &num_SC_AsIII);
1913 [ - + ]: 6 : if (ret < 0)
1914 : : {
1915 : 0 : return ret; /* error */
1916 : : }
1917 : 6 : bHasStereo += (ret == 2);
1918 [ + + ]: 6 : if ((ret > 0))
1919 : : {
1920 : : /* ret == 0 => Empty InChI, 1=> No Stereo, 2=> Has Stereo */
1921 : 3 : num_components++;
1922 : 3 : *bHasReconnected |= (iInChI == INCHI_REC);
1923 : 3 : *bHasFixedH |= (iMobileH == TAUT_NON);
1924 : : }
1925 : 6 : *bHasMetal |= bInChIHasReconnectedMetal(pInChI);
1926 : : }
1927 : : }
1928 : : }
1929 : :
1930 [ - + - - ]: 3 : if ((nModeFlagsValue & REQ_MODE_RELATIVE_STEREO) && (nModeFlagsValue & REQ_MODE_RACEMIC_STEREO))
1931 : : {
1932 : 0 : return RI_ERR_SYNTAX;
1933 : : }
1934 [ - + - - ]: 3 : if (bHasStereo && !nModeFlagsValue) /* REQ_MODE_SB_IGN_ALL_UU | REQ_MODE_SC_IGN_ALL_UU*/
1935 : : {
1936 : : /* inversion does not change the stereo or no stereo at all */
1937 : 0 : nModeFlagsValue = REQ_MODE_STEREO | REQ_MODE_ISO_STEREO; /* Abs */
1938 : : }
1939 : :
1940 [ + - - + ]: 3 : if (!num_known_SB && num_unk_und_SB)
1941 : : {
1942 : : ; /* full SUU option or SB part of it */
1943 : : }
1944 : : else
1945 : : {
1946 : 3 : nModeFlagsValue |= REQ_MODE_SB_IGN_ALL_UU; /* ignore Unknown/Undefind SB if no well-defined SB exist */
1947 : : }
1948 : :
1949 [ + - - + ]: 3 : if (!num_known_SC && num_unk_und_SC)
1950 : : {
1951 : : ; /* full SUU option or SB part of it */
1952 : : }
1953 : : else
1954 : : {
1955 : 3 : nModeFlagsValue |= REQ_MODE_SC_IGN_ALL_UU; /* ignore Unknown/Undefind SC if no well-defined SB exist */
1956 : : }
1957 : : /* Phosphine and Arsine Stereo */
1958 [ - + ]: 3 : if (num_SC_PIII)
1959 : : {
1960 : 0 : bTautFlagsValue |= TG_FLAG_PHOSPHINE_STEREO;
1961 : : }
1962 : : /* Phosphine and Arsine Stereo */
1963 [ - + ]: 3 : if (num_SC_AsIII)
1964 : : {
1965 : 0 : bTautFlagsValue |= TG_FLAG_ARSINE_STEREO;
1966 : : }
1967 : :
1968 : 3 : *nModeFlagsStereo = nModeFlagsValue;
1969 : 3 : *bTautFlagsStereo = bTautFlagsValue;
1970 : :
1971 : 3 : return 0;
1972 : : }
1973 : :
1974 : :
1975 : : /****************************************************************************/
1976 : 6 : int bInChIHasReconnectedMetal(INChI* pInChI)
1977 : : {
1978 : : int i;
1979 [ + - + + : 6 : if (pInChI && !pInChI->bDeleted && pInChI->nNumberOfAtoms && pInChI->nAtom)
+ - + - ]
1980 : : {
1981 [ + + ]: 11 : for (i = 0; i < pInChI->nNumberOfAtoms; i++)
1982 : : {
1983 [ - + ]: 8 : if (is_el_a_metal((int)pInChI->nAtom[i]))
1984 : : {
1985 [ # # # # : 0 : if (pInChI->nNumberOfAtoms > 1 || (pInChI->nNum_H && pInChI->nNum_H[0])) /* djb-rwth: addressing LLVM warning */
# # ]
1986 : : {
1987 : 0 : return 1;
1988 : : }
1989 : : }
1990 : : }
1991 : : }
1992 : :
1993 : 6 : return 0;
1994 : : }
1995 : :
1996 : :
1997 : : /****************************************************************************/
1998 : 19 : int SetProtonsAndXchgIsoH(int bInChI2Structure,
1999 : : int bReqSplitOutputInChI,
2000 : : int bReqProtonsForEachComponent,
2001 : : int bReqNonTaut,
2002 : : int bReqStereo,
2003 : : int num_components[INCHI_NUM],
2004 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
2005 : : InpInChI* OneInput)
2006 : : {
2007 : 19 : int j, k, k1, ret2 = 0, iINChI;
2008 : : int bAvailableProtonsForEachComponent, bAvailableProtonsTotal;
2009 : :
2010 : : INCHI_HEAPCHK
2011 : :
2012 : 19 : num_components[INCHI_BAS] = num_components[INCHI_REC] = 0;
2013 : :
2014 [ + + ]: 57 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
2015 : : {
2016 : 38 : nModeProtonIsoExchgH[iINChI] = MODE_PIXH_UNDEFINED;
2017 : : /* are totals of /p and/or /i/h available ? */
2018 : 38 : bAvailableProtonsTotal = 0 != OneInput->nNumProtons[iINChI][TAUT_YES].nNumRemovedProtons;
2019 [ + + ]: 152 : for (k1 = 0; k1 < NUM_H_ISOTOPES; k1++)
2020 : : {
2021 : 114 : bAvailableProtonsTotal |= 0 != OneInput->nNumProtons[iINChI][TAUT_YES].nNumRemovedIsotopicH[k1];
2022 : : }
2023 : : /* are /p and/or /i/h available for each component ? */
2024 : 38 : bAvailableProtonsForEachComponent = (NULL != OneInput->nNumProtons[iINChI][TAUT_YES].pNumProtons);
2025 : :
2026 : : /* decision: add /p to each component, add total to the 1st, add total as one more component */
2027 : : /* In case of bInChI2Structure just keep totals if not available for each component */
2028 : :
2029 [ + + ]: 38 : if (bInChI2Structure)
2030 : : {
2031 : 6 : nModeProtonIsoExchgH[iINChI] = bAvailableProtonsForEachComponent ?
2032 [ - + ]: 6 : MODE_PIXH_ADD_TO_EACH :
2033 : : MODE_PIXH_KEEP_TOTALS;
2034 : : }
2035 : : else
2036 : : {
2037 [ + - ]: 32 : if (!bReqSplitOutputInChI)
2038 : : {
2039 : 32 : nModeProtonIsoExchgH[iINChI] = bAvailableProtonsForEachComponent ?
2040 [ - + ]: 32 : MODE_PIXH_ADD_TO_EACH :
2041 : : MODE_PIXH_ADD_TO_FIRST;
2042 : : }
2043 : : else
2044 : : {
2045 [ # # ]: 0 : if (!bAvailableProtonsForEachComponent)
2046 : : {
2047 : 0 : nModeProtonIsoExchgH[iINChI] = bAvailableProtonsTotal ?
2048 [ # # ]: 0 : MODE_PIXH_ADD_A_PIXH_COMPONENT :
2049 : : MODE_PIXH_ADD_TO_FIRST;
2050 : : }
2051 : : else
2052 : : {
2053 : : /* bAvailableProtonsForEachComponent && bReqSplitOutputInChI */
2054 [ # # ]: 0 : if (bReqProtonsForEachComponent)
2055 : : {
2056 : 0 : nModeProtonIsoExchgH[iINChI] = MODE_PIXH_ADD_TO_EACH;
2057 : : }
2058 : : else
2059 : : {
2060 : 0 : nModeProtonIsoExchgH[iINChI] = bReqNonTaut ?
2061 [ # # ]: 0 : MODE_PIXH_ADD_TO_EACH :
2062 : : MODE_PIXH_ADD_A_PIXH_COMPONENT;
2063 : : }
2064 : : }
2065 : : }
2066 : : }
2067 : :
2068 : : /* remove unneeded data: protons for each component */
2069 [ - + ]: 38 : if (bAvailableProtonsForEachComponent &&
2070 [ # # ]: 0 : nModeProtonIsoExchgH[iINChI] != MODE_PIXH_ADD_TO_EACH)
2071 : : {
2072 [ # # ]: 0 : inchi_free(OneInput->nNumProtons[iINChI][TAUT_YES].pNumProtons);
2073 : 0 : OneInput->nNumProtons[iINChI][TAUT_YES].pNumProtons = NULL;
2074 : 0 : bAvailableProtonsForEachComponent = 0;
2075 : : }
2076 : : /* remove unneeded data: total protons all components */
2077 [ - + - - ]: 38 : if (bAvailableProtonsTotal && nModeProtonIsoExchgH[iINChI] == MODE_PIXH_ADD_TO_EACH)
2078 : : {
2079 : 0 : OneInput->nNumProtons[iINChI][TAUT_YES].nNumRemovedProtons = 0;
2080 [ # # ]: 0 : for (k1 = 0; k1 < NUM_H_ISOTOPES; k1++)
2081 : : {
2082 : 0 : OneInput->nNumProtons[iINChI][TAUT_YES].nNumRemovedIsotopicH[k1] = 0;
2083 : : }
2084 : : /* djb-rwth: removing redundant code */
2085 : : }
2086 : : /* remove unneeded data: Fixed-H InChI; no protons data exist for Fixed-H */
2087 [ + + - + ]: 38 : if (!bReqNonTaut && OneInput->nNumComponents[iINChI][TAUT_NON])
2088 : : {
2089 : 0 : j = TAUT_NON;
2090 [ # # ]: 0 : for (k = 0; k < OneInput->nNumComponents[iINChI][j]; k++)
2091 : : {
2092 : 0 : Free_INChI_Members(&OneInput->pInpInChI[iINChI][j][k]);
2093 : : }
2094 [ # # ]: 0 : inchi_free(OneInput->pInpInChI[iINChI][j]);
2095 : 0 : OneInput->pInpInChI[iINChI][j] = NULL;
2096 : 0 : OneInput->nNumComponents[iINChI][j] = 0;
2097 : : }
2098 : : #ifdef NEVER
2099 : : /* remove unneeded data: Mobile-H InChI ????? */
2100 : : if (bReqNonTaut && OneInput->nNumComponents[iINChI][TAUT_NON])
2101 : : {
2102 : : j = TAUT_YES;
2103 : : for (k = 0; k < OneInput->nNumComponents[iINChI][j]; k++)
2104 : : {
2105 : : Free_INChI_Members(&OneInput->pInpInChI[iINChI][j][k]);
2106 : : }
2107 : : inchi_free(OneInput->pInpInChI[iINChI][j]);
2108 : : OneInput->pInpInChI[iINChI][j] = NULL;
2109 : : OneInput->nNumComponents[iINChI][j] = 0;
2110 : : nModeProtonIsoExchgH[iINChI] = MODE_PIXH_UNDEFINED;
2111 : : if (OneInput->nNumProtons[iINChI][TAUT_YES].pNumProtons)
2112 : : {
2113 : : inchi_free(OneInput->nNumProtons[iINChI][TAUT_YES].pNumProtons);
2114 : : OneInput->nNumProtons[iINChI][TAUT_YES].pNumProtons = NULL;
2115 : : }
2116 : : }
2117 : : #endif
2118 : : /* add one more component containing only /p and /i/h */
2119 [ - + ]: 38 : if ((nModeProtonIsoExchgH[iINChI] == MODE_PIXH_ADD_A_PIXH_COMPONENT &&
2120 [ - - + + ]: 38 : OneInput->nNumComponents[iINChI][TAUT_YES]) ||
2121 : : /* always add one deleted component if no non-taut InChI is available */
2122 [ + - ]: 6 : (bInChI2Structure && !bAvailableProtonsForEachComponent &&
2123 [ + - ]: 6 : !OneInput->nNumComponents[iINChI][TAUT_NON] &&
2124 [ + + ]: 6 : OneInput->nNumComponents[iINChI][TAUT_YES])) /* djb-rwth: addressing LLVM warnings */
2125 : : {
2126 : 3 : int nPrevLen, nLen = 0;
2127 : 3 : j = TAUT_YES;
2128 : 3 : nPrevLen = OneInput->nNumComponents[iINChI][j];
2129 [ + + ]: 6 : for (k = 0; k < nPrevLen; k++)
2130 : : {
2131 : 3 : nLen += !OneInput->pInpInChI[iINChI][j][k].bDeleted;
2132 : : }
2133 [ + - ]: 3 : if (nLen == nPrevLen)
2134 : : {
2135 : : /* add one more component */
2136 : 3 : INChI* pInChI = (INChI*)inchi_calloc((long long)nLen + 1, sizeof(*pInChI)); /* djb-rwth: cast operator added */
2137 [ - + ]: 3 : if (!pInChI)
2138 : : {
2139 : 0 : ret2 = RI_ERR_ALLOC;
2140 : 0 : goto exit_error;
2141 : : }
2142 : 3 : memcpy(pInChI, OneInput->pInpInChI[iINChI][j], nLen * sizeof(*pInChI));
2143 [ + - ]: 3 : inchi_free(OneInput->pInpInChI[iINChI][j]);
2144 : 3 : OneInput->pInpInChI[iINChI][j] = pInChI;
2145 : : }
2146 : 3 : OneInput->nNumComponents[iINChI][j] = nLen + 1;
2147 : :
2148 [ - + ]: 3 : for (k = nLen; k < nPrevLen; k++)
2149 : : {
2150 : 0 : Free_INChI_Members(&OneInput->pInpInChI[iINChI][j][k]);
2151 : 0 : memset(&OneInput->pInpInChI[iINChI][j][k], 0, sizeof(OneInput->pInpInChI[iINChI][j][k])); /* djb-rwth: memset_s C11/Annex K variant? */
2152 : : }
2153 : : /* mark the last component as a proton */
2154 [ - + ]: 3 : if (0 > (ret2 = nFillOutProtonMobileH(OneInput->pInpInChI[iINChI][j] + nLen)))
2155 : : {
2156 : 0 : goto exit_error;
2157 : : }
2158 : : }
2159 : : INCHI_HEAPCHK
2160 : :
2161 : : /* remove unneeded Stereo and/or Fixed H */
2162 [ - + ]: 38 : if (!bReqStereo)
2163 : : {
2164 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
2165 : : {
2166 [ # # ]: 0 : for (k = 0; k < OneInput->nNumComponents[iINChI][j]; k++)
2167 : : {
2168 [ # # ]: 0 : if (OneInput->pInpInChI[iINChI][j][k].Stereo)
2169 : : {
2170 : 0 : Free_INChI_Stereo(OneInput->pInpInChI[iINChI][j][k].Stereo);
2171 [ # # ]: 0 : inchi_free(OneInput->pInpInChI[iINChI][j][k].Stereo);
2172 : 0 : OneInput->pInpInChI[iINChI][j][k].Stereo = NULL;
2173 : : }
2174 [ # # ]: 0 : if (OneInput->pInpInChI[iINChI][j][k].StereoIsotopic)
2175 : : {
2176 : 0 : Free_INChI_Stereo(OneInput->pInpInChI[iINChI][j][k].StereoIsotopic);
2177 [ # # ]: 0 : inchi_free(OneInput->pInpInChI[iINChI][j][k].StereoIsotopic);
2178 : 0 : OneInput->pInpInChI[iINChI][j][k].StereoIsotopic = NULL;
2179 : : }
2180 : : INCHI_HEAPCHK
2181 : : }
2182 : : }
2183 : : }
2184 : :
2185 : : }
2186 : :
2187 : 19 : num_components[INCHI_BAS] = inchi_max(OneInput->nNumComponents[INCHI_BAS][TAUT_YES],
2188 : : OneInput->nNumComponents[INCHI_BAS][TAUT_NON]);
2189 : 19 : num_components[INCHI_REC] = inchi_max(OneInput->nNumComponents[INCHI_REC][TAUT_YES],
2190 : : OneInput->nNumComponents[INCHI_REC][TAUT_NON]);
2191 : :
2192 : 19 : exit_error:
2193 : 19 : return ret2;
2194 : : }
2195 : :
2196 : :
2197 : : /****************************************************************************/
2198 : 19 : int GetInChIFormulaNumH(INChI* pInChI, int* nNumH)
2199 : : { /* get number of H including bridging hydrogen atoms */
2200 : : const char* p, * q;
2201 : 19 : *nNumH = 0;
2202 [ + - ]: 19 : if (pInChI->szHillFormula)
2203 : : {
2204 [ + + ]: 38 : for (p = strchr(pInChI->szHillFormula, 'H'); p; p = strchr(p, 'H'))
2205 : : {
2206 : 19 : p++;
2207 [ + - ]: 19 : if (!islower(UCINT * p))
2208 : : {
2209 : : /* found hydrogen in the formula */
2210 [ + - ]: 19 : if (isdigit(UCINT * p))
2211 : : {
2212 : : #if ( CHECK_STRTOL_ATNUMB==1 )
2213 : 19 : int delta = (int)inchi_strtol(p, &q, 10);
2214 [ + - - + ]: 19 : if (delta > MAX_ATOMS || delta < 0)
2215 : : {
2216 : 0 : return RI_ERR_SYNTAX; /* syntax error */
2217 : : }
2218 : 19 : *nNumH += delta;
2219 : : #else
2220 : : * nNumH += (int)inchi_strtol(p, &q, 10);
2221 : : #endif
2222 : 19 : p = q;
2223 : : }
2224 : : else
2225 : : {
2226 : 0 : *nNumH += 1;
2227 : : }
2228 : : }
2229 : : }
2230 : : }
2231 : :
2232 : 19 : return 0;
2233 : : }
2234 : :
2235 : :
2236 : : /****************************************************************************/
2237 : 41 : int GetInChINumH(INChI* pInChI, int* nNumH)
2238 : : {
2239 : : int i, j, nNumTautGroups, iTautGroup, nTautGroupLen, lenTautomer;
2240 : 41 : *nNumH = 0;
2241 [ + + ]: 151 : for (i = 0; i < pInChI->nNumberOfAtoms; i++)
2242 : : {
2243 : 110 : *nNumH += (pInChI->nAtom[i] == EL_NUMBER_H); /* bridging H */
2244 : 110 : *nNumH += pInChI->nNum_H[i];
2245 : : }
2246 : : /* earlier nNum_H_fixed[] should have been added to pInChI->nNum_H[] */
2247 : : /*
2248 : : if ( pInChI->nNum_H_fixed ) {
2249 : : for ( i = 0; i < pInChI->nNumberOfAtoms; i ++ ) {
2250 : : *nNumH += pInChI->nNum_H_fixed[i];
2251 : : }
2252 : : }
2253 : : */
2254 [ + + + - ]: 41 : if (pInChI->lenTautomer > 3 && pInChI->nTautomer)
2255 : : {
2256 : 6 : lenTautomer = pInChI->lenTautomer;
2257 : 6 : j = 0;
2258 : 6 : nNumTautGroups = pInChI->nTautomer[j++];
2259 [ + + + - ]: 12 : for (iTautGroup = 0; j < lenTautomer && iTautGroup < nNumTautGroups; iTautGroup++, j += nTautGroupLen)
2260 : : {
2261 : 6 : nTautGroupLen = pInChI->nTautomer[j] + 1;
2262 : 6 : *nNumH += pInChI->nTautomer[j + 1];
2263 : : }
2264 [ + - - + ]: 6 : if (iTautGroup != nNumTautGroups || j != lenTautomer)
2265 : : {
2266 : 0 : return RI_ERR_PROGR;
2267 : : }
2268 : : }
2269 [ - + - - : 41 : if (pInChI->nNum_H_fixed && (pInChI->lenTautomer || pInChI->nTautomer))
- - ]
2270 : : {
2271 : 0 : return RI_ERR_PROGR;
2272 : : }
2273 : :
2274 : 41 : return 0;
2275 : : }
2276 : :
2277 : :
2278 : : /****************************************************************************/
2279 : 0 : int GetInChIIsoH(INChI* pInChI, int nNumIsotopicH[NUM_H_ISOTOPES])
2280 : : {
2281 : : int i;
2282 [ # # ]: 0 : for (i = 0; i < NUM_H_ISOTOPES; i++)
2283 : : {
2284 : 0 : nNumIsotopicH[i] = 0;
2285 : : }
2286 [ # # ]: 0 : for (i = 0; i < pInChI->nNumberOfIsotopicAtoms; i++)
2287 : : {
2288 [ # # ]: 0 : if (pInChI->IsotopicAtom[i].nIsoDifference > 0 &&
2289 [ # # ]: 0 : pInChI->IsotopicAtom[i].nIsoDifference <= NUM_H_ISOTOPES)
2290 : : {
2291 [ # # ]: 0 : if (!pInChI->nAtom ||
2292 [ # # ]: 0 : !pInChI->IsotopicAtom[i].nAtomNumber ||
2293 [ # # ]: 0 : pInChI->IsotopicAtom[i].nAtomNumber > pInChI->nNumberOfAtoms)
2294 : : {
2295 : 0 : return RI_ERR_PROGR;
2296 : : }
2297 [ # # ]: 0 : if (pInChI->nAtom[pInChI->IsotopicAtom[i].nAtomNumber - 1] == (AT_NUMB)EL_NUMBER_H)
2298 : : {
2299 : : /* isotopic H in connection table */
2300 : 0 : nNumIsotopicH[pInChI->IsotopicAtom[i].nIsoDifference - 1]++;
2301 : : }
2302 : : }
2303 : 0 : nNumIsotopicH[0] += pInChI->IsotopicAtom[i].nNum_H;
2304 : 0 : nNumIsotopicH[1] += pInChI->IsotopicAtom[i].nNum_D;
2305 : 0 : nNumIsotopicH[2] += pInChI->IsotopicAtom[i].nNum_T;
2306 : : }
2307 : :
2308 : 0 : return 0;
2309 : : }
2310 : :
2311 : :
2312 : : /****************************************************************************/
2313 : : typedef struct tagNumElem
2314 : : {
2315 : : int num;
2316 : : /*
2317 : : int iso;
2318 : : */
2319 : : } NUM_ELEM;
2320 : :
2321 : :
2322 : : /****************************************************************************
2323 : : Read a single InChI input Line and convert to data
2324 : : ****************************************************************************/
2325 : 28 : int InChILine2Data(INCHI_IOSTREAM* pInp,
2326 : : SEGM_LINE* pLine,
2327 : : char** pStr,
2328 : : int* pState,
2329 : : int* nErr,
2330 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
2331 : : int nNumComponents[INCHI_NUM][TAUT_NUM],
2332 : : REM_PROTONS nNumProtons[INCHI_NUM][TAUT_NUM],
2333 : : int s[INCHI_NUM][TAUT_NUM][2],
2334 : : int bReadCoord,
2335 : : int bInchi2Struct,
2336 : : INCHI_MODE nMode,
2337 : : int* bStdFormat,
2338 : : int* input_has_save_opt,
2339 : : unsigned char* input_save_opt_bits,
2340 : : OAD_Polymer** ppolymer,
2341 : : OAD_V3000** pv3000)
2342 : : {
2343 : 28 : int iINChI, i, j, k, m, len1, len2, ret2 = 0, retAux = 0, stateAux = 0; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2344 : : int ret, tot_charge[INCHI_NUM][TAUT_NUM];
2345 : : int i1, i2, i3;
2346 : : int kc;
2347 : : NUM_ELEM* num_elem[INCHI_NUM][TAUT_NUM];
2348 : :
2349 : :
2350 : : #if ( FIX_I2I_STEREOCONVERSION_BUG == 1 )
2351 : : /* (2008-03-06) 1=> Fix bug of i2i conversion SAbs-->(SRel||Srac) */
2352 : : /* (converter does not placed proper stereo to output) */
2353 : :
2354 : : /* set new stereo type as requested by conversion option */
2355 : 28 : int target_stereo_type = 1;
2356 [ - + ]: 28 : if (nMode & REQ_MODE_RELATIVE_STEREO)
2357 : : {
2358 : 0 : target_stereo_type = 2;
2359 : : }
2360 [ - + ]: 28 : else if (nMode & REQ_MODE_RACEMIC_STEREO)
2361 : : {
2362 : 0 : target_stereo_type = 3;
2363 : : }
2364 : : #endif
2365 : :
2366 : 28 : memset(num_elem, 0, sizeof(num_elem)); /* djb-rwth: memset_s C11/Annex K variant? */
2367 : :
2368 : 28 : ret = ReadInChILine(pInp, pLine, pStr, pState, pInpInChI,
2369 : : nNumComponents, nNumProtons, s, bStdFormat,
2370 : : input_has_save_opt, input_save_opt_bits,
2371 : : bInchi2Struct, ppolymer, pv3000);
2372 : :
2373 : : #if ( FIX_I2I_STEREOCONVERSION_BUG == 1 )
2374 : : /* modify stereo type for layers as requested */
2375 [ - + ]: 28 : if (target_stereo_type > 1)
2376 : : {
2377 [ # # ]: 0 : for (i1 = 0; i1 < INCHI_NUM; i1++)
2378 : : {
2379 [ # # ]: 0 : for (i2 = 0; i2 < TAUT_NUM; i2++)
2380 : : {
2381 [ # # ]: 0 : for (i3 = 0; i3 < 2; i3++)
2382 : : {
2383 [ # # ]: 0 : if (s[i1][i2][i3] != 0)
2384 : : {
2385 [ # # ]: 0 : if (target_stereo_type != 1)
2386 : : {
2387 : : /* do not allow conversion SRel=>SAbs, SRac=>SAbs */
2388 : 0 : s[i1][i2][i3] = target_stereo_type;
2389 : : }
2390 : : }
2391 : : }
2392 : : }
2393 : : }
2394 : : }
2395 : : #endif
2396 : :
2397 : 28 : * nErr = 0;
2398 : :
2399 [ - + ]: 28 : if ((ret == RI_ERR_EOL) &&
2400 : 0 : nNumComponents[INCHI_BAS][TAUT_YES]
2401 [ # # # # ]: 0 : + nNumComponents[INCHI_BAS][TAUT_NON] && bReadCoord)
2402 : : {
2403 : 0 : retAux = ReadInChICoord(pInp, pLine, &stateAux, pInpInChI, nNumComponents); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2404 : : }
2405 : :
2406 [ + - + + ]: 28 : if ((ret == RI_ERR_EOL || ret == RI_ERR_EOF) &&
2407 : 19 : nNumComponents[INCHI_BAS][TAUT_YES]
2408 [ + - ]: 19 : + nNumComponents[INCHI_BAS][TAUT_NON])
2409 : : {
2410 : : /* post-processing: add omitted layers */
2411 : 19 : *pState = IST_MATERIAL_BALANCE_ERROR;
2412 [ + + ]: 57 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
2413 : : {
2414 [ + + ]: 114 : for (j = 0; j < TAUT_NUM; j++)
2415 : : {
2416 : : /* for Mobile/Fixed H (j) ... */
2417 : : int bIsotopic, bStereoType, bStereoTypeAlt;
2418 : 76 : int nMH2FH_AltInv = 0, nFH2iFH_AltInv = 0 /*, niMH2iFH_AltInv=0, nMH2iMH_AltInv=0*/;
2419 [ + - ]: 76 : int jAlt = ALT_TAUT(j);
2420 : 76 : INCHI_MODE nFlags = 0, nFlagsAlt = 0;
2421 : : /* get stereo type: ABS, REL, RAC, or nothing */
2422 : 76 : tot_charge[iINChI][j] = 0;
2423 [ + + ]: 228 : for (bIsotopic = bStereoType = bStereoTypeAlt = 0; bIsotopic < 2; bIsotopic++)
2424 : : {
2425 [ + + - + ]: 152 : if (!bStereoType || bStereoType < s[iINChI][j][bIsotopic])
2426 : : {
2427 : 151 : bStereoType = s[iINChI][j][bIsotopic];
2428 : : }
2429 [ + + - + ]: 152 : if (!bStereoTypeAlt || bStereoTypeAlt < s[iINChI][jAlt][bIsotopic])
2430 : : {
2431 : 151 : bStereoTypeAlt = s[iINChI][jAlt][bIsotopic];
2432 : : }
2433 [ + - - + ]: 152 : nFlags = bStereoType == 2 ? INCHI_FLAG_REL_STEREO : bStereoType == 3 ? INCHI_FLAG_RAC_STEREO : 0;
2434 [ + - - + ]: 152 : nFlagsAlt = bStereoTypeAlt == 2 ? INCHI_FLAG_REL_STEREO : bStereoTypeAlt == 3 ? INCHI_FLAG_RAC_STEREO : 0;
2435 : : }
2436 : : /* set stereo type to each component */
2437 : : /* add missing nNum_H and nConnTable */
2438 [ + + ]: 76 : if (nNumComponents[iINChI][j])
2439 : : {
2440 : 19 : num_elem[iINChI][j] = (NUM_ELEM*)inchi_calloc((long long)nElDataLen + 1, sizeof(num_elem[0][0][0])); /* djb-rwth: cast operator added */
2441 [ - + ]: 19 : if (!num_elem[iINChI][j])
2442 : : {
2443 : 0 : ret2 = RI_ERR_ALLOC;
2444 : 0 : goto exit_function;
2445 : : }
2446 : : }
2447 [ + + ]: 98 : for (k = 0; k < nNumComponents[iINChI][j]; k++)
2448 : : {
2449 : : /* for each component k ... */
2450 [ + - ]: 22 : if (pInpInChI[iINChI][j])
2451 : : {
2452 : 22 : INChI* pInChI = &pInpInChI[iINChI][j][k];
2453 : 44 : INChI* pInChI_Alt = (k < nNumComponents[iINChI][jAlt] &&
2454 [ # # ]: 0 : pInpInChI[iINChI][jAlt] &&
2455 : : /*pInpInChI[iINChI][jAlt]->nNumberOfAtoms)? pInpInChI[iINChI][jAlt]:NULL;*/ /* 2007-09-25 DT */
2456 [ - + - - ]: 22 : pInpInChI[iINChI][jAlt][k].nNumberOfAtoms) ? &pInpInChI[iINChI][jAlt][k] : NULL;
2457 [ - + ]: 22 : if (nFlags)
2458 : : {
2459 : 0 : pInChI->nFlags |= nFlags;
2460 : : }
2461 : : else
2462 : : {
2463 [ - + - - : 22 : if (j == TAUT_NON && !nFlags && nFlagsAlt)
- - ]
2464 : : {
2465 : 0 : pInChI->nFlags |= nFlagsAlt;
2466 : : }
2467 : : }
2468 : : /**** add empty immobile H (nNum_H) if it is missing ****/
2469 [ + + ]: 22 : if (!pInChI->nNum_H &&
2470 [ - + ]: 6 : !(pInChI->nNum_H = (S_CHAR*)inchi_calloc((long long)pInChI->nNumberOfAtoms + 1, sizeof(pInChI->nNum_H[0])))) /* djb-rwth: cast operator added */
2471 : : {
2472 : 0 : ret2 = RI_ERR_ALLOC;
2473 : 0 : goto exit_function;
2474 : : }
2475 : : /**** add single atom nConnTable if it is missing ****/
2476 [ + + + - ]: 22 : if (!pInChI->nConnTable && pInpInChI[iINChI][TAUT_YES]) /* djb-rwth: fixing a NULL pointer dereference */
2477 : 6 : {
2478 : : AT_NUMB* pCT;
2479 : : int lenCT;
2480 [ - + - - ]: 6 : if (j == TAUT_NON && k < nNumComponents[iINChI][TAUT_YES] &&
2481 [ # # ]: 0 : (pCT = pInpInChI[iINChI][TAUT_YES][k].nConnTable) &&
2482 [ # # ]: 0 : (lenCT = pInpInChI[iINChI][TAUT_YES][k].lenConnTable) > 0)
2483 : : {
2484 [ # # ]: 0 : if (!(pInChI->nConnTable = (AT_NUMB*)inchi_calloc((long long)lenCT + 1, sizeof(pInChI->nConnTable[0])))) /* djb-rwth: cast operator added */
2485 : : {
2486 : 0 : ret2 = RI_ERR_ALLOC;
2487 : 0 : goto exit_function;
2488 : : }
2489 : 0 : memcpy(pInChI->nConnTable, pCT, lenCT * sizeof(pInChI->nConnTable[0]));
2490 : 0 : pInChI->lenConnTable = lenCT;
2491 : : }
2492 : : else
2493 : : {
2494 [ + - - + ]: 6 : if (j == TAUT_YES && pInChI->nNumberOfAtoms > 1)
2495 : : {
2496 [ # # ]: 0 : *pState = IST_MOBILE_H_CONNECTIONS + (iINChI == INCHI_REC ? IST_HAPPENED_IN_RECMET : 0);
2497 : 0 : ret2 = RI_ERR_SYNTAX;
2498 : 0 : goto exit_function;
2499 : : }
2500 [ - + ]: 6 : if (!(pInChI->nConnTable = (AT_NUMB*)inchi_calloc((long long)pInChI->nNumberOfAtoms + 1, sizeof(pInChI->nConnTable[0])))) /* djb-rwth: cast operator added */
2501 : : {
2502 : 0 : ret2 = RI_ERR_ALLOC;
2503 : 0 : goto exit_function;
2504 : : }
2505 : 6 : pInChI->lenConnTable = 1;
2506 : 6 : pInChI->nConnTable[0] = 1;
2507 : : }
2508 : : }
2509 : : else
2510 : : {
2511 [ + - - + : 16 : if (pInChI->nConnTable && !pInChI->lenConnTable && pInChI->nNumberOfAtoms == 1)
- - ]
2512 : : {
2513 : 0 : pInChI->nConnTable[0] = 1;
2514 : 0 : pInChI->lenConnTable = 1;
2515 : : }
2516 : : }
2517 : : /**** copy charge: Mobile H --> Fixed H; ****/
2518 [ - + ]: 22 : if (j == TAUT_NON)
2519 : : {
2520 : : /*
2521 : : if ( pInChI->nTotalCharge == NO_VALUE_INT )
2522 : : {
2523 : : pInChI->nTotalCharge = 0;
2524 : : }
2525 : : else
2526 : : */
2527 [ # # # # : 0 : if (!pInChI->nTotalCharge && k < nNumComponents[iINChI][TAUT_YES] && pInpInChI[iINChI][TAUT_YES]) /* djb-rwth: fixing a NULL pointer dereference */
# # ]
2528 : : {
2529 : 0 : INChI* pAltInChI = &pInpInChI[iINChI][TAUT_YES][k]; /* Mobile H InChI */
2530 [ # # # # ]: 0 : if (pAltInChI->nTotalCharge && pAltInChI->nTotalCharge != NO_VALUE_INT)
2531 : : {
2532 : 0 : pInChI->nTotalCharge = pAltInChI->nTotalCharge;
2533 : : }
2534 : : }
2535 : : }
2536 : : /***** Fixed H: add pInChI->nNum_H_fixed to pInChI->nNum_H ****/
2537 [ - + - - : 22 : if (j == TAUT_NON && pInChI->nNum_H && pInChI->nNum_H_fixed)
- - ]
2538 : : {
2539 [ # # ]: 0 : for (m = 0; m < pInChI->nNumberOfAtoms; m++)
2540 : : {
2541 : 0 : pInChI->nNum_H[m] += pInChI->nNum_H_fixed[m];
2542 : : }
2543 : : }
2544 : : /***** copy isotopic atoms: Mobile H --> Fixed H ******/
2545 [ + - + + ]: 22 : if (j == TAUT_YES && pInChI->nNumberOfIsotopicAtoms &&
2546 [ - + ]: 2 : k < nNumComponents[iINChI][TAUT_NON])
2547 : : {
2548 : 0 : INChI* pAltInChI = &pInpInChI[iINChI][TAUT_NON][k]; /* Fixed H InChI */
2549 : :
2550 [ # # ]: 0 : if (!pAltInChI->nNumberOfIsotopicAtoms)
2551 : : {
2552 : 0 : ret2 = CopySegment(pAltInChI, pInChI, CPY_ISO_AT, 0, 0);
2553 [ # # ]: 0 : if (ret2 < 0)
2554 : : {
2555 : 0 : goto exit_function;
2556 : : }
2557 : : }
2558 : : }
2559 : : /**** copy coordinates: Mobile H --> Fixed H ******/
2560 [ + - - + ]: 22 : if (j == TAUT_YES && pInChI->IsotopicTGroup &&
2561 [ # # ]: 0 : k < nNumComponents[iINChI][TAUT_NON])
2562 : : {
2563 : 0 : INChI* pAltInChI = &pInpInChI[iINChI][TAUT_NON][k]; /* Fixed H InChI */
2564 : :
2565 [ # # ]: 0 : if (!pAltInChI->IsotopicTGroup)
2566 : : {
2567 : 0 : XYZ_COORD* pxyz = (XYZ_COORD*)inchi_calloc(pInChI->nNumberOfAtoms, sizeof(pxyz[0]));
2568 [ # # ]: 0 : if (pxyz)
2569 : : {
2570 : 0 : memcpy(pxyz, pInChI->IsotopicTGroup, pInChI->nNumberOfAtoms * sizeof(pxyz[0]));
2571 : 0 : pAltInChI->IsotopicTGroup = (INChI_IsotopicTGroup*)pxyz;
2572 : : }
2573 : : else
2574 : : {
2575 : 0 : ret2 = RI_ERR_ALLOC;
2576 : 0 : goto exit_function;
2577 : : }
2578 : : }
2579 : : }
2580 : :
2581 : : /********************************************************
2582 : : * *
2583 : : * Restore omitted stereo seqments *
2584 : : * *
2585 : : * order of restoring: *
2586 : : * *
2587 : : * 1. Fixed H (F) -> (FI) Isotopic Fixed H *
2588 : : * 2. Mobile H (M) -> (F) Fixed H *
2589 : : * 3. Isotopic Mobile H (MI) -> (FI) Isotopic Fixed H *
2590 : : * 4. Mobile H (M) -> (MI) Isotopic Mobile H *
2591 : : * *
2592 : : ********************************************************/
2593 : :
2594 : : /***** (4) copy stereo: Mobile H --> isotopic Mobile H ******/
2595 [ + - ]: 22 : if (j == TAUT_YES)
2596 : : {
2597 : 64 : int bIso = pInChI->nNumberOfIsotopicAtoms ||
2598 [ - + ]: 20 : (pInChI->StereoIsotopic &&
2599 : 0 : pInChI->StereoIsotopic->nNumberOfStereoCenters
2600 [ + + - - : 42 : + pInChI->StereoIsotopic->nNumberOfStereoBonds) ||
- + ]
2601 [ # # ]: 0 : (pInChI_Alt && pInChI_Alt->nNumberOfIsotopicAtoms); /* djb-rwth: addressing LLVM warning */
2602 : :
2603 : : /* non-isotopic Mobile H => isotopic Mobile H */
2604 [ + + ]: 22 : if (bIso)
2605 : : {
2606 [ - + - - ]: 2 : if (pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters &&
2607 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->t_parity))
2608 : : {
2609 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3, 1, 0)) ||
2610 [ # # # # : 0 : ((!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs) &&
# # ]
2611 : 0 : 0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3_M, 1, 0)))) /* djb-rwth: addressing LLVM warning */
2612 : : {
2613 : 0 : goto exit_function;
2614 : : }
2615 [ # # ]: 0 : if ((nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
2616 : : {
2617 [ # # ]: 0 : if (pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT)
2618 : : {
2619 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = s[iINChI][j][0] > 0 ? 2 : 0;
2620 : : }
2621 [ # # ]: 0 : if (pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
2622 : : {
2623 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs = s[iINChI][j][1] > 0 ? 2 : 0;
2624 : : }
2625 : : }
2626 : : }
2627 : : else
2628 : : {
2629 : : /* copy sp3 inversion info: non-isotopic Mobile H => isotopic Mobile H */
2630 [ - + - - ]: 2 : if (pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters &&
2631 [ # # # # ]: 0 : pInChI->StereoIsotopic && pInChI->StereoIsotopic->nNumberOfStereoCenters &&
2632 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs)
2633 : : {
2634 [ # # ]: 0 : if ((nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
2635 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT &&
2636 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
2637 : : {
2638 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = s[iINChI][j][0] > 0 ? 2 : 0;
2639 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs = s[iINChI][j][1] > 0 ? 2 : 0;
2640 : : }
2641 : : else
2642 : : {
2643 [ # # # # ]: 0 : if (!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs)
2644 : : {
2645 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = pInChI->Stereo->nCompInv2Abs;
2646 : : }
2647 : : }
2648 : : }
2649 : : }
2650 : : }
2651 [ + + ]: 22 : if (bIso &&
2652 [ - + - - ]: 2 : pInChI->Stereo && pInChI->Stereo->nNumberOfStereoBonds &&
2653 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->b_parity))
2654 : : {
2655 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP2, 1, 0)))
2656 : : {
2657 : 0 : goto exit_function;
2658 : : }
2659 : : }
2660 : : }
2661 : : /***** (0) set nCompInv2Abs to Fixed-H *********************************/
2662 [ - + ]: 22 : if (j == TAUT_NON)
2663 : : {
2664 [ # # # # ]: 0 : if (pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters &&
2665 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT)
2666 : : {
2667 : : /* case of /sN and /t... in non-isotopic Mobile-H, no /s in non-isotopic Fixed-H */
2668 [ # # # # : 0 : if (!s[iINChI][j][0] && s[iINChI][jAlt][0] > 0 && /* /sN is not present in F and is present in M */
# # ]
2669 [ # # # # ]: 0 : pInChI_Alt && pInChI_Alt->Stereo && pInChI_Alt->Stereo->nNumberOfStereoCenters)
2670 : : {
2671 : : /* inherit from Mobile-H */
2672 : : /* /s1 in M and MI; /m1 or /m0 in MI; /m. in M; no /m in F. Inherit MI->FI. Added 10-15-2007 */
2673 [ # # ]: 0 : if (pInChI_Alt->Stereo->nCompInv2Abs == 0 && /* M: /m. ; means no /m for this component */
2674 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT && /* F: no /m segment for all components */
2675 [ # # ]: 0 : pInChI_Alt->StereoIsotopic && /* MI: present */
2676 [ # # ]: 0 : pInChI_Alt->StereoIsotopic->nCompInv2Abs != 0 &&
2677 [ # # ]: 0 : pInChI_Alt->StereoIsotopic->nCompInv2Abs != NO_VALUE_INT && /* MI: /m0 or /m1 */
2678 [ # # # # ]: 0 : !s[iINChI][j][0] && !s[iINChI][j][1] && /* F, FI: no /s */
2679 [ # # # # ]: 0 : s[iINChI][jAlt][0] == 1 && s[iINChI][jAlt][1] == 1 /* M, MI: /s1 and /s1 */
2680 : : )
2681 : : {
2682 : : /* copy /m from MI to FI */
2683 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI_Alt, CPY_SP3_M, 1, 1)))
2684 : : {
2685 : 0 : goto exit_function;
2686 : : }
2687 : : }
2688 : : else
2689 : : {
2690 : : /* the following if(){...} was added to fix m1 bug 2007-09-25 DT */
2691 [ # # # # ]: 0 : if (pInChI_Alt->Stereo->nCompInv2Abs != NO_VALUE_INT && s[iINChI][jAlt][0] == 1)
2692 : : {
2693 : 0 : pInChI->Stereo->nCompInv2Abs = pInChI_Alt->Stereo->nCompInv2Abs;
2694 : : }
2695 : : else
2696 : : {
2697 : : /* M and MI contain /sN and /sN, N=2,3. Added 10-15-2007 */
2698 [ # # ]: 0 : if (pInChI_Alt->Stereo->nCompInv2Abs == NO_VALUE_INT &&
2699 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT &&
2700 [ # # # # ]: 0 : !s[iINChI][j][0] && !s[iINChI][j][1] &&
2701 [ # # ]: 0 : (s[iINChI][jAlt][0] & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
2702 [ # # ]: 0 : (s[iINChI][jAlt][1] & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
2703 : 0 : {
2704 : 0 : int bIso = pInChI->nNumberOfIsotopicAtoms ||
2705 [ # # ]: 0 : (pInChI->StereoIsotopic &&
2706 : 0 : pInChI->StereoIsotopic->nNumberOfStereoCenters
2707 [ # # # # : 0 : + pInChI->StereoIsotopic->nNumberOfStereoBonds) ||
# # ]
2708 [ # # ]: 0 : (pInChI_Alt && pInChI_Alt->nNumberOfIsotopicAtoms); /* djb-rwth: addressing LLVM warning */
2709 [ # # ]: 0 : if (bIso)
2710 : : {
2711 [ # # # # ]: 0 : if (!pInChI_Alt->StereoIsotopic && /* create zero/NULL-initialized pInChI_Alt->StereoIsotopic */
2712 : 0 : 0 > (ret2 = CopySegment(pInChI_Alt, pInChI_Alt, CPY_SP3_M, 1, -1)))
2713 : : {
2714 : 0 : goto exit_function;
2715 : : }
2716 : 0 : pInChI_Alt->StereoIsotopic->nCompInv2Abs = 2; /* MI: /m1 or /m0 */
2717 : 0 : pInChI_Alt->Stereo->nCompInv2Abs = 0; /* M: /m. ; no /m for this component */
2718 : 0 : pInChI->Stereo->nCompInv2Abs = NO_VALUE_INT + 1; /* FI: Stereo->CompInv2Abs=0, StereoIsotopic->CompInv2Abs=1 or -1 */
2719 : : }
2720 : : else
2721 : : {
2722 : 0 : pInChI->Stereo->nCompInv2Abs = 2; /* F: /m1 or /m0, omitted from InChI as a repetition */
2723 : 0 : pInChI_Alt->Stereo->nCompInv2Abs = 2; /* M: /m1 or /m0; in Srel/SRac case the value = 2 */
2724 : : }
2725 : : }
2726 : : else
2727 : : {
2728 : 0 : pInChI->Stereo->nCompInv2Abs = 2; /* F: /m1 or /m0, omitted from InChI as a repetition */
2729 : 0 : pInChI_Alt->Stereo->nCompInv2Abs = 2; /* M: /m1 or /m0; in Srel/SRac case the value = 2 */
2730 : : }
2731 : : }
2732 : : }
2733 : : }
2734 : : else
2735 : : {
2736 : : /* case of /sN in Isotopic Fixed-H only, /t... in Fixed-H, no /m (2007-08-27 DT) */
2737 [ # # # # ]: 0 : if (!s[iINChI][j][0] && !s[iINChI][jAlt][0] && /* /sN in Fixed-H isotopic only */
2738 [ # # ]: 0 : (nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
2739 [ # # # # : 0 : !(pInChI->StereoIsotopic && pInChI->StereoIsotopic->nNumberOfStereoCenters) &&
# # ]
2740 : : /*!(pInChI_Alt && pInChI_Alt->Stereo && pInChI_Alt->Stereo->nNumberOfStereoCenters) &&*/
2741 [ # # # # ]: 0 : !(pInChI_Alt && pInChI_Alt->StereoIsotopic && pInChI_Alt->StereoIsotopic->nNumberOfStereoCenters))
2742 : : {
2743 : 0 : pInChI->Stereo->nCompInv2Abs = NO_VALUE_INT + 1; /* Stereo->CompInv2Abs=0, StereoIsotopic->CompInv2Abs=1 or -1 */
2744 : : }
2745 : : else
2746 : : {
2747 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = s[iINChI][j][0] > 0 ? 2 : 0;
2748 : : }
2749 : : }
2750 : : }
2751 : : }
2752 : :
2753 : : /***** (1) copy stereo: non-isotopic Fixed H --> isotopic Fixed H ******/
2754 [ - + ]: 22 : if (j == TAUT_NON)
2755 : : {
2756 : 0 : int bIso = pInChI->nNumberOfIsotopicAtoms ||
2757 [ # # ]: 0 : (pInChI->StereoIsotopic &&
2758 : 0 : pInChI->StereoIsotopic->nNumberOfStereoCenters
2759 [ # # # # : 0 : + pInChI->StereoIsotopic->nNumberOfStereoBonds) ||
# # ]
2760 [ # # ]: 0 : (pInChI_Alt && pInChI_Alt->nNumberOfIsotopicAtoms); /* djb-rwth: addressing LLVM warning */
2761 : : /* non-isotopic Fixed H => isotopic Fixed H */
2762 [ # # ]: 0 : if (bIso)
2763 : : {
2764 [ # # # # ]: 0 : if (pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters &&
2765 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->t_parity))
2766 : : {
2767 : : /* -- replaced 2007-08-27 by (aaa), see below -- DT
2768 : : if ( 0 > (ret2 = CopySegment( pInChI, pInChI, CPY_SP3, 1, 0)) ||
2769 : : !(pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs) &&
2770 : : 0 > (ret2 = CopySegment( pInChI, pInChI, CPY_SP3_M, 1, 0))) {
2771 : : goto exit_function;
2772 : : }
2773 : : */
2774 : : /*----------- replacement (aaa) begin 2007-08-27 DT */
2775 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3, 1, 0)))
2776 : : {
2777 : 0 : goto exit_function;
2778 : : }
2779 [ # # ]: 0 : if (pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT + 1)
2780 : : {
2781 : 0 : pInChI->Stereo->nCompInv2Abs = 0;
2782 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 2;
2783 : : }
2784 : : else
2785 : : {
2786 [ # # # # : 0 : if (!(pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs) &&
# # ]
2787 : 0 : 0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3_M, 1, 0)))
2788 : : {
2789 : 0 : goto exit_function;
2790 : : }
2791 : : }
2792 : : /*----------- replacement (aaa) end 2007-08-27 DT */
2793 [ # # ]: 0 : if ((nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
2794 : : {
2795 [ # # ]: 0 : if (pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT)
2796 : : {
2797 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = s[iINChI][j][0] > 0 ? 2 : 0;
2798 : : }
2799 [ # # ]: 0 : if (pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
2800 : : {
2801 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs = s[iINChI][j][1] > 0 ? 2 : 0;
2802 : : }
2803 : : }
2804 : : #ifdef NEVER
2805 : : if ((nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
2806 : : !s[iINChI][j][0] && s[iINChI][j][0] > 0)
2807 : : {
2808 : : /* copied Rel/Rac stereo to Iso; /s is in Iso /s is not in non-Iso */
2809 : : /* this means all difference in stereo is in inversion */
2810 : : if (pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT &&
2811 : : pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
2812 : : {
2813 : : pInChI->Stereo->nCompInv2Abs = 0; /* missing */
2814 : : pInChI->StereoIsotopic->nCompInv2Abs = 2; /* unusual value */
2815 : : }
2816 : : }
2817 : : #endif
2818 : : }
2819 : : else
2820 : : {
2821 : : /* copy sp3 inversion info: non-isotopic Fixed H --> isotopic Fixed H */
2822 [ # # # # ]: 0 : if (pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters &&
2823 [ # # # # ]: 0 : pInChI->StereoIsotopic && pInChI->StereoIsotopic->nNumberOfStereoCenters &&
2824 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs)
2825 : : {
2826 [ # # ]: 0 : if ((nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
2827 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT &&
2828 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
2829 : : {
2830 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = s[iINChI][j][0] > 0 ? 2 : 0;
2831 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs = s[iINChI][j][1] > 0 ? 2 : 0;
2832 : : }
2833 : : else
2834 : : {
2835 [ # # # # ]: 0 : if (!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs)
2836 : : {
2837 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = pInChI->Stereo->nCompInv2Abs;
2838 : : }
2839 : : }
2840 : : }
2841 : : }
2842 : : }
2843 [ # # ]: 0 : if (bIso &&
2844 [ # # # # ]: 0 : pInChI->Stereo && pInChI->Stereo->nNumberOfStereoBonds &&
2845 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->b_parity))
2846 : : {
2847 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP2, 1, 0)))
2848 : : {
2849 : 0 : goto exit_function;
2850 : : }
2851 : : }
2852 : : }
2853 : :
2854 : : /***** copy stereo: Mobile H --> Fixed H ******/
2855 [ - + - - ]: 22 : if (j == TAUT_NON && k < nNumComponents[iINChI][TAUT_YES])
2856 : : {
2857 : 0 : INChI* pAltInChI = &pInpInChI[iINChI][TAUT_YES][k]; /* Mobile H InChI */
2858 : 0 : int bIso = pInChI->nNumberOfIsotopicAtoms ||
2859 [ # # ]: 0 : (pInChI->StereoIsotopic &&
2860 : 0 : pInChI->StereoIsotopic->nNumberOfStereoCenters
2861 [ # # # # : 0 : + pInChI->StereoIsotopic->nNumberOfStereoBonds) ||
# # ]
2862 : 0 : (pAltInChI && (
2863 [ # # ]: 0 : pAltInChI->nNumberOfIsotopicAtoms ||
2864 [ # # ]: 0 : (pAltInChI->StereoIsotopic &&
2865 : 0 : pAltInChI->StereoIsotopic->nNumberOfStereoCenters
2866 [ # # ]: 0 : + pAltInChI->StereoIsotopic->nNumberOfStereoBonds))); /* djb-rwth: addressing LLVM warning */
2867 [ # # # # ]: 0 : int bNo_InChI_t = (!pInChI->Stereo || !pInChI->Stereo->t_parity);
2868 [ # # # # ]: 0 : int bNo_InChI_m = (!pInChI->Stereo || NO_VALUE_INT == pInChI->Stereo->nCompInv2Abs);
2869 : :
2870 : : /* (2) non-isotopic Mobile H => non-isotopic Fixed H */
2871 [ # # # # ]: 0 : if (pAltInChI->Stereo && pAltInChI->Stereo->nNumberOfStereoCenters &&
2872 [ # # # # ]: 0 : (!pInChI->Stereo || !pInChI->Stereo->t_parity))
2873 : 0 : {
2874 : : #if ( FIX_I2I_STEREOCONVERSION_BUG2 == 1 )
2875 : : /* (2008-04-02) 1=> Fix bug of i2i conversion SAbs-->(SRel||Srac) */
2876 : : /* (converter skipped empty '/t' or sometimes produced an excess one */
2877 : :
2878 : : /* check whether t stereo is actually present */
2879 : 0 : int bHave_t_stereo = 1;
2880 [ # # ]: 0 : if (pInChI->Stereo)
2881 : 0 : bHave_t_stereo = pInChI->Stereo->nNumberOfStereoCenters;
2882 : : /* account for stereobonds present */
2883 [ # # ]: 0 : if (bHave_t_stereo < 1)
2884 [ # # ]: 0 : if (pInChI->Stereo->nNumberOfStereoBonds > 0)
2885 : 0 : bHave_t_stereo = 1;
2886 : : /* copy stereo anyway ... */
2887 : : #endif
2888 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3, 0, 0)) ||
2889 [ # # # # : 0 : ((!pInChI->Stereo->nCompInv2Abs || NO_VALUE_INT == pInChI->Stereo->nCompInv2Abs) &&
# # ]
2890 : 0 : 0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3_M, 0, 0)))) /* djb-rwth: addressing LLVM warning */
2891 : : {
2892 : 0 : goto exit_function;
2893 : : }
2894 : :
2895 : : #if ( FIX_I2I_STEREOCONVERSION_BUG2 == 1 )
2896 : : /* ... correct just copied stereo if applicable */
2897 [ # # # # ]: 0 : if ((s[iINChI][j][0] < 1) &&
2898 : 0 : (bHave_t_stereo < 1) &&
2899 [ # # ]: 0 : (pAltInChI->Stereo->nNumberOfStereoCenters > 0) &&
2900 [ # # ]: 0 : (s[iINChI][jAlt][0] < 1))
2901 : : {
2902 : : /* (2010-02-28) if not all stereo centers are unknown/undefined */
2903 : : /* at which condition stereo still should present .. */
2904 : 0 : int all_UU = 1;
2905 [ # # ]: 0 : for (kc = 0; kc < pAltInChI->Stereo->nNumberOfStereoCenters; kc++)
2906 : : {
2907 [ # # ]: 0 : if ((pAltInChI->Stereo->t_parity[kc] != AB_PARITY_UNKN) &&
2908 [ # # ]: 0 : (pAltInChI->Stereo->t_parity[kc] != AB_PARITY_UNDF))
2909 : : {
2910 : 0 : all_UU = 0;
2911 : 0 : break;
2912 : : }
2913 : : }
2914 [ # # ]: 0 : if (!all_UU)
2915 : 0 : pInChI->Stereo->nNumberOfStereoCenters = 0;
2916 : : }
2917 : : #endif
2918 : :
2919 : : /* in case of missing nCompInv2Abs, 2005-05-10 */
2920 [ # # ]: 0 : if ((pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT) &&
2921 [ # # ]: 0 : (nFlagsAlt & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
2922 : : {
2923 [ # # # # ]: 0 : if (s[iINChI][jAlt][0] > 0 && s[iINChI][j][0] > 0)
2924 : : {
2925 : : /* suppose once in a while only non-taut stereo changes if inverted */
2926 [ # # ]: 0 : pAltInChI->Stereo->nCompInv2Abs = (++nMH2FH_AltInv) % 2 ? 2 : 0;
2927 : 0 : pInChI->Stereo->nCompInv2Abs = 2;
2928 : : }
2929 : : else
2930 : : {
2931 : : /* Mobile-H: /t.. /sN; Mobile-H isotopic: /sN (n=2 or 3), not /t...; Fixed-H layer is present, has no /t, no /i/t */
2932 : : /* Mobile-H /sN was caused by another component that would have same /mN in all layers */
2933 : : /* therefore, in case of Abs. Stereo, Mobile-H stereo isotopic stereo would have /m1 */
2934 : : /* In case of Rel/Rac stereo, since no /m1 could occur in Mobile-H isotopic, */
2935 : : /* no pAltInChI->StereoIsotopic or pInChI->StereoIsotopic have been created yet. */
2936 : : /* added 10-11-2007 to fix i2i bug for Rel/Rac stereo */
2937 [ # # # # ]: 0 : if (nNumComponents[iINChI][j] > 1 &&
2938 [ # # # # ]: 0 : bNo_InChI_t && bNo_InChI_m /* no /t... or /mN in Fixed-H */ && !nFlags &&
2939 [ # # # # ]: 0 : !(pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->t_parity) &&
2940 [ # # # # ]: 0 : !(pInChI->StereoIsotopic && pInChI->StereoIsotopic->t_parity) &&
2941 [ # # # # ]: 0 : s[iINChI][j][0] == 0 && s[iINChI][j][1] == 0 &&
2942 : : /* /sN, N=2 or 3 only in Mobile-H AND Mobile-H isotopic */
2943 [ # # ]: 0 : (s[iINChI][jAlt][0] & ((INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO))) &&
2944 [ # # ]: 0 : (s[iINChI][jAlt][1] & ((INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO))))
2945 : : {
2946 [ # # ]: 0 : if (bIso)
2947 : : {
2948 : : /* create two zero/NULL-initialized isotopic stereo if they do not exist */
2949 [ # # # # ]: 0 : if ((!pInChI->StereoIsotopic && 0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3_M, 1, -1)))
2950 : : /* -- the following will be created later, in TAUT_YES part of the code -- */
2951 [ # # # # ]: 0 : || (!pAltInChI->StereoIsotopic && 0 > (ret2 = CopySegment(pAltInChI, pAltInChI, CPY_SP3_M, 1, -1)))) /* djb-rwth: addressing LLVM warnings */ /* djb-rwth: addressing coverity ID #499533 -- unresolved issue -- revision required */
2952 : : {
2953 : 0 : goto exit_function;
2954 : : }
2955 : : /* same value = 2 for MI and FI; here we assign only FI */
2956 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 2;
2957 : 0 : pInChI->Stereo->nCompInv2Abs = 0;
2958 : : /* -- the following will NOT be assigned later, in TAUT_YES part of the code -- */
2959 : 0 : pAltInChI->StereoIsotopic->nCompInv2Abs = 2;
2960 : 0 : pAltInChI->Stereo->nCompInv2Abs = 0;
2961 : : /* */
2962 : : }
2963 : : else
2964 : : {
2965 [ # # ]: 0 : if (NO_VALUE_INT == pInChI->Stereo->nCompInv2Abs &&
2966 [ # # ]: 0 : NO_VALUE_INT == pAltInChI->Stereo->nCompInv2Abs)
2967 : : {
2968 : 0 : pInChI->Stereo->nCompInv2Abs = 2;
2969 : 0 : pAltInChI->Stereo->nCompInv2Abs = 2;
2970 : : }
2971 : : }
2972 : : }
2973 : : else
2974 : : {
2975 [ # # # # : 0 : if ((s[iINChI][jAlt][0] > 0 || s[iINChI][j][0] > 0) && s[iINChI][j][0] >= 0)
# # ]
2976 : : {
2977 : 0 : pInChI->Stereo->nCompInv2Abs = 2;
2978 : : }
2979 : : else
2980 : : {
2981 : : /* Mobile-H: /t..., no /sN; Mobile-H isotopic: /s2 or /s3, not /t; Fixed-H layer is present, has no /t, no /i/t */
2982 : : /* therefore, in case of Abs. Stereo, Mobile-H stereo isotopic stereo would have /m1 */
2983 : : /* In case of Rel/Rac stereo, since no /m1 could occur in Mobile-H isotopic, */
2984 : : /* no pAltInChI->StereoIsotopic or pInChI->StereoIsotopic have been created yet. */
2985 : : /* added 10-10-2007 to fix i2i bug for Rel/Rac stereo */
2986 [ # # # # : 0 : if (bIso && bNo_InChI_t && bNo_InChI_m /* no /t... or /mN in Fixed-H */ && !nFlags &&
# # # # ]
2987 [ # # # # ]: 0 : !(pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->t_parity) &&
2988 [ # # # # ]: 0 : !(pInChI->StereoIsotopic && pInChI->StereoIsotopic->t_parity) &&
2989 [ # # # # : 0 : s[iINChI][jAlt][0] == 0 && s[iINChI][j][0] == 0 && s[iINChI][j][1] == 0 &&
# # ]
2990 : : /* /sN, N=2 or 3 only in Mobile-H isotopic */
2991 [ # # ]: 0 : (s[iINChI][jAlt][1] & ((INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO))))
2992 : : {
2993 : : /* create two zero/NULL-initialized isotopic stereo if they do not exist */
2994 [ # # # # ]: 0 : if (!pInChI->StereoIsotopic && 0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3_M, 1, -1))
2995 : : /* -- the following will be created later, in TAUT_YES part of the code -- */
2996 : : /*|| !pAltInChI->StereoIsotopic && 0 > (ret2 = CopySegment( pAltInChI, pAltInChI, CPY_SP3_M, 1, -1))*/)
2997 : : {
2998 : 0 : goto exit_function;
2999 : : }
3000 : : /* same value = 2 for MI and FI; here we assign only FI */
3001 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 2;
3002 : 0 : pInChI->Stereo->nCompInv2Abs = 0;
3003 : : /* -- the following will be assigned later, in TAUT_YES part of the code -- */
3004 : : /*
3005 : : pAltInChI->StereoIsotopic->nCompInv2Abs = 2;
3006 : : pAltInChI->Stereo->nCompInv2Abs = 0;
3007 : : */
3008 : : }
3009 : : else
3010 : : {
3011 : 0 : pInChI->Stereo->nCompInv2Abs = 0;
3012 : : }
3013 : : }
3014 : : }
3015 : : }
3016 [ # # ]: 0 : if (!(pInChI->nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
3017 : : {
3018 : 0 : pInChI->nFlags |= ((nFlagsAlt | nFlags) & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO));
3019 : : }
3020 : : }
3021 : : }
3022 : : else
3023 : : {
3024 : : /* copy sp3 inversion info: non-isotopic Mobile H => non-isotopic Fixed H */
3025 [ # # # # ]: 0 : if (pAltInChI->Stereo && pAltInChI->Stereo->nNumberOfStereoCenters &&
3026 [ # # # # ]: 0 : pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters &&
3027 [ # # ]: 0 : pAltInChI->Stereo->nCompInv2Abs &&
3028 [ # # # # ]: 0 : (!pInChI->Stereo->nCompInv2Abs || NO_VALUE_INT == pInChI->Stereo->nCompInv2Abs))
3029 : : {
3030 [ # # # # : 0 : if (!(nFlagsAlt && !nFlags) || NO_VALUE_INT == pInChI->Stereo->nCompInv2Abs)
# # ]
3031 : : {
3032 : : /* ??? */
3033 : 0 : pInChI->Stereo->nCompInv2Abs = pAltInChI->Stereo->nCompInv2Abs;
3034 : : }
3035 : : }
3036 : : }
3037 : :
3038 : : /* use same rule to copy stereobonds */
3039 [ # # # # ]: 0 : if (pAltInChI->Stereo && pAltInChI->Stereo->nNumberOfStereoBonds &&
3040 [ # # # # ]: 0 : (!pInChI->Stereo || !pInChI->Stereo->b_parity))
3041 : : {
3042 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP2, 0, 0)))
3043 : : {
3044 : 0 : goto exit_function;
3045 : : }
3046 : : }
3047 : : /* (3) isotopic Mobile H -> isotopic Fixed H */
3048 : : /* if !FH_Stereo && !MH_Stereo && MH_IsoStereo!=NULL && FH_IsoStereo==NULL */
3049 [ # # ]: 0 : if (bIso)
3050 : : {
3051 [ # # # # ]: 0 : if (!(pInChI->Stereo && pInChI->Stereo->t_parity) && /* !FH_Stereo */
3052 [ # # # # ]: 0 : !(pAltInChI->Stereo && pAltInChI->Stereo->t_parity) && /* !MH_Stereo */
3053 [ # # # # ]: 0 : (pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->nNumberOfStereoCenters) && /* MH_IsoStereo */
3054 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->t_parity))
3055 : : {
3056 : : /* !FH_IsoStereo */
3057 : : /* copy sp3 iso stereo MI->FI (/t) and, if FH nCompInv2Abs (/m) is missing, copy it, too, MI->FI */
3058 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3, 1, 1)) ||
3059 [ # # # # : 0 : ((!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs) &&
# # ]
3060 : 0 : 0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3_M, 1, 1)))) /* djb-rwth: addressing LLVM warning */
3061 : : {
3062 : 0 : goto exit_function;
3063 : : }
3064 : : /* in case of missing nCompInv2Abs, Relative or Racemic stereo 2005-05-10 */
3065 [ # # ]: 0 : if (pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT &&
3066 [ # # ]: 0 : (nFlagsAlt & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
3067 : : {
3068 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs = s[iINChI][jAlt][1] > 0 ? 2 : 0;
3069 [ # # ]: 0 : if (!(pInChI->nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
3070 : : {
3071 : 0 : pInChI->nFlags |= (nFlagsAlt & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO));
3072 : : }
3073 : : }
3074 : : }
3075 : : else
3076 : : {
3077 : : /* copy sp3 inversion info only: isotopic Mobile H -> isotopic Fixed H */
3078 [ # # # # ]: 0 : if (!(pInChI->Stereo && pInChI->Stereo->t_parity) && /* !FH_Stereo /t */
3079 [ # # # # ]: 0 : !(pAltInChI->Stereo && pAltInChI->Stereo->t_parity) && /* !MH_Stereo /t */
3080 [ # # # # ]: 0 : (pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->nNumberOfStereoCenters) && /* MH_IsoStereo /t */
3081 [ # # # # ]: 0 : (pInChI->StereoIsotopic && pInChI->StereoIsotopic->nNumberOfStereoCenters) && /* FH_IsoStereo /t */
3082 [ # # ]: 0 : pAltInChI->StereoIsotopic->nCompInv2Abs && /* MH_IsoStereo /m */
3083 [ # # # # ]: 0 : (!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs))
3084 : : {
3085 : : /* !FH_IsoStereo /m */
3086 : : /* added 02-09-2006 */
3087 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3_M, 1, 1)))
3088 : : {
3089 : 0 : goto exit_function;
3090 : : }
3091 : : }
3092 : : }
3093 : : /* use same rule to copy stereobonds */
3094 [ # # # # ]: 0 : if (!(pInChI->Stereo && pInChI->Stereo->b_parity) &&
3095 [ # # # # ]: 0 : !(pAltInChI->Stereo && pAltInChI->Stereo->b_parity) &&
3096 [ # # # # ]: 0 : (pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->nNumberOfStereoBonds) &&
3097 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->b_parity))
3098 : : {
3099 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP2, 1, 1)))
3100 : : {
3101 : 0 : goto exit_function;
3102 : : }
3103 : : }
3104 : :
3105 : : /* (4) Copy Fixed-H -> isotopic Fixed-H */
3106 : : /* if FH_Stereo && !MH_IsoStereo && && !FH_IsoStereo */
3107 [ # # # # ]: 0 : if ((pInChI->Stereo && pInChI->Stereo->nNumberOfStereoCenters) && /* FH_Stereo /t */
3108 [ # # # # ]: 0 : !(pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->t_parity) && /* !MH_IsoStereo /t */
3109 [ # # # # ]: 0 : !(pInChI->StereoIsotopic && pInChI->StereoIsotopic->t_parity))
3110 : : {
3111 : : /* !FH_IsoStereo /t */
3112 : :
3113 : : /* added 10-10-2007 DT: copy MH_Iso /m => FH_Iso /m to fix i2i bug for Abs stereo */
3114 : : /* InChI string contains: MH(/t...), MH_Iso(/mN, no /t), FH(no /t /m), FH_Iso(no /t /m) */
3115 [ # # # # : 0 : if (pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->nCompInv2Abs && /* MH_IsoStereo /m */
# # ]
3116 : 0 : bNo_InChI_t &&
3117 [ # # ]: 0 : NO_VALUE_INT != pAltInChI->StereoIsotopic->nCompInv2Abs && /* undef FH_IsoStereo /m */
3118 [ # # # # ]: 0 : !(pInChI->StereoIsotopic && NO_VALUE_INT != pInChI->StereoIsotopic->nCompInv2Abs))
3119 : : {
3120 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP3_M, 1, 1)))
3121 : : {
3122 : 0 : goto exit_function;
3123 : : }
3124 : : }
3125 : :
3126 : : /* added 05-09-2006: copy sp3 FH=>FH_Iso */
3127 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3, 1, 0)) ||
3128 [ # # # # : 0 : ((!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs) &&
# # ]
3129 : 0 : 0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3_M, 1, 0)))) /* djb-rwth: addressing LLVM warning */
3130 : : {
3131 : 0 : goto exit_function;
3132 : : }
3133 : :
3134 : : /* in case of missing nCompInv2Abs, Relative or Racemic stereo, /sN in Fixed-H, 2005-05-10 */
3135 [ # # ]: 0 : if (pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT &&
3136 [ # # ]: 0 : (nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
3137 : : {
3138 [ # # # # ]: 0 : if (s[iINChI][j][0] > 0 && s[iINChI][j][1] > 0)
3139 : : {
3140 : : /* suppose once in a while only non-taut stereo changes if inverted */
3141 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 2;
3142 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = (++nFH2iFH_AltInv) % 2 ? 2 : 0;
3143 : : }
3144 : : else
3145 : : {
3146 [ # # # # : 0 : if ((s[iINChI][j][0] > 0 || s[iINChI][j][1] > 0) && s[iINChI][j][1] >= 0) /* ??? != NO_VALUE_INT ??? */
# # ]
3147 : : {
3148 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 2;
3149 : : }
3150 : : else
3151 : : {
3152 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 0;
3153 : : }
3154 : : }
3155 [ # # ]: 0 : if (!(pInChI->nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)))
3156 : : {
3157 : 0 : pInChI->nFlags |= (nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO));
3158 : : }
3159 : : }
3160 : : }
3161 : : else
3162 : : {
3163 : : /* copy sp3 inversion info only: Fixed-H -> isotopic Fixed H */
3164 [ # # # # ]: 0 : if ((pInChI->Stereo && pInChI->Stereo->t_parity) &&
3165 [ # # # # ]: 0 : !(pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->t_parity) &&
3166 [ # # # # ]: 0 : (pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->nNumberOfStereoCenters) &&
3167 [ # # # # ]: 0 : (pInChI->StereoIsotopic && pInChI->StereoIsotopic->nNumberOfStereoCenters) &&
3168 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs &&
3169 [ # # # # ]: 0 : (!pInChI->StereoIsotopic->nCompInv2Abs || NO_VALUE_INT == pInChI->StereoIsotopic->nCompInv2Abs))
3170 : : {
3171 : : /* added 05-09-2006 */
3172 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pInChI, CPY_SP3_M, 1, 0)))
3173 : : {
3174 : 0 : goto exit_function;
3175 : : }
3176 : : }
3177 : : }
3178 : : }
3179 [ # # ]: 0 : if (bIso &&
3180 [ # # # # ]: 0 : !(pInChI->Stereo && pInChI->Stereo->nNumberOfStereoBonds) &&
3181 [ # # # # ]: 0 : !(pAltInChI->Stereo && pAltInChI->Stereo->nNumberOfStereoBonds) &&
3182 [ # # # # ]: 0 : (pAltInChI->StereoIsotopic && pAltInChI->StereoIsotopic->nNumberOfStereoBonds) &&
3183 [ # # # # ]: 0 : (!pInChI->StereoIsotopic || !pInChI->StereoIsotopic->b_parity))
3184 : : {
3185 [ # # ]: 0 : if (0 > (ret2 = CopySegment(pInChI, pAltInChI, CPY_SP2, 1, 1)))
3186 : : {
3187 : 0 : goto exit_function;
3188 : : }
3189 : : }
3190 : : }
3191 : : }
3192 : : } /* end of component cycle (k) */
3193 : : } /* end of Mobile/Fixed H cycle (j) */
3194 : :
3195 : : /**** replace NO_VALUE_INT with zeroes in all Mobile & Fixed H components ****/
3196 [ + + ]: 114 : for (j = 0; j < TAUT_NUM; j++)
3197 : : {
3198 [ + + ]: 98 : for (k = 0; k < nNumComponents[iINChI][j]; k++)
3199 : : {
3200 [ + - ]: 22 : if (pInpInChI[iINChI][j])
3201 : : {
3202 : 22 : INChI* pInChI = &pInpInChI[iINChI][j][k];
3203 [ - + ]: 22 : if (pInChI->nTotalCharge == NO_VALUE_INT)
3204 : : {
3205 : 0 : pInChI->nTotalCharge = 0;
3206 : : }
3207 [ + + - + ]: 22 : if (pInChI->Stereo && pInChI->StereoIsotopic &&
3208 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
3209 : : {
3210 [ # # ]: 0 : if (pInChI->Stereo->nNumberOfStereoCenters &&
3211 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs != NO_VALUE_INT)
3212 : : {
3213 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = pInChI->Stereo->nCompInv2Abs;
3214 : : }
3215 : : }
3216 : : /* Add special nCompInv2Abs=2 to force /s2 or /s3 in InChI output */
3217 [ + + - + ]: 22 : if (pInChI->Stereo && pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT)
3218 : : {
3219 [ # # ]: 0 : if (pInChI->nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO) &&
3220 [ # # ]: 0 : pInChI->Stereo->nNumberOfStereoCenters)
3221 : : {
3222 [ # # ]: 0 : pInChI->Stereo->nCompInv2Abs = (s[iINChI][j][0] > 0 /*|| s[iINChI][j][1]>0*/) ? 2 : 0; /* we do not know the real value */
3223 : : }
3224 : : else
3225 : : {
3226 : 0 : pInChI->Stereo->nCompInv2Abs = 0;
3227 : : }
3228 : : }
3229 [ - + - - ]: 22 : if (pInChI->StereoIsotopic && pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)
3230 : : {
3231 [ # # ]: 0 : if (pInChI->nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO) &&
3232 [ # # ]: 0 : pInChI->StereoIsotopic->nNumberOfStereoCenters)
3233 : : {
3234 [ # # ]: 0 : pInChI->StereoIsotopic->nCompInv2Abs = s[iINChI][j][1] > 0 ? 2 : 0; /* we do not know the real value */
3235 : : }
3236 : : else
3237 : : {
3238 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 0;
3239 : : }
3240 : : }
3241 : : /* added 02-07-2006 */
3242 [ + + + - ]: 22 : if ((pInChI->Stereo && pInChI->Stereo->nCompInv2Abs == NO_VALUE_INT) ||
3243 [ - + - - ]: 22 : (pInChI->StereoIsotopic && pInChI->StereoIsotopic->nCompInv2Abs == NO_VALUE_INT)) /* djb-rwth: addressing LLVM warnings */
3244 : : {
3245 : 0 : ret2 = RI_ERR_PROGR;
3246 : 0 : goto exit_function;
3247 : : }
3248 [ + - + - ]: 22 : if (!pInChI->bDeleted && pInChI->nNumberOfAtoms)
3249 : : {
3250 : 22 : tot_charge[iINChI][j] += pInChI->nTotalCharge;
3251 [ + + ]: 72 : for (m = 0; m < pInChI->nNumberOfAtoms; m++)
3252 : : {
3253 [ + - - + ]: 50 : if (pInChI->nAtom[m] < EL_NUMBER_H || pInChI->nAtom[m] > nElDataLen)
3254 : : {
3255 : 0 : ret2 = RI_ERR_PROGR;
3256 : 0 : goto exit_function;
3257 : : }
3258 : : /* all atoms except H */
3259 [ + + ]: 50 : if (pInChI->nAtom[m] > EL_NUMBER_H)
3260 : : {
3261 : 48 : num_elem[iINChI][j][pInChI->nAtom[m]].num++;
3262 : : }
3263 : : }
3264 [ - + ]: 22 : if (0 > (ret2 = GetInChINumH(pInChI, &m)))
3265 : : {
3266 : 0 : goto exit_function;
3267 : : }
3268 : 22 : num_elem[iINChI][j][EL_NUMBER_H].num += m;
3269 : : }
3270 : : }
3271 : : }
3272 : : }
3273 : :
3274 [ + + ]: 114 : for (j = 0; j < TAUT_NUM; j++)
3275 : : {
3276 [ + + ]: 98 : for (k = 0; k < nNumComponents[iINChI][j]; k++)
3277 : : {
3278 [ + - ]: 22 : if (pInpInChI[iINChI][j])
3279 : : {
3280 : 22 : INChI* pInChI = &pInpInChI[iINChI][j][k];
3281 [ + + + + ]: 22 : if (pInChI->Stereo && !pInChI->Stereo->nNumberOfStereoCenters)
3282 : : {
3283 : 2 : pInChI->Stereo->nCompInv2Abs = 0;
3284 : : }
3285 [ - + - - ]: 22 : if (pInChI->StereoIsotopic && !pInChI->StereoIsotopic->nNumberOfStereoCenters)
3286 : : {
3287 : 0 : pInChI->StereoIsotopic->nCompInv2Abs = 0;
3288 : : }
3289 : : }
3290 : : }
3291 : : }
3292 : :
3293 : : #if ( FIX_I2I_STEREOCONVERSION_BUG3 == 1 )
3294 : : /* (2008-04-10) 1=> Fix bug of i2i conversion */
3295 : : /* (missed repeating /s in FI after F for multi-component case) */
3296 [ + - ]: 38 : if (nNumComponents[iINChI][TAUT_NON] > 1) /* if multi-component */
3297 : : {
3298 [ # # # # ]: 0 : if (!s[iINChI][TAUT_YES][0] && !s[iINChI][TAUT_YES][1])/* if no /s in M, MI */
3299 : : {
3300 [ # # # # ]: 0 : if ((s[iINChI][TAUT_NON][0] > 1) && (s[iINChI][TAUT_NON][1] > 1)) /* if /srel/srac in both F, FI */
3301 : : {
3302 [ # # ]: 0 : if (s[iINChI][TAUT_NON][0] == s[iINChI][TAUT_NON][1]) /* if same stereo in F and FI */
3303 : : {
3304 : : /* we assume that at least one component in F has no actual stereo */
3305 : : /* and place deliberately 0 to appropriate place */
3306 [ # # ]: 0 : for (k = 0; k < nNumComponents[iINChI][TAUT_NON]; k++)
3307 : : {
3308 : 0 : INChI* pInChI = &pInpInChI[iINChI][TAUT_NON][k];
3309 [ # # ]: 0 : if (pInChI->Stereo->nCompInv2Abs != 0)
3310 : : {
3311 : 0 : pInChI->Stereo->nCompInv2Abs = 0;
3312 : 0 : goto fini;
3313 : : }
3314 : : }
3315 : : }
3316 : : }
3317 : : }
3318 : : }
3319 : 38 : fini:;
3320 : : #endif
3321 [ + + ]: 38 : if (num_elem[iINChI][TAUT_YES])
3322 : : {
3323 : 19 : tot_charge[iINChI][TAUT_YES] += nNumProtons[iINChI][TAUT_YES].nNumRemovedProtons;
3324 : 19 : num_elem[iINChI][TAUT_YES][EL_NUMBER_H].num += nNumProtons[iINChI][TAUT_YES].nNumRemovedProtons;
3325 : : }
3326 : :
3327 : : /**** Count H and isotopic H in Mobile and Fixed H represntations of components */
3328 : : /* if at least one component has Fixed-H layer then all components have Fixed-H */
3329 : : /* layer; those whose Fixed-H layer is empty have Fixed-H layer same as Mobile-H layer */
3330 [ - + ]: 38 : if (nNumComponents[iINChI][TAUT_NON])
3331 : : {
3332 : : /* only if both Mobile and Fixed H exist */
3333 : : int nFormulaH[TAUT_NUM], nNumH[TAUT_NUM], nCharge[TAUT_NUM], nNumIsotopicH[TAUT_NUM][NUM_H_ISOTOPES];
3334 : : int nRemovedCharge, nRemovedH, nRemovedIsotopicH[NUM_H_ISOTOPES], nFoundRemovedIsoH;
3335 : : int nTotRemovedProtons, nTotRemovedIsotopicH[NUM_H_ISOTOPES], bExists[TAUT_NUM];
3336 : : INChI* pInChI[TAUT_NUM];
3337 : 0 : nTotRemovedProtons = 0;
3338 : 0 : memset(nTotRemovedIsotopicH, 0, sizeof(nTotRemovedIsotopicH)); /* djb-rwth: memset_s C11/Annex K variant? */
3339 : 0 : len2 = inchi_max(nNumComponents[iINChI][TAUT_YES], nNumComponents[iINChI][TAUT_NON]);
3340 : :
3341 [ # # ]: 0 : for (k = 0; k < len2; k++)
3342 : : {
3343 : : /* k is a component index */
3344 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
3345 : : {
3346 : : /* j is 0=TAUT_NON or 1=TAUT_YES */
3347 : 0 : pInChI[j] = NULL; /* initialization 2006-03 */
3348 : 0 : bExists[j] = (k < nNumComponents[iINChI][j]) &&
3349 [ # # # # ]: 0 : pInpInChI[iINChI][j][k].nNumberOfAtoms &&
3350 [ # # ]: 0 : !pInpInChI[iINChI][j][k].bDeleted;
3351 : : }
3352 [ # # ]: 0 : if (!bExists[TAUT_NON])
3353 : : {
3354 : : /* TAUT_YES does not exist for a proton (H+) in TAUT_NON */
3355 : 0 : ret2 = RI_ERR_SYNTAX;
3356 : 0 : goto exit_function;
3357 : : }
3358 : : /* at this point at least one of Mobile[k] and Fixed[k] real InChI exists */
3359 : : /* initialize for counting removed protons and isotopic H from kth Mobile-H component */
3360 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
3361 : : {
3362 [ # # ]: 0 : if (bExists[j])
3363 : : {
3364 : 0 : pInChI[j] = &pInpInChI[iINChI][j][k]; /* BC: reading uninit memory (fixed?) */
3365 : : }
3366 : 0 : nFormulaH[j] = 0;
3367 : 0 : nNumH[j] = 0;
3368 : 0 : nCharge[j] = 0;
3369 [ # # ]: 0 : for (m = 0; m < NUM_H_ISOTOPES; m++)
3370 : : {
3371 : 0 : nNumIsotopicH[j][m] = 0;
3372 : : }
3373 : : }
3374 : : /* extract number of H, isotopic H, and charge */
3375 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
3376 : : {
3377 [ # # ]: 0 : if (!bExists[j])
3378 : : {
3379 : 0 : continue;
3380 : : }
3381 [ # # # # ]: 0 : if (0 > (ret2 = GetInChIFormulaNumH(pInChI[j], &nFormulaH[j])) ||
3382 [ # # ]: 0 : 0 > (ret2 = GetInChINumH(pInChI[j], &nNumH[j])) ||
3383 : 0 : 0 > (ret2 = GetInChIIsoH(pInChI[j], nNumIsotopicH[j])))
3384 : : {
3385 : 0 : goto exit_function;
3386 : : }
3387 : 0 : nCharge[j] = pInChI[j]->nTotalCharge;
3388 : : }
3389 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
3390 : : {
3391 [ # # ]: 0 : if (!bExists[j])
3392 : : {
3393 : 0 : continue;
3394 : : }
3395 [ # # ]: 0 : if (nFormulaH[j] != nNumH[j])
3396 : : {
3397 : 0 : ret2 = RI_ERR_SYNTAX;
3398 : 0 : goto exit_function;
3399 : : }
3400 : : }
3401 : 0 : nFoundRemovedIsoH = 0;
3402 : 0 : nRemovedCharge = nCharge[TAUT_NON] - nCharge[TAUT_YES];
3403 : 0 : nRemovedH = nNumH[TAUT_NON] - nNumH[TAUT_YES];
3404 [ # # ]: 0 : for (m = 0; m < NUM_H_ISOTOPES; m++)
3405 : : {
3406 : 0 : nFoundRemovedIsoH += 0 != (nRemovedIsotopicH[m] = nNumIsotopicH[TAUT_NON][m] -
3407 : 0 : nNumIsotopicH[TAUT_YES][m]);
3408 : : }
3409 [ # # ]: 0 : if (nRemovedCharge != nRemovedH)
3410 : : {
3411 : 0 : ret2 = RI_ERR_SYNTAX;
3412 : 0 : goto exit_function;
3413 : : }
3414 [ # # # # ]: 0 : if (nRemovedCharge || nFoundRemovedIsoH)
3415 : : {
3416 : : COMPONENT_REM_PROTONS* pNumProtons;
3417 [ # # ]: 0 : if (!nNumProtons[iINChI][TAUT_YES].pNumProtons)
3418 : : {
3419 : : /* allocate only if needed */
3420 : 0 : nNumProtons[iINChI][TAUT_YES].pNumProtons =
3421 : 0 : (COMPONENT_REM_PROTONS*)inchi_calloc(len2,
3422 : : sizeof(nNumProtons[0][0].pNumProtons[0]));
3423 [ # # ]: 0 : if (!nNumProtons[iINChI][TAUT_YES].pNumProtons)
3424 : : {
3425 : 0 : ret2 = RI_ERR_ALLOC;
3426 : 0 : goto exit_function;
3427 : : }
3428 : : }
3429 : 0 : pNumProtons = nNumProtons[iINChI][TAUT_YES].pNumProtons + k;
3430 : 0 : pNumProtons->nNumRemovedProtons = nRemovedH;
3431 : 0 : nTotRemovedProtons += nRemovedH;
3432 [ # # ]: 0 : for (m = 0; m < NUM_H_ISOTOPES; m++)
3433 : : {
3434 : 0 : pNumProtons->nNumRemovedIsotopicH[m] = nRemovedIsotopicH[m];
3435 : 0 : nTotRemovedIsotopicH[m] += nRemovedIsotopicH[m];
3436 : : }
3437 : : /* make sure the Mobile-H InChI has nTautomer */
3438 [ # # # # ]: 0 : if (pInChI[TAUT_YES] && bExists[TAUT_YES])
3439 : : {
3440 [ # # ]: 0 : if (!pInChI[TAUT_YES]->lenTautomer)
3441 : : {
3442 : 0 : pInChI[TAUT_YES]->lenTautomer = 1;
3443 : : }
3444 [ # # ]: 0 : if (!pInChI[TAUT_YES]->nTautomer)
3445 : : {
3446 : 0 : pInChI[TAUT_YES]->nTautomer = (AT_NUMB*)inchi_calloc(pInChI[TAUT_YES]->lenTautomer, sizeof(pInChI[0]->nTautomer[0]));
3447 : : }
3448 : : }
3449 : : }
3450 : : }
3451 [ # # ]: 0 : if (nNumProtons[iINChI][TAUT_YES].pNumProtons)
3452 : : {
3453 : : /* check consistency */
3454 : : #if ( FIX_ISO_FIXEDH_BUG_READ == 1 )
3455 : : int iso_diff[NUM_H_ISOTOPES], iso_diff_tot = 0;
3456 : : #endif
3457 [ # # ]: 0 : if (nTotRemovedProtons != nNumProtons[iINChI][TAUT_YES].nNumRemovedProtons)
3458 : : {
3459 : 0 : ret2 = RI_ERR_SYNTAX;
3460 : 0 : goto exit_function;
3461 : : }
3462 : : #if ( FIX_ISO_FIXEDH_BUG_READ == 1 )
3463 : : for (m = 0; m < NUM_H_ISOTOPES; m++)
3464 : : {
3465 : : iso_diff[m] = nNumProtons[iINChI][TAUT_YES].nNumRemovedIsotopicH[m] - nTotRemovedIsotopicH[m];
3466 : : if (iso_diff[m] < 0)
3467 : : {
3468 : : ret2 = RI_ERR_SYNTAX;
3469 : : goto exit_function;
3470 : : }
3471 : : else
3472 : : {
3473 : : /* InChI-1.02b bug: nTotRemovedIsotopicH[m] < nNumProtons[iINChI][TAUT_YES].nNumRemovedIsotopicH[m] */
3474 : : /* in non-tautomeric components where D(+) or T(+) was removed from -NH(+)= or =OH(+) */
3475 : : iso_diff_tot += iso_diff[m];
3476 : : }
3477 : : }
3478 : : if (iso_diff_tot)
3479 : : {
3480 : : if (0 > bIsoMayBeArranged(bInchi2Struct, iso_diff, nNumProtons, pInpInChI, nNumComponents, iINChI))
3481 : : {
3482 : : ret2 = RI_ERR_SYNTAX;
3483 : : goto exit_function;
3484 : : }
3485 : : }
3486 : : #else
3487 [ # # ]: 0 : for (m = 0; m < NUM_H_ISOTOPES; m++)
3488 : : {
3489 [ # # ]: 0 : if (nTotRemovedIsotopicH[m] != nNumProtons[iINChI][TAUT_YES].nNumRemovedIsotopicH[m])
3490 : : {
3491 : 0 : ret2 = RI_ERR_SYNTAX;
3492 : 0 : goto exit_function;
3493 : : }
3494 : : }
3495 : : #endif
3496 : : }
3497 : : }
3498 : :
3499 : : /* make Mobile H and Fixed H InChI arrays have same length */
3500 : 38 : len2 = len1 = 0;
3501 [ - + ]: 38 : if (nNumComponents[iINChI][TAUT_YES] < nNumComponents[iINChI][TAUT_NON])
3502 : : {
3503 : 0 : j = TAUT_YES; /* less components in Mobile-H layer */
3504 : 0 : len2 = nNumComponents[iINChI][TAUT_NON];
3505 : 0 : len1 = nNumComponents[iINChI][TAUT_YES];
3506 : : }
3507 : : else
3508 [ + + ]: 38 : if (nNumComponents[iINChI][TAUT_YES] > nNumComponents[iINChI][TAUT_NON])
3509 : : {
3510 : 19 : j = TAUT_NON; /* less components in Fixed-H layer */
3511 : 19 : len2 = nNumComponents[iINChI][TAUT_YES];
3512 : 19 : len1 = nNumComponents[iINChI][TAUT_NON];
3513 : : }
3514 : : /* always len1 <= len2; if Mobile-H and Fixed-H have same number of components then len1=len2=0 */
3515 [ + + - + ]: 38 : if (len2 && len1)
3516 : : {
3517 : 0 : INChI* pInChI = (INChI*)inchi_calloc(len2, sizeof(pInChI[0]));
3518 [ # # ]: 0 : if (!pInChI)
3519 : : {
3520 : 0 : ret2 = RI_ERR_ALLOC;
3521 : 0 : goto exit_function;
3522 : : }
3523 : 0 : memcpy(pInChI, pInpInChI[iINChI][j], len1 * sizeof(pInChI[0]));
3524 [ # # ]: 0 : inchi_free(pInpInChI[iINChI][j]);
3525 : 0 : pInpInChI[iINChI][j] = pInChI;
3526 : 0 : nNumComponents[iINChI][j] = len2;
3527 [ # # ]: 0 : for (; len1 < len2; len1++)
3528 : : {
3529 [ # # ]: 0 : if (j == TAUT_YES)
3530 : : {
3531 : : /* mark added to Mobile H layer components as deleted protons */
3532 [ # # ]: 0 : if (0 > (ret2 = nFillOutProtonMobileH(pInpInChI[iINChI][j] + len1)))
3533 : : {
3534 : 0 : goto exit_function;
3535 : : }
3536 [ # # ]: 0 : if (0 > (ret2 = nProtonCopyIsotopicInfo(pInpInChI[iINChI][j] + len1/* to */,
3537 : 0 : pInpInChI[iINChI][TAUT_NON] + len1/* from */)))
3538 : : {
3539 : 0 : goto exit_function;
3540 : : }
3541 : : }
3542 : : else
3543 : : {
3544 : : /* mark added to Fixed H layer components as empty deleted */
3545 : : /* this should not happen */
3546 : 0 : pInChI[len1].bDeleted = 1;
3547 : : }
3548 : : }
3549 : : }
3550 : : } /* end of iINChI cycle */
3551 : :
3552 : : /* check balances */
3553 [ + + ]: 57 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
3554 : : {
3555 [ + + ]: 95 : for (i = iINChI; i < INCHI_NUM; i++)
3556 : : {
3557 [ + + ]: 171 : for (j = 0; j < TAUT_NUM; j++)
3558 : : {
3559 [ + + ]: 285 : for (k = j; k < TAUT_NUM; k++)
3560 : : {
3561 [ + + + + : 171 : if ((iINChI != i || j != k) && num_elem[iINChI][j] && num_elem[i][k])
+ + - + ]
3562 : : {
3563 [ # # ]: 0 : if (tot_charge[iINChI][j] != tot_charge[i][k])
3564 : : {
3565 : 0 : ret2 = RI_ERR_SYNTAX;
3566 : 0 : goto exit_function;
3567 : : }
3568 [ # # ]: 0 : for (m = 0; m <= nElDataLen; m++)
3569 : : {
3570 [ # # ]: 0 : if (num_elem[iINChI][j][m].num != num_elem[i][k][m].num)
3571 : : {
3572 : 0 : ret2 = RI_ERR_SYNTAX;
3573 : 0 : goto exit_function;
3574 : : }
3575 : : }
3576 : : /*
3577 : : if ( memcmp( num_elem[iINChI], num_elem[i][k], (nElDataLen+1)*sizeof(num_elem[0][0][0]) ) {
3578 : : ret2 = RI_ERR_SYNTAX;
3579 : : goto exit_function;
3580 : : }
3581 : : */
3582 : : }
3583 : : }
3584 : : }
3585 : : }
3586 : : }
3587 : :
3588 : : }
3589 : : else
3590 : : {
3591 : 9 : ret2 = ret;
3592 : : }
3593 : :
3594 : 28 : exit_function:
3595 : :
3596 [ + + ]: 84 : for (i = 0; i < INCHI_NUM; i++)
3597 : : {
3598 [ + + ]: 168 : for (j = 0; j < TAUT_NUM; j++)
3599 : : {
3600 [ + + ]: 112 : if (num_elem[i][j])
3601 : : {
3602 [ + - ]: 19 : inchi_free(num_elem[i][j]);
3603 : 19 : num_elem[i][j] = NULL;
3604 : : }
3605 : : }
3606 : : }
3607 [ + + + - ]: 28 : *nErr = (ret2 < 0 && ret2 != RI_ERR_EOL) ? ret2 : 0;
3608 : :
3609 : 28 : return ret;
3610 : : }
3611 : :
3612 : :
3613 : : /****************************************************************************/
3614 : : #if ( FIX_ISO_FIXEDH_BUG_READ == 1 )
3615 : : #undef TAUT_YES
3616 : : int bIsoMayBeArranged(int bInchi2Struct,
3617 : : int iso_diff[NUM_H_ISOTOPES],
3618 : : REM_PROTONS nNumProtons[INCHI_NUM][TAUT_NUM],
3619 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
3620 : : int nNumComponents[INCHI_NUM][TAUT_NUM],
3621 : : int iINChI)
3622 : : {
3623 : : const int TAUT_YES = 1;
3624 : : int i, k, m, n_found = 0, n_found_at_in_component, n_found_H_in_component, i_iso_at, num_iso_H = 0, num_iso_H_orig, num_add_iso_H, orig_add_H;
3625 : : for (m = 0; m < NUM_H_ISOTOPES; m++)
3626 : : {
3627 : : num_iso_H += iso_diff[m];
3628 : : }
3629 : : num_iso_H_orig = num_iso_H;
3630 : : for (k = 0; k < nNumComponents[iINChI][TAUT_YES] && k < nNumComponents[iINChI][TAUT_NON]; k++)
3631 : : {
3632 : : INChI* pInChI = &pInpInChI[iINChI][TAUT_NON][k];
3633 : : INChI* pInChITaut = &pInpInChI[iINChI][TAUT_YES][k];
3634 : : if (pInChITaut->bDeleted || pInChI->bDeleted ||
3635 : : pInChITaut->nNumberOfIsotopicAtoms > 0 ||
3636 : : pInChITaut->lenTautomer > 1 && pInChITaut->nTautomer && pInChITaut->nTautomer[0] > 0 ||
3637 : : NULL == nNumProtons[iINChI][TAUT_YES].pNumProtons ||
3638 : : nNumProtons[iINChI][TAUT_YES].pNumProtons[k].nNumRemovedProtons <= 0 ||
3639 : : pInChI->nNumberOfIsotopicAtoms > 0 ||
3640 : : nNumProtons[iINChI][TAUT_YES].pNumProtons[k].nNumRemovedIsotopicH[0] ||
3641 : : nNumProtons[iINChI][TAUT_YES].pNumProtons[k].nNumRemovedIsotopicH[1] ||
3642 : : nNumProtons[iINChI][TAUT_YES].pNumProtons[k].nNumRemovedIsotopicH[2]
3643 : : )
3644 : : {
3645 : : continue;
3646 : : }
3647 : : /* check if fixed-H has isotopic H; count the possibilities */
3648 : : orig_add_H = nNumProtons[iINChI][TAUT_YES].pNumProtons[k].nNumRemovedProtons;
3649 : : n_found_at_in_component = 0; /* number of atoms that may accept isotopic H */
3650 : : n_found_H_in_component = 0;
3651 : : for (i = 0; i < pInChI->nNumberOfAtoms; i++)
3652 : : {
3653 : : int nNumRemovedH = (int)pInChI->nNum_H[i] - (int)pInChITaut->nNum_H[i];
3654 : : if (nNumRemovedH > 0)
3655 : : {
3656 : : n_found_at_in_component++;
3657 : : n_found_H_in_component += nNumRemovedH;
3658 : : }
3659 : : }
3660 : : if (n_found_at_in_component > 0 && num_iso_H > 0 && bInchi2Struct)
3661 : : {
3662 : : pInChI->IsotopicAtom = (INChI_IsotopicAtom*)inchi_calloc(inchi_min(n_found_at_in_component, num_iso_H), sizeof(pInChI->IsotopicAtom[0]));
3663 : : }
3664 : : for (i = 0, i_iso_at = 0; i < pInChI->nNumberOfAtoms; i++)
3665 : : {
3666 : : int nNumRemovedH = (int)pInChI->nNum_H[i] - (int)pInChITaut->nNum_H[i];
3667 : : n_found += nNumRemovedH; /* found H removed in mobile-H layer */
3668 : : if (nNumRemovedH > 0 && num_iso_H > 0 && orig_add_H)
3669 : : {
3670 : : for (m = 0; m < NUM_H_ISOTOPES && 0 < num_iso_H && 0 < orig_add_H && 0 < nNumRemovedH; m++)
3671 : : {
3672 : : if (iso_diff[m] > 0)
3673 : : {
3674 : : num_add_iso_H = inchi_min(iso_diff[m], nNumRemovedH); /* atom limit */
3675 : : if (num_add_iso_H > orig_add_H) /* component limit */
3676 : : num_add_iso_H = orig_add_H;
3677 : : iso_diff[m] -= num_add_iso_H; /* update tot removed single isotope H limit */
3678 : : num_iso_H -= num_add_iso_H; /* update tot removed isotopic H limit */
3679 : : orig_add_H -= num_add_iso_H; /* update component limit */
3680 : : nNumRemovedH -= num_add_iso_H; /* update atom limit */
3681 : : nNumProtons[iINChI][TAUT_YES].pNumProtons[k].nNumRemovedIsotopicH[m] += num_add_iso_H;
3682 : : if (pInChI->IsotopicAtom)
3683 : : {
3684 : : pInChI->IsotopicAtom[i_iso_at].nAtomNumber = i + 1;
3685 : : switch (m)
3686 : : {
3687 : : case 0:
3688 : : pInChI->IsotopicAtom[i_iso_at].nNum_H += num_add_iso_H;
3689 : : break;
3690 : : case 1:
3691 : : pInChI->IsotopicAtom[i_iso_at].nNum_D += num_add_iso_H;
3692 : : break;
3693 : : case 2:
3694 : : pInChI->IsotopicAtom[i_iso_at].nNum_T += num_add_iso_H;
3695 : : break;
3696 : : }
3697 : : }
3698 : : }
3699 : : }
3700 : : if (pInChI->IsotopicAtom)
3701 : : {
3702 : : i_iso_at++;
3703 : : }
3704 : : }
3705 : : }
3706 : : if (pInChI->IsotopicAtom && i_iso_at)
3707 : : {
3708 : : pInChI->nNumberOfIsotopicAtoms = i_iso_at;
3709 : : }
3710 : : }
3711 : : if (n_found - num_iso_H >= 0)
3712 : : {
3713 : : /* Success. Arrange isotopic H between components */
3714 : : }
3715 : :
3716 : : return n_found - num_iso_H_orig; /* >0 => ambiguous reconstruction, 0 => unambiguous, <0 => impossible */
3717 : : }
3718 : : #define TAUT_YES 1
3719 : : #endif
3720 : :
3721 : :
3722 : :
3723 : : /****************************************************************************/
3724 : : typedef enum tagAuxInfoState {
3725 : : AST_VERSION, /* 0 */
3726 : :
3727 : : AST_MOBILE_H_NUMBERS, /* 1 /N: */
3728 : : AST_MOBILE_H_ATOM_EQ, /* 2 /E: */
3729 : : AST_MOBILE_H_GROUP_EQ, /* 3 /gE: */
3730 : : AST_MOBILE_H_SP3_INV, /* 4 /it: */
3731 : : AST_MOBILE_H_SP3_INV_NUMBERS, /* 5 /iN: */
3732 : :
3733 : : AST_MOBILE_H_ISO_LAYER_FORK, /* 6 */
3734 : :
3735 : : AST_MOBILE_H_ISO_NUMBERS, /* 7 /I: */
3736 : : AST_MOBILE_H_ISO_ATOM_EQ, /* 8 /E: */
3737 : : AST_MOBILE_H_ISO_GROUP_EQ, /* 9 /gE: */
3738 : : AST_MOBILE_H_ISO_SP3_INV, /* 10 /it: */
3739 : : AST_MOBILE_H_ISO_SP3_INV_NUMBERS, /* 11 /iN: */
3740 : :
3741 : : AST_FIXED_H_LAYER_FORK, /* 12 */
3742 : :
3743 : : AST_FIXED_H_NUMBERS, /* 13 /F: */
3744 : : AST_FIXED_H_ATOM_EQ, /* 14 /E: */
3745 : : AST_FIXED_H_SP3_INV, /* 15 /it: */
3746 : : AST_FIXED_H_SP3_INV_NUMBERS, /* 16 /iN: */
3747 : :
3748 : : AST_FIXED_H_ISO_LAYER_FORK, /* 17 */
3749 : :
3750 : : AST_FIXED_H_ISO_NUMBERS, /* 18 /I: */
3751 : : AST_FIXED_H_ISO_ATOM_EQ, /* 19 /E: */
3752 : : AST_FIXED_H_ISO_SP3_INV, /* 20 /it: */
3753 : : AST_FIXED_H_ISO_SP3_INV_NUMBERS, /* 21 /iN: */
3754 : :
3755 : : AST_REVERSE_INFO_CRV, /* 22 /CRV: */
3756 : : AST_REVERSE_INFO_ATOMS, /* 23 /rA: */
3757 : : AST_REVERSE_INFO_BONDS, /* 24 /rB: */
3758 : : AST_REVERSE_INFO_XYZ, /* 25 /rC: */
3759 : :
3760 : : AST_RECONNECTED_LAYER_FORK, /* 26 /R: */
3761 : : AST_RECONNECTED_LAYER_NUMBERS /* 27 */
3762 : : }AUX_INFO_STATE;
3763 : :
3764 : :
3765 : : /****************************************************************************/
3766 : 0 : int ParseAuxSegmentVersion(const char* str,
3767 : : int bMobileH,
3768 : : INChI* pInpInChI[],
3769 : : int ppnNumComponents[],
3770 : : int state)
3771 : : {
3772 : : const char* q;
3773 [ # # # # ]: 0 : if (isdigit(UCINT * str) && (inchi_strtol(str, &q, 10), !*q))
3774 : : {
3775 : 0 : return 1;
3776 : : }
3777 : 0 : return RI_ERR_SYNTAX;
3778 : : }
3779 : :
3780 : :
3781 : : /****************************************************************************
3782 : : CopyAtomNumbers
3783 : :
3784 : : Save isotopic numbering into the first nNumberOfAtoms
3785 : : elements of INChI::nPossibleLocationsOfIsotopicH;
3786 : : save non-isotopic numbering into the second half of
3787 : : nNumberOfAtoms elements of INChI::nPossibleLocationsOfIsotopicH
3788 : : ****************************************************************************/
3789 : 0 : int CopyAtomNumbers(INChI* pInChI_To,
3790 : : int bIsoTo,
3791 : : INChI* pInChI_From,
3792 : : int bIsoFrom)
3793 : : {
3794 : : AT_NUMB* pTo, * pFrom;
3795 [ # # # # : 0 : if (!pInChI_To || !pInChI_From || pInChI_To->bDeleted || pInChI_From->bDeleted ||
# # # # ]
3796 [ # # # # ]: 0 : !pInChI_To->nNumberOfAtoms || !pInChI_From->nNumberOfAtoms ||
3797 [ # # ]: 0 : pInChI_To->nNumberOfAtoms != pInChI_From->nNumberOfAtoms ||
3798 [ # # ]: 0 : !pInChI_From->nPossibleLocationsOfIsotopicH)
3799 : : {
3800 : 0 : return RI_ERR_PROGR;
3801 : : }
3802 [ # # ]: 0 : if (!pInChI_To->nPossibleLocationsOfIsotopicH)
3803 : : {
3804 : 0 : pInChI_To->nPossibleLocationsOfIsotopicH = (AT_NUMB*)inchi_calloc(2 * (long long)pInChI_To->nNumberOfAtoms,
3805 : : sizeof(pInChI_To->nPossibleLocationsOfIsotopicH[0])); /* djb-rwth: cast operator added */
3806 [ # # ]: 0 : if (!pInChI_To->nPossibleLocationsOfIsotopicH)
3807 : : {
3808 : 0 : return RI_ERR_ALLOC;
3809 : : }
3810 : : }
3811 [ # # ]: 0 : pTo = pInChI_To->nPossibleLocationsOfIsotopicH + (bIsoTo ? 0 : pInChI_To->nNumberOfAtoms);
3812 [ # # ]: 0 : pFrom = pInChI_From->nPossibleLocationsOfIsotopicH + (bIsoFrom ? 0 : pInChI_To->nNumberOfAtoms);
3813 [ # # ]: 0 : if (pTo == pFrom)
3814 : : {
3815 : 0 : return RI_ERR_PROGR;
3816 : : }
3817 : 0 : memcpy(pTo, pFrom, pInChI_To->nNumberOfAtoms * sizeof(pTo[0]));
3818 : 0 : return 1;
3819 : : }
3820 : :
3821 : :
3822 : : /****************************************************************************
3823 : : Parse and save AuxInfo atom numbers in "/N:" or "/F:" or "/I:" segment
3824 : :
3825 : : NB: save isotopic numbering into the first nNumberOfAtoms
3826 : : elements of INChI::nPossibleLocationsOfIsotopicH;
3827 : : save non-isotopic numbering into the second half of nNumberOfAtoms
3828 : : elements of INChI::nPossibleLocationsOfIsotopicH
3829 : : ****************************************************************************/
3830 : 0 : int ParseAuxSegmentNumbers(const char* str, /* AuxInfo string */
3831 : : int bMobileH, /* treat mobile or fixedH domain nums */
3832 : : INChI* pInpInChI[],
3833 : : int ppnNumComponents[],
3834 : : int state, /* start position==state of reading */
3835 : : int* pbAbc /* ==1 if treating compresssed InChI */
3836 : : )
3837 : : {
3838 : 0 : int bIso = 0, iComponent = 0, nNumComponents, bIso_From = 0, bAltInChIExists;
3839 : 0 : INChI* pInChI = NULL, * pAltInChI = NULL, * pInChI_From = NULL;
3840 : : const char* p, * q, * pStart, * pEnd, * t;
3841 : : static const char mult_type[] = "mnM";
3842 : : int val, ret, k, mpy_component, num;
3843 : : AT_NUMB* pNumb;
3844 : 0 : int base = 10;
3845 : 0 : int if_cnd = 1; /* djb-rwth: needed for some if condition restructuring */
3846 : :
3847 [ # # # # : 0 : switch (state)
# ]
3848 : : {
3849 : 0 : case AST_MOBILE_H_NUMBERS:
3850 [ # # ]: 0 : if (bMobileH != TAUT_YES)
3851 : : {
3852 : 0 : return RI_ERR_PROGR;
3853 : : }
3854 [ # # ]: 0 : if (memcmp(str, "N:", 2))
3855 : : {
3856 : 0 : return 0;
3857 : : }
3858 : 0 : break;
3859 : 0 : case AST_FIXED_H_NUMBERS:
3860 [ # # ]: 0 : if (bMobileH != TAUT_NON)
3861 : : {
3862 : 0 : return RI_ERR_PROGR;
3863 : : }
3864 [ # # ]: 0 : if (memcmp(str, "F:", 2))
3865 : : {
3866 : 0 : return 0;
3867 : : }
3868 : 0 : break;
3869 : 0 : case AST_MOBILE_H_ISO_NUMBERS:
3870 [ # # ]: 0 : if (bMobileH != TAUT_YES)
3871 : : {
3872 : 0 : return RI_ERR_PROGR;
3873 : : }
3874 [ # # ]: 0 : if (memcmp(str, "I:", 2))
3875 : : {
3876 : 0 : return 0;
3877 : : }
3878 : 0 : bIso = 1;
3879 : 0 : break;
3880 : 0 : case AST_FIXED_H_ISO_NUMBERS:
3881 [ # # ]: 0 : if (bMobileH != TAUT_NON)
3882 : : {
3883 : 0 : return RI_ERR_PROGR;
3884 : : }
3885 [ # # ]: 0 : if (memcmp(str, "I:", 2))
3886 : : {
3887 : 0 : return 0;
3888 : : }
3889 : 0 : bIso = 1;
3890 : 0 : break;
3891 : 0 : default:
3892 : 0 : return RI_ERR_PROGR;
3893 : : }
3894 : :
3895 : 0 : pStart = (char*)str + 2;
3896 [ # # ]: 0 : if (!*pStart)
3897 : : {
3898 : 0 : return 1;
3899 : : }
3900 : 0 : iComponent = 0;
3901 : 0 : nNumComponents = ppnNumComponents[bMobileH];
3902 : :
3903 [ # # ]: 0 : bAltInChIExists = (NULL != pInpInChI[ALT_TAUT(bMobileH)]);
3904 : : while (1)
3905 : : {
3906 : : /* Cycle over components */
3907 [ # # ]: 0 : if (!(pEnd = strchr(pStart, ';')))
3908 : : {
3909 : 0 : pEnd = pStart + strlen(pStart);
3910 : : }
3911 : : /* check */
3912 [ # # ]: 0 : if (!pInpInChI[bMobileH])
3913 : : {
3914 : 0 : return 1; /* invalid aux info */
3915 : : }
3916 : 0 : pInChI = pInpInChI[bMobileH] + iComponent;
3917 [ # # ]: 0 : pAltInChI = pInpInChI[ALT_TAUT(bMobileH)] + iComponent;
3918 : :
3919 : : /* djb-rwth: condition for if block had to be rewritten */
3920 [ # # ]: 0 : if ((int)inchi_strtol(pStart, &q, 10) > 0)
3921 : : {
3922 : 0 : val = (int)inchi_strtol(pStart, &q, 10);
3923 : 0 : if_cnd = isdigit(UCINT * pStart);
3924 : :
3925 : : }
3926 : : else
3927 : : {
3928 : 0 : val = 1;
3929 : 0 : q = pStart;
3930 : 0 : if_cnd = 1;
3931 : : }
3932 : :
3933 [ # # # # : 0 : if (if_cnd && (t = strchr((char*)mult_type, *q)) && q + 1 == pEnd) /* djb-rwth: if_cnd applied; ignoring LLVM warning: variable used to store function return value */
# # ]
3934 : : {
3935 : : /* Process the abbreviation */
3936 : 0 : pInChI_From = NULL;
3937 [ # # # ]: 0 : switch (bMobileH)
3938 : : {
3939 : 0 : case TAUT_YES:
3940 [ # # # ]: 0 : switch (bIso)
3941 : : {
3942 : 0 : case 0:
3943 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
3944 : 0 : goto exit_function;
3945 : 0 : case 1:
3946 [ # # ]: 0 : if (*q != 'm')
3947 : : {
3948 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
3949 : 0 : goto exit_function;
3950 : : }
3951 : : /* isotopic Mobile-H <-- non-isotopic Mobile H */
3952 : 0 : pInChI_From = pInChI;
3953 : 0 : bIso_From = 0;
3954 : 0 : break;
3955 : 0 : default:
3956 : 0 : ret = RI_ERR_PROGR;
3957 : 0 : goto exit_function;
3958 : : }
3959 : 0 : break;
3960 : :
3961 : 0 : case TAUT_NON:
3962 [ # # # # ]: 0 : switch (*q)
3963 : : {
3964 : 0 : case 'm': /* same as mobile H */
3965 [ # # # ]: 0 : switch (bIso)
3966 : : {
3967 : 0 : case 0: /* from Mobile-H not isotopic */
3968 [ # # ]: 0 : pInChI_From = bAltInChIExists ? pAltInChI : NULL;
3969 : 0 : bIso_From = 0;
3970 : 0 : break;
3971 : :
3972 : 0 : case 1:
3973 [ # # ]: 0 : pInChI_From = bAltInChIExists ? pAltInChI : NULL;;
3974 : 0 : bIso_From = 1;
3975 : 0 : break;
3976 : 0 : default:
3977 : 0 : ret = RI_ERR_PROGR;
3978 : 0 : goto exit_function;
3979 : : }
3980 : 0 : break;
3981 : 0 : case 'n': /* same as non-isotopic Fixed-H */
3982 [ # # # ]: 0 : switch (bIso)
3983 : : {
3984 : 0 : case 0:
3985 : 0 : ret = 1; /*RI_ERR_SYNTAX;*/
3986 : 0 : goto exit_function;
3987 : 0 : case 1:
3988 : 0 : pInChI_From = pInChI; /* djb-rwth: ignoring LLVM warning: value used */
3989 : 0 : bIso_From = 0; /* djb-rwth: ignoring LLVM warning: value used */
3990 : 0 : default:
3991 : 0 : ret = RI_ERR_PROGR;
3992 : 0 : goto exit_function;
3993 : : }
3994 : : break;
3995 : 0 : case 'M': /* same as isotopic Mobile-H */
3996 [ # # # ]: 0 : switch (bIso)
3997 : : {
3998 : 0 : case 0:
3999 : 0 : ret = RI_ERR_SYNTAX;
4000 : 0 : goto exit_function;
4001 : 0 : case 1:
4002 [ # # ]: 0 : pInChI_From = bAltInChIExists ? pAltInChI : NULL;;
4003 : 0 : bIso_From = 1;
4004 : 0 : break;
4005 : 0 : default:
4006 : 0 : ret = RI_ERR_PROGR;
4007 : 0 : goto exit_function;
4008 : : }
4009 : 0 : break;
4010 : 0 : default:
4011 : 0 : ret = 1; /*RI_ERR_SYNTAX;*/
4012 : 0 : goto exit_function;
4013 : : }
4014 : 0 : break;
4015 : : }
4016 : :
4017 : : /* Save numbers */
4018 [ # # ]: 0 : if (pInChI_From)
4019 : : {
4020 [ # # ]: 0 : for (k = 0; k < val; k++)
4021 : : {
4022 : 0 : CopyAtomNumbers(pInChI + k, bIso, pInChI_From + k, bIso_From); /* djb-rwth: addressing coverity ID #499525 -- return values handled properly */
4023 : : }
4024 : : }
4025 : 0 : mpy_component = val;
4026 : : }
4027 : : else
4028 : : {
4029 : 0 : mpy_component = 1;
4030 : 0 : p = pStart; /* djb-rwth: ignoring LLVM warning: value used */
4031 : 0 : pNumb = pInChI->nPossibleLocationsOfIsotopicH;
4032 [ # # ]: 0 : if (!pNumb)
4033 : : {
4034 : 0 : pNumb = (AT_NUMB*)inchi_calloc(2 * (long long)pInChI->nNumberOfAtoms, sizeof(pNumb[0])); /* djb-rwth: cast operator added */
4035 [ # # ]: 0 : if (!pNumb)
4036 : : {
4037 : 0 : ret = RI_ERR_ALLOC;
4038 : 0 : goto exit_function;
4039 : : }
4040 : 0 : pInChI->nPossibleLocationsOfIsotopicH = pNumb;
4041 : : }
4042 [ # # ]: 0 : pNumb += bIso ? 0 : pInChI->nNumberOfAtoms;
4043 [ # # # # ]: 0 : if (pStart < pEnd && *pbAbc == -1)
4044 : : {
4045 : : /* Check if compressed InChI */
4046 : 0 : *pbAbc = isupper(UCINT * pStart) ? 1 : 0;
4047 : : }
4048 [ # # ]: 0 : base = (*pbAbc == 1) ? ALPHA_BASE : 10;
4049 : :
4050 [ # # ]: 0 : if (*pbAbc == 1)
4051 : : {
4052 [ # # # # ]: 0 : for (k = 0, p = pStart; k < pInChI->nNumberOfAtoms && p < pEnd; k++, p++)
4053 : : {
4054 : 0 : num = (AT_NUMB)inchi_strtol(p, &q, base);
4055 : : #if ( CHECK_STRTOL_ATNUMB==1 )
4056 [ # # # # ]: 0 : if (num > MAX_ATOMS || num < 0)
4057 : : {
4058 : 0 : ret = RI_ERR_SYNTAX;
4059 : 0 : goto exit_function;
4060 : : }
4061 : : #endif
4062 [ # # # # ]: 0 : if (num <= 0 || p == q)
4063 : : {
4064 : 0 : ret = RI_ERR_SYNTAX;
4065 : 0 : goto exit_function;
4066 : : }
4067 : 0 : pNumb[k] = (AT_NUMB)num;
4068 : 0 : p = q;
4069 [ # # ]: 0 : if (p == pEnd)
4070 : : {
4071 : 0 : break; /* main end of cycle */
4072 : : }
4073 : : }
4074 : : }
4075 : : else
4076 : : {
4077 [ # # # # ]: 0 : for (k = 0, p = pStart; k < pInChI->nNumberOfAtoms && p < pEnd; k++, p++)
4078 : : {
4079 : 0 : pNumb[k] = (AT_NUMB)inchi_strtol(p, &q, 10);
4080 : : #if ( CHECK_STRTOL_ATNUMB==1 )
4081 [ # # ]: 0 : if (pNumb[k] > MAX_ATOMS || pNumb[k] < 0)
4082 : : {
4083 : 0 : ret = RI_ERR_SYNTAX;
4084 : 0 : goto exit_function;
4085 : : }
4086 : : #endif
4087 : 0 : p = q;
4088 [ # # ]: 0 : if (p == pEnd)
4089 : : {
4090 : 0 : break; /* main end of cycle */
4091 : : }
4092 : : else
4093 : : {
4094 [ # # ]: 0 : if (*p != ',')
4095 : : {
4096 : 0 : ret = RI_ERR_SYNTAX;
4097 : 0 : goto exit_function;
4098 : : }
4099 : : }
4100 : : }
4101 : : }
4102 [ # # # # ]: 0 : if (p != pEnd || k + 1 != pInChI->nNumberOfAtoms)
4103 : : {
4104 : 0 : ret = RI_ERR_SYNTAX;
4105 : 0 : goto exit_function;
4106 : : }
4107 : : }
4108 : :
4109 : 0 : iComponent += mpy_component;
4110 [ # # ]: 0 : if (*pEnd)
4111 : : {
4112 : 0 : pStart = pEnd + 1;
4113 : 0 : continue;
4114 : : }
4115 : : else
4116 : : {
4117 : 0 : break;
4118 : : }
4119 : : } /* end of cycle over components */
4120 : :
4121 [ # # ]: 0 : if (nNumComponents != iComponent)
4122 : : {
4123 : 0 : ret = 1; /*RI_ERR_SYNTAX;*/
4124 : 0 : goto exit_function;
4125 : : }
4126 : 0 : ret = iComponent + 1;
4127 : :
4128 : 0 : exit_function:
4129 : :
4130 : 0 : return ret;
4131 : : }
4132 : :
4133 : :
4134 : : /****************************************************************************
4135 : : Read and skip AuxInfo segment atom equivalence classes, "/E:" segments
4136 : : ****************************************************************************/
4137 : 0 : int ParseAuxSegmentAtomEqu(const char* str,
4138 : : int bMobileH,
4139 : : INChI* pInpInChI[],
4140 : : int ppnNumComponents[],
4141 : : int state)
4142 : : {
4143 [ # # # # : 0 : switch (state)
# ]
4144 : : {
4145 : 0 : case AST_MOBILE_H_ATOM_EQ:
4146 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4147 : : {
4148 : 0 : return RI_ERR_PROGR;
4149 : : }
4150 [ # # ]: 0 : if (memcmp(str, "E:", 2))
4151 : : {
4152 : 0 : return 0;
4153 : : }
4154 : 0 : break;
4155 : 0 : case AST_MOBILE_H_ISO_ATOM_EQ:
4156 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4157 : : {
4158 : 0 : return RI_ERR_PROGR;
4159 : : }
4160 [ # # ]: 0 : if (memcmp(str, "E:", 2))
4161 : : {
4162 : 0 : return 0;
4163 : : }
4164 : 0 : break;
4165 : 0 : case AST_FIXED_H_ATOM_EQ:
4166 [ # # ]: 0 : if (bMobileH != TAUT_NON)
4167 : : {
4168 : 0 : return RI_ERR_PROGR;
4169 : : }
4170 [ # # ]: 0 : if (memcmp(str, "E:", 2))
4171 : : {
4172 : 0 : return 0;
4173 : : }
4174 : 0 : break;
4175 : 0 : case AST_FIXED_H_ISO_ATOM_EQ:
4176 [ # # ]: 0 : if (bMobileH != TAUT_NON)
4177 : : {
4178 : 0 : return RI_ERR_PROGR;
4179 : : }
4180 [ # # ]: 0 : if (memcmp(str, "E:", 2))
4181 : : {
4182 : 0 : return 0;
4183 : : }
4184 : 0 : break;
4185 : 0 : default:
4186 : 0 : return RI_ERR_PROGR;
4187 : : }
4188 : :
4189 : 0 : return 1;
4190 : : }
4191 : :
4192 : :
4193 : : /****************************************************************************
4194 : : Read and skip AuxInfo segment group equivalence classes, "/gE:" segments
4195 : : ****************************************************************************/
4196 : 0 : int ParseAuxSegmentGroupEqu(const char* str,
4197 : : int bMobileH,
4198 : : INChI* pInpInChI[],
4199 : : int ppnNumComponents[],
4200 : : int state)
4201 : : {
4202 [ # # # ]: 0 : switch (state)
4203 : : {
4204 : 0 : case AST_MOBILE_H_GROUP_EQ:
4205 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4206 : : {
4207 : 0 : return RI_ERR_PROGR;
4208 : : }
4209 [ # # ]: 0 : if (memcmp(str, "gE:", 3))
4210 : : {
4211 : 0 : return 0;
4212 : : }
4213 : 0 : break;
4214 : 0 : case AST_MOBILE_H_ISO_GROUP_EQ:
4215 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4216 : : {
4217 : 0 : return RI_ERR_PROGR;
4218 : : }
4219 [ # # ]: 0 : if (memcmp(str, "gE:", 3))
4220 : : {
4221 : 0 : return 0;
4222 : : }
4223 : 0 : break;
4224 : 0 : default:
4225 : 0 : return RI_ERR_PROGR;
4226 : : }
4227 : :
4228 : 0 : return 1;
4229 : : }
4230 : :
4231 : :
4232 : : /****************************************************************************
4233 : : Read and skip AuxInfo segment sp3 inv info, , "/it:" segment
4234 : : ****************************************************************************/
4235 : 0 : int ParseAuxSegmentSp3Inv(const char* str,
4236 : : int bMobileH,
4237 : : INChI* pInpInChI[],
4238 : : int ppnNumComponents[],
4239 : : int state)
4240 : : {
4241 [ # # # # : 0 : switch (state)
# ]
4242 : : {
4243 : 0 : case AST_MOBILE_H_SP3_INV:
4244 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4245 : : {
4246 : 0 : return RI_ERR_PROGR;
4247 : : }
4248 [ # # ]: 0 : if (memcmp(str, "it:", 3))
4249 : : {
4250 : 0 : return 0;
4251 : : }
4252 : 0 : break;
4253 : 0 : case AST_MOBILE_H_ISO_SP3_INV:
4254 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4255 : : {
4256 : 0 : return RI_ERR_PROGR;
4257 : : }
4258 [ # # ]: 0 : if (memcmp(str, "it:", 3))
4259 : : {
4260 : 0 : return 0;
4261 : : }
4262 : 0 : break;
4263 : 0 : case AST_FIXED_H_SP3_INV:
4264 [ # # ]: 0 : if (bMobileH != TAUT_NON)
4265 : : {
4266 : 0 : return RI_ERR_PROGR;
4267 : : }
4268 [ # # ]: 0 : if (memcmp(str, "it:", 3))
4269 : : {
4270 : 0 : return 0;
4271 : : }
4272 : 0 : break;
4273 : 0 : case AST_FIXED_H_ISO_SP3_INV:
4274 [ # # ]: 0 : if (bMobileH != TAUT_NON)
4275 : : {
4276 : 0 : return RI_ERR_PROGR;
4277 : : }
4278 [ # # ]: 0 : if (memcmp(str, "it:", 3))
4279 : : {
4280 : 0 : return 0;
4281 : : }
4282 : 0 : break;
4283 : 0 : default:
4284 : 0 : return RI_ERR_PROGR;
4285 : : }
4286 : :
4287 : 0 : return 1;
4288 : : }
4289 : :
4290 : :
4291 : : /****************************************************************************
4292 : : Read and skip AuxInfo atom numbers in "/iN:"segment,
4293 : : for sp3 inv sub-layer at specific reading state
4294 : : ****************************************************************************/
4295 : 0 : int ParseAuxSegmentSp3InvNumbers(const char* str,
4296 : : int bMobileH,
4297 : : INChI* pInpInChI[],
4298 : : int ppnNumComponents[],
4299 : : int state)
4300 : : {
4301 [ # # # # : 0 : switch (state)
# ]
4302 : : {
4303 : 0 : case AST_MOBILE_H_SP3_INV_NUMBERS:
4304 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4305 : : {
4306 : 0 : return RI_ERR_PROGR;
4307 : : }
4308 [ # # ]: 0 : if (memcmp(str, "iN:", 3))
4309 : : {
4310 : 0 : return 0;
4311 : : }
4312 : 0 : break;
4313 : 0 : case AST_MOBILE_H_ISO_SP3_INV_NUMBERS:
4314 [ # # ]: 0 : if (bMobileH != TAUT_YES)
4315 : : {
4316 : 0 : return RI_ERR_PROGR;
4317 : : }
4318 [ # # ]: 0 : if (memcmp(str, "iN:", 3))
4319 : : {
4320 : 0 : return 0;
4321 : : }
4322 : 0 : break;
4323 : 0 : case AST_FIXED_H_SP3_INV_NUMBERS:
4324 [ # # ]: 0 : if (bMobileH != TAUT_NON)
4325 : : {
4326 : 0 : return RI_ERR_PROGR;
4327 : : }
4328 [ # # ]: 0 : if (memcmp(str, "iN:", 3))
4329 : : {
4330 : 0 : return 0;
4331 : : }
4332 : 0 : break;
4333 : 0 : case AST_FIXED_H_ISO_SP3_INV_NUMBERS:
4334 [ # # ]: 0 : if (bMobileH != TAUT_NON)
4335 : : {
4336 : 0 : return RI_ERR_PROGR;
4337 : : }
4338 [ # # ]: 0 : if (memcmp(str, "iN:", 3))
4339 : : {
4340 : 0 : return 0;
4341 : : }
4342 : 0 : break;
4343 : 0 : default:
4344 : 0 : return RI_ERR_PROGR;
4345 : : }
4346 : :
4347 : 0 : return 1;
4348 : : }
4349 : :
4350 : :
4351 : : /****************************************************************************
4352 : : Read and skip AuxInfo sp3 CRV (charge, radical, valence) segment
4353 : : ****************************************************************************/
4354 : 0 : int ParseAuxSegmentReverseCRV(const char* str, int state)
4355 : : {
4356 [ # # ]: 0 : switch (state)
4357 : : {
4358 : 0 : case AST_REVERSE_INFO_CRV:
4359 [ # # ]: 0 : if (memcmp(str, "CRV:", 4))
4360 : : {
4361 : 0 : return 0;
4362 : : }
4363 : 0 : break;
4364 : 0 : default:
4365 : 0 : return RI_ERR_PROGR;
4366 : : }
4367 : :
4368 : 0 : return 1;
4369 : : }
4370 : :
4371 : :
4372 : : /****************************************************************************
4373 : : Read and skip AuxInfo segment ReverseAtoms
4374 : : ****************************************************************************/
4375 : 0 : int ParseAuxSegmentReverseAtoms(const char* str, int state)
4376 : : {
4377 [ # # ]: 0 : switch (state)
4378 : : {
4379 : 0 : case AST_REVERSE_INFO_ATOMS:
4380 [ # # ]: 0 : if (memcmp(str, "rA:", 3))
4381 : : {
4382 : 0 : return 0;
4383 : : }
4384 : 0 : break;
4385 : 0 : default:
4386 : 0 : return RI_ERR_PROGR;
4387 : : }
4388 : :
4389 : 0 : return 1;
4390 : : }
4391 : :
4392 : :
4393 : : /****************************************************************************
4394 : : Read and skip AuxInfo segment ReverseBonds
4395 : : ****************************************************************************/
4396 : 0 : int ParseAuxSegmentReverseBonds(const char* str, int state)
4397 : : {
4398 [ # # ]: 0 : switch (state)
4399 : : {
4400 : 0 : case AST_REVERSE_INFO_BONDS:
4401 [ # # ]: 0 : if (memcmp(str, "rB:", 3))
4402 : : {
4403 : 0 : return 0;
4404 : : }
4405 : 0 : break;
4406 : 0 : default:
4407 : 0 : return RI_ERR_PROGR;
4408 : : }
4409 : :
4410 : 0 : return 1;
4411 : : }
4412 : :
4413 : :
4414 : : /* Parse and save AuxInfo segment ReverseXYZ */
4415 : 0 : int ParseAuxSegmentReverseXYZ(const char* str,
4416 : : XYZ_COORD** ppXYZ,
4417 : : int state)
4418 : : {
4419 : : const char* pStart, * p, * q;
4420 : 0 : XYZ_COORD* pXYZ = NULL;
4421 : 0 : int nLenXYZ = 0, i, j;
4422 : :
4423 [ # # ]: 0 : switch (state)
4424 : : {
4425 : 0 : case AST_REVERSE_INFO_XYZ:
4426 [ # # ]: 0 : if (memcmp(str, "rC:", 3))
4427 : : {
4428 : 0 : return 0;
4429 : : }
4430 : 0 : break;
4431 : 0 : default:
4432 : 0 : return RI_ERR_PROGR;
4433 : : }
4434 : 0 : pStart = (char*)str + 3;
4435 : : /* Count coordinates */
4436 [ # # ]: 0 : for (p = pStart, nLenXYZ = 0; *p; p++)
4437 : : {
4438 : 0 : nLenXYZ += (*p == ';');
4439 : : }
4440 [ # # ]: 0 : if (!nLenXYZ)
4441 : : {
4442 : 0 : return RI_ERR_SYNTAX;
4443 : : }
4444 [ # # ]: 0 : if (NULL == (pXYZ = (XYZ_COORD*)inchi_calloc(nLenXYZ, sizeof(pXYZ[0]))))
4445 : : {
4446 : 0 : return RI_ERR_ALLOC;
4447 : : }
4448 [ # # # # ]: 0 : for (p = pStart, i = 0; *p && i < nLenXYZ; p++, i++)
4449 : : {
4450 [ # # ]: 0 : for (j = 0; j < 3; j++)
4451 : : {
4452 : 0 : pXYZ[i].xyz[j] = inchi_strtod(p, &q);
4453 : 0 : p = q + (*q == ',');
4454 : : }
4455 [ # # ]: 0 : if (*p != ';')
4456 : : {
4457 : 0 : break;
4458 : : }
4459 : : }
4460 [ # # # # ]: 0 : if (i != nLenXYZ || *p)
4461 : : {
4462 [ # # ]: 0 : inchi_free(pXYZ); /* djb-rwth: fixing a NULL pointer dereference */
4463 : 0 : return RI_ERR_SYNTAX;
4464 : : }
4465 : 0 : *ppXYZ = pXYZ;
4466 : :
4467 : 0 : return nLenXYZ + 1;
4468 : : }
4469 : :
4470 : :
4471 : : /****************************************************************************
4472 : : Parse and save atom coordinates from AuxInfo
4473 : : ****************************************************************************/
4474 : 0 : int AddAuxSegmentCoord(int nRet,
4475 : : XYZ_COORD* pXYZ,
4476 : : int nLenXYZ,
4477 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
4478 : : int nNumComponents[INCHI_NUM][TAUT_NUM])
4479 : : {
4480 : 0 : int iINChI, j, k, n, m, numAt[TAUT_NUM], num_at, ret = 0; /* djb-rwth: removing redundant variables */
4481 : 0 : INChI* pInChI = NULL;
4482 : 0 : INChI* pAltInChI = NULL;
4483 : : XYZ_COORD* pxyz;
4484 : :
4485 : : /* Propagate numberings (original:canonical atom mapping) */
4486 : : /* NB: we already saved isotopic numbering, if any, into the first nNumberOfAtoms elements of INChI::nPossibleLocationsOfIsotopicH, */
4487 : : /* and the non-isotopic numbering into the second half of nNumberOfAtoms elements of that INChI::nPossibleLocationsOfIsotopicH */
4488 : :
4489 [ # # ]: 0 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
4490 : : {
4491 [ # # ]: 0 : for (j = TAUT_YES; TAUT_NON <= j; j--) /* for FixedH and MobileH ... */
4492 : : {
4493 [ # # ]: 0 : if (pInpInChI[iINChI][j]) /* djb-rwth: fixing a NULL pointer dereference */
4494 : : {
4495 [ # # ]: 0 : for (k = 0; k < nNumComponents[iINChI][j]; k++) /* for each component ... */
4496 : : {
4497 [ # # ]: 0 : int jj = ALT_TAUT(j);
4498 : 0 : pInChI = pInpInChI[iINChI][j] + k;
4499 [ # # ]: 0 : pAltInChI = (k < nNumComponents[iINChI][jj]) ? pInpInChI[iINChI][jj] + k : NULL;
4500 [ # # ]: 0 : numAt[j] = (!pInChI->bDeleted) ? pInChI->nNumberOfAtoms : 0;
4501 [ # # # # ]: 0 : numAt[jj] = (pAltInChI && !pAltInChI->bDeleted) ? pAltInChI->nNumberOfAtoms : 0;
4502 [ # # # ]: 0 : switch (j)
4503 : : {
4504 : 0 : case TAUT_YES:
4505 [ # # ]: 0 : if (!numAt[j])
4506 : : {
4507 : 0 : break; /* component does not exist */
4508 : : }
4509 [ # # ]: 0 : if (!pInChI->nPossibleLocationsOfIsotopicH)
4510 : : {
4511 : : /* djb-rwth: removing redundant code */
4512 : 0 : break;
4513 : : }
4514 [ # # ]: 0 : if (!pInChI->nPossibleLocationsOfIsotopicH[0])
4515 : : {
4516 [ # # ]: 0 : if (pInChI->nPossibleLocationsOfIsotopicH[numAt[j]])
4517 : : {
4518 : : /* copy from non-isotopic (2nd half of the at. numbers array) to the isotopic (1st half) */
4519 : 0 : ret = CopyAtomNumbers(pInChI, 1, pInChI, 0);
4520 [ # # ]: 0 : if (ret < 0)
4521 : : {
4522 : 0 : goto exit_function;
4523 : : }
4524 : : }
4525 : : else
4526 : : {
4527 [ # # ]: 0 : inchi_free(pInChI->nPossibleLocationsOfIsotopicH);
4528 : 0 : pInChI->nPossibleLocationsOfIsotopicH = NULL;
4529 : : /* djb-rwth: removing redundant code */
4530 : : }
4531 : : }
4532 : 0 : break;
4533 : :
4534 : 0 : case TAUT_NON:
4535 [ # # ]: 0 : if (!numAt[j])
4536 : : {
4537 : 0 : break; /* component does not exist */
4538 : : }
4539 [ # # ]: 0 : if (!pInChI->nPossibleLocationsOfIsotopicH)
4540 : : {
4541 : : /* trying to get numbers from Mobile-H component */
4542 [ # # # # ]: 0 : if (!numAt[jj] || !(pAltInChI->nPossibleLocationsOfIsotopicH))
4543 : : {
4544 : : /* djb-rwth: removing redundant code */
4545 : : break;
4546 : : }
4547 [ # # ]: 0 : if (pAltInChI->nPossibleLocationsOfIsotopicH[0])
4548 : : {
4549 : 0 : ret = CopyAtomNumbers(pInChI, 1, pAltInChI, 1);
4550 [ # # ]: 0 : if (ret < 0)
4551 : : {
4552 : 0 : goto exit_function;
4553 : : }
4554 : : }
4555 : : else
4556 [ # # ]: 0 : if (pAltInChI->nPossibleLocationsOfIsotopicH[numAt[jj]])
4557 : : {
4558 : 0 : ret = CopyAtomNumbers(pInChI, 1, pAltInChI, 0);
4559 [ # # ]: 0 : if (ret < 0)
4560 : : {
4561 : 0 : goto exit_function;
4562 : : }
4563 : : }
4564 : : else
4565 : : {
4566 : : /* pAltInChI->nPossibleLocationsOfIsotopicH should have */
4567 : : /* been deallocated on previous TAUT_YES pass */
4568 : 0 : ret = RI_ERR_PROGR;
4569 : 0 : goto exit_function;
4570 : : }
4571 : : }
4572 [ # # ]: 0 : else if (!pInChI->nPossibleLocationsOfIsotopicH[0])
4573 : : {
4574 [ # # ]: 0 : if (pInChI->nPossibleLocationsOfIsotopicH[numAt[j]])
4575 : : {
4576 : : /* copy from non-isotopic to isotopic */
4577 : 0 : ret = CopyAtomNumbers(pInChI, 1, pInChI, 0);
4578 [ # # ]: 0 : if (ret < 0)
4579 : : {
4580 : 0 : goto exit_function;
4581 : : }
4582 : : }
4583 : : else
4584 : : {
4585 [ # # ]: 0 : inchi_free(pInChI->nPossibleLocationsOfIsotopicH);
4586 : 0 : pInChI->nPossibleLocationsOfIsotopicH = NULL;
4587 : : /* djb-rwth: removing redundant code */
4588 : : }
4589 : : }
4590 : 0 : break;
4591 : : }
4592 : : }
4593 : : }
4594 : : }
4595 : : }
4596 : :
4597 : : /* Add coordinates */
4598 [ # # ]: 0 : for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
4599 : : {
4600 [ # # ]: 0 : for (j = 0; j < TAUT_NUM; j++)
4601 : : {
4602 [ # # ]: 0 : for (k = 0; k < nNumComponents[iINChI][j]; k++)
4603 : : {
4604 : 0 : pInChI = pInpInChI[iINChI][j] + k;
4605 [ # # ]: 0 : if (pInChI) /* djb-rwth: fixing a NULL pointer dereference */
4606 : : {
4607 [ # # ]: 0 : num_at = (!pInChI->bDeleted) ? pInChI->nNumberOfAtoms : 0;
4608 [ # # ]: 0 : if (!num_at)
4609 : : {
4610 [ # # ]: 0 : if (pInChI->nPossibleLocationsOfIsotopicH)
4611 : : {
4612 [ # # ]: 0 : inchi_free(pInChI->nPossibleLocationsOfIsotopicH);
4613 : 0 : pInChI->nPossibleLocationsOfIsotopicH = NULL;
4614 : : }
4615 : 0 : continue;
4616 : : }
4617 [ # # ]: 0 : if (!pInChI->nPossibleLocationsOfIsotopicH)
4618 : : {
4619 : 0 : continue;
4620 : : }
4621 [ # # # # ]: 0 : if (iINChI == INCHI_BAS && num_at == 1 &&
4622 [ # # # # ]: 0 : pInChI->szHillFormula && !strcmp(pInChI->szHillFormula, "H") &&
4623 [ # # ]: 0 : (int)pInChI->nPossibleLocationsOfIsotopicH[0] - 1 >= nLenXYZ)
4624 : : {
4625 : : ; /* a single atom H disconnected from a metal atom has no coordinates */
4626 : : }
4627 : : else
4628 : : {
4629 : : /* add atom coordinates */
4630 : 0 : pxyz = (XYZ_COORD*)inchi_calloc(num_at, sizeof(pxyz[0]));
4631 [ # # ]: 0 : if (!pxyz)
4632 : : {
4633 : 0 : ret = RI_ERR_ALLOC;
4634 : 0 : goto exit_function;
4635 : : }
4636 [ # # ]: 0 : for (n = 0; n < num_at; n++)
4637 : : {
4638 : 0 : m = (int)pInChI->nPossibleLocationsOfIsotopicH[n] - 1;
4639 [ # # # # ]: 0 : if (m < 0 || m >= nLenXYZ)
4640 : : {
4641 [ # # ]: 0 : inchi_free(pxyz);
4642 : 0 : ret = RI_ERR_SYNTAX;
4643 : 0 : goto exit_function;
4644 : : }
4645 : 0 : pxyz[n] = pXYZ[m];
4646 : : }
4647 : 0 : pInChI->IsotopicTGroup = (INChI_IsotopicTGroup*)pxyz;
4648 : : }
4649 [ # # ]: 0 : inchi_free(pInChI->nPossibleLocationsOfIsotopicH);
4650 : 0 : pInChI->nPossibleLocationsOfIsotopicH = NULL;
4651 : : }
4652 : : }
4653 : : }
4654 : : }
4655 : 0 : ret = nRet; /* normal exit */
4656 : :
4657 : 0 : exit_function:
4658 : :
4659 : 0 : return ret;
4660 : : }
4661 : :
4662 : :
4663 : : /****************************************************************************
4664 : : ReadInChICoord (from AuxInfo if present)
4665 : : ****************************************************************************/
4666 : 0 : int ReadInChICoord(INCHI_IOSTREAM* pInp,
4667 : : SEGM_LINE* pLine,
4668 : : int* pState,
4669 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
4670 : : int nNumComponents[INCHI_NUM][TAUT_NUM])
4671 : : {
4672 : : int c;
4673 : : /* djb-rwth: removing redundant variables */
4674 : 0 : int ret = RI_ERR_ALLOC;
4675 : 0 : int bMobileH = TAUT_YES;
4676 : 0 : int bReconn = INCHI_BAS;
4677 : 0 : int state = -1;
4678 : 0 : int prev_state = -1;
4679 : 0 : int nLenXYZ = 0;
4680 : 0 : int bAbc = -1; /* initially undefined */
4681 : : const char
4682 : 0 : szToken[] = INCHI_TOKEN;
4683 : : XYZ_COORD
4684 : 0 : * pXYZ = NULL;
4685 : :
4686 : 0 : *pState = 0;
4687 : : INCHI_HEAPCHK
4688 : :
4689 : : /* Get "InChI=1/" */
4690 [ # # ]: 0 : if (pLine->len)
4691 : : {
4692 : 0 : c = pLine->c;
4693 : : }
4694 : : else
4695 : : {
4696 : 0 : c = nGetInChISegment(pInp, pLine, szToken);
4697 : : }
4698 [ # # # # : 0 : if (c == RI_ERR_EOF && !pLine->len && !pLine->str[0])
# # ]
4699 : : {
4700 : 0 : ret = c;
4701 : 0 : pLine->len = 0;
4702 : 0 : goto exit_error;
4703 : : }
4704 [ # # # # : 0 : if (pLine->len == 0 || (c != SEG_END && c != RI_ERR_EOF && !INCHI_INP_EOL(c))) /* djb-rwth: addressing LLVM warning */
# # # # #
# # # ]
4705 : : {
4706 : 0 : *pState = -1;
4707 : 0 : pLine->len = 0;
4708 : 0 : ret = RI_ERR_PROGR;
4709 : 0 : goto exit_error;
4710 : : }
4711 [ # # ]: 0 : if (memcmp(pLine->str, "AuxInfo=", 8))
4712 : : {
4713 : 0 : *pState = -1;
4714 : 0 : return c;
4715 : : }
4716 : :
4717 : 0 : state = AST_VERSION;
4718 : 0 : ret = 1; /* means read the next segment */
4719 : : do
4720 : : {
4721 : : /* Read the next segment up to the '/' */
4722 : : INCHI_HEAPCHK
4723 [ # # ]: 0 : if (ret < 0)
4724 : : {
4725 : 0 : *pState = prev_state;
4726 : 0 : break;
4727 : : }
4728 [ # # ]: 0 : prev_state = state + (bReconn ? IST_HAPPENED_IN_RECMET : 0);
4729 [ # # ]: 0 : if (0 < ret)
4730 : : {
4731 : : /* read next segment */
4732 [ # # # # ]: 0 : if (c != RI_ERR_EOF && c != SEG_END)
4733 : : {
4734 : : /* abnormal reading result; should not happen */
4735 [ # # # # : 0 : while (c != RI_ERR_EOF && !INCHI_INP_EOL(c))
# # # # ]
4736 : : {
4737 : : /* bypass to the end of line or file */
4738 : 0 : c = getInChIChar(pInp);
4739 : : }
4740 [ # # ]: 0 : ret = (c == RI_ERR_EOF) ? RI_ERR_EOF : RI_ERR_EOL; /* end of line */
4741 : 0 : pLine->len = 0;
4742 : 0 : pLine->c = ret;
4743 : 0 : break;
4744 : : }
4745 [ # # ]: 0 : if (c == RI_ERR_EOF)
4746 : : {
4747 : 0 : ret = RI_ERR_EOF; /* end of line */
4748 : 0 : break;
4749 : : }
4750 [ # # ]: 0 : if (c == SEG_END)
4751 : : {
4752 : 0 : c = nGetInChISegment(pInp, pLine, szToken);
4753 : : }
4754 [ # # ]: 0 : if (c < 0)
4755 : : {
4756 : 0 : goto exit_error; /* error */
4757 : : }
4758 [ # # ]: 0 : if (!pLine->len)
4759 : : {
4760 : 0 : ret = RI_ERR_EOL; /* end of line */
4761 : 0 : break;
4762 : : }
4763 : : /* djb-rwth: removing redundant code */
4764 : : }
4765 : :
4766 : : /* Process the seqment */
4767 [ # # # # : 0 : switch (state)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
4768 : : {
4769 : 0 : case AST_VERSION:
4770 : : /* Mobile H */
4771 : 0 : bMobileH = TAUT_YES;
4772 : 0 : ret = ParseAuxSegmentVersion(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4773 : 0 : state = AST_MOBILE_H_NUMBERS;
4774 : 0 : break;
4775 : 0 : case AST_MOBILE_H_NUMBERS:
4776 : 0 : ret = ParseAuxSegmentNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
4777 : 0 : state = AST_MOBILE_H_ATOM_EQ;
4778 : 0 : break;
4779 : 0 : case AST_MOBILE_H_ATOM_EQ:
4780 : 0 : ret = ParseAuxSegmentAtomEqu(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4781 : 0 : state = AST_MOBILE_H_GROUP_EQ;
4782 : 0 : break;
4783 : 0 : case AST_MOBILE_H_GROUP_EQ:
4784 : 0 : ret = ParseAuxSegmentGroupEqu(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4785 : 0 : state = AST_MOBILE_H_SP3_INV;
4786 : 0 : break;
4787 : 0 : case AST_MOBILE_H_SP3_INV:
4788 : 0 : ret = ParseAuxSegmentSp3Inv(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4789 : 0 : state = AST_MOBILE_H_SP3_INV_NUMBERS;
4790 : 0 : break;
4791 : 0 : case AST_MOBILE_H_SP3_INV_NUMBERS:
4792 : 0 : ret = ParseAuxSegmentSp3InvNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4793 : 0 : state = AST_MOBILE_H_ISO_LAYER_FORK;
4794 : 0 : break;
4795 : :
4796 : 0 : case AST_MOBILE_H_ISO_LAYER_FORK:
4797 [ # # ]: 0 : if (!memcmp(pLine->str, "I:", 2))
4798 : : {
4799 : 0 : state = AST_MOBILE_H_ISO_NUMBERS;
4800 : : }
4801 [ # # ]: 0 : else if (!inchi_memicmp(pLine->str, "F:", 2))
4802 : : {
4803 : 0 : state = AST_FIXED_H_NUMBERS;
4804 : 0 : bMobileH = TAUT_NON;
4805 : : }
4806 [ # # ]: 0 : else if ( /*bReconn == INCHI_BAS &&*/ !inchi_memicmp(pLine->str, "CRV:", 4))
4807 : : {
4808 : 0 : state = AST_REVERSE_INFO_CRV;
4809 : : }
4810 : : else
4811 [ # # # # ]: 0 : if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "rA:", 3))
4812 : : {
4813 : 0 : state = AST_REVERSE_INFO_ATOMS;
4814 : : }
4815 [ # # # # ]: 0 : else if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "R:", 3))
4816 : : {
4817 : 0 : ret = 1; /* read the next segment */
4818 : 0 : state = AST_VERSION;
4819 : 0 : bMobileH = TAUT_YES;
4820 : 0 : bReconn = INCHI_REC;
4821 : : }
4822 : : else
4823 : : {
4824 : 0 : ret = RI_ERR_SYNTAX;
4825 : : }
4826 : 0 : break;
4827 : :
4828 : : /* Mobile H, isotopic */
4829 : 0 : case AST_MOBILE_H_ISO_NUMBERS:
4830 : 0 : ret = ParseAuxSegmentNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
4831 : 0 : state = AST_MOBILE_H_ISO_ATOM_EQ;
4832 : 0 : break;
4833 : 0 : case AST_MOBILE_H_ISO_ATOM_EQ:
4834 : 0 : ret = ParseAuxSegmentAtomEqu(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4835 : 0 : state = AST_MOBILE_H_ISO_GROUP_EQ;
4836 : 0 : break;
4837 : 0 : case AST_MOBILE_H_ISO_GROUP_EQ:
4838 : 0 : ret = ParseAuxSegmentGroupEqu(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4839 : 0 : state = AST_MOBILE_H_ISO_SP3_INV;
4840 : 0 : break;
4841 : 0 : case AST_MOBILE_H_ISO_SP3_INV:
4842 : 0 : ret = ParseAuxSegmentSp3Inv(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4843 : 0 : state = AST_MOBILE_H_ISO_SP3_INV_NUMBERS;
4844 : 0 : break;
4845 : 0 : case AST_MOBILE_H_ISO_SP3_INV_NUMBERS:
4846 : 0 : ret = ParseAuxSegmentSp3InvNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4847 : 0 : state = AST_FIXED_H_LAYER_FORK;
4848 : 0 : break;
4849 : :
4850 : 0 : case AST_FIXED_H_LAYER_FORK:
4851 [ # # ]: 0 : if (!inchi_memicmp(pLine->str, "F:", 2))
4852 : : {
4853 : 0 : state = AST_FIXED_H_NUMBERS;
4854 : 0 : bMobileH = TAUT_NON;
4855 : : }
4856 [ # # ]: 0 : else if ( /*bReconn == INCHI_BAS &&*/ !inchi_memicmp(pLine->str, "CRV:", 4))
4857 : : {
4858 : 0 : state = AST_REVERSE_INFO_CRV;
4859 : : }
4860 [ # # # # ]: 0 : else if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "rA:", 3))
4861 : : {
4862 : 0 : state = AST_REVERSE_INFO_ATOMS;
4863 : : }
4864 [ # # # # ]: 0 : else if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "R:", 3))
4865 : : {
4866 : 0 : ret = 1; /* read the next segment */
4867 : 0 : state = AST_VERSION;
4868 : 0 : bMobileH = TAUT_YES;
4869 : 0 : bReconn = INCHI_REC;
4870 : : }
4871 : : else
4872 : : {
4873 : 0 : ret = RI_ERR_SYNTAX;
4874 : : }
4875 : 0 : break;
4876 : :
4877 : 0 : case AST_FIXED_H_NUMBERS:
4878 : 0 : ret = ParseAuxSegmentNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
4879 : 0 : state = AST_FIXED_H_ATOM_EQ;
4880 : 0 : break;
4881 : 0 : case AST_FIXED_H_ATOM_EQ:
4882 : 0 : ret = ParseAuxSegmentAtomEqu(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4883 : 0 : state = AST_FIXED_H_SP3_INV;
4884 : 0 : break;
4885 : 0 : case AST_FIXED_H_SP3_INV:
4886 : 0 : ret = ParseAuxSegmentSp3Inv(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4887 : 0 : state = AST_FIXED_H_SP3_INV_NUMBERS;
4888 : 0 : break;
4889 : 0 : case AST_FIXED_H_SP3_INV_NUMBERS:
4890 : 0 : ret = ParseAuxSegmentSp3InvNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4891 : 0 : state = AST_FIXED_H_ISO_LAYER_FORK;
4892 : 0 : break;
4893 : :
4894 : 0 : case AST_FIXED_H_ISO_LAYER_FORK:
4895 [ # # ]: 0 : if (!memcmp(pLine->str, "I:", 2))
4896 : : {
4897 : 0 : state = AST_FIXED_H_ISO_NUMBERS;
4898 : : }
4899 [ # # ]: 0 : else if ( /*bReconn == INCHI_BAS &&*/ !inchi_memicmp(pLine->str, "CRV:", 4))
4900 : : {
4901 : 0 : state = AST_REVERSE_INFO_CRV;
4902 : : }
4903 [ # # # # ]: 0 : else if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "rA:", 3))
4904 : : {
4905 : 0 : state = AST_REVERSE_INFO_ATOMS;
4906 : : }
4907 [ # # # # ]: 0 : else if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "R:", 3))
4908 : : {
4909 : 0 : ret = 1; /* read the next segment */
4910 : 0 : state = AST_VERSION;
4911 : 0 : bMobileH = TAUT_YES;
4912 : 0 : bReconn = INCHI_REC;
4913 : : }
4914 : : else
4915 : : {
4916 : 0 : ret = RI_ERR_SYNTAX;
4917 : : }
4918 : 0 : break;
4919 : :
4920 : 0 : case AST_FIXED_H_ISO_NUMBERS:
4921 : 0 : ret = ParseAuxSegmentNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
4922 : 0 : state = AST_FIXED_H_ISO_ATOM_EQ;
4923 : 0 : break;
4924 : 0 : case AST_FIXED_H_ISO_ATOM_EQ:
4925 : 0 : ret = ParseAuxSegmentAtomEqu(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4926 : 0 : state = AST_FIXED_H_SP3_INV;
4927 : 0 : break;
4928 : 0 : case AST_FIXED_H_ISO_SP3_INV:
4929 : 0 : ret = ParseAuxSegmentSp3Inv(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4930 : 0 : state = AST_FIXED_H_ISO_SP3_INV_NUMBERS;
4931 : 0 : break;
4932 : 0 : case AST_FIXED_H_ISO_SP3_INV_NUMBERS:
4933 : 0 : ret = ParseAuxSegmentSp3InvNumbers(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
4934 : 0 : state = AST_REVERSE_INFO_CRV;
4935 : 0 : break;
4936 : 0 : case AST_REVERSE_INFO_CRV:
4937 : 0 : ret = ParseAuxSegmentReverseCRV(pLine->str, state);
4938 : : /* state = (bReconn == INCHI_BAS)? AST_REVERSE_INFO_ATOMS : AST_RECONNECTED_LAYER_FORK;*/
4939 : 0 : state = AST_REVERSE_INFO_ATOMS;
4940 : 0 : break;
4941 : 0 : case AST_REVERSE_INFO_ATOMS:
4942 : 0 : ret = ParseAuxSegmentReverseAtoms(pLine->str, state);
4943 : 0 : state = AST_REVERSE_INFO_BONDS;
4944 : 0 : break;
4945 : 0 : case AST_REVERSE_INFO_BONDS:
4946 : 0 : ret = ParseAuxSegmentReverseBonds(pLine->str, state);
4947 : 0 : state = AST_REVERSE_INFO_XYZ;
4948 : 0 : break;
4949 : 0 : case AST_REVERSE_INFO_XYZ:
4950 : 0 : ret = ParseAuxSegmentReverseXYZ(pLine->str, &pXYZ, state);
4951 : 0 : state = AST_RECONNECTED_LAYER_FORK;
4952 [ # # ]: 0 : if (ret > 0)
4953 : : {
4954 : 0 : nLenXYZ = ret - 1;
4955 : : }
4956 : 0 : break;
4957 : 0 : case AST_RECONNECTED_LAYER_FORK:
4958 [ # # # # ]: 0 : if (bReconn == INCHI_BAS && !inchi_memicmp(pLine->str, "R:", 3))
4959 : : {
4960 : 0 : ret = 1; /* read the next segment */
4961 : 0 : state = AST_VERSION;
4962 : 0 : bMobileH = TAUT_YES;
4963 : 0 : bReconn = INCHI_REC;
4964 : : }
4965 : : else
4966 : : {
4967 : 0 : ret = RI_ERR_SYNTAX;
4968 : : }
4969 : 0 : break;
4970 : : }
4971 [ # # ]: 0 : } while (c >= 0);
4972 : :
4973 [ # # ]: 0 : if (pXYZ) /* djb-rwth: fixing coverity ID #499576 */
4974 : : {
4975 : 0 : ret = AddAuxSegmentCoord(ret, pXYZ, nLenXYZ, pInpInChI, nNumComponents);
4976 : : }
4977 : : else
4978 : : {
4979 : 0 : ret = RI_ERR_ALLOC;
4980 : : }
4981 : :
4982 : 0 : exit_error:
4983 [ # # ]: 0 : if (pXYZ)
4984 : : {
4985 [ # # ]: 0 : inchi_free(pXYZ);
4986 : : }
4987 [ # # # # : 0 : if (ret >= 0 || c == RI_ERR_EOF || c == RI_ERR_EOL)
# # ]
4988 : : {
4989 : 0 : pLine->len = 0;
4990 : : }
4991 : :
4992 : 0 : return ret;
4993 : : }
4994 : :
4995 : :
4996 : : /****************************************************************************
4997 : : Read a single InChI input line
4998 : : ****************************************************************************/
4999 : 28 : int ReadInChILine(INCHI_IOSTREAM* pInp,
5000 : : SEGM_LINE* pLine,
5001 : : char** pStr,
5002 : : int* pState,
5003 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
5004 : : int nNumComponents[INCHI_NUM][TAUT_NUM],
5005 : : REM_PROTONS nNumProtons[INCHI_NUM][TAUT_NUM],
5006 : : int s[INCHI_NUM][TAUT_NUM][2],
5007 : : int* bStdFormat,
5008 : : int* input_has_save_opt,
5009 : : unsigned char* input_save_opt_bits,
5010 : : int bInchi2Struct,
5011 : : OAD_Polymer** ppPolymer,
5012 : : OAD_V3000** ppV3000)
5013 : : {
5014 : 28 : int c, ret = RI_ERR_ALLOC, len; /* djb-rwth: removing redundant variables */
5015 : 28 : int bMobileH = TAUT_YES, bReconn = INCHI_BAS;
5016 : 28 : const char szToken[] = INCHI_TOKEN;
5017 : : char* p;
5018 : 28 : int state = -1, prev_state = -1;
5019 : 28 : int bAbc = -1; /* -1=> undefined, 0=> decimal, 1=> abc (compressed) */
5020 : :
5021 : 28 : const int len_version_prefix = (int)(sizeof("InChI=1") - 1);
5022 : 28 : size_t k = 0;
5023 : 28 : unsigned char let1 = 0, let2 = 0;
5024 : 28 : const char a2p[] = "ABCDEFGHIJKLMNOP";
5025 : :
5026 : 28 : int na_total = 0; /* whole struct, without explH */
5027 : 28 : int nb_total = 0; /* whole struct, without explH */
5028 : : #if ( DISABLE_READ_COMPRESSED_INCHI==1 )
5029 : 28 : bAbc = 0;
5030 : : /* We do not support compressed InChI explicitly */
5031 : : /* Parsing compressed InChI easily fails on fake/fuzzing inputs */
5032 : : #endif
5033 : : /* memset( pLine, 0, sizeof( pLine[0] ) ); */
5034 : 28 : * pState = 0;
5035 : :
5036 : 0 : next_line:
5037 : : INCHI_HEAPCHK
5038 : : /* Got "InChI=1/" */
5039 [ - + ]: 28 : if (pLine->len)
5040 : : {
5041 : 0 : c = pLine->c;
5042 : : }
5043 : : else
5044 : : {
5045 : : INCHI_HEAPCHK
5046 : 28 : c = nGetInChISegment(pInp, pLine, szToken);
5047 : : INCHI_HEAPCHK
5048 : : }
5049 [ + - - + : 28 : if (pLine->str && (c == RI_ERR_EOF && !pLine->len && !pLine->str[0])) /* djb-rwth: fixing a NULL pointer dereference */
- - - - ]
5050 : : {
5051 : 0 : ret = c;
5052 : 0 : goto exit_function;
5053 : : }
5054 : : INCHI_HEAPCHK
5055 : :
5056 [ + - + - : 28 : if (pLine->str && (pLine->len == 0 || (c != SEG_END && c != RI_ERR_EOF) || !(p = strstr(pLine->str, "InChI=1")))) /* djb-rwth: fixing a NULL pointer dereference; addressing LLVM warning; ignoring LLVM warning: value used */
- + - - -
+ ]
5057 : : {
5058 [ # # # # ]: 0 : if (pLine->str && pLine->str == strstr(pLine->str, "Structure"))
5059 : : {
5060 [ # # ]: 0 : if (*pStr)
5061 : : {
5062 : : INCHI_HEAPCHK
5063 [ # # ]: 0 : inchi_free(*pStr);
5064 : : }
5065 : 0 : *pStr = pLine->str;
5066 : : /* bypass to the end of the 'Structure nnn' line */
5067 : 0 : memset(pLine, 0, sizeof(pLine[0])); /* djb-rwth: memset_s C11/Annex K variant? */
5068 [ # # # # : 0 : while (c && !INCHI_INP_EOL(c))
# # # # ]
5069 : : {
5070 : 0 : c = getInChIChar(pInp);
5071 : : }
5072 : 0 : goto next_line;
5073 : : }
5074 : : /* bypass to the end of unrecognized line */
5075 [ # # # # : 0 : while (c != RI_ERR_EOF && !INCHI_INP_EOL(c))
# # # # ]
5076 : : {
5077 : 0 : c = getInChIChar(pInp);
5078 : : }
5079 : 0 : pLine->len = 0;
5080 : : INCHI_HEAPCHK
5081 : 0 : goto next_line;
5082 : : }
5083 : :
5084 : :
5085 : : /*
5086 : : * Preserve the calculation-class designator and Molecular Inorganics
5087 : : * subversion designator from the input prefix. Historical InChI=1/...
5088 : : * remains accepted as non-standard input.
5089 : : */
5090 : 28 : *bStdFormat = INCHI_INPUT_CLASS_NONSTANDARD;
5091 : :
5092 [ + - ]: 28 : if (pLine->str &&
5093 [ + + ]: 28 : (pLine->len == len_version_prefix + 1 ||
5094 [ + + ]: 11 : (pLine->len == len_version_prefix + 2 &&
5095 [ + - ]: 6 : pLine->str[len_version_prefix + 1] == 'B')))
5096 : : {
5097 [ + + + - ]: 23 : switch (pLine->str[len_version_prefix])
5098 : : {
5099 : 16 : case 'S':
5100 : 16 : *bStdFormat = INCHI_INPUT_CLASS_STANDARD;
5101 : 16 : break;
5102 : :
5103 : 4 : case 'B':
5104 : 4 : *bStdFormat = INCHI_INPUT_CLASS_BETA;
5105 : 4 : break;
5106 : :
5107 : 3 : case 'N':
5108 : 3 : *bStdFormat = INCHI_INPUT_CLASS_NONSTANDARD;
5109 : 3 : break;
5110 : : }
5111 : :
5112 [ + + ]: 23 : if (pLine->len == len_version_prefix + 2 &&
5113 [ + - ]: 6 : strchr("SBN", pLine->str[len_version_prefix]))
5114 : : {
5115 : 6 : *bStdFormat |= INCHI_INPUT_MI_MARKER;
5116 : : }
5117 : : }
5118 : :
5119 : 28 : state = IST_MOBILE_H_FORMULA;
5120 : 28 : ret = 1; /* means read the next segment */
5121 : : do
5122 : : {
5123 : : /* read the next segment up to the '/' */
5124 : : INCHI_HEAPCHK
5125 [ + + ]: 163 : if (ret < 0)
5126 : : {
5127 : 9 : *pState = prev_state;
5128 : 9 : break;
5129 : : }
5130 [ - + ]: 154 : prev_state = state + (bReconn ? IST_HAPPENED_IN_RECMET : 0);
5131 [ + + ]: 154 : if (0 < ret)
5132 : : {
5133 : : /* read next segment */
5134 [ + + - + ]: 94 : if (c != RI_ERR_EOF && c != SEG_END)
5135 : : {
5136 : : /* abnormal reading result; should not happen */
5137 : : /* unless we got backslash-SaveOpt */
5138 [ # # ]: 0 : if (c == '\\')
5139 : : {
5140 : : /* May be SaveOpt */
5141 : 0 : *input_has_save_opt = 1;
5142 : : }
5143 : 0 : k = 0;
5144 [ # # # # : 0 : while (c != RI_ERR_EOF && !INCHI_INP_EOL(c))
# # # # ]
5145 : : {
5146 : : /* bypass to the end of line or file */
5147 : 0 : c = getInChIChar(pInp);
5148 : 0 : k++;
5149 [ # # ]: 0 : if (k == 1)
5150 : : {
5151 : 0 : let1 = c;
5152 : : }
5153 : : else
5154 : : {
5155 [ # # ]: 0 : if (k == 2)
5156 : : {
5157 : 0 : let2 = c;
5158 : : }
5159 : : }
5160 : : }
5161 [ # # ]: 0 : if (k != 3)
5162 : : {
5163 : : /* not a valid SaveOpt which must be of two chars */
5164 : 0 : *input_has_save_opt = 0;
5165 : : /* djb-rwth: removing redundant code */
5166 : : }
5167 : : else
5168 : : {
5169 : : /* may be SaveOpt - analyze the content */
5170 [ # # # # ]: 0 : if ((let2 >= 'A') && (let2 <= 'D')) /* letter-2 OK */
5171 : : {
5172 : 0 : *input_has_save_opt = 0;
5173 : 0 : *input_save_opt_bits = 0;
5174 [ # # ]: 0 : for (k = 0; k < 16; k++)
5175 : : {
5176 [ # # ]: 0 : if (a2p[k] == let1) /* letter-1 OK */
5177 : : {
5178 : 0 : *input_save_opt_bits = (unsigned char)k;
5179 : 0 : *input_has_save_opt = 1;
5180 : 0 : break;
5181 : : }
5182 : : }
5183 [ # # ]: 0 : if (*input_has_save_opt)
5184 : : {
5185 [ # # # # ]: 0 : if (let2 == 'B' || let2 == 'D')
5186 : : {
5187 : 0 : *input_save_opt_bits |= SAVE_OPT_15T;
5188 : : }
5189 [ # # # # ]: 0 : if (let2 == 'C' || let2 == 'D')
5190 : : {
5191 : 0 : *input_save_opt_bits |= SAVE_OPT_KET;
5192 : : }
5193 : : }
5194 : : }
5195 : : }
5196 : :
5197 [ # # ]: 0 : ret = (c == RI_ERR_EOF) ? RI_ERR_EOF : RI_ERR_EOL; /* end of line */
5198 : 0 : pLine->len = 0;
5199 : 0 : pLine->c = ret;
5200 : 0 : break; /* exit */
5201 : : }
5202 [ + + ]: 94 : if (c == RI_ERR_EOF)
5203 : : {
5204 : 19 : ret = RI_ERR_EOF; /* end of line */
5205 : 19 : break;
5206 : : }
5207 [ + - ]: 75 : if (c == SEG_END)
5208 : : {
5209 : 75 : c = nGetInChISegment(pInp, pLine, szToken);
5210 : : }
5211 [ - + ]: 75 : if (c < 0)
5212 : : {
5213 : 0 : goto exit_error; /* error */
5214 : : }
5215 [ - + ]: 75 : if (!pLine->len)
5216 : : {
5217 : 0 : ret = RI_ERR_EOL; /* end of line */
5218 : 0 : break;
5219 : : }
5220 : : /* djb-rwth: removing redundant code */
5221 : :
5222 : : /*
5223 : : if ( fst == 'z' )
5224 : : {
5225 : : ret = RI_ERR_EOL;
5226 : : break;
5227 : : }*/
5228 : : }
5229 : : /* process the seqment */
5230 [ + + + + : 135 : switch (state)
+ + + + +
+ + + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ]
5231 : : {
5232 : : /* Mobile H, M */ /* / */
5233 : 28 : case IST_MOBILE_H_FORMULA:
5234 : 28 : bMobileH = TAUT_YES;
5235 : : #if ( FIX_GAF_2019_2==1 )
5236 : : /* hack: pass state in na_total (will be updated in ParseSegmentFormula anyway) */
5237 : 28 : na_total = state;
5238 : : #endif
5239 : 28 : ret = ParseSegmentFormula(pLine->str, bMobileH, pInpInChI[bReconn],
5240 : 28 : nNumComponents[bReconn], &na_total);
5241 : 28 : state = IST_MOBILE_H_CONNECTIONS;
5242 : 28 : break;
5243 : 22 : case IST_MOBILE_H_CONNECTIONS: /* /c */
5244 : 22 : ret = ParseSegmentConnections(pLine->str, bMobileH, &pInpInChI[bReconn][bMobileH],
5245 : 22 : &nNumComponents[bReconn][bMobileH], &bAbc, &nb_total);
5246 : 22 : state = IST_MOBILE_H;
5247 : 22 : break;
5248 : 22 : case IST_MOBILE_H: /* /h */
5249 : 22 : ret = ParseSegmentMobileH(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], &bAbc);
5250 : 22 : state = IST_MOBILE_H_CHARGE;
5251 : 22 : break;
5252 : 11 : case IST_MOBILE_H_CHARGE: /* /q */
5253 : 11 : ret = ParseSegmentCharge(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn]);
5254 : 11 : state = IST_MOBILE_H_PROTONS;
5255 : 11 : break;
5256 : 11 : case IST_MOBILE_H_PROTONS: /* /p */
5257 : 11 : ret = ParseSegmentProtons(pLine->str, bMobileH, nNumProtons[bReconn], nNumComponents[bReconn]);
5258 : 11 : state = IST_MOBILE_H_POLYMER;
5259 : 11 : break;
5260 : 11 : case IST_MOBILE_H_POLYMER: /* /z */
5261 : 11 : ret = ParseSegmentPolymer(pLine->str, bMobileH,
5262 : 11 : nNumProtons[bReconn], nNumComponents[bReconn],
5263 : : na_total, nb_total, bInchi2Struct, ppPolymer, ppV3000);
5264 [ + + ]: 11 : if (*ppPolymer)
5265 : 1 : (*ppPolymer)->is_in_reconn = bReconn;
5266 : 11 : state = IST_MOBILE_H_SP2;
5267 : 11 : break;
5268 : 10 : case IST_MOBILE_H_SP2: /* /b */
5269 : 10 : ret = ParseSegmentSp2(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5270 : 10 : state = IST_MOBILE_H_SP3;
5271 : 10 : break;
5272 : 6 : case IST_MOBILE_H_SP3: /* t */
5273 : 6 : ret = ParseSegmentSp3(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5274 : 6 : state = IST_MOBILE_H_SP3_M;
5275 : 6 : break;
5276 : 4 : case IST_MOBILE_H_SP3_M: /* /m */
5277 : 4 : ret = ParseSegmentSp3m(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
5278 : 4 : state = IST_MOBILE_H_SP3_S;
5279 : 4 : break;
5280 : 4 : case IST_MOBILE_H_SP3_S: /* /s */
5281 : 4 : ret = ParseSegmentSp3s(pLine->str, bMobileH, pInpInChI[bReconn], s[bReconn], nNumComponents[bReconn], state);
5282 : 4 : state = IST_MOBILE_H_ISO_LAYER_FORK;
5283 : 4 : break;
5284 : 3 : case IST_MOBILE_H_ISO_LAYER_FORK:
5285 : : /* find layer type after M */
5286 : 3 : ret = 0;
5287 [ + - - - ]: 3 : switch (pLine->str[0])
5288 : : {
5289 : 3 : case 'i':
5290 : 3 : state = IST_MOBILE_H_ISO_ATOMS; /* MI */
5291 : 3 : break;
5292 : 0 : case 'f':
5293 : 0 : state = IST_FIXED_H_FORMULA; /* F */
5294 : 0 : break;
5295 : 0 : case 'r':
5296 : 0 : state = IST_RECONNECTED_FORMULA; /* reconnected */
5297 : 0 : break;
5298 : 0 : default:
5299 : 0 : ret = RI_ERR_SYNTAX;
5300 : : }
5301 [ + - + - : 3 : if (INCHI_INP_EOL(c) && ret == 0 && !pLine->str[1])
- + - - -
- ]
5302 : : {
5303 [ # # ]: 0 : prev_state = state + (bReconn ? IST_HAPPENED_IN_RECMET : 0);
5304 : 0 : ret = RI_ERR_SYNTAX; /* empty layer /i or /f or /r at the end of InChI line */
5305 : : }
5306 : : else
5307 : : {
5308 [ + - - + ]: 3 : if (!ret && state != IST_MOBILE_H_ISO_ATOMS)
5309 : : {
5310 : 0 : len = (int)strlen(pLine->str);
5311 [ # # ]: 0 : if (len > 1)
5312 : : {
5313 : 0 : memmove(pLine->str, pLine->str + 1, len);
5314 : : }
5315 : : else
5316 : : {
5317 : 0 : ret = 1; /* read the next segment */
5318 : : }
5319 : : }
5320 : : }
5321 : 3 : break;
5322 : : /* Mobile H, isotopic, MI */
5323 : 3 : case IST_MOBILE_H_ISO_ATOMS: /* i */
5324 : 3 : ret = ParseSegmentIsoAtoms(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5325 : 3 : state = IST_MOBILE_H_ISO_EXCH_H;
5326 : 3 : break;
5327 : 0 : case IST_MOBILE_H_ISO_EXCH_H: /* /i/h */
5328 : 0 : ret = ParseSegmentIsoExchgH(pLine->str, bMobileH, nNumProtons[bReconn], nNumComponents[bReconn], state, &bAbc);
5329 : 0 : state = IST_MOBILE_H_ISO_SP2;
5330 : 0 : break;
5331 : 0 : case IST_MOBILE_H_ISO_SP2: /* /i/b */
5332 : 0 : ret = ParseSegmentSp2(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5333 : 0 : state = IST_MOBILE_H_ISO_SP3;
5334 : 0 : break;
5335 : 0 : case IST_MOBILE_H_ISO_SP3: /* /i/t */
5336 : 0 : ret = ParseSegmentSp3(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5337 : 0 : state = IST_MOBILE_H_ISO_SP3_M;
5338 : 0 : break;
5339 : 0 : case IST_MOBILE_H_ISO_SP3_M: /* /i/m */
5340 : 0 : ret = ParseSegmentSp3m(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
5341 : 0 : state = IST_MOBILE_H_ISO_SP3_S;
5342 : 0 : break;
5343 : 0 : case IST_MOBILE_H_ISO_SP3_S: /* /i/s */
5344 : 0 : ret = ParseSegmentSp3s(pLine->str, bMobileH, pInpInChI[bReconn], s[bReconn], nNumComponents[bReconn], state);
5345 : 0 : state = IST_FIXED_H_LAYER_FORK;
5346 : 0 : break;
5347 : 0 : case IST_FIXED_H_LAYER_FORK:
5348 : : /* find layer type after MI */
5349 : 0 : ret = 0;
5350 [ # # # ]: 0 : switch (pLine->str[0])
5351 : : {
5352 : 0 : case 'f':
5353 : 0 : state = IST_FIXED_H_FORMULA; /* F */
5354 : 0 : break;
5355 : 0 : case 'r':
5356 : 0 : state = IST_RECONNECTED_FORMULA; /* reconnected */
5357 : 0 : break;
5358 : 0 : default:
5359 : 0 : ret = RI_ERR_SYNTAX;
5360 : : }
5361 [ # # # # : 0 : if (INCHI_INP_EOL(c) && ret == 0 && !pLine->str[1])
# # # # #
# ]
5362 : : {
5363 [ # # ]: 0 : prev_state = state + (bReconn ? IST_HAPPENED_IN_RECMET : 0);
5364 : 0 : ret = RI_ERR_SYNTAX; /* empty layer /f or /r at the end of InChI line */
5365 : : }
5366 : : else
5367 : : {
5368 [ # # ]: 0 : if (!ret)
5369 : : {
5370 : 0 : len = (int)strlen(pLine->str);
5371 [ # # ]: 0 : if (len > 1)
5372 : : {
5373 : 0 : memmove(pLine->str, pLine->str + 1, len);
5374 : : }
5375 : : else
5376 : : {
5377 : 0 : ret = 1; /* read the next segment */
5378 : : }
5379 : : }
5380 : : }
5381 : 0 : break;
5382 : :
5383 : : /* Fixed H, F */
5384 : 0 : case IST_FIXED_H_FORMULA:
5385 : 0 : bMobileH = TAUT_NON;
5386 : : #if ( FIX_GAF_2019_2==1 )
5387 : : /* hack: pass state in na_total (will be updated in ParseSegmentFormula anyway) */
5388 : 0 : na_total = state;
5389 : : #endif
5390 : 0 : ret = ParseSegmentFormula(pLine->str, bMobileH, pInpInChI[bReconn],
5391 : 0 : nNumComponents[bReconn], &na_total);
5392 : 0 : state = IST_FIXED_H;
5393 : 0 : break;
5394 : 0 : case IST_FIXED_H: /* /f/h */
5395 : 0 : ret = ParseSegmentMobileH(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], &bAbc);
5396 : 0 : state = IST_FIXED_H_CHARGE;
5397 : 0 : break;
5398 : 0 : case IST_FIXED_H_CHARGE: /* /f/q */
5399 : 0 : ret = ParseSegmentCharge(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn]);
5400 : 0 : state = IST_FIXED_H_SP2;
5401 : 0 : break;
5402 : 0 : case IST_FIXED_H_SP2: /* /f/b */
5403 : 0 : ret = ParseSegmentSp2(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5404 : 0 : state = IST_FIXED_H_SP3;
5405 : 0 : break;
5406 : 0 : case IST_FIXED_H_SP3: /* /f/t */
5407 : 0 : ret = ParseSegmentSp3(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5408 : 0 : state = IST_FIXED_H_SP3_M;
5409 : 0 : break;
5410 : 0 : case IST_FIXED_H_SP3_M: /* /f/m */
5411 : 0 : ret = ParseSegmentSp3m(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
5412 : 0 : state = IST_FIXED_H_SP3_S;
5413 : 0 : break;
5414 : 0 : case IST_FIXED_H_SP3_S: /* /f/s */
5415 : 0 : ret = ParseSegmentSp3s(pLine->str, bMobileH, pInpInChI[bReconn], s[bReconn], nNumComponents[bReconn], state);
5416 : 0 : state = IST_FIXED_H_PERMUTATION;
5417 : 0 : break;
5418 : 0 : case IST_FIXED_H_PERMUTATION: /* /f/o */
5419 : 0 : ret = ParseSegmentPerm(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5420 : 0 : state = IST_FIXED_H_ISO_LAYER_FORK;
5421 : 0 : break;
5422 : 0 : case IST_FIXED_H_ISO_LAYER_FORK:
5423 : : /* find layer type after M */
5424 : 0 : ret = 0;
5425 [ # # # ]: 0 : switch (pLine->str[0])
5426 : : {
5427 : 0 : case 'i':
5428 : 0 : state = IST_FIXED_H_ISO_ATOMS; /* FI */
5429 : 0 : break;
5430 : 0 : case 'r':
5431 : 0 : state = IST_RECONNECTED_FORMULA; /* reconnected */
5432 : 0 : break;
5433 : 0 : default:
5434 : 0 : ret = RI_ERR_SYNTAX;
5435 : : }
5436 [ # # # # : 0 : if (INCHI_INP_EOL(c) && ret == 0 && !pLine->str[1])
# # # # #
# ]
5437 : : {
5438 [ # # ]: 0 : prev_state = state + (bReconn ? IST_HAPPENED_IN_RECMET : 0);
5439 : 0 : ret = RI_ERR_SYNTAX; /* empty layer /i or /r at the end of InChI line */
5440 : : }
5441 : : else
5442 : : {
5443 [ # # # # ]: 0 : if (!ret && state != IST_FIXED_H_ISO_ATOMS)
5444 : : {
5445 : 0 : len = (int)strlen(pLine->str);
5446 [ # # ]: 0 : if (len > 1)
5447 : : {
5448 : 0 : memmove(pLine->str, pLine->str + 1, len);
5449 : : }
5450 : : else
5451 : : {
5452 : 0 : ret = 1; /* read the next segment */
5453 : : }
5454 : : }
5455 : : }
5456 : 0 : break;
5457 : :
5458 : : /* Fixed H, isotopic, FI */
5459 : 0 : case IST_FIXED_H_ISO_ATOMS: /* /f/i */
5460 : 0 : ret = ParseSegmentIsoAtoms(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5461 : 0 : state = IST_FIXED_H_ISO_SP2;
5462 : 0 : break;
5463 : 0 : case IST_FIXED_H_ISO_SP2: /* /f/i/b */
5464 : 0 : ret = ParseSegmentSp2(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5465 : 0 : state = IST_FIXED_H_ISO_SP3;
5466 : 0 : break;
5467 : 0 : case IST_FIXED_H_ISO_SP3: /* /f/i/t */
5468 : 0 : ret = ParseSegmentSp3(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5469 : 0 : state = IST_FIXED_H_ISO_SP3_M;
5470 : 0 : break;
5471 : 0 : case IST_FIXED_H_ISO_SP3_M: /* /f/i/m */
5472 : 0 : ret = ParseSegmentSp3m(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state);
5473 : 0 : state = IST_FIXED_H_ISO_SP3_S;
5474 : 0 : break;
5475 : 0 : case IST_FIXED_H_ISO_SP3_S: /* /f/i/s */
5476 : 0 : ret = ParseSegmentSp3s(pLine->str, bMobileH, pInpInChI[bReconn], s[bReconn], nNumComponents[bReconn], state);
5477 : 0 : state = IST_FIXED_H_ISO_PERMUTATION;
5478 : 0 : break;
5479 : 0 : case IST_FIXED_H_ISO_PERMUTATION: /* /f/i/o */
5480 : 0 : ret = ParseSegmentPerm(pLine->str, bMobileH, pInpInChI[bReconn], nNumComponents[bReconn], state, &bAbc);
5481 : 0 : state = IST_RECONNECTED_LAYER_FORK;
5482 : 0 : break;
5483 : 0 : case IST_RECONNECTED_LAYER_FORK:
5484 : : /* find layer type after FI */
5485 : 0 : ret = 0;
5486 [ # # ]: 0 : switch (pLine->str[0])
5487 : : {
5488 : 0 : case 'r':
5489 : 0 : state = IST_RECONNECTED_FORMULA; /* reconnected */
5490 : 0 : break;
5491 : 0 : default:
5492 : 0 : ret = RI_ERR_SYNTAX;
5493 : : }
5494 [ # # # # : 0 : if (INCHI_INP_EOL(c) && ret == 0 && !pLine->str[1])
# # # # #
# ]
5495 : : {
5496 [ # # ]: 0 : prev_state = state + (bReconn ? IST_HAPPENED_IN_RECMET : 0);
5497 : 0 : ret = RI_ERR_SYNTAX; /* empty layer /r at the end of InChI line */
5498 : : }
5499 : : else
5500 : : {
5501 [ # # ]: 0 : if (!ret)
5502 : : {
5503 : 0 : len = (int)strlen(pLine->str);
5504 [ # # ]: 0 : if (len > 1)
5505 : : {
5506 : 0 : memmove(pLine->str, pLine->str + 1, len);
5507 : : }
5508 : : else
5509 : : {
5510 : 0 : ret = 1; /* read the next segment */
5511 : : }
5512 : : }
5513 : : }
5514 : 0 : break;
5515 : 0 : case IST_RECONNECTED_FORMULA:
5516 : : #if ( FIX_GAF_2019_1==1 )
5517 [ # # ]: 0 : if (bReconn == INCHI_REC)
5518 : : {
5519 : : /* reconnected layer may appear only once */
5520 : 0 : ret = RI_ERR_SYNTAX;
5521 : 0 : break;
5522 : : }
5523 : : #endif
5524 : 0 : bReconn = INCHI_REC;
5525 : 0 : bMobileH = TAUT_YES;
5526 : 0 : state = IST_MOBILE_H_FORMULA;
5527 : 0 : break;
5528 : : }
5529 [ + - ]: 135 : } while (c >= 0);
5530 : :
5531 : 28 : exit_function:;
5532 : 28 : exit_error:;
5533 : :
5534 : : INCHI_HEAPCHK
5535 : :
5536 [ + + + + : 28 : if (ret >= 0 || c == RI_ERR_EOF || c == RI_ERR_EOL)
- + ]
5537 : : {
5538 : 26 : pLine->len = 0;
5539 : : }
5540 : :
5541 : 28 : return ret;
5542 : : }
5543 : :
5544 : :
5545 : : /****************************************************************************
5546 : : Parse InChI layer "/i/h"
5547 : : ****************************************************************************/
5548 : 0 : int ParseSegmentIsoExchgH(const char* str,
5549 : : int bMobileH,
5550 : : REM_PROTONS nNumProtons[],
5551 : : int pnNumComponents[],
5552 : : int state,
5553 : : int* pbAbc)
5554 : : {
5555 : : /* Pass 1: count bonds and find actual numbers of atom */
5556 : : const char* p, * q, * pStart, * pEnd;
5557 : 0 : int ret = 0, num, i, i_prev;
5558 : : static const char abc_h[] = "hdt";
5559 : :
5560 [ # # ]: 0 : if (str[0] != 'h')
5561 : : {
5562 : 0 : return 0;
5563 : : }
5564 : :
5565 : 0 : pStart = (char*)str + 1;
5566 : :
5567 [ # # # # ]: 0 : if (!(bMobileH == TAUT_YES && state == IST_MOBILE_H_ISO_EXCH_H))
5568 : : {
5569 : 0 : return RI_ERR_PROGR; /* program error */
5570 : : }
5571 : :
5572 [ # # ]: 0 : if (!(strchr(pStart, ';'))) /* djb-rwth: removing redundant code */
5573 : : {
5574 : 0 : pEnd = pStart + strlen(pStart);
5575 : : }
5576 : : else
5577 : : {
5578 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5579 : 0 : goto exit_function;
5580 : : }
5581 : 0 : p = pStart;
5582 : :
5583 [ # # # # ]: 0 : if (p < pEnd && *pbAbc == -1)
5584 : : {
5585 : : /* check if compressed InChI */
5586 : : /* compressed: /hNtNdNh where N is a decimal number */
5587 : : /* uncompressed: /hT[n]D[n]H[n] where n > 1 is a decimal number */
5588 : 0 : *pbAbc = isdigit(UCINT * p) ? 1 : 0;
5589 : : }
5590 : :
5591 [ # # ]: 0 : if (*pbAbc == 1)
5592 : : {
5593 : 0 : i_prev = (int)sizeof(abc_h);
5594 [ # # ]: 0 : while (p < pEnd)
5595 : : {
5596 : 0 : num = (int)inchi_strtol(p, &q, 10);
5597 : : #if ( CHECK_STRTOL_ATNUMB==1 )
5598 [ # # # # ]: 0 : if (num > MAX_ATOMS || num < 0)
5599 : : {
5600 : 0 : ret = RI_ERR_SYNTAX;
5601 : 0 : goto exit_function;
5602 : : }
5603 : : #endif
5604 [ # # # # : 0 : if (0 >= num || p == q || q >= pEnd)
# # ]
5605 : : {
5606 : 0 : ret = RI_ERR_SYNTAX;
5607 : 0 : goto exit_function;
5608 : : }
5609 : 0 : p = strchr((char*)abc_h, *q);
5610 [ # # # # : 0 : if (p && (i = (int)(p - abc_h)) < i_prev && (i < NUM_H_ISOTOPES)) /* djb-rwth: additional condition for buffer overrun prevention */
# # ]
5611 : : {
5612 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[i] = (NUM_H)num;
5613 : 0 : p = q + 1;
5614 : 0 : i_prev = i;
5615 : : }
5616 : : else
5617 : : {
5618 : 0 : ret = RI_ERR_SYNTAX;
5619 : 0 : goto exit_function;
5620 : : }
5621 : : }
5622 : : }
5623 : : else
5624 : : {
5625 [ # # ]: 0 : if (*p == 'T')
5626 : : {
5627 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[2] = 1;
5628 : 0 : p++;
5629 [ # # ]: 0 : if (isdigit(UCINT p[0]))
5630 : : {
5631 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[2] = (NUM_H)inchi_strtol(p, &q, 10);
5632 : 0 : p = q;
5633 : : }
5634 : : }
5635 [ # # ]: 0 : if (*p == 'D')
5636 : : {
5637 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[1] = 1;
5638 : 0 : p++;
5639 [ # # ]: 0 : if (isdigit(UCINT p[0]))
5640 : : {
5641 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[1] = (NUM_H)inchi_strtol(p, &q, 10);
5642 : 0 : p = q;
5643 : : }
5644 : : }
5645 [ # # ]: 0 : if (*p == 'H')
5646 : : {
5647 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[0] = 1;
5648 : 0 : p++;
5649 [ # # ]: 0 : if (isdigit(UCINT p[0]))
5650 : : {
5651 : 0 : nNumProtons[bMobileH].nNumRemovedIsotopicH[0] = (NUM_H)inchi_strtol(p, &q, 10);
5652 : 0 : p = q;
5653 : : }
5654 : : }
5655 : : }
5656 [ # # ]: 0 : if (p != pEnd)
5657 : : {
5658 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5659 : 0 : goto exit_function;
5660 : : }
5661 : 0 : ret = 1;
5662 : :
5663 : 0 : exit_function:
5664 : :
5665 : 0 : return ret;
5666 : : }
5667 : :
5668 : :
5669 : : /****************************************************************************/
5670 : 0 : int ParseSegmentPerm(const char* str,
5671 : : int bMobileH,
5672 : : INChI* pInpInChI[],
5673 : : int ppnNumComponents[],
5674 : : int state,
5675 : : int* pbAbc)
5676 : : {
5677 : : int nNumComponents, iComponent1, iComponent2, numTrans;
5678 : : const char* p, * q, * pStart, * pEnd, * pPermStart, * pPermEnd;
5679 : 0 : int ret = 0;
5680 : 0 : INChI* pInChI = pInpInChI[bMobileH]; /* bMobileH should be TAUT_NON = 0 */
5681 : : INChI tmp;
5682 : 0 : int base = 10;
5683 : :
5684 [ # # ]: 0 : if (str[0] != 'o')
5685 : : {
5686 : 0 : return 0;
5687 : : }
5688 : :
5689 : : /* djb-rwth: fixing oss-fuzz issue #66746 */
5690 [ # # ]: 0 : if (!pInChI)
5691 : : {
5692 : 0 : return RI_ERR_ALLOC;
5693 : : }
5694 : :
5695 : 0 : pStart = (char*)str + 1;
5696 : 0 : nNumComponents = ppnNumComponents[bMobileH];
5697 : :
5698 [ # # # # : 0 : if (!(bMobileH == TAUT_NON && (state == IST_FIXED_H_PERMUTATION || state == IST_FIXED_H_ISO_PERMUTATION)))
# # ]
5699 : : {
5700 : 0 : return RI_ERR_PROGR; /* program error */
5701 : : }
5702 : :
5703 [ # # ]: 0 : if (!(strchr(pStart, ';'))) /* djb-rwth: removing redundant code */
5704 : : {
5705 : 0 : pEnd = pStart + strlen(pStart);
5706 : : }
5707 : : else
5708 : : {
5709 : 0 : return RI_ERR_SYNTAX; /* syntax error */
5710 : : }
5711 [ # # ]: 0 : while (pStart < pEnd)
5712 : : {
5713 : : /* cycle over components; rearrange Fixed H components in order of Mobile H components */
5714 : : /* if /o(1,2,3) then reaarange Fixed H components in this way: tmp<-1, 1<-2, 2<-3, 3<-tmp */
5715 [ # # ]: 0 : if (*pStart != '(')
5716 : : {
5717 : 0 : ret = RI_ERR_SYNTAX;
5718 : 0 : goto exit_function;
5719 : : }
5720 : 0 : pPermStart = pStart + 1;
5721 : 0 : memset(&tmp, 0, sizeof(tmp)); /* initialization 2006-03 */ /* djb-rwth: memset_s C11/Annex K variant? */
5722 [ # # # # ]: 0 : if (!(pPermEnd = strchr(pPermStart, ')')) || pPermEnd == pPermStart)
5723 : : {
5724 : 0 : ret = RI_ERR_SYNTAX;
5725 : 0 : goto exit_function;
5726 : : }
5727 : :
5728 [ # # # # ]: 0 : if (pPermStart < pPermEnd && *pbAbc == -1)
5729 : : {
5730 : : /* check if compressed InChI */
5731 : 0 : *pbAbc = isupper(UCINT * pPermStart) ? 1 : 0;
5732 : : }
5733 [ # # ]: 0 : base = (*pbAbc == 1) ? ALPHA_BASE : 10;
5734 : :
5735 : : /* permutation cycle */
5736 [ # # ]: 0 : if (*pbAbc == 1)
5737 : : {
5738 [ # # ]: 0 : for (p = pPermStart, iComponent2 = numTrans = 0; p < pPermEnd; iComponent2 = iComponent1, p = q)
5739 : : {
5740 : : /* get first atom number */
5741 [ # # # # ]: 0 : if (0 >= (iComponent1 = (int)inchi_strtol(p, &q, base)) || iComponent1 > nNumComponents)
5742 : : {
5743 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5744 : 0 : goto exit_function;
5745 : : }
5746 : : #if ( FIX_GAF_2019_2==1 )
5747 [ # # # # ]: 0 : if ((iComponent1 - 1 > nNumComponents - 1) || (iComponent1 - 1 < 0))
5748 : : {
5749 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5750 : 0 : goto exit_function;
5751 : : }
5752 : : #endif
5753 [ # # ]: 0 : if (iComponent2)
5754 : : {
5755 : 0 : pInChI[iComponent2 - 1] = pInChI[iComponent1 - 1];
5756 : 0 : numTrans++;
5757 : : }
5758 : : else
5759 : : {
5760 : 0 : tmp = pInChI[iComponent1 - 1]; /* on the 1st pass save Component1 */
5761 : : }
5762 : : }
5763 : : }
5764 : : else
5765 : : {
5766 [ # # ]: 0 : for (p = pPermStart, iComponent2 = numTrans = 0; p < pPermEnd; iComponent2 = iComponent1, p = q + (*q == ','))
5767 : : {
5768 : : /* get first atom number */
5769 [ # # ]: 0 : if (!isdigit(UCINT * p))
5770 : : {
5771 : 0 : ret = RI_ERR_SYNTAX;
5772 : 0 : goto exit_function;
5773 : : }
5774 : 0 : iComponent1 = (int)inchi_strtol(p, &q, 10);
5775 [ # # # # ]: 0 : if ((iComponent1 < 1) || (iComponent1 > nNumComponents)) /* djb-rwth: fixing oss-fuzz issue #66746 */
5776 : : {
5777 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5778 : 0 : goto exit_function;
5779 : : }
5780 [ # # ]: 0 : if (iComponent2)
5781 : : {
5782 : 0 : pInChI[iComponent2 - 1] = pInChI[iComponent1 - 1];
5783 : 0 : numTrans++;
5784 : : }
5785 : : else
5786 : : {
5787 : 0 : tmp = pInChI[iComponent1 - 1]; /* on the 1st pass save Component1 */
5788 : : }
5789 : : }
5790 : : }
5791 : 0 : pInChI[iComponent2 - 1] = tmp;
5792 [ # # # # ]: 0 : if (!numTrans || p != pPermEnd)
5793 : : {
5794 : 0 : ret = RI_ERR_SYNTAX;
5795 : 0 : goto exit_function;
5796 : : }
5797 : : else
5798 : : {
5799 : 0 : pStart = p + 1;
5800 : : }
5801 : : }
5802 : 0 : ret = 1;
5803 : :
5804 : 0 : exit_function:
5805 : :
5806 : 0 : return ret;
5807 : : }
5808 : :
5809 : :
5810 : : /****************************************************************************
5811 : : Parse InChI layer "/i"
5812 : : ****************************************************************************/
5813 : 3 : int ParseSegmentIsoAtoms(const char* str,
5814 : : int bMobileH,
5815 : : INChI* pInpInChI[],
5816 : : int ppnNumComponents[],
5817 : : int state,
5818 : : int* pbAbc)
5819 : : {
5820 : : int i, mpy_component, val;
5821 : 3 : int nNumComponents, iComponent, len = 0, iAtom;
5822 : : int nAtom1; /* djb-rwth: fixing coverity ID #499573 */
5823 : : const char* p, * q, * t, * pStart, * pEnd, * r;
5824 : 3 : int ret = 0;
5825 : 3 : INChI* pInChI = pInpInChI[bMobileH];
5826 : 3 : INChI* pInChIFrom = NULL;
5827 : 3 : INChI_IsotopicAtom** pIsotopicAtom = NULL;
5828 : : INChI_IsotopicAtom isoAtom;
5829 : :
5830 : 3 : const char mult_type[] = "mnMNe";
5831 : 3 : const char parity_type[] = "-+TDH";
5832 : 3 : int bIsoFrom, nCpyType = CPY_ISO_AT;
5833 : 3 : int base = 10;
5834 : 3 : int if_cnd = 1; /* djb-rwth: needed for some if condition restructuring */
5835 : :
5836 [ - + ]: 3 : if (str[0] != 'i')
5837 : : {
5838 : 0 : return 0;
5839 : : }
5840 : :
5841 : 3 : pStart = (char*)str + 1;
5842 : 3 : iComponent = 0;
5843 : 3 : nNumComponents = ppnNumComponents[bMobileH];
5844 : :
5845 [ + - - + : 3 : if (!((bMobileH == TAUT_YES && state == IST_MOBILE_H_ISO_ATOMS) ||
- - ]
5846 [ # # ]: 0 : (bMobileH == TAUT_NON && state == IST_FIXED_H_ISO_ATOMS))) /* djb-rwth: addressing LLVM warnings */
5847 : : {
5848 : 0 : return RI_ERR_PROGR; /* program error */
5849 : : }
5850 [ - + ]: 3 : if (!*pStart)
5851 : : {
5852 : 0 : return nNumComponents + 1; /* no isotopic atoms */
5853 : : }
5854 : :
5855 : : while (1)
5856 : : {
5857 : : /* cycle over components */
5858 [ + - ]: 3 : if (!(pEnd = strchr(pStart, ';')))
5859 : : {
5860 : 3 : pEnd = pStart + strlen(pStart);
5861 : : }
5862 [ - + - - ]: 3 : if ((p = strchr(pStart, '*')) && p < pEnd)
5863 : : {
5864 : 0 : mpy_component = (int)inchi_strtol(pStart, &q, 10);
5865 [ # # ]: 0 : if (p != q)
5866 : : {
5867 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5868 : 0 : goto exit_function;
5869 : : }
5870 : : #if (FIX_DALKE_BUGS == 1)
5871 [ # # ]: 0 : if (iComponent + mpy_component > nNumComponents)
5872 : : {
5873 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5874 : 0 : goto exit_function;
5875 : : }
5876 : : #endif
5877 : 0 : p++; /* move to the 1st character of the component */
5878 : : }
5879 : : else
5880 : : {
5881 : : /* djb-rwth: condition for if block had to be rewritten */
5882 [ + - ]: 3 : if ((int)inchi_strtol(pStart, &q, 10) > 0)
5883 : : {
5884 : 3 : val = (int)inchi_strtol(pStart, &q, 10);
5885 : 3 : if_cnd = isdigit(*pStart);
5886 : :
5887 : : }
5888 : : else
5889 : : {
5890 : 0 : val = 1;
5891 : 0 : q = pStart;
5892 : 0 : if_cnd = 1;
5893 : : }
5894 : :
5895 [ + - - + : 3 : if (if_cnd && (t = strchr((char*)mult_type, *q)) && q + 1 == pEnd) /* djb-rwth: if_cnd applied; ignoring LLVM warning: variable used to store function return value */
- - ]
5896 : : {
5897 : : /* process the abbreviation */
5898 : 0 : ret = 0;
5899 : : #if (FIX_DALKE_BUGS == 1)
5900 [ # # ]: 0 : if (iComponent + val > nNumComponents)
5901 : : {
5902 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5903 : 0 : goto exit_function;
5904 : : }
5905 : : #endif
5906 : 0 : bIsoFrom = 0;
5907 [ # # # ]: 0 : switch (bMobileH)
5908 : : {
5909 : 0 : case TAUT_YES:
5910 : 0 : ret = RI_ERR_SYNTAX;
5911 : 0 : break;
5912 : 0 : case TAUT_NON:
5913 [ # # ]: 0 : if (*q == 'm')
5914 : : {
5915 : : /* copy from mobile H to fixed H */
5916 [ # # ]: 0 : pInChIFrom = pInpInChI[ALT_TAUT(bMobileH)];
5917 : : }
5918 : : else
5919 : : {
5920 [ # # ]: 0 : if (*q == 'e')
5921 : : {
5922 : : /* copy from mobile H to isotopic mobile H */
5923 : 0 : pInChIFrom = pInChI;
5924 : 0 : bIsoFrom = -1; /* empty */
5925 : : }
5926 : : else
5927 : : {
5928 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5929 : : }
5930 : : }
5931 : 0 : break;
5932 : 0 : default:
5933 : 0 : ret = RI_ERR_SYNTAX;
5934 : 0 : break;
5935 : : }
5936 [ # # ]: 0 : if (ret < 0)
5937 : : {
5938 : 0 : goto exit_function;
5939 : : }
5940 : : /* copy */
5941 [ # # ]: 0 : for (i = 0; i < val; i++)
5942 : : {
5943 : : #if ( FIX_GAF_2019_2==1 )
5944 : : {
5945 [ # # # # ]: 0 : if ((iComponent + i > nNumComponents) || (iComponent + i < 0))
5946 : : {
5947 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5948 : 0 : goto exit_function;
5949 : : }
5950 : : }
5951 : : #endif
5952 : 0 : ret = CopySegment(pInChI + iComponent + i, pInChIFrom + iComponent + i, nCpyType, 0, bIsoFrom);
5953 [ # # ]: 0 : if (!ret)
5954 : : {
5955 : 0 : ret = RI_ERR_SYNTAX;
5956 : : }
5957 [ # # ]: 0 : if (ret < 0)
5958 : : {
5959 : 0 : goto exit_function;
5960 : : }
5961 : : }
5962 : 0 : iComponent += val;
5963 : : /* continue to the next component(s) */
5964 [ # # ]: 0 : if (*pEnd)
5965 : : {
5966 : 0 : pStart = pEnd + 1;
5967 : 0 : continue;
5968 : : }
5969 : : else
5970 : : {
5971 : 0 : break;
5972 : : }
5973 : : }
5974 : : else
5975 : : {
5976 : 3 : mpy_component = 1;
5977 : 3 : p = pStart;
5978 : : }
5979 : : }
5980 : :
5981 : : #if ( FIX_GAF_2019_2==1 )
5982 [ + - - + ]: 3 : if ((iComponent > nNumComponents - 1) || (iComponent < 0))
5983 : : {
5984 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5985 : 0 : goto exit_function;
5986 : : }
5987 [ + - - + ]: 3 : if (pInChI[iComponent].nNumberOfAtoms <= 0 || pInChI[iComponent].nNumberOfAtoms > MAX_ATOMS)
5988 : : {
5989 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
5990 : 0 : goto exit_function;
5991 : : }
5992 : : #endif
5993 : 3 : pStart = p;
5994 : 3 : pIsotopicAtom = &pInChI[iComponent].IsotopicAtom;
5995 [ - + ]: 3 : if (*pIsotopicAtom)
5996 : : {
5997 : 0 : ret = RI_ERR_PROGR; /* program error */
5998 : 0 : goto exit_function;
5999 : : }
6000 : :
6001 [ + - - + ]: 3 : if (p < pEnd && *pbAbc == -1)
6002 : : {
6003 : : /* check if compressed InChI */
6004 : 0 : *pbAbc = isupper(UCINT * p) ? 1 : 0;
6005 : : }
6006 [ - + ]: 3 : base = (*pbAbc == 1) ? ALPHA_BASE : 10;
6007 : :
6008 : 5 : one_more_time:
6009 [ - + ]: 5 : if (*pbAbc == 1)
6010 : : {
6011 : : /* process the componnt: At[+/-Charge]TDH,... */
6012 : : /* pass 1: find number of stereoatoms */
6013 [ # # ]: 0 : for (p = pStart, iAtom = 0; p < pEnd; iAtom++)
6014 : : {
6015 : 0 : nAtom1 = (AT_NUMB)inchi_strtol(p, &p, base);
6016 : : #if ( CHECK_STRTOL_ATNUMB==1 )
6017 [ # # # # ]: 0 : if (nAtom1 > MAX_ATOMS || nAtom1 < 0)
6018 : : {
6019 : 0 : ret = RI_ERR_SYNTAX;
6020 : 0 : goto exit_function;
6021 : : }
6022 : : #endif
6023 [ # # ]: 0 : if (!nAtom1 ||
6024 [ # # ]: 0 : nAtom1 > pInChI[iComponent].nNumberOfAtoms)
6025 : : {
6026 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6027 : 0 : goto exit_function;
6028 : : }
6029 : 0 : memset(&isoAtom, 0, sizeof(isoAtom)); /* djb-rwth: memset_s C11/Annex K variant? */
6030 : 0 : isoAtom.nAtomNumber = nAtom1;
6031 : 0 : isoAtom.nIsoDifference = (NUM_H)inchi_strtol(p, &q, 10); /* alway in abc */
6032 [ # # ]: 0 : if (p == q)
6033 : : {
6034 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6035 : 0 : goto exit_function;
6036 : : }
6037 : 0 : p = q;
6038 [ # # ]: 0 : if (*p == 't')
6039 : : {
6040 : 0 : isoAtom.nNum_T = 1;
6041 : 0 : p++;
6042 [ # # ]: 0 : if (isdigit(UCINT * p))
6043 : : {
6044 : 0 : isoAtom.nNum_T = (NUM_H)inchi_strtol(p, &q, 10);
6045 : 0 : p = q;
6046 : : }
6047 : : }
6048 [ # # ]: 0 : if (*p == 'd')
6049 : : {
6050 : 0 : isoAtom.nNum_D = 1;
6051 : 0 : p++;
6052 [ # # ]: 0 : if (isdigit(UCINT * p))
6053 : : {
6054 : 0 : isoAtom.nNum_D = (NUM_H)inchi_strtol(p, &q, 10);
6055 : 0 : p = q;
6056 : : }
6057 : : }
6058 [ # # ]: 0 : if (*p == 'h')
6059 : : {
6060 : 0 : isoAtom.nNum_H = 1;
6061 : 0 : p++;
6062 [ # # ]: 0 : if (isdigit(UCINT * p))
6063 : : {
6064 : 0 : isoAtom.nNum_H = (NUM_H)inchi_strtol(p, &q, 10);
6065 : 0 : p = q;
6066 : : }
6067 : : }
6068 [ # # # # : 0 : if (p > pEnd || (!isoAtom.nIsoDifference && !isoAtom.nNum_T && !isoAtom.nNum_D && !isoAtom.nNum_H)) /* djb-rwth: addressing LLVM warning */
# # # # #
# ]
6069 : : {
6070 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6071 : 0 : goto exit_function;
6072 : : }
6073 [ # # ]: 0 : if (*pIsotopicAtom)
6074 : : {
6075 : 0 : pIsotopicAtom[0][iAtom] = isoAtom;
6076 : : }
6077 : : }
6078 : : }
6079 : : else
6080 : : {
6081 : : /* process the componnt: At[+/-Charge]TDH,... */
6082 : : /* pass 1: find number of stereoatoms */
6083 [ + + ]: 9 : for (p = pStart, iAtom = 0; p < pEnd; iAtom++)
6084 : : {
6085 : 5 : nAtom1 = (AT_NUMB)inchi_strtol(p, &q, 10);
6086 : : #if ( CHECK_STRTOL_ATNUMB==1 )
6087 [ + - - + ]: 5 : if (nAtom1 > MAX_ATOMS || nAtom1 < 0)
6088 : : {
6089 : 0 : ret = RI_ERR_SYNTAX;
6090 : 0 : goto exit_function;
6091 : : }
6092 : : #endif
6093 : 5 : p = q;
6094 [ + - ]: 5 : if (!nAtom1 ||
6095 [ + + ]: 5 : nAtom1 > pInChI[iComponent].nNumberOfAtoms ||
6096 [ - + ]: 4 : !(r = strchr((char*)parity_type, *p))) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
6097 : : {
6098 : 1 : ret = RI_ERR_SYNTAX; /* syntax error */
6099 : 1 : goto exit_function;
6100 : : }
6101 : 4 : memset(&isoAtom, 0, sizeof(isoAtom)); /* djb-rwth: memset_s C11/Annex K variant? */
6102 : 4 : isoAtom.nAtomNumber = nAtom1;
6103 [ - + - - ]: 4 : if (p[0] == '+' && isdigit(UCINT p[1]))
6104 : : {
6105 : 0 : isoAtom.nIsoDifference = (NUM_H)inchi_strtol(p + 1, &q, 10);
6106 [ # # ]: 0 : if (isoAtom.nIsoDifference >= 0) isoAtom.nIsoDifference++;
6107 : 0 : p = q;
6108 : : }
6109 : : else
6110 [ + - + - ]: 4 : if (p[0] == '-' && isdigit(UCINT p[1]))
6111 : : {
6112 : 4 : isoAtom.nIsoDifference = -(NUM_H)inchi_strtol(p + 1, &q, 10);
6113 [ - + ]: 4 : if (isoAtom.nIsoDifference == 0) isoAtom.nIsoDifference++;
6114 : 4 : p = q;
6115 : : }
6116 [ - + ]: 4 : if (*p == 'T')
6117 : : {
6118 : 0 : isoAtom.nNum_T = 1;
6119 : 0 : p++;
6120 [ # # ]: 0 : if (isdigit(UCINT * p))
6121 : : {
6122 : 0 : isoAtom.nNum_T = (NUM_H)inchi_strtol(p, &q, 10);
6123 : 0 : p = q;
6124 : : }
6125 : : }
6126 [ - + ]: 4 : if (*p == 'D')
6127 : : {
6128 : 0 : isoAtom.nNum_D = 1;
6129 : 0 : p++;
6130 [ # # ]: 0 : if (isdigit(UCINT * p))
6131 : : {
6132 : 0 : isoAtom.nNum_D = (NUM_H)inchi_strtol(p, &q, 10);
6133 : 0 : p = q;
6134 : : }
6135 : : }
6136 [ - + ]: 4 : if (*p == 'H')
6137 : : {
6138 : 0 : isoAtom.nNum_H = 1;
6139 : 0 : p++;
6140 [ # # ]: 0 : if (isdigit(UCINT * p))
6141 : : {
6142 : 0 : isoAtom.nNum_H = (NUM_H)inchi_strtol(p, &q, 10);
6143 : 0 : p = q;
6144 : : }
6145 : : }
6146 [ - + - - : 4 : if (!isoAtom.nIsoDifference && !isoAtom.nNum_T && !isoAtom.nNum_D && !isoAtom.nNum_H)
- - - - ]
6147 : : {
6148 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6149 : 0 : goto exit_function;
6150 : : }
6151 [ - + ]: 4 : if (p < pEnd)
6152 : : {
6153 [ # # ]: 0 : if (*p == ',')
6154 : : {
6155 : 0 : p++;
6156 : : }
6157 : : else
6158 : : {
6159 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6160 : 0 : goto exit_function;
6161 : : }
6162 : : }
6163 [ + + ]: 4 : if (*pIsotopicAtom)
6164 : : {
6165 : 2 : pIsotopicAtom[0][iAtom] = isoAtom;
6166 : : }
6167 : : }
6168 : : }
6169 [ - + ]: 4 : if (p != pEnd)
6170 : : {
6171 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6172 : 0 : goto exit_function;
6173 : : }
6174 : :
6175 [ + + ]: 4 : if (!*pIsotopicAtom)
6176 : : {
6177 : : /* end of the 1st pass */
6178 : 2 : len = iAtom;
6179 : : /* memory allocation */
6180 [ - + ]: 2 : if (!(*pIsotopicAtom = (INChI_IsotopicAtom*)inchi_calloc((long long)len + 1, sizeof(**pIsotopicAtom)))) /* djb-rwth: cast operator added */
6181 : : {
6182 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6183 : 0 : goto exit_function;
6184 : : }
6185 : 2 : goto one_more_time; /* goto the 2nd pass */
6186 : : }
6187 : : else
6188 : : {
6189 : : /* 2nd pass */
6190 [ - + ]: 2 : if (len != iAtom)
6191 : : {
6192 : 0 : ret = RI_ERR_PROGR; /* program error */
6193 : 0 : goto exit_function;
6194 : : }
6195 : 2 : pInChI[iComponent].nNumberOfIsotopicAtoms = len;
6196 : : }
6197 : :
6198 : : /* multiplier */
6199 [ - + ]: 2 : for (i = 1; i < mpy_component; i++)
6200 : : {
6201 : 0 : ret = CopySegment(pInChI + iComponent + i, pInChI + iComponent, nCpyType, 0, 0);
6202 [ # # ]: 0 : if (!ret)
6203 : : {
6204 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6205 : : }
6206 [ # # ]: 0 : if (ret < 0)
6207 : : {
6208 : 0 : goto exit_function;
6209 : : }
6210 : : }
6211 : :
6212 : 2 : iComponent += mpy_component;
6213 [ - + ]: 2 : if (*pEnd)
6214 : : {
6215 : 0 : pStart = pEnd + 1;
6216 : 0 : continue;
6217 : : }
6218 : : else
6219 : : {
6220 : 2 : break;
6221 : : }
6222 : :
6223 : : }
6224 : :
6225 [ - + ]: 2 : if (nNumComponents != iComponent)
6226 : : {
6227 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6228 : 0 : goto exit_function;
6229 : : }
6230 : :
6231 : 2 : ret = iComponent + 1;
6232 : :
6233 : 3 : exit_function:
6234 : :
6235 : 3 : return ret;
6236 : : }
6237 : :
6238 : :
6239 : : /****************************************************************************
6240 : : Parse "/i/s" InChI layer
6241 : : ****************************************************************************/
6242 : 4 : int ParseSegmentSp3s(const char* str,
6243 : : int bMobileH,
6244 : : INChI* pInpInChI[],
6245 : : int s[TAUT_NUM][2],
6246 : : int ppnNumComponents[],
6247 : : int state)
6248 : : {
6249 : : /* Pass 1: count bonds and find actual numbers of atom */
6250 : : int nNumComponents, iComponent, val;
6251 : : const char* p, * q, * pStart, * pEnd;
6252 : 4 : int ret = 0;
6253 : 4 : INChI* pInChI = pInpInChI[bMobileH];
6254 : 4 : INChI_Stereo** pStereo = NULL;
6255 : :
6256 [ + - - + ]: 4 : int bIso = (state == IST_MOBILE_H_ISO_SP3_S || state == IST_FIXED_H_ISO_SP3_S);
6257 : :
6258 [ + - - + : 4 : if (!bIso && state != IST_MOBILE_H_SP3_S && state != IST_FIXED_H_SP3_S)
- - ]
6259 : : {
6260 : 0 : return RI_ERR_PROGR; /* program error */
6261 : : }
6262 : :
6263 [ + + ]: 4 : if (str[0] != 's')
6264 : : {
6265 : 3 : return 0;
6266 : : }
6267 : :
6268 : 1 : pStart = (char*)str + 1;
6269 : : /* djb-rwth: removing redundant code */
6270 : 1 : nNumComponents = ppnNumComponents[bMobileH];
6271 : :
6272 : : /*if ( !(pEnd = strchr( pStart, ';' )) )*/ /* 2007-09-25 DT */
6273 [ + - ]: 1 : if (!(strchr(pStart, '/'))) /* djb-rwth: removing redundant variables/code */
6274 : : {
6275 : 1 : pEnd = pStart + strlen(pStart);
6276 : : }
6277 : : else
6278 : : {
6279 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6280 : 0 : goto exit_function;
6281 : : }
6282 : 1 : p = pStart;
6283 [ - + ]: 1 : if (pEnd == pStart)
6284 : : {
6285 : : /* create empty sp3 segment */
6286 : 0 : int len = 0;
6287 : 0 : s[bMobileH][bIso] = NO_VALUE_INT; /* empty */
6288 : : /* create empty sp3 segment */
6289 [ # # ]: 0 : for (iComponent = 0; iComponent < nNumComponents; iComponent++)
6290 : : {
6291 [ # # ]: 0 : pStereo = bIso ? &pInChI[iComponent].StereoIsotopic : &pInChI[iComponent].Stereo;
6292 [ # # ]: 0 : if (!*pStereo)
6293 : : {
6294 [ # # ]: 0 : if (!(*pStereo = (INChI_Stereo*)inchi_calloc(1, sizeof(**pStereo))))
6295 : : {
6296 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6297 : 0 : goto exit_function;
6298 : : }
6299 : : }
6300 : 0 : pStereo[0]->nCompInv2Abs = 0; /* deliberately empty */
6301 : :
6302 [ # # ]: 0 : if (pStereo[0]->nNumberOfStereoCenters)
6303 : : {
6304 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: "/s" without a digit describes "no stereo" */
6305 : 0 : goto exit_function;
6306 : : }
6307 : : /* allocate empty sp3 stereo */
6308 [ # # ]: 0 : if ((!pStereo[0]->t_parity &&
6309 [ # # ]: 0 : !(pStereo[0]->t_parity = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->b_parity[0])))) ||
6310 [ # # ]: 0 : (!pStereo[0]->nNumber &&
6311 [ # # ]: 0 : !(pStereo[0]->nNumber = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->nNumber[0]))))) /* djb-rwth: cast operators added; addressing LLVM warnings */
6312 : : {
6313 : : /* cleanup */
6314 [ # # ]: 0 : if (pStereo[0]->t_parity)
6315 : : {
6316 : : INCHI_HEAPCHK
6317 [ # # ]: 0 : inchi_free(pStereo[0]->t_parity);
6318 : 0 : pStereo[0]->t_parity = NULL;
6319 : : }
6320 [ # # ]: 0 : if (pStereo[0]->nNumber)
6321 : : {
6322 : : INCHI_HEAPCHK
6323 [ # # ]: 0 : inchi_free(pStereo[0]->nNumber);
6324 : 0 : pStereo[0]->nNumber = NULL;
6325 : : }
6326 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6327 : 0 : goto exit_function;
6328 : : }
6329 : : }
6330 : 0 : ret = nNumComponents + 1;
6331 : : }
6332 : : else
6333 : : {
6334 : 1 : val = (int)inchi_strtol(p, &q, 10);
6335 [ + - + - : 1 : if (q == pEnd && 1 <= val && val <= 3)
+ - ]
6336 : : {
6337 : 1 : s[bMobileH][bIso] = val;
6338 : 1 : ret = nNumComponents + 1;
6339 : : }
6340 : : else
6341 : : {
6342 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6343 : : }
6344 : : }
6345 : :
6346 : 1 : exit_function:
6347 : :
6348 : 1 : return ret;
6349 : : }
6350 : :
6351 : :
6352 : : /****************************************************************************/
6353 : 0 : int bIsSp3LayerNotEmpty(INChI* pInpInChI[],
6354 : : int bMobileH,
6355 : : int bIso,
6356 : : int nNumComponents)
6357 : : {
6358 : : INChI* pInChI;
6359 : : INChI_Stereo* pStereo;
6360 : 0 : int iComponent, num_not_empty = 0;
6361 : :
6362 [ # # ]: 0 : if (pInpInChI[bMobileH])
6363 : : {
6364 [ # # ]: 0 : for (iComponent = 0; iComponent < nNumComponents; iComponent++)
6365 : : {
6366 : 0 : pInChI = pInpInChI[bMobileH] + iComponent;
6367 [ # # # # ]: 0 : if (pInChI->bDeleted || !pInChI->nNumberOfAtoms)
6368 : : {
6369 : 0 : continue;
6370 : : }
6371 [ # # ]: 0 : pStereo = bIso ? pInChI->StereoIsotopic : pInChI->Stereo;
6372 [ # # # # : 0 : if (pStereo && pStereo->nNumberOfStereoCenters > 0 && pStereo->nNumber && pStereo->t_parity)
# # # # ]
6373 : : {
6374 : 0 : num_not_empty++;
6375 : : }
6376 : : }
6377 : : }
6378 : 0 : return num_not_empty;
6379 : : }
6380 : :
6381 : :
6382 : : /****************************************************************************
6383 : : Parse "/i/m" InChI layer
6384 : : ****************************************************************************/
6385 : 4 : int ParseSegmentSp3m(const char* str,
6386 : : int bMobileH,
6387 : : INChI* pInpInChI[],
6388 : : int ppnNumComponents[],
6389 : : int state)
6390 : : {
6391 : : /* Pass 1: count bonds and find actual numbers of atom */
6392 : : int nNumComponents, iComponent;
6393 : : const char* p, * pStart, * pEnd;
6394 : 4 : int ret = 0;
6395 : 4 : INChI* pInChI = pInpInChI[bMobileH];
6396 : 4 : INChI_Stereo** pStereo = NULL;
6397 : :
6398 [ + - - + ]: 4 : int bIso = (state == IST_MOBILE_H_ISO_SP3_M || state == IST_FIXED_H_ISO_SP3_M);
6399 : :
6400 [ + - - + : 4 : if (!bIso && state != IST_MOBILE_H_SP3_M && state != IST_FIXED_H_SP3_M)
- - ]
6401 : : {
6402 : 0 : return RI_ERR_PROGR; /* program error */
6403 : : }
6404 : 4 : nNumComponents = ppnNumComponents[bMobileH];
6405 : :
6406 [ + + ]: 4 : if (str[0] != 'm')
6407 : : {
6408 : : /* /m is missing: check whether we have to inherit /m from a preceding stereo layer */
6409 : : INChI_Stereo* pStereoFrom, * pStereoTo;
6410 : : INChI* pInChIFrom;
6411 : 3 : int bMobileHFrom = -1, bIsoFrom = -1; /* djb-rwth: removing redundant variables */
6412 [ + - + - ]: 3 : if (bMobileH && !bIso)
6413 : : {
6414 : 3 : return 0; /* Main non-isotopic cannot inherit: it has no preceding layer */
6415 : : }
6416 : : else
6417 : : {
6418 [ # # # # ]: 0 : if (!bMobileH && !bIso)
6419 : : {
6420 : : /* fixed-H non-isotopic (F) inherits from Mobile-H non-isotopic (M) */
6421 : 0 : bMobileHFrom = TAUT_YES;
6422 : 0 : bIsoFrom = 0;
6423 : : }
6424 : : else
6425 : : {
6426 [ # # # # ]: 0 : if (bMobileH && bIso)
6427 : : {
6428 : : /* Mobile-H isotopic (MI) inherits from Mobile-H non-isotopic (M) */
6429 : 0 : bMobileHFrom = TAUT_YES;
6430 : 0 : bIsoFrom = 0;
6431 : : }
6432 : : else
6433 : : {
6434 [ # # # # ]: 0 : if (!bMobileH && bIso)
6435 : : {
6436 : : /* Fixed-H isotopic (FI) inherits from Fixed-H non-isotopic (F) */
6437 : 0 : bMobileHFrom = TAUT_NON;
6438 : 0 : bIsoFrom = 0;
6439 : : /* if Sp3 is empty in F as well as in M, then inherit from MI */
6440 [ # # # # ]: 0 : if (!bIsSp3LayerNotEmpty(pInpInChI, TAUT_NON, 0, ppnNumComponents[TAUT_NON /*bMobileH*/]) /* F */ &&
6441 : 0 : !bIsSp3LayerNotEmpty(pInpInChI, TAUT_YES, 0, ppnNumComponents[TAUT_YES /*bMobileH*/]) /* M */)
6442 : : {
6443 : 0 : bMobileHFrom = TAUT_YES;
6444 : 0 : bIsoFrom = 1;
6445 : : }
6446 : : }
6447 : : }
6448 : : }
6449 : : }
6450 [ # # # # ]: 0 : if (bMobileHFrom < 0 || bIsoFrom < 0) /* djb-rwth: addressing coverity ID #499556 -- check necessary due to initialisation values */
6451 : : {
6452 : 0 : return RI_ERR_PROGR;
6453 : : }
6454 [ # # ]: 0 : if (!bIsSp3LayerNotEmpty(pInpInChI, bMobileHFrom, bIsoFrom, ppnNumComponents[/*bMobileH*/ bMobileHFrom]))
6455 : : {
6456 : : /* nothing to copy; check whether it should have inherited from a preceding layer */
6457 [ # # # # : 0 : if ((!bMobileHFrom && bIsoFrom) || (bMobileHFrom && !bIsoFrom)) /* djb-rwth: addressing LLVM warnings */
# # # # ]
6458 : : {
6459 : : /* MI or F inherit stereo from M */
6460 : 0 : bMobileHFrom = TAUT_YES;
6461 : 0 : bIsoFrom = 0;
6462 [ # # ]: 0 : if (!bIsSp3LayerNotEmpty(pInpInChI, bMobileHFrom, bIsoFrom, ppnNumComponents[bMobileHFrom /*bMobileH*/]))
6463 : : {
6464 : 0 : return 0;
6465 : : }
6466 : : }
6467 : : else
6468 : : {
6469 : 0 : return 0;
6470 : : }
6471 : : }
6472 : 0 : nNumComponents = inchi_min(ppnNumComponents[bMobileH], ppnNumComponents[bMobileHFrom]);
6473 [ # # ]: 0 : for (iComponent = 0; iComponent < nNumComponents; iComponent++)
6474 : : {
6475 : 0 : pInChIFrom = pInpInChI[bMobileHFrom] + iComponent;
6476 : 0 : pInChI = pInpInChI[bMobileH] + iComponent;
6477 [ # # # # ]: 0 : if (pInChIFrom->nNumberOfAtoms > 0 && !pInChIFrom->bDeleted &&
6478 [ # # # # ]: 0 : pInChI->nNumberOfAtoms > 0 && !pInChI->bDeleted)
6479 : : {
6480 [ # # ]: 0 : pStereoFrom = bIsoFrom ? pInChIFrom->StereoIsotopic : pInChIFrom->Stereo;
6481 [ # # ]: 0 : pStereoTo = bIso ? pInChI->StereoIsotopic : pInChI->Stereo;
6482 [ # # # # ]: 0 : if (pStereoFrom && pStereoTo)
6483 : : {
6484 : 0 : pStereoTo->nCompInv2Abs = pStereoFrom->nCompInv2Abs;
6485 : : /* djb-rwth: removing redundant code */
6486 : : }
6487 : : }
6488 : : }
6489 : 0 : return 0; /* return value > 0 means the non-/m segment has been processed here */
6490 : : }
6491 : :
6492 : 1 : pStart = str + 1;
6493 : 1 : iComponent = 0;
6494 : :
6495 : : /*if ( !(pEnd = strchr( pStart, ';' )) )*/ /* 2007-09-25 DT */
6496 [ + - ]: 1 : if (!(strchr(pStart, '/'))) /* djb-rwth: removing redundant code */
6497 : : {
6498 : 1 : pEnd = pStart + strlen(pStart);
6499 : : }
6500 : : else
6501 : : {
6502 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6503 : 0 : goto exit_function;
6504 : : }
6505 : 1 : p = pStart;
6506 [ - + ]: 1 : if (pEnd == pStart)
6507 : : {
6508 : : /* create empty sp3 segment */
6509 : 0 : int len = 0;
6510 [ # # ]: 0 : for (iComponent = 0; iComponent < nNumComponents; iComponent++)
6511 : : {
6512 : 0 : INChI* pIsoInChI = &pInChI[iComponent];
6513 [ # # ]: 0 : pStereo = bIso ? &pIsoInChI->StereoIsotopic : &pIsoInChI->Stereo;
6514 [ # # ]: 0 : if (!*pStereo)
6515 : : {
6516 [ # # ]: 0 : if (!(*pStereo = (INChI_Stereo*)inchi_calloc(1, sizeof(**pStereo))))
6517 : : {
6518 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6519 : 0 : goto exit_function;
6520 : : }
6521 : : }
6522 : 0 : pStereo[0]->nCompInv2Abs = NO_VALUE_INT; /* deliberately empty */
6523 : : #ifdef NEVER
6524 : : if (pStereo[0]->nNumberOfStereoCenters)
6525 : : {
6526 : : ret = RI_ERR_SYNTAX; /* syntax error */
6527 : : goto exit_function;
6528 : : }
6529 : : #endif
6530 : : /* allocate empty sp3 stereo */
6531 [ # # ]: 0 : if ((!pStereo[0]->t_parity &&
6532 [ # # ]: 0 : !(pStereo[0]->t_parity = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->b_parity[0])))) ||
6533 [ # # ]: 0 : (!pStereo[0]->nNumber &&
6534 [ # # ]: 0 : !(pStereo[0]->nNumber = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->nNumber[0]))))) /* djb-rwth: cast operators added; addressing LLVM warnings */
6535 : : {
6536 : : /* cleanup */
6537 [ # # ]: 0 : if (pStereo[0]->t_parity)
6538 : : {
6539 : : INCHI_HEAPCHK
6540 [ # # ]: 0 : inchi_free(pStereo[0]->t_parity);
6541 : 0 : pStereo[0]->t_parity = NULL;
6542 : : }
6543 [ # # ]: 0 : if (pStereo[0]->nNumber)
6544 : : {
6545 : : INCHI_HEAPCHK
6546 [ # # ]: 0 : inchi_free(pStereo[0]->nNumber);
6547 : 0 : pStereo[0]->nNumber = NULL;
6548 : : }
6549 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6550 : 0 : goto exit_function;
6551 : : }
6552 : : }
6553 : 0 : ret = nNumComponents + 1;
6554 : : }
6555 : : else
6556 : : {
6557 [ + + + - ]: 3 : while (p < pEnd && iComponent < nNumComponents)
6558 : : {
6559 : : /* cycle over components */
6560 [ - + ]: 2 : pStereo = bIso ? &pInChI[iComponent].StereoIsotopic : &pInChI[iComponent].Stereo;
6561 [ + - - + ]: 2 : if (*p != '.' && !*pStereo)
6562 : : {
6563 [ # # ]: 0 : if (!(*pStereo = (INChI_Stereo*)inchi_calloc(1, sizeof(**pStereo))))
6564 : : {
6565 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6566 : 0 : goto exit_function;
6567 : : }
6568 : : }
6569 [ - + - - ]: 2 : switch (*p)
6570 : : {
6571 : 0 : case '1':
6572 : 0 : pStereo[0]->nCompInv2Abs = -1;
6573 : 0 : break;
6574 : 2 : case '0':
6575 : 2 : pStereo[0]->nCompInv2Abs = 1;
6576 : 2 : break;
6577 : 0 : case '.':
6578 [ # # ]: 0 : if (*pStereo)
6579 : : {
6580 : 0 : pStereo[0]->nCompInv2Abs = 0;
6581 : : }
6582 : 0 : break;
6583 : 0 : default:
6584 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6585 : 0 : goto exit_function;
6586 : : }
6587 : 2 : iComponent++;
6588 : 2 : p++;
6589 : : }
6590 [ + - - + ]: 1 : if (p != pEnd || iComponent != nNumComponents)
6591 : : {
6592 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6593 : 0 : goto exit_function;
6594 : : }
6595 : 1 : ret = nNumComponents + 1;
6596 : : }
6597 : :
6598 : 1 : exit_function:
6599 : :
6600 : 1 : return ret;
6601 : : }
6602 : :
6603 : :
6604 : : /****************************************************************************
6605 : : Parse "/t" InChI layer
6606 : : ****************************************************************************/
6607 : 6 : int ParseSegmentSp3(const char* str, /* input; string of segment starting with "/t" */
6608 : : int bMobileH, /* input; bMobileH indicates what we have, fixed or tauto */
6609 : : INChI* pInpInChI[], /* output; to be allocated and filled */
6610 : : int ppnNumComponents[], /* input; ppnNumComponents[bMobileH] is number of components*/
6611 : : int state, /* input; gen parser state code */
6612 : : int* pbAbc) /* input; inicator of compresssed InChI or not */
6613 : : {
6614 : : /* Pass 1: count bonds and find actual numbers of atom */
6615 : 6 : const char mult_type[] = "mnMNe";
6616 : 6 : const char parity_type[] = "-+u?";
6617 : : const char* p, * q, * t, * pStart, * pEnd, * r;
6618 : : AT_NUMB nAtom1;
6619 : 6 : int mpy_component = 0, val;
6620 : : int nNumComponents, iComponent, len, iAtom;
6621 : : int atomParity;
6622 : 6 : int ret = 0, retf = 0;
6623 : 6 : int base = 10;
6624 : 6 : int nCpyType = CPY_SP3;
6625 [ + - - + ]: 6 : int bIso = (state == IST_MOBILE_H_ISO_SP3 || state == IST_FIXED_H_ISO_SP3);
6626 : 6 : INChI* pInChI = pInpInChI[bMobileH];
6627 : 6 : INChI_Stereo** pStereo = NULL;
6628 : 6 : int if_cnd = 1; /* djb-rwth: needed for some if condition restructuring */
6629 : :
6630 [ + - - + : 6 : if (!bIso && state != IST_MOBILE_H_SP3 && state != IST_FIXED_H_SP3)
- - ]
6631 : : {
6632 : 0 : return RI_ERR_PROGR;
6633 : : }
6634 [ + + ]: 6 : if (str[0] != 't')
6635 : : {
6636 : 3 : return 0; /* RI_ERR_EOF - ? */
6637 : : }
6638 : :
6639 : 3 : pStart = (char*)str + 1;
6640 : 3 : iComponent = 0;
6641 : 3 : nNumComponents = ppnNumComponents[bMobileH];
6642 : :
6643 : : /* Pass 1: create empty segment and exit */
6644 [ - + ]: 3 : if (!*pStart)
6645 : : {
6646 : 0 : ret = SegmentSp3CreateEmpty(str, bMobileH, pInpInChI, nNumComponents, state, pbAbc);
6647 : 0 : goto exit_function;
6648 : : }
6649 : :
6650 : : /* Cycle over components */
6651 : : while (1)
6652 : : {
6653 [ + + ]: 5 : if (!(pEnd = strchr(pStart, ';')))
6654 : : {
6655 : 3 : pEnd = pStart + strlen(pStart);
6656 : : }
6657 : :
6658 : : /* djb-rwth: condition for if block had to be rewritten */
6659 [ + + ]: 5 : if ((int)inchi_strtol(pStart, &q, 10) > 0)
6660 : : {
6661 : 4 : val = (int)inchi_strtol(pStart, &q, 10);
6662 : 4 : if_cnd = isdigit(*pStart);
6663 : :
6664 : : }
6665 : : else
6666 : : {
6667 : 1 : val = 1;
6668 : 1 : q = pStart;
6669 : 1 : if_cnd = 1;
6670 : : }
6671 : :
6672 : : /* Abbreviation? */
6673 [ + - - + : 5 : if (if_cnd && (t = strchr((char*)mult_type, *q)) && q + 1 == pEnd) /* djb-rwth: if_cnd applied; ignoring LLVM warning: variable used */
- - ]
6674 : : {
6675 : : /* Process abbrebiation */
6676 : 0 : retf = SegmentSp3ProcessAbbreviation(&mpy_component, iComponent, nNumComponents,
6677 : : val, q, state, pbAbc, bMobileH, nCpyType,
6678 [ # # ]: 0 : pInChI, pInpInChI[ALT_TAUT(bMobileH)]);
6679 [ # # ]: 0 : if (retf == RI_ERR_SYNTAX)
6680 : : {
6681 : 0 : ret = RI_ERR_SYNTAX;
6682 : 0 : goto exit_function;
6683 : : }
6684 : 0 : goto end_main_cycle;
6685 : : }
6686 : : /* Multiplier? */
6687 [ + - + + ]: 5 : else if ((p = strchr(pStart, '*')) && p < pEnd)
6688 : : {
6689 : : /* Process regular multiplier */
6690 : 3 : mpy_component = (int)inchi_strtol(pStart, &q, 10);
6691 [ - + ]: 3 : if (p != q)
6692 : : {
6693 : 0 : ret = RI_ERR_SYNTAX;
6694 : 0 : goto exit_function;
6695 : : }
6696 : 3 : p++; /* move to the 1st character of the component */
6697 : : }
6698 : : else
6699 : : {
6700 : : /* Just normal sequence of centers/configs, prepare to read */
6701 : 2 : mpy_component = 1;
6702 : 2 : p = pStart;
6703 : : }
6704 : : #if (FIX_DALKE_BUGS == 1)
6705 [ + + + + ]: 5 : if (mpy_component < 1 || iComponent + mpy_component > nNumComponents)
6706 : : {
6707 : 2 : ret = RI_ERR_SYNTAX;
6708 : 2 : goto exit_function;
6709 : : }
6710 : : #endif
6711 : :
6712 : 3 : pStart = p;
6713 [ + - - + ]: 3 : if (p < pEnd && *pbAbc == -1)
6714 : : {
6715 : : /* check if compressed InChI */
6716 : 0 : *pbAbc = isupper(UCINT * p) ? 1 : 0;
6717 : : }
6718 [ - + ]: 3 : base = (*pbAbc == 1) ? ALPHA_BASE : 10;
6719 : :
6720 : : /* Process the component: at1p,at1p,... */
6721 : :
6722 : : /* Pass 1: find number of stereoatoms len */
6723 [ - + ]: 3 : if (*pbAbc == 1)
6724 : : {
6725 [ # # ]: 0 : for (p = pStart, iAtom = 0; p < pEnd; iAtom++)
6726 : : {
6727 [ # # # # ]: 0 : if ((nAtom1 = (AT_NUMB)inchi_strtol(p, &p, base)) &&
6728 : 0 : (atomParity = (int)inchi_strtol(p, &p, 10),
6729 [ # # # # ]: 0 : AB_MIN_KNOWN_PARITY <= atomParity && atomParity <= AB_MAX_KNOWN_PARITY))
6730 : : {
6731 : : ; /* okay */
6732 : : }
6733 : : else
6734 : : {
6735 : 0 : ret = RI_ERR_SYNTAX;
6736 : 0 : goto exit_function;
6737 : : }
6738 [ # # ]: 0 : if (nAtom1 > pInChI[iComponent].nNumberOfAtoms)
6739 : : {
6740 : 0 : ret = RI_ERR_SYNTAX;
6741 : 0 : goto exit_function;
6742 : : }
6743 : : }
6744 : : }
6745 : : else
6746 : : {
6747 [ + + ]: 6 : for (p = pStart, iAtom = 0; p < pEnd; iAtom++, p += (*p == ','))
6748 : : {
6749 : 3 : nAtom1 = (AT_NUMB)inchi_strtol(p, &q, 10);
6750 : 3 : p = q + 1;
6751 [ + - ]: 3 : if (!nAtom1 ||
6752 [ + - ]: 3 : nAtom1 > pInChI[iComponent].nNumberOfAtoms ||
6753 [ - + ]: 3 : !(r = strchr((char*)parity_type, *q))) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
6754 : : {
6755 : 0 : ret = RI_ERR_SYNTAX;
6756 : 0 : goto exit_function;
6757 : : }
6758 : : }
6759 : : }
6760 [ - + ]: 3 : if (p != pEnd)
6761 : : {
6762 : 0 : ret = RI_ERR_SYNTAX;
6763 : 0 : goto exit_function;
6764 : : }
6765 : 3 : len = iAtom;
6766 : : /* Found len, the number of stereo centers in /t segment for component iComponent */
6767 : :
6768 : : #if ( ( FIX_GAF_2019_1==1 ) || ( FIX_GAF_2019_2==1 ) )
6769 [ + - - + ]: 3 : if ((iComponent > nNumComponents - 1) || (iComponent < 0))
6770 : : {
6771 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6772 : 0 : goto exit_function;
6773 : : }
6774 [ + - - + ]: 3 : if (pInChI[iComponent].nNumberOfAtoms <= 0 || pInChI[iComponent].nNumberOfAtoms > MAX_ATOMS)
6775 : : {
6776 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6777 : 0 : goto exit_function;
6778 : : }
6779 : : #endif
6780 : :
6781 : : /* Allocate memory for pStereo */
6782 [ - + ]: 3 : pStereo = bIso ? &pInChI[iComponent].StereoIsotopic : &pInChI[iComponent].Stereo;
6783 [ + - ]: 3 : if (!*pStereo)
6784 : : {
6785 [ - + ]: 3 : if (!(*pStereo = (INChI_Stereo*)inchi_calloc(1, sizeof(**pStereo))))
6786 : : {
6787 : 0 : ret = RI_ERR_ALLOC;
6788 : 0 : goto exit_function;
6789 : : }
6790 : : }
6791 [ + - + - ]: 3 : if (pStereo[0]->t_parity || pStereo[0]->nNumberOfStereoCenters ||
6792 [ - + ]: 3 : pStereo[0]->nNumber)
6793 : : {
6794 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6795 : 0 : goto exit_function;
6796 : : }
6797 : : /* Allocate sp3 stereo */
6798 [ + - ]: 3 : if (!(pStereo[0]->t_parity = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->b_parity[0]))) ||
6799 [ - + ]: 3 : !(pStereo[0]->nNumber = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->nNumber[0])))) /* djb-rwth: cast operators added */
6800 : : {
6801 : : /* cleanup */
6802 [ # # ]: 0 : if (pStereo[0]->t_parity)
6803 : : {
6804 : : INCHI_HEAPCHK
6805 [ # # ]: 0 : inchi_free(pStereo[0]->t_parity);
6806 : 0 : pStereo[0]->t_parity = NULL;
6807 : : }
6808 [ # # ]: 0 : if (pStereo[0]->nNumber)
6809 : : {
6810 : : INCHI_HEAPCHK
6811 [ # # ]: 0 : inchi_free(pStereo[0]->nNumber);
6812 : 0 : pStereo[0]->nNumber = NULL;
6813 : : }
6814 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
6815 : 0 : goto exit_function;
6816 : : }
6817 : :
6818 : :
6819 : : /* Store stereocenters (pass 2) */
6820 : 3 : retf = SegmentSp3StoreStereoCenters(pbAbc, pStart, pEnd, pInChI[iComponent].nNumberOfAtoms, pStereo[0]);
6821 [ - + ]: 3 : if (retf == RI_ERR_SYNTAX)
6822 : : {
6823 : 0 : ret = RI_ERR_SYNTAX;
6824 : 0 : goto exit_function;
6825 : : }
6826 : :
6827 : : /* Treat multiplier-covered components */
6828 : 3 : retf = SegmentSp3CopyMultiplierCovered(mpy_component, iComponent, pInChI, bIso, nCpyType);
6829 [ - + ]: 3 : if (retf == RI_ERR_SYNTAX)
6830 : : {
6831 : 0 : ret = RI_ERR_SYNTAX;
6832 : 0 : goto exit_function;
6833 : : }
6834 : :
6835 : 3 : end_main_cycle:
6836 : 3 : iComponent += mpy_component;
6837 [ + + ]: 3 : if (*pEnd)
6838 : : {
6839 : 2 : pStart = pEnd + 1;
6840 : 2 : continue;
6841 : : }
6842 : : else
6843 : : {
6844 : 1 : break;
6845 : : }
6846 : :
6847 : : } /* Cycle over components */
6848 : :
6849 [ - + ]: 1 : if (nNumComponents != iComponent)
6850 : : {
6851 : : /* Not all components treated, that's an error */
6852 : 0 : ret = RI_ERR_SYNTAX;
6853 : 0 : goto exit_function;
6854 : : }
6855 : : /* At this moment, INChI_Stereo pInChI[k].Stereo provides stereocenter parity info for k-th component
6856 : : pInChI[k].Stereo.
6857 : : nNumber[nNumberOfStereoCenters] cano numbers
6858 : : t_parity[nNumberOfStereoCenters] tetrahedral atom parities
6859 : : */
6860 : :
6861 : :
6862 : 1 : ret = iComponent + 1;
6863 : :
6864 : 3 : exit_function:
6865 : :
6866 : 3 : return ret;
6867 : : }
6868 : :
6869 : :
6870 : : /****************************************************************************
6871 : : Parse "/b" InChI layer
6872 : : ****************************************************************************/
6873 : 10 : int ParseSegmentSp2(const char* str,
6874 : : int bMobileH,
6875 : : INChI* pInpInChI[],
6876 : : int ppnNumComponents[],
6877 : : int state,
6878 : : int* pbAbc)
6879 : : {
6880 : : /* Pass 1: count bonds and find actual numbers of atom */
6881 : : int i, mpy_component, val, len_limit;
6882 : : int nNumComponents, iComponent, len, iBond;
6883 : : AT_NUMB nAtom1, nAtom2;
6884 : : int bondParity;
6885 : : const char* p, * q, * t, * pStart, * pEnd, * r;
6886 : 10 : int ret = 0;
6887 : 10 : INChI* pInChI = pInpInChI[bMobileH];
6888 : 10 : INChI* pInChIFrom = NULL;
6889 : : /*
6890 : : INChI_Stereo *Stereo = NULL;
6891 : : INChI_Stereo *StereoOther = NULL;
6892 : : */
6893 : 10 : INChI_Stereo** pStereo = NULL;
6894 : :
6895 : 10 : const char mult_type[] = "mnMNe";
6896 : 10 : const char parity_type[] = "-+u?";
6897 : 10 : int bIsoTo, bIsoFrom, nCpyType = CPY_SP2;
6898 [ + - - + ]: 10 : int bIso = (state == IST_MOBILE_H_ISO_SP2 || state == IST_FIXED_H_ISO_SP2);
6899 : 10 : int base = 10;
6900 : 10 : int if_cnd = 1; /* djb-rwth: needed for some if condition restructuring */
6901 : :
6902 [ + - - + : 10 : if (!bIso && state != IST_MOBILE_H_SP2 && state != IST_FIXED_H_SP2)
- - ]
6903 : : {
6904 : 0 : return RI_ERR_PROGR; /* program error */
6905 : : }
6906 : :
6907 [ + + ]: 10 : if (str[0] != 'b')
6908 : : {
6909 : 6 : return 0;
6910 : : }
6911 : :
6912 : 4 : pStart = (char*)str + 1;
6913 : 4 : iComponent = 0;
6914 : 4 : nNumComponents = ppnNumComponents[bMobileH];
6915 : :
6916 [ - + ]: 4 : if (!*pStart)
6917 : : {
6918 : : /* create empty sp2 segment which means no sp2 */
6919 [ # # ]: 0 : for (iComponent = 0; iComponent < nNumComponents; iComponent++)
6920 : : {
6921 : 0 : INChI* pIsoInChI = &pInChI[iComponent];
6922 [ # # ]: 0 : pStereo = bIso ? &pIsoInChI->StereoIsotopic : &pIsoInChI->Stereo;
6923 [ # # # # : 0 : if (*pStereo && (pStereo[0]->b_parity || pStereo[0]->nNumberOfStereoBonds ||
# # ]
6924 [ # # # # ]: 0 : pStereo[0]->nBondAtom1 || pStereo[0]->nBondAtom2))
6925 : : {
6926 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6927 : 0 : goto exit_function;
6928 : : }
6929 : : /* allocate empty sp2 stereo */
6930 : 0 : ret = CopySegment(pIsoInChI, NULL, CPY_SP2, bIso, -1);
6931 [ # # ]: 0 : if (ret < 0)
6932 : : {
6933 : 0 : goto exit_function;
6934 : : }
6935 : : }
6936 : 0 : ret = nNumComponents + 1;
6937 : 0 : goto exit_function;
6938 : : }
6939 : :
6940 : : while (1)
6941 : : {
6942 : : /* Cycle over components */
6943 [ + + ]: 7 : if (!(pEnd = strchr(pStart, ';')))
6944 : : {
6945 : 4 : pEnd = pStart + strlen(pStart);
6946 : : }
6947 : :
6948 : : /* djb-rwth: condition for if block had to be rewritten -- GH issue #09, thanks to Istvan Ori */
6949 [ + + ]: 7 : if ((int)inchi_strtol(pStart, &q, 10) > 0)
6950 : : {
6951 : 5 : val = (int)inchi_strtol(pStart, &q, 10);
6952 : 5 : if_cnd = isdigit(*pStart);
6953 : :
6954 : : }
6955 : : else
6956 : : {
6957 : 2 : val = 1;
6958 : 2 : q = pStart;
6959 : 2 : if_cnd = 1;
6960 : : }
6961 : :
6962 : :
6963 [ + - - + : 7 : if (if_cnd && (t = strchr((char*)mult_type, *q)) && q + 1 == pEnd) /* djb-rwth: if_cnd applied; ignoring LLVM warning: variable used to store function return value */
- - ]
6964 : : {
6965 : : /* process the abbreviation */
6966 : 0 : ret = 0;
6967 : : #if (FIX_DALKE_BUGS == 1)
6968 : : /* djb-rwth: fixing GH issue #59.2 */
6969 : : if ((iComponent + val >= INT_MIN) && (iComponent + val <= INT_MAX))
6970 : : {
6971 [ # # ]: 0 : if ((iComponent + val > nNumComponents))
6972 : : {
6973 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
6974 : 0 : goto exit_function;
6975 : : }
6976 : : }
6977 : : else
6978 : : {
6979 : : ret = BNS_PROGRAM_ERR;
6980 : : goto exit_function;
6981 : : }
6982 : : #endif
6983 [ # # # ]: 0 : switch (bMobileH)
6984 : : {
6985 : 0 : case TAUT_YES:
6986 [ # # ]: 0 : switch (state)
6987 : : {
6988 : 0 : case IST_MOBILE_H_ISO_SP2:
6989 [ # # ]: 0 : if (*q == 'm')
6990 : : {
6991 : : /* copy from mobile H to isotopic mobile H */
6992 : 0 : pInChIFrom = pInChI;
6993 : 0 : bIsoTo = 1;
6994 : 0 : bIsoFrom = 0;
6995 : : }
6996 : : else
6997 : : {
6998 [ # # ]: 0 : if (*q == 'e')
6999 : : {
7000 : : /* copy from mobile H to isotopic mobile H */
7001 : 0 : pInChIFrom = pInChI;
7002 : 0 : bIsoTo = 1;
7003 : 0 : bIsoFrom = -1; /* empty */
7004 : : }
7005 : : else
7006 : : {
7007 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7008 : : }
7009 : : }
7010 : 0 : break;
7011 : 0 : default:
7012 : 0 : ret = RI_ERR_SYNTAX;
7013 : 0 : break;
7014 : : }
7015 : 0 : break;
7016 : 0 : case TAUT_NON:
7017 [ # # # ]: 0 : switch (state)
7018 : : {
7019 : 0 : case IST_FIXED_H_SP2:
7020 [ # # ]: 0 : if (*q == 'm')
7021 : : {
7022 : : /* copy from mobile H to fixed H */
7023 : : #if ( FIX_GAF_2019_2==1 )
7024 [ # # ]: 0 : int inum = ALT_TAUT(bMobileH);
7025 : 0 : pInChIFrom = pInpInChI[inum];
7026 : : #else
7027 : : pInChIFrom = pInpInChI[ALT_TAUT(bMobileH)];
7028 : : #endif
7029 : 0 : bIsoTo = 0;
7030 : 0 : bIsoFrom = 0;
7031 : : }
7032 : : else
7033 : : {
7034 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7035 : : }
7036 : 0 : break;
7037 : 0 : case IST_FIXED_H_ISO_SP2:
7038 [ # # ]: 0 : if (*q == 'm')
7039 : : {
7040 : : /* copy from mobile H to fixed isotopic H */
7041 [ # # ]: 0 : pInChIFrom = pInpInChI[ALT_TAUT(bMobileH)];
7042 : 0 : bIsoTo = 1;
7043 : 0 : bIsoFrom = 0;
7044 : : }
7045 : : else
7046 : : {
7047 [ # # ]: 0 : if (*q == 'M')
7048 : : {
7049 : : /* copy from isotopic mobile H to fixed isotopic H */
7050 [ # # ]: 0 : pInChIFrom = pInpInChI[ALT_TAUT(bMobileH)];
7051 : 0 : bIsoTo = 1;
7052 : 0 : bIsoFrom = 1;
7053 : : }
7054 : : else
7055 : : {
7056 [ # # ]: 0 : if (*q == 'n')
7057 : : {
7058 : : /* copy from fixed H to fixed isotopic H */
7059 : 0 : pInChIFrom = pInChI;
7060 : 0 : bIsoTo = 1;
7061 : 0 : bIsoFrom = 0;
7062 : : }
7063 : : else
7064 : : {
7065 [ # # ]: 0 : if (*q == 'e')
7066 : : {
7067 : : /* copy from mobile H to isotopic mobile H */
7068 : 0 : pInChIFrom = pInChI;
7069 : 0 : bIsoTo = 1;
7070 : 0 : bIsoFrom = -1; /* empty */
7071 : : }
7072 : : else
7073 : : {
7074 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7075 : : }
7076 : : }
7077 : : }
7078 : : }
7079 : 0 : break;
7080 : 0 : default:
7081 : 0 : ret = RI_ERR_SYNTAX;
7082 : 0 : break;
7083 : : }
7084 : 0 : break;
7085 : :
7086 : 0 : default:
7087 : 0 : ret = RI_ERR_SYNTAX;
7088 : 0 : break;
7089 : : }
7090 [ # # ]: 0 : if (ret < 0)
7091 : : {
7092 : 0 : goto exit_function;
7093 : : }
7094 : : /* copy */
7095 [ # # ]: 0 : for (i = 0; i < val; i++)
7096 : : {
7097 : : #if ( FIX_GAF_2019_2==1 )
7098 [ # # # # ]: 0 : if ((iComponent + i > nNumComponents - 1) || (iComponent + i < 0))
7099 : : {
7100 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7101 : 0 : goto exit_function;
7102 : : }
7103 [ # # # # ]: 0 : if (NULL == pInChIFrom || NULL == pInChIFrom + iComponent + i) /* djb-rwth: ignoring GCC warning */
7104 : : {
7105 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7106 : 0 : goto exit_function;
7107 : : }
7108 : :
7109 [ # # # # ]: 0 : if ((pInChIFrom[iComponent + i].nNumberOfAtoms <= 0) || (pInChIFrom[iComponent + i].nNumberOfAtoms > MAX_ATOMS))
7110 : : {
7111 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7112 : 0 : goto exit_function;
7113 : : }
7114 : : #endif
7115 : 0 : ret = CopySegment(pInChI + iComponent + i, pInChIFrom + iComponent + i, nCpyType, bIsoTo, bIsoFrom);
7116 [ # # ]: 0 : if (!ret)
7117 : : {
7118 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7119 : : }
7120 [ # # ]: 0 : if (ret < 0)
7121 : : {
7122 : 0 : goto exit_function;
7123 : : }
7124 : : }
7125 : 0 : mpy_component = val;
7126 : 0 : goto end_main_cycle;
7127 : : }
7128 : : else
7129 : : /* regular multiplier */
7130 [ + - + + ]: 7 : if ((p = strchr(pStart, '*')) && p < pEnd)
7131 : : {
7132 : 4 : mpy_component = (int)inchi_strtol(pStart, &q, 10);
7133 : : #if ( CHECK_STRTOL_ATNUMB==1 )
7134 [ + - - + ]: 4 : if (mpy_component > MAX_ATOMS || mpy_component < 0)
7135 : : {
7136 : 0 : ret = RI_ERR_SYNTAX;
7137 : 0 : goto exit_function;
7138 : : }
7139 : : #endif
7140 [ - + ]: 4 : if (p != q)
7141 : : {
7142 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7143 : 0 : goto exit_function;
7144 : : }
7145 : :
7146 : 4 : p++; /* move to the 1st character of the component */
7147 : : }
7148 : : else
7149 : : {
7150 : 3 : mpy_component = 1;
7151 : 3 : p = pStart;
7152 : : }
7153 : : #if (FIX_DALKE_BUGS == 1)
7154 [ + + + + ]: 7 : if (mpy_component < 1 || iComponent + mpy_component > nNumComponents)
7155 : : {
7156 : 3 : ret = RI_ERR_SYNTAX; /* syntax error */
7157 : 3 : goto exit_function;
7158 : : }
7159 : : #endif
7160 : 4 : pStart = p;
7161 [ + - - + ]: 4 : if (p < pEnd && *pbAbc == -1)
7162 : : {
7163 : : /* check if compressed InChI */
7164 : 0 : *pbAbc = isupper(UCINT * p) ? 1 : 0;
7165 : : }
7166 [ - + ]: 4 : base = (*pbAbc == 1) ? ALPHA_BASE : 10;
7167 [ - + ]: 4 : if (*pbAbc == 1)
7168 : : {
7169 : : /* process the componnt: at1-at2p,at1-at2p,... */
7170 : : /* pass 1: find number of stereobonds */
7171 [ # # ]: 0 : for (p = pStart, iBond = 0; p < pEnd; iBond++)
7172 : : {
7173 : : /* atoms 1, 2, and parity */
7174 [ # # ]: 0 : if ((nAtom1 = (AT_NUMB)inchi_strtol(p, &p, base)) &&
7175 [ # # # # ]: 0 : (nAtom2 = (AT_NUMB)inchi_strtol(p, &p, base)) &&
7176 : 0 : (bondParity = (int)inchi_strtol(p, &p, 10),
7177 [ # # # # ]: 0 : AB_MIN_KNOWN_PARITY <= bondParity && bondParity <= AB_MAX_KNOWN_PARITY))
7178 : : {
7179 : : ; /* okay */
7180 : : }
7181 : : else
7182 : : {
7183 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7184 : 0 : goto exit_function;
7185 : : }
7186 [ # # ]: 0 : if (nAtom1 <= nAtom2 ||
7187 [ # # ]: 0 : nAtom1 > pInChI[iComponent].nNumberOfAtoms)
7188 : : {
7189 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7190 : 0 : goto exit_function;
7191 : : }
7192 : : }
7193 : : }
7194 : : else
7195 : : {
7196 : : /* process the componnt: at1-at2p,at1-at2p,... */
7197 : : /* pass 1: find number of stereobonds */
7198 [ + + ]: 8 : for (p = pStart, iBond = 0; p < pEnd; iBond++, p += (*p == ','))
7199 : : {
7200 : 4 : nAtom1 = (AT_NUMB)inchi_strtol(p, &q, 10);
7201 : : #if ( CHECK_STRTOL_ATNUMB==1 )
7202 [ - + ]: 4 : if (nAtom1 > MAX_ATOMS || nAtom1 < 0)
7203 : : {
7204 : 0 : ret = RI_ERR_SYNTAX;
7205 : 0 : goto exit_function;
7206 : : }
7207 : : #endif
7208 [ - + ]: 4 : if (*q != '-')
7209 : : {
7210 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7211 : 0 : goto exit_function;
7212 : : }
7213 : 4 : p = q + 1;
7214 : 4 : nAtom2 = (AT_NUMB)inchi_strtol(p, &q, 10);
7215 : : #if ( CHECK_STRTOL_ATNUMB==1 )
7216 [ - + ]: 4 : if (nAtom2 > MAX_ATOMS || nAtom2 < 0)
7217 : : {
7218 : 0 : ret = RI_ERR_SYNTAX;
7219 : 0 : goto exit_function;
7220 : : }
7221 : : #endif
7222 [ + - + - : 4 : if (!nAtom1 || !nAtom2 ||
+ - ]
7223 : 4 : nAtom1 <= nAtom2 ||
7224 [ + - ]: 4 : nAtom1 > pInChI[iComponent].nNumberOfAtoms ||
7225 [ - + ]: 4 : !(r = strchr((char*)parity_type, *q))) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
7226 : : {
7227 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7228 : 0 : goto exit_function;
7229 : : }
7230 : 4 : p = q + 1;
7231 : : }
7232 : : }
7233 : :
7234 [ - + ]: 4 : if (p != pEnd)
7235 : : {
7236 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7237 : 0 : goto exit_function;
7238 : : }
7239 : 4 : len = iBond;
7240 : :
7241 : : #if ( FIX_GAF_2019_2==1 )
7242 [ + - - + ]: 4 : if ((iComponent > nNumComponents - 1) || (iComponent < 0))
7243 : : {
7244 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7245 : 0 : goto exit_function;
7246 : : }
7247 [ + - - + ]: 4 : if (pInChI[iComponent].nNumberOfAtoms <= 0 || pInChI[iComponent].nNumberOfAtoms > MAX_ATOMS)
7248 : : {
7249 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7250 : 0 : goto exit_function;
7251 : : }
7252 : : #endif
7253 : :
7254 : : /* memory allocation */
7255 [ - + ]: 4 : pStereo = bIso ? &pInChI[iComponent].StereoIsotopic : &pInChI[iComponent].Stereo;
7256 [ + - ]: 4 : if (!*pStereo)
7257 : : {
7258 [ - + ]: 4 : if (!(*pStereo = (INChI_Stereo*)inchi_calloc(1, sizeof(**pStereo))))
7259 : : {
7260 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
7261 : 0 : goto exit_function;
7262 : : }
7263 : : }
7264 [ + - + - ]: 4 : if (pStereo[0]->b_parity || pStereo[0]->nNumberOfStereoBonds ||
7265 [ + - - + ]: 4 : pStereo[0]->nBondAtom1 || pStereo[0]->nBondAtom2)
7266 : : {
7267 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: bonds have already been allocated */
7268 : 0 : goto exit_function;
7269 : : }
7270 : : /* allocate sp2 stereo */
7271 : 4 : len_limit = len + 1;
7272 [ + - ]: 4 : if (!(pStereo[0]->b_parity = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->b_parity[0]))) ||
7273 [ + - ]: 4 : !(pStereo[0]->nBondAtom1 = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->nBondAtom1[0]))) ||
7274 [ - + ]: 4 : !(pStereo[0]->nBondAtom2 = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pStereo[0]->nBondAtom2[0])))) /* djb-rwth: cast operators added */
7275 : : {
7276 : : /* cleanup */
7277 [ # # ]: 0 : if (pStereo[0]->b_parity)
7278 : : {
7279 : : INCHI_HEAPCHK
7280 [ # # ]: 0 : inchi_free(pStereo[0]->b_parity);
7281 : 0 : pStereo[0]->b_parity = NULL;
7282 : : }
7283 [ # # ]: 0 : if (pStereo[0]->nBondAtom1)
7284 : : {
7285 : : INCHI_HEAPCHK
7286 [ # # ]: 0 : inchi_free(pStereo[0]->nBondAtom1);
7287 : 0 : pStereo[0]->nBondAtom1 = NULL;
7288 : : }
7289 [ # # ]: 0 : if (pStereo[0]->nBondAtom2)
7290 : : {
7291 : : INCHI_HEAPCHK
7292 [ # # ]: 0 : inchi_free(pStereo[0]->nBondAtom2);
7293 : 0 : pStereo[0]->nBondAtom2 = NULL;
7294 : : }
7295 : : INCHI_HEAPCHK
7296 : 0 : ret = RI_ERR_ALLOC; /* memory allocation failed */
7297 : 0 : goto exit_function;
7298 : : }
7299 : :
7300 : : /* pass 2: store stereobonds */
7301 [ - + ]: 4 : if (*pbAbc == 1)
7302 : : {
7303 [ # # ]: 0 : for (p = pStart, iBond = 0; p < pEnd; iBond++)
7304 : : {
7305 [ # # ]: 0 : if ((nAtom1 = (AT_NUMB)inchi_strtol(p, &p, base)) &&
7306 [ # # # # ]: 0 : (nAtom2 = (AT_NUMB)inchi_strtol(p, &p, base)) &&
7307 : 0 : (bondParity = (int)inchi_strtol(p, &p, 10),
7308 [ # # # # ]: 0 : AB_MIN_KNOWN_PARITY <= bondParity && bondParity <= AB_MAX_KNOWN_PARITY))
7309 : : {
7310 : : ; /* okay */
7311 : : }
7312 : : else
7313 : : {
7314 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7315 : 0 : goto exit_function;
7316 : : }
7317 : 0 : pStereo[0]->b_parity[iBond] = bondParity;
7318 : 0 : pStereo[0]->nBondAtom1[iBond] = nAtom1;
7319 : 0 : pStereo[0]->nBondAtom2[iBond] = nAtom2;
7320 : :
7321 [ # # ]: 0 : if (iBond &&
7322 [ # # ]: 0 : !(pStereo[0]->nBondAtom1[iBond - 1] < nAtom1 ||
7323 [ # # ]: 0 : (pStereo[0]->nBondAtom1[iBond - 1] == nAtom1 &&
7324 [ # # ]: 0 : pStereo[0]->nBondAtom2[iBond - 1] < nAtom2))) /* djb-rwth: addressing LLVM warning */
7325 : : {
7326 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: wrong bond order */
7327 : 0 : goto exit_function;
7328 : : }
7329 : : }
7330 : : }
7331 : : else
7332 : : {
7333 [ + + ]: 8 : for (p = pStart, iBond = 0; p < pEnd; iBond++, p += (*p == ','))
7334 : : {
7335 : 4 : nAtom1 = (AT_NUMB)inchi_strtol(p, &q, 10);
7336 : : #if ( CHECK_STRTOL_ATNUMB==1 )
7337 [ - + ]: 4 : if (nAtom1 > MAX_ATOMS || nAtom1 < 0)
7338 : : {
7339 : 0 : ret = RI_ERR_SYNTAX;
7340 : 0 : goto exit_function;
7341 : : }
7342 : : #endif
7343 [ - + ]: 4 : if (*q != '-')
7344 : : {
7345 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7346 : 0 : goto exit_function;
7347 : : }
7348 : 4 : p = q + 1;
7349 : 4 : nAtom2 = (AT_NUMB)inchi_strtol(p, &q, 10);
7350 : : #if ( CHECK_STRTOL_ATNUMB==1 )
7351 [ - + ]: 4 : if (nAtom2 > MAX_ATOMS || nAtom2 < 0)
7352 : : {
7353 : 0 : ret = RI_ERR_SYNTAX;
7354 : 0 : goto exit_function;
7355 : : }
7356 : : #endif
7357 [ - + ]: 4 : if (!(r = strchr((char*)parity_type, *q)))
7358 : : {
7359 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7360 : 0 : goto exit_function;
7361 : : }
7362 : 4 : p = q + 1;
7363 : 4 : bondParity = (int)(r - parity_type) + 1;
7364 : : /* djb-rwth: preventing buffer overrun */
7365 [ + - ]: 4 : if (iBond < len_limit)
7366 : : {
7367 : 4 : pStereo[0]->b_parity[iBond] = bondParity;
7368 : 4 : pStereo[0]->nBondAtom1[iBond] = nAtom1;
7369 : 4 : pStereo[0]->nBondAtom2[iBond] = nAtom2;
7370 : : }
7371 : : else
7372 : : {
7373 : 0 : ret = RI_ERR_PROGR;
7374 : 0 : goto exit_function;
7375 : : }
7376 : :
7377 [ - + ]: 4 : if (iBond &&
7378 [ # # ]: 0 : !(pStereo[0]->nBondAtom1[iBond - 1] < nAtom1 ||
7379 [ # # ]: 0 : (pStereo[0]->nBondAtom1[iBond - 1] == nAtom1 &&
7380 [ # # ]: 0 : pStereo[0]->nBondAtom2[iBond - 1] < nAtom2))) /* djb-rwth: addressing LLVM warning */
7381 : : {
7382 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: wrong bond order */
7383 : 0 : goto exit_function;
7384 : : }
7385 : : }
7386 : : }
7387 : 4 : pStereo[0]->nNumberOfStereoBonds = iBond;
7388 : :
7389 [ - + ]: 4 : if (p != pEnd)
7390 : : {
7391 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7392 : 0 : goto exit_function;
7393 : : }
7394 : :
7395 : : /* multiplier */
7396 [ + + ]: 5 : for (i = 1; i < mpy_component; i++)
7397 : : {
7398 : 1 : ret = CopySegment(pInChI + iComponent + i, pInChI + iComponent, nCpyType, bIso, bIso);
7399 [ - + ]: 1 : if (ret < 0)
7400 : : {
7401 : 0 : goto exit_function;
7402 : : }
7403 : : }
7404 : :
7405 : 4 : end_main_cycle:
7406 : 4 : iComponent += mpy_component;
7407 [ + + ]: 4 : if (*pEnd)
7408 : : {
7409 : 3 : pStart = pEnd + 1;
7410 : 3 : continue;
7411 : : }
7412 : : else
7413 : : {
7414 : 1 : break;
7415 : : }
7416 : : }
7417 [ - + ]: 1 : if (nNumComponents != iComponent)
7418 : : {
7419 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7420 : 0 : goto exit_function;
7421 : : }
7422 : 1 : ret = iComponent + 1;
7423 : :
7424 : 4 : exit_function:
7425 : :
7426 : 4 : return ret;
7427 : : }
7428 : :
7429 : :
7430 : : /****************************************************************************
7431 : : Parse "/p" InChI layer
7432 : : ****************************************************************************/
7433 : 11 : int ParseSegmentProtons(const char* str,
7434 : : int bMobileH,
7435 : : REM_PROTONS nNumProtons[],
7436 : : int ppnNumComponents[])
7437 : : {
7438 : : /* Pass 1: count bonds and find actual numbers of atom */
7439 : : int val;
7440 : : const char* q, * pStart, * pEnd;
7441 : : int ret;
7442 : :
7443 [ + - ]: 11 : if (str[0] != 'p')
7444 : : {
7445 : 11 : return 0;
7446 : : }
7447 : :
7448 : 0 : pStart = (char*)str + 1;
7449 : :
7450 : : while (1)
7451 : : {
7452 : : /* cycle over components */
7453 [ # # ]: 0 : if (!(pEnd = strchr(pStart, ';')))
7454 : : {
7455 : 0 : pEnd = pStart + strlen(pStart);
7456 : : }
7457 : :
7458 [ # # # # ]: 0 : if (pStart[0] == '+' && isdigit(UCINT pStart[1]))
7459 : : {
7460 : 0 : val = (int)inchi_strtol(pStart + 1, &q, 10);
7461 : : }
7462 : : else
7463 : : {
7464 [ # # # # ]: 0 : if (pStart[0] == '-' && isdigit(UCINT pStart[1]))
7465 : : {
7466 : 0 : val = -(int)inchi_strtol(pStart + 1, &q, 10);
7467 : : }
7468 : : else
7469 : : {
7470 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7471 : 0 : goto exit_function;
7472 : : }
7473 : : }
7474 [ # # ]: 0 : if (!val)
7475 : : {
7476 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7477 : 0 : goto exit_function;
7478 : : }
7479 : 0 : nNumProtons[bMobileH].nNumRemovedProtons = val;
7480 [ # # # # ]: 0 : if (*pEnd || q != pEnd)
7481 : : {
7482 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
7483 : 0 : goto exit_function;
7484 : : }
7485 : : else
7486 : : {
7487 : : break;
7488 : : }
7489 : : }
7490 : 0 : ret = 1;
7491 : :
7492 : 0 : exit_function:
7493 : :
7494 : 0 : return ret;
7495 : : }
7496 : :
7497 : :
7498 : : /****************************************************************************
7499 : : Parse "/z" InChI layer
7500 : : ****************************************************************************/
7501 : 11 : int ParseSegmentPolymer(const char* str,
7502 : : int bMobileH,
7503 : : REM_PROTONS nNumProtons[],
7504 : : int ppnNumComponents[],
7505 : : int na_total,
7506 : : int nb_total,
7507 : : int bInchi2Struct,
7508 : : OAD_Polymer** ppPolymer,
7509 : : OAD_V3000** ppV3000)
7510 : : {
7511 : : const char* p, * q, * pStart, * pEnd, * p0;
7512 : 11 : char comma = ',', dot = '.', dash = '-', lt_par = '(', rt_par = ')';
7513 : : int iunit, val, ret, prev, is_range, pdn_limit;
7514 : 11 : int curr_atom, type = -1, subtype = -1, conn = -1;
7515 : : AT_NUMB num_atom;
7516 : : INT_ARRAY alist;
7517 : 11 : OAD_Polymer* pd = NULL;
7518 : :
7519 [ + + ]: 11 : if (str[0] != 'z')
7520 : : {
7521 : 10 : return 0;
7522 : : }
7523 : :
7524 [ - + ]: 1 : if (IntArray_Alloc(&alist, 4))
7525 : : {
7526 : 0 : return RI_ERR_ALLOC;
7527 : : }
7528 : :
7529 [ - + ]: 1 : if (*ppPolymer)
7530 : : {
7531 : 0 : OAD_Polymer_Free(*ppPolymer);
7532 : : }
7533 : :
7534 : 1 : pd = *ppPolymer = (OAD_Polymer*)
7535 : 1 : inchi_calloc(1, sizeof(OAD_Polymer));
7536 : 1 : (*ppPolymer)->pzz = NULL;
7537 : :
7538 [ - + ]: 1 : if (!pd)
7539 : : {
7540 : 0 : ret = RI_ERR_ALLOC; goto exit_function;
7541 : : }
7542 : :
7543 : :
7544 [ - + ]: 1 : if (!bInchi2Struct)
7545 : : {
7546 : 0 : ret = RI_ERR_SYNTAX; goto exit_function;
7547 : : }
7548 : :
7549 : : /* Count units */
7550 : 1 : pd->n = 1;
7551 : 1 : p = (char*)str + 1;
7552 [ - + ]: 1 : while ((p = strchr(p, ';'))) /* djb-rwth: addressing LLVM warning */
7553 : : {
7554 : 0 : p++;
7555 : 0 : pd->n++;
7556 : : }
7557 : 1 : pd->units = (OAD_PolymerUnit**)
7558 : 1 : inchi_calloc(pd->n, sizeof(OAD_PolymerUnit*));
7559 : 1 : pdn_limit = pd->n;
7560 [ - + ]: 1 : if (!pd->units)
7561 : : {
7562 : 0 : ret = RI_ERR_ALLOC; goto exit_function;
7563 : : }
7564 : :
7565 : 1 : pStart = (char*)str;
7566 : 1 : pStart++;
7567 [ - + ]: 1 : if (!pStart)
7568 : : {
7569 : 0 : ret = RI_ERR_PROGR;
7570 : 0 : goto exit_function;
7571 : : }
7572 : :
7573 : 1 : iunit = 0;
7574 : : /* djb-rwth: fixing oss-fuzz issue #67678 */
7575 [ + - + + ]: 2 : while (pStart && (*pStart))
7576 : : {
7577 : 1 : OAD_PolymerUnit* unit = NULL;
7578 : :
7579 [ - + ]: 1 : if (*pStart == ';')
7580 : : {
7581 : 0 : pStart++;
7582 : : }
7583 : :
7584 [ + - ]: 1 : if (!(pEnd = strchr(pStart, ';')))
7585 : : {
7586 : 1 : pEnd = pStart + strlen(pStart);
7587 : : }
7588 [ - + ]: 1 : if (!isdigit(UCINT pStart[0]))
7589 : : {
7590 : 0 : ret = RI_ERR_SYNTAX;
7591 : 0 : goto exit_function;
7592 : : }
7593 : 1 : val = (int)inchi_strtol(pStart + 0, &q, 10);
7594 : 1 : type = val / 100;
7595 : 1 : subtype = (val - (type * 100)) / 10;
7596 : 1 : conn = (val - (type * 100 + subtype * 10));
7597 [ - + ]: 1 : if (*q != '-')
7598 : : {
7599 : 0 : ret = RI_ERR_SYNTAX;
7600 : 0 : goto exit_function;
7601 : : }
7602 : : #if ( FIX_OSS_FUZZ_30162_30343==1 )
7603 [ - + ]: 1 : if (val < 100) /* type should always be non-zero followed by subtype and conn, like 101 or 200 */
7604 : : {
7605 : 0 : ret = RI_ERR_SYNTAX;
7606 : 0 : goto exit_function;
7607 : : }
7608 : : #endif
7609 : :
7610 : : #if ( FIX_GAF_2020_25741==1 )
7611 : : {
7612 : 1 : int valid_unit = 0;
7613 [ + - - + : 1 : if (type == POLYMER_STY_NON || type == POLYMER_STY_SRU || type == POLYMER_STY_MON ||
- - - - ]
7614 [ # # # # : 0 : type == POLYMER_STY_COP || type == POLYMER_STY_MOD || type == POLYMER_STY_CRO ||
# # ]
7615 : : type == POLYMER_STY_MER)
7616 : : {
7617 [ - + - - : 1 : if (subtype == POLYMER_SST_NON || subtype == POLYMER_SST_ALT ||
- - ]
7618 [ # # ]: 0 : subtype == POLYMER_SST_RAN || subtype == POLYMER_SST_BLK)
7619 : : {
7620 [ + - - + : 1 : if (conn == POLYMER_CONN_NON || conn == POLYMER_CONN_HT ||
- - ]
7621 [ # # ]: 0 : conn == POLYMER_CONN_HH || conn == POLYMER_CONN_EU)
7622 : : {
7623 : 1 : valid_unit = 1;
7624 : : }
7625 : : }
7626 : : }
7627 [ - + ]: 1 : if (!valid_unit)
7628 : : {
7629 : 0 : ret = RI_ERR_SYNTAX;
7630 : 0 : goto exit_function;
7631 : : }
7632 : : }
7633 : : #endif
7634 : :
7635 : 1 : q++;
7636 : 1 : prev = 0;
7637 : 1 : is_range = 0;
7638 [ + - + + ]: 3 : for (p = q, curr_atom = 0; p < pEnd && *p != '('; curr_atom++)
7639 : : {
7640 : 2 : num_atom = (AT_NUMB)inchi_strtol(p, &p, 10);
7641 : : #if ( ( CHECK_STRTOL_ATNUMB==1 ) || ( FIX_GAF_2019_2==1 ) )
7642 [ - + ]: 2 : if (num_atom > na_total || num_atom < 0)
7643 : : {
7644 : 0 : ret = RI_ERR_SYNTAX;
7645 : 0 : goto exit_function;
7646 : : }
7647 : : #endif
7648 [ + - - + ]: 2 : if (!num_atom || num_atom > na_total)
7649 : : {
7650 : 0 : ret = RI_ERR_SYNTAX; goto exit_function;
7651 : : }
7652 [ + + ]: 2 : if (is_range)
7653 : : {
7654 : : int a;
7655 [ + + ]: 4 : for (a = prev + 1; a <= num_atom; a++)
7656 : : {
7657 [ - + ]: 3 : if (0 != IntArray_Append(&alist, a))
7658 : : {
7659 : 0 : ret = RI_ERR_ALLOC; goto exit_function;
7660 : : }
7661 : : }
7662 : 1 : is_range = 0;
7663 : 1 : prev = 0;
7664 : : }
7665 : : else
7666 : : {
7667 [ - + ]: 1 : if (0 != IntArray_Append(&alist, num_atom))
7668 : : {
7669 : 0 : ret = RI_ERR_ALLOC; goto exit_function;
7670 : : }
7671 : 1 : prev = num_atom;
7672 : : }
7673 [ + + ]: 2 : if (*p == '-')
7674 : : {
7675 : 1 : p++;
7676 : 1 : is_range = 1;
7677 : : }
7678 [ - + ]: 1 : else if (*p == ',')
7679 : : {
7680 : 0 : p++;
7681 : : }
7682 : : }
7683 : :
7684 [ + - ]: 1 : if (alist.used)
7685 : : {
7686 : 1 : unit = OAD_PolymerUnit_New(4, /* maxatoms */
7687 : : 0, /* maxbonds=0 for now */
7688 : : iunit + 1, /* id */
7689 : : iunit + 1, /* label */
7690 : : type,
7691 : : subtype,
7692 : : conn,
7693 : : "", /* smt */
7694 : : alist.used,
7695 : : &alist,
7696 : : 0, /* blist.used */
7697 : : NULL, /* &blist */
7698 : : 0, /* nlinks */
7699 : : NULL /* **links */
7700 : : );
7701 : :
7702 [ - + ]: 1 : if (!unit)
7703 : : {
7704 : 0 : ret = RI_ERR_ALLOC; goto exit_function;
7705 : : }
7706 : : /* djb-rwth: preventing buffer overrun */
7707 [ + - ]: 1 : if (iunit < pdn_limit)
7708 : : {
7709 : 1 : pd->units[iunit] = unit;
7710 : : }
7711 : : else
7712 : : {
7713 : 0 : ret = RI_ERR_PROGR;
7714 : 0 : goto exit_function;
7715 : : }
7716 : 1 : IntArray_Reset(&alist);
7717 : 1 : iunit++;
7718 : : }
7719 : :
7720 [ + - ]: 1 : if (*p == lt_par)
7721 : 0 : {
7722 : : /* Structure-based representn, read crossing bonds information */
7723 : 1 : const int nothing = 0, endgroups = 1, stars = 2, stars_ring = 3, stars_bond = 4, stars_atom = 5;
7724 : 1 : int have = nothing;
7725 : 1 : int res, ib, err = 0; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
7726 : : INT_ARRAY numlist;
7727 : :
7728 [ - + ]: 1 : if (IntArray_Alloc(&numlist, 4))
7729 : : {
7730 : 0 : ret = RI_ERR_ALLOC;
7731 : 0 : goto exit_function;
7732 : : }
7733 : :
7734 : 1 : p0 = p;
7735 [ + - ]: 2 : while (*(++p))
7736 : : {
7737 [ + + ]: 2 : if (*p == '-')
7738 : : {
7739 : 1 : have = endgroups; break;
7740 : : }
7741 [ - + ]: 1 : if (*p == ',')
7742 : : {
7743 : 0 : have = stars; break;
7744 : : }
7745 : : }
7746 [ - + ]: 1 : if (have == stars)
7747 : : {
7748 [ # # ]: 0 : while (*(++p))
7749 : : {
7750 [ # # ]: 0 : if (*p == ',')
7751 : : {
7752 : 0 : have = stars_ring; break;
7753 : : }
7754 [ # # ]: 0 : if (*p == '.')
7755 : : {
7756 : 0 : have = stars_bond; break;
7757 : : }
7758 [ # # ]: 0 : if (*p == ')')
7759 : : {
7760 : 0 : have = stars_atom; break;
7761 : : }
7762 : : }
7763 : : }
7764 : 1 : p = p0;
7765 : :
7766 [ + - ]: 1 : if (unit) /* djb-rwth: fixing a NULL pointer dereference */
7767 : : {
7768 : 1 : unit->cyclizable = CLOSING_SRU_NOT_APPLICABLE;
7769 [ + - ]: 1 : if (have == endgroups)
7770 : : {
7771 : : /* Read end groups notation */
7772 : 1 : p = ParseSegmentReadDelimitedNumbers(p, pEnd, &numlist, dash, comma, &res);
7773 [ + - + - ]: 1 : if (res == 1 && numlist.used == 2)
7774 : : {
7775 : 1 : p = ParseSegmentReadDelimitedNumbers(p, pEnd, &numlist, dash, rt_par, &res);
7776 : : }
7777 [ + - + - ]: 1 : if (res == 1 && numlist.used == 4)
7778 : : {
7779 : 1 : pStart = p + 1;
7780 : 1 : unit->nb = 2;
7781 : 1 : unit->blist = (int*)inchi_calloc(2 * (long long)unit->nb, sizeof(int)); /* djb-rwth: cast operator added */
7782 [ - + ]: 1 : if (!unit->blist)
7783 : : {
7784 : 0 : ret = RI_ERR_ALLOC; IntArray_Free(&numlist); goto exit_function;
7785 : : }
7786 : 1 : unit->blist[0] = numlist.item[0]; unit->blist[1] = numlist.item[1];
7787 : 1 : unit->blist[2] = numlist.item[2]; unit->blist[3] = numlist.item[3];
7788 : 1 : unit->cap1 = numlist.item[0]; /* just for GAF check belows */
7789 : 1 : unit->cap2 = numlist.item[2];
7790 : 1 : IntArray_Free(&numlist);
7791 : 1 : continue;
7792 : : }
7793 : : }
7794 [ # # # # ]: 0 : else if (have == stars_ring ||
7795 [ # # ]: 0 : have == stars_bond ||
7796 : : have == stars_atom)
7797 : : {
7798 : : /* Read star atoms - frame shiftable bonds notation */
7799 : 0 : IntArray_Reset(&numlist);
7800 : 0 : p = ParseSegmentReadDelimitedNumbers(p, pEnd, &numlist, comma, dash, &res);
7801 [ # # # # ]: 0 : if (res != 1 || numlist.used != 2)
7802 : : {
7803 : 0 : ret = RI_ERR_SYNTAX; IntArray_Free(&numlist); goto exit_function;
7804 : : }
7805 : : /* OK, we got star atom numbers */
7806 [ # # ]: 0 : if (have == stars_ring)
7807 : : {
7808 : 0 : p = ParseSegmentReadDelimitedNumbers(p, pEnd, &numlist, comma, rt_par, &res);
7809 [ # # # # ]: 0 : if (res != 1 || numlist.used < 4)
7810 : : {
7811 : 0 : ret = RI_ERR_SYNTAX;
7812 : 0 : IntArray_Free(&numlist);
7813 : 0 : goto exit_function;
7814 : : }
7815 : 0 : unit->cyclizable = CLOSING_SRU_RING;
7816 : 0 : unit->nbkbonds = (numlist.used - 2) / 2;
7817 : : }
7818 [ # # ]: 0 : else if (have == stars_bond)
7819 : : {
7820 : 0 : p = ParseSegmentReadDelimitedNumbers(p, pEnd, &numlist, dot, rt_par, &res);
7821 [ # # # # ]: 0 : if (res != 1 || numlist.used < 4)
7822 : : {
7823 : 0 : ret = RI_ERR_SYNTAX;
7824 : 0 : IntArray_Free(&numlist);
7825 : 0 : goto exit_function;
7826 : : }
7827 : 0 : unit->cyclizable = CLOSING_SRU_HIGHER_ORDER_BOND;
7828 : 0 : unit->nbkbonds = 1;
7829 : : }
7830 [ # # ]: 0 : else if (have == stars_atom)
7831 : : {
7832 : 0 : int num = inchi_strtol(++p, &p, 10);
7833 : : #if ( ( CHECK_STRTOL_ATNUMB==1 ) || ( FIX_GAF_2019_2==1 ) )
7834 [ # # # # ]: 0 : if (num > na_total || num < 0)
7835 : : {
7836 : 0 : IntArray_Free(&numlist);
7837 : 0 : ret = RI_ERR_SYNTAX;
7838 : 0 : goto exit_function;
7839 : : }
7840 : : #endif
7841 [ # # ]: 0 : if (*p != rt_par)
7842 : : {
7843 : 0 : ret = RI_ERR_SYNTAX;
7844 : 0 : IntArray_Free(&numlist);
7845 : 0 : goto exit_function;
7846 : : }
7847 : 0 : IntArray_Append(&numlist, num);
7848 : 0 : unit->cyclizable = CLOSING_SRU_DIRADICAL;
7849 : 0 : unit->nbkbonds = 1;
7850 : : }
7851 : : }
7852 : : else
7853 : : {
7854 : 0 : ret = RI_ERR_SYNTAX; IntArray_Free(&numlist); goto exit_function;
7855 : : }
7856 : :
7857 : 0 : unit->cap1 = numlist.item[0];
7858 : 0 : unit->cap2 = numlist.item[1];
7859 : :
7860 [ # # ]: 0 : if (unit->bkbonds)
7861 : : {
7862 : 0 : imat_free(unit->maxbkbonds, unit->bkbonds);
7863 : 0 : unit->bkbonds = NULL;
7864 : : }
7865 : 0 : unit->maxbkbonds = inchi_max(unit->maxbkbonds, unit->nbkbonds);
7866 : 0 : err = imat_new(unit->maxbkbonds, 2, &(unit->bkbonds)); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
7867 [ # # ]: 0 : for (ib = 0; ib < unit->nbkbonds; ib++)
7868 : : {
7869 : 0 : unit->bkbonds[ib][0] = numlist.item[ib * 2 + 2];
7870 [ # # ]: 0 : if (numlist.used != 3)
7871 : : {
7872 : 0 : unit->bkbonds[ib][1] = numlist.item[ib * 2 + 3];
7873 : : }
7874 : : else
7875 : : {
7876 : 0 : unit->bkbonds[ib][1] = unit->bkbonds[ib][0];
7877 : : }
7878 : : }
7879 [ # # ]: 0 : if (unit->nbkbonds > 0)
7880 : : {
7881 : : /*unit->cyclizable = 1;*/
7882 : 0 : unit->cap1 = numlist.item[0];
7883 : 0 : unit->cap2 = numlist.item[1];
7884 : : }
7885 : : }
7886 : :
7887 : 0 : pStart = p + 1;
7888 : 0 : IntArray_Free(&numlist);
7889 : 0 : continue;
7890 : : }
7891 : :
7892 [ # # ]: 0 : if (*p == ';')
7893 : : {
7894 : 0 : p++;
7895 : : }
7896 : 0 : q = p;
7897 : 0 : pStart = p;
7898 : : }
7899 : :
7900 : 1 : pd->really_do_frame_shift = 1;
7901 : 1 : pd->frame_shift_scheme = FSS_STARS_CYCLED;
7902 : 1 : pd->treat = POLYMERS_MODERN;
7903 : 1 : pd->is_in_reconn = 0;
7904 : :
7905 : : /*OAD_Polymer_DebugTrace( pd );*/
7906 : :
7907 : 1 : ret = pd->n;
7908 : :
7909 : 1 : exit_function:
7910 : :
7911 : 1 : IntArray_Free(&alist);
7912 [ + - + - : 1 : if (ret == RI_ERR_ALLOC || ret == RI_ERR_SYNTAX || ret == RI_ERR_PROGR)
+ - ]
7913 : : {
7914 : : /*FreeExtOrigAtData( pd, pv );*/
7915 : : }
7916 : : #if ( FIX_GAF_2019_2==1 )
7917 : : else
7918 : : {
7919 [ + - ]: 1 : if (pd)
7920 : : {
7921 : 1 : int iu, ipsb, maxats = na_total + pd->n_pzz;
7922 [ + + ]: 2 : for (iu = 0; iu < pd->n; iu++)
7923 : : {
7924 : : int astar1, astar2;
7925 : : #if ( FIX_OSS_FUZZ_25604==1 )
7926 [ - + ]: 1 : if (NULL == pd->units[iu])
7927 : : {
7928 : 0 : ret = RI_ERR_SYNTAX;
7929 : 0 : break;
7930 : : }
7931 : : #endif
7932 : 1 : astar1 = pd->units[iu]->cap1;
7933 : 1 : astar2 = pd->units[iu]->cap2;
7934 [ - + ]: 1 : if (0 == pd->units[iu]->nb)
7935 : : {
7936 : 0 : continue;
7937 : : }
7938 [ - + ]: 1 : if (ret == RI_ERR_SYNTAX)
7939 : : {
7940 : 0 : break;
7941 : : }
7942 [ + - + - : 1 : if (astar1 > maxats || astar1 <= 0 || astar2 > maxats || astar2 <= 0)
+ - - + ]
7943 : : {
7944 : 0 : ret = RI_ERR_SYNTAX;
7945 : 0 : break;
7946 : : }
7947 [ - + ]: 1 : for (ipsb = 0; ipsb < pd->units[iu]->nbkbonds; ipsb++)
7948 : : {
7949 : : int a1psb, a2psb;
7950 : 0 : a1psb = pd->units[iu]->bkbonds[ipsb][0];
7951 : 0 : a2psb = pd->units[iu]->bkbonds[ipsb][1];
7952 [ # # # # : 0 : if (a1psb > maxats || a1psb <= 0 || a2psb > maxats || a2psb <= 0)
# # # # ]
7953 : : {
7954 : 0 : ret = RI_ERR_SYNTAX;
7955 : 0 : break;
7956 : : }
7957 : : }
7958 : : }
7959 : : }
7960 : : }
7961 : : #endif
7962 : :
7963 : 1 : return ret;
7964 : : }
7965 : :
7966 : : /****************************************************************************
7967 : : Read sequence of integer numbers from str into growing int array numlist
7968 : : until NULL, EOL or 'c_stop' symbol occurred, whichever is the first.
7969 : : Numbers are assumed to be delimited with commas.
7970 : :
7971 : : NB: on success, returns 1.
7972 : : ****************************************************************************/
7973 : 2 : const char* ParseSegmentReadDelimitedNumbers(const char* str,
7974 : : const char* pEnd,
7975 : : INT_ARRAY* numlist,
7976 : : char c_delim,
7977 : : char c_stop,
7978 : : int* ret)
7979 : : {
7980 : : const char* p, * pStart;
7981 : 2 : int num, curr_atom = 0;
7982 : :
7983 : 2 : *ret = 1;
7984 : :
7985 [ - + ]: 2 : if (!str)
7986 : : {
7987 : 0 : *ret = -1;
7988 : 0 : return NULL;
7989 : : }
7990 : :
7991 : 2 : pStart = (char*)(str + 1);
7992 : 2 : p = pStart;
7993 [ + - ]: 2 : while (*pStart)
7994 : : {
7995 [ + - + - ]: 4 : for (p = pStart, curr_atom = 0; p < pEnd && *p != c_stop; curr_atom++)
7996 : : {
7997 : 4 : num = (AT_NUMB)inchi_strtol(p, &p, 10);
7998 : : #if ( CHECK_STRTOL_ATNUMB==1 )
7999 [ + - - + ]: 4 : if (num > MAX_ATOMS || num < 0)
8000 : : {
8001 : 0 : *ret = RI_ERR_SYNTAX;
8002 : 0 : return p;
8003 : : }
8004 : : #endif
8005 : : {
8006 [ - + ]: 4 : if (0 != IntArray_Append(numlist, num))
8007 : : {
8008 : 0 : *ret = RI_ERR_SYNTAX;
8009 : 0 : return p;
8010 : : }
8011 : : }
8012 [ + + ]: 4 : if (*p == c_delim)
8013 : : {
8014 : 2 : p++;
8015 : : }
8016 [ + - ]: 2 : else if (*p == c_stop)
8017 : : {
8018 : 2 : return p;
8019 : : }
8020 : : else
8021 : : {
8022 : 0 : *ret = -1; return NULL;
8023 : : }
8024 : : }
8025 [ # # ]: 0 : if (*p == c_stop)
8026 : : {
8027 : 0 : return p;
8028 : : }
8029 : : }
8030 : :
8031 : 0 : return p;
8032 : : }
8033 : :
8034 : :
8035 : :
8036 : : /****************************************************************************
8037 : : Parse "/q" InChI layer
8038 : : ****************************************************************************/
8039 : 11 : int ParseSegmentCharge(const char* str,
8040 : : int bMobileH,
8041 : : INChI* pInpInChI[],
8042 : : int ppnNumComponents[])
8043 : : {
8044 : : /* Pass 1: count bonds and find actual numbers of atom */
8045 : : int i, mpy_component, val;
8046 : : int nNumComponents, iComponent;
8047 : : const char* p, * q, * t, * pStart, * pEnd;
8048 : : int ret;
8049 : 11 : INChI* pInChI = pInpInChI[bMobileH];
8050 : 11 : const char mult_type[] = "mnMNe";
8051 : 11 : int if_cnd = 1; /* djb-rwth: needed for some if condition restructuring */
8052 : :
8053 [ + + ]: 11 : if (str[0] != 'q')
8054 : : {
8055 : 8 : return 0;
8056 : : }
8057 : :
8058 : 3 : pStart = (char*)str + 1;
8059 : 3 : iComponent = 0;
8060 : 3 : nNumComponents = ppnNumComponents[bMobileH];
8061 : :
8062 [ + - - - ]: 3 : if (!*pStart && bMobileH == TAUT_NON)
8063 : : {
8064 [ # # ]: 0 : for (i = 0; i < nNumComponents; i++)
8065 : : {
8066 : 0 : pInChI[i].nTotalCharge = NO_VALUE_INT;
8067 : : }
8068 : 0 : return nNumComponents + 1;
8069 : : }
8070 : :
8071 : : while (1)
8072 : : {
8073 : : /* cycle over components */
8074 [ + - ]: 3 : if (!(pEnd = strchr(pStart, ';')))
8075 : : {
8076 : 3 : pEnd = pStart + strlen(pStart);
8077 : : }
8078 : :
8079 : : /* djb-rwth: condition for if block had to be rewritten */
8080 [ + - ]: 3 : if ((int)inchi_strtol(pStart, &q, 10) > 0)
8081 : : {
8082 : 3 : val = (int)inchi_strtol(pStart, &q, 10);
8083 : 3 : if_cnd = isdigit(UCINT * pStart);
8084 : :
8085 : : }
8086 : : else
8087 : : {
8088 : 0 : val = 1;
8089 : 0 : q = pStart;
8090 : 0 : if_cnd = 1;
8091 : : }
8092 : :
8093 : :
8094 [ - + - - : 3 : if (if_cnd && (t = strchr((char*)mult_type, *q)) && q + 1 == pEnd) /* djb-rwth: if_cnd applied; ignoring LLVM warning: variable used to store function return value */
- - ]
8095 : : {
8096 : : /* process the abbreviation */
8097 : :
8098 [ # # # ]: 0 : switch (bMobileH)
8099 : : {
8100 : 0 : case TAUT_YES:
8101 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8102 : 0 : goto exit_function;
8103 : 0 : case TAUT_NON:
8104 [ # # ]: 0 : if (*q != 'm' ||
8105 [ # # ]: 0 : iComponent + val > nNumComponents ||
8106 [ # # ]: 0 : iComponent + val > ppnNumComponents[TAUT_YES])
8107 : : {
8108 : :
8109 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8110 : 0 : goto exit_function;
8111 : : }
8112 [ # # ]: 0 : for (i = 0; i < val; i++)
8113 : : {
8114 : : /* avoid 0 which means "omitted" */
8115 : 0 : pInChI[iComponent + i].nTotalCharge = pInpInChI[TAUT_YES][iComponent + i].nTotalCharge ?
8116 [ # # ]: 0 : pInpInChI[TAUT_YES][iComponent + i].nTotalCharge :
8117 : : NO_VALUE_INT;
8118 : : }
8119 : 0 : mpy_component = val;
8120 : 0 : goto end_main_cycle;
8121 : 0 : default:
8122 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8123 : 0 : goto exit_function;
8124 : : }
8125 : : }
8126 : : else
8127 : : {
8128 [ - + - - ]: 3 : if ((p = strchr(pStart, '*')) && p < pEnd)
8129 : : {
8130 : 0 : mpy_component = (int)inchi_strtol(pStart, &q, 10);
8131 : : #if ( CHECK_STRTOL_ATNUMB==1 )
8132 [ # # # # ]: 0 : if (mpy_component > MAX_ATOMS || mpy_component < 0)
8133 : : {
8134 : 0 : ret = RI_ERR_SYNTAX;
8135 : 0 : goto exit_function;
8136 : : }
8137 : : #endif
8138 [ # # ]: 0 : if (p != q)
8139 : : {
8140 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8141 : 0 : goto exit_function;
8142 : : }
8143 : 0 : p++;
8144 : : }
8145 : : else
8146 : : {
8147 : 3 : mpy_component = 1;
8148 : 3 : p = pStart;
8149 : : }
8150 : : }
8151 : : #if ( FIX_DALKE_BUGS == 1 )
8152 [ + - - + ]: 3 : if (mpy_component + iComponent > nNumComponents || mpy_component <= 0)
8153 : : {
8154 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: too many components in charge layer */
8155 : 0 : goto exit_function;
8156 : : }
8157 : : #endif
8158 : 3 : pStart = p;
8159 [ + - ]: 3 : if (pStart < pEnd)
8160 : : {
8161 [ + - + - ]: 3 : if (pStart[0] == '+' && isdigit(UCINT pStart[1]))
8162 : : {
8163 : 3 : val = (int)inchi_strtol(pStart + 1, &q, 10);
8164 : 3 : pStart = q;
8165 : : }
8166 : : else
8167 : : {
8168 [ # # # # ]: 0 : if (pStart[0] == '-' && isdigit(UCINT pStart[1]))
8169 : : {
8170 : 0 : val = -(int)inchi_strtol(pStart + 1, &q, 10);
8171 : 0 : pStart = q;
8172 : : }
8173 : : else
8174 : : {
8175 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8176 : 0 : goto exit_function;
8177 : : }
8178 : : }
8179 : : #if ( FIX_DALKE_BUGS == 1 )
8180 [ + - - + ]: 3 : if (val < -256 || val > 256)
8181 : : {
8182 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8183 : 0 : goto exit_function;
8184 : : }
8185 : : #endif
8186 [ - + ]: 3 : if (!val)
8187 : : {
8188 [ # # ]: 0 : if (pStart != pEnd)
8189 : : {
8190 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8191 : 0 : goto exit_function;
8192 : : }
8193 [ # # ]: 0 : if (bMobileH == TAUT_NON)
8194 : : {
8195 : 0 : val = NO_VALUE_INT; /* avoid 0 which means "omitted" */
8196 : : }
8197 : : }
8198 : : }
8199 : : else
8200 : : {
8201 : 0 : val = NO_VALUE_INT;
8202 : : }
8203 [ + + ]: 6 : for (i = 0; i < mpy_component; i++)
8204 : : {
8205 : 3 : pInChI[iComponent + i].nTotalCharge = val;
8206 : : }
8207 : :
8208 : 3 : end_main_cycle:
8209 : 3 : iComponent += mpy_component;
8210 [ - + ]: 3 : if (*pEnd)
8211 : : {
8212 : 0 : pStart = pEnd + 1;
8213 : 0 : continue;
8214 : : }
8215 : : else
8216 : : {
8217 : 3 : break;
8218 : : }
8219 : : }
8220 : :
8221 [ - + ]: 3 : if (nNumComponents != iComponent)
8222 : : {
8223 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8224 : 0 : goto exit_function;
8225 : : }
8226 : 3 : ret = iComponent + 1;
8227 : :
8228 : 3 : exit_function:
8229 : :
8230 : 3 : return ret;
8231 : : }
8232 : :
8233 : :
8234 : : /****************************************************************************
8235 : : Parse "/h" InChI layer
8236 : : ****************************************************************************/
8237 : 22 : int ParseSegmentMobileH(const char* str,
8238 : : int bMobileH,
8239 : : INChI* pInpInChI[],
8240 : : int pnNumComponents[],
8241 : : int* pbAbc)
8242 : : {
8243 : : #define nNum_H( ICOMPONENT ) ((bMobileH==TAUT_YES)? pInChI[ICOMPONENT].nNum_H : pInChI[ICOMPONENT].nNum_H_fixed)
8244 : :
8245 : : /* Pass 1: count bonds and find actual numbers of atom */
8246 : :
8247 : 22 : int i, mpy_component, num_H, num_Minus, val, num_Atoms, numCtAtoms, tg_alloc_len, len, len2, k = 0;
8248 : : int num_H_component, num_H_formula, num_taut_H_component, num_H_InChI, ret2;
8249 : : int nNumComponents, iComponent, lenTautomer, tg_pos_Tautomer, iTGroup; /* djb-rwth: removing redundant variables */
8250 : : const char* p, * q, * h, * t, * p1, * pTaut, * pStart, * pEnd;
8251 : : int curAtom, nxtAtom; /* djb-rwth: fixing coverity ID #499563 */
8252 [ + - ]: 22 : int state, ret, nAltMobileH = ALT_TAUT(bMobileH); /* djb-rwth: removing redundant variables */
8253 : 22 : INChI* pInChI = pInpInChI[bMobileH];
8254 : 22 : INChI* pAltInChI = pInpInChI[nAltMobileH];
8255 : 22 : int base = 10;
8256 : :
8257 : 22 : num_H = -999; /* impossible value */
8258 : 22 : num_Minus = -999; /* impossible value */
8259 : 22 : tg_pos_Tautomer = -999; /* impossible value */
8260 : :
8261 : : /* number of immobile H is always allocated; immobile H are present in M layer only */
8262 : 22 : nNumComponents = pnNumComponents[bMobileH];
8263 : :
8264 : : /* djb-rwth: fixing oss-fuzz issues #66985, #66718, #43512, #43456, #43420, #42774, #34772, #30156 */
8265 : :
8266 [ + + ]: 47 : for (i = 0; i < nNumComponents; i++)
8267 : : {
8268 : 25 : len = pInChI[i].nNumberOfAtoms;
8269 [ - + - - ]: 25 : if (bMobileH == TAUT_NON && i < pnNumComponents[nAltMobileH])
8270 : : {
8271 [ # # ]: 0 : if (len < pAltInChI[i].nNumberOfAtoms)
8272 : : {
8273 : 0 : len = pAltInChI[i].nNumberOfAtoms;
8274 [ # # ]: 0 : if (pInChI[i].nNum_H)
8275 : : {
8276 [ # # ]: 0 : inchi_free(pInChI[i].nNum_H);
8277 : 0 : pInChI[i].nNum_H = NULL;
8278 : : }
8279 : : }
8280 : : }
8281 : 25 : len++;
8282 [ + - ]: 25 : if (!pInChI[i].nNum_H)
8283 : : {
8284 : 25 : S_CHAR* pi_nnh2 = (S_CHAR*)inchi_calloc(len, sizeof(pInChI[0].nNum_H[0]));
8285 [ - + ]: 25 : if (!pi_nnh2)
8286 : : {
8287 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8288 : 0 : goto exit_function;
8289 : : }
8290 : 25 : pInChI[i].nNum_H = pi_nnh2;
8291 : : /* pi_nnh2_init = true; */
8292 : : }
8293 : : /* copy immobile H from Mobile-H layer to Fixed-H layer */
8294 [ - + - - ]: 25 : if (bMobileH == TAUT_NON && i < pnNumComponents[nAltMobileH])
8295 : : {
8296 : 0 : S_CHAR* pai_nnh = (S_CHAR*)inchi_realloc(pAltInChI[i].nNum_H, len * sizeof(pAltInChI[0].nNum_H[0]));
8297 [ # # ]: 0 : if (pai_nnh)
8298 : : {
8299 : 0 : S_CHAR* pi_nnh1 = NULL; /* copied from below to satisfy C syntax 2024-09-01 DT */
8300 : 0 : pAltInChI[i].nNum_H = pai_nnh;
8301 : : /*if (!pi_nnh2_init)
8302 : : {
8303 : : */
8304 : 0 : pi_nnh1 = (S_CHAR*)inchi_calloc(len, sizeof(pInChI[0].nNum_H[0]));
8305 [ # # ]: 0 : if (!pi_nnh1)
8306 : : {
8307 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8308 : 0 : goto exit_function;
8309 : : }
8310 : 0 : memcpy(pi_nnh1, pai_nnh, ((long long)len - 1) * sizeof(pInChI[0].nNum_H[0])); /* djb-rwth: cast operator added */
8311 : : /* djb-rwth: alternative solution
8312 : : k = memcpy_custom(&pi_nnh1[i], pAltInChI[i].nNum_H, ((long long)len - 1) * sizeof(pInChI[0].nNum_H[0]))
8313 : : if (k)
8314 : : {
8315 : : ret = RI_ERR_ALLOC;
8316 : : goto exit_function;
8317 : : }
8318 : : */
8319 : 0 : pInChI[i].nNum_H = pi_nnh1;
8320 : : /*
8321 : : }
8322 : : else
8323 : : {
8324 : : memcpy(pi_nnh2, pai_nnh, ((long long)len - 1) * sizeof(pInChI[0].nNum_H[0])); * djb-rwth: cast operator added *
8325 : : * djb-rwth: alternative solution *
8326 : : k = memcpy_custom(&pi_nnh2[i], pAltInChI[i].nNum_H, ((long long)len - 1) * sizeof(pInChI[0].nNum_H[0]))
8327 : : if (k)
8328 : : {
8329 : : ret = RI_ERR_ALLOC;
8330 : : goto exit_function;
8331 : : }
8332 : :
8333 : : }
8334 : : */
8335 : : }
8336 : : else
8337 : : {
8338 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8339 : 0 : goto exit_function;
8340 : : }
8341 : : }
8342 : : }
8343 : :
8344 [ + + ]: 22 : if (str[0] != 'h')
8345 : : {
8346 : 3 : return 0;
8347 : : }
8348 : :
8349 : : /* Read Hydrogen info in 1 pass */
8350 : :
8351 : 19 : pStart = (char*)str + 1;
8352 : 19 : iComponent = 0;
8353 : 19 : nNumComponents = pnNumComponents[bMobileH];
8354 : :
8355 : : while (1)
8356 : : {
8357 : : /* cycle over components */
8358 [ + - ]: 19 : if (!(pEnd = strchr(pStart, ';')))
8359 : : {
8360 : 19 : pEnd = pStart + strlen(pStart);
8361 : : }
8362 [ + + + - ]: 19 : if ((p = strchr(pStart, '*')) && p < pEnd)
8363 : : {
8364 : 5 : mpy_component = (int)inchi_strtol(pStart, &q, 10);
8365 : : #if ( CHECK_STRTOL_ATNUMB==1 )
8366 [ + - - + ]: 5 : if (mpy_component > MAX_ATOMS || mpy_component < 0)
8367 : : {
8368 : 0 : ret = RI_ERR_SYNTAX;
8369 : 0 : goto exit_function;
8370 : : }
8371 : : #endif
8372 : : #if ( FIX_DALKE_BUGS == 1 )
8373 [ + - - + ]: 5 : if (p != q || !isdigit(UCINT* pStart)) /* prevent non-positive multipliers */
8374 : : #else
8375 : : if (p != q)
8376 : : #endif
8377 : : {
8378 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8379 : 0 : goto exit_function;
8380 : : }
8381 : : /* multiplier must not run past the last component */
8382 [ + + ]: 5 : if (iComponent + mpy_component > nNumComponents)
8383 : : {
8384 : 2 : ret = RI_ERR_SYNTAX; /* syntax error */
8385 : 2 : goto exit_function;
8386 : : }
8387 : 3 : p++;
8388 : : }
8389 : : else
8390 : : {
8391 : 14 : mpy_component = 1;
8392 : 14 : p = pStart;
8393 : : }
8394 : 17 : pStart = p;
8395 : : /* Pass 1.1 parse a component */
8396 : : /* djb-rwth: removing redundant code */
8397 : : /* djb-rwth: removing redundant code */
8398 : : /* djb-rwth: removing redundant code */
8399 : 17 : curAtom = 0; /* djb-rwth: ignoring LLVM warning: value used */
8400 : 17 : numCtAtoms = pInChI[iComponent].nNumberOfAtoms;
8401 [ - + - - ]: 17 : if (bMobileH == TAUT_NON && iComponent < pnNumComponents[nAltMobileH])
8402 : : {
8403 : 0 : numCtAtoms = pAltInChI[iComponent].nNumberOfAtoms;
8404 : : }
8405 : :
8406 [ + - - + ]: 17 : if (p < pEnd && *pbAbc == -1)
8407 : : {
8408 : : /* check if compressed InChI */
8409 [ # # # # ]: 0 : *pbAbc = (*p == ',' || isupper(UCINT* p)) ? 1 : 0;
8410 : : }
8411 [ - + ]: 17 : base = (*pbAbc == 1) ? ALPHA_BASE : 10;
8412 : :
8413 : : /* immobile H */
8414 [ - + ]: 17 : t = pTaut = (*pbAbc == 1) ? strchr(p, ',') : strchr(p, '('); /* locate the first tautomer group character */
8415 : :
8416 [ + + - + ]: 17 : if (t && bMobileH == TAUT_NON)
8417 : : {
8418 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8419 : 0 : goto exit_function;
8420 : : }
8421 [ + + - + ]: 17 : if (!pTaut || pTaut > pEnd)
8422 : : {
8423 : 15 : pTaut = pEnd;
8424 : 15 : t = NULL; /* found no tautomeric group for this component */
8425 : : }
8426 : : /* djb-rwth: fixing oss-fuzz issue #69489 */
8427 [ + + + - ]: 37 : for (i = 0; (i < mpy_component) && (iComponent + i < nNumComponents); i++)
8428 : : {
8429 [ - + ]: 20 : if (bMobileH == TAUT_NON)
8430 : : {
8431 : : /* allocate nNum_H_fixed */
8432 [ # # ]: 0 : if (pInChI[iComponent + i].nNum_H_fixed)
8433 : : {
8434 : 0 : ret = RI_ERR_PROGR; /* program error */
8435 : 0 : goto exit_function;
8436 : : }
8437 [ # # ]: 0 : if (iComponent + i < pnNumComponents[nAltMobileH])
8438 : : {
8439 : 0 : len = inchi_max(pInChI[iComponent + i].nNumberOfAtoms, pAltInChI[iComponent + i].nNumberOfAtoms) + 1;
8440 : : }
8441 : : else
8442 : : {
8443 : 0 : len = pInChI[iComponent + i].nNumberOfAtoms + 1;
8444 : : }
8445 : 0 : pInChI[iComponent + i].nNum_H_fixed = (S_CHAR*)inchi_calloc(len, sizeof(pInChI[0].nNum_H_fixed[0]));
8446 [ # # ]: 0 : if (!pInChI[iComponent + i].nNum_H_fixed)
8447 : : {
8448 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8449 : 0 : goto exit_function;
8450 : : }
8451 : : /* compare nAtom */
8452 [ # # ]: 0 : if (iComponent + i < pnNumComponents[nAltMobileH])
8453 : : {
8454 : 0 : len2 = inchi_min(pInChI[iComponent + i].nNumberOfAtoms, pAltInChI[iComponent + i].nNumberOfAtoms);
8455 [ # # # # ]: 0 : if (pInChI[iComponent + i].nAtom && len2)
8456 : : {
8457 : : /* check */
8458 [ # # ]: 0 : if (memcmp(pInChI[iComponent + i].nAtom, pAltInChI[iComponent + i].nAtom, len2 * sizeof(pInChI[0].nAtom[0])))
8459 : : {
8460 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8461 : 0 : goto exit_function;
8462 : : }
8463 : : }
8464 : : /* allocate and copy atom if bridging H are present */
8465 [ # # ]: 0 : if (pInChI[iComponent + i].nNumberOfAtoms < pAltInChI[iComponent + i].nNumberOfAtoms)
8466 : : {
8467 [ # # ]: 0 : if (pInChI[iComponent + i].nAtom)
8468 [ # # ]: 0 : inchi_free(pInChI[iComponent + i].nAtom);
8469 [ # # ]: 0 : if (!(pInChI[iComponent + i].nAtom = (U_CHAR*)inchi_calloc(len, sizeof(pInChI[0].nAtom[0]))))
8470 : : {
8471 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8472 : 0 : goto exit_function;
8473 : : }
8474 [ # # ]: 0 : if (len > 1)
8475 : : {
8476 : 0 : memcpy(pInChI[iComponent + i].nAtom, pAltInChI[iComponent + i].nAtom, ((long long)len - 1) * sizeof(pInChI[0].nAtom[0])); /* djb-rwth: cast operator added */
8477 : : }
8478 : : /* correct number of atoms including bridging H */
8479 : 0 : pInChI[iComponent + i].nNumberOfAtoms = pAltInChI[iComponent + i].nNumberOfAtoms;
8480 : : }
8481 : : }
8482 : : }
8483 : : }
8484 : :
8485 [ - + ]: 17 : if (*pbAbc == 1)
8486 : : {
8487 : : /* read numbers of H: XnYn... or XYn... */
8488 : 0 : p = pStart;
8489 : 0 : tg_alloc_len = 0;
8490 : 0 : num_H_component = num_taut_H_component = 0;
8491 [ # # ]: 0 : while (p < pTaut)
8492 : : {
8493 : : /* syntax check: atom number */
8494 [ # # # # ]: 0 : if (!*p || !isupper(UCINT* p))
8495 : : {
8496 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8497 : 0 : goto exit_function;
8498 : : }
8499 [ # # ]: 0 : if ((curAtom = nxtAtom = (int)inchi_strtol(p, &q, base))) /* djb-rwth: addressing LLVM warning */
8500 : : {
8501 : 0 : p = q;
8502 [ # # ]: 0 : if (isupper(UCINT* p))
8503 : : {
8504 : 0 : nxtAtom = (int)inchi_strtol(p, &q, base);
8505 : : #if ( CHECK_STRTOL_ATNUMB==1 )
8506 [ # # # # ]: 0 : if (nxtAtom > MAX_ATOMS || nxtAtom < 0)
8507 : : {
8508 : 0 : ret = RI_ERR_SYNTAX;
8509 : 0 : goto exit_function;
8510 : : }
8511 : : #endif
8512 : 0 : p = q;
8513 : : }
8514 : : }
8515 [ # # # # : 0 : if (curAtom <= 0 || curAtom > nxtAtom ||
# # ]
8516 [ # # ]: 0 : nxtAtom > numCtAtoms || p > pTaut)
8517 : : {
8518 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8519 : 0 : goto exit_function;
8520 : : }
8521 : : /* number of H, may be negative */
8522 [ # # # # ]: 0 : if (!(num_H = (int)inchi_strtol(p, &q, 10)) || q > pTaut)
8523 : : {
8524 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8525 : 0 : goto exit_function;
8526 : : }
8527 : 0 : p = q;
8528 : : /* set number of H */
8529 : : /* djb-rwth: fixing oss-fuzz issue #38399 */
8530 [ # # # # : 0 : if (((bMobileH == TAUT_YES) && pInChI[iComponent].nNum_H) || ((bMobileH != TAUT_YES) && pInChI[iComponent].nNum_H_fixed))
# # # # ]
8531 : : {
8532 [ # # ]: 0 : for (i = curAtom; i <= nxtAtom; i++)
8533 : : {
8534 [ # # ]: 0 : nNum_H(iComponent)[i - 1] = num_H;
8535 : 0 : num_H_component += num_H;
8536 : : }
8537 : : }
8538 : : }
8539 [ # # ]: 0 : if (p != pTaut)
8540 : : {
8541 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8542 : 0 : goto exit_function;
8543 : : }
8544 : : }
8545 : : else
8546 : : {
8547 : : /* read numbers of H: 1-2,3H2,4,5H3 */
8548 : 17 : p = pStart;
8549 : 17 : tg_alloc_len = 0;
8550 : 17 : num_H_component = num_taut_H_component = 0;
8551 : : #if ( FIX_GAF_2019_1==1 )
8552 : : {
8553 : : char invalid;
8554 : 17 : const char* str1 = str + 1;
8555 [ - + ]: 17 : if (bMobileH == TAUT_NON) /* FixedH layer "/h"*/
8556 : : {
8557 : 0 : invalid = str1[strspn(str1, "0123456789hDHT-,;()*")];
8558 : : }
8559 : : else /* Main layer (mobileH) "/h"*/
8560 : : {
8561 : 17 : invalid = str1[strspn(str1, "0123456789DHT-,;()*")];
8562 : : }
8563 [ - + ]: 17 : if (invalid != '\0')
8564 : : {
8565 : 0 : ret = RI_ERR_SYNTAX;
8566 : 0 : goto exit_function;
8567 : : }
8568 : : }
8569 : : #endif
8570 [ + + ]: 45 : while (p < pTaut)
8571 : : {
8572 : : /* syntax check: atom number */
8573 [ + - - + ]: 29 : if (!*p || !isdigit(UCINT* p))
8574 : : {
8575 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8576 : 0 : goto exit_function;
8577 : : }
8578 : : /* number of H */
8579 : 29 : h = p + strcspn(p, "Hh");
8580 : : /*h = strchr( p, 'H' );*/
8581 [ + - - + ]: 29 : if (!*h || h >= pTaut)
8582 : : {
8583 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8584 : 0 : goto exit_function;
8585 : : /*
8586 : : p = pTaut;
8587 : : h = NULL;
8588 : : break; */ /* no more H found */
8589 : : }
8590 [ - + - - ]: 29 : num_H = (*h == 'H') ? 1 : (*h == 'h') ? -1 : 0;
8591 [ - + ]: 29 : if (!num_H)
8592 : : {
8593 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8594 : 0 : goto exit_function;
8595 : : }
8596 [ + + + + ]: 29 : if (h[1] && isdigit(UCINT h[1]))
8597 : : {
8598 : 20 : num_H *= (int)inchi_strtol(h + 1, &p1, 10);
8599 : : }
8600 : : else
8601 : : {
8602 : 9 : p1 = h + 1; /* next set of immobile H */
8603 : : }
8604 [ + + ]: 29 : if (*p1 == ',')
8605 : : {
8606 : 14 : p1++; /* next H-subsegment; otherwise (H or ; or end of the segment */
8607 : : }
8608 : : /* list of atoms that have num_H */
8609 [ + + ]: 57 : while (p < h)
8610 : : {
8611 [ + - - + ]: 29 : if (!*p || !isdigit(UCINT* p))
8612 : : {
8613 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8614 : 0 : goto exit_function;
8615 : : }
8616 : 29 : nxtAtom = curAtom = (int)inchi_strtol(p, &q, 10);
8617 [ + + ]: 29 : if (*q == '-')
8618 : : {
8619 : 9 : nxtAtom = (int)inchi_strtol(q + 1, &q, 10);
8620 : : #if ( CHECK_STRTOL_ATNUMB==1 )
8621 [ + - - + ]: 9 : if (nxtAtom > MAX_ATOMS || nxtAtom < 0)
8622 : : {
8623 : 0 : ret = RI_ERR_SYNTAX;
8624 : 0 : goto exit_function;
8625 : : }
8626 : : #endif
8627 : : }
8628 : : /* consitency check */
8629 [ + + + - : 29 : if (curAtom <= 0 || curAtom > numCtAtoms ||
+ - ]
8630 [ - + ]: 28 : nxtAtom < curAtom || nxtAtom > numCtAtoms)
8631 : : {
8632 : 1 : ret = RI_ERR_SYNTAX; /* syntax error */
8633 : 1 : goto exit_function;
8634 : : }
8635 : : /* set number of H */
8636 : : /* djb-rwth: fixing oss-fuzz issue #38399 */
8637 [ + - - + : 28 : if (((bMobileH == TAUT_YES) && pInChI[iComponent].nNum_H) || ((bMobileH != TAUT_YES) && pInChI[iComponent].nNum_H_fixed))
- - - - ]
8638 : : {
8639 [ + + ]: 65 : for (i = curAtom; i <= nxtAtom; i++)
8640 : : {
8641 [ + - ]: 37 : nNum_H(iComponent)[i - 1] = num_H;
8642 : 37 : num_H_component += num_H;
8643 : : }
8644 : : }
8645 : : /* move to the next atom number if any */
8646 : 28 : p = q;
8647 [ - + ]: 28 : if (*p == ',')
8648 : : {
8649 : 0 : p++;
8650 : : }
8651 : : }
8652 : :
8653 [ + - ]: 28 : if (p == h)
8654 : : {
8655 : 28 : p = p1;
8656 : : }
8657 : : else
8658 : : {
8659 [ # # ]: 0 : if (p == pTaut)
8660 : : {
8661 : 0 : break;
8662 : : }
8663 : : else
8664 : : {
8665 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8666 : 0 : goto exit_function;
8667 : : }
8668 : : }
8669 : : }
8670 : : }
8671 : :
8672 : : INCHI_HEAPCHK
8673 : : /* ) -> (, H, N, [-, N,], AtNum,... AtNum) */
8674 : 16 : lenTautomer = 0;
8675 [ + + ]: 16 : if ((p = t)) /* djb-rwth: addressing LLVM warning */
8676 : : {
8677 [ - + ]: 2 : if (*pbAbc == 1)
8678 : : {
8679 : : /* tautomeric groups: pass 1 */
8680 : 0 : iTGroup = 0;
8681 : 0 : state = ')'; /* init as if the prev. t-group just ended */ /* djb-rwth: ignoring LLVM warning: value used */
8682 : 0 : num_Atoms = 0;
8683 : : /* Tautomeric info storage */
8684 : : /* NumGroups; ((NumAt+2, NumH, Num(-), At1..AtNumAt),...); {INCHI_T_NUM_MOVABLE = 2} */
8685 : : /* Allocated length: [5*nNumberOfAtoms/2+1], see Alloc_INChI(...) */
8686 [ # # ]: 0 : if (*p == ',')
8687 : : { /* start t-group */
8688 : 0 : p++;
8689 : : }
8690 : : else
8691 : : {
8692 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8693 : 0 : goto exit_function;
8694 : : }
8695 [ # # ]: 0 : while (p < pEnd)
8696 : : {
8697 : : /* start t-group */
8698 [ # # # # : 0 : if (!isdigit(UCINT* p) || !(num_H = (int)inchi_strtol(p, &q, 10)) || q > pEnd)
# # ]
8699 : : {
8700 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8701 : 0 : goto exit_function;
8702 : : }
8703 : 0 : p = q;
8704 : 0 : num_Minus = 0;
8705 [ # # ]: 0 : if (*p == '-')
8706 : : {
8707 : 0 : p++;
8708 [ # # ]: 0 : if (isdigit(UCINT* p))
8709 : : {
8710 : 0 : num_Minus = (int)inchi_strtol(p, &q, 10);
8711 : 0 : p = q;
8712 : : }
8713 : : else
8714 : : {
8715 : 0 : num_Minus = 1;
8716 : : }
8717 : : }
8718 [ # # ]: 0 : if (p >= pEnd)
8719 : : {
8720 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8721 : 0 : goto exit_function;
8722 : : }
8723 [ # # ]: 0 : if (!tg_alloc_len)
8724 : : {
8725 : : /*
8726 : : --- header ---
8727 : : [num_t_groups]
8728 : : --- one t-group: ---
8729 : : [len=group length no including this value]
8730 : : [num_H]
8731 : : [num_(-)]
8732 : : [Endpoint(1),...,Endpoint(len-2)]
8733 : : --- next t-group ---
8734 : : ...
8735 : :
8736 : : Max. size = 1 + 3*max_num_t_groups + max_num_endpoints
8737 : :
8738 : : max_num_t_groups = num_at/2
8739 : : max_num_endpoints = num_at
8740 : :
8741 : : Max. size = 1 + 3*(num_at/2) + num_at = 1 + (5*num_at)/2
8742 : : 5 = 3 + INCHI_T_NUM_MOVABLE = 3 + num_types_of_attachments
8743 : :
8744 : : This does not include zero termination!
8745 : :
8746 : : */
8747 : 0 : tg_alloc_len = ((3 + INCHI_T_NUM_MOVABLE) * pInChI[iComponent].nNumberOfAtoms) / 2 + 1;
8748 [ # # ]: 0 : for (i = 0; i < mpy_component; i++)
8749 : : {
8750 : 0 : pInChI[iComponent + i].nTautomer = (AT_NUMB*)inchi_calloc((long long)tg_alloc_len + 1, sizeof(pInChI->nTautomer[0])); /* djb-rwth: cast operator added */
8751 [ # # ]: 0 : if (!pInChI[iComponent + i].nTautomer)
8752 : : {
8753 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8754 : 0 : goto exit_function;
8755 : : }
8756 : 0 : pInChI[iComponent + i].lenTautomer = 0;
8757 : : }
8758 : 0 : tg_pos_Tautomer = 1; /* number atoms (NumAt+2) position */
8759 : : }
8760 : : else
8761 : : {
8762 : : /* next t-group */
8763 : 0 : tg_pos_Tautomer = lenTautomer;
8764 : : }
8765 [ # # ]: 0 : if (tg_pos_Tautomer + 3 >= tg_alloc_len)
8766 : : {
8767 : 0 : ret = RI_ERR_PROGR; /* wrong tautomer array length */
8768 : 0 : goto exit_function;
8769 : : }
8770 : 0 : pInChI[iComponent].nTautomer[tg_pos_Tautomer + 1] = num_H;
8771 : 0 : pInChI[iComponent].nTautomer[tg_pos_Tautomer + 2] = num_Minus;
8772 : 0 : lenTautomer = tg_pos_Tautomer + 3; /* first atom number position */
8773 : 0 : num_taut_H_component += num_H;
8774 : :
8775 [ # # # # ]: 0 : while (p < pEnd && isupper(UCINT* p))
8776 : : {
8777 : : /* read list of tautomeric atoms */
8778 : 0 : val = (int)inchi_strtol(p, &q, base);
8779 [ # # # # ]: 0 : if (lenTautomer >= tg_alloc_len || val > numCtAtoms)
8780 : : {
8781 : 0 : ret = RI_ERR_PROGR; /* wrong tautomer array length */
8782 : 0 : goto exit_function;
8783 : : }
8784 : 0 : num_Atoms++;
8785 : 0 : pInChI[iComponent].nTautomer[lenTautomer++] = val;
8786 : 0 : p = q;
8787 : : }
8788 [ # # # # : 0 : if (!num_Atoms || (p < pEnd && !isdigit(UCINT * p))) /* djb-rwth: addressing LLVM warning */
# # ]
8789 : : {
8790 : 0 : ret = RI_ERR_PROGR; /* wrong tautomer array length */
8791 : 0 : goto exit_function;
8792 : : }
8793 : 0 : iTGroup++;
8794 : 0 : pInChI[iComponent].nTautomer[tg_pos_Tautomer] = lenTautomer - tg_pos_Tautomer - 1; /* length of the rest of the t-group */
8795 : 0 : pInChI[iComponent].lenTautomer = lenTautomer;
8796 : : }
8797 [ # # # # ]: 0 : if (!iTGroup || p != pEnd)
8798 : : {
8799 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8800 : 0 : goto exit_function;
8801 : : }
8802 : 0 : pInChI[iComponent].nTautomer[0] = iTGroup;
8803 : : }
8804 : : else
8805 : : {
8806 : : /* tautomeric groups: pass 1 */
8807 : 2 : iTGroup = 0;
8808 : 2 : state = ')'; /* init as if the prev. t-group just ended */
8809 : 2 : num_Atoms = 0;
8810 : : /* Tautomeric info storage */
8811 : : /* NumGroups; ((NumAt+2, NumH, Num(-), At1..AtNumAt),...); {INCHI_T_NUM_MOVABLE = 2} */
8812 : : /* Allocated length: [5*nNumberOfAtoms/2+1], see Alloc_INChI(...) */
8813 [ + + ]: 16 : while (p < pEnd)
8814 : : {
8815 : : /* t-group */
8816 [ + + + - : 14 : switch (*p)
+ + ]
8817 : : {
8818 : 2 : case '(': /* start t-group */
8819 [ + - ]: 2 : switch (state)
8820 : : {
8821 : 2 : case ')':
8822 : 2 : state = *p++;
8823 : 2 : num_H = 0;
8824 : 2 : num_Minus = 0;
8825 : 2 : continue;
8826 : 0 : default:
8827 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8828 : 0 : goto exit_function;
8829 : : }
8830 : 2 : case ')': /* end t-group */
8831 [ + - ]: 2 : switch (state)
8832 : : {
8833 : 2 : case 'A': /* previuos was atom number */
8834 [ - + ]: 2 : if (!tg_alloc_len)
8835 : : {
8836 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8837 : 0 : goto exit_function;
8838 : : }
8839 : 2 : iTGroup++;
8840 : 2 : state = *p++;
8841 : 2 : pInChI[iComponent].nTautomer[tg_pos_Tautomer] = lenTautomer - tg_pos_Tautomer - 1; /* length of the rest of the t-group */
8842 : 2 : pInChI[iComponent].lenTautomer = lenTautomer;
8843 : 2 : continue;
8844 : 0 : default:
8845 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8846 : 0 : goto exit_function;
8847 : : }
8848 : 2 : case 'H': /* number of H */
8849 [ + - ]: 2 : switch (state)
8850 : : {
8851 : 2 : case '(':
8852 : 2 : state = *p++;
8853 : 2 : num_H = 1;
8854 : 2 : continue;
8855 : 0 : default:
8856 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8857 : 0 : goto exit_function;
8858 : : }
8859 : 0 : case '-': /* number of (-) */
8860 [ # # ]: 0 : switch (state)
8861 : : {
8862 : 0 : case 'N': /* previous was number of H */
8863 : : case 'H': /* previous was H */
8864 : 0 : state = *p++;
8865 : 0 : num_Minus = 1;
8866 : 0 : continue;
8867 : 0 : default:
8868 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8869 : 0 : goto exit_function;
8870 : : }
8871 : 4 : case ',':
8872 [ + + - ]: 4 : switch (state)
8873 : : {
8874 : 2 : case 'N': /* previous was number of H */
8875 : : case 'H': /* previous was H */
8876 : : case '-': /* previuos was - */
8877 : : case 'M': /* previous was number of (-) */
8878 : : /* the next must be the first tautomeric atom number; save num_H & num_Minus */
8879 [ - + - - ]: 2 : if (num_H <= 0 && num_Minus <= 0)
8880 : : {
8881 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8882 : 0 : goto exit_function;
8883 : : }
8884 [ + - ]: 2 : if (!tg_alloc_len)
8885 : : {
8886 : : /*
8887 : : --- header ---
8888 : : [num_t_groups]
8889 : : --- one t-group: ---
8890 : : [len=group length no including this value]
8891 : : [num_H]
8892 : : [num_(-)]
8893 : : [Endpoint(1),...,Endpoint(len-2)]
8894 : : --- next t-group ---
8895 : : ...
8896 : :
8897 : : Max. size = 1 + 3*max_num_t_groups + max_num_endpoints
8898 : :
8899 : : max_num_t_groups = num_at/2
8900 : : max_num_endpoints = num_at
8901 : :
8902 : : Max. size = 1 + 3*(num_at/2) + num_at = 1 + (5*num_at)/2
8903 : : 5 = 3 + INCHI_T_NUM_MOVABLE = 3 + num_types_of_attachments
8904 : :
8905 : : This does not include zero termination!
8906 : :
8907 : : */
8908 : 2 : tg_alloc_len = ((3 + INCHI_T_NUM_MOVABLE) * pInChI[iComponent].nNumberOfAtoms) / 2 + 1;
8909 [ + + ]: 5 : for (i = 0; i < mpy_component; i++)
8910 : : {
8911 : : /* djb-rwth: fixing oss-fuzz issue #68314 */
8912 : 3 : AT_NUMB* pinchi_icint = (AT_NUMB*)inchi_calloc((long long)tg_alloc_len + 1, sizeof(pInChI->nTautomer[0])); /* djb-rwth: cast operator added */
8913 [ - + ]: 3 : if (!pinchi_icint)
8914 : : {
8915 : 0 : ret = RI_ERR_ALLOC; /* allocation error */
8916 : 0 : goto exit_function;
8917 : : }
8918 : 3 : pInChI[iComponent + i].nTautomer = pinchi_icint;
8919 : 3 : pInChI[iComponent + i].lenTautomer = 0;
8920 : : }
8921 : 2 : tg_pos_Tautomer = 1; /* number atoms (NumAt+2) position */
8922 : : }
8923 : : else
8924 : : {
8925 : : /* next t-group */
8926 : 0 : tg_pos_Tautomer = lenTautomer;
8927 : : }
8928 [ - + ]: 2 : if (tg_pos_Tautomer + 3 >= tg_alloc_len)
8929 : : {
8930 : 0 : ret = RI_ERR_PROGR; /* wrong tautomer array length */
8931 : 0 : goto exit_function;
8932 : : }
8933 : 2 : pInChI[iComponent].nTautomer[tg_pos_Tautomer + 1] = num_H;
8934 : 2 : pInChI[iComponent].nTautomer[tg_pos_Tautomer + 2] = num_Minus;
8935 : 2 : lenTautomer = tg_pos_Tautomer + 3; /* first atom number position */
8936 : : /* djb-rwth: fixing GH issue #59.1 */
8937 : : if (num_H >= INT_MIN && num_H <= INT_MAX) /* djb-rwth: addressing coverity ID #499582 -- boundary check is required */
8938 : : {
8939 : 2 : num_taut_H_component += num_H;
8940 : : }
8941 : : else
8942 : : {
8943 : : ret = BNS_PROGRAM_ERR;
8944 : : goto exit_function;
8945 : : }
8946 : 2 : state = *p++;
8947 : 2 : continue;
8948 : 2 : case 'A':
8949 : : /* previuos was atom number */
8950 : 2 : state = *p++;
8951 : 2 : continue;
8952 : 0 : default:
8953 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8954 : 0 : goto exit_function;
8955 : : }
8956 : 4 : default:
8957 [ + - ]: 4 : if (isdigit(UCINT* p))
8958 : : {
8959 : 4 : val = (int)inchi_strtol(p, &q, 10);
8960 [ - + ]: 4 : if (val <= 0)
8961 : : {
8962 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8963 : 0 : goto exit_function;
8964 : : }
8965 : 4 : p = q;
8966 [ - - + - ]: 4 : switch (state)
8967 : : {
8968 : 0 : case 'H':
8969 : 0 : num_H = val;
8970 : 0 : state = 'N';
8971 : 0 : continue;
8972 : 0 : case '-':
8973 : 0 : num_Minus = val;
8974 : 0 : state = 'M';
8975 : 0 : continue;
8976 : 4 : case ',':
8977 [ + - - + ]: 4 : if (lenTautomer >= tg_alloc_len || val > numCtAtoms)
8978 : : {
8979 : 0 : ret = RI_ERR_PROGR; /* wrong tautomer array length */
8980 : 0 : goto exit_function;
8981 : : }
8982 : 4 : num_Atoms++;
8983 : 4 : pInChI[iComponent].nTautomer[lenTautomer++] = val;
8984 : 4 : state = 'A';
8985 : 4 : continue;
8986 : 0 : default:
8987 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8988 : 0 : goto exit_function;
8989 : : }
8990 : : }
8991 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8992 : 0 : goto exit_function;
8993 : : }
8994 : : }
8995 [ + - - + ]: 2 : if (!iTGroup || state != ')')
8996 : : {
8997 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
8998 : 0 : goto exit_function;
8999 : : }
9000 : 2 : pInChI[iComponent].nTautomer[0] = iTGroup;
9001 : : }
9002 : : }
9003 : : /* check num_H in components; for bMobileH=TAUT_NON, pInChI->nNum_H_fixed[] has not been added to pInChI->nNum_H[] yet */
9004 [ + - - + ]: 32 : if (0 > (ret2 = GetInChIFormulaNumH(pInChI + iComponent, &num_H_formula)) ||
9005 : 16 : 0 > (ret2 = GetInChINumH(pInChI + iComponent, &num_H_InChI)))
9006 : : {
9007 : 0 : ret = ret2;
9008 : 0 : goto exit_function;
9009 : : }
9010 [ - + - + ]: 16 : if (num_H_formula != num_H_InChI + (bMobileH == TAUT_NON ? num_H_component : 0))
9011 : : {
9012 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9013 : 0 : goto exit_function;
9014 : : }
9015 : :
9016 : : /* duplicate according to multiplication */
9017 [ + + ]: 19 : for (i = 1; i < mpy_component; i++)
9018 : : {
9019 : : #if ( FIX_GAF_2019_2==1 )
9020 [ + - - + ]: 3 : if ((iComponent + i > nNumComponents - 1) || (iComponent + i < 0))
9021 : : {
9022 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9023 : 0 : goto exit_function;
9024 : : }
9025 : :
9026 [ + - - + ]: 3 : if ((pInChI[iComponent + i].nNumberOfAtoms <= 0) || (pInChI[iComponent + i].nNumberOfAtoms > MAX_ATOMS))
9027 : : {
9028 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9029 : 0 : goto exit_function;
9030 : : }
9031 : : #endif
9032 : : /* djb-rwth: fixing oss-fuzz issue #69699 */
9033 [ + - + - ]: 3 : if (nNum_H(iComponent)) /* djb-rwth: fixing GH issues #27/#28 */
9034 : : {
9035 [ + - + - ]: 3 : memcpy(nNum_H(iComponent + i), nNum_H(iComponent), pInChI[iComponent + i].nNumberOfAtoms * sizeof(nNum_H(0)[0]));
9036 : : }
9037 : : /*
9038 : : memcpy( pInChI[iComponent+i].nNum_H, pInChI[iComponent].nNum_H,
9039 : : pInChI[iComponent+i].nNumberOfAtoms * sizeof(pInChI[0].nNum_H[0]) );
9040 : : */
9041 [ + + + - : 3 : if (pInChI[iComponent + i].nTautomer && pInChI[iComponent].nTautomer && pInChI[iComponent].lenTautomer)
+ - ]
9042 : : {
9043 : 1 : memcpy(pInChI[iComponent + i].nTautomer, pInChI[iComponent].nTautomer,
9044 : 1 : pInChI[iComponent].lenTautomer * sizeof(pInChI[0].nTautomer[0]));
9045 : 1 : pInChI[iComponent + i].lenTautomer = pInChI[iComponent].lenTautomer;
9046 : : }
9047 : : /* check num_H in components */
9048 [ + - - + ]: 6 : if (0 > (ret2 = GetInChIFormulaNumH(pInChI + iComponent + i, &num_H_formula)) ||
9049 : 3 : 0 > (ret2 = GetInChINumH(pInChI + iComponent + i, &num_H_InChI)))
9050 : : {
9051 : 0 : ret = ret2;
9052 : 0 : goto exit_function;
9053 : : }
9054 [ - + - + ]: 3 : if (num_H_formula != num_H_InChI + (bMobileH == TAUT_NON ? num_H_component : 0))
9055 : : {
9056 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9057 : 0 : goto exit_function;
9058 : : }
9059 : : }
9060 : :
9061 : : /* prepare for the next component */
9062 : 16 : iComponent += i;
9063 [ - + ]: 16 : if (*pEnd)
9064 : : {
9065 : : #if (FIX_DALKE_BUGS == 1)
9066 : : /* prevent crash on extra trailing ';' */
9067 [ # # ]: 0 : if (iComponent >= nNumComponents)
9068 : : {
9069 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: extra component */
9070 : 0 : goto exit_function;
9071 : : }
9072 : : #endif
9073 : 0 : pStart = pEnd + 1;
9074 : : }
9075 : : else
9076 : : {
9077 : 16 : break;
9078 : : }
9079 : : }
9080 : :
9081 [ - + ]: 16 : if (nNumComponents != iComponent)
9082 : : {
9083 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9084 : 0 : goto exit_function;
9085 : : }
9086 : 16 : ret = iComponent + 1;
9087 : :
9088 : 19 : exit_function:
9089 : : INCHI_HEAPCHK
9090 : :
9091 : 19 : return ret;
9092 : : }
9093 : :
9094 : :
9095 : : /****************************************************************************
9096 : : Parse "/c" InChI layer
9097 : : ****************************************************************************/
9098 : 22 : int ParseSegmentConnections(const char* str,
9099 : : int bMobileH,
9100 : : INChI** pInpInChI,
9101 : : int* pnNumComponents,
9102 : : int* pbAbc,
9103 : : int* nb_total)
9104 : : {
9105 : : #define LAST_AT_LEN 256
9106 : : /* Pass 1: count bonds and find actual numbers of atom */
9107 : : int i, j, k, m, c, mpy_component;
9108 : : int nNumComponents, iComponent, nNumAtoms, nNumBonds, lenConnTable; /* djb-rwth: removing redundant variables */
9109 : : const char* p, * q, * pStart, * pEnd;
9110 : : AT_NUMB last_atom[LAST_AT_LEN], curAtom, maxAtom;
9111 : : int num_open, state, ret, base;
9112 : 22 : INChI* pInChI = *pInpInChI;
9113 : : LINKED_BONDS LB;
9114 : 22 : LINKED_BONDS* pLB = &LB; /* a list of linked lists of bonds, for each atom */
9115 : : AT_NUMB neighbor[MAXVAL];
9116 : 22 : int bPrevVersion = -1;
9117 : :
9118 : 22 : *nb_total = 0;
9119 : 22 : iComponent = 0;
9120 : 22 : LB.pBond = NULL; /* djb-rwth: initialization required to avoid garbage values */
9121 [ + + ]: 22 : if (str[0] != 'c')
9122 : : {
9123 [ - + - - ]: 10 : if (!pInChI && !*pnNumComponents)
9124 : 0 : {
9125 : 0 : int lenFormula = 1;
9126 : : /* component has no formula; allocate InChI */
9127 : 0 : lenConnTable = 0;
9128 : 0 : nNumComponents = 1;
9129 : : /* allocate InChI */
9130 [ # # ]: 0 : if (!(pInChI = *pInpInChI = (INChI*)inchi_calloc(nNumComponents, sizeof(INChI))))
9131 : : {
9132 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9133 : : }
9134 : : /* allocate empty formula */
9135 : 0 : pInChI[iComponent].szHillFormula = (char*)inchi_calloc((long long)lenFormula + 1, sizeof(pInChI[0].szHillFormula[0])); /* djb-rwth: cast operator added */
9136 [ # # ]: 0 : if (!pInChI[iComponent].szHillFormula)
9137 : : {
9138 : 0 : ret = RI_ERR_ALLOC; /* allocation failure */
9139 : 0 : goto exit_function;
9140 : : }
9141 : : /* allocate empty connection table */
9142 : 0 : pInChI[iComponent].nConnTable = (AT_NUMB*)inchi_calloc((long long)lenConnTable + 1, sizeof(pInChI[0].nConnTable[0])); /* djb-rwth: cast operator added */
9143 [ # # ]: 0 : if (!pInChI[iComponent].nConnTable)
9144 : : {
9145 : 0 : ret = RI_ERR_ALLOC; /* allocation failure */
9146 : 0 : goto exit_function;
9147 : : }
9148 : 0 : pInChI[iComponent].lenConnTable = lenConnTable;
9149 : 0 : *pnNumComponents = nNumComponents;
9150 : : }
9151 : : else
9152 : : {
9153 : 10 : lenConnTable = 1;
9154 : 10 : nNumComponents = *pnNumComponents;
9155 [ + + ]: 20 : for (i = 0; i < nNumComponents; i++)
9156 : : {
9157 : : /* allocate 1 atom connection table */
9158 [ + - ]: 10 : if (pInChI) /* djb-rwth: fixing a NULL pointer dereference */
9159 : : {
9160 [ - + ]: 10 : if (pInChI[i].nConnTable)
9161 : : {
9162 [ # # ]: 0 : inchi_free(pInChI[i].nConnTable);
9163 : : }
9164 : 10 : pInChI[i].nConnTable = (AT_NUMB*)inchi_calloc((long long)lenConnTable + 1, sizeof(pInChI[0].nConnTable[0])); /* djb-rwth: cast operator added */
9165 [ - + ]: 10 : if (!pInChI[i].nConnTable)
9166 : : {
9167 : 0 : ret = RI_ERR_ALLOC; /* allocation failure */
9168 : 0 : goto exit_function;
9169 : : }
9170 : 10 : pInChI[i].nConnTable[0] = 1;
9171 : 10 : pInChI[i].lenConnTable = lenConnTable;
9172 : : }
9173 : : }
9174 : : }
9175 : 10 : return 0;
9176 : : }
9177 : :
9178 : : /* Pass 1. Re-Count atoms, count bonds */
9179 : :
9180 : 12 : pStart = (char*)str + 1;
9181 : 12 : nNumComponents = *pnNumComponents;
9182 : : #if (FIX_DALKE_BUGS == 1)
9183 : : /* prevent crash on too many components */
9184 [ - + ]: 12 : if (nNumComponents > MAX_ATOMS)
9185 : : {
9186 : 0 : ret = RI_ERR_SYNTAX; /* syntax error: extra component */
9187 : 0 : goto exit_function;
9188 : : }
9189 : : #endif
9190 : 12 : memset(pLB, 0, sizeof(pLB[0])); /* djb-rwth: memset_s C11/Annex K variant? */
9191 : :
9192 : : while (1)
9193 : : {
9194 : : /* cycle over components */
9195 [ + - ]: 12 : if (!(pEnd = strchr(pStart, ';')))
9196 : : {
9197 : 12 : pEnd = pStart + strlen(pStart);
9198 : : }
9199 [ + + + - ]: 12 : if ((p = strchr(pStart, '*')) && p < pEnd)
9200 : : {
9201 : 3 : mpy_component = (int)inchi_strtol(pStart, &q, 10);
9202 : : #if ( CHECK_STRTOL_ATNUMB==1 )
9203 [ + - - + ]: 3 : if (mpy_component > MAX_ATOMS || mpy_component < 0)
9204 : : {
9205 : 0 : ret = RI_ERR_SYNTAX;
9206 : 0 : goto exit_function;
9207 : : }
9208 : : #endif
9209 [ + - ]: 3 : if (p != q
9210 : : #if (FIX_DALKE_BUGS == 1)
9211 [ - + ]: 3 : || !isdigit(UCINT * pStart)
9212 : : #endif
9213 : : )
9214 : : {
9215 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9216 : 0 : goto exit_function;
9217 : : }
9218 : 3 : p++;
9219 : : }
9220 : : else
9221 : : {
9222 : 9 : mpy_component = 1;
9223 : 9 : p = pStart;
9224 : : }
9225 : : #if (FIX_DALKE_BUGS == 1)
9226 [ - + ]: 12 : if (iComponent + mpy_component > MAX_ATOMS)
9227 : : {
9228 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9229 : 0 : goto exit_function;
9230 : : }
9231 : : #endif
9232 : 12 : pStart = p;
9233 : : /* Pass 1.1 parse a component */
9234 : 12 : num_open = 0;
9235 : 12 : memset(last_atom, 0, sizeof(last_atom)); /* djb-rwth: memset_s C11/Annex K variant? */
9236 : 12 : state = '\0'; /* initial state */
9237 : 12 : maxAtom = 0;
9238 : 12 : nNumBonds = 0;
9239 : : /* djb-rwth: removing redundant code */
9240 [ + - - + ]: 12 : if (p < pEnd && *pbAbc == -1)
9241 : : {
9242 : : /* check if compressed InChI */
9243 : 0 : *pbAbc = isupper(UCINT * p) ? 1 : 0;
9244 : : }
9245 [ - + ]: 12 : base = *pbAbc ? ALPHA_BASE : 10;
9246 : :
9247 [ - + ]: 12 : if (*pbAbc == 1)
9248 : : {
9249 : 0 : nNumAtoms = 1;
9250 [ # # ]: 0 : while (p < pEnd)
9251 : : {
9252 [ # # ]: 0 : if (*p == '-')
9253 : : {
9254 [ # # ]: 0 : if (bPrevVersion == -1)
9255 : : {
9256 : : /* previous InChI version */
9257 : 0 : bPrevVersion = 1;
9258 : : }
9259 : : else
9260 [ # # ]: 0 : if (bPrevVersion != 1)
9261 : : {
9262 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9263 : 0 : goto exit_function;
9264 : : }
9265 : 0 : nNumAtoms--;
9266 : 0 : p++;
9267 : : }
9268 [ # # ]: 0 : if (isdigit(UCINT * p))
9269 : : {
9270 [ # # ]: 0 : if (bPrevVersion == -1)
9271 : : {
9272 : : /* curreny InChI, version 1 */
9273 : 0 : bPrevVersion = 0;
9274 : : }
9275 : : else
9276 [ # # ]: 0 : if (bPrevVersion != 0)
9277 : : {
9278 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9279 : 0 : goto exit_function;
9280 : : }
9281 : 0 : nNumAtoms -= inchi_strtol(p, &p, 10); /* bypass digits */
9282 : : }
9283 [ # # # # ]: 0 : if (*p != '-' && (curAtom = (AT_NUMB)inchi_strtol(p, &q, base)))
9284 : : {
9285 : 0 : nNumAtoms++;
9286 : 0 : nNumBonds++;
9287 : 0 : p = q;
9288 [ # # ]: 0 : if (maxAtom < curAtom)
9289 : 0 : maxAtom = curAtom;
9290 : : }
9291 : : else
9292 : : {
9293 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9294 : 0 : goto exit_function;
9295 : : }
9296 : : }
9297 [ # # # # ]: 0 : if (maxAtom < nNumAtoms && nNumBonds)
9298 : : {
9299 : 0 : maxAtom = nNumAtoms;
9300 : : }
9301 : : }
9302 : : else
9303 : : {
9304 [ + + ]: 98 : while (p < pEnd)
9305 : : {
9306 : : /* atom number */
9307 : 86 : c = UCINT * p++;
9308 [ + + ]: 86 : switch (c)
9309 : : {
9310 : 37 : case '(':
9311 : : case ')':
9312 : : case ',':
9313 : : case '-':
9314 [ - + ]: 37 : if (state != 'N')
9315 : : {
9316 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9317 : 0 : goto exit_function;
9318 : : }
9319 : 37 : state = c;
9320 : 37 : num_open += (c == '(') - (c == ')');
9321 [ - + ]: 37 : if (num_open < 0)
9322 : : {
9323 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9324 : 0 : goto exit_function;
9325 : : }
9326 : 37 : break;
9327 : 49 : default:
9328 [ + - + - ]: 49 : if (isdigit(c) && (curAtom = (AT_NUMB)inchi_strtol(p - 1, &q, 10)))
9329 : : {
9330 : 49 : p = q;
9331 [ + + - ]: 49 : switch (state)
9332 : : {
9333 : 37 : case '(':
9334 : : case ')':
9335 : : case ',':
9336 : : case '-':
9337 : 37 : nNumBonds++;
9338 : 49 : case '\0':
9339 [ + + ]: 49 : if (maxAtom < curAtom)
9340 : 42 : maxAtom = curAtom;
9341 : 49 : state = 'N';
9342 : 49 : break;
9343 : 0 : default:
9344 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9345 : 0 : goto exit_function;
9346 : : }
9347 : : }
9348 : : else
9349 : : {
9350 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9351 : 0 : goto exit_function;
9352 : : }
9353 : 49 : break;
9354 : : }
9355 : : }
9356 [ - + ]: 12 : if (num_open)
9357 : : {
9358 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9359 : 0 : goto exit_function;
9360 : : /* syntax error: parentheses do not match */
9361 : : }
9362 : : }
9363 : 12 : (*nb_total) += nNumBonds;
9364 : :
9365 : : /* Save the results and allocate memory */
9366 : 12 : nNumAtoms = (int)maxAtom; /* 0 if empty connection table and no bonds present */
9367 : 12 : lenConnTable = nNumAtoms + nNumBonds;
9368 : :
9369 : : /* connection table format: At1[,Neigh11,Neigh12,...],At2[,Neigh21,Neigh22,...],AtN[NeighN1,NeighN2,...] */
9370 : : /* where AtK > NeighK1 > NeighK2,...; At(K) < At(K+1); the length = num.atoms + num.bonds */
9371 [ + + ]: 27 : for (i = 0; i < mpy_component; i++)
9372 : : {
9373 : : #if ( FIX_GAF_2019_2==1 )
9374 [ + - - + ]: 15 : if ((iComponent + i > nNumComponents - 1) || (iComponent + i < 0))
9375 : : {
9376 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9377 : 0 : goto exit_function;
9378 : : }
9379 [ + - - + ]: 15 : if (pInChI[iComponent + i].nNumberOfAtoms <= 0 || pInChI[iComponent + i].nNumberOfAtoms > MAX_ATOMS)
9380 : : {
9381 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9382 : 0 : goto exit_function;
9383 : : }
9384 : : #endif
9385 : : /* check number of atoms: the difference may be due to bridging H */
9386 [ - + ]: 15 : if ((j = pInChI[iComponent + i].nNumberOfAtoms) < nNumAtoms)
9387 : : {
9388 : : /* reallocate */
9389 : 0 : U_CHAR* nAtomTmp = (U_CHAR*)inchi_malloc((long long)nNumAtoms + 1); /* djb-rwth: cast operator added */
9390 [ # # ]: 0 : if (!nAtomTmp)
9391 : : {
9392 : 0 : ret = RI_ERR_ALLOC; /* allocation failure */
9393 : 0 : goto exit_function;
9394 : : }
9395 : 0 : memcpy(nAtomTmp, pInChI[iComponent + i].nAtom, sizeof(nAtomTmp[0]) * j);
9396 [ # # ]: 0 : while (j < nNumAtoms)
9397 : : {
9398 : 0 : nAtomTmp[j++] = EL_NUMBER_H; /* bridging H */
9399 : : }
9400 : 0 : nAtomTmp[j] = '\0';
9401 : : INCHI_HEAPCHK
9402 [ # # ]: 0 : if (pInChI[iComponent + i].nAtom)
9403 : : {
9404 [ # # ]: 0 : inchi_free(pInChI[iComponent + i].nAtom);
9405 : : }
9406 : 0 : pInChI[iComponent + i].nAtom = nAtomTmp;
9407 : 0 : pInChI[iComponent + i].nNumberOfAtoms = nNumAtoms;
9408 : : }
9409 : : else
9410 : : {
9411 [ - + - - : 15 : if (j > nNumAtoms && (lenConnTable || j != 1))
- - ]
9412 : : {
9413 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9414 : 0 : goto exit_function;
9415 : : }
9416 : : }
9417 : : /* allocate connection table */
9418 [ - + ]: 15 : if (pInChI[iComponent + i].nConnTable)
9419 : : {
9420 [ # # ]: 0 : inchi_free(pInChI[iComponent + i].nConnTable);
9421 : : }
9422 [ - + - - : 15 : if (!nNumAtoms && !nNumBonds && !lenConnTable)
- - ]
9423 : : {
9424 : 0 : lenConnTable = 1; /* one atom, no bonds */
9425 : : }
9426 : 15 : pInChI[iComponent + i].nConnTable = (AT_NUMB*)inchi_calloc((long long)lenConnTable + 1, sizeof(pInChI[0].nConnTable[0])); /* djb-rwth: cast operator added */
9427 [ - + ]: 15 : if (!pInChI[iComponent + i].nConnTable)
9428 : : {
9429 : 0 : ret = RI_ERR_ALLOC; /* allocation failure */
9430 : 0 : goto exit_function;
9431 : : }
9432 : 15 : pInChI[iComponent + i].lenConnTable = lenConnTable;
9433 : : }
9434 : :
9435 : : /* Pass 1.2 parse a component and extract the bonds */
9436 : 12 : num_open = 0;
9437 : 12 : memset(last_atom, 0, sizeof(last_atom)); /* djb-rwth: memset_s C11/Annex K variant? */
9438 : 12 : state = '\0'; /* initial state */
9439 : : /* djb-rwth: removing redundant code */
9440 : 12 : p = pStart;
9441 : 12 : pLB->len = 0;
9442 : :
9443 [ - + ]: 12 : if (*pbAbc == 1)
9444 : : {
9445 : : /* compressed */
9446 : : int num_neigh;
9447 : 0 : num_open = 0;
9448 : 0 : last_atom[num_open] = 2;
9449 [ # # ]: 0 : while (p < pEnd)
9450 : : {
9451 [ # # ]: 0 : if (last_atom[num_open] > maxAtom)
9452 : : {
9453 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9454 : 0 : goto exit_function;
9455 : : }
9456 [ # # ]: 0 : if (isupper(UCINT * p))
9457 : : {
9458 : 0 : curAtom = (AT_NUMB)inchi_strtol(p, &q, base);
9459 [ # # ]: 0 : if ((ret = AddLinkedBond(last_atom[num_open], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9460 : : {
9461 : 0 : goto exit_function;
9462 : : }
9463 : 0 : p = q;
9464 [ # # ]: 0 : if (bPrevVersion == 1)
9465 : : {
9466 [ # # # # ]: 0 : while (p < pEnd && *p == '-')
9467 : : {
9468 : 0 : p++;
9469 [ # # ]: 0 : if ((curAtom = (AT_NUMB)inchi_strtol(p, &q, base))) /* djb-rwth: addressing LLVM warning */
9470 : : {
9471 [ # # ]: 0 : if ((ret = AddLinkedBond(last_atom[num_open], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9472 : : {
9473 : 0 : goto exit_function;
9474 : : }
9475 : 0 : p = q;
9476 : : }
9477 : : else
9478 : : {
9479 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9480 : 0 : goto exit_function;
9481 : : }
9482 : : }
9483 : : }
9484 : : else
9485 : : {
9486 [ # # # # ]: 0 : if (bPrevVersion == 0 && isdigit(*p))
9487 : : {
9488 : 0 : num_neigh = (int)inchi_strtol(p, &q, 10);
9489 : 0 : p = q;
9490 [ # # # # ]: 0 : while (num_neigh-- && p < pEnd)
9491 : : {
9492 [ # # ]: 0 : if ((curAtom = (AT_NUMB)inchi_strtol(p, &q, base))) /* djb-rwth: addressing LLVM warning */
9493 : : {
9494 [ # # ]: 0 : if ((ret = AddLinkedBond(last_atom[num_open], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9495 : : {
9496 : 0 : goto exit_function;
9497 : : }
9498 : 0 : p = q;
9499 : : }
9500 : : else
9501 : : {
9502 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9503 : 0 : goto exit_function;
9504 : : }
9505 : : }
9506 : : }
9507 : : }
9508 : 0 : last_atom[num_open]++;
9509 : : }
9510 : : else
9511 : : {
9512 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9513 : 0 : goto exit_function;
9514 : : }
9515 : : }
9516 : : }
9517 : : else
9518 : : {
9519 [ + + ]: 98 : while (p < pEnd)
9520 : : {
9521 : : /* each atom number except the first means a new bond */
9522 : 86 : c = UCINT * p++;
9523 [ + + ]: 86 : switch (c)
9524 : : {
9525 : 37 : case '(':
9526 : : case ')':
9527 : : case ',':
9528 : : case '-':
9529 [ + - ]: 37 : switch (state)
9530 : : {
9531 : 37 : case 'N':
9532 : 37 : state = c;
9533 : 37 : break;
9534 : 0 : default:
9535 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9536 : 0 : goto exit_function;
9537 : : }
9538 : 37 : break;
9539 : 49 : default:
9540 [ + - + - ]: 49 : if (isdigit(c) && (curAtom = (AT_NUMB)inchi_strtol(p - 1, &q, 10)))
9541 : : {
9542 : 49 : p = q;
9543 [ + + + - : 49 : switch (state)
+ - ]
9544 : : {
9545 : 12 : case '\0':
9546 : 12 : last_atom[num_open] = curAtom;
9547 : 12 : state = 'N';
9548 : 12 : break;
9549 : 6 : case '(':
9550 [ - + ]: 6 : if ((ret = AddLinkedBond(last_atom[num_open], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9551 : : {
9552 : 0 : goto exit_function;
9553 : : }
9554 [ - + ]: 6 : if (++num_open >= LAST_AT_LEN)
9555 : : {
9556 : 0 : ret = RI_ERR_PROGR; /* program error: buffer overflow */
9557 : 0 : goto exit_function;
9558 : : }
9559 : 6 : last_atom[num_open] = curAtom;
9560 : 6 : state = 'N';
9561 : 6 : break;
9562 : :
9563 : 6 : case ')':
9564 [ - + ]: 6 : if (!num_open)
9565 : : {
9566 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9567 : 0 : goto exit_function;
9568 : : }
9569 [ - + ]: 6 : if ((ret = AddLinkedBond(last_atom[--num_open], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9570 : : {
9571 : 0 : goto exit_function;
9572 : : }
9573 : 6 : last_atom[num_open] = curAtom;
9574 : 6 : state = 'N';
9575 : 6 : break;
9576 : :
9577 : 0 : case ',':
9578 [ # # ]: 0 : if (!num_open)
9579 : : {
9580 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9581 : 0 : goto exit_function;
9582 : : }
9583 [ # # ]: 0 : if ((ret = AddLinkedBond(last_atom[num_open - 1], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9584 : : {
9585 : 0 : goto exit_function;
9586 : : }
9587 : 0 : last_atom[num_open] = curAtom;
9588 : 0 : state = 'N';
9589 : 0 : break;
9590 : 25 : case '-':
9591 [ - + ]: 25 : if ((ret = AddLinkedBond(last_atom[num_open], curAtom, (AT_NUMB)nNumAtoms, pLB))) /* djb-rwth: addressing LLVM warning */
9592 : : {
9593 : 0 : goto exit_function;
9594 : : }
9595 : 25 : last_atom[num_open] = curAtom;
9596 : 25 : state = 'N';
9597 : 25 : break;
9598 : 0 : default:
9599 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9600 : 0 : goto exit_function;
9601 : : }
9602 : : }
9603 : : else
9604 : : {
9605 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9606 : 0 : goto exit_function;
9607 : : }
9608 : 49 : break;
9609 : : }
9610 : : }
9611 : : }
9612 : :
9613 : : /* Store the bonds in connection table */
9614 [ + - ]: 12 : if (lenConnTable > 1)
9615 : : {
9616 [ + + ]: 61 : for (i = 0, m = 0; i < nNumAtoms; i++)
9617 : : {
9618 : 49 : k = 0;
9619 : :
9620 [ - + ]: 49 : if (!pLB->pBond)
9621 : : {
9622 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9623 : 0 : goto exit_function;
9624 : : }
9625 : :
9626 [ + - ]: 49 : if ((j = pLB->pBond[i + 1].prev)) /* djb-rwth: addressing LLVM warning */
9627 : : {
9628 [ + - ]: 74 : while (k < MAXVAL)
9629 : : {
9630 : 74 : neighbor[k++] = pLB->pBond[j].neigh;
9631 [ + + ]: 74 : if (j == i + 1)
9632 : 49 : break;
9633 : 25 : j = pLB->pBond[j].prev;
9634 : : }
9635 : : }
9636 [ - + ]: 49 : if (j != i + 1)
9637 : : {
9638 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9639 : 0 : goto exit_function;
9640 : : }
9641 : :
9642 : : /* sort the neighbors */
9643 : :
9644 : 49 : insertions_sort_AT_NUMB(neighbor, k);
9645 : :
9646 [ - + ]: 49 : if (m == pInChI[iComponent].lenConnTable)
9647 : : {
9648 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9649 : 0 : goto exit_function;
9650 : : }
9651 : :
9652 : : #if ( FIX_GAF_2020_25726==1 )
9653 [ + + ]: 74 : for (j = 1; j < k; j++)
9654 : : {
9655 [ - + ]: 25 : if (neighbor[j] == neighbor[j - 1])
9656 : : {
9657 : 0 : ret = RI_ERR_SYNTAX; /* syntax error - same nbr twice, i.e. multiple bond between same atoms */
9658 : 0 : goto exit_function;
9659 : : }
9660 : : }
9661 : : #endif
9662 : 49 : pInChI[iComponent].nConnTable[m++] = i + 1; /* atom number */
9663 [ + + + + ]: 86 : for (j = 0; j < k && (int)neighbor[j] <= i; j++)
9664 : : {
9665 [ - + ]: 37 : if (m == pInChI[iComponent].lenConnTable)
9666 : : {
9667 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
9668 : 0 : goto exit_function;
9669 : : }
9670 : 37 : pInChI[iComponent].nConnTable[m++] = neighbor[j];
9671 : : }
9672 : : }
9673 [ - + ]: 12 : if (m != lenConnTable)
9674 : : {
9675 : 0 : ret = RI_ERR_PROGR; /* program error */
9676 : 0 : goto exit_function;
9677 : : }
9678 : : }
9679 : : else
9680 : : {
9681 : 0 : pInChI[iComponent].nConnTable[0] = 1; /* single atom */
9682 : : }
9683 : :
9684 : : /* Duplicate if needed */
9685 [ + + ]: 15 : for (i = 1; i < mpy_component; i++)
9686 : : {
9687 : : /*
9688 : : if ( pInChI[iComponent+i].nConnTable ) {
9689 : : inchi_free( pInChI[iComponent+i].nConnTable );
9690 : : }
9691 : : pInChI[iComponent+i].nConnTable = (AT_NUMB *)inchi_calloc( lenConnTable+1, sizeof(pInChI[0].nConnTable[0]) );
9692 : : if ( !pInChI[iComponent+i].nConnTable ) {
9693 : : ret = RI_ERR_ALLOC;
9694 : : goto exit_function;
9695 : : }
9696 : : */
9697 [ + - - + ]: 3 : if (!pInChI[iComponent + i].nConnTable || pInChI[iComponent + i].lenConnTable != lenConnTable)
9698 : : {
9699 : 0 : ret = RI_ERR_PROGR;
9700 : 0 : goto exit_function;
9701 : : }
9702 : 3 : memcpy(pInChI[iComponent + i].nConnTable, pInChI[iComponent].nConnTable, lenConnTable * sizeof(pInChI[0].nConnTable[0]));
9703 : : }
9704 : : /* prepare for the next connection table */
9705 : 12 : iComponent += i;
9706 [ - + ]: 12 : if (*pEnd)
9707 : 0 : pStart = pEnd + 1;
9708 : : else
9709 : 12 : break;
9710 : : /* We must check if we have already read as many components as we have in the fragment. */
9711 : : /* If yes, then we break, because anything else that might follow cannot be useful information. */
9712 : : /* There are files with a trailing ";" that would cause a problem (memory allocation bug) if we did not do this. */
9713 [ # # ]: 0 : if (iComponent == nNumComponents)
9714 : 0 : break;
9715 : : }
9716 : 12 : ret = iComponent;
9717 : :
9718 : 12 : exit_function:
9719 : :
9720 [ + - ]: 12 : if (pLB->pBond)
9721 : : {
9722 : : INCHI_HEAPCHK
9723 [ + - ]: 12 : inchi_free(pLB->pBond);
9724 : : }
9725 : :
9726 : 12 : return ret;
9727 : : #undef LAST_AT_LEN
9728 : : }
9729 : :
9730 : :
9731 : : /****************************************************************************/
9732 : 3 : int nFillOutProtonMobileH(INChI* pInChI)
9733 : : {
9734 : 3 : int len = 1;
9735 : 3 : pInChI->bDeleted = 1;
9736 : : /* formula */
9737 [ + - ]: 3 : if (!pInChI->szHillFormula &&
9738 [ - + ]: 3 : !(pInChI->szHillFormula = (char*)inchi_calloc((long long)len + 1, sizeof(pInChI->szHillFormula[0])))) /* djb-rwth: cast operator added */
9739 : : {
9740 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9741 : : }
9742 : 3 : strcpy(pInChI->szHillFormula, "H");
9743 : 3 : pInChI->nNumberOfAtoms = 1;
9744 : :
9745 : : /* atoms */
9746 [ + - ]: 3 : if (!pInChI->nAtom &&
9747 [ - + ]: 3 : !(pInChI->nAtom = (U_CHAR*)inchi_calloc((long long)len + 1, sizeof(pInChI->nAtom[0])))) /* djb-rwth: cast operator added */
9748 : : {
9749 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9750 : : }
9751 : 3 : pInChI->nAtom[0] = 1;
9752 : : /* charge */
9753 : 3 : pInChI->nTotalCharge = 1;
9754 : : /* connection table */
9755 [ + - ]: 3 : if (!pInChI->nConnTable &&
9756 [ - + ]: 3 : !(pInChI->nConnTable = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pInChI->nConnTable[0])))) /* djb-rwth: cast operator added */
9757 : : {
9758 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9759 : : }
9760 : 3 : pInChI->nConnTable[0] = 1;
9761 : 3 : pInChI->lenConnTable = len;
9762 : : /* tautomer */
9763 [ + - ]: 3 : if (!pInChI->nTautomer &&
9764 [ - + ]: 3 : !(pInChI->nTautomer = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pInChI->nTautomer[0])))) /* djb-rwth: cast operator added */
9765 : : {
9766 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9767 : : }
9768 : : /* nNum_H */
9769 [ + - ]: 3 : if (!pInChI->nNum_H &&
9770 [ - + ]: 3 : !(pInChI->nNum_H = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pInChI->nNum_H[0])))) /* djb-rwth: cast operator added */
9771 : : {
9772 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9773 : : }
9774 : 3 : pInChI->nNum_H[0] = 0;
9775 : :
9776 : 3 : pInChI->nTautomer[0] = 0;
9777 : 3 : pInChI->lenTautomer = 1;
9778 : :
9779 : 3 : return 0;
9780 : : }
9781 : :
9782 : :
9783 : : /****************************************************************************/
9784 : 0 : int nProtonCopyIsotopicInfo(INChI* pInChI_to, INChI* pInChI_from)
9785 : : {
9786 [ # # ]: 0 : if (pInChI_from->nNumberOfIsotopicAtoms)
9787 : : {
9788 [ # # ]: 0 : if (pInChI_to->nNumberOfIsotopicAtoms &&
9789 [ # # ]: 0 : pInChI_from->nNumberOfIsotopicAtoms > pInChI_to->nNumberOfIsotopicAtoms)
9790 : : {
9791 : :
9792 [ # # ]: 0 : inchi_free(pInChI_to->IsotopicAtom);
9793 : 0 : pInChI_to->IsotopicAtom = NULL;
9794 : 0 : pInChI_to->nNumberOfIsotopicAtoms = 0;
9795 : : }
9796 [ # # ]: 0 : if (!pInChI_to->IsotopicAtom &&
9797 : 0 : !(pInChI_to->IsotopicAtom =
9798 [ # # ]: 0 : (INChI_IsotopicAtom*)inchi_calloc(pInChI_from->nNumberOfIsotopicAtoms,
9799 : : sizeof(pInChI_to->IsotopicAtom[0]))))
9800 : : {
9801 : 0 : return RI_ERR_ALLOC;
9802 : : }
9803 : 0 : pInChI_to->nNumberOfIsotopicAtoms = pInChI_from->nNumberOfIsotopicAtoms;
9804 : 0 : memcpy(pInChI_to->IsotopicAtom, pInChI_from->IsotopicAtom,
9805 : 0 : pInChI_from->nNumberOfIsotopicAtoms * sizeof(pInChI_to->IsotopicAtom[0]));
9806 : : }
9807 : : else
9808 : : {
9809 [ # # ]: 0 : if (pInChI_to->IsotopicAtom)
9810 : : {
9811 [ # # ]: 0 : inchi_free(pInChI_to->IsotopicAtom);
9812 : : }
9813 : 0 : pInChI_to->IsotopicAtom = NULL;
9814 : 0 : pInChI_to->nNumberOfIsotopicAtoms = 0;
9815 : : }
9816 : :
9817 : 0 : return 0;
9818 : : }
9819 : :
9820 : :
9821 : : /****************************************************************************
9822 : : Parse InChI formula layer
9823 : : ****************************************************************************/
9824 : 28 : int ParseSegmentFormula(const char* str,
9825 : : int bMobileH,
9826 : : INChI* pInpInChI[],
9827 : : int pnNumComponents[],
9828 : : int* na_total)
9829 : : {
9830 : : int i, j, mpy_component, mpy_atom, len, el_number;
9831 [ + - ]: 28 : int nNumComponents = 0, iComponent, nNumAtoms, nNumAtomsAndH, iAtom, nNumH, nAltMobileH = ALT_TAUT(bMobileH);
9832 : : const char* p, * q, * e, * pStart, * pEnd;
9833 : : INChI* pInChI;
9834 : : char szEl[3];
9835 : : #if ( FIX_GAF_2019_2==1 )
9836 : : /* hack: state passed in *na_total (will be updated in ParseSegmentFormula anyway) */
9837 : 28 : int state = *na_total;
9838 : : #endif
9839 : 28 : nNumAtoms = -999; /* impossible value */
9840 : 28 : *na_total = 0;
9841 : :
9842 : : /* Pass 1. Count components */
9843 : :
9844 : 28 : pStart = (char*)str;
9845 : : while (1)
9846 : : {
9847 [ + - ]: 28 : if (!(pEnd = strchr(pStart, '.')))
9848 : : {
9849 : 28 : pEnd = pStart + strlen(pStart);
9850 : : }
9851 : 28 : p = pStart;
9852 [ + + ]: 28 : if (isdigit(*p))
9853 : : {
9854 : 3 : mpy_component = (int)inchi_strtol(p, &q, 10);
9855 : 3 : p = q;
9856 : : }
9857 : : else
9858 : : {
9859 : 25 : mpy_component = 1;
9860 : : }
9861 [ - + ]: 28 : if (!mpy_component)
9862 : : {
9863 : 0 : break;
9864 : : }
9865 [ - + ]: 28 : if (!isupper(UCINT * p))
9866 : : {
9867 : 0 : break; /* not a formula layer */
9868 : : }
9869 [ - + ]: 28 : if (pEnd == p)
9870 : : {
9871 : 0 : break; /* zero length formula */
9872 : : }
9873 : 28 : nNumComponents += mpy_component;
9874 [ - + ]: 28 : if (*pEnd)
9875 : : {
9876 : 0 : pStart = pEnd + 1;
9877 : : }
9878 : : else
9879 : : {
9880 : 28 : break;
9881 : : }
9882 : : }
9883 : 28 : pnNumComponents[bMobileH] = nNumComponents;
9884 : : #if ( FIX_GAF_2019_1==1 )
9885 [ - + ]: 28 : if (nNumComponents > MAX_ATOMS)
9886 : : {
9887 : 0 : pnNumComponents[bMobileH] = 0;
9888 : 0 : return RI_ERR_SYNTAX; /* syntax error */
9889 : : }
9890 : : /* pnNumComponents was not reset to 0 which results
9891 : : in attempt then to free() garbage at
9892 : : Free_INChI_Stereo ( <-- Free_INChI_Members <-- FreeInpInChI ) */
9893 : : #elif ( FIX_DALKE_BUGS == 1 )
9894 : : if (nNumComponents > MAX_ATOMS)
9895 : : {
9896 : : return RI_ERR_SYNTAX; /* syntax error */
9897 : : }
9898 : : #endif
9899 : : /* exit or error check */
9900 [ - + ]: 28 : if (!nNumComponents)
9901 : : {
9902 : : #if ( FIX_GAF_2019_2==1 )
9903 : 0 : int low_case = 0;
9904 [ # # ]: 0 : if (*pStart)
9905 : : {
9906 : 0 : low_case = islower(UCINT * pStart);
9907 : : }
9908 [ # # # # ]: 0 : if (!*pStart || low_case) {
9909 [ # # ]: 0 : if (low_case)
9910 : : {
9911 [ # # ]: 0 : if (state == IST_MOBILE_H_FORMULA)
9912 : : {
9913 [ # # # # ]: 0 : if (*pStart != 'q' && *pStart != 'p')
9914 : : {
9915 : 0 : return RI_ERR_SYNTAX; /* syntax error */
9916 : : }
9917 : : }
9918 : : /*
9919 : : else if (state == IST_FIXED_H_FORMULA)
9920 : : {
9921 : : if (*pStart != 'h' && *pStart != 'i' && *pStart != 'b' && *pStart != 'q' && *pStart != 'p')
9922 : : {
9923 : : return RI_ERR_SYNTAX;
9924 : : }
9925 : : }*/
9926 : : }
9927 : : #else
9928 : : if (!*pStart || islower(UCINT * pStart)) {
9929 : : #endif
9930 : :
9931 : : INCHI_HEAPCHK
9932 [ # # # # ]: 0 : if (bMobileH == TAUT_NON && 0 < (nNumComponents = pnNumComponents[nAltMobileH]))
9933 : : {
9934 : : /* allocate InChI */
9935 : 0 : pInChI = (INChI*)inchi_calloc(nNumComponents, sizeof(INChI));
9936 [ # # ]: 0 : if (!(pInChI))
9937 : : {
9938 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9939 : : }
9940 : 0 : pInpInChI[bMobileH] = pInChI;
9941 : 0 : pnNumComponents[bMobileH] = nNumComponents;
9942 : : /* djb-rwth: fixing oss-fuzz issue #66985, #66718 */
9943 : : /* U_CHAR** piibmi_na = (U_CHAR**)inchi_malloc(nNumComponents * sizeof(U_CHAR*));
9944 : : char** piibmi_shf = (char**)inchi_malloc(nNumComponents * sizeof(char*)); */
9945 [ # # ]: 0 : for (i = 0; i < nNumComponents; i++)
9946 : : {
9947 : 0 : U_CHAR* piibmi_na = NULL; /* copied from below to obey C syntax - 2024-09-01 DT */
9948 : 0 : char* piibmi_shf = NULL; /* copied from below to obey C syntax - 2024-09-01 DT */
9949 : : /* copy number of atoms */
9950 : 0 : len = pInpInChI[bMobileH][i].nNumberOfAtoms = pInpInChI[nAltMobileH][i].nNumberOfAtoms;
9951 : : /* copy atoms */
9952 : 0 : len = (len + 1) * sizeof(pInpInChI[0][0].nAtom[0]);
9953 [ # # ]: 0 : if (pInpInChI[bMobileH][i].nAtom)
9954 : : {
9955 [ # # ]: 0 : inchi_free(pInpInChI[bMobileH][i].nAtom);
9956 : : }
9957 : 0 : piibmi_na = (U_CHAR*)inchi_malloc(((long long)len + 1) * sizeof(pInpInChI[0][0].nAtom[0]));
9958 [ # # ]: 0 : if (piibmi_na) /* djb-rwth: cast operator added; addressing LLVM warning */
9959 : : {
9960 : 0 : memcpy(piibmi_na, pInpInChI[nAltMobileH][i].nAtom, len);
9961 : 0 : piibmi_na[len] = 0;
9962 : 0 : pInpInChI[bMobileH][i].nAtom = piibmi_na;
9963 : : }
9964 : : else
9965 : : {
9966 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9967 : : }
9968 : : /* copy Hill formula */
9969 : 0 : len = (int)strlen(pInpInChI[nAltMobileH][i].szHillFormula) + 1;
9970 [ # # ]: 0 : if (pInpInChI[bMobileH][i].szHillFormula)
9971 : : {
9972 [ # # ]: 0 : inchi_free(pInpInChI[bMobileH][i].szHillFormula);
9973 : : }
9974 : 0 : piibmi_shf = (char*)inchi_malloc((inchi_max(len, 2)) * sizeof(char));
9975 [ # # ]: 0 : if (piibmi_shf) /* djb-rwth: addressing LLVM warning */
9976 : : {
9977 : 0 : memcpy(piibmi_shf, pInpInChI[nAltMobileH][i].szHillFormula, len);
9978 : 0 : pInpInChI[bMobileH][i].szHillFormula = piibmi_shf;
9979 : : }
9980 : : else
9981 : : {
9982 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9983 : : }
9984 : : }
9985 : : }
9986 : : else
9987 : : {
9988 [ # # ]: 0 : if (bMobileH == TAUT_YES)
9989 : : {
9990 : : int ret;
9991 : : /* allocate InChI */
9992 : 0 : nNumComponents = 1;
9993 : : /* InChI */
9994 : 0 : pnNumComponents[bMobileH] = nNumComponents;
9995 [ # # ]: 0 : if (!(pInChI = (INChI*)inchi_calloc(nNumComponents, sizeof(INChI))))
9996 : : {
9997 : 0 : return RI_ERR_ALLOC; /* alloc failure */
9998 : : }
9999 : 0 : pInpInChI[bMobileH] = pInChI;
10000 : 0 : ret = nFillOutProtonMobileH(pInChI);
10001 [ # # ]: 0 : if (ret < 0)
10002 : : {
10003 : 0 : return ret;
10004 : : }
10005 : : }
10006 : : }
10007 : 0 : return 0;
10008 : : }
10009 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10010 : : }
10011 : :
10012 [ - + ]: 28 : if (*pEnd)
10013 : : {
10014 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10015 : : }
10016 : :
10017 : : /* allocate InChI */
10018 [ - + ]: 28 : if (!((sizeof(INChI) > 0) && (pInpInChI[bMobileH] = (INChI*)inchi_calloc(nNumComponents, sizeof(INChI))))) /* djb-rwth: fixing GH issue #58 */
10019 : : {
10020 : 0 : return RI_ERR_ALLOC; /* alloc failure */
10021 : : }
10022 : 28 : pInChI = pInpInChI[bMobileH];
10023 : :
10024 : : /* Pass 2. Count elements, save formulas and elements */
10025 : 28 : pStart = (char*)str;
10026 : 28 : iComponent = 0;
10027 : : while (1)
10028 : : {
10029 [ + - ]: 28 : if (!(pEnd = strchr(pStart, '.')))
10030 : : {
10031 : 28 : pEnd = pStart + strlen(pStart);
10032 : : }
10033 : 28 : p = pStart;
10034 [ + + ]: 28 : if (isdigit(UCINT * p))
10035 : : {
10036 : 3 : mpy_component = (int)inchi_strtol(p, &q, 10);
10037 : : #if ( CHECK_STRTOL_ATNUMB==1 )
10038 [ + - - + ]: 3 : if (mpy_component > MAX_ATOMS || mpy_component < 0)
10039 : : {
10040 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10041 : : }
10042 : : #endif
10043 : 3 : p = q;
10044 : : }
10045 : : else
10046 : : {
10047 : 25 : mpy_component = 1;
10048 : : }
10049 : : #if ( FIX_DALKE_BUGS == 1 )
10050 [ - + ]: 28 : if (iComponent + mpy_component > MAX_ATOMS)
10051 : : {
10052 : : #if ( FIX_GAF_2019_1==1 )
10053 : 0 : nNumComponents = 0; /* djb-rwth: ignoring LLVM warning: value used */
10054 : : #endif
10055 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10056 : : }
10057 : : #endif
10058 : 28 : len = (int)(pEnd - p);
10059 [ + + ]: 59 : for (i = 0; i < mpy_component; i++)
10060 : : {
10061 [ - + ]: 31 : if (iComponent + i >= nNumComponents)
10062 : : {
10063 : 0 : return RI_ERR_SYNTAX;
10064 : : }
10065 [ - + ]: 31 : if (pInChI[iComponent + i].szHillFormula)
10066 : : {
10067 [ # # ]: 0 : inchi_free(pInChI[iComponent + i].szHillFormula);
10068 : : }
10069 : 31 : pInChI[iComponent + i].szHillFormula = (char*)inchi_malloc(inchi_max((long long)len, 1) + 1); /* djb-rwth: cast operator added */
10070 [ + - ]: 31 : if (pInChI[iComponent + i].szHillFormula) /* djb-rwth: fixing a NULL pointer dereference */
10071 : : {
10072 : 31 : memcpy(pInChI[iComponent].szHillFormula, p, len);
10073 : 31 : pInChI[iComponent + i].szHillFormula[len] = '\0';
10074 : : }
10075 : : else
10076 : : {
10077 : 0 : return RI_ERR_ALLOC; /* djb-rwth: memory could not be allocated */
10078 : : }
10079 [ + + ]: 31 : if (!i)
10080 : : {
10081 : : /* Pass 2.1 Parse formula and count atoms except H */
10082 : 28 : nNumAtoms = 0;
10083 : 28 : nNumH = 0;
10084 : : /* djb-rwth: removing redundant code */
10085 : 28 : e = pInChI[iComponent].szHillFormula;
10086 [ + - + + ]: 83 : while (e && *e) /* djb-rwth: fixing a NULL pointer dereference */
10087 : : {
10088 [ - + ]: 55 : if (!isupper(UCINT * e))
10089 : : {
10090 : 0 : return RI_ERR_SYNTAX;
10091 : : }
10092 : 55 : j = 0;
10093 : 55 : szEl[j++] = *e++;
10094 [ + + + + ]: 55 : if (*e && islower(UCINT * e))
10095 : 8 : szEl[j++] = *e++;
10096 : 55 : szEl[j++] = '\0';
10097 [ + + + + ]: 55 : if (*e && isdigit(UCINT * e))
10098 : : {
10099 : 35 : mpy_atom = (int)inchi_strtol(e, &q, 10);
10100 : : #if ( CHECK_STRTOL_ATNUMB==1 )
10101 [ + - - + ]: 35 : if (mpy_atom > MAX_ATOMS || mpy_atom < 0)
10102 : : {
10103 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10104 : : }
10105 : : #endif
10106 : 35 : e = q;
10107 : : }
10108 : : else
10109 : : {
10110 : 20 : mpy_atom = 1;
10111 : : }
10112 [ - + ]: 55 : if (!mpy_atom)
10113 : : {
10114 : 0 : return RI_ERR_SYNTAX;
10115 : : }
10116 [ + + + - ]: 55 : if (szEl[0] == 'H' && !szEl[1])
10117 : : {
10118 : 21 : nNumH += mpy_atom;
10119 : 21 : continue; /* ignore H in counting number of atoms */
10120 : : }
10121 : 34 : nNumAtoms += mpy_atom;
10122 : : }
10123 : : #if ( FIX_DALKE_BUGS == 1 )
10124 [ - + ]: 28 : if (nNumAtoms > MAX_ATOMS)
10125 : : {
10126 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10127 : : }
10128 : : #endif
10129 : 28 : (*na_total) += mpy_component * nNumAtoms;
10130 : :
10131 [ + + ]: 28 : nNumAtomsAndH = nNumAtoms ? nNumAtoms : (nNumH > 0);
10132 : 28 : pInChI[iComponent + i].nNumberOfAtoms = nNumAtomsAndH;
10133 [ - + ]: 28 : if (pInChI[iComponent + i].nAtom)
10134 : : {
10135 [ # # ]: 0 : inchi_free(pInChI[iComponent + i].nAtom);
10136 : : }
10137 : 28 : pInChI[iComponent + i].nAtom = (U_CHAR*)inchi_malloc(((long long)nNumAtomsAndH + 1) * sizeof(pInChI[0].nAtom[0])); /* djb-rwth: cast operator added */
10138 [ - + ]: 28 : if (!pInChI[iComponent + i].nAtom)
10139 : : {
10140 : 0 : return RI_ERR_ALLOC; /* failed allocation */
10141 : : }
10142 : : /* Pass 2.2 Store elements; this assumes no bridging H. Bridging H will be found in connection table, /c */
10143 : 28 : iAtom = 0;
10144 [ + + ]: 28 : if (nNumAtoms > 0)
10145 : : {
10146 : 25 : e = pInChI[iComponent + i].szHillFormula;
10147 [ + + ]: 77 : while (*e)
10148 : : {
10149 [ - + ]: 52 : if (!isupper(UCINT * e))
10150 : : {
10151 : 0 : return RI_ERR_SYNTAX;
10152 : : }
10153 : 52 : j = 0;
10154 : 52 : szEl[j++] = *e++;
10155 [ + + + + ]: 52 : if (*e && islower(UCINT * e))
10156 : 8 : szEl[j++] = *e++;
10157 : 52 : szEl[j++] = '\0';
10158 [ + + + + ]: 52 : if (*e && isdigit(UCINT * e))
10159 : : {
10160 : 35 : mpy_atom = (int)inchi_strtol(e, &q, 10);
10161 : : #if ( CHECK_STRTOL_ATNUMB==1 )
10162 [ + - - + ]: 35 : if (mpy_atom > MAX_ATOMS || mpy_atom < 0)
10163 : : {
10164 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10165 : : }
10166 : : #endif
10167 : 35 : e = q;
10168 : : }
10169 : : else
10170 : : {
10171 : 17 : mpy_atom = 1;
10172 : : }
10173 [ - + ]: 52 : if (!mpy_atom)
10174 : : {
10175 : 0 : return RI_ERR_SYNTAX;
10176 : : }
10177 [ + + + - ]: 52 : if (szEl[0] == 'H' && !szEl[1])
10178 : 18 : continue; /* ignore H */
10179 : 34 : el_number = get_periodic_table_number(szEl);
10180 [ - + ]: 34 : if (el_number == ERR_ELEM)
10181 : : {
10182 : 0 : return RI_ERR_SYNTAX; /* wrong element */
10183 : : }
10184 [ + + ]: 96 : while (mpy_atom--)
10185 : : {
10186 [ - + ]: 62 : if (iAtom >= nNumAtoms)
10187 : : {
10188 : 0 : return RI_ERR_PROGR; /* program error */
10189 : : }
10190 : 62 : pInChI[iComponent + i].nAtom[iAtom++] = (U_CHAR)el_number;
10191 : : }
10192 : : }
10193 : : }
10194 : : else
10195 : : {
10196 [ + - ]: 3 : if (nNumH > 0)
10197 : : {
10198 : 3 : pInChI[iComponent + i].nAtom[iAtom++] = EL_NUMBER_H;
10199 : 3 : nNumAtoms = 1;
10200 : : }
10201 : : }
10202 : 28 : pInChI[iComponent + i].nAtom[iAtom] = '\0';
10203 [ - + ]: 28 : if (nNumAtoms != iAtom)
10204 : : {
10205 : 0 : return RI_ERR_PROGR; /* program error */
10206 : : }
10207 : : }
10208 : : else
10209 : : {
10210 : 3 : U_CHAR* pci1 = NULL; /* copied from below to obey C syntax - 2024-09-01 DT */
10211 : : /* Copy duplicated formula */
10212 : 3 : strcpy(pInChI[iComponent + i].szHillFormula, pInChI[iComponent].szHillFormula); /* djb-rwth: unresolved issue -- revision required? */
10213 : : /* Copy atoms in the duplicated formula */
10214 : 3 : pInChI[iComponent + i].nNumberOfAtoms = nNumAtoms;
10215 : : /* djb-rwth: fixing oss-fuzz issue #43420, #34772 */
10216 : 3 : pci1 = (U_CHAR*)inchi_malloc((long long)nNumAtoms + 1); /* djb-rwth: cast operator added */
10217 [ - + ]: 3 : if (!pci1)
10218 : : {
10219 : 0 : return RI_ERR_ALLOC; /* failed allocation */
10220 : : }
10221 : 3 : pInChI[iComponent + i].nAtom = pci1;
10222 : 3 : memcpy(pci1, pInChI[iComponent].nAtom, (long long)nNumAtoms + 1); /* djb-rwth: cast operator added */
10223 : : }
10224 : : }
10225 : 28 : iComponent += i;
10226 [ - + ]: 28 : if (*pEnd)
10227 : : {
10228 [ # # ]: 0 : if (*pEnd != '.')
10229 : : {
10230 : 0 : return RI_ERR_SYNTAX; /* syntax error */
10231 : : }
10232 : 0 : pStart = pEnd + 1;
10233 : : }
10234 : : else
10235 : : {
10236 : 28 : break;
10237 : : }
10238 : : }
10239 : :
10240 [ - + ]: 28 : if (iComponent != nNumComponents)
10241 : : {
10242 : 0 : return RI_ERR_PROGR; /* program error */
10243 : : }
10244 [ - + ]: 28 : if (bMobileH == TAUT_NON)
10245 : : {
10246 : : /* at this point the exact number of atoms including bridging H is known from TAUT_YES */
10247 [ # # # # ]: 0 : for (i = 0; i < nNumComponents && i < pnNumComponents[nAltMobileH]; i++)
10248 : : {
10249 [ # # ]: 0 : if (pInpInChI[bMobileH][i].nNumberOfAtoms < (len = pInpInChI[nAltMobileH][i].nNumberOfAtoms))
10250 : : {
10251 : : /* there are bridging H in this component */
10252 [ # # ]: 0 : if (pInpInChI[nAltMobileH][i].nAtom)
10253 : : {
10254 : 0 : U_CHAR* nAtom = (U_CHAR*)inchi_malloc(((long long)len + 1) * sizeof(nAtom[0])); /* djb-rwth: cast operator added */
10255 [ # # ]: 0 : if (!nAtom)
10256 : : {
10257 : 0 : return RI_ERR_ALLOC;
10258 : : }
10259 : 0 : memcpy(nAtom, pInpInChI[nAltMobileH][i].nAtom, len * sizeof(nAtom[0]));
10260 : 0 : nAtom[len] = 0;
10261 [ # # ]: 0 : if (pInpInChI[bMobileH][i].nAtom)
10262 : : {
10263 [ # # ]: 0 : inchi_free(pInpInChI[bMobileH][i].nAtom);
10264 : : }
10265 : 0 : pInpInChI[bMobileH][i].nAtom = nAtom;
10266 : : }
10267 : 0 : pInpInChI[bMobileH][i].nNumberOfAtoms = len;
10268 : : }
10269 : : }
10270 : : }
10271 : :
10272 : 28 : return nNumComponents + 1;
10273 : : }
10274 : :
10275 : :
10276 : : /****************************************************************************/
10277 : 3 : int CopySegment(INChI* pInChITo,
10278 : : INChI* pInChIFrom,
10279 : : int SegmentType,
10280 : : int bIsotopicTo,
10281 : : int bIsotopicFrom)
10282 : : {
10283 : 3 : int ret = RI_ERR_ALLOC;
10284 : : int len;
10285 : :
10286 [ + + + + ]: 3 : if (SegmentType == CPY_SP2 ||
10287 [ - + ]: 1 : SegmentType == CPY_SP3 ||
10288 [ # # ]: 0 : SegmentType == CPY_SP3_M ||
10289 : : SegmentType == CPY_SP3_S)
10290 : : {
10291 : 3 : INChI_Stereo** pstereoTo = NULL;
10292 [ - + ]: 6 : INChI_Stereo* stereoFrom = bIsotopicFrom == 1 ? pInChIFrom->StereoIsotopic :
10293 [ + - ]: 3 : bIsotopicFrom == 0 ? pInChIFrom->Stereo : NULL;
10294 [ - + - - ]: 3 : if (stereoFrom || bIsotopicFrom < 0)
10295 : : {
10296 [ + + ]: 3 : if (SegmentType == CPY_SP2)
10297 : : {
10298 : : #if ( FIX_GAF_2019_1==1 )
10299 [ + - ]: 1 : if (bIsotopicFrom >= 0 &&
10300 [ + - - + ]: 1 : (pInChIFrom->nNumberOfAtoms > MAX_ATOMS || pInChIFrom->nNumberOfAtoms < 0))
10301 : : {
10302 : 0 : ret = RI_ERR_SYNTAX;
10303 : 0 : goto exit_function;
10304 : : }
10305 : : #endif
10306 [ + - ]: 1 : if (bIsotopicFrom < 0 ||
10307 [ + - ]: 1 : (stereoFrom->b_parity &&
10308 [ + - ]: 1 : stereoFrom->nBondAtom1 &&
10309 [ + - ]: 1 : stereoFrom->nBondAtom2)) /* djb-rwth: addressing LLVM warning */
10310 : : {
10311 : 1 : S_CHAR* pst0_bp = NULL; /* copied from below to obey C syntax - 2024-09-01 DT */
10312 : 1 : AT_NUMB* pst0_nba1 = NULL; /* copied from below to obey C syntax - 2024-09-01 DT */
10313 : 1 : AT_NUMB* pst0_nba2 = NULL; /* copied from below to obey C syntax - 2024-09-01 DT */
10314 : :
10315 [ + - ]: 1 : len = (bIsotopicFrom < 0) ? 0 : stereoFrom->nNumberOfStereoBonds;
10316 [ - + ]: 1 : pstereoTo = bIsotopicTo ? &pInChITo->StereoIsotopic : &pInChITo->Stereo;
10317 [ + - ]: 1 : if (!pstereoTo[0])
10318 : : {
10319 : : /* djb-rwth: fixing oss-fuzz issue #66985 */
10320 : 1 : INChI_Stereo* pst0 = (INChI_Stereo*)inchi_calloc(1, sizeof(**pstereoTo));
10321 : 1 : pstereoTo[0] = pst0;
10322 [ - + ]: 1 : if (!pst0)
10323 : : {
10324 : 0 : goto exit_function;
10325 : : }
10326 : : }
10327 [ + - + - ]: 1 : if (pstereoTo[0]->nNumberOfStereoBonds > 0 || pstereoTo[0]->b_parity ||
10328 [ + - - + ]: 1 : pstereoTo[0]->nBondAtom1 || pstereoTo[0]->nBondAtom2)
10329 : : {
10330 : 0 : ret = RI_ERR_SYNTAX; /* stereo already exists */
10331 : 0 : goto exit_function;
10332 : : }
10333 : : /* allocate sp2 stereo */
10334 : : /* djb-rwth: fixing oss-fuzz issue #66985 */
10335 : : /* djb-rwth: cast operators added */
10336 : 1 : pst0_bp = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pstereoTo[0]->b_parity[0]));
10337 : 1 : pst0_nba1 = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pstereoTo[0]->nBondAtom1[0]));
10338 : 1 : pst0_nba2 = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pstereoTo[0]->nBondAtom2[0]));
10339 : 1 : pstereoTo[0]->b_parity = pst0_bp;
10340 : 1 : pstereoTo[0]->nBondAtom1 = pst0_nba1;
10341 : 1 : pstereoTo[0]->nBondAtom2 = pst0_nba2;
10342 [ + - + - : 1 : if (!pst0_bp || !pst0_nba1 || !pst0_nba2)
- + ]
10343 : : {
10344 : : /* cleanup */
10345 [ # # ]: 0 : if (pst0_bp)
10346 : : {
10347 : : INCHI_HEAPCHK
10348 [ # # ]: 0 : inchi_free(pst0_bp);
10349 : 0 : pst0_bp = NULL;
10350 : : }
10351 [ # # ]: 0 : if (pst0_nba1)
10352 : : {
10353 : : INCHI_HEAPCHK
10354 [ # # ]: 0 : inchi_free(pst0_nba1);
10355 : 0 : pst0_nba1 = NULL;
10356 : : }
10357 [ # # ]: 0 : if (pstereoTo[0]->nBondAtom2)
10358 : : {
10359 : : INCHI_HEAPCHK
10360 [ # # ]: 0 : inchi_free(pst0_nba2);
10361 : 0 : pst0_nba2 = NULL;
10362 : : }
10363 : : INCHI_HEAPCHK
10364 : 0 : goto exit_function;
10365 : : }
10366 : : /* copy stereo */
10367 [ + - + - ]: 1 : if (bIsotopicFrom >= 0 && len)
10368 : : {
10369 : : #if ( FIX_GAF_2019_1==1 )
10370 [ + - - + ]: 1 : if (pInChIFrom->nNumberOfAtoms > MAX_ATOMS || pInChIFrom->nNumberOfAtoms < 0)
10371 : : {
10372 : 0 : ret = RI_ERR_SYNTAX;
10373 : 0 : goto exit_function;
10374 : : }
10375 : : #endif
10376 : 1 : memcpy(pst0_bp, stereoFrom->b_parity, ((long long)len + 1) * sizeof(pst0_bp[0])); /* djb-rwth: cast operator added */
10377 : 1 : memcpy(pst0_nba1, stereoFrom->nBondAtom1, ((long long)len + 1) * sizeof(pst0_nba1[0])); /* djb-rwth: cast operator added */
10378 : 1 : memcpy(pst0_nba2, stereoFrom->nBondAtom2, ((long long)len + 1) * sizeof(pst0_nba2[0])); /* djb-rwth: cast operator added */
10379 : : }
10380 : 1 : pstereoTo[0]->nNumberOfStereoBonds = len;
10381 : :
10382 : 1 : return len + 1;
10383 : : }
10384 : : else
10385 : : {
10386 : 0 : return 0;
10387 : : }
10388 : : }
10389 : : else
10390 : : {
10391 [ + + ]: 2 : if (SegmentType == CPY_SP3)
10392 : : {
10393 [ + - ]: 1 : if (bIsotopicFrom < 0 ||
10394 [ + - ]: 1 : (stereoFrom->t_parity &&
10395 [ + - ]: 1 : stereoFrom->nNumber)) /* djb-rwth: addressing LLVM warning */
10396 : : {
10397 : :
10398 [ + - ]: 1 : len = (bIsotopicFrom < 0) ? 0 : stereoFrom->nNumberOfStereoCenters;
10399 : :
10400 [ - + ]: 1 : pstereoTo = bIsotopicTo ? &pInChITo->StereoIsotopic : &pInChITo->Stereo;
10401 [ + - ]: 1 : if (!pstereoTo[0])
10402 : : {
10403 [ - + ]: 1 : if (!(pstereoTo[0] = (INChI_Stereo*)inchi_calloc(1, sizeof(**pstereoTo))))
10404 : : {
10405 : 0 : goto exit_function;
10406 : : }
10407 : : }
10408 [ + - + - ]: 1 : if (pstereoTo[0]->nNumberOfStereoCenters > 0 || pstereoTo[0]->t_parity ||
10409 [ - + ]: 1 : pstereoTo[0]->nNumber)
10410 : : {
10411 : 0 : ret = RI_ERR_SYNTAX; /* stereo already exists */
10412 : 0 : goto exit_function;
10413 : : }
10414 : : /* allocate sp3 stereo */
10415 [ + - ]: 1 : if (!(pstereoTo[0]->t_parity = (S_CHAR*)inchi_calloc((long long)len + 1, sizeof(pstereoTo[0]->b_parity[0]))) ||
10416 [ - + ]: 1 : !(pstereoTo[0]->nNumber = (AT_NUMB*)inchi_calloc((long long)len + 1, sizeof(pstereoTo[0]->nBondAtom1[0])))) /* djb-rwth: cast operators added */
10417 : : {
10418 : : /* cleanup */
10419 [ # # ]: 0 : if (pstereoTo[0]->t_parity)
10420 : : {
10421 [ # # ]: 0 : inchi_free(pstereoTo[0]->t_parity);
10422 : 0 : pstereoTo[0]->t_parity = NULL;
10423 : : }
10424 [ # # ]: 0 : if (pstereoTo[0]->nNumber)
10425 : : {
10426 [ # # ]: 0 : inchi_free(pstereoTo[0]->nNumber);
10427 : 0 : pstereoTo[0]->nNumber = NULL;
10428 : : }
10429 : 0 : goto exit_function;
10430 : : }
10431 : : /* copy stereo */
10432 [ + - + - ]: 1 : if (bIsotopicFrom >= 0 && len)
10433 : : {
10434 : 1 : memcpy(pstereoTo[0]->t_parity, stereoFrom->t_parity, ((long long)len + 1) * sizeof(pstereoTo[0]->t_parity[0])); /* djb-rwth: cast operator added */
10435 : 1 : memcpy(pstereoTo[0]->nNumber, stereoFrom->nNumber, ((long long)len + 1) * sizeof(pstereoTo[0]->nNumber[0])); /* djb-rwth: cast operator added */
10436 : : }
10437 : 1 : pstereoTo[0]->nNumberOfStereoCenters = len;
10438 : 1 : return len + 1;
10439 : : }
10440 : : else
10441 : : {
10442 : 0 : return 0;
10443 : : }
10444 : : }
10445 : : else
10446 : : {
10447 [ + - ]: 1 : if (SegmentType == CPY_SP3_M)
10448 : : {
10449 [ - + ]: 1 : pstereoTo = bIsotopicTo ? &pInChITo->StereoIsotopic : &pInChITo->Stereo;
10450 [ - + ]: 1 : if (!pstereoTo[0])
10451 : : {
10452 [ # # ]: 0 : if (!(pstereoTo[0] = (INChI_Stereo*)inchi_calloc(1, sizeof(**pstereoTo))))
10453 : : {
10454 : 0 : goto exit_function;
10455 : : }
10456 : : }
10457 [ - + - - ]: 1 : if (pstereoTo[0]->nCompInv2Abs && NO_VALUE_INT != pstereoTo[0]->nCompInv2Abs)
10458 : : {
10459 : 0 : ret = RI_ERR_SYNTAX; /* stereo already exists */
10460 : 0 : goto exit_function;
10461 : : }
10462 [ - + ]: 1 : if (bIsotopicFrom < 0)
10463 : : {
10464 : 0 : pstereoTo[0]->nCompInv2Abs = 0;
10465 : : }
10466 : : else
10467 : : {
10468 : 1 : pstereoTo[0]->nCompInv2Abs = stereoFrom->nCompInv2Abs;
10469 : : }
10470 : 1 : return 1;
10471 : : }
10472 : : else
10473 : : {
10474 : : /* use bTrivialInv to save /s1, /s2, /s3 */
10475 [ # # ]: 0 : if (SegmentType == CPY_SP3_S)
10476 : : {
10477 [ # # ]: 0 : pstereoTo = bIsotopicFrom ? &pInChITo->StereoIsotopic : &pInChITo->Stereo;
10478 [ # # ]: 0 : if (!pstereoTo[0])
10479 : : {
10480 [ # # ]: 0 : if (!(pstereoTo[0] = (INChI_Stereo*)inchi_calloc(1, sizeof(**pstereoTo))))
10481 : : {
10482 : 0 : goto exit_function;
10483 : : }
10484 : : }
10485 [ # # ]: 0 : if (pstereoTo[0]->bTrivialInv)
10486 : : {
10487 : 0 : ret = RI_ERR_SYNTAX; /* stereo already exists */
10488 : 0 : goto exit_function;
10489 : : }
10490 [ # # ]: 0 : if (stereoFrom) /* djb-rwth: fixing a NULL pointer dereference */
10491 : 0 : pstereoTo[0]->bTrivialInv = stereoFrom->bTrivialInv;
10492 [ # # ]: 0 : if (bIsotopicFrom < 0)
10493 : : {
10494 : 0 : pstereoTo[0]->bTrivialInv = 0;
10495 : : }
10496 : : else
10497 : : {
10498 : 0 : pstereoTo[0]->bTrivialInv = stereoFrom->bTrivialInv;
10499 : : }
10500 : 0 : return 1;
10501 : : }
10502 : : }
10503 : : }
10504 : : }
10505 : : }
10506 : :
10507 : 0 : return 0; /* nothing to copy */
10508 : : }
10509 : :
10510 [ # # ]: 0 : else if (SegmentType == CPY_ISO_AT)
10511 : : {
10512 : 0 : int nNumberOfIsotopicAtoms = pInChIFrom->nNumberOfIsotopicAtoms;
10513 : 0 : INChI_IsotopicAtom** pIsotopicAtomTo = NULL;
10514 : 0 : INChI_IsotopicAtom* IsotopicAtomFrom = pInChIFrom->IsotopicAtom;
10515 [ # # # # ]: 0 : if (bIsotopicFrom < 0 || IsotopicAtomFrom)
10516 : : {
10517 [ # # ]: 0 : len = (bIsotopicFrom < 0) ? 0 : nNumberOfIsotopicAtoms;
10518 : : #if ( FIX_GAF_2019_3==1 )
10519 [ # # ]: 0 : if (pInChITo->nNumberOfIsotopicAtoms < 1)
10520 : : {
10521 : : /* forcibly free iso-related memory and set to NULL */
10522 [ # # # # ]: 0 : qzfree(pInChITo->IsotopicAtom);
10523 : : }
10524 : : #endif
10525 : 0 : pIsotopicAtomTo = &pInChITo->IsotopicAtom;
10526 [ # # ]: 0 : if (!*pIsotopicAtomTo)
10527 : : {
10528 [ # # ]: 0 : if (!(*pIsotopicAtomTo = (INChI_IsotopicAtom*)inchi_calloc((long long)len + 1, sizeof(**pIsotopicAtomTo)))) /* djb-rwth: cast operator added */
10529 : : {
10530 : 0 : goto exit_function;
10531 : : }
10532 : : }
10533 [ # # ]: 0 : if (pInChITo->nNumberOfIsotopicAtoms)
10534 : : {
10535 : 0 : ret = RI_ERR_SYNTAX; /* stereo already exists */
10536 : 0 : goto exit_function;
10537 : : }
10538 [ # # # # ]: 0 : if (bIsotopicFrom >= 0 && len)
10539 : : {
10540 : 0 : memcpy(*pIsotopicAtomTo, IsotopicAtomFrom, ((long long)len + 1) * sizeof(**pIsotopicAtomTo)); /* djb-rwth: cast operator added */
10541 : : }
10542 : 0 : pInChITo->nNumberOfIsotopicAtoms = len;
10543 : 0 : return len + 1;
10544 : : }
10545 : :
10546 : 0 : return 0;
10547 : : }
10548 : :
10549 : 0 : ret = RI_ERR_PROGR; /* program error */
10550 : :
10551 : 0 : exit_function:
10552 : :
10553 : 0 : return ret;
10554 : : }
10555 : :
10556 : :
10557 : : /****************************************************************************
10558 : : Sort neighbors in ascending order
10559 : : ****************************************************************************/
10560 : 49 : int insertions_sort_AT_NUMB(AT_NUMB* base, int num)
10561 : : {
10562 : : AT_NUMB* i, * j, * pk, tmp;
10563 : 49 : int k, num_trans = 0;
10564 [ + + ]: 74 : for (k = 1, pk = base; k < num; k++, pk++)
10565 : : {
10566 [ + + + + ]: 51 : for (j = (i = pk) + 1, tmp = *j; j > base && *i > tmp; j = i, i--)
10567 : : {
10568 : 26 : *j = *i;
10569 : 26 : num_trans++;
10570 : : }
10571 : 25 : *j = tmp;
10572 : : }
10573 : :
10574 : 49 : return num_trans;
10575 : : }
10576 : :
10577 : :
10578 : : /****************************************************************************/
10579 : 828 : int getInChIChar(INCHI_IOSTREAM* pInp)
10580 : : {
10581 [ + - ]: 828 : if (pInp->type == INCHI_IOS_TYPE_STRING)
10582 : : {
10583 : : /* input from string */
10584 [ + + ]: 828 : if (pInp->s.nPtr < pInp->s.nUsedLength)
10585 : : {
10586 : 824 : return (int)pInp->s.pStr[pInp->s.nPtr++];
10587 : : }
10588 : 4 : return RI_ERR_EOF;
10589 : : }
10590 : : else
10591 : : {
10592 : : /* input from plain file */
10593 : : int c;
10594 : : #if ( defined(_MSC_VER)&&defined(_WIN32) || defined(__BORLANDC__)&&defined(__WIN32__) || defined(__GNUC__)&&defined(__MINGW32__)&&defined(_WIN32) )
10595 : : do
10596 : : {
10597 : : c = getc(pInp->f);
10598 : : if (c == EOF)
10599 : : {
10600 : : c = RI_ERR_EOF;
10601 : : break;
10602 : : }
10603 : : } while (c == '\r');
10604 : : #else
10605 : 0 : c = getc(pInp->f);
10606 [ # # ]: 0 : if (c == EOF)
10607 : : {
10608 : 0 : c = RI_ERR_EOF;
10609 : : }
10610 : : #endif
10611 : 0 : return c;
10612 : : }
10613 : : }
10614 : :
10615 : :
10616 : : /****************************************************************************/
10617 : 828 : int AddInChIChar(INCHI_IOSTREAM* pInp,
10618 : : SEGM_LINE* Line,
10619 : : const char* pszToken)
10620 : : {
10621 : 828 : int c = getInChIChar(pInp);
10622 : : /*
10623 : : while ( c == '\r' ) {
10624 : : c = getInChIChar( pInp );
10625 : : }
10626 : : */
10627 : :
10628 : : INCHI_HEAPCHK
10629 : :
10630 [ + + ]: 828 : if (Line->len + 2 >= Line->len_alloc)
10631 : : {
10632 : 28 : char* str = (char*)inchi_calloc((long long)Line->len_alloc + SEGM_LINE_ADD, sizeof(str[0])); /* djb-rwth: cast operator added */
10633 : : INCHI_HEAPCHK
10634 [ + - ]: 28 : if (str)
10635 : : {
10636 [ - + - - ]: 28 : if (Line->len > 0 && Line->str)
10637 : : {
10638 : 0 : memcpy(str, Line->str, sizeof(str[0]) * Line->len);
10639 : 0 : Line->len_alloc += SEGM_LINE_ADD;
10640 [ # # ]: 0 : inchi_free(Line->str);
10641 : : INCHI_HEAPCHK
10642 : : }
10643 : : else
10644 : : {
10645 : 28 : Line->len_alloc += SEGM_LINE_ADD;
10646 : : }
10647 : 28 : Line->str = str;
10648 : : }
10649 : : else
10650 : : {
10651 : 0 : c = RI_ERR_ALLOC; /* fatal error */
10652 : 0 : goto exit_function;
10653 : : }
10654 : : }
10655 : : INCHI_HEAPCHK
10656 [ - + ]: 828 : if (c < 0)
10657 : : {
10658 : 0 : Line->str[Line->len] = '\0';
10659 : : INCHI_HEAPCHK
10660 : 0 : c = RI_ERR_SYNTAX; /* fatal error: wrong char */
10661 : 0 : goto exit_function;
10662 : : }
10663 : :
10664 [ + + + + ]: 828 : if (c && strchr(pszToken, c)) /* /\ */
10665 : : {
10666 : 77 : Line->str[Line->len] = '\0';
10667 : : INCHI_HEAPCHK
10668 : 77 : c = -(c + 2);
10669 : 77 : goto exit_function;
10670 : : }
10671 [ + + - + ]: 751 : else if (!c && !Line->len)
10672 : : {
10673 : 0 : Line->str[Line->len] = c;
10674 : : INCHI_HEAPCHK
10675 : : }
10676 : : else
10677 : : {
10678 : 751 : Line->str[Line->len++] = c;
10679 : : INCHI_HEAPCHK
10680 : : }
10681 : :
10682 : 828 : exit_function:
10683 : :
10684 : : INCHI_HEAPCHK
10685 : :
10686 : 828 : return c; /* djb-rwth: addressing coverity ID #499500 -- c = getInChIChar(pInp) cannot be tainted */
10687 : : }
10688 : :
10689 : : /****************************************************************************/
10690 : 103 : int nGetInChISegment(INCHI_IOSTREAM* pInp,
10691 : : SEGM_LINE* Line,
10692 : : const char* pszToken)
10693 : : {
10694 : : int c;
10695 : 103 : Line->len = 0;
10696 [ + + ]: 828 : while (0 < (c = AddInChIChar(pInp, Line, pszToken)))
10697 : : {
10698 : : ;
10699 : : }
10700 [ + + ]: 103 : if (c < -2)
10701 : : {
10702 : 77 : c = -(c + 2);
10703 : : }
10704 : 103 : Line->c = c;
10705 : :
10706 : 103 : return c;
10707 : : }
10708 : :
10709 : :
10710 : : /****************************************************************************
10711 : : Add one more bond to the linked lists for both neighbors
10712 : : ****************************************************************************/
10713 : 37 : int AddLinkedBond(AT_NUMB at1,
10714 : : AT_NUMB at2,
10715 : : AT_NUMB num_at,
10716 : : LINKED_BONDS* pLB)
10717 : : {
10718 : 37 : int nReqLen = inchi_max(2 * num_at + 2, pLB->len + 2);
10719 : : AT_NUMB prev;
10720 : : #if ( FIX_GAF_2019_2==1 )
10721 : : {
10722 [ + - + - : 37 : if (at2 > num_at || at1 > num_at || at1 < 0 || at2<0 || num_at>MAX_ATOMS)
- + ]
10723 : : {
10724 : 0 : return RI_ERR_SYNTAX;
10725 : : }
10726 : : }
10727 : : #endif
10728 [ + + ]: 37 : if (pLB->len_alloc <= nReqLen)
10729 : : {
10730 : : /*int nNewLen = nReqLen + (nReqLen + LINKED_BOND_ADD - 1)%LINKED_BOND_ADD + LINKED_BOND_ADD;*/
10731 : 12 : int nNewLen = nReqLen - nReqLen % LINKED_BOND_ADD + 2 * LINKED_BOND_ADD;
10732 : 12 : ONE_LINKED_BOND* pBond = (ONE_LINKED_BOND*)inchi_calloc(nNewLen, sizeof(pBond[0]));
10733 [ - + ]: 12 : if (!pBond)
10734 : : {
10735 : 0 : return RI_ERR_ALLOC; /* allocation error */
10736 : : }
10737 [ - + - - ]: 12 : if (pLB->pBond && pLB->len)
10738 : : {
10739 : 0 : memcpy(pBond, pLB->pBond, pLB->len * sizeof(pBond[0]));
10740 : : }
10741 [ - + ]: 12 : if (pLB->pBond)
10742 [ # # ]: 0 : inchi_free(pLB->pBond);
10743 : 12 : pLB->pBond = pBond;
10744 : 12 : pLB->len_alloc = nNewLen;
10745 : : }
10746 [ + + ]: 37 : if (!pLB->len)
10747 : : {
10748 : 12 : pLB->len = num_at + 1;
10749 : 12 : memset(pLB->pBond, 0, ((long long)num_at + 1) * sizeof(pLB->pBond[0])); /* djb-rwth: cast operator added; memset_s C11/Annex K variant? */
10750 : : }
10751 : :
10752 : 37 : prev = pLB->pBond[at1].prev; /* position of the last neighbor of at1 in the pLB->pBond */
10753 [ + + ]: 37 : if (!prev)
10754 : : {
10755 : 12 : pLB->pBond[at1].neigh = at2;
10756 : 12 : pLB->pBond[at1].prev = at1;
10757 : : }
10758 : : else
10759 : : {
10760 : 25 : pLB->pBond[pLB->len].neigh = at2;
10761 : 25 : pLB->pBond[pLB->len].prev = prev;
10762 : 25 : pLB->pBond[at1].prev = pLB->len++;
10763 : : }
10764 : :
10765 : 37 : prev = pLB->pBond[at2].prev; /* position of the last neighbor of at2 in the pLB->pBond */
10766 [ + - ]: 37 : if (!prev)
10767 : : {
10768 : 37 : pLB->pBond[at2].neigh = at1;
10769 : 37 : pLB->pBond[at2].prev = at2;
10770 : : }
10771 : : else
10772 : : {
10773 : 0 : pLB->pBond[pLB->len].neigh = at1;
10774 : 0 : pLB->pBond[pLB->len].prev = prev;
10775 : 0 : pLB->pBond[at2].prev = pLB->len++;
10776 : : }
10777 : :
10778 : 37 : return 0;
10779 : : }
10780 : :
10781 : :
10782 : : /****************************************************************************
10783 : : PrepareSaveOptBits
10784 : : ****************************************************************************/
10785 : 0 : void PrepareSaveOptBits(INPUT_PARMS* ip,
10786 : : INCHI_IOSTREAM* pLog,
10787 : : const long num_inp,
10788 : : const char* szCurHdr,
10789 : : int input_has_save_opt,
10790 : : unsigned char input_save_opt_bits,
10791 : : unsigned char* save_opt_bits)
10792 : : {
10793 : :
10794 [ # # ]: 0 : if (!input_has_save_opt)
10795 : : {
10796 : : /* Does not allow to create SaveOpt if the source lacks appendix */
10797 : 0 : ip->bINChIOutputOptions &= ~INCHI_OUT_SAVEOPT;
10798 [ # # # # ]: 0 : if (szCurHdr && szCurHdr[0])
10799 : : {
10800 : 0 : inchi_ios_eprint(pLog,
10801 : : "Warning: ignore SaveOpt request for SaveOpt-less input, %s\n",
10802 : : szCurHdr);
10803 : : }
10804 : : else
10805 : : {
10806 : 0 : inchi_ios_eprint(pLog,
10807 : : "Warning: ignore SaveOpt request for SaveOpt-less input, Structure %ld\n",
10808 : : num_inp);
10809 : : }
10810 : : }
10811 : : else
10812 : : {
10813 : : /* Analyze existing and prepare new SaveOpt appendix */
10814 : : /* djb-rwth: addressing coverity ID #499490 -- these are variable initialisers setting values to 0 */
10815 : 0 : int input_save_opt_has_recmet = input_save_opt_bits & SAVE_OPT_RECMET;
10816 : 0 : int input_save_opt_has_fixedh = input_save_opt_bits & SAVE_OPT_FIXEDH;
10817 : 0 : int input_save_opt_has_suu = input_save_opt_bits & SAVE_OPT_SUU;
10818 : 0 : int input_save_opt_has_sluud = input_save_opt_bits & SAVE_OPT_SLUUD;
10819 : 0 : int input_save_opt_has_ket = input_save_opt_bits & SAVE_OPT_KET;
10820 : 0 : int input_save_opt_has_15t = input_save_opt_bits & SAVE_OPT_15T;
10821 : 0 : int input_save_opt_has_pt_22_00 = input_save_opt_bits & SAVE_OPT_PT_22_00;
10822 : 0 : int input_save_opt_has_pt_16_00 = input_save_opt_bits & SAVE_OPT_PT_16_00;
10823 : 0 : int input_save_opt_has_pt_06_00 = input_save_opt_bits & SAVE_OPT_PT_06_00;
10824 : 0 : int input_save_opt_has_pt_39_00 = input_save_opt_bits & SAVE_OPT_PT_39_00;
10825 : 0 : int input_save_opt_has_pt_13_00 = input_save_opt_bits & SAVE_OPT_PT_13_00;
10826 : 0 : int input_save_opt_has_pt_18_00 = input_save_opt_bits & SAVE_OPT_PT_18_00;
10827 : :
10828 [ # # ]: 0 : if (0 != (ip->bTautFlags & TG_FLAG_RECONNECT_COORD))
10829 : : {
10830 : : /* RecMet requested */
10831 [ # # ]: 0 : if (input_save_opt_has_recmet)
10832 : : {
10833 : 0 : *save_opt_bits |= SAVE_OPT_RECMET;
10834 : : }
10835 : : else
10836 : : {
10837 : 0 : ip->bTautFlags &= ~TG_FLAG_RECONNECT_COORD;
10838 [ # # # # ]: 0 : if (szCurHdr && szCurHdr[0])
10839 : : {
10840 : 0 : inchi_ios_eprint(pLog, "Warning: input created w/o RecMet - ignoring RecMet request, %s\n", szCurHdr);
10841 : : }
10842 : : else
10843 : : {
10844 : 0 : inchi_ios_eprint(pLog, "Warning: input created w/o RecMet - ignoring RecMet request, Structure %ld\n", num_inp);
10845 : : }
10846 : : }
10847 : : }
10848 : :
10849 [ # # ]: 0 : if (0 != (ip->nMode & REQ_MODE_BASIC))
10850 : : {
10851 : : /* FixedH requested */
10852 [ # # ]: 0 : if (input_save_opt_has_fixedh)
10853 : : {
10854 : 0 : *save_opt_bits |= SAVE_OPT_FIXEDH;
10855 : : }
10856 : : else
10857 : : {
10858 : 0 : ip->nMode &= ~REQ_MODE_BASIC;
10859 [ # # # # ]: 0 : if (szCurHdr && szCurHdr[0])
10860 : : {
10861 : 0 : inchi_ios_eprint(pLog, "Warning: input created w/o FixedH - ignoring FixedH request, %s\n", szCurHdr);
10862 : : }
10863 : : else
10864 : : {
10865 : 0 : inchi_ios_eprint(pLog, "Warning: input created w/o FixedH - ignoring FixedH request, Structure %ld\n", num_inp);
10866 : : }
10867 : : }
10868 : : }
10869 : :
10870 : : /* Copy from source SaveOpt those bits which we do not touch */
10871 : : /* while converting InChI: SUU SLUUD KET 15T */
10872 [ # # ]: 0 : if (input_save_opt_has_suu)
10873 : : {
10874 : 0 : *save_opt_bits |= SAVE_OPT_SUU;
10875 : : }
10876 [ # # ]: 0 : if (input_save_opt_has_sluud)
10877 : : {
10878 : 0 : *save_opt_bits |= SAVE_OPT_SLUUD;
10879 : : }
10880 [ # # ]: 0 : if (input_save_opt_has_ket)
10881 : : {
10882 : 0 : *save_opt_bits |= SAVE_OPT_KET;
10883 : : }
10884 [ # # ]: 0 : if (input_save_opt_has_15t)
10885 : : {
10886 : 0 : *save_opt_bits |= SAVE_OPT_15T;
10887 : : }
10888 : :
10889 [ # # ]: 0 : if (input_save_opt_has_pt_22_00)
10890 : 0 : *save_opt_bits |= SAVE_OPT_PT_22_00;
10891 [ # # ]: 0 : if (input_save_opt_has_pt_16_00)
10892 : 0 : *save_opt_bits |= SAVE_OPT_PT_16_00;
10893 [ # # ]: 0 : if (input_save_opt_has_pt_06_00)
10894 : 0 : *save_opt_bits |= SAVE_OPT_PT_06_00;
10895 [ # # ]: 0 : if (input_save_opt_has_pt_39_00)
10896 : 0 : *save_opt_bits |= SAVE_OPT_PT_39_00;
10897 [ # # ]: 0 : if (input_save_opt_has_pt_13_00)
10898 : 0 : *save_opt_bits |= SAVE_OPT_PT_13_00;
10899 [ # # ]: 0 : if (input_save_opt_has_pt_18_00)
10900 : 0 : *save_opt_bits |= SAVE_OPT_PT_18_00;
10901 : :
10902 : : /* Check if /SNon requested and turn OFF stereo bits if so */
10903 [ # # ]: 0 : if (!(ip->nMode & REQ_MODE_STEREO))
10904 : : {
10905 : 0 : *save_opt_bits &= ~SAVE_OPT_SUU;
10906 : 0 : *save_opt_bits &= ~SAVE_OPT_SLUUD;
10907 : : }
10908 : : }
10909 : :
10910 : 0 : return;
10911 : : }
10912 : :
10913 : :
10914 : : /****************************************************************************/
10915 : 9 : void TreatErrorsInReadInChIString(int nReadStatus,
10916 : : int nErr,
10917 : : int pState,
10918 : : INPUT_PARMS* ip,
10919 : : INCHI_IOSTREAM* pOut,
10920 : : INCHI_IOSTREAM* pLog,
10921 : : long* num_inp,
10922 : : long* num_errors,
10923 : : long* num_processed,
10924 : : char** pstrHdr,
10925 : : char** pszCurHdr,
10926 : : InpInChI* pOneInput)
10927 : : {
10928 [ + + + - ]: 9 : int bInChI2Struct = (ip->bReadInChIOptions & READ_INCHI_TO_STRUCTURE) && ip->nInputType == INPUT_INCHI;
10929 : :
10930 : : /* InChI could not be read */
10931 [ - + - - : 9 : if (nReadStatus == RI_ERR_EOF && nErr == 0 && pState == 0) /* && !(*pstrHdr) ) */
- - ]
10932 : : {
10933 : : /*if ( !(*pstrHdr) ) */
10934 : : ;/*inchi_ios_eprint( pLog, "\nEnd of file detected after structure %ld. \n", *num_inp );*/
10935 : : }
10936 : : else
10937 : : {
10938 : : /* Output InChI parsing error message */
10939 : : char szHdrSimulation[128];
10940 : : char szMsg2[1024];
10941 : 9 : (*num_inp)++;
10942 : 9 : sprintf(szHdrSimulation, "Structure: %ld", *num_inp);
10943 : 9 : getInchiStateReadErr(pState, szMsg2);
10944 : :
10945 : : #ifdef TARGET_EXE_STANDALONE
10946 : : if (pOneInput->polymer &&
10947 : : bInChI2Struct &&
10948 : : !(ip->bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY))
10949 : : {
10950 : : inchi_ios_eprint(pLog, "%s Skipping polymer InChI (only conversion to Molfile is available, use OutputSDF option)\n",
10951 : : *pstrHdr ? *pstrHdr : szHdrSimulation);
10952 : : }
10953 : : else
10954 : : #endif
10955 : : {
10956 [ + + - + ]: 9 : if (!bInChI2Struct &&
10957 [ # # ]: 0 : (pState == IST_MOBILE_H_POLYMER && !ip->bPolymers))
10958 : : {
10959 : : /* TO DO: implement InChI2InChI for polymers in a way similar to InChI2Struct
10960 : : thru an external (to ReadWriteInchi) loop */
10961 : 0 : inchi_ios_eprint(pLog, "%s Skipping polymer InChI for conversion of InChI to InChI\n",
10962 [ # # ]: 0 : *pstrHdr ? *pstrHdr : szHdrSimulation);
10963 : : }
10964 : : else
10965 : : {
10966 : 18 : inchi_ios_eprint(pLog, "\n%s %s (%d) in %s (%d)\n",
10967 [ - + ]: 9 : *pstrHdr ? *pstrHdr : szHdrSimulation,
10968 : : getInchiErrName(nErr), nErr,
10969 : : szMsg2, pState);
10970 : : }
10971 : : }
10972 : :
10973 [ - + ]: 9 : if (ip->bINChIOutputOptions2 & INCHI_OUT_INCHI_GEN_ERROR)
10974 : : {
10975 [ # # ]: 0 : if (!(ip->bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY))
10976 : : {
10977 [ # # ]: 0 : inchi_ios_eprint(pOut, "%s\n", *pstrHdr ? *pstrHdr : szHdrSimulation);
10978 [ # # ]: 0 : if (ip->bINChIOutputOptions & INCHI_OUT_STDINCHI)
10979 : : {
10980 : 0 : inchi_ios_eprint(pOut, "InChI=1S//\n");
10981 : : }
10982 : : else
10983 : : {
10984 : 0 : inchi_ios_eprint(pOut, "InChI=1N//\n");
10985 : : }
10986 : : }
10987 : : }
10988 : :
10989 [ + + ]: 9 : if (0 != (ip->bReadInChIOptions & READ_INCHI_TO_STRUCTURE))
10990 : :
10991 : 1 : (*num_errors)++;
10992 : 9 : (*num_processed)++;
10993 : : }
10994 [ - + ]: 9 : if (*pstrHdr)
10995 : : {
10996 [ # # ]: 0 : inchi_free(*pstrHdr);
10997 : 0 : *pstrHdr = NULL;
10998 : : }
10999 [ - + ]: 9 : if (*pszCurHdr)
11000 : : {
11001 [ # # ]: 0 : inchi_free(*pszCurHdr);
11002 : 0 : *pszCurHdr = NULL;
11003 : : }
11004 : :
11005 : 9 : FreeInpInChI(pOneInput);
11006 : :
11007 : 9 : return;
11008 : : }
11009 : :
11010 : :
11011 : : /*
11012 : : InChi --> InChI string(s)
11013 : : */
11014 : :
11015 : :
11016 : : /****************************************************************************/
11017 : 16 : int ConvertInChI2InChI(INPUT_PARMS* ip,
11018 : : InpInChI* pOneInput,
11019 : : INCHI_IOSTREAM* pOut,
11020 : : INCHI_IOSTREAM* pLog,
11021 : : STRUCT_DATA* sd,
11022 : : int num_components[INCHI_NUM],
11023 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
11024 : : char** pszCurHdr,
11025 : : long num_inp,
11026 : : long* num_errors,
11027 : : unsigned char save_opt_bits,
11028 : : inchiTime* pulTStart,
11029 : : long* ulProcessingTime,
11030 : : struct tagINCHI_CLOCK* ic,
11031 : : struct tagCANON_GLOBALS* pCG)
11032 : : {
11033 : : int ret, tmp;
11034 : :
11035 : 16 : InchiTimeGet(pulTStart);
11036 : :
11037 : 16 : tmp = ip->bNoStructLabels;
11038 : 16 : ip->bNoStructLabels = 1;
11039 : : INCHI_HEAPCHK
11040 : 16 : ip->pSdfValue = NULL;
11041 : 16 : ip->pSdfLabel = NULL;
11042 : :
11043 : : #if ( FIX_DALKE_BUGS == 1 )
11044 : 16 : SetHillFormFromInChI(pOneInput);
11045 : : #endif
11046 : :
11047 : : /*
11048 : : * Legacy metal handling cannot be applied during InChI-to-InChI
11049 : : * conversion because the structure is not reconstructed and the
11050 : : * metal-bond processing is therefore not rerun.
11051 : : */
11052 [ + + + - ]: 16 : if (!ip->bMolecularInorganics && (ip->bReadInChIOptions & READ_INCHI_INPUT_MI))
11053 : : {
11054 : 3 : inchi_ios_eprint(pLog, "LegacyMetalHandling cannot be used with InChI2InChI for Molecular Inorganics input\n");
11055 : 3 : ret = RI_ERR_PROGR;
11056 : : }
11057 : : else
11058 : : {
11059 : 13 : ret = OutputInChIAsRequested(pCG, pOut, pLog, ip, sd,
11060 : : pOneInput, num_components,
11061 : : nModeProtonIsoExchgH,
11062 : : num_inp, save_opt_bits);
11063 : : }
11064 : :
11065 : : #if ( !defined(TARGET_API_LIB) && defined(TARGET_EXE_STANDALONE) )
11066 : :
11067 : : /* Calculate InChIKey if requested */
11068 : : /* However, do not calculate/write it if this function is called from within dll */
11069 : : {
11070 : : char ik_string[256]; /* Resulting InChIKey string */
11071 : : int ik_ret = 0; /* InChIKey-calc result code */
11072 : : int xhash1, xhash2;
11073 : : char szXtra1[65], szXtra2[65];
11074 : :
11075 : : inchi_ios_flush2(pLog, stderr);
11076 : :
11077 : : /* post-1.02b addition - correctly treat tabbed output with InChIKey */
11078 : : if (ip->bINChIOutputOptions & INCHI_OUT_TABBED_OUTPUT)
11079 : : {
11080 : : if (ip->bCalcInChIHash != INCHIHASH_NONE)
11081 : : {
11082 : : if (pOut->s.pStr)
11083 : : {
11084 : : if (pOut->s.nUsedLength > 0)
11085 : : {
11086 : : if (pOut->s.pStr[pOut->s.nUsedLength - 1] == '\n')
11087 : : { /* replace LF with TAB */
11088 : : pOut->s.pStr[pOut->s.nUsedLength - 1] = '\t';
11089 : : }
11090 : : }
11091 : : }
11092 : : }
11093 : : }
11094 : :
11095 : : if (ip->bCalcInChIHash == INCHIHASH_NONE)
11096 : : {
11097 : : /* inchi_ios_flush(pOut); */
11098 : : }
11099 : : else
11100 : : {
11101 : : char* buf = NULL;
11102 : : size_t slen = pOut->s.nUsedLength;
11103 : : extract_inchi_substring(&buf, pOut->s.pStr, slen);
11104 : :
11105 : : if (NULL != buf)
11106 : : {
11107 : : xhash1 = xhash2 = 0;
11108 : : if ((ip->bCalcInChIHash == INCHIHASH_KEY_XTRA1) ||
11109 : : (ip->bCalcInChIHash == INCHIHASH_KEY_XTRA1_XTRA2))
11110 : : {
11111 : : xhash1 = 1;
11112 : : }
11113 : : if ((ip->bCalcInChIHash == INCHIHASH_KEY_XTRA2) ||
11114 : : (ip->bCalcInChIHash == INCHIHASH_KEY_XTRA1_XTRA2))
11115 : : {
11116 : : xhash2 = 1;
11117 : : }
11118 : :
11119 : : ik_ret = GetINCHIKeyFromINCHI(buf,
11120 : : xhash1,
11121 : : xhash2,
11122 : : ik_string,
11123 : : szXtra1,
11124 : : szXtra2);
11125 : : inchi_free(buf);
11126 : : }
11127 : : else
11128 : : {
11129 : : ik_ret = INCHIKEY_NOT_ENOUGH_MEMORY;
11130 : : }
11131 : :
11132 : :
11133 : : if (ik_ret == INCHIKEY_OK)
11134 : : {
11135 : : inchi_ios_print(pOut, "InChIKey=%-s\n", ik_string);
11136 : : }
11137 : : else
11138 : : {
11139 : : inchi_ios_print(pLog, "Warning (Could not compute InChIKey: ", num_inp);
11140 : : switch (ik_ret)
11141 : : {
11142 : : case INCHIKEY_UNKNOWN_ERROR:
11143 : : inchi_ios_print(pLog, "unresolved error)");
11144 : : break;
11145 : : case INCHIKEY_EMPTY_INPUT:
11146 : : inchi_ios_print(pLog, "got an empty string)");
11147 : : break;
11148 : : case INCHIKEY_INVALID_INCHI_PREFIX:
11149 : : case INCHIKEY_INVALID_INCHI:
11150 : : case INCHIKEY_INVALID_STD_INCHI:
11151 : : inchi_ios_print(pLog, "no valid InChI string found)");
11152 : : break;
11153 : : case INCHIKEY_NOT_ENOUGH_MEMORY:
11154 : : inchi_ios_print(pLog, "not enough memory to treat the string)");
11155 : : break;
11156 : : default:inchi_ios_print(pLog, "internal program error)");
11157 : : break;
11158 : : }
11159 : :
11160 : : inchi_ios_print(pLog, " structure #%-lu.\n", num_inp);
11161 : : if (ip->bINChIOutputOptions & INCHI_OUT_TABBED_OUTPUT)
11162 : : {
11163 : : inchi_ios_print(pOut, "\n");
11164 : : }
11165 : : } /* if (ip->bCalcInChIHash!=INCHIHASH_NONE) */
11166 : :
11167 : : inchi_ios_flush(pOut);
11168 : : inchi_ios_flush2(pLog, stderr);
11169 : : }
11170 : : } /* Calculate InChIKey if requested */
11171 : : #endif
11172 : :
11173 : 16 : ip->bNoStructLabels = tmp;
11174 : :
11175 : : #ifndef TARGET_API_LIB
11176 : : if (ret < 0)
11177 : : {
11178 : :
11179 : : if (*pszCurHdr && (*pszCurHdr)[0])
11180 : : {
11181 : : inchi_ios_eprint(pLog, "Error %d creating InChI string %s\n", ret, *pszCurHdr);
11182 : : }
11183 : : else
11184 : : {
11185 : : inchi_ios_eprint(pLog, "Error %d creating InChI string, Structure %ld\n", ret, num_inp);
11186 : : }
11187 : : if (ip->bINChIOutputOptions2 & INCHI_OUT_INCHI_GEN_ERROR)
11188 : : {/* inchi_ios_eprint( pOut, "InChICreationError!\n"); *//* emit err string */
11189 : : if (ip->bINChIOutputOptions & INCHI_OUT_STDINCHI)
11190 : : {
11191 : : inchi_ios_eprint(pOut, "InChI=1S//\n");
11192 : : }
11193 : : else
11194 : : {
11195 : : inchi_ios_eprint(pOut, "InChI=1N//\n");
11196 : : }
11197 : : }
11198 : :
11199 : : (*num_errors)++;
11200 : : }
11201 : :
11202 : : #if ( !defined(TARGET_API_LIB) && !defined(TARGET_EXE_STANDALONE) )
11203 : : else
11204 : : if (*pszCurHdr && (*pszCurHdr)[0])
11205 : : {
11206 : : inchi_fprintf(stderr, "%s\r", *pszCurHdr);
11207 : : }
11208 : : #endif
11209 : : #endif
11210 : :
11211 : :
11212 [ - + ]: 16 : if (*pszCurHdr)
11213 : : {
11214 [ # # ]: 0 : inchi_free(*pszCurHdr);
11215 : 0 : *pszCurHdr = NULL;
11216 : : }
11217 : :
11218 : : INCHI_HEAPCHK
11219 : :
11220 : 16 : * ulProcessingTime += InchiTimeElapsed(ic, pulTStart);
11221 : :
11222 : 16 : return ret;
11223 : : }
11224 : :
11225 : :
11226 : : /* InChi --> Structure (presented as AuxInfo or MolFile) */
11227 : :
11228 : :
11229 : : /****************************************************************************/
11230 : 3 : int ConvertInChI2Struct(ICHICONST INPUT_PARMS* ip_inp,
11231 : : INPUT_PARMS* ip,
11232 : : InpInChI* pOneInput,
11233 : : inp_ATOM** at,
11234 : : int* num_at,
11235 : : OAD_Polymer** polymer,
11236 : : OAD_V3000** v3000,
11237 : : INCHI_IOSTREAM* pOut,
11238 : : INCHI_IOSTREAM* pLog,
11239 : : STRUCT_DATA* sd,
11240 : : int num_components[INCHI_NUM],
11241 : : MODE_PIXH nModeProtonIsoExchgH[INCHI_NUM],
11242 : : char** pszCurHdr,
11243 : : char* szMsg,
11244 : : int nMsgLen,
11245 : : char szMessage[MAX_MSG_LEN],
11246 : : int nInitLenMessage,
11247 : : int nMessageLen,
11248 : : int input_is_stdinchi,
11249 : : int bHasSomeReconnected,
11250 : : int bHasSomeFixedH,
11251 : : int bHasMetal,
11252 : : int nModeFlagsStereo,
11253 : : int bTautFlags,
11254 : : int bReqNonTaut,
11255 : : unsigned long WarningFlags[2][2],
11256 : : long num_inp,
11257 : : long* num_errors,
11258 : : unsigned char save_opt_bits,
11259 : : inchiTime* pulTStart,
11260 : : long* ulProcessingTime,
11261 : : struct tagINCHI_CLOCK* ic,
11262 : : struct tagCANON_GLOBALS* pCG)
11263 : : {
11264 : : int ret, i, j;
11265 : : SRM srm; /* rules how to handle bonds to metal atoms */
11266 : : StrFromINChI* pStruct[INCHI_NUM][TAUT_NUM];
11267 : :
11268 : 3 : int bINChIOutputOptions = /* djb-rwth: ignoring LLVM warning: variable used */
11269 : : #if ( I2S_MODIFY_OUTPUT != 1 )
11270 : : 0;
11271 : : #else
11272 : : /* transfer user's InChI output options to serialization 10-12-2007 */
11273 : : ip_inp->bINChIOutputOptions&
11274 : : (
11275 : : INCHI_OUT_NO_AUX_INFO | /* do not output Aux Info */
11276 : : INCHI_OUT_SHORT_AUX_INFO | /* output short version of Aux Info */
11277 : : INCHI_OUT_ONLY_AUX_INFO | /* output only Aux Info */
11278 : : /* INCHI_OUT_EMBED_REC |*/ /* embed reconnected INChI into disconnected INChI */
11279 : : INCHI_OUT_SDFILE_ONLY | /* save input data in a Molfile instead of creating INChI */
11280 : : INCHI_OUT_PLAIN_TEXT | /* output plain text INChI */
11281 : : INCHI_OUT_PLAIN_TEXT_COMMENTS | /* output plain text annotation */
11282 : : /* INCHI_OUT_WINCHI_WINDOW |*/ /* output into wINChI text window */
11283 : : INCHI_OUT_TABBED_OUTPUT | /* tab-delimited (only for plain text) */
11284 : : INCHI_OUT_SDFILE_ATOMS_DT | /* SDfile output H isotopes as D and T */
11285 : : INCHI_OUT_SDFILE_SPLIT | /* Split SDfile into components */
11286 : : 0
11287 : : );
11288 : : #endif
11289 : :
11290 : : /* Preliminaries */
11291 : 3 : InchiTimeGet(pulTStart);
11292 : :
11293 [ + + ]: 3 : if ((input_is_stdinchi & INCHI_INPUT_CLASS_MASK) == INCHI_INPUT_CLASS_STANDARD ||
11294 [ + - ]: 1 : (input_is_stdinchi & INCHI_INPUT_CLASS_MASK) == INCHI_INPUT_CLASS_BETA)
11295 : : {
11296 [ + - ]: 3 : if (ip_inp->bINChIOutputOptions & INCHI_OUT_STDINCHI)
11297 : : {
11298 : 3 : bINChIOutputOptions |= INCHI_OUT_STDINCHI;
11299 : : }
11300 : : }
11301 : : else
11302 : : {
11303 [ # # ]: 0 : if (ip_inp->bINChIOutputOptions & INCHI_OUT_SAVEOPT)
11304 : : {
11305 : 0 : bINChIOutputOptions |= INCHI_OUT_SAVEOPT;
11306 : : }
11307 : : }
11308 : :
11309 : 3 : memset(pStruct, 0, sizeof(pStruct)); /* djb-rwth: memset_s C11/Annex K variant? */
11310 : :
11311 : 3 : SetUpSrm(&srm); /* structure restore parms */
11312 : :
11313 : : /* Eliminate Fixed-H InChI that are exactly same as the corresponding Mobile-H structures */
11314 : 3 : RemoveFixHInChIIdentical2MobH(pOneInput);
11315 : :
11316 : : /* Recheck layers after thee elimination; get optional stereo flags */
11317 : 3 : ret = DetectInpInchiCreationOptions(pOneInput, &bHasSomeReconnected,
11318 : : &bHasMetal, &bHasSomeFixedH,
11319 : : &nModeFlagsStereo, &bTautFlags);
11320 [ - + ]: 3 : if (ret < 0)
11321 : : {
11322 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen,
11323 : : "Error in detecting input InChI options", "; ");
11324 : 0 : (*num_errors)++;
11325 : 0 : goto dealloc;
11326 : : }
11327 : :
11328 [ - + - - ]: 3 : if (bHasSomeFixedH && !bReqNonTaut)
11329 : : {
11330 : 0 : bHasSomeFixedH = 0;
11331 : : }
11332 : :
11333 : : /* Set stereo flags */
11334 : 3 : ip->nMode &= ~(REQ_MODE_STEREO |
11335 : : REQ_MODE_ISO_STEREO |
11336 : : REQ_MODE_RELATIVE_STEREO |
11337 : : REQ_MODE_RACEMIC_STEREO |
11338 : : REQ_MODE_CHIR_FLG_STEREO |
11339 : : REQ_MODE_SB_IGN_ALL_UU |
11340 : : REQ_MODE_SC_IGN_ALL_UU);
11341 : :
11342 : 3 : ip->nMode |= nModeFlagsStereo;
11343 : :
11344 : : /* Remove Phosphine and Arsine Stereo Flags */
11345 : 3 : ip->bTautFlags &= ~TG_FLAG_PHOSPHINE_STEREO;
11346 : 3 : ip->bTautFlags &= ~TG_FLAG_ARSINE_STEREO;
11347 : 3 : ip->bTautFlags &= ~TG_FLAG_FIX_SP3_BUG;
11348 : :
11349 : 3 : ip->bTautFlags |= bTautFlags;
11350 : :
11351 : : /* Mark Disconnected InChI components that are exactly came as Reconnected ones */
11352 : : /* Disconnected will have a negative number of the reconnected component */
11353 : : /* Reconnected will have a positive number of the disconnected component */
11354 : :
11355 : 3 : MarkDisconectedIdenticalToReconnected(pOneInput);
11356 : :
11357 : : /*****************************************************************************/
11358 : : /* Pay attention to: */
11359 : : /* 1) .nLink < 0 in Disonnected which means InChI is same as in Reconnected */
11360 : : /* The component in Reconnected has .nLink pointing to the Disconnected; */
11361 : : /* each .nLink = (1+component index) or -(1+component index) */
11362 : : /* In the future .nLink>0 in Disconnected shall point to the Reconnectrd */
11363 : : /* component from which it was created */
11364 : : /* 2) Currently reversed structures from Disconnected components are created */
11365 : : /* and abandoned if Reconnected layer exists */
11366 : : /* 3) Connect/disconnect H depends on the presence of atom/bond parity */
11367 : : /* The combined Mobile/Fixed-H parity should be set for Fixed-H components*/
11368 : : /* 4) No comparison of the Disconnected layer is done if Reconnected exists */
11369 : : /* 5) Reading InChI was not fully tested in case one component has stereo in */
11370 : : /* both Mobile-H and Fixed-H layers while another component has stereo */
11371 : : /* only in Mobile-H layer */
11372 : : /*****************************************************************************/
11373 : :
11374 : :
11375 : : /* Main conversion InChI->Structure for each component and */
11376 : : /* after that pStruct[iRec][iMobH][iComponent].at2 is the structure, */
11377 : : /* pStruct[iRec][iMobH][iComponent].RevInChI full InChI for the structure */
11378 : : /* In case of both Fixed-H and Mobile-H layers the results are in iMobH=0 */
11379 : : /* In case of only Mobile-H/Main layer the results are in iMobH=1 */
11380 : :
11381 : 3 : ulProcessingTime += InchiTimeElapsed(ic, pulTStart);
11382 : :
11383 : 3 : sd->ulStructTime = 0;
11384 : :
11385 : 3 : ret = AllInchiToStructure(ic, pCG, ip, sd, num_inp, *pszCurHdr, &srm, bHasSomeFixedH, pStruct, pOneInput);
11386 : :
11387 : 3 : ulProcessingTime += sd->ulStructTime;
11388 : 3 : InchiTimeGet(pulTStart);
11389 : :
11390 : : /* ret < 0 is error code; ret > 0 is number of errors */
11391 : : /* in pStruct[iInchiRec][iMobileH][iComponent].nError */
11392 [ - + ]: 3 : if (ret)
11393 : : {
11394 : : #if ( FIX_GAF_2019_1==1 )
11395 [ # # ]: 0 : if (ret > 0)
11396 : : {
11397 : 0 : ret = RI_ERR_PROGR;
11398 : : }
11399 : : #endif
11400 : : /* conversion error */
11401 : 0 : (*num_errors)++;
11402 : 0 : goto dealloc;
11403 : : }
11404 : :
11405 : : /* Attempt to fix the numumber of removed protons in case of Mobile-H */
11406 [ + - ]: 3 : if (!pOneInput->nNumProtons[INCHI_BAS][TAUT_YES].pNumProtons &&
11407 [ + - ]: 3 : !pOneInput->nNumProtons[INCHI_REC][TAUT_YES].pNumProtons)
11408 : : {
11409 : 3 : ret = AddProtonAndIsoHBalanceToMobHStruct(ic, pCG, ip, sd, num_inp, bHasSomeFixedH, *pszCurHdr, pStruct, pOneInput);
11410 : :
11411 [ - + ]: 3 : if (ret < 0)
11412 : : {
11413 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "Add/Remove protons error", "; ");
11414 : 0 : (*num_errors)++;
11415 : 0 : goto dealloc;
11416 : : }
11417 : : }
11418 : :
11419 : : /* Compare InChI from the Reversed Structure to the original input InChI */
11420 : 3 : ret = CompareAllOrigInchiToRevInChI(pStruct, pOneInput, bHasSomeFixedH, num_inp, *pszCurHdr);
11421 [ - + ]: 3 : if (ret < 0)
11422 : : {
11423 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "InChI compare error", "; ");
11424 : 0 : (*num_errors)++;
11425 : 0 : goto dealloc;
11426 : : }
11427 : :
11428 : : /* Compare disconnected versions */
11429 : 3 : ret = CompareAllDisconnectedOrigInchiToRevInChI(pStruct, pOneInput,
11430 : : bHasSomeFixedH, num_inp, *pszCurHdr);
11431 [ - + ]: 3 : if (ret < 0)
11432 : : {
11433 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "InChI compare2 error", "; ");
11434 : 0 : (*num_errors)++;
11435 : 0 : goto dealloc;
11436 : : }
11437 : :
11438 [ + - ]: 3 : if (WarningFlags)
11439 : : {
11440 [ + + ]: 9 : for (i = 0; i < 2; i++)
11441 : : {
11442 [ + + ]: 18 : for (j = 0; j < TAUT_NUM; j++)
11443 : : {
11444 : 12 : WarningFlags[i][j] = (unsigned long)pOneInput->CompareInchiFlags[i][j];
11445 : : }
11446 : : }
11447 : : }
11448 : :
11449 : 3 : ulProcessingTime += InchiTimeElapsed(ic, pulTStart);
11450 : :
11451 : : #ifndef COMPILE_ANSI_ONLY
11452 : : ret = DisplayStructureComponents(pCG, ip, sd, num_inp, *pszCurHdr, &srm, bReqNonTaut, pStruct, pOneInput);
11453 : : if (ret < 0)
11454 : : {
11455 : : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "Display structure error", "; ");
11456 : : }
11457 : : #endif
11458 : :
11459 : :
11460 : 3 : InchiTimeGet(pulTStart);
11461 : :
11462 : 3 : ret = MergeStructureComponents(ip, sd, num_inp, *pszCurHdr, &srm, bReqNonTaut, pStruct, pOneInput);
11463 : :
11464 : 3 : ulProcessingTime += InchiTimeElapsed(ic, pulTStart);
11465 : :
11466 [ - + ]: 3 : if (ret < 0)
11467 : : {
11468 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "Merge Components error", "; ");
11469 : 0 : (*num_errors)++;
11470 : 0 : goto dealloc;
11471 : : }
11472 : :
11473 : :
11474 : : #ifdef TARGET_API_LIB
11475 : : /*------------- for debug only -------------------
11476 : : InchiTimeGet(&ulTStart);
11477 : : ret = OutputInChIOutOfStrFromINChI( ic, pCG,
11478 : : ip, sd, num_inp, 0,
11479 : : pOut, pLog, &OneInput,
11480 : : save_opt_bits);
11481 : : ulProcessingTime += InchiTimeElapsed( ic, pulTStart);
11482 : : if ( ret < 0 )
11483 : : {
11484 : : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "Restored structure to InChI conversion failed", "; ");
11485 : : goto dealloc;
11486 : : }
11487 : : -------------------------------------------------*/
11488 [ + - + - ]: 3 : if (at && num_at)
11489 : : {
11490 : 3 : *at = pOneInput->atom;
11491 : 3 : *num_at = pOneInput->num_atoms;
11492 : 3 : pOneInput->atom = NULL;
11493 : 3 : *polymer = pOneInput->polymer;
11494 : 3 : pOneInput->polymer = NULL;
11495 : 3 : *v3000 = pOneInput->v3000;
11496 : 3 : pOneInput->v3000 = NULL;
11497 : : }
11498 : : #else
11499 : :
11500 : : InchiTimeGet(pulTStart);
11501 : :
11502 : : ret = OutputInChIOutOfStrFromINChI(ic, pCG, ip, sd, num_inp, bINChIOutputOptions,
11503 : : pOut, NULL, pOneInput, bHasSomeFixedH, save_opt_bits);
11504 : :
11505 : : ulProcessingTime += InchiTimeElapsed(ic, pulTStart);
11506 : :
11507 : : if (ret < 0)
11508 : : {
11509 : : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "Restored structure to InChI conversion error", "; ");
11510 : : (*num_errors)++;
11511 : : goto dealloc;
11512 : : }
11513 : : #endif
11514 : :
11515 [ + - ]: 3 : if (szMessage)
11516 : : {
11517 : 3 : int len, retcomp = 0, retcomp1 = 0;
11518 : 3 : InchiTimeGet(pulTStart);
11519 : 3 : retcomp = FillOutCompareMessage(szMessage, nMessageLen, pOneInput->CompareInchiFlags[0]);
11520 : :
11521 [ + - - + ]: 3 : if (pOneInput->CompareInchiFlags[1][0] || pOneInput->CompareInchiFlags[1][1])
11522 : : {
11523 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "Disconnected: ", "; ");
11524 : 0 : retcomp1 = FillOutCompareMessage(szMessage, nMessageLen, pOneInput->CompareInchiFlags[1]);
11525 : : }
11526 : : /* add a metal warning */
11527 [ - + - - ]: 3 : if (bHasMetal && nInitLenMessage < (len = (int)strlen(szMessage)))
11528 : : {
11529 : 0 : char szMetal[] = " (Metal compound)";
11530 : : int shift;
11531 [ # # ]: 0 : if (len + (int)sizeof(szMetal) > nMessageLen)
11532 : : {
11533 : 0 : len = nMessageLen - (int)sizeof(szMetal);
11534 : : }
11535 : 0 : shift = nInitLenMessage + (int)sizeof(szMetal) - 1;
11536 : 0 : memmove(szMessage + shift, szMessage + nInitLenMessage, ((long long)len - nInitLenMessage) * sizeof(szMessage[0])); /* djb-rwth: cast operator added */
11537 : 0 : memcpy(szMessage + nInitLenMessage, szMetal, sizeof(szMetal) - sizeof(szMessage[0]));
11538 : 0 : szMessage[shift + len - nInitLenMessage] = '\0';
11539 : : }
11540 : :
11541 : 3 : retcomp = inchi_min(retcomp, retcomp1);
11542 : :
11543 [ + + ]: 3 : if (retcomp < 0 &&
11544 [ - + ]: 2 : (ip_inp->bINChIOutputOptions2 & INCHI_OUT_MISMATCH_AS_ERROR)
11545 : : )
11546 : : {
11547 : 0 : ret = RI_ERR_MISMATCH;
11548 : : /* AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "* Treated as error by user supplied option", "; "); */
11549 : 0 : (*num_errors)++;
11550 : 0 : goto dealloc;
11551 : : }
11552 : :
11553 : 3 : ulProcessingTime += InchiTimeElapsed(ic, pulTStart);
11554 : : }
11555 : :
11556 : 3 : ret = 0;
11557 : :
11558 : 3 : dealloc:
11559 : : /* Deallocate */
11560 [ - + ]: 3 : if (ret)
11561 : : {
11562 [ # # ]: 0 : if (ret < 0)
11563 : : {
11564 [ # # ]: 0 : if (ret == CT_USER_QUIT_ERR)
11565 : : {
11566 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "*Terminated by the user*", "; ");
11567 : : }
11568 : : else
11569 : : {
11570 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "*Conversion failed*", "; ");
11571 : : }
11572 : : }
11573 : : else
11574 : : {
11575 : : int iRec, iMob, iComp, nComp, len; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
11576 : : char szTemp[128];
11577 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, "*Conversion failed on component(s)", "; ");
11578 : 0 : len = (int)strlen(szMessage); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
11579 [ # # ]: 0 : for (iRec = 0; iRec < INCHI_NUM; iRec++)
11580 : : {
11581 [ # # ]: 0 : for (iMob = bHasSomeFixedH ? TAUT_NON : TAUT_YES; iMob < TAUT_NUM; iMob++)
11582 : : {
11583 : 0 : nComp = pOneInput->nNumComponents[iRec][iMob];
11584 [ # # ]: 0 : if (!pStruct[iRec][iMob])
11585 : : {
11586 : 0 : continue;
11587 : : }
11588 [ # # ]: 0 : for (iComp = 0; iComp < nComp; iComp++)
11589 : : {
11590 [ # # ]: 0 : if (pStruct[iRec][iMob][iComp].nError)
11591 : : {
11592 : 0 : char* szFormula = pOneInput->pInpInChI[iRec][iMob][iComp].szHillFormula;
11593 [ # # ]: 0 : sprintf(szTemp,
11594 : : #if ( FIX_DALKE_BUGS == 1 )
11595 : : " %s%s%d(%.96s)",
11596 : : #else
11597 : : " %s%s%d(%s)",
11598 : : #endif
11599 [ # # # # ]: 0 : !bHasSomeReconnected ? "" : iRec ? "R" : "D",
11600 [ # # # # ]: 0 : !bHasSomeFixedH ? "" : iMob ? "M" : "F",
11601 : : iComp + 1, szFormula ? szFormula : "???");
11602 : 0 : AddOneMsg(szMessage, (int)strlen(szMessage), nMessageLen, szTemp, NULL);
11603 : : }
11604 : : }
11605 : : }
11606 : : }
11607 : : } /* if ( ret > 0 ) */
11608 : : } /* if ( ret ) */
11609 : :
11610 : :
11611 : 3 : InchiTimeGet(pulTStart);
11612 : :
11613 : : /* Print one structure report */
11614 [ + - + - : 3 : if (szMessage && szMsg && nMsgLen > 1) /* djb-rwth: additional condition for szMessage */
+ - ]
11615 : : {
11616 [ + - ]: 3 : int len = inchi_min((int)strlen(szMessage), nMsgLen - 1);
11617 [ + + ]: 3 : if (len > 0)
11618 : : {
11619 : 2 : memcpy(szMsg, szMessage, len);
11620 : 2 : szMsg[len] = '\0';
11621 : : }
11622 : : else
11623 : : {
11624 : 1 : szMsg[0] = '\0';
11625 : : }
11626 : : }
11627 : :
11628 [ + - + + ]: 3 : if (szMessage && (nInitLenMessage < (int)strlen(szMessage))) /* djb-rwth: additional condition for szMessage */
11629 : : {
11630 : 2 : inchi_ios_eprint(pLog, "%s\n", szMessage);
11631 : : }
11632 : : #ifndef TARGET_API_LIB
11633 : : else
11634 : : {
11635 : : /*^^^inchi_ios_eprint( stderr, "%s\r", szMessage );*/
11636 : : inchi_fprintf(stderr, "%s\r", szMessage);
11637 : : }
11638 : : #endif
11639 : :
11640 : 3 : FreeStrFromINChI(pStruct, pOneInput->nNumComponents);
11641 : 3 : FreeInpInChI(pOneInput);
11642 [ - + ]: 3 : if (*pszCurHdr)
11643 : : {
11644 [ # # ]: 0 : inchi_free(*pszCurHdr);
11645 : 0 : *pszCurHdr = NULL;
11646 : : }
11647 : :
11648 : : INCHI_HEAPCHK
11649 : :
11650 : 3 : ulProcessingTime += InchiTimeElapsed(ic, pulTStart); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
11651 : :
11652 : 3 : return ret;
11653 : : }
11654 : :
11655 : :
11656 : : /****************************************************************************/
11657 : 4 : int DetectAndExposePolymerInternals(INCHI_IOSTREAM* is)
11658 : : {
11659 : 4 : int i, j, elindex, ret = 0, nheavy = 0,
11660 : 4 : nstars = 0, zlen = 0, star0 = 0, i_last_sym,
11661 : 4 : slen = 0, i2 = 0, ninsert = 0, kinsert, lead_pos, nc, ntimes, nc_max;
11662 : : const char* p, * pz, * pz2, * pr, * pend, * q;
11663 : 4 : char prev_layer_symbol = '0';
11664 : 4 : char element[3], * tmpstr = NULL, * edited_s = NULL;
11665 : 4 : int* insert_pos = NULL; /* inserts go before insert_pos[k] */
11666 : 4 : char* s = NULL, * s2 = NULL;
11667 : : int slength;
11668 : :
11669 : 4 : slength = is->s.nUsedLength;
11670 : 4 : s = (char*)inchi_calloc(2 * (long long)slength + 32, sizeof(char)); /* djb-rwth: cast operator added */
11671 [ - + ]: 4 : if (!s)
11672 : : {
11673 : 0 : goto endf;
11674 : : }
11675 : :
11676 : : /* Remove but save a tail (AuxInfo, InChIKey, etc.) if any */
11677 : 4 : strcpy(s, is->s.pStr);
11678 [ + + ]: 126 : for (i = 0; i < slength; i++)
11679 : : {
11680 [ - + ]: 122 : if (isspace(UCINT s[i]))
11681 : : {
11682 : 0 : i2 = i;
11683 : 0 : break;
11684 : : }
11685 : : }
11686 [ - + ]: 4 : if (i2)
11687 : : {
11688 : 0 : s2 = (char*)inchi_calloc((long long)slength - (long long)i2 + 2, sizeof(char)); /* djb-rwth: cast operators added */
11689 [ # # ]: 0 : if (!s2) goto endf;
11690 : 0 : strcpy(s2, s + i2);
11691 : 0 : s[i2] = '\0';
11692 : : }
11693 : :
11694 : 4 : i_last_sym = strlen(s) - 1;
11695 : 4 : p = strstr(s, "InChI=1");
11696 [ - + ]: 4 : if (!p)
11697 : : {
11698 : 0 : goto endf;
11699 : : }
11700 : :
11701 : 4 : pz = strstr(p, "/z");
11702 [ + + ]: 4 : if (!pz)
11703 : : {
11704 : 3 : goto endf;
11705 : : }
11706 : 1 : pz++;
11707 : :
11708 : : /* Check formula */
11709 : 1 : p = strchr(p, '/');
11710 [ - + ]: 1 : if (!p) /* djb-rwth: fixing coverity ID #499505 */
11711 : : {
11712 : 0 : goto endf;
11713 : : }
11714 : 1 : p++;
11715 : 1 : pend = strchr(p, '/');
11716 : 1 : ntimes = 1;
11717 [ + + ]: 5 : while (p != pend)
11718 : : {
11719 [ - + ]: 4 : if (isdigit(*p))
11720 : : {
11721 : 0 : ntimes = (int)inchi_strtol(p, &q, 10);
11722 : : #if ( CHECK_STRTOL_ATNUMB==1 )
11723 [ # # # # ]: 0 : if (ntimes > MAX_ATOMS || ntimes < 0)
11724 : : {
11725 : : #if (FIX_GAF_2020_25607 == 1)
11726 : 0 : ret = RI_ERR_SYNTAX;
11727 : 0 : goto endf;
11728 : : #else
11729 : : return RI_ERR_SYNTAX; /* syntax error */
11730 : : #endif
11731 : : }
11732 : : #endif
11733 : 0 : p = q;
11734 : : }
11735 : : else
11736 : : {
11737 [ - + ]: 4 : if (*p == '.')
11738 : : {
11739 : 0 : ntimes = 1;
11740 : : }
11741 : : }
11742 : :
11743 [ - + ]: 4 : if (!isupper(UCINT * p))
11744 : : {
11745 : 0 : ret = -1;
11746 : 0 : goto endf;
11747 : : }
11748 : :
11749 : 4 : j = 0;
11750 : 4 : element[j++] = *p++;
11751 [ + - + + ]: 4 : if (*p && islower(UCINT * p))
11752 : : {
11753 : 1 : element[j++] = *p++;
11754 : : }
11755 : 4 : element[j++] = '\0';
11756 [ + - + + ]: 4 : if (*p && isdigit(UCINT * p))
11757 : : {
11758 : 3 : elindex = (int)inchi_strtol(p, &q, 10);
11759 : : #if ( CHECK_STRTOL_ATNUMB==1 )
11760 [ + - - + ]: 3 : if (elindex > MAX_ATOMS || elindex < 0)
11761 : : {
11762 : : #if (FIX_GAF_2020_25607 == 1)
11763 : 0 : ret = RI_ERR_SYNTAX;
11764 : 0 : goto endf;
11765 : : #else
11766 : : return RI_ERR_SYNTAX; /* syntax error */
11767 : : #endif
11768 : :
11769 : : }
11770 : : #endif
11771 : 3 : p = q;
11772 : : }
11773 : : else
11774 : : {
11775 : 1 : elindex = 1;
11776 : : }
11777 [ - + ]: 4 : if (!elindex)
11778 : : {
11779 : 0 : ret = -1;
11780 : 0 : goto endf;
11781 : : }
11782 : : #if ( FIX_GAF_2019_2==1 )
11783 [ - + ]: 4 : if (ERR_ELEM == get_periodic_table_number(element))
11784 : : {
11785 : 0 : ret = -1;
11786 : 0 : goto endf;
11787 : : }
11788 : : #endif
11789 [ + + - + ]: 4 : if (element[0] != 'H' || element[1])
11790 : : {
11791 : 3 : nheavy += ntimes * elindex;
11792 : : }
11793 : :
11794 [ - + ]: 4 : if (*p == '.')
11795 : : {
11796 : 0 : p++;
11797 : : }
11798 : : }
11799 : :
11800 : : #if ( FIX_GAF_2019_2==1 )
11801 : 1 : insert_pos = (int*)inchi_calloc((long long)is->s.nUsedLength + 1, sizeof(int)); /* djb-rwth: cast operator added */
11802 : : #else
11803 : : /* max num of insert positions is 2 in formulas + Npolymeric units, the latter may not be > nheavy */
11804 : : insert_pos = (int*)inchi_calloc(nheavy + 32, sizeof(int));
11805 : : #endif
11806 [ - + ]: 1 : if (!insert_pos)
11807 : : {
11808 : 0 : ret = -2;
11809 : 0 : goto endf;
11810 : : }
11811 : :
11812 : 1 : ninsert = 0;
11813 [ + - ]: 1 : if (pend)
11814 : : {
11815 : 1 : insert_pos[ninsert] = (int)(pend - s);
11816 : : }
11817 : : else
11818 : : {
11819 : 0 : insert_pos[ninsert] = i_last_sym;
11820 : : }
11821 : 1 : ninsert++;
11822 : :
11823 : : /* Check hidden stars */
11824 : 1 : lead_pos = (int)(pz - s);
11825 : 1 : pend = strchr(pz, '/');
11826 [ - + ]: 1 : if (pend)
11827 : : {
11828 : 0 : zlen = (int)(pend - pz);
11829 : : }
11830 : : else
11831 : : {
11832 : 1 : zlen = (int)strlen(pz);
11833 : : }
11834 : 1 : tmpstr = (char*)inchi_calloc((long long)zlen + 32, sizeof(char)); /* djb-rwth: cast operator added */
11835 [ - + ]: 1 : if (!tmpstr)
11836 : : {
11837 : 0 : ret = -2;
11838 : 0 : goto endf;
11839 : : }
11840 : 1 : memcpy(tmpstr, pz, zlen);
11841 : 1 : ret = DetectHiddenPolymerStuff(tmpstr, zlen, &ninsert, insert_pos, lead_pos, &nstars);
11842 [ - + ]: 1 : if (ret)
11843 : : {
11844 : 0 : goto endf;
11845 : : }
11846 [ + - ]: 1 : if (!nstars)
11847 : : {
11848 : 1 : goto endf;
11849 : : }
11850 : :
11851 : : /* Have second '/z' ? */
11852 : 0 : pr = strstr(s, "/r");
11853 [ # # ]: 0 : if (pr)
11854 : : {
11855 : 0 : pr++;
11856 : :
11857 : 0 : pend = strchr(pr, '/');
11858 [ # # ]: 0 : if (pend)
11859 : : {
11860 : 0 : insert_pos[ninsert] = (int)(pend - s);
11861 : : }
11862 : : else
11863 : : {
11864 : 0 : insert_pos[ninsert] = i_last_sym;
11865 : : }
11866 : 0 : ninsert++;
11867 : :
11868 : 0 : pz2 = strstr(pr, "/z");
11869 [ # # ]: 0 : if (pz2)
11870 : : {
11871 : 0 : pz2++;
11872 : 0 : lead_pos = (int)(pz2 - s);
11873 : 0 : pend = strchr(pz2, '/');
11874 [ # # ]: 0 : if (pend)
11875 : : {
11876 : 0 : zlen = (int)(pend - pz2);
11877 : : }
11878 : : else
11879 : : {
11880 : 0 : zlen = (int)strlen(pz2);
11881 : : }
11882 [ # # ]: 0 : if (tmpstr)
11883 : : {
11884 [ # # ]: 0 : inchi_free(tmpstr);
11885 : : }
11886 : 0 : tmpstr = (char*)inchi_calloc((long long)zlen + 32, sizeof(char)); /* djb-rwth: cast operator added */
11887 [ # # ]: 0 : if (!tmpstr)
11888 : : {
11889 : 0 : ret = -2;
11890 : 0 : goto endf;
11891 : : }
11892 : 0 : memcpy(tmpstr, pz2, zlen);
11893 : 0 : nstars = 0;
11894 : 0 : ret = DetectHiddenPolymerStuff(tmpstr, zlen, &ninsert, insert_pos, lead_pos, &nstars);
11895 [ # # ]: 0 : if (ret)
11896 : : {
11897 : 0 : goto endf;
11898 : : }
11899 : : }
11900 : : }
11901 : :
11902 : 0 : slen = (int)strlen(s);
11903 : 0 : edited_s = (char*)inchi_calloc((long long)slen * 100 + 32 * 10 * (long long)ninsert, sizeof(char)); /* high reservation */ /* djb-rwth: cast operator added */
11904 [ # # ]: 0 : if (!edited_s)
11905 : : {
11906 : 0 : ret = -2;
11907 : 0 : goto endf;
11908 : : }
11909 : :
11910 : : /* Edits */
11911 : 0 : nc = 0;
11912 : 0 : nc_max = slen * 100 + 32 * 10 * ninsert; /* djb-rwth: fixing oss-fuzz issue #384549256 */
11913 : 0 : kinsert = 0;
11914 : 0 : star0 = nheavy + 1;
11915 [ # # ]: 0 : for (i = 0; i < slen; i++)
11916 : : {
11917 [ # # # # ]: 0 : if (kinsert < ninsert && i == insert_pos[kinsert])
11918 : : {
11919 [ # # # # ]: 0 : if (kinsert == 0 || prev_layer_symbol == 'r')
11920 : : {
11921 : 0 : sprintf(tmpstr, ".%dZz", nstars);
11922 : 0 : star0 = nheavy + 1; /* reset star numbers pool */
11923 : 0 : prev_layer_symbol = '0'; /* avoid printing ';' also */
11924 : : }
11925 : : else
11926 : : {
11927 : 0 : sprintf(tmpstr, "%d,%d-", star0, star0 + 1);
11928 : 0 : star0 += 2;
11929 : : }
11930 : 0 : kinsert++;
11931 [ # # ]: 0 : for (j = 0; j < (int)strlen(tmpstr); j++)
11932 : : {
11933 [ # # ]: 0 : if (nc < nc_max)
11934 : : {
11935 : 0 : edited_s[nc] = tmpstr[j];
11936 : 0 : nc++;
11937 : : }
11938 : : }
11939 : : }
11940 : :
11941 [ # # # # ]: 0 : if ((i == i_last_sym) && (nc < nc_max))
11942 : : {
11943 : 0 : edited_s[nc++] = s[i];
11944 : : }
11945 : :
11946 [ # # # # ]: 0 : if (s[i] == '/' || i == i_last_sym)
11947 : : {
11948 [ # # # # ]: 0 : if (prev_layer_symbol != '0' &&
11949 [ # # ]: 0 : prev_layer_symbol != 'f' &&
11950 [ # # ]: 0 : prev_layer_symbol != 'z' &&
11951 [ # # ]: 0 : prev_layer_symbol != 'p' &&
11952 [ # # ]: 0 : prev_layer_symbol != 'r' &&
11953 : : prev_layer_symbol != 's'
11954 : : )
11955 : 0 : {
11956 : 0 : char addon = ';';
11957 [ # # ]: 0 : if (prev_layer_symbol == 'm')
11958 : : {
11959 : 0 : addon = '.';
11960 : : }
11961 [ # # ]: 0 : for (j = 0; j < nstars; j++)
11962 : : {
11963 [ # # ]: 0 : if (nc < nc_max)
11964 : : {
11965 : 0 : edited_s[nc++] = addon;
11966 : : }
11967 : : }
11968 : : }
11969 [ # # ]: 0 : else if (prev_layer_symbol == 'f')
11970 : : {
11971 [ # # # # ]: 0 : if (s[i - 1] != 'f' && s[i - 2] != '/')
11972 : : {
11973 : 0 : sprintf(tmpstr, ".%dZz", nstars);
11974 [ # # ]: 0 : for (j = 0; j < (int)strlen(tmpstr); j++)
11975 : : {
11976 [ # # ]: 0 : if (nc < nc_max)
11977 : : {
11978 : 0 : edited_s[nc] = tmpstr[j];
11979 : 0 : nc++;
11980 : : }
11981 : : }
11982 : : }
11983 : : }
11984 : :
11985 [ # # ]: 0 : if (i != i_last_sym)
11986 : : {
11987 : 0 : prev_layer_symbol = s[i + 1];
11988 : : }
11989 : : else
11990 : : {
11991 : 0 : break;
11992 : : }
11993 : : }
11994 [ # # ]: 0 : if (nc < nc_max)
11995 : : {
11996 : 0 : edited_s[nc] = s[i];
11997 : 0 : nc++;
11998 : : }
11999 : : }
12000 [ # # ]: 0 : if (nc < nc_max)
12001 : : {
12002 : 0 : edited_s[nc] = '\0';
12003 : : }
12004 : 0 : inchi_strbuf_close(&is->s);
12005 [ # # ]: 0 : inchi_ios_print(is, "%-s%-s\n", edited_s, s2 ? s2 : "");
12006 : :
12007 : 4 : endf:
12008 [ + - ]: 4 : if (s)
12009 : : {
12010 [ + - ]: 4 : inchi_free(s);
12011 : : }
12012 [ - + ]: 4 : if (s2)
12013 : : {
12014 [ # # ]: 0 : inchi_free(s2);
12015 : : }
12016 [ - + ]: 4 : if (edited_s)
12017 : : {
12018 [ # # ]: 0 : inchi_free(edited_s);
12019 : : }
12020 [ + + ]: 4 : if (tmpstr)
12021 : : {
12022 [ + - ]: 1 : inchi_free(tmpstr);
12023 : : }
12024 [ + + ]: 4 : if (insert_pos)
12025 : : {
12026 [ + - ]: 1 : inchi_free(insert_pos);
12027 : : }
12028 : :
12029 : 4 : return ret;
12030 : : }
12031 : :
12032 : :
12033 : : /****************************************************************************/
12034 : 1 : int DetectHiddenPolymerStuff(char* tmpstr,
12035 : : int tmpstrlen,
12036 : : int* ninsert,
12037 : : int* insert_pos,
12038 : : int insert_lead_offset,
12039 : : int* nstars)
12040 : : {
12041 : : char c;
12042 : 1 : int opened, skip, i, i0, closed, ret = 0;
12043 : :
12044 : 1 : *nstars = opened = skip = i0 = 0;
12045 : 1 : closed = 1;
12046 [ + + ]: 18 : for (i = 0; i < tmpstrlen; i++)
12047 : : {
12048 : 17 : c = tmpstr[i];
12049 : :
12050 [ + + ]: 17 : if (c == '(')
12051 : : {
12052 [ - + ]: 1 : if (!closed)
12053 : : {
12054 : 0 : ret = -3; goto endf;
12055 : : }
12056 : 1 : opened = 1;
12057 : 1 : skip = 0;
12058 : 1 : i0 = i;
12059 : : }
12060 [ + + ]: 16 : else if (c == ')')
12061 : : {
12062 [ - + ]: 1 : if (!opened)
12063 : : {
12064 : 0 : ret = -3; goto endf;
12065 : : }
12066 [ - + ]: 1 : if (!skip)
12067 : : {
12068 : 0 : (*nstars) += 2;
12069 : 0 : insert_pos[(*ninsert)] = i0 + 1 + insert_lead_offset;
12070 : 0 : (*ninsert)++;
12071 : : }
12072 : 1 : opened = 0;
12073 : : }
12074 [ + + ]: 15 : else if (c == '-')
12075 : : {
12076 : 4 : skip = 1;
12077 : : }
12078 : : }
12079 : :
12080 : 1 : endf:
12081 : 1 : return ret;
12082 : : }
12083 : :
12084 : :
12085 : : /****************************************************************************
12086 : : Create empty sp3 segment
12087 : : ****************************************************************************/
12088 : 0 : static int SegmentSp3CreateEmpty(const char* str,
12089 : : int bMobileH,
12090 : : INChI* pInpInChI[],
12091 : : int nNumComponents,
12092 : : int state,
12093 : : int* pbAbc)
12094 : : {
12095 : 0 : int ret = 0;
12096 : : int iComponent;
12097 : 0 : int len0 = 0;
12098 [ # # # # ]: 0 : int bIso = (state == IST_MOBILE_H_ISO_SP3 || state == IST_FIXED_H_ISO_SP3);
12099 : 0 : INChI_Stereo** pStereo = NULL;
12100 : 0 : INChI* pInChI = pInpInChI[bMobileH];
12101 : :
12102 [ # # ]: 0 : for (iComponent = 0; iComponent < nNumComponents; iComponent++)
12103 : : {
12104 : 0 : INChI* pIsoInChI = &pInChI[iComponent];
12105 [ # # ]: 0 : pStereo = bIso ? &pIsoInChI->StereoIsotopic : &pIsoInChI->Stereo;
12106 [ # # ]: 0 : if (!*pStereo)
12107 : : {
12108 [ # # ]: 0 : if (!(*pStereo = (INChI_Stereo*)inchi_calloc(1, sizeof(**pStereo))))
12109 : : {
12110 : 0 : return RI_ERR_ALLOC;
12111 : : }
12112 : : }
12113 : : /* allocate empty sp3 stereo */
12114 [ # # ]: 0 : if ((!pStereo[0]->b_parity &&
12115 [ # # ]: 0 : !(pStereo[0]->b_parity = (S_CHAR*)inchi_calloc((long long)len0 + 1, sizeof(pStereo[0]->b_parity[0])))) ||
12116 [ # # ]: 0 : (!pStereo[0]->nBondAtom1 &&
12117 [ # # ]: 0 : !(pStereo[0]->nBondAtom1 = (AT_NUMB*)inchi_calloc((long long)len0 + 1, sizeof(pStereo[0]->nBondAtom1[0])))) ||
12118 [ # # ]: 0 : (!pStereo[0]->nBondAtom2 &&
12119 [ # # ]: 0 : !(pStereo[0]->nBondAtom2 = (AT_NUMB*)inchi_calloc((long long)len0 + 1, sizeof(pStereo[0]->nBondAtom2[0]))))) /* djb-rwth: cast operator added; addressing LLVM warnings */
12120 : : {
12121 : : /* cleanup */
12122 [ # # ]: 0 : if (pStereo[0]->b_parity)
12123 : : {
12124 : : INCHI_HEAPCHK
12125 [ # # ]: 0 : inchi_free(pStereo[0]->b_parity);
12126 : 0 : pStereo[0]->b_parity = NULL;
12127 : : }
12128 [ # # ]: 0 : if (pStereo[0]->nBondAtom1)
12129 : : {
12130 : : INCHI_HEAPCHK
12131 [ # # ]: 0 : inchi_free(pStereo[0]->nBondAtom1);
12132 : 0 : pStereo[0]->nBondAtom1 = NULL;
12133 : : }
12134 [ # # ]: 0 : if (pStereo[0]->nBondAtom2)
12135 : : {
12136 : : INCHI_HEAPCHK
12137 [ # # ]: 0 : inchi_free(pStereo[0]->nBondAtom2);
12138 : 0 : pStereo[0]->nBondAtom2 = NULL;
12139 : : }
12140 : 0 : return RI_ERR_ALLOC;
12141 : : }
12142 : 0 : pStereo[0]->nCompInv2Abs = NO_VALUE_INT;
12143 : : }
12144 : 0 : ret = nNumComponents + 1;
12145 : :
12146 : 0 : return ret;
12147 : : }
12148 : :
12149 : :
12150 : : /****************************************************************************/
12151 : 3 : static int SegmentSp3StoreStereoCenters(int* pbAbc,
12152 : : const char* pStart,
12153 : : const char* pEnd,
12154 : : int pInChI_iComponent_nNumberOfAtoms,
12155 : : INChI_Stereo* PStereo_0)
12156 : : {
12157 : 3 : const char parity_type[] = "-+u?";
12158 : : const char* p, * q, * r;
12159 : : AT_NUMB nAtom1;
12160 : : int iAtom;
12161 : : int atomParity;
12162 : 3 : int base = 10;
12163 : :
12164 [ - + ]: 3 : if (*pbAbc == 1)
12165 : : {
12166 [ # # ]: 0 : for (p = (char*)pStart, iAtom = 0; p < pEnd; iAtom++)
12167 : : {
12168 [ # # # # ]: 0 : if ((nAtom1 = (AT_NUMB)inchi_strtol(p, &p, base)) &&
12169 : 0 : (atomParity = (int)inchi_strtol(p, &p, 10),
12170 [ # # # # ]: 0 : AB_MIN_KNOWN_PARITY <= atomParity && atomParity <= AB_MAX_KNOWN_PARITY))
12171 : : {
12172 : : ; /* okay */
12173 : : }
12174 : : else
12175 : : {
12176 : 0 : return RI_ERR_SYNTAX; /* syntax error */
12177 : : }
12178 [ # # ]: 0 : if (nAtom1 > pInChI_iComponent_nNumberOfAtoms)
12179 : : {
12180 : 0 : return RI_ERR_SYNTAX;
12181 : : }
12182 : 0 : PStereo_0->t_parity[iAtom] = atomParity;
12183 : 0 : PStereo_0->nNumber[iAtom] = nAtom1;
12184 [ # # # # ]: 0 : if (iAtom && !(PStereo_0->nNumber[iAtom - 1] < nAtom1))
12185 : : {
12186 : 0 : return RI_ERR_SYNTAX; /* syntax error */
12187 : : }
12188 : : }
12189 : : }
12190 : : else
12191 : : {
12192 [ + + ]: 6 : for (p = (char*)pStart, iAtom = 0; p < pEnd; iAtom++, p += (*p == ','))
12193 : : {
12194 : 3 : nAtom1 = (AT_NUMB)inchi_strtol(p, &q, 10);
12195 : : #if ( CHECK_STRTOL_ATNUMB==1 )
12196 [ - + ]: 3 : if (nAtom1 > MAX_ATOMS || nAtom1 < 0)
12197 : : {
12198 : 0 : return RI_ERR_SYNTAX;
12199 : : }
12200 : : #endif
12201 [ - + ]: 3 : if (!(r = strchr((char*)parity_type, *q)))
12202 : : {
12203 : 0 : return RI_ERR_SYNTAX; /* syntax error */
12204 : : }
12205 : 3 : p = q + 1;
12206 : 3 : atomParity = (int)(r - parity_type) + 1;
12207 : 3 : PStereo_0->t_parity[iAtom] = atomParity;
12208 : 3 : PStereo_0->nNumber[iAtom] = nAtom1;
12209 [ - + - - ]: 3 : if (iAtom && !(PStereo_0->nNumber[iAtom - 1] < nAtom1))
12210 : : {
12211 : 0 : return RI_ERR_SYNTAX;
12212 : : }
12213 : : }
12214 : : }
12215 : 3 : PStereo_0->nNumberOfStereoCenters = iAtom;
12216 : : /*if ( iAtom ) {*/
12217 : 3 : PStereo_0->nCompInv2Abs = NO_VALUE_INT; /* unknown yet */
12218 : :
12219 [ - + ]: 3 : if (p != pEnd)
12220 : : {
12221 : 0 : return RI_ERR_SYNTAX;
12222 : : }
12223 : :
12224 : 3 : return 0;
12225 : : }
12226 : :
12227 : :
12228 : : /****************************************************************************
12229 : : Treat multiplier-served components
12230 : : ****************************************************************************/
12231 : 3 : static int SegmentSp3CopyMultiplierCovered(int mpy_component,
12232 : : int iComponent,
12233 : : INChI* pInChI,
12234 : : int bIso,
12235 : : int nCpyType)
12236 : : {
12237 : 3 : int i, ret = 0;
12238 [ + + ]: 4 : for (i = 1; i < mpy_component; i++)
12239 : : {
12240 : 1 : ret = CopySegment(pInChI + iComponent + i, pInChI + iComponent, nCpyType, bIso, bIso);
12241 [ - + ]: 1 : if (!ret)
12242 : : {
12243 : 0 : ret = RI_ERR_SYNTAX;
12244 : : }
12245 [ - + ]: 1 : if (ret < 0)
12246 : : {
12247 : 0 : return ret;
12248 : : }
12249 : 1 : ret = CopySegment(pInChI + iComponent + i, pInChI + iComponent, CPY_SP3_M, bIso, bIso);
12250 [ - + ]: 1 : if (!ret)
12251 : : {
12252 : 0 : ret = RI_ERR_SYNTAX;
12253 : : }
12254 [ - + ]: 1 : if (ret < 0)
12255 : : {
12256 : 0 : return ret;
12257 : : }
12258 : : }
12259 : :
12260 : 3 : return ret;
12261 : : }
12262 : :
12263 : :
12264 : : /****************************************************************************
12265 : : Process the abbreviation
12266 : : ****************************************************************************/
12267 : 0 : static int SegmentSp3ProcessAbbreviation(int* mpy_component,
12268 : : int iComponent,
12269 : : int nNumComponents,
12270 : : int val,
12271 : : const char* q,
12272 : : int state,
12273 : : int* pbAbc,
12274 : : int bMobileH,
12275 : : int nCpyType,
12276 : : INChI* pInChI,
12277 : : INChI* pInpInChI_ALT_TAUT_bMobileH)
12278 : : {
12279 : 0 : int i, bIsoTo = -1, bIsoFrom = -1;
12280 : 0 : int ret = 0;
12281 : 0 : INChI* pInChIFrom = NULL;
12282 : :
12283 : : #if (FIX_DALKE_BUGS == 1)
12284 [ # # ]: 0 : if (iComponent + val > nNumComponents)
12285 : : {
12286 : 0 : return RI_ERR_SYNTAX;
12287 : : }
12288 : : #endif
12289 : : #if (FIX_GAF_ISSUES==1)
12290 : : if (iComponent < 0)
12291 : : {
12292 : : return RI_ERR_SYNTAX;
12293 : : }
12294 : : #endif
12295 : :
12296 [ # # # ]: 0 : switch (bMobileH)
12297 : : {
12298 : 0 : case TAUT_YES:
12299 [ # # ]: 0 : switch (state)
12300 : : {
12301 : 0 : case IST_MOBILE_H_ISO_SP3:
12302 [ # # ]: 0 : if (*q == 'm')
12303 : : {
12304 : : /* copy from mobile H to isotopic mobile H */
12305 : 0 : pInChIFrom = pInChI;
12306 : 0 : bIsoTo = 1;
12307 : 0 : bIsoFrom = 0;
12308 : : }
12309 : : else
12310 : : {
12311 [ # # ]: 0 : if (*q == 'e')
12312 : : {
12313 : : /* copy from mobile H to isotopic mobile H */
12314 : 0 : pInChIFrom = pInChI;
12315 : 0 : bIsoTo = 1;
12316 : 0 : bIsoFrom = -1; /* empty */
12317 : : }
12318 : : else
12319 : : {
12320 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
12321 : : }
12322 : : }
12323 : 0 : break;
12324 : 0 : default:
12325 : 0 : ret = RI_ERR_SYNTAX;
12326 : 0 : break;
12327 : : }
12328 : 0 : break;
12329 : 0 : case TAUT_NON:
12330 [ # # # ]: 0 : switch (state)
12331 : : {
12332 : 0 : case IST_FIXED_H_SP3:
12333 [ # # ]: 0 : if (*q == 'm')
12334 : : {
12335 : : /* copy from mobile H to fixed H */
12336 : 0 : pInChIFrom = pInpInChI_ALT_TAUT_bMobileH;
12337 : 0 : bIsoTo = 0;
12338 : 0 : bIsoFrom = 0;
12339 : : }
12340 : : else
12341 : : {
12342 [ # # ]: 0 : if (*q == 'e')
12343 : : {
12344 : : /* copy from mobile H to isotopic mobile H */
12345 : 0 : pInChIFrom = pInChI; /* djb-rwth: addressing coverity ID #499498 -- definitely not a copy-paste error */
12346 : 0 : bIsoTo = 1;
12347 : 0 : bIsoFrom = -1; /* empty */
12348 : : }
12349 : : else
12350 : : {
12351 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
12352 : : }
12353 : : }
12354 : 0 : break;
12355 : 0 : case IST_FIXED_H_ISO_SP3:
12356 [ # # ]: 0 : if (*q == 'm')
12357 : : {
12358 : : /* copy from mobile H to fixed isotopic H */
12359 : 0 : pInChIFrom = pInpInChI_ALT_TAUT_bMobileH;
12360 : 0 : bIsoTo = 1;
12361 : 0 : bIsoFrom = 0;
12362 : : }
12363 : : else
12364 : : {
12365 [ # # ]: 0 : if (*q == 'M')
12366 : : {
12367 : : /* copy from isotopic mobile H to fixed isotopic H */
12368 : 0 : pInChIFrom = pInpInChI_ALT_TAUT_bMobileH;
12369 : 0 : bIsoTo = 1;
12370 : 0 : bIsoFrom = 1;
12371 : : }
12372 : : else
12373 : : {
12374 [ # # ]: 0 : if (*q == 'n')
12375 : : {
12376 : : /* copy from fixed H to fixed isotopic H */
12377 : 0 : pInChIFrom = pInChI;
12378 : 0 : bIsoTo = 1;
12379 : 0 : bIsoFrom = 0;
12380 : : }
12381 : : else
12382 : : {
12383 [ # # ]: 0 : if (*q == 'e')
12384 : : {
12385 : : /* copy from mobile H to isotopic mobile H */
12386 : 0 : pInChIFrom = pInChI;
12387 : 0 : bIsoTo = 1;
12388 : 0 : bIsoFrom = -1; /* empty */
12389 : : }
12390 : : else
12391 : : {
12392 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
12393 : : }
12394 : : }
12395 : : }
12396 : : }
12397 : 0 : break;
12398 : 0 : default:
12399 : 0 : ret = RI_ERR_SYNTAX;
12400 : 0 : break;
12401 : : }
12402 : 0 : break;
12403 : :
12404 : 0 : default:
12405 : 0 : ret = RI_ERR_SYNTAX;
12406 : 0 : break;
12407 : : }
12408 : :
12409 [ # # ]: 0 : if (ret < 0)
12410 : : {
12411 : 0 : return ret;
12412 : : }
12413 : :
12414 : : /* copy */
12415 [ # # ]: 0 : for (i = 0; i < val; i++)
12416 : : {
12417 : : /* djb-rwth: fixing oss-fuzz issue #26540 */
12418 : 0 : ret = CopySegment(pInChI + iComponent + i, pInChIFrom + iComponent + i, nCpyType, bIsoTo, bIsoFrom);
12419 [ # # ]: 0 : if (!ret)
12420 : : {
12421 : 0 : ret = RI_ERR_SYNTAX; /* syntax error */
12422 : : }
12423 [ # # ]: 0 : if (ret < 0)
12424 : : {
12425 : 0 : return ret;
12426 : : }
12427 [ # # ]: 0 : if (bIsoFrom >= 0)
12428 : : {
12429 [ # # ]: 0 : INChI_Stereo* pStereoTo = bIsoTo ? pInChI[iComponent + i].StereoIsotopic : pInChI[iComponent + i].Stereo;
12430 [ # # ]: 0 : if (pStereoTo)
12431 : : {
12432 : 0 : pStereoTo->nCompInv2Abs = NO_VALUE_INT; /* in case there in no /m segment after this */
12433 : : }
12434 : : }
12435 : : }
12436 : :
12437 : 0 : *mpy_component = val;
12438 : :
12439 : 0 : return ret;
12440 : : }
12441 : :
12442 : :
12443 : : /* Internal: a generic parser/extractor interface */
12444 : 0 : int extract_from_inchi_string(char* sinchi, InpInChI* OneInput)
12445 : : {
12446 : 0 : int ret = _IS_OKAY;
12447 : :
12448 : 0 : char* strHdr = NULL;
12449 : : SEGM_LINE Line;
12450 : 0 : SEGM_LINE* pLine = &Line;
12451 : : int pState, nErr;
12452 : 0 : const int bInChI2Structure = 1, bReadCoord = 1;
12453 : : int input_is_stdinchi, input_has_save_opt;
12454 : : unsigned char input_save_opt_bits;
12455 : : int end_of_data_reached, read_inchi_ok;
12456 : : INCHI_IOSTREAM tmpinputstream;
12457 : 0 : INCHI_IOSTREAM* pInp = &tmpinputstream;
12458 : 0 : INCHI_MODE nMode = 0;
12459 : : /* djb-rwth: removing redundant code */
12460 : :
12461 : 0 : nMode = (INCHI_MODE)530462;
12462 : :
12463 : 0 : memset(OneInput, 0, sizeof(*OneInput)); /* djb-rwth: memset_s C11/Annex K variant? */
12464 : 0 : memset(pLine, 0, sizeof(pLine[0])); /* djb-rwth: memset_s C11/Annex K variant? */
12465 : 0 : OneInput->polymer = NULL; /* v. 1.05 added */
12466 : 0 : OneInput->v3000 = NULL;
12467 : 0 : inchi_ios_init(pInp, INCHI_IOS_TYPE_STRING, NULL);
12468 : 0 : inchi_ios_print(pInp, "%-s", sinchi);
12469 : :
12470 : 0 : ret = DetectAndExposePolymerInternals(pInp);
12471 [ # # ]: 0 : if (ret)
12472 : : {
12473 : 0 : ret = _IS_ERROR;
12474 : 0 : goto exit_function;
12475 : : }
12476 : : /*strcpy(pInp->s.pStr, "InChI=1B/C4H4N4.2Zz/c1-5-2-7-4-8-3-6-1;;/h1-4H;;/z101-1-8(9,10-8,3,1,6,2,5,2,7,3,6,1,5,4,7,4,8)/b5-1-,5-2+,6-1+,6-3-,7-2+,7-4+,8-3+,8-4+;;");*/
12477 : 0 : ret = InChILine2Data(pInp,
12478 : : pLine,
12479 : : &strHdr,
12480 : : &pState,
12481 : : &nErr,
12482 : 0 : OneInput->pInpInChI,
12483 : 0 : OneInput->nNumComponents,
12484 : 0 : OneInput->nNumProtons,
12485 : 0 : OneInput->s,
12486 : : bReadCoord,
12487 : : bInChI2Structure,
12488 : : nMode,
12489 : : &input_is_stdinchi,
12490 : : &input_has_save_opt,
12491 : : &input_save_opt_bits,
12492 : : &OneInput->polymer,
12493 : : &OneInput->v3000);
12494 : :
12495 [ # # # # ]: 0 : end_of_data_reached = ret == RI_ERR_EOL || ret == RI_ERR_EOF;
12496 [ # # # # ]: 0 : read_inchi_ok = end_of_data_reached && !nErr;
12497 [ # # ]: 0 : if (!read_inchi_ok)
12498 : : {
12499 : 0 : ret = _IS_ERROR;
12500 : 0 : goto exit_function;
12501 : : }
12502 : : else
12503 : : {
12504 : 0 : ret = _IS_OKAY;
12505 : : }
12506 : :
12507 : :
12508 : 0 : exit_function:
12509 [ # # ]: 0 : if (strHdr)
12510 : : {
12511 [ # # ]: 0 : inchi_free(strHdr);
12512 : 0 : strHdr = NULL;
12513 : : }
12514 [ # # ]: 0 : if (pLine->str)
12515 : : {
12516 [ # # ]: 0 : inchi_free(pLine->str);
12517 : : }
12518 : 0 : inchi_ios_close(pInp);
12519 : :
12520 : 0 : return ret;
12521 : : }
12522 : :
12523 : :
12524 : : /****************************************************************************
12525 : : Extract_stereo_info_from_inchi_string
12526 : : ****************************************************************************/
12527 : 0 : int extract_stereo_info_from_inchi_string(char* sinchi,
12528 : : int nat,
12529 : : int* orig,
12530 : : int* at_stereo_mark_orig)
12531 : : {
12532 : : InpInChI OneInput;
12533 : 0 : int ret = _IS_OKAY;
12534 : 0 : int icomponent, i, bReconn = 0, bMobileH = 1, at_offset_component = 0;
12535 : :
12536 : : /* 0 is INCHI_PARITY_NONE */
12537 : 0 : memset(at_stereo_mark_orig, 0, ((long long)nat + 1) * sizeof(int)); /* djb-rwth: cast operator added; memset_s C11/Annex K variant? */
12538 : :
12539 : 0 : ret = extract_from_inchi_string(sinchi, &OneInput);
12540 [ # # # # ]: 0 : if (ret == _IS_ERROR || ret == _IS_FATAL)
12541 : : {
12542 : 0 : ret = _IS_ERROR;
12543 : 0 : goto exit_function;
12544 : : }
12545 : :
12546 : :
12547 : :
12548 [ # # ]: 0 : for (icomponent = 0; icomponent < OneInput.nNumComponents[bReconn][bMobileH]; icomponent++)
12549 : : {
12550 : 0 : INChI pI = OneInput.pInpInChI[bReconn][bMobileH][icomponent];
12551 [ # # ]: 0 : if (NULL == pI.Stereo)
12552 : : {
12553 : 0 : continue;
12554 : : }
12555 [ # # ]: 0 : for (i = 0; i < pI.Stereo->nNumberOfStereoCenters; i++)
12556 : : {
12557 : 0 : int icano = pI.Stereo->nNumber[i] + at_offset_component;
12558 : 0 : int iorig = orig[icano];
12559 : 0 : at_stereo_mark_orig[iorig] = pI.Stereo->t_parity[i];
12560 : : }
12561 : 0 : at_offset_component += pI.nNumberOfAtoms;
12562 : : }
12563 : :
12564 : 0 : exit_function:
12565 : 0 : FreeInpInChI(&OneInput);
12566 : :
12567 : 0 : return ret;
12568 : : }
12569 : :
12570 : :
12571 : : /****************************************************************************
12572 : : Extract all backbone bonds, in all units, from InChI string
12573 : : NB: as units are not 'inter-crossing' any bkbond belongs to some unique CRU
12574 : : ****************************************************************************/
12575 : 0 : int extract_all_backbone_bonds_from_inchi_string(char* sinchi,
12576 : : int* n_all_bkb_orig,
12577 : : int* orig,
12578 : : int* all_bkb_orig)
12579 : : {
12580 : : InpInChI OneInput;
12581 : 0 : int i, ret = _IS_OKAY;
12582 : :
12583 : 0 : ret = extract_from_inchi_string(sinchi, &OneInput);
12584 [ # # # # ]: 0 : if (ret == _IS_ERROR || ret == _IS_FATAL)
12585 : : {
12586 : 0 : ret = _IS_ERROR;
12587 : 0 : goto exit_function;
12588 : : }
12589 : :
12590 [ # # ]: 0 : for (i = 0; i < OneInput.polymer->n; i++)
12591 : : {
12592 : : int j;
12593 : 0 : OAD_PolymerUnit* u = OneInput.polymer->units[i];
12594 [ # # ]: 0 : for (j = 0; j < u->nbkbonds; j++)
12595 : : {
12596 : : int icano1, iorig1, icano2, iorig2;
12597 : 0 : icano1 = u->bkbonds[j][0];
12598 : 0 : iorig1 = orig[icano1];
12599 : 0 : icano2 = u->bkbonds[j][1];
12600 : 0 : iorig2 = orig[icano2];
12601 : :
12602 : 0 : all_bkb_orig[2 * (*n_all_bkb_orig)] = iorig1;
12603 : 0 : all_bkb_orig[2 * (*n_all_bkb_orig) + 1] = iorig2;
12604 : 0 : (*n_all_bkb_orig)++;
12605 : : }
12606 : : }
12607 : :
12608 : 0 : FreeInpInChI(&OneInput);
12609 : :
12610 : 0 : exit_function:
12611 : 0 : return ret;
12612 : : }
12613 : :
12614 : : #endif /* READ_INCHI_STRING */
|