This file is indexed.

/usr/include/hidrd/util/num.h is in libhidrd0-dev 0.2.0-11.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/** @file
 * @brief HID report descriptor - utilities - number conversions
 *
 * Copyright (C) 2010 Nikolai Kondrashov
 *
 * This file is part of hidrd.
 *
 * Hidrd is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Hidrd is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with hidrd; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author Nikolai Kondrashov <spbnick@gmail.com>
 *
 * @(#) $Id: num.h 384 2010-04-29 14:31:35Z spb_nick $
 */

#ifndef __HIDRD_UTIL_NUM_H__
#define __HIDRD_UTIL_NUM_H__

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/** The type behind convenience type name "u32" (used in macros) */
#define HIDRD_NUM_u32_TYPE  uint32_t
/** The type behind convenience type name "s32" (used in macros) */
#define HIDRD_NUM_s32_TYPE  int32_t
/** The type behind convenience type name "u16" (used in macros) */
#define HIDRD_NUM_u16_TYPE  uint16_t
/** The type behind convenience type name "s16" (used in macros) */
#define HIDRD_NUM_s16_TYPE  int16_t
/** The type behind convenience type name "u8" (used in macros) */
#define HIDRD_NUM_u8_TYPE   uint8_t
/** The type behind convenience type name "s8" (used in macros) */
#define HIDRD_NUM_s8_TYPE   int8_t

/** Number base */
typedef enum hidrd_num_base {
    HIDRD_NUM_BASE_NONE = 0,    /**< None, not specified */
    HIDRD_NUM_BASE_DEC  = 10,   /**< Decimal */
    HIDRD_NUM_BASE_HEX  = 16    /**< Hexadecimal */
} hidrd_num_base;

/** A lowercase version of HIDRD_NUM_BASE_DEC - for macro convenience */
#define HIDRD_NUM_BASE_dec  HIDRD_NUM_BASE_DEC
/** A lowercase version of HIDRD_NUM_BASE_HEX - for macro convenience */
#define HIDRD_NUM_BASE_hex  HIDRD_NUM_BASE_HEX

/**
 * Check if a number base is valid.
 *
 * @param base  Base to check.
 *
 * @return True if the base is valid, false otherwise.
 */
extern bool hidrd_num_base_valid(hidrd_num_base base);

/** Number base mark type */
typedef enum hidrd_num_bmrk {
    HIDRD_NUM_BMRK_NONE = 0,    /**< No base mark, explicit base required */
    HIDRD_NUM_BMRK_SFX,         /**< Suffix (h, b, etc) */
    HIDRD_NUM_BMRK_PFX,         /**< Prefix (0x, 0, etc) */
} hidrd_num_bmrk;

/**
 * Check if a number base mark type is valid.
 *
 * @param bmrk  Base mark type to check.
 *
 * @return True if the base mark type is valid, false otherwise.
 */
extern bool hidrd_num_bmrk_valid(hidrd_num_bmrk bmrk);

/**
 * Convert a string to an unsigned 32-bit integer.
 *
 * @param pnum  Location for the converted number; could be NULL.
 * @param str   String to convert from.
 * @param bmrk  Number base mark type; if HIDRD_NUM_BMRK_NONE, @e base must
 *              be specified (not HIDRD_NUM_BASE_NONE).
 * @param base  Number base to convert from; only applicable if @e bmrk is
 *              HIDRD_NUM_BMRK_NONE.
 *
 * @return True if the string was valid and converted successfully, false
 *         otherwise.
 */
extern bool hidrd_num_u32_from_str(uint32_t        *pnum,
                                   const char      *str,
                                   hidrd_num_bmrk   bmrk,
                                   hidrd_num_base   base);

/**
 * Convert a string to a signed 32-bit integer.
 *
 * @param pnum  Location for the converted number; could be NULL.
 * @param str   String to convert from.
 * @param bmrk  Number base mark type; if HIDRD_NUM_BMRK_NONE, @e base must
 *              be specified (not HIDRD_NUM_BASE_NONE).
 * @param base  Number base to convert from; only applicable if @e bmrk is
 *              HIDRD_NUM_BMRK_NONE.
 *
 * @return True if the string was valid and converted successfully, false
 *         otherwise.
 */
