Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : : #include <string.h>
8 : :
9 : : #include "mode.h"
10 : : #include "ichimain.h"
11 : : #include "ichi_io.h"
12 : :
13 : : #include "bcf_s.h"
14 : :
15 : : #include "logging.h" /*(@nnuk : Nauman Ullah Khan) :: Needed for logging functionality*/
16 : :
17 : : /* Local */
18 : :
19 : : int GetHillFormulaCounts( U_CHAR *nAtom,
20 : : S_CHAR *nNum_H,
21 : : int num_atoms,
22 : : AT_NUMB *nTautomer,
23 : : int lenTautomer,
24 : : int *pnum_C,
25 : : int *pnum_H,
26 : : int *pnLen,
27 : : int *pnNumNonHAtoms );
28 : : int MakeHillFormula( U_CHAR *nAtom,
29 : : int num_atoms,
30 : : char *szLinearCT,
31 : : int nLen_szLinearCT,
32 : : int num_C,
33 : : int num_H,
34 : : int *bOverflow );
35 : :
36 : : #if ( FIX_DALKE_BUGS == 1 )
37 : : #else
38 : : char *AllocateAndFillHillFormula( INChI *pINChI );
39 : : #endif
40 : :
41 : : int AddElementAndCount( const char *szElement,
42 : : int mult, char *szLinearCT,
43 : : int nLenLinearCT,
44 : : int *bOverflow );
45 : :
46 : : int Copy2StereoBondOrAllene( INChI_Stereo *Stereo,
47 : : int *nNumberOfStereoCenters,
48 : : int *nNumberOfStereoBonds,
49 : : AT_STEREO_DBLE *LinearCTStereoDble,
50 : : AT_NUMB *pCanonOrd, AT_RANK *pCanonRank,
51 : : sp_ATOM *at,
52 : : int bIsotopic );
53 : :
54 : : int CopyLinearCTStereoToINChIStereo( INChI_Stereo *Stereo,
55 : : AT_STEREO_CARB *LinearCTStereoCarb,
56 : : int nLenLinearCTStereoCarb,
57 : : AT_STEREO_DBLE *LinearCTStereoDble,
58 : : int nLenLinearCTStereoDble,
59 : : AT_NUMB *pCanonOrd, AT_RANK *pCanonRank,
60 : : sp_ATOM *at,
61 : : int bIsotopic,
62 : : AT_STEREO_CARB *LinearCTStereoCarbInv,
63 : : AT_STEREO_DBLE *LinearCTStereoDbleInv,
64 : : AT_NUMB *pCanonOrdInv,
65 : : AT_RANK *pCanonRankInv );
66 : :
67 : : int GetHillFormulaIndexLength( int count );
68 : :
69 : : int MarkAmbiguousStereo( sp_ATOM *at,
70 : : inp_ATOM *norm_at,
71 : : int bIsotopic,
72 : : AT_NUMB *pCanonOrd,
73 : : AT_STEREO_CARB *LinearCTStereoCarb, int nLenLinearCTStereoCarb,
74 : : AT_STEREO_DBLE *LinearCTStereoDble, int nLenLinearCTStereoDble );
75 : :
76 : : INCHI_MODE UnmarkAllUndefinedUnknownStereo( INChI_Stereo *Stereo, INCHI_MODE nUserMode );
77 : :
78 : : int CleanCoord( MOL_COORD szCoord, int delim );
79 : :
80 : :
81 : :
82 : : /****************************************************************************
83 : : Print single-component Hill formula whole structure output.
84 : : ****************************************************************************/
85 : 244 : int MakeHillFormulaString( char *szHillFormula,
86 : : INCHI_IOS_STRING *strbuf,
87 : : int *bOverflow )
88 : : {
89 : 244 : int nUsedLength0 = strbuf->nUsedLength;
90 [ + - + - ]: 244 : if (szHillFormula && !*bOverflow)
91 : : {
92 [ - + ]: 244 : if (-1 == inchi_strbuf_printf( strbuf, "%s", szHillFormula ))
93 : : {
94 : 0 : *bOverflow |= 1;
95 : 0 : return nUsedLength0 + 1;
96 : : }
97 : : }
98 : 244 : return ( strbuf->nUsedLength - nUsedLength0 );
99 : : }
100 : :
101 : :
102 : : /****************************************************************************
103 : : MS Windows dependent: sprintf() is supposed to return the length
104 : : of the output string.
105 : : Carbon atoms are always first.
106 : : Bridging hydrogen atoms are always last.
107 : : ****************************************************************************/
108 : 2682 : int GetHillFormulaIndexLength( int count )
109 : : {
110 : : char szCount[16];
111 [ + + ]: 2682 : if (count > 1)
112 : : {
113 : 1368 : return sprintf(szCount, "%d", count);
114 : : }
115 : :
116 : 1314 : return 0;
117 : : }
118 : :
119 : :
120 : : /****************************************************************************
121 : : Estimate dimensions of Hill formula for single component
122 : : ****************************************************************************/
123 : 1286 : int GetHillFormulaCounts( U_CHAR *nAtom,
124 : : S_CHAR *nNum_H,
125 : : int num_atoms,
126 : : AT_NUMB *nTautomer,
127 : : int lenTautomer,
128 : : int *pnum_C,
129 : : int *pnum_H,
130 : : int *pnLen,
131 : : int *pnNumNonHAtoms )
132 : : {
133 : : char szElement[4];
134 : 1286 : U_CHAR nPrevAtom = (U_CHAR) -2;
135 : : int bCarbon, bHydrogen, nElemLen, nFormLen, nNumNonHAtoms;
136 : : int mult, i, num_H, num_C;
137 : :
138 : 1286 : num_H = 0;
139 : 1286 : num_C = 0;
140 : 1286 : bCarbon = 0;
141 : 1286 : bHydrogen = 0;
142 : 1286 : nElemLen = 0;
143 : 1286 : nFormLen = 0;
144 : 1286 : mult = 0;
145 : 1286 : nNumNonHAtoms = num_atoms;
146 : :
147 [ + + ]: 10535 : for (i = 0; i < num_atoms; i++)
148 : : {
149 [ + + ]: 9249 : if (nPrevAtom != nAtom[i])
150 : : {
151 [ + + ]: 1496 : if (mult)
152 : : {
153 [ - + ]: 210 : if (bHydrogen)
154 : : {
155 : 0 : num_H += mult;
156 : : }
157 [ + + ]: 210 : else if (bCarbon)
158 : : {
159 : 130 : num_C += mult;
160 : : }
161 : : else
162 : : {
163 : 80 : nFormLen += nElemLen;
164 : 80 : nFormLen += GetHillFormulaIndexLength( mult );
165 : : }
166 : : }
167 : : /*if (-1 == get_element_chemical_symbol( (int) nAtom[i], szElement ))*/
168 [ - + ]: 1496 : if (-1 == get_element_or_pseudoelement_symbol( (int) nAtom[i], szElement ))
169 : : {
170 : 0 : return -1;
171 : : }
172 : 1496 : mult = 1;
173 : 1496 : nElemLen = (int) strlen( szElement );
174 : 1496 : nPrevAtom = nAtom[i];
175 : 1496 : bCarbon = !strcmp( szElement, "C" );
176 : 1496 : bHydrogen = !strcmp( szElement, "H" );
177 [ + + ]: 1496 : if (bHydrogen)
178 : : {
179 : 8 : nNumNonHAtoms = i;
180 : : }
181 : : }
182 : : else
183 : : {
184 : 7753 : mult++;
185 : : }
186 : :
187 : 9249 : num_H += nNum_H[i];
188 : : }
189 : :
190 : : /* NumGroups; ((NumAt+1, NumH, At1..AtNumAt),...) */
191 : :
192 [ + + + + ]: 1286 : if (nTautomer && lenTautomer > 0)
193 : : {
194 : 21 : int num_groups = nTautomer[0];
195 : :
196 [ + + + - ]: 34 : for (i = 1; i < lenTautomer && num_groups > 0; i += nTautomer[i] + 1, num_groups--)
197 : : {
198 : 13 : num_H += nTautomer[i + 1];
199 : : }
200 : : }
201 : :
202 [ + - ]: 1286 : if (mult)
203 : : {
204 [ + + ]: 1286 : if (bHydrogen)
205 : : {
206 : 8 : num_H += mult;
207 : : }
208 [ + + ]: 1278 : else if (bCarbon)
209 : : {
210 : 1021 : num_C += mult;
211 : : }
212 : : else
213 : : {
214 : 257 : nFormLen += nElemLen;
215 : 257 : nFormLen += GetHillFormulaIndexLength( mult );
216 : : }
217 : : }
218 : :
219 [ + + ]: 1286 : if (num_C)
220 : : {
221 : 1151 : nFormLen += (int) strlen( "C" );
222 : 1151 : nFormLen += GetHillFormulaIndexLength( num_C );
223 : : }
224 : :
225 [ + + ]: 1286 : if (num_H)
226 : : {
227 : 1194 : nFormLen += (int) strlen( "H" );
228 : 1194 : nFormLen += GetHillFormulaIndexLength( num_H );
229 : : }
230 : :
231 : 1286 : *pnum_C = num_C;
232 : 1286 : *pnum_H = num_H;
233 : 1286 : *pnLen = nFormLen;
234 : 1286 : *pnNumNonHAtoms = nNumNonHAtoms;
235 : :
236 : 1286 : return 0;
237 : : }
238 : :
239 : :
240 : : /****************************************************************************
241 : : Add a portion to Hill formula for single component.
242 : : ****************************************************************************/
243 : 2682 : int AddElementAndCount( const char *szElement, int mult, char *szLinearCT, int nLenLinearCT, int *bOverflow )
244 : : {
245 : : char szMult[16];
246 : : int len1, len2;
247 [ + - + - : 2682 : if (mult > 0 && !*bOverflow && 0 < ( len1 = strlen( szElement ) ))
+ - ]
248 : : {
249 [ + + ]: 2682 : if (mult > 1)
250 : : {
251 : 1368 : len2 = sprintf(szMult, "%d", mult);
252 : : }
253 : : else
254 : : {
255 : 1314 : len2 = 0;
256 : 1314 : szMult[0] = '\0';
257 : : }
258 [ + - ]: 2682 : if (len1 + len2 < nLenLinearCT)
259 : : {
260 : 2682 : memcpy(szLinearCT, szElement, len1);
261 : 2682 : memcpy(szLinearCT + len1, szMult, (long long)len2 + 1); /* adding zero termination */ /* djb-rwth: added cast operator */
262 : 2682 : return len1 + len2;
263 : : }
264 : : else
265 : : {
266 : 0 : ( *bOverflow )++;
267 : : }
268 : : }
269 : :
270 : 0 : return 0;
271 : : }
272 : :
273 : :
274 : : /****************************************************************************
275 : : Make Hill formula for single component.
276 : :
277 : : If num_C > 0 then nAtom does not contain C or H
278 : : otherwise all elements are in alphabetic order
279 : : ****************************************************************************/
280 : 1286 : int MakeHillFormula( U_CHAR *nAtom,
281 : : int num_atoms,
282 : : char *szLinearCT,
283 : : int nLen_szLinearCT,
284 : : int num_C,
285 : : int num_H,
286 : : int *bOverflow )
287 : : {
288 : : char szElement[4];
289 : : int mult, compare2H;
290 : : int i, nLen, bOvfl;
291 : : U_CHAR nPrevAtom;
292 : :
293 : 1286 : nLen = 0;
294 : 1286 : mult = 0;
295 : 1286 : bOvfl = 0;
296 : 1286 : nPrevAtom = (U_CHAR) -2; /* non-existent number */
297 : 1286 : memset(szElement, '\0', sizeof(szElement)); /* djb-rwth: fixing coverity ID #499542 */
298 : :
299 [ + + ]: 1286 : if (num_C)
300 : : {
301 : 1151 : nLen += AddElementAndCount( "C", num_C, szLinearCT + nLen, nLen_szLinearCT - nLen, &bOvfl );
302 [ + - ]: 1151 : if (num_H)
303 : : {
304 : 1151 : nLen += AddElementAndCount( "H", num_H, szLinearCT + nLen, nLen_szLinearCT - nLen, &bOvfl );
305 : 1151 : num_H = 0;
306 : : }
307 : : }
308 : :
309 [ + + ]: 1806 : for (i = 0; i < num_atoms; i++)
310 : : {
311 : :
312 [ + + ]: 520 : if (nPrevAtom != nAtom[i])
313 : : {
314 [ + + ]: 337 : if (mult)
315 : : {
316 : 76 : nLen += AddElementAndCount( szElement, mult, szLinearCT + nLen, nLen_szLinearCT - nLen, &bOvfl );
317 : : }
318 : 337 : mult = 1;
319 : : /*if (-1 == get_element_chemical_symbol( (int) nAtom[i], szElement ))*/
320 [ - + ]: 337 : if (-1 == get_element_or_pseudoelement_symbol( (int) nAtom[i], szElement ))
321 : : {
322 : 0 : return -1; /* wrong element */
323 : : }
324 : :
325 : 337 : nPrevAtom = nAtom[i];
326 [ - + ]: 337 : if (!strcmp( "C", szElement ))
327 : : {
328 : 0 : return -1;
329 : : }
330 : 337 : compare2H = strcmp( "H", szElement );
331 [ - + ]: 337 : if (!compare2H)
332 : : {
333 : 0 : return -1;
334 : : }
335 [ + + + + ]: 337 : if (compare2H < 0 && num_H)
336 : : {
337 : : /* H-atom should be located in front of szElement */
338 : 25 : nLen += AddElementAndCount( "H", num_H, szLinearCT + nLen, nLen_szLinearCT - nLen, &bOvfl );
339 : 25 : num_H = 0;
340 : : }
341 : : }
342 : : else
343 : : {
344 : 183 : mult++;
345 : : }
346 : : }
347 [ + + ]: 1286 : if (mult)
348 : : {
349 : : /* the last element if any */
350 : 261 : nLen += AddElementAndCount( szElement, mult, szLinearCT + nLen, nLen_szLinearCT - nLen, &bOvfl );
351 : : }
352 [ + + ]: 1286 : if (num_H)
353 : : {
354 : : /* if H has not been output... */
355 : 18 : nLen += AddElementAndCount( "H", num_H, szLinearCT + nLen, nLen_szLinearCT - nLen, &bOvfl );
356 : : }
357 : 1286 : *bOverflow |= ( 0 != bOvfl );
358 : :
359 [ - + ]: 1286 : return bOvfl ? nLen_szLinearCT + 1 : nLen;
360 : : }
361 : :
362 : :
363 : : /****************************************************************************
364 : : Allocate space and produce Hill formula for single component
365 : : ****************************************************************************/
366 : 1286 : char *AllocateAndFillHillFormula( INChI *pINChI )
367 : : {
368 : : int num_C, num_H, nLen, nNumNonHAtoms, ret, bOverflow;
369 : 1286 : char *pHillFormula = NULL;
370 : :
371 : 1286 : bOverflow = 0;
372 : :
373 [ + - ]: 1286 : if (!GetHillFormulaCounts( pINChI->nAtom,
374 : : pINChI->nNum_H,
375 : : pINChI->nNumberOfAtoms,
376 : : pINChI->nTautomer,
377 : : pINChI->lenTautomer,
378 : : &num_C, &num_H,
379 : : &nLen,
380 : : &nNumNonHAtoms ))
381 : : {
382 : :
383 : : #if ( FIX_GAF_2019_2==1 )
384 : 1286 : pHillFormula = (char*)inchi_calloc((long long )nLen + 1, sizeof(char)); /* djb-rwth: cast operator added */
385 : : #else
386 : : pHillFormula = (char*)inchi_malloc(nLen + 1);
387 : : #endif
388 : :
389 [ + - ]: 1286 : if (pHillFormula)
390 : : {
391 : :
392 : 1286 : ret = MakeHillFormula( pINChI->nAtom + num_C,
393 : : nNumNonHAtoms - num_C,
394 : : pHillFormula,
395 : : nLen + 1,
396 : : num_C, num_H,
397 : : &bOverflow );
398 : :
399 [ + - - + ]: 1286 : if (ret != nLen || bOverflow)
400 : : {
401 [ # # ]: 0 : inchi_free( pHillFormula );
402 : 0 : pHillFormula = NULL;
403 : : }
404 : : }
405 : : }
406 : :
407 : 1286 : return pHillFormula;
408 : : }
409 : :
410 : :
411 : : /************************************************************************************/
412 : : /* Return value: 0 => copied to stereo bonds; 1=> Allene copied to stereocenters */
413 : : /* on input nNumberOfStereoBonds==NULL means second call, use Stereo->...Inv */
414 : : /************************************************************************************/
415 : 0 : int Copy2StereoBondOrAllene( INChI_Stereo *Stereo,
416 : : int *nNumberOfStereoCenters,
417 : : int *nNumberOfStereoBonds,
418 : : AT_STEREO_DBLE *LinearCTStereoDble,
419 : : AT_NUMB *pCanonOrd,
420 : : AT_RANK *pCanonRank,
421 : : sp_ATOM *at,
422 : : int bIsotopic )
423 : : {
424 : : int cumulene_len,
425 : : j,
426 : 0 : next_j = 0 /* ordering number of the central allene atom */,
427 : : next_neigh;
428 : : AT_RANK
429 : : at_num;
430 : : int parity;
431 : :
432 [ # # # # ]: 0 : if (pCanonOrd && pCanonRank)
433 : : {
434 : 0 : j = pCanonOrd[(int) LinearCTStereoDble->at_num1 - 1];
435 : :
436 : : /* if allene then find the central atom, at[next_j] */
437 : :
438 [ # # ]: 0 : if (bIsotopic)
439 : : {
440 : 0 : cumulene_len = BOND_CHAIN_LEN( at[j].stereo_bond_parity2[0] );
441 : :
442 [ # # # # ]: 0 : if (cumulene_len % 2 && ( 1 >= MAX_NUM_STEREO_BONDS || !at[j].stereo_bond_neighbor2[1] ))
443 : : {
444 : 0 : next_j = at[j].neighbor[(int) at[j].stereo_bond_ord2[0]];
445 : :
446 [ # # # # ]: 0 : for (cumulene_len = ( cumulene_len - 1 ) / 2; cumulene_len && 2 == at[next_j].valence; cumulene_len--)
447 : : {
448 : 0 : next_neigh = ( j == at[next_j].neighbor[0] );
449 : 0 : j = next_j;
450 : 0 : next_j = at[next_j].neighbor[next_neigh];
451 : : }
452 : : /* next_j is the central atom */
453 : : }
454 : : else
455 : : {
456 : 0 : cumulene_len = -1; /* not an allene */
457 : : }
458 : : }
459 : : else
460 : : {
461 : 0 : cumulene_len = BOND_CHAIN_LEN( at[j].stereo_bond_parity[0] );
462 : :
463 [ # # # # ]: 0 : if (cumulene_len % 2 && ( 1 >= MAX_NUM_STEREO_BONDS || !at[j].stereo_bond_neighbor[1] ))
464 : : {
465 : 0 : next_j = at[j].neighbor[(int) at[j].stereo_bond_ord[0]];
466 : :
467 [ # # # # ]: 0 : for (cumulene_len = ( cumulene_len - 1 ) / 2; cumulene_len && 2 == at[next_j].valence; cumulene_len--)
468 : : {
469 : 0 : next_neigh = ( j == at[next_j].neighbor[0] );
470 : 0 : j = next_j;
471 : 0 : next_j = at[next_j].neighbor[next_neigh];
472 : : }
473 : : }
474 : : else
475 : : {
476 : 0 : cumulene_len = -1; /* not an allene */
477 : : }
478 : : }
479 : :
480 [ # # ]: 0 : if (!cumulene_len)
481 : : {
482 : : /* allene has been found; insert new stereocenter and parity */
483 : :
484 : : AT_NUMB *nNumber;
485 : : S_CHAR *t_parity;
486 : :
487 [ # # ]: 0 : nNumber = nNumberOfStereoBonds ? Stereo->nNumber : Stereo->nNumberInv;
488 [ # # ]: 0 : t_parity = nNumberOfStereoBonds ? Stereo->t_parity : Stereo->t_parityInv;
489 : :
490 : 0 : at_num = pCanonRank[next_j];
491 : 0 : parity = LinearCTStereoDble->parity;
492 : :
493 : : /* free room for the new stereocenter */
494 [ # # # # ]: 0 : for (j = 0; j < *nNumberOfStereoCenters && Stereo->nNumber[j] < at_num; j++)
495 : : {
496 : : ;
497 : : }
498 : :
499 [ # # ]: 0 : if (j < *nNumberOfStereoCenters)
500 : : {
501 : 0 : memmove(nNumber + j + 1, nNumber + j, (*nNumberOfStereoCenters - (long long)j) * sizeof(nNumber[0])); /* djb-rwth: cast operator added */
502 : 0 : memmove(t_parity + j + 1, t_parity + j, (*nNumberOfStereoCenters - (long long)j) * sizeof(t_parity[0])); /* djb-rwth: cast operator added */
503 : : }
504 : :
505 : : /* fill the new stereo center info */
506 : :
507 : 0 : nNumber[j] = at_num;
508 : 0 : t_parity[j] = parity;
509 : 0 : ( *nNumberOfStereoCenters )++;
510 : 0 : return 1;
511 : : }
512 : : }
513 : :
514 : : /* Save the stereo bond info */
515 : :
516 [ # # ]: 0 : if (nNumberOfStereoBonds)
517 : : {
518 : 0 : j = *nNumberOfStereoBonds;
519 : 0 : Stereo->b_parity[j] = LinearCTStereoDble->parity;
520 : 0 : Stereo->nBondAtom1[j] = LinearCTStereoDble->at_num1;
521 : 0 : Stereo->nBondAtom2[j] = LinearCTStereoDble->at_num2;
522 : 0 : ( *nNumberOfStereoBonds )++;
523 : : }
524 : :
525 : 0 : return 0;
526 : : }
527 : :
528 : :
529 : : /***************************************************************************/
530 : 75 : int CopyLinearCTStereoToINChIStereo( INChI_Stereo *Stereo,
531 : : AT_STEREO_CARB *LinearCTStereoCarb,
532 : : int nLenLinearCTStereoCarb,
533 : : AT_STEREO_DBLE *LinearCTStereoDble,
534 : : int nLenLinearCTStereoDble,
535 : : AT_NUMB *pCanonOrd, AT_RANK *pCanonRank,
536 : : sp_ATOM *at,
537 : : int bIsotopic,
538 : : AT_STEREO_CARB *LinearCTStereoCarbInv,
539 : : AT_STEREO_DBLE *LinearCTStereoDbleInv,
540 : : AT_NUMB *pCanonOrdInv, AT_RANK *pCanonRankInv )
541 : : {
542 : 75 : int n, i, nErrorCode = 0, len;
543 : : int bAllene;
544 : : int diff;
545 : : int lenInv, bAlleneInv;
546 : :
547 : : /* Stereo centers */
548 : :
549 : : /* djb-rwth: fixing oss-fuzz issue #68271 */
550 [ + - ]: 75 : if (Stereo)
551 : : {
552 : 75 : n = Stereo->nNumberOfStereoCenters = nLenLinearCTStereoCarb;
553 : :
554 [ + + ]: 289 : for (i = 0; i < n; i++)
555 : : {
556 [ + - ]: 214 : if (LinearCTStereoCarb) /* djb-rwth: fixing a NULL pointer dereference */
557 : : {
558 : 214 : Stereo->nNumber[i] = LinearCTStereoCarb[i].at_num;
559 : 214 : Stereo->t_parity[i] = LinearCTStereoCarb[i].parity;
560 : : }
561 [ + - ]: 214 : if (LinearCTStereoCarbInv) /* djb-rwth: fixing a NULL pointer dereference */
562 : : {
563 : 214 : Stereo->nNumberInv[i] = LinearCTStereoCarbInv[i].at_num;
564 : 214 : Stereo->t_parityInv[i] = LinearCTStereoCarbInv[i].parity;
565 : : }
566 : : }
567 : :
568 : : /* Stereo bonds */
569 : :
570 : 75 : n = nLenLinearCTStereoDble;
571 : 75 : lenInv = Stereo->nNumberOfStereoCenters;
572 : :
573 [ - + ]: 75 : for (i = len = 0; i < n; i++)
574 : : {
575 : : bAllene =
576 : 0 : Copy2StereoBondOrAllene(Stereo,
577 : : &Stereo->nNumberOfStereoCenters,
578 : : &len,
579 : 0 : LinearCTStereoDble + i,
580 : : pCanonOrd, pCanonRank,
581 : : at,
582 : : bIsotopic);
583 : :
584 : : bAlleneInv =
585 : 0 : Copy2StereoBondOrAllene(Stereo,
586 : : &lenInv,
587 : : NULL,
588 : 0 : LinearCTStereoDbleInv + i,
589 : : pCanonOrdInv, pCanonRankInv,
590 : : at,
591 : : bIsotopic);
592 : :
593 : : /* make sure double bond stereo is identical in original and inverted geometry */
594 : : /* Note: all allenes are AFTER double bonds in LinearCTStereoDble... */
595 [ # # # # : 0 : if (bAllene != bAlleneInv || (!bAllene &&
# # ]
596 : 0 : CompareLinCtStereoDble(LinearCTStereoDble + i, 1,
597 : 0 : LinearCTStereoDbleInv + i, 1))) /* djb-rwth: addressing LLVM warning */
598 : : {
599 : : /* double bond stereo Inv is NOT identical to Abs */
600 : 0 : nErrorCode = -4;
601 : 0 : goto exit_function;
602 : : }
603 : : }
604 : :
605 : 75 : Stereo->nNumberOfStereoBonds = len;
606 : :
607 [ - + ]: 75 : if (lenInv != Stereo->nNumberOfStereoCenters)
608 : : {
609 : 0 : nErrorCode = -5; /* different number of stereo centers in Abs and Inv */
610 : 0 : goto exit_function;
611 : : }
612 : :
613 : :
614 : : /* compare inverted stereocenters to absolute */
615 : :
616 : 75 : n = Stereo->nNumberOfStereoCenters;
617 : : /* djb-rwth: removing redundant code */
618 : :
619 [ + + ]: 94 : for (i = 0, diff = 0; i < n; i++)
620 : : {
621 [ - + ]: 83 : if (Stereo->nNumberInv[i] != Stereo->nNumber[i])
622 : : {
623 [ # # ]: 0 : diff = (Stereo->nNumberInv[i] > Stereo->nNumber[i]) ? 2 : -2;
624 : 0 : break; /* Abs != Inv */
625 : : }
626 [ + + ]: 83 : if (Stereo->t_parityInv[i] != Stereo->t_parity[i])
627 : : {
628 [ + + ]: 64 : diff = (Stereo->t_parityInv[i] > Stereo->t_parity[i]) ? 1 : -1;
629 : 64 : break; /* Abs != Inv */
630 : : }
631 : : }
632 : :
633 : 75 : Stereo->nCompInv2Abs =
634 [ + + + + ]: 75 : (diff > 0) ? 1 : (diff < 0) ? -1 : 0;
635 : :
636 [ + + + + ]: 75 : if (diff == -1 || diff == 1)
637 : : {
638 : : /* The first found difference was in parities */
639 [ + + ]: 242 : for (i = 0, diff = 0; i < n; i++)
640 : : {
641 [ - + ]: 191 : if (Stereo->nNumberInv[i] != Stereo->nNumber[i])
642 : : {
643 : 0 : diff = 2; /* difference in stereo center numbering */
644 : 0 : break;
645 : : }
646 : :
647 : : /* Parities can be only 1, 2, 3, 4. Therefore only mutually inverted pairs
648 : : * (t_parityInv, t_parity) = (1,2) or (2,1) statisfy conditions
649 : : * (t_parityInv != t_parity) && (t_parityInv + t_parity == 3)
650 : : */
651 : :
652 [ + + ]: 191 : if (Stereo->t_parityInv[i] == Stereo->t_parity[i] ||
653 [ - + ]: 178 : Stereo->t_parityInv[i] + Stereo->t_parity[i] != 3)
654 : : {
655 : 13 : diff = 1; /* parities are same or different and cannot be obtained by simple inversion */
656 : 13 : break;
657 : : }
658 : : }
659 : 64 : Stereo->bTrivialInv = !diff;
660 : : }
661 : : else
662 : : {
663 : 11 : Stereo->bTrivialInv = 0;
664 : : }
665 : : }
666 : : else
667 : : {
668 : 0 : nErrorCode = 1;
669 : : }
670 : :
671 : 75 : exit_function:
672 : 75 : return nErrorCode;
673 : : }
674 : :
675 : :
676 : : /****************************************************************************/
677 : 1277 : int MarkAmbiguousStereo( sp_ATOM *at,
678 : : inp_ATOM *norm_at,
679 : : int bIsotopic,
680 : : AT_NUMB *pCanonOrd,
681 : : AT_STEREO_CARB *LinearCTStereoCarb,
682 : : int nLenLinearCTStereoCarb,
683 : : AT_STEREO_DBLE *LinearCTStereoDble,
684 : : int nLenLinearCTStereoDble )
685 : : {
686 : : int n, i, j1, j2, num, mark_atom, mark_bond;
687 : :
688 [ - + ]: 1277 : if (!pCanonOrd)
689 : : {
690 : 0 : return -1;
691 : : }
692 : :
693 : 1277 : num = 0;
694 : :
695 : 1277 : n = nLenLinearCTStereoCarb;
696 : :
697 [ + + ]: 1277 : mark_atom = bIsotopic ? AMBIGUOUS_STEREO_ATOM_ISO : AMBIGUOUS_STEREO_ATOM;
698 : :
699 [ + + ]: 1491 : for (i = 0; i < n; i++)
700 : : {
701 : : /* Mark ambiguous stereo centers (for displaying and "Ambiguous stereo" message) */
702 [ + - + - : 214 : if (ATOM_PARITY_NOT_UNKN( LinearCTStereoCarb[i].parity ) &&
+ - ]
703 [ - + ]: 214 : at[j1 = pCanonOrd[(int) LinearCTStereoCarb[i].at_num - 1]].bAmbiguousStereo)
704 : : {
705 : 0 : at[j1].bAmbiguousStereo |= mark_atom;
706 : 0 : norm_at[j1].bAmbiguousStereo |= mark_atom;
707 : 0 : num++;
708 : : }
709 : : }
710 : :
711 : 1277 : n = nLenLinearCTStereoDble;
712 : :
713 [ + + ]: 1277 : mark_bond = bIsotopic ? AMBIGUOUS_STEREO_BOND_ISO : AMBIGUOUS_STEREO_BOND;
714 : :
715 [ - + ]: 1277 : for (i = 0; i < n; i++)
716 : : {
717 : : /* Mark ambiguous stereo bonds or allenes (for displaying and "Ambiguous stereo" message) */
718 : :
719 [ # # # # ]: 0 : if (ATOM_PARITY_WELL_DEF( LinearCTStereoDble[i].parity ))
720 : : {
721 : 0 : j1 = pCanonOrd[(int) LinearCTStereoDble[i].at_num1 - 1];
722 : 0 : j2 = pCanonOrd[(int) LinearCTStereoDble[i].at_num2 - 1];
723 : :
724 [ # # # # ]: 0 : if (at[j1].bAmbiguousStereo || at[j2].bAmbiguousStereo)
725 : : {
726 : : /* If it is an allene then mark the central atom only
727 : : because the bonds should not be marked to avoid misleading
728 : : message "Ambiguous stereo: bond(s)": Allene makes a stereocenter
729 : : */
730 : :
731 [ # # ]: 0 : int j1_parity = bIsotopic ? at[j1].stereo_bond_parity2[0] : at[j1].stereo_bond_parity[0];
732 : :
733 : 0 : int cumulene_len = BOND_CHAIN_LEN( j1_parity ); /* 0 => double bond, 1 => allene, 2 => cumulene,..*/
734 : :
735 [ # # # # : 0 : if (cumulene_len % 2 && ( 1 >= MAX_NUM_STEREO_BONDS ||
# # ]
736 : 0 : !( bIsotopic ? at[j1].stereo_bond_neighbor2[1] : at[j1].stereo_bond_neighbor[1] ) )
737 : : )
738 : : {
739 : : /* found an allene; locate its central atom */
740 : :
741 : : int next_j, next_neigh;
742 : 0 : int j = j1;
743 : :
744 [ # # ]: 0 : next_j = at[j].neighbor[bIsotopic ? at[j].stereo_bond_ord2[0] : at[j].stereo_bond_ord[0]];
745 : :
746 : 0 : for (cumulene_len = ( cumulene_len - 1 ) / 2;
747 [ # # # # ]: 0 : cumulene_len && 2 == at[next_j].valence;
748 : 0 : cumulene_len--)
749 : : {
750 : 0 : next_neigh = ( j == at[next_j].neighbor[0] );
751 : 0 : j = next_j;
752 : 0 : next_j = at[next_j].neighbor[next_neigh];
753 : : }
754 : : /* next_j is the central atom */
755 [ # # ]: 0 : if (2 == at[next_j].valence)
756 : : {
757 : 0 : at[next_j].bAmbiguousStereo |= mark_atom;
758 : 0 : norm_at[next_j].bAmbiguousStereo |= mark_atom;
759 : 0 : num++;
760 : 0 : continue; /* do not mark the cumulene "bond" endpoints */
761 : : }
762 : : }
763 : :
764 : : /* Not an allene, mark double bond or cumulene end atoms */
765 [ # # ]: 0 : if (at[j1].bAmbiguousStereo)
766 : : {
767 : 0 : at[j1].bAmbiguousStereo |= mark_bond; /* ??? */
768 : 0 : norm_at[j1].bAmbiguousStereo |= mark_bond;
769 : 0 : num++;
770 : : }
771 : :
772 [ # # ]: 0 : if (at[j2].bAmbiguousStereo)
773 : : {
774 : 0 : at[j2].bAmbiguousStereo |= mark_bond; /* ??? */
775 : 0 : norm_at[j2].bAmbiguousStereo |= mark_bond;
776 : 0 : num++;
777 : : }
778 : : }
779 : : }
780 : : }
781 : :
782 : 1277 : return num;
783 : : }
784 : :
785 : :
786 : : /****************************************************************************/
787 : 1277 : INCHI_MODE UnmarkAllUndefinedUnknownStereo( INChI_Stereo *Stereo,
788 : : INCHI_MODE nUserMode )
789 : : {
790 : 1277 : INCHI_MODE nRet = 0;
791 : : int i, n;
792 : :
793 [ + - + - : 1277 : if (!Stereo || (Stereo && !Stereo->nNumberOfStereoCenters && !Stereo->nNumberOfStereoBonds)) /* djb-rwth: addressing LLVM warning */
+ + + - ]
794 : : {
795 : 1202 : return nRet;
796 : : }
797 : :
798 : : /* Stereocenters */
799 [ + + ]: 75 : if (!Stereo->nCompInv2Abs &&
800 [ + - + + ]: 11 : ( n = Stereo->nNumberOfStereoCenters ) > 0 && ( nUserMode & REQ_MODE_SC_IGN_ALL_UU ))
801 : : {
802 : :
803 [ + + - + : 24 : for (i = 0; i < n && !ATOM_PARITY_WELL_DEF( Stereo->t_parity[i] ); i++)
+ + ]
804 : : {
805 : : ;
806 : : }
807 : :
808 [ + + ]: 10 : if (i == n)
809 : : {
810 : 9 : Stereo->nNumberOfStereoCenters = 0;
811 : :
812 [ + + ]: 23 : for (i = 0; i < n; i++)
813 : : {
814 : 14 : Stereo->t_parity[i] = 0;
815 : 14 : Stereo->nNumber[i] = 0;
816 : 14 : Stereo->t_parityInv[i] = 0;
817 : 14 : Stereo->nNumberInv[i] = 0;
818 : : }
819 : :
820 : 9 : nRet |= REQ_MODE_SC_IGN_ALL_UU;
821 : : }
822 : : }
823 : :
824 : : /* Stereobonds */
825 : :
826 [ - + - - ]: 75 : if (( n = Stereo->nNumberOfStereoBonds ) > 0 && ( nUserMode & REQ_MODE_SB_IGN_ALL_UU ))
827 : : {
828 [ # # # # : 0 : for (i = 0; i < n && !ATOM_PARITY_WELL_DEF( Stereo->b_parity[i] ); i++)
# # ]
829 : : {
830 : : ;
831 : : }
832 : :
833 [ # # ]: 0 : if (i == n)
834 : : {
835 : 0 : Stereo->nNumberOfStereoBonds = 0;
836 [ # # ]: 0 : for (i = 0; i < n; i++)
837 : : {
838 : 0 : Stereo->b_parity[i] = 0;
839 : 0 : Stereo->nBondAtom1[i] = 0;
840 : 0 : Stereo->nBondAtom2[i] = 0;
841 : : }
842 : 0 : nRet |= REQ_MODE_SB_IGN_ALL_UU;
843 : : }
844 : : }
845 : :
846 : 75 : return nRet;
847 : : }
848 : :
849 : :
850 : : #if ( defined(TARGET_API_LIB) )
851 : :
852 : :
853 : : /****************************************************************************/
854 : 27 : void WriteCoord( char *str, double x )
855 : : {
856 [ - + ]: 27 : if (x < -9999999.9)
857 : : {
858 : 0 : sprintf(str, "%10.2e", x);
859 : : }
860 : : else
861 : : {
862 [ - + ]: 27 : if (x < -999999.99)
863 : : {
864 : 0 : sprintf( str, "%10.2f", x );
865 : : }
866 : : else
867 : : {
868 [ - + ]: 27 : if (x < -99999.999)
869 : : {
870 : 0 : sprintf( str, "%10.3f", x );
871 : : }
872 : : else
873 : : {
874 [ + - ]: 27 : if (x < 99999.9999)
875 : : {
876 : 27 : sprintf( str, "%10.4f", x );
877 : : }
878 : : else
879 : : {
880 [ # # ]: 0 : if (x < 999999.999)
881 : : {
882 : 0 : sprintf( str, "%10.3f", x );
883 : : }
884 : : else
885 : : {
886 [ # # ]: 0 : if (x < 9999999.99)
887 : : {
888 : 0 : sprintf( str, "%10.2f", x );
889 : : }
890 : : else
891 : : {
892 [ # # ]: 0 : if (x < 99999999.9)
893 : : {
894 : 0 : sprintf( str, "%10.1f", x );
895 : : }
896 : : else
897 : : {
898 : 0 : sprintf( str, "%10.3e", x );
899 : : }
900 : : }
901 : : }
902 : : }
903 : : }
904 : : }
905 : : }
906 : :
907 : 27 : inchi_dot_decimal(str); /* GH #100 */
908 : 27 : }
909 : : #endif
910 : :
911 : :
912 : :
913 : : /* used CANON_STAT members
914 : :
915 : : pCS->LinearCT
916 : : pCS->LinearCTIsotopic
917 : : pCS->LinearCTIsotopicStereoCarb
918 : : pCS->LinearCTIsotopicStereoCarbInv
919 : : pCS->LinearCTIsotopicStereoDble
920 : : pCS->LinearCTIsotopicStereoDbleInv
921 : : pCS->LinearCTIsotopicTautomer
922 : : pCS->LinearCTStereoCarb
923 : : pCS->LinearCTStereoCarbInv
924 : : pCS->LinearCTStereoDble
925 : : pCS->LinearCTStereoDbleInv
926 : : pCS->nCanonOrd
927 : : pCS->nCanonOrdIsotopic
928 : : pCS->nCanonOrdIsotopicStereo
929 : : pCS->nCanonOrdIsotopicStereoInv
930 : : pCS->nCanonOrdIsotopicStereoTaut
931 : : pCS->nCanonOrdIsotopicTaut
932 : : pCS->nCanonOrdStereo
933 : : pCS->nCanonOrdStereoInv
934 : : pCS->nCanonOrdStereoTaut
935 : : pCS->nCanonOrdTaut
936 : : pCS->nLenCanonOrd
937 : : pCS->nLenCanonOrdIsotopic
938 : : pCS->nLenCanonOrdIsotopicStereo
939 : : pCS->nLenCanonOrdIsotopicStereoTaut
940 : : pCS->nLenCanonOrdIsotopicTaut
941 : : pCS->nLenCanonOrdStereo
942 : : pCS->nLenCanonOrdStereoTaut
943 : : pCS->nLenCanonOrdTaut
944 : : pCS->nLenLinearCTAtOnly
945 : : pCS->nLenLinearCTIsotopic
946 : : pCS->nLenLinearCTIsotopicStereoCarb
947 : : pCS->nLenLinearCTIsotopicStereoDble
948 : : pCS->nLenLinearCTIsotopicTautomer
949 : : pCS->nLenLinearCTStereoCarb
950 : : pCS->nLenLinearCTStereoDble
951 : : pCS->nNum_H
952 : : pCS->nNum_H_fixed
953 : : pCS->nSymmRank
954 : : pCS->nSymmRankIsotopic
955 : : pCS->nSymmRankIsotopicTaut
956 : : pCS->nSymmRankTaut
957 : : pCS->t_group_info
958 : : pCS->t_group_info->num_t_groups
959 : :
960 : : */
961 : :
962 : :
963 : : /****************************************************************************
964 : : Serialization: one-component InChI
965 : : ****************************************************************************/
966 : 1265 : int FillOutINChI( INChI *pINChI,
967 : : INChI_Aux *pINChI_Aux,
968 : : int num_atoms,
969 : : int num_at_tg,
970 : : int num_removed_H,
971 : : sp_ATOM *at,
972 : : inp_ATOM *norm_at,
973 : : CANON_STAT *pCS,
974 : : CANON_GLOBALS *pCG,
975 : : int bTautomeric,
976 : : INCHI_MODE nUserMode,
977 : : char *pStrErrStruct,
978 : : int bNoWarnings )
979 : : {
980 : 1265 : int i, j, m, n, g, len, ii, ret = 0;
981 : :
982 : 1265 : AT_NUMB *pSymmRank, *pOrigNosInCanonOrd, *pConstitEquNumb, *pCanonOrd = NULL, *pCanonOrdInv = NULL, *pCanonOrdTaut;
983 : 1265 : T_GROUP_INFO *t_group_info = pCS->t_group_info;
984 : : T_GROUP *t_group;
985 : :
986 : 1265 : int nErrorCode = 0;
987 : : AT_NUMB *pCanonRank, *pCanonRankInv; /* canonical ranks of the atoms or tautomeric groups */
988 : 1265 : AT_NUMB *pCanonRankAtoms = NULL, *pSortOrd = NULL;
989 : : AT_RANK nMinOrd;
990 : :
991 : : INChI_Stereo *Stereo;
992 : 1265 : int bUseNumberingInv = 0, bUseIsotopicNumberingInv = 0;
993 : :
994 : : INCHI_MODE nStereoUnmarkMode;
995 : :
996 : : /*AT_NUMB *pCanonOrdNonIso = NULL, *pCanonOrdIso = NULL;*/
997 : : /*AT_NUMB *nOrigAtNosInCanonOrdNonIso = NULL, *nOrigAtNosInCanonOrdIso = NULL;*/
998 : :
999 : :
1000 : : /* Check for warnings */
1001 [ + - + - ]: 1265 : if (pCS->nLenLinearCTStereoCarb < 0 || pCS->nLenLinearCTStereoDble < 0 ||
1002 [ + - - + ]: 1265 : pCS->nLenCanonOrdStereo < 0 || pCS->nLenCanonOrdStereoTaut < 0)
1003 : : {
1004 : 0 : nErrorCode |= WARN_FAILED_STEREO;
1005 : : }
1006 [ + - + - ]: 1265 : if (pCS->nLenLinearCTIsotopic < 0 || pCS->nLenLinearCTIsotopicTautomer < 0 ||
1007 [ + - - + ]: 1265 : pCS->nLenCanonOrdIsotopic < 0 || pCS->nLenCanonOrdIsotopicTaut < 0)
1008 : : {
1009 : 0 : nErrorCode |= WARN_FAILED_ISOTOPIC;
1010 : : }
1011 [ + - + - ]: 1265 : if (pCS->nLenLinearCTIsotopicStereoCarb < 0 || pCS->nLenLinearCTIsotopicStereoDble < 0 ||
1012 [ + - - + ]: 1265 : pCS->nLenCanonOrdIsotopicStereo < 0 || pCS->nLenCanonOrdIsotopicStereoTaut < 0)
1013 : : {
1014 : 0 : nErrorCode |= WARN_FAILED_ISOTOPIC_STEREO;
1015 : : }
1016 : :
1017 : 1265 : pCanonRankAtoms = (AT_NUMB *) inchi_calloc( (long long)num_at_tg + 1, sizeof( pCanonRankAtoms[0] ) ); /* djb-rwth: cast operator added */
1018 : :
1019 : 1265 : pSortOrd = (AT_NUMB *) inchi_calloc( (long long)num_at_tg + 1, sizeof( pSortOrd[0] ) ); /* must have more than num_atoms */ /* djb-rwth: cast operator added */
1020 : :
1021 [ + - - + ]: 1265 : if (!pCanonRankAtoms || !pSortOrd)
1022 : : {
1023 : 0 : nErrorCode = 0;
1024 : 0 : ret = CT_OUT_OF_RAM; /* <BRKPT> */
1025 : 0 : pINChI->nErrorCode = pINChI_Aux->nErrorCode = CT_OUT_OF_RAM;
1026 : 0 : goto exit_function;
1027 : : }
1028 : :
1029 : : /* Total charge */
1030 : : /* djb-rwth: fixing oss-fuzz issue #69656 */
1031 [ + + ]: 10517 : for (i = 0, n = 0; i < num_atoms + num_removed_H; i++)
1032 : : {
1033 : 9252 : n += at[i].charge;
1034 : : }
1035 : 1265 : pINChI->nTotalCharge = n;
1036 : :
1037 : : /* Number of atoms */
1038 : 1265 : pINChI->nNumberOfAtoms = num_atoms;
1039 : 1265 : pINChI_Aux->nNumberOfAtoms = num_atoms;
1040 : :
1041 : : /* Removed protons and detachable isotopic H */
1042 [ + - + - ]: 1265 : if (bTautomeric && t_group_info)
1043 : : {
1044 : 1265 : pINChI_Aux->nNumRemovedProtons = t_group_info->tni.nNumRemovedProtons;
1045 : :
1046 [ + + ]: 5060 : for (i = 0; i < NUM_H_ISOTOPES; i++)
1047 : : {
1048 : 3795 : pINChI_Aux->nNumRemovedIsotopicH[i] = t_group_info->num_iso_H[i]
1049 : 3795 : + t_group_info->tni.nNumRemovedProtonsIsotopic[i];
1050 : : }
1051 : :
1052 [ - + ]: 1265 : if (pINChI_Aux->bNormalizationFlags & FLAG_FORCE_SALT_TAUT)
1053 : : {
1054 : 0 : pINChI->nFlags |= INCHI_FLAG_HARD_ADD_REM_PROTON;
1055 : : }
1056 [ + + ]: 1265 : if (pINChI_Aux->bNormalizationFlags & ( FLAG_NORM_CONSIDER_TAUT &~FLAG_PROTON_CHARGE_CANCEL ))
1057 : : {
1058 [ + - ]: 18 : if (!bNoWarnings)
1059 : : {
1060 : 18 : WarningMessage( pStrErrStruct, "Proton(s) added/removed" );
1061 : : }
1062 : : }
1063 [ - + ]: 1265 : if (pINChI_Aux->bNormalizationFlags & FLAG_PROTON_CHARGE_CANCEL)
1064 : : {
1065 [ # # ]: 0 : if (!bNoWarnings)
1066 : : {
1067 : 0 : WarningMessage( pStrErrStruct, "Charges neutralized" );
1068 : : }
1069 : : }
1070 : : }
1071 : :
1072 : : /* Abs or rel stereo may establish one of two canonical numberings */
1073 [ + + - + ]: 1265 : if (( pCS->nLenLinearCTStereoCarb > 0 || pCS->nLenLinearCTStereoDble > 0 ) &&
1074 [ + - ]: 71 : pCS->nLenCanonOrdStereo > 0 &&
1075 [ + - - + ]: 71 : ( (pCS->LinearCTStereoCarb && pCS->LinearCTStereoCarbInv) ||
1076 [ # # # # ]: 0 : (pCS->LinearCTStereoDble && pCS->LinearCTStereoDbleInv) ) &&
1077 [ + - + - ]: 71 : pCS->nCanonOrdStereo && pCS->nCanonOrdStereoInv
1078 : : ) /* djb-rwth: addressing LLVM warning */
1079 : : {
1080 : 71 : pCanonRank = pCanonRankAtoms;
1081 : 71 : pCanonOrd = pCS->nCanonOrdStereo;
1082 : 71 : pCanonRankInv = pSortOrd;
1083 : 71 : pCanonOrdInv = pCS->nCanonOrdStereoInv;
1084 : 71 : Stereo = pINChI->Stereo;
1085 : :
1086 [ + + ]: 4758 : for (i = 0; i < num_at_tg; i++)
1087 : : {
1088 : 4687 : pCanonRankInv[pCanonOrdInv[i]] =
1089 : 4687 : pCanonRank[pCanonOrd[i]] = (AT_NUMB) ( i + 1 );
1090 : : }
1091 : :
1092 : : /********************************************************************/
1093 : : /* Copy stereo bonds and stereo centers; compare Inv and Abs stereo */
1094 : : /********************************************************************/
1095 : :
1096 : 71 : nErrorCode = CopyLinearCTStereoToINChIStereo( Stereo,
1097 : : pCS->LinearCTStereoCarb,
1098 : : pCS->nLenLinearCTStereoCarb,
1099 : : pCS->LinearCTStereoDble,
1100 : : pCS->nLenLinearCTStereoDble,
1101 : : pCanonOrd, pCanonRank,
1102 : : at, 0 /* non-isotopic */,
1103 : : pCS->LinearCTStereoCarbInv,
1104 : : pCS->LinearCTStereoDbleInv,
1105 : : pCanonOrdInv, pCanonRankInv );
1106 : :
1107 : : /* djb-rwth: fixing oss-fuzz issue #68271 */
1108 [ - + ]: 71 : if (nErrorCode == 1)
1109 : : {
1110 : 0 : nErrorCode = 0;
1111 : 0 : ret = CT_OUT_OF_RAM; /* <BRKPT> */
1112 : 0 : pINChI->nErrorCode = pINChI_Aux->nErrorCode = CT_OUT_OF_RAM;
1113 : 0 : goto exit_function;
1114 : : }
1115 : :
1116 [ + - + - ]: 71 : if (Stereo->t_parityInv && Stereo->nNumberInv)
1117 : : {
1118 [ - + ]: 71 : if (nUserMode & REQ_MODE_RELATIVE_STEREO)
1119 : : {
1120 : 0 : pINChI->nFlags |= INCHI_FLAG_REL_STEREO;
1121 : : }
1122 [ - + ]: 71 : if (nUserMode & REQ_MODE_RACEMIC_STEREO)
1123 : : {
1124 : 0 : pINChI->nFlags |= INCHI_FLAG_RAC_STEREO;
1125 : : }
1126 [ + + ]: 71 : if (Stereo->nCompInv2Abs)
1127 : : {
1128 [ + + ]: 60 : if (Stereo->nCompInv2Abs == -1)
1129 : : {
1130 : : /* switch pointers in Stereo so that the stereo becomes the smallest (relative) */
1131 : : /* flag Stereo->nCompInv2Abs == -1 will keep track of this exchange */
1132 : 24 : AT_NUMB *nNumberInv = Stereo->nNumberInv;
1133 : 24 : S_CHAR *t_parityInv = Stereo->t_parityInv;
1134 : 24 : Stereo->nNumberInv = Stereo->nNumber;
1135 : 24 : Stereo->t_parityInv = Stereo->t_parity;
1136 : 24 : Stereo->nNumber = nNumberInv;
1137 : 24 : Stereo->t_parity = t_parityInv;
1138 : : /* switch pointers to set rel. stereo to pINChI_Aux->nOrigAtNosInCanonOrd
1139 : : and inv. stereo to pINChI_Aux->nOrigAtNosInCanonOrdInv */
1140 : 24 : switch_ptrs( &pCanonRank, &pCanonRankInv );
1141 : 24 : switch_ptrs( &pCanonOrd, &pCanonOrdInv );
1142 : 24 : bUseNumberingInv = 1; /* use inverted stereo numbering instead of normal */
1143 : : }
1144 : : }
1145 : : }
1146 : :
1147 : : LOG_NO_ARGS("************************** Canonical Ordering with Stereo (L1183:ichimak2.c) ***************************\n");
1148 [ + + ]: 4758 : for (i = 0; i < num_atoms; i++)
1149 : : {
1150 : 4687 : pINChI_Aux->nOrigAtNosInCanonOrdInv[i] = at[pCanonOrdInv[i]].orig_at_number;
1151 : 4687 : pINChI_Aux->nOrigAtNosInCanonOrd[i] = at[pCanonOrd[i]].orig_at_number;
1152 : : LOG_MULT_ARGS("Atom Nr: %d, Canonical Numbering Normal: %d, Element Name: %s\n", i + 1, at[pCanonOrd[i]].orig_at_number, at[i].elname);
1153 : : }
1154 : :
1155 : : LOG_NO_ARGS("\n******************************************************************************************************\n");
1156 : :
1157 [ + + ]: 71 : if (bUseNumberingInv)
1158 : : {
1159 : : /* switch ptrs back to avoid confusion */
1160 : 24 : switch_ptrs( &pCanonRank, &pCanonRankInv );
1161 : 24 : switch_ptrs( &pCanonOrd, &pCanonOrdInv );
1162 : : /* save inverted stereo ranks & order because it represents the smallest (relative) */
1163 : 24 : memcpy(pCanonRank, pCanonRankInv, num_at_tg * sizeof(pCanonRank[0]));
1164 : : /* change pCS->nCanonOrdStereo[] to inverted: */
1165 : 24 : memcpy(pCanonOrd, pCanonOrdInv, num_at_tg * sizeof(pCanonOrd[0]));
1166 : : }
1167 : :
1168 : 71 : pCanonRankInv = NULL;
1169 : 71 : pCanonOrdInv = NULL;
1170 : 71 : pOrigNosInCanonOrd = NULL;
1171 : : }
1172 : : else
1173 : : {
1174 : : /*------------------------------ no stereo */
1175 : 2388 : pCanonOrd = pCS->nLenCanonOrdStereo > 0 ? pCS->nCanonOrdStereo
1176 [ + + + - ]: 1194 : : pCS->nLenCanonOrd > 0 ? pCS->nCanonOrd
1177 : : : NULL;
1178 : 1194 : pCanonRank = pCanonRankAtoms;
1179 : 1194 : pOrigNosInCanonOrd = pINChI_Aux->nOrigAtNosInCanonOrd;
1180 : :
1181 [ + - + - ]: 1194 : if (pCanonOrd && pCanonRank)
1182 : : {
1183 [ + + ]: 5711 : for (i = 0; i < num_atoms; i++)
1184 : : {
1185 : 4517 : pCanonRank[pCanonOrd[i]] = (AT_NUMB) ( i + 1 );
1186 : 4517 : pOrigNosInCanonOrd[i] = at[pCanonOrd[i]].orig_at_number;
1187 : : }
1188 [ + + ]: 1204 : for (; i < num_at_tg; i++)
1189 : : {
1190 : 10 : pCanonRank[pCanonOrd[i]] = (AT_NUMB) ( i + 1 );
1191 : : }
1192 : : }
1193 : : }
1194 : : /*pCanonOrdNonIso = pCanonOrd;*/ /* save for aux info */
1195 : :
1196 : :
1197 [ + - ]: 1265 : if (pINChI_Aux->OrigInfo)
1198 : : {
1199 : : /* charges, radicals, valences */
1200 [ + + ]: 10469 : for (i = 0; i < num_atoms; i++)
1201 : : {
1202 [ + - ]: 9204 : if (pCanonOrd) /* djb-rwth: fixing a NULL pointer dereference */
1203 : : {
1204 : 9204 : ii = pCanonOrd[i];
1205 [ + + + + ]: 9204 : if (norm_at[ii].valence || norm_at[ii].num_H)
1206 : : {
1207 : 9122 : pINChI_Aux->OrigInfo[i].cCharge = norm_at[ii].charge;
1208 : :
1209 [ + - ]: 18244 : pINChI_Aux->OrigInfo[i].cRadical = (norm_at[ii].radical == RADICAL_SINGLET) ? 0 :
1210 [ + - ]: 9122 : (norm_at[ii].radical == RADICAL_DOUBLET) ? 1 :
1211 [ + - ]: 9122 : (norm_at[ii].radical == RADICAL_TRIPLET) ? 2 :
1212 [ - + ]: 9122 : norm_at[ii].radical ? 3 : 0;
1213 : :
1214 : 9122 : pINChI_Aux->OrigInfo[i].cUnusualValence =
1215 : 9122 : get_unusual_el_valence(norm_at[ii].el_number, norm_at[ii].charge, norm_at[ii].radical,
1216 : 9122 : norm_at[ii].chem_bonds_valence, norm_at[ii].num_H, norm_at[ii].valence);
1217 : : }
1218 : : else
1219 : : {
1220 : : /* charge of a single atom component is in the INChI; valence = 0 is standard */
1221 [ + - ]: 164 : pINChI_Aux->OrigInfo[i].cRadical = (norm_at[ii].radical == RADICAL_SINGLET) ? 0 :
1222 [ + - ]: 82 : (norm_at[ii].radical == RADICAL_DOUBLET) ? 1 :
1223 [ + - ]: 82 : (norm_at[ii].radical == RADICAL_TRIPLET) ? 2 :
1224 [ - + ]: 82 : norm_at[ii].radical ? 3 : 0;
1225 : : }
1226 : : }
1227 : : }
1228 : : }
1229 : :
1230 : : /* Non-isotopic canonical numbers and equivalence of atoms (Aux) */
1231 : 1265 : pConstitEquNumb = pINChI_Aux->nConstitEquNumbers; /* contitutional equivalence */
1232 : 1265 : pSymmRank = pCS->nSymmRank;
1233 : :
1234 [ + - + - : 1265 : if (pCanonOrd && pCanonRank && pSymmRank && pConstitEquNumb)
+ - + - ]
1235 : : {
1236 [ + + ]: 10469 : for (i = 0; i < num_atoms; i++)
1237 : : {
1238 : 9204 : pConstitEquNumb[i] = pSymmRank[pCanonOrd[i]]; /* constit. equ. ranks in order of canonical numbers */
1239 : 9204 : pSortOrd[i] = i;
1240 : : }
1241 [ + + ]: 1275 : for (; i < num_at_tg; i++)
1242 : : {
1243 : 10 : pSortOrd[i] = MAX_ATOMS; /* for debugging only */
1244 : : }
1245 : :
1246 : 1265 : pCG->m_pn_RankForSort = pConstitEquNumb;
1247 : 1265 : inchi_qsort( pCG, pSortOrd, num_atoms, sizeof( pSortOrd[0] ), CompRanksOrd );
1248 : :
1249 [ + + ]: 10469 : for (i = 0, nMinOrd = pSortOrd[0], j = 1; j <= num_atoms; j++)
1250 : : {
1251 [ + + + + ]: 9204 : if (j == num_atoms || pConstitEquNumb[pSortOrd[i]] != pConstitEquNumb[pSortOrd[j]])
1252 : : {
1253 : 7527 : nMinOrd++;
1254 [ + + ]: 7527 : if (j - i > 1)
1255 : : {
1256 : : /* found a sequence of equivalent atoms: i..j-1 */
1257 [ + + ]: 4933 : while (i < j)
1258 : : {
1259 : 3305 : pConstitEquNumb[pSortOrd[i++]] = nMinOrd; /* = min. canon. rank in the group of equ. atoms */
1260 : : }
1261 : : /* at this point j == i */
1262 : : }
1263 : : else
1264 : : {
1265 : 5899 : pConstitEquNumb[pSortOrd[i++]] = 0; /* means the atom is not equivalent to any other */
1266 : : }
1267 : 7527 : nMinOrd = pSortOrd[j]; /* at the end j = num_atoms */
1268 : : }
1269 : : }
1270 : : }
1271 : :
1272 : : else
1273 : : {
1274 : 0 : nErrorCode |= ERR_NO_CANON_RESULTS;
1275 : 0 : ret = -1; /* program error; no breakpoint here */
1276 : 0 : goto exit_function;
1277 : : }
1278 : :
1279 : :
1280 : : /* Atomic numbers from the Periodic Table */
1281 [ + + ]: 10469 : for (i = 0; i < num_atoms; i++)
1282 : : {
1283 : 9204 : pINChI->nAtom[i] = (int) at[pCanonOrd[i]].el_number;
1284 : : }
1285 : :
1286 : :
1287 : : /* Connection table: atoms only (before 7-29-2003 pCS->LinearCT2 contained non-isotopic CT) */
1288 [ + - + - : 1265 : if (pCS->nLenLinearCTAtOnly <= 0 || !pCS->LinearCT || !pINChI->nConnTable)
- + ]
1289 : : {
1290 : 0 : nErrorCode |= ERR_NO_CANON_RESULTS;
1291 : 0 : ret = -2;
1292 : 0 : goto exit_function;
1293 : : }
1294 : :
1295 : 1265 : memcpy(pINChI->nConnTable, pCS->LinearCT, sizeof(pINChI->nConnTable[0]) * pCS->nLenLinearCTAtOnly);
1296 : :
1297 : 1265 : pINChI->lenConnTable = pCS->nLenLinearCTAtOnly;
1298 : :
1299 : : /* Tautomeric group(s) canonical representation */
1300 : 1265 : len = 0;
1301 [ + - + + ]: 1265 : if (bTautomeric && 0 < ( n = SortTautomerGroupsAndEndpoints(
1302 : : pCG, t_group_info,
1303 : : num_atoms, num_at_tg, pCanonRank ) ))
1304 : : {
1305 : : /* SortTautomerGroupsAndEndpoints() produces canonically ordered t-groups */
1306 : 10 : pINChI->nFlags |=
1307 : 5 : ( t_group_info->bTautFlagsDone & TG_FLAG_ALL_SALT_DONE ) ? INCHI_FLAG_ACID_TAUT
1308 [ - + ]: 5 : : 0;
1309 : :
1310 : : /* number of tautomeric groups */
1311 : 5 : pINChI->nTautomer[len++] = (AT_NUMB) n;
1312 : :
1313 : : /* store each tautomeric group, one by one */
1314 [ + + ]: 15 : for (i = 0; i < n; i++)
1315 : : {
1316 : 10 : g = (int) t_group_info->tGroupNumber[i]; /* original group numbers in sorted order */
1317 : 10 : t_group = t_group_info->t_group + g; /* pointer to the tautomeric group */
1318 : :
1319 : : /* NumAt+INCHI_T_NUM_MOVABLE (group length excluding this number) */
1320 : :
1321 : 10 : pINChI->nTautomer[len++] = t_group->nNumEndpoints + INCHI_T_NUM_MOVABLE;
1322 : :
1323 : : /* Num(H), Num(-) */
1324 : :
1325 [ + + ]: 30 : for (j = 0; j < INCHI_T_NUM_MOVABLE; j++) /* djb-rwth: redundant condition; && j < T_NUM_NO_ISOTOPIC part should be checked; can INCHI_T_NUM_MOVABLE and T_NUM_NO_ISOTOPIC change values from 2? */
1326 : : {
1327 : 20 : pINChI->nTautomer[len++] = t_group->num[j];
1328 : : }
1329 : :
1330 : :
1331 : : /* djb-rwth: erroneous loop execution condition
1332 : :
1333 : : for (j = T_NUM_NO_ISOTOPIC; j < INCHI_T_NUM_MOVABLE; j++)
1334 : : {
1335 : : pINChI->nTautomer[len++] = 0; // should not happen
1336 : : }
1337 : :
1338 : : */
1339 : :
1340 : : /* tautomeric group endpoint canonical numbers, pre-sorted in ascending order */
1341 : :
1342 : 10 : for (j = (int) t_group->nFirstEndpointAtNoPos,
1343 [ + + ]: 34 : m = j + (int) t_group->nNumEndpoints; j < m; j++)
1344 : : {
1345 : 24 : pINChI->nTautomer[len++] = pCanonRank[(int) t_group_info->nEndpointAtomNumber[j]]; /* At[j] */
1346 : : }
1347 : : }
1348 : :
1349 : 5 : pINChI->lenTautomer = len;
1350 : 5 : pINChI_Aux->nNumberOfTGroups = n;
1351 : : }
1352 : : else
1353 : : {
1354 : 1260 : pINChI->lenTautomer = 0;
1355 : 1260 : pINChI_Aux->nNumberOfTGroups = 0;
1356 : :
1357 [ + - + + ]: 1260 : if (t_group_info && ( ( t_group_info->tni.bNormalizationFlags & FLAG_NORM_CONSIDER_TAUT ) ||
1358 [ - + ]: 1247 : (t_group_info->nNumIsotopicEndpoints > 1 &&
1359 [ # # ]: 0 : ( t_group_info->bTautFlagsDone & ( TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE ) )) )
1360 : : ) /* djb-rwth: addressing LLVM warning */
1361 : : {
1362 : : /* only protons (re)moved or added */
1363 : 13 : pINChI->lenTautomer = 1;
1364 : 13 : pINChI->nTautomer[0] = 0;
1365 : : }
1366 : : }
1367 : :
1368 : : /* Number of H (excluding tautomeric) */
1369 [ + - ]: 1265 : if (pCS->nNum_H)
1370 : : {
1371 [ + + ]: 10469 : for (i = 0; i < num_atoms; i++)
1372 : : {
1373 : 9204 : pINChI->nNum_H[i] = pCS->nNum_H[i];
1374 : : }
1375 : : }
1376 : :
1377 : :
1378 : : /* Number of fixed H (tautomeric H in non-tautomeric representation) */
1379 [ + - + + ]: 1265 : if (pCS->nNum_H_fixed && !pINChI->lenTautomer)
1380 : : {
1381 [ + + ]: 10402 : for (i = 0; i < num_atoms; i++)
1382 : : {
1383 : 9155 : pINChI->nNum_H_fixed[i] = pCS->nNum_H_fixed[i];
1384 : 9155 : pINChI->nNum_H[i] += pCS->nNum_H_fixed[i];
1385 : : }
1386 : : }
1387 : :
1388 : :
1389 : : /***********************************************************
1390 : : * Tautomeric group(s) numbering and symmetry;
1391 : : * should not depend on switching to rel. stereo numbering
1392 : : */
1393 [ + + + + ]: 1265 : if (pINChI->lenTautomer && ( n = pINChI_Aux->nNumberOfTGroups ))
1394 : : {
1395 [ + + ]: 8 : pCanonOrdTaut = pCS->nLenCanonOrdStereoTaut > 0 ? pCS->nCanonOrdStereoTaut :
1396 [ + - ]: 3 : pCS->nLenCanonOrdTaut > 0 ? pCS->nCanonOrdTaut : NULL;
1397 : 5 : pConstitEquNumb = pINChI_Aux->nConstitEquTGroupNumbers;
1398 : 5 : pSymmRank = pCS->nSymmRankTaut;
1399 : :
1400 [ + - + - : 5 : if (pCanonOrdTaut && pSymmRank && pConstitEquNumb)
+ - ]
1401 : : {
1402 [ + + ]: 15 : for (i = 0; i < n; i++)
1403 : : {
1404 : 10 : pConstitEquNumb[i] = pSymmRank[pCanonOrdTaut[i]];
1405 : 10 : pSortOrd[i] = i;
1406 : : }
1407 : 5 : pCG->m_pn_RankForSort = pConstitEquNumb;
1408 : 5 : inchi_qsort( pCG, pSortOrd, n, sizeof( pSortOrd[0] ), CompRanksOrd );
1409 [ + + ]: 15 : for (i = 0, nMinOrd = pSortOrd[0], j = 1; j <= n; j++)
1410 : : {
1411 [ + + - + ]: 10 : if (j == n || pConstitEquNumb[pSortOrd[i]] != pConstitEquNumb[pSortOrd[j]])
1412 : : {
1413 : 5 : nMinOrd++; /* make is start from 1, not from zero */
1414 [ + + ]: 5 : if (j - i > 1)
1415 : : {
1416 : : /* found a sequence of more than one equivalent t-groups: i..j-1 */
1417 [ + + ]: 11 : while (i < j)
1418 : : {
1419 : 8 : pConstitEquNumb[pSortOrd[i++]] = nMinOrd;
1420 : : }
1421 : : }
1422 : : else
1423 : : {
1424 : 2 : pConstitEquNumb[pSortOrd[i++]] = 0;
1425 : : }
1426 : 5 : nMinOrd = pSortOrd[j]; /* at the end j == n */
1427 : : }
1428 : : }
1429 : : }
1430 : : }
1431 : :
1432 : :
1433 : : /* Allocate and fill Hill formula */
1434 : :
1435 : 1265 : pINChI->szHillFormula = AllocateAndFillHillFormula( pINChI );
1436 : :
1437 [ - + ]: 1265 : if (!pINChI->szHillFormula)
1438 : : {
1439 : 0 : nErrorCode = 0;
1440 : 0 : ret = CT_WRONG_FORMULA; /* CT_OUT_OF_RAM;*/ /* <BRKPT> */
1441 : 0 : pINChI->nErrorCode = pINChI_Aux->nErrorCode = ret;
1442 : 0 : goto exit_function;
1443 : : }
1444 : :
1445 : 1265 : nStereoUnmarkMode = UnmarkAllUndefinedUnknownStereo( pINChI->Stereo, nUserMode );
1446 : :
1447 [ + + ]: 1265 : if (nStereoUnmarkMode)
1448 : : {
1449 : 9 : pINChI->nFlags |=
1450 : : ( nStereoUnmarkMode & REQ_MODE_SC_IGN_ALL_UU ) ? INCHI_FLAG_SC_IGN_ALL_UU
1451 : 9 : : 0;
1452 : 9 : pINChI->nFlags |=
1453 : : ( nStereoUnmarkMode & REQ_MODE_SB_IGN_ALL_UU ) ? INCHI_FLAG_SB_IGN_ALL_UU
1454 : 9 : : 0;
1455 : :
1456 [ - + ]: 9 : if (( nStereoUnmarkMode & REQ_MODE_SC_IGN_ALL_UU ) ||
1457 [ # # ]: 0 : ( nStereoUnmarkMode & REQ_MODE_SB_IGN_ALL_UU ))
1458 : : {
1459 [ + - ]: 9 : if (!bNoWarnings)
1460 : : {
1461 : 9 : WarningMessage( pStrErrStruct, "Omitted undefined stereo" );
1462 : : }
1463 : : }
1464 : : }
1465 : :
1466 : : /*************************/
1467 : : /* Mark ambiguous stereo */
1468 : : /*************************/
1469 : :
1470 : 1265 : MarkAmbiguousStereo( at, norm_at, 0 /* non-isotopic */, pCanonOrd,
1471 : : pCS->LinearCTStereoCarb, pCS->nLenLinearCTStereoCarb,
1472 : : pCS->LinearCTStereoDble, pCS->nLenLinearCTStereoDble );
1473 : :
1474 : :
1475 : : /************************************************************************
1476 : : *
1477 : : * Isotopic part
1478 : : */
1479 : :
1480 : : /* abs or rel stereo may establish one of two canonical numberings */
1481 : :
1482 [ + + - + ]: 1265 : if (( pCS->nLenLinearCTIsotopicStereoCarb > 0 || pCS->nLenLinearCTIsotopicStereoDble > 0 ) &&
1483 [ + - ]: 4 : pCS->nLenCanonOrdIsotopicStereo > 0 &&
1484 [ + - - + ]: 4 : ( (pCS->LinearCTIsotopicStereoCarb && pCS->LinearCTIsotopicStereoCarbInv) ||
1485 [ # # # # ]: 0 : (pCS->LinearCTIsotopicStereoDble && pCS->LinearCTIsotopicStereoDbleInv) ) &&
1486 [ + - + - ]: 4 : pCS->nCanonOrdIsotopicStereo && pCS->nCanonOrdIsotopicStereoInv
1487 : : ) /* djb-rwth: addressing LLVM warning */
1488 : : {
1489 : : /* found isotopic stereo */
1490 : :
1491 : 4 : pCanonRank = pCanonRankAtoms;
1492 : 4 : pCanonOrd = pCS->nCanonOrdIsotopicStereo;
1493 : 4 : pCanonRankInv = pSortOrd;
1494 : 4 : pCanonOrdInv = pCS->nCanonOrdIsotopicStereoInv;
1495 : 4 : Stereo = pINChI->StereoIsotopic;
1496 : :
1497 [ + + ]: 4004 : for (i = 0; i < num_at_tg; i++)
1498 : : {
1499 : 4000 : pCanonRankInv[pCanonOrdInv[i]] =
1500 : 4000 : pCanonRank[pCanonOrd[i]] = (AT_NUMB) ( i + 1 );
1501 : : }
1502 : :
1503 : :
1504 : : /********************************************************************/
1505 : : /* copy stereo bonds and stereo centers; compare Inv and Abs stereo */
1506 : : /********************************************************************/
1507 : :
1508 : 4 : nErrorCode = CopyLinearCTStereoToINChIStereo( Stereo,
1509 : : pCS->LinearCTIsotopicStereoCarb,
1510 : : pCS->nLenLinearCTIsotopicStereoCarb,
1511 : : pCS->LinearCTIsotopicStereoDble,
1512 : : pCS->nLenLinearCTIsotopicStereoDble,
1513 : : pCanonOrd, pCanonRank, at, 1 /* isotopic */,
1514 : : pCS->LinearCTIsotopicStereoCarbInv,
1515 : : pCS->LinearCTIsotopicStereoDbleInv,
1516 : : pCanonOrdInv, pCanonRankInv );
1517 : :
1518 : : /* djb-rwth: fixing oss-fuzz issue #68271 */
1519 [ - + ]: 4 : if (nErrorCode == 1)
1520 : : {
1521 : 0 : nErrorCode = 0;
1522 : 0 : ret = CT_OUT_OF_RAM; /* <BRKPT> */
1523 : 0 : pINChI->nErrorCode = pINChI_Aux->nErrorCode = CT_OUT_OF_RAM;
1524 : 0 : goto exit_function;
1525 : : }
1526 : :
1527 [ + - + - ]: 4 : if (Stereo->t_parityInv && Stereo->nNumberInv)
1528 : : {
1529 [ - + ]: 4 : if (nUserMode & REQ_MODE_RELATIVE_STEREO)
1530 : : {
1531 : 0 : pINChI->nFlags |= INCHI_FLAG_REL_STEREO;
1532 : : }
1533 : :
1534 [ - + ]: 4 : if (nUserMode & REQ_MODE_RACEMIC_STEREO)
1535 : : {
1536 : 0 : pINChI->nFlags |= INCHI_FLAG_RAC_STEREO;
1537 : : }
1538 : :
1539 [ + - ]: 4 : if (Stereo->nCompInv2Abs)
1540 : : {
1541 [ + + ]: 4 : if (Stereo->nCompInv2Abs == -1)
1542 : : {
1543 : : /* switch pointers so that the stereo becomes the smallest (relative) */
1544 : : /* flag Stereo->nCompInv2Abs == -1 will keep track of this exchange */
1545 : 2 : AT_NUMB *nNumberInv = Stereo->nNumberInv;
1546 : 2 : S_CHAR *t_parityInv = Stereo->t_parityInv;
1547 : 2 : Stereo->nNumberInv = Stereo->nNumber;
1548 : 2 : Stereo->t_parityInv = Stereo->t_parity;
1549 : 2 : Stereo->nNumber = nNumberInv;
1550 : 2 : Stereo->t_parity = t_parityInv;
1551 : 2 : switch_ptrs( &pCanonRank, &pCanonRankInv );
1552 : 2 : switch_ptrs( &pCanonOrd, &pCanonOrdInv );
1553 : 2 : bUseIsotopicNumberingInv = 1;
1554 : : }
1555 : : }
1556 : : }
1557 : :
1558 [ + + ]: 4004 : for (i = 0; i < num_atoms; i++)
1559 : : {
1560 : 4000 : pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv[i] = at[pCanonOrdInv[i]].orig_at_number;
1561 : 4000 : pINChI_Aux->nIsotopicOrigAtNosInCanonOrd[i] = at[pCanonOrd[i]].orig_at_number;
1562 : : }
1563 : :
1564 [ + + ]: 4 : if (bUseIsotopicNumberingInv)
1565 : : {
1566 : 2 : switch_ptrs( &pCanonRank, &pCanonRankInv );
1567 : 2 : switch_ptrs( &pCanonOrd, &pCanonOrdInv );
1568 : 2 : memcpy(pCanonRank, pCanonRankInv, num_at_tg * sizeof(pCanonRank[0]));
1569 : 2 : memcpy(pCanonOrd, pCanonOrdInv, num_at_tg * sizeof(pCanonOrd[0]));
1570 : : }
1571 : :
1572 : 4 : pCanonRankInv = NULL;
1573 : 4 : pCanonOrdInv = NULL;
1574 : 4 : pOrigNosInCanonOrd = NULL;
1575 : : }
1576 : : else
1577 : : {
1578 : : /* no isotopic stereo */
1579 : :
1580 : 2522 : pCanonOrd = pCS->nLenCanonOrdIsotopicStereo > 0 ? pCS->nCanonOrdIsotopicStereo
1581 [ - + + + ]: 1261 : : pCS->nLenCanonOrdIsotopic > 0 ? pCS->nCanonOrdIsotopic
1582 : : : NULL;
1583 : 1261 : pCanonRank = pCanonRankAtoms;
1584 : 1261 : pOrigNosInCanonOrd = pINChI_Aux->nIsotopicOrigAtNosInCanonOrd;
1585 : :
1586 : : /* djb-rwth: fixing oss-fuzz issue #30496 */
1587 [ + + + - : 1261 : if (pCanonOrd && pCanonRank && pOrigNosInCanonOrd)
+ - ]
1588 : : {
1589 [ + + ]: 1013 : for (i = 0; i < num_atoms; i++)
1590 : : {
1591 : : /* Fix13 -- out of bounds */
1592 : 1007 : pCanonRank[pCanonOrd[i]] = (AT_NUMB) ( i + 1 );
1593 : 1007 : pOrigNosInCanonOrd[i] = at[pCanonOrd[i]].orig_at_number;
1594 : : }
1595 [ - + ]: 6 : for (; i < num_at_tg; i++)
1596 : : {
1597 : : /* Fix13 -- out of bounds */
1598 : 0 : pCanonRank[pCanonOrd[i]] = (AT_NUMB) ( i + 1 );
1599 : : }
1600 : : }
1601 : : }
1602 : : /*pCanonOrdIso = pCanonOrd;*/
1603 : :
1604 : :
1605 : 1265 : pConstitEquNumb = pINChI_Aux->nConstitEquIsotopicNumbers;
1606 : 1265 : pSymmRank = pCS->nSymmRankIsotopic;
1607 : :
1608 [ + + + - : 1265 : if (pCanonOrd && pCanonRank && pConstitEquNumb && pSymmRank)
+ - + - ]
1609 : : {
1610 [ + + ]: 5017 : for (i = 0; i < num_atoms; i++)
1611 : : {
1612 : 5007 : pConstitEquNumb[i] = pSymmRank[pCanonOrd[i]];
1613 : 5007 : pSortOrd[i] = i;
1614 : : }
1615 : :
1616 [ - + ]: 10 : for (; i < num_at_tg; i++)
1617 : : {
1618 : 0 : pSortOrd[i] = i;
1619 : : }
1620 : :
1621 : 10 : pCG->m_pn_RankForSort = pConstitEquNumb;
1622 : 10 : inchi_qsort( pCG, pSortOrd, num_atoms, sizeof( pSortOrd[0] ), CompRanksOrd );
1623 : :
1624 [ + + ]: 5017 : for (i = 0, nMinOrd = pSortOrd[0], j = 1; j <= num_atoms; j++)
1625 : : {
1626 [ + + + - ]: 5007 : if (j == num_atoms || pConstitEquNumb[pSortOrd[i]] != pConstitEquNumb[pSortOrd[j]])
1627 : : {
1628 : 5007 : nMinOrd++;
1629 : :
1630 [ - + ]: 5007 : if (j - i > 1)
1631 : : {
1632 : : /* found a sequence of equivalent atoms: i..j-1 */
1633 [ # # ]: 0 : while (i < j)
1634 : : {
1635 : 0 : pConstitEquNumb[pSortOrd[i++]] = nMinOrd;
1636 : : }
1637 : : }
1638 : : else
1639 : : {
1640 : 5007 : pConstitEquNumb[pSortOrd[i++]] = 0; /* nMinOrd; */
1641 : : }
1642 : 5007 : nMinOrd = pSortOrd[j];
1643 : : }
1644 : : }
1645 : : }
1646 : : else
1647 : : {
1648 : 1255 : goto exit_function; /* no isotopic info available */
1649 : : }
1650 : :
1651 : :
1652 : : /* Isotopic atoms */
1653 : :
1654 : 10 : n = pINChI->nNumberOfIsotopicAtoms = pCS->nLenLinearCTIsotopic;
1655 : :
1656 [ + + ]: 20 : for (i = 0; i < n; i++)
1657 : : {
1658 : 10 : pINChI->IsotopicAtom[i].nAtomNumber = pCS->LinearCTIsotopic[i].at_num;
1659 : 10 : pINChI->IsotopicAtom[i].nIsoDifference = pCS->LinearCTIsotopic[i].iso_atw_diff;
1660 : 10 : pINChI->IsotopicAtom[i].nNum_H = pCS->LinearCTIsotopic[i].num_1H;
1661 : 10 : pINChI->IsotopicAtom[i].nNum_D = pCS->LinearCTIsotopic[i].num_D;
1662 : 10 : pINChI->IsotopicAtom[i].nNum_T = pCS->LinearCTIsotopic[i].num_T;
1663 : : }
1664 : :
1665 : : /* Isotopic tautomeric groups */
1666 : :
1667 : 10 : n = pINChI->nNumberOfIsotopicTGroups = pCS->nLenLinearCTIsotopicTautomer;
1668 : :
1669 [ - + ]: 10 : for (i = 0; i < n; i++)
1670 : : {
1671 : 0 : pINChI->IsotopicTGroup[i].nTGroupNumber = pCS->LinearCTIsotopicTautomer[i].tgroup_num;
1672 : 0 : pINChI->IsotopicTGroup[i].nNum_H = pCS->LinearCTIsotopicTautomer[i].num[2];
1673 : 0 : pINChI->IsotopicTGroup[i].nNum_D = pCS->LinearCTIsotopicTautomer[i].num[1];
1674 : 0 : pINChI->IsotopicTGroup[i].nNum_T = pCS->LinearCTIsotopicTautomer[i].num[0];
1675 : : }
1676 : :
1677 : : /* Atoms that may exchange isotopic H-atoms */
1678 : :
1679 [ + - + - ]: 10 : if (pCS->nExchgIsoH && pINChI->nPossibleLocationsOfIsotopicH)
1680 : : {
1681 [ + + ]: 5017 : for (i = 0, j = 1; i < num_atoms; i++)
1682 : : {
1683 [ - + ]: 5007 : if (pCS->nExchgIsoH[i])
1684 : : {
1685 : 0 : pINChI->nPossibleLocationsOfIsotopicH[j++] = (AT_NUMB) ( i + 1 ); /* canonical number */
1686 : : }
1687 : : }
1688 : 10 : pINChI->nPossibleLocationsOfIsotopicH[0] = (AT_NUMB) j; /* length including the 0th element */
1689 : : }
1690 : :
1691 [ - + ]: 10 : if ((nStereoUnmarkMode = UnmarkAllUndefinedUnknownStereo( pINChI->StereoIsotopic, nUserMode ))) /* djb-rwth: addressing LLVM warning */
1692 : : {
1693 : 0 : pINChI->nFlags |=
1694 : : ( nStereoUnmarkMode & REQ_MODE_SC_IGN_ALL_UU ) ? INCHI_FLAG_SC_IGN_ALL_ISO_UU
1695 : 0 : : 0;
1696 : 0 : pINChI->nFlags |=
1697 : : ( nStereoUnmarkMode & REQ_MODE_SB_IGN_ALL_UU ) ? INCHI_FLAG_SC_IGN_ALL_ISO_UU
1698 : 0 : : 0;
1699 [ # # ]: 0 : if (( nStereoUnmarkMode & REQ_MODE_SC_IGN_ALL_UU ) ||
1700 [ # # ]: 0 : ( nStereoUnmarkMode & REQ_MODE_SB_IGN_ALL_UU ))
1701 : : {
1702 [ # # ]: 0 : if (!bNoWarnings)
1703 : : {
1704 : 0 : WarningMessage( pStrErrStruct, "Omitted undefined stereo" );
1705 : : }
1706 : : }
1707 : : }
1708 : :
1709 : : /* Mark ambiguous stereo */
1710 : :
1711 : 10 : MarkAmbiguousStereo( at, norm_at, 1 /* isotopic */, pCanonOrd,
1712 : : pCS->LinearCTIsotopicStereoCarb, pCS->nLenLinearCTIsotopicStereoCarb,
1713 : : pCS->LinearCTIsotopicStereoDble, pCS->nLenLinearCTIsotopicStereoDble );
1714 : :
1715 : :
1716 : : /***********************************************************
1717 : : * Isotopic tautomeric group(s) numbering and symmetry;
1718 : : * should not depend on switching to rel. stereo numbering
1719 : : */
1720 : :
1721 [ + + ]: 10 : if (pINChI->lenTautomer &&
1722 [ - + ]: 4 : pINChI_Aux->nConstitEquIsotopicTGroupNumbers &&
1723 [ + - ]: 4 : pCS->nSymmRankIsotopicTaut &&
1724 [ # # # # : 0 : ( pCS->nLenLinearCTIsotopic || pCS->nLenLinearCTIsotopicTautomer ) &&
# # ]
1725 [ # # ]: 0 : t_group_info && t_group_info->num_t_groups > 0)
1726 : : {
1727 : :
1728 : : /* djb-rwth: removing redundant code */
1729 : :
1730 : 0 : pCanonOrdTaut =
1731 : 0 : pCS->nLenCanonOrdIsotopicStereoTaut > 0 ? ( n = pCS->nLenCanonOrdIsotopicStereoTaut, pCS->nCanonOrdIsotopicStereoTaut )
1732 [ # # # # ]: 0 : : pCS->nLenCanonOrdIsotopicTaut > 0 ? ( n = pCS->nLenCanonOrdIsotopicTaut, pCS->nCanonOrdIsotopicTaut )
1733 : 0 : : ( n = 0, (AT_RANK*) NULL );
1734 : :
1735 : 0 : pConstitEquNumb = pINChI_Aux->nConstitEquIsotopicTGroupNumbers;
1736 : :
1737 : 0 : pSymmRank = pCS->nSymmRankIsotopicTaut;
1738 : :
1739 [ # # # # : 0 : if (pCanonOrdTaut && pSymmRank && pConstitEquNumb && n > 0)
# # # # ]
1740 : : {
1741 [ # # ]: 0 : for (i = 0; i < n; i++)
1742 : : {
1743 : 0 : pConstitEquNumb[i] = pSymmRank[pCanonOrdTaut[i]];
1744 : 0 : pSortOrd[i] = i;
1745 : : }
1746 : :
1747 : 0 : pCG->m_pn_RankForSort = pConstitEquNumb;
1748 : 0 : inchi_qsort( pCG, pSortOrd, n, sizeof( pSortOrd[0] ), CompRanksOrd );
1749 [ # # ]: 0 : for (i = 0, nMinOrd = pSortOrd[0], j = 1; j <= n; j++)
1750 : : {
1751 [ # # # # ]: 0 : if (j == n || pConstitEquNumb[pSortOrd[i]] != pConstitEquNumb[pSortOrd[j]])
1752 : : {
1753 : 0 : nMinOrd++;
1754 [ # # ]: 0 : if (j - i > 1)
1755 : : {
1756 : : /* found a sequence of equivalent t-groups: i..j-1 */
1757 [ # # ]: 0 : while (i < j)
1758 : : {
1759 : 0 : pConstitEquNumb[pSortOrd[i++]] = nMinOrd;
1760 : : }
1761 : : }
1762 : : else
1763 : : {
1764 : 0 : pConstitEquNumb[pSortOrd[i++]] = 0; /* nMinOrd; */
1765 : : }
1766 : 0 : nMinOrd = pSortOrd[j]; /* at the end j = n */
1767 : : }
1768 : : }
1769 : : }
1770 : : }
1771 : :
1772 : :
1773 : 10 : exit_function:
1774 : :
1775 [ + - ]: 1265 : if (pCanonRankAtoms)
1776 : : {
1777 [ + - ]: 1265 : inchi_free( pCanonRankAtoms );
1778 : : }
1779 [ + - ]: 1265 : if (pSortOrd)
1780 : : {
1781 [ + - ]: 1265 : inchi_free( pSortOrd );
1782 : : }
1783 : :
1784 : 1265 : pINChI->nErrorCode |= nErrorCode;
1785 : 1265 : pINChI_Aux->nErrorCode |= nErrorCode;
1786 : :
1787 : 1265 : return ret;
1788 : : }
|