1 /******************************************************************************
2 * LibGHT, software to manage point clouds.
3 * LibGHT is free and open source software provided by the Government of Canada
4 * Copyright (c) 2012 Natural Resources Canada
5 *
6 * Nouri Sabo <nsabo@NRCan.gc.ca>, Natural Resources Canada
7 * Paul Ramsey <pramsey@opengeo.org>, OpenGeo
8 *
9 ******************************************************************************/
10
11 #include "ght_internal.h"
12
13 int fexists(const char *filename); /* ght_util.c */
14 char machine_endian(void); /* ght_util.c */
15
16 /* Our static character->number map. Anything > 15 is invalid */
17 static uint8_t hex2char[256] = {
18 /* not Hex characters */
19 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
21 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
22 /* 0-9 */
23 0,1,2,3,4,5,6,7,8,9,20,20,20,20,20,20,
24 /* A-F */
25 20,10,11,12,13,14,15,20,20,20,20,20,20,20,20,20,
26 /* not Hex characters */
27 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
28 /* a-f */
29 20,10,11,12,13,14,15,20,20,20,20,20,20,20,20,20,
30 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
31 /* not Hex characters (upper 128 characters) */
32 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
33 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
34 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
35 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
36 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
37 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
38 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
39 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20
40 };
41
42
43 GhtErr
bytes_from_hexbytes(const char * hexbuf,size_t hexsize,uint8_t ** bytes)44 bytes_from_hexbytes(const char *hexbuf, size_t hexsize, uint8_t **bytes)
45 {
46 uint8_t *buf = NULL;
47 register uint8_t h1, h2;
48 int i;
49
50 if( hexsize % 2 )
51 {
52 ght_error("Invalid hex string, length (%d) has to be a multiple of two!", hexsize);
53 return GHT_ERROR;
54 }
55
56 buf = ght_malloc(hexsize/2);
57
58 if( ! buf )
59 {
60 ght_error("Unable to allocate memory buffer.");
61 return GHT_ERROR;
62 }
63
64 for( i = 0; i < hexsize/2; i++ )
65 {
66 h1 = hex2char[(int)hexbuf[2*i]];
67 h2 = hex2char[(int)hexbuf[2*i+1]];
68 if( h1 > 15 )
69 {
70 ght_error("Invalid hex character (%c) encountered", hexbuf[2*i]);
71 return GHT_ERROR;
72 }
73 if( h2 > 15 )
74 {
75 ght_error("Invalid hex character (%c) encountered", hexbuf[2*i+1]);
76 return GHT_ERROR;
77 }
78 /* First character is high bits, second is low bits */
79 buf[i] = ((h1 & 0x0F) << 4) | (h2 & 0x0F);
80 }
81 *bytes = buf;
82 return GHT_OK;
83 }
84
85 GhtErr
hexbytes_from_bytes(const uint8_t * bytebuf,size_t bytesize,char ** hexbytes)86 hexbytes_from_bytes(const uint8_t *bytebuf, size_t bytesize, char **hexbytes)
87 {
88 char *buf = ght_malloc(2*bytesize + 1); /* 2 chars per byte + null terminator */
89 int i;
90 char *ptr = buf;
91
92 for ( i = 0; i < bytesize; i++ )
93 {
94 int incr = snprintf(ptr, 3, "%02X", bytebuf[i]);
95 if ( incr < 0 )
96 {
97 ght_error("write failure in hexbytes_from_bytes");
98 return GHT_ERROR;
99 }
100 ptr += incr;
101 }
102
103 *hexbytes = buf;
104 return GHT_OK;
105 }
106
107 int
fexists(const char * filename)108 fexists(const char *filename)
109 {
110 FILE *file;
111 if ( file = fopen(filename, "r") )
112 {
113 fclose(file);
114 return 1;
115 }
116 return 0;
117 }
118
119 char
machine_endian(void)120 machine_endian(void)
121 {
122 static int check_int = 1; /* dont modify this!!! */
123 return *((char *) &check_int); /* 0 = big endian | xdr, */
124 /* 1 = little endian | ndr */
125 }
126
127 int
ght_version_major(void)128 ght_version_major(void)
129 {
130 return GHT_VERSION_MAJOR;
131 }
132
133 int
ght_version_minor(void)134 ght_version_minor(void)
135 {
136 return GHT_VERSION_MINOR;
137 }
138
139 int
ght_version_patch(void)140 ght_version_patch(void)
141 {
142 return GHT_VERSION_PATCH;
143 }
144
145 char *
ght_version(void)146 ght_version(void)
147 {
148 char buf[32];
149 snprintf(buf, 32, "%d.%d.%d", GHT_VERSION_MAJOR, GHT_VERSION_MINOR, GHT_VERSION_PATCH);
150 return ght_strdup(buf);
151 }
152
153 GhtErr
ght_config_init(GhtConfig * config)154 ght_config_init(GhtConfig *config)
155 {
156 // typedef struct
157 // {
158 // unsigned char allow_duplicates;
159 // unsigned char max_hash_length;
160 // unsigned char version;
161 // unsigned char endian;
162 // } GhtConfig;
163 memset(config, 0, sizeof(GhtConfig));
164 config->allow_duplicates = GHT_DUPES_YES;
165 config->max_hash_length = GHT_MAX_HASH_LENGTH;
166 config->version = GHT_FORMAT_VERSION;
167 config->endian = machine_endian();
168 return GHT_OK;
169 }
170
171
172