extern bool hidrd_num_s32_from_str(int32_t         *pnum,
                                   const char      *str,
                                   hidrd_num_bmrk   bmrk,
                                   hidrd_num_base   base);

/**
 * Convert a string to an unsigned 16-bit integer.
 *
 * @param pnum  Location for the converted number; could be NULL.
 * @param str   String to convert from.
 * @param bmrk  Number base mark type; if HIDRD_NUM_BMRK_NONE, @e base must
 *              be specified (not HIDRD_NUM_BASE_NONE).
 * @param base  Number base to convert from; only applicable if @e bmrk is
 *              HIDRD_NUM_BMRK_NONE.
 *
 * @return True if the string was valid and converted successfully, false
 *         otherwise.
 */
extern bool hidrd_num_u16_from_str(uint16_t        *pnum,
                                   const char      *str,
                                   hidrd_num_bmrk   bmrk,
                                   hidrd_num_base   base);

/**
 * Convert a string to a signed 16-bit integer.
 *
 * @param pnum  Location for the converted number; could be NULL.
 * @param str   String to convert from.
 * @param bmrk  Number base mark type; if HIDRD_NUM_BMRK_NONE, @e base must
 *              be specified (not HIDRD_NUM_BASE_NONE).
 * @param base  Number base to convert from; only applicable if @e bmrk is
 *              HIDRD_NUM_BMRK_NONE.
 *
 * @return True if the string was valid and converted successfully, false
 *         otherwise.
 */
extern bool hidrd_num_s16_from_str(int16_t         *pnum,
                                   const char      *str,
                                   hidrd_num_bmrk   bmrk,
                                   hidrd_num_base   base);

/**
 * Convert a string to an unsigned 8-bit integer.
 *
 * @param pnum  Location for the converted number; could be NULL.
 * @param str   String to convert from.
 * @param bmrk  Number base mark type; if HIDRD_NUM_BMRK_NONE, @e base must
 *              be specified (not HIDRD_NUM_BASE_NONE).
 * @param base  Number base to convert from; only applicable if @e bmrk is
 *              HIDRD_NUM_BMRK_NONE.
 *
 * @return True if the string was valid and converted successfully, false
 *         otherwise.
 */
extern bool hidrd_num_u8_from_str(uint8_t          *pnum,
                                  const char       *str,
                                  hidrd_num_bmrk    bmrk,
                                  hidrd_num_base    base);

/**
 * Convert a string to a signed 8-bit integer.
 *
 * @param pnum  Location for the converted number; could be NULL.
 * @param str   String to convert from.
 * @param bmrk  Number base mark type; if HIDRD_NUM_BMRK_NONE, @e base must
 *              be specified (not HIDRD_NUM_BASE_NONE).
 * @param base  Number base to convert from; only applicable if @e bmrk is
 *              HIDRD_NUM_BMRK_NONE.
 *
 * @return True if the string was valid and converted successfully, false
 *         otherwise.
 */
extern bool hidrd_num_s8_from_str(int8_t           *pnum,
                                  const char       *str,
                                  hidrd_num_bmrk    bmrk,
                                  hidrd_num_base    base);

/**
 * Convert an unsigned 32-bit integer to a string.
 *
 * @param num   The number to convert.
 * @param bmrk  Number base mark type.
 * @param base  Number base to convert to, must be specified (not
 *              HIDRD_NUM_BASE_NONE).
 *
 * @return Dynamically allocated string, or NULL if failed to allocate
 *         memory.
 */
extern char *hidrd_num_u32_to_str(uint32_t          num,
                                  hidrd_num_bmrk    bmrk,
                                  hidrd_num_base    base);

/**
 * Convert a signed 32-bit integer to a string.
 *
 * @param num   The number to convert.
 * @param bmrk  Number base mark type.
 * @param base  Number base to convert to, must be specified (not
 *              HIDRD_NUM_BASE_NONE).
 *
 * @return Dynamically allocated string, or NULL if failed to allocate
 *         memory.
 */
extern char *hidrd_num_s32_to_str(int32_t           num,
                                  hidrd_num_bmrk    bmrk,
                                  hidrd_num_base    base);

/**
 * Convert an unsigned 16-bit integer to a string.
 *
 * @param num   The number to convert.
 * @param bmrk  Number base mark type.
 * @param base  Number base to convert to, must be specified (not
 *              HIDRD_NUM_BASE_NONE).
 *
 * @return Dynamically allocated string, or NULL if failed to allocate
 *         memory.
 */
