1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci * you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci * You may obtain a copy of the License at
6049e185fSopenharmony_ci *
7049e185fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci *
9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci * See the License for the specific language governing permissions and
13049e185fSopenharmony_ci * limitations under the License.
14049e185fSopenharmony_ci */
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#ifndef URI_HELPER_H
17049e185fSopenharmony_ci#define URI_HELPER_H
18049e185fSopenharmony_ci
19049e185fSopenharmony_ci#include <cstdint>
20049e185fSopenharmony_ci#include <string>
21049e185fSopenharmony_ci#include <string_view>
22049e185fSopenharmony_ci#include <map>
23049e185fSopenharmony_ci#include <unistd.h>
24049e185fSopenharmony_ci#include <fcntl.h>
25049e185fSopenharmony_ci#include "nocopyable.h"
26049e185fSopenharmony_ci
27049e185fSopenharmony_cinamespace OHOS {
28049e185fSopenharmony_cinamespace Media {
29049e185fSopenharmony_ci/**
30049e185fSopenharmony_ci * The simple utility is designed to facilitate the uri processing.
31049e185fSopenharmony_ci */
32049e185fSopenharmony_ciclass __attribute__((visibility("default"))) UriHelper : public NoCopyable {
33049e185fSopenharmony_cipublic:
34049e185fSopenharmony_ci    enum UriType : uint8_t {
35049e185fSopenharmony_ci        URI_TYPE_FILE,
36049e185fSopenharmony_ci        URI_TYPE_FD,
37049e185fSopenharmony_ci        URI_TYPE_HTTP,
38049e185fSopenharmony_ci        URI_TYPE_UNKNOWN,
39049e185fSopenharmony_ci    };
40049e185fSopenharmony_ci
41049e185fSopenharmony_ci    enum UriAccessMode : uint8_t {
42049e185fSopenharmony_ci        URI_READ = 1 << 0,
43049e185fSopenharmony_ci        URI_WRITE = 1 << 1,
44049e185fSopenharmony_ci    };
45049e185fSopenharmony_ci
46049e185fSopenharmony_ci    explicit UriHelper(const std::string_view &uri);
47049e185fSopenharmony_ci    UriHelper(int32_t fd, int64_t offset, int64_t size);
48049e185fSopenharmony_ci    ~UriHelper();
49049e185fSopenharmony_ci
50049e185fSopenharmony_ci    uint8_t UriType() const;
51049e185fSopenharmony_ci    std::string FormattedUri() const;
52049e185fSopenharmony_ci    bool AccessCheck(uint8_t flag) const;
53049e185fSopenharmony_ci
54049e185fSopenharmony_ciprivate:
55049e185fSopenharmony_ci    void FormatMeForUri(const std::string_view &uri) noexcept;
56049e185fSopenharmony_ci    void FormatMeForFd() noexcept;
57049e185fSopenharmony_ci    bool ParseFdUri(std::string_view uri);
58049e185fSopenharmony_ci    bool CorrectFdParam();
59049e185fSopenharmony_ci
60049e185fSopenharmony_ci    std::string formattedUri_ = "";
61049e185fSopenharmony_ci    std::string_view rawFileUri_ = "";
62049e185fSopenharmony_ci    uint8_t type_ = 0;
63049e185fSopenharmony_ci    int32_t fd_ = -1;
64049e185fSopenharmony_ci    int64_t offset_ = 0;
65049e185fSopenharmony_ci    int64_t size_ = 0;
66049e185fSopenharmony_ci};
67049e185fSopenharmony_ci} // namespace Media
68049e185fSopenharmony_ci} // namespace OHOS
69049e185fSopenharmony_ci
70049e185fSopenharmony_ci#endif