Coverage Report

Created: 2026-03-28 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/protobuf-3.2.0/src/unknown.rs
Line
Count
Source
1
use std::collections::hash_map;
2
use std::collections::hash_map::DefaultHasher;
3
use std::collections::HashMap;
4
use std::default::Default;
5
use std::hash::BuildHasherDefault;
6
use std::hash::Hash;
7
use std::hash::Hasher;
8
use std::slice;
9
10
use crate::reflect::ReflectValueRef;
11
use crate::rt;
12
use crate::wire_format::WireType;
13
use crate::zigzag::encode_zig_zag_32;
14
use crate::zigzag::encode_zig_zag_64;
15
use crate::CodedOutputStream;
16
17
/// Unknown value.
18
///
19
/// See [`UnknownFields`](crate::UnknownFields) for the explanations.
20
#[derive(Debug)]
21
pub enum UnknownValue {
22
    /// 32-bit unknown (e. g. `fixed32` or `float`)
23
    Fixed32(u32),
24
    /// 64-bit unknown (e. g. `fixed64` or `double`)
25
    Fixed64(u64),
26
    /// Varint unknown (e. g. `int32` or `bool`)
27
    Varint(u64),
28
    /// Length-delimited unknown (e. g. `message` or `string`)
29
    LengthDelimited(Vec<u8>),
30
}
31
32
impl UnknownValue {
33
    /// Wire type for this unknown
34
0
    pub fn wire_type(&self) -> WireType {
35
0
        self.get_ref().wire_type()
36
0
    }
37
38
    /// As ref
39
0
    pub fn get_ref<'s>(&'s self) -> UnknownValueRef<'s> {
40
0
        match *self {
41
0
            UnknownValue::Fixed32(fixed32) => UnknownValueRef::Fixed32(fixed32),
42
0
            UnknownValue::Fixed64(fixed64) => UnknownValueRef::Fixed64(fixed64),
43
0
            UnknownValue::Varint(varint) => UnknownValueRef::Varint(varint),
44
0
            UnknownValue::LengthDelimited(ref bytes) => UnknownValueRef::LengthDelimited(&bytes),
45
        }
46
0
    }
47
48
    /// Construct unknown value from `int64` value.
49
0
    pub fn int32(i: i32) -> UnknownValue {
50
0
        UnknownValue::int64(i as i64)
51
0
    }
52
53
    /// Construct unknown value from `int64` value.
54
0
    pub fn int64(i: i64) -> UnknownValue {
55
0
        UnknownValue::Varint(i as u64)
56
0
    }
57
58
    /// Construct unknown value from `sint32` value.
59
0
    pub fn sint32(i: i32) -> UnknownValue {
60
0
        UnknownValue::Varint(encode_zig_zag_32(i) as u64)
61
0
    }
62
63
    /// Construct unknown value from `sint64` value.
64
0
    pub fn sint64(i: i64) -> UnknownValue {
65
0
        UnknownValue::Varint(encode_zig_zag_64(i))
66
0
    }
67
68
    /// Construct unknown value from `float` value.
69
0
    pub fn float(f: f32) -> UnknownValue {
70
0
        UnknownValue::Fixed32(f.to_bits())
71
0
    }
72
73
    /// Construct unknown value from `double` value.
74
0
    pub fn double(f: f64) -> UnknownValue {
75
0
        UnknownValue::Fixed64(f.to_bits())
76
0
    }
77
78
    /// Construct unknown value from `sfixed32` value.
79
0
    pub fn sfixed32(i: i32) -> UnknownValue {
80
0
        UnknownValue::Fixed32(i as u32)
81
0
    }
82
83
    /// Construct unknown value from `sfixed64` value.
84
0
    pub fn sfixed64(i: i64) -> UnknownValue {
85
0
        UnknownValue::Fixed64(i as u64)
86
0
    }
87
}
88
89
/// Reference to unknown value.
90
///
91
/// See [`UnknownFields`](crate::UnknownFields) for explanations.
92
#[derive(Debug, PartialEq)]
93
pub enum UnknownValueRef<'o> {
94
    /// 32-bit unknown
95
    Fixed32(u32),
96
    /// 64-bit unknown
97
    Fixed64(u64),
98
    /// Varint unknown
99
    Varint(u64),
100
    /// Length-delimited unknown
