Deleted Added
full compact
libusb20_desc.c (185087) libusb20_desc.c (186730)
1/* $FreeBSD: head/lib/libusb20/libusb20_desc.c 185087 2008-11-19 08:56:35Z alfred $ */
1/* $FreeBSD: head/lib/libusb20/libusb20_desc.c 186730 2009-01-04 00:12:01Z alfred $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 223 unchanged lines hidden (view full) ---

233 * Return values:
234 * NULL: End of descriptors
235 * Else: Pointer to next descriptor
236 *------------------------------------------------------------------------*/
237const uint8_t *
238libusb20_desc_foreach(const struct libusb20_me_struct *pdesc,
239 const uint8_t *psubdesc)
240{
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 223 unchanged lines hidden (view full) ---

233 * Return values:
234 * NULL: End of descriptors
235 * Else: Pointer to next descriptor
236 *------------------------------------------------------------------------*/
237const uint8_t *
238libusb20_desc_foreach(const struct libusb20_me_struct *pdesc,
239 const uint8_t *psubdesc)
240{
241 const void *end;
241 const uint8_t *start;
242 const uint8_t *end;
243 const uint8_t *desc_next;
242
244
243 if (pdesc == NULL) {
245 /* be NULL safe */
246 if (pdesc == NULL)
244 return (NULL);
247 return (NULL);
245 }
246 end = LIBUSB20_ADD_BYTES(pdesc->ptr, pdesc->len);
247
248
248 if (psubdesc == NULL) {
249 psubdesc = LIBUSB20_ADD_BYTES(pdesc->ptr, 0);
250 } else {
251 psubdesc = LIBUSB20_ADD_BYTES(psubdesc, psubdesc[0]);
252 }
253 return (((((const void *)psubdesc) >= ((void *)(pdesc->ptr))) &&
254 (((const void *)psubdesc) < end) &&
255 (LIBUSB20_ADD_BYTES(psubdesc, psubdesc[0]) >= ((void *)(pdesc->ptr))) &&
256 (LIBUSB20_ADD_BYTES(psubdesc, psubdesc[0]) <= end) &&
257 (psubdesc[0] >= 3)) ? psubdesc : NULL);
249 start = (const uint8_t *)pdesc->ptr;
250 end = LIBUSB20_ADD_BYTES(start, pdesc->len);
251
252 /* get start of next descriptor */
253 if (psubdesc == NULL)
254 psubdesc = start;
255 else
256 psubdesc = psubdesc + psubdesc[0];
257
258 /* check that the next USB descriptor is within the range */
259 if ((psubdesc < start) || (psubdesc >= end))
260 return (NULL); /* out of range, or EOD */
261
262 /* check start of the second next USB descriptor, if any */
263 desc_next = psubdesc + psubdesc[0];
264 if ((desc_next < start) || (desc_next > end))
265 return (NULL); /* out of range */
266
267 /* check minimum descriptor length */
268 if (psubdesc[0] < 3)
269 return (NULL); /* too short descriptor */
270
271 return (psubdesc); /* return start of next descriptor */
258}
259
260/*------------------------------------------------------------------------*
261 * libusb20_me_get_1 - safety wrapper to read out one byte
262 *------------------------------------------------------------------------*/
263uint8_t
264libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset)
265{

--- 506 unchanged lines hidden ---
272}
273
274/*------------------------------------------------------------------------*
275 * libusb20_me_get_1 - safety wrapper to read out one byte
276 *------------------------------------------------------------------------*/
277uint8_t
278libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset)
279{

--- 506 unchanged lines hidden ---