Coverage Report

Created: 2025-06-13 06:29

/src/proj/src/init.cpp
Line
Count
Source (jump to first uncovered line)
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
170
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
170
    const char *c = definition;
49
170
    paralist *first = nullptr, *last = nullptr;
50
51
554
    while (*c) {
52
        /* Keep a handle to the start of the list, so we have something to
53
         * return */
54
384
        auto param = pj_mkparam_ws(c, &c);
55
384
        if (nullptr == param) {
56
0
            free_params(ctx, first, PROJ_ERR_OTHER /*ENOMEM*/);
57
0
            return nullptr;
58
0
        }
59
384
        if (nullptr == last) {
60
170
            first = param;
61
214
        } else {
62
214
            last->next = param;
63
214
        }
64
384
        last = param;
65
384
    }
66
170
    return first;
67
170
}
68
69
/**************************************************************************************/
70
560
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
7.52k
#define MAX_LINE_LENGTH 1000
77
560
    size_t current_buffer_size = 5 * (MAX_LINE_LENGTH + 1);
78
560
    char *fname, *section;
79
560
    const char *key;
80
560
    char *buffer = nullptr;
81
560
    size_t n;
82
83
560
    fname = static_cast<char *>(malloc(MAX_PATH_FILENAME + ID_TAG_MAX + 3));
84
560
    if (nullptr == fname) {
85
0
        return nullptr;
86
0
    }
87
88
    /* Support "init=file:section", "+init=file:section", and "file:section"
89
     * format */
90
560
    key = strstr(name, "init=");
91
560
    if (nullptr == key)
92
503
        key = name;
93
57
    else
94
57
        key += 5;
95
560
    if (MAX_PATH_FILENAME + ID_TAG_MAX + 2 < strlen(key)) {
96
21
        free(fname);
97
21
        return nullptr;
98
21
    }
99
539
    memmove(fname, key, strlen(key) + 1);
100
101
    /* Locate the name of the section we search for */
102
539
    section = strrchr(fname, ':');
103
539
    if (nullptr == section) {
104
142
        pj_log(ctx, PJ_LOG_ERROR, _("Missing colon in +init"));
105
142
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
106
142
        free(fname);
107
142
        return nullptr;
108
142
    }
109
397
    *section = 0;
110
397
    section++;
111
397
    n = strlen(section);
112
397
    pj_log(ctx, PJ_LOG_TRACE,
113
397
           "get_init_string: searching for section [%s] in init file [%s]",
114
397
           section, fname);
115
116
397
    auto file = NS_PROJ::FileManager::open_resource_file(ctx, fname);
117
397
    if (nullptr == file) {
118
376
        pj_log(ctx, PJ_LOG_ERROR, _("Cannot open %s"), fname);
119
376
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
120
376
        free(fname);
121
376
        return nullptr;
122
376
    }
123
124
    /* Search for section in init file */
125
21
    std::string line;
126
6.96k
    for (;;) {
127
128
6.96k
        bool eofReached = false;
129
6.96k
        bool maxLenReached = false;
130
6.96k
        line = file->read_line(MAX_LINE_LENGTH, maxLenReached, eofReached);
131
        /* End of file? */
132
6.96k
        if (maxLenReached || eofReached) {
133
21
            pj_log(ctx, PJ_LOG_ERROR, _("Invalid content for %s"), fname);
134
21
            proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
135
21
            free(fname);
136
21
            return nullptr;
137
21
        }
138
139
        /* At start of right section? */
140
6.94k
        pj_chomp(&line[0]);
141
6.94k
        if ('<' != line[0])
142
5.71k
            continue;
143
1.23k
        if (strlen(line.c_str()) < n + 2)
144
378
            continue;
145
852
        if (line[n + 1] != '>')
146
711
            continue;
147
141
        if (0 == strncmp(line.data() + 1, section, n))
148
0
            break;
149
141
    }
150
151
    /* We're at the first line of the right section - copy line to buffer */
