static guint gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length) { GstAlsaSink *alsa; gint err; gint cptr; gint16 *ptr = data; alsa = GST_ALSA_SINK (asink); if (alsa->iec958 && alsa->need_swap) { guint i; GST_DEBUG_OBJECT (asink, "swapping bytes"); for (i = 0; i < length / 2; i++) { ptr[i] = GUINT16_SWAP_LE_BE (ptr[i]); } } GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length); cptr = length / alsa->bytes_per_sample; GST_ALSA_SINK_LOCK (asink); while (cptr > 0) { /* start by doing a blocking wait for free space. Set the timeout * to 4 times the period time */ err = snd_pcm_wait (alsa->handle, (4 * alsa->period_time / 1000)); if (err < 0) { GST_DEBUG_OBJECT (asink, "wait error, %d", err); } else { GST_DELAY_SINK_LOCK (asink); err = snd_pcm_writei (alsa->handle, ptr, cptr); GST_DELAY_SINK_UNLOCK (asink); } GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr); if (err < 0) { GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err)); if (err == -EAGAIN) { continue; } else if (xrun_recovery (alsa, alsa->handle, err) < 0) { goto write_error; } continue; } ptr += snd_pcm_frames_to_bytes (alsa->handle, err); cptr -= err; } GST_ALSA_SINK_UNLOCK (asink); return length - (cptr * alsa->bytes_per_sample); write_error: { GST_ALSA_SINK_UNLOCK (asink); return length; /* skip one period */ } }
int main (int argc, char *argv[]) { guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U; guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U; #ifdef G_HAVE_GINT64 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U), gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU); #endif /* type sizes */ g_assert (sizeof (gint8) == 1); g_assert (sizeof (gint16) == 2); g_assert (sizeof (gint32) == 4); #ifdef G_HAVE_GINT64 g_assert (sizeof (gint64) == 8); #endif /* G_HAVE_GINT64 */ g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2); g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2); #ifdef G_HAVE_GINT64 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2); #endif return 0; }
Elf32_Half read_half(Elf32_Half h) { if (need_byteswap) h = GUINT16_SWAP_LE_BE(h); return h; }
void write_half(Elf32_Half *ptr, Elf32_Half h) { if (need_byteswap) h = GUINT16_SWAP_LE_BE(h); *ptr = h; }
static void _old_entry_swap_bytes(OldNVEntry *entry) { _swap_old_entry_flags(entry); entry->alloc_len = GUINT16_SWAP_LE_BE(entry->alloc_len); if (!entry->indirect) { entry->vdirect.value_len = GUINT16_SWAP_LE_BE(entry->vdirect.value_len); } else { entry->vindirect.handle = GUINT16_SWAP_LE_BE(entry->vindirect.handle); entry->vindirect.ofs = GUINT16_SWAP_LE_BE(entry->vindirect.ofs); entry->vindirect.len = GUINT16_SWAP_LE_BE(entry->vindirect.len); } }
// pgm -- portable gray map. values are between 0 and max, single channel static dt_imageio_retval_t _read_pgm(dt_image_t *img, FILE*f, float *buf) { dt_imageio_retval_t result = DT_IMAGEIO_OK; unsigned int max; int ret = fscanf(f, "%u", &max); if(ret != 1 || max > 65535) return DT_IMAGEIO_FILE_CORRUPTED; if(max <= 255) { uint8_t *line = calloc(img->width, sizeof(uint8_t)); float *buf_iter = buf; for(size_t y = 0; y < img->height; y++) { if(fread(line, sizeof(uint8_t), (size_t)img->width, f) != img->width) { result = DT_IMAGEIO_FILE_CORRUPTED; break; } for(size_t x = 0; x < img->width; x++) { float value = (float)line[x] / (float)max; buf_iter[0] = buf_iter[1] = buf_iter[2] = value; buf_iter[3] = 0.0; buf_iter += 4; } } free(line); } else { uint16_t *line = calloc(img->width, sizeof(uint16_t)); float *buf_iter = buf; for(size_t y = 0; y < img->height; y++) { if(fread(line, sizeof(uint16_t), (size_t)img->width, f) != img->width) { result = DT_IMAGEIO_FILE_CORRUPTED; break; } for(size_t x = 0; x < img->width; x++) { uint16_t intvalue = line[x]; if(G_BYTE_ORDER != G_BIG_ENDIAN) intvalue = GUINT16_SWAP_LE_BE(intvalue); float value = (float)intvalue / (float)max; buf_iter[0] = buf_iter[1] = buf_iter[2] = value; buf_iter[3] = 0.0; buf_iter += 4; } } free(line); } return result; }
static int convert_swap_sign_and_endian_to_native(struct xmms_convert_buffers* buf, void **data, int length) { guint16 *ptr = *data; int i; for (i = 0; i < length; i += 2, ptr++) *ptr = GUINT16_SWAP_LE_BE(*ptr) ^ 1 << 15; return i; }
static int convert_swap_endian(void **data, int length) { guint16 *ptr = *data; int i; for (i = 0; i < length; i += 2, ptr++) *ptr = GUINT16_SWAP_LE_BE(*ptr); return i; }
static int convert_swap_sign_and_endian_to_alien(void **data, int length) { guint16 *ptr = *data; int i; for (i = 0; i < length; i += 2, ptr++) *ptr = GUINT16_SWAP_LE_BE(*ptr ^ 1 << 15); return i; }
void vips__copy_2byte( gboolean swap, unsigned char *to, unsigned char *from ) { guint16 *in = (guint16 *) from; guint16 *out = (guint16 *) to; if( swap ) *out = GUINT16_SWAP_LE_BE( *in ); else *out = *in; }
/* Swap pairs of bytes. */ static void vips_copy_swap2( VipsPel *in, VipsPel *out, int width, VipsImage *im ) { guint16 *p = (guint16 *) in; guint16 *q = (guint16 *) out; int sz = (VIPS_IMAGE_SIZEOF_PEL( im ) * width) / 2; int x; for( x = 0; x < sz; x++ ) q[x] = GUINT16_SWAP_LE_BE( p[x] ); }
static void convert_buffer(gpointer buffer, gint length) { gint i; if (afmt == FMT_S8) { guint8 *ptr1 = buffer; gint8 *ptr2 = buffer; for (i = 0; i < length; i++) *(ptr1++) = *(ptr2++) ^ 128; } if (afmt == FMT_S16_BE) { gint16 *ptr = buffer; for (i = 0; i < length >> 1; i++, ptr++) *ptr = GUINT16_SWAP_LE_BE(*ptr); } if (afmt == FMT_S16_NE) { gint16 *ptr = buffer; for (i = 0; i < length >> 1; i++, ptr++) *ptr = GINT16_TO_LE(*ptr); } if (afmt == FMT_U16_BE) { gint16 *ptr1 = buffer; guint16 *ptr2 = buffer; for (i = 0; i < length >> 1; i++, ptr2++) *(ptr1++) = GINT16_TO_LE(GUINT16_FROM_BE(*ptr2) ^ 32768); } if (afmt == FMT_U16_LE) { gint16 *ptr1 = buffer; guint16 *ptr2 = buffer; for (i = 0; i < length >> 1; i++, ptr2++) *(ptr1++) = GINT16_TO_LE(GUINT16_FROM_LE(*ptr2) ^ 32768); } if (afmt == FMT_U16_NE) { gint16 *ptr1 = buffer; guint16 *ptr2 = buffer; for (i = 0; i < length >> 1; i++, ptr2++) *(ptr1++) = GINT16_TO_LE((*ptr2) ^ 32768); } }
const int16_t *pcm_byteswap_16(struct pcm_buffer *buffer, const int16_t *src, size_t len) { int16_t *buf = pcm_buffer_get(buffer, len); assert(buf != NULL); const int16_t *src_end = src + len / sizeof(*src); int16_t *dest = buf; while (src < src_end) { const int16_t x = *src++; *dest++ = GUINT16_SWAP_LE_BE(x); } return buf; }
/* Don't do this genericaly, union's suck genericaly */ static gboolean giop_GIOP_TargetAddress_demarshal (GIOPRecvBuffer *buf, GIOP_TargetAddress *value) { gboolean do_bswap = giop_msg_conversion_needed (buf); buf->cur = ALIGN_ADDRESS(buf->cur, 2); if ((buf->cur + 2) > buf->end) return TRUE; if (do_bswap) value->_d = GUINT16_SWAP_LE_BE ( *(guint16 *) buf->cur); else value->_d = *(guint16 *) buf->cur; buf->cur += 2; switch (value->_d) { case GIOP_KeyAddr: buf->cur = ALIGN_ADDRESS (buf->cur, 4); if ((buf->cur + 4) > buf->end) return TRUE; value->_u.object_key._release = CORBA_FALSE; if (do_bswap) value->_u.object_key._length = GUINT32_SWAP_LE_BE(*((guint32 *)buf->cur)); else value->_u.object_key._length = *((guint32 *)buf->cur); buf->cur += 4; if ((buf->cur + value->_u.object_key._length) > buf->end || (buf->cur + value->_u.object_key._length) < buf->cur) return TRUE; value->_u.object_key._buffer = buf->cur; buf->cur += value->_u.object_key._length; break; case GIOP_ProfileAddr: g_warning ("XXX FIXME GIOP_ProfileAddr not handled"); return TRUE; break; case GIOP_ReferenceAddr: g_warning ("XXX FIXME GIOP_ReferenceAddr not handled"); return TRUE; break; } return FALSE; }
static void fix_utf16_endianness (gunichar2 *utf16) { gunichar2 *it; if ((utf16 == NULL) || (*utf16 == '\0')) { return; } if (*utf16 != ANTIBOM) { return; } for (it = utf16; *it != '\0'; it++) { *it = GUINT16_SWAP_LE_BE (*it); } }
int main( int argc, char *argv[] ) { tUint16 value = 0x42aa; tUint16 swap = GUINT16_SWAP_LE_BE( value ); tUint16 result; unsigned int s = Demarshal( MarshalByteOrder() ? 0 : 1, &Marshal_Uint16Type, &result, &swap ); if ( s != sizeof( tUint16 ) ) return 1; if ( value != result ) return 1; return 0; }
// ppm -- portable pix map. values are between 0 and max, three channels static dt_imageio_retval_t _read_ppm(dt_image_t *img, FILE*f, float *buf) { dt_imageio_retval_t result = DT_IMAGEIO_OK; unsigned int max; int ret = fscanf(f, "%u", &max); if(ret != 1 || max > 65535) return DT_IMAGEIO_FILE_CORRUPTED; if(max <= 255) { uint8_t *line = calloc((size_t)3 * img->width, sizeof(uint8_t)); float *buf_iter = buf; for(size_t y = 0; y < img->height; y++) { if(fread(line, 3 * sizeof(uint8_t), (size_t)img->width, f) != img->width) { result = DT_IMAGEIO_FILE_CORRUPTED; break; } for(size_t x = 0; x < img->width; x++) { for(int c = 0; c < 3; c++) { float value = (float)line[x * 3 + c] / (float)max; *buf_iter++ = value; } *buf_iter++ = 0.0; } } free(line); } else { uint16_t *line = calloc((size_t)3 * img->width, sizeof(uint16_t)); float *buf_iter = buf; for(size_t y = 0; y < img->height; y++) { if(fread(line, 3 * sizeof(uint16_t), (size_t)img->width, f) != img->width) { result = DT_IMAGEIO_FILE_CORRUPTED; break; } for(size_t x = 0; x < img->width; x++) { for(int c = 0; c < 3; c++) { uint16_t intvalue = line[x * 3 + c]; // PPM files are big endian! http://netpbm.sourceforge.net/doc/ppm.html if(G_BYTE_ORDER != G_BIG_ENDIAN) intvalue = GUINT16_SWAP_LE_BE(intvalue); float value = (float)intvalue / (float)max; *buf_iter++ = value; } *buf_iter++ = 0.0; } } free(line); } return result; }
int main (int argc, char *argv[]) { gchar *string; gushort gus; guint gui; gulong gul; gsize gsz; gshort gs; gint gi; glong gl; gint16 gi16t1; gint16 gi16t2; gint32 gi32t1; gint32 gi32t2; guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U; guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U; guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U), gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU); gint64 gi64t1; gint64 gi64t2; gssize gsst1; gssize gsst2; gsize gst1; gsize gst2; #ifdef SYMBIAN g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); g_set_print_handler(mrtPrintHandler); #endif /*SYMBIAN*/ /* type sizes */ g_assert (sizeof (gint8) == 1); g_assert (sizeof (gint16) == 2); g_assert (sizeof (gint32) == 4); g_assert (sizeof (gint64) == 8); g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2); g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2); g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2); /* Test the G_(MIN|MAX|MAXU)(SHORT|INT|LONG) macros */ gus = G_MAXUSHORT; gus++; g_assert (gus == 0); gui = G_MAXUINT; gui++; g_assert (gui == 0); gul = G_MAXULONG; gul++; g_assert (gul == 0); gsz = G_MAXSIZE; gsz++; g_assert (gsz == 0); gs = G_MAXSHORT; gs++; g_assert (gs == G_MINSHORT); gi = G_MAXINT; gi++; g_assert (gi == G_MININT); gl = G_MAXLONG; gl++; g_assert (gl == G_MINLONG); /* Test the G_G(U)?INT(16|32|64)_FORMAT macros */ gi16t1 = -0x3AFA; gu16t1 = 0xFAFA; gi32t1 = -0x3AFAFAFA; gu32t1 = 0xFAFAFAFA; #define FORMAT "%" G_GINT16_FORMAT " %" G_GINT32_FORMAT \ " %" G_GUINT16_FORMAT " %" G_GUINT32_FORMAT "\n" string = g_strdup_printf (FORMAT, gi16t1, gi32t1, gu16t1, gu32t1); sscanf (string, FORMAT, &gi16t2, &gi32t2, &gu16t2, &gu32t2); g_free (string); g_assert (gi16t1 == gi16t2); g_assert (gi32t1 == gi32t2); g_assert (gu16t1 == gu16t2); g_assert (gu32t1 == gu32t2); gi64t1 = G_GINT64_CONSTANT (-0x3AFAFAFAFAFAFAFA); gu64t1 = G_GINT64_CONSTANT (0xFAFAFAFAFAFAFAFA); #define FORMAT64 "%" G_GINT64_FORMAT " %" G_GUINT64_FORMAT "\n" string = g_strdup_printf (FORMAT64, gi64t1, gu64t1); sscanf (string, FORMAT64, &gi64t2, &gu64t2); g_free (string); g_assert (gi64t1 == gi64t2); g_assert (gu64t1 == gu64t2); gsst1 = -0x3AFAFAFA; gst1 = 0xFAFAFAFA; #define FORMATSIZE "%" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT "\n" string = g_strdup_printf (FORMATSIZE, gsst1, gst1); sscanf (string, FORMATSIZE, &gsst2, &gst2); g_free (string); g_assert (gsst1 == gsst2); g_assert (gst1 == gst2); #ifdef SYMBIAN testResultXml("type-test"); #endif /* EMULATOR */ return 0; }
wtap_open_return_val libpcap_open(wtap *wth, int *err, gchar **err_info) { guint32 magic; struct pcap_hdr hdr; gboolean byte_swapped; gboolean modified; gboolean aix; int file_encap; gint64 first_packet_offset; libpcap_t *libpcap; static const int subtypes_modified[] = { WTAP_FILE_TYPE_SUBTYPE_PCAP_SS991029, WTAP_FILE_TYPE_SUBTYPE_PCAP_SS990915 }; #define N_SUBTYPES_MODIFIED G_N_ELEMENTS(subtypes_modified) static const int subtypes_standard[] = { WTAP_FILE_TYPE_SUBTYPE_PCAP, WTAP_FILE_TYPE_SUBTYPE_PCAP_SS990417, WTAP_FILE_TYPE_SUBTYPE_PCAP_NOKIA }; #define N_SUBTYPES_STANDARD G_N_ELEMENTS(subtypes_standard) static const int subtypes_nsec[] = { WTAP_FILE_TYPE_SUBTYPE_PCAP_NSEC }; #define N_SUBTYPES_NSEC G_N_ELEMENTS(subtypes_nsec) #define MAX_FIGURES_OF_MERIT \ MAX(MAX(N_SUBTYPES_MODIFIED, N_SUBTYPES_STANDARD), N_SUBTYPES_NSEC) int figures_of_merit[MAX_FIGURES_OF_MERIT]; const int *subtypes; int n_subtypes; int best_subtype; int i; /* Read in the number that should be at the start of a "libpcap" file */ if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) { if (*err != WTAP_ERR_SHORT_READ) return WTAP_OPEN_ERROR; return WTAP_OPEN_NOT_MINE; } switch (magic) { case PCAP_MAGIC: /* Host that wrote it has our byte order, and was running a program using either standard or ss990417 libpcap. */ byte_swapped = FALSE; modified = FALSE; wth->file_tsprec = WTAP_TSPREC_USEC; break; case PCAP_MODIFIED_MAGIC: /* Host that wrote it has our byte order, and was running a program using either ss990915 or ss991029 libpcap. */ byte_swapped = FALSE; modified = TRUE; wth->file_tsprec = WTAP_TSPREC_USEC; break; case PCAP_SWAPPED_MAGIC: /* Host that wrote it has a byte order opposite to ours, and was running a program using either standard or ss990417 libpcap. */ byte_swapped = TRUE; modified = FALSE; wth->file_tsprec = WTAP_TSPREC_USEC; break; case PCAP_SWAPPED_MODIFIED_MAGIC: /* Host that wrote it out has a byte order opposite to ours, and was running a program using either ss990915 or ss991029 libpcap. */ byte_swapped = TRUE; modified = TRUE; wth->file_tsprec = WTAP_TSPREC_USEC; break; case PCAP_NSEC_MAGIC: /* Host that wrote it has our byte order, and was writing the file in a format similar to standard libpcap except that the time stamps have nanosecond resolution. */ byte_swapped = FALSE; modified = FALSE; wth->file_tsprec = WTAP_TSPREC_NSEC; break; case PCAP_SWAPPED_NSEC_MAGIC: /* Host that wrote it out has a byte order opposite to ours, and was writing the file in a format similar to standard libpcap except that the time stamps have nanosecond resolution. */ byte_swapped = TRUE; modified = FALSE; wth->file_tsprec = WTAP_TSPREC_NSEC; break; default: /* Not a "libpcap" type we know about. */ return WTAP_OPEN_NOT_MINE; } /* Read the rest of the header. */ if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info)) return WTAP_OPEN_ERROR; if (byte_swapped) { /* Byte-swap the header fields about which we care. */ hdr.version_major = GUINT16_SWAP_LE_BE(hdr.version_major); hdr.version_minor = GUINT16_SWAP_LE_BE(hdr.version_minor); hdr.snaplen = GUINT32_SWAP_LE_BE(hdr.snaplen); hdr.network = GUINT32_SWAP_LE_BE(hdr.network); } if (hdr.version_major < 2) { /* We only support version 2.0 and later. */ *err = WTAP_ERR_UNSUPPORTED; *err_info = g_strdup_printf("pcap: major version %u unsupported", hdr.version_major); return WTAP_OPEN_ERROR; } /* * AIX's non-standard tcpdump uses a minor version number of 2. * Unfortunately, older versions of libpcap might have used * that as well. * * The AIX libpcap uses RFC 1573 ifType values rather than * DLT_ values in the header; the ifType values for LAN devices * are: * * Ethernet 6 * Token Ring 9 * FDDI 15 * * which correspond to DLT_IEEE802 (used for Token Ring), * DLT_PPP, and DLT_SLIP_BSDOS, respectively. The ifType value * for a loopback interface is 24, which currently isn't * used by any version of libpcap I know about (and, as * tcpdump.org are assigning DLT_ values above 100, and * NetBSD started assigning values starting at 50, and * the values chosen by other libpcaps appear to stop at * 19, it's probably not going to be used by any libpcap * in the future). * * We shall assume that if the minor version number is 2, and * the network type is 6, 9, 15, or 24, that it's AIX libpcap. * * I'm assuming those older versions of libpcap didn't * use DLT_IEEE802 for Token Ring, and didn't use DLT_SLIP_BSDOS * as that came later. It may have used DLT_PPP, however, in * which case we're out of luck; we assume it's Token Ring * in AIX libpcap rather than PPP in standard libpcap, as * you're probably more likely to be handing an AIX libpcap * token-ring capture than an old (pre-libpcap 0.4) PPP capture * to Wireshark. */ aix = FALSE; /* assume it's not AIX */ if (hdr.version_major == 2 && hdr.version_minor == 2) { switch (hdr.network) { case 6: hdr.network = 1; /* DLT_EN10MB, Ethernet */ aix = TRUE; break; case 9: hdr.network = 6; /* DLT_IEEE802, Token Ring */ aix = TRUE; break; case 15: hdr.network = 10; /* DLT_FDDI, FDDI */ aix = TRUE; break; case 24: hdr.network = 0; /* DLT_NULL, loopback */ aix = TRUE; break; } } file_encap = wtap_pcap_encap_to_wtap_encap(hdr.network); if (file_encap == WTAP_ENCAP_UNKNOWN) { *err = WTAP_ERR_UNSUPPORTED; *err_info = g_strdup_printf("pcap: network type %u unknown or unsupported", hdr.network); return WTAP_OPEN_ERROR; } /* This is a libpcap file */ libpcap = (libpcap_t *)g_malloc(sizeof(libpcap_t)); libpcap->byte_swapped = byte_swapped; libpcap->version_major = hdr.version_major; libpcap->version_minor = hdr.version_minor; wth->priv = (void *)libpcap; wth->subtype_read = libpcap_read; wth->subtype_seek_read = libpcap_seek_read; wth->file_encap = file_encap; wth->snapshot_length = hdr.snaplen; /* In file format version 2.3, the order of the "incl_len" and "orig_len" fields in the per-packet header was reversed, in order to match the BPF header layout. Therefore, in files with versions prior to that, we must swap those two fields. Unfortunately, some files were, according to a comment in the "libpcap" source, written with version 2.3 in their headers but without the interchanged fields, so if "incl_len" is greater than "orig_len" - which would make no sense - we assume that we need to swap them in version 2.3 files as well. In addition, DG/UX's tcpdump uses version 543.0, and writes the two fields in the pre-2.3 order. */ switch (hdr.version_major) { case 2: if (hdr.version_minor < 3) libpcap->lengths_swapped = SWAPPED; else if (hdr.version_minor == 3) libpcap->lengths_swapped = MAYBE_SWAPPED; else libpcap->lengths_swapped = NOT_SWAPPED; break; case 543: libpcap->lengths_swapped = SWAPPED; break; default: libpcap->lengths_swapped = NOT_SWAPPED; break; } /* * Is this AIX format? */ if (aix) { /* * Yes. Skip all the tests for other mutant formats, * and for the ERF link-layer header type, and set the * precision to nanosecond precision. */ wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAP_AIX; wth->file_tsprec = WTAP_TSPREC_NSEC; return WTAP_OPEN_MINE; } /* * No. Let's look at the header for the first record, * and see if, interpreting it as a standard header (if the * magic number was standard) or a modified header (if the * magic number was modified), the position where it says the * header for the *second* record is contains a corrupted header. * * If so, then: * * If this file had the standard magic number, it may be * an ss990417 capture file - in that version of Alexey's * patch, the packet header format was changed but the * magic number wasn't, and, alas, Red Hat appear to have * picked up that version of the patch for RH 6.1, meaning * RH 6.1 has a tcpdump that writes out files that can't * be read by any software that expects non-modified headers * if the magic number isn't the modified magic number (e.g., * any normal version of tcpdump, and Wireshark if we don't * do this gross heuristic). * * If this file had the modified magic number, it may be * an ss990915 capture file - in that version of Alexey's * patch, the magic number was changed, but the record * header had some extra fields, and, alas, SuSE appear * to have picked up that version of the patch for SuSE * 6.3, meaning that programs expecting the standard per- * packet header in captures with the modified magic number * can't read dumps from its tcpdump. * * Oh, and if it has the standard magic number, it might, instead, * be a Nokia libpcap file, so we may need to try that if * neither normal nor ss990417 headers work. */ if (modified) { /* * Well, we have the magic number from Alexey's * later two patches. Try the subtypes for that. */ subtypes = subtypes_modified; n_subtypes = N_SUBTYPES_MODIFIED; } else { if (wth->file_tsprec == WTAP_TSPREC_NSEC) { /* * We have nanosecond-format libpcap's magic * number. Try the subtypes for that. */ subtypes = subtypes_nsec; n_subtypes = N_SUBTYPES_NSEC; } else { /* * We have the regular libpcap magic number. * Try the subtypes for that. */ subtypes = subtypes_standard; n_subtypes = N_SUBTYPES_STANDARD; } } /* * Try all the subtypes. */ first_packet_offset = file_tell(wth->fh); for (i = 0; i < n_subtypes; i++) { wth->file_type_subtype = subtypes[i]; figures_of_merit[i] = libpcap_try(wth, err, err_info); if (figures_of_merit[i] == -1) { /* * Well, we couldn't even read it. * Give up. */ return WTAP_OPEN_ERROR; } if (figures_of_merit[i] == 0) { /* * This format doesn't have any issues. * Put the seek pointer back, and finish. */ if (file_seek(wth->fh, first_packet_offset, SEEK_SET, err) == -1) { return WTAP_OPEN_ERROR; } goto done; } /* * OK, we've recorded the figure of merit for this one; * go back to the first packet and try the next one. */ if (file_seek(wth->fh, first_packet_offset, SEEK_SET, err) == -1) { return WTAP_OPEN_ERROR; } } /* * OK, none are perfect; let's see which one is least bad. */ best_subtype = INT_MAX; for (i = 0; i < n_subtypes; i++) { /* * Is this subtype better than the last one we saw? */ if (figures_of_merit[i] < best_subtype) { /* * Yes. Choose it until we find a better one. */ wth->file_type_subtype = subtypes[i]; best_subtype = figures_of_merit[i]; } } done: /* * We treat a DLT_ value of 13 specially - it appears that in * Nokia libpcap format, it's some form of ATM with what I * suspect is a pseudo-header (even though Nokia's IPSO is * based on FreeBSD, which #defines DLT_SLIP_BSDOS as 13). * * If this is a Nokia capture, treat 13 as WTAP_ENCAP_ATM_PDUS, * rather than as what we normally treat it. */ if (wth->file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_PCAP_NOKIA && hdr.network == 13) wth->file_encap = WTAP_ENCAP_ATM_PDUS; if (wth->file_encap == WTAP_ENCAP_ERF) { /* * Populate set of interface IDs for ERF format. * Currently, this *has* to be done at open time. */ erf_populate_interfaces(wth); } return WTAP_OPEN_MINE; }
static int DemarshalSimpleTypes( int byte_order, tMarshalType type, void *data, const void *buffer ) { union { tInt16 i16; tUint16 ui16; tInt32 i32; tUint32 ui32; tInt64 i64; tUint64 ui64; tFloat32 f32; tFloat64 f64; } u; // NB Glib does not provide SWAP_LE_BE macro for signed types! switch( type ) { case eMtVoid: return 0; case eMtInt8: memcpy(data, buffer, sizeof(tInt8)); return sizeof(tInt8); case eMtUint8: memcpy(data, buffer, sizeof(tUint8)); return sizeof(tUint8); case eMtInt16: memcpy(&u.i16, buffer, sizeof(tInt16)); if ( G_BYTE_ORDER != byte_order ) { u.ui16 = GUINT16_SWAP_LE_BE( u.ui16 ); } memcpy(data, &u.i16, sizeof(tInt16)); return sizeof(tInt16); case eMtUint16: memcpy(&u.ui16, buffer, sizeof(tUint16)); if ( G_BYTE_ORDER != byte_order ) { u.ui16 = GUINT16_SWAP_LE_BE( u.ui16 ); } memcpy(data, &u.ui16, sizeof(tUint16)); return sizeof(tUint16); case eMtInt32: memcpy(&u.i32, buffer, sizeof(tInt32)); if ( G_BYTE_ORDER != byte_order ) { u.ui32 = GUINT32_SWAP_LE_BE( u.ui32 ); } memcpy(data, &u.i32, sizeof(tInt32)); return sizeof(tInt32); case eMtUint32: memcpy(&u.ui32, buffer, sizeof(tUint32)); if ( G_BYTE_ORDER != byte_order ) { u.ui32 = GUINT32_SWAP_LE_BE( u.ui32 ); } memcpy(data, &u.ui32, sizeof(tUint32)); return sizeof(tUint32); case eMtInt64: memcpy(&u.i64, buffer, sizeof(tInt64)); if ( G_BYTE_ORDER != byte_order ) { u.ui64 = GUINT64_SWAP_LE_BE( u.ui64 ); } memcpy(data, &u.i64, sizeof(tInt64)); return sizeof(tInt64); case eMtUint64: memcpy(&u.ui64, buffer, sizeof(tUint64)); if ( G_BYTE_ORDER != byte_order ) { u.ui64 = GUINT64_SWAP_LE_BE( u.ui64 ); } memcpy(data, &u.ui64, sizeof(tUint64)); return sizeof(tUint64); case eMtFloat32: memcpy(&u.f32, buffer, sizeof(tFloat32)); if ( G_BYTE_ORDER != byte_order ) { u.ui32 = GUINT32_SWAP_LE_BE( u.ui32 ); } memcpy(data, &u.f32, sizeof(tFloat32)); return sizeof(tFloat32); case eMtFloat64: memcpy(&u.f64, buffer, sizeof(tFloat64)); if ( G_BYTE_ORDER != byte_order ) { u.ui64 = GUINT64_SWAP_LE_BE( u.ui64 ); } memcpy(data, &u.f64, sizeof(tFloat64)); return sizeof(tFloat64); default: CRIT( "Unknown marshal type %d!", type ); return -ENOSYS; } }
static GslLong wave_handle_read (GslDataHandle *data_handle, GslLong voffset, GslLong n_values, gfloat *values) { WaveHandle *whandle = (WaveHandle*) data_handle; gpointer buffer = values; GslLong l, i, byte_offset; byte_offset = voffset * wave_format_byte_width (whandle->format); /* float offset into bytes */ byte_offset += whandle->byte_offset; switch (whandle->format) { guint8 *u8; gint8 *s8; guint16 *u16; guint32 *u32; case GSL_WAVE_FORMAT_UNSIGNED_8: u8 = buffer; u8 += n_values * 3; l = gsl_hfile_pread (whandle->hfile, byte_offset, n_values, u8); if (l < 1) return l; for (i = 0; i < l; i++) { int v = u8[i] - 128; values[i] = v * (1. / 128.); } break; case GSL_WAVE_FORMAT_SIGNED_8: s8 = buffer; s8 += n_values * 3; l = gsl_hfile_pread (whandle->hfile, byte_offset, n_values, s8); if (l < 1) return l; for (i = 0; i < l; i++) values[i] = s8[i] * (1. / 128.); break; case GSL_WAVE_FORMAT_SIGNED_12: case GSL_WAVE_FORMAT_UNSIGNED_12: case GSL_WAVE_FORMAT_SIGNED_16: case GSL_WAVE_FORMAT_UNSIGNED_16: u16 = buffer; u16 += n_values; l = gsl_hfile_pread (whandle->hfile, byte_offset, n_values << 1, u16); if (l < 2) return l < 0 ? l : 0; l >>= 1; switch (whandle->format) { case GSL_WAVE_FORMAT_UNSIGNED_16: if (whandle->byte_order != G_BYTE_ORDER) for (i = 0; i < l; i++) { int v = GUINT16_SWAP_LE_BE (u16[i]); v -= 32768; values[i] = v * (1. / 32768.); } else /* whandle->byte_order == G_BYTE_ORDER */ for (i = 0; i < l; i++) { int v = u16[i]; v -= 32768; values[i] = v * (1. / 32768.); } break; case GSL_WAVE_FORMAT_UNSIGNED_12: if (whandle->byte_order != G_BYTE_ORDER) for (i = 0; i < l; i++) { int v = GUINT16_SWAP_LE_BE (u16[i]); v &= 0x0fff; v -= 4096; values[i] = v * (1. / 4096.); } else /* whandle->byte_order == G_BYTE_ORDER */ for (i = 0; i < l; i++) { int v = u16[i]; v &= 0x0fff; v -= 4096; values[i] = v * (1. / 4096.); } break; case GSL_WAVE_FORMAT_SIGNED_16: if (whandle->byte_order != G_BYTE_ORDER) for (i = 0; i < l; i++) { gint16 v = GUINT16_SWAP_LE_BE (u16[i]); values[i] = v * (1. / 32768.); } else /* whandle->byte_order == G_BYTE_ORDER */ for (i = 0; i < l; i++) { gint16 v = u16[i]; values[i] = v * (1. / 32768.); } break; case GSL_WAVE_FORMAT_SIGNED_12: if (whandle->byte_order != G_BYTE_ORDER) for (i = 0; i < l; i++) { gint16 v = GUINT16_SWAP_LE_BE (u16[i]); values[i] = CLAMP (v, -4096, 4096) * (1. / 4096.); } else /* whandle->byte_order == G_BYTE_ORDER */ for (i = 0; i < l; i++) { gint16 v = u16[i]; values[i] = CLAMP (v, -4096, 4096) * (1. / 4096.); } break; default: g_assert_not_reached (); } break; case GSL_WAVE_FORMAT_FLOAT: u32 = buffer; l = gsl_hfile_pread (whandle->hfile, byte_offset, n_values << 2, u32); if (l < 4) return l < 0 ? l : 0; l >>= 2; if (whandle->byte_order != G_BYTE_ORDER) for (i = 0; i < l; i++) u32[i] = GUINT32_SWAP_LE_BE (u32[i]); break; default: l = -1; g_assert_not_reached (); } return l; }
char * crypto_encrypt (const char *cipher, const guint8 *data, gsize data_len, const char *iv, gsize iv_len, const char *key, gsize key_len, gsize *out_len, GError **error) { SECStatus ret; CK_MECHANISM_TYPE cipher_mech = CKM_DES3_CBC_PAD; PK11SlotInfo *slot = NULL; SECItem key_item = { .data = (unsigned char *) key, .len = key_len }; SECItem iv_item = { .data = (unsigned char *) iv, .len = iv_len }; PK11SymKey *sym_key = NULL; SECItem *sec_param = NULL; PK11Context *ctx = NULL; unsigned char *output, *padded_buf; gsize output_len; int encrypted_len = 0, i; gboolean success = FALSE; gsize padded_buf_len, pad_len; if (!crypto_init (error)) return NULL; if (!strcmp (cipher, CIPHER_DES_EDE3_CBC)) cipher_mech = CKM_DES3_CBC_PAD; else if (!strcmp (cipher, CIPHER_AES_CBC)) cipher_mech = CKM_AES_CBC_PAD; else { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_UNKNOWN_CIPHER, _("Private key cipher '%s' was unknown."), cipher); return NULL; } /* If data->len % ivlen == 0, then we add another complete block * onto the end so that the decrypter knows there's padding. */ pad_len = iv_len - (data_len % iv_len); output_len = padded_buf_len = data_len + pad_len; padded_buf = g_malloc0 (padded_buf_len); memcpy (padded_buf, data, data_len); for (i = 0; i < pad_len; i++) padded_buf[data_len + i] = (guint8) (pad_len & 0xFF); output = g_malloc0 (output_len); slot = PK11_GetBestSlot (cipher_mech, NULL); if (!slot) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_FAILED, _("Failed to initialize the encryption cipher slot.")); goto out; } sym_key = PK11_ImportSymKey (slot, cipher_mech, PK11_OriginUnwrap, CKA_ENCRYPT, &key_item, NULL); if (!sym_key) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_ENCRYPTION_FAILED, _("Failed to set symmetric key for encryption.")); goto out; } sec_param = PK11_ParamFromIV (cipher_mech, &iv_item); if (!sec_param) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_ENCRYPTION_FAILED, _("Failed to set IV for encryption.")); goto out; } ctx = PK11_CreateContextBySymKey (cipher_mech, CKA_ENCRYPT, sym_key, sec_param); if (!ctx) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_ENCRYPTION_FAILED, _("Failed to initialize the encryption context.")); goto out; } ret = PK11_CipherOp (ctx, output, &encrypted_len, output_len, padded_buf, padded_buf_len); if (ret != SECSuccess) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_ENCRYPTION_FAILED, _("Failed to encrypt: %d."), PORT_GetError ()); goto out; } if (encrypted_len != output_len) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_ENCRYPTION_FAILED, _("Unexpected amount of data after encrypting.")); goto out; } *out_len = encrypted_len; success = TRUE; out: if (ctx) PK11_DestroyContext (ctx, PR_TRUE); if (sym_key) PK11_FreeSymKey (sym_key); if (sec_param) SECITEM_FreeItem (sec_param, PR_TRUE); if (slot) PK11_FreeSlot (slot); memset (padded_buf, 0, padded_buf_len); g_free (padded_buf); if (!success) { memset (output, 0, output_len); g_free (output); output = NULL; } return (char *) output; } NMCryptoFileFormat crypto_verify_cert (const unsigned char *data, gsize len, GError **error) { CERTCertificate *cert; if (!crypto_init (error)) return NM_CRYPTO_FILE_FORMAT_UNKNOWN; /* Try DER/PEM first */ cert = CERT_DecodeCertFromPackage ((char *) data, len); if (!cert) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_INVALID_DATA, _("Couldn't decode certificate: %d"), PORT_GetError()); return NM_CRYPTO_FILE_FORMAT_UNKNOWN; } CERT_DestroyCertificate (cert); return NM_CRYPTO_FILE_FORMAT_X509; } gboolean crypto_verify_pkcs12 (const guint8 *data, gsize data_len, const char *password, GError **error) { SEC_PKCS12DecoderContext *p12ctx = NULL; SECItem pw = { 0 }; PK11SlotInfo *slot = NULL; SECStatus s; gunichar2 *ucs2_password; glong ucs2_chars = 0; #ifndef WORDS_BIGENDIAN guint16 *p; #endif /* WORDS_BIGENDIAN */ if (error) g_return_val_if_fail (*error == NULL, FALSE); if (!crypto_init (error)) return FALSE; /* PKCS#12 passwords are apparently UCS2 BIG ENDIAN, and NSS doesn't do * any conversions for us. */ if (password && *password) { if (!g_utf8_validate (password, -1, NULL)) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_INVALID_PASSWORD, _("Password must be UTF-8")); return FALSE; } ucs2_password = g_utf8_to_utf16 (password, strlen (password), NULL, &ucs2_chars, NULL); /* Can't fail if g_utf8_validate() succeeded */ g_return_val_if_fail (ucs2_password != NULL && ucs2_chars != 0, FALSE); ucs2_chars *= 2; /* convert # UCS2 characters -> bytes */ pw.data = PORT_ZAlloc(ucs2_chars + 2); memcpy (pw.data, ucs2_password, ucs2_chars); pw.len = ucs2_chars + 2; /* include terminating NULL */ memset (ucs2_password, 0, ucs2_chars); g_free (ucs2_password); #ifndef WORDS_BIGENDIAN for (p = (guint16 *) pw.data; p < (guint16 *) (pw.data + pw.len); p++) *p = GUINT16_SWAP_LE_BE (*p); #endif /* WORDS_BIGENDIAN */ } else { /* NULL password */ pw.data = NULL; pw.len = 0; } slot = PK11_GetInternalKeySlot(); p12ctx = SEC_PKCS12DecoderStart (&pw, slot, NULL, NULL, NULL, NULL, NULL, NULL); if (!p12ctx) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_FAILED, _("Couldn't initialize PKCS#12 decoder: %d"), PORT_GetError()); goto error; } s = SEC_PKCS12DecoderUpdate (p12ctx, (guint8 *)data, data_len); if (s != SECSuccess) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_INVALID_DATA, _("Couldn't decode PKCS#12 file: %d"), PORT_GetError()); goto error; } s = SEC_PKCS12DecoderVerify (p12ctx); if (s != SECSuccess) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERROR_DECRYPTION_FAILED, _("Couldn't verify PKCS#12 file: %d"), PORT_GetError()); goto error; } SEC_PKCS12DecoderFinish (p12ctx); SECITEM_ZfreeItem (&pw, PR_FALSE); return TRUE; error: if (p12ctx) SEC_PKCS12DecoderFinish (p12ctx); if (slot) PK11_FreeSlot(slot); SECITEM_ZfreeItem (&pw, PR_FALSE); return FALSE; }
int DemarshalSimpleTypes( int byte_order, tMarshalType type, void *data, const void *buffer ) { switch( type ) { case eMtVoid: return 0; case eMtUint8: case eMtInt8: { tUint8 v = *(const tUint8 *)buffer; *(tUint8 *)data = v; } return sizeof( tUint8 ); case eMtInt16: case eMtUint16: { tUint16 v; memcpy( &v, buffer, sizeof( tUint16 ) ); if ( G_BYTE_ORDER != byte_order ) v = GUINT16_SWAP_LE_BE( v ); *(tUint16 *)data = v; } return sizeof( tUint16 ); case eMtUint32: case eMtInt32: { tUint32 v; memcpy( &v, buffer, sizeof( tUint32 ) ); if ( G_BYTE_ORDER != byte_order ) v = GUINT32_SWAP_LE_BE( v ); *(tUint32 *)data = v; } return sizeof( tUint32 ); case eMtUint64: case eMtInt64: { tUint64 v; memcpy( &v, buffer, sizeof( tUint64 ) ); if ( G_BYTE_ORDER != byte_order ) v = GUINT64_SWAP_LE_BE( v ); *(tUint64 *)data = v; } return sizeof( tUint64 ); case eMtFloat32: { // this has been tested for i386 and PPC tFloat32Uint32 v; memcpy( &(v.m_f32), buffer, sizeof( tFloat32 ) ); if ( G_BYTE_ORDER != byte_order ) v.m_u32 = GUINT32_SWAP_LE_BE( v.m_u32 ); *(tFloat32 *)data = v.m_f32; } return sizeof( tFloat32 ); case eMtFloat64: { // this has been tested for i386 and PPC tFloat64Uint64 v; memcpy( &(v.m_f64), buffer, sizeof( tFloat64 ) ); if ( G_BYTE_ORDER != byte_order ) v.m_u64 = GUINT64_SWAP_LE_BE( v.m_u64 ); *(tFloat64 *)data = v.m_f64; } return sizeof( tFloat64 ); default: break; } assert( 0 ); return -1; }
static gint gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length) { GstAlsaSink *alsa; gint err; gint cptr; guint8 *ptr = data; alsa = GST_ALSA_SINK (asink); if (alsa->iec958 && alsa->need_swap) { guint i; guint16 *ptr_tmp = (guint16 *) ptr; GST_DEBUG_OBJECT (asink, "swapping bytes"); for (i = 0; i < length / 2; i++) { ptr_tmp[i] = GUINT16_SWAP_LE_BE (ptr_tmp[i]); } } GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length); cptr = length / alsa->bpf; GST_ALSA_SINK_LOCK (asink); while (cptr > 0) { /* start by doing a blocking wait for free space. Set the timeout * to 4 times the period time */ err = snd_pcm_wait (alsa->handle, (4 * alsa->period_time / 1000)); if (err < 0) { GST_DEBUG_OBJECT (asink, "wait error, %d", err); } else { GST_DELAY_SINK_LOCK (asink); err = snd_pcm_writei (alsa->handle, ptr, cptr); GST_DELAY_SINK_UNLOCK (asink); } GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr); if (err < 0) { GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err)); if (err == -EAGAIN) { continue; } else if (err == -ENODEV) { goto device_disappeared; } else if (xrun_recovery (alsa, alsa->handle, err) < 0) { goto write_error; } continue; } ptr += snd_pcm_frames_to_bytes (alsa->handle, err); cptr -= err; } GST_ALSA_SINK_UNLOCK (asink); return length - (cptr * alsa->bpf); write_error: { GST_ALSA_SINK_UNLOCK (asink); return length; /* skip one period */ } device_disappeared: { GST_ELEMENT_ERROR (asink, RESOURCE, WRITE, (_("Error outputting to audio device. " "The device has been disconnected.")), (NULL)); goto write_error; } }
wtap_open_return_val csids_open(wtap *wth, int *err, gchar **err_info) { /* There is no file header. There is only a header for each packet * so we read a packet header and compare the caplen with iplen. They * should always be equal except with the weird byteswap version. * * THIS IS BROKEN-- anytime the caplen is 0x0101 or 0x0202 up to 0x0505 * this will byteswap it. I need to fix this. XXX --mlh */ int tmp,iplen; gboolean byteswap = FALSE; struct csids_header hdr; csids_t *csids; /* check the file to make sure it is a csids file. */ if( !wtap_read_bytes( wth->fh, &hdr, sizeof( struct csids_header), err, err_info ) ) { if( *err != WTAP_ERR_SHORT_READ ) { return WTAP_OPEN_ERROR; } return WTAP_OPEN_NOT_MINE; } if( hdr.zeropad != 0 || hdr.caplen == 0 ) { return WTAP_OPEN_NOT_MINE; } hdr.seconds = pntoh32( &hdr.seconds ); hdr.caplen = pntoh16( &hdr.caplen ); if( !wtap_read_bytes( wth->fh, &tmp, 2, err, err_info ) ) { if( *err != WTAP_ERR_SHORT_READ ) { return WTAP_OPEN_ERROR; } return WTAP_OPEN_NOT_MINE; } if( !wtap_read_bytes(wth->fh, &iplen, 2, err, err_info ) ) { if( *err != WTAP_ERR_SHORT_READ ) { return WTAP_OPEN_ERROR; } return WTAP_OPEN_NOT_MINE; } iplen = pntoh16(&iplen); if ( iplen == 0 ) return WTAP_OPEN_NOT_MINE; /* if iplen and hdr.caplen are equal, default to no byteswap. */ if( iplen > hdr.caplen ) { /* maybe this is just a byteswapped version. the iplen ipflags */ /* and ipid are swapped. We cannot use the normal swaps because */ /* we don't know the host */ iplen = GUINT16_SWAP_LE_BE(iplen); if( iplen <= hdr.caplen ) { /* we know this format */ byteswap = TRUE; } else { /* don't know this one */ return WTAP_OPEN_NOT_MINE; } } else { byteswap = FALSE; } /* no file header. So reset the fh to 0 so we can read the first packet */ if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) return WTAP_OPEN_ERROR; csids = (csids_t *)g_malloc(sizeof(csids_t)); wth->priv = (void *)csids; csids->byteswapped = byteswap; wth->file_encap = WTAP_ENCAP_RAW_IP; wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_CSIDS; wth->snapshot_length = 0; /* not known */ wth->subtype_read = csids_read; wth->subtype_seek_read = csids_seek_read; wth->file_tsprec = WTAP_TSPREC_SEC; return WTAP_OPEN_MINE; }
static void swap_endian(guint16 *data, int length) { int i; for (i = 0; i < length; i += 2, data++) *data = GUINT16_SWAP_LE_BE(*data); }
int main (int argc, char *argv[]) { GList *list, *t; GSList *slist, *st; GHashTable *hash_table; GMemChunk *mem_chunk; GStringChunk *string_chunk; GTimer *timer; gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6}; gchar *string; gchar *mem[10000], *tmp_string = NULL, *tmp_string_2; gint i, j; GArray *garray; GPtrArray *gparray; GByteArray *gbarray; GString *string1, *string2; GTree *tree; char chars[62]; GRelation *relation; GTuples *tuples; gint data [1024]; struct { gchar *filename; gchar *dirname; } dirname_checks[] = { #ifndef NATIVE_WIN32 { "/", "/" }, { "////", "/" }, { ".////", "." }, { ".", "." }, { "..", "." }, { "../", ".." }, { "..////", ".." }, { "", "." }, { "a/b", "a" }, { "a/b/", "a/b" }, { "c///", "c" }, #else { "\\", "\\" }, { ".\\\\\\\\", "." }, { ".", "." }, { "..", "." }, { "..\\", ".." }, { "..\\\\\\\\", ".." }, { "", "." }, { "a\\b", "a" }, { "a\\b\\", "a\\b" }, { "c\\\\\\", "c" }, #endif }; guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]); guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U; guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U; #ifdef G_HAVE_GINT64 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U), gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU); #endif g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n", glib_major_version, glib_minor_version, glib_micro_version, glib_interface_age, glib_binary_age); string = g_get_current_dir (); g_print ("cwd: %s\n", string); g_free (string); g_print ("user: %s\n", g_get_user_name ()); g_print ("real: %s\n", g_get_real_name ()); g_print ("home: %s\n", g_get_home_dir ()); g_print ("tmp-dir: %s\n", g_get_tmp_dir ()); /* type sizes */ g_print ("checking size of gint8: %d", (int)sizeof (gint8)); TEST (NULL, sizeof (gint8) == 1); g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16)); TEST (NULL, sizeof (gint16) == 2); g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32)); TEST (NULL, sizeof (gint32) == 4); #ifdef G_HAVE_GINT64 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64)); TEST (NULL, sizeof (gint64) == 8); #endif /* G_HAVE_GINT64 */ g_print ("\n"); g_print ("checking g_dirname()..."); for (i = 0; i < n_dirname_checks; i++) { gchar *dirname; dirname = g_dirname (dirname_checks[i].filename); if (strcmp (dirname, dirname_checks[i].dirname) != 0) { g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n", dirname_checks[i].filename, dirname_checks[i].dirname, dirname); n_dirname_checks = 0; } g_free (dirname); } if (n_dirname_checks) g_print ("ok\n"); g_print ("checking doubly linked lists..."); list = NULL; for (i = 0; i < 10; i++) list = g_list_append (list, &nums[i]); list = g_list_reverse (list); for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != (9 - i)) g_error ("Regular insert failed"); } for (i = 0; i < 10; i++) if(g_list_position(list, g_list_nth (list, i)) != i) g_error("g_list_position does not seem to be the inverse of g_list_nth\n"); g_list_free (list); list = NULL; for (i = 0; i < 10; i++) list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one); /* g_print("\n"); g_list_foreach (list, my_list_print, NULL); */ for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != i) g_error ("Sorted insert failed"); } g_list_free (list); list = NULL; for (i = 0; i < 10; i++) list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two); /* g_print("\n"); g_list_foreach (list, my_list_print, NULL); */ for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != (9 - i)) g_error ("Sorted insert failed"); } g_list_free (list); list = NULL; for (i = 0; i < 10; i++) list = g_list_prepend (list, &morenums[i]); list = g_list_sort (list, my_list_compare_two); /* g_print("\n"); g_list_foreach (list, my_list_print, NULL); */ for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != (9 - i)) g_error ("Merge sort failed"); } g_list_free (list); g_print ("ok\n"); g_print ("checking singly linked lists..."); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_append (slist, &nums[i]); slist = g_slist_reverse (slist); for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != (9 - i)) g_error ("failed"); } g_slist_free (slist); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one); /* g_print("\n"); g_slist_foreach (slist, my_list_print, NULL); */ for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != i) g_error ("Sorted insert failed"); } g_slist_free(slist); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two); /* g_print("\n"); g_slist_foreach (slist, my_list_print, NULL); */ for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != (9 - i)) g_error("Sorted insert failed"); } g_slist_free(slist); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_prepend (slist, &morenums[i]); slist = g_slist_sort (slist, my_list_compare_two); /* g_print("\n"); g_slist_foreach (slist, my_list_print, NULL); */ for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != (9 - i)) g_error("Sorted insert failed"); } g_slist_free(slist); g_print ("ok\n"); g_print ("checking binary trees...\n"); tree = g_tree_new (my_compare); i = 0; for (j = 0; j < 10; j++, i++) { chars[i] = '0' + j; g_tree_insert (tree, &chars[i], &chars[i]); } for (j = 0; j < 26; j++, i++) { chars[i] = 'A' + j; g_tree_insert (tree, &chars[i], &chars[i]); } for (j = 0; j < 26; j++, i++) { chars[i] = 'a' + j; g_tree_insert (tree, &chars[i], &chars[i]); } g_print ("tree height: %d\n", g_tree_height (tree)); g_print ("tree nnodes: %d\n", g_tree_nnodes (tree)); g_print ("tree: "); g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL); g_print ("\n"); for (i = 0; i < 10; i++) g_tree_remove (tree, &chars[i]); g_print ("tree height: %d\n", g_tree_height (tree)); g_print ("tree nnodes: %d\n", g_tree_nnodes (tree)); g_print ("tree: "); g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL); g_print ("\n"); g_print ("ok\n"); /* check n-way trees */ g_node_test (); g_print ("checking mem chunks..."); mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE); for (i = 0; i < 10000; i++) { mem[i] = g_chunk_new (gchar, mem_chunk); for (j = 0; j < 50; j++) mem[i][j] = i * j; } for (i = 0; i < 10000; i++) { g_mem_chunk_free (mem_chunk, mem[i]); } g_print ("ok\n"); g_print ("checking hash tables..."); hash_table = g_hash_table_new (my_hash, my_hash_compare); for (i = 0; i < 10000; i++) { array[i] = i; g_hash_table_insert (hash_table, &array[i], &array[i]); } g_hash_table_foreach (hash_table, my_hash_callback, NULL); for (i = 0; i < 10000; i++) if (array[i] == 0) g_print ("%d\n", i); for (i = 0; i < 10000; i++) g_hash_table_remove (hash_table, &array[i]); for (i = 0; i < 10000; i++) { array[i] = i; g_hash_table_insert (hash_table, &array[i], &array[i]); } if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 || g_hash_table_size (hash_table) != 5000) g_print ("bad!\n"); g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL); g_hash_table_destroy (hash_table); g_print ("ok\n"); g_print ("checking string chunks..."); string_chunk = g_string_chunk_new (1024); for (i = 0; i < 100000; i ++) { tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); if (strcmp ("hi pete", tmp_string) != 0) g_error ("string chunks are broken.\n"); } tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 != tmp_string && strcmp(tmp_string_2, tmp_string) == 0); tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 == tmp_string); g_string_chunk_free (string_chunk); g_print ("ok\n"); g_print ("checking arrays..."); garray = g_array_new (FALSE, FALSE, sizeof (gint)); for (i = 0; i < 10000; i++) g_array_append_val (garray, i); for (i = 0; i < 10000; i++) if (g_array_index (garray, gint, i) != i) g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i); g_array_free (garray, TRUE); garray = g_array_new (FALSE, FALSE, sizeof (gint)); for (i = 0; i < 100; i++) g_array_prepend_val (garray, i); for (i = 0; i < 100; i++) if (g_array_index (garray, gint, i) != (100 - i - 1)) g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1); g_array_free (garray, TRUE); g_print ("ok\n"); g_print ("checking strings..."); string1 = g_string_new ("hi pete!"); string2 = g_string_new (""); g_assert (strcmp ("hi pete!", string1->str) == 0); for (i = 0; i < 10000; i++) g_string_append_c (string1, 'a'+(i%26)); #if !(defined (_MSC_VER) || defined (__LCC__)) /* MSVC and LCC use the same run-time C library, which doesn't like the %10000.10000f format... */ g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #else g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #endif g_print ("string2 length = %d...\n", string2->len); string2->str[70] = '\0'; g_print ("first 70 chars:\n%s\n", string2->str); string2->str[141] = '\0'; g_print ("next 70 chars:\n%s\n", string2->str+71); string2->str[212] = '\0'; g_print ("and next 70:\n%s\n", string2->str+142); g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70); g_print ("ok\n"); g_print ("checking timers...\n"); timer = g_timer_new (); g_print (" spinning for 3 seconds...\n"); g_timer_start (timer); while (g_timer_elapsed (timer, NULL) < 3) ; g_timer_stop (timer); g_timer_destroy (timer); g_print ("ok\n"); g_print ("checking g_strcasecmp..."); g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0); g_assert (g_strcasecmp ("frobozz", "frobozz") == 0); g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0); g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0); g_assert (g_strcasecmp ("", "") == 0); g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0); g_assert (g_strcasecmp ("a", "b") < 0); g_assert (g_strcasecmp ("a", "B") < 0); g_assert (g_strcasecmp ("A", "b") < 0); g_assert (g_strcasecmp ("A", "B") < 0); g_assert (g_strcasecmp ("b", "a") > 0); g_assert (g_strcasecmp ("b", "A") > 0); g_assert (g_strcasecmp ("B", "a") > 0); g_assert (g_strcasecmp ("B", "A") > 0); g_print ("ok\n"); g_print ("checking g_strdup..."); g_assert(g_strdup(NULL) == NULL); string = g_strdup(GLIB_TEST_STRING); g_assert(string != NULL); g_assert(strcmp(string, GLIB_TEST_STRING) == 0); g_free(string); g_print ("ok\n"); g_print ("checking g_strconcat..."); string = g_strconcat(GLIB_TEST_STRING, NULL); g_assert(string != NULL); g_assert(strcmp(string, GLIB_TEST_STRING) == 0); g_free(string); string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, GLIB_TEST_STRING, NULL); g_assert(string != NULL); g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING) == 0); g_free(string); g_print ("ok\n"); g_print ("checking g_strdup_printf..."); string = g_strdup_printf ("%05d %-5s", 21, "test"); g_assert (string != NULL); g_assert (strcmp(string, "00021 test ") == 0); g_free (string); g_print ("ok\n"); /* g_debug (argv[0]); */ /* Relation tests */ g_print ("checking relations..."); relation = g_relation_new (2); g_relation_index (relation, 0, g_int_hash, g_int_equal); g_relation_index (relation, 1, g_int_hash, g_int_equal); for (i = 0; i < 1024; i += 1) data[i] = i; for (i = 1; i < 1023; i += 1) { g_relation_insert (relation, data + i, data + i + 1); g_relation_insert (relation, data + i, data + i - 1); } for (i = 2; i < 1022; i += 1) { g_assert (! g_relation_exists (relation, data + i, data + i)); g_assert (! g_relation_exists (relation, data + i, data + i + 2)); g_assert (! g_relation_exists (relation, data + i, data + i - 2)); } for (i = 1; i < 1023; i += 1) { g_assert (g_relation_exists (relation, data + i, data + i + 1)); g_assert (g_relation_exists (relation, data + i, data + i - 1)); } for (i = 2; i < 1022; i += 1) { g_assert (g_relation_count (relation, data + i, 0) == 2); g_assert (g_relation_count (relation, data + i, 1) == 2); } g_assert (g_relation_count (relation, data, 0) == 0); g_assert (g_relation_count (relation, data + 42, 0) == 2); g_assert (g_relation_count (relation, data + 43, 1) == 2); g_assert (g_relation_count (relation, data + 41, 1) == 2); g_relation_delete (relation, data + 42, 0); g_assert (g_relation_count (relation, data + 42, 0) == 0); g_assert (g_relation_count (relation, data + 43, 1) == 1); g_assert (g_relation_count (relation, data + 41, 1) == 1); tuples = g_relation_select (relation, data + 200, 0); g_assert (tuples->len == 2); #if 0 for (i = 0; i < tuples->len; i += 1) { printf ("%d %d\n", *(gint*) g_tuples_index (tuples, i, 0), *(gint*) g_tuples_index (tuples, i, 1)); } #endif g_assert (g_relation_exists (relation, data + 300, data + 301)); g_relation_delete (relation, data + 300, 0); g_assert (!g_relation_exists (relation, data + 300, data + 301)); g_tuples_destroy (tuples); g_relation_destroy (relation); relation = NULL; g_print ("ok\n"); g_print ("checking pointer arrays..."); gparray = g_ptr_array_new (); for (i = 0; i < 10000; i++) g_ptr_array_add (gparray, GINT_TO_POINTER (i)); for (i = 0; i < 10000; i++) if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i)) g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i)); g_ptr_array_free (gparray, TRUE); g_print ("ok\n"); g_print ("checking byte arrays..."); gbarray = g_byte_array_new (); for (i = 0; i < 10000; i++) g_byte_array_append (gbarray, (guint8*) "abcd", 4); for (i = 0; i < 10000; i++) { g_assert (gbarray->data[4*i] == 'a'); g_assert (gbarray->data[4*i+1] == 'b'); g_assert (gbarray->data[4*i+2] == 'c'); g_assert (gbarray->data[4*i+3] == 'd'); } g_byte_array_free (gbarray, TRUE); g_print ("ok\n"); g_printerr ("g_log tests:"); g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345); g_message ("the next warning is a test:"); string = NULL; g_print (string); g_print ("checking endian macros (host is "); #if G_BYTE_ORDER == G_BIG_ENDIAN g_print ("big endian)..."); #else g_print ("little endian)..."); #endif g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2); g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2); #ifdef G_HAVE_GINT64 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2); #endif g_print ("ok\n"); return 0; }