101
    LengthDelimited(&'o [u8]),
102
}
103
104
impl<'o> UnknownValueRef<'o> {
105
    /// Wire-type to serialize this unknown
106
0
    pub fn wire_type(&self) -> WireType {
107
0
        match *self {
108
0
            UnknownValueRef::Fixed32(_) => WireType::Fixed32,
109
0
            UnknownValueRef::Fixed64(_) => WireType::Fixed64,
110
0
            UnknownValueRef::Varint(_) => WireType::Varint,
111
0
            UnknownValueRef::LengthDelimited(_) => WireType::LengthDelimited,
112
        }
113
0
    }
114
115
0
    pub(crate) fn to_reflect_value_ref(&'o self) -> ReflectValueRef<'o> {
116
0
        match self {
117
0
            UnknownValueRef::Fixed32(v) => ReflectValueRef::U32(*v),
118
0
            UnknownValueRef::Fixed64(v) => ReflectValueRef::U64(*v),
119
0
            UnknownValueRef::Varint(v) => ReflectValueRef::U64(*v),
120
0
            UnknownValueRef::LengthDelimited(v) => ReflectValueRef::Bytes(v),
121
        }
122
0
    }
123
}
124
125
/// Field unknown values.
126
///
127
/// See [`UnknownFields`](crate::UnknownFields) for explanations.
128
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
129
pub(crate) struct UnknownValues {
130
    /// 32-bit unknowns
131
    pub(crate) fixed32: Vec<u32>,
132
    /// 64-bit unknowns
133
    pub(crate) fixed64: Vec<u64>,
134
    /// Varint unknowns
135
    pub(crate) varint: Vec<u64>,
136
    /// Length-delimited unknowns
137
    pub(crate) length_delimited: Vec<Vec<u8>>,
138
}
139
140
impl UnknownValues {
141
    /// Add unknown value
142
0
    pub fn add_value(&mut self, value: UnknownValue) {
143
0
        match value {
144
0
            UnknownValue::Fixed64(fixed64) => self.fixed64.push(fixed64),
145
0
            UnknownValue::Fixed32(fixed32) => self.fixed32.push(fixed32),
146
0
            UnknownValue::Varint(varint) => self.varint.push(varint),
147
0
            UnknownValue::LengthDelimited(length_delimited) => {
148
0
                self.length_delimited.push(length_delimited)
149
            }
150
        };
151
0
    }
152
153
    /// Iterate over unknown values
154
0
    pub fn iter<'s>(&'s self) -> UnknownValuesIter<'s> {
155
0
        UnknownValuesIter {
156
0
            fixed32: self.fixed32.iter(),
157
0
            fixed64: self.fixed64.iter(),
158
0
            varint: self.varint.iter(),
159
0
            length_delimited: self.length_delimited.iter(),
160
0
        }
161
0
    }
162
163
0
    pub(crate) fn any(&self) -> Option<UnknownValueRef> {
164
0
        if let Some(last) = self.fixed32.last() {
165
0
            Some(UnknownValueRef::Fixed32(*last))
166
0
        } else if let Some(last) = self.fixed64.last() {
167
0
            Some(UnknownValueRef::Fixed64(*last))
168
0
        } else if let Some(last) = self.varint.last() {
169
0
            Some(UnknownValueRef::Varint(*last))
170
0
        } else if let Some(last) = self.length_delimited.last() {
171
0
            Some(UnknownValueRef::LengthDelimited(last))
172
        } else {
173
0
            None
174
        }
175
0
    }
176
}
177
178
impl<'a> IntoIterator for &'a UnknownValues {
179
    type Item = UnknownValueRef<'a>;
180
    type IntoIter = UnknownValuesIter<'a>;
181
182
0
    fn into_iter(self) -> UnknownValuesIter<'a> {
183
0
        self.iter()
184
0
    }
185
}
186
187
/// Iterator over unknown values
188
pub(crate) struct UnknownValuesIter<'o> {
189
    fixed32: slice::Iter<'o, u32>,
190
    fixed64: slice::Iter<'o, u64>,
191
    varint: slice::Iter<'o, u64>,
192
    length_delimited: slice::Iter<'o, Vec<u8>>,
193
}
194
195
impl<'o> Iterator for UnknownValuesIter<'o> {
196
    type Item = UnknownValueRef<'o>;
197
198
0
    fn next(&mut self) -> Option<UnknownValueRef<'o>> {
199
0
        if let Some(fixed32) = self.fixed32.next() {
200
0
            return Some(UnknownValueRef::Fixed32(*fixed32));
201
0
        }
202
0
        if let Some(fixed64) = self.fixed64.next() {
203
0
            return Some(UnknownValueRef::Fixed64(*fixed64));
204
0
        }
205
0
        if let Some(varint) = self.varint.next() {
206
0
            return Some(UnknownValueRef::Varint(*varint));
207
0
        }
208
0
        if let Some(length_delimited) = self.length_delimited.next() {
209
0
            return Some(UnknownValueRef::LengthDelimited(&length_delimited));
210
0
        }
211
0
        None
212
0
    }
