Coverage Report

Created: 2026-12-01 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/src/init.cpp
Line
Count
Source
1
/******************************************************************************
2
 * Project:  PROJ.4
3
 * Purpose:  Initialize projection object from string definition.  Includes
4
 *           pj_init(), and pj_init_plus() function.
5
 * Author:   Gerald Evenden, Frank Warmerdam <warmerdam@pobox.com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1995, Gerald Evenden
9
 * Copyright (c) 2002, Frank Warmerdam <warmerdam@pobox.com>
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a
12
 * copy of this software and associated documentation files (the "Software"),
13
 * to deal in the Software without restriction, including without limitation
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
 * and/or sell copies of the Software, and to permit persons to whom the
16
 * Software is furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included
19
 * in all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
 * DEALINGS IN THE SOFTWARE.
28
 *****************************************************************************/
29
30
#include <ctype.h>
31
#include <math.h>
32
#include <stddef.h>
33
#include <stdio.h>
34
#include <string.h>
35
36
#include "filemanager.hpp"
37
#include "geodesic.h"
38
#include "proj.h"
39
#include "proj_internal.h"
40
#include <math.h>
41
42
/**************************************************************************************/
43
471
static paralist *string_to_paralist(PJ_CONTEXT *ctx, char *definition) {
44
    /***************************************************************************************
45
        Convert a string (presumably originating from get_init_string) to a
46
    paralist.
47
    ***************************************************************************************/
48
471
    const char *c = definition;
49
471
    paralist *first = nullptr, *last = nullptr;
50
51
2.78k
    while (*c) {
52
        /* Keep a handle to the start of the list, so we have something to
53
         * return */
54
2.31k
        auto param = pj_mkparam_ws(c, &c);
55
2.31k
        if (nullptr == param) {
56
0
            free_params(ctx, first, PROJ_ERR_OTHER /*ENOMEM*/);
57
0
            return nullptr;
58
0
        }
59
2.31k
        if (nullptr == last) {
60
471
            first = param;
61
1.84k
        } else {
62
1.84k
            last->next = param;
63
1.84k
        }
64
2.31k
        last = param;
65
2.31k
    }
66
471
    return first;
67
471
}
68
69
/**************************************************************************************/
70
848
static char *get_init_string(PJ_CONTEXT *ctx, const char *name) {
71
/***************************************************************************************
72
    Read a section of an init file. Return its contents as a plain character
73
string. It is the duty of the caller to free the memory allocated for the
74
string.
75
***************************************************************************************/
76
13.6k
#define MAX_LINE_LENGTH 1000
77
848
    size_t current_buffer_size = 5 * (MAX_LINE_LENGTH + 1);
78
848
    char *fname, *section;
79
848
    const char *key;
80
848
    char *buffer = nullptr;
81
848
    size_t n;
82
83
848
    fname = static_cast<char *>(malloc(MAX_PATH_FILENAME + ID_TAG_MAX + 3));
84
848
    if (nullptr == fname) {
85
0
        return nullptr;
86
0
    }
87
88
    /* Support "init=file:section", "+init=file:section", and "file:section"
89
     * format */
90
848
    key = strstr(name, "init=");
91
848
    if (nullptr == key)
92
676
        key = name;
93
172
    else
94
172
        key += 5;
95
848
    if (MAX_PATH_FILENAME + ID_TAG_MAX + 2 < strlen(key)) {
96
6
        free(fname);
97
6
        return nullptr;
98
6
    }
99
842
    memmove(fname, key, strlen(key) + 1);
100
101
    /* Locate the name of the section we search for */
102
842
    section = strrchr(fname, ':');
103
842
    if (nullptr == section) {
104
194
        pj_log(ctx, PJ_LOG_ERROR, _("Missing colon in +init"));
105
194
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
106
194
        free(fname);
107
194
        return nullptr;
108
194
    }
109
648
    *section = 0;
110
648
    section++;
111
648
    n = strlen(section);
112
648
    pj_log(ctx, PJ_LOG_TRACE,
113
648
           "get_init_string: searching for section [%s] in init file [%s]",
114
648
           section, fname);
115
116
648
    auto file = NS_PROJ::FileManager::open_resource_file(ctx, fname);
117
648
    if (nullptr == file) {
118
81
        pj_log(ctx, PJ_LOG_ERROR, _("Cannot open %s"), fname);
119
81
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
120
81
        free(fname);
121
81
        return nullptr;
122
81
    }
123
124
    /* Search for section in init file */
125
567
    std::string line;
126
12.6k
    for (;;) {
127
128
12.6k
        bool eofReached = false;
129
12.6k
        bool maxLenReached = false;
130
12.6k
        line = file->read_line(MAX_LINE_LENGTH, maxLenReached, eofReached);
131
        /* End of file? */
132
12.6k
        if (maxLenReached || eofReached) {
133
96
            pj_log(ctx, PJ_LOG_ERROR, _("Invalid content for %s"), fname);
134
96
            proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
135
96
            free(fname);
136
96
            return nullptr;
137
96
        }
138
139
        /* At start of right section? */
140
12.5k
        pj_chomp(&line[0]);
141
12.5k
        if ('<' != line[0])
142
10.7k
            continue;
143
1.77k
        if (strlen(line.c_str()) < n + 2)
144
13
            continue;
145
1.76k
        if (line[n + 1] != '>')
146
1.16k
            continue;
147
599
        if (0 == strncmp(line.data() + 1, section, n))
148
471
            break;
149
599
    }
150
151
    /* We're at the first line of the right section - copy line to buffer */
152
471
    buffer = static_cast<char *>(malloc(current_buffer_size));
153
471
    if (nullptr == buffer) {
154
0
        free(fname);
155
0
        return nullptr;
156
0
    }
157
158
    /* Skip the "<section>" indicator, and copy the rest of the line over */
159
471
    strcpy(buffer, line.data() + strlen(section) + 2);
160
161
    /* Copy the remaining lines of the section to buffer */
162
645
    for (;;) {
163
645
        char *end_i_cator;
164
645
        size_t next_length, buffer_length;
165
166
        /* Did the section end somewhere in the most recently read line? */
167
645
        end_i_cator = strchr(buffer, '<');
168
645
        if (end_i_cator) {
169
471
            *end_i_cator = 0;
170
471
            break;
171
471
        }
172
173
174
        bool eofReached = false;
174
174
        bool maxLenReached = false;
175
174
        line = file->read_line(MAX_LINE_LENGTH, maxLenReached, eofReached);
176
        /* End of file? - done! */
177
174
        if (maxLenReached || eofReached)
178
0
            break;
179
180
        /* Otherwise, handle the line. It MAY be the start of the next section,
181
         */
182
        /* but that will be handled at the start of next trip through the loop
183
         */
184
174
        buffer_length = strlen(buffer);
185
174
        pj_chomp(&line[0]); /* Remove '#' style comments */
186
174
        next_length = strlen(line.data()) + buffer_length + 2;
187
174
        if (next_length > current_buffer_size) {
188
0
            char *b = static_cast<char *>(malloc(2 * current_buffer_size));
189
0
            if (nullptr == b) {
190
0
                free(buffer);
191
0
                buffer = nullptr;
192
0
                break;
193
0
            }
194
0
            strcpy(b, buffer);
195
0
            current_buffer_size *= 2;
196
0
            free(buffer);
197
0
            buffer = b;
198
0
        }
199
174
        buffer[buffer_length] = ' ';
200
174
        strcpy(buffer + buffer_length + 1, line.data());
201
174
    }
202
203
471
    free(fname);
204
471
    if (nullptr == buffer)
205
0
        return nullptr;
206
471
    pj_shrink(buffer);
207
471
    pj_log(ctx, PJ_LOG_TRACE, "key=%s, value: [%s]", key, buffer);
208
471
    return buffer;
209
471
}
210
211
/************************************************************************/
212
static paralist *get_init(PJ_CONTEXT *ctx, const char *key,
213
1.69k
                          int allow_init_epsg) {
214
    /*************************************************************************
215
    Expand key from buffer or (if not in buffer) from init file
216
    *************************************************************************/
217
1.69k
    const char *xkey;
218
1.69k
    char *definition = nullptr;
219
1.69k
    paralist *init_items = nullptr;
220
221
1.69k
    if (!ctx) {
222
0
        ctx = pj_get_default_ctx();
223
0
    }
224
225
    /* support "init=file:section", "+init=file:section", and "file:section"
226
     * format */
227
1.69k
    xkey = strstr(key, "init=");
228
1.69k
    if (nullptr == xkey)
229
3
        xkey = key;
230
1.69k
    else
231
1.69k
        xkey += 5;
232
1.69k
    pj_log(ctx, PJ_LOG_TRACE, "get_init: searching cache for key: [%s]", xkey);
233
234
    /* Is file/key pair already in cache? */
235
1.69k
    init_items = pj_search_initcache(xkey);
236
1.69k
    if (init_items)
237
839
        return init_items;
238
239
857
    if ((strncmp(xkey, "epsg:", 5) == 0 || strncmp(xkey, "IGNF:", 5) == 0)) {
240
9
        char unused[256];
241
9
        char initname[5];
242
9
        int exists;
243
244
9
        strncpy(initname, xkey, 4);
245
9
        initname[4] = 0;
246
247
9
        if (strncmp(xkey, "epsg:", 5) == 0) {
248
8
            exists = ctx->epsg_file_exists;
249
8
            if (exists < 0) {
250
1
                exists = pj_find_file(ctx, initname, unused, sizeof(unused),
251
1
                                      /* disable_network = */ true);
252
1
                ctx->epsg_file_exists = exists;
253
1
            }
254
8
        } else {
255
1
            exists = pj_find_file(ctx, initname, unused, sizeof(unused),
256
1
                                  /* disable_network = */ true);
257
1
        }
258
259
9
        if (!exists) {
260
9
            char szInitStr[7 + 64];
261
9
            PJ *src;
262
9
            const char *proj_string;
263
264
9
            proj_context_errno_set(ctx, 0);
265
266
9
            if (!allow_init_epsg) {
267
9
                pj_log(ctx, PJ_LOG_TRACE, "%s expansion disallowed", xkey);
268
9
                return nullptr;
269
9
            }
270
0
            if (strlen(xkey) > 64) {
271
0
                return nullptr;
272
0
            }
273
0
            strcpy(szInitStr, "+init=");
274
0
            strcat(szInitStr, xkey);
275
276
0
            auto old_proj4_init_rules = ctx->use_proj4_init_rules;
277
0
            ctx->use_proj4_init_rules = true;
278
0
            src = proj_create(ctx, szInitStr);
279
0
            ctx->use_proj4_init_rules = old_proj4_init_rules;
280
0
            if (!src) {
281
0
                return nullptr;
282
0
            }
283
284
0
            proj_string = proj_as_proj_string(ctx, src, PJ_PROJ_4, nullptr);
285
0
            if (!proj_string) {
286
0
                proj_destroy(src);
287
0
                return nullptr;
288
0
            }
289
0
            definition = (char *)calloc(1, strlen(proj_string) + 1);
290
0
            if (definition) {
291
0
                strcpy(definition, proj_string);
292
0
            }
293
294
0
            proj_destroy(src);
295
0
        }
296
9
    }
297
298
848
    if (!definition) {
299
        /* If not, we must read it from file */
300
848
        pj_log(ctx, PJ_LOG_TRACE,
301
848
               "get_init: searching on in init files for [%s]", xkey);
302
848
        definition = get_init_string(ctx, xkey);
303
848
    }
304
305
848
    if (nullptr == definition)
306
377
        return nullptr;
307
471
    init_items = string_to_paralist(ctx, definition);
308
471
    if (init_items)
309
471
        pj_log(ctx, PJ_LOG_TRACE, "get_init: got [%s], paralist[0,1]: [%s,%s]",
310
471
               definition, init_items->param,
311
471
               init_items->next ? init_items->next->param : "(empty)");
312
471
    free(definition);
313
471
    if (nullptr == init_items)
314
0
        return nullptr;
315
316
    /* We found it in file - now insert into the cache, before returning */
317
471
    pj_insert_initcache(xkey, init_items);
318
471
    return init_items;
319
471
}
320
321
605k
static void append_default_ellipsoid_to_paralist(paralist *start) {
322
605k
    if (nullptr == start)
323
0
        return;
324
325
    /* Set defaults, unless inhibited (either explicitly through a "no_defs"
326
     * token
327
     */
328
    /* or implicitly, because we are initializing a pipeline) */
329
605k
    if (pj_param_exists(start, "no_defs"))
330
1.16k
        return;
331
604k
    auto proj = pj_param_exists(start, "proj");
332
604k
    if (nullptr == proj)
333
0
        return;
334
604k
    if (strlen(proj->param) < 6)
335
0
        return;
336
604k
    if (0 == strcmp("pipeline", proj->param + 5))
337
56.6k
        return;
338
339
    /* Don't default ellipse if datum, ellps or any ellipsoid information is set
340
     */
341
547k
    if (pj_param_exists(start, "datum"))
342
11.4k
        return;
343
536k
    if (pj_param_exists(start, "ellps"))
344
181k
        return;
345
354k
    if (pj_param_exists(start, "a"))
346
76.4k
        return;
347
277k
    if (pj_param_exists(start, "b"))
348
167
        return;
349
277k
    if (pj_param_exists(start, "rf"))
350
204
        return;
351
277k
    if (pj_param_exists(start, "f"))
352
205
        return;
353
277k
    if (pj_param_exists(start, "e"))
354
598
        return;
355
276k
    if (pj_param_exists(start, "es"))
356
98
        return;
357
358
    /* Locate end of start-list */
359
276k
    paralist *last = nullptr;
360
2.46M
    for (last = start; last->next; last = last->next)
361
2.19M
        ;
362
363
    /* If we're here, it's OK to append the current default item */
364
276k
    last->next = pj_mkparam("ellps=GRS80");
365
276k
}
366
367
/*****************************************************************************/
368
static paralist *pj_expand_init_internal(PJ_CONTEXT *ctx, paralist *init,
369
1.69k
                                         int allow_init_epsg) {
370
    /******************************************************************************
371
    Append expansion of <key> to the paralist <init>. The expansion is appended,
372
    rather than inserted at <init>'s place, since <init> may contain
373
    overrides to the expansion. These must take precedence, and hence come first
374
    in the expanded list.
375
376
    Consider e.g. the key 'foo:bar' which (hypothetically) expands to 'proj=utm
377
    zone=32 ellps=GRS80', i.e. a UTM projection on the GRS80 ellipsoid.
378
379
    The expression 'init=foo:bar ellps=intl' will then expand to:
380
381
               'init=foo:bar ellps=intl proj=utm zone=32 ellps=GRS80',
382
383
    where 'ellps=intl' precedes 'ellps=GRS80', and hence takes precedence,
384
    turning the expansion into an UTM projection on the Hayford ellipsoid.
385
386
    Note that 'init=foo:bar' stays in the list. It is ignored after expansion.
387
388
    ******************************************************************************/
389
1.69k
    paralist *last;
390
1.69k
    paralist *expn;
391
392
    /* Nowhere to start? */
393
1.69k
    if (nullptr == init)
394
0
        return nullptr;
395
396
1.69k
    expn = get_init(ctx, init->param, allow_init_epsg);
397
398
    /* Nothing in expansion? */
399
1.69k
    if (nullptr == expn)
400
386
        return nullptr;
401
402
    /* Locate  the end of the list */
403
8.06k
    for (last = init; last && last->next; last = last->next)
404
6.75k
        ;
405
406
    /* Then append and return */
407
1.31k
    last->next = expn;
408
1.31k
    return init;
409
1.69k
}
410
411
823
paralist *pj_expand_init(PJ_CONTEXT *ctx, paralist *init) {
412
823
    return pj_expand_init_internal(ctx, init, TRUE);
413
823
}
414
415
/************************************************************************/
416
/*                              pj_init()                               */
417
/*                                                                      */
418
/*      Main entry point for initialing a PJ projections                */
419
/*      definition.  Note that the projection specific function is      */
420
/*      called to do the initial allocation so it can be created        */
421
/*      large enough to hold projection specific parameters.            */
422
/************************************************************************/
423
424
606k
static PJ_CONSTRUCTOR locate_constructor(const char *name) {
425
606k
    int i;
426
606k
    const char *s;
427
606k
    const PJ_OPERATIONS *operations;
428
606k
    operations = proj_list_operations();
429
48.4M
    for (i = 0; (s = operations[i].id) && strcmp(name, s); ++i)
430
47.8M
        ;
431
606k
    if (nullptr == s)
432
1.56k
        return nullptr;
433
605k
    return (PJ_CONSTRUCTOR)operations[i].proj;
434
606k
}
435
436
PJ *pj_init_ctx_with_allow_init_epsg(PJ_CONTEXT *ctx, int argc, char **argv,
437
608k
                                     int allow_init_epsg) {
438
608k
    const char *s;
439
608k
    char *name;
440
608k
    PJ_CONSTRUCTOR proj;
441
608k
    paralist *curr, *init, *start;
442
608k
    int i;
443
608k
    int err;
444
608k
    PJ *PIN = nullptr;
445
608k
    int n_pipelines = 0;
446
608k
    int n_inits = 0;
447
608k
    const PJ_UNITS *units;
448
608k
    const PJ_PRIME_MERIDIANS *prime_meridians;
449
450
608k
    if (nullptr == ctx)
451
0
        ctx = pj_get_default_ctx();
452
453
608k
    ctx->last_errno = 0;
454
455
608k
    if (argc <= 0) {
456
5
        pj_log(ctx, PJ_LOG_ERROR, _("No arguments"));
457
5
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_MISSING_ARG);
458
5
        return nullptr;
459
5
    }