static inline char *
hidrd_num_u16_to_str(uint16_t num, hidrd_num_bmrk bmrk, hidrd_num_base base)
{
    return hidrd_num_u32_to_str(num, bmrk, base);
}

/**
 * Convert a signed 16-bit integer to a string.
 *
 * @param num   The number to convert.
 * @param bmrk  Number base mark type.
 * @param base  Number base to convert to, must be specified (not
 *              HIDRD_NUM_BASE_NONE).
 *
 * @return Dynamically allocated string, or NULL if failed to allocate
 *         memory.
 */
static inline char *
hidrd_num_s16_to_str(int16_t num, hidrd_num_bmrk bmrk, hidrd_num_base base)
{
    return hidrd_num_s32_to_str(num, bmrk, base);
}

/**
 * Convert an unsigned 8-bit integer to a string.
 *
 * @param num   The number to convert.
 * @param bmrk  Number base mark type.
 * @param base  Number base to convert to, must be specified (not
 *              HIDRD_NUM_BASE_NONE).
 *
 * @return Dynamically allocated string, or NULL if failed to allocate
 *         memory.
 */
static inline char *
hidrd_num_u8_to_str(uint8_t num, hidrd_num_bmrk bmrk, hidrd_num_base base)
{
    return hidrd_num_u32_to_str(num, bmrk, base);
}

/**
 * Convert a signed 8-bit integer to a string.
 *
 * @param num   The number to convert.
 * @param bmrk  Number base mark type.
 * @param base  Number base to convert to, must be specified (not
 *              HIDRD_NUM_BASE_NONE).
 *
 * @return Dynamically allocated string, or NULL if failed to allocate
 *         memory.
 */
static inline char *
hidrd_num_s8_to_str(int8_t num, hidrd_num_bmrk bmrk, hidrd_num_base base)
{
    return hidrd_num_s32_to_str(num, bmrk, base);
}


/**
 * Convert a specified integer number type to a string.
 *
 * @param _t    Short integer number type name (u32/s32/u16/s16/u8/s8).
 * @param _n    Number.
 * @param _m    Short base mark type name (without HIDRD_NUM_BMRK_ prefix).
 * @param _b    Short base name (without HIDRD_NUM_BASE_ prefix); cannot be
 *              NONE.
 *
 * @return Dynamically allocated string, or NULL, if failed to allocate
 *         memory.
 */
#define HIDRD_NUM_TO_STR(_t, _n, _m, _b) \
    hidrd_num_##_t##_to_str(_n, HIDRD_NUM_BMRK_##_m, HIDRD_NUM_BASE_##_b)


/**
 * Convert a string to a specified integer type.
 *
 * @param _t    Short integer number type name (u32/s32/u16/s16/u8/s8).
 * @param _pn   Location for resulting number.
 * @param _s    String to parse.
 * @param _m    Short base mark type name (without HIDRD_NUM_BMRK_ prefix);
 *              if not specified (NONE), @e _b must be specified (not NONE).
 * @param _b    Short base name (without HIDRD_NUM_BASE_ prefix); only
 *              applicable if @e _m is specified (not NONE).
 *
 * @return True if the string was valid and parsed successfully, false
 *         otherwise.
 */
#define HIDRD_NUM_FROM_STR(_t, _pn, _s, _m, _b) \
    hidrd_num_##_t##_from_str(_pn, _s,                                  \
                              HIDRD_NUM_BMRK_##_m, HIDRD_NUM_BASE_##_b)


/**
 * Declare a function set for converting a number type to/from a string.
 *
 * @param _t    Name of the type being converted, without hidrd_ prefix.
 */
#define HIDRD_NUM_CONV_DECLS(_t) \
    extern char *hidrd_##_t##_to_dec(hidrd_##_t v);                     \
    extern char *hidrd_##_t##_to_hex(hidrd_##_t v);                     \
    extern char *hidrd_##_t##_to_phex(hidrd_##_t v);                    \
    extern char *hidrd_##_t##_to_shex(hidrd_##_t v);                    \
    extern bool hidrd_##_t##_from_dec(hidrd_##_t *pv, const char *s);   \
    extern bool hidrd_##_t##_from_hex(hidrd_##_t *pv, const char *s);   \
    extern bool hidrd_##_t##_from_pstr(hidrd_##_t *pv, const char *s);  \
    extern bool hidrd_##_t##_from_sstr(hidrd_##_t *pv, const char *s)