213
}
214
215
/// Hold "unknown" fields in parsed message.
216
///
217
/// Field may be unknown if it they are added in newer version of `.proto`.
218
/// Unknown fields are stored in `UnknownFields` structure, so
219
/// protobuf message could process messages without losing data.
220
///
221
/// For example, in this operation: load from DB, modify, store to DB,
222
/// even when working with older `.proto` file, new fields won't be lost.
223
#[derive(Clone, PartialEq, Eq, Debug, Default)]
224
pub struct UnknownFields {
225
    /// The map.
226
    //
227
    // `Option` is needed, because HashMap constructor performs allocation,
228
    // and very expensive.
229
    //
230
    // We use "default hasher" to make iteration order deterministic.
231
    // Which is used to make codegen output deterministic in presence of unknown fields
232
    // (e. g. file options are represented as unknown fields).
233
    // Using default hasher is suboptimal, because it makes unknown fields less safe.
234
    // Note, Google Protobuf C++ simply uses linear map (which can exploitable the same way),
235
    // and Google Protobuf Java uses tree map to store unknown fields
236
    // (which is more expensive than hashmap).
237
    fields: Option<Box<HashMap<u32, UnknownValues, BuildHasherDefault<DefaultHasher>>>>,
238
}
239
240
/// Very simple hash implementation of `Hash` for `UnknownFields`.
241
/// Since map is unordered, we cannot put entry hashes into hasher,
242
/// instead we summing hashes of entries.
243
impl Hash for UnknownFields {
244
0
    fn hash<H: Hasher>(&self, state: &mut H) {
245
0
        if let Some(ref map) = self.fields {
246
0
            if !map.is_empty() {
247
0
                let mut hash: u64 = 0;
248
0
                for (k, v) in &**map {
249
0
                    let mut entry_hasher = DefaultHasher::new();
250
0
                    Hash::hash(&(k, v), &mut entry_hasher);
251
0
                    hash = hash.wrapping_add(entry_hasher.finish());
252
0
                }
253
0
                Hash::hash(&map.len(), state);
254
0
                Hash::hash(&hash, state);
255
0
            }
256
0
        }
257
0
    }
258
}
259
260
impl UnknownFields {
261
    /// Empty unknown fields.
262
0
    pub const fn new() -> UnknownFields {
263
0
        UnknownFields { fields: None }
264
0
    }
265
266
    /// Clear all unknown fields.
267
0
    pub fn clear(&mut self) {
268
0
        if let Some(ref mut fields) = self.fields {
269
0
            fields.clear();
270
0
        }
271
0
    }
272
273
0
    fn init_map(&mut self) {
274
0
        if self.fields.is_none() {
275
0
            self.fields = Some(Default::default());
276
0
        }
277
0
    }
278
279
0
    fn find_field<'a>(&'a mut self, number: &'a u32) -> &'a mut UnknownValues {
280
0
        self.init_map();
281
282
0
        match self.fields.as_mut().unwrap().entry(*number) {
283
0
            hash_map::Entry::Occupied(e) => e.into_mut(),
284
0
            hash_map::Entry::Vacant(e) => e.insert(Default::default()),
285
        }
286
0
    }
287
288
    /// Add unknown fixed 32-bit
289
0
    pub fn add_fixed32(&mut self, number: u32, fixed32: u32) {
290
0
        self.find_field(&number).fixed32.push(fixed32);
291
0
    }
292
293
    /// Add unknown fixed 64-bit
294
0
    pub fn add_fixed64(&mut self, number: u32, fixed64: u64) {
295
0
        self.find_field(&number).fixed64.push(fixed64);
296
0
    }
297
298
    /// Add unknown varint
299
0
    pub fn add_varint(&mut self, number: u32, varint: u64) {
300
0
        self.find_field(&number).varint.push(varint);
301
0
    }
302
303
    /// Add unknown length delimited
304
0
    pub fn add_length_delimited(&mut self, number: u32, length_delimited: Vec<u8>) {
305
0
        self.find_field(&number)
306
0
            .length_delimited
307
0
            .push(length_delimited);
308
0
    }
309
310
    /// Add unknown value
311
0
    pub fn add_value(&mut self, number: u32, value: UnknownValue) {
312
0
        self.find_field(&number).add_value(value);
313
0
    }
314
315
    /// Remove unknown field by number
316
0
    pub fn remove(&mut self, field_number: u32) {
317
0
        if let Some(fields) = &mut self.fields {
318
0
            fields.remove(&field_number);
319
0
        }
320
0
    }
321
322
    /// Iterate over all unknowns
323
0
    pub fn iter<'s>(&'s self) -> UnknownFieldsIter<'s> {
324
        UnknownFieldsIter {
325
0
            entries: self.fields.as_ref().map(|m| UnknownFieldsNotEmptyIter {
326
0
                fields: m.iter(),
327
0
                current: None,
328
0
            }),
329
        }