460
461
    /* count occurrences of pipelines and inits */
462
8.44M
    for (i = 0; i < argc; ++i) {
463
7.83M
        if (!strcmp(argv[i], "+proj=pipeline") ||
464
7.83M
            !strcmp(argv[i], "proj=pipeline"))
465
57.6k
            n_pipelines++;
466
7.83M
        if (!strncmp(argv[i], "+init=", 6) || !strncmp(argv[i], "init=", 5))
467
2.34k
            n_inits++;
468
7.83M
    }
469
470
    /* can't have nested pipelines directly */
471
608k
    if (n_pipelines > 1) {
472
80
        pj_log(ctx, PJ_LOG_ERROR, _("Nested pipelines are not supported"));
473
80
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
474
80
        return nullptr;
475
80
    }
476
477
    /* don't allow more than one +init in non-pipeline operations */
478
608k
    if (n_pipelines == 0 && n_inits > 1) {
479
129
        pj_log(ctx, PJ_LOG_ERROR, _("Too many inits"));
480
129
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
481
129
        return nullptr;
482
129
    }
483
484
    /* put arguments into internal linked list */
485
608k
    start = curr = pj_mkparam(argv[0]);
486
608k
    if (!curr) {
487
0
        free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
488
0
        return nullptr;
489
0
    }