152
0
    buffer = static_cast<char *>(malloc(current_buffer_size));
153
0
    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
0
    strcpy(buffer, line.data() + strlen(section) + 2);
160
161
    /* Copy the remaining lines of the section to buffer */
162
0
    for (;;) {
163
0
        char *end_i_cator;
164
0
        size_t next_length, buffer_length;
165
166
        /* Did the section end somewhere in the most recently read line? */
167
0
        end_i_cator = strchr(buffer, '<');
168
0
        if (end_i_cator) {
169
0
            *end_i_cator = 0;
170
0
            break;
171
0
        }
172
173
0
        bool eofReached = false;
174
0
        bool maxLenReached = false;
175
0
        line = file->read_line(MAX_LINE_LENGTH, maxLenReached, eofReached);
176
        /* End of file? - done! */
177
0
        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
0
        buffer_length = strlen(buffer);
185
0
        pj_chomp(&line[0]); /* Remove '#' style comments */
186
0
        next_length = strlen(line.data()) + buffer_length + 2;
187
0
        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
0
        buffer[buffer_length] = ' ';
200
0
        strcpy(buffer + buffer_length + 1, line.data());
201
0
    }
202
203
0
    free(fname);
204
0
    if (nullptr == buffer)
205
0
        return nullptr;
206
0
    pj_shrink(buffer);
207
0
    pj_log(ctx, PJ_LOG_TRACE, "key=%s, value: [%s]", key, buffer);
208
0
    return buffer;
