File indexing completed on 2024-11-17 04:55:18

0001 /*
0002     SPDX-License-Identifier: MPL-2.0
0003 */
0004 
0005 /* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
0006  * This Source Code Form is subject to the terms of the Mozilla Public
0007  * License, v. 2.0. If a copy of the MPL was not distributed with this
0008  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
0009 
0010 #include "./no_fingerprint_domain.h"
0011 
0012 #include <stdio.h>
0013 #include <string.h>
0014 
0015 #include "hashFn.h"
0016 
0017 static HashFn h(19);
0018 
0019 NoFingerprintDomain::NoFingerprintDomain() :
0020     borrowed_data(false),
0021     data(nullptr),
0022     dataLen(-1) {
0023 }
0024 
0025 NoFingerprintDomain::NoFingerprintDomain(const NoFingerprintDomain &other) {
0026   borrowed_data = other.borrowed_data;
0027   dataLen = other.dataLen;
0028   if (other.dataLen == -1 && other.data) {
0029     dataLen = static_cast<int>(strlen(other.data));
0030   }
0031 
0032   if (other.borrowed_data) {
0033     data = other.data;
0034   } else {
0035     if (other.data) {
0036       data = new char[dataLen];
0037       memcpy(data, other.data, dataLen);
0038     } else {
0039       data = nullptr;
0040     }
0041   }
0042 }
0043 
0044 NoFingerprintDomain::NoFingerprintDomain(const char * data, int dataLen) :
0045     borrowed_data(true),  data(const_cast<char*>(data)),
0046     dataLen(dataLen) {
0047 }
0048 
0049 NoFingerprintDomain::~NoFingerprintDomain() {
0050   if (borrowed_data) {
0051     return;
0052   }
0053   if (data) {
0054     delete[] data;
0055   }
0056 }
0057 
0058 uint64_t NoFingerprintDomain::hash() const {
0059   if (!data) {
0060     return 0;
0061   }
0062   return h(data, dataLen);
0063 }
0064 
0065 uint32_t NoFingerprintDomain::Serialize(char *buffer) {
0066   uint32_t totalSize = 0;
0067   char sz[64];
0068   uint32_t dataLenSize = 1 + snprintf(sz, sizeof(sz),
0069       "%xx", dataLen);
0070   if (buffer) {
0071     memcpy(buffer + totalSize, sz, dataLenSize);
0072   }
0073   totalSize += dataLenSize;
0074   if (buffer) {
0075     memcpy(buffer + totalSize, data, dataLen);
0076   }
0077   totalSize += dataLen;
0078 
0079   totalSize += 1;
0080 
0081   return totalSize;
0082 }
0083 
0084 uint32_t NoFingerprintDomain::Deserialize(char *buffer, uint32_t bufferSize) {
0085   dataLen = 0;
0086   sscanf(buffer, "%x", (unsigned int*)&dataLen);
0087   uint32_t consumed = static_cast<uint32_t>(strlen(buffer)) + 1;
0088   if (consumed + dataLen >= bufferSize) {
0089     return 0;
0090   }
0091   data = buffer + consumed;
0092   consumed += dataLen;
0093   borrowed_data = true;
0094   // Since we serialize a \0 character, we need to skip past it.
0095   consumed++;
0096   return consumed;
0097 }
0098 
0099 bool NoFingerprintDomain::operator==(const NoFingerprintDomain &rhs) const {
0100   if (dataLen != rhs.dataLen) {
0101     return false;
0102   }
0103   if (dataLen == 0) {
0104     return true;
0105   }
0106   return !memcmp(data, rhs.data, dataLen);
0107 }