1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/message/accessors.h"
29 
30 #include "upb/collections/array.h"
31 #include "upb/collections/array_internal.h"
32 #include "upb/collections/map.h"
33 #include "upb/message/message.h"
34 #include "upb/mini_table/field_internal.h"
35 #include "upb/wire/common.h"
36 #include "upb/wire/decode.h"
37 #include "upb/wire/encode.h"
38 #include "upb/wire/eps_copy_input_stream.h"
39 #include "upb/wire/reader.h"
40 
41 // Must be last.
42 #include "upb/port/def.inc"
43 
upb_Message_InsertMapEntry(upb_Map * map,const upb_MiniTable * mini_table,const upb_MiniTableField * field,upb_Message * map_entry_message,upb_Arena * arena)44 upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
45                                                const upb_MiniTable* mini_table,
46                                                const upb_MiniTableField* field,
47                                                upb_Message* map_entry_message,
48                                                upb_Arena* arena) {
49   const upb_MiniTable* map_entry_mini_table =
50       mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
51   UPB_ASSERT(map_entry_mini_table);
52   UPB_ASSERT(map_entry_mini_table->field_count == 2);
53   const upb_MiniTableField* map_entry_key_field =
54       &map_entry_mini_table->fields[0];
55   const upb_MiniTableField* map_entry_value_field =
56       &map_entry_mini_table->fields[1];
57   // Map key/value cannot have explicit defaults,
58   // hence assuming a zero default is valid.
59   upb_MessageValue default_val;
60   memset(&default_val, 0, sizeof(upb_MessageValue));
61   upb_MessageValue map_entry_key;
62   upb_MessageValue map_entry_value;
63   _upb_Message_GetField(map_entry_message, map_entry_key_field, &default_val,
64                         &map_entry_key);
65   _upb_Message_GetField(map_entry_message, map_entry_value_field, &default_val,
66                         &map_entry_value);
67   return upb_Map_Insert(map, map_entry_key, map_entry_value, arena);
68 }
69