209
0
}
210
211
/************************************************************************/
212
static paralist *get_init(PJ_CONTEXT *ctx, const char *key,
213
17.4k
                          int allow_init_epsg) {
214
    /*************************************************************************
215
    Expand key from buffer or (if not in buffer) from init file
216
    *************************************************************************/
217
17.4k
    const char *xkey;
218
17.4k
    char *definition = nullptr;
219
17.4k
    paralist *init_items = nullptr;
220
221
17.4k
    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
17.4k
    xkey = strstr(key, "init=");
228
17.4k
    if (nullptr == xkey)
229
11
        xkey = key;
230
17.4k
    else
231
17.4k
        xkey += 5;
232
17.4k
    pj_log(ctx, PJ_LOG_TRACE, "get_init: searching cache for key: [%s]", xkey);
233
234
    /* Is file/key pair already in cache? */
235
17.4k
    init_items = pj_search_initcache(xkey);
236
17.4k
    if (init_items)
237
16.1k
        return init_items;
238
239
1.35k
    if ((strncmp(xkey, "epsg:", 5) == 0 || strncmp(xkey, "IGNF:", 5) == 0)) {
240
792
        char unused[256];
241
792
        char initname[5];
242
792
        int exists;
243
244
792
        strncpy(initname, xkey, 4);
245
792
        initname[4] = 0;
246
247
792
        if (strncmp(xkey, "epsg:", 5) == 0) {
248
347
            exists = ctx->epsg_file_exists;
249
347
            if (exists < 0) {
250
40
                exists = pj_find_file(ctx, initname, unused, sizeof(unused));
251
40
                ctx->epsg_file_exists = exists;
252
40
            }
253
445
        } else {
254
445
            exists = pj_find_file(ctx, initname, unused, sizeof(unused));
255
445
        }
256
257
792
        if (!exists) {
258
792
            char szInitStr[7 + 64];
259
792
            PJ *src;
260
792
            const char *proj_string;
261
262
792
            proj_context_errno_set(ctx, 0);
263
264
792
            if (!allow_init_epsg) {
265
0
                pj_log(ctx, PJ_LOG_TRACE, "%s expansion disallowed", xkey);
266
0
                return nullptr;
267
0
            }
268
792
            if (strlen(xkey) > 64) {
269
238
                return nullptr;
270
238
            }
271
554
            strcpy(szInitStr, "+init=");
272
554
            strcat(szInitStr, xkey);
273
274
554
            auto old_proj4_init_rules = ctx->use_proj4_init_rules;
275
554
            ctx->use_proj4_init_rules = true;
276
554
            src = proj_create(ctx, szInitStr);
277
554
            ctx->use_proj4_init_rules = old_proj4_init_rules;
278
554
            if (!src) {
279
372
                return nullptr;
280
372
            }
281
282
182
            proj_string = proj_as_proj_string(ctx, src, PJ_PROJ_4, nullptr);
283
182
            if (!proj_string) {
284
12
                proj_destroy(src);
285
12
                return nullptr;
286
12
            }
287
170
            definition = (char *)calloc(1, strlen(proj_string) + 1);
288
170
            if (definition) {
289
170
                strcpy(definition, proj_string);
290
170
            }
291
292
170
            proj_destroy(src);
293
170
        }
294
792
    }
295
296
730
    if (!definition) {
297
        /* If not, we must read it from file */
298
560
        pj_log(ctx, PJ_LOG_TRACE,
299
560
               "get_init: searching on in init files for [%s]", xkey);
300
560
        definition = get_init_string(ctx, xkey);
301
560
    }
302
303
730
    if (nullptr == definition)
304
560
        return nullptr;
305
170
    init_items = string_to_paralist(ctx, definition);
306
170
    if (init_items)
307
170
        pj_log(ctx, PJ_LOG_TRACE, "get_init: got [%s], paralist[0,1]: [%s,%s]",
308
170
               definition, init_items->param,
309
170
               init_items->next ? init_items->next->param : "(empty)");
310
170
    free(definition);
311
170
    if (nullptr == init_items)
312
0
        return nullptr;
313
314
    /* We found it in file - now insert into the cache, before returning */
315
170
    pj_insert_initcache(xkey, init_items);
316
170
    return init_items;
317
170
}
318
319
131k
static void append_default_ellipsoid_to_paralist(paralist *start) {
320
131k
    if (nullptr == start)
321
0
        return;
322
323
    /* Set defaults, unless inhibited (either explicitly through a "no_defs"
324
     * token
325
     */
326
    /* or implicitly, because we are initializing a pipeline) */
327
131k
    if (pj_param_exists(start, "no_defs"))
328
12.9k
        return;
329
118k
    auto proj = pj_param_exists(start, "proj");
330
118k
    if (nullptr == proj)
331
0
        return;
332
118k
    if (strlen(proj->param) < 6)
333
0
        return;
334
118k
    if (0 == strcmp("pipeline", proj->param + 5))
335
9.13k
        return;
336
337
    /* Don't default ellipse if datum, ellps or any ellipsoid information is set
338
     */
339
109k
    if (pj_param_exists(start, "datum"))
340
802
        return;
341
108k
    if (pj_param_exists(start, "ellps"))
342
26.9k
        return;
343
81.5k
    if (pj_param_exists(start, "a"))
344
17.2k
        return;
345
64.2k
    if (pj_param_exists(start, "b"))
346
100
        return;
347
64.1k
    if (pj_param_exists(start, "rf"))
348
81
        return;
349
64.0k
    if (pj_param_exists(start, "f"))
350
1.40k
        return;
351
62.6k
    if (pj_param_exists(start, "e"))
352
356
        return;
353
62.3k
    if (pj_param_exists(start, "es"))
354
1.06k
        return;
355
356
    /* Locate end of start-list */
357
61.2k
    paralist *last = nullptr;
358
761k
    for (last = start; last->next; last = last->next)
359
699k
        ;
360
361
    /* If we're here, it's OK to append the current default item */
362
61.2k
    last->next = pj_mkparam("ellps=GRS80");
363
61.2k
}
364
365
/*****************************************************************************/
366
static paralist *pj_expand_init_internal(PJ_CONTEXT *ctx, paralist *init,
367
17.4k
                                         int allow_init_epsg) {
368
    /******************************************************************************
369
    Append expansion of <key> to the paralist <init>. The expansion is appended,
370
    rather than inserted at <init>'s place, since <init> may contain
371
    overrides to the expansion. These must take precedence, and hence come first
372
    in the expanded list.
373
374
    Consider e.g. the key 'foo:bar' which (hypothetically) expands to 'proj=utm
375
    zone=32 ellps=GRS80', i.e. a UTM projection on the GRS80 ellipsoid.
376
377
    The expression 'init=foo:bar ellps=intl' will then expand to:
378
379
               'init=foo:bar ellps=intl proj=utm zone=32 ellps=GRS80',
380
381
    where 'ellps=intl' precedes 'ellps=GRS80', and hence takes precedence,
382
    turning the expansion into an UTM projection on the Hayford ellipsoid.
383
384
    Note that 'init=foo:bar' stays in the list. It is ignored after expansion.
385
386
    ******************************************************************************/
387
17.4k
    paralist *last;
388
17.4k
    paralist *expn;
389
390
    /* Nowhere to start? */
391
17.4k
    if (nullptr == init)
392
0
        return nullptr;
393
394
17.4k
    expn = get_init(ctx, init->param, allow_init_epsg);
395
396
    /* Nothing in expansion? */
397
17.4k
    if (nullptr == expn)
398
1.18k
        return nullptr;
399
400
    /* Locate  the end of the list */
401
90.5k
    for (last = init; last && last->next; last = last->next)
402
74.2k
        ;
403
404
    /* Then append and return */
405
16.3k
    last->next = expn;
406
16.3k
    return init;
407
17.4k
}
408
409
1.24k
paralist *pj_expand_init(PJ_CONTEXT *ctx, paralist *init) {
410
1.24k
    return pj_expand_init_internal(ctx, init, TRUE);
411
1.24k
}
412
413
/************************************************************************/
414
/*                              pj_init()                               */
415
/*                                                                      */
416
/*      Main entry point for initialing a PJ projections                */
417
/*      definition.  Note that the projection specific function is      */
418
/*      called to do the initial allocation so it can be created        */
419
/*      large enough to hold projection specific parameters.            */
420
/************************************************************************/
421
422
132k
static PJ_CONSTRUCTOR locate_constructor(const char *name) {
423
132k
    int i;
424
132k
    const char *s;
425
132k
    const PJ_OPERATIONS *operations;
426
132k
    operations = proj_list_operations();
427
11.2M
    for (i = 0; (s = operations[i].id) && strcmp(name, s); ++i)
428
11.1M
        ;
429
132k
    if (nullptr == s)
430
656
        return nullptr;
431
131k
    return (PJ_CONSTRUCTOR)operations[i].proj;
432
132k
}
433
434
PJ *pj_init_ctx_with_allow_init_epsg(PJ_CONTEXT *ctx, int argc, char **argv,
435
132k
                                     int allow_init_epsg) {
436
132k
    const char *s;
437
132k
    char *name;
438
132k
    PJ_CONSTRUCTOR proj;
439
132k
    paralist *curr, *init, *start;
440
132k
    int i;
441
132k
    int err;
442
132k
    PJ *PIN = nullptr;
443
132k
    int n_pipelines = 0;
444
132k
    int n_inits = 0;
445
132k
    const PJ_UNITS *units;
446
132k
    const PJ_PRIME_MERIDIANS *prime_meridians;
447
448
132k
    if (nullptr == ctx)
449
0
        ctx = pj_get_default_ctx();
450
451
132k
    ctx->last_errno = 0;
452
453
132k
    if (argc <= 0) {
454
15
        pj_log(ctx, PJ_LOG_ERROR, _("No arguments"));
455
15
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_MISSING_ARG);
456
15
        return nullptr;
457
15
    }
