File: applyranges.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (352 lines) | stat: -rw-r--r-- 13,315 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
  Copyright (C) 2023-2024  Selwin van Dijk

  This file is part of signalbackup-tools.

  signalbackup-tools is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  signalbackup-tools is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with signalbackup-tools.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "signalbackup.ih"
#include "msgrange.h"

/*
  While the data in 'body' is utf8 encoded, the range (start+length) deals with utf16 (one or two 16bit bytes)

  at each point in the string (which only deals with single 8bit bytes, we need to know the number of bytes to
  skip to the next char (which is its utf8 size) as well as compensate the index as if the data were 16bit
  utf16 (possibly multibyte).

  It's a mess, and I hope it works


  eg:

  RANGE: {start: 3, length: 1, replacement: 'AA'}
  '💩 ...'
  0xF0 0x9F 0x92 0xA9 0x20 0xef 0xbf 0xbc ...
   \_______________/   |     \_______/
        emoji        space     placeholder

  idx0: '0xF0' => utf8 size == 4 => idx0->idx4
               => utf16 size == 2 => range{start: 3 + (4 - 2)}
  idx4: '0x20' => utf8 size == 1 => idx4->idx5
               => utf16 size == 1 => range{start: 5 + (1 - 1)}
  idx5: MATCH! adjust length at this position from utf16 codepoints to 8bit bytes and replace
               => utf16 length 1 at idx 5 == 3 => replace 3 with 'AA'
               => 0xF0 0x9F 0x92 0xA9 0x20 0x41 0x41 ...
               => idx5->idx7 (5 +
 */

void SignalBackup::applyRanges(std::string *body, std::vector<Range> *ranges, std::set<int> *positions_excluded_from_escape) const
{
  // sort the ranges and adjust them
  // to deal with overlaps
  prepRanges(ranges);


  // then, apply the ranges:
  unsigned int rangesidx = 0;
  unsigned int bodyidx = 0;
  while (bodyidx < body->size() && rangesidx < ranges->size())
  {
    //std::cout << "Checking char idx: " << bodyidx << "('" << (*body)[bodyidx] << "') for range with start: " << (*ranges)[rangesidx].start << std::flush;

    int charsizeinbytes = bytesToUtf8CharSize(*body, bodyidx);
    int charsizeinutf16entities = utf16CharSize(*body, bodyidx);

    //std::cout << ", charsize: " << charsizeinbytes << " codepoints: " << charsizeinutf16entities <<  std::endl;

    if (bodyidx == (*ranges)[rangesidx].start)
    {
      //int length = bytesToUtf8CharSize(*body, bodyidx, (*ranges)[rangesidx].length);
      int length = numBytesInUtf16Substring(*body, bodyidx, (*ranges)[rangesidx].length);
      std::string pre = (*ranges)[rangesidx].pre;
      std::string replacement = (*ranges)[rangesidx].replacement;
      std::string post = (*ranges)[rangesidx].post;

      if (replacement.empty())
        replacement = body->substr(bodyidx, length);

      body->replace(bodyidx, length, pre + replacement + post);

      for (unsigned int i = 0; i < pre.size(); ++i)
        if (positions_excluded_from_escape)
          positions_excluded_from_escape->insert(i + bodyidx);
      for (unsigned int i = 0; i < post.size(); ++i)
        if (positions_excluded_from_escape)
          positions_excluded_from_escape->insert(i + bodyidx + pre.size() + replacement.size());

      //std::cout << "BODY: " << *body << std::endl;

      for (unsigned int i = rangesidx + 1; i < ranges->size(); ++i)
        (*ranges)[i].start += pre.size() + post.size() + (replacement.size() - (*ranges)[rangesidx].length);

      // skip the just inserted string
      bodyidx += pre.size() + post.size() + replacement.size();

      // look for next range
      // while the prepwork should make sure it is the first one,
      // interactions with mention replacements might throw it off?
      // just to be sure, lets not just `++rangesidx'
      while (++rangesidx < ranges->size() && (*ranges)[rangesidx].start < bodyidx)
        ;
      continue;
    }

    // update all following ranges for multibyte char
    for (unsigned int i = rangesidx; i < ranges->size(); ++i)
      (*ranges)[i].start += (charsizeinbytes - charsizeinutf16entities);

    // next char...
    bodyidx += charsizeinbytes;
  }
}