/**
 * Define a function set for converting a number type to/from a string.
 *
 * @param _t    Name of the type being converted, without hidrd_ prefix.
 * @param _n    Short (convenience) name of the number type (like s8).
 */
#define HIDRD_NUM_CONV_DEFS(_t, _n) \
    char *                                                  \
    hidrd_##_t##_to_dec(hidrd_##_t v)                       \
    {                                                       \
        assert(hidrd_##_t##_valid(v));                      \
        return HIDRD_NUM_TO_STR(_n, v, NONE, DEC);          \
    }                                                       \
                                                            \
                                                            \
    char *                                                  \
    hidrd_##_t##_to_hex(hidrd_##_t v)                       \
    {                                                       \
        assert(hidrd_##_t##_valid(v));                      \
        return HIDRD_NUM_TO_STR(_n, v, NONE, HEX);          \
    }                                                       \
                                                            \
                                                            \
    char *                                                  \
    hidrd_##_t##_to_phex(hidrd_##_t v)                      \
    {                                                       \
        assert(hidrd_##_t##_valid(v));                      \
        return HIDRD_NUM_TO_STR(_n, v, PFX, HEX);           \
    }                                                       \
                                                            \
                                                            \
    char *                                                  \
    hidrd_##_t##_to_shex(hidrd_##_t v)                      \
    {                                                       \
        assert(hidrd_##_t##_valid(v));                      \
        return HIDRD_NUM_TO_STR(_n, v, SFX, HEX);           \
    }                                                       \
                                                            \
                                                            \
    bool                                                    \
    hidrd_##_t##_from_dec(hidrd_##_t *pv, const char *s)    \
    {                                                       \
        HIDRD_NUM_##_n##_TYPE   v;                          \
                                                            \
        assert(s != NULL);                                  \
                                                            \
        if (!HIDRD_NUM_FROM_STR(_n, &v, s, NONE, DEC) ||    \
            !hidrd_##_t##_valid(v))                         \
            return false;                                   \
                                                            \
        if (pv != NULL)                                     \
            *pv = v;                                        \
                                                            \
        return true;                                        \
    }                                                       \
                                                            \
                                                            \
    bool                                                    \
    hidrd_##_t##_from_hex(hidrd_##_t *pv, const char *s)    \
    {                                                       \
        HIDRD_NUM_##_n##_TYPE   v;                          \
                                                            \
        assert(s != NULL);                                  \
                                                            \
        if (!HIDRD_NUM_FROM_STR(_n, &v, s, NONE, HEX) ||    \
            !hidrd_##_t##_valid(v))                         \
            return false;                                   \
                                                            \
        if (pv != NULL)                                     \
            *pv = v;                                        \
                                                            \
        return true;                                        \
    }                                                       \
                                                            \
                                                            \
    bool                                                    \
    hidrd_##_t##_from_pstr(hidrd_##_t *pv, const char *s)   \
    {                                                       \
        HIDRD_NUM_##_n##_TYPE   v;                          \
                                                            \
        assert(s != NULL);                                  \
                                                            \
        if (!HIDRD_NUM_FROM_STR(_n, &v, s, PFX, NONE) ||    \
            !hidrd_##_t##_valid(v))                         \
            return false;                                   \
                                                            \
        if (pv != NULL)                                     \
            *pv = v;                                        \
                                                            \
        return true;                                        \
    }                                                       \
                                                            \
                                                            \
    bool                                                    \
    hidrd_##_t##_from_sstr(hidrd_##_t *pv, const char  *s)  \
    {                                                       \
        HIDRD_NUM_##_n##_TYPE   v;                          \
                                                            \
        assert(s != NULL);                                  \
                                                            \
        if (!HIDRD_NUM_FROM_STR(_n, &v, s, SFX, NONE) ||    \
            !hidrd_##_t##_valid(v))                         \
            return false;                                   \
                                                            \
        if (pv != NULL)                                     \
            *pv = v;                                        \
                                                            \
        return true;                                        \
    }

/**
 * Prototype for a function used to parse a number string.
 *
 * This prototype is not meant to be used to define functions, but to
 * describe hidrd_num_from_alt_str function parameters.
 *
 * @param pnum  Location for the output number.
 * @param str   The string to parse.
 *
 * @return True if the string was valid and parsed successfully.
 */
typedef bool hidrd_num_parse_fn(void *pnum, const char *str);

/**
 * Convert a number from alternate string formats.
 *
 * @param pnum  Location for the output number; could be NULL.
 * @param str   The string to parse.
 * @param ...   List of @e hidrd_num_parse_fn functions to try to parse the
 *              string with; terminated with NULL.
 *
 * @return True if at least one function succeeded parsing the string.
 */
extern bool hidrd_num_from_alt_str(void *pnum, const char *str, ...);


/**
 * Convert a number from a string representations.
 *
 * @param _type     Number type name token (used to generate parsing
 *                  function names).
 * @param _pnum     Location for the output number; could be NULL.
 * @param _str      The string to parse.
 * @param _rerp     Representation name token (used to generate parsing
 *                  function name).
 *
 * @return True if the function succeeded parsing the string.
 */
#define HIDRD_NUM_FROM_ALT_STR1(_type, _pnum, _str, _repr) \
    hidrd_num_from_alt_str(_pnum, _str,                     \
                           hidrd_##_type##_from_##_repr,    \
                           NULL)

/**
 * Convert a number from two alternate string representations.
 *
 * @param _type     Number type name token (used to generate parsing
 *                  function names).
 * @param _pnum     Location for the output number; could be NULL.
 * @param _str      The string to parse.
 * @param _rerp1    First representation name token (used to generate
 *                  first parsing function name).
 * @param _rerp2    Second representation name token (used to generate
 *                  second parsing function name).
 *
 * @return True if at least one function succeeded parsing the string.
 */
#define HIDRD_NUM_FROM_ALT_STR2(_type, _pnum, _str, \
                                _repr1, _repr2)             \
    hidrd_num_from_alt_str(_pnum, _str,                     \
                           hidrd_##_type##_from_##_repr1,   \
                           hidrd_##_type##_from_##_repr2,   \
                           NULL)

/**
 * Convert a number from three alternate string representations.
 *
 * @param _type     Number type name token (used to generate parsing
 *                  function names).
 * @param _pnum     Location for the output number; could be NULL.
 * @param _str      The string to parse.
 * @param _rerp1    First representation name token (used to generate
 *                  first parsing function name).
 * @param _rerp2    Second representation name token (used to generate
 *                  second parsing function name).
 * @param _rerp3    Third representation name token (used to generate
 *                  third parsing function name).
 *
 * @return True if at least one function succeeded parsing the string.
 */
#define HIDRD_NUM_FROM_ALT_STR3(_type, _pnum, _str, \
                                _repr1, _repr2, _repr3)     \
    hidrd_num_from_alt_str(_pnum, _str,                     \
                           hidrd_##_type##_from_##_repr1,   \
                           hidrd_##_type##_from_##_repr2,   \
                           hidrd_##_type##_from_##_repr3,   \
                           NULL)

/**
 * Convert a number from four alternate string representations.
 *
 * @param _type     Number type name token (used to generate parsing
 *                  function names).
 * @param _pnum     Location for the output number; could be NULL.
 * @param _str      The string to parse.
 * @param _rerp1    First representation name token (used to generate
 *                  first parsing function name).
 * @param _rerp2    Second representation name token (used to generate
 *                  second parsing function name).
 * @param _rerp3    Third representation name token (used to generate
 *                  third parsing function name).
 * @param _rerp4    Fourth representation name token (used to generate
 *                  fourth parsing function name).
 *
 * @return True if at least one function succeeded parsing the string.
 */
#define HIDRD_NUM_FROM_ALT_STR4(_type, _pnum, _str, \
                                _repr1, _repr2,             \
                                _repr3, _repr4)             \
    hidrd_num_from_alt_str(_pnum, _str,                     \
                           hidrd_##_type##_from_##_repr1,   \
                           hidrd_##_type##_from_##_repr2,   \
                           hidrd_##_type##_from_##_repr3,   \
                           hidrd_##_type##_from_##_repr4,   \
                           NULL)


/**
 * Prototype for a function used to format an 8-bit number string (either
 * signed or unsigned).
 *
 * This prototype is not meant to be used to define functions, but to
 * describe hidrd_num_to_alt_str function parameters.
 *
 * @param num   The number to convert.
 *
 * @return Dynamically allocated string, or NULL if the match is not found
 *         or an error occurred; check errno for the latter.
 */
typedef char *hidrd_num_fmt8_fn(int8_t num);
typedef char *hidrd_num_fmtu8_fn(uint8_t num);

/**
 * Prototype for a function used to format an 16-bit number string (either
 * signed or unsigned).
 *
 * This prototype is not meant to be used to define functions, but to
 * describe hidrd_num_to_alt_str function parameters.
 *
 * @param num   The number to convert.
 *
 * @return Dynamically allocated string, or NULL if the match is not found
 *         or an error occurred; check errno for the latter.
 */
typedef char *hidrd_num_fmt16_fn(int16_t num);
typedef char *hidrd_num_fmtu16_fn(uint16_t num);

/**
 * Prototype for a function used to format an 32-bit number string (either
 * signed or unsigned).
 *
 * This prototype is not meant to be used to define functions, but to
 * describe hidrd_num_to_alt_str function parameters.
 *
 * @param num   The number to convert.
 *
 * @return Dynamically allocated string, or NULL if the match is not found
 *         or an error occurred; check errno for the latter.
 */
typedef char *hidrd_num_fmt32_fn(int32_t num);
typedef char *hidrd_num_fmtu32_fn(uint32_t num);

/**
 * Convert a number to a string of alternate formats.
 *
 * @param bits  Size of the number in bits; only 8, 16 and 32 accepted.
 * @param ...   A succession of arguments:
 *              - the number to convert;
 *              - conversion function chains (hidrd_num_fmtX_fn,
 *                hidrd_str_proc_fn..., NULL), terminated by an empty chain
 *                (NULL).
 *
 * @return Dynamically allocated string, or NULL if failed to find a match,
 *         or an error occurred; check errno for the latter.
 *
 * @note Always resets errno.
 */
char *hidrd_num_to_alt_str(size_t bits, int sign, ...);

/**
 * Convert a number to a string of two alternate formats, without additional
 * conversion.
 *
 * @param _t    The number type name token.
 * @param _s    True iff the type name token is a signed type.
 * @param _n    The number to convert.
 * @param _r1   First converter representation token.
 * @param _r2   Second converter representation token.
 *
 * @return Dynamically allocated string, or NULL if failed to find a match
 *         or an error occurred; check errno for the latter.
 */
#define HIDRD_NUM_TO_ALT_STR1_1(_t, _s, _n, _r1, _r2)	\
    hidrd_num_to_alt_str(sizeof(hidrd_##_t) * 8, _s, _n,\
                         hidrd_##_t##_to_##_r1, NULL,   \
                         hidrd_##_t##_to_##_r2, NULL,   \
                         NULL)

/**
 * Convert a number to a string of two alternate formats, with first format
 * string having additional processing.
 *
 * @param _t    The number type name token.
 * @param _s    True iff the type name token is a signed type.
 * @param _n    The number to convert.
 * @param _r1   First converter representation token (hex, token, etc.).
 * @param _p1   First converter processing function name (uc, lc, etc.).
 * @param _r2   Second converter representation token (hex, token, etc.).
 *
 * @return Dynamically allocated string, or NULL if failed to find a match
 *         or an error occurred; check errno for the latter.
 */
#define HIDRD_NUM_TO_ALT_STR2_1(_t, _s, _n, _r1, _p1, _r2)		\
    hidrd_num_to_alt_str(sizeof(hidrd_##_t) * 8, _s, _n, 		\
                         hidrd_##_t##_to_##_r1, hidrd_str_##_p1, NULL,  \
                         hidrd_##_t##_to_##_r2, NULL,                   \
                         NULL)

/**
 * Convert a 32-bit unsigned number to little-endian order.
 *
 * @param ple   Location for the output number in little-endian order.
 * @param num   Input number in host order.
 */
extern void hidrd_num_u32_to_le(uint32_t *ple, uint32_t num);

/**
 * Convert a 32-bit unsigned number from little-endian order.
 *
 * @param ple   Location of the input number in little-endian order.
 *
 * @return Output number in host order.
 */
extern uint32_t hidrd_num_u32_from_le(const uint32_t *ple);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* __HIDRD_UTIL_NUM_H__ */