• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "upb/lex/atoi.h"
9 
10 // Must be last.
11 #include "upb/port/def.inc"
12 
upb_BufToUint64(const char * ptr,const char * end,uint64_t * val)13 const char* upb_BufToUint64(const char* ptr, const char* end, uint64_t* val) {
14   uint64_t u64 = 0;
15   while (ptr < end) {
16     unsigned ch = *ptr - '0';
17     if (ch >= 10) break;
18     if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
19       return NULL;  // integer overflow
20     }
21     u64 *= 10;
22     u64 += ch;
23     ptr++;
24   }
25 
26   *val = u64;
27   return ptr;
28 }
29 
upb_BufToInt64(const char * ptr,const char * end,int64_t * val,bool * is_neg)30 const char* upb_BufToInt64(const char* ptr, const char* end, int64_t* val,
31                            bool* is_neg) {
32   bool neg = false;
33   uint64_t u64;
34 
35   if (ptr != end && *ptr == '-') {
36     ptr++;
37     neg = true;
38   }
39 
40   ptr = upb_BufToUint64(ptr, end, &u64);
41   if (!ptr || u64 > (uint64_t)INT64_MAX + neg) {
42     return NULL;  // integer overflow
43   }
44 
45   *val = neg ? -u64 : u64;
46   if (is_neg) *is_neg = neg;
47   return ptr;
48 }
49