xref: /aosp_15_r20/external/pigweed/pw_snapshot/uuid.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2021 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_snapshot/uuid.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include <cstddef>
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker #include "pw_bytes/span.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_protobuf/decoder.h"
21*61c4878aSAndroid Build Coastguard Worker #include "pw_result/result.h"
22*61c4878aSAndroid Build Coastguard Worker #include "pw_snapshot_metadata_proto/snapshot_metadata.pwpb.h"
23*61c4878aSAndroid Build Coastguard Worker #include "pw_span/span.h"
24*61c4878aSAndroid Build Coastguard Worker #include "pw_status/try.h"
25*61c4878aSAndroid Build Coastguard Worker 
26*61c4878aSAndroid Build Coastguard Worker namespace pw::snapshot {
27*61c4878aSAndroid Build Coastguard Worker 
28*61c4878aSAndroid Build Coastguard Worker using protobuf::Decoder;
29*61c4878aSAndroid Build Coastguard Worker 
ReadUuidFromSnapshot(ConstByteSpan snapshot,UuidSpan output)30*61c4878aSAndroid Build Coastguard Worker Result<ConstByteSpan> ReadUuidFromSnapshot(ConstByteSpan snapshot,
31*61c4878aSAndroid Build Coastguard Worker                                            UuidSpan output) {
32*61c4878aSAndroid Build Coastguard Worker   Decoder decoder(snapshot);
33*61c4878aSAndroid Build Coastguard Worker   ConstByteSpan metadata;
34*61c4878aSAndroid Build Coastguard Worker   while (decoder.Next().ok()) {
35*61c4878aSAndroid Build Coastguard Worker     if (decoder.FieldNumber() ==
36*61c4878aSAndroid Build Coastguard Worker         static_cast<uint32_t>(
37*61c4878aSAndroid Build Coastguard Worker             pw::snapshot::pwpb::SnapshotBasicInfo::Fields::kMetadata)) {
38*61c4878aSAndroid Build Coastguard Worker       PW_TRY(decoder.ReadBytes(&metadata));
39*61c4878aSAndroid Build Coastguard Worker       break;
40*61c4878aSAndroid Build Coastguard Worker     }
41*61c4878aSAndroid Build Coastguard Worker   }
42*61c4878aSAndroid Build Coastguard Worker   if (metadata.empty()) {
43*61c4878aSAndroid Build Coastguard Worker     return Status::NotFound();
44*61c4878aSAndroid Build Coastguard Worker   }
45*61c4878aSAndroid Build Coastguard Worker 
46*61c4878aSAndroid Build Coastguard Worker   // Start to read from the metadata.
47*61c4878aSAndroid Build Coastguard Worker   decoder.Reset(metadata);
48*61c4878aSAndroid Build Coastguard Worker   ConstByteSpan snapshot_uuid;
49*61c4878aSAndroid Build Coastguard Worker   while (decoder.Next().ok()) {
50*61c4878aSAndroid Build Coastguard Worker     if (decoder.FieldNumber() ==
51*61c4878aSAndroid Build Coastguard Worker         static_cast<uint32_t>(
52*61c4878aSAndroid Build Coastguard Worker             pw::snapshot::pwpb::Metadata::Fields::kSnapshotUuid)) {
53*61c4878aSAndroid Build Coastguard Worker       PW_TRY(decoder.ReadBytes(&snapshot_uuid));
54*61c4878aSAndroid Build Coastguard Worker       break;
55*61c4878aSAndroid Build Coastguard Worker     }
56*61c4878aSAndroid Build Coastguard Worker   }
57*61c4878aSAndroid Build Coastguard Worker   if (snapshot_uuid.empty()) {
58*61c4878aSAndroid Build Coastguard Worker     return Status::NotFound();
59*61c4878aSAndroid Build Coastguard Worker   }
60*61c4878aSAndroid Build Coastguard Worker   if (snapshot_uuid.size_bytes() > output.size_bytes()) {
61*61c4878aSAndroid Build Coastguard Worker     return Status::ResourceExhausted();
62*61c4878aSAndroid Build Coastguard Worker   }
63*61c4878aSAndroid Build Coastguard Worker 
64*61c4878aSAndroid Build Coastguard Worker   memcpy(output.data(), snapshot_uuid.data(), snapshot_uuid.size_bytes());
65*61c4878aSAndroid Build Coastguard Worker   return ConstByteSpan(output.first(snapshot_uuid.size_bytes()));
66*61c4878aSAndroid Build Coastguard Worker }
67*61c4878aSAndroid Build Coastguard Worker 
68*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::snapshot
69