Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GHOST_PathUtils.cpp « intern « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b57480039a5ed5e322e625cbbe6b094146a948e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2010 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup GHOST
 */

#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include "GHOST_PathUtils.h"
#include "GHOST_Types.h"

/* Based on: https://stackoverflow.com/a/2766963/432509 */

using DecodeState_e = enum DecodeState_e {
  /** Searching for an ampersand to convert. */
  STATE_SEARCH = 0,
  /** Convert the two proceeding characters from hex. */
  STATE_CONVERTING
};

void GHOST_URL_decode(char *buf_dst, int buf_dst_size, const char *buf_src)
{
  const unsigned int buf_src_len = strlen(buf_src);
  DecodeState_e state = STATE_SEARCH;
  unsigned int ascii_character;
  char temp_num_buf[3] = {0};

  memset(buf_dst, 0, buf_dst_size);

  for (unsigned int i = 0; i < buf_src_len; i++) {
    switch (state) {
      case STATE_SEARCH: {
        if (buf_src[i] != '%') {
          strncat(buf_dst, &buf_src[i], 1);
          assert((int)strlen(buf_dst) < buf_dst_size);
          break;
        }

        /* We are now converting. */
        state = STATE_CONVERTING;
        break;
      }
      case STATE_CONVERTING: {
        bool both_digits = true;

        /* Create a buffer to hold the hex. For example, if `%20`,
         * this buffer would hold 20 (in ASCII). */
        memset(temp_num_buf, 0, sizeof(temp_num_buf));

        /* Conversion complete (i.e. don't convert again next iteration). */
        state = STATE_SEARCH;

        strncpy(temp_num_buf, &buf_src[i], 2);

        /* Ensure both characters are hexadecimal. */
        for (int j = 0; j < 2; j++) {
          if (!isxdigit(temp_num_buf[j])) {
            both_digits = false;
          }
        }

        if (!both_digits) {
          break;
        }
        /* Convert two hexadecimal characters into one character. */
        sscanf(temp_num_buf, "%x", &ascii_character);

        /* Ensure we aren't going to overflow. */
        assert((int)strlen(buf_dst) < buf_dst_size);

        /* Concatenate this character onto the output. */
        strncat(buf_dst, (char *)&ascii_character, 1);

        /* Skip the next character. */
        i++;
        break;
      }
    }
  }
}

char *GHOST_URL_decode_alloc(const char *buf_src)
{
  /* Assume one character of encoded URL can be expanded to 4 chars max. */
  const size_t decoded_size_max = 4 * strlen(buf_src) + 1;
  char *buf_dst = (char *)malloc(decoded_size_max);
  GHOST_URL_decode(buf_dst, decoded_size_max, buf_src);
  const size_t decoded_size = strlen(buf_dst) + 1;
  if (decoded_size != decoded_size_max) {
    char *buf_dst_trim = (char *)malloc(decoded_size);
    memcpy(buf_dst_trim, buf_dst, decoded_size);
    free(buf_dst);
    buf_dst = buf_dst_trim;
  }
  return buf_dst;
}