458
459
    /* count occurrences of pipelines and inits */
460
1.66M
    for (i = 0; i < argc; ++i) {
461
1.53M
        if (!strcmp(argv[i], "+proj=pipeline") ||
462
1.53M
            !strcmp(argv[i], "proj=pipeline"))
463
14.6k
            n_pipelines++;
464
1.53M
        if (!strncmp(argv[i], "+init=", 6) || !strncmp(argv[i], "init=", 5))
465
25.2k
            n_inits++;
466
1.53M
    }
467
468
    /* can't have nested pipelines directly */
469
132k
    if (n_pipelines > 1) {
470
37
        pj_log(ctx, PJ_LOG_ERROR, _("Nested pipelines are not supported"));
471
37
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
472
37
        return nullptr;
473
37
    }
474
475
    /* don't allow more than one +init in non-pipeline operations */
476
132k
    if (n_pipelines == 0 && n_inits > 1) {
477
58
        pj_log(ctx, PJ_LOG_ERROR, _("Too many inits"));
478
58
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
479
58
        return nullptr;
480
58
    }
481
482
    /* put arguments into internal linked list */
483
132k
    start = curr = pj_mkparam(argv[0]);
484
132k
    if (!curr) {
485
0
        free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
486
0
        return nullptr;
487
0
    }