/*
  This should work for (possible) overlapping ranges
  depending on how html renders certain things.
*/
void SignalBackup::prepRanges(std::vector<Range> *ranges) const
{
  std::sort(ranges->begin(), ranges->end());

  // if (ranges->size())
  // {
  //   std::cout << "got range:" << std::endl;
  //   for (auto const &r : *ranges)
  //     std::cout << "[" << r.start << "-" << r.start + r.length << "] '" << r.pre << "' '" << r.post << "'" << std::endl;
  // }

  /*
    After sorting, there are the following possible overlaps between range_i-1 and range_i:

    (1)
      O1: <1>      </1>
      O1: <2>      </2>
      ->
      N1: <1><2>   </2></1>

      ! NOTE only one of them can have replacement, not both *
      ! <N1> copies replacement from 1/2

    (2)
      O1: <1>      </1>
      O2: <2>               </2>
      ->
      N1: <2><1>   </1>
      N2:          ""       </2>

      ! NOTE <2> cannot have replacement (<1> would fall in the middle of it) **
      ! IF <1> has replacement <N1> has replacement

    (3)
      O1: <1>          </1>
      O2:      <2>     </2>
      ->
      N1: <1>  ""
      N2:      <2>     </2></1>

      ! NOTE <1> cannot have replacement (<2> would fall in the middle of it) **
      ! IF <2> has replacement <N2> has replacement

    (4a)
      O1: <1>          </1>
      O2:      <2>                </2>
      ->
      N1: <1>  ""
      N2:      <2>     </2></1>
      N3:              <2>        </2>

      ! NOTE: <1> is unbroken!

      ! NOTE neither <1> or <2> can have replacement **

    (4b) ???
      O1: <1>                   </1>
      O2:      <2>                      </2>
      ->
      N1: <1>  </1><2>
      N2:      <1>              </1>
      N3:                       ""      </2>

      ! NOTE : <2> is unbroken!

      ! NOTE neither <1> or <2> can have replacement **

      !! NOTE CASE 4 (a&b) DO NOT SEEM TO BE ALLOWED IN SIGNAL
      !!! NOTE IN CASE 4ab, ONE OF THE RANGES MUST BE BROKEN (IF BOTH
          HAVE nobreak == true, ONE WILL BREAK ANYWAY)

    (5)
      O1: <1>                    </1>
      O2:      <2>    </2>
      ->
      N1: <1>  ""
      N2: (O2)
      N3:             ""         </1>

      ! NOTE <1> cannot have replacement (<2> would fall in the middle of it) **
      ! IF <2> has replacement <N2> has replacement


      *) This is hypothetical, no action can currently cause the same string location
         to be replaced by multiple substrings
      **) This is also hypothetical, all replacement-ranges (mentions) currently have
          length == 1, which makes it impossible for another range to start/end in the
          middle of it

  */

  for (unsigned int i = 1; i < ranges->size(); ++i)
  {
    if ((*ranges)[i].start == (*ranges)[i - 1].start)
    {
      if ((*ranges)[i].length == (*ranges)[i -1].length)
      {
        // (1) SAME START, SAME FINISH
        if (!(*ranges)[i - 1].replacement.empty() &&
            !(*ranges)[i].replacement.empty())
        {
          Logger::warning("Illegal range-set (overlapping ranges both have replacement).");
          continue;
        }
        //std::cout << "CASE 1" << std::endl;
        (*ranges)[i - 1].pre += (*ranges)[i].pre;
        (*ranges)[i - 1].post = (*ranges)[i].post + (*ranges)[i - 1].post;
        if (!(*ranges)[i].replacement.empty()) // only one can have replacement, if it's i-1 its already kept...
          (*ranges)[i - 1].replacement = (*ranges)[i].replacement;
        ranges->erase(ranges->begin() + i);
        return prepRanges(ranges);
      }
      // (2) SAME START, LATER FINISH
      // else -> i.length > (i - 1).length
      if (!(*ranges)[i].replacement.empty())
      {
        Logger::warning("Illegal range-set (overlapping ranges both have replacement).");
        continue;
      }
      //std::cout << "CASE 2" << std::endl;
      // (*ranges)[i - 1].replacement is automatically kept if it has one
      (*ranges)[i - 1].pre = (*ranges)[i].pre + (*ranges)[i - 1].pre;
      (*ranges)[i].pre = "";
      (*ranges)[i].start = (*ranges)[i - 1].start + (*ranges)[i - 1].length;
      (*ranges)[i].length -= (*ranges)[i - 1].length;
      return prepRanges(ranges);
    }
    // else (i].start > (i - 1).start)
    if ((*ranges)[i].start + (*ranges)[i].length == (*ranges)[i - 1].start + (*ranges)[i - 1].length) // same end pos
    {
      // (3) LATER START, SAME FINISH
      if (!(*ranges)[i - 1].replacement.empty())
      {
        Logger::warning("Warning. Illegal range-set (overlapping ranges both have replacement).");
        continue;
      }
      //std::cout << "CASE 3" << std::endl;
      // (*ranges)[i].replacement is automatically kept if it has one
      (*ranges)[i].post = (*ranges)[i].post + (*ranges)[i - 1].post;
      (*ranges)[i - 1].post = "";
      (*ranges)[i - 1].length = (*ranges)[i].start - (*ranges)[i - 1].start;
      return prepRanges(ranges);
    }
    if ((*ranges)[i].start < (*ranges)[i - 1].start + (*ranges)[i - 1].length)
    {
      if ((*ranges)[i].start + (*ranges)[i].length > (*ranges)[i - 1].start + (*ranges)[i - 1].length) // end later
      {
        // (4) LATER START, LATER FINISH"
        if (!(*ranges)[i - 1].replacement.empty() ||
            !(*ranges)[i].replacement.empty())
        {
          Logger::warning("Warning. Illegal range-set (overlapping ranges both have replacement).");
          continue;
        }

        if ((*ranges)[i].nobreak && !(*ranges)[i - 1].nobreak)
        {
          //std::cout << "CASE 4b" << std::endl;
          Range newrange = {(*ranges)[i - 1].start,
                            (*ranges)[i].start - (*ranges)[i - 1].start,
                            (*ranges)[i - 1].pre,
                            "",
                            (*ranges)[i - 1].post + (*ranges)[i].pre,
                            false}; // N1
          ranges->emplace_back(newrange);

          (*ranges)[i - 1].start = (*ranges)[i].start;
          (*ranges)[i - 1].length = (*ranges)[i - 1].length - newrange.length; // N2

          (*ranges)[i].start = (*ranges)[i - 1].start + (*ranges)[i - 1].length;
          (*ranges)[i].pre = "";
          (*ranges)[i].length = (*ranges)[i].length - (*ranges)[i - 1].length; // N3
        }
        else
        {
          if ((*ranges)[i].nobreak && (*ranges)[i - 1].nobreak) [[unlikely]]
          {
            Logger::warning("Illegal ranges: overlapping but unbreakable");
            // std::cout << i << std::endl;
            // std::cout << (*ranges)[i].start << std::endl;
            // std::cout << (*ranges)[i].length << std::endl;
            // std::cout << (*ranges)[i].pre << std::endl;
            // std::cout << (*ranges)[i].post << std::endl;
            // std::cout << i - 1 << std::endl;
            // std::cout << (*ranges)[i-1].start << std::endl;
            // std::cout << (*ranges)[i-1].length << std::endl;
            // std::cout << (*ranges)[i-1].pre << std::endl;
            // std::cout << (*ranges)[i-1].post << std::endl;
          }

          //std::cout << "CASE 4a" << std::endl;
          Range newrange = {(*ranges)[i].start,
                            (*ranges)[i - 1].length - ((*ranges)[i].start - (*ranges)[i - 1].start),
                            (*ranges)[i].pre,
                            "",
                            (*ranges)[i].post + (*ranges)[i - 1].post,
                            false}; // N2
          ranges->emplace_back(newrange);

          (*ranges)[i - 1].length = newrange.start - (*ranges)[i - 1].start; // N1
          (*ranges)[i - 1].post = "";

          (*ranges)[i].start = newrange.start + newrange.length; // N3
          (*ranges)[i].length -= newrange.length;
        }
        return prepRanges(ranges);
      }
      //if ((*ranges)[i].start + (*ranges)[i].length < (*ranges)[i - 1].start + (*ranges)[i - 1].length) // end sooner
      // (5) LATER START, EARLIER FINISH
      if (!(*ranges)[i - 1].replacement.empty())
      {
        Logger::warning("Warning. Illegal range-set (overlapping ranges both have replacement).");
        continue;
      }
      //std::cout << "CASE 5" << std::endl;
      Range newrange = {(*ranges)[i].start + (*ranges)[i].length,
                        (*ranges)[i - 1].start + (*ranges)[i - 1].length - ((*ranges)[i].start + (*ranges)[i].length),
                        "",
                        "",
                        (*ranges)[i - 1].post,
                        false};
      ranges->emplace_back(newrange);                                           // -> N3

      (*ranges)[i - 1].post = "";
      (*ranges)[i - 1].length = (*ranges)[i].start - (*ranges)[i - 1].start; // O1 -> N1

      // (*ranges)[i].replacement is automatically kept if it has one
      // O2 == N2
      return prepRanges(ranges);
    }
  }
  // std::cout << "DONE!" << std::endl;
}