summaryrefslogtreecommitdiff
path: root/support/dktools/dk3maoh8.ctr
blob: b2aa479dc988403f1375dc9cb48c7237d57e6b49 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2014-xxxx
license		=	bsd



%%	header

/**	@file	dk3maod8.h Write numbers to hexadecimal char strings.
*/

#include <dk3conf.h>

#include <stdio.h>
#if DK3_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if DK3_HAVE_STDINT
#include <stdint.h>
#endif
#if DK3_HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#if DK3_HAVE_LIMITS_H
#include <limits.h>
#endif
#if DK3_HAVE_MATH_H
#include <math.h>
#endif
#if DK3_HAVE_FLOAT_H
#include <float.h>
#endif

#include <dk3types.h>

#ifdef __cplusplus
extern "C" {
#endif

/**	Convert largest size unsigned integer to hexadecimal string.
	@param	rb	Result buffer.
	@param	sz	Result buffer size (number of bytes).
	@param	va	Value to convert.
	@param	leftpad	Padding size, 0 for no padding, 1 for automatic
	choice (2*sizeof(dk3_um_t)), other values specified directly.
	@return	1 on success, 0 on error.
*/
int
dk3ma_um_to_c8_hex_string(char *rb, size_t sz, dk3_um_t va, size_t leftpad);

/**	Convert largest size unsigned integer to hexadecimal string.
	@param	rb	Result buffer.
	@param	sz	Result buffer size (number of bytes).
	@param	va	Value to convert.
	@param	leftpad	Padding size, 0 for no padding, 1 for automatic
	choice (2*sizeof(dk3_im_t)), other values specified directly.
	@return	1 on success, 0 on error.
*/
int
dk3ma_im_to_c8_hex_string(char *rb, size_t sz, dk3_im_t va, size_t leftpad);

#ifdef __cplusplus
}
#endif

%%	module

#include "dk3ma.h"



$!trace-include



#ifndef DK3MASTR_HEX_PADDED_LEFT
/**	When converting to hexadecimal notation, use padding.
*/
#define	DK3MASTR_HEX_PADDED_LEFT	1
#endif



/**	Size correction for alignment.
*/
#define	DK3MAOH8_ALIGN(sz,al) \
((0 == (sz % al)) ? (sz) : (((sz / al) + 1) * al))



int
dk3ma_um_to_c8_hex_string(char *rb, size_t sz, dk3_um_t va, size_t leftpad)
{
  char		 buf[DK3MAOH8_ALIGN((8*DK3_SIZEOF_UM),16)];
  char		*ptr;
  dk3_um_t	 x;
  size_t	 szmax	=	sizeof(buf);
  size_t	 szused	=	0;
  size_t	 i;
  int		 back	=	1;
  char		 c;

  if ((NULL != rb) && (0 < sz)) {
    rb[0] = '\0';
    ptr = buf;
    if (2 <= szmax) {
      do {
        x = va % 16;
	va = va / 16;
	c = '\0';
	switch((int)x) {
          case  0: { c = '0'; } break;
          case  1: { c = '1'; } break;
          case  2: { c = '2'; } break;
          case  3: { c = '3'; } break;
          case  4: { c = '4'; } break;
          case  5: { c = '5'; } break;
          case  6: { c = '6'; } break;
          case  7: { c = '7'; } break;
          case  8: { c = '8'; } break;
          case  9: { c = '9'; } break;
          case 10: { c = 'A'; } break;
          case 11: { c = 'B'; } break;
          case 12: { c = 'C'; } break;
          case 13: { c = 'D'; } break;
          case 14: { c = 'E'; } break;
          case 15: { c = 'F'; } break;
	}
	if (szused < szmax) {
	  *(ptr++) = c;
	  szused++;
	} else {
	  back = 0;
	}
      } while((DK3_UM_0 != va) && (szused < szmax) && (1 == back));
      if (szused < szmax) {
        buf[szused] = '\0';
      } else {
        back = 0;
	buf[szmax - 1] = '\0';
      }
      if (1 == back) {
        if (szused < sz) {
#if VERSION_BEFORE_20141005
/*
*/
#if DK3MASTR_HEX_PADDED_LEFT
	  if ((2 * DK3_SIZEOF_UM) < sz) {
	    for (i = 0; i < (2 * DK3_SIZEOF_UM); i++) {
	      rb[i] = '0';
	    }
	    rb[2 * DK3_SIZEOF_UM] = '\0';
	    for (i = 0; i < szused; i++) {
	      rb[(2 * DK3_SIZEOF_UM) - 1 - i] = buf[i];
	    }
	  } else {
#endif
	    for (i = 0; i < szused; i++) {
	      rb[i] = buf[szused - i - 1];
	    }
	    rb[szused] = '\0';
#if DK3MASTR_HEX_PADDED_LEFT
	  }
#endif
#else
	  if (1 == leftpad) { leftpad = 2 * DK3_SIZEOF_UM; }
	  if (0 != leftpad) {
	    if ((szused <= leftpad) && (leftpad < sz)) {
	      for (i = 0; i < leftpad; i++) {
	        rb[i] = '0';
	      }
	      rb[leftpad] = '\0';
	      for (i = 0; i < szused; i++) {
	        rb[leftpad - 1 - i] = buf[i];
	      }
	    } else {
	      back = 0;
	    }
	  } else {
	    for (i = 0; i < szused; i++) {
	      rb[i] = buf[szused - i - 1];
	    }
	    rb[szused] = '\0';
	  }
#endif
	} else {
	  back = 0;
	}
      }
    }
  }
  return back;
}



int
dk3ma_im_to_c8_hex_string(char *rb, size_t sz, dk3_im_t va, size_t leftpad)
{
  return (dk3ma_um_to_c8_hex_string(rb, sz, (dk3_um_t)va, leftpad));
}