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