Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : : #include <string.h>
8 : : #include <ctype.h>
9 : : #include <locale.h>
10 : :
11 : : #include "mode.h"
12 : : #include "ichimake.h"
13 : : #include "inchi_api.h"
14 : : #include "ichimain.h"
15 : : #include "ichister.h"
16 : : #include "ichi_io.h"
17 : : #include "ichitime.h"
18 : : #include "ichi_bns.h"
19 : :
20 : : #include "bcf_s.h"
21 : :
22 : : /*
23 : : Local functions
24 : : */
25 : :
26 : : int inp2spATOM(inp_ATOM* inp_at, int num_inp_at, sp_ATOM* at);
27 : : int GetElementAndCount(const char** f, char* szEl, int* count);
28 : : int CompareHillFormulas(const char* f1, const char* f2);
29 : : int CompareInchiStereo(INChI_Stereo* Stereo1,
30 : : INCHI_MODE nFlags1,
31 : : INChI_Stereo* Stereo2,
32 : : INCHI_MODE nFlags2);
33 : : int CompareReversedStereoINChI(INChI_Stereo* s1,
34 : : INChI_Stereo* s2);
35 : : int GetAtomOrdNbrInCanonOrd(struct tagCANON_GLOBALS* pCG,
36 : : inp_ATOM* norm_at,
37 : : AT_NUMB* nAtomOrdNbr,
38 : : AT_NUMB* nOrigAtNosInCanonOrd,
39 : : int num_at);
40 : : int FillOutCanonInfAtom(struct tagCANON_GLOBALS* pCG,
41 : : inp_ATOM* norm_at,
42 : : INF_ATOM_DATA* inf_norm_at_data,
43 : : int init_num_at,
44 : : int bIsotopic,
45 : : INChI* pINChI,
46 : : INChI_Aux* pINChI_Aux,
47 : : int bAbcNumbers,
48 : : INCHI_MODE nMode);
49 : : int FillOutOneCanonInfAtom(struct tagCANON_GLOBALS* pCG,
50 : : inp_ATOM* inp_norm_at,
51 : : INF_ATOM_DATA* inf_norm_at_data,
52 : : AT_NUMB* pStereoFlags,
53 : : int init_num_at,
54 : : int offset,
55 : : int offset_H,
56 : : int bIsotopic,
57 : : INChI* pINChI,
58 : : INChI_Aux* pINChI_Aux,
59 : : int bAbcNumbers,
60 : : INCHI_MODE nMode);
61 : : int FillOutInputInfAtom(inp_ATOM* inp_at,
62 : : INF_ATOM_DATA* inf_at_data,
63 : : int init_num_at,
64 : : int num_removed_H,
65 : : int bAdd_DT_to_num_H,
66 : : int nNumRemovedProtons,
67 : : NUM_H* nNumRemovedProtonsIsotopic,
68 : : int bIsotopic,
69 : : int bAbcNumbers);
70 : : int CheckCanonNumberingCorrectness(int num_atoms,
71 : : int num_at_tg,
72 : : sp_ATOM* at,
73 : : CANON_STAT* pCS,
74 : : CANON_GLOBALS* pCG,
75 : : int bTautomeric,
76 : : char* pStrErrStruct);
77 : : static int CompareDfsDescendants4CT(const void* a1, const void* a2, void*);
78 : : int GetSp3RelRacAbs(const INChI* pINChI, INChI_Stereo* Stereo);
79 : : #if ( READ_INCHI_STRING == 1 ) /* { */
80 : : int CompareReversedStereoINChI2(INChI_Stereo* s1, INChI_Stereo* s2, ICR* picr);
81 : : #endif
82 : :
83 : : /****************************************************************************/
84 : 161 : int inp2spATOM(inp_ATOM* inp_at, int num_inp_at, sp_ATOM* at)
85 : : {
86 : : int i, j, val, elname_len;
87 : :
88 : 161 : memset(at, 0, sizeof(at[0]) * num_inp_at); /* djb-rwth: memset_s C11/Annex K variant? */
89 : :
90 [ + + ]: 971 : for (i = 0; i < num_inp_at; i++)
91 : : {
92 : 810 : elname_len = sizeof(at[0].elname) - 1; /* djb-rwth: fixing coverity ID #499609 */
93 : 810 : strncpy(at[i].elname, inp_at[i].elname, elname_len);
94 : 810 : at[i].elname[elname_len] = '\0';
95 : 810 : at[i].el_number = (U_CHAR)get_periodic_table_number(at[i].elname);
96 : 810 : val = at[i].valence = inp_at[i].valence;
97 [ + + ]: 2160 : for (j = 0; j < val; j++)
98 : : {
99 : 1350 : at[i].neighbor[j] = inp_at[i].neighbor[j];
100 : 1350 : at[i].bond_type[j] = inp_at[i].bond_type[j];
101 : : }
102 : 810 : at[i].chem_bonds_valence = inp_at[i].chem_bonds_valence;
103 : 810 : at[i].orig_at_number = inp_at[i].orig_at_number;
104 : 810 : at[i].orig_compt_at_numb = inp_at[i].orig_compt_at_numb;
105 : 810 : at[i].endpoint = inp_at[i].endpoint;
106 : 810 : at[i].iso_atw_diff = inp_at[i].iso_atw_diff;
107 : 810 : at[i].num_H = inp_at[i].num_H;
108 : 810 : at[i].cFlags = inp_at[i].cFlags;
109 [ + + ]: 3240 : for (j = 0; j < NUM_H_ISOTOPES; j++)
110 : : {
111 : 2430 : at[i].num_iso_H[j] = inp_at[i].num_iso_H[j];
112 : : }
113 : 810 : at[i].charge = inp_at[i].charge;
114 : 810 : at[i].radical = inp_at[i].radical;
115 : :
116 : : #if ( FIND_RING_SYSTEMS == 1 )
117 : 810 : at[i].nBlockSystem = inp_at[i].nBlockSystem;
118 : 810 : at[i].bCutVertex = inp_at[i].bCutVertex;
119 : 810 : at[i].nRingSystem = inp_at[i].nRingSystem;
120 : 810 : at[i].nNumAtInRingSystem = inp_at[i].nNumAtInRingSystem;
121 : : #if ( FIND_RINS_SYSTEMS_DISTANCES == 1 )
122 : : at[i].nDistanceFromTerminal = inp_at[i].nDistanceFromTerminal;
123 : : #endif
124 : : #endif
125 : :
126 : : /*
127 : : at[i].x = inp_at[i].x;
128 : : at[i].y = inp_at[i].y;
129 : : at[i].z = inp_at[i].z;
130 : : */
131 : : }
132 : :
133 : 161 : return 0;
134 : : }
135 : :
136 : :
137 : : /****************************************************************************/
138 : 488 : int GetElementAndCount(const char** f, char* szEl, int* count)
139 : : {
140 : 488 : const char* p = *f;
141 : : char* q;
142 : 488 : int i = 0;
143 : :
144 : : /* djb-rwth: fixing oss-fuzz issue #37224 */
145 [ + - + + ]: 488 : if (p && *p)
146 : : {
147 [ + - ]: 372 : if (isupper(UCINT * p))
148 : : {
149 : 372 : szEl[i++] = *p++;
150 [ + + + + ]: 372 : if (*p && islower(UCINT * p))
151 : : {
152 : 120 : szEl[i++] = *p++;
153 : : }
154 : 372 : szEl[i] = '\0';
155 [ + + + + ]: 372 : if (1 == i && szEl[0] == 'C')
156 : : {
157 : 84 : szEl[0] = 'A'; /* less than any element: */
158 : : /* carbon-containing compounds should be first */
159 : : }
160 [ + + + + ]: 372 : if (*p && isdigit(UCINT * p))
161 : : {
162 : 166 : *count = strtol(p, &q, 10);
163 : 166 : p = q;
164 : : }
165 : : else
166 : : {
167 : 206 : *count = 1;
168 : : }
169 : 372 : *f = p; /* next element; */
170 : 372 : return 1;
171 : : }
172 : 0 : return -1; /* not a chemical formula */
173 : : }
174 : : /* v. 1.06 Changed "Zz" to "Zzz" as "Zz" is valid symbol now */
175 : 116 : strcpy(szEl, "Zzz");
176 : : /*strcpy( szEl, "Zz" );*/
177 : : /* zero termination 'element' is larger than any other element */
178 : 116 : *count = 99999; /* zero termination 'element count' is larger than any other count */
179 : 116 : return 0;
180 : : }
181 : :
182 : :
183 : : /*
184 : : CompareHillFormulas( ... )
185 : :
186 : : E1 < E2 if strcmp( E1, E2) < 0 OR E2 is empty and E1 is not
187 : : n1 < n2 if value n1 > n2
188 : :
189 : : Sorting order:
190 : :
191 : : C10H22N
192 : : C10H22
193 : : C2
194 : : Ag2Cl2
195 : : Ag2Cl
196 : : Ag2F2
197 : : Ag2
198 : : AgCl
199 : : AgF
200 : : F6S
201 : : F2S
202 : : */
203 : :
204 : :
205 : : /****************************************************************************/
206 : 0 : int CompareHillFormulas(const char* f1, const char* f2)
207 : : {
208 : : char szEl1[4], szEl2[4];
209 : : int count1, count2, ret1, ret2, ret;
210 : :
211 : : do
212 : : {
213 : 0 : ret1 = GetElementAndCount(&f1, szEl1, &count1);
214 : 0 : ret2 = GetElementAndCount(&f2, szEl2, &count2);
215 [ # # # # ]: 0 : if (0 <= ret1 && 0 <= ret2)
216 : : {
217 [ # # ]: 0 : if ((ret = strcmp(szEl1, szEl2))) /* djb-rwth: addressing LLVM warning */
218 : : {
219 : 0 : return ret; /* lexicographic order, string termination > any character */
220 : : }
221 [ # # ]: 0 : if ((ret = count2 - count1)) /* djb-rwth: addressing LLVM warning */
222 : : {
223 : 0 : return ret; /* inverse atom count order */
224 : : }
225 : : }
226 : : else
227 : : {
228 : 0 : return 0; /* program error <BRKPT> */
229 : : }
230 [ # # # # ]: 0 : } while (0 < ret1 && 0 < ret2);
231 : :
232 : 0 : return 0;
233 : : }
234 : :
235 : :
236 : : /****************************************************************************/
237 : 114 : int CompareHillFormulasNoH(const char* f1,
238 : : const char* f2,
239 : : int* num_H1,
240 : : int* num_H2)
241 : : {
242 : : char szEl1[4], szEl2[4];
243 : : int count1, count2, ret1, ret2, ret;
244 : :
245 : : do
246 : : {
247 : 194 : ret1 = GetElementAndCount(&f1, szEl1, &count1);
248 [ + + + + : 194 : if (0 < ret1 && szEl1[0] == 'H' && !szEl1[1])
+ - ]
249 : : {
250 : 48 : *num_H1 += count1;
251 : 48 : ret1 = GetElementAndCount(&f1, szEl1, &count1);
252 : : }
253 : 194 : ret2 = GetElementAndCount(&f2, szEl2, &count2);
254 [ + + + + : 194 : if (0 < ret2 && szEl2[0] == 'H' && !szEl2[1])
+ - ]
255 : : {
256 : 52 : *num_H2 += count2;
257 : 52 : ret2 = GetElementAndCount(&f2, szEl2, &count2);
258 : : }
259 [ + - + - ]: 194 : if (0 <= ret1 && 0 <= ret2)
260 : : {
261 [ + + ]: 194 : if ((ret = strcmp(szEl1, szEl2))) /* djb-rwth: addressing LLVM warning */
262 : : {
263 : 44 : return ret; /* lexicographic order, string termination > any character */
264 : : }
265 [ + + ]: 150 : if ((ret = count2 - count1)) /* djb-rwth: addressing LLVM warning */
266 : : {
267 : 14 : return ret; /* inverse atom count order */
268 : : }
269 : : }
270 : : else
271 : : {
272 : 0 : return 0; /* program error <BRKPT> */
273 : : }
274 [ + + + - ]: 136 : } while (0 < ret1 && 0 < ret2);
275 : :
276 : 56 : return 0;
277 : : }
278 : :
279 : :
280 : : /****************************************************************************/
281 : 68 : int CompareTautNonIsoPartOfINChI(const INChI* i1, const INChI* i2)
282 : : {
283 : : int len1, len2, ret, i;
284 : :
285 [ + + + + ]: 68 : len1 = i1->lenTautomer > 0 && i1->nTautomer[0] ? i1->lenTautomer : 0;
286 [ + + + + ]: 68 : len2 = i2->lenTautomer > 0 && i2->nTautomer[0] ? i2->lenTautomer : 0;
287 [ - + ]: 68 : if ((ret = len2 - len1)) /* djb-rwth: addressing LLVM warning */
288 : : {
289 : 0 : return ret;
290 : : }
291 [ + + ]: 148 : for (i = 0; i < len1; i++)
292 : : {
293 [ - + ]: 80 : if ((ret = (int)i2->nTautomer[i] - (int)i1->nTautomer[i])) /* djb-rwth: addressing LLVM warning */
294 : 0 : return ret;
295 : : }
296 : 68 : return 0;
297 : : }
298 : :
299 : :
300 : : /****************************************************************************
301 : : CompINChITautVsNonTaut( ... )
302 : :
303 : : Sorting in descending order:
304 : : return -1 if *p1 > *p2, return +1 if *p1 < *p2
305 : : ****************************************************************************/
306 : 161 : int CompINChITautVsNonTaut(const INCHI_SORT* p1,
307 : : const INCHI_SORT* p2,
308 : : int bCompareIsotopic)
309 : : {
310 : : int ret, num, i, num_H1, num_H2;
311 : :
312 : 161 : const INChI* i1 = NULL; /* Mobile-H layers in Mobile-H sorting order */
313 : 161 : const INChI* i2 = NULL; /* Fixed-H layers in Fixed-H sorting order */
314 : :
315 : : int n1; /* TAUT_YES if tautomeric i1 exists, otherwise TAUT_NON */
316 : :
317 : : /* INChI_Stereo *Stereo1, *Stereo2; */
318 : :
319 [ + - + - ]: 161 : n1 = (p1->pINChI[TAUT_YES] && p1->pINChI[TAUT_YES]->nNumberOfAtoms) ? TAUT_YES : TAUT_NON;
320 : :
321 : 161 : i1 = p1->pINChI[n1];
322 [ - + ]: 161 : i2 = (n1 == TAUT_YES && p2->pINChI[TAUT_NON] &&
323 [ + - - - ]: 322 : p2->pINChI[TAUT_NON]->nNumberOfAtoms) ? p2->pINChI[TAUT_NON] : (const INChI*)NULL;
324 : :
325 : : /* non-deleted-non-empty < deleted < empty */
326 [ + - + - ]: 161 : if (i1 && !i2)
327 : 161 : return 0; /* non-empty is the smallest (first) */
328 [ # # # # ]: 0 : if (!i1 && i2)
329 : 0 : return 0;
330 [ # # # # ]: 0 : if (!i1 && !i2)
331 : 0 : return 0;
332 [ # # ]: 0 : if (i1->bDeleted)
333 : 0 : return 1; /* deleted is the largest (last) among non-empty */
334 [ # # ]: 0 : if (i2->bDeleted)
335 : 0 : return -1;
336 : :
337 [ # # # # ]: 0 : if (i1->nNumberOfAtoms > 0 && !i2->nNumberOfAtoms)
338 : 0 : return 0;
339 : :
340 : : /* i2 = i2; djb-rwth: an obviously useless statement */
341 : :
342 : 0 : num_H1 = num_H2 = 0;
343 : :
344 : : /* do not compare terminal H */
345 [ # # ]: 0 : if ((ret = CompareHillFormulasNoH(i1->szHillFormula, i2->szHillFormula, &num_H1, &num_H2))) /* djb-rwth: addressing LLVM warning */
346 : : {
347 : 0 : return ret; /* lexicographic order except the shorter one is greater (last): CH2O < CH2; C3XX < C2XX */
348 : : }
349 : :
350 : : /*
351 : : compare non-isotopic non-tautomeric part
352 : : */
353 : :
354 : : /* compare number of atoms (excluding terminal H) */
355 [ # # ]: 0 : if ((ret = i2->nNumberOfAtoms - i1->nNumberOfAtoms)) /* djb-rwth: addressing LLVM warning */
356 : 0 : return ret; /* more atoms first */
357 : :
358 : : /* compare elements (excluding terminal H) */
359 : 0 : num = i1->nNumberOfAtoms;
360 [ # # ]: 0 : for (i = 0; i < num; i++)
361 : : { /* should always be equal if Hill formulas are same */
362 [ # # ]: 0 : if ((ret = (int)i2->nAtom[i] - (int)i1->nAtom[i])) /* djb-rwth: addressing LLVM warning */
363 : 0 : return ret; /* greater periodic number first */
364 : : }
365 : : /*
366 : : compare connection tables
367 : : */
368 [ # # ]: 0 : if ((ret = i2->lenConnTable - i1->lenConnTable)) /* djb-rwth: addressing LLVM warning */
369 : 0 : return ret; /* longer connection table first */
370 : 0 : num = i2->lenConnTable;
371 [ # # ]: 0 : for (i = 0; i < num; i++)
372 : : {
373 [ # # ]: 0 : if ((ret = (int)i2->nConnTable[i] - (int)i1->nConnTable[i])) /* djb-rwth: addressing LLVM warning */
374 : 0 : return ret; /* greater connection table first */
375 : : }
376 : : /*
377 : : compare total number of H (inverse: H3 < H2 )
378 : : */
379 [ # # ]: 0 : if ((ret = num_H2 - num_H1)) /* djb-rwth: addressing LLVM warning */
380 : 0 : return ret;
381 : : /*
382 : : compare non-tautomeric num_H: N < NH3 < NH2 < NH
383 : : */
384 : 0 : num = i1->nNumberOfAtoms;
385 [ # # ]: 0 : for (i = 0; i < num; i++)
386 : : {
387 [ # # ]: 0 : if (i2->nNum_H[i] != i1->nNum_H[i])
388 : : {
389 [ # # ]: 0 : return !i2->nNum_H[i] ? 1 : /* no H first */
390 [ # # ]: 0 : !i1->nNum_H[i] ? -1 :
391 : 0 : (int)i2->nNum_H[i] - (int)i1->nNum_H[i];
392 : : }
393 : : }
394 : : /*
395 : : compare non-isotopic tautomeric part
396 : : */
397 [ # # ]: 0 : if ((ret = CompareTautNonIsoPartOfINChI(i1, i2))) /* djb-rwth: addressing LLVM warning */
398 : : {
399 : 0 : return ret;
400 : : }
401 : : /*
402 : : if ( ret = i2->lenTautomer - i1->lenTautomer )
403 : : return ret;
404 : : num = inchi_min( i2->lenTautomer, i1->lenTautomer );
405 : : for ( i = 0; i < num; i ++ ) {
406 : : if ( ret = (int)i2->nTautomer[i] - (int)i1->nTautomer[i] )
407 : : return ret;
408 : : }
409 : : */
410 : :
411 : : /*
412 : : at this point both components are either tautomeric
413 : : or non-tautomeric
414 : : */
415 : :
416 : : /*
417 : : non-tautomeric "fixed H" specific
418 : : */
419 [ # # ]: 0 : if ( /*TAUT_NON == bTaut && (i2 &&*/ i2->nNum_H_fixed) /* djb-rwth: fixing coverity ID #499493 */
420 : : {
421 : : /* first, compare non-tautomeric chem. formulas -- they may be different */
422 : : /* secondly, compare fixed-H distribution */
423 [ # # ]: 0 : if (i2->nNum_H_fixed)
424 : : {
425 : 0 : num = i2->nNumberOfAtoms;
426 [ # # ]: 0 : for (i = 0; i < num; i++)
427 : : {
428 [ # # ]: 0 : if (i2->nNum_H_fixed[i] != 0)
429 : : {
430 : 0 : return 1;
431 : : }
432 : : }
433 : : }
434 : : }
435 : : /*
436 : : compare non-isotopic stereo
437 : : */
438 : 0 : ret = CompareInchiStereo(i1->Stereo, i1->nFlags, i2->Stereo, i2->nFlags);
439 [ # # ]: 0 : if (ret)
440 : : {
441 : 0 : return ret;
442 : : }
443 : :
444 : : /*
445 : : do not switch back to tautomeric i1, i2
446 : : */
447 : :
448 : : /* -- how to switch back --
449 : : if ( i1t ) {
450 : : i1 = i1t;
451 : : i1t = NULL;
452 : : }
453 : : if ( i2t ) {
454 : : i2 = i2t;
455 : : i2t = NULL;
456 : : }
457 : : */
458 : :
459 : : /*
460 : : compare isotopic non-tautomeric part
461 : : */
462 [ # # ]: 0 : if (bCompareIsotopic)
463 : : {
464 [ # # ]: 0 : if ((ret = i2->nNumberOfIsotopicAtoms - i1->nNumberOfIsotopicAtoms)) /* djb-rwth: addressing LLVM warning */
465 : 0 : return ret;
466 : 0 : num = i1->nNumberOfIsotopicAtoms;
467 : :
468 : : /* compare isotopic atoms */
469 [ # # ]: 0 : for (i = 0; i < num; i++)
470 : : {
471 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nAtomNumber - (int)i1->IsotopicAtom[i].nAtomNumber)) /* djb-rwth: addressing LLVM warning */
472 : 0 : return ret;
473 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nIsoDifference - (int)i1->IsotopicAtom[i].nIsoDifference)) /* djb-rwth: addressing LLVM warning */
474 : 0 : return ret;
475 : : }
476 : :
477 : : /* compare isotopic H */
478 : : /* if tautomeric comparison mode then here are compared only non-tautomeric H */
479 [ # # ]: 0 : for (i = 0; i < num; i++)
480 : : {
481 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nNum_T - (int)i1->IsotopicAtom[i].nNum_T)) /* djb-rwth: addressing LLVM warning */
482 : 0 : return ret;
483 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nNum_D - (int)i1->IsotopicAtom[i].nNum_D)) /* djb-rwth: addressing LLVM warning */
484 : 0 : return ret;
485 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nNum_H - (int)i1->IsotopicAtom[i].nNum_H)) /* djb-rwth: addressing LLVM warning */
486 : 0 : return ret;
487 : : }
488 : :
489 : : /* compare isotopic tautomeric part */
490 [ # # # # : 0 : if ((ret = i2->nNumberOfIsotopicTGroups || i1->nNumberOfIsotopicTGroups)) /* djb-rwth: addressing LLVM warning */
# # ]
491 : 0 : return ret;
492 : :
493 : : /*
494 : : num = i1->nNumberOfIsotopicTGroups;
495 : : for ( i = 0; i < num; i ++ ) {
496 : : if ( ret = (int)i2->IsotopicTGroup[i].nTGroupNumber - (int)i1->IsotopicTGroup[i].nTGroupNumber )
497 : : return ret;
498 : : if ( ret = (int)i2->IsotopicTGroup[i].nNum_T - (int)i1->IsotopicTGroup[i].nNum_T )
499 : : return ret;
500 : : if ( ret = (int)i2->IsotopicTGroup[i].nNum_D - (int)i1->IsotopicTGroup[i].nNum_D )
501 : : return ret;
502 : : if ( ret = (int)i2->IsotopicTGroup[i].nNum_H - (int)i1->IsotopicTGroup[i].nNum_H )
503 : : return ret;
504 : : }
505 : : */
506 : :
507 : : /* compare isotopic stereo */
508 : 0 : ret = CompareInchiStereo(i1->StereoIsotopic, i1->nFlags,
509 : 0 : i2->StereoIsotopic, i2->nFlags);
510 [ # # ]: 0 : if (ret)
511 : : {
512 : 0 : return ret;
513 : : }
514 : : }
515 : :
516 : : /*
517 : : compare charges: non-charged first, then in order of
518 : : ascending charges (negative first)
519 : : */
520 : :
521 [ # # # # ]: 0 : if (i2->nTotalCharge && i1->nTotalCharge)
522 : : {
523 : : /* both are charged; smaller charges first */
524 : 0 : ret = (int)i1->nTotalCharge - (int)i2->nTotalCharge;
525 : 0 : return ret;
526 : : }
527 [ # # ]: 0 : if ((ret = (i1->nTotalCharge ? 1 : 0) - (i2->nTotalCharge ? 1 : 0))) /* djb-rwth: addressing LLVM warning */
528 : : {
529 : : /* only one is charged; uncharged first */
530 : 0 : return ret;
531 : : }
532 : : /* stable sort */
533 : : /*ret = p1->ord_number - p2->ord_number;*/
534 : :
535 : 0 : return ret;
536 : : }
537 : :
538 : : /*
539 : : Stereo
540 : : */
541 : :
542 : :
543 : : /****************************************************************************/
544 : : typedef enum tagSp3StereoTypeTmp
545 : : {
546 : : SP3_NONE = 0, /* no sp3 stereo: no /t, /m, /s segments */
547 : : /* /t is present: */
548 : : SP3_ONLY = 1, /* no /s or /m segment: inversion leaves the structure unchanged */
549 : : SP3_ABS = 2, /* abs stereo: both /m and /s are present */
550 : : SP3_REL = 4, /* rel stereo: /s is present, /m is not */
551 : : SP3_RAC = 8, /* racemic stereo: /s is presen, /m is nott */
552 : : SP3_TYPE = (SP3_ABS | SP3_REL | SP3_RAC), /* bitmap for checking the presence of /m */
553 : : SP3_ANY = (SP3_ABS | SP3_REL | SP3_RAC | SP3_ONLY) /* bitmap for checking the presence of /t */
554 : : } SP3_TYPE_TMP;
555 : :
556 : :
557 : : /****************************************************************************/
558 : 644 : int GetSp3RelRacAbs(const INChI* pINChI, INChI_Stereo* Stereo)
559 : : {
560 : 644 : int nRet = SP3_NONE;
561 [ + + + - : 644 : if (pINChI && !pINChI->bDeleted && Stereo && 0 < Stereo->nNumberOfStereoCenters)
+ + + + ]
562 : : {
563 [ + - ]: 51 : if (0 != Stereo->nCompInv2Abs)
564 : : {
565 [ - + ]: 51 : if (pINChI->nFlags & INCHI_FLAG_REL_STEREO)
566 : : {
567 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
568 : : if (1 < Stereo->nNumberOfStereoCenters)
569 : : {
570 : : nRet = SP3_REL;
571 : : }
572 : : #else
573 : 0 : nRet = SP3_REL;
574 : : #endif
575 : : }
576 [ - + ]: 51 : else if (pINChI->nFlags & INCHI_FLAG_RAC_STEREO)
577 : : {
578 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
579 : : if (1 < Stereo->nNumberOfStereoCenters)
580 : : {
581 : : nRet = SP3_REL;
582 : : }
583 : : #else
584 : 0 : nRet = SP3_RAC;
585 : : #endif
586 : : }
587 : : else
588 : : {
589 : 51 : nRet = SP3_ABS;
590 : : }
591 : : }
592 : : else
593 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
594 : : if (!((pINChI->nFlags & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) && 1 == Stereo->nNumberOfStereoCenters))
595 : : #endif
596 : : {
597 : 0 : nRet = SP3_ONLY; /* SP3_NONE if relative stereo and 1 stereocenter */
598 : : }
599 : : }
600 : 644 : return nRet;
601 : : }
602 : :
603 : :
604 : : /****************************************************************************
605 : : CompINChILayers( ... )
606 : :
607 : : Sorting in descending order: return -1 if *p1 > *p2, return +1 if *p1 < *p2
608 : : ****************************************************************************/
609 : 161 : int CompINChILayers(const INCHI_SORT* p1,
610 : : const INCHI_SORT* p2,
611 : : char sDifSegs[][DIFS_LENGTH],
612 : : int bFixTranspChargeBug)
613 : : {
614 : 161 : int ret = 0, num, i, num_H1, num_H2;
615 : :
616 : 161 : const INChI* i1 = NULL; /* Mobile-H layers in Mobile-H sorting order */
617 : 161 : const INChI* i2 = NULL; /* Fixed-H layers in Fixed-H sorting order */
618 : :
619 : : int n1; /* TAUT_YES if tautomeric i1 exists, otherwise TAUT_NON */
620 : :
621 : : INChI_Stereo* Stereo1, * Stereo2;
622 : : INChI_Stereo* IsoStereo1, * IsoStereo2;
623 : : int bRelRac[DIFL_LENGTH];
624 : : char* psDifSegs;
625 : :
626 [ + - + - ]: 161 : n1 = (p1->pINChI[TAUT_YES] && p1->pINChI[TAUT_YES]->nNumberOfAtoms) ? TAUT_YES : TAUT_NON;
627 : :
628 : 161 : i1 = p1->pINChI[n1];
629 [ - + ]: 161 : i2 = (n1 == TAUT_YES && p2->pINChI[TAUT_NON] &&
630 [ + - - - ]: 322 : p2->pINChI[TAUT_NON]->nNumberOfAtoms) ? p2->pINChI[TAUT_NON] : (const INChI*)NULL;
631 : :
632 : 161 : num_H1 = num_H2 = 0;
633 : 161 : memset(bRelRac, DIFV_BOTH_EMPTY, sizeof(bRelRac)); /* djb-rwth: memset_s C11/Annex K variant? */
634 : : /*=====================*/
635 : : /*==== /f ======*/
636 : : /*=====================*/
637 [ + - + - : 161 : if (i1 && !i1->bDeleted && i1->szHillFormula && i1->szHillFormula[0])
+ - + - ]
638 : : {
639 : 161 : sDifSegs[DIFL_M][DIFS_f_FORMULA] |= DIFV_NEQ2PRECED;
640 [ - + - - : 161 : if (i2 && !i2->bDeleted && i2->szHillFormula && i2->szHillFormula[0])
- - - - ]
641 : : {
642 [ # # ]: 0 : if (!CompareHillFormulasNoH(i1->szHillFormula, i2->szHillFormula, &num_H1, &num_H2) &&
643 [ # # ]: 0 : num_H1 == num_H2)
644 : : {
645 : 0 : sDifSegs[DIFL_F][DIFS_f_FORMULA] |= DIFV_EQL2PRECED;
646 : : }
647 : : else
648 : : {
649 : 0 : sDifSegs[DIFL_F][DIFS_f_FORMULA] |= DIFV_NEQ2PRECED;
650 : : }
651 : : }
652 : : else
653 : : {
654 [ - + ]: 161 : sDifSegs[DIFL_F][DIFS_f_FORMULA] |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
655 : : }
656 : : }
657 : : else
658 : : {
659 : 0 : sDifSegs[DIFL_M][DIFS_f_FORMULA] |= DIFV_BOTH_EMPTY;
660 [ # # # # : 0 : if (i2 && !i2->bDeleted && i2->szHillFormula && i2->szHillFormula[0])
# # # # ]
661 : : {
662 : 0 : sDifSegs[DIFL_F][DIFS_f_FORMULA] |= DIFV_NEQ2PRECED;
663 : : }
664 : : else
665 : : {
666 : 0 : sDifSegs[DIFL_F][DIFS_f_FORMULA] |= DIFV_BOTH_EMPTY;
667 : : }
668 : : }
669 : : /*=====================*/
670 : : /*==== /c ======*/
671 : : /*=====================*/
672 [ + - + - : 161 : if (i1 && !i1->bDeleted && i1->lenConnTable > 1)
+ + ]
673 : : {
674 : 77 : sDifSegs[DIFL_M][DIFS_f_FORMULA] |= DIFV_NEQ2PRECED;
675 : : }
676 : : else
677 : : {
678 : 84 : sDifSegs[DIFL_M][DIFS_f_FORMULA] |= DIFV_BOTH_EMPTY;
679 : : }
680 : : /*=====================*/
681 : : /*==== /h ======*/
682 : : /*=====================*/
683 : : /* M: H atoms */
684 [ + - + - ]: 161 : if (i1 && !i1->bDeleted)
685 : : {
686 [ + + + - : 161 : num_H1 = (i1->lenTautomer > 0 && i1->nTautomer && i1->nTautomer[0]) ? 1 : 0; /* number of t-groups */
+ + ]
687 [ + + + - ]: 161 : if (!num_H1 && i1->nNum_H)
688 : : {
689 [ + + ]: 228 : for (i = 0; i < i1->nNumberOfAtoms; i++)
690 : : { /* immobile H */
691 [ + + ]: 165 : if (i1->nNum_H[i])
692 : : {
693 : 91 : num_H1 = 1;
694 : 91 : break;
695 : : }
696 : : }
697 : : }
698 [ + + ]: 161 : sDifSegs[DIFL_M][DIFS_h_H_ATOMS] |= num_H1 ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
699 : : }
700 : : else
701 : : {
702 : 0 : sDifSegs[DIFL_M][DIFS_h_H_ATOMS] |= DIFV_BOTH_EMPTY;
703 : : }
704 : : /* F: fixed mobile H */
705 [ - + - - : 161 : if (i2 && !i2->bDeleted && i2->nNum_H_fixed)
- - ]
706 : : {
707 : 0 : num_H2 = 0;
708 [ # # # # ]: 0 : if (i1 && !i1->bDeleted)
709 : : {
710 [ # # ]: 0 : for (i = 0; i < i1->nNumberOfAtoms; i++)
711 : : {
712 [ # # ]: 0 : if (i2->nNum_H_fixed[i])
713 : : {
714 : 0 : num_H2 = 1;
715 : 0 : break;
716 : : }
717 : : }
718 : : }
719 [ # # ]: 0 : sDifSegs[DIFL_F][DIFS_h_H_ATOMS] |= num_H2 ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
720 : : }
721 : : else
722 : : {
723 : 161 : sDifSegs[DIFL_F][DIFS_h_H_ATOMS] |= DIFV_BOTH_EMPTY;
724 : : }
725 : : /* MI: exchangable isotopic H: see OutputINChI1(), num_iso_H[] */
726 : :
727 : : /*=====================*/
728 : : /*==== /q ======*/
729 : : /*=====================*/
730 : 161 : psDifSegs = &sDifSegs[DIFL_F][DIFS_q_CHARGE];
731 [ + - + - ]: 161 : if (i1 && !i1->bDeleted)
732 : : {
733 [ + + ]: 161 : if (i1->nTotalCharge)
734 : : {
735 : 30 : sDifSegs[DIFL_M][DIFS_q_CHARGE] |= DIFV_NEQ2PRECED;
736 : : }
737 : : else
738 : : {
739 : 131 : sDifSegs[DIFL_M][DIFS_q_CHARGE] |= DIFV_BOTH_EMPTY;
740 : : }
741 [ - + - - ]: 161 : if (i2 && !i2->bDeleted)
742 : : {
743 [ # # ]: 0 : if (i1->nTotalCharge)
744 : : {
745 [ # # ]: 0 : if (i1->nTotalCharge == i2->nTotalCharge)
746 : : {
747 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
748 : : }
749 : : else
750 : : {
751 [ # # ]: 0 : if (i2->nTotalCharge)
752 : : {
753 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
754 : : }
755 : : else
756 : : {
757 : 0 : *psDifSegs |= DIFV_IS_EMPTY;
758 : : }
759 : : }
760 : : }
761 : : else
762 : : {
763 [ # # ]: 0 : if (i2->nTotalCharge)
764 : : {
765 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
766 : : }
767 : : else
768 : : {
769 : 0 : *psDifSegs |= DIFV_BOTH_EMPTY;
770 : : }
771 : : }
772 : : }
773 : : else
774 : : {
775 [ + - ]: 161 : if (!i2)
776 : : {
777 [ + - ]: 161 : if (bFixTranspChargeBug == 1)
778 : : {
779 : : /* bug explanation:
780 : :
781 : : component #1 is tautomeric, component #2 is not
782 : : Mobile-H(#2) > Mobile-H(#1)
783 : : Fixed-H(#2) = Mobile-H(#2) < Fixed-H(#1)
784 : :
785 : : Layer first_charge second_charge
786 : :
787 : : Mobile-H 0 (comp#1) -1 (comp#2)
788 : : Fixed-H none (comp#2) -1 (comp#1)
789 : :
790 : : v1.01 charge compared decided that charge layers are same and omitted Fixed-H /q layer
791 : :
792 : : Solution: when component permutation is detected AND fixed-H component does not exist,
793 : : compare Mobile-H charge [0 (comp#1) in the example] to the charge of Mobile-H [-1 (comp#2)]
794 : : of the component that has none Fixed-H charge
795 : : */
796 : :
797 : : /* Fixed-H i2 is empty because Fixed-H struct is same as Mobile-H */
798 [ - + - - ]: 161 : if (p1->ord_number != p2->ord_number && /* component order in Fixed-H is different from Mobile-H */
799 [ # # # # ]: 0 : n1 == TAUT_YES && p2->pINChI[TAUT_YES] && !p2->pINChI[TAUT_YES]->bDeleted &&
800 [ # # ]: 0 : p2->pINChI[TAUT_YES]->nNumberOfAtoms)
801 : 0 : {
802 : 0 : int i2_nTotalCharge = p2->pINChI[TAUT_YES]->nTotalCharge;
803 : :
804 [ # # ]: 0 : if (i1->nTotalCharge)
805 : : {
806 [ # # ]: 0 : if (i1->nTotalCharge == i2_nTotalCharge)
807 : : {
808 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
809 : : }
810 : : else
811 : : {
812 [ # # ]: 0 : if (i2_nTotalCharge)
813 : : {
814 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
815 : : }
816 : : else
817 : : {
818 : 0 : *psDifSegs |= DIFV_IS_EMPTY;
819 : : }
820 : : }
821 : : }
822 : : else
823 : : {
824 [ # # ]: 0 : if (i2_nTotalCharge)
825 : : {
826 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
827 : : }
828 : : else
829 : : {
830 : 0 : *psDifSegs |= DIFV_BOTH_EMPTY;
831 : : }
832 : : }
833 : : }
834 : : else
835 : : {
836 : 161 : *psDifSegs |= i1->nTotalCharge ? DIFV_EQL2PRECED : DIFV_BOTH_EMPTY;
837 : : }
838 : : }
839 : : else /* if (bFixTranspChargeBug==1) */
840 : : {
841 : 0 : *psDifSegs |= i1->nTotalCharge ? DIFV_EQL2PRECED : DIFV_BOTH_EMPTY;
842 : : }
843 : : }
844 : : else /* if ( !i2 ) { */
845 : : {
846 : : /* i2 && i2->bDeleted */
847 [ # # ]: 0 : *psDifSegs |= i1->nTotalCharge ? DIFV_IS_EMPTY : DIFV_BOTH_EMPTY;
848 : : }
849 : : }
850 : : }
851 : : else
852 : : {
853 : 0 : sDifSegs[DIFL_M][DIFS_q_CHARGE] |= DIFV_BOTH_EMPTY;
854 [ # # # # ]: 0 : if (i2 && !i2->bDeleted)
855 : : {
856 [ # # ]: 0 : if (i2->nTotalCharge)
857 : : {
858 : 0 : sDifSegs[DIFL_F][DIFS_q_CHARGE] |= DIFV_NEQ2PRECED;
859 : : }
860 : : else
861 : : {
862 : 0 : sDifSegs[DIFL_F][DIFS_q_CHARGE] |= DIFV_BOTH_EMPTY;
863 : : }
864 : : }
865 : : }
866 : :
867 : :
868 : : /*************** stereo *****************/
869 : :
870 [ + - + - ]: 161 : if (i1 && !i1->bDeleted)
871 : : {
872 : 161 : Stereo1 = i1->Stereo;
873 : 161 : IsoStereo1 = i1->StereoIsotopic;
874 : : }
875 : : else
876 : : {
877 : 0 : Stereo1 = NULL;
878 : 0 : IsoStereo1 = NULL;
879 : : }
880 [ - + - - ]: 161 : if (i2 && !i2->bDeleted)
881 : : {
882 : 0 : Stereo2 = i2->Stereo;
883 : 0 : IsoStereo2 = i2->StereoIsotopic;
884 : : }
885 : : else
886 : : {
887 : 161 : Stereo2 = NULL;
888 : 161 : IsoStereo2 = NULL;
889 : : }
890 : :
891 : : /*=====================*/
892 : : /*==== /b ======*/
893 : : /*=====================*/
894 : : /* M double bond stereo */
895 : 161 : psDifSegs = &sDifSegs[DIFL_M][DIFS_b_SBONDS];
896 [ + - - + ]: 161 : if (Stereo1 && Stereo1->nNumberOfStereoBonds)
897 : : {
898 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
899 : : }
900 : : else
901 : : {
902 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
903 : : }
904 : : /* F double bond stereo */
905 : 161 : psDifSegs = &sDifSegs[DIFL_F][DIFS_b_SBONDS];
906 [ - + - - ]: 161 : if (Stereo2 && Stereo2->nNumberOfStereoBonds)
907 : : {
908 [ # # # # ]: 0 : if (Stereo1 && Stereo1->nNumberOfStereoBonds)
909 : : {
910 [ # # ]: 0 : if (Eql_INChI_Stereo(Stereo1, EQL_SP2, Stereo2, EQL_SP2, 0))
911 : : {
912 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
913 : : }
914 : : else
915 : : {
916 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
917 : : }
918 : : }
919 : : else
920 : : {
921 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
922 : : }
923 : : }
924 : : else
925 : : {
926 [ + - - + ]: 161 : if (Stereo1 && Stereo1->nNumberOfStereoBonds)
927 : : {
928 [ # # ]: 0 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
929 : : }
930 : : else
931 : : {
932 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
933 : : }
934 : : }
935 : :
936 : : /* MI double bond stereo */
937 : 161 : psDifSegs = &sDifSegs[DIFL_MI][DIFS_b_SBONDS];
938 [ - + - - ]: 161 : if (IsoStereo1 && IsoStereo1->nNumberOfStereoBonds)
939 : : {
940 [ # # ]: 0 : if (Eql_INChI_Stereo(IsoStereo1, EQL_SP2, Stereo1, EQL_SP2, 0))
941 : : {
942 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
943 : : }
944 : : else
945 : : {
946 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
947 : : }
948 : : }
949 : : else
950 : : {
951 [ + - - + ]: 161 : if (Stereo1 && Stereo1->nNumberOfStereoBonds)
952 : : {
953 : 0 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
954 : : }
955 : : else
956 : : {
957 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
958 : : }
959 : : }
960 : :
961 : : /* FI double bond stereo */
962 : 161 : psDifSegs = &sDifSegs[DIFL_FI][DIFS_b_SBONDS];
963 [ - + - - ]: 161 : if (IsoStereo2 && IsoStereo2->nNumberOfStereoBonds)
964 : : {
965 [ # # ]: 0 : if (Eql_INChI_Stereo(IsoStereo2, EQL_SP2, Stereo2, EQL_SP2, 0))
966 : : {
967 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
968 : : }
969 : : else
970 : : {
971 [ # # # # : 0 : if (!(Stereo1 && Stereo1->nNumberOfStereoBonds) &&
# # ]
972 [ # # # # ]: 0 : !(Stereo2 && Stereo2->nNumberOfStereoBonds) &&
973 : 0 : Eql_INChI_Stereo(IsoStereo2, EQL_SP2, IsoStereo1, EQL_SP2, 0))
974 : : {
975 : 0 : *psDifSegs |= DIFV_FI_EQ_MI;
976 : : }
977 : : else
978 : : {
979 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
980 : : }
981 : : }
982 : : }
983 : : else
984 : : {
985 : : /* the solution table for FI stereo,
986 : : in case of FI stereo is empty
987 : : E = segment is empty, NE = not empty
988 : : +==============================+
989 : : | M | MI | F | result |
990 : : +=====+=====+=====+============+
991 : : | E | E | E | both empty |
992 : : +-----+-----+-----+------------+
993 : : | NE | E | E | both empty |
994 : : +-----+-----+-----+------------+
995 : : | E | NE | E | is empty |
996 : : +-----+-----+-----+------------+
997 : : | NE | NE | E | both empty |
998 : : +-----+-----+-----+------------+
999 : : | E | E | NE | is empty |
1000 : : +-----+-----+-----+------------+
1001 : : | NE | E | NE | is empty |
1002 : : +-----+-----+-----+------------+
1003 : : | E | NE | NE | is empty |
1004 : : +-----+-----+-----+------------+
1005 : : | NE | NE | ME | is empty |
1006 : : +==============================+
1007 : : */
1008 [ - + - - ]: 161 : if (Stereo2 && Stereo2->nNumberOfStereoBonds)
1009 : : {
1010 : 0 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1011 : : }
1012 : : else
1013 [ - + - - : 161 : if (IsoStereo1 && IsoStereo1->nNumberOfStereoBonds &&
- - ]
1014 [ # # ]: 0 : !(Stereo1 && Stereo1->nNumberOfStereoBonds)
1015 : : )
1016 : : {
1017 [ # # ]: 0 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1018 : : }
1019 : : else
1020 : : {
1021 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
1022 : : }
1023 : : }
1024 : :
1025 : : /*==================================*/
1026 : : /*==== /t, /m, /s for M ======*/
1027 : : /*==================================*/
1028 : : /* M sp3 stereo */
1029 : 161 : bRelRac[DIFL_M] = GetSp3RelRacAbs(i1, Stereo1); /* Mobile-H */
1030 : 161 : bRelRac[DIFL_MI] = GetSp3RelRacAbs(i1, IsoStereo1);
1031 : 161 : bRelRac[DIFL_F] = GetSp3RelRacAbs(i2, Stereo2); /* Fixed-H */
1032 : 161 : bRelRac[DIFL_FI] = GetSp3RelRacAbs(i2, IsoStereo2);
1033 [ + + ]: 161 : if (SP3_NONE != bRelRac[DIFL_M])
1034 : : {
1035 [ + - ]: 51 : sDifSegs[DIFL_M][DIFS_t_SATOMS] |= (bRelRac[DIFL_M] & SP3_ANY) ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
1036 : 51 : sDifSegs[DIFL_M][DIFS_m_SP3INV] |= (bRelRac[DIFL_M] & SP3_ABS) ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
1037 [ + - ]: 51 : sDifSegs[DIFL_M][DIFS_s_STYPE] |= (bRelRac[DIFL_M] & SP3_TYPE) ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
1038 : : }
1039 : : else
1040 : : {
1041 : 110 : sDifSegs[DIFL_M][DIFS_t_SATOMS] |= DIFV_BOTH_EMPTY;
1042 : 110 : sDifSegs[DIFL_M][DIFS_m_SP3INV] |= DIFV_BOTH_EMPTY;
1043 : 110 : sDifSegs[DIFL_M][DIFS_s_STYPE] |= DIFV_BOTH_EMPTY;
1044 : : }
1045 : :
1046 : : /*=====================*/
1047 : : /*==== /t ======*/
1048 : : /*=====================*/
1049 : : /* F sp3 stereo */
1050 : 161 : psDifSegs = &sDifSegs[DIFL_F][DIFS_t_SATOMS];
1051 [ - + ]: 161 : if (SP3_ANY & bRelRac[DIFL_F])
1052 : : {
1053 [ # # ]: 0 : if (Eql_INChI_Stereo(Stereo2, EQL_SP3, Stereo1, EQL_SP3, 0))
1054 : : {
1055 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1056 : : }
1057 : : else
1058 : : {
1059 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1060 : : }
1061 : : }
1062 : : else
1063 : : {
1064 [ + + ]: 161 : if (SP3_ANY & bRelRac[DIFL_M])
1065 : : {
1066 [ - + ]: 51 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1067 : : }
1068 : : else
1069 : : {
1070 : 110 : *psDifSegs |= DIFV_BOTH_EMPTY;
1071 : : }
1072 : : }
1073 : :
1074 : : /* MI sp3 stereo */
1075 : 161 : psDifSegs = &sDifSegs[DIFL_MI][DIFS_t_SATOMS];
1076 [ - + ]: 161 : if (SP3_ANY & bRelRac[DIFL_MI])
1077 : : {
1078 [ # # ]: 0 : if (Eql_INChI_Stereo(IsoStereo1, EQL_SP3, Stereo1, EQL_SP3, 0))
1079 : : {
1080 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1081 : : }
1082 : : else
1083 : : {
1084 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1085 : : }
1086 : : }
1087 : : else
1088 : : {
1089 [ + + ]: 161 : if (SP3_ANY & bRelRac[DIFL_M])
1090 : : {
1091 : 51 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1092 : : }
1093 : : else
1094 : : {
1095 : 110 : *psDifSegs |= DIFV_BOTH_EMPTY;
1096 : : }
1097 : : }
1098 : :
1099 : : /* FI sp3 stereo */
1100 : 161 : psDifSegs = &sDifSegs[DIFL_FI][DIFS_t_SATOMS];
1101 [ - + ]: 161 : if (SP3_ANY & bRelRac[DIFL_FI])
1102 : : {
1103 [ # # ]: 0 : if (Eql_INChI_Stereo(IsoStereo2, EQL_SP3, Stereo2, EQL_SP3, 0))
1104 : : {
1105 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1106 : : }
1107 : : else
1108 : : {
1109 [ # # ]: 0 : if (!(SP3_ANY & bRelRac[DIFL_M]) &&
1110 [ # # # # ]: 0 : !(SP3_ANY & bRelRac[DIFL_F]) &&
1111 : 0 : Eql_INChI_Stereo(IsoStereo2, EQL_SP3, IsoStereo1, EQL_SP3, 0))
1112 : : {
1113 : 0 : *psDifSegs |= DIFV_FI_EQ_MI;
1114 : : }
1115 : : else
1116 : : {
1117 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1118 : : }
1119 : : }
1120 : : }
1121 : : else /* similar to /b */
1122 [ - + ]: 161 : if ((SP3_ANY & bRelRac[DIFL_F]))
1123 : : {
1124 : 0 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1125 : : }
1126 : : else
1127 : : {
1128 [ - + - - ]: 161 : if ((SP3_ANY & bRelRac[DIFL_MI]) && !(SP3_ANY & bRelRac[DIFL_M]))
1129 : : {
1130 [ # # ]: 0 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1131 : : }
1132 : : else
1133 : : {
1134 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
1135 : : }
1136 : : }
1137 : :
1138 : : /*=====================*/
1139 : : /*==== /m ======*/
1140 : : /*=====================*/
1141 : : /* F sp3 abs stereo inversion */
1142 : 161 : psDifSegs = &sDifSegs[DIFL_F][DIFS_m_SP3INV];
1143 [ - + ]: 161 : if (bRelRac[DIFL_F] & SP3_ABS)
1144 : : {
1145 : : /* the order of || operands below is critically important: || is not a commutative operation */
1146 [ # # # # ]: 0 : if (!(bRelRac[DIFL_M] & SP3_ABS) || Stereo2->nCompInv2Abs != Stereo1->nCompInv2Abs)
1147 : : {
1148 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1149 : : }
1150 : : else
1151 : : {
1152 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1153 : : }
1154 : : }
1155 : : else
1156 : : {
1157 [ + + ]: 161 : if (bRelRac[DIFL_M] & SP3_ABS)
1158 : : {
1159 [ - + ]: 51 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1160 : : }
1161 : : else
1162 : : {
1163 : 110 : *psDifSegs |= DIFV_BOTH_EMPTY;
1164 : : }
1165 : : }
1166 : :
1167 : : /* MI sp3 abs stereo inversion */
1168 : 161 : psDifSegs = &sDifSegs[DIFL_MI][DIFS_m_SP3INV];
1169 [ - + ]: 161 : if (SP3_ABS & bRelRac[DIFL_MI])
1170 : : {
1171 [ # # # # ]: 0 : if ((SP3_ABS & bRelRac[DIFL_M]) && IsoStereo1->nCompInv2Abs == Stereo1->nCompInv2Abs)
1172 : : {
1173 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1174 : : }
1175 : : else
1176 : : {
1177 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1178 : : }
1179 : : }
1180 : : else
1181 : : {
1182 [ + + ]: 161 : if (SP3_ABS & bRelRac[DIFL_M])
1183 : : {
1184 : 51 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1185 : : }
1186 : : else
1187 : : {
1188 : 110 : *psDifSegs |= DIFV_BOTH_EMPTY;
1189 : : }
1190 : : }
1191 : :
1192 : : /* FI sp3 abs stereo inversion */
1193 : 161 : psDifSegs = &sDifSegs[DIFL_FI][DIFS_m_SP3INV];
1194 [ - + ]: 161 : if (SP3_ABS & bRelRac[DIFL_FI])
1195 : : {
1196 [ # # # # ]: 0 : if ((SP3_ABS & bRelRac[DIFL_F]) && IsoStereo2->nCompInv2Abs == Stereo2->nCompInv2Abs)
1197 : : {
1198 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1199 : : }
1200 : : else
1201 : : {
1202 [ # # ]: 0 : if (!(SP3_ABS & bRelRac[DIFL_M]) &&
1203 [ # # ]: 0 : !(SP3_ABS & bRelRac[DIFL_F]) &&
1204 [ # # ]: 0 : (SP3_ABS & bRelRac[DIFL_MI]) && /* make sure IsoStereo1 != NULL */
1205 [ # # ]: 0 : IsoStereo2->nCompInv2Abs == IsoStereo1->nCompInv2Abs)
1206 : : {
1207 : 0 : *psDifSegs |= DIFV_FI_EQ_MI;
1208 : : }
1209 : : else
1210 : : {
1211 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1212 : : }
1213 : : }
1214 : : }
1215 : :
1216 : : else
1217 : : {
1218 : : /* similar to /b */
1219 : : /* the order of || operands below is critically important: || is no a commutative operation */
1220 [ - + ]: 161 : if ((SP3_ABS & bRelRac[DIFL_F]))
1221 : : {
1222 : 0 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1223 : : }
1224 : : else
1225 : : {
1226 [ - + - - ]: 161 : if ((SP3_ABS & bRelRac[DIFL_MI]) && !(SP3_ABS & bRelRac[DIFL_M]))
1227 : : {
1228 [ # # ]: 0 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1229 : : }
1230 : : else
1231 : : {
1232 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
1233 : : }
1234 : : }
1235 : : }
1236 : :
1237 : : /*=====================*/
1238 : : /*==== /s ======*/
1239 : : /*=====================*/
1240 : : /* F sp3 stereo type */
1241 : 161 : psDifSegs = &sDifSegs[DIFL_F][DIFS_s_STYPE];
1242 [ - + ]: 161 : if (bRelRac[DIFL_F] & SP3_TYPE)
1243 : : {
1244 [ # # ]: 0 : if ((bRelRac[DIFL_F] & SP3_TYPE) == (bRelRac[DIFL_M] & SP3_TYPE))
1245 : : {
1246 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1247 : : }
1248 : : else
1249 : : {
1250 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1251 : : }
1252 : : }
1253 : : else
1254 : : {
1255 [ + + ]: 161 : if (bRelRac[DIFL_M] & SP3_TYPE)
1256 : : {
1257 [ - + ]: 51 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1258 : : }
1259 : : else
1260 : : {
1261 : 110 : *psDifSegs |= DIFV_BOTH_EMPTY;
1262 : : }
1263 : : }
1264 : :
1265 : : /* MI sp3 stereo type */
1266 : 161 : psDifSegs = &sDifSegs[DIFL_MI][DIFS_s_STYPE];
1267 [ - + ]: 161 : if (SP3_TYPE & bRelRac[DIFL_MI])
1268 : : {
1269 [ # # ]: 0 : if ((SP3_TYPE & bRelRac[DIFL_MI]) == (SP3_TYPE & bRelRac[DIFL_M]))
1270 : : {
1271 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1272 : : }
1273 : : else
1274 : : {
1275 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1276 : : }
1277 : : }
1278 : : else
1279 : : {
1280 [ + + ]: 161 : if (SP3_TYPE & bRelRac[DIFL_M])
1281 : : {
1282 : 51 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1283 : : }
1284 : : else
1285 : : {
1286 : 110 : *psDifSegs |= DIFV_BOTH_EMPTY;
1287 : : }
1288 : : }
1289 : :
1290 : : /* FI sp3 stereo type */
1291 : 161 : psDifSegs = &sDifSegs[DIFL_FI][DIFS_s_STYPE];
1292 [ - + ]: 161 : if (SP3_TYPE & bRelRac[DIFL_FI])
1293 : : {
1294 [ # # ]: 0 : if ((SP3_TYPE & bRelRac[DIFL_FI]) == (SP3_TYPE & bRelRac[DIFL_F]))
1295 : : {
1296 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1297 : : }
1298 : : else
1299 : : {
1300 [ # # ]: 0 : if (!(SP3_TYPE & bRelRac[DIFL_M]) &&
1301 [ # # ]: 0 : !(SP3_TYPE & bRelRac[DIFL_F]) &&
1302 [ # # ]: 0 : (SP3_TYPE & bRelRac[DIFL_MI]))
1303 : : {
1304 : 0 : *psDifSegs |= DIFV_FI_EQ_MI;
1305 : : }
1306 : : else
1307 : : {
1308 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1309 : : }
1310 : : }
1311 : : }
1312 : : else
1313 : : {
1314 : : /* similar to /b */
1315 : : /* the order of || operands below is critically important: || is not a commutative operation */
1316 [ - + ]: 161 : if ((SP3_TYPE & bRelRac[DIFL_F]))
1317 : : {
1318 : 0 : *psDifSegs |= DIFV_EQL2PRECED; /* isotopic is missing because there is no isotopes */
1319 : : }
1320 : : else
1321 : : {
1322 [ - + - - ]: 161 : if ((SP3_TYPE & bRelRac[DIFL_MI]) && !(SP3_TYPE & bRelRac[DIFL_M]))
1323 : : {
1324 [ # # ]: 0 : *psDifSegs |= i2 ? DIFV_IS_EMPTY : DIFV_EQL2PRECED;
1325 : : }
1326 : : else
1327 : : {
1328 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
1329 : : }
1330 : : }
1331 : : }
1332 : :
1333 : : /*=====================*/
1334 : : /*==== /o ======*/
1335 : : /*=====================*/
1336 [ + - + - : 161 : if (p1 && p2 && p1->ord_number != p2->ord_number)
- + ]
1337 : : {
1338 : 0 : sDifSegs[DIFL_F][DIFS_o_TRANSP] |= DIFV_NEQ2PRECED;
1339 : : }
1340 : : /*=====================*/
1341 : : /*==== /i ======*/
1342 : : /*=====================*/
1343 : :
1344 : : /* M isotopic atoms */
1345 : 161 : psDifSegs = &sDifSegs[DIFL_MI][DIFS_i_IATOMS];
1346 [ + - + - : 161 : if (i1 && !i1->bDeleted && (i1->nNumberOfIsotopicAtoms || i1->nNumberOfIsotopicTGroups))
+ - - + ]
1347 : : {
1348 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1349 : : }
1350 : : else
1351 : : {
1352 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
1353 : : }
1354 : : /* F isotopic atoms */
1355 : 161 : psDifSegs = &sDifSegs[DIFL_FI][DIFS_i_IATOMS];
1356 [ - + - - ]: 161 : if (i2 && !i2->bDeleted)
1357 : : {
1358 [ # # # # ]: 0 : if (i2->nNumberOfIsotopicAtoms || i2->nNumberOfIsotopicTGroups)
1359 : : {
1360 [ # # # # ]: 0 : if (!i1 || i1->bDeleted ||
1361 [ # # ]: 0 : i2->nNumberOfIsotopicAtoms != i1->nNumberOfIsotopicAtoms ||
1362 [ # # ]: 0 : i2->nNumberOfIsotopicTGroups != i1->nNumberOfIsotopicTGroups)
1363 : : {
1364 : 0 : *psDifSegs |= DIFV_NEQ2PRECED;
1365 : : }
1366 : : else
1367 : : {
1368 : : int diff;
1369 : 0 : num = i1->nNumberOfIsotopicAtoms;
1370 : 0 : diff = 0;
1371 [ # # ]: 0 : for (i = 0; i < num; i++)
1372 : : {
1373 : : /* compare isotopic atoms */
1374 [ # # ]: 0 : if ((diff = (int)i2->IsotopicAtom[i].nAtomNumber - (int)i1->IsotopicAtom[i].nAtomNumber)) /* djb-rwth: addressing LLVM warning */
1375 : : {
1376 : 0 : break;
1377 : : }
1378 [ # # ]: 0 : if ((diff = (int)i2->IsotopicAtom[i].nIsoDifference - (int)i1->IsotopicAtom[i].nIsoDifference)) /* djb-rwth: addressing LLVM warning */
1379 : : {
1380 : 0 : break;
1381 : : }
1382 : : /* compare isotopic H */
1383 [ # # ]: 0 : if ((diff = (int)i2->IsotopicAtom[i].nNum_T - (int)i1->IsotopicAtom[i].nNum_T)) /* djb-rwth: addressing LLVM warning */
1384 : : {
1385 : 0 : break;
1386 : : }
1387 [ # # ]: 0 : if ((diff = (int)i2->IsotopicAtom[i].nNum_D - (int)i1->IsotopicAtom[i].nNum_D)) /* djb-rwth: addressing LLVM warning */
1388 : : {
1389 : 0 : break;
1390 : : }
1391 [ # # ]: 0 : if ((diff = (int)i2->IsotopicAtom[i].nNum_H - (int)i1->IsotopicAtom[i].nNum_H)) /* djb-rwth: addressing LLVM warning */
1392 : : {
1393 : 0 : break;
1394 : : }
1395 : : }
1396 [ # # ]: 0 : if (!diff)
1397 : : {
1398 : 0 : num = i1->nNumberOfIsotopicTGroups;
1399 [ # # ]: 0 : for (i = 0; i < num; i++)
1400 : : {
1401 [ # # ]: 0 : if ((diff = (int)i2->IsotopicTGroup[i].nTGroupNumber - (int)i1->IsotopicTGroup[i].nTGroupNumber)) /* djb-rwth: addressing LLVM warning */
1402 : : {
1403 : 0 : break;
1404 : : }
1405 [ # # ]: 0 : if ((diff = (int)i2->IsotopicTGroup[i].nNum_T - (int)i1->IsotopicTGroup[i].nNum_T)) /* djb-rwth: addressing LLVM warning */
1406 : : {
1407 : 0 : break;
1408 : : }
1409 [ # # ]: 0 : if ((diff = (int)i2->IsotopicTGroup[i].nNum_D - (int)i1->IsotopicTGroup[i].nNum_D)) /* djb-rwth: addressing LLVM warning */
1410 : : {
1411 : 0 : return diff;
1412 : : }
1413 [ # # ]: 0 : if ((diff = (int)i2->IsotopicTGroup[i].nNum_H - (int)i1->IsotopicTGroup[i].nNum_H)) /* djb-rwth: addressing LLVM warning */
1414 : : {
1415 : 0 : break;
1416 : : }
1417 : : }
1418 : : }
1419 [ # # ]: 0 : *psDifSegs |= diff ? DIFV_NEQ2PRECED : DIFV_FI_EQ_MI;
1420 : : }
1421 : : }
1422 : : else
1423 : : {
1424 [ # # # # : 0 : if (i1 && !i1->bDeleted && (i1->nNumberOfIsotopicAtoms || i1->nNumberOfIsotopicTGroups))
# # # # ]
1425 : : {
1426 : 0 : *psDifSegs |= DIFV_IS_EMPTY;
1427 : : }
1428 : : }
1429 : : }
1430 : : else
1431 : : {
1432 [ + - ]: 161 : if (!i2)
1433 : : {
1434 [ + - + - : 161 : if (i1 && !i1->bDeleted && (i1->nNumberOfIsotopicAtoms || i1->nNumberOfIsotopicTGroups))
+ - - + ]
1435 : : {
1436 : 0 : *psDifSegs |= DIFV_EQL2PRECED;
1437 : : }
1438 : : else
1439 : : {
1440 : 161 : *psDifSegs |= DIFV_BOTH_EMPTY;
1441 : : }
1442 : : }
1443 : : }
1444 : :
1445 : 161 : return ret;
1446 : : }
1447 : :
1448 : :
1449 : : /****************************************************************************/
1450 : 1060 : int INChI_SegmentAction(char cDifSegs)
1451 : : {
1452 [ + + ]: 1060 : if (!(cDifSegs & DIFV_OUTPUT_OMIT_F))
1453 : : {
1454 : 778 : return INCHI_SEGM_OMIT;
1455 : : }
1456 [ - + - - ]: 282 : if ((cDifSegs & DIFV_OUTPUT_EMPTY_T) && !(cDifSegs & DIFV_OUTPUT_EMPTY_F))
1457 : : {
1458 : 0 : return INCHI_SEGM_EMPTY;
1459 : : }
1460 [ + - ]: 282 : if ((cDifSegs & DIFV_OUTPUT_FILL_T))
1461 : : {
1462 : 282 : return INCHI_SEGM_FILL;
1463 : : }
1464 : :
1465 : 0 : return INCHI_SEGM_OMIT; /* the control flow shoul never reach this point */
1466 : : }
1467 : :
1468 : :
1469 : : /****************************************************************************
1470 : : MarkUnusedAndEmptyLayers( ... )
1471 : :
1472 : : 1. If all elements of a layer are DIFV_IS_EMPTY and/or DIFV_BOTH_EMPTY
1473 : : and/or DIFV_EQL2PRECED and/or DIFV_FI_EQ_MI
1474 : : and there is NO succeeding non-empty layer then mark the 1st element
1475 : : of the layer DIFV_BOTH_EMPTY; this layerr will be omitted.
1476 : :
1477 : : 2. If all elements of a layer are DIFV_IS_EMPTY and/or DIFV_BOTH_EMPTY
1478 : : and/or DIFV_EQL2PRECED and/or DIFV_FI_EQ_MI
1479 : : and there IS a succeeding non-empty layer then mark the 1st element
1480 : : of the layer DIFV_IS_EMPTY and all other elements DIFV_BOTH_EMPTY;
1481 : : only the first empty segment of this layerr will be output.
1482 : :
1483 : : 3. If NOT all elements of a layer are DIFV_IS_EMPTY and/or DIFV_BOTH_EMPTY
1484 : : and/or DIFV_EQL2PRECED and/or DIFV_FI_EQ_MI
1485 : : and the 1st element of the layer is DIFV_BOTH_EMPTY then mark it
1486 : : DIFV_IS_EMPTY; it will be output as empty (except M layer).
1487 : :
1488 : : ****************************************************************************/
1489 : 122 : int MarkUnusedAndEmptyLayers(char sDifSegs[][DIFS_LENGTH])
1490 : : {
1491 : : int i, nLayer, sBits, nFirstSegm;
1492 : : #define nFirstFmlSegm DIFS_f_FORMULA
1493 : : #define nFirstIsoSegm DIFS_i_IATOMS
1494 : : /* FI */
1495 : 122 : nLayer = DIFL_FI;
1496 : 122 : nFirstSegm = nFirstIsoSegm;
1497 : 122 : sBits = 0;
1498 [ + + ]: 1464 : for (i = 0; i < DIFS_idf_LENGTH; i++)
1499 : : {
1500 : 1342 : sBits |= sDifSegs[nLayer][i];
1501 : : }
1502 [ + - ]: 122 : if (!(sBits & DIFV_OUTPUT_OMIT_F))
1503 : : {
1504 : : /* Omit the FI layer */
1505 : 122 : memset(sDifSegs[nLayer], DIFV_BOTH_EMPTY, DIFS_idf_LENGTH); /* djb-rwth: memset_s C11/Annex K variant? */
1506 : : }
1507 : : else
1508 : : {
1509 [ # # ]: 0 : if (sDifSegs[nLayer][nFirstSegm] == DIFV_BOTH_EMPTY ||
1510 [ # # ]: 0 : !(sDifSegs[nLayer][nFirstSegm] & DIFV_OUTPUT_OMIT_F))
1511 : : {
1512 : 0 : sDifSegs[nLayer][nFirstSegm] = DIFV_IS_EMPTY;
1513 : : }
1514 : : }
1515 : :
1516 : : /* MI */
1517 : 122 : nLayer = DIFL_MI;
1518 : 122 : nFirstSegm = nFirstIsoSegm;
1519 : 122 : sBits = 0;
1520 [ + + ]: 1464 : for (i = 0; i < DIFS_idf_LENGTH; i++)
1521 : : {
1522 : 1342 : sBits |= sDifSegs[nLayer][i];
1523 : : }
1524 [ + - ]: 122 : if (!(sBits & DIFV_OUTPUT_OMIT_F))
1525 : : {
1526 : : /* Omit the MI layer */
1527 : 122 : memset(sDifSegs[nLayer], DIFV_BOTH_EMPTY, DIFS_idf_LENGTH); /* djb-rwth: memset_s C11/Annex K variant? */
1528 : : }
1529 : : else
1530 : : {
1531 [ # # ]: 0 : if (sDifSegs[nLayer][nFirstSegm] == DIFV_BOTH_EMPTY ||
1532 [ # # ]: 0 : !(sDifSegs[nLayer][nFirstSegm] & DIFV_OUTPUT_OMIT_F))
1533 : : {
1534 : 0 : sDifSegs[nLayer][nFirstSegm] = DIFV_IS_EMPTY;
1535 : : }
1536 : : }
1537 : :
1538 : : /* F */
1539 : 122 : nLayer = DIFL_F;
1540 : 122 : nFirstSegm = nFirstFmlSegm;
1541 : 122 : sBits = 0;
1542 [ + + ]: 1464 : for (i = 0; i < DIFS_idf_LENGTH; i++)
1543 : : {
1544 : 1342 : sBits |= sDifSegs[nLayer][i];
1545 : : }
1546 [ + - ]: 122 : if (!(sBits & DIFV_OUTPUT_OMIT_F) &&
1547 [ + - ]: 122 : sDifSegs[DIFL_FI][nFirstIsoSegm] == DIFV_BOTH_EMPTY)
1548 : : {
1549 : : /* Omit the F layer: no non-iotopic and no isotopic segments */
1550 : 122 : memset(sDifSegs[nLayer], DIFV_BOTH_EMPTY, DIFS_idf_LENGTH); /* djb-rwth: memset_s C11/Annex K variant? */
1551 : : }
1552 : : else
1553 : : { /* do not omit fixed-H layer */
1554 [ # # ]: 0 : if (sDifSegs[nLayer][nFirstSegm] == DIFV_BOTH_EMPTY ||
1555 [ # # ]: 0 : !(sDifSegs[nLayer][nFirstSegm] & DIFV_OUTPUT_OMIT_F))
1556 : : {
1557 : 0 : sDifSegs[nLayer][nFirstSegm] = DIFV_IS_EMPTY;
1558 : : }
1559 : : }
1560 : :
1561 : : /* M -- leave as it is */
1562 : :
1563 : 122 : return 0;
1564 : :
1565 : : #undef nFirstFmlSegm
1566 : : #undef nFirstIsoSegm
1567 : : }
1568 : :
1569 : :
1570 : : /****************************************************************************/
1571 : 104 : int CompareInchiStereo(INChI_Stereo* Stereo1,
1572 : : INCHI_MODE nFlags1,
1573 : : INChI_Stereo* Stereo2,
1574 : : INCHI_MODE nFlags2)
1575 : : {
1576 : : int i, num, ret;
1577 [ + + + - ]: 104 : if (Stereo2 && Stereo1)
1578 : : {
1579 : : /* compare stereogenic bonds */
1580 : 56 : num = inchi_min(Stereo2->nNumberOfStereoBonds, Stereo1->nNumberOfStereoBonds);
1581 [ - + ]: 56 : for (i = 0; i < num; i++)
1582 : : {
1583 [ # # ]: 0 : if ((ret = (int)Stereo2->nBondAtom1[i] - (int)Stereo1->nBondAtom1[i])) /* djb-rwth: addressing LLVM warning */
1584 : : {
1585 : 0 : return ret;
1586 : : }
1587 [ # # ]: 0 : if ((ret = (int)Stereo2->nBondAtom2[i] - (int)Stereo1->nBondAtom2[i])) /* djb-rwth: addressing LLVM warning */
1588 : : {
1589 : 0 : return ret;
1590 : : }
1591 [ # # ]: 0 : if ((ret = (int)Stereo2->b_parity[i] - (int)Stereo1->b_parity[i])) /* djb-rwth: addressing LLVM warning */
1592 : : {
1593 : 0 : return ret;
1594 : : }
1595 : : }
1596 [ - + ]: 56 : if ((ret = (int)Stereo2->nNumberOfStereoBonds - (int)Stereo1->nNumberOfStereoBonds)) /* djb-rwth: addressing LLVM warning */
1597 : : {
1598 : 0 : return ret;
1599 : : }
1600 : :
1601 : : /* compare stereogenic atoms */
1602 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
1603 : : if (((nFlags1 | nFlags2) & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
1604 : : 1 == Stereo2->nNumberOfStereoCenters &&
1605 : : 1 == Stereo1->nNumberOfStereoCenters)
1606 : : {
1607 : : ; /* do not compare single stereocenters in case of relative stereo */
1608 : : }
1609 : : else
1610 : : #endif
1611 : : {
1612 : 56 : num = inchi_min(Stereo2->nNumberOfStereoCenters, Stereo1->nNumberOfStereoCenters);
1613 [ + + ]: 88 : for (i = 0; i < num; i++)
1614 : : {
1615 [ - + ]: 40 : if ((ret = (int)Stereo2->nNumber[i] - (int)Stereo1->nNumber[i])) /* djb-rwth: addressing LLVM warning */
1616 : : {
1617 : 0 : return ret;
1618 : : }
1619 [ + + ]: 40 : if ((ret = (int)Stereo2->t_parity[i] - (int)Stereo1->t_parity[i])) /* djb-rwth: addressing LLVM warning */
1620 : : {
1621 : 8 : return ret;
1622 : : }
1623 : : }
1624 [ - + ]: 48 : if ((ret = (int)Stereo2->nNumberOfStereoCenters - (int)Stereo1->nNumberOfStereoCenters)) /* djb-rwth: addressing LLVM warning */
1625 : 0 : return ret;
1626 : : /* compare stereo-abs-is-inverted flags for non-relative, non-racemic */
1627 [ + - ]: 48 : if (!((nFlags1 | nFlags2) & (INCHI_FLAG_RAC_STEREO | INCHI_FLAG_REL_STEREO)))
1628 : : {
1629 [ - + ]: 48 : if ((ret = (Stereo2->nCompInv2Abs < 0) - (Stereo1->nCompInv2Abs < 0))) /* djb-rwth: addressing LLVM warning */
1630 : : {
1631 : 0 : return ret;
1632 : : }
1633 : : }
1634 : : }
1635 : : }
1636 : : else
1637 : : {
1638 [ - + - - : 48 : if (Stereo2 && (Stereo2->nNumberOfStereoBonds > 0 || Stereo2->nNumberOfStereoCenters > 0
- - ]
1639 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
1640 : : && /* do not compare single stereocenters in case of relative stereo */
1641 : : !((nFlags2 & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
1642 : : 1 == Stereo2->nNumberOfStereoCenters
1643 : : )
1644 : : #endif
1645 : : ))
1646 : : {
1647 : 0 : return 1;
1648 : : }
1649 : : else
1650 : : {
1651 [ - + - - : 48 : if (Stereo1 && (Stereo1->nNumberOfStereoBonds > 0 || Stereo1->nNumberOfStereoCenters > 0
- - ]
1652 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
1653 : : && /* do not compare single stereocenters in case of relative stereo */
1654 : : !((nFlags1 & (INCHI_FLAG_REL_STEREO | INCHI_FLAG_RAC_STEREO)) &&
1655 : : 1 == Stereo1->nNumberOfStereoCenters
1656 : : )
1657 : : #endif
1658 : : ))
1659 : : {
1660 : 0 : return -1;
1661 : : }
1662 : : }
1663 : : }
1664 : :
1665 : 96 : return 0;
1666 : : }
1667 : :
1668 : :
1669 : : /****************************************************************************
1670 : : CompINChI2( ... )
1671 : :
1672 : : Sorting in descending order:
1673 : : return -1 if *p1 > *p2, return +1 if *p1 < *p2
1674 : :
1675 : : ****************************************************************************/
1676 : 114 : int CompINChI2(const INCHI_SORT* p1,
1677 : : const INCHI_SORT* p2,
1678 : : int bTaut,
1679 : : int bCompareIsotopic)
1680 : : {
1681 : : int ret, num, i, num_H1, num_H2;
1682 : :
1683 : 114 : const INChI* i1 = NULL; /* tautomeric if exists, otherwise non-tautomeric */
1684 : 114 : const INChI* i2 = NULL; /* tautomeric if exists, otherwise non-tautomeric */
1685 : :
1686 : : int n1; /* TAUT_YES if tautomeric i1 exists, otherwise TAUT_NON */
1687 : : int n2; /* TAUT_YES if tautomeric i2 exists, otherwise TAUT_NON */
1688 : :
1689 : 114 : const INChI* i1n = NULL; /* non-tautomeric if both tautomeric AND non-tautomeric exist */
1690 : 114 : const INChI* i2n = NULL; /* non-tautomeric if both tautomeric AND non-tautomeric exist */
1691 : :
1692 : : /*const INChI *i1t = NULL;*/ /* temp for i1 if both tautomeric AND non-tautomeric exist */
1693 : : /*const INChI *i2t = NULL;*/ /* temp for i2 if both tautomeric AND non-tautomeric exist */
1694 : :
1695 : : /* INChI_Stereo *Stereo1, *Stereo2; */
1696 : :
1697 [ + - + - ]: 114 : n1 = (p1->pINChI[TAUT_YES] && p1->pINChI[TAUT_YES]->nNumberOfAtoms) ? TAUT_YES : TAUT_NON;
1698 [ + - + - ]: 114 : n2 = (p2->pINChI[TAUT_YES] && p2->pINChI[TAUT_YES]->nNumberOfAtoms) ? TAUT_YES : TAUT_NON;
1699 : :
1700 : 114 : i1 = p1->pINChI[n1];
1701 [ - + ]: 114 : i1n = (n1 == TAUT_YES && p1->pINChI[TAUT_NON] &&
1702 [ + - - - ]: 228 : p1->pINChI[TAUT_NON]->nNumberOfAtoms) ? p1->pINChI[TAUT_NON] : (const INChI*)NULL;
1703 : :
1704 : 114 : i2 = p2->pINChI[n2];
1705 [ - + ]: 114 : i2n = (n2 == TAUT_YES && p2->pINChI[TAUT_NON] &&
1706 [ + - - - ]: 228 : p2->pINChI[TAUT_NON]->nNumberOfAtoms) ? p2->pINChI[TAUT_NON] : (const INChI*)NULL;
1707 : :
1708 : : /* non-deleted-non-empty < deleted < empty */
1709 [ + - - + ]: 114 : if (i1 && !i2)
1710 : 0 : return -1; /* non-empty is the smallest (first) */
1711 [ - + - - ]: 114 : if (!i1 && i2)
1712 : 0 : return 1;
1713 [ - + - - ]: 114 : if (!i1 && !i2)
1714 : 0 : return 0;
1715 [ - + - - ]: 114 : if (i1->bDeleted && !i2->bDeleted)
1716 : 0 : return 1; /* deleted is the largest (last) among non-empty */
1717 [ + - - + ]: 114 : if (!i1->bDeleted && i2->bDeleted)
1718 : 0 : return -1;
1719 : :
1720 : 114 : num_H1 = num_H2 = 0;
1721 : :
1722 : : /* do not compare terminal H */
1723 [ + + ]: 114 : if ((ret = CompareHillFormulasNoH(i1->szHillFormula, i2->szHillFormula, &num_H1, &num_H2))) /* djb-rwth: addressing LLVM warning */
1724 : : {
1725 : 58 : return ret; /* lexicographic order except the shorter one is greater (last): CH2O < CH2; C3XX < C2XX */
1726 : : }
1727 : :
1728 : : /*********************************************************
1729 : : compare non-isotopic non-tautomeric part
1730 : : *********************************************************/
1731 : :
1732 : : /* compare number of atoms (excluding terminal H) */
1733 [ - + ]: 56 : if ((ret = i2->nNumberOfAtoms - i1->nNumberOfAtoms)) /* djb-rwth: addressing LLVM warning */
1734 : 0 : return ret; /* more atoms first */
1735 : :
1736 : : /* compare elements (excluding terminal H) */
1737 : 56 : num = i1->nNumberOfAtoms;
1738 [ + + ]: 324 : for (i = 0; i < num; i++)
1739 : : {
1740 : : /* should always be equal if Hill formulas are same */
1741 [ - + ]: 268 : if ((ret = (int)i2->nAtom[i] - (int)i1->nAtom[i])) /* djb-rwth: addressing LLVM warning */
1742 : 0 : return ret; /* greater periodic number first */
1743 : : }
1744 : :
1745 : : /**********************************************************
1746 : : compare connection tables
1747 : : ***********************************************************/
1748 [ - + ]: 56 : if ((ret = i2->lenConnTable - i1->lenConnTable)) /* djb-rwth: addressing LLVM warning */
1749 : 0 : return ret; /* longer connection table first */
1750 : 56 : num = i2->lenConnTable;
1751 [ + + ]: 540 : for (i = 0; i < num; i++)
1752 : : {
1753 [ - + ]: 484 : if ((ret = (int)i2->nConnTable[i] - (int)i1->nConnTable[i])) /* djb-rwth: addressing LLVM warning */
1754 : 0 : return ret; /* greater connection table first */
1755 : : }
1756 : :
1757 : : /*********************************************************
1758 : : compare compare total number of H (inverse: H3 < H2 )
1759 : : **********************************************************/
1760 [ - + ]: 56 : if ((ret = num_H2 - num_H1)) /* djb-rwth: addressing LLVM warning */
1761 : : {
1762 : 0 : return ret;
1763 : : }
1764 : :
1765 : : /*********************************************************
1766 : : compare non-tautomeric num_H: N < NH3 < NH2 < NH
1767 : : **********************************************************/
1768 : 56 : num = i1->nNumberOfAtoms;
1769 [ + + ]: 324 : for (i = 0; i < num; i++)
1770 : : {
1771 [ - + ]: 268 : if (i2->nNum_H[i] != i1->nNum_H[i])
1772 : : {
1773 [ # # ]: 0 : return !i2->nNum_H[i] ? 1 : /* no H first */
1774 [ # # ]: 0 : !i1->nNum_H[i] ? -1 :
1775 : 0 : (int)i2->nNum_H[i] - (int)i1->nNum_H[i];
1776 : : }
1777 : : }
1778 : :
1779 : : /*********************************************************
1780 : : compare non-isotopic tautomeric part
1781 : : *********************************************************/
1782 [ - + ]: 56 : if ((ret = CompareTautNonIsoPartOfINChI(i1, i2))) /* djb-rwth: addressing LLVM warning */
1783 : : {
1784 : 0 : return ret;
1785 : : }
1786 : : /*
1787 : : if ( ret = i2->lenTautomer - i1->lenTautomer )
1788 : : return ret;
1789 : : num = inchi_min( i2->lenTautomer, i1->lenTautomer );
1790 : : for ( i = 0; i < num; i ++ ) {
1791 : : if ( ret = (int)i2->nTautomer[i] - (int)i1->nTautomer[i] )
1792 : : return ret;
1793 : : }
1794 : : */
1795 : :
1796 : : /*********************************************************
1797 : : * *
1798 : : * at this point both components are either tautomeric *
1799 : : * or non-tautomeric *
1800 : : * *
1801 : : *********************************************************/
1802 : :
1803 : : /*********************************************************
1804 : : non-tautomeric "fixed H" specific
1805 : : *********************************************************/
1806 [ + + - + : 56 : if (TAUT_NON == bTaut && ((i1n && i1n->nNum_H_fixed) || (i2n && i2n->nNum_H_fixed))) /* djb-rwth: addressing LLVM warning */
- - - + -
- ]
1807 : : {
1808 : : /* first, compare non-tautomeric chem. formulas -- they may be different */
1809 [ # # ]: 0 : const char* f1 = (i1n /*&& i1n->nNum_H_fixed*/) ? i1n->szHillFormula : i1->szHillFormula;
1810 [ # # ]: 0 : const char* f2 = (i2n /*&& i2n->nNum_H_fixed*/) ? i2n->szHillFormula : i2->szHillFormula;
1811 [ # # # # : 0 : if (f1 && f2 && (ret = CompareHillFormulas(f1, f2)))
# # ]
1812 : : {
1813 : 0 : return ret;
1814 : : }
1815 : : /* secondly, compare fixed-H distribution */
1816 [ # # # # : 0 : if (i1n && i1n->nNum_H_fixed && i2n && i2n->nNum_H_fixed)
# # # # ]
1817 : : {
1818 : 0 : num = inchi_min(i1n->nNumberOfAtoms, i2n->nNumberOfAtoms);
1819 [ # # ]: 0 : for (i = 0; i < num; i++)
1820 : : {
1821 [ # # ]: 0 : if (i2n->nNum_H_fixed[i] != i1n->nNum_H_fixed[i])
1822 : : {
1823 [ # # ]: 0 : return !i2n->nNum_H_fixed[i] ? 1 : /* no fixed H first */
1824 [ # # ]: 0 : !i1n->nNum_H_fixed[i] ? -1 :
1825 : 0 : (int)i2n->nNum_H_fixed[i] - (int)i1n->nNum_H_fixed[i];
1826 : : }
1827 : : }
1828 [ # # ]: 0 : if ((ret = (int)i2n->nNumberOfAtoms - (int)i1n->nNumberOfAtoms)) /* djb-rwth: addressing LLVM warning */
1829 : : {
1830 : 0 : return ret; /* should not happen <BRKPT> */
1831 : : }
1832 : : }
1833 : : else
1834 : : {
1835 [ # # # # ]: 0 : if (i1n && i1n->nNum_H_fixed)
1836 : : {
1837 : 0 : num = i1n->nNumberOfAtoms;
1838 [ # # ]: 0 : for (i = 0; i < num; i++)
1839 : : {
1840 : : /* added 2004-05-04 */
1841 [ # # ]: 0 : if (i1n->nNum_H_fixed[i])
1842 : : {
1843 : 0 : return -1; /* i1n->nNum_H_fixed[i] > 0? -1:1;*/
1844 : : }
1845 : : }
1846 : : /* p1 is tautomeric, p2 is not tautomeric; this must have been detected earlier */
1847 : : /*return -1;*/ /* has fixed H first *//* <BRKPT> */ /* removed 2004-05-04 */
1848 : : }
1849 : : else
1850 : : {
1851 : 0 : num = i2n->nNumberOfAtoms;
1852 [ # # ]: 0 : for (i = 0; i < num; i++)
1853 : : {
1854 : : /* added 2004-05-04 */
1855 [ # # ]: 0 : if (i2n->nNum_H_fixed[i])
1856 : : {
1857 : 0 : return 1; /* i2n->nNum_H_fixed[i] > 0? 1:-1;*/
1858 : : }
1859 : : }
1860 : : /* p2 is tautomeric, p1 is not tautomeric; this must have been detected earlier */
1861 : : /*return 1; */ /* has fixed H first *//* <BRKPT> */ /* removed 2004-05-04 */
1862 : : }
1863 : : }
1864 : : }
1865 : :
1866 : : /*************************************************************************
1867 : : if requested non-tautomeric comparison then
1868 : : prepare to compare non-taut non-isotopic stereo, etc.
1869 : : *************************************************************************/
1870 [ + + ]: 56 : if (TAUT_NON == bTaut)
1871 : : {
1872 [ - + ]: 28 : if (i1n)
1873 : : {
1874 : : /*i1t = i1;*/
1875 : 0 : i1 = i1n;
1876 : : }
1877 [ - + ]: 28 : if (i2n)
1878 : : {
1879 : : /*i2t = i2;*/
1880 : 0 : i2 = i2n;
1881 : : }
1882 : : }
1883 : :
1884 : : /*********************************************************
1885 : : compare non-isotopic stereo
1886 : : *********************************************************/
1887 : 56 : ret = CompareInchiStereo(i1->Stereo, i1->nFlags, i2->Stereo, i2->nFlags);
1888 [ + + ]: 56 : if (ret)
1889 : : {
1890 : 8 : return ret;
1891 : : }
1892 : : /*******************************************************
1893 : : do not switch back to tautomeric i1, i2
1894 : : *******************************************************/
1895 : : /* -- how to switch back --
1896 : : if ( i1t ) {
1897 : : i1 = i1t;
1898 : : i1t = NULL;
1899 : : }
1900 : : if ( i2t ) {
1901 : : i2 = i2t;
1902 : : i2t = NULL;
1903 : : }
1904 : : */
1905 : :
1906 : : /******************************************************
1907 : : compare isotopic non-tautomeric part
1908 : : ******************************************************/
1909 [ + - ]: 48 : if (bCompareIsotopic)
1910 : : {
1911 [ - + ]: 48 : if ((ret = i2->nNumberOfIsotopicAtoms - i1->nNumberOfIsotopicAtoms)) /* djb-rwth: addressing LLVM warning */
1912 : : {
1913 : 0 : return ret;
1914 : : }
1915 : 48 : num = i1->nNumberOfIsotopicAtoms;
1916 : : /* compare isotopic atoms */
1917 [ - + ]: 48 : for (i = 0; i < num; i++)
1918 : : {
1919 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nAtomNumber - (int)i1->IsotopicAtom[i].nAtomNumber)) /* djb-rwth: addressing LLVM warning */
1920 : : {
1921 : 0 : return ret;
1922 : : }
1923 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nIsoDifference - (int)i1->IsotopicAtom[i].nIsoDifference)) /* djb-rwth: addressing LLVM warning */
1924 : : {
1925 : 0 : return ret;
1926 : : }
1927 : : }
1928 : :
1929 : : /* compare isotopic H */
1930 : : /* if tautomeric comparison mode then here are compared only non-tautomeric H */
1931 [ - + ]: 48 : for (i = 0; i < num; i++)
1932 : : {
1933 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nNum_T - (int)i1->IsotopicAtom[i].nNum_T)) /* djb-rwth: addressing LLVM warning */
1934 : : {
1935 : 0 : return ret;
1936 : : }
1937 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nNum_D - (int)i1->IsotopicAtom[i].nNum_D)) /* djb-rwth: addressing LLVM warning */
1938 : : {
1939 : 0 : return ret;
1940 : : }
1941 [ # # ]: 0 : if ((ret = (int)i2->IsotopicAtom[i].nNum_H - (int)i1->IsotopicAtom[i].nNum_H)) /* djb-rwth: addressing LLVM warning */
1942 : : {
1943 : 0 : return ret;
1944 : : }
1945 : : }
1946 : :
1947 : : /*****************************************************
1948 : : compare isotopic tautomeric part
1949 : : *****************************************************/
1950 [ - + ]: 48 : if ((ret = i2->nNumberOfIsotopicTGroups - i1->nNumberOfIsotopicTGroups)) /* djb-rwth: addressing LLVM warning */
1951 : 0 : return ret;
1952 : 48 : num = i1->nNumberOfIsotopicTGroups;
1953 [ - + ]: 48 : for (i = 0; i < num; i++)
1954 : : {
1955 [ # # ]: 0 : if ((ret = (int)i2->IsotopicTGroup[i].nTGroupNumber - (int)i1->IsotopicTGroup[i].nTGroupNumber)) /* djb-rwth: addressing LLVM warning */
1956 : : {
1957 : 0 : return ret;
1958 : : }
1959 [ # # ]: 0 : if ((ret = (int)i2->IsotopicTGroup[i].nNum_T - (int)i1->IsotopicTGroup[i].nNum_T)) /* djb-rwth: addressing LLVM warning */
1960 : : {
1961 : 0 : return ret;
1962 : : }
1963 [ # # ]: 0 : if ((ret = (int)i2->IsotopicTGroup[i].nNum_D - (int)i1->IsotopicTGroup[i].nNum_D)) /* djb-rwth: addressing LLVM warning */
1964 : : {
1965 : 0 : return ret;
1966 : : }
1967 [ # # ]: 0 : if ((ret = (int)i2->IsotopicTGroup[i].nNum_H - (int)i1->IsotopicTGroup[i].nNum_H)) /* djb-rwth: addressing LLVM warning */
1968 : : {
1969 : 0 : return ret;
1970 : : }
1971 : : }
1972 : :
1973 : : /****************************************************
1974 : : compare isotopic stereo
1975 : : ****************************************************/
1976 : 48 : ret = CompareInchiStereo(i1->StereoIsotopic, i1->nFlags, i2->StereoIsotopic, i2->nFlags);
1977 [ - + ]: 48 : if (ret)
1978 : : {
1979 : 0 : return ret;
1980 : : }
1981 : : }
1982 : :
1983 : : /**********************************************************
1984 : : compare charges: non-charged first, then in order of
1985 : : ascending charges (negative first)
1986 : : ***********************************************************/
1987 [ + + + - ]: 48 : if (i2->nTotalCharge && i1->nTotalCharge)
1988 : : {
1989 : : /* both are charged; smaller charges first */
1990 : 20 : ret = (int)i1->nTotalCharge - (int)i2->nTotalCharge;
1991 : 20 : return ret;
1992 : : }
1993 [ - + ]: 28 : if ((ret = (i1->nTotalCharge ? 1 : 0) - (i2->nTotalCharge ? 1 : 0))) /* djb-rwth: addressing LLVM warning */
1994 : : {
1995 : : /* only one is charged; uncharged first */
1996 : 0 : return ret;
1997 : : }
1998 : : /* stable sort */
1999 : : /*ret = p1->ord_number - p2->ord_number;*/
2000 : :
2001 : 28 : return ret;
2002 : : }
2003 : :
2004 : :
2005 : : /****************************************************************************/
2006 : 45 : int CompINChINonTaut2(const void* p1, const void* p2)
2007 : : {
2008 : : int ret;
2009 : 45 : ret = CompINChI2((const INCHI_SORT*)p1, (const INCHI_SORT*)p2, TAUT_NON, 1);
2010 : : #if ( CANON_FIXH_TRANS == 1 )
2011 [ + + ]: 45 : if (!ret)
2012 : : {
2013 : : /* to obtain canonical transposition 2004-05-10 */
2014 : 12 : ret = CompINChI2((const INCHI_SORT*)p1, (const INCHI_SORT*)p2, TAUT_YES, 1);
2015 : : }
2016 : : #endif
2017 [ + + ]: 45 : if (!ret)
2018 : : {
2019 : : /* stable sort */
2020 : 12 : ret = ((const INCHI_SORT*)p1)->ord_number - ((const INCHI_SORT*)p2)->ord_number;
2021 : : }
2022 : :
2023 : 45 : return ret;
2024 : : }
2025 : :
2026 : :
2027 : : /****************************************************************************/
2028 : 45 : int CompINChITaut2(const void* p1, const void* p2)
2029 : : {
2030 : : int ret;
2031 : 45 : ret = CompINChI2((const INCHI_SORT*)p1, (const INCHI_SORT*)p2, TAUT_YES, 1);
2032 : : #if ( CANON_FIXH_TRANS == 1 )
2033 [ + + ]: 45 : if (!ret)
2034 : : {
2035 : : /* to obtain canonical transposition 2004-05-10 */
2036 : 12 : ret = CompINChI2((const INCHI_SORT*)p1, (const INCHI_SORT*)p2, TAUT_NON, 1);
2037 : : }
2038 : : #endif
2039 [ + + ]: 45 : if (!ret)
2040 : : {
2041 : : /* stable sort */
2042 : 12 : ret = ((const INCHI_SORT*)p1)->ord_number - ((const INCHI_SORT*)p2)->ord_number;
2043 : : }
2044 : :
2045 : 45 : return ret;
2046 : : }
2047 : :
2048 : :
2049 : : /****************************************************************************
2050 : : mystrrev( ... )
2051 : : NB:
2052 : : strrev from K&R is not in ANSI-compatible C library
2053 : : ****************************************************************************/
2054 : 2534 : void mystrrev(char* p)
2055 : : {
2056 : 2534 : char c, * q = p;
2057 [ + + ]: 5539 : while (*q++)
2058 : : {
2059 : : ;
2060 : : }
2061 : 2534 : q -= 2; /* pointer to the last character */
2062 [ + + ]: 3005 : while (p < q)
2063 : : {
2064 : 471 : c = *q; /* swap */
2065 : 471 : *q-- = *p;
2066 : 471 : *p++ = c;
2067 : : }
2068 : 2534 : }
2069 : :
2070 : :
2071 : : /****************************************************************************
2072 : : Find DFS order for CT(canon. numbers and Hs) output
2073 : : ****************************************************************************/
2074 : : typedef struct tagOrderStruct
2075 : : {
2076 : : AT_NUMB* m_gDfs4CT_nDfsNumber;
2077 : : AT_NUMB* m_gDfs4CT_nNumDescendants;
2078 : : int m_gDfs4CT_nCurrentAtom;
2079 : : } OrderStruct;
2080 : :
2081 : :
2082 : : /****************************************************************************/
2083 : 723 : static int CompareDfsDescendants4CT(const void* a1, const void* a2, void* p)
2084 : : {
2085 : 723 : OrderStruct* os = (OrderStruct*)p;
2086 : 723 : int neigh1 = (int)*(const AT_RANK*)a1;
2087 : 723 : int neigh2 = (int)*(const AT_RANK*)a2;
2088 [ + + ]: 723 : if (neigh1 > MAX_ATOMS)
2089 : : {
2090 [ + + ]: 362 : if (neigh2 > MAX_ATOMS)
2091 : : {
2092 : 24 : return 0;
2093 : : }
2094 : 338 : return 1;
2095 : : }
2096 [ + + ]: 361 : else if (neigh2 > MAX_ATOMS)
2097 : : {
2098 : 168 : return -1;
2099 : : }
2100 : : else
2101 : : {
2102 : 193 : AT_RANK nCurDfsNumber = os->m_gDfs4CT_nDfsNumber[os->m_gDfs4CT_nCurrentAtom];
2103 : 386 : int nDesc1 = nCurDfsNumber > os->m_gDfs4CT_nDfsNumber[neigh1] ?
2104 [ + + ]: 193 : 0 : (int)os->m_gDfs4CT_nNumDescendants[neigh1];
2105 : 386 : int nDesc2 = nCurDfsNumber > os->m_gDfs4CT_nDfsNumber[neigh2] ?
2106 [ + - ]: 193 : 0 : (int)os->m_gDfs4CT_nNumDescendants[neigh2];
2107 : : int ret;
2108 [ + + ]: 193 : if ((ret = nDesc1 - nDesc2)) /* djb-rwth: addressing LLVM warning */
2109 : : {
2110 : 139 : return ret;
2111 : : }
2112 : 54 : return (int)neigh1 - (int)neigh2; /* canon. numbers difference */
2113 : : }
2114 : : }
2115 : :
2116 : :
2117 : : /****************************************************************************/
2118 : 70 : AT_NUMB* GetDfsOrder4CT(CANON_GLOBALS* pCG,
2119 : : AT_NUMB* LinearCT,
2120 : : int nLenCT,
2121 : : S_CHAR* nNum_H,
2122 : : int num_atoms,
2123 : : int nCtMode)
2124 : : {
2125 : 70 : AT_NUMB* nStackAtom = NULL;
2126 : 70 : int nTopStackAtom = -1;
2127 : 70 : AT_NUMB* nNumDescendants = NULL; /* number of descendants incl. closures and the atom itself */
2128 : 70 : AT_NUMB* nDfsNumber = NULL;
2129 : 70 : S_CHAR* cNeighNumb = NULL;
2130 : 70 : NEIGH_LIST* nl = NULL;
2131 : : AT_NUMB nDfs;
2132 : : int i, j, u, k, start, num_rings, nTotOutputStringLen;
2133 : 70 : AT_NUMB* nOutputString = NULL, cDelim;
2134 : 70 : int bCtPredecessors = (nCtMode & CT_MODE_PREDECESSORS);
2135 : : OrderStruct os;
2136 : :
2137 : : /* allocate arrays */
2138 : 70 : nStackAtom = (AT_NUMB*)inchi_malloc(num_atoms * sizeof(nStackAtom[0]));
2139 : 70 : nNumDescendants = (AT_NUMB*)inchi_malloc(num_atoms * sizeof(nNumDescendants[0]));
2140 : 70 : nDfsNumber = (AT_NUMB*)inchi_malloc(num_atoms * sizeof(nDfsNumber[0]));
2141 : 70 : cNeighNumb = (S_CHAR*)inchi_malloc(num_atoms * sizeof(cNeighNumb[0]));
2142 : 70 : nl = CreateNeighListFromLinearCT(LinearCT, nLenCT, num_atoms);
2143 : :
2144 : : /* check allocation */
2145 [ + - + - : 70 : if (!nStackAtom || !nNumDescendants || !nDfsNumber || !cNeighNumb || !nl)
+ - + - -
+ ]
2146 : : {
2147 : : /* ret = CT_OUT_OF_RAM; */ /* program error */ /* <BRKPT> */
2148 : 0 : goto exit_function;
2149 : : }
2150 [ - + ]: 70 : if (bCtPredecessors)
2151 : : {
2152 : 0 : start = 0;
2153 : : }
2154 : : else
2155 : : {
2156 : : /* find DFS start vertex (atom) */
2157 [ + + ]: 632 : for (i = 1, start = 0; i < num_atoms; i++)
2158 : : {
2159 [ + + ]: 562 : if (nl[i][0] < nl[start][0])
2160 : : { /* index = nRank-1 */
2161 : 19 : start = i;
2162 : : }
2163 : : }
2164 : : }
2165 : :
2166 : : /*
2167 : : vertex information:
2168 : : 1. Number of (forward edges) + (back edges, first visit -- ring closures): nl[i][0]
2169 : : 2. Number of vertices traversed from this vertex, including the vertex: nNumDescendants[i]
2170 : : 3. Each edge information:
2171 : : a. forward edge (0) or back edge (1) indicator: nDfsNumber[i] > nDfsNumber[neigh]
2172 : : b. neighbor at another end of the edge neigh = nl[i][k+1], k < i
2173 : :
2174 : : Total per edge: 2 + 2*(number of edges)
2175 : : */
2176 : :
2177 : : /* DFS initiation */
2178 : 70 : u = start; /* start atom */
2179 : 70 : nDfs = 0;
2180 : 70 : nTopStackAtom = -1;
2181 : 70 : memset(nDfsNumber, 0, num_atoms * sizeof(nDfsNumber[0])); /* djb-rwth: memset_s C11/Annex K variant? */
2182 : 70 : memset(nNumDescendants, 0, num_atoms * sizeof(nNumDescendants[0])); /* djb-rwth: memset_s C11/Annex K variant? */
2183 : 70 : memset(cNeighNumb, 0, num_atoms * sizeof(cNeighNumb[0])); /* djb-rwth: memset_s C11/Annex K variant? */
2184 : : /* push the start atom on the stack */
2185 : 70 : nDfsNumber[u] = ++nDfs;
2186 [ - + ]: 70 : if (bCtPredecessors)
2187 : : {
2188 : 0 : nNumDescendants[u] = 0; /* atom #1 has no predecessor */
2189 : : }
2190 : : else
2191 : : {
2192 : 70 : nNumDescendants[u] = 1; /* count itself as a descendant */
2193 : : }
2194 : 70 : nStackAtom[++nTopStackAtom] = (AT_NUMB)u;
2195 : : /* nNumStartChildren = 0; */
2196 : 70 : num_rings = 0;
2197 : :
2198 : : /* DFS */
2199 : : do
2200 : : {
2201 : : /* advance */
2202 [ + + ]: 1806 : while (i = (int)nStackAtom[nTopStackAtom], j = (int)cNeighNumb[i] + 1, (int)nl[i][0] >= j)
2203 : : /*while ( (int)nl[i=nStackAtom[nTopStackAtom]][0] >= (j = (int)cNeighNumb[i]+1) )*/
2204 : :
2205 : : /* replaced due to missing sequence point; undefined behavior, pointed by Geoffrey Hutchison */
2206 : : {
2207 : 1174 : cNeighNumb[i]++;
2208 : 1174 : u = (int)nl[i][j]; /* jth neighbor of the vertex i */
2209 [ + + ]: 1174 : if (!nDfsNumber[u])
2210 : : {
2211 : : /* tree edge, 1st visit -- advance */
2212 : : /* put unexplored vertex u on the stack for further examination */
2213 : 562 : nStackAtom[++nTopStackAtom] = (AT_NUMB)u;
2214 : 562 : nDfsNumber[u] = ++nDfs;
2215 [ - + ]: 562 : if (bCtPredecessors)
2216 : : {
2217 : 0 : nNumDescendants[u] = i + 1; /* predecessor's rank */
2218 : : }
2219 : : else
2220 : : {
2221 : 562 : nNumDescendants[u]++; /* count atom u as its descendant */
2222 : : }
2223 : : }
2224 : : else
2225 : : {
2226 [ + + + + ]: 612 : if (nTopStackAtom && u != (int)nStackAtom[nTopStackAtom - 1] &&
2227 : : /* back edge: u is not a predecessor of i */
2228 [ + + ]: 49 : nDfsNumber[u] < nDfsNumber[i])
2229 : : {
2230 : : /* Back edge, 1st visit: u is an ancestor of i (ring closure) */
2231 [ + - ]: 25 : if (!bCtPredecessors)
2232 : : {
2233 : 25 : nNumDescendants[i]++; /* count closures as descendants */
2234 : : }
2235 : 25 : num_rings++; /* count ring closures */
2236 : : }
2237 : : else
2238 : : {
2239 : 587 : nl[i][j] = MAX_ATOMS + 1; /* back edge, 2nd visit: mark as deleted */
2240 : : }
2241 : : }
2242 : : }
2243 : 632 : cNeighNumb[i] = 0; /* all neighbors of the ith atom have been
2244 : : traversed; resore the neighbor counter */
2245 : : /* back up */
2246 [ + - + + ]: 632 : if (!bCtPredecessors && nTopStackAtom /* that is, i != start */)
2247 : : {
2248 : 562 : u = (int)nStackAtom[nTopStackAtom - 1]; /* predecessor of i */
2249 : 562 : nNumDescendants[u] += nNumDescendants[i]; /* add descendants */
2250 : : }
2251 [ + + ]: 632 : } while (--nTopStackAtom >= 0);
2252 : :
2253 : : /* Sort the neighbors in ascending order so that:
2254 : : primary key = number of descendants in the DFS tree; closure neighbor is 0
2255 : : secondary key = canonical number (here vertex number = canonical number - 1)
2256 : : */
2257 : :
2258 : 70 : os.m_gDfs4CT_nDfsNumber = nDfsNumber;
2259 : 70 : os.m_gDfs4CT_nNumDescendants = nNumDescendants;
2260 : 70 : os.m_gDfs4CT_nCurrentAtom = -1;
2261 : :
2262 : : /* sorting; deleted will be the last neighbors */
2263 [ + + ]: 702 : for (i = 0; i < num_atoms; i++)
2264 : : {
2265 [ + + ]: 632 : if (nl[i][0] > 1)
2266 : : {
2267 : 326 : os.m_gDfs4CT_nCurrentAtom = i;
2268 : 326 : insertions_sort(&os, &nl[i][1], nl[i][0], sizeof(nl[i][1]), CompareDfsDescendants4CT);
2269 : : }
2270 : : /* reduce number of neighbors to exclude deleted */
2271 [ + + + + ]: 1219 : for (k = 0; k < nl[i][0] && nl[i][k + 1] <= MAX_ATOMS; k++)
2272 : : {
2273 : : ;
2274 : : }
2275 : 632 : nl[i][0] = k;
2276 : : }
2277 : :
2278 : 70 : nTotOutputStringLen = 3 * (num_atoms + num_rings + 1); /* last 3 elements are a 'zero termination' */
2279 : :
2280 [ - + ]: 70 : if (bCtPredecessors)
2281 : : {
2282 [ # # ]: 0 : if ((nOutputString = (AT_RANK*)inchi_calloc(nTotOutputStringLen, sizeof(nOutputString[0])))) /* djb-rwth: addressing LLVM warning */
2283 : : {
2284 : 0 : cDelim = '-';
2285 [ # # ]: 0 : for (u = 0, k = -3; u < num_atoms; u++)
2286 : : {
2287 : 0 : k += 3;
2288 [ # # ]: 0 : if (k + 6 > nTotOutputStringLen)
2289 : : {
2290 : 0 : goto exit_error; /* program error */
2291 : : }
2292 [ # # ]: 0 : nOutputString[k] = nNumDescendants[u] ? nNumDescendants[u] : MAX_ATOMS + 1;
2293 [ # # ]: 0 : nOutputString[k + 1] = nNum_H ? 16 + nNum_H[u] : 0;
2294 [ # # ]: 0 : nOutputString[k + 2] = k ? ',' : '\0';
2295 [ # # # # ]: 0 : for (j = 1; j <= nl[u][0] && nDfsNumber[u] > nDfsNumber[i = nl[u][j]]; j++)
2296 : : {
2297 : : /* closures */
2298 : 0 : k += 3;
2299 [ # # ]: 0 : if (k + 6 > nTotOutputStringLen)
2300 : : {
2301 : 0 : goto exit_error; /* program error */
2302 : : }
2303 : 0 : nOutputString[k] = i + 1; /* closure */
2304 : 0 : nOutputString[k + 1] = 0;
2305 : 0 : nOutputString[k + 2] = cDelim;
2306 : : }
2307 : : }
2308 : : }
2309 : : }
2310 : : else
2311 : : {
2312 [ + - ]: 70 : if (nNumDescendants)
2313 : : { /* do not need anymore */
2314 [ + - ]: 70 : inchi_free(nNumDescendants);
2315 : 70 : nNumDescendants = NULL;
2316 : : }
2317 : :
2318 : : /*
2319 : : the output string contains:
2320 : : (num_atoms) atoms for the DFS (spanning) tree
2321 : : (num_atoms-1) delimiters for the DFS (spanning) tree
2322 : : 1 character for each atom that has 1 terminal hydrogen atoms
2323 : : 2 characters for each atom that has 2-9 terminal hydrogen atoms
2324 : : 3 characters for each atom that has 10-99 terminal hydrogen atoms, etc.
2325 : : (num_rings) atoms for the ring closures
2326 : : (num_rings) delimiters for the ring closures
2327 : : */
2328 : :
2329 [ + - ]: 70 : if ((nOutputString = (AT_RANK*)inchi_calloc(nTotOutputStringLen, sizeof(nOutputString[0])))) /* djb-rwth: addressing LLVM warning */
2330 : : {
2331 : 70 : u = start; /* start atom */
2332 : 70 : nTopStackAtom = -1;
2333 : 70 : memset(cNeighNumb, 0, num_atoms * sizeof(cNeighNumb[0])); /* djb-rwth: memset_s C11/Annex K variant? */
2334 : : /* push the start atom on the stack */
2335 : 70 : nStackAtom[++nTopStackAtom] = (AT_NUMB)u;
2336 : : /* output the starting atom */
2337 : 70 : k = 0;
2338 : 70 : nOutputString[k] = u + 1;
2339 [ - + ]: 70 : nOutputString[k + 1] = nNum_H ? 16 + nNum_H[u] : 0;
2340 : 70 : nOutputString[k + 2] = '\0';
2341 : :
2342 : : do
2343 : : {
2344 : : /* advance */
2345 [ + - + + : 1219 : while (i = (int)nStackAtom[nTopStackAtom], j = (int)cNeighNumb[i] + 1, i < num_atoms && (int)nl[i][0] >= j) /* djb-rwth: additional condition to avoid buffer overruns */
+ + ]
2346 : : /*while ( (int)nl[i=nStackAtom[nTopStackAtom]][0] >= (j = (int)cNeighNumb[i]+1) )*/
2347 : : /* replaced due to missing sequence point; undefined behavior, reported by Geoffrey Hutchison */
2348 : : {
2349 : 587 : k += 3;
2350 [ - + ]: 587 : if (k + 6 > nTotOutputStringLen)
2351 : : {
2352 : 0 : goto exit_error; /* program error */
2353 : : }
2354 : 587 : cNeighNumb[i]++;
2355 : 587 : u = (int)nl[i][j]; /* neighbor */
2356 : :
2357 : : /* output neighbor's canonical number */
2358 : 587 : nOutputString[k] = u + 1;
2359 : :
2360 [ + + ]: 587 : if (nDfsNumber[u] > nDfsNumber[i])
2361 : : {
2362 : : /* tree edge, 1st visit -- advance */
2363 : : /* put 'unexplored' vertex u on the stack */
2364 : 562 : nStackAtom[++nTopStackAtom] = (AT_NUMB)u;
2365 : :
2366 : : /* output neighbor's number of H */
2367 [ - + ]: 562 : nOutputString[k + 1] = nNum_H ? 16 + nNum_H[u] : 0;
2368 : : }
2369 : : else
2370 : : {
2371 : 25 : nOutputString[k + 1] = 0;
2372 : : }
2373 : : /* output a delimiter preceding the neighbor */
2374 [ + + ]: 587 : if (1 < nl[i][0])
2375 : : {
2376 [ + + ]: 379 : if (j == 1)
2377 : : {
2378 : 187 : cDelim = '(';
2379 : : }
2380 : : else
2381 : : {
2382 [ + + ]: 192 : if (j == nl[i][0])
2383 : : {
2384 : 187 : cDelim = ')';
2385 : : }
2386 : : else
2387 : : {
2388 : 5 : cDelim = ',';
2389 : : }
2390 : : }
2391 : : }
2392 : : else
2393 : : {
2394 : 208 : cDelim = '-';
2395 : : }
2396 : 587 : nOutputString[k + 2] = cDelim;
2397 : : }
2398 : :
2399 [ + - + - ]: 632 : if ((i >= 0) && (i < num_atoms)) /* djb-rwth: fixing coverity ID #499483 */
2400 : : {
2401 : 632 : cNeighNumb[i] = 0;
2402 : : }
2403 : :
2404 : : /* back up: nothing else to do */
2405 [ + + ]: 632 : } while (--nTopStackAtom >= 0);
2406 : : }
2407 : : }
2408 : 70 : goto exit_function;
2409 : :
2410 : 0 : exit_error:
2411 [ # # ]: 0 : if (nOutputString)
2412 : : {
2413 [ # # ]: 0 : inchi_free(nOutputString);
2414 : 0 : nOutputString = NULL;
2415 : : }
2416 : :
2417 : 0 : exit_function:
2418 [ + - ]: 70 : if (nStackAtom)
2419 : : {
2420 [ + - ]: 70 : inchi_free(nStackAtom);
2421 : : }
2422 [ - + ]: 70 : if (nNumDescendants)
2423 : : {
2424 [ # # ]: 0 : inchi_free(nNumDescendants);
2425 : : }
2426 [ + - ]: 70 : if (nDfsNumber)
2427 : : {
2428 [ + - ]: 70 : inchi_free(nDfsNumber);
2429 : : }
2430 [ + - ]: 70 : if (cNeighNumb)
2431 : : {
2432 [ + - ]: 70 : inchi_free(cNeighNumb);
2433 : : }
2434 [ + - ]: 70 : if (nl)
2435 : : {
2436 : 70 : FreeNeighList(nl);
2437 : : }
2438 : :
2439 : 70 : return nOutputString;
2440 : : }
2441 : :
2442 : :
2443 : : /****************************************************************************/
2444 : 121 : int GetInpStructErrorType(INPUT_PARMS* ip,
2445 : : int err,
2446 : : char* pStrErrStruct,
2447 : : int num_inp_atoms)
2448 : : {
2449 [ - + - - ]: 121 : if (err && err == 9)
2450 : : {
2451 : 0 : return _IS_ERROR; /* sdfile bypassed to $$$$ */
2452 : : }
2453 [ - + - - ]: 121 : if (err && err < 30)
2454 : : {
2455 : 0 : return _IS_FATAL;
2456 : : }
2457 [ + - - + ]: 121 : if (num_inp_atoms <= 0 || err)
2458 : : {
2459 [ # # # # : 0 : if (98 == err && 0 == num_inp_atoms && ip->bAllowEmptyStructure)
# # ]
2460 : : {
2461 : 0 : return _IS_WARNING;
2462 : : }
2463 : 0 : return _IS_ERROR;
2464 : : }
2465 [ + + ]: 121 : if (pStrErrStruct[0])
2466 : : {
2467 : 11 : return _IS_WARNING;
2468 : : }
2469 : :
2470 : 110 : return _IS_OKAY;
2471 : : }
2472 : :
2473 : :
2474 : : /****************************************************************************/
2475 : 0 : int ProcessStructError(INCHI_IOSTREAM* out_file,
2476 : : INCHI_IOSTREAM* log_file,
2477 : : char* pStrErrStruct,
2478 : : int nErrorType,
2479 : : long num_inp,
2480 : : INPUT_PARMS* ip)
2481 : : {
2482 : : int b_ok;
2483 : : #ifdef TARGET_LIB_FOR_WINCHI
2484 : : int bPlainText = (ip->bINChIOutputOptions & INCHI_OUT_PLAIN_TEXT) &&
2485 : : (ip->bINChIOutputOptions & INCHI_OUT_WINCHI_WINDOW);
2486 : : #else
2487 : 0 : int bPlainText = 0;
2488 : : #endif
2489 [ # # ]: 0 : if (!bPlainText)
2490 : : {
2491 : 0 : return nErrorType;
2492 : : }
2493 : :
2494 : : /* Fatal error, Error, Warning */
2495 [ # # ]: 0 : if (nErrorType)
2496 : : {
2497 : 0 : b_ok = 0; /* to make VC++ happy */
2498 [ # # ]: 0 : if (bPlainText)
2499 : : {
2500 [ # # ]: 0 : if (!(b_ok = OutputINChIPlainError(out_file, pStrErrStruct, nErrorType)))
2501 : : {
2502 : 0 : inchi_ios_eprint(log_file, "Cannot create message for error (structure #%ld.%s%s%s%s) Terminating.\n",
2503 [ # # # # : 0 : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
# # # # #
# # # # #
# # # # #
# # # #
# ]
2504 : : }
2505 : : else
2506 : : {
2507 : 0 : inchi_ios_print(out_file, "\n"); /* add a blank line after the WINCHI Window message */
2508 : : }
2509 : : }
2510 [ # # ]: 0 : return b_ok ? nErrorType : _IS_FATAL;
2511 : : }
2512 : :
2513 : 0 : return nErrorType;
2514 : : }
2515 : :
2516 : :
2517 : : #if ( READ_INCHI_STRING == 1 ) /* { */
2518 : : /****************************************************************************/
2519 : 0 : int CompareReversedStereoINChI(INChI_Stereo* s1/* InChI from reversed struct */,
2520 : : INChI_Stereo* s2 /* input InChI */
2521 : : )
2522 : : {
2523 [ # # # # ]: 0 : if (s1 == NULL && s2 == NULL)
2524 : : {
2525 : 0 : return 0;
2526 : : }
2527 [ # # ]: 0 : if ((s1 == NULL) ^ (s2 == NULL))
2528 : : {
2529 [ # # ]: 0 : INChI_Stereo* s = s1 ? s1 : s2;
2530 [ # # # # ]: 0 : if (s->nNumberOfStereoCenters || s->nNumberOfStereoBonds)
2531 : : {
2532 : 0 : return 20; /* Diff: Missing Stereo */
2533 : : }
2534 : : else
2535 : : {
2536 : 0 : return 0;
2537 : : }
2538 : : }
2539 : :
2540 [ # # ]: 0 : if (s1->nNumberOfStereoCenters != s2->nNumberOfStereoCenters)
2541 : : {
2542 : 0 : return 21; /* Diff: Number of sp3 stereocenters */
2543 : : }
2544 [ # # ]: 0 : if (s1->nNumberOfStereoCenters > 0)
2545 : : {
2546 [ # # ]: 0 : if (memcmp(s1->nNumber, s2->nNumber, s1->nNumberOfStereoCenters * sizeof(s1->nNumber[0])))
2547 : : {
2548 : 0 : return 22; /* Diff: sp3 stereocenter locations */
2549 : : }
2550 [ # # ]: 0 : if (memcmp(s1->t_parity, s2->t_parity, s1->nNumberOfStereoCenters * sizeof(s1->t_parity[0])))
2551 : : {
2552 : 0 : return 23; /* Diff: sp3 stereocenter parities */
2553 : : }
2554 [ # # # # : 0 : if (s1->nCompInv2Abs != s2->nCompInv2Abs && s1->nCompInv2Abs && s2->nCompInv2Abs)
# # ]
2555 : : {
2556 : 0 : return 24; /* Diff: sp3 inversion */
2557 : : }
2558 : : /*
2559 : : if ( s1->nNumberInv && s2->nNumberInv ) {
2560 : : if ( memcmp( s1->nNumberInv, s2->nNumberInv, s1->nNumberOfStereoCenters*sizeof(s1->nNumber[0]) ) )
2561 : : return 25;
2562 : : if ( memcmp( s1->t_parityInv, s2->t_parityInv, s1->nNumberOfStereoCenters*sizeof(s1->t_parity[0]) ) )
2563 : : return 26;
2564 : : if ( s1->nCompInv2Abs != s2->nCompInv2Abs ||
2565 : : s1->bTrivialInv != s2->bTrivialInv ) {
2566 : : return 27;
2567 : : }
2568 : : } else
2569 : : if ( s1->nNumberInv || s2->nNumberInv ) {
2570 : : return 28;
2571 : : }
2572 : : */
2573 : : }
2574 [ # # ]: 0 : if (s1->nNumberOfStereoBonds != s2->nNumberOfStereoBonds)
2575 : : {
2576 : 0 : return 25; /* Diff: Number of stereobonds */
2577 : : }
2578 [ # # ]: 0 : if (s1->nNumberOfStereoBonds > 0)
2579 : : {
2580 [ # # ]: 0 : if (memcmp(s1->nBondAtom1, s2->nBondAtom1, s1->nNumberOfStereoBonds * sizeof(s1->nBondAtom1[0])))
2581 : : {
2582 : 0 : return 26; /* Diff: Stereobond 1st atom locations */
2583 : : }
2584 [ # # ]: 0 : if (memcmp(s1->nBondAtom2, s2->nBondAtom2, s1->nNumberOfStereoBonds * sizeof(s1->nBondAtom2[0])))
2585 : : {
2586 : 0 : return 27; /* Diff: Stereobond 2nd atom locations */
2587 : : }
2588 [ # # ]: 0 : if (memcmp(s1->b_parity, s2->b_parity, s1->nNumberOfStereoBonds * sizeof(s1->b_parity[0])))
2589 : : {
2590 : 0 : return 28; /* Diff: Stereobond parities */
2591 : : }
2592 : : }
2593 : :
2594 : 0 : return 0;
2595 : : }
2596 : :
2597 : :
2598 : : /****************************************************************************/
2599 : 0 : int CompareReversedStereoINChI2(INChI_Stereo* s1/* InChI from reversed struct */,
2600 : : INChI_Stereo* s2 /* input InChI */,
2601 : : ICR* picr)
2602 : : {
2603 : 0 : int ret = 0;
2604 : : int j1, j2, num_dif, num_extra_undf, num_miss_undf, num_in1_only, num_in2_only; /* djb-rwth: removing redundant variables */
2605 : 0 : int bAddSb = !(picr->num_sb_undef_in1_only + picr->num_sb_in1_only + picr->num_sb_in2_only);
2606 : 0 : int bAddSc = !(picr->num_sc_undef_in1_only + picr->num_sc_in1_only + picr->num_sc_in2_only);
2607 : :
2608 [ # # ]: 0 : int nNumSc1 = s1 ? s1->nNumberOfStereoCenters : 0;
2609 [ # # ]: 0 : int nNumSc2 = s2 ? s2->nNumberOfStereoCenters : 0;
2610 [ # # ]: 0 : int nNumSb1 = s1 ? s1->nNumberOfStereoBonds : 0;
2611 [ # # ]: 0 : int nNumSb2 = s2 ? s2->nNumberOfStereoBonds : 0;
2612 : :
2613 : : /* djb-rwth: redundant part of the condition corrected; fixing a NULL pointer dereference */
2614 [ # # # # : 0 : if (s1 && s2 && (nNumSc1 || nNumSc2) && (nNumSc1 != nNumSc2 ||
# # # # #
# ]
2615 [ # # ]: 0 : memcmp(s1->nNumber, s2->nNumber, nNumSc1 * sizeof(s1->nNumber[0])) ||
2616 [ # # ]: 0 : memcmp(s1->t_parity, s2->t_parity, nNumSc1 * sizeof(s1->t_parity[0]))))
2617 : : {
2618 : 0 : num_dif = num_extra_undf = num_miss_undf = num_in1_only = num_in2_only = 0;
2619 [ # # # # ]: 0 : for (j1 = j2 = 0; j1 < nNumSc1 && j2 < nNumSc2; )
2620 : : {
2621 [ # # ]: 0 : if (s1->nNumber[j1] == s2->nNumber[j2])
2622 : : {
2623 [ # # ]: 0 : if (s1->t_parity[j1] != s2->t_parity[j2]) /* djb-rwth: removing redundant code */
2624 : : {
2625 : 0 : num_dif++;
2626 : : }
2627 : 0 : j1++;
2628 : 0 : j2++;
2629 : : }
2630 : : else
2631 : : {
2632 [ # # ]: 0 : if (s1->nNumber[j1] < s2->nNumber[j2])
2633 : : {
2634 : 0 : num_in1_only++;
2635 [ # # ]: 0 : if (s1->t_parity[j1] == AB_PARITY_UNDF)
2636 : : {
2637 : 0 : num_extra_undf++;
2638 : : }
2639 [ # # ]: 0 : if (bAddSc)
2640 : : {
2641 [ # # ]: 0 : if (picr->num_sc_in1_only < ICR_MAX_SC_IN1_ONLY)
2642 : : {
2643 : 0 : picr->sc_in1_only[picr->num_sc_in1_only++] = j1;
2644 : : }
2645 [ # # ]: 0 : if (s1->t_parity[j1] == AB_PARITY_UNDF)
2646 : : {
2647 [ # # ]: 0 : if (picr->num_sc_undef_in1_only < ICR_MAX_SC_UNDF)
2648 : : {
2649 : 0 : picr->sc_undef_in1_only[picr->num_sc_undef_in1_only++] = j1;
2650 : : }
2651 : : }
2652 : : }
2653 : 0 : j1++;
2654 : : }
2655 : : else
2656 : : {
2657 : 0 : num_in2_only++;
2658 [ # # ]: 0 : if (s2->t_parity[j2] == AB_PARITY_UNDF)
2659 : : {
2660 : 0 : num_miss_undf++;
2661 : : }
2662 [ # # ]: 0 : if (bAddSc)
2663 : : {
2664 [ # # ]: 0 : if (picr->num_sc_in2_only < ICR_MAX_SC_IN2_ONLY)
2665 : : {
2666 : 0 : picr->sc_in2_only[picr->num_sc_in2_only++] = j2;
2667 : : }
2668 [ # # ]: 0 : if (s2->t_parity[j2] == AB_PARITY_UNDF)
2669 : : {
2670 [ # # ]: 0 : if (picr->num_sc_undef_in2_only < ICR_MAX_SC_UNDF)
2671 : : {
2672 : 0 : picr->sc_undef_in2_only[picr->num_sc_undef_in2_only++] = j1;
2673 : : }
2674 : : }
2675 : : }
2676 : 0 : j2++;
2677 : : }
2678 : : }
2679 : : }
2680 [ # # ]: 0 : while (j1 < nNumSc1)
2681 : : {
2682 [ # # ]: 0 : if (s1->t_parity[j1] == AB_PARITY_UNDF)
2683 : : {
2684 : 0 : num_extra_undf++;
2685 : : }
2686 : 0 : num_in1_only++;
2687 [ # # ]: 0 : if (bAddSc)
2688 : : {
2689 [ # # ]: 0 : if (picr->num_sc_in1_only < ICR_MAX_SC_IN1_ONLY)
2690 : : {
2691 : 0 : picr->sc_in1_only[picr->num_sc_in1_only++] = j1;
2692 : : }
2693 [ # # ]: 0 : if (s1->t_parity[j1] == AB_PARITY_UNDF)
2694 : : {
2695 [ # # ]: 0 : if (picr->num_sc_undef_in1_only < ICR_MAX_SC_UNDF)
2696 : : {
2697 : 0 : picr->sc_undef_in1_only[picr->num_sc_undef_in1_only++] = j1;
2698 : : }
2699 : : }
2700 : : }
2701 : 0 : j1++;
2702 : : }
2703 [ # # ]: 0 : while (j2 < nNumSc2)
2704 : : {
2705 [ # # ]: 0 : if (s2->t_parity[j2] == AB_PARITY_UNDF)
2706 : : {
2707 : 0 : num_miss_undf++;
2708 : : }
2709 : 0 : num_in2_only++;
2710 [ # # ]: 0 : if (bAddSc)
2711 : : {
2712 [ # # ]: 0 : if (picr->num_sc_in2_only < ICR_MAX_SC_IN2_ONLY)
2713 : : {
2714 : 0 : picr->sc_in2_only[picr->num_sc_in2_only++] = j2;
2715 : : }
2716 : : }
2717 : 0 : j2++;
2718 : : }
2719 [ # # ]: 0 : if (num_dif)
2720 : : {
2721 : 0 : ret |= IDIF_SC_PARITY;
2722 : : }
2723 [ # # ]: 0 : if (num_in1_only)
2724 : : {
2725 [ # # ]: 0 : if (num_extra_undf)
2726 : : {
2727 : 0 : ret |= IDIF_SC_EXTRA_UNDF;
2728 : : }
2729 [ # # ]: 0 : if (num_in1_only != num_extra_undf)
2730 : : {
2731 : 0 : ret |= IDIF_SC_EXTRA;
2732 : : }
2733 : : }
2734 [ # # ]: 0 : if (num_in2_only)
2735 : : {
2736 [ # # ]: 0 : if (num_miss_undf)
2737 : : {
2738 : 0 : ret |= IDIF_SC_MISS_UNDF;
2739 : : }
2740 [ # # ]: 0 : if (num_in2_only != num_miss_undf)
2741 : : {
2742 : 0 : ret |= IDIF_SC_MISS;
2743 : : }
2744 : : }
2745 : : }
2746 [ # # # # : 0 : if (s1 && s2 && s1->nCompInv2Abs != s2->nCompInv2Abs && s1->nCompInv2Abs && s2->nCompInv2Abs)
# # # # #
# ]
2747 : : {
2748 : 0 : ret |= IDIF_SC_INV;
2749 : : }
2750 : :
2751 [ # # # # : 0 : if ((nNumSb1 || nNumSb2) &&
# # ]
2752 : 0 : (nNumSb1 != nNumSb2 ||
2753 [ # # ]: 0 : memcmp(s1->nBondAtom1, s2->nBondAtom1, nNumSb1 * sizeof(s1->nBondAtom1[0])) ||
2754 [ # # ]: 0 : memcmp(s1->nBondAtom2, s2->nBondAtom2, nNumSb1 * sizeof(s1->nBondAtom2[0])) ||
2755 [ # # ]: 0 : memcmp(s1->b_parity, s2->b_parity, nNumSb1 * sizeof(s1->b_parity[0]))))
2756 : : {
2757 : 0 : num_dif = num_extra_undf = num_miss_undf = num_in1_only = num_in2_only = 0; /* djb-rwth: removing redundant code */
2758 [ # # # # ]: 0 : for (j1 = j2 = 0; j1 < nNumSb1 && j2 < nNumSb2; )
2759 : : {
2760 [ # # ]: 0 : if (s1->nBondAtom1[j1] == s2->nBondAtom1[j2] &&
2761 [ # # ]: 0 : s1->nBondAtom2[j1] == s2->nBondAtom2[j2])
2762 : : {
2763 [ # # ]: 0 : if (s1->b_parity[j1] != s2->b_parity[j2]) /* djb-rwth: removing redundant code */
2764 : : {
2765 : 0 : num_dif++;
2766 : : }
2767 : 0 : j1++;
2768 : 0 : j2++;
2769 : : }
2770 : : else
2771 : : {
2772 [ # # ]: 0 : if (s1->nBondAtom1[j1] < s2->nBondAtom1[j2] ||
2773 [ # # # # ]: 0 : (s1->nBondAtom1[j1] == s2->nBondAtom1[j2] && s1->nBondAtom2[j1] < s2->nBondAtom2[j2])) /* djb-rwth: addressing LLVM warning */
2774 : : {
2775 : 0 : num_in1_only++;
2776 [ # # ]: 0 : if (s1->b_parity[j1] == AB_PARITY_UNDF)
2777 : : {
2778 : 0 : num_extra_undf++;
2779 : : }
2780 [ # # ]: 0 : if (bAddSb)
2781 : : {
2782 [ # # ]: 0 : if (picr->num_sb_in1_only < ICR_MAX_SB_IN1_ONLY)
2783 : : {
2784 : 0 : picr->sb_in1_only[picr->num_sb_in1_only++] = j1;
2785 : : }
2786 [ # # ]: 0 : if (s1->b_parity[j1] == AB_PARITY_UNDF)
2787 : : {
2788 [ # # ]: 0 : if (picr->num_sb_undef_in1_only < ICR_MAX_SB_UNDF)
2789 : : {
2790 : 0 : picr->sb_undef_in1_only[picr->num_sb_undef_in1_only++] = j1;
2791 : : }
2792 : : }
2793 : : }
2794 : 0 : j1++;
2795 : : }
2796 : : else
2797 : : {
2798 : 0 : num_in2_only++;
2799 [ # # ]: 0 : if (s2->b_parity[j2] == AB_PARITY_UNDF)
2800 : : {
2801 : 0 : num_miss_undf++;
2802 : : }
2803 [ # # ]: 0 : if (bAddSb)
2804 : : {
2805 [ # # ]: 0 : if (picr->num_sb_in2_only < ICR_MAX_SB_IN2_ONLY)
2806 : : {
2807 : 0 : picr->sb_in2_only[picr->num_sb_in2_only++] = j2;
2808 : : }
2809 [ # # ]: 0 : if (s2->b_parity[j2] == AB_PARITY_UNDF)
2810 : : {
2811 [ # # ]: 0 : if (picr->num_sb_undef_in2_only < ICR_MAX_SB_UNDF)
2812 : : {
2813 : 0 : picr->sb_undef_in2_only[picr->num_sb_undef_in2_only++] = j1;
2814 : : }
2815 : : }
2816 : : }
2817 : 0 : j2++;
2818 : : }
2819 : : }
2820 : : }
2821 [ # # ]: 0 : while (j1 < nNumSb1)
2822 : : {
2823 : 0 : num_in1_only++;
2824 [ # # ]: 0 : if (s1->b_parity[j1] == AB_PARITY_UNDF)
2825 : : {
2826 : 0 : num_extra_undf++;
2827 : : }
2828 [ # # ]: 0 : if (bAddSb)
2829 : : {
2830 [ # # ]: 0 : if (picr->num_sb_in1_only < ICR_MAX_SB_IN1_ONLY)
2831 : : {
2832 : 0 : picr->sb_in1_only[picr->num_sb_in1_only++] = j1;
2833 : : }
2834 [ # # ]: 0 : if (s1->b_parity[j1] == AB_PARITY_UNDF)
2835 : : {
2836 [ # # ]: 0 : if (picr->num_sb_undef_in1_only < ICR_MAX_SB_UNDF)
2837 : : {
2838 : 0 : picr->sb_undef_in1_only[picr->num_sb_undef_in1_only++] = j1;
2839 : : }
2840 : : }
2841 : : }
2842 : 0 : j1++;
2843 : : }
2844 [ # # ]: 0 : while (j2 < nNumSb2)
2845 : : {
2846 : 0 : num_in2_only++;
2847 [ # # ]: 0 : if (s2->b_parity[j2] == AB_PARITY_UNDF)
2848 : : {
2849 : 0 : num_miss_undf++;
2850 : : }
2851 [ # # ]: 0 : if (bAddSb)
2852 : : {
2853 [ # # ]: 0 : if (picr->num_sb_in2_only < ICR_MAX_SB_IN2_ONLY)
2854 : : {
2855 : 0 : picr->sb_in2_only[picr->num_sb_in2_only++] = j2;
2856 : : }
2857 [ # # ]: 0 : if (s2->b_parity[j2] == AB_PARITY_UNDF)
2858 : : {
2859 [ # # ]: 0 : if (picr->num_sb_undef_in2_only < ICR_MAX_SB_UNDF)
2860 : : {
2861 : 0 : picr->sb_undef_in2_only[picr->num_sb_undef_in2_only++] = j1;
2862 : : }
2863 : : }
2864 : : }
2865 : 0 : j2++;
2866 : : }
2867 [ # # ]: 0 : if (num_dif)
2868 : : {
2869 : 0 : ret |= IDIF_SB_PARITY;
2870 : : }
2871 [ # # ]: 0 : if (num_in1_only)
2872 : : {
2873 [ # # ]: 0 : if (num_extra_undf)
2874 : : {
2875 : 0 : ret |= IDIF_SB_EXTRA_UNDF;
2876 : : }
2877 [ # # ]: 0 : if (num_in1_only != num_extra_undf)
2878 : : {
2879 : 0 : ret |= IDIF_SB_EXTRA;
2880 : : }
2881 : : }
2882 [ # # ]: 0 : if (num_in2_only)
2883 : : {
2884 [ # # ]: 0 : if (num_miss_undf)
2885 : : {
2886 : 0 : ret |= IDIF_SB_MISS_UNDF;
2887 : : }
2888 [ # # ]: 0 : if (num_in2_only != num_miss_undf)
2889 : : {
2890 : 0 : ret |= IDIF_SB_MISS;
2891 : : }
2892 : : }
2893 : : }
2894 : :
2895 : 0 : return ret;
2896 : : }
2897 : :
2898 : :
2899 : : /****************************************************************************/
2900 : 0 : int CompareReversedINChI(INChI* i1 /* InChI from reversed struct */,
2901 : : INChI* i2 /* input InChI */,
2902 : : INChI_Aux* a1,
2903 : : INChI_Aux* a2)
2904 : : {
2905 : : int ret;
2906 : :
2907 [ # # # # ]: 0 : if (i1 == NULL && i2 == NULL)
2908 : : {
2909 : 0 : return 0;
2910 : : }
2911 [ # # ]: 0 : if ((i1 == NULL) ^ (i2 == NULL))
2912 : : {
2913 : 0 : return 1; /* Diff: Missing InChI */
2914 : : }
2915 : :
2916 [ # # ]: 0 : if (i1->nErrorCode == i2->nErrorCode)
2917 : : {
2918 [ # # ]: 0 : if (i1->nErrorCode)
2919 : : {
2920 : 0 : return 0;
2921 : : }
2922 : : }
2923 : : else
2924 : : {
2925 : 0 : return 2; /* Diff: Error codes */
2926 : : }
2927 [ # # ]: 0 : if (i1->bDeleted != i2->bDeleted)
2928 : : {
2929 : 0 : return 1; /* Diff: Missing InChI */
2930 : : }
2931 [ # # ]: 0 : if (i1->nNumberOfAtoms != i2->nNumberOfAtoms)
2932 : : {
2933 : 0 : return 3; /* Diff: Num. atoms */
2934 : : }
2935 : :
2936 [ # # ]: 0 : if (i1->nNumberOfAtoms > 0)
2937 : : {
2938 [ # # ]: 0 : if (memcmp(i1->nAtom, i2->nAtom, i1->nNumberOfAtoms * sizeof(i1->nAtom[0])))
2939 : : {
2940 : 0 : return 4; /* Diff: Elements */
2941 : : }
2942 [ # # ]: 0 : if (strcmp(i1->szHillFormula, i2->szHillFormula))
2943 : : {
2944 : 0 : return 7; /* Diff: Hill Formulas */
2945 : : }
2946 [ # # ]: 0 : if (memcmp(i1->nNum_H, i2->nNum_H, i1->nNumberOfAtoms * sizeof(i1->nNum_H[0])))
2947 : : {
2948 [ # # # # ]: 0 : if (i1->lenConnTable > 1 || i2->lenConnTable > 1)
2949 : : {
2950 : 0 : return 5; /* Diff: H Locations (mobile H present) */
2951 : : }
2952 : : else
2953 : : {
2954 : 0 : return 6; /* Diff: H Locations (no mobile H) */
2955 : : }
2956 : : }
2957 : :
2958 : : /* fixed H */
2959 [ # # # # ]: 0 : if (i1->nNum_H_fixed || i2->nNum_H_fixed)
2960 : : {
2961 : 0 : int bHasFixedH1 = 0, bHasFixedH2 = 0, i, j1, j2;
2962 [ # # ]: 0 : if (i1->nNum_H_fixed)
2963 : : {
2964 [ # # ]: 0 : for (i = 0; i < i1->nNumberOfAtoms; i++)
2965 : : {
2966 [ # # ]: 0 : if (i1->nNum_H_fixed[i])
2967 : : {
2968 : 0 : bHasFixedH1++;
2969 : : }
2970 : : }
2971 : : }
2972 [ # # ]: 0 : if (i2->nNum_H_fixed)
2973 : : {
2974 [ # # ]: 0 : for (i = 0; i < i2->nNumberOfAtoms; i++)
2975 : : {
2976 [ # # ]: 0 : if (i2->nNum_H_fixed[i])
2977 : : {
2978 : 0 : bHasFixedH2++;
2979 : : }
2980 : : }
2981 : : }
2982 : :
2983 : : /* count the differences */
2984 : 0 : j1 = j2 = 0;
2985 [ # # # # ]: 0 : if (bHasFixedH1 && !bHasFixedH2)
2986 : : {
2987 [ # # ]: 0 : for (i = 0; i < i1->nNumberOfAtoms; i++)
2988 : : {
2989 [ # # ]: 0 : if (i1->nNum_H_fixed[i] > 0)
2990 : : {
2991 : 0 : j1++;
2992 : : }
2993 : : else
2994 [ # # ]: 0 : if (i1->nNum_H_fixed[i] < 0)
2995 : : {
2996 : 0 : j2++;
2997 : : }
2998 : : }
2999 : 0 : return 18; /* Diff: Extra Fixed-H */
3000 : : }
3001 : : else
3002 : : {
3003 [ # # # # ]: 0 : if (!bHasFixedH1 && bHasFixedH2)
3004 : : {
3005 [ # # ]: 0 : for (i = j1 = j2 = 0; i < i1->nNumberOfAtoms; i++)
3006 : : {
3007 [ # # ]: 0 : if (0 > i2->nNum_H_fixed[i])
3008 : : {
3009 : 0 : j1++;
3010 : : }
3011 : : else
3012 [ # # ]: 0 : if (0 < i2->nNum_H_fixed[i])
3013 : : {
3014 : 0 : j2++;
3015 : : }
3016 : : }
3017 : 0 : return 19; /* Diff: Missed Fixed-H */
3018 : : }
3019 : : else
3020 : : {
3021 [ # # # # ]: 0 : if (bHasFixedH1 && bHasFixedH2 &&
3022 [ # # ]: 0 : memcmp(i1->nNum_H_fixed, i2->nNum_H_fixed, i1->nNumberOfAtoms * sizeof(i1->nNum_H_fixed[0])))
3023 : : {
3024 [ # # ]: 0 : for (i = j1 = j2 = 0; i < i1->nNumberOfAtoms; i++)
3025 : : {
3026 [ # # ]: 0 : if (i1->nNum_H_fixed[i] > i2->nNum_H_fixed[i])
3027 : : {
3028 : 0 : j1++;
3029 : : }
3030 : : else
3031 : : {
3032 [ # # ]: 0 : if (i1->nNum_H_fixed[i] < i2->nNum_H_fixed[i])
3033 : : {
3034 : 0 : j2++;
3035 : : }
3036 : : }
3037 : : }
3038 : : }
3039 : : }
3040 : : }
3041 [ # # # # : 0 : ret = (j1 && j2) ? 20 : j1 ? 18 : j2 ? 19 : 0;
# # # # ]
3042 [ # # ]: 0 : if (ret)
3043 : : {
3044 : 0 : return ret; /* 20 => Diff: NotEql Fixed-H */
3045 : : /* 19 => Diff: Missed Fixed-H (i1 has less) */
3046 : : /* 18 => Diff: Extra Fixed-H (i1 has more) */
3047 : : }
3048 : : }
3049 : : }
3050 : :
3051 [ # # ]: 0 : if (i1->lenConnTable != i2->lenConnTable)
3052 : : {
3053 : 0 : return 8; /* Diff: Connections length */
3054 : : }
3055 [ # # # # ]: 0 : if (i1->lenConnTable > 0 && memcmp(i1->nConnTable, i2->nConnTable, i1->lenConnTable * sizeof(i1->nConnTable[0])))
3056 : : {
3057 : 0 : return 9; /* Diff: Connections */
3058 : : }
3059 : :
3060 : : /* output special cases: different number of t-groups, different sizes of t-groups, different endpoints */
3061 [ # # # # : 0 : if (i1->lenTautomer != i2->lenTautomer && (i1->lenTautomer > 1 || i2->lenTautomer > 1))
# # ]
3062 : : {
3063 : 0 : return 10; /* Diff: Mobile groups length */ /* in isotopic or deprotonated cases i1->lenTautomer == 1 && i1->nTautomer[0] = 0 */
3064 : : }
3065 [ # # # # ]: 0 : if ((i1->lenTautomer > 1 && i2->lenTautomer > 1) &&
3066 [ # # ]: 0 : memcmp(i1->nTautomer, i2->nTautomer, i1->lenTautomer * sizeof(i1->nTautomer[0])))
3067 : : {
3068 : 0 : return 11; /* Diff: Mobile groups */
3069 : : }
3070 : :
3071 [ # # ]: 0 : if (i1->nNumberOfIsotopicAtoms != i2->nNumberOfIsotopicAtoms)
3072 : : {
3073 : 0 : return 12; /* Diff: Isotopic atoms number */
3074 : : }
3075 [ # # # # ]: 0 : if (i1->nNumberOfIsotopicAtoms > 0 && memcmp(i1->IsotopicAtom, i2->IsotopicAtom, i1->nNumberOfIsotopicAtoms * sizeof(i1->IsotopicAtom[0])))
3076 : : {
3077 : 0 : return 13; /* Diff: Isotopic atoms */
3078 : : }
3079 [ # # ]: 0 : if (i1->nTotalCharge != i2->nTotalCharge)
3080 : : {
3081 : 0 : return 14; /* Diff: Charge */
3082 : : }
3083 : :
3084 : : /*
3085 : : if ( i1->nNumberOfIsotopicTGroups != i2->nNumberOfIsotopicTGroups )
3086 : : {
3087 : : return 14;
3088 : : }
3089 : : if ( i1->nNumberOfIsotopicTGroups > 0 && memcmp( i1->IsotopicTGroup, i2->IsotopicTGroup, i1->nNumberOfIsotopicTGroups*sizeof(i1->IsotopicTGroup[0]) ) )
3090 : : {
3091 : : return 15;
3092 : : }
3093 : : */
3094 : :
3095 [ # # # # ]: 0 : if (a1 && a2)
3096 : : {
3097 [ # # ]: 0 : if (a1->nNumRemovedProtons != a2->nNumRemovedProtons)
3098 : : {
3099 : 0 : return 16; /* Diff: Number of removed protons */
3100 : : }
3101 [ # # ]: 0 : if (memcmp(a1->nNumRemovedIsotopicH, a2->nNumRemovedIsotopicH, sizeof(a1->nNumRemovedIsotopicH)))
3102 : : {
3103 : 0 : return 17; /* Diff: Removed isotopic H */
3104 : : }
3105 : : }
3106 : :
3107 : : /*
3108 : : if ( i1->nPossibleLocationsOfIsotopicH && i2->nPossibleLocationsOfIsotopicH ) {
3109 : : {
3110 : : if ( i1->nPossibleLocationsOfIsotopicH[0] != i2->nPossibleLocationsOfIsotopicH[0] ||
3111 : : memcmp(i1->nPossibleLocationsOfIsotopicH, i2->nPossibleLocationsOfIsotopicH,
3112 : : sizeof(i1->nPossibleLocationsOfIsotopicH[0])*i1->nPossibleLocationsOfIsotopicH[0]) )
3113 : : {
3114 : : return 18;
3115 : : }
3116 : : }
3117 : : } else
3118 : : {
3119 : : if ( !i1->nPossibleLocationsOfIsotopicH != !i2->nPossibleLocationsOfIsotopicH ) {
3120 : : {
3121 : : return 19;}
3122 : : }
3123 : : }
3124 : : */
3125 : :
3126 : : /* ret = 20..31 => 40..51 */
3127 [ # # ]: 0 : if ((ret = CompareReversedStereoINChI(i1->Stereo, i2->Stereo))) /* djb-rwth: addressing LLVM warning */
3128 : : {
3129 : 0 : return ret + 20;
3130 : : }
3131 : : /* ret = 40..51 => 60..71 */
3132 : :
3133 [ # # # # : 0 : if (!i2->StereoIsotopic && i2->Stereo && i1->StereoIsotopic &&
# # ]
3134 [ # # # # ]: 0 : 0 < (i1->StereoIsotopic->nNumberOfStereoBonds + i1->StereoIsotopic->nNumberOfStereoCenters) &&
3135 : 0 : 0 == CompareReversedStereoINChI(i1->StereoIsotopic, i2->Stereo))
3136 : : {
3137 : : /* InChI from reversed structure does not contain fully duplicated isotopic stereo */
3138 : : ;
3139 : : }
3140 : : else
3141 : : {
3142 [ # # ]: 0 : if ((ret = CompareReversedStereoINChI(i1->StereoIsotopic, i2->StereoIsotopic))) /* djb-rwth: addressing LLVM warning */
3143 : : {
3144 : 0 : return ret + 40;
3145 : : }
3146 : : }
3147 : :
3148 : 0 : return 0;
3149 : : }
3150 : :
3151 : :
3152 : : /****************************************************************************/
3153 : 0 : int CompareIcr(ICR* picr1,
3154 : : ICR* picr2,
3155 : : INCHI_MODE* pin1,
3156 : : INCHI_MODE* pin2,
3157 : : INCHI_MODE mask)
3158 : : {
3159 : 0 : int nNumExtraBits1 = 0, nNumExtraBits2 = 0, bit1, bit2;
3160 : 0 : INCHI_MODE Flg1 = picr1->flags, Flg2 = picr2->flags, cur_bit = 1, in1, in2;
3161 : : int i, ret;
3162 : :
3163 : : /* compare flags */
3164 : 0 : in1 = in2 = 0;
3165 [ # # # # ]: 0 : for (i = 0; Flg1 || Flg2; i++, Flg1 >>= 1, Flg2 >>= 1, cur_bit <<= 1)
3166 : : {
3167 [ # # ]: 0 : if (!(mask & cur_bit))
3168 : : {
3169 : 0 : continue;
3170 : : }
3171 : 0 : bit1 = Flg1 & 1;
3172 : 0 : bit2 = Flg2 & 1;
3173 [ # # # # ]: 0 : if (bit1 && !bit2)
3174 : : {
3175 : 0 : in1 |= 1 << i;
3176 : 0 : nNumExtraBits1++;
3177 : : }
3178 : : else
3179 : : {
3180 [ # # # # ]: 0 : if (!bit1 && bit2)
3181 : : {
3182 : 0 : in2 |= 1 << i;
3183 : 0 : nNumExtraBits2++;
3184 : : }
3185 : : }
3186 : : }
3187 [ # # # # ]: 0 : if (nNumExtraBits1 && !nNumExtraBits2)
3188 : : {
3189 : 0 : ret = 1;
3190 : : }
3191 : : else
3192 : : {
3193 [ # # # # ]: 0 : if (!nNumExtraBits1 && nNumExtraBits2)
3194 : : {
3195 : 0 : ret = -1;
3196 : : }
3197 : : else
3198 : : {
3199 [ # # # # ]: 0 : if (!in1 && !in2)
3200 : : {
3201 : 0 : ret = 0;
3202 : : }
3203 : : else
3204 : : {
3205 : 0 : ret = 2; /* compare produced undefined results */
3206 : : }
3207 : : }
3208 : : }
3209 : :
3210 [ # # ]: 0 : if (pin1)
3211 : : {
3212 : 0 : *pin1 = in1;
3213 : : }
3214 [ # # ]: 0 : if (pin2)
3215 : : {
3216 : 0 : *pin2 = in2;
3217 : : }
3218 : :
3219 : : /* more detailed compare not implemented */
3220 : :
3221 : 0 : return ret;
3222 : : }
3223 : :
3224 : :
3225 : : /****************************************************************************/
3226 : 0 : INCHI_MODE CompareReversedINChI2(INChI* i1 /* InChI from reversed struct */,
3227 : : INChI* i2 /* input InChI */,
3228 : : INChI_Aux* a1,
3229 : : INChI_Aux* a2,
3230 : : ICR* picr,
3231 : : int* err)
3232 : : {
3233 : 0 : INCHI_MODE ret = 0;
3234 : 0 : INChI_Stereo* Stereo1 = NULL, * Stereo2 = NULL;
3235 : : int n1, n2, m, j, j1, j2, ret2, num_H1, num_H2;
3236 : :
3237 : 0 : *err = 0;
3238 : :
3239 : 0 : memset(picr, 0, sizeof(*picr)); /* djb-rwth: memset_s C11/Annex K variant? */
3240 : :
3241 [ # # # # ]: 0 : if (i1 == NULL && i2 == NULL)
3242 : : {
3243 : 0 : return 0;
3244 : : }
3245 [ # # ]: 0 : if ((i1 == NULL) ^ (i2 == NULL))
3246 : : {
3247 : 0 : ret |= IDIF_PROBLEM; /* one InChI exists while another doesn't */
3248 : 0 : goto exit_function;
3249 : : }
3250 : :
3251 [ # # ]: 0 : if (i1->nErrorCode == i2->nErrorCode)
3252 : : {
3253 [ # # ]: 0 : if (i1->nErrorCode)
3254 : : {
3255 : 0 : ret |= IDIF_PROBLEM; /* both InChI have same error codes */
3256 : 0 : goto exit_function;
3257 : : }
3258 : : }
3259 : : else
3260 : : {
3261 : 0 : ret |= IDIF_PROBLEM; /* at least one InChI has an error code */
3262 : 0 : goto exit_function;
3263 : : }
3264 : :
3265 [ # # ]: 0 : if (i1->nNumberOfAtoms != i2->nNumberOfAtoms)
3266 : : {
3267 : 0 : ret |= IDIF_NUM_AT;
3268 : 0 : goto exit_function;
3269 : : }
3270 [ # # ]: 0 : if (i1->nNumberOfAtoms > 0)
3271 : : {
3272 [ # # ]: 0 : if (memcmp(i1->nAtom, i2->nAtom, i1->nNumberOfAtoms * sizeof(i1->nAtom[0])))
3273 : : {
3274 : 0 : ret |= IDIF_ATOMS;
3275 : 0 : goto exit_function;
3276 : : }
3277 : : /* IDIF_NON_TAUT_H, IDIF_MORE_FH, IDIF_LESS_FH */
3278 [ # # ]: 0 : if (memcmp(i1->nNum_H, i2->nNum_H, i1->nNumberOfAtoms * sizeof(i1->nNum_H[0])))
3279 : : {
3280 : 0 : ret |= IDIF_POSITION_H;
3281 [ # # ]: 0 : for (j1 = 0; j1 < i1->nNumberOfAtoms; j1++)
3282 : : {
3283 [ # # # # ]: 0 : if (i1->nNum_H[j1] != i2->nNum_H[j1] && picr->num_diff_pos_H < ICR_MAX_DIFF_FIXED_H)
3284 : : {
3285 : 0 : picr->diff_pos_H_at[picr->num_diff_pos_H] = j1;
3286 : 0 : picr->diff_pos_H_nH[picr->num_diff_pos_H] = i1->nNum_H[j1] - i2->nNum_H[j1];
3287 : 0 : picr->num_diff_pos_H++;
3288 : : }
3289 : : }
3290 : : }
3291 : : /* fixed H */
3292 [ # # # # ]: 0 : if (i1->nNum_H_fixed || i2->nNum_H_fixed)
3293 : : {
3294 : 0 : int bHasFixedH1 = 0, bHasFixedH2 = 0, i;
3295 [ # # ]: 0 : if (i1->nNum_H_fixed)
3296 : : {
3297 [ # # ]: 0 : for (i = 0; i < i1->nNumberOfAtoms; i++)
3298 : : {
3299 [ # # ]: 0 : if (i1->nNum_H_fixed[i])
3300 : : {
3301 : 0 : bHasFixedH1++;
3302 : : }
3303 : : }
3304 : : }
3305 [ # # ]: 0 : if (i2->nNum_H_fixed)
3306 : : {
3307 [ # # ]: 0 : for (i = 0; i < i2->nNumberOfAtoms; i++)
3308 : : {
3309 [ # # ]: 0 : if (i2->nNum_H_fixed[i])
3310 : : {
3311 : 0 : bHasFixedH2++;
3312 : : }
3313 : : }
3314 : : }
3315 [ # # # # ]: 0 : if (bHasFixedH1 && !bHasFixedH2)
3316 : : {
3317 [ # # ]: 0 : for (i = j = 0; i < i1->nNumberOfAtoms; i++)
3318 : : {
3319 [ # # ]: 0 : if (i1->nNum_H_fixed[i])
3320 : : {
3321 [ # # ]: 0 : if (j < ICR_MAX_DIFF_FIXED_H)
3322 : : {
3323 : 0 : picr->fixed_H_at1_more[j] = i;
3324 : 0 : picr->fixed_H_nH1_more[j] = i1->nNum_H_fixed[i];
3325 : 0 : j++;
3326 : : }
3327 : : }
3328 : : }
3329 : 0 : picr->num_fixed_H1_more = j;
3330 : 0 : ret |= IDIF_MORE_FH; /* Extra Fixed-H */
3331 : : }
3332 : : else
3333 : : {
3334 [ # # # # ]: 0 : if (!bHasFixedH1 && bHasFixedH2)
3335 : : {
3336 [ # # ]: 0 : for (i = j = 0; i < i2->nNumberOfAtoms; i++)
3337 : : {
3338 [ # # ]: 0 : if (i2->nNum_H_fixed[i])
3339 : : {
3340 [ # # ]: 0 : if (j < ICR_MAX_DIFF_FIXED_H)
3341 : : {
3342 : 0 : picr->fixed_H_at2_more[j] = i;
3343 : 0 : picr->fixed_H_nH2_more[j] = i2->nNum_H_fixed[i];
3344 : 0 : j++;
3345 : : }
3346 : : }
3347 : : }
3348 : 0 : picr->num_fixed_H2_more = j;
3349 : 0 : ret |= IDIF_LESS_FH; /* Missed Fixed-H */
3350 : : }
3351 : : else
3352 : : {
3353 [ # # # # ]: 0 : if (bHasFixedH1 && bHasFixedH2 &&
3354 [ # # ]: 0 : memcmp(i1->nNum_H_fixed, i2->nNum_H_fixed, i1->nNumberOfAtoms * sizeof(i1->nNum_H_fixed[0])))
3355 : : {
3356 [ # # ]: 0 : for (i = j1 = j2 = 0; i < i1->nNumberOfAtoms; i++)
3357 : : {
3358 [ # # ]: 0 : if (i1->nNum_H_fixed[i] > i2->nNum_H_fixed[i])
3359 : : {
3360 [ # # ]: 0 : if (j1 < ICR_MAX_DIFF_FIXED_H)
3361 : : {
3362 : 0 : picr->fixed_H_at1_more[j1] = i;
3363 : 0 : picr->fixed_H_nH1_more[j1] = i1->nNum_H_fixed[i] - i2->nNum_H_fixed[i];
3364 : 0 : j1++;
3365 : : }
3366 : : }
3367 : : else
3368 : : {
3369 [ # # ]: 0 : if (i1->nNum_H_fixed[i] < i2->nNum_H_fixed[i])
3370 : : {
3371 [ # # ]: 0 : if (j2 < ICR_MAX_DIFF_FIXED_H)
3372 : : {
3373 : 0 : picr->fixed_H_at2_more[j2] = i;
3374 : 0 : picr->fixed_H_nH2_more[j2] = i2->nNum_H_fixed[i] - i1->nNum_H_fixed[i];
3375 : 0 : j2++;
3376 : : }
3377 : : }
3378 : : }
3379 : : }
3380 [ # # # # ]: 0 : ret |= (j1 ? IDIF_MORE_FH : 0) | (j2 ? IDIF_LESS_FH : 0);
3381 : 0 : picr->num_fixed_H1_more = j1;
3382 : 0 : picr->num_fixed_H2_more = j2;
3383 : : }
3384 : : }
3385 : : }
3386 : : }
3387 : : }
3388 : :
3389 : : /* compare formulas and H */
3390 : 0 : num_H1 = 0;
3391 : 0 : num_H2 = 0;
3392 : 0 : ret2 = CompareHillFormulasNoH(i1->szHillFormula, i2->szHillFormula, &num_H1, &num_H2);
3393 : 0 : picr->tot_num_H1 = num_H1;
3394 : 0 : picr->tot_num_H2 = num_H2;
3395 [ # # ]: 0 : if (ret2)
3396 : : {
3397 : 0 : ret |= IDIF_NUM_EL;
3398 : 0 : goto exit_function;
3399 : : }
3400 [ # # ]: 0 : if (num_H1 > num_H2)
3401 : : {
3402 : 0 : ret |= IDIF_MORE_H;
3403 : : }
3404 [ # # ]: 0 : if (num_H1 < num_H2)
3405 : : {
3406 : 0 : ret |= IDIF_LESS_H;
3407 : : }
3408 [ # # ]: 0 : if (i1->lenConnTable != i2->lenConnTable)
3409 : : {
3410 : 0 : ret |= IDIF_CON_LEN;
3411 : 0 : goto exit_function;
3412 : : }
3413 : : else
3414 : : {
3415 [ # # # # ]: 0 : if (i1->lenConnTable > 0 && memcmp(i1->nConnTable, i2->nConnTable, i1->lenConnTable * sizeof(i1->nConnTable[0])))
3416 : : {
3417 : 0 : ret |= IDIF_CON_TBL;
3418 : 0 : goto exit_function;
3419 : : }
3420 : : }
3421 : :
3422 : : /* output special cases: different number of t-groups, different sizes of t-groups, different endpoints */
3423 : : /* in isotopic or deprotonated cases i1->lenTautomer == 1 && i1->nTautomer[0] = 0 */
3424 : :
3425 : : /*
3426 : : if ( i1->lenTautomer != i2->lenTautomer && (i1->lenTautomer > 1 || i2->lenTautomer > 1) ) {
3427 : : ret |= IDIF_TAUT_LEN;
3428 : : }
3429 : : */
3430 : :
3431 : : /* compare number of t-groups */
3432 [ # # ]: 0 : n1 = i1->lenTautomer ? i1->nTautomer[0] : 0;
3433 [ # # ]: 0 : n2 = i2->lenTautomer ? i2->nTautomer[0] : 0;
3434 [ # # # # ]: 0 : if (!n1 && n2)
3435 : : {
3436 : 0 : ret |= IDIF_NO_TAUT;
3437 : : }
3438 : : else
3439 : : {
3440 [ # # # # ]: 0 : if (n1 && !n2)
3441 : : {
3442 : 0 : ret |= IDIF_WRONG_TAUT;
3443 : : }
3444 : : else
3445 : : {
3446 [ # # # # ]: 0 : if (n1 == 1 && n2 > 1)
3447 : : {
3448 : 0 : ret |= IDIF_SINGLE_TG;
3449 : : }
3450 : : else
3451 : : {
3452 [ # # # # ]: 0 : if (n1 > 1 && n2 == 1)
3453 : : {
3454 : 0 : ret |= IDIF_MULTIPLE_TG;
3455 : : }
3456 : : else
3457 : : {
3458 [ # # ]: 0 : if (n1 != n2)
3459 : : {
3460 : 0 : ret |= IDIF_NUM_TG;
3461 : : }
3462 : : }
3463 : : }
3464 : : }
3465 : : }
3466 : :
3467 [ # # # # ]: 0 : if (n1 || n2)
3468 : : {
3469 : : /* number of endpoints */
3470 : 0 : int num1 = 0, num2 = 0, num_M1 = 0, num_M2 = 0;
3471 : : int len, num_eq, num_in1_only, num_in2_only;
3472 : 0 : AT_NUMB* pe1 = (AT_NUMB*)inchi_malloc(((long long)i1->lenTautomer + 1) * sizeof(pe1[0])); /* djb-rwth: cast operator added */
3473 : 0 : AT_NUMB* pe2 = (AT_NUMB*)inchi_malloc(((long long)i2->lenTautomer + 1) * sizeof(pe2[0])); /* djb-rwth: cast operator added */
3474 : 0 : num_H1 = num_H2 = 0;
3475 : : /* collect endpoints, H, (-) */
3476 [ # # # # ]: 0 : if (!pe1 || !pe2)
3477 : : {
3478 [ # # ]: 0 : if (pe1)
3479 : : {
3480 [ # # ]: 0 : inchi_free(pe1);
3481 : : }
3482 [ # # ]: 0 : if (pe2)
3483 : : {
3484 [ # # ]: 0 : inchi_free(pe2);
3485 : : }
3486 : 0 : *err = -1; /* allocation error */
3487 : 0 : goto exit_function;
3488 : : }
3489 [ # # ]: 0 : for (m = 1; m < i1->lenTautomer; m += len)
3490 : : {
3491 : 0 : len = i1->nTautomer[m++];
3492 : 0 : num_H1 += i1->nTautomer[m];
3493 : 0 : num_M1 += i1->nTautomer[m + 1];
3494 [ # # ]: 0 : for (j = 2; j < len; j++)
3495 : : {
3496 : 0 : pe1[num1++] = i1->nTautomer[m + j];
3497 : : }
3498 : : }
3499 [ # # ]: 0 : for (m = 1; m < i2->lenTautomer; m += len)
3500 : : {
3501 : 0 : len = i2->nTautomer[m++];
3502 : 0 : num_H2 += i2->nTautomer[m];
3503 : 0 : num_M2 += i2->nTautomer[m + 1];
3504 [ # # ]: 0 : for (j = 2; j < len; j++)
3505 : : {
3506 : 0 : pe2[num2++] = i2->nTautomer[m + j];
3507 : : }
3508 : : }
3509 : 0 : picr->num_taut_H1 = num_H1;
3510 : 0 : picr->num_taut_H2 = num_H2;
3511 : 0 : picr->num_taut_M1 = num_M1;
3512 : 0 : picr->num_taut_M2 = num_M2;
3513 : : /* sort endpoints */
3514 : 0 : insertions_sort_AT_RANK(pe1, num1);
3515 : 0 : insertions_sort_AT_RANK(pe2, num2);
3516 : : /* compare */
3517 : : /*
3518 : : if ( num1 < num2 ) {
3519 : : ret |= IDIF_LESS_TG_ENDP;
3520 : : } else
3521 : : if ( num1 > num2 ) {
3522 : : ret |= IDIF_MORE_TG_ENDP;
3523 : : }
3524 : : */
3525 : : /* compare all */
3526 : 0 : num_eq = num_in1_only = num_in2_only = 0;
3527 [ # # # # ]: 0 : for (j1 = j2 = 0; j1 < num1 && j2 < num2; )
3528 : : {
3529 [ # # ]: 0 : if (pe1[j1] == pe2[j2])
3530 : : {
3531 : 0 : j1++;
3532 : 0 : j2++;
3533 : 0 : num_eq++;
3534 : : }
3535 : : else
3536 : : {
3537 [ # # ]: 0 : if (pe1[j1] < pe2[j2])
3538 : : { /* BC: fixed, was pe2[j1] 2006-03-27 */
3539 [ # # ]: 0 : if (picr->num_endp_in1_only < ICR_MAX_ENDP_IN1_ONLY)
3540 : : {
3541 : 0 : picr->endp_in1_only[picr->num_endp_in1_only++] = pe1[j1];
3542 : : }
3543 : 0 : j1++;
3544 : 0 : num_in1_only++;
3545 : : }
3546 : : else
3547 : : {
3548 [ # # ]: 0 : if (picr->num_endp_in2_only < ICR_MAX_ENDP_IN2_ONLY)
3549 : : {
3550 : 0 : picr->endp_in2_only[picr->num_endp_in2_only++] = pe2[j2];
3551 : : }
3552 : 0 : j2++;
3553 : 0 : num_in2_only++;
3554 : : }
3555 : : }
3556 : : }
3557 [ # # ]: 0 : while (j1 < num1)
3558 : : {
3559 [ # # ]: 0 : if (picr->num_endp_in1_only < ICR_MAX_ENDP_IN1_ONLY)
3560 : : {
3561 : 0 : picr->endp_in1_only[picr->num_endp_in1_only++] = pe1[j1];
3562 : : }
3563 : 0 : j1++;
3564 : 0 : num_in1_only++;
3565 : : }
3566 [ # # ]: 0 : while (j2 < num2)
3567 : : {
3568 [ # # ]: 0 : if (picr->num_endp_in2_only < ICR_MAX_ENDP_IN2_ONLY)
3569 : : {
3570 : 0 : picr->endp_in2_only[picr->num_endp_in2_only++] = pe2[j2];
3571 : : }
3572 : 0 : j2++;
3573 : 0 : num_in2_only++;
3574 : : }
3575 [ # # ]: 0 : if (num_in1_only)
3576 : : {
3577 : 0 : ret |= IDIF_EXTRA_TG_ENDP;
3578 : : }
3579 [ # # ]: 0 : if (num_in2_only)
3580 : : {
3581 : 0 : ret |= IDIF_MISS_TG_ENDP;
3582 : : }
3583 [ # # # # : 0 : if (!num_in1_only && !num_in2_only && num_eq)
# # ]
3584 : : {
3585 : : ; /* same t-groups endpoints */
3586 : : }
3587 : : else
3588 : : {
3589 : 0 : ret |= IDIF_DIFF_TG_ENDP;
3590 : : }
3591 [ # # ]: 0 : inchi_free(pe1);
3592 [ # # ]: 0 : inchi_free(pe2);
3593 : : }
3594 : :
3595 [ # # # # ]: 0 : if ((i1->lenTautomer > 1 && i2->lenTautomer > 1) &&
3596 [ # # ]: 0 : (i1->lenTautomer != i2->lenTautomer ||
3597 [ # # ]: 0 : memcmp(i1->nTautomer, i2->nTautomer, i1->lenTautomer * sizeof(i1->nTautomer[0]))))
3598 : : {
3599 : 0 : ret |= IDIF_TG;
3600 : : }
3601 : :
3602 [ # # ]: 0 : if (i1->nNumberOfIsotopicAtoms != i2->nNumberOfIsotopicAtoms)
3603 : : {
3604 : 0 : ret |= IDIF_NUM_ISO_AT;
3605 : : }
3606 : : else
3607 : : {
3608 [ # # # # ]: 0 : if (i1->nNumberOfIsotopicAtoms > 0 && memcmp(i1->IsotopicAtom, i2->IsotopicAtom, i1->nNumberOfIsotopicAtoms * sizeof(i1->IsotopicAtom[0])))
3609 : : {
3610 : 0 : ret |= IDIF_ISO_AT;
3611 : : }
3612 : : }
3613 [ # # ]: 0 : if (i1->nTotalCharge != i2->nTotalCharge)
3614 : : {
3615 : 0 : ret |= IDIF_CHARGE;
3616 : : }
3617 [ # # # # : 0 : if (a1 && a1->nNumRemovedProtons && (!a2 || a2->nNumRemovedProtons != a1->nNumRemovedProtons))
# # # # ]
3618 : : {
3619 : 0 : ret |= IDIF_REM_PROT;
3620 : : }
3621 [ # # # # ]: 0 : if (a1 && (!a2 ||
3622 [ # # ]: 0 : a2->nNumRemovedIsotopicH[0] != a1->nNumRemovedIsotopicH[0] ||
3623 [ # # ]: 0 : a2->nNumRemovedIsotopicH[1] != a1->nNumRemovedIsotopicH[1] ||
3624 [ # # ]: 0 : a2->nNumRemovedIsotopicH[2] != a1->nNumRemovedIsotopicH[2]))
3625 : : {
3626 : 0 : ret |= IDIF_REM_ISO_H;
3627 : : }
3628 : :
3629 : : /*
3630 : : if ( i1->nPossibleLocationsOfIsotopicH && i2->nPossibleLocationsOfIsotopicH ) {
3631 : : if ( i1->nPossibleLocationsOfIsotopicH[0] != i2->nPossibleLocationsOfIsotopicH[0] ||
3632 : : memcmp(i1->nPossibleLocationsOfIsotopicH, i2->nPossibleLocationsOfIsotopicH,
3633 : : sizeof(i1->nPossibleLocationsOfIsotopicH[0])*i1->nPossibleLocationsOfIsotopicH[0]) )
3634 : : return 18;
3635 : : } else
3636 : : if ( !i1->nPossibleLocationsOfIsotopicH != !i2->nPossibleLocationsOfIsotopicH ) {
3637 : : return 19;
3638 : : }
3639 : : */
3640 : :
3641 [ # # ]: 0 : if (i1->StereoIsotopic &&
3642 [ # # ]: 0 : i1->StereoIsotopic->nNumberOfStereoBonds + i1->StereoIsotopic->nNumberOfStereoCenters)
3643 : : {
3644 : 0 : Stereo1 = i1->StereoIsotopic;
3645 : : }
3646 : : else
3647 : : {
3648 : 0 : Stereo1 = i1->Stereo;
3649 : : }
3650 [ # # ]: 0 : if (i2->StereoIsotopic &&
3651 [ # # ]: 0 : i2->StereoIsotopic->nNumberOfStereoBonds + i2->StereoIsotopic->nNumberOfStereoCenters)
3652 : : {
3653 : 0 : Stereo2 = i2->StereoIsotopic;
3654 : : }
3655 : : else
3656 : : {
3657 : 0 : Stereo2 = i2->Stereo;
3658 : : }
3659 : 0 : ret |= CompareReversedStereoINChI2(Stereo1, Stereo2, picr);
3660 : :
3661 : 0 : exit_function:
3662 : :
3663 : 0 : picr->flags = ret;
3664 : :
3665 : 0 : return ret;
3666 : : }
3667 : : #endif /* } READ_INCHI_STRING */
3668 : :
3669 : :
3670 : : /****************************************************************************/
3671 : 161 : int Create_INChI(CANON_GLOBALS* pCG,
3672 : : INCHI_CLOCK* ic,
3673 : : INPUT_PARMS* ip,
3674 : : INChI** ppINChI,
3675 : : INChI_Aux** ppINChI_Aux,
3676 : : ORIG_ATOM_DATA* orig_inp_data,
3677 : : inp_ATOM* inp_at,
3678 : : INP_ATOM_DATA* out_norm_data[2],
3679 : : int num_inp_at,
3680 : : const unsigned char* keep_explicit_HDT,
3681 : : INCHI_MODE nUserMode,
3682 : : INCHI_MODE* pbTautFlags,
3683 : : INCHI_MODE* pbTautFlagsDone,
3684 : : struct tagInchiTime* ulMaxTime,
3685 : : T_GROUP_INFO* ti_out,
3686 : : char* pStrErrStruct)
3687 : : {
3688 : : /*
3689 : : #define NON_TAUT 0
3690 : : #define TAUT 1
3691 : : */
3692 : 161 : int nebend = 0, * ebend = NULL;
3693 : :
3694 : : sp_ATOM* at[TAUT_NUM]; /* at[0]=>non-tautomeric, at[1]=>tautomeric */
3695 : 161 : int i, n1, n2, num_atoms, num_at_tg, num_removed_H, num_removed_H_taut = 0, ret = 0, ret2 = 0;
3696 : 161 : INCHI_MODE nMode = 0;
3697 : : T_GROUP_INFO vt_group_info;
3698 : : T_GROUP_INFO vt_group_info_orig;
3699 : 161 : T_GROUP_INFO* /*const*/ t_group_info = &vt_group_info;
3700 : 161 : T_GROUP_INFO* /*const*/ t_group_info_orig = &vt_group_info_orig;
3701 : :
3702 : : CANON_STAT CS, CS2;
3703 : 161 : CANON_STAT* pCS = &CS;
3704 : 161 : CANON_STAT* pCS2 = &CS2; /* save all allocations to avoid memory leaks in case Canon_INChI() removes the pointer */
3705 : :
3706 : : ATOM_SIZES s[TAUT_NUM];
3707 : :
3708 : : BCN Bcn;
3709 : 161 : BCN* pBCN = &Bcn;
3710 : :
3711 : 161 : int bHasIsotopicAtoms = 0;
3712 : 161 : int bMayHaveStereo = 0;
3713 : 161 : int num_taut_at = 0;
3714 : :
3715 : 161 : inp_ATOM* out_at = NULL; /*, *norm_at_fixed_bonds[TAUT_NUM]; */ /* = {out_norm_nontaut_at, out_norm_taut_at} ; */
3716 : 161 : INChI* pINChI = NULL; /* added initialization 2006-03 */
3717 : 161 : INChI_Aux* pINChI_Aux = NULL; /* added initialization 2006-03 */
3718 : 161 : int bPointedEdgeStereo = ((TG_FLAG_POINTED_EDGE_STEREO & *pbTautFlags) ? PES_BIT_POINT_EDGE_STEREO : 0)
3719 : 161 : | ((TG_FLAG_PHOSPHINE_STEREO & *pbTautFlags) ? PES_BIT_PHOSPHINE_STEREO : 0)
3720 : 161 : | ((TG_FLAG_ARSINE_STEREO & *pbTautFlags) ? PES_BIT_ARSINE_STEREO : 0)
3721 : 161 : | ((TG_FLAG_FIX_SP3_BUG & *pbTautFlags) ? PES_BIT_FIX_SP3_BUG : 0);
3722 : 161 : INCHI_MODE bTautFlags = (*pbTautFlags & (~(INCHI_MODE)TG_FLAG_ALL_TAUTOMERIC));
3723 : 161 : INCHI_MODE bTautFlagsDone = (*pbTautFlagsDone /*& (~(INCHI_MODE)TG_FLAG_ALL_TAUTOMERIC) */);
3724 : : #if ( bRELEASE_VERSION == 0 )
3725 : : int bExtract = 0; /* EXTR_HAS_ATOM_WITH_DEFINED_PARITY; */
3726 : : #endif
3727 : :
3728 : : #ifdef GHI100_FIX
3729 : : #if ((SPRINTF_FLAG != 1) && (SPRINTF_FLAG != 2))
3730 : : setlocale(LC_ALL, "en-US"); /* djb-rwth: setting all locales to "en-US" */
3731 : : #endif
3732 : : #endif
3733 : :
3734 : : /* */
3735 : 161 : int bFixIsoFixedH = 0;
3736 : 161 : int bFixTermHChrg = 0;
3737 : :
3738 : 161 : int LargeMolecules = ip->bLargeMolecules;
3739 : : /* djb-rwth: removing redundant variables */
3740 : :
3741 : : /* vABParityUnknown holds actual value of an internal constant signifying
3742 : : unknown parity: either the same as for undefined parity (default==standard)
3743 : : or a specific one (non-std; requested by SLUUD switch). */
3744 : 161 : int vABParityUnknown = AB_PARITY_UNDF;
3745 [ - + ]: 161 : if (0 != (nUserMode & REQ_MODE_DIFF_UU_STEREO))
3746 : : {
3747 : : /* Make labels for unknown and undefined stereo different */
3748 : 0 : vABParityUnknown = AB_PARITY_UNKN;
3749 : : }
3750 : :
3751 : : /* djb-rwth: removing redundant code */
3752 : :
3753 : : #if ( FIX_ISO_FIXEDH_BUG == 1 )
3754 [ + - ]: 161 : if (TG_FLAG_FIX_ISO_FIXEDH_BUG & *pbTautFlags)
3755 : 161 : bFixIsoFixedH = 1;
3756 : : #endif
3757 : : #if ( FIX_TERM_H_CHRG_BUG == 1 )
3758 [ + - ]: 161 : if (TG_FLAG_FIX_TERM_H_CHRG_BUG & *pbTautFlags)
3759 : 161 : bFixTermHChrg = 1;
3760 : : #endif
3761 : :
3762 : : #ifdef FIX_SRU_CYCLIZING_PS_BONDS_IN_BNS
3763 : : /* Polymer related */
3764 : : if (orig_inp_data && orig_inp_data->polymer && orig_inp_data->polymer->n > 0 && orig_inp_data->polymer->valid)
3765 : : {
3766 : : int j, jj;
3767 : : nebend = 0;
3768 : : for (j = 0; j < orig_inp_data->polymer->n; j++)
3769 : : if (orig_inp_data->polymer->units[j]->cyclized == 1)
3770 : : nebend++;
3771 : : if (nebend)
3772 : : {
3773 : : nebend *= 2;
3774 : : ebend = inchi_calloc(2 * nebend, sizeof(int));
3775 : : if (!ebend)
3776 : : {
3777 : : ret = CT_OUT_OF_RAM; goto exit_function;
3778 : : }
3779 : : jj = 0;
3780 : : for (j = 0; j < orig_inp_data->polymer->n; j++)
3781 : : {
3782 : : if (orig_inp_data->polymer->units[j]->cyclized == 1)
3783 : : {
3784 : : ebend[jj] = orig_inp_data->polymer->units[j]->end_atom1;
3785 : : ebend[jj + 1] = orig_inp_data->polymer->units[j]->end_atom2;
3786 : : jj += 2;
3787 : : }
3788 : : }
3789 : : }
3790 : : }
3791 : : #endif
3792 : :
3793 : 161 : memset(s, 0, sizeof(s)); /* djb-rwth: memset_s C11/Annex K variant? */
3794 [ + - ]: 161 : if (pBCN)
3795 : : {
3796 : 161 : memset(pBCN, 0, sizeof(pBCN[0])); /* djb-rwth: memset_s C11/Annex K variant? */
3797 : : }
3798 : 161 : memset(t_group_info, 0, sizeof(*t_group_info)); /* djb-rwth: memset_s C11/Annex K variant? */
3799 : 161 : memset(t_group_info_orig, 0, sizeof(*t_group_info_orig)); /* djb-rwth: memset_s C11/Annex K variant? */
3800 : : /*norm_at[TAUT_NON] = out_norm_data[TAUT_NON]->at; *//* output normalized non-tautomeric component */
3801 : : /*norm_at[TAUT_YES] = out_norm_data[TAUT_YES]->at; *//* output normalized tautomeric component */
3802 : : /*norm_at_fixed_bonds[TAUT_NON] = NULL;*/
3803 : : /*norm_at_fixed_bonds[TAUT_YES] = out_norm_data[TAUT_YES]->at_fixed_bonds;*/
3804 [ + + ]: 483 : for (i = 0; i < TAUT_NUM; i++)
3805 : : {
3806 [ + + ]: 322 : if (out_norm_data[i]->at)
3807 : : {
3808 [ - + ]: 161 : if (!(at[i] = (sp_ATOM*)inchi_malloc(num_inp_at * sizeof(*at[0]))))
3809 : : {
3810 : 0 : ret = -1;
3811 : : }
3812 : : }
3813 : : else
3814 : : {
3815 : 161 : at[i] = NULL;
3816 : : }
3817 : : }
3818 : :
3819 [ + - + - ]: 161 : if ((!out_norm_data[TAUT_NON]->at && !out_norm_data[TAUT_YES]->at)
3820 [ + - - + ]: 161 : || !inp_at || ret) /* djb-rwth: addressing LLVM warning */
3821 : : {
3822 : 0 : ret = -1;
3823 : 0 : goto exit_function;
3824 : : }
3825 : :
3826 : : /* the first struct to process: tautomeric if exists else non-tautomeric */
3827 [ + - ]: 161 : out_at = out_norm_data[TAUT_YES]->at ? out_norm_data[TAUT_YES]->at : out_norm_data[TAUT_NON]->at;
3828 : : /* copy the input structure to be normalized to the buffer for the normalization data */
3829 : 161 : memcpy(out_at, inp_at, num_inp_at * sizeof(out_at[0]));
3830 : : /* tautomeric groups setting */
3831 : 161 : t_group_info->bIgnoreIsotopic = 0; /* include tautomeric group isotopic info in MarkTautomerGroups() */
3832 : 161 : t_group_info->bTautFlags = *pbTautFlags;
3833 : 161 : t_group_info->bTautFlagsDone = *pbTautFlagsDone;
3834 : 161 : t_group_info->t_group = NULL; /* djb-rwth: fixing oss-fuzz issue #70475 */
3835 : :
3836 : : /*
3837 : : Preprocess the structure
3838 : : (here THE NUMBER OF ATOMS MAY BE REDUCED)
3839 : : */
3840 : :
3841 : : /* ??? Ambiguity: H-D may become HD or DH
3842 : : (that is, H+implicit D or D+implicit H) */
3843 [ - + ]: 161 : if (TG_FLAG_H_ALREADY_REMOVED & bTautFlags)
3844 : : {
3845 [ # # ]: 0 : INP_ATOM_DATA* out_norm_data1 = out_norm_data[TAUT_YES]->at ? out_norm_data[TAUT_YES] :
3846 [ # # ]: 0 : out_norm_data[TAUT_NON]->at ? out_norm_data[TAUT_NON] : NULL;
3847 [ # # ]: 0 : if (out_norm_data1)
3848 : : {
3849 : 0 : num_at_tg =
3850 : 0 : num_atoms = out_norm_data1->num_at - out_norm_data1->num_removed_H;
3851 : 0 : num_removed_H = out_norm_data1->num_removed_H;
3852 : 0 : t_group_info->tni.nNumRemovedExplicitH = num_removed_H;
3853 : : }
3854 : : else
3855 : : {
3856 : 0 : ret = -1;
3857 : 0 : goto exit_function;
3858 : : }
3859 : : }
3860 : : else
3861 : : {
3862 : 161 : num_at_tg = num_atoms = remove_terminal_HDT(num_inp_at, out_at, bFixTermHChrg, keep_explicit_HDT);
3863 : 161 : num_removed_H = num_inp_at - num_atoms;
3864 : 161 : t_group_info->tni.nNumRemovedExplicitH = num_removed_H;
3865 : 161 : add_DT_to_num_H(num_atoms, out_at);
3866 : : }
3867 : : /*fix_odd_things( num_atoms, out_at );*/
3868 : : #if ( FIND_RING_SYSTEMS == 1 )
3869 : 161 : MarkRingSystemsInp(out_at, num_atoms, 0);
3870 : : #endif
3871 : : /* duplicate the preprocessed structure so that all supplied out_norm_data[]->at buffers are filled */
3872 [ - + - - ]: 161 : if (out_at != out_norm_data[TAUT_YES]->at && out_norm_data[TAUT_YES]->at)
3873 : : {
3874 : 0 : memcpy(out_norm_data[TAUT_YES]->at, out_at, num_inp_at * sizeof(out_at[0]));
3875 : : }
3876 [ + - + - ]: 161 : if (out_norm_data[TAUT_YES]->at_fixed_bonds && out_norm_data[TAUT_YES]->at)
3877 : : {
3878 : 161 : memcpy(out_norm_data[TAUT_YES]->at_fixed_bonds, out_at, num_inp_at * sizeof(out_at[0]));
3879 : : }
3880 [ + - - + ]: 161 : if (out_at != out_norm_data[TAUT_NON]->at && out_norm_data[TAUT_NON]->at)
3881 : : {
3882 : 0 : memcpy(out_norm_data[TAUT_NON]->at, out_at, num_inp_at * sizeof(out_at[0]));
3883 : : }
3884 : :
3885 : : /*
3886 : : ??? not true ??? duplicate inp_at and keep inp_at[] unchanged after terminal hydrogens removal
3887 : : set stereo parities in taut_at[], non_taut_at[]
3888 : : obtain max. lenghts of the name stereo parts
3889 : : Ignore absence/presence of isotopic stereo for now
3890 : : mark isotopic atoms
3891 : : */
3892 [ + - + - ]: 161 : if (out_norm_data[TAUT_YES]->at && at[TAUT_YES])
3893 : : {
3894 : : /* final normalization of possibly tautomeric structure */
3895 : 161 : ret = mark_alt_bonds_and_taut_groups(ic, pCG,
3896 : 161 : out_norm_data[TAUT_YES]->at,
3897 : 161 : out_norm_data[TAUT_YES]->at_fixed_bonds,
3898 : : num_atoms, ulMaxTime, t_group_info,
3899 : : NULL, NULL,
3900 : : nebend, ebend);
3901 [ - + ]: 161 : if (ret < 0)
3902 : : {
3903 : 0 : goto exit_function;/* out of RAM or other normalization problem */
3904 : : }
3905 : 161 : num_taut_at = ret; /* number of atoms without removed H? */
3906 : 161 : num_removed_H_taut = t_group_info->tni.nNumRemovedExplicitH;
3907 : 161 : out_norm_data[TAUT_YES]->num_at = num_atoms + num_removed_H_taut; /* protons might have been removed */
3908 : 161 : out_norm_data[TAUT_YES]->num_removed_H = num_removed_H_taut;
3909 : 161 : out_norm_data[TAUT_YES]->nNumRemovedProtons += t_group_info->tni.nNumRemovedProtons;
3910 [ + + ]: 644 : for (i = 0; i < NUM_H_ISOTOPES; i++)
3911 : : {
3912 : 483 : out_norm_data[TAUT_YES]->nNumRemovedProtonsIsotopic[i] +=
3913 : 483 : t_group_info->tni.nNumRemovedProtonsIsotopic[i] /*+ t_group_info->num_iso_H[i]*/;
3914 : 483 : out_norm_data[TAUT_YES]->num_iso_H[i] +=
3915 : 483 : t_group_info->num_iso_H[i];
3916 : : }
3917 : : /* mark deleted isolated tautomeric H(+) */
3918 [ + + ]: 161 : if (num_taut_at == 1 &&
3919 [ - + - - ]: 84 : out_norm_data[TAUT_YES]->at[0].at_type == ATT_PROTON &&
3920 [ # # ]: 0 : t_group_info && t_group_info->tni.nNumRemovedProtons == 1)
3921 : : {
3922 : 0 : out_norm_data[TAUT_YES]->bDeleted = 1;
3923 : 0 : FreeInpAtom(&out_norm_data[TAUT_YES]->at_fixed_bonds);
3924 : : }
3925 : : else
3926 : : {
3927 [ + + ]: 161 : if ((t_group_info->tni.bNormalizationFlags & FLAG_NORM_CONSIDER_TAUT) &&
3928 [ + - ]: 14 : out_norm_data[TAUT_YES]->at_fixed_bonds)
3929 : : {
3930 : 14 : out_norm_data[TAUT_YES]->bTautPreprocessed = 1;
3931 : : }
3932 : : }
3933 : :
3934 : : /*
3935 : : if ( !(t_group_info->tni.bNormalizationFlags & (FLAG_NORM_CONSIDER_TAUT & ~FLAG_PROTON_SINGLE_REMOVED)) &&
3936 : : out_norm_data[TAUT_YES]->at_fixed_bonds) {
3937 : : FreeInpAtom( &out_norm_data[TAUT_YES]->at_fixed_bonds );
3938 : : }
3939 : : */
3940 : :
3941 : : /*out_norm_data[TAUT_YES]->num_removed_H = num_removed_H_taut;*/
3942 : :
3943 : 161 : out_norm_data[TAUT_YES]->bTautFlags = *pbTautFlags = t_group_info->bTautFlags;
3944 : 161 : out_norm_data[TAUT_YES]->bTautFlagsDone = *pbTautFlagsDone = t_group_info->bTautFlagsDone;
3945 : 161 : out_norm_data[TAUT_YES]->bNormalizationFlags = t_group_info->tni.bNormalizationFlags;
3946 : :
3947 : : /* create internal sp_ATOM at[] out of out_norm_data[]->at */
3948 : 161 : inp2spATOM(out_norm_data[TAUT_YES]->at, num_inp_at, at[TAUT_YES]);
3949 : :
3950 : : /* set stereo parities to at[]; nUserMode: accept alt. stereo bonds, min ring size */
3951 : 161 : ret = set_stereo_parity(pCG, out_norm_data[TAUT_YES]->at, at[TAUT_YES],
3952 : : num_taut_at, num_removed_H_taut,
3953 : : &s[TAUT_YES].nMaxNumStereoAtoms,
3954 : : &s[TAUT_YES].nMaxNumStereoBonds,
3955 : : nUserMode, bPointedEdgeStereo,
3956 : : vABParityUnknown, ip->bLooseTSACheck, ip->bStereoAtZz);
3957 : :
3958 : : #if ( bRELEASE_VERSION == 0 )
3959 : : if (0 < ret)
3960 : : {
3961 : : bExtract |= EXTR_HAS_ATOM_WITH_DEFINED_PARITY;
3962 : : }
3963 : : if (t_group_info->tni.bNormalizationFlags & FLAG_NORM_CONSIDER_TAUT)
3964 : : {
3965 : : bExtract |= EXTR_TAUT_TREATMENT_CHARGES;
3966 : : }
3967 : : #endif
3968 : :
3969 [ + - - + ]: 161 : if (RETURNED_ERROR(ret))
3970 : : {
3971 : 0 : goto exit_function; /* stereo bond error */
3972 : : }
3973 : :
3974 : 161 : s[TAUT_YES].bMayHaveStereo =
3975 [ + + ]: 256 : (s[TAUT_YES].nMaxNumStereoAtoms ||
3976 [ + + ]: 95 : s[TAUT_YES].nMaxNumStereoBonds);
3977 : :
3978 : : /*
3979 : : mark isotopic atoms and atoms that have non-tautomeric
3980 : : isotopic terminal hydrogen atoms 1H, 2H(D), 3H(T)
3981 : : */
3982 : :
3983 : 161 : s[TAUT_YES].num_isotopic_atoms =
3984 : 161 : set_atom_iso_sort_keys(num_taut_at,
3985 : : at[TAUT_YES],
3986 : : t_group_info,
3987 : : &s[TAUT_YES].bHasIsotopicTautGroups);
3988 : :
3989 : : /*
3990 : : Prepare tautomeric (if no tautomerism found then prepare non-tautomeric)
3991 : : structure for canonicalizaton:
3992 : :
3993 : : remove t-groups that have no H,
3994 : : remove charges from t-groups if requested
3995 : : renumber t-groups and find final t_group_info->num_t_groups
3996 : : add to t-groups lists of endpoints tgroup->nEndpointAtomNumber[]
3997 : : calculate length of the t-group part of the connection table
3998 : : */
3999 : :
4000 : 161 : s[TAUT_YES].nLenLinearCTTautomer = CountTautomerGroups(at[TAUT_YES], num_taut_at, t_group_info);
4001 : :
4002 [ + - - + ]: 161 : if (RETURNED_ERROR(s[TAUT_YES].nLenLinearCTTautomer))
4003 : : {
4004 : : /* added error treatment 9-11-2003 */
4005 : 0 : ret = s[TAUT_YES].nLenLinearCTTautomer;
4006 : 0 : goto exit_function;
4007 : : /* error has happened; no breakpoint here
4008 : : s[TAUT_YES].nLenLinearCTTautomer = 0;
4009 : : */
4010 : : }
4011 : :
4012 [ + + ]: 161 : else if (s[TAUT_YES].nLenLinearCTTautomer > 0)
4013 : : {
4014 : 14 : num_at_tg = num_taut_at + t_group_info->num_t_groups;
4015 : : /* ??? -not true- create t_group_info_orig for multiple calls with atom renumbering */
4016 : 14 : make_a_copy_of_t_group_info(t_group_info_orig /* dest*/, t_group_info /* source*/); /* djb-rwth: addressing coverity ID #499544 -- properly used sequence of arguments according to the comment in previous line */
4017 : : /* mark isotopic tautomer groups: calculate t_group->iWeight */
4018 : 14 : s[TAUT_YES].nLenLinearCTIsotopicTautomer = set_tautomer_iso_sort_keys(t_group_info);
4019 [ - + ]: 14 : if (s[TAUT_YES].nLenLinearCTIsotopicTautomer < 0)
4020 : : {
4021 : : /* ??? -error cannot happen- error has happened; no breakpoint here */
4022 : 0 : s[TAUT_YES].nLenLinearCTIsotopicTautomer = 0;
4023 : : }
4024 : 14 : out_norm_data[TAUT_YES]->bTautomeric = s[TAUT_YES].nLenLinearCTTautomer;
4025 : : }
4026 : :
4027 : : /* new variable: s[TAUT_YES].nLenCT introduced 7-22-2002 */
4028 : 161 : GetCanonLengths(num_taut_at, at[TAUT_YES], &s[TAUT_YES], t_group_info);
4029 : : }
4030 : :
4031 [ - + ]: 161 : if (out_norm_data[TAUT_NON]->at &&
4032 [ # # ]: 0 : out_norm_data[TAUT_YES]->at &&
4033 [ # # ]: 0 : at[TAUT_NON] &&
4034 [ # # ]: 0 : !s[TAUT_YES].nLenLinearCTTautomer)
4035 : : {
4036 : : /* the structure is non-tautomeric: use tautomeric treatment results only for it */
4037 [ # # ]: 0 : inchi_free(at[TAUT_NON]);
4038 : 0 : at[TAUT_NON] = NULL;
4039 : : }
4040 : :
4041 [ + - ]: 161 : else if (!out_norm_data[TAUT_NON]->at &&
4042 [ + - ]: 161 : out_norm_data[TAUT_YES]->at &&
4043 [ + - ]: 161 : !at[TAUT_NON] &&
4044 [ + - ]: 161 : at[TAUT_YES] &&
4045 [ + + ]: 161 : !s[TAUT_YES].nLenLinearCTTautomer)
4046 : : {
4047 : : /* requested tautomeric; found non-tautomeric; it is located in out_norm_data[TAUT_YES]->at */
4048 : 147 : out_norm_data[TAUT_YES]->bTautomeric = 0;
4049 : : }
4050 : :
4051 [ - + - - ]: 14 : else if (out_norm_data[TAUT_NON]->at && at[TAUT_NON])
4052 : : {
4053 : : /* the structure needs non-tautomeric treatment:
4054 : : final normalization of non-tautomeric structure */
4055 : 0 : ret = mark_alt_bonds_and_taut_groups(ic, pCG,
4056 : 0 : out_norm_data[TAUT_NON]->at,
4057 : : NULL,
4058 : : num_atoms,
4059 : : ulMaxTime,
4060 : : NULL,
4061 : : &bTautFlags,
4062 : : &bTautFlagsDone,
4063 : : nebend, ebend);
4064 [ # # ]: 0 : if (ret < 0)
4065 : : {
4066 : 0 : goto exit_function; /* out of RAM or other normalization problem */
4067 : : }
4068 : 0 : out_norm_data[TAUT_NON]->num_at = num_atoms + num_removed_H;
4069 : 0 : out_norm_data[TAUT_NON]->num_removed_H = num_removed_H;
4070 : 0 : out_norm_data[TAUT_NON]->bTautFlags = *pbTautFlags;
4071 : 0 : out_norm_data[TAUT_NON]->bTautFlagsDone = *pbTautFlagsDone;
4072 : 0 : out_norm_data[TAUT_NON]->bNormalizationFlags = 0;
4073 : :
4074 : : /* create internal sp_ATOM at[] out of out_norm_data[]->at */
4075 : 0 : inp2spATOM(out_norm_data[TAUT_NON]->at, num_inp_at, at[TAUT_NON]);
4076 : :
4077 : : /* set stereo parities to at[]; nUserMode: accept alt. stereo bonds, min ring size */
4078 : 0 : ret = set_stereo_parity(pCG, out_norm_data[TAUT_NON]->at,
4079 : : at[TAUT_NON], num_atoms, num_removed_H,
4080 : : &s[TAUT_NON].nMaxNumStereoAtoms,
4081 : : &s[TAUT_NON].nMaxNumStereoBonds, nUserMode,
4082 : : bPointedEdgeStereo, vABParityUnknown,
4083 : : ip->bLooseTSACheck, ip->bStereoAtZz);
4084 : : #if ( bRELEASE_VERSION == 0 )
4085 : : if (0 < ret)
4086 : : {
4087 : : bExtract |= EXTR_HAS_ATOM_WITH_DEFINED_PARITY;
4088 : : }
4089 : : #endif
4090 [ # # # # ]: 0 : if (RETURNED_ERROR(ret))
4091 : : {
4092 : 0 : goto exit_function; /* stereo bond error */
4093 : : }
4094 [ # # ]: 0 : s[TAUT_NON].bMayHaveStereo = (s[TAUT_NON].nMaxNumStereoAtoms ||
4095 [ # # ]: 0 : s[TAUT_NON].nMaxNumStereoBonds);
4096 : :
4097 : : /*
4098 : : * mark isotopic atoms and atoms that have non-tautomeric
4099 : : * isotopic terminal hydrogen atoms 1H, 2H(D), 3H(T)
4100 : : */
4101 : 0 : s[TAUT_NON].num_isotopic_atoms = set_atom_iso_sort_keys(num_atoms, at[TAUT_NON], NULL, NULL);
4102 : 0 : GetCanonLengths(num_atoms, at[TAUT_NON], &s[TAUT_NON], NULL);
4103 : 0 : out_norm_data[TAUT_NON]->bTautomeric = 0;
4104 : : }
4105 : :
4106 : : /* common */
4107 [ + + - + ]: 161 : bMayHaveStereo = s[TAUT_YES].bMayHaveStereo || s[TAUT_NON].bMayHaveStereo;
4108 [ + - ]: 161 : bHasIsotopicAtoms = s[TAUT_NON].num_isotopic_atoms > 0 || s[TAUT_NON].bHasIsotopicTautGroups > 0 ||
4109 [ + - + - : 322 : s[TAUT_YES].num_isotopic_atoms > 0 || s[TAUT_YES].bHasIsotopicTautGroups > 0;
- + ]
4110 [ + - ]: 161 : if (bFixIsoFixedH)
4111 : : {
4112 : : /* 2008-03-21 DT */
4113 : 161 : bHasIsotopicAtoms = bHasIsotopicAtoms
4114 [ + - ]: 322 : ||
4115 [ + + + - ]: 161 : (s[TAUT_YES].nLenLinearCTTautomer > 0 && t_group_info &&
4116 [ + - ]: 14 : ((0 < NUM_H_ISOTOPES && t_group_info->tni.nNumRemovedProtonsIsotopic[0]) ||
4117 [ + - ]: 14 : (1 < NUM_H_ISOTOPES && t_group_info->tni.nNumRemovedProtonsIsotopic[1]) ||
4118 [ - + ]: 14 : (2 < NUM_H_ISOTOPES && t_group_info->tni.nNumRemovedProtonsIsotopic[2])
4119 : : )); /* djb-rwth: addressing LLVM warning */
4120 : : }
4121 : 161 : bHasIsotopicAtoms = bHasIsotopicAtoms
4122 [ + - ]: 322 : ||
4123 [ - + - - ]: 161 : (s[TAUT_YES].nLenIsotopicEndpoints > 1 && t_group_info &&
4124 [ # # ]: 0 : (t_group_info->bTautFlagsDone & (TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE))); /* djb-rwth: addressing LLVM warning */
4125 : :
4126 : : /* default mode */
4127 [ - + ]: 161 : if (!(nUserMode & REQ_MODE_DEFAULT))
4128 : : {
4129 : : /* default */
4130 : 0 : nUserMode |= REQ_MODE_DEFAULT;
4131 : : }
4132 : :
4133 : : /* adjust the mode to the reality */
4134 [ + - + - ]: 161 : if ((nUserMode & REQ_MODE_ISO) && !bHasIsotopicAtoms)
4135 : : {
4136 : 161 : nUserMode ^= REQ_MODE_ISO;
4137 : 161 : nUserMode |= REQ_MODE_NON_ISO; /* at least one is needed */
4138 : : }
4139 [ + - - + ]: 161 : if ((nUserMode & REQ_MODE_STEREO) && (nUserMode & REQ_MODE_ISO))
4140 : : {
4141 : 0 : nUserMode |= REQ_MODE_ISO_STEREO;
4142 : : }
4143 [ + - - + ]: 161 : if ((nUserMode & REQ_MODE_STEREO) && !(nUserMode & REQ_MODE_NON_ISO))
4144 : : {
4145 : 0 : nUserMode ^= REQ_MODE_STEREO;
4146 : : }
4147 [ + + ]: 161 : if (!bMayHaveStereo)
4148 : : {
4149 [ + - ]: 91 : if (nUserMode & REQ_MODE_STEREO)
4150 : 91 : nUserMode ^= REQ_MODE_STEREO;
4151 [ - + ]: 91 : if (nUserMode & REQ_MODE_ISO_STEREO)
4152 : 0 : nUserMode ^= REQ_MODE_ISO_STEREO;
4153 : : }
4154 : :
4155 [ - + ]: 161 : if ((nUserMode & REQ_MODE_BASIC) &&
4156 [ # # # # ]: 0 : (!out_norm_data[TAUT_NON]->at || !ppINChI[TAUT_NON] ||
4157 [ # # # # ]: 0 : !ppINChI_Aux[TAUT_NON] || !at[TAUT_NON]))
4158 : : {
4159 : 0 : nUserMode ^= REQ_MODE_BASIC;
4160 : : }
4161 [ + - ]: 161 : if ((nUserMode & REQ_MODE_TAUT) &&
4162 [ + - + - ]: 161 : (!out_norm_data[TAUT_YES]->at || !ppINChI[TAUT_YES] ||
4163 [ + - - + ]: 161 : !ppINChI_Aux[TAUT_YES] || !at[TAUT_YES]))
4164 : : {
4165 : 0 : nUserMode ^= REQ_MODE_TAUT;
4166 : : }
4167 : :
4168 [ - + - - ]: 161 : switch ((int)nUserMode & (REQ_MODE_BASIC | REQ_MODE_TAUT))
4169 : : {
4170 : 0 : case REQ_MODE_BASIC:
4171 : 0 : n1 = TAUT_NON;
4172 : 0 : n2 = TAUT_NON;
4173 : 0 : break;
4174 : 161 : case REQ_MODE_TAUT:
4175 : 161 : n1 = TAUT_YES;
4176 : 161 : n2 = TAUT_YES;
4177 : 161 : break;
4178 : 0 : case (REQ_MODE_BASIC | REQ_MODE_TAUT):
4179 : 0 : n1 = TAUT_NON;
4180 : 0 : n2 = TAUT_YES;
4181 : 0 : break;
4182 : 0 : default:
4183 : 0 : ret = -3;
4184 : 0 : goto exit_function; /* program error: inconsistent nUserMode or missing taut/non-taut allocation */ /* <BRKPT> */
4185 : : }
4186 : :
4187 : : /*
4188 : : Obtain all non-stereo canonical numberings
4189 : : */
4190 : :
4191 [ + - + - ]: 161 : if ((nUserMode & REQ_MODE_NON_ISO) && !(nUserMode & REQ_MODE_ISO))
4192 : : {
4193 : : /* added for special non-isotopic test mode 2004-10-04 */
4194 [ + - ]: 161 : if (t_group_info)
4195 : : {
4196 : 161 : t_group_info->bIgnoreIsotopic = 1;
4197 [ - + ]: 161 : if (t_group_info->nIsotopicEndpointAtomNumber)
4198 : : {
4199 : 0 : t_group_info->nIsotopicEndpointAtomNumber[0] = inchi_min(1, t_group_info->nIsotopicEndpointAtomNumber[0]);
4200 : : }
4201 : 161 : memset(t_group_info->num_iso_H, 0, sizeof(t_group_info->num_iso_H)); /* djb-rwth: memset_s C11/Annex K variant? */
4202 : 161 : memset(t_group_info->tni.nNumRemovedProtonsIsotopic, 0, sizeof(t_group_info->tni.nNumRemovedProtonsIsotopic)); /* djb-rwth: memset_s C11/Annex K variant? */
4203 : 161 : t_group_info->bTautFlagsDone &= ~(TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE);
4204 : : }
4205 [ + + ]: 483 : for (i = 0; i < TAUT_NUM; i++)
4206 : : {
4207 : 322 : s[i].bHasIsotopicTautGroups = 0;
4208 : 322 : s[i].bIgnoreIsotopic = 1;
4209 : 322 : s[i].nLenIsotopic = 0;
4210 : 322 : s[i].nLenIsotopicEndpoints = 0;
4211 : 322 : s[i].nLenLinearCTIsotopicTautomer = 0;
4212 : 322 : s[i].num_isotopic_atoms = 0;
4213 : : }
4214 : 161 : bHasIsotopicAtoms = 0;
4215 : : }
4216 : :
4217 : 161 : ret = GetBaseCanonRanking(ic, num_atoms, num_at_tg, at,
4218 : : t_group_info, s, pBCN, ulMaxTime,
4219 : : pCG, bFixIsoFixedH, LargeMolecules);
4220 : :
4221 [ - + ]: 161 : if (ret < 0)
4222 : : {
4223 : 0 : goto exit_function; /* program error */
4224 : : }
4225 : : #if ( bRELEASE_VERSION == 0 && FIND_CANON_NE_EQUITABLE == 1 )
4226 : : /* Debug only: find whether canonical equivalence is different from equitable partition */
4227 : : if (bCanonIsFinerThanEquitablePartition(num_atoms, at[n1], pBCN->ftcn[TAUT_NON].nSymmRankCt))
4228 : : {
4229 : : bExtract |= EXTR_CANON_NE_EQUITABLE;
4230 : : }
4231 : : #endif
4232 : :
4233 : : /* added for special non-isotopic test mode 2004-10-04 */
4234 [ - + ]: 161 : if (!pBCN->ftcn[n1].PartitionCt.Rank)
4235 : : {
4236 [ # # ]: 0 : n1 = ALT_TAUT(n1);
4237 : : }
4238 [ - + ]: 161 : if (!pBCN->ftcn[n2].PartitionCt.Rank)
4239 : : {
4240 [ # # ]: 0 : n2 = ALT_TAUT(n2);
4241 : : }
4242 [ - + ]: 161 : if (n1 > n2)
4243 : : {
4244 : 0 : ret = CT_TAUCOUNT_ERR;
4245 : 0 : goto exit_function; /* program error */
4246 : : }
4247 : :
4248 : : /*
4249 : : Obtain stereo canonical numberings
4250 : : */
4251 : :
4252 [ + + - + : 322 : for (i = n2; i >= n1 && !RETURNED_ERROR(ret); i--)
+ - ]
4253 : : {
4254 : 161 : memset(pCS, 0, sizeof(*pCS)); /* djb-rwth: memset_s C11/Annex K variant? */
4255 : :
4256 [ - + - ]: 161 : switch (i)
4257 : : {
4258 : 0 : case TAUT_NON:
4259 : : /* non-tautomeric */
4260 : : /* djb-rwth: removing redundant code */
4261 [ # # ]: 0 : nMode = (s[i].nLenLinearCTTautomer == 0) ? CANON_MODE_CT : CANON_MODE_TAUT;
4262 [ # # # # ]: 0 : nMode |= (bHasIsotopicAtoms && (nUserMode & REQ_MODE_ISO)) ? CANON_MODE_ISO : 0;
4263 [ # # # # ]: 0 : nMode |= (s[TAUT_NON].bMayHaveStereo && (nUserMode & REQ_MODE_STEREO)) ? CANON_MODE_STEREO : 0;
4264 [ # # # # : 0 : nMode |= (bHasIsotopicAtoms && s[TAUT_NON].bMayHaveStereo && (nUserMode & REQ_MODE_ISO_STEREO)) ? CANON_MODE_ISO_STEREO : 0;
# # ]
4265 : 0 : nMode |= (nUserMode & REQ_MODE_NOEQ_STEREO) ? CMODE_NOEQ_STEREO : 0;
4266 : 0 : nMode |= (nUserMode & REQ_MODE_REDNDNT_STEREO) ? CMODE_REDNDNT_STEREO : 0;
4267 : 0 : nMode |= (nUserMode & REQ_MODE_NO_ALT_SBONDS) ? CMODE_NO_ALT_SBONDS : 0;
4268 [ # # ]: 0 : if ((nMode & CANON_MODE_STEREO) == CANON_MODE_STEREO ||
4269 [ # # ]: 0 : (nMode & CANON_MODE_ISO_STEREO) == CANON_MODE_ISO_STEREO)
4270 : : {
4271 : 0 : nMode |= (nUserMode & REQ_MODE_RELATIVE_STEREO) ? CMODE_RELATIVE_STEREO : 0;
4272 : 0 : nMode |= (nUserMode & REQ_MODE_RACEMIC_STEREO) ? CMODE_RACEMIC_STEREO : 0;
4273 : 0 : nMode |= (nUserMode & REQ_MODE_SC_IGN_ALL_UU) ? CMODE_SC_IGN_ALL_UU : 0;
4274 : 0 : nMode |= (nUserMode & REQ_MODE_SB_IGN_ALL_UU) ? CMODE_SB_IGN_ALL_UU : 0;
4275 : : }
4276 [ # # ]: 0 : if ((ret = AllocateCS(pCS, num_atoms, num_atoms, s[TAUT_NON].nLenCT, s[TAUT_NON].nLenCTAtOnly,
4277 : : s[TAUT_NON].nLenLinearCTStereoDble, s[TAUT_NON].nMaxNumStereoBonds,
4278 : : s[TAUT_NON].nLenLinearCTStereoCarb, s[TAUT_NON].nMaxNumStereoAtoms,
4279 : : 0, 0, s[TAUT_NON].nLenIsotopic, nMode, pBCN))) /* djb-rwth: addressing LLVM warning */
4280 : : {
4281 : 0 : goto exit_function;
4282 : : }
4283 : 0 : *pCS2 = *pCS;
4284 : 0 : break;
4285 : 161 : case TAUT_YES: /* tautomeric */
4286 : : /* djb-rwth: removing redundant code */
4287 [ + + ]: 161 : nMode = (s[i].nLenLinearCTTautomer == 0) ? CANON_MODE_CT : CANON_MODE_TAUT;
4288 [ - + - - ]: 161 : nMode |= (bHasIsotopicAtoms && (nUserMode & REQ_MODE_ISO)) ? CANON_MODE_ISO : 0;
4289 [ + + + - ]: 161 : nMode |= (s[TAUT_YES].bMayHaveStereo && (nUserMode & REQ_MODE_STEREO)) ? CANON_MODE_STEREO : 0;
4290 [ - + - - : 161 : nMode |= (bHasIsotopicAtoms && s[TAUT_YES].bMayHaveStereo && (nUserMode & REQ_MODE_ISO_STEREO)) ? CANON_MODE_ISO_STEREO : 0;
- - ]
4291 : 161 : nMode |= (nUserMode & REQ_MODE_NOEQ_STEREO) ? CMODE_NOEQ_STEREO : 0;
4292 : 161 : nMode |= (nUserMode & REQ_MODE_REDNDNT_STEREO) ? CMODE_REDNDNT_STEREO : 0;
4293 : 161 : nMode |= (nUserMode & REQ_MODE_NO_ALT_SBONDS) ? CMODE_NO_ALT_SBONDS : 0;
4294 [ + + ]: 161 : if ((nMode & CANON_MODE_STEREO) == CANON_MODE_STEREO ||
4295 [ - + ]: 91 : (nMode & CANON_MODE_ISO_STEREO) == CANON_MODE_ISO_STEREO)
4296 : : {
4297 : 70 : nMode |= (nUserMode & REQ_MODE_RELATIVE_STEREO) ? CMODE_RELATIVE_STEREO : 0;
4298 : 70 : nMode |= (nUserMode & REQ_MODE_RACEMIC_STEREO) ? CMODE_RACEMIC_STEREO : 0;
4299 : 70 : nMode |= (nUserMode & REQ_MODE_SC_IGN_ALL_UU) ? CMODE_SC_IGN_ALL_UU : 0;
4300 : 70 : nMode |= (nUserMode & REQ_MODE_SB_IGN_ALL_UU) ? CMODE_SB_IGN_ALL_UU : 0;
4301 : : }
4302 [ - + ]: 161 : if ((ret = AllocateCS(pCS, num_atoms, num_at_tg, s[TAUT_YES].nLenCT, s[TAUT_YES].nLenCTAtOnly,
4303 : : s[TAUT_YES].nLenLinearCTStereoDble, s[TAUT_YES].nMaxNumStereoBonds,
4304 : : s[TAUT_YES].nLenLinearCTStereoCarb, s[TAUT_YES].nMaxNumStereoAtoms,
4305 : : s[TAUT_YES].nLenLinearCTTautomer, s[TAUT_YES].nLenLinearCTIsotopicTautomer,
4306 : : s[TAUT_YES].nLenIsotopic, nMode, pBCN))) /* djb-rwth: addressing LLVM warning */
4307 : : {
4308 : 0 : goto exit_function;
4309 : : }
4310 : 161 : *pCS2 = *pCS;
4311 : 161 : break;
4312 : : }
4313 : :
4314 : : /* 2009-12-05 */
4315 : 161 : nMode |= (nUserMode & REQ_MODE_DIFF_UU_STEREO) ? REQ_MODE_DIFF_UU_STEREO : 0;
4316 : : /* 2009-12-05 */
4317 : :
4318 : : /* settings */
4319 : 161 : pCS->lNumDecreasedCT = -1;
4320 : 161 : pCS->bDoubleBondSquare = DOUBLE_BOND_NEIGH_LIST ? 2 : 0; /* 2 => special mode */
4321 [ + - ]: 322 : pCS->bIgnoreIsotopic = !((s[TAUT_NON].num_isotopic_atoms ||
4322 [ + - ]: 161 : s[TAUT_YES].num_isotopic_atoms ||
4323 [ + - ]: 161 : s[TAUT_YES].bHasIsotopicTautGroups) ||
4324 [ - + ]: 161 : (nUserMode & REQ_MODE_NON_ISO) ||
4325 [ # # ]: 0 : !(nUserMode & REQ_MODE_ISO));
4326 : :
4327 [ + - + - ]: 161 : if ((nUserMode & REQ_MODE_NON_ISO) && !(nUserMode & REQ_MODE_ISO))
4328 : : {
4329 : 161 : pCS->bIgnoreIsotopic = 1; /* 10-04-2004 */
4330 : : }
4331 : :
4332 [ + - ]: 161 : if (i == TAUT_YES)
4333 : : {
4334 : : /* tautomeric */
4335 : 161 : pCS->t_group_info = t_group_info; /* ??? make a copy or reuse ??? */
4336 [ + - ]: 322 : pCS->t_group_info->bIgnoreIsotopic = !(s[TAUT_YES].bHasIsotopicTautGroups ||
4337 [ - + ]: 161 : (nUserMode & REQ_MODE_NON_ISO) ||
4338 [ # # ]: 0 : !(nUserMode & REQ_MODE_ISO));
4339 [ + - + - ]: 161 : if ((nUserMode & REQ_MODE_NON_ISO) && !(nUserMode & REQ_MODE_ISO))
4340 : : {
4341 : 161 : pCS->t_group_info->bIgnoreIsotopic = 1; /* 10-04-2004 */
4342 : : }
4343 : : }
4344 : :
4345 : 161 : pCS->ulTimeOutTime = pBCN->ulTimeOutTime;
4346 : : /*=========== Obsolete Mode Bits (bit 0 is Least Significant Bit) ===========
4347 : : *
4348 : : * Mode Bits Description
4349 : : * '0' c 0 Only one connection table canonicalization
4350 : : * '1' C 1 Recalculate CT using fixed nSymmRank
4351 : : * '2' i 1|2 Isotopic canonicalization (internal)
4352 : : * '3' I 1|2|4 Isotopic canonicalization (output)
4353 : : * '4' s 1|8 Stereo canonicalization
4354 : : * '5' S 1|2|4|16 Stereo isotopic canonicalization
4355 : : * '6' A 1|2|4|8|16 Output All
4356 : : */
4357 : :
4358 : : /*
4359 : : The last canonicalization step
4360 : : */
4361 : :
4362 [ + - ]: 161 : if (pBCN)
4363 : : {
4364 : : /* USE_CANON2 == 1 */
4365 : 161 : pCS->NeighList = NULL;
4366 : 161 : pCS->pBCN = pBCN;
4367 : :
4368 [ + - ]: 161 : ret = Canon_INChI(ic,
4369 : : num_atoms,
4370 : : i ? num_at_tg : num_atoms,
4371 : : at[i], pCS,
4372 : : pCG,
4373 : : nMode, i);
4374 : : }
4375 : : else
4376 : : {
4377 : : /* old way */
4378 [ # # ]: 0 : pCS->NeighList = CreateNeighList(num_atoms,
4379 : : i ? num_at_tg : num_atoms,
4380 : : at[i],
4381 : : pCS->bDoubleBondSquare,
4382 : : pCS->t_group_info);
4383 : 0 : pCS->pBCN = NULL;
4384 : :
4385 [ # # ]: 0 : ret = Canon_INChI(ic,
4386 : : num_atoms,
4387 : : i ? num_at_tg : num_atoms,
4388 : : at[i], pCS,
4389 : : pCG,
4390 : : nMode, i);
4391 : : }
4392 : :
4393 : 161 : pINChI = ppINChI[i]; /* pointers to already allocated still empty InChI */
4394 : 161 : pINChI_Aux = ppINChI_Aux[i];
4395 : :
4396 [ - + ]: 161 : if (ret <= 0)
4397 : : {
4398 : : /*
4399 : : Failure in Canon_INChI()
4400 : : */
4401 : 0 : pINChI->nErrorCode = ret;
4402 : 0 : pINChI_Aux->nErrorCode = ret;
4403 : : }
4404 : : else
4405 : : {
4406 : : /*
4407 : : Success Canon_INChI()
4408 : :
4409 : : save canonicalization results in
4410 : : pINChI and pINChI_Aux
4411 : : */
4412 : 161 : pINChI->nErrorCode = 0;
4413 : 161 : pINChI_Aux->nErrorCode = 0;
4414 : 161 : pINChI->bDeleted = pINChI_Aux->bDeleted = out_norm_data[i]->bDeleted;
4415 : 161 : pINChI_Aux->nCanonFlags = pCS->nCanonFlags;
4416 : 161 : pINChI_Aux->bTautFlags = out_norm_data[i]->bTautFlags;
4417 : 161 : pINChI_Aux->bTautFlagsDone = out_norm_data[i]->bTautFlagsDone;
4418 : 161 : pINChI_Aux->bNormalizationFlags = out_norm_data[i]->bNormalizationFlags;
4419 : :
4420 : : /* may return an error or a warning */
4421 [ + - ]: 161 : ret = FillOutINChI(pINChI, pINChI_Aux,
4422 : : num_atoms, i ? num_at_tg : num_atoms,
4423 : : i ? num_removed_H_taut : num_removed_H, at[i],
4424 [ + - ]: 161 : out_norm_data[i]->at, pCS,
4425 : : pCG,
4426 : : i, nUserMode,
4427 : : pStrErrStruct, ip->bNoWarnings);
4428 : :
4429 [ + - - + ]: 161 : if (RETURNED_ERROR(ret))
4430 : : {
4431 : : /* Failure in FillOutINChI() */
4432 : 0 : pINChI->nErrorCode = ret;
4433 : 0 : pINChI_Aux->nErrorCode = ret;
4434 : : }
4435 : : else
4436 : : {
4437 : : /* Success in FillOutINChI() */
4438 : :
4439 : : #if ( bRELEASE_VERSION == 0 )
4440 : : if (pINChI->Stereo &&
4441 : : (pINChI->Stereo->nCompInv2Abs && !pINChI->Stereo->bTrivialInv) ||
4442 : : pINChI->StereoIsotopic &&
4443 : : (pINChI->StereoIsotopic->nCompInv2Abs && !pINChI->StereoIsotopic->bTrivialInv))
4444 : : {
4445 : : bExtract |= EXTR_NON_TRIVIAL_STEREO;
4446 : : }
4447 : : #endif
4448 : : /* Mark non-tautomeric representation as having
4449 : : another, tautomeric representation */
4450 [ + - + + ]: 161 : if (pINChI_Aux && s[TAUT_YES].nLenLinearCTTautomer)
4451 : : {
4452 : 14 : pINChI_Aux->bIsTautomeric = s[TAUT_YES].nLenLinearCTTautomer;
4453 : : }
4454 : : #if ( bRELEASE_VERSION == 0 )
4455 : : pCS->bExtract |= bExtract;
4456 : : pINChI->bExtract |= pCS->bExtract;
4457 : : #endif
4458 : :
4459 [ + - ]: 161 : ret2 = CheckCanonNumberingCorrectness(num_atoms,
4460 : : i ? num_at_tg : num_atoms,
4461 : : at[i], pCS,
4462 : : pCG,
4463 : : i, pStrErrStruct);
4464 [ - + - - ]: 161 : if (ret2 && pINChI_Aux) /* djb-rwth: fixing a NULL pointer dereference */
4465 : : {
4466 : 0 : pINChI->nErrorCode = ret2;
4467 : 0 : pINChI_Aux->nErrorCode = ret2;
4468 : 0 : ret = ret2;
4469 : : }
4470 : : }
4471 : : }
4472 : :
4473 : 161 : FreeNeighList(pCS->NeighList);
4474 : 161 : DeAllocateCS(pCS2);
4475 : :
4476 : 161 : pINChI = NULL; /* avoid dangling pointers */
4477 : 161 : pINChI_Aux = NULL; /* avoid dangling pointers */
4478 : : }
4479 : :
4480 [ - + ]: 161 : if (ret == 0)
4481 : : {
4482 : 161 : ret = num_atoms;
4483 : : }
4484 : : /* treat the results later */
4485 : :
4486 : 0 : exit_function:
4487 : :
4488 : 161 : DeAllocBCN(pBCN);
4489 [ + - ]: 161 : if (at[TAUT_YES])
4490 : : {
4491 [ + - ]: 161 : inchi_free(at[TAUT_YES]);
4492 : : }
4493 [ - + ]: 161 : if (at[TAUT_NON])
4494 : : {
4495 [ # # ]: 0 : inchi_free(at[TAUT_NON]);
4496 : : }
4497 [ - + ]: 161 : if (ti_out)
4498 : : {
4499 : 0 : *ti_out = *t_group_info;
4500 : : }
4501 : : else
4502 : : {
4503 : : /* free_t_group_info(t_group_info); */
4504 [ + - ]: 161 : if (t_group_info) /* djb-rwth: fixing oss-fuzz issue #42537161/70475 */
4505 : : {
4506 [ + + ]: 161 : if (t_group_info->nEndpointAtomNumber)
4507 : : {
4508 [ + - ]: 7 : inchi_free(t_group_info->nEndpointAtomNumber);
4509 : : }
4510 [ + + ]: 161 : if (t_group_info->tGroupNumber)
4511 : : {
4512 [ + - ]: 7 : inchi_free(t_group_info->tGroupNumber);
4513 : : }
4514 [ - + ]: 161 : if (t_group_info->nIsotopicEndpointAtomNumber)
4515 : : {
4516 [ # # ]: 0 : inchi_free(t_group_info->nIsotopicEndpointAtomNumber);
4517 : : }
4518 [ + - ]: 161 : if (t_group_info->t_group)
4519 : : {
4520 [ + - ]: 161 : inchi_free(t_group_info->t_group);
4521 : : }
4522 : 161 : memset(t_group_info, 0, sizeof(*t_group_info)); /* djb-rwth: memset_s C11/Annex K variant? */
4523 : : }
4524 : : }
4525 : 161 : free_t_group_info(t_group_info_orig);
4526 : :
4527 [ - + ]: 161 : if (ebend)
4528 : : {
4529 [ # # ]: 0 : inchi_free(ebend);
4530 : : }
4531 : :
4532 : 161 : return ret;
4533 : : }
4534 : :
4535 : :
4536 : : /****************************************************************************/
4537 : : #ifndef COMPILE_ANSI_ONLY /* { */
4538 : : int GetAtomOrdNbrInCanonOrd(struct tagCANON_GLOBALS* pCG,
4539 : : inp_ATOM* norm_at,
4540 : : AT_NUMB* nAtomOrdNbr,
4541 : : AT_NUMB* nOrigAtNosInCanonOrd,
4542 : : int num_at)
4543 : : {
4544 : : AT_NUMB* nCanonNbr, * nOrigAtNos, * nOrigAtNosOrd;
4545 : : int i, ret;
4546 : :
4547 : : ret = 0;
4548 : :
4549 : : nCanonNbr = (AT_NUMB*)inchi_calloc(num_at, sizeof(nCanonNbr[0]));
4550 : : nOrigAtNos = (AT_NUMB*)inchi_calloc(num_at, sizeof(nOrigAtNos[0]));
4551 : : nOrigAtNosOrd = (AT_NUMB*)inchi_calloc(num_at, sizeof(nOrigAtNosOrd[0]));
4552 : :
4553 : : if (!nCanonNbr || !nOrigAtNos || !nAtomOrdNbr || !nOrigAtNosOrd)
4554 : : {
4555 : : ret = CT_OUT_OF_RAM; /* <BRKPT> */
4556 : : goto exit_function;
4557 : : }
4558 : : for (i = 0; i < num_at; i++)
4559 : : {
4560 : : nCanonNbr[i] = nAtomOrdNbr[i] = nOrigAtNosOrd[i] = (AT_NUMB)i;
4561 : : nOrigAtNos[i] = norm_at[i].orig_at_number;
4562 : : }
4563 : :
4564 : : /* get nCanonNbr[]: canon. numbers-1 in order of increasing original atom numbers */
4565 : : pCG->m_pn_RankForSort = nOrigAtNosInCanonOrd;
4566 : : inchi_qsort(pCG, nCanonNbr, num_at, sizeof(nCanonNbr[0]), CompRank);
4567 : : /* get nOrigAtNosOrd[]: norm_atom ord. numbers the same order of increasing original atom numbers */
4568 : : pCG->m_pn_RankForSort = nOrigAtNos;
4569 : : inchi_qsort(pCG, nOrigAtNosOrd, num_at, sizeof(nOrigAtNosOrd[0]), CompRank);
4570 : :
4571 : : /* check whether the 2 sets of origiginal atom numbers have identical elements */
4572 : : for (i = 0; i < num_at; i++)
4573 : : {
4574 : : if (nOrigAtNosInCanonOrd[nCanonNbr[i]] != nOrigAtNos[nOrigAtNosOrd[i]])
4575 : : {
4576 : : ret = CT_RANKING_ERR; /* <BRKPT> */
4577 : : goto exit_function;
4578 : : }
4579 : : }
4580 : : for (i = 0; i < num_at; i++)
4581 : : {
4582 : : nAtomOrdNbr[(int)nCanonNbr[i]] = nOrigAtNosOrd[i];
4583 : : }
4584 : :
4585 : : /*
4586 : : pn_RankForSort = nCanonNbr;
4587 : : qsort( nAtomOrdNbr, num_at, sizeof(nCanonNbr[0]), CompRank );
4588 : : */
4589 : :
4590 : : exit_function:
4591 : : if (nCanonNbr)
4592 : : {
4593 : : inchi_free(nCanonNbr);
4594 : : }
4595 : : if (nOrigAtNos)
4596 : : {
4597 : : inchi_free(nOrigAtNos);
4598 : : }
4599 : : if (nOrigAtNosOrd)
4600 : : {
4601 : : inchi_free(nOrigAtNosOrd);
4602 : : }
4603 : :
4604 : : return ret;
4605 : : }
4606 : :
4607 : :
4608 : : /****************************************************************************/
4609 : : int FillOutCanonInfAtom(struct tagCANON_GLOBALS* pCG,
4610 : : inp_ATOM* norm_at,
4611 : : INF_ATOM_DATA* inf_norm_at_data,
4612 : : int init_num_at,
4613 : : int bIsotopic,
4614 : : INChI* pINChI,
4615 : : INChI_Aux* pINChI_Aux,
4616 : : int bAbcNumbers,
4617 : : INCHI_MODE nMode)
4618 : : {
4619 : : int i, j, m, n, num_stereo, k, c, ret, len_str, len, atw; /* djb-rwth: removing redundant variable */
4620 : : int next_atom[MAX_CUMULENE_LEN + 1], best_next_atom[MAX_CUMULENE_LEN + 1], cur_atom;
4621 : : int next_neigh[MAX_CUMULENE_LEN + 1], best_next_neigh[MAX_CUMULENE_LEN + 1], best_len;
4622 : : int num_iso_H[NUM_H_ISOTOPES];
4623 : : char* str;
4624 : : AT_NUMB g, e;
4625 : : int num_at = pINChI->nNumberOfAtoms;
4626 : : int nNumberOfTGroups = (pINChI->lenTautomer && pINChI->nTautomer && pINChI->nTautomer[0]) ? (int)pINChI->nTautomer[0] : 0;
4627 : : AT_NUMB* nOrigAtNosInCanonOrd;
4628 : : INChI_Stereo* Stereo;
4629 : : AT_NUMB* nConstitEquNumbers;
4630 : : AT_NUMB* nConstitEquTGroupNumbers;
4631 : : S_CHAR* t_parity = NULL;
4632 : : AT_NUMB* nNumber = NULL;
4633 : : int bIncludeIsotopicH;
4634 : :
4635 : : AT_NUMB* nNormAtNosInCanonOrd;
4636 : : int(*MakeNumber)(char*, int, const char*, int) = bAbcNumbers ? MakeAbcNumber : MakeDecNumber;
4637 : : int bRel = (0 != (nMode & (REQ_MODE_RELATIVE_STEREO)));
4638 : : int bRac = (0 != (nMode & (REQ_MODE_RACEMIC_STEREO)));
4639 : : int bRelRac = bRel || bRac;
4640 : : int bDoDisplaySp3 = 1;
4641 : :
4642 : : inf_ATOM* inf_norm_at = inf_norm_at_data ? inf_norm_at_data->at : NULL;
4643 : :
4644 : : ret = 0;
4645 : : /* djb-rwth: removing redundant code */
4646 : :
4647 : : if (!inf_norm_at)
4648 : : {
4649 : : return ret;
4650 : : }
4651 : :
4652 : : /* prepare removeable protons and H info */
4653 : : inf_norm_at_data->nNumRemovedProtons = pINChI_Aux->nNumRemovedProtons;
4654 : :
4655 : : MakeRemovedProtonsString(pINChI_Aux->nNumRemovedProtons, pINChI_Aux->nNumRemovedIsotopicH, NULL, bIsotopic,
4656 : : inf_norm_at_data->szRemovedProtons, &inf_norm_at_data->num_removed_iso_H);
4657 : :
4658 : : /* fill out info atom */
4659 : : if (bIsotopic && !(pINChI->nNumberOfIsotopicAtoms || pINChI->nNumberOfIsotopicTGroups ||
4660 : : (pINChI->nPossibleLocationsOfIsotopicH && pINChI->nPossibleLocationsOfIsotopicH[0] > 1))) /* djb-rwth: addressing LLVM warning */
4661 : : {
4662 : : bIsotopic = 0;
4663 : : }
4664 : :
4665 : : Stereo = bIsotopic ? pINChI->StereoIsotopic :
4666 : : pINChI->Stereo;
4667 : : bDoDisplaySp3 = (NULL != Stereo) && (Stereo->nNumberOfStereoCenters > 0);
4668 : :
4669 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
4670 : : if (bDoDisplaySp3 && bRelRac && Stereo->nNumberOfStereoCenters < 2 &&
4671 : : (Stereo->nCompInv2Abs || ATOM_PARITY_ILL_DEF(Stereo->t_parity[0])))
4672 : : {
4673 : : bDoDisplaySp3 = 0;
4674 : : if (Stereo->nCompInv2Abs)
4675 : : {
4676 : : inf_norm_at_data->StereoFlags |= bRel ? INF_STEREO_REL : bRac ? INF_STEREO_RAC : 0;
4677 : : }
4678 : : }
4679 : : #endif
4680 : : /* flag has stereo */
4681 : : if ((NULL != Stereo) && (bDoDisplaySp3 || Stereo->nNumberOfStereoBonds > 0))
4682 : : {
4683 : : inf_norm_at_data->StereoFlags |= INF_STEREO;
4684 : : }
4685 : :
4686 : : /*
4687 : : if ( bDoDisplaySp3 && bRelRac && Stereo->nNumberOfStereoCenters < 2 &&
4688 : : (Stereo->nCompInv2Abs || ATOM_PARITY_ILL_DEF(Stereo->t_parity[0]) ) ) {
4689 : : bDoDisplaySp3 = 0;
4690 : : }
4691 : : */
4692 : : if (bDoDisplaySp3 && Stereo->nCompInv2Abs)
4693 : : {
4694 : : /* inversion changes stereo */
4695 : : if (bRel)
4696 : : {
4697 : : inf_norm_at_data->StereoFlags |= INF_STEREO_REL;
4698 : : }
4699 : : else
4700 : : {
4701 : : if (bRac)
4702 : : {
4703 : : inf_norm_at_data->StereoFlags |= INF_STEREO_RAC;
4704 : : }
4705 : : else
4706 : : {
4707 : : inf_norm_at_data->StereoFlags |= INF_STEREO_ABS;
4708 : : }
4709 : : }
4710 : : if (bRelRac)
4711 : : {
4712 : : inf_norm_at_data->StereoFlags |= (Stereo->nCompInv2Abs > 0) ? INF_STEREO_NORM : INF_STEREO_INV;
4713 : : }
4714 : : }
4715 : : if (bDoDisplaySp3 && Stereo->nCompInv2Abs < 0 && !bRelRac)
4716 : : {
4717 : : /* display Inv stereo which is Absolute Stereo */
4718 : : nNumber = Stereo->nNumberInv;
4719 : : t_parity = Stereo->t_parityInv;
4720 : : nOrigAtNosInCanonOrd = bIsotopic ? pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv :
4721 : : pINChI_Aux->nOrigAtNosInCanonOrdInv;
4722 : : }
4723 : : else
4724 : : {
4725 : : /* display Inv stereo which is Absolute Stereo */
4726 : : if (bDoDisplaySp3)
4727 : : {
4728 : : nNumber = Stereo->nNumber;
4729 : : t_parity = Stereo->t_parity;
4730 : : }
4731 : : nOrigAtNosInCanonOrd = bIsotopic ? pINChI_Aux->nIsotopicOrigAtNosInCanonOrd :
4732 : : pINChI_Aux->nOrigAtNosInCanonOrd;
4733 : : }
4734 : :
4735 : : nConstitEquNumbers = bIsotopic ? pINChI_Aux->nConstitEquIsotopicNumbers :
4736 : : pINChI_Aux->nConstitEquNumbers;
4737 : : nConstitEquTGroupNumbers = bIsotopic ? pINChI_Aux->nConstitEquIsotopicTGroupNumbers :
4738 : : pINChI_Aux->nConstitEquTGroupNumbers;
4739 : : memset(inf_norm_at, 0, init_num_at * sizeof(inf_norm_at[0])); /* djb-rwth: memset_s C11/Annex K variant? */
4740 : :
4741 : : /* obtain norm_at[] atom numbers (from zero) in order of canonical numbers */
4742 : : nNormAtNosInCanonOrd = (AT_NUMB*)inchi_calloc(num_at, sizeof(nNormAtNosInCanonOrd[0]));
4743 : : if ((ret = GetAtomOrdNbrInCanonOrd(pCG, norm_at, nNormAtNosInCanonOrd, nOrigAtNosInCanonOrd, num_at))) /* djb-rwth: addressing LLVM warning */
4744 : : {
4745 : : goto exit_function;
4746 : : }
4747 : :
4748 : : /* atom canonical and equivalence numbers > 0 */
4749 : : for (i = 0; i < num_at; i++)
4750 : : {
4751 : : j = (int)nNormAtNosInCanonOrd[i];
4752 : : if (j < 0 || j >= num_at)
4753 : : {
4754 : : continue;
4755 : : }
4756 : : inf_norm_at[j].nCanonNbr = (AT_NUMB)(i + 1);
4757 : : inf_norm_at[j].nCanonEquNbr = nConstitEquNumbers[i];
4758 : : #ifdef DISPLAY_DEBUG_DATA
4759 : : inf_norm_at[j].nDebugData = 0;
4760 : : #if ( DISPLAY_DEBUG_DATA == DISPLAY_DEBUG_DATA_C_POINT )
4761 : : inf_norm_at[j].nDebugData = norm_at[j].c_point;
4762 : : #endif
4763 : : #endif
4764 : : }
4765 : : /* tautomeric groups */
4766 : : if (nNumberOfTGroups)
4767 : : {
4768 : : /*
4769 : : : start from 1: bypass number of t-groups
4770 : : : j is a counter within the current t-group
4771 : : : g is a tautomeric group canonical number
4772 : : : e is a tautomeric group equivalence
4773 : : */
4774 : : for (g = 1, i = 1; g <= nNumberOfTGroups; g++)
4775 : : {
4776 : : n = (int)pINChI->nTautomer[i] - INCHI_T_NUM_MOVABLE; /* number of atoms in t-group */
4777 : : e = nConstitEquTGroupNumbers[(int)g - 1];
4778 : : /* bypass number of hydrogen atoms, negative charges, ... */
4779 : : for (i += INCHI_T_NUM_MOVABLE + 1, j = 0; j < n && i < pINChI->lenTautomer; j++, i++)
4780 : : {
4781 : : /* scan canonical numbers of atoms within the atom t-group */
4782 : : k = (int)nNormAtNosInCanonOrd[(int)pINChI->nTautomer[i] - 1];
4783 : : inf_norm_at[k].nTautGroupCanonNbr = g;
4784 : : inf_norm_at[k].nTautGroupEquNbr = e;
4785 : : }
4786 : : }
4787 : : if (i != pINChI->lenTautomer || g != nNumberOfTGroups + 1)
4788 : : {
4789 : : ret = CT_TAUCOUNT_ERR; /* <BRKPT> */
4790 : : goto exit_function;
4791 : : }
4792 : : }
4793 : : /* atoms that may exchange isotopic H */
4794 : : if (bIsotopic && pINChI->nPossibleLocationsOfIsotopicH && (n = (int)pINChI->nPossibleLocationsOfIsotopicH[0]))
4795 : : {
4796 : : for (i = 1; i < n; i++)
4797 : : {
4798 : : j = (int)pINChI->nPossibleLocationsOfIsotopicH[i];
4799 : : k = (int)nNormAtNosInCanonOrd[j - 1];
4800 : : if (!inf_norm_at[k].nTautGroupCanonNbr)
4801 : : {
4802 : : inf_norm_at[k].cFlags |= AT_FLAG_ISO_H_POINT;
4803 : : }
4804 : : }
4805 : : }
4806 : :
4807 : : #if ( DISPLAY_RING_SYSTEMS == 1 )
4808 : : /* debug only */
4809 : : for (j = 0; j < num_at; j++)
4810 : : {
4811 : : inf_norm_at[j].nCanonNbr = norm_at[j].nBlockSystem;
4812 : : inf_norm_at[j].nCanonEquNbr = norm_at[j].nRingSystem;
4813 : : #if ( USE_DISTANCES_FOR_RANKING == 1 )
4814 : : inf_norm_at[j].nTautGroupCanonNbr = norm_at[j].nDistanceFromTerminal;
4815 : : inf_norm_at[j].nTautGroupEquNbr = norm_at[j].bCutVertex;
4816 : : #else
4817 : : inf_norm_at[j].nTautGroupCanonNbr = norm_at[j].bCutVertex;
4818 : : inf_norm_at[j].nTautGroupEquNbr = 0;
4819 : : #endif
4820 : : }
4821 : : #endif
4822 : :
4823 : : /* Fill inf_at[i] field 'at_string':
4824 : : write isotopic mass, chemical element symbols and hydrogens, charge, radical, canon. numbers
4825 : : */
4826 : : len_str = sizeof(inf_norm_at[0].at_string);
4827 : : for (i = 0; i < init_num_at; i++)
4828 : : {
4829 : : str = inf_norm_at[i].at_string;
4830 : : len = 0;
4831 : : bIncludeIsotopicH = bIsotopic && !inf_norm_at[i].nTautGroupCanonNbr && !(inf_norm_at[i].cFlags & AT_FLAG_ISO_H_POINT);
4832 : : /* isotopic mass */
4833 : : atw = 0;
4834 : : if (norm_at[i].iso_atw_diff && bIsotopic)
4835 : : {
4836 : : if (norm_at[i].at_type == ATT_PROTON)
4837 : : {
4838 : : ; /* do not set isotopic mass of a tautomeric proton */
4839 : : }
4840 : : else
4841 : : {
4842 : : if (norm_at[i].el_number == PERIODIC_NUMBER_H && norm_at[i].chem_bonds_valence == 1 &&
4843 : : !norm_at[i].charge && !norm_at[i].radical && !norm_at[i].num_H &&
4844 : : (inf_norm_at[j = (int)norm_at[i].neighbor[0]].nTautGroupCanonNbr || (inf_norm_at[j].cFlags & AT_FLAG_ISO_H_POINT)))
4845 : : {
4846 : : ; /* do not set isotopic mass of an exchangeable proton */
4847 : : }
4848 : : else
4849 : : {
4850 : : atw = get_atomic_mass(norm_at[i].elname);
4851 : : atw += (norm_at[i].iso_atw_diff > 0) ? norm_at[i].iso_atw_diff - 1 : norm_at[i].iso_atw_diff;
4852 : : /*len += sprintf( str+len, "^%d", atw );*/
4853 : : }
4854 : : }
4855 : : }
4856 : : /* element name */
4857 : : if (norm_at[i].el_number == PERIODIC_NUMBER_H && 2 <= atw && atw <= 3)
4858 : : {
4859 : : len += sprintf(str + len, "%s", atw == 2 ? "D" : "T");
4860 : : }
4861 : : else
4862 : : {
4863 : : if (atw)
4864 : : {
4865 : : len += sprintf(str + len, "^%d", atw);
4866 : : }
4867 : : if (strcmp(norm_at[i].elname, "Zz") && strcmp(norm_at[i].elname, "Zy"))
4868 : : {
4869 : : len += sprintf(str + len, "%s", norm_at[i].elname);
4870 : : }
4871 : : else /* always show "Zy" as "Zz" */
4872 : : {
4873 : : #if ( DISPLAY_ZZ_AS_STAR == 1 )
4874 : : len += sprintf(str + len, "*");
4875 : : #else
4876 : : len += sprintf(str + len, "Zz");
4877 : : #endif
4878 : : }
4879 : : }
4880 : :
4881 : : /* hydrogens */
4882 : : /* find number of previuosly removed terminal hydrogen atoms because these terminal H will be displayed */
4883 : :
4884 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
4885 : : {
4886 : : num_iso_H[j] = norm_at[i].num_iso_H[j];
4887 : : }
4888 : : /* n = number of implicit H to display */
4889 : : for (j = num_at, n = (int)norm_at[i].num_H; j < init_num_at; j++)
4890 : : {
4891 : : /* subtract number of removed terminal */
4892 : : /* H atoms from the total number of H atoms */
4893 : : if (i == (int)norm_at[j].neighbor[0])
4894 : : {
4895 : : n -= 1; /* found explicit H => decrement number of implicit H */
4896 : : m = (int)norm_at[j].iso_atw_diff - 1;
4897 : : if (0 <= m && m < NUM_H_ISOTOPES)
4898 : : {
4899 : : /* subtract number of removed terminal isotopic H */
4900 : : /* atoms from the total number of isotopic H atoms */
4901 : : num_iso_H[m] -= 1;
4902 : : }
4903 : : }
4904 : : }
4905 : : /* at this point n = number of implicit H to display,
4906 : : num_iso_H[] contains number of implicit isotopic H among n */
4907 : : if (bIncludeIsotopicH)
4908 : : {
4909 : : /* subtract number of isotopic H atoms from the total number of H atoms */
4910 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
4911 : : {
4912 : : n -= num_iso_H[j];
4913 : : }
4914 : : }
4915 : : /* non-isotopic hydrogen atoms */
4916 : : if (n > 1)
4917 : : {
4918 : : len += sprintf(str + len, "H%d", n);
4919 : : }
4920 : : else
4921 : : {
4922 : : if (n == 1)
4923 : : {
4924 : : len += sprintf(str + len, "H");
4925 : : }
4926 : : }
4927 : :
4928 : : /* isotopic hydrogen atoms */
4929 : : if (bIncludeIsotopicH)
4930 : : {
4931 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
4932 : : {
4933 : : if (num_iso_H[j])
4934 : : {
4935 : : if (j == 0 || (j != 1 && j != 2)) /* djb-rwth: addressing LLVM warning */
4936 : : {
4937 : : len += sprintf(str + len, "^%dH", j + 1);
4938 : : }
4939 : : else
4940 : : {
4941 : : len += sprintf(str + len, j == 1 ? "D" : "T");
4942 : : }
4943 : : if (num_iso_H[j] != 1)
4944 : : {
4945 : : len += sprintf(str + len, "%d", (int)num_iso_H[j]);
4946 : : }
4947 : : }
4948 : : }
4949 : : }
4950 : : if (norm_at[i].el_number == PERIODIC_NUMBER_H && str[0] == str[1])
4951 : : {
4952 : : char* q;
4953 : : if (!str[2])
4954 : : {
4955 : : str[1] = '2'; /* quick fix: replace HH with H2 */
4956 : : }
4957 : : else
4958 : : {
4959 : : if (isdigit(UCINT str[2]) && (n = strtol(str + 2, &q, 10)) && !q[0])
4960 : : {
4961 : : len = 1 + sprintf(str + 1, "%d", n + 1);
4962 : : }
4963 : : }
4964 : : }
4965 : : /*
4966 : : if ( str[0] == 'H' && str[1] == 'H' && !str[2] ) {
4967 : : str[1] = '2';
4968 : : }
4969 : : */
4970 : : /* charge */
4971 : : if (abs(norm_at[i].charge) > 1)
4972 : : {
4973 : : len += sprintf(str + len, "%+d", norm_at[i].charge);
4974 : : }
4975 : : else
4976 : : {
4977 : : if (abs(norm_at[i].charge) == 1)
4978 : : {
4979 : : len += sprintf(str + len, "%s", norm_at[i].charge > 0 ? "+" : "-");
4980 : : }
4981 : : }
4982 : : /* radical */
4983 : : if (norm_at[i].radical)
4984 : : {
4985 : : len += sprintf(str + len, "%s", norm_at[i].radical == RADICAL_SINGLET ? ":" :
4986 : : norm_at[i].radical == RADICAL_DOUBLET ? "." :
4987 : : norm_at[i].radical == RADICAL_TRIPLET ? ".." : "?");
4988 : : }
4989 : : }
4990 : :
4991 : : /* Stereogenic centers */
4992 : : if (bDoDisplaySp3 && Stereo && 0 < (num_stereo = Stereo->nNumberOfStereoCenters))
4993 : : {
4994 : : for (i = 0; i < num_stereo; i++)
4995 : : {
4996 : : j = (int)nNormAtNosInCanonOrd[(int)nNumber[i] - 1];
4997 : : c = t_parity[i];
4998 : : c = c == 1 ? '-' : c == 2 ? '+' : c == 3 ? 'u' : c == 4 ? '?' : '*';
4999 : : inf_norm_at[j].cStereoCenterParity = c;
5000 : : str = inf_norm_at[j].at_string;
5001 : : len = (int)strlen(str);
5002 : : if (len + 3 < (int)sizeof(inf_norm_at[0].at_string))
5003 : : {
5004 : : str[len++] = '(';
5005 : : str[len++] = inf_norm_at[j].cStereoCenterParity;
5006 : : str[len++] = ')';
5007 : : str[len] = '\0';
5008 : : /* mark ambuguous stereo center */
5009 : : if (norm_at[j].bAmbiguousStereo && (c == '+' || c == '-' || c == '?') && str[0] != '!' &&
5010 : : len + 1 < (int)sizeof(inf_norm_at[0].at_string))
5011 : : {
5012 : : memmove(str + 1, str, (long long)len + 1); /* djb-rwth: cast operator added */
5013 : : str[0] = '!'; /* output the atom in red color */
5014 : : }
5015 : : }
5016 : : }
5017 : : }
5018 : :
5019 : : /* Stereogenic bonds */
5020 : : /* (cumulenes with odd number of double bonds are stereocenters, */
5021 : : /* and atom parity should be set) */
5022 : : if (Stereo && 0 < (num_stereo = Stereo->nNumberOfStereoBonds))
5023 : : {
5024 : : for (i = 0; i < num_stereo; i++)
5025 : : {
5026 : : int start_at, bAmbiguousStereoBond = 0; /* djb-rwth: removing redundant variable */
5027 : : j = (int)nNormAtNosInCanonOrd[(int)Stereo->nBondAtom1[i] - 1];
5028 : : k = (int)nNormAtNosInCanonOrd[(int)Stereo->nBondAtom2[i] - 1];
5029 : : start_at = j;
5030 : : c = Stereo->b_parity[i];
5031 : :
5032 : : c = c == 1 ? '-' : c == 2 ? '+' : c == 3 ? 'u' : c == 4 ? '?' : '*';
5033 : :
5034 : : /* mark ambuguous stereo bond atom(s) */
5035 : : if (norm_at[j].bAmbiguousStereo && (c == '+' || c == '-') &&
5036 : : (len = strlen(str = inf_norm_at[j].at_string) + 1) < (int)sizeof(inf_norm_at[0].at_string) &&
5037 : : str[0] != '!')
5038 : : {
5039 : : memmove(str + 1, str, len);
5040 : : str[0] = '!'; /* output the atom in red color */
5041 : : bAmbiguousStereoBond++;
5042 : : }
5043 : :
5044 : : if (norm_at[k].bAmbiguousStereo && (c == '+' || c == '-') &&
5045 : : (len = strlen(str = inf_norm_at[k].at_string) + 1) < (int)sizeof(inf_norm_at[0].at_string) &&
5046 : : str[0] != '!')
5047 : : {
5048 : : memmove(str + 1, str, len);
5049 : : str[0] = '!'; /* output the atom in red color */
5050 : : bAmbiguousStereoBond++;
5051 : : }
5052 : :
5053 : : /* find the opposite atom k. */
5054 : : /* Note: since it may be a cumulene, find the shortest(best) path */
5055 : : /* to atom number k to avoid confusion in case of, for example, */
5056 : : /* 4-member aromatic rings. */
5057 : : best_len = MAX_CUMULENE_LEN + 1; /* moved here from inside the cycle 1-8-2003 */
5058 : : for (n = 0; n < norm_at[j].valence; n++)
5059 : : {
5060 : : if (norm_at[j].bond_type[n] == BOND_SINGLE)
5061 : : {
5062 : : /* single bond cannot be stereogenic. */
5063 : : continue;
5064 : : }
5065 : : /* best_len = MAX_CUMULENE_LEN+1; */
5066 : : len = 0; /* number of bonds in cumulene - 1 */
5067 : : next_atom[len] = (int)norm_at[j].neighbor[n];
5068 : : next_neigh[len] = n;
5069 : : cur_atom = j;
5070 : : while (next_atom[len] != k && len < MAX_CUMULENE_LEN && 2 == norm_at[next_atom[len]].valence)
5071 : : {
5072 : : next_neigh[len + 1] = ((int)norm_at[next_atom[len]].neighbor[0] == cur_atom);
5073 : : next_atom[len + 1] = (int)norm_at[next_atom[len]].neighbor[next_neigh[len + 1]];
5074 : : cur_atom = next_atom[len];
5075 : : len++;
5076 : : }
5077 : : if (next_atom[len] == k)
5078 : : {
5079 : : if (len < best_len)
5080 : : {
5081 : : memcpy(best_next_neigh, next_neigh, sizeof(best_next_neigh));
5082 : : memcpy(best_next_atom, next_atom, sizeof(best_next_atom));
5083 : : best_len = len;
5084 : : /* djb-rwth: removing redundant code */
5085 : : if (len == 0)
5086 : : {
5087 : : break; /* path length cannot be smaller than 1 */
5088 : : }
5089 : : }
5090 : : /* djb-rwth: removing redundant code */
5091 : : }
5092 : : }
5093 : : if (best_len <= MAX_CUMULENE_LEN && best_next_atom[best_len] == k)
5094 : : {
5095 : : /* djb-rwth: removing redundant code */
5096 : : if (best_len % 2)
5097 : : {
5098 : : /* even number of bonds: chiral atom, draw parity on the cenrtal atom */
5099 : : j = best_next_atom[best_len / 2];
5100 : : inf_norm_at[j].cStereoCenterParity = c;
5101 : : str = inf_norm_at[j].at_string;
5102 : : len = (int)strlen(str);
5103 : : if (len + 3 < (int)sizeof(inf_norm_at[0].at_string))
5104 : : {
5105 : : str[len++] = '(';
5106 : : str[len++] = inf_norm_at[j].cStereoCenterParity;
5107 : : str[len++] = ')';
5108 : : str[len] = '\0';
5109 : : }
5110 : : }
5111 : : else
5112 : : {
5113 : : /* odd number of bonds: draw parity on the central bond */
5114 : : if (best_len == 0)
5115 : : {
5116 : : /* double bond */
5117 : : j = start_at;
5118 : : k = best_next_neigh[0];
5119 : : }
5120 : : else
5121 : : {
5122 : : /* cumulene */
5123 : : best_len = best_len / 2 - 1;
5124 : : j = best_next_atom[best_len];
5125 : : k = best_next_neigh[best_len + 1]; /* added +1 to display cumulene parity on the middle bond (6-24-2002) */
5126 : : }
5127 : : /* mark "forward" bond */
5128 : : for (m = 0; m < MAX_STEREO_BONDS && inf_norm_at[j].cStereoBondParity[m]; m++)
5129 : : ;
5130 : : if (m < MAX_STEREO_BONDS)
5131 : : {
5132 : : inf_norm_at[j].cStereoBondParity[m] = c;
5133 : : inf_norm_at[j].cStereoBondNumber[m] = k;
5134 : : inf_norm_at[j].cStereoBondWarning[m] = bAmbiguousStereoBond;
5135 : : }
5136 : : /* djb-rwth: removing redundant code */
5137 : : /* mark "backward" bond */
5138 : : n = norm_at[j].neighbor[k];
5139 : : for (k = 0; k < norm_at[n].valence && j != (int)norm_at[n].neighbor[k]; k++)
5140 : : {
5141 : : ;
5142 : : }
5143 : : if (k < norm_at[n].valence)
5144 : : {
5145 : : j = n;
5146 : : for (m = 0; m < MAX_STEREO_BONDS && inf_norm_at[j].cStereoBondParity[m]; m++)
5147 : : {
5148 : : ;
5149 : : }
5150 : : if (m < MAX_STEREO_BONDS)
5151 : : {
5152 : : inf_norm_at[j].cStereoBondParity[m] = c;
5153 : : inf_norm_at[j].cStereoBondNumber[m] = k;
5154 : : inf_norm_at[j].cStereoBondWarning[m] = bAmbiguousStereoBond;
5155 : : }
5156 : : /* djb-rwth: removing redundant code */
5157 : : }
5158 : : /* djb-rwth: removing redundant code */
5159 : : }
5160 : : }
5161 : : /* djb-rwth: removing redundant code */
5162 : : }
5163 : : }
5164 : :
5165 : : for (i = 0; i < init_num_at; i++)
5166 : : {
5167 : : /* canonical numbers */
5168 : : if (inf_norm_at[i].nCanonNbr)
5169 : : {
5170 : : str = inf_norm_at[i].at_string;
5171 : : len = (int)strlen(str);
5172 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nCanonNbr);
5173 : : if (inf_norm_at[i].nCanonEquNbr || inf_norm_at[i].nTautGroupCanonNbr || (inf_norm_at[i].cFlags & AT_FLAG_ISO_H_POINT))
5174 : : {
5175 : : if (inf_norm_at[i].nCanonEquNbr)
5176 : : {
5177 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nCanonEquNbr);
5178 : : }
5179 : : else
5180 : : {
5181 : : if (len + 1 < len_str)
5182 : : {
5183 : : len += 1;
5184 : : strcat(str, "/");
5185 : : }
5186 : : }
5187 : : }
5188 : : /* tautomeric groups */
5189 : : if (inf_norm_at[i].nTautGroupCanonNbr)
5190 : : {
5191 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nTautGroupCanonNbr);
5192 : : if (inf_norm_at[i].nTautGroupEquNbr)
5193 : : {
5194 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nTautGroupEquNbr);
5195 : : }
5196 : : }
5197 : : if ((inf_norm_at[i].cFlags & AT_FLAG_ISO_H_POINT) && len + 2 <= len_str)
5198 : : {
5199 : : str[len++] = '/';
5200 : : str[len++] = '*';
5201 : : str[len] = '\0';
5202 : : }
5203 : : #ifdef DISPLAY_DEBUG_DATA
5204 : : if (inf_norm_at[i].nDebugData)
5205 : : {
5206 : : len += (*MakeNumber)(str + len, len_str - len, "`", (int)inf_norm_at[i].nDebugData);
5207 : : }
5208 : : #endif
5209 : : }
5210 : : }
5211 : :
5212 : : exit_function:
5213 : :
5214 : : if (nNormAtNosInCanonOrd)
5215 : : {
5216 : : inchi_free(nNormAtNosInCanonOrd);
5217 : : }
5218 : :
5219 : : return ret;
5220 : : }
5221 : :
5222 : :
5223 : : /****************************************************************************/
5224 : : int FillOutOneCanonInfAtom(struct tagCANON_GLOBALS* pCG,
5225 : : inp_ATOM* inp_norm_at,
5226 : : INF_ATOM_DATA* inf_norm_at_data,
5227 : : AT_NUMB* pStereoFlags,
5228 : : int init_num_at,
5229 : : int offset,
5230 : : int offset_H,
5231 : : int bIsotopic,
5232 : : INChI* pINChI,
5233 : : INChI_Aux* pINChI_Aux,
5234 : : int bAbcNumbers,
5235 : : INCHI_MODE nMode)
5236 : : {
5237 : : int i, j, m, n, num_stereo, k, c, ret, len_str, len, atw; /* djb-rwth: removing redundant variable */
5238 : : int next_atom[MAX_CUMULENE_LEN + 1], best_next_atom[MAX_CUMULENE_LEN + 1], cur_atom;
5239 : : int next_neigh[MAX_CUMULENE_LEN + 1], best_next_neigh[MAX_CUMULENE_LEN + 1], best_len, bIncludeIsotopicH;
5240 : : int num_iso_H[NUM_H_ISOTOPES];
5241 : : char* str;
5242 : : AT_NUMB g, e;
5243 : : int num_at = pINChI->nNumberOfAtoms;
5244 : : int num_H = init_num_at - num_at; /* number of removed H */
5245 : : int nNumberOfTGroups = (pINChI->lenTautomer && pINChI->nTautomer && pINChI->nTautomer[0]) ? (int)pINChI->nTautomer[0] : 0;
5246 : : AT_NUMB* nOrigAtNosInCanonOrd;
5247 : : INChI_Stereo* Stereo;
5248 : : AT_NUMB* nConstitEquNumbers;
5249 : : AT_NUMB* nConstitEquTGroupNumbers;
5250 : : S_CHAR* t_parity = NULL;
5251 : : AT_NUMB* nNumber = NULL;
5252 : :
5253 : : AT_NUMB* nNormAtNosInCanonOrd;
5254 : : int(*MakeNumber)(char*, int, const char*, int) = bAbcNumbers ? MakeAbcNumber : MakeDecNumber;
5255 : : int bRel = (0 != (nMode & (REQ_MODE_RELATIVE_STEREO)));
5256 : : int bRac = (0 != (nMode & (REQ_MODE_RACEMIC_STEREO)));
5257 : : int bRelRac = bRel || bRac;
5258 : : int bDoDisplaySp3 = 1;
5259 : :
5260 : : inf_ATOM* inf_norm_at = (inf_norm_at_data && inf_norm_at_data->at) ? inf_norm_at_data->at + offset : NULL;
5261 : : inp_ATOM* norm_at = inp_norm_at ? inp_norm_at + offset : NULL;
5262 : : inf_ATOM* inf_norm_at_H = (inf_norm_at_data && inf_norm_at_data->at) ? inf_norm_at_data->at + offset_H : NULL;
5263 : : inp_ATOM* norm_at_H = inp_norm_at ? inp_norm_at + offset_H : NULL;
5264 : :
5265 : : ret = 0;
5266 : : /* djb-rwth: removing redundant code */
5267 : :
5268 : : if (!inf_norm_at)
5269 : : return ret;
5270 : : /* -- already added in FillOutCompositeCanonInfAtom() --
5271 : : if ( bIsotopic ) {
5272 : : for ( i = 0, j = 0; i < NUM_H_ISOTOPES; i ++ ) {
5273 : : if ( pINChI_Aux->nNumRemovedIsotopicH[i] ) {
5274 : : inf_norm_at_data->num_iso_H[i] += pINChI_Aux->nNumRemovedIsotopicH[i];
5275 : : inf_norm_at_data->num_removed_iso_H ++;
5276 : : }
5277 : : }
5278 : : }
5279 : : */
5280 : :
5281 : : if (bIsotopic && !(pINChI->nNumberOfIsotopicAtoms || pINChI->nNumberOfIsotopicTGroups ||
5282 : : (pINChI->nPossibleLocationsOfIsotopicH && pINChI->nPossibleLocationsOfIsotopicH[0] > 1))) /* djb-rwth: addressing LLVM warning */
5283 : : {
5284 : : bIsotopic = 0;
5285 : : }
5286 : :
5287 : : Stereo = bIsotopic ? pINChI->StereoIsotopic : pINChI->Stereo;
5288 : :
5289 : : bDoDisplaySp3 = (NULL != Stereo) && (Stereo->nNumberOfStereoCenters > 0);
5290 : :
5291 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
5292 : : if (bDoDisplaySp3 && bRelRac && Stereo->nNumberOfStereoCenters < 2 &&
5293 : : (Stereo->nCompInv2Abs || ATOM_PARITY_ILL_DEF(Stereo->t_parity[0])))
5294 : : {
5295 : : bDoDisplaySp3 = 0;
5296 : : if (Stereo->nCompInv2Abs)
5297 : : {
5298 : : inf_norm_at_data->StereoFlags |= bRel ? INF_STEREO_REL : bRac ? INF_STEREO_RAC : 0;
5299 : : }
5300 : : }
5301 : : #endif
5302 : : /* flag has stereo */
5303 : : if ((NULL != Stereo) && (bDoDisplaySp3 || Stereo->nNumberOfStereoBonds > 0))
5304 : : {
5305 : : (*pStereoFlags) |= INF_STEREO;
5306 : : }
5307 : :
5308 : : /*
5309 : : if ( bDoDisplaySp3 && bRelRac && Stereo->nCompInv2Abs && Stereo->nNumberOfStereoCenters < 2 ) {
5310 : : bDoDisplaySp3 = 0;
5311 : : }
5312 : : */
5313 : : if (bDoDisplaySp3 && Stereo->nCompInv2Abs)
5314 : : {
5315 : : /* inversion changes stereo */
5316 : : if (bRel)
5317 : : {
5318 : : (*pStereoFlags) |= INF_STEREO_REL;
5319 : : }
5320 : : else
5321 : : {
5322 : : if (bRac)
5323 : : {
5324 : : (*pStereoFlags) |= INF_STEREO_RAC;
5325 : : }
5326 : : else
5327 : : {
5328 : : (*pStereoFlags) |= INF_STEREO_ABS;
5329 : : }
5330 : : }
5331 : : if (bRelRac)
5332 : : {
5333 : : (*pStereoFlags) |= (Stereo->nCompInv2Abs > 0) ? INF_STEREO_NORM : INF_STEREO_INV;
5334 : : }
5335 : : }
5336 : : if (bDoDisplaySp3 && Stereo->nCompInv2Abs < 0 && !bRelRac)
5337 : : {
5338 : : /* display Inv stereo which is Absolute Stereo */
5339 : : nNumber = Stereo->nNumberInv;
5340 : : t_parity = Stereo->t_parityInv;
5341 : : nOrigAtNosInCanonOrd = bIsotopic ? pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv :
5342 : : pINChI_Aux->nOrigAtNosInCanonOrdInv;
5343 : : }
5344 : : else
5345 : : {
5346 : : /* display Output stereo which is Absolute Stereo */
5347 : : if (bDoDisplaySp3)
5348 : : {
5349 : : nNumber = Stereo->nNumber;
5350 : : t_parity = Stereo->t_parity;
5351 : : }
5352 : : nOrigAtNosInCanonOrd = bIsotopic
5353 : : ? pINChI_Aux->nIsotopicOrigAtNosInCanonOrd
5354 : : : pINChI_Aux->nOrigAtNosInCanonOrd;
5355 : : }
5356 : :
5357 : : nConstitEquNumbers = bIsotopic ? pINChI_Aux->nConstitEquIsotopicNumbers :
5358 : : pINChI_Aux->nConstitEquNumbers;
5359 : : nConstitEquTGroupNumbers = bIsotopic ? pINChI_Aux->nConstitEquIsotopicTGroupNumbers :
5360 : : pINChI_Aux->nConstitEquTGroupNumbers;
5361 : : memset(inf_norm_at, 0, num_at * sizeof(inf_norm_at[0])); /* djb-rwth: memset_s C11/Annex K variant? */
5362 : : if (num_H > 0)
5363 : : {
5364 : : memset(inf_norm_at_H, 0, num_H * sizeof(inf_norm_at[0])); /* djb-rwth: memset_s C11/Annex K variant? */
5365 : : }
5366 : :
5367 : : /* obtain norm_at[] atom numbers (from zero) in order of canonical numbers */
5368 : : nNormAtNosInCanonOrd = (AT_NUMB*)inchi_calloc(num_at, sizeof(nNormAtNosInCanonOrd[0]));
5369 : : if ((ret = GetAtomOrdNbrInCanonOrd(pCG, norm_at, nNormAtNosInCanonOrd, nOrigAtNosInCanonOrd, num_at))) /* djb-rwth: addressing LLVM warning */
5370 : : {
5371 : : goto exit_function;
5372 : : }
5373 : :
5374 : : /* atom canonical and equivalence numbers > 0 */
5375 : : for (i = 0; i < num_at; i++)
5376 : : {
5377 : : j = (int)nNormAtNosInCanonOrd[i];
5378 : : if (j < 0 || j >= num_at)
5379 : : {
5380 : : continue;
5381 : : }
5382 : : inf_norm_at[j].nCanonNbr = (AT_NUMB)(i + 1);
5383 : : inf_norm_at[j].nCanonEquNbr = nConstitEquNumbers[i];
5384 : : #ifdef DISPLAY_DEBUG_DATA
5385 : : inf_norm_at[j].nDebugData = 0;
5386 : : #if ( DISPLAY_DEBUG_DATA == DISPLAY_DEBUG_DATA_C_POINT )
5387 : : inf_norm_at[j].nDebugData = norm_at[j].c_point;
5388 : : #endif
5389 : : #endif
5390 : : }
5391 : : /* tautomeric groups */
5392 : : if (nNumberOfTGroups)
5393 : : {
5394 : : /*
5395 : : : start from 1: bypass number of t-groups
5396 : : : j is a counter within the current t-group
5397 : : : g is a tautomeric group canonical number
5398 : : : e is a tautomeric group equivalence
5399 : : */
5400 : : for (g = 1, i = 1; g <= nNumberOfTGroups; g++)
5401 : : {
5402 : : n = (int)pINChI->nTautomer[i] - INCHI_T_NUM_MOVABLE; /* number of atoms in t-group */
5403 : : e = nConstitEquTGroupNumbers[(int)g - 1];
5404 : : /* bypass number of hydrogen atoms, negative charges, ... */
5405 : : for (i += INCHI_T_NUM_MOVABLE + 1, j = 0; j < n && i < pINChI->lenTautomer; j++, i++)
5406 : : {
5407 : : /* scan canonical numbers of atoms within the atom t-group */
5408 : : k = (int)nNormAtNosInCanonOrd[(int)pINChI->nTautomer[i] - 1];
5409 : : inf_norm_at[k].nTautGroupCanonNbr = g;
5410 : : inf_norm_at[k].nTautGroupEquNbr = e;
5411 : : }
5412 : : }
5413 : : if (i != pINChI->lenTautomer || g != nNumberOfTGroups + 1)
5414 : : {
5415 : : ret = CT_TAUCOUNT_ERR; /* <BRKPT> */
5416 : : goto exit_function;
5417 : : }
5418 : : }
5419 : : /* atoms that may exchange isotopic H */
5420 : : if (bIsotopic && pINChI->nPossibleLocationsOfIsotopicH && (n = (int)pINChI->nPossibleLocationsOfIsotopicH[0]))
5421 : : {
5422 : : for (i = 1; i < n; i++)
5423 : : {
5424 : : j = (int)pINChI->nPossibleLocationsOfIsotopicH[i];
5425 : : k = (int)nNormAtNosInCanonOrd[j - 1];
5426 : : if (!inf_norm_at[k].nTautGroupCanonNbr)
5427 : : {
5428 : : inf_norm_at[k].cFlags |= AT_FLAG_ISO_H_POINT;
5429 : : }
5430 : : }
5431 : : }
5432 : : #if ( DISPLAY_RING_SYSTEMS == 1 )
5433 : : /* debug only */
5434 : : for (j = 0; j < num_at; j++)
5435 : : {
5436 : : inf_norm_at[j].nCanonNbr = norm_at[j].nBlockSystem;
5437 : : inf_norm_at[j].nCanonEquNbr = norm_at[j].nRingSystem;
5438 : : #if ( USE_DISTANCES_FOR_RANKING == 1 )
5439 : : inf_norm_at[j].nTautGroupCanonNbr = norm_at[j].nDistanceFromTerminal;
5440 : : inf_norm_at[j].nTautGroupEquNbr = norm_at[j].bCutVertex;
5441 : : #else
5442 : : inf_norm_at[j].nTautGroupCanonNbr = norm_at[j].bCutVertex;
5443 : : inf_norm_at[j].nTautGroupEquNbr = 0;
5444 : : #endif
5445 : : }
5446 : : #endif
5447 : :
5448 : : /* Fill inf_at[i] field 'at_string':
5449 : : write isotopic mass, chemical element symbols and hydrogens, charge, radical, canon.numbers
5450 : : */
5451 : : len_str = sizeof(inf_norm_at[0].at_string);
5452 : : for (i = 0; i < init_num_at; i++)
5453 : : {
5454 : : inf_ATOM* cur_inf_norm_at = (i < num_at) ? inf_norm_at + i : inf_norm_at_H + i - num_at;
5455 : : inp_ATOM* cur_norm_at = (i < num_at) ? norm_at + i : norm_at_H + i - num_at;
5456 : : str = cur_inf_norm_at->at_string;
5457 : : len = 0;
5458 : : bIncludeIsotopicH = bIsotopic && (i >= num_at || (!inf_norm_at[i].nTautGroupCanonNbr && !(inf_norm_at[i].cFlags & AT_FLAG_ISO_H_POINT))); /* djb-rwth: addressing LLVM warning */
5459 : : /* isotopic mass */
5460 : : atw = 0;
5461 : : if (cur_norm_at) /* djb-rwth: fixing a NULL pointer dereference */
5462 : : {
5463 : : if (cur_norm_at->iso_atw_diff && bIsotopic)
5464 : : {
5465 : : if (cur_norm_at->at_type == ATT_PROTON)
5466 : : {
5467 : : ; /* do not set isotopic mass of a tautomeric proton */
5468 : : }
5469 : : else
5470 : : {
5471 : : if (num_at <= i && cur_norm_at->el_number == PERIODIC_NUMBER_H && cur_norm_at->chem_bonds_valence == 1 &&
5472 : : !cur_norm_at->charge && !cur_norm_at->radical && !cur_norm_at->num_H &&
5473 : : 0 <= (j = (int)cur_norm_at->neighbor[0] - offset) && j < num_at &&
5474 : : (inf_norm_at[j].nTautGroupCanonNbr || (inf_norm_at[j].cFlags & AT_FLAG_ISO_H_POINT)))
5475 : : {
5476 : : ; /* do not set isotopic mass of an exchangeable proton */
5477 : : }
5478 : : else
5479 : : {
5480 : : atw = get_atomic_mass(cur_norm_at->elname);
5481 : : atw += (cur_norm_at->iso_atw_diff > 0) ? cur_norm_at->iso_atw_diff - 1 : cur_norm_at->iso_atw_diff;
5482 : : /*len += sprintf( str+len, "^%d", atw );*/
5483 : : }
5484 : : }
5485 : : }
5486 : : /* element name */
5487 : : if (cur_norm_at->el_number == PERIODIC_NUMBER_H && 2 <= atw && atw <= 3)
5488 : : {
5489 : : len += sprintf(str + len, "%s", atw == 2 ? "D" : "T");
5490 : : }
5491 : : else
5492 : : {
5493 : : if (atw)
5494 : : {
5495 : : len += sprintf(str + len, "^%d", atw);
5496 : : }
5497 : : /*if (strcmp( cur_norm_at->elname, "Zz" ))*/
5498 : : if (strcmp(cur_norm_at->elname, "Zz") && strcmp(cur_norm_at->elname, "Zy"))
5499 : : {
5500 : : len += sprintf(str + len, "%s", cur_norm_at->elname);
5501 : : }
5502 : : else /* always show "Zy" as "Zz" */
5503 : : {
5504 : : #if ( DISPLAY_ZZ_AS_STAR == 1 )
5505 : : len += sprintf(str + len, "*");
5506 : : #else
5507 : : len += sprintf(str + len, "Zz");
5508 : : #endif
5509 : : }
5510 : : }
5511 : :
5512 : : /* hydrogens */
5513 : : /* find number of previuosly removed terminal hydrogen atoms */
5514 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
5515 : : {
5516 : : num_iso_H[j] = cur_norm_at->num_iso_H[j];
5517 : : }
5518 : : for (j = 0, n = (int)cur_norm_at->num_H; j < num_H; j++)
5519 : : {
5520 : : /* subtract number of removed terminal */
5521 : : /* H atoms from the total number of H atoms */
5522 : : if (i == (int)norm_at_H[j].neighbor[0] - offset)
5523 : : {
5524 : : n -= 1;
5525 : : m = (int)norm_at_H[j].iso_atw_diff - 1;
5526 : : if (0 <= m && m < NUM_H_ISOTOPES)
5527 : : {
5528 : : /* subtract number of removed terminal isotopic */
5529 : : /* H atoms from the total number of isotopic H atoms */
5530 : : num_iso_H[m] -= 1;
5531 : : }
5532 : : }
5533 : : }
5534 : : if (bIncludeIsotopicH)
5535 : : {
5536 : : /* subtract number of isotopic H atoms from the total number of H atoms */
5537 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
5538 : : {
5539 : : n -= num_iso_H[j];
5540 : : }
5541 : : }
5542 : : /* non-isotopic hydrogen atoms */
5543 : : if (n > 1)
5544 : : {
5545 : : len += sprintf(str + len, "H%d", n);
5546 : : }
5547 : : else
5548 : : {
5549 : : if (n == 1)
5550 : : {
5551 : : len += sprintf(str + len, "H");
5552 : : }
5553 : : }
5554 : :
5555 : : /* isotopic hydrogen atoms */
5556 : : if (bIncludeIsotopicH)
5557 : : {
5558 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
5559 : : {
5560 : : if (num_iso_H[j])
5561 : : {
5562 : : if (j == 0 || (j != 1 && j != 2)) /* djb-rwth: addressing LLVM warning */
5563 : : {
5564 : : len += sprintf(str + len, "^%dH", j + 1);
5565 : : }
5566 : : else
5567 : : {
5568 : : len += sprintf(str + len, j == 1 ? "D" : "T");
5569 : : }
5570 : : if (num_iso_H[j] != 1)
5571 : : {
5572 : : len += sprintf(str + len, "%d", (int)num_iso_H[j]);
5573 : : }
5574 : : }
5575 : : }
5576 : : }
5577 : : if (cur_norm_at->el_number == PERIODIC_NUMBER_H && str[0] == str[1])
5578 : : {
5579 : : char* q;
5580 : : if (!str[2])
5581 : : {
5582 : : str[1] = '2'; /* quick fix: replace HH with H2 */
5583 : : }
5584 : : else
5585 : : {
5586 : : if (isdigit(UCINT str[2]) && (n = strtol(str + 2, &q, 10)) && !q[0])
5587 : : {
5588 : : len = 1 + sprintf(str + 1, "%d", n + 1);
5589 : : }
5590 : : }
5591 : : }
5592 : : /* charge */
5593 : : if (abs(cur_norm_at->charge) > 1)
5594 : : {
5595 : : len += sprintf(str + len, "%+d", cur_norm_at->charge);
5596 : : }
5597 : : else
5598 : : {
5599 : : if (abs(cur_norm_at->charge) == 1)
5600 : : {
5601 : : len += sprintf(str + len, "%s", cur_norm_at->charge > 0 ? "+" : "-");
5602 : : }
5603 : : }
5604 : : /* radical */
5605 : : if (cur_norm_at->radical)
5606 : : {
5607 : : len += sprintf(str + len, "%s", cur_norm_at->radical == RADICAL_SINGLET ? ":" :
5608 : : cur_norm_at->radical == RADICAL_DOUBLET ? "." :
5609 : : cur_norm_at->radical == RADICAL_TRIPLET ? ".." : "?");
5610 : : }
5611 : : }
5612 : : }
5613 : :
5614 : : /* Stereogenic centers */
5615 : : if (bDoDisplaySp3 && Stereo && 0 < (num_stereo = Stereo->nNumberOfStereoCenters))
5616 : : {
5617 : : for (i = 0; i < num_stereo; i++)
5618 : : {
5619 : : j = (int)nNormAtNosInCanonOrd[(int)nNumber[i] - 1];
5620 : : c = t_parity[i];
5621 : : c = c == 1 ? '-' : c == 2 ? '+' : c == 3 ? 'u' : c == 4 ? '?' : '*';
5622 : : inf_norm_at[j].cStereoCenterParity = c;
5623 : : str = inf_norm_at[j].at_string;
5624 : : len = (int)strlen(str);
5625 : : if (len + 3 < (int)sizeof(inf_norm_at[0].at_string))
5626 : : {
5627 : : str[len++] = '(';
5628 : : str[len++] = inf_norm_at[j].cStereoCenterParity;
5629 : : str[len++] = ')';
5630 : : str[len] = '\0';
5631 : : /* mark ambuguous stereo center */
5632 : : if (norm_at[j].bAmbiguousStereo && (c == '+' || c == '-' || c == '?') && str[0] != '!' &&
5633 : : len + 1 < (int)sizeof(inf_norm_at[0].at_string))
5634 : : {
5635 : : memmove(str + 1, str, (long long)len + 1); /* djb-rwth: cast operator added */
5636 : : str[0] = '!'; /* output the atom in red color */
5637 : : }
5638 : : }
5639 : : }
5640 : : }
5641 : :
5642 : : /* Stereogenic bonds */
5643 : : /* (cumulenes with odd number of double bonds are stereocenters, */
5644 : : /* and atom parity should be set) */
5645 : : if (Stereo && 0 < (num_stereo = Stereo->nNumberOfStereoBonds))
5646 : : {
5647 : : for (i = 0; i < num_stereo; i++)
5648 : : {
5649 : : int start_at, bAmbiguousStereoBond = 0; /* djb-rwth: removing redundant variable */
5650 : : j = (int)nNormAtNosInCanonOrd[(int)Stereo->nBondAtom1[i] - 1];
5651 : : k = (int)nNormAtNosInCanonOrd[(int)Stereo->nBondAtom2[i] - 1];
5652 : : start_at = j;
5653 : : c = Stereo->b_parity[i];
5654 : :
5655 : : c = c == 1 ? '-' : c == 2 ? '+' : c == 3 ? 'u' : c == 4 ? '?' : '*';
5656 : :
5657 : : /* mark ambuguous stereo bond atom(s) */
5658 : : if (norm_at[j].bAmbiguousStereo && (c == '+' || c == '-') &&
5659 : : (len = strlen(str = inf_norm_at[j].at_string) + 1) < (int)sizeof(inf_norm_at[0].at_string) &&
5660 : : str[0] != '!')
5661 : : {
5662 : : memmove(str + 1, str, len);
5663 : : str[0] = '!'; /* output the atom in red color */
5664 : : bAmbiguousStereoBond++;
5665 : : }
5666 : : if (norm_at[k].bAmbiguousStereo && (c == '+' || c == '-') &&
5667 : : (len = strlen(str = inf_norm_at[k].at_string) + 1) < (int)sizeof(inf_norm_at[0].at_string) &&
5668 : : str[0] != '!')
5669 : : {
5670 : : memmove(str + 1, str, len);
5671 : : str[0] = '!'; /* output the atom in red color */
5672 : : bAmbiguousStereoBond++;
5673 : : }
5674 : :
5675 : : /* find the opposite atom k. */
5676 : : /* Note: since it may be a cumulene, find the shortest(best) path */
5677 : : /* to atom number k to avoid confusion in case of, for example, */
5678 : : /* 4-member aromatic rings. */
5679 : : best_len = MAX_CUMULENE_LEN + 1; /* moved here from inside the cycle 1-8-2003 */
5680 : : for (n = 0; n < norm_at[j].valence; n++)
5681 : : {
5682 : : if (norm_at[j].bond_type[n] == BOND_SINGLE)
5683 : : {
5684 : : /* single bond cannot be stereogenic. */
5685 : : continue;
5686 : : }
5687 : : /* best_len = MAX_CUMULENE_LEN+1; */
5688 : : len = 0; /* number of bonds in cumulene - 1 */
5689 : : next_atom[len] = (int)norm_at[j].neighbor[n] - offset;
5690 : : next_neigh[len] = n;
5691 : : cur_atom = j;
5692 : : while (next_atom[len] != k && len < MAX_CUMULENE_LEN && 2 == norm_at[next_atom[len]].valence)
5693 : : {
5694 : : next_neigh[len + 1] = ((int)norm_at[next_atom[len]].neighbor[0] - offset == cur_atom);
5695 : : next_atom[len + 1] = (int)norm_at[next_atom[len]].neighbor[next_neigh[len + 1]] - offset;
5696 : : cur_atom = next_atom[len];
5697 : : len++;
5698 : : }
5699 : : if (next_atom[len] == k)
5700 : : {
5701 : : if (len < best_len)
5702 : : {
5703 : : memcpy(best_next_neigh, next_neigh, sizeof(best_next_neigh));
5704 : : memcpy(best_next_atom, next_atom, sizeof(best_next_atom));
5705 : : best_len = len;
5706 : : /* djb-rwth: removing redundant code */
5707 : : if (len == 0)
5708 : : {
5709 : : break; /* path length cannot be smaller than 1 */
5710 : : }
5711 : : }
5712 : : /* djb-rwth: removing redundant code */
5713 : : }
5714 : : }
5715 : : if (best_len <= MAX_CUMULENE_LEN && best_next_atom[best_len] == k)
5716 : : {
5717 : : /* djb-rwth: removing redundant code */
5718 : : if (best_len % 2)
5719 : : {
5720 : : /* even number of bonds: chiral atom, draw parity on the cenrtal atom */
5721 : : j = best_next_atom[best_len / 2];
5722 : : inf_norm_at[j].cStereoCenterParity = c;
5723 : : str = inf_norm_at[j].at_string;
5724 : : len = (int)strlen(str);
5725 : : if (len + 3 < (int)sizeof(inf_norm_at[0].at_string))
5726 : : {
5727 : : str[len++] = '(';
5728 : : str[len++] = inf_norm_at[j].cStereoCenterParity;
5729 : : str[len++] = ')';
5730 : : str[len] = '\0';
5731 : : }
5732 : : }
5733 : : else
5734 : : {
5735 : : /* odd number of bonds: draw parity on the central bond */
5736 : : if (best_len == 0)
5737 : : {
5738 : : /* double bond */
5739 : : j = start_at;
5740 : : k = best_next_neigh[0];
5741 : : }
5742 : : else
5743 : : {
5744 : : /* cumulene */
5745 : : best_len = best_len / 2 - 1;
5746 : : j = best_next_atom[best_len];
5747 : : k = best_next_neigh[best_len + 1]; /* added +1 to display cumulene parity on the middle bond (6-24-2002) */
5748 : : }
5749 : : /* mark "forward" bond */
5750 : : for (m = 0; m < MAX_STEREO_BONDS && inf_norm_at[j].cStereoBondParity[m]; m++)
5751 : : ;
5752 : : if (m < MAX_STEREO_BONDS)
5753 : : {
5754 : : inf_norm_at[j].cStereoBondParity[m] = c;
5755 : : inf_norm_at[j].cStereoBondNumber[m] = k;
5756 : : inf_norm_at[j].cStereoBondWarning[m] = bAmbiguousStereoBond;
5757 : : }
5758 : : /* djb-rwth: removing redundant code */
5759 : : /* mark "backward" bond */
5760 : : n = norm_at[j].neighbor[k] - offset;
5761 : : for (k = 0; k < norm_at[n].valence && j != (int)norm_at[n].neighbor[k] - offset; k++)
5762 : : {
5763 : : ;
5764 : : }
5765 : : if (k < norm_at[n].valence)
5766 : : {
5767 : : j = n;
5768 : : for (m = 0; m < MAX_STEREO_BONDS && inf_norm_at[j].cStereoBondParity[m]; m++)
5769 : : {
5770 : : ;
5771 : : }
5772 : : if (m < MAX_STEREO_BONDS)
5773 : : {
5774 : : inf_norm_at[j].cStereoBondParity[m] = c;
5775 : : inf_norm_at[j].cStereoBondNumber[m] = k;
5776 : : inf_norm_at[j].cStereoBondWarning[m] = bAmbiguousStereoBond;
5777 : : }
5778 : : /* djb-rwth: removing redundant code */
5779 : : }
5780 : : /* djb-rwth: removing redundant code */
5781 : : }
5782 : : }
5783 : : /* djb-rwth: removing redundant code */
5784 : : }
5785 : : }
5786 : :
5787 : : for (i = 0; i < num_at; i++)
5788 : : {
5789 : : /* canonical numbers */
5790 : : if (inf_norm_at[i].nCanonNbr)
5791 : : {
5792 : : str = inf_norm_at[i].at_string;
5793 : : len = (int)strlen(str);
5794 : : #define SHOW_GLOBAL_CANO_NUMBERING 1
5795 : : #if SHOW_GLOBAL_CANO_NUMBERING==1
5796 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nCanonNbr); // -offset );
5797 : : #else
5798 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nCanonNbr);
5799 : : #endif
5800 : : if (inf_norm_at[i].nCanonEquNbr || inf_norm_at[i].nTautGroupCanonNbr || (inf_norm_at[i].cFlags & AT_FLAG_ISO_H_POINT))
5801 : : {
5802 : : if (inf_norm_at[i].nCanonEquNbr)
5803 : : {
5804 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nCanonEquNbr);
5805 : : }
5806 : : else
5807 : : {
5808 : : if (len + 1 < len_str)
5809 : : {
5810 : : len += 1;
5811 : : strcat(str, "/");
5812 : : }
5813 : : }
5814 : : }
5815 : : /* tautomeric groups */
5816 : : if (inf_norm_at[i].nTautGroupCanonNbr)
5817 : : {
5818 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nTautGroupCanonNbr);
5819 : : if (inf_norm_at[i].nTautGroupEquNbr)
5820 : : {
5821 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_norm_at[i].nTautGroupEquNbr);
5822 : : }
5823 : : }
5824 : : if ((inf_norm_at[i].cFlags & AT_FLAG_ISO_H_POINT) && len + 2 <= len_str)
5825 : : {
5826 : : str[len++] = '/';
5827 : : str[len++] = '*';
5828 : : str[len] = '\0';
5829 : : }
5830 : : #ifdef DISPLAY_DEBUG_DATA
5831 : : if (inf_norm_at[i].nDebugData)
5832 : : {
5833 : : len += (*MakeNumber)(str + len, len_str - len, "`", (int)inf_norm_at[i].nDebugData);
5834 : : }
5835 : : #endif
5836 : : }
5837 : : }
5838 : :
5839 : : exit_function:
5840 : :
5841 : : if (nNormAtNosInCanonOrd)
5842 : : {
5843 : : inchi_free(nNormAtNosInCanonOrd);
5844 : : }
5845 : :
5846 : : return ret;
5847 : : }
5848 : :
5849 : :
5850 : : /****************************************************************************/
5851 : : int FillOutInputInfAtom(inp_ATOM* inp_at,
5852 : : INF_ATOM_DATA* inf_at_data,
5853 : : int init_num_at,
5854 : : int num_removed_H,
5855 : : int bAdd_DT_to_num_H,
5856 : : int nNumRemovedProtons,
5857 : : NUM_H* nNumRemovedProtonsIsotopic,
5858 : : int bIsotopic,
5859 : : int bAbcNumbers) /* djb-rwth: this whole function seems to be useless as it returns the value of a function argument -- init_num_at */
5860 : : {
5861 : : int i, j, m, n, ret, len_str, len, atw;
5862 : : int num_iso_H[NUM_H_ISOTOPES];
5863 : : char* str;
5864 : : int num_at = init_num_at - num_removed_H;
5865 : : int(*MakeNumber)(char*, int, const char*, int) = MakeDecNumber;
5866 : :
5867 : : inf_ATOM* inf_at = inf_at_data ? inf_at_data->at : NULL;
5868 : :
5869 : : ret = 0;
5870 : :
5871 : : if (!inf_at)
5872 : : {
5873 : : return ret;
5874 : : }
5875 : :
5876 : : memset(inf_at, 0, init_num_at * sizeof(inf_at[0])); /* djb-rwth: memset_s C11/Annex K variant? */
5877 : :
5878 : : inf_at_data->nNumRemovedProtons = nNumRemovedProtons;
5879 : : MakeRemovedProtonsString(nNumRemovedProtons, nNumRemovedProtonsIsotopic, NULL, bIsotopic, inf_at_data->szRemovedProtons, NULL);
5880 : : /* atom canonical and equivalence numbers > 0 */
5881 : : for (i = 0; i < num_at; i++)
5882 : : {
5883 : : #if ( DISPLAY_ORIG_AT_NUMBERS == 1 )
5884 : : inf_at[i].nCanonNbr = inp_at[i].orig_at_number;
5885 : : #else
5886 : : inf_at[i].nCanonNbr = (AT_NUMB)(i + 1);
5887 : : #endif
5888 : : }
5889 : : /* Fill inf_at[i] field 'at_string':
5890 : : write isotopic mass, chemical element symbols and hydrogens, charge, radical, canon. numbers
5891 : : */
5892 : : len_str = sizeof(inf_at[0].at_string);
5893 : : for (i = 0; i < init_num_at; i++)
5894 : : {
5895 : : str = inf_at[i].at_string;
5896 : : len = 0;
5897 : : /* isotopic mass */
5898 : : atw = 0;
5899 : : if (inp_at[i].iso_atw_diff && bIsotopic)
5900 : : {
5901 : : atw = get_atomic_mass(inp_at[i].elname);
5902 : : atw += (inp_at[i].iso_atw_diff > 0) ? inp_at[i].iso_atw_diff - 1 : inp_at[i].iso_atw_diff;
5903 : : /*len += sprintf( str+len, "^%d", atw );*/
5904 : : }
5905 : : /* element name */
5906 : : if (inp_at[i].el_number == PERIODIC_NUMBER_H && 2 <= atw && atw <= 3)
5907 : : {
5908 : : len += sprintf(str + len, "%s", atw == 2 ? "D" : "T");
5909 : : }
5910 : : else
5911 : : {
5912 : : if (atw)
5913 : : {
5914 : : len += sprintf(str + len, "^%d", atw);
5915 : : }
5916 : : /*if (strcmp( inp_at[i].elname, "Zz" ))*/
5917 : : if (strcmp(inp_at[i].elname, "Zz") && strcmp(inp_at[i].elname, "Zy"))
5918 : : {
5919 : : len += sprintf(str + len, "%s", inp_at[i].elname);
5920 : : }
5921 : : else /* always show "Zy" as "Zz" */
5922 : : {
5923 : : #if ( DISPLAY_ZZ_AS_STAR == 1 )
5924 : : len += sprintf(str + len, "*");
5925 : : #else
5926 : : len += sprintf(str + len, "Zz");
5927 : : #endif
5928 : : }
5929 : : }
5930 : :
5931 : : /* hydrogens */
5932 : : /* find number of previuosly removed terminal hydrogen atoms */
5933 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
5934 : : {
5935 : : num_iso_H[j] = inp_at[i].num_iso_H[j];
5936 : : }
5937 : : for (j = num_at, n = (int)inp_at[i].num_H; j < init_num_at; j++)
5938 : : {
5939 : : /* subtract number of removed terminal */
5940 : : /* H atoms from the total number of H atoms */
5941 : : if (i == (int)inp_at[j].neighbor[0])
5942 : : {
5943 : : n -= 1;
5944 : : m = (int)inp_at[j].iso_atw_diff - 1;
5945 : : if (0 <= m && m < NUM_H_ISOTOPES)
5946 : : {
5947 : : /* subtract number of removed terminal isotopic */
5948 : : /* H atoms from the total number of isotopic H atoms */
5949 : : num_iso_H[m] -= 1;
5950 : : }
5951 : : }
5952 : : }
5953 : : if (bIsotopic && !bAdd_DT_to_num_H)
5954 : : {
5955 : : /* subtract number of isotopic H atoms from the total number of H atoms */
5956 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
5957 : : {
5958 : : n -= num_iso_H[j];
5959 : : }
5960 : : }
5961 : : /* non-isotopic hydrogen atoms */
5962 : : if (n > 1)
5963 : : {
5964 : : len += sprintf(str + len, "H%d", n);
5965 : : }
5966 : : else
5967 : : {
5968 : : if (n == 1)
5969 : : {
5970 : : len += sprintf(str + len, "H"); /* fixed 12-21-2002: removed 3rd argument */
5971 : : }
5972 : : }
5973 : : if (bIsotopic)
5974 : : {
5975 : : /* isotopic hydrogen atoms */
5976 : : for (j = 0; j < NUM_H_ISOTOPES; j++)
5977 : : {
5978 : : if (num_iso_H[j])
5979 : : {
5980 : : if (j == 0 || (j != 1 && j != 2))
5981 : : {
5982 : : len += sprintf(str + len, "^%dH", j + 1);
5983 : : }
5984 : : else
5985 : : {
5986 : : len += sprintf(str + len, j == 1 ? "D" : "T");
5987 : : }
5988 : : if (num_iso_H[j] != 1)
5989 : : {
5990 : : len += sprintf(str + len, "%d", (int)num_iso_H[j]);
5991 : : }
5992 : : }
5993 : : }
5994 : : }
5995 : : if (inp_at[i].el_number == PERIODIC_NUMBER_H && str[0] == str[1])
5996 : : {
5997 : : char* q;
5998 : : if (!str[2])
5999 : : {
6000 : : str[1] = '2'; /* quick fix: replace HH with H2 */
6001 : : }
6002 : : else
6003 : : {
6004 : : if (isdigit(UCINT str[2]) && (n = strtol(str + 2, &q, 10)) && !q[0])
6005 : : {
6006 : : len = 1 + sprintf(str + 1, "%d", n + 1);
6007 : : }
6008 : : }
6009 : : }
6010 : : /*
6011 : : if ( str[0] == 'H' && str[1] == 'H' && !str[2] ) {
6012 : : str[1] = '2';
6013 : : }
6014 : : */
6015 : : /* charge */
6016 : : if (abs(inp_at[i].charge) > 1)
6017 : : {
6018 : : len += sprintf(str + len, "%+d", inp_at[i].charge);
6019 : : }
6020 : : else
6021 : : {
6022 : : if (abs(inp_at[i].charge) == 1)
6023 : : {
6024 : : len += sprintf(str + len, "%s", inp_at[i].charge > 0 ? "+" : "-");
6025 : : }
6026 : : }
6027 : :
6028 : : /* radical */
6029 : : if (inp_at[i].radical)
6030 : : {
6031 : : len += sprintf(str + len, "%s", inp_at[i].radical == RADICAL_SINGLET ? ":" :
6032 : : inp_at[i].radical == RADICAL_DOUBLET ? "." :
6033 : : inp_at[i].radical == RADICAL_TRIPLET ? ".." : "?");
6034 : : }
6035 : : }
6036 : :
6037 : : for (i = 0; i < init_num_at; i++)
6038 : : {
6039 : : /* canonical numbers */
6040 : : if (inf_at[i].nCanonNbr)
6041 : : {
6042 : : str = inf_at[i].at_string;
6043 : : len = (int)strlen(str);
6044 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_at[i].nCanonNbr);
6045 : : if (inf_at[i].nCanonEquNbr || inf_at[i].nTautGroupCanonNbr)
6046 : : {
6047 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_at[i].nCanonEquNbr);
6048 : : }
6049 : : /* tautomeric groups */
6050 : : if (inf_at[i].nTautGroupCanonNbr)
6051 : : {
6052 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_at[i].nTautGroupCanonNbr);
6053 : : if (inf_at[i].nTautGroupEquNbr)
6054 : : {
6055 : : len += (*MakeNumber)(str + len, len_str - len, "/", (int)inf_at[i].nTautGroupEquNbr); /* djb-rwth: ignoring LLVM warning: variable used? */
6056 : : }
6057 : : }
6058 : : }
6059 : : }
6060 : : ret = init_num_at;
6061 : :
6062 : : return ret;
6063 : : }
6064 : :
6065 : :
6066 : : /****************************************************************************/
6067 : : int FillOutInfAtom(struct tagCANON_GLOBALS* pCG,
6068 : : inp_ATOM* norm_at,
6069 : : INF_ATOM_DATA* inf_norm_at_data,
6070 : : int init_num_at,
6071 : : int num_removed_H,
6072 : : int bAdd_DT_to_num_H,
6073 : : int nNumRemovedProtons,
6074 : : NUM_H* nNumRemovedProtonsIsotopic,
6075 : : int bIsotopic,
6076 : : INChI* pINChI,
6077 : : INChI_Aux* pINChI_Aux,
6078 : : int bAbcNumbers,
6079 : : INCHI_MODE nMode)
6080 : : {
6081 : : if (norm_at && inf_norm_at_data && inf_norm_at_data->at)
6082 : : {
6083 : : if (pINChI && pINChI_Aux)
6084 : : {
6085 : : return FillOutCanonInfAtom(pCG, norm_at, inf_norm_at_data, init_num_at, bIsotopic, pINChI,
6086 : : pINChI_Aux, bAbcNumbers, nMode);
6087 : : }
6088 : : else
6089 : : {
6090 : : return FillOutInputInfAtom(norm_at, inf_norm_at_data, init_num_at, num_removed_H, bAdd_DT_to_num_H,
6091 : : nNumRemovedProtons, nNumRemovedProtonsIsotopic, bIsotopic, bAbcNumbers);
6092 : : }
6093 : : }
6094 : :
6095 : : return 0;
6096 : : }
6097 : :
6098 : :
6099 : : /****************************************************************************/
6100 : : int FillOutCompositeCanonInfAtom(struct tagCANON_GLOBALS* pCG,
6101 : : COMP_ATOM_DATA* composite_norm_data,
6102 : : INF_ATOM_DATA* inf_norm_at_data,
6103 : : int bIsotopic, int bTautomeric,
6104 : : PINChI2* pINChI2,
6105 : : PINChI_Aux2* pINChI_Aux2,
6106 : : int bAbcNumbers,
6107 : : INCHI_MODE nMode)
6108 : : {
6109 : : int i, num_components, j, k, ret;
6110 : : inp_ATOM* inp_norm_at;
6111 : : INChI* pINChI;
6112 : : INChI_Aux* pINChI_Aux;
6113 : : int num_inp_at, num_at, num_H, offset, offset_H, next_offset, next_offset_H;
6114 : :
6115 : : if (composite_norm_data && inf_norm_at_data && (bTautomeric == TAUT_INI || (pINChI2 && pINChI_Aux2))) /* djb-rwth: addressing LLVM warning */
6116 : : {
6117 : : composite_norm_data += bTautomeric;
6118 : : inp_norm_at = composite_norm_data->at;
6119 : : num_components = composite_norm_data->num_components;
6120 : : offset = 0;
6121 : : offset_H = composite_norm_data->num_at - composite_norm_data->num_removed_H;
6122 : : if (bTautomeric == TAUT_INI)
6123 : : {
6124 : : ret = FillOutInputInfAtom(composite_norm_data->at, inf_norm_at_data, composite_norm_data->num_at,
6125 : : composite_norm_data->num_removed_H, 0 /*bAdd_DT_to_num_H*/,
6126 : : composite_norm_data->nNumRemovedProtons,
6127 : : composite_norm_data->nNumRemovedProtonsIsotopic,
6128 : : bIsotopic, bAbcNumbers);
6129 : : return ret;
6130 : : }
6131 : : else
6132 : : {
6133 : : for (i = 0; i < num_components; i++)
6134 : : {
6135 : : j = inchi_min(bTautomeric, TAUT_YES);
6136 : : /* count isotopic H on removed atoms -- isolated H(+) cations */
6137 : : inf_norm_at_data->nNumRemovedProtons += pINChI_Aux2[i][j]->nNumRemovedProtons;
6138 : : if (bIsotopic && bTautomeric == TAUT_YES)
6139 : : {
6140 : : for (k = 0; k < NUM_H_ISOTOPES; k++)
6141 : : {
6142 : : if (pINChI_Aux2[i][j]->nNumRemovedIsotopicH[k])
6143 : : {
6144 : : inf_norm_at_data->num_iso_H[k] += pINChI_Aux2[i][j]->nNumRemovedIsotopicH[k];
6145 : : inf_norm_at_data->num_removed_iso_H += pINChI_Aux2[i][j]->nNumRemovedIsotopicH[k];
6146 : : }
6147 : : }
6148 : : }
6149 : : /* ignore deleted components */
6150 : : if (pINChI2[i][j] && pINChI2[i][j]->bDeleted)
6151 : : {
6152 : : continue;
6153 : : }
6154 : : if (!pINChI2[i][j] || !pINChI2[i][j]->nNumberOfAtoms)
6155 : : {
6156 : : j = ALT_TAUT(j);
6157 : : if (!pINChI2[i][j] || !pINChI2[i][j]->nNumberOfAtoms)
6158 : : {
6159 : : continue; /* error ??? */
6160 : : }
6161 : : }
6162 : : pINChI = pINChI2[i][j];
6163 : : pINChI_Aux = pINChI_Aux2[i][j];
6164 : : next_offset = composite_norm_data->nOffsetAtAndH[2 * i];
6165 : : next_offset_H = composite_norm_data->nOffsetAtAndH[2 * i + 1];
6166 : : num_at = next_offset - offset;
6167 : : if (num_at <= 0)
6168 : : continue;
6169 : : num_H = next_offset_H - offset_H;
6170 : : num_inp_at = num_at + num_H;
6171 : : if (num_at != pINChI->nNumberOfAtoms || num_at != pINChI_Aux->nNumberOfAtoms)
6172 : : {
6173 : : return 0; /* error */
6174 : : }
6175 : : ret = FillOutOneCanonInfAtom(pCG,
6176 : : inp_norm_at, inf_norm_at_data,
6177 : : inf_norm_at_data->pStereoFlags + i + 1,
6178 : : num_inp_at,
6179 : : offset, offset_H, bIsotopic,
6180 : : pINChI, pINChI_Aux, bAbcNumbers, nMode);
6181 : : if (ret)
6182 : : return 0; /* error */
6183 : :
6184 : : inf_norm_at_data->StereoFlags |= inf_norm_at_data->pStereoFlags[i + 1];
6185 : : offset = next_offset;
6186 : : offset_H = next_offset_H;
6187 : : }
6188 : : }
6189 : : MakeRemovedProtonsString(inf_norm_at_data->nNumRemovedProtons, inf_norm_at_data->num_iso_H, NULL, bIsotopic,
6190 : : inf_norm_at_data->szRemovedProtons, &inf_norm_at_data->num_removed_iso_H);
6191 : : }
6192 : :
6193 : : return 1;
6194 : : }
6195 : : #endif /* } ifndef COMPILE_ANSI_ONLY */
6196 : :
6197 : :
6198 : : /****************************************************************************/
6199 : 161 : int CheckCanonNumberingCorrectness(int num_atoms,
6200 : : int num_at_tg,
6201 : : sp_ATOM* at,
6202 : : CANON_STAT* pCS,
6203 : : CANON_GLOBALS* pCG,
6204 : : int bTautomeric,
6205 : : char* pStrErrStruct)
6206 : : {
6207 : 161 : int i, ret = 0;
6208 : 161 : AT_NUMB* pCanonOrd = NULL;
6209 : 161 : int nErrorCode = 0;
6210 : : AT_NUMB* pCanonRank; /* canonical ranks of the atoms or tautomeric groups */
6211 : 161 : AT_NUMB* pCanonRankAtoms = NULL;
6212 : :
6213 : : /* djb-rwth: removing redundant code */
6214 : :
6215 : 161 : pCanonRankAtoms = (AT_NUMB*)inchi_calloc((long long)num_at_tg + 1, sizeof(pCanonRankAtoms[0])); /* djb-rwth: cast operator added */
6216 : :
6217 : : /*
6218 : : Non-isotopic part
6219 : : */
6220 : :
6221 [ + + ]: 264 : pCanonOrd = pCS->nLenCanonOrdStereo > 0 ? pCS->nCanonOrdStereo :
6222 [ + - ]: 103 : pCS->nLenCanonOrd > 0 ? pCS->nCanonOrd : NULL;
6223 : 161 : pCanonRank = pCanonRankAtoms;
6224 [ + - + - ]: 161 : if (pCanonOrd && pCanonRank)
6225 : : {
6226 [ + + ]: 983 : for (i = 0; i < num_at_tg; i++)
6227 : : {
6228 : 822 : pCanonRank[pCanonOrd[i]] = (AT_NUMB)(i + 1);
6229 : : }
6230 : 161 : ret = UpdateFullLinearCT(num_atoms, num_at_tg, at, pCanonRank, pCanonOrd, pCS,
6231 : : pCG,
6232 : : 0);
6233 [ - + ]: 161 : if (ret /*|| memcmp(pCS->LinearCT, pCS->LinearCT2, sizeof(AT_RANK) * pCS->nLenLinearCT )*/)
6234 : : {
6235 : 0 : nErrorCode |= WARN_FAILED_STEREO;
6236 : : }
6237 : : }
6238 : : else
6239 : : {
6240 : 0 : nErrorCode |= ERR_NO_CANON_RESULTS;
6241 : 0 : goto exit_function;
6242 : : }
6243 : :
6244 : : /*
6245 : : Isotopic part
6246 : : */
6247 : :
6248 [ - + ]: 322 : pCanonOrd = pCS->nLenCanonOrdIsotopicStereo > 0 ? pCS->nCanonOrdIsotopicStereo :
6249 [ - + ]: 161 : pCS->nLenCanonOrdIsotopic > 0 ? pCS->nCanonOrdIsotopic : NULL;
6250 : 161 : pCanonRank = pCanonRankAtoms;
6251 : :
6252 [ + - - - ]: 161 : if (pCanonOrd && pCanonRank)
6253 : : {
6254 [ # # ]: 0 : for (i = 0; i < num_at_tg; i++)
6255 : : {
6256 : 0 : pCanonRank[pCanonOrd[i]] = (AT_NUMB)(i + 1);
6257 : : }
6258 : 0 : ret = UpdateFullLinearCT(num_atoms, num_at_tg, at, pCanonRank, pCanonOrd, pCS,
6259 : : pCG,
6260 : : 0);
6261 [ # # ]: 0 : if (ret /*|| memcmp(pCS->LinearCT, pCS->LinearCT2, sizeof(AT_RANK) * pCS->nLenLinearCT )*/)
6262 : : {
6263 [ # # ]: 0 : nErrorCode |= (pCS->nLenCanonOrdIsotopicStereo ? WARN_FAILED_ISOTOPIC_STEREO : WARN_FAILED_ISOTOPIC);
6264 : : }
6265 : : }
6266 : :
6267 : 161 : exit_function:
6268 [ + - ]: 161 : if (pCanonRankAtoms)
6269 : : {
6270 [ + - ]: 161 : inchi_free(pCanonRankAtoms);
6271 : : }
6272 : :
6273 [ - + ]: 161 : if (nErrorCode)
6274 : : {
6275 : 0 : return CT_CANON_ERR;
6276 : : }
6277 : :
6278 : 161 : return 0;
6279 : : }
|