Coverage Report

Created: 2025-06-20 06:58

/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
508
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
508
    const char *c = definition;
49
508
    paralist *first = nullptr, *last = nullptr;
50
51
3.30k
    while (*c) {
52
        /* Keep a handle to the start of the list, so we have something to
53
         * return */
54
2.79k
        auto param = pj_mkparam_ws(c, &c);
55
2.79k
        if (nullptr == param) {
56
0
            free_params(ctx, first, PROJ_ERR_OTHER /*ENOMEM*/);
57
0
            return nullptr;
58
0
        }
59
2.79k
        if (nullptr == last) {
60
508
            first = param;
61
2.28k
        } else {
62
2.28k
            last->next = param;
63
2.28k
        }
64
2.79k
        last = param;
65
2.79k
    }
66
508
    return first;
67
508
}
68
69
/**************************************************************************************/
70
1.06k
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
17.5k
#define MAX_LINE_LENGTH 1000
77
1.06k
    size_t current_buffer_size = 5 * (MAX_LINE_LENGTH + 1);
78
1.06k
    char *fname, *section;
79
1.06k
    const char *key;
80
1.06k
    char *buffer = nullptr;
81
1.06k
    size_t n;
82
83
1.06k
    fname = static_cast<char *>(malloc(MAX_PATH_FILENAME + ID_TAG_MAX + 3));
84
1.06k
    if (nullptr == fname) {
85
0
        return nullptr;
86
0
    }
87
88
    /* Support "init=file:section", "+init=file:section", and "file:section"
89
     * format */
90
1.06k
    key = strstr(name, "init=");
91
1.06k
    if (nullptr == key)
92
893
        key = name;
93
168
    else
94
168
        key += 5;
95
1.06k
    if (MAX_PATH_FILENAME + ID_TAG_MAX + 2 < strlen(key)) {
96
6
        free(fname);
97
6
        return nullptr;
98
6
    }
99
1.05k
    memmove(fname, key, strlen(key) + 1);
100
101
    /* Locate the name of the section we search for */
102
1.05k
    section = strrchr(fname, ':');
103
1.05k
    if (nullptr == section) {
104
173
        pj_log(ctx, PJ_LOG_ERROR, _("Missing colon in +init"));
105
173
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
106
173
        free(fname);
107
173
        return nullptr;
108
173
    }
109
882
    *section = 0;
110
882
    section++;
111
882
    n = strlen(section);
112
882
    pj_log(ctx, PJ_LOG_TRACE,
113
882
           "get_init_string: searching for section [%s] in init file [%s]",
114
882
           section, fname);
115
116
882
    auto file = NS_PROJ::FileManager::open_resource_file(ctx, fname);
117
882
    if (nullptr == file) {
118
140
        pj_log(ctx, PJ_LOG_ERROR, _("Cannot open %s"), fname);
119
140
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
120
140
        free(fname);
121
140
        return nullptr;
122
140
    }
123
124
    /* Search for section in init file */
125
742
    std::string line;
126
16.2k
    for (;;) {
127
128
16.2k
        bool eofReached = false;
129
16.2k
        bool maxLenReached = false;
130
16.2k
        line = file->read_line(MAX_LINE_LENGTH, maxLenReached, eofReached);
131
        /* End of file? */
132
16.2k
        if (maxLenReached || eofReached) {
133
234
            pj_log(ctx, PJ_LOG_ERROR, _("Invalid content for %s"), fname);
134
234
            proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
135
234
            free(fname);
136
234
            return nullptr;
137
234
        }
138
139
        /* At start of right section? */
140
15.9k
        pj_chomp(&line[0]);
141
15.9k
        if ('<' != line[0])
142
13.9k
            continue;
143
2.07k
        if (strlen(line.c_str()) < n + 2)
144
93
            continue;
145
1.98k
        if (line[n + 1] != '>')
146
1.38k
            continue;
147
600
        if (0 == strncmp(line.data() + 1, section, n))
148
508
            break;
149
600
    }
150
151
    /* We're at the first line of the right section - copy line to buffer */
152
508
    buffer = static_cast<char *>(malloc(current_buffer_size));