490
491
7.82M
    for (i = 1; i < argc; ++i) {
492
7.21M
        curr->next = pj_mkparam(argv[i]);
493
7.21M
        if (!curr->next) {
494
0
            free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
495
0
            return nullptr;
496
0
        }
497
7.21M
        curr = curr->next;
498
7.21M
    }
499
500
    /* Only expand '+init's in non-pipeline operations. '+init's in pipelines
501
     * are
502
     */
503
    /* expanded in the individual pipeline steps during pipeline initialization.
504
     */
505
    /* Potentially this leads to many nested pipelines, which shouldn't be a */
506
    /* problem when '+init's are expanded as late as possible. */
507
608k
    init = pj_param_exists(start, "init");
508
608k
    if (init && n_pipelines == 0) {
509
873
        init = pj_expand_init_internal(ctx, init, allow_init_epsg);
510
873
        if (!init) {
511
210
            free_params(ctx, start, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
512
210
            return nullptr;
513
210
        }
514
873
    }
515
607k
    if (ctx->last_errno) {
516
0
        free_params(ctx, start, ctx->last_errno);
517
0
        return nullptr;
518
0
    }
519
520
    /* Find projection selection */
521
607k
    curr = pj_param_exists(start, "proj");
522
607k
    if (nullptr == curr) {
523
788
        pj_log(ctx, PJ_LOG_ERROR, _("Missing proj"));
524
788
        free_params(ctx, start, PROJ_ERR_INVALID_OP_MISSING_ARG);
525
788
        return nullptr;
526
788
    }
527
607k
    name = curr->param;
528
607k
    if (strlen(name) < 6) {
529
144
        pj_log(ctx, PJ_LOG_ERROR, _("Invalid value for proj"));
530
144
        free_params(ctx, start, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
531
144
        return nullptr;
532
144
    }
533
606k
    name += 5;
534
535
606k
    proj = locate_constructor(name);
536
606k
    if (nullptr == proj) {
537
1.56k
        pj_log(ctx, PJ_LOG_ERROR, _("Unknown projection"));
538
1.56k
        free_params(ctx, start, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
539
1.56k
        return nullptr;
540
1.56k
    }
541
542
605k
    append_default_ellipsoid_to_paralist(start);
543
544
    /* Allocate projection structure */
545
605k
    PIN = proj(nullptr);
546
605k
    if (nullptr == PIN) {
547
0
        free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
548
0
        return nullptr;
549
0
    }
550
551
605k
    PIN->ctx = ctx;
552
605k
    PIN->params = start;
553
605k
    PIN->is_latlong = 0;
554
605k
    PIN->is_geocent = 0;
555
605k
    PIN->is_long_wrap_set = 0;
556
605k
    PIN->long_wrap_center = 0.0;
557
605k
    strcpy(PIN->axis, "enu");
558
559
    /* Set datum parameters. Similarly to +init parameters we want to expand */
560
    /* +datum parameters as late as possible when dealing with pipelines. */
561
    /* otherwise only the first occurrence of +datum will be expanded and that
562
     */
563
605k
    if (n_pipelines == 0) {
564
547k
        if (pj_datum_set(ctx, start, PIN))
565
399
            return pj_default_destructor(PIN, proj_errno(PIN));
566
547k
    }
567
568
605k
    err = pj_ellipsoid(PIN);
569
570
605k
    if (err) {
571
        /* Didn't get an ellps, but doesn't need one: Get a free WGS84 */
572
9.26k
        if (PIN->need_ellps) {
573
546
            pj_log(ctx, PJ_LOG_ERROR,
574
546
                   _("pj_init_ctx: Must specify ellipsoid or sphere"));
575
546
            return pj_default_destructor(PIN, proj_errno(PIN));
576
8.71k
        } else {
577
8.71k
            if (PIN->a == 0)
578
8.40k
                proj_errno_reset(PIN);
579
8.71k
            PIN->f = 1.0 / 298.257223563;
580
8.71k
            PIN->a = 6378137.0;
581
8.71k
            PIN->es = PIN->f * (2 - PIN->f);
582
8.71k
        }
583
9.26k
    }
584
604k
    PIN->a_orig = PIN->a;
585
604k
    PIN->es_orig = PIN->es;
586
604k
    if (pj_calc_ellipsoid_params(PIN, PIN->a, PIN->es))
587
0
        return pj_default_destructor(PIN,
588
0
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
589
590
    /* Now that we have ellipse information check for WGS84 datum */
591
604k
    if (PIN->datum_type == PJD_3PARAM && PIN->datum_params[0] == 0.0 &&
592
14.1k
        PIN->datum_params[1] == 0.0 && PIN->datum_params[2] == 0.0 &&
593
4.13k
        PIN->a == 6378137.0 &&
594
3.17k
        ABS(PIN->es - 0.006694379990) < 0.000000000050) /*WGS84/GRS80*/
595
3.01k
    {
596
3.01k
        PIN->datum_type = PJD_WGS84;
597
3.01k
    }
598
599
    /* Set PIN->geoc coordinate system */
600
604k
    PIN->geoc = (PIN->es != 0.0 && pj_param(ctx, start, "bgeoc").i);
601
602
    /* Over-ranging flag */
603
604k
    PIN->over = pj_param(ctx, start, "bover").i;
604
604k
    if (ctx->forceOver) {
605
0
        PIN->over = ctx->forceOver;
606
0
    }
607
608
    /* Vertical datum geoid grids */
609
604k
    PIN->has_geoid_vgrids = pj_param(ctx, start, "tgeoidgrids").i;
610
604k
    if (PIN->has_geoid_vgrids) /* we need to mark it as used. */
611
24.8k
        pj_param(ctx, start, "sgeoidgrids");
612
613
    /* Longitude center for wrapping */
614
604k
    PIN->is_long_wrap_set = pj_param(ctx, start, "tlon_wrap").i;
615
604k
    if (PIN->is_long_wrap_set) {
616
18
        PIN->long_wrap_center = pj_param(ctx, start, "rlon_wrap").f;
617
        /* Don't accept excessive values otherwise we might perform badly */
618
        /* when correcting longitudes around it */
619
        /* The test is written this way to error on long_wrap_center "=" NaN */
620
18
        if (!(fabs(PIN->long_wrap_center) < 10 * M_TWOPI)) {
621
0
            proj_log_error(PIN, _("Invalid value for lon_wrap"));
622
0
            return pj_default_destructor(PIN,
623
0
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
624
0
        }
625
18
    }
626
627
    /* Axis orientation */
628
604k
    if ((pj_param(ctx, start, "saxis").s) != nullptr) {
629
7.07k
        const char *axis_legal = "ewnsud";
630
7.07k
        const char *axis_arg = pj_param(ctx, start, "saxis").s;
631
7.07k
        if (strlen(axis_arg) != 3) {
632
20
            proj_log_error(PIN, _("Invalid value for axis"));
633
20
            return pj_default_destructor(PIN,
634
20
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
635
20
        }
636
637
7.05k
        if (strchr(axis_legal, axis_arg[0]) == nullptr ||
638
7.05k
            strchr(axis_legal, axis_arg[1]) == nullptr ||
639
7.04k
            strchr(axis_legal, axis_arg[2]) == nullptr) {
640
9
            proj_log_error(PIN, _("Invalid value for axis"));
641
9
            return pj_default_destructor(PIN,
642
9
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
643
9
        }
644
645
        /* TODO: it would be nice to validate we don't have on axis repeated */
646
7.04k
        strcpy(PIN->axis, axis_arg);
647
7.04k
    }
648
649
    /* Central meridian */
650
604k
    PIN->lam0 = pj_param(ctx, start, "rlon_0").f;
651
652
    /* Central latitude */
653
604k
    PIN->phi0 = pj_param(ctx, start, "rlat_0").f;
654
604k
    if (fabs(PIN->phi0) > M_HALFPI) {
655
58
        proj_log_error(PIN,
656
58
                       _("Invalid value for lat_0: |lat_0| should be <= 90°"));
657
58
        return pj_default_destructor(PIN,
658
58
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
659
58
    }
660
661
    /* False easting and northing */
662
604k
    PIN->x0 = pj_param(ctx, start, "dx_0").f;
663
604k
    PIN->y0 = pj_param(ctx, start, "dy_0").f;
664
604k
    PIN->z0 = pj_param(ctx, start, "dz_0").f;
665
604k
    PIN->t0 = pj_param(ctx, start, "dt_0").f;
666
667
    /* General scaling factor */
668
604k
    if (pj_param(ctx, start, "tk_0").i)
669
926
        PIN->k0 = pj_param(ctx, start, "dk_0").f;
670
603k
    else if (pj_param(ctx, start, "tk").i)
671
19.9k
        PIN->k0 = pj_param(ctx, start, "dk").f;
672
583k
    else
673
583k
        PIN->k0 = 1.;
674
604k
    if (PIN->k0 <= 0.) {
675
115
        proj_log_error(PIN, _("Invalid value for k/k_0: it should be > 0"));
676
115
        return pj_default_destructor(PIN,
677
115
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
678
115
    }
679
680
    /* Set units */
681
604k
    units = pj_list_linear_units();
682
604k
    s = nullptr;
683
604k
    if ((name = pj_param(ctx, start, "sunits").s) != nullptr) {
684
8.54k
        for (i = 0; (s = units[i].id) && strcmp(name, s); ++i)
685
7.62k
            ;
686
913
        if (!s) {
687
44
            proj_log_error(PIN, _("Invalid value for units"));
688
44
            return pj_default_destructor(PIN,
689
44
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
690
44
        }
691
869
        s = units[i].to_meter;
692
869
    }
693
604k
    if (s || (s = pj_param(ctx, start, "sto_meter").s)) {
694
1.74k
        char *end_ptr = const_cast<char *>(s);
695
1.74k
        PIN->to_meter = pj_strtod(s, &end_ptr);
696
1.74k
        s = end_ptr;
697
1.74k
        if (*s == '/') { /* ratio number */
698
275
            ++s;
699
275
            double denom = pj_strtod(s, nullptr);
700
275
            if (denom == 0.0) {
701
0
                proj_log_error(PIN,
702
0
                               _("Invalid value for to_meter donominator"));
703
0
                return pj_default_destructor(
704
0
                    PIN, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
705
0
            }
706
275
            PIN->to_meter /= denom;
707
275
        }
708
1.74k
        if (PIN->to_meter <= 0.0) {
709
5
            proj_log_error(PIN, _("Invalid value for to_meter"));
710
5
            return pj_default_destructor(PIN,
711
5
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
712
5
        }
713
1.74k
        PIN->fr_meter = 1 / PIN->to_meter;
714
715
1.74k
    } else
716
602k
        PIN->to_meter = PIN->fr_meter = 1.;
717
718
    /* Set vertical units */
719
604k
    s = nullptr;
720
604k
    if ((name = pj_param(ctx, start, "svunits").s) != nullptr) {
721
24.2k
        for (i = 0; (s = units[i].id) && strcmp(name, s); ++i)
722
18.1k
            ;
723
6.12k
        if (!s) {
724
122
            proj_log_error(PIN, _("Invalid value for vunits"));
725
122
            return pj_default_destructor(PIN,
726
122
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
727
122
        }
728
5.99k
        s = units[i].to_meter;
729
5.99k
    }
730
604k
    if (s || (s = pj_param(ctx, start, "svto_meter").s)) {
731
7.34k
        char *end_ptr = const_cast<char *>(s);
732
7.34k
        PIN->vto_meter = pj_strtod(s, &end_ptr);
733
7.34k
        s = end_ptr;
734
7.34k
        if (*s == '/') { /* ratio number */
735
1.97k
            ++s;
736
1.97k
            double denom = pj_strtod(s, nullptr);
737
1.97k
            if (denom == 0.0) {
738
3
                proj_log_error(PIN,
739
3
                               _("Invalid value for vto_meter donominator"));
740
3
                return pj_default_destructor(
741
3
                    PIN, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
742
3
            }
743
1.96k
            PIN->vto_meter /= denom;
744
1.96k
        }
745
7.34k
        if (PIN->vto_meter <= 0.0) {
746
38
            proj_log_error(PIN, _("Invalid value for vto_meter"));
747
38
            return pj_default_destructor(PIN,
748
38
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
749
38
        }
750
7.30k
        PIN->vfr_meter = 1. / PIN->vto_meter;
751
596k
    } else {
752
596k
        PIN->vto_meter = PIN->to_meter;
753
596k
        PIN->vfr_meter = PIN->fr_meter;
754
596k
    }
755
756
    /* Prime meridian */
757
604k
    prime_meridians = proj_list_prime_meridians();
758
604k
    s = nullptr;
759
604k
    if ((name = pj_param(ctx, start, "spm").s) != nullptr) {
760
80.5k
        const char *value = nullptr;
761
80.5k
        char *next_str = nullptr;
762
763
1.20M
        for (i = 0; prime_meridians[i].id != nullptr; ++i) {
764
1.12M
            if (strcmp(name, prime_meridians[i].id) == 0) {
765
317
                value = prime_meridians[i].defn;
766
317
                break;
767
317
            }
768
1.12M
        }
769
770
80.5k
        if (value == nullptr &&
771
80.2k
            (dmstor_ctx(ctx, name, &next_str) != 0.0 || *name == '0') &&
772
80.0k
            *next_str == '\0')
773
79.9k
            value = name;
774
775
80.5k
        if (!value) {
776
273
            proj_log_error(PIN, _("Invalid value for pm"));
777
273
            return pj_default_destructor(PIN,
778
273
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
779
273
        }
780
80.3k
        PIN->from_greenwich = dmstor_ctx(ctx, value, nullptr);
781
80.3k
    } else
782
523k
        PIN->from_greenwich = 0.0;
783
784
    /* Private object for the geodesic functions */
785
603k
    PIN->geod = static_cast<struct geod_geodesic *>(
786
603k
        calloc(1, sizeof(struct geod_geodesic)));
787
603k
    if (nullptr == PIN->geod)
788
0
        return pj_default_destructor(PIN, PROJ_ERR_OTHER /*ENOMEM*/);
789
603k
    geod_init(PIN->geod, PIN->a, PIN->f);
790
791
    /* Projection specific initialization */
792
603k
    err = proj_errno_reset(PIN);
793
603k
    PIN = proj(PIN);
794
603k
    if (proj_errno(PIN)) {
795
64.8k
        proj_destroy(PIN);
796
64.8k
        return nullptr;
797
64.8k
    }
798
538k
    proj_errno_restore(PIN, err);
799
538k
    return PIN;
800
603k
}