Flutter Engine
fl_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/linux/testing/fl_test.h"
6 
7 static uint8_t hex_digit_to_int(char value) {
8  if (value >= '0' && value <= '9')
9  return value - '0';
10  else if (value >= 'a' && value <= 'f')
11  return value - 'a' + 10;
12  else if (value >= 'F' && value <= 'F')
13  return value - 'A' + 10;
14  else
15  return 0;
16 }
17 
18 static uint8_t parse_hex8(const gchar* hex_string) {
19  if (hex_string[0] == '\0')
20  return 0x00;
21  return hex_digit_to_int(hex_string[0]) << 4 | hex_digit_to_int(hex_string[1]);
22 }
23 
24 GBytes* hex_string_to_bytes(const gchar* hex_string) {
25  GByteArray* buffer = g_byte_array_new();
26  for (int i = 0; hex_string[i] != '\0' && hex_string[i + 1] != '\0'; i += 2) {
27  uint8_t value = parse_hex8(hex_string + i);
28  g_byte_array_append(buffer, &value, 1);
29  }
30  return g_byte_array_free_to_bytes(buffer);
31 }
32 
33 gchar* bytes_to_hex_string(GBytes* bytes) {
34  GString* hex_string = g_string_new("");
35  size_t data_length;
36  const uint8_t* data =
37  static_cast<const uint8_t*>(g_bytes_get_data(bytes, &data_length));
38  for (size_t i = 0; i < data_length; i++)
39  g_string_append_printf(hex_string, "%02x", data[i]);
40  return g_string_free(hex_string, FALSE);
41 }
gchar * bytes_to_hex_string(GBytes *bytes)
Definition: fl_test.cc:33
uint8_t value
static uint8_t parse_hex8(const gchar *hex_string)
Definition: fl_test.cc:18
GBytes * hex_string_to_bytes(const gchar *hex_string)
Definition: fl_test.cc:24
static uint8_t hex_digit_to_int(char value)
Definition: fl_test.cc:7