static void test_bson_iter_utf8 (void) { uint32_t len = 0; bson_iter_t iter; bson_t *b; char *s; b = bson_new(); assert(bson_append_utf8(b, "foo", -1, "bar", -1)); assert(bson_append_utf8(b, "bar", -1, "baz", -1)); assert(bson_iter_init(&iter, b)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_UTF8(&iter)); assert(!strcmp(bson_iter_key(&iter), "foo")); assert(!strcmp(bson_iter_utf8(&iter, NULL), "bar")); s = bson_iter_dup_utf8(&iter, &len); assert_cmpstr("bar", s); assert_cmpint(len, ==, 3); bson_free(s); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_UTF8(&iter)); assert(!strcmp(bson_iter_key(&iter), "bar")); assert(!strcmp(bson_iter_utf8(&iter, NULL), "baz")); assert(!bson_iter_next(&iter)); bson_destroy(b); }
int get_column_map (bson_t *bson_schema, const char *table_name, column_map_t **column_map, int *column_map_size) { bson_iter_t iter_col, iter_dup; int size; column_map_t *column_map_p; const char *data_type; data_type_map_t *data_type_map_p; bson_find_create_table (bson_schema, table_name, &iter_col) || DIE; for (iter_dup = iter_col, size = 0; bson_iter_next (&iter_dup); size++) ; *column_map = calloc (sizeof (column_map_t), size); *column_map_size = size; column_map_p = *column_map; while (bson_iter_next (&iter_col)) { bson_iter_t iter_col_prop; BSON_ITER_HOLDS_DOCUMENT (&iter_col) || DIE; bson_iter_recurse (&iter_col, &iter_col_prop) || DIE; bson_iter_find (&iter_col_prop, "column_name") || DIE; BSON_ITER_HOLDS_UTF8 (&iter_col_prop) || DIE; column_map_p->column_name = bson_iter_dup_utf8 (&iter_col_prop, NULL); bson_iter_find (&iter_col_prop, "data_type") || DIE; BSON_ITER_HOLDS_UTF8 (&iter_col_prop) || DIE; data_type = bson_iter_utf8 (&iter_col_prop, NULL); column_map_p->data_type = data_type; for (data_type_map_p = data_type_map; data_type_map_p < (data_type_map + sizeof (data_type_map)) && strcmp (data_type_map_p->data_type, data_type) != 0; data_type_map_p++) ; if (data_type_map < (data_type_map + sizeof (data_type_map))) column_map_p->bson_append_from_s = data_type_map_p->bson_append_from_s ? data_type_map_p->bson_append_from_s : bson_append_utf8_from_s; else DIE; column_map_p++; } return true; }
int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, const char *topic, int acc) { struct mongo_backend *handle = (struct mongo_backend *) conf; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; bson_iter_t iter; bool check = false; int match = 0, foundFlag = 0; bson_t query; bson_init(&query); bson_append_utf8(&query, "username", -1, username, -1); collection = mongoc_client_get_collection(handle->client, dbName, colName); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL); while (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_more (cursor)) { if (foundFlag == 0 && mongoc_cursor_next (cursor, &doc)) { bson_iter_init(&iter, doc); bson_iter_find(&iter, topicLoc); int64_t topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); bson_destroy(&query); mongoc_cursor_destroy(cursor); mongoc_collection_destroy(collection); bson_init(&query); bson_append_int64(&query, topicID, -1, topId); collection = mongoc_client_get_collection(handle->client, dbName, topicLoc); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL); foundFlag = 1; } if (foundFlag == 1 && mongoc_cursor_next(cursor, &doc)) { bson_iter_init(&iter, doc); bson_iter_find(&iter, topicLoc); uint32_t len; const uint8_t *arr; bson_iter_array(&iter, &len, &arr); bson_t b; if (bson_init_static(&b, arr, len)) { bson_iter_init(&iter, &b); while (bson_iter_next(&iter)) { char *str = bson_iter_dup_utf8(&iter, &len); mosquitto_topic_matches_sub(str, topic, &check); if (check) { match = 1; bson_free(str); break; } bson_free(str); } } } } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); return 0; } bson_destroy(&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy(collection); return match; }