summaryrefslogtreecommitdiff
path: root/core/fxcrt/fx_guid.cpp
blob: 4d3a4e9a5f38a791f06999f3322c1649bfac02d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxcrt/fx_guid.h"

#include "core/fxcrt/fx_extension.h"
#include "core/fxcrt/fx_random.h"

void FX_GUID_CreateV4(FX_GUID* pGUID) {
  FX_Random_GenerateMT((uint32_t*)pGUID, 4);
  uint8_t& b = ((uint8_t*)pGUID)[6];
  b = (b & 0x0F) | 0x40;
}

CFX_ByteString FX_GUID_ToString(const FX_GUID* pGUID, bool bSeparator) {
  CFX_ByteString bsStr;
  char* pBuf = bsStr.GetBuffer(40);
  for (int32_t i = 0; i < 16; i++) {
    uint8_t b = reinterpret_cast<const uint8_t*>(pGUID)[i];
    FXSYS_IntToTwoHexChars(b, pBuf);
    pBuf += 2;
    if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9))
      *pBuf++ = L'-';
  }
  bsStr.ReleaseBuffer(bSeparator ? 36 : 32);
  return bsStr;
}