330
0
    }
331
332
    /// Get any value for unknown fields.
333
0
    pub fn get(&self, field_number: u32) -> Option<UnknownValueRef> {
334
0
        match &self.fields {
335
0
            Some(map) => map.get(&field_number).and_then(|v| v.any()),
336
0
            None => None,
337
        }
338
0
    }
339
340
    #[doc(hidden)]
341
0
    pub fn write_to_bytes(&self) -> Vec<u8> {
342
0
        let mut r = Vec::with_capacity(rt::unknown_fields_size(self) as usize);
343
0
        let mut stream = CodedOutputStream::vec(&mut r);
344
        // Do we need it stable everywhere?
345
0
        stream.write_unknown_fields_sorted(self).unwrap();
346
0
        stream.flush().unwrap();
347
0
        drop(stream);
348
0
        r
349
0
    }
350
}
351
352
impl<'a> IntoIterator for &'a UnknownFields {
353
    type Item = (u32, UnknownValueRef<'a>);
354
    type IntoIter = UnknownFieldsIter<'a>;
355
356
0
    fn into_iter(self) -> UnknownFieldsIter<'a> {
357
0
        self.iter()
358
0
    }
359
}
360
361
struct UnknownFieldsNotEmptyIter<'s> {
362
    fields: hash_map::Iter<'s, u32, UnknownValues>,
363
    current: Option<(u32, UnknownValuesIter<'s>)>,
364
}
365
366
/// Iterator over [`UnknownFields`](crate::UnknownFields)
367
pub struct UnknownFieldsIter<'s> {
368
    entries: Option<UnknownFieldsNotEmptyIter<'s>>,
369
}
370
371
impl<'s> Iterator for UnknownFieldsNotEmptyIter<'s> {
372
    type Item = (u32, UnknownValueRef<'s>);
373
374
0
    fn next(&mut self) -> Option<(u32, UnknownValueRef<'s>)> {
375
        loop {
376
0
            if let Some((field_number, values)) = &mut self.current {
377
0
                if let Some(value) = values.next() {
378
0
                    return Some((*field_number, value));
379
0
                }
380
0
            }
381
0
            let (field_number, values) = self.fields.next()?;
382
0
            self.current = Some((*field_number, values.iter()));
383
        }
384
0
    }
385
}
386
387
impl<'s> Iterator for UnknownFieldsIter<'s> {
388
    type Item = (u32, UnknownValueRef<'s>);
389
390
0
    fn next(&mut self) -> Option<(u32, UnknownValueRef<'s>)> {
391
0
        self.entries.as_mut().and_then(|entries| entries.next())
392
0
    }
393
}
394
395
#[cfg(test)]
396
mod test {
397
    use std::collections::hash_map::DefaultHasher;
398
    use std::hash::Hash;
399
    use std::hash::Hasher;
400
401
    use super::UnknownFields;
402
403
    #[test]
404
    fn unknown_fields_hash() {
405
        let mut unknown_fields_1 = UnknownFields::new();
406
        let mut unknown_fields_2 = UnknownFields::new();
407
408
        // Check field order is not important
409
410
        unknown_fields_1.add_fixed32(10, 222);
411
        unknown_fields_1.add_fixed32(10, 223);
412
        unknown_fields_1.add_fixed64(14, 224);
413
414
        unknown_fields_2.add_fixed32(10, 222);
415
        unknown_fields_2.add_fixed64(14, 224);
416
        unknown_fields_2.add_fixed32(10, 223);
417
418
        fn hash(unknown_fields: &UnknownFields) -> u64 {
419
            let mut hasher = DefaultHasher::new();
420
            Hash::hash(unknown_fields, &mut hasher);
421
            hasher.finish()
422
        }
423
424
        assert_eq!(hash(&unknown_fields_1), hash(&unknown_fields_2));
425
    }
426
427
    #[test]
428
    fn unknown_fields_iteration_order_deterministic() {
429
        let mut u_1 = UnknownFields::new();
430
        let mut u_2 = UnknownFields::new();
431
        for u in &mut [&mut u_1, &mut u_2] {
432
            u.add_fixed32(10, 20);
433
            u.add_varint(30, 40);
434
            u.add_fixed64(50, 60);
435
            u.add_length_delimited(70, Vec::new());
436
            u.add_varint(80, 90);
437
            u.add_fixed32(11, 22);
438
            u.add_fixed64(33, 44);
439
        }
440
441
        let items_1: Vec<_> = u_1.iter().collect();
442
        let items_2: Vec<_> = u_2.iter().collect();
443
        assert_eq!(items_1, items_2);
444
    }
445
}