153
508
    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
508
    strcpy(buffer, line.data() + strlen(section) + 2);
160
161
    /* Copy the remaining lines of the section to buffer */
162
778
    for (;;) {
163
778
        char *end_i_cator;
164
778
        size_t next_length, buffer_length;
165
166
        /* Did the section end somewhere in the most recently read line? */
167
778
        end_i_cator = strchr(buffer, '<');
168
778
        if (end_i_cator) {
169
508
            *end_i_cator = 0;
170
508
            break;
171
508
        }
172
173
270
        bool eofReached = false;
174
270
        bool maxLenReached = false;
175
270
        line = file->read_line(MAX_LINE_LENGTH, maxLenReached, eofReached);
176
        /* End of file? - done! */
177
270
        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
270
        buffer_length = strlen(buffer);
185
270
        pj_chomp(&line[0]); /* Remove '#' style comments */
186
270
        next_length = strlen(line.data()) + buffer_length + 2;
187
270
        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
270
        buffer[buffer_length] = ' ';
200
270
        strcpy(buffer + buffer_length + 1, line.data());
201
270
    }
202
203
508
    free(fname);
204
508
    if (nullptr == buffer)
205
0
        return nullptr;
206
508
    pj_shrink(buffer);
207
508
    pj_log(ctx, PJ_LOG_TRACE, "key=%s, value: [%s]", key, buffer);
208
508
    return buffer;
