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
// libwebp v1.6.0, src/utils/utils.c:
//
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Misc. common utility functions
//
// Author: Skal (pascal.massimino@gmail.com)
// libwebp v1.6.0, src/utils/utils.h:
//
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Misc. common utility functions
//
// Authors: Skal (pascal.massimino@gmail.com)
// Urvang (urvang@google.com)
//! Translation of `src/utils/utils.c` and `src/utils/utils.h` from libwebp v1.6.0.
//!
//! Work package: WP1 (types, memory, workers).
//!
//! Exception: WP3 translates `WebPGetColorPalette` from `src/utils/utils.c`, because it calls `GetColorPalette` from `src/utils/palette.c`.
//!
//! Not translated here, with the reason:
//! - `WebPMalloc` and `WebPFree` hand memory to C callers; they belong to the C
//! ABI module (ADR 0004, `src/abi/types.rs`, WP12).
//! - `WebPSafeFree` only frees; the owner of a `Vec` drops it instead.
//! - `WebPLog2FloorC` and `WebPLogTable8bit` are the fallback of `BitsLog2Floor`
//! and `BitsCtz`, which GCC and Clang never compile; they use the builtins.
//! - The `PRINT_MEM_INFO` bookkeeping is off in the reference configuration.
//!
//! # Excluded undefined behavior
//!
//! Inputs that are undefined behavior (or assertion failures) in C and are
//! therefore excluded from the oracle comparison (PORTING rule 8):
//!
//! 1. `BitsLog2Floor(0)` and `BitsCtz(0)`. The C builtins are undefined for
//! zero. The Rust `bits_ctz(0)` returns 32, the value of the portable
//! fallback, and `bits_log2_floor(0)` debug-asserts. Neither is compared.
//! 2. `WebPSafeMalloc` and `WebPSafeCalloc` with a zero count or a zero size.
//! The C functions assert `nmemb * size > 0` on the accepted path
//! (`utils.c`, lines 200 and 210), so the C decision is undefined: the debug
//! oracle aborts, and the other variants return whatever `malloc(0)`
//! returns. These requests are excluded from the comparison in every
//! variant, and the probe exits with status 2 on a zero value.
//! `zero_size_requests_are_recorded_for_rust_only` (`tests/wp1_alloc.rs`)
//! records the Rust decision, which accepts them. A product that wraps in 64
//! bits fails the size test before the assertion, so it stays compared.
use crateWebPPicture;
// upstream: src/utils/utils.h WEBP_ALIGN_CST
/// Mask of the alignment that `webp_align` applies.
pub const WEBP_ALIGN_CST: usize = 31;
// upstream: src/utils/utils.h WEBP_MAX_ALLOCABLE_MEMORY
/// The largest amount of memory that libwebp will try to allocate.
pub const WEBP_MAX_ALLOCABLE_MEMORY: u64 = if usizeMAX as u64 > else ;
// upstream: src/utils/utils.h CheckSizeOverflow
/// Reports whether a byte count is representable as a `usize`.
// upstream: src/utils/utils.c CheckSizeArgumentsOverflow
/// The size test of `WebPSafeMalloc` and `WebPSafeCalloc`: zero elements always
/// pass; otherwise `size` must not exceed `WEBP_MAX_ALLOCABLE_MEMORY / nmemb`,
/// and the product must fit in `usize`. The product wraps in 64 bits, as in C.
// upstream: src/utils/utils.c WebPSafeMalloc
/// Reserves room for `nmemb` elements of `T`, after the size test of
/// `WebPSafeMalloc`.
///
/// `size` is the C `sizeof` of one element, so that the decision matches the C
/// function even where the Rust type has another size. The returned vector is
/// empty with a capacity of at least `nmemb`; the caller initializes the
/// elements it reads. Returns `None` when the size test fails or the host
/// refuses the reservation, which is where C returns `NULL`.
// upstream: src/utils/utils.c WebPSafeCalloc
/// Like [`webp_safe_malloc`], with `nmemb` elements set to `T::default()`, which
/// is the zero value that C's `calloc` provides for the numeric types.
// upstream: src/utils/utils.h WEBP_ALIGN
/// Rounds an address up to a multiple of 32, as `WEBP_ALIGN` does for a
/// `uintptr_t`. The address is passed as an integer, because Rust code keeps no
/// raw pointers outside the C ABI.
// upstream: src/utils/utils.h WebPMemToUint32
/// Reads four bytes in host byte order, as the C `memcpy` does.
///
/// # Panics
///
/// Panics when `ptr` holds fewer than four bytes; C reads past the buffer there.
// upstream: src/utils/utils.h WebPMemToInt32
/// Reads four bytes in host byte order as an `i32`.
///
/// # Panics
///
/// Panics when `ptr` holds fewer than four bytes.
// upstream: src/utils/utils.h WebPUint32ToMem
/// Writes `val` in host byte order into the first four bytes of `ptr`.
///
/// # Panics
///
/// Panics when `ptr` holds fewer than four bytes.
// upstream: src/utils/utils.h WebPInt32ToMem
/// Writes `val` in host byte order into the first four bytes of `ptr`.
///
/// # Panics
///
/// Panics when `ptr` holds fewer than four bytes.
// upstream: src/utils/utils.h GetLE16
/// Reads a 16-bit little-endian value.
///
/// # Panics
///
/// Panics when `data` holds fewer than two bytes.
// upstream: src/utils/utils.h GetLE24
/// Reads a 24-bit little-endian value.
///
/// # Panics
///
/// Panics when `data` holds fewer than three bytes.
// upstream: src/utils/utils.h GetLE32
/// Reads a 32-bit little-endian value.
///
/// # Panics
///
/// Panics when `data` holds fewer than four bytes.
/// Keeps the low byte of `value`, as the C conversion `(val >> k) & 0xff` does.
// upstream: src/utils/utils.h PutLE16
/// Writes a 16-bit little-endian value.
///
/// # Panics
///
/// Panics when `data` holds fewer than two bytes.
// upstream: src/utils/utils.h PutLE24
/// Writes a 24-bit little-endian value.
///
/// # Panics
///
/// Panics when `data` holds fewer than three bytes.
// upstream: src/utils/utils.h PutLE32
/// Writes a 32-bit little-endian value.
///
/// # Panics
///
/// Panics when `data` holds fewer than four bytes.
// upstream: src/utils/utils.h BitsLog2Floor
/// Returns `floor(log2(n))`, the builtin `31 ^ __builtin_clz(n)` of GCC and
/// Clang. `n` must be positive; the builtin is undefined for zero.
// upstream: src/utils/utils.h BitsCtz
/// Returns the number of trailing zero bits, the builtin `__builtin_ctz(n)`.
/// The builtin is undefined for zero; this returns 32, as the portable C
/// fallback does.
/// Start of row `row` of a plane whose first row starts at `offset` and whose
/// rows are `stride` bytes apart; a negative stride walks upward.
// upstream: src/utils/utils.c WebPCopyPlane
/// Copies `width` by `height` bytes from `src` to `dst`, honoring both strides.
/// The first row of `src` starts at `src_offset`, and row `y` at
/// `src_offset + y * src_stride`; `dst` is addressed the same way.
///
/// # Panics
///
/// Panics when a row falls outside its slice, or when `width` is negative and
/// `height` positive; the C function leaves both undefined.
/// Copies `width` by `height` samples of any type from `src` to `dst`, honoring both
/// strides. This is the row loop of `WebPCopyPlane`, which copies bytes, and of
/// `WebPCopyPixels`, which copies `u32` samples with the same row arithmetic.
// upstream: src/utils/utils.c WebPCopyPixels
/// Copies the ARGB samples of `src` into `dst`, honoring both strides. C copies
/// `4 * width` bytes per row, with `4 * argb_stride` bytes between rows, and this
/// copies the same `u32` samples with the strides in pixels.
///
/// # Panics
///
/// Panics when either picture has no ARGB plane or a row falls outside its storage.
/// C asserts equal dimensions and `use_argb` in debug builds; the same asserts run
/// here in the debug and `checked` profiles.
//------------------------------------------------------------------------------
// upstream: src/utils/utils.c WebPGetColorPalette
/// Returns count of unique colors in `pic`, assuming `pic.use_argb` is true.
/// If the unique color count is more than `MAX_PALETTE_SIZE`, returns
/// `MAX_PALETTE_SIZE`+1.
/// If `palette` is not `None` and the number of unique colors is less than or
/// equal to `MAX_PALETTE_SIZE`, also outputs the actual unique colors into
/// `palette` in a sorted order. Note: `palette` is assumed to be an array
/// already allocated with at least `MAX_PALETTE_SIZE` elements.
///
/// Exception item of WP3 in WP1's module (`tools/units.json`).