-
Notifications
You must be signed in to change notification settings - Fork 813
/
lib.rs
599 lines (554 loc) · 20.9 KB
/
lib.rs
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
use adblock::engine::{Engine, EngineDebugInfo};
use adblock::lists::FilterListMetadata;
use adblock::regex_manager::RegexManagerDiscardPolicy;
use adblock::resources::{MimeType, Resource, ResourceType};
use core::ptr;
use libc::size_t;
use std::collections::HashSet;
use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::c_char;
/// An external callback that receives a hostname and two out-parameters for
/// start and end position. The callback should fill the start and end positions
/// with the start and end indices of the domain part of the hostname.
pub type DomainResolverCallback = unsafe extern "C" fn(*const c_char, *mut u32, *mut u32);
/// Passes a callback to the adblock library, allowing it to be used for domain
/// resolution.
///
/// This is required to be able to use any adblocking functionality.
///
/// Returns true on success, false if a callback was already set previously.
#[no_mangle]
pub unsafe extern "C" fn set_domain_resolver(resolver: DomainResolverCallback) -> bool {
struct RemoteResolverImpl {
remote_callback: DomainResolverCallback,
}
impl adblock::url_parser::ResolvesDomain for RemoteResolverImpl {
fn get_host_domain(&self, host: &str) -> (usize, usize) {
let mut start: u32 = 0;
let mut end: u32 = 0;
let host_c_str = CString::new(host).expect("Error: CString::new()");
let remote_callback = self.remote_callback;
unsafe {
remote_callback(host_c_str.as_ptr(), &mut start as *mut u32, &mut end as *mut u32);
}
(start as usize, end as usize)
}
}
adblock::url_parser::set_domain_resolver(Box::new(RemoteResolverImpl {
remote_callback: resolver,
}))
.is_ok()
}
/// Create a new `Engine`, interpreting `data` as a C string and then parsing as
/// a filter list in ABP syntax.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_create_from_buffer(
data: *const c_char,
data_size: size_t,
) -> *mut Engine {
let data: &[u8] = std::slice::from_raw_parts(data as *const u8, data_size);
let rules = std::str::from_utf8(data).unwrap_or_else(|_| {
eprintln!("Failed to parse filter list with invalid UTF-8 content");
""
});
engine_create_from_str(rules).1
}
/// Create a new `Engine`, interpreting `data` as a C string and then parsing as
/// a filter list in ABP syntax. Also populates metadata from the filter list
/// into `metadata`.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_create_from_buffer_with_metadata(
data: *const c_char,
data_size: size_t,
metadata: *mut *mut FilterListMetadata,
) -> *mut Engine {
let data: &[u8] = std::slice::from_raw_parts(data as *const u8, data_size);
let rules = std::str::from_utf8(data).unwrap_or_else(|_| {
eprintln!("Failed to parse filter list with invalid UTF-8 content");
""
});
let (metadata_ptr, engine_ptr) = engine_create_from_str(rules);
*metadata = metadata_ptr;
engine_ptr
}
/// Create a new `Engine`, interpreting `rules` as a null-terminated C string
/// and then parsing as a filter list in ABP syntax.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_create(rules: *const c_char) -> *mut Engine {
let rules = CStr::from_ptr(rules).to_str().unwrap_or_else(|_| {
eprintln!("Failed to parse filter list with invalid UTF-8 content");
""
});
engine_create_from_str(rules).1
}
/// Create a new `Engine`, interpreting `rules` as a null-terminated C string
/// and then parsing as a filter list in ABP syntax. Also populates metadata
/// from the filter list into `metadata`.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_create_with_metadata(
rules: *const c_char,
metadata: *mut *mut FilterListMetadata,
) -> *mut Engine {
let rules = CStr::from_ptr(rules).to_str().unwrap_or_else(|_| {
eprintln!("Failed to parse filter list with invalid UTF-8 content");
""
});
let (metadata_ptr, engine_ptr) = engine_create_from_str(rules);
*metadata = metadata_ptr;
engine_ptr
}
/// Scans the beginning of the list for metadata and returns it without parsing
/// any other list content.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn read_list_metadata(
data: *const c_char,
data_size: size_t,
) -> *mut FilterListMetadata {
let data: &[u8] = std::slice::from_raw_parts(data as *const u8, data_size);
let list = std::str::from_utf8(data).unwrap_or_else(|_| {
eprintln!("Failed to parse filter list with invalid UTF-8 content");
""
});
let metadata = adblock::lists::read_list_metadata(list);
Box::into_raw(Box::new(metadata))
}
fn engine_create_from_str(rules: &str) -> (*mut FilterListMetadata, *mut Engine) {
let mut filter_set = adblock::lists::FilterSet::new(false);
let metadata = filter_set.add_filter_list(&rules, Default::default());
let engine = Engine::from_filter_set(filter_set, true);
(Box::into_raw(Box::new(metadata)), Box::into_raw(Box::new(engine)))
}
/// Checks if a `url` matches for the specified `Engine` within the context.
///
/// This API is designed for multi-engine use, so block results are used both as
/// inputs and outputs. They will be updated to reflect additional checking
/// within this engine, rather than being replaced with results just for this
/// engine.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_match(
engine: *mut Engine,
url: *const c_char,
host: *const c_char,
tab_host: *const c_char,
third_party: bool,
resource_type: *const c_char,
did_match_rule: *mut bool,
did_match_exception: *mut bool,
did_match_important: *mut bool,
redirect: *mut *mut c_char,
rewritten_url: *mut *mut c_char,
) {
let url = CStr::from_ptr(url).to_str().unwrap();
let host = CStr::from_ptr(host).to_str().unwrap();
let tab_host = CStr::from_ptr(tab_host).to_str().unwrap();
let resource_type = CStr::from_ptr(resource_type).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
let blocker_result = engine.check_network_urls_with_hostnames_subset(
url,
host,
tab_host,
resource_type,
Some(third_party),
// Checking normal rules is skipped if a normal rule or exception rule was found previously
*did_match_rule || *did_match_exception,
// Always check exceptions unless one was found previously
!*did_match_exception,
);
*did_match_rule |= blocker_result.matched;
*did_match_exception |= blocker_result.exception.is_some();
*did_match_important |= blocker_result.important;
*redirect = blocker_result
.redirect
.and_then(|x| CString::new(x).map(CString::into_raw).ok())
.unwrap_or(ptr::null_mut());
*rewritten_url = blocker_result
.rewritten_url
.and_then(|x| CString::new(x).map(CString::into_raw).ok())
.unwrap_or(ptr::null_mut());
}
/// Returns any CSP directives that should be added to a subdocument or document
/// request's response headers.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_get_csp_directives(
engine: *mut Engine,
url: *const c_char,
host: *const c_char,
tab_host: *const c_char,
third_party: bool,
resource_type: *const c_char,
) -> *mut c_char {
let url = CStr::from_ptr(url).to_str().unwrap();
let host = CStr::from_ptr(host).to_str().unwrap();
let tab_host = CStr::from_ptr(tab_host).to_str().unwrap();
let resource_type = CStr::from_ptr(resource_type).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
if let Some(directive) =
engine.get_csp_directives(url, host, tab_host, resource_type, Some(third_party))
{
CString::new(directive).expect("Error: CString::new()").into_raw()
} else {
CString::new("").expect("Error: CString::new()").into_raw()
}
}
/// Adds a tag to the engine for consideration
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_add_tag(engine: *mut Engine, tag: *const c_char) {
let tag = CStr::from_ptr(tag).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.enable_tags(&[tag]);
}
/// Checks if a tag exists in the engine
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_tag_exists(engine: *mut Engine, tag: *const c_char) -> bool {
let tag = CStr::from_ptr(tag).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.tag_exists(tag)
}
/// Adds a resource to the engine by name
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_add_resource(
engine: *mut Engine,
key: *const c_char,
content_type: *const c_char,
data: *const c_char,
) -> bool {
let key = CStr::from_ptr(key).to_str().unwrap();
let content_type = CStr::from_ptr(content_type).to_str().unwrap();
let data = CStr::from_ptr(data).to_str().unwrap();
let resource = Resource {
name: key.to_string(),
aliases: vec![],
kind: ResourceType::Mime(MimeType::from(std::borrow::Cow::from(content_type))),
content: data.to_string(),
};
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.add_resource(resource).is_ok()
}
/// Uses a list of `Resource`s from JSON format
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_use_resources(engine: *mut Engine, resources: *const c_char) {
let resources = CStr::from_ptr(resources).to_str().unwrap();
let resources: Vec<Resource> = serde_json::from_str(resources).unwrap_or_else(|e| {
eprintln!("Failed to parse JSON adblock resources: {}", e);
vec![]
});
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.use_resources(&resources);
}
/// Removes a tag to the engine for consideration
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_remove_tag(engine: *mut Engine, tag: *const c_char) {
let tag = CStr::from_ptr(tag).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.disable_tags(&[tag]);
}
/// Deserializes a previously serialized data file list.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_deserialize(
engine: *mut Engine,
data: *const c_char,
data_size: size_t,
) -> bool {
let data: &[u8] = std::slice::from_raw_parts(data as *const u8, data_size);
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
let ok = engine.deserialize(&data).is_ok();
if !ok {
eprintln!("Error deserializing adblock engine");
}
ok
}
/// Destroy a `Engine` once you are done with it.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_destroy(engine: *mut Engine) {
if !engine.is_null() {
drop(Box::from_raw(engine));
}
}
/// Puts a pointer to the homepage of the `FilterListMetadata` into `homepage`.
/// Returns `true` if a homepage was returned.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn filter_list_metadata_homepage(
metadata: *const FilterListMetadata,
homepage: *mut *mut c_char,
) -> bool {
if let Some(this_homepage) = (*metadata).homepage.as_ref() {
let cstring = CString::new(this_homepage.as_str());
match cstring {
Ok(cstring) => {
*homepage = cstring.into_raw();
true
}
Err(_) => false,
}
} else {
false
}
}
/// Puts a pointer to the title of the `FilterListMetadata` into `title`.
/// Returns `true` if a title was returned.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn filter_list_metadata_title(
metadata: *const FilterListMetadata,
title: *mut *mut c_char,
) -> bool {
if let Some(this_title) = (*metadata).title.as_ref() {
let cstring = CString::new(this_title.as_str());
match cstring {
Ok(cstring) => {
*title = cstring.into_raw();
true
}
Err(_) => false,
}
} else {
false
}
}
#[no_mangle]
pub static SUBSCRIPTION_DEFAULT_EXPIRES_HOURS: u16 = 7 * 24;
/// Returns the amount of time this filter list should be considered valid for,
/// in hours. Defaults to 168 (i.e. 7 days) if unspecified by the
/// `FilterListMetadata`.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn filter_list_metadata_expires(metadata: *const FilterListMetadata) -> u16 {
use adblock::lists::ExpiresInterval;
match (*metadata).expires.as_ref() {
Some(ExpiresInterval::Days(d)) => 24 * *d as u16,
Some(ExpiresInterval::Hours(h)) => *h,
None => SUBSCRIPTION_DEFAULT_EXPIRES_HOURS,
}
}
/// Destroy a `FilterListMetadata` once you are done with it.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn filter_list_metadata_destroy(metadata: *mut FilterListMetadata) {
if !metadata.is_null() {
drop(Box::from_raw(metadata));
}
}
/// Get EngineDebugInfo from the engine. Should be destoyed later by calling
/// engine_debug_info_destroy(..).
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn get_engine_debug_info(engine: *mut Engine) -> *mut EngineDebugInfo {
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
Box::into_raw(Box::new(engine.get_debug_info()))
}
/// Returns the field of EngineDebugInfo structure.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_debug_info_get_attr(
debug_info: *mut EngineDebugInfo,
compiled_regex_count: *mut size_t,
regex_data_size: *mut size_t,
) {
assert!(!debug_info.is_null());
let info = Box::leak(Box::from_raw(debug_info));
*compiled_regex_count = info.blocker_debug_info.compiled_regex_count;
*regex_data_size = info.blocker_debug_info.regex_data.len();
}
/// Returns the fields of EngineDebugInfo->regex_data[index].
///
/// |regex| stay untouched if it ==None in the original structure.
///
/// |index| must be in range [0, regex_data.len() - 1].
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_debug_info_get_regex_entry(
debug_info: *mut EngineDebugInfo,
index: size_t,
id: *mut u64,
regex: *mut *mut c_char,
unused_sec: *mut u64,
usage_count: *mut usize,
) {
assert!(!debug_info.is_null());
let info = Box::leak(Box::from_raw(debug_info));
let regex_data = &info.blocker_debug_info.regex_data;
assert!(index < regex_data.len());
let entry = ®ex_data[index];
*id = entry.id;
*regex = CString::new(entry.regex.as_deref().unwrap_or(""))
.expect("Error: CString::new()")
.into_raw();
*unused_sec = entry.last_used.elapsed().as_secs();
*usage_count = entry.usage_count;
}
/// Destroy a `EngineDebugInfo` once you are done with it.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_debug_info_destroy(debug_info: *mut EngineDebugInfo) {
if !debug_info.is_null() {
drop(Box::from_raw(debug_info));
}
}
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn discard_regex(engine: *mut Engine, regex_id: u64) {
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.discard_regex(regex_id);
}
/// Setup discard policy for adblock regexps.
///
/// |cleanup_interval_sec| how ofter the engine should check the policy.
///
/// |discard_unused_sec| time in sec after unused regex will be discarded. Zero
/// means disable discarding completely.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn setup_discard_policy(
engine: *mut Engine,
cleanup_interval_sec: u64,
discard_unused_sec: u64,
) {
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
engine.set_regex_discard_policy(RegexManagerDiscardPolicy {
cleanup_interval: std::time::Duration::from_secs(cleanup_interval_sec),
discard_unused_time: std::time::Duration::from_secs(discard_unused_sec),
});
}
/// Destroy a `*c_char` once you are done with it.
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn c_char_buffer_destroy(s: *mut c_char) {
if !s.is_null() {
drop(CString::from_raw(s));
}
}
/// Returns a set of cosmetic filtering resources specific to the given url, in
/// JSON format
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_url_cosmetic_resources(
engine: *mut Engine,
url: *const c_char,
) -> *mut c_char {
let url = CStr::from_ptr(url).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
CString::new(
serde_json::to_string(&engine.url_cosmetic_resources(url)).unwrap_or_else(|_| "{}".into()),
)
.expect("Error: CString::new()")
.into_raw()
}
/// Returns a stylesheet containing all generic cosmetic rules that begin with
/// any of the provided class and id selectors
///
/// The leading '.' or '#' character should not be provided
#[no_mangle]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn engine_hidden_class_id_selectors(
engine: *mut Engine,
classes: *const *const c_char,
classes_size: size_t,
ids: *const *const c_char,
ids_size: size_t,
exceptions: *const *const c_char,
exceptions_size: size_t,
) -> *mut c_char {
// Note checks for `size == 0` - `std::vector<T>::data()`'s return value when
// empty is undefined behavior and should never be used.
let classes = if classes_size == 0 {
Vec::new()
} else {
let classes = std::slice::from_raw_parts(classes, classes_size);
(0..classes_size)
.map(|index| CStr::from_ptr(classes[index]).to_str().unwrap().to_owned())
.collect()
};
let ids = if ids_size == 0 {
Vec::new()
} else {
let ids = std::slice::from_raw_parts(ids, ids_size);
(0..ids_size).map(|index| CStr::from_ptr(ids[index]).to_str().unwrap().to_owned()).collect()
};
let exceptions = if exceptions_size == 0 {
HashSet::new()
} else {
let exceptions = std::slice::from_raw_parts(exceptions, exceptions_size);
(0..exceptions_size)
.map(|index| CStr::from_ptr(exceptions[index]).to_str().unwrap().to_owned())
.collect()
};
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
let stylesheet = engine.hidden_class_id_selectors(&classes, &ids, &exceptions);
CString::new(serde_json::to_string(&stylesheet).unwrap_or_else(|_| "".into()))
.expect("Error: CString::new()")
.into_raw()
}
/// Converts a list in adblock syntax to its corresponding iOS content-blocking
/// syntax. `truncated` will be set to indicate whether or not some rules had to
/// be removed to avoid iOS's maximum rule count limit.
#[cfg(feature = "ios")]
#[allow(unsafe_op_in_unsafe_fn)]
#[no_mangle]
pub unsafe extern "C" fn convert_rules_to_content_blocking(
rules: *const c_char,
truncated: *mut bool,
) -> *mut c_char {
use adblock::lists::{ParseOptions, RuleTypes};
/// This value corresponds to `maxRuleCount` here:
/// https://github.com/WebKit/WebKit/blob/4a2df13be2253f64d8da58b794d74347a3742652/Source/WebCore/contentextensions/ContentExtensionParser.cpp#L299
const MAX_CB_LIST_SIZE: usize = 150000;
let rules = CStr::from_ptr(rules).to_str().unwrap_or_else(|_| {
eprintln!("Failed to parse filter list with invalid UTF-8 content");
""
});
let mut filter_set = adblock::lists::FilterSet::new(true);
filter_set.add_filter_list(
&rules,
ParseOptions { rule_types: RuleTypes::NetworkOnly, ..Default::default() },
);
// `unwrap` is safe here because `into_content_blocking` only panics if the
// `FilterSet` was not created in debug mode
let (mut cb_rules, _) = filter_set.into_content_blocking().unwrap();
let rules_len = cb_rules.len();
if rules_len > MAX_CB_LIST_SIZE {
// Note that the last rule is always the first-party document exception rule,
// which we want to keep. Otherwise, we can arbitrarily truncate rules
// before that to ensure that the list can actually compile.
cb_rules.swap(rules_len - 1, MAX_CB_LIST_SIZE - 1);
cb_rules.truncate(MAX_CB_LIST_SIZE);
*truncated = true;
} else {
*truncated = false;
}
CString::new(serde_json::to_string(&cb_rules).unwrap_or_else(|_| "".into()))
.expect("Error: CString::new()")
.into_raw()
}