209
508
}
210
211
/************************************************************************/
212
static paralist *get_init(PJ_CONTEXT *ctx, const char *key,
213
2.31k
                          int allow_init_epsg) {
214
    /*************************************************************************
215
    Expand key from buffer or (if not in buffer) from init file
216
    *************************************************************************/
217
2.31k
    const char *xkey;
218
2.31k
    char *definition = nullptr;
219
2.31k
    paralist *init_items = nullptr;
220
221
2.31k
    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
2.31k
    xkey = strstr(key, "init=");
228
2.31k
    if (nullptr == xkey)
229
19
        xkey = key;
230
2.29k
    else
231
2.29k
        xkey += 5;
232
2.31k
    pj_log(ctx, PJ_LOG_TRACE, "get_init: searching cache for key: [%s]", xkey);
233
234
    /* Is file/key pair already in cache? */
235
2.31k
    init_items = pj_search_initcache(xkey);
236
2.31k
    if (init_items)
237
1.23k
        return init_items;
238
239
1.07k
    if ((strncmp(xkey, "epsg:", 5) == 0 || strncmp(xkey, "IGNF:", 5) == 0)) {
240
15
        char unused[256];
241
15
        char initname[5];
242
15
        int exists;
243
244
15
        strncpy(initname, xkey, 4);
245
15
        initname[4] = 0;
246
247
15
        if (strncmp(xkey, "epsg:", 5) == 0) {
248
6
            exists = ctx->epsg_file_exists;
249
6
            if (exists < 0) {
250
1
                exists = pj_find_file(ctx, initname, unused, sizeof(unused));
251
1
                ctx->epsg_file_exists = exists;
252
1
            }
253
9
        } else {
254
9
            exists = pj_find_file(ctx, initname, unused, sizeof(unused));
255
9
        }
256
257
15
        if (!exists) {
258
15
            char szInitStr[7 + 64];
259
15
            PJ *src;
260
15
            const char *proj_string;
261
262
15
            proj_context_errno_set(ctx, 0);
263
264
15
            if (!allow_init_epsg) {
265
15
                pj_log(ctx, PJ_LOG_TRACE, "%s expansion disallowed", xkey);
266
15
                return nullptr;
267
15
            }
268
0
            if (strlen(xkey) > 64) {
269
0
                return nullptr;
270
0
            }
271
0
            strcpy(szInitStr, "+init=");
272
0
            strcat(szInitStr, xkey);
273
274
0
            auto old_proj4_init_rules = ctx->use_proj4_init_rules;
275
0
            ctx->use_proj4_init_rules = true;
276
0
            src = proj_create(ctx, szInitStr);
277
0
            ctx->use_proj4_init_rules = old_proj4_init_rules;
278
0
            if (!src) {
279
0
                return nullptr;
280
0
            }
281
282
0
            proj_string = proj_as_proj_string(ctx, src, PJ_PROJ_4, nullptr);
283
0
            if (!proj_string) {
284
0
                proj_destroy(src);
285
0
                return nullptr;
286
0
            }
287
0
            definition = (char *)calloc(1, strlen(proj_string) + 1);
288
0
            if (definition) {
289
0
                strcpy(definition, proj_string);
290
0
            }
291
292
0
            proj_destroy(src);
293
0
        }
294
15
    }
295
296
1.06k
    if (!definition) {
297
        /* If not, we must read it from file */
298
1.06k
        pj_log(ctx, PJ_LOG_TRACE,
299
1.06k
               "get_init: searching on in init files for [%s]", xkey);
300
1.06k
        definition = get_init_string(ctx, xkey);
301
1.06k
    }
302
303
1.06k
    if (nullptr == definition)
304
553
        return nullptr;
305
508
    init_items = string_to_paralist(ctx, definition);
306
508
    if (init_items)
307
508
        pj_log(ctx, PJ_LOG_TRACE, "get_init: got [%s], paralist[0,1]: [%s,%s]",
308
508
               definition, init_items->param,
309
508
               init_items->next ? init_items->next->param : "(empty)");
310
508
    free(definition);
311
508
    if (nullptr == init_items)
312
0
        return nullptr;
313
314
    /* We found it in file - now insert into the cache, before returning */
315
508
    pj_insert_initcache(xkey, init_items);
316
508
    return init_items;
317
508
}
318
319
481k
static void append_default_ellipsoid_to_paralist(paralist *start) {
320
481k
    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
481k
    if (pj_param_exists(start, "no_defs"))
328
2.15k
        return;
329
479k
    auto proj = pj_param_exists(start, "proj");
330
479k
    if (nullptr == proj)
331
0
        return;
332
479k
    if (strlen(proj->param) < 6)
333
0
        return;
334
479k
    if (0 == strcmp("pipeline", proj->param + 5))
335
50.0k
        return;
336
337
    /* Don't default ellipse if datum, ellps or any ellipsoid information is set
338
     */
339
429k
    if (pj_param_exists(start, "datum"))
340
19.1k
        return;
341
410k
    if (pj_param_exists(start, "ellps"))
342
145k
        return;
343
264k
    if (pj_param_exists(start, "a"))
344
50.0k
        return;
345
214k
    if (pj_param_exists(start, "b"))
346
114
        return;
347
214k
    if (pj_param_exists(start, "rf"))
348
54
        return;
349
214k
    if (pj_param_exists(start, "f"))
350
186
        return;
351
214k
    if (pj_param_exists(start, "e"))
352
293
        return;
353
213k
    if (pj_param_exists(start, "es"))
354
154
        return;
355
356
    /* Locate end of start-list */
357
213k
    paralist *last = nullptr;
358
1.46M
    for (last = start; last->next; last = last->next)
359
1.24M
        ;
360
361
    /* If we're here, it's OK to append the current default item */
362
213k
    last->next = pj_mkparam("ellps=GRS80");
363
213k
}
364
365
/*****************************************************************************/
366
static paralist *pj_expand_init_internal(PJ_CONTEXT *ctx, paralist *init,
367
2.31k
                                         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
2.31k
    paralist *last;
388
2.31k
    paralist *expn;
389
390
    /* Nowhere to start? */
391
2.31k
    if (nullptr == init)
392
0
        return nullptr;
393
394
2.31k
    expn = get_init(ctx, init->param, allow_init_epsg);
395
396
    /* Nothing in expansion? */
397
2.31k
    if (nullptr == expn)
398
568
        return nullptr;
399
400
    /* Locate  the end of the list */
401
10.2k
    for (last = init; last && last->next; last = last->next)
402
8.46k
        ;
403
404
    /* Then append and return */
405
1.74k
    last->next = expn;
406
1.74k
    return init;
407
2.31k
}
408
409
823
paralist *pj_expand_init(PJ_CONTEXT *ctx, paralist *init) {
410
823
    return pj_expand_init_internal(ctx, init, TRUE);
411
823
}
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
482k
static PJ_CONSTRUCTOR locate_constructor(const char *name) {
423
482k
    int i;
424
482k
    const char *s;
425
482k
    const PJ_OPERATIONS *operations;
426
482k
    operations = proj_list_operations();
427
38.9M
    for (i = 0; (s = operations[i].id) && strcmp(name, s); ++i)
428
38.5M
        ;
429
482k
    if (nullptr == s)
430
1.28k
        return nullptr;
431
481k
    return (PJ_CONSTRUCTOR)operations[i].proj;
432
482k
}
433
434
PJ *pj_init_ctx_with_allow_init_epsg(PJ_CONTEXT *ctx, int argc, char **argv,
435
484k
                                     int allow_init_epsg) {
436
484k
    const char *s;
437
484k
    char *name;
438
484k
    PJ_CONSTRUCTOR proj;
439
484k
    paralist *curr, *init, *start;
440
484k
    int i;
441
484k
    int err;
442
484k
    PJ *PIN = nullptr;
443
484k
    int n_pipelines = 0;
444
484k
    int n_inits = 0;
445
484k
    const PJ_UNITS *units;
446
484k
    const PJ_PRIME_MERIDIANS *prime_meridians;
447
448
484k
    if (nullptr == ctx)
449
0
        ctx = pj_get_default_ctx();
450
451
484k
    ctx->last_errno = 0;
452
453
484k
    if (argc <= 0) {
454
4
        pj_log(ctx, PJ_LOG_ERROR, _("No arguments"));
455
4
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_MISSING_ARG);
456
4
        return nullptr;
457
4
    }
