blob: 944348f30022f14f19859d2bd27ebb3c1d26893f [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "proto_format.h"
18
19#include <string.h>
20
21extern int const PROTO_FORMAT_STRING_POOL_SIZE;
22extern int const PROTO_FORMAT_ENUM_LABELS_LENGTH;
23extern int const PROTO_FORMAT_MESSAGES_LENGTH;
24extern int const PROTO_FORMAT_FIELDS_LENGTH;
25
26extern char const PROTO_FORMAT_STRING_POOL[];
27extern ProtoFieldFormat const PROTO_FORMAT_FIELDS[];
28extern ProtoEnumLabel const PROTO_FORMAT_ENUM_LABELS[];
29extern ProtoMessageFormat const PROTO_FORMAT_MESSAGES[];
30
31static const char*
32get_string(int index)
33{
34 if (index >= 0 && index < PROTO_FORMAT_STRING_POOL_SIZE) {
35 return PROTO_FORMAT_STRING_POOL + index;
36 } else {
37 // These indices all come from within the generated table, so just crash now.
38 *(int*)NULL = 42;
39 return NULL;
40 }
41}
42
43static ProtoMessageFormat const*
44get_message(int index)
45{
46 if (index >= 0 && index < PROTO_FORMAT_MESSAGES_LENGTH) {
47 return PROTO_FORMAT_MESSAGES + index;
48 } else {
49 // These indices all come from within the generated table, so just crash now.
50 *(int*)NULL = 42;
51 return NULL;
52 }
53}
54
55static int
56compare_name(const char* full, const char* package, const char* clazz)
57{
58 int const packageLen = strlen(package);
59 int cmp = strncmp(full, package, packageLen);
60 if (cmp == 0) {
61 cmp = full[packageLen] - '.';
62 if (cmp == 0) {
63 return strcmp(full + packageLen, clazz);
64 }
65 }
66 return cmp;
67}
68
69int
70find_message_index(const char* name)
71{
72 size_t low = 0;
73 size_t high = PROTO_FORMAT_FIELDS_LENGTH - 1;
74
75 while (low <= high) {
76 size_t mid = (low + high) >> 1;
77 ProtoMessageFormat const* msg = get_message(mid);
78
79 int cmp = compare_name(name, get_string(msg->package_name), get_string(msg->package_name));
80 if (cmp < 0) {
81 low = mid + 1;
82 } else if (cmp > 0) {
83 high = mid - 1;
84 } else {
85 return mid;
86 }
87 }
88 return -1;
89}