488
489
1.53M
    for (i = 1; i < argc; ++i) {
490
1.40M
        curr->next = pj_mkparam(argv[i]);
491
1.40M
        if (!curr->next) {
492
0
            free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
493
0
            return nullptr;
494
0
        }
495
1.40M
        curr = curr->next;
496
1.40M
    }
497
498
    /* Only expand '+init's in non-pipeline operations. '+init's in pipelines
499
     * are
500
     */
501
    /* expanded in the individual pipeline steps during pipeline initialization.
502
     */
503
    /* Potentially this leads to many nested pipelines, which shouldn't be a */
504
    /* problem when '+init's are expanded as late as possible. */
505
132k
    init = pj_param_exists(start, "init");
506
132k
    if (init && n_pipelines == 0) {
507
16.2k
        init = pj_expand_init_internal(ctx, init, allow_init_epsg);
508
16.2k
        if (!init) {
509
361
            free_params(ctx, start, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
510
361
            return nullptr;
511
361
        }
512
16.2k
    }
513
132k
    if (ctx->last_errno) {
514
0
        free_params(ctx, start, ctx->last_errno);
515
0
        return nullptr;
516
0
    }
517
518
    /* Find projection selection */
519
132k
    curr = pj_param_exists(start, "proj");
520
132k
    if (nullptr == curr) {
521
244
        pj_log(ctx, PJ_LOG_ERROR, _("Missing proj"));
522
244
        free_params(ctx, start, PROJ_ERR_INVALID_OP_MISSING_ARG);
523
244
        return nullptr;
524
244
    }
525
132k
    name = curr->param;
526
132k
    if (strlen(name) < 6) {
527
13
        pj_log(ctx, PJ_LOG_ERROR, _("Invalid value for proj"));
528
13
        free_params(ctx, start, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
529
13
        return nullptr;
530
13
    }
531
132k
    name += 5;
532
533
132k
    proj = locate_constructor(name);
534
132k
    if (nullptr == proj) {
535
656
        pj_log(ctx, PJ_LOG_ERROR, _("Unknown projection"));
536
656
        free_params(ctx, start, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
537
656
        return nullptr;
538
656
    }
539
540
131k
    append_default_ellipsoid_to_paralist(start);
541
542
    /* Allocate projection structure */
543
131k
    PIN = proj(nullptr);
544
131k
    if (nullptr == PIN) {
545
0
        free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
546
0
        return nullptr;
547
0
    }
548
549
131k
    PIN->ctx = ctx;
550
131k
    PIN->params = start;
551
131k
    PIN->is_latlong = 0;
552
131k
    PIN->is_geocent = 0;
553
131k
    PIN->is_long_wrap_set = 0;
554
131k
    PIN->long_wrap_center = 0.0;
555
131k
    strcpy(PIN->axis, "enu");
556
557
    /* Set datum parameters. Similarly to +init parameters we want to expand */
558
    /* +datum parameters as late as possible when dealing with pipelines. */
559
    /* otherwise only the first occurrence of +datum will be expanded and that
560
     */
561
131k
    if (n_pipelines == 0) {
562
116k
        if (pj_datum_set(ctx, start, PIN))
563
40
            return pj_default_destructor(PIN, proj_errno(PIN));
564
116k
    }
565
566
131k
    err = pj_ellipsoid(PIN);
567
568
131k
    if (err) {
569
        /* Didn't get an ellps, but doesn't need one: Get a free WGS84 */
570
13.8k
        if (PIN->need_ellps) {
571
195
            pj_log(ctx, PJ_LOG_ERROR,
572
195
                   _("pj_init_ctx: Must specify ellipsoid or sphere"));
573
195
            return pj_default_destructor(PIN, proj_errno(PIN));
574
13.6k
        } else {
575
13.6k
            if (PIN->a == 0)
576
13.1k
                proj_errno_reset(PIN);
577
13.6k
            PIN->f = 1.0 / 298.257223563;
578
13.6k
            PIN->a = 6378137.0;
579
13.6k
            PIN->es = PIN->f * (2 - PIN->f);
580
13.6k
        }
581
13.8k
    }
582
131k
    PIN->a_orig = PIN->a;
583
131k
    PIN->es_orig = PIN->es;
584
131k
    if (pj_calc_ellipsoid_params(PIN, PIN->a, PIN->es))
585
0
        return pj_default_destructor(PIN,
586
0
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
587
588
    /* Now that we have ellipse information check for WGS84 datum */
589
131k
    if (PIN->datum_type == PJD_3PARAM && PIN->datum_params[0] == 0.0 &&
590
131k
        PIN->datum_params[1] == 0.0 && PIN->datum_params[2] == 0.0 &&
591
131k
        PIN->a == 6378137.0 &&
592
131k
        ABS(PIN->es - 0.006694379990) < 0.000000000050) /*WGS84/GRS80*/
593
4.91k
    {
594
4.91k
        PIN->datum_type = PJD_WGS84;
595
4.91k
    }
596
597
    /* Set PIN->geoc coordinate system */
598
131k
    PIN->geoc = (PIN->es != 0.0 && pj_param(ctx, start, "bgeoc").i);
599
600
    /* Over-ranging flag */
601
131k
    PIN->over = pj_param(ctx, start, "bover").i;
602
131k
    if (ctx->forceOver) {
603
0
        PIN->over = ctx->forceOver;
604
0
    }
605
606
    /* Vertical datum geoid grids */
607
131k
    PIN->has_geoid_vgrids = pj_param(ctx, start, "tgeoidgrids").i;
608
131k
    if (PIN->has_geoid_vgrids) /* we need to mark it as used. */
609
5.43k
        pj_param(ctx, start, "sgeoidgrids");
610
611
    /* Longitude center for wrapping */
612
131k
    PIN->is_long_wrap_set = pj_param(ctx, start, "tlon_wrap").i;
613
131k
    if (PIN->is_long_wrap_set) {
614
106
        PIN->long_wrap_center = pj_param(ctx, start, "rlon_wrap").f;
615
        /* Don't accept excessive values otherwise we might perform badly */
616
        /* when correcting longitudes around it */
617
        /* The test is written this way to error on long_wrap_center "=" NaN */
618
106
        if (!(fabs(PIN->long_wrap_center) < 10 * M_TWOPI)) {
619
0
            proj_log_error(PIN, _("Invalid value for lon_wrap"));
620
0
            return pj_default_destructor(PIN,
621
0
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
622
0
        }
623
106
    }
624
625
    /* Axis orientation */
626
131k
    if ((pj_param(ctx, start, "saxis").s) != nullptr) {
627
2.73k
        const char *axis_legal = "ewnsud";
628
2.73k
        const char *axis_arg = pj_param(ctx, start, "saxis").s;
629
2.73k
        if (strlen(axis_arg) != 3) {
630
9
            proj_log_error(PIN, _("Invalid value for axis"));
631
9
            return pj_default_destructor(PIN,
632
9
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
633
9
        }
634
635
2.72k
        if (strchr(axis_legal, axis_arg[0]) == nullptr ||
636
2.72k
            strchr(axis_legal, axis_arg[1]) == nullptr ||
637
2.72k
            strchr(axis_legal, axis_arg[2]) == nullptr) {
638
100
            proj_log_error(PIN, _("Invalid value for axis"));
639
100
            return pj_default_destructor(PIN,
640
100
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
641
100
        }
642
643
        /* TODO: it would be nice to validate we don't have on axis repeated */
644
2.62k
        strcpy(PIN->axis, axis_arg);
645
2.62k
    }
646
647
    /* Central meridian */
648
131k
    PIN->lam0 = pj_param(ctx, start, "rlon_0").f;
649
650
    /* Central latitude */
651
131k
    PIN->phi0 = pj_param(ctx, start, "rlat_0").f;
652
131k
    if (fabs(PIN->phi0) > M_HALFPI) {
653
7
        proj_log_error(PIN,
654
7
                       _("Invalid value for lat_0: |lat_0| should be <= 90°"));
655
7
        return pj_default_destructor(PIN,
656
7
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
657
7
    }
658
659
    /* False easting and northing */
660
131k
    PIN->x0 = pj_param(ctx, start, "dx_0").f;
661
131k
    PIN->y0 = pj_param(ctx, start, "dy_0").f;
662
131k
    PIN->z0 = pj_param(ctx, start, "dz_0").f;
663
131k
    PIN->t0 = pj_param(ctx, start, "dt_0").f;
664
665
    /* General scaling factor */
666
131k
    if (pj_param(ctx, start, "tk_0").i)
667
566
        PIN->k0 = pj_param(ctx, start, "dk_0").f;
668
130k
    else if (pj_param(ctx, start, "tk").i)
669
3.36k
        PIN->k0 = pj_param(ctx, start, "dk").f;
670
127k
    else
671
127k
        PIN->k0 = 1.;
672
131k
    if (PIN->k0 <= 0.) {
673
89
        proj_log_error(PIN, _("Invalid value for k/k_0: it should be > 0"));
674
89
        return pj_default_destructor(PIN,
675
89
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
676
89
    }
677
678
    /* Set units */
679
130k
    units = pj_list_linear_units();
680
130k
    s = nullptr;
681
130k
    if ((name = pj_param(ctx, start, "sunits").s) != nullptr) {
682
26.6k
        for (i = 0; (s = units[i].id) && strcmp(name, s); ++i)
683
14.2k
            ;
684
12.4k
        if (!s) {
685
16
            proj_log_error(PIN, _("Invalid value for units"));
686
16
            return pj_default_destructor(PIN,
687
16
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
688
16
        }
689
12.4k
        s = units[i].to_meter;
690
12.4k
    }
691
130k
    if (s || (s = pj_param(ctx, start, "sto_meter").s)) {
692
13.1k
        char *end_ptr = const_cast<char *>(s);
693
13.1k
        PIN->to_meter = pj_strtod(s, &end_ptr);
694
13.1k
        s = end_ptr;
695
13.1k
        if (*s == '/') { /* ratio number */
696
60
            ++s;
697
60
            double denom = pj_strtod(s, nullptr);
698
60
            if (denom == 0.0) {
699
3
                proj_log_error(PIN,
700
3
                               _("Invalid value for to_meter donominator"));
701
3
                return pj_default_destructor(
702
3
                    PIN, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
703
3
            }
704
57
            PIN->to_meter /= denom;
705
57
        }
706
13.1k
        if (PIN->to_meter <= 0.0) {
707
10
            proj_log_error(PIN, _("Invalid value for to_meter"));
708
10
            return pj_default_destructor(PIN,
709
10
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
710
10
        }
711
13.1k
        PIN->fr_meter = 1 / PIN->to_meter;
712
713
13.1k
    } else
714
117k
        PIN->to_meter = PIN->fr_meter = 1.;
715
716
    /* Set vertical units */
717
130k
    s = nullptr;
718
130k
    if ((name = pj_param(ctx, start, "svunits").s) != nullptr) {
719
10.7k
        for (i = 0; (s = units[i].id) && strcmp(name, s); ++i)
720
8.57k
            ;
721
2.14k
        if (!s) {
722
23
            proj_log_error(PIN, _("Invalid value for vunits"));
723
23
            return pj_default_destructor(PIN,
724
23
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
725
23
        }
726
2.12k
        s = units[i].to_meter;
727
2.12k
    }
728
130k
    if (s || (s = pj_param(ctx, start, "svto_meter").s)) {
729
2.34k
        char *end_ptr = const_cast<char *>(s);
730
2.34k
        PIN->vto_meter = pj_strtod(s, &end_ptr);
731
2.34k
        s = end_ptr;
732
2.34k
        if (*s == '/') { /* ratio number */
733
13
            ++s;
734
13
            double denom = pj_strtod(s, nullptr);
735
13
            if (denom == 0.0) {
736
11
                proj_log_error(PIN,
737
11
                               _("Invalid value for vto_meter donominator"));
738
11
                return pj_default_destructor(
739
11
                    PIN, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
740
11
            }
741
2
            PIN->vto_meter /= denom;
742
2
        }
743
2.33k
        if (PIN->vto_meter <= 0.0) {
744
15
            proj_log_error(PIN, _("Invalid value for vto_meter"));
745
15
            return pj_default_destructor(PIN,
746
15
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
747
15
        }
748
2.32k
        PIN->vfr_meter = 1. / PIN->vto_meter;
749
128k
    } else {
750
128k
        PIN->vto_meter = PIN->to_meter;
751
128k
        PIN->vfr_meter = PIN->fr_meter;
752
128k
    }
753
754
    /* Prime meridian */
755
130k
    prime_meridians = proj_list_prime_meridians();
756
130k
    s = nullptr;
757
130k
    if ((name = pj_param(ctx, start, "spm").s) != nullptr) {
758
1.78k
        const char *value = nullptr;
759
1.78k
        char *next_str = nullptr;
760
761
21.2k
        for (i = 0; prime_meridians[i].id != nullptr; ++i) {
762
20.0k
            if (strcmp(name, prime_meridians[i].id) == 0) {
763
613
                value = prime_meridians[i].defn;
764
613
                break;
765
613
            }
766
20.0k
        }
767
768
1.78k
        if (value == nullptr &&
769
1.78k
            (dmstor_ctx(ctx, name, &next_str) != 0.0 || *name == '0') &&
770
1.78k
            *next_str == '\0')
771
1.12k
            value = name;
772
773
1.78k
        if (!value) {
774
40
            proj_log_error(PIN, _("Invalid value for pm"));
775
40
            return pj_default_destructor(PIN,
776
40
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
777
40
        }
778
1.74k
        PIN->from_greenwich = dmstor_ctx(ctx, value, nullptr);
779
1.74k
    } else
780
129k
        PIN->from_greenwich = 0.0;
781
782
    /* Private object for the geodesic functions */
783
130k
    PIN->geod = static_cast<struct geod_geodesic *>(
784
130k
        calloc(1, sizeof(struct geod_geodesic)));
785
130k
    if (nullptr == PIN->geod)
786
0
        return pj_default_destructor(PIN, PROJ_ERR_OTHER /*ENOMEM*/);
787
130k
    geod_init(PIN->geod, PIN->a, PIN->f);
788
789
    /* Projection specific initialization */
790
130k
    err = proj_errno_reset(PIN);
791
130k
    PIN = proj(PIN);
792
130k
    if (proj_errno(PIN)) {
793
6.84k
        proj_destroy(PIN);
794
6.84k
        return nullptr;
795
6.84k
    }
796
124k
    proj_errno_restore(PIN, err);
797
124k
    return PIN;
798
130k
}