458
459
    /* count occurrences of pipelines and inits */
460
6.51M
    for (i = 0; i < argc; ++i) {
461
6.03M
        if (!strcmp(argv[i], "+proj=pipeline") ||
462
6.03M
            !strcmp(argv[i], "proj=pipeline"))
463
51.7k
            n_pipelines++;
464
6.03M
        if (!strncmp(argv[i], "+init=", 6) || !strncmp(argv[i], "init=", 5))
465
3.93k
            n_inits++;
466
6.03M
    }
467
468
    /* can't have nested pipelines directly */
469
484k
    if (n_pipelines > 1) {
470
107
        pj_log(ctx, PJ_LOG_ERROR, _("Nested pipelines are not supported"));
471
107
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
472
107
        return nullptr;
473
107
    }
474
475
    /* don't allow more than one +init in non-pipeline operations */
476
483k
    if (n_pipelines == 0 && n_inits > 1) {
477
187
        pj_log(ctx, PJ_LOG_ERROR, _("Too many inits"));
478
187
        proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
479
187
        return nullptr;
480
187
    }
481
482
    /* put arguments into internal linked list */
483
483k
    start = curr = pj_mkparam(argv[0]);
484
483k
    if (!curr) {
485
0
        free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
486
0
        return nullptr;
487
0
    }
488
489
6.00M
    for (i = 1; i < argc; ++i) {
490
5.52M
        curr->next = pj_mkparam(argv[i]);
491
5.52M
        if (!curr->next) {
492
0
            free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
493
0
            return nullptr;
494
0
        }
495
5.52M
        curr = curr->next;
496
5.52M
    }
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
483k
    init = pj_param_exists(start, "init");
506
483k
    if (init && n_pipelines == 0) {
507
1.49k
        init = pj_expand_init_internal(ctx, init, allow_init_epsg);
508
1.49k
        if (!init) {
509
376
            free_params(ctx, start, PROJ_ERR_INVALID_OP_WRONG_SYNTAX);
510
376
            return nullptr;
511
376
        }
512
1.49k
    }
513
483k
    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
483k
    curr = pj_param_exists(start, "proj");
520
483k
    if (nullptr == curr) {
521
561
        pj_log(ctx, PJ_LOG_ERROR, _("Missing proj"));
522
561
        free_params(ctx, start, PROJ_ERR_INVALID_OP_MISSING_ARG);
523
561
        return nullptr;
524
561
    }
