BRL-CAD
db5_bin.c
Go to the documentation of this file.
1 /* D B 5 _ B I N . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2000-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup db5 */
21 /** @{ */
22 /** @file db5_bin.c
23  *
24  * Handle bulk binary objects
25  *
26  */
27 /** @} */
28 
29 #include "common.h"
30 
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34 #include <ctype.h>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h> /* for ntohl/htonl */
37 #endif
38 #include "bio.h"
39 
40 
41 #include "bu/cv.h"
42 #include "bu/parse.h"
43 #include "vmath.h"
44 #include "db5.h"
45 #include "rtgeom.h"
46 #include "raytrace.h"
47 #include "nurb.h"
48 
49 
50 /* size of each element (in bytes) for the different BINUNIF types */
51 /* this array depends on the values of the definitions of the DB5_MINORTYPE_BINU_... in db5.h */
52 static const int binu_sizes[]={
53  0,
54  0,
57  1,
58  2,
59  4,
60  8,
61  0,
62  0,
63  0,
64  0,
65  1,
66  2,
67  4,
68  8
69 };
70 
71 
72 /**
73  * XXX these are the interface routines needed for table.c
74  */
75 int
77  const struct rt_db_internal *UNUSED(ip),
78  double UNUSED(local2mm),
79  const struct db_i *UNUSED(dbip),
80  struct resource *UNUSED(resp))
81 {
82  bu_log("rt_bin_unif_export5() not implemented\n");
83  return -1;
84 }
85 
86 int
88  const struct bu_external *UNUSED(ep),
89  const mat_t UNUSED(mat),
90  const struct db_i *UNUSED(dbip),
91  struct resource *UNUSED(resp))
92 {
93  bu_log("rt_bin_unif_import5() not implemented\n");
94  return -1;
95 }
96 
97 int
99  const struct bu_external *UNUSED(ep),
100  const mat_t UNUSED(mat),
101  const struct db_i *UNUSED(dbip),
102  struct resource *UNUSED(resp))
103 {
104  bu_log("rt_bin_mime_import5() not implemented\n");
105  return -1;
106 }
107 
108 /**
109  * Import a uniform-array binary object from the database format to
110  * the internal structure.
111  */
112 int
114  const struct bu_external *ep,
115  const mat_t UNUSED(mat),
116  const struct db_i *dbip,
117  struct resource *resp,
118  int minor_type)
119 {
120  struct rt_binunif_internal *bip;
121  size_t i;
122  unsigned char *srcp;
123  unsigned long *ldestp;
124  int in_cookie, out_cookie;
125  size_t gotten;
126 
127  BU_CK_EXTERNAL(ep);
128  if (dbip) RT_CK_DBI(dbip);
129  if (resp) RT_CK_RESOURCE(resp);
130 
131  /*
132  * There's no particular size to expect
133  *
134  * BU_ASSERT_LONG(ep->ext_nbytes, ==, SIZEOF_NETWORK_DOUBLE * 3*4);
135  */
136 
137  RT_CK_DB_INTERNAL(ip);
138  ip->idb_major_type = DB5_MAJORTYPE_BINARY_UNIF;
139  ip->idb_minor_type = minor_type;
140  ip->idb_meth = &OBJ[ID_BINUNIF];
141  BU_ALLOC(ip->idb_ptr, struct rt_binunif_internal);
142 
143  bip = (struct rt_binunif_internal *)ip->idb_ptr;
145  bip->type = minor_type;
146 
147  /*
148  * Convert from database (network) to internal (host) format
149  */
150  switch (bip->type) {
151  case DB5_MINORTYPE_BINU_FLOAT:
153  bip->u.uint8 = (unsigned char *) bu_malloc(bip->count * sizeof(float),
154  "rt_binunif_internal");
155  bu_cv_ntohf((unsigned char *) bip->u.uint8,
156  ep->ext_buf, bip->count);
157  break;
158  case DB5_MINORTYPE_BINU_DOUBLE:
160  bip->u.uint8 = (unsigned char *) bu_malloc(bip->count * sizeof(double),
161  "rt_binunif_internal");
162  bu_cv_ntohd((unsigned char *) bip->u.uint8, ep->ext_buf, bip->count);
163  break;
164  case DB5_MINORTYPE_BINU_8BITINT:
165  case DB5_MINORTYPE_BINU_8BITINT_U:
166  bip->count = ep->ext_nbytes;
167  bip->u.uint8 = (unsigned char *) bu_malloc(ep->ext_nbytes,
168  "rt_binunif_internal");
169  memcpy((char *) bip->u.uint8, (char *) ep->ext_buf, ep->ext_nbytes);
170  break;
171  case DB5_MINORTYPE_BINU_16BITINT:
172  case DB5_MINORTYPE_BINU_16BITINT_U:
173  bip->count = ep->ext_nbytes/2;
174  bip->u.uint8 = (unsigned char *) bu_malloc(ep->ext_nbytes,
175  "rt_binunif_internal");
176  in_cookie = bu_cv_cookie("nus");
177  out_cookie = bu_cv_cookie("hus");
178  if (bu_cv_optimize(in_cookie) != bu_cv_optimize(out_cookie)) {
179  gotten =
180  bu_cv_w_cookie((void *)bip->u.uint8, out_cookie,
181  ep->ext_nbytes,
182  ep->ext_buf, in_cookie, bip->count);
183  if (gotten != bip->count) {
184  bu_log("%s:%d: Tried to convert %zu, did %zu",
185  __FILE__, __LINE__, bip->count, gotten);
186  bu_bomb("\n");
187  }
188  } else
189  memcpy((char *) bip->u.uint8,
190  (char *) ep->ext_buf,
191  ep->ext_nbytes);
192  break;
193  case DB5_MINORTYPE_BINU_32BITINT:
194  case DB5_MINORTYPE_BINU_32BITINT_U:
195  bip->count = ep->ext_nbytes/4;
196  bip->u.uint8 = (unsigned char *) bu_malloc(ep->ext_nbytes,
197  "rt_binunif_internal");
198  srcp = (unsigned char *) ep->ext_buf;
199  ldestp = (unsigned long *) bip->u.uint8;
200  for (i = 0; i < bip->count; ++i, ++ldestp, srcp += 4) {
201  *ldestp = ntohl(*(uint32_t *)&srcp[0]);
202  }
203  break;
204  case DB5_MINORTYPE_BINU_64BITINT:
205  case DB5_MINORTYPE_BINU_64BITINT_U:
206  bu_log("rt_binunif_import5_minor_type() Can't handle 64-bit integers yet\n");
207  return -1;
208  }
209 
210  return 0; /* OK */
211 }
212 
213 
214 void
216  RT_CK_BINUNIF(bip);
217  bu_log("rt_bin_unif_internal <%p>...\n", (void *)bip);
218  bu_log(" type = x%x = %d", bip->type, bip->type);
219  bu_log(" count = %ld first = 0x%02x", bip->count,
220  bip->u.uint8[0] & 0x0ff);
221  bu_log("- - - - -\n");
222 }
223 
224 
225 /**
226  * Create the "body" portion of external form
227  */
228 int
230  const struct rt_db_internal *ip,
231  double UNUSED(local2mm), /* we ignore */
232  const struct db_i *dbip,
233  struct resource *resp)
234 {
235  struct rt_binunif_internal *bip;
236  size_t i;
237  unsigned char *destp;
238  unsigned long *lsrcp;
239  int in_cookie, out_cookie;
240  size_t gotten;
241 
242  if (dbip) RT_CK_DBI(dbip);
243  if (resp) RT_CK_RESOURCE(resp);
244 
245  RT_CK_DB_INTERNAL(ip);
246  bip = (struct rt_binunif_internal *)ip->idb_ptr;
247  RT_CK_BINUNIF(bip);
248 
249  BU_EXTERNAL_INIT(ep);
250 
251  /*
252  * Convert from internal (host) to database (network) format
253  */
254  switch (bip->type) {
255  case DB5_MINORTYPE_BINU_FLOAT:
257  ep->ext_buf = (uint8_t *)bu_malloc(ep->ext_nbytes,
258  "binunif external");
259  bu_cv_htonf(ep->ext_buf, (unsigned char *) bip->u.uint8, bip->count);
260  break;
261  case DB5_MINORTYPE_BINU_DOUBLE:
263  ep->ext_buf = (uint8_t *)bu_malloc(ep->ext_nbytes,
264  "binunif external");
265  bu_cv_htond(ep->ext_buf, (unsigned char *)bip->u.uint8, bip->count);
266  break;
267  case DB5_MINORTYPE_BINU_8BITINT:
268  case DB5_MINORTYPE_BINU_8BITINT_U:
269  ep->ext_nbytes = bip->count;
270  ep->ext_buf = (uint8_t *)bu_malloc(ep->ext_nbytes,
271  "binunif external");
272  memcpy((char *)ep->ext_buf, (char *)bip->u.uint8, bip->count);
273  break;
274  case DB5_MINORTYPE_BINU_16BITINT:
275  case DB5_MINORTYPE_BINU_16BITINT_U:
276  ep->ext_nbytes = bip->count * 2;
277  ep->ext_buf = (uint8_t *)bu_malloc(ep->ext_nbytes, "binunif external");
278  in_cookie = bu_cv_cookie("hus");
279  out_cookie = bu_cv_cookie("nus");
280  if (bu_cv_optimize(in_cookie) != bu_cv_optimize(out_cookie)) {
281  gotten =
282  bu_cv_w_cookie(ep->ext_buf, out_cookie,
283  ep->ext_nbytes,
284  (void *) bip->u.uint8, in_cookie,
285  bip->count);
286 
287  if (gotten != bip->count) {
288  bu_log("%s:%d: Tried to convert %zu, did %zu",
289  __FILE__, __LINE__, bip->count, gotten);
290  bu_bomb("\n");
291  }
292  } else {
293  memcpy((char *) ep->ext_buf,
294  (char *) bip->u.uint8,
295  ep->ext_nbytes);
296  }
297  break;
298  case DB5_MINORTYPE_BINU_32BITINT:
299  case DB5_MINORTYPE_BINU_32BITINT_U:
300  ep->ext_nbytes = bip->count * 4;
301  ep->ext_buf = (uint8_t *)bu_malloc(ep->ext_nbytes, "binunif external");
302 
303  lsrcp = (unsigned long *) bip->u.uint8;
304  destp = (unsigned char *) ep->ext_buf;
305  for (i = 0; i < bip->count; ++i, ++destp, ++lsrcp) {
306  *(uint32_t *)&destp[0] = htonl(*lsrcp);
307  }
308  break;
309  case DB5_MINORTYPE_BINU_64BITINT:
310  case DB5_MINORTYPE_BINU_64BITINT_U:
311  bu_log("rt_binunif_export5() Can't handle 64-bit integers yet\n");
312  return -1;
313  }
314 
315  return 0;
316 }
317 
318 /**
319  * Make human-readable formatted presentation of this object. First
320  * line describes type of object. Additional lines are indented one
321  * tab, and give parameter values.
322  */
323 int
325  const struct rt_db_internal *ip,
326  int UNUSED(verbose),
327  double UNUSED(mm2local))
328 {
329  register struct rt_binunif_internal *bip;
330  char buf[256];
331  unsigned short wid;
332 
333  bip = (struct rt_binunif_internal *) ip->idb_ptr;
334  RT_CK_BINUNIF(bip);
335  bu_vls_strcat(str, "uniform-array binary object (BINUNIF)\n");
336  wid = (bip->type & DB5_MINORTYPE_BINU_WID_MASK) >> 4;
337  switch (wid) {
338  case 0:
339  sprintf(buf, "%llu ", (unsigned long long)bip->count); break;
340  case 1:
341  sprintf(buf, "%llu pairs of ", (unsigned long long)(bip->count / 2)); break;
342  case 2:
343  sprintf(buf, "%llu triples of ", (unsigned long long)(bip->count / 3)); break;
344  case 3:
345  sprintf(buf, "%llu quadruples of ", (unsigned long long)(bip->count / 4)); break;
346  }
347  bu_vls_strcat(str, buf);
348  switch (bip->type & DB5_MINORTYPE_BINU_ATM_MASK) {
349  case DB5_MINORTYPE_BINU_FLOAT:
350  bu_vls_strcat(str, "floats\n"); break;
351  case DB5_MINORTYPE_BINU_DOUBLE:
352  bu_vls_strcat(str, "doubles\n"); break;
353  case DB5_MINORTYPE_BINU_8BITINT:
354  bu_vls_strcat(str, "8-bit ints\n"); break;
355  case DB5_MINORTYPE_BINU_16BITINT:
356  bu_vls_strcat(str, "16-bit ints\n"); break;
357  case DB5_MINORTYPE_BINU_32BITINT:
358  bu_vls_strcat(str, "32-bit ints\n"); break;
359  case DB5_MINORTYPE_BINU_64BITINT:
360  bu_vls_strcat(str, "64-bit ints\n"); break;
361  case DB5_MINORTYPE_BINU_8BITINT_U:
362  bu_vls_strcat(str, "unsigned 8-bit ints\n"); break;
363  case DB5_MINORTYPE_BINU_16BITINT_U:
364  bu_vls_strcat(str, "unsigned 16-bit ints\n"); break;
365  case DB5_MINORTYPE_BINU_32BITINT_U:
366  bu_vls_strcat(str, "unsigned 32-bit ints\n"); break;
367  case DB5_MINORTYPE_BINU_64BITINT_U:
368  bu_vls_strcat(str, "unsigned 64-bit ints\n"); break;
369  default:
370  bu_log("%s:%d: This shouldn't happen", __FILE__, __LINE__);
371  return 1;
372  }
373 
374  return 0;
375 }
376 
377 void
379  RT_CK_BINUNIF(bip);
380  bu_free((void *) bip->u.uint8, "binunif free uint8");
381  bu_free(bip, "binunif free");
382  bip = NULL; /* sanity */
383 }
384 
385 
386 /**
387  * Free the storage associated with the rt_db_internal version of this
388  * thing.
389  */
390 void
392 {
393  struct rt_binunif_internal *bip;
394 
395  RT_CK_DB_INTERNAL(ip);
396  bip = (struct rt_binunif_internal *)ip->idb_ptr;
397  RT_CK_BINUNIF(bip);
398  bu_free((void *) bip->u.uint8, "binunif ifree");
399  bu_free(ip->idb_ptr, "binunif ifree");
400  ip->idb_ptr = ((void *)0);
401 }
402 
403 
404 int
406  struct db_i *dbip,
407  char *name)
408 {
409  register struct directory *dp;
410  struct rt_binunif_internal *bip;
411  struct bu_external ext;
412  struct db5_raw_internal raw;
413  char *tmp;
414 
415  /*
416  *Find the guy we're told to write
417  */
418  if ((dp = db_lookup(dbip, name, LOOKUP_NOISY)) == RT_DIR_NULL)
419  return -1;
420 
421  RT_DB_INTERNAL_INIT(intern);
422  if (rt_db_get_internal5(intern, dp, dbip, NULL, &rt_uniresource)
423  != ID_BINUNIF || db_get_external(&ext, dp, dbip) < 0)
424  return -1;
425 
426  if (db5_get_raw_internal_ptr(&raw, ext.ext_buf) == NULL) {
427  bu_log("%s:%d\n", __FILE__, __LINE__);
428  bu_free_external(&ext);
429  return -1;
430  }
431  if (db5_type_descrip_from_codes(&tmp, raw.major_type, raw.minor_type))
432  tmp = 0;
433 
434  if (RT_G_DEBUG & DEBUG_VOL)
435  bu_log("get_body() sees type (%d, %d)='%s'\n",
436  raw.major_type, raw.minor_type, tmp);
437 
438  if (raw.major_type != DB5_MAJORTYPE_BINARY_UNIF)
439  return -1;
440 
441  bip = (struct rt_binunif_internal *)intern->idb_ptr;
442  RT_CK_BINUNIF(bip);
443  if (RT_G_DEBUG & DEBUG_HF)
444  rt_binunif_dump(bip);
445 
446  if (RT_G_DEBUG & DEBUG_VOL)
447  bu_log("cmd_export_body() thinks bip->count=%zu\n",
448  bip->count);
449 
450  switch (bip->type) {
451  case DB5_MINORTYPE_BINU_FLOAT:
452  if (RT_G_DEBUG & DEBUG_VOL)
453  bu_log("bip->type switch... float");
454  break;
455  case DB5_MINORTYPE_BINU_DOUBLE:
456  if (RT_G_DEBUG & DEBUG_VOL)
457  bu_log("bip->type switch... double");
458  break;
459  case DB5_MINORTYPE_BINU_8BITINT:
460  if (RT_G_DEBUG & DEBUG_VOL)
461  bu_log("bip->type switch... 8bitint");
462  break;
463  case DB5_MINORTYPE_BINU_8BITINT_U:
464  if (RT_G_DEBUG & DEBUG_VOL)
465  bu_log("bip->type switch... 8bituint");
466  break;
467  case DB5_MINORTYPE_BINU_16BITINT:
468  if (RT_G_DEBUG & DEBUG_VOL)
469  bu_log("bip->type switch... 16bituint");
470  break;
471  case DB5_MINORTYPE_BINU_16BITINT_U:
472  if (RT_G_DEBUG & DEBUG_VOL)
473  bu_log("bip->type switch... 16bitint");
474  break;
475  case DB5_MINORTYPE_BINU_32BITINT:
476  case DB5_MINORTYPE_BINU_32BITINT_U:
477  if (RT_G_DEBUG & DEBUG_VOL)
478  bu_log("bip->type switch... 32bitint");
479  break;
480  case DB5_MINORTYPE_BINU_64BITINT:
481  case DB5_MINORTYPE_BINU_64BITINT_U:
482  if (RT_G_DEBUG & DEBUG_VOL)
483  bu_log("bip->type switch... 64bitint");
484  break;
485  default:
486  /* XXX This shouldn't happen!! */
487  bu_log("bip->type switch... default");
488  break;
489  }
490 
491  bu_free_external(&ext);
492 
493  return 0;
494 }
495 
496 void
497 rt_binunif_make(const struct rt_functab *ftp, struct rt_db_internal *intern)
498 {
499  struct rt_binunif_internal *bip;
500 
501  intern->idb_type = DB5_MINORTYPE_BINU_8BITINT;
502  intern->idb_major_type = DB5_MAJORTYPE_BINARY_UNIF;
503  BU_ASSERT(&OBJ[ID_BINUNIF] == ftp);
504 
505  intern->idb_meth = ftp;
506  BU_ALLOC(bip, struct rt_binunif_internal);
507 
508  intern->idb_ptr = (void *) bip;
510  bip->type = DB5_MINORTYPE_BINU_8BITINT;
511  bip->count = 0;
512  bip->u.int8 = NULL;
513 }
514 
515 int
516 rt_binunif_get(struct bu_vls *logstr, const struct rt_db_internal *intern, const char *attr)
517 {
518  register struct rt_binunif_internal *bip=(struct rt_binunif_internal *)intern->idb_ptr;
519  struct bu_external ext;
520  size_t i;
521  unsigned char *c;
522 
523  RT_CHECK_BINUNIF(bip);
524 
525  if (attr == (char *)NULL) {
526  /* export the object to get machine independent form */
527  if (rt_binunif_export5(&ext, intern, 1.0, NULL, NULL)) {
528  bu_vls_strcpy(logstr, "Failed to export binary object!!\n");
529  return BRLCAD_ERROR;
530  } else {
531  bu_vls_strcpy(logstr, "binunif");
532  bu_vls_printf(logstr, " T %d D {", bip->type);
533  c = ext.ext_buf;
534  for (i = 0; i < ext.ext_nbytes; i++, c++) {
535  if (i%40 == 0) bu_vls_strcat(logstr, "\n");
536  bu_vls_printf(logstr, "%2.2x", *c);
537  }
538  bu_vls_strcat(logstr, "}");
539  bu_free_external(&ext);
540  }
541 
542  } else {
543  if (BU_STR_EQUAL(attr, "T")) {
544  bu_vls_printf(logstr, "%d", bip->type);
545  } else if (BU_STR_EQUAL(attr, "D")) {
546  /* export the object to get machine independent form */
547  if (rt_binunif_export5(&ext, intern, 1.0, NULL, NULL)) {
548  bu_vls_strcpy(logstr, "Failed to export binary object!!\n");
549  return BRLCAD_ERROR;
550  } else {
551  c = ext.ext_buf;
552  for (i = 0; i < ext.ext_nbytes; i++, c++) {
553  if (i != 0 && i%40 == 0) bu_vls_strcat(logstr, "\n");
554  bu_vls_printf(logstr, "%2.2x", *c);
555  }
556  bu_free_external(&ext);
557  }
558  } else {
559  bu_vls_printf(logstr, "Binary object has no attribute '%s'", attr);
560  return BRLCAD_ERROR;
561  }
562  }
563 
564  return BRLCAD_OK;
565 }
566 
567 int
568 rt_binunif_adjust(struct bu_vls *logstr, struct rt_db_internal *intern, int argc, char **argv)
569 {
570  struct rt_binunif_internal *bip;
571  size_t i;
572 
573  RT_CK_DB_INTERNAL(intern);
574  bip = (struct rt_binunif_internal *)intern->idb_ptr;
575  RT_CHECK_BINUNIF(bip);
576 
577  while (argc >= 2) {
578  if (BU_STR_EQUAL(argv[0], "T")) {
579  int new_type=-1;
580  char *c;
581  int type_is_digit=1;
582 
583  c = argv[1];
584  while (*c != '\0') {
585  if (!isdigit((int)*c)) {
586  type_is_digit = 0;
587  break;
588  }
589  c++;
590  }
591 
592  if (type_is_digit) {
593  new_type = atoi(argv[1]);
594  } else {
595  if (argv[1][1] != '\0') {
596  bu_vls_printf(logstr,
597  "Illegal type: %s, must be 'f', 'd', 'c', 'i', 'l', 'C', 'S', 'I', or 'L'",
598  argv[1]);
599  return BRLCAD_ERROR;
600  }
601  switch (argv[1][0]) {
602  case 'f':
603  new_type = 2;
604  break;
605  case 'd':
606  new_type = 3;
607  break;
608  case 'c':
609  new_type = 4;
610  break;
611  case 's':
612  new_type = 5;
613  break;
614  case 'i':
615  new_type = 6;
616  break;
617  case 'l':
618  new_type = 7;
619  break;
620  case 'C':
621  new_type = 12;
622  break;
623  case 'S':
624  new_type = 13;
625  break;
626  case 'I':
627  new_type = 14;
628  break;
629  case 'L':
630  new_type = 15;
631  break;
632  }
633  }
634  if (new_type < 0 ||
635  new_type > DB5_MINORTYPE_BINU_64BITINT ||
636  binu_types[new_type] == NULL) {
637  /* Illegal value for type */
638  bu_vls_printf(logstr, "Illegal value for binary type: %s", argv[1]);
639  return BRLCAD_ERROR;
640  } else {
641  if (bip->u.uint8) {
642  size_t new_count;
643  size_t old_byte_count, new_byte_count;
644  size_t remainder_count;
645 
646  old_byte_count = bip->count * binu_sizes[bip->type];
647  new_count = old_byte_count / binu_sizes[new_type];
648  remainder_count = old_byte_count % binu_sizes[new_type];
649  if (remainder_count) {
650  new_count++;
651  new_byte_count = new_count * binu_sizes[new_type];
652  } else {
653  new_byte_count = old_byte_count;
654  }
655 
656  if (new_byte_count != old_byte_count) {
657  bip->u.uint8 = (unsigned char *)bu_realloc(bip->u.uint8,
658  new_byte_count,
659  "new bytes for binunif");
660  /* zero out the new bytes */
661  for (i = old_byte_count; i < new_byte_count; i++) {
662  bip->u.uint8[i] = 0;
663  }
664  }
665  bip->count = new_count;
666  }
667  bip->type = new_type;
668  intern->idb_type = new_type;
669  }
670  } else if (BU_STR_EQUAL(argv[0], "D")) {
671  Tcl_Obj *obj, *list, **obj_array;
672  int list_len;
673  unsigned char *buf, *d;
674  char *s;
675  int hexlen;
676  unsigned int h;
677 
678  obj = Tcl_NewStringObj(argv[1], -1);
679  list = Tcl_NewListObj(0, NULL);
680  Tcl_ListObjAppendList(brlcad_interp, list, obj);
681  (void)Tcl_ListObjGetElements(brlcad_interp, list, &list_len, &obj_array);
682 
683  hexlen = 0;
684  for (i = 0; i < (size_t)list_len; i++) {
685  hexlen += Tcl_GetCharLength(obj_array[i]);
686  }
687 
688  if (hexlen % 2) {
689  bu_vls_printf(logstr, "Hex form of binary data must have an even number of hex digits");
690  return BRLCAD_ERROR;
691  }
692 
693  buf = (unsigned char *)bu_malloc(hexlen / 2, "tcladjust binary data");
694  d = buf;
695  for (i = 0; i < (size_t)list_len; i++) {
696  s = Tcl_GetString(obj_array[i]);
697  while (*s) {
698  sscanf(s, "%2x", &h);
699  *d++ = h;
700  s += 2;
701  }
702  }
703  Tcl_DecrRefCount(list);
704 
705  if (bip->u.uint8) {
706  bu_free(bip->u.uint8, "binary data");
707  }
708  bip->u.uint8 = buf;
709  bip->count = hexlen / 2 / binu_sizes[bip->type];
710  }
711 
712  argc -= 2;
713  argv += 2;
714  }
715 
716  return TCL_OK;
717 }
718 
719 /*
720  * Local Variables:
721  * mode: C
722  * tab-width: 8
723  * indent-tabs-mode: t
724  * c-file-style: "stroustrup"
725  * End:
726  * ex: shiftwidth=4 tabstop=8
727  */
int rt_binunif_import5_minor_type(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip, struct resource *resp, int minor_type)
Definition: db5_bin.c:113
#define RT_BINUNIF_INTERNAL_MAGIC
Definition: magic.h:84
Definition: raytrace.h:800
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define SIZEOF_NETWORK_DOUBLE
Definition: cv.h:48
unsigned char * uint8
Definition: raytrace.h:1009
int rt_retrieve_binunif(struct rt_db_internal *intern, struct db_i *dbip, char *name)
Definition: db5_bin.c:405
int rt_binunif_get(struct bu_vls *logstr, const struct rt_db_internal *intern, const char *attr)
Definition: db5_bin.c:516
if lu s
Definition: nmg_mod.c:3860
Definition: clone.c:90
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
const unsigned char * db5_get_raw_internal_ptr(struct db5_raw_internal *rip, const unsigned char *ip)
Definition: db5_io.c:245
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
int db5_type_descrip_from_codes(char **descrip, const int major, const int minor)
Definition: db5_types.c:169
void rt_binunif_ifree(struct rt_db_internal *ip)
Definition: db5_bin.c:391
int rt_binunif_adjust(struct bu_vls *logstr, struct rt_db_internal *intern, int argc, char **argv)
Definition: db5_bin.c:568
void bu_free_external(struct bu_external *ep)
Header file for the BRL-CAD common definitions.
const char * binu_types[]
Definition: globals.c:55
#define BU_ASSERT(_equation)
Definition: defines.h:216
void bu_cv_htond(unsigned char *out, const unsigned char *in, size_t count)
#define DEBUG_HF
27 Height Field solids
Definition: raytrace.h:114
union rt_binunif_internal::@9 u
#define ID_BINUNIF
Uniform-array binary.
Definition: raytrace.h:501
void rt_binunif_dump(struct rt_binunif_internal *bip)
Definition: db5_bin.c:215
#define RT_CK_BINUNIF(_p)
Definition: raytrace.h:1016
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
int idb_major_type
Definition: raytrace.h:192
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define RT_G_DEBUG
Definition: raytrace.h:1718
#define RT_CHECK_BINUNIF(_p)
Definition: raytrace.h:1015
size_t bu_cv_w_cookie(void *out, int outcookie, size_t size, void *in, int incookie, size_t count)
Definition: convert.c:474
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
int db_get_external(struct bu_external *ep, const struct directory *dp, const struct db_i *dbip)
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
int rt_binunif_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double mm2local)
Definition: db5_bin.c:324
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
#define BRLCAD_OK
Definition: defines.h:71
uint8_t * ext_buf
Definition: parse.h:216
int bu_cv_cookie(const char *in)
Definition: convert.c:33
void * bu_realloc(void *ptr, size_t siz, const char *str)
#define UNUSED(parameter)
Definition: common.h:239
void rt_binunif_free(struct rt_binunif_internal *bip)
Definition: db5_bin.c:378
#define DEBUG_VOL
12 Volume & opaque Binary solid
Definition: raytrace.h:95
int rt_bin_mime_import5(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip, struct resource *resp)
Definition: db5_bin.c:98
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
void bu_cv_ntohf(unsigned char *out, const unsigned char *in, size_t count)
#define SIZEOF_NETWORK_FLOAT
Definition: cv.h:47
void * idb_ptr
Definition: raytrace.h:195
void bu_cv_ntohd(unsigned char *out, const unsigned char *in, size_t count)
const struct rt_functab OBJ[]
Definition: table.c:159
#define RT_CK_RESOURCE(_p)
Definition: raytrace.h:1490
int rt_bin_unif_import5(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip, struct resource *resp)
Definition: db5_bin.c:87
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
#define BU_EXTERNAL_INIT(_p)
Definition: parse.h:229
int rt_bin_unif_export5(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip, struct resource *resp)
Definition: db5_bin.c:76
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
void bu_cv_htonf(unsigned char *out, const unsigned char *in, size_t count)
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
int rt_db_get_internal5(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: db5_io.c:961
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_CK_EXTERNAL(_p)
Definition: parse.h:224
size_t ext_nbytes
Definition: parse.h:210
int rt_binunif_export5(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip, struct resource *resp)
Definition: db5_bin.c:229
HIDDEN void verbose(struct human_data_t *dude)
Definition: human.c:2008
void rt_binunif_make(const struct rt_functab *ftp, struct rt_db_internal *intern)
Definition: db5_bin.c:497
Definition: vls.h:56
#define BRLCAD_ERROR
Definition: defines.h:72
void bu_bomb(const char *str) _BU_ATTR_NORETURN
Definition: bomb.c:91
int bu_cv_optimize(int cookie)
Tcl_Interp * brlcad_interp
Definition: tcl.c:41
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126