Esempio n. 1
0
static char *
scan_to_unichar (const char      *str,
                 bson_unichar_t   match,
                 const char      *terminators,
                 const char     **end)
{
   bson_unichar_t c;
   const char *iter;

   for (iter = str;
        iter && *iter && (c = bson_utf8_get_char(iter));
        iter = bson_utf8_next_char(iter)) {
      if (c == match) {
         *end = iter;
         return bson_strndup(str, iter - str);
      } else if (c == '\\') {
         iter = bson_utf8_next_char(iter);
         if (!bson_utf8_get_char(iter)) {
            break;
         }
      } else {
         const char *term_iter;
         for (term_iter = terminators; *term_iter; term_iter++) {
            if (c == *term_iter) {
               return NULL;
            }
         }
      }
   }

   return NULL;
}
Esempio n. 2
0
static char *
scan_to_unichar (const char      *str,
                 bson_unichar_t   stop,
                 const char     **end)
{
   bson_unichar_t c;
   const char *iter;

   for (iter = str;
        iter && *iter && (c = bson_utf8_get_char(iter));
        iter = bson_utf8_next_char(iter))
   {
      if (c == stop) {
         *end = iter;
         return bson_strndup(str, iter - str);
      } else if (c == '\\') {
         iter = bson_utf8_next_char(iter);
         if (!bson_utf8_get_char(iter)) {
            break;
         }
      }
   }

   return NULL;
}
Esempio n. 3
0
static void
test_bson_utf8_get_char (void)
{
   static const char *test1 = "asdf";
   static const char test2[] = {0xe2, 0x82, 0xac, ' ', 0xe2, 0x82, 0xac, ' ', 0xe2, 0x82, 0xac, 0};
   const char *c;

   c = test1;
   assert(bson_utf8_get_char(c) == 'a');
   c = bson_utf8_next_char(c);
   assert(bson_utf8_get_char(c) == 's');
   c = bson_utf8_next_char(c);
   assert(bson_utf8_get_char(c) == 'd');
   c = bson_utf8_next_char(c);
   assert(bson_utf8_get_char(c) == 'f');
   c = bson_utf8_next_char(c);
   assert(!*c);

   c = test2;
   assert(bson_utf8_get_char(c) == 0x20AC);
   c = bson_utf8_next_char(c);
   assert(c == test2 + 3);
   assert(bson_utf8_get_char(c) == ' ');
   c = bson_utf8_next_char(c);
   assert(bson_utf8_get_char(c) == 0x20AC);
   c = bson_utf8_next_char(c);
   assert(bson_utf8_get_char(c) == ' ');
   c = bson_utf8_next_char(c);
   assert(bson_utf8_get_char(c) == 0x20AC);
   c = bson_utf8_next_char(c);
   assert(!*c);
}
Esempio n. 4
0
char *
mongoc_uri_unescape (const char *escaped_string)
{
   bson_unichar_t c;
   bson_string_t *str;
   unsigned int hex = 0;
   const char *ptr;
   const char *end;
   size_t len;

   BSON_ASSERT (escaped_string);

   len = strlen(escaped_string);

   /*
    * Double check that this is a UTF-8 valid string. Bail out if necessary.
    */
   if (!bson_utf8_validate(escaped_string, len, false)) {
      MONGOC_WARNING("%s(): escaped_string contains invalid UTF-8",
                     BSON_FUNC);
      return NULL;
   }

   ptr = escaped_string;
   end = ptr + len;
   str = bson_string_new(NULL);

   for (; *ptr; ptr = bson_utf8_next_char(ptr)) {
      c = bson_utf8_get_char(ptr);
      switch (c) {
      case '%':
         if (((end - ptr) < 2) ||
             !isxdigit(ptr[1]) ||
             !isxdigit(ptr[2]) ||
#ifdef _MSC_VER
             (1 != sscanf_s(&ptr[1], "%02x", &hex)) ||
#else
             (1 != sscanf(&ptr[1], "%02x", &hex)) ||
#endif
             !isprint(hex)) {
            bson_string_free(str, true);
            return NULL;
         }
         bson_string_append_c(str, hex);
         ptr += 2;
         break;
      default:
         bson_string_append_unichar(str, c);
         break;
      }
   }

   return bson_string_free(str, false);
}
Esempio n. 5
0
static bool
_bson_json_all_whitespace (const char *utf8)
{
   bool all_whitespace = true;

   for (; *utf8; utf8 = bson_utf8_next_char (utf8)) {
      if (!isspace (bson_utf8_get_char (utf8))) {
         all_whitespace = false;
         break;
      }
   }

   return all_whitespace;
}
Esempio n. 6
0
void
mongoc_uri_lowercase_hostname (const char *src,
                               char *buf /* OUT */,
                               int len)
{
   bson_unichar_t c;
   const char *iter;
   char *buf_iter;

   /* TODO: this code only accepts ascii, and assumes that lowercased
      chars are the same width as originals */
   for (iter = src, buf_iter = buf;
        iter && *iter && (c = bson_utf8_get_char(iter)) && buf_iter - buf < len;
        iter = bson_utf8_next_char(iter), buf_iter++) {
      assert(c < 128);
      *buf_iter = tolower(c);
   }
}
Esempio n. 7
0
int monary_load_length_value(const bson_iter_t* bsonit,
                             monary_column_item* citem,
                             int idx)
{
    bson_type_t type;
    bson_iter_t child;
    const char* discard;
    uint32_t length;
    uint32_t* dest;

    type = bson_iter_type(bsonit);
    switch (type) {
        case BSON_TYPE_UTF8:
        case BSON_TYPE_CODE:
            discard = bson_iter_utf8(bsonit, &length);
            for (length = 0; *discard; length++) {
                discard = bson_utf8_next_char(discard);
            }
            break;
        case BSON_TYPE_ARRAY:
        case BSON_TYPE_DOCUMENT:
            if (!bson_iter_recurse(bsonit, &child)) {
                return 0;
            }
            for (length = 0; bson_iter_next(&child); length++);
            break;
        case BSON_TYPE_BINARY:
            bson_iter_binary(bsonit, NULL, &length, (const uint8_t**) &discard);
            break;
        default:
            return 0;
    }

    dest = ((uint32_t*) citem->storage) + idx;
    memcpy(dest, &length, sizeof(uint32_t));
    return 1;
}