525
482k
    name = curr->param;
526
482k
    if (strlen(name) < 6) {
527
70
        pj_log(ctx, PJ_LOG_ERROR, _("Invalid value for proj"));
528
70
        free_params(ctx, start, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
529
70
        return nullptr;
530
70
    }
531
482k
    name += 5;
532
533
482k
    proj = locate_constructor(name);
534
482k
    if (nullptr == proj) {
535
1.28k
        pj_log(ctx, PJ_LOG_ERROR, _("Unknown projection"));
536
1.28k
        free_params(ctx, start, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
537
1.28k
        return nullptr;
538
1.28k
    }
539
540
481k
    append_default_ellipsoid_to_paralist(start);
541
542
    /* Allocate projection structure */
543
481k
    PIN = proj(nullptr);
544
481k
    if (nullptr == PIN) {
545
0
        free_params(ctx, start, PROJ_ERR_OTHER /*ENOMEM*/);
546
0
        return nullptr;
547
0
    }
548
549
481k
    PIN->ctx = ctx;
550
481k
    PIN->params = start;
551
481k
    PIN->is_latlong = 0;
552
481k
    PIN->is_geocent = 0;
553
481k
    PIN->is_long_wrap_set = 0;
554
481k
    PIN->long_wrap_center = 0.0;
555
481k
    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
481k
    if (n_pipelines == 0) {
562
429k
        if (pj_datum_set(ctx, start, PIN))
563
192
            return pj_default_destructor(PIN, proj_errno(PIN));
564
429k
    }
565
566
481k
    err = pj_ellipsoid(PIN);
567
568
481k
    if (err) {
569
        /* Didn't get an ellps, but doesn't need one: Get a free WGS84 */
570
7.72k
        if (PIN->need_ellps) {
571
502
            pj_log(ctx, PJ_LOG_ERROR,
572
502
                   _("pj_init_ctx: Must specify ellipsoid or sphere"));
573
502
            return pj_default_destructor(PIN, proj_errno(PIN));
574
7.22k
        } else {
575
7.22k
            if (PIN->a == 0)
576
6.87k
                proj_errno_reset(PIN);
577
7.22k
            PIN->f = 1.0 / 298.257223563;
578
7.22k
            PIN->a = 6378137.0;
579
7.22k
            PIN->es = PIN->f * (2 - PIN->f);
580
7.22k
        }
581
7.72k
    }
582
480k
    PIN->a_orig = PIN->a;
583
480k
    PIN->es_orig = PIN->es;
584
480k
    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
480k
    if (PIN->datum_type == PJD_3PARAM && PIN->datum_params[0] == 0.0 &&
590
480k
        PIN->datum_params[1] == 0.0 && PIN->datum_params[2] == 0.0 &&
591
480k
        PIN->a == 6378137.0 &&
592
480k
        ABS(PIN->es - 0.006694379990) < 0.000000000050) /*WGS84/GRS80*/
593
2.83k
    {
594
2.83k
        PIN->datum_type = PJD_WGS84;
595
2.83k
    }
596
597
    /* Set PIN->geoc coordinate system */
598
480k
    PIN->geoc = (PIN->es != 0.0 && pj_param(ctx, start, "bgeoc").i);
599
600
    /* Over-ranging flag */
601
480k
    PIN->over = pj_param(ctx, start, "bover").i;
602
480k
    if (ctx->forceOver) {
603
0
        PIN->over = ctx->forceOver;
604
0
    }
605
606
    /* Vertical datum geoid grids */
607
480k
    PIN->has_geoid_vgrids = pj_param(ctx, start, "tgeoidgrids").i;
608
480k
    if (PIN->has_geoid_vgrids) /* we need to mark it as used. */
609
22.6k
        pj_param(ctx, start, "sgeoidgrids");
610
611
    /* Longitude center for wrapping */
612
480k
    PIN->is_long_wrap_set = pj_param(ctx, start, "tlon_wrap").i;
613
480k
    if (PIN->is_long_wrap_set) {
614
21
        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
21
        if (!(fabs(PIN->long_wrap_center) < 10 * M_TWOPI)) {
619
3
            proj_log_error(PIN, _("Invalid value for lon_wrap"));
620
3
            return pj_default_destructor(PIN,
621
3
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
622
3
        }
623
21
    }
624
625
    /* Axis orientation */
626
480k
    if ((pj_param(ctx, start, "saxis").s) != nullptr) {
627
4.47k
        const char *axis_legal = "ewnsud";
628
4.47k
        const char *axis_arg = pj_param(ctx, start, "saxis").s;
629
4.47k
        if (strlen(axis_arg) != 3) {
630
25
            proj_log_error(PIN, _("Invalid value for axis"));
631
25
            return pj_default_destructor(PIN,
632
25
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
633
25
        }
634
635
4.45k
        if (strchr(axis_legal, axis_arg[0]) == nullptr ||
636
4.45k
            strchr(axis_legal, axis_arg[1]) == nullptr ||
637
4.45k
            strchr(axis_legal, axis_arg[2]) == nullptr) {
638
12
            proj_log_error(PIN, _("Invalid value for axis"));
639
12
            return pj_default_destructor(PIN,
640
12
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
641
12
        }
642
643
        /* TODO: it would be nice to validate we don't have on axis repeated */
644
4.43k
        strcpy(PIN->axis, axis_arg);
645
4.43k
    }
646
647
    /* Central meridian */
648
480k
    PIN->lam0 = pj_param(ctx, start, "rlon_0").f;
649
650
    /* Central latitude */
651
480k
    PIN->phi0 = pj_param(ctx, start, "rlat_0").f;
652
480k
    if (fabs(PIN->phi0) > M_HALFPI) {
653
45
        proj_log_error(PIN,
654
45
                       _("Invalid value for lat_0: |lat_0| should be <= 90°"));
655
45
        return pj_default_destructor(PIN,
656
45
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
657
45
    }
658
659
    /* False easting and northing */
660
480k
    PIN->x0 = pj_param(ctx, start, "dx_0").f;
661
480k
    PIN->y0 = pj_param(ctx, start, "dy_0").f;
662
480k
    PIN->z0 = pj_param(ctx, start, "dz_0").f;
663
480k
    PIN->t0 = pj_param(ctx, start, "dt_0").f;
664
665
    /* General scaling factor */
666
480k
    if (pj_param(ctx, start, "tk_0").i)
667
1.14k
        PIN->k0 = pj_param(ctx, start, "dk_0").f;
668
479k
    else if (pj_param(ctx, start, "tk").i)
669
19.8k
        PIN->k0 = pj_param(ctx, start, "dk").f;
670
459k
    else
671
459k
        PIN->k0 = 1.;
672
480k
    if (PIN->k0 <= 0.) {
673
47
        proj_log_error(PIN, _("Invalid value for k/k_0: it should be > 0"));
674
47
        return pj_default_destructor(PIN,
675
47
                                     PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
676
47
    }
677
678
    /* Set units */
679
480k
    units = pj_list_linear_units();
680
480k
    s = nullptr;
681
480k
    if ((name = pj_param(ctx, start, "sunits").s) != nullptr) {
682
3.71k
        for (i = 0; (s = units[i].id) && strcmp(name, s); ++i)
683
2.91k
            ;
684
797
        if (!s) {
685
24
            proj_log_error(PIN, _("Invalid value for units"));
686
24
            return pj_default_destructor(PIN,
687
24
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
688
24
        }
689
773
        s = units[i].to_meter;
690
773
    }
691
480k
    if (s || (s = pj_param(ctx, start, "sto_meter").s)) {
692
2.47k
        char *end_ptr = const_cast<char *>(s);
693
2.47k
        PIN->to_meter = pj_strtod(s, &end_ptr);
694
2.47k
        s = end_ptr;
695
2.47k
        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
2.47k
        if (PIN->to_meter <= 0.0) {
707
39
            proj_log_error(PIN, _("Invalid value for to_meter"));
708
39
            return pj_default_destructor(PIN,
709
39
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
710
39
        }
711
2.43k
        PIN->fr_meter = 1 / PIN->to_meter;
712
713
2.43k
    } else
714
478k
        PIN->to_meter = PIN->fr_meter = 1.;
715
716
    /* Set vertical units */
717
480k
    s = nullptr;
718
480k
    if ((name = pj_param(ctx, start, "svunits").s) != nullptr) {
719
24.8k
        for (i = 0; (s = units[i].id) && strcmp(name, s); ++i)
720
17.5k
            ;
721
7.36k
        if (!s) {
722
111
            proj_log_error(PIN, _("Invalid value for vunits"));
723
111
            return pj_default_destructor(PIN,
724
111
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
725
111
        }
726
7.25k
        s = units[i].to_meter;
727
7.25k
    }
728
480k
    if (s || (s = pj_param(ctx, start, "svto_meter").s)) {
729
7.77k
        char *end_ptr = const_cast<char *>(s);
730
7.77k
        PIN->vto_meter = pj_strtod(s, &end_ptr);
731
7.77k
        s = end_ptr;
732
7.77k
        if (*s == '/') { /* ratio number */
733
2.95k
            ++s;
734
2.95k
            double denom = pj_strtod(s, nullptr);
735
2.95k
            if (denom == 0.0) {
736
0
                proj_log_error(PIN,
737
0
                               _("Invalid value for vto_meter donominator"));
738
0
                return pj_default_destructor(
739
0
                    PIN, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
740
0
            }
741
2.95k
            PIN->vto_meter /= denom;
742
2.95k
        }
743
7.77k
        if (PIN->vto_meter <= 0.0) {
744
43
            proj_log_error(PIN, _("Invalid value for vto_meter"));
745
43
            return pj_default_destructor(PIN,
746
43
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
747
43
        }
748
7.73k
        PIN->vfr_meter = 1. / PIN->vto_meter;
749
472k
    } else {
750
472k
        PIN->vto_meter = PIN->to_meter;
751
472k
        PIN->vfr_meter = PIN->fr_meter;
752
472k
    }
753
754
    /* Prime meridian */
755
480k
    prime_meridians = proj_list_prime_meridians();
756
480k
    s = nullptr;
757
480k
    if ((name = pj_param(ctx, start, "spm").s) != nullptr) {
758
51.3k
        const char *value = nullptr;
759
51.3k
        char *next_str = nullptr;
760
761
769k
        for (i = 0; prime_meridians[i].id != nullptr; ++i) {
762
718k
            if (strcmp(name, prime_meridians[i].id) == 0) {
763
623
                value = prime_meridians[i].defn;
764
623
                break;
765
623
            }
766
718k
        }
767
768
51.3k
        if (value == nullptr &&
769
51.3k
            (dmstor_ctx(ctx, name, &next_str) != 0.0 || *name == '0') &&
770
51.3k
            *next_str == '\0')
771
50.6k
            value = name;
772
773
51.3k
        if (!value) {
774
137
            proj_log_error(PIN, _("Invalid value for pm"));
775
137
            return pj_default_destructor(PIN,
776
137
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
777
137
        }
778
51.2k
        PIN->from_greenwich = dmstor_ctx(ctx, value, nullptr);
779
51.2k
    } else
780
429k
        PIN->from_greenwich = 0.0;
781
782
    /* Private object for the geodesic functions */
783
480k
    PIN->geod = static_cast<struct geod_geodesic *>(
784
480k
        calloc(1, sizeof(struct geod_geodesic)));
785
480k
    if (nullptr == PIN->geod)
786
0
        return pj_default_destructor(PIN, PROJ_ERR_OTHER /*ENOMEM*/);
787
480k
    geod_init(PIN->geod, PIN->a, PIN->f);
788
789
    /* Projection specific initialization */
790
480k
    err = proj_errno_reset(PIN);
791
480k
    PIN = proj(PIN);
792
480k
    if (proj_errno(PIN)) {
793
59.2k
        proj_destroy(PIN);
794
59.2k
        return nullptr;
795
59.2k
    }
796
421k
    proj_errno_restore(PIN, err);
797
421k
    return PIN;
798
480k
}