xapian-core  1.4.26
length.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2015 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "length.h"
24 
25 #include "noreturn.h"
26 
27 XAPIAN_NORETURN(static void throw_network_error(const char * msg));
28 
29 #ifndef XAPIAN_UNITTEST
30 
31 #include "xapian/error.h"
32 
33 static void
34 throw_network_error(const char * msg)
35 {
36  throw Xapian::NetworkError(msg);
37 }
38 
39 #else
40 
41 class Xapian_NetworkError {
42  const char * msg;
43 
44  public:
45  explicit Xapian_NetworkError(const char * msg_) : msg(msg_) { }
46 
47  const char * get_description() const { return msg; }
48 };
49 
50 static void
51 throw_network_error(const char * msg)
52 {
53  throw Xapian_NetworkError(msg);
54 }
55 
56 #endif
57 
58 template<typename T>
59 static inline void
60 decode_length_(const char ** p, const char *end, T & out)
61 {
62  if (*p == end) {
63  throw_network_error("Bad encoded length: no data");
64  }
65 
66  T len = static_cast<unsigned char>(*(*p)++);
67  if (len == 0xff) {
68  len = 0;
69  unsigned char ch;
70  unsigned shift = 0;
71  do {
72  if (*p == end || shift > (sizeof(T) * 8 / 7 * 7))
73  throw_network_error("Bad encoded length: insufficient data");
74  ch = *(*p)++;
75  len |= T(ch & 0x7f) << shift;
76  shift += 7;
77  } while ((ch & 0x80) == 0);
78  len += 255;
79  }
80  out = len;
81 }
82 
83 template<typename T>
84 static inline void
85 decode_length_and_check_(const char ** p, const char *end, T & out)
86 {
87  decode_length(p, end, out);
88  if (out > T(end - *p)) {
89  throw_network_error("Bad encoded length: length greater than data");
90  }
91 }
92 
93 void
94 decode_length(const char ** p, const char *end, unsigned & out)
95 {
96  decode_length_(p, end, out);
97 }
98 
99 void
100 decode_length(const char ** p, const char *end, unsigned long & out)
101 {
102  decode_length_(p, end, out);
103 }
104 
105 void
106 decode_length(const char ** p, const char *end, unsigned long long & out)
107 {
108  decode_length_(p, end, out);
109 }
110 
111 void
112 decode_length_and_check(const char ** p, const char *end, unsigned & out)
113 {
114  decode_length_and_check_(p, end, out);
115 }
116 
117 void
118 decode_length_and_check(const char ** p, const char *end, unsigned long & out)
119 {
120  decode_length_and_check_(p, end, out);
121 }
122 
123 void
124 decode_length_and_check(const char ** p, const char *end,
125  unsigned long long & out)
126 {
127  decode_length_and_check_(p, end, out);
128 }
Define the XAPIAN_NORETURN macro.
length encoded as a string
static void decode_length_and_check_(const char **p, const char *end, T &out)
Definition: length.cc:85
Hierarchy of classes which Xapian can throw as exceptions.
static void decode_length_(const char **p, const char *end, T &out)
Definition: length.cc:60
void decode_length_and_check(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
Definition: length.cc:112
Indicates a problem communicating with a remote database.
Definition: error.h:803
static void throw_network_error(const char *msg)
Definition: length.cc:34
void decode_length(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
Definition: length.cc:94