* Initializes a pgbson structure from a hexadecimal String. */
| 132 | * Initializes a pgbson structure from a hexadecimal String. |
| 133 | */ |
| 134 | pgbson * |
| 135 | PgbsonInitFromHexadecimalString(const char *hexadecimalString) |
| 136 | { |
| 137 | uint32_t strLength = strlen(hexadecimalString); |
| 138 | |
| 139 | uint32_t hexStringLength = (strLength - BsonHexPrefixLength); |
| 140 | if (hexStringLength <= 0 || (hexStringLength % 2 != 0)) |
| 141 | { |
| 142 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), errmsg( |
| 143 | "Invalid Hex string for pgbson input"))); |
| 144 | } |
| 145 | |
| 146 | if (strncmp(hexadecimalString, BsonHexPrefix, BsonHexPrefixLength) != 0) |
| 147 | { |
| 148 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE), |
| 149 | errmsg("Bson Hex string does not have valid prefix %s", |
| 150 | BsonHexPrefix))); |
| 151 | } |
| 152 | |
| 153 | uint32_t binaryLength = hexStringLength / 2; |
| 154 | |
| 155 | int allocSize = binaryLength + VARHDRSZ; |
| 156 | pgbson *pgbsonVal = (pgbson *) palloc(allocSize); |
| 157 | |
| 158 | uint64 actualBinarySize = hex_decode(&hexadecimalString[BsonHexPrefixLength], |
| 159 | hexStringLength, VARDATA(pgbsonVal)); |
| 160 | Assert(actualBinarySize == binaryLength); |
| 161 | SET_VARSIZE(pgbsonVal, (uint32_t) actualBinarySize + VARHDRSZ); |
| 162 | return pgbsonVal; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* |
no outgoing calls
no test coverage detected