aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
blob: 7f778a527205c9c29e108acb8680ab540ff76880 (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
/**
 * \file util.c
 *
 * This file contains generic utility functions such as can be
 * used for debugging for example.
 *
 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* MSVC does not have these */
#ifndef _MSC_VER
#include <sys/time.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "config.h"
#include "libmtp.h"
#include "util.h"


/**
 * This prints to stdout info about device being UNKNOWN, its
 * ids, and libmtp's version number.
 *
 * @param dev_number the device number
 * @param id_vendor vendor ID from the usb_device_desc struct
 * @param id_product product ID from the usb_device_desc struct
 */
void device_unknown(const int dev_number, const int id_vendor, const int id_product)
{
  // This device is unknown to the developers
  LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN in libmtp v%s.\n",
    dev_number,
    id_vendor,
    id_product,
    LIBMTP_VERSION_STRING);
  LIBMTP_ERROR("Please report this VID/PID and the device model to the "
               "libmtp development team\n");
  /*
   * Trying to get iManufacturer or iProduct from the device at this
   * point would require opening a device handle, that we don't want
   * to do right now. (Takes time for no good enough reason.)
   */
}

/**
 * This dumps out a number of bytes to a textual, hexadecimal
 * dump.
 *
 * @param f the file to dump to (e.g. stdout or stderr)
 * @param buf a pointer to the buffer containing the bytes to
 *            be dumped out in hex
 * @param n the number of bytes to dump from this buffer
 */
void data_dump (FILE *f, void *buf, uint32_t n)
{
  unsigned char *bp = (unsigned char *) buf;
  uint32_t i;

  for (i = 0; i < n; i++) {
    fprintf(f, "%02x ", *bp);
    bp++;
  }
  fprintf(f, "\n");
}

/**
 * This dumps out a number of bytes to a textual, hexadecimal
 * dump, and also prints out the string ASCII representation
 * for each line of bytes. It will also print the memory address
 * offset from a certain boundry.
 *
 * @param f the file to dump to (e.g. stdout or stderr)
 * @param buf a pointer to the buffer containing the bytes to
 *            be dumped out in hex
 * @param n the number of bytes to dump from this buffer
 * @param dump_boundry the address offset to start at (usually 0)
 */
void data_dump_ascii (FILE *f, void *buf, uint32_t n, uint32_t dump_boundry)
{
  uint32_t remain = n;
  uint32_t ln, lc;
  int i;
  unsigned char *bp = (unsigned char *) buf;

  lc = 0;
  while (remain) {
    fprintf(f, "\t%04x:", dump_boundry-0x10);

    ln = ( remain > 16 ) ? 16 : remain;

    for (i = 0; i < ln; i++) {
      if ( ! (i%2) ) fprintf(f, " ");
      fprintf(f, "%02x", bp[16*lc+i]);
    }

    if ( ln < 16 ) {
      int width = ((16-ln)/2)*5 + (2*(ln%2));
      fprintf(f, "%*.*s", width, width, "");
    }

    fprintf(f, "\t");
    for (i = 0; i < ln; i++) {
      unsigned char ch= bp[16*lc+i];
      fprintf(f, "%c", ( ch >= 0x20 && ch <= 0x7e ) ?
	      ch : '.');
    }
    fprintf(f, "\n");

    lc++;
    remain -= ln;
    dump_boundry += ln;
  }
}

#ifndef HAVE_STRNDUP
char *strndup (const char *s, size_t n)
{
  size_t len = strlen (s);
  char *ret;

  if (len <= n)
    return strdup (s);

  ret = malloc(n + 1);
  strncpy(ret, s, n);
  ret[n] = '\0';
  return ret;
}
#endif