Index: head/Mk/Uses/gecko.mk =================================================================== --- head/Mk/Uses/gecko.mk (revision 472726) +++ head/Mk/Uses/gecko.mk (revision 472727) @@ -1,127 +1,127 @@ # $FreeBSD$ # # Handle dependency of different gecko based applications # # MAINTAINER: gecko@FreeBSD.org # # Feature: gecko # Usage: USES=gecko or USES=gecko:ARGS # Valid ARGS: libxul, firefox, seamonkey, thunderbird # in case the first argument is not libxul # The following arguments are available # - build: also add the dependency as a build # dependency # - [0-9][0-9][+]?: a version optionnally # followed by a + # .if !defined(_INCLUDE_USES_GECKO_MK) _INCLUDE_USES_GECKO_MK= yes .if empty(gecko_ARGS) gecko_ARGS= libxul .endif _GECKO_VERSION= ${gecko_ARGS:M[0-9][0-9]*} .if ${gecko_ARGS:Mlibxul} # Compat with older versions GECKO= libxul GECKO_CONFING?= ${LOCALBASE}/bin/${GECKO}-config XPIDL?= ${LOCALBASE}/lib/${GECKO}/xpidl XPIDL_INCL?= `${GECKO_CONFIG} --idlflags` BUILD_DEPENDS+= libxul>=45:www/libxul RUN_DEPENDS+= libxul>=45:www/libxul .elif ${gecko_ARGS:Mfirefox} _GECKO_DEFAULT_VERSION= 52 -_GECKO_VERSIONS= 52 60 +_GECKO_VERSIONS= 52 61 _GECKO_TYPE= firefox # Dependence lines for different Firefox versions 52_DEPENDS= ${LOCALBASE}/lib/firefox/firefox:www/firefox-esr -60_DEPENDS= ${LOCALBASE}/lib/firefox/firefox:www/firefox +61_DEPENDS= ${LOCALBASE}/lib/firefox/firefox:www/firefox .if exists(${LOCALBASE}/bin/firefox) _GECKO_INSTALLED_VER!= ${PKG_QUERY} %v firefox firefox-esr _GECKO_INSTALLED_VER:= ${_GECKO_INSTALLED_VER:C/\..*//} .endif .elif ${gecko_ARGS:Mseamonkey} _GECKO_DEFAULT_VERSION= 49 _GECKO_VERSIONS= 49 _GECKO_TYPE= seamonkey .if exists(${LOCALBASE}/bin/seamonkey) _GECKO_INSTALLED_VER!= ${PKG_QUERY} %v seamonkey _GECKO_INSTALLED_VER:= ${_GECKO_INSTALLED_VER:C/[0-9]*\.//:C/\..*//} .endif # Dependence lines for different Seamonkey versions 49_DEPENDS= ${LOCALBASE}/lib/seamonkey/seamonkey:www/seamonkey .elif ${gecko_ARGS:Mthunderbird} _GECKO_DEFAULT_VERSION= 52 _GECKO_VERSIONS= 52 _GECKO_TYPE= thunderbird .if exists(${LOCALBASE}/bin/thunderbird) _GECKO_INSTALLED_VER!= ${PKG_QUERY} %v thunderbird _GECKO_INSTALLED_VER:= ${_GECKO_INSTALLED_VER:C/\..*//} .endif # Dependence lines for different Thunderbird versions 52_DEPENDS= ${LOCALBASE}/lib/thunderbird/thunderbird:mail/thunderbird .else IGNORE= Unknown type of gecko dependency you may specify either libxul, firefox, seamonkey or thunderbird .endif .if defined(_GECKO_TYPE) .if ${_GECKO_VERSION:M*+} _GECKO_MIN_VERSION:= ${_GECKO_VERSION:S/+//} _GECKO_WANTED_VERSIONS:= ${_GECKO_DEFAULT_VERSION} .endif .if ${_GECKO_VERSION:M[0-9][0-9]} _GECKO_WANTED_VERSIONS:= ${_GECKO_VERSION:M[0-9][0-9]} .endif _GECKO_WANTED_VERSIONS?= ${_GECKO_DEFAULT_VERSION} .if defined(_GECKO_MIN_VERSION) . for _v in ${_GECKO_VERSIONS} . if ${_GECKO_MIN_VERSION} <= ${_v} _GECKO_WANTED_VERSIONS+= ${_v} . endif . endfor .endif .for _v in ${_GECKO_WANTED_VERSIONS:O:u} _GECKO_HIGHEST_VERSION:= ${_v} .if defined(_GECKO_INSTALLED_VER) && ${_GECKO_INSTALLED_VER} == ${_v} _GECKO_WANTED_VERSION:= ${_v} .endif .endfor .if !defined(_GECKO_WANTED_VERSION) .if defined(_GECKO_INSTALLED_VER) IGNORE= cannot install: ${_GECKO_TYPE} versions mismatch: ${_GECKO_TYPE}-${_GECKO_INSTALLED_VER} is installed and wanted version is ${_GECKO_TYPE}-${_GECKO_VERSION:M[0-9][0-9]} .else _GECKO_WANTED_VERSION:= ${_GECKO_HIGHEST_VERSION} .endif .endif .if ${gecko_ARGS:Mbuild} BUILD_DEPENDS+= ${${_GECKO_WANTED_VERSION}_DEPENDS} .endif RUN_DEPENDS+= ${${_GECKO_WANTED_VERSION}_DEPENDS} .endif .endif Index: head/Mk/bsd.gecko.mk =================================================================== --- head/Mk/bsd.gecko.mk (revision 472726) +++ head/Mk/bsd.gecko.mk (revision 472727) @@ -1,590 +1,595 @@ #-*- tab-width: 4; -*- # ex:ts=4 # # Date created: 12 Nov 2005 # Whom: Michael Johnson # # $FreeBSD$ # # 4 column tabs prevent hair loss and tooth decay! # bsd.gecko.mk abstracts the selection of gecko-based backends. It allows users # and porters to support any available gecko backend without needing to build # many conditional tests. ${USE_GECKO} is the list of backends that your port # can handle, and ${GECKO} is set by bsd.gecko.mk to be the chosen backend. # Users set ${WITH_GECKO} to the list of gecko backends they want on their # system. .if defined(USE_GECKO) .if !defined(_POSTMKINCLUDED) && !defined(Gecko_Pre_Include) Gecko_Pre_Include= bsd.gecko.mk # This file contains some reusable components for mozilla ports. It's of # use primarily to apps from the mozilla project itself (such as Firefox, # Thunderbird, etc.), and probably won't be of use for gecko-based ports # like epiphany, galeon, etc. # # You need to make sure to add USE_GECKO=gecko to for your port can uses # one of these options below. # # Ports can use the following: # # USE_MOZILLA By default, it enables every system dependency # listed in '_ALL_DEPENDS'. If your port doesn't # need one of those then you can use '-' like # 'USE_MOZILLA= -png -vpx' to subtract the # dependencies. Experimental deps use '+' like # 'USE_MOZILLA= +speex +theora'. # # MOZILLA_PLIST_DIRS List of directories to descend into when installing # and creating the plist # # MOZ_PIS_SCRIPTS List of scripts residing in ${FILESDIR} to be # filtered through MOZCONFIG_SED and installed along # with our Pluggable Init Scripts (PIS) # # MOZ_SED_ARGS sed(1) commands through which MOZ_PIS_SCRIPTS are # filtered. There is a default set defined here, so # you probably want to add to MOZ_SED_ARGS rather # than clobber it # # MOZ_OPTIONS configure arguments (added to .mozconfig). If # NOMOZCONFIG is defined, you probably want to set # CONFIGURE_ARGS+=${MOZ_OPTIONS} # # MOZ_MK_OPTIONS The make(1) arguments (added to .mozconfig). If # NOMOZCONFIG is defined, you probably want to set # MAKE_ARGS+=${MOZ_MK_OPTIONS} # # MOZ_EXPORT Environment variables for the build process (added # to .mozconfig). If NOMOZCONFIG is defined, you # probably want to set MAKE_ENV+=${MOZ_EXPORT} # # MOZ_CHROME A variable for the --enable-chrome-format= in # CONFIGURE_ARGS. The default is omni. # # MOZ_TOOLKIT A variable for the --enable-default-toolkit= in # CONFIGURE_ARGS. The default is cairo-gtk2. # # PORT_MOZCONFIG Defaults to ${FILESDIR}/mozconfig.in, but can be # set to a generic mozconfig included with the port # # NOMOZCONFIG Don't drop a customized .mozconfig into the build # directory. Options will have to be specified in # CONFIGURE_ARGS instead # MAINTAINER?= gecko@FreeBSD.org MOZILLA?= ${PORTNAME} MOZILLA_VER?= ${PORTVERSION} MOZILLA_BIN?= ${PORTNAME}-bin MOZILLA_EXEC_NAME?=${MOZILLA} MOZ_RPATH?= ${MOZILLA} USES+= cpe gmake iconv localbase perl5 pkgconfig \ python:2.7,build desktop-file-utils CPE_VENDOR?=mozilla USE_PERL5= build USE_XORG= x11 xcomposite xdamage xext xfixes xrender xt HAS_CONFIGURE= yes CONFIGURE_OUTSOURCE= yes .if ${MOZILLA} != "libxul" BUNDLE_LIBS= yes .endif .if ${MOZILLA_VER:R:R} >= 49 USES+= compiler:c++14-lang CPPFLAGS+= -D_GLIBCXX_USE_C99 -D_GLIBCXX_USE_C99_MATH_TR1 \ -D_DECLARE_C99_LDBL_MATH # XXX ports/193528 .else USES+= compiler:c++11-lang .endif .if ${MOZILLA_VER:R:R} >= 50 USE_XORG+= xcb .endif .if ${MOZILLA_VER:R:R} >= 56 MESA_LLVM_VER?= 60 BUILD_DEPENDS+= llvm${MESA_LLVM_VER}>0:devel/llvm${MESA_LLVM_VER} MOZ_EXPORT+= LLVM_CONFIG=llvm-config${MESA_LLVM_VER} .endif +.if ${MOZILLA_VER:R:R} >= 61 +BUILD_DEPENDS+= ${LOCALBASE}/bin/python${PYTHON3_DEFAULT}:lang/python${PYTHON3_DEFAULT:S/.//g} +MOZ_EXPORT+= PYTHON3="${LOCALBASE}/bin/python${PYTHON3_DEFAULT}" +.endif + .if ${OPSYS} == FreeBSD && ${OSREL} == 11.1 LLD_UNSAFE= yes .endif MOZILLA_SUFX?= none MOZSRC?= ${WRKSRC} PLISTF?= ${WRKDIR}/plist_files MOZ_PIS_DIR?= lib/${MOZILLA}/init.d PORT_MOZCONFIG?= ${FILESDIR}/mozconfig.in MOZCONFIG?= ${WRKSRC}/.mozconfig MOZILLA_PLIST_DIRS?= bin lib share/pixmaps share/applications PKGINSTALL?= ${WRKDIR}/pkg-install PKGDEINSTALL?= ${WRKDIR}/pkg-deinstall PKGINSTALL_INC?= ${.CURDIR}/../../www/firefox/files/pkg-install.in PKGDEINSTALL_INC?= ${.CURDIR}/../../www/firefox/files/pkg-deinstall.in MOZ_PKGCONFIG_FILES?= ${MOZILLA}-gtkmozembed ${MOZILLA}-js \ ${MOZILLA}-xpcom ${MOZILLA}-plugin MOZ_EXPORT+= ${CONFIGURE_ENV} \ RUSTFLAGS="${RUSTFLAGS}" \ PERL="${PERL}" MOZ_OPTIONS+= --prefix="${PREFIX}" MOZ_MK_OPTIONS+=MOZ_OBJDIR="${BUILD_WRKSRC}" LDFLAGS+= -Wl,--as-needed # Adjust -C target-cpu if -march/-mcpu is set by bsd.cpu.mk .if ${ARCH} == amd64 || ${ARCH} == i386 RUSTFLAGS+= ${CFLAGS:M-march=*:S/-march=/-C target-cpu=/} .else RUSTFLAGS+= ${CFLAGS:M-mcpu=*:S/-mcpu=/-C target-cpu=/} .endif .if ${MOZILLA_VER:R:R} < 55 && ${OPSYS} == FreeBSD && ${OSVERSION} < 1200032 # use jemalloc 3.0.0 (4.0 for firefox 43+) API for stats/tuning MOZ_EXPORT+= MOZ_JEMALLOC4=1 .if ${MOZILLA_VER:R:R} >= 48 MOZ_OPTIONS+= --enable-jemalloc=4 .elif ${OSVERSION} < 1100079 MOZ_OPTIONS+= --enable-jemalloc .endif # Mozilla >= 48 .endif # Mozilla < 55 # Standard depends _ALL_DEPENDS= cairo event ffi graphite harfbuzz hunspell icu jpeg nspr nss png pixman soundtouch sqlite vpx .if ${PORT_OPTIONS:MINTEGER_SAMPLES} MOZ_EXPORT+= MOZ_INTEGER_SAMPLES=1 _ALL_DEPENDS+= tremor .else _ALL_DEPENDS+= vorbis .endif .if ! ${PORT_OPTIONS:MBUNDLED_CAIRO} cairo_BUILD_DEPENDS=cairo>=1.12.16_1,2:graphics/cairo cairo_LIB_DEPENDS= libcairo.so:graphics/cairo cairo_MOZ_OPTIONS= --enable-system-cairo .endif event_LIB_DEPENDS= libevent.so:devel/libevent event_MOZ_OPTIONS= --with-system-libevent ffi_LIB_DEPENDS= libffi.so:devel/libffi ffi_MOZ_OPTIONS= --enable-system-ffi .if exists(${FILESDIR}/patch-bug847568) graphite_LIB_DEPENDS= libgraphite2.so:graphics/graphite2 graphite_MOZ_OPTIONS= --with-system-graphite2 harfbuzz_LIB_DEPENDS= libharfbuzz.so:print/harfbuzz harfbuzz_MOZ_OPTIONS= --with-system-harfbuzz .endif hunspell_LIB_DEPENDS= libhunspell-1.6.so:textproc/hunspell hunspell_MOZ_OPTIONS= --enable-system-hunspell icu_LIB_DEPENDS= libicui18n.so:devel/icu icu_MOZ_OPTIONS= --with-system-icu --with-intl-api -jpeg_BUILD_DEPENDS=yasm:devel/yasm # XXX Remove files/patch-ijg-libjpeg once -turbo is default jpeg_USES= jpeg jpeg_MOZ_OPTIONS= --with-system-jpeg=${LOCALBASE} nspr_LIB_DEPENDS= libnspr4.so:devel/nspr nspr_MOZ_OPTIONS= --with-system-nspr nss_LIB_DEPENDS= libnss3.so:security/nss nss_MOZ_OPTIONS= --with-system-nss pixman_LIB_DEPENDS= libpixman-1.so:x11/pixman pixman_MOZ_OPTIONS= --enable-system-pixman png_LIB_DEPENDS= libpng.so:graphics/png png_MOZ_OPTIONS= --with-system-png=${LOCALBASE} .if exists(${FILESDIR}/patch-z-bug517422) soundtouch_LIB_DEPENDS= libSoundTouch.so:audio/soundtouch soundtouch_MOZ_OPTIONS= --with-system-soundtouch .endif sqlite_LIB_DEPENDS= libsqlite3.so:databases/sqlite3 sqlite_MOZ_OPTIONS= --enable-system-sqlite .if exists(${FILESDIR}/patch-z-bug517422) # XXX disabled: update to 1.2.x or review backported fixes theora_LIB_DEPENDS= libtheora.so:multimedia/libtheora theora_MOZ_OPTIONS= --with-system-theora tremor_LIB_DEPENDS= libogg.so:audio/libogg libvorbisidec.so:audio/libtremor tremor_MOZ_OPTIONS= --with-system-tremor --with-system-ogg vorbis_LIB_DEPENDS= libogg.so:audio/libogg libvorbis.so:audio/libvorbis vorbis_MOZ_OPTIONS= --with-system-vorbis --with-system-ogg .endif -vpx_BUILD_DEPENDS= yasm:devel/yasm vpx_LIB_DEPENDS= libvpx.so:multimedia/libvpx vpx_MOZ_OPTIONS= --with-system-libvpx .for use in ${USE_MOZILLA} ${use:S/-/_WITHOUT_/}= ${TRUE} .endfor LIB_DEPENDS+= libfontconfig.so:x11-fonts/fontconfig \ libfreetype.so:print/freetype2 .for dep in ${_ALL_DEPENDS} ${USE_MOZILLA:M+*:S/+//} .if !defined(_WITHOUT_${dep}) BUILD_DEPENDS+= ${${dep}_BUILD_DEPENDS} LIB_DEPENDS+= ${${dep}_LIB_DEPENDS} RUN_DEPENDS+= ${${dep}_RUN_DEPENDS} USES+= ${${dep}_USES} MOZ_OPTIONS+= ${${dep}_MOZ_OPTIONS} .else BUILD_DEPENDS+= ${-${dep}_BUILD_DEPENDS} .endif .endfor # Standard options MOZ_CHROME?= omni MOZ_TOOLKIT?= cairo-gtk3 MOZ_CHANNEL?= ${PKGNAMESUFFIX:Urelease:S/^-//} MOZ_OPTIONS+= \ --enable-chrome-format=${MOZ_CHROME} \ --enable-default-toolkit=${MOZ_TOOLKIT} \ --enable-update-channel=${MOZ_CHANNEL} \ --disable-updater \ --enable-pie \ --with-pthreads # others MOZ_OPTIONS+= --with-system-zlib \ --with-system-bz2 # API keys from www/chromium # http://www.chromium.org/developers/how-tos/api-keys # Note: these are for FreeBSD use ONLY. For your own distribution, # please get your own set of keys. MOZ_EXPORT+= MOZ_GOOGLE_API_KEY=AIzaSyBsp9n41JLW8jCokwn7vhoaMejDFRd1mp8 .if ${PORT_OPTIONS:MGTK2} MOZ_TOOLKIT= cairo-gtk2 .endif .if ${MOZ_TOOLKIT:Mcairo-gtk3} BUILD_DEPENDS+= gtk3>=3.14.6:x11-toolkits/gtk30 USE_GNOME+= gdkpixbuf2 gtk20 gtk30 .else # gtk2, cairo-gtk2 USE_GNOME+= gdkpixbuf2 gtk20 .endif .if ${PORT_OPTIONS:MOPTIMIZED_CFLAGS} CFLAGS+= -O3 MOZ_EXPORT+= MOZ_OPTIMIZE_FLAGS="${CFLAGS:M-O*}" MOZ_OPTIONS+= --enable-optimize .else MOZ_OPTIONS+= --disable-optimize . if ${MOZILLA_VER:R:R} >= 56 . if ${/usr/bin/ld:L:tA} != /usr/bin/ld.lld # ld 2.17 barfs on Stylo built with -C opt-level=0 USE_BINUTILS= yes LDFLAGS+= -B${LOCALBASE}/bin . endif . endif .endif .if ${PORT_OPTIONS:MCANBERRA} RUN_DEPENDS+= libcanberra>0:audio/libcanberra .endif .if ${PORT_OPTIONS:MDBUS} BUILD_DEPENDS+= libnotify>0:devel/libnotify LIB_DEPENDS+= libdbus-1.so:devel/dbus \ libdbus-glib-1.so:devel/dbus-glib \ libstartup-notification-1.so:x11/startup-notification MOZ_OPTIONS+= --enable-startup-notification .else MOZ_OPTIONS+= --disable-dbus .endif .if ${PORT_OPTIONS:MFFMPEG} # dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp RUN_DEPENDS+= ffmpeg>=0.8,1:multimedia/ffmpeg .endif .if ${MOZILLA_VER:R:R} < 46 MOZ_OPTIONS+= --disable-gstreamer .endif .if ${PORT_OPTIONS:MGCONF} USE_GNOME+= gconf2 MOZ_OPTIONS+= --enable-gconf .else MOZ_OPTIONS+= --disable-gconf .endif .if ${PORT_OPTIONS:MLIBPROXY} LIB_DEPENDS+= libproxy.so:net/libproxy MOZ_OPTIONS+= --enable-libproxy .else MOZ_OPTIONS+= --disable-libproxy .endif .if ${PORT_OPTIONS:MALSA} LIB_DEPENDS+= libasound.so:audio/alsa-lib RUN_DEPENDS+= ${LOCALBASE}/lib/alsa-lib/libasound_module_pcm_oss.so:audio/alsa-plugins RUN_DEPENDS+= alsa-lib>=1.1.1_1:audio/alsa-lib MOZ_OPTIONS+= --enable-alsa .endif .if ${PORT_OPTIONS:MJACK} BUILD_DEPENDS+= ${LOCALBASE}/include/jack/jack.h:audio/jack MOZ_OPTIONS+= --enable-jack .endif .if ${PORT_OPTIONS:MPULSEAUDIO} BUILD_DEPENDS+= ${LOCALBASE}/include/pulse/pulseaudio.h:audio/pulseaudio MOZ_OPTIONS+= --enable-pulseaudio .else MOZ_OPTIONS+= --disable-pulseaudio .endif .if ${PORT_OPTIONS:MSNDIO} LIB_DEPENDS+= libsndio.so:audio/sndio post-patch-SNDIO-on: @${REINPLACE_CMD} -e 's|OpenBSD|${OPSYS}|g' \ ${MOZSRC}/media/libcubeb/src/moz.build \ ${MOZSRC}/toolkit/library/moz.build . for tests in tests gtest @if [ -f "${MOZSRC}/media/libcubeb/${tests}/moz.build" ]; then \ ${REINPLACE_CMD} -e 's|OpenBSD|${OPSYS}|g' \ ${MOZSRC}/media/libcubeb/${tests}/moz.build; \ fi . endfor @if [ -f "${MOZSRC}/media/webrtc/trunk/webrtc/build/common.gypi" ]; then \ ${REINPLACE_CMD} -e 's|OS==\"openbsd\"|OS==\"${OPSYS:tl}\"|g' \ ${MOZSRC}/media/webrtc/trunk/webrtc/build/common.gypi; \ fi @if [ -f "${MOZSRC}/media/webrtc/signaling/test/common.build" ]; then \ ${ECHO_CMD} "OS_LIBS += ['sndio']" >> \ ${MOZSRC}/media/webrtc/signaling/test/common.build; \ fi .endif .if ${PORT_OPTIONS:MRUST} || ${MOZILLA_VER:R:R} >= 54 BUILD_DEPENDS+= ${RUST_PORT:T}>=1.24:${RUST_PORT} RUST_PORT?= lang/rust . if ${MOZILLA_VER:R:R} < 54 MOZ_OPTIONS+= --enable-rust . endif .else MOZ_OPTIONS+= --disable-rust .endif .if ${PORT_OPTIONS:MDEBUG} MOZ_OPTIONS+= --enable-debug --disable-release STRIP= # ports/184285 .else MOZ_OPTIONS+= --disable-debug --disable-debug-symbols --enable-release . if ${MOZILLA_VER:R:R} >= 56 && (${ARCH:Maarch64} || ${MACHINE_CPU:Msse2}) MOZ_OPTIONS+= --enable-rust-simd . endif .endif .if ${PORT_OPTIONS:MDTRACE} MOZ_OPTIONS+= --enable-dtrace \ --disable-gold . if ${OPSYS} == FreeBSD && ${OSVERSION} < 1100061 LIBS+= -lelf . endif STRIP= .else MOZ_OPTIONS+= --disable-dtrace .endif .if ${PORT_OPTIONS:MPROFILE} MOZ_OPTIONS+= --enable-profiling STRIP= .else MOZ_OPTIONS+= --disable-profiling .endif .if ${PORT_OPTIONS:MTEST} USE_XORG+= xscrnsaver MOZ_OPTIONS+= --enable-tests .else MOZ_OPTIONS+= --disable-tests .endif .if !defined(STRIP) || ${STRIP} == "" MOZ_OPTIONS+= --disable-strip --disable-install-strip .else MOZ_OPTIONS+= --enable-strip --enable-install-strip .endif # _MAKE_JOBS is only available after bsd.port.post.mk, thus cannot be # used in .mozconfig. And client.mk automatically uses -jN where N # is what multiprocessing.cpu_count() returns. .if defined(DISABLE_MAKE_JOBS) || defined(MAKE_JOBS_UNSAFE) MAKE_JOBS_NUMBER= 1 .endif .if defined(MAKE_JOBS_NUMBER) MOZ_MAKE_FLAGS+=-j${MAKE_JOBS_NUMBER} .endif .if defined(MOZ_MAKE_FLAGS) MOZ_MK_OPTIONS+=MOZ_MAKE_FLAGS="${MOZ_MAKE_FLAGS}" .endif MOZ_SED_ARGS+= -e's|@CPPFLAGS@|${CPPFLAGS}|g' \ -e 's|@CFLAGS@|${CFLAGS}|g' \ -e 's|@LDFLAGS@|${LDFLAGS}|g' \ -e 's|@LIBS@|${LIBS}|g' \ -e 's|@LOCALBASE@|${LOCALBASE}|g' \ -e 's|@PERL@|${PERL}|g' \ -e 's|@MOZDIR@|${PREFIX}/lib/${MOZILLA}|g' \ -e 's|%%PREFIX%%|${PREFIX}|g' \ -e 's|%%CFLAGS%%|${CFLAGS}|g' \ -e 's|%%LDFLAGS%%|${LDFLAGS}|g' \ -e 's|%%LIBS%%|${LIBS}|g' \ -e 's|%%LOCALBASE%%|${LOCALBASE}|g' \ -e 's|%%PERL%%|${PERL}|g' \ -e 's|%%MOZILLA%%|${MOZILLA}|g' \ -e 's|%%MOZILLA_BIN%%|${MOZILLA_BIN}|g' \ -e 's|%%MOZDIR%%|${PREFIX}/lib/${MOZILLA}|g' MOZCONFIG_SED?= ${SED} ${MOZ_SED_ARGS} .if ${ARCH} == amd64 . if ${USE_MOZILLA:M-nss} USE_BINUTILS= # intel-gcm.s CFLAGS+= -B${LOCALBASE}/bin LDFLAGS+= -B${LOCALBASE}/bin . endif .elif ${ARCH:Mpowerpc*} . if ${ARCH} == "powerpc64" MOZ_EXPORT+= UNAME_m="${ARCH}" CFLAGS+= -mminimal-toc . endif .elif ${ARCH} == "sparc64" # Work around miscompilation/mislinkage of the sCanonicalVTable hacks. MOZ_OPTIONS+= --disable-v1-string-abi .endif .else # bsd.port.post.mk post-patch: gecko-post-patch gecko-moz-pis-patch gecko-post-patch: .if exists(${PKGINSTALL_INC}) @${MOZCONFIG_SED} < ${PKGINSTALL_INC} > ${PKGINSTALL} .endif .if exists(${PKGDEINSTALL_INC}) @${MOZCONFIG_SED} < ${PKGDEINSTALL_INC} > ${PKGDEINSTALL} .endif @${RM} ${MOZCONFIG} .if !defined(NOMOZCONFIG) @if [ -e ${PORT_MOZCONFIG} ] ; then \ ${MOZCONFIG_SED} < ${PORT_MOZCONFIG} >> ${MOZCONFIG} ; \ fi .for arg in ${MOZ_OPTIONS} @${ECHO_CMD} ac_add_options ${arg:Q} >> ${MOZCONFIG} .endfor .for arg in ${MOZ_MK_OPTIONS} @${ECHO_CMD} mk_add_options ${arg:Q} >> ${MOZCONFIG} .endfor .for var in ${MOZ_EXPORT} @${ECHO_CMD} export ${var:Q} >> ${MOZCONFIG} .endfor .endif # .if !defined(NOMOZCONFIG) .if exists(${MOZSRC}/build/unix/mozilla-config.in) @${REINPLACE_CMD} -e 's/%{idldir}/%idldir%/g ; \ s|"%FULL_NSPR_CFLAGS%"|`nspr-config --cflags`|g ; \ s|"%FULL_NSPR_LIBS%"|`nspr-config --libs`|g' \ ${MOZSRC}/build/unix/mozilla-config.in .endif .if ${USE_MOZILLA:M-nspr} @${ECHO_MSG} "===> Applying NSPR patches" @for i in ${.CURDIR}/../../devel/nspr/files/patch-*; do \ ${PATCH} ${PATCH_ARGS} -d ${MOZSRC}/nsprpub < $$i; \ done .endif .if ${USE_MOZILLA:M-nss} @${ECHO_MSG} "===> Applying NSS patches" @for i in ${.CURDIR}/../../security/nss/files/patch-*; do \ ${PATCH} ${PATCH_ARGS} -d ${MOZSRC}/security/nss < $$i; \ done .endif @for f in \ ${WRKSRC}/directory/c-sdk/config/FreeBSD.mk \ ${WRKSRC}/directory/c-sdk/configure \ ${MOZSRC}/security/coreconf/FreeBSD.mk \ ${MOZSRC}/js/src/Makefile.in \ ${MOZSRC}/js/src/configure \ ${MOZSRC}/configure \ ${WRKSRC}/configure; do \ if [ -f $$f ] ; then \ ${REINPLACE_CMD} -Ee 's|-lc_r|-pthread|g ; \ s|-l?pthread|-pthread|g ; \ s|echo aout|echo elf|g ; \ s|/usr/X11R6|${LOCALBASE}|g' \ $$f; \ fi; \ done @if [ -f ${WRKSRC}/config/baseconfig.mk ] ; then \ ${REINPLACE_CMD} -e 's|%%MOZILLA%%|${MOZILLA}|g' \ ${WRKSRC}/config/baseconfig.mk; \ fi @${REINPLACE_CMD} -e 's|%%MOZILLA%%|${MOZILLA}|g' \ ${MOZSRC}/config/baseconfig.mk @${REINPLACE_CMD} -e 's|%%PREFIX%%|${PREFIX}|g ; \ s|%%LOCALBASE%%|${LOCALBASE}|g' \ ${MOZSRC}/build/unix/run-mozilla.sh @${REINPLACE_CMD} -e 's|/usr/local/netscape|${LOCALBASE}|g ; \ s|/usr/local/lib/netscape|${LOCALBASE}/lib|g' \ ${MOZSRC}/xpcom/io/SpecialSystemDirectory.cpp @${REINPLACE_CMD} -e 's|/etc|${PREFIX}&|g' \ ${MOZSRC}/xpcom/build/nsXPCOMPrivate.h @${REINPLACE_CMD} -e 's|/usr/local|${LOCALBASE}|g' \ -e 's|mozilla/plugins|browser_plugins|g' \ -e 's|share/mozilla/extensions|lib/xpi|g' \ ${MOZSRC}/xpcom/io/nsAppFileLocationProvider.cpp \ ${MOZSRC}/toolkit/xre/nsXREDirProvider.cpp @${REINPLACE_CMD} -e 's|%%LOCALBASE%%|${LOCALBASE}|g' \ ${MOZSRC}/extensions/spellcheck/hunspell/*/mozHunspell.cpp # handles mozilla pis scripts. gecko-moz-pis-patch: .for moz in ${MOZ_PIS_SCRIPTS} @${MOZCONFIG_SED} < ${FILESDIR}/${moz} > ${WRKDIR}/${moz} .endfor pre-install: gecko-moz-pis-pre-install post-install-script: gecko-create-plist gecko-create-plist: # Create the plist ${RM} ${PLISTF} .for dir in ${MOZILLA_PLIST_DIRS} @cd ${STAGEDIR}${PREFIX}/${dir} && ${FIND} -H -s * ! -type d | \ ${SED} -e 's|^|${dir}/|' >> ${PLISTF} .endfor ${CAT} ${PLISTF} | ${SORT} >> ${TMPPLIST} gecko-moz-pis-pre-install: .if defined(MOZ_PIS_SCRIPTS) ${MKDIR} ${STAGEDIR}${PREFIX}/${MOZ_PIS_DIR} .for moz in ${MOZ_PIS_SCRIPTS} ${INSTALL_SCRIPT} ${WRKDIR}/${moz} ${STAGEDIR}${PREFIX}/${MOZ_PIS_DIR} .endfor .endif .endif .endif # HERE THERE BE TACOS -- adamw Index: head/www/firefox/Makefile =================================================================== --- head/www/firefox/Makefile (revision 472726) +++ head/www/firefox/Makefile (revision 472727) @@ -1,66 +1,66 @@ # Created by: Alan Eldridge # $FreeBSD$ PORTNAME= firefox -DISTVERSION= 60.0.2 +DISTVERSION= 61.0 PORTEPOCH= 1 CATEGORIES= www ipv6 MASTER_SITES= MOZILLA/${PORTNAME}/releases/${DISTVERSION}/source \ MOZILLA/${PORTNAME}/candidates/${DISTVERSION}-candidates/build1/source DISTFILES= ${DISTNAME}.source${EXTRACT_SUFX} MAINTAINER= gecko@FreeBSD.org COMMENT= Web browser based on the browser portion of Mozilla BUILD_DEPENDS= nspr>=4.19:devel/nspr \ - nss>=3.36.1:security/nss \ + nss>=3.37:security/nss \ icu>=59.1,1:devel/icu \ libevent>=2.1.8:devel/libevent \ harfbuzz>=1.7.6:print/harfbuzz \ graphite2>=1.3.11:graphics/graphite2 \ png>=1.6.34:graphics/png \ libvorbis>=1.3.6,3:audio/libvorbis \ libvpx>=1.5.0:multimedia/libvpx \ - sqlite3>=3.22.0:databases/sqlite3 \ + sqlite3>=3.23.1:databases/sqlite3 \ ${PYTHON_PKGNAMEPREFIX}sqlite3>0:databases/py-sqlite3@${PY_FLAVOR} \ v4l_compat>0:multimedia/v4l_compat \ autoconf-2.13:devel/autoconf213 \ yasm:devel/yasm \ zip:archivers/zip # soundtouch>=1.9.0:audio/soundtouch \ USE_GECKO= gecko CONFLICTS_INSTALL= firefox-esr MOZ_PKGCONFIG_FILES= # empty -USE_MOZILLA= -cairo -soundtouch +USE_MOZILLA= -cairo -hunspell -soundtouch USE_GL= gl USES= tar:xz FIREFOX_ICON= ${MOZILLA}.png FIREFOX_ICON_SRC= ${PREFIX}/lib/${MOZILLA}/browser/chrome/icons/default/default48.png FIREFOX_DESKTOP= ${MOZSRC}/taskcluster/docker/${MOZILLA}-snap/${MOZILLA}.desktop MOZ_OPTIONS= --enable-application=browser \ --enable-official-branding -OPTIONS_EXCLUDE= BUNDLED_CAIRO +OPTIONS_EXCLUDE= BUNDLED_CAIRO DTRACE .include "${.CURDIR}/../../www/firefox/Makefile.options" post-patch: @${REINPLACE_CMD} -e 's/%u/%U/' -e '/X-MultipleArgs/d' \ -e '/^Icon/s/=.*/=${FIREFOX_ICON:R}/' \ ${FIREFOX_DESKTOP} @${REINPLACE_CMD} -e 's|%%LOCALBASE%%|${LOCALBASE}|g' \ ${WRKSRC}/browser/app/nsBrowserApp.cpp pre-configure: (cd ${WRKSRC} && ${LOCALBASE}/bin/autoconf-2.13) (cd ${WRKSRC}/js/src/ && ${LOCALBASE}/bin/autoconf-2.13) post-install: ${INSTALL_DATA} ${FIREFOX_DESKTOP} ${STAGEDIR}${PREFIX}/share/applications/ ${MKDIR} ${STAGEDIR}${PREFIX}/share/pixmaps ${LN} -sf ${FIREFOX_ICON_SRC} ${STAGEDIR}${PREFIX}/share/pixmaps/${FIREFOX_ICON} .include Index: head/www/firefox/distinfo =================================================================== --- head/www/firefox/distinfo (revision 472726) +++ head/www/firefox/distinfo (revision 472727) @@ -1,3 +1,3 @@ -TIMESTAMP = 1528228087 -SHA256 (firefox-60.0.2.source.tar.xz) = 764566a06b71164e7fd20b2b0e6b08a71b4ccd4d6fd61867eb08011a551f6725 -SIZE (firefox-60.0.2.source.tar.xz) = 271930220 +TIMESTAMP = 1529363060 +SHA256 (firefox-61.0.source.tar.xz) = d1219830af3b8b64eace38cc9844bb04bf06537909e9aa1dd7682cb1b2099721 +SIZE (firefox-61.0.source.tar.xz) = 266279484 Index: head/www/firefox/files/patch-bug702179 =================================================================== --- head/www/firefox/files/patch-bug702179 (revision 472726) +++ head/www/firefox/files/patch-bug702179 (nonexistent) @@ -1,12 +0,0 @@ -Don't build static JS lib to make sure DTrace probes are picked up. - ---- js/src/build/moz.build.orig 2017-04-16 18:00:50 UTC -+++ js/src/build/moz.build -@@ -71,7 +71,3 @@ if CONFIG['OS_ARCH'] == 'SunOS': - ] - - OS_LIBS += CONFIG['REALTIME_LIBS'] -- --NO_EXPAND_LIBS = True -- --DIST_INSTALL = True Property changes on: head/www/firefox/files/patch-bug702179 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1411481 =================================================================== --- head/www/firefox/files/patch-bug1411481 (revision 472726) +++ head/www/firefox/files/patch-bug1411481 (nonexistent) @@ -1,85 +0,0 @@ -commit 57a55d1c2d33 -Author: sotaro -Date: Wed Mar 21 08:59:38 2018 +0900 - - Bug 1411481- Enable SkiaGL canvas usage on Mac r=jrmuizel ---- - dom/canvas/CanvasRenderingContext2D.cpp | 3 ++- - gfx/thebes/gfxPlatform.cpp | 3 ++- - gfx/thebes/gfxPlatform.h | 2 +- - gfx/thebes/gfxWindowsPlatform.cpp | 7 +++++++ - gfx/thebes/gfxWindowsPlatform.h | 2 ++ - 5 files changed, 14 insertions(+), 3 deletions(-) - -diff --git dom/canvas/CanvasRenderingContext2D.cpp dom/canvas/CanvasRenderingContext2D.cpp -index ef3f868bdbf7..8666564b4d0c 100644 ---- dom/canvas/CanvasRenderingContext2D.cpp -+++ dom/canvas/CanvasRenderingContext2D.cpp -@@ -1438,7 +1438,8 @@ CanvasRenderingContext2D::AllowOpenGLCanvas() const - // HTMLCanvasElement::GetCompositorBackendType would return LAYERS_NONE - // as well, so it wouldn't help much. - -- return (mCompositorBackend == LayersBackend::LAYERS_OPENGL) && -+ return (mCompositorBackend == LayersBackend::LAYERS_OPENGL || -+ mCompositorBackend == LayersBackend::LAYERS_WR) && - gfxPlatform::GetPlatform()->AllowOpenGLCanvas(); - } - -diff --git gfx/thebes/gfxPlatform.cpp gfx/thebes/gfxPlatform.cpp -index b9beea68d8c5..82635f9ab3ce 100644 ---- gfx/thebes/gfxPlatform.cpp -+++ gfx/thebes/gfxPlatform.cpp -@@ -1386,7 +1386,8 @@ bool gfxPlatform::AllowOpenGLCanvas() - // so we let content process always assume correct compositor backend. - // The callers have to do the right thing. - bool correctBackend = !XRE_IsParentProcess() || -- ((mCompositorBackend == LayersBackend::LAYERS_OPENGL) && -+ ((mCompositorBackend == LayersBackend::LAYERS_OPENGL || -+ mCompositorBackend == LayersBackend::LAYERS_WR) && - (GetContentBackendFor(mCompositorBackend) == BackendType::SKIA)); - - if (gfxPrefs::CanvasAzureAccelerated() && correctBackend) { -diff --git gfx/thebes/gfxPlatform.h gfx/thebes/gfxPlatform.h -index c988eb1168a8..3bbf2b763f26 100644 ---- gfx/thebes/gfxPlatform.h -+++ gfx/thebes/gfxPlatform.h -@@ -286,7 +286,7 @@ public: - /// asking for it, we will examine the commands in the first few seconds - /// of the canvas usage, and potentially change to accelerated or - /// non-accelerated canvas. -- bool AllowOpenGLCanvas(); -+ virtual bool AllowOpenGLCanvas(); - virtual void InitializeSkiaCacheLimits(); - - static bool AsyncPanZoomEnabled(); -diff --git gfx/thebes/gfxWindowsPlatform.cpp gfx/thebes/gfxWindowsPlatform.cpp -index 9ba6f40c6cc0..4d65791d075f 100644 ---- gfx/thebes/gfxWindowsPlatform.cpp -+++ gfx/thebes/gfxWindowsPlatform.cpp -@@ -509,6 +509,13 @@ gfxWindowsPlatform::UpdateRenderMode() - } - } - -+bool -+gfxWindowsPlatform::AllowOpenGLCanvas() -+{ -+ // OpenGL canvas is not supported on windows -+ return false; -+} -+ - mozilla::gfx::BackendType - gfxWindowsPlatform::GetContentBackendFor(mozilla::layers::LayersBackend aLayers) - { -diff --git gfx/thebes/gfxWindowsPlatform.h gfx/thebes/gfxWindowsPlatform.h -index 47048de8f5f5..47ec0e9e5547 100644 ---- gfx/thebes/gfxWindowsPlatform.h -+++ gfx/thebes/gfxWindowsPlatform.h -@@ -174,6 +174,8 @@ public: - void SchedulePaintIfDeviceReset() override; - void CheckForContentOnlyDeviceReset(); - -+ bool AllowOpenGLCanvas() override; -+ - mozilla::gfx::BackendType GetContentBackendFor(mozilla::layers::LayersBackend aLayers) override; - - mozilla::gfx::BackendType GetPreferredCanvasBackend() override; Property changes on: head/www/firefox/files/patch-bug1411481 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1435212 =================================================================== --- head/www/firefox/files/patch-bug1435212 (revision 472726) +++ head/www/firefox/files/patch-bug1435212 (nonexistent) @@ -1,13678 +0,0 @@ -commit abe6e049cdd3 -Author: Jean-Yves Avenard -Date: Thu Apr 19 10:19:15 2018 +0200 - - Bug 1435212 - Add support for FFmpeg 4.0. r=bryce - - MozReview-Commit-ID: JlDFSUyGQu - - --HG-- - extra : rebase_source : 310135ac5453b01164910bd3bf50b6107dcbc710 ---- - dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp | 6 + - dom/media/platforms/ffmpeg/FFmpegLibWrapper.cpp | 13 +- - dom/media/platforms/ffmpeg/FFmpegLibs.h | 4 +- - dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp | 4 + - dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp | 2 +- - .../ffmpeg/ffmpeg58/include/COPYING.LGPLv2.1 | 504 ++ - .../ffmpeg/ffmpeg58/include/libavcodec/avcodec.h | 6146 ++++++++++++++++++++ - .../ffmpeg/ffmpeg58/include/libavcodec/avfft.h | 118 + - .../ffmpeg/ffmpeg58/include/libavcodec/vaapi.h | 86 + - .../ffmpeg/ffmpeg58/include/libavcodec/vdpau.h | 176 + - .../ffmpeg/ffmpeg58/include/libavcodec/version.h | 137 + - .../ffmpeg/ffmpeg58/include/libavutil/attributes.h | 167 + - .../ffmpeg/ffmpeg58/include/libavutil/avconfig.h | 6 + - .../ffmpeg/ffmpeg58/include/libavutil/avutil.h | 365 ++ - .../ffmpeg/ffmpeg58/include/libavutil/buffer.h | 291 + - .../ffmpeg58/include/libavutil/channel_layout.h | 232 + - .../ffmpeg/ffmpeg58/include/libavutil/common.h | 560 ++ - .../ffmpeg/ffmpeg58/include/libavutil/cpu.h | 130 + - .../ffmpeg/ffmpeg58/include/libavutil/dict.h | 200 + - .../ffmpeg/ffmpeg58/include/libavutil/error.h | 126 + - .../ffmpeg/ffmpeg58/include/libavutil/frame.h | 893 +++ - .../ffmpeg/ffmpeg58/include/libavutil/hwcontext.h | 584 ++ - .../ffmpeg/ffmpeg58/include/libavutil/intfloat.h | 77 + - .../ffmpeg/ffmpeg58/include/libavutil/log.h | 362 ++ - .../ffmpeg/ffmpeg58/include/libavutil/macros.h | 50 + - .../ffmpeg58/include/libavutil/mathematics.h | 242 + - .../ffmpeg/ffmpeg58/include/libavutil/mem.h | 700 +++ - .../ffmpeg/ffmpeg58/include/libavutil/pixfmt.h | 529 ++ - .../ffmpeg/ffmpeg58/include/libavutil/rational.h | 214 + - .../ffmpeg/ffmpeg58/include/libavutil/samplefmt.h | 272 + - .../ffmpeg/ffmpeg58/include/libavutil/version.h | 139 + - dom/media/platforms/ffmpeg/ffmpeg58/moz.build | 25 + - dom/media/platforms/ffmpeg/moz.build | 1 + - 33 files changed, 13355 insertions(+), 6 deletions(-) - -diff --git dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp -index 4600ad6d247c..a9c7089fa880 100644 ---- dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp -+++ dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp -@@ -77,15 +77,21 @@ FFmpegDataDecoder::InitDecoder() - mCodecContext->extradata_size = mExtraData->Length(); - // FFmpeg may use SIMD instructions to access the data which reads the - // data in 32 bytes block. Must ensure we have enough data to read. -+#if LIBAVCODEC_VERSION_MAJOR >= 58 -+ mExtraData->AppendElements(AV_INPUT_BUFFER_PADDING_SIZE); -+#else - mExtraData->AppendElements(FF_INPUT_BUFFER_PADDING_SIZE); -+#endif - mCodecContext->extradata = mExtraData->Elements(); - } else { - mCodecContext->extradata_size = 0; - } - -+#if LIBAVCODEC_VERSION_MAJOR < 57 - if (codec->capabilities & CODEC_CAP_DR1) { - mCodecContext->flags |= CODEC_FLAG_EMU_EDGE; - } -+#endif - - if (mLib->avcodec_open2(mCodecContext, codec, nullptr) < 0) { - mLib->avcodec_close(mCodecContext); -diff --git dom/media/platforms/ffmpeg/FFmpegLibWrapper.cpp dom/media/platforms/ffmpeg/FFmpegLibWrapper.cpp -index 6b8b38487d56..d08eee3fc8e8 100644 ---- dom/media/platforms/ffmpeg/FFmpegLibWrapper.cpp -+++ dom/media/platforms/ffmpeg/FFmpegLibWrapper.cpp -@@ -63,12 +63,14 @@ FFmpegLibWrapper::Link() - AV_FUNC_55 = 1 << 2, - AV_FUNC_56 = 1 << 3, - AV_FUNC_57 = 1 << 4, -+ AV_FUNC_58 = 1 << 5, - AV_FUNC_AVUTIL_53 = AV_FUNC_53 | AV_FUNC_AVUTIL_MASK, - AV_FUNC_AVUTIL_54 = AV_FUNC_54 | AV_FUNC_AVUTIL_MASK, - AV_FUNC_AVUTIL_55 = AV_FUNC_55 | AV_FUNC_AVUTIL_MASK, - AV_FUNC_AVUTIL_56 = AV_FUNC_56 | AV_FUNC_AVUTIL_MASK, - AV_FUNC_AVUTIL_57 = AV_FUNC_57 | AV_FUNC_AVUTIL_MASK, -- AV_FUNC_AVCODEC_ALL = AV_FUNC_53 | AV_FUNC_54 | AV_FUNC_55 | AV_FUNC_56 | AV_FUNC_57, -+ AV_FUNC_AVUTIL_58 = AV_FUNC_58 | AV_FUNC_AVUTIL_MASK, -+ AV_FUNC_AVCODEC_ALL = AV_FUNC_53 | AV_FUNC_54 | AV_FUNC_55 | AV_FUNC_56 | AV_FUNC_57 | AV_FUNC_58, - AV_FUNC_AVUTIL_ALL = AV_FUNC_AVCODEC_ALL | AV_FUNC_AVUTIL_MASK - }; - -@@ -88,6 +90,9 @@ FFmpegLibWrapper::Link() - case 57: - version = AV_FUNC_57; - break; -+ case 58: -+ version = AV_FUNC_58; -+ break; - default: - FFMPEG_LOG("Unknown avcodec version"); - Unlink(); -@@ -136,9 +141,9 @@ FFmpegLibWrapper::Link() - AV_FUNC(av_log_set_level, AV_FUNC_AVUTIL_ALL) - AV_FUNC(av_malloc, AV_FUNC_AVUTIL_ALL) - AV_FUNC(av_freep, AV_FUNC_AVUTIL_ALL) -- AV_FUNC(av_frame_alloc, (AV_FUNC_AVUTIL_55 | AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57)) -- AV_FUNC(av_frame_free, (AV_FUNC_AVUTIL_55 | AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57)) -- AV_FUNC(av_frame_unref, (AV_FUNC_AVUTIL_55 | AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57)) -+ AV_FUNC(av_frame_alloc, (AV_FUNC_AVUTIL_55 | AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57 | AV_FUNC_AVUTIL_58)) -+ AV_FUNC(av_frame_free, (AV_FUNC_AVUTIL_55 | AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57 | AV_FUNC_AVUTIL_58)) -+ AV_FUNC(av_frame_unref, (AV_FUNC_AVUTIL_55 | AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57 | AV_FUNC_AVUTIL_58)) - AV_FUNC_OPTION(av_frame_get_colorspace, AV_FUNC_AVUTIL_ALL) - #undef AV_FUNC - #undef AV_FUNC_OPTION -diff --git dom/media/platforms/ffmpeg/FFmpegLibs.h dom/media/platforms/ffmpeg/FFmpegLibs.h -index b8c092330212..be99eaa244ca 100644 ---- dom/media/platforms/ffmpeg/FFmpegLibs.h -+++ dom/media/platforms/ffmpeg/FFmpegLibs.h -@@ -27,9 +27,11 @@ extern "C" { - #define AV_CODEC_ID_VP8 CODEC_ID_VP8 - #define AV_CODEC_ID_NONE CODEC_ID_NONE - #define AV_CODEC_ID_FLAC CODEC_ID_FLAC --#define AV_CODEC_FLAG_LOW_DELAY CODEC_FLAG_LOW_DELAY - typedef CodecID AVCodecID; - #endif -+#if LIBAVCODEC_VERSION_MAJOR <= 55 -+#define AV_CODEC_FLAG_LOW_DELAY CODEC_FLAG_LOW_DELAY -+#endif - - #ifdef FFVPX_VERSION - enum { LIBAV_VER = FFVPX_VERSION }; -diff --git dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp -index f671e6e61bc4..b7ab49329502 100644 ---- dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp -+++ dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp -@@ -26,12 +26,15 @@ static FFmpegLibWrapper sLibAV; - - static const char* sLibs[] = { - #if defined(XP_DARWIN) -+ "libavcodec.58.dylib", - "libavcodec.57.dylib", - "libavcodec.56.dylib", - "libavcodec.55.dylib", - "libavcodec.54.dylib", - "libavcodec.53.dylib", - #else -+ "libavcodec.so.58", -+ "libavcodec-ffmpeg.so.58", - "libavcodec-ffmpeg.so.57", - "libavcodec-ffmpeg.so.56", - "libavcodec.so.57", -@@ -134,6 +137,7 @@ FFmpegRuntimeLinker::CreateDecoderModule() - case 55: - case 56: module = FFmpegDecoderModule<55>::Create(&sLibAV); break; - case 57: module = FFmpegDecoderModule<57>::Create(&sLibAV); break; -+ case 58: module = FFmpegDecoderModule<58>::Create(&sLibAV); break; - default: module = nullptr; - } - return module.forget(); -diff --git dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp -index 184fd43b6573..de37e007488f 100644 ---- dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp -+++ dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp -@@ -166,7 +166,7 @@ FFmpegVideoDecoder::InitCodecContext() - } - - if (mLowLatency) { -- mCodecContext->flags |= CODEC_FLAG_LOW_DELAY; -+ mCodecContext->flags |= AV_CODEC_FLAG_LOW_DELAY; - // ffvp9 and ffvp8 at this stage do not support slice threading, but it may - // help with the h264 decoder if there's ever one. - mCodecContext->thread_type = FF_THREAD_SLICE; -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/COPYING.LGPLv2.1 dom/media/platforms/ffmpeg/ffmpeg58/include/COPYING.LGPLv2.1 -new file mode 100644 -index 000000000000..00b4fedfe7e7 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/COPYING.LGPLv2.1 -@@ -0,0 +1,504 @@ -+ GNU LESSER GENERAL PUBLIC LICENSE -+ Version 2.1, February 1999 -+ -+ Copyright (C) 1991, 1999 Free Software Foundation, Inc. -+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ Everyone is permitted to copy and distribute verbatim copies -+ of this license document, but changing it is not allowed. -+ -+[This is the first released version of the Lesser GPL. It also counts -+ as the successor of the GNU Library Public License, version 2, hence -+ the version number 2.1.] -+ -+ Preamble -+ -+ The licenses for most software are designed to take away your -+freedom to share and change it. By contrast, the GNU General Public -+Licenses are intended to guarantee your freedom to share and change -+free software--to make sure the software is free for all its users. -+ -+ This license, the Lesser General Public License, applies to some -+specially designated software packages--typically libraries--of the -+Free Software Foundation and other authors who decide to use it. You -+can use it too, but we suggest you first think carefully about whether -+this license or the ordinary General Public License is the better -+strategy to use in any particular case, based on the explanations below. -+ -+ When we speak of free software, we are referring to freedom of use, -+not price. Our General Public Licenses are designed to make sure that -+you have the freedom to distribute copies of free software (and charge -+for this service if you wish); that you receive source code or can get -+it if you want it; that you can change the software and use pieces of -+it in new free programs; and that you are informed that you can do -+these things. -+ -+ To protect your rights, we need to make restrictions that forbid -+distributors to deny you these rights or to ask you to surrender these -+rights. These restrictions translate to certain responsibilities for -+you if you distribute copies of the library or if you modify it. -+ -+ For example, if you distribute copies of the library, whether gratis -+or for a fee, you must give the recipients all the rights that we gave -+you. You must make sure that they, too, receive or can get the source -+code. If you link other code with the library, you must provide -+complete object files to the recipients, so that they can relink them -+with the library after making changes to the library and recompiling -+it. And you must show them these terms so they know their rights. -+ -+ We protect your rights with a two-step method: (1) we copyright the -+library, and (2) we offer you this license, which gives you legal -+permission to copy, distribute and/or modify the library. -+ -+ To protect each distributor, we want to make it very clear that -+there is no warranty for the free library. Also, if the library is -+modified by someone else and passed on, the recipients should know -+that what they have is not the original version, so that the original -+author's reputation will not be affected by problems that might be -+introduced by others. -+ -+ Finally, software patents pose a constant threat to the existence of -+any free program. We wish to make sure that a company cannot -+effectively restrict the users of a free program by obtaining a -+restrictive license from a patent holder. Therefore, we insist that -+any patent license obtained for a version of the library must be -+consistent with the full freedom of use specified in this license. -+ -+ Most GNU software, including some libraries, is covered by the -+ordinary GNU General Public License. This license, the GNU Lesser -+General Public License, applies to certain designated libraries, and -+is quite different from the ordinary General Public License. We use -+this license for certain libraries in order to permit linking those -+libraries into non-free programs. -+ -+ When a program is linked with a library, whether statically or using -+a shared library, the combination of the two is legally speaking a -+combined work, a derivative of the original library. The ordinary -+General Public License therefore permits such linking only if the -+entire combination fits its criteria of freedom. The Lesser General -+Public License permits more lax criteria for linking other code with -+the library. -+ -+ We call this license the "Lesser" General Public License because it -+does Less to protect the user's freedom than the ordinary General -+Public License. It also provides other free software developers Less -+of an advantage over competing non-free programs. These disadvantages -+are the reason we use the ordinary General Public License for many -+libraries. However, the Lesser license provides advantages in certain -+special circumstances. -+ -+ For example, on rare occasions, there may be a special need to -+encourage the widest possible use of a certain library, so that it becomes -+a de-facto standard. To achieve this, non-free programs must be -+allowed to use the library. A more frequent case is that a free -+library does the same job as widely used non-free libraries. In this -+case, there is little to gain by limiting the free library to free -+software only, so we use the Lesser General Public License. -+ -+ In other cases, permission to use a particular library in non-free -+programs enables a greater number of people to use a large body of -+free software. For example, permission to use the GNU C Library in -+non-free programs enables many more people to use the whole GNU -+operating system, as well as its variant, the GNU/Linux operating -+system. -+ -+ Although the Lesser General Public License is Less protective of the -+users' freedom, it does ensure that the user of a program that is -+linked with the Library has the freedom and the wherewithal to run -+that program using a modified version of the Library. -+ -+ The precise terms and conditions for copying, distribution and -+modification follow. Pay close attention to the difference between a -+"work based on the library" and a "work that uses the library". The -+former contains code derived from the library, whereas the latter must -+be combined with the library in order to run. -+ -+ GNU LESSER GENERAL PUBLIC LICENSE -+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION -+ -+ 0. This License Agreement applies to any software library or other -+program which contains a notice placed by the copyright holder or -+other authorized party saying it may be distributed under the terms of -+this Lesser General Public License (also called "this License"). -+Each licensee is addressed as "you". -+ -+ A "library" means a collection of software functions and/or data -+prepared so as to be conveniently linked with application programs -+(which use some of those functions and data) to form executables. -+ -+ The "Library", below, refers to any such software library or work -+which has been distributed under these terms. A "work based on the -+Library" means either the Library or any derivative work under -+copyright law: that is to say, a work containing the Library or a -+portion of it, either verbatim or with modifications and/or translated -+straightforwardly into another language. (Hereinafter, translation is -+included without limitation in the term "modification".) -+ -+ "Source code" for a work means the preferred form of the work for -+making modifications to it. For a library, complete source code means -+all the source code for all modules it contains, plus any associated -+interface definition files, plus the scripts used to control compilation -+and installation of the library. -+ -+ Activities other than copying, distribution and modification are not -+covered by this License; they are outside its scope. The act of -+running a program using the Library is not restricted, and output from -+such a program is covered only if its contents constitute a work based -+on the Library (independent of the use of the Library in a tool for -+writing it). Whether that is true depends on what the Library does -+and what the program that uses the Library does. -+ -+ 1. You may copy and distribute verbatim copies of the Library's -+complete source code as you receive it, in any medium, provided that -+you conspicuously and appropriately publish on each copy an -+appropriate copyright notice and disclaimer of warranty; keep intact -+all the notices that refer to this License and to the absence of any -+warranty; and distribute a copy of this License along with the -+Library. -+ -+ You may charge a fee for the physical act of transferring a copy, -+and you may at your option offer warranty protection in exchange for a -+fee. -+ -+ 2. You may modify your copy or copies of the Library or any portion -+of it, thus forming a work based on the Library, and copy and -+distribute such modifications or work under the terms of Section 1 -+above, provided that you also meet all of these conditions: -+ -+ a) The modified work must itself be a software library. -+ -+ b) You must cause the files modified to carry prominent notices -+ stating that you changed the files and the date of any change. -+ -+ c) You must cause the whole of the work to be licensed at no -+ charge to all third parties under the terms of this License. -+ -+ d) If a facility in the modified Library refers to a function or a -+ table of data to be supplied by an application program that uses -+ the facility, other than as an argument passed when the facility -+ is invoked, then you must make a good faith effort to ensure that, -+ in the event an application does not supply such function or -+ table, the facility still operates, and performs whatever part of -+ its purpose remains meaningful. -+ -+ (For example, a function in a library to compute square roots has -+ a purpose that is entirely well-defined independent of the -+ application. Therefore, Subsection 2d requires that any -+ application-supplied function or table used by this function must -+ be optional: if the application does not supply it, the square -+ root function must still compute square roots.) -+ -+These requirements apply to the modified work as a whole. If -+identifiable sections of that work are not derived from the Library, -+and can be reasonably considered independent and separate works in -+themselves, then this License, and its terms, do not apply to those -+sections when you distribute them as separate works. But when you -+distribute the same sections as part of a whole which is a work based -+on the Library, the distribution of the whole must be on the terms of -+this License, whose permissions for other licensees extend to the -+entire whole, and thus to each and every part regardless of who wrote -+it. -+ -+Thus, it is not the intent of this section to claim rights or contest -+your rights to work written entirely by you; rather, the intent is to -+exercise the right to control the distribution of derivative or -+collective works based on the Library. -+ -+In addition, mere aggregation of another work not based on the Library -+with the Library (or with a work based on the Library) on a volume of -+a storage or distribution medium does not bring the other work under -+the scope of this License. -+ -+ 3. You may opt to apply the terms of the ordinary GNU General Public -+License instead of this License to a given copy of the Library. To do -+this, you must alter all the notices that refer to this License, so -+that they refer to the ordinary GNU General Public License, version 2, -+instead of to this License. (If a newer version than version 2 of the -+ordinary GNU General Public License has appeared, then you can specify -+that version instead if you wish.) Do not make any other change in -+these notices. -+ -+ Once this change is made in a given copy, it is irreversible for -+that copy, so the ordinary GNU General Public License applies to all -+subsequent copies and derivative works made from that copy. -+ -+ This option is useful when you wish to copy part of the code of -+the Library into a program that is not a library. -+ -+ 4. You may copy and distribute the Library (or a portion or -+derivative of it, under Section 2) in object code or executable form -+under the terms of Sections 1 and 2 above provided that you accompany -+it with the complete corresponding machine-readable source code, which -+must be distributed under the terms of Sections 1 and 2 above on a -+medium customarily used for software interchange. -+ -+ If distribution of object code is made by offering access to copy -+from a designated place, then offering equivalent access to copy the -+source code from the same place satisfies the requirement to -+distribute the source code, even though third parties are not -+compelled to copy the source along with the object code. -+ -+ 5. A program that contains no derivative of any portion of the -+Library, but is designed to work with the Library by being compiled or -+linked with it, is called a "work that uses the Library". Such a -+work, in isolation, is not a derivative work of the Library, and -+therefore falls outside the scope of this License. -+ -+ However, linking a "work that uses the Library" with the Library -+creates an executable that is a derivative of the Library (because it -+contains portions of the Library), rather than a "work that uses the -+library". The executable is therefore covered by this License. -+Section 6 states terms for distribution of such executables. -+ -+ When a "work that uses the Library" uses material from a header file -+that is part of the Library, the object code for the work may be a -+derivative work of the Library even though the source code is not. -+Whether this is true is especially significant if the work can be -+linked without the Library, or if the work is itself a library. The -+threshold for this to be true is not precisely defined by law. -+ -+ If such an object file uses only numerical parameters, data -+structure layouts and accessors, and small macros and small inline -+functions (ten lines or less in length), then the use of the object -+file is unrestricted, regardless of whether it is legally a derivative -+work. (Executables containing this object code plus portions of the -+Library will still fall under Section 6.) -+ -+ Otherwise, if the work is a derivative of the Library, you may -+distribute the object code for the work under the terms of Section 6. -+Any executables containing that work also fall under Section 6, -+whether or not they are linked directly with the Library itself. -+ -+ 6. As an exception to the Sections above, you may also combine or -+link a "work that uses the Library" with the Library to produce a -+work containing portions of the Library, and distribute that work -+under terms of your choice, provided that the terms permit -+modification of the work for the customer's own use and reverse -+engineering for debugging such modifications. -+ -+ You must give prominent notice with each copy of the work that the -+Library is used in it and that the Library and its use are covered by -+this License. You must supply a copy of this License. If the work -+during execution displays copyright notices, you must include the -+copyright notice for the Library among them, as well as a reference -+directing the user to the copy of this License. Also, you must do one -+of these things: -+ -+ a) Accompany the work with the complete corresponding -+ machine-readable source code for the Library including whatever -+ changes were used in the work (which must be distributed under -+ Sections 1 and 2 above); and, if the work is an executable linked -+ with the Library, with the complete machine-readable "work that -+ uses the Library", as object code and/or source code, so that the -+ user can modify the Library and then relink to produce a modified -+ executable containing the modified Library. (It is understood -+ that the user who changes the contents of definitions files in the -+ Library will not necessarily be able to recompile the application -+ to use the modified definitions.) -+ -+ b) Use a suitable shared library mechanism for linking with the -+ Library. A suitable mechanism is one that (1) uses at run time a -+ copy of the library already present on the user's computer system, -+ rather than copying library functions into the executable, and (2) -+ will operate properly with a modified version of the library, if -+ the user installs one, as long as the modified version is -+ interface-compatible with the version that the work was made with. -+ -+ c) Accompany the work with a written offer, valid for at -+ least three years, to give the same user the materials -+ specified in Subsection 6a, above, for a charge no more -+ than the cost of performing this distribution. -+ -+ d) If distribution of the work is made by offering access to copy -+ from a designated place, offer equivalent access to copy the above -+ specified materials from the same place. -+ -+ e) Verify that the user has already received a copy of these -+ materials or that you have already sent this user a copy. -+ -+ For an executable, the required form of the "work that uses the -+Library" must include any data and utility programs needed for -+reproducing the executable from it. However, as a special exception, -+the materials to be distributed need not include anything that is -+normally distributed (in either source or binary form) with the major -+components (compiler, kernel, and so on) of the operating system on -+which the executable runs, unless that component itself accompanies -+the executable. -+ -+ It may happen that this requirement contradicts the license -+restrictions of other proprietary libraries that do not normally -+accompany the operating system. Such a contradiction means you cannot -+use both them and the Library together in an executable that you -+distribute. -+ -+ 7. You may place library facilities that are a work based on the -+Library side-by-side in a single library together with other library -+facilities not covered by this License, and distribute such a combined -+library, provided that the separate distribution of the work based on -+the Library and of the other library facilities is otherwise -+permitted, and provided that you do these two things: -+ -+ a) Accompany the combined library with a copy of the same work -+ based on the Library, uncombined with any other library -+ facilities. This must be distributed under the terms of the -+ Sections above. -+ -+ b) Give prominent notice with the combined library of the fact -+ that part of it is a work based on the Library, and explaining -+ where to find the accompanying uncombined form of the same work. -+ -+ 8. You may not copy, modify, sublicense, link with, or distribute -+the Library except as expressly provided under this License. Any -+attempt otherwise to copy, modify, sublicense, link with, or -+distribute the Library is void, and will automatically terminate your -+rights under this License. However, parties who have received copies, -+or rights, from you under this License will not have their licenses -+terminated so long as such parties remain in full compliance. -+ -+ 9. You are not required to accept this License, since you have not -+signed it. However, nothing else grants you permission to modify or -+distribute the Library or its derivative works. These actions are -+prohibited by law if you do not accept this License. Therefore, by -+modifying or distributing the Library (or any work based on the -+Library), you indicate your acceptance of this License to do so, and -+all its terms and conditions for copying, distributing or modifying -+the Library or works based on it. -+ -+ 10. Each time you redistribute the Library (or any work based on the -+Library), the recipient automatically receives a license from the -+original licensor to copy, distribute, link with or modify the Library -+subject to these terms and conditions. You may not impose any further -+restrictions on the recipients' exercise of the rights granted herein. -+You are not responsible for enforcing compliance by third parties with -+this License. -+ -+ 11. If, as a consequence of a court judgment or allegation of patent -+infringement or for any other reason (not limited to patent issues), -+conditions are imposed on you (whether by court order, agreement or -+otherwise) that contradict the conditions of this License, they do not -+excuse you from the conditions of this License. If you cannot -+distribute so as to satisfy simultaneously your obligations under this -+License and any other pertinent obligations, then as a consequence you -+may not distribute the Library at all. For example, if a patent -+license would not permit royalty-free redistribution of the Library by -+all those who receive copies directly or indirectly through you, then -+the only way you could satisfy both it and this License would be to -+refrain entirely from distribution of the Library. -+ -+If any portion of this section is held invalid or unenforceable under any -+particular circumstance, the balance of the section is intended to apply, -+and the section as a whole is intended to apply in other circumstances. -+ -+It is not the purpose of this section to induce you to infringe any -+patents or other property right claims or to contest validity of any -+such claims; this section has the sole purpose of protecting the -+integrity of the free software distribution system which is -+implemented by public license practices. Many people have made -+generous contributions to the wide range of software distributed -+through that system in reliance on consistent application of that -+system; it is up to the author/donor to decide if he or she is willing -+to distribute software through any other system and a licensee cannot -+impose that choice. -+ -+This section is intended to make thoroughly clear what is believed to -+be a consequence of the rest of this License. -+ -+ 12. If the distribution and/or use of the Library is restricted in -+certain countries either by patents or by copyrighted interfaces, the -+original copyright holder who places the Library under this License may add -+an explicit geographical distribution limitation excluding those countries, -+so that distribution is permitted only in or among countries not thus -+excluded. In such case, this License incorporates the limitation as if -+written in the body of this License. -+ -+ 13. The Free Software Foundation may publish revised and/or new -+versions of the Lesser General Public License from time to time. -+Such new versions will be similar in spirit to the present version, -+but may differ in detail to address new problems or concerns. -+ -+Each version is given a distinguishing version number. If the Library -+specifies a version number of this License which applies to it and -+"any later version", you have the option of following the terms and -+conditions either of that version or of any later version published by -+the Free Software Foundation. If the Library does not specify a -+license version number, you may choose any version ever published by -+the Free Software Foundation. -+ -+ 14. If you wish to incorporate parts of the Library into other free -+programs whose distribution conditions are incompatible with these, -+write to the author to ask for permission. For software which is -+copyrighted by the Free Software Foundation, write to the Free -+Software Foundation; we sometimes make exceptions for this. Our -+decision will be guided by the two goals of preserving the free status -+of all derivatives of our free software and of promoting the sharing -+and reuse of software generally. -+ -+ NO WARRANTY -+ -+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY -+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE -+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE -+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. -+ -+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU -+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR -+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE -+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -+DAMAGES. -+ -+ END OF TERMS AND CONDITIONS -+ -+ How to Apply These Terms to Your New Libraries -+ -+ If you develop a new library, and you want it to be of the greatest -+possible use to the public, we recommend making it free software that -+everyone can redistribute and change. You can do so by permitting -+redistribution under these terms (or, alternatively, under the terms of the -+ordinary General Public License). -+ -+ To apply these terms, attach the following notices to the library. It is -+safest to attach them to the start of each source file to most effectively -+convey the exclusion of warranty; and each file should have at least the -+"copyright" line and a pointer to where the full notice is found. -+ -+ -+ Copyright (C) -+ -+ 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ -+Also add information on how to contact you by electronic and paper mail. -+ -+You should also get your employer (if you work as a programmer) or your -+school, if any, to sign a "copyright disclaimer" for the library, if -+necessary. Here is a sample; alter the names: -+ -+ Yoyodyne, Inc., hereby disclaims all copyright interest in the -+ library `Frob' (a library for tweaking knobs) written by James Random Hacker. -+ -+ , 1 April 1990 -+ Ty Coon, President of Vice -+ -+That's all there is to it! -+ -+ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/avcodec.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/avcodec.h -new file mode 100644 -index 000000000000..fb0c6fae70b3 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/avcodec.h -@@ -0,0 +1,6146 @@ -+/* -+ * copyright (c) 2001 Fabrice Bellard -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVCODEC_AVCODEC_H -+#define AVCODEC_AVCODEC_H -+ -+/** -+ * @file -+ * @ingroup libavc -+ * Libavcodec external API header -+ */ -+ -+#include -+#include "libavutil/samplefmt.h" -+#include "libavutil/attributes.h" -+#include "libavutil/avutil.h" -+#include "libavutil/buffer.h" -+#include "libavutil/cpu.h" -+#include "libavutil/channel_layout.h" -+#include "libavutil/dict.h" -+#include "libavutil/frame.h" -+#include "libavutil/hwcontext.h" -+#include "libavutil/log.h" -+#include "libavutil/pixfmt.h" -+#include "libavutil/rational.h" -+ -+#include "version.h" -+ -+/** -+ * @defgroup libavc libavcodec -+ * Encoding/Decoding Library -+ * -+ * @{ -+ * -+ * @defgroup lavc_decoding Decoding -+ * @{ -+ * @} -+ * -+ * @defgroup lavc_encoding Encoding -+ * @{ -+ * @} -+ * -+ * @defgroup lavc_codec Codecs -+ * @{ -+ * @defgroup lavc_codec_native Native Codecs -+ * @{ -+ * @} -+ * @defgroup lavc_codec_wrappers External library wrappers -+ * @{ -+ * @} -+ * @defgroup lavc_codec_hwaccel Hardware Accelerators bridge -+ * @{ -+ * @} -+ * @} -+ * @defgroup lavc_internal Internal -+ * @{ -+ * @} -+ * @} -+ */ -+ -+/** -+ * @ingroup libavc -+ * @defgroup lavc_encdec send/receive encoding and decoding API overview -+ * @{ -+ * -+ * The avcodec_send_packet()/avcodec_receive_frame()/avcodec_send_frame()/ -+ * avcodec_receive_packet() functions provide an encode/decode API, which -+ * decouples input and output. -+ * -+ * The API is very similar for encoding/decoding and audio/video, and works as -+ * follows: -+ * - Set up and open the AVCodecContext as usual. -+ * - Send valid input: -+ * - For decoding, call avcodec_send_packet() to give the decoder raw -+ * compressed data in an AVPacket. -+ * - For encoding, call avcodec_send_frame() to give the encoder an AVFrame -+ * containing uncompressed audio or video. -+ * In both cases, it is recommended that AVPackets and AVFrames are -+ * refcounted, or libavcodec might have to copy the input data. (libavformat -+ * always returns refcounted AVPackets, and av_frame_get_buffer() allocates -+ * refcounted AVFrames.) -+ * - Receive output in a loop. Periodically call one of the avcodec_receive_*() -+ * functions and process their output: -+ * - For decoding, call avcodec_receive_frame(). On success, it will return -+ * an AVFrame containing uncompressed audio or video data. -+ * - For encoding, call avcodec_receive_packet(). On success, it will return -+ * an AVPacket with a compressed frame. -+ * Repeat this call until it returns AVERROR(EAGAIN) or an error. The -+ * AVERROR(EAGAIN) return value means that new input data is required to -+ * return new output. In this case, continue with sending input. For each -+ * input frame/packet, the codec will typically return 1 output frame/packet, -+ * but it can also be 0 or more than 1. -+ * -+ * At the beginning of decoding or encoding, the codec might accept multiple -+ * input frames/packets without returning a frame, until its internal buffers -+ * are filled. This situation is handled transparently if you follow the steps -+ * outlined above. -+ * -+ * In theory, sending input can result in EAGAIN - this should happen only if -+ * not all output was received. You can use this to structure alternative decode -+ * or encode loops other than the one suggested above. For example, you could -+ * try sending new input on each iteration, and try to receive output if that -+ * returns EAGAIN. -+ * -+ * End of stream situations. These require "flushing" (aka draining) the codec, -+ * as the codec might buffer multiple frames or packets internally for -+ * performance or out of necessity (consider B-frames). -+ * This is handled as follows: -+ * - Instead of valid input, send NULL to the avcodec_send_packet() (decoding) -+ * or avcodec_send_frame() (encoding) functions. This will enter draining -+ * mode. -+ * - Call avcodec_receive_frame() (decoding) or avcodec_receive_packet() -+ * (encoding) in a loop until AVERROR_EOF is returned. The functions will -+ * not return AVERROR(EAGAIN), unless you forgot to enter draining mode. -+ * - Before decoding can be resumed again, the codec has to be reset with -+ * avcodec_flush_buffers(). -+ * -+ * Using the API as outlined above is highly recommended. But it is also -+ * possible to call functions outside of this rigid schema. For example, you can -+ * call avcodec_send_packet() repeatedly without calling -+ * avcodec_receive_frame(). In this case, avcodec_send_packet() will succeed -+ * until the codec's internal buffer has been filled up (which is typically of -+ * size 1 per output frame, after initial input), and then reject input with -+ * AVERROR(EAGAIN). Once it starts rejecting input, you have no choice but to -+ * read at least some output. -+ * -+ * Not all codecs will follow a rigid and predictable dataflow; the only -+ * guarantee is that an AVERROR(EAGAIN) return value on a send/receive call on -+ * one end implies that a receive/send call on the other end will succeed, or -+ * at least will not fail with AVERROR(EAGAIN). In general, no codec will -+ * permit unlimited buffering of input or output. -+ * -+ * This API replaces the following legacy functions: -+ * - avcodec_decode_video2() and avcodec_decode_audio4(): -+ * Use avcodec_send_packet() to feed input to the decoder, then use -+ * avcodec_receive_frame() to receive decoded frames after each packet. -+ * Unlike with the old video decoding API, multiple frames might result from -+ * a packet. For audio, splitting the input packet into frames by partially -+ * decoding packets becomes transparent to the API user. You never need to -+ * feed an AVPacket to the API twice (unless it is rejected with AVERROR(EAGAIN) - then -+ * no data was read from the packet). -+ * Additionally, sending a flush/draining packet is required only once. -+ * - avcodec_encode_video2()/avcodec_encode_audio2(): -+ * Use avcodec_send_frame() to feed input to the encoder, then use -+ * avcodec_receive_packet() to receive encoded packets. -+ * Providing user-allocated buffers for avcodec_receive_packet() is not -+ * possible. -+ * - The new API does not handle subtitles yet. -+ * -+ * Mixing new and old function calls on the same AVCodecContext is not allowed, -+ * and will result in undefined behavior. -+ * -+ * Some codecs might require using the new API; using the old API will return -+ * an error when calling it. All codecs support the new API. -+ * -+ * A codec is not allowed to return AVERROR(EAGAIN) for both sending and receiving. This -+ * would be an invalid state, which could put the codec user into an endless -+ * loop. The API has no concept of time either: it cannot happen that trying to -+ * do avcodec_send_packet() results in AVERROR(EAGAIN), but a repeated call 1 second -+ * later accepts the packet (with no other receive/flush API calls involved). -+ * The API is a strict state machine, and the passage of time is not supposed -+ * to influence it. Some timing-dependent behavior might still be deemed -+ * acceptable in certain cases. But it must never result in both send/receive -+ * returning EAGAIN at the same time at any point. It must also absolutely be -+ * avoided that the current state is "unstable" and can "flip-flop" between -+ * the send/receive APIs allowing progress. For example, it's not allowed that -+ * the codec randomly decides that it actually wants to consume a packet now -+ * instead of returning a frame, after it just returned AVERROR(EAGAIN) on an -+ * avcodec_send_packet() call. -+ * @} -+ */ -+ -+/** -+ * @defgroup lavc_core Core functions/structures. -+ * @ingroup libavc -+ * -+ * Basic definitions, functions for querying libavcodec capabilities, -+ * allocating core structures, etc. -+ * @{ -+ */ -+ -+ -+/** -+ * Identify the syntax and semantics of the bitstream. -+ * The principle is roughly: -+ * Two decoders with the same ID can decode the same streams. -+ * Two encoders with the same ID can encode compatible streams. -+ * There may be slight deviations from the principle due to implementation -+ * details. -+ * -+ * If you add a codec ID to this list, add it so that -+ * 1. no value of an existing codec ID changes (that would break ABI), -+ * 2. it is as close as possible to similar codecs -+ * -+ * After adding new codec IDs, do not forget to add an entry to the codec -+ * descriptor list and bump libavcodec minor version. -+ */ -+enum AVCodecID { -+ AV_CODEC_ID_NONE, -+ -+ /* video codecs */ -+ AV_CODEC_ID_MPEG1VIDEO, -+ AV_CODEC_ID_MPEG2VIDEO, ///< preferred ID for MPEG-1/2 video decoding -+ AV_CODEC_ID_H261, -+ AV_CODEC_ID_H263, -+ AV_CODEC_ID_RV10, -+ AV_CODEC_ID_RV20, -+ AV_CODEC_ID_MJPEG, -+ AV_CODEC_ID_MJPEGB, -+ AV_CODEC_ID_LJPEG, -+ AV_CODEC_ID_SP5X, -+ AV_CODEC_ID_JPEGLS, -+ AV_CODEC_ID_MPEG4, -+ AV_CODEC_ID_RAWVIDEO, -+ AV_CODEC_ID_MSMPEG4V1, -+ AV_CODEC_ID_MSMPEG4V2, -+ AV_CODEC_ID_MSMPEG4V3, -+ AV_CODEC_ID_WMV1, -+ AV_CODEC_ID_WMV2, -+ AV_CODEC_ID_H263P, -+ AV_CODEC_ID_H263I, -+ AV_CODEC_ID_FLV1, -+ AV_CODEC_ID_SVQ1, -+ AV_CODEC_ID_SVQ3, -+ AV_CODEC_ID_DVVIDEO, -+ AV_CODEC_ID_HUFFYUV, -+ AV_CODEC_ID_CYUV, -+ AV_CODEC_ID_H264, -+ AV_CODEC_ID_INDEO3, -+ AV_CODEC_ID_VP3, -+ AV_CODEC_ID_THEORA, -+ AV_CODEC_ID_ASV1, -+ AV_CODEC_ID_ASV2, -+ AV_CODEC_ID_FFV1, -+ AV_CODEC_ID_4XM, -+ AV_CODEC_ID_VCR1, -+ AV_CODEC_ID_CLJR, -+ AV_CODEC_ID_MDEC, -+ AV_CODEC_ID_ROQ, -+ AV_CODEC_ID_INTERPLAY_VIDEO, -+ AV_CODEC_ID_XAN_WC3, -+ AV_CODEC_ID_XAN_WC4, -+ AV_CODEC_ID_RPZA, -+ AV_CODEC_ID_CINEPAK, -+ AV_CODEC_ID_WS_VQA, -+ AV_CODEC_ID_MSRLE, -+ AV_CODEC_ID_MSVIDEO1, -+ AV_CODEC_ID_IDCIN, -+ AV_CODEC_ID_8BPS, -+ AV_CODEC_ID_SMC, -+ AV_CODEC_ID_FLIC, -+ AV_CODEC_ID_TRUEMOTION1, -+ AV_CODEC_ID_VMDVIDEO, -+ AV_CODEC_ID_MSZH, -+ AV_CODEC_ID_ZLIB, -+ AV_CODEC_ID_QTRLE, -+ AV_CODEC_ID_TSCC, -+ AV_CODEC_ID_ULTI, -+ AV_CODEC_ID_QDRAW, -+ AV_CODEC_ID_VIXL, -+ AV_CODEC_ID_QPEG, -+ AV_CODEC_ID_PNG, -+ AV_CODEC_ID_PPM, -+ AV_CODEC_ID_PBM, -+ AV_CODEC_ID_PGM, -+ AV_CODEC_ID_PGMYUV, -+ AV_CODEC_ID_PAM, -+ AV_CODEC_ID_FFVHUFF, -+ AV_CODEC_ID_RV30, -+ AV_CODEC_ID_RV40, -+ AV_CODEC_ID_VC1, -+ AV_CODEC_ID_WMV3, -+ AV_CODEC_ID_LOCO, -+ AV_CODEC_ID_WNV1, -+ AV_CODEC_ID_AASC, -+ AV_CODEC_ID_INDEO2, -+ AV_CODEC_ID_FRAPS, -+ AV_CODEC_ID_TRUEMOTION2, -+ AV_CODEC_ID_BMP, -+ AV_CODEC_ID_CSCD, -+ AV_CODEC_ID_MMVIDEO, -+ AV_CODEC_ID_ZMBV, -+ AV_CODEC_ID_AVS, -+ AV_CODEC_ID_SMACKVIDEO, -+ AV_CODEC_ID_NUV, -+ AV_CODEC_ID_KMVC, -+ AV_CODEC_ID_FLASHSV, -+ AV_CODEC_ID_CAVS, -+ AV_CODEC_ID_JPEG2000, -+ AV_CODEC_ID_VMNC, -+ AV_CODEC_ID_VP5, -+ AV_CODEC_ID_VP6, -+ AV_CODEC_ID_VP6F, -+ AV_CODEC_ID_TARGA, -+ AV_CODEC_ID_DSICINVIDEO, -+ AV_CODEC_ID_TIERTEXSEQVIDEO, -+ AV_CODEC_ID_TIFF, -+ AV_CODEC_ID_GIF, -+ AV_CODEC_ID_DXA, -+ AV_CODEC_ID_DNXHD, -+ AV_CODEC_ID_THP, -+ AV_CODEC_ID_SGI, -+ AV_CODEC_ID_C93, -+ AV_CODEC_ID_BETHSOFTVID, -+ AV_CODEC_ID_PTX, -+ AV_CODEC_ID_TXD, -+ AV_CODEC_ID_VP6A, -+ AV_CODEC_ID_AMV, -+ AV_CODEC_ID_VB, -+ AV_CODEC_ID_PCX, -+ AV_CODEC_ID_SUNRAST, -+ AV_CODEC_ID_INDEO4, -+ AV_CODEC_ID_INDEO5, -+ AV_CODEC_ID_MIMIC, -+ AV_CODEC_ID_RL2, -+ AV_CODEC_ID_ESCAPE124, -+ AV_CODEC_ID_DIRAC, -+ AV_CODEC_ID_BFI, -+ AV_CODEC_ID_CMV, -+ AV_CODEC_ID_MOTIONPIXELS, -+ AV_CODEC_ID_TGV, -+ AV_CODEC_ID_TGQ, -+ AV_CODEC_ID_TQI, -+ AV_CODEC_ID_AURA, -+ AV_CODEC_ID_AURA2, -+ AV_CODEC_ID_V210X, -+ AV_CODEC_ID_TMV, -+ AV_CODEC_ID_V210, -+ AV_CODEC_ID_DPX, -+ AV_CODEC_ID_MAD, -+ AV_CODEC_ID_FRWU, -+ AV_CODEC_ID_FLASHSV2, -+ AV_CODEC_ID_CDGRAPHICS, -+ AV_CODEC_ID_R210, -+ AV_CODEC_ID_ANM, -+ AV_CODEC_ID_BINKVIDEO, -+ AV_CODEC_ID_IFF_ILBM, -+#define AV_CODEC_ID_IFF_BYTERUN1 AV_CODEC_ID_IFF_ILBM -+ AV_CODEC_ID_KGV1, -+ AV_CODEC_ID_YOP, -+ AV_CODEC_ID_VP8, -+ AV_CODEC_ID_PICTOR, -+ AV_CODEC_ID_ANSI, -+ AV_CODEC_ID_A64_MULTI, -+ AV_CODEC_ID_A64_MULTI5, -+ AV_CODEC_ID_R10K, -+ AV_CODEC_ID_MXPEG, -+ AV_CODEC_ID_LAGARITH, -+ AV_CODEC_ID_PRORES, -+ AV_CODEC_ID_JV, -+ AV_CODEC_ID_DFA, -+ AV_CODEC_ID_WMV3IMAGE, -+ AV_CODEC_ID_VC1IMAGE, -+ AV_CODEC_ID_UTVIDEO, -+ AV_CODEC_ID_BMV_VIDEO, -+ AV_CODEC_ID_VBLE, -+ AV_CODEC_ID_DXTORY, -+ AV_CODEC_ID_V410, -+ AV_CODEC_ID_XWD, -+ AV_CODEC_ID_CDXL, -+ AV_CODEC_ID_XBM, -+ AV_CODEC_ID_ZEROCODEC, -+ AV_CODEC_ID_MSS1, -+ AV_CODEC_ID_MSA1, -+ AV_CODEC_ID_TSCC2, -+ AV_CODEC_ID_MTS2, -+ AV_CODEC_ID_CLLC, -+ AV_CODEC_ID_MSS2, -+ AV_CODEC_ID_VP9, -+ AV_CODEC_ID_AIC, -+ AV_CODEC_ID_ESCAPE130, -+ AV_CODEC_ID_G2M, -+ AV_CODEC_ID_WEBP, -+ AV_CODEC_ID_HNM4_VIDEO, -+ AV_CODEC_ID_HEVC, -+#define AV_CODEC_ID_H265 AV_CODEC_ID_HEVC -+ AV_CODEC_ID_FIC, -+ AV_CODEC_ID_ALIAS_PIX, -+ AV_CODEC_ID_BRENDER_PIX, -+ AV_CODEC_ID_PAF_VIDEO, -+ AV_CODEC_ID_EXR, -+ AV_CODEC_ID_VP7, -+ AV_CODEC_ID_SANM, -+ AV_CODEC_ID_SGIRLE, -+ AV_CODEC_ID_MVC1, -+ AV_CODEC_ID_MVC2, -+ AV_CODEC_ID_HQX, -+ AV_CODEC_ID_TDSC, -+ AV_CODEC_ID_HQ_HQA, -+ AV_CODEC_ID_HAP, -+ AV_CODEC_ID_DDS, -+ AV_CODEC_ID_DXV, -+ AV_CODEC_ID_SCREENPRESSO, -+ AV_CODEC_ID_RSCC, -+ -+ AV_CODEC_ID_Y41P = 0x8000, -+ AV_CODEC_ID_AVRP, -+ AV_CODEC_ID_012V, -+ AV_CODEC_ID_AVUI, -+ AV_CODEC_ID_AYUV, -+ AV_CODEC_ID_TARGA_Y216, -+ AV_CODEC_ID_V308, -+ AV_CODEC_ID_V408, -+ AV_CODEC_ID_YUV4, -+ AV_CODEC_ID_AVRN, -+ AV_CODEC_ID_CPIA, -+ AV_CODEC_ID_XFACE, -+ AV_CODEC_ID_SNOW, -+ AV_CODEC_ID_SMVJPEG, -+ AV_CODEC_ID_APNG, -+ AV_CODEC_ID_DAALA, -+ AV_CODEC_ID_CFHD, -+ AV_CODEC_ID_TRUEMOTION2RT, -+ AV_CODEC_ID_M101, -+ AV_CODEC_ID_MAGICYUV, -+ AV_CODEC_ID_SHEERVIDEO, -+ AV_CODEC_ID_YLC, -+ AV_CODEC_ID_PSD, -+ AV_CODEC_ID_PIXLET, -+ AV_CODEC_ID_SPEEDHQ, -+ AV_CODEC_ID_FMVC, -+ AV_CODEC_ID_SCPR, -+ AV_CODEC_ID_CLEARVIDEO, -+ AV_CODEC_ID_XPM, -+ AV_CODEC_ID_AV1, -+ AV_CODEC_ID_BITPACKED, -+ AV_CODEC_ID_MSCC, -+ AV_CODEC_ID_SRGC, -+ AV_CODEC_ID_SVG, -+ AV_CODEC_ID_GDV, -+ AV_CODEC_ID_FITS, -+ -+ /* various PCM "codecs" */ -+ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs -+ AV_CODEC_ID_PCM_S16LE = 0x10000, -+ AV_CODEC_ID_PCM_S16BE, -+ AV_CODEC_ID_PCM_U16LE, -+ AV_CODEC_ID_PCM_U16BE, -+ AV_CODEC_ID_PCM_S8, -+ AV_CODEC_ID_PCM_U8, -+ AV_CODEC_ID_PCM_MULAW, -+ AV_CODEC_ID_PCM_ALAW, -+ AV_CODEC_ID_PCM_S32LE, -+ AV_CODEC_ID_PCM_S32BE, -+ AV_CODEC_ID_PCM_U32LE, -+ AV_CODEC_ID_PCM_U32BE, -+ AV_CODEC_ID_PCM_S24LE, -+ AV_CODEC_ID_PCM_S24BE, -+ AV_CODEC_ID_PCM_U24LE, -+ AV_CODEC_ID_PCM_U24BE, -+ AV_CODEC_ID_PCM_S24DAUD, -+ AV_CODEC_ID_PCM_ZORK, -+ AV_CODEC_ID_PCM_S16LE_PLANAR, -+ AV_CODEC_ID_PCM_DVD, -+ AV_CODEC_ID_PCM_F32BE, -+ AV_CODEC_ID_PCM_F32LE, -+ AV_CODEC_ID_PCM_F64BE, -+ AV_CODEC_ID_PCM_F64LE, -+ AV_CODEC_ID_PCM_BLURAY, -+ AV_CODEC_ID_PCM_LXF, -+ AV_CODEC_ID_S302M, -+ AV_CODEC_ID_PCM_S8_PLANAR, -+ AV_CODEC_ID_PCM_S24LE_PLANAR, -+ AV_CODEC_ID_PCM_S32LE_PLANAR, -+ AV_CODEC_ID_PCM_S16BE_PLANAR, -+ -+ AV_CODEC_ID_PCM_S64LE = 0x10800, -+ AV_CODEC_ID_PCM_S64BE, -+ AV_CODEC_ID_PCM_F16LE, -+ AV_CODEC_ID_PCM_F24LE, -+ -+ /* various ADPCM codecs */ -+ AV_CODEC_ID_ADPCM_IMA_QT = 0x11000, -+ AV_CODEC_ID_ADPCM_IMA_WAV, -+ AV_CODEC_ID_ADPCM_IMA_DK3, -+ AV_CODEC_ID_ADPCM_IMA_DK4, -+ AV_CODEC_ID_ADPCM_IMA_WS, -+ AV_CODEC_ID_ADPCM_IMA_SMJPEG, -+ AV_CODEC_ID_ADPCM_MS, -+ AV_CODEC_ID_ADPCM_4XM, -+ AV_CODEC_ID_ADPCM_XA, -+ AV_CODEC_ID_ADPCM_ADX, -+ AV_CODEC_ID_ADPCM_EA, -+ AV_CODEC_ID_ADPCM_G726, -+ AV_CODEC_ID_ADPCM_CT, -+ AV_CODEC_ID_ADPCM_SWF, -+ AV_CODEC_ID_ADPCM_YAMAHA, -+ AV_CODEC_ID_ADPCM_SBPRO_4, -+ AV_CODEC_ID_ADPCM_SBPRO_3, -+ AV_CODEC_ID_ADPCM_SBPRO_2, -+ AV_CODEC_ID_ADPCM_THP, -+ AV_CODEC_ID_ADPCM_IMA_AMV, -+ AV_CODEC_ID_ADPCM_EA_R1, -+ AV_CODEC_ID_ADPCM_EA_R3, -+ AV_CODEC_ID_ADPCM_EA_R2, -+ AV_CODEC_ID_ADPCM_IMA_EA_SEAD, -+ AV_CODEC_ID_ADPCM_IMA_EA_EACS, -+ AV_CODEC_ID_ADPCM_EA_XAS, -+ AV_CODEC_ID_ADPCM_EA_MAXIS_XA, -+ AV_CODEC_ID_ADPCM_IMA_ISS, -+ AV_CODEC_ID_ADPCM_G722, -+ AV_CODEC_ID_ADPCM_IMA_APC, -+ AV_CODEC_ID_ADPCM_VIMA, -+ -+ AV_CODEC_ID_ADPCM_AFC = 0x11800, -+ AV_CODEC_ID_ADPCM_IMA_OKI, -+ AV_CODEC_ID_ADPCM_DTK, -+ AV_CODEC_ID_ADPCM_IMA_RAD, -+ AV_CODEC_ID_ADPCM_G726LE, -+ AV_CODEC_ID_ADPCM_THP_LE, -+ AV_CODEC_ID_ADPCM_PSX, -+ AV_CODEC_ID_ADPCM_AICA, -+ AV_CODEC_ID_ADPCM_IMA_DAT4, -+ AV_CODEC_ID_ADPCM_MTAF, -+ -+ /* AMR */ -+ AV_CODEC_ID_AMR_NB = 0x12000, -+ AV_CODEC_ID_AMR_WB, -+ -+ /* RealAudio codecs*/ -+ AV_CODEC_ID_RA_144 = 0x13000, -+ AV_CODEC_ID_RA_288, -+ -+ /* various DPCM codecs */ -+ AV_CODEC_ID_ROQ_DPCM = 0x14000, -+ AV_CODEC_ID_INTERPLAY_DPCM, -+ AV_CODEC_ID_XAN_DPCM, -+ AV_CODEC_ID_SOL_DPCM, -+ -+ AV_CODEC_ID_SDX2_DPCM = 0x14800, -+ AV_CODEC_ID_GREMLIN_DPCM, -+ -+ /* audio codecs */ -+ AV_CODEC_ID_MP2 = 0x15000, -+ AV_CODEC_ID_MP3, ///< preferred ID for decoding MPEG audio layer 1, 2 or 3 -+ AV_CODEC_ID_AAC, -+ AV_CODEC_ID_AC3, -+ AV_CODEC_ID_DTS, -+ AV_CODEC_ID_VORBIS, -+ AV_CODEC_ID_DVAUDIO, -+ AV_CODEC_ID_WMAV1, -+ AV_CODEC_ID_WMAV2, -+ AV_CODEC_ID_MACE3, -+ AV_CODEC_ID_MACE6, -+ AV_CODEC_ID_VMDAUDIO, -+ AV_CODEC_ID_FLAC, -+ AV_CODEC_ID_MP3ADU, -+ AV_CODEC_ID_MP3ON4, -+ AV_CODEC_ID_SHORTEN, -+ AV_CODEC_ID_ALAC, -+ AV_CODEC_ID_WESTWOOD_SND1, -+ AV_CODEC_ID_GSM, ///< as in Berlin toast format -+ AV_CODEC_ID_QDM2, -+ AV_CODEC_ID_COOK, -+ AV_CODEC_ID_TRUESPEECH, -+ AV_CODEC_ID_TTA, -+ AV_CODEC_ID_SMACKAUDIO, -+ AV_CODEC_ID_QCELP, -+ AV_CODEC_ID_WAVPACK, -+ AV_CODEC_ID_DSICINAUDIO, -+ AV_CODEC_ID_IMC, -+ AV_CODEC_ID_MUSEPACK7, -+ AV_CODEC_ID_MLP, -+ AV_CODEC_ID_GSM_MS, /* as found in WAV */ -+ AV_CODEC_ID_ATRAC3, -+ AV_CODEC_ID_APE, -+ AV_CODEC_ID_NELLYMOSER, -+ AV_CODEC_ID_MUSEPACK8, -+ AV_CODEC_ID_SPEEX, -+ AV_CODEC_ID_WMAVOICE, -+ AV_CODEC_ID_WMAPRO, -+ AV_CODEC_ID_WMALOSSLESS, -+ AV_CODEC_ID_ATRAC3P, -+ AV_CODEC_ID_EAC3, -+ AV_CODEC_ID_SIPR, -+ AV_CODEC_ID_MP1, -+ AV_CODEC_ID_TWINVQ, -+ AV_CODEC_ID_TRUEHD, -+ AV_CODEC_ID_MP4ALS, -+ AV_CODEC_ID_ATRAC1, -+ AV_CODEC_ID_BINKAUDIO_RDFT, -+ AV_CODEC_ID_BINKAUDIO_DCT, -+ AV_CODEC_ID_AAC_LATM, -+ AV_CODEC_ID_QDMC, -+ AV_CODEC_ID_CELT, -+ AV_CODEC_ID_G723_1, -+ AV_CODEC_ID_G729, -+ AV_CODEC_ID_8SVX_EXP, -+ AV_CODEC_ID_8SVX_FIB, -+ AV_CODEC_ID_BMV_AUDIO, -+ AV_CODEC_ID_RALF, -+ AV_CODEC_ID_IAC, -+ AV_CODEC_ID_ILBC, -+ AV_CODEC_ID_OPUS, -+ AV_CODEC_ID_COMFORT_NOISE, -+ AV_CODEC_ID_TAK, -+ AV_CODEC_ID_METASOUND, -+ AV_CODEC_ID_PAF_AUDIO, -+ AV_CODEC_ID_ON2AVC, -+ AV_CODEC_ID_DSS_SP, -+ AV_CODEC_ID_CODEC2, -+ -+ AV_CODEC_ID_FFWAVESYNTH = 0x15800, -+ AV_CODEC_ID_SONIC, -+ AV_CODEC_ID_SONIC_LS, -+ AV_CODEC_ID_EVRC, -+ AV_CODEC_ID_SMV, -+ AV_CODEC_ID_DSD_LSBF, -+ AV_CODEC_ID_DSD_MSBF, -+ AV_CODEC_ID_DSD_LSBF_PLANAR, -+ AV_CODEC_ID_DSD_MSBF_PLANAR, -+ AV_CODEC_ID_4GV, -+ AV_CODEC_ID_INTERPLAY_ACM, -+ AV_CODEC_ID_XMA1, -+ AV_CODEC_ID_XMA2, -+ AV_CODEC_ID_DST, -+ AV_CODEC_ID_ATRAC3AL, -+ AV_CODEC_ID_ATRAC3PAL, -+ AV_CODEC_ID_DOLBY_E, -+ AV_CODEC_ID_APTX, -+ AV_CODEC_ID_APTX_HD, -+ AV_CODEC_ID_SBC, -+ -+ /* subtitle codecs */ -+ AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs. -+ AV_CODEC_ID_DVD_SUBTITLE = 0x17000, -+ AV_CODEC_ID_DVB_SUBTITLE, -+ AV_CODEC_ID_TEXT, ///< raw UTF-8 text -+ AV_CODEC_ID_XSUB, -+ AV_CODEC_ID_SSA, -+ AV_CODEC_ID_MOV_TEXT, -+ AV_CODEC_ID_HDMV_PGS_SUBTITLE, -+ AV_CODEC_ID_DVB_TELETEXT, -+ AV_CODEC_ID_SRT, -+ -+ AV_CODEC_ID_MICRODVD = 0x17800, -+ AV_CODEC_ID_EIA_608, -+ AV_CODEC_ID_JACOSUB, -+ AV_CODEC_ID_SAMI, -+ AV_CODEC_ID_REALTEXT, -+ AV_CODEC_ID_STL, -+ AV_CODEC_ID_SUBVIEWER1, -+ AV_CODEC_ID_SUBVIEWER, -+ AV_CODEC_ID_SUBRIP, -+ AV_CODEC_ID_WEBVTT, -+ AV_CODEC_ID_MPL2, -+ AV_CODEC_ID_VPLAYER, -+ AV_CODEC_ID_PJS, -+ AV_CODEC_ID_ASS, -+ AV_CODEC_ID_HDMV_TEXT_SUBTITLE, -+ -+ /* other specific kind of codecs (generally used for attachments) */ -+ AV_CODEC_ID_FIRST_UNKNOWN = 0x18000, ///< A dummy ID pointing at the start of various fake codecs. -+ AV_CODEC_ID_TTF = 0x18000, -+ -+ AV_CODEC_ID_SCTE_35, ///< Contain timestamp estimated through PCR of program stream. -+ AV_CODEC_ID_BINTEXT = 0x18800, -+ AV_CODEC_ID_XBIN, -+ AV_CODEC_ID_IDF, -+ AV_CODEC_ID_OTF, -+ AV_CODEC_ID_SMPTE_KLV, -+ AV_CODEC_ID_DVD_NAV, -+ AV_CODEC_ID_TIMED_ID3, -+ AV_CODEC_ID_BIN_DATA, -+ -+ -+ AV_CODEC_ID_PROBE = 0x19000, ///< codec_id is not known (like AV_CODEC_ID_NONE) but lavf should attempt to identify it -+ -+ AV_CODEC_ID_MPEG2TS = 0x20000, /**< _FAKE_ codec to indicate a raw MPEG-2 TS -+ * stream (only used by libavformat) */ -+ AV_CODEC_ID_MPEG4SYSTEMS = 0x20001, /**< _FAKE_ codec to indicate a MPEG-4 Systems -+ * stream (only used by libavformat) */ -+ AV_CODEC_ID_FFMETADATA = 0x21000, ///< Dummy codec for streams containing only metadata information. -+ AV_CODEC_ID_WRAPPED_AVFRAME = 0x21001, ///< Passthrough codec, AVFrames wrapped in AVPacket -+}; -+ -+/** -+ * This struct describes the properties of a single codec described by an -+ * AVCodecID. -+ * @see avcodec_descriptor_get() -+ */ -+typedef struct AVCodecDescriptor { -+ enum AVCodecID id; -+ enum AVMediaType type; -+ /** -+ * Name of the codec described by this descriptor. It is non-empty and -+ * unique for each codec descriptor. It should contain alphanumeric -+ * characters and '_' only. -+ */ -+ const char *name; -+ /** -+ * A more descriptive name for this codec. May be NULL. -+ */ -+ const char *long_name; -+ /** -+ * Codec properties, a combination of AV_CODEC_PROP_* flags. -+ */ -+ int props; -+ /** -+ * MIME type(s) associated with the codec. -+ * May be NULL; if not, a NULL-terminated array of MIME types. -+ * The first item is always non-NULL and is the preferred MIME type. -+ */ -+ const char *const *mime_types; -+ /** -+ * If non-NULL, an array of profiles recognized for this codec. -+ * Terminated with FF_PROFILE_UNKNOWN. -+ */ -+ const struct AVProfile *profiles; -+} AVCodecDescriptor; -+ -+/** -+ * Codec uses only intra compression. -+ * Video and audio codecs only. -+ */ -+#define AV_CODEC_PROP_INTRA_ONLY (1 << 0) -+/** -+ * Codec supports lossy compression. Audio and video codecs only. -+ * @note a codec may support both lossy and lossless -+ * compression modes -+ */ -+#define AV_CODEC_PROP_LOSSY (1 << 1) -+/** -+ * Codec supports lossless compression. Audio and video codecs only. -+ */ -+#define AV_CODEC_PROP_LOSSLESS (1 << 2) -+/** -+ * Codec supports frame reordering. That is, the coded order (the order in which -+ * the encoded packets are output by the encoders / stored / input to the -+ * decoders) may be different from the presentation order of the corresponding -+ * frames. -+ * -+ * For codecs that do not have this property set, PTS and DTS should always be -+ * equal. -+ */ -+#define AV_CODEC_PROP_REORDER (1 << 3) -+/** -+ * Subtitle codec is bitmap based -+ * Decoded AVSubtitle data can be read from the AVSubtitleRect->pict field. -+ */ -+#define AV_CODEC_PROP_BITMAP_SUB (1 << 16) -+/** -+ * Subtitle codec is text based. -+ * Decoded AVSubtitle data can be read from the AVSubtitleRect->ass field. -+ */ -+#define AV_CODEC_PROP_TEXT_SUB (1 << 17) -+ -+/** -+ * @ingroup lavc_decoding -+ * Required number of additionally allocated bytes at the end of the input bitstream for decoding. -+ * This is mainly needed because some optimized bitstream readers read -+ * 32 or 64 bit at once and could read over the end.
-+ * Note: If the first 23 bits of the additional bytes are not 0, then damaged -+ * MPEG bitstreams could cause overread and segfault. -+ */ -+#define AV_INPUT_BUFFER_PADDING_SIZE 64 -+ -+/** -+ * @ingroup lavc_encoding -+ * minimum encoding buffer size -+ * Used to avoid some checks during header writing. -+ */ -+#define AV_INPUT_BUFFER_MIN_SIZE 16384 -+ -+/** -+ * @ingroup lavc_decoding -+ */ -+enum AVDiscard{ -+ /* We leave some space between them for extensions (drop some -+ * keyframes for intra-only or drop just some bidir frames). */ -+ AVDISCARD_NONE =-16, ///< discard nothing -+ AVDISCARD_DEFAULT = 0, ///< discard useless packets like 0 size packets in avi -+ AVDISCARD_NONREF = 8, ///< discard all non reference -+ AVDISCARD_BIDIR = 16, ///< discard all bidirectional frames -+ AVDISCARD_NONINTRA= 24, ///< discard all non intra frames -+ AVDISCARD_NONKEY = 32, ///< discard all frames except keyframes -+ AVDISCARD_ALL = 48, ///< discard all -+}; -+ -+enum AVAudioServiceType { -+ AV_AUDIO_SERVICE_TYPE_MAIN = 0, -+ AV_AUDIO_SERVICE_TYPE_EFFECTS = 1, -+ AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED = 2, -+ AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED = 3, -+ AV_AUDIO_SERVICE_TYPE_DIALOGUE = 4, -+ AV_AUDIO_SERVICE_TYPE_COMMENTARY = 5, -+ AV_AUDIO_SERVICE_TYPE_EMERGENCY = 6, -+ AV_AUDIO_SERVICE_TYPE_VOICE_OVER = 7, -+ AV_AUDIO_SERVICE_TYPE_KARAOKE = 8, -+ AV_AUDIO_SERVICE_TYPE_NB , ///< Not part of ABI -+}; -+ -+/** -+ * @ingroup lavc_encoding -+ */ -+typedef struct RcOverride{ -+ int start_frame; -+ int end_frame; -+ int qscale; // If this is 0 then quality_factor will be used instead. -+ float quality_factor; -+} RcOverride; -+ -+/* encoding support -+ These flags can be passed in AVCodecContext.flags before initialization. -+ Note: Not everything is supported yet. -+*/ -+ -+/** -+ * Allow decoders to produce frames with data planes that are not aligned -+ * to CPU requirements (e.g. due to cropping). -+ */ -+#define AV_CODEC_FLAG_UNALIGNED (1 << 0) -+/** -+ * Use fixed qscale. -+ */ -+#define AV_CODEC_FLAG_QSCALE (1 << 1) -+/** -+ * 4 MV per MB allowed / advanced prediction for H.263. -+ */ -+#define AV_CODEC_FLAG_4MV (1 << 2) -+/** -+ * Output even those frames that might be corrupted. -+ */ -+#define AV_CODEC_FLAG_OUTPUT_CORRUPT (1 << 3) -+/** -+ * Use qpel MC. -+ */ -+#define AV_CODEC_FLAG_QPEL (1 << 4) -+/** -+ * Use internal 2pass ratecontrol in first pass mode. -+ */ -+#define AV_CODEC_FLAG_PASS1 (1 << 9) -+/** -+ * Use internal 2pass ratecontrol in second pass mode. -+ */ -+#define AV_CODEC_FLAG_PASS2 (1 << 10) -+/** -+ * loop filter. -+ */ -+#define AV_CODEC_FLAG_LOOP_FILTER (1 << 11) -+/** -+ * Only decode/encode grayscale. -+ */ -+#define AV_CODEC_FLAG_GRAY (1 << 13) -+/** -+ * error[?] variables will be set during encoding. -+ */ -+#define AV_CODEC_FLAG_PSNR (1 << 15) -+/** -+ * Input bitstream might be truncated at a random location -+ * instead of only at frame boundaries. -+ */ -+#define AV_CODEC_FLAG_TRUNCATED (1 << 16) -+/** -+ * Use interlaced DCT. -+ */ -+#define AV_CODEC_FLAG_INTERLACED_DCT (1 << 18) -+/** -+ * Force low delay. -+ */ -+#define AV_CODEC_FLAG_LOW_DELAY (1 << 19) -+/** -+ * Place global headers in extradata instead of every keyframe. -+ */ -+#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22) -+/** -+ * Use only bitexact stuff (except (I)DCT). -+ */ -+#define AV_CODEC_FLAG_BITEXACT (1 << 23) -+/* Fx : Flag for H.263+ extra options */ -+/** -+ * H.263 advanced intra coding / MPEG-4 AC prediction -+ */ -+#define AV_CODEC_FLAG_AC_PRED (1 << 24) -+/** -+ * interlaced motion estimation -+ */ -+#define AV_CODEC_FLAG_INTERLACED_ME (1 << 29) -+#define AV_CODEC_FLAG_CLOSED_GOP (1U << 31) -+ -+/** -+ * Allow non spec compliant speedup tricks. -+ */ -+#define AV_CODEC_FLAG2_FAST (1 << 0) -+/** -+ * Skip bitstream encoding. -+ */ -+#define AV_CODEC_FLAG2_NO_OUTPUT (1 << 2) -+/** -+ * Place global headers at every keyframe instead of in extradata. -+ */ -+#define AV_CODEC_FLAG2_LOCAL_HEADER (1 << 3) -+ -+/** -+ * timecode is in drop frame format. DEPRECATED!!!! -+ */ -+#define AV_CODEC_FLAG2_DROP_FRAME_TIMECODE (1 << 13) -+ -+/** -+ * Input bitstream might be truncated at a packet boundaries -+ * instead of only at frame boundaries. -+ */ -+#define AV_CODEC_FLAG2_CHUNKS (1 << 15) -+/** -+ * Discard cropping information from SPS. -+ */ -+#define AV_CODEC_FLAG2_IGNORE_CROP (1 << 16) -+ -+/** -+ * Show all frames before the first keyframe -+ */ -+#define AV_CODEC_FLAG2_SHOW_ALL (1 << 22) -+/** -+ * Export motion vectors through frame side data -+ */ -+#define AV_CODEC_FLAG2_EXPORT_MVS (1 << 28) -+/** -+ * Do not skip samples and export skip information as frame side data -+ */ -+#define AV_CODEC_FLAG2_SKIP_MANUAL (1 << 29) -+/** -+ * Do not reset ASS ReadOrder field on flush (subtitles decoding) -+ */ -+#define AV_CODEC_FLAG2_RO_FLUSH_NOOP (1 << 30) -+ -+/* Unsupported options : -+ * Syntax Arithmetic coding (SAC) -+ * Reference Picture Selection -+ * Independent Segment Decoding */ -+/* /Fx */ -+/* codec capabilities */ -+ -+/** -+ * Decoder can use draw_horiz_band callback. -+ */ -+#define AV_CODEC_CAP_DRAW_HORIZ_BAND (1 << 0) -+/** -+ * Codec uses get_buffer() for allocating buffers and supports custom allocators. -+ * If not set, it might not use get_buffer() at all or use operations that -+ * assume the buffer was allocated by avcodec_default_get_buffer. -+ */ -+#define AV_CODEC_CAP_DR1 (1 << 1) -+#define AV_CODEC_CAP_TRUNCATED (1 << 3) -+/** -+ * Encoder or decoder requires flushing with NULL input at the end in order to -+ * give the complete and correct output. -+ * -+ * NOTE: If this flag is not set, the codec is guaranteed to never be fed with -+ * with NULL data. The user can still send NULL data to the public encode -+ * or decode function, but libavcodec will not pass it along to the codec -+ * unless this flag is set. -+ * -+ * Decoders: -+ * The decoder has a non-zero delay and needs to be fed with avpkt->data=NULL, -+ * avpkt->size=0 at the end to get the delayed data until the decoder no longer -+ * returns frames. -+ * -+ * Encoders: -+ * The encoder needs to be fed with NULL data at the end of encoding until the -+ * encoder no longer returns data. -+ * -+ * NOTE: For encoders implementing the AVCodec.encode2() function, setting this -+ * flag also means that the encoder must set the pts and duration for -+ * each output packet. If this flag is not set, the pts and duration will -+ * be determined by libavcodec from the input frame. -+ */ -+#define AV_CODEC_CAP_DELAY (1 << 5) -+/** -+ * Codec can be fed a final frame with a smaller size. -+ * This can be used to prevent truncation of the last audio samples. -+ */ -+#define AV_CODEC_CAP_SMALL_LAST_FRAME (1 << 6) -+ -+/** -+ * Codec can output multiple frames per AVPacket -+ * Normally demuxers return one frame at a time, demuxers which do not do -+ * are connected to a parser to split what they return into proper frames. -+ * This flag is reserved to the very rare category of codecs which have a -+ * bitstream that cannot be split into frames without timeconsuming -+ * operations like full decoding. Demuxers carrying such bitstreams thus -+ * may return multiple frames in a packet. This has many disadvantages like -+ * prohibiting stream copy in many cases thus it should only be considered -+ * as a last resort. -+ */ -+#define AV_CODEC_CAP_SUBFRAMES (1 << 8) -+/** -+ * Codec is experimental and is thus avoided in favor of non experimental -+ * encoders -+ */ -+#define AV_CODEC_CAP_EXPERIMENTAL (1 << 9) -+/** -+ * Codec should fill in channel configuration and samplerate instead of container -+ */ -+#define AV_CODEC_CAP_CHANNEL_CONF (1 << 10) -+/** -+ * Codec supports frame-level multithreading. -+ */ -+#define AV_CODEC_CAP_FRAME_THREADS (1 << 12) -+/** -+ * Codec supports slice-based (or partition-based) multithreading. -+ */ -+#define AV_CODEC_CAP_SLICE_THREADS (1 << 13) -+/** -+ * Codec supports changed parameters at any point. -+ */ -+#define AV_CODEC_CAP_PARAM_CHANGE (1 << 14) -+/** -+ * Codec supports avctx->thread_count == 0 (auto). -+ */ -+#define AV_CODEC_CAP_AUTO_THREADS (1 << 15) -+/** -+ * Audio encoder supports receiving a different number of samples in each call. -+ */ -+#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE (1 << 16) -+/** -+ * Decoder is not a preferred choice for probing. -+ * This indicates that the decoder is not a good choice for probing. -+ * It could for example be an expensive to spin up hardware decoder, -+ * or it could simply not provide a lot of useful information about -+ * the stream. -+ * A decoder marked with this flag should only be used as last resort -+ * choice for probing. -+ */ -+#define AV_CODEC_CAP_AVOID_PROBING (1 << 17) -+/** -+ * Codec is intra only. -+ */ -+#define AV_CODEC_CAP_INTRA_ONLY 0x40000000 -+/** -+ * Codec is lossless. -+ */ -+#define AV_CODEC_CAP_LOSSLESS 0x80000000 -+ -+/** -+ * Codec is backed by a hardware implementation. Typically used to -+ * identify a non-hwaccel hardware decoder. For information about hwaccels, use -+ * avcodec_get_hw_config() instead. -+ */ -+#define AV_CODEC_CAP_HARDWARE (1 << 18) -+ -+/** -+ * Codec is potentially backed by a hardware implementation, but not -+ * necessarily. This is used instead of AV_CODEC_CAP_HARDWARE, if the -+ * implementation provides some sort of internal fallback. -+ */ -+#define AV_CODEC_CAP_HYBRID (1 << 19) -+ -+/** -+ * Pan Scan area. -+ * This specifies the area which should be displayed. -+ * Note there may be multiple such areas for one frame. -+ */ -+typedef struct AVPanScan { -+ /** -+ * id -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ int id; -+ -+ /** -+ * width and height in 1/16 pel -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ int width; -+ int height; -+ -+ /** -+ * position of the top left corner in 1/16 pel for up to 3 fields/frames -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ int16_t position[3][2]; -+} AVPanScan; -+ -+/** -+ * This structure describes the bitrate properties of an encoded bitstream. It -+ * roughly corresponds to a subset the VBV parameters for MPEG-2 or HRD -+ * parameters for H.264/HEVC. -+ */ -+typedef struct AVCPBProperties { -+ /** -+ * Maximum bitrate of the stream, in bits per second. -+ * Zero if unknown or unspecified. -+ */ -+ int max_bitrate; -+ /** -+ * Minimum bitrate of the stream, in bits per second. -+ * Zero if unknown or unspecified. -+ */ -+ int min_bitrate; -+ /** -+ * Average bitrate of the stream, in bits per second. -+ * Zero if unknown or unspecified. -+ */ -+ int avg_bitrate; -+ -+ /** -+ * The size of the buffer to which the ratecontrol is applied, in bits. -+ * Zero if unknown or unspecified. -+ */ -+ int buffer_size; -+ -+ /** -+ * The delay between the time the packet this structure is associated with -+ * is received and the time when it should be decoded, in periods of a 27MHz -+ * clock. -+ * -+ * UINT64_MAX when unknown or unspecified. -+ */ -+ uint64_t vbv_delay; -+} AVCPBProperties; -+ -+/** -+ * The decoder will keep a reference to the frame and may reuse it later. -+ */ -+#define AV_GET_BUFFER_FLAG_REF (1 << 0) -+ -+/** -+ * @defgroup lavc_packet AVPacket -+ * -+ * Types and functions for working with AVPacket. -+ * @{ -+ */ -+enum AVPacketSideDataType { -+ /** -+ * An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE -+ * bytes worth of palette. This side data signals that a new palette is -+ * present. -+ */ -+ AV_PKT_DATA_PALETTE, -+ -+ /** -+ * The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format -+ * that the extradata buffer was changed and the receiving side should -+ * act upon it appropriately. The new extradata is embedded in the side -+ * data buffer and should be immediately used for processing the current -+ * frame or packet. -+ */ -+ AV_PKT_DATA_NEW_EXTRADATA, -+ -+ /** -+ * An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows: -+ * @code -+ * u32le param_flags -+ * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) -+ * s32le channel_count -+ * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) -+ * u64le channel_layout -+ * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) -+ * s32le sample_rate -+ * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) -+ * s32le width -+ * s32le height -+ * @endcode -+ */ -+ AV_PKT_DATA_PARAM_CHANGE, -+ -+ /** -+ * An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of -+ * structures with info about macroblocks relevant to splitting the -+ * packet into smaller packets on macroblock edges (e.g. as for RFC 2190). -+ * That is, it does not necessarily contain info about all macroblocks, -+ * as long as the distance between macroblocks in the info is smaller -+ * than the target payload size. -+ * Each MB info structure is 12 bytes, and is laid out as follows: -+ * @code -+ * u32le bit offset from the start of the packet -+ * u8 current quantizer at the start of the macroblock -+ * u8 GOB number -+ * u16le macroblock address within the GOB -+ * u8 horizontal MV predictor -+ * u8 vertical MV predictor -+ * u8 horizontal MV predictor for block number 3 -+ * u8 vertical MV predictor for block number 3 -+ * @endcode -+ */ -+ AV_PKT_DATA_H263_MB_INFO, -+ -+ /** -+ * This side data should be associated with an audio stream and contains -+ * ReplayGain information in form of the AVReplayGain struct. -+ */ -+ AV_PKT_DATA_REPLAYGAIN, -+ -+ /** -+ * This side data contains a 3x3 transformation matrix describing an affine -+ * transformation that needs to be applied to the decoded video frames for -+ * correct presentation. -+ * -+ * See libavutil/display.h for a detailed description of the data. -+ */ -+ AV_PKT_DATA_DISPLAYMATRIX, -+ -+ /** -+ * This side data should be associated with a video stream and contains -+ * Stereoscopic 3D information in form of the AVStereo3D struct. -+ */ -+ AV_PKT_DATA_STEREO3D, -+ -+ /** -+ * This side data should be associated with an audio stream and corresponds -+ * to enum AVAudioServiceType. -+ */ -+ AV_PKT_DATA_AUDIO_SERVICE_TYPE, -+ -+ /** -+ * This side data contains quality related information from the encoder. -+ * @code -+ * u32le quality factor of the compressed frame. Allowed range is between 1 (good) and FF_LAMBDA_MAX (bad). -+ * u8 picture type -+ * u8 error count -+ * u16 reserved -+ * u64le[error count] sum of squared differences between encoder in and output -+ * @endcode -+ */ -+ AV_PKT_DATA_QUALITY_STATS, -+ -+ /** -+ * This side data contains an integer value representing the stream index -+ * of a "fallback" track. A fallback track indicates an alternate -+ * track to use when the current track can not be decoded for some reason. -+ * e.g. no decoder available for codec. -+ */ -+ AV_PKT_DATA_FALLBACK_TRACK, -+ -+ /** -+ * This side data corresponds to the AVCPBProperties struct. -+ */ -+ AV_PKT_DATA_CPB_PROPERTIES, -+ -+ /** -+ * Recommmends skipping the specified number of samples -+ * @code -+ * u32le number of samples to skip from start of this packet -+ * u32le number of samples to skip from end of this packet -+ * u8 reason for start skip -+ * u8 reason for end skip (0=padding silence, 1=convergence) -+ * @endcode -+ */ -+ AV_PKT_DATA_SKIP_SAMPLES, -+ -+ /** -+ * An AV_PKT_DATA_JP_DUALMONO side data packet indicates that -+ * the packet may contain "dual mono" audio specific to Japanese DTV -+ * and if it is true, recommends only the selected channel to be used. -+ * @code -+ * u8 selected channels (0=mail/left, 1=sub/right, 2=both) -+ * @endcode -+ */ -+ AV_PKT_DATA_JP_DUALMONO, -+ -+ /** -+ * A list of zero terminated key/value strings. There is no end marker for -+ * the list, so it is required to rely on the side data size to stop. -+ */ -+ AV_PKT_DATA_STRINGS_METADATA, -+ -+ /** -+ * Subtitle event position -+ * @code -+ * u32le x1 -+ * u32le y1 -+ * u32le x2 -+ * u32le y2 -+ * @endcode -+ */ -+ AV_PKT_DATA_SUBTITLE_POSITION, -+ -+ /** -+ * Data found in BlockAdditional element of matroska container. There is -+ * no end marker for the data, so it is required to rely on the side data -+ * size to recognize the end. 8 byte id (as found in BlockAddId) followed -+ * by data. -+ */ -+ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, -+ -+ /** -+ * The optional first identifier line of a WebVTT cue. -+ */ -+ AV_PKT_DATA_WEBVTT_IDENTIFIER, -+ -+ /** -+ * The optional settings (rendering instructions) that immediately -+ * follow the timestamp specifier of a WebVTT cue. -+ */ -+ AV_PKT_DATA_WEBVTT_SETTINGS, -+ -+ /** -+ * A list of zero terminated key/value strings. There is no end marker for -+ * the list, so it is required to rely on the side data size to stop. This -+ * side data includes updated metadata which appeared in the stream. -+ */ -+ AV_PKT_DATA_METADATA_UPDATE, -+ -+ /** -+ * MPEGTS stream ID, this is required to pass the stream ID -+ * information from the demuxer to the corresponding muxer. -+ */ -+ AV_PKT_DATA_MPEGTS_STREAM_ID, -+ -+ /** -+ * Mastering display metadata (based on SMPTE-2086:2014). This metadata -+ * should be associated with a video stream and contains data in the form -+ * of the AVMasteringDisplayMetadata struct. -+ */ -+ AV_PKT_DATA_MASTERING_DISPLAY_METADATA, -+ -+ /** -+ * This side data should be associated with a video stream and corresponds -+ * to the AVSphericalMapping structure. -+ */ -+ AV_PKT_DATA_SPHERICAL, -+ -+ /** -+ * Content light level (based on CTA-861.3). This metadata should be -+ * associated with a video stream and contains data in the form of the -+ * AVContentLightMetadata struct. -+ */ -+ AV_PKT_DATA_CONTENT_LIGHT_LEVEL, -+ -+ /** -+ * ATSC A53 Part 4 Closed Captions. This metadata should be associated with -+ * a video stream. A53 CC bitstream is stored as uint8_t in AVPacketSideData.data. -+ * The number of bytes of CC data is AVPacketSideData.size. -+ */ -+ AV_PKT_DATA_A53_CC, -+ -+ /** -+ * This side data is encryption initialization data. -+ * The format is not part of ABI, use av_encryption_init_info_* methods to -+ * access. -+ */ -+ AV_PKT_DATA_ENCRYPTION_INIT_INFO, -+ -+ /** -+ * This side data contains encryption info for how to decrypt the packet. -+ * The format is not part of ABI, use av_encryption_info_* methods to access. -+ */ -+ AV_PKT_DATA_ENCRYPTION_INFO, -+ -+ /** -+ * The number of side data types. -+ * This is not part of the public API/ABI in the sense that it may -+ * change when new side data types are added. -+ * This must stay the last enum value. -+ * If its value becomes huge, some code using it -+ * needs to be updated as it assumes it to be smaller than other limits. -+ */ -+ AV_PKT_DATA_NB -+}; -+ -+#define AV_PKT_DATA_QUALITY_FACTOR AV_PKT_DATA_QUALITY_STATS //DEPRECATED -+ -+typedef struct AVPacketSideData { -+ uint8_t *data; -+ int size; -+ enum AVPacketSideDataType type; -+} AVPacketSideData; -+ -+/** -+ * This structure stores compressed data. It is typically exported by demuxers -+ * and then passed as input to decoders, or received as output from encoders and -+ * then passed to muxers. -+ * -+ * For video, it should typically contain one compressed frame. For audio it may -+ * contain several compressed frames. Encoders are allowed to output empty -+ * packets, with no compressed data, containing only side data -+ * (e.g. to update some stream parameters at the end of encoding). -+ * -+ * AVPacket is one of the few structs in FFmpeg, whose size is a part of public -+ * ABI. Thus it may be allocated on stack and no new fields can be added to it -+ * without libavcodec and libavformat major bump. -+ * -+ * The semantics of data ownership depends on the buf field. -+ * If it is set, the packet data is dynamically allocated and is -+ * valid indefinitely until a call to av_packet_unref() reduces the -+ * reference count to 0. -+ * -+ * If the buf field is not set av_packet_ref() would make a copy instead -+ * of increasing the reference count. -+ * -+ * The side data is always allocated with av_malloc(), copied by -+ * av_packet_ref() and freed by av_packet_unref(). -+ * -+ * @see av_packet_ref -+ * @see av_packet_unref -+ */ -+typedef struct AVPacket { -+ /** -+ * A reference to the reference-counted buffer where the packet data is -+ * stored. -+ * May be NULL, then the packet data is not reference-counted. -+ */ -+ AVBufferRef *buf; -+ /** -+ * Presentation timestamp in AVStream->time_base units; the time at which -+ * the decompressed packet will be presented to the user. -+ * Can be AV_NOPTS_VALUE if it is not stored in the file. -+ * pts MUST be larger or equal to dts as presentation cannot happen before -+ * decompression, unless one wants to view hex dumps. Some formats misuse -+ * the terms dts and pts/cts to mean something different. Such timestamps -+ * must be converted to true pts/dts before they are stored in AVPacket. -+ */ -+ int64_t pts; -+ /** -+ * Decompression timestamp in AVStream->time_base units; the time at which -+ * the packet is decompressed. -+ * Can be AV_NOPTS_VALUE if it is not stored in the file. -+ */ -+ int64_t dts; -+ uint8_t *data; -+ int size; -+ int stream_index; -+ /** -+ * A combination of AV_PKT_FLAG values -+ */ -+ int flags; -+ /** -+ * Additional packet data that can be provided by the container. -+ * Packet can contain several types of side information. -+ */ -+ AVPacketSideData *side_data; -+ int side_data_elems; -+ -+ /** -+ * Duration of this packet in AVStream->time_base units, 0 if unknown. -+ * Equals next_pts - this_pts in presentation order. -+ */ -+ int64_t duration; -+ -+ int64_t pos; ///< byte position in stream, -1 if unknown -+ -+#if FF_API_CONVERGENCE_DURATION -+ /** -+ * @deprecated Same as the duration field, but as int64_t. This was required -+ * for Matroska subtitles, whose duration values could overflow when the -+ * duration field was still an int. -+ */ -+ attribute_deprecated -+ int64_t convergence_duration; -+#endif -+} AVPacket; -+#define AV_PKT_FLAG_KEY 0x0001 ///< The packet contains a keyframe -+#define AV_PKT_FLAG_CORRUPT 0x0002 ///< The packet content is corrupted -+/** -+ * Flag is used to discard packets which are required to maintain valid -+ * decoder state but are not required for output and should be dropped -+ * after decoding. -+ **/ -+#define AV_PKT_FLAG_DISCARD 0x0004 -+/** -+ * The packet comes from a trusted source. -+ * -+ * Otherwise-unsafe constructs such as arbitrary pointers to data -+ * outside the packet may be followed. -+ */ -+#define AV_PKT_FLAG_TRUSTED 0x0008 -+/** -+ * Flag is used to indicate packets that contain frames that can -+ * be discarded by the decoder. I.e. Non-reference frames. -+ */ -+#define AV_PKT_FLAG_DISPOSABLE 0x0010 -+ -+ -+enum AVSideDataParamChangeFlags { -+ AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT = 0x0001, -+ AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT = 0x0002, -+ AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE = 0x0004, -+ AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS = 0x0008, -+}; -+/** -+ * @} -+ */ -+ -+struct AVCodecInternal; -+ -+enum AVFieldOrder { -+ AV_FIELD_UNKNOWN, -+ AV_FIELD_PROGRESSIVE, -+ AV_FIELD_TT, //< Top coded_first, top displayed first -+ AV_FIELD_BB, //< Bottom coded first, bottom displayed first -+ AV_FIELD_TB, //< Top coded first, bottom displayed first -+ AV_FIELD_BT, //< Bottom coded first, top displayed first -+}; -+ -+/** -+ * main external API structure. -+ * New fields can be added to the end with minor version bumps. -+ * Removal, reordering and changes to existing fields require a major -+ * version bump. -+ * You can use AVOptions (av_opt* / av_set/get*()) to access these fields from user -+ * applications. -+ * The name string for AVOptions options matches the associated command line -+ * parameter name and can be found in libavcodec/options_table.h -+ * The AVOption/command line parameter names differ in some cases from the C -+ * structure field names for historic reasons or brevity. -+ * sizeof(AVCodecContext) must not be used outside libav*. -+ */ -+typedef struct AVCodecContext { -+ /** -+ * information on struct for av_log -+ * - set by avcodec_alloc_context3 -+ */ -+ const AVClass *av_class; -+ int log_level_offset; -+ -+ enum AVMediaType codec_type; /* see AVMEDIA_TYPE_xxx */ -+ const struct AVCodec *codec; -+ enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */ -+ -+ /** -+ * fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A'). -+ * This is used to work around some encoder bugs. -+ * A demuxer should set this to what is stored in the field used to identify the codec. -+ * If there are multiple such fields in a container then the demuxer should choose the one -+ * which maximizes the information about the used codec. -+ * If the codec tag field in a container is larger than 32 bits then the demuxer should -+ * remap the longer ID to 32 bits with a table or other structure. Alternatively a new -+ * extra_codec_tag + size could be added but for this a clear advantage must be demonstrated -+ * first. -+ * - encoding: Set by user, if not then the default based on codec_id will be used. -+ * - decoding: Set by user, will be converted to uppercase by libavcodec during init. -+ */ -+ unsigned int codec_tag; -+ -+ void *priv_data; -+ -+ /** -+ * Private context used for internal data. -+ * -+ * Unlike priv_data, this is not codec-specific. It is used in general -+ * libavcodec functions. -+ */ -+ struct AVCodecInternal *internal; -+ -+ /** -+ * Private data of the user, can be used to carry app specific stuff. -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ void *opaque; -+ -+ /** -+ * the average bitrate -+ * - encoding: Set by user; unused for constant quantizer encoding. -+ * - decoding: Set by user, may be overwritten by libavcodec -+ * if this info is available in the stream -+ */ -+ int64_t bit_rate; -+ -+ /** -+ * number of bits the bitstream is allowed to diverge from the reference. -+ * the reference can be CBR (for CBR pass1) or VBR (for pass2) -+ * - encoding: Set by user; unused for constant quantizer encoding. -+ * - decoding: unused -+ */ -+ int bit_rate_tolerance; -+ -+ /** -+ * Global quality for codecs which cannot change it per frame. -+ * This should be proportional to MPEG-1/2/4 qscale. -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int global_quality; -+ -+ /** -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int compression_level; -+#define FF_COMPRESSION_DEFAULT -1 -+ -+ /** -+ * AV_CODEC_FLAG_*. -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int flags; -+ -+ /** -+ * AV_CODEC_FLAG2_* -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int flags2; -+ -+ /** -+ * some codecs need / can use extradata like Huffman tables. -+ * MJPEG: Huffman tables -+ * rv10: additional flags -+ * MPEG-4: global headers (they can be in the bitstream or here) -+ * The allocated memory should be AV_INPUT_BUFFER_PADDING_SIZE bytes larger -+ * than extradata_size to avoid problems if it is read with the bitstream reader. -+ * The bytewise contents of extradata must not depend on the architecture or CPU endianness. -+ * - encoding: Set/allocated/freed by libavcodec. -+ * - decoding: Set/allocated/freed by user. -+ */ -+ uint8_t *extradata; -+ int extradata_size; -+ -+ /** -+ * This is the fundamental unit of time (in seconds) in terms -+ * of which frame timestamps are represented. For fixed-fps content, -+ * timebase should be 1/framerate and timestamp increments should be -+ * identically 1. -+ * This often, but not always is the inverse of the frame rate or field rate -+ * for video. 1/time_base is not the average frame rate if the frame rate is not -+ * constant. -+ * -+ * Like containers, elementary streams also can store timestamps, 1/time_base -+ * is the unit in which these timestamps are specified. -+ * As example of such codec time base see ISO/IEC 14496-2:2001(E) -+ * vop_time_increment_resolution and fixed_vop_rate -+ * (fixed_vop_rate == 0 implies that it is different from the framerate) -+ * -+ * - encoding: MUST be set by user. -+ * - decoding: the use of this field for decoding is deprecated. -+ * Use framerate instead. -+ */ -+ AVRational time_base; -+ -+ /** -+ * For some codecs, the time base is closer to the field rate than the frame rate. -+ * Most notably, H.264 and MPEG-2 specify time_base as half of frame duration -+ * if no telecine is used ... -+ * -+ * Set to time_base ticks per frame. Default 1, e.g., H.264/MPEG-2 set it to 2. -+ */ -+ int ticks_per_frame; -+ -+ /** -+ * Codec delay. -+ * -+ * Encoding: Number of frames delay there will be from the encoder input to -+ * the decoder output. (we assume the decoder matches the spec) -+ * Decoding: Number of frames delay in addition to what a standard decoder -+ * as specified in the spec would produce. -+ * -+ * Video: -+ * Number of frames the decoded output will be delayed relative to the -+ * encoded input. -+ * -+ * Audio: -+ * For encoding, this field is unused (see initial_padding). -+ * -+ * For decoding, this is the number of samples the decoder needs to -+ * output before the decoder's output is valid. When seeking, you should -+ * start decoding this many samples prior to your desired seek point. -+ * -+ * - encoding: Set by libavcodec. -+ * - decoding: Set by libavcodec. -+ */ -+ int delay; -+ -+ -+ /* video only */ -+ /** -+ * picture width / height. -+ * -+ * @note Those fields may not match the values of the last -+ * AVFrame output by avcodec_decode_video2 due frame -+ * reordering. -+ * -+ * - encoding: MUST be set by user. -+ * - decoding: May be set by the user before opening the decoder if known e.g. -+ * from the container. Some decoders will require the dimensions -+ * to be set by the caller. During decoding, the decoder may -+ * overwrite those values as required while parsing the data. -+ */ -+ int width, height; -+ -+ /** -+ * Bitstream width / height, may be different from width/height e.g. when -+ * the decoded frame is cropped before being output or lowres is enabled. -+ * -+ * @note Those field may not match the value of the last -+ * AVFrame output by avcodec_receive_frame() due frame -+ * reordering. -+ * -+ * - encoding: unused -+ * - decoding: May be set by the user before opening the decoder if known -+ * e.g. from the container. During decoding, the decoder may -+ * overwrite those values as required while parsing the data. -+ */ -+ int coded_width, coded_height; -+ -+ /** -+ * the number of pictures in a group of pictures, or 0 for intra_only -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int gop_size; -+ -+ /** -+ * Pixel format, see AV_PIX_FMT_xxx. -+ * May be set by the demuxer if known from headers. -+ * May be overridden by the decoder if it knows better. -+ * -+ * @note This field may not match the value of the last -+ * AVFrame output by avcodec_receive_frame() due frame -+ * reordering. -+ * -+ * - encoding: Set by user. -+ * - decoding: Set by user if known, overridden by libavcodec while -+ * parsing the data. -+ */ -+ enum AVPixelFormat pix_fmt; -+ -+ /** -+ * If non NULL, 'draw_horiz_band' is called by the libavcodec -+ * decoder to draw a horizontal band. It improves cache usage. Not -+ * all codecs can do that. You must check the codec capabilities -+ * beforehand. -+ * When multithreading is used, it may be called from multiple threads -+ * at the same time; threads might draw different parts of the same AVFrame, -+ * or multiple AVFrames, and there is no guarantee that slices will be drawn -+ * in order. -+ * The function is also used by hardware acceleration APIs. -+ * It is called at least once during frame decoding to pass -+ * the data needed for hardware render. -+ * In that mode instead of pixel data, AVFrame points to -+ * a structure specific to the acceleration API. The application -+ * reads the structure and can change some fields to indicate progress -+ * or mark state. -+ * - encoding: unused -+ * - decoding: Set by user. -+ * @param height the height of the slice -+ * @param y the y position of the slice -+ * @param type 1->top field, 2->bottom field, 3->frame -+ * @param offset offset into the AVFrame.data from which the slice should be read -+ */ -+ void (*draw_horiz_band)(struct AVCodecContext *s, -+ const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], -+ int y, int type, int height); -+ -+ /** -+ * callback to negotiate the pixelFormat -+ * @param fmt is the list of formats which are supported by the codec, -+ * it is terminated by -1 as 0 is a valid format, the formats are ordered by quality. -+ * The first is always the native one. -+ * @note The callback may be called again immediately if initialization for -+ * the selected (hardware-accelerated) pixel format failed. -+ * @warning Behavior is undefined if the callback returns a value not -+ * in the fmt list of formats. -+ * @return the chosen format -+ * - encoding: unused -+ * - decoding: Set by user, if not set the native format will be chosen. -+ */ -+ enum AVPixelFormat (*get_format)(struct AVCodecContext *s, const enum AVPixelFormat * fmt); -+ -+ /** -+ * maximum number of B-frames between non-B-frames -+ * Note: The output will be delayed by max_b_frames+1 relative to the input. -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int max_b_frames; -+ -+ /** -+ * qscale factor between IP and B-frames -+ * If > 0 then the last P-frame quantizer will be used (q= lastp_q*factor+offset). -+ * If < 0 then normal ratecontrol will be done (q= -normal_q*factor+offset). -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float b_quant_factor; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int b_frame_strategy; -+#endif -+ -+ /** -+ * qscale offset between IP and B-frames -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float b_quant_offset; -+ -+ /** -+ * Size of the frame reordering buffer in the decoder. -+ * For MPEG-2 it is 1 IPB or 0 low delay IP. -+ * - encoding: Set by libavcodec. -+ * - decoding: Set by libavcodec. -+ */ -+ int has_b_frames; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int mpeg_quant; -+#endif -+ -+ /** -+ * qscale factor between P- and I-frames -+ * If > 0 then the last P-frame quantizer will be used (q = lastp_q * factor + offset). -+ * If < 0 then normal ratecontrol will be done (q= -normal_q*factor+offset). -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float i_quant_factor; -+ -+ /** -+ * qscale offset between P and I-frames -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float i_quant_offset; -+ -+ /** -+ * luminance masking (0-> disabled) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float lumi_masking; -+ -+ /** -+ * temporary complexity masking (0-> disabled) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float temporal_cplx_masking; -+ -+ /** -+ * spatial complexity masking (0-> disabled) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float spatial_cplx_masking; -+ -+ /** -+ * p block masking (0-> disabled) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float p_masking; -+ -+ /** -+ * darkness masking (0-> disabled) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ float dark_masking; -+ -+ /** -+ * slice count -+ * - encoding: Set by libavcodec. -+ * - decoding: Set by user (or 0). -+ */ -+ int slice_count; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int prediction_method; -+#define FF_PRED_LEFT 0 -+#define FF_PRED_PLANE 1 -+#define FF_PRED_MEDIAN 2 -+#endif -+ -+ /** -+ * slice offsets in the frame in bytes -+ * - encoding: Set/allocated by libavcodec. -+ * - decoding: Set/allocated by user (or NULL). -+ */ -+ int *slice_offset; -+ -+ /** -+ * sample aspect ratio (0 if unknown) -+ * That is the width of a pixel divided by the height of the pixel. -+ * Numerator and denominator must be relatively prime and smaller than 256 for some video standards. -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ AVRational sample_aspect_ratio; -+ -+ /** -+ * motion estimation comparison function -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int me_cmp; -+ /** -+ * subpixel motion estimation comparison function -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int me_sub_cmp; -+ /** -+ * macroblock comparison function (not supported yet) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int mb_cmp; -+ /** -+ * interlaced DCT comparison function -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int ildct_cmp; -+#define FF_CMP_SAD 0 -+#define FF_CMP_SSE 1 -+#define FF_CMP_SATD 2 -+#define FF_CMP_DCT 3 -+#define FF_CMP_PSNR 4 -+#define FF_CMP_BIT 5 -+#define FF_CMP_RD 6 -+#define FF_CMP_ZERO 7 -+#define FF_CMP_VSAD 8 -+#define FF_CMP_VSSE 9 -+#define FF_CMP_NSSE 10 -+#define FF_CMP_W53 11 -+#define FF_CMP_W97 12 -+#define FF_CMP_DCTMAX 13 -+#define FF_CMP_DCT264 14 -+#define FF_CMP_MEDIAN_SAD 15 -+#define FF_CMP_CHROMA 256 -+ -+ /** -+ * ME diamond size & shape -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int dia_size; -+ -+ /** -+ * amount of previous MV predictors (2a+1 x 2a+1 square) -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int last_predictor_count; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int pre_me; -+#endif -+ -+ /** -+ * motion estimation prepass comparison function -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int me_pre_cmp; -+ -+ /** -+ * ME prepass diamond size & shape -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int pre_dia_size; -+ -+ /** -+ * subpel ME quality -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int me_subpel_quality; -+ -+ /** -+ * maximum motion estimation search range in subpel units -+ * If 0 then no limit. -+ * -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int me_range; -+ -+ /** -+ * slice flags -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int slice_flags; -+#define SLICE_FLAG_CODED_ORDER 0x0001 ///< draw_horiz_band() is called in coded order instead of display -+#define SLICE_FLAG_ALLOW_FIELD 0x0002 ///< allow draw_horiz_band() with field slices (MPEG-2 field pics) -+#define SLICE_FLAG_ALLOW_PLANE 0x0004 ///< allow draw_horiz_band() with 1 component at a time (SVQ1) -+ -+ /** -+ * macroblock decision mode -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int mb_decision; -+#define FF_MB_DECISION_SIMPLE 0 ///< uses mb_cmp -+#define FF_MB_DECISION_BITS 1 ///< chooses the one which needs the fewest bits -+#define FF_MB_DECISION_RD 2 ///< rate distortion -+ -+ /** -+ * custom intra quantization matrix -+ * - encoding: Set by user, can be NULL. -+ * - decoding: Set by libavcodec. -+ */ -+ uint16_t *intra_matrix; -+ -+ /** -+ * custom inter quantization matrix -+ * - encoding: Set by user, can be NULL. -+ * - decoding: Set by libavcodec. -+ */ -+ uint16_t *inter_matrix; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int scenechange_threshold; -+ -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int noise_reduction; -+#endif -+ -+ /** -+ * precision of the intra DC coefficient - 8 -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec -+ */ -+ int intra_dc_precision; -+ -+ /** -+ * Number of macroblock rows at the top which are skipped. -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int skip_top; -+ -+ /** -+ * Number of macroblock rows at the bottom which are skipped. -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int skip_bottom; -+ -+ /** -+ * minimum MB Lagrange multiplier -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int mb_lmin; -+ -+ /** -+ * maximum MB Lagrange multiplier -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int mb_lmax; -+ -+#if FF_API_PRIVATE_OPT -+ /** -+ * @deprecated use encoder private options instead -+ */ -+ attribute_deprecated -+ int me_penalty_compensation; -+#endif -+ -+ /** -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int bidir_refine; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int brd_scale; -+#endif -+ -+ /** -+ * minimum GOP size -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int keyint_min; -+ -+ /** -+ * number of reference frames -+ * - encoding: Set by user. -+ * - decoding: Set by lavc. -+ */ -+ int refs; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int chromaoffset; -+#endif -+ -+ /** -+ * Note: Value depends upon the compare function used for fullpel ME. -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int mv0_threshold; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int b_sensitivity; -+#endif -+ -+ /** -+ * Chromaticity coordinates of the source primaries. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVColorPrimaries color_primaries; -+ -+ /** -+ * Color Transfer Characteristic. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVColorTransferCharacteristic color_trc; -+ -+ /** -+ * YUV colorspace type. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVColorSpace colorspace; -+ -+ /** -+ * MPEG vs JPEG YUV range. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVColorRange color_range; -+ -+ /** -+ * This defines the location of chroma samples. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVChromaLocation chroma_sample_location; -+ -+ /** -+ * Number of slices. -+ * Indicates number of picture subdivisions. Used for parallelized -+ * decoding. -+ * - encoding: Set by user -+ * - decoding: unused -+ */ -+ int slices; -+ -+ /** Field order -+ * - encoding: set by libavcodec -+ * - decoding: Set by user. -+ */ -+ enum AVFieldOrder field_order; -+ -+ /* audio only */ -+ int sample_rate; ///< samples per second -+ int channels; ///< number of audio channels -+ -+ /** -+ * audio sample format -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ enum AVSampleFormat sample_fmt; ///< sample format -+ -+ /* The following data should not be initialized. */ -+ /** -+ * Number of samples per channel in an audio frame. -+ * -+ * - encoding: set by libavcodec in avcodec_open2(). Each submitted frame -+ * except the last must contain exactly frame_size samples per channel. -+ * May be 0 when the codec has AV_CODEC_CAP_VARIABLE_FRAME_SIZE set, then the -+ * frame size is not restricted. -+ * - decoding: may be set by some decoders to indicate constant frame size -+ */ -+ int frame_size; -+ -+ /** -+ * Frame counter, set by libavcodec. -+ * -+ * - decoding: total number of frames returned from the decoder so far. -+ * - encoding: total number of frames passed to the encoder so far. -+ * -+ * @note the counter is not incremented if encoding/decoding resulted in -+ * an error. -+ */ -+ int frame_number; -+ -+ /** -+ * number of bytes per packet if constant and known or 0 -+ * Used by some WAV based audio codecs. -+ */ -+ int block_align; -+ -+ /** -+ * Audio cutoff bandwidth (0 means "automatic") -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int cutoff; -+ -+ /** -+ * Audio channel layout. -+ * - encoding: set by user. -+ * - decoding: set by user, may be overwritten by libavcodec. -+ */ -+ uint64_t channel_layout; -+ -+ /** -+ * Request decoder to use this channel layout if it can (0 for default) -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ uint64_t request_channel_layout; -+ -+ /** -+ * Type of service that the audio stream conveys. -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ enum AVAudioServiceType audio_service_type; -+ -+ /** -+ * desired sample format -+ * - encoding: Not used. -+ * - decoding: Set by user. -+ * Decoder will decode to this format if it can. -+ */ -+ enum AVSampleFormat request_sample_fmt; -+ -+ /** -+ * This callback is called at the beginning of each frame to get data -+ * buffer(s) for it. There may be one contiguous buffer for all the data or -+ * there may be a buffer per each data plane or anything in between. What -+ * this means is, you may set however many entries in buf[] you feel necessary. -+ * Each buffer must be reference-counted using the AVBuffer API (see description -+ * of buf[] below). -+ * -+ * The following fields will be set in the frame before this callback is -+ * called: -+ * - format -+ * - width, height (video only) -+ * - sample_rate, channel_layout, nb_samples (audio only) -+ * Their values may differ from the corresponding values in -+ * AVCodecContext. This callback must use the frame values, not the codec -+ * context values, to calculate the required buffer size. -+ * -+ * This callback must fill the following fields in the frame: -+ * - data[] -+ * - linesize[] -+ * - extended_data: -+ * * if the data is planar audio with more than 8 channels, then this -+ * callback must allocate and fill extended_data to contain all pointers -+ * to all data planes. data[] must hold as many pointers as it can. -+ * extended_data must be allocated with av_malloc() and will be freed in -+ * av_frame_unref(). -+ * * otherwise extended_data must point to data -+ * - buf[] must contain one or more pointers to AVBufferRef structures. Each of -+ * the frame's data and extended_data pointers must be contained in these. That -+ * is, one AVBufferRef for each allocated chunk of memory, not necessarily one -+ * AVBufferRef per data[] entry. See: av_buffer_create(), av_buffer_alloc(), -+ * and av_buffer_ref(). -+ * - extended_buf and nb_extended_buf must be allocated with av_malloc() by -+ * this callback and filled with the extra buffers if there are more -+ * buffers than buf[] can hold. extended_buf will be freed in -+ * av_frame_unref(). -+ * -+ * If AV_CODEC_CAP_DR1 is not set then get_buffer2() must call -+ * avcodec_default_get_buffer2() instead of providing buffers allocated by -+ * some other means. -+ * -+ * Each data plane must be aligned to the maximum required by the target -+ * CPU. -+ * -+ * @see avcodec_default_get_buffer2() -+ * -+ * Video: -+ * -+ * If AV_GET_BUFFER_FLAG_REF is set in flags then the frame may be reused -+ * (read and/or written to if it is writable) later by libavcodec. -+ * -+ * avcodec_align_dimensions2() should be used to find the required width and -+ * height, as they normally need to be rounded up to the next multiple of 16. -+ * -+ * Some decoders do not support linesizes changing between frames. -+ * -+ * If frame multithreading is used and thread_safe_callbacks is set, -+ * this callback may be called from a different thread, but not from more -+ * than one at once. Does not need to be reentrant. -+ * -+ * @see avcodec_align_dimensions2() -+ * -+ * Audio: -+ * -+ * Decoders request a buffer of a particular size by setting -+ * AVFrame.nb_samples prior to calling get_buffer2(). The decoder may, -+ * however, utilize only part of the buffer by setting AVFrame.nb_samples -+ * to a smaller value in the output frame. -+ * -+ * As a convenience, av_samples_get_buffer_size() and -+ * av_samples_fill_arrays() in libavutil may be used by custom get_buffer2() -+ * functions to find the required data size and to fill data pointers and -+ * linesize. In AVFrame.linesize, only linesize[0] may be set for audio -+ * since all planes must be the same size. -+ * -+ * @see av_samples_get_buffer_size(), av_samples_fill_arrays() -+ * -+ * - encoding: unused -+ * - decoding: Set by libavcodec, user can override. -+ */ -+ int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags); -+ -+ /** -+ * If non-zero, the decoded audio and video frames returned from -+ * avcodec_decode_video2() and avcodec_decode_audio4() are reference-counted -+ * and are valid indefinitely. The caller must free them with -+ * av_frame_unref() when they are not needed anymore. -+ * Otherwise, the decoded frames must not be freed by the caller and are -+ * only valid until the next decode call. -+ * -+ * This is always automatically enabled if avcodec_receive_frame() is used. -+ * -+ * - encoding: unused -+ * - decoding: set by the caller before avcodec_open2(). -+ */ -+ attribute_deprecated -+ int refcounted_frames; -+ -+ /* - encoding parameters */ -+ float qcompress; ///< amount of qscale change between easy & hard scenes (0.0-1.0) -+ float qblur; ///< amount of qscale smoothing over time (0.0-1.0) -+ -+ /** -+ * minimum quantizer -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int qmin; -+ -+ /** -+ * maximum quantizer -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int qmax; -+ -+ /** -+ * maximum quantizer difference between frames -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int max_qdiff; -+ -+ /** -+ * decoder bitstream buffer size -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int rc_buffer_size; -+ -+ /** -+ * ratecontrol override, see RcOverride -+ * - encoding: Allocated/set/freed by user. -+ * - decoding: unused -+ */ -+ int rc_override_count; -+ RcOverride *rc_override; -+ -+ /** -+ * maximum bitrate -+ * - encoding: Set by user. -+ * - decoding: Set by user, may be overwritten by libavcodec. -+ */ -+ int64_t rc_max_rate; -+ -+ /** -+ * minimum bitrate -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int64_t rc_min_rate; -+ -+ /** -+ * Ratecontrol attempt to use, at maximum, of what can be used without an underflow. -+ * - encoding: Set by user. -+ * - decoding: unused. -+ */ -+ float rc_max_available_vbv_use; -+ -+ /** -+ * Ratecontrol attempt to use, at least, times the amount needed to prevent a vbv overflow. -+ * - encoding: Set by user. -+ * - decoding: unused. -+ */ -+ float rc_min_vbv_overflow_use; -+ -+ /** -+ * Number of bits which should be loaded into the rc buffer before decoding starts. -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int rc_initial_buffer_occupancy; -+ -+#if FF_API_CODER_TYPE -+#define FF_CODER_TYPE_VLC 0 -+#define FF_CODER_TYPE_AC 1 -+#define FF_CODER_TYPE_RAW 2 -+#define FF_CODER_TYPE_RLE 3 -+ /** -+ * @deprecated use encoder private options instead -+ */ -+ attribute_deprecated -+ int coder_type; -+#endif /* FF_API_CODER_TYPE */ -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int context_model; -+#endif -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int frame_skip_threshold; -+ -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int frame_skip_factor; -+ -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int frame_skip_exp; -+ -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int frame_skip_cmp; -+#endif /* FF_API_PRIVATE_OPT */ -+ -+ /** -+ * trellis RD quantization -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int trellis; -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int min_prediction_order; -+ -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int max_prediction_order; -+ -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int64_t timecode_frame_start; -+#endif -+ -+#if FF_API_RTP_CALLBACK -+ /** -+ * @deprecated unused -+ */ -+ /* The RTP callback: This function is called */ -+ /* every time the encoder has a packet to send. */ -+ /* It depends on the encoder if the data starts */ -+ /* with a Start Code (it should). H.263 does. */ -+ /* mb_nb contains the number of macroblocks */ -+ /* encoded in the RTP payload. */ -+ attribute_deprecated -+ void (*rtp_callback)(struct AVCodecContext *avctx, void *data, int size, int mb_nb); -+#endif -+ -+#if FF_API_PRIVATE_OPT -+ /** @deprecated use encoder private options instead */ -+ attribute_deprecated -+ int rtp_payload_size; /* The size of the RTP payload: the coder will */ -+ /* do its best to deliver a chunk with size */ -+ /* below rtp_payload_size, the chunk will start */ -+ /* with a start code on some codecs like H.263. */ -+ /* This doesn't take account of any particular */ -+ /* headers inside the transmitted RTP payload. */ -+#endif -+ -+#if FF_API_STAT_BITS -+ /* statistics, used for 2-pass encoding */ -+ attribute_deprecated -+ int mv_bits; -+ attribute_deprecated -+ int header_bits; -+ attribute_deprecated -+ int i_tex_bits; -+ attribute_deprecated -+ int p_tex_bits; -+ attribute_deprecated -+ int i_count; -+ attribute_deprecated -+ int p_count; -+ attribute_deprecated -+ int skip_count; -+ attribute_deprecated -+ int misc_bits; -+ -+ /** @deprecated this field is unused */ -+ attribute_deprecated -+ int frame_bits; -+#endif -+ -+ /** -+ * pass1 encoding statistics output buffer -+ * - encoding: Set by libavcodec. -+ * - decoding: unused -+ */ -+ char *stats_out; -+ -+ /** -+ * pass2 encoding statistics input buffer -+ * Concatenated stuff from stats_out of pass1 should be placed here. -+ * - encoding: Allocated/set/freed by user. -+ * - decoding: unused -+ */ -+ char *stats_in; -+ -+ /** -+ * Work around bugs in encoders which sometimes cannot be detected automatically. -+ * - encoding: Set by user -+ * - decoding: Set by user -+ */ -+ int workaround_bugs; -+#define FF_BUG_AUTODETECT 1 ///< autodetection -+#define FF_BUG_XVID_ILACE 4 -+#define FF_BUG_UMP4 8 -+#define FF_BUG_NO_PADDING 16 -+#define FF_BUG_AMV 32 -+#define FF_BUG_QPEL_CHROMA 64 -+#define FF_BUG_STD_QPEL 128 -+#define FF_BUG_QPEL_CHROMA2 256 -+#define FF_BUG_DIRECT_BLOCKSIZE 512 -+#define FF_BUG_EDGE 1024 -+#define FF_BUG_HPEL_CHROMA 2048 -+#define FF_BUG_DC_CLIP 4096 -+#define FF_BUG_MS 8192 ///< Work around various bugs in Microsoft's broken decoders. -+#define FF_BUG_TRUNCATED 16384 -+#define FF_BUG_IEDGE 32768 -+ -+ /** -+ * strictly follow the standard (MPEG-4, ...). -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ * Setting this to STRICT or higher means the encoder and decoder will -+ * generally do stupid things, whereas setting it to unofficial or lower -+ * will mean the encoder might produce output that is not supported by all -+ * spec-compliant decoders. Decoders don't differentiate between normal, -+ * unofficial and experimental (that is, they always try to decode things -+ * when they can) unless they are explicitly asked to behave stupidly -+ * (=strictly conform to the specs) -+ */ -+ int strict_std_compliance; -+#define FF_COMPLIANCE_VERY_STRICT 2 ///< Strictly conform to an older more strict version of the spec or reference software. -+#define FF_COMPLIANCE_STRICT 1 ///< Strictly conform to all the things in the spec no matter what consequences. -+#define FF_COMPLIANCE_NORMAL 0 -+#define FF_COMPLIANCE_UNOFFICIAL -1 ///< Allow unofficial extensions -+#define FF_COMPLIANCE_EXPERIMENTAL -2 ///< Allow nonstandardized experimental things. -+ -+ /** -+ * error concealment flags -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int error_concealment; -+#define FF_EC_GUESS_MVS 1 -+#define FF_EC_DEBLOCK 2 -+#define FF_EC_FAVOR_INTER 256 -+ -+ /** -+ * debug -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int debug; -+#define FF_DEBUG_PICT_INFO 1 -+#define FF_DEBUG_RC 2 -+#define FF_DEBUG_BITSTREAM 4 -+#define FF_DEBUG_MB_TYPE 8 -+#define FF_DEBUG_QP 16 -+#if FF_API_DEBUG_MV -+/** -+ * @deprecated this option does nothing -+ */ -+#define FF_DEBUG_MV 32 -+#endif -+#define FF_DEBUG_DCT_COEFF 0x00000040 -+#define FF_DEBUG_SKIP 0x00000080 -+#define FF_DEBUG_STARTCODE 0x00000100 -+#define FF_DEBUG_ER 0x00000400 -+#define FF_DEBUG_MMCO 0x00000800 -+#define FF_DEBUG_BUGS 0x00001000 -+#if FF_API_DEBUG_MV -+#define FF_DEBUG_VIS_QP 0x00002000 -+#define FF_DEBUG_VIS_MB_TYPE 0x00004000 -+#endif -+#define FF_DEBUG_BUFFERS 0x00008000 -+#define FF_DEBUG_THREADS 0x00010000 -+#define FF_DEBUG_GREEN_MD 0x00800000 -+#define FF_DEBUG_NOMC 0x01000000 -+ -+#if FF_API_DEBUG_MV -+ /** -+ * debug -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int debug_mv; -+#define FF_DEBUG_VIS_MV_P_FOR 0x00000001 // visualize forward predicted MVs of P-frames -+#define FF_DEBUG_VIS_MV_B_FOR 0x00000002 // visualize forward predicted MVs of B-frames -+#define FF_DEBUG_VIS_MV_B_BACK 0x00000004 // visualize backward predicted MVs of B-frames -+#endif -+ -+ /** -+ * Error recognition; may misdetect some more or less valid parts as errors. -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int err_recognition; -+ -+/** -+ * Verify checksums embedded in the bitstream (could be of either encoded or -+ * decoded data, depending on the codec) and print an error message on mismatch. -+ * If AV_EF_EXPLODE is also set, a mismatching checksum will result in the -+ * decoder returning an error. -+ */ -+#define AV_EF_CRCCHECK (1<<0) -+#define AV_EF_BITSTREAM (1<<1) ///< detect bitstream specification deviations -+#define AV_EF_BUFFER (1<<2) ///< detect improper bitstream length -+#define AV_EF_EXPLODE (1<<3) ///< abort decoding on minor error detection -+ -+#define AV_EF_IGNORE_ERR (1<<15) ///< ignore errors and continue -+#define AV_EF_CAREFUL (1<<16) ///< consider things that violate the spec, are fast to calculate and have not been seen in the wild as errors -+#define AV_EF_COMPLIANT (1<<17) ///< consider all spec non compliances as errors -+#define AV_EF_AGGRESSIVE (1<<18) ///< consider things that a sane encoder should not do as an error -+ -+ -+ /** -+ * opaque 64-bit number (generally a PTS) that will be reordered and -+ * output in AVFrame.reordered_opaque -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int64_t reordered_opaque; -+ -+ /** -+ * Hardware accelerator in use -+ * - encoding: unused. -+ * - decoding: Set by libavcodec -+ */ -+ const struct AVHWAccel *hwaccel; -+ -+ /** -+ * Hardware accelerator context. -+ * For some hardware accelerators, a global context needs to be -+ * provided by the user. In that case, this holds display-dependent -+ * data FFmpeg cannot instantiate itself. Please refer to the -+ * FFmpeg HW accelerator documentation to know how to fill this -+ * is. e.g. for VA API, this is a struct vaapi_context. -+ * - encoding: unused -+ * - decoding: Set by user -+ */ -+ void *hwaccel_context; -+ -+ /** -+ * error -+ * - encoding: Set by libavcodec if flags & AV_CODEC_FLAG_PSNR. -+ * - decoding: unused -+ */ -+ uint64_t error[AV_NUM_DATA_POINTERS]; -+ -+ /** -+ * DCT algorithm, see FF_DCT_* below -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int dct_algo; -+#define FF_DCT_AUTO 0 -+#define FF_DCT_FASTINT 1 -+#define FF_DCT_INT 2 -+#define FF_DCT_MMX 3 -+#define FF_DCT_ALTIVEC 5 -+#define FF_DCT_FAAN 6 -+ -+ /** -+ * IDCT algorithm, see FF_IDCT_* below. -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int idct_algo; -+#define FF_IDCT_AUTO 0 -+#define FF_IDCT_INT 1 -+#define FF_IDCT_SIMPLE 2 -+#define FF_IDCT_SIMPLEMMX 3 -+#define FF_IDCT_ARM 7 -+#define FF_IDCT_ALTIVEC 8 -+#define FF_IDCT_SIMPLEARM 10 -+#define FF_IDCT_XVID 14 -+#define FF_IDCT_SIMPLEARMV5TE 16 -+#define FF_IDCT_SIMPLEARMV6 17 -+#define FF_IDCT_FAAN 20 -+#define FF_IDCT_SIMPLENEON 22 -+#define FF_IDCT_NONE 24 /* Used by XvMC to extract IDCT coefficients with FF_IDCT_PERM_NONE */ -+#define FF_IDCT_SIMPLEAUTO 128 -+ -+ /** -+ * bits per sample/pixel from the demuxer (needed for huffyuv). -+ * - encoding: Set by libavcodec. -+ * - decoding: Set by user. -+ */ -+ int bits_per_coded_sample; -+ -+ /** -+ * Bits per sample/pixel of internal libavcodec pixel/sample format. -+ * - encoding: set by user. -+ * - decoding: set by libavcodec. -+ */ -+ int bits_per_raw_sample; -+ -+#if FF_API_LOWRES -+ /** -+ * low resolution decoding, 1-> 1/2 size, 2->1/4 size -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int lowres; -+#endif -+ -+#if FF_API_CODED_FRAME -+ /** -+ * the picture in the bitstream -+ * - encoding: Set by libavcodec. -+ * - decoding: unused -+ * -+ * @deprecated use the quality factor packet side data instead -+ */ -+ attribute_deprecated AVFrame *coded_frame; -+#endif -+ -+ /** -+ * thread count -+ * is used to decide how many independent tasks should be passed to execute() -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int thread_count; -+ -+ /** -+ * Which multithreading methods to use. -+ * Use of FF_THREAD_FRAME will increase decoding delay by one frame per thread, -+ * so clients which cannot provide future frames should not use it. -+ * -+ * - encoding: Set by user, otherwise the default is used. -+ * - decoding: Set by user, otherwise the default is used. -+ */ -+ int thread_type; -+#define FF_THREAD_FRAME 1 ///< Decode more than one frame at once -+#define FF_THREAD_SLICE 2 ///< Decode more than one part of a single frame at once -+ -+ /** -+ * Which multithreading methods are in use by the codec. -+ * - encoding: Set by libavcodec. -+ * - decoding: Set by libavcodec. -+ */ -+ int active_thread_type; -+ -+ /** -+ * Set by the client if its custom get_buffer() callback can be called -+ * synchronously from another thread, which allows faster multithreaded decoding. -+ * draw_horiz_band() will be called from other threads regardless of this setting. -+ * Ignored if the default get_buffer() is used. -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int thread_safe_callbacks; -+ -+ /** -+ * The codec may call this to execute several independent things. -+ * It will return only after finishing all tasks. -+ * The user may replace this with some multithreaded implementation, -+ * the default implementation will execute the parts serially. -+ * @param count the number of things to execute -+ * - encoding: Set by libavcodec, user can override. -+ * - decoding: Set by libavcodec, user can override. -+ */ -+ int (*execute)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size); -+ -+ /** -+ * The codec may call this to execute several independent things. -+ * It will return only after finishing all tasks. -+ * The user may replace this with some multithreaded implementation, -+ * the default implementation will execute the parts serially. -+ * Also see avcodec_thread_init and e.g. the --enable-pthread configure option. -+ * @param c context passed also to func -+ * @param count the number of things to execute -+ * @param arg2 argument passed unchanged to func -+ * @param ret return values of executed functions, must have space for "count" values. May be NULL. -+ * @param func function that will be called count times, with jobnr from 0 to count-1. -+ * threadnr will be in the range 0 to c->thread_count-1 < MAX_THREADS and so that no -+ * two instances of func executing at the same time will have the same threadnr. -+ * @return always 0 currently, but code should handle a future improvement where when any call to func -+ * returns < 0 no further calls to func may be done and < 0 is returned. -+ * - encoding: Set by libavcodec, user can override. -+ * - decoding: Set by libavcodec, user can override. -+ */ -+ int (*execute2)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count); -+ -+ /** -+ * noise vs. sse weight for the nsse comparison function -+ * - encoding: Set by user. -+ * - decoding: unused -+ */ -+ int nsse_weight; -+ -+ /** -+ * profile -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ int profile; -+#define FF_PROFILE_UNKNOWN -99 -+#define FF_PROFILE_RESERVED -100 -+ -+#define FF_PROFILE_AAC_MAIN 0 -+#define FF_PROFILE_AAC_LOW 1 -+#define FF_PROFILE_AAC_SSR 2 -+#define FF_PROFILE_AAC_LTP 3 -+#define FF_PROFILE_AAC_HE 4 -+#define FF_PROFILE_AAC_HE_V2 28 -+#define FF_PROFILE_AAC_LD 22 -+#define FF_PROFILE_AAC_ELD 38 -+#define FF_PROFILE_MPEG2_AAC_LOW 128 -+#define FF_PROFILE_MPEG2_AAC_HE 131 -+ -+#define FF_PROFILE_DNXHD 0 -+#define FF_PROFILE_DNXHR_LB 1 -+#define FF_PROFILE_DNXHR_SQ 2 -+#define FF_PROFILE_DNXHR_HQ 3 -+#define FF_PROFILE_DNXHR_HQX 4 -+#define FF_PROFILE_DNXHR_444 5 -+ -+#define FF_PROFILE_DTS 20 -+#define FF_PROFILE_DTS_ES 30 -+#define FF_PROFILE_DTS_96_24 40 -+#define FF_PROFILE_DTS_HD_HRA 50 -+#define FF_PROFILE_DTS_HD_MA 60 -+#define FF_PROFILE_DTS_EXPRESS 70 -+ -+#define FF_PROFILE_MPEG2_422 0 -+#define FF_PROFILE_MPEG2_HIGH 1 -+#define FF_PROFILE_MPEG2_SS 2 -+#define FF_PROFILE_MPEG2_SNR_SCALABLE 3 -+#define FF_PROFILE_MPEG2_MAIN 4 -+#define FF_PROFILE_MPEG2_SIMPLE 5 -+ -+#define FF_PROFILE_H264_CONSTRAINED (1<<9) // 8+1; constraint_set1_flag -+#define FF_PROFILE_H264_INTRA (1<<11) // 8+3; constraint_set3_flag -+ -+#define FF_PROFILE_H264_BASELINE 66 -+#define FF_PROFILE_H264_CONSTRAINED_BASELINE (66|FF_PROFILE_H264_CONSTRAINED) -+#define FF_PROFILE_H264_MAIN 77 -+#define FF_PROFILE_H264_EXTENDED 88 -+#define FF_PROFILE_H264_HIGH 100 -+#define FF_PROFILE_H264_HIGH_10 110 -+#define FF_PROFILE_H264_HIGH_10_INTRA (110|FF_PROFILE_H264_INTRA) -+#define FF_PROFILE_H264_MULTIVIEW_HIGH 118 -+#define FF_PROFILE_H264_HIGH_422 122 -+#define FF_PROFILE_H264_HIGH_422_INTRA (122|FF_PROFILE_H264_INTRA) -+#define FF_PROFILE_H264_STEREO_HIGH 128 -+#define FF_PROFILE_H264_HIGH_444 144 -+#define FF_PROFILE_H264_HIGH_444_PREDICTIVE 244 -+#define FF_PROFILE_H264_HIGH_444_INTRA (244|FF_PROFILE_H264_INTRA) -+#define FF_PROFILE_H264_CAVLC_444 44 -+ -+#define FF_PROFILE_VC1_SIMPLE 0 -+#define FF_PROFILE_VC1_MAIN 1 -+#define FF_PROFILE_VC1_COMPLEX 2 -+#define FF_PROFILE_VC1_ADVANCED 3 -+ -+#define FF_PROFILE_MPEG4_SIMPLE 0 -+#define FF_PROFILE_MPEG4_SIMPLE_SCALABLE 1 -+#define FF_PROFILE_MPEG4_CORE 2 -+#define FF_PROFILE_MPEG4_MAIN 3 -+#define FF_PROFILE_MPEG4_N_BIT 4 -+#define FF_PROFILE_MPEG4_SCALABLE_TEXTURE 5 -+#define FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION 6 -+#define FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE 7 -+#define FF_PROFILE_MPEG4_HYBRID 8 -+#define FF_PROFILE_MPEG4_ADVANCED_REAL_TIME 9 -+#define FF_PROFILE_MPEG4_CORE_SCALABLE 10 -+#define FF_PROFILE_MPEG4_ADVANCED_CODING 11 -+#define FF_PROFILE_MPEG4_ADVANCED_CORE 12 -+#define FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE 13 -+#define FF_PROFILE_MPEG4_SIMPLE_STUDIO 14 -+#define FF_PROFILE_MPEG4_ADVANCED_SIMPLE 15 -+ -+#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0 1 -+#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1 2 -+#define FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION 32768 -+#define FF_PROFILE_JPEG2000_DCINEMA_2K 3 -+#define FF_PROFILE_JPEG2000_DCINEMA_4K 4 -+ -+#define FF_PROFILE_VP9_0 0 -+#define FF_PROFILE_VP9_1 1 -+#define FF_PROFILE_VP9_2 2 -+#define FF_PROFILE_VP9_3 3 -+ -+#define FF_PROFILE_HEVC_MAIN 1 -+#define FF_PROFILE_HEVC_MAIN_10 2 -+#define FF_PROFILE_HEVC_MAIN_STILL_PICTURE 3 -+#define FF_PROFILE_HEVC_REXT 4 -+ -+#define FF_PROFILE_AV1_MAIN 0 -+#define FF_PROFILE_AV1_HIGH 1 -+#define FF_PROFILE_AV1_PROFESSIONAL 2 -+ -+#define FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT 0xc0 -+#define FF_PROFILE_MJPEG_HUFFMAN_EXTENDED_SEQUENTIAL_DCT 0xc1 -+#define FF_PROFILE_MJPEG_HUFFMAN_PROGRESSIVE_DCT 0xc2 -+#define FF_PROFILE_MJPEG_HUFFMAN_LOSSLESS 0xc3 -+#define FF_PROFILE_MJPEG_JPEG_LS 0xf7 -+ -+#define FF_PROFILE_SBC_MSBC 1 -+ -+ /** -+ * level -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ int level; -+#define FF_LEVEL_UNKNOWN -99 -+ -+ /** -+ * Skip loop filtering for selected frames. -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ enum AVDiscard skip_loop_filter; -+ -+ /** -+ * Skip IDCT/dequantization for selected frames. -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ enum AVDiscard skip_idct; -+ -+ /** -+ * Skip decoding for selected frames. -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ enum AVDiscard skip_frame; -+ -+ /** -+ * Header containing style information for text subtitles. -+ * For SUBTITLE_ASS subtitle type, it should contain the whole ASS -+ * [Script Info] and [V4+ Styles] section, plus the [Events] line and -+ * the Format line following. It shouldn't include any Dialogue line. -+ * - encoding: Set/allocated/freed by user (before avcodec_open2()) -+ * - decoding: Set/allocated/freed by libavcodec (by avcodec_open2()) -+ */ -+ uint8_t *subtitle_header; -+ int subtitle_header_size; -+ -+#if FF_API_VBV_DELAY -+ /** -+ * VBV delay coded in the last frame (in periods of a 27 MHz clock). -+ * Used for compliant TS muxing. -+ * - encoding: Set by libavcodec. -+ * - decoding: unused. -+ * @deprecated this value is now exported as a part of -+ * AV_PKT_DATA_CPB_PROPERTIES packet side data -+ */ -+ attribute_deprecated -+ uint64_t vbv_delay; -+#endif -+ -+#if FF_API_SIDEDATA_ONLY_PKT -+ /** -+ * Encoding only and set by default. Allow encoders to output packets -+ * that do not contain any encoded data, only side data. -+ * -+ * Some encoders need to output such packets, e.g. to update some stream -+ * parameters at the end of encoding. -+ * -+ * @deprecated this field disables the default behaviour and -+ * it is kept only for compatibility. -+ */ -+ attribute_deprecated -+ int side_data_only_packets; -+#endif -+ -+ /** -+ * Audio only. The number of "priming" samples (padding) inserted by the -+ * encoder at the beginning of the audio. I.e. this number of leading -+ * decoded samples must be discarded by the caller to get the original audio -+ * without leading padding. -+ * -+ * - decoding: unused -+ * - encoding: Set by libavcodec. The timestamps on the output packets are -+ * adjusted by the encoder so that they always refer to the -+ * first sample of the data actually contained in the packet, -+ * including any added padding. E.g. if the timebase is -+ * 1/samplerate and the timestamp of the first input sample is -+ * 0, the timestamp of the first output packet will be -+ * -initial_padding. -+ */ -+ int initial_padding; -+ -+ /** -+ * - decoding: For codecs that store a framerate value in the compressed -+ * bitstream, the decoder may export it here. { 0, 1} when -+ * unknown. -+ * - encoding: May be used to signal the framerate of CFR content to an -+ * encoder. -+ */ -+ AVRational framerate; -+ -+ /** -+ * Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx. -+ * - encoding: unused. -+ * - decoding: Set by libavcodec before calling get_format() -+ */ -+ enum AVPixelFormat sw_pix_fmt; -+ -+ /** -+ * Timebase in which pkt_dts/pts and AVPacket.dts/pts are. -+ * - encoding unused. -+ * - decoding set by user. -+ */ -+ AVRational pkt_timebase; -+ -+ /** -+ * AVCodecDescriptor -+ * - encoding: unused. -+ * - decoding: set by libavcodec. -+ */ -+ const AVCodecDescriptor *codec_descriptor; -+ -+#if !FF_API_LOWRES -+ /** -+ * low resolution decoding, 1-> 1/2 size, 2->1/4 size -+ * - encoding: unused -+ * - decoding: Set by user. -+ */ -+ int lowres; -+#endif -+ -+ /** -+ * Current statistics for PTS correction. -+ * - decoding: maintained and used by libavcodec, not intended to be used by user apps -+ * - encoding: unused -+ */ -+ int64_t pts_correction_num_faulty_pts; /// Number of incorrect PTS values so far -+ int64_t pts_correction_num_faulty_dts; /// Number of incorrect DTS values so far -+ int64_t pts_correction_last_pts; /// PTS of the last frame -+ int64_t pts_correction_last_dts; /// DTS of the last frame -+ -+ /** -+ * Character encoding of the input subtitles file. -+ * - decoding: set by user -+ * - encoding: unused -+ */ -+ char *sub_charenc; -+ -+ /** -+ * Subtitles character encoding mode. Formats or codecs might be adjusting -+ * this setting (if they are doing the conversion themselves for instance). -+ * - decoding: set by libavcodec -+ * - encoding: unused -+ */ -+ int sub_charenc_mode; -+#define FF_SUB_CHARENC_MODE_DO_NOTHING -1 ///< do nothing (demuxer outputs a stream supposed to be already in UTF-8, or the codec is bitmap for instance) -+#define FF_SUB_CHARENC_MODE_AUTOMATIC 0 ///< libavcodec will select the mode itself -+#define FF_SUB_CHARENC_MODE_PRE_DECODER 1 ///< the AVPacket data needs to be recoded to UTF-8 before being fed to the decoder, requires iconv -+#define FF_SUB_CHARENC_MODE_IGNORE 2 ///< neither convert the subtitles, nor check them for valid UTF-8 -+ -+ /** -+ * Skip processing alpha if supported by codec. -+ * Note that if the format uses pre-multiplied alpha (common with VP6, -+ * and recommended due to better video quality/compression) -+ * the image will look as if alpha-blended onto a black background. -+ * However for formats that do not use pre-multiplied alpha -+ * there might be serious artefacts (though e.g. libswscale currently -+ * assumes pre-multiplied alpha anyway). -+ * -+ * - decoding: set by user -+ * - encoding: unused -+ */ -+ int skip_alpha; -+ -+ /** -+ * Number of samples to skip after a discontinuity -+ * - decoding: unused -+ * - encoding: set by libavcodec -+ */ -+ int seek_preroll; -+ -+#if !FF_API_DEBUG_MV -+ /** -+ * debug motion vectors -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ int debug_mv; -+#define FF_DEBUG_VIS_MV_P_FOR 0x00000001 //visualize forward predicted MVs of P frames -+#define FF_DEBUG_VIS_MV_B_FOR 0x00000002 //visualize forward predicted MVs of B frames -+#define FF_DEBUG_VIS_MV_B_BACK 0x00000004 //visualize backward predicted MVs of B frames -+#endif -+ -+ /** -+ * custom intra quantization matrix -+ * - encoding: Set by user, can be NULL. -+ * - decoding: unused. -+ */ -+ uint16_t *chroma_intra_matrix; -+ -+ /** -+ * dump format separator. -+ * can be ", " or "\n " or anything else -+ * - encoding: Set by user. -+ * - decoding: Set by user. -+ */ -+ uint8_t *dump_separator; -+ -+ /** -+ * ',' separated list of allowed decoders. -+ * If NULL then all are allowed -+ * - encoding: unused -+ * - decoding: set by user -+ */ -+ char *codec_whitelist; -+ -+ /** -+ * Properties of the stream that gets decoded -+ * - encoding: unused -+ * - decoding: set by libavcodec -+ */ -+ unsigned properties; -+#define FF_CODEC_PROPERTY_LOSSLESS 0x00000001 -+#define FF_CODEC_PROPERTY_CLOSED_CAPTIONS 0x00000002 -+ -+ /** -+ * Additional data associated with the entire coded stream. -+ * -+ * - decoding: unused -+ * - encoding: may be set by libavcodec after avcodec_open2(). -+ */ -+ AVPacketSideData *coded_side_data; -+ int nb_coded_side_data; -+ -+ /** -+ * A reference to the AVHWFramesContext describing the input (for encoding) -+ * or output (decoding) frames. The reference is set by the caller and -+ * afterwards owned (and freed) by libavcodec - it should never be read by -+ * the caller after being set. -+ * -+ * - decoding: This field should be set by the caller from the get_format() -+ * callback. The previous reference (if any) will always be -+ * unreffed by libavcodec before the get_format() call. -+ * -+ * If the default get_buffer2() is used with a hwaccel pixel -+ * format, then this AVHWFramesContext will be used for -+ * allocating the frame buffers. -+ * -+ * - encoding: For hardware encoders configured to use a hwaccel pixel -+ * format, this field should be set by the caller to a reference -+ * to the AVHWFramesContext describing input frames. -+ * AVHWFramesContext.format must be equal to -+ * AVCodecContext.pix_fmt. -+ * -+ * This field should be set before avcodec_open2() is called. -+ */ -+ AVBufferRef *hw_frames_ctx; -+ -+ /** -+ * Control the form of AVSubtitle.rects[N]->ass -+ * - decoding: set by user -+ * - encoding: unused -+ */ -+ int sub_text_format; -+#define FF_SUB_TEXT_FMT_ASS 0 -+#if FF_API_ASS_TIMING -+#define FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS 1 -+#endif -+ -+ /** -+ * Audio only. The amount of padding (in samples) appended by the encoder to -+ * the end of the audio. I.e. this number of decoded samples must be -+ * discarded by the caller from the end of the stream to get the original -+ * audio without any trailing padding. -+ * -+ * - decoding: unused -+ * - encoding: unused -+ */ -+ int trailing_padding; -+ -+ /** -+ * The number of pixels per image to maximally accept. -+ * -+ * - decoding: set by user -+ * - encoding: set by user -+ */ -+ int64_t max_pixels; -+ -+ /** -+ * A reference to the AVHWDeviceContext describing the device which will -+ * be used by a hardware encoder/decoder. The reference is set by the -+ * caller and afterwards owned (and freed) by libavcodec. -+ * -+ * This should be used if either the codec device does not require -+ * hardware frames or any that are used are to be allocated internally by -+ * libavcodec. If the user wishes to supply any of the frames used as -+ * encoder input or decoder output then hw_frames_ctx should be used -+ * instead. When hw_frames_ctx is set in get_format() for a decoder, this -+ * field will be ignored while decoding the associated stream segment, but -+ * may again be used on a following one after another get_format() call. -+ * -+ * For both encoders and decoders this field should be set before -+ * avcodec_open2() is called and must not be written to thereafter. -+ * -+ * Note that some decoders may require this field to be set initially in -+ * order to support hw_frames_ctx at all - in that case, all frames -+ * contexts used must be created on the same device. -+ */ -+ AVBufferRef *hw_device_ctx; -+ -+ /** -+ * Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated -+ * decoding (if active). -+ * - encoding: unused -+ * - decoding: Set by user (either before avcodec_open2(), or in the -+ * AVCodecContext.get_format callback) -+ */ -+ int hwaccel_flags; -+ -+ /** -+ * Video decoding only. Certain video codecs support cropping, meaning that -+ * only a sub-rectangle of the decoded frame is intended for display. This -+ * option controls how cropping is handled by libavcodec. -+ * -+ * When set to 1 (the default), libavcodec will apply cropping internally. -+ * I.e. it will modify the output frame width/height fields and offset the -+ * data pointers (only by as much as possible while preserving alignment, or -+ * by the full amount if the AV_CODEC_FLAG_UNALIGNED flag is set) so that -+ * the frames output by the decoder refer only to the cropped area. The -+ * crop_* fields of the output frames will be zero. -+ * -+ * When set to 0, the width/height fields of the output frames will be set -+ * to the coded dimensions and the crop_* fields will describe the cropping -+ * rectangle. Applying the cropping is left to the caller. -+ * -+ * @warning When hardware acceleration with opaque output frames is used, -+ * libavcodec is unable to apply cropping from the top/left border. -+ * -+ * @note when this option is set to zero, the width/height fields of the -+ * AVCodecContext and output AVFrames have different meanings. The codec -+ * context fields store display dimensions (with the coded dimensions in -+ * coded_width/height), while the frame fields store the coded dimensions -+ * (with the display dimensions being determined by the crop_* fields). -+ */ -+ int apply_cropping; -+ -+ /* -+ * Video decoding only. Sets the number of extra hardware frames which -+ * the decoder will allocate for use by the caller. This must be set -+ * before avcodec_open2() is called. -+ * -+ * Some hardware decoders require all frames that they will use for -+ * output to be defined in advance before decoding starts. For such -+ * decoders, the hardware frame pool must therefore be of a fixed size. -+ * The extra frames set here are on top of any number that the decoder -+ * needs internally in order to operate normally (for example, frames -+ * used as reference pictures). -+ */ -+ int extra_hw_frames; -+} AVCodecContext; -+ -+#if FF_API_CODEC_GET_SET -+/** -+ * Accessors for some AVCodecContext fields. These used to be provided for ABI -+ * compatibility, and do not need to be used anymore. -+ */ -+attribute_deprecated -+AVRational av_codec_get_pkt_timebase (const AVCodecContext *avctx); -+attribute_deprecated -+void av_codec_set_pkt_timebase (AVCodecContext *avctx, AVRational val); -+ -+attribute_deprecated -+const AVCodecDescriptor *av_codec_get_codec_descriptor(const AVCodecContext *avctx); -+attribute_deprecated -+void av_codec_set_codec_descriptor(AVCodecContext *avctx, const AVCodecDescriptor *desc); -+ -+attribute_deprecated -+unsigned av_codec_get_codec_properties(const AVCodecContext *avctx); -+ -+#if FF_API_LOWRES -+attribute_deprecated -+int av_codec_get_lowres(const AVCodecContext *avctx); -+attribute_deprecated -+void av_codec_set_lowres(AVCodecContext *avctx, int val); -+#endif -+ -+attribute_deprecated -+int av_codec_get_seek_preroll(const AVCodecContext *avctx); -+attribute_deprecated -+void av_codec_set_seek_preroll(AVCodecContext *avctx, int val); -+ -+attribute_deprecated -+uint16_t *av_codec_get_chroma_intra_matrix(const AVCodecContext *avctx); -+attribute_deprecated -+void av_codec_set_chroma_intra_matrix(AVCodecContext *avctx, uint16_t *val); -+#endif -+ -+/** -+ * AVProfile. -+ */ -+typedef struct AVProfile { -+ int profile; -+ const char *name; ///< short name for the profile -+} AVProfile; -+ -+enum { -+ /** -+ * The codec supports this format via the hw_device_ctx interface. -+ * -+ * When selecting this format, AVCodecContext.hw_device_ctx should -+ * have been set to a device of the specified type before calling -+ * avcodec_open2(). -+ */ -+ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX = 0x01, -+ /** -+ * The codec supports this format via the hw_frames_ctx interface. -+ * -+ * When selecting this format for a decoder, -+ * AVCodecContext.hw_frames_ctx should be set to a suitable frames -+ * context inside the get_format() callback. The frames context -+ * must have been created on a device of the specified type. -+ */ -+ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX = 0x02, -+ /** -+ * The codec supports this format by some internal method. -+ * -+ * This format can be selected without any additional configuration - -+ * no device or frames context is required. -+ */ -+ AV_CODEC_HW_CONFIG_METHOD_INTERNAL = 0x04, -+ /** -+ * The codec supports this format by some ad-hoc method. -+ * -+ * Additional settings and/or function calls are required. See the -+ * codec-specific documentation for details. (Methods requiring -+ * this sort of configuration are deprecated and others should be -+ * used in preference.) -+ */ -+ AV_CODEC_HW_CONFIG_METHOD_AD_HOC = 0x08, -+}; -+ -+typedef struct AVCodecHWConfig { -+ /** -+ * A hardware pixel format which the codec can use. -+ */ -+ enum AVPixelFormat pix_fmt; -+ /** -+ * Bit set of AV_CODEC_HW_CONFIG_METHOD_* flags, describing the possible -+ * setup methods which can be used with this configuration. -+ */ -+ int methods; -+ /** -+ * The device type associated with the configuration. -+ * -+ * Must be set for AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX and -+ * AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX, otherwise unused. -+ */ -+ enum AVHWDeviceType device_type; -+} AVCodecHWConfig; -+ -+typedef struct AVCodecDefault AVCodecDefault; -+ -+struct AVSubtitle; -+ -+/** -+ * AVCodec. -+ */ -+typedef struct AVCodec { -+ /** -+ * Name of the codec implementation. -+ * The name is globally unique among encoders and among decoders (but an -+ * encoder and a decoder can share the same name). -+ * This is the primary way to find a codec from the user perspective. -+ */ -+ const char *name; -+ /** -+ * Descriptive name for the codec, meant to be more human readable than name. -+ * You should use the NULL_IF_CONFIG_SMALL() macro to define it. -+ */ -+ const char *long_name; -+ enum AVMediaType type; -+ enum AVCodecID id; -+ /** -+ * Codec capabilities. -+ * see AV_CODEC_CAP_* -+ */ -+ int capabilities; -+ const AVRational *supported_framerates; ///< array of supported framerates, or NULL if any, array is terminated by {0,0} -+ const enum AVPixelFormat *pix_fmts; ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1 -+ const int *supported_samplerates; ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0 -+ const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1 -+ const uint64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0 -+ uint8_t max_lowres; ///< maximum value for lowres supported by the decoder -+ const AVClass *priv_class; ///< AVClass for the private context -+ const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN} -+ -+ /** -+ * Group name of the codec implementation. -+ * This is a short symbolic name of the wrapper backing this codec. A -+ * wrapper uses some kind of external implementation for the codec, such -+ * as an external library, or a codec implementation provided by the OS or -+ * the hardware. -+ * If this field is NULL, this is a builtin, libavcodec native codec. -+ * If non-NULL, this will be the suffix in AVCodec.name in most cases -+ * (usually AVCodec.name will be of the form "_"). -+ */ -+ const char *wrapper_name; -+ -+ /***************************************************************** -+ * No fields below this line are part of the public API. They -+ * may not be used outside of libavcodec and can be changed and -+ * removed at will. -+ * New public fields should be added right above. -+ ***************************************************************** -+ */ -+ int priv_data_size; -+ struct AVCodec *next; -+ /** -+ * @name Frame-level threading support functions -+ * @{ -+ */ -+ /** -+ * If defined, called on thread contexts when they are created. -+ * If the codec allocates writable tables in init(), re-allocate them here. -+ * priv_data will be set to a copy of the original. -+ */ -+ int (*init_thread_copy)(AVCodecContext *); -+ /** -+ * Copy necessary context variables from a previous thread context to the current one. -+ * If not defined, the next thread will start automatically; otherwise, the codec -+ * must call ff_thread_finish_setup(). -+ * -+ * dst and src will (rarely) point to the same context, in which case memcpy should be skipped. -+ */ -+ int (*update_thread_context)(AVCodecContext *dst, const AVCodecContext *src); -+ /** @} */ -+ -+ /** -+ * Private codec-specific defaults. -+ */ -+ const AVCodecDefault *defaults; -+ -+ /** -+ * Initialize codec static data, called from avcodec_register(). -+ * -+ * This is not intended for time consuming operations as it is -+ * run for every codec regardless of that codec being used. -+ */ -+ void (*init_static_data)(struct AVCodec *codec); -+ -+ int (*init)(AVCodecContext *); -+ int (*encode_sub)(AVCodecContext *, uint8_t *buf, int buf_size, -+ const struct AVSubtitle *sub); -+ /** -+ * Encode data to an AVPacket. -+ * -+ * @param avctx codec context -+ * @param avpkt output AVPacket (may contain a user-provided buffer) -+ * @param[in] frame AVFrame containing the raw data to be encoded -+ * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a -+ * non-empty packet was returned in avpkt. -+ * @return 0 on success, negative error code on failure -+ */ -+ int (*encode2)(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, -+ int *got_packet_ptr); -+ int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt); -+ int (*close)(AVCodecContext *); -+ /** -+ * Encode API with decoupled packet/frame dataflow. The API is the -+ * same as the avcodec_ prefixed APIs (avcodec_send_frame() etc.), except -+ * that: -+ * - never called if the codec is closed or the wrong type, -+ * - if AV_CODEC_CAP_DELAY is not set, drain frames are never sent, -+ * - only one drain frame is ever passed down, -+ */ -+ int (*send_frame)(AVCodecContext *avctx, const AVFrame *frame); -+ int (*receive_packet)(AVCodecContext *avctx, AVPacket *avpkt); -+ -+ /** -+ * Decode API with decoupled packet/frame dataflow. This function is called -+ * to get one output frame. It should call ff_decode_get_packet() to obtain -+ * input data. -+ */ -+ int (*receive_frame)(AVCodecContext *avctx, AVFrame *frame); -+ /** -+ * Flush buffers. -+ * Will be called when seeking -+ */ -+ void (*flush)(AVCodecContext *); -+ /** -+ * Internal codec capabilities. -+ * See FF_CODEC_CAP_* in internal.h -+ */ -+ int caps_internal; -+ -+ /** -+ * Decoding only, a comma-separated list of bitstream filters to apply to -+ * packets before decoding. -+ */ -+ const char *bsfs; -+ -+ /** -+ * Array of pointers to hardware configurations supported by the codec, -+ * or NULL if no hardware supported. The array is terminated by a NULL -+ * pointer. -+ * -+ * The user can only access this field via avcodec_get_hw_config(). -+ */ -+ const struct AVCodecHWConfigInternal **hw_configs; -+} AVCodec; -+ -+#if FF_API_CODEC_GET_SET -+attribute_deprecated -+int av_codec_get_max_lowres(const AVCodec *codec); -+#endif -+ -+struct MpegEncContext; -+ -+/** -+ * Retrieve supported hardware configurations for a codec. -+ * -+ * Values of index from zero to some maximum return the indexed configuration -+ * descriptor; all other values return NULL. If the codec does not support -+ * any hardware configurations then it will always return NULL. -+ */ -+const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index); -+ -+/** -+ * @defgroup lavc_hwaccel AVHWAccel -+ * -+ * @note Nothing in this structure should be accessed by the user. At some -+ * point in future it will not be externally visible at all. -+ * -+ * @{ -+ */ -+typedef struct AVHWAccel { -+ /** -+ * Name of the hardware accelerated codec. -+ * The name is globally unique among encoders and among decoders (but an -+ * encoder and a decoder can share the same name). -+ */ -+ const char *name; -+ -+ /** -+ * Type of codec implemented by the hardware accelerator. -+ * -+ * See AVMEDIA_TYPE_xxx -+ */ -+ enum AVMediaType type; -+ -+ /** -+ * Codec implemented by the hardware accelerator. -+ * -+ * See AV_CODEC_ID_xxx -+ */ -+ enum AVCodecID id; -+ -+ /** -+ * Supported pixel format. -+ * -+ * Only hardware accelerated formats are supported here. -+ */ -+ enum AVPixelFormat pix_fmt; -+ -+ /** -+ * Hardware accelerated codec capabilities. -+ * see AV_HWACCEL_CODEC_CAP_* -+ */ -+ int capabilities; -+ -+ /***************************************************************** -+ * No fields below this line are part of the public API. They -+ * may not be used outside of libavcodec and can be changed and -+ * removed at will. -+ * New public fields should be added right above. -+ ***************************************************************** -+ */ -+ -+ /** -+ * Allocate a custom buffer -+ */ -+ int (*alloc_frame)(AVCodecContext *avctx, AVFrame *frame); -+ -+ /** -+ * Called at the beginning of each frame or field picture. -+ * -+ * Meaningful frame information (codec specific) is guaranteed to -+ * be parsed at this point. This function is mandatory. -+ * -+ * Note that buf can be NULL along with buf_size set to 0. -+ * Otherwise, this means the whole frame is available at this point. -+ * -+ * @param avctx the codec context -+ * @param buf the frame data buffer base -+ * @param buf_size the size of the frame in bytes -+ * @return zero if successful, a negative value otherwise -+ */ -+ int (*start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size); -+ -+ /** -+ * Callback for parameter data (SPS/PPS/VPS etc). -+ * -+ * Useful for hardware decoders which keep persistent state about the -+ * video parameters, and need to receive any changes to update that state. -+ * -+ * @param avctx the codec context -+ * @param type the nal unit type -+ * @param buf the nal unit data buffer -+ * @param buf_size the size of the nal unit in bytes -+ * @return zero if successful, a negative value otherwise -+ */ -+ int (*decode_params)(AVCodecContext *avctx, int type, const uint8_t *buf, uint32_t buf_size); -+ -+ /** -+ * Callback for each slice. -+ * -+ * Meaningful slice information (codec specific) is guaranteed to -+ * be parsed at this point. This function is mandatory. -+ * The only exception is XvMC, that works on MB level. -+ * -+ * @param avctx the codec context -+ * @param buf the slice data buffer base -+ * @param buf_size the size of the slice in bytes -+ * @return zero if successful, a negative value otherwise -+ */ -+ int (*decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size); -+ -+ /** -+ * Called at the end of each frame or field picture. -+ * -+ * The whole picture is parsed at this point and can now be sent -+ * to the hardware accelerator. This function is mandatory. -+ * -+ * @param avctx the codec context -+ * @return zero if successful, a negative value otherwise -+ */ -+ int (*end_frame)(AVCodecContext *avctx); -+ -+ /** -+ * Size of per-frame hardware accelerator private data. -+ * -+ * Private data is allocated with av_mallocz() before -+ * AVCodecContext.get_buffer() and deallocated after -+ * AVCodecContext.release_buffer(). -+ */ -+ int frame_priv_data_size; -+ -+ /** -+ * Called for every Macroblock in a slice. -+ * -+ * XvMC uses it to replace the ff_mpv_reconstruct_mb(). -+ * Instead of decoding to raw picture, MB parameters are -+ * stored in an array provided by the video driver. -+ * -+ * @param s the mpeg context -+ */ -+ void (*decode_mb)(struct MpegEncContext *s); -+ -+ /** -+ * Initialize the hwaccel private data. -+ * -+ * This will be called from ff_get_format(), after hwaccel and -+ * hwaccel_context are set and the hwaccel private data in AVCodecInternal -+ * is allocated. -+ */ -+ int (*init)(AVCodecContext *avctx); -+ -+ /** -+ * Uninitialize the hwaccel private data. -+ * -+ * This will be called from get_format() or avcodec_close(), after hwaccel -+ * and hwaccel_context are already uninitialized. -+ */ -+ int (*uninit)(AVCodecContext *avctx); -+ -+ /** -+ * Size of the private data to allocate in -+ * AVCodecInternal.hwaccel_priv_data. -+ */ -+ int priv_data_size; -+ -+ /** -+ * Internal hwaccel capabilities. -+ */ -+ int caps_internal; -+ -+ /** -+ * Fill the given hw_frames context with current codec parameters. Called -+ * from get_format. Refer to avcodec_get_hw_frames_parameters() for -+ * details. -+ * -+ * This CAN be called before AVHWAccel.init is called, and you must assume -+ * that avctx->hwaccel_priv_data is invalid. -+ */ -+ int (*frame_params)(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx); -+} AVHWAccel; -+ -+/** -+ * HWAccel is experimental and is thus avoided in favor of non experimental -+ * codecs -+ */ -+#define AV_HWACCEL_CODEC_CAP_EXPERIMENTAL 0x0200 -+ -+/** -+ * Hardware acceleration should be used for decoding even if the codec level -+ * used is unknown or higher than the maximum supported level reported by the -+ * hardware driver. -+ * -+ * It's generally a good idea to pass this flag unless you have a specific -+ * reason not to, as hardware tends to under-report supported levels. -+ */ -+#define AV_HWACCEL_FLAG_IGNORE_LEVEL (1 << 0) -+ -+/** -+ * Hardware acceleration can output YUV pixel formats with a different chroma -+ * sampling than 4:2:0 and/or other than 8 bits per component. -+ */ -+#define AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH (1 << 1) -+ -+/** -+ * Hardware acceleration should still be attempted for decoding when the -+ * codec profile does not match the reported capabilities of the hardware. -+ * -+ * For example, this can be used to try to decode baseline profile H.264 -+ * streams in hardware - it will often succeed, because many streams marked -+ * as baseline profile actually conform to constrained baseline profile. -+ * -+ * @warning If the stream is actually not supported then the behaviour is -+ * undefined, and may include returning entirely incorrect output -+ * while indicating success. -+ */ -+#define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH (1 << 2) -+ -+/** -+ * @} -+ */ -+ -+#if FF_API_AVPICTURE -+/** -+ * @defgroup lavc_picture AVPicture -+ * -+ * Functions for working with AVPicture -+ * @{ -+ */ -+ -+/** -+ * Picture data structure. -+ * -+ * Up to four components can be stored into it, the last component is -+ * alpha. -+ * @deprecated use AVFrame or imgutils functions instead -+ */ -+typedef struct AVPicture { -+ attribute_deprecated -+ uint8_t *data[AV_NUM_DATA_POINTERS]; ///< pointers to the image data planes -+ attribute_deprecated -+ int linesize[AV_NUM_DATA_POINTERS]; ///< number of bytes per line -+} AVPicture; -+ -+/** -+ * @} -+ */ -+#endif -+ -+enum AVSubtitleType { -+ SUBTITLE_NONE, -+ -+ SUBTITLE_BITMAP, ///< A bitmap, pict will be set -+ -+ /** -+ * Plain text, the text field must be set by the decoder and is -+ * authoritative. ass and pict fields may contain approximations. -+ */ -+ SUBTITLE_TEXT, -+ -+ /** -+ * Formatted text, the ass field must be set by the decoder and is -+ * authoritative. pict and text fields may contain approximations. -+ */ -+ SUBTITLE_ASS, -+}; -+ -+#define AV_SUBTITLE_FLAG_FORCED 0x00000001 -+ -+typedef struct AVSubtitleRect { -+ int x; ///< top left corner of pict, undefined when pict is not set -+ int y; ///< top left corner of pict, undefined when pict is not set -+ int w; ///< width of pict, undefined when pict is not set -+ int h; ///< height of pict, undefined when pict is not set -+ int nb_colors; ///< number of colors in pict, undefined when pict is not set -+ -+#if FF_API_AVPICTURE -+ /** -+ * @deprecated unused -+ */ -+ attribute_deprecated -+ AVPicture pict; -+#endif -+ /** -+ * data+linesize for the bitmap of this subtitle. -+ * Can be set for text/ass as well once they are rendered. -+ */ -+ uint8_t *data[4]; -+ int linesize[4]; -+ -+ enum AVSubtitleType type; -+ -+ char *text; ///< 0 terminated plain UTF-8 text -+ -+ /** -+ * 0 terminated ASS/SSA compatible event line. -+ * The presentation of this is unaffected by the other values in this -+ * struct. -+ */ -+ char *ass; -+ -+ int flags; -+} AVSubtitleRect; -+ -+typedef struct AVSubtitle { -+ uint16_t format; /* 0 = graphics */ -+ uint32_t start_display_time; /* relative to packet pts, in ms */ -+ uint32_t end_display_time; /* relative to packet pts, in ms */ -+ unsigned num_rects; -+ AVSubtitleRect **rects; -+ int64_t pts; ///< Same as packet pts, in AV_TIME_BASE -+} AVSubtitle; -+ -+/** -+ * This struct describes the properties of an encoded stream. -+ * -+ * sizeof(AVCodecParameters) is not a part of the public ABI, this struct must -+ * be allocated with avcodec_parameters_alloc() and freed with -+ * avcodec_parameters_free(). -+ */ -+typedef struct AVCodecParameters { -+ /** -+ * General type of the encoded data. -+ */ -+ enum AVMediaType codec_type; -+ /** -+ * Specific type of the encoded data (the codec used). -+ */ -+ enum AVCodecID codec_id; -+ /** -+ * Additional information about the codec (corresponds to the AVI FOURCC). -+ */ -+ uint32_t codec_tag; -+ -+ /** -+ * Extra binary data needed for initializing the decoder, codec-dependent. -+ * -+ * Must be allocated with av_malloc() and will be freed by -+ * avcodec_parameters_free(). The allocated size of extradata must be at -+ * least extradata_size + AV_INPUT_BUFFER_PADDING_SIZE, with the padding -+ * bytes zeroed. -+ */ -+ uint8_t *extradata; -+ /** -+ * Size of the extradata content in bytes. -+ */ -+ int extradata_size; -+ -+ /** -+ * - video: the pixel format, the value corresponds to enum AVPixelFormat. -+ * - audio: the sample format, the value corresponds to enum AVSampleFormat. -+ */ -+ int format; -+ -+ /** -+ * The average bitrate of the encoded data (in bits per second). -+ */ -+ int64_t bit_rate; -+ -+ /** -+ * The number of bits per sample in the codedwords. -+ * -+ * This is basically the bitrate per sample. It is mandatory for a bunch of -+ * formats to actually decode them. It's the number of bits for one sample in -+ * the actual coded bitstream. -+ * -+ * This could be for example 4 for ADPCM -+ * For PCM formats this matches bits_per_raw_sample -+ * Can be 0 -+ */ -+ int bits_per_coded_sample; -+ -+ /** -+ * This is the number of valid bits in each output sample. If the -+ * sample format has more bits, the least significant bits are additional -+ * padding bits, which are always 0. Use right shifts to reduce the sample -+ * to its actual size. For example, audio formats with 24 bit samples will -+ * have bits_per_raw_sample set to 24, and format set to AV_SAMPLE_FMT_S32. -+ * To get the original sample use "(int32_t)sample >> 8"." -+ * -+ * For ADPCM this might be 12 or 16 or similar -+ * Can be 0 -+ */ -+ int bits_per_raw_sample; -+ -+ /** -+ * Codec-specific bitstream restrictions that the stream conforms to. -+ */ -+ int profile; -+ int level; -+ -+ /** -+ * Video only. The dimensions of the video frame in pixels. -+ */ -+ int width; -+ int height; -+ -+ /** -+ * Video only. The aspect ratio (width / height) which a single pixel -+ * should have when displayed. -+ * -+ * When the aspect ratio is unknown / undefined, the numerator should be -+ * set to 0 (the denominator may have any value). -+ */ -+ AVRational sample_aspect_ratio; -+ -+ /** -+ * Video only. The order of the fields in interlaced video. -+ */ -+ enum AVFieldOrder field_order; -+ -+ /** -+ * Video only. Additional colorspace characteristics. -+ */ -+ enum AVColorRange color_range; -+ enum AVColorPrimaries color_primaries; -+ enum AVColorTransferCharacteristic color_trc; -+ enum AVColorSpace color_space; -+ enum AVChromaLocation chroma_location; -+ -+ /** -+ * Video only. Number of delayed frames. -+ */ -+ int video_delay; -+ -+ /** -+ * Audio only. The channel layout bitmask. May be 0 if the channel layout is -+ * unknown or unspecified, otherwise the number of bits set must be equal to -+ * the channels field. -+ */ -+ uint64_t channel_layout; -+ /** -+ * Audio only. The number of audio channels. -+ */ -+ int channels; -+ /** -+ * Audio only. The number of audio samples per second. -+ */ -+ int sample_rate; -+ /** -+ * Audio only. The number of bytes per coded audio frame, required by some -+ * formats. -+ * -+ * Corresponds to nBlockAlign in WAVEFORMATEX. -+ */ -+ int block_align; -+ /** -+ * Audio only. Audio frame size, if known. Required by some formats to be static. -+ */ -+ int frame_size; -+ -+ /** -+ * Audio only. The amount of padding (in samples) inserted by the encoder at -+ * the beginning of the audio. I.e. this number of leading decoded samples -+ * must be discarded by the caller to get the original audio without leading -+ * padding. -+ */ -+ int initial_padding; -+ /** -+ * Audio only. The amount of padding (in samples) appended by the encoder to -+ * the end of the audio. I.e. this number of decoded samples must be -+ * discarded by the caller from the end of the stream to get the original -+ * audio without any trailing padding. -+ */ -+ int trailing_padding; -+ /** -+ * Audio only. Number of samples to skip after a discontinuity. -+ */ -+ int seek_preroll; -+} AVCodecParameters; -+ -+/** -+ * Iterate over all registered codecs. -+ * -+ * @param opaque a pointer where libavcodec will store the iteration state. Must -+ * point to NULL to start the iteration. -+ * -+ * @return the next registered codec or NULL when the iteration is -+ * finished -+ */ -+const AVCodec *av_codec_iterate(void **opaque); -+ -+#if FF_API_NEXT -+/** -+ * If c is NULL, returns the first registered codec, -+ * if c is non-NULL, returns the next registered codec after c, -+ * or NULL if c is the last one. -+ */ -+attribute_deprecated -+AVCodec *av_codec_next(const AVCodec *c); -+#endif -+ -+/** -+ * Return the LIBAVCODEC_VERSION_INT constant. -+ */ -+unsigned avcodec_version(void); -+ -+/** -+ * Return the libavcodec build-time configuration. -+ */ -+const char *avcodec_configuration(void); -+ -+/** -+ * Return the libavcodec license. -+ */ -+const char *avcodec_license(void); -+ -+#if FF_API_NEXT -+/** -+ * Register the codec codec and initialize libavcodec. -+ * -+ * @warning either this function or avcodec_register_all() must be called -+ * before any other libavcodec functions. -+ * -+ * @see avcodec_register_all() -+ */ -+attribute_deprecated -+void avcodec_register(AVCodec *codec); -+ -+/** -+ * Register all the codecs, parsers and bitstream filters which were enabled at -+ * configuration time. If you do not call this function you can select exactly -+ * which formats you want to support, by using the individual registration -+ * functions. -+ * -+ * @see avcodec_register -+ * @see av_register_codec_parser -+ * @see av_register_bitstream_filter -+ */ -+attribute_deprecated -+void avcodec_register_all(void); -+#endif -+ -+/** -+ * Allocate an AVCodecContext and set its fields to default values. The -+ * resulting struct should be freed with avcodec_free_context(). -+ * -+ * @param codec if non-NULL, allocate private data and initialize defaults -+ * for the given codec. It is illegal to then call avcodec_open2() -+ * with a different codec. -+ * If NULL, then the codec-specific defaults won't be initialized, -+ * which may result in suboptimal default settings (this is -+ * important mainly for encoders, e.g. libx264). -+ * -+ * @return An AVCodecContext filled with default values or NULL on failure. -+ */ -+AVCodecContext *avcodec_alloc_context3(const AVCodec *codec); -+ -+/** -+ * Free the codec context and everything associated with it and write NULL to -+ * the provided pointer. -+ */ -+void avcodec_free_context(AVCodecContext **avctx); -+ -+#if FF_API_GET_CONTEXT_DEFAULTS -+/** -+ * @deprecated This function should not be used, as closing and opening a codec -+ * context multiple time is not supported. A new codec context should be -+ * allocated for each new use. -+ */ -+int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec); -+#endif -+ -+/** -+ * Get the AVClass for AVCodecContext. It can be used in combination with -+ * AV_OPT_SEARCH_FAKE_OBJ for examining options. -+ * -+ * @see av_opt_find(). -+ */ -+const AVClass *avcodec_get_class(void); -+ -+#if FF_API_COPY_CONTEXT -+/** -+ * Get the AVClass for AVFrame. It can be used in combination with -+ * AV_OPT_SEARCH_FAKE_OBJ for examining options. -+ * -+ * @see av_opt_find(). -+ */ -+const AVClass *avcodec_get_frame_class(void); -+ -+/** -+ * Get the AVClass for AVSubtitleRect. It can be used in combination with -+ * AV_OPT_SEARCH_FAKE_OBJ for examining options. -+ * -+ * @see av_opt_find(). -+ */ -+const AVClass *avcodec_get_subtitle_rect_class(void); -+ -+/** -+ * Copy the settings of the source AVCodecContext into the destination -+ * AVCodecContext. The resulting destination codec context will be -+ * unopened, i.e. you are required to call avcodec_open2() before you -+ * can use this AVCodecContext to decode/encode video/audio data. -+ * -+ * @param dest target codec context, should be initialized with -+ * avcodec_alloc_context3(NULL), but otherwise uninitialized -+ * @param src source codec context -+ * @return AVERROR() on error (e.g. memory allocation error), 0 on success -+ * -+ * @deprecated The semantics of this function are ill-defined and it should not -+ * be used. If you need to transfer the stream parameters from one codec context -+ * to another, use an intermediate AVCodecParameters instance and the -+ * avcodec_parameters_from_context() / avcodec_parameters_to_context() -+ * functions. -+ */ -+attribute_deprecated -+int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src); -+#endif -+ -+/** -+ * Allocate a new AVCodecParameters and set its fields to default values -+ * (unknown/invalid/0). The returned struct must be freed with -+ * avcodec_parameters_free(). -+ */ -+AVCodecParameters *avcodec_parameters_alloc(void); -+ -+/** -+ * Free an AVCodecParameters instance and everything associated with it and -+ * write NULL to the supplied pointer. -+ */ -+void avcodec_parameters_free(AVCodecParameters **par); -+ -+/** -+ * Copy the contents of src to dst. Any allocated fields in dst are freed and -+ * replaced with newly allocated duplicates of the corresponding fields in src. -+ * -+ * @return >= 0 on success, a negative AVERROR code on failure. -+ */ -+int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src); -+ -+/** -+ * Fill the parameters struct based on the values from the supplied codec -+ * context. Any allocated fields in par are freed and replaced with duplicates -+ * of the corresponding fields in codec. -+ * -+ * @return >= 0 on success, a negative AVERROR code on failure -+ */ -+int avcodec_parameters_from_context(AVCodecParameters *par, -+ const AVCodecContext *codec); -+ -+/** -+ * Fill the codec context based on the values from the supplied codec -+ * parameters. Any allocated fields in codec that have a corresponding field in -+ * par are freed and replaced with duplicates of the corresponding field in par. -+ * Fields in codec that do not have a counterpart in par are not touched. -+ * -+ * @return >= 0 on success, a negative AVERROR code on failure. -+ */ -+int avcodec_parameters_to_context(AVCodecContext *codec, -+ const AVCodecParameters *par); -+ -+/** -+ * Initialize the AVCodecContext to use the given AVCodec. Prior to using this -+ * function the context has to be allocated with avcodec_alloc_context3(). -+ * -+ * The functions avcodec_find_decoder_by_name(), avcodec_find_encoder_by_name(), -+ * avcodec_find_decoder() and avcodec_find_encoder() provide an easy way for -+ * retrieving a codec. -+ * -+ * @warning This function is not thread safe! -+ * -+ * @note Always call this function before using decoding routines (such as -+ * @ref avcodec_receive_frame()). -+ * -+ * @code -+ * avcodec_register_all(); -+ * av_dict_set(&opts, "b", "2.5M", 0); -+ * codec = avcodec_find_decoder(AV_CODEC_ID_H264); -+ * if (!codec) -+ * exit(1); -+ * -+ * context = avcodec_alloc_context3(codec); -+ * -+ * if (avcodec_open2(context, codec, opts) < 0) -+ * exit(1); -+ * @endcode -+ * -+ * @param avctx The context to initialize. -+ * @param codec The codec to open this context for. If a non-NULL codec has been -+ * previously passed to avcodec_alloc_context3() or -+ * for this context, then this parameter MUST be either NULL or -+ * equal to the previously passed codec. -+ * @param options A dictionary filled with AVCodecContext and codec-private options. -+ * On return this object will be filled with options that were not found. -+ * -+ * @return zero on success, a negative value on error -+ * @see avcodec_alloc_context3(), avcodec_find_decoder(), avcodec_find_encoder(), -+ * av_dict_set(), av_opt_find(). -+ */ -+int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); -+ -+/** -+ * Close a given AVCodecContext and free all the data associated with it -+ * (but not the AVCodecContext itself). -+ * -+ * Calling this function on an AVCodecContext that hasn't been opened will free -+ * the codec-specific data allocated in avcodec_alloc_context3() with a non-NULL -+ * codec. Subsequent calls will do nothing. -+ * -+ * @note Do not use this function. Use avcodec_free_context() to destroy a -+ * codec context (either open or closed). Opening and closing a codec context -+ * multiple times is not supported anymore -- use multiple codec contexts -+ * instead. -+ */ -+int avcodec_close(AVCodecContext *avctx); -+ -+/** -+ * Free all allocated data in the given subtitle struct. -+ * -+ * @param sub AVSubtitle to free. -+ */ -+void avsubtitle_free(AVSubtitle *sub); -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @addtogroup lavc_packet -+ * @{ -+ */ -+ -+/** -+ * Allocate an AVPacket and set its fields to default values. The resulting -+ * struct must be freed using av_packet_free(). -+ * -+ * @return An AVPacket filled with default values or NULL on failure. -+ * -+ * @note this only allocates the AVPacket itself, not the data buffers. Those -+ * must be allocated through other means such as av_new_packet. -+ * -+ * @see av_new_packet -+ */ -+AVPacket *av_packet_alloc(void); -+ -+/** -+ * Create a new packet that references the same data as src. -+ * -+ * This is a shortcut for av_packet_alloc()+av_packet_ref(). -+ * -+ * @return newly created AVPacket on success, NULL on error. -+ * -+ * @see av_packet_alloc -+ * @see av_packet_ref -+ */ -+AVPacket *av_packet_clone(const AVPacket *src); -+ -+/** -+ * Free the packet, if the packet is reference counted, it will be -+ * unreferenced first. -+ * -+ * @param pkt packet to be freed. The pointer will be set to NULL. -+ * @note passing NULL is a no-op. -+ */ -+void av_packet_free(AVPacket **pkt); -+ -+/** -+ * Initialize optional fields of a packet with default values. -+ * -+ * Note, this does not touch the data and size members, which have to be -+ * initialized separately. -+ * -+ * @param pkt packet -+ */ -+void av_init_packet(AVPacket *pkt); -+ -+/** -+ * Allocate the payload of a packet and initialize its fields with -+ * default values. -+ * -+ * @param pkt packet -+ * @param size wanted payload size -+ * @return 0 if OK, AVERROR_xxx otherwise -+ */ -+int av_new_packet(AVPacket *pkt, int size); -+ -+/** -+ * Reduce packet size, correctly zeroing padding -+ * -+ * @param pkt packet -+ * @param size new size -+ */ -+void av_shrink_packet(AVPacket *pkt, int size); -+ -+/** -+ * Increase packet size, correctly zeroing padding -+ * -+ * @param pkt packet -+ * @param grow_by number of bytes by which to increase the size of the packet -+ */ -+int av_grow_packet(AVPacket *pkt, int grow_by); -+ -+/** -+ * Initialize a reference-counted packet from av_malloc()ed data. -+ * -+ * @param pkt packet to be initialized. This function will set the data, size, -+ * buf and destruct fields, all others are left untouched. -+ * @param data Data allocated by av_malloc() to be used as packet data. If this -+ * function returns successfully, the data is owned by the underlying AVBuffer. -+ * The caller may not access the data through other means. -+ * @param size size of data in bytes, without the padding. I.e. the full buffer -+ * size is assumed to be size + AV_INPUT_BUFFER_PADDING_SIZE. -+ * -+ * @return 0 on success, a negative AVERROR on error -+ */ -+int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size); -+ -+#if FF_API_AVPACKET_OLD_API -+/** -+ * @warning This is a hack - the packet memory allocation stuff is broken. The -+ * packet is allocated if it was not really allocated. -+ * -+ * @deprecated Use av_packet_ref or av_packet_make_refcounted -+ */ -+attribute_deprecated -+int av_dup_packet(AVPacket *pkt); -+/** -+ * Copy packet, including contents -+ * -+ * @return 0 on success, negative AVERROR on fail -+ * -+ * @deprecated Use av_packet_ref -+ */ -+attribute_deprecated -+int av_copy_packet(AVPacket *dst, const AVPacket *src); -+ -+/** -+ * Copy packet side data -+ * -+ * @return 0 on success, negative AVERROR on fail -+ * -+ * @deprecated Use av_packet_copy_props -+ */ -+attribute_deprecated -+int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src); -+ -+/** -+ * Free a packet. -+ * -+ * @deprecated Use av_packet_unref -+ * -+ * @param pkt packet to free -+ */ -+attribute_deprecated -+void av_free_packet(AVPacket *pkt); -+#endif -+/** -+ * Allocate new information of a packet. -+ * -+ * @param pkt packet -+ * @param type side information type -+ * @param size side information size -+ * @return pointer to fresh allocated data or NULL otherwise -+ */ -+uint8_t* av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, -+ int size); -+ -+/** -+ * Wrap an existing array as a packet side data. -+ * -+ * @param pkt packet -+ * @param type side information type -+ * @param data the side data array. It must be allocated with the av_malloc() -+ * family of functions. The ownership of the data is transferred to -+ * pkt. -+ * @param size side information size -+ * @return a non-negative number on success, a negative AVERROR code on -+ * failure. On failure, the packet is unchanged and the data remains -+ * owned by the caller. -+ */ -+int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, -+ uint8_t *data, size_t size); -+ -+/** -+ * Shrink the already allocated side data buffer -+ * -+ * @param pkt packet -+ * @param type side information type -+ * @param size new side information size -+ * @return 0 on success, < 0 on failure -+ */ -+int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, -+ int size); -+ -+/** -+ * Get side information from packet. -+ * -+ * @param pkt packet -+ * @param type desired side information type -+ * @param size pointer for side information size to store (optional) -+ * @return pointer to data if present or NULL otherwise -+ */ -+uint8_t* av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, -+ int *size); -+ -+#if FF_API_MERGE_SD_API -+attribute_deprecated -+int av_packet_merge_side_data(AVPacket *pkt); -+ -+attribute_deprecated -+int av_packet_split_side_data(AVPacket *pkt); -+#endif -+ -+const char *av_packet_side_data_name(enum AVPacketSideDataType type); -+ -+/** -+ * Pack a dictionary for use in side_data. -+ * -+ * @param dict The dictionary to pack. -+ * @param size pointer to store the size of the returned data -+ * @return pointer to data if successful, NULL otherwise -+ */ -+uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size); -+/** -+ * Unpack a dictionary from side_data. -+ * -+ * @param data data from side_data -+ * @param size size of the data -+ * @param dict the metadata storage dictionary -+ * @return 0 on success, < 0 on failure -+ */ -+int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict); -+ -+ -+/** -+ * Convenience function to free all the side data stored. -+ * All the other fields stay untouched. -+ * -+ * @param pkt packet -+ */ -+void av_packet_free_side_data(AVPacket *pkt); -+ -+/** -+ * Setup a new reference to the data described by a given packet -+ * -+ * If src is reference-counted, setup dst as a new reference to the -+ * buffer in src. Otherwise allocate a new buffer in dst and copy the -+ * data from src into it. -+ * -+ * All the other fields are copied from src. -+ * -+ * @see av_packet_unref -+ * -+ * @param dst Destination packet -+ * @param src Source packet -+ * -+ * @return 0 on success, a negative AVERROR on error. -+ */ -+int av_packet_ref(AVPacket *dst, const AVPacket *src); -+ -+/** -+ * Wipe the packet. -+ * -+ * Unreference the buffer referenced by the packet and reset the -+ * remaining packet fields to their default values. -+ * -+ * @param pkt The packet to be unreferenced. -+ */ -+void av_packet_unref(AVPacket *pkt); -+ -+/** -+ * Move every field in src to dst and reset src. -+ * -+ * @see av_packet_unref -+ * -+ * @param src Source packet, will be reset -+ * @param dst Destination packet -+ */ -+void av_packet_move_ref(AVPacket *dst, AVPacket *src); -+ -+/** -+ * Copy only "properties" fields from src to dst. -+ * -+ * Properties for the purpose of this function are all the fields -+ * beside those related to the packet data (buf, data, size) -+ * -+ * @param dst Destination packet -+ * @param src Source packet -+ * -+ * @return 0 on success AVERROR on failure. -+ */ -+int av_packet_copy_props(AVPacket *dst, const AVPacket *src); -+ -+/** -+ * Ensure the data described by a given packet is reference counted. -+ * -+ * @note This function does not ensure that the reference will be writable. -+ * Use av_packet_make_writable instead for that purpose. -+ * -+ * @see av_packet_ref -+ * @see av_packet_make_writable -+ * -+ * @param pkt packet whose data should be made reference counted. -+ * -+ * @return 0 on success, a negative AVERROR on error. On failure, the -+ * packet is unchanged. -+ */ -+int av_packet_make_refcounted(AVPacket *pkt); -+ -+/** -+ * Create a writable reference for the data described by a given packet, -+ * avoiding data copy if possible. -+ * -+ * @param pkt Packet whose data should be made writable. -+ * -+ * @return 0 on success, a negative AVERROR on failure. On failure, the -+ * packet is unchanged. -+ */ -+int av_packet_make_writable(AVPacket *pkt); -+ -+/** -+ * Convert valid timing fields (timestamps / durations) in a packet from one -+ * timebase to another. Timestamps with unknown values (AV_NOPTS_VALUE) will be -+ * ignored. -+ * -+ * @param pkt packet on which the conversion will be performed -+ * @param tb_src source timebase, in which the timing fields in pkt are -+ * expressed -+ * @param tb_dst destination timebase, to which the timing fields will be -+ * converted -+ */ -+void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst); -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @addtogroup lavc_decoding -+ * @{ -+ */ -+ -+/** -+ * Find a registered decoder with a matching codec ID. -+ * -+ * @param id AVCodecID of the requested decoder -+ * @return A decoder if one was found, NULL otherwise. -+ */ -+AVCodec *avcodec_find_decoder(enum AVCodecID id); -+ -+/** -+ * Find a registered decoder with the specified name. -+ * -+ * @param name name of the requested decoder -+ * @return A decoder if one was found, NULL otherwise. -+ */ -+AVCodec *avcodec_find_decoder_by_name(const char *name); -+ -+/** -+ * The default callback for AVCodecContext.get_buffer2(). It is made public so -+ * it can be called by custom get_buffer2() implementations for decoders without -+ * AV_CODEC_CAP_DR1 set. -+ */ -+int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags); -+ -+/** -+ * Modify width and height values so that they will result in a memory -+ * buffer that is acceptable for the codec if you do not use any horizontal -+ * padding. -+ * -+ * May only be used if a codec with AV_CODEC_CAP_DR1 has been opened. -+ */ -+void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height); -+ -+/** -+ * Modify width and height values so that they will result in a memory -+ * buffer that is acceptable for the codec if you also ensure that all -+ * line sizes are a multiple of the respective linesize_align[i]. -+ * -+ * May only be used if a codec with AV_CODEC_CAP_DR1 has been opened. -+ */ -+void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, -+ int linesize_align[AV_NUM_DATA_POINTERS]); -+ -+/** -+ * Converts AVChromaLocation to swscale x/y chroma position. -+ * -+ * The positions represent the chroma (0,0) position in a coordinates system -+ * with luma (0,0) representing the origin and luma(1,1) representing 256,256 -+ * -+ * @param xpos horizontal chroma sample position -+ * @param ypos vertical chroma sample position -+ */ -+int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos); -+ -+/** -+ * Converts swscale x/y chroma position to AVChromaLocation. -+ * -+ * The positions represent the chroma (0,0) position in a coordinates system -+ * with luma (0,0) representing the origin and luma(1,1) representing 256,256 -+ * -+ * @param xpos horizontal chroma sample position -+ * @param ypos vertical chroma sample position -+ */ -+enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos); -+ -+/** -+ * Decode the audio frame of size avpkt->size from avpkt->data into frame. -+ * -+ * Some decoders may support multiple frames in a single AVPacket. Such -+ * decoders would then just decode the first frame and the return value would be -+ * less than the packet size. In this case, avcodec_decode_audio4 has to be -+ * called again with an AVPacket containing the remaining data in order to -+ * decode the second frame, etc... Even if no frames are returned, the packet -+ * needs to be fed to the decoder with remaining data until it is completely -+ * consumed or an error occurs. -+ * -+ * Some decoders (those marked with AV_CODEC_CAP_DELAY) have a delay between input -+ * and output. This means that for some packets they will not immediately -+ * produce decoded output and need to be flushed at the end of decoding to get -+ * all the decoded data. Flushing is done by calling this function with packets -+ * with avpkt->data set to NULL and avpkt->size set to 0 until it stops -+ * returning samples. It is safe to flush even those decoders that are not -+ * marked with AV_CODEC_CAP_DELAY, then no samples will be returned. -+ * -+ * @warning The input buffer, avpkt->data must be AV_INPUT_BUFFER_PADDING_SIZE -+ * larger than the actual read bytes because some optimized bitstream -+ * readers read 32 or 64 bits at once and could read over the end. -+ * -+ * @note The AVCodecContext MUST have been opened with @ref avcodec_open2() -+ * before packets may be fed to the decoder. -+ * -+ * @param avctx the codec context -+ * @param[out] frame The AVFrame in which to store decoded audio samples. -+ * The decoder will allocate a buffer for the decoded frame by -+ * calling the AVCodecContext.get_buffer2() callback. -+ * When AVCodecContext.refcounted_frames is set to 1, the frame is -+ * reference counted and the returned reference belongs to the -+ * caller. The caller must release the frame using av_frame_unref() -+ * when the frame is no longer needed. The caller may safely write -+ * to the frame if av_frame_is_writable() returns 1. -+ * When AVCodecContext.refcounted_frames is set to 0, the returned -+ * reference belongs to the decoder and is valid only until the -+ * next call to this function or until closing or flushing the -+ * decoder. The caller may not write to it. -+ * @param[out] got_frame_ptr Zero if no frame could be decoded, otherwise it is -+ * non-zero. Note that this field being set to zero -+ * does not mean that an error has occurred. For -+ * decoders with AV_CODEC_CAP_DELAY set, no given decode -+ * call is guaranteed to produce a frame. -+ * @param[in] avpkt The input AVPacket containing the input buffer. -+ * At least avpkt->data and avpkt->size should be set. Some -+ * decoders might also require additional fields to be set. -+ * @return A negative error code is returned if an error occurred during -+ * decoding, otherwise the number of bytes consumed from the input -+ * AVPacket is returned. -+ * -+* @deprecated Use avcodec_send_packet() and avcodec_receive_frame(). -+ */ -+attribute_deprecated -+int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, -+ int *got_frame_ptr, const AVPacket *avpkt); -+ -+/** -+ * Decode the video frame of size avpkt->size from avpkt->data into picture. -+ * Some decoders may support multiple frames in a single AVPacket, such -+ * decoders would then just decode the first frame. -+ * -+ * @warning The input buffer must be AV_INPUT_BUFFER_PADDING_SIZE larger than -+ * the actual read bytes because some optimized bitstream readers read 32 or 64 -+ * bits at once and could read over the end. -+ * -+ * @warning The end of the input buffer buf should be set to 0 to ensure that -+ * no overreading happens for damaged MPEG streams. -+ * -+ * @note Codecs which have the AV_CODEC_CAP_DELAY capability set have a delay -+ * between input and output, these need to be fed with avpkt->data=NULL, -+ * avpkt->size=0 at the end to return the remaining frames. -+ * -+ * @note The AVCodecContext MUST have been opened with @ref avcodec_open2() -+ * before packets may be fed to the decoder. -+ * -+ * @param avctx the codec context -+ * @param[out] picture The AVFrame in which the decoded video frame will be stored. -+ * Use av_frame_alloc() to get an AVFrame. The codec will -+ * allocate memory for the actual bitmap by calling the -+ * AVCodecContext.get_buffer2() callback. -+ * When AVCodecContext.refcounted_frames is set to 1, the frame is -+ * reference counted and the returned reference belongs to the -+ * caller. The caller must release the frame using av_frame_unref() -+ * when the frame is no longer needed. The caller may safely write -+ * to the frame if av_frame_is_writable() returns 1. -+ * When AVCodecContext.refcounted_frames is set to 0, the returned -+ * reference belongs to the decoder and is valid only until the -+ * next call to this function or until closing or flushing the -+ * decoder. The caller may not write to it. -+ * -+ * @param[in] avpkt The input AVPacket containing the input buffer. -+ * You can create such packet with av_init_packet() and by then setting -+ * data and size, some decoders might in addition need other fields like -+ * flags&AV_PKT_FLAG_KEY. All decoders are designed to use the least -+ * fields possible. -+ * @param[in,out] got_picture_ptr Zero if no frame could be decompressed, otherwise, it is nonzero. -+ * @return On error a negative value is returned, otherwise the number of bytes -+ * used or zero if no frame could be decompressed. -+ * -+ * @deprecated Use avcodec_send_packet() and avcodec_receive_frame(). -+ */ -+attribute_deprecated -+int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, -+ int *got_picture_ptr, -+ const AVPacket *avpkt); -+ -+/** -+ * Decode a subtitle message. -+ * Return a negative value on error, otherwise return the number of bytes used. -+ * If no subtitle could be decompressed, got_sub_ptr is zero. -+ * Otherwise, the subtitle is stored in *sub. -+ * Note that AV_CODEC_CAP_DR1 is not available for subtitle codecs. This is for -+ * simplicity, because the performance difference is expect to be negligible -+ * and reusing a get_buffer written for video codecs would probably perform badly -+ * due to a potentially very different allocation pattern. -+ * -+ * Some decoders (those marked with AV_CODEC_CAP_DELAY) have a delay between input -+ * and output. This means that for some packets they will not immediately -+ * produce decoded output and need to be flushed at the end of decoding to get -+ * all the decoded data. Flushing is done by calling this function with packets -+ * with avpkt->data set to NULL and avpkt->size set to 0 until it stops -+ * returning subtitles. It is safe to flush even those decoders that are not -+ * marked with AV_CODEC_CAP_DELAY, then no subtitles will be returned. -+ * -+ * @note The AVCodecContext MUST have been opened with @ref avcodec_open2() -+ * before packets may be fed to the decoder. -+ * -+ * @param avctx the codec context -+ * @param[out] sub The Preallocated AVSubtitle in which the decoded subtitle will be stored, -+ * must be freed with avsubtitle_free if *got_sub_ptr is set. -+ * @param[in,out] got_sub_ptr Zero if no subtitle could be decompressed, otherwise, it is nonzero. -+ * @param[in] avpkt The input AVPacket containing the input buffer. -+ */ -+int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, -+ int *got_sub_ptr, -+ AVPacket *avpkt); -+ -+/** -+ * Supply raw packet data as input to a decoder. -+ * -+ * Internally, this call will copy relevant AVCodecContext fields, which can -+ * influence decoding per-packet, and apply them when the packet is actually -+ * decoded. (For example AVCodecContext.skip_frame, which might direct the -+ * decoder to drop the frame contained by the packet sent with this function.) -+ * -+ * @warning The input buffer, avpkt->data must be AV_INPUT_BUFFER_PADDING_SIZE -+ * larger than the actual read bytes because some optimized bitstream -+ * readers read 32 or 64 bits at once and could read over the end. -+ * -+ * @warning Do not mix this API with the legacy API (like avcodec_decode_video2()) -+ * on the same AVCodecContext. It will return unexpected results now -+ * or in future libavcodec versions. -+ * -+ * @note The AVCodecContext MUST have been opened with @ref avcodec_open2() -+ * before packets may be fed to the decoder. -+ * -+ * @param avctx codec context -+ * @param[in] avpkt The input AVPacket. Usually, this will be a single video -+ * frame, or several complete audio frames. -+ * Ownership of the packet remains with the caller, and the -+ * decoder will not write to the packet. The decoder may create -+ * a reference to the packet data (or copy it if the packet is -+ * not reference-counted). -+ * Unlike with older APIs, the packet is always fully consumed, -+ * and if it contains multiple frames (e.g. some audio codecs), -+ * will require you to call avcodec_receive_frame() multiple -+ * times afterwards before you can send a new packet. -+ * It can be NULL (or an AVPacket with data set to NULL and -+ * size set to 0); in this case, it is considered a flush -+ * packet, which signals the end of the stream. Sending the -+ * first flush packet will return success. Subsequent ones are -+ * unnecessary and will return AVERROR_EOF. If the decoder -+ * still has frames buffered, it will return them after sending -+ * a flush packet. -+ * -+ * @return 0 on success, otherwise negative error code: -+ * AVERROR(EAGAIN): input is not accepted in the current state - user -+ * must read output with avcodec_receive_frame() (once -+ * all output is read, the packet should be resent, and -+ * the call will not fail with EAGAIN). -+ * AVERROR_EOF: the decoder has been flushed, and no new packets can -+ * be sent to it (also returned if more than 1 flush -+ * packet is sent) -+ * AVERROR(EINVAL): codec not opened, it is an encoder, or requires flush -+ * AVERROR(ENOMEM): failed to add packet to internal queue, or similar -+ * other errors: legitimate decoding errors -+ */ -+int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt); -+ -+/** -+ * Return decoded output data from a decoder. -+ * -+ * @param avctx codec context -+ * @param frame This will be set to a reference-counted video or audio -+ * frame (depending on the decoder type) allocated by the -+ * decoder. Note that the function will always call -+ * av_frame_unref(frame) before doing anything else. -+ * -+ * @return -+ * 0: success, a frame was returned -+ * AVERROR(EAGAIN): output is not available in this state - user must try -+ * to send new input -+ * AVERROR_EOF: the decoder has been fully flushed, and there will be -+ * no more output frames -+ * AVERROR(EINVAL): codec not opened, or it is an encoder -+ * other negative values: legitimate decoding errors -+ */ -+int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame); -+ -+/** -+ * Supply a raw video or audio frame to the encoder. Use avcodec_receive_packet() -+ * to retrieve buffered output packets. -+ * -+ * @param avctx codec context -+ * @param[in] frame AVFrame containing the raw audio or video frame to be encoded. -+ * Ownership of the frame remains with the caller, and the -+ * encoder will not write to the frame. The encoder may create -+ * a reference to the frame data (or copy it if the frame is -+ * not reference-counted). -+ * It can be NULL, in which case it is considered a flush -+ * packet. This signals the end of the stream. If the encoder -+ * still has packets buffered, it will return them after this -+ * call. Once flushing mode has been entered, additional flush -+ * packets are ignored, and sending frames will return -+ * AVERROR_EOF. -+ * -+ * For audio: -+ * If AV_CODEC_CAP_VARIABLE_FRAME_SIZE is set, then each frame -+ * can have any number of samples. -+ * If it is not set, frame->nb_samples must be equal to -+ * avctx->frame_size for all frames except the last. -+ * The final frame may be smaller than avctx->frame_size. -+ * @return 0 on success, otherwise negative error code: -+ * AVERROR(EAGAIN): input is not accepted in the current state - user -+ * must read output with avcodec_receive_packet() (once -+ * all output is read, the packet should be resent, and -+ * the call will not fail with EAGAIN). -+ * AVERROR_EOF: the encoder has been flushed, and no new frames can -+ * be sent to it -+ * AVERROR(EINVAL): codec not opened, refcounted_frames not set, it is a -+ * decoder, or requires flush -+ * AVERROR(ENOMEM): failed to add packet to internal queue, or similar -+ * other errors: legitimate decoding errors -+ */ -+int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame); -+ -+/** -+ * Read encoded data from the encoder. -+ * -+ * @param avctx codec context -+ * @param avpkt This will be set to a reference-counted packet allocated by the -+ * encoder. Note that the function will always call -+ * av_frame_unref(frame) before doing anything else. -+ * @return 0 on success, otherwise negative error code: -+ * AVERROR(EAGAIN): output is not available in the current state - user -+ * must try to send input -+ * AVERROR_EOF: the encoder has been fully flushed, and there will be -+ * no more output packets -+ * AVERROR(EINVAL): codec not opened, or it is an encoder -+ * other errors: legitimate decoding errors -+ */ -+int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt); -+ -+/** -+ * Create and return a AVHWFramesContext with values adequate for hardware -+ * decoding. This is meant to get called from the get_format callback, and is -+ * a helper for preparing a AVHWFramesContext for AVCodecContext.hw_frames_ctx. -+ * This API is for decoding with certain hardware acceleration modes/APIs only. -+ * -+ * The returned AVHWFramesContext is not initialized. The caller must do this -+ * with av_hwframe_ctx_init(). -+ * -+ * Calling this function is not a requirement, but makes it simpler to avoid -+ * codec or hardware API specific details when manually allocating frames. -+ * -+ * Alternatively to this, an API user can set AVCodecContext.hw_device_ctx, -+ * which sets up AVCodecContext.hw_frames_ctx fully automatically, and makes -+ * it unnecessary to call this function or having to care about -+ * AVHWFramesContext initialization at all. -+ * -+ * There are a number of requirements for calling this function: -+ * -+ * - It must be called from get_format with the same avctx parameter that was -+ * passed to get_format. Calling it outside of get_format is not allowed, and -+ * can trigger undefined behavior. -+ * - The function is not always supported (see description of return values). -+ * Even if this function returns successfully, hwaccel initialization could -+ * fail later. (The degree to which implementations check whether the stream -+ * is actually supported varies. Some do this check only after the user's -+ * get_format callback returns.) -+ * - The hw_pix_fmt must be one of the choices suggested by get_format. If the -+ * user decides to use a AVHWFramesContext prepared with this API function, -+ * the user must return the same hw_pix_fmt from get_format. -+ * - The device_ref passed to this function must support the given hw_pix_fmt. -+ * - After calling this API function, it is the user's responsibility to -+ * initialize the AVHWFramesContext (returned by the out_frames_ref parameter), -+ * and to set AVCodecContext.hw_frames_ctx to it. If done, this must be done -+ * before returning from get_format (this is implied by the normal -+ * AVCodecContext.hw_frames_ctx API rules). -+ * - The AVHWFramesContext parameters may change every time time get_format is -+ * called. Also, AVCodecContext.hw_frames_ctx is reset before get_format. So -+ * you are inherently required to go through this process again on every -+ * get_format call. -+ * - It is perfectly possible to call this function without actually using -+ * the resulting AVHWFramesContext. One use-case might be trying to reuse a -+ * previously initialized AVHWFramesContext, and calling this API function -+ * only to test whether the required frame parameters have changed. -+ * - Fields that use dynamically allocated values of any kind must not be set -+ * by the user unless setting them is explicitly allowed by the documentation. -+ * If the user sets AVHWFramesContext.free and AVHWFramesContext.user_opaque, -+ * the new free callback must call the potentially set previous free callback. -+ * This API call may set any dynamically allocated fields, including the free -+ * callback. -+ * -+ * The function will set at least the following fields on AVHWFramesContext -+ * (potentially more, depending on hwaccel API): -+ * -+ * - All fields set by av_hwframe_ctx_alloc(). -+ * - Set the format field to hw_pix_fmt. -+ * - Set the sw_format field to the most suited and most versatile format. (An -+ * implication is that this will prefer generic formats over opaque formats -+ * with arbitrary restrictions, if possible.) -+ * - Set the width/height fields to the coded frame size, rounded up to the -+ * API-specific minimum alignment. -+ * - Only _if_ the hwaccel requires a pre-allocated pool: set the initial_pool_size -+ * field to the number of maximum reference surfaces possible with the codec, -+ * plus 1 surface for the user to work (meaning the user can safely reference -+ * at most 1 decoded surface at a time), plus additional buffering introduced -+ * by frame threading. If the hwaccel does not require pre-allocation, the -+ * field is left to 0, and the decoder will allocate new surfaces on demand -+ * during decoding. -+ * - Possibly AVHWFramesContext.hwctx fields, depending on the underlying -+ * hardware API. -+ * -+ * Essentially, out_frames_ref returns the same as av_hwframe_ctx_alloc(), but -+ * with basic frame parameters set. -+ * -+ * The function is stateless, and does not change the AVCodecContext or the -+ * device_ref AVHWDeviceContext. -+ * -+ * @param avctx The context which is currently calling get_format, and which -+ * implicitly contains all state needed for filling the returned -+ * AVHWFramesContext properly. -+ * @param device_ref A reference to the AVHWDeviceContext describing the device -+ * which will be used by the hardware decoder. -+ * @param hw_pix_fmt The hwaccel format you are going to return from get_format. -+ * @param out_frames_ref On success, set to a reference to an _uninitialized_ -+ * AVHWFramesContext, created from the given device_ref. -+ * Fields will be set to values required for decoding. -+ * Not changed if an error is returned. -+ * @return zero on success, a negative value on error. The following error codes -+ * have special semantics: -+ * AVERROR(ENOENT): the decoder does not support this functionality. Setup -+ * is always manual, or it is a decoder which does not -+ * support setting AVCodecContext.hw_frames_ctx at all, -+ * or it is a software format. -+ * AVERROR(EINVAL): it is known that hardware decoding is not supported for -+ * this configuration, or the device_ref is not supported -+ * for the hwaccel referenced by hw_pix_fmt. -+ */ -+int avcodec_get_hw_frames_parameters(AVCodecContext *avctx, -+ AVBufferRef *device_ref, -+ enum AVPixelFormat hw_pix_fmt, -+ AVBufferRef **out_frames_ref); -+ -+ -+ -+/** -+ * @defgroup lavc_parsing Frame parsing -+ * @{ -+ */ -+ -+enum AVPictureStructure { -+ AV_PICTURE_STRUCTURE_UNKNOWN, //< unknown -+ AV_PICTURE_STRUCTURE_TOP_FIELD, //< coded as top field -+ AV_PICTURE_STRUCTURE_BOTTOM_FIELD, //< coded as bottom field -+ AV_PICTURE_STRUCTURE_FRAME, //< coded as frame -+}; -+ -+typedef struct AVCodecParserContext { -+ void *priv_data; -+ struct AVCodecParser *parser; -+ int64_t frame_offset; /* offset of the current frame */ -+ int64_t cur_offset; /* current offset -+ (incremented by each av_parser_parse()) */ -+ int64_t next_frame_offset; /* offset of the next frame */ -+ /* video info */ -+ int pict_type; /* XXX: Put it back in AVCodecContext. */ -+ /** -+ * This field is used for proper frame duration computation in lavf. -+ * It signals, how much longer the frame duration of the current frame -+ * is compared to normal frame duration. -+ * -+ * frame_duration = (1 + repeat_pict) * time_base -+ * -+ * It is used by codecs like H.264 to display telecined material. -+ */ -+ int repeat_pict; /* XXX: Put it back in AVCodecContext. */ -+ int64_t pts; /* pts of the current frame */ -+ int64_t dts; /* dts of the current frame */ -+ -+ /* private data */ -+ int64_t last_pts; -+ int64_t last_dts; -+ int fetch_timestamp; -+ -+#define AV_PARSER_PTS_NB 4 -+ int cur_frame_start_index; -+ int64_t cur_frame_offset[AV_PARSER_PTS_NB]; -+ int64_t cur_frame_pts[AV_PARSER_PTS_NB]; -+ int64_t cur_frame_dts[AV_PARSER_PTS_NB]; -+ -+ int flags; -+#define PARSER_FLAG_COMPLETE_FRAMES 0x0001 -+#define PARSER_FLAG_ONCE 0x0002 -+/// Set if the parser has a valid file offset -+#define PARSER_FLAG_FETCHED_OFFSET 0x0004 -+#define PARSER_FLAG_USE_CODEC_TS 0x1000 -+ -+ int64_t offset; ///< byte offset from starting packet start -+ int64_t cur_frame_end[AV_PARSER_PTS_NB]; -+ -+ /** -+ * Set by parser to 1 for key frames and 0 for non-key frames. -+ * It is initialized to -1, so if the parser doesn't set this flag, -+ * old-style fallback using AV_PICTURE_TYPE_I picture type as key frames -+ * will be used. -+ */ -+ int key_frame; -+ -+#if FF_API_CONVERGENCE_DURATION -+ /** -+ * @deprecated unused -+ */ -+ attribute_deprecated -+ int64_t convergence_duration; -+#endif -+ -+ // Timestamp generation support: -+ /** -+ * Synchronization point for start of timestamp generation. -+ * -+ * Set to >0 for sync point, 0 for no sync point and <0 for undefined -+ * (default). -+ * -+ * For example, this corresponds to presence of H.264 buffering period -+ * SEI message. -+ */ -+ int dts_sync_point; -+ -+ /** -+ * Offset of the current timestamp against last timestamp sync point in -+ * units of AVCodecContext.time_base. -+ * -+ * Set to INT_MIN when dts_sync_point unused. Otherwise, it must -+ * contain a valid timestamp offset. -+ * -+ * Note that the timestamp of sync point has usually a nonzero -+ * dts_ref_dts_delta, which refers to the previous sync point. Offset of -+ * the next frame after timestamp sync point will be usually 1. -+ * -+ * For example, this corresponds to H.264 cpb_removal_delay. -+ */ -+ int dts_ref_dts_delta; -+ -+ /** -+ * Presentation delay of current frame in units of AVCodecContext.time_base. -+ * -+ * Set to INT_MIN when dts_sync_point unused. Otherwise, it must -+ * contain valid non-negative timestamp delta (presentation time of a frame -+ * must not lie in the past). -+ * -+ * This delay represents the difference between decoding and presentation -+ * time of the frame. -+ * -+ * For example, this corresponds to H.264 dpb_output_delay. -+ */ -+ int pts_dts_delta; -+ -+ /** -+ * Position of the packet in file. -+ * -+ * Analogous to cur_frame_pts/dts -+ */ -+ int64_t cur_frame_pos[AV_PARSER_PTS_NB]; -+ -+ /** -+ * Byte position of currently parsed frame in stream. -+ */ -+ int64_t pos; -+ -+ /** -+ * Previous frame byte position. -+ */ -+ int64_t last_pos; -+ -+ /** -+ * Duration of the current frame. -+ * For audio, this is in units of 1 / AVCodecContext.sample_rate. -+ * For all other types, this is in units of AVCodecContext.time_base. -+ */ -+ int duration; -+ -+ enum AVFieldOrder field_order; -+ -+ /** -+ * Indicate whether a picture is coded as a frame, top field or bottom field. -+ * -+ * For example, H.264 field_pic_flag equal to 0 corresponds to -+ * AV_PICTURE_STRUCTURE_FRAME. An H.264 picture with field_pic_flag -+ * equal to 1 and bottom_field_flag equal to 0 corresponds to -+ * AV_PICTURE_STRUCTURE_TOP_FIELD. -+ */ -+ enum AVPictureStructure picture_structure; -+ -+ /** -+ * Picture number incremented in presentation or output order. -+ * This field may be reinitialized at the first picture of a new sequence. -+ * -+ * For example, this corresponds to H.264 PicOrderCnt. -+ */ -+ int output_picture_number; -+ -+ /** -+ * Dimensions of the decoded video intended for presentation. -+ */ -+ int width; -+ int height; -+ -+ /** -+ * Dimensions of the coded video. -+ */ -+ int coded_width; -+ int coded_height; -+ -+ /** -+ * The format of the coded data, corresponds to enum AVPixelFormat for video -+ * and for enum AVSampleFormat for audio. -+ * -+ * Note that a decoder can have considerable freedom in how exactly it -+ * decodes the data, so the format reported here might be different from the -+ * one returned by a decoder. -+ */ -+ int format; -+} AVCodecParserContext; -+ -+typedef struct AVCodecParser { -+ int codec_ids[5]; /* several codec IDs are permitted */ -+ int priv_data_size; -+ int (*parser_init)(AVCodecParserContext *s); -+ /* This callback never returns an error, a negative value means that -+ * the frame start was in a previous packet. */ -+ int (*parser_parse)(AVCodecParserContext *s, -+ AVCodecContext *avctx, -+ const uint8_t **poutbuf, int *poutbuf_size, -+ const uint8_t *buf, int buf_size); -+ void (*parser_close)(AVCodecParserContext *s); -+ int (*split)(AVCodecContext *avctx, const uint8_t *buf, int buf_size); -+ struct AVCodecParser *next; -+} AVCodecParser; -+ -+/** -+ * Iterate over all registered codec parsers. -+ * -+ * @param opaque a pointer where libavcodec will store the iteration state. Must -+ * point to NULL to start the iteration. -+ * -+ * @return the next registered codec parser or NULL when the iteration is -+ * finished -+ */ -+const AVCodecParser *av_parser_iterate(void **opaque); -+ -+attribute_deprecated -+AVCodecParser *av_parser_next(const AVCodecParser *c); -+ -+attribute_deprecated -+void av_register_codec_parser(AVCodecParser *parser); -+AVCodecParserContext *av_parser_init(int codec_id); -+ -+/** -+ * Parse a packet. -+ * -+ * @param s parser context. -+ * @param avctx codec context. -+ * @param poutbuf set to pointer to parsed buffer or NULL if not yet finished. -+ * @param poutbuf_size set to size of parsed buffer or zero if not yet finished. -+ * @param buf input buffer. -+ * @param buf_size buffer size in bytes without the padding. I.e. the full buffer -+ size is assumed to be buf_size + AV_INPUT_BUFFER_PADDING_SIZE. -+ To signal EOF, this should be 0 (so that the last frame -+ can be output). -+ * @param pts input presentation timestamp. -+ * @param dts input decoding timestamp. -+ * @param pos input byte position in stream. -+ * @return the number of bytes of the input bitstream used. -+ * -+ * Example: -+ * @code -+ * while(in_len){ -+ * len = av_parser_parse2(myparser, AVCodecContext, &data, &size, -+ * in_data, in_len, -+ * pts, dts, pos); -+ * in_data += len; -+ * in_len -= len; -+ * -+ * if(size) -+ * decode_frame(data, size); -+ * } -+ * @endcode -+ */ -+int av_parser_parse2(AVCodecParserContext *s, -+ AVCodecContext *avctx, -+ uint8_t **poutbuf, int *poutbuf_size, -+ const uint8_t *buf, int buf_size, -+ int64_t pts, int64_t dts, -+ int64_t pos); -+ -+/** -+ * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed -+ * @deprecated use AVBitStreamFilter -+ */ -+int av_parser_change(AVCodecParserContext *s, -+ AVCodecContext *avctx, -+ uint8_t **poutbuf, int *poutbuf_size, -+ const uint8_t *buf, int buf_size, int keyframe); -+void av_parser_close(AVCodecParserContext *s); -+ -+/** -+ * @} -+ * @} -+ */ -+ -+/** -+ * @addtogroup lavc_encoding -+ * @{ -+ */ -+ -+/** -+ * Find a registered encoder with a matching codec ID. -+ * -+ * @param id AVCodecID of the requested encoder -+ * @return An encoder if one was found, NULL otherwise. -+ */ -+AVCodec *avcodec_find_encoder(enum AVCodecID id); -+ -+/** -+ * Find a registered encoder with the specified name. -+ * -+ * @param name name of the requested encoder -+ * @return An encoder if one was found, NULL otherwise. -+ */ -+AVCodec *avcodec_find_encoder_by_name(const char *name); -+ -+/** -+ * Encode a frame of audio. -+ * -+ * Takes input samples from frame and writes the next output packet, if -+ * available, to avpkt. The output packet does not necessarily contain data for -+ * the most recent frame, as encoders can delay, split, and combine input frames -+ * internally as needed. -+ * -+ * @param avctx codec context -+ * @param avpkt output AVPacket. -+ * The user can supply an output buffer by setting -+ * avpkt->data and avpkt->size prior to calling the -+ * function, but if the size of the user-provided data is not -+ * large enough, encoding will fail. If avpkt->data and -+ * avpkt->size are set, avpkt->destruct must also be set. All -+ * other AVPacket fields will be reset by the encoder using -+ * av_init_packet(). If avpkt->data is NULL, the encoder will -+ * allocate it. The encoder will set avpkt->size to the size -+ * of the output packet. -+ * -+ * If this function fails or produces no output, avpkt will be -+ * freed using av_packet_unref(). -+ * @param[in] frame AVFrame containing the raw audio data to be encoded. -+ * May be NULL when flushing an encoder that has the -+ * AV_CODEC_CAP_DELAY capability set. -+ * If AV_CODEC_CAP_VARIABLE_FRAME_SIZE is set, then each frame -+ * can have any number of samples. -+ * If it is not set, frame->nb_samples must be equal to -+ * avctx->frame_size for all frames except the last. -+ * The final frame may be smaller than avctx->frame_size. -+ * @param[out] got_packet_ptr This field is set to 1 by libavcodec if the -+ * output packet is non-empty, and to 0 if it is -+ * empty. If the function returns an error, the -+ * packet can be assumed to be invalid, and the -+ * value of got_packet_ptr is undefined and should -+ * not be used. -+ * @return 0 on success, negative error code on failure -+ * -+ * @deprecated use avcodec_send_frame()/avcodec_receive_packet() instead -+ */ -+attribute_deprecated -+int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, -+ const AVFrame *frame, int *got_packet_ptr); -+ -+/** -+ * Encode a frame of video. -+ * -+ * Takes input raw video data from frame and writes the next output packet, if -+ * available, to avpkt. The output packet does not necessarily contain data for -+ * the most recent frame, as encoders can delay and reorder input frames -+ * internally as needed. -+ * -+ * @param avctx codec context -+ * @param avpkt output AVPacket. -+ * The user can supply an output buffer by setting -+ * avpkt->data and avpkt->size prior to calling the -+ * function, but if the size of the user-provided data is not -+ * large enough, encoding will fail. All other AVPacket fields -+ * will be reset by the encoder using av_init_packet(). If -+ * avpkt->data is NULL, the encoder will allocate it. -+ * The encoder will set avpkt->size to the size of the -+ * output packet. The returned data (if any) belongs to the -+ * caller, he is responsible for freeing it. -+ * -+ * If this function fails or produces no output, avpkt will be -+ * freed using av_packet_unref(). -+ * @param[in] frame AVFrame containing the raw video data to be encoded. -+ * May be NULL when flushing an encoder that has the -+ * AV_CODEC_CAP_DELAY capability set. -+ * @param[out] got_packet_ptr This field is set to 1 by libavcodec if the -+ * output packet is non-empty, and to 0 if it is -+ * empty. If the function returns an error, the -+ * packet can be assumed to be invalid, and the -+ * value of got_packet_ptr is undefined and should -+ * not be used. -+ * @return 0 on success, negative error code on failure -+ * -+ * @deprecated use avcodec_send_frame()/avcodec_receive_packet() instead -+ */ -+attribute_deprecated -+int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, -+ const AVFrame *frame, int *got_packet_ptr); -+ -+int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, -+ const AVSubtitle *sub); -+ -+ -+/** -+ * @} -+ */ -+ -+#if FF_API_AVPICTURE -+/** -+ * @addtogroup lavc_picture -+ * @{ -+ */ -+ -+/** -+ * @deprecated unused -+ */ -+attribute_deprecated -+int avpicture_alloc(AVPicture *picture, enum AVPixelFormat pix_fmt, int width, int height); -+ -+/** -+ * @deprecated unused -+ */ -+attribute_deprecated -+void avpicture_free(AVPicture *picture); -+ -+/** -+ * @deprecated use av_image_fill_arrays() instead. -+ */ -+attribute_deprecated -+int avpicture_fill(AVPicture *picture, const uint8_t *ptr, -+ enum AVPixelFormat pix_fmt, int width, int height); -+ -+/** -+ * @deprecated use av_image_copy_to_buffer() instead. -+ */ -+attribute_deprecated -+int avpicture_layout(const AVPicture *src, enum AVPixelFormat pix_fmt, -+ int width, int height, -+ unsigned char *dest, int dest_size); -+ -+/** -+ * @deprecated use av_image_get_buffer_size() instead. -+ */ -+attribute_deprecated -+int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height); -+ -+/** -+ * @deprecated av_image_copy() instead. -+ */ -+attribute_deprecated -+void av_picture_copy(AVPicture *dst, const AVPicture *src, -+ enum AVPixelFormat pix_fmt, int width, int height); -+ -+/** -+ * @deprecated unused -+ */ -+attribute_deprecated -+int av_picture_crop(AVPicture *dst, const AVPicture *src, -+ enum AVPixelFormat pix_fmt, int top_band, int left_band); -+ -+/** -+ * @deprecated unused -+ */ -+attribute_deprecated -+int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width, enum AVPixelFormat pix_fmt, -+ int padtop, int padbottom, int padleft, int padright, int *color); -+ -+/** -+ * @} -+ */ -+#endif -+ -+/** -+ * @defgroup lavc_misc Utility functions -+ * @ingroup libavc -+ * -+ * Miscellaneous utility functions related to both encoding and decoding -+ * (or neither). -+ * @{ -+ */ -+ -+/** -+ * @defgroup lavc_misc_pixfmt Pixel formats -+ * -+ * Functions for working with pixel formats. -+ * @{ -+ */ -+ -+#if FF_API_GETCHROMA -+/** -+ * @deprecated Use av_pix_fmt_get_chroma_sub_sample -+ */ -+ -+attribute_deprecated -+void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift); -+#endif -+ -+/** -+ * Return a value representing the fourCC code associated to the -+ * pixel format pix_fmt, or 0 if no associated fourCC code can be -+ * found. -+ */ -+unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt); -+ -+/** -+ * @deprecated see av_get_pix_fmt_loss() -+ */ -+int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt, -+ int has_alpha); -+ -+/** -+ * Find the best pixel format to convert to given a certain source pixel -+ * format. When converting from one pixel format to another, information loss -+ * may occur. For example, when converting from RGB24 to GRAY, the color -+ * information will be lost. Similarly, other losses occur when converting from -+ * some formats to other formats. avcodec_find_best_pix_fmt_of_2() searches which of -+ * the given pixel formats should be used to suffer the least amount of loss. -+ * The pixel formats from which it chooses one, are determined by the -+ * pix_fmt_list parameter. -+ * -+ * -+ * @param[in] pix_fmt_list AV_PIX_FMT_NONE terminated array of pixel formats to choose from -+ * @param[in] src_pix_fmt source pixel format -+ * @param[in] has_alpha Whether the source pixel format alpha channel is used. -+ * @param[out] loss_ptr Combination of flags informing you what kind of losses will occur. -+ * @return The best pixel format to convert to or -1 if none was found. -+ */ -+enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list, -+ enum AVPixelFormat src_pix_fmt, -+ int has_alpha, int *loss_ptr); -+ -+/** -+ * @deprecated see av_find_best_pix_fmt_of_2() -+ */ -+enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, -+ enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr); -+ -+attribute_deprecated -+enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, -+ enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr); -+ -+enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat * fmt); -+ -+/** -+ * @} -+ */ -+ -+#if FF_API_TAG_STRING -+/** -+ * Put a string representing the codec tag codec_tag in buf. -+ * -+ * @param buf buffer to place codec tag in -+ * @param buf_size size in bytes of buf -+ * @param codec_tag codec tag to assign -+ * @return the length of the string that would have been generated if -+ * enough space had been available, excluding the trailing null -+ * -+ * @deprecated see av_fourcc_make_string() and av_fourcc2str(). -+ */ -+attribute_deprecated -+size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag); -+#endif -+ -+void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode); -+ -+/** -+ * Return a name for the specified profile, if available. -+ * -+ * @param codec the codec that is searched for the given profile -+ * @param profile the profile value for which a name is requested -+ * @return A name for the profile if found, NULL otherwise. -+ */ -+const char *av_get_profile_name(const AVCodec *codec, int profile); -+ -+/** -+ * Return a name for the specified profile, if available. -+ * -+ * @param codec_id the ID of the codec to which the requested profile belongs -+ * @param profile the profile value for which a name is requested -+ * @return A name for the profile if found, NULL otherwise. -+ * -+ * @note unlike av_get_profile_name(), which searches a list of profiles -+ * supported by a specific decoder or encoder implementation, this -+ * function searches the list of profiles from the AVCodecDescriptor -+ */ -+const char *avcodec_profile_name(enum AVCodecID codec_id, int profile); -+ -+int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size); -+int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count); -+//FIXME func typedef -+ -+/** -+ * Fill AVFrame audio data and linesize pointers. -+ * -+ * The buffer buf must be a preallocated buffer with a size big enough -+ * to contain the specified samples amount. The filled AVFrame data -+ * pointers will point to this buffer. -+ * -+ * AVFrame extended_data channel pointers are allocated if necessary for -+ * planar audio. -+ * -+ * @param frame the AVFrame -+ * frame->nb_samples must be set prior to calling the -+ * function. This function fills in frame->data, -+ * frame->extended_data, frame->linesize[0]. -+ * @param nb_channels channel count -+ * @param sample_fmt sample format -+ * @param buf buffer to use for frame data -+ * @param buf_size size of buffer -+ * @param align plane size sample alignment (0 = default) -+ * @return >=0 on success, negative error code on failure -+ * @todo return the size in bytes required to store the samples in -+ * case of success, at the next libavutil bump -+ */ -+int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, -+ enum AVSampleFormat sample_fmt, const uint8_t *buf, -+ int buf_size, int align); -+ -+/** -+ * Reset the internal decoder state / flush internal buffers. Should be called -+ * e.g. when seeking or when switching to a different stream. -+ * -+ * @note when refcounted frames are not used (i.e. avctx->refcounted_frames is 0), -+ * this invalidates the frames previously returned from the decoder. When -+ * refcounted frames are used, the decoder just releases any references it might -+ * keep internally, but the caller's reference remains valid. -+ */ -+void avcodec_flush_buffers(AVCodecContext *avctx); -+ -+/** -+ * Return codec bits per sample. -+ * -+ * @param[in] codec_id the codec -+ * @return Number of bits per sample or zero if unknown for the given codec. -+ */ -+int av_get_bits_per_sample(enum AVCodecID codec_id); -+ -+/** -+ * Return the PCM codec associated with a sample format. -+ * @param be endianness, 0 for little, 1 for big, -+ * -1 (or anything else) for native -+ * @return AV_CODEC_ID_PCM_* or AV_CODEC_ID_NONE -+ */ -+enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be); -+ -+/** -+ * Return codec bits per sample. -+ * Only return non-zero if the bits per sample is exactly correct, not an -+ * approximation. -+ * -+ * @param[in] codec_id the codec -+ * @return Number of bits per sample or zero if unknown for the given codec. -+ */ -+int av_get_exact_bits_per_sample(enum AVCodecID codec_id); -+ -+/** -+ * Return audio frame duration. -+ * -+ * @param avctx codec context -+ * @param frame_bytes size of the frame, or 0 if unknown -+ * @return frame duration, in samples, if known. 0 if not able to -+ * determine. -+ */ -+int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes); -+ -+/** -+ * This function is the same as av_get_audio_frame_duration(), except it works -+ * with AVCodecParameters instead of an AVCodecContext. -+ */ -+int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes); -+ -+#if FF_API_OLD_BSF -+typedef struct AVBitStreamFilterContext { -+ void *priv_data; -+ const struct AVBitStreamFilter *filter; -+ AVCodecParserContext *parser; -+ struct AVBitStreamFilterContext *next; -+ /** -+ * Internal default arguments, used if NULL is passed to av_bitstream_filter_filter(). -+ * Not for access by library users. -+ */ -+ char *args; -+} AVBitStreamFilterContext; -+#endif -+ -+typedef struct AVBSFInternal AVBSFInternal; -+ -+/** -+ * The bitstream filter state. -+ * -+ * This struct must be allocated with av_bsf_alloc() and freed with -+ * av_bsf_free(). -+ * -+ * The fields in the struct will only be changed (by the caller or by the -+ * filter) as described in their documentation, and are to be considered -+ * immutable otherwise. -+ */ -+typedef struct AVBSFContext { -+ /** -+ * A class for logging and AVOptions -+ */ -+ const AVClass *av_class; -+ -+ /** -+ * The bitstream filter this context is an instance of. -+ */ -+ const struct AVBitStreamFilter *filter; -+ -+ /** -+ * Opaque libavcodec internal data. Must not be touched by the caller in any -+ * way. -+ */ -+ AVBSFInternal *internal; -+ -+ /** -+ * Opaque filter-specific private data. If filter->priv_class is non-NULL, -+ * this is an AVOptions-enabled struct. -+ */ -+ void *priv_data; -+ -+ /** -+ * Parameters of the input stream. This field is allocated in -+ * av_bsf_alloc(), it needs to be filled by the caller before -+ * av_bsf_init(). -+ */ -+ AVCodecParameters *par_in; -+ -+ /** -+ * Parameters of the output stream. This field is allocated in -+ * av_bsf_alloc(), it is set by the filter in av_bsf_init(). -+ */ -+ AVCodecParameters *par_out; -+ -+ /** -+ * The timebase used for the timestamps of the input packets. Set by the -+ * caller before av_bsf_init(). -+ */ -+ AVRational time_base_in; -+ -+ /** -+ * The timebase used for the timestamps of the output packets. Set by the -+ * filter in av_bsf_init(). -+ */ -+ AVRational time_base_out; -+} AVBSFContext; -+ -+typedef struct AVBitStreamFilter { -+ const char *name; -+ -+ /** -+ * A list of codec ids supported by the filter, terminated by -+ * AV_CODEC_ID_NONE. -+ * May be NULL, in that case the bitstream filter works with any codec id. -+ */ -+ const enum AVCodecID *codec_ids; -+ -+ /** -+ * A class for the private data, used to declare bitstream filter private -+ * AVOptions. This field is NULL for bitstream filters that do not declare -+ * any options. -+ * -+ * If this field is non-NULL, the first member of the filter private data -+ * must be a pointer to AVClass, which will be set by libavcodec generic -+ * code to this class. -+ */ -+ const AVClass *priv_class; -+ -+ /***************************************************************** -+ * No fields below this line are part of the public API. They -+ * may not be used outside of libavcodec and can be changed and -+ * removed at will. -+ * New public fields should be added right above. -+ ***************************************************************** -+ */ -+ -+ int priv_data_size; -+ int (*init)(AVBSFContext *ctx); -+ int (*filter)(AVBSFContext *ctx, AVPacket *pkt); -+ void (*close)(AVBSFContext *ctx); -+} AVBitStreamFilter; -+ -+#if FF_API_OLD_BSF -+/** -+ * @deprecated the old bitstream filtering API (using AVBitStreamFilterContext) -+ * is deprecated. Use the new bitstream filtering API (using AVBSFContext). -+ */ -+attribute_deprecated -+void av_register_bitstream_filter(AVBitStreamFilter *bsf); -+/** -+ * @deprecated the old bitstream filtering API (using AVBitStreamFilterContext) -+ * is deprecated. Use av_bsf_get_by_name(), av_bsf_alloc(), and av_bsf_init() -+ * from the new bitstream filtering API (using AVBSFContext). -+ */ -+attribute_deprecated -+AVBitStreamFilterContext *av_bitstream_filter_init(const char *name); -+/** -+ * @deprecated the old bitstream filtering API (using AVBitStreamFilterContext) -+ * is deprecated. Use av_bsf_send_packet() and av_bsf_receive_packet() from the -+ * new bitstream filtering API (using AVBSFContext). -+ */ -+attribute_deprecated -+int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, -+ AVCodecContext *avctx, const char *args, -+ uint8_t **poutbuf, int *poutbuf_size, -+ const uint8_t *buf, int buf_size, int keyframe); -+/** -+ * @deprecated the old bitstream filtering API (using AVBitStreamFilterContext) -+ * is deprecated. Use av_bsf_free() from the new bitstream filtering API (using -+ * AVBSFContext). -+ */ -+attribute_deprecated -+void av_bitstream_filter_close(AVBitStreamFilterContext *bsf); -+/** -+ * @deprecated the old bitstream filtering API (using AVBitStreamFilterContext) -+ * is deprecated. Use av_bsf_iterate() from the new bitstream filtering API (using -+ * AVBSFContext). -+ */ -+attribute_deprecated -+const AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f); -+#endif -+ -+/** -+ * @return a bitstream filter with the specified name or NULL if no such -+ * bitstream filter exists. -+ */ -+const AVBitStreamFilter *av_bsf_get_by_name(const char *name); -+ -+/** -+ * Iterate over all registered bitstream filters. -+ * -+ * @param opaque a pointer where libavcodec will store the iteration state. Must -+ * point to NULL to start the iteration. -+ * -+ * @return the next registered bitstream filter or NULL when the iteration is -+ * finished -+ */ -+const AVBitStreamFilter *av_bsf_iterate(void **opaque); -+#if FF_API_NEXT -+attribute_deprecated -+const AVBitStreamFilter *av_bsf_next(void **opaque); -+#endif -+ -+/** -+ * Allocate a context for a given bitstream filter. The caller must fill in the -+ * context parameters as described in the documentation and then call -+ * av_bsf_init() before sending any data to the filter. -+ * -+ * @param filter the filter for which to allocate an instance. -+ * @param ctx a pointer into which the pointer to the newly-allocated context -+ * will be written. It must be freed with av_bsf_free() after the -+ * filtering is done. -+ * -+ * @return 0 on success, a negative AVERROR code on failure -+ */ -+int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx); -+ -+/** -+ * Prepare the filter for use, after all the parameters and options have been -+ * set. -+ */ -+int av_bsf_init(AVBSFContext *ctx); -+ -+/** -+ * Submit a packet for filtering. -+ * -+ * After sending each packet, the filter must be completely drained by calling -+ * av_bsf_receive_packet() repeatedly until it returns AVERROR(EAGAIN) or -+ * AVERROR_EOF. -+ * -+ * @param pkt the packet to filter. The bitstream filter will take ownership of -+ * the packet and reset the contents of pkt. pkt is not touched if an error occurs. -+ * This parameter may be NULL, which signals the end of the stream (i.e. no more -+ * packets will be sent). That will cause the filter to output any packets it -+ * may have buffered internally. -+ * -+ * @return 0 on success, a negative AVERROR on error. -+ */ -+int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt); -+ -+/** -+ * Retrieve a filtered packet. -+ * -+ * @param[out] pkt this struct will be filled with the contents of the filtered -+ * packet. It is owned by the caller and must be freed using -+ * av_packet_unref() when it is no longer needed. -+ * This parameter should be "clean" (i.e. freshly allocated -+ * with av_packet_alloc() or unreffed with av_packet_unref()) -+ * when this function is called. If this function returns -+ * successfully, the contents of pkt will be completely -+ * overwritten by the returned data. On failure, pkt is not -+ * touched. -+ * -+ * @return 0 on success. AVERROR(EAGAIN) if more packets need to be sent to the -+ * filter (using av_bsf_send_packet()) to get more output. AVERROR_EOF if there -+ * will be no further output from the filter. Another negative AVERROR value if -+ * an error occurs. -+ * -+ * @note one input packet may result in several output packets, so after sending -+ * a packet with av_bsf_send_packet(), this function needs to be called -+ * repeatedly until it stops returning 0. It is also possible for a filter to -+ * output fewer packets than were sent to it, so this function may return -+ * AVERROR(EAGAIN) immediately after a successful av_bsf_send_packet() call. -+ */ -+int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt); -+ -+/** -+ * Free a bitstream filter context and everything associated with it; write NULL -+ * into the supplied pointer. -+ */ -+void av_bsf_free(AVBSFContext **ctx); -+ -+/** -+ * Get the AVClass for AVBSFContext. It can be used in combination with -+ * AV_OPT_SEARCH_FAKE_OBJ for examining options. -+ * -+ * @see av_opt_find(). -+ */ -+const AVClass *av_bsf_get_class(void); -+ -+/** -+ * Structure for chain/list of bitstream filters. -+ * Empty list can be allocated by av_bsf_list_alloc(). -+ */ -+typedef struct AVBSFList AVBSFList; -+ -+/** -+ * Allocate empty list of bitstream filters. -+ * The list must be later freed by av_bsf_list_free() -+ * or finalized by av_bsf_list_finalize(). -+ * -+ * @return Pointer to @ref AVBSFList on success, NULL in case of failure -+ */ -+AVBSFList *av_bsf_list_alloc(void); -+ -+/** -+ * Free list of bitstream filters. -+ * -+ * @param lst Pointer to pointer returned by av_bsf_list_alloc() -+ */ -+void av_bsf_list_free(AVBSFList **lst); -+ -+/** -+ * Append bitstream filter to the list of bitstream filters. -+ * -+ * @param lst List to append to -+ * @param bsf Filter context to be appended -+ * -+ * @return >=0 on success, negative AVERROR in case of failure -+ */ -+int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf); -+ -+/** -+ * Construct new bitstream filter context given it's name and options -+ * and append it to the list of bitstream filters. -+ * -+ * @param lst List to append to -+ * @param bsf_name Name of the bitstream filter -+ * @param options Options for the bitstream filter, can be set to NULL -+ * -+ * @return >=0 on success, negative AVERROR in case of failure -+ */ -+int av_bsf_list_append2(AVBSFList *lst, const char * bsf_name, AVDictionary **options); -+/** -+ * Finalize list of bitstream filters. -+ * -+ * This function will transform @ref AVBSFList to single @ref AVBSFContext, -+ * so the whole chain of bitstream filters can be treated as single filter -+ * freshly allocated by av_bsf_alloc(). -+ * If the call is successful, @ref AVBSFList structure is freed and lst -+ * will be set to NULL. In case of failure, caller is responsible for -+ * freeing the structure by av_bsf_list_free() -+ * -+ * @param lst Filter list structure to be transformed -+ * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure -+ * representing the chain of bitstream filters -+ * -+ * @return >=0 on success, negative AVERROR in case of failure -+ */ -+int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf); -+ -+/** -+ * Parse string describing list of bitstream filters and create single -+ * @ref AVBSFContext describing the whole chain of bitstream filters. -+ * Resulting @ref AVBSFContext can be treated as any other @ref AVBSFContext freshly -+ * allocated by av_bsf_alloc(). -+ * -+ * @param str String describing chain of bitstream filters in format -+ * `bsf1[=opt1=val1:opt2=val2][,bsf2]` -+ * @param[out] bsf Pointer to be set to newly created @ref AVBSFContext structure -+ * representing the chain of bitstream filters -+ * -+ * @return >=0 on success, negative AVERROR in case of failure -+ */ -+int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf); -+ -+/** -+ * Get null/pass-through bitstream filter. -+ * -+ * @param[out] bsf Pointer to be set to new instance of pass-through bitstream filter -+ * -+ * @return -+ */ -+int av_bsf_get_null_filter(AVBSFContext **bsf); -+ -+/* memory */ -+ -+/** -+ * Same behaviour av_fast_malloc but the buffer has additional -+ * AV_INPUT_BUFFER_PADDING_SIZE at the end which will always be 0. -+ * -+ * In addition the whole buffer will initially and after resizes -+ * be 0-initialized so that no uninitialized data will ever appear. -+ */ -+void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size); -+ -+/** -+ * Same behaviour av_fast_padded_malloc except that buffer will always -+ * be 0-initialized after call. -+ */ -+void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size); -+ -+/** -+ * Encode extradata length to a buffer. Used by xiph codecs. -+ * -+ * @param s buffer to write to; must be at least (v/255+1) bytes long -+ * @param v size of extradata in bytes -+ * @return number of bytes written to the buffer. -+ */ -+unsigned int av_xiphlacing(unsigned char *s, unsigned int v); -+ -+#if FF_API_USER_VISIBLE_AVHWACCEL -+/** -+ * Register the hardware accelerator hwaccel. -+ * -+ * @deprecated This function doesn't do anything. -+ */ -+attribute_deprecated -+void av_register_hwaccel(AVHWAccel *hwaccel); -+ -+/** -+ * If hwaccel is NULL, returns the first registered hardware accelerator, -+ * if hwaccel is non-NULL, returns the next registered hardware accelerator -+ * after hwaccel, or NULL if hwaccel is the last one. -+ * -+ * @deprecated AVHWaccel structures contain no user-serviceable parts, so -+ * this function should not be used. -+ */ -+attribute_deprecated -+AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel); -+#endif -+ -+#if FF_API_LOCKMGR -+/** -+ * Lock operation used by lockmgr -+ * -+ * @deprecated Deprecated together with av_lockmgr_register(). -+ */ -+enum AVLockOp { -+ AV_LOCK_CREATE, ///< Create a mutex -+ AV_LOCK_OBTAIN, ///< Lock the mutex -+ AV_LOCK_RELEASE, ///< Unlock the mutex -+ AV_LOCK_DESTROY, ///< Free mutex resources -+}; -+ -+/** -+ * Register a user provided lock manager supporting the operations -+ * specified by AVLockOp. The "mutex" argument to the function points -+ * to a (void *) where the lockmgr should store/get a pointer to a user -+ * allocated mutex. It is NULL upon AV_LOCK_CREATE and equal to the -+ * value left by the last call for all other ops. If the lock manager is -+ * unable to perform the op then it should leave the mutex in the same -+ * state as when it was called and return a non-zero value. However, -+ * when called with AV_LOCK_DESTROY the mutex will always be assumed to -+ * have been successfully destroyed. If av_lockmgr_register succeeds -+ * it will return a non-negative value, if it fails it will return a -+ * negative value and destroy all mutex and unregister all callbacks. -+ * av_lockmgr_register is not thread-safe, it must be called from a -+ * single thread before any calls which make use of locking are used. -+ * -+ * @param cb User defined callback. av_lockmgr_register invokes calls -+ * to this callback and the previously registered callback. -+ * The callback will be used to create more than one mutex -+ * each of which must be backed by its own underlying locking -+ * mechanism (i.e. do not use a single static object to -+ * implement your lock manager). If cb is set to NULL the -+ * lockmgr will be unregistered. -+ * -+ * @deprecated This function does nothing, and always returns 0. Be sure to -+ * build with thread support to get basic thread safety. -+ */ -+attribute_deprecated -+int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)); -+#endif -+ -+/** -+ * Get the type of the given codec. -+ */ -+enum AVMediaType avcodec_get_type(enum AVCodecID codec_id); -+ -+/** -+ * Get the name of a codec. -+ * @return a static string identifying the codec; never NULL -+ */ -+const char *avcodec_get_name(enum AVCodecID id); -+ -+/** -+ * @return a positive value if s is open (i.e. avcodec_open2() was called on it -+ * with no corresponding avcodec_close()), 0 otherwise. -+ */ -+int avcodec_is_open(AVCodecContext *s); -+ -+/** -+ * @return a non-zero number if codec is an encoder, zero otherwise -+ */ -+int av_codec_is_encoder(const AVCodec *codec); -+ -+/** -+ * @return a non-zero number if codec is a decoder, zero otherwise -+ */ -+int av_codec_is_decoder(const AVCodec *codec); -+ -+/** -+ * @return descriptor for given codec ID or NULL if no descriptor exists. -+ */ -+const AVCodecDescriptor *avcodec_descriptor_get(enum AVCodecID id); -+ -+/** -+ * Iterate over all codec descriptors known to libavcodec. -+ * -+ * @param prev previous descriptor. NULL to get the first descriptor. -+ * -+ * @return next descriptor or NULL after the last descriptor -+ */ -+const AVCodecDescriptor *avcodec_descriptor_next(const AVCodecDescriptor *prev); -+ -+/** -+ * @return codec descriptor with the given name or NULL if no such descriptor -+ * exists. -+ */ -+const AVCodecDescriptor *avcodec_descriptor_get_by_name(const char *name); -+ -+/** -+ * Allocate a CPB properties structure and initialize its fields to default -+ * values. -+ * -+ * @param size if non-NULL, the size of the allocated struct will be written -+ * here. This is useful for embedding it in side data. -+ * -+ * @return the newly allocated struct or NULL on failure -+ */ -+AVCPBProperties *av_cpb_properties_alloc(size_t *size); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVCODEC_AVCODEC_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/avfft.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/avfft.h -new file mode 100644 -index 000000000000..0c0f9b8d8dae ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/avfft.h -@@ -0,0 +1,118 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVCODEC_AVFFT_H -+#define AVCODEC_AVFFT_H -+ -+/** -+ * @file -+ * @ingroup lavc_fft -+ * FFT functions -+ */ -+ -+/** -+ * @defgroup lavc_fft FFT functions -+ * @ingroup lavc_misc -+ * -+ * @{ -+ */ -+ -+typedef float FFTSample; -+ -+typedef struct FFTComplex { -+ FFTSample re, im; -+} FFTComplex; -+ -+typedef struct FFTContext FFTContext; -+ -+/** -+ * Set up a complex FFT. -+ * @param nbits log2 of the length of the input array -+ * @param inverse if 0 perform the forward transform, if 1 perform the inverse -+ */ -+FFTContext *av_fft_init(int nbits, int inverse); -+ -+/** -+ * Do the permutation needed BEFORE calling ff_fft_calc(). -+ */ -+void av_fft_permute(FFTContext *s, FFTComplex *z); -+ -+/** -+ * Do a complex FFT with the parameters defined in av_fft_init(). The -+ * input data must be permuted before. No 1.0/sqrt(n) normalization is done. -+ */ -+void av_fft_calc(FFTContext *s, FFTComplex *z); -+ -+void av_fft_end(FFTContext *s); -+ -+FFTContext *av_mdct_init(int nbits, int inverse, double scale); -+void av_imdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input); -+void av_imdct_half(FFTContext *s, FFTSample *output, const FFTSample *input); -+void av_mdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input); -+void av_mdct_end(FFTContext *s); -+ -+/* Real Discrete Fourier Transform */ -+ -+enum RDFTransformType { -+ DFT_R2C, -+ IDFT_C2R, -+ IDFT_R2C, -+ DFT_C2R, -+}; -+ -+typedef struct RDFTContext RDFTContext; -+ -+/** -+ * Set up a real FFT. -+ * @param nbits log2 of the length of the input array -+ * @param trans the type of transform -+ */ -+RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans); -+void av_rdft_calc(RDFTContext *s, FFTSample *data); -+void av_rdft_end(RDFTContext *s); -+ -+/* Discrete Cosine Transform */ -+ -+typedef struct DCTContext DCTContext; -+ -+enum DCTTransformType { -+ DCT_II = 0, -+ DCT_III, -+ DCT_I, -+ DST_I, -+}; -+ -+/** -+ * Set up DCT. -+ * -+ * @param nbits size of the input array: -+ * (1 << nbits) for DCT-II, DCT-III and DST-I -+ * (1 << nbits) + 1 for DCT-I -+ * @param type the type of transform -+ * -+ * @note the first element of the input of DST-I is ignored -+ */ -+DCTContext *av_dct_init(int nbits, enum DCTTransformType type); -+void av_dct_calc(DCTContext *s, FFTSample *data); -+void av_dct_end (DCTContext *s); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVCODEC_AVFFT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/vaapi.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/vaapi.h -new file mode 100644 -index 000000000000..2cf7da5889ab ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/vaapi.h -@@ -0,0 +1,86 @@ -+/* -+ * Video Acceleration API (shared data between FFmpeg and the video player) -+ * HW decode acceleration for MPEG-2, MPEG-4, H.264 and VC-1 -+ * -+ * Copyright (C) 2008-2009 Splitted-Desktop Systems -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVCODEC_VAAPI_H -+#define AVCODEC_VAAPI_H -+ -+/** -+ * @file -+ * @ingroup lavc_codec_hwaccel_vaapi -+ * Public libavcodec VA API header. -+ */ -+ -+#include -+#include "libavutil/attributes.h" -+#include "version.h" -+ -+#if FF_API_STRUCT_VAAPI_CONTEXT -+ -+/** -+ * @defgroup lavc_codec_hwaccel_vaapi VA API Decoding -+ * @ingroup lavc_codec_hwaccel -+ * @{ -+ */ -+ -+/** -+ * This structure is used to share data between the FFmpeg library and -+ * the client video application. -+ * This shall be zero-allocated and available as -+ * AVCodecContext.hwaccel_context. All user members can be set once -+ * during initialization or through each AVCodecContext.get_buffer() -+ * function call. In any case, they must be valid prior to calling -+ * decoding functions. -+ * -+ * Deprecated: use AVCodecContext.hw_frames_ctx instead. -+ */ -+struct attribute_deprecated vaapi_context { -+ /** -+ * Window system dependent data -+ * -+ * - encoding: unused -+ * - decoding: Set by user -+ */ -+ void *display; -+ -+ /** -+ * Configuration ID -+ * -+ * - encoding: unused -+ * - decoding: Set by user -+ */ -+ uint32_t config_id; -+ -+ /** -+ * Context ID (video decode pipeline) -+ * -+ * - encoding: unused -+ * - decoding: Set by user -+ */ -+ uint32_t context_id; -+}; -+ -+/* @} */ -+ -+#endif /* FF_API_STRUCT_VAAPI_CONTEXT */ -+ -+#endif /* AVCODEC_VAAPI_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/vdpau.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/vdpau.h -new file mode 100644 -index 000000000000..4d9994336911 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/vdpau.h -@@ -0,0 +1,176 @@ -+/* -+ * The Video Decode and Presentation API for UNIX (VDPAU) is used for -+ * hardware-accelerated decoding of MPEG-1/2, H.264 and VC-1. -+ * -+ * Copyright (C) 2008 NVIDIA -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVCODEC_VDPAU_H -+#define AVCODEC_VDPAU_H -+ -+/** -+ * @file -+ * @ingroup lavc_codec_hwaccel_vdpau -+ * Public libavcodec VDPAU header. -+ */ -+ -+ -+/** -+ * @defgroup lavc_codec_hwaccel_vdpau VDPAU Decoder and Renderer -+ * @ingroup lavc_codec_hwaccel -+ * -+ * VDPAU hardware acceleration has two modules -+ * - VDPAU decoding -+ * - VDPAU presentation -+ * -+ * The VDPAU decoding module parses all headers using FFmpeg -+ * parsing mechanisms and uses VDPAU for the actual decoding. -+ * -+ * As per the current implementation, the actual decoding -+ * and rendering (API calls) are done as part of the VDPAU -+ * presentation (vo_vdpau.c) module. -+ * -+ * @{ -+ */ -+ -+#include -+ -+#include "libavutil/avconfig.h" -+#include "libavutil/attributes.h" -+ -+#include "avcodec.h" -+#include "version.h" -+ -+struct AVCodecContext; -+struct AVFrame; -+ -+typedef int (*AVVDPAU_Render2)(struct AVCodecContext *, struct AVFrame *, -+ const VdpPictureInfo *, uint32_t, -+ const VdpBitstreamBuffer *); -+ -+/** -+ * This structure is used to share data between the libavcodec library and -+ * the client video application. -+ * The user shall allocate the structure via the av_alloc_vdpau_hwaccel -+ * function and make it available as -+ * AVCodecContext.hwaccel_context. Members can be set by the user once -+ * during initialization or through each AVCodecContext.get_buffer() -+ * function call. In any case, they must be valid prior to calling -+ * decoding functions. -+ * -+ * The size of this structure is not a part of the public ABI and must not -+ * be used outside of libavcodec. Use av_vdpau_alloc_context() to allocate an -+ * AVVDPAUContext. -+ */ -+typedef struct AVVDPAUContext { -+ /** -+ * VDPAU decoder handle -+ * -+ * Set by user. -+ */ -+ VdpDecoder decoder; -+ -+ /** -+ * VDPAU decoder render callback -+ * -+ * Set by the user. -+ */ -+ VdpDecoderRender *render; -+ -+ AVVDPAU_Render2 render2; -+} AVVDPAUContext; -+ -+/** -+ * @brief allocation function for AVVDPAUContext -+ * -+ * Allows extending the struct without breaking API/ABI -+ */ -+AVVDPAUContext *av_alloc_vdpaucontext(void); -+ -+AVVDPAU_Render2 av_vdpau_hwaccel_get_render2(const AVVDPAUContext *); -+void av_vdpau_hwaccel_set_render2(AVVDPAUContext *, AVVDPAU_Render2); -+ -+/** -+ * Associate a VDPAU device with a codec context for hardware acceleration. -+ * This function is meant to be called from the get_format() codec callback, -+ * or earlier. It can also be called after avcodec_flush_buffers() to change -+ * the underlying VDPAU device mid-stream (e.g. to recover from non-transparent -+ * display preemption). -+ * -+ * @note get_format() must return AV_PIX_FMT_VDPAU if this function completes -+ * successfully. -+ * -+ * @param avctx decoding context whose get_format() callback is invoked -+ * @param device VDPAU device handle to use for hardware acceleration -+ * @param get_proc_address VDPAU device driver -+ * @param flags zero of more OR'd AV_HWACCEL_FLAG_* flags -+ * -+ * @return 0 on success, an AVERROR code on failure. -+ */ -+int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device, -+ VdpGetProcAddress *get_proc_address, unsigned flags); -+ -+/** -+ * Gets the parameters to create an adequate VDPAU video surface for the codec -+ * context using VDPAU hardware decoding acceleration. -+ * -+ * @note Behavior is undefined if the context was not successfully bound to a -+ * VDPAU device using av_vdpau_bind_context(). -+ * -+ * @param avctx the codec context being used for decoding the stream -+ * @param type storage space for the VDPAU video surface chroma type -+ * (or NULL to ignore) -+ * @param width storage space for the VDPAU video surface pixel width -+ * (or NULL to ignore) -+ * @param height storage space for the VDPAU video surface pixel height -+ * (or NULL to ignore) -+ * -+ * @return 0 on success, a negative AVERROR code on failure. -+ */ -+int av_vdpau_get_surface_parameters(AVCodecContext *avctx, VdpChromaType *type, -+ uint32_t *width, uint32_t *height); -+ -+/** -+ * Allocate an AVVDPAUContext. -+ * -+ * @return Newly-allocated AVVDPAUContext or NULL on failure. -+ */ -+AVVDPAUContext *av_vdpau_alloc_context(void); -+ -+#if FF_API_VDPAU_PROFILE -+/** -+ * Get a decoder profile that should be used for initializing a VDPAU decoder. -+ * Should be called from the AVCodecContext.get_format() callback. -+ * -+ * @deprecated Use av_vdpau_bind_context() instead. -+ * -+ * @param avctx the codec context being used for decoding the stream -+ * @param profile a pointer into which the result will be written on success. -+ * The contents of profile are undefined if this function returns -+ * an error. -+ * -+ * @return 0 on success (non-negative), a negative AVERROR on failure. -+ */ -+attribute_deprecated -+int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile); -+#endif -+ -+/* @}*/ -+ -+#endif /* AVCODEC_VDPAU_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/version.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/version.h -new file mode 100644 -index 000000000000..6895f1a461e3 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavcodec/version.h -@@ -0,0 +1,137 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVCODEC_VERSION_H -+#define AVCODEC_VERSION_H -+ -+/** -+ * @file -+ * @ingroup libavc -+ * Libavcodec version macros. -+ */ -+ -+#include "libavutil/version.h" -+ -+#define LIBAVCODEC_VERSION_MAJOR 58 -+#define LIBAVCODEC_VERSION_MINOR 18 -+#define LIBAVCODEC_VERSION_MICRO 100 -+ -+#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ -+ LIBAVCODEC_VERSION_MINOR, \ -+ LIBAVCODEC_VERSION_MICRO) -+#define LIBAVCODEC_VERSION AV_VERSION(LIBAVCODEC_VERSION_MAJOR, \ -+ LIBAVCODEC_VERSION_MINOR, \ -+ LIBAVCODEC_VERSION_MICRO) -+#define LIBAVCODEC_BUILD LIBAVCODEC_VERSION_INT -+ -+#define LIBAVCODEC_IDENT "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION) -+ -+/** -+ * FF_API_* defines may be placed below to indicate public API that will be -+ * dropped at a future version bump. The defines themselves are not part of -+ * the public API and may change, break or disappear at any time. -+ * -+ * @note, when bumping the major version it is recommended to manually -+ * disable each FF_API_* in its own commit instead of disabling them all -+ * at once through the bump. This improves the git bisect-ability of the change. -+ */ -+ -+#ifndef FF_API_LOWRES -+#define FF_API_LOWRES (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_DEBUG_MV -+#define FF_API_DEBUG_MV (LIBAVCODEC_VERSION_MAJOR < 58) -+#endif -+#ifndef FF_API_AVCTX_TIMEBASE -+#define FF_API_AVCTX_TIMEBASE (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_CODED_FRAME -+#define FF_API_CODED_FRAME (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_SIDEDATA_ONLY_PKT -+#define FF_API_SIDEDATA_ONLY_PKT (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_VDPAU_PROFILE -+#define FF_API_VDPAU_PROFILE (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_CONVERGENCE_DURATION -+#define FF_API_CONVERGENCE_DURATION (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_AVPICTURE -+#define FF_API_AVPICTURE (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_AVPACKET_OLD_API -+#define FF_API_AVPACKET_OLD_API (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_RTP_CALLBACK -+#define FF_API_RTP_CALLBACK (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_VBV_DELAY -+#define FF_API_VBV_DELAY (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_CODER_TYPE -+#define FF_API_CODER_TYPE (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_STAT_BITS -+#define FF_API_STAT_BITS (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_PRIVATE_OPT -+#define FF_API_PRIVATE_OPT (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_ASS_TIMING -+#define FF_API_ASS_TIMING (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_OLD_BSF -+#define FF_API_OLD_BSF (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_COPY_CONTEXT -+#define FF_API_COPY_CONTEXT (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_GET_CONTEXT_DEFAULTS -+#define FF_API_GET_CONTEXT_DEFAULTS (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_NVENC_OLD_NAME -+#define FF_API_NVENC_OLD_NAME (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_STRUCT_VAAPI_CONTEXT -+#define FF_API_STRUCT_VAAPI_CONTEXT (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_MERGE_SD_API -+#define FF_API_MERGE_SD_API (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_TAG_STRING -+#define FF_API_TAG_STRING (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_GETCHROMA -+#define FF_API_GETCHROMA (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_CODEC_GET_SET -+#define FF_API_CODEC_GET_SET (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_USER_VISIBLE_AVHWACCEL -+#define FF_API_USER_VISIBLE_AVHWACCEL (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_LOCKMGR -+#define FF_API_LOCKMGR (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+#ifndef FF_API_NEXT -+#define FF_API_NEXT (LIBAVCODEC_VERSION_MAJOR < 59) -+#endif -+ -+ -+#endif /* AVCODEC_VERSION_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/attributes.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/attributes.h -new file mode 100644 -index 000000000000..ced108aa2c75 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/attributes.h -@@ -0,0 +1,167 @@ -+/* -+ * copyright (c) 2006 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * Macro definitions for various function/variable attributes -+ */ -+ -+#ifndef AVUTIL_ATTRIBUTES_H -+#define AVUTIL_ATTRIBUTES_H -+ -+#ifdef __GNUC__ -+# define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y)) -+# define AV_GCC_VERSION_AT_MOST(x,y) (__GNUC__ < (x) || __GNUC__ == (x) && __GNUC_MINOR__ <= (y)) -+#else -+# define AV_GCC_VERSION_AT_LEAST(x,y) 0 -+# define AV_GCC_VERSION_AT_MOST(x,y) 0 -+#endif -+ -+#ifndef av_always_inline -+#if AV_GCC_VERSION_AT_LEAST(3,1) -+# define av_always_inline __attribute__((always_inline)) inline -+#elif defined(_MSC_VER) -+# define av_always_inline __forceinline -+#else -+# define av_always_inline inline -+#endif -+#endif -+ -+#ifndef av_extern_inline -+#if defined(__ICL) && __ICL >= 1210 || defined(__GNUC_STDC_INLINE__) -+# define av_extern_inline extern inline -+#else -+# define av_extern_inline inline -+#endif -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(3,4) -+# define av_warn_unused_result __attribute__((warn_unused_result)) -+#else -+# define av_warn_unused_result -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(3,1) -+# define av_noinline __attribute__((noinline)) -+#elif defined(_MSC_VER) -+# define av_noinline __declspec(noinline) -+#else -+# define av_noinline -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__) -+# define av_pure __attribute__((pure)) -+#else -+# define av_pure -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(2,6) || defined(__clang__) -+# define av_const __attribute__((const)) -+#else -+# define av_const -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(4,3) || defined(__clang__) -+# define av_cold __attribute__((cold)) -+#else -+# define av_cold -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(4,1) && !defined(__llvm__) -+# define av_flatten __attribute__((flatten)) -+#else -+# define av_flatten -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(3,1) -+# define attribute_deprecated __attribute__((deprecated)) -+#elif defined(_MSC_VER) -+# define attribute_deprecated __declspec(deprecated) -+#else -+# define attribute_deprecated -+#endif -+ -+/** -+ * Disable warnings about deprecated features -+ * This is useful for sections of code kept for backward compatibility and -+ * scheduled for removal. -+ */ -+#ifndef AV_NOWARN_DEPRECATED -+#if AV_GCC_VERSION_AT_LEAST(4,6) -+# define AV_NOWARN_DEPRECATED(code) \ -+ _Pragma("GCC diagnostic push") \ -+ _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") \ -+ code \ -+ _Pragma("GCC diagnostic pop") -+#elif defined(_MSC_VER) -+# define AV_NOWARN_DEPRECATED(code) \ -+ __pragma(warning(push)) \ -+ __pragma(warning(disable : 4996)) \ -+ code; \ -+ __pragma(warning(pop)) -+#else -+# define AV_NOWARN_DEPRECATED(code) code -+#endif -+#endif -+ -+#if defined(__GNUC__) || defined(__clang__) -+# define av_unused __attribute__((unused)) -+#else -+# define av_unused -+#endif -+ -+/** -+ * Mark a variable as used and prevent the compiler from optimizing it -+ * away. This is useful for variables accessed only from inline -+ * assembler without the compiler being aware. -+ */ -+#if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__) -+# define av_used __attribute__((used)) -+#else -+# define av_used -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(3,3) || defined(__clang__) -+# define av_alias __attribute__((may_alias)) -+#else -+# define av_alias -+#endif -+ -+#if (defined(__GNUC__) || defined(__clang__)) && !defined(__INTEL_COMPILER) -+# define av_uninit(x) x=x -+#else -+# define av_uninit(x) x -+#endif -+ -+#if defined(__GNUC__) || defined(__clang__) -+# define av_builtin_constant_p __builtin_constant_p -+# define av_printf_format(fmtpos, attrpos) __attribute__((__format__(__printf__, fmtpos, attrpos))) -+#else -+# define av_builtin_constant_p(x) 0 -+# define av_printf_format(fmtpos, attrpos) -+#endif -+ -+#if AV_GCC_VERSION_AT_LEAST(2,5) || defined(__clang__) -+# define av_noreturn __attribute__((noreturn)) -+#else -+# define av_noreturn -+#endif -+ -+#endif /* AVUTIL_ATTRIBUTES_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/avconfig.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/avconfig.h -new file mode 100644 -index 000000000000..c289fbb551c1 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/avconfig.h -@@ -0,0 +1,6 @@ -+/* Generated by ffmpeg configure */ -+#ifndef AVUTIL_AVCONFIG_H -+#define AVUTIL_AVCONFIG_H -+#define AV_HAVE_BIGENDIAN 0 -+#define AV_HAVE_FAST_UNALIGNED 1 -+#endif /* AVUTIL_AVCONFIG_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/avutil.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/avutil.h -new file mode 100644 -index 000000000000..4d633156d14d ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/avutil.h -@@ -0,0 +1,365 @@ -+/* -+ * copyright (c) 2006 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_AVUTIL_H -+#define AVUTIL_AVUTIL_H -+ -+/** -+ * @file -+ * @ingroup lavu -+ * Convenience header that includes @ref lavu "libavutil"'s core. -+ */ -+ -+/** -+ * @mainpage -+ * -+ * @section ffmpeg_intro Introduction -+ * -+ * This document describes the usage of the different libraries -+ * provided by FFmpeg. -+ * -+ * @li @ref libavc "libavcodec" encoding/decoding library -+ * @li @ref lavfi "libavfilter" graph-based frame editing library -+ * @li @ref libavf "libavformat" I/O and muxing/demuxing library -+ * @li @ref lavd "libavdevice" special devices muxing/demuxing library -+ * @li @ref lavu "libavutil" common utility library -+ * @li @ref lswr "libswresample" audio resampling, format conversion and mixing -+ * @li @ref lpp "libpostproc" post processing library -+ * @li @ref libsws "libswscale" color conversion and scaling library -+ * -+ * @section ffmpeg_versioning Versioning and compatibility -+ * -+ * Each of the FFmpeg libraries contains a version.h header, which defines a -+ * major, minor and micro version number with the -+ * LIBRARYNAME_VERSION_{MAJOR,MINOR,MICRO} macros. The major version -+ * number is incremented with backward incompatible changes - e.g. removing -+ * parts of the public API, reordering public struct members, etc. The minor -+ * version number is incremented for backward compatible API changes or major -+ * new features - e.g. adding a new public function or a new decoder. The micro -+ * version number is incremented for smaller changes that a calling program -+ * might still want to check for - e.g. changing behavior in a previously -+ * unspecified situation. -+ * -+ * FFmpeg guarantees backward API and ABI compatibility for each library as long -+ * as its major version number is unchanged. This means that no public symbols -+ * will be removed or renamed. Types and names of the public struct members and -+ * values of public macros and enums will remain the same (unless they were -+ * explicitly declared as not part of the public API). Documented behavior will -+ * not change. -+ * -+ * In other words, any correct program that works with a given FFmpeg snapshot -+ * should work just as well without any changes with any later snapshot with the -+ * same major versions. This applies to both rebuilding the program against new -+ * FFmpeg versions or to replacing the dynamic FFmpeg libraries that a program -+ * links against. -+ * -+ * However, new public symbols may be added and new members may be appended to -+ * public structs whose size is not part of public ABI (most public structs in -+ * FFmpeg). New macros and enum values may be added. Behavior in undocumented -+ * situations may change slightly (and be documented). All those are accompanied -+ * by an entry in doc/APIchanges and incrementing either the minor or micro -+ * version number. -+ */ -+ -+/** -+ * @defgroup lavu libavutil -+ * Common code shared across all FFmpeg libraries. -+ * -+ * @note -+ * libavutil is designed to be modular. In most cases, in order to use the -+ * functions provided by one component of libavutil you must explicitly include -+ * the specific header containing that feature. If you are only using -+ * media-related components, you could simply include libavutil/avutil.h, which -+ * brings in most of the "core" components. -+ * -+ * @{ -+ * -+ * @defgroup lavu_crypto Crypto and Hashing -+ * -+ * @{ -+ * @} -+ * -+ * @defgroup lavu_math Mathematics -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_string String Manipulation -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_mem Memory Management -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_data Data Structures -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_video Video related -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_audio Audio related -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_error Error Codes -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_log Logging Facility -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup lavu_misc Other -+ * -+ * @{ -+ * -+ * @defgroup preproc_misc Preprocessor String Macros -+ * -+ * @{ -+ * -+ * @} -+ * -+ * @defgroup version_utils Library Version Macros -+ * -+ * @{ -+ * -+ * @} -+ */ -+ -+ -+/** -+ * @addtogroup lavu_ver -+ * @{ -+ */ -+ -+/** -+ * Return the LIBAVUTIL_VERSION_INT constant. -+ */ -+unsigned avutil_version(void); -+ -+/** -+ * Return an informative version string. This usually is the actual release -+ * version number or a git commit description. This string has no fixed format -+ * and can change any time. It should never be parsed by code. -+ */ -+const char *av_version_info(void); -+ -+/** -+ * Return the libavutil build-time configuration. -+ */ -+const char *avutil_configuration(void); -+ -+/** -+ * Return the libavutil license. -+ */ -+const char *avutil_license(void); -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @addtogroup lavu_media Media Type -+ * @brief Media Type -+ */ -+ -+enum AVMediaType { -+ AVMEDIA_TYPE_UNKNOWN = -1, ///< Usually treated as AVMEDIA_TYPE_DATA -+ AVMEDIA_TYPE_VIDEO, -+ AVMEDIA_TYPE_AUDIO, -+ AVMEDIA_TYPE_DATA, ///< Opaque data information usually continuous -+ AVMEDIA_TYPE_SUBTITLE, -+ AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse -+ AVMEDIA_TYPE_NB -+}; -+ -+/** -+ * Return a string describing the media_type enum, NULL if media_type -+ * is unknown. -+ */ -+const char *av_get_media_type_string(enum AVMediaType media_type); -+ -+/** -+ * @defgroup lavu_const Constants -+ * @{ -+ * -+ * @defgroup lavu_enc Encoding specific -+ * -+ * @note those definition should move to avcodec -+ * @{ -+ */ -+ -+#define FF_LAMBDA_SHIFT 7 -+#define FF_LAMBDA_SCALE (1< -+ -+/** -+ * @defgroup lavu_buffer AVBuffer -+ * @ingroup lavu_data -+ * -+ * @{ -+ * AVBuffer is an API for reference-counted data buffers. -+ * -+ * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer -+ * represents the data buffer itself; it is opaque and not meant to be accessed -+ * by the caller directly, but only through AVBufferRef. However, the caller may -+ * e.g. compare two AVBuffer pointers to check whether two different references -+ * are describing the same data buffer. AVBufferRef represents a single -+ * reference to an AVBuffer and it is the object that may be manipulated by the -+ * caller directly. -+ * -+ * There are two functions provided for creating a new AVBuffer with a single -+ * reference -- av_buffer_alloc() to just allocate a new buffer, and -+ * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing -+ * reference, additional references may be created with av_buffer_ref(). -+ * Use av_buffer_unref() to free a reference (this will automatically free the -+ * data once all the references are freed). -+ * -+ * The convention throughout this API and the rest of FFmpeg is such that the -+ * buffer is considered writable if there exists only one reference to it (and -+ * it has not been marked as read-only). The av_buffer_is_writable() function is -+ * provided to check whether this is true and av_buffer_make_writable() will -+ * automatically create a new writable buffer when necessary. -+ * Of course nothing prevents the calling code from violating this convention, -+ * however that is safe only when all the existing references are under its -+ * control. -+ * -+ * @note Referencing and unreferencing the buffers is thread-safe and thus -+ * may be done from multiple threads simultaneously without any need for -+ * additional locking. -+ * -+ * @note Two different references to the same buffer can point to different -+ * parts of the buffer (i.e. their AVBufferRef.data will not be equal). -+ */ -+ -+/** -+ * A reference counted buffer type. It is opaque and is meant to be used through -+ * references (AVBufferRef). -+ */ -+typedef struct AVBuffer AVBuffer; -+ -+/** -+ * A reference to a data buffer. -+ * -+ * The size of this struct is not a part of the public ABI and it is not meant -+ * to be allocated directly. -+ */ -+typedef struct AVBufferRef { -+ AVBuffer *buffer; -+ -+ /** -+ * The data buffer. It is considered writable if and only if -+ * this is the only reference to the buffer, in which case -+ * av_buffer_is_writable() returns 1. -+ */ -+ uint8_t *data; -+ /** -+ * Size of data in bytes. -+ */ -+ int size; -+} AVBufferRef; -+ -+/** -+ * Allocate an AVBuffer of the given size using av_malloc(). -+ * -+ * @return an AVBufferRef of given size or NULL when out of memory -+ */ -+AVBufferRef *av_buffer_alloc(int size); -+ -+/** -+ * Same as av_buffer_alloc(), except the returned buffer will be initialized -+ * to zero. -+ */ -+AVBufferRef *av_buffer_allocz(int size); -+ -+/** -+ * Always treat the buffer as read-only, even when it has only one -+ * reference. -+ */ -+#define AV_BUFFER_FLAG_READONLY (1 << 0) -+ -+/** -+ * Create an AVBuffer from an existing array. -+ * -+ * If this function is successful, data is owned by the AVBuffer. The caller may -+ * only access data through the returned AVBufferRef and references derived from -+ * it. -+ * If this function fails, data is left untouched. -+ * @param data data array -+ * @param size size of data in bytes -+ * @param free a callback for freeing this buffer's data -+ * @param opaque parameter to be got for processing or passed to free -+ * @param flags a combination of AV_BUFFER_FLAG_* -+ * -+ * @return an AVBufferRef referring to data on success, NULL on failure. -+ */ -+AVBufferRef *av_buffer_create(uint8_t *data, int size, -+ void (*free)(void *opaque, uint8_t *data), -+ void *opaque, int flags); -+ -+/** -+ * Default free callback, which calls av_free() on the buffer data. -+ * This function is meant to be passed to av_buffer_create(), not called -+ * directly. -+ */ -+void av_buffer_default_free(void *opaque, uint8_t *data); -+ -+/** -+ * Create a new reference to an AVBuffer. -+ * -+ * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on -+ * failure. -+ */ -+AVBufferRef *av_buffer_ref(AVBufferRef *buf); -+ -+/** -+ * Free a given reference and automatically free the buffer if there are no more -+ * references to it. -+ * -+ * @param buf the reference to be freed. The pointer is set to NULL on return. -+ */ -+void av_buffer_unref(AVBufferRef **buf); -+ -+/** -+ * @return 1 if the caller may write to the data referred to by buf (which is -+ * true if and only if buf is the only reference to the underlying AVBuffer). -+ * Return 0 otherwise. -+ * A positive answer is valid until av_buffer_ref() is called on buf. -+ */ -+int av_buffer_is_writable(const AVBufferRef *buf); -+ -+/** -+ * @return the opaque parameter set by av_buffer_create. -+ */ -+void *av_buffer_get_opaque(const AVBufferRef *buf); -+ -+int av_buffer_get_ref_count(const AVBufferRef *buf); -+ -+/** -+ * Create a writable reference from a given buffer reference, avoiding data copy -+ * if possible. -+ * -+ * @param buf buffer reference to make writable. On success, buf is either left -+ * untouched, or it is unreferenced and a new writable AVBufferRef is -+ * written in its place. On failure, buf is left untouched. -+ * @return 0 on success, a negative AVERROR on failure. -+ */ -+int av_buffer_make_writable(AVBufferRef **buf); -+ -+/** -+ * Reallocate a given buffer. -+ * -+ * @param buf a buffer reference to reallocate. On success, buf will be -+ * unreferenced and a new reference with the required size will be -+ * written in its place. On failure buf will be left untouched. *buf -+ * may be NULL, then a new buffer is allocated. -+ * @param size required new buffer size. -+ * @return 0 on success, a negative AVERROR on failure. -+ * -+ * @note the buffer is actually reallocated with av_realloc() only if it was -+ * initially allocated through av_buffer_realloc(NULL) and there is only one -+ * reference to it (i.e. the one passed to this function). In all other cases -+ * a new buffer is allocated and the data is copied. -+ */ -+int av_buffer_realloc(AVBufferRef **buf, int size); -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @defgroup lavu_bufferpool AVBufferPool -+ * @ingroup lavu_data -+ * -+ * @{ -+ * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers. -+ * -+ * Frequently allocating and freeing large buffers may be slow. AVBufferPool is -+ * meant to solve this in cases when the caller needs a set of buffers of the -+ * same size (the most obvious use case being buffers for raw video or audio -+ * frames). -+ * -+ * At the beginning, the user must call av_buffer_pool_init() to create the -+ * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to -+ * get a reference to a new buffer, similar to av_buffer_alloc(). This new -+ * reference works in all aspects the same way as the one created by -+ * av_buffer_alloc(). However, when the last reference to this buffer is -+ * unreferenced, it is returned to the pool instead of being freed and will be -+ * reused for subsequent av_buffer_pool_get() calls. -+ * -+ * When the caller is done with the pool and no longer needs to allocate any new -+ * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable. -+ * Once all the buffers are released, it will automatically be freed. -+ * -+ * Allocating and releasing buffers with this API is thread-safe as long as -+ * either the default alloc callback is used, or the user-supplied one is -+ * thread-safe. -+ */ -+ -+/** -+ * The buffer pool. This structure is opaque and not meant to be accessed -+ * directly. It is allocated with av_buffer_pool_init() and freed with -+ * av_buffer_pool_uninit(). -+ */ -+typedef struct AVBufferPool AVBufferPool; -+ -+/** -+ * Allocate and initialize a buffer pool. -+ * -+ * @param size size of each buffer in this pool -+ * @param alloc a function that will be used to allocate new buffers when the -+ * pool is empty. May be NULL, then the default allocator will be used -+ * (av_buffer_alloc()). -+ * @return newly created buffer pool on success, NULL on error. -+ */ -+AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size)); -+ -+/** -+ * Allocate and initialize a buffer pool with a more complex allocator. -+ * -+ * @param size size of each buffer in this pool -+ * @param opaque arbitrary user data used by the allocator -+ * @param alloc a function that will be used to allocate new buffers when the -+ * pool is empty. -+ * @param pool_free a function that will be called immediately before the pool -+ * is freed. I.e. after av_buffer_pool_uninit() is called -+ * by the caller and all the frames are returned to the pool -+ * and freed. It is intended to uninitialize the user opaque -+ * data. -+ * @return newly created buffer pool on success, NULL on error. -+ */ -+AVBufferPool *av_buffer_pool_init2(int size, void *opaque, -+ AVBufferRef* (*alloc)(void *opaque, int size), -+ void (*pool_free)(void *opaque)); -+ -+/** -+ * Mark the pool as being available for freeing. It will actually be freed only -+ * once all the allocated buffers associated with the pool are released. Thus it -+ * is safe to call this function while some of the allocated buffers are still -+ * in use. -+ * -+ * @param pool pointer to the pool to be freed. It will be set to NULL. -+ */ -+void av_buffer_pool_uninit(AVBufferPool **pool); -+ -+/** -+ * Allocate a new AVBuffer, reusing an old buffer from the pool when available. -+ * This function may be called simultaneously from multiple threads. -+ * -+ * @return a reference to the new buffer on success, NULL on error. -+ */ -+AVBufferRef *av_buffer_pool_get(AVBufferPool *pool); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_BUFFER_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/channel_layout.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/channel_layout.h -new file mode 100644 -index 000000000000..50bb8f03c586 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/channel_layout.h -@@ -0,0 +1,232 @@ -+/* -+ * Copyright (c) 2006 Michael Niedermayer -+ * Copyright (c) 2008 Peter Ross -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_CHANNEL_LAYOUT_H -+#define AVUTIL_CHANNEL_LAYOUT_H -+ -+#include -+ -+/** -+ * @file -+ * audio channel layout utility functions -+ */ -+ -+/** -+ * @addtogroup lavu_audio -+ * @{ -+ */ -+ -+/** -+ * @defgroup channel_masks Audio channel masks -+ * -+ * A channel layout is a 64-bits integer with a bit set for every channel. -+ * The number of bits set must be equal to the number of channels. -+ * The value 0 means that the channel layout is not known. -+ * @note this data structure is not powerful enough to handle channels -+ * combinations that have the same channel multiple times, such as -+ * dual-mono. -+ * -+ * @{ -+ */ -+#define AV_CH_FRONT_LEFT 0x00000001 -+#define AV_CH_FRONT_RIGHT 0x00000002 -+#define AV_CH_FRONT_CENTER 0x00000004 -+#define AV_CH_LOW_FREQUENCY 0x00000008 -+#define AV_CH_BACK_LEFT 0x00000010 -+#define AV_CH_BACK_RIGHT 0x00000020 -+#define AV_CH_FRONT_LEFT_OF_CENTER 0x00000040 -+#define AV_CH_FRONT_RIGHT_OF_CENTER 0x00000080 -+#define AV_CH_BACK_CENTER 0x00000100 -+#define AV_CH_SIDE_LEFT 0x00000200 -+#define AV_CH_SIDE_RIGHT 0x00000400 -+#define AV_CH_TOP_CENTER 0x00000800 -+#define AV_CH_TOP_FRONT_LEFT 0x00001000 -+#define AV_CH_TOP_FRONT_CENTER 0x00002000 -+#define AV_CH_TOP_FRONT_RIGHT 0x00004000 -+#define AV_CH_TOP_BACK_LEFT 0x00008000 -+#define AV_CH_TOP_BACK_CENTER 0x00010000 -+#define AV_CH_TOP_BACK_RIGHT 0x00020000 -+#define AV_CH_STEREO_LEFT 0x20000000 ///< Stereo downmix. -+#define AV_CH_STEREO_RIGHT 0x40000000 ///< See AV_CH_STEREO_LEFT. -+#define AV_CH_WIDE_LEFT 0x0000000080000000ULL -+#define AV_CH_WIDE_RIGHT 0x0000000100000000ULL -+#define AV_CH_SURROUND_DIRECT_LEFT 0x0000000200000000ULL -+#define AV_CH_SURROUND_DIRECT_RIGHT 0x0000000400000000ULL -+#define AV_CH_LOW_FREQUENCY_2 0x0000000800000000ULL -+ -+/** Channel mask value used for AVCodecContext.request_channel_layout -+ to indicate that the user requests the channel order of the decoder output -+ to be the native codec channel order. */ -+#define AV_CH_LAYOUT_NATIVE 0x8000000000000000ULL -+ -+/** -+ * @} -+ * @defgroup channel_mask_c Audio channel layouts -+ * @{ -+ * */ -+#define AV_CH_LAYOUT_MONO (AV_CH_FRONT_CENTER) -+#define AV_CH_LAYOUT_STEREO (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT) -+#define AV_CH_LAYOUT_2POINT1 (AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY) -+#define AV_CH_LAYOUT_2_1 (AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER) -+#define AV_CH_LAYOUT_SURROUND (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) -+#define AV_CH_LAYOUT_3POINT1 (AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY) -+#define AV_CH_LAYOUT_4POINT0 (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_CENTER) -+#define AV_CH_LAYOUT_4POINT1 (AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY) -+#define AV_CH_LAYOUT_2_2 (AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT) -+#define AV_CH_LAYOUT_QUAD (AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) -+#define AV_CH_LAYOUT_5POINT0 (AV_CH_LAYOUT_SURROUND|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT) -+#define AV_CH_LAYOUT_5POINT1 (AV_CH_LAYOUT_5POINT0|AV_CH_LOW_FREQUENCY) -+#define AV_CH_LAYOUT_5POINT0_BACK (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) -+#define AV_CH_LAYOUT_5POINT1_BACK (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_LOW_FREQUENCY) -+#define AV_CH_LAYOUT_6POINT0 (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_CENTER) -+#define AV_CH_LAYOUT_6POINT0_FRONT (AV_CH_LAYOUT_2_2|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER) -+#define AV_CH_LAYOUT_HEXAGONAL (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_BACK_CENTER) -+#define AV_CH_LAYOUT_6POINT1 (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) -+#define AV_CH_LAYOUT_6POINT1_BACK (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER) -+#define AV_CH_LAYOUT_6POINT1_FRONT (AV_CH_LAYOUT_6POINT0_FRONT|AV_CH_LOW_FREQUENCY) -+#define AV_CH_LAYOUT_7POINT0 (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) -+#define AV_CH_LAYOUT_7POINT0_FRONT (AV_CH_LAYOUT_5POINT0|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER) -+#define AV_CH_LAYOUT_7POINT1 (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) -+#define AV_CH_LAYOUT_7POINT1_WIDE (AV_CH_LAYOUT_5POINT1|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER) -+#define AV_CH_LAYOUT_7POINT1_WIDE_BACK (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER) -+#define AV_CH_LAYOUT_OCTAGONAL (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_CENTER|AV_CH_BACK_RIGHT) -+#define AV_CH_LAYOUT_HEXADECAGONAL (AV_CH_LAYOUT_OCTAGONAL|AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_FRONT_CENTER|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT) -+#define AV_CH_LAYOUT_STEREO_DOWNMIX (AV_CH_STEREO_LEFT|AV_CH_STEREO_RIGHT) -+ -+enum AVMatrixEncoding { -+ AV_MATRIX_ENCODING_NONE, -+ AV_MATRIX_ENCODING_DOLBY, -+ AV_MATRIX_ENCODING_DPLII, -+ AV_MATRIX_ENCODING_DPLIIX, -+ AV_MATRIX_ENCODING_DPLIIZ, -+ AV_MATRIX_ENCODING_DOLBYEX, -+ AV_MATRIX_ENCODING_DOLBYHEADPHONE, -+ AV_MATRIX_ENCODING_NB -+}; -+ -+/** -+ * Return a channel layout id that matches name, or 0 if no match is found. -+ * -+ * name can be one or several of the following notations, -+ * separated by '+' or '|': -+ * - the name of an usual channel layout (mono, stereo, 4.0, quad, 5.0, -+ * 5.0(side), 5.1, 5.1(side), 7.1, 7.1(wide), downmix); -+ * - the name of a single channel (FL, FR, FC, LFE, BL, BR, FLC, FRC, BC, -+ * SL, SR, TC, TFL, TFC, TFR, TBL, TBC, TBR, DL, DR); -+ * - a number of channels, in decimal, followed by 'c', yielding -+ * the default channel layout for that number of channels (@see -+ * av_get_default_channel_layout); -+ * - a channel layout mask, in hexadecimal starting with "0x" (see the -+ * AV_CH_* macros). -+ * -+ * Example: "stereo+FC" = "2c+FC" = "2c+1c" = "0x7" -+ */ -+uint64_t av_get_channel_layout(const char *name); -+ -+/** -+ * Return a channel layout and the number of channels based on the specified name. -+ * -+ * This function is similar to (@see av_get_channel_layout), but can also parse -+ * unknown channel layout specifications. -+ * -+ * @param[in] name channel layout specification string -+ * @param[out] channel_layout parsed channel layout (0 if unknown) -+ * @param[out] nb_channels number of channels -+ * -+ * @return 0 on success, AVERROR(EINVAL) if the parsing fails. -+ */ -+int av_get_extended_channel_layout(const char *name, uint64_t* channel_layout, int* nb_channels); -+ -+/** -+ * Return a description of a channel layout. -+ * If nb_channels is <= 0, it is guessed from the channel_layout. -+ * -+ * @param buf put here the string containing the channel layout -+ * @param buf_size size in bytes of the buffer -+ */ -+void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout); -+ -+struct AVBPrint; -+/** -+ * Append a description of a channel layout to a bprint buffer. -+ */ -+void av_bprint_channel_layout(struct AVBPrint *bp, int nb_channels, uint64_t channel_layout); -+ -+/** -+ * Return the number of channels in the channel layout. -+ */ -+int av_get_channel_layout_nb_channels(uint64_t channel_layout); -+ -+/** -+ * Return default channel layout for a given number of channels. -+ */ -+int64_t av_get_default_channel_layout(int nb_channels); -+ -+/** -+ * Get the index of a channel in channel_layout. -+ * -+ * @param channel a channel layout describing exactly one channel which must be -+ * present in channel_layout. -+ * -+ * @return index of channel in channel_layout on success, a negative AVERROR -+ * on error. -+ */ -+int av_get_channel_layout_channel_index(uint64_t channel_layout, -+ uint64_t channel); -+ -+/** -+ * Get the channel with the given index in channel_layout. -+ */ -+uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index); -+ -+/** -+ * Get the name of a given channel. -+ * -+ * @return channel name on success, NULL on error. -+ */ -+const char *av_get_channel_name(uint64_t channel); -+ -+/** -+ * Get the description of a given channel. -+ * -+ * @param channel a channel layout with a single channel -+ * @return channel description on success, NULL on error -+ */ -+const char *av_get_channel_description(uint64_t channel); -+ -+/** -+ * Get the value and name of a standard channel layout. -+ * -+ * @param[in] index index in an internal list, starting at 0 -+ * @param[out] layout channel layout mask -+ * @param[out] name name of the layout -+ * @return 0 if the layout exists, -+ * <0 if index is beyond the limits -+ */ -+int av_get_standard_channel_layout(unsigned index, uint64_t *layout, -+ const char **name); -+ -+/** -+ * @} -+ * @} -+ */ -+ -+#endif /* AVUTIL_CHANNEL_LAYOUT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/common.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/common.h -new file mode 100644 -index 000000000000..0fffa67714e8 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/common.h -@@ -0,0 +1,560 @@ -+/* -+ * copyright (c) 2006 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * common internal and external API header -+ */ -+ -+#ifndef AVUTIL_COMMON_H -+#define AVUTIL_COMMON_H -+ -+#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C) -+#error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS -+#endif -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "attributes.h" -+#include "macros.h" -+#include "version.h" -+#include "libavutil/avconfig.h" -+ -+#if AV_HAVE_BIGENDIAN -+# define AV_NE(be, le) (be) -+#else -+# define AV_NE(be, le) (le) -+#endif -+ -+//rounded division & shift -+#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b)) -+/* assume b>0 */ -+#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) -+/* Fast a/(1<=0 and b>=0 */ -+#define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \ -+ : ((a) + (1<<(b)) - 1) >> (b)) -+/* Backwards compat. */ -+#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT -+ -+#define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b)) -+#define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b)) -+ -+/** -+ * Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they -+ * are not representable as absolute values of their type. This is the same -+ * as with *abs() -+ * @see FFNABS() -+ */ -+#define FFABS(a) ((a) >= 0 ? (a) : (-(a))) -+#define FFSIGN(a) ((a) > 0 ? 1 : -1) -+ -+/** -+ * Negative Absolute value. -+ * this works for all integers of all types. -+ * As with many macros, this evaluates its argument twice, it thus must not have -+ * a sideeffect, that is FFNABS(x++) has undefined behavior. -+ */ -+#define FFNABS(a) ((a) <= 0 ? (a) : (-(a))) -+ -+/** -+ * Comparator. -+ * For two numerical expressions x and y, gives 1 if x > y, -1 if x < y, and 0 -+ * if x == y. This is useful for instance in a qsort comparator callback. -+ * Furthermore, compilers are able to optimize this to branchless code, and -+ * there is no risk of overflow with signed types. -+ * As with many macros, this evaluates its argument multiple times, it thus -+ * must not have a side-effect. -+ */ -+#define FFDIFFSIGN(x,y) (((x)>(y)) - ((x)<(y))) -+ -+#define FFMAX(a,b) ((a) > (b) ? (a) : (b)) -+#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c) -+#define FFMIN(a,b) ((a) > (b) ? (b) : (a)) -+#define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c) -+ -+#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) -+#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) -+ -+/* misc math functions */ -+ -+#ifdef HAVE_AV_CONFIG_H -+# include "config.h" -+# include "intmath.h" -+#endif -+ -+/* Pull in unguarded fallback defines at the end of this file. */ -+#include "common.h" -+ -+#ifndef av_log2 -+av_const int av_log2(unsigned v); -+#endif -+ -+#ifndef av_log2_16bit -+av_const int av_log2_16bit(unsigned v); -+#endif -+ -+/** -+ * Clip a signed integer value into the amin-amax range. -+ * @param a value to clip -+ * @param amin minimum value of the clip range -+ * @param amax maximum value of the clip range -+ * @return clipped value -+ */ -+static av_always_inline av_const int av_clip_c(int a, int amin, int amax) -+{ -+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2 -+ if (amin > amax) abort(); -+#endif -+ if (a < amin) return amin; -+ else if (a > amax) return amax; -+ else return a; -+} -+ -+/** -+ * Clip a signed 64bit integer value into the amin-amax range. -+ * @param a value to clip -+ * @param amin minimum value of the clip range -+ * @param amax maximum value of the clip range -+ * @return clipped value -+ */ -+static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax) -+{ -+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2 -+ if (amin > amax) abort(); -+#endif -+ if (a < amin) return amin; -+ else if (a > amax) return amax; -+ else return a; -+} -+ -+/** -+ * Clip a signed integer value into the 0-255 range. -+ * @param a value to clip -+ * @return clipped value -+ */ -+static av_always_inline av_const uint8_t av_clip_uint8_c(int a) -+{ -+ if (a&(~0xFF)) return (~a)>>31; -+ else return a; -+} -+ -+/** -+ * Clip a signed integer value into the -128,127 range. -+ * @param a value to clip -+ * @return clipped value -+ */ -+static av_always_inline av_const int8_t av_clip_int8_c(int a) -+{ -+ if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F; -+ else return a; -+} -+ -+/** -+ * Clip a signed integer value into the 0-65535 range. -+ * @param a value to clip -+ * @return clipped value -+ */ -+static av_always_inline av_const uint16_t av_clip_uint16_c(int a) -+{ -+ if (a&(~0xFFFF)) return (~a)>>31; -+ else return a; -+} -+ -+/** -+ * Clip a signed integer value into the -32768,32767 range. -+ * @param a value to clip -+ * @return clipped value -+ */ -+static av_always_inline av_const int16_t av_clip_int16_c(int a) -+{ -+ if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF; -+ else return a; -+} -+ -+/** -+ * Clip a signed 64-bit integer value into the -2147483648,2147483647 range. -+ * @param a value to clip -+ * @return clipped value -+ */ -+static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a) -+{ -+ if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF); -+ else return (int32_t)a; -+} -+ -+/** -+ * Clip a signed integer into the -(2^p),(2^p-1) range. -+ * @param a value to clip -+ * @param p bit position to clip at -+ * @return clipped value -+ */ -+static av_always_inline av_const int av_clip_intp2_c(int a, int p) -+{ -+ if (((unsigned)a + (1 << p)) & ~((2 << p) - 1)) -+ return (a >> 31) ^ ((1 << p) - 1); -+ else -+ return a; -+} -+ -+/** -+ * Clip a signed integer to an unsigned power of two range. -+ * @param a value to clip -+ * @param p bit position to clip at -+ * @return clipped value -+ */ -+static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p) -+{ -+ if (a & ~((1<> 31 & ((1<= 2 -+ if (amin > amax) abort(); -+#endif -+ if (a < amin) return amin; -+ else if (a > amax) return amax; -+ else return a; -+} -+ -+/** -+ * Clip a double value into the amin-amax range. -+ * @param a value to clip -+ * @param amin minimum value of the clip range -+ * @param amax maximum value of the clip range -+ * @return clipped value -+ */ -+static av_always_inline av_const double av_clipd_c(double a, double amin, double amax) -+{ -+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2 -+ if (amin > amax) abort(); -+#endif -+ if (a < amin) return amin; -+ else if (a > amax) return amax; -+ else return a; -+} -+ -+/** Compute ceil(log2(x)). -+ * @param x value used to compute ceil(log2(x)) -+ * @return computed ceiling of log2(x) -+ */ -+static av_always_inline av_const int av_ceil_log2_c(int x) -+{ -+ return av_log2((x - 1) << 1); -+} -+ -+/** -+ * Count number of bits set to one in x -+ * @param x value to count bits of -+ * @return the number of bits set to one in x -+ */ -+static av_always_inline av_const int av_popcount_c(uint32_t x) -+{ -+ x -= (x >> 1) & 0x55555555; -+ x = (x & 0x33333333) + ((x >> 2) & 0x33333333); -+ x = (x + (x >> 4)) & 0x0F0F0F0F; -+ x += x >> 8; -+ return (x + (x >> 16)) & 0x3F; -+} -+ -+/** -+ * Count number of bits set to one in x -+ * @param x value to count bits of -+ * @return the number of bits set to one in x -+ */ -+static av_always_inline av_const int av_popcount64_c(uint64_t x) -+{ -+ return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32)); -+} -+ -+static av_always_inline av_const int av_parity_c(uint32_t v) -+{ -+ return av_popcount(v) & 1; -+} -+ -+#define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24)) -+#define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24)) -+ -+/** -+ * Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form. -+ * -+ * @param val Output value, must be an lvalue of type uint32_t. -+ * @param GET_BYTE Expression reading one byte from the input. -+ * Evaluated up to 7 times (4 for the currently -+ * assigned Unicode range). With a memory buffer -+ * input, this could be *ptr++. -+ * @param ERROR Expression to be evaluated on invalid input, -+ * typically a goto statement. -+ * -+ * @warning ERROR should not contain a loop control statement which -+ * could interact with the internal while loop, and should force an -+ * exit from the macro code (e.g. through a goto or a return) in order -+ * to prevent undefined results. -+ */ -+#define GET_UTF8(val, GET_BYTE, ERROR)\ -+ val= (GET_BYTE);\ -+ {\ -+ uint32_t top = (val & 128) >> 1;\ -+ if ((val & 0xc0) == 0x80 || val >= 0xFE)\ -+ ERROR\ -+ while (val & top) {\ -+ int tmp= (GET_BYTE) - 128;\ -+ if(tmp>>6)\ -+ ERROR\ -+ val= (val<<6) + tmp;\ -+ top <<= 5;\ -+ }\ -+ val &= (top << 1) - 1;\ -+ } -+ -+/** -+ * Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form. -+ * -+ * @param val Output value, must be an lvalue of type uint32_t. -+ * @param GET_16BIT Expression returning two bytes of UTF-16 data converted -+ * to native byte order. Evaluated one or two times. -+ * @param ERROR Expression to be evaluated on invalid input, -+ * typically a goto statement. -+ */ -+#define GET_UTF16(val, GET_16BIT, ERROR)\ -+ val = GET_16BIT;\ -+ {\ -+ unsigned int hi = val - 0xD800;\ -+ if (hi < 0x800) {\ -+ val = GET_16BIT - 0xDC00;\ -+ if (val > 0x3FFU || hi > 0x3FFU)\ -+ ERROR\ -+ val += (hi<<10) + 0x10000;\ -+ }\ -+ }\ -+ -+/** -+ * @def PUT_UTF8(val, tmp, PUT_BYTE) -+ * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long). -+ * @param val is an input-only argument and should be of type uint32_t. It holds -+ * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If -+ * val is given as a function it is executed only once. -+ * @param tmp is a temporary variable and should be of type uint8_t. It -+ * represents an intermediate value during conversion that is to be -+ * output by PUT_BYTE. -+ * @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination. -+ * It could be a function or a statement, and uses tmp as the input byte. -+ * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be -+ * executed up to 4 times for values in the valid UTF-8 range and up to -+ * 7 times in the general case, depending on the length of the converted -+ * Unicode character. -+ */ -+#define PUT_UTF8(val, tmp, PUT_BYTE)\ -+ {\ -+ int bytes, shift;\ -+ uint32_t in = val;\ -+ if (in < 0x80) {\ -+ tmp = in;\ -+ PUT_BYTE\ -+ } else {\ -+ bytes = (av_log2(in) + 4) / 5;\ -+ shift = (bytes - 1) * 6;\ -+ tmp = (256 - (256 >> bytes)) | (in >> shift);\ -+ PUT_BYTE\ -+ while (shift >= 6) {\ -+ shift -= 6;\ -+ tmp = 0x80 | ((in >> shift) & 0x3f);\ -+ PUT_BYTE\ -+ }\ -+ }\ -+ } -+ -+/** -+ * @def PUT_UTF16(val, tmp, PUT_16BIT) -+ * Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes). -+ * @param val is an input-only argument and should be of type uint32_t. It holds -+ * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If -+ * val is given as a function it is executed only once. -+ * @param tmp is a temporary variable and should be of type uint16_t. It -+ * represents an intermediate value during conversion that is to be -+ * output by PUT_16BIT. -+ * @param PUT_16BIT writes the converted UTF-16 data to any proper destination -+ * in desired endianness. It could be a function or a statement, and uses tmp -+ * as the input byte. For example, PUT_BYTE could be "*output++ = tmp;" -+ * PUT_BYTE will be executed 1 or 2 times depending on input character. -+ */ -+#define PUT_UTF16(val, tmp, PUT_16BIT)\ -+ {\ -+ uint32_t in = val;\ -+ if (in < 0x10000) {\ -+ tmp = in;\ -+ PUT_16BIT\ -+ } else {\ -+ tmp = 0xD800 | ((in - 0x10000) >> 10);\ -+ PUT_16BIT\ -+ tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\ -+ PUT_16BIT\ -+ }\ -+ }\ -+ -+ -+ -+#include "mem.h" -+ -+#ifdef HAVE_AV_CONFIG_H -+# include "internal.h" -+#endif /* HAVE_AV_CONFIG_H */ -+ -+#endif /* AVUTIL_COMMON_H */ -+ -+/* -+ * The following definitions are outside the multiple inclusion guard -+ * to ensure they are immediately available in intmath.h. -+ */ -+ -+#ifndef av_ceil_log2 -+# define av_ceil_log2 av_ceil_log2_c -+#endif -+#ifndef av_clip -+# define av_clip av_clip_c -+#endif -+#ifndef av_clip64 -+# define av_clip64 av_clip64_c -+#endif -+#ifndef av_clip_uint8 -+# define av_clip_uint8 av_clip_uint8_c -+#endif -+#ifndef av_clip_int8 -+# define av_clip_int8 av_clip_int8_c -+#endif -+#ifndef av_clip_uint16 -+# define av_clip_uint16 av_clip_uint16_c -+#endif -+#ifndef av_clip_int16 -+# define av_clip_int16 av_clip_int16_c -+#endif -+#ifndef av_clipl_int32 -+# define av_clipl_int32 av_clipl_int32_c -+#endif -+#ifndef av_clip_intp2 -+# define av_clip_intp2 av_clip_intp2_c -+#endif -+#ifndef av_clip_uintp2 -+# define av_clip_uintp2 av_clip_uintp2_c -+#endif -+#ifndef av_mod_uintp2 -+# define av_mod_uintp2 av_mod_uintp2_c -+#endif -+#ifndef av_sat_add32 -+# define av_sat_add32 av_sat_add32_c -+#endif -+#ifndef av_sat_dadd32 -+# define av_sat_dadd32 av_sat_dadd32_c -+#endif -+#ifndef av_sat_sub32 -+# define av_sat_sub32 av_sat_sub32_c -+#endif -+#ifndef av_sat_dsub32 -+# define av_sat_dsub32 av_sat_dsub32_c -+#endif -+#ifndef av_clipf -+# define av_clipf av_clipf_c -+#endif -+#ifndef av_clipd -+# define av_clipd av_clipd_c -+#endif -+#ifndef av_popcount -+# define av_popcount av_popcount_c -+#endif -+#ifndef av_popcount64 -+# define av_popcount64 av_popcount64_c -+#endif -+#ifndef av_parity -+# define av_parity av_parity_c -+#endif -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/cpu.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/cpu.h -new file mode 100644 -index 000000000000..8bb9eb606bf2 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/cpu.h -@@ -0,0 +1,130 @@ -+/* -+ * Copyright (c) 2000, 2001, 2002 Fabrice Bellard -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_CPU_H -+#define AVUTIL_CPU_H -+ -+#include -+ -+#include "attributes.h" -+ -+#define AV_CPU_FLAG_FORCE 0x80000000 /* force usage of selected flags (OR) */ -+ -+ /* lower 16 bits - CPU features */ -+#define AV_CPU_FLAG_MMX 0x0001 ///< standard MMX -+#define AV_CPU_FLAG_MMXEXT 0x0002 ///< SSE integer functions or AMD MMX ext -+#define AV_CPU_FLAG_MMX2 0x0002 ///< SSE integer functions or AMD MMX ext -+#define AV_CPU_FLAG_3DNOW 0x0004 ///< AMD 3DNOW -+#define AV_CPU_FLAG_SSE 0x0008 ///< SSE functions -+#define AV_CPU_FLAG_SSE2 0x0010 ///< PIV SSE2 functions -+#define AV_CPU_FLAG_SSE2SLOW 0x40000000 ///< SSE2 supported, but usually not faster -+ ///< than regular MMX/SSE (e.g. Core1) -+#define AV_CPU_FLAG_3DNOWEXT 0x0020 ///< AMD 3DNowExt -+#define AV_CPU_FLAG_SSE3 0x0040 ///< Prescott SSE3 functions -+#define AV_CPU_FLAG_SSE3SLOW 0x20000000 ///< SSE3 supported, but usually not faster -+ ///< than regular MMX/SSE (e.g. Core1) -+#define AV_CPU_FLAG_SSSE3 0x0080 ///< Conroe SSSE3 functions -+#define AV_CPU_FLAG_SSSE3SLOW 0x4000000 ///< SSSE3 supported, but usually not faster -+#define AV_CPU_FLAG_ATOM 0x10000000 ///< Atom processor, some SSSE3 instructions are slower -+#define AV_CPU_FLAG_SSE4 0x0100 ///< Penryn SSE4.1 functions -+#define AV_CPU_FLAG_SSE42 0x0200 ///< Nehalem SSE4.2 functions -+#define AV_CPU_FLAG_AESNI 0x80000 ///< Advanced Encryption Standard functions -+#define AV_CPU_FLAG_AVX 0x4000 ///< AVX functions: requires OS support even if YMM registers aren't used -+#define AV_CPU_FLAG_AVXSLOW 0x8000000 ///< AVX supported, but slow when using YMM registers (e.g. Bulldozer) -+#define AV_CPU_FLAG_XOP 0x0400 ///< Bulldozer XOP functions -+#define AV_CPU_FLAG_FMA4 0x0800 ///< Bulldozer FMA4 functions -+#define AV_CPU_FLAG_CMOV 0x1000 ///< supports cmov instruction -+#define AV_CPU_FLAG_AVX2 0x8000 ///< AVX2 functions: requires OS support even if YMM registers aren't used -+#define AV_CPU_FLAG_FMA3 0x10000 ///< Haswell FMA3 functions -+#define AV_CPU_FLAG_BMI1 0x20000 ///< Bit Manipulation Instruction Set 1 -+#define AV_CPU_FLAG_BMI2 0x40000 ///< Bit Manipulation Instruction Set 2 -+#define AV_CPU_FLAG_AVX512 0x100000 ///< AVX-512 functions: requires OS support even if YMM/ZMM registers aren't used -+ -+#define AV_CPU_FLAG_ALTIVEC 0x0001 ///< standard -+#define AV_CPU_FLAG_VSX 0x0002 ///< ISA 2.06 -+#define AV_CPU_FLAG_POWER8 0x0004 ///< ISA 2.07 -+ -+#define AV_CPU_FLAG_ARMV5TE (1 << 0) -+#define AV_CPU_FLAG_ARMV6 (1 << 1) -+#define AV_CPU_FLAG_ARMV6T2 (1 << 2) -+#define AV_CPU_FLAG_VFP (1 << 3) -+#define AV_CPU_FLAG_VFPV3 (1 << 4) -+#define AV_CPU_FLAG_NEON (1 << 5) -+#define AV_CPU_FLAG_ARMV8 (1 << 6) -+#define AV_CPU_FLAG_VFP_VM (1 << 7) ///< VFPv2 vector mode, deprecated in ARMv7-A and unavailable in various CPUs implementations -+#define AV_CPU_FLAG_SETEND (1 <<16) -+ -+/** -+ * Return the flags which specify extensions supported by the CPU. -+ * The returned value is affected by av_force_cpu_flags() if that was used -+ * before. So av_get_cpu_flags() can easily be used in an application to -+ * detect the enabled cpu flags. -+ */ -+int av_get_cpu_flags(void); -+ -+/** -+ * Disables cpu detection and forces the specified flags. -+ * -1 is a special case that disables forcing of specific flags. -+ */ -+void av_force_cpu_flags(int flags); -+ -+/** -+ * Set a mask on flags returned by av_get_cpu_flags(). -+ * This function is mainly useful for testing. -+ * Please use av_force_cpu_flags() and av_get_cpu_flags() instead which are more flexible -+ */ -+attribute_deprecated void av_set_cpu_flags_mask(int mask); -+ -+/** -+ * Parse CPU flags from a string. -+ * -+ * The returned flags contain the specified flags as well as related unspecified flags. -+ * -+ * This function exists only for compatibility with libav. -+ * Please use av_parse_cpu_caps() when possible. -+ * @return a combination of AV_CPU_* flags, negative on error. -+ */ -+attribute_deprecated -+int av_parse_cpu_flags(const char *s); -+ -+/** -+ * Parse CPU caps from a string and update the given AV_CPU_* flags based on that. -+ * -+ * @return negative on error. -+ */ -+int av_parse_cpu_caps(unsigned *flags, const char *s); -+ -+/** -+ * @return the number of logical CPU cores present. -+ */ -+int av_cpu_count(void); -+ -+/** -+ * Get the maximum data alignment that may be required by FFmpeg. -+ * -+ * Note that this is affected by the build configuration and the CPU flags mask, -+ * so e.g. if the CPU supports AVX, but libavutil has been built with -+ * --disable-avx or the AV_CPU_FLAG_AVX flag has been disabled through -+ * av_set_cpu_flags_mask(), then this function will behave as if AVX is not -+ * present. -+ */ -+size_t av_cpu_max_align(void); -+ -+#endif /* AVUTIL_CPU_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/dict.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/dict.h -new file mode 100644 -index 000000000000..118f1f00ed20 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/dict.h -@@ -0,0 +1,200 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * Public dictionary API. -+ * @deprecated -+ * AVDictionary is provided for compatibility with libav. It is both in -+ * implementation as well as API inefficient. It does not scale and is -+ * extremely slow with large dictionaries. -+ * It is recommended that new code uses our tree container from tree.c/h -+ * where applicable, which uses AVL trees to achieve O(log n) performance. -+ */ -+ -+#ifndef AVUTIL_DICT_H -+#define AVUTIL_DICT_H -+ -+#include -+ -+#include "version.h" -+ -+/** -+ * @addtogroup lavu_dict AVDictionary -+ * @ingroup lavu_data -+ * -+ * @brief Simple key:value store -+ * -+ * @{ -+ * Dictionaries are used for storing key:value pairs. To create -+ * an AVDictionary, simply pass an address of a NULL pointer to -+ * av_dict_set(). NULL can be used as an empty dictionary wherever -+ * a pointer to an AVDictionary is required. -+ * Use av_dict_get() to retrieve an entry or iterate over all -+ * entries and finally av_dict_free() to free the dictionary -+ * and all its contents. -+ * -+ @code -+ AVDictionary *d = NULL; // "create" an empty dictionary -+ AVDictionaryEntry *t = NULL; -+ -+ av_dict_set(&d, "foo", "bar", 0); // add an entry -+ -+ char *k = av_strdup("key"); // if your strings are already allocated, -+ char *v = av_strdup("value"); // you can avoid copying them like this -+ av_dict_set(&d, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); -+ -+ while (t = av_dict_get(d, "", t, AV_DICT_IGNORE_SUFFIX)) { -+ <....> // iterate over all entries in d -+ } -+ av_dict_free(&d); -+ @endcode -+ */ -+ -+#define AV_DICT_MATCH_CASE 1 /**< Only get an entry with exact-case key match. Only relevant in av_dict_get(). */ -+#define AV_DICT_IGNORE_SUFFIX 2 /**< Return first entry in a dictionary whose first part corresponds to the search key, -+ ignoring the suffix of the found key string. Only relevant in av_dict_get(). */ -+#define AV_DICT_DONT_STRDUP_KEY 4 /**< Take ownership of a key that's been -+ allocated with av_malloc() or another memory allocation function. */ -+#define AV_DICT_DONT_STRDUP_VAL 8 /**< Take ownership of a value that's been -+ allocated with av_malloc() or another memory allocation function. */ -+#define AV_DICT_DONT_OVERWRITE 16 ///< Don't overwrite existing entries. -+#define AV_DICT_APPEND 32 /**< If the entry already exists, append to it. Note that no -+ delimiter is added, the strings are simply concatenated. */ -+#define AV_DICT_MULTIKEY 64 /**< Allow to store several equal keys in the dictionary */ -+ -+typedef struct AVDictionaryEntry { -+ char *key; -+ char *value; -+} AVDictionaryEntry; -+ -+typedef struct AVDictionary AVDictionary; -+ -+/** -+ * Get a dictionary entry with matching key. -+ * -+ * The returned entry key or value must not be changed, or it will -+ * cause undefined behavior. -+ * -+ * To iterate through all the dictionary entries, you can set the matching key -+ * to the null string "" and set the AV_DICT_IGNORE_SUFFIX flag. -+ * -+ * @param prev Set to the previous matching element to find the next. -+ * If set to NULL the first matching element is returned. -+ * @param key matching key -+ * @param flags a collection of AV_DICT_* flags controlling how the entry is retrieved -+ * @return found entry or NULL in case no matching entry was found in the dictionary -+ */ -+AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key, -+ const AVDictionaryEntry *prev, int flags); -+ -+/** -+ * Get number of entries in dictionary. -+ * -+ * @param m dictionary -+ * @return number of entries in dictionary -+ */ -+int av_dict_count(const AVDictionary *m); -+ -+/** -+ * Set the given entry in *pm, overwriting an existing entry. -+ * -+ * Note: If AV_DICT_DONT_STRDUP_KEY or AV_DICT_DONT_STRDUP_VAL is set, -+ * these arguments will be freed on error. -+ * -+ * Warning: Adding a new entry to a dictionary invalidates all existing entries -+ * previously returned with av_dict_get. -+ * -+ * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL -+ * a dictionary struct is allocated and put in *pm. -+ * @param key entry key to add to *pm (will either be av_strduped or added as a new key depending on flags) -+ * @param value entry value to add to *pm (will be av_strduped or added as a new key depending on flags). -+ * Passing a NULL value will cause an existing entry to be deleted. -+ * @return >= 0 on success otherwise an error code <0 -+ */ -+int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags); -+ -+/** -+ * Convenience wrapper for av_dict_set that converts the value to a string -+ * and stores it. -+ * -+ * Note: If AV_DICT_DONT_STRDUP_KEY is set, key will be freed on error. -+ */ -+int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags); -+ -+/** -+ * Parse the key/value pairs list and add the parsed entries to a dictionary. -+ * -+ * In case of failure, all the successfully set entries are stored in -+ * *pm. You may need to manually free the created dictionary. -+ * -+ * @param key_val_sep a 0-terminated list of characters used to separate -+ * key from value -+ * @param pairs_sep a 0-terminated list of characters used to separate -+ * two pairs from each other -+ * @param flags flags to use when adding to dictionary. -+ * AV_DICT_DONT_STRDUP_KEY and AV_DICT_DONT_STRDUP_VAL -+ * are ignored since the key/value tokens will always -+ * be duplicated. -+ * @return 0 on success, negative AVERROR code on failure -+ */ -+int av_dict_parse_string(AVDictionary **pm, const char *str, -+ const char *key_val_sep, const char *pairs_sep, -+ int flags); -+ -+/** -+ * Copy entries from one AVDictionary struct into another. -+ * @param dst pointer to a pointer to a AVDictionary struct. If *dst is NULL, -+ * this function will allocate a struct for you and put it in *dst -+ * @param src pointer to source AVDictionary struct -+ * @param flags flags to use when setting entries in *dst -+ * @note metadata is read using the AV_DICT_IGNORE_SUFFIX flag -+ * @return 0 on success, negative AVERROR code on failure. If dst was allocated -+ * by this function, callers should free the associated memory. -+ */ -+int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags); -+ -+/** -+ * Free all the memory allocated for an AVDictionary struct -+ * and all keys and values. -+ */ -+void av_dict_free(AVDictionary **m); -+ -+/** -+ * Get dictionary entries as a string. -+ * -+ * Create a string containing dictionary's entries. -+ * Such string may be passed back to av_dict_parse_string(). -+ * @note String is escaped with backslashes ('\'). -+ * -+ * @param[in] m dictionary -+ * @param[out] buffer Pointer to buffer that will be allocated with string containg entries. -+ * Buffer must be freed by the caller when is no longer needed. -+ * @param[in] key_val_sep character used to separate key from value -+ * @param[in] pairs_sep character used to separate two pairs from each other -+ * @return >= 0 on success, negative on error -+ * @warning Separators cannot be neither '\\' nor '\0'. They also cannot be the same. -+ */ -+int av_dict_get_string(const AVDictionary *m, char **buffer, -+ const char key_val_sep, const char pairs_sep); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_DICT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/error.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/error.h -new file mode 100644 -index 000000000000..71df4da353b9 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/error.h -@@ -0,0 +1,126 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * error code definitions -+ */ -+ -+#ifndef AVUTIL_ERROR_H -+#define AVUTIL_ERROR_H -+ -+#include -+#include -+ -+/** -+ * @addtogroup lavu_error -+ * -+ * @{ -+ */ -+ -+ -+/* error handling */ -+#if EDOM > 0 -+#define AVERROR(e) (-(e)) ///< Returns a negative error code from a POSIX error code, to return from library functions. -+#define AVUNERROR(e) (-(e)) ///< Returns a POSIX error code from a library function error return value. -+#else -+/* Some platforms have E* and errno already negated. */ -+#define AVERROR(e) (e) -+#define AVUNERROR(e) (e) -+#endif -+ -+#define FFERRTAG(a, b, c, d) (-(int)MKTAG(a, b, c, d)) -+ -+#define AVERROR_BSF_NOT_FOUND FFERRTAG(0xF8,'B','S','F') ///< Bitstream filter not found -+#define AVERROR_BUG FFERRTAG( 'B','U','G','!') ///< Internal bug, also see AVERROR_BUG2 -+#define AVERROR_BUFFER_TOO_SMALL FFERRTAG( 'B','U','F','S') ///< Buffer too small -+#define AVERROR_DECODER_NOT_FOUND FFERRTAG(0xF8,'D','E','C') ///< Decoder not found -+#define AVERROR_DEMUXER_NOT_FOUND FFERRTAG(0xF8,'D','E','M') ///< Demuxer not found -+#define AVERROR_ENCODER_NOT_FOUND FFERRTAG(0xF8,'E','N','C') ///< Encoder not found -+#define AVERROR_EOF FFERRTAG( 'E','O','F',' ') ///< End of file -+#define AVERROR_EXIT FFERRTAG( 'E','X','I','T') ///< Immediate exit was requested; the called function should not be restarted -+#define AVERROR_EXTERNAL FFERRTAG( 'E','X','T',' ') ///< Generic error in an external library -+#define AVERROR_FILTER_NOT_FOUND FFERRTAG(0xF8,'F','I','L') ///< Filter not found -+#define AVERROR_INVALIDDATA FFERRTAG( 'I','N','D','A') ///< Invalid data found when processing input -+#define AVERROR_MUXER_NOT_FOUND FFERRTAG(0xF8,'M','U','X') ///< Muxer not found -+#define AVERROR_OPTION_NOT_FOUND FFERRTAG(0xF8,'O','P','T') ///< Option not found -+#define AVERROR_PATCHWELCOME FFERRTAG( 'P','A','W','E') ///< Not yet implemented in FFmpeg, patches welcome -+#define AVERROR_PROTOCOL_NOT_FOUND FFERRTAG(0xF8,'P','R','O') ///< Protocol not found -+ -+#define AVERROR_STREAM_NOT_FOUND FFERRTAG(0xF8,'S','T','R') ///< Stream not found -+/** -+ * This is semantically identical to AVERROR_BUG -+ * it has been introduced in Libav after our AVERROR_BUG and with a modified value. -+ */ -+#define AVERROR_BUG2 FFERRTAG( 'B','U','G',' ') -+#define AVERROR_UNKNOWN FFERRTAG( 'U','N','K','N') ///< Unknown error, typically from an external library -+#define AVERROR_EXPERIMENTAL (-0x2bb2afa8) ///< Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it. -+#define AVERROR_INPUT_CHANGED (-0x636e6701) ///< Input changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_OUTPUT_CHANGED) -+#define AVERROR_OUTPUT_CHANGED (-0x636e6702) ///< Output changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_INPUT_CHANGED) -+/* HTTP & RTSP errors */ -+#define AVERROR_HTTP_BAD_REQUEST FFERRTAG(0xF8,'4','0','0') -+#define AVERROR_HTTP_UNAUTHORIZED FFERRTAG(0xF8,'4','0','1') -+#define AVERROR_HTTP_FORBIDDEN FFERRTAG(0xF8,'4','0','3') -+#define AVERROR_HTTP_NOT_FOUND FFERRTAG(0xF8,'4','0','4') -+#define AVERROR_HTTP_OTHER_4XX FFERRTAG(0xF8,'4','X','X') -+#define AVERROR_HTTP_SERVER_ERROR FFERRTAG(0xF8,'5','X','X') -+ -+#define AV_ERROR_MAX_STRING_SIZE 64 -+ -+/** -+ * Put a description of the AVERROR code errnum in errbuf. -+ * In case of failure the global variable errno is set to indicate the -+ * error. Even in case of failure av_strerror() will print a generic -+ * error message indicating the errnum provided to errbuf. -+ * -+ * @param errnum error code to describe -+ * @param errbuf buffer to which description is written -+ * @param errbuf_size the size in bytes of errbuf -+ * @return 0 on success, a negative value if a description for errnum -+ * cannot be found -+ */ -+int av_strerror(int errnum, char *errbuf, size_t errbuf_size); -+ -+/** -+ * Fill the provided buffer with a string containing an error string -+ * corresponding to the AVERROR code errnum. -+ * -+ * @param errbuf a buffer -+ * @param errbuf_size size in bytes of errbuf -+ * @param errnum error code to describe -+ * @return the buffer in input, filled with the error description -+ * @see av_strerror() -+ */ -+static inline char *av_make_error_string(char *errbuf, size_t errbuf_size, int errnum) -+{ -+ av_strerror(errnum, errbuf, errbuf_size); -+ return errbuf; -+} -+ -+/** -+ * Convenience macro, the return value should be used only directly in -+ * function arguments but never stand-alone. -+ */ -+#define av_err2str(errnum) \ -+ av_make_error_string((char[AV_ERROR_MAX_STRING_SIZE]){0}, AV_ERROR_MAX_STRING_SIZE, errnum) -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_ERROR_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/frame.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/frame.h -new file mode 100644 -index 000000000000..9d57d6ce66ff ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/frame.h -@@ -0,0 +1,893 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * @ingroup lavu_frame -+ * reference-counted frame API -+ */ -+ -+#ifndef AVUTIL_FRAME_H -+#define AVUTIL_FRAME_H -+ -+#include -+#include -+ -+#include "avutil.h" -+#include "buffer.h" -+#include "dict.h" -+#include "rational.h" -+#include "samplefmt.h" -+#include "pixfmt.h" -+#include "version.h" -+ -+ -+/** -+ * @defgroup lavu_frame AVFrame -+ * @ingroup lavu_data -+ * -+ * @{ -+ * AVFrame is an abstraction for reference-counted raw multimedia data. -+ */ -+ -+enum AVFrameSideDataType { -+ /** -+ * The data is the AVPanScan struct defined in libavcodec. -+ */ -+ AV_FRAME_DATA_PANSCAN, -+ /** -+ * ATSC A53 Part 4 Closed Captions. -+ * A53 CC bitstream is stored as uint8_t in AVFrameSideData.data. -+ * The number of bytes of CC data is AVFrameSideData.size. -+ */ -+ AV_FRAME_DATA_A53_CC, -+ /** -+ * Stereoscopic 3d metadata. -+ * The data is the AVStereo3D struct defined in libavutil/stereo3d.h. -+ */ -+ AV_FRAME_DATA_STEREO3D, -+ /** -+ * The data is the AVMatrixEncoding enum defined in libavutil/channel_layout.h. -+ */ -+ AV_FRAME_DATA_MATRIXENCODING, -+ /** -+ * Metadata relevant to a downmix procedure. -+ * The data is the AVDownmixInfo struct defined in libavutil/downmix_info.h. -+ */ -+ AV_FRAME_DATA_DOWNMIX_INFO, -+ /** -+ * ReplayGain information in the form of the AVReplayGain struct. -+ */ -+ AV_FRAME_DATA_REPLAYGAIN, -+ /** -+ * This side data contains a 3x3 transformation matrix describing an affine -+ * transformation that needs to be applied to the frame for correct -+ * presentation. -+ * -+ * See libavutil/display.h for a detailed description of the data. -+ */ -+ AV_FRAME_DATA_DISPLAYMATRIX, -+ /** -+ * Active Format Description data consisting of a single byte as specified -+ * in ETSI TS 101 154 using AVActiveFormatDescription enum. -+ */ -+ AV_FRAME_DATA_AFD, -+ /** -+ * Motion vectors exported by some codecs (on demand through the export_mvs -+ * flag set in the libavcodec AVCodecContext flags2 option). -+ * The data is the AVMotionVector struct defined in -+ * libavutil/motion_vector.h. -+ */ -+ AV_FRAME_DATA_MOTION_VECTORS, -+ /** -+ * Recommmends skipping the specified number of samples. This is exported -+ * only if the "skip_manual" AVOption is set in libavcodec. -+ * This has the same format as AV_PKT_DATA_SKIP_SAMPLES. -+ * @code -+ * u32le number of samples to skip from start of this packet -+ * u32le number of samples to skip from end of this packet -+ * u8 reason for start skip -+ * u8 reason for end skip (0=padding silence, 1=convergence) -+ * @endcode -+ */ -+ AV_FRAME_DATA_SKIP_SAMPLES, -+ /** -+ * This side data must be associated with an audio frame and corresponds to -+ * enum AVAudioServiceType defined in avcodec.h. -+ */ -+ AV_FRAME_DATA_AUDIO_SERVICE_TYPE, -+ /** -+ * Mastering display metadata associated with a video frame. The payload is -+ * an AVMasteringDisplayMetadata type and contains information about the -+ * mastering display color volume. -+ */ -+ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA, -+ /** -+ * The GOP timecode in 25 bit timecode format. Data format is 64-bit integer. -+ * This is set on the first frame of a GOP that has a temporal reference of 0. -+ */ -+ AV_FRAME_DATA_GOP_TIMECODE, -+ -+ /** -+ * The data represents the AVSphericalMapping structure defined in -+ * libavutil/spherical.h. -+ */ -+ AV_FRAME_DATA_SPHERICAL, -+ -+ /** -+ * Content light level (based on CTA-861.3). This payload contains data in -+ * the form of the AVContentLightMetadata struct. -+ */ -+ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL, -+ -+ /** -+ * The data contains an ICC profile as an opaque octet buffer following the -+ * format described by ISO 15076-1 with an optional name defined in the -+ * metadata key entry "name". -+ */ -+ AV_FRAME_DATA_ICC_PROFILE, -+ -+#if FF_API_FRAME_QP -+ /** -+ * Implementation-specific description of the format of AV_FRAME_QP_TABLE_DATA. -+ * The contents of this side data are undocumented and internal; use -+ * av_frame_set_qp_table() and av_frame_get_qp_table() to access this in a -+ * meaningful way instead. -+ */ -+ AV_FRAME_DATA_QP_TABLE_PROPERTIES, -+ -+ /** -+ * Raw QP table data. Its format is described by -+ * AV_FRAME_DATA_QP_TABLE_PROPERTIES. Use av_frame_set_qp_table() and -+ * av_frame_get_qp_table() to access this instead. -+ */ -+ AV_FRAME_DATA_QP_TABLE_DATA, -+#endif -+}; -+ -+enum AVActiveFormatDescription { -+ AV_AFD_SAME = 8, -+ AV_AFD_4_3 = 9, -+ AV_AFD_16_9 = 10, -+ AV_AFD_14_9 = 11, -+ AV_AFD_4_3_SP_14_9 = 13, -+ AV_AFD_16_9_SP_14_9 = 14, -+ AV_AFD_SP_4_3 = 15, -+}; -+ -+ -+/** -+ * Structure to hold side data for an AVFrame. -+ * -+ * sizeof(AVFrameSideData) is not a part of the public ABI, so new fields may be added -+ * to the end with a minor bump. -+ */ -+typedef struct AVFrameSideData { -+ enum AVFrameSideDataType type; -+ uint8_t *data; -+ int size; -+ AVDictionary *metadata; -+ AVBufferRef *buf; -+} AVFrameSideData; -+ -+/** -+ * This structure describes decoded (raw) audio or video data. -+ * -+ * AVFrame must be allocated using av_frame_alloc(). Note that this only -+ * allocates the AVFrame itself, the buffers for the data must be managed -+ * through other means (see below). -+ * AVFrame must be freed with av_frame_free(). -+ * -+ * AVFrame is typically allocated once and then reused multiple times to hold -+ * different data (e.g. a single AVFrame to hold frames received from a -+ * decoder). In such a case, av_frame_unref() will free any references held by -+ * the frame and reset it to its original clean state before it -+ * is reused again. -+ * -+ * The data described by an AVFrame is usually reference counted through the -+ * AVBuffer API. The underlying buffer references are stored in AVFrame.buf / -+ * AVFrame.extended_buf. An AVFrame is considered to be reference counted if at -+ * least one reference is set, i.e. if AVFrame.buf[0] != NULL. In such a case, -+ * every single data plane must be contained in one of the buffers in -+ * AVFrame.buf or AVFrame.extended_buf. -+ * There may be a single buffer for all the data, or one separate buffer for -+ * each plane, or anything in between. -+ * -+ * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added -+ * to the end with a minor bump. -+ * -+ * Fields can be accessed through AVOptions, the name string used, matches the -+ * C structure field name for fields accessible through AVOptions. The AVClass -+ * for AVFrame can be obtained from avcodec_get_frame_class() -+ */ -+typedef struct AVFrame { -+#define AV_NUM_DATA_POINTERS 8 -+ /** -+ * pointer to the picture/channel planes. -+ * This might be different from the first allocated byte -+ * -+ * Some decoders access areas outside 0,0 - width,height, please -+ * see avcodec_align_dimensions2(). Some filters and swscale can read -+ * up to 16 bytes beyond the planes, if these filters are to be used, -+ * then 16 extra bytes must be allocated. -+ * -+ * NOTE: Except for hwaccel formats, pointers not needed by the format -+ * MUST be set to NULL. -+ */ -+ uint8_t *data[AV_NUM_DATA_POINTERS]; -+ -+ /** -+ * For video, size in bytes of each picture line. -+ * For audio, size in bytes of each plane. -+ * -+ * For audio, only linesize[0] may be set. For planar audio, each channel -+ * plane must be the same size. -+ * -+ * For video the linesizes should be multiples of the CPUs alignment -+ * preference, this is 16 or 32 for modern desktop CPUs. -+ * Some code requires such alignment other code can be slower without -+ * correct alignment, for yet other it makes no difference. -+ * -+ * @note The linesize may be larger than the size of usable data -- there -+ * may be extra padding present for performance reasons. -+ */ -+ int linesize[AV_NUM_DATA_POINTERS]; -+ -+ /** -+ * pointers to the data planes/channels. -+ * -+ * For video, this should simply point to data[]. -+ * -+ * For planar audio, each channel has a separate data pointer, and -+ * linesize[0] contains the size of each channel buffer. -+ * For packed audio, there is just one data pointer, and linesize[0] -+ * contains the total size of the buffer for all channels. -+ * -+ * Note: Both data and extended_data should always be set in a valid frame, -+ * but for planar audio with more channels that can fit in data, -+ * extended_data must be used in order to access all channels. -+ */ -+ uint8_t **extended_data; -+ -+ /** -+ * @name Video dimensions -+ * Video frames only. The coded dimensions (in pixels) of the video frame, -+ * i.e. the size of the rectangle that contains some well-defined values. -+ * -+ * @note The part of the frame intended for display/presentation is further -+ * restricted by the @ref cropping "Cropping rectangle". -+ * @{ -+ */ -+ int width, height; -+ /** -+ * @} -+ */ -+ -+ /** -+ * number of audio samples (per channel) described by this frame -+ */ -+ int nb_samples; -+ -+ /** -+ * format of the frame, -1 if unknown or unset -+ * Values correspond to enum AVPixelFormat for video frames, -+ * enum AVSampleFormat for audio) -+ */ -+ int format; -+ -+ /** -+ * 1 -> keyframe, 0-> not -+ */ -+ int key_frame; -+ -+ /** -+ * Picture type of the frame. -+ */ -+ enum AVPictureType pict_type; -+ -+ /** -+ * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified. -+ */ -+ AVRational sample_aspect_ratio; -+ -+ /** -+ * Presentation timestamp in time_base units (time when frame should be shown to user). -+ */ -+ int64_t pts; -+ -+#if FF_API_PKT_PTS -+ /** -+ * PTS copied from the AVPacket that was decoded to produce this frame. -+ * @deprecated use the pts field instead -+ */ -+ attribute_deprecated -+ int64_t pkt_pts; -+#endif -+ -+ /** -+ * DTS copied from the AVPacket that triggered returning this frame. (if frame threading isn't used) -+ * This is also the Presentation time of this AVFrame calculated from -+ * only AVPacket.dts values without pts values. -+ */ -+ int64_t pkt_dts; -+ -+ /** -+ * picture number in bitstream order -+ */ -+ int coded_picture_number; -+ /** -+ * picture number in display order -+ */ -+ int display_picture_number; -+ -+ /** -+ * quality (between 1 (good) and FF_LAMBDA_MAX (bad)) -+ */ -+ int quality; -+ -+ /** -+ * for some private data of the user -+ */ -+ void *opaque; -+ -+#if FF_API_ERROR_FRAME -+ /** -+ * @deprecated unused -+ */ -+ attribute_deprecated -+ uint64_t error[AV_NUM_DATA_POINTERS]; -+#endif -+ -+ /** -+ * When decoding, this signals how much the picture must be delayed. -+ * extra_delay = repeat_pict / (2*fps) -+ */ -+ int repeat_pict; -+ -+ /** -+ * The content of the picture is interlaced. -+ */ -+ int interlaced_frame; -+ -+ /** -+ * If the content is interlaced, is top field displayed first. -+ */ -+ int top_field_first; -+ -+ /** -+ * Tell user application that palette has changed from previous frame. -+ */ -+ int palette_has_changed; -+ -+ /** -+ * reordered opaque 64 bits (generally an integer or a double precision float -+ * PTS but can be anything). -+ * The user sets AVCodecContext.reordered_opaque to represent the input at -+ * that time, -+ * the decoder reorders values as needed and sets AVFrame.reordered_opaque -+ * to exactly one of the values provided by the user through AVCodecContext.reordered_opaque -+ * @deprecated in favor of pkt_pts -+ */ -+ int64_t reordered_opaque; -+ -+ /** -+ * Sample rate of the audio data. -+ */ -+ int sample_rate; -+ -+ /** -+ * Channel layout of the audio data. -+ */ -+ uint64_t channel_layout; -+ -+ /** -+ * AVBuffer references backing the data for this frame. If all elements of -+ * this array are NULL, then this frame is not reference counted. This array -+ * must be filled contiguously -- if buf[i] is non-NULL then buf[j] must -+ * also be non-NULL for all j < i. -+ * -+ * There may be at most one AVBuffer per data plane, so for video this array -+ * always contains all the references. For planar audio with more than -+ * AV_NUM_DATA_POINTERS channels, there may be more buffers than can fit in -+ * this array. Then the extra AVBufferRef pointers are stored in the -+ * extended_buf array. -+ */ -+ AVBufferRef *buf[AV_NUM_DATA_POINTERS]; -+ -+ /** -+ * For planar audio which requires more than AV_NUM_DATA_POINTERS -+ * AVBufferRef pointers, this array will hold all the references which -+ * cannot fit into AVFrame.buf. -+ * -+ * Note that this is different from AVFrame.extended_data, which always -+ * contains all the pointers. This array only contains the extra pointers, -+ * which cannot fit into AVFrame.buf. -+ * -+ * This array is always allocated using av_malloc() by whoever constructs -+ * the frame. It is freed in av_frame_unref(). -+ */ -+ AVBufferRef **extended_buf; -+ /** -+ * Number of elements in extended_buf. -+ */ -+ int nb_extended_buf; -+ -+ AVFrameSideData **side_data; -+ int nb_side_data; -+ -+/** -+ * @defgroup lavu_frame_flags AV_FRAME_FLAGS -+ * @ingroup lavu_frame -+ * Flags describing additional frame properties. -+ * -+ * @{ -+ */ -+ -+/** -+ * The frame data may be corrupted, e.g. due to decoding errors. -+ */ -+#define AV_FRAME_FLAG_CORRUPT (1 << 0) -+/** -+ * A flag to mark the frames which need to be decoded, but shouldn't be output. -+ */ -+#define AV_FRAME_FLAG_DISCARD (1 << 2) -+/** -+ * @} -+ */ -+ -+ /** -+ * Frame flags, a combination of @ref lavu_frame_flags -+ */ -+ int flags; -+ -+ /** -+ * MPEG vs JPEG YUV range. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVColorRange color_range; -+ -+ enum AVColorPrimaries color_primaries; -+ -+ enum AVColorTransferCharacteristic color_trc; -+ -+ /** -+ * YUV colorspace type. -+ * - encoding: Set by user -+ * - decoding: Set by libavcodec -+ */ -+ enum AVColorSpace colorspace; -+ -+ enum AVChromaLocation chroma_location; -+ -+ /** -+ * frame timestamp estimated using various heuristics, in stream time base -+ * - encoding: unused -+ * - decoding: set by libavcodec, read by user. -+ */ -+ int64_t best_effort_timestamp; -+ -+ /** -+ * reordered pos from the last AVPacket that has been input into the decoder -+ * - encoding: unused -+ * - decoding: Read by user. -+ */ -+ int64_t pkt_pos; -+ -+ /** -+ * duration of the corresponding packet, expressed in -+ * AVStream->time_base units, 0 if unknown. -+ * - encoding: unused -+ * - decoding: Read by user. -+ */ -+ int64_t pkt_duration; -+ -+ /** -+ * metadata. -+ * - encoding: Set by user. -+ * - decoding: Set by libavcodec. -+ */ -+ AVDictionary *metadata; -+ -+ /** -+ * decode error flags of the frame, set to a combination of -+ * FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there -+ * were errors during the decoding. -+ * - encoding: unused -+ * - decoding: set by libavcodec, read by user. -+ */ -+ int decode_error_flags; -+#define FF_DECODE_ERROR_INVALID_BITSTREAM 1 -+#define FF_DECODE_ERROR_MISSING_REFERENCE 2 -+ -+ /** -+ * number of audio channels, only used for audio. -+ * - encoding: unused -+ * - decoding: Read by user. -+ */ -+ int channels; -+ -+ /** -+ * size of the corresponding packet containing the compressed -+ * frame. -+ * It is set to a negative value if unknown. -+ * - encoding: unused -+ * - decoding: set by libavcodec, read by user. -+ */ -+ int pkt_size; -+ -+#if FF_API_FRAME_QP -+ /** -+ * QP table -+ */ -+ attribute_deprecated -+ int8_t *qscale_table; -+ /** -+ * QP store stride -+ */ -+ attribute_deprecated -+ int qstride; -+ -+ attribute_deprecated -+ int qscale_type; -+ -+ attribute_deprecated -+ AVBufferRef *qp_table_buf; -+#endif -+ /** -+ * For hwaccel-format frames, this should be a reference to the -+ * AVHWFramesContext describing the frame. -+ */ -+ AVBufferRef *hw_frames_ctx; -+ -+ /** -+ * AVBufferRef for free use by the API user. FFmpeg will never check the -+ * contents of the buffer ref. FFmpeg calls av_buffer_unref() on it when -+ * the frame is unreferenced. av_frame_copy_props() calls create a new -+ * reference with av_buffer_ref() for the target frame's opaque_ref field. -+ * -+ * This is unrelated to the opaque field, although it serves a similar -+ * purpose. -+ */ -+ AVBufferRef *opaque_ref; -+ -+ /** -+ * @anchor cropping -+ * @name Cropping -+ * Video frames only. The number of pixels to discard from the the -+ * top/bottom/left/right border of the frame to obtain the sub-rectangle of -+ * the frame intended for presentation. -+ * @{ -+ */ -+ size_t crop_top; -+ size_t crop_bottom; -+ size_t crop_left; -+ size_t crop_right; -+ /** -+ * @} -+ */ -+ -+ /** -+ * AVBufferRef for internal use by a single libav* library. -+ * Must not be used to transfer data between libraries. -+ * Has to be NULL when ownership of the frame leaves the respective library. -+ * -+ * Code outside the FFmpeg libs should never check or change the contents of the buffer ref. -+ * -+ * FFmpeg calls av_buffer_unref() on it when the frame is unreferenced. -+ * av_frame_copy_props() calls create a new reference with av_buffer_ref() -+ * for the target frame's private_ref field. -+ */ -+ AVBufferRef *private_ref; -+} AVFrame; -+ -+#if FF_API_FRAME_GET_SET -+/** -+ * Accessors for some AVFrame fields. These used to be provided for ABI -+ * compatibility, and do not need to be used anymore. -+ */ -+attribute_deprecated -+int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_best_effort_timestamp(AVFrame *frame, int64_t val); -+attribute_deprecated -+int64_t av_frame_get_pkt_duration (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_pkt_duration (AVFrame *frame, int64_t val); -+attribute_deprecated -+int64_t av_frame_get_pkt_pos (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_pkt_pos (AVFrame *frame, int64_t val); -+attribute_deprecated -+int64_t av_frame_get_channel_layout (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_channel_layout (AVFrame *frame, int64_t val); -+attribute_deprecated -+int av_frame_get_channels (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_channels (AVFrame *frame, int val); -+attribute_deprecated -+int av_frame_get_sample_rate (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_sample_rate (AVFrame *frame, int val); -+attribute_deprecated -+AVDictionary *av_frame_get_metadata (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_metadata (AVFrame *frame, AVDictionary *val); -+attribute_deprecated -+int av_frame_get_decode_error_flags (const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_decode_error_flags (AVFrame *frame, int val); -+attribute_deprecated -+int av_frame_get_pkt_size(const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_pkt_size(AVFrame *frame, int val); -+#if FF_API_FRAME_QP -+attribute_deprecated -+int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type); -+attribute_deprecated -+int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int type); -+#endif -+attribute_deprecated -+enum AVColorSpace av_frame_get_colorspace(const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_colorspace(AVFrame *frame, enum AVColorSpace val); -+attribute_deprecated -+enum AVColorRange av_frame_get_color_range(const AVFrame *frame); -+attribute_deprecated -+void av_frame_set_color_range(AVFrame *frame, enum AVColorRange val); -+#endif -+ -+/** -+ * Get the name of a colorspace. -+ * @return a static string identifying the colorspace; can be NULL. -+ */ -+const char *av_get_colorspace_name(enum AVColorSpace val); -+ -+/** -+ * Allocate an AVFrame and set its fields to default values. The resulting -+ * struct must be freed using av_frame_free(). -+ * -+ * @return An AVFrame filled with default values or NULL on failure. -+ * -+ * @note this only allocates the AVFrame itself, not the data buffers. Those -+ * must be allocated through other means, e.g. with av_frame_get_buffer() or -+ * manually. -+ */ -+AVFrame *av_frame_alloc(void); -+ -+/** -+ * Free the frame and any dynamically allocated objects in it, -+ * e.g. extended_data. If the frame is reference counted, it will be -+ * unreferenced first. -+ * -+ * @param frame frame to be freed. The pointer will be set to NULL. -+ */ -+void av_frame_free(AVFrame **frame); -+ -+/** -+ * Set up a new reference to the data described by the source frame. -+ * -+ * Copy frame properties from src to dst and create a new reference for each -+ * AVBufferRef from src. -+ * -+ * If src is not reference counted, new buffers are allocated and the data is -+ * copied. -+ * -+ * @warning: dst MUST have been either unreferenced with av_frame_unref(dst), -+ * or newly allocated with av_frame_alloc() before calling this -+ * function, or undefined behavior will occur. -+ * -+ * @return 0 on success, a negative AVERROR on error -+ */ -+int av_frame_ref(AVFrame *dst, const AVFrame *src); -+ -+/** -+ * Create a new frame that references the same data as src. -+ * -+ * This is a shortcut for av_frame_alloc()+av_frame_ref(). -+ * -+ * @return newly created AVFrame on success, NULL on error. -+ */ -+AVFrame *av_frame_clone(const AVFrame *src); -+ -+/** -+ * Unreference all the buffers referenced by frame and reset the frame fields. -+ */ -+void av_frame_unref(AVFrame *frame); -+ -+/** -+ * Move everything contained in src to dst and reset src. -+ * -+ * @warning: dst is not unreferenced, but directly overwritten without reading -+ * or deallocating its contents. Call av_frame_unref(dst) manually -+ * before calling this function to ensure that no memory is leaked. -+ */ -+void av_frame_move_ref(AVFrame *dst, AVFrame *src); -+ -+/** -+ * Allocate new buffer(s) for audio or video data. -+ * -+ * The following fields must be set on frame before calling this function: -+ * - format (pixel format for video, sample format for audio) -+ * - width and height for video -+ * - nb_samples and channel_layout for audio -+ * -+ * This function will fill AVFrame.data and AVFrame.buf arrays and, if -+ * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf. -+ * For planar formats, one buffer will be allocated for each plane. -+ * -+ * @warning: if frame already has been allocated, calling this function will -+ * leak memory. In addition, undefined behavior can occur in certain -+ * cases. -+ * -+ * @param frame frame in which to store the new buffers. -+ * @param align Required buffer size alignment. If equal to 0, alignment will be -+ * chosen automatically for the current CPU. It is highly -+ * recommended to pass 0 here unless you know what you are doing. -+ * -+ * @return 0 on success, a negative AVERROR on error. -+ */ -+int av_frame_get_buffer(AVFrame *frame, int align); -+ -+/** -+ * Check if the frame data is writable. -+ * -+ * @return A positive value if the frame data is writable (which is true if and -+ * only if each of the underlying buffers has only one reference, namely the one -+ * stored in this frame). Return 0 otherwise. -+ * -+ * If 1 is returned the answer is valid until av_buffer_ref() is called on any -+ * of the underlying AVBufferRefs (e.g. through av_frame_ref() or directly). -+ * -+ * @see av_frame_make_writable(), av_buffer_is_writable() -+ */ -+int av_frame_is_writable(AVFrame *frame); -+ -+/** -+ * Ensure that the frame data is writable, avoiding data copy if possible. -+ * -+ * Do nothing if the frame is writable, allocate new buffers and copy the data -+ * if it is not. -+ * -+ * @return 0 on success, a negative AVERROR on error. -+ * -+ * @see av_frame_is_writable(), av_buffer_is_writable(), -+ * av_buffer_make_writable() -+ */ -+int av_frame_make_writable(AVFrame *frame); -+ -+/** -+ * Copy the frame data from src to dst. -+ * -+ * This function does not allocate anything, dst must be already initialized and -+ * allocated with the same parameters as src. -+ * -+ * This function only copies the frame data (i.e. the contents of the data / -+ * extended data arrays), not any other properties. -+ * -+ * @return >= 0 on success, a negative AVERROR on error. -+ */ -+int av_frame_copy(AVFrame *dst, const AVFrame *src); -+ -+/** -+ * Copy only "metadata" fields from src to dst. -+ * -+ * Metadata for the purpose of this function are those fields that do not affect -+ * the data layout in the buffers. E.g. pts, sample rate (for audio) or sample -+ * aspect ratio (for video), but not width/height or channel layout. -+ * Side data is also copied. -+ */ -+int av_frame_copy_props(AVFrame *dst, const AVFrame *src); -+ -+/** -+ * Get the buffer reference a given data plane is stored in. -+ * -+ * @param plane index of the data plane of interest in frame->extended_data. -+ * -+ * @return the buffer reference that contains the plane or NULL if the input -+ * frame is not valid. -+ */ -+AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane); -+ -+/** -+ * Add a new side data to a frame. -+ * -+ * @param frame a frame to which the side data should be added -+ * @param type type of the added side data -+ * @param size size of the side data -+ * -+ * @return newly added side data on success, NULL on error -+ */ -+AVFrameSideData *av_frame_new_side_data(AVFrame *frame, -+ enum AVFrameSideDataType type, -+ int size); -+ -+/** -+ * Add a new side data to a frame from an existing AVBufferRef -+ * -+ * @param frame a frame to which the side data should be added -+ * @param type the type of the added side data -+ * @param buf an AVBufferRef to add as side data. The ownership of -+ * the reference is transferred to the frame. -+ * -+ * @return newly added side data on success, NULL on error. On failure -+ * the frame is unchanged and the AVBufferRef remains owned by -+ * the caller. -+ */ -+AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame, -+ enum AVFrameSideDataType type, -+ AVBufferRef *buf); -+ -+/** -+ * @return a pointer to the side data of a given type on success, NULL if there -+ * is no side data with such type in this frame. -+ */ -+AVFrameSideData *av_frame_get_side_data(const AVFrame *frame, -+ enum AVFrameSideDataType type); -+ -+/** -+ * If side data of the supplied type exists in the frame, free it and remove it -+ * from the frame. -+ */ -+void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type); -+ -+ -+/** -+ * Flags for frame cropping. -+ */ -+enum { -+ /** -+ * Apply the maximum possible cropping, even if it requires setting the -+ * AVFrame.data[] entries to unaligned pointers. Passing unaligned data -+ * to FFmpeg API is generally not allowed, and causes undefined behavior -+ * (such as crashes). You can pass unaligned data only to FFmpeg APIs that -+ * are explicitly documented to accept it. Use this flag only if you -+ * absolutely know what you are doing. -+ */ -+ AV_FRAME_CROP_UNALIGNED = 1 << 0, -+}; -+ -+/** -+ * Crop the given video AVFrame according to its crop_left/crop_top/crop_right/ -+ * crop_bottom fields. If cropping is successful, the function will adjust the -+ * data pointers and the width/height fields, and set the crop fields to 0. -+ * -+ * In all cases, the cropping boundaries will be rounded to the inherent -+ * alignment of the pixel format. In some cases, such as for opaque hwaccel -+ * formats, the left/top cropping is ignored. The crop fields are set to 0 even -+ * if the cropping was rounded or ignored. -+ * -+ * @param frame the frame which should be cropped -+ * @param flags Some combination of AV_FRAME_CROP_* flags, or 0. -+ * -+ * @return >= 0 on success, a negative AVERROR on error. If the cropping fields -+ * were invalid, AVERROR(ERANGE) is returned, and nothing is changed. -+ */ -+int av_frame_apply_cropping(AVFrame *frame, int flags); -+ -+/** -+ * @return a string identifying the side data type -+ */ -+const char *av_frame_side_data_name(enum AVFrameSideDataType type); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_FRAME_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/hwcontext.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/hwcontext.h -new file mode 100644 -index 000000000000..f5a4b6238774 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/hwcontext.h -@@ -0,0 +1,584 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_HWCONTEXT_H -+#define AVUTIL_HWCONTEXT_H -+ -+#include "buffer.h" -+#include "frame.h" -+#include "log.h" -+#include "pixfmt.h" -+ -+enum AVHWDeviceType { -+ AV_HWDEVICE_TYPE_NONE, -+ AV_HWDEVICE_TYPE_VDPAU, -+ AV_HWDEVICE_TYPE_CUDA, -+ AV_HWDEVICE_TYPE_VAAPI, -+ AV_HWDEVICE_TYPE_DXVA2, -+ AV_HWDEVICE_TYPE_QSV, -+ AV_HWDEVICE_TYPE_VIDEOTOOLBOX, -+ AV_HWDEVICE_TYPE_D3D11VA, -+ AV_HWDEVICE_TYPE_DRM, -+ AV_HWDEVICE_TYPE_OPENCL, -+ AV_HWDEVICE_TYPE_MEDIACODEC, -+}; -+ -+typedef struct AVHWDeviceInternal AVHWDeviceInternal; -+ -+/** -+ * This struct aggregates all the (hardware/vendor-specific) "high-level" state, -+ * i.e. state that is not tied to a concrete processing configuration. -+ * E.g., in an API that supports hardware-accelerated encoding and decoding, -+ * this struct will (if possible) wrap the state that is common to both encoding -+ * and decoding and from which specific instances of encoders or decoders can be -+ * derived. -+ * -+ * This struct is reference-counted with the AVBuffer mechanism. The -+ * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field -+ * points to the actual AVHWDeviceContext. Further objects derived from -+ * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with -+ * specific properties) will hold an internal reference to it. After all the -+ * references are released, the AVHWDeviceContext itself will be freed, -+ * optionally invoking a user-specified callback for uninitializing the hardware -+ * state. -+ */ -+typedef struct AVHWDeviceContext { -+ /** -+ * A class for logging. Set by av_hwdevice_ctx_alloc(). -+ */ -+ const AVClass *av_class; -+ -+ /** -+ * Private data used internally by libavutil. Must not be accessed in any -+ * way by the caller. -+ */ -+ AVHWDeviceInternal *internal; -+ -+ /** -+ * This field identifies the underlying API used for hardware access. -+ * -+ * This field is set when this struct is allocated and never changed -+ * afterwards. -+ */ -+ enum AVHWDeviceType type; -+ -+ /** -+ * The format-specific data, allocated and freed by libavutil along with -+ * this context. -+ * -+ * Should be cast by the user to the format-specific context defined in the -+ * corresponding header (hwcontext_*.h) and filled as described in the -+ * documentation before calling av_hwdevice_ctx_init(). -+ * -+ * After calling av_hwdevice_ctx_init() this struct should not be modified -+ * by the caller. -+ */ -+ void *hwctx; -+ -+ /** -+ * This field may be set by the caller before calling av_hwdevice_ctx_init(). -+ * -+ * If non-NULL, this callback will be called when the last reference to -+ * this context is unreferenced, immediately before it is freed. -+ * -+ * @note when other objects (e.g an AVHWFramesContext) are derived from this -+ * struct, this callback will be invoked after all such child objects -+ * are fully uninitialized and their respective destructors invoked. -+ */ -+ void (*free)(struct AVHWDeviceContext *ctx); -+ -+ /** -+ * Arbitrary user data, to be used e.g. by the free() callback. -+ */ -+ void *user_opaque; -+} AVHWDeviceContext; -+ -+typedef struct AVHWFramesInternal AVHWFramesInternal; -+ -+/** -+ * This struct describes a set or pool of "hardware" frames (i.e. those with -+ * data not located in normal system memory). All the frames in the pool are -+ * assumed to be allocated in the same way and interchangeable. -+ * -+ * This struct is reference-counted with the AVBuffer mechanism and tied to a -+ * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor -+ * yields a reference, whose data field points to the actual AVHWFramesContext -+ * struct. -+ */ -+typedef struct AVHWFramesContext { -+ /** -+ * A class for logging. -+ */ -+ const AVClass *av_class; -+ -+ /** -+ * Private data used internally by libavutil. Must not be accessed in any -+ * way by the caller. -+ */ -+ AVHWFramesInternal *internal; -+ -+ /** -+ * A reference to the parent AVHWDeviceContext. This reference is owned and -+ * managed by the enclosing AVHWFramesContext, but the caller may derive -+ * additional references from it. -+ */ -+ AVBufferRef *device_ref; -+ -+ /** -+ * The parent AVHWDeviceContext. This is simply a pointer to -+ * device_ref->data provided for convenience. -+ * -+ * Set by libavutil in av_hwframe_ctx_init(). -+ */ -+ AVHWDeviceContext *device_ctx; -+ -+ /** -+ * The format-specific data, allocated and freed automatically along with -+ * this context. -+ * -+ * Should be cast by the user to the format-specific context defined in the -+ * corresponding header (hwframe_*.h) and filled as described in the -+ * documentation before calling av_hwframe_ctx_init(). -+ * -+ * After any frames using this context are created, the contents of this -+ * struct should not be modified by the caller. -+ */ -+ void *hwctx; -+ -+ /** -+ * This field may be set by the caller before calling av_hwframe_ctx_init(). -+ * -+ * If non-NULL, this callback will be called when the last reference to -+ * this context is unreferenced, immediately before it is freed. -+ */ -+ void (*free)(struct AVHWFramesContext *ctx); -+ -+ /** -+ * Arbitrary user data, to be used e.g. by the free() callback. -+ */ -+ void *user_opaque; -+ -+ /** -+ * A pool from which the frames are allocated by av_hwframe_get_buffer(). -+ * This field may be set by the caller before calling av_hwframe_ctx_init(). -+ * The buffers returned by calling av_buffer_pool_get() on this pool must -+ * have the properties described in the documentation in the corresponding hw -+ * type's header (hwcontext_*.h). The pool will be freed strictly before -+ * this struct's free() callback is invoked. -+ * -+ * This field may be NULL, then libavutil will attempt to allocate a pool -+ * internally. Note that certain device types enforce pools allocated at -+ * fixed size (frame count), which cannot be extended dynamically. In such a -+ * case, initial_pool_size must be set appropriately. -+ */ -+ AVBufferPool *pool; -+ -+ /** -+ * Initial size of the frame pool. If a device type does not support -+ * dynamically resizing the pool, then this is also the maximum pool size. -+ * -+ * May be set by the caller before calling av_hwframe_ctx_init(). Must be -+ * set if pool is NULL and the device type does not support dynamic pools. -+ */ -+ int initial_pool_size; -+ -+ /** -+ * The pixel format identifying the underlying HW surface type. -+ * -+ * Must be a hwaccel format, i.e. the corresponding descriptor must have the -+ * AV_PIX_FMT_FLAG_HWACCEL flag set. -+ * -+ * Must be set by the user before calling av_hwframe_ctx_init(). -+ */ -+ enum AVPixelFormat format; -+ -+ /** -+ * The pixel format identifying the actual data layout of the hardware -+ * frames. -+ * -+ * Must be set by the caller before calling av_hwframe_ctx_init(). -+ * -+ * @note when the underlying API does not provide the exact data layout, but -+ * only the colorspace/bit depth, this field should be set to the fully -+ * planar version of that format (e.g. for 8-bit 420 YUV it should be -+ * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else). -+ */ -+ enum AVPixelFormat sw_format; -+ -+ /** -+ * The allocated dimensions of the frames in this pool. -+ * -+ * Must be set by the user before calling av_hwframe_ctx_init(). -+ */ -+ int width, height; -+} AVHWFramesContext; -+ -+/** -+ * Look up an AVHWDeviceType by name. -+ * -+ * @param name String name of the device type (case-insensitive). -+ * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if -+ * not found. -+ */ -+enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name); -+ -+/** Get the string name of an AVHWDeviceType. -+ * -+ * @param type Type from enum AVHWDeviceType. -+ * @return Pointer to a static string containing the name, or NULL if the type -+ * is not valid. -+ */ -+const char *av_hwdevice_get_type_name(enum AVHWDeviceType type); -+ -+/** -+ * Iterate over supported device types. -+ * -+ * @param type AV_HWDEVICE_TYPE_NONE initially, then the previous type -+ * returned by this function in subsequent iterations. -+ * @return The next usable device type from enum AVHWDeviceType, or -+ * AV_HWDEVICE_TYPE_NONE if there are no more. -+ */ -+enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev); -+ -+/** -+ * Allocate an AVHWDeviceContext for a given hardware type. -+ * -+ * @param type the type of the hardware device to allocate. -+ * @return a reference to the newly created AVHWDeviceContext on success or NULL -+ * on failure. -+ */ -+AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type); -+ -+/** -+ * Finalize the device context before use. This function must be called after -+ * the context is filled with all the required information and before it is -+ * used in any way. -+ * -+ * @param ref a reference to the AVHWDeviceContext -+ * @return 0 on success, a negative AVERROR code on failure -+ */ -+int av_hwdevice_ctx_init(AVBufferRef *ref); -+ -+/** -+ * Open a device of the specified type and create an AVHWDeviceContext for it. -+ * -+ * This is a convenience function intended to cover the simple cases. Callers -+ * who need to fine-tune device creation/management should open the device -+ * manually and then wrap it in an AVHWDeviceContext using -+ * av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init(). -+ * -+ * The returned context is already initialized and ready for use, the caller -+ * should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of -+ * the created AVHWDeviceContext are set by this function and should not be -+ * touched by the caller. -+ * -+ * @param device_ctx On success, a reference to the newly-created device context -+ * will be written here. The reference is owned by the caller -+ * and must be released with av_buffer_unref() when no longer -+ * needed. On failure, NULL will be written to this pointer. -+ * @param type The type of the device to create. -+ * @param device A type-specific string identifying the device to open. -+ * @param opts A dictionary of additional (type-specific) options to use in -+ * opening the device. The dictionary remains owned by the caller. -+ * @param flags currently unused -+ * -+ * @return 0 on success, a negative AVERROR code on failure. -+ */ -+int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type, -+ const char *device, AVDictionary *opts, int flags); -+ -+/** -+ * Create a new device of the specified type from an existing device. -+ * -+ * If the source device is a device of the target type or was originally -+ * derived from such a device (possibly through one or more intermediate -+ * devices of other types), then this will return a reference to the -+ * existing device of the same type as is requested. -+ * -+ * Otherwise, it will attempt to derive a new device from the given source -+ * device. If direct derivation to the new type is not implemented, it will -+ * attempt the same derivation from each ancestor of the source device in -+ * turn looking for an implemented derivation method. -+ * -+ * @param dst_ctx On success, a reference to the newly-created -+ * AVHWDeviceContext. -+ * @param type The type of the new device to create. -+ * @param src_ctx A reference to an existing AVHWDeviceContext which will be -+ * used to create the new device. -+ * @param flags Currently unused; should be set to zero. -+ * @return Zero on success, a negative AVERROR code on failure. -+ */ -+int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx, -+ enum AVHWDeviceType type, -+ AVBufferRef *src_ctx, int flags); -+ -+ -+/** -+ * Allocate an AVHWFramesContext tied to a given device context. -+ * -+ * @param device_ctx a reference to a AVHWDeviceContext. This function will make -+ * a new reference for internal use, the one passed to the -+ * function remains owned by the caller. -+ * @return a reference to the newly created AVHWFramesContext on success or NULL -+ * on failure. -+ */ -+AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx); -+ -+/** -+ * Finalize the context before use. This function must be called after the -+ * context is filled with all the required information and before it is attached -+ * to any frames. -+ * -+ * @param ref a reference to the AVHWFramesContext -+ * @return 0 on success, a negative AVERROR code on failure -+ */ -+int av_hwframe_ctx_init(AVBufferRef *ref); -+ -+/** -+ * Allocate a new frame attached to the given AVHWFramesContext. -+ * -+ * @param hwframe_ctx a reference to an AVHWFramesContext -+ * @param frame an empty (freshly allocated or unreffed) frame to be filled with -+ * newly allocated buffers. -+ * @param flags currently unused, should be set to zero -+ * @return 0 on success, a negative AVERROR code on failure -+ */ -+int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags); -+ -+/** -+ * Copy data to or from a hw surface. At least one of dst/src must have an -+ * AVHWFramesContext attached. -+ * -+ * If src has an AVHWFramesContext attached, then the format of dst (if set) -+ * must use one of the formats returned by av_hwframe_transfer_get_formats(src, -+ * AV_HWFRAME_TRANSFER_DIRECTION_FROM). -+ * If dst has an AVHWFramesContext attached, then the format of src must use one -+ * of the formats returned by av_hwframe_transfer_get_formats(dst, -+ * AV_HWFRAME_TRANSFER_DIRECTION_TO) -+ * -+ * dst may be "clean" (i.e. with data/buf pointers unset), in which case the -+ * data buffers will be allocated by this function using av_frame_get_buffer(). -+ * If dst->format is set, then this format will be used, otherwise (when -+ * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen. -+ * -+ * The two frames must have matching allocated dimensions (i.e. equal to -+ * AVHWFramesContext.width/height), since not all device types support -+ * transferring a sub-rectangle of the whole surface. The display dimensions -+ * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but -+ * also have to be equal for both frames. When the display dimensions are -+ * smaller than the allocated dimensions, the content of the padding in the -+ * destination frame is unspecified. -+ * -+ * @param dst the destination frame. dst is not touched on failure. -+ * @param src the source frame. -+ * @param flags currently unused, should be set to zero -+ * @return 0 on success, a negative AVERROR error code on failure. -+ */ -+int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags); -+ -+enum AVHWFrameTransferDirection { -+ /** -+ * Transfer the data from the queried hw frame. -+ */ -+ AV_HWFRAME_TRANSFER_DIRECTION_FROM, -+ -+ /** -+ * Transfer the data to the queried hw frame. -+ */ -+ AV_HWFRAME_TRANSFER_DIRECTION_TO, -+}; -+ -+/** -+ * Get a list of possible source or target formats usable in -+ * av_hwframe_transfer_data(). -+ * -+ * @param hwframe_ctx the frame context to obtain the information for -+ * @param dir the direction of the transfer -+ * @param formats the pointer to the output format list will be written here. -+ * The list is terminated with AV_PIX_FMT_NONE and must be freed -+ * by the caller when no longer needed using av_free(). -+ * If this function returns successfully, the format list will -+ * have at least one item (not counting the terminator). -+ * On failure, the contents of this pointer are unspecified. -+ * @param flags currently unused, should be set to zero -+ * @return 0 on success, a negative AVERROR code on failure. -+ */ -+int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx, -+ enum AVHWFrameTransferDirection dir, -+ enum AVPixelFormat **formats, int flags); -+ -+ -+/** -+ * This struct describes the constraints on hardware frames attached to -+ * a given device with a hardware-specific configuration. This is returned -+ * by av_hwdevice_get_hwframe_constraints() and must be freed by -+ * av_hwframe_constraints_free() after use. -+ */ -+typedef struct AVHWFramesConstraints { -+ /** -+ * A list of possible values for format in the hw_frames_ctx, -+ * terminated by AV_PIX_FMT_NONE. This member will always be filled. -+ */ -+ enum AVPixelFormat *valid_hw_formats; -+ -+ /** -+ * A list of possible values for sw_format in the hw_frames_ctx, -+ * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is -+ * not known. -+ */ -+ enum AVPixelFormat *valid_sw_formats; -+ -+ /** -+ * The minimum size of frames in this hw_frames_ctx. -+ * (Zero if not known.) -+ */ -+ int min_width; -+ int min_height; -+ -+ /** -+ * The maximum size of frames in this hw_frames_ctx. -+ * (INT_MAX if not known / no limit.) -+ */ -+ int max_width; -+ int max_height; -+} AVHWFramesConstraints; -+ -+/** -+ * Allocate a HW-specific configuration structure for a given HW device. -+ * After use, the user must free all members as required by the specific -+ * hardware structure being used, then free the structure itself with -+ * av_free(). -+ * -+ * @param device_ctx a reference to the associated AVHWDeviceContext. -+ * @return The newly created HW-specific configuration structure on -+ * success or NULL on failure. -+ */ -+void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx); -+ -+/** -+ * Get the constraints on HW frames given a device and the HW-specific -+ * configuration to be used with that device. If no HW-specific -+ * configuration is provided, returns the maximum possible capabilities -+ * of the device. -+ * -+ * @param ref a reference to the associated AVHWDeviceContext. -+ * @param hwconfig a filled HW-specific configuration structure, or NULL -+ * to return the maximum possible capabilities of the device. -+ * @return AVHWFramesConstraints structure describing the constraints -+ * on the device, or NULL if not available. -+ */ -+AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref, -+ const void *hwconfig); -+ -+/** -+ * Free an AVHWFrameConstraints structure. -+ * -+ * @param constraints The (filled or unfilled) AVHWFrameConstraints structure. -+ */ -+void av_hwframe_constraints_free(AVHWFramesConstraints **constraints); -+ -+ -+/** -+ * Flags to apply to frame mappings. -+ */ -+enum { -+ /** -+ * The mapping must be readable. -+ */ -+ AV_HWFRAME_MAP_READ = 1 << 0, -+ /** -+ * The mapping must be writeable. -+ */ -+ AV_HWFRAME_MAP_WRITE = 1 << 1, -+ /** -+ * The mapped frame will be overwritten completely in subsequent -+ * operations, so the current frame data need not be loaded. Any values -+ * which are not overwritten are unspecified. -+ */ -+ AV_HWFRAME_MAP_OVERWRITE = 1 << 2, -+ /** -+ * The mapping must be direct. That is, there must not be any copying in -+ * the map or unmap steps. Note that performance of direct mappings may -+ * be much lower than normal memory. -+ */ -+ AV_HWFRAME_MAP_DIRECT = 1 << 3, -+}; -+ -+/** -+ * Map a hardware frame. -+ * -+ * This has a number of different possible effects, depending on the format -+ * and origin of the src and dst frames. On input, src should be a usable -+ * frame with valid buffers and dst should be blank (typically as just created -+ * by av_frame_alloc()). src should have an associated hwframe context, and -+ * dst may optionally have a format and associated hwframe context. -+ * -+ * If src was created by mapping a frame from the hwframe context of dst, -+ * then this function undoes the mapping - dst is replaced by a reference to -+ * the frame that src was originally mapped from. -+ * -+ * If both src and dst have an associated hwframe context, then this function -+ * attempts to map the src frame from its hardware context to that of dst and -+ * then fill dst with appropriate data to be usable there. This will only be -+ * possible if the hwframe contexts and associated devices are compatible - -+ * given compatible devices, av_hwframe_ctx_create_derived() can be used to -+ * create a hwframe context for dst in which mapping should be possible. -+ * -+ * If src has a hwframe context but dst does not, then the src frame is -+ * mapped to normal memory and should thereafter be usable as a normal frame. -+ * If the format is set on dst, then the mapping will attempt to create dst -+ * with that format and fail if it is not possible. If format is unset (is -+ * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate -+ * format to use is (probably the sw_format of the src hwframe context). -+ * -+ * A return value of AVERROR(ENOSYS) indicates that the mapping is not -+ * possible with the given arguments and hwframe setup, while other return -+ * values indicate that it failed somehow. -+ * -+ * @param dst Destination frame, to contain the mapping. -+ * @param src Source frame, to be mapped. -+ * @param flags Some combination of AV_HWFRAME_MAP_* flags. -+ * @return Zero on success, negative AVERROR code on failure. -+ */ -+int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags); -+ -+ -+/** -+ * Create and initialise an AVHWFramesContext as a mapping of another existing -+ * AVHWFramesContext on a different device. -+ * -+ * av_hwframe_ctx_init() should not be called after this. -+ * -+ * @param derived_frame_ctx On success, a reference to the newly created -+ * AVHWFramesContext. -+ * @param derived_device_ctx A reference to the device to create the new -+ * AVHWFramesContext on. -+ * @param source_frame_ctx A reference to an existing AVHWFramesContext -+ * which will be mapped to the derived context. -+ * @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the -+ * mapping parameters to apply to frames which are allocated -+ * in the derived device. -+ * @return Zero on success, negative AVERROR code on failure. -+ */ -+int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx, -+ enum AVPixelFormat format, -+ AVBufferRef *derived_device_ctx, -+ AVBufferRef *source_frame_ctx, -+ int flags); -+ -+#endif /* AVUTIL_HWCONTEXT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/intfloat.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/intfloat.h -new file mode 100644 -index 000000000000..fe3d7ec4a5b6 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/intfloat.h -@@ -0,0 +1,77 @@ -+/* -+ * Copyright (c) 2011 Mans Rullgard -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_INTFLOAT_H -+#define AVUTIL_INTFLOAT_H -+ -+#include -+#include "attributes.h" -+ -+union av_intfloat32 { -+ uint32_t i; -+ float f; -+}; -+ -+union av_intfloat64 { -+ uint64_t i; -+ double f; -+}; -+ -+/** -+ * Reinterpret a 32-bit integer as a float. -+ */ -+static av_always_inline float av_int2float(uint32_t i) -+{ -+ union av_intfloat32 v; -+ v.i = i; -+ return v.f; -+} -+ -+/** -+ * Reinterpret a float as a 32-bit integer. -+ */ -+static av_always_inline uint32_t av_float2int(float f) -+{ -+ union av_intfloat32 v; -+ v.f = f; -+ return v.i; -+} -+ -+/** -+ * Reinterpret a 64-bit integer as a double. -+ */ -+static av_always_inline double av_int2double(uint64_t i) -+{ -+ union av_intfloat64 v; -+ v.i = i; -+ return v.f; -+} -+ -+/** -+ * Reinterpret a double as a 64-bit integer. -+ */ -+static av_always_inline uint64_t av_double2int(double f) -+{ -+ union av_intfloat64 v; -+ v.f = f; -+ return v.i; -+} -+ -+#endif /* AVUTIL_INTFLOAT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/log.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/log.h -new file mode 100644 -index 000000000000..d9554e609d40 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/log.h -@@ -0,0 +1,362 @@ -+/* -+ * copyright (c) 2006 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_LOG_H -+#define AVUTIL_LOG_H -+ -+#include -+#include "avutil.h" -+#include "attributes.h" -+#include "version.h" -+ -+typedef enum { -+ AV_CLASS_CATEGORY_NA = 0, -+ AV_CLASS_CATEGORY_INPUT, -+ AV_CLASS_CATEGORY_OUTPUT, -+ AV_CLASS_CATEGORY_MUXER, -+ AV_CLASS_CATEGORY_DEMUXER, -+ AV_CLASS_CATEGORY_ENCODER, -+ AV_CLASS_CATEGORY_DECODER, -+ AV_CLASS_CATEGORY_FILTER, -+ AV_CLASS_CATEGORY_BITSTREAM_FILTER, -+ AV_CLASS_CATEGORY_SWSCALER, -+ AV_CLASS_CATEGORY_SWRESAMPLER, -+ AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT = 40, -+ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT, -+ AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT, -+ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT, -+ AV_CLASS_CATEGORY_DEVICE_OUTPUT, -+ AV_CLASS_CATEGORY_DEVICE_INPUT, -+ AV_CLASS_CATEGORY_NB ///< not part of ABI/API -+}AVClassCategory; -+ -+#define AV_IS_INPUT_DEVICE(category) \ -+ (((category) == AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT) || \ -+ ((category) == AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT) || \ -+ ((category) == AV_CLASS_CATEGORY_DEVICE_INPUT)) -+ -+#define AV_IS_OUTPUT_DEVICE(category) \ -+ (((category) == AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT) || \ -+ ((category) == AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT) || \ -+ ((category) == AV_CLASS_CATEGORY_DEVICE_OUTPUT)) -+ -+struct AVOptionRanges; -+ -+/** -+ * Describe the class of an AVClass context structure. That is an -+ * arbitrary struct of which the first field is a pointer to an -+ * AVClass struct (e.g. AVCodecContext, AVFormatContext etc.). -+ */ -+typedef struct AVClass { -+ /** -+ * The name of the class; usually it is the same name as the -+ * context structure type to which the AVClass is associated. -+ */ -+ const char* class_name; -+ -+ /** -+ * A pointer to a function which returns the name of a context -+ * instance ctx associated with the class. -+ */ -+ const char* (*item_name)(void* ctx); -+ -+ /** -+ * a pointer to the first option specified in the class if any or NULL -+ * -+ * @see av_set_default_options() -+ */ -+ const struct AVOption *option; -+ -+ /** -+ * LIBAVUTIL_VERSION with which this structure was created. -+ * This is used to allow fields to be added without requiring major -+ * version bumps everywhere. -+ */ -+ -+ int version; -+ -+ /** -+ * Offset in the structure where log_level_offset is stored. -+ * 0 means there is no such variable -+ */ -+ int log_level_offset_offset; -+ -+ /** -+ * Offset in the structure where a pointer to the parent context for -+ * logging is stored. For example a decoder could pass its AVCodecContext -+ * to eval as such a parent context, which an av_log() implementation -+ * could then leverage to display the parent context. -+ * The offset can be NULL. -+ */ -+ int parent_log_context_offset; -+ -+ /** -+ * Return next AVOptions-enabled child or NULL -+ */ -+ void* (*child_next)(void *obj, void *prev); -+ -+ /** -+ * Return an AVClass corresponding to the next potential -+ * AVOptions-enabled child. -+ * -+ * The difference between child_next and this is that -+ * child_next iterates over _already existing_ objects, while -+ * child_class_next iterates over _all possible_ children. -+ */ -+ const struct AVClass* (*child_class_next)(const struct AVClass *prev); -+ -+ /** -+ * Category used for visualization (like color) -+ * This is only set if the category is equal for all objects using this class. -+ * available since version (51 << 16 | 56 << 8 | 100) -+ */ -+ AVClassCategory category; -+ -+ /** -+ * Callback to return the category. -+ * available since version (51 << 16 | 59 << 8 | 100) -+ */ -+ AVClassCategory (*get_category)(void* ctx); -+ -+ /** -+ * Callback to return the supported/allowed ranges. -+ * available since version (52.12) -+ */ -+ int (*query_ranges)(struct AVOptionRanges **, void *obj, const char *key, int flags); -+} AVClass; -+ -+/** -+ * @addtogroup lavu_log -+ * -+ * @{ -+ * -+ * @defgroup lavu_log_constants Logging Constants -+ * -+ * @{ -+ */ -+ -+/** -+ * Print no output. -+ */ -+#define AV_LOG_QUIET -8 -+ -+/** -+ * Something went really wrong and we will crash now. -+ */ -+#define AV_LOG_PANIC 0 -+ -+/** -+ * Something went wrong and recovery is not possible. -+ * For example, no header was found for a format which depends -+ * on headers or an illegal combination of parameters is used. -+ */ -+#define AV_LOG_FATAL 8 -+ -+/** -+ * Something went wrong and cannot losslessly be recovered. -+ * However, not all future data is affected. -+ */ -+#define AV_LOG_ERROR 16 -+ -+/** -+ * Something somehow does not look correct. This may or may not -+ * lead to problems. An example would be the use of '-vstrict -2'. -+ */ -+#define AV_LOG_WARNING 24 -+ -+/** -+ * Standard information. -+ */ -+#define AV_LOG_INFO 32 -+ -+/** -+ * Detailed information. -+ */ -+#define AV_LOG_VERBOSE 40 -+ -+/** -+ * Stuff which is only useful for libav* developers. -+ */ -+#define AV_LOG_DEBUG 48 -+ -+/** -+ * Extremely verbose debugging, useful for libav* development. -+ */ -+#define AV_LOG_TRACE 56 -+ -+#define AV_LOG_MAX_OFFSET (AV_LOG_TRACE - AV_LOG_QUIET) -+ -+/** -+ * @} -+ */ -+ -+/** -+ * Sets additional colors for extended debugging sessions. -+ * @code -+ av_log(ctx, AV_LOG_DEBUG|AV_LOG_C(134), "Message in purple\n"); -+ @endcode -+ * Requires 256color terminal support. Uses outside debugging is not -+ * recommended. -+ */ -+#define AV_LOG_C(x) ((x) << 8) -+ -+/** -+ * Send the specified message to the log if the level is less than or equal -+ * to the current av_log_level. By default, all logging messages are sent to -+ * stderr. This behavior can be altered by setting a different logging callback -+ * function. -+ * @see av_log_set_callback -+ * -+ * @param avcl A pointer to an arbitrary struct of which the first field is a -+ * pointer to an AVClass struct or NULL if general log. -+ * @param level The importance level of the message expressed using a @ref -+ * lavu_log_constants "Logging Constant". -+ * @param fmt The format string (printf-compatible) that specifies how -+ * subsequent arguments are converted to output. -+ */ -+void av_log(void *avcl, int level, const char *fmt, ...) av_printf_format(3, 4); -+ -+ -+/** -+ * Send the specified message to the log if the level is less than or equal -+ * to the current av_log_level. By default, all logging messages are sent to -+ * stderr. This behavior can be altered by setting a different logging callback -+ * function. -+ * @see av_log_set_callback -+ * -+ * @param avcl A pointer to an arbitrary struct of which the first field is a -+ * pointer to an AVClass struct. -+ * @param level The importance level of the message expressed using a @ref -+ * lavu_log_constants "Logging Constant". -+ * @param fmt The format string (printf-compatible) that specifies how -+ * subsequent arguments are converted to output. -+ * @param vl The arguments referenced by the format string. -+ */ -+void av_vlog(void *avcl, int level, const char *fmt, va_list vl); -+ -+/** -+ * Get the current log level -+ * -+ * @see lavu_log_constants -+ * -+ * @return Current log level -+ */ -+int av_log_get_level(void); -+ -+/** -+ * Set the log level -+ * -+ * @see lavu_log_constants -+ * -+ * @param level Logging level -+ */ -+void av_log_set_level(int level); -+ -+/** -+ * Set the logging callback -+ * -+ * @note The callback must be thread safe, even if the application does not use -+ * threads itself as some codecs are multithreaded. -+ * -+ * @see av_log_default_callback -+ * -+ * @param callback A logging function with a compatible signature. -+ */ -+void av_log_set_callback(void (*callback)(void*, int, const char*, va_list)); -+ -+/** -+ * Default logging callback -+ * -+ * It prints the message to stderr, optionally colorizing it. -+ * -+ * @param avcl A pointer to an arbitrary struct of which the first field is a -+ * pointer to an AVClass struct. -+ * @param level The importance level of the message expressed using a @ref -+ * lavu_log_constants "Logging Constant". -+ * @param fmt The format string (printf-compatible) that specifies how -+ * subsequent arguments are converted to output. -+ * @param vl The arguments referenced by the format string. -+ */ -+void av_log_default_callback(void *avcl, int level, const char *fmt, -+ va_list vl); -+ -+/** -+ * Return the context name -+ * -+ * @param ctx The AVClass context -+ * -+ * @return The AVClass class_name -+ */ -+const char* av_default_item_name(void* ctx); -+AVClassCategory av_default_get_category(void *ptr); -+ -+/** -+ * Format a line of log the same way as the default callback. -+ * @param line buffer to receive the formatted line -+ * @param line_size size of the buffer -+ * @param print_prefix used to store whether the prefix must be printed; -+ * must point to a persistent integer initially set to 1 -+ */ -+void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl, -+ char *line, int line_size, int *print_prefix); -+ -+/** -+ * Format a line of log the same way as the default callback. -+ * @param line buffer to receive the formatted line; -+ * may be NULL if line_size is 0 -+ * @param line_size size of the buffer; at most line_size-1 characters will -+ * be written to the buffer, plus one null terminator -+ * @param print_prefix used to store whether the prefix must be printed; -+ * must point to a persistent integer initially set to 1 -+ * @return Returns a negative value if an error occurred, otherwise returns -+ * the number of characters that would have been written for a -+ * sufficiently large buffer, not including the terminating null -+ * character. If the return value is not less than line_size, it means -+ * that the log message was truncated to fit the buffer. -+ */ -+int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl, -+ char *line, int line_size, int *print_prefix); -+ -+/** -+ * Skip repeated messages, this requires the user app to use av_log() instead of -+ * (f)printf as the 2 would otherwise interfere and lead to -+ * "Last message repeated x times" messages below (f)printf messages with some -+ * bad luck. -+ * Also to receive the last, "last repeated" line if any, the user app must -+ * call av_log(NULL, AV_LOG_QUIET, "%s", ""); at the end -+ */ -+#define AV_LOG_SKIP_REPEATED 1 -+ -+/** -+ * Include the log severity in messages originating from codecs. -+ * -+ * Results in messages such as: -+ * [rawvideo @ 0xDEADBEEF] [error] encode did not produce valid pts -+ */ -+#define AV_LOG_PRINT_LEVEL 2 -+ -+void av_log_set_flags(int arg); -+int av_log_get_flags(void); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_LOG_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/macros.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/macros.h -new file mode 100644 -index 000000000000..2007ee561987 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/macros.h -@@ -0,0 +1,50 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * @ingroup lavu -+ * Utility Preprocessor macros -+ */ -+ -+#ifndef AVUTIL_MACROS_H -+#define AVUTIL_MACROS_H -+ -+/** -+ * @addtogroup preproc_misc Preprocessor String Macros -+ * -+ * String manipulation macros -+ * -+ * @{ -+ */ -+ -+#define AV_STRINGIFY(s) AV_TOSTRING(s) -+#define AV_TOSTRING(s) #s -+ -+#define AV_GLUE(a, b) a ## b -+#define AV_JOIN(a, b) AV_GLUE(a, b) -+ -+/** -+ * @} -+ */ -+ -+#define AV_PRAGMA(s) _Pragma(#s) -+ -+#define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1)) -+ -+#endif /* AVUTIL_MACROS_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/mathematics.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/mathematics.h -new file mode 100644 -index 000000000000..54901800ba6a ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/mathematics.h -@@ -0,0 +1,242 @@ -+/* -+ * copyright (c) 2005-2012 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * @addtogroup lavu_math -+ * Mathematical utilities for working with timestamp and time base. -+ */ -+ -+#ifndef AVUTIL_MATHEMATICS_H -+#define AVUTIL_MATHEMATICS_H -+ -+#include -+#include -+#include "attributes.h" -+#include "rational.h" -+#include "intfloat.h" -+ -+#ifndef M_E -+#define M_E 2.7182818284590452354 /* e */ -+#endif -+#ifndef M_LN2 -+#define M_LN2 0.69314718055994530942 /* log_e 2 */ -+#endif -+#ifndef M_LN10 -+#define M_LN10 2.30258509299404568402 /* log_e 10 */ -+#endif -+#ifndef M_LOG2_10 -+#define M_LOG2_10 3.32192809488736234787 /* log_2 10 */ -+#endif -+#ifndef M_PHI -+#define M_PHI 1.61803398874989484820 /* phi / golden ratio */ -+#endif -+#ifndef M_PI -+#define M_PI 3.14159265358979323846 /* pi */ -+#endif -+#ifndef M_PI_2 -+#define M_PI_2 1.57079632679489661923 /* pi/2 */ -+#endif -+#ifndef M_SQRT1_2 -+#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ -+#endif -+#ifndef M_SQRT2 -+#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ -+#endif -+#ifndef NAN -+#define NAN av_int2float(0x7fc00000) -+#endif -+#ifndef INFINITY -+#define INFINITY av_int2float(0x7f800000) -+#endif -+ -+/** -+ * @addtogroup lavu_math -+ * -+ * @{ -+ */ -+ -+/** -+ * Rounding methods. -+ */ -+enum AVRounding { -+ AV_ROUND_ZERO = 0, ///< Round toward zero. -+ AV_ROUND_INF = 1, ///< Round away from zero. -+ AV_ROUND_DOWN = 2, ///< Round toward -infinity. -+ AV_ROUND_UP = 3, ///< Round toward +infinity. -+ AV_ROUND_NEAR_INF = 5, ///< Round to nearest and halfway cases away from zero. -+ /** -+ * Flag telling rescaling functions to pass `INT64_MIN`/`MAX` through -+ * unchanged, avoiding special cases for #AV_NOPTS_VALUE. -+ * -+ * Unlike other values of the enumeration AVRounding, this value is a -+ * bitmask that must be used in conjunction with another value of the -+ * enumeration through a bitwise OR, in order to set behavior for normal -+ * cases. -+ * -+ * @code{.c} -+ * av_rescale_rnd(3, 1, 2, AV_ROUND_UP | AV_ROUND_PASS_MINMAX); -+ * // Rescaling 3: -+ * // Calculating 3 * 1 / 2 -+ * // 3 / 2 is rounded up to 2 -+ * // => 2 -+ * -+ * av_rescale_rnd(AV_NOPTS_VALUE, 1, 2, AV_ROUND_UP | AV_ROUND_PASS_MINMAX); -+ * // Rescaling AV_NOPTS_VALUE: -+ * // AV_NOPTS_VALUE == INT64_MIN -+ * // AV_NOPTS_VALUE is passed through -+ * // => AV_NOPTS_VALUE -+ * @endcode -+ */ -+ AV_ROUND_PASS_MINMAX = 8192, -+}; -+ -+/** -+ * Compute the greatest common divisor of two integer operands. -+ * -+ * @param a,b Operands -+ * @return GCD of a and b up to sign; if a >= 0 and b >= 0, return value is >= 0; -+ * if a == 0 and b == 0, returns 0. -+ */ -+int64_t av_const av_gcd(int64_t a, int64_t b); -+ -+/** -+ * Rescale a 64-bit integer with rounding to nearest. -+ * -+ * The operation is mathematically equivalent to `a * b / c`, but writing that -+ * directly can overflow. -+ * -+ * This function is equivalent to av_rescale_rnd() with #AV_ROUND_NEAR_INF. -+ * -+ * @see av_rescale_rnd(), av_rescale_q(), av_rescale_q_rnd() -+ */ -+int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const; -+ -+/** -+ * Rescale a 64-bit integer with specified rounding. -+ * -+ * The operation is mathematically equivalent to `a * b / c`, but writing that -+ * directly can overflow, and does not support different rounding methods. -+ * -+ * @see av_rescale(), av_rescale_q(), av_rescale_q_rnd() -+ */ -+int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const; -+ -+/** -+ * Rescale a 64-bit integer by 2 rational numbers. -+ * -+ * The operation is mathematically equivalent to `a * bq / cq`. -+ * -+ * This function is equivalent to av_rescale_q_rnd() with #AV_ROUND_NEAR_INF. -+ * -+ * @see av_rescale(), av_rescale_rnd(), av_rescale_q_rnd() -+ */ -+int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const; -+ -+/** -+ * Rescale a 64-bit integer by 2 rational numbers with specified rounding. -+ * -+ * The operation is mathematically equivalent to `a * bq / cq`. -+ * -+ * @see av_rescale(), av_rescale_rnd(), av_rescale_q() -+ */ -+int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, -+ enum AVRounding rnd) av_const; -+ -+/** -+ * Compare two timestamps each in its own time base. -+ * -+ * @return One of the following values: -+ * - -1 if `ts_a` is before `ts_b` -+ * - 1 if `ts_a` is after `ts_b` -+ * - 0 if they represent the same position -+ * -+ * @warning -+ * The result of the function is undefined if one of the timestamps is outside -+ * the `int64_t` range when represented in the other's timebase. -+ */ -+int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b); -+ -+/** -+ * Compare the remainders of two integer operands divided by a common divisor. -+ * -+ * In other words, compare the least significant `log2(mod)` bits of integers -+ * `a` and `b`. -+ * -+ * @code{.c} -+ * av_compare_mod(0x11, 0x02, 0x10) < 0 // since 0x11 % 0x10 (0x1) < 0x02 % 0x10 (0x2) -+ * av_compare_mod(0x11, 0x02, 0x20) > 0 // since 0x11 % 0x20 (0x11) > 0x02 % 0x20 (0x02) -+ * @endcode -+ * -+ * @param a,b Operands -+ * @param mod Divisor; must be a power of 2 -+ * @return -+ * - a negative value if `a % mod < b % mod` -+ * - a positive value if `a % mod > b % mod` -+ * - zero if `a % mod == b % mod` -+ */ -+int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod); -+ -+/** -+ * Rescale a timestamp while preserving known durations. -+ * -+ * This function is designed to be called per audio packet to scale the input -+ * timestamp to a different time base. Compared to a simple av_rescale_q() -+ * call, this function is robust against possible inconsistent frame durations. -+ * -+ * The `last` parameter is a state variable that must be preserved for all -+ * subsequent calls for the same stream. For the first call, `*last` should be -+ * initialized to #AV_NOPTS_VALUE. -+ * -+ * @param[in] in_tb Input time base -+ * @param[in] in_ts Input timestamp -+ * @param[in] fs_tb Duration time base; typically this is finer-grained -+ * (greater) than `in_tb` and `out_tb` -+ * @param[in] duration Duration till the next call to this function (i.e. -+ * duration of the current packet/frame) -+ * @param[in,out] last Pointer to a timestamp expressed in terms of -+ * `fs_tb`, acting as a state variable -+ * @param[in] out_tb Output timebase -+ * @return Timestamp expressed in terms of `out_tb` -+ * -+ * @note In the context of this function, "duration" is in term of samples, not -+ * seconds. -+ */ -+int64_t av_rescale_delta(AVRational in_tb, int64_t in_ts, AVRational fs_tb, int duration, int64_t *last, AVRational out_tb); -+ -+/** -+ * Add a value to a timestamp. -+ * -+ * This function guarantees that when the same value is repeatly added that -+ * no accumulation of rounding errors occurs. -+ * -+ * @param[in] ts Input timestamp -+ * @param[in] ts_tb Input timestamp time base -+ * @param[in] inc Value to be added -+ * @param[in] inc_tb Time base of `inc` -+ */ -+int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc); -+ -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_MATHEMATICS_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/mem.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/mem.h -new file mode 100644 -index 000000000000..7e0b12a8a782 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/mem.h -@@ -0,0 +1,700 @@ -+/* -+ * copyright (c) 2006 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * @ingroup lavu_mem -+ * Memory handling functions -+ */ -+ -+#ifndef AVUTIL_MEM_H -+#define AVUTIL_MEM_H -+ -+#include -+#include -+ -+#include "attributes.h" -+#include "error.h" -+#include "avutil.h" -+ -+/** -+ * @addtogroup lavu_mem -+ * Utilities for manipulating memory. -+ * -+ * FFmpeg has several applications of memory that are not required of a typical -+ * program. For example, the computing-heavy components like video decoding and -+ * encoding can be sped up significantly through the use of aligned memory. -+ * -+ * However, for each of FFmpeg's applications of memory, there might not be a -+ * recognized or standardized API for that specific use. Memory alignment, for -+ * instance, varies wildly depending on operating systems, architectures, and -+ * compilers. Hence, this component of @ref libavutil is created to make -+ * dealing with memory consistently possible on all platforms. -+ * -+ * @{ -+ * -+ * @defgroup lavu_mem_macros Alignment Macros -+ * Helper macros for declaring aligned variables. -+ * @{ -+ */ -+ -+/** -+ * @def DECLARE_ALIGNED(n,t,v) -+ * Declare a variable that is aligned in memory. -+ * -+ * @code{.c} -+ * DECLARE_ALIGNED(16, uint16_t, aligned_int) = 42; -+ * DECLARE_ALIGNED(32, uint8_t, aligned_array)[128]; -+ * -+ * // The default-alignment equivalent would be -+ * uint16_t aligned_int = 42; -+ * uint8_t aligned_array[128]; -+ * @endcode -+ * -+ * @param n Minimum alignment in bytes -+ * @param t Type of the variable (or array element) -+ * @param v Name of the variable -+ */ -+ -+/** -+ * @def DECLARE_ASM_ALIGNED(n,t,v) -+ * Declare an aligned variable appropriate for use in inline assembly code. -+ * -+ * @code{.c} -+ * DECLARE_ASM_ALIGNED(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008); -+ * @endcode -+ * -+ * @param n Minimum alignment in bytes -+ * @param t Type of the variable (or array element) -+ * @param v Name of the variable -+ */ -+ -+/** -+ * @def DECLARE_ASM_CONST(n,t,v) -+ * Declare a static constant aligned variable appropriate for use in inline -+ * assembly code. -+ * -+ * @code{.c} -+ * DECLARE_ASM_CONST(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008); -+ * @endcode -+ * -+ * @param n Minimum alignment in bytes -+ * @param t Type of the variable (or array element) -+ * @param v Name of the variable -+ */ -+ -+#if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C) -+ #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v -+ #define DECLARE_ASM_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v -+ #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v -+#elif defined(__DJGPP__) -+ #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (FFMIN(n, 16)))) v -+ #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v -+ #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v -+#elif defined(__GNUC__) || defined(__clang__) -+ #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v -+ #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (n))) v -+ #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v -+#elif defined(_MSC_VER) -+ #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v -+ #define DECLARE_ASM_ALIGNED(n,t,v) __declspec(align(n)) t v -+ #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v -+#else -+ #define DECLARE_ALIGNED(n,t,v) t v -+ #define DECLARE_ASM_ALIGNED(n,t,v) t v -+ #define DECLARE_ASM_CONST(n,t,v) static const t v -+#endif -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @defgroup lavu_mem_attrs Function Attributes -+ * Function attributes applicable to memory handling functions. -+ * -+ * These function attributes can help compilers emit more useful warnings, or -+ * generate better code. -+ * @{ -+ */ -+ -+/** -+ * @def av_malloc_attrib -+ * Function attribute denoting a malloc-like function. -+ * -+ * @see Function attribute `malloc` in GCC's documentation -+ */ -+ -+#if AV_GCC_VERSION_AT_LEAST(3,1) -+ #define av_malloc_attrib __attribute__((__malloc__)) -+#else -+ #define av_malloc_attrib -+#endif -+ -+/** -+ * @def av_alloc_size(...) -+ * Function attribute used on a function that allocates memory, whose size is -+ * given by the specified parameter(s). -+ * -+ * @code{.c} -+ * void *av_malloc(size_t size) av_alloc_size(1); -+ * void *av_calloc(size_t nmemb, size_t size) av_alloc_size(1, 2); -+ * @endcode -+ * -+ * @param ... One or two parameter indexes, separated by a comma -+ * -+ * @see Function attribute `alloc_size` in GCC's documentation -+ */ -+ -+#if AV_GCC_VERSION_AT_LEAST(4,3) -+ #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) -+#else -+ #define av_alloc_size(...) -+#endif -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @defgroup lavu_mem_funcs Heap Management -+ * Functions responsible for allocating, freeing, and copying memory. -+ * -+ * All memory allocation functions have a built-in upper limit of `INT_MAX` -+ * bytes. This may be changed with av_max_alloc(), although exercise extreme -+ * caution when doing so. -+ * -+ * @{ -+ */ -+ -+/** -+ * Allocate a memory block with alignment suitable for all memory accesses -+ * (including vectors if available on the CPU). -+ * -+ * @param size Size in bytes for the memory block to be allocated -+ * @return Pointer to the allocated block, or `NULL` if the block cannot -+ * be allocated -+ * @see av_mallocz() -+ */ -+void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1); -+ -+/** -+ * Allocate a memory block with alignment suitable for all memory accesses -+ * (including vectors if available on the CPU) and zero all the bytes of the -+ * block. -+ * -+ * @param size Size in bytes for the memory block to be allocated -+ * @return Pointer to the allocated block, or `NULL` if it cannot be allocated -+ * @see av_malloc() -+ */ -+void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); -+ -+/** -+ * Allocate a memory block for an array with av_malloc(). -+ * -+ * The allocated memory will have size `size * nmemb` bytes. -+ * -+ * @param nmemb Number of element -+ * @param size Size of a single element -+ * @return Pointer to the allocated block, or `NULL` if the block cannot -+ * be allocated -+ * @see av_malloc() -+ */ -+av_alloc_size(1, 2) void *av_malloc_array(size_t nmemb, size_t size); -+ -+/** -+ * Allocate a memory block for an array with av_mallocz(). -+ * -+ * The allocated memory will have size `size * nmemb` bytes. -+ * -+ * @param nmemb Number of elements -+ * @param size Size of the single element -+ * @return Pointer to the allocated block, or `NULL` if the block cannot -+ * be allocated -+ * -+ * @see av_mallocz() -+ * @see av_malloc_array() -+ */ -+av_alloc_size(1, 2) void *av_mallocz_array(size_t nmemb, size_t size); -+ -+/** -+ * Non-inlined equivalent of av_mallocz_array(). -+ * -+ * Created for symmetry with the calloc() C function. -+ */ -+void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib; -+ -+/** -+ * Allocate, reallocate, or free a block of memory. -+ * -+ * If `ptr` is `NULL` and `size` > 0, allocate a new block. If `size` is -+ * zero, free the memory block pointed to by `ptr`. Otherwise, expand or -+ * shrink that block of memory according to `size`. -+ * -+ * @param ptr Pointer to a memory block already allocated with -+ * av_realloc() or `NULL` -+ * @param size Size in bytes of the memory block to be allocated or -+ * reallocated -+ * -+ * @return Pointer to a newly-reallocated block or `NULL` if the block -+ * cannot be reallocated or the function is used to free the memory block -+ * -+ * @warning Unlike av_malloc(), the returned pointer is not guaranteed to be -+ * correctly aligned. -+ * @see av_fast_realloc() -+ * @see av_reallocp() -+ */ -+void *av_realloc(void *ptr, size_t size) av_alloc_size(2); -+ -+/** -+ * Allocate, reallocate, or free a block of memory through a pointer to a -+ * pointer. -+ * -+ * If `*ptr` is `NULL` and `size` > 0, allocate a new block. If `size` is -+ * zero, free the memory block pointed to by `*ptr`. Otherwise, expand or -+ * shrink that block of memory according to `size`. -+ * -+ * @param[in,out] ptr Pointer to a pointer to a memory block already allocated -+ * with av_realloc(), or a pointer to `NULL`. The pointer -+ * is updated on success, or freed on failure. -+ * @param[in] size Size in bytes for the memory block to be allocated or -+ * reallocated -+ * -+ * @return Zero on success, an AVERROR error code on failure -+ * -+ * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be -+ * correctly aligned. -+ */ -+av_warn_unused_result -+int av_reallocp(void *ptr, size_t size); -+ -+/** -+ * Allocate, reallocate, or free a block of memory. -+ * -+ * This function does the same thing as av_realloc(), except: -+ * - It takes two size arguments and allocates `nelem * elsize` bytes, -+ * after checking the result of the multiplication for integer overflow. -+ * - It frees the input block in case of failure, thus avoiding the memory -+ * leak with the classic -+ * @code{.c} -+ * buf = realloc(buf); -+ * if (!buf) -+ * return -1; -+ * @endcode -+ * pattern. -+ */ -+void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); -+ -+/** -+ * Allocate, reallocate, or free an array. -+ * -+ * If `ptr` is `NULL` and `nmemb` > 0, allocate a new block. If -+ * `nmemb` is zero, free the memory block pointed to by `ptr`. -+ * -+ * @param ptr Pointer to a memory block already allocated with -+ * av_realloc() or `NULL` -+ * @param nmemb Number of elements in the array -+ * @param size Size of the single element of the array -+ * -+ * @return Pointer to a newly-reallocated block or NULL if the block -+ * cannot be reallocated or the function is used to free the memory block -+ * -+ * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be -+ * correctly aligned. -+ * @see av_reallocp_array() -+ */ -+av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size); -+ -+/** -+ * Allocate, reallocate, or free an array through a pointer to a pointer. -+ * -+ * If `*ptr` is `NULL` and `nmemb` > 0, allocate a new block. If `nmemb` is -+ * zero, free the memory block pointed to by `*ptr`. -+ * -+ * @param[in,out] ptr Pointer to a pointer to a memory block already -+ * allocated with av_realloc(), or a pointer to `NULL`. -+ * The pointer is updated on success, or freed on failure. -+ * @param[in] nmemb Number of elements -+ * @param[in] size Size of the single element -+ * -+ * @return Zero on success, an AVERROR error code on failure -+ * -+ * @warning Unlike av_malloc(), the allocated memory is not guaranteed to be -+ * correctly aligned. -+ */ -+av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size); -+ -+/** -+ * Reallocate the given buffer if it is not large enough, otherwise do nothing. -+ * -+ * If the given buffer is `NULL`, then a new uninitialized buffer is allocated. -+ * -+ * If the given buffer is not large enough, and reallocation fails, `NULL` is -+ * returned and `*size` is set to 0, but the original buffer is not changed or -+ * freed. -+ * -+ * A typical use pattern follows: -+ * -+ * @code{.c} -+ * uint8_t *buf = ...; -+ * uint8_t *new_buf = av_fast_realloc(buf, ¤t_size, size_needed); -+ * if (!new_buf) { -+ * // Allocation failed; clean up original buffer -+ * av_freep(&buf); -+ * return AVERROR(ENOMEM); -+ * } -+ * @endcode -+ * -+ * @param[in,out] ptr Already allocated buffer, or `NULL` -+ * @param[in,out] size Pointer to current size of buffer `ptr`. `*size` is -+ * changed to `min_size` in case of success or 0 in -+ * case of failure -+ * @param[in] min_size New size of buffer `ptr` -+ * @return `ptr` if the buffer is large enough, a pointer to newly reallocated -+ * buffer if the buffer was not large enough, or `NULL` in case of -+ * error -+ * @see av_realloc() -+ * @see av_fast_malloc() -+ */ -+void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); -+ -+/** -+ * Allocate a buffer, reusing the given one if large enough. -+ * -+ * Contrary to av_fast_realloc(), the current buffer contents might not be -+ * preserved and on error the old buffer is freed, thus no special handling to -+ * avoid memleaks is necessary. -+ * -+ * `*ptr` is allowed to be `NULL`, in which case allocation always happens if -+ * `size_needed` is greater than 0. -+ * -+ * @code{.c} -+ * uint8_t *buf = ...; -+ * av_fast_malloc(&buf, ¤t_size, size_needed); -+ * if (!buf) { -+ * // Allocation failed; buf already freed -+ * return AVERROR(ENOMEM); -+ * } -+ * @endcode -+ * -+ * @param[in,out] ptr Pointer to pointer to an already allocated buffer. -+ * `*ptr` will be overwritten with pointer to new -+ * buffer on success or `NULL` on failure -+ * @param[in,out] size Pointer to current size of buffer `*ptr`. `*size` is -+ * changed to `min_size` in case of success or 0 in -+ * case of failure -+ * @param[in] min_size New size of buffer `*ptr` -+ * @see av_realloc() -+ * @see av_fast_mallocz() -+ */ -+void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size); -+ -+/** -+ * Allocate and clear a buffer, reusing the given one if large enough. -+ * -+ * Like av_fast_malloc(), but all newly allocated space is initially cleared. -+ * Reused buffer is not cleared. -+ * -+ * `*ptr` is allowed to be `NULL`, in which case allocation always happens if -+ * `size_needed` is greater than 0. -+ * -+ * @param[in,out] ptr Pointer to pointer to an already allocated buffer. -+ * `*ptr` will be overwritten with pointer to new -+ * buffer on success or `NULL` on failure -+ * @param[in,out] size Pointer to current size of buffer `*ptr`. `*size` is -+ * changed to `min_size` in case of success or 0 in -+ * case of failure -+ * @param[in] min_size New size of buffer `*ptr` -+ * @see av_fast_malloc() -+ */ -+void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size); -+ -+/** -+ * Free a memory block which has been allocated with a function of av_malloc() -+ * or av_realloc() family. -+ * -+ * @param ptr Pointer to the memory block which should be freed. -+ * -+ * @note `ptr = NULL` is explicitly allowed. -+ * @note It is recommended that you use av_freep() instead, to prevent leaving -+ * behind dangling pointers. -+ * @see av_freep() -+ */ -+void av_free(void *ptr); -+ -+/** -+ * Free a memory block which has been allocated with a function of av_malloc() -+ * or av_realloc() family, and set the pointer pointing to it to `NULL`. -+ * -+ * @code{.c} -+ * uint8_t *buf = av_malloc(16); -+ * av_free(buf); -+ * // buf now contains a dangling pointer to freed memory, and accidental -+ * // dereference of buf will result in a use-after-free, which may be a -+ * // security risk. -+ * -+ * uint8_t *buf = av_malloc(16); -+ * av_freep(&buf); -+ * // buf is now NULL, and accidental dereference will only result in a -+ * // NULL-pointer dereference. -+ * @endcode -+ * -+ * @param ptr Pointer to the pointer to the memory block which should be freed -+ * @note `*ptr = NULL` is safe and leads to no action. -+ * @see av_free() -+ */ -+void av_freep(void *ptr); -+ -+/** -+ * Duplicate a string. -+ * -+ * @param s String to be duplicated -+ * @return Pointer to a newly-allocated string containing a -+ * copy of `s` or `NULL` if the string cannot be allocated -+ * @see av_strndup() -+ */ -+char *av_strdup(const char *s) av_malloc_attrib; -+ -+/** -+ * Duplicate a substring of a string. -+ * -+ * @param s String to be duplicated -+ * @param len Maximum length of the resulting string (not counting the -+ * terminating byte) -+ * @return Pointer to a newly-allocated string containing a -+ * substring of `s` or `NULL` if the string cannot be allocated -+ */ -+char *av_strndup(const char *s, size_t len) av_malloc_attrib; -+ -+/** -+ * Duplicate a buffer with av_malloc(). -+ * -+ * @param p Buffer to be duplicated -+ * @param size Size in bytes of the buffer copied -+ * @return Pointer to a newly allocated buffer containing a -+ * copy of `p` or `NULL` if the buffer cannot be allocated -+ */ -+void *av_memdup(const void *p, size_t size); -+ -+/** -+ * Overlapping memcpy() implementation. -+ * -+ * @param dst Destination buffer -+ * @param back Number of bytes back to start copying (i.e. the initial size of -+ * the overlapping window); must be > 0 -+ * @param cnt Number of bytes to copy; must be >= 0 -+ * -+ * @note `cnt > back` is valid, this will copy the bytes we just copied, -+ * thus creating a repeating pattern with a period length of `back`. -+ */ -+void av_memcpy_backptr(uint8_t *dst, int back, int cnt); -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @defgroup lavu_mem_dynarray Dynamic Array -+ * -+ * Utilities to make an array grow when needed. -+ * -+ * Sometimes, the programmer would want to have an array that can grow when -+ * needed. The libavutil dynamic array utilities fill that need. -+ * -+ * libavutil supports two systems of appending elements onto a dynamically -+ * allocated array, the first one storing the pointer to the value in the -+ * array, and the second storing the value directly. In both systems, the -+ * caller is responsible for maintaining a variable containing the length of -+ * the array, as well as freeing of the array after use. -+ * -+ * The first system stores pointers to values in a block of dynamically -+ * allocated memory. Since only pointers are stored, the function does not need -+ * to know the size of the type. Both av_dynarray_add() and -+ * av_dynarray_add_nofree() implement this system. -+ * -+ * @code -+ * type **array = NULL; //< an array of pointers to values -+ * int nb = 0; //< a variable to keep track of the length of the array -+ * -+ * type to_be_added = ...; -+ * type to_be_added2 = ...; -+ * -+ * av_dynarray_add(&array, &nb, &to_be_added); -+ * if (nb == 0) -+ * return AVERROR(ENOMEM); -+ * -+ * av_dynarray_add(&array, &nb, &to_be_added2); -+ * if (nb == 0) -+ * return AVERROR(ENOMEM); -+ * -+ * // Now: -+ * // nb == 2 -+ * // &to_be_added == array[0] -+ * // &to_be_added2 == array[1] -+ * -+ * av_freep(&array); -+ * @endcode -+ * -+ * The second system stores the value directly in a block of memory. As a -+ * result, the function has to know the size of the type. av_dynarray2_add() -+ * implements this mechanism. -+ * -+ * @code -+ * type *array = NULL; //< an array of values -+ * int nb = 0; //< a variable to keep track of the length of the array -+ * -+ * type to_be_added = ...; -+ * type to_be_added2 = ...; -+ * -+ * type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), NULL); -+ * if (!addr) -+ * return AVERROR(ENOMEM); -+ * memcpy(addr, &to_be_added, sizeof(to_be_added)); -+ * -+ * // Shortcut of the above. -+ * type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), -+ * (const void *)&to_be_added2); -+ * if (!addr) -+ * return AVERROR(ENOMEM); -+ * -+ * // Now: -+ * // nb == 2 -+ * // to_be_added == array[0] -+ * // to_be_added2 == array[1] -+ * -+ * av_freep(&array); -+ * @endcode -+ * -+ * @{ -+ */ -+ -+/** -+ * Add the pointer to an element to a dynamic array. -+ * -+ * The array to grow is supposed to be an array of pointers to -+ * structures, and the element to add must be a pointer to an already -+ * allocated structure. -+ * -+ * The array is reallocated when its size reaches powers of 2. -+ * Therefore, the amortized cost of adding an element is constant. -+ * -+ * In case of success, the pointer to the array is updated in order to -+ * point to the new grown array, and the number pointed to by `nb_ptr` -+ * is incremented. -+ * In case of failure, the array is freed, `*tab_ptr` is set to `NULL` and -+ * `*nb_ptr` is set to 0. -+ * -+ * @param[in,out] tab_ptr Pointer to the array to grow -+ * @param[in,out] nb_ptr Pointer to the number of elements in the array -+ * @param[in] elem Element to add -+ * @see av_dynarray_add_nofree(), av_dynarray2_add() -+ */ -+void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); -+ -+/** -+ * Add an element to a dynamic array. -+ * -+ * Function has the same functionality as av_dynarray_add(), -+ * but it doesn't free memory on fails. It returns error code -+ * instead and leave current buffer untouched. -+ * -+ * @return >=0 on success, negative otherwise -+ * @see av_dynarray_add(), av_dynarray2_add() -+ */ -+av_warn_unused_result -+int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem); -+ -+/** -+ * Add an element of size `elem_size` to a dynamic array. -+ * -+ * The array is reallocated when its number of elements reaches powers of 2. -+ * Therefore, the amortized cost of adding an element is constant. -+ * -+ * In case of success, the pointer to the array is updated in order to -+ * point to the new grown array, and the number pointed to by `nb_ptr` -+ * is incremented. -+ * In case of failure, the array is freed, `*tab_ptr` is set to `NULL` and -+ * `*nb_ptr` is set to 0. -+ * -+ * @param[in,out] tab_ptr Pointer to the array to grow -+ * @param[in,out] nb_ptr Pointer to the number of elements in the array -+ * @param[in] elem_size Size in bytes of an element in the array -+ * @param[in] elem_data Pointer to the data of the element to add. If -+ * `NULL`, the space of the newly added element is -+ * allocated but left uninitialized. -+ * -+ * @return Pointer to the data of the element to copy in the newly allocated -+ * space -+ * @see av_dynarray_add(), av_dynarray_add_nofree() -+ */ -+void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, -+ const uint8_t *elem_data); -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @defgroup lavu_mem_misc Miscellaneous Functions -+ * -+ * Other functions related to memory allocation. -+ * -+ * @{ -+ */ -+ -+/** -+ * Multiply two `size_t` values checking for overflow. -+ * -+ * @param[in] a,b Operands of multiplication -+ * @param[out] r Pointer to the result of the operation -+ * @return 0 on success, AVERROR(EINVAL) on overflow -+ */ -+static inline int av_size_mult(size_t a, size_t b, size_t *r) -+{ -+ size_t t = a * b; -+ /* Hack inspired from glibc: don't try the division if nelem and elsize -+ * are both less than sqrt(SIZE_MAX). */ -+ if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b) -+ return AVERROR(EINVAL); -+ *r = t; -+ return 0; -+} -+ -+/** -+ * Set the maximum size that may be allocated in one block. -+ * -+ * The value specified with this function is effective for all libavutil's @ref -+ * lavu_mem_funcs "heap management functions." -+ * -+ * By default, the max value is defined as `INT_MAX`. -+ * -+ * @param max Value to be set as the new maximum size -+ * -+ * @warning Exercise extreme caution when using this function. Don't touch -+ * this if you do not understand the full consequence of doing so. -+ */ -+void av_max_alloc(size_t max); -+ -+/** -+ * @} -+ * @} -+ */ -+ -+#endif /* AVUTIL_MEM_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/pixfmt.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/pixfmt.h -new file mode 100644 -index 000000000000..e184a56672dc ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/pixfmt.h -@@ -0,0 +1,529 @@ -+/* -+ * copyright (c) 2006 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_PIXFMT_H -+#define AVUTIL_PIXFMT_H -+ -+/** -+ * @file -+ * pixel format definitions -+ */ -+ -+#include "libavutil/avconfig.h" -+#include "version.h" -+ -+#define AVPALETTE_SIZE 1024 -+#define AVPALETTE_COUNT 256 -+ -+/** -+ * Pixel format. -+ * -+ * @note -+ * AV_PIX_FMT_RGB32 is handled in an endian-specific manner. An RGBA -+ * color is put together as: -+ * (A << 24) | (R << 16) | (G << 8) | B -+ * This is stored as BGRA on little-endian CPU architectures and ARGB on -+ * big-endian CPUs. -+ * -+ * @par -+ * When the pixel format is palettized RGB32 (AV_PIX_FMT_PAL8), the palettized -+ * image data is stored in AVFrame.data[0]. The palette is transported in -+ * AVFrame.data[1], is 1024 bytes long (256 4-byte entries) and is -+ * formatted the same as in AV_PIX_FMT_RGB32 described above (i.e., it is -+ * also endian-specific). Note also that the individual RGB32 palette -+ * components stored in AVFrame.data[1] should be in the range 0..255. -+ * This is important as many custom PAL8 video codecs that were designed -+ * to run on the IBM VGA graphics adapter use 6-bit palette components. -+ * -+ * @par -+ * For all the 8 bits per pixel formats, an RGB32 palette is in data[1] like -+ * for pal8. This palette is filled in automatically by the function -+ * allocating the picture. -+ */ -+enum AVPixelFormat { -+ AV_PIX_FMT_NONE = -1, -+ AV_PIX_FMT_YUV420P, ///< planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples) -+ AV_PIX_FMT_YUYV422, ///< packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr -+ AV_PIX_FMT_RGB24, ///< packed RGB 8:8:8, 24bpp, RGBRGB... -+ AV_PIX_FMT_BGR24, ///< packed RGB 8:8:8, 24bpp, BGRBGR... -+ AV_PIX_FMT_YUV422P, ///< planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples) -+ AV_PIX_FMT_YUV444P, ///< planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples) -+ AV_PIX_FMT_YUV410P, ///< planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples) -+ AV_PIX_FMT_YUV411P, ///< planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) -+ AV_PIX_FMT_GRAY8, ///< Y , 8bpp -+ AV_PIX_FMT_MONOWHITE, ///< Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb -+ AV_PIX_FMT_MONOBLACK, ///< Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb -+ AV_PIX_FMT_PAL8, ///< 8 bits with AV_PIX_FMT_RGB32 palette -+ AV_PIX_FMT_YUVJ420P, ///< planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting color_range -+ AV_PIX_FMT_YUVJ422P, ///< planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting color_range -+ AV_PIX_FMT_YUVJ444P, ///< planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting color_range -+ AV_PIX_FMT_UYVY422, ///< packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1 -+ AV_PIX_FMT_UYYVYY411, ///< packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3 -+ AV_PIX_FMT_BGR8, ///< packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb) -+ AV_PIX_FMT_BGR4, ///< packed RGB 1:2:1 bitstream, 4bpp, (msb)1B 2G 1R(lsb), a byte contains two pixels, the first pixel in the byte is the one composed by the 4 msb bits -+ AV_PIX_FMT_BGR4_BYTE, ///< packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb) -+ AV_PIX_FMT_RGB8, ///< packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb) -+ AV_PIX_FMT_RGB4, ///< packed RGB 1:2:1 bitstream, 4bpp, (msb)1R 2G 1B(lsb), a byte contains two pixels, the first pixel in the byte is the one composed by the 4 msb bits -+ AV_PIX_FMT_RGB4_BYTE, ///< packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb) -+ AV_PIX_FMT_NV12, ///< planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (first byte U and the following byte V) -+ AV_PIX_FMT_NV21, ///< as above, but U and V bytes are swapped -+ -+ AV_PIX_FMT_ARGB, ///< packed ARGB 8:8:8:8, 32bpp, ARGBARGB... -+ AV_PIX_FMT_RGBA, ///< packed RGBA 8:8:8:8, 32bpp, RGBARGBA... -+ AV_PIX_FMT_ABGR, ///< packed ABGR 8:8:8:8, 32bpp, ABGRABGR... -+ AV_PIX_FMT_BGRA, ///< packed BGRA 8:8:8:8, 32bpp, BGRABGRA... -+ -+ AV_PIX_FMT_GRAY16BE, ///< Y , 16bpp, big-endian -+ AV_PIX_FMT_GRAY16LE, ///< Y , 16bpp, little-endian -+ AV_PIX_FMT_YUV440P, ///< planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples) -+ AV_PIX_FMT_YUVJ440P, ///< planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range -+ AV_PIX_FMT_YUVA420P, ///< planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples) -+ AV_PIX_FMT_RGB48BE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big-endian -+ AV_PIX_FMT_RGB48LE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as little-endian -+ -+ AV_PIX_FMT_RGB565BE, ///< packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian -+ AV_PIX_FMT_RGB565LE, ///< packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian -+ AV_PIX_FMT_RGB555BE, ///< packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined -+ AV_PIX_FMT_RGB555LE, ///< packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined -+ -+ AV_PIX_FMT_BGR565BE, ///< packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), big-endian -+ AV_PIX_FMT_BGR565LE, ///< packed BGR 5:6:5, 16bpp, (msb) 5B 6G 5R(lsb), little-endian -+ AV_PIX_FMT_BGR555BE, ///< packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), big-endian , X=unused/undefined -+ AV_PIX_FMT_BGR555LE, ///< packed BGR 5:5:5, 16bpp, (msb)1X 5B 5G 5R(lsb), little-endian, X=unused/undefined -+ -+#if FF_API_VAAPI -+ /** @name Deprecated pixel formats */ -+ /**@{*/ -+ AV_PIX_FMT_VAAPI_MOCO, ///< HW acceleration through VA API at motion compensation entry-point, Picture.data[3] contains a vaapi_render_state struct which contains macroblocks as well as various fields extracted from headers -+ AV_PIX_FMT_VAAPI_IDCT, ///< HW acceleration through VA API at IDCT entry-point, Picture.data[3] contains a vaapi_render_state struct which contains fields extracted from headers -+ AV_PIX_FMT_VAAPI_VLD, ///< HW decoding through VA API, Picture.data[3] contains a VASurfaceID -+ /**@}*/ -+ AV_PIX_FMT_VAAPI = AV_PIX_FMT_VAAPI_VLD, -+#else -+ /** -+ * Hardware acceleration through VA-API, data[3] contains a -+ * VASurfaceID. -+ */ -+ AV_PIX_FMT_VAAPI, -+#endif -+ -+ AV_PIX_FMT_YUV420P16LE, ///< planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian -+ AV_PIX_FMT_YUV420P16BE, ///< planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian -+ AV_PIX_FMT_YUV422P16LE, ///< planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian -+ AV_PIX_FMT_YUV422P16BE, ///< planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian -+ AV_PIX_FMT_YUV444P16LE, ///< planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian -+ AV_PIX_FMT_YUV444P16BE, ///< planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian -+ AV_PIX_FMT_DXVA2_VLD, ///< HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer -+ -+ AV_PIX_FMT_RGB444LE, ///< packed RGB 4:4:4, 16bpp, (msb)4X 4R 4G 4B(lsb), little-endian, X=unused/undefined -+ AV_PIX_FMT_RGB444BE, ///< packed RGB 4:4:4, 16bpp, (msb)4X 4R 4G 4B(lsb), big-endian, X=unused/undefined -+ AV_PIX_FMT_BGR444LE, ///< packed BGR 4:4:4, 16bpp, (msb)4X 4B 4G 4R(lsb), little-endian, X=unused/undefined -+ AV_PIX_FMT_BGR444BE, ///< packed BGR 4:4:4, 16bpp, (msb)4X 4B 4G 4R(lsb), big-endian, X=unused/undefined -+ AV_PIX_FMT_YA8, ///< 8 bits gray, 8 bits alpha -+ -+ AV_PIX_FMT_Y400A = AV_PIX_FMT_YA8, ///< alias for AV_PIX_FMT_YA8 -+ AV_PIX_FMT_GRAY8A= AV_PIX_FMT_YA8, ///< alias for AV_PIX_FMT_YA8 -+ -+ AV_PIX_FMT_BGR48BE, ///< packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big-endian -+ AV_PIX_FMT_BGR48LE, ///< packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as little-endian -+ -+ /** -+ * The following 12 formats have the disadvantage of needing 1 format for each bit depth. -+ * Notice that each 9/10 bits sample is stored in 16 bits with extra padding. -+ * If you want to support multiple bit depths, then using AV_PIX_FMT_YUV420P16* with the bpp stored separately is better. -+ */ -+ AV_PIX_FMT_YUV420P9BE, ///< planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian -+ AV_PIX_FMT_YUV420P9LE, ///< planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian -+ AV_PIX_FMT_YUV420P10BE,///< planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian -+ AV_PIX_FMT_YUV420P10LE,///< planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian -+ AV_PIX_FMT_YUV422P10BE,///< planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian -+ AV_PIX_FMT_YUV422P10LE,///< planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian -+ AV_PIX_FMT_YUV444P9BE, ///< planar YUV 4:4:4, 27bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian -+ AV_PIX_FMT_YUV444P9LE, ///< planar YUV 4:4:4, 27bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian -+ AV_PIX_FMT_YUV444P10BE,///< planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian -+ AV_PIX_FMT_YUV444P10LE,///< planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian -+ AV_PIX_FMT_YUV422P9BE, ///< planar YUV 4:2:2, 18bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian -+ AV_PIX_FMT_YUV422P9LE, ///< planar YUV 4:2:2, 18bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian -+ AV_PIX_FMT_GBRP, ///< planar GBR 4:4:4 24bpp -+ AV_PIX_FMT_GBR24P = AV_PIX_FMT_GBRP, // alias for #AV_PIX_FMT_GBRP -+ AV_PIX_FMT_GBRP9BE, ///< planar GBR 4:4:4 27bpp, big-endian -+ AV_PIX_FMT_GBRP9LE, ///< planar GBR 4:4:4 27bpp, little-endian -+ AV_PIX_FMT_GBRP10BE, ///< planar GBR 4:4:4 30bpp, big-endian -+ AV_PIX_FMT_GBRP10LE, ///< planar GBR 4:4:4 30bpp, little-endian -+ AV_PIX_FMT_GBRP16BE, ///< planar GBR 4:4:4 48bpp, big-endian -+ AV_PIX_FMT_GBRP16LE, ///< planar GBR 4:4:4 48bpp, little-endian -+ AV_PIX_FMT_YUVA422P, ///< planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples) -+ AV_PIX_FMT_YUVA444P, ///< planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples) -+ AV_PIX_FMT_YUVA420P9BE, ///< planar YUV 4:2:0 22.5bpp, (1 Cr & Cb sample per 2x2 Y & A samples), big-endian -+ AV_PIX_FMT_YUVA420P9LE, ///< planar YUV 4:2:0 22.5bpp, (1 Cr & Cb sample per 2x2 Y & A samples), little-endian -+ AV_PIX_FMT_YUVA422P9BE, ///< planar YUV 4:2:2 27bpp, (1 Cr & Cb sample per 2x1 Y & A samples), big-endian -+ AV_PIX_FMT_YUVA422P9LE, ///< planar YUV 4:2:2 27bpp, (1 Cr & Cb sample per 2x1 Y & A samples), little-endian -+ AV_PIX_FMT_YUVA444P9BE, ///< planar YUV 4:4:4 36bpp, (1 Cr & Cb sample per 1x1 Y & A samples), big-endian -+ AV_PIX_FMT_YUVA444P9LE, ///< planar YUV 4:4:4 36bpp, (1 Cr & Cb sample per 1x1 Y & A samples), little-endian -+ AV_PIX_FMT_YUVA420P10BE, ///< planar YUV 4:2:0 25bpp, (1 Cr & Cb sample per 2x2 Y & A samples, big-endian) -+ AV_PIX_FMT_YUVA420P10LE, ///< planar YUV 4:2:0 25bpp, (1 Cr & Cb sample per 2x2 Y & A samples, little-endian) -+ AV_PIX_FMT_YUVA422P10BE, ///< planar YUV 4:2:2 30bpp, (1 Cr & Cb sample per 2x1 Y & A samples, big-endian) -+ AV_PIX_FMT_YUVA422P10LE, ///< planar YUV 4:2:2 30bpp, (1 Cr & Cb sample per 2x1 Y & A samples, little-endian) -+ AV_PIX_FMT_YUVA444P10BE, ///< planar YUV 4:4:4 40bpp, (1 Cr & Cb sample per 1x1 Y & A samples, big-endian) -+ AV_PIX_FMT_YUVA444P10LE, ///< planar YUV 4:4:4 40bpp, (1 Cr & Cb sample per 1x1 Y & A samples, little-endian) -+ AV_PIX_FMT_YUVA420P16BE, ///< planar YUV 4:2:0 40bpp, (1 Cr & Cb sample per 2x2 Y & A samples, big-endian) -+ AV_PIX_FMT_YUVA420P16LE, ///< planar YUV 4:2:0 40bpp, (1 Cr & Cb sample per 2x2 Y & A samples, little-endian) -+ AV_PIX_FMT_YUVA422P16BE, ///< planar YUV 4:2:2 48bpp, (1 Cr & Cb sample per 2x1 Y & A samples, big-endian) -+ AV_PIX_FMT_YUVA422P16LE, ///< planar YUV 4:2:2 48bpp, (1 Cr & Cb sample per 2x1 Y & A samples, little-endian) -+ AV_PIX_FMT_YUVA444P16BE, ///< planar YUV 4:4:4 64bpp, (1 Cr & Cb sample per 1x1 Y & A samples, big-endian) -+ AV_PIX_FMT_YUVA444P16LE, ///< planar YUV 4:4:4 64bpp, (1 Cr & Cb sample per 1x1 Y & A samples, little-endian) -+ -+ AV_PIX_FMT_VDPAU, ///< HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface -+ -+ AV_PIX_FMT_XYZ12LE, ///< packed XYZ 4:4:4, 36 bpp, (msb) 12X, 12Y, 12Z (lsb), the 2-byte value for each X/Y/Z is stored as little-endian, the 4 lower bits are set to 0 -+ AV_PIX_FMT_XYZ12BE, ///< packed XYZ 4:4:4, 36 bpp, (msb) 12X, 12Y, 12Z (lsb), the 2-byte value for each X/Y/Z is stored as big-endian, the 4 lower bits are set to 0 -+ AV_PIX_FMT_NV16, ///< interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples) -+ AV_PIX_FMT_NV20LE, ///< interleaved chroma YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian -+ AV_PIX_FMT_NV20BE, ///< interleaved chroma YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian -+ -+ AV_PIX_FMT_RGBA64BE, ///< packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is stored as big-endian -+ AV_PIX_FMT_RGBA64LE, ///< packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is stored as little-endian -+ AV_PIX_FMT_BGRA64BE, ///< packed RGBA 16:16:16:16, 64bpp, 16B, 16G, 16R, 16A, the 2-byte value for each R/G/B/A component is stored as big-endian -+ AV_PIX_FMT_BGRA64LE, ///< packed RGBA 16:16:16:16, 64bpp, 16B, 16G, 16R, 16A, the 2-byte value for each R/G/B/A component is stored as little-endian -+ -+ AV_PIX_FMT_YVYU422, ///< packed YUV 4:2:2, 16bpp, Y0 Cr Y1 Cb -+ -+ AV_PIX_FMT_YA16BE, ///< 16 bits gray, 16 bits alpha (big-endian) -+ AV_PIX_FMT_YA16LE, ///< 16 bits gray, 16 bits alpha (little-endian) -+ -+ AV_PIX_FMT_GBRAP, ///< planar GBRA 4:4:4:4 32bpp -+ AV_PIX_FMT_GBRAP16BE, ///< planar GBRA 4:4:4:4 64bpp, big-endian -+ AV_PIX_FMT_GBRAP16LE, ///< planar GBRA 4:4:4:4 64bpp, little-endian -+ /** -+ * HW acceleration through QSV, data[3] contains a pointer to the -+ * mfxFrameSurface1 structure. -+ */ -+ AV_PIX_FMT_QSV, -+ /** -+ * HW acceleration though MMAL, data[3] contains a pointer to the -+ * MMAL_BUFFER_HEADER_T structure. -+ */ -+ AV_PIX_FMT_MMAL, -+ -+ AV_PIX_FMT_D3D11VA_VLD, ///< HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView pointer -+ -+ /** -+ * HW acceleration through CUDA. data[i] contain CUdeviceptr pointers -+ * exactly as for system memory frames. -+ */ -+ AV_PIX_FMT_CUDA, -+ -+ AV_PIX_FMT_0RGB, ///< packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined -+ AV_PIX_FMT_RGB0, ///< packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined -+ AV_PIX_FMT_0BGR, ///< packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined -+ AV_PIX_FMT_BGR0, ///< packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined -+ -+ AV_PIX_FMT_YUV420P12BE, ///< planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian -+ AV_PIX_FMT_YUV420P12LE, ///< planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian -+ AV_PIX_FMT_YUV420P14BE, ///< planar YUV 4:2:0,21bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian -+ AV_PIX_FMT_YUV420P14LE, ///< planar YUV 4:2:0,21bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian -+ AV_PIX_FMT_YUV422P12BE, ///< planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian -+ AV_PIX_FMT_YUV422P12LE, ///< planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian -+ AV_PIX_FMT_YUV422P14BE, ///< planar YUV 4:2:2,28bpp, (1 Cr & Cb sample per 2x1 Y samples), big-endian -+ AV_PIX_FMT_YUV422P14LE, ///< planar YUV 4:2:2,28bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian -+ AV_PIX_FMT_YUV444P12BE, ///< planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian -+ AV_PIX_FMT_YUV444P12LE, ///< planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian -+ AV_PIX_FMT_YUV444P14BE, ///< planar YUV 4:4:4,42bpp, (1 Cr & Cb sample per 1x1 Y samples), big-endian -+ AV_PIX_FMT_YUV444P14LE, ///< planar YUV 4:4:4,42bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian -+ AV_PIX_FMT_GBRP12BE, ///< planar GBR 4:4:4 36bpp, big-endian -+ AV_PIX_FMT_GBRP12LE, ///< planar GBR 4:4:4 36bpp, little-endian -+ AV_PIX_FMT_GBRP14BE, ///< planar GBR 4:4:4 42bpp, big-endian -+ AV_PIX_FMT_GBRP14LE, ///< planar GBR 4:4:4 42bpp, little-endian -+ AV_PIX_FMT_YUVJ411P, ///< planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV411P and setting color_range -+ -+ AV_PIX_FMT_BAYER_BGGR8, ///< bayer, BGBG..(odd line), GRGR..(even line), 8-bit samples */ -+ AV_PIX_FMT_BAYER_RGGB8, ///< bayer, RGRG..(odd line), GBGB..(even line), 8-bit samples */ -+ AV_PIX_FMT_BAYER_GBRG8, ///< bayer, GBGB..(odd line), RGRG..(even line), 8-bit samples */ -+ AV_PIX_FMT_BAYER_GRBG8, ///< bayer, GRGR..(odd line), BGBG..(even line), 8-bit samples */ -+ AV_PIX_FMT_BAYER_BGGR16LE, ///< bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, little-endian */ -+ AV_PIX_FMT_BAYER_BGGR16BE, ///< bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, big-endian */ -+ AV_PIX_FMT_BAYER_RGGB16LE, ///< bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, little-endian */ -+ AV_PIX_FMT_BAYER_RGGB16BE, ///< bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, big-endian */ -+ AV_PIX_FMT_BAYER_GBRG16LE, ///< bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, little-endian */ -+ AV_PIX_FMT_BAYER_GBRG16BE, ///< bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, big-endian */ -+ AV_PIX_FMT_BAYER_GRBG16LE, ///< bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, little-endian */ -+ AV_PIX_FMT_BAYER_GRBG16BE, ///< bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, big-endian */ -+ -+ AV_PIX_FMT_XVMC,///< XVideo Motion Acceleration via common packet passing -+ -+ AV_PIX_FMT_YUV440P10LE, ///< planar YUV 4:4:0,20bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian -+ AV_PIX_FMT_YUV440P10BE, ///< planar YUV 4:4:0,20bpp, (1 Cr & Cb sample per 1x2 Y samples), big-endian -+ AV_PIX_FMT_YUV440P12LE, ///< planar YUV 4:4:0,24bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian -+ AV_PIX_FMT_YUV440P12BE, ///< planar YUV 4:4:0,24bpp, (1 Cr & Cb sample per 1x2 Y samples), big-endian -+ AV_PIX_FMT_AYUV64LE, ///< packed AYUV 4:4:4,64bpp (1 Cr & Cb sample per 1x1 Y & A samples), little-endian -+ AV_PIX_FMT_AYUV64BE, ///< packed AYUV 4:4:4,64bpp (1 Cr & Cb sample per 1x1 Y & A samples), big-endian -+ -+ AV_PIX_FMT_VIDEOTOOLBOX, ///< hardware decoding through Videotoolbox -+ -+ AV_PIX_FMT_P010LE, ///< like NV12, with 10bpp per component, data in the high bits, zeros in the low bits, little-endian -+ AV_PIX_FMT_P010BE, ///< like NV12, with 10bpp per component, data in the high bits, zeros in the low bits, big-endian -+ -+ AV_PIX_FMT_GBRAP12BE, ///< planar GBR 4:4:4:4 48bpp, big-endian -+ AV_PIX_FMT_GBRAP12LE, ///< planar GBR 4:4:4:4 48bpp, little-endian -+ -+ AV_PIX_FMT_GBRAP10BE, ///< planar GBR 4:4:4:4 40bpp, big-endian -+ AV_PIX_FMT_GBRAP10LE, ///< planar GBR 4:4:4:4 40bpp, little-endian -+ -+ AV_PIX_FMT_MEDIACODEC, ///< hardware decoding through MediaCodec -+ -+ AV_PIX_FMT_GRAY12BE, ///< Y , 12bpp, big-endian -+ AV_PIX_FMT_GRAY12LE, ///< Y , 12bpp, little-endian -+ AV_PIX_FMT_GRAY10BE, ///< Y , 10bpp, big-endian -+ AV_PIX_FMT_GRAY10LE, ///< Y , 10bpp, little-endian -+ -+ AV_PIX_FMT_P016LE, ///< like NV12, with 16bpp per component, little-endian -+ AV_PIX_FMT_P016BE, ///< like NV12, with 16bpp per component, big-endian -+ -+ /** -+ * Hardware surfaces for Direct3D11. -+ * -+ * This is preferred over the legacy AV_PIX_FMT_D3D11VA_VLD. The new D3D11 -+ * hwaccel API and filtering support AV_PIX_FMT_D3D11 only. -+ * -+ * data[0] contains a ID3D11Texture2D pointer, and data[1] contains the -+ * texture array index of the frame as intptr_t if the ID3D11Texture2D is -+ * an array texture (or always 0 if it's a normal texture). -+ */ -+ AV_PIX_FMT_D3D11, -+ -+ AV_PIX_FMT_GRAY9BE, ///< Y , 9bpp, big-endian -+ AV_PIX_FMT_GRAY9LE, ///< Y , 9bpp, little-endian -+ -+ AV_PIX_FMT_GBRPF32BE, ///< IEEE-754 single precision planar GBR 4:4:4, 96bpp, big-endian -+ AV_PIX_FMT_GBRPF32LE, ///< IEEE-754 single precision planar GBR 4:4:4, 96bpp, little-endian -+ AV_PIX_FMT_GBRAPF32BE, ///< IEEE-754 single precision planar GBRA 4:4:4:4, 128bpp, big-endian -+ AV_PIX_FMT_GBRAPF32LE, ///< IEEE-754 single precision planar GBRA 4:4:4:4, 128bpp, little-endian -+ -+ /** -+ * DRM-managed buffers exposed through PRIME buffer sharing. -+ * -+ * data[0] points to an AVDRMFrameDescriptor. -+ */ -+ AV_PIX_FMT_DRM_PRIME, -+ /** -+ * Hardware surfaces for OpenCL. -+ * -+ * data[i] contain 2D image objects (typed in C as cl_mem, used -+ * in OpenCL as image2d_t) for each plane of the surface. -+ */ -+ AV_PIX_FMT_OPENCL, -+ -+ AV_PIX_FMT_NB ///< number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of formats might differ between versions -+}; -+ -+#if AV_HAVE_BIGENDIAN -+# define AV_PIX_FMT_NE(be, le) AV_PIX_FMT_##be -+#else -+# define AV_PIX_FMT_NE(be, le) AV_PIX_FMT_##le -+#endif -+ -+#define AV_PIX_FMT_RGB32 AV_PIX_FMT_NE(ARGB, BGRA) -+#define AV_PIX_FMT_RGB32_1 AV_PIX_FMT_NE(RGBA, ABGR) -+#define AV_PIX_FMT_BGR32 AV_PIX_FMT_NE(ABGR, RGBA) -+#define AV_PIX_FMT_BGR32_1 AV_PIX_FMT_NE(BGRA, ARGB) -+#define AV_PIX_FMT_0RGB32 AV_PIX_FMT_NE(0RGB, BGR0) -+#define AV_PIX_FMT_0BGR32 AV_PIX_FMT_NE(0BGR, RGB0) -+ -+#define AV_PIX_FMT_GRAY9 AV_PIX_FMT_NE(GRAY9BE, GRAY9LE) -+#define AV_PIX_FMT_GRAY10 AV_PIX_FMT_NE(GRAY10BE, GRAY10LE) -+#define AV_PIX_FMT_GRAY12 AV_PIX_FMT_NE(GRAY12BE, GRAY12LE) -+#define AV_PIX_FMT_GRAY16 AV_PIX_FMT_NE(GRAY16BE, GRAY16LE) -+#define AV_PIX_FMT_YA16 AV_PIX_FMT_NE(YA16BE, YA16LE) -+#define AV_PIX_FMT_RGB48 AV_PIX_FMT_NE(RGB48BE, RGB48LE) -+#define AV_PIX_FMT_RGB565 AV_PIX_FMT_NE(RGB565BE, RGB565LE) -+#define AV_PIX_FMT_RGB555 AV_PIX_FMT_NE(RGB555BE, RGB555LE) -+#define AV_PIX_FMT_RGB444 AV_PIX_FMT_NE(RGB444BE, RGB444LE) -+#define AV_PIX_FMT_RGBA64 AV_PIX_FMT_NE(RGBA64BE, RGBA64LE) -+#define AV_PIX_FMT_BGR48 AV_PIX_FMT_NE(BGR48BE, BGR48LE) -+#define AV_PIX_FMT_BGR565 AV_PIX_FMT_NE(BGR565BE, BGR565LE) -+#define AV_PIX_FMT_BGR555 AV_PIX_FMT_NE(BGR555BE, BGR555LE) -+#define AV_PIX_FMT_BGR444 AV_PIX_FMT_NE(BGR444BE, BGR444LE) -+#define AV_PIX_FMT_BGRA64 AV_PIX_FMT_NE(BGRA64BE, BGRA64LE) -+ -+#define AV_PIX_FMT_YUV420P9 AV_PIX_FMT_NE(YUV420P9BE , YUV420P9LE) -+#define AV_PIX_FMT_YUV422P9 AV_PIX_FMT_NE(YUV422P9BE , YUV422P9LE) -+#define AV_PIX_FMT_YUV444P9 AV_PIX_FMT_NE(YUV444P9BE , YUV444P9LE) -+#define AV_PIX_FMT_YUV420P10 AV_PIX_FMT_NE(YUV420P10BE, YUV420P10LE) -+#define AV_PIX_FMT_YUV422P10 AV_PIX_FMT_NE(YUV422P10BE, YUV422P10LE) -+#define AV_PIX_FMT_YUV440P10 AV_PIX_FMT_NE(YUV440P10BE, YUV440P10LE) -+#define AV_PIX_FMT_YUV444P10 AV_PIX_FMT_NE(YUV444P10BE, YUV444P10LE) -+#define AV_PIX_FMT_YUV420P12 AV_PIX_FMT_NE(YUV420P12BE, YUV420P12LE) -+#define AV_PIX_FMT_YUV422P12 AV_PIX_FMT_NE(YUV422P12BE, YUV422P12LE) -+#define AV_PIX_FMT_YUV440P12 AV_PIX_FMT_NE(YUV440P12BE, YUV440P12LE) -+#define AV_PIX_FMT_YUV444P12 AV_PIX_FMT_NE(YUV444P12BE, YUV444P12LE) -+#define AV_PIX_FMT_YUV420P14 AV_PIX_FMT_NE(YUV420P14BE, YUV420P14LE) -+#define AV_PIX_FMT_YUV422P14 AV_PIX_FMT_NE(YUV422P14BE, YUV422P14LE) -+#define AV_PIX_FMT_YUV444P14 AV_PIX_FMT_NE(YUV444P14BE, YUV444P14LE) -+#define AV_PIX_FMT_YUV420P16 AV_PIX_FMT_NE(YUV420P16BE, YUV420P16LE) -+#define AV_PIX_FMT_YUV422P16 AV_PIX_FMT_NE(YUV422P16BE, YUV422P16LE) -+#define AV_PIX_FMT_YUV444P16 AV_PIX_FMT_NE(YUV444P16BE, YUV444P16LE) -+ -+#define AV_PIX_FMT_GBRP9 AV_PIX_FMT_NE(GBRP9BE , GBRP9LE) -+#define AV_PIX_FMT_GBRP10 AV_PIX_FMT_NE(GBRP10BE, GBRP10LE) -+#define AV_PIX_FMT_GBRP12 AV_PIX_FMT_NE(GBRP12BE, GBRP12LE) -+#define AV_PIX_FMT_GBRP14 AV_PIX_FMT_NE(GBRP14BE, GBRP14LE) -+#define AV_PIX_FMT_GBRP16 AV_PIX_FMT_NE(GBRP16BE, GBRP16LE) -+#define AV_PIX_FMT_GBRAP10 AV_PIX_FMT_NE(GBRAP10BE, GBRAP10LE) -+#define AV_PIX_FMT_GBRAP12 AV_PIX_FMT_NE(GBRAP12BE, GBRAP12LE) -+#define AV_PIX_FMT_GBRAP16 AV_PIX_FMT_NE(GBRAP16BE, GBRAP16LE) -+ -+#define AV_PIX_FMT_BAYER_BGGR16 AV_PIX_FMT_NE(BAYER_BGGR16BE, BAYER_BGGR16LE) -+#define AV_PIX_FMT_BAYER_RGGB16 AV_PIX_FMT_NE(BAYER_RGGB16BE, BAYER_RGGB16LE) -+#define AV_PIX_FMT_BAYER_GBRG16 AV_PIX_FMT_NE(BAYER_GBRG16BE, BAYER_GBRG16LE) -+#define AV_PIX_FMT_BAYER_GRBG16 AV_PIX_FMT_NE(BAYER_GRBG16BE, BAYER_GRBG16LE) -+ -+#define AV_PIX_FMT_GBRPF32 AV_PIX_FMT_NE(GBRPF32BE, GBRPF32LE) -+#define AV_PIX_FMT_GBRAPF32 AV_PIX_FMT_NE(GBRAPF32BE, GBRAPF32LE) -+ -+#define AV_PIX_FMT_YUVA420P9 AV_PIX_FMT_NE(YUVA420P9BE , YUVA420P9LE) -+#define AV_PIX_FMT_YUVA422P9 AV_PIX_FMT_NE(YUVA422P9BE , YUVA422P9LE) -+#define AV_PIX_FMT_YUVA444P9 AV_PIX_FMT_NE(YUVA444P9BE , YUVA444P9LE) -+#define AV_PIX_FMT_YUVA420P10 AV_PIX_FMT_NE(YUVA420P10BE, YUVA420P10LE) -+#define AV_PIX_FMT_YUVA422P10 AV_PIX_FMT_NE(YUVA422P10BE, YUVA422P10LE) -+#define AV_PIX_FMT_YUVA444P10 AV_PIX_FMT_NE(YUVA444P10BE, YUVA444P10LE) -+#define AV_PIX_FMT_YUVA420P16 AV_PIX_FMT_NE(YUVA420P16BE, YUVA420P16LE) -+#define AV_PIX_FMT_YUVA422P16 AV_PIX_FMT_NE(YUVA422P16BE, YUVA422P16LE) -+#define AV_PIX_FMT_YUVA444P16 AV_PIX_FMT_NE(YUVA444P16BE, YUVA444P16LE) -+ -+#define AV_PIX_FMT_XYZ12 AV_PIX_FMT_NE(XYZ12BE, XYZ12LE) -+#define AV_PIX_FMT_NV20 AV_PIX_FMT_NE(NV20BE, NV20LE) -+#define AV_PIX_FMT_AYUV64 AV_PIX_FMT_NE(AYUV64BE, AYUV64LE) -+#define AV_PIX_FMT_P010 AV_PIX_FMT_NE(P010BE, P010LE) -+#define AV_PIX_FMT_P016 AV_PIX_FMT_NE(P016BE, P016LE) -+ -+/** -+ * Chromaticity coordinates of the source primaries. -+ * These values match the ones defined by ISO/IEC 23001-8_2013 § 7.1. -+ */ -+enum AVColorPrimaries { -+ AVCOL_PRI_RESERVED0 = 0, -+ AVCOL_PRI_BT709 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B -+ AVCOL_PRI_UNSPECIFIED = 2, -+ AVCOL_PRI_RESERVED = 3, -+ AVCOL_PRI_BT470M = 4, ///< also FCC Title 47 Code of Federal Regulations 73.682 (a)(20) -+ -+ AVCOL_PRI_BT470BG = 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM -+ AVCOL_PRI_SMPTE170M = 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC -+ AVCOL_PRI_SMPTE240M = 7, ///< functionally identical to above -+ AVCOL_PRI_FILM = 8, ///< colour filters using Illuminant C -+ AVCOL_PRI_BT2020 = 9, ///< ITU-R BT2020 -+ AVCOL_PRI_SMPTE428 = 10, ///< SMPTE ST 428-1 (CIE 1931 XYZ) -+ AVCOL_PRI_SMPTEST428_1 = AVCOL_PRI_SMPTE428, -+ AVCOL_PRI_SMPTE431 = 11, ///< SMPTE ST 431-2 (2011) / DCI P3 -+ AVCOL_PRI_SMPTE432 = 12, ///< SMPTE ST 432-1 (2010) / P3 D65 / Display P3 -+ AVCOL_PRI_JEDEC_P22 = 22, ///< JEDEC P22 phosphors -+ AVCOL_PRI_NB ///< Not part of ABI -+}; -+ -+/** -+ * Color Transfer Characteristic. -+ * These values match the ones defined by ISO/IEC 23001-8_2013 § 7.2. -+ */ -+enum AVColorTransferCharacteristic { -+ AVCOL_TRC_RESERVED0 = 0, -+ AVCOL_TRC_BT709 = 1, ///< also ITU-R BT1361 -+ AVCOL_TRC_UNSPECIFIED = 2, -+ AVCOL_TRC_RESERVED = 3, -+ AVCOL_TRC_GAMMA22 = 4, ///< also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM -+ AVCOL_TRC_GAMMA28 = 5, ///< also ITU-R BT470BG -+ AVCOL_TRC_SMPTE170M = 6, ///< also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC -+ AVCOL_TRC_SMPTE240M = 7, -+ AVCOL_TRC_LINEAR = 8, ///< "Linear transfer characteristics" -+ AVCOL_TRC_LOG = 9, ///< "Logarithmic transfer characteristic (100:1 range)" -+ AVCOL_TRC_LOG_SQRT = 10, ///< "Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)" -+ AVCOL_TRC_IEC61966_2_4 = 11, ///< IEC 61966-2-4 -+ AVCOL_TRC_BT1361_ECG = 12, ///< ITU-R BT1361 Extended Colour Gamut -+ AVCOL_TRC_IEC61966_2_1 = 13, ///< IEC 61966-2-1 (sRGB or sYCC) -+ AVCOL_TRC_BT2020_10 = 14, ///< ITU-R BT2020 for 10-bit system -+ AVCOL_TRC_BT2020_12 = 15, ///< ITU-R BT2020 for 12-bit system -+ AVCOL_TRC_SMPTE2084 = 16, ///< SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems -+ AVCOL_TRC_SMPTEST2084 = AVCOL_TRC_SMPTE2084, -+ AVCOL_TRC_SMPTE428 = 17, ///< SMPTE ST 428-1 -+ AVCOL_TRC_SMPTEST428_1 = AVCOL_TRC_SMPTE428, -+ AVCOL_TRC_ARIB_STD_B67 = 18, ///< ARIB STD-B67, known as "Hybrid log-gamma" -+ AVCOL_TRC_NB ///< Not part of ABI -+}; -+ -+/** -+ * YUV colorspace type. -+ * These values match the ones defined by ISO/IEC 23001-8_2013 § 7.3. -+ */ -+enum AVColorSpace { -+ AVCOL_SPC_RGB = 0, ///< order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB) -+ AVCOL_SPC_BT709 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B -+ AVCOL_SPC_UNSPECIFIED = 2, -+ AVCOL_SPC_RESERVED = 3, -+ AVCOL_SPC_FCC = 4, ///< FCC Title 47 Code of Federal Regulations 73.682 (a)(20) -+ AVCOL_SPC_BT470BG = 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 -+ AVCOL_SPC_SMPTE170M = 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC -+ AVCOL_SPC_SMPTE240M = 7, ///< functionally identical to above -+ AVCOL_SPC_YCGCO = 8, ///< Used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16 -+ AVCOL_SPC_YCOCG = AVCOL_SPC_YCGCO, -+ AVCOL_SPC_BT2020_NCL = 9, ///< ITU-R BT2020 non-constant luminance system -+ AVCOL_SPC_BT2020_CL = 10, ///< ITU-R BT2020 constant luminance system -+ AVCOL_SPC_SMPTE2085 = 11, ///< SMPTE 2085, Y'D'zD'x -+ AVCOL_SPC_CHROMA_DERIVED_NCL = 12, ///< Chromaticity-derived non-constant luminance system -+ AVCOL_SPC_CHROMA_DERIVED_CL = 13, ///< Chromaticity-derived constant luminance system -+ AVCOL_SPC_ICTCP = 14, ///< ITU-R BT.2100-0, ICtCp -+ AVCOL_SPC_NB ///< Not part of ABI -+}; -+ -+/** -+ * MPEG vs JPEG YUV range. -+ */ -+enum AVColorRange { -+ AVCOL_RANGE_UNSPECIFIED = 0, -+ AVCOL_RANGE_MPEG = 1, ///< the normal 219*2^(n-8) "MPEG" YUV ranges -+ AVCOL_RANGE_JPEG = 2, ///< the normal 2^n-1 "JPEG" YUV ranges -+ AVCOL_RANGE_NB ///< Not part of ABI -+}; -+ -+/** -+ * Location of chroma samples. -+ * -+ * Illustration showing the location of the first (top left) chroma sample of the -+ * image, the left shows only luma, the right -+ * shows the location of the chroma sample, the 2 could be imagined to overlay -+ * each other but are drawn separately due to limitations of ASCII -+ * -+ * 1st 2nd 1st 2nd horizontal luma sample positions -+ * v v v v -+ * ______ ______ -+ *1st luma line > |X X ... |3 4 X ... X are luma samples, -+ * | |1 2 1-6 are possible chroma positions -+ *2nd luma line > |X X ... |5 6 X ... 0 is undefined/unknown position -+ */ -+enum AVChromaLocation { -+ AVCHROMA_LOC_UNSPECIFIED = 0, -+ AVCHROMA_LOC_LEFT = 1, ///< MPEG-2/4 4:2:0, H.264 default for 4:2:0 -+ AVCHROMA_LOC_CENTER = 2, ///< MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0 -+ AVCHROMA_LOC_TOPLEFT = 3, ///< ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2 -+ AVCHROMA_LOC_TOP = 4, -+ AVCHROMA_LOC_BOTTOMLEFT = 5, -+ AVCHROMA_LOC_BOTTOM = 6, -+ AVCHROMA_LOC_NB ///< Not part of ABI -+}; -+ -+#endif /* AVUTIL_PIXFMT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/rational.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/rational.h -new file mode 100644 -index 000000000000..5c6b67b4e9f8 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/rational.h -@@ -0,0 +1,214 @@ -+/* -+ * rational numbers -+ * Copyright (c) 2003 Michael Niedermayer -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * @ingroup lavu_math_rational -+ * Utilties for rational number calculation. -+ * @author Michael Niedermayer -+ */ -+ -+#ifndef AVUTIL_RATIONAL_H -+#define AVUTIL_RATIONAL_H -+ -+#include -+#include -+#include "attributes.h" -+ -+/** -+ * @defgroup lavu_math_rational AVRational -+ * @ingroup lavu_math -+ * Rational number calculation. -+ * -+ * While rational numbers can be expressed as floating-point numbers, the -+ * conversion process is a lossy one, so are floating-point operations. On the -+ * other hand, the nature of FFmpeg demands highly accurate calculation of -+ * timestamps. This set of rational number utilities serves as a generic -+ * interface for manipulating rational numbers as pairs of numerators and -+ * denominators. -+ * -+ * Many of the functions that operate on AVRational's have the suffix `_q`, in -+ * reference to the mathematical symbol "â„š" (Q) which denotes the set of all -+ * rational numbers. -+ * -+ * @{ -+ */ -+ -+/** -+ * Rational number (pair of numerator and denominator). -+ */ -+typedef struct AVRational{ -+ int num; ///< Numerator -+ int den; ///< Denominator -+} AVRational; -+ -+/** -+ * Create an AVRational. -+ * -+ * Useful for compilers that do not support compound literals. -+ * -+ * @note The return value is not reduced. -+ * @see av_reduce() -+ */ -+static inline AVRational av_make_q(int num, int den) -+{ -+ AVRational r = { num, den }; -+ return r; -+} -+ -+/** -+ * Compare two rationals. -+ * -+ * @param a First rational -+ * @param b Second rational -+ * -+ * @return One of the following values: -+ * - 0 if `a == b` -+ * - 1 if `a > b` -+ * - -1 if `a < b` -+ * - `INT_MIN` if one of the values is of the form `0 / 0` -+ */ -+static inline int av_cmp_q(AVRational a, AVRational b){ -+ const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den; -+ -+ if(tmp) return (int)((tmp ^ a.den ^ b.den)>>63)|1; -+ else if(b.den && a.den) return 0; -+ else if(a.num && b.num) return (a.num>>31) - (b.num>>31); -+ else return INT_MIN; -+} -+ -+/** -+ * Convert an AVRational to a `double`. -+ * @param a AVRational to convert -+ * @return `a` in floating-point form -+ * @see av_d2q() -+ */ -+static inline double av_q2d(AVRational a){ -+ return a.num / (double) a.den; -+} -+ -+/** -+ * Reduce a fraction. -+ * -+ * This is useful for framerate calculations. -+ * -+ * @param[out] dst_num Destination numerator -+ * @param[out] dst_den Destination denominator -+ * @param[in] num Source numerator -+ * @param[in] den Source denominator -+ * @param[in] max Maximum allowed values for `dst_num` & `dst_den` -+ * @return 1 if the operation is exact, 0 otherwise -+ */ -+int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max); -+ -+/** -+ * Multiply two rationals. -+ * @param b First rational -+ * @param c Second rational -+ * @return b*c -+ */ -+AVRational av_mul_q(AVRational b, AVRational c) av_const; -+ -+/** -+ * Divide one rational by another. -+ * @param b First rational -+ * @param c Second rational -+ * @return b/c -+ */ -+AVRational av_div_q(AVRational b, AVRational c) av_const; -+ -+/** -+ * Add two rationals. -+ * @param b First rational -+ * @param c Second rational -+ * @return b+c -+ */ -+AVRational av_add_q(AVRational b, AVRational c) av_const; -+ -+/** -+ * Subtract one rational from another. -+ * @param b First rational -+ * @param c Second rational -+ * @return b-c -+ */ -+AVRational av_sub_q(AVRational b, AVRational c) av_const; -+ -+/** -+ * Invert a rational. -+ * @param q value -+ * @return 1 / q -+ */ -+static av_always_inline AVRational av_inv_q(AVRational q) -+{ -+ AVRational r = { q.den, q.num }; -+ return r; -+} -+ -+/** -+ * Convert a double precision floating point number to a rational. -+ * -+ * In case of infinity, the returned value is expressed as `{1, 0}` or -+ * `{-1, 0}` depending on the sign. -+ * -+ * @param d `double` to convert -+ * @param max Maximum allowed numerator and denominator -+ * @return `d` in AVRational form -+ * @see av_q2d() -+ */ -+AVRational av_d2q(double d, int max) av_const; -+ -+/** -+ * Find which of the two rationals is closer to another rational. -+ * -+ * @param q Rational to be compared against -+ * @param q1,q2 Rationals to be tested -+ * @return One of the following values: -+ * - 1 if `q1` is nearer to `q` than `q2` -+ * - -1 if `q2` is nearer to `q` than `q1` -+ * - 0 if they have the same distance -+ */ -+int av_nearer_q(AVRational q, AVRational q1, AVRational q2); -+ -+/** -+ * Find the value in a list of rationals nearest a given reference rational. -+ * -+ * @param q Reference rational -+ * @param q_list Array of rationals terminated by `{0, 0}` -+ * @return Index of the nearest value found in the array -+ */ -+int av_find_nearest_q_idx(AVRational q, const AVRational* q_list); -+ -+/** -+ * Convert an AVRational to a IEEE 32-bit `float` expressed in fixed-point -+ * format. -+ * -+ * @param q Rational to be converted -+ * @return Equivalent floating-point value, expressed as an unsigned 32-bit -+ * integer. -+ * @note The returned value is platform-indepedant. -+ */ -+uint32_t av_q2intfloat(AVRational q); -+ -+/** -+ * @} -+ */ -+ -+#endif /* AVUTIL_RATIONAL_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/samplefmt.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/samplefmt.h -new file mode 100644 -index 000000000000..8cd43ae8568a ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/samplefmt.h -@@ -0,0 +1,272 @@ -+/* -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+#ifndef AVUTIL_SAMPLEFMT_H -+#define AVUTIL_SAMPLEFMT_H -+ -+#include -+ -+#include "avutil.h" -+#include "attributes.h" -+ -+/** -+ * @addtogroup lavu_audio -+ * @{ -+ * -+ * @defgroup lavu_sampfmts Audio sample formats -+ * -+ * Audio sample format enumeration and related convenience functions. -+ * @{ -+ */ -+ -+/** -+ * Audio sample formats -+ * -+ * - The data described by the sample format is always in native-endian order. -+ * Sample values can be expressed by native C types, hence the lack of a signed -+ * 24-bit sample format even though it is a common raw audio data format. -+ * -+ * - The floating-point formats are based on full volume being in the range -+ * [-1.0, 1.0]. Any values outside this range are beyond full volume level. -+ * -+ * - The data layout as used in av_samples_fill_arrays() and elsewhere in FFmpeg -+ * (such as AVFrame in libavcodec) is as follows: -+ * -+ * @par -+ * For planar sample formats, each audio channel is in a separate data plane, -+ * and linesize is the buffer size, in bytes, for a single plane. All data -+ * planes must be the same size. For packed sample formats, only the first data -+ * plane is used, and samples for each channel are interleaved. In this case, -+ * linesize is the buffer size, in bytes, for the 1 plane. -+ * -+ */ -+enum AVSampleFormat { -+ AV_SAMPLE_FMT_NONE = -1, -+ AV_SAMPLE_FMT_U8, ///< unsigned 8 bits -+ AV_SAMPLE_FMT_S16, ///< signed 16 bits -+ AV_SAMPLE_FMT_S32, ///< signed 32 bits -+ AV_SAMPLE_FMT_FLT, ///< float -+ AV_SAMPLE_FMT_DBL, ///< double -+ -+ AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar -+ AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar -+ AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar -+ AV_SAMPLE_FMT_FLTP, ///< float, planar -+ AV_SAMPLE_FMT_DBLP, ///< double, planar -+ AV_SAMPLE_FMT_S64, ///< signed 64 bits -+ AV_SAMPLE_FMT_S64P, ///< signed 64 bits, planar -+ -+ AV_SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if linking dynamically -+}; -+ -+/** -+ * Return the name of sample_fmt, or NULL if sample_fmt is not -+ * recognized. -+ */ -+const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt); -+ -+/** -+ * Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE -+ * on error. -+ */ -+enum AVSampleFormat av_get_sample_fmt(const char *name); -+ -+/** -+ * Return the planar<->packed alternative form of the given sample format, or -+ * AV_SAMPLE_FMT_NONE on error. If the passed sample_fmt is already in the -+ * requested planar/packed format, the format returned is the same as the -+ * input. -+ */ -+enum AVSampleFormat av_get_alt_sample_fmt(enum AVSampleFormat sample_fmt, int planar); -+ -+/** -+ * Get the packed alternative form of the given sample format. -+ * -+ * If the passed sample_fmt is already in packed format, the format returned is -+ * the same as the input. -+ * -+ * @return the packed alternative form of the given sample format or -+ AV_SAMPLE_FMT_NONE on error. -+ */ -+enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt); -+ -+/** -+ * Get the planar alternative form of the given sample format. -+ * -+ * If the passed sample_fmt is already in planar format, the format returned is -+ * the same as the input. -+ * -+ * @return the planar alternative form of the given sample format or -+ AV_SAMPLE_FMT_NONE on error. -+ */ -+enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt); -+ -+/** -+ * Generate a string corresponding to the sample format with -+ * sample_fmt, or a header if sample_fmt is negative. -+ * -+ * @param buf the buffer where to write the string -+ * @param buf_size the size of buf -+ * @param sample_fmt the number of the sample format to print the -+ * corresponding info string, or a negative value to print the -+ * corresponding header. -+ * @return the pointer to the filled buffer or NULL if sample_fmt is -+ * unknown or in case of other errors -+ */ -+char *av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat sample_fmt); -+ -+/** -+ * Return number of bytes per sample. -+ * -+ * @param sample_fmt the sample format -+ * @return number of bytes per sample or zero if unknown for the given -+ * sample format -+ */ -+int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt); -+ -+/** -+ * Check if the sample format is planar. -+ * -+ * @param sample_fmt the sample format to inspect -+ * @return 1 if the sample format is planar, 0 if it is interleaved -+ */ -+int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt); -+ -+/** -+ * Get the required buffer size for the given audio parameters. -+ * -+ * @param[out] linesize calculated linesize, may be NULL -+ * @param nb_channels the number of channels -+ * @param nb_samples the number of samples in a single channel -+ * @param sample_fmt the sample format -+ * @param align buffer size alignment (0 = default, 1 = no alignment) -+ * @return required buffer size, or negative error code on failure -+ */ -+int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, -+ enum AVSampleFormat sample_fmt, int align); -+ -+/** -+ * @} -+ * -+ * @defgroup lavu_sampmanip Samples manipulation -+ * -+ * Functions that manipulate audio samples -+ * @{ -+ */ -+ -+/** -+ * Fill plane data pointers and linesize for samples with sample -+ * format sample_fmt. -+ * -+ * The audio_data array is filled with the pointers to the samples data planes: -+ * for planar, set the start point of each channel's data within the buffer, -+ * for packed, set the start point of the entire buffer only. -+ * -+ * The value pointed to by linesize is set to the aligned size of each -+ * channel's data buffer for planar layout, or to the aligned size of the -+ * buffer for all channels for packed layout. -+ * -+ * The buffer in buf must be big enough to contain all the samples -+ * (use av_samples_get_buffer_size() to compute its minimum size), -+ * otherwise the audio_data pointers will point to invalid data. -+ * -+ * @see enum AVSampleFormat -+ * The documentation for AVSampleFormat describes the data layout. -+ * -+ * @param[out] audio_data array to be filled with the pointer for each channel -+ * @param[out] linesize calculated linesize, may be NULL -+ * @param buf the pointer to a buffer containing the samples -+ * @param nb_channels the number of channels -+ * @param nb_samples the number of samples in a single channel -+ * @param sample_fmt the sample format -+ * @param align buffer size alignment (0 = default, 1 = no alignment) -+ * @return >=0 on success or a negative error code on failure -+ * @todo return minimum size in bytes required for the buffer in case -+ * of success at the next bump -+ */ -+int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, -+ const uint8_t *buf, -+ int nb_channels, int nb_samples, -+ enum AVSampleFormat sample_fmt, int align); -+ -+/** -+ * Allocate a samples buffer for nb_samples samples, and fill data pointers and -+ * linesize accordingly. -+ * The allocated samples buffer can be freed by using av_freep(&audio_data[0]) -+ * Allocated data will be initialized to silence. -+ * -+ * @see enum AVSampleFormat -+ * The documentation for AVSampleFormat describes the data layout. -+ * -+ * @param[out] audio_data array to be filled with the pointer for each channel -+ * @param[out] linesize aligned size for audio buffer(s), may be NULL -+ * @param nb_channels number of audio channels -+ * @param nb_samples number of samples per channel -+ * @param align buffer size alignment (0 = default, 1 = no alignment) -+ * @return >=0 on success or a negative error code on failure -+ * @todo return the size of the allocated buffer in case of success at the next bump -+ * @see av_samples_fill_arrays() -+ * @see av_samples_alloc_array_and_samples() -+ */ -+int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, -+ int nb_samples, enum AVSampleFormat sample_fmt, int align); -+ -+/** -+ * Allocate a data pointers array, samples buffer for nb_samples -+ * samples, and fill data pointers and linesize accordingly. -+ * -+ * This is the same as av_samples_alloc(), but also allocates the data -+ * pointers array. -+ * -+ * @see av_samples_alloc() -+ */ -+int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, -+ int nb_samples, enum AVSampleFormat sample_fmt, int align); -+ -+/** -+ * Copy samples from src to dst. -+ * -+ * @param dst destination array of pointers to data planes -+ * @param src source array of pointers to data planes -+ * @param dst_offset offset in samples at which the data will be written to dst -+ * @param src_offset offset in samples at which the data will be read from src -+ * @param nb_samples number of samples to be copied -+ * @param nb_channels number of audio channels -+ * @param sample_fmt audio sample format -+ */ -+int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset, -+ int src_offset, int nb_samples, int nb_channels, -+ enum AVSampleFormat sample_fmt); -+ -+/** -+ * Fill an audio buffer with silence. -+ * -+ * @param audio_data array of pointers to data planes -+ * @param offset offset in samples at which to start filling -+ * @param nb_samples number of samples to fill -+ * @param nb_channels number of audio channels -+ * @param sample_fmt audio sample format -+ */ -+int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, -+ int nb_channels, enum AVSampleFormat sample_fmt); -+ -+/** -+ * @} -+ * @} -+ */ -+#endif /* AVUTIL_SAMPLEFMT_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/version.h dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/version.h -new file mode 100644 -index 000000000000..3a63e6355f84 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/include/libavutil/version.h -@@ -0,0 +1,139 @@ -+/* -+ * copyright (c) 2003 Fabrice Bellard -+ * -+ * This file is part of FFmpeg. -+ * -+ * FFmpeg 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.1 of the License, or (at your option) any later version. -+ * -+ * FFmpeg 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 FFmpeg; if not, write to the Free Software -+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -+ */ -+ -+/** -+ * @file -+ * @ingroup lavu -+ * Libavutil version macros -+ */ -+ -+#ifndef AVUTIL_VERSION_H -+#define AVUTIL_VERSION_H -+ -+#include "macros.h" -+ -+/** -+ * @addtogroup version_utils -+ * -+ * Useful to check and match library version in order to maintain -+ * backward compatibility. -+ * -+ * The FFmpeg libraries follow a versioning sheme very similar to -+ * Semantic Versioning (http://semver.org/) -+ * The difference is that the component called PATCH is called MICRO in FFmpeg -+ * and its value is reset to 100 instead of 0 to keep it above or equal to 100. -+ * Also we do not increase MICRO for every bugfix or change in git master. -+ * -+ * Prior to FFmpeg 3.2 point releases did not change any lib version number to -+ * avoid aliassing different git master checkouts. -+ * Starting with FFmpeg 3.2, the released library versions will occupy -+ * a separate MAJOR.MINOR that is not used on the master development branch. -+ * That is if we branch a release of master 55.10.123 we will bump to 55.11.100 -+ * for the release and master will continue at 55.12.100 after it. Each new -+ * point release will then bump the MICRO improving the usefulness of the lib -+ * versions. -+ * -+ * @{ -+ */ -+ -+#define AV_VERSION_INT(a, b, c) ((a)<<16 | (b)<<8 | (c)) -+#define AV_VERSION_DOT(a, b, c) a ##.## b ##.## c -+#define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c) -+ -+/** -+ * Extract version components from the full ::AV_VERSION_INT int as returned -+ * by functions like ::avformat_version() and ::avcodec_version() -+ */ -+#define AV_VERSION_MAJOR(a) ((a) >> 16) -+#define AV_VERSION_MINOR(a) (((a) & 0x00FF00) >> 8) -+#define AV_VERSION_MICRO(a) ((a) & 0xFF) -+ -+/** -+ * @} -+ */ -+ -+/** -+ * @defgroup lavu_ver Version and Build diagnostics -+ * -+ * Macros and function useful to check at compiletime and at runtime -+ * which version of libavutil is in use. -+ * -+ * @{ -+ */ -+ -+#define LIBAVUTIL_VERSION_MAJOR 56 -+#define LIBAVUTIL_VERSION_MINOR 14 -+#define LIBAVUTIL_VERSION_MICRO 100 -+ -+#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ -+ LIBAVUTIL_VERSION_MINOR, \ -+ LIBAVUTIL_VERSION_MICRO) -+#define LIBAVUTIL_VERSION AV_VERSION(LIBAVUTIL_VERSION_MAJOR, \ -+ LIBAVUTIL_VERSION_MINOR, \ -+ LIBAVUTIL_VERSION_MICRO) -+#define LIBAVUTIL_BUILD LIBAVUTIL_VERSION_INT -+ -+#define LIBAVUTIL_IDENT "Lavu" AV_STRINGIFY(LIBAVUTIL_VERSION) -+ -+/** -+ * @defgroup lavu_depr_guards Deprecation Guards -+ * FF_API_* defines may be placed below to indicate public API that will be -+ * dropped at a future version bump. The defines themselves are not part of -+ * the public API and may change, break or disappear at any time. -+ * -+ * @note, when bumping the major version it is recommended to manually -+ * disable each FF_API_* in its own commit instead of disabling them all -+ * at once through the bump. This improves the git bisect-ability of the change. -+ * -+ * @{ -+ */ -+ -+#ifndef FF_API_VAAPI -+#define FF_API_VAAPI (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_FRAME_QP -+#define FF_API_FRAME_QP (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_PLUS1_MINUS1 -+#define FF_API_PLUS1_MINUS1 (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_ERROR_FRAME -+#define FF_API_ERROR_FRAME (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_PKT_PTS -+#define FF_API_PKT_PTS (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_CRYPTO_SIZE_T -+#define FF_API_CRYPTO_SIZE_T (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_FRAME_GET_SET -+#define FF_API_FRAME_GET_SET (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+#ifndef FF_API_PSEUDOPAL -+#define FF_API_PSEUDOPAL (LIBAVUTIL_VERSION_MAJOR < 57) -+#endif -+ -+ -+/** -+ * @} -+ * @} -+ */ -+ -+#endif /* AVUTIL_VERSION_H */ -diff --git dom/media/platforms/ffmpeg/ffmpeg58/moz.build dom/media/platforms/ffmpeg/ffmpeg58/moz.build -new file mode 100644 -index 000000000000..c757ed6fad04 ---- /dev/null -+++ dom/media/platforms/ffmpeg/ffmpeg58/moz.build -@@ -0,0 +1,25 @@ -+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- -+# vim: set filetype=python: -+# This Source Code Form is subject to the terms of the Mozilla Public -+# License, v. 2.0. If a copy of the MPL was not distributed with this -+# file, You can obtain one at http://mozilla.org/MPL/2.0/. -+ -+UNIFIED_SOURCES += [ -+ '../FFmpegAudioDecoder.cpp', -+ '../FFmpegDataDecoder.cpp', -+ '../FFmpegDecoderModule.cpp', -+ '../FFmpegVideoDecoder.cpp', -+] -+LOCAL_INCLUDES += [ -+ '..', -+ 'include', -+] -+ -+if CONFIG['CC_TYPE'] in ('clang', 'gcc'): -+ CXXFLAGS += [ '-Wno-deprecated-declarations' ] -+if CONFIG['CC_TYPE'] == 'clang': -+ CXXFLAGS += [ -+ '-Wno-unknown-attributes', -+ ] -+ -+FINAL_LIBRARY = 'xul' -diff --git dom/media/platforms/ffmpeg/moz.build dom/media/platforms/ffmpeg/moz.build -index 604e445aa4d9..af96fb521e3d 100644 ---- dom/media/platforms/ffmpeg/moz.build -+++ dom/media/platforms/ffmpeg/moz.build -@@ -13,6 +13,7 @@ DIRS += [ - 'libav54', - 'libav55', - 'ffmpeg57', -+ 'ffmpeg58', - ] - - UNIFIED_SOURCES += [ Property changes on: head/www/firefox/files/patch-bug1435212 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1445907 =================================================================== --- head/www/firefox/files/patch-bug1445907 (revision 472726) +++ head/www/firefox/files/patch-bug1445907 (nonexistent) @@ -1,75 +0,0 @@ -commit 0c6dd4a750db -Author: Lars T Hansen -Date: Mon Mar 19 09:58:06 2018 +0100 - - Bug 1445907 - Save x28 before clobbering it in the regex compiler. r=sstangl ---- - js/src/irregexp/NativeRegExpMacroAssembler.cpp | 25 ++++++++++++++++++++++++- - js/src/jit-test/tests/regexp/bug1445907.js | 15 +++++++++++++++ - 2 files changed, 39 insertions(+), 1 deletion(-) - -diff --git js/src/irregexp/NativeRegExpMacroAssembler.cpp js/src/irregexp/NativeRegExpMacroAssembler.cpp -index 28a4c35e75bfe..c08b005cf856b 100644 ---- js/src/irregexp/NativeRegExpMacroAssembler.cpp -+++ js/src/irregexp/NativeRegExpMacroAssembler.cpp -@@ -123,7 +123,15 @@ NativeRegExpMacroAssembler::GenerateCode(JSContext* cx, bool match_only) - masm.bind(&entry_label_); - - #ifdef JS_CODEGEN_ARM64 -- // ARM64 communicates stack address via sp, but uses a pseudo-sp for addressing. -+ // ARM64 communicates stack address via SP, but uses a pseudo-sp (PSP) for -+ // addressing. The register we use for PSP may however also be used by -+ // calling code, and it is nonvolatile, so save it. Do this as a special -+ // case first because the generic save/restore code needs the PSP to be -+ // initialized already. -+ MOZ_ASSERT(PseudoStackPointer64.Is(masm.GetStackPointer64())); -+ masm.Str(PseudoStackPointer64, vixl::MemOperand(sp, -16, vixl::PreIndex)); -+ -+ // Initialize the PSP from the SP. - masm.initStackPtr(); - #endif - -@@ -421,7 +429,22 @@ NativeRegExpMacroAssembler::GenerateCode(JSContext* cx, bool match_only) - for (GeneralRegisterBackwardIterator iter(savedNonVolatileRegisters); iter.more(); ++iter) - masm.Pop(*iter); - -+#ifdef JS_CODEGEN_ARM64 -+ // Now restore the value that was in the PSP register on entry, and return. -+ -+ // Obtain the correct SP from the PSP. -+ masm.Mov(sp, PseudoStackPointer64); -+ -+ // Restore the saved value of the PSP register, this value is whatever the -+ // caller had saved in it, not any actual SP value, and it must not be -+ // overwritten subsequently. -+ masm.Ldr(PseudoStackPointer64, vixl::MemOperand(sp, 16, vixl::PostIndex)); -+ -+ // Perform a plain Ret(), as abiret() will move SP <- PSP and that is wrong. -+ masm.Ret(vixl::lr); -+#else - masm.abiret(); -+#endif - - // Backtrack code (branch target for conditional backtracks). - if (backtrack_label_.used()) { -diff --git js/src/jit-test/tests/regexp/bug1445907.js js/src/jit-test/tests/regexp/bug1445907.js -new file mode 100644 -index 0000000000000..75b23753eaf93 ---- /dev/null -+++ js/src/jit-test/tests/regexp/bug1445907.js -@@ -0,0 +1,15 @@ -+// On ARM64, we failed to save x28 properly when generating code for the regexp -+// matcher. -+// -+// There's wasm and Debugger code here because the combination forces the use of -+// x28 and exposes the bug when running on the simulator. -+ -+if (!wasmIsSupported()) -+ quit(); -+ -+var g = newGlobal(''); -+var dbg = new Debugger(g); -+g.eval(`var m = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary('(module (func (export "test")))')))`); -+var re = /./; -+dbg.onEnterFrame = function(frame) { re.exec("x") }; -+result = g.eval("m.exports.test()"); Property changes on: head/www/firefox/files/patch-bug1445907 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1451292 =================================================================== --- head/www/firefox/files/patch-bug1451292 (revision 472726) +++ head/www/firefox/files/patch-bug1451292 (nonexistent) @@ -1,33 +0,0 @@ -commit b3a02fddbce8 -Author: Lars T Hansen -Date: Wed Apr 4 15:48:48 2018 +0200 - - Bug 1451292 - Better payload for arm64 breakpoint instruction. r=sstangl - - At least some non-zero payloads confuse GDB and make it iloop on the - breakpoint instruction rather than break to the command line as it - should. There seems to be no reason not to use a zero payload. - - --HG-- - extra : rebase_source : 6d6f9aa2911b86b02572f88948d48bc2238c6353 - extra : amend_source : 9fed9235d481a9eadafc4a3e0075c9fef8b6050d ---- - js/src/jit/arm64/MacroAssembler-arm64.cpp | 5 +++-- - 1 file changed, 3 insertions(+), 2 deletions(-) - -diff --git js/src/jit/arm64/MacroAssembler-arm64.cpp js/src/jit/arm64/MacroAssembler-arm64.cpp -index 4ea64b1225d00..a212de0ec2f45 100644 ---- js/src/jit/arm64/MacroAssembler-arm64.cpp -+++ js/src/jit/arm64/MacroAssembler-arm64.cpp -@@ -237,8 +237,9 @@ MacroAssemblerCompat::profilerEnterFrame(RegisterOrSP framePtr, Register scratch - void - MacroAssemblerCompat::breakpoint() - { -- static int code = 0xA77; -- Brk((code++) & 0xffff); -+ // Note, other payloads are possible, but GDB is known to misinterpret them -+ // sometimes and iloop on the breakpoint instead of stopping properly. -+ Brk(0); - } - - // Either `any` is valid or `sixtyfour` is valid. Return a 32-bit ARMRegister Property changes on: head/www/firefox/files/patch-bug1451292 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1442583 =================================================================== --- head/www/firefox/files/patch-bug1442583 (revision 472726) +++ head/www/firefox/files/patch-bug1442583 (nonexistent) @@ -1,39 +0,0 @@ -commit 7371a080accd -Author: Lars T Hansen -Date: Mon Mar 5 09:55:28 2018 +0100 - - Bug 1442583 - Properly initialize ARM64 icache flushing machinery. r=sstangl - - --HG-- - extra : rebase_source : 73b5921da1fa0a19d6072e35d09bd7b528bb6bfc - extra : intermediate-source : 19516efbbf6750ba04e11c7099586d5be2fe818f - extra : source : 4316cc82d6302edf839a4af6fcb815f0ffa9f65c ---- - js/src/jit/ProcessExecutableMemory.cpp | 7 +++++++ - 1 file changed, 7 insertions(+) - -diff --git js/src/jit/ProcessExecutableMemory.cpp js/src/jit/ProcessExecutableMemory.cpp -index e763a9d68dc17..8a6d50b6a7845 100644 ---- js/src/jit/ProcessExecutableMemory.cpp -+++ js/src/jit/ProcessExecutableMemory.cpp -@@ -20,6 +20,9 @@ - #include "jsutil.h" - - #include "gc/Memory.h" -+#ifdef JS_CODEGEN_ARM64 -+# include "jit/arm64/vixl/Cpu-vixl.h" -+#endif - #include "threading/LockGuard.h" - #include "threading/Mutex.h" - #include "util/Windows.h" -@@ -621,6 +624,10 @@ js::jit::DeallocateExecutableMemory(void* addr, size_t bytes) - bool - js::jit::InitProcessExecutableMemory() - { -+#ifdef JS_CODEGEN_ARM64 -+ // Initialize instruction cache flushing. -+ vixl::CPU::SetUp(); -+#endif - return execMemory.init(); - } - Property changes on: head/www/firefox/files/patch-bug1442583 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1375074 =================================================================== --- head/www/firefox/files/patch-bug1375074 (revision 472726) +++ head/www/firefox/files/patch-bug1375074 (nonexistent) @@ -1,82 +0,0 @@ -commit fc25eb4380d0 -Author: Lars T Hansen -Date: Wed Feb 28 13:57:52 2018 +0100 - - Bug 1375074 - Save and restore non-volatile x28 on ARM64 for generated unboxed object constructor. r=sstangl - - --HG-- - extra : rebase_source : 4a2824b23cf7453d07c962123f780c0ff2fd5292 - extra : source : dec6ab6522a4eb4b3c2defc5078b38ed904f3383 ---- - js/src/jit-test/tests/bug1375074.js | 18 ++++++++++++++++++ - js/src/vm/UnboxedObject.cpp | 25 ++++++++++++++++++++++++- - 2 files changed, 42 insertions(+), 1 deletion(-) - -diff --git js/src/jit-test/tests/bug1375074.js js/src/jit-test/tests/bug1375074.js -new file mode 100644 -index 0000000000000..8bf01d96903fc ---- /dev/null -+++ js/src/jit-test/tests/bug1375074.js -@@ -0,0 +1,18 @@ -+// This forces the VM to start creating unboxed objects and thus stresses a -+// particular path into generated code for a specialized unboxed object -+// constructor. -+ -+var K = 2000; // 2000 should be plenty -+var s = "["; -+var i; -+for ( i=0; i < K-1; i++ ) -+ s = s + `{"i":${i}},`; -+s += `{"i":${i}}]`; -+var v = JSON.parse(s); -+ -+assertEq(v.length == K, true); -+ -+for ( i=0; i < K; i++) { -+ assertEq(v[i] instanceof Object, true); -+ assertEq(v[i].i, i); -+} -diff --git js/src/vm/UnboxedObject.cpp js/src/vm/UnboxedObject.cpp -index c8c178965c488..5badf328ede4f 100644 ---- js/src/vm/UnboxedObject.cpp -+++ js/src/vm/UnboxedObject.cpp -@@ -95,7 +95,15 @@ UnboxedLayout::makeConstructorCode(JSContext* cx, HandleObjectGroup group) - #endif - - #ifdef JS_CODEGEN_ARM64 -- // ARM64 communicates stack address via sp, but uses a pseudo-sp for addressing. -+ // ARM64 communicates stack address via sp, but uses a pseudo-sp (PSP) for -+ // addressing. The register we use for PSP may however also be used by -+ // calling code, and it is nonvolatile, so save it. Do this as a special -+ // case first because the generic save/restore code needs the PSP to be -+ // initialized already. -+ MOZ_ASSERT(PseudoStackPointer64.Is(masm.GetStackPointer64())); -+ masm.Str(PseudoStackPointer64, vixl::MemOperand(sp, -16, vixl::PreIndex)); -+ -+ // Initialize the PSP from the SP. - masm.initStackPtr(); - #endif - -@@ -233,7 +241,22 @@ UnboxedLayout::makeConstructorCode(JSContext* cx, HandleObjectGroup group) - masm.pop(ScratchDoubleReg); - masm.PopRegsInMask(savedNonVolatileRegisters); - -+#ifdef JS_CODEGEN_ARM64 -+ // Now restore the value that was in the PSP register on entry, and return. -+ -+ // Obtain the correct SP from the PSP. -+ masm.Mov(sp, PseudoStackPointer64); -+ -+ // Restore the saved value of the PSP register, this value is whatever the -+ // caller had saved in it, not any actual SP value, and it must not be -+ // overwritten subsequently. -+ masm.Ldr(PseudoStackPointer64, vixl::MemOperand(sp, 16, vixl::PostIndex)); -+ -+ // Perform a plain Ret(), as abiret() will move SP <- PSP and that is wrong. -+ masm.Ret(vixl::lr); -+#else - masm.abiret(); -+#endif - - masm.bind(&failureStoreOther); - Property changes on: head/www/firefox/files/patch-bug1375074 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1456556 =================================================================== --- head/www/firefox/files/patch-bug1456556 (revision 472726) +++ head/www/firefox/files/patch-bug1456556 (nonexistent) @@ -1,22 +0,0 @@ -commit f6fc0b418aa3 -Author: Andrea Marchesini -Date: Tue May 1 08:47:13 2018 +0200 - - Bug 1456556 - FetchConsumer scope-exit RAII must grab 'self' by value, r=erahm ---- - dom/fetch/FetchConsumer.cpp | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git dom/fetch/FetchConsumer.cpp dom/fetch/FetchConsumer.cpp -index 134bf7f2e703a..3c3687fea1d5a 100644 ---- dom/fetch/FetchConsumer.cpp -+++ dom/fetch/FetchConsumer.cpp -@@ -582,7 +582,7 @@ FetchBodyConsumer::ContinueConsumeBody(nsresult aStatus, - RefPtr localPromise = mConsumePromise.forget(); - - RefPtr> self = this; -- auto autoReleaseObject = mozilla::MakeScopeExit([&] { -+ auto autoReleaseObject = mozilla::MakeScopeExit([self] { - self->ReleaseObject(); - }); - Property changes on: head/www/firefox/files/patch-bug1456556 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1444798 =================================================================== --- head/www/firefox/files/patch-bug1444798 (revision 472726) +++ head/www/firefox/files/patch-bug1444798 (nonexistent) @@ -1,45 +0,0 @@ -commit 7871e211ec43 -Author: sotaro -Date: Fri Mar 16 21:03:59 2018 +0900 - - Bug 1444798 - Fix ExternalImageCallback func types r=jrmuizel ---- - gfx/webrender_bindings/src/bindings.rs | 11 +++++++---- - 1 file changed, 7 insertions(+), 4 deletions(-) - -diff --git gfx/webrender_bindings/src/bindings.rs gfx/webrender_bindings/src/bindings.rs -index 8517d7dd33e5..cf885ee713f3 100644 ---- gfx/webrender_bindings/src/bindings.rs -+++ gfx/webrender_bindings/src/bindings.rs -@@ -312,8 +312,8 @@ struct WrExternalImage { - size: usize, - } - --type LockExternalImageCallback = fn(*mut c_void, WrExternalImageId, u8) -> WrExternalImage; --type UnlockExternalImageCallback = fn(*mut c_void, WrExternalImageId, u8); -+type LockExternalImageCallback = unsafe extern "C" fn(*mut c_void, WrExternalImageId, u8) -> WrExternalImage; -+type UnlockExternalImageCallback = unsafe extern "C" fn(*mut c_void, WrExternalImageId, u8); - - #[repr(C)] - pub struct WrExternalImageHandler { -@@ -327,7 +327,8 @@ impl ExternalImageHandler for WrExternalImageHandler { - id: ExternalImageId, - channel_index: u8) - -> ExternalImage { -- let image = (self.lock_func)(self.external_image_obj, id.into(), channel_index); -+ -+ let image = unsafe { (self.lock_func)(self.external_image_obj, id.into(), channel_index) }; - ExternalImage { - uv: TexelRect::new(image.u0, image.v0, image.u1, image.v1), - source: match image.image_type { -@@ -341,7 +342,9 @@ impl ExternalImageHandler for WrExternalImageHandler { - fn unlock(&mut self, - id: ExternalImageId, - channel_index: u8) { -- (self.unlock_func)(self.external_image_obj, id.into(), channel_index); -+ unsafe { -+ (self.unlock_func)(self.external_image_obj, id.into(), channel_index); -+ } - } - } - Property changes on: head/www/firefox/files/patch-bug1444798 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1447359 =================================================================== --- head/www/firefox/files/patch-bug1447359 (revision 472726) +++ head/www/firefox/files/patch-bug1447359 (nonexistent) @@ -1,45 +0,0 @@ -commit fef467b5a93d -Author: Luke Wagner -Date: Thu Mar 22 10:28:13 2018 -0500 - - Bug 1447359 - Baldr: add AutoForbidPools in a few missing places (r=lth) - - --HG-- - extra : rebase_source : 75f1909deb352391529ce5e58a89e5f9cfeb3662 ---- - js/src/jit/arm64/MacroAssembler-arm64-inl.h | 1 + - js/src/jit/arm64/MacroAssembler-arm64.cpp | 2 ++ - 2 files changed, 3 insertions(+) - -diff --git js/src/jit/arm64/MacroAssembler-arm64-inl.h js/src/jit/arm64/MacroAssembler-arm64-inl.h -index 7061cbfd93ebb..190442f7afa0a 100644 ---- js/src/jit/arm64/MacroAssembler-arm64-inl.h -+++ js/src/jit/arm64/MacroAssembler-arm64-inl.h -@@ -359,6 +359,7 @@ MacroAssembler::sub32FromStackPtrWithPatch(Register dest) - { - vixl::UseScratchRegisterScope temps(this); - const ARMRegister scratch = temps.AcquireX(); -+ AutoForbidPools afp(this, /* max number of instructions in scope = */ 3); - CodeOffset offs = CodeOffset(currentOffset()); - movz(scratch, 0, 0); - movk(scratch, 0, 16); -diff --git js/src/jit/arm64/MacroAssembler-arm64.cpp js/src/jit/arm64/MacroAssembler-arm64.cpp -index 7b599b7e9d610..4e8fdb6a67019 100644 ---- js/src/jit/arm64/MacroAssembler-arm64.cpp -+++ js/src/jit/arm64/MacroAssembler-arm64.cpp -@@ -707,6 +707,7 @@ MacroAssembler::patchFarJump(CodeOffset farJump, uint32_t targetOffset) - CodeOffset - MacroAssembler::nopPatchableToCall(const wasm::CallSiteDesc& desc) - { -+ AutoForbidPools afp(this, /* max number of instructions in scope = */ 1); - CodeOffset offset(currentOffset()); - Nop(); - append(desc, CodeOffset(currentOffset())); -@@ -1077,6 +1078,7 @@ MacroAssembler::comment(const char* msg) - CodeOffset - MacroAssembler::wasmTrapInstruction() - { -+ AutoForbidPools afp(this, /* max number of instructions in scope = */ 1); - CodeOffset offs(currentOffset()); - Unreachable(); - return offs; Property changes on: head/www/firefox/files/patch-bug1447359 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-bug1438678 =================================================================== --- head/www/firefox/files/patch-bug1438678 (revision 472726) +++ head/www/firefox/files/patch-bug1438678 (nonexistent) @@ -1,1000 +0,0 @@ -commit 68124009fc5a -Author: Nicholas Nethercote -Date: Fri Feb 16 17:54:16 2018 +1100 - - Bug 1438678 - Pass early prefs via shared memory instead of the command line. r=bobowen,jld,glandium. - - This patch replaces the large -intPrefs/-boolPrefs/-stringPrefs flags with - a short-lived, anonymous, shared memory segment that is used to pass the early - prefs. - - Removing the bloat from the command line is nice, but more important is the - fact that this will let us pass more prefs at content process start-up, which - will allow us to remove the early/late prefs split (bug 1436911). - - Although this mechanism is only used for prefs, it's conceivable that it could - be used for other data that must be received very early by children, and for - which the command line isn't ideal. - - Notable details: - - - Much of the patch deals with the various platform-specific ways of passing - handles/fds to children. - - - Linux and Mac: we use a fixed fd (8) in combination with the new - GeckoChildProcessHost::AddFdToRemap() function (which ensures the child - won't close the fd). - - - Android: like Linux and Mac, but the handles get passed via "parcels" and - we use the new SetPrefsFd() function instead of the fixed fd. - - - Windows: there is no need to duplicate the handle because Windows handles - are system-wide. But we do use the new - GeckoChildProcessHost::AddHandleToShare() function to add it to the list of - inheritable handles. We also ensure that list is processed on all paths - (MOZ_SANDBOX with sandbox, MOZ_SANDBOX without sandbox, non-MOZ_SANDBOX) so - that the handles are marked as inheritable. The handle is passed via the - -prefsHandle flag. - - The -prefsLen flag is used on all platforms to indicate the size of the - shared memory segment. - - - The patch also moves the serialization/deserialization of the prefs in/out of - the shared memory into libpref, which is a better spot for it. (This means - Preferences::MustSendToContentProcesses() can be removed.) - - MozReview-Commit-ID: 8fREEBiYFvc - - --HG-- - extra : rebase_source : 7e4c8ebdbcd7d74d6bd2ab3c9e75a6a17dbd8dfe ---- - dom/ipc/ContentParent.cpp | 91 +++++++------- - dom/ipc/ContentProcess.cpp | 121 ++++++++++--------- - dom/ipc/ContentProcess.h | 5 + - ipc/chromium/src/base/process_util_win.cc | 4 + - ipc/glue/GeckoChildProcessHost.cpp | 36 +++--- - ipc/glue/GeckoChildProcessHost.h | 10 ++ - .../org/mozilla/gecko/process/IChildProcess.aidl | 3 +- - .../main/java/org/mozilla/gecko/GeckoThread.java | 13 +- - .../org/mozilla/gecko/mozglue/GeckoLoader.java | 2 +- - .../mozilla/gecko/process/GeckoProcessManager.java | 19 +-- - .../gecko/process/GeckoServiceChildProcess.java | 4 +- - modules/libpref/Preferences.cpp | 134 +++++++++++++++++++-- - modules/libpref/Preferences.h | 17 +-- - mozglue/android/APKOpen.cpp | 4 +- - toolkit/xre/Bootstrap.cpp | 4 +- - toolkit/xre/Bootstrap.h | 2 +- - toolkit/xre/nsEmbedFunctions.cpp | 3 +- - widget/android/GeneratedJNIWrappers.cpp | 4 +- - widget/android/GeneratedJNIWrappers.h | 5 +- - xpcom/build/nsXULAppAPI.h | 2 +- - 20 files changed, 318 insertions(+), 165 deletions(-) - -diff --git dom/ipc/ContentParent.cpp dom/ipc/ContentParent.cpp -index e27f3eedc1b1..60be7005354b 100644 ---- dom/ipc/ContentParent.cpp -+++ dom/ipc/ContentParent.cpp -@@ -7,6 +7,7 @@ - #include "mozilla/DebugOnly.h" - - #include "base/basictypes.h" -+#include "base/shared_memory.h" - - #include "ContentParent.h" - #include "TabParent.h" -@@ -1998,61 +1999,56 @@ ContentParent::LaunchSubprocess(ProcessPriority aInitialPriority /* = PROCESS_PR - extraArgs.push_back(idStr); - extraArgs.push_back(IsForBrowser() ? "-isForBrowser" : "-notForBrowser"); - -- nsAutoCStringN<1024> boolPrefs; -- nsAutoCStringN<1024> intPrefs; -- nsAutoCStringN<1024> stringPrefs; -+ // Prefs information is passed via anonymous shared memory to avoid bloating -+ // the command line. - -- size_t prefsLen; -- ContentPrefs::GetEarlyPrefs(&prefsLen); -+ // Serialize the early prefs. -+ nsAutoCStringN<1024> prefs; -+ Preferences::SerializeEarlyPreferences(prefs); - -- for (unsigned int i = 0; i < prefsLen; i++) { -- const char* prefName = ContentPrefs::GetEarlyPref(i); -- MOZ_ASSERT(i == 0 || strcmp(prefName, ContentPrefs::GetEarlyPref(i - 1)) > 0, -- "Content process preferences should be sorted alphabetically."); -- -- if (!Preferences::MustSendToContentProcesses(prefName)) { -- continue; -- } -- -- switch (Preferences::GetType(prefName)) { -- case nsIPrefBranch::PREF_INT: -- intPrefs.Append(nsPrintfCString("%u:%d|", i, Preferences::GetInt(prefName))); -- break; -- case nsIPrefBranch::PREF_BOOL: -- boolPrefs.Append(nsPrintfCString("%u:%d|", i, Preferences::GetBool(prefName))); -- break; -- case nsIPrefBranch::PREF_STRING: { -- nsAutoCString value; -- Preferences::GetCString(prefName, value); -- stringPrefs.Append(nsPrintfCString("%u:%d;%s|", i, value.Length(), value.get())); -- } -- break; -- case nsIPrefBranch::PREF_INVALID: -- break; -- default: -- printf("preference type: %x\n", Preferences::GetType(prefName)); -- MOZ_CRASH(); -- } -+ // Set up the shared memory. -+ base::SharedMemory shm; -+ if (!shm.Create("", /* read_only */ false, /* open_existing */ false, -+ prefs.Length())) { -+ NS_ERROR("failed to create shared memory in the parent"); -+ MarkAsDead(); -+ return false; -+ } -+ if (!shm.Map(prefs.Length())) { -+ NS_ERROR("failed to map shared memory in the parent"); -+ MarkAsDead(); -+ return false; - } - -- nsCString schedulerPrefs = Scheduler::GetPrefs(); -+ // Copy the serialized prefs into the shared memory. -+ memcpy(static_cast(shm.memory()), prefs.get(), prefs.Length()); - -- // Only do these ones if they're non-empty. -- if (!intPrefs.IsEmpty()) { -- extraArgs.push_back("-intPrefs"); -- extraArgs.push_back(intPrefs.get()); -- } -- if (!boolPrefs.IsEmpty()) { -- extraArgs.push_back("-boolPrefs"); -- extraArgs.push_back(boolPrefs.get()); -- } -- if (!stringPrefs.IsEmpty()) { -- extraArgs.push_back("-stringPrefs"); -- extraArgs.push_back(stringPrefs.get()); -- } -+#if defined(XP_WIN) -+ // Record the handle as to-be-shared, and pass it via a command flag. This -+ // works because Windows handles are system-wide. -+ HANDLE prefsHandle = shm.handle(); -+ mSubprocess->AddHandleToShare(prefsHandle); -+ extraArgs.push_back("-prefsHandle"); -+ extraArgs.push_back( -+ nsPrintfCString("%zu", reinterpret_cast(prefsHandle)).get()); -+#else -+ // In contrast, Unix fds are per-process. So remap the fd to a fixed one that -+ // will be used in the child. -+ // XXX: bug 1440207 is about improving how fixed fds are used. -+ // -+ // Note: on Android, AddFdToRemap() sets up the fd to be passed via a Parcel, -+ // and the fixed fd isn't used. However, we still need to mark it for -+ // remapping so it doesn't get closed in the child. -+ mSubprocess->AddFdToRemap(shm.handle().fd, kPrefsFileDescriptor); -+#endif -+ -+ // Pass the length via a command flag. -+ extraArgs.push_back("-prefsLen"); -+ extraArgs.push_back(nsPrintfCString("%zu", uintptr_t(prefs.Length())).get()); - - // Scheduler prefs need to be handled differently because the scheduler needs - // to start up in the content process before the normal preferences service. -+ nsCString schedulerPrefs = Scheduler::GetPrefs(); - extraArgs.push_back("-schedulerPrefs"); - extraArgs.push_back(schedulerPrefs.get()); - -@@ -2061,6 +2057,7 @@ ContentParent::LaunchSubprocess(ProcessPriority aInitialPriority /* = PROCESS_PR - } - - if (!mSubprocess->LaunchAndWaitForProcessHandle(extraArgs)) { -+ NS_ERROR("failed to launch child in the parent"); - MarkAsDead(); - return false; - } -diff --git dom/ipc/ContentProcess.cpp dom/ipc/ContentProcess.cpp -index e3c1f16910c6..2441c8cb9224 100644 ---- dom/ipc/ContentProcess.cpp -+++ dom/ipc/ContentProcess.cpp -@@ -8,6 +8,8 @@ - - #include "ContentProcess.h" - #include "ContentPrefs.h" -+#include "base/shared_memory.h" -+#include "mozilla/Preferences.h" - #include "mozilla/Scheduler.h" - - #if defined(XP_MACOSX) && defined(MOZ_CONTENT_SANDBOX) -@@ -15,7 +17,6 @@ - #endif - - #if (defined(XP_WIN) || defined(XP_MACOSX)) && defined(MOZ_CONTENT_SANDBOX) --#include "mozilla/Preferences.h" - #include "mozilla/SandboxSettings.h" - #include "nsAppDirectoryServiceDefs.h" - #include "nsDirectoryService.h" -@@ -81,6 +82,16 @@ SetUpSandboxEnvironment() - } - #endif - -+#ifdef ANDROID -+static int gPrefsFd = -1; -+ -+void -+SetPrefsFd(int aFd) -+{ -+ gPrefsFd = aFd; -+} -+#endif -+ - bool - ContentProcess::Init(int aArgc, char* aArgv[]) - { -@@ -88,9 +99,10 @@ ContentProcess::Init(int aArgc, char* aArgv[]) - bool foundAppdir = false; - bool foundChildID = false; - bool foundIsForBrowser = false; -- bool foundIntPrefs = false; -- bool foundBoolPrefs = false; -- bool foundStringPrefs = false; -+#ifdef XP_WIN -+ bool foundPrefsHandle = false; -+#endif -+ bool foundPrefsLen = false; - bool foundSchedulerPrefs = false; - - uint64_t childID; -@@ -103,7 +115,8 @@ ContentProcess::Init(int aArgc, char* aArgv[]) - #endif - - char* schedulerPrefs = nullptr; -- InfallibleTArray prefsArray; -+ base::SharedMemoryHandle prefsHandle = base::SharedMemory::NULLHandle(); -+ size_t prefsLen = 0; - for (int idx = aArgc; idx > 0; idx--) { - if (!aArgv[idx]) { - continue; -@@ -134,54 +147,24 @@ ContentProcess::Init(int aArgc, char* aArgv[]) - } - isForBrowser = strcmp(aArgv[idx], "-notForBrowser"); - foundIsForBrowser = true; -- } else if (!strcmp(aArgv[idx], "-intPrefs")) { -- char* str = aArgv[idx + 1]; -- while (*str) { -- int32_t index = strtol(str, &str, 10); -- MOZ_ASSERT(str[0] == ':'); -- str++; -- MaybePrefValue value(PrefValue(static_cast(strtol(str, &str, 10)))); -- MOZ_ASSERT(str[0] == '|'); -- str++; -- // XXX: we assume these values as default values, which may not be -- // true. We also assume they are unlocked. Fortunately, these prefs -- // get reset properly by the first IPC message. -- Pref pref(nsCString(ContentPrefs::GetEarlyPref(index)), -- /* isLocked */ false, value, MaybePrefValue()); -- prefsArray.AppendElement(pref); -- } -- foundIntPrefs = true; -- } else if (!strcmp(aArgv[idx], "-boolPrefs")) { -+#ifdef XP_WIN -+ } else if (!strcmp(aArgv[idx], "-prefsHandle")) { - char* str = aArgv[idx + 1]; -- while (*str) { -- int32_t index = strtol(str, &str, 10); -- MOZ_ASSERT(str[0] == ':'); -- str++; -- MaybePrefValue value(PrefValue(!!strtol(str, &str, 10))); -- MOZ_ASSERT(str[0] == '|'); -- str++; -- Pref pref(nsCString(ContentPrefs::GetEarlyPref(index)), -- /* isLocked */ false, value, MaybePrefValue()); -- prefsArray.AppendElement(pref); -- } -- foundBoolPrefs = true; -- } else if (!strcmp(aArgv[idx], "-stringPrefs")) { -+ MOZ_ASSERT(str[0] != '\0'); -+ // ContentParent uses %zu to print a word-sized unsigned integer. So even -+ // though strtoull() returns a long long int, it will fit in a uintptr_t. -+ prefsHandle = reinterpret_cast(strtoull(str, &str, 10)); -+ MOZ_ASSERT(str[0] == '\0'); -+ foundPrefsHandle = true; -+#endif -+ } else if (!strcmp(aArgv[idx], "-prefsLen")) { - char* str = aArgv[idx + 1]; -- while (*str) { -- int32_t index = strtol(str, &str, 10); -- MOZ_ASSERT(str[0] == ':'); -- str++; -- int32_t length = strtol(str, &str, 10); -- MOZ_ASSERT(str[0] == ';'); -- str++; -- MaybePrefValue value(PrefValue(nsCString(str, length))); -- Pref pref(nsCString(ContentPrefs::GetEarlyPref(index)), -- /* isLocked */ false, value, MaybePrefValue()); -- prefsArray.AppendElement(pref); -- str += length + 1; -- MOZ_ASSERT(*(str - 1) == '|'); -- } -- foundStringPrefs = true; -+ MOZ_ASSERT(str[0] != '\0'); -+ // ContentParent uses %zu to print a word-sized unsigned integer. So even -+ // though strtoull() returns a long long int, it will fit in a uintptr_t. -+ prefsLen = strtoull(str, &str, 10); -+ MOZ_ASSERT(str[0] == '\0'); -+ foundPrefsLen = true; - } else if (!strcmp(aArgv[idx], "-schedulerPrefs")) { - schedulerPrefs = aArgv[idx + 1]; - foundSchedulerPrefs = true; -@@ -209,21 +192,43 @@ ContentProcess::Init(int aArgc, char* aArgv[]) - bool allFound = foundAppdir - && foundChildID - && foundIsForBrowser -- && foundIntPrefs -- && foundBoolPrefs -- && foundStringPrefs -- && foundSchedulerPrefs; -- -+ && foundPrefsLen -+ && foundSchedulerPrefs -+#ifdef XP_WIN -+ && foundPrefsHandle -+#endif - #if defined(XP_MACOSX) && defined(MOZ_CONTENT_SANDBOX) -- allFound &= foundProfile; -+ && foundProfile - #endif -+ && true; - - if (allFound) { - break; - } - } - -- Preferences::SetEarlyPreferences(&prefsArray); -+#ifdef ANDROID -+ // Android is different; get the FD via gPrefsFd instead of a fixed fd. -+ MOZ_RELEASE_ASSERT(gPrefsFd != -1); -+ prefsHandle = base::FileDescriptor(gPrefsFd, /* auto_close */ true); -+#elif XP_UNIX -+ prefsHandle = base::FileDescriptor(kPrefsFileDescriptor, -+ /* auto_close */ true); -+#endif -+ -+ // Set up early prefs from the shared memory. -+ base::SharedMemory shm; -+ if (!shm.SetHandle(prefsHandle, /* read_only */ true)) { -+ NS_ERROR("failed to open shared memory in the child"); -+ return false; -+ } -+ if (!shm.Map(prefsLen)) { -+ NS_ERROR("failed to map shared memory in the child"); -+ return false; -+ } -+ Preferences::DeserializeEarlyPreferences(static_cast(shm.memory()), -+ prefsLen); -+ - Scheduler::SetPrefs(schedulerPrefs); - mContent.Init(IOThreadChild::message_loop(), - ParentPid(), -diff --git dom/ipc/ContentProcess.h dom/ipc/ContentProcess.h -index a3854c761e10..6582c94da496 100644 ---- dom/ipc/ContentProcess.h -+++ dom/ipc/ContentProcess.h -@@ -49,6 +49,11 @@ private: - DISALLOW_EVIL_CONSTRUCTORS(ContentProcess); - }; - -+#ifdef ANDROID -+// Android doesn't use -prefsHandle, it gets that FD another way. -+void SetPrefsFd(int aFd); -+#endif -+ - } // namespace dom - } // namespace mozilla - -diff --git ipc/chromium/src/base/process_util_win.cc ipc/chromium/src/base/process_util_win.cc -index 3ed54cd744ac..46667985cd71 100644 ---- ipc/chromium/src/base/process_util_win.cc -+++ ipc/chromium/src/base/process_util_win.cc -@@ -354,6 +354,10 @@ bool LaunchApp(const std::wstring& cmdline, - LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL; - std::vector handlesToInherit; - for (HANDLE h : options.handles_to_inherit) { -+ if (SetHandleInformation(h, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) == 0) { -+ MOZ_DIAGNOSTIC_ASSERT(false, "SetHandleInformation failed"); -+ return false; -+ } - handlesToInherit.push_back(h); - } - -diff --git ipc/glue/GeckoChildProcessHost.cpp ipc/glue/GeckoChildProcessHost.cpp -index d18ed9edd4ca..3be1c51d10bb 100644 ---- ipc/glue/GeckoChildProcessHost.cpp -+++ ipc/glue/GeckoChildProcessHost.cpp -@@ -1030,9 +1030,6 @@ GeckoChildProcessHost::PerformAsyncLaunchInternal(std::vector& aExt - - if (!CrashReporter::IsDummy()) { - PROsfd h = PR_FileDesc2NativeHandle(crashAnnotationWritePipe); --# if defined(MOZ_SANDBOX) -- mSandboxBroker.AddHandleToShare(reinterpret_cast(h)); --# endif // defined(MOZ_SANDBOX) - mLaunchOptions->handles_to_inherit.push_back(reinterpret_cast(h)); - std::string hStr = std::to_string(h); - cmdLine.AppendLooseValue(UTF8ToWide(hStr)); -@@ -1043,6 +1040,11 @@ GeckoChildProcessHost::PerformAsyncLaunchInternal(std::vector& aExt - - # if defined(MOZ_SANDBOX) - if (shouldSandboxCurrentProcess) { -+ // Mark the handles to inherit as inheritable. -+ for (HANDLE h : mLaunchOptions->handles_to_inherit) { -+ mSandboxBroker.AddHandleToShare(h); -+ } -+ - if (mSandboxBroker.LaunchApp(cmdLine.program().c_str(), - cmdLine.command_line_string().c_str(), - mLaunchOptions->env_map, -@@ -1180,7 +1182,7 @@ GeckoChildProcessHost::LaunchAndroidService(const char* type, - const base::file_handle_mapping_vector& fds_to_remap, - ProcessHandle* process_handle) - { -- MOZ_ASSERT((fds_to_remap.size() > 0) && (fds_to_remap.size() <= 3)); -+ MOZ_RELEASE_ASSERT((2 <= fds_to_remap.size()) && (fds_to_remap.size() <= 4)); - JNIEnv* const env = mozilla::jni::GetEnvForThread(); - MOZ_ASSERT(env); - -@@ -1189,21 +1191,25 @@ GeckoChildProcessHost::LaunchAndroidService(const char* type, - for (int ix = 0; ix < argvSize; ix++) { - jargs->SetElement(ix, jni::StringParam(argv[ix].c_str(), env)); - } -- base::file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); -- int32_t ipcFd = it->first; -- it++; -- // If the Crash Reporter is disabled, there will not be a second file descriptor. -+ -+ // XXX: this processing depends entirely on the internals of -+ // ContentParent::LaunchSubprocess() -+ // GeckoChildProcessHost::PerformAsyncLaunchInternal(), and the order in -+ // which they append to fds_to_remap. There must be a better way to do it. -+ // See bug 1440207. -+ int32_t prefsFd = fds_to_remap[0].first; -+ int32_t ipcFd = fds_to_remap[1].first; - int32_t crashFd = -1; - int32_t crashAnnotationFd = -1; -- if (it != fds_to_remap.end() && !CrashReporter::IsDummy()) { -- crashFd = it->first; -- it++; -+ if (fds_to_remap.size() == 3) { -+ crashAnnotationFd = fds_to_remap[2].first; - } -- if (it != fds_to_remap.end()) { -- crashAnnotationFd = it->first; -- it++; -+ if (fds_to_remap.size() == 4) { -+ crashFd = fds_to_remap[2].first; -+ crashAnnotationFd = fds_to_remap[3].first; - } -- int32_t handle = java::GeckoProcessManager::Start(type, jargs, ipcFd, crashFd, crashAnnotationFd); -+ -+ int32_t handle = java::GeckoProcessManager::Start(type, jargs, prefsFd, ipcFd, crashFd, crashAnnotationFd); - - if (process_handle) { - *process_handle = handle; -diff --git ipc/glue/GeckoChildProcessHost.h ipc/glue/GeckoChildProcessHost.h -index 631c42066bc7..0345e221abcc 100644 ---- ipc/glue/GeckoChildProcessHost.h -+++ ipc/glue/GeckoChildProcessHost.h -@@ -103,6 +103,16 @@ public: - } - #endif - -+#ifdef XP_WIN -+ void AddHandleToShare(HANDLE aHandle) { -+ mLaunchOptions->handles_to_inherit.push_back(aHandle); -+ } -+#else -+ void AddFdToRemap(int aSrcFd, int aDstFd) { -+ mLaunchOptions->fds_to_remap.push_back(std::make_pair(aSrcFd, aDstFd)); -+ } -+#endif -+ - /** - * Must run on the IO thread. Cause the OS process to exit and - * ensure its OS resources are cleaned up. -diff --git mobile/android/geckoview/src/main/aidl/org/mozilla/gecko/process/IChildProcess.aidl mobile/android/geckoview/src/main/aidl/org/mozilla/gecko/process/IChildProcess.aidl -index ba26ae1ba06b..a2535f44c72b 100644 ---- mobile/android/geckoview/src/main/aidl/org/mozilla/gecko/process/IChildProcess.aidl -+++ mobile/android/geckoview/src/main/aidl/org/mozilla/gecko/process/IChildProcess.aidl -@@ -12,6 +12,7 @@ import android.os.ParcelFileDescriptor; - interface IChildProcess { - int getPid(); - boolean start(in IProcessManager procMan, in String[] args, in Bundle extras, -- in ParcelFileDescriptor ipcPfd, in ParcelFileDescriptor crashReporterPfd, -+ in ParcelFileDescriptor prefsPfd, in ParcelFileDescriptor ipcPfd, -+ in ParcelFileDescriptor crashReporterPfd, - in ParcelFileDescriptor crashAnnotationPfd); - } -diff --git mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoThread.java mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoThread.java -index dfabfd05daf0..8311920afeec 100644 ---- mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoThread.java -+++ mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoThread.java -@@ -128,6 +128,7 @@ public class GeckoThread extends Thread { - public static final int FLAG_PRELOAD_CHILD = 2; // Preload child during main thread start. - - private static final String EXTRA_ARGS = "args"; -+ private static final String EXTRA_PREFS_FD = "prefsFd"; - private static final String EXTRA_IPC_FD = "ipcFd"; - private static final String EXTRA_CRASH_FD = "crashFd"; - private static final String EXTRA_CRASH_ANNOTATION_FD = "crashAnnotationFd"; -@@ -149,7 +150,8 @@ public class GeckoThread extends Thread { - - private synchronized boolean init(final GeckoProfile profile, final String[] args, - final Bundle extras, final int flags, -- final int ipcFd, final int crashFd, -+ final int prefsFd, final int ipcFd, -+ final int crashFd, - final int crashAnnotationFd) { - ThreadUtils.assertOnUiThread(); - uiThreadId = android.os.Process.myTid(); -@@ -163,6 +165,7 @@ public class GeckoThread extends Thread { - mFlags = flags; - - mExtras = (extras != null) ? new Bundle(extras) : new Bundle(3); -+ mExtras.putInt(EXTRA_PREFS_FD, prefsFd); - mExtras.putInt(EXTRA_IPC_FD, ipcFd); - mExtras.putInt(EXTRA_CRASH_FD, crashFd); - mExtras.putInt(EXTRA_CRASH_ANNOTATION_FD, crashAnnotationFd); -@@ -174,15 +177,16 @@ public class GeckoThread extends Thread { - - public static boolean initMainProcess(final GeckoProfile profile, final String[] args, - final Bundle extras, final int flags) { -- return INSTANCE.init(profile, args, extras, flags, -+ return INSTANCE.init(profile, args, extras, flags, /* fd */ -1, - /* fd */ -1, /* fd */ -1, /* fd */ -1); - } - - public static boolean initChildProcess(final String[] args, final Bundle extras, -- final int ipcFd, final int crashFd, -+ final int prefsFd, final int ipcFd, -+ final int crashFd, - final int crashAnnotationFd) { - return INSTANCE.init(/* profile */ null, args, extras, /* flags */ 0, -- ipcFd, crashFd, crashAnnotationFd); -+ prefsFd, ipcFd, crashFd, crashAnnotationFd); - } - - private static boolean canUseProfile(final Context context, final GeckoProfile profile, -@@ -442,6 +446,7 @@ public class GeckoThread extends Thread { - - // And go. - GeckoLoader.nativeRun(args, -+ mExtras.getInt(EXTRA_PREFS_FD, -1), - mExtras.getInt(EXTRA_IPC_FD, -1), - mExtras.getInt(EXTRA_CRASH_FD, -1), - mExtras.getInt(EXTRA_CRASH_ANNOTATION_FD, -1)); -diff --git mobile/android/geckoview/src/main/java/org/mozilla/gecko/mozglue/GeckoLoader.java mobile/android/geckoview/src/main/java/org/mozilla/gecko/mozglue/GeckoLoader.java -index b1830fd86945..ac128b651e7b 100644 ---- mobile/android/geckoview/src/main/java/org/mozilla/gecko/mozglue/GeckoLoader.java -+++ mobile/android/geckoview/src/main/java/org/mozilla/gecko/mozglue/GeckoLoader.java -@@ -463,7 +463,7 @@ public final class GeckoLoader { - public static native boolean verifyCRCs(String apkName); - - // These methods are implemented in mozglue/android/APKOpen.cpp -- public static native void nativeRun(String[] args, int ipcFd, int crashFd, int crashAnnotationFd); -+ public static native void nativeRun(String[] args, int prefsFd, int ipcFd, int crashFd, int crashAnnotationFd); - private static native void loadGeckoLibsNative(String apkName); - private static native void loadSQLiteLibsNative(String apkName); - private static native void loadNSSLibsNative(String apkName); -diff --git mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoProcessManager.java mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoProcessManager.java -index b762e1c9a3eb..dba329ba8f92 100644 ---- mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoProcessManager.java -+++ mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoProcessManager.java -@@ -169,14 +169,14 @@ public final class GeckoProcessManager extends IProcessManager.Stub { - - @WrapForJNI - private static int start(final String type, final String[] args, -- final int ipcFd, final int crashFd, -- final int crashAnnotationFd) { -- return INSTANCE.start(type, args, ipcFd, crashFd, crashAnnotationFd, /* retry */ false); -+ final int prefsFd, final int ipcFd, -+ final int crashFd, final int crashAnnotationFd) { -+ return INSTANCE.start(type, args, prefsFd, ipcFd, crashFd, crashAnnotationFd, /* retry */ false); - } - -- private int start(final String type, final String[] args, final int ipcFd, -- final int crashFd, final int crashAnnotationFd, -- final boolean retry) { -+ private int start(final String type, final String[] args, final int prefsFd, -+ final int ipcFd, final int crashFd, -+ final int crashAnnotationFd, final boolean retry) { - final ChildConnection connection = getConnection(type); - final IChildProcess child = connection.bind(); - if (child == null) { -@@ -184,10 +184,12 @@ public final class GeckoProcessManager extends IProcessManager.Stub { - } - - final Bundle extras = GeckoThread.getActiveExtras(); -+ final ParcelFileDescriptor prefsPfd; - final ParcelFileDescriptor ipcPfd; - final ParcelFileDescriptor crashPfd; - final ParcelFileDescriptor crashAnnotationPfd; - try { -+ prefsPfd = ParcelFileDescriptor.fromFd(prefsFd); - ipcPfd = ParcelFileDescriptor.fromFd(ipcFd); - crashPfd = (crashFd >= 0) ? ParcelFileDescriptor.fromFd(crashFd) : null; - crashAnnotationPfd = (crashAnnotationFd >= 0) ? ParcelFileDescriptor.fromFd(crashAnnotationFd) : null; -@@ -198,7 +200,8 @@ public final class GeckoProcessManager extends IProcessManager.Stub { - - boolean started = false; - try { -- started = child.start(this, args, extras, ipcPfd, crashPfd, crashAnnotationPfd); -+ started = child.start(this, args, extras, prefsPfd, ipcPfd, crashPfd, -+ crashAnnotationPfd); - } catch (final RemoteException e) { - } - -@@ -209,7 +212,7 @@ public final class GeckoProcessManager extends IProcessManager.Stub { - } - Log.w(LOGTAG, "Attempting to kill running child " + type); - connection.unbind(); -- return start(type, args, ipcFd, crashFd, crashAnnotationFd, /* retry */ true); -+ return start(type, args, prefsFd, ipcFd, crashFd, crashAnnotationFd, /* retry */ true); - } - - try { -diff --git mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java -index f1f6ce109fda..6dc19813fc10 100644 ---- mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java -+++ mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/GeckoServiceChildProcess.java -@@ -63,6 +63,7 @@ public class GeckoServiceChildProcess extends Service { - public boolean start(final IProcessManager procMan, - final String[] args, - final Bundle extras, -+ final ParcelFileDescriptor prefsPfd, - final ParcelFileDescriptor ipcPfd, - final ParcelFileDescriptor crashReporterPfd, - final ParcelFileDescriptor crashAnnotationPfd) { -@@ -74,6 +75,7 @@ public class GeckoServiceChildProcess extends Service { - sProcessManager = procMan; - } - -+ final int prefsFd = prefsPfd.detachFd(); - final int ipcFd = ipcPfd.detachFd(); - final int crashReporterFd = crashReporterPfd != null ? - crashReporterPfd.detachFd() : -1; -@@ -83,7 +85,7 @@ public class GeckoServiceChildProcess extends Service { - ThreadUtils.postToUiThread(new Runnable() { - @Override - public void run() { -- if (GeckoThread.initChildProcess(args, extras, ipcFd, crashReporterFd, -+ if (GeckoThread.initChildProcess(args, extras, prefsFd, ipcFd, crashReporterFd, - crashAnnotationFd)) { - GeckoThread.launch(); - } -diff --git modules/libpref/Preferences.cpp modules/libpref/Preferences.cpp -index 330ed4a09b54..b884591c9271 100644 ---- modules/libpref/Preferences.cpp -+++ modules/libpref/Preferences.cpp -@@ -2920,7 +2920,7 @@ public: - - } // namespace - --// A list of prefs sent early from the parent, via the command line. -+// A list of prefs sent early from the parent, via shared memory. - static InfallibleTArray* gEarlyDomPrefs; - - /* static */ already_AddRefed -@@ -3081,11 +3081,130 @@ NS_IMPL_ISUPPORTS(Preferences, - nsISupportsWeakReference) - - /* static */ void --Preferences::SetEarlyPreferences(const nsTArray* aDomPrefs) -+Preferences::SerializeEarlyPreferences(nsCString& aStr) -+{ -+ MOZ_RELEASE_ASSERT(InitStaticMembers()); -+ -+ nsAutoCStringN<256> boolPrefs, intPrefs, stringPrefs; -+ size_t numEarlyPrefs; -+ dom::ContentPrefs::GetEarlyPrefs(&numEarlyPrefs); -+ -+ for (unsigned int i = 0; i < numEarlyPrefs; i++) { -+ const char* prefName = dom::ContentPrefs::GetEarlyPref(i); -+ MOZ_ASSERT_IF(i > 0, -+ strcmp(prefName, dom::ContentPrefs::GetEarlyPref(i - 1)) > 0); -+ -+ Pref* pref = pref_HashTableLookup(prefName); -+ if (!pref || !pref->MustSendToContentProcesses()) { -+ continue; -+ } -+ -+ switch (pref->Type()) { -+ case PrefType::Bool: -+ boolPrefs.Append( -+ nsPrintfCString("%u:%d|", i, Preferences::GetBool(prefName))); -+ break; -+ case PrefType::Int: -+ intPrefs.Append( -+ nsPrintfCString("%u:%d|", i, Preferences::GetInt(prefName))); -+ break; -+ case PrefType::String: { -+ nsAutoCString value; -+ Preferences::GetCString(prefName, value); -+ stringPrefs.Append( -+ nsPrintfCString("%u:%d;%s|", i, value.Length(), value.get())); -+ } break; -+ case PrefType::None: -+ break; -+ default: -+ printf_stderr("preference type: %d\n", int(pref->Type())); -+ MOZ_CRASH(); -+ } -+ } -+ -+ aStr.Truncate(); -+ aStr.Append(boolPrefs); -+ aStr.Append('\n'); -+ aStr.Append(intPrefs); -+ aStr.Append('\n'); -+ aStr.Append(stringPrefs); -+ aStr.Append('\n'); -+ aStr.Append('\0'); -+} -+ -+/* static */ void -+Preferences::DeserializeEarlyPreferences(char* aStr, size_t aStrLen) - { - MOZ_ASSERT(!XRE_IsParentProcess()); - -- gEarlyDomPrefs = new InfallibleTArray(mozilla::Move(*aDomPrefs)); -+ MOZ_ASSERT(!gEarlyDomPrefs); -+ gEarlyDomPrefs = new InfallibleTArray(); -+ -+ char* p = aStr; -+ -+ // XXX: we assume these pref values are default values, which may not be -+ // true. We also assume they are unlocked. Fortunately, these prefs get reset -+ // properly by the first IPC message. -+ -+ // Get the bool prefs. -+ while (*p != '\n') { -+ int32_t index = strtol(p, &p, 10); -+ MOZ_ASSERT(p[0] == ':'); -+ p++; -+ int v = strtol(p, &p, 10); -+ MOZ_ASSERT(v == 0 || v == 1); -+ dom::MaybePrefValue value(dom::PrefValue(!!v)); -+ MOZ_ASSERT(p[0] == '|'); -+ p++; -+ dom::Pref pref(nsCString(dom::ContentPrefs::GetEarlyPref(index)), -+ /* isLocked */ false, -+ value, -+ dom::MaybePrefValue()); -+ gEarlyDomPrefs->AppendElement(pref); -+ } -+ p++; -+ -+ // Get the int prefs. -+ while (*p != '\n') { -+ int32_t index = strtol(p, &p, 10); -+ MOZ_ASSERT(p[0] == ':'); -+ p++; -+ dom::MaybePrefValue value( -+ dom::PrefValue(static_cast(strtol(p, &p, 10)))); -+ MOZ_ASSERT(p[0] == '|'); -+ p++; -+ dom::Pref pref(nsCString(dom::ContentPrefs::GetEarlyPref(index)), -+ /* isLocked */ false, -+ value, -+ dom::MaybePrefValue()); -+ gEarlyDomPrefs->AppendElement(pref); -+ } -+ p++; -+ -+ // Get the string prefs. -+ while (*p != '\n') { -+ int32_t index = strtol(p, &p, 10); -+ MOZ_ASSERT(p[0] == ':'); -+ p++; -+ int32_t length = strtol(p, &p, 10); -+ MOZ_ASSERT(p[0] == ';'); -+ p++; -+ dom::MaybePrefValue value(dom::PrefValue(nsCString(p, length))); -+ dom::Pref pref(nsCString(dom::ContentPrefs::GetEarlyPref(index)), -+ /* isLocked */ false, -+ value, -+ dom::MaybePrefValue()); -+ gEarlyDomPrefs->AppendElement(pref); -+ p += length + 1; -+ MOZ_ASSERT(*(p - 1) == '|'); -+ } -+ p++; -+ -+ MOZ_ASSERT(*p == '\0'); -+ -+ // We finished parsing on a '\0'. That should be the last char in the shared -+ // memory. -+ MOZ_ASSERT(aStr + aStrLen - 1 == p); - - #ifdef DEBUG - MOZ_ASSERT(gPhase == ContentProcessPhase::eNoPrefsSet); -@@ -4298,15 +4417,6 @@ Preferences::HasUserValue(const char* aPrefName) - return pref && pref->HasUserValue(); - } - --/* static */ bool --Preferences::MustSendToContentProcesses(const char* aPrefName) --{ -- NS_ENSURE_TRUE(InitStaticMembers(), false); -- -- Pref* pref = pref_HashTableLookup(aPrefName); -- return pref && pref->MustSendToContentProcesses(); --} -- - /* static */ int32_t - Preferences::GetType(const char* aPrefName) - { -diff --git modules/libpref/Preferences.h modules/libpref/Preferences.h -index 1cb825ecbfe5..c149db62b525 100644 ---- modules/libpref/Preferences.h -+++ modules/libpref/Preferences.h -@@ -41,6 +41,11 @@ class PrefValue; - - struct PrefsSizes; - -+#ifdef XP_UNIX -+// XXX: bug 1440207 is about improving how fixed fds such as this are used. -+static const int kPrefsFileDescriptor = 8; -+#endif -+ - // Keep this in sync with PrefType in parser/src/lib.rs. - enum class PrefValueKind : uint8_t - { -@@ -230,9 +235,6 @@ public: - // Whether the pref has a user value or not. - static bool HasUserValue(const char* aPref); - -- // Must the pref be sent to content processes when they start? -- static bool MustSendToContentProcesses(const char* aPref); -- - // Adds/Removes the observer for the root pref branch. See nsIPrefBranch.idl - // for details. - static nsresult AddStrongObserver(nsIObserver* aObserver, const char* aPref); -@@ -328,11 +330,12 @@ public: - - // When a content process is created these methods are used to pass prefs in - // bulk from the parent process. "Early" preferences are ones that are needed -- // very early on in the content process's lifetime; they are passed via the -- // command line. "Late" preferences are the remainder, which are passed via -- // IPC message. -+ // very early on in the content process's lifetime; they are passed via a -+ // special shared memory segment. "Late" preferences are the remainder, which -+ // are passed via a standard IPC message. -+ static void SerializeEarlyPreferences(nsCString& aStr); -+ static void DeserializeEarlyPreferences(char* aStr, size_t aStrLen); - static void GetPreferences(InfallibleTArray* aSettings); -- static void SetEarlyPreferences(const nsTArray* aSettings); - static void SetLatePreferences(const nsTArray* aSettings); - - // When a single pref is changed in the parent process, these methods are -diff --git mozglue/android/APKOpen.cpp mozglue/android/APKOpen.cpp -index 5f1ef55b605e..b57192488725 100644 ---- mozglue/android/APKOpen.cpp -+++ mozglue/android/APKOpen.cpp -@@ -392,7 +392,7 @@ FreeArgv(char** argv, int argc) - } - - extern "C" APKOPEN_EXPORT void MOZ_JNICALL --Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun(JNIEnv *jenv, jclass jc, jobjectArray jargs, int ipcFd, int crashFd, int crashAnnotationFd) -+Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun(JNIEnv *jenv, jclass jc, jobjectArray jargs, int prefsFd, int ipcFd, int crashFd, int crashAnnotationFd) - { - int argc = 0; - char** argv = CreateArgvFromObjectArray(jenv, jargs, &argc); -@@ -407,7 +407,7 @@ Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun(JNIEnv *jenv, jclass jc, jo - gBootstrap->GeckoStart(jenv, argv, argc, sAppData); - ElfLoader::Singleton.ExpectShutdown(true); - } else { -- gBootstrap->XRE_SetAndroidChildFds(jenv, ipcFd, crashFd, crashAnnotationFd); -+ gBootstrap->XRE_SetAndroidChildFds(jenv, prefsFd, ipcFd, crashFd, crashAnnotationFd); - gBootstrap->XRE_SetProcessType(argv[argc - 1]); - - XREChildData childData; -diff --git toolkit/xre/Bootstrap.cpp toolkit/xre/Bootstrap.cpp -index 5688519822a9..7e857969a4fb 100644 ---- toolkit/xre/Bootstrap.cpp -+++ toolkit/xre/Bootstrap.cpp -@@ -78,8 +78,8 @@ public: - ::GeckoStart(aEnv, argv, argc, aAppData); - } - -- virtual void XRE_SetAndroidChildFds(JNIEnv* aEnv, int aIPCFd, int aCrashFd, int aCrashAnnotationFd) override { -- ::XRE_SetAndroidChildFds(aEnv, aIPCFd, aCrashFd, aCrashAnnotationFd); -+ virtual void XRE_SetAndroidChildFds(JNIEnv* aEnv, int aPrefsFd, int aIPCFd, int aCrashFd, int aCrashAnnotationFd) override { -+ ::XRE_SetAndroidChildFds(aEnv, aPrefsFd, aIPCFd, aCrashFd, aCrashAnnotationFd); - } - #endif - -diff --git toolkit/xre/Bootstrap.h toolkit/xre/Bootstrap.h -index 686d0a38e324..77adcef80e1f 100644 ---- toolkit/xre/Bootstrap.h -+++ toolkit/xre/Bootstrap.h -@@ -113,7 +113,7 @@ public: - #ifdef MOZ_WIDGET_ANDROID - virtual void GeckoStart(JNIEnv* aEnv, char** argv, int argc, const StaticXREAppData& aAppData) = 0; - -- virtual void XRE_SetAndroidChildFds(JNIEnv* aEnv, int aIPCFd, int aCrashFd, int aCrashAnnotationFd) = 0; -+ virtual void XRE_SetAndroidChildFds(JNIEnv* aEnv, int aPrefsFd, int aIPCFd, int aCrashFd, int aCrashAnnotationFd) = 0; - #endif - - #ifdef LIBFUZZER -diff --git toolkit/xre/nsEmbedFunctions.cpp toolkit/xre/nsEmbedFunctions.cpp -index 53bd2bc2eb47..83184e97ba92 100644 ---- toolkit/xre/nsEmbedFunctions.cpp -+++ toolkit/xre/nsEmbedFunctions.cpp -@@ -243,9 +243,10 @@ GeckoProcessType sChildProcessType = GeckoProcessType_Default; - - #if defined(MOZ_WIDGET_ANDROID) - void --XRE_SetAndroidChildFds (JNIEnv* env, int ipcFd, int crashFd, int crashAnnotationFd) -+XRE_SetAndroidChildFds (JNIEnv* env, int prefsFd, int ipcFd, int crashFd, int crashAnnotationFd) - { - mozilla::jni::SetGeckoThreadEnv(env); -+ mozilla::dom::SetPrefsFd(prefsFd); - IPC::Channel::SetClientChannelFd(ipcFd); - CrashReporter::SetNotificationPipeForChild(crashFd); - CrashReporter::SetCrashAnnotationPipeForChild(crashAnnotationFd); -diff --git widget/android/GeneratedJNIWrappers.cpp widget/android/GeneratedJNIWrappers.cpp -index e3f6af0cc575..4165df59f0e8 100644 ---- widget/android/GeneratedJNIWrappers.cpp -+++ widget/android/GeneratedJNIWrappers.cpp -@@ -2355,9 +2355,9 @@ constexpr char GeckoProcessManager::GetEditableParent_t::signature[]; - constexpr char GeckoProcessManager::Start_t::name[]; - constexpr char GeckoProcessManager::Start_t::signature[]; - --auto GeckoProcessManager::Start(mozilla::jni::String::Param a0, mozilla::jni::ObjectArray::Param a1, int32_t a2, int32_t a3, int32_t a4) -> int32_t -+auto GeckoProcessManager::Start(mozilla::jni::String::Param a0, mozilla::jni::ObjectArray::Param a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5) -> int32_t - { -- return mozilla::jni::Method::Call(GeckoProcessManager::Context(), nullptr, a0, a1, a2, a3, a4); -+ return mozilla::jni::Method::Call(GeckoProcessManager::Context(), nullptr, a0, a1, a2, a3, a4, a5); - } - - const char GeckoServiceChildProcess::name[] = -diff --git widget/android/GeneratedJNIWrappers.h widget/android/GeneratedJNIWrappers.h -index ece79ac94a71..228affa1e550 100644 ---- widget/android/GeneratedJNIWrappers.h -+++ widget/android/GeneratedJNIWrappers.h -@@ -6696,10 +6696,11 @@ public: - mozilla::jni::ObjectArray::Param, - int32_t, - int32_t, -+ int32_t, - int32_t> Args; - static constexpr char name[] = "start"; - static constexpr char signature[] = -- "(Ljava/lang/String;[Ljava/lang/String;III)I"; -+ "(Ljava/lang/String;[Ljava/lang/String;IIII)I"; - static const bool isStatic = true; - static const mozilla::jni::ExceptionMode exceptionMode = - mozilla::jni::ExceptionMode::ABORT; -@@ -6709,7 +6710,7 @@ public: - mozilla::jni::DispatchTarget::CURRENT; - }; - -- static auto Start(mozilla::jni::String::Param, mozilla::jni::ObjectArray::Param, int32_t, int32_t, int32_t) -> int32_t; -+ static auto Start(mozilla::jni::String::Param, mozilla::jni::ObjectArray::Param, int32_t, int32_t, int32_t, int32_t) -> int32_t; - - static const mozilla::jni::CallingThread callingThread = - mozilla::jni::CallingThread::ANY; -diff --git xpcom/build/nsXULAppAPI.h xpcom/build/nsXULAppAPI.h -index 94f6daf864c9..d6ac10d51d76 100644 ---- xpcom/build/nsXULAppAPI.h -+++ xpcom/build/nsXULAppAPI.h -@@ -398,7 +398,7 @@ XRE_API(const char*, - - #if defined(MOZ_WIDGET_ANDROID) - XRE_API(void, -- XRE_SetAndroidChildFds, (JNIEnv* env, int ipcFd, int crashFd, int crashAnnotationFd)) -+ XRE_SetAndroidChildFds, (JNIEnv* env, int prefsFd, int ipcFd, int crashFd, int crashAnnotationFd)) - #endif // defined(MOZ_WIDGET_ANDROID) - - XRE_API(void, Property changes on: head/www/firefox/files/patch-bug1438678 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-z-bug1436911 =================================================================== --- head/www/firefox/files/patch-z-bug1436911 (revision 472726) +++ head/www/firefox/files/patch-z-bug1436911 (nonexistent) @@ -1,1162 +0,0 @@ -commit 6b81d0b99f20 -Author: Nicholas Nethercote -Date: Thu Mar 8 15:47:24 2018 +1100 - - Bug 1436911 - Avoid the early/late prefs split. r=glandium - - All prefs that need to be sent to a new content process are now put into the - shared memory segment, and they are identified by the pref name instead of an - index into a list. The old IPC used at process startup (in XPCOMInitData) is - removed. - - Benefits: - - - It removes the need for the early prefs list - (dom/ipc/ContentProcesses.{h,cpp}) and the associated checking, which is ugly - and often trips people up (e.g. bug 1432979, bug 1439406). - - - Using prefnames instead of indices fixes some fragility (fixing bug 1419432). - - - It fixes the problem of early prefs being installed as unlocked default - values even if they are locked and/or have user values. - - MozReview-Commit-ID: FRIzHF8Tjd ---- - dom/ipc/ContentChild.cpp | 2 - - dom/ipc/ContentParent.cpp | 4 +- - dom/ipc/ContentPrefs.cpp | 357 ------------------------------ - dom/ipc/ContentPrefs.h | 27 --- - dom/ipc/ContentProcess.cpp | 5 +- - dom/ipc/PContent.ipdl | 7 +- - dom/ipc/moz.build | 2 - - layout/style/nsCSSProps.h | 2 +- - modules/libpref/Preferences.cpp | 470 ++++++++++++++++++++++------------------ - modules/libpref/Preferences.h | 15 +- - 10 files changed, 278 insertions(+), 613 deletions(-) - -diff --git dom/ipc/ContentChild.cpp dom/ipc/ContentChild.cpp -index f61ab07b81e2..af1ef9cf4c7e 100644 ---- dom/ipc/ContentChild.cpp -+++ dom/ipc/ContentChild.cpp -@@ -1185,8 +1185,6 @@ void - ContentChild::InitXPCOM(const XPCOMInitData& aXPCOMInit, - const mozilla::dom::ipc::StructuredCloneData& aInitialData) - { -- Preferences::SetLatePreferences(&aXPCOMInit.prefs()); -- - // Do this as early as possible to get the parent process to initialize the - // background thread since we'll likely need database information very soon. - BackgroundChild::Startup(); -diff --git dom/ipc/ContentParent.cpp dom/ipc/ContentParent.cpp -index 208bb47a970b..4ce5c6bf23d5 100644 ---- dom/ipc/ContentParent.cpp -+++ dom/ipc/ContentParent.cpp -@@ -197,7 +197,6 @@ - - #include "nsLayoutStylesheetCache.h" - --#include "ContentPrefs.h" - #include "mozilla/Sprintf.h" - - #ifdef MOZ_WEBRTC -@@ -2009,7 +2008,7 @@ ContentParent::LaunchSubprocess(ProcessPriority aInitialPriority /* = PROCESS_PR - - // Serialize the early prefs. - nsAutoCStringN<1024> prefs; -- Preferences::SerializeEarlyPreferences(prefs); -+ Preferences::SerializePreferences(prefs); - - // Set up the shared memory. - base::SharedMemory shm; -@@ -2228,7 +2227,6 @@ ContentParent::InitInternal(ProcessPriority aInitialPriority) - - XPCOMInitData xpcomInit; - -- Preferences::GetPreferences(&xpcomInit.prefs()); - nsCOMPtr io(do_GetIOService()); - MOZ_ASSERT(io, "No IO service?"); - DebugOnly rv = io->GetOffline(&xpcomInit.isOffline()); -diff --git dom/ipc/ContentPrefs.cpp dom/ipc/ContentPrefs.cpp -deleted file mode 100644 -index 808b797d9bee..000000000000 ---- dom/ipc/ContentPrefs.cpp -+++ /dev/null -@@ -1,360 +0,0 @@ --/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ --/* vim: set ts=8 sts=2 et sw=2 tw=80: */ --/* This Source Code Form is subject to the terms of the Mozilla Public -- * License, v. 2.0. If a copy of the MPL was not distributed with this -- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -- --#include "ContentPrefs.h" -- --/****************************************************************************** -- * -- * DO NOT ADD PREFS TO THIS LIST WITHOUT DOM PEER REVIEW -- * -- * This is the list of preferences that are sent to the content process on -- * startup. Only prefs that are required immediately upon startup should be -- * listed here. The first IPC message received in the content process will -- * contain all the other prefs. Prefs should only be listed here if they must be -- * read before the first IPC message is received. -- * -- ******************************************************************************/ -- --const char* mozilla::dom::ContentPrefs::gEarlyPrefs[] = { -- "accessibility.monoaudio.enable", -- "accessibility.mouse_focuses_formcontrol", -- "accessibility.tabfocus_applies_to_xul", -- "app.update.channel", -- "browser.autofocus", -- "browser.dom.window.dump.enabled", -- "browser.sessionhistory.max_entries", -- "browser.sessionhistory.max_total_viewers", --#if defined(NIGHTLY_BUILD) || defined(DEBUG) -- "browser.startup.record", --#endif --#if defined(ANDROID) -- "consoleservice.logcat", --#endif -- "content.cors.disable", -- "content.cors.no_private_data", -- "content.notify.backoffcount", -- "content.notify.interval", -- "content.notify.ontimer", -- "content.sink.enable_perf_mode", -- "content.sink.event_probe_rate", -- "content.sink.initial_perf_time", -- "content.sink.interactive_deflect_count", -- "content.sink.interactive_parse_time", -- "content.sink.interactive_time", -- "content.sink.pending_event_mode", -- "content.sink.perf_deflect_count", -- "content.sink.perf_parse_time", -- "device.storage.prompt.testing", -- "device.storage.writable.name", -- "devtools.enabled", -- "dom.allow_XUL_XBL_for_file", -- "dom.allow_cut_copy", -- "dom.animations-api.core.enabled", -- "dom.animations-api.element-animate.enabled", -- "dom.animations-api.pending-member.enabled", -- "dom.enable_frame_timing", -- "dom.enable_performance", -- "dom.enable_performance_navigation_timing", -- "dom.enable_resource_timing", -- "dom.event.handling-user-input-time-limit", -- "dom.event.touch.coalescing.enabled", -- "dom.forms.autocomplete.formautofill", -- "dom.forms.inputmode", -- "dom.input.skip_cursor_move_for_same_value_set", -- "dom.ipc.processPriorityManager.backgroundGracePeriodMS", -- "dom.ipc.processPriorityManager.backgroundPerceivableGracePeriodMS", -- "dom.ipc.useNativeEventProcessing.content", -- "dom.max_chrome_script_run_time", -- "dom.max_ext_content_script_run_time", -- "dom.max_script_run_time", -- "dom.mozBrowserFramesEnabled", -- "dom.performance.enable_notify_performance_timing", -- "dom.performance.enable_user_timing_logging", -- "dom.placeholder.show_on_focus", -- "dom.requestIdleCallback.enabled", -- "dom.script_loader.bytecode_cache.enabled", -- "dom.script_loader.bytecode_cache.strategy", -- "dom.storage.testing", -- "dom.url.encode_decode_hash", -- "dom.url.getters_decode_hash", -- "dom.use_watchdog", -- "dom.vibrator.enabled", -- "dom.vibrator.max_vibrate_list_len", -- "dom.vibrator.max_vibrate_ms", -- "dom.webcomponents.customelements.enabled", -- "dom.webcomponents.shadowdom.enabled", -- "focusmanager.testmode", -- "font.size.inflation.disabledInMasterProcess", -- "font.size.inflation.emPerLine", -- "font.size.inflation.forceEnabled", -- "font.size.inflation.lineThreshold", -- "font.size.inflation.mappingIntercept", -- "font.size.inflation.maxRatio", -- "font.size.inflation.minTwips", -- "font.size.systemFontScale", -- "full-screen-api.allow-trusted-requests-only", -- "full-screen-api.enabled", -- "full-screen-api.unprefix.enabled", --#ifdef FUZZING -- "fuzzing.enabled", --#endif -- "gfx.font_rendering.opentype_svg.enabled", -- "hangmonitor.timeout", -- "html5.flushtimer.initialdelay", -- "html5.flushtimer.subsequentdelay", -- "html5.offmainthread", -- "intl.charset.fallback.tld", -- "intl.charset.fallback.utf8_for_file", -- "intl.ime.hack.on_ime_unaware_apps.fire_key_events_for_composition", -- "javascript.enabled", -- "javascript.options.array_prototype_values", -- "javascript.options.asmjs", -- "javascript.options.asyncstack", -- "javascript.options.baselinejit", -- "javascript.options.baselinejit.threshold", -- "javascript.options.baselinejit.unsafe_eager_compilation", -- "javascript.options.discardSystemSource", -- "javascript.options.dump_stack_on_debuggee_would_run", -- "javascript.options.gczeal", -- "javascript.options.gczeal.frequency", -- "javascript.options.ion", -- "javascript.options.ion.offthread_compilation", -- "javascript.options.ion.threshold", -- "javascript.options.ion.unsafe_eager_compilation", -- "javascript.options.jit.full_debug_checks", -- "javascript.options.native_regexp", -- "javascript.options.parallel_parsing", -- "javascript.options.shared_memory", -- "javascript.options.spectre.index_masking", -- "javascript.options.spectre.jit_to_C++_calls", -- "javascript.options.spectre.object_mitigations.barriers", -- "javascript.options.spectre.object_mitigations.misc", -- "javascript.options.spectre.string_mitigations", -- "javascript.options.spectre.value_masking", -- "javascript.options.streams", -- "javascript.options.strict", -- "javascript.options.strict.debug", -- "javascript.options.throw_on_asmjs_validation_failure", -- "javascript.options.throw_on_debuggee_would_run", -- "javascript.options.wasm", -- "javascript.options.wasm_baselinejit", -- "javascript.options.wasm_ionjit", -- "javascript.options.werror", -- "javascript.use_us_english_locale", -- "jsloader.shareGlobal", -- "layout.css.all-shorthand.enabled", -- "layout.css.background-blend-mode.enabled", -- "layout.css.box-decoration-break.enabled", -- "layout.css.color-adjust.enabled", -- "layout.css.column-span.enabled", -- "layout.css.contain.enabled", -- "layout.css.control-characters.visible", -- "layout.css.emulate-moz-box-with-flex", -- "layout.css.expensive-style-struct-assertions.enabled", -- "layout.css.float-logical-values.enabled", -- "layout.css.font-display.enabled", -- "layout.css.font-variations.enabled", -- "layout.css.frames-timing.enabled", -- "layout.css.getBoxQuads.enabled", -- "layout.css.grid-template-subgrid-value.enabled", -- "layout.css.grid.enabled", -- "layout.css.image-orientation.enabled", -- "layout.css.individual-transform.enabled", -- "layout.css.initial-letter.enabled", -- "layout.css.isolation.enabled", -- "layout.css.mix-blend-mode.enabled", -- "layout.css.moz-document.content.enabled", -- "layout.css.osx-font-smoothing.enabled", -- "layout.css.overflow-clip-box.enabled", -- "layout.css.overscroll-behavior.enabled", -- "layout.css.prefixes.animations", -- "layout.css.prefixes.border-image", -- "layout.css.prefixes.box-sizing", -- "layout.css.prefixes.device-pixel-ratio-webkit", -- "layout.css.prefixes.font-features", -- "layout.css.prefixes.gradients", -- "layout.css.prefixes.transforms", -- "layout.css.prefixes.transitions", -- "layout.css.prefixes.webkit", -- "layout.css.scope-pseudo.enabled", -- "layout.css.scoped-style.enabled", -- "layout.css.scroll-behavior.property-enabled", -- "layout.css.scroll-snap.enabled", --#ifdef MOZ_STYLO -- "layout.css.servo.chrome.enabled", -- "layout.css.servo.enabled", --#endif -- "layout.css.shape-outside.enabled", -- "layout.css.text-align-unsafe-value.enabled", -- "layout.css.text-combine-upright-digits.enabled", -- "layout.css.text-combine-upright.enabled", -- "layout.css.text-justify.enabled", -- "layout.css.touch_action.enabled", -- "layout.css.visited_links_enabled", -- "layout.idle_period.required_quiescent_frames", -- "layout.idle_period.time_limit", -- "layout.interruptible-reflow.enabled", -- "mathml.disabled", -- "media.audio-max-decode-error", -- "media.cache_readahead_limit", -- "media.cache_resume_threshold", -- "media.cache_size", -- "media.clearkey.persistent-license.enabled", -- "media.cubeb.backend", -- "media.cubeb.sandbox", -- "media.cubeb_latency_msg_frames", -- "media.cubeb_latency_playback_ms", -- "media.decoder-doctor.wmf-disabled-is-failure", -- "media.decoder.recycle.enabled", -- "media.decoder.skip-to-next-key-frame.enabled", -- "media.dormant-on-pause-timeout-ms", -- "media.eme.audio.blank", -- "media.eme.chromium-api.video-shmems", -- "media.eme.enabled", -- "media.eme.video.blank", -- "media.ffmpeg.enabled", -- "media.ffmpeg.low-latency.enabled", -- "media.ffvpx.enabled", -- "media.ffvpx.low-latency.enabled", -- "media.flac.enabled", -- "media.forcestereo.enabled", -- "media.gmp.decoder.enabled", -- "media.gmp.insecure.allow", -- "media.gpu-process-decoder", -- "media.hls.enabled", -- "media.libavcodec.allow-obsolete", -- "media.memory_cache_max_size", -- "media.memory_caches_combined_limit_kb", -- "media.memory_caches_combined_limit_pc_sysmem", -- "media.mp4.enabled", -- "media.navigator.mediadatadecoder_enabled", -- "media.ogg.enabled", -- "media.ogg.flac.enabled", -- "media.playback.warnings-as-errors", -- "media.playback.warnings-as-errors.stagefright-vs-rust", -- "media.resampling.enabled", -- "media.resume-bkgnd-video-on-tabhover", -- "media.ruin-av-sync.enabled", -- "media.rust.mp4parser", -- "media.rust.test_mode", -- "media.seamless-looping", -- "media.suspend-bkgnd-video.delay-ms", -- "media.suspend-bkgnd-video.enabled", -- "media.use-blank-decoder", -- "media.video-max-decode-error", -- "media.video_stats.enabled", -- "media.videocontrols.lock-video-orientation", -- "media.volume_scale", -- "media.webspeech.recognition.enable", -- "media.webspeech.recognition.force_enable", -- "media.webspeech.synth.force_global_queue", -- "media.webspeech.test.enable", -- "media.webspeech.test.fake_fsm_events", -- "media.webspeech.test.fake_recognition_service", -- "media.wmf.allow-unsupported-resolutions", -- "media.wmf.enabled", -- "media.wmf.skip-blacklist", -- "media.wmf.vp9.enabled", -- "network.IDN.blacklist_chars", -- "network.IDN.restriction_profile", -- "network.IDN.use_whitelist", -- "network.IDN_show_punycode", -- "network.buffer.cache.count", -- "network.buffer.cache.size", -- "network.captive-portal-service.enabled", -- "network.cookie.cookieBehavior", -- "network.cookie.lifetimePolicy", -- "network.dns.disablePrefetch", -- "network.dns.disablePrefetchFromHTTPS", -- "network.http.tailing.enabled", -- "network.jar.block-remote-files", -- "network.loadinfo.skip_type_assertion", -- "network.notify.changed", -- "network.offline-mirrors-connectivity", -- "network.protocol-handler.external.jar", -- "network.proxy.type", -- "network.security.ports.banned", -- "network.security.ports.banned.override", -- "network.standard-url.enable-rust", -- "network.standard-url.max-length", -- "network.standard-url.punycode-host", -- "network.sts.max_time_for_events_between_two_polls", -- "network.sts.max_time_for_pr_close_during_shutdown", -- "network.tcp.keepalive.enabled", -- "network.tcp.keepalive.idle_time", -- "network.tcp.keepalive.probe_count", -- "network.tcp.keepalive.retry_interval", -- "network.tcp.sendbuffer", -- "nglayout.debug.invalidation", -- "privacy.donottrackheader.enabled", -- "privacy.firstparty.isolate", -- "privacy.firstparty.isolate.restrict_opener_access", -- "privacy.reduceTimerPrecision", -- "privacy.resistFingerprinting", -- "privacy.resistFingerprinting.autoDeclineNoUserInputCanvasPrompts", -- "privacy.resistFingerprinting.reduceTimerPrecision.jitter", -- "privacy.resistFingerprinting.reduceTimerPrecision.microseconds", -- "privacy.resistFingerprinting.target_video_res", -- "privacy.resistFingerprinting.video_dropped_ratio", -- "privacy.resistFingerprinting.video_frames_per_sec", -- "privacy.trackingprotection.lower_network_priority", -- "privacy.window.maxInnerHeight", -- "privacy.window.maxInnerWidth", -- "security.csp.enable", -- "security.data_uri.block_toplevel_data_uri_navigations", -- "security.data_uri.unique_opaque_origin", -- "security.fileuri.strict_origin_policy", -- "security.mixed_content.block_active_content", -- "security.mixed_content.block_display_content", -- "security.mixed_content.block_object_subrequest", -- "security.mixed_content.hsts_priming_cache_timeout", -- "security.mixed_content.send_hsts_priming", -- "security.mixed_content.upgrade_display_content", -- "security.mixed_content.use_hsts", -- "security.sandbox.content.level", -- "security.sandbox.content.tempDirSuffix", -- "security.sandbox.logging.enabled", -- "security.sandbox.mac.track.violations", -- "security.sandbox.windows.log.stackTraceDepth", -- "svg.disabled", -- "svg.display-lists.hit-testing.enabled", -- "svg.display-lists.painting.enabled", -- "svg.new-getBBox.enabled", -- "svg.path-caching.enabled", -- "svg.transform-box.enabled", -- "toolkit.asyncshutdown.crash_timeout", -- "toolkit.asyncshutdown.log", -- "toolkit.osfile.log", -- "toolkit.osfile.log.redirect", -- "toolkit.telemetry.enabled", -- "toolkit.telemetry.idleTimeout", -- "toolkit.telemetry.initDelay", -- "toolkit.telemetry.log.dump", -- "toolkit.telemetry.log.level", -- "toolkit.telemetry.minSubsessionLength", -- "toolkit.telemetry.scheduler.idleTickInterval", -- "toolkit.telemetry.scheduler.tickInterval", -- "toolkit.telemetry.testing.overridePreRelease", -- "toolkit.telemetry.unified", -- "ui.key.menuAccessKeyFocuses", -- "ui.popup.disable_autohide", -- "ui.use_activity_cursor", -- "view_source.editor.external", -- "zoom.maxPercent", -- "zoom.minPercent" --}; -- --const char** mozilla::dom::ContentPrefs::GetEarlyPrefs(size_t* aCount) --{ -- *aCount = ArrayLength(ContentPrefs::gEarlyPrefs); -- return gEarlyPrefs; --} -- --const char* mozilla::dom::ContentPrefs::GetEarlyPref(size_t aIndex) --{ -- MOZ_ASSERT(aIndex < ArrayLength(ContentPrefs::gEarlyPrefs)); -- return gEarlyPrefs[aIndex]; --} -diff --git dom/ipc/ContentPrefs.h dom/ipc/ContentPrefs.h -deleted file mode 100644 -index 72ce8d236c11..000000000000 ---- dom/ipc/ContentPrefs.h -+++ /dev/null -@@ -1,27 +0,0 @@ --/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ --/* vim: set ts=8 sts=2 et sw=2 tw=80: */ --/* This Source Code Form is subject to the terms of the Mozilla Public -- * License, v. 2.0. If a copy of the MPL was not distributed with this -- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -- --#ifndef mozilla_dom_ContentPrefs_h --#define mozilla_dom_ContentPrefs_h -- --// See the comment in ContentPrefs.cpp for more information. -- --namespace mozilla { --namespace dom { -- --class ContentPrefs { --public: -- static const char** GetEarlyPrefs(size_t* aCount); -- static const char* GetEarlyPref(size_t aIndex); -- --private: -- static const char* gEarlyPrefs[]; --}; -- --} --} -- --#endif -diff --git dom/ipc/ContentProcess.cpp dom/ipc/ContentProcess.cpp -index 2441c8cb9224..c00283dc5084 100644 ---- dom/ipc/ContentProcess.cpp -+++ dom/ipc/ContentProcess.cpp -@@ -7,7 +7,6 @@ - #include "mozilla/ipc/IOThreadChild.h" - - #include "ContentProcess.h" --#include "ContentPrefs.h" - #include "base/shared_memory.h" - #include "mozilla/Preferences.h" - #include "mozilla/Scheduler.h" -@@ -226,8 +225,8 @@ ContentProcess::Init(int aArgc, char* aArgv[]) - NS_ERROR("failed to map shared memory in the child"); - return false; - } -- Preferences::DeserializeEarlyPreferences(static_cast(shm.memory()), -- prefsLen); -+ Preferences::DeserializePreferences(static_cast(shm.memory()), -+ prefsLen); - - Scheduler::SetPrefs(schedulerPrefs); - mContent.Init(IOThreadChild::message_loop(), -diff --git dom/ipc/PContent.ipdl dom/ipc/PContent.ipdl -index 5d077976569a..fb967ffc53a9 100644 ---- dom/ipc/PContent.ipdl -+++ dom/ipc/PContent.ipdl -@@ -155,6 +155,12 @@ union MaybePrefValue { - null_t; - }; - -+// This serialization form mirrors that used in mozilla::Pref in -+// Preferences.cpp. The two should be kept in sync, e.g. if something is added -+// to one it should also be added to the other. -+// -+// Note: there is no need to pass the isSticky attribute because that's an -+// immutable attribute obtained from file at startup. - struct Pref { - nsCString name; - bool isLocked; -@@ -273,7 +279,6 @@ struct XPCOMInitData - ClipboardCapabilities clipboardCaps; - DomainPolicyClone domainPolicy; - OptionalURIParams userContentSheetURL; -- Pref[] prefs; - GfxVarUpdate[] gfxNonDefaultVarUpdates; - ContentDeviceData contentDeviceData; - GfxInfoFeatureStatus[] gfxFeatureStatus; -diff --git dom/ipc/moz.build dom/ipc/moz.build -index 3c5541c7791b..5e92a0d52b5d 100644 ---- dom/ipc/moz.build -+++ dom/ipc/moz.build -@@ -26,7 +26,6 @@ EXPORTS.mozilla.dom += [ - 'ContentBridgeParent.h', - 'ContentChild.h', - 'ContentParent.h', -- 'ContentPrefs.h', - 'ContentProcess.h', - 'ContentProcessHost.h', - 'ContentProcessManager.h', -@@ -59,7 +58,6 @@ UNIFIED_SOURCES += [ - 'ContentBridgeChild.cpp', - 'ContentBridgeParent.cpp', - 'ContentParent.cpp', -- 'ContentPrefs.cpp', - 'ContentProcess.cpp', - 'ContentProcessHost.cpp', - 'ContentProcessManager.cpp', -diff --git layout/style/nsCSSProps.h layout/style/nsCSSProps.h -index dce44bf61b35..80288336a08b 100644 ---- layout/style/nsCSSProps.h -+++ layout/style/nsCSSProps.h -@@ -645,7 +645,7 @@ public: - // In the child process, assert that we're not trying to parse stylesheets - // before we've gotten all our prefs. - MOZ_ASSERT_IF(!XRE_IsParentProcess(), -- mozilla::Preferences::AreAllPrefsSetInContentProcess()); -+ mozilla::Preferences::ArePrefsInitedInContentProcess()); - return gPropertyEnabled[aProperty]; - } - -diff --git modules/libpref/Preferences.cpp modules/libpref/Preferences.cpp -index fe780686f2eb..488095f49236 100644 ---- modules/libpref/Preferences.cpp -+++ modules/libpref/Preferences.cpp -@@ -15,7 +15,6 @@ - #include "mozilla/ArenaAllocator.h" - #include "mozilla/ArrayUtils.h" - #include "mozilla/Attributes.h" --#include "mozilla/dom/ContentPrefs.h" - #include "mozilla/dom/PContent.h" - #include "mozilla/HashFunctions.h" - #include "mozilla/Logging.h" -@@ -131,6 +130,29 @@ enum class PrefType : uint8_t - Bool = 3, - }; - -+// This is used for pref names and string pref values. We encode the string -+// length, then a '/', then the string chars. This encoding means there are no -+// special chars that are forbidden or require escaping. -+static void -+SerializeAndAppendString(const char* aChars, nsCString& aStr) -+{ -+ aStr.AppendInt(uint32_t(strlen(aChars))); -+ aStr.Append('/'); -+ aStr.Append(aChars); -+} -+ -+static char* -+DeserializeString(char* aChars, nsCString& aStr) -+{ -+ char* p = aChars; -+ uint32_t length = strtol(p, &p, 10); -+ MOZ_ASSERT(p[0] == '/'); -+ p++; // move past the '/' -+ aStr.Assign(p, length); -+ p += length; // move past the string itself -+ return p; -+} -+ - // Keep this in sync with PrefValue in prefs_parser/src/lib.rs. - union PrefValue { - const char* mStringVal; -@@ -223,6 +245,64 @@ union PrefValue { - MOZ_CRASH(); - } - } -+ -+ void SerializeAndAppend(PrefType aType, nsCString& aStr) -+ { -+ switch (aType) { -+ case PrefType::Bool: -+ aStr.Append(mBoolVal ? 'T' : 'F'); -+ break; -+ -+ case PrefType::Int: -+ aStr.AppendInt(mIntVal); -+ break; -+ -+ case PrefType::String: { -+ SerializeAndAppendString(mStringVal, aStr); -+ break; -+ } -+ -+ case PrefType::None: -+ default: -+ MOZ_CRASH(); -+ } -+ } -+ -+ static char* Deserialize(PrefType aType, -+ char* aStr, -+ dom::MaybePrefValue* aDomValue) -+ { -+ char* p = aStr; -+ -+ switch (aType) { -+ case PrefType::Bool: -+ if (*p == 'T') { -+ *aDomValue = true; -+ } else if (*p == 'F') { -+ *aDomValue = false; -+ } else { -+ *aDomValue = false; -+ NS_ERROR("bad bool pref value"); -+ } -+ p++; -+ return p; -+ -+ case PrefType::Int: { -+ *aDomValue = int32_t(strtol(p, &p, 10)); -+ return p; -+ } -+ -+ case PrefType::String: { -+ nsCString str; -+ p = DeserializeString(p, str); -+ *aDomValue = str; -+ return p; -+ } -+ -+ default: -+ MOZ_CRASH(); -+ } -+ } - }; - - #ifdef DEBUG -@@ -694,6 +774,159 @@ public: - return false; - } - -+ // Prefs are serialized in a manner that mirrors dom::Pref. The two should be -+ // kept in sync. E.g. if something is added to one it should also be added to -+ // the other. (It would be nice to be able to use the code generated from -+ // IPDL for serializing dom::Pref here instead of writing by hand this -+ // serialization/deserialization. Unfortunately, that generated code is -+ // difficult to use directly, outside of the IPDL IPC code.) -+ // -+ // The grammar for the serialized prefs has the following form. -+ // -+ // = ':' ':' ? ':' ? '\n' -+ // = 'B' | 'I' | 'S' -+ // = 'L' | '-' -+ // = -+ // = | | -+ // = 'T' | 'F' -+ // = an integer literal accepted by strtol() -+ // = '/' -+ // = any char sequence of length dictated by the preceding -+ // . -+ // -+ // No whitespace is tolerated between tokens. must match the types of -+ // the values. -+ // -+ // The serialization is text-based, rather than binary, for the following -+ // reasons. -+ // -+ // - The size difference wouldn't be much different between text-based and -+ // binary. Most of the space is for strings (pref names and string pref -+ // values), which would be the same in both styles. And other differences -+ // would be minimal, e.g. small integers are shorter in text but long -+ // integers are longer in text. -+ // -+ // - Likewise, speed differences should be negligible. -+ // -+ // - It's much easier to debug a text-based serialization. E.g. you can -+ // print it and inspect it easily in a debugger. -+ // -+ // Examples of unlocked boolean prefs: -+ // - "B-:8/my.bool1:F:T\n" -+ // - "B-:8/my.bool2:F:\n" -+ // - "B-:8/my.bool3::T\n" -+ // -+ // Examples of locked integer prefs: -+ // - "IL:7/my.int1:0:1\n" -+ // - "IL:7/my.int2:123:\n" -+ // - "IL:7/my.int3::-99\n" -+ // -+ // Examples of unlocked string prefs: -+ // - "S-:10/my.string1:3/abc:4/wxyz\n" -+ // - "S-:10/my.string2:5/1.234:\n" -+ // - "S-:10/my.string3::7/string!\n" -+ -+ void SerializeAndAppend(nsCString& aStr) -+ { -+ switch (Type()) { -+ case PrefType::Bool: -+ aStr.Append('B'); -+ break; -+ -+ case PrefType::Int: -+ aStr.Append('I'); -+ break; -+ -+ case PrefType::String: { -+ aStr.Append('S'); -+ break; -+ } -+ -+ case PrefType::None: -+ default: -+ MOZ_CRASH(); -+ } -+ -+ aStr.Append(mIsLocked ? 'L' : '-'); -+ aStr.Append(':'); -+ -+ SerializeAndAppendString(mName, aStr); -+ aStr.Append(':'); -+ -+ if (mHasDefaultValue) { -+ mDefaultValue.SerializeAndAppend(Type(), aStr); -+ } -+ aStr.Append(':'); -+ -+ if (mHasUserValue) { -+ mUserValue.SerializeAndAppend(Type(), aStr); -+ } -+ aStr.Append('\n'); -+ } -+ -+ static char* Deserialize(char* aStr, dom::Pref* aDomPref) -+ { -+ char* p = aStr; -+ -+ // The type. -+ PrefType type; -+ if (*p == 'B') { -+ type = PrefType::Bool; -+ } else if (*p == 'I') { -+ type = PrefType::Int; -+ } else if (*p == 'S') { -+ type = PrefType::String; -+ } else { -+ NS_ERROR("bad pref type"); -+ type = PrefType::None; -+ } -+ p++; // move past the type char -+ -+ // Locked? -+ bool isLocked; -+ if (*p == 'L') { -+ isLocked = true; -+ } else if (*p == '-') { -+ isLocked = false; -+ } else { -+ NS_ERROR("bad pref locked status"); -+ isLocked = false; -+ } -+ p++; // move past the isLocked char -+ -+ MOZ_ASSERT(*p == ':'); -+ p++; // move past the ':' -+ -+ // The pref name. -+ nsCString name; -+ p = DeserializeString(p, name); -+ -+ MOZ_ASSERT(*p == ':'); -+ p++; // move past the ':' preceding the default value -+ -+ dom::MaybePrefValue maybeDefaultValue; -+ if (*p != ':') { -+ dom::PrefValue defaultValue; -+ p = PrefValue::Deserialize(type, p, &maybeDefaultValue); -+ } -+ -+ MOZ_ASSERT(*p == ':'); -+ p++; // move past the ':' between the default and user values -+ -+ dom::MaybePrefValue maybeUserValue; -+ if (*p != '\n') { -+ dom::PrefValue userValue; -+ p = PrefValue::Deserialize(type, p, &maybeUserValue); -+ } -+ -+ MOZ_ASSERT(*p == '\n'); -+ p++; // move past the '\n' following the user value -+ -+ *aDomPref = dom::Pref(name, isLocked, maybeDefaultValue, maybeUserValue); -+ -+ return p; -+ } -+ - void AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf, PrefsSizes& aSizes) - { - // Note: mName is allocated in gPrefNameArena, measured elsewhere. -@@ -880,41 +1113,9 @@ pref_savePrefs() - - #ifdef DEBUG - --// For content processes, what prefs have been initialized? --enum class ContentProcessPhase --{ -- eNoPrefsSet, -- eEarlyPrefsSet, -- eEarlyAndLatePrefsSet, --}; -- - // Note that this never changes in the parent process, and is only read in - // content processes. --static ContentProcessPhase gPhase = ContentProcessPhase::eNoPrefsSet; -- --struct StringComparator --{ -- const char* mPrefName; -- -- explicit StringComparator(const char* aPrefName) -- : mPrefName(aPrefName) -- { -- } -- -- int operator()(const char* aPrefName) const -- { -- return strcmp(mPrefName, aPrefName); -- } --}; -- --static bool --IsEarlyPref(const char* aPrefName) --{ -- size_t prefsLen; -- size_t found; -- const char** list = mozilla::dom::ContentPrefs::GetEarlyPrefs(&prefsLen); -- return BinarySearchIf(list, 0, prefsLen, StringComparator(aPrefName), &found); --} -+static bool gContentProcessPrefsAreInited = false; - - #endif // DEBUG - -@@ -923,23 +1124,7 @@ pref_HashTableLookupInner(const char* aPrefName) - { - MOZ_ASSERT(NS_IsMainThread() || mozilla::ServoStyleSet::IsInServoTraversal()); - --#ifdef DEBUG -- if (!XRE_IsParentProcess()) { -- if (gPhase == ContentProcessPhase::eNoPrefsSet) { -- MOZ_CRASH_UNSAFE_PRINTF("accessing pref %s before early prefs are set", -- aPrefName); -- } -- -- if (gPhase == ContentProcessPhase::eEarlyPrefsSet && -- !IsEarlyPref(aPrefName)) { -- // If you hit this crash, you have an early access of a non-early pref. -- // Consider moving the access later or add the pref to the whitelist of -- // early prefs in ContentPrefs.cpp and get review from a DOM peer. -- MOZ_CRASH_UNSAFE_PRINTF( -- "accessing non-early pref %s before late prefs are set", aPrefName); -- } -- } --#endif -+ MOZ_ASSERT_IF(!XRE_IsParentProcess(), gContentProcessPrefsAreInited); - - return static_cast(gHashTable->Search(aPrefName)); - } -@@ -2932,8 +3117,8 @@ public: - - } // namespace - --// A list of prefs sent early from the parent, via shared memory. --static InfallibleTArray* gEarlyDomPrefs; -+// A list of changed prefs sent from the parent via shared memory. -+static InfallibleTArray* gChangedDomPrefs; - - static const char kTelemetryPref[] = "toolkit.telemetry.enabled"; - static const char kChannelPref[] = "app.update.channel"; -@@ -3050,12 +3235,12 @@ Preferences::GetInstanceForService() - } - - if (!XRE_IsParentProcess()) { -- MOZ_ASSERT(gEarlyDomPrefs); -- for (unsigned int i = 0; i < gEarlyDomPrefs->Length(); i++) { -- Preferences::SetPreference(gEarlyDomPrefs->ElementAt(i)); -+ MOZ_ASSERT(gChangedDomPrefs); -+ for (unsigned int i = 0; i < gChangedDomPrefs->Length(); i++) { -+ Preferences::SetPreference(gChangedDomPrefs->ElementAt(i)); - } -- delete gEarlyDomPrefs; -- gEarlyDomPrefs = nullptr; -+ delete gChangedDomPrefs; -+ gChangedDomPrefs = nullptr; - - } else { - // Check if there is a deployment configuration file. If so, set up the -@@ -3179,149 +3364,44 @@ NS_IMPL_ISUPPORTS(Preferences, - nsISupportsWeakReference) - - /* static */ void --Preferences::SerializeEarlyPreferences(nsCString& aStr) -+Preferences::SerializePreferences(nsCString& aStr) - { - MOZ_RELEASE_ASSERT(InitStaticMembers()); - -- nsAutoCStringN<256> boolPrefs, intPrefs, stringPrefs; -- size_t numEarlyPrefs; -- dom::ContentPrefs::GetEarlyPrefs(&numEarlyPrefs); -- -- for (unsigned int i = 0; i < numEarlyPrefs; i++) { -- const char* prefName = dom::ContentPrefs::GetEarlyPref(i); -- MOZ_ASSERT_IF(i > 0, -- strcmp(prefName, dom::ContentPrefs::GetEarlyPref(i - 1)) > 0); -- -- Pref* pref = pref_HashTableLookup(prefName); -- if (!pref || !pref->MustSendToContentProcesses()) { -- continue; -- } -+ aStr.Truncate(); - -- switch (pref->Type()) { -- case PrefType::Bool: -- boolPrefs.Append( -- nsPrintfCString("%u:%d|", i, Preferences::GetBool(prefName))); -- break; -- case PrefType::Int: -- intPrefs.Append( -- nsPrintfCString("%u:%d|", i, Preferences::GetInt(prefName))); -- break; -- case PrefType::String: { -- nsAutoCString value; -- Preferences::GetCString(prefName, value); -- stringPrefs.Append( -- nsPrintfCString("%u:%d;%s|", i, value.Length(), value.get())); -- } break; -- case PrefType::None: -- break; -- default: -- printf_stderr("preference type: %d\n", int(pref->Type())); -- MOZ_CRASH(); -+ for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) { -+ Pref* pref = static_cast(iter.Get())->mPref; -+ if (pref->MustSendToContentProcesses() && pref->HasAdvisablySizedValues()) { -+ pref->SerializeAndAppend(aStr); - } - } - -- aStr.Truncate(); -- aStr.Append(boolPrefs); -- aStr.Append('\n'); -- aStr.Append(intPrefs); -- aStr.Append('\n'); -- aStr.Append(stringPrefs); -- aStr.Append('\n'); - aStr.Append('\0'); - } - - /* static */ void --Preferences::DeserializeEarlyPreferences(char* aStr, size_t aStrLen) -+Preferences::DeserializePreferences(char* aStr, size_t aPrefsLen) - { - MOZ_ASSERT(!XRE_IsParentProcess()); - -- MOZ_ASSERT(!gEarlyDomPrefs); -- gEarlyDomPrefs = new InfallibleTArray(); -+ MOZ_ASSERT(!gChangedDomPrefs); -+ gChangedDomPrefs = new InfallibleTArray(); - - char* p = aStr; -- -- // XXX: we assume these pref values are default values, which may not be -- // true. We also assume they are unlocked. Fortunately, these prefs get reset -- // properly by the first IPC message. -- -- // Get the bool prefs. -- while (*p != '\n') { -- int32_t index = strtol(p, &p, 10); -- MOZ_ASSERT(p[0] == ':'); -- p++; -- int v = strtol(p, &p, 10); -- MOZ_ASSERT(v == 0 || v == 1); -- dom::MaybePrefValue value(dom::PrefValue(!!v)); -- MOZ_ASSERT(p[0] == '|'); -- p++; -- dom::Pref pref(nsCString(dom::ContentPrefs::GetEarlyPref(index)), -- /* isLocked */ false, -- value, -- dom::MaybePrefValue()); -- gEarlyDomPrefs->AppendElement(pref); -- } -- p++; -- -- // Get the int prefs. -- while (*p != '\n') { -- int32_t index = strtol(p, &p, 10); -- MOZ_ASSERT(p[0] == ':'); -- p++; -- dom::MaybePrefValue value( -- dom::PrefValue(static_cast(strtol(p, &p, 10)))); -- MOZ_ASSERT(p[0] == '|'); -- p++; -- dom::Pref pref(nsCString(dom::ContentPrefs::GetEarlyPref(index)), -- /* isLocked */ false, -- value, -- dom::MaybePrefValue()); -- gEarlyDomPrefs->AppendElement(pref); -- } -- p++; -- -- // Get the string prefs. -- while (*p != '\n') { -- int32_t index = strtol(p, &p, 10); -- MOZ_ASSERT(p[0] == ':'); -- p++; -- int32_t length = strtol(p, &p, 10); -- MOZ_ASSERT(p[0] == ';'); -- p++; -- dom::MaybePrefValue value(dom::PrefValue(nsCString(p, length))); -- dom::Pref pref(nsCString(dom::ContentPrefs::GetEarlyPref(index)), -- /* isLocked */ false, -- value, -- dom::MaybePrefValue()); -- gEarlyDomPrefs->AppendElement(pref); -- p += length + 1; -- MOZ_ASSERT(*(p - 1) == '|'); -- } -- p++; -- -- MOZ_ASSERT(*p == '\0'); -+ while (*p != '\0') { -+ dom::Pref pref; -+ p = Pref::Deserialize(p, &pref); -+ gChangedDomPrefs->AppendElement(pref); -+ } - - // We finished parsing on a '\0'. That should be the last char in the shared -- // memory. -- MOZ_ASSERT(aStr + aStrLen - 1 == p); -- --#ifdef DEBUG -- MOZ_ASSERT(gPhase == ContentProcessPhase::eNoPrefsSet); -- gPhase = ContentProcessPhase::eEarlyPrefsSet; --#endif --} -- --/* static */ void --Preferences::SetLatePreferences(const nsTArray* aDomPrefs) --{ -- MOZ_ASSERT(!XRE_IsParentProcess()); -- -- for (unsigned int i = 0; i < aDomPrefs->Length(); i++) { -- Preferences::SetPreference(aDomPrefs->ElementAt(i)); -- } -+ // memory. (aPrefsLen includes the '\0'.) -+ MOZ_ASSERT(p == aStr + aPrefsLen - 1); - - #ifdef DEBUG -- MOZ_ASSERT(gPhase == ContentProcessPhase::eEarlyPrefsSet); -- gPhase = ContentProcessPhase::eEarlyAndLatePrefsSet; -+ MOZ_ASSERT(!gContentProcessPrefsAreInited); -+ gContentProcessPrefsAreInited = true; - #endif - } - -@@ -3558,36 +3638,12 @@ Preferences::GetPreference(dom::Pref* aDomPref) - } - } - --void --Preferences::GetPreferences(InfallibleTArray* aDomPrefs) --{ -- MOZ_ASSERT(XRE_IsParentProcess()); -- MOZ_ASSERT(NS_IsMainThread()); -- -- aDomPrefs->SetCapacity(gHashTable->EntryCount()); -- for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) { -- Pref* pref = static_cast(iter.Get())->mPref; -- -- if (!pref->MustSendToContentProcesses()) { -- // The pref value hasn't changed since it was initialized at startup. -- // Don't bother sending it, because the content process will initialize -- // it the same way. -- continue; -- } -- -- if (pref->HasAdvisablySizedValues()) { -- dom::Pref* setting = aDomPrefs->AppendElement(); -- pref->ToDomPref(setting); -- } -- } --} -- - #ifdef DEBUG - bool --Preferences::AreAllPrefsSetInContentProcess() -+Preferences::ArePrefsInitedInContentProcess() - { - MOZ_ASSERT(!XRE_IsParentProcess()); -- return gPhase == ContentProcessPhase::eEarlyAndLatePrefsSet; -+ return gContentProcessPrefsAreInited; - } - #endif - -diff --git modules/libpref/Preferences.h modules/libpref/Preferences.h -index 0d976483daae..901425b5b663 100644 ---- modules/libpref/Preferences.h -+++ modules/libpref/Preferences.h -@@ -328,15 +328,10 @@ public: - const char* aPref, - float aDefault = 0.0f); - -- // When a content process is created these methods are used to pass prefs in -- // bulk from the parent process. "Early" preferences are ones that are needed -- // very early on in the content process's lifetime; they are passed via a -- // special shared memory segment. "Late" preferences are the remainder, which -- // are passed via a standard IPC message. -- static void SerializeEarlyPreferences(nsCString& aStr); -- static void DeserializeEarlyPreferences(char* aStr, size_t aStrLen); -- static void GetPreferences(InfallibleTArray* aSettings); -- static void SetLatePreferences(const nsTArray* aSettings); -+ // When a content process is created these methods are used to pass changed -+ // prefs in bulk from the parent process, via shared memory. -+ static void SerializePreferences(nsCString& aStr); -+ static void DeserializePreferences(char* aStr, size_t aPrefsLen); - - // When a single pref is changed in the parent process, these methods are - // used to pass the update to content processes. -@@ -344,7 +339,7 @@ public: - static void SetPreference(const dom::Pref& aPref); - - #ifdef DEBUG -- static bool AreAllPrefsSetInContentProcess(); -+ static bool ArePrefsInitedInContentProcess(); - #endif - - static void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf, Property changes on: head/www/firefox/files/patch-z-bug1436911 ___________________________________________________________________ Deleted: fbsd:nokeywords ## -1 +0,0 ## -yes \ No newline at end of property Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Deleted: svn:mime-type ## -1 +0,0 ## -text/plain \ No newline at end of property Index: head/www/firefox/files/patch-addon-search =================================================================== --- head/www/firefox/files/patch-addon-search (revision 472726) +++ head/www/firefox/files/patch-addon-search (revision 472727) @@ -1,55 +1,55 @@ https://github.com/mozilla/addons/issues/708 https://github.com/mozilla/addons-frontend/issues/4610 diff --git browser/app/profile/firefox.js browser/app/profile/firefox.js index 75c2c5e435e35..4d8c09c02759b 100644 --- browser/app/profile/firefox.js +++ browser/app/profile/firefox.js @@ -45,8 +45,8 @@ pref("extensions.webextOptionalPermissionPrompts", tru pref("extensions.getAddons.cache.enabled", true); pref("extensions.getAddons.get.url", "https://services.addons.mozilla.org/api/v3/addons/search/?guid=%IDS%&lang=%LOCALE%"); pref("extensions.getAddons.compatOverides.url", "https://services.addons.mozilla.org/api/v3/addons/compat-override/?guid=%IDS%&lang=%LOCALE%"); -pref("extensions.getAddons.search.browseURL", "https://addons.mozilla.org/%LOCALE%/firefox/search?q=%TERMS%&platform=%OS%&appver=%VERSION%"); -pref("extensions.webservice.discoverURL", "https://discovery.addons.mozilla.org/%LOCALE%/firefox/discovery/pane/%VERSION%/%OS%/%COMPATIBILITY_MODE%"); +pref("extensions.getAddons.search.browseURL", "https://addons.mozilla.org/%LOCALE%/firefox/search?q=%TERMS%&platform=Linux&appver=%VERSION%"); +pref("extensions.webservice.discoverURL", "https://discovery.addons.mozilla.org/%LOCALE%/firefox/discovery/pane/%VERSION%/Linux/%COMPATIBILITY_MODE%"); pref("extensions.getAddons.link.url", "https://addons.mozilla.org/%LOCALE%/firefox/"); pref("extensions.getAddons.themes.browseURL", "https://addons.mozilla.org/%LOCALE%/firefox/themes/?src=firefox"); @@ -186,8 +186,8 @@ pref("app.update.service.enabled", true); // .. etc .. // pref("extensions.update.enabled", true); -pref("extensions.update.url", "https://versioncheck.addons.mozilla.org/update/VersionCheck.php?reqVersion=%REQ_VERSION%&id=%ITEM_ID%&version=%ITEM_VERSION%&maxAppVersion=%ITEM_MAXAPPVERSION%&status=%ITEM_STATUS%&appID=%APP_ID%&appVersion=%APP_VERSION%&appOS=%APP_OS%&appABI=%APP_ABI%&locale=%APP_LOCALE%¤tAppVersion=%CURRENT_APP_VERSION%&updateType=%UPDATE_TYPE%&compatMode=%COMPATIBILITY_MODE%"); -pref("extensions.update.background.url", "https://versioncheck-bg.addons.mozilla.org/update/VersionCheck.php?reqVersion=%REQ_VERSION%&id=%ITEM_ID%&version=%ITEM_VERSION%&maxAppVersion=%ITEM_MAXAPPVERSION%&status=%ITEM_STATUS%&appID=%APP_ID%&appVersion=%APP_VERSION%&appOS=%APP_OS%&appABI=%APP_ABI%&locale=%APP_LOCALE%¤tAppVersion=%CURRENT_APP_VERSION%&updateType=%UPDATE_TYPE%&compatMode=%COMPATIBILITY_MODE%"); +pref("extensions.update.url", "https://versioncheck.addons.mozilla.org/update/VersionCheck.php?reqVersion=%REQ_VERSION%&id=%ITEM_ID%&version=%ITEM_VERSION%&maxAppVersion=%ITEM_MAXAPPVERSION%&status=%ITEM_STATUS%&appID=%APP_ID%&appVersion=%APP_VERSION%&appOS=Linux&appABI=%APP_ABI%&locale=%APP_LOCALE%¤tAppVersion=%CURRENT_APP_VERSION%&updateType=%UPDATE_TYPE%&compatMode=%COMPATIBILITY_MODE%"); +pref("extensions.update.background.url", "https://versioncheck-bg.addons.mozilla.org/update/VersionCheck.php?reqVersion=%REQ_VERSION%&id=%ITEM_ID%&version=%ITEM_VERSION%&maxAppVersion=%ITEM_MAXAPPVERSION%&status=%ITEM_STATUS%&appID=%APP_ID%&appVersion=%APP_VERSION%&appOS=Linux&appABI=%APP_ABI%&locale=%APP_LOCALE%¤tAppVersion=%CURRENT_APP_VERSION%&updateType=%UPDATE_TYPE%&compatMode=%COMPATIBILITY_MODE%"); pref("extensions.update.interval", 86400); // Check for updates to Extensions and // Themes every day // Non-symmetric (not shared by extensions) extension-specific [update] preferences diff --git toolkit/mozapps/extensions/internal/AddonRepository.jsm toolkit/mozapps/extensions/internal/AddonRepository.jsm index f70fd8d7e3bd8..81e8cd7764fdf 100644 --- toolkit/mozapps/extensions/internal/AddonRepository.jsm +++ toolkit/mozapps/extensions/internal/AddonRepository.jsm @@ -588,7 +588,7 @@ var AddonRepository = { addon.version = String(aEntry.current_version.version); if (Array.isArray(aEntry.current_version.files)) { for (let file of aEntry.current_version.files) { - if (file.platform == "all" || file.platform == Services.appinfo.OS.toLowerCase()) { + if (file.platform == "all" || file.platform == "linux" || file.platform == Services.appinfo.OS.toLowerCase()) { if (file.url) { addon.sourceURI = NetUtil.newURI(file.url); } -diff --git toolkit/mozapps/extensions/internal/XPIProvider.jsm toolkit/mozapps/extensions/internal/XPIProvider.jsm +diff --git toolkit/mozapps/extensions/internal/XPIDatabase.jsm toolkit/mozapps/extensions/internal/XPIDatabase.jsm index f70fd8d7e3bd8..81e8cd7764fdf 100644 ---- toolkit/mozapps/extensions/internal/XPIProvider.jsm -+++ toolkit/mozapps/extensions/internal/XPIProvider.jsm -@@ -4974,7 +4974,7 @@ AddonInternal.prototype = { +--- toolkit/mozapps/extensions/internal/XPIDatabase.jsm ++++ toolkit/mozapps/extensions/internal/XPIDatabase.jsm +@@ -355,7 +355,7 @@ class AddonInternal { // Something is causing errors in here try { for (let platform of this.targetPlatforms) { - if (platform.os == Services.appinfo.OS) { + if (platform.os == "Linux" || platform.os == Services.appinfo.OS) { if (platform.abi) { needsABI = true; if (platform.abi === abi) Index: head/www/firefox/files/patch-bug1021761 =================================================================== --- head/www/firefox/files/patch-bug1021761 (revision 472726) +++ head/www/firefox/files/patch-bug1021761 (revision 472727) @@ -1,1269 +1,1268 @@ commit f9bcf9c81c4a Author: Evgeniy Vodolazskiy Date: Tue Sep 9 14:38:00 2014 -0700 Bug 1021761 - Make ALSA optional on Linux, allowing fallback to another backend. r=kinetik r=glandium --- media/libcubeb/gtest/moz.build | 1 - media/libcubeb/src/cubeb_alsa.c | 228 +++++++++++++++++++++++++++++----------- toolkit/library/moz.build | 3 - 3 files changed, 168 insertions(+), 64 deletions(-) diff --git media/libcubeb/gtest/moz.build media/libcubeb/gtest/moz.build index 558130188c2e..0cf157d41903 100644 --- media/libcubeb/gtest/moz.build +++ media/libcubeb/gtest/moz.build @@ -72,7 +72,6 @@ elif CONFIG['OS_TARGET'] == 'OpenBSD': 'sndio', ] else: - OS_LIBS += CONFIG['MOZ_ALSA_LIBS'] OS_LIBS += CONFIG['MOZ_PULSEAUDIO_LIBS'] if CONFIG['CC_TYPE'] in ('clang', 'gcc'): diff --git media/libcubeb/src/cubeb_alsa.c media/libcubeb/src/cubeb_alsa.c index bfd4d8f199d4..213c1eaa3d07 100644 --- media/libcubeb/src/cubeb_alsa.c +++ media/libcubeb/src/cubeb_alsa.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,52 @@ #define ALSA_PA_PLUGIN "ALSA <-> PulseAudio PCM I/O Plugin" +#ifdef DISABLE_LIBASOUND_DLOPEN +#define WRAP(x) x +#else +#define WRAP(x) cubeb_##x +#define MAKE_TYPEDEF(x) static typeof(x) * cubeb_##x +MAKE_TYPEDEF(snd_config); +MAKE_TYPEDEF(snd_config_add); +MAKE_TYPEDEF(snd_config_copy); +MAKE_TYPEDEF(snd_config_delete); +MAKE_TYPEDEF(snd_config_get_id); +MAKE_TYPEDEF(snd_config_get_string); +MAKE_TYPEDEF(snd_config_imake_integer); +MAKE_TYPEDEF(snd_config_search); +MAKE_TYPEDEF(snd_config_search_definition); +MAKE_TYPEDEF(snd_lib_error_set_handler); +MAKE_TYPEDEF(snd_pcm_avail_update); +MAKE_TYPEDEF(snd_pcm_close); +MAKE_TYPEDEF(snd_pcm_delay); +MAKE_TYPEDEF(snd_pcm_drain); +MAKE_TYPEDEF(snd_pcm_frames_to_bytes); +MAKE_TYPEDEF(snd_pcm_get_params); +/* snd_pcm_hw_params_alloca is actually a macro */ +/* MAKE_TYPEDEF(snd_pcm_hw_params_alloca); */ +MAKE_TYPEDEF(snd_pcm_hw_params_sizeof); +#define snd_pcm_hw_params_sizeof cubeb_snd_pcm_hw_params_sizeof +MAKE_TYPEDEF(snd_pcm_hw_params_any); +MAKE_TYPEDEF(snd_pcm_hw_params_get_channels_max); +MAKE_TYPEDEF(snd_pcm_hw_params_get_rate); +MAKE_TYPEDEF(snd_pcm_hw_params_set_rate_near); +MAKE_TYPEDEF(snd_pcm_nonblock); +MAKE_TYPEDEF(snd_pcm_open); +MAKE_TYPEDEF(snd_pcm_open_lconf); +MAKE_TYPEDEF(snd_pcm_pause); +MAKE_TYPEDEF(snd_pcm_poll_descriptors); +MAKE_TYPEDEF(snd_pcm_poll_descriptors_count); +MAKE_TYPEDEF(snd_pcm_poll_descriptors_revents); +MAKE_TYPEDEF(snd_pcm_readi); +MAKE_TYPEDEF(snd_pcm_recover); +MAKE_TYPEDEF(snd_pcm_set_params); +MAKE_TYPEDEF(snd_pcm_start); +MAKE_TYPEDEF(snd_pcm_state); +MAKE_TYPEDEF(snd_pcm_writei); + +#undef MAKE_TYPEDEF +#endif + /* ALSA is not thread-safe. snd_pcm_t instances are individually protected by the owning cubeb_stream's mutex. snd_pcm_t creation and destruction is not thread-safe until ALSA 1.0.24 (see alsa-lib.git commit 91c9c8f1), @@ -65,6 +112,8 @@ struct cubeb { workaround is not required. */ snd_config_t * local_config; int is_pa; + + void * libasound; }; enum stream_state { @@ -245,8 +294,8 @@ set_timeout(struct timeval * timeout, unsigned int ms) static void stream_buffer_decrement(cubeb_stream * stm, long count) { - char * bufremains = stm->buffer + snd_pcm_frames_to_bytes(stm->pcm, count); - memmove(stm->buffer, bufremains, snd_pcm_frames_to_bytes(stm->pcm, stm->bufframes - count)); + char * bufremains = stm->buffer + WRAP(snd_pcm_frames_to_bytes)(stm->pcm, count); + memmove(stm->buffer, bufremains, WRAP(snd_pcm_frames_to_bytes)(stm->pcm, stm->bufframes - count)); stm->bufframes -= count; } @@ -278,9 +327,9 @@ alsa_process_stream(cubeb_stream * stm) /* Call _poll_descriptors_revents() even if we don't use it to let underlying plugins clear null events. Otherwise poll() may wake up again and again, producing unnecessary CPU usage. */ - snd_pcm_poll_descriptors_revents(stm->pcm, stm->fds, stm->nfds, &revents); + WRAP(snd_pcm_poll_descriptors_revents)(stm->pcm, stm->fds, stm->nfds, &revents); - avail = snd_pcm_avail_update(stm->pcm); + avail = WRAP(snd_pcm_avail_update)(stm->pcm); /* Got null event? Bail and wait for another wakeup. */ if (avail == 0) { @@ -303,7 +352,7 @@ alsa_process_stream(cubeb_stream * stm) // TODO: should it be marked as DRAINING? } - got = snd_pcm_readi(stm->pcm, stm->buffer+stm->bufframes, avail); + got = WRAP(snd_pcm_readi)(stm->pcm, stm->buffer+stm->bufframes, avail); if (got < 0) { avail = got; // the error handler below will recover us @@ -347,7 +396,7 @@ alsa_process_stream(cubeb_stream * stm) (!stm->other_stream || stm->other_stream->bufframes > 0)) { long got = avail - stm->bufframes; void * other_buffer = stm->other_stream ? stm->other_stream->buffer : NULL; - char * buftail = stm->buffer + snd_pcm_frames_to_bytes(stm->pcm, stm->bufframes); + char * buftail = stm->buffer + WRAP(snd_pcm_frames_to_bytes)(stm->pcm, stm->bufframes); /* Correct read size to the other stream available frames */ if (stm->other_stream && got > (snd_pcm_sframes_t) stm->other_stream->bufframes) { @@ -374,8 +423,8 @@ alsa_process_stream(cubeb_stream * stm) long drain_frames = avail - stm->bufframes; double drain_time = (double) drain_frames / stm->params.rate; - char * buftail = stm->buffer + snd_pcm_frames_to_bytes(stm->pcm, stm->bufframes); - memset(buftail, 0, snd_pcm_frames_to_bytes(stm->pcm, drain_frames)); + char * buftail = stm->buffer + WRAP(snd_pcm_frames_to_bytes)(stm->pcm, stm->bufframes); + memset(buftail, 0, WRAP(snd_pcm_frames_to_bytes)(stm->pcm, drain_frames)); stm->bufframes = avail; /* Mark as draining, unless we're waiting for capture */ @@ -402,7 +451,7 @@ alsa_process_stream(cubeb_stream * stm) } } - wrote = snd_pcm_writei(stm->pcm, stm->buffer, avail); + wrote = WRAP(snd_pcm_writei)(stm->pcm, stm->buffer, avail); if (wrote < 0) { avail = wrote; // the error handler below will recover us } else { @@ -415,13 +464,13 @@ alsa_process_stream(cubeb_stream * stm) /* Got some error? Let's try to recover the stream. */ if (avail < 0) { - avail = snd_pcm_recover(stm->pcm, avail, 0); + avail = WRAP(snd_pcm_recover)(stm->pcm, avail, 0); /* Capture pcm must be started after initial setup/recover */ if (avail >= 0 && stm->stream_type == SND_PCM_STREAM_CAPTURE && - snd_pcm_state(stm->pcm) == SND_PCM_STATE_PREPARED) { - avail = snd_pcm_start(stm->pcm); + WRAP(snd_pcm_state)(stm->pcm) == SND_PCM_STATE_PREPARED) { + avail = WRAP(snd_pcm_start)(stm->pcm); } } @@ -537,26 +586,26 @@ get_slave_pcm_node(snd_config_t * lconf, snd_config_t * root_pcm) slave_def = NULL; - r = snd_config_search(root_pcm, "slave", &slave_pcm); + r = WRAP(snd_config_search)(root_pcm, "slave", &slave_pcm); if (r < 0) { return NULL; } - r = snd_config_get_string(slave_pcm, &string); + r = WRAP(snd_config_get_string)(slave_pcm, &string); if (r >= 0) { - r = snd_config_search_definition(lconf, "pcm_slave", string, &slave_def); + r = WRAP(snd_config_search_definition)(lconf, "pcm_slave", string, &slave_def); if (r < 0) { return NULL; } } do { - r = snd_config_search(slave_def ? slave_def : slave_pcm, "pcm", &pcm); + r = WRAP(snd_config_search)(slave_def ? slave_def : slave_pcm, "pcm", &pcm); if (r < 0) { break; } - r = snd_config_get_string(slave_def ? slave_def : slave_pcm, &string); + r = WRAP(snd_config_get_string)(slave_def ? slave_def : slave_pcm, &string); if (r < 0) { break; } @@ -565,7 +614,7 @@ get_slave_pcm_node(snd_config_t * lconf, snd_config_t * root_pcm) if (r < 0 || r > (int) sizeof(node_name)) { break; } - r = snd_config_search(lconf, node_name, &pcm); + r = WRAP(snd_config_search)(lconf, node_name, &pcm); if (r < 0) { break; } @@ -574,7 +623,7 @@ get_slave_pcm_node(snd_config_t * lconf, snd_config_t * root_pcm) } while (0); if (slave_def) { - snd_config_delete(slave_def); + WRAP(snd_config_delete)(slave_def); } return NULL; @@ -597,22 +646,22 @@ init_local_config_with_workaround(char const * pcm_name) lconf = NULL; - if (snd_config == NULL) { + if (*WRAP(snd_config) == NULL) { return NULL; } - r = snd_config_copy(&lconf, snd_config); + r = WRAP(snd_config_copy)(&lconf, *WRAP(snd_config)); if (r < 0) { return NULL; } do { - r = snd_config_search_definition(lconf, "pcm", pcm_name, &pcm_node); + r = WRAP(snd_config_search_definition)(lconf, "pcm", pcm_name, &pcm_node); if (r < 0) { break; } - r = snd_config_get_id(pcm_node, &string); + r = WRAP(snd_config_get_id)(pcm_node, &string); if (r < 0) { break; } @@ -621,7 +670,7 @@ init_local_config_with_workaround(char const * pcm_name) if (r < 0 || r > (int) sizeof(node_name)) { break; } - r = snd_config_search(lconf, node_name, &pcm_node); + r = WRAP(snd_config_search)(lconf, node_name, &pcm_node); if (r < 0) { break; } @@ -632,12 +681,12 @@ init_local_config_with_workaround(char const * pcm_name) } /* Fetch the PCM node's type, and bail out if it's not the PulseAudio plugin. */ - r = snd_config_search(pcm_node, "type", &node); + r = WRAP(snd_config_search)(pcm_node, "type", &node); if (r < 0) { break; } - r = snd_config_get_string(node, &string); + r = WRAP(snd_config_get_string)(node, &string); if (r < 0) { break; } @@ -648,18 +697,18 @@ init_local_config_with_workaround(char const * pcm_name) /* Don't clobber an explicit existing handle_underrun value, set it only if it doesn't already exist. */ - r = snd_config_search(pcm_node, "handle_underrun", &node); + r = WRAP(snd_config_search)(pcm_node, "handle_underrun", &node); if (r != -ENOENT) { break; } /* Disable pcm_pulse's asynchronous underrun handling. */ - r = snd_config_imake_integer(&node, "handle_underrun", 0); + r = WRAP(snd_config_imake_integer)(&node, "handle_underrun", 0); if (r < 0) { break; } - r = snd_config_add(pcm_node, node); + r = WRAP(snd_config_add)(pcm_node, node); if (r < 0) { break; } @@ -667,7 +716,7 @@ init_local_config_with_workaround(char const * pcm_name) return lconf; } while (0); - snd_config_delete(lconf); + WRAP(snd_config_delete)(lconf); return NULL; } @@ -679,9 +728,9 @@ alsa_locked_pcm_open(snd_pcm_t ** pcm, char const * pcm_name, snd_pcm_stream_t s pthread_mutex_lock(&cubeb_alsa_mutex); if (local_config) { - r = snd_pcm_open_lconf(pcm, pcm_name, stream, SND_PCM_NONBLOCK, local_config); + r = WRAP(snd_pcm_open_lconf)(pcm, pcm_name, stream, SND_PCM_NONBLOCK, local_config); } else { - r = snd_pcm_open(pcm, pcm_name, stream, SND_PCM_NONBLOCK); + r = WRAP(snd_pcm_open)(pcm, pcm_name, stream, SND_PCM_NONBLOCK); } pthread_mutex_unlock(&cubeb_alsa_mutex); @@ -694,7 +743,7 @@ alsa_locked_pcm_close(snd_pcm_t * pcm) int r; pthread_mutex_lock(&cubeb_alsa_mutex); - r = snd_pcm_close(pcm); + r = WRAP(snd_pcm_close)(pcm); pthread_mutex_unlock(&cubeb_alsa_mutex); return r; @@ -757,12 +806,65 @@ alsa_init(cubeb ** context, char const * context_name) pthread_attr_t attr; snd_pcm_t * dummy; + void * libasound = NULL; + +#ifndef DISABLE_LIBASOUND_DLOPEN + libasound = dlopen("libasound.so", RTLD_LAZY); + if (!libasound) { + return CUBEB_ERROR; + } + +#define LOAD(x) do { \ + cubeb_##x = dlsym(libasound, #x); \ + if (!cubeb_##x) { \ + dlclose(libasound); \ + return CUBEB_ERROR; \ + } \ + } while(0) + + LOAD(snd_config); + LOAD(snd_config_add); + LOAD(snd_config_copy); + LOAD(snd_config_delete); + LOAD(snd_config_get_id); + LOAD(snd_config_get_string); + LOAD(snd_config_imake_integer); + LOAD(snd_config_search); + LOAD(snd_config_search_definition); + LOAD(snd_lib_error_set_handler); + LOAD(snd_pcm_avail_update); + LOAD(snd_pcm_close); + LOAD(snd_pcm_delay); + LOAD(snd_pcm_drain); + LOAD(snd_pcm_frames_to_bytes); + LOAD(snd_pcm_get_params); + /* snd_pcm_hw_params_alloca is actually a macro */ + /* LOAD(snd_pcm_hw_params_alloca); */ + LOAD(snd_pcm_hw_params_sizeof); + LOAD(snd_pcm_hw_params_any); + LOAD(snd_pcm_hw_params_get_channels_max); + LOAD(snd_pcm_hw_params_get_rate); + LOAD(snd_pcm_hw_params_set_rate_near); + LOAD(snd_pcm_nonblock); + LOAD(snd_pcm_open); + LOAD(snd_pcm_open_lconf); + LOAD(snd_pcm_pause); + LOAD(snd_pcm_poll_descriptors); + LOAD(snd_pcm_poll_descriptors_count); + LOAD(snd_pcm_poll_descriptors_revents); + LOAD(snd_pcm_recover); + LOAD(snd_pcm_set_params); + LOAD(snd_pcm_state); + LOAD(snd_pcm_writei); + +#undef LOAD +#endif assert(context); *context = NULL; pthread_mutex_lock(&cubeb_alsa_mutex); if (!cubeb_alsa_error_handler_set) { - snd_lib_error_set_handler(silent_error_handler); + WRAP(snd_lib_error_set_handler)(silent_error_handler); cubeb_alsa_error_handler_set = 1; } pthread_mutex_unlock(&cubeb_alsa_mutex); @@ -770,6 +872,8 @@ alsa_init(cubeb ** context, char const * context_name) ctx = calloc(1, sizeof(*ctx)); assert(ctx); + ctx->libasound = libasound; + ctx->ops = &alsa_ops; r = pthread_mutex_init(&ctx->mutex, NULL); @@ -819,7 +923,7 @@ alsa_init(cubeb ** context, char const * context_name) config fails with EINVAL, the PA PCM is too old for this workaround. */ if (r == -EINVAL) { pthread_mutex_lock(&cubeb_alsa_mutex); - snd_config_delete(ctx->local_config); + WRAP(snd_config_delete)(ctx->local_config); pthread_mutex_unlock(&cubeb_alsa_mutex); ctx->local_config = NULL; } else if (r >= 0) { @@ -859,9 +963,13 @@ alsa_destroy(cubeb * ctx) pthread_mutex_destroy(&ctx->mutex); free(ctx->fds); + if (ctx->libasound) { + dlclose(ctx->libasound); + } + if (ctx->local_config) { pthread_mutex_lock(&cubeb_alsa_mutex); - snd_config_delete(ctx->local_config); + WRAP(snd_config_delete)(ctx->local_config); pthread_mutex_unlock(&cubeb_alsa_mutex); } @@ -948,7 +1056,7 @@ alsa_stream_init_single(cubeb * ctx, cubeb_stream ** stream, char const * stream return CUBEB_ERROR; } - r = snd_pcm_nonblock(stm->pcm, 1); + r = WRAP(snd_pcm_nonblock)(stm->pcm, 1); assert(r == 0); latency_us = latency_frames * 1e6 / stm->params.rate; @@ -961,7 +1069,7 @@ alsa_stream_init_single(cubeb * ctx, cubeb_stream ** stream, char const * stream latency_us = latency_us < min_latency ? min_latency: latency_us; } - r = snd_pcm_set_params(stm->pcm, format, SND_PCM_ACCESS_RW_INTERLEAVED, + r = WRAP(snd_pcm_set_params)(stm->pcm, format, SND_PCM_ACCESS_RW_INTERLEAVED, stm->params.channels, stm->params.rate, 1, latency_us); if (r < 0) { @@ -969,20 +1077,20 @@ alsa_stream_init_single(cubeb * ctx, cubeb_stream ** stream, char const * stream return CUBEB_ERROR_INVALID_FORMAT; } - r = snd_pcm_get_params(stm->pcm, &stm->buffer_size, &period_size); + r = WRAP(snd_pcm_get_params)(stm->pcm, &stm->buffer_size, &period_size); assert(r == 0); /* Double internal buffer size to have enough space when waiting for the other side of duplex connection */ stm->buffer_size *= 2; - stm->buffer = calloc(1, snd_pcm_frames_to_bytes(stm->pcm, stm->buffer_size)); + stm->buffer = calloc(1, WRAP(snd_pcm_frames_to_bytes)(stm->pcm, stm->buffer_size)); assert(stm->buffer); - stm->nfds = snd_pcm_poll_descriptors_count(stm->pcm); + stm->nfds = WRAP(snd_pcm_poll_descriptors_count)(stm->pcm); assert(stm->nfds > 0); stm->saved_fds = calloc(stm->nfds, sizeof(struct pollfd)); assert(stm->saved_fds); - r = snd_pcm_poll_descriptors(stm->pcm, stm->saved_fds, stm->nfds); + r = WRAP(snd_pcm_poll_descriptors)(stm->pcm, stm->saved_fds, stm->nfds); assert((nfds_t) r == stm->nfds); if (alsa_register_stream(ctx, stm) != 0) { @@ -1054,7 +1162,7 @@ alsa_stream_destroy(cubeb_stream * stm) pthread_mutex_lock(&stm->mutex); if (stm->pcm) { if (stm->state == DRAINING) { - snd_pcm_drain(stm->pcm); + WRAP(snd_pcm_drain)(stm->pcm); } alsa_locked_pcm_close(stm->pcm); stm->pcm = NULL; @@ -1100,12 +1208,12 @@ alsa_get_max_channel_count(cubeb * ctx, uint32_t * max_channels) assert(stm); - r = snd_pcm_hw_params_any(stm->pcm, hw_params); + r = WRAP(snd_pcm_hw_params_any)(stm->pcm, hw_params); if (r < 0) { return CUBEB_ERROR; } - r = snd_pcm_hw_params_get_channels_max(hw_params, max_channels); + r = WRAP(snd_pcm_hw_params_get_channels_max)(hw_params, max_channels); if (r < 0) { return CUBEB_ERROR; } @@ -1126,34 +1234,34 @@ alsa_get_preferred_sample_rate(cubeb * ctx, uint32_t * rate) { /* get a pcm, disabling resampling, so we get a rate the * hardware/dmix/pulse/etc. supports. */ - r = snd_pcm_open(&pcm, CUBEB_ALSA_PCM_NAME, SND_PCM_STREAM_PLAYBACK, SND_PCM_NO_AUTO_RESAMPLE); + r = WRAP(snd_pcm_open)(&pcm, CUBEB_ALSA_PCM_NAME, SND_PCM_STREAM_PLAYBACK, SND_PCM_NO_AUTO_RESAMPLE); if (r < 0) { return CUBEB_ERROR; } - r = snd_pcm_hw_params_any(pcm, hw_params); + r = WRAP(snd_pcm_hw_params_any)(pcm, hw_params); if (r < 0) { - snd_pcm_close(pcm); + WRAP(snd_pcm_close)(pcm); return CUBEB_ERROR; } - r = snd_pcm_hw_params_get_rate(hw_params, rate, &dir); + r = WRAP(snd_pcm_hw_params_get_rate)(hw_params, rate, &dir); if (r >= 0) { /* There is a default rate: use it. */ - snd_pcm_close(pcm); + WRAP(snd_pcm_close)(pcm); return CUBEB_OK; } /* Use a common rate, alsa may adjust it based on hw/etc. capabilities. */ *rate = 44100; - r = snd_pcm_hw_params_set_rate_near(pcm, hw_params, rate, NULL); + r = WRAP(snd_pcm_hw_params_set_rate_near)(pcm, hw_params, rate, NULL); if (r < 0) { - snd_pcm_close(pcm); + WRAP(snd_pcm_close)(pcm); return CUBEB_ERROR; } - snd_pcm_close(pcm); + WRAP(snd_pcm_close)(pcm); return CUBEB_OK; } @@ -1186,10 +1294,10 @@ alsa_stream_start(cubeb_stream * stm) pthread_mutex_lock(&stm->mutex); /* Capture pcm must be started after initial setup/recover */ if (stm->stream_type == SND_PCM_STREAM_CAPTURE && - snd_pcm_state(stm->pcm) == SND_PCM_STATE_PREPARED) { - snd_pcm_start(stm->pcm); + WRAP(snd_pcm_state)(stm->pcm) == SND_PCM_STATE_PREPARED) { + WRAP(snd_pcm_start)(stm->pcm); } - snd_pcm_pause(stm->pcm, 0); + WRAP(snd_pcm_pause)(stm->pcm, 0); gettimeofday(&stm->last_activity, NULL); pthread_mutex_unlock(&stm->mutex); @@ -1229,7 +1337,7 @@ alsa_stream_stop(cubeb_stream * stm) pthread_mutex_unlock(&ctx->mutex); pthread_mutex_lock(&stm->mutex); - snd_pcm_pause(stm->pcm, 1); + WRAP(snd_pcm_pause)(stm->pcm, 1); pthread_mutex_unlock(&stm->mutex); return CUBEB_OK; @@ -1245,8 +1353,8 @@ alsa_stream_get_position(cubeb_stream * stm, uint64_t * position) pthread_mutex_lock(&stm->mutex); delay = -1; - if (snd_pcm_state(stm->pcm) != SND_PCM_STATE_RUNNING || - snd_pcm_delay(stm->pcm, &delay) != 0) { + if (WRAP(snd_pcm_state)(stm->pcm) != SND_PCM_STATE_RUNNING || + WRAP(snd_pcm_delay)(stm->pcm, &delay) != 0) { *position = stm->last_position; pthread_mutex_unlock(&stm->mutex); return CUBEB_OK; @@ -1271,7 +1379,7 @@ alsa_stream_get_latency(cubeb_stream * stm, uint32_t * latency) snd_pcm_sframes_t delay; /* This function returns the delay in frames until a frame written using snd_pcm_writei is sent to the DAC. The DAC delay should be < 1ms anyways. */ - if (snd_pcm_delay(stm->pcm, &delay)) { + if (WRAP(snd_pcm_delay)(stm->pcm, &delay)) { return CUBEB_ERROR; } diff --git toolkit/library/moz.build toolkit/library/moz.build index b0df6b98b91f..e06592daa265 100644 --- toolkit/library/moz.build +++ toolkit/library/moz.build @@ -247,9 +247,6 @@ if CONFIG['MOZ_SYSTEM_LIBVPX']: if not CONFIG['MOZ_TREE_PIXMAN']: OS_LIBS += CONFIG['MOZ_PIXMAN_LIBS'] -if CONFIG['MOZ_ALSA']: - OS_LIBS += CONFIG['MOZ_ALSA_LIBS'] - if CONFIG['HAVE_CLOCK_MONOTONIC']: OS_LIBS += CONFIG['REALTIME_LIBS'] commit 161bcd671217 Author: Evgeniy Vodolazskiy Date: Wed Sep 3 10:47:00 2014 -0700 Bug 1021761 - Add OSS backend to libcubeb, default but last on Linux. r=kinetik r=glandium --- build/moz.configure/old.configure | 1 + dom/media/CubebUtils.cpp | 3 +- media/libcubeb/AUTHORS | 1 + media/libcubeb/src/cubeb.c | 10 + media/libcubeb/src/cubeb_oss.c | 453 ++++++++++++++++++++++++++++++++++++++ media/libcubeb/src/moz.build | 7 + media/libcubeb/update.sh | 1 + old-configure.in | 62 ++++++ toolkit/library/moz.build | 3 + 9 files changed, 540 insertions(+), 1 deletion(-) diff --git build/moz.configure/old.configure build/moz.configure/old.configure index 17d0c5bf3420..3e6dbc16ca14 100644 --- build/moz.configure/old.configure +++ build/moz.configure/old.configure @@ -262,6 +262,7 @@ def old_configure_options(*options): '--with-nspr-prefix', '--with-nss-exec-prefix', '--with-nss-prefix', + '--with-oss', '--with-pthreads', '--with-qemu-exe', '--with-sixgill', diff --git dom/media/CubebUtils.cpp dom/media/CubebUtils.cpp index 88063ed3a4d6..8613f86dbd16 100644 --- dom/media/CubebUtils.cpp +++ dom/media/CubebUtils.cpp @@ -149,7 +149,8 @@ const char* AUDIOSTREAM_BACKEND_ID_STR[] = { "sndio", "opensl", "audiotrack", - "kai" + "kai", + "oss", }; /* Index for failures to create an audio stream the first time. */ const int CUBEB_BACKEND_INIT_FAILURE_FIRST = diff --git media/libcubeb/AUTHORS media/libcubeb/AUTHORS index f0f9595227f2..e7e7048190ab 100644 --- media/libcubeb/AUTHORS +++ media/libcubeb/AUTHORS @@ -4,6 +4,7 @@ Michael Wu Paul Adenot David Richards Sebastien Alaiwan +Evgeniy Vodolazskiy KO Myung-Hun Haakon Sporsheim Alex Chronopoulos diff --git media/libcubeb/src/cubeb.c media/libcubeb/src/cubeb.c index bb35e0ce349f..e523d94108a3 100644 --- media/libcubeb/src/cubeb.c +++ media/libcubeb/src/cubeb.c @@ -60,6 +60,9 @@ int audiotrack_init(cubeb ** context, char const * context_name); #if defined(USE_KAI) int kai_init(cubeb ** context, char const * context_name); #endif +#if defined(USE_OSS) +int oss_init(cubeb ** context, char const * context_name); +#endif static int validate_stream_params(cubeb_stream_params * input_stream_params, @@ -159,6 +162,10 @@ cubeb_init(cubeb ** context, char const * context_name, char const * backend_nam } else if (!strcmp(backend_name, "kai")) { #if defined(USE_KAI) init_oneshot = kai_init; +#endif + } else if (!strcmp(backend_name, "oss")) { +#if defined(USE_OSS) + init_oneshot = oss_init; #endif } else { /* Already set */ @@ -203,6 +210,9 @@ cubeb_init(cubeb ** context, char const * context_name, char const * backend_nam #endif #if defined(USE_KAI) kai_init, +#endif +#if defined(USE_OSS) + oss_init, #endif }; int i; diff --git media/libcubeb/src/cubeb_oss.c media/libcubeb/src/cubeb_oss.c new file mode 100644 index 000000000000..7d96168b9ea6 --- /dev/null +++ media/libcubeb/src/cubeb_oss.c -@@ -0,0 +1,454 @@ +@@ -0,0 +1,453 @@ +/* + * Copyright © 2014 Mozilla Foundation + * + * This program is made available under an ISC-style license. See the + * accompanying file LICENSE for details. + */ +#if defined(HAVE_SYS_SOUNDCARD_H) +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cubeb/cubeb.h" +#include "cubeb-internal.h" + +#ifndef CUBEB_OSS_DEFAULT_OUTPUT +#define CUBEB_OSS_DEFAULT_OUTPUT "/dev/dsp" +#endif + +#define OSS_BUFFER_SIZE 1024 + +struct cubeb { + struct cubeb_ops const * ops; +}; + +struct cubeb_stream { + /* Note: Must match cubeb_stream layout in cubeb.c. */ + cubeb * context; + void * user_ptr; + /**/ + + cubeb_data_callback data_callback; + cubeb_state_callback state_callback; + float volume; + float panning; + + pthread_mutex_t state_mutex; + pthread_cond_t state_cond; + + int running; + int stopped; + int floating; + + /* These two vars are needed to support old versions of OSS */ + unsigned int position_bytes; + unsigned int last_position_bytes; + + uint64_t written_frags; /* The number of fragments written to /dev/dsp */ + uint64_t missed_frags; /* fragments output with stopped stream */ + + cubeb_stream_params params; + int fd; + pthread_t th; +}; + +static struct cubeb_ops const oss_ops; + +int oss_init(cubeb ** context, char const * context_name) +{ + cubeb* ctx = (cubeb*)malloc(sizeof(cubeb)); + ctx->ops = &oss_ops; + *context = ctx; + return CUBEB_OK; +} + +static void oss_destroy(cubeb *ctx) +{ + free(ctx); +} + +static char const * oss_get_backend_id(cubeb * context) +{ + static char oss_name[] = "oss"; + return oss_name; +} + +static int oss_get_max_channel_count(cubeb * ctx, uint32_t * max_channels) +{ + *max_channels = 2; /* Let's support only stereo for now */ + return CUBEB_OK; +} + +static int oss_get_min_latency(cubeb * context, cubeb_stream_params params, + uint32_t * latency_frames) +{ + (void)context; + /* 40ms is a big enough number to work ok */ + *latency_frames = 40 * params.rate / 1000; + return CUBEB_OK; +} + +static int oss_get_preferred_sample_rate(cubeb *context, uint32_t * rate) +{ + /* 48000 seems a prefered choice for most audio devices + * and a good choice for OSS */ + *rate = 48000; + return CUBEB_OK; +} + +static void run_state_callback(cubeb_stream *stream, cubeb_state state) +{ + if (stream->state_callback) { + stream->state_callback(stream, stream->user_ptr, state); + } +} + +static long run_data_callback(cubeb_stream *stream, void *buffer, long nframes) +{ + long got = 0; + pthread_mutex_lock(&stream->state_mutex); + if (stream->data_callback && stream->running && !stream->stopped) { + pthread_mutex_unlock(&stream->state_mutex); + got = stream->data_callback(stream, stream->user_ptr, NULL, buffer, nframes); + } else { + pthread_mutex_unlock(&stream->state_mutex); + } + return got; +} + +static void apply_volume_int(int16_t* buffer, unsigned int n, + float volume, float panning) +{ + float left = volume; + float right = volume; + unsigned int i; + int pan[2]; + if (panning<0) { + right *= (1+panning); + } else { + left *= (1-panning); + } + pan[0] = 128.0*left; + pan[1] = 128.0*right; + for(i=0; irunning) { + pthread_mutex_lock(&stream->state_mutex); + if (stream->stopped) { + pthread_mutex_unlock(&stream->state_mutex); + run_state_callback(stream, CUBEB_STATE_STOPPED); + pthread_mutex_lock(&stream->state_mutex); + while (stream->stopped) { + pthread_cond_wait(&stream->state_cond, &stream->state_mutex); + } + pthread_mutex_unlock(&stream->state_mutex); + run_state_callback(stream, CUBEB_STATE_STARTED); + continue; + } + pthread_mutex_unlock(&stream->state_mutex); + if (stream->floating) { + got = run_data_callback(stream, f_buffer, + OSS_BUFFER_SIZE/stream->params.channels); + apply_volume_float(f_buffer, got*stream->params.channels, + stream->volume, stream->panning); + for (i=0; i<((unsigned long)got)*stream->params.channels; i++) { + /* Clipping is prefered to overflow */ + if(f_buffer[i]>=1.0){ + f_buffer[i]=1.0; + } + if(f_buffer[i]<=-1.0){ + f_buffer[i]=-1.0; + } + /* One might think that multipling by 32767.0 is logical but results in clipping */ + buffer[i] = f_buffer[i]*32767.0; + } + } else { + got = run_data_callback(stream, buffer, + OSS_BUFFER_SIZE/stream->params.channels); + apply_volume_int(buffer, got*stream->params.channels, + stream->volume, stream->panning); + } + if (got<0) { + run_state_callback(stream, CUBEB_STATE_ERROR); + break; + } + if (!got) { + run_state_callback(stream, CUBEB_STATE_DRAINED); + } + if (got) { + size_t i = 0; + size_t s = got*stream->params.channels*sizeof(int16_t); + while (i < s) { + ssize_t n = write(stream->fd, ((char*)buffer) + i, s - i); + if (n<=0) { + run_state_callback(stream, CUBEB_STATE_ERROR); + break; + } + i+=n; + } + stream->written_frags+=got; + } + } + return NULL; +} + +static void oss_try_set_latency(cubeb_stream* stream, unsigned int latency) +{ + unsigned int latency_bytes, n_frag; + int frag; + /* fragment size of 1024 is a good choice with good chances to be accepted */ + unsigned int frag_log=10; /* 2^frag_log = fragment size */ + latency_bytes = + latency*stream->params.rate*stream->params.channels*sizeof(uint16_t)/1000; + n_frag = latency_bytes>>frag_log; + frag = (n_frag<<16) | frag_log; + /* Even if this fails we wish to continue, not checking for errors */ + ioctl(stream->fd, SNDCTL_DSP_SETFRAGMENT, &frag); +} + +static int oss_stream_init(cubeb * context, cubeb_stream ** stm, + char const * stream_name, + cubeb_devid input_device, + cubeb_stream_params * input_stream_params, + cubeb_devid output_device, + cubeb_stream_params * output_stream_params, + unsigned int latency, + cubeb_data_callback data_callback, + cubeb_state_callback state_callback, void * user_ptr) +{ + cubeb_stream* stream = (cubeb_stream*)malloc(sizeof(cubeb_stream)); + stream->context = context; + stream->data_callback = data_callback; + stream->state_callback = state_callback; + stream->user_ptr = user_ptr; + + assert(!input_stream_params && "not supported."); + if (input_device || output_device) { + /* Device selection not yet implemented. */ + return CUBEB_ERROR_DEVICE_UNAVAILABLE; + } + + if ((input_stream_params && input_stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK) || + (output_stream_params && output_stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK)) { + return CUBEB_ERROR_NOT_SUPPORTED; + } + + if ((stream->fd = open(CUBEB_OSS_DEFAULT_OUTPUT, O_WRONLY)) == -1) { + free(stream); + return CUBEB_ERROR; + } +#define SET(what, to) do { unsigned int i = to; \ + int j = ioctl(stream->fd, what, &i); \ + if (j == -1 || i != to) { \ + close(stream->fd); \ + free(stream); \ + return CUBEB_ERROR_INVALID_FORMAT; } } while (0) + + stream->params = *output_stream_params; + stream->volume = 1.0; + stream->panning = 0.0; + + oss_try_set_latency(stream, latency); + + stream->floating = 0; + SET(SNDCTL_DSP_CHANNELS, stream->params.channels); + SET(SNDCTL_DSP_SPEED, stream->params.rate); + switch (stream->params.format) { + case CUBEB_SAMPLE_S16LE: + SET(SNDCTL_DSP_SETFMT, AFMT_S16_LE); + break; + case CUBEB_SAMPLE_S16BE: + SET(SNDCTL_DSP_SETFMT, AFMT_S16_BE); + break; + case CUBEB_SAMPLE_FLOAT32LE: + SET(SNDCTL_DSP_SETFMT, AFMT_S16_NE); + stream->floating = 1; + break; + default: + close(stream->fd); + free(stream); + return CUBEB_ERROR; + } + + + pthread_mutex_init(&stream->state_mutex, NULL); + pthread_cond_init(&stream->state_cond, NULL); + + stream->running = 1; + stream->stopped = 1; + stream->position_bytes = 0; + stream->last_position_bytes = 0; + stream->written_frags = 0; + stream->missed_frags = 0; + + pthread_create(&stream->th, NULL, writer, (void*)stream); + + *stm = stream; + + return CUBEB_OK; +} + +static void oss_stream_destroy(cubeb_stream * stream) +{ + pthread_mutex_lock(&stream->state_mutex); + + stream->running = 0; + stream->stopped = 0; + pthread_cond_signal(&stream->state_cond); + + pthread_mutex_unlock(&stream->state_mutex); + + pthread_join(stream->th, NULL); + + pthread_mutex_destroy(&stream->state_mutex); + pthread_cond_destroy(&stream->state_cond); + close(stream->fd); + free(stream); +} + +static int oss_stream_get_latency(cubeb_stream * stream, uint32_t * latency) +{ + if (ioctl(stream->fd, SNDCTL_DSP_GETODELAY, latency)==-1) { + return CUBEB_ERROR; + } + /* Convert latency from bytes to frames */ + *latency /= stream->params.channels*sizeof(int16_t); + return CUBEB_OK; +} + + +static int oss_stream_current_optr(cubeb_stream * stream, uint64_t * position) +{ + count_info ci; + /* Unfortunately, this ioctl is only available in OSS 4.x */ +#ifdef SNDCTL_DSP_CURRENT_OPTR + oss_count_t count; + if (ioctl(stream->fd, SNDCTL_DSP_CURRENT_OPTR, &count) != -1) { + *position = count.samples;// + count.fifo_samples; + return CUBEB_OK; + } +#endif + /* Fall back to this ioctl in case the previous one fails */ + if (ioctl(stream->fd, SNDCTL_DSP_GETOPTR, &ci) == -1) { + return CUBEB_ERROR; + } + /* ci.bytes is only 32 bit and will start to wrap after arithmetic overflow */ + stream->position_bytes += ci.bytes - stream->last_position_bytes; + stream->last_position_bytes = ci.bytes; + *position = stream->position_bytes/stream->params.channels/sizeof(int16_t); + return CUBEB_OK; +} + +static int oss_stream_get_position(cubeb_stream * stream, uint64_t * position) +{ + if ( oss_stream_current_optr(stream, position) == CUBEB_OK ){ + *position -= stream->missed_frags; + return CUBEB_OK; + } + /* If no correct method to get position works we resort to this */ + *position = stream->written_frags; + return CUBEB_OK; +} + + +static int oss_stream_start(cubeb_stream * stream) +{ + pthread_mutex_lock(&stream->state_mutex); + if (stream->stopped) { + uint64_t ptr; + oss_stream_current_optr(stream, &ptr); + stream->missed_frags = ptr - stream->written_frags; + stream->stopped = 0; + pthread_cond_signal(&stream->state_cond); + } + pthread_mutex_unlock(&stream->state_mutex); + return CUBEB_OK; +} + +static int oss_stream_stop(cubeb_stream * stream) +{ + pthread_mutex_lock(&stream->state_mutex); + stream->stopped = 1; + pthread_mutex_unlock(&stream->state_mutex); + return CUBEB_OK; +} + +int oss_stream_set_panning(cubeb_stream * stream, float panning) +{ + if (stream->params.channels == 2) { + stream->panning=panning; + } + return CUBEB_OK; +} + +int oss_stream_set_volume(cubeb_stream * stream, float volume) +{ + stream->volume=volume; + return CUBEB_OK; +} + +static struct cubeb_ops const oss_ops = { + .init = oss_init, + .get_backend_id = oss_get_backend_id, + .get_max_channel_count = oss_get_max_channel_count, + .get_min_latency = oss_get_min_latency, + .get_preferred_sample_rate = oss_get_preferred_sample_rate, -+ .get_preferred_channel_layout = NULL, + .enumerate_devices = NULL, + .device_collection_destroy = NULL, + .destroy = oss_destroy, + .stream_init = oss_stream_init, + .stream_destroy = oss_stream_destroy, + .stream_start = oss_stream_start, + .stream_stop = oss_stream_stop, + .stream_reset_default_device = NULL, + .stream_get_position = oss_stream_get_position, + .stream_get_latency = oss_stream_get_latency, + .stream_set_volume = oss_stream_set_volume, + .stream_set_panning = oss_stream_set_panning, + .stream_get_current_device = NULL, + .stream_device_destroy = NULL, + .stream_register_device_changed_callback = NULL, + .register_device_collection_changed = NULL +}; diff --git media/libcubeb/src/moz.build media/libcubeb/src/moz.build index e1fea30ca417..a5b1100f1014 100644 --- media/libcubeb/src/moz.build +++ media/libcubeb/src/moz.build @@ -23,6 +23,12 @@ if CONFIG['MOZ_ALSA']: ] DEFINES['USE_ALSA'] = True +if CONFIG['MOZ_OSS']: + SOURCES += [ + 'cubeb_oss.c', + ] + DEFINES['USE_OSS'] = True + if CONFIG['MOZ_PULSEAUDIO'] or CONFIG['MOZ_JACK']: SOURCES += [ 'cubeb_resampler.cpp', -@@ -88,6 +94,7 @@ if CONFIG['OS_TARGET'] == 'Android': - FINAL_LIBRARY = 'gkmedias' +@@ -89,6 +95,7 @@ FINAL_LIBRARY = 'gkmedias' CFLAGS += CONFIG['MOZ_ALSA_CFLAGS'] + CFLAGS += CONFIG['MOZ_JACK_CFLAGS'] +CFLAGS += CONFIG['MOZ_OSS_CFLAGS'] CFLAGS += CONFIG['MOZ_PULSEAUDIO_CFLAGS'] # We allow warnings for third-party code that can be updated from upstream. diff --git media/libcubeb/update.sh media/libcubeb/update.sh index 0bb6345c9fa9..78a102dc47cb 100755 --- media/libcubeb/update.sh +++ media/libcubeb/update.sh @@ -20,6 +20,7 @@ cp $1/src/cubeb_log.h src cp $1/src/cubeb_mixer.cpp src cp $1/src/cubeb_mixer.h src cp $1/src/cubeb_opensl.c src +cp $1/src/cubeb_oss.c src cp $1/src/cubeb-jni.cpp src cp $1/src/cubeb-jni.h src cp $1/src/android/cubeb-output-latency.h src/android diff --git old-configure.in old-configure.in index 28e1a9e48d61..edacedcf6e5d 100644 --- old-configure.in +++ old-configure.in @@ -2598,6 +2598,67 @@ MOZ_WEBM_ENCODER=1 AC_DEFINE(MOZ_WEBM_ENCODER) AC_SUBST(MOZ_WEBM_ENCODER) +dnl ================================== +dnl = Check OSS availability +dnl ================================== + +dnl If using Linux, Solaris or BSDs, ensure that OSS is available +case "$OS_TARGET" in +Linux|SunOS|DragonFly|FreeBSD|NetBSD|GNU/kFreeBSD) + MOZ_OSS=1 + ;; +esac + +MOZ_ARG_WITH_STRING(oss, +[ --with-oss[=PFX] Enable OpenSoundSystem support [installed at prefix PFX]], + OSSPREFIX=$withval) + +if test -n "$OSSPREFIX"; then + if test "$OSSPREFIX" != "no"; then + MOZ_OSS=1 + else + MOZ_OSS= + fi +fi + +_SAVE_CFLAGS=$CFLAGS +_SAVE_LIBS=$LIBS +if test -n "$MOZ_OSS"; then + dnl Prefer 4Front implementation + AC_MSG_CHECKING([MOZ_OSS_CFLAGS]) + if test "$OSSPREFIX" != "yes"; then + oss_conf=${OSSPREFIX%/usr}/etc/oss.conf + if test -f "$oss_conf"; then + . "$oss_conf" + else + OSSLIBDIR=$OSSPREFIX/lib/oss + fi + if test -d "$OSSLIBDIR"; then + MOZ_OSS_CFLAGS="$MOZ_OSS_CFLAGS -I$OSSLIBDIR/include" + fi + fi + AC_MSG_RESULT([$MOZ_OSS_CFLAGS]) + + CFLAGS="$CFLAGS $MOZ_OSS_CFLAGS" + MOZ_CHECK_HEADERS(sys/soundcard.h soundcard.h) + + if test "$ac_cv_header_sys_soundcard_h" != "yes" -a \ + "$ac_cv_header_soundcard_h" != "yes"; then + AC_MSG_ERROR([Need OSS for Ogg, Wave or WebM decoding on $OS_TARGET. Disable with --without-oss.]) + fi + + dnl Assume NetBSD implementation over SunAudio + AC_CHECK_LIB(ossaudio, _oss_ioctl, + [AC_DEFINE_UNQUOTED(CUBEB_OSS_DEFAULT_OUTPUT, "/dev/sound") + MOZ_OSS_LIBS="$MOZ_OSS_LIBS -lossaudio"]) +fi +CFLAGS=$_SAVE_CFLAGS +LIBS=$_SAVE_LIBS + +AC_SUBST(MOZ_OSS) +AC_SUBST_LIST(MOZ_OSS_CFLAGS) +AC_SUBST_LIST(MOZ_OSS_LIBS) + - dnl ================================== - dnl = Check alsa availability on Linux - dnl ================================== + dnl ======================================================== + dnl NegotiateAuth + dnl ======================================================== diff --git toolkit/library/moz.build toolkit/library/moz.build index e06592daa265..ce016b96c2bc 100644 --- toolkit/library/moz.build +++ toolkit/library/moz.build @@ -247,6 +247,9 @@ if CONFIG['MOZ_SYSTEM_LIBVPX']: if not CONFIG['MOZ_TREE_PIXMAN']: OS_LIBS += CONFIG['MOZ_PIXMAN_LIBS'] +if CONFIG['MOZ_OSS']: + OS_LIBS += CONFIG['MOZ_OSS_LIBS'] + if CONFIG['HAVE_CLOCK_MONOTONIC']: OS_LIBS += CONFIG['REALTIME_LIBS'] Index: head/www/firefox/files/patch-dom_media_flac_FlacDecoder.cpp =================================================================== --- head/www/firefox/files/patch-dom_media_flac_FlacDecoder.cpp (revision 472726) +++ head/www/firefox/files/patch-dom_media_flac_FlacDecoder.cpp (revision 472727) @@ -1,25 +1,25 @@ Enable FLAC on platforms without ffvpx like powerpc* diff --git dom/media/flac/FlacDecoder.cpp dom/media/flac/FlacDecoder.cpp index 53fc3c9937f7..b23771ab80fa 100644 --- dom/media/flac/FlacDecoder.cpp +++ dom/media/flac/FlacDecoder.cpp @@ -7,6 +7,7 @@ #include "FlacDecoder.h" #include "MediaContainerType.h" - #include "MediaPrefs.h" + #include "mozilla/StaticPrefs.h" +#include "PDMFactory.h" namespace mozilla { @@ -15,6 +16,10 @@ FlacDecoder::IsEnabled() { #ifdef MOZ_FFVPX - return MediaPrefs::FlacEnabled(); + return StaticPrefs::MediaFlacEnabled(); +#elif defined(MOZ_FFMPEG) + RefPtr platform = new PDMFactory(); -+ return MediaPrefs::FlacEnabled() && platform->SupportsMimeType(NS_LITERAL_CSTRING("audio/flac"), ++ return StaticPrefs::MediaFlacEnabled() && platform->SupportsMimeType(NS_LITERAL_CSTRING("audio/flac"), + /* DecoderDoctorDiagnostics* */ nullptr); #else // Until bug 1295886 is fixed. return false; Index: head/www/firefox/files/patch-u2f-hid-rs62 =================================================================== --- head/www/firefox/files/patch-u2f-hid-rs62 (revision 472726) +++ head/www/firefox/files/patch-u2f-hid-rs62 (revision 472727) @@ -1,31339 +1,31077 @@ https://github.com/jcjones/u2f-hid-rs/pull/62 diff --git Cargo.lock Cargo.lock index 4bfba0eb2f52..cd4d80035d81 100644 --- Cargo.lock +++ Cargo.lock -@@ -486,6 +486,15 @@ dependencies = [ +@@ -552,6 +552,15 @@ dependencies = [ "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "devd-rs" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ -+ "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", ++ "libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] - name = "dtoa" - version = "0.4.2" -@@ -1170,6 +1179,14 @@ name = "nom" + name = "diff" + version = "0.1.11" +@@ -1351,6 +1360,14 @@ name = "nom" version = "1.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "nom" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "nserror" version = "0.1.0" -@@ -1863,6 +1880,7 @@ dependencies = [ +@@ -2126,6 +2143,7 @@ dependencies = [ "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "boxfnonce 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "devd-rs 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", "libudev 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", -@@ -2220,6 +2238,7 @@ dependencies = [ + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +@@ -2511,6 +2529,7 @@ dependencies = [ "checksum darling_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "167dd3e235c2f1da16a635c282630452cdf49191eb05711de1bcd1d3d5068c00" "checksum darling_macro 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c53edaba455f6073a10c27c72440860eb3f60444f8c8660a391032eeae744d82" "checksum debug_unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9a032eac705ca39214d169f83e3d3da290af06d8d1d344d1baad2fd002dca4b3" +"checksum devd-rs 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e7c9ac481c38baf400d3b732e4a06850dfaa491d1b6379a249d9d40d14c2434c" + "checksum diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3c2b69f912779fbb121ceb775d74d51e915af17aaebc38d28a592843a2dd0a3a" + "checksum docopt 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d8acd393692c503b168471874953a2531df0e9ab77d0b6bbc582395743300a4a" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" - "checksum dtoa-short 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "068d4026697c1a18f0b0bb8cfcad1b0c151b90d8edb9bf4c235ad68128920d1d" - "checksum dwrote 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a207eb7b40e25d1d28dc679f451d321fb6954b73ceaa47986702575865469461" -@@ -2279,6 +2298,7 @@ dependencies = [ +@@ -2581,6 +2600,7 @@ dependencies = [ "checksum net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "3a80f842784ef6c9a958b68b7516bc7e35883c614004dd94959a4dca1b716c09" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce" +"checksum nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b" "checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525" "checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba" "checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01" diff --git dom/webauthn/u2f-hid-rs/Cargo.toml dom/webauthn/u2f-hid-rs/Cargo.toml index a0ab8ce71067..046de1e46f15 100644 --- dom/webauthn/u2f-hid-rs/Cargo.toml +++ dom/webauthn/u2f-hid-rs/Cargo.toml @@ -6,6 +6,9 @@ authors = ["Kyle Machulis ", "J.C. Jones { - match $val { -- Ok(v) => { v } -- Err(e) => { return $or(e); } -+ Ok(v) => v, -+ Err(e) => { -+ return $or(e); -+ } - } -- } -+ }; - } - - fn u2f_get_key_handle_from_register_response(register_response: &Vec) -> io::Result> { -@@ -46,15 +47,13 @@ fn main() { - let challenge_str = format!("{}{}", - r#"{"challenge": "1vQ9mxionq0ngCnjD-wTsv1zUSrGRtFqG2xP09SbZ70","#, - r#" "version": "U2F_V2", "appId": "http://demo.yubico.com"}"#); -- let mut challenge = Sha256::new(); -- challenge.input_str(&challenge_str); -- let mut chall_bytes: Vec = vec![0; challenge.output_bytes()]; -- challenge.result(&mut chall_bytes); -+ let mut challenge = Sha256::default(); -+ challenge.input(challenge_str.as_bytes()); -+ let chall_bytes = Vec::from(challenge.result().as_slice()); - -- let mut application = Sha256::new(); -- application.input_str("http://demo.yubico.com"); -- let mut app_bytes: Vec = vec![0; application.output_bytes()]; -- application.result(&mut app_bytes); -+ let mut application = Sha256::default(); -+ application.input("http://demo.yubico.com".as_bytes()); -+ let app_bytes = Vec::from(application.result().as_slice()); - - let manager = U2FManager::new().unwrap(); - let flags = RegisterFlags::empty(); diff --git dom/webauthn/u2f-hid-rs/src/freebsd/device.rs dom/webauthn/u2f-hid-rs/src/freebsd/device.rs new file mode 100644 index 000000000000..5b9e7f2a912e --- /dev/null +++ dom/webauthn/u2f-hid-rs/src/freebsd/device.rs @@ -0,0 +1,88 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +extern crate libc; + +use std::ffi::{CString, OsString}; +use std::io; +use std::io::{Read, Write}; +use std::os::unix::prelude::*; + +use consts::CID_BROADCAST; +use platform::uhid; +use u2ftypes::U2FDevice; +use util::from_unix_result; + +#[derive(Debug)] +pub struct Device { + path: OsString, + fd: libc::c_int, + cid: [u8; 4], +} + +impl Device { + pub fn new(path: OsString) -> io::Result { + let cstr = CString::new(path.as_bytes())?; + let fd = unsafe { libc::open(cstr.as_ptr(), libc::O_RDWR) }; + let fd = from_unix_result(fd)?; + Ok(Self { + path, + fd, + cid: CID_BROADCAST, + }) + } + + pub fn is_u2f(&self) -> bool { + uhid::is_u2f_device(self.fd) + } +} + +impl Drop for Device { + fn drop(&mut self) { + // Close the fd, ignore any errors. + let _ = unsafe { libc::close(self.fd) }; + } +} + +impl PartialEq for Device { + fn eq(&self, other: &Device) -> bool { + self.path == other.path + } +} + +impl Read for Device { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let bufp = buf.as_mut_ptr() as *mut libc::c_void; + let rv = unsafe { libc::read(self.fd, bufp, buf.len()) }; + from_unix_result(rv as usize) + } +} + +impl Write for Device { + fn write(&mut self, buf: &[u8]) -> io::Result { + let report_id = buf[0] as i64; + // Skip report number when not using numbered reports. + let start = if report_id == 0x0 { 1 } else { 0 }; + let data = &buf[start..]; + + let data_ptr = data.as_ptr() as *const libc::c_void; + let rv = unsafe { libc::write(self.fd, data_ptr, data.len()) }; + from_unix_result(rv as usize + 1) + } + + // USB HID writes don't buffer, so this will be a nop. + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl U2FDevice for Device { + fn get_cid<'a>(&'a self) -> &'a [u8; 4] { + &self.cid + } + + fn set_cid(&mut self, cid: [u8; 4]) { + self.cid = cid; + } +} diff --git dom/webauthn/u2f-hid-rs/src/freebsd/mod.rs dom/webauthn/u2f-hid-rs/src/freebsd/mod.rs new file mode 100644 index 000000000000..7ed5727157d5 --- /dev/null +++ dom/webauthn/u2f-hid-rs/src/freebsd/mod.rs @@ -0,0 +1,9 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +pub mod device; +pub mod transaction; + +mod monitor; +mod uhid; diff --git dom/webauthn/u2f-hid-rs/src/freebsd/monitor.rs dom/webauthn/u2f-hid-rs/src/freebsd/monitor.rs new file mode 100644 index 000000000000..174c11d8955c --- /dev/null +++ dom/webauthn/u2f-hid-rs/src/freebsd/monitor.rs @@ -0,0 +1,139 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use devd_rs; +use std::collections::HashMap; +use std::ffi::OsString; +use std::sync::Arc; +use std::{fs, io}; + +use runloop::RunLoop; + +const POLL_TIMEOUT: usize = 100; + +pub enum Event { + Add(OsString), + Remove(OsString), +} + +impl Event { + fn from_devd(event: devd_rs::Event) -> Option { + match event { + devd_rs::Event::Attach { + ref dev, + parent: _, + location: _, + } if dev.starts_with("uhid") => + { + Some(Event::Add(("/dev/".to_owned() + dev).into())) + } + devd_rs::Event::Detach { + ref dev, + parent: _, + location: _, + } if dev.starts_with("uhid") => + { + Some(Event::Remove(("/dev/".to_owned() + dev).into())) + } + _ => None, + } + } +} + +fn convert_error(e: devd_rs::Error) -> io::Error { + e.into() +} + +pub struct Monitor +where + F: Fn(OsString, &Fn() -> bool) + Sync, +{ + runloops: HashMap, + new_device_cb: Arc, +} + +impl Monitor +where + F: Fn(OsString, &Fn() -> bool) + Send + Sync + 'static, +{ + pub fn new(new_device_cb: F) -> Self { + Self { + runloops: HashMap::new(), + new_device_cb: Arc::new(new_device_cb), + } + } + + pub fn run(&mut self, alive: &Fn() -> bool) -> io::Result<()> { + let mut ctx = devd_rs::Context::new().map_err(convert_error)?; + + // Iterate all existing devices. + for dev in fs::read_dir("/dev")? { + if let Ok(dev) = dev { + let filename_ = dev.file_name(); + let filename = filename_.to_str().unwrap_or(""); + if filename.starts_with("uhid") { + self.add_device(("/dev/".to_owned() + filename).into()); + } + } + } + + // Loop until we're stopped by the controlling thread, or fail. + while alive() { + // Wait for new events, break on failure. + match ctx.wait_for_event(POLL_TIMEOUT) { + Err(devd_rs::Error::Timeout) => (), + Err(e) => return Err(e.into()), + Ok(event) => { + if let Some(event) = Event::from_devd(event) { + self.process_event(event); + } + } + } + } + + // Remove all tracked devices. + self.remove_all_devices(); + + Ok(()) + } + + fn process_event(&mut self, event: Event) { + match event { + Event::Add(path) => { + self.add_device(path); + } + Event::Remove(path) => { + self.remove_device(path); + } + } + } + + fn add_device(&mut self, path: OsString) { + let f = self.new_device_cb.clone(); + let key = path.clone(); + + let runloop = RunLoop::new(move |alive| { + if alive() { + f(path, alive); + } + }); + + if let Ok(runloop) = runloop { + self.runloops.insert(key, runloop); + } + } + + fn remove_device(&mut self, path: OsString) { + if let Some(runloop) = self.runloops.remove(&path) { + runloop.cancel(); + } + } + + fn remove_all_devices(&mut self) { + while !self.runloops.is_empty() { + let path = self.runloops.keys().next().unwrap().clone(); + self.remove_device(path); + } + } +} diff --git dom/webauthn/u2f-hid-rs/src/freebsd/transaction.rs dom/webauthn/u2f-hid-rs/src/freebsd/transaction.rs new file mode 100644 index 000000000000..8f5ed990c8a0 --- /dev/null +++ dom/webauthn/u2f-hid-rs/src/freebsd/transaction.rs @@ -0,0 +1,48 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use platform::monitor::Monitor; +use runloop::RunLoop; +use std::ffi::OsString; +use util::OnceCallback; + +pub struct Transaction { + // Handle to the thread loop. + thread: Option, +} + +impl Transaction { + pub fn new( + timeout: u64, + callback: OnceCallback, + new_device_cb: F, + ) -> Result + where + F: Fn(OsString, &Fn() -> bool) + Sync + Send + 'static, + T: 'static, + { + let thread = RunLoop::new_with_timeout( + move |alive| { + // Create a new device monitor. + let mut monitor = Monitor::new(new_device_cb); + + // Start polling for new devices. + try_or!(monitor.run(alive), |_| callback.call(Err(::Error::Unknown))); + + // Send an error, if the callback wasn't called already. + callback.call(Err(::Error::NotAllowed)); + }, + timeout, + ).map_err(|_| ::Error::Unknown)?; + + Ok(Self { + thread: Some(thread), + }) + } + + pub fn cancel(&mut self) { + // This must never be None. + self.thread.take().unwrap().cancel(); + } +} diff --git dom/webauthn/u2f-hid-rs/src/freebsd/uhid.rs dom/webauthn/u2f-hid-rs/src/freebsd/uhid.rs new file mode 100644 index 000000000000..dc7fd0ca7a3f --- /dev/null +++ dom/webauthn/u2f-hid-rs/src/freebsd/uhid.rs @@ -0,0 +1,90 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +extern crate libc; + +use std::io; +use std::os::unix::io::RawFd; +use std::ptr; + +use hidproto::*; +use util::from_unix_result; + +#[allow(non_camel_case_types)] +#[repr(C)] +#[derive(Debug)] +pub struct GenDescriptor { + ugd_data: *mut u8, + ugd_lang_id: u16, + ugd_maxlen: u16, + ugd_actlen: u16, + ugd_offset: u16, + ugd_config_index: u8, + ugd_string_index: u8, + ugd_iface_index: u8, + ugd_altif_index: u8, + ugd_endpt_index: u8, + ugd_report_index: u8, + reserved: [u8; 16], +} + +impl Default for GenDescriptor { + fn default() -> GenDescriptor { + GenDescriptor { + ugd_data: ptr::null_mut(), + ugd_lang_id: 0, + ugd_maxlen: 65535, + ugd_actlen: 0, + ugd_offset: 0, + ugd_config_index: 0, + ugd_string_index: 0, + ugd_iface_index: 0, + ugd_altif_index: 0, + ugd_endpt_index: 0, + ugd_report_index: 0, + reserved: [0; 16], + } + } +} + +const IOWR: u32 = 0x40000000 | 0x80000000; + +const IOCPARM_SHIFT: u32 = 13; +const IOCPARM_MASK: u32 = ((1 << IOCPARM_SHIFT) - 1); + +const TYPESHIFT: u32 = 8; +const SIZESHIFT: u32 = 16; + +macro_rules! ioctl { + ($dir:expr, $name:ident, $ioty:expr, $nr:expr, $size:expr; $ty:ty) => { + pub unsafe fn $name(fd: libc::c_int, val: *mut $ty) -> io::Result { + let ioc = ($dir as u32) | (($size as u32 & IOCPARM_MASK) << SIZESHIFT) + | (($ioty as u32) << TYPESHIFT) | ($nr as u32); + from_unix_result(libc::ioctl(fd, ioc as libc::c_ulong, val)) + } + }; +} + +// https://github.com/freebsd/freebsd/blob/master/sys/dev/usb/usb_ioctl.h +ioctl!(IOWR, usb_get_report_desc, b'U', 21, 32; /*struct*/ GenDescriptor); + +fn read_report_descriptor(fd: RawFd) -> io::Result { + let mut desc = GenDescriptor::default(); + let _ = unsafe { usb_get_report_desc(fd, &mut desc)? }; + desc.ugd_maxlen = desc.ugd_actlen; + let mut value = Vec::with_capacity(desc.ugd_actlen as usize); + unsafe { + value.set_len(desc.ugd_actlen as usize); + } + desc.ugd_data = value.as_mut_ptr(); + let _ = unsafe { usb_get_report_desc(fd, &mut desc)? }; + Ok(ReportDescriptor { value }) +} + +pub fn is_u2f_device(fd: RawFd) -> bool { + match read_report_descriptor(fd) { + Ok(desc) => has_fido_usage(desc), + Err(_) => false, // Upon failure, just say it's not a U2F device. + } +} diff --git dom/webauthn/u2f-hid-rs/src/hidproto.rs dom/webauthn/u2f-hid-rs/src/hidproto.rs new file mode 100644 index 000000000000..ea30890e0e1e --- /dev/null +++ dom/webauthn/u2f-hid-rs/src/hidproto.rs @@ -0,0 +1,158 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Shared code for platforms that use raw HID access (Linux, FreeBSD, etc.) + +use std::mem; + +use consts::{FIDO_USAGE_U2FHID, FIDO_USAGE_PAGE}; + +// The 4 MSBs (the tag) are set when it's a long item. +const HID_MASK_LONG_ITEM_TAG: u8 = 0b11110000; +// The 2 LSBs denote the size of a short item. +const HID_MASK_SHORT_ITEM_SIZE: u8 = 0b00000011; +// The 6 MSBs denote the tag (4) and type (2). +const HID_MASK_ITEM_TAGTYPE: u8 = 0b11111100; +// tag=0000, type=10 (local) +const HID_ITEM_TAGTYPE_USAGE: u8 = 0b00001000; +// tag=0000, type=01 (global) +const HID_ITEM_TAGTYPE_USAGE_PAGE: u8 = 0b00000100; + +pub struct ReportDescriptor { + pub value: Vec, +} + +impl ReportDescriptor { + fn iter(self) -> ReportDescriptorIterator { + ReportDescriptorIterator::new(self) + } +} + +#[derive(Debug)] +pub enum Data { + UsagePage { data: u32 }, + Usage { data: u32 }, +} + +pub struct ReportDescriptorIterator { + desc: ReportDescriptor, + pos: usize, +} + +impl ReportDescriptorIterator { + fn new(desc: ReportDescriptor) -> Self { + Self { desc, pos: 0 } + } + + fn next_item(&mut self) -> Option { + let item = get_hid_item(&self.desc.value[self.pos..]); + if item.is_none() { + self.pos = self.desc.value.len(); // Close, invalid data. + return None; + } + + let (tag_type, key_len, data) = item.unwrap(); + + // Advance if we have a valid item. + self.pos += key_len + data.len(); + + // We only check short items. + if key_len > 1 { + return None; // Check next item. + } + + // Short items have max. length of 4 bytes. + assert!(data.len() <= mem::size_of::()); + + // Convert data bytes to a uint. + let data = read_uint_le(data); + + match tag_type { + HID_ITEM_TAGTYPE_USAGE_PAGE => Some(Data::UsagePage { data }), + HID_ITEM_TAGTYPE_USAGE => Some(Data::Usage { data }), + _ => None, + } + } +} + +impl Iterator for ReportDescriptorIterator { + type Item = Data; + + fn next(&mut self) -> Option { + if self.pos >= self.desc.value.len() { + return None; + } + + self.next_item().or_else(|| self.next()) + } +} + +fn get_hid_item<'a>(buf: &'a [u8]) -> Option<(u8, usize, &'a [u8])> { + if (buf[0] & HID_MASK_LONG_ITEM_TAG) == HID_MASK_LONG_ITEM_TAG { + get_hid_long_item(buf) + } else { + get_hid_short_item(buf) + } +} + +fn get_hid_long_item<'a>(buf: &'a [u8]) -> Option<(u8, usize, &'a [u8])> { + // A valid long item has at least three bytes. + if buf.len() < 3 { + return None; + } + + let len = buf[1] as usize; + + // Ensure that there are enough bytes left in the buffer. + if len > buf.len() - 3 { + return None; + } + + Some((buf[2], 3 /* key length */, &buf[3..])) +} + +fn get_hid_short_item<'a>(buf: &'a [u8]) -> Option<(u8, usize, &'a [u8])> { + // This is a short item. The bottom two bits of the key + // contain the length of the data section (value) for this key. + let len = match buf[0] & HID_MASK_SHORT_ITEM_SIZE { + s @ 0...2 => s as usize, + _ => 4, /* _ == 3 */ + }; + + // Ensure that there are enough bytes left in the buffer. + if len > buf.len() - 1 { + return None; + } + + Some(( + buf[0] & HID_MASK_ITEM_TAGTYPE, + 1, /* key length */ + &buf[1..1 + len], + )) +} + +fn read_uint_le(buf: &[u8]) -> u32 { + assert!(buf.len() <= 4); + // Parse the number in little endian byte order. + buf.iter().rev().fold(0, |num, b| (num << 8) | (*b as u32)) +} + +pub fn has_fido_usage(desc: ReportDescriptor) -> bool { + let mut usage_page = None; + let mut usage = None; + + for data in desc.iter() { + match data { + Data::UsagePage { data } => usage_page = Some(data), + Data::Usage { data } => usage = Some(data), + } + + // Check the values we found. + if let (Some(usage_page), Some(usage)) = (usage_page, usage) { + return usage_page == FIDO_USAGE_PAGE as u32 && usage == FIDO_USAGE_U2FHID as u32; + } + } + + false +} diff --git dom/webauthn/u2f-hid-rs/src/lib.rs dom/webauthn/u2f-hid-rs/src/lib.rs index 1307497e91ac..e0cdc5080924 100644 --- dom/webauthn/u2f-hid-rs/src/lib.rs +++ dom/webauthn/u2f-hid-rs/src/lib.rs @@ -5,6 +5,9 @@ #[macro_use] mod util; +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +pub mod hidproto; + #[cfg(any(target_os = "linux"))] extern crate libudev; @@ -12,6 +15,13 @@ extern crate libudev; #[path = "linux/mod.rs"] pub mod platform; +#[cfg(any(target_os = "freebsd"))] +extern crate devd_rs; + +#[cfg(any(target_os = "freebsd"))] +#[path = "freebsd/mod.rs"] +pub mod platform; + #[cfg(any(target_os = "macos"))] extern crate core_foundation_sys; @@ -23,7 +33,9 @@ pub mod platform; #[path = "windows/mod.rs"] pub mod platform; -#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] +#[cfg( + not(any(target_os = "linux", target_os = "freebsd", target_os = "macos", target_os = "windows")) +)] #[path = "stub/mod.rs"] pub mod platform; -@@ -39,8 +51,8 @@ extern crate bitflags; - - mod consts; - mod statemachine; --mod u2ftypes; - mod u2fprotocol; -+mod u2ftypes; - - mod manager; - pub use manager::U2FManager; -@@ -88,9 +100,9 @@ pub enum Error { - NotAllowed = 5, - } - -+#[cfg(fuzzing)] -+pub use consts::*; - #[cfg(fuzzing)] - pub use u2fprotocol::*; - #[cfg(fuzzing)] - pub use u2ftypes::*; --#[cfg(fuzzing)] --pub use consts::*; -diff --git dom/webauthn/u2f-hid-rs/src/linux/device.rs dom/webauthn/u2f-hid-rs/src/linux/device.rs -index 6ed57106920f..35569f88a190 100644 ---- dom/webauthn/u2f-hid-rs/src/linux/device.rs -+++ dom/webauthn/u2f-hid-rs/src/linux/device.rs -@@ -11,8 +11,8 @@ use std::os::unix::prelude::*; - - use consts::CID_BROADCAST; - use platform::hidraw; --use util::from_unix_result; - use u2ftypes::U2FDevice; -+use util::from_unix_result; - - #[derive(Debug)] - pub struct Device { diff --git dom/webauthn/u2f-hid-rs/src/linux/hidraw.rs dom/webauthn/u2f-hid-rs/src/linux/hidraw.rs index 0ce5d379c3f7..182fe0ec5f2e 100644 --- dom/webauthn/u2f-hid-rs/src/linux/hidraw.rs +++ dom/webauthn/u2f-hid-rs/src/linux/hidraw.rs @@ -8,22 +8,16 @@ use std::io; use std::mem; use std::os::unix::io::RawFd; -use consts::{FIDO_USAGE_U2FHID, FIDO_USAGE_PAGE}; +use hidproto::*; use util::{from_unix_result, io_err}; #[allow(non_camel_case_types)] #[repr(C)] -pub struct ReportDescriptor { +pub struct LinuxReportDescriptor { size: ::libc::c_int, value: [u8; 4096], } -impl ReportDescriptor { - fn iter(self) -> ReportDescriptorIterator { - ReportDescriptorIterator::new(self) - } -} - const NRBITS: u32 = 8; const TYPEBITS: u32 = 8; -@@ -35,28 +29,15 @@ const TYPESHIFT: u32 = NRSHIFT + NRBITS as u32; +@@ -35,17 +29,6 @@ const TYPESHIFT: u32 = NRSHIFT + NRBITS as u32; const SIZESHIFT: u32 = TYPESHIFT + TYPEBITS as u32; const DIRSHIFT: u32 = SIZESHIFT + SIZEBITS as u32; -// The 4 MSBs (the tag) are set when it's a long item. -const HID_MASK_LONG_ITEM_TAG: u8 = 0b11110000; -// The 2 LSBs denote the size of a short item. -const HID_MASK_SHORT_ITEM_SIZE: u8 = 0b00000011; -// The 6 MSBs denote the tag (4) and type (2). -const HID_MASK_ITEM_TAGTYPE: u8 = 0b11111100; -// tag=0000, type=10 (local) -const HID_ITEM_TAGTYPE_USAGE: u8 = 0b00001000; -// tag=0000, type=01 (global) -const HID_ITEM_TAGTYPE_USAGE_PAGE: u8 = 0b00000100; - // https://github.com/torvalds/linux/blob/master/include/uapi/linux/hid.h const HID_MAX_DESCRIPTOR_SIZE: usize = 4096; - macro_rules! ioctl { -- ($dir:expr, $name:ident, $ioty:expr, $nr:expr; $ty:ty) => ( -+ ($dir:expr, $name:ident, $ioty:expr, $nr:expr; $ty:ty) => { - pub unsafe fn $name(fd: libc::c_int, val: *mut $ty) -> io::Result { - let size = mem::size_of::<$ty>(); -- let ioc = (($dir as u32) << DIRSHIFT) | -- (($ioty as u32) << TYPESHIFT) | -- (($nr as u32) << NRSHIFT) | -- ((size as u32) << SIZESHIFT); -+ let ioc = (($dir as u32) << DIRSHIFT) | (($ioty as u32) << TYPESHIFT) -+ | (($nr as u32) << NRSHIFT) | ((size as u32) << SIZESHIFT); +@@ -68,115 +51,7 @@ macro_rules! ioctl { - #[cfg(not(target_env = "musl"))] - type IocType = libc::c_ulong; -@@ -65,120 +46,12 @@ macro_rules! ioctl { - - from_unix_result(libc::ioctl(fd, ioc as IocType, val)) - } -- ); -+ }; - } - // https://github.com/torvalds/linux/blob/master/include/uapi/linux/hidraw.h ioctl!(READ, hidiocgrdescsize, b'H', 0x01; ::libc::c_int); -ioctl!(READ, hidiocgrdesc, b'H', 0x02; /*struct*/ ReportDescriptor); - -enum Data { - UsagePage { data: u32 }, - Usage { data: u32 }, -} - -struct ReportDescriptorIterator { - desc: ReportDescriptor, - pos: usize, -} - -impl ReportDescriptorIterator { - fn new(desc: ReportDescriptor) -> Self { - Self { desc, pos: 0 } - } - - fn next_item(&mut self) -> Option { - let item = get_hid_item(&self.desc.value[self.pos..]); - if item.is_none() { - self.pos = self.desc.size as usize; // Close, invalid data. - return None; - } - - let (tag_type, key_len, data) = item.unwrap(); - - // Advance if we have a valid item. - self.pos += key_len + data.len(); - - // We only check short items. - if key_len > 1 { - return None; // Check next item. - } - - // Short items have max. length of 4 bytes. - assert!(data.len() <= mem::size_of::()); - - // Convert data bytes to a uint. - let data = read_uint_le(data); - - match tag_type { - HID_ITEM_TAGTYPE_USAGE_PAGE => Some(Data::UsagePage { data }), - HID_ITEM_TAGTYPE_USAGE => Some(Data::Usage { data }), - _ => None, - } - } -} - -impl Iterator for ReportDescriptorIterator { - type Item = Data; - - fn next(&mut self) -> Option { - if self.pos >= self.desc.size as usize { - return None; - } - - self.next_item().or_else(|| self.next()) - } -} - -fn get_hid_item<'a>(buf: &'a [u8]) -> Option<(u8, usize, &'a [u8])> { - if (buf[0] & HID_MASK_LONG_ITEM_TAG) == HID_MASK_LONG_ITEM_TAG { - get_hid_long_item(buf) - } else { - get_hid_short_item(buf) - } -} - -fn get_hid_long_item<'a>(buf: &'a [u8]) -> Option<(u8, usize, &'a [u8])> { - // A valid long item has at least three bytes. - if buf.len() < 3 { - return None; - } - - let len = buf[1] as usize; - - // Ensure that there are enough bytes left in the buffer. - if len > buf.len() - 3 { - return None; - } - - Some((buf[2], 3 /* key length */, &buf[3..])) -} - -fn get_hid_short_item<'a>(buf: &'a [u8]) -> Option<(u8, usize, &'a [u8])> { - // This is a short item. The bottom two bits of the key - // contain the length of the data section (value) for this key. - let len = match buf[0] & HID_MASK_SHORT_ITEM_SIZE { - s @ 0...2 => s as usize, - _ => 4, /* _ == 3 */ - }; - - // Ensure that there are enough bytes left in the buffer. - if len > buf.len() - 1 { - return None; - } - - Some(( - buf[0] & HID_MASK_ITEM_TAGTYPE, - 1, /* key length */ - &buf[1..1 + len], - )) -} - -fn read_uint_le(buf: &[u8]) -> u32 { - assert!(buf.len() <= 4); - // Parse the number in little endian byte order. - buf.iter().rev().fold(0, |num, b| (num << 8) | (*b as u32)) -} +ioctl!(READ, hidiocgrdesc, b'H', 0x02; /*struct*/ LinuxReportDescriptor); pub fn is_u2f_device(fd: RawFd) -> bool { match read_report_descriptor(fd) { @@ -188,7 +61,7 @@ pub fn is_u2f_device(fd: RawFd) -> bool { } fn read_report_descriptor(fd: RawFd) -> io::Result { - let mut desc = ReportDescriptor { + let mut desc = LinuxReportDescriptor { size: 0, value: [0; HID_MAX_DESCRIPTOR_SIZE], }; @@ -199,24 +72,7 @@ fn read_report_descriptor(fd: RawFd) -> io::Result { } let _ = unsafe { hidiocgrdesc(fd, &mut desc)? }; - Ok(desc) -} - -fn has_fido_usage(desc: ReportDescriptor) -> bool { - let mut usage_page = None; - let mut usage = None; - - for data in desc.iter() { - match data { - Data::UsagePage { data } => usage_page = Some(data), - Data::Usage { data } => usage = Some(data), - } - - // Check the values we found. - if let (Some(usage_page), Some(usage)) = (usage_page, usage) { - return usage_page == FIDO_USAGE_PAGE as u32 && usage == FIDO_USAGE_U2FHID as u32; - } - } - - false + let mut value = Vec::from(&desc.value[..]); + value.truncate(desc.size as usize); + Ok(ReportDescriptor { value }) } -diff --git dom/webauthn/u2f-hid-rs/src/linux/monitor.rs dom/webauthn/u2f-hid-rs/src/linux/monitor.rs -index 47c376ba9176..408ea4b7b9ad 100644 ---- dom/webauthn/u2f-hid-rs/src/linux/monitor.rs -+++ dom/webauthn/u2f-hid-rs/src/linux/monitor.rs -@@ -65,13 +65,11 @@ where - - // Start listening for new devices. - let mut socket = monitor.listen()?; -- let mut fds = vec![ -- ::libc::pollfd { -- fd: socket.as_raw_fd(), -- events: POLLIN, -- revents: 0, -- }, -- ]; -+ let mut fds = vec![::libc::pollfd { -+ fd: socket.as_raw_fd(), -+ events: POLLIN, -+ revents: 0, -+ }]; - - while alive() { - // Wait for new events, break on failure. -diff --git dom/webauthn/u2f-hid-rs/src/macos/iokit.rs dom/webauthn/u2f-hid-rs/src/macos/iokit.rs -index 7e550af2a445..79e53599004e 100644 ---- dom/webauthn/u2f-hid-rs/src/macos/iokit.rs -+++ dom/webauthn/u2f-hid-rs/src/macos/iokit.rs -@@ -282,13 +282,13 @@ extern "C" { - - #[cfg(test)] - mod tests { -+ use super::*; - use core_foundation_sys::base::*; - use core_foundation_sys::runloop::*; - use libc::c_void; - use std::ptr; - use std::sync::mpsc::{channel, Sender}; - use std::thread; -- use super::*; - - extern "C" fn observe(_: CFRunLoopObserverRef, _: CFRunLoopActivity, context: *mut c_void) { - let tx: &Sender = unsafe { &*(context as *mut _) }; -diff --git dom/webauthn/u2f-hid-rs/src/macos/monitor.rs dom/webauthn/u2f-hid-rs/src/macos/monitor.rs -index c346fe5eff57..b971ed885d18 100644 ---- dom/webauthn/u2f-hid-rs/src/macos/monitor.rs -+++ dom/webauthn/u2f-hid-rs/src/macos/monitor.rs -@@ -10,9 +10,9 @@ use core_foundation_sys::runloop::*; - use libc::c_void; - use platform::iokit::*; - use runloop::RunLoop; --use std::{io, slice}; - use std::collections::HashMap; - use std::sync::mpsc::{channel, Receiver, Sender}; -+use std::{io, slice}; - use util::io_err; - - struct DeviceData { -diff --git dom/webauthn/u2f-hid-rs/src/manager.rs dom/webauthn/u2f-hid-rs/src/manager.rs -index c9f92bdbfad5..ca8b80a9584a 100644 ---- dom/webauthn/u2f-hid-rs/src/manager.rs -+++ dom/webauthn/u2f-hid-rs/src/manager.rs -@@ -7,8 +7,8 @@ use std::sync::mpsc::{channel, RecvTimeoutError, Sender}; - use std::time::Duration; - - use consts::PARAMETER_SIZE; --use statemachine::StateMachine; - use runloop::RunLoop; -+use statemachine::StateMachine; - use util::OnceCallback; - - enum QueueAction { -diff --git dom/webauthn/u2f-hid-rs/src/statemachine.rs dom/webauthn/u2f-hid-rs/src/statemachine.rs -index 8e4abaa7cf02..55e7d36155a3 100644 ---- dom/webauthn/u2f-hid-rs/src/statemachine.rs -+++ dom/webauthn/u2f-hid-rs/src/statemachine.rs -@@ -7,8 +7,8 @@ use platform::device::Device; - use platform::transaction::Transaction; - use std::thread; - use std::time::Duration; --use util::OnceCallback; - use u2fprotocol::{u2f_init_device, u2f_is_keyhandle_valid, u2f_register, u2f_sign}; -+use util::OnceCallback; - - fn is_valid_transport(transports: ::AuthenticatorTransports) -> bool { - transports.is_empty() || transports.contains(::AuthenticatorTransports::USB) -diff --git dom/webauthn/u2f-hid-rs/src/u2fprotocol.rs dom/webauthn/u2f-hid-rs/src/u2fprotocol.rs -index 4d6737b1289e..0b984407d873 100644 ---- dom/webauthn/u2f-hid-rs/src/u2fprotocol.rs -+++ dom/webauthn/u2f-hid-rs/src/u2fprotocol.rs -@@ -5,9 +5,9 @@ - extern crate std; - - use rand::{thread_rng, Rng}; -+use std::ffi::CString; - use std::io; - use std::io::{Read, Write}; --use std::ffi::CString; - - use consts::*; - use u2ftypes::*; diff --git dom/webauthn/u2f-hid-rs/src/util.rs dom/webauthn/u2f-hid-rs/src/util.rs index 27db864e2fcd..770e166d57d2 100644 --- dom/webauthn/u2f-hid-rs/src/util.rs +++ dom/webauthn/u2f-hid-rs/src/util.rs -@@ -12,10 +12,12 @@ use boxfnonce::SendBoxFnOnce; - macro_rules! try_or { - ($val:expr, $or:expr) => { - match $val { -- Ok(v) => { v } -- Err(e) => { return $or(e); } -+ Ok(v) => v, -+ Err(e) => { -+ return $or(e); -+ } - } -- } -+ }; - } - - pub trait Signed { @@ -44,6 +46,16 @@ pub fn from_unix_result(rv: T) -> io::Result { } } +#[cfg(any(target_os = "freebsd"))] +pub fn from_unix_result(rv: T) -> io::Result { + if rv.is_negative() { + let errno = unsafe { *libc::__error() }; + Err(io::Error::from_raw_os_error(errno)) + } else { + Ok(rv) + } +} + pub fn io_err(msg: &str) -> io::Error { io::Error::new(io::ErrorKind::Other, msg) } -diff --git dom/webauthn/u2f-hid-rs/src/windows/device.rs dom/webauthn/u2f-hid-rs/src/windows/device.rs -index cbbe2811d672..5579cc052a6f 100644 ---- dom/webauthn/u2f-hid-rs/src/windows/device.rs -+++ dom/webauthn/u2f-hid-rs/src/windows/device.rs -@@ -7,8 +7,8 @@ use std::io; - use std::io::{Read, Write}; - use std::os::windows::io::AsRawHandle; - --use consts::{FIDO_USAGE_U2FHID, CID_BROADCAST, FIDO_USAGE_PAGE, HID_RPT_SIZE}; - use super::winapi::DeviceCapabilities; -+use consts::{FIDO_USAGE_U2FHID, CID_BROADCAST, FIDO_USAGE_PAGE, HID_RPT_SIZE}; - - use u2ftypes::U2FDevice; - -diff --git dom/webauthn/u2f-hid-rs/src/windows/winapi.rs dom/webauthn/u2f-hid-rs/src/windows/winapi.rs -index 18c6898e86ee..bd8a7e58d06a 100644 ---- dom/webauthn/u2f-hid-rs/src/windows/winapi.rs -+++ dom/webauthn/u2f-hid-rs/src/windows/winapi.rs -@@ -60,7 +60,7 @@ extern "stdcall" { - macro_rules! offset_of { - ($ty:ty, $field:ident) => { - unsafe { &(*(0 as *const $ty)).$field as *const _ as usize } -- } -+ }; - } - - fn from_wide_ptr(ptr: *const u16, len: usize) -> String { diff --git third_party/rust/devd-rs/.cargo-checksum.json third_party/rust/devd-rs/.cargo-checksum.json new file mode 100644 index 000000000000..7f8c6af3141e --- /dev/null +++ third_party/rust/devd-rs/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{".rustfmt.toml":"2af0439152afb5f592e67eb815db1a2711e3951d94d6ec2a3343ccf17cf7eb53","CODE_OF_CONDUCT.md":"62f073941a34756006851cef8d5d081f6332a986063e87deafeb621f3f6ff554","Cargo.toml":"82c3a9280afb5f4ac916fbca17ca4913b9f66f90c28eb48be1b66f5efe363e87","README.md":"27a78f684d46d92d64bdda18e8b55f132960836347a654d4024ede000e980bec","UNLICENSE":"7e12e5df4bae12cb21581ba157ced20e1986a0508dd10d0e8a4ab9a4cf94e85c","examples/main.rs":"734a87846b61d09d2aaca444c69dc61765f66df34602f3a4acf1255f95404226","src/data.rs":"677b52a636deb1f0ffc623dbdc5ed7acd78d915117825ced7031c6fa6f0c861e","src/lib.rs":"5e1539f2e197214f90cdcb5835c9b082773b0cd18f6c18e03067ebe04f18a6b7","src/parser.rs":"8459eed676eb9190f592b159d099d542bbcc447d6fb19b46f7a61c60a1ef8a8e","src/result.rs":"4088fc879652c115a13d8a6e6a71fab8571a7982e740af6a91115f3a82aef236"},"package":"e7c9ac481c38baf400d3b732e4a06850dfaa491d1b6379a249d9d40d14c2434c"} \ No newline at end of file diff --git third_party/rust/devd-rs/.rustfmt.toml third_party/rust/devd-rs/.rustfmt.toml new file mode 100644 index 000000000000..6be7cd87020f --- /dev/null +++ third_party/rust/devd-rs/.rustfmt.toml @@ -0,0 +1,9 @@ +max_width = 256 +fn_call_width = 96 +struct_lit_width = 64 +struct_variant_width = 96 +array_width = 256 +newline_style = "Native" +use_try_shorthand = true +match_block_trailing_comma = true +fn_call_style = "Block" diff --git third_party/rust/devd-rs/CODE_OF_CONDUCT.md third_party/rust/devd-rs/CODE_OF_CONDUCT.md new file mode 100644 index 000000000000..a375db8308be --- /dev/null +++ third_party/rust/devd-rs/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project owner at greg@unrelenting.technology. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project owner is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git third_party/rust/devd-rs/Cargo.toml third_party/rust/devd-rs/Cargo.toml new file mode 100644 index 000000000000..9c6f1250697f --- /dev/null +++ third_party/rust/devd-rs/Cargo.toml @@ -0,0 +1,28 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g. crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "devd-rs" +version = "0.2.1" +authors = ["Greg V "] +description = "An interface to devd, the device hotplug daemon on FreeBSD and DragonFlyBSD" +homepage = "https://github.com/myfreeweb/devd-rs" +readme = "README.md" +keywords = ["System", "FreeBSD", "DragonFlyBSD", "devd", "hotplug"] +categories = ["os::unix-apis"] +license = "Unlicense/MIT" +repository = "https://github.com/myfreeweb/devd-rs" +[dependencies.libc] +version = "0" + +[dependencies.nom] +version = "3.2" diff --git third_party/rust/devd-rs/README.md third_party/rust/devd-rs/README.md new file mode 100644 index 000000000000..c10c4081f189 --- /dev/null +++ third_party/rust/devd-rs/README.md @@ -0,0 +1,25 @@ +[![crates.io](https://img.shields.io/crates/v/systemstat.svg)](https://crates.io/crates/systemstat) +[![unlicense](https://img.shields.io/badge/un-license-green.svg?style=flat)](http://unlicense.org) + +# devd-rs + +A Rust library for listening to FreeBSD (also DragonFlyBSD) [devd](https://www.freebsd.org/cgi/man.cgi?devd)'s device attach-detach notifications. + +Listens on `/var/run/devd.seqpacket.pipe` and parses messages using [nom](https://github.com/Geal/nom). + +## Usage + +See [examples/main.rs](https://github.com/myfreeweb/devd-rs/blob/master/examples/main.rs). + +## Contributing + +Please feel free to submit pull requests! + +By participating in this project you agree to follow the [Contributor Code of Conduct](http://contributor-covenant.org/version/1/4/). + +[The list of contributors is available on GitHub](https://github.com/myfreeweb/devd-rs/graphs/contributors). + +## License + +This is free and unencumbered software released into the public domain. +For more information, please refer to the `UNLICENSE` file or [unlicense.org](http://unlicense.org). diff --git third_party/rust/devd-rs/UNLICENSE third_party/rust/devd-rs/UNLICENSE new file mode 100644 index 000000000000..68a49daad8ff --- /dev/null +++ third_party/rust/devd-rs/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git third_party/rust/devd-rs/examples/main.rs third_party/rust/devd-rs/examples/main.rs new file mode 100644 index 000000000000..fee8612f1e6b --- /dev/null +++ third_party/rust/devd-rs/examples/main.rs @@ -0,0 +1,12 @@ +extern crate devd_rs; + +use devd_rs::*; + +fn main() { + let mut ctx = Context::new().unwrap(); + loop { + if let Ok(ev) = ctx.wait_for_event(1000) { + println!("{:?}", ev); + } + } +} diff --git third_party/rust/devd-rs/src/data.rs third_party/rust/devd-rs/src/data.rs new file mode 100644 index 000000000000..52084362e537 --- /dev/null +++ third_party/rust/devd-rs/src/data.rs @@ -0,0 +1,9 @@ +pub use std::collections::BTreeMap; + +#[derive(Debug, Clone, PartialEq)] +pub enum Event { + Notify { system: String, subsystem: String, kind: String, data: BTreeMap }, + Attach { dev: String, parent: BTreeMap, location: String }, + Detach { dev: String, parent: BTreeMap, location: String }, + Nomatch { parent: BTreeMap, location: String }, +} diff --git third_party/rust/devd-rs/src/lib.rs third_party/rust/devd-rs/src/lib.rs new file mode 100644 index 000000000000..10e43a328c4d --- /dev/null +++ third_party/rust/devd-rs/src/lib.rs @@ -0,0 +1,98 @@ +extern crate libc; +#[macro_use] +extern crate nom; + +pub mod result; +pub mod data; +pub mod parser; + +use libc::{ + c_int, nfds_t, + poll, pollfd, POLLIN, + socket, connect, sockaddr_un, AF_UNIX, SOCK_SEQPACKET +}; +use std::os::unix::io::{FromRawFd, RawFd}; +use std::os::unix::net::UnixStream; +use std::{io, mem, ptr}; +use io::{BufRead, BufReader}; + +pub use result::*; +pub use data::*; + +const SOCKET_PATH: &'static str = "/var/run/devd.seqpacket.pipe"; + +pub fn parse_devd_event(e: String) -> Result { + match parser::event(e.as_bytes()) { + parser::IResult::Done(_, x) => Ok(x), + _ => Err(Error::Parse), + } +} + +#[derive(Debug)] +pub struct Context { + sock: BufReader, + sockfd: RawFd, +} + +impl Context { + pub fn new() -> Result { + unsafe { + let sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); + if sockfd < 0 { + return Err(io::Error::last_os_error().into()); + } + let mut sockaddr = sockaddr_un { + sun_family: AF_UNIX as _, + .. mem::zeroed() + }; + ptr::copy_nonoverlapping( + SOCKET_PATH.as_ptr(), + sockaddr.sun_path.as_mut_ptr() as *mut u8, + SOCKET_PATH.len()); + if connect( + sockfd, + &sockaddr as *const sockaddr_un as *const _, + (mem::size_of_val(&AF_UNIX) + SOCKET_PATH.len()) as _) < 0 { + return Err(io::Error::last_os_error().into()); + } + Ok(Context { + sock: BufReader::new(UnixStream::from_raw_fd(sockfd)), + sockfd: sockfd, + }) + } + } + + /// Waits for an event using poll(), reads it but does not parse + pub fn wait_for_event_raw(&mut self, timeout_ms: usize) -> Result { + let mut fds = vec![pollfd { fd: self.sockfd, events: POLLIN, revents: 0 }]; + let x = unsafe { poll((&mut fds).as_mut_ptr(), fds.len() as nfds_t, timeout_ms as c_int) }; + if x < 0 { + Err(io::Error::last_os_error().into()) + } else if x == 0 { + Err(Error::Timeout) + } else { + let mut s = String::new(); + let _ = self.sock.read_line(&mut s); + Ok(s) + } + } + + /// Waits for an event using poll(), reads and parses it + pub fn wait_for_event<'a>(&mut self, timeout_ms: usize) -> Result { + self.wait_for_event_raw(timeout_ms) + .and_then(parse_devd_event) + } + + /// Returns the devd socket file descriptor in case you want to select/poll on it together with + /// other file descriptors + pub fn fd(&self) -> RawFd { + self.sockfd + } + + /// Reads an event and parses it. Use when polling on the raw fd by yourself + pub fn read_event(&mut self) -> Result { + let mut s = String::new(); + let _ = self.sock.read_line(&mut s); + parse_devd_event(s) + } +} diff --git third_party/rust/devd-rs/src/parser.rs third_party/rust/devd-rs/src/parser.rs new file mode 100644 index 000000000000..59e5a9ce60e3 --- /dev/null +++ third_party/rust/devd-rs/src/parser.rs @@ -0,0 +1,164 @@ +use std::str; +use nom::{alphanumeric, multispace}; +pub use nom::IResult; +use data::*; + +named!(key<&str>, map_res!(alphanumeric, str::from_utf8)); + +named!( + val<&str>, + alt!(delimited!(char!('"'), map_res!(take_while!(call!(|c| c != '"' as u8)), str::from_utf8), char!('"')) | map_res!(take_while!(call!(|c| c != '\n' as u8 && c != ' ' as u8)), str::from_utf8)) +); + +named!(keyval <&[u8], (String, String)>, + do_parse!( + key: key + >> char!('=') + >> val: val + >> (key.to_owned(), val.to_owned()) + ) + ); + +named!(keyvals <&[u8], BTreeMap >, + map!( + many0!(terminated!(keyval, opt!(multispace))), + |vec: Vec<_>| vec.into_iter().collect() + ) + ); + +named!(pub event <&[u8], Event>, + alt!( + do_parse!( + tag!("!") >> + tag!("system=") >> + sys: val >> + multispace >> + tag!("subsystem=") >> + subsys: val >> + multispace >> + tag!("type=") >> + kind: val >> + multispace >> + data: keyvals >> + (Event::Notify { system: sys.to_owned(), subsystem: subsys.to_owned(), kind: kind.to_owned(), data: data }) + ) | + do_parse!( + tag!("+") >> + dev: key >> + multispace >> + tag!("at") >> + multispace >> + parent: keyvals >> + tag!("on") >> + multispace >> + loc: val >> + (Event::Attach { dev: dev.to_owned(), parent: parent, location: loc.to_owned() }) + ) | + do_parse!( + tag!("-") >> + dev: key >> + multispace >> + tag!("at") >> + multispace >> + parent: keyvals >> + tag!("on") >> + multispace >> + loc: val >> + (Event::Detach { dev: dev.to_owned(), parent: parent, location: loc.to_owned() }) + ) | + do_parse!( + tag!("?") >> + multispace >> + tag!("at") >> + multispace >> + parent: keyvals >> + tag!("on") >> + multispace >> + loc: val >> + (Event::Nomatch { parent: parent, location: loc.to_owned() }) + ) + + + + + ) + ); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_notify() { + let txt = b"!system=USB subsystem=INTERFACE type=ATTACH ugen=ugen0.2 vendor=0x1050 sernum=\"\" mode=host\n"; + let res = event(txt); + let mut data = BTreeMap::new(); + data.insert("ugen".to_owned(), "ugen0.2".to_owned()); + data.insert("vendor".to_owned(), "0x1050".to_owned()); + data.insert("sernum".to_owned(), "".to_owned()); + data.insert("mode".to_owned(), "host".to_owned()); + assert_eq!( + res, + IResult::Done( + &b""[..], + Event::Notify { + system: "USB".to_owned(), + subsystem: "INTERFACE".to_owned(), + kind: "ATTACH".to_owned(), + data: data, + } + ) + ) + } + + #[test] + fn test_attach() { + let txt = b"+uhid1 at bus=0 sernum=\"\" on uhub1"; + let res = event(txt); + let mut data = BTreeMap::new(); + data.insert("bus".to_owned(), "0".to_owned()); + data.insert("sernum".to_owned(), "".to_owned()); + assert_eq!( + res, + IResult::Done( + &b""[..], + Event::Attach { + dev: "uhid1".to_owned(), + parent: data, + location: "uhub1".to_owned(), + } + ) + ) + } + + #[test] + fn test_detach() { + let txt = b"-uhid1 at on uhub1"; + let res = event(txt); + let data = BTreeMap::new(); + assert_eq!( + res, + IResult::Done( + &b""[..], + Event::Detach { + dev: "uhid1".to_owned(), + parent: data.to_owned(), + location: "uhub1".to_owned(), + } + ) + ) + } + + #[test] + fn test_nomatch() { + let txt = b"? at bus=0 on uhub1"; + let res = event(txt); + let mut data = BTreeMap::new(); + data.insert("bus".to_owned(), "0".to_owned()); + assert_eq!( + res, + IResult::Done(&b""[..], Event::Nomatch { parent: data, location: "uhub1".to_owned() }) + ) + } + +} diff --git third_party/rust/devd-rs/src/result.rs third_party/rust/devd-rs/src/result.rs new file mode 100644 index 000000000000..481cd808ed93 --- /dev/null +++ third_party/rust/devd-rs/src/result.rs @@ -0,0 +1,26 @@ +use std::{io, result}; + +#[derive(Debug)] +pub enum Error { + IoError(io::Error), + Timeout, + Parse, +} + +impl Into for Error { + fn into(self) -> io::Error { + match self { + Error::IoError(e) => e, + Error::Timeout => io::Error::new(io::ErrorKind::Other, "devd poll timeout"), + Error::Parse => io::Error::new(io::ErrorKind::Other, "devd parse error"), + } + } +} + +impl From for Error { + fn from(err: io::Error) -> Error { + Error::IoError(err) + } +} + +pub type Result = result::Result; diff --git third_party/rust/nom-1.2.4/.cargo-checksum.json third_party/rust/nom-1.2.4/.cargo-checksum.json new file mode 100644 index 000000000000..9d93bcc0afc2 --- /dev/null +++ third_party/rust/nom-1.2.4/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{".travis.yml":"6d4e81838b10c5e330749857c72c2f2b1a2e575e71abcd11c094f3b612347b2a","CHANGELOG.md":"d4722e028b2a5b88c466b0d759e463b90bdcfa1b79181a1c76cd313b0a27c615","Cargo.toml":"aebcb999933c3425db85012bea19f9ce78da8e7834dbab54d4a2966e8bc62149","LICENSE":"de730187d5563a81342a3c011d968f78dff37c934fac9b3701e8c762b6118a55","src/bits.rs":"97c9148f63e175489bb6199d039c594ddc56bdf0b7491b9f38b8d74e898bca80","src/bytes.rs":"8f29b976a5e8e6500eb618a9dead7f212688ba9eb06c7066a4016e2db99fed00","src/character.rs":"9ee081f56b508212231ff70d7455b1b85ae44722a39aa60223e8cd95c6570859","src/internal.rs":"ada499b9c178be2a7f9b56319ffb10a778f25fafcda39c78d26b364d89debd72","src/lib.rs":"34efb051214acfde2053e93a7ba718a4fd41b6e0d9edd65a1737605d99b994ab","src/macros.rs":"d39ce3a2cd2b1cb9dd57ce90c06a1ca84720a2dc75e6332cffebba6086cb75d3","src/methods.rs":"24bdbcb0e3570c8bf3fa270dd8d79dd6dfcb982276c82180a89a1e73c5e38019","src/nom.rs":"b0a9c7ce0d09388179bce8f8e23bf57df76b504d925815583c249ec3fc04baab","src/regexp.rs":"8fdae52b761dbad90179e6be87e0e66357fefa34d76af541fb0fcf550fd6ec08","src/str.rs":"198fa15d45c3636289d92c0a592002a07e5a04a431e8cfdf724266e44d484be2","src/stream.rs":"c1bd5b8e7a2061ff66eb2c954033146001f1d65a26d12efa06af8cf93ffa53e4","src/util.rs":"da40ebac865d3176567d3a37b01170234398a03e938553720ce30aa1f6005b6d","tests/arithmetic.rs":"b98936b7fa0228835ca022f6db5342b72a9c01cc3f16a4e05263bbe6424ba3e9","tests/arithmetic_ast.rs":"b18b9a46ba573ae13c40a31217425f6e8cf8fade09a75cdbbfa7146ec668f0b2","tests/cross_function_backtracking.rs":"b071d13031c1f12195473186e3775943991496b10f4590db3f36d511e9f98a1c","tests/ini.rs":"776f681542028564899e55f71533b3bcda5ed1bbb971f24b5b1b9578111ba0cb","tests/ini_str.rs":"315046d9b6dc38d6d306d3562d7ac6518c9ecce9aabcc58fb80c07577ad99789","tests/issues.rs":"2193c219397b7a417cc009b72c13adc42471e7a4917a2a4009aa0fca23c6ea8c","tests/mp4.rs":"b4bf0514fd645160851cc4da9ad6bf81d571cd14865bf134837c19578caaf6e6","tests/omnom.rs":"409d2349fa24f3503bd02e0079c1554a58ce3d40dd7eb0e5d4bb63b588afdae4","tests/test1.rs":"3e0c187bad91d822ebc113eb5cf30fc6585e53a961728304ac24e05ab2123d10"},"package":"a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"} \ No newline at end of file diff --git third_party/rust/nom-1.2.4/.travis.yml third_party/rust/nom-1.2.4/.travis.yml new file mode 100644 index 000000000000..1d1e36c593aa --- /dev/null +++ third_party/rust/nom-1.2.4/.travis.yml @@ -0,0 +1,46 @@ +language: rust + +addons: + apt: + packages: + - libcurl4-openssl-dev + - libelf-dev + - libdw-dev + +rust: + - nightly + - beta + - stable + - 1.2.0 + +before_script: + - pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH + +script: + - | + travis-cargo --only 1.2 test -- --features regexp && + travis-cargo --only stable test -- --features "regexp regexp_macros" && + travis-cargo --only beta test -- --features "regexp regexp_macros" && + travis-cargo --only nightly build -- --features "nightly core regexp" && + travis-cargo --only nightly test -- --features "regexp" && + travis-cargo bench && + travis-cargo --only stable doc -- --features "regexp" + +after_success: + - travis-cargo coveralls --no-sudo + +notifications: + webhooks: + urls: + - https://webhooks.gitter.im/e/9c035a194ac4fd4cc061 + on_success: change + on_failure: always + on_start: false + + +env: + global: + # override the default `--features unstable` used for the nightly branch (optional) + - TRAVIS_CARGO_NIGHTLY_FEATURE=nightly + +sudo: false diff --git third_party/rust/nom-1.2.4/CHANGELOG.md third_party/rust/nom-1.2.4/CHANGELOG.md new file mode 100644 index 000000000000..f1c331585779 --- /dev/null +++ third_party/rust/nom-1.2.4/CHANGELOG.md @@ -0,0 +1,555 @@ +# Change Log + +## [Unreleased][unreleased] + +### Changed + +## 1.2.4 - 2016-07-20 + +### Thanks +- @Phlosioneer for documentation fixes +- @sourrust for fixing offsets in `take_bits!` +- @ChrisMacNaughton for the XFS crate +- @pwoolcoc for `rest_s` +- @fitzgen for more `IResult` methods +- @gtors for the negative lookahead feature +- @frk1 and @jeandudey for little endian float parsing +- @jethrogb for fixing input usage in `many1` +- @acatton for beating me at nom golf :D + +### Added +- the `rest_s` method on `IResult` returns the remaining `&str` input +- `unwrap_err` and `unwrap_inc` methods on `IResult` +- `not!` will peek at the input and return `Done` if the underlying parser returned `Error` or `Incomplete`, without consuming the input +- `le_f32` and `le_f64` parse little endian floating point numbers (IEEE 754) +- + +### Fixed +- documentation fixes +- `take_bits!` is now more precise +- `many1` inccorectly used the `len` function instead of `input_len` +- the INI parser is simpler +- `recognize!` had an early `return` taht is removed now + +## 1.2.3 - 2016-05-10 + +### Thanks +- @lu-zero for the contribution guidelines +- @GuillaumeGomez for fixes on `length_bytes` and some documentation +- @Hywan for ducomentation and test fixes +- @Xirdus for correct trait import issues +- @mspiegel for the new AST example +- @cholcombe973 for adding the `cond_with_error!` combinator +- @tstorch for refactoring `many0!` +- @panicbit for the folding combinators +- @evestera for `separated_list!` fixes +- @DanielKeep for correcting some enum imports + +### Added +- Regular expression combinators starting with `re_bytes_` work on byte slices +- example parsing arithmetic expressions to an AST +- `cond_with_error!` works like `cond!` but will return `None` if the condition is false, and `Some(value)` if the underlying parser succeeded +- `fold_many0!`, `fold_many1!` and `fold_many_m_n!` will take a parser, an initial value and a combining function, and fold over the successful applications of the parser + +### Fixed +- `length_bytes!` converts the result of its child parser to usize +- `take_till!` now imports `InputLength` instead of assuming it's in scope +- `separated_list!` and `separated_nonempty_list!` will not consume the separator if there's no following successfully parsed value +- no more warnings on build + +### Changed +- simpler implementation of `many0!` + +## 1.2.2 - 2016-03-09 + +### Thanks +- @conradev for fixing take_until_s!` +- @GuillaumeGomez for some documentation fixes +- @frewsxcv for some documentation fixes +- @tstorch for some test refactorings + +### Added +- `nom::Err` now implements `std::error::Error` + +### Fixed +- `hex_u32` does not parses more than 8 chars now +- `take_while!` and `take_while1!` will not perturb the behaviour of `recognize!` anymore + +## 1.2.1 - 2016-02-23 + +### Thanks +- @sourrust for adding methods to `IResult` +- @tstorch for the test refactoring, and for adding methods to `IResult` and `Needed` +- @joelself for fixing the method system + +### Added + +- mapping methods over `IResult` and `Needed` + +### Changed + +- `apply_rf` is renamed to `apply_m`. This will not warrant a major version, since it is part missing from the methods feture added in the 1.2.0 release +- the `regexp_macros` feature that used `regex!` to precompile regular expressions has been replaced by the normal regex engine combined with `lazy_static` + +### Fixed + +- when a parser or combinator was returning an empty buffer as remaining part, it was generating one from a static empty string. This was messing with buffer offset calculation. Now, that empty slice is taken like this: `&input[input.len()..]`. +- The `regexp_macros` and `no_std` feature build again and are now tested with Travis CI + +## 1.2.0 - 2016-02-08 + +### Thanks +- @zentner-kyle for type inference fixes +- @joelself for his work on `&str` parsing and method parsers +- @GuillaumeGomez for implementing methods on `IResult` +- @dirk for the `alt_complete!` combinator +- @tstorch for a lot of refactoring work and unit tests additions +- @jansegre for the hex digit parsers +- @belgum for some documentation fixes +- @lwandrebeck for some documentation fixes and code fixes in `hex_digit` + +### Added +- `take_until_and_consume_s!` for consumption of string data until a tag +- more function patterns in `named!`. The error type can now be specified +- `alt_complete!` works like the `alt!` combinator, but tries the next branch if the current one returned `Incomplete`, instead of returning directly +- more unit tests for a lot of combinators +- hexadecimal digit parsers +- the `tuple!` combinator takes a list of parsers as argument, and applies them serially on the input. If all of them are successful, it willr eturn a tuple accumulating all the values. This combinator will (hopefully) replace most uses of `chain!` +- parsers can now be implemented as a method for a struct thanks to the `method!`, `call_m!` and `apply_rf!` combinators + +### Fixed +- there were type inference issues in a few combinators. They will now be easier to compile +- `peek!` compilation with bare functions +- `&str` parsers were splitting data at the byte level, not at the char level, which can result in inconsistencies in parsing UTF-8 characters. They now use character indexes +- some method implementations were missing on `ÃŒResult` (with specified error type instead of implicit) + +## 1.1.0 - 2016-01-01 + +This release adds a lot of features related to `&str` parsing. The previous versions +were focused on `&[u8]` and bit streams parsing, but there's a need for more text +parsing with nom. The parsing functions like `alpha`, `digit` and others will now +accept either a `&[u8]` or a `&str`, so there is no breaking change on that part. + +There are also a few performance improvements and documentation fixes. + +### Thanks +- @Binero for pushing the work on `&str` parsing +- @meh for fixing `Option` and `Vec` imports +- @hoodie for a documentation fix +- @joelself for some documentation fixes +- @vberger for his traits magic making `nom functions more generic + +### Added + +- string related parsers: `tag_s!`, `take_s!`, `is_a_s!`, `is_not_s!`, `take_while_s!`, `take_while1_s!`, `take_till_s! +- `value!` is a combinator that always returns the same value. If a child parser is passed as second argument, that value is returned when the child parser succeeds + +### Changed + +- `tag!` will now compare even on partial input. If it expects "abcd" but receives "ef", it will now return an `Error` instead of `Incomplete` +- `many0!` and others will preallocate a larger vector to avoid some copies and reallocations +- `alpha`, `digit`, `alphanumeric`, `space` and `multispace` now accept as input a `&[u8]` or a `&str`. Additionally, they return an error if they receive an empty input +- `take_while!`, `take_while1!`, `take_while_s!`, `take_while1_s!` wilreturn an error on empty input + +### Fixed + +- if the child parser of `many0!` or `many1!` returns `Incomplete`, it will return `Incomplete` too, possibly updating the needed size +- `Option,` `Some`, `None` and `Vec` are now used with full path imports + +## 1.0.1 - 2015-11-22 + +This releases makes the 1.0 version compatible with Rust 1.2 and 1.3 + +### Thanks +- @steveklabnik for fixing lifetime issues in Producers and Consumers + +## 1.0.0 - 2015-11-16 + +Stable release for nom. A lot of new features, a few breaking changes + +### Thanks +- @ahenry for macro fixes +- @bluss for fixing documentation +- @sourrust for cleaning code and debugging the new streaming utilities +- @meh for inline optimizations +- @ccmtaylor for fixing function imports +- @soro for improvements to the streaming utilities +- @breard-r for catching my typos +- @nelsonjchen for catching my typos too +- @divarvel for hex string parsers +- @mrordinaire for the `length_bytes!` combinator + +### Breaking changes +- `IResult::Error` can now use custom error types, and is generic over the input type +- Producers and consumers have been replaced. The new implementation uses less memory and integrates more with parsers +- `nom::ErrorCode` is now `nom::ErrorKind` +- `filter!` has been renamed to `take_while!` +- `chain!` will count how much data is consumed and use that number to calculate how much data is needed if a parser returned `Incomplete` +- `alt!` returns `Incomplete` if a child parser returned `Incomplete`, instead of skipping to the next parser +- `IResult` does not require a lifetime tag anymore, yay! + +### Added + +- `complete!` will return an error if the child parser returned `Incomplete` +- `add_error!` will wrap an error, but allow backtracking +- `hex_u32` parser + +### Fixed +- the behaviour around `Incomplete` is better for most parsers now + +## 0.5.0 - 2015-10-16 + +This release fixes a few issues and stabilizes the code. + +### Thanks +- @nox for documentation fixes +- @daboross for linting fixes +- @ahenry for fixing `tap!` and extending `dbg!` and `dbg_dmp!` +- @bluss for tracking down and fixing issues with unsafe code +- @meh for inlining parser functions +- @ccmtaylor for fixing import of `str::from_utf8` + +### Fixed +- `tap!`, `dbg!` and `dbg_dmp!` now accept function parameters + +### Changed +- the type used in `count_fixed!` must be `Copy` +- `chain!` calculates how much data is needed if one of the parsers returns `Incomplete +- optional parsers in `chain!` can return `Incomplete` + +## 0.4.0 - 2015-09-08 + +Considering the number of changes since the last release, this version can contain breaking changes, so the version number becomes 0.4.0. A lot of new features and performance improvements! + +### Thanks +- @frewsxcv for documentation fixes +- @ngrewe for his work on producers and consumers +- @meh for fixes on `chain!` and for the `rest` parser +- @daboross for refactoring `many0!` and `many1!` +- @aleksander for the `switch!` combinator idea +- @TechnoMancer for his help with bit level parsing +- @sxeraverx for pointing out a bug in `is_a!` + +### Fixed +- `count_fixed!` must take an explicit type as argument to generate the fixed-size array +- optional parsing behaviour in `chain!` +- `count!` can take 0 elements +- `is_a!` and `is_not!` can now consume the whole input + +### Added +- it is now possible to seek to the end of a `MemProducer` +- `opt!` returns `Done(input, None)` if `the child parser returned `Incomplete` +- `rest` will return the remaining input +- consumers can now seek to and from the end of input +- `switch!` applies a first parser then matches on its result to choose the next parser +- bit-level parsers +- character-level parsers +- regular expression parsers +- implementation of `take_till!`, `take_while!` and `take_while1!` + +### Changed +- `alt!` can return `Incomplete` +- the error analysis functions will now take references to functions instead of moving them +- performance improvements on producers +- performance improvement for `filter!` +- performance improvement for `count!`: a `Vec` of the right size is directly allocated + +## 0.3.11 - 2015-08-04 + +### Thanks +- @bluss for remarking that the crate included random junk lying non commited in my local repository + +### Fixed +- cleanup of my local repository will ship less files in the crates, resulting in a smaller download + +## 0.3.10 - 2015-08-03 + +### Added + +- `bits!` for bit level parsing. It indicates that all child parsers will take a `(&[u8], usize)`as input, with the second parameter indicating the bit offset in the first byte. This allows viewing a byte slice as a bit stream. Most combinators can be used directly under `bits!` +- `take_bits!` takes an integer type and a number of bits, consumes that number of bits and updates the offset, possibly by crossing byte boundaries +- bit level parsers are all written in `src/bits.rs` + +### Changed + +- Parsers that specifically handle bytes have been moved to src/bytes.rs`. This applies to `tag!`, `is_not!`, `is_a!`, `filter!`, `take!`, `take_str!`, `take_until_and_consume!`, `take_until!`, `take_until_either_and_consume!`, `take_until_either!` + +## 0.3.9 - 2015-07-20 + +### Thanks +- @badboy for fixing `filter!` +- @idmit for some documentation fixes + +### Added +- `opt_res!` applies a parser and transform its result in a Result. This parser never fails +- `cond_reduce!` takes an expression as parameter, applies the parser if the expression is true, and returns an error if the expression is false +- `tap!` pass the result of a parser to a block to manipulate it, but do not affect the parser's result +- `AccReader` is a Read+BufRead that supports data accumulation and partial consumption. The `consume` method must be called afterwardsto indicate how much was consumed +- Arithmetic expression evaluation and parsing example +- `u16!`, `u32!`, `u64!`, `i16!`, `i32!`, `i64!` take an expression as parameter, if the expression is true, apply the big endian integer parser, if false, the little endian version +- type information for combinators. This will make the documentation a bit easier to navigate + +### Fixed +- `map_opt!` and `map_res!` had issues with argument order due to bad macros +- `delimited!` did not compile for certain combinations of arguments +- `filter!` did not return a byte slice but a fixed array + +## 0.3.8 - 2015-07-03 + +### Added +- code coverage is now calculated automatically on Travis CI +- `Stepper`: wrap a `Producer`, and call the method `step` with a parser. This method will buffer data if there is not enough, apply the parser if there is, and keep the rest of the input in memory for the next call +- `ReadProducer`: takes something implementing `Read`, and makes a `Producer` out of it + +### Fixed +- the combinators `separated_pair!` and `delimited!` did not work because an implementation macro was not exported +- if a `MemProducer` reached its end, it should always return `Eof` +- `map!` had issues with argument matching + +## 0.3.7 - 2015-06-24 + +### Added +- `expr_res!` and `expr_opt!` evaluate an expression returning a Result or Opt and convert it to IResult +- `AsBytes` is implemented for fixed size arrays. This allows `tag!([41u8, 42u8])` + +### Fixed +- `count_fixed!` argument parsing works again + +## 0.3.6 - 2015-06-15 + +### Added +- documentation for a few functions +- the consumer trait now requires the `failed(&self, error_code)` method in case of parsing error +- `named!` now handles thge alternative `named!(pub fun_name, ...)` + +### Fixed +- `filter!` now returns the whole input if the filter function never returned false +- `take!` casts its argument as usize, so it can accepts any integer type now + +## 0.3.5 - 2015-06-10 + +### Thanks +- @cmr for some documentation fixes + +### Added +- `count_fixed!` returns a fixed array + +### Fixed +- `count!` is back to the previous behaviour, returning a `Vec` for sizes known at runtime + +### Changed +- functions and traits exported from `nom::util` are now directly in `nom::` + +## 0.3.4 - 2015-06-09 + +### Thanks +- @andrew-d for fixes on `cond!` +- @keruspe for features in `chain!` + +### Added +- `chain!` can now have mutable fields + +### Fixed +- `cond!` had an infinite macro recursion + +### Changed +- `chain!` generates less code now. No apprent compilation time improvement + +## 0.3.3 - 2015-06-09 + +### Thanks +- @andrew-d for the little endian signed integer parsers +- @keruspe for fixes on `count!` + +### Added +- `le_i8`, `le_i16`, `le_i32`, `le_i64`: little endian signed integer parsers + +### Changed +- the `alt!` parser compiles much faster, even with more than 8 branches +- `count!` can now return a fixed size array instead of a growable vector + +## 0.3.2 - 2015-05-31 + +### Thanks +- @keruspe for the `take_str` parser and the function application combinator + +### Added +- `take_str!`: takes the specified number of bytes and return a UTF-8 string +- `apply!`: do partial application on the parameters of a function + +### Changed +- `Needed::Size` now contains a `usize` instead of a `u32` + +## 0.3.1 - 2015-05-21 + +### Thanks +- @divarvel for the big endian signed integer parsers + +### Added +- `be_i8`, `be_i16`, `be_i32`, `be_i64`: big endian signed integer parsers +- the `core` feature can be passed to cargo to build with `no_std` +- colored hexdump can be generated from error chains + +## 0.3.0 - 2015-05-07 + +### Thanks +- @filipegoncalves for some documentation and the new eof parser +- @CrimsonVoid for putting fully qualified types in the macros +- @lu_zero for some documentation fixes + +### Added +- new error types that can contain an error code, an input slice, and a list of following errors +- `error!` will cut backtracking and return directly from the parser, with a specified error code +- `eof` parser, successful if there is no more input +- specific error codes for the parsers provided by nom + +### Changed +- fully qualified types in macros. A lot of imports are not needed anymore + +### Removed +- `FlatMap`, `FlatpMapOpt` and `Functor` traits (replaced by `map!`, `map_opt!` and `map_res!`) + +## 0.2.2 - 2015-04-12 + +### Thanks +- @filipegoncalves and @thehydroimpulse for debugging an infinite loop in many0 and many1 +- @thehydroimpulse for suggesting public named parsers +- @skade for removing the dependency on the collections gate + +### Added +- `named!` can now declare public functions like this: `named!(pub tst, tag!("abcd"));` +- `pair!(X,Y)` returns a tuple `(x, y)` +- `separated_pair!(X, sep, Y)` returns a tuple `(x, y)` +- `preceded!(opening, X)` returns `x` +- `terminated!(X, closing)` returns `x` +- `delimited(opening, X, closing)` returns `x` +- `separated_list(sep, X)` returns a `Vec` +- `separated_nonempty_list(sep, X)` returns a `Vec` of at list one element + +### Changed +- `many0!` and `many1!` forbid parsers that do not consume input +- `is_a!`, `is_not!`, `alpha`, `digit`, `space`, `multispace` will now return an error if they do not consume at least one byte + +## 0.2.1 - 2015-04-04 + +### Thanks +- @mtsr for catching the remaining debug println! +- @jag426 who killed a lot of warnings +- @skade for removing the dependency on the core feature gate + + +### Added +- little endian unsigned int parsers le_u8, le_u16, le_u32, le_u64 +- `count!` to apply a parser a specified number of times +- `cond!` applies a parser if the condition is met +- more parser development tools in `util::*` + +### Fixed +- in one case, `opt!` would not compile + +### Removed +- most of the feature gates are now removed. The only one still needed is `collections` + +## 0.2.0 - 2015-03-24 +*works with `rustc 1.0.0-dev (81e2396c7 2015-03-19) (built 2015-03-19)`* + +### Thanks +- Ryman for the AsBytes implementation +- jag426 and jaredly for documentation fixes +- eternaleye on #rust IRC for his help on the new macro syntax + +### Changed +- the AsBytes trait improves readability, no more b"...", but "..." instead +- Incomplete will now hold either Needed;;Unknown, or Needed::Size(u32). Matching on Incomplete without caring for the value is done with `Incomplete(_)`, but if more granularity is mandatory, `Needed` can be matched too +- `alt!` can pass the result of the parser to a closure +- the `take_*` macros changed behaviour, the default case is now not to consume the separator. The macros have been renamed as follows: `take_until!` -> `take_until_and_consume!`, `take_until_and_leave!` -> `take_until!`, `take_until_either_and_leave!` -> `take_until_either!`, `take_until_either!` -> `take_until_either_and_consume!` + +### Added +- `peek!` macro: matches the future input but does not consume it +- `length_value!` macro: the first argument is a parser returning a `n` that can cast to usize, then applies the second parser `n` times. The macro has a variant with a third argument indicating the expected input size for the second parser +- benchmarks are available at https://github.com/Geal/nom_benchmarks +- more documentation +- **Unnamed parser syntax**: warning, this is a breaking change. With this new syntax, the macro combinators do not generate functions anymore, they create blocks. That way, they can be nested, for better readability. The `named!` macro is provided to create functions from parsers. Please be aware that nesting parsers comes with a small cost of compilation time, negligible in most cases, but can quickly get to the minutes scale if not careful. If this happens, separate your parsers in multiple subfunctions. +- `named!`, `closure!` and `call!` macros used to support the unnamed syntax +- `map!`, `map_opt!` and `map_res!` to combine a parser with a normal function, transforming the input directly, or returning an `Option` or `Result` + +### Fixed +- `is_a!` is now working properly + +### Removed +- the `o!` macro does less than `chain!`, so it has been removed +- the `fold0!` and `fold1!` macros were too complex and awkward to use, the `many*` combinators will be useful for most uses for now + +## 0.1.6 - 2015-02-24 +### Changed +- consumers must have an end method that will be called after parsing + +### Added +- big endian unsigned int and float parsers: be_u8, be_u16, be_u32, be_u64, be_f32, be_f64 +- producers can seek +- function and macros documentation +- README documentation +### Fixed +- lifetime declarations +- tag! can return Incomplete + +## 0.1.5 - 2015-02-17 +### Changed +- traits were renamed: FlatMapper -> FlatMap, Mapper -> FlatMapOpt, Mapper2 -> Functor + +### Fixed +- woeks with rustc f1bb6c2f4 + +## 0.1.4 - 2015-02-17 +### Changed +- the chaining macro can take optional arguments with '?' + +## 0.1.3 - 2015-02-16 +### Changed +- the chaining macro now takes the closure at the end of the argument list + +## 0.1.2 - 2015-02-16 +### Added +- flat_map implementation for <&[u8], &[u8]> +- chaining macro +- partial MP4 parser example + + +## 0.1.1 - 2015-02-06 +### Fixed +- closure syntax change + +## Compare code + +* [unreleased]: https://github.com/Geal/nom/compare/1.2.4...HEAD +* [1.2.3]: https://github.com/Geal/nom/compare/1.2.3...1.2.4 +* [1.2.3]: https://github.com/Geal/nom/compare/1.2.2...1.2.3 +* [1.2.2]: https://github.com/Geal/nom/compare/1.2.1...1.2.2 +* [1.2.1]: https://github.com/Geal/nom/compare/1.2.0...1.2.1 +* [1.2.0]: https://github.com/Geal/nom/compare/1.1.0...1.2.0 +* [1.1.0]: https://github.com/Geal/nom/compare/1.0.1...1.1.0 +* [1.0.1]: https://github.com/Geal/nom/compare/1.0.0...1.0.1 +* [1.0.0]: https://github.com/Geal/nom/compare/0.5.0...1.0.0 +* [0.5.0]: https://github.com/geal/nom/compare/0.4.0...0.5.0 +* [0.4.0]: https://github.com/geal/nom/compare/0.3.11...0.4.0 +* [0.3.11]: https://github.com/geal/nom/compare/0.3.10...0.3.11 +* [0.3.10]: https://github.com/geal/nom/compare/0.3.9...0.3.10 +* [0.3.9]: https://github.com/geal/nom/compare/0.3.8...0.3.9 +* [0.3.8]: https://github.com/Geal/nom/compare/0.3.7...0.3.8 +* [0.3.7]: https://github.com/Geal/nom/compare/0.3.6...0.3.7 +* [0.3.6]: https://github.com/Geal/nom/compare/0.3.5...0.3.6 +* [0.3.5]: https://github.com/Geal/nom/compare/0.3.4...0.3.5 +* [0.3.4]: https://github.com/Geal/nom/compare/0.3.3...0.3.4 +* [0.3.3]: https://github.com/Geal/nom/compare/0.3.2...0.3.3 +* [0.3.2]: https://github.com/Geal/nom/compare/0.3.1...0.3.2 +* [0.3.1]: https://github.com/Geal/nom/compare/0.3.0...0.3.1 +* [0.3.0]: https://github.com/Geal/nom/compare/0.2.2...0.3.0 +* [0.2.2]: https://github.com/Geal/nom/compare/0.2.1...0.2.2 +* [0.2.1]: https://github.com/Geal/nom/compare/0.2.0...0.2.1 +* [0.2.0]: https://github.com/Geal/nom/compare/0.1.6...0.2.0 +* [0.1.6]: https://github.com/Geal/nom/compare/0.1.5...0.1.6 +* [0.1.5]: https://github.com/Geal/nom/compare/0.1.4...0.1.5 +* [0.1.4]: https://github.com/Geal/nom/compare/0.1.3...0.1.4 +* [0.1.3]: https://github.com/Geal/nom/compare/0.1.2...0.1.3 +* [0.1.2]: https://github.com/Geal/nom/compare/0.1.1...0.1.2 +* [0.1.1]: https://github.com/Geal/nom/compare/0.1.0...0.1.1 diff --git third_party/rust/nom-1.2.4/Cargo.toml third_party/rust/nom-1.2.4/Cargo.toml new file mode 100644 index 000000000000..ae8045bf1135 --- /dev/null +++ third_party/rust/nom-1.2.4/Cargo.toml @@ -0,0 +1,38 @@ +[package] + +name = "nom" +version = "1.2.4" +authors = [ "contact@geoffroycouprie.com" ] +description = "A byte-oriented, zero-copy, parser combinators library" +license = "MIT" +repository = "https://github.com/Geal/nom" +readme = "README.md" +documentation = "http://rust.unhandledexpression.com/nom/" +keywords = ["parser", "parser-combinators", "parsing", "streaming", "bit"] + +include = [ + "CHANGELOG.md", + "LICENSE", + ".gitignore", + ".travis.yml", + "Cargo.toml", + "src/*.rs", + "tests/*.rs" +] + +[features] +core = [] +nightly = [] +default = ["stream"] +regexp = ["regex"] +regexp_macros = ["regexp", "lazy_static"] +stream = [] + +[dependencies.regex] +version = "^0.1.56" +optional = true + +[dependencies.lazy_static] +version = "^0.2.1" +optional = true + diff --git third_party/rust/nom-1.2.4/LICENSE third_party/rust/nom-1.2.4/LICENSE new file mode 100644 index 000000000000..0bd6a1c33dc6 --- /dev/null +++ third_party/rust/nom-1.2.4/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2015 Geoffroy Couprie + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git third_party/rust/nom-1.2.4/src/bits.rs third_party/rust/nom-1.2.4/src/bits.rs new file mode 100644 index 000000000000..a8bd8b24aa99 --- /dev/null +++ third_party/rust/nom-1.2.4/src/bits.rs @@ -0,0 +1,220 @@ +//! Bit level parsers and combinators +//! +//! Bit parsing is handled by tweaking the input in most macros. +//! In byte level parsing, the input is generally a `&[u8]` passed from combinator +//! to combinator until the slices are manipulated. +//! +//! Bit parsers take a `(&[u8], usize)` as input. The first part of the tuple is an byte slice, +//! the second part is a bit offset in the first byte of the slice. +//! +//! By passing a pair like this, we can leverage most of the combinators, and avoid +//! transforming the whole slice to a vector of booleans. This should make it easy +//! to see a byte slice as a bit stream, and parse code points of arbitrary bit length. + + +/// `bits!( parser ) => ( &[u8], (&[u8], usize) -> IResult<(&[u8], usize), T> ) -> IResult<&[u8], T>` +/// transforms its byte slice input into a bit stream for the underlying parsers +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!( take_3_bits, bits!( take_bits!( u8, 3 ) ) ); +/// +/// let input = vec![0b10101010, 0b11110000, 0b00110011]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_3_bits( sl ), Done(&sl[1..], 5) ); +/// # } +#[macro_export] +macro_rules! bits ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + bits_impl!($i, $submac!($($args)*)); + ); + ($i:expr, $f:expr) => ( + bits_impl!($i, call!($f)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! bits_impl ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + let input = ($i, 0usize); + match $submac!(input, $($args)*) { + $crate::IResult::Error(e) => { + let err = match e { + $crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k), + $crate::Err::Position(k, (i,b)) | $crate::Err::NodePosition(k, (i,b), _) => { + $crate::Err::Position(k, &i[b/8..]) + } + }; + $crate::IResult::Error(err) + } + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + //println!("bits parser returned Needed::Size({})", i); + $crate::IResult::Incomplete($crate::Needed::Size(i / 8 + 1)) + }, + $crate::IResult::Done((i, bit_index), o) => { + let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ; + //println!("bit index=={} => byte index=={}", bit_index, byte_index); + $crate::IResult::Done(&i[byte_index..], o) + } + } + } + ); +); + +/// `take_bits!(type, nb) => ( (&[T], usize), U, usize) -> IResult<(&[T], usize), U>` +/// generates a parser consuming the specified number of bits. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!( take_pair<(u8, u8)>, bits!( pair!( take_bits!( u8, 3 ), take_bits!(u8, 5) ) ) ); +/// +/// let input = vec![0b10101010, 0b11110000, 0b00110011]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_pair( sl ), Done(&sl[1..], (5, 10)) ); +/// assert_eq!(take_pair( &sl[1..] ), Done(&sl[2..], (7, 16)) ); +/// # } +/// ``` +#[macro_export] +macro_rules! take_bits ( + ($i:expr, $t:ty, $count:expr) => ( + { + use std::ops::Div; + //println!("taking {} bits from {:?}", $count, $i); + let (input, bit_offset) = $i; + let res : $crate::IResult<(&[u8],usize), $t> = if $count == 0 { + $crate::IResult::Done( (input, bit_offset), 0) + } else { + let cnt = ($count as usize + bit_offset).div(8); + if input.len() * 8 < $count as usize + bit_offset { + //println!("returning incomplete: {}", $count as usize + bit_offset); + $crate::IResult::Incomplete($crate::Needed::Size($count as usize)) + } else { + let mut acc:$t = 0; + let mut offset: usize = bit_offset; + let mut remaining: usize = $count; + let mut end_offset: usize = 0; + + for byte in input.iter().take(cnt + 1) { + if remaining == 0 { + break; + } + let val: $t = if offset == 0 { + *byte as $t + } else { + ((*byte << offset) as u8 >> offset) as $t + }; + + if remaining < 8 - offset { + acc += val >> (8 - offset - remaining); + end_offset = remaining + offset; + break; + } else { + acc += val << (remaining - (8 - offset)); + remaining -= 8 - offset; + offset = 0; + } + } + $crate::IResult::Done( (&input[cnt..], end_offset) , acc) + } + }; + res + } + ); +); + +/// matches an integer pattern to a bitstream. The number of bits of the input to compare must be specified +#[macro_export] +macro_rules! tag_bits ( + ($i:expr, $t:ty, $count:expr, $p: pat) => ( + { + match take_bits!($i, $t, $count) { + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + if let $p = o { + let res: $crate::IResult<(&[u8],usize),$t> = $crate::IResult::Done(i, o); + res + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagBits, $i)) + } + }, + _ => { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagBits, $i)) + } + } + } + ) +); + +#[cfg(test)] +mod tests { + use internal::{IResult,Needed,Err}; + use ErrorKind; + + #[test] + fn take_bits() { + let input = vec![0b10101010, 0b11110000, 0b00110011]; + let sl = &input[..]; + + assert_eq!(take_bits!( (sl, 0), u8, 0 ), IResult::Done((sl, 0), 0)); + assert_eq!(take_bits!( (sl, 0), u8, 8 ), IResult::Done((&sl[1..], 0), 170)); + assert_eq!(take_bits!( (sl, 0), u8, 3 ), IResult::Done((&sl[0..], 3), 5)); + assert_eq!(take_bits!( (sl, 0), u8, 6 ), IResult::Done((&sl[0..], 6), 42)); + assert_eq!(take_bits!( (sl, 1), u8, 1 ), IResult::Done((&sl[0..], 2), 0)); + assert_eq!(take_bits!( (sl, 1), u8, 2 ), IResult::Done((&sl[0..], 3), 1)); + assert_eq!(take_bits!( (sl, 1), u8, 3 ), IResult::Done((&sl[0..], 4), 2)); + assert_eq!(take_bits!( (sl, 6), u8, 3 ), IResult::Done((&sl[1..], 1), 5)); + assert_eq!(take_bits!( (sl, 0), u16, 10 ), IResult::Done((&sl[1..], 2), 683)); + assert_eq!(take_bits!( (sl, 0), u16, 8 ), IResult::Done((&sl[1..], 0), 170)); + assert_eq!(take_bits!( (sl, 6), u16, 10 ), IResult::Done((&sl[2..], 0), 752)); + assert_eq!(take_bits!( (sl, 6), u16, 11 ), IResult::Done((&sl[2..], 1), 1504)); + assert_eq!(take_bits!( (sl, 0), u32, 20 ), IResult::Done((&sl[2..], 4), 700163)); + assert_eq!(take_bits!( (sl, 4), u32, 20 ), IResult::Done((&sl[3..], 0), 716851)); + assert_eq!(take_bits!( (sl, 4), u32, 22 ), IResult::Incomplete(Needed::Size(22))); + } + + #[test] + fn tag_bits() { + let input = vec![0b10101010, 0b11110000, 0b00110011]; + let sl = &input[..]; + + assert_eq!(tag_bits!( (sl, 0), u8, 3, 0b101), IResult::Done((&sl[0..], 3), 5)); + assert_eq!(tag_bits!( (sl, 0), u8, 4, 0b1010), IResult::Done((&sl[0..], 4), 10)); + } + + named!(ch<(&[u8],usize),(u8,u8)>, + chain!( + tag_bits!(u8, 3, 0b101) ~ + x: take_bits!(u8, 4) ~ + y: take_bits!(u8, 5) , + || { (x,y) } + ) + ); + + #[test] + fn chain_bits() { + let input = vec![0b10101010, 0b11110000, 0b00110011]; + let sl = &input[..]; + assert_eq!(ch((&input[..],0)), IResult::Done((&sl[1..], 4), (5,15))); + assert_eq!(ch((&input[..],4)), IResult::Done((&sl[2..], 0), (7,16))); + assert_eq!(ch((&input[..1],0)), IResult::Incomplete(Needed::Size(12))); + } + + named!(ch_bytes<(u8,u8)>, bits!(ch)); + #[test] + fn bits_to_bytes() { + let input = vec![0b10101010, 0b11110000, 0b00110011]; + assert_eq!(ch_bytes(&input[..]), IResult::Done(&input[2..], (5,15))); + assert_eq!(ch_bytes(&input[..1]), IResult::Incomplete(Needed::Size(2))); + assert_eq!(ch_bytes(&input[1..]), IResult::Error(Err::Position(ErrorKind::TagBits, &input[1..]))); + } +} diff --git third_party/rust/nom-1.2.4/src/bytes.rs third_party/rust/nom-1.2.4/src/bytes.rs new file mode 100644 index 000000000000..3f31598dc344 --- /dev/null +++ third_party/rust/nom-1.2.4/src/bytes.rs @@ -0,0 +1,1027 @@ +//! Byte level parsers and combinators +//! + +/// `recognize!(&[T] -> IResult<&[T], O> ) => &[T] -> IResult<&[T], &[T]>` +/// if the child parser was successful, return the consumed input as produced value +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(x, recognize!(delimited!(tag!("")))); +/// let r = x(&b" aaa"[..]); +/// assert_eq!(r, Done(&b" aaa"[..], &b""[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! recognize ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::HexDisplay; + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,_) => { + let index = ($i).offset(i); + $crate::IResult::Done(i, &($i)[..index]) + }, + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } + ); + ($i:expr, $f:expr) => ( + recognize!($i, call!($f)) + ); +); + +/// `tag!(&[T]: nom::AsBytes) => &[T] -> IResult<&[T], &[T]>` +/// declares a byte array as a suite to recognize +/// +/// consumes the recognized characters +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(x, tag!("abcd")); +/// let r = x(&b"abcdefgh"[..]); +/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + let len = $i.len(); + let blen = $bytes.len(); + let m = if len < blen { len } else { blen }; + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: $crate::IResult<_,_> = if reduced != b { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Tag, $i)) + } else if m < blen { + $crate::IResult::Incomplete($crate::Needed::Size(blen)) + } else { + $crate::IResult::Done(&$i[blen..], reduced) + }; + res + } + ); +); + +/// `is_not!(&[T:AsBytes]) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes that do not appear in the provided array +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!( not_space, is_not!( " \t\r\n" ) ); +/// +/// let r = not_space(&b"abcdefgh\nijkl"[..]); +/// assert_eq!(r, Done(&b"\nijkl"[..], &b"abcdefgh"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! is_not( + ($input:expr, $arr:expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $arr; + let bytes = as_bytes(&expected); + + is_not_bytes!($input, bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! is_not_bytes ( + ($input:expr, $bytes:expr) => ( + { + use $crate::InputLength; + let res: $crate::IResult<_,_> = match $input.iter().position(|c| { + for &i in $bytes.iter() { + if *c == i { return true } + } + false + }) { + Some(0) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsNot,$input)), + Some(n) => { + let res = $crate::IResult::Done(&$input[n..], &$input[..n]); + res + }, + None => { + $crate::IResult::Done(&$input[$input.input_len()..], $input) + } + }; + res + } + ); +); + +/// `is_a!(&[T]) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes that appear in the provided array +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(abcd, is_a!( "abcd" )); +/// +/// let r1 = abcd(&b"aaaaefgh"[..]); +/// assert_eq!(r1, Done(&b"efgh"[..], &b"aaaa"[..])); +/// +/// let r2 = abcd(&b"dcbaefgh"[..]); +/// assert_eq!(r2, Done(&b"efgh"[..], &b"dcba"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! is_a ( + ($input:expr, $arr:expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $arr; + let bytes = as_bytes(&expected); + + is_a_bytes!($input, bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! is_a_bytes ( + ($input:expr, $bytes:expr) => ( + { + use $crate::InputLength; + let res: $crate::IResult<_,_> = match $input.iter().position(|c| { + for &i in $bytes.iter() { + if *c == i { return false } + } + true + }) { + Some(0) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsA,$input)), + Some(n) => { + let res: $crate::IResult<_,_> = $crate::IResult::Done(&$input[n..], &$input[..n]); + res + }, + None => { + $crate::IResult::Done(&$input[($input).input_len()..], $input) + } + }; + res + } + ); +); + +/// `escaped!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], &[T]>` +/// matches a byte string with escaped characters. +/// +/// The first argument matches the normal characters (it must not accept the control character), the second argument is the control character (like `\` in most languages), +/// the third argument matches the escaped characters +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::alpha; +/// # fn main() { +/// named!(esc, escaped!(call!(alpha), '\\', is_a_bytes!(&b"\"n\\"[..]))); +/// assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..])); +/// assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! escaped ( + ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => ( + { + escaped1!($i, $submac!($($args)*), $control_char, $($rest)*) + } + ); + + ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => ( + escaped1!($i, call!($f), $control_char, $($rest)*) + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! escaped1 ( + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_impl!($i, $submac1!($($args)*), $control_char, $submac2!($($args2)*)) + } + ); + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => ( + escaped_impl!($i, $submac1!($($args)*), $control_char, call!($g)) + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! escaped_impl ( + ($i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $escapable:ident!( $($args2:tt)* )) => ( + { + use $crate::InputLength; + let cl = || { + use $crate::HexDisplay; + let mut index = 0; + + while index < $i.len() { + if let $crate::IResult::Done(i,_) = $normal!(&$i[index..], $($args)*) { + if i.is_empty() { + return $crate::IResult::Done(&$i[$i.input_len()..], $i) + } else { + index = $i.offset(i); + } + } else if $i[index] == $control_char as u8 { + if index + 1 >= $i.len() { + return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Escaped,&$i[index..])); + } else { + match $escapable!(&$i[index+1..], $($args2)*) { + $crate::IResult::Done(i,_) => { + if i.is_empty() { + return $crate::IResult::Done(&$i[$i.input_len()..], $i) + } else { + index = $i.offset(i); + } + }, + $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i), + $crate::IResult::Error(e) => return $crate::IResult::Error(e) + } + } + } else { + if index == 0 { + return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Escaped,&$i[index..])) + } else { + return $crate::IResult::Done(&$i[index..], &$i[..index]) + } + } + } + $crate::IResult::Done(&$i[index..], &$i[..index]) + }; + match cl() { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => { + return $crate::IResult::Error($crate::Err::NodePosition($crate::ErrorKind::Escaped, $i, Box::new(e))) + } + } + } + ); +); + +/// `escaped_transform!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], Vec>` +/// matches a byte string with escaped characters. +/// +/// The first argument matches the normal characters (it must not match the control character), the second argument is the control character (like `\` in most languages), +/// the third argument matches the escaped characters and trnasforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::alpha; +/// # use std::str::from_utf8; +/// # fn main() { +/// fn to_s(i:Vec) -> String { +/// String::from_utf8_lossy(&i).into_owned() +/// } + +/// named!(transform < String >, +/// map!( +/// escaped_transform!(call!(alpha), '\\', +/// alt!( +/// tag!("\\") => { |_| &b"\\"[..] } +/// | tag!("\"") => { |_| &b"\""[..] } +/// | tag!("n") => { |_| &b"\n"[..] } +/// ) +/// ), to_s +/// ) +/// ); +/// assert_eq!(transform(&b"ab\\\"cd"[..]), Done(&b""[..], String::from("ab\"cd"))); +/// # } +/// ``` +#[macro_export] +macro_rules! escaped_transform ( + ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => ( + { + escaped_transform1!($i, $submac!($($args)*), $control_char, $($rest)*) + } + ); + + ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => ( + escaped_transform1!($i, call!($f), $control_char, $($rest)*) + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! escaped_transform1 ( + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_transform_impl!($i, $submac1!($($args)*), $control_char, $submac2!($($args2)*)) + } + ); + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => ( + escaped_transform_impl!($i, $submac1!($($args)*), $control_char, call!($g)) + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! escaped_transform_impl ( + ($i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $transform:ident!( $($args2:tt)* )) => ( + { + use $crate::InputLength; + let cl = || { + use $crate::HexDisplay; + let mut index = 0; + let mut res = Vec::new(); + + while index < $i.len() { + if let $crate::IResult::Done(i,o) = $normal!(&$i[index..], $($args)*) { + res.extend(o.iter().cloned()); + if i.is_empty() { + return $crate::IResult::Done(&$i[$i.input_len()..], res) + } else { + index = $i.offset(i); + } + } else if $i[index] == $control_char as u8 { + if index + 1 >= $i.len() { + return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::EscapedTransform,&$i[index..])); + } else { + match $transform!(&$i[index+1..], $($args2)*) { + $crate::IResult::Done(i,o) => { + res.extend(o.iter().cloned()); + if i.is_empty() { + return $crate::IResult::Done(&$i[$i.input_len()..], res) + } else { + index = $i.offset(i); + } + }, + $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i), + $crate::IResult::Error(e) => return $crate::IResult::Error(e) + } + } + } else { + if index == 0 { + return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::EscapedTransform,&$i[index..])) + } else { + return $crate::IResult::Done(&$i[index..], res) + } + } + } + $crate::IResult::Done(&$i[index..], res) + }; + match cl() { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => { + return $crate::IResult::Error($crate::Err::NodePosition($crate::ErrorKind::EscapedTransform, $i, Box::new(e))) + } + } + } + ) +); + +/// `take_while!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes until the provided function fails. +/// +/// The argument is either a function `T -> bool` or a macro returning a `bool`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while!( is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Done(&b"\nefgh"[..], &b"abcd"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! take_while ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $input.iter().position(|c| !$submac!(*c, $($args)*)) { + Some(n) => { + let res:$crate::IResult<_,_> = $crate::IResult::Done(&$input[n..], &$input[..n]); + res + }, + None => { + $crate::IResult::Done(&$input[($input).len()..], $input) + } + } + } + ); + ($input:expr, $f:expr) => ( + take_while!($input, call!($f)); + ); +); + +/// `take_while1!(&[T] -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest (non empty) list of bytes until the provided function fails. +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool +#[macro_export] +macro_rules! take_while1 ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + if ($input).input_len() == 0 { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeWhile1,$input)) + } else { + match $input.iter().position(|c| !$submac!(*c, $($args)*)) { + Some(0) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeWhile1,$input)), + Some(n) => { + $crate::IResult::Done(&$input[n..], &$input[..n]) + }, + None => { + $crate::IResult::Done(&$input[($input).len()..], $input) + } + } + } + } + ); + ($input:expr, $f:expr) => ( + take_while1!($input, call!($f)); + ); +); + +/// `take_till!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes until the provided function succeeds +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool +#[macro_export] +macro_rules! take_till ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + match $input.iter().position(|c| $submac!(c, $($args)*)) { + Some(n) => $crate::IResult::Done(&$input[n..], &$input[..n]), + None => $crate::IResult::Done(&$input[($input).input_len()..], $input) + } + } + ); + ($input:expr, $f:expr) => ( + take_till!($input, call!($f)); + ); +); + +/// `take!(nb) => &[T] -> IResult<&[T], &[T]>` +/// generates a parser consuming the specified number of bytes +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// // Desmond parser +/// named!(take5, take!( 5 ) ); +/// +/// let a = b"abcdefgh"; +/// +/// assert_eq!(take5(&a[..]), Done(&b"fgh"[..], &b"abcde"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! take ( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res: $crate::IResult<_,_> = if $i.len() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) + }; + res + } + ); +); + +/// `take!(nb) => &[T] -> IResult<&[T], &str>` +/// same as take! but returning a &str +#[macro_export] +macro_rules! take_str ( + ( $i:expr, $size:expr ) => ( map_res!($i, take!($size), ::std::str::from_utf8) ); +); + +/// `take_until_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>` +/// generates a parser consuming bytes until the specified byte sequence is found, and consumes it +#[macro_export] +macro_rules! take_until_and_consume( + ($i:expr, $inp:expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + take_until_and_consume_bytes!($i, bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! take_until_and_consume_bytes ( + ($i:expr, $bytes:expr) => ( + { + let res: $crate::IResult<_,_> = if $bytes.len() > $i.len() { + $crate::IResult::Incomplete($crate::Needed::Size($bytes.len())) + } else { + let mut index = 0; + let mut parsed = false; + + for idx in 0..$i.len() { + if idx + $bytes.len() > $i.len() { + index = idx; + break; + } + if &$i[idx..idx + $bytes.len()] == $bytes { + parsed = true; + index = idx; + break; + } + } + + if parsed { + $crate::IResult::Done(&$i[(index + $bytes.len())..], &$i[0..index]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilAndConsume,$i)) + } + }; + res + } + ); +); + +/// `take_until!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data until it finds the specified tag +#[macro_export] +macro_rules! take_until( + ($i:expr, $inp:expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + take_until_bytes!($i, bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! take_until_bytes( + ($i:expr, $bytes:expr) => ( + { + let res: $crate::IResult<_,_> = if $bytes.len() > $i.len() { + $crate::IResult::Incomplete($crate::Needed::Size($bytes.len())) + } else { + let mut index = 0; + let mut parsed = false; + + for idx in 0..$i.len() { + if idx + $bytes.len() > $i.len() { + index = idx; + break; + } + if &$i[idx..idx+$bytes.len()] == $bytes { + parsed = true; + index = idx; + break; + } + } + + if parsed { + $crate::IResult::Done(&$i[index..], &$i[0..index]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntil,$i)) + } + }; + res + } + ); +); + +/// `take_until_either_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data until it finds any of the specified characters, and consume it +#[macro_export] +macro_rules! take_until_either_and_consume( + ($i:expr, $inp:expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + take_until_either_and_consume_bytes!($i, bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! take_until_either_and_consume_bytes( + ($i:expr, $bytes:expr) => ( + { + let res: $crate::IResult<_,_> = if 1 > $i.len() { + $crate::IResult::Incomplete($crate::Needed::Size(1)) + } else { + let mut index = 0; + let mut parsed = false; + + for idx in 0..$i.len() { + if idx + 1 > $i.len() { + index = idx; + break; + } + for &t in $bytes.iter() { + if $i[idx] == t { + parsed = true; + index = idx; + break; + } + } + if parsed { break; } + } + + if parsed { + $crate::IResult::Done(&$i[(index+1)..], &$i[0..index]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilEitherAndConsume,$i)) + } + }; + res + } + ); +); + +/// `take_until_either!(tag) => &[T] -> IResult<&[T], &[T]>` +#[macro_export] +macro_rules! take_until_either( + ($i:expr, $inp:expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + take_until_either_bytes!($i, bytes) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! take_until_either_bytes( + ($i:expr, $bytes:expr) => ( + { + let res: $crate::IResult<_,_> = if 1 > $i.len() { + $crate::IResult::Incomplete($crate::Needed::Size(1)) + } else { + let mut index = 0; + let mut parsed = false; + + for idx in 0..$i.len() { + if idx + 1 > $i.len() { + index = idx; + break; + } + for &t in $bytes.iter() { + if $i[idx] == t { + parsed = true; + index = idx; + break; + } + } + if parsed { break; } + } + + if parsed { + $crate::IResult::Done(&$i[index..], &$i[0..index]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilEither,$i)) + } + }; + res + } + ); +); + +/// `length_bytes!(&[T] -> IResult<&[T], nb>) => &[T] -> IResult<&[T], &[T]> +/// gets a number from the first parser, then extracts that many bytes from the +/// remaining stream +#[macro_export] +macro_rules! length_bytes( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1,nb) => { + let nb = nb as usize; + let length_remaining = i1.len(); + if length_remaining < nb { + $crate::IResult::Incomplete($crate::Needed::Size(nb - length_remaining)) + } else { + $crate::IResult::Done(&i1[nb..], &i1[..nb]) + } + } + } + } + ); + ($i:expr, $f:expr) => ( + length_bytes!($i, call!($f)) + ) +); + +#[cfg(test)] +mod tests { + use internal::Needed; + use internal::IResult::*; + use internal::Err::*; + use util::ErrorKind; + use nom::{alpha, digit, hex_digit, oct_digit, alphanumeric, space, multispace}; + + #[test] + fn is_a() { + named!(a_or_b, is_a!(&b"ab"[..])); + + let a = &b"abcd"[..]; + assert_eq!(a_or_b(a), Done(&b"cd"[..], &b"ab"[..])); + + let b = &b"bcde"[..]; + assert_eq!(a_or_b(b), Done(&b"cde"[..], &b"b"[..])); + + let c = &b"cdef"[..]; + assert_eq!(a_or_b(c), Error(Position(ErrorKind::IsA,c))); + + let d = &b"bacdef"[..]; + assert_eq!(a_or_b(d), Done(&b"cdef"[..], &b"ba"[..])); + } + + #[test] + fn is_not() { + named!(a_or_b, is_not!(&b"ab"[..])); + + let a = &b"cdab"[..]; + assert_eq!(a_or_b(a), Done(&b"ab"[..], &b"cd"[..])); + + let b = &b"cbde"[..]; + assert_eq!(a_or_b(b), Done(&b"bde"[..], &b"c"[..])); + + let c = &b"abab"[..]; + assert_eq!(a_or_b(c), Error(Position(ErrorKind::IsNot,c))); + + let d = &b"cdefba"[..]; + assert_eq!(a_or_b(d), Done(&b"ba"[..], &b"cdef"[..])); + + let e = &b"e"[..]; + assert_eq!(a_or_b(e), Done(&b""[..], &b"e"[..])); + + let f = &b"fghi"[..]; + assert_eq!(a_or_b(f), Done(&b""[..], &b"fghi"[..])); + } + + #[test] + fn escaping() { + named!(esc, escaped!(call!(alpha), '\\', is_a_bytes!(&b"\"n\\"[..]))); + assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..])); + assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..])); + assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], &b"\\\"abcd"[..])); + assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], &b"\\n"[..])); + assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], &b"ab\\\""[..])); + assert_eq!(esc(&b"AB\\"[..]), Error(NodePosition(ErrorKind::Escaped, &b"AB\\"[..], Box::new(Position(ErrorKind::Escaped, &b"\\"[..]))))); + assert_eq!(esc(&b"AB\\A"[..]), Error(NodePosition(ErrorKind::Escaped, &b"AB\\A"[..], Box::new(Position(ErrorKind::IsA, &b"A"[..]))))); + } + + fn to_s(i:Vec) -> String { + String::from_utf8_lossy(&i).into_owned() + } + + #[test] + fn escape_transform() { + use std::str; + + named!(esc< String >, map!(escaped_transform!(alpha, '\\', + alt!( + tag!("\\") => { |_| &b"\\"[..] } + | tag!("\"") => { |_| &b"\""[..] } + | tag!("n") => { |_| &b"\n"[..] } + )), to_s) + ); + + assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], String::from("abcd"))); + assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], String::from("ab\"cd"))); + assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], String::from("\"abcd"))); + assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], String::from("\n"))); + assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], String::from("ab\""))); + assert_eq!(esc(&b"AB\\"[..]), Error(NodePosition(ErrorKind::EscapedTransform, &b"AB\\"[..], Box::new(Position(ErrorKind::EscapedTransform, &b"\\"[..]))))); + assert_eq!(esc(&b"AB\\A"[..]), Error(NodePosition(ErrorKind::EscapedTransform, &b"AB\\A"[..], Box::new(Position(ErrorKind::Alt, &b"A"[..]))))); + + let e = "è"; + let a = "à"; + println!("è: {:?} | à: {:?}", str::as_bytes(e), str::as_bytes(a)); + named!(esc2< String >, map!(escaped_transform!(call!(alpha), '&', + alt!( + tag!("egrave;") => { |_| str::as_bytes("è") } + | tag!("agrave;") => { |_| str::as_bytes("à") } + )), to_s) + ); + assert_eq!(esc2(&b"abèDEF"[..]), Done(&b""[..], String::from("abèDEF"))); + assert_eq!(esc2(&b"abèDàEF"[..]), Done(&b""[..], String::from("abèDàEF"))); + } + + #[test] + fn issue_84() { + let r0 = is_a!(&b"aaaaefgh"[..], "abcd"); + assert_eq!(r0, Done(&b"efgh"[..], &b"aaaa"[..])); + let r1 = is_a!(&b"aaaa"[..], "abcd"); + assert_eq!(r1, Done(&b""[..], &b"aaaa"[..])); + let r2 = is_a!(&b"1"[..], "123456789"); + assert_eq!(r2, Done(&b""[..], &b"1"[..])); + } + + #[test] + fn take_str_test() { + let a = b"omnomnom"; + + assert_eq!(take_str!(&a[..], 5), Done(&b"nom"[..], "omnom")); + assert_eq!(take_str!(&a[..], 9), Incomplete(Needed::Size(9))); + } + + #[test] + fn take_until_test() { + named!(x, take_until_and_consume!("efgh")); + let r = x(&b"abcdabcdefghijkl"[..]); + assert_eq!(r, Done(&b"ijkl"[..], &b"abcdabcd"[..])); + + println!("Done 1\n"); + + let r2 = x(&b"abcdabcdefgh"[..]); + assert_eq!(r2, Done(&b""[..], &b"abcdabcd"[..])); + + println!("Done 2\n"); + let r3 = x(&b"abcefg"[..]); + assert_eq!(r3, Error(Position(ErrorKind::TakeUntilAndConsume, &b"abcefg"[..]))); + + assert_eq!( + x(&b"ab"[..]), + Incomplete(Needed::Size(4)) + ); + } + + #[test] + fn take_until_either_incomplete() { + named!(x, take_until_either!("!.")); + assert_eq!( + x(&b"123"[..]), + Error(Position(ErrorKind::TakeUntilEither, &b"123"[..])) + ); + } + + #[test] + fn take_until_incomplete() { + named!(y, take_until!("end")); + assert_eq!( + y(&b"nd"[..]), + Incomplete(Needed::Size(3)) + ); + assert_eq!( + y(&b"123"[..]), + Error(Position(ErrorKind::TakeUntil, &b"123"[..])) + ); + } + + #[test] + fn recognize() { + named!(x, recognize!(delimited!(tag!("")))); + let r = x(&b" aaa"[..]); + assert_eq!(r, Done(&b" aaa"[..], &b""[..])); + + let empty = &b""[..]; + + named!(ya, recognize!(alpha)); + let ra = ya(&b"abc"[..]); + assert_eq!(ra, Done(empty, &b"abc"[..])); + + named!(yd, recognize!(digit)); + let rd = yd(&b"123"[..]); + assert_eq!(rd, Done(empty, &b"123"[..])); + + named!(yhd, recognize!(hex_digit)); + let rhd = yhd(&b"123abcDEF"[..]); + assert_eq!(rhd, Done(empty, &b"123abcDEF"[..])); + + named!(yod, recognize!(oct_digit)); + let rod = yod(&b"1234567"[..]); + assert_eq!(rod, Done(empty, &b"1234567"[..])); + + named!(yan, recognize!(alphanumeric)); + let ran = yan(&b"123abc"[..]); + assert_eq!(ran, Done(empty, &b"123abc"[..])); + + named!(ys, recognize!(space)); + let rs = ys(&b" \t"[..]); + assert_eq!(rs, Done(empty, &b" \t"[..])); + + named!(yms, recognize!(multispace)); + let rms = yms(&b" \t\r\n"[..]); + assert_eq!(rms, Done(empty, &b" \t\r\n"[..])); + } + + #[test] + fn take_while() { + use nom::is_alphabetic; + named!(f, take_while!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"abcd123"; + let d = b"123"; + + assert_eq!(f(&a[..]), Done(&a[..], &a[..])); + assert_eq!(f(&b[..]), Done(&a[..], &b[..])); + assert_eq!(f(&c[..]), Done(&d[..], &b[..])); + assert_eq!(f(&d[..]), Done(&d[..], &a[..])); + } + + #[test] + fn take_while1() { + use nom::is_alphabetic; + named!(f, take_while1!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"abcd123"; + let d = b"123"; + + assert_eq!(f(&a[..]), Error(Position(ErrorKind::TakeWhile1, &b""[..]))); + assert_eq!(f(&b[..]), Done(&a[..], &b[..])); + assert_eq!(f(&c[..]), Done(&b"123"[..], &b[..])); + assert_eq!(f(&d[..]), Error(Position(ErrorKind::TakeWhile1, &d[..]))); + } + + #[cfg(feature = "nightly")] + use test::Bencher; + + #[cfg(feature = "nightly")] + #[bench] + fn take_while_bench(b: &mut Bencher) { + use nom::is_alphabetic; + named!(f, take_while!(is_alphabetic)); + b.iter(|| { + f(&b"abcdefghijklABCDEejfrfrjgro12aa"[..]) + }); + } + + #[test] + fn recognize_take_while() { + use nom::is_alphanumeric; + named!(x, take_while!(is_alphanumeric)); + named!(y, recognize!(x)); + assert_eq!(x(&b"ab"[..]), Done(&[][..], &b"ab"[..])); + println!("X: {:?}", x(&b"ab"[..])); + assert_eq!(y(&b"ab"[..]), Done(&[][..], &b"ab"[..])); + } +} diff --git third_party/rust/nom-1.2.4/src/character.rs third_party/rust/nom-1.2.4/src/character.rs new file mode 100644 index 000000000000..781065b35270 --- /dev/null +++ third_party/rust/nom-1.2.4/src/character.rs @@ -0,0 +1,184 @@ +/// Character level parsers + +use internal::{IResult,Needed,Err}; +use util::ErrorKind; + +/// matches one of the provided characters +#[macro_export] +macro_rules! one_of ( + ($i:expr, $inp: expr) => ( + { + if $i.is_empty() { + $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) + } else { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + one_of_bytes!($i, bytes) + } + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! one_of_bytes ( + ($i:expr, $bytes: expr) => ( + { + if $i.is_empty() { + $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) + } else { + let mut found = false; + + for &i in $bytes { + if i == $i[0] { + found = true; + break; + } + } + + if found { + $crate::IResult::Done(&$i[1..], $i[0] as char) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::OneOf, $i)) + } + } + } + ); +); + +/// matches anything but the provided characters +#[macro_export] +macro_rules! none_of ( + ($i:expr, $inp: expr) => ( + { + if $i.is_empty() { + $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) + } else { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + none_of_bytes!($i, bytes) + } + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! none_of_bytes ( + ($i:expr, $bytes: expr) => ( + { + if $i.is_empty() { + $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) + } else { + let mut found = false; + + for &i in $bytes { + if i == $i[0] { + found = true; + break; + } + } + + if !found { + $crate::IResult::Done(&$i[1..], $i[0] as char) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::NoneOf, $i)) + } + } + } + ); +); + +/// matches one character: `char!(char) => &[u8] -> IResult<&[u8], char> +#[macro_export] +macro_rules! char ( + ($i:expr, $c: expr) => ( + { + if $i.is_empty() { + let res: $crate::IResult<&[u8], char> = $crate::IResult::Incomplete($crate::Needed::Size(1)); + res + } else { + if $i[0] == $c as u8 { + $crate::IResult::Done(&$i[1..], $i[0] as char) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Char, $i)) + } + } + } + ); +); + +named!(pub newline, char!('\n')); + +pub fn crlf(input:&[u8]) -> IResult<&[u8], char> { + if input.len() < 2 { + IResult::Incomplete(Needed::Size(2)) + } else { + if &input[0..2] == &b"\r\n"[..] { + IResult::Done(&input[2..], '\n') + } else { + IResult::Error(Err::Position(ErrorKind::CrLf, input)) + } + } +} + +named!(pub eol, alt!(crlf | newline)); +named!(pub tab, char!('\t')); + +pub fn anychar(input:&[u8]) -> IResult<&[u8], char> { + if input.is_empty() { + IResult::Incomplete(Needed::Size(1)) + } else { + IResult::Done(&input[1..], input[0] as char) + } +} + +#[cfg(test)] +mod tests { + use internal::IResult::*; + use internal::Err::*; + use util::ErrorKind; + + #[test] + fn one_of() { + named!(f, one_of!("ab")); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Done(&b"bcd"[..], 'a')); + + let b = &b"cde"[..]; + assert_eq!(f(b), Error(Position(ErrorKind::OneOf, b))); + } + + #[test] + fn none_of() { + named!(f, none_of!("ab")); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Error(Position(ErrorKind::NoneOf, a))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Done(&b"de"[..], 'c')); + } + + #[test] + fn char() { + named!(f, char!('c')); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Error(Position(ErrorKind::Char, a))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Done(&b"de"[..], 'c')); + } +} diff --git third_party/rust/nom-1.2.4/src/internal.rs third_party/rust/nom-1.2.4/src/internal.rs new file mode 100644 index 000000000000..deb39a86654d --- /dev/null +++ third_party/rust/nom-1.2.4/src/internal.rs @@ -0,0 +1,347 @@ +//! Basic types to build the parsers + +use self::IResult::*; +use self::Needed::*; +use util::ErrorKind; + +#[cfg(feature = "core")] +use std::prelude::v1::*; +use std::boxed::Box; + +/// Contains the error that a parser can return +/// +/// It can represent a linked list of errors, indicating the path taken in the parsing tree, with corresponding position in the input data. +/// It depends on P, the input position (for a &[u8] parser, it would be a &[u8]), and E, the custom error type (by default, u32) +#[derive(Debug,PartialEq,Eq,Clone)] +pub enum Err{ + /// An error code, represented by an ErrorKind, which can contain a custom error code represented by E + Code(ErrorKind), + /// An error code, and the next error + Node(ErrorKind, Box>), + /// An error code, and the input position + Position(ErrorKind, P), + /// An error code, the input position and the next error + NodePosition(ErrorKind, P, Box>) +} + +/// Contains information on needed data if a parser returned `Incomplete` +#[derive(Debug,PartialEq,Eq,Clone,Copy)] +pub enum Needed { + /// needs more data, but we do not know how much + Unknown, + /// contains the required data size + Size(usize) +} + +impl Needed { + pub fn is_known(&self) -> bool { + *self != Unknown + } + + /// Maps a `Needed` to `Needed` by appling a function to a contained `Size` value. + #[inline] + pub fn map usize>(self, f: F) -> Needed { + match self { + Unknown => Unknown, + Size(n) => Size(f(n)), + } + } +} + +/// Holds the result of parsing functions +/// +/// It depends on I, the input type, O, the output type, and E, the error type (by default u32) +/// +#[derive(Debug,PartialEq,Eq,Clone)] +pub enum IResult { + /// indicates a correct parsing, the first field containing the rest of the unparsed data, the second field contains the parsed data + Done(I,O), + /// contains a Err, an enum that can indicate an error code, a position in the input, and a pointer to another error, making a list of errors in the parsing tree + Error(Err), + /// Incomplete contains a Needed, an enum than can represent a known quantity of input data, or unknown + Incomplete(Needed) +} + +impl IResult { + pub fn is_done(&self) -> bool { + match *self { + Done(_,_) => true, + _ => false + } + } + + pub fn is_err(&self) -> bool { + match *self { + Error(_) => true, + _ => false + } + } + + pub fn is_incomplete(&self) -> bool { + match *self { + Incomplete(_) => true, + _ => false + } + } + + /// Maps a `IResult` to `IResult` by appling a function + /// to a contained `Done` value, leaving `Error` and `Incomplete` value + /// untouched. + #[inline] + pub fn map N>(self, f: F) -> IResult { + match self { + Done(i, o) => Done(i, f(o)), + Error(e) => Error(e), + Incomplete(n) => Incomplete(n), + } + } + + /// Maps a `IResult` to `IResult` by appling a function + /// to a contained `Incomplete` value, leaving `Done` and `Error` value + /// untouched. + #[inline] + pub fn map_inc(self, f: F) -> IResult + where F: FnOnce(Needed) -> Needed { + match self { + Error(e) => Error(e), + Incomplete(n) => Incomplete(f(n)), + Done(i, o) => Done(i, o), + } + } + + /// Maps a `IResult` to `IResult` by appling a function + /// to a contained `Error` value, leaving `Done` and `Incomplete` value + /// untouched. + #[inline] + pub fn map_err(self, f: F) -> IResult + where F: FnOnce(Err) -> Err { + match self { + Error(e) => Error(f(e)), + Incomplete(n) => Incomplete(n), + Done(i, o) => Done(i, o), + } + } + + /// Unwrap the contained `Done(I, O)` value, or panic if the `IResult` is not + /// `Done`. + pub fn unwrap(self) -> (I, O) { + match self { + Done(i, o) => (i, o), + Incomplete(_) => panic!("unwrap() called on an IResult that is Incomplete"), + Error(_) => panic!("unwrap() called on an IResult that is Error") + } + } + + /// Unwrap the contained `Done(I, O)` value, or panic if the `IResult` is not + /// `Done`. + pub fn unwrap_inc(self) -> Needed { + match self { + Incomplete(n) => n, + Done(_, _) => panic!("unwrap_inc() called on an IResult that is Done"), + Error(_) => panic!("unwrap_inc() called on an IResult that is Error") + } + } + + /// Unwrap the contained `Done(I, O)` value, or panic if the `IResult` is not + /// `Done`. + pub fn unwrap_err(self) -> Err { + match self { + Error(e) => e, + Done(_, _) => panic!("unwrap_err() called on an IResult that is Done"), + Incomplete(_) => panic!("unwrap_err() called on an IResult that is Incomplete"), + } + } +} + +pub trait GetInput { + fn remaining_input(&self) -> Option; +} + +pub trait GetOutput { + fn output(&self) -> Option; +} + +impl<'a,I,O,E> GetInput<&'a[I]> for IResult<&'a[I],O,E> { + fn remaining_input(&self) -> Option<&'a[I]> { + match *self { + Done(ref i,_) => Some(*i), + _ => None + } + } +} + +impl GetInput<()> for IResult<(),O,E> { + fn remaining_input(&self) -> Option<()> { + match *self { + Done((),_) => Some(()), + _ => None + } + } +} + +impl<'a,O,E> GetInput<&'a str> for IResult<&'a str,O,E> { + fn remaining_input(&self) -> Option<&'a str> { + match *self { + Done(ref i,_) => Some(*i), + _ => None + } + } +} + +impl<'a,I,O,E> GetOutput<&'a[O]> for IResult { + fn output(&self) -> Option<&'a[O]> { + match *self { + Done(_, ref o) => Some(*o), + _ => None + } + } +} + +impl GetOutput<()> for IResult { + fn output(&self) -> Option<()> { + match *self { + Done(_,()) => Some(()), + _ => None + } + } +} + +impl<'a,I,E> GetOutput<&'a str> for IResult { + fn output(&self) -> Option<&'a str> { + match *self { + Done(_,ref o) => Some(*o), + _ => None + } + } +} + +#[cfg(not(feature = "core"))] +use std::any::Any; +#[cfg(not(feature = "core"))] +use std::{error,fmt}; +#[cfg(not(feature = "core"))] +use std::fmt::Debug; +#[cfg(not(feature = "core"))] +impl error::Error for Err { + fn description(&self) -> &str { + let kind = match *self { + Err::Code(ref e) | Err::Node(ref e, _) | Err::Position(ref e, _) | Err::NodePosition(ref e, _, _) => e + }; + kind.description() + } +} + +#[cfg(not(feature = "core"))] +impl fmt::Display for Err { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Err::Code(ref e) | Err::Node(ref e, _) => { + write!(f, "{:?}", e) + }, + Err::Position(ref e, ref p) | Err::NodePosition(ref e, ref p, _) => { + write!(f, "{:?}:{:?}", p, e) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use util::ErrorKind; + + const REST: [u8; 0] = []; + const DONE: IResult<&'static [u8], u32> = IResult::Done(&REST, 5); + const ERROR: IResult<&'static [u8], u32> = IResult::Error(Err::Code(ErrorKind::Tag)); + const INCOMPLETE: IResult<&'static [u8], u32> = IResult::Incomplete(Needed::Unknown); + + #[test] + fn needed_map() { + let unknown = Needed::Unknown; + let size = Needed::Size(5); + + assert_eq!(size.map(|x| x * 2), Needed::Size(10)); + assert_eq!(unknown.map(|x| x * 2), Needed::Unknown); + } + + #[test] + fn iresult_map() { + assert_eq!(DONE.map(|x| x * 2), IResult::Done(&b""[..], 10)); + assert_eq!(ERROR.map(|x| x * 2), IResult::Error(Err::Code(ErrorKind::Tag))); + assert_eq!(INCOMPLETE.map(|x| x * 2), IResult::Incomplete(Needed::Unknown)); + } + + #[test] + fn iresult_map_inc() { + let inc_unknown: IResult<&[u8], u32> = IResult::Incomplete(Needed::Unknown); + let inc_size: IResult<&[u8], u32> = IResult::Incomplete(Needed::Size(5)); + + assert_eq!(DONE.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Done(&b""[..], 5)); + assert_eq!(ERROR.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Error(Err::Code(ErrorKind::Tag))); + assert_eq!(inc_unknown.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Incomplete(Needed::Unknown)); + assert_eq!(inc_size.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Incomplete(Needed::Size(6))); + } + + #[test] + fn iresult_map_err() { + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + struct Error(u32); + + let error_kind = Err::Code(ErrorKind::Custom(Error(5))); + + assert_eq!(DONE.map_err(|_| error_kind.clone()), IResult::Done(&b""[..], 5)); + assert_eq!(ERROR.map_err(|x| {println!("err: {:?}", x); error_kind.clone()}), IResult::Error(error_kind.clone())); + assert_eq!(INCOMPLETE.map_err(|x| {println!("err: {:?}", x); error_kind.clone()}), IResult::Incomplete(Needed::Unknown)); + } + + #[test] + fn iresult_unwrap_on_done() { + assert_eq!(DONE.unwrap(), (&b""[..], 5)); + } + + #[test] + #[should_panic] + fn iresult_unwrap_on_err() { + ERROR.unwrap(); + } + + #[test] + #[should_panic] + fn iresult_unwrap_on_inc() { + INCOMPLETE.unwrap(); + } + + #[test] + #[should_panic] + fn iresult_unwrap_err_on_done() { + DONE.unwrap_err(); + } + + #[test] + fn iresult_unwrap_err_on_err() { + assert_eq!(ERROR.unwrap_err(), Err::Code(ErrorKind::Tag)); + } + + #[test] + #[should_panic] + fn iresult_unwrap_err_on_inc() { + INCOMPLETE.unwrap_err(); + } + + #[test] + #[should_panic] + fn iresult_unwrap_inc_on_done() { + DONE.unwrap_inc(); + } + + #[test] + #[should_panic] + fn iresult_unwrap_inc_on_err() { + ERROR.unwrap_inc(); + } + + #[test] + fn iresult_unwrap_inc_on_inc() { + assert_eq!(INCOMPLETE.unwrap_inc(), Needed::Unknown); + } +} diff --git third_party/rust/nom-1.2.4/src/lib.rs third_party/rust/nom-1.2.4/src/lib.rs new file mode 100644 index 000000000000..11b475d59517 --- /dev/null +++ third_party/rust/nom-1.2.4/src/lib.rs @@ -0,0 +1,151 @@ +//! nom, eating data byte by byte +//! +//! nom is a parser combinator library with a focus on safe parsing, +//! streaming patterns, and as much as possible zero copy. +//! +//! The code is available on [Github](https://github.com/Geal/nom) +//! +//! # Example +//! +//! ``` +//! #[macro_use] +//! extern crate nom; +//! +//! use nom::{IResult,digit}; +//! use nom::IResult::*; +//! +//! // Parser definition +//! +//! use std::str; +//! use std::str::FromStr; +//! +//! named!(parens, delimited!( +//! char!('('), +//! expr, +//! char!(')') +//! ) +//! ); +//! +//! named!(i64_digit, +//! map_res!( +//! map_res!( +//! digit, +//! str::from_utf8 +//! ), +//! FromStr::from_str +//! ) +//! ); +//! +//! // We transform an integer string into a i64 +//! // we look for a digit suite, and try to convert it. +//! // if either str::from_utf8 or FromStr::from_str fail, +//! // the parser will fail +//! named!(factor, +//! alt!( +//! i64_digit +//! | parens +//! ) +//! ); +//! +//! // we define acc as mutable to update its value whenever a new term is found +//! named!(term , +//! chain!( +//! mut acc: factor ~ +//! many0!( +//! alt!( +//! tap!(mul: preceded!(tag!("*"), factor) => acc = acc * mul) | +//! tap!(div: preceded!(tag!("/"), factor) => acc = acc / div) +//! ) +//! ), +//! || { return acc } +//! ) +//! ); +//! +//! named!(expr , +//! chain!( +//! mut acc: term ~ +//! many0!( +//! alt!( +//! tap!(add: preceded!(tag!("+"), term) => acc = acc + add) | +//! tap!(sub: preceded!(tag!("-"), term) => acc = acc - sub) +//! ) +//! ), +//! || { return acc } +//! ) +//! ); +//! +//! fn main() { +//! assert_eq!(expr(b"1+2"), IResult::Done(&b""[..], 3)); +//! assert_eq!(expr(b"12+6-4+3"), IResult::Done(&b""[..], 17)); +//! assert_eq!(expr(b"1+2*3+4"), IResult::Done(&b""[..], 11)); +//! +//! assert_eq!(expr(b"(2)"), IResult::Done(&b""[..], 2)); +//! assert_eq!(expr(b"2*(3+4)"), IResult::Done(&b""[..], 14)); +//! assert_eq!(expr(b"2*2/(5-1)+3"), IResult::Done(&b""[..], 4)); +//! } +//! ``` +#![cfg_attr(feature = "core", feature(no_std))] +#![cfg_attr(feature = "core", feature(collections))] +#![cfg_attr(feature = "core", no_std)] +#![cfg_attr(feature = "nightly", feature(test))] +#![cfg_attr(feature = "nightly", feature(const_fn))] + +#[cfg(feature = "core")] +extern crate collections; +#[cfg(feature = "regexp")] +extern crate regex; +#[cfg(feature = "regexp_macros")] +#[macro_use] extern crate lazy_static; +#[cfg(feature = "nightly")] +extern crate test; + +#[cfg(feature = "core")] +mod std { +#[macro_use] + pub use core::{fmt, iter, option, ops, slice, mem}; + pub use collections::{boxed, vec, string}; + pub mod prelude { + pub use core::prelude as v1; + } +} + +pub use self::util::*; +pub use self::internal::*; +pub use self::macros::*; +pub use self::methods::*; +pub use self::bytes::*; +pub use self::bits::*; + +pub use self::nom::*; +pub use self::character::*; + +#[cfg(feature = "regexp")] +pub use self::regexp::*; + +#[cfg(not(feature = "core"))] +#[cfg(feature = "stream")] +pub use self::stream::*; + +#[cfg(not(feature = "core"))] +pub use self::str::*; + +#[macro_use] mod util; +mod internal; +#[macro_use] mod macros; +#[macro_use] mod methods; +#[macro_use] mod bytes; +#[macro_use] mod bits; + +#[macro_use] mod nom; +#[macro_use] mod character; + +#[cfg(feature = "regexp")] +#[macro_use] mod regexp; + +#[macro_use] +#[cfg(not(feature = "core"))] +#[cfg(feature = "stream")] +mod stream; + +#[cfg(not(feature = "core"))] +mod str; diff --git third_party/rust/nom-1.2.4/src/macros.rs third_party/rust/nom-1.2.4/src/macros.rs new file mode 100644 index 000000000000..3e346a94f522 --- /dev/null +++ third_party/rust/nom-1.2.4/src/macros.rs @@ -0,0 +1,3561 @@ +//! Macro combinators +//! +//! Macros are used to make combination easier, +//! since they often do not depend on the type +//! of the data they manipulate or return. +//! +//! There is a trick to make them easier to assemble, +//! combinators are defined like this: +//! +//! ```ignore +//! macro_rules! tag ( +//! ($i:expr, $inp: expr) => ( +//! { +//! ... +//! } +//! ); +//! ); +//! ``` +//! +//! But when used in other combinators, are Used +//! like this: +//! +//! ```ignore +//! named!(my_function, tag!("abcd")); +//! ``` +//! +//! Internally, other combinators will rewrite +//! that call to pass the input as first argument: +//! +//! ```ignore +//! macro_rules! named ( +//! ($name:ident, $submac:ident!( $($args:tt)* )) => ( +//! fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<'a,&[u8], &[u8]> { +//! $submac!(i, $($args)*) +//! } +//! ); +//! ); +//! ``` +//! +//! If you want to call a combinator directly, you can +//! do it like this: +//! +//! ```ignore +//! let res = { tag!(input, "abcd"); } +//! ``` +//! +//! Combinators must have a specific variant for +//! non-macro arguments. Example: passing a function +//! to take_while! instead of another combinator. +//! +//! ```ignore +//! macro_rules! take_while( +//! ($input:expr, $submac:ident!( $($args:tt)* )) => ( +//! { +//! ... +//! } +//! ); +//! +//! // wrap the function in a macro to pass it to the main implementation +//! ($input:expr, $f:expr) => ( +//! take_while!($input, call!($f)); +//! ); +//! ); +//! + +/// Wraps a parser in a closure +#[macro_export] +macro_rules! closure ( + ($ty:ty, $submac:ident!( $($args:tt)* )) => ( + |i: $ty| { $submac!(i, $($args)*) } + ); + ($submac:ident!( $($args:tt)* )) => ( + |i| { $submac!(i, $($args)*) } + ); +); + +/// Makes a function from a parser combination +/// +/// The type can be set up if the compiler needs +/// more information +/// +/// ```ignore +/// named!(my_function( &[u8] ) -> &[u8], tag!("abcd")); +/// // first type parameter is input, second is output +/// named!(my_function<&[u8], &[u8]>, tag!("abcd")); +/// // will have &[u8] as input type, &[u8] as output type +/// named!(my_function, tag!("abcd")); +/// // will use &[u8] as input type (use this if the compiler +/// // complains about lifetime issues +/// named!(my_function<&[u8]>, tag!("abcd")); +/// //prefix them with 'pub' to make the functions public +/// named!(pub my_function, tag!("abcd")); +/// ``` +#[macro_export] +macro_rules! named ( + ($name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: $i ) -> $crate::IResult<$i,$o,u32> { + $submac!(i, $($args)*) + } + ); + ($name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> { + $submac!(i, $($args)*) + } + ); + ($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( i: &'a[u8] ) -> $crate::IResult<&'a [u8], $o, u32> { + $submac!(i, $($args)*) + } + ); + ($name:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: &[u8] ) -> $crate::IResult<&[u8], &[u8], u32> { + $submac!(i, $($args)*) + } + ); + (pub $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> { + $submac!(i, $($args)*) + } + ); + (pub $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + (pub $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> { + $submac!(i, $($args)*) + } + ); + (pub $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> { + $submac!(i, $($args)*) + } + ); + (pub $name:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> { + $submac!(i, $($args)*) + } + ); +); + +/// Used to wrap common expressions and function as macros +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// fn take_wrapper(input: &[u8], i: u8) -> IResult<&[u8],&[u8]> { take!(input, i * 10) } +/// +/// // will make a parser taking 20 bytes +/// named!(parser, apply!(take_wrapper, 2)); +/// # } +/// ``` +#[macro_export] +macro_rules! call ( + ($i:expr, $fun:expr) => ( $fun( $i ) ); + ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); +); + +/// emulate function currying: `apply!(my_function, arg1, arg2, ...)` becomes `my_function(input, arg1, arg2, ...)` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// fn take_wrapper(input: &[u8], i: u8) -> IResult<&[u8],&[u8]> { take!(input, i * 10) } +/// +/// // will make a parser taking 20 bytes +/// named!(parser, apply!(take_wrapper, 2)); +/// # } +/// ``` +#[macro_export] +macro_rules! apply ( + ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); +); + +/// Prevents backtracking if the child parser fails +/// +/// This parser will do an early return instead of sending +/// its result to the parent parser. +/// +/// If another `error!` combinator is present in the parent +/// chain, the error will be wrapped and another early +/// return will be made. +/// +/// This makes it easy to build report on which parser failed, +/// where it failed in the input, and the chain of parsers +/// that led it there. +/// +/// Additionally, the error chain contains number identifiers +/// that can be matched to provide useful error messages. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::IResult::Error; +/// # use nom::Err::{Position,NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(err_test, alt!( +/// tag!("abcd") | +/// preceded!(tag!("efgh"), error!(ErrorKind::Custom(42), +/// chain!( +/// tag!("ijkl") ~ +/// res: error!(ErrorKind::Custom(128), tag!("mnop")) , +/// || { res } +/// ) +/// ) +/// ) +/// )); +/// let a = &b"efghblah"[..]; +/// let b = &b"efghijklblah"[..]; +/// let c = &b"efghijklmnop"[..]; +/// +/// let blah = &b"blah"[..]; +/// +/// let res_a = err_test(a); +/// let res_b = err_test(b); +/// let res_c = err_test(c); +/// assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah))))); +/// assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..], +/// Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah)))))) +/// ); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! error ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + let cl = || { + $submac!($i, $($args)*) + }; + + match cl() { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => { + return $crate::IResult::Error($crate::Err::NodePosition($code, $i, Box::new(e))) + } + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + error!($i, $code, call!($f)); + ); +); + +/// Add an error if the child parser fails +/// +/// While error! does an early return and avoids backtracking, +/// add_error! backtracks normally. It just provides more context +/// for an error +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::IResult::Error; +/// # use nom::Err::{Position,NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(err_test, add_error!(ErrorKind::Custom(42), tag!("abcd"))); +/// +/// let a = &b"efghblah"[..]; +/// let res_a = err_test(a); +/// assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), a, Box::new(Position(ErrorKind::Tag, a))))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! add_error ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => { + $crate::IResult::Error($crate::Err::NodePosition($code, $i, Box::new(e))) + } + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + add_error!($i, $code, call!($f)); + ); +); + + +/// translate parser result from IResult to IResult with a custom type +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::IResult::Error; +/// # use nom::Err::{Position,NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// // will add a Custom(42) error to the error chain +/// named!(err_test, add_error!(ErrorKind::Custom(42), tag!("abcd"))); +/// // Convert to IREsult<&[u8], &[u8], &str> +/// named!(parser<&[u8], &[u8], &str>, add_error!(ErrorKind::Custom("custom error message"), fix_error!(&str, err_test))); +/// +/// let a = &b"efghblah"[..]; +/// let res_a = parser(a); +/// assert_eq!(res_a, Error(NodePosition( ErrorKind::Custom("custom error message"), a, Box::new(Position(ErrorKind::Fix, a))))); +/// # } +/// ``` +#[macro_export] +macro_rules! fix_error ( + ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => { + let err = match e { + $crate::Err::Code($crate::ErrorKind::Custom(_)) | + $crate::Err::Node($crate::ErrorKind::Custom(_), _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Code(e) + }, + $crate::Err::Position($crate::ErrorKind::Custom(_), p) | + $crate::Err::NodePosition($crate::ErrorKind::Custom(_), p, _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Position(e, p) + }, + $crate::Err::Code(_) | + $crate::Err::Node(_, _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Code(e) + }, + $crate::Err::Position(_, p) | + $crate::Err::NodePosition(_, p, _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Position(e, p) + }, + }; + $crate::IResult::Error(err) + } + } + } + ); + ($i:expr, $t:ty, $f:expr) => ( + fix_error!($i, $t, call!($f)); + ); +); + +/// replaces a `Incomplete` returned by the child parser +/// with an `Error` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::IResult::Error; +/// # use nom::Err::{Position,NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(take_5, complete!(take!(5))); +/// +/// let a = &b"abcd"[..]; +/// let res_a = take_5(a); +/// assert_eq!(res_a, Error(Position(ErrorKind::Complete, a))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! complete ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(_) => { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Complete, $i)) + }, + } + } + ); + ($i:expr, $f:expr) => ( + complete!($i, call!($f)); + ); +); + +/// A bit like `std::try!`, this macro will return the remaining input and parsed value if the child parser returned `Done`, +/// and will do an early return for `Error` and `Incomplete` +/// this can provide more flexibility than `chain!` if needed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Error}; +/// # use nom::Err::Position; +/// # use nom::{be_u8,ErrorKind}; +/// +/// fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> { +/// let (i1, sz) = try_parse!(input, be_u8); +/// let (i2, length) = try_parse!(i1, expr_opt!(size.checked_add(sz))); +/// let (i3, data) = try_parse!(i2, take!(length)); +/// return Done(i3, data); +/// } +/// # fn main() { +/// let arr1 = [1, 2, 3, 4, 5]; +/// let r1 = take_add(&arr1[..], 1); +/// assert_eq!(r1, Done(&[4,5][..], &[2,3][..])); +/// +/// let arr2 = [0xFE, 2, 3, 4, 5]; +/// // size is overflowing +/// let r1 = take_add(&arr2[..], 42); +/// assert_eq!(r1, Error(Position(ErrorKind::ExprOpt,&[2,3,4,5][..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! try_parse ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => (i,o), + $crate::IResult::Error(e) => return $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i) + } + ); + ($i:expr, $f:expr) => ( + try_parse!($i, call!($f)) + ); +); + +/// `flat_map!(R -> IResult, S -> IResult) => R -> IResult` +/// +/// combines a parser R -> IResult and +/// a parser S -> IResult to return another +/// parser R -> IResult +#[macro_export] +macro_rules! flat_map( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), + $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { + $crate::IResult::Error(e) => { + let err = match e { + $crate::Err::Code(k) | $crate::Err::Node(k, _) | $crate::Err::Position(k, _) | $crate::Err::NodePosition(k, _, _) => { + $crate::Err::Position(k, $i) + } + }; + $crate::IResult::Error(err) + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(ref i2)) => $crate::IResult::Incomplete($crate::Needed::Size(*i2)), + $crate::IResult::Done(_, o2) => $crate::IResult::Done(i, o2) + } + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + flat_map!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $g:expr) => ( + flat_map!($i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + flat_map!($i, call!($f), $submac!($($args)*)); + ); +); + +/// `map!(I -> IResult, O -> P) => I -> IResult` +/// maps a function on the result of a parser +#[macro_export] +macro_rules! map( + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_impl!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_impl!($i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_impl!($i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_impl!($i, call!($f), $submac!($($args)*)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! map_impl( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, $submac2!(o, $($args2)*)) + } + } + ); +); + +/// `map_res!(I -> IResult, O -> Result

) => I -> IResult` +/// maps a function returning a Result on the output of a parser +#[macro_export] +macro_rules! map_res ( + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_res_impl!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_res_impl!($i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_res_impl!($i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_res_impl!($i, call!($f), $submac!($($args)*)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! map_res_impl ( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), + $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { + Ok(output) => $crate::IResult::Done(i, output), + Err(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::MapRes, $i)) + } + } + } + ); +); + + +/// `map_opt!(I -> IResult, O -> Option

) => I -> IResult` +/// maps a function returning an Option on the output of a parser +#[macro_export] +macro_rules! map_opt ( + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_opt_impl!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_opt_impl!($i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_opt_impl!($i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_opt_impl!($i, call!($f), $submac!($($args)*)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! map_opt_impl ( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), + $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { + ::std::option::Option::Some(output) => $crate::IResult::Done(i, output), + ::std::option::Option::None => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::MapOpt, $i)) + } + } + } + ); +); + +/// `value!(T, R -> IResult ) => R -> IResult` +/// +/// or `value!(T) => R -> IResult` +/// +/// If the child parser was successful, return the value. +/// If no child parser is provided, always return the value +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(x, value!(42, delimited!(tag!("")))); +/// named!(y, delimited!(tag!(""))); +/// let r = x(&b" aaa"[..]); +/// assert_eq!(r, Done(&b" aaa"[..], 42)); +/// +/// let r2 = y(&b" aaa"[..]); +/// assert_eq!(r2, Done(&b" aaa"[..], 42)); +/// # } +/// ``` +#[macro_export] +macro_rules! value ( + ($i:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,_) => { + $crate::IResult::Done(i, $res) + }, + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } + ); + ($i:expr, $res:expr, $f:expr) => ( + value!($i, $res, call!($f)) + ); + ($i:expr, $res:expr) => ( + $crate::IResult::Done($i, $res) + ); +); + +/// `expr_res!(Result) => I -> IResult` +/// evaluate an expression that returns a Result and returns a IResult::Done(I,T) if Ok +/// +/// See expr_opt for an example +#[macro_export] +macro_rules! expr_res ( + ($i:expr, $e:expr) => ( + { + match $e { + Ok(output) => $crate::IResult::Done($i, output), + Err(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ExprRes, $i)) + } + } + ); +); + +/// `expr_opt!(Option) => I -> IResult` +/// evaluate an expression that returns a Option and returns a IResult::Done(I,T) if Some +/// +/// Useful when doing computations in a chain +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Error}; +/// # use nom::Err::Position; +/// # use nom::{be_u8,ErrorKind}; +/// +/// fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> { +/// chain!(input, +/// sz: be_u8 ~ +/// length: expr_opt!(size.checked_add(sz)) ~ // checking for integer overflow (returns an Option) +/// data: take!(length) , +/// ||{ data } +/// ) +/// } +/// # fn main() { +/// let arr1 = [1, 2, 3, 4, 5]; +/// let r1 = take_add(&arr1[..], 1); +/// assert_eq!(r1, Done(&[4,5][..], &[2,3][..])); +/// +/// let arr2 = [0xFE, 2, 3, 4, 5]; +/// // size is overflowing +/// let r1 = take_add(&arr2[..], 42); +/// assert_eq!(r1, Error(Position(ErrorKind::ExprOpt,&[2,3,4,5][..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! expr_opt ( + ($i:expr, $e:expr) => ( + { + match $e { + ::std::option::Option::Some(output) => $crate::IResult::Done($i, output), + ::std::option::Option::None => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ExprOpt, $i)) + } + } + ); +); + +/// `chain!(I->IResult ~ I->IResult ~ ... I->IResult , || { return O } ) => I -> IResult` +/// chains parsers and assemble the results through a closure +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser and take it into account if +/// there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// #[derive(PartialEq,Eq,Debug)] +/// struct B { +/// a: u8, +/// b: Option +/// } +/// +/// named!(y, tag!("efgh")); +/// +/// fn ret_int(i:&[u8]) -> IResult<&[u8], u8> { Done(i, 1) } +/// named!(ret_y<&[u8], u8>, map!(y, |_| 1)); // return 1 if the "efgh" tag is found +/// +/// named!(z<&[u8], B>, +/// chain!( +/// tag!("abcd") ~ // the '~' character is used as separator +/// aa: ret_int ~ // the result of that parser will be used in the closure +/// tag!("abcd")? ~ // this parser is optional +/// bb: ret_y? , // the result of that parser is an option +/// // the last parser in the chain is followed by a ',' +/// ||{B{a: aa, b: bb}} +/// ) +/// ); +/// +/// # fn main() { +/// // the first "abcd" tag is not present, we have an error +/// let r1 = z(&b"efgh"[..]); +/// assert_eq!(r1, Error(Position(ErrorKind::Tag,&b"efgh"[..]))); +/// +/// // everything is present, everything is parsed +/// let r2 = z(&b"abcdabcdefgh"[..]); +/// assert_eq!(r2, Done(&b""[..], B{a: 1, b: Some(1)})); +/// +/// // the second "abcd" tag is optional +/// let r3 = z(&b"abcdefgh"[..]); +/// assert_eq!(r3, Done(&b""[..], B{a: 1, b: Some(1)})); +/// +/// // the result of ret_y is optional, as seen in the B structure +/// let r4 = z(&b"abcdabcdwxyz"[..]); +/// assert_eq!(r4, Done(&b"wxyz"[..], B{a: 1, b: None})); +/// # } +/// ``` +#[macro_export] +macro_rules! chain ( + ($i:expr, $($rest:tt)*) => ( + { + chaining_parser!($i, 0usize, $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! chaining_parser ( + ($i:expr, $consumed:expr, $e:ident ~ $($rest:tt)*) => ( + chaining_parser!($i, $consumed, call!($e) ~ $($rest)*); + ); + ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ~ $($rest:tt)*) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,_) => { + chaining_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), $($rest)*) + } + } + } +); + + ($i:expr, $consumed:expr, $e:ident ? ~ $($rest:tt)*) => ( + chaining_parser!($i, $consumed, call!($e) ? ~ $($rest)*); + ); + + ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ? ~ $($rest:tt)*) => ( + { + let res = $submac!($i, $($args)*); + if let $crate::IResult::Incomplete(inc) = res { + match inc { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + } + } else { + let input = if let $crate::IResult::Done(i,_) = res { + i + } else { + $i + }; + chaining_parser!(input, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input)), $($rest)*) + } + } + ); + + ($i:expr, $consumed:expr, $field:ident : $e:ident ~ $($rest:tt)*) => ( + chaining_parser!($i, $consumed, $field: call!($e) ~ $($rest)*); + ); + + ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)*) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + let $field = o; + chaining_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), $($rest)*) + } + } + } + ); + + ($i:expr, $consumed:expr, mut $field:ident : $e:ident ~ $($rest:tt)*) => ( + chaining_parser!($i, $consumed, mut $field: call!($e) ~ $($rest)*); + ); + + ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)*) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + let mut $field = o; + chaining_parser!(i, $consumed + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i), $($rest)*) + } + } + } + ); + + ($i:expr, $consumed:expr, $field:ident : $e:ident ? ~ $($rest:tt)*) => ( + chaining_parser!($i, $consumed, $field : call!($e) ? ~ $($rest)*); + ); + + ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ? ~ $($rest:tt)*) => ( + { + let res = $submac!($i, $($args)*); + if let $crate::IResult::Incomplete(inc) = res { + match inc { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + } + } else { + let ($field,input) = if let $crate::IResult::Done(i,o) = res { + (::std::option::Option::Some(o),i) + } else { + (::std::option::Option::None,$i) + }; + chaining_parser!(input, $consumed + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input), $($rest)*) + } + } + ); + + ($i:expr, $consumed:expr, mut $field:ident : $e:ident ? ~ $($rest:tt)*) => ( + chaining_parser!($i, $consumed, mut $field : call!($e) ? ~ $($rest)*); + ); + + ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) ? ~ $($rest:tt)*) => ( + { + let res = $submac!($i, $($args)*); + if let $crate::IResult::Incomplete(inc) = res { + match inc { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + } + } else { + let (mut $field,input) = if let $crate::IResult::Done(i,o) = res { + (::std::option::Option::Some(o),i) + } else { + (::std::option::Option::None,$i) + }; + chaining_parser!(input, $consumed + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input), $($rest)*) + } + } + ); + + // ending the chain + ($i:expr, $consumed:expr, $e:ident, $assemble:expr) => ( + chaining_parser!($i, $consumed, call!($e), $assemble); + ); + + ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ), $assemble:expr) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,_) => { + $crate::IResult::Done(i, $assemble()) + } + } + ); + + ($i:expr, $consumed:expr, $e:ident ?, $assemble:expr) => ( + chaining_parser!($i, $consumed, call!($e) ?, $assemble); + ); + + ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ?, $assemble:expr) => ({ + let res = $submac!($i, $($args)*); + if let $crate::IResult::Incomplete(inc) = res { + match inc { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + } + } else { + let input = if let $crate::IResult::Done(i,_) = res { + i + } else { + $i + }; + $crate::IResult::Done(input, $assemble()) + } + }); + + ($i:expr, $consumed:expr, $field:ident : $e:ident, $assemble:expr) => ( + chaining_parser!($i, $consumed, $field: call!($e), $assemble); + ); + + ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ), $assemble:expr) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + let $field = o; + $crate::IResult::Done(i, $assemble()) + } + } + ); + + ($i:expr, $consumed:expr, mut $field:ident : $e:ident, $assemble:expr) => ( + chaining_parser!($i, $consumed, mut $field: call!($e), $assemble); + ); + + ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ), $assemble:expr) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + let mut $field = o; + $crate::IResult::Done(i, $assemble()) + } + } + ); + + ($i:expr, $consumed:expr, $field:ident : $e:ident ? , $assemble:expr) => ( + chaining_parser!($i, $consumed, $field : call!($e) ? , $assemble); + ); + + ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ? , $assemble:expr) => ({ + let res = $submac!($i, $($args)*); + if let $crate::IResult::Incomplete(inc) = res { + match inc { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + } + } else { + let ($field,input) = if let $crate::IResult::Done(i,o) = res { + (::std::option::Option::Some(o), i) + } else { + (::std::option::Option::None, $i) + }; + $crate::IResult::Done(input, $assemble()) + } + }); + + ($i:expr, $consumed:expr, mut $field:ident : $e:ident ? , $assemble:expr) => ( + chaining_parser!($i, $consumed, $field : call!($e) ? , $assemble); + ); + + ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) ? , $assemble:expr) => ({ + let res = $submac!($i, $($args)*); + if let $crate::IResult::Incomplete(inc) = res { + match inc { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + } + } else { + let (mut $field,input) = if let $crate::IResult::Done(i,o) = res { + (::std::option::Option::Some(o), i) + } else { + (::std::option::Option::None, $i) + }; + $crate::IResult::Done(input, $assemble()) + } + }); + + ($i:expr, $consumed:expr, $assemble:expr) => ( + $crate::IResult::Done($i, $assemble()) + ) +); + + +/// `tuple!(I->IResult, I->IResult, ... I->IResult) => I -> IResult` +/// chains parsers and assemble the sub results in a tuple. +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser and take it into account if +/// there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # use nom::be_u16; +/// // the return type depends of the children parsers +/// named!(parser<&[u8], (u16, &[u8], &[u8]) >, +/// tuple!( +/// be_u16 , +/// take!(3), +/// tag!("fg") +/// ) +/// ); +/// +/// # fn main() { +/// assert_eq!( +/// parser(&b"abcdefgh"[..]), +/// Done( +/// &b"h"[..], +/// (0x6162u16, &b"cde"[..], &b"fg"[..]) +/// ) +/// ); +/// # } +/// ``` +#[macro_export] +macro_rules! tuple ( + ($i:expr, $($rest:tt)*) => ( + { + tuple_parser!($i, 0usize, (), $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! tuple_parser ( + ($i:expr, $consumed:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => ( + tuple_parser!($i, $consumed, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, $consumed:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + tuple_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), (o), $($rest)*) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + tuple_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), ($($parsed)* , o), $($rest)*) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:tt),*), $e:ident) => ( + tuple_parser!($i, $consumed, ($($parsed),*), call!($e)); + ); + ($i:expr, $consumed:expr, (), $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + $crate::IResult::Done(i, (o)) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + $crate::IResult::Done(i, ($($parsed),* , o)) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:expr),*)) => ( + { + $crate::IResult::Done($i, ($($parsed),*)) + } + ); +); +/// `alt!(I -> IResult | I -> IResult | ... | I -> IResult ) => I -> IResult` +/// try a list of parsers, return the result of the first successful one +/// +/// If one of the parser returns Incomplete, alt will return Incomplete, to retry +/// once you get more input. Note that it is better for performance to know the +/// minimum size of data you need before you get into alt. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!( test, alt!( tag!( "abcd" ) | tag!( "efgh" ) ) ); +/// let r1 = test(b"abcdefgh"); +/// assert_eq!(r1, Done(&b"efgh"[..], &b"abcd"[..])); +/// let r2 = test(&b"efghijkl"[..]); +/// assert_eq!(r2, Done(&b"ijkl"[..], &b"efgh"[..])); +/// # } +/// ``` +/// +/// There is another syntax for alt allowing a block to manipulate the result: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// #[derive(Debug,PartialEq,Eq)] +/// enum Tagged { +/// Abcd, +/// Efgh, +/// Took(usize) +/// } +/// named!(test, alt!( +/// tag!("abcd") => { |_| Tagged::Abcd } +/// | tag!("efgh") => { |_| Tagged::Efgh } +/// | take!(5) => { |res: &[u8]| Tagged::Took(res.len()) } // the closure takes the result as argument if the parser is successful +/// )); +/// let r1 = test(b"abcdefgh"); +/// assert_eq!(r1, Done(&b"efgh"[..], Tagged::Abcd)); +/// let r2 = test(&b"efghijkl"[..]); +/// assert_eq!(r2, Done(&b"ijkl"[..], Tagged::Efgh)); +/// let r3 = test(&b"mnopqrst"[..]); +/// assert_eq!(r3, Done(&b"rst"[..], Tagged::Took(5))); +/// # } +/// ``` +/// +/// **BE CAREFUL** there is a case where the behaviour of `alt!` can be confusing: +/// +/// when the alternatives have different lengths, like this case: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ef" ) | tag!( "ghi" ) | tag!( "kl" ) ) ); +/// ``` +/// +/// With this parser, if you pass `"abcd"` as input, the first alternative parses it correctly, +/// but if you pass `"efg"`, the first alternative will return `Incomplete`, since it needs an input +/// of 4 bytes. This behaviour of `alt!` is expected: if you get a partial input that isn't matched +/// by the first alternative, but would match if the input was complete, you want `alt!` to indicate +/// that it cannot decide with limited information. +/// +/// There are two ways to fix this behaviour. The first one consists in ordering the alternatives +/// by size, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "ef" ) | tag!( "kl") | tag!( "ghi" ) | tag!( "abcd" ) ) ); +/// ``` +/// +/// With this solution, the largest alternative will be tested last. +/// +/// The other solution uses the `complete!` combinator, which transforms an `Incomplete` in an +/// `Error`. If one of the alternatives returns `Incomplete` but is wrapped by `complete!`, +/// `alt!` will try the next alternative. This is useful when you know that +/// you will not get partial input: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// complete!( tag!( "abcd" ) ) | +/// complete!( tag!( "ef" ) ) | +/// complete!( tag!( "ghi" ) ) | +/// complete!( tag!( "kl" ) ) +/// ) +/// ); +/// ``` +/// +/// If you want the `complete!` combinator to be applied to all rules then use the convenience +/// `alt_complete!` macro (see below). +/// +/// This behaviour of `alt!` can get especially confusing if multiple alternatives have different +/// sizes but a common prefix, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ab" ) | tag!( "ef" ) ) ); +/// ``` +/// +/// in that case, if you order by size, passing `"abcd"` as input will always be matched by the +/// smallest parser, so the solution using `complete!` is better suited. +/// +/// You can also nest multiple `alt!`, like this: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// preceded!( +/// tag!("ab"), +/// alt!( +/// tag!( "cd" ) | +/// eof +/// ) +/// ) +/// | tag!( "ef" ) +/// ) +/// ); +/// ``` +/// +/// `preceded!` will first parse `"ab"` then, if successful, try the alternatives "cd", +/// or empty input (End Of File). If none of them work, `preceded!` will fail and +/// "ef" will be tested. +/// +#[macro_export] +macro_rules! alt ( + ($i:expr, $($rest:tt)*) => ( + { + alt_parser!($i, $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! alt_parser ( + ($i:expr, $e:ident | $($rest:tt)*) => ( + alt_parser!($i, call!($e) | $($rest)*); + ); + + ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + let res = $subrule!($i, $($args)*); + match res { + $crate::IResult::Done(_,_) => res, + $crate::IResult::Incomplete(_) => res, + _ => alt_parser!($i, $($rest)*) + } + } + ); + + ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + match $subrule!( $i, $($args)* ) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(_) => { + alt_parser!($i, $($rest)*) + } + } + } + ); + + ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => ( + alt_parser!($i, call!($e) => { $gen } | $($rest)*); + ); + + ($i:expr, $e:ident => { $gen:expr }) => ( + alt_parser!($i, call!($e) => { $gen }); + ); + + ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + { + match $subrule!( $i, $($args)* ) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(_) => { + alt_parser!($i) + } + } + } + ); + + ($i:expr, $e:ident) => ( + alt_parser!($i, call!($e)); + ); + + ($i:expr, $subrule:ident!( $($args:tt)*)) => ( + { + match $subrule!( $i, $($args)* ) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,o), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(_) => { + alt_parser!($i) + } + } + } + ); + + ($i:expr) => ( + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Alt,$i)) + ); +); + +/// This is a combination of the `alt!` and `complete!` combinators. Rather +/// than returning `Incomplete` on partial input, `alt_complete!` will try the +/// next alternative in the chain. You should use this only if you know you +/// will not receive partial input for the rules you're trying to match (this +/// is almost always the case for parsing programming languages). +#[macro_export] +macro_rules! alt_complete ( + // Recursive rules (must include `complete!` around the head) + + ($i:expr, $e:ident | $($rest:tt)*) => ( + alt_complete!($i, complete!(call!($e)) | $($rest)*); + ); + + ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + let res = complete!($i, $subrule!($($args)*)); + match res { + $crate::IResult::Done(_,_) => res, + _ => alt_complete!($i, $($rest)*), + } + } + ); + + ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + match complete!($i, $subrule!($($args)*)) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + _ => alt_complete!($i, $($rest)*), + } + } + ); + + ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => ( + alt_complete!($i, complete!(call!($e)) => { $gen } | $($rest)*); + ); + + // Tail (non-recursive) rules + + ($i:expr, $e:ident => { $gen:expr }) => ( + alt_complete!($i, call!($e) => { $gen }); + ); + + ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + alt_parser!($i, $subrule!($($args)*) => { $gen }) + ); + + ($i:expr, $e:ident) => ( + alt_complete!($i, call!($e)); + ); + + ($i:expr, $subrule:ident!( $($args:tt)*)) => ( + alt_parser!($i, $subrule!($($args)*)) + ); +); + +/// `switch!(I -> IResult, P => I -> IResult | ... | P => I -> IResult ) => I -> IResult` +/// choose the next parser depending on the result of the first one, if successful, +/// and returns the result of the second parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # use nom::Err::{Position, NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// b"efgh" => tag!("123") +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"abcdef"; +/// let c = b"efgh123"; +/// let d = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Done(&b"123"[..], &b"XYZ"[..])); +/// assert_eq!(sw(&b[..]), Error(NodePosition(ErrorKind::Switch, &b"abcdef"[..], Box::new(Position(ErrorKind::Tag, &b"ef"[..]))))); +/// assert_eq!(sw(&c[..]), Done(&b""[..], &b"123"[..])); +/// assert_eq!(sw(&d[..]), Error(Position(ErrorKind::Switch, &b"blah"[..]))); +/// # } +/// ``` +/// +/// Due to limitations in Rust macros, it is not possible to have simple functions on the right hand +/// side of pattern, like this: +/// +/// ```ignore +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// b"efgh" => tag!("123") +/// ) +/// ); +/// ``` +/// +/// If you want to pass your own functions instead, you can use the `call!` combinator as follows: +/// +/// ```ignore +/// named!(xyz, tag!("XYZ")); +/// named!(num, tag!("123")); +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => call!(xyz) | +/// b"efgh" => call!(num) +/// ) +/// ); +/// ``` +/// +#[macro_export] +macro_rules! switch ( + ($i:expr, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch_impl!($i, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $e:ident, $($rest:tt)*) => ( + { + switch_impl!($i, call!($e), $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! switch_impl ( + ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error($crate::Err::NodePosition( + $crate::ErrorKind::Switch, $i, ::std::boxed::Box::new(e) + )), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + match o { + $($p => match $subrule!(i, $($args2)*) { + $crate::IResult::Error(e) => $crate::IResult::Error($crate::Err::NodePosition( + $crate::ErrorKind::Switch, $i, ::std::boxed::Box::new(e) + )), + a => a, + }),*, + _ => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Switch,$i)) + } + } + } + } + ); +); +/// `opt!(I -> IResult) => I -> IResult>` +/// make the underlying parser optional +/// +/// returns an Option of the returned type. This parser returns `Some(result)` if the child parser +/// succeeds,`None` if it fails, and `Incomplete` if it did not have enough data to decide +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); +/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], None)); +/// # } +/// ``` +#[macro_export] +macro_rules! opt( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), + $crate::IResult::Error(_) => $crate::IResult::Done($i, ::std::option::Option::None), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } + ); + ($i:expr, $f:expr) => ( + opt!($i, call!($f)); + ); +); + +/// `opt_res!(I -> IResult) => I -> IResult>` +/// make the underlying parser optional +/// +/// returns a Result, with Err containing the parsing error +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!( o<&[u8], Result<&[u8], nom::Err<&[u8]> > >, opt_res!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Ok(&b"abcd"[..]))); +/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], Err(Position(ErrorKind::Tag, &b[..])))); +/// # } +/// ``` +#[macro_export] +macro_rules! opt_res ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, Ok(o)), + $crate::IResult::Error(e) => $crate::IResult::Done($i, Err(e)), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } + ); + ($i:expr, $f:expr) => ( + opt_res!($i, call!($f)); + ); +); + +/// `cond_with_error!(bool, I -> IResult) => I -> IResult>` +/// Conditional combinator +/// +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an Option of the return type of the child +/// parser. +/// +/// This is especially useful if a parser depends +/// on the value return by a preceding parser in +/// a `chain!`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::IResult; +/// # fn main() { +/// let b = true; +/// let f: Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b, tag!("abcd") )) +/// ); +/// +/// let a = b"abcdef"; +/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); +/// +/// let b2 = false; +/// let f2:Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b2, tag!("abcd") )) +/// ); +/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None)); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! cond_with_error( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( + { + if $cond { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } else { + $crate::IResult::Done($i, ::std::option::Option::None) + } + } + ); + ($i:expr, $cond:expr, $f:expr) => ( + cond!($i, $cond, call!($f)); + ); +); + +/// `cond!(bool, I -> IResult) => I -> IResult>` +/// Conditional combinator +/// +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an Option of the return type of the child +/// parser. +/// +/// This is especially useful if a parser depends +/// on the value return by a preceding parser in +/// a `chain!`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::IResult; +/// # fn main() { +/// let b = true; +/// let f: Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b, tag!("abcd") )) +/// ); +/// +/// let a = b"abcdef"; +/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); +/// +/// let b2 = false; +/// let f2:Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b2, tag!("abcd") )) +/// ); +/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None)); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! cond( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( + { + if $cond { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), + $crate::IResult::Error(_) => $crate::IResult::Done($i, ::std::option::Option::None), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } else { + $crate::IResult::Done($i, ::std::option::Option::None) + } + } + ); + ($i:expr, $cond:expr, $f:expr) => ( + cond!($i, $cond, call!($f)); + ); +); + +/// `cond_reduce!(bool, I -> IResult) => I -> IResult` +/// Conditional combinator with error +/// +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an error if the condition is false +/// +/// This is especially useful if a parser depends +/// on the value return by a preceding parser in +/// a `chain!`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # use nom::{Err,ErrorKind}; +/// # fn main() { +/// let b = true; +/// let f = closure!(&'static[u8], +/// cond_reduce!( b, tag!("abcd") ) +/// ); +/// +/// let a = b"abcdef"; +/// assert_eq!(f(&a[..]), Done(&b"ef"[..], &b"abcd"[..])); +/// +/// let b2 = false; +/// let f2 = closure!(&'static[u8], +/// cond_reduce!( b2, tag!("abcd") ) +/// ); +/// assert_eq!(f2(&a[..]), Error(Err::Position(ErrorKind::CondReduce, &a[..]))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! cond_reduce( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( + { + if $cond { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::CondReduce, $i)) + } + } + ); + ($i:expr, $cond:expr, $f:expr) => ( + cond_reduce!($i, $cond, call!($f)); + ); +); + +/// `peek!(I -> IResult) => I -> IResult` +/// returns a result without consuming the input +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(ptag, peek!( tag!( "abcd" ) ) ); +/// +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Done(&b"abcdefgh"[..], &b"abcd"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! peek( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(_,o) => $crate::IResult::Done($i, o), + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } + ); + ($i:expr, $f:expr) => ( + peek!($i, call!($f)); + ); +); + +/// `not!(I -> IResult) => I -> IResult` +/// returns a result only if the embedded parser returns Error or Incomplete +/// does not consume the input +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # use nom::Err::{Position}; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(not_e, chain!( +/// res: tag!("abc") ~ +/// not!(char!('e')), +/// || { res })); +/// +/// let r = not_e(&b"abcd"[..]); +/// assert_eq!(r, Done(&b"d"[..], &b"abc"[..])); +/// +/// let r2 = not_e(&b"abce"[..]); +/// assert_eq!(r2, Error(Position(ErrorKind::Not, &b"e"[..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! not( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(_, _) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Not, $i)), + $crate::IResult::Error(_) => $crate::IResult::Done($i, &($i)[..0]), + $crate::IResult::Incomplete(_) => $crate::IResult::Done($i, &($i)[..0]) + } + } + ); +); + +/// `tap!(name: I -> IResult => { block }) => I -> IResult` +/// allows access to the parser's result without affecting it +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use std::str; +/// # fn main() { +/// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) ); +/// +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! tap ( + ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => { + let $name = o; + $e; + $crate::IResult::Done(i, $name) + }, + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) + } + } + ); + ($i:expr, $name: ident: $f:expr => $e:expr) => ( + tap!($i, $name: call!($f) => $e); + ); +); + +/// `pair!(I -> IResult, I -> IResult) => I -> IResult` +/// pair(X,Y), returns (x,y) +/// +#[macro_export] +macro_rules! pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + pair!($i, call!($f), call!($g)); + ); +); + +/// `separated_pair!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` +/// separated_pair(X,sep,Y) returns (x,y) +#[macro_export] +macro_rules! separated_pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => ( + { + match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1, (o1, _, o2)) => { + $crate::IResult::Done(i1, (o1, o2)) + } + } + } + ); + + ($i:expr, $f:expr, $($rest:tt)+) => ( + separated_pair!($i, call!($f), $($rest)*); + ); +); + +/// `preceded!(I -> IResult, I -> IResult) => I -> IResult` +/// preceded(opening, X) returns X +#[macro_export] +macro_rules! preceded( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (_,o)) => { + $crate::IResult::Done(remaining, o) + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + preceded!($i, call!($f), call!($g)); + ); +); + +/// `terminated!(I -> IResult, I -> IResult) => I -> IResult` +/// terminated(X, closing) returns X +#[macro_export] +macro_rules! terminated( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (o,_)) => { + $crate::IResult::Done(remaining, o) + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + terminated!($i, call!($f), call!($g)); + ); +); + +/// `delimited!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` +/// delimited(opening, X, closing) returns X +#[macro_export] +macro_rules! delimited( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => ( + { + match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1, (_, o, _)) => { + $crate::IResult::Done(i1, o) + } + } + } + ); + + ($i:expr, $f:expr, $($rest:tt)+) => ( + delimited!($i, call!($f), $($rest)*); + ); +); + +/// `separated_list!(I -> IResult, I -> IResult) => I -> IResult>` +/// separated_list(sep, X) returns Vec +#[macro_export] +macro_rules! separated_list( + ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( + { + let mut res = ::std::vec::Vec::new(); + let mut input = $i; + + // get the first element + match $submac!(input, $($args2)*) { + $crate::IResult::Error(_) => $crate::IResult::Done(input, ::std::vec::Vec::new()), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i,o) => { + if i.len() == input.len() { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::SeparatedList,input)) + } else { + res.push(o); + input = i; + + loop { + // get the separator first + if let $crate::IResult::Done(i2,_) = $sep!(input, $($args)*) { + if i2.len() == input.len() { + break; + } + + // get the element next + if let $crate::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) { + if i3.len() == i2.len() { + break; + } + res.push(o3); + input = i3; + } else { + break; + } + } else { + break; + } + } + $crate::IResult::Done(input, res) + } + }, + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list!($i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + separated_list!($i, call!($f), call!($g)); + ); +); + +/// `separated_nonempty_list!(I -> IResult, I -> IResult) => I -> IResult>` +/// separated_nonempty_list(sep, X) returns Vec +#[macro_export] +macro_rules! separated_nonempty_list( + ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( + { + let mut res = ::std::vec::Vec::new(); + let mut input = $i; + + // get the first element + match $submac!(input, $($args2)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i,o) => { + if i.len() == input.len() { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::SeparatedNonEmptyList,input)) + } else { + res.push(o); + input = i; + + loop { + if let $crate::IResult::Done(i2,_) = $sep!(input, $($args)*) { + if i2.len() == input.len() { + break; + } + + if let $crate::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) { + if i3.len() == i2.len() { + break; + } + res.push(o3); + input = i3; + } else { + break; + } + } else { + break; + } + } + $crate::IResult::Done(input, res) + } + }, + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_nonempty_list!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_nonempty_list!($i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + separated_nonempty_list!($i, call!($f), call!($g)); + ); +); + +/// `many0!(I -> IResult) => I -> IResult>` +/// Applies the parser 0 or more times and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); +/// # } +/// ``` +/// 0 or more +#[macro_export] +macro_rules! many0( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + + let ret; + let mut res = ::std::vec::Vec::new(); + let mut input = $i; + + loop { + if input.input_len() == 0 { + ret = $crate::IResult::Done(input, res); break; + } + + match $submac!(input, $($args)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let size = i + ($i).input_len() - input.input_len(); + ret = $crate::IResult::Incomplete($crate::Needed::Size(size)); break; + }, + $crate::IResult::Done(i, o) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many0,input)); break; + } + + res.push(o); + input = i; + } + } + } + + ret + } + ); + ($i:expr, $f:expr) => ( + many0!($i, call!($f)); + ); +); + +/// `many1!(I -> IResult) => I -> IResult>` +/// Applies the parser 1 or more times and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Error(Position(ErrorKind::Many1,&b[..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! many1( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + match $submac!($i, $($args)*) { + $crate::IResult::Error(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many1,$i)), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1,o1) => { + if i1.input_len() == 0 { + $crate::IResult::Done(i1,vec![o1]) + } else { + + let mut res = ::std::vec::Vec::with_capacity(4); + res.push(o1); + let mut input = i1; + let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; + loop { + if input.input_len() == 0 { + break; + } + match $submac!(input, $($args)*) { + $crate::IResult::Error(_) => { + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); + break; + }, + $crate::IResult::Done(i, o) => { + if i.input_len() == input.input_len() { + break; + } + res.push(o); + input = i; + } + } + } + + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, res) + } + } + } + } + } + ); + ($i:expr, $f:expr) => ( + many1!($i, call!($f)); + ); +); + +/// `many_m_n!(usize, usize, I -> IResult) => I -> IResult>` +/// Applies the parser between m and n times (n included) and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]),Error(Position(ErrorKind::ManyMN,&a[..]))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); +/// # } +/// ``` +#[macro_export] +macro_rules! many_m_n( + ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + let mut res = ::std::vec::Vec::with_capacity($m); + let mut input = $i; + let mut count: usize = 0; + let mut err = false; + let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; + loop { + if count == $n { break } + match $submac!(input, $($args)*) { + $crate::IResult::Done(i, o) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i.input_len() == input.input_len() { + break; + } + res.push(o); + input = i; + count += 1; + } + $crate::IResult::Error(_) => { + err = true; + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); + break; + }, + } + if input.input_len() == 0 { + break; + } + } + + if count < $m { + if err { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ManyMN,$i)) + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Incomplete($crate::Needed::Unknown) + } + } + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, res) + } + } + } + ); + ($i:expr, $m:expr, $n: expr, $f:expr) => ( + many_m_n!($i, $m, $n, call!($f)); + ); +); + +/// `count!(I -> IResult, nb) => I -> IResult>` +/// Applies the child parser a specified number of times +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); +/// +/// let a = b"abcdabcdabcdef"; +/// let b = b"abcdefgh"; +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// +/// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); +/// assert_eq!(counter(&b[..]), Error(Position(ErrorKind::Count, &b[..]))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! count( + ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( + { + let ret; + let mut input = $i; + let mut res = ::std::vec::Vec::with_capacity($count); + + loop { + if res.len() == $count { + ret = $crate::IResult::Done(input, res); break; + } + + match $submac!(input, $($args)*) { + $crate::IResult::Done(i,o) => { + res.push(o); + input = i; + }, + $crate::IResult::Error(_) => { + ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Count,$i)); break; + }, + $crate::IResult::Incomplete(_) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; + } + } + } + + ret + } + ); + ($i:expr, $f:expr, $count: expr) => ( + count!($i, call!($f), $count); + ); +); + +/// `count_fixed!(O, I -> IResult, nb) => I -> IResult` +/// Applies the child parser a fixed number of times and returns a fixed size array +/// The type must be specified and it must be `Copy` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], tag!( "abcd" ), 2 ) ); +/// // can omit the type specifier if returning slices +/// // named!(counter< [&[u8]; 2] >, count_fixed!( tag!( "abcd" ), 2 ) ); +/// +/// let a = b"abcdabcdabcdef"; +/// let b = b"abcdefgh"; +/// let res = [&b"abcd"[..], &b"abcd"[..]]; +/// +/// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); +/// assert_eq!(counter(&b[..]), Error(Position(ErrorKind::Count, &b[..]))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! count_fixed ( + ($i:expr, $typ:ty, $submac:ident!( $($args:tt)* ), $count: expr) => ( + { + let ret; + let mut input = $i; + // `$typ` must be Copy, and thus having no destructor, this is panic safe + let mut res: [$typ; $count] = unsafe{[::std::mem::uninitialized(); $count as usize]}; + let mut cnt: usize = 0; + + loop { + if cnt == $count { + ret = $crate::IResult::Done(input, res); break; + } + + match $submac!(input, $($args)*) { + $crate::IResult::Done(i,o) => { + res[cnt] = o; + cnt += 1; + input = i; + }, + $crate::IResult::Error(_) => { + ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Count,$i)); break; + }, + $crate::IResult::Incomplete(_) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; + } + } + } + + ret + } + ); + ($i:expr, $typ: ty, $f:ident, $count: expr) => ( + count_fixed!($i, $typ, call!($f), $count); + ); +); + +/// `length_value!(I -> IResult, I -> IResult) => I -> IResult>` +/// gets a number from the first parser, then applies the second parser that many times +#[macro_export] +macro_rules! length_value( + ($i:expr, $f:expr, $g:expr) => ( + { + match $f($i) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(inum, onum) => { + let ret; + let length_token = $i.len() - inum.len(); + let mut input = inum; + let mut res = ::std::vec::Vec::new(); + + loop { + if res.len() == onum as usize { + ret = $crate::IResult::Done(input, res); break; + } + + match $g(input) { + $crate::IResult::Done(iparse, oparse) => { + res.push(oparse); + input = iparse; + }, + $crate::IResult::Error(_) => { + ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::LengthValue,$i)); break; + }, + $crate::IResult::Incomplete(a) => { + ret = match a { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(length) => $crate::IResult::Incomplete($crate::Needed::Size(length_token + onum as usize * length)) + }; + break; + } + } + } + + ret + } + } + } + ); + ($i:expr, $f:expr, $g:expr, $length:expr) => ( + { + match $f($i) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(inum, onum) => { + let ret; + let length_token = $i.len() - inum.len(); + let mut input = inum; + let mut res = ::std::vec::Vec::new(); + + loop { + if res.len() == onum as usize { + ret = $crate::IResult::Done(input, res); break; + } + + match $g(input) { + $crate::IResult::Done(iparse, oparse) => { + res.push(oparse); + input = iparse; + }, + $crate::IResult::Error(_) => { + ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::LengthValue,$i)); break; + }, + $crate::IResult::Incomplete(a) => { + ret = match a { + $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::Needed::Size(_) => $crate::IResult::Incomplete($crate::Needed::Size(length_token + onum as usize * $length)) + }; + break; + } + } + } + + ret + } + } + } + ); +); + +/// `fold_many0!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser 0 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); +/// # } +/// ``` +/// 0 or more +#[macro_export] +macro_rules! fold_many0( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( + { + use $crate::InputLength; + let ret; + let f = $f; + let mut res = $init; + let mut input = $i; + + loop { + if input.input_len() == 0 { + ret = $crate::IResult::Done(input, res); break; + } + + match $submac!(input, $($args)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let size = i + ($i).input_len() - input.input_len(); + ret = $crate::IResult::Incomplete($crate::Needed::Size(size)); break; + }, + $crate::IResult::Done(i, o) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many0,input)); break; + } + + res = f(res, o); + input = i; + } + } + } + + ret + } + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many0!($i, call!($f), $init, $fold_f); + ); +); + +/// `fold_many1!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser 1 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Error(Position(ErrorKind::Many1,&b[..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! fold_many1( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( + { + use $crate::InputLength; + match $submac!($i, $($args)*) { + $crate::IResult::Error(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many1,$i)), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1,o1) => { + let acc = $init; + let f = $f; + if i1.len() == 0 { + let acc = f(acc, o1); + $crate::IResult::Done(i1,acc) + } else { + let mut acc = f(acc, o1); + let mut input = i1; + let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; + loop { + if input.input_len() == 0 { + break; + } + match $submac!(input, $($args)*) { + $crate::IResult::Error(_) => { + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); + break; + }, + $crate::IResult::Done(i, o) => { + if i.input_len() == input.input_len() { + break; + } + acc = f(acc, o); + input = i; + } + } + } + + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, acc) + } + } + } + } + } + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many1!($i, call!($f), $init, $fold_f); + ); +); + +/// `fold_many_m_n!(usize, usize, I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser between m and n times (n included) and folds the list of return value +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]),Error(Position(ErrorKind::ManyMN,&a[..]))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); +/// # } +/// ``` +#[macro_export] +macro_rules! fold_many_m_n( + ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( + { + use $crate::InputLength; + let mut acc = $init; + let f = $f; + let mut input = $i; + let mut count: usize = 0; + let mut err = false; + let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; + loop { + if count == $n { break } + match $submac!(input, $($args)*) { + $crate::IResult::Done(i, o) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i.input_len() == input.input_len() { + break; + } + acc = f(acc, o); + input = i; + count += 1; + } + $crate::IResult::Error(_) => { + err = true; + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); + break; + }, + } + if input.input_len() == 0 { + break; + } + } + + if count < $m { + if err { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ManyMN,$i)) + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Incomplete($crate::Needed::Unknown) + } + } + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, acc) + } + } + } + ); + ($i:expr, $m:expr, $n: expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many_m_n!($i, $m, $n, call!($f), $init, $fold_f); + ); +); + +#[cfg(test)] +mod tests { + use internal::{Needed,IResult,Err}; + use internal::IResult::*; + use internal::Err::*; + use util::ErrorKind; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use std::cmp::min; + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: $crate::IResult<_,_> = if reduced != b { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Tag, $i)) + } else if m < blen { + $crate::IResult::Incomplete($crate::Needed::Size(blen)) + } else { + $crate::IResult::Done(&$i[blen..], reduced) + }; + res + } + ); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) + }; + res + } + ); + ); + + + mod pub_named_mod { + named!(pub tst, tag!("abcd")); + } + + #[test] + fn pub_named_test() { + let a = &b"abcd"[..]; + let res = pub_named_mod::tst(a); + assert_eq!(res, Done(&b""[..], a)); + } + + #[test] + fn apply_test() { + fn sum2(a:u8, b:u8) -> u8 { a + b } + fn sum3(a:u8, b:u8, c:u8) -> u8 { a + b + c } + let a = apply!(1, sum2, 2); + let b = apply!(1, sum3, 2, 3); + + assert_eq!(a, 3); + assert_eq!(b, 6); + } + + #[derive(PartialEq,Eq,Debug)] + struct B { + a: u8, + b: u8 + } + + #[test] + fn chain2() { + fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; + fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) }; + + named!(chain_parser<&[u8],B>, + chain!( + tag!("abcd") ~ + tag!("abcd")? ~ + aa: ret_int1 ~ + tag!("efgh") ~ + bb: ret_int2 ~ + tag!("efgh") , + ||{B{a: aa, b: bb}} + ) + ); + + assert_eq!(chain_parser(&b"abcdabcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); + assert_eq!(chain_parser(&b"abcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); + assert_eq!(chain_parser(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(chain_parser(&b"abcdefghef"[..]), Incomplete(Needed::Size(12))); + } + + #[test] + fn nested_chain() { + fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; + fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) }; + + named!(chain_parser<&[u8],B>, + chain!( + chain!( + tag!("abcd") ~ + tag!("abcd")? , + || {} + ) ~ + aa: ret_int1 ~ + tag!("efgh") ~ + bb: ret_int2 ~ + tag!("efgh") , + ||{B{a: aa, b: bb}} + ) + ); + + assert_eq!(chain_parser(&b"abcdabcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); + assert_eq!(chain_parser(&b"abcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); + assert_eq!(chain_parser(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(chain_parser(&b"abcdefghef"[..]), Incomplete(Needed::Size(12))); + } + + #[derive(PartialEq,Eq,Debug)] + struct C { + a: u8, + b: Option + } + + #[test] + fn chain_mut() { + fn ret_b1_2(i:&[u8]) -> IResult<&[u8], B> { Done(i,B{a:1,b:2}) }; + named!(f<&[u8],B>, + chain!( + tag!("abcd") ~ + tag!("abcd")? ~ + tag!("efgh") ~ + mut bb: ret_b1_2 ~ + tag!("efgh") , + ||{ + bb.b = 3; + bb + } + ) + ); + + let r = f(&b"abcdabcdefghefghX"[..]); + assert_eq!(r, Done(&b"X"[..], B{a: 1, b: 3})); + } + + #[test] + fn chain_opt() { + named!(y, tag!("efgh")); + fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; + named!(ret_y<&[u8], u8>, map!(y, |_| 2)); + + named!(chain_parser<&[u8],C>, + chain!( + tag!("abcd") ~ + aa: ret_int1 ~ + bb: ret_y? , + ||{C{a: aa, b: bb}} + ) + ); + + assert_eq!(chain_parser(&b"abcdefghX"[..]), Done(&b"X"[..], C{a: 1, b: Some(2)})); + assert_eq!(chain_parser(&b"abcdWXYZ"[..]), Done(&b"WXYZ"[..], C{a: 1, b: None})); + assert_eq!(chain_parser(&b"abcdX"[..]), Done(&b"X"[..], C{ a: 1, b: None })); + assert_eq!(chain_parser(&b"abcdef"[..]), Incomplete(Needed::Size(8))); + } + + use util::{error_to_list, add_error_pattern, print_error}; + + fn error_to_string

(e: &Err

) -> &'static str { + let v:Vec = error_to_list(e); + // do it this way if you can use slice patterns + /* + match &v[..] { + [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag", + [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`", + _ => "unrecognized error" + } + */ + if &v[..] == [ErrorKind::Custom(42),ErrorKind::Tag] { + "missing `ijkl` tag" + } else if &v[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] { + "missing `mnop` tag after `ijkl`" + } else { + "unrecognized error" + } + } + + // do it this way if you can use box patterns + /*use std::str; + fn error_to_string(e:Err) -> String + match e { + NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => { + format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap()) + }, + NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => { + format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap()) + }, + _ => "unrecognized error".to_string() + } + }*/ + use std::collections; + #[test] + fn err() { + named!(err_test, alt!( + tag!("abcd") | + preceded!(tag!("efgh"), error!(ErrorKind::Custom(42), + chain!( + tag!("ijkl") ~ + res: error!(ErrorKind::Custom(128), tag!("mnop")) , + || { res } + ) + ) + ) + )); + let a = &b"efghblah"[..]; + let b = &b"efghijklblah"[..]; + let c = &b"efghijklmnop"[..]; + + let blah = &b"blah"[..]; + + let res_a = err_test(a); + let res_b = err_test(b); + let res_c = err_test(c); + assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah))))); + assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..], Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah))))))); + assert_eq!(res_c, Done(&b""[..], &b"mnop"[..])); + + // Merr-like error matching + let mut err_map = collections::HashMap::new(); + assert!(add_error_pattern(&mut err_map, err_test(&b"efghpouet"[..]), "missing `ijkl` tag")); + assert!(add_error_pattern(&mut err_map, err_test(&b"efghijklpouet"[..]), "missing `mnop` tag after `ijkl`")); + + let res_a2 = res_a.clone(); + match res_a { + Error(e) => { + assert_eq!(error_to_list(&e), [ErrorKind::Custom(42), ErrorKind::Tag]); + assert_eq!(error_to_string(&e), "missing `ijkl` tag"); + assert_eq!(err_map.get(&error_to_list(&e)), Some(&"missing `ijkl` tag")); + }, + _ => panic!() + }; + + let res_b2 = res_b.clone(); + match res_b { + Error(e) => { + assert_eq!(error_to_list(&e), [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag]); + assert_eq!(error_to_string(&e), "missing `mnop` tag after `ijkl`"); + assert_eq!(err_map.get(&error_to_list(&e)), Some(&"missing `mnop` tag after `ijkl`")); + }, + _ => panic!() + }; + + print_error(a, res_a2); + print_error(b, res_b2); + } + + #[test] + fn add_err() { + named!(err_test, + preceded!(tag!("efgh"), add_error!(ErrorKind::Custom(42), + chain!( + tag!("ijkl") ~ + res: add_error!(ErrorKind::Custom(128), tag!("mnop")) , + || { res } + ) + ) + )); + let a = &b"efghblah"[..]; + let b = &b"efghijklblah"[..]; + let c = &b"efghijklmnop"[..]; + + let blah = &b"blah"[..]; + + let res_a = err_test(a); + let res_b = err_test(b); + let res_c = err_test(c); + assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah))))); + assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..], Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah))))))); + assert_eq!(res_c, Done(&b""[..], &b"mnop"[..])); + } + + #[test] + fn complete() { + named!(err_test, + chain!( + tag!("ijkl") ~ + res: complete!(tag!("mnop")) , + || { res } + ) + ); + let a = &b"ijklmn"[..]; + + let res_a = err_test(a); + assert_eq!(res_a, Error(Position(ErrorKind::Complete, &b"mn"[..]))); + } + #[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { + Done(&b""[..], input) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8],&[u8],&'static str> { + Error(Code(ErrorKind::Custom("abcd"))) + } + + fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { + Done(input, &b""[..]) + } + + fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | dont_work) + } + fn alt2(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | work) + } + fn alt3(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + //named!(alt1, alt!(dont_work | dont_work)); + //named!(alt2, alt!(dont_work | work)); + //named!(alt3, alt!(dont_work | dont_work | work2 | dont_work)); + + let a = &b"abcd"[..]; + assert_eq!(alt1(a), Error(Position(ErrorKind::Alt, a))); + assert_eq!(alt2(a), Done(&b""[..], a)); + assert_eq!(alt3(a), Done(a, &b""[..])); + + named!(alt4, alt!(tag!("abcd") | tag!("efgh"))); + let b = &b"efgh"[..]; + assert_eq!(alt4(a), Done(&b""[..], a)); + assert_eq!(alt4(b), Done(&b""[..], b)); + + // test the alternative syntax + named!(alt5, alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })); + assert_eq!(alt5(a), Done(&b""[..], false)); + assert_eq!(alt5(b), Done(&b""[..], true)); + + } + + #[test] + fn alt_incomplete() { + named!(alt1, alt!(tag!("a") | tag!("bc") | tag!("def"))); + + let a = &b""[..]; + assert_eq!(alt1(a), Incomplete(Needed::Size(1))); + let a = &b"b"[..]; + assert_eq!(alt1(a), Incomplete(Needed::Size(2))); + let a = &b"bcd"[..]; + assert_eq!(alt1(a), Done(&b"d"[..], &b"bc"[..])); + let a = &b"cde"[..]; + assert_eq!(alt1(a), Error(Position(ErrorKind::Alt, a))); + let a = &b"de"[..]; + assert_eq!(alt1(a), Incomplete(Needed::Size(3))); + let a = &b"defg"[..]; + assert_eq!(alt1(a), Done(&b"g"[..], &b"def"[..])); + } + + #[test] + fn alt_complete() { + named!(ac<&[u8], &[u8]>, + alt_complete!(tag!("abcd") | tag!("ef") | tag!("ghi") | tag!("kl")) + ); + + let a = &b""[..]; + assert_eq!(ac(a), Incomplete(Needed::Size(2))); + let a = &b"ef"[..]; + assert_eq!(ac(a), Done(&b""[..], &b"ef"[..])); + let a = &b"cde"[..]; + assert_eq!(ac(a), Error(Position(ErrorKind::Alt, a))); + } + + #[test] + fn switch() { + named!(sw, + switch!(take!(4), + b"abcd" => take!(2) | + b"efgh" => take!(4) + ) + ); + + let a = &b"abcdefgh"[..]; + assert_eq!(sw(a), Done(&b"gh"[..], &b"ef"[..])); + + let b = &b"efghijkl"[..]; + assert_eq!(sw(b), Done(&b""[..], &b"ijkl"[..])); + let c = &b"afghijkl"[..]; + assert_eq!(sw(c), Error(Position(ErrorKind::Switch, &b"afghijkl"[..]))); + } + + #[test] + fn opt() { + named!(opt_abcd<&[u8],Option<&[u8]> >, opt!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_abcd(a), Done(&b"ef"[..], Some(&b"abcd"[..]))); + assert_eq!(opt_abcd(b), Done(&b"bcdefg"[..], None)); + assert_eq!(opt_abcd(c), Incomplete(Needed::Size(4))); + } + + #[test] + fn opt_res() { + named!(opt_res_abcd<&[u8], Result<&[u8], Err<&[u8]>> >, opt_res!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_res_abcd(a), Done(&b"ef"[..], Ok(&b"abcd"[..]))); + assert_eq!(opt_res_abcd(b), Done(&b"bcdefg"[..], Err(Position(ErrorKind::Tag, b)))); + assert_eq!(opt_res_abcd(c), Incomplete(Needed::Size(4))); + } + + #[test] + fn cond() { + let f_true: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( true, tag!("abcd") ) )); + let f_false: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( false, tag!("abcd") ) )); + //let f_false = closure!(&'static [u8], cond!( false, tag!("abcd") ) ); + + assert_eq!(f_true(&b"abcdef"[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); + assert_eq!(f_true(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(f_true(&b"xxx"[..]), Done(&b"xxx"[..], None)); + + assert_eq!(f_false(&b"abcdef"[..]), Done(&b"abcdef"[..], None)); + assert_eq!(f_false(&b"ab"[..]), Done(&b"ab"[..], None)); + assert_eq!(f_false(&b"xxx"[..]), Done(&b"xxx"[..], None)); + } + + #[test] + fn cond_wrapping() { + // Test that cond!() will wrap a given identifier in the call!() macro. + named!( tag_abcd, tag!("abcd") ); + let f_true: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( true, tag_abcd ) )); + let f_false: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( false, tag_abcd ) )); + //let f_false = closure!(&'static [u8], cond!( b2, tag!("abcd") ) ); + + assert_eq!(f_true(&b"abcdef"[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); + assert_eq!(f_true(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(f_true(&b"xxx"[..]), Done(&b"xxx"[..], None)); + + assert_eq!(f_false(&b"abcdef"[..]), Done(&b"abcdef"[..], None)); + assert_eq!(f_false(&b"ab"[..]), Done(&b"ab"[..], None)); + assert_eq!(f_false(&b"xxx"[..]), Done(&b"xxx"[..], None)); + } + + #[test] + fn peek() { + named!(peek_tag<&[u8],&[u8]>, peek!(tag!("abcd"))); + + assert_eq!(peek_tag(&b"abcdef"[..]), Done(&b"abcdef"[..], &b"abcd"[..])); + assert_eq!(peek_tag(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(peek_tag(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn pair() { + named!( tag_abc, tag!("abc") ); + named!( tag_def, tag!("def") ); + named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) ); + + assert_eq!(pair_abc_def(&b"abcdefghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))); + assert_eq!(pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(pair_abc_def(&b"abcd"[..]), Incomplete(Needed::Size(6))); + assert_eq!(pair_abc_def(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(pair_abc_def(&b"xxxdef"[..]), Error(Position(ErrorKind::Tag, &b"xxxdef"[..]))); + assert_eq!(pair_abc_def(&b"abcxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn separated_pair() { + named!( tag_abc, tag!("abc") ); + named!( tag_def, tag!("def") ); + named!( tag_separator, tag!(",") ); + named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); + + assert_eq!(sep_pair_abc_def(&b"abc,defghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))); + assert_eq!(sep_pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Incomplete(Needed::Size(7))); + assert_eq!(sep_pair_abc_def(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(sep_pair_abc_def(&b"xxx,def"[..]), Error(Position(ErrorKind::Tag, &b"xxx,def"[..]))); + assert_eq!(sep_pair_abc_def(&b"abc,xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn preceded() { + named!( tag_abcd, tag!("abcd") ); + named!( tag_efgh, tag!("efgh") ); + named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); + + assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"efgh"[..])); + assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8))); + assert_eq!(preceded_abcd_efgh(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(preceded_abcd_efgh(&b"xxxxdef"[..]), Error(Position(ErrorKind::Tag, &b"xxxxdef"[..]))); + assert_eq!(preceded_abcd_efgh(&b"abcdxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn terminated() { + named!( tag_abcd, tag!("abcd") ); + named!( tag_efgh, tag!("efgh") ); + named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); + + assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"abcd"[..])); + assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8))); + assert_eq!(terminated_abcd_efgh(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(terminated_abcd_efgh(&b"xxxxdef"[..]), Error(Position(ErrorKind::Tag, &b"xxxxdef"[..]))); + assert_eq!(terminated_abcd_efgh(&b"abcdxxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxxx"[..]))); + } + + #[test] + fn delimited() { + named!( tag_abc, tag!("abc") ); + named!( tag_def, tag!("def") ); + named!( tag_ghi, tag!("ghi") ); + named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); + + assert_eq!(delimited_abc_def_ghi(&b"abcdefghijkl"[..]), Done(&b"jkl"[..], &b"def"[..])); + assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Incomplete(Needed::Size(6))); + assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Incomplete(Needed::Size(9))); + assert_eq!(delimited_abc_def_ghi(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"xxxdefghi"[..]), Error(Position(ErrorKind::Tag, &b"xxxdefghi"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"abcxxxghi"[..]), Error(Position(ErrorKind::Tag, &b"xxxghi"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"abcdefxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn separated_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); + named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b",,abc"[..]; + let e = &b"abcd,abcd,ef"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"ef"[..], res2)); + assert_eq!(multi(c), Done(&b"azerty"[..], Vec::new())); + assert_eq!(multi_empty(d), Error(Position(ErrorKind::SeparatedList, d))); + //let res3 = vec![&b""[..], &b""[..], &b""[..]]; + //assert_eq!(multi_empty(d), Done(&b"abc"[..], res3)); + let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(e), Done(&b",ef"[..], res4)); + } + + #[test] + fn separated_nonempty_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b"abcd,abcd,ef"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"ef"[..], res2)); + assert_eq!(multi(c), Error(Position(ErrorKind::Tag,c))); + let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(d), Done(&b",ef"[..], res3)); + } + + #[test] + fn many0() { + named!( tag_abcd, tag!("abcd") ); + named!( tag_empty, tag!("") ); + named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); + + assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); + assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); + assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); + assert_eq!(multi_empty(&b"abcdef"[..]), Error(Position(ErrorKind::Many0, &b"abcdef"[..]))); + } + + #[cfg(feature = "nightly")] + use test::Bencher; + + #[cfg(feature = "nightly")] + #[bench] + fn many0_bench(b: &mut Bencher) { + named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); + b.iter(|| { + multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..]) + }); + } + + #[test] + fn many1() { + named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res2)); + assert_eq!(multi(c), Error(Position(ErrorKind::Many1,c))); + assert_eq!(multi(d), Incomplete(Needed::Size(8))); + } + + #[test] + fn infinite_many() { + fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { + println!("input: {:?}", input); + Error(Position(ErrorKind::Custom(0),input)) + } + + // should not go into an infinite loop + named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi0(a), Done(a, Vec::new())); + + named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi1(a), Error(Position(ErrorKind::Many1,a))); + } + + #[test] + fn many_m_n() { + named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Error(Err::Position(ErrorKind::ManyMN,a))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res1)); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Done(&b"efgh"[..], res2)); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); + assert_eq!(multi(e), Incomplete(Needed::Size(8))); + } + + #[test] + fn count() { + const TIMES: usize = 2; + named!( tag_abc, tag!("abc") ); + named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]])); + assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Unknown)); + assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Unknown)); + assert_eq!(cnt_2(&b"xxx"[..]), Error(Position(ErrorKind::Count, &b"xxx"[..]))); + assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(Position(ErrorKind::Count, &b"xxxabcabcdef"[..]))); + assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(Position(ErrorKind::Count, &b"abcxxxabcdef"[..]))); + } + + #[test] + fn count_zero() { + const TIMES: usize = 0; + named!( tag_abc, tag!("abc") ); + named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + let done = &b"abcabcabcdef"[..]; + let parsed_done = Vec::new(); + let rest = done; + let incomplete_1 = &b"ab"[..]; + let parsed_incompl_1 = Vec::new(); + let incomplete_2 = &b"abcab"[..]; + let parsed_incompl_2 = Vec::new(); + let error = &b"xxx"[..]; + let error_remain = &b"xxx"[..]; + let parsed_err = Vec::new(); + let error_1 = &b"xxxabcabcdef"[..]; + let parsed_err_1 = Vec::new(); + let error_1_remain = &b"xxxabcabcdef"[..]; + let error_2 = &b"abcxxxabcdef"[..]; + let parsed_err_2 = Vec::new(); + let error_2_remain = &b"abcxxxabcdef"[..]; + + assert_eq!(counter_2(done), Done(rest, parsed_done)); + assert_eq!(counter_2(incomplete_1), Done(incomplete_1, parsed_incompl_1)); + assert_eq!(counter_2(incomplete_2), Done(incomplete_2, parsed_incompl_2)); + assert_eq!(counter_2(error), Done(error_remain, parsed_err)); + assert_eq!(counter_2(error_1), Done(error_1_remain, parsed_err_1)); + assert_eq!(counter_2(error_2), Done(error_2_remain, parsed_err_2)); + } + + #[test] + fn count_fixed() { + const TIMES: usize = 2; + named!( tag_abc, tag!("abc") ); + named!( cnt_2<&[u8], [&[u8]; TIMES] >, count_fixed!(&[u8], tag_abc, TIMES ) ); + + assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], [&b"abc"[..], &b"abc"[..]])); + assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Unknown)); + assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Unknown)); + assert_eq!(cnt_2(&b"xxx"[..]), Error(Position(ErrorKind::Count, &b"xxx"[..]))); + assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(Position(ErrorKind::Count, &b"xxxabcabcdef"[..]))); + assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(Position(ErrorKind::Count, &b"abcxxxabcdef"[..]))); + } + + use nom::{le_u16,eof}; + #[allow(dead_code)] + pub fn compile_count_fixed(input: &[u8]) -> IResult<&[u8], ()> { + chain!(input, + tag!("abcd") ~ + count_fixed!( u16, le_u16, 4 ) ~ + eof , + || { () } + ) + } + + #[test] + fn count_fixed_no_type() { + const TIMES: usize = 2; + named!( tag_abc, tag!("abc") ); + named!( counter_2<&[u8], [&[u8]; TIMES], () >, count_fixed!(&[u8], tag_abc, TIMES ) ); + + let done = &b"abcabcabcdef"[..]; + let parsed_main = [&b"abc"[..], &b"abc"[..]]; + let rest = &b"abcdef"[..]; + let incomplete_1 = &b"ab"[..]; + let incomplete_2 = &b"abcab"[..]; + let error = &b"xxx"[..]; + let error_1 = &b"xxxabcabcdef"[..]; + let error_1_remain = &b"xxxabcabcdef"[..]; + let error_2 = &b"abcxxxabcdef"[..]; + let error_2_remain = &b"abcxxxabcdef"[..]; + + assert_eq!(counter_2(done), Done(rest, parsed_main)); + assert_eq!(counter_2(incomplete_1), Incomplete(Needed::Unknown)); + assert_eq!(counter_2(incomplete_2), Incomplete(Needed::Unknown)); + assert_eq!(counter_2(error), Error(Position(ErrorKind::Count, error))); + assert_eq!(counter_2(error_1), Error(Position(ErrorKind::Count, error_1_remain))); + assert_eq!(counter_2(error_2), Error(Position(ErrorKind::Count, error_2_remain))); + } + + use nom::{be_u8,be_u16}; + #[test] + fn length_value_test() { + named!(length_value_1<&[u8], Vec >, length_value!(be_u8, be_u16)); + named!(length_value_2<&[u8], Vec >, length_value!(be_u8, be_u16, 2)); + + let i1 = vec![0, 5, 6]; + assert_eq!(length_value_1(&i1), IResult::Done(&i1[1..], vec![])); + assert_eq!(length_value_2(&i1), IResult::Done(&i1[1..], vec![])); + + let i2 = vec![1, 5, 6, 3]; + assert_eq!(length_value_1(&i2), IResult::Done(&i2[3..], vec![1286])); + assert_eq!(length_value_2(&i2), IResult::Done(&i2[3..], vec![1286])); + + let i3 = vec![2, 5, 6, 3, 4, 5, 7]; + assert_eq!(length_value_1(&i3), IResult::Done(&i3[5..], vec![1286, 772])); + assert_eq!(length_value_2(&i3), IResult::Done(&i3[5..], vec![1286, 772])); + + let i4 = vec![2, 5, 6, 3]; + assert_eq!(length_value_1(&i4), IResult::Incomplete(Needed::Size(5))); + assert_eq!(length_value_2(&i4), IResult::Incomplete(Needed::Size(5))); + + let i5 = vec![3, 5, 6, 3, 4, 5]; + assert_eq!(length_value_1(&i5), IResult::Incomplete(Needed::Size(7))); + assert_eq!(length_value_2(&i5), IResult::Incomplete(Needed::Size(7))); + } + + #[test] + fn fold_many0() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!( tag_abcd, tag!("abcd") ); + named!( tag_empty, tag!("") ); + named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); + + assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); + assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); + assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); + assert_eq!(multi_empty(&b"abcdef"[..]), Error(Position(ErrorKind::Many0, &b"abcdef"[..]))); + } + + #[test] + fn fold_many1() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res2)); + assert_eq!(multi(c), Error(Position(ErrorKind::Many1,c))); + assert_eq!(multi(d), Incomplete(Needed::Size(8))); + } + + #[test] + fn fold_many_m_n() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Error(Err::Position(ErrorKind::ManyMN,a))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res1)); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Done(&b"efgh"[..], res2)); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); + assert_eq!(multi(e), Incomplete(Needed::Size(8))); + } + + #[test] + fn chain_incomplete() { + let res = chain!(&b"abcdefgh"[..], + a: take!(4) ~ + b: take!(8), + ||{(a,b )} + ); + + assert_eq!(res, IResult::Incomplete(Needed::Size(12))); + } + + #[test] + fn tuple_test() { + named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, + tuple!( be_u16 , take!(3), tag!("fg") ) ); + + assert_eq!(tuple_3(&b"abcdefgh"[..]), Done(&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..]))); + assert_eq!(tuple_3(&b"abcd"[..]), Incomplete(Needed::Size(5))); + assert_eq!(tuple_3(&b"abcde"[..]), Incomplete(Needed::Size(7))); + assert_eq!(tuple_3(&b"abcdejk"[..]), Error(Position(ErrorKind::Tag, &b"jk"[..]))); + } + + #[test] + fn not() { + named!(not_aaa, not!(tag!("aaa"))); + assert_eq!(not_aaa(&b"aaa"[..]), Error(Position(ErrorKind::Not, &b"aaa"[..]))); + assert_eq!(not_aaa(&b"aa"[..]), Done(&b"aa"[..], &b""[..])); + assert_eq!(not_aaa(&b"abcd"[..]), Done(&b"abcd"[..], &b""[..])); + } +} diff --git third_party/rust/nom-1.2.4/src/methods.rs third_party/rust/nom-1.2.4/src/methods.rs new file mode 100644 index 000000000000..22868541a96c --- /dev/null +++ third_party/rust/nom-1.2.4/src/methods.rs @@ -0,0 +1,480 @@ +//! Method macro combinators +//! +//! These macros make parsers as methods of structs +//! and that can take methods of structs to call +//! as parsers. +//! +//! There is a trick to make them easier to assemble, +//! combinators are defined like this: +//! +//! ```ignore +//! macro_rules! tag ( +//! ($i:expr, $inp: expr) => ( +//! { +//! ... +//! } +//! ); +//! ); +//! ``` +//! +//! But when used as methods in other combinators, are used +//! like this: +//! +//! ```ignore +//! method!(my_function >, self, tag!("abcd")); +//! ``` +//! +//! Internally, other combinators will rewrite +//! that call to pass the input as second argument: +//! +//! ```ignore +//! macro_rules! method ( +//! ($name:ident<$a:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( +//! fn $name( $self_: $a, i: &[u8] ) -> $crate::IResult<&[u8], &[u8]> { +//! $submac!(i, $($args)*) +//! } +//! ); +//! ); +//! ``` +//! +//! The `method!` macro is similar to the `named!` macro in the macros module. +//! While `named!` will create a parser function, `method!` will create a parser +//! method on the struct it is defined in. +//! +//! Compared to the `named!` macro there are a few differences in how they are +//! invoked. A `method!` invocation always has to have the type of `self` +//! declared and it can't be a reference due to Rust's borrow lifetime +//! restrictions: +//! ```ignore +//! // -`self`'s type- +//! method!(method_name< Parser<'a> >, ...); +//! ``` +//! `self`'s type always comes first. +//! The next difference is you have to input the self struct. Due to Rust's +//! macro hygiene the macro can't declare it on it's own. +//! ```ignore +//! // -self- +//! method!(method_name, &'a str, &'a str>, self, ...); +//! ``` +//! When making a parsing struct with parsing methods, due to the static borrow +//! checker,calling any parsing methods on self (or any other parsing struct) +//! will cause self to be moved for the rest of the method.To get around this +//! restriction all self is moved into the called method and then the called +//! method will return self to the caller. +//! +//! To call a method on self you need to use the `call_m!` macro. For example: +//! ```ignore +//! struct<'a> Parser<'a> { +//! parsed: &'a str, +//! } +//! impl<'a> Parser<'a> { +//! // Constructor omitted for brevity +//! method!(take4, &'a str, &'a str>, self, take!(4)); +//! method!(caller, &'a str, &'a str>, self, call_m!(self.take4)); +//! } +//! ``` +//! More complicated combinations still mostly look the same as their `named!` +//! counterparts: +//! ```ignore +//! method!(pub simple_chain<&mut Parser<'a>, &'a str, &'a str>, self, +//! chain!( +//! call_m!(self.tag_abc) ~ +//! call_m!(self.tag_def) ~ +//! call_m!(self.tag_ghi) ~ +//! last: call_m!(self.simple_peek) , +//! ||{sb.parsed = last; last} +//! ) +//! ); +//! ``` +//! The three additions to method definitions to remember are: +//! 1. Specify `self`'s type +//! 2. Pass `self` to the macro +//! 4. Call parser methods using the `call_m!` macro. + +/// Makes a method from a parser combination +/// +/// The must be set up because the compiler needs +/// the information +/// +/// ```ignore +/// method!(my_function >( &[u8] ) -> &[u8], tag!("abcd")); +/// // first type parameter is `self`'s type, second is input, third is output +/// method!(my_function, &[u8], &[u8]>, tag!("abcd")); +/// //prefix them with 'pub' to make the methods public +/// method!(pub my_function,&[u8], &[u8]>, tag!("abcd")); +/// ``` +#[macro_export] +macro_rules! method ( + // Non-public immutable self + ($name:ident<$a:ty>( $i:ty ) -> $o:ty, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty,$i:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + // Public immutable self + (pub $name:ident<$a:ty>( $i:ty ) -> $o:ty, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty,$i:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( $self_: $a,i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name<'a>( $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + // Non-public mutable self + ($name:ident<$a:ty>( $i:ty ) -> $o:ty, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty,$i:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( mut $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + ($name:ident<$a:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( mut $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + // Public mutable self + (pub $name:ident<$a:ty>( $i:ty ) -> $o:ty, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty,$i:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( mut $self_: $a,i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name<'a>( mut $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); + (pub $name:ident<$a:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( mut $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { + let result = $submac!(i, $($args)*); + ($self_, result) + } + ); +); + +/// Used to called methods then move self back into self +#[macro_export] +macro_rules! call_m ( + ($i:expr, $self_:ident.$method:ident) => ( + { + let (tmp, res) = $self_.$method($i); + $self_ = tmp; + res + } + ); + ($i:expr, $self_:ident.$method:ident, $($args:expr),* ) => ( + { + let (tmp, res) = $self_.$method($i, $($args),*); + $self_ = tmp; + res + } + ); +); + + +/// emulate function currying for method calls on structs +/// `apply!(self.my_function, arg1, arg2, ...)` becomes `self.my_function(input, arg1, arg2, ...)` +/// +/// Supports up to 6 arguments +#[macro_export] +macro_rules! apply_m ( + ($i:expr, $self_:ident.$method:ident, $($args:expr),* ) => ( { let (tmp, res) = $self_.$method( $i, $($args),* ); $self_ = tmp; res } ); +); + +#[cfg(test)] +mod tests { + use internal::IResult::*; + + // reproduce the tag_s and take_s macros, because of module import order + macro_rules! tag_s ( + ($i:expr, $tag: expr) => ( + { + let res: $crate::IResult<_,_> = if $tag.len() > $i.len() { + $crate::IResult::Incomplete($crate::Needed::Size($tag.len())) + //} else if &$i[0..$tag.len()] == $tag { + } else if ($i).starts_with($tag) { + $crate::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagStr, $i)) + }; + res + } + ); + ); + + macro_rules! take_s ( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res: $crate::IResult<_,_> = if $i.chars().count() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + let mut offset = $i.len(); + let mut count = 0; + for (o, _) in $i.char_indices() { + if count == cnt { + offset = o; + break; + } + count += 1; + } + $crate::IResult::Done(&$i[offset..], &$i[..offset]) + }; + res + } + ); + ); + + struct Parser<'a> { + bcd: &'a str, + } + + impl<'a> Parser<'a> { + pub fn new() -> Parser<'a> { + Parser{bcd: ""} + } + + method!(tag_abc, &'a str, &'a str>, self, tag_s!("áβç")); + method!(tag_bcd >(&'a str) -> &'a str, self, tag_s!("βçδ")); + method!(pub tag_hij >(&'a str) -> &'a str, self, tag_s!("λïJ")); + method!(pub tag_ijk, &'a str, &'a str>, self, tag_s!("ïJƙ")); + method!(take3, &'a str, &'a str>, self, take_s!(3)); + method!(pub simple_call, &'a str, &'a str>, mut self, + call_m!(self.tag_abc) + ); + method!(pub simple_peek, &'a str, &'a str>, mut self, + peek!(call_m!(self.take3)) + ); + method!(pub simple_chain, &'a str, &'a str>, mut self, + chain!( + bcd: call_m!(self.tag_bcd) ~ + last: call_m!(self.simple_peek) , + ||{self.bcd = bcd; last} + ) + ); + fn tag_stuff(mut self: Parser<'a>, input: &'a str, something: &'a str) -> (Parser<'a>, ::IResult<&'a str, &'a str>) { + self.bcd = something; + let(tmp, res) = self.tag_abc(input); + self = tmp; + (self, res) + } + method!(use_apply, &'a str, &'a str>, mut self, apply_m!(self.tag_stuff, "βçδ")); + } + + #[test] + fn test_method_call_abc() { + let p = Parser::new(); + let input: &str = "áβçδèƒϱλïJƙ"; + let consumed: &str = "áβç"; + let leftover: &str = "δèƒϱλïJƙ"; + let(_, res) = p.tag_abc(input); + match res { + Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_abc` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.tag_abc` doesnt return the string it consumed \ + on success. Expected `{}`, got `{}`.", consumed, output); + }, + other => panic!("`Parser.tag_abc` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + + #[test] + fn test_method_call_bcd() { + let p = Parser::new(); + let input: &str = "βçδèƒϱλïJƙ"; + let consumed: &str = "βçδ"; + let leftover: &str = "èƒϱλïJƙ"; + let(_, res) = p.tag_bcd(input); + match res { + Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_bcd` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.tag_bcd` doesn't return the string it consumed \ + on success. Expected `{}`, got `{}`.", consumed, output); + }, + other => panic!("`Parser.tag_bcd` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + + #[test] + fn test_method_call_hij() { + let p = Parser::new(); + let input: &str = "λïJƙℓ₥ñôƥ9řƨ"; + let consumed: &str = "λïJ"; + let leftover: &str = "ƙℓ₥ñôƥ9řƨ"; + let(_, res) = p.tag_hij(input); + match res { + Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_hij` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.tag_hij` doesn't return the string it consumed \ + on success. Expected `{}`, got `{}`.", consumed, output); + }, + other => panic!("`Parser.tag_hij` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + + #[test] + fn test_method_call_ijk() { + let p = Parser::new(); + let input: &str = "ïJƙℓ₥ñôƥ9řƨ"; + let consumed: &str = "ïJƙ"; + let leftover: &str = "ℓ₥ñôƥ9řƨ"; + let(_, res) = p.tag_ijk(input); + match res { + Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_ijk` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.tag_ijk` doesn't return the string it consumed \ + on success. Expected `{}`, got `{}`.", consumed, output); + }, + other => panic!("`Parser.tag_ijk` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + #[test] + fn test_method_simple_call() { + let p = Parser::new(); + let input: &str = "áβçδèƒϱλïJƙ"; + let consumed: &str = "áβç"; + let leftover: &str = "δèƒϱλïJƙ"; + let(_, res) = p.simple_call(input); + match res { + Done(extra, output) => { assert!(extra == leftover, "`Parser.simple_call` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.simple_call` doesn't return the string it consumed \ + on success. Expected `{}`, got `{}`.", consumed, output); + }, + other => panic!("`Parser.simple_call` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + + #[test] + fn test_apply_m() { + let mut p = Parser::new(); + let input: &str = "áβçδèƒϱλïJƙ"; + let consumed: &str = "áβç"; + let leftover: &str = "δèƒϱλïJƙ"; + let(tmp, res) = p.use_apply(input); + p = tmp; + match res { + Done(extra, output) => { assert!(extra == leftover, "`Parser.use_apply` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.use_apply` doesn't return the string it was supposed to \ + on success. Expected `{}`, got `{}`.", leftover, output); + assert!(p.bcd == "βçδ", "Parser.use_apply didn't modify the parser field correctly: {}", p.bcd); + }, + other => panic!("`Parser.use_apply` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + + #[test] + fn test_method_call_peek() { + let p = Parser::new(); + let input: &str = "ж¥ƺáβçδèƒϱλïJƙ"; + let consumed: &str = "ж¥ƺ"; + let(_, res) = p.simple_peek(input); + match res { + Done(extra, output) => { assert!(extra == input, "`Parser.simple_peek` consumed leftover input. leftover: {}", extra); + assert!(output == consumed, "`Parser.simple_peek` doesn't return the string it consumed \ + on success. Expected `{}`, got `{}`.", consumed, output); + }, + other => panic!("`Parser.simple_peek` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } + + #[test] + fn test_method_call_chain() { + let mut p = Parser::new(); + let input : &str = "βçδδèƒϱλïJƙℓ"; + let leftover : &str = "δèƒϱλïJƙℓ"; + let output : &str = "δèƒ"; + let(tmp, res) = p.simple_chain(input); + p = tmp; + match res { + Done(extra, out) => { assert!(extra == leftover, "`Parser.simple_chain` consumed leftover input. leftover: {}", extra); + assert!(out == output, "`Parser.simple_chain` doesn't return the string it was supposed to \ + on success. Expected `{}`, got `{}`.", output, out); + assert!(p.bcd == "βçδ", "Parser.simple_chain didn't modify the parser field correctly: {}", p.bcd); + }, + other => panic!("`Parser.simple_chain` didn't succeed when it should have. \ + Got `{:?}`.", other), + } + } +} diff --git third_party/rust/nom-1.2.4/src/nom.rs third_party/rust/nom-1.2.4/src/nom.rs new file mode 100644 index 000000000000..74bac5bfb0c4 --- /dev/null +++ third_party/rust/nom-1.2.4/src/nom.rs @@ -0,0 +1,950 @@ +//! Useful parser combinators +//! +//! A number of useful parser combinators have already been implemented. +//! Some of them use macros, other are implemented through functions. +//! Hopefully, the syntax will converge to onely one way in the future, +//! but the macros system makes no promises. +//! + +#[cfg(feature = "core")] +use std::prelude::v1::*; +use std::boxed::Box; + +use std::fmt::Debug; +use internal::*; +use internal::IResult::*; +use internal::Err::*; +use util::{ErrorKind,IterIndices,AsChar,InputLength}; +use std::mem::transmute; + +#[inline] +pub fn tag_cl<'a,'b>(rec:&'a[u8]) -> Box IResult<&'b[u8], &'b[u8]> + 'a> { + Box::new(move |i: &'b[u8]| -> IResult<&'b[u8], &'b[u8]> { + if i.len() >= rec.len() && &i[0..rec.len()] == rec { + Done(&i[rec.len()..], &i[0..rec.len()]) + } else { + Error(Position(ErrorKind::TagClosure, i)) + } + }) +} + +#[cfg(not(feature = "core"))] +#[inline] +pub fn print(input: T) -> IResult { + println!("{:?}", input); + Done(input, ()) +} + +#[inline] +pub fn begin(input: &[u8]) -> IResult<(), &[u8]> { + Done((), input) +} + +// FIXME: when rust-lang/rust#17436 is fixed, macros will be able to export +// public methods +//pub is_not!(line_ending b"\r\n") +pub fn not_line_ending(input:&[u8]) -> IResult<&[u8], &[u8]> { + for (idx, item) in input.iter().enumerate() { + for &i in b"\r\n".iter() { + if *item == i { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input.len()..], input) +} + +named!(tag_ln, tag!("\n")); + +/// Recognizes a line feed +#[inline] +pub fn line_ending(input:&[u8]) -> IResult<&[u8], &[u8]> { + tag_ln(input) +} + +#[inline] +pub fn is_alphabetic(chr:u8) -> bool { + (chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A) +} + +#[inline] +pub fn is_digit(chr: u8) -> bool { + chr >= 0x30 && chr <= 0x39 +} + +#[inline] +pub fn is_hex_digit(chr: u8) -> bool { + (chr >= 0x30 && chr <= 0x39) || + (chr >= 0x41 && chr <= 0x46) || + (chr >= 0x61 && chr <= 0x66) +} + +#[inline] +pub fn is_oct_digit(chr: u8) -> bool { + chr >= 0x30 && chr <= 0x37 +} + +#[inline] +pub fn is_alphanumeric(chr: u8) -> bool { + is_alphabetic(chr) || is_digit(chr) +} + +#[inline] +pub fn is_space(chr:u8) -> bool { + chr == ' ' as u8 || chr == '\t' as u8 +} + +// FIXME: when rust-lang/rust#17436 is fixed, macros will be able to export +//pub filter!(alpha is_alphabetic) +//pub filter!(digit is_digit) +//pub filter!(hex_digit is_hex_digit) +//pub filter!(oct_digit is_oct_digit) +//pub filter!(alphanumeric is_alphanumeric) + +use std::ops::{Index,Range,RangeFrom}; +/// Recognizes lowercase and uppercase alphabetic characters: a-zA-Z +pub fn alpha<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::Alpha, input)) + } + + for (idx, item) in input.iter_indices() { + if ! item.is_alpha() { + if idx == 0 { + return Error(Position(ErrorKind::Alpha, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +/// Recognizes numerical characters: 0-9 +pub fn digit<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::Digit, input)) + } + + for (idx, item) in input.iter_indices() { + if ! item.is_0_to_9() { + if idx == 0 { + return Error(Position(ErrorKind::Digit, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +/// Recognizes hexadecimal numerical characters: 0-9, A-F, a-f +pub fn hex_digit<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::HexDigit, input)) + } + + for (idx, item) in input.iter_indices() { + if ! item.is_hex_digit() { + if idx == 0 { + return Error(Position(ErrorKind::HexDigit, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +/// Recognizes octal characters: 0-7 +pub fn oct_digit<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::OctDigit, input)) + } + + for (idx, item) in input.iter_indices() { + if ! item.is_oct_digit() { + if idx == 0 { + return Error(Position(ErrorKind::OctDigit, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +/// Recognizes numerical and alphabetic characters: 0-9a-zA-Z +pub fn alphanumeric<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::AlphaNumeric, input)); + } + + for (idx, item) in input.iter_indices() { + if ! item.is_alphanum() { + if idx == 0 { + return Error(Position(ErrorKind::AlphaNumeric, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +/// Recognizes spaces and tabs +pub fn space<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::Space, input)); + } + + for (idx, item) in input.iter_indices() { + let chr = item.as_char(); + if ! (chr == ' ' || chr == '\t') { + if idx == 0 { + return Error(Position(ErrorKind::Space, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +/// Recognizes spaces, tabs, carriage returns and line feeds +pub fn multispace<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: IterIndices+InputLength { + let input_length = input.input_len(); + if input_length == 0 { + return Error(Position(ErrorKind::MultiSpace, input)); + } + + for (idx, item) in input.iter_indices() { + let chr = item.as_char(); + if ! (chr == ' ' || chr == '\t' || chr == '\r' || chr == '\n') { + if idx == 0 { + return Error(Position(ErrorKind::MultiSpace, input)) + } else { + return Done(&input[idx..], &input[0..idx]) + } + } + } + Done(&input[input_length..], input) +} + +pub fn sized_buffer(input:&[u8]) -> IResult<&[u8], &[u8]> { + if input.is_empty() { + return Incomplete(Needed::Unknown) + } + + let len = input[0] as usize; + + if input.len() >= len + 1 { + Done(&input[len+1..], &input[1..len+1]) + } else { + Incomplete(Needed::Size(1 + len)) + } +} + +pub fn length_value(input:&[u8]) -> IResult<&[u8], &[u8]> { + let input_len = input.len(); + if input_len == 0 { + return Error(Position(ErrorKind::LengthValueFn, input)) + } + + let len = input[0] as usize; + if input_len - 1 >= len { + IResult::Done(&input[len+1..], &input[1..len+1]) + } else { + IResult::Incomplete(Needed::Size(1+len)) + } +} + +/// Recognizes an unsigned 1 byte integer (equivalent to take!(1) +#[inline] +pub fn be_u8(i: &[u8]) -> IResult<&[u8], u8> { + if i.len() < 1 { + Incomplete(Needed::Size(1)) + } else { + Done(&i[1..], i[0]) + } +} + +/// Recognizes big endian unsigned 2 bytes integer +#[inline] +pub fn be_u16(i: &[u8]) -> IResult<&[u8], u16> { + if i.len() < 2 { + Incomplete(Needed::Size(2)) + } else { + let res = ((i[0] as u16) << 8) + i[1] as u16; + Done(&i[2..], res) + } +} + +/// Recognizes big endian unsigned 4 bytes integer +#[inline] +pub fn be_u32(i: &[u8]) -> IResult<&[u8], u32> { + if i.len() < 4 { + Incomplete(Needed::Size(4)) + } else { + let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32; + Done(&i[4..], res) + } +} + +/// Recognizes big endian unsigned 8 bytes integer +#[inline] +pub fn be_u64(i: &[u8]) -> IResult<&[u8], u64> { + if i.len() < 8 { + Incomplete(Needed::Size(8)) + } else { + let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + + ((i[4] as u64) << 24) + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64; + Done(&i[8..], res) + } +} + +/// Recognizes a signed 1 byte integer (equivalent to take!(1) +#[inline] +pub fn be_i8(i:&[u8]) -> IResult<&[u8], i8> { + map!(i, be_u8, | x | { x as i8 }) +} + +/// Recognizes big endian signed 2 bytes integer +#[inline] +pub fn be_i16(i:&[u8]) -> IResult<&[u8], i16> { + map!(i, be_u16, | x | { x as i16 }) +} + +/// Recognizes big endian signed 4 bytes integer +#[inline] +pub fn be_i32(i:&[u8]) -> IResult<&[u8], i32> { + map!(i, be_u32, | x | { x as i32 }) +} + +/// Recognizes big endian signed 8 bytes integer +#[inline] +pub fn be_i64(i:&[u8]) -> IResult<&[u8], i64> { + map!(i, be_u64, | x | { x as i64 }) +} + +/// Recognizes an unsigned 1 byte integer (equivalent to take!(1) +#[inline] +pub fn le_u8(i: &[u8]) -> IResult<&[u8], u8> { + if i.len() < 1 { + Incomplete(Needed::Size(1)) + } else { + Done(&i[1..], i[0]) + } +} + +/// Recognizes little endian unsigned 2 bytes integer +#[inline] +pub fn le_u16(i: &[u8]) -> IResult<&[u8], u16> { + if i.len() < 2 { + Incomplete(Needed::Size(2)) + } else { + let res = ((i[1] as u16) << 8) + i[0] as u16; + Done(&i[2..], res) + } +} + +/// Recognizes little endian unsigned 4 bytes integer +#[inline] +pub fn le_u32(i: &[u8]) -> IResult<&[u8], u32> { + if i.len() < 4 { + Incomplete(Needed::Size(4)) + } else { + let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32; + Done(&i[4..], res) + } +} + +/// Recognizes little endian unsigned 8 bytes integer +#[inline] +pub fn le_u64(i: &[u8]) -> IResult<&[u8], u64> { + if i.len() < 8 { + Incomplete(Needed::Size(8)) + } else { + let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + + ((i[3] as u64) << 24) + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64; + Done(&i[8..], res) + } +} + +/// Recognizes a signed 1 byte integer (equivalent to take!(1) +#[inline] +pub fn le_i8(i:&[u8]) -> IResult<&[u8], i8> { + map!(i, le_u8, | x | { x as i8 }) +} + +/// Recognizes little endian signed 2 bytes integer +#[inline] +pub fn le_i16(i:&[u8]) -> IResult<&[u8], i16> { + map!(i, le_u16, | x | { x as i16 }) +} + +/// Recognizes little endian signed 4 bytes integer +#[inline] +pub fn le_i32(i:&[u8]) -> IResult<&[u8], i32> { + map!(i, le_u32, | x | { x as i32 }) +} + +/// Recognizes little endian signed 8 bytes integer +#[inline] +pub fn le_i64(i:&[u8]) -> IResult<&[u8], i64> { + map!(i, le_u64, | x | { x as i64 }) +} + +/// if parameter is true, parse a big endian u16 integer, +/// otherwise a little endian u16 integer +#[macro_export] +macro_rules! u16 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_u16($i) } else { $crate::le_u16($i) } } );); +/// if parameter is true, parse a big endian u32 integer, +/// otherwise a little endian u32 integer +#[macro_export] +macro_rules! u32 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_u32($i) } else { $crate::le_u32($i) } } );); +/// if parameter is true, parse a big endian u64 integer, +/// otherwise a little endian u64 integer +#[macro_export] +macro_rules! u64 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_u64($i) } else { $crate::le_u64($i) } } );); + +/// if parameter is true, parse a big endian i16 integer, +/// otherwise a little endian i16 integer +#[macro_export] +macro_rules! i16 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_i16($i) } else { $crate::le_i16($i) } } );); +/// if parameter is true, parse a big endian i32 integer, +/// otherwise a little endian i32 integer +#[macro_export] +macro_rules! i32 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_i32($i) } else { $crate::le_i32($i) } } );); +/// if parameter is true, parse a big endian i64 integer, +/// otherwise a little endian i64 integer +#[macro_export] +macro_rules! i64 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_i64($i) } else { $crate::le_i64($i) } } );); + +/// Recognizes big endian 4 bytes floating point number +#[inline] +pub fn be_f32(input: &[u8]) -> IResult<&[u8], f32> { + match be_u32(input) { + Error(e) => Error(e), + Incomplete(e) => Incomplete(e), + Done(i,o) => { + unsafe { + Done(i, transmute::(o)) + } + } + } +} + +/// Recognizes big endian 8 bytes floating point number +#[inline] +pub fn be_f64(input: &[u8]) -> IResult<&[u8], f64> { + match be_u64(input) { + Error(e) => Error(e), + Incomplete(e) => Incomplete(e), + Done(i,o) => { + unsafe { + Done(i, transmute::(o)) + } + } + } +} + +/// Recognizes little endian 4 bytes floating point number +#[inline] +pub fn le_f32(input: &[u8]) -> IResult<&[u8], f32> { + match le_u32(input) { + Error(e) => Error(e), + Incomplete(e) => Incomplete(e), + Done(i,o) => { + unsafe { + Done(i, transmute::(o)) + } + } + } +} + +/// Recognizes little endian 8 bytes floating point number +#[inline] +pub fn le_f64(input: &[u8]) -> IResult<&[u8], f64> { + match le_u64(input) { + Error(e) => Error(e), + Incomplete(e) => Incomplete(e), + Done(i,o) => { + unsafe { + Done(i, transmute::(o)) + } + } + } +} + +/// Recognizes a hex-encoded integer +#[inline] +pub fn hex_u32(input: &[u8]) -> IResult<&[u8], u32> { + match is_a!(input, &b"0123456789abcdef"[..]) { + Error(e) => Error(e), + Incomplete(e) => Incomplete(e), + Done(i,o) => { + let mut res = 0u32; + + // Do not parse more than 8 characters for a u32 + let mut remaining = i; + let mut parsed = o; + if o.len() > 8 { + remaining = &input[8..]; + parsed = &input[..8]; + } + + for &e in parsed { + let digit = e as char; + let value = digit.to_digit(16).unwrap_or(0); + res = value + (res << 4); + } + Done(remaining, res) + } + } +} + +/// Recognizes empty input buffers +/// +/// useful to verify that the previous parsers used all of the input +#[inline] +//pub fn eof(input:&[u8]) -> IResult<&[u8], &[u8]> { +pub fn eof<'a, T:?Sized>(input: &'a T) -> IResult<&'a T,&'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: InputLength { + if input.input_len() == 0 { + Done(input, input) + } else { + Error(Position(ErrorKind::Eof, input)) + } +} + +/// Recognizes non empty buffers +#[inline] +pub fn non_empty<'a, T:?Sized>(input: &'a T) -> IResult<&'a T,&'a T> where + T:Index, Output=T>+Index, Output=T>, + &'a T: InputLength { + if input.input_len() == 0 { + Error(Position(ErrorKind::NonEmpty, input)) + } else { + Done(&input[input.input_len()..], input) + } +} + +/// Return the remaining input. +#[inline] +pub fn rest(input: &[u8]) -> IResult<&[u8], &[u8]> { + IResult::Done(&input[input.len()..], input) +} + +/// Return the remaining input, for strings. +#[inline] +pub fn rest_s(input: &str) -> IResult<&str, &str> { + IResult::Done(&input[input.len()..], input) +} + +#[cfg(test)] +mod tests { + use super::*; + use internal::{Needed,IResult}; + use internal::IResult::*; + use internal::Err::*; + use util::ErrorKind; + + #[test] + fn tag_closure() { + let x = tag_cl(&b"abcd"[..]); + let r = x(&b"abcdabcdefgh"[..]); + assert_eq!(r, Done(&b"abcdefgh"[..], &b"abcd"[..])); + + let r2 = x(&b"abcefgh"[..]); + assert_eq!(r2, Error(Position(ErrorKind::TagClosure, &b"abcefgh"[..]))); + } + + #[test] + fn character() { + let empty: &[u8] = b""; + let a: &[u8] = b"abcd"; + let b: &[u8] = b"1234"; + let c: &[u8] = b"a123"; + let d: &[u8] = "azé12".as_bytes(); + let e: &[u8] = b" "; + assert_eq!(alpha(a), Done(empty, a)); + assert_eq!(alpha(b), Error(Position(ErrorKind::Alpha,b))); + assert_eq!(alpha(c), Done(&c[1..], &b"a"[..])); + assert_eq!(alpha(d), Done("é12".as_bytes(), &b"az"[..])); + assert_eq!(digit(a), Error(Position(ErrorKind::Digit,a))); + assert_eq!(digit(b), Done(empty, b)); + assert_eq!(digit(c), Error(Position(ErrorKind::Digit,c))); + assert_eq!(digit(d), Error(Position(ErrorKind::Digit,d))); + assert_eq!(hex_digit(a), Done(empty, a)); + assert_eq!(hex_digit(b), Done(empty, b)); + assert_eq!(hex_digit(c), Done(empty, c)); + assert_eq!(hex_digit(d), Done("zé12".as_bytes(), &b"a"[..])); + assert_eq!(hex_digit(e), Error(Position(ErrorKind::HexDigit,e))); + assert_eq!(oct_digit(a), Error(Position(ErrorKind::OctDigit,a))); + assert_eq!(oct_digit(b), Done(empty, b)); + assert_eq!(oct_digit(c), Error(Position(ErrorKind::OctDigit,c))); + assert_eq!(oct_digit(d), Error(Position(ErrorKind::OctDigit,d))); + assert_eq!(alphanumeric(a), Done(empty, a)); + assert_eq!(fix_error!(b,(), alphanumeric), Done(empty, b)); + assert_eq!(alphanumeric(c), Done(empty, c)); + assert_eq!(alphanumeric(d), Done("é12".as_bytes(), &b"az"[..])); + assert_eq!(space(e), Done(&b""[..], &b" "[..])); + } + + #[test] + fn character_s() { + let empty = ""; + let a = "abcd"; + let b = "1234"; + let c = "a123"; + let d = "azé12"; + let e = " "; + assert_eq!(alpha(a), Done(empty, a)); + assert_eq!(alpha(b), Error(Position(ErrorKind::Alpha,b))); + assert_eq!(alpha(c), Done(&c[1..], &"a"[..])); + assert_eq!(alpha(d), Done("12", &"azé"[..])); + assert_eq!(digit(a), Error(Position(ErrorKind::Digit,a))); + assert_eq!(digit(b), Done(empty, b)); + assert_eq!(digit(c), Error(Position(ErrorKind::Digit,c))); + assert_eq!(digit(d), Error(Position(ErrorKind::Digit,d))); + assert_eq!(hex_digit(a), Done(empty, a)); + assert_eq!(hex_digit(b), Done(empty, b)); + assert_eq!(hex_digit(c), Done(empty, c)); + assert_eq!(hex_digit(d), Done("zé12", &"a"[..])); + assert_eq!(hex_digit(e), Error(Position(ErrorKind::HexDigit,e))); + assert_eq!(oct_digit(a), Error(Position(ErrorKind::OctDigit,a))); + assert_eq!(oct_digit(b), Done(empty, b)); + assert_eq!(oct_digit(c), Error(Position(ErrorKind::OctDigit,c))); + assert_eq!(oct_digit(d), Error(Position(ErrorKind::OctDigit,d))); + assert_eq!(alphanumeric(a), Done(empty, a)); + assert_eq!(fix_error!(b,(), alphanumeric), Done(empty, b)); + assert_eq!(alphanumeric(c), Done(empty, c)); + assert_eq!(alphanumeric(d), Done("", &"azé12"[..])); + assert_eq!(space(e), Done(&""[..], &" "[..])); + } + + use util::HexDisplay; + #[test] + fn offset() { + let a = &b"abcd"[..]; + let b = &b"1234"[..]; + let c = &b"a123"[..]; + let d = &b" \t"[..]; + let e = &b" \t\r\n"[..]; + let f = &b"123abcDEF"[..]; + + match alpha(a) { + Done(i, _) => { assert_eq!(a.offset(i) + i.len(), a.len()); } + _ => { panic!("wrong return type in offset test for alpha") } + } + match digit(b) { + Done(i, _) => { assert_eq!(b.offset(i) + i.len(), b.len()); } + _ => { panic!("wrong return type in offset test for digit") } + } + match alphanumeric(c) { + Done(i, _) => { assert_eq!(c.offset(i) + i.len(), c.len()); } + _ => { panic!("wrong return type in offset test for alphanumeric") } + } + match space(d) { + Done(i, _) => { assert_eq!(d.offset(i) + i.len(), d.len()); } + _ => { panic!("wrong return type in offset test for space") } + } + match multispace(e) { + Done(i, _) => { assert_eq!(e.offset(i) + i.len(), e.len()); } + _ => { panic!("wrong return type in offset test for multispace") } + } + match hex_digit(f) { + Done(i, _) => { assert_eq!(f.offset(i) + i.len(), f.len()); } + _ => { panic!("wrong return type in offset test for hex_digit") } + } + match oct_digit(f) { + Done(i, _) => { assert_eq!(f.offset(i) + i.len(), f.len()); } + _ => { panic!("wrong return type in offset test for oct_digit") } + } + } + + #[test] + fn is_not() { + let a: &[u8] = b"ab12cd\nefgh"; + assert_eq!(not_line_ending(a), Done(&b"\nefgh"[..], &b"ab12cd"[..])); + + let b: &[u8] = b"ab12cd\nefgh\nijkl"; + assert_eq!(not_line_ending(b), Done(&b"\nefgh\nijkl"[..], &b"ab12cd"[..])); + + let c: &[u8] = b"ab12cd"; + assert_eq!(not_line_ending(c), Done(&b""[..], c)); + } + + #[test] + fn buffer_with_size() { + let i:Vec = vec![7,8]; + let o:Vec = vec![4,5,6]; + //let arr:[u8; 6usize] = [3, 4, 5, 6, 7, 8]; + let arr:[u8; 6usize] = [3, 4, 5, 6, 7, 8]; + let res = sized_buffer(&arr[..]); + assert_eq!(res, Done(&i[..], &o[..])) + } + + /*#[test] + fn t1() { + let v1:Vec = vec![1,2,3]; + let v2:Vec = vec![4,5,6]; + let d = Done(&v1[..], &v2[..]); + let res = d.flat_map(print); + assert_eq!(res, Done(&v2[..], ())); + }*/ + + #[test] + fn length_value_test() { + let i1 = vec![7,8]; + let o1 = vec![4, 5, 6]; + let arr1:[u8; 6usize] = [3, 4, 5, 6, 7, 8]; + let res1 = length_value(&arr1); + assert_eq!(Done(&i1[..], &o1[..]), res1); + + let i2:Vec = vec![4,5,6,7,8]; + let o2: &[u8] = b""; + let arr2:[u8; 6usize] = [0, 4, 5, 6, 7, 8]; + let res2 = length_value(&arr2); + assert_eq!(Done(&i2[..], o2), res2); + + let arr3:[u8; 7usize] = [8, 4, 5, 6, 7, 8, 9]; + let res3 = length_value(&arr3); + //FIXME: should be incomplete + assert_eq!(Incomplete(Needed::Size(9)), res3); + } + + #[test] + fn i8_tests() { + assert_eq!(be_i8(&[0x00]), Done(&b""[..], 0)); + assert_eq!(be_i8(&[0x7f]), Done(&b""[..], 127)); + assert_eq!(be_i8(&[0xff]), Done(&b""[..], -1)); + assert_eq!(be_i8(&[0x80]), Done(&b""[..], -128)); + } + + #[test] + fn i16_tests() { + assert_eq!(be_i16(&[0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(be_i16(&[0x7f, 0xff]), Done(&b""[..], 32767_i16)); + assert_eq!(be_i16(&[0xff, 0xff]), Done(&b""[..], -1)); + assert_eq!(be_i16(&[0x80, 0x00]), Done(&b""[..], -32768_i16)); + } + + #[test] + fn i32_tests() { + assert_eq!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(be_i32(&[0x7f, 0xff, 0xff, 0xff]), Done(&b""[..], 2147483647_i32)); + assert_eq!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Done(&b""[..], -1)); + assert_eq!(be_i32(&[0x80, 0x00, 0x00, 0x00]), Done(&b""[..], -2147483648_i32)); + } + + #[test] + fn i64_tests() { + assert_eq!(be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), Done(&b""[..], 9223372036854775807_i64)); + assert_eq!(be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), Done(&b""[..], -1)); + assert_eq!(be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), Done(&b""[..], -9223372036854775808_i64)); + } + + #[test] + fn le_i8_tests() { + assert_eq!(le_i8(&[0x00]), Done(&b""[..], 0)); + assert_eq!(le_i8(&[0x7f]), Done(&b""[..], 127)); + assert_eq!(le_i8(&[0xff]), Done(&b""[..], -1)); + assert_eq!(le_i8(&[0x80]), Done(&b""[..], -128)); + } + + #[test] + fn le_i16_tests() { + assert_eq!(le_i16(&[0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(le_i16(&[0xff, 0x7f]), Done(&b""[..], 32767_i16)); + assert_eq!(le_i16(&[0xff, 0xff]), Done(&b""[..], -1)); + assert_eq!(le_i16(&[0x00, 0x80]), Done(&b""[..], -32768_i16)); + } + + #[test] + fn le_i32_tests() { + assert_eq!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(le_i32(&[0xff, 0xff, 0xff, 0x7f]), Done(&b""[..], 2147483647_i32)); + assert_eq!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Done(&b""[..], -1)); + assert_eq!(le_i32(&[0x00, 0x00, 0x00, 0x80]), Done(&b""[..], -2147483648_i32)); + } + + #[test] + fn le_i64_tests() { + assert_eq!(le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), Done(&b""[..], 9223372036854775807_i64)); + assert_eq!(le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), Done(&b""[..], -1)); + assert_eq!(le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), Done(&b""[..], -9223372036854775808_i64)); + } + + #[test] + fn be_f32_tests() { + assert_eq!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0_f32)); + assert_eq!(be_f32(&[0x4d, 0x31, 0x1f, 0xd8]), Done(&b""[..], 185728392_f32)); + } + + #[test] + fn be_f64_tests() { + assert_eq!(be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0_f64)); + assert_eq!(be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]), Done(&b""[..], 185728392_f64)); + } + + #[test] + fn le_f32_tests() { + assert_eq!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0_f32)); + assert_eq!(le_f32(&[0xd8, 0x1f, 0x31, 0x4d]), Done(&b""[..], 185728392_f32)); + } + + #[test] + fn le_f64_tests() { + assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0_f64)); + assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]), Done(&b""[..], 185728392_f64)); + } + + #[test] + fn hex_u32_tests() { + assert_eq!(hex_u32(&b""[..]), Done(&b""[..], 0)); + assert_eq!(hex_u32(&b"ff"[..]), Done(&b""[..], 255)); + assert_eq!(hex_u32(&b"1be2"[..]), Done(&b""[..], 7138)); + assert_eq!(hex_u32(&b"c5a31be2"[..]), Done(&b""[..], 3315801058)); + assert_eq!(hex_u32(&b"00c5a31be2"[..]), Done(&b"e2"[..], 12952347)); + assert_eq!(hex_u32(&b"c5a31be201"[..]), Done(&b"01"[..], 3315801058)); + assert_eq!(hex_u32(&b"ffffffff"[..]), Done(&b""[..], 4294967295)); + assert_eq!(hex_u32(&b"0x1be2"[..]), Done(&b"x1be2"[..], 0)); + } + + #[test] + fn end_of_input() { + let not_over = &b"Hello, world!"[..]; + let is_over = &b""[..]; + + let res_not_over = eof(not_over); + assert_eq!(res_not_over, Error(Position(ErrorKind::Eof, not_over))); + + let res_over = eof(is_over); + assert_eq!(res_over, Done(is_over, is_over)); + } + + #[test] + fn configurable_endianness() { + named!(be_tst16, u16!(true)); + named!(le_tst16, u16!(false)); + assert_eq!(be_tst16(&[0x80, 0x00]), Done(&b""[..], 32768_u16)); + assert_eq!(le_tst16(&[0x80, 0x00]), Done(&b""[..], 128_u16)); + + named!(be_tst32, u32!(true)); + named!(le_tst32, u32!(false)); + assert_eq!(be_tst32(&[0x12, 0x00, 0x60, 0x00]), Done(&b""[..], 302014464_u32)); + assert_eq!(le_tst32(&[0x12, 0x00, 0x60, 0x00]), Done(&b""[..], 6291474_u32)); + + named!(be_tst64, u64!(true)); + named!(le_tst64, u64!(false)); + assert_eq!(be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 1297142246100992000_u64)); + assert_eq!(le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 36028874334666770_u64)); + + named!(be_tsti16, i16!(true)); + named!(le_tsti16, i16!(false)); + assert_eq!(be_tsti16(&[0x00, 0x80]), Done(&b""[..], 128_i16)); + assert_eq!(le_tsti16(&[0x00, 0x80]), Done(&b""[..], -32768_i16)); + + named!(be_tsti32, i32!(true)); + named!(le_tsti32, i32!(false)); + assert_eq!(be_tsti32(&[0x00, 0x12, 0x60, 0x00]), Done(&b""[..], 1204224_i32)); + assert_eq!(le_tsti32(&[0x00, 0x12, 0x60, 0x00]), Done(&b""[..], 6296064_i32)); + + named!(be_tsti64, i64!(true)); + named!(le_tsti64, i64!(false)); + assert_eq!(be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 71881672479506432_i64)); + assert_eq!(le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 36028874334732032_i64)); + + } + + #[test] + fn manual_configurable_endianness_test() { + let x = 1; + let int_parse: Box IResult<&[u8], u16> > = if x == 2 { + Box::new(be_u16) + } else { + Box::new(le_u16) + }; + println!("{:?}", int_parse(&b"3"[..])); + assert_eq!(int_parse(&[0x80, 0x00]), Done(&b""[..], 128_u16)); + } + + #[allow(dead_code)] + fn custom_error(input: &[u8]) -> IResult<&[u8], &[u8], ()> { + fix_error!(input, (), alphanumeric) + } + + #[test] + fn hex_digit_test() { + let empty = &b""[..]; + + let i = &b"0123456789abcdefABCDEF"[..]; + assert_eq!(hex_digit(i), Done(empty, i)); + + let i = &b"g"[..]; + assert_eq!(hex_digit(i), Error(Position(ErrorKind::HexDigit,i))); + + let i = &b"G"[..]; + assert_eq!(hex_digit(i), Error(Position(ErrorKind::HexDigit,i))); + + assert!(is_hex_digit(b'0')); + assert!(is_hex_digit(b'9')); + assert!(is_hex_digit(b'a')); + assert!(is_hex_digit(b'f')); + assert!(is_hex_digit(b'A')); + assert!(is_hex_digit(b'F')); + assert!(!is_hex_digit(b'g')); + assert!(!is_hex_digit(b'G')); + assert!(!is_hex_digit(b'/')); + assert!(!is_hex_digit(b':')); + assert!(!is_hex_digit(b'@')); + assert!(!is_hex_digit(b'\x60')); + } + + #[test] + fn oct_digit_test() { + let empty = &b""[..]; + + let i = &b"01234567"[..]; + assert_eq!(oct_digit(i), Done(empty, i)); + + let i = &b"8"[..]; + assert_eq!(oct_digit(i), Error(Position(ErrorKind::OctDigit,i))); + + assert!(is_oct_digit(b'0')); + assert!(is_oct_digit(b'7')); + assert!(!is_oct_digit(b'8')); + assert!(!is_oct_digit(b'9')); + assert!(!is_oct_digit(b'a')); + assert!(!is_oct_digit(b'A')); + assert!(!is_oct_digit(b'/')); + assert!(!is_oct_digit(b':')); + assert!(!is_oct_digit(b'@')); + assert!(!is_oct_digit(b'\x60')); + } +} diff --git third_party/rust/nom-1.2.4/src/regexp.rs third_party/rust/nom-1.2.4/src/regexp.rs new file mode 100644 index 000000000000..17f8ede1a3f7 --- /dev/null +++ third_party/rust/nom-1.2.4/src/regexp.rs @@ -0,0 +1,644 @@ +#[doc(hidden)] +#[macro_export] +macro_rules! regex ( + ($re: ident, $s:expr) => ( + lazy_static! { + static ref $re: ::regex::Regex = ::regex::Regex::new($s).unwrap(); + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! regex_bytes ( + ($re: ident, $s:expr) => ( + lazy_static! { + static ref $re: ::regex::bytes::Regex = ::regex::bytes::Regex::new($s).unwrap(); + } + ); +); + + +/// `re_match!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_match ( + ($i:expr, $re:expr) => ( + { + use $crate::InputLength; + let re = ::regex::Regex::new($re).unwrap(); + if re.is_match($i) { + $crate::IResult::Done(&$i[$i.input_len()..], $i) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_match_static ( + ($i:expr, $re:expr) => ( + { + use $crate::InputLength; + regex!(RE, $re); + if RE.is_match($i) { + $crate::IResult::Done(&$i[$i.input_len()..], $i) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + } + } + ) +); + +/// `re_bytes_match!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_bytes_match ( + ($i:expr, $re:expr) => ( + { + use $crate::InputLength; + let re = ::regex::bytes::Regex::new($re).unwrap(); + if re.is_match($i) { + $crate::IResult::Done(&$i[$i.input_len()..], $i) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_bytes_match_static ( + ($i:expr, $re:expr) => ( + { + use $crate::InputLength; + regex_bytes!(RE, $re); + if RE.is_match($i) { + $crate::IResult::Done(&$i[$i.input_len()..], $i) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + } + } + ) +); + +/// `re_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_find ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::Regex::new($re).unwrap(); + if let Some((begin, end)) = re.find($i) { + $crate::IResult::Done(&$i[end..], &$i[begin..end]) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_find_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_find_static ( + ($i:expr, $re:expr) => ( + { + regex!(RE, $re); + if let Some((begin, end)) = RE.find($i) { + $crate::IResult::Done(&$i[end..], &$i[begin..end]) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + } + } + + ) +); + +/// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_bytes_find ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::bytes::Regex::new($re).unwrap(); + if let Some((begin, end)) = re.find($i) { + $crate::IResult::Done(&$i[end..], &$i[begin..end]) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_bytes_find_static ( + ($i:expr, $re:expr) => ( + { + regex_bytes!(RE, $re); + if let Some((begin, end)) = RE.find($i) { + $crate::IResult::Done(&$i[end..], &$i[begin..end]) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + } + } + + ) +); + +/// `re_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_matches ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::Regex::new($re).unwrap(); + let v: Vec<&str> = re.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_matches_static ( + ($i:expr, $re:expr) => ( + { + regex!(RE, $re); + let v: Vec<&str> = RE.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + } + } + ) +); + +/// `re_bytes_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_bytes_matches ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::bytes::Regex::new($re).unwrap(); + let v: Vec<&[u8]> = re.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_bytes_matches_static ( + ($i:expr, $re:expr) => ( + { + regex_bytes!(RE, $re); + let v: Vec<&[u8]> = RE.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + } + } + ) +); + +/// `re_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_capture ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::Regex::new($re).unwrap(); + if let Some(c) = re.captures($i) { + let v:Vec<&str> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_capture_static ( + ($i:expr, $re:expr) => ( + { + regex!(RE, $re); + if let Some(c) = RE.captures($i) { + let v:Vec<&str> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +/// `re_bytes_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_bytes_capture ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::bytes::Regex::new($re).unwrap(); + if let Some(c) = re.captures($i) { + let v:Vec<&[u8]> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_bytes_capture_static ( + ($i:expr, $re:expr) => ( + { + regex_bytes!(RE, $re); + if let Some(c) = RE.captures($i) { + let v:Vec<&[u8]> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +/// `re_captures!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_captures ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::Regex::new($re).unwrap(); + let v:Vec> = re.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_captures_static!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_captures_static ( + ($i:expr, $re:expr) => ( + { + regex!(RE, $re); + let v:Vec> = RE.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +/// `re_bytes_captures!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups +/// +/// requires the `regexp` feature +#[macro_export] +macro_rules! re_bytes_captures ( + ($i:expr, $re:expr) => ( + { + let re = ::regex::bytes::Regex::new($re).unwrap(); + let v:Vec> = re.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_captures_static!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export] +macro_rules! re_bytes_captures_static ( + ($i:expr, $re:expr) => ( + { + regex_bytes!(RE, $re); + let v:Vec> = RE.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + $crate::IResult::Done(&$i[offset..], v) + } else { + $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + } + } + ) +); +#[cfg(test)] +mod tests { + use internal::IResult::*; + use internal::Err::*; + use util::ErrorKind; + + #[test] + fn re_match() { + named!(rm<&str,&str>, re_match!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm("2015-09-07blah"), Done("", "2015-09-07blah")); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_match_static() { + named!(rm<&str,&str>, re_match_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm("2015-09-07blah"), Done("", "2015-09-07blah")); + } + + #[test] + fn re_find() { + named!(rm<&str,&str>, re_find!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm("2015-09-07blah"), Done("blah", "2015-09-07")); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_find_static() { + named!(rm<&str,&str>, re_find_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm("2015-09-07blah"), Done("blah", "2015-09-07")); + } + + #[test] + fn re_matches() { + named!(rm< &str,Vec<&str> >, re_matches!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Done("", vec!["2015-09-07"])); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"), Done("pouet", vec!["2015-09-07", "2015-09-09"])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_matches_static() { + named!(rm< &str,Vec<&str> >, re_matches_static!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Done("", vec!["2015-09-07"])); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"), Done("pouet", vec!["2015-09-07", "2015-09-09"])); + } + + #[test] + fn re_capture() { + named!(rm< &str,Vec<&str> >, re_capture!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41"), Done(" world regex 0.1.41", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_capture_static() { + named!(rm< &str,Vec<&str> >, re_capture_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41"), Done(" world regex 0.1.41", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); + } + + #[test] + fn re_captures() { + named!(rm< &str,Vec> >, re_captures!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]])); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41 aaa"), Done(" aaa", vec![ + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], + vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], + ])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_captures_static() { + named!(rm< &str,Vec> >, re_captures_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]])); + assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41 aaa"), Done(" aaa", vec![ + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], + vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], + ])); + } + + #[test] + fn re_bytes_match() { + named!(rm, re_bytes_match!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b""[..], &b"2015-09-07blah"[..])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_match_static() { + named!(rm, re_bytes_match_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b""[..], &b"2015-09-07blah"[..])); + } + + #[test] + fn re_bytes_find() { + named!(rm, re_bytes_find!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b"blah"[..], &b"2015-09-07"[..])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_find_static() { + named!(rm, re_bytes_find_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b"blah"[..], &b"2015-09-07"[..])); + } + + #[test] + fn re_bytes_matches() { + named!(rm >, re_bytes_matches!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], vec![&b"2015-09-07"[..]])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), Done(&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_matches_static() { + named!(rm >, re_bytes_matches_static!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], vec![&b"2015-09-07"[..]])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), Done(&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])); + } + + #[test] + fn re_bytes_capture() { + named!(rm >, re_bytes_capture!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), Done(&b" world regex 0.1.41"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_capture_static() { + named!(rm< Vec<&[u8]> >, re_bytes_capture_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), Done(&b" world regex 0.1.41"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); + } + + #[test] + fn re_bytes_captures() { + named!(rm< Vec> >, re_bytes_captures!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]]])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), Done(&b" aaa"[..], vec![ + vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]], + vec![&b"regex 0.1.41"[..], &b"regex"[..], &b"0.1.41"[..], &b"0"[..], &b"1"[..], &b"41"[..]], + ])); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_captures_static() { + named!(rm< Vec> >, re_bytes_captures_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]]])); + assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), Done(&b" aaa"[..], vec![ + vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]], + vec![&b"regex 0.1.41"[..], &b"regex"[..], &b"0.1.41"[..], &b"0"[..], &b"1"[..], &b"41"[..]], + ])); + } +} diff --git third_party/rust/nom-1.2.4/src/str.rs third_party/rust/nom-1.2.4/src/str.rs new file mode 100644 index 000000000000..768786edaba1 --- /dev/null +++ third_party/rust/nom-1.2.4/src/str.rs @@ -0,0 +1,734 @@ +//! Parsers and helper functions operating on strings, especially useful when writing parsers for +//! text-based formats. + +/// `tag_s!(&str) => &str -> IResult<&str, &str>` +/// declares a string as a suite to recognize +/// +/// consumes the recognized characters +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self,Done}; +/// # fn main() { +/// fn test(input: &str) -> IResult<&str, &str> { +/// tag_s!(input, "abcd") +/// } +/// let r = test("abcdefgh"); +/// assert_eq!(r, Done("efgh", "abcd")); +/// # } +/// ``` +#[macro_export] +macro_rules! tag_s ( + ($i:expr, $tag: expr) => ( + { + let res: $crate::IResult<_,_> = if $tag.len() > $i.len() { + $crate::IResult::Incomplete($crate::Needed::Size($tag.len())) + //} else if &$i[0..$tag.len()] == $tag { + } else if ($i).starts_with($tag) { + $crate::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagStr, $i)) + }; + res + } + ); +); + +/// `take_s!(nb) => &str -> IResult<&str, &str>` +/// generates a parser consuming the specified number of characters +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// // Desmond parser +/// named!(take5<&str,&str>, take_s!( 5 ) ); +/// +/// let a = "abcdefgh"; +/// +/// assert_eq!(take5(a), Done("fgh", "abcde")); +/// # } +/// ``` +#[macro_export] +macro_rules! take_s ( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res: $crate::IResult<_,_> = if $i.chars().count() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + let mut offset = $i.len(); + let mut count = 0; + for (o, _) in $i.char_indices() { + if count == cnt { + offset = o; + break; + } + count += 1; + } + $crate::IResult::Done(&$i[offset..], &$i[..offset]) + }; + res + } + ); +); + + +/// `is_not_s!(&str) => &str -> IResult<&str, &str>` +/// returns the longest list of characters that do not appear in the provided array +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!( not_space<&str,&str>, is_not_s!( " \t\r\n" ) ); +/// +/// let r = not_space("abcdefgh\nijkl"); +/// assert_eq!(r, Done("\nijkl", "abcdefgh")); +/// # } +/// ``` +#[macro_export] +macro_rules! is_not_s ( + ($input:expr, $arr:expr) => ( + { + use std::collections::HashSet; + let set: HashSet = $arr.chars().collect(); + let mut offset = $input.len(); + for (o, c) in $input.char_indices() { + if set.contains(&c) { + offset = o; + break; + } + } + if offset == 0 { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsAStr,$input)) + } else if offset < $input.len() { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Done("", $input) + } + } + ); +); + +/// `is_a_s!(&str) => &str -> IResult<&str, &str>` +/// returns the longest list of characters that appear in the provided array +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(abcd<&str, &str>, is_a_s!( "abcd" )); +/// +/// let r1 = abcd("aaaaefgh"); +/// assert_eq!(r1, Done("efgh", "aaaa")); +/// +/// let r2 = abcd("dcbaefgh"); +/// assert_eq!(r2, Done("efgh", "dcba")); +/// # } +/// ``` +#[macro_export] +macro_rules! is_a_s ( + ($input:expr, $arr:expr) => ( + { + use std::collections::HashSet; + let set: HashSet = $arr.chars().collect(); + let mut offset = $input.len(); + for (o, c) in $input.char_indices() { + if !set.contains(&c) { + offset = o; + break; + } + } + if offset == 0 { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsAStr,$input)) + } else if offset < $input.len() { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Done("", $input) + } + } + ); +); + + +/// `take_while_s!(char -> bool) => &str -> IResult<&str, &str>` +/// returns the longest list of characters until the provided function fails. +/// +/// The argument is either a function `char -> bool` or a macro returning a `bool +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::is_alphanumeric; +/// # fn main() { +/// fn alphabetic(chr: char) -> bool { (chr >= 0x41 as char && chr <= 0x5A as char) || (chr >= 0x61 as char && chr <= 0x7A as char) } +/// named!( alpha<&str,&str>, take_while_s!( alphabetic ) ); +/// +/// let r = alpha("abcd\nefgh"); +/// assert_eq!(r, Done("\nefgh", "abcd")); +/// # } +/// ``` +#[macro_export] +macro_rules! take_while_s ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + let mut offset = $input.len(); + for (o, c) in $input.char_indices() { + if !$submac!(c, $($args)*) { + offset = o; + break; + } + } + if offset < $input.len() { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Done("", $input) + } + } + ); + ($input:expr, $f:expr) => ( + take_while_s!($input, call!($f)); + ); +); + +/// `take_while1_s!(char -> bool) => &str -> IResult<&str, &str>` +/// returns the longest (non empty) list of characters until the provided function fails. +/// +/// The argument is either a function `char -> bool` or a macro returning a `bool` +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::is_alphanumeric; +/// # fn main() { +/// fn alphabetic(chr: char) -> bool { (chr >= 0x41 as char && chr <= 0x5A as char) || (chr >= 0x61 as char && chr <= 0x7A as char) } +/// named!( alpha<&str,&str>, take_while1_s!( alphabetic ) ); +/// +/// let r = alpha("abcd\nefgh"); +/// assert_eq!(r, Done("\nefgh", "abcd")); +/// # } +/// ``` +#[macro_export] +macro_rules! take_while1_s ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + let mut offset = $input.len(); + for (o, c) in $input.char_indices() { + if !$submac!(c, $($args)*) { + offset = o; + break; + } + } + if offset == 0 { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeWhile1Str,$input)) + } else if offset < $input.len() { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Done("", $input) + } + } + ); + ($input:expr, $f:expr) => ( + take_while1_s!($input, call!($f)); + ); +); + + +/// `take_till_s!(&str -> bool) => &str -> IResult<&str, &str>` +/// returns the longest list of characters until the provided function succeeds +/// +/// The argument is either a function `char -> bool` or a macro returning a `bool +#[macro_export] +macro_rules! take_till_s ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + + { + let mut offset = $input.len(); + for (o, c) in $input.char_indices() { + if $submac!(c, $($args)*) { + offset = o; + break; + } + } + if offset < $input.len() { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Done("", $input) + } + } + ); + ($input:expr, $f:expr) => ( + take_till_s!($input, call!($f)); + ); +); + +/// `take_until_and_consume_s!(&str) => &str -> IResult<&str, &str>` +/// generates a parser consuming all chars until the specified string is found and consumes it +#[macro_export] +macro_rules! take_until_and_consume_s ( + ($input:expr, $substr:expr) => ( + { + #[inline(always)] + fn shift_window_and_cmp(window: & mut ::std::vec::Vec, c: char, substr_vec: & ::std::vec::Vec) -> bool { + window.push(c); + if window.len() > substr_vec.len() { + window.remove(0); + } + window == substr_vec + } + let res: $crate::IResult<_, _> = if $substr.len() > $input.len() { + $crate::IResult::Incomplete($crate::Needed::Size($substr.len())) + } else { + let substr_vec: ::std::vec::Vec = $substr.chars().collect(); + let mut window: ::std::vec::Vec = vec![]; + let mut offset = $input.len(); + let mut parsed = false; + for (o, c) in $input.char_indices() { + if parsed { + // The easiest way to get the byte offset of the char after the found string + offset = o; + break; + } + if shift_window_and_cmp(& mut window, c, &substr_vec) { + parsed = true; + } + } + if parsed { + if offset < $input.len() { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Done("", $input) + } + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilAndConsumeStr,$input)) + } + }; + res + } + ); +); + +/// `take_until_s!(&str) => &str -> IResult<&str, &str>` +/// generates a parser consuming all chars until the specified string is found and leaves it in the remaining input +#[macro_export] +macro_rules! take_until_s ( + ($input:expr, $substr:expr) => ( + { + #[inline(always)] + fn shift_window_and_cmp(window: & mut Vec, c: char, substr_vec: &Vec) -> bool { + window.push(c); + if window.len() > substr_vec.len() { + window.remove(0); + } + window == substr_vec + } + let res: $crate::IResult<&str, &str> = if $substr.len() > $input.len() { + $crate::IResult::Incomplete($crate::Needed::Size($substr.len())) + } else { + let substr_vec: Vec = $substr.chars().collect(); + let mut window: Vec = vec![]; + let mut offset = $input.len(); + let mut parsed = false; + for (o, c) in $input.char_indices() { + if shift_window_and_cmp(& mut window, c, &substr_vec) { + parsed = true; + window.pop(); + let window_len: usize = window.iter() + .map(|x| x.len_utf8()) + .fold(0, |x, y| x + y); + offset = o - window_len; + break; + } + } + if parsed { + $crate::IResult::Done(&$input[offset..], &$input[..offset]) + } else { + $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilStr,$input)) + } + }; + res + } + ); +); + +#[cfg(test)] +mod test { + use ::IResult; + + #[test] + fn tag_str_succeed() { + const INPUT: &'static str = "Hello World!"; + const TAG: &'static str = "Hello"; + fn test(input: &str) -> IResult<&str, &str> { + tag_s!(input, TAG) + } + + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == " World!", "Parser `tag_s` consumed leftover input."); + assert!(output == TAG, + "Parser `tag_s` doesn't return the tag it matched on success. \ + Expected `{}`, got `{}`.", TAG, output); + }, + other => panic!("Parser `tag_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn tag_str_incomplete() { + const INPUT: &'static str = "Hello"; + const TAG: &'static str = "Hello World!"; + + match tag_s!(INPUT, TAG) { + IResult::Incomplete(_) => (), + other => { + panic!("Parser `tag_s` didn't require more input when it should have. \ + Got `{:?}`.", other); + } + }; + } + + #[test] + fn tag_str_error() { + const INPUT: &'static str = "Hello World!"; + const TAG: &'static str = "Random"; // TAG must be closer than INPUT. + + match tag_s!(INPUT, TAG) { + IResult::Error(_) => (), + other => { + panic!("Parser `tag_s` didn't fail when it should have. Got `{:?}`.`", other); + }, + }; + } + + #[test] + fn take_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + + match take_s!(INPUT, 9) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_s` consumed leftover input. Leftover `{}`.", extra); + assert!(output == CONSUMED, + "Parser `take_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, output); + }, + other => panic!("Parser `take_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_until_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇ∂áƒƭèř"; + const FIND: &'static str = "ÂßÇ∂"; + const CONSUMED: &'static str = "βèƒôřè"; + const LEFTOVER: &'static str = "ÂßÇ∂áƒƭèř"; + + match take_until_s!(INPUT, FIND) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_until_s`\ + consumed leftover input. Leftover `{}`.", extra); + assert!(output == CONSUMED, "Parser `take_until_s`\ + doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, output); + } + other => panic!("Parser `take_until_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_s_incomplete() { + const INPUT: &'static str = "βèƒôřèÂßÇá"; + + match take_s!(INPUT, 13) { + IResult::Incomplete(_) => (), + other => panic!("Parser `take_s` didn't require more input when it should have. \ + Got `{:?}`.", other), + } + } + + use internal::IResult::{Done, Error}; + use internal::Err::Position; + use util::ErrorKind; + + pub fn is_alphabetic(c:char) -> bool { + (c as u8 >= 0x41 && c as u8 <= 0x5A) || (c as u8 >= 0x61 && c as u8 <= 0x7A) + } + #[test] + fn take_while_s() { + named!(f<&str,&str>, take_while_s!(is_alphabetic)); + let a = ""; + let b = "abcd"; + let c = "abcd123"; + let d = "123"; + + assert_eq!(f(&a[..]), Done(&a[..], &a[..])); + assert_eq!(f(&b[..]), Done(&a[..], &b[..])); + assert_eq!(f(&c[..]), Done(&d[..], &b[..])); + assert_eq!(f(&d[..]), Done(&d[..], &a[..])); + } + + #[test] + fn take_while1_s() { + named!(f<&str,&str>, take_while1_s!(is_alphabetic)); + let a = ""; + let b = "abcd"; + let c = "abcd123"; + let d = "123"; + + assert_eq!(f(&a[..]), Error(Position(ErrorKind::TakeWhile1Str, &""[..]))); + assert_eq!(f(&b[..]), Done(&a[..], &b[..])); + assert_eq!(f(&c[..]), Done(&"123"[..], &b[..])); + assert_eq!(f(&d[..]), Error(Position(ErrorKind::TakeWhile1Str, &d[..]))); + } + + #[test] + fn take_till_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + fn till_s(c: char) -> bool { + c == 'á' + } + fn test(input: &str) -> IResult<&str, &str> { + take_till_s!(input, till_s) + } + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_till_s` consumed leftover input."); + assert!(output == CONSUMED, + "Parser `take_till_s` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", CONSUMED, output); + }, + other => panic!("Parser `take_till_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_while_s_succeed_none() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &'static str = ""; + const LEFTOVER: &'static str = "βèƒôřèÂßÇáƒƭèř"; + fn while_s(c: char) -> bool { + c == '9' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while_s!(input, while_s) + } + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_while_s` consumed leftover input."); + assert!(output == CONSUMED, + "Parser `take_while_s` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", CONSUMED, output); + }, + other => panic!("Parser `take_while_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn is_not_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const AVOID: &'static str = "£úçƙ¥á"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + fn test(input: &str) -> IResult<&str, &str> { + is_not_s!(input, AVOID) + } + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `is_not_s` consumed leftover input. Leftover `{}`.", extra); + assert!(output == CONSUMED, + "Parser `is_not_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, output); + }, + other => panic!("Parser `is_not_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_until_and_consume_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const FIND: &'static str = "ÂßÇ"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + + match take_until_and_consume_s!(INPUT, FIND) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_until_and_consume_s`\ + consumed leftover input. Leftover `{}`.", extra); + assert!(output == CONSUMED, "Parser `take_until_and_consume_s`\ + doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, output); + } + other => panic!("Parser `take_until_and_consume_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_while_s_succeed_some() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + fn while_s(c: char) -> bool { + c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || + c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while_s!(input, while_s) + } + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_while_s` consumed leftover input."); + assert!(output == CONSUMED, + "Parser `take_while_s` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", CONSUMED, output); + }, + other => panic!("Parser `take_while_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn is_not_s_fail() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const AVOID: &'static str = "βúçƙ¥"; + fn test(input: &str) -> IResult<&str, &str> { + is_not_s!(input, AVOID) + } + match test(INPUT) { + IResult::Error(_) => (), + other => panic!("Parser `is_not_s` didn't fail when it should have. Got `{:?}`.", other), + }; + } + + #[test] + fn take_while1_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + fn while1_s(c: char) -> bool { + c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || + c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while1_s!(input, while1_s) + } + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `take_while1_s` consumed leftover input."); + assert!(output == CONSUMED, + "Parser `take_while1_s` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", CONSUMED, output); + }, + other => panic!("Parser `take_while1_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_until_and_consume_s_incomplete() { + const INPUT: &'static str = "βèƒôřè"; + const FIND: &'static str = "βèƒôřèÂßÇ"; + + match take_until_and_consume_s!(INPUT, FIND) { + IResult::Incomplete(_) => (), + other => panic!("Parser `take_until_and_consume_s` didn't require more input when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_until_s_incomplete() { + const INPUT: &'static str = "βèƒôřè"; + const FIND: &'static str = "βèƒôřèÂßÇ"; + + match take_until_s!(INPUT, FIND) { + IResult::Incomplete(_) => (), + other => panic!("Parser `take_until_s` didn't require more input when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn is_a_s_succeed() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const MATCH: &'static str = "βèƒôřèÂßÇ"; + const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const LEFTOVER: &'static str = "áƒƭèř"; + fn test(input: &str) -> IResult<&str, &str> { + is_a_s!(input, MATCH) + } + match test(INPUT) { + IResult::Done(extra, output) => { + assert!(extra == LEFTOVER, "Parser `is_a_s` consumed leftover input. Leftover `{}`.", extra); + assert!(output == CONSUMED, + "Parser `is_a_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, output); + }, + other => panic!("Parser `is_a_s` didn't succeed when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_while1_s_fail() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + fn while1_s(c: char) -> bool { + c == '9' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while1_s!(input, while1_s) + } + match test(INPUT) { + IResult::Error(_) => (), + other => panic!("Parser `take_while1_s` didn't fail when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn is_a_s_fail() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const MATCH: &'static str = "Ûñℓúçƙ¥"; + fn test(input: &str) -> IResult<&str, &str> { + is_a_s!(input, MATCH) + } + match test(INPUT) { + IResult::Error(_) => (), + other => panic!("Parser `is_a_s` didn't fail when it should have. Got `{:?}`.", other), + }; + } + + #[test] + fn take_until_and_consume_s_error() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const FIND: &'static str = "Ráñδô₥"; + + match take_until_and_consume_s!(INPUT, FIND) { + IResult::Error(_) => (), + other => panic!("Parser `take_until_and_consume_s` didn't fail when it should have. \ + Got `{:?}`.", other), + }; + } + + #[test] + fn take_until_s_error() { + const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; + const FIND: &'static str = "Ráñδô₥"; + + match take_until_s!(INPUT, FIND) { + IResult::Error(_) => (), + other => panic!("Parser `take_until_and_consume_s` didn't fail when it should have. \ + Got `{:?}`.", other), + }; + } +} diff --git third_party/rust/nom-1.2.4/src/stream.rs third_party/rust/nom-1.2.4/src/stream.rs new file mode 100644 index 000000000000..38d5c870c938 --- /dev/null +++ third_party/rust/nom-1.2.4/src/stream.rs @@ -0,0 +1,1031 @@ +/// Context: +/// * Rust does not have tail call optimization, so we cannot recurse wildly +/// * data lifetimes makes sure that the result of a function applied to a producer cannot live longer than the producer's data (unless there is cloning) +/// * previous implementation of Producer and Consumer spent its time copying buffers +/// * the old Consumer was handling everything and buffering data. The new design has the producer handle data, but the consumer makes seeking decision +use std::io::{self,Read,Write,Seek,SeekFrom}; +use std::fs::File; +use std::path::Path; +use std::ptr; +use std::iter::repeat; +use internal::Needed; + +//pub type Computation = Box) -> (I,Consumer)>; + +#[derive(Debug,Clone)] +pub enum Input { + Element(I), + Empty, + Eof(Option) +} + +/// Stores a consumer's current computation state +#[derive(Debug,Clone)] +pub enum ConsumerState { + /// A value of type O has been produced + Done(M,O), + /// An error of type E has been encountered + Error(E), + /// Continue applying, and pass a message of type M to the data source + Continue(M) +} + +impl ConsumerState { + pub fn map(&self, f: F) -> ConsumerState where F: FnOnce(O) -> P { + match *self { + ConsumerState::Error(e) => ConsumerState::Error(e), + ConsumerState::Continue(m) => ConsumerState::Continue(m), + ConsumerState::Done(m, ref o) => ConsumerState::Done(m, f(o.clone())) + } + } + pub fn flat_map(&self, f: F) -> ConsumerState where F: FnOnce(M, O) -> ConsumerState { + match *self { + ConsumerState::Error(e) => ConsumerState::Error(e), + ConsumerState::Continue(m) => ConsumerState::Continue(m), + ConsumerState::Done(m, ref o) => f(m, o.clone()) + } + } +} +/// The Consumer trait wraps a computation and its state +/// +/// it depends on the input type I, the produced value's type O, the error type E, and the message type M +pub trait Consumer { + + /// implement handle for the current computation, returning the new state of the consumer + fn handle(&mut self, input: Input) -> &ConsumerState; + /// returns the current state + fn state(&self) -> &ConsumerState; + +} + +/// The producer wraps a data source, like file or network, and applies a consumer on it +/// +/// it handles buffer copying and reallocation, to provide streaming patterns. +/// it depends on the input type I, and the message type M. +/// the consumer can change the way data is produced (for example, to seek in the source) by sending a message of type M. +pub trait Producer<'b,I,M: 'b> { + /// Applies a consumer once on the produced data, and return the consumer's state + /// + /// a new producer has to implement this method. + /// + /// WARNING: if the `ConsumerState` generated by your consumer has a reference + /// to the input, it will generate borrow checking errors such as + /// `error: cannot borrow `producer` as mutable more than once at a time [E0499]`. + /// + /// It is caused by the producer's ability to refill the input at will, so it can modify + /// the input slice the `ConsumerState` is referring to. + /// + /// To avoid that kind of issue, try to do all the computations on input slices inside the + /// `Consumer` chain + fn apply<'a, O,E>(&'b mut self, consumer: &'a mut Consumer) -> &'a ConsumerState; + + /// Applies a consumer once on the produced data, and returns the generated value if there is one + fn run<'a: 'b,O,E: 'b>(&'b mut self, consumer: &'a mut Consumer) -> Option<&O> { + if let &ConsumerState::Done(_,ref o) = self.apply(consumer) { + Some(o) + } else { + None + } + } + // fn fromFile, FromSocket, fromRead +} + +/// ProducerRepeat takes a single value, and generates it at each step +pub struct ProducerRepeat { + value: I +} + +impl<'b,I:Copy,M: 'b> Producer<'b,I,M> for ProducerRepeat { + fn apply<'a,O,E>(&'b mut self, consumer: &'a mut Consumer) -> &'a ConsumerState { + if { + if let &ConsumerState::Continue(_) = consumer.state() { + true + } else { + false + } + } + { + consumer.handle(Input::Element(self.value)) + } else { + consumer.state() + } + } +} + +/// A MemProducer generates values from an in memory byte buffer +/// +/// it generates data by chunks, and keeps track of how much was consumed. +/// It can receive messages of type `Move` to handle consumption and seeking +pub struct MemProducer<'x> { + buffer: &'x [u8], + chunk_size: usize, + length: usize, + index: usize +} + +impl<'x> MemProducer<'x> { + pub fn new(buffer: &'x[u8], chunk_size: usize) -> MemProducer { + MemProducer { + buffer: buffer, + chunk_size: chunk_size, + length: buffer.len(), + index: 0 + } + } +} + +#[derive(Debug,Clone,Copy,PartialEq,Eq)] +pub enum Move { + /// indcates how much data was consumed + Consume(usize), + /// indicates where in the input the consumer must seek + Seek(SeekFrom), + /// indicates more data is needed + Await(Needed) +} + +impl<'x,'b> Producer<'b,&'x[u8],Move> for MemProducer<'x> { + fn apply<'a,O,E>(&'b mut self, consumer: &'a mut Consumer<&'x[u8],O,E,Move>) -> &'a ConsumerState { + if { + if let &ConsumerState::Continue(ref m) = consumer.state() { + match *m { + Move::Consume(s) => { + if self.length - self.index >= s { + self.index += s + } else { + panic!("cannot consume past the end of the buffer"); + } + }, + Move::Await(a) => { + panic!("not handled for now: await({:?}", a); + } + Move::Seek(SeekFrom::Start(position)) => { + if position as usize > self.length { + self.index = self.length + } else { + self.index = position as usize + } + }, + Move::Seek(SeekFrom::Current(offset)) => { + let next = if offset >= 0 { + (self.index as u64).checked_add(offset as u64) + } else { + (self.index as u64).checked_sub(-offset as u64) + }; + match next { + None => None, + Some(u) => { + if u as usize > self.length { + self.index = self.length + } else { + self.index = u as usize + } + Some(self.index as u64) + } + }; + }, + Move::Seek(SeekFrom::End(i)) => { + let next = if i < 0 { + (self.length as u64).checked_sub(-i as u64) + } else { + // std::io::SeekFrom documentation explicitly allows + // seeking beyond the end of the stream, so we seek + // to the end of the content if the offset is 0 or + // greater. + Some(self.length as u64) + }; + match next { + // std::io:SeekFrom documentation states that it `is an + // error to seek before byte 0.' So it's the sensible + // thing to refuse to seek on underflow. + None => None, + Some(u) => { + self.index = u as usize; + Some(u) + } + }; + } + } + true + } else { + false + } + } + { + use std::cmp; + let end = cmp::min(self.index + self.chunk_size, self.length); + consumer.handle(Input::Element(&self.buffer[self.index..end])) + } else { + consumer.state() + } + } +} + +#[derive(Debug,Copy,Clone,PartialEq,Eq)] +pub enum FileProducerState { + Normal, + Error, + Eof +} + +#[derive(Debug)] +pub struct FileProducer { + size: usize, + file: File, + position: usize, + v: Vec, + start: usize, + end: usize, + state: FileProducerState, +} + +impl FileProducer { + pub fn new(filename: &str, buffer_size: usize) -> io::Result { + File::open(&Path::new(filename)).and_then(|mut f| { + f.seek(SeekFrom::Start(0)).map(|_| { + let mut v = Vec::with_capacity(buffer_size); + v.extend(repeat(0).take(buffer_size)); + FileProducer {size: buffer_size, file: f, position: 0, v: v, start: 0, end: 0, state: FileProducerState::Normal } + }) + }) + } + + pub fn state(&self) -> FileProducerState { + self.state + } + + // FIXME: should handle refill until a certain size is obtained + pub fn refill(&mut self) -> Option { + shift(&mut self.v, self.start, self.end); + self.end = self.end - self.start; + self.start = 0; + match self.file.read(&mut self.v[self.end..]) { + Err(_) => { + self.state = FileProducerState::Error; + None + }, + Ok(n) => { + //println!("read: {} bytes\ndata:\n{:?}", n, &self.v); + if n == 0 { + self.state = FileProducerState::Eof; + } + self.end += n; + Some(0) + } + } + } + + /// Resize the internal buffer, copy the data to the new one and returned how much data was copied + /// + /// If the new buffer is smaller, the prefix will be copied, and the rest of the data will be dropped + pub fn resize(&mut self, s: usize) -> usize { + let mut v = vec![0; s]; + let length = self.end - self.start; + + let size = if length <= s { length } else { s }; + + // Use `Write` for `&mut [u8]` + (&mut v[..]).write(&self.v[self.start..self.start + size]).unwrap(); + + self.v = v; + self.start = 0; + self.end = size; + + size + } +} + +pub fn shift(s: &mut[u8], start: usize, end: usize) { + if start > 0 { + unsafe { + let length = end - start; + ptr::copy( (&s[start..end]).as_ptr(), (&mut s[..length]).as_mut_ptr(), length); + } + } +} + + +impl<'x> Producer<'x,&'x [u8],Move> for FileProducer { + + fn apply<'a,O,E>(&'x mut self, consumer: &'a mut Consumer<&'x[u8],O,E,Move>) -> &'a ConsumerState { + //consumer.handle(Input::Element(&self.v[self.start..self.end])) + //self.my_apply(consumer) + if { + if let &ConsumerState::Continue(ref m) = consumer.state() { + match *m { + Move::Consume(s) => { + //println!("start: {}, end: {}, consumed: {}", self.start, self.end, s); + if self.end - self.start >= s { + self.start = self.start + s; + self.position = self.position + s; + } else { + panic!("cannot consume past the end of the buffer"); + } + if self.start == self.end { + self.refill(); + } + }, + Move::Await(_) => { + self.refill(); + }, + + // FIXME: naive seeking for now + Move::Seek(position) => { + let pos = match position { + // take into account data in the buffer + SeekFrom::Current(c) => SeekFrom::Current(c - (self.end - self.start) as i64), + default => default + }; + match self.file.seek(pos) { + Ok(pos) => { + //println!("file got seek to position {:?}. New position is {:?}", position, next); + self.position = pos as usize; + self.start = 0; + self.end = 0; + self.refill(); + }, + Err(_) => { + self.state = FileProducerState::Error; + } + } + } + } + true + } else { + false + } + } + { + //println!("producer state: {:?}", self.state); + match self.state { + FileProducerState::Normal => consumer.handle(Input::Element(&self.v[self.start..self.end])), + FileProducerState::Eof => { + let slice = &self.v[self.start..self.end]; + + if slice.is_empty() { + consumer.handle(Input::Eof(None)) + } else { + consumer.handle(Input::Eof(Some(slice))) + } + } + // is it right? + FileProducerState::Error => consumer.state() + } + } else { + consumer.state() + } + } +} + + +use std::marker::PhantomData; + +/// MapConsumer takes a function S -> T and applies it on a consumer producing values of type S +pub struct MapConsumer<'a, C:'a,R,S,T,E,M,F> { + state: ConsumerState, + consumer: &'a mut C, + f: F, + consumer_input_type: PhantomData, + f_input_type: PhantomData, + f_output_type: PhantomData +} + +impl<'a,R,S:Clone,T,E:Clone,M:Clone,F:Fn(S) -> T,C:Consumer> MapConsumer<'a,C,R,S,T,E,M,F> { + pub fn new(c: &'a mut C, f: F) -> MapConsumer<'a,C,R,S,T,E,M,F> { + //let state = c.state(); + let initial = match *c.state() { + ConsumerState::Done(ref m, ref o) => ConsumerState::Done(m.clone(), f(o.clone())), + ConsumerState::Error(ref e) => ConsumerState::Error(e.clone()), + ConsumerState::Continue(ref m) => ConsumerState::Continue(m.clone()) + }; + + MapConsumer { + state: initial, + consumer: c, + f: f, + consumer_input_type: PhantomData, + f_input_type: PhantomData, + f_output_type: PhantomData + } + } +} + +impl<'a,R,S:Clone,T,E:Clone,M:Clone,F:Fn(S) -> T,C:Consumer> Consumer for MapConsumer<'a,C,R,S,T,E,M,F> { + fn handle(&mut self, input: Input) -> &ConsumerState { + let res:&ConsumerState = self.consumer.handle(input); + self.state = match res { + &ConsumerState::Done(ref m, ref o) => ConsumerState::Done(m.clone(), (self.f)(o.clone())), + &ConsumerState::Error(ref e) => ConsumerState::Error(e.clone()), + &ConsumerState::Continue(ref m) => ConsumerState::Continue(m.clone()) + }; + &self.state + } + + fn state(&self) -> &ConsumerState { + &self.state + } +} + +/// ChainConsumer takes a consumer C1 R -> S, and a consumer C2 S -> T, and makes a consumer R -> T by applying C2 on C1's result +pub struct ChainConsumer<'a,'b, C1:'a,C2:'b,R,S,T,E,M> { + state: ConsumerState, + consumer1: &'a mut C1, + consumer2: &'b mut C2, + input_type: PhantomData, + temp_type: PhantomData +} + +impl<'a,'b,R,S:Clone,T:Clone,E:Clone,M:Clone,C1:Consumer, C2:Consumer> ChainConsumer<'a,'b,C1,C2,R,S,T,E,M> { + pub fn new(c1: &'a mut C1, c2: &'b mut C2) -> ChainConsumer<'a,'b,C1,C2,R,S,T,E,M> { + let initial = match *c1.state() { + ConsumerState::Error(ref e) => ConsumerState::Error(e.clone()), + ConsumerState::Continue(ref m) => ConsumerState::Continue(m.clone()), + ConsumerState::Done(ref m, ref o) => match *c2.handle(Input::Element(o.clone())) { + ConsumerState::Error(ref e) => ConsumerState::Error(e.clone()), + ConsumerState::Continue(ref m2) => ConsumerState::Continue(m2.clone()), + ConsumerState::Done(_,ref o2) => ConsumerState::Done(m.clone(), o2.clone()) + } + }; + + ChainConsumer { + state: initial, + consumer1: c1, + consumer2: c2, + input_type: PhantomData, + temp_type: PhantomData + } + } +} + +impl<'a,'b,R,S:Clone,T:Clone,E:Clone,M:Clone,C1:Consumer, C2:Consumer> Consumer for ChainConsumer<'a,'b,C1,C2,R,S,T,E,M> { + fn handle(&mut self, input: Input) -> &ConsumerState { + let res:&ConsumerState = self.consumer1.handle(input); + self.state = match *res { + ConsumerState::Error(ref e) => ConsumerState::Error(e.clone()), + ConsumerState::Continue(ref m) => ConsumerState::Continue(m.clone()), + ConsumerState::Done(ref m, ref o) => match *self.consumer2.handle(Input::Element(o.clone())) { + ConsumerState::Error(ref e) => ConsumerState::Error(e.clone()), + ConsumerState::Continue(ref m) => ConsumerState::Continue(m.clone()), + ConsumerState::Done(_, ref o2) => ConsumerState::Done(m.clone(), o2.clone()) + } + }; + &self.state + } + + fn state(&self) -> &ConsumerState { + &self.state + } +} + +#[macro_export] +macro_rules! consumer_from_parser ( + //FIXME: should specify the error and move type + ($name:ident<$input:ty, $output:ty>, $submac:ident!( $($args:tt)* )) => ( + #[derive(Debug)] + struct $name { + state: $crate::ConsumerState<$output, (), $crate::Move> + } + + impl $name { + fn new() -> $name { + $name { state: $crate::ConsumerState::Continue($crate::Move::Consume(0)) } + } + } + + impl $crate::Consumer<$input, $output, (), $crate::Move> for $name { + fn handle(&mut self, input: $crate::Input<$input>) -> & $crate::ConsumerState<$output, (), $crate::Move> { + use $crate::HexDisplay; + match input { + $crate::Input::Empty | $crate::Input::Eof(None) => &self.state, + $crate::Input::Element(sl) | $crate::Input::Eof(Some(sl)) => { + self.state = match $submac!(sl, $($args)*) { + $crate::IResult::Incomplete(n) => { + $crate::ConsumerState::Continue($crate::Move::Await(n)) + }, + $crate::IResult::Error(_) => { + $crate::ConsumerState::Error(()) + }, + $crate::IResult::Done(i,o) => { + $crate::ConsumerState::Done($crate::Move::Consume(sl.offset(i)), o) + } + }; + + &self.state + } + } + + } + + fn state(&self) -> &$crate::ConsumerState<$output, (), $crate::Move> { + &self.state + } + } + ); + ($name:ident<$output:ty>, $submac:ident!( $($args:tt)* )) => ( + #[derive(Debug)] + struct $name { + state: $crate::ConsumerState<$output, (), $crate::Move> + } + + impl $name { + // Allow this to go unused, because code in the defining scope can create the struct directly. + #[allow(dead_code)] + fn new() -> $name { + $name { state: $crate::ConsumerState::Continue($crate::Move::Consume(0)) } + } + } + + impl<'a> $crate::Consumer<&'a[u8], $output, (), $crate::Move> for $name { + fn handle(&mut self, input: $crate::Input<&'a[u8]>) -> & $crate::ConsumerState<$output, (), $crate::Move> { + use $crate::HexDisplay; + match input { + $crate::Input::Empty | $crate::Input::Eof(None) => &self.state, + $crate::Input::Element(sl) | $crate::Input::Eof(Some(sl)) => { + self.state = match $submac!(sl, $($args)*) { + $crate::IResult::Incomplete(n) => { + $crate::ConsumerState::Continue($crate::Move::Await(n)) + }, + $crate::IResult::Error(_) => { + $crate::ConsumerState::Error(()) + }, + $crate::IResult::Done(i,o) => { + $crate::ConsumerState::Done($crate::Move::Consume(sl.offset(i)), o) + } + }; + + &self.state + } + } + + } + + fn state(&self) -> &$crate::ConsumerState<$output, (), $crate::Move> { + &self.state + } + } + ); + ($name:ident<$input:ty, $output:ty>, $f:expr) => ( + consumer_from_parser!($name<$input, $output>, call!($f)); + ); + ($name:ident<$output:ty>, $f:expr) => ( + consumer_from_parser!($name<$output>, call!($f)); + ); + +); + +#[cfg(test)] +mod tests { + use super::*; + use internal::IResult; + use util::HexDisplay; + use std::str::from_utf8; + use std::io::SeekFrom; + + #[derive(Debug)] + struct AbcdConsumer<'a> { + state: ConsumerState<&'a [u8], (), Move> + } + + named!(abcd, tag!("abcd")); + impl<'a> Consumer<&'a [u8], &'a [u8], (), Move> for AbcdConsumer<'a> { + fn handle(&mut self, input: Input<&'a [u8]>) -> &ConsumerState<&'a [u8],(),Move> { + match input { + Input::Empty | Input::Eof(None) => &self.state, + Input::Element(sl) => { + match abcd(sl) { + IResult::Error(_) => { + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.state = ConsumerState::Continue(Move::Consume(0)) + }, + IResult::Done(i,o) => { + self.state = ConsumerState::Done(Move::Consume(sl.offset(i)),o) + } + }; + &self.state + } + Input::Eof(Some(sl)) => { + match abcd(sl) { + IResult::Error(_) => { + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + // we cannot return incomplete on Eof + self.state = ConsumerState::Error(()) + }, + IResult::Done(i,o) => { + self.state = ConsumerState::Done(Move::Consume(sl.offset(i)), o) + } + }; + &self.state + } + } + + } + + fn state(&self) -> &ConsumerState<&'a [u8], (), Move> { + &self.state + } + } + + #[test] + fn mem() { + let mut m = MemProducer::new(&b"abcdabcdabcdabcdabcd"[..], 8); + + let mut a = AbcdConsumer { state: ConsumerState::Continue(Move::Consume(0)) }; + + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + //assert!(false); + } + + named!(efgh, tag!("efgh")); + named!(ijkl, tag!("ijkl")); + #[derive(Debug)] + enum State { + Initial, + A, + B, + End, + Error + } + #[derive(Debug)] + struct StateConsumer<'a> { + state: ConsumerState<&'a [u8], (), Move>, + parsing_state: State + } + + impl<'a> Consumer<&'a [u8], &'a [u8], (), Move> for StateConsumer<'a> { + fn handle(&mut self, input: Input<&'a [u8]>) -> &ConsumerState<&'a [u8], (), Move> { + match input { + Input::Empty | Input::Eof(None) => &self.state, + Input::Element(sl) => { + match self.parsing_state { + State::Initial => match abcd(sl) { + IResult::Error(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.state = ConsumerState::Continue(Move::Consume(0)) + }, + IResult::Done(i,_) => { + self.parsing_state = State::A; + self.state = ConsumerState::Continue(Move::Consume(sl.offset(i))) + } + }, + State::A => match efgh(sl) { + IResult::Error(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.state = ConsumerState::Continue(Move::Consume(0)) + }, + IResult::Done(i,_) => { + self.parsing_state = State::B; + self.state = ConsumerState::Continue(Move::Consume(sl.offset(i))) + } + }, + State::B => match ijkl(sl) { + IResult::Error(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.state = ConsumerState::Continue(Move::Consume(0)) + }, + IResult::Done(i,o) => { + self.parsing_state = State::End; + self.state = ConsumerState::Done(Move::Consume(sl.offset(i)),o) + } + }, + _ => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + } + } + &self.state + } + Input::Eof(Some(sl)) => { + match self.parsing_state { + State::Initial => match abcd(sl) { + IResult::Error(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Done(_,_) => { + self.parsing_state = State::A; + self.state = ConsumerState::Error(()) + } + }, + State::A => match efgh(sl) { + IResult::Error(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Done(_,_) => { + self.parsing_state = State::B; + self.state = ConsumerState::Error(()) + } + }, + State::B => match ijkl(sl) { + IResult::Error(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Incomplete(_) => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + }, + IResult::Done(i,o) => { + self.parsing_state = State::End; + self.state = ConsumerState::Done(Move::Consume(sl.offset(i)), o) + } + }, + _ => { + self.parsing_state = State::Error; + self.state = ConsumerState::Error(()) + } + } + &self.state + } + } + + } + + fn state(&self) -> &ConsumerState<&'a [u8], (), Move> { + &self.state + } + } + impl<'a> StateConsumer<'a> { + fn parsing(&self) -> &State { + &self.parsing_state + } + } + + #[test] + fn mem2() { + let mut m = MemProducer::new(&b"abcdefghijklabcdabcd"[..], 8); + + let mut a = StateConsumer { state: ConsumerState::Continue(Move::Consume(0)), parsing_state: State::Initial }; + + println!("apply {:?}", m.apply(&mut a)); + println!("state {:?}", a.parsing()); + println!("apply {:?}", m.apply(&mut a)); + println!("state {:?}", a.parsing()); + println!("apply {:?}", m.apply(&mut a)); + println!("state {:?}", a.parsing()); + println!("apply {:?}", m.apply(&mut a)); + println!("state {:?}", a.parsing()); + //assert!(false); + } + + + #[test] + fn map() { + let mut m = MemProducer::new(&b"abcdefghijklabcdabcd"[..], 8); + + let mut s = StateConsumer { state: ConsumerState::Continue(Move::Consume(0)), parsing_state: State::Initial }; + let mut a = MapConsumer::new(&mut s, from_utf8); + + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + //assert!(false); + } + + #[derive(Debug)] + struct StrConsumer<'a> { + state: ConsumerState<&'a str, (), Move> + } + + impl<'a> Consumer<&'a [u8], &'a str, (), Move> for StrConsumer<'a> { + fn handle(&mut self, input: Input<&'a [u8]>) -> &ConsumerState<&'a str, (), Move> { + match input { + Input::Empty | Input::Eof(None) => &self.state, + Input::Element(sl) | Input::Eof(Some(sl)) => { + self.state = ConsumerState::Done(Move::Consume(sl.len()), from_utf8(sl).unwrap()); + &self.state + } + } + + } + + fn state(&self) -> &ConsumerState<&'a str, (), Move> { + &self.state + } + } + + + #[test] + fn chain() { + let mut m = MemProducer::new(&b"abcdefghijklabcdabcd"[..], 8); + + let mut s1 = StateConsumer { state: ConsumerState::Continue(Move::Consume(0)), parsing_state: State::Initial }; + let mut s2 = StrConsumer { state: ConsumerState::Continue(Move::Consume(0)) }; + let mut a = ChainConsumer::new(&mut s1, &mut s2); + + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + println!("apply {:?}", m.apply(&mut a)); + //assert!(false); + // + //let x = [0, 1, 2, 3, 4]; + //let b = [1, 2, 3]; + //assert_eq!(&x[1..3], &b[..]); + } + + #[test] + fn shift_test() { + let mut v = vec![0,1,2,3,4,5]; + shift(&mut v, 1, 3); + assert_eq!(&v[..2], &[1,2][..]); + let mut v2 = vec![0,1,2,3,4,5]; + shift(&mut v2, 2, 6); + assert_eq!(&v2[..4], &[2,3,4,5][..]); + } + + /*#[derive(Debug)] + struct LineConsumer { + state: ConsumerState + } + impl<'a> Consumer<&'a [u8], String, (), Move> for LineConsumer { + fn handle(&mut self, input: Input<&'a [u8]>) -> &ConsumerState { + match input { + Input::Empty | Input::Eof(None) => &self.state, + Input::Element(sl) | Input::Eof(Some(sl)) => { + //println!("got slice: {:?}", sl); + self.state = match line(sl) { + IResult::Incomplete(n) => { + println!("line not complete, continue (line was \"{}\")", from_utf8(sl).unwrap()); + ConsumerState::Continue(Move::Await(n)) + }, + IResult::Error(e) => { + println!("LineConsumer parsing error: {:?}", e); + ConsumerState::Error(()) + }, + IResult::Done(i,o) => { + let res = String::from(from_utf8(o).unwrap()); + println!("found: {}", res); + //println!("sl: {:?}\ni:{:?}\noffset:{}", sl, i, sl.offset(i)); + ConsumerState::Done(Move::Consume(sl.offset(i)), res) + } + }; + + &self.state + } + } + + } + + fn state(&self) -> &ConsumerState { + &self.state + } + }*/ + + fn lf(i:& u8) -> bool { + *i == '\n' as u8 + } + fn to_utf8_string(input:&[u8]) -> String { + String::from(from_utf8(input).unwrap()) + } + + //named!(line<&[u8]>, terminated!(take_till!(lf), tag!("\n"))); + + consumer_from_parser!(LineConsumer, map!(terminated!(take_till!(lf), tag!("\n")), to_utf8_string)); + + fn get_line(producer: &mut FileProducer, mv: Move) -> Option<(Move,String)> { + let mut a = LineConsumer { state: ConsumerState::Continue(mv) }; + while let &ConsumerState::Continue(_) = producer.apply(&mut a) { + println!("continue"); + } + + if let &ConsumerState::Done(ref m, ref s) = a.state() { + Some((m.clone(), s.clone())) + } else { + None + } + } + + #[test] + fn file() { + let mut f = FileProducer::new("LICENSE", 200).unwrap(); + f.refill(); + + let mut mv = Move::Consume(0); + for i in 1..10 { + if let Some((m,s)) = get_line(&mut f, mv.clone()) { + println!("got line[{}]: {}", i, s); + mv = m; + } else { + assert!(false, "LineConsumer should not have failed"); + } + } + //assert!(false); + } + + #[derive(Debug,Clone,Copy,PartialEq,Eq)] + enum SeekState { + Begin, + SeekedToEnd, + ShouldEof, + IsEof + } + + #[derive(Debug)] + struct SeekingConsumer { + state: ConsumerState<(), u8, Move>, + position: SeekState + } + + impl SeekingConsumer { + fn position(&self) -> SeekState { + self.position + } + } + + impl<'a> Consumer<&'a [u8], (), u8, Move> for SeekingConsumer { + fn handle(&mut self, input: Input<&'a [u8]>) -> &ConsumerState<(), u8, Move> { + println!("input: {:?}", input); + match self.position { + SeekState::Begin => { + self.state = ConsumerState::Continue(Move::Seek(SeekFrom::End(-4))); + self.position = SeekState::SeekedToEnd; + }, + SeekState::SeekedToEnd => match input { + Input::Element(sl) => { + if sl.len() == 4 { + self.state = ConsumerState::Continue(Move::Consume(4)); + self.position = SeekState::ShouldEof; + } else { + self.state = ConsumerState::Error(0); + } + }, + Input::Eof(Some(sl)) => { + if sl.len() == 4 { + self.state = ConsumerState::Done(Move::Consume(4), ()); + self.position = SeekState::IsEof; + } else { + self.state = ConsumerState::Error(1); + } + }, + _ => self.state = ConsumerState::Error(2) + }, + SeekState::ShouldEof => match input { + Input::Eof(Some(sl)) => { + if sl.len() == 0 { + self.state = ConsumerState::Done(Move::Consume(0), ()); + self.position = SeekState::IsEof; + } else { + self.state = ConsumerState::Error(3); + } + }, + Input::Eof(None) => { + self.state = ConsumerState::Done(Move::Consume(0), ()); + self.position = SeekState::IsEof; + }, + _ => self.state = ConsumerState::Error(4) + }, + _ => self.state = ConsumerState::Error(5) + }; + &self.state + } + + fn state(&self) -> &ConsumerState<(), u8, Move> { + &self.state + } + } + + #[test] + fn seeking_consumer() { + let mut f = FileProducer::new("assets/testfile.txt", 200).unwrap(); + f.refill(); + + let mut a = SeekingConsumer { state: ConsumerState::Continue(Move::Consume(0)), position: SeekState::Begin }; + for _ in 1..4 { + println!("file apply {:?}", f.apply(&mut a)); + } + println!("consumer is now: {:?}", a); + if let &ConsumerState::Done(Move::Consume(0), ()) = a.state() { + println!("end"); + } else { + println!("invalid state is {:?}", a.state()); + assert!(false, "consumer is not at EOF"); + } + assert_eq!(a.position(), SeekState::IsEof); + } +} diff --git third_party/rust/nom-1.2.4/src/util.rs third_party/rust/nom-1.2.4/src/util.rs new file mode 100644 index 000000000000..e2428ead0a4d --- /dev/null +++ third_party/rust/nom-1.2.4/src/util.rs @@ -0,0 +1,769 @@ +use internal::{IResult,Err}; + +#[cfg(not(feature = "core"))] +use std::collections::HashMap; + +#[cfg(feature = "core")] +use std::prelude::v1::*; +use std::vec::Vec; +use std::string::ToString; + +/// useful functions to calculate the offset between slices and show a hexdump of a slice +#[cfg(not(feature = "core"))] +pub trait HexDisplay { + /// offset between the first byte of self and the first byte of the argument + fn offset(&self, second:&[u8]) -> usize;// OFFSET SHOULD GO TO ITS OWN TRAIT + + /// Converts the value of `self` to a hex dump, returning the owned + /// string. + fn to_hex(&self, chunk_size: usize) -> String; + + /// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned + /// string. + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String; +} + +pub trait InputLength { + #[inline] + fn input_len(&self) -> usize; +} + +impl<'a, T> InputLength for &'a[T] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for &'a str { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for (&'a [u8], usize) { + #[inline] + fn input_len(&self) -> usize { + //println!("bit input length for ({:?}, {}):", self.0, self.1); + let res = self.0.len() * 8 - self.1; + //println!("-> {}", res); + res + } +} + +use std::iter::Enumerate; +#[cfg(not(feature = "core"))] +use std::str::CharIndices; + +pub trait AsChar { + #[inline] + fn as_char(self) -> char; + #[inline] + fn is_alpha(self) -> bool; + #[inline] + fn is_alphanum(self) -> bool; + #[inline] + fn is_0_to_9(self) -> bool; + #[inline] + fn is_hex_digit(self) -> bool; + #[inline] + fn is_oct_digit(self) -> bool; +} + +impl<'a> AsChar for &'a u8 { + #[inline] + fn as_char(self) -> char { *self as char } + #[inline] + fn is_alpha(self) -> bool { + (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { self.is_alpha() || self.is_0_to_9() } + #[inline] + fn is_0_to_9(self) -> bool { + *self >= 0x30 && *self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (*self >= 0x30 && *self <= 0x39) || + (*self >= 0x41 && *self <= 0x46) || + (*self >= 0x61 && *self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + *self >= 0x30 && *self <= 0x37 + } +} + +impl AsChar for char { + #[inline] + fn as_char(self) -> char { self } + #[inline] + fn is_alpha(self) -> bool { self.is_alphabetic() } + #[inline] + fn is_alphanum(self) -> bool { self.is_alpha() || self.is_0_to_9() } + #[inline] + fn is_0_to_9(self) -> bool { self.is_digit(10) } + #[inline] + fn is_hex_digit(self) -> bool { self.is_digit(16) } + #[inline] + fn is_oct_digit(self) -> bool { self.is_digit(8) } +} + +pub trait IterIndices { + type Item: AsChar; + type Iter : Iterator; + fn iter_indices(self) -> Self::Iter; +} + +impl<'a> IterIndices for &'a [u8] { + type Item = &'a u8; + type Iter = Enumerate<::std::slice::Iter<'a, u8>>; + #[inline] + fn iter_indices(self) -> Enumerate<::std::slice::Iter<'a, u8>> { + self.iter().enumerate() + } +} + +#[cfg(not(feature = "core"))] +impl<'a> IterIndices for &'a str { + type Item = char; + type Iter = CharIndices<'a>; + #[inline] + fn iter_indices(self) -> CharIndices<'a> { + self.char_indices() + } +} + +static CHARS: &'static[u8] = b"0123456789abcdef"; + +#[cfg(not(feature = "core"))] +impl HexDisplay for [u8] { + fn offset(&self, second:&[u8]) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } + + #[allow(unused_variables)] + fn to_hex(&self, chunk_size: usize) -> String { + self.to_hex_from(chunk_size, 0) + } + + #[allow(unused_variables)] + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { + let mut v = Vec::with_capacity(self.len() * 3); + let mut i = from; + for chunk in self.chunks(chunk_size) { + let s = format!("{:08x}", i); + for &ch in s.as_bytes().iter() { + v.push(ch); + } + v.push('\t' as u8); + + i = i + chunk_size; + + for &byte in chunk { + v.push(CHARS[(byte >> 4) as usize]); + v.push(CHARS[(byte & 0xf) as usize]); + v.push(' ' as u8); + } + if chunk_size > chunk.len() { + for j in 0..(chunk_size - chunk.len()) { + v.push(' ' as u8); + v.push(' ' as u8); + v.push(' ' as u8); + } + } + v.push('\t' as u8); + + for &byte in chunk { + if (byte >=32 && byte <= 126) || byte >= 128 { + v.push(byte); + } else { + v.push('.' as u8); + } + } + v.push('\n' as u8); + } + + String::from_utf8_lossy(&v[..]).into_owned() + } +} + +/// Prints a message if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(f, dbg!( tag!( "abcd" ) ) ); +/// +/// let a = &b"efgh"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104])) at l.5 by ' tag ! ( "abcd" ) ' +/// f(a); +/// # } +/// ``` +#[macro_export] +macro_rules! dbg ( + ($i: expr, $submac:ident!( $($args:tt)* )) => ( + { + let l = line!(); + match $submac!($i, $($args)*) { + $crate::IResult::Error(a) => { + println!("Error({:?}) at l.{} by ' {} '", a, l, stringify!($submac!($($args)*))); + $crate::IResult::Error(a) + }, + $crate::IResult::Incomplete(a) => { + println!("Incomplete({:?}) at {} by ' {} '", a, l, stringify!($submac!($($args)*))); + $crate::IResult::Incomplete(a) + }, + a => a + } + } + ); + + ($i:expr, $f:ident) => ( + dbg!($i, call!($f)); + ); +); + +/// Prints a message and the input if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code. +/// +/// It also displays the input in hexdump format +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(f, dbg_dmp!( tag!( "abcd" ) ) ); +/// +/// let a = &b"efghijkl"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) ' +/// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl +/// f(a); +/// # } +#[macro_export] +macro_rules! dbg_dmp ( + ($i: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::HexDisplay; + let l = line!(); + match $submac!($i, $($args)*) { + $crate::IResult::Error(a) => { + println!("Error({:?}) at l.{} by ' {} '\n{}", a, l, stringify!($submac!($($args)*)), $i.to_hex(8)); + $crate::IResult::Error(a) + }, + $crate::IResult::Incomplete(a) => { + println!("Incomplete({:?}) at {} by ' {} '\n{}", a, l, stringify!($submac!($($args)*)), $i.to_hex(8)); + $crate::IResult::Incomplete(a) + }, + a => a + } + } + ); + + ($i:expr, $f:ident) => ( + dbg_dmp!($i, call!($f)); + ); +); + +pub fn error_to_list(e:&Err) -> Vec> { + let mut v:Vec> = Vec::new(); + let mut err = e; + loop { + match *err { + Err::Code(ref i) | Err::Position(ref i,_) => { + v.push(i.clone()); + return v; + }, + Err::Node(ref i, ref next) | Err::NodePosition(ref i, _, ref next) => { + v.push(i.clone()); + err = &*next; + } + } + } +} + +pub fn compare_error_paths(e1:&Err, e2:&Err) -> bool { + error_to_list(e1) == error_to_list(e2) +} + + +#[cfg(not(feature = "core"))] +use std::hash::Hash; + +#[cfg(not(feature = "core"))] +pub fn add_error_pattern<'a,I,O,E: Clone+Hash+Eq>(h: &mut HashMap>, &'a str>, res: IResult, message: &'a str) -> bool { + if let IResult::Error(e) = res { + h.insert(error_to_list(&e), message); + true + } else { + false + } +} + +pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) { + let start = input.as_ptr(); + let off1 = s.as_ptr() as usize - start as usize; + let off2 = off1 + s.len(); + (off1, off2) +} + +#[cfg(not(feature = "core"))] +pub fn prepare_errors(input: &[u8], res: IResult<&[u8],O,E>) -> Option, usize, usize)> > { + if let IResult::Error(e) = res { + let mut v:Vec<(ErrorKind, usize, usize)> = Vec::new(); + let mut err = e.clone(); + loop { + match err { + Err::Position(i,s) => { + let (o1, o2) = slice_to_offsets(input, s); + v.push((i, o1, o2)); + //println!("v is: {:?}", v); + break; + }, + Err::NodePosition(i, s, next) => { + let (o1, o2) = slice_to_offsets(input, s); + v.push((i, o1, o2)); + err = *next; + }, + Err::Node(_, next) => { + err = *next; + }, + Err::Code(_) => { + break; + } + } + } + v.sort_by(|a, b| a.1.cmp(&b.1)); + Some(v) + } else { + None + } +} + +#[cfg(not(feature = "core"))] +pub fn print_error(input: &[u8], res: IResult<&[u8],O,E>) { + if let Some(v) = prepare_errors(input, res) { + let colors = generate_colors(&v); + println!("parser codes: {}", print_codes(colors, HashMap::new())); + println!("{}", print_offsets(input, 0, &v)); + + } else { + println!("not an error"); + } +} + +#[cfg(not(feature = "core"))] +pub fn generate_colors(v: &[(ErrorKind, usize, usize)]) -> HashMap { + let mut h: HashMap = HashMap::new(); + let mut color = 0; + + for &(ref c,_,_) in v.iter() { + h.insert(error_to_u32(c), color + 31); + color = color + 1 % 7; + } + + h +} + +pub fn code_from_offset(v: &[(ErrorKind, usize, usize)], offset: usize) -> Option { + let mut acc: Option<(u32, usize, usize)> = None; + for &(ref ek, s, e) in v.iter() { + let c = error_to_u32(ek); + if s <= offset && offset <=e { + if let Some((_, start, end)) = acc { + if start <= s && e <= end { + acc = Some((c, s, e)); + } + } else { + acc = Some((c, s, e)); + } + } + } + if let Some((code, _, _)) = acc { + return Some(code); + } else { + return None; + } +} + +pub fn reset_color(v: &mut Vec) { + v.push(0x1B); + v.push('[' as u8); + v.push(0); + v.push('m' as u8); +} + +pub fn write_color(v: &mut Vec, color: u8) { + v.push(0x1B); + v.push('[' as u8); + v.push(1); + v.push(';' as u8); + let s = color.to_string(); + let bytes = s.as_bytes(); + v.extend(bytes.iter().cloned()); + v.push('m' as u8); +} + +#[cfg(not(feature = "core"))] +pub fn print_codes(colors: HashMap, names: HashMap) -> String { + let mut v = Vec::new(); + for (code, &color) in &colors { + if let Some(&s) = names.get(&code) { + let bytes = s.as_bytes(); + write_color(&mut v, color); + v.extend(bytes.iter().cloned()); + } else { + let s = code.to_string(); + let bytes = s.as_bytes(); + write_color(&mut v, color); + v.extend(bytes.iter().cloned()); + } + reset_color(&mut v); + v.push(' ' as u8); + } + reset_color(&mut v); + + String::from_utf8_lossy(&v[..]).into_owned() +} + +#[cfg(not(feature = "core"))] +pub fn print_offsets(input: &[u8], from: usize, offsets: &[(ErrorKind, usize, usize)]) -> String { + let mut v = Vec::with_capacity(input.len() * 3); + let mut i = from; + let chunk_size = 8; + let mut current_code: Option = None; + let mut current_code2: Option = None; + + let colors = generate_colors(&offsets); + + for chunk in input.chunks(chunk_size) { + let s = format!("{:08x}", i); + for &ch in s.as_bytes().iter() { + v.push(ch); + } + v.push('\t' as u8); + + let mut k = i; + let mut l = i; + for &byte in chunk { + if let Some(code) = code_from_offset(&offsets, k) { + if let Some(current) = current_code { + if current != code { + reset_color(&mut v); + current_code = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } else { + current_code = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } + v.push(CHARS[(byte >> 4) as usize]); + v.push(CHARS[(byte & 0xf) as usize]); + v.push(' ' as u8); + k = k + 1; + } + + reset_color(&mut v); + + if chunk_size > chunk.len() { + for _ in 0..(chunk_size - chunk.len()) { + v.push(' ' as u8); + v.push(' ' as u8); + v.push(' ' as u8); + } + } + v.push('\t' as u8); + + for &byte in chunk { + if let Some(code) = code_from_offset(&offsets, l) { + if let Some(current) = current_code2 { + if current != code { + reset_color(&mut v); + current_code2 = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } else { + current_code2 = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } + if (byte >=32 && byte <= 126) || byte >= 128 { + v.push(byte); + } else { + v.push('.' as u8); + } + l = l + 1; + } + reset_color(&mut v); + + v.push('\n' as u8); + i = i + chunk_size; + } + + String::from_utf8_lossy(&v[..]).into_owned() +} + +pub trait AsBytes { + fn as_bytes(&self) -> &[u8]; +} + +impl<'a> AsBytes for &'a str { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + str::as_bytes(self) + } +} + +impl AsBytes for str { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + str::as_bytes(self) + } +} + +impl<'a> AsBytes for &'a [u8] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + *self + } +} + +impl AsBytes for [u8] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self + } +} + +macro_rules! array_impls { + ($($N:expr)+) => { + $( + impl<'a> AsBytes for &'a [u8; $N] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + *self + } + } + + impl AsBytes for [u8; $N] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self + } + } + )+ + }; +} + + +array_impls! { + 0 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 +} + +/// indicates which parser returned an error +#[derive(Debug,PartialEq,Eq,Hash,Clone)] +pub enum ErrorKind { + Custom(E), + Tag, + MapRes, + MapOpt, + Alt, + IsNot, + IsA, + SeparatedList, + SeparatedNonEmptyList, + Many0, + Many1, + Count, + TakeUntilAndConsume, + TakeUntil, + TakeUntilEitherAndConsume, + TakeUntilEither, + LengthValue, + TagClosure, + Alpha, + Digit, + HexDigit, + OctDigit, + AlphaNumeric, + Space, + MultiSpace, + LengthValueFn, + Eof, + ExprOpt, + ExprRes, + CondReduce, + Switch, + TagBits, + OneOf, + NoneOf, + Char, + CrLf, + RegexpMatch, + RegexpMatches, + RegexpFind, + RegexpCapture, + RegexpCaptures, + TakeWhile1, + Complete, + Fix, + Escaped, + EscapedTransform, + TagStr, + IsNotStr, + IsAStr, + TakeWhile1Str, + NonEmpty, + ManyMN, + TakeUntilAndConsumeStr, + TakeUntilStr, + Not +} + +pub fn error_to_u32(e: &ErrorKind) -> u32 { + match *e { + ErrorKind::Custom(_) => 0, + ErrorKind::Tag => 1, + ErrorKind::MapRes => 2, + ErrorKind::MapOpt => 3, + ErrorKind::Alt => 4, + ErrorKind::IsNot => 5, + ErrorKind::IsA => 6, + ErrorKind::SeparatedList => 7, + ErrorKind::SeparatedNonEmptyList => 8, + ErrorKind::Many1 => 9, + ErrorKind::Count => 10, + ErrorKind::TakeUntilAndConsume => 11, + ErrorKind::TakeUntil => 12, + ErrorKind::TakeUntilEitherAndConsume => 13, + ErrorKind::TakeUntilEither => 14, + ErrorKind::LengthValue => 15, + ErrorKind::TagClosure => 16, + ErrorKind::Alpha => 17, + ErrorKind::Digit => 18, + ErrorKind::AlphaNumeric => 19, + ErrorKind::Space => 20, + ErrorKind::MultiSpace => 21, + ErrorKind::LengthValueFn => 22, + ErrorKind::Eof => 23, + ErrorKind::ExprOpt => 24, + ErrorKind::ExprRes => 25, + ErrorKind::CondReduce => 26, + ErrorKind::Switch => 27, + ErrorKind::TagBits => 28, + ErrorKind::OneOf => 29, + ErrorKind::NoneOf => 30, + ErrorKind::Char => 40, + ErrorKind::CrLf => 41, + ErrorKind::RegexpMatch => 42, + ErrorKind::RegexpMatches => 43, + ErrorKind::RegexpFind => 44, + ErrorKind::RegexpCapture => 45, + ErrorKind::RegexpCaptures => 46, + ErrorKind::TakeWhile1 => 47, + ErrorKind::Complete => 48, + ErrorKind::Fix => 49, + ErrorKind::Escaped => 50, + ErrorKind::EscapedTransform => 51, + ErrorKind::TagStr => 52, + ErrorKind::IsNotStr => 53, + ErrorKind::IsAStr => 54, + ErrorKind::TakeWhile1Str => 55, + ErrorKind::NonEmpty => 56, + ErrorKind::ManyMN => 57, + ErrorKind::TakeUntilAndConsumeStr => 58, + ErrorKind::HexDigit => 59, + ErrorKind::TakeUntilStr => 60, + ErrorKind::OctDigit => 61, + ErrorKind::Many0 => 62, + ErrorKind::Not => 63, + } +} + + impl ErrorKind { + pub fn description(&self) -> &str { + match *self { + ErrorKind::Custom(_) => "Custom error", + ErrorKind::Tag => "Tag", + ErrorKind::MapRes => "Map on Result", + ErrorKind::MapOpt => "Map on Option", + ErrorKind::Alt => "Alternative", + ErrorKind::IsNot => "IsNot", + ErrorKind::IsA => "IsA", + ErrorKind::SeparatedList => "Separated list", + ErrorKind::SeparatedNonEmptyList => "Separated non empty list", + ErrorKind::Many0 => "Many0", + ErrorKind::Many1 => "Many1", + ErrorKind::Count => "Count", + ErrorKind::TakeUntilAndConsume => "Take until and consume", + ErrorKind::TakeUntil => "Take until", + ErrorKind::TakeUntilEitherAndConsume => "Take until either and consume", + ErrorKind::TakeUntilEither => "Take until either", + ErrorKind::LengthValue => "Length followed by value", + ErrorKind::TagClosure => "Tag closure", + ErrorKind::Alpha => "Alphabetic", + ErrorKind::Digit => "Digit", + ErrorKind::AlphaNumeric => "AlphaNumeric", + ErrorKind::Space => "Space", + ErrorKind::MultiSpace => "Multiple spaces", + ErrorKind::LengthValueFn => "LengthValueFn", + ErrorKind::Eof => "End of file", + ErrorKind::ExprOpt => "Evaluate Option", + ErrorKind::ExprRes => "Evaluate Result", + ErrorKind::CondReduce => "Condition reduce", + ErrorKind::Switch => "Switch", + ErrorKind::TagBits => "Tag on bitstream", + ErrorKind::OneOf => "OneOf", + ErrorKind::NoneOf => "NoneOf", + ErrorKind::Char => "Char", + ErrorKind::CrLf => "CrLf", + ErrorKind::RegexpMatch => "RegexpMatch", + ErrorKind::RegexpMatches => "RegexpMatches", + ErrorKind::RegexpFind => "RegexpFind", + ErrorKind::RegexpCapture => "RegexpCapture", + ErrorKind::RegexpCaptures => "RegexpCaptures", + ErrorKind::TakeWhile1 => "TakeWhile1", + ErrorKind::Complete => "Complete", + ErrorKind::Fix => "Fix", + ErrorKind::Escaped => "Escaped", + ErrorKind::EscapedTransform => "EscapedTransform", + ErrorKind::TagStr => "Tag on strings", + ErrorKind::IsNotStr => "IsNot on strings", + ErrorKind::IsAStr => "IsA on strings", + ErrorKind::TakeWhile1Str => "TakeWhile1 on strings", + ErrorKind::NonEmpty => "NonEmpty", + ErrorKind::ManyMN => "Many(m, n)", + ErrorKind::TakeUntilAndConsumeStr => "Take until and consume on strings", + ErrorKind::HexDigit => "Hexadecimal Digit", + ErrorKind::TakeUntilStr => "Take until on strings", + ErrorKind::OctDigit => "Octal digit", + ErrorKind::Not => "Negation", + } + + } + } diff --git third_party/rust/nom-1.2.4/tests/arithmetic.rs third_party/rust/nom-1.2.4/tests/arithmetic.rs new file mode 100644 index 000000000000..eea990fb07cf --- /dev/null +++ third_party/rust/nom-1.2.4/tests/arithmetic.rs @@ -0,0 +1,80 @@ +#[macro_use] +extern crate nom; + +use nom::{IResult,digit, multispace}; + +use std::str; +use std::str::FromStr; + +named!(parens, delimited!( + delimited!(opt!(multispace), tag!("("), opt!(multispace)), + expr, + delimited!(opt!(multispace), tag!(")"), opt!(multispace)) + ) +); + +named!(factor, alt!( + map_res!( + map_res!( + delimited!(opt!(multispace), digit, opt!(multispace)), + str::from_utf8 + ), + FromStr::from_str + ) + | parens + ) +); + +named!(term , chain!( + mut acc: factor ~ + many0!( + alt!( + tap!(mul: preceded!(tag!("*"), factor) => acc = acc * mul) | + tap!(div: preceded!(tag!("/"), factor) => acc = acc / div) + ) + ), + || { return acc } + ) +); + +named!(expr , chain!( + mut acc: term ~ + many0!( + alt!( + tap!(add: preceded!(tag!("+"), term) => acc = acc + add) | + tap!(sub: preceded!(tag!("-"), term) => acc = acc - sub) + ) + ), + || { return acc } + ) +); + +#[test] +fn factor_test() { + assert_eq!(factor(&b"3"[..]), IResult::Done(&b""[..], 3)); + assert_eq!(factor(&b" 12"[..]), IResult::Done(&b""[..], 12)); + assert_eq!(factor(&b"537 "[..]), IResult::Done(&b""[..], 537)); + assert_eq!(factor(&b" 24 "[..]), IResult::Done(&b""[..], 24)); +} + + +#[test] +fn term_test() { + assert_eq!(term(&b" 12 *2 / 3"[..]), IResult::Done(&b""[..], 8)); + assert_eq!(term(&b" 2* 3 *2 *2 / 3"[..]), IResult::Done(&b""[..], 8)); + assert_eq!(term(&b" 48 / 3/2"[..]), IResult::Done(&b""[..], 8)); +} + +#[test] +fn expr_test() { + assert_eq!(expr(&b" 1 + 2 "[..]), IResult::Done(&b""[..], 3)); + assert_eq!(expr(&b" 12 + 6 - 4+ 3"[..]), IResult::Done(&b""[..], 17)); + assert_eq!(expr(&b" 1 + 2*3 + 4"[..]), IResult::Done(&b""[..], 11)); +} + +#[test] +fn parens_test() { + assert_eq!(expr(&b" ( 2 )"[..]), IResult::Done(&b""[..], 2)); + assert_eq!(expr(&b" 2* ( 3 + 4 ) "[..]), IResult::Done(&b""[..], 14)); + assert_eq!(expr(&b" 2*2 / ( 5 - 1) + 3"[..]), IResult::Done(&b""[..], 4)); +} diff --git third_party/rust/nom-1.2.4/tests/arithmetic_ast.rs third_party/rust/nom-1.2.4/tests/arithmetic_ast.rs new file mode 100644 index 000000000000..9a8956936ff8 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/arithmetic_ast.rs @@ -0,0 +1,137 @@ +#[macro_use] +extern crate nom; + +use std::fmt; +use std::fmt::{Display, Debug, Formatter}; + +use std::str; +use std::str::FromStr; + +use nom::{IResult, digit, multispace}; + +pub enum Expr { + Value(i64), + Add(Box, Box), + Sub(Box, Box), + Mul(Box, Box), + Div(Box, Box), + Paren(Box), +} + +pub enum Oper { + Add, + Sub, + Mul, + Div, +} + +impl Display for Expr { + fn fmt(&self, format: &mut Formatter) -> fmt::Result { + use self::Expr::*; + match *self { + Value(val) => write!(format, "{}", val), + Add(ref left, ref right) => write!(format, "{} + {}", left, right), + Sub(ref left, ref right) => write!(format, "{} - {}", left, right), + Mul(ref left, ref right) => write!(format, "{} * {}", left, right), + Div(ref left, ref right) => write!(format, "{} / {}", left, right), + Paren(ref expr) => write!(format, "({})", expr), + } + } +} + +impl Debug for Expr { + fn fmt(&self, format: &mut Formatter) -> fmt::Result { + use self::Expr::*; + match *self { + Value(val) => write!(format, "{}", val), + Add(ref left, ref right) => write!(format, "({:?} + {:?})", left, right), + Sub(ref left, ref right) => write!(format, "({:?} - {:?})", left, right), + Mul(ref left, ref right) => write!(format, "({:?} * {:?})", left, right), + Div(ref left, ref right) => write!(format, "({:?} / {:?})", left, right), + Paren(ref expr) => write!(format, "[{:?}]", expr), + } + } +} + +named!(parens< Expr >, delimited!( + delimited!(opt!(multispace), tag!("("), opt!(multispace)), + map!(map!(expr, Box::new), Expr::Paren), + delimited!(opt!(multispace), tag!(")"), opt!(multispace)) + ) +); + +named!(factor< Expr >, alt_complete!( + map!( + map_res!( + map_res!( + delimited!(opt!(multispace), digit, opt!(multispace)), + str::from_utf8 + ), + FromStr::from_str + ), + Expr::Value) + | parens + ) +); + +fn fold_exprs(initial: Expr, remainder: Vec<(Oper, Expr)>) -> Expr { + remainder.into_iter().fold(initial, |acc, pair| { + let (oper, expr) = pair; + match oper { + Oper::Add => Expr::Add(Box::new(acc), Box::new(expr)), + Oper::Sub => Expr::Sub(Box::new(acc), Box::new(expr)), + Oper::Mul => Expr::Mul(Box::new(acc), Box::new(expr)), + Oper::Div => Expr::Div(Box::new(acc), Box::new(expr)), + } + }) +} + +named!(term< Expr >, chain!( + initial: factor ~ + remainder: many0!( + alt!( + chain!(tag!("*") ~ mul: factor, || { (Oper::Mul, mul) }) | + chain!(tag!("/") ~ div: factor, || { (Oper::Div, div) }) + ) + ), + || fold_exprs(initial, remainder)) +); + +named!(expr< Expr >, chain!( + initial: term ~ + remainder: many0!( + alt!( + chain!(tag!("+") ~ add: term, || { (Oper::Add, add) }) | + chain!(tag!("-") ~ sub: term, || { (Oper::Sub, sub) }) + ) + ), + || fold_exprs(initial, remainder)) +); + +#[test] +fn factor_test() { + assert_eq!(factor(&b" 3 "[..]).map(|x| format!("{:?}", x)), + IResult::Done(&b""[..], String::from("3"))); +} + +#[test] +fn term_test() { + assert_eq!(term(&b" 3 * 5 "[..]).map(|x| format!("{:?}", x)), + IResult::Done(&b""[..], String::from("(3 * 5)"))); +} + +#[test] +fn expr_test() { + assert_eq!(expr(&b" 1 + 2 * 3 "[..]).map(|x| format!("{:?}", x)), + IResult::Done(&b""[..], String::from("(1 + (2 * 3))"))); + assert_eq!(expr(&b" 1 + 2 * 3 / 4 - 5 "[..]).map(|x| format!("{:?}", x)), + IResult::Done(&b""[..], String::from("((1 + ((2 * 3) / 4)) - 5)"))); + assert_eq!(expr(&b" 72 / 2 / 3 "[..]).map(|x| format!("{:?}", x)), + IResult::Done(&b""[..], String::from("((72 / 2) / 3)"))); +} + +#[test] +fn parens_test() { + assert_eq!(expr(&b" ( 1 + 2 ) * 3 "[..]).map(|x| format!("{:?}", x)), + IResult::Done(&b""[..], String::from("([(1 + 2)] * 3)"))); +} diff --git third_party/rust/nom-1.2.4/tests/cross_function_backtracking.rs third_party/rust/nom-1.2.4/tests/cross_function_backtracking.rs new file mode 100644 index 000000000000..592670c43999 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/cross_function_backtracking.rs @@ -0,0 +1,140 @@ +/// this file tests a different backtracking behaviour. With the current +/// `error!` macro, an early return is done in the current function, but +/// backtracking continues normally outside of that function. +/// +/// The solution here wraps `IResult` in a `Result`: a `Ok` indicates usual +/// backtracking, `Err` indicates that we must "cut". + +#[macro_use] +extern crate nom; + +macro_rules! n ( + ($name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: $i ) -> std::result::Result, nom::Err<$i, u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + ($name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: $i ) -> std::result::Result, nom::Err<$i, $e>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + ($name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: $i ) -> std::result::Result, nom::Err<$i, u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + ($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( i: &'a[u8] ) -> std::result::Result, nom::Err<&'a [u8], u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + ($name:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name( i: &[u8] ) -> std::result::Result, nom::Err<&[u8], u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + (pub $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: $i ) -> std::result::Result, nom::Err<$i, u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + (pub $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: $i ) -> std::result::Result, nom::Err<$i, $e>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + (pub $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: $i ) -> std::result::Result, nom::Err<$i, u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + (pub $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + pub fn $name( i: &[u8] ) -> std::result::Result, nom::Err<&[u8], u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); + (pub $name:ident, $submac:ident!( $($args:tt)* )) => ( + pub fn $name<'a>( i: &'a [u8] ) -> std::result::Result, nom::Err<&[u8], u32>> { + std::result::Result::Ok($submac!(i, $($args)*)) + } + ); +); + +macro_rules! cut ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + let cl = || { + Ok($submac!($i, $($args)*)) + }; + + match cl() { + std::result::Result::Ok(nom::IResult::Incomplete(x)) => nom::IResult::Incomplete(x), + std::result::Result::Ok(nom::IResult::Done(i, o)) => nom::IResult::Done(i, o), + std::result::Result::Ok(nom::IResult::Error(e)) | std::result::Result::Err(e) => { + return std::result::Result::Err(nom::Err::NodePosition($code, $i, Box::new(e))) + } + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + cut!($i, $code, call!($f)); + ); +); + +macro_rules! c ( + ($i:expr, $f:expr) => ( + { + match $f($i) { + std::result::Result::Ok(nom::IResult::Incomplete(x)) => nom::IResult::Incomplete(x), + std::result::Result::Ok(nom::IResult::Done(i, o)) => nom::IResult::Done(i, o), + std::result::Result::Ok(nom::IResult::Error(e)) => nom::IResult::Error(e), + std::result::Result::Err(e) => { + return std::result::Result::Err(e) + } + } + } + ); +); + +n!(pub foo< bool >, + chain!( + tag!("a") ~ + cut!(nom::ErrorKind::Custom(42),dbg_dmp!(tag!("b"))) , + || { true } + ) +); + +n!(pub foos< Vec >, + delimited!( + tag!("("), + many0!(c!(foo)), + tag!(")") + ) +); + +#[test] +fn test_ok() { + let r = foos(b"(abab)"); + println!("result: {:?}", r); + match r { + Ok(nom::IResult::Done(_,result)) => assert_eq!(result,vec![true,true]), + res => panic!("Oops {:?}.",res) + } +} + +#[test] +fn test_err() { + let input = b"(ac)"; + let r = foos(&input[..]); + println!("result: {:?}", r); + match r { + //Ok(nom::IResult::Error(nom::Err::Position(kind,_))) => assert_eq!(kind,nom::ErrorKind::Custom(42)), + Err(nom::Err::NodePosition(kind, position, _)) => { + assert_eq!(kind, nom::ErrorKind::Custom(42)); + assert_eq!(position, &input[2..]); + } + res => panic!("Oops, {:?}",res) + } +} + diff --git third_party/rust/nom-1.2.4/tests/ini.rs third_party/rust/nom-1.2.4/tests/ini.rs new file mode 100644 index 000000000000..a3a33431bf02 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/ini.rs @@ -0,0 +1,234 @@ + +#[macro_use] +extern crate nom; + +use nom::{IResult,not_line_ending, space, alphanumeric, multispace}; + +use std::str; +use std::collections::HashMap; + +named!(category<&str>, map_res!( + terminated!( + delimited!(tag!("["), take_until!("]"), tag!("]")), + opt!(multispace) + ), + str::from_utf8 +)); + +named!(key_value <&[u8],(&str,&str)>, + chain!( + key: map_res!(alphanumeric, std::str::from_utf8) ~ + space? ~ + tag!("=") ~ + space? ~ + val: map_res!( + take_until_either!("\n;"), + str::from_utf8 + ) ~ + space? ~ + chain!( + tag!(";") ~ + not_line_ending , + ||{} + ) ? ~ + multispace? , + ||{(key, val)} + ) +); + + +named!(keys_and_values_aggregator<&[u8], Vec<(&str,&str)> >, many0!(key_value)); + +fn keys_and_values(input:&[u8]) -> IResult<&[u8], HashMap<&str, &str> > { + let mut h: HashMap<&str, &str> = HashMap::new(); + + match keys_and_values_aggregator(input) { + IResult::Done(i,tuple_vec) => { + for &(k,v) in &tuple_vec { + h.insert(k, v); + } + IResult::Done(i, h) + }, + IResult::Incomplete(a) => IResult::Incomplete(a), + IResult::Error(a) => IResult::Error(a) + } +} + +named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>, + chain!( + category: category ~ + keys: keys_and_values , + move ||{(category, keys)} + ) +); + +named!(categories_aggregator<&[u8], Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys)); + +fn categories(input: &[u8]) -> IResult<&[u8], HashMap<&str, HashMap<&str, &str> > > { + let mut h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); + + match categories_aggregator(input) { + IResult::Done(i,tuple_vec) => { + for &(k,ref v) in &tuple_vec { + h.insert(k, v.clone()); + } + IResult::Done(i, h) + }, + IResult::Incomplete(a) => IResult::Incomplete(a), + IResult::Error(a) => IResult::Error(a) + } +} + +#[test] +fn parse_category_test() { + let ini_file = &b"[category] + +parameter=value +key = value2"[..]; + + let ini_without_category = &b"parameter=value +key = value2"[..]; + + let res = category(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_category, "category")); +} + +#[test] +fn parse_key_value_test() { + let ini_file = &b"parameter=value +key = value2"[..]; + + let ini_without_key_value = &b"key = value2"[..]; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value"))); +} + + +#[test] +fn parse_key_value_with_space_test() { + let ini_file = &b"parameter = value +key = value2"[..]; + + let ini_without_key_value = &b"key = value2"[..]; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value"))); +} + +#[test] +fn parse_key_value_with_comment_test() { + let ini_file = &b"parameter=value;abc +key = value2"[..]; + + let ini_without_key_value = &b"key = value2"[..]; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value"))); +} + +#[test] +fn parse_multiple_keys_and_values_test() { + let ini_file = &b"parameter=value;abc + +key = value2 + +[category]"[..]; + + let ini_without_key_value = &b"[category]"[..]; + + let res = keys_and_values(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error") + } + + let mut expected: HashMap<&str, &str> = HashMap::new(); + expected.insert("parameter", "value"); + expected.insert("key", "value2"); + assert_eq!(res, IResult::Done(ini_without_key_value, expected)); +} + +#[test] +fn parse_category_then_multiple_keys_and_values_test() { + //FIXME: there can be an empty line or a comment line after a category + let ini_file = &b"[abcd] +parameter=value;abc + +key = value2 + +[category]"[..]; + + let ini_after_parser = &b"[category]"[..]; + + let res = category_and_keys(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error") + } + + let mut expected_h: HashMap<&str, &str> = HashMap::new(); + expected_h.insert("parameter", "value"); + expected_h.insert("key", "value2"); + assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h))); +} + +#[test] +fn parse_multiple_categories_test() { + let ini_file = &b"[abcd] + +parameter=value;abc + +key = value2 + +[category] +parameter3=value3 +key4 = value4 +"[..]; + + let ini_after_parser = &b""[..]; + + let res = categories(ini_file); + //println!("{:?}", res); + match res { + IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o), + _ => println!("error") + } + + let mut expected_1: HashMap<&str, &str> = HashMap::new(); + expected_1.insert("parameter", "value"); + expected_1.insert("key", "value2"); + let mut expected_2: HashMap<&str, &str> = HashMap::new(); + expected_2.insert("parameter3", "value3"); + expected_2.insert("key4", "value4"); + let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); + expected_h.insert("abcd", expected_1); + expected_h.insert("category", expected_2); + assert_eq!(res, IResult::Done(ini_after_parser, expected_h)); +} diff --git third_party/rust/nom-1.2.4/tests/ini_str.rs third_party/rust/nom-1.2.4/tests/ini_str.rs new file mode 100644 index 000000000000..c69756dc2aca --- /dev/null +++ third_party/rust/nom-1.2.4/tests/ini_str.rs @@ -0,0 +1,251 @@ + +#[macro_use] +extern crate nom; + +use nom::IResult; + +use std::collections::HashMap; + +fn is_alphabetic(chr:char) -> bool { + (chr as u8 >= 0x41 && chr as u8 <= 0x5A) || (chr as u8 >= 0x61 && chr as u8 <= 0x7A) +} + +fn is_digit(chr: char) -> bool { + chr as u8 >= 0x30 && chr as u8 <= 0x39 +} + +fn is_alphanumeric(chr: char) -> bool { + is_alphabetic(chr) || is_digit(chr) +} + +fn is_space(chr:char) -> bool { + chr == ' ' || chr == '\t' +} + +fn is_line_ending_or_comment(chr:char) -> bool { + chr == ';' || chr == '\n' +} + +named!(alphanumeric<&str,&str>, take_while_s!(is_alphanumeric)); +named!(not_line_ending<&str,&str>, is_not_s!("\r\n")); +named!(space<&str,&str>, take_while_s!(is_space)); +named!(space_or_line_ending<&str,&str>, is_a_s!(" \r\n")); + +fn right_bracket(c:char) -> bool { + c == ']' +} + +named!(category <&str, &str>, + chain!( + tag_s!("[") ~ + name: take_till_s!(right_bracket) ~ + tag_s!("]") ~ + space_or_line_ending? , + ||{ name } + ) +); + +named!(key_value <&str,(&str,&str)>, + chain!( + key: alphanumeric ~ + space? ~ + tag_s!("=") ~ + space? ~ + val: take_till_s!(is_line_ending_or_comment) ~ + space? ~ + pair!(tag_s!(";"), not_line_ending)? ~ + space_or_line_ending? , + ||{(key, val)} + ) +); + +named!(keys_and_values_aggregator<&str, Vec<(&str,&str)> >, many0!(key_value)); + +fn keys_and_values(input:&str) -> IResult<&str, HashMap<&str, &str> > { + let mut h: HashMap<&str, &str> = HashMap::new(); + + match keys_and_values_aggregator(input) { + IResult::Done(i,tuple_vec) => { + for &(k,v) in &tuple_vec { + h.insert(k, v); + } + IResult::Done(i, h) + }, + IResult::Incomplete(a) => IResult::Incomplete(a), + IResult::Error(a) => IResult::Error(a) + } +} + + +named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>, + pair!(category, keys_and_values) +); + +named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys)); + +fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str> > > { + let mut h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); + + match categories_aggregator(input) { + IResult::Done(i,tuple_vec) => { + for &(k,ref v) in &tuple_vec { + h.insert(k, v.clone()); + } + IResult::Done(i, h) + }, + IResult::Incomplete(a) => IResult::Incomplete(a), + IResult::Error(a) => IResult::Error(a) + } +} + + +#[test] +fn parse_category_test() { + let ini_file = "[category] + +parameter=value +key = value2"; + + let ini_without_category = "parameter=value +key = value2"; + + let res = category(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, o) => println!("i: {} | o: {:?}", i, o), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_category, "category")); +} + +#[test] +fn parse_key_value_test() { + let ini_file = "parameter=value +key = value2"; + + let ini_without_key_value = "key = value2"; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value"))); +} + +#[test] +fn parse_key_value_with_space_test() { + let ini_file = "parameter = value +key = value2"; + + let ini_without_key_value = "key = value2"; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value"))); +} + +#[test] +fn parse_key_value_with_comment_test() { + let ini_file = "parameter=value;abc +key = value2"; + + let ini_without_key_value = "key = value2"; + + let res = key_value(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2), + _ => println!("error") + } + + assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value"))); +} + +#[test] +fn parse_multiple_keys_and_values_test() { + let ini_file = "parameter=value;abc + +key = value2 + +[category]"; + + let ini_without_key_value = "[category]"; + + let res = keys_and_values(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o), + _ => println!("error") + } + + let mut expected: HashMap<&str, &str> = HashMap::new(); + expected.insert("parameter", "value"); + expected.insert("key", "value2"); + assert_eq!(res, IResult::Done(ini_without_key_value, expected)); +} + +#[test] +fn parse_category_then_multiple_keys_and_values_test() { + //FIXME: there can be an empty line or a comment line after a category + let ini_file = "[abcd] +parameter=value;abc + +key = value2 + +[category]"; + + let ini_after_parser = "[category]"; + + let res = category_and_keys(ini_file); + println!("{:?}", res); + match res { + IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o), + _ => println!("error") + } + + let mut expected_h: HashMap<&str, &str> = HashMap::new(); + expected_h.insert("parameter", "value"); + expected_h.insert("key", "value2"); + assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h))); +} + +#[test] +fn parse_multiple_categories_test() { + let ini_file = "[abcd] + +parameter=value;abc + +key = value2 + +[category] +parameter3=value3 +key4 = value4 +"; + + let res = categories(ini_file); + //println!("{:?}", res); + match res { + IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o), + _ => println!("error") + } + + let mut expected_1: HashMap<&str, &str> = HashMap::new(); + expected_1.insert("parameter", "value"); + expected_1.insert("key", "value2"); + let mut expected_2: HashMap<&str, &str> = HashMap::new(); + expected_2.insert("parameter3", "value3"); + expected_2.insert("key4", "value4"); + let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); + expected_h.insert("abcd", expected_1); + expected_h.insert("category", expected_2); + assert_eq!(res, IResult::Done("", expected_h)); +} diff --git third_party/rust/nom-1.2.4/tests/issues.rs third_party/rust/nom-1.2.4/tests/issues.rs new file mode 100644 index 000000000000..6466d7f2e069 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/issues.rs @@ -0,0 +1,131 @@ +//#![feature(trace_macros)] +#[macro_use] +extern crate nom; + +use nom::{IResult,Needed,HexDisplay,space,digit,be_u16}; +use std::str; + +#[allow(dead_code)] +struct Range { + start: char, + end: char +} + +pub fn take_char(input: &[u8]) -> IResult<&[u8], char> { + if input.len() > 0 { + IResult::Done(&input[1..], input[0] as char) + } else { + IResult::Incomplete(Needed::Size(1)) + } +} + +//trace_macros!(true); + +#[allow(dead_code)] +named!(range<&[u8], Range>, + alt!( + chain!( + start: take_char ~ + tag!("-") ~ + end: take_char, + || { + Range { + start: start, + end: end, + } + } + ) | + map!( + take_char, + |c| { + Range { + start: c, + end: c, + } + } + ) + ) +); + + +#[allow(dead_code)] +named!(literal<&[u8], Vec >, + map!( + many1!(take_char), + |cs| { + cs + } + ) +); + +#[test] +fn issue_58() { + range(&b"abcd"[..]); + literal(&b"abcd"[..]); +} + +//trace_macros!(false); + +named!(parse_ints< Vec >, many0!(spaces_or_int)); + +fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>{ + println!("{}", input.to_hex(8)); + chain!(input, + opt!(space) ~ + x: digit, + || { + println!("x: {:?}", x); + let result = str::from_utf8(x).unwrap(); + println!("Result: {}", result); + println!("int is empty?: {}", x.is_empty()); + match result.parse(){ + Ok(i) => i, + Err(_) => panic!("UH OH! NOT A DIGIT!") + } + } + ) +} + +#[test] +fn issue_142(){ + let subject = parse_ints(&b"12 34 5689"[..]); + let expected = IResult::Done(&b""[..], vec![12, 34, 5689]); + assert_eq!(subject, expected); + + let subject = parse_ints(&b"12 34 5689 "[..]); + let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]); + assert_eq!(subject, expected) +} + +#[test] +fn usize_length_bytes_issue(){ + length_bytes!(b"012346", be_u16); +} + +/* + DOES NOT COMPILE +#[test] +fn issue_152() { + named!(take4, take!(4)); + named!(xyz, tag!("XYZ")); + named!(abc, tag!("abc")); + + + named!(sw, + switch!(take4, + b"abcd" => xyz | + b"efgh" => abc + ) + ); +} +*/ + +#[test] +fn take_till_issue() { + named!(nothing, + take_till!(call!(|_| true)) + ); + + assert_eq!(nothing(b""), IResult::Done(&b""[..], &b""[..])); + assert_eq!(nothing(b"abc"), IResult::Done(&b"abc"[..], &b""[..])); +} diff --git third_party/rust/nom-1.2.4/tests/mp4.rs third_party/rust/nom-1.2.4/tests/mp4.rs new file mode 100644 index 000000000000..8c128f57a928 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/mp4.rs @@ -0,0 +1,531 @@ +#![cfg(feature = "stream")] +#![allow(dead_code)] + +#[macro_use] +extern crate nom; + +use nom::{HexDisplay,Needed,IResult,be_u16,be_u32,be_u64,be_f32,ErrorKind}; +use nom::{Consumer,ConsumerState,Move,Input,Producer,FileProducer,FileProducerState}; +use nom::IResult::*; +use nom::Err::*; + +use std::str; +use std::io::SeekFrom; + +fn mp4_box(input:&[u8]) -> IResult<&[u8], &[u8]> { + match be_u32(input) { + Done(i, offset) => { + let sz: usize = offset as usize; + if i.len() >= sz - 4 { + Done(&i[(sz-4)..], &i[0..(sz-4)]) + } else { + Incomplete(Needed::Size(offset as usize + 4)) + } + } + Error(e) => Error(e), + Incomplete(e) => Incomplete(e) + } +} + +#[derive(PartialEq,Eq,Debug)] +struct FileType<'a> { + major_brand: &'a str, + major_brand_version: &'a [u8], + compatible_brands: Vec<&'a str> +} + +#[allow(non_snake_case)] +#[derive(Debug,Clone)] +pub struct Mvhd32 { + version_flags: u32, // actually: + // version: u8, + // flags: u24 // 3 bytes + created_date: u32, + modified_date: u32, + scale: u32, + duration: u32, + speed: f32, + volume: u16, // actually a 2 bytes decimal + /* 10 bytes reserved */ + scaleA: f32, + rotateB: f32, + angleU: f32, + rotateC: f32, + scaleD: f32, + angleV: f32, + positionX: f32, + positionY: f32, + scaleW: f32, + preview: u64, + poster: u32, + selection: u64, + current_time: u32, + track_id: u32 +} + +#[allow(non_snake_case)] +#[derive(Debug,Clone)] +pub struct Mvhd64 { + version_flags: u32, // actually: + // version: u8, + // flags: u24 // 3 bytes + created_date: u64, + modified_date: u64, + scale: u32, + duration: u64, + speed: f32, + volume: u16, // actually a 2 bytes decimal + /* 10 bytes reserved */ + scaleA: f32, + rotateB: f32, + angleU: f32, + rotateC: f32, + scaleD: f32, + angleV: f32, + positionX: f32, + positionY: f32, + scaleW: f32, + preview: u64, + poster: u32, + selection: u64, + current_time: u32, + track_id: u32 +} + +#[allow(non_snake_case)] +named!(mvhd32 <&[u8], MvhdBox>, + chain!( + version_flags: be_u32 ~ + created_date: be_u32 ~ + modified_date: be_u32 ~ + scale: be_u32 ~ + duration: be_u32 ~ + speed: be_f32 ~ + volume: be_u16 ~ // actually a 2 bytes decimal + take!(10) ~ + scale_a: be_f32 ~ + rotate_b: be_f32 ~ + angle_u: be_f32 ~ + rotate_c: be_f32 ~ + scale_d: be_f32 ~ + angle_v: be_f32 ~ + position_x: be_f32 ~ + position_y: be_f32 ~ + scale_w: be_f32 ~ + preview: be_u64 ~ + poster: be_u32 ~ + selection: be_u64 ~ + current_time: be_u32 ~ + track_id: be_u32, + ||{ + MvhdBox::M32(Mvhd32 { + version_flags: version_flags, + created_date: created_date, + modified_date: modified_date, + scale: scale, + duration: duration, + speed: speed, + volume: volume, + scaleA: scale_a, + rotateB: rotate_b, + angleU: angle_u, + rotateC: rotate_c, + scaleD: scale_d, + angleV: angle_v, + positionX: position_x, + positionY: position_y, + scaleW: scale_w, + preview: preview, + poster: poster, + selection: selection, + current_time: current_time, + track_id: track_id + }) + } + ) +); + +#[allow(non_snake_case)] +named!(mvhd64 <&[u8], MvhdBox>, + chain!( + version_flags: be_u32 ~ + created_date: be_u64 ~ + modified_date: be_u64 ~ + scale: be_u32 ~ + duration: be_u64 ~ + speed: be_f32 ~ + volume: be_u16 ~ // actually a 2 bytes decimal + take!(10) ~ + scale_a: be_f32 ~ + rotate_b: be_f32 ~ + angle_u: be_f32 ~ + rotate_c: be_f32 ~ + scale_d: be_f32 ~ + angle_v: be_f32 ~ + position_x: be_f32 ~ + position_y: be_f32 ~ + scale_w: be_f32 ~ + preview: be_u64 ~ + poster: be_u32 ~ + selection: be_u64 ~ + current_time: be_u32 ~ + track_id: be_u32, + ||{ + MvhdBox::M64(Mvhd64 { + version_flags: version_flags, + created_date: created_date, + modified_date: modified_date, + scale: scale, + duration: duration, + speed: speed, + volume: volume, + scaleA: scale_a, + rotateB: rotate_b, + angleU: angle_u, + rotateC: rotate_c, + scaleD: scale_d, + angleV: angle_v, + positionX: position_x, + positionY: position_y, + scaleW: scale_w, + preview: preview, + poster: poster, + selection: selection, + current_time: current_time, + track_id: track_id + }) + } + ) +); + +#[derive(Debug,Clone)] +pub enum MvhdBox { + M32(Mvhd32), + M64(Mvhd64) +} + +#[derive(Debug,Clone)] +pub enum MoovBox { + Mdra, + Dref, + Cmov, + Rmra, + Iods, + Mvhd(MvhdBox), + Clip, + Trak, + Udta +} + +#[derive(Debug)] +enum MP4BoxType { + Ftyp, + Moov, + Mdat, + Free, + Skip, + Wide, + Mdra, + Dref, + Cmov, + Rmra, + Iods, + Mvhd, + Clip, + Trak, + Udta, + Unknown +} + +#[derive(Debug)] +struct MP4BoxHeader { + length: u32, + tag: MP4BoxType +} + +named!(brand_name<&[u8],&str>, map_res!(take!(4), str::from_utf8)); + +named!(filetype_parser<&[u8], FileType>, + chain!( + m: brand_name ~ + v: take!(4) ~ + c: many0!(brand_name) , + ||{ FileType{ major_brand: m, major_brand_version:v, compatible_brands: c } } + ) +); + +fn mvhd_box(input:&[u8]) -> IResult<&[u8],MvhdBox> { + let res = if input.len() < 100 { + Incomplete(Needed::Size(100)) + } else if input.len() == 100 { + mvhd32(input) + } else if input.len() == 112 { + mvhd64(input) + } else { + Error(Position(ErrorKind::Custom(32),input)) + }; + println!("res: {:?}", res); + res +} + +fn unknown_box_type(input:&[u8]) -> IResult<&[u8], MP4BoxType> { + Done(input, MP4BoxType::Unknown) +} + +//named!(box_type<&[u8], MP4BoxType>, +fn box_type(input: &[u8]) -> IResult<&[u8], MP4BoxType, u32> { + alt!(input, + tag!("ftyp") => { |_| MP4BoxType::Ftyp } | + tag!("moov") => { |_| MP4BoxType::Moov } | + tag!("mdat") => { |_| MP4BoxType::Mdat } | + tag!("free") => { |_| MP4BoxType::Free } | + tag!("skip") => { |_| MP4BoxType::Skip } | + tag!("wide") => { |_| MP4BoxType::Wide } | + unknown_box_type + ) +} + +// warning, an alt combinator with 9 branches containing a tag combinator +// can make the compilation very slow. Use functions as sub parsers, +// or split into multiple alt! parsers if it gets slow +named!(moov_type<&[u8], MP4BoxType>, + alt!( + tag!("mdra") => { |_| MP4BoxType::Mdra } | + tag!("dref") => { |_| MP4BoxType::Dref } | + tag!("cmov") => { |_| MP4BoxType::Cmov } | + tag!("rmra") => { |_| MP4BoxType::Rmra } | + tag!("iods") => { |_| MP4BoxType::Iods } | + tag!("mvhd") => { |_| MP4BoxType::Mvhd } | + tag!("clip") => { |_| MP4BoxType::Clip } | + tag!("trak") => { |_| MP4BoxType::Trak } | + tag!("udta") => { |_| MP4BoxType::Udta } + ) +); + +named!(box_header<&[u8],MP4BoxHeader>, + chain!( + length: be_u32 ~ + tag: box_type , + || { MP4BoxHeader{ length: length, tag: tag} } + ) +); + +named!(moov_header<&[u8],MP4BoxHeader>, + chain!( + length: be_u32 ~ + tag: moov_type , + || { MP4BoxHeader{ length: length, tag: tag} } + ) +); + +#[derive(Debug,PartialEq,Eq)] +enum MP4State { + Main, + Moov, + Mvhd(usize) +} + +pub struct MP4Consumer { + state: MP4State, + moov_bytes: usize, + c_state: ConsumerState<(), (), Move> +} + +impl MP4Consumer { + fn new() -> MP4Consumer { + MP4Consumer { state: MP4State::Main, moov_bytes: 0, c_state: ConsumerState::Continue(Move::Consume(0)) } + } + + fn consume_main(&mut self, input: Input<&[u8]>) -> ConsumerState<(), (), Move> { + //println!("\nparsing box header:\n{}", input.to_hex(8)); + match input { + Input::Eof(None) => ConsumerState::Done(Move::Consume(0), ()), + Input::Empty => ConsumerState::Continue(Move::Consume(0)), + Input::Element(sl) | Input::Eof(Some(sl)) => { + match box_header(sl) { + Done(i, header) => { + match header.tag { + MP4BoxType::Ftyp => { + println!("-> FTYP"); + match filetype_parser(&i[0..(header.length as usize - 8)]) { + Done(rest, filetype_header) => { + println!("filetype header: {:?}", filetype_header); + //return ConsumerState::Await(header.length as usize, header.length as usize - 8); + return ConsumerState::Continue(Move::Consume(sl.offset(rest))); + } + Error(a) => { + println!("ftyp parsing error: {:?}", a); + assert!(false); + return ConsumerState::Error(()); + }, + Incomplete(n) => { + println!("ftyp incomplete -> await: {}", sl.len()); + return ConsumerState::Continue(Move::Await(n)); + //return ConsumerState::Await(0, input.len() + 100); + } + } + }, + MP4BoxType::Moov => { + println!("-> MOOV"); + self.state = MP4State::Moov; + self.moov_bytes = header.length as usize - 8; + return ConsumerState::Continue(Move::Consume(sl.offset(i))); + }, + MP4BoxType::Mdat => println!("-> MDAT"), + MP4BoxType::Free => println!("-> FREE"), + MP4BoxType::Skip => println!("-> SKIP"), + MP4BoxType::Wide => println!("-> WIDE"), + MP4BoxType::Unknown => { + println!("-> UNKNOWN"); + println!("bytes:\n{}", (sl).to_hex(8)); + //return ConsumerState::Continue(Move::Consume(sl.offset(i))); + }, + _ => { println!("invalid"); return ConsumerState::Error(())} + } + return ConsumerState::Continue(Move::Seek(SeekFrom::Current((header.length) as i64))) + }, + Error(a) => { + println!("mp4 parsing error: {:?}", a); + assert!(false); + return ConsumerState::Error(()); + }, + Incomplete(i) => { + // FIXME: incomplete should send the required size + println!("mp4 incomplete -> await: {}", sl.len()); + return ConsumerState::Continue(Move::Await(i)); + } + } + } + } + } + + fn consume_moov(&mut self, input: Input<&[u8]>) -> ConsumerState<(), (), Move> { + //println!("\nparsing moov box(remaining {} bytes):\n{}", self.moov_bytes, input.to_hex(8)); + match input { + Input::Eof(None) => return ConsumerState::Error(()), + Input::Empty => return ConsumerState::Continue(Move::Consume(0)), + Input::Element(sl) | Input::Eof(Some(sl)) => { + if self.moov_bytes == 0 { + //println!("finished parsing moov atom, continuing with main parser"); + self.state = MP4State::Main; + return ConsumerState::Continue(Move::Consume(0)); + } + match moov_header(sl) { + Done(i, header) => { + match header.tag { + MP4BoxType::Mvhd => { + println!("-> MVHD"); + self.state = MP4State::Mvhd(header.length as usize - 8); + // TODO: check for overflow here + self.moov_bytes = self.moov_bytes - (sl.len() - i.len()); + println!("remaining moov_bytes: {}", self.moov_bytes); + return ConsumerState::Continue(Move::Consume(sl.offset(i))); + }, + MP4BoxType::Wide => println!("-> WIDE"), + MP4BoxType::Mdra => println!("-> MDRA"), + MP4BoxType::Dref => println!("-> DREF"), + MP4BoxType::Cmov => println!("-> CMOV"), + MP4BoxType::Rmra => println!("-> RMRA"), + MP4BoxType::Iods => println!("-> IODS"), + MP4BoxType::Clip => println!("-> CLIP"), + MP4BoxType::Trak => println!("-> TRAK"), + MP4BoxType::Udta => println!("-> UDTA"), + MP4BoxType::Unknown => println!("-> MOOV UNKNOWN"), + _ => { println!("invalid header here: {:?}", header.tag); return ConsumerState::Error(());} + }; + // TODO: check for overflow here + self.moov_bytes = self.moov_bytes - header.length as usize; + println!("remaining moov_bytes: {}", self.moov_bytes); + return ConsumerState::Continue(Move::Seek(SeekFrom::Current((header.length) as i64))) + }, + Error(a) => { + println!("moov parsing error: {:?}", a); + println!("data:\n{}", sl.to_hex(8)); + assert!(false); + return ConsumerState::Error(()); + }, + Incomplete(i) => { + println!("moov incomplete -> await: {}", sl.len()); + return ConsumerState::Continue(Move::Await(i)); + } + } + } + }; + } + +} + +consumer_from_parser!(MvhdConsumer, mvhd_box); + +impl<'a> Consumer<&'a[u8], (), (), Move> for MP4Consumer { + fn handle(&mut self, input: Input<&[u8]>) -> &ConsumerState<(), (), Move> { + match self.state { + MP4State::Main => { + self.c_state = self.consume_main(input); + }, + MP4State::Moov => { + self.c_state = self.consume_moov(input); + }, + MP4State::Mvhd(sz) => { + match input { + Input::Eof(None) => self.c_state = ConsumerState::Error(()), + Input::Empty => self.c_state = ConsumerState::Continue(Move::Consume(0)), + Input::Element(sl) | Input::Eof(Some(sl)) => { + let mut c = MvhdConsumer{ state:ConsumerState::Continue(Move::Consume(0)) }; + self.c_state = c.handle(Input::Element(&sl[..sz])).flat_map(|m, _| { + self.state = MP4State::Moov; + ConsumerState::Continue(m) + }); + println!("found mvhd?: {:?}", c.state()); + match self.c_state { + ConsumerState::Continue(Move::Consume(sz)) => self.moov_bytes = self.moov_bytes - sz, + ConsumerState::Continue(Move::Seek(SeekFrom::Current(sz))) => self.moov_bytes = self.moov_bytes - (sz as usize), + _ => () + }; + println!("remaining moov_bytes: {}", self.moov_bytes); + } + } + } + }; + &self.c_state + } + + fn state(&self) -> &ConsumerState<(), (), Move> { + &self.c_state + } +} + +#[allow(unused_must_use)] +fn explore_mp4_file(filename: &str) { + let mut p = FileProducer::new(filename, 400).unwrap(); + let mut c = MP4Consumer{state: MP4State::Main, moov_bytes: 0, c_state: ConsumerState::Continue(Move::Consume(0))}; + //c.run(&mut p); + while let &ConsumerState::Continue(mv) = p.apply(&mut c) { + println!("move: {:?}", mv); + } + println!("last consumer state: {:?} | last state: {:?}", c.c_state, c.state); + + if let ConsumerState::Done(Move::Consume(0), ()) = c.c_state { + println!("consumer state ok"); + } else { + assert!(false, "consumer should have reached Done state"); + } + assert_eq!(c.state, MP4State::Main); + assert_eq!(p.state(), FileProducerState::Eof); + //assert!(false); +} + + +#[test] +fn small_test() { + explore_mp4_file("assets/small.mp4"); +} + + +#[test] +fn big_bunny_test() { + explore_mp4_file("assets/bigbuckbunny.mp4"); +} + + + diff --git third_party/rust/nom-1.2.4/tests/omnom.rs third_party/rust/nom-1.2.4/tests/omnom.rs new file mode 100644 index 000000000000..b8cfa04fb7e4 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/omnom.rs @@ -0,0 +1,160 @@ +#![cfg(feature = "stream")] + +#[macro_use] +extern crate nom; + +use nom::{Producer,Consumer,ConsumerState,Input,Move,MemProducer,IResult,HexDisplay}; + +#[derive(PartialEq,Eq,Debug)] +enum State { + Beginning, + Middle, + End, + Done, + Error +} + +struct TestConsumer { + state: State, + c_state: ConsumerState, + counter: usize, +} + +named!(om_parser, tag!("om")); +named!(nomnom_parser<&[u8],Vec<&[u8]> >, many1!(tag!("nom"))); +named!(end_parser, tag!("kthxbye")); + +impl<'a> Consumer<&'a[u8], usize, (), Move> for TestConsumer { + fn state(&self) -> &ConsumerState { + &self.c_state + } + + fn handle(&mut self, input: Input<&'a [u8]>) -> &ConsumerState { + match self.state { + State::Beginning => { + match input { + Input::Empty | Input::Eof(None) => { + self.state = State::Error; + self.c_state = ConsumerState::Error(()); + }, + Input::Element(sl) | Input::Eof(Some(sl)) => { + match om_parser(sl) { + IResult::Error(_) => { + self.state = State::Error; + self.c_state = ConsumerState::Error(()); + }, + IResult::Incomplete(n) => { + self.c_state = ConsumerState::Continue(Move::Await(n)); + }, + IResult::Done(i,_) => { + self.state = State::Middle; + self.c_state = ConsumerState::Continue(Move::Consume(sl.offset(i))); + } + } + } + } + }, + State::Middle => { + match input { + Input::Empty | Input::Eof(None) => { + self.state = State::Error; + self.c_state = ConsumerState::Error(()); + }, + Input::Element(sl) | Input::Eof(Some(sl)) => { + match nomnom_parser(sl) { + IResult::Error(_) => { + self.state = State::End; + self.c_state = ConsumerState::Continue(Move::Consume(0)); + }, + IResult::Incomplete(n) => { + println!("Middle got Incomplete({:?})", n); + self.c_state = ConsumerState::Continue(Move::Await(n)); + }, + IResult::Done(i,noms_vec) => { + self.counter = self.counter + noms_vec.len(); + self.state = State::Middle; + self.c_state = ConsumerState::Continue(Move::Consume(sl.offset(i))); + } + } + } + } + }, + State::End => { + match input { + Input::Empty | Input::Eof(None) => { + self.state = State::Error; + self.c_state = ConsumerState::Error(()); + }, + Input::Element(sl) | Input::Eof(Some(sl)) => { + match end_parser(sl) { + IResult::Error(_) => { + self.state = State::Error; + self.c_state = ConsumerState::Error(()); + }, + IResult::Incomplete(n) => { + self.c_state = ConsumerState::Continue(Move::Await(n)); + }, + IResult::Done(i,_) => { + self.state = State::Done; + self.c_state = ConsumerState::Done(Move::Consume(sl.offset(i)), self.counter); + } + } + } + } + }, + State::Done | State::Error => { + // this should not be called + self.state = State::Error; + self.c_state = ConsumerState::Error(()) + } + }; + &self.c_state + } +} + +#[test] +fn nom1() { + let mut p = MemProducer::new(&b"omnomkthxbye"[..], 8); + let mut c = TestConsumer{state: State::Beginning, counter: 0, c_state: ConsumerState::Continue(Move::Consume(0))}; + while let &ConsumerState::Continue(Move::Consume(_)) = p.apply(&mut c) { + } + + assert_eq!(c.counter, 1); + assert_eq!(c.state, State::Done); +} + +#[test] +fn nomnomnom() { + let mut p = MemProducer::new(&b"omnomnomnomkthxbye"[..], 9); + let mut c = TestConsumer{state: State::Beginning, counter: 0, c_state: ConsumerState::Continue(Move::Consume(0))}; + while let &ConsumerState::Continue(_) = p.apply(&mut c) { + } + + assert_eq!(c.counter, 3); + assert_eq!(c.state, State::Done); +} + +#[test] +fn no_nomnom() { + let mut p = MemProducer::new(&b"omkthxbye"[..], 8); + let mut c = TestConsumer{state: State::Beginning, counter: 0, c_state: ConsumerState::Continue(Move::Consume(0))}; + while let &ConsumerState::Continue(_) = p.apply(&mut c) { + } + + assert_eq!(c.counter, 0); + assert_eq!(c.state, State::Done); +} + +/* +#[test] +fn impolite() { + let mut p = MemProducer::new(&b"omnomnomnom"[..], 11); + let mut c = TestConsumer{state: State::Beginning, counter: 0, c_state: ConsumerState::Continue(Move::Consume(0))}; + while let &ConsumerState::Continue(cont) = p.apply(&mut c) { + println!("continue {:?}", cont); + } + + assert_eq!(c.counter, 3); + assert_eq!(c.state, State::End); +} +*/ diff --git third_party/rust/nom-1.2.4/tests/test1.rs third_party/rust/nom-1.2.4/tests/test1.rs new file mode 100644 index 000000000000..95f8fd45b6d4 --- /dev/null +++ third_party/rust/nom-1.2.4/tests/test1.rs @@ -0,0 +1,44 @@ +#![cfg(feature = "stream")] + +#[macro_use] +extern crate nom; + +use nom::{IResult,Producer,FileProducer,not_line_ending}; + +use std::str; +use std::fmt::Debug; + +#[test] +#[allow(unused_must_use)] +fn tag() { + FileProducer::new("assets/links.txt", 20).map(|producer: FileProducer| { + let mut p = producer; + p.refill(); + + consumer_from_parser!(PrintConsumer<()>, flat_map!(map_res!(tag!("https!"), str::from_utf8), print)); + let mut cs = PrintConsumer::new(); + for _ in 1..4 { + p.apply(&mut cs); + } + }); +} + +pub fn print(input: T) -> IResult { + println!("{:?}", input); + IResult::Done(input, ()) +} + + +#[test] +fn is_not() { + //is_not!(foo b"\r\n"); + named!(foo<&[u8],&[u8]>, is_not!(&b"\r\n"[..])); + let a = &b"ab12cd\nefgh"[..]; + assert_eq!(foo(a), IResult::Done(&b"\nefgh"[..], &b"ab12cd"[..])); +} + +#[test] +fn exported_public_method_defined_by_macro() { + let a = &b"ab12cd\nefgh"[..]; + assert_eq!(not_line_ending(a), IResult::Done(&b"\nefgh"[..], &b"ab12cd"[..])); +} diff --git third_party/rust/nom/.cargo-checksum.json third_party/rust/nom/.cargo-checksum.json index 9d93bcc0afc2..5baf0864c842 100644 --- third_party/rust/nom/.cargo-checksum.json +++ third_party/rust/nom/.cargo-checksum.json @@ -1 +1 @@ -{"files":{".travis.yml":"6d4e81838b10c5e330749857c72c2f2b1a2e575e71abcd11c094f3b612347b2a","CHANGELOG.md":"d4722e028b2a5b88c466b0d759e463b90bdcfa1b79181a1c76cd313b0a27c615","Cargo.toml":"aebcb999933c3425db85012bea19f9ce78da8e7834dbab54d4a2966e8bc62149","LICENSE":"de730187d5563a81342a3c011d968f78dff37c934fac9b3701e8c762b6118a55","src/bits.rs":"97c9148f63e175489bb6199d039c594ddc56bdf0b7491b9f38b8d74e898bca80","src/bytes.rs":"8f29b976a5e8e6500eb618a9dead7f212688ba9eb06c7066a4016e2db99fed00","src/character.rs":"9ee081f56b508212231ff70d7455b1b85ae44722a39aa60223e8cd95c6570859","src/internal.rs":"ada499b9c178be2a7f9b56319ffb10a778f25fafcda39c78d26b364d89debd72","src/lib.rs":"34efb051214acfde2053e93a7ba718a4fd41b6e0d9edd65a1737605d99b994ab","src/macros.rs":"d39ce3a2cd2b1cb9dd57ce90c06a1ca84720a2dc75e6332cffebba6086cb75d3","src/methods.rs":"24bdbcb0e3570c8bf3fa270dd8d79dd6dfcb982276c82180a89a1e73c5e38019","src/nom.rs":"b0a9c7ce0d09388179bce8f8e23bf57df76b504d925815583c249ec3fc04baab","src/regexp.rs":"8fdae52b761dbad90179e6be87e0e66357fefa34d76af541fb0fcf550fd6ec08","src/str.rs":"198fa15d45c3636289d92c0a592002a07e5a04a431e8cfdf724266e44d484be2","src/stream.rs":"c1bd5b8e7a2061ff66eb2c954033146001f1d65a26d12efa06af8cf93ffa53e4","src/util.rs":"da40ebac865d3176567d3a37b01170234398a03e938553720ce30aa1f6005b6d","tests/arithmetic.rs":"b98936b7fa0228835ca022f6db5342b72a9c01cc3f16a4e05263bbe6424ba3e9","tests/arithmetic_ast.rs":"b18b9a46ba573ae13c40a31217425f6e8cf8fade09a75cdbbfa7146ec668f0b2","tests/cross_function_backtracking.rs":"b071d13031c1f12195473186e3775943991496b10f4590db3f36d511e9f98a1c","tests/ini.rs":"776f681542028564899e55f71533b3bcda5ed1bbb971f24b5b1b9578111ba0cb","tests/ini_str.rs":"315046d9b6dc38d6d306d3562d7ac6518c9ecce9aabcc58fb80c07577ad99789","tests/issues.rs":"2193c219397b7a417cc009b72c13adc42471e7a4917a2a4009aa0fca23c6ea8c","tests/mp4.rs":"b4bf0514fd645160851cc4da9ad6bf81d571cd14865bf134837c19578caaf6e6","tests/omnom.rs":"409d2349fa24f3503bd02e0079c1554a58ce3d40dd7eb0e5d4bb63b588afdae4","tests/test1.rs":"3e0c187bad91d822ebc113eb5cf30fc6585e53a961728304ac24e05ab2123d10"},"package":"a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"} \ No newline at end of file +{"files":{"CHANGELOG.md":"0ca452f3de1955e58ac7e6814077b430c0ee1dbf7e64c5348b6d7b0b997ffb27","Cargo.toml":"69edf4fdee4c17415625f77366f55cf6b3fd7410222c392a2b4bc262bf2e2bc9","LICENSE":"568d0ae12e18bf9dda98fcd838732321852abdb557c24900d474e71f8fd29f4e","src/bits.rs":"8cd8786191145b5365d381ffdf3aedf69002b6a8d40520c7b752c3e6adb83cc9","src/branch.rs":"e298f1d370a25e0a85f34186d38427f5805e745808f98586c2e8c51581e40480","src/bytes.rs":"bf0fe97e34f47d1f5d4ef93d3a00bd7553ae87ea248162cbce8872ea64c44b36","src/character.rs":"3891258d893c09833f6577dee5b9c7af3e12dc2e60458df542a8fcfb8516e068","src/internal.rs":"7be274fd578166404e0b8236417efeaa8a6cda4a8b10ab637083b1d6cba70f1c","src/lib.rs":"14300d7984ea66e052923e0db31b6c99840e5e234cc76042ce94645d2779c7ea","src/macros.rs":"58cde368c072e219ac19a83b7cb6eb9942d57030b2f74426110e8d91e7968145","src/methods.rs":"e6438ac91baec05fb898765879722d55bb54d9e96e29ab823ed34fe571802063","src/multi.rs":"318b4a345b185252515e1373e3fb1302d74ba3a0c65d44c55b4da39bf74a4c29","src/nom.rs":"48acec4a5dd92222823272e38b63e0decf33a31bdd3e97d22ed5d02ae4c6696c","src/regexp.rs":"8a780a8d328b31012a083ca763b8326e7126320a9071ea011629ddb9d82d178a","src/sequence.rs":"20055b97349f135fae182ba3755261439784b3b83572938ed2d9e4eda3c04758","src/simple_errors.rs":"0a37b042eba1c41da6d18b9249f3b7360f1732a5ed43150c39f36877b40594df","src/str.rs":"4cc81376b1d8c48709d73af357141554bbabb4e0546c1bb4606cfdd2ad8082cb","src/stream.rs":"f80895c621aae949a655576fc1082b65235d3e3af5d6a1cafcc9c0d2398264c9","src/traits.rs":"15db8ac6d5f698d9f55d23fbd025dd10f0e461533b34166e6234bf00e443f42e","src/util.rs":"5c8af3f73dff0efe3bcff59ff0c9cdbfeeecc4bafed14763c229a37bf26f7c99","src/verbose_errors.rs":"f640709d9bd8ffffb8baebab8252bc3d55e247a4c4ee814007b7fbda29856233","src/whitespace.rs":"0117cc62c288acd3ba53903fc14fd650934039919039ba5f83195e0435e12535","tests/arithmetic.rs":"aac143de5c80179f9cb71bf3c79268aa892876622b6f08d5e360ab9308f21341","tests/arithmetic_ast.rs":"bb8995cf726ac382e87dd9665f168cba0d25c8cc040372321027d23499ddea99","tests/blockbuf-arithmetic.rs":"485605360f68fc301390eff084657c27cb30347b8eaaed6d92a5830767d50ce6","tests/cross_function_backtracking.rs":"ea847fa762954e1ff8d8e4fbd130e453d57f41132735e4745286a2fbaf10dd6e","tests/float.rs":"7e380464cd8c2c315eaa2bcd3bb7407a4ba558ee5d6adde3ec72492c7987f784","tests/ini.rs":"c3b2b3ccd6b854c36dc89afc8d68877ff7f5cb8b2cb9d8f138e143deb8ee6ddc","tests/ini_str.rs":"7c5db358330be22e1c64900a8182f26881f7e5d38f1c78d827c60de0af68612f","tests/issues.rs":"d9af6f28be33a70732bec51213a4811910c656a1c38c4ffb5b3202efc188b4b6","tests/json.rs":"5d2c2f3f6ebb9f9c188f5b0cfad723a421ce161caceb3d8517d02fc6546fece6","tests/mp4.rs":"0e5f248dc9e27182ff82b3153481df529600bc4418b84f1f0554e1ada6cc3e08","tests/multiline.rs":"5165e95ba471f77dc0e614a716828d71bbcebcd42fe0fb882b7da1d9633222af","tests/named_args.rs":"3954a031e17e55e12e2bbb107c36a29e2204969ee2e4afb9cbd604decfe3f81a","tests/omnom.rs":"ff749d621b51df8aa3db289b1b626c706190fa15e29061dd5653de83635976d4","tests/overflow.rs":"aec98fc65bf102ae934625aa70e2b48cfcce70f4af38d6520716d79df3e95335","tests/reborrow_fold.rs":"8f140330dd0bfb3bbdab8286b4be6d6f698cadb7b064fac586dcca97e3d397fe","tests/test1.rs":"3e0c187bad91d822ebc113eb5cf30fc6585e53a961728304ac24e05ab2123d10"},"package":"05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b"} \ No newline at end of file diff --git third_party/rust/nom/.travis.yml third_party/rust/nom/.travis.yml deleted file mode 100644 index 1d1e36c593aa..000000000000 --- third_party/rust/nom/.travis.yml +++ /dev/null @@ -1,46 +0,0 @@ -language: rust - -addons: - apt: - packages: - - libcurl4-openssl-dev - - libelf-dev - - libdw-dev - -rust: - - nightly - - beta - - stable - - 1.2.0 - -before_script: - - pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH - -script: - - | - travis-cargo --only 1.2 test -- --features regexp && - travis-cargo --only stable test -- --features "regexp regexp_macros" && - travis-cargo --only beta test -- --features "regexp regexp_macros" && - travis-cargo --only nightly build -- --features "nightly core regexp" && - travis-cargo --only nightly test -- --features "regexp" && - travis-cargo bench && - travis-cargo --only stable doc -- --features "regexp" - -after_success: - - travis-cargo coveralls --no-sudo - -notifications: - webhooks: - urls: - - https://webhooks.gitter.im/e/9c035a194ac4fd4cc061 - on_success: change - on_failure: always - on_start: false - - -env: - global: - # override the default `--features unstable` used for the nightly branch (optional) - - TRAVIS_CARGO_NIGHTLY_FEATURE=nightly - -sudo: false diff --git third_party/rust/nom/CHANGELOG.md third_party/rust/nom/CHANGELOG.md index f1c331585779..35e523666bd4 100644 --- third_party/rust/nom/CHANGELOG.md +++ third_party/rust/nom/CHANGELOG.md @@ -4,6 +4,267 @@ ### Changed +## 3.2.1 - 2017-10-27 + +### Thanks + +- @ordian for `alt_complete` fixes +- @friedm for documentation fixes +- @kali for improving error management + +### Fixed + +- there were cases where `alt_complete` could return `Incomplete` + +### Added + +- an `into_error_kind` method can be used to transform any error to a common value. This helps when the library is included multiple times as dependency with different feature sets + + +## 3.2.0 - 2017-07-24 + +### Thanks + +- @jedireza for documentation fixes +- @gmorenz for the `bytes` combinator +- @meh for character combinator fixes for UTF-8 +- @jethrogb for avoiding move issues in `separated_list` + +### Changed + +- new layout for the main page of documentation +- `anychar` can now work on any input type +- `length_bytes` is now an alias for `length_data` + +### Fixed + +- `one_of`, `none_of` and `char` will now index correctly UTF-8 characters +- the `compiler_error` macro is now correctly exported + + +### Added + +- the `bytes` combinator transforms a bit stream back to a byte slice for child parsers + +## 3.1.0 - 2017-06-16 + +### Thanks + +- @sdroege: implementing be_i24 and le_i24 +- @Hywan: integrating faster substring search using memchr +- @nizox: fixing type issues in bit stream parsing +- @grissiom: documentation fixes +- @doomrobo: implementing separated_list_complete and separated_nonempty_list_complete +- @CWood1: fixing memchr integration in no_std +- @lu_zero: integrating the compiler_error crate +- @dtolnay: helping debug a type inference issue in map + +### Changed + +- memchr is used for substring search if possible +- if building on nightly, some common syntax errors will display a specific error message. If building no stable, display the documentation to activate those messages +- `count` no longer preallocates its vector + +### Fixed + +- better type inference in alt_complete +- `alt` should now work with whitespace parsing +- `map` should not make type inference errors anymore + +### Added + +- be_i24 and le_i24, parsing big endian and little endian signed 24 bit integers +- `separated_list_complete` and `separated_nonempty_list_complete` will treat incomplete from sub parsers as error + +## 3.0.0 - 2017-05-12 + +### Thanks + +- Chris Pick for some `Incomplete` related refactors +- @drbgn for documentation fixes +- @valarauca for adding `be_u24` +- @ithinuel for usability fixes +- @evuez for README readability fixes and improvements to `IResult` +- @s3bk for allowing non-`Copy` types as input +- @keruspe for documentation fixes +- @0xd34d10cc for trait fixes on `InputIter` +- @sdleffler for lifetime shenanigans on `named_args` +- @chengsun for type inference fixes in `alt` +- @iBelieve for adding str to no_std +- @Hywan for simplifying code in input traits +- @azerupi for extensive documentation of `alt` and `alt_complete` + +### Breaking Changes + +- `escaped`, `separated_list` and `separated_nonempty_list` can now return `Incomplete` when necessary +- `InputIter` does not require `AsChar` on its `Item` type anymore +- the `core` feature that was putting nom in `no_std` mode has been removed. There is now a `std` feature, activated by default. If it is not activated, nom is in `no_std` +- in `verbose-errors` mode, the error list is now stored in a `Vec` instead of a box based linked list +- `chain!` has finally been removed + +### Changed + +- `Endianness` now implements `Debug`, `PartialEq`, `Eq`, `Clone` and `Copy` +- custom input types can now be cloned if they're not `Copy` +- the infamous 'Cannot infer type for E' error should happen less often now +- `str` is now available in `no_std` mode + +### Fixed + +- `FileProducer` will be marked as `Eof` on full buffer +- `named_args!` now has lifetimes that cannot conflict with the lifetimes from other arguments + +### Added + +- `be_u24`: big endian 24 bit unsigned integer parsing +- `IResult` now has a `unwrap_or` method + + +## 2.2.1 - 2017-04-03 + +### Thanks + +- @Victor-Savu for formatting fixes in the README +- @chifflier for detecting and fixing integer overflows +- @utkarshkukreti for some performance improvements in benchmarks + +### Changed + +- when calculating how much data is needed in `IResult::Incomplete`, the addition could overflow (it is stored as a usize). This would apparently not result in any security vulnerability on release code + +## 2.2.0 - 2017-03-20 + +### Thanks + +- @seppo0010 for fixing `named_args` +- @keruspe for implementing or() on `IResult`, adding the option of default cases in `switch!`, adding support for `cargo-travis` +- @timlyo for documentation fixes +- @JayKickliter for extending `hex_u32` +- @1011X for fixing regex integration +- @Kerollmops for actually marking `chain!` as deprecated +- @joliss for documentation fixes +- @utkarshkukreti for tests refactoring and performance improvement +- @tmccombs for documentation fixes + +### Added + +- `IResult` gets an `or()` method +- `take_until1`, `take_until_and_consume1`, `take_till1!` and `take_till1_s!` require at least 1 character + +### Changed + +- `hex_u32` accepts uppercase digits as well +- the character based combinators leverage the input traits +- the whitespace parsers now work on &str and other types +- `take_while1` returns `Incomplete` on empty input +- `switch!` can now take a default case + +### Fixed + +- `named_args!` now imports `IResult` directly +- the upgrade to regex 0.2 broke the regex combinators, they work now + +## 2.1.0 - 2017-01-27 + +### Thanks + +- @nickbabcock for documentation fixes +- @derekdreery for documentation fixes +- @DirkyJerky for documentation fixes +- @saschagrunert for documentation fixes +- @lucab for documentation fixes +- @hyone for documentation fixes +- @tstorch for factoring `Slice` +- @shepmaster for adding crate categories +- @antoyo for adding `named_args!` + +### Added + +- `verify!` uses a first parser, then applies a function to check that its result satisfies some conditions +- `named_args!` creates a parser function that can accept other arguments along with the input +- `parse_to!` will use the `parse` method from `FromStr` to parse a value. It will automatically translate the input to a string if necessary +- `float`, `float_s`, `double`, `double_s` can recognize floating point numbers in text + +### Changed + +- `escaped!` will now return `Incomplete` if needed +- `permutation!` supports up to 20 child parsers + +## 2.0.1 - 2016-12-10 + +Bugfix release + +*Warning*: there is a small breaking change, `add_error!` is renamed to `add_return_error!`. This was planned for the 2.0 release but was forgotten. This is a small change in a feature that not many people use, for a release that is not yet widely in use, so there will be no 3.0 release for that change. + +### Thanks + +- @nickbabcock for catching and fixing the `add_error!` mixup +- @lucab for documentation fixes +- @jtdowney for noticing that `tag_no_case!` was not working at all for byte slices + +### Fixed + +- `add_error!` has been renamed to `add_return_error!` +- the `not!` combinator now accepts functions +- `tag_no_case!` is now working as accepted (before, it accepted everything) + + +## 2.0 - 2016-11-25 + +The 2.0 release is one of the biggest yet. It was a good opportunity to clean up some badly named combinators and fix invalid behaviours. + +Since this version introduces a few breaking changes, an [upgrade documentation](https://github.com/Geal/nom/blob/master/doc/upgrading_to_nom_2.md) is available, detailing the steps to fix the most common migration issues. After testing on a set of 30 crates, most of them will build directly, a large part will just need to activate the "verbose-errors" compilation feature. The remaining fixes are documented. + +This version also adds a lot of interesting features, like the permutation combinator or whitespace separated formats support. + +### Thanks + +- @lu-zero for license help +- @adamgreig for type inference fixes +- @keruspe for documentation and example fixes, for the `IResult => Result` conversion work, making `AsChar`'s method more consistent, and adding `many_till!` +- @jdeeny for implementing `Offset` on `&str` +- @vickenty for documentation fixes and his refactoring of `length_value!` and `length_bytes!` +- @overdrivenpotato for refactoring some combinators +- @taralx for documentation fixes +- @keeperofdakeys for fixing eol behaviour, writing documentation and adding `named_attr!` +- @jturner314 for writing documentation +- @bozaro for fixing compilation errors +- @uniphil for adding a `crates.io` badge +- @badboy for documentation fixes +- @jugglerchris for fixing `take_s!` +- @AndyShiue for implementing `Error` and `Display` on `ErrorKind` and detecting incorrect UTF-8 string indexing + +### Added + +- the "simple" error management system does not accumulates errors when backtracking. This is a big perf gain, and is activated by default in nom 2.0 +- nom can now work on any type that implement the traits defined in `src/traits.rs`: `InputLength`, `InputIter`, `InputTake`, `Compare`, `FindToken`, `FindSubstring`, `Slice` +- the documentation from Github's wiki has been moved to the `doc/` directory. They are markdown files that you can build with [cargo-external-doc](https://crates.io/crates/cargo-external-doc) +- whitespace separated format support: with the `ws!` combinator, you can automatically introduce whitespace parsers between all parsers and combinators +- the `permutation!` combinator applies its child parsers in any order, as long as they all succeed once, and return a tuple of the results +- `do_parse!` is a simpler alternative to `chain!`, which is now deprecated +- you can now transform an `IResult` in a `std::result::Result` +- `length_data!` parses a length, and returns a subslice of that length +- `tag_no_case!` provides case independent comparison. It works nicely, without any allocation, for ASCII strings, but for UTF-8 strings, it defaults to an unsatisfying (and incorrect) comparison by lowercasing both strings +- `named_attr!` creates functions like `named!` but can add attributes like documentation +- `many_till!` applies repeatedly its first child parser until the second succeeds + +### Changed + +- the "verbose" error management that was available in previous versions is now activated by the "verbose-errors" compilation feature +- code reorganization: most of the parsers were moved in separate files to make the source easier to navigate +- most of the combinators are now independent from the input type +- the `eof` function was replaced with the `eof!` macro +- `error!` and `add_error!` were replaced with `return_error!` and `add_return_error!` to fix the name conflict with the log crate +- the `offset()` method is now in the `Offset` trait +- `length_value!` has been renamed to `length_count!`. The new `length_value!` selects a slice and applies the second parser once on that slice +- `AsChar::is_0_to_9` is now `AsChar::is_dec_digit` +- the combinators with configurable endianness now take an enum instead of a boolean as parameter + +### Fixed +- the `count!`, `count_fixed!` and `length_*!` combinator calculate incomplete data needs correctly +- `eol`, `line_ending` and `not_line_ending` now have a consistent behaviour that works correctly with incomplete data +- `take_s!` didn't correctly handle the case when the slice is exactly the right length + ## 1.2.4 - 2016-07-20 ### Thanks @@ -29,14 +290,14 @@ - `take_bits!` is now more precise - `many1` inccorectly used the `len` function instead of `input_len` - the INI parser is simpler -- `recognize!` had an early `return` taht is removed now +- `recognize!` had an early `return` that is removed now ## 1.2.3 - 2016-05-10 ### Thanks - @lu-zero for the contribution guidelines - @GuillaumeGomez for fixes on `length_bytes` and some documentation -- @Hywan for ducomentation and test fixes +- @Hywan for documentation and test fixes - @Xirdus for correct trait import issues - @mspiegel for the new AST example - @cholcombe973 for adding the `cond_with_error!` combinator @@ -63,7 +324,7 @@ ## 1.2.2 - 2016-03-09 ### Thanks -- @conradev for fixing take_until_s!` +- @conradev for fixing `take_until_s!` - @GuillaumeGomez for some documentation fixes - @frewsxcv for some documentation fixes - @tstorch for some test refactorings @@ -121,7 +382,7 @@ - there were type inference issues in a few combinators. They will now be easier to compile - `peek!` compilation with bare functions - `&str` parsers were splitting data at the byte level, not at the char level, which can result in inconsistencies in parsing UTF-8 characters. They now use character indexes -- some method implementations were missing on `ÌResult` (with specified error type instead of implicit) +- some method implementations were missing on `IResult` (with specified error type instead of implicit) ## 1.1.0 - 2016-01-01 @@ -137,11 +398,11 @@ There are also a few performance improvements and documentation fixes. - @meh for fixing `Option` and `Vec` imports - @hoodie for a documentation fix - @joelself for some documentation fixes -- @vberger for his traits magic making `nom functions more generic +- @vberger for his traits magic making nom functions more generic ### Added -- string related parsers: `tag_s!`, `take_s!`, `is_a_s!`, `is_not_s!`, `take_while_s!`, `take_while1_s!`, `take_till_s! +- string related parsers: `tag_s!`, `take_s!`, `is_a_s!`, `is_not_s!`, `take_while_s!`, `take_while1_s!`, `take_till_s!` - `value!` is a combinator that always returns the same value. If a child parser is passed as second argument, that value is returned when the child parser succeeds ### Changed @@ -521,8 +782,17 @@ Considering the number of changes since the last release, this version can conta ## Compare code -* [unreleased]: https://github.com/Geal/nom/compare/1.2.4...HEAD -* [1.2.3]: https://github.com/Geal/nom/compare/1.2.3...1.2.4 +* [unreleased]: https://github.com/Geal/nom/compare/3.2.1...HEAD +* [3.2.1]: https://github.com/Geal/nom/compare/3.2.0...3.2.1 +* [3.2.0]: https://github.com/Geal/nom/compare/3.1.0...3.2.0 +* [3.1.0]: https://github.com/Geal/nom/compare/3.0.0...3.1.0 +* [3.0.0]: https://github.com/Geal/nom/compare/2.2.1...3.0.0 +* [2.2.1]: https://github.com/Geal/nom/compare/2.2.0...2.2.1 +* [2.2.0]: https://github.com/Geal/nom/compare/2.1.0...2.2.0 +* [2.1.0]: https://github.com/Geal/nom/compare/2.0.1...2.1.0 +* [2.0.1]: https://github.com/Geal/nom/compare/2.0.0...2.0.1 +* [2.0.0]: https://github.com/Geal/nom/compare/1.2.4...2.0.0 +* [1.2.4]: https://github.com/Geal/nom/compare/1.2.3...1.2.4 * [1.2.3]: https://github.com/Geal/nom/compare/1.2.2...1.2.3 * [1.2.2]: https://github.com/Geal/nom/compare/1.2.1...1.2.2 * [1.2.1]: https://github.com/Geal/nom/compare/1.2.0...1.2.1 diff --git third_party/rust/nom/Cargo.toml third_party/rust/nom/Cargo.toml index ae8045bf1135..1ee10cf12c2b 100644 --- third_party/rust/nom/Cargo.toml +++ third_party/rust/nom/Cargo.toml @@ -1,38 +1,50 @@ -[package] +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g. crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) +[package] name = "nom" -version = "1.2.4" -authors = [ "contact@geoffroycouprie.com" ] +version = "3.2.1" +authors = ["contact@geoffroycouprie.com"] +include = ["CHANGELOG.md", "LICENSE", ".gitignore", ".travis.yml", "Cargo.toml", "src/*.rs", "tests/*.rs"] description = "A byte-oriented, zero-copy, parser combinators library" -license = "MIT" -repository = "https://github.com/Geal/nom" -readme = "README.md" documentation = "http://rust.unhandledexpression.com/nom/" +readme = "README.md" keywords = ["parser", "parser-combinators", "parsing", "streaming", "bit"] +categories = ["parsing"] +license = "MIT" +repository = "https://github.com/Geal/nom" +[dependencies.memchr] +version = "^1.0.1" +default-features = false -include = [ - "CHANGELOG.md", - "LICENSE", - ".gitignore", - ".travis.yml", - "Cargo.toml", - "src/*.rs", - "tests/*.rs" -] - -[features] -core = [] -nightly = [] -default = ["stream"] -regexp = ["regex"] -regexp_macros = ["regexp", "lazy_static"] -stream = [] +[dependencies.compiler_error] +version = "0.1.1" +optional = true [dependencies.regex] -version = "^0.1.56" +version = "^0.2" optional = true [dependencies.lazy_static] -version = "^0.2.1" +version = "^0.2.2" optional = true +[features] +default = ["std", "stream"] +regexp = ["regex"] +verbose-errors = [] +stream = [] +nightly = ["compiler_error"] +regexp_macros = ["regexp", "lazy_static"] +std = ["memchr/use_std"] +[badges.travis-ci] +repository = "Geal/nom" diff --git third_party/rust/nom/LICENSE third_party/rust/nom/LICENSE index 0bd6a1c33dc6..a885458136ae 100644 --- third_party/rust/nom/LICENSE +++ third_party/rust/nom/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015 Geoffroy Couprie +Copyright (c) 2015-2016 Geoffroy Couprie Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git third_party/rust/nom/src/bits.rs third_party/rust/nom/src/bits.rs index a8bd8b24aa99..d475cd32ebcd 100644 --- third_party/rust/nom/src/bits.rs +++ third_party/rust/nom/src/bits.rs @@ -36,6 +36,7 @@ macro_rules! bits ( ); ); +#[cfg(feature = "verbose-errors")] /// Internal parser, do not use directly #[doc(hidden)] #[macro_export] @@ -44,7 +45,7 @@ macro_rules! bits_impl ( { let input = ($i, 0usize); match $submac!(input, $($args)*) { - $crate::IResult::Error(e) => { + $crate::IResult::Error(e) => { let err = match e { $crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k), $crate::Err::Position(k, (i,b)) | $crate::Err::NodePosition(k, (i,b), _) => { @@ -68,6 +69,131 @@ macro_rules! bits_impl ( ); ); +#[cfg(not(feature = "verbose-errors"))] +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! bits_impl ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + let input = ($i, 0usize); + match $submac!(input, $($args)*) { + $crate::IResult::Error(e) => { + $crate::IResult::Error(e) + } + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + //println!("bits parser returned Needed::Size({})", i); + $crate::IResult::Incomplete($crate::Needed::Size(i / 8 + 1)) + }, + $crate::IResult::Done((i, bit_index), o) => { + let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ; + //println!("bit index=={} => byte index=={}", bit_index, byte_index); + $crate::IResult::Done(&i[byte_index..], o) + } + } + } + ); +); + +/// Counterpart to bits, +/// `bytes!( parser ) => ( (&[u8], usize), &[u8] -> IResult<&[u8], T> ) -> IResult<(&[u8], usize), T>`, +/// transforms its bits stream input into a byte slice for the underlying parsers. If we start in the +/// middle of a byte throws away the bits until the end of the byte. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::rest; +/// # fn main() { +/// named!( parse<(u8, u8, &[u8])>, bits!( tuple!( +/// take_bits!(u8, 4), +/// take_bits!(u8, 8), +/// bytes!(rest) +/// ))); +/// +/// let input = &[0xde, 0xad, 0xbe, 0xaf]; +/// +/// assert_eq!(parse( input ), Done(&[][..], (0xd, 0xea, &[0xbe, 0xaf][..]))); +/// # } +#[macro_export] +macro_rules! bytes ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + bytes_impl!($i, $submac!($($args)*)); + ); + ($i:expr, $f:expr) => ( + bytes_impl!($i, call!($f)); + ); +); + +#[cfg(feature = "verbose-errors")] +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! bytes_impl ( + ($macro_i:expr, $submac:ident!( $($args:tt)* )) => ( + { + let inp; + if $macro_i.1 % 8 != 0 { + inp = & $macro_i.0[1 + $macro_i.1 / 8 ..]; + } + else { + inp = & $macro_i.0[$macro_i.1 / 8 ..]; + } + + match $submac!(inp, $($args)*) { + $crate::IResult::Error(e) => { + let err = match e { + $crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k), + $crate::Err::Position(k, i) | $crate::Err::NodePosition(k, i, _) => { + $crate::Err::Position(k, (i, 0)) + } + }; + $crate::IResult::Error(err) + } + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + $crate::IResult::Incomplete($crate::Needed::Size(i * 8)) + }, + $crate::IResult::Done(i, o) => { + $crate::IResult::Done((i, 0), o) + } + } + } + ); +); + +#[cfg(not(feature = "verbose-errors"))] +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! bytes_impl ( + ($macro_i:expr, $submac:ident!( $($args:tt)* )) => ( + { + let inp; + if $macro_i.1 % 8 != 0 { + inp = & $macro_i.0[1 + $macro_i.1 / 8 ..]; + } + else { + inp = & $macro_i.0[$macro_i.1 / 8 ..]; + } + + match $submac!(inp, $($args)*) { + $crate::IResult::Error(e) => { + $crate::IResult::Error(e) + } + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + $crate::IResult::Incomplete($crate::Needed::Size(i * 8)) + }, + $crate::IResult::Done(i, o) => { + $crate::IResult::Done((i, 0), o) + } + } + } + ); +); + /// `take_bits!(type, nb) => ( (&[T], usize), U, usize) -> IResult<(&[T], usize), U>` /// generates a parser consuming the specified number of bits. /// @@ -89,17 +215,18 @@ macro_rules! take_bits ( ($i:expr, $t:ty, $count:expr) => ( { use std::ops::Div; + use std::convert::Into; //println!("taking {} bits from {:?}", $count, $i); let (input, bit_offset) = $i; let res : $crate::IResult<(&[u8],usize), $t> = if $count == 0 { - $crate::IResult::Done( (input, bit_offset), 0) + $crate::IResult::Done( (input, bit_offset), (0 as u8).into()) } else { let cnt = ($count as usize + bit_offset).div(8); if input.len() * 8 < $count as usize + bit_offset { //println!("returning incomplete: {}", $count as usize + bit_offset); $crate::IResult::Incomplete($crate::Needed::Size($count as usize)) } else { - let mut acc:$t = 0; + let mut acc:$t = (0 as u8).into(); let mut offset: usize = bit_offset; let mut remaining: usize = $count; let mut end_offset: usize = 0; @@ -109,9 +236,9 @@ macro_rules! take_bits ( break; } let val: $t = if offset == 0 { - *byte as $t + (*byte as u8).into() } else { - ((*byte << offset) as u8 >> offset) as $t + (((*byte as u8) << offset) as u8 >> offset).into() }; if remaining < 8 - offset { @@ -144,11 +271,11 @@ macro_rules! tag_bits ( let res: $crate::IResult<(&[u8],usize),$t> = $crate::IResult::Done(i, o); res } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagBits, $i)) + $crate::IResult::Error(error_position!($crate::ErrorKind::TagBits, $i)) } }, _ => { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagBits, $i)) + $crate::IResult::Error(error_position!($crate::ErrorKind::TagBits, $i)) } } } @@ -157,12 +284,13 @@ macro_rules! tag_bits ( #[cfg(test)] mod tests { - use internal::{IResult,Needed,Err}; + use std::ops::{Shr,Shl,AddAssign}; + use internal::{IResult,Needed}; use ErrorKind; #[test] fn take_bits() { - let input = vec![0b10101010, 0b11110000, 0b00110011]; + let input = [0b10101010, 0b11110000, 0b00110011]; let sl = &input[..]; assert_eq!(take_bits!( (sl, 0), u8, 0 ), IResult::Done((sl, 0), 0)); @@ -184,7 +312,7 @@ mod tests { #[test] fn tag_bits() { - let input = vec![0b10101010, 0b11110000, 0b00110011]; + let input = [0b10101010, 0b11110000, 0b00110011]; let sl = &input[..]; assert_eq!(tag_bits!( (sl, 0), u8, 3, 0b101), IResult::Done((&sl[0..], 3), 5)); @@ -192,17 +320,17 @@ mod tests { } named!(ch<(&[u8],usize),(u8,u8)>, - chain!( - tag_bits!(u8, 3, 0b101) ~ - x: take_bits!(u8, 4) ~ - y: take_bits!(u8, 5) , - || { (x,y) } + do_parse!( + tag_bits!(u8, 3, 0b101) >> + x: take_bits!(u8, 4) >> + y: take_bits!(u8, 5) >> + (x,y) ) ); #[test] fn chain_bits() { - let input = vec![0b10101010, 0b11110000, 0b00110011]; + let input = [0b10101010, 0b11110000, 0b00110011]; let sl = &input[..]; assert_eq!(ch((&input[..],0)), IResult::Done((&sl[1..], 4), (5,15))); assert_eq!(ch((&input[..],4)), IResult::Done((&sl[2..], 0), (7,16))); @@ -212,9 +340,55 @@ mod tests { named!(ch_bytes<(u8,u8)>, bits!(ch)); #[test] fn bits_to_bytes() { - let input = vec![0b10101010, 0b11110000, 0b00110011]; + let input = [0b10101010, 0b11110000, 0b00110011]; assert_eq!(ch_bytes(&input[..]), IResult::Done(&input[2..], (5,15))); assert_eq!(ch_bytes(&input[..1]), IResult::Incomplete(Needed::Size(2))); - assert_eq!(ch_bytes(&input[1..]), IResult::Error(Err::Position(ErrorKind::TagBits, &input[1..]))); + assert_eq!(ch_bytes(&input[1..]), IResult::Error(error_position!(ErrorKind::TagBits, &input[1..]))); + } + + #[derive(PartialEq,Debug)] + struct FakeUint(u32); + + impl AddAssign for FakeUint { + + fn add_assign(&mut self, other: FakeUint) { + *self = FakeUint(&self.0 + other.0); + } + + } + + impl Shr for FakeUint { + type Output = FakeUint; + + fn shr(self, shift: usize) -> FakeUint { + FakeUint(&self.0 >> shift) + } + + } + + impl Shl for FakeUint { + type Output = FakeUint; + + fn shl(self, shift: usize) -> FakeUint { + FakeUint(&self.0 << shift) + } + + } + + impl From for FakeUint { + + fn from(i: u8) -> FakeUint { + FakeUint(u32::from(i)) + } + } + + #[test] + fn non_privitive_type() { + let input = [0b10101010, 0b11110000, 0b00110011]; + let sl = &input[..]; + + assert_eq!(take_bits!( (sl, 0), FakeUint, 20 ), IResult::Done((&sl[2..], 4), FakeUint(700163))); + assert_eq!(take_bits!( (sl, 4), FakeUint, 20 ), IResult::Done((&sl[3..], 0), FakeUint(716851))); + assert_eq!(take_bits!( (sl, 4), FakeUint, 22 ), IResult::Incomplete(Needed::Size(22))); } } diff --git third_party/rust/nom/src/branch.rs third_party/rust/nom/src/branch.rs new file mode 100644 index 000000000000..a925437145a0 --- /dev/null +++ third_party/rust/nom/src/branch.rs @@ -0,0 +1,871 @@ +/// Try a list of parsers and return the result of the first successful one +/// +/// ```rust,ignore +/// alt!(I -> IResult | I -> IResult | ... | I -> IResult ) => I -> IResult +/// ``` +/// All the parsers must have the same return type. +/// +/// If one of the parsers returns `Incomplete`, `alt!` will return `Incomplete`, to retry +/// once you get more input. Note that it is better for performance to know the +/// minimum size of data you need before you get into `alt!`. +/// +/// The `alt!` combinator is used in the following way: +/// +/// ```rust,ignore +/// alt!(parser_1 | parser_2 | ... | parser_n) +/// ``` +/// +/// # Basic example +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// // Create a parser that will match either "dragon" or "beast" +/// named!( dragon_or_beast, alt!( tag!( "dragon" ) | tag!( "beast" ) ) ); +/// +/// // Given the input "dragon slayer", the parser will match "dragon" +/// // and the rest will be " slayer" +/// let (rest, result) = dragon_or_beast(b"dragon slayer").unwrap(); +/// assert_eq!(result, b"dragon"); +/// assert_eq!(rest, b" slayer"); +/// +/// // Given the input "beast of Gevaudan", the parser will match "beast" +/// // and the rest will be " of Gevaudan" +/// let (rest, result) = dragon_or_beast(&b"beast of Gevaudan"[..]).unwrap(); +/// assert_eq!(result, b"beast"); +/// assert_eq!(rest, b" of Gevaudan"); +/// # } +/// ``` +/// +/// # Manipulate results +/// +/// There exists another syntax for `alt!` that gives you the ability to +/// manipulate the result from each parser: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// # +/// // We create an enum to represent our creatures +/// #[derive(Debug,PartialEq,Eq)] +/// enum Creature { +/// Dragon, +/// Beast, +/// Unknown(usize) +/// } +/// +/// // Let's make a helper function that returns true when not a space +/// // we are required to do this because the `take_while!` macro is limited +/// // to idents, so we can't negate `ìs_space` at the call site +/// fn is_not_space(c: u8) -> bool { ! nom::is_space(c) } +/// +/// // Our parser will return the `Dragon` variant when matching "dragon", +/// // the `Beast` variant when matching "beast" and otherwise it will consume +/// // the input until a space is found and return an `Unknown` creature with +/// // the size of it's name. +/// named!(creature, alt!( +/// tag!("dragon") => { |_| Creature::Dragon } | +/// tag!("beast") => { |_| Creature::Beast } | +/// take_while!(is_not_space) => { |r: &[u8]| Creature::Unknown(r.len()) } +/// // the closure takes the result as argument if the parser is successful +/// )); +/// +/// // Given the input "dragon slayer" the parser will return `Creature::Dragon` +/// // and the rest will be " slayer" +/// let (rest, result) = creature(b"dragon slayer").unwrap(); +/// assert_eq!(result, Creature::Dragon); +/// assert_eq!(rest, b" slayer"); +/// +/// // Given the input "beast of Gevaudan" the parser will return `Creature::Beast` +/// // and the rest will be " of Gevaudan" +/// let (rest, result) = creature(b"beast of Gevaudan").unwrap(); +/// assert_eq!(result, Creature::Beast); +/// assert_eq!(rest, b" of Gevaudan"); +/// +/// // Given the input "demon hunter" the parser will return `Creature::Unkown(5)` +/// // and the rest will be " hunter" +/// let (rest, result) = creature(b"demon hunter").unwrap(); +/// assert_eq!(result, Creature::Unknown(5)); +/// assert_eq!(rest, b" hunter"); +/// # } +/// ``` +/// +/// # Behaviour of `alt!` +/// +/// **BE CAREFUL** there is a case where the behaviour of `alt!` can be confusing: +/// +/// when the alternatives have different lengths, like this case: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ef" ) | tag!( "ghi" ) | tag!( "kl" ) ) ); +/// ``` +/// +/// With this parser, if you pass `"abcd"` as input, the first alternative parses it correctly, +/// but if you pass `"efg"`, the first alternative will return `Incomplete`, since it needs an input +/// of 4 bytes. This behaviour of `alt!` is expected: if you get a partial input that isn't matched +/// by the first alternative, but would match if the input was complete, you want `alt!` to indicate +/// that it cannot decide with limited information. +/// +/// There are two ways to fix this behaviour. The first one consists in ordering the alternatives +/// by size, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "ef" ) | tag!( "kl") | tag!( "ghi" ) | tag!( "abcd" ) ) ); +/// ``` +/// +/// With this solution, the largest alternative will be tested last. +/// +/// The other solution uses the `complete!` combinator, which transforms an `Incomplete` in an +/// `Error`. If one of the alternatives returns `Incomplete` but is wrapped by `complete!`, +/// `alt!` will try the next alternative. This is useful when you know that +/// you will not get partial input: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// complete!( tag!( "abcd" ) ) | +/// complete!( tag!( "ef" ) ) | +/// complete!( tag!( "ghi" ) ) | +/// complete!( tag!( "kl" ) ) +/// ) +/// ); +/// ``` +/// +/// If you want the `complete!` combinator to be applied to all rules then use the convenience +/// `alt_complete!` macro (see below). +/// +/// This behaviour of `alt!` can get especially confusing if multiple alternatives have different +/// sizes but a common prefix, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ab" ) | tag!( "ef" ) ) ); +/// ``` +/// +/// in that case, if you order by size, passing `"abcd"` as input will always be matched by the +/// smallest parser, so the solution using `complete!` is better suited. +/// +/// You can also nest multiple `alt!`, like this: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// preceded!( +/// tag!("ab"), +/// alt!( +/// tag!( "cd" ) | +/// eof!() +/// ) +/// ) +/// | tag!( "ef" ) +/// ) +/// ); +/// ``` +/// +/// `preceded!` will first parse `"ab"` then, if successful, try the alternatives "cd", +/// or empty input (End Of File). If none of them work, `preceded!` will fail and +/// "ef" will be tested. +/// +#[macro_export] +macro_rules! alt ( + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)* ) => ( + compiler_error!("alt uses '|' as separator, not ',': + + alt!( + tag!(\"abcd\") | + tag!(\"efgh\") | + tag!(\"ijkl\") + ) + "); + ); + (__impl $i:expr, $e:ident, $($rest:tt)* ) => ( + alt!(__impl $i, call!($e) , $($rest)*); + ); + (__impl $i:expr, $e:ident | $($rest:tt)*) => ( + alt!(__impl $i, call!($e) | $($rest)*); + ); + + (__impl $i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + let i_ = $i.clone(); + let res = $subrule!(i_, $($args)*); + match res { + $crate::IResult::Done(_,_) => res, + $crate::IResult::Incomplete(_) => res, + $crate::IResult::Error(e) => { + let out = alt!(__impl $i, $($rest)*); + + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + fn unify_types(_: &T, _: &T) {} + if let $crate::IResult::Error(ref e2) = out { + unify_types(&e, e2); + } + + out + } + } + } + ); + + (__impl $i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)*) => ( + { + let i_ = $i.clone(); + match $subrule!(i_, $($args)* ) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(e) => { + let out = alt!(__impl $i, $($rest)*); + + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + fn unify_types(_: &T, _: &T) {} + if let $crate::IResult::Error(ref e2) = out { + unify_types(&e, e2); + } + + out + } + } + } + ); + + (__impl $i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => ( + alt!(__impl $i, call!($e) => { $gen } | $($rest)*); + ); + + (__impl $i:expr, __end) => ( + $crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i)) + ); + + ($i:expr, $($rest:tt)*) => ( + { + alt!(__impl $i, $($rest)* | __end) + } + ); +); + +/// Is equivalent to the `alt!` combinator, except that it will not return `Incomplete` +/// when one of the constituting parsers returns `Incomplete`. Instead, it will try the +/// next alternative in the chain. +/// +/// You should use this combinator only if you know you +/// will not receive partial input for the rules you're trying to match (this +/// is almost always the case for parsing programming languages). +/// +/// ```rust,ignore +/// alt_complete!(I -> IResult | I -> IResult | ... | I -> IResult ) => I -> IResult +/// ``` +/// All the parsers must have the same return type. +/// +/// If one of the parsers return `Incomplete`, `alt_complete!` will try the next alternative. +/// If there is no other parser left to try, an `Error` will be returned. +/// +/// ```rust,ignore +/// alt_complete!(parser_1 | parser_2 | ... | parser_n) +/// ``` +/// **For more in depth examples, refer to the documentation of `alt!`** +#[macro_export] +macro_rules! alt_complete ( + // Recursive rules (must include `complete!` around the head) + + ($i:expr, $e:ident | $($rest:tt)*) => ( + alt_complete!($i, complete!(call!($e)) | $($rest)*); + ); + + ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + let i_ = $i.clone(); + let res = complete!(i_, $subrule!($($args)*)); + match res { + $crate::IResult::Done(_,_) => res, + e => { + let out = alt_complete!($i, $($rest)*); + + if let (&$crate::IResult::Error(ref e1), &$crate::IResult::Error(ref e2)) = (&e, &out) { + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + fn unify_types(_: &T, _: &T) {} + unify_types(e1, e2); + } + + out + }, + } + } + ); + + ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + let i_ = $i.clone(); + match complete!(i_, $subrule!($($args)*)) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + e => { + let out = alt_complete!($i, $($rest)*); + + if let (&$crate::IResult::Error(ref e1), &$crate::IResult::Error(ref e2)) = (&e, &out) { + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + fn unify_types(_: &T, _: &T) {} + unify_types(e1, e2); + } + + out + }, + } + } + ); + + ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => ( + alt_complete!($i, complete!(call!($e)) => { $gen } | $($rest)*); + ); + + // Tail (non-recursive) rules + + ($i:expr, $e:ident => { $gen:expr }) => ( + alt_complete!($i, call!($e) => { $gen }); + ); + + ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + alt!(__impl $i, complete!($subrule!($($args)*)) => { $gen } | __end) + ); + + ($i:expr, $e:ident) => ( + alt_complete!($i, call!($e)); + ); + + ($i:expr, $subrule:ident!( $($args:tt)*)) => ( + alt!(__impl $i, complete!($subrule!($($args)*)) | __end) + ); +); + +/// `switch!(I -> IResult, P => I -> IResult | ... | P => I -> IResult ) => I -> IResult` +/// choose the next parser depending on the result of the first one, if successful, +/// and returns the result of the second parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::{Position, NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// b"efgh" => tag!("123") +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"abcdef"; +/// let c = b"efgh123"; +/// let d = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Done(&b"123"[..], &b"XYZ"[..])); +/// assert_eq!(sw(&b[..]), Error(error_node_position!(ErrorKind::Switch, &b"abcdef"[..], +/// error_position!(ErrorKind::Tag, &b"ef"[..])))); +/// assert_eq!(sw(&c[..]), Done(&b""[..], &b"123"[..])); +/// assert_eq!(sw(&d[..]), Error(error_position!(ErrorKind::Switch, &b"blah"[..]))); +/// # } +/// ``` +/// +/// You can specify a default case like with a normal match, using `_` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// _ => value!(&b"default"[..]) +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Done(&b"123"[..], &b"XYZ"[..])); +/// assert_eq!(sw(&b[..]), Done(&b""[..], &b"default"[..])); +/// # } +/// ``` +/// +/// Due to limitations in Rust macros, it is not possible to have simple functions on the right hand +/// side of pattern, like this: +/// +/// ```ignore +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// b"efgh" => tag!("123") +/// ) +/// ); +/// ``` +/// +/// If you want to pass your own functions instead, you can use the `call!` combinator as follows: +/// +/// ```ignore +/// named!(xyz, tag!("XYZ")); +/// named!(num, tag!("123")); +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => call!(xyz) | +/// b"efgh" => call!(num) +/// ) +/// ); +/// ``` +/// +#[macro_export] +macro_rules! switch ( + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + let i_ = $i.clone(); + match map!(i_, $submac!($($args)*), |o| Some(o)) { + $crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!( + $crate::ErrorKind::Switch, $i, e + )), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + match o { + $(Some($p) => match $subrule!(i, $($args2)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!( + $crate::ErrorKind::Switch, $i, e + )), + a => a, + }),*, + _ => $crate::IResult::Error(error_position!($crate::ErrorKind::Switch,$i)) + } + } + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch!(__impl $i, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $e:ident, $($rest:tt)*) => ( + { + switch!(__impl $i, call!($e), $($rest)*) + } + ); +); + +/// +/// +/// `permutation!(I -> IResult, I -> IResult, ... I -> IResult ) => I -> IResult` +/// applies its sub parsers in a sequence, but independent from their order +/// this parser will only succeed if all of its sub parsers succeed +/// +/// the tuple of results is in the same order as the parsers are declared +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error,Incomplete}; +/// # use nom::{ErrorKind,Needed}; +/// # fn main() { +/// named!(perm<(&[u8], &[u8], &[u8])>, +/// permutation!(tag!("abcd"), tag!("efg"), tag!("hi")) +/// ); +/// +/// // whatever the order, if the parser succeeds, each +/// // tag should have matched correctly +/// let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); +/// +/// let a = &b"abcdefghijk"[..]; +/// assert_eq!(perm(a), Done(&b"jk"[..], expected)); +/// let b = &b"efgabcdhijkl"[..]; +/// assert_eq!(perm(b), Done(&b"jkl"[..], expected)); +/// let c = &b"hiefgabcdjklm"[..]; +/// assert_eq!(perm(c), Done(&b"jklm"[..], expected)); +/// +/// let d = &b"efgxyzabcdefghi"[..]; +/// assert_eq!(perm(d), Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..]))); +/// +/// let e = &b"efgabc"[..]; +/// assert_eq!(perm(e), Incomplete(Needed::Size(7))); +/// # } +/// ``` +#[macro_export] +macro_rules! permutation ( + ($i:expr, $($rest:tt)*) => ( + { + let mut res = permutation_init!((), $($rest)*); + let mut input = $i; + let mut error = ::std::option::Option::None; + let mut needed = ::std::option::Option::None; + + loop { + let mut all_done = true; + permutation_iterator!(0, input, all_done, needed, res, $($rest)*); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + error = ::std::option::Option::Some(error_position!($crate::ErrorKind::Permutation, input)); + } + break; + } + + if let ::std::option::Option::Some(need) = needed { + if let $crate::Needed::Size(sz) = need { + $crate::IResult::Incomplete( + $crate::Needed::Size( + $crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&input) + + sz + ) + ) + } else { + $crate::IResult::Incomplete($crate::Needed::Unknown) + } + } else if let ::std::option::Option::Some(e) = error { + $crate::IResult::Error(e) + } else { + let unwrapped_res = permutation_unwrap!(0, (), res, $($rest)*); + $crate::IResult::Done(input, unwrapped_res) + } + } + ); +); + + +#[doc(hidden)] +#[macro_export] +macro_rules! permutation_init ( + ((), $e:ident, $($rest:tt)*) => ( + permutation_init!((::std::option::Option::None), $($rest)*) + ); + ((), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + permutation_init!((::std::option::Option::None), $($rest)*) + ); + (($($parsed:expr),*), $e:ident, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , ::std::option::Option::None), $($rest)*); + ); + (($($parsed:expr),*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + permutation_init!(($($parsed),* , ::std::option::Option::None), $($rest)*); + ); + (($($parsed:expr),*), $e:ident) => ( + ($($parsed),* , ::std::option::Option::None) + ); + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + ($($parsed),* , ::std::option::Option::None) + ); + (($($parsed:expr),*),) => ( + ($($parsed),*) + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! succ ( + (0, $submac:ident ! ($($rest:tt)*)) => ($submac!(1, $($rest)*)); + (1, $submac:ident ! ($($rest:tt)*)) => ($submac!(2, $($rest)*)); + (2, $submac:ident ! ($($rest:tt)*)) => ($submac!(3, $($rest)*)); + (3, $submac:ident ! ($($rest:tt)*)) => ($submac!(4, $($rest)*)); + (4, $submac:ident ! ($($rest:tt)*)) => ($submac!(5, $($rest)*)); + (5, $submac:ident ! ($($rest:tt)*)) => ($submac!(6, $($rest)*)); + (6, $submac:ident ! ($($rest:tt)*)) => ($submac!(7, $($rest)*)); + (7, $submac:ident ! ($($rest:tt)*)) => ($submac!(8, $($rest)*)); + (8, $submac:ident ! ($($rest:tt)*)) => ($submac!(9, $($rest)*)); + (9, $submac:ident ! ($($rest:tt)*)) => ($submac!(10, $($rest)*)); + (10, $submac:ident ! ($($rest:tt)*)) => ($submac!(11, $($rest)*)); + (11, $submac:ident ! ($($rest:tt)*)) => ($submac!(12, $($rest)*)); + (12, $submac:ident ! ($($rest:tt)*)) => ($submac!(13, $($rest)*)); + (13, $submac:ident ! ($($rest:tt)*)) => ($submac!(14, $($rest)*)); + (14, $submac:ident ! ($($rest:tt)*)) => ($submac!(15, $($rest)*)); + (15, $submac:ident ! ($($rest:tt)*)) => ($submac!(16, $($rest)*)); + (16, $submac:ident ! ($($rest:tt)*)) => ($submac!(17, $($rest)*)); + (17, $submac:ident ! ($($rest:tt)*)) => ($submac!(18, $($rest)*)); + (18, $submac:ident ! ($($rest:tt)*)) => ($submac!(19, $($rest)*)); + (19, $submac:ident ! ($($rest:tt)*)) => ($submac!(20, $($rest)*)); +); + +// HACK: for some reason, Rust 1.11 does not accept $res.$it in +// permutation_unwrap. This is a bit ugly, but it will have no +// impact on the generated code +#[doc(hidden)] +#[macro_export] +macro_rules! acc ( + (0, $tup:expr) => ($tup.0); + (1, $tup:expr) => ($tup.1); + (2, $tup:expr) => ($tup.2); + (3, $tup:expr) => ($tup.3); + (4, $tup:expr) => ($tup.4); + (5, $tup:expr) => ($tup.5); + (6, $tup:expr) => ($tup.6); + (7, $tup:expr) => ($tup.7); + (8, $tup:expr) => ($tup.8); + (9, $tup:expr) => ($tup.9); + (10, $tup:expr) => ($tup.10); + (11, $tup:expr) => ($tup.11); + (12, $tup:expr) => ($tup.12); + (13, $tup:expr) => ($tup.13); + (14, $tup:expr) => ($tup.14); + (15, $tup:expr) => ($tup.15); + (16, $tup:expr) => ($tup.16); + (17, $tup:expr) => ($tup.17); + (18, $tup:expr) => ($tup.18); + (19, $tup:expr) => ($tup.19); + (20, $tup:expr) => ($tup.20); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! permutation_unwrap ( + ($it:tt, (), $res:ident, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + succ!($it, permutation_unwrap!((acc!($it, $res).unwrap()), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($($parsed),* , acc!($it, $res).unwrap()), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($($parsed),* , acc!($it, $res).unwrap()), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident) => ( + ($($parsed),* , { acc!($it, $res).unwrap() }) + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )) => ( + ($($parsed),* , acc!($it, $res).unwrap() ) + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! permutation_iterator ( + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e), $($rest)*); + ); + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { + if acc!($it, $res) == ::std::option::Option::None { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => { + $i = i; + acc!($it, $res) = ::std::option::Option::Some(o); + continue; + }, + $crate::IResult::Error(_) => { + $all_done = false; + }, + $crate::IResult::Incomplete(i) => { + $needed = ::std::option::Option::Some(i); + break; + } + }; + } + succ!($it, permutation_iterator!($i, $all_done, $needed, $res, $($rest)*)); + }; + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident) => ( + permutation_iterator!($it, $i, $all_done, $res, call!($e)); + ); + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => { + if acc!($it, $res) == ::std::option::Option::None { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => { + $i = i; + acc!($it, $res) = ::std::option::Option::Some(o); + continue; + }, + $crate::IResult::Error(_) => { + $all_done = false; + }, + $crate::IResult::Incomplete(i) => { + $needed = ::std::option::Option::Some(i); + break; + } + }; + } + }; +); + +#[cfg(test)] +mod tests { + use internal::{Needed,IResult}; + use internal::IResult::*; + use util::ErrorKind; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use std::cmp::min; + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: $crate::IResult<_,_> = if reduced != b { + $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) + } else if m < blen { + $crate::IResult::Incomplete($crate::Needed::Size(blen)) + } else { + $crate::IResult::Done(&$i[blen..], reduced) + }; + res + } + ); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) + }; + res + } + ); + ); + +#[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { + Done(&b""[..], input) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8],&[u8],&'static str> { + Error(error_code!(ErrorKind::Custom("abcd"))) + } + + fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { + Done(input, &b""[..]) + } + + fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | dont_work) + } + fn alt2(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | work) + } + fn alt3(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + //named!(alt1, alt!(dont_work | dont_work)); + //named!(alt2, alt!(dont_work | work)); + //named!(alt3, alt!(dont_work | dont_work | work2 | dont_work)); + + let a = &b"abcd"[..]; + assert_eq!(alt1(a), Error(error_position!(ErrorKind::Alt, a))); + assert_eq!(alt2(a), Done(&b""[..], a)); + assert_eq!(alt3(a), Done(a, &b""[..])); + + named!(alt4, alt!(tag!("abcd") | tag!("efgh"))); + let b = &b"efgh"[..]; + assert_eq!(alt4(a), Done(&b""[..], a)); + assert_eq!(alt4(b), Done(&b""[..], b)); + + // test the alternative syntax + named!(alt5, alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })); + assert_eq!(alt5(a), Done(&b""[..], false)); + assert_eq!(alt5(b), Done(&b""[..], true)); + + // compile-time test guarding against an underspecified E generic type (#474) + named!(alt_eof1, alt!(eof!() | eof!())); + named!(alt_eof2, alt!(eof!() => {|x| x} | eof!() => {|x| x})); + let _ = (alt_eof1, alt_eof2); + + } + + #[test] + fn alt_incomplete() { + named!(alt1, alt!(tag!("a") | tag!("bc") | tag!("def"))); + + let a = &b""[..]; + assert_eq!(alt1(a), Incomplete(Needed::Size(1))); + let a = &b"b"[..]; + assert_eq!(alt1(a), Incomplete(Needed::Size(2))); + let a = &b"bcd"[..]; + assert_eq!(alt1(a), Done(&b"d"[..], &b"bc"[..])); + let a = &b"cde"[..]; + assert_eq!(alt1(a), Error(error_position!(ErrorKind::Alt, a))); + let a = &b"de"[..]; + assert_eq!(alt1(a), Incomplete(Needed::Size(3))); + let a = &b"defg"[..]; + assert_eq!(alt1(a), Done(&b"g"[..], &b"def"[..])); + } + + #[test] + fn alt_complete() { + named!(ac<&[u8], &[u8]>, + alt_complete!(tag!("abcd") | tag!("ef") | tag!("ghi") | tag!("kl")) + ); + + let a = &b""[..]; + assert_eq!(ac(a), Error(error_position!(ErrorKind::Alt, a))); + let a = &b"ef"[..]; + assert_eq!(ac(a), Done(&b""[..], &b"ef"[..])); + let a = &b"cde"[..]; + assert_eq!(ac(a), Error(error_position!(ErrorKind::Alt, a))); + } + + #[allow(unused_variables)] + #[test] + fn switch() { + named!(sw, + switch!(take!(4), + b"abcd" => take!(2) | + b"efgh" => take!(4) + ) + ); + + let a = &b"abcdefgh"[..]; + assert_eq!(sw(a), Done(&b"gh"[..], &b"ef"[..])); + + let b = &b"efghijkl"[..]; + assert_eq!(sw(b), Done(&b""[..], &b"ijkl"[..])); + let c = &b"afghijkl"[..]; + assert_eq!(sw(c), Error(error_position!(ErrorKind::Switch, &b"afghijkl"[..]))); + } + + #[test] + fn permutation() { + //trace_macros!(true); + named!(perm<(&[u8], &[u8], &[u8])>, + permutation!(tag!("abcd"), tag!("efg"), tag!("hi")) + ); + //trace_macros!(false); + + let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); + + let a = &b"abcdefghijk"[..]; + assert_eq!(perm(a), Done(&b"jk"[..], expected)); + let b = &b"efgabcdhijk"[..]; + assert_eq!(perm(b), Done(&b"jk"[..], expected)); + let c = &b"hiefgabcdjk"[..]; + assert_eq!(perm(c), Done(&b"jk"[..], expected)); + + let d = &b"efgxyzabcdefghi"[..]; + assert_eq!(perm(d), Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..]))); + + let e = &b"efgabc"[..]; + assert_eq!(perm(e), Incomplete(Needed::Size(7))); + } + + /* + named!(does_not_compile, + alt!(tag!("abcd"), tag!("efgh")) + ); + */ +} diff --git third_party/rust/nom/src/bytes.rs third_party/rust/nom/src/bytes.rs index 3f31598dc344..462ed0262f67 100644 --- third_party/rust/nom/src/bytes.rs +++ third_party/rust/nom/src/bytes.rs @@ -1,92 +1,81 @@ //! Byte level parsers and combinators //! +#[allow(unused_variables)] -/// `recognize!(&[T] -> IResult<&[T], O> ) => &[T] -> IResult<&[T], &[T]>` -/// if the child parser was successful, return the consumed input as produced value +/// `tag!(&[T]: nom::AsBytes) => &[T] -> IResult<&[T], &[T]>` +/// declares a byte array as a suite to recognize +/// +/// consumes the recognized characters /// /// ``` /// # #[macro_use] extern crate nom; /// # use nom::IResult::Done; /// # fn main() { -/// named!(x, recognize!(delimited!(tag!("")))); -/// let r = x(&b" aaa"[..]); -/// assert_eq!(r, Done(&b" aaa"[..], &b""[..])); +/// named!(x, tag!("abcd")); +/// let r = x(&b"abcdefgh"[..]); +/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..])); /// # } /// ``` #[macro_export] -macro_rules! recognize ( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( +macro_rules! tag ( + ($i:expr, $tag: expr) => ( { - use $crate::HexDisplay; - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,_) => { - let index = ($i).offset(i); - $crate::IResult::Done(i, &($i)[..index]) + use $crate::{Compare,CompareResult,InputLength,Slice}; + let res: $crate::IResult<_,_> = match ($i).compare($tag) { + CompareResult::Ok => { + let blen = $tag.input_len(); + $crate::IResult::Done($i.slice(blen..), $i.slice(..blen)) }, - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } + CompareResult::Incomplete => { + $crate::IResult::Incomplete($crate::Needed::Size($tag.input_len())) + }, + CompareResult::Error => { + $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) + } + }; + res } ); - ($i:expr, $f:expr) => ( - recognize!($i, call!($f)) - ); ); -/// `tag!(&[T]: nom::AsBytes) => &[T] -> IResult<&[T], &[T]>` -/// declares a byte array as a suite to recognize +/// `tag_no_case!(&[T]) => &[T] -> IResult<&[T], &[T]>` +/// declares a case insensitive ascii string as a suite to recognize /// /// consumes the recognized characters /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; +/// # use nom::IResult::{self,Done}; /// # fn main() { -/// named!(x, tag!("abcd")); -/// let r = x(&b"abcdefgh"[..]); -/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..])); +/// named!(test, tag_no_case!("ABcd")); +/// +/// let r = test(&b"aBCdefgh"[..]); +/// assert_eq!(r, Done(&b"efgh"[..], &b"aBCd"[..])); /// # } /// ``` #[macro_export] -macro_rules! tag ( - ($i:expr, $inp: expr) => ( - { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $inp; - let bytes = as_bytes(&expected); - - tag_bytes!($i,bytes) - } - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! tag_bytes ( - ($i:expr, $bytes: expr) => ( +macro_rules! tag_no_case ( + ($i:expr, $tag: expr) => ( { - let len = $i.len(); - let blen = $bytes.len(); - let m = if len < blen { len } else { blen }; - let reduced = &$i[..m]; - let b = &$bytes[..m]; - - let res: $crate::IResult<_,_> = if reduced != b { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Tag, $i)) - } else if m < blen { - $crate::IResult::Incomplete($crate::Needed::Size(blen)) - } else { - $crate::IResult::Done(&$i[blen..], reduced) + use $crate::{Compare,CompareResult,InputLength,Slice}; + let res: $crate::IResult<_,_> = match ($i).compare_no_case($tag) { + CompareResult::Ok => { + let blen = $tag.input_len(); + $crate::IResult::Done($i.slice(blen..), $i.slice(..blen)) + }, + CompareResult::Incomplete => { + $crate::IResult::Incomplete($crate::Needed::Size($tag.input_len())) + }, + CompareResult::Error => { + $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) + } }; res } ); ); + /// `is_not!(&[T:AsBytes]) => &[T] -> IResult<&[T], &[T]>` /// returns the longest list of bytes that do not appear in the provided array /// @@ -103,39 +92,22 @@ macro_rules! tag_bytes ( #[macro_export] macro_rules! is_not( ($input:expr, $arr:expr) => ( - { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $arr; - let bytes = as_bytes(&expected); - - is_not_bytes!($input, bytes) - } - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! is_not_bytes ( - ($input:expr, $bytes:expr) => ( { use $crate::InputLength; - let res: $crate::IResult<_,_> = match $input.iter().position(|c| { - for &i in $bytes.iter() { - if *c == i { return true } - } - false + use $crate::InputIter; + use $crate::FindToken; + use $crate::Slice; + + let res: $crate::IResult<_,_> = match $input.position(|c| { + c.find_token($arr) }) { - Some(0) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsNot,$input)), + Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::IsNot,$input)), Some(n) => { - let res = $crate::IResult::Done(&$input[n..], &$input[..n]); + let res = $crate::IResult::Done($input.slice(n..), $input.slice(..n)); res }, None => { - $crate::IResult::Done(&$input[$input.input_len()..], $input) + $crate::IResult::Done($input.slice($input.input_len()..), $input) } }; res @@ -162,39 +134,22 @@ macro_rules! is_not_bytes ( #[macro_export] macro_rules! is_a ( ($input:expr, $arr:expr) => ( - { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $arr; - let bytes = as_bytes(&expected); - - is_a_bytes!($input, bytes) - } - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! is_a_bytes ( - ($input:expr, $bytes:expr) => ( { use $crate::InputLength; - let res: $crate::IResult<_,_> = match $input.iter().position(|c| { - for &i in $bytes.iter() { - if *c == i { return false } - } - true + use $crate::InputIter; + use $crate::FindToken; + use $crate::Slice; + + let res: $crate::IResult<_,_> = match $input.position(|c| { + !c.find_token($arr) }) { - Some(0) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsA,$input)), + Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::IsA,$input)), Some(n) => { - let res: $crate::IResult<_,_> = $crate::IResult::Done(&$input[n..], &$input[..n]); + let res: $crate::IResult<_,_> = $crate::IResult::Done($input.slice(n..), $input.slice(..n)); res }, None => { - $crate::IResult::Done(&$input[($input).input_len()..], $input) + $crate::IResult::Done($input.slice(($input).input_len()..), $input) } }; res @@ -213,77 +168,55 @@ macro_rules! is_a_bytes ( /// # use nom::IResult::Done; /// # use nom::alpha; /// # fn main() { -/// named!(esc, escaped!(call!(alpha), '\\', is_a_bytes!(&b"\"n\\"[..]))); +/// named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\"))); /// assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..])); /// assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..])); /// # } /// ``` #[macro_export] macro_rules! escaped ( - ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => ( - { - escaped1!($i, $submac!($($args)*), $control_char, $($rest)*) - } - ); - - ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => ( - escaped1!($i, call!($f), $control_char, $($rest)*) - ); -); - -/// Internal parser, do not use directly -#[doc(hidden)] -#[macro_export] -macro_rules! escaped1 ( - ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( - { - escaped_impl!($i, $submac1!($($args)*), $control_char, $submac2!($($args2)*)) - } - ); - ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => ( - escaped_impl!($i, $submac1!($($args)*), $control_char, call!($g)) - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! escaped_impl ( - ($i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $escapable:ident!( $($args2:tt)* )) => ( + // Internal parser, do not use directly + (__impl $i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $escapable:ident!( $($args2:tt)* )) => ( { use $crate::InputLength; - let cl = || { - use $crate::HexDisplay; + use $crate::Slice; + let cl = || -> $crate::IResult<_,_,_> { + use $crate::Offset; let mut index = 0; - while index < $i.len() { - if let $crate::IResult::Done(i,_) = $normal!(&$i[index..], $($args)*) { - if i.is_empty() { - return $crate::IResult::Done(&$i[$i.input_len()..], $i) - } else { - index = $i.offset(i); - } - } else if $i[index] == $control_char as u8 { - if index + 1 >= $i.len() { - return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Escaped,&$i[index..])); - } else { - match $escapable!(&$i[index+1..], $($args2)*) { - $crate::IResult::Done(i,_) => { - if i.is_empty() { - return $crate::IResult::Done(&$i[$i.input_len()..], $i) - } else { - index = $i.offset(i); + while index < $i.input_len() { + match $normal!($i.slice(index..), $($args)*) { + $crate::IResult::Done(i, _) => { + if i.is_empty() { + return $crate::IResult::Done($i.slice($i.input_len()..), $i) + } else { + index = $i.offset(i); + } + }, + $crate::IResult::Incomplete(i) => { + return $crate::IResult::Incomplete(i) + }, + $crate::IResult::Error(e) => { + if $i[index] == $control_char as u8 { + if index + 1 >= $i.input_len() { + return $crate::IResult::Incomplete($crate::Needed::Unknown) + } else { + match $escapable!($i.slice(index+1..), $($args2)*) { + $crate::IResult::Done(i,_) => { + if i.is_empty() { + return $crate::IResult::Done($i.slice($i.input_len()..), $i) + } else { + index = $i.offset(i); + } + }, + $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i), + $crate::IResult::Error(e2) => return $crate::IResult::Error(e2) } - }, - $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i), - $crate::IResult::Error(e) => return $crate::IResult::Error(e) + } + } else { + return $crate::IResult::Done($i.slice(index..), $i.slice(..index)); } } - } else { - if index == 0 { - return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Escaped,&$i[index..])) - } else { - return $crate::IResult::Done(&$i[index..], &$i[..index]) - } } } $crate::IResult::Done(&$i[index..], &$i[..index]) @@ -292,22 +225,46 @@ macro_rules! escaped_impl ( $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), $crate::IResult::Error(e) => { - return $crate::IResult::Error($crate::Err::NodePosition($crate::ErrorKind::Escaped, $i, Box::new(e))) + return $crate::IResult::Error(error_node_position!($crate::ErrorKind::Escaped, $i, e)) } } } ); + // Internal parser, do not use directly + (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped!(__impl $i, $submac1!($($args)*), $control_char, $submac2!($($args2)*)) + } + ); + // Internal parser, do not use directly + (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => ( + escaped!(__impl $i, $submac1!($($args)*), $control_char, call!($g)) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => ( + { + let input: &[u8] = $i; + + escaped!(__impl_1 input, $submac!($($args)*), $control_char, $($rest)*) + } + ); + + ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => ( + escaped!(__impl_1 $i, call!($f), $control_char, $($rest)*) + ); ); /// `escaped_transform!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], Vec>` /// matches a byte string with escaped characters. /// /// The first argument matches the normal characters (it must not match the control character), the second argument is the control character (like `\` in most languages), -/// the third argument matches the escaped characters and trnasforms them. +/// the third argument matches the escaped characters and transforms them. /// /// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) /// -/// ``` +/// WARNING: if you do not use the `verbose-errors` feature, this combinator will currently fail to build +/// because of a type inference error +/// +/// ```ignore /// # #[macro_use] extern crate nom; /// # use nom::IResult::Done; /// # use nom::alpha; @@ -333,59 +290,32 @@ macro_rules! escaped_impl ( /// ``` #[macro_export] macro_rules! escaped_transform ( - ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => ( - { - escaped_transform1!($i, $submac!($($args)*), $control_char, $($rest)*) - } - ); - - ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => ( - escaped_transform1!($i, call!($f), $control_char, $($rest)*) - ); -); - -/// Internal parser, do not use directly -#[doc(hidden)] -#[macro_export] -macro_rules! escaped_transform1 ( - ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( - { - escaped_transform_impl!($i, $submac1!($($args)*), $control_char, $submac2!($($args2)*)) - } - ); - ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => ( - escaped_transform_impl!($i, $submac1!($($args)*), $control_char, call!($g)) - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! escaped_transform_impl ( - ($i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $transform:ident!( $($args2:tt)* )) => ( + // Internal parser, do not use directly + (__impl $i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $transform:ident!( $($args2:tt)* )) => ( { - use $crate::InputLength; + use $crate::{InputLength,Slice}; let cl = || { - use $crate::HexDisplay; + use $crate::Offset; let mut index = 0; let mut res = Vec::new(); - while index < $i.len() { - if let $crate::IResult::Done(i,o) = $normal!(&$i[index..], $($args)*) { + while index < $i.input_len() { + if let $crate::IResult::Done(i,o) = $normal!($i.slice(index..), $($args)*) { res.extend(o.iter().cloned()); if i.is_empty() { - return $crate::IResult::Done(&$i[$i.input_len()..], res) + return $crate::IResult::Done($i.slice($i.input_len()..), res); } else { index = $i.offset(i); } } else if $i[index] == $control_char as u8 { - if index + 1 >= $i.len() { - return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::EscapedTransform,&$i[index..])); + if index + 1 >= $i.input_len() { + return $crate::IResult::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..))); } else { - match $transform!(&$i[index+1..], $($args2)*) { + match $transform!($i.slice(index+1..), $($args2)*) { $crate::IResult::Done(i,o) => { res.extend(o.iter().cloned()); if i.is_empty() { - return $crate::IResult::Done(&$i[$i.input_len()..], res) + return $crate::IResult::Done($i.slice($i.input_len()..), res) } else { index = $i.offset(i); } @@ -396,23 +326,44 @@ macro_rules! escaped_transform_impl ( } } else { if index == 0 { - return $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::EscapedTransform,&$i[index..])) + return $crate::IResult::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..))) } else { - return $crate::IResult::Done(&$i[index..], res) + return $crate::IResult::Done($i.slice(index..), res) } } } - $crate::IResult::Done(&$i[index..], res) + $crate::IResult::Done($i.slice(index..), res) }; match cl() { $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), $crate::IResult::Error(e) => { - return $crate::IResult::Error($crate::Err::NodePosition($crate::ErrorKind::EscapedTransform, $i, Box::new(e))) + return $crate::IResult::Error(error_node_position!($crate::ErrorKind::EscapedTransform, $i, e)) } } } - ) + ); + // Internal parser, do not use directly + (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_transform!(__impl $i, $submac1!($($args)*), $control_char, $submac2!($($args2)*)) + } + ); + // Internal parser, do not use directly + (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => ( + escaped_transform_impl!($i, $submac1!($($args)*), $control_char, call!($g)) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => ( + { + let input: &[u8] = $i; + + escaped_transform!(__impl_1 input, $submac!($($args)*), $control_char, $($rest)*) + } + ); + + ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => ( + escaped_transform!(__impl_1 $i, call!($f), $control_char, $($rest)*) + ); ); /// `take_while!(T -> bool) => &[T] -> IResult<&[T], &[T]>` @@ -435,13 +386,16 @@ macro_rules! escaped_transform_impl ( macro_rules! take_while ( ($input:expr, $submac:ident!( $($args:tt)* )) => ( { - match $input.iter().position(|c| !$submac!(*c, $($args)*)) { + use $crate::{InputLength,InputIter,Slice}; + let input = $input; + + match input.position(|c| !$submac!(c, $($args)*)) { Some(n) => { - let res:$crate::IResult<_,_> = $crate::IResult::Done(&$input[n..], &$input[..n]); + let res:$crate::IResult<_,_> = $crate::IResult::Done(input.slice(n..), input.slice(..n)); res }, None => { - $crate::IResult::Done(&$input[($input).len()..], $input) + $crate::IResult::Done(input.slice(input.input_len()..), input) } } } @@ -451,7 +405,7 @@ macro_rules! take_while ( ); ); -/// `take_while1!(&[T] -> bool) => &[T] -> IResult<&[T], &[T]>` +/// `take_while1!(T -> bool) => &[T] -> IResult<&[T], &[T]>` /// returns the longest (non empty) list of bytes until the provided function fails. /// /// The argument is either a function `&[T] -> bool` or a macro returning a `bool @@ -459,17 +413,21 @@ macro_rules! take_while ( macro_rules! take_while1 ( ($input:expr, $submac:ident!( $($args:tt)* )) => ( { + let input = $input; + use $crate::InputLength; - if ($input).input_len() == 0 { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeWhile1,$input)) + use $crate::InputIter; + use $crate::Slice; + if input.input_len() == 0 { + $crate::IResult::Incomplete($crate::Needed::Size(1)) } else { - match $input.iter().position(|c| !$submac!(*c, $($args)*)) { - Some(0) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeWhile1,$input)), + match input.position(|c| !$submac!(c, $($args)*)) { + Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeWhile1,input)), Some(n) => { - $crate::IResult::Done(&$input[n..], &$input[..n]) + $crate::IResult::Done(input.slice(n..), input.slice(..n)) }, None => { - $crate::IResult::Done(&$input[($input).len()..], $input) + $crate::IResult::Done(input.slice(input.input_len()..), input) } } } @@ -488,10 +446,14 @@ macro_rules! take_while1 ( macro_rules! take_till ( ($input:expr, $submac:ident!( $($args:tt)* )) => ( { + let input = $input; + use $crate::InputLength; - match $input.iter().position(|c| $submac!(c, $($args)*)) { - Some(n) => $crate::IResult::Done(&$input[n..], &$input[..n]), - None => $crate::IResult::Done(&$input[($input).input_len()..], $input) + use $crate::InputIter; + use $crate::Slice; + match input.position(|c| $submac!(c, $($args)*)) { + Some(n) => $crate::IResult::Done(input.slice(n..), input.slice(..n)), + None => $crate::IResult::Done(input.slice(input.input_len()..), input) } } ); @@ -500,6 +462,35 @@ macro_rules! take_till ( ); ); +/// `take_till1!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest non empty list of bytes until the provided function succeeds +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool +#[macro_export] +macro_rules! take_till1 ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + let input = $input; + + use $crate::InputLength; + use $crate::InputIter; + use $crate::Slice; + if input.input_len() == 0 { + $crate::IResult::Incomplete($crate::Needed::Size(1)) + } else { + match input.position(|c| $submac!(c, $($args)*)) { + Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeTill1,input)), + Some(n) => $crate::IResult::Done(input.slice(n..), input.slice(..n)), + None => $crate::IResult::Done(input.slice(input.input_len()..), input) + } + } + } + ); + ($input:expr, $f:expr) => ( + take_till1!($input, call!($f)); + ); +); + /// `take!(nb) => &[T] -> IResult<&[T], &[T]>` /// generates a parser consuming the specified number of bytes /// @@ -519,69 +510,80 @@ macro_rules! take_till ( macro_rules! take ( ($i:expr, $count:expr) => ( { + use $crate::InputIter; + use $crate::Slice; + let input = $i; + let cnt = $count as usize; - let res: $crate::IResult<_,_> = if $i.len() < cnt { - $crate::IResult::Incomplete($crate::Needed::Size(cnt)) - } else { - $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) + + let res: $crate::IResult<_,_> = match input.slice_index(cnt) { + None => $crate::IResult::Incomplete($crate::Needed::Size(cnt)), + //FIXME: use the InputTake trait + Some(index) => $crate::IResult::Done(input.slice(index..), input.slice(..index)) }; res } ); ); -/// `take!(nb) => &[T] -> IResult<&[T], &str>` +/// `take_str!(nb) => &[T] -> IResult<&[T], &str>` /// same as take! but returning a &str #[macro_export] macro_rules! take_str ( - ( $i:expr, $size:expr ) => ( map_res!($i, take!($size), ::std::str::from_utf8) ); + ( $i:expr, $size:expr ) => ( + { + let input: &[u8] = $i; + + map_res!(input, take!($size), ::std::str::from_utf8) + } + ); ); /// `take_until_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>` /// generates a parser consuming bytes until the specified byte sequence is found, and consumes it #[macro_export] -macro_rules! take_until_and_consume( - ($i:expr, $inp:expr) => ( +macro_rules! take_until_and_consume ( + ($i:expr, $substr:expr) => ( { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } + use $crate::InputLength; + use $crate::FindSubstring; + use $crate::Slice; - let expected = $inp; - let bytes = as_bytes(&expected); - take_until_and_consume_bytes!($i, bytes) + let res: $crate::IResult<_,_> = if $substr.input_len() > $i.input_len() { + $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len())) + } else { + match ($i).find_substring($substr) { + None => { + $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i)) + }, + Some(index) => { + $crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index)) + }, + } + }; + res } ); ); -#[doc(hidden)] +/// `take_until_and_consume1!(tag) => &[T] -> IResult<&[T], &[T]>` +/// generates a parser consuming bytes (at least 1) until the specified byte sequence is found, and consumes it #[macro_export] -macro_rules! take_until_and_consume_bytes ( - ($i:expr, $bytes:expr) => ( +macro_rules! take_until_and_consume1 ( + ($i:expr, $substr:expr) => ( { - let res: $crate::IResult<_,_> = if $bytes.len() > $i.len() { - $crate::IResult::Incomplete($crate::Needed::Size($bytes.len())) - } else { - let mut index = 0; - let mut parsed = false; - - for idx in 0..$i.len() { - if idx + $bytes.len() > $i.len() { - index = idx; - break; - } - if &$i[idx..idx + $bytes.len()] == $bytes { - parsed = true; - index = idx; - break; - } - } + use $crate::InputLength; - if parsed { - $crate::IResult::Done(&$i[(index + $bytes.len())..], &$i[0..index]) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilAndConsume,$i)) + let res: $crate::IResult<_,_> = if 1 + $substr.input_len() > $i.input_len() { + $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len())) + } else { + match ($i).find_substring($substr) { + None => { + $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i)) + }, + Some(index) => { + $crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index)) + }, } }; res @@ -592,48 +594,50 @@ macro_rules! take_until_and_consume_bytes ( /// `take_until!(tag) => &[T] -> IResult<&[T], &[T]>` /// consumes data until it finds the specified tag #[macro_export] -macro_rules! take_until( - ($i:expr, $inp:expr) => ( +macro_rules! take_until ( + ($i:expr, $substr:expr) => ( { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } + use $crate::InputLength; + use $crate::FindSubstring; + use $crate::Slice; - let expected = $inp; - let bytes = as_bytes(&expected); - take_until_bytes!($i, bytes) + let res: $crate::IResult<_,_> = if $substr.input_len() > $i.input_len() { + $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len())) + } else { + match ($i).find_substring($substr) { + None => { + $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i)) + }, + Some(index) => { + $crate::IResult::Done($i.slice(index..), $i.slice(0..index)) + }, + } + }; + res } ); ); -#[doc(hidden)] +/// `take_until1!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data until it finds the specified tag #[macro_export] -macro_rules! take_until_bytes( - ($i:expr, $bytes:expr) => ( +macro_rules! take_until1 ( + ($i:expr, $substr:expr) => ( { - let res: $crate::IResult<_,_> = if $bytes.len() > $i.len() { - $crate::IResult::Incomplete($crate::Needed::Size($bytes.len())) - } else { - let mut index = 0; - let mut parsed = false; - - for idx in 0..$i.len() { - if idx + $bytes.len() > $i.len() { - index = idx; - break; - } - if &$i[idx..idx+$bytes.len()] == $bytes { - parsed = true; - index = idx; - break; - } - } + use $crate::InputLength; + use $crate::FindSubstring; + use $crate::Slice; - if parsed { - $crate::IResult::Done(&$i[index..], &$i[0..index]) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntil,$i)) + let res: $crate::IResult<_,_> = if 1+$substr.input_len() > $i.input_len() { + $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len())) + } else { + match ($i).find_substring($substr) { + None => { + $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i)) + }, + Some(index) => { + $crate::IResult::Done($i.slice(index..), $i.slice(0..index)) + }, } }; res @@ -644,136 +648,78 @@ macro_rules! take_until_bytes( /// `take_until_either_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>` /// consumes data until it finds any of the specified characters, and consume it #[macro_export] -macro_rules! take_until_either_and_consume( - ($i:expr, $inp:expr) => ( +macro_rules! take_until_either_and_consume ( + ($input:expr, $arr:expr) => ( { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $inp; - let bytes = as_bytes(&expected); - take_until_either_and_consume_bytes!($i, bytes) - } - ); -); + use $crate::InputLength; + use $crate::InputIter; + use $crate::FindToken; + use $crate::Slice; -#[doc(hidden)] -#[macro_export] -macro_rules! take_until_either_and_consume_bytes( - ($i:expr, $bytes:expr) => ( - { - let res: $crate::IResult<_,_> = if 1 > $i.len() { + if $input.input_len() == 0 { $crate::IResult::Incomplete($crate::Needed::Size(1)) } else { - let mut index = 0; - let mut parsed = false; - - for idx in 0..$i.len() { - if idx + 1 > $i.len() { - index = idx; - break; - } - for &t in $bytes.iter() { - if $i[idx] == t { - parsed = true; - index = idx; - break; - } + let res: $crate::IResult<_,_> = match $input.position(|c| { + c.find_token($arr) + }) { + Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input)), + Some(n) => { + let res = $crate::IResult::Done($input.slice(n+1..), $input.slice(..n)); + res + }, + None => { + $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input)) } - if parsed { break; } - } - - if parsed { - $crate::IResult::Done(&$i[(index+1)..], &$i[0..index]) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilEitherAndConsume,$i)) - } - }; - res + }; + res + } } ); ); /// `take_until_either!(tag) => &[T] -> IResult<&[T], &[T]>` #[macro_export] -macro_rules! take_until_either( - ($i:expr, $inp:expr) => ( +macro_rules! take_until_either ( + ($input:expr, $arr:expr) => ( { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $inp; - let bytes = as_bytes(&expected); - take_until_either_bytes!($i, bytes) - } - ); -); + use $crate::InputLength; + use $crate::InputIter; + use $crate::FindToken; + use $crate::Slice; -#[doc(hidden)] -#[macro_export] -macro_rules! take_until_either_bytes( - ($i:expr, $bytes:expr) => ( - { - let res: $crate::IResult<_,_> = if 1 > $i.len() { + if $input.input_len() == 0 { $crate::IResult::Incomplete($crate::Needed::Size(1)) } else { - let mut index = 0; - let mut parsed = false; - - for idx in 0..$i.len() { - if idx + 1 > $i.len() { - index = idx; - break; - } - for &t in $bytes.iter() { - if $i[idx] == t { - parsed = true; - index = idx; - break; - } + let res: $crate::IResult<_,_> = match $input.position(|c| { + c.find_token($arr) + }) { + Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input)), + Some(n) => { + let res = $crate::IResult::Done($input.slice(n..), $input.slice(..n)); + res + }, + None => { + $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input)) } - if parsed { break; } - } - - if parsed { - $crate::IResult::Done(&$i[index..], &$i[0..index]) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilEither,$i)) - } - }; - res + }; + res + } } ); ); -/// `length_bytes!(&[T] -> IResult<&[T], nb>) => &[T] -> IResult<&[T], &[T]> -/// gets a number from the first parser, then extracts that many bytes from the +/// `length_bytes!(&[T] -> IResult<&[T], nb>) => &[T] -> IResult<&[T], &[T]>` +/// Gets a number from the first parser, then extracts that many bytes from the /// remaining stream #[macro_export] macro_rules! length_bytes( ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - match $submac!($i, $($args)*) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i1,nb) => { - let nb = nb as usize; - let length_remaining = i1.len(); - if length_remaining < nb { - $crate::IResult::Incomplete($crate::Needed::Size(nb - length_remaining)) - } else { - $crate::IResult::Done(&i1[nb..], &i1[..nb]) - } - } - } + length_data!($i, $submac!($($args)*)) } ); ($i:expr, $f:expr) => ( - length_bytes!($i, call!($f)) + length_data!($i, call!($f)) ) ); @@ -781,10 +727,53 @@ macro_rules! length_bytes( mod tests { use internal::Needed; use internal::IResult::*; - use internal::Err::*; use util::ErrorKind; use nom::{alpha, digit, hex_digit, oct_digit, alphanumeric, space, multispace}; + macro_rules! one_of ( + ($i:expr, $inp: expr) => ( + { + if $i.is_empty() { + $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) + } else { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + one_of_bytes!($i, bytes) + } + } + ); + ); + + macro_rules! one_of_bytes ( + ($i:expr, $bytes: expr) => ( + { + if $i.is_empty() { + $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) + } else { + let mut found = false; + + for &i in $bytes { + if i == $i[0] { + found = true; + break; + } + } + + if found { + $crate::IResult::Done(&$i[1..], $i[0] as char) + } else { + $crate::IResult::Error(error_position!($crate::ErrorKind::OneOf, $i)) + } + } + } + ); + ); + #[test] fn is_a() { named!(a_or_b, is_a!(&b"ab"[..])); @@ -796,7 +785,7 @@ mod tests { assert_eq!(a_or_b(b), Done(&b"cde"[..], &b"b"[..])); let c = &b"cdef"[..]; - assert_eq!(a_or_b(c), Error(Position(ErrorKind::IsA,c))); + assert_eq!(a_or_b(c), Error(error_position!(ErrorKind::IsA,c))); let d = &b"bacdef"[..]; assert_eq!(a_or_b(d), Done(&b"cdef"[..], &b"ba"[..])); @@ -813,7 +802,7 @@ mod tests { assert_eq!(a_or_b(b), Done(&b"bde"[..], &b"c"[..])); let c = &b"abab"[..]; - assert_eq!(a_or_b(c), Error(Position(ErrorKind::IsNot,c))); + assert_eq!(a_or_b(c), Error(error_position!(ErrorKind::IsNot,c))); let d = &b"cdefba"[..]; assert_eq!(a_or_b(d), Done(&b"ba"[..], &b"cdef"[..])); @@ -825,27 +814,34 @@ mod tests { assert_eq!(a_or_b(f), Done(&b""[..], &b"fghi"[..])); } + #[allow(unused_variables)] #[test] fn escaping() { - named!(esc, escaped!(call!(alpha), '\\', is_a_bytes!(&b"\"n\\"[..]))); + named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\"))); assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..])); assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..])); assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], &b"\\\"abcd"[..])); assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], &b"\\n"[..])); assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], &b"ab\\\""[..])); - assert_eq!(esc(&b"AB\\"[..]), Error(NodePosition(ErrorKind::Escaped, &b"AB\\"[..], Box::new(Position(ErrorKind::Escaped, &b"\\"[..]))))); - assert_eq!(esc(&b"AB\\A"[..]), Error(NodePosition(ErrorKind::Escaped, &b"AB\\A"[..], Box::new(Position(ErrorKind::IsA, &b"A"[..]))))); + assert_eq!(esc(&b"AB\\"[..]), Incomplete(Needed::Unknown)); + assert_eq!(esc(&b"AB\\A"[..]), Error(error_node_position!(ErrorKind::Escaped, &b"AB\\A"[..], + error_position!(ErrorKind::OneOf, &b"A"[..])))); + + named!(esc2, escaped!(call!(digit), '\\', one_of!("\"n\\"))); + assert_eq!(esc2(&b"12\\nnn34"[..]), Done(&b"nn34"[..], &b"12\\n"[..])); } + #[cfg(feature = "verbose-errors")] fn to_s(i:Vec) -> String { String::from_utf8_lossy(&i).into_owned() } + #[cfg(feature = "verbose-errors")] #[test] fn escape_transform() { use std::str; - named!(esc< String >, map!(escaped_transform!(alpha, '\\', + named!(esc, map!(escaped_transform!(alpha, '\\', alt!( tag!("\\") => { |_| &b"\\"[..] } | tag!("\"") => { |_| &b"\""[..] } @@ -858,8 +854,9 @@ mod tests { assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], String::from("\"abcd"))); assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], String::from("\n"))); assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], String::from("ab\""))); - assert_eq!(esc(&b"AB\\"[..]), Error(NodePosition(ErrorKind::EscapedTransform, &b"AB\\"[..], Box::new(Position(ErrorKind::EscapedTransform, &b"\\"[..]))))); - assert_eq!(esc(&b"AB\\A"[..]), Error(NodePosition(ErrorKind::EscapedTransform, &b"AB\\A"[..], Box::new(Position(ErrorKind::Alt, &b"A"[..]))))); + assert_eq!(esc(&b"AB\\"[..]), Error(error_node_position!(ErrorKind::EscapedTransform, &b"AB\\"[..], error_position!(ErrorKind::EscapedTransform, &b"\\"[..])))); + assert_eq!(esc(&b"AB\\A"[..]), Error(error_node_position!(ErrorKind::EscapedTransform, &b"AB\\A"[..], + error_position!(ErrorKind::Alt, &b"A"[..])))); let e = "è"; let a = "à"; @@ -893,6 +890,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn take_until_test() { named!(x, take_until_and_consume!("efgh")); let r = x(&b"abcdabcdefghijkl"[..]); @@ -905,7 +903,7 @@ mod tests { println!("Done 2\n"); let r3 = x(&b"abcefg"[..]); - assert_eq!(r3, Error(Position(ErrorKind::TakeUntilAndConsume, &b"abcefg"[..]))); + assert_eq!(r3, Error(error_position!(ErrorKind::TakeUntilAndConsume, &b"abcefg"[..]))); assert_eq!( x(&b"ab"[..]), @@ -913,15 +911,34 @@ mod tests { ); } + #[test] + fn take_until_either() { + named!(x, take_until_either!("!.")); + assert_eq!( + x(&b"123!abc"[..]), + Done(&b"!abc"[..], &b"123"[..]) + ); + } + #[test] fn take_until_either_incomplete() { named!(x, take_until_either!("!.")); assert_eq!( x(&b"123"[..]), - Error(Position(ErrorKind::TakeUntilEither, &b"123"[..])) + Error(error_position!(ErrorKind::TakeUntilEither, &b"123"[..])) + ); + } + + #[test] + fn take_until_either_and_consume() { + named!(x, take_until_either_and_consume!("!.")); + assert_eq!( + x(&b"123.abc"[..]), + Done(&b"abc"[..], &b"123"[..]) ); } + #[test] fn take_until_incomplete() { named!(y, take_until!("end")); @@ -931,7 +948,7 @@ mod tests { ); assert_eq!( y(&b"123"[..]), - Error(Position(ErrorKind::TakeUntil, &b"123"[..])) + Error(error_position!(ErrorKind::TakeUntil, &b"123"[..])) ); } @@ -996,10 +1013,40 @@ mod tests { let c = b"abcd123"; let d = b"123"; - assert_eq!(f(&a[..]), Error(Position(ErrorKind::TakeWhile1, &b""[..]))); + assert_eq!(f(&a[..]), Incomplete(Needed::Size(1))); assert_eq!(f(&b[..]), Done(&a[..], &b[..])); assert_eq!(f(&c[..]), Done(&b"123"[..], &b[..])); - assert_eq!(f(&d[..]), Error(Position(ErrorKind::TakeWhile1, &d[..]))); + assert_eq!(f(&d[..]), Error(error_position!(ErrorKind::TakeWhile1, &d[..]))); + } + + #[test] + fn take_till() { + use nom::is_alphabetic; + named!(f, take_till!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"123abcd"; + let d = b"123"; + + assert_eq!(f(&a[..]), Done(&b""[..], &b""[..])); + assert_eq!(f(&b[..]), Done(&b"abcd"[..], &b""[..])); + assert_eq!(f(&c[..]), Done(&b"abcd"[..], &b"123"[..])); + assert_eq!(f(&d[..]), Done(&b""[..], &b"123"[..])); + } + + #[test] + fn take_till1() { + use nom::is_alphabetic; + named!(f, take_till1!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"123abcd"; + let d = b"123"; + + assert_eq!(f(&a[..]), Incomplete(Needed::Size(1))); + assert_eq!(f(&b[..]), Error(error_position!(ErrorKind::TakeTill1, &b[..]))); + assert_eq!(f(&c[..]), Done(&b"abcd"[..], &b"123"[..])); + assert_eq!(f(&d[..]), Done(&b""[..], &b"123"[..])); } #[cfg(feature = "nightly")] @@ -1016,6 +1063,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn recognize_take_while() { use nom::is_alphanumeric; named!(x, take_while!(is_alphanumeric)); @@ -1024,4 +1072,48 @@ mod tests { println!("X: {:?}", x(&b"ab"[..])); assert_eq!(y(&b"ab"[..]), Done(&[][..], &b"ab"[..])); } + + #[test] + fn length_bytes() { + use nom::le_u8; + named!(x, length_bytes!(le_u8)); + assert_eq!(x(b"\x02..>>"), Done(&b">>"[..], &b".."[..])); + assert_eq!(x(b"\x02.."), Done(&[][..], &b".."[..])); + assert_eq!(x(b"\x02."), Incomplete(Needed::Size(3))); + assert_eq!(x(b"\x02"), Incomplete(Needed::Size(3))); + + named!(y, do_parse!(tag!("magic") >> b: length_bytes!(le_u8) >> (b))); + assert_eq!(y(b"magic\x02..>>"), Done(&b">>"[..], &b".."[..])); + assert_eq!(y(b"magic\x02.."), Done(&[][..], &b".."[..])); + assert_eq!(y(b"magic\x02."), Incomplete(Needed::Size(8))); + assert_eq!(y(b"magic\x02"), Incomplete(Needed::Size(8))); + } + + #[test] + fn case_insensitive() { + named!(test, tag_no_case!("ABcd")); + assert_eq!(test(&b"aBCdefgh"[..]), Done(&b"efgh"[..], &b"aBCd"[..])); + assert_eq!(test(&b"abcdefgh"[..]), Done(&b"efgh"[..], &b"abcd"[..])); + assert_eq!(test(&b"ABCDefgh"[..]), Done(&b"efgh"[..], &b"ABCD"[..])); + assert_eq!(test(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(test(&b"Hello"[..]), Error(error_position!(ErrorKind::Tag, &b"Hello"[..]))); + assert_eq!(test(&b"Hel"[..]), Error(error_position!(ErrorKind::Tag, &b"Hel"[..]))); + + named!(test2<&str, &str>, tag_no_case!("ABcd")); + assert_eq!(test2("aBCdefgh"), Done("efgh", "aBCd")); + assert_eq!(test2("abcdefgh"), Done("efgh", "abcd")); + assert_eq!(test2("ABCDefgh"), Done("efgh", "ABCD")); + assert_eq!(test2("ab"), Incomplete(Needed::Size(4))); + assert_eq!(test2("Hello"), Error(error_position!(ErrorKind::Tag, &"Hello"[..]))); + assert_eq!(test2("Hel"), Error(error_position!(ErrorKind::Tag, &"Hel"[..]))); + } + + #[test] + fn tag_fixed_size_array() { + named!(test, tag!([0x42])); + named!(test2, tag!(&[0x42])); + let input = [0x42, 0x00]; + assert_eq!(test(&input), Done(&b"\x00"[..], &b"\x42"[..])); + assert_eq!(test2(&input), Done(&b"\x00"[..], &b"\x42"[..])); + } } diff --git third_party/rust/nom/src/character.rs third_party/rust/nom/src/character.rs index 781065b35270..6748e0790b68 100644 --- third_party/rust/nom/src/character.rs +++ third_party/rust/nom/src/character.rs @@ -1,51 +1,39 @@ /// Character level parsers -use internal::{IResult,Needed,Err}; -use util::ErrorKind; +use internal::{IResult,Needed}; +use traits::{AsChar,InputIter,InputLength,Slice}; +use std::ops::RangeFrom; /// matches one of the provided characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// named!(simple, one_of!(&b"abc"[..])); +/// assert_eq!(simple(b"a123"), IResult::Done(&b"123"[..], 'a')); +/// +/// named!(a_or_b<&str, char>, one_of!("ab汉")); +/// assert_eq!(a_or_b("汉jiosfe"), IResult::Done("jiosfe", '汉')); +/// # } +/// ``` #[macro_export] macro_rules! one_of ( ($i:expr, $inp: expr) => ( { - if $i.is_empty() { - $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) - } else { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $inp; - let bytes = as_bytes(&expected); - one_of_bytes!($i, bytes) - } - } - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! one_of_bytes ( - ($i:expr, $bytes: expr) => ( - { - if $i.is_empty() { - $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) - } else { - let mut found = false; - - for &i in $bytes { - if i == $i[0] { - found = true; - break; - } - } - - if found { - $crate::IResult::Done(&$i[1..], $i[0] as char) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::OneOf, $i)) - } + use $crate::Slice; + use $crate::AsChar; + use $crate::FindToken; + use $crate::InputIter; + + match ($i).iter_elements().next().map(|c| { + (c, c.find_token($inp)) + }) { + None => $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)), + Some((_, false)) => $crate::IResult::Error(error_position!($crate::ErrorKind::OneOf, $i)), + //the unwrap should be safe here + Some((c, true)) => $crate::IResult::Done($i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char()) } } ); @@ -56,44 +44,18 @@ macro_rules! one_of_bytes ( macro_rules! none_of ( ($i:expr, $inp: expr) => ( { - if $i.is_empty() { - $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) - } else { - #[inline(always)] - fn as_bytes(b: &T) -> &[u8] { - b.as_bytes() - } - - let expected = $inp; - let bytes = as_bytes(&expected); - none_of_bytes!($i, bytes) - } - } - ); -); - -#[doc(hidden)] -#[macro_export] -macro_rules! none_of_bytes ( - ($i:expr, $bytes: expr) => ( - { - if $i.is_empty() { - $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)) - } else { - let mut found = false; - - for &i in $bytes { - if i == $i[0] { - found = true; - break; - } - } - - if !found { - $crate::IResult::Done(&$i[1..], $i[0] as char) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::NoneOf, $i)) - } + use $crate::Slice; + use $crate::AsChar; + use $crate::FindToken; + use $crate::InputIter; + + match ($i).iter_elements().next().map(|c| { + (c, !c.find_token($inp)) + }) { + None => $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)), + Some((_, false)) => $crate::IResult::Error(error_position!($crate::ErrorKind::NoneOf, $i)), + //the unwrap should be safe here + Some((c, true)) => $crate::IResult::Done($i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char()) } } ); @@ -104,49 +66,39 @@ macro_rules! none_of_bytes ( macro_rules! char ( ($i:expr, $c: expr) => ( { - if $i.is_empty() { - let res: $crate::IResult<&[u8], char> = $crate::IResult::Incomplete($crate::Needed::Size(1)); - res - } else { - if $i[0] == $c as u8 { - $crate::IResult::Done(&$i[1..], $i[0] as char) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Char, $i)) - } + use $crate::Slice; + use $crate::AsChar; + use $crate::InputIter; + + match ($i).iter_elements().next().map(|c| { + (c, c.as_char() == $c) + }) { + None => $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)), + Some((_, false)) => $crate::IResult::Error(error_position!($crate::ErrorKind::Char, $i)), + //the unwrap should be safe here + Some((c, true)) => $crate::IResult::Done($i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char()) } } ); ); -named!(pub newline, char!('\n')); +named!(#[doc="Matches a newline character '\\n'"], pub newline, char!('\n')); -pub fn crlf(input:&[u8]) -> IResult<&[u8], char> { - if input.len() < 2 { - IResult::Incomplete(Needed::Size(2)) - } else { - if &input[0..2] == &b"\r\n"[..] { - IResult::Done(&input[2..], '\n') - } else { - IResult::Error(Err::Position(ErrorKind::CrLf, input)) - } - } -} - -named!(pub eol, alt!(crlf | newline)); -named!(pub tab, char!('\t')); +named!(#[doc="Matches a tab character '\\t'"], pub tab, char!('\t')); -pub fn anychar(input:&[u8]) -> IResult<&[u8], char> { - if input.is_empty() { +pub fn anychar(input: T) -> IResult where + T: InputIter+InputLength+Slice>, + ::Item: AsChar { + if input.input_len() == 0 { IResult::Incomplete(Needed::Size(1)) } else { - IResult::Done(&input[1..], input[0] as char) + IResult::Done(input.slice(1..), input.iter_elements().next().expect("slice should contain at least one element").as_char()) } } #[cfg(test)] mod tests { use internal::IResult::*; - use internal::Err::*; use util::ErrorKind; #[test] @@ -157,7 +109,13 @@ mod tests { assert_eq!(f(a), Done(&b"bcd"[..], 'a')); let b = &b"cde"[..]; - assert_eq!(f(b), Error(Position(ErrorKind::OneOf, b))); + assert_eq!(f(b), Error(error_position!(ErrorKind::OneOf, b))); + + named!(utf8(&str) -> char, + one_of!("+\u{FF0B}")); + + assert!(utf8("+").is_done()); + assert!(utf8("\u{FF0B}").is_done()); } #[test] @@ -165,7 +123,7 @@ mod tests { named!(f, none_of!("ab")); let a = &b"abcd"[..]; - assert_eq!(f(a), Error(Position(ErrorKind::NoneOf, a))); + assert_eq!(f(a), Error(error_position!(ErrorKind::NoneOf, a))); let b = &b"cde"[..]; assert_eq!(f(b), Done(&b"de"[..], 'c')); @@ -176,9 +134,10 @@ mod tests { named!(f, char!('c')); let a = &b"abcd"[..]; - assert_eq!(f(a), Error(Position(ErrorKind::Char, a))); + assert_eq!(f(a), Error(error_position!(ErrorKind::Char, a))); let b = &b"cde"[..]; assert_eq!(f(b), Done(&b"de"[..], 'c')); } + } diff --git third_party/rust/nom/src/internal.rs third_party/rust/nom/src/internal.rs index deb39a86654d..c5a51ee2bb09 100644 --- third_party/rust/nom/src/internal.rs +++ third_party/rust/nom/src/internal.rs @@ -2,27 +2,15 @@ use self::IResult::*; use self::Needed::*; -use util::ErrorKind; -#[cfg(feature = "core")] +#[cfg(not(feature = "std"))] use std::prelude::v1::*; -use std::boxed::Box; -/// Contains the error that a parser can return -/// -/// It can represent a linked list of errors, indicating the path taken in the parsing tree, with corresponding position in the input data. -/// It depends on P, the input position (for a &[u8] parser, it would be a &[u8]), and E, the custom error type (by default, u32) -#[derive(Debug,PartialEq,Eq,Clone)] -pub enum Err{ - /// An error code, represented by an ErrorKind, which can contain a custom error code represented by E - Code(ErrorKind), - /// An error code, and the next error - Node(ErrorKind, Box>), - /// An error code, and the input position - Position(ErrorKind, P), - /// An error code, the input position and the next error - NodePosition(ErrorKind, P, Box>) -} +#[cfg(feature = "verbose-errors")] +use verbose_errors::Err; + +#[cfg(not(feature = "verbose-errors"))] +use simple_errors::Err; /// Contains information on needed data if a parser returned `Incomplete` #[derive(Debug,PartialEq,Eq,Clone,Copy)] @@ -48,10 +36,23 @@ impl Needed { } } +#[cfg(feature = "verbose-errors")] /// Holds the result of parsing functions /// /// It depends on I, the input type, O, the output type, and E, the error type (by default u32) /// +/// Depending on a compilation flag, the content of the `Error` variant +/// can change. By default, it will be a `ErrorKind` (with `E` configurable). +/// +/// If you activate the `verbose-errors` compilation flags, it will be an +/// enum that contains an error code, optionally, an input position, +/// and an error sent by child parsers. +/// +/// The verbose errors feature allows very flexible error management: +/// you can know precisely which parser got to which part of the input. +/// The main drawback is that it is a lot slower than default error +/// management. +/// #[derive(Debug,PartialEq,Eq,Clone)] pub enum IResult { /// indicates a correct parsing, the first field containing the rest of the unparsed data, the second field contains the parsed data @@ -62,6 +63,41 @@ pub enum IResult { Incomplete(Needed) } +#[cfg(not(feature = "verbose-errors"))] +/// Holds the result of parsing functions +/// +/// It depends on I, the input type, O, the output type, and E, the error type (by default u32) +/// +#[derive(Debug,PartialEq,Eq,Clone)] +pub enum IResult { + /// indicates a correct parsing, the first field containing the rest of the unparsed data, the second field contains the parsed data + Done(I,O), + /// contains a Err, an enum that can indicate an error code, a position in the input, and a pointer to another error, making a list of errors in the parsing tree + Error(Err), + /// Incomplete contains a Needed, an enum than can represent a known quantity of input data, or unknown + Incomplete(Needed) +} + +#[cfg(feature = "verbose-errors")] +/// This is the same as IResult, but without Done +/// +/// This is used as the Error type when converting to std::result::Result +#[derive(Debug,PartialEq,Eq,Clone)] +pub enum IError { + Error(Err), + Incomplete(Needed) +} + +#[cfg(not(feature = "verbose-errors"))] +/// This is the same as IResult, but without Done +/// +/// This is used as the Error type when converting to std::result::Result +#[derive(Debug,PartialEq,Eq,Clone)] +pub enum IError { + Error(Err), + Incomplete(Needed) +} + impl IResult { pub fn is_done(&self) -> bool { match *self { @@ -84,6 +120,14 @@ impl IResult { } } + pub fn or(self, other: IResult) -> IResult { + if self.is_done() { + self + } else { + other + } + } + /// Maps a `IResult` to `IResult` by appling a function /// to a contained `Done` value, leaving `Error` and `Incomplete` value /// untouched. @@ -109,19 +153,6 @@ impl IResult { } } - /// Maps a `IResult` to `IResult` by appling a function - /// to a contained `Error` value, leaving `Done` and `Incomplete` value - /// untouched. - #[inline] - pub fn map_err(self, f: F) -> IResult - where F: FnOnce(Err) -> Err { - match self { - Error(e) => Error(f(e)), - Incomplete(n) => Incomplete(n), - Done(i, o) => Done(i, o), - } - } - /// Unwrap the contained `Done(I, O)` value, or panic if the `IResult` is not /// `Done`. pub fn unwrap(self) -> (I, O) { @@ -132,23 +163,23 @@ impl IResult { } } - /// Unwrap the contained `Done(I, O)` value, or panic if the `IResult` is not + /// Unwrap the contained `Done(I, O)` value or a default if the `IResult` is not /// `Done`. - pub fn unwrap_inc(self) -> Needed { + pub fn unwrap_or(self, default: (I, O)) -> (I, O) { match self { - Incomplete(n) => n, - Done(_, _) => panic!("unwrap_inc() called on an IResult that is Done"), - Error(_) => panic!("unwrap_inc() called on an IResult that is Error") + Done(i, o) => (i, o), + Incomplete(_) => default, + Error(_) => default } } - /// Unwrap the contained `Done(I, O)` value, or panic if the `IResult` is not - /// `Done`. - pub fn unwrap_err(self) -> Err { + /// Unwrap the contained `Incomplete(n)` value, or panic if the `IResult` is not + /// `Incomplete`. + pub fn unwrap_inc(self) -> Needed { match self { - Error(e) => e, - Done(_, _) => panic!("unwrap_err() called on an IResult that is Done"), - Incomplete(_) => panic!("unwrap_err() called on an IResult that is Incomplete"), + Incomplete(n) => n, + Done(_, _) => panic!("unwrap_inc() called on an IResult that is Done"), + Error(_) => panic!("unwrap_inc() called on an IResult that is Error") } } } @@ -215,35 +246,130 @@ impl<'a,I,E> GetOutput<&'a str> for IResult { } } -#[cfg(not(feature = "core"))] -use std::any::Any; -#[cfg(not(feature = "core"))] -use std::{error,fmt}; -#[cfg(not(feature = "core"))] -use std::fmt::Debug; -#[cfg(not(feature = "core"))] -impl error::Error for Err { - fn description(&self) -> &str { - let kind = match *self { - Err::Code(ref e) | Err::Node(ref e, _) | Err::Position(ref e, _) | Err::NodePosition(ref e, _, _) => e +#[cfg(feature = "verbose-errors")] +/// creates a parse error from a `nom::ErrorKind` +#[macro_export] +macro_rules! error_code( + ($code:expr) => ($crate::Err::Code($code)); +); + +#[cfg(not(feature = "verbose-errors"))] +/// creates a parse error from a `nom::ErrorKind` +#[macro_export] +macro_rules! error_code( + ($code:expr) => ($code); +); + +#[cfg(feature = "verbose-errors")] +/// creates a parse error from a `nom::ErrorKind` +/// and the next error in the parsing tree. +/// if "verbose-errors" is not activated, +/// it default to only the error code +#[macro_export] +macro_rules! error_node( + ($code:expr, $next:expr) => { + let next_errors = match $next { + $crate::Err::Code(e) => { + let mut v = ::std::vec::Vec::new(); + v.push($crate::Err::Code(e)); + v + }, + $crate::Err::Position(e, p) => { + let mut v = ::std::vec::Vec::new(); + v.push($crate::Err::Position(e,p)); + v + }, + $crate::Err::Node(e, mut next) => { + next.push($crate::Err::Code(e)); + next + }, + $crate::Err::NodePosition(e, p, mut next) => { + next.push($crate::Err::Position(e,p)); + next + }, }; - kind.description() - } -} - -#[cfg(not(feature = "core"))] -impl fmt::Display for Err { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Err::Code(ref e) | Err::Node(ref e, _) => { - write!(f, "{:?}", e) + $crate::Err::Node($code, next_errors) + }; +); + +#[cfg(not(feature = "verbose-errors"))] +/// creates a parse error from a `nom::ErrorKind` +/// and the next error in the parsing tree. +/// if "verbose-errors" is not activated, +/// it default to only the error code +#[allow(unused_variables)] +#[macro_export] +macro_rules! error_node( + ($code:expr, $next:expr) => ($code); +); + +#[cfg(feature = "verbose-errors")] +/// creates a parse error from a `nom::ErrorKind` +/// and the position in the input +/// if "verbose-errors" is not activated, +/// it default to only the error code +#[macro_export] +macro_rules! error_position( + ($code:expr, $input:expr) => ($crate::Err::Position($code, $input)); +); + +#[cfg(not(feature = "verbose-errors"))] +/// creates a parse error from a `nom::ErrorKind` +/// and the position in the input +/// if "verbose-errors" is not activated, +/// it default to only the error code +#[allow(unused_variables)] +#[macro_export] +macro_rules! error_position( + ($code:expr, $input:expr) => ($code); +); + +#[cfg(feature = "verbose-errors")] +/// creates a parse error from a `nom::ErrorKind`, +/// the position in the input and the next error in +/// the parsing tree. +/// if "verbose-errors" is not activated, +/// it default to only the error code +#[macro_export] +macro_rules! error_node_position( + ($code:expr, $input:expr, $next:expr) => { + { + let next_errors = match $next { + $crate::Err::Code(e) => { + let mut v = ::std::vec::Vec::new(); + v.push($crate::Err::Code(e)); + v }, - Err::Position(ref e, ref p) | Err::NodePosition(ref e, ref p, _) => { - write!(f, "{:?}:{:?}", p, e) + $crate::Err::Position(e, p) => { + let mut v = ::std::vec::Vec::new(); + v.push($crate::Err::Position(e,p)); + v + }, + $crate::Err::Node(e, mut next) => { + next.push($crate::Err::Code(e)); + next + }, + $crate::Err::NodePosition(e, p, mut next) => { + next.push($crate::Err::Position(e,p)); + next } + }; + $crate::Err::NodePosition($code, $input, next_errors) } } -} +); + +#[cfg(not(feature = "verbose-errors"))] +/// creates a parse error from a `nom::ErrorKind`, +/// the position in the input and the next error in +/// the parsing tree. +/// if "verbose-errors" is not activated, +/// it default to only the error code +#[allow(unused_variables)] +#[macro_export] +macro_rules! error_node_position( + ($code:expr, $input: expr, $next:expr) => ($code); +); #[cfg(test)] mod tests { @@ -252,9 +378,16 @@ mod tests { const REST: [u8; 0] = []; const DONE: IResult<&'static [u8], u32> = IResult::Done(&REST, 5); - const ERROR: IResult<&'static [u8], u32> = IResult::Error(Err::Code(ErrorKind::Tag)); + const ERROR: IResult<&'static [u8], u32> = IResult::Error(error_code!(ErrorKind::Tag)); const INCOMPLETE: IResult<&'static [u8], u32> = IResult::Incomplete(Needed::Unknown); + #[test] + fn iresult_or() { + assert_eq!(DONE.or(ERROR), DONE); + assert_eq!(ERROR.or(DONE), DONE); + assert_eq!(INCOMPLETE.or(ERROR), ERROR); + } + #[test] fn needed_map() { let unknown = Needed::Unknown; @@ -267,7 +400,7 @@ mod tests { #[test] fn iresult_map() { assert_eq!(DONE.map(|x| x * 2), IResult::Done(&b""[..], 10)); - assert_eq!(ERROR.map(|x| x * 2), IResult::Error(Err::Code(ErrorKind::Tag))); + assert_eq!(ERROR.map(|x| x * 2), IResult::Error(error_code!(ErrorKind::Tag))); assert_eq!(INCOMPLETE.map(|x| x * 2), IResult::Incomplete(Needed::Unknown)); } @@ -277,17 +410,18 @@ mod tests { let inc_size: IResult<&[u8], u32> = IResult::Incomplete(Needed::Size(5)); assert_eq!(DONE.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Done(&b""[..], 5)); - assert_eq!(ERROR.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Error(Err::Code(ErrorKind::Tag))); + assert_eq!(ERROR.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Error(error_code!(ErrorKind::Tag))); assert_eq!(inc_unknown.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Incomplete(Needed::Unknown)); assert_eq!(inc_size.map_inc(|n| if let Needed::Size(i) = n {Needed::Size(i+1)} else {n}), IResult::Incomplete(Needed::Size(6))); } #[test] + #[cfg(feature = "std")] fn iresult_map_err() { #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct Error(u32); - let error_kind = Err::Code(ErrorKind::Custom(Error(5))); + let error_kind = error_code!(ErrorKind::Custom(Error(5))); assert_eq!(DONE.map_err(|_| error_kind.clone()), IResult::Done(&b""[..], 5)); assert_eq!(ERROR.map_err(|x| {println!("err: {:?}", x); error_kind.clone()}), IResult::Error(error_kind.clone())); @@ -311,6 +445,21 @@ mod tests { INCOMPLETE.unwrap(); } + #[test] + fn iresult_unwrap_or_on_done() { + assert_eq!(DONE.unwrap_or((&b""[..], 2)), (&b""[..], 5)); + } + + #[test] + fn iresult_unwrap_or_on_err() { + assert_eq!(ERROR.unwrap_or((&b""[..], 2)), (&b""[..], 2)); + } + + #[test] + fn iresult_unwrap_or_on_inc() { + assert_eq!(INCOMPLETE.unwrap_or((&b""[..], 2)), (&b""[..], 2)); + } + #[test] #[should_panic] fn iresult_unwrap_err_on_done() { @@ -319,7 +468,7 @@ mod tests { #[test] fn iresult_unwrap_err_on_err() { - assert_eq!(ERROR.unwrap_err(), Err::Code(ErrorKind::Tag)); + assert_eq!(ERROR.unwrap_err(), error_code!(ErrorKind::Tag)); } #[test] @@ -344,4 +493,23 @@ mod tests { fn iresult_unwrap_inc_on_inc() { assert_eq!(INCOMPLETE.unwrap_inc(), Needed::Unknown); } + + #[test] + fn iresult_to_result() { + assert_eq!(DONE.to_result(), Ok(5)); + assert_eq!(ERROR.to_result(), Err(error_code!(ErrorKind::Tag))); + } + + #[test] + #[should_panic] + fn iresult_to_result_on_incomplete() { + INCOMPLETE.to_result().unwrap(); + } + + #[test] + fn iresult_to_full_result() { + assert_eq!(DONE.to_full_result(), Ok(5)); + assert_eq!(INCOMPLETE.to_full_result(), Err(IError::Incomplete(Needed::Unknown))); + assert_eq!(ERROR.to_full_result(), Err(IError::Error(error_code!(ErrorKind::Tag)))); + } } diff --git third_party/rust/nom/src/lib.rs third_party/rust/nom/src/lib.rs index 11b475d59517..150ef85fce57 100644 --- third_party/rust/nom/src/lib.rs +++ third_party/rust/nom/src/lib.rs @@ -5,6 +5,318 @@ //! //! The code is available on [Github](https://github.com/Geal/nom) //! +//! There are a few [guides](http://rust.unhandledexpression.com/nom/home.html) with more details +//! about [the design of nom](http://rust.unhandledexpression.com/nom/how_nom_macros_work.html), +//! [how to write parsers](http://rust.unhandledexpression.com/nom/making_a_new_parser_from_scratch.html), +//! or the [error management system](http://rust.unhandledexpression.com/nom/error_management.html). +//! +//! If you are upgrading to nom 2.0, please read the +//! [migration document](http://rust.unhandledexpression.com/nom/upgrading_to_nom_2.html). +//! +//! See also the [FAQ](http://rust.unhandledexpression.com/nom/FAQ.html). +//! +//! # What are parser combinators? +//! +//! Parser combinators are a way to build parsers out of small functions. instead of +//! writing a huge grammar file then generaing code, like you would do with lex and yacc, +//! you write small functions, to parse small things like a character, or a number, +//! and then you assemble them in larger and larger functions, that can parse larger +//! parts of your formats. +//! +//! You end up with a list of small functions that you can reuse everywhere you need. Each +//! of them can be unit tested anf fuzzed separately. +//! +//! # nom parser design +//! +//! All nom parsers follow the same convention. They are all functions with the following signature: +//! +//! ```ignore +//! fn parser(input: I) -> IResult { ... } +//! ``` +//! +//! Here is the definition of that `IResult` type: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! # use nom::{Err,Needed}; +//! # fn main() {} +//! pub enum IResult { +//! Done(I,O), +//! Error(Err), // indicates the parser encountered an error. E is a custom error type you can redefine +//! /// Incomplete contains a Needed, an enum than can represent a known quantity of input data, or unknown +//! Incomplete(Needed) // if the parser did not have enough data to decide +//! } +//! ``` +//! +//! What it means: +//! +//! * `Done(i,o)` means the parser was successful. `i` is the remaining part of the input, `o` is the correctly parsed value +//! The remaining part can then be used as input for other parsers called in a sequence +//! * `Error(e)` indicates the parser encountered an error. The `Err` type is an enum of possible parser errors, +//! that can also contain a custom error that you'd specify, by redefining the `E` error type +//! * `Incomplete(i)` means the parser did not have enough information to decide, and tells you, if possible, +//! how much data it needs +//! +//! That way, you could write your own parser that recognizes the letter 'a' like this: +//! +//! ``` +//! #[macro_use] extern crate nom; +//! use nom::{IResult,Needed,Err,ErrorKind}; +//! # fn main() {} +//! +//! fn a(input: &[u8]) -> IResult<&[u8], char> { +//! // if there is not enough data, we return Ìncomplete +//! if input.len() == 0 { +//! IResult::Incomplete(Needed::Size(1)) +//! } else { +//! if input[0] == 'a' as u8 { +//! // the first part of the returned value is the remaining slice +//! IResult::Done(&input[1..], 'a') +//! } else { +//! IResult::Error(error_code!(ErrorKind::Custom(42))) +//! } +//! } +//! } +//! ``` +//! +//! Writing all the parsers manually, like this, is dangerous, despite Rust's safety features. There +//! are still a lot of mistakes one can make. That's why nom provides a list of macros to help in +//! developing parsers. As an example, here is a parser that would recognize the phrase +//! "Hello " and return the name of the person we hail: +//! +//! ``` +//! #[macro_use] extern crate nom; +//! use nom::alpha; +//! +//! named!(hello, preceded!(tag!("Hello "), alpha)); +//! # use nom::IResult; +//! # fn main() { +//! # assert_eq!(hello(b"Hello nom."), IResult::Done(&b"."[..], &b"nom"[..])); +//! # } +//! ``` +//! +//! Let's deconstruct it: +//! +//! * `named!` generates a function with the correct type. Without `named` here, we could write the parser +//! as follows: +//! +//! ``` +//! #[macro_use] extern crate nom; +//! use nom::{alpha,IResult}; +//! +//! fn hello(input: &[u8]) -> IResult<&[u8], &[u8]> { +//! preceded!(input, +//! tag!("Hello "), alpha) +//! } +//! # fn main() { +//! # assert_eq!(hello(b"Hello nom."), IResult::Done(&b"."[..], &b"nom"[..])); +//! # } +//! ``` +//! +//! By default, `named` makes a function that takes `&[u8]` as input type, and returns `&[u8]` as output type. +//! You can override it like this: +//! +//! * `named!(hello<&str>, ...):` would take `&[u8]` as input type, and return `&str` as output type. +//! * `named!(hello<&str, &str>, ...):` would take `&str` as input type, and return `&str` as output type. +//! +//! *Note* : when we don't use `named!`, we must pass the input as first argument of the top +//! level combinator (see the line `preceded!(input,` in the preceding code example). This is a macro trick +//! in nom to pass input from one combinator to the next by rewriting the call. +//! +//! Next part of the parser: `preceded!(tag!("Hello "), alpha))`. Here, `tag!` is a combinator that recognizes +//! a specific serie of bytes or characters. `alpha` is a function that recognizes alphabetical characters. +//! The `preceded!` combinator assembles them in a more complex parser: if both parsers are successful, +//! it returns the result of the second one (`alpha` is preceded by `tag!`). +//! +//! *Note* : combinators can assemble other combinators (macros), or parser functions, as long as they follow +//! the same interface. Here, `alpha` is a parser function already implemented in nom. +//! +//! # List of parsers and combinators +//! +//! ## Basic elements +//! +//! Those are used to recognize the lowest level elements of your grammar, like, "here is a dot", or +//! "here is an big endian integer". +//! +//! * **char!**: matches one character: `char!('a')` will make a parser that recognizes the letter 'a' (works with non ASCII chars too) +//! * **eof!**: `eof!()` returns its input if it is at the end of input data +//! * **is_a!, is_a_s!**: matches a sequence of any of the characters passed as arguments. `is_a!("ab1")` could recognize `ababa` or `1bb`. `is_a_s!` is a legacy combinator, it does exactly the same thing as `is_a` +//! * **is_not!, is_not_s!**: matches a sequence of none of the characters passed as arguments +//! * **one_of!**: matches one of the provided characters. `one_of!("abc")` could recognize 'a', 'b', or 'c'. It also works with non ASCII characters +//! * **none_of!**: matches anything but the provided characters +//! * **tag!, tag_s!**: recognizes a specific suite of characters or bytes. `tag!("hello")` matches "hello" +//! * **tag_no_case!**: recognizes a suite of ASCII characters, case insensitive. `tag_no_case!("hello")` could match "hello", "Hello" or even "HeLlO" +//! * **tag_no_case_s!** works like `tag_no_case` but on UTF-8 characters too (uses `&str` as input). Note that case insensitive comparison is not well defined for unicode, and that you might have bad surprises. Also, this combinator allocates a new string for the comparison. Ponder for a bit before using this combinator +//! * **take!, take_s!**: takes a specific number of bytes or characters. `take!(5)` would return "hello" from the string "hello world" +//! * **take_str!**: same as `take!` but returning a `&str` +//! * **take_till!, take_till_s!**: returns the longest list of bytes until the provided function succeeds. `take_till!(is_alphabetic)` with input "123abc" would return "123" +//! * **take_till1!, take_till1_s!**: same as `take_till!`, but the result must not be empty: `take_till1!(is_alphabetic)` would fail on "abc" +//! * **take_until!, take_until_s!**: returns the longest list of bytes until the provided tag is found. `take_until!("world")` with input "Hello world!" would return "Hello " and leave "world!" as remaining input +//! * **take_until1!**: same as `take_until!`, but cannot return an empty result +//! * **take_until_and_consume!, take_until_and_consume_s!**: same as `take_until!` but consumes the tag. `take_until_and_consume!("world")` with input "Hello world!" would return "Hello " and leave "!" as remaining input +//! * **take_until_and_consume1!**: same as `take_until_and_consume!`, but cannot return an empty result +//! * **take_until_either!**: returns the longest list of bytes until any of the provided characters are found +//! * **take_until_either_and_consume!**: same as `take_until_either!`, but consumes the terminating character +//! * **take_while!, take_while_s!**: returns the longest list of bytes for which the function is true. `take_while!(is_alphabetic)` with input "abc123" would return "abc" +//! * **take_while1!, take_while1_s!**: same as `take_while!`, but cannot return an empty result +//! * **value!**: you can use `value!` to always return the same result value without consuming input, like this: `value!(42)`. Or you can replace the result of a child parser with a predefined value, like this: `value!(42, tag!("abcd"))` which would replace, if successful, the return value from "abcd", to 42 +//! +//! Parsing integers from binary formats can be done in two ways: with parser functions, or combinators with configurable endianness: +//! +//! * configurable endianness: **i16!, i32!, i64!, u16!, u32!, u64!** are combinators that take as argument a `nom::Endianness`, +//! like this: `i16!(endianness)`. If the parameter is nom::Endianness::Big, parse a big endian i16 integer, otherwise a little endian i16 integer +//! * fixed endianness: the functions are prefixed by "be_" for big endian numbers, and by "le_" for little endian numbers, and the suffix is the type they parse to. As an example, "be_u32" parses a big endian unsigned integer stored in 32 bits. +//! * **be_f32, be_f64, le_f32, le_f64**: recognize floating point numbers +//! * **be_i8, be_i16, be_i32, be_i24, be_i32, be_i64**: big endian signed integers +//! * **be_u8, be_u16, be_u32, be_u24, be_u32, be_u64**: big endian unsigned integers +//! * **le_i8, le_i16, le_i32, le_i24, le_i32, le_i64**: little endian signed integers +//! * **le_u8, le_u16, le_u32, le_u24, le_u32, le_u64**: little endian unsigned integers +//! +//! ## Modifiers +//! +//! * **complete!**: replaces a Incomplete returned by the child parser with an Error +//! * **cond!**: conditional combinator +//! * **cond_reduce!**: Conditional combinator with error +//! * **cond_with_error!**: Conditional combinator +//! * **expr_opt!**: evaluates an expression that returns a Option and returns a IResult::Done(I,T) if Some +//! * **expr_res!**: evaluates an expression that returns a Result and returns a IResult::Done(I,T) if Ok +//! * **flat_map!**: +//! * **map!**: maps a function on the result of a parser +//! * **map_opt!**: maps a function returning an Option on the output of a parser +//! * **map_res!**: maps a function returning a Result on the output of a parser +//! * **not!**: returns a result only if the embedded parser returns Error or Incomplete does not consume the input +//! * **opt!**: make the underlying parser optional +//! * **opt_res!**: make the underlying parser optional +//! * **parse_to!**: uses the parse method from std::str::FromStr to convert the current input to the specified type +//! * **peek!**: returns a result without consuming the input +//! * **recognize!**: if the child parser was successful, return the consumed input as produced value +//! * **return_error!**: prevents backtracking if the child parser fails +//! * **tap!**: allows access to the parser's result without affecting it +//! * **verify!**: returns the result of the child parser if it satisfies a verification function +//! +//! ## Error management and debugging +//! +//! * **add_return_error!**: Add an error if the child parser fails +//! * **dbg!**: Prints a message if the parser fails +//! * **dbg_dmp!**: Prints a message and the input if the parser fails +//! * **error_code!**: creates a parse error from a nom::ErrorKind +//! * **error_node!**: creates a parse error from a nom::ErrorKind and the next error in the parsing tree. if "verbose-errors" is not activated, it default to only the error code +//! * **error_node_position!**: creates a parse error from a nom::ErrorKind, the position in the input and the next error in the parsing tree. if "verbose-errors" is not activated, it default to only the error code +//! * **error_position!**: creates a parse error from a nom::ErrorKind and the position in the input if "verbose-errors" is not activated, it default to only the error code +//! * **fix_error!**: translate parser result from IResult to IResult with a custom type +//! +//! ## Choice combinators +//! +//! * **alt!**: try a list of parsers and return the result of the first successful one +//! * **alt_complete!**: is equivalent to the alt! combinator, except that it will not return Incomplete when one of the constituting parsers returns Incomplete. Instead, it will try the next alternative in the chain. +//! * **switch!**: choose the next parser depending on the result of the first one, if successful, and returns the result of the second parser +//! +//! # Sequence combinators +//! +//! * **delimited!**: delimited(opening, X, closing) returns X +//! * **do_parse!**: do_parse applies sub parsers in a sequence. it can store intermediary results and make them available for later parsers +//! * **pair!**: pair(X,Y), returns (x,y) +//! * **permutation!**: applies its sub parsers in a sequence, but independent from their order this parser will only succeed if all of its sub parsers succeed +//! * **preceded!**: preceded(opening, X) returns X +//! * **separated_pair!**: separated_pair(X,sep,Y) returns (x,y) +//! * **terminated!**: terminated(X, closing) returns X +//! * **tuple!**: chains parsers and assemble the sub results in a tuple. +//! +//! ## Applying a parser multiple times +//! +//! * **count!**: Applies the child parser a specified number of times +//! * **count_fixed!**: Applies the child parser a fixed number of times and returns a fixed size array The type must be specified and it must be Copy +//! * **fold_many0!**: Applies the parser 0 or more times and folds the list of return values +//! * **fold_many1!**: Applies the parser 1 or more times and folds the list of return values +//! * **fold_many_m_n!**: Applies the parser between m and n times (n included) and folds the list of return value +//! * **length_count!**: gets a number from the first parser, then applies the second parser that many times +//! * **many0!**: Applies the parser 0 or more times and returns the list of results in a Vec +//! * **many1!**: Applies the parser 1 or more times and returns the list of results in a Vec +//! * **many_m_n!**: Applies the parser between m and n times (n included) and returns the list of results in a Vec +//! * **many_till!**: Applies the first parser until the second applies. Returns a tuple containing the list of results from the first in a Vec and the result of the second. +//! * **separated_list!**: separated_list(sep, X) returns Vec will return Incomplete if there may be more elements +//! * **separated_list_complete!**: This is equivalent to the separated_list! combinator, except that it will return Error when either the separator or element subparser returns Incomplete. +//! * **separated_nonempty_list!**: separated_nonempty_list(sep, X) returns Vec will return Incomplete if there may be more elements +//! * **separated_nonempty_list_complete!**: This is equivalent to the separated_nonempty_list! combinator, except that it will return Error when either the separator or element subparser returns Incomplete. +//! +//! ## Text parsing +//! +//! * **escaped!**: matches a byte string with escaped characters. +//! * **escaped_transform!**: matches a byte string with escaped characters, and returns a new string with the escaped characters replaced +//! +//! ## Binary format parsing +//! +//! * **length_data!**: gets a number from the first parser, than takes a subslice of the input of that size, and returns that subslice +//! * **length_bytes!**: alias for `length_data` +//! * **length_value!**: gets a number from the first parser, takes a subslice of the input of that size, then applies the second parser on that subslice. If the second parser returns Incomplete, length_value will return an error +//! +//! ## Bit stream parsing +//! +//! * **bits!**: transforms the current input type (byte slice `&[u8]`) to a bit stream on which bit specific parsers and more general combinators can be applied +//! * **bytes!**: transforms its bits stream input back into a byte slice for the underlying parsers. +//! * **tag_bits!**: matches an integer pattern to a bitstream. The number of bits of the input to compare must be specified +//! * **take_bits!**: generates a parser consuming the specified number of bits +//! +//! ## Whitespace delimited formats parsing +//! +//! * **eat_separator!**: helper macros to build a separator parser +//! * **sep!**: sep is the parser rewriting macro for whitespace separated formats +//! * **wrap_sep!**: +//! * **ws!**: +//! +//! ## Remaining combinators +//! +//! * **apply!**: emulate function currying: apply!(my_function, arg1, arg2, ...) becomes my_function(input, arg1, arg2, ...) +//! * **apply_m!**: emulate function currying for method calls on structs apply_m!(self.my_function, arg1, arg2, ...) becomes self.my_function(input, arg1, arg2, ...) +//! * **call!**: Used to wrap common expressions and function as macros +//! * **call_m!**: Used to called methods then move self back into self +//! * **closure!**: Wraps a parser in a closure +//! * **method!**: Makes a method from a parser combination +//! * **named!**: Makes a function from a parser combination +//! * **named_args!**: Makes a function from a parser combination with arguments. +//! * **named_attr!**: Makes a function from a parser combination, with attributes +//! * **try_parse!**: A bit like std::try!, this macro will return the remaining input and parsed value if the child parser returned Done, and will do an early return for Error and Incomplete this can provide more flexibility than do_parse! if needed +//! +//! ## Character test functions +//! +//! use those functions with a combinator like `take_while!`: +//! +//! * **is_alphabetic**: Tests if byte is ASCII alphabetic: A-Z, a-z +//! * **is_alphanumeric**: Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9 +//! * **is_digit**: Tests if byte is ASCII digit: 0-9 +//! * **is_hex_digit**: Tests if byte is ASCII hex digit: 0-9, A-F, a-f +//! * **is_oct_digit**: Tests if byte is ASCII octal digit: 0-7 +//! * **is_space**: Tests if byte is ASCII space or tab +//! +//! ## Remaining functions (sort those out in the other categories) +//! +//! * **alpha**: Recognizes one or more lowercase and uppercase alphabetic characters: a-zA-Z +//! * **alphanumeric**: Recognizes one or more numerical and alphabetic characters: 0-9a-zA-Z +//! * **anychar**: +//! * **begin**: +//! * **crlf**: +//! * **digit**: Recognizes one or more numerical characters: 0-9 +//! * **double**: Recognizes floating point number in a byte string and returns a f64 +//! * **double_s**: Recognizes floating point number in a string and returns a f64 +//! * **eol**: +//! * **float**: Recognizes floating point number in a byte string and returns a f32 +//! * **float_s**: Recognizes floating point number in a string and returns a f32 +//! * **hex_digit**: Recognizes one or more hexadecimal numerical characters: 0-9, A-F, a-f +//! * **hex_u32**: Recognizes a hex-encoded integer +//! * **line_ending**: Recognizes an end of line (both '\n' and "\r\n") +//! * **multispace**: Recognizes one or more spaces, tabs, carriage returns and line feeds +//! * **newline**: Matches a newline character '\n' +//! * **non_empty**: Recognizes non empty buffers +//! * **not_line_ending**: +//! * **oct_digit**: Recognizes one or more octal characters: 0-7 +//! * **rest**: Return the remaining input. +//! * **rest_s**: Return the remaining input, for strings. +//! * **shift**: +//! * **sized_buffer**: +//! * **space**: Recognizes one or more spaces and tabs +//! * **tab**: Matches a tab character '\t' +//! * **tag_cl**: +//! //! # Example //! //! ``` @@ -12,65 +324,57 @@ //! extern crate nom; //! //! use nom::{IResult,digit}; -//! use nom::IResult::*; //! //! // Parser definition //! //! use std::str; //! use std::str::FromStr; //! -//! named!(parens, delimited!( -//! char!('('), -//! expr, -//! char!(')') -//! ) -//! ); +//! // We parse any expr surrounded by parens, ignoring all whitespaces around those +//! named!(parens, ws!(delimited!( tag!("("), expr, tag!(")") )) ); //! -//! named!(i64_digit, -//! map_res!( +//! // We transform an integer string into a i64, ignoring surrounding whitespaces +//! // We look for a digit suite, and try to convert it. +//! // If either str::from_utf8 or FromStr::from_str fail, +//! // we fallback to the parens parser defined above +//! named!(factor, alt!( //! map_res!( -//! digit, -//! str::from_utf8 -//! ), -//! FromStr::from_str -//! ) -//! ); -//! -//! // We transform an integer string into a i64 -//! // we look for a digit suite, and try to convert it. -//! // if either str::from_utf8 or FromStr::from_str fail, -//! // the parser will fail -//! named!(factor, -//! alt!( -//! i64_digit +//! map_res!( +//! ws!(digit), +//! str::from_utf8 +//! ), +//! FromStr::from_str +//! ) //! | parens //! ) //! ); //! -//! // we define acc as mutable to update its value whenever a new term is found -//! named!(term , -//! chain!( -//! mut acc: factor ~ -//! many0!( -//! alt!( -//! tap!(mul: preceded!(tag!("*"), factor) => acc = acc * mul) | -//! tap!(div: preceded!(tag!("/"), factor) => acc = acc / div) -//! ) -//! ), -//! || { return acc } +//! // We read an initial factor and for each time we find +//! // a * or / operator followed by another factor, we do +//! // the math by folding everything +//! named!(term , do_parse!( +//! init: factor >> +//! res: fold_many0!( +//! pair!(alt!(tag!("*") | tag!("/")), factor), +//! init, +//! |acc, (op, val): (&[u8], i64)| { +//! if (op[0] as char) == '*' { acc * val } else { acc / val } +//! } +//! ) >> +//! (res) //! ) //! ); //! -//! named!(expr , -//! chain!( -//! mut acc: term ~ -//! many0!( -//! alt!( -//! tap!(add: preceded!(tag!("+"), term) => acc = acc + add) | -//! tap!(sub: preceded!(tag!("-"), term) => acc = acc - sub) -//! ) -//! ), -//! || { return acc } +//! named!(expr , do_parse!( +//! init: term >> +//! res: fold_many0!( +//! pair!(alt!(tag!("+") | tag!("-")), term), +//! init, +//! |acc, (op, val): (&[u8], i64)| { +//! if (op[0] as char) == '+' { acc + val } else { acc - val } +//! } +//! ) >> +//! (res) //! ) //! ); //! @@ -84,25 +388,38 @@ //! assert_eq!(expr(b"2*2/(5-1)+3"), IResult::Done(&b""[..], 4)); //! } //! ``` -#![cfg_attr(feature = "core", feature(no_std))] -#![cfg_attr(feature = "core", feature(collections))] -#![cfg_attr(feature = "core", no_std)] +#![cfg_attr(not(feature = "std"), feature(no_std))] +#![cfg_attr(not(feature = "std"), feature(collections))] +#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "nightly", feature(test))] #![cfg_attr(feature = "nightly", feature(const_fn))] +#![cfg_attr(feature = "nightly", feature(plugin))] +#![cfg_attr(feature = "nightly", plugin(compiler_error))] +//#![warn(missing_docs)] -#[cfg(feature = "core")] +#[cfg(not(feature = "std"))] extern crate collections; #[cfg(feature = "regexp")] extern crate regex; #[cfg(feature = "regexp_macros")] #[macro_use] extern crate lazy_static; +extern crate memchr; #[cfg(feature = "nightly")] extern crate test; -#[cfg(feature = "core")] +#[cfg(not(feature = "nightly"))] +#[allow(unused_macros)] +#[macro_export] +macro_rules! compiler_error { + ($e:expr) => { + INVALID_NOM_SYNTAX_PLEASE_SEE_FAQ //https://github.com/Geal/nom/blob/master/doc/FAQ.md#using-nightly-to-get-better-error-messages + } +} + +#[cfg(not(feature = "std"))] mod std { #[macro_use] - pub use core::{fmt, iter, option, ops, slice, mem}; + pub use core::{fmt, cmp, iter, option, result, ops, slice, str, mem, convert}; pub use collections::{boxed, vec, string}; pub mod prelude { pub use core::prelude as v1; @@ -110,8 +427,19 @@ mod std { } pub use self::util::*; +pub use self::traits::*; + +#[cfg(feature = "verbose-errors")] +pub use self::verbose_errors::*; + +#[cfg(not(feature = "verbose-errors"))] +pub use self::simple_errors::*; + pub use self::internal::*; pub use self::macros::*; +pub use self::branch::*; +pub use self::sequence::*; +pub use self::multi::*; pub use self::methods::*; pub use self::bytes::*; pub use self::bits::*; @@ -119,33 +447,45 @@ pub use self::bits::*; pub use self::nom::*; pub use self::character::*; +pub use self::whitespace::*; + #[cfg(feature = "regexp")] pub use self::regexp::*; -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] #[cfg(feature = "stream")] pub use self::stream::*; -#[cfg(not(feature = "core"))] pub use self::str::*; #[macro_use] mod util; -mod internal; +mod traits; + +#[cfg(feature = "verbose-errors")] #[macro_use] pub mod verbose_errors; + +#[cfg(not(feature = "verbose-errors"))] #[macro_use] pub mod simple_errors; + +#[macro_use] mod internal; #[macro_use] mod macros; -#[macro_use] mod methods; +#[macro_use] mod branch; +#[macro_use] mod sequence; +#[macro_use] mod multi; +#[macro_use] pub mod methods; #[macro_use] mod bytes; -#[macro_use] mod bits; +#[macro_use] pub mod bits; #[macro_use] mod nom; #[macro_use] mod character; +#[macro_use] +pub mod whitespace; + #[cfg(feature = "regexp")] #[macro_use] mod regexp; #[macro_use] -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] #[cfg(feature = "stream")] mod stream; -#[cfg(not(feature = "core"))] mod str; diff --git third_party/rust/nom/src/macros.rs third_party/rust/nom/src/macros.rs index 3e346a94f522..2bbe4bca412d 100644 --- third_party/rust/nom/src/macros.rs +++ third_party/rust/nom/src/macros.rs @@ -61,7 +61,8 @@ //! take_while!($input, call!($f)); //! ); //! ); -//! +//! ``` +#[allow(unused_variables)] /// Wraps a parser in a closure #[macro_export] @@ -93,52 +94,168 @@ macro_rules! closure ( /// ``` #[macro_export] macro_rules! named ( + (#$($args:tt)*) => ( + named_attr!(#$($args)*); + ); ($name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( i: $i ) -> $crate::IResult<$i,$o,u32> { $submac!(i, $($args)*) } ); ($name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { $submac!(i, $($args)*) } ); ($name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> { $submac!(i, $($args)*) } ); ($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name<'a>( i: &'a[u8] ) -> $crate::IResult<&'a [u8], $o, u32> { $submac!(i, $($args)*) } ); ($name:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( i: &[u8] ) -> $crate::IResult<&[u8], &[u8], u32> { $submac!(i, $($args)*) } ); (pub $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> { $submac!(i, $($args)*) } ); (pub $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { $submac!(i, $($args)*) } ); (pub $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> { $submac!(i, $($args)*) } ); (pub $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> { $submac!(i, $($args)*) } ); (pub $name:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] + pub fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> { + $submac!(i, $($args)*) + } + ); +); + +/// Makes a function from a parser combination with arguments. +#[macro_export] +macro_rules! named_args { + (pub $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + pub fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> { + $submac!(input, $($args)*) + } + }; + (pub $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + pub fn $func_name<'this_is_probably_unique_i_hope_please, 'a>(input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) -> $crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type> { + $submac!(input, $($args)*) + } + }; + ($func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> { + $submac!(input, $($args)*) + } + }; + ($func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + fn $func_name<'this_is_probably_unique_i_hope_please, 'a>(input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) -> $crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type> { + $submac!(input, $($args)*) + } + }; +} + +/// Makes a function from a parser combination, with attributes +/// +/// The usage of this macro is almost identical to `named!`, except that +/// you also pass attributes to be attached to the generated function. +/// This is ideal for adding documentation to your parser. +/// +/// ```ignore +/// // Create my_function as if you wrote it with the doc comment /// My Func +/// named_attr!(#[doc = "My Func"], my_function( &[u8] ) -> &[u8], tag!("abcd")); +/// // Also works for pub functions, and multiple lines +/// named!(#[doc = "My Func\nRecognise abcd"], pub my_function, tag!("abcd")); +/// // Multiple attributes can be passed if required +/// named!(#[doc = "My Func"] #[inline(always)], pub my_function, tag!("abcd")); +/// ``` +#[macro_export] +macro_rules! named_attr ( + ($(#[$attr:meta])*, $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + fn $name( i: $i ) -> $crate::IResult<$i,$o,u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + fn $name<'a>( i: &'a[u8] ) -> $crate::IResult<&'a [u8], $o, u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $name:ident, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + fn $name( i: &[u8] ) -> $crate::IResult<&[u8], &[u8], u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, pub $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + pub fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, pub $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + pub fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, pub $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + pub fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, pub $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + pub fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, pub $name:ident, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* pub fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> { $submac!(i, $($args)*) } @@ -154,7 +271,7 @@ macro_rules! named ( /// fn take_wrapper(input: &[u8], i: u8) -> IResult<&[u8],&[u8]> { take!(input, i * 10) } /// /// // will make a parser taking 20 bytes -/// named!(parser, apply!(take_wrapper, 2)); +/// named!(parser, call!(take_wrapper, 2)); /// # } /// ``` #[macro_export] @@ -185,7 +302,7 @@ macro_rules! apply ( /// This parser will do an early return instead of sending /// its result to the parent parser. /// -/// If another `error!` combinator is present in the parent +/// If another `return_error!` combinator is present in the parent /// chain, the error will be wrapped and another early /// return will be made. /// @@ -200,16 +317,17 @@ macro_rules! apply ( /// # #[macro_use] extern crate nom; /// # use std::collections; /// # use nom::IResult::Error; +/// # #[cfg(feature = "verbose-errors")] /// # use nom::Err::{Position,NodePosition}; /// # use nom::ErrorKind; /// # fn main() { /// named!(err_test, alt!( /// tag!("abcd") | -/// preceded!(tag!("efgh"), error!(ErrorKind::Custom(42), -/// chain!( -/// tag!("ijkl") ~ -/// res: error!(ErrorKind::Custom(128), tag!("mnop")) , -/// || { res } +/// preceded!(tag!("efgh"), return_error!(ErrorKind::Custom(42), +/// do_parse!( +/// tag!("ijkl") >> +/// res: return_error!(ErrorKind::Custom(128), tag!("mnop")) >> +/// (res) /// ) /// ) /// ) @@ -223,131 +341,73 @@ macro_rules! apply ( /// let res_a = err_test(a); /// let res_b = err_test(b); /// let res_c = err_test(c); -/// assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah))))); -/// assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..], -/// Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah)))))) +/// assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah)))); +/// assert_eq!(res_b, Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..], +/// error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah)))) /// ); /// # } /// ``` /// #[macro_export] -macro_rules! error ( +macro_rules! return_error ( ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( { + let i_ = $i.clone(); let cl = || { - $submac!($i, $($args)*) + $submac!(i_, $($args)*) }; match cl() { $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), $crate::IResult::Error(e) => { - return $crate::IResult::Error($crate::Err::NodePosition($code, $i, Box::new(e))) + return $crate::IResult::Error(error_node_position!($code, $i, e)) } } } ); ($i:expr, $code:expr, $f:expr) => ( - error!($i, $code, call!($f)); + return_error!($i, $code, call!($f)); ); ); /// Add an error if the child parser fails /// /// While error! does an early return and avoids backtracking, -/// add_error! backtracks normally. It just provides more context +/// add_return_error! backtracks normally. It just provides more context /// for an error /// /// ``` /// # #[macro_use] extern crate nom; /// # use std::collections; /// # use nom::IResult::Error; +/// # #[cfg(feature = "verbose-errors")] /// # use nom::Err::{Position,NodePosition}; /// # use nom::ErrorKind; /// # fn main() { -/// named!(err_test, add_error!(ErrorKind::Custom(42), tag!("abcd"))); +/// named!(err_test, add_return_error!(ErrorKind::Custom(42), tag!("abcd"))); /// /// let a = &b"efghblah"[..]; /// let res_a = err_test(a); -/// assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), a, Box::new(Position(ErrorKind::Tag, a))))); +/// assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), a, error_position!(ErrorKind::Tag, a)))); /// # } /// ``` /// #[macro_export] -macro_rules! add_error ( +macro_rules! add_return_error ( ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( { match $submac!($i, $($args)*) { $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), $crate::IResult::Error(e) => { - $crate::IResult::Error($crate::Err::NodePosition($code, $i, Box::new(e))) + $crate::IResult::Error(error_node_position!($code, $i, e)) } } } ); ($i:expr, $code:expr, $f:expr) => ( - add_error!($i, $code, call!($f)); - ); -); - - -/// translate parser result from IResult to IResult with a custom type -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use std::collections; -/// # use nom::IResult::Error; -/// # use nom::Err::{Position,NodePosition}; -/// # use nom::ErrorKind; -/// # fn main() { -/// // will add a Custom(42) error to the error chain -/// named!(err_test, add_error!(ErrorKind::Custom(42), tag!("abcd"))); -/// // Convert to IREsult<&[u8], &[u8], &str> -/// named!(parser<&[u8], &[u8], &str>, add_error!(ErrorKind::Custom("custom error message"), fix_error!(&str, err_test))); -/// -/// let a = &b"efghblah"[..]; -/// let res_a = parser(a); -/// assert_eq!(res_a, Error(NodePosition( ErrorKind::Custom("custom error message"), a, Box::new(Position(ErrorKind::Fix, a))))); -/// # } -/// ``` -#[macro_export] -macro_rules! fix_error ( - ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), - $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), - $crate::IResult::Error(e) => { - let err = match e { - $crate::Err::Code($crate::ErrorKind::Custom(_)) | - $crate::Err::Node($crate::ErrorKind::Custom(_), _) => { - let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; - $crate::Err::Code(e) - }, - $crate::Err::Position($crate::ErrorKind::Custom(_), p) | - $crate::Err::NodePosition($crate::ErrorKind::Custom(_), p, _) => { - let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; - $crate::Err::Position(e, p) - }, - $crate::Err::Code(_) | - $crate::Err::Node(_, _) => { - let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; - $crate::Err::Code(e) - }, - $crate::Err::Position(_, p) | - $crate::Err::NodePosition(_, p, _) => { - let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; - $crate::Err::Position(e, p) - }, - }; - $crate::IResult::Error(err) - } - } - } - ); - ($i:expr, $t:ty, $f:expr) => ( - fix_error!($i, $t, call!($f)); + add_return_error!($i, $code, call!($f)); ); ); @@ -358,6 +418,7 @@ macro_rules! fix_error ( /// # #[macro_use] extern crate nom; /// # use std::collections; /// # use nom::IResult::Error; +/// # #[cfg(feature = "verbose-errors")] /// # use nom::Err::{Position,NodePosition}; /// # use nom::ErrorKind; /// # fn main() { @@ -365,7 +426,7 @@ macro_rules! fix_error ( /// /// let a = &b"abcd"[..]; /// let res_a = take_5(a); -/// assert_eq!(res_a, Error(Position(ErrorKind::Complete, a))); +/// assert_eq!(res_a, Error(error_position!(ErrorKind::Complete, a))); /// # } /// ``` /// @@ -373,11 +434,12 @@ macro_rules! fix_error ( macro_rules! complete ( ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - match $submac!($i, $($args)*) { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), $crate::IResult::Error(e) => $crate::IResult::Error(e), $crate::IResult::Incomplete(_) => { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Complete, $i)) + $crate::IResult::Error(error_position!($crate::ErrorKind::Complete, $i)) }, } } @@ -389,11 +451,12 @@ macro_rules! complete ( /// A bit like `std::try!`, this macro will return the remaining input and parsed value if the child parser returned `Done`, /// and will do an early return for `Error` and `Incomplete` -/// this can provide more flexibility than `chain!` if needed +/// this can provide more flexibility than `do_parse!` if needed /// /// ``` /// # #[macro_use] extern crate nom; /// # use nom::IResult::{self, Done, Error}; +/// # #[cfg(feature = "verbose-errors")] /// # use nom::Err::Position; /// # use nom::{be_u8,ErrorKind}; /// @@ -411,7 +474,7 @@ macro_rules! complete ( /// let arr2 = [0xFE, 2, 3, 4, 5]; /// // size is overflowing /// let r1 = take_add(&arr2[..], 42); -/// assert_eq!(r1, Error(Position(ErrorKind::ExprOpt,&[2,3,4,5][..]))); +/// assert_eq!(r1, Error(error_position!(ErrorKind::ExprOpt,&[2,3,4,5][..]))); /// # } /// ``` #[macro_export] @@ -428,153 +491,159 @@ macro_rules! try_parse ( ); ); -/// `flat_map!(R -> IResult, S -> IResult) => R -> IResult` -/// -/// combines a parser R -> IResult and -/// a parser S -> IResult to return another -/// parser R -> IResult +/// `map!(I -> IResult, O -> P) => I -> IResult` +/// maps a function on the result of a parser #[macro_export] -macro_rules! flat_map( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( +macro_rules! map( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( { + pub fn _unify R>(f: F, t: T) -> R { + f(t) + } match $submac!($i, $($args)*) { $crate::IResult::Error(e) => $crate::IResult::Error(e), $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), - $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { - $crate::IResult::Error(e) => { - let err = match e { - $crate::Err::Code(k) | $crate::Err::Node(k, _) | $crate::Err::Position(k, _) | $crate::Err::NodePosition(k, _, _) => { - $crate::Err::Position(k, $i) - } - }; - $crate::IResult::Error(err) - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(ref i2)) => $crate::IResult::Incomplete($crate::Needed::Size(*i2)), - $crate::IResult::Done(_, o2) => $crate::IResult::Done(i, o2) - } + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, _unify($g, o)) } } ); ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - flat_map!($i, $submac!($($args)*), call!($g)); - ); - ($i:expr, $f:expr, $g:expr) => ( - flat_map!($i, call!($f), call!($g)); - ); - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - flat_map!($i, call!($f), $submac!($($args)*)); - ); -); - -/// `map!(I -> IResult, O -> P) => I -> IResult` -/// maps a function on the result of a parser -#[macro_export] -macro_rules! map( - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - map_impl!($i, $submac!($($args)*), call!($g)); - ); - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( - map_impl!($i, $submac!($($args)*), $submac2!($($args2)*)); + map!(__impl $i, $submac!($($args)*), $g); ); ($i:expr, $f:expr, $g:expr) => ( - map_impl!($i, call!($f), call!($g)); - ); - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - map_impl!($i, call!($f), $submac!($($args)*)); + map!(__impl $i, call!($f), $g); ); ); -/// Internal parser, do not use directly -#[doc(hidden)] +/// `map_res!(I -> IResult, O -> Result

) => I -> IResult` +/// maps a function returning a Result on the output of a parser #[macro_export] -macro_rules! map_impl( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( +macro_rules! map_res ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( { - match $submac!($i, $($args)*) { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { $crate::IResult::Error(e) => $crate::IResult::Error(e), $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), - $crate::IResult::Done(i, o) => $crate::IResult::Done(i, $submac2!(o, $($args2)*)) + $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { + Ok(output) => $crate::IResult::Done(i, output), + Err(_) => $crate::IResult::Error(error_position!($crate::ErrorKind::MapRes, $i)) + } } } ); -); - -/// `map_res!(I -> IResult, O -> Result

) => I -> IResult` -/// maps a function returning a Result on the output of a parser -#[macro_export] -macro_rules! map_res ( ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - map_res_impl!($i, $submac!($($args)*), call!($g)); + map_res!(__impl $i, $submac!($($args)*), call!($g)); ); ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( - map_res_impl!($i, $submac!($($args)*), $submac2!($($args2)*)); + map_res!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); ); ($i:expr, $f:expr, $g:expr) => ( - map_res_impl!($i, call!($f), call!($g)); + map_res!(__impl $i, call!($f), call!($g)); ); ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - map_res_impl!($i, call!($f), $submac!($($args)*)); + map_res!(__impl $i, call!($f), $submac!($($args)*)); ); ); -/// Internal parser, do not use directly -#[doc(hidden)] +/// `map_opt!(I -> IResult, O -> Option

) => I -> IResult` +/// maps a function returning an Option on the output of a parser #[macro_export] -macro_rules! map_res_impl ( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( +macro_rules! map_opt ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( { - match $submac!($i, $($args)*) { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { $crate::IResult::Error(e) => $crate::IResult::Error(e), $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { - Ok(output) => $crate::IResult::Done(i, output), - Err(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::MapRes, $i)) + ::std::option::Option::Some(output) => $crate::IResult::Done(i, output), + ::std::option::Option::None => $crate::IResult::Error(error_position!($crate::ErrorKind::MapOpt, $i)) } } } ); -); - - -/// `map_opt!(I -> IResult, O -> Option

) => I -> IResult` -/// maps a function returning an Option on the output of a parser -#[macro_export] -macro_rules! map_opt ( ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - map_opt_impl!($i, $submac!($($args)*), call!($g)); + map_opt!(__impl $i, $submac!($($args)*), call!($g)); ); ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( - map_opt_impl!($i, $submac!($($args)*), $submac2!($($args2)*)); + map_opt!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); ); ($i:expr, $f:expr, $g:expr) => ( - map_opt_impl!($i, call!($f), call!($g)); + map_opt!(__impl $i, call!($f), call!($g)); ); ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - map_opt_impl!($i, call!($f), $submac!($($args)*)); + map_opt!(__impl $i, call!($f), $submac!($($args)*)); ); ); -/// Internal parser, do not use directly -#[doc(hidden)] +/// `parse_to!(O) => I -> IResult` +/// uses the `parse` method from `std::str::FromStr` to convert the current +/// input to the specified type +/// +/// this will completely consume the input #[macro_export] -macro_rules! map_opt_impl ( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( +macro_rules! parse_to ( + ($i:expr, $t:ty ) => ( { - match $submac!($i, $($args)*) { + use $crate::ParseTo; + use $crate::Slice; + use $crate::InputLength; + match ($i).parse_to() { + ::std::option::Option::Some(output) => $crate::IResult::Done($i.slice(..$i.input_len()), output), + ::std::option::Option::None => $crate::IResult::Error(error_position!($crate::ErrorKind::MapOpt, $i)) + } + } + ); +); + +/// `verify!(I -> IResult, O -> bool) => I -> IResult` +/// returns the result of the child parser if it satisfies a verification function +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # use nom::be_u32; +/// # fn main() { +/// named!(check, verify!(be_u32, |val:u32| val >= 0 && val < 3)); +/// # } +/// ``` +#[macro_export] +macro_rules! verify ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { $crate::IResult::Error(e) => $crate::IResult::Error(e), $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), - $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { - ::std::option::Option::Some(output) => $crate::IResult::Done(i, output), - ::std::option::Option::None => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::MapOpt, $i)) + $crate::IResult::Done(i, o) => if $submac2!(o, $($args2)*) { + $crate::IResult::Done(i, o) + } else { + $crate::IResult::Error(error_position!($crate::ErrorKind::Verify, $i)) } } } ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + verify!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + verify!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + verify!(__impl $i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + verify!(__impl $i, call!($f), $submac!($($args)*)); + ); ); /// `value!(T, R -> IResult ) => R -> IResult` @@ -603,7 +672,8 @@ macro_rules! value ( { match $submac!($i, $($args)*) { $crate::IResult::Done(i,_) => { - $crate::IResult::Done(i, $res) + let res: $crate::IResult<_,_> = $crate::IResult::Done(i, $res); + res }, $crate::IResult::Error(e) => $crate::IResult::Error(e), $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) @@ -614,7 +684,10 @@ macro_rules! value ( value!($i, $res, call!($f)) ); ($i:expr, $res:expr) => ( - $crate::IResult::Done($i, $res) + { + let res: $crate::IResult<_,_> = $crate::IResult::Done($i, $res); + res + } ); ); @@ -628,7 +701,7 @@ macro_rules! expr_res ( { match $e { Ok(output) => $crate::IResult::Done($i, output), - Err(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ExprRes, $i)) + Err(_) => $crate::IResult::Error(error_position!($crate::ErrorKind::ExprRes, $i)) } } ); @@ -642,15 +715,16 @@ macro_rules! expr_res ( /// ``` /// # #[macro_use] extern crate nom; /// # use nom::IResult::{self, Done, Error}; +/// # #[cfg(feature = "verbose-errors")] /// # use nom::Err::Position; /// # use nom::{be_u8,ErrorKind}; /// /// fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> { -/// chain!(input, -/// sz: be_u8 ~ -/// length: expr_opt!(size.checked_add(sz)) ~ // checking for integer overflow (returns an Option) -/// data: take!(length) , -/// ||{ data } +/// do_parse!(input, +/// sz: be_u8 >> +/// length: expr_opt!(size.checked_add(sz)) >> // checking for integer overflow (returns an Option) +/// data: take!(length) >> +/// (data) /// ) /// } /// # fn main() { @@ -661,7 +735,7 @@ macro_rules! expr_res ( /// let arr2 = [0xFE, 2, 3, 4, 5]; /// // size is overflowing /// let r1 = take_add(&arr2[..], 42); -/// assert_eq!(r1, Error(Position(ErrorKind::ExprOpt,&[2,3,4,5][..]))); +/// assert_eq!(r1, Error(error_position!(ErrorKind::ExprOpt,&[2,3,4,5][..]))); /// # } /// ``` #[macro_export] @@ -670,2011 +744,423 @@ macro_rules! expr_opt ( { match $e { ::std::option::Option::Some(output) => $crate::IResult::Done($i, output), - ::std::option::Option::None => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ExprOpt, $i)) + ::std::option::Option::None => $crate::IResult::Error(error_position!($crate::ErrorKind::ExprOpt, $i)) } } ); ); -/// `chain!(I->IResult ~ I->IResult ~ ... I->IResult , || { return O } ) => I -> IResult` -/// chains parsers and assemble the results through a closure -/// -/// The input type `I` must implement `nom::InputLength`. +/// `opt!(I -> IResult) => I -> IResult>` +/// make the underlying parser optional /// -/// This combinator will count how much data is consumed by every child parser and take it into account if -/// there is not enough data +/// returns an Option of the returned type. This parser returns `Some(result)` if the child parser +/// succeeds,`None` if it fails, and `Incomplete` if it did not have enough data to decide /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::{self, Done, Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; -/// #[derive(PartialEq,Eq,Debug)] -/// struct B { -/// a: u8, -/// b: Option -/// } -/// -/// named!(y, tag!("efgh")); -/// -/// fn ret_int(i:&[u8]) -> IResult<&[u8], u8> { Done(i, 1) } -/// named!(ret_y<&[u8], u8>, map!(y, |_| 1)); // return 1 if the "efgh" tag is found -/// -/// named!(z<&[u8], B>, -/// chain!( -/// tag!("abcd") ~ // the '~' character is used as separator -/// aa: ret_int ~ // the result of that parser will be used in the closure -/// tag!("abcd")? ~ // this parser is optional -/// bb: ret_y? , // the result of that parser is an option -/// // the last parser in the chain is followed by a ',' -/// ||{B{a: aa, b: bb}} -/// ) -/// ); -/// +/// # use nom::IResult::Done; /// # fn main() { -/// // the first "abcd" tag is not present, we have an error -/// let r1 = z(&b"efgh"[..]); -/// assert_eq!(r1, Error(Position(ErrorKind::Tag,&b"efgh"[..]))); -/// -/// // everything is present, everything is parsed -/// let r2 = z(&b"abcdabcdefgh"[..]); -/// assert_eq!(r2, Done(&b""[..], B{a: 1, b: Some(1)})); -/// -/// // the second "abcd" tag is optional -/// let r3 = z(&b"abcdefgh"[..]); -/// assert_eq!(r3, Done(&b""[..], B{a: 1, b: Some(1)})); +/// named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) ); /// -/// // the result of ret_y is optional, as seen in the B structure -/// let r4 = z(&b"abcdabcdwxyz"[..]); -/// assert_eq!(r4, Done(&b"wxyz"[..], B{a: 1, b: None})); -/// # } +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); +/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], None)); +/// # } /// ``` #[macro_export] -macro_rules! chain ( - ($i:expr, $($rest:tt)*) => ( - { - chaining_parser!($i, 0usize, $($rest)*) - } - ); -); - -/// Internal parser, do not use directly -#[doc(hidden)] -#[macro_export] -macro_rules! chaining_parser ( - ($i:expr, $consumed:expr, $e:ident ~ $($rest:tt)*) => ( - chaining_parser!($i, $consumed, call!($e) ~ $($rest)*); - ); - ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ~ $($rest:tt)*) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,_) => { - chaining_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), $($rest)*) - } - } - } -); - - ($i:expr, $consumed:expr, $e:ident ? ~ $($rest:tt)*) => ( - chaining_parser!($i, $consumed, call!($e) ? ~ $($rest)*); - ); - - ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ? ~ $($rest:tt)*) => ( +macro_rules! opt( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - let res = $submac!($i, $($args)*); - if let $crate::IResult::Incomplete(inc) = res { - match inc { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - } - } else { - let input = if let $crate::IResult::Done(i,_) = res { - i - } else { - $i - }; - chaining_parser!(input, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input)), $($rest)*) + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + _ => { + let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None); + res + }, } } ); - - ($i:expr, $consumed:expr, $field:ident : $e:ident ~ $($rest:tt)*) => ( - chaining_parser!($i, $consumed, $field: call!($e) ~ $($rest)*); + ($i:expr, $f:expr) => ( + opt!($i, call!($f)); ); +); - ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)*) => ( +/// `opt_res!(I -> IResult) => I -> IResult>` +/// make the underlying parser optional +/// +/// returns a Result, with Err containing the parsing error +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!( o<&[u8], Result<&[u8], nom::Err<&[u8]> > >, opt_res!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Ok(&b"abcd"[..]))); +/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, &b[..])))); +/// # } +/// ``` +#[macro_export] +macro_rules! opt_res ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - let $field = o; - chaining_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), $($rest)*) - } + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::result::Result::Ok(o)), + $crate::IResult::Error(e) => $crate::IResult::Done($i, ::std::result::Result::Err(e)), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) } } ); - - ($i:expr, $consumed:expr, mut $field:ident : $e:ident ~ $($rest:tt)*) => ( - chaining_parser!($i, $consumed, mut $field: call!($e) ~ $($rest)*); + ($i:expr, $f:expr) => ( + opt_res!($i, call!($f)); ); +); - ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)*) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - let mut $field = o; - chaining_parser!(i, $consumed + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i), $($rest)*) - } - } - } - ); - - ($i:expr, $consumed:expr, $field:ident : $e:ident ? ~ $($rest:tt)*) => ( - chaining_parser!($i, $consumed, $field : call!($e) ? ~ $($rest)*); - ); - - ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ? ~ $($rest:tt)*) => ( - { - let res = $submac!($i, $($args)*); - if let $crate::IResult::Incomplete(inc) = res { - match inc { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - } - } else { - let ($field,input) = if let $crate::IResult::Done(i,o) = res { - (::std::option::Option::Some(o),i) - } else { - (::std::option::Option::None,$i) - }; - chaining_parser!(input, $consumed + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input), $($rest)*) - } - } - ); - - ($i:expr, $consumed:expr, mut $field:ident : $e:ident ? ~ $($rest:tt)*) => ( - chaining_parser!($i, $consumed, mut $field : call!($e) ? ~ $($rest)*); - ); - - ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) ? ~ $($rest:tt)*) => ( - { - let res = $submac!($i, $($args)*); - if let $crate::IResult::Incomplete(inc) = res { - match inc { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - } - } else { - let (mut $field,input) = if let $crate::IResult::Done(i,o) = res { - (::std::option::Option::Some(o),i) - } else { - (::std::option::Option::None,$i) - }; - chaining_parser!(input, $consumed + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input), $($rest)*) - } - } - ); - - // ending the chain - ($i:expr, $consumed:expr, $e:ident, $assemble:expr) => ( - chaining_parser!($i, $consumed, call!($e), $assemble); - ); - - ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ), $assemble:expr) => ( - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,_) => { - $crate::IResult::Done(i, $assemble()) - } - } - ); - - ($i:expr, $consumed:expr, $e:ident ?, $assemble:expr) => ( - chaining_parser!($i, $consumed, call!($e) ?, $assemble); - ); - - ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ?, $assemble:expr) => ({ - let res = $submac!($i, $($args)*); - if let $crate::IResult::Incomplete(inc) = res { - match inc { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - } - } else { - let input = if let $crate::IResult::Done(i,_) = res { - i - } else { - $i - }; - $crate::IResult::Done(input, $assemble()) - } - }); - - ($i:expr, $consumed:expr, $field:ident : $e:ident, $assemble:expr) => ( - chaining_parser!($i, $consumed, $field: call!($e), $assemble); - ); - - ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ), $assemble:expr) => ( - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - let $field = o; - $crate::IResult::Done(i, $assemble()) - } - } - ); - - ($i:expr, $consumed:expr, mut $field:ident : $e:ident, $assemble:expr) => ( - chaining_parser!($i, $consumed, mut $field: call!($e), $assemble); - ); - - ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ), $assemble:expr) => ( - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - let mut $field = o; - $crate::IResult::Done(i, $assemble()) - } - } - ); - - ($i:expr, $consumed:expr, $field:ident : $e:ident ? , $assemble:expr) => ( - chaining_parser!($i, $consumed, $field : call!($e) ? , $assemble); - ); - - ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ? , $assemble:expr) => ({ - let res = $submac!($i, $($args)*); - if let $crate::IResult::Incomplete(inc) = res { - match inc { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - } - } else { - let ($field,input) = if let $crate::IResult::Done(i,o) = res { - (::std::option::Option::Some(o), i) - } else { - (::std::option::Option::None, $i) - }; - $crate::IResult::Done(input, $assemble()) - } - }); - - ($i:expr, $consumed:expr, mut $field:ident : $e:ident ? , $assemble:expr) => ( - chaining_parser!($i, $consumed, $field : call!($e) ? , $assemble); - ); - - ($i:expr, $consumed:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) ? , $assemble:expr) => ({ - let res = $submac!($i, $($args)*); - if let $crate::IResult::Incomplete(inc) = res { - match inc { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(i) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - } - } else { - let (mut $field,input) = if let $crate::IResult::Done(i,o) = res { - (::std::option::Option::Some(o), i) - } else { - (::std::option::Option::None, $i) - }; - $crate::IResult::Done(input, $assemble()) - } - }); - - ($i:expr, $consumed:expr, $assemble:expr) => ( - $crate::IResult::Done($i, $assemble()) - ) -); - - -/// `tuple!(I->IResult, I->IResult, ... I->IResult) => I -> IResult` -/// chains parsers and assemble the sub results in a tuple. -/// -/// The input type `I` must implement `nom::InputLength`. -/// -/// This combinator will count how much data is consumed by every child parser and take it into account if -/// there is not enough data -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::{self, Done, Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; -/// # use nom::be_u16; -/// // the return type depends of the children parsers -/// named!(parser<&[u8], (u16, &[u8], &[u8]) >, -/// tuple!( -/// be_u16 , -/// take!(3), -/// tag!("fg") -/// ) -/// ); -/// -/// # fn main() { -/// assert_eq!( -/// parser(&b"abcdefgh"[..]), -/// Done( -/// &b"h"[..], -/// (0x6162u16, &b"cde"[..], &b"fg"[..]) -/// ) -/// ); -/// # } -/// ``` -#[macro_export] -macro_rules! tuple ( - ($i:expr, $($rest:tt)*) => ( - { - tuple_parser!($i, 0usize, (), $($rest)*) - } - ); -); - -/// Internal parser, do not use directly -#[doc(hidden)] -#[macro_export] -macro_rules! tuple_parser ( - ($i:expr, $consumed:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => ( - tuple_parser!($i, $consumed, ($($parsed),*), call!($e), $($rest)*); - ); - ($i:expr, $consumed:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - tuple_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), (o), $($rest)*) - } - } - } - ); - ($i:expr, $consumed:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - tuple_parser!(i, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), ($($parsed)* , o), $($rest)*) - } - } - } - ); - ($i:expr, $consumed:expr, ($($parsed:tt),*), $e:ident) => ( - tuple_parser!($i, $consumed, ($($parsed),*), call!($e)); - ); - ($i:expr, $consumed:expr, (), $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - $crate::IResult::Done(i, (o)) - } - } - } - ); - ($i:expr, $consumed:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), - $crate::IResult::Done(i,o) => { - $crate::IResult::Done(i, ($($parsed),* , o)) - } - } - } - ); - ($i:expr, $consumed:expr, ($($parsed:expr),*)) => ( - { - $crate::IResult::Done($i, ($($parsed),*)) - } - ); -); -/// `alt!(I -> IResult | I -> IResult | ... | I -> IResult ) => I -> IResult` -/// try a list of parsers, return the result of the first successful one -/// -/// If one of the parser returns Incomplete, alt will return Incomplete, to retry -/// once you get more input. Note that it is better for performance to know the -/// minimum size of data you need before you get into alt. -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # fn main() { -/// named!( test, alt!( tag!( "abcd" ) | tag!( "efgh" ) ) ); -/// let r1 = test(b"abcdefgh"); -/// assert_eq!(r1, Done(&b"efgh"[..], &b"abcd"[..])); -/// let r2 = test(&b"efghijkl"[..]); -/// assert_eq!(r2, Done(&b"ijkl"[..], &b"efgh"[..])); -/// # } -/// ``` -/// -/// There is another syntax for alt allowing a block to manipulate the result: -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # fn main() { -/// #[derive(Debug,PartialEq,Eq)] -/// enum Tagged { -/// Abcd, -/// Efgh, -/// Took(usize) -/// } -/// named!(test, alt!( -/// tag!("abcd") => { |_| Tagged::Abcd } -/// | tag!("efgh") => { |_| Tagged::Efgh } -/// | take!(5) => { |res: &[u8]| Tagged::Took(res.len()) } // the closure takes the result as argument if the parser is successful -/// )); -/// let r1 = test(b"abcdefgh"); -/// assert_eq!(r1, Done(&b"efgh"[..], Tagged::Abcd)); -/// let r2 = test(&b"efghijkl"[..]); -/// assert_eq!(r2, Done(&b"ijkl"[..], Tagged::Efgh)); -/// let r3 = test(&b"mnopqrst"[..]); -/// assert_eq!(r3, Done(&b"rst"[..], Tagged::Took(5))); -/// # } -/// ``` -/// -/// **BE CAREFUL** there is a case where the behaviour of `alt!` can be confusing: -/// -/// when the alternatives have different lengths, like this case: -/// -/// ```ignore -/// named!( test, alt!( tag!( "abcd" ) | tag!( "ef" ) | tag!( "ghi" ) | tag!( "kl" ) ) ); -/// ``` -/// -/// With this parser, if you pass `"abcd"` as input, the first alternative parses it correctly, -/// but if you pass `"efg"`, the first alternative will return `Incomplete`, since it needs an input -/// of 4 bytes. This behaviour of `alt!` is expected: if you get a partial input that isn't matched -/// by the first alternative, but would match if the input was complete, you want `alt!` to indicate -/// that it cannot decide with limited information. -/// -/// There are two ways to fix this behaviour. The first one consists in ordering the alternatives -/// by size, like this: -/// -/// ```ignore -/// named!( test, alt!( tag!( "ef" ) | tag!( "kl") | tag!( "ghi" ) | tag!( "abcd" ) ) ); -/// ``` -/// -/// With this solution, the largest alternative will be tested last. -/// -/// The other solution uses the `complete!` combinator, which transforms an `Incomplete` in an -/// `Error`. If one of the alternatives returns `Incomplete` but is wrapped by `complete!`, -/// `alt!` will try the next alternative. This is useful when you know that -/// you will not get partial input: -/// -/// ```ignore -/// named!( test, -/// alt!( -/// complete!( tag!( "abcd" ) ) | -/// complete!( tag!( "ef" ) ) | -/// complete!( tag!( "ghi" ) ) | -/// complete!( tag!( "kl" ) ) -/// ) -/// ); -/// ``` -/// -/// If you want the `complete!` combinator to be applied to all rules then use the convenience -/// `alt_complete!` macro (see below). -/// -/// This behaviour of `alt!` can get especially confusing if multiple alternatives have different -/// sizes but a common prefix, like this: -/// -/// ```ignore -/// named!( test, alt!( tag!( "abcd" ) | tag!( "ab" ) | tag!( "ef" ) ) ); -/// ``` -/// -/// in that case, if you order by size, passing `"abcd"` as input will always be matched by the -/// smallest parser, so the solution using `complete!` is better suited. -/// -/// You can also nest multiple `alt!`, like this: -/// -/// ```ignore -/// named!( test, -/// alt!( -/// preceded!( -/// tag!("ab"), -/// alt!( -/// tag!( "cd" ) | -/// eof -/// ) -/// ) -/// | tag!( "ef" ) -/// ) -/// ); -/// ``` -/// -/// `preceded!` will first parse `"ab"` then, if successful, try the alternatives "cd", -/// or empty input (End Of File). If none of them work, `preceded!` will fail and -/// "ef" will be tested. -/// -#[macro_export] -macro_rules! alt ( - ($i:expr, $($rest:tt)*) => ( - { - alt_parser!($i, $($rest)*) - } - ); -); - -/// Internal parser, do not use directly -#[doc(hidden)] -#[macro_export] -macro_rules! alt_parser ( - ($i:expr, $e:ident | $($rest:tt)*) => ( - alt_parser!($i, call!($e) | $($rest)*); - ); - - ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( - { - let res = $subrule!($i, $($args)*); - match res { - $crate::IResult::Done(_,_) => res, - $crate::IResult::Incomplete(_) => res, - _ => alt_parser!($i, $($rest)*) - } - } - ); - - ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( - { - match $subrule!( $i, $($args)* ) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), - $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), - $crate::IResult::Error(_) => { - alt_parser!($i, $($rest)*) - } - } - } - ); - - ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => ( - alt_parser!($i, call!($e) => { $gen } | $($rest)*); - ); - - ($i:expr, $e:ident => { $gen:expr }) => ( - alt_parser!($i, call!($e) => { $gen }); - ); - - ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( - { - match $subrule!( $i, $($args)* ) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), - $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), - $crate::IResult::Error(_) => { - alt_parser!($i) - } - } - } - ); - - ($i:expr, $e:ident) => ( - alt_parser!($i, call!($e)); - ); - - ($i:expr, $subrule:ident!( $($args:tt)*)) => ( - { - match $subrule!( $i, $($args)* ) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i,o), - $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), - $crate::IResult::Error(_) => { - alt_parser!($i) - } - } - } - ); - - ($i:expr) => ( - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Alt,$i)) - ); -); - -/// This is a combination of the `alt!` and `complete!` combinators. Rather -/// than returning `Incomplete` on partial input, `alt_complete!` will try the -/// next alternative in the chain. You should use this only if you know you -/// will not receive partial input for the rules you're trying to match (this -/// is almost always the case for parsing programming languages). -#[macro_export] -macro_rules! alt_complete ( - // Recursive rules (must include `complete!` around the head) - - ($i:expr, $e:ident | $($rest:tt)*) => ( - alt_complete!($i, complete!(call!($e)) | $($rest)*); - ); - - ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( - { - let res = complete!($i, $subrule!($($args)*)); - match res { - $crate::IResult::Done(_,_) => res, - _ => alt_complete!($i, $($rest)*), - } - } - ); - - ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( - { - match complete!($i, $subrule!($($args)*)) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), - _ => alt_complete!($i, $($rest)*), - } - } - ); - - ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => ( - alt_complete!($i, complete!(call!($e)) => { $gen } | $($rest)*); - ); - - // Tail (non-recursive) rules - - ($i:expr, $e:ident => { $gen:expr }) => ( - alt_complete!($i, call!($e) => { $gen }); - ); - - ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( - alt_parser!($i, $subrule!($($args)*) => { $gen }) - ); - - ($i:expr, $e:ident) => ( - alt_complete!($i, call!($e)); - ); - - ($i:expr, $subrule:ident!( $($args:tt)*)) => ( - alt_parser!($i, $subrule!($($args)*)) - ); -); - -/// `switch!(I -> IResult, P => I -> IResult | ... | P => I -> IResult ) => I -> IResult` -/// choose the next parser depending on the result of the first one, if successful, -/// and returns the result of the second parser -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done,Error}; -/// # use nom::Err::{Position, NodePosition}; -/// # use nom::ErrorKind; -/// # fn main() { -/// named!(sw, -/// switch!(take!(4), -/// b"abcd" => tag!("XYZ") | -/// b"efgh" => tag!("123") -/// ) -/// ); -/// -/// let a = b"abcdXYZ123"; -/// let b = b"abcdef"; -/// let c = b"efgh123"; -/// let d = b"blah"; -/// -/// assert_eq!(sw(&a[..]), Done(&b"123"[..], &b"XYZ"[..])); -/// assert_eq!(sw(&b[..]), Error(NodePosition(ErrorKind::Switch, &b"abcdef"[..], Box::new(Position(ErrorKind::Tag, &b"ef"[..]))))); -/// assert_eq!(sw(&c[..]), Done(&b""[..], &b"123"[..])); -/// assert_eq!(sw(&d[..]), Error(Position(ErrorKind::Switch, &b"blah"[..]))); -/// # } -/// ``` -/// -/// Due to limitations in Rust macros, it is not possible to have simple functions on the right hand -/// side of pattern, like this: -/// -/// ```ignore -/// named!(sw, -/// switch!(take!(4), -/// b"abcd" => tag!("XYZ") | -/// b"efgh" => tag!("123") -/// ) -/// ); -/// ``` -/// -/// If you want to pass your own functions instead, you can use the `call!` combinator as follows: -/// -/// ```ignore -/// named!(xyz, tag!("XYZ")); -/// named!(num, tag!("123")); -/// named!(sw, -/// switch!(take!(4), -/// b"abcd" => call!(xyz) | -/// b"efgh" => call!(num) -/// ) -/// ); -/// ``` -/// -#[macro_export] -macro_rules! switch ( - ($i:expr, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( - { - switch_impl!($i, $submac!($($args)*), $($rest)*) - } - ); - ($i:expr, $e:ident, $($rest:tt)*) => ( - { - switch_impl!($i, call!($e), $($rest)*) - } - ); -); - -/// Internal parser, do not use directly -#[doc(hidden)] -#[macro_export] -macro_rules! switch_impl ( - ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Error(e) => $crate::IResult::Error($crate::Err::NodePosition( - $crate::ErrorKind::Switch, $i, ::std::boxed::Box::new(e) - )), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i, o) => { - match o { - $($p => match $subrule!(i, $($args2)*) { - $crate::IResult::Error(e) => $crate::IResult::Error($crate::Err::NodePosition( - $crate::ErrorKind::Switch, $i, ::std::boxed::Box::new(e) - )), - a => a, - }),*, - _ => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Switch,$i)) - } - } - } - } - ); -); -/// `opt!(I -> IResult) => I -> IResult>` -/// make the underlying parser optional -/// -/// returns an Option of the returned type. This parser returns `Some(result)` if the child parser -/// succeeds,`None` if it fails, and `Incomplete` if it did not have enough data to decide -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # fn main() { -/// named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) ); -/// -/// let a = b"abcdef"; -/// let b = b"bcdefg"; -/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); -/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], None)); -/// # } -/// ``` -#[macro_export] -macro_rules! opt( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), - $crate::IResult::Error(_) => $crate::IResult::Done($i, ::std::option::Option::None), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } - ); - ($i:expr, $f:expr) => ( - opt!($i, call!($f)); - ); -); - -/// `opt_res!(I -> IResult) => I -> IResult>` -/// make the underlying parser optional -/// -/// returns a Result, with Err containing the parsing error -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; -/// # fn main() { -/// named!( o<&[u8], Result<&[u8], nom::Err<&[u8]> > >, opt_res!( tag!( "abcd" ) ) ); -/// -/// let a = b"abcdef"; -/// let b = b"bcdefg"; -/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Ok(&b"abcd"[..]))); -/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], Err(Position(ErrorKind::Tag, &b[..])))); -/// # } -/// ``` -#[macro_export] -macro_rules! opt_res ( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i, Ok(o)), - $crate::IResult::Error(e) => $crate::IResult::Done($i, Err(e)), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } - ); - ($i:expr, $f:expr) => ( - opt_res!($i, call!($f)); - ); -); - -/// `cond_with_error!(bool, I -> IResult) => I -> IResult>` -/// Conditional combinator -/// -/// Wraps another parser and calls it if the -/// condition is met. This combinator returns -/// an Option of the return type of the child -/// parser. -/// -/// This is especially useful if a parser depends -/// on the value return by a preceding parser in -/// a `chain!`. -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # use nom::IResult; -/// # fn main() { -/// let b = true; -/// let f: Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], -/// cond!( b, tag!("abcd") )) -/// ); -/// -/// let a = b"abcdef"; -/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); -/// -/// let b2 = false; -/// let f2:Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], -/// cond!( b2, tag!("abcd") )) -/// ); -/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None)); -/// # } -/// ``` -/// -#[macro_export] -macro_rules! cond_with_error( - ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( - { - if $cond { - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } else { - $crate::IResult::Done($i, ::std::option::Option::None) - } - } - ); - ($i:expr, $cond:expr, $f:expr) => ( - cond!($i, $cond, call!($f)); - ); -); - -/// `cond!(bool, I -> IResult) => I -> IResult>` -/// Conditional combinator -/// -/// Wraps another parser and calls it if the -/// condition is met. This combinator returns -/// an Option of the return type of the child -/// parser. -/// -/// This is especially useful if a parser depends -/// on the value return by a preceding parser in -/// a `chain!`. -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # use nom::IResult; -/// # fn main() { -/// let b = true; -/// let f: Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], -/// cond!( b, tag!("abcd") )) -/// ); -/// -/// let a = b"abcdef"; -/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); -/// -/// let b2 = false; -/// let f2:Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], -/// cond!( b2, tag!("abcd") )) -/// ); -/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None)); -/// # } -/// ``` -/// -#[macro_export] -macro_rules! cond( - ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( - { - if $cond { - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), - $crate::IResult::Error(_) => $crate::IResult::Done($i, ::std::option::Option::None), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } else { - $crate::IResult::Done($i, ::std::option::Option::None) - } - } - ); - ($i:expr, $cond:expr, $f:expr) => ( - cond!($i, $cond, call!($f)); - ); -); - -/// `cond_reduce!(bool, I -> IResult) => I -> IResult` -/// Conditional combinator with error -/// -/// Wraps another parser and calls it if the -/// condition is met. This combinator returns -/// an error if the condition is false -/// -/// This is especially useful if a parser depends -/// on the value return by a preceding parser in -/// a `chain!`. -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done,Error}; -/// # use nom::{Err,ErrorKind}; -/// # fn main() { -/// let b = true; -/// let f = closure!(&'static[u8], -/// cond_reduce!( b, tag!("abcd") ) -/// ); -/// -/// let a = b"abcdef"; -/// assert_eq!(f(&a[..]), Done(&b"ef"[..], &b"abcd"[..])); -/// -/// let b2 = false; -/// let f2 = closure!(&'static[u8], -/// cond_reduce!( b2, tag!("abcd") ) -/// ); -/// assert_eq!(f2(&a[..]), Error(Err::Position(ErrorKind::CondReduce, &a[..]))); -/// # } -/// ``` -/// -#[macro_export] -macro_rules! cond_reduce( - ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( - { - if $cond { - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,o) => $crate::IResult::Done(i, o), - $crate::IResult::Error(e) => $crate::IResult::Error(e), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::CondReduce, $i)) - } - } - ); - ($i:expr, $cond:expr, $f:expr) => ( - cond_reduce!($i, $cond, call!($f)); - ); -); - -/// `peek!(I -> IResult) => I -> IResult` -/// returns a result without consuming the input -/// -/// the embedded parser may return Incomplete -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # fn main() { -/// named!(ptag, peek!( tag!( "abcd" ) ) ); -/// -/// let r = ptag(&b"abcdefgh"[..]); -/// assert_eq!(r, Done(&b"abcdefgh"[..], &b"abcd"[..])); -/// # } -/// ``` -#[macro_export] -macro_rules! peek( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Done(_,o) => $crate::IResult::Done($i, o), - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } - ); - ($i:expr, $f:expr) => ( - peek!($i, call!($f)); - ); -); - -/// `not!(I -> IResult) => I -> IResult` -/// returns a result only if the embedded parser returns Error or Incomplete -/// does not consume the input -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done, Error}; -/// # use nom::Err::{Position}; -/// # use nom::ErrorKind; -/// # fn main() { -/// named!(not_e, chain!( -/// res: tag!("abc") ~ -/// not!(char!('e')), -/// || { res })); -/// -/// let r = not_e(&b"abcd"[..]); -/// assert_eq!(r, Done(&b"d"[..], &b"abc"[..])); -/// -/// let r2 = not_e(&b"abce"[..]); -/// assert_eq!(r2, Error(Position(ErrorKind::Not, &b"e"[..]))); -/// # } -/// ``` -#[macro_export] -macro_rules! not( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Done(_, _) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Not, $i)), - $crate::IResult::Error(_) => $crate::IResult::Done($i, &($i)[..0]), - $crate::IResult::Incomplete(_) => $crate::IResult::Done($i, &($i)[..0]) - } - } - ); -); - -/// `tap!(name: I -> IResult => { block }) => I -> IResult` -/// allows access to the parser's result without affecting it -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::Done; -/// # use std::str; -/// # fn main() { -/// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) ); -/// -/// let r = ptag(&b"abcdefgh"[..]); -/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..])); -/// # } -/// ``` -#[macro_export] -macro_rules! tap ( - ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => ( - { - match $submac!($i, $($args)*) { - $crate::IResult::Done(i,o) => { - let $name = o; - $e; - $crate::IResult::Done(i, $name) - }, - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) - } - } - ); - ($i:expr, $name: ident: $f:expr => $e:expr) => ( - tap!($i, $name: call!($f) => $e); - ); -); - -/// `pair!(I -> IResult, I -> IResult) => I -> IResult` -/// pair(X,Y), returns (x,y) -/// -#[macro_export] -macro_rules! pair( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( - { - tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) - } - ); - - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - pair!($i, $submac!($($args)*), call!($g)); - ); - - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - pair!($i, call!($f), $submac!($($args)*)); - ); - - ($i:expr, $f:expr, $g:expr) => ( - pair!($i, call!($f), call!($g)); - ); -); - -/// `separated_pair!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` -/// separated_pair(X,sep,Y) returns (x,y) -#[macro_export] -macro_rules! separated_pair( - ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => ( - { - match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i1, (o1, _, o2)) => { - $crate::IResult::Done(i1, (o1, o2)) - } - } - } - ); - - ($i:expr, $f:expr, $($rest:tt)+) => ( - separated_pair!($i, call!($f), $($rest)*); - ); -); - -/// `preceded!(I -> IResult, I -> IResult) => I -> IResult` -/// preceded(opening, X) returns X -#[macro_export] -macro_rules! preceded( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( - { - match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(remaining, (_,o)) => { - $crate::IResult::Done(remaining, o) - } - } - } - ); - - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - preceded!($i, $submac!($($args)*), call!($g)); - ); - - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - preceded!($i, call!($f), $submac!($($args)*)); - ); - - ($i:expr, $f:expr, $g:expr) => ( - preceded!($i, call!($f), call!($g)); - ); -); - -/// `terminated!(I -> IResult, I -> IResult) => I -> IResult` -/// terminated(X, closing) returns X -#[macro_export] -macro_rules! terminated( - ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( - { - match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(remaining, (o,_)) => { - $crate::IResult::Done(remaining, o) - } - } - } - ); - - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - terminated!($i, $submac!($($args)*), call!($g)); - ); - - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - terminated!($i, call!($f), $submac!($($args)*)); - ); - - ($i:expr, $f:expr, $g:expr) => ( - terminated!($i, call!($f), call!($g)); - ); -); - -/// `delimited!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` -/// delimited(opening, X, closing) returns X -#[macro_export] -macro_rules! delimited( - ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => ( - { - match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i1, (_, o, _)) => { - $crate::IResult::Done(i1, o) - } - } - } - ); - - ($i:expr, $f:expr, $($rest:tt)+) => ( - delimited!($i, call!($f), $($rest)*); - ); -); - -/// `separated_list!(I -> IResult, I -> IResult) => I -> IResult>` -/// separated_list(sep, X) returns Vec -#[macro_export] -macro_rules! separated_list( - ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( - { - let mut res = ::std::vec::Vec::new(); - let mut input = $i; - - // get the first element - match $submac!(input, $($args2)*) { - $crate::IResult::Error(_) => $crate::IResult::Done(input, ::std::vec::Vec::new()), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i,o) => { - if i.len() == input.len() { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::SeparatedList,input)) - } else { - res.push(o); - input = i; - - loop { - // get the separator first - if let $crate::IResult::Done(i2,_) = $sep!(input, $($args)*) { - if i2.len() == input.len() { - break; - } - - // get the element next - if let $crate::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) { - if i3.len() == i2.len() { - break; - } - res.push(o3); - input = i3; - } else { - break; - } - } else { - break; - } - } - $crate::IResult::Done(input, res) - } - }, - } - } - ); - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - separated_list!($i, $submac!($($args)*), call!($g)); - ); - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - separated_list!($i, call!($f), $submac!($($args)*)); - ); - ($i:expr, $f:expr, $g:expr) => ( - separated_list!($i, call!($f), call!($g)); - ); -); - -/// `separated_nonempty_list!(I -> IResult, I -> IResult) => I -> IResult>` -/// separated_nonempty_list(sep, X) returns Vec -#[macro_export] -macro_rules! separated_nonempty_list( - ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( - { - let mut res = ::std::vec::Vec::new(); - let mut input = $i; - - // get the first element - match $submac!(input, $($args2)*) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i,o) => { - if i.len() == input.len() { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::SeparatedNonEmptyList,input)) - } else { - res.push(o); - input = i; - - loop { - if let $crate::IResult::Done(i2,_) = $sep!(input, $($args)*) { - if i2.len() == input.len() { - break; - } - - if let $crate::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) { - if i3.len() == i2.len() { - break; - } - res.push(o3); - input = i3; - } else { - break; - } - } else { - break; - } - } - $crate::IResult::Done(input, res) - } - }, - } - } - ); - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - separated_nonempty_list!($i, $submac!($($args)*), call!($g)); - ); - ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( - separated_nonempty_list!($i, call!($f), $submac!($($args)*)); - ); - ($i:expr, $f:expr, $g:expr) => ( - separated_nonempty_list!($i, call!($f), call!($g)); - ); -); - -/// `many0!(I -> IResult) => I -> IResult>` -/// Applies the parser 0 or more times and returns the list of results in a Vec +/// `cond_with_error!(bool, I -> IResult) => I -> IResult>` +/// Conditional combinator /// -/// the embedded parser may return Incomplete +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an Option of the return type of the child +/// parser. +/// +/// This is especially useful if a parser depends +/// on the value returned by a preceding parser in +/// a `do_parse!`. /// /// ``` /// # #[macro_use] extern crate nom; /// # use nom::IResult::Done; +/// # use nom::IResult; /// # fn main() { -/// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); +/// let b = true; +/// let f: Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b, tag!("abcd") )) +/// ); /// -/// let a = b"abcdabcdefgh"; -/// let b = b"azerty"; +/// let a = b"abcdef"; +/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); /// -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); -/// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); -/// # } +/// let b2 = false; +/// let f2:Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b2, tag!("abcd") )) +/// ); +/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None)); +/// # } /// ``` -/// 0 or more +/// #[macro_export] -macro_rules! many0( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( +macro_rules! cond_with_error( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( { - use $crate::InputLength; - - let ret; - let mut res = ::std::vec::Vec::new(); - let mut input = $i; - - loop { - if input.input_len() == 0 { - ret = $crate::IResult::Done(input, res); break; - } - - match $submac!(input, $($args)*) { - $crate::IResult::Error(_) => { - ret = $crate::IResult::Done(input, res); break; - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => { - ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; - }, - $crate::IResult::Incomplete($crate::Needed::Size(i)) => { - let size = i + ($i).input_len() - input.input_len(); - ret = $crate::IResult::Incomplete($crate::Needed::Size(size)); break; - }, - $crate::IResult::Done(i, o) => { - // loop trip must always consume (otherwise infinite loops) - if i == input { - ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many0,input)); break; - } - - res.push(o); - input = i; - } + if $cond { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) } + } else { + let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None); + res } - - ret } ); - ($i:expr, $f:expr) => ( - many0!($i, call!($f)); + ($i:expr, $cond:expr, $f:expr) => ( + cond_with_error!($i, $cond, call!($f)); ); ); -/// `many1!(I -> IResult) => I -> IResult>` -/// Applies the parser 1 or more times and returns the list of results in a Vec +/// `cond!(bool, I -> IResult) => I -> IResult>` +/// Conditional combinator /// -/// the embedded parser may return Incomplete +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an Option of the return type of the child +/// parser. +/// +/// This is especially useful if a parser depends +/// on the value returned by a preceding parser in +/// a `do_parse!`. /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done, Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; +/// # use nom::IResult::Done; +/// # use nom::IResult; /// # fn main() { -/// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); +/// let b = true; +/// let f: Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b, tag!("abcd") )) +/// ); /// -/// let a = b"abcdabcdefgh"; -/// let b = b"azerty"; +/// let a = b"abcdef"; +/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); /// -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); -/// assert_eq!(multi(&b[..]), Error(Position(ErrorKind::Many1,&b[..]))); -/// # } +/// let b2 = false; +/// let f2:Box IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], +/// cond!( b2, tag!("abcd") )) +/// ); +/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None)); +/// # } /// ``` +/// #[macro_export] -macro_rules! many1( - ($i:expr, $submac:ident!( $($args:tt)* )) => ( +macro_rules! cond( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( { - use $crate::InputLength; - match $submac!($i, $($args)*) { - $crate::IResult::Error(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many1,$i)), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i1,o1) => { - if i1.input_len() == 0 { - $crate::IResult::Done(i1,vec![o1]) - } else { - - let mut res = ::std::vec::Vec::with_capacity(4); - res.push(o1); - let mut input = i1; - let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; - loop { - if input.input_len() == 0 { - break; - } - match $submac!(input, $($args)*) { - $crate::IResult::Error(_) => { - break; - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => { - incomplete = ::std::option::Option::Some($crate::Needed::Unknown); - break; - }, - $crate::IResult::Incomplete($crate::Needed::Size(i)) => { - incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); - break; - }, - $crate::IResult::Done(i, o) => { - if i.input_len() == input.input_len() { - break; - } - res.push(o); - input = i; - } - } - } - - match incomplete { - ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), - ::std::option::Option::None => $crate::IResult::Done(input, res) - } - } + if $cond { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Error(_) => { + let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None); + res + }, } + } else { + let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None); + res } } ); - ($i:expr, $f:expr) => ( - many1!($i, call!($f)); + ($i:expr, $cond:expr, $f:expr) => ( + cond!($i, $cond, call!($f)); ); ); -/// `many_m_n!(usize, usize, I -> IResult) => I -> IResult>` -/// Applies the parser between m and n times (n included) and returns the list of results in a Vec +/// `cond_reduce!(bool, I -> IResult) => I -> IResult` +/// Conditional combinator with error /// -/// the embedded parser may return Incomplete +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an error if the condition is false +/// +/// This is especially useful if a parser depends +/// on the value returned by a preceding parser in +/// a `do_parse!`. /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done, Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; +/// # use nom::IResult::{Done,Error}; +/// # use nom::{Err,ErrorKind}; /// # fn main() { -/// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); +/// let b = true; +/// let f = closure!(&'static[u8], +/// cond_reduce!( b, tag!("abcd") ) +/// ); /// -/// let a = b"abcdefgh"; -/// let b = b"abcdabcdefgh"; -/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// let a = b"abcdef"; +/// assert_eq!(f(&a[..]), Done(&b"ef"[..], &b"abcd"[..])); /// -/// assert_eq!(multi(&a[..]),Error(Position(ErrorKind::ManyMN,&a[..]))); -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); -/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); -/// # } +/// let b2 = false; +/// let f2 = closure!(&'static[u8], +/// cond_reduce!( b2, tag!("abcd") ) +/// ); +/// assert_eq!(f2(&a[..]), Error(error_position!(ErrorKind::CondReduce, &a[..]))); +/// # } /// ``` +/// #[macro_export] -macro_rules! many_m_n( - ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( +macro_rules! cond_reduce( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( { - use $crate::InputLength; - let mut res = ::std::vec::Vec::with_capacity($m); - let mut input = $i; - let mut count: usize = 0; - let mut err = false; - let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; - loop { - if count == $n { break } - match $submac!(input, $($args)*) { - $crate::IResult::Done(i, o) => { - // do not allow parsers that do not consume input (causes infinite loops) - if i.input_len() == input.input_len() { - break; - } - res.push(o); - input = i; - count += 1; - } - $crate::IResult::Error(_) => { - err = true; - break; - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => { - incomplete = ::std::option::Option::Some($crate::Needed::Unknown); - break; - }, - $crate::IResult::Incomplete($crate::Needed::Size(i)) => { - incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); - break; - }, - } - if input.input_len() == 0 { - break; - } - } - - if count < $m { - if err { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ManyMN,$i)) - } else { - match incomplete { - ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), - ::std::option::Option::None => $crate::IResult::Incomplete($crate::Needed::Unknown) - } + if $cond { + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) } } else { - match incomplete { - ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), - ::std::option::Option::None => $crate::IResult::Done(input, res) - } + $crate::IResult::Error(error_position!($crate::ErrorKind::CondReduce, $i)) } } ); - ($i:expr, $m:expr, $n: expr, $f:expr) => ( - many_m_n!($i, $m, $n, call!($f)); + ($i:expr, $cond:expr, $f:expr) => ( + cond_reduce!($i, $cond, call!($f)); ); ); -/// `count!(I -> IResult, nb) => I -> IResult>` -/// Applies the child parser a specified number of times +/// `peek!(I -> IResult) => I -> IResult` +/// returns a result without consuming the input +/// +/// the embedded parser may return Incomplete /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done,Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; +/// # use nom::IResult::Done; /// # fn main() { -/// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); -/// -/// let a = b"abcdabcdabcdef"; -/// let b = b"abcdefgh"; -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// named!(ptag, peek!( tag!( "abcd" ) ) ); /// -/// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); -/// assert_eq!(counter(&b[..]), Error(Position(ErrorKind::Count, &b[..]))); +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Done(&b"abcdefgh"[..], &b"abcd"[..])); /// # } /// ``` -/// #[macro_export] -macro_rules! count( - ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( +macro_rules! peek( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - let ret; - let mut input = $i; - let mut res = ::std::vec::Vec::with_capacity($count); - - loop { - if res.len() == $count { - ret = $crate::IResult::Done(input, res); break; - } - - match $submac!(input, $($args)*) { - $crate::IResult::Done(i,o) => { - res.push(o); - input = i; - }, - $crate::IResult::Error(_) => { - ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Count,$i)); break; - }, - $crate::IResult::Incomplete(_) => { - ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; - } - } + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(_,o) => $crate::IResult::Done($i, o), + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) } - - ret } ); - ($i:expr, $f:expr, $count: expr) => ( - count!($i, call!($f), $count); + ($i:expr, $f:expr) => ( + peek!($i, call!($f)); ); ); -/// `count_fixed!(O, I -> IResult, nb) => I -> IResult` -/// Applies the child parser a fixed number of times and returns a fixed size array -/// The type must be specified and it must be `Copy` +/// `not!(I -> IResult) => I -> IResult` +/// returns a result only if the embedded parser returns Error or Incomplete +/// does not consume the input /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done,Error}; +/// # use nom::IResult::{Done, Error}; +/// # #[cfg(feature = "verbose-errors")] /// # use nom::Err::Position; /// # use nom::ErrorKind; /// # fn main() { -/// named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], tag!( "abcd" ), 2 ) ); -/// // can omit the type specifier if returning slices -/// // named!(counter< [&[u8]; 2] >, count_fixed!( tag!( "abcd" ), 2 ) ); +/// named!(not_e, do_parse!( +/// res: tag!("abc") >> +/// not!(char!('e')) >> +/// (res) +/// )); /// -/// let a = b"abcdabcdabcdef"; -/// let b = b"abcdefgh"; -/// let res = [&b"abcd"[..], &b"abcd"[..]]; +/// let r = not_e(&b"abcd"[..]); +/// assert_eq!(r, Done(&b"d"[..], &b"abc"[..])); /// -/// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); -/// assert_eq!(counter(&b[..]), Error(Position(ErrorKind::Count, &b[..]))); +/// let r2 = not_e(&b"abce"[..]); +/// assert_eq!(r2, Error(error_position!(ErrorKind::Not, &b"e"[..]))); /// # } /// ``` -/// -#[macro_export] -macro_rules! count_fixed ( - ($i:expr, $typ:ty, $submac:ident!( $($args:tt)* ), $count: expr) => ( - { - let ret; - let mut input = $i; - // `$typ` must be Copy, and thus having no destructor, this is panic safe - let mut res: [$typ; $count] = unsafe{[::std::mem::uninitialized(); $count as usize]}; - let mut cnt: usize = 0; - - loop { - if cnt == $count { - ret = $crate::IResult::Done(input, res); break; - } - - match $submac!(input, $($args)*) { - $crate::IResult::Done(i,o) => { - res[cnt] = o; - cnt += 1; - input = i; - }, - $crate::IResult::Error(_) => { - ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Count,$i)); break; - }, - $crate::IResult::Incomplete(_) => { - ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; - } - } - } - - ret - } - ); - ($i:expr, $typ: ty, $f:ident, $count: expr) => ( - count_fixed!($i, $typ, call!($f), $count); - ); -); - -/// `length_value!(I -> IResult, I -> IResult) => I -> IResult>` -/// gets a number from the first parser, then applies the second parser that many times #[macro_export] -macro_rules! length_value( - ($i:expr, $f:expr, $g:expr) => ( +macro_rules! not( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - match $f($i) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), - $crate::IResult::Done(inum, onum) => { - let ret; - let length_token = $i.len() - inum.len(); - let mut input = inum; - let mut res = ::std::vec::Vec::new(); - - loop { - if res.len() == onum as usize { - ret = $crate::IResult::Done(input, res); break; - } - - match $g(input) { - $crate::IResult::Done(iparse, oparse) => { - res.push(oparse); - input = iparse; - }, - $crate::IResult::Error(_) => { - ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::LengthValue,$i)); break; - }, - $crate::IResult::Incomplete(a) => { - ret = match a { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(length) => $crate::IResult::Incomplete($crate::Needed::Size(length_token + onum as usize * length)) - }; - break; - } - } - } - - ret - } + use $crate::Slice; + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(_, _) => $crate::IResult::Error(error_position!($crate::ErrorKind::Not, $i)), + $crate::IResult::Error(_) => $crate::IResult::Done($i, ($i).slice(..0)), + $crate::IResult::Incomplete(_) => $crate::IResult::Done($i, ($i).slice(..0)) } } ); - ($i:expr, $f:expr, $g:expr, $length:expr) => ( - { - match $f($i) { - $crate::IResult::Error(a) => $crate::IResult::Error(a), - $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), - $crate::IResult::Done(inum, onum) => { - let ret; - let length_token = $i.len() - inum.len(); - let mut input = inum; - let mut res = ::std::vec::Vec::new(); - - loop { - if res.len() == onum as usize { - ret = $crate::IResult::Done(input, res); break; - } - - match $g(input) { - $crate::IResult::Done(iparse, oparse) => { - res.push(oparse); - input = iparse; - }, - $crate::IResult::Error(_) => { - ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::LengthValue,$i)); break; - }, - $crate::IResult::Incomplete(a) => { - ret = match a { - $crate::Needed::Unknown => $crate::IResult::Incomplete($crate::Needed::Unknown), - $crate::Needed::Size(_) => $crate::IResult::Incomplete($crate::Needed::Size(length_token + onum as usize * $length)) - }; - break; - } - } - } - - ret - } - } - } + ($i:expr, $f:expr) => ( + not!($i, call!($f)); ); ); -/// `fold_many0!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` -/// Applies the parser 0 or more times and folds the list of return values -/// -/// the embedded parser may return Incomplete +/// `tap!(name: I -> IResult => { block }) => I -> IResult` +/// allows access to the parser's result without affecting it /// /// ``` /// # #[macro_use] extern crate nom; /// # use nom::IResult::Done; +/// # use std::str; /// # fn main() { -/// named!(multi<&[u8], Vec<&[u8]> >, fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { -/// acc.push(item); -/// acc -/// })); -/// -/// let a = b"abcdabcdefgh"; -/// let b = b"azerty"; +/// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) ); /// -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); -/// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..])); /// # } /// ``` -/// 0 or more #[macro_export] -macro_rules! fold_many0( - ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( +macro_rules! tap ( + ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => ( { - use $crate::InputLength; - let ret; - let f = $f; - let mut res = $init; - let mut input = $i; - - loop { - if input.input_len() == 0 { - ret = $crate::IResult::Done(input, res); break; - } - - match $submac!(input, $($args)*) { - $crate::IResult::Error(_) => { - ret = $crate::IResult::Done(input, res); break; - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => { - ret = $crate::IResult::Incomplete($crate::Needed::Unknown); break; - }, - $crate::IResult::Incomplete($crate::Needed::Size(i)) => { - let size = i + ($i).input_len() - input.input_len(); - ret = $crate::IResult::Incomplete($crate::Needed::Size(size)); break; - }, - $crate::IResult::Done(i, o) => { - // loop trip must always consume (otherwise infinite loops) - if i == input { - ret = $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many0,input)); break; - } - - res = f(res, o); - input = i; - } - } + match $submac!($i, $($args)*) { + $crate::IResult::Done(i,o) => { + let $name = o; + $e; + $crate::IResult::Done(i, $name) + }, + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) } - - ret } ); - ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( - fold_many0!($i, call!($f), $init, $fold_f); - ); -); - -/// `fold_many1!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` -/// Applies the parser 1 or more times and folds the list of return values -/// -/// the embedded parser may return Incomplete -/// -/// ``` -/// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done, Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; -/// # fn main() { -/// named!(multi<&[u8], Vec<&[u8]> >, fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { -/// acc.push(item); -/// acc -/// })); -/// -/// let a = b"abcdabcdefgh"; -/// let b = b"azerty"; + ($i:expr, $name: ident: $f:expr => $e:expr) => ( + tap!($i, $name: call!($f) => $e); + ); +); + +/// `eof!()` returns its input if it is at the end of input data /// -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); -/// assert_eq!(multi(&b[..]), Error(Position(ErrorKind::Many1,&b[..]))); -/// # } -/// ``` +/// please note that for now, eof only means there's no more +/// data available, it does not work yet with smarter input +/// types #[macro_export] -macro_rules! fold_many1( - ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( +macro_rules! eof ( + ($i:expr,) => ( { use $crate::InputLength; - match $submac!($i, $($args)*) { - $crate::IResult::Error(_) => $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Many1,$i)), - $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), - $crate::IResult::Done(i1,o1) => { - let acc = $init; - let f = $f; - if i1.len() == 0 { - let acc = f(acc, o1); - $crate::IResult::Done(i1,acc) - } else { - let mut acc = f(acc, o1); - let mut input = i1; - let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; - loop { - if input.input_len() == 0 { - break; - } - match $submac!(input, $($args)*) { - $crate::IResult::Error(_) => { - break; - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => { - incomplete = ::std::option::Option::Some($crate::Needed::Unknown); - break; - }, - $crate::IResult::Incomplete($crate::Needed::Size(i)) => { - incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); - break; - }, - $crate::IResult::Done(i, o) => { - if i.input_len() == input.input_len() { - break; - } - acc = f(acc, o); - input = i; - } - } - } - - match incomplete { - ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), - ::std::option::Option::None => $crate::IResult::Done(input, acc) - } - } - } + if ($i).input_len() == 0 { + $crate::IResult::Done($i, $i) + } else { + $crate::IResult::Error(error_position!($crate::ErrorKind::Eof, $i)) } } ); - ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( - fold_many1!($i, call!($f), $init, $fold_f); - ); ); -/// `fold_many_m_n!(usize, usize, I -> IResult, R, Fn(R, O) -> R) => I -> IResult` -/// Applies the parser between m and n times (n included) and folds the list of return value -/// -/// the embedded parser may return Incomplete +/// `recognize!(I -> IResult ) => I -> IResult` +/// if the child parser was successful, return the consumed input as produced value /// /// ``` /// # #[macro_use] extern crate nom; -/// # use nom::IResult::{Done, Error}; -/// # use nom::Err::Position; -/// # use nom::ErrorKind; +/// # use nom::IResult::Done; /// # fn main() { -/// named!(multi<&[u8], Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { -/// acc.push(item); -/// acc -/// })); -/// -/// let a = b"abcdefgh"; -/// let b = b"abcdabcdefgh"; -/// let c = b"abcdabcdabcdabcdabcdefgh"; -/// -/// assert_eq!(multi(&a[..]),Error(Position(ErrorKind::ManyMN,&a[..]))); -/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); -/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; -/// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); +/// named!(x, recognize!(delimited!(tag!("")))); +/// let r = x(&b" aaa"[..]); +/// assert_eq!(r, Done(&b" aaa"[..], &b""[..])); /// # } /// ``` #[macro_export] -macro_rules! fold_many_m_n( - ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( +macro_rules! recognize ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( { - use $crate::InputLength; - let mut acc = $init; - let f = $f; - let mut input = $i; - let mut count: usize = 0; - let mut err = false; - let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; - loop { - if count == $n { break } - match $submac!(input, $($args)*) { - $crate::IResult::Done(i, o) => { - // do not allow parsers that do not consume input (causes infinite loops) - if i.input_len() == input.input_len() { - break; - } - acc = f(acc, o); - input = i; - count += 1; - } - $crate::IResult::Error(_) => { - err = true; - break; - }, - $crate::IResult::Incomplete($crate::Needed::Unknown) => { - incomplete = ::std::option::Option::Some($crate::Needed::Unknown); - break; - }, - $crate::IResult::Incomplete($crate::Needed::Size(i)) => { - incomplete = ::std::option::Option::Some($crate::Needed::Size(i + ($i).input_len() - input.input_len())); - break; - }, - } - if input.input_len() == 0 { - break; - } - } - - if count < $m { - if err { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::ManyMN,$i)) - } else { - match incomplete { - ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), - ::std::option::Option::None => $crate::IResult::Incomplete($crate::Needed::Unknown) - } - } - } else { - match incomplete { - ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), - ::std::option::Option::None => $crate::IResult::Done(input, acc) - } + use $crate::Offset; + use $crate::Slice; + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(i,_) => { + let index = (&$i).offset(&i); + $crate::IResult::Done(i, ($i).slice(..index)) + }, + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i) } } ); - ($i:expr, $m:expr, $n: expr, $f:expr, $init:expr, $fold_f:expr) => ( - fold_many_m_n!($i, $m, $n, call!($f), $init, $fold_f); + ($i:expr, $f:expr) => ( + recognize!($i, call!($f)) ); ); + #[cfg(test)] mod tests { - use internal::{Needed,IResult,Err}; + use internal::{Needed,IResult}; + #[cfg(feature = "verbose-errors")] + use verbose_errors::Err; + + #[cfg(not(feature = "verbose-errors"))] + use simple_errors::Err; + use internal::IResult::*; - use internal::Err::*; use util::ErrorKind; // reproduce the tag and take macros, because of module import order @@ -2705,7 +1191,7 @@ mod tests { let b = &$bytes[..m]; let res: $crate::IResult<_,_> = if reduced != b { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::Tag, $i)) + $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) } else if m < blen { $crate::IResult::Incomplete($crate::Needed::Size(blen)) } else { @@ -2753,333 +1239,6 @@ mod tests { assert_eq!(b, 6); } - #[derive(PartialEq,Eq,Debug)] - struct B { - a: u8, - b: u8 - } - - #[test] - fn chain2() { - fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; - fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) }; - - named!(chain_parser<&[u8],B>, - chain!( - tag!("abcd") ~ - tag!("abcd")? ~ - aa: ret_int1 ~ - tag!("efgh") ~ - bb: ret_int2 ~ - tag!("efgh") , - ||{B{a: aa, b: bb}} - ) - ); - - assert_eq!(chain_parser(&b"abcdabcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); - assert_eq!(chain_parser(&b"abcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); - assert_eq!(chain_parser(&b"abcdab"[..]), Incomplete(Needed::Size(8))); - assert_eq!(chain_parser(&b"abcdefghef"[..]), Incomplete(Needed::Size(12))); - } - - #[test] - fn nested_chain() { - fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; - fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) }; - - named!(chain_parser<&[u8],B>, - chain!( - chain!( - tag!("abcd") ~ - tag!("abcd")? , - || {} - ) ~ - aa: ret_int1 ~ - tag!("efgh") ~ - bb: ret_int2 ~ - tag!("efgh") , - ||{B{a: aa, b: bb}} - ) - ); - - assert_eq!(chain_parser(&b"abcdabcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); - assert_eq!(chain_parser(&b"abcdefghefghX"[..]), Done(&b"X"[..], B{a: 1, b: 2})); - assert_eq!(chain_parser(&b"abcdab"[..]), Incomplete(Needed::Size(8))); - assert_eq!(chain_parser(&b"abcdefghef"[..]), Incomplete(Needed::Size(12))); - } - - #[derive(PartialEq,Eq,Debug)] - struct C { - a: u8, - b: Option - } - - #[test] - fn chain_mut() { - fn ret_b1_2(i:&[u8]) -> IResult<&[u8], B> { Done(i,B{a:1,b:2}) }; - named!(f<&[u8],B>, - chain!( - tag!("abcd") ~ - tag!("abcd")? ~ - tag!("efgh") ~ - mut bb: ret_b1_2 ~ - tag!("efgh") , - ||{ - bb.b = 3; - bb - } - ) - ); - - let r = f(&b"abcdabcdefghefghX"[..]); - assert_eq!(r, Done(&b"X"[..], B{a: 1, b: 3})); - } - - #[test] - fn chain_opt() { - named!(y, tag!("efgh")); - fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; - named!(ret_y<&[u8], u8>, map!(y, |_| 2)); - - named!(chain_parser<&[u8],C>, - chain!( - tag!("abcd") ~ - aa: ret_int1 ~ - bb: ret_y? , - ||{C{a: aa, b: bb}} - ) - ); - - assert_eq!(chain_parser(&b"abcdefghX"[..]), Done(&b"X"[..], C{a: 1, b: Some(2)})); - assert_eq!(chain_parser(&b"abcdWXYZ"[..]), Done(&b"WXYZ"[..], C{a: 1, b: None})); - assert_eq!(chain_parser(&b"abcdX"[..]), Done(&b"X"[..], C{ a: 1, b: None })); - assert_eq!(chain_parser(&b"abcdef"[..]), Incomplete(Needed::Size(8))); - } - - use util::{error_to_list, add_error_pattern, print_error}; - - fn error_to_string

(e: &Err

) -> &'static str { - let v:Vec = error_to_list(e); - // do it this way if you can use slice patterns - /* - match &v[..] { - [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag", - [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`", - _ => "unrecognized error" - } - */ - if &v[..] == [ErrorKind::Custom(42),ErrorKind::Tag] { - "missing `ijkl` tag" - } else if &v[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] { - "missing `mnop` tag after `ijkl`" - } else { - "unrecognized error" - } - } - - // do it this way if you can use box patterns - /*use std::str; - fn error_to_string(e:Err) -> String - match e { - NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => { - format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap()) - }, - NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => { - format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap()) - }, - _ => "unrecognized error".to_string() - } - }*/ - use std::collections; - #[test] - fn err() { - named!(err_test, alt!( - tag!("abcd") | - preceded!(tag!("efgh"), error!(ErrorKind::Custom(42), - chain!( - tag!("ijkl") ~ - res: error!(ErrorKind::Custom(128), tag!("mnop")) , - || { res } - ) - ) - ) - )); - let a = &b"efghblah"[..]; - let b = &b"efghijklblah"[..]; - let c = &b"efghijklmnop"[..]; - - let blah = &b"blah"[..]; - - let res_a = err_test(a); - let res_b = err_test(b); - let res_c = err_test(c); - assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah))))); - assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..], Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah))))))); - assert_eq!(res_c, Done(&b""[..], &b"mnop"[..])); - - // Merr-like error matching - let mut err_map = collections::HashMap::new(); - assert!(add_error_pattern(&mut err_map, err_test(&b"efghpouet"[..]), "missing `ijkl` tag")); - assert!(add_error_pattern(&mut err_map, err_test(&b"efghijklpouet"[..]), "missing `mnop` tag after `ijkl`")); - - let res_a2 = res_a.clone(); - match res_a { - Error(e) => { - assert_eq!(error_to_list(&e), [ErrorKind::Custom(42), ErrorKind::Tag]); - assert_eq!(error_to_string(&e), "missing `ijkl` tag"); - assert_eq!(err_map.get(&error_to_list(&e)), Some(&"missing `ijkl` tag")); - }, - _ => panic!() - }; - - let res_b2 = res_b.clone(); - match res_b { - Error(e) => { - assert_eq!(error_to_list(&e), [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag]); - assert_eq!(error_to_string(&e), "missing `mnop` tag after `ijkl`"); - assert_eq!(err_map.get(&error_to_list(&e)), Some(&"missing `mnop` tag after `ijkl`")); - }, - _ => panic!() - }; - - print_error(a, res_a2); - print_error(b, res_b2); - } - - #[test] - fn add_err() { - named!(err_test, - preceded!(tag!("efgh"), add_error!(ErrorKind::Custom(42), - chain!( - tag!("ijkl") ~ - res: add_error!(ErrorKind::Custom(128), tag!("mnop")) , - || { res } - ) - ) - )); - let a = &b"efghblah"[..]; - let b = &b"efghijklblah"[..]; - let c = &b"efghijklmnop"[..]; - - let blah = &b"blah"[..]; - - let res_a = err_test(a); - let res_b = err_test(b); - let res_c = err_test(c); - assert_eq!(res_a, Error(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah))))); - assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..], Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah))))))); - assert_eq!(res_c, Done(&b""[..], &b"mnop"[..])); - } - - #[test] - fn complete() { - named!(err_test, - chain!( - tag!("ijkl") ~ - res: complete!(tag!("mnop")) , - || { res } - ) - ); - let a = &b"ijklmn"[..]; - - let res_a = err_test(a); - assert_eq!(res_a, Error(Position(ErrorKind::Complete, &b"mn"[..]))); - } - #[test] - fn alt() { - fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { - Done(&b""[..], input) - } - - #[allow(unused_variables)] - fn dont_work(input: &[u8]) -> IResult<&[u8],&[u8],&'static str> { - Error(Code(ErrorKind::Custom("abcd"))) - } - - fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { - Done(input, &b""[..]) - } - - fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { - alt!(i, dont_work | dont_work) - } - fn alt2(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { - alt!(i, dont_work | work) - } - fn alt3(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { - alt!(i, dont_work | dont_work | work2 | dont_work) - } - //named!(alt1, alt!(dont_work | dont_work)); - //named!(alt2, alt!(dont_work | work)); - //named!(alt3, alt!(dont_work | dont_work | work2 | dont_work)); - - let a = &b"abcd"[..]; - assert_eq!(alt1(a), Error(Position(ErrorKind::Alt, a))); - assert_eq!(alt2(a), Done(&b""[..], a)); - assert_eq!(alt3(a), Done(a, &b""[..])); - - named!(alt4, alt!(tag!("abcd") | tag!("efgh"))); - let b = &b"efgh"[..]; - assert_eq!(alt4(a), Done(&b""[..], a)); - assert_eq!(alt4(b), Done(&b""[..], b)); - - // test the alternative syntax - named!(alt5, alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })); - assert_eq!(alt5(a), Done(&b""[..], false)); - assert_eq!(alt5(b), Done(&b""[..], true)); - - } - - #[test] - fn alt_incomplete() { - named!(alt1, alt!(tag!("a") | tag!("bc") | tag!("def"))); - - let a = &b""[..]; - assert_eq!(alt1(a), Incomplete(Needed::Size(1))); - let a = &b"b"[..]; - assert_eq!(alt1(a), Incomplete(Needed::Size(2))); - let a = &b"bcd"[..]; - assert_eq!(alt1(a), Done(&b"d"[..], &b"bc"[..])); - let a = &b"cde"[..]; - assert_eq!(alt1(a), Error(Position(ErrorKind::Alt, a))); - let a = &b"de"[..]; - assert_eq!(alt1(a), Incomplete(Needed::Size(3))); - let a = &b"defg"[..]; - assert_eq!(alt1(a), Done(&b"g"[..], &b"def"[..])); - } - - #[test] - fn alt_complete() { - named!(ac<&[u8], &[u8]>, - alt_complete!(tag!("abcd") | tag!("ef") | tag!("ghi") | tag!("kl")) - ); - - let a = &b""[..]; - assert_eq!(ac(a), Incomplete(Needed::Size(2))); - let a = &b"ef"[..]; - assert_eq!(ac(a), Done(&b""[..], &b"ef"[..])); - let a = &b"cde"[..]; - assert_eq!(ac(a), Error(Position(ErrorKind::Alt, a))); - } - - #[test] - fn switch() { - named!(sw, - switch!(take!(4), - b"abcd" => take!(2) | - b"efgh" => take!(4) - ) - ); - - let a = &b"abcdefgh"[..]; - assert_eq!(sw(a), Done(&b"gh"[..], &b"ef"[..])); - - let b = &b"efghijkl"[..]; - assert_eq!(sw(b), Done(&b""[..], &b"ijkl"[..])); - let c = &b"afghijkl"[..]; - assert_eq!(sw(c), Error(Position(ErrorKind::Switch, &b"afghijkl"[..]))); - } - #[test] fn opt() { named!(opt_abcd<&[u8],Option<&[u8]> >, opt!(tag!("abcd"))); @@ -3092,22 +1251,39 @@ mod tests { assert_eq!(opt_abcd(c), Incomplete(Needed::Size(4))); } + #[cfg(feature = "verbose-errors")] + #[test] + fn opt_res() { + named!(opt_res_abcd<&[u8], Result<&[u8], Err<&[u8]> > >, opt_res!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_res_abcd(a), Done(&b"ef"[..], Ok(&b"abcd"[..]))); + assert_eq!(opt_res_abcd(b), Done(&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, b)))); + assert_eq!(opt_res_abcd(c), Incomplete(Needed::Size(4))); + } + + #[cfg(not(feature = "verbose-errors"))] #[test] fn opt_res() { - named!(opt_res_abcd<&[u8], Result<&[u8], Err<&[u8]>> >, opt_res!(tag!("abcd"))); + named!(opt_res_abcd<&[u8], Result<&[u8], Err> >, opt_res!(tag!("abcd"))); let a = &b"abcdef"[..]; let b = &b"bcdefg"[..]; let c = &b"ab"[..]; assert_eq!(opt_res_abcd(a), Done(&b"ef"[..], Ok(&b"abcd"[..]))); - assert_eq!(opt_res_abcd(b), Done(&b"bcdefg"[..], Err(Position(ErrorKind::Tag, b)))); + assert_eq!(opt_res_abcd(b), Done(&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, b)))); assert_eq!(opt_res_abcd(c), Incomplete(Needed::Size(4))); } #[test] + #[cfg(feature = "std")] fn cond() { - let f_true: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( true, tag!("abcd") ) )); - let f_false: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( false, tag!("abcd") ) )); + let f_true: Box IResult<&[u8],Option<&[u8]>, &str>> = + Box::new(closure!(&'static [u8], fix_error!(&str, cond!( true, tag!("abcd") ) ))); + let f_false: Box IResult<&[u8],Option<&[u8]>, &str>> = + Box::new(closure!(&'static [u8], fix_error!(&str, cond!( false, tag!("abcd") ) ))); //let f_false = closure!(&'static [u8], cond!( false, tag!("abcd") ) ); assert_eq!(f_true(&b"abcdef"[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); @@ -3120,11 +1296,14 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn cond_wrapping() { // Test that cond!() will wrap a given identifier in the call!() macro. named!( tag_abcd, tag!("abcd") ); - let f_true: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( true, tag_abcd ) )); - let f_false: Box IResult<&[u8],Option<&[u8]>, &str>> = Box::new(closure!(&'static [u8], cond!( false, tag_abcd ) )); + let f_true: Box IResult<&[u8],Option<&[u8]>, &str>> = + Box::new(closure!(&'static [u8], fix_error!(&str, cond!( true, tag_abcd ) ))); + let f_false: Box IResult<&[u8],Option<&[u8]>, &str>> = + Box::new(closure!(&'static [u8], fix_error!(&str, cond!( false, tag_abcd ) ))); //let f_false = closure!(&'static [u8], cond!( b2, tag!("abcd") ) ); assert_eq!(f_true(&b"abcdef"[..]), Done(&b"ef"[..], Some(&b"abcd"[..]))); @@ -3142,420 +1321,22 @@ mod tests { assert_eq!(peek_tag(&b"abcdef"[..]), Done(&b"abcdef"[..], &b"abcd"[..])); assert_eq!(peek_tag(&b"ab"[..]), Incomplete(Needed::Size(4))); - assert_eq!(peek_tag(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - } - - #[test] - fn pair() { - named!( tag_abc, tag!("abc") ); - named!( tag_def, tag!("def") ); - named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) ); - - assert_eq!(pair_abc_def(&b"abcdefghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))); - assert_eq!(pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3))); - assert_eq!(pair_abc_def(&b"abcd"[..]), Incomplete(Needed::Size(6))); - assert_eq!(pair_abc_def(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - assert_eq!(pair_abc_def(&b"xxxdef"[..]), Error(Position(ErrorKind::Tag, &b"xxxdef"[..]))); - assert_eq!(pair_abc_def(&b"abcxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - } - - #[test] - fn separated_pair() { - named!( tag_abc, tag!("abc") ); - named!( tag_def, tag!("def") ); - named!( tag_separator, tag!(",") ); - named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); - - assert_eq!(sep_pair_abc_def(&b"abc,defghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))); - assert_eq!(sep_pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3))); - assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Incomplete(Needed::Size(7))); - assert_eq!(sep_pair_abc_def(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - assert_eq!(sep_pair_abc_def(&b"xxx,def"[..]), Error(Position(ErrorKind::Tag, &b"xxx,def"[..]))); - assert_eq!(sep_pair_abc_def(&b"abc,xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - } - - #[test] - fn preceded() { - named!( tag_abcd, tag!("abcd") ); - named!( tag_efgh, tag!("efgh") ); - named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); - - assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"efgh"[..])); - assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4))); - assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8))); - assert_eq!(preceded_abcd_efgh(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - assert_eq!(preceded_abcd_efgh(&b"xxxxdef"[..]), Error(Position(ErrorKind::Tag, &b"xxxxdef"[..]))); - assert_eq!(preceded_abcd_efgh(&b"abcdxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - } - - #[test] - fn terminated() { - named!( tag_abcd, tag!("abcd") ); - named!( tag_efgh, tag!("efgh") ); - named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); - - assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"abcd"[..])); - assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4))); - assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8))); - assert_eq!(terminated_abcd_efgh(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - assert_eq!(terminated_abcd_efgh(&b"xxxxdef"[..]), Error(Position(ErrorKind::Tag, &b"xxxxdef"[..]))); - assert_eq!(terminated_abcd_efgh(&b"abcdxxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxxx"[..]))); - } - - #[test] - fn delimited() { - named!( tag_abc, tag!("abc") ); - named!( tag_def, tag!("def") ); - named!( tag_ghi, tag!("ghi") ); - named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); - - assert_eq!(delimited_abc_def_ghi(&b"abcdefghijkl"[..]), Done(&b"jkl"[..], &b"def"[..])); - assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Incomplete(Needed::Size(3))); - assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Incomplete(Needed::Size(6))); - assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Incomplete(Needed::Size(9))); - assert_eq!(delimited_abc_def_ghi(&b"xxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - assert_eq!(delimited_abc_def_ghi(&b"xxxdefghi"[..]), Error(Position(ErrorKind::Tag, &b"xxxdefghi"[..]))); - assert_eq!(delimited_abc_def_ghi(&b"abcxxxghi"[..]), Error(Position(ErrorKind::Tag, &b"xxxghi"[..]))); - assert_eq!(delimited_abc_def_ghi(&b"abcdefxxx"[..]), Error(Position(ErrorKind::Tag, &b"xxx"[..]))); - } - - #[test] - fn separated_list() { - named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); - named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); - - let a = &b"abcdef"[..]; - let b = &b"abcd,abcdef"[..]; - let c = &b"azerty"[..]; - let d = &b",,abc"[..]; - let e = &b"abcd,abcd,ef"[..]; - - let res1 = vec![&b"abcd"[..]]; - assert_eq!(multi(a), Done(&b"ef"[..], res1)); - let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; - assert_eq!(multi(b), Done(&b"ef"[..], res2)); - assert_eq!(multi(c), Done(&b"azerty"[..], Vec::new())); - assert_eq!(multi_empty(d), Error(Position(ErrorKind::SeparatedList, d))); - //let res3 = vec![&b""[..], &b""[..], &b""[..]]; - //assert_eq!(multi_empty(d), Done(&b"abc"[..], res3)); - let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; - assert_eq!(multi(e), Done(&b",ef"[..], res4)); - } - - #[test] - fn separated_nonempty_list() { - named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); - - let a = &b"abcdef"[..]; - let b = &b"abcd,abcdef"[..]; - let c = &b"azerty"[..]; - let d = &b"abcd,abcd,ef"[..]; - - let res1 = vec![&b"abcd"[..]]; - assert_eq!(multi(a), Done(&b"ef"[..], res1)); - let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; - assert_eq!(multi(b), Done(&b"ef"[..], res2)); - assert_eq!(multi(c), Error(Position(ErrorKind::Tag,c))); - let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; - assert_eq!(multi(d), Done(&b",ef"[..], res3)); - } - - #[test] - fn many0() { - named!( tag_abcd, tag!("abcd") ); - named!( tag_empty, tag!("") ); - named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); - named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); - - assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); - assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); - assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); - assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); - assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); - assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); - assert_eq!(multi_empty(&b"abcdef"[..]), Error(Position(ErrorKind::Many0, &b"abcdef"[..]))); - } - - #[cfg(feature = "nightly")] - use test::Bencher; - - #[cfg(feature = "nightly")] - #[bench] - fn many0_bench(b: &mut Bencher) { - named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); - b.iter(|| { - multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..]) - }); - } - - #[test] - fn many1() { - named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); - - let a = &b"abcdef"[..]; - let b = &b"abcdabcdefgh"[..]; - let c = &b"azerty"[..]; - let d = &b"abcdab"[..]; - - let res1 = vec![&b"abcd"[..]]; - assert_eq!(multi(a), Done(&b"ef"[..], res1)); - let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; - assert_eq!(multi(b), Done(&b"efgh"[..], res2)); - assert_eq!(multi(c), Error(Position(ErrorKind::Many1,c))); - assert_eq!(multi(d), Incomplete(Needed::Size(8))); - } - - #[test] - fn infinite_many() { - fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { - println!("input: {:?}", input); - Error(Position(ErrorKind::Custom(0),input)) - } - - // should not go into an infinite loop - named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); - let a = &b"abcdef"[..]; - assert_eq!(multi0(a), Done(a, Vec::new())); - - named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); - let a = &b"abcdef"[..]; - assert_eq!(multi1(a), Error(Position(ErrorKind::Many1,a))); - } - - #[test] - fn many_m_n() { - named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); - - let a = &b"Abcdef"[..]; - let b = &b"AbcdAbcdefgh"[..]; - let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; - let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; - let e = &b"AbcdAb"[..]; - - assert_eq!(multi(a), Error(Err::Position(ErrorKind::ManyMN,a))); - let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; - assert_eq!(multi(b), Done(&b"efgh"[..], res1)); - let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; - assert_eq!(multi(c), Done(&b"efgh"[..], res2)); - let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; - assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); - assert_eq!(multi(e), Incomplete(Needed::Size(8))); - } - - #[test] - fn count() { - const TIMES: usize = 2; - named!( tag_abc, tag!("abc") ); - named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); - - assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]])); - assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Unknown)); - assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Unknown)); - assert_eq!(cnt_2(&b"xxx"[..]), Error(Position(ErrorKind::Count, &b"xxx"[..]))); - assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(Position(ErrorKind::Count, &b"xxxabcabcdef"[..]))); - assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(Position(ErrorKind::Count, &b"abcxxxabcdef"[..]))); - } - - #[test] - fn count_zero() { - const TIMES: usize = 0; - named!( tag_abc, tag!("abc") ); - named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); - - let done = &b"abcabcabcdef"[..]; - let parsed_done = Vec::new(); - let rest = done; - let incomplete_1 = &b"ab"[..]; - let parsed_incompl_1 = Vec::new(); - let incomplete_2 = &b"abcab"[..]; - let parsed_incompl_2 = Vec::new(); - let error = &b"xxx"[..]; - let error_remain = &b"xxx"[..]; - let parsed_err = Vec::new(); - let error_1 = &b"xxxabcabcdef"[..]; - let parsed_err_1 = Vec::new(); - let error_1_remain = &b"xxxabcabcdef"[..]; - let error_2 = &b"abcxxxabcdef"[..]; - let parsed_err_2 = Vec::new(); - let error_2_remain = &b"abcxxxabcdef"[..]; - - assert_eq!(counter_2(done), Done(rest, parsed_done)); - assert_eq!(counter_2(incomplete_1), Done(incomplete_1, parsed_incompl_1)); - assert_eq!(counter_2(incomplete_2), Done(incomplete_2, parsed_incompl_2)); - assert_eq!(counter_2(error), Done(error_remain, parsed_err)); - assert_eq!(counter_2(error_1), Done(error_1_remain, parsed_err_1)); - assert_eq!(counter_2(error_2), Done(error_2_remain, parsed_err_2)); - } - - #[test] - fn count_fixed() { - const TIMES: usize = 2; - named!( tag_abc, tag!("abc") ); - named!( cnt_2<&[u8], [&[u8]; TIMES] >, count_fixed!(&[u8], tag_abc, TIMES ) ); - - assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], [&b"abc"[..], &b"abc"[..]])); - assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Unknown)); - assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Unknown)); - assert_eq!(cnt_2(&b"xxx"[..]), Error(Position(ErrorKind::Count, &b"xxx"[..]))); - assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(Position(ErrorKind::Count, &b"xxxabcabcdef"[..]))); - assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(Position(ErrorKind::Count, &b"abcxxxabcdef"[..]))); - } - - use nom::{le_u16,eof}; - #[allow(dead_code)] - pub fn compile_count_fixed(input: &[u8]) -> IResult<&[u8], ()> { - chain!(input, - tag!("abcd") ~ - count_fixed!( u16, le_u16, 4 ) ~ - eof , - || { () } - ) - } - - #[test] - fn count_fixed_no_type() { - const TIMES: usize = 2; - named!( tag_abc, tag!("abc") ); - named!( counter_2<&[u8], [&[u8]; TIMES], () >, count_fixed!(&[u8], tag_abc, TIMES ) ); - - let done = &b"abcabcabcdef"[..]; - let parsed_main = [&b"abc"[..], &b"abc"[..]]; - let rest = &b"abcdef"[..]; - let incomplete_1 = &b"ab"[..]; - let incomplete_2 = &b"abcab"[..]; - let error = &b"xxx"[..]; - let error_1 = &b"xxxabcabcdef"[..]; - let error_1_remain = &b"xxxabcabcdef"[..]; - let error_2 = &b"abcxxxabcdef"[..]; - let error_2_remain = &b"abcxxxabcdef"[..]; - - assert_eq!(counter_2(done), Done(rest, parsed_main)); - assert_eq!(counter_2(incomplete_1), Incomplete(Needed::Unknown)); - assert_eq!(counter_2(incomplete_2), Incomplete(Needed::Unknown)); - assert_eq!(counter_2(error), Error(Position(ErrorKind::Count, error))); - assert_eq!(counter_2(error_1), Error(Position(ErrorKind::Count, error_1_remain))); - assert_eq!(counter_2(error_2), Error(Position(ErrorKind::Count, error_2_remain))); - } - - use nom::{be_u8,be_u16}; - #[test] - fn length_value_test() { - named!(length_value_1<&[u8], Vec >, length_value!(be_u8, be_u16)); - named!(length_value_2<&[u8], Vec >, length_value!(be_u8, be_u16, 2)); - - let i1 = vec![0, 5, 6]; - assert_eq!(length_value_1(&i1), IResult::Done(&i1[1..], vec![])); - assert_eq!(length_value_2(&i1), IResult::Done(&i1[1..], vec![])); - - let i2 = vec![1, 5, 6, 3]; - assert_eq!(length_value_1(&i2), IResult::Done(&i2[3..], vec![1286])); - assert_eq!(length_value_2(&i2), IResult::Done(&i2[3..], vec![1286])); - - let i3 = vec![2, 5, 6, 3, 4, 5, 7]; - assert_eq!(length_value_1(&i3), IResult::Done(&i3[5..], vec![1286, 772])); - assert_eq!(length_value_2(&i3), IResult::Done(&i3[5..], vec![1286, 772])); - - let i4 = vec![2, 5, 6, 3]; - assert_eq!(length_value_1(&i4), IResult::Incomplete(Needed::Size(5))); - assert_eq!(length_value_2(&i4), IResult::Incomplete(Needed::Size(5))); - - let i5 = vec![3, 5, 6, 3, 4, 5]; - assert_eq!(length_value_1(&i5), IResult::Incomplete(Needed::Size(7))); - assert_eq!(length_value_2(&i5), IResult::Incomplete(Needed::Size(7))); - } - - #[test] - fn fold_many0() { - fn fold_into_vec(mut acc: Vec, item: T) -> Vec { - acc.push(item); - acc - }; - named!( tag_abcd, tag!("abcd") ); - named!( tag_empty, tag!("") ); - named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); - named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); - - assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); - assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); - assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); - assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); - assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); - assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); - assert_eq!(multi_empty(&b"abcdef"[..]), Error(Position(ErrorKind::Many0, &b"abcdef"[..]))); - } - - #[test] - fn fold_many1() { - fn fold_into_vec(mut acc: Vec, item: T) -> Vec { - acc.push(item); - acc - }; - named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); - - let a = &b"abcdef"[..]; - let b = &b"abcdabcdefgh"[..]; - let c = &b"azerty"[..]; - let d = &b"abcdab"[..]; - - let res1 = vec![&b"abcd"[..]]; - assert_eq!(multi(a), Done(&b"ef"[..], res1)); - let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; - assert_eq!(multi(b), Done(&b"efgh"[..], res2)); - assert_eq!(multi(c), Error(Position(ErrorKind::Many1,c))); - assert_eq!(multi(d), Incomplete(Needed::Size(8))); - } - - #[test] - fn fold_many_m_n() { - fn fold_into_vec(mut acc: Vec, item: T) -> Vec { - acc.push(item); - acc - }; - named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); - - let a = &b"Abcdef"[..]; - let b = &b"AbcdAbcdefgh"[..]; - let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; - let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; - let e = &b"AbcdAb"[..]; - - assert_eq!(multi(a), Error(Err::Position(ErrorKind::ManyMN,a))); - let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; - assert_eq!(multi(b), Done(&b"efgh"[..], res1)); - let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; - assert_eq!(multi(c), Done(&b"efgh"[..], res2)); - let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; - assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); - assert_eq!(multi(e), Incomplete(Needed::Size(8))); - } - - #[test] - fn chain_incomplete() { - let res = chain!(&b"abcdefgh"[..], - a: take!(4) ~ - b: take!(8), - ||{(a,b )} - ); - - assert_eq!(res, IResult::Incomplete(Needed::Size(12))); + assert_eq!(peek_tag(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); } - #[test] - fn tuple_test() { - named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, - tuple!( be_u16 , take!(3), tag!("fg") ) ); - - assert_eq!(tuple_3(&b"abcdefgh"[..]), Done(&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..]))); - assert_eq!(tuple_3(&b"abcd"[..]), Incomplete(Needed::Size(5))); - assert_eq!(tuple_3(&b"abcde"[..]), Incomplete(Needed::Size(7))); - assert_eq!(tuple_3(&b"abcdejk"[..]), Error(Position(ErrorKind::Tag, &b"jk"[..]))); - } - #[test] fn not() { named!(not_aaa, not!(tag!("aaa"))); - assert_eq!(not_aaa(&b"aaa"[..]), Error(Position(ErrorKind::Not, &b"aaa"[..]))); + assert_eq!(not_aaa(&b"aaa"[..]), Error(error_position!(ErrorKind::Not, &b"aaa"[..]))); assert_eq!(not_aaa(&b"aa"[..]), Done(&b"aa"[..], &b""[..])); assert_eq!(not_aaa(&b"abcd"[..]), Done(&b"abcd"[..], &b""[..])); } + + #[test] + fn verify() { + named!(test, verify!(take!(5), |slice: &[u8]| slice[0] == 'a' as u8)); + assert_eq!(test(&b"bcd"[..]), Incomplete(Needed::Size(5))); + assert_eq!(test(&b"bcdefg"[..]), Error(error_position!(ErrorKind::Verify, &b"bcdefg"[..]))); + assert_eq!(test(&b"abcdefg"[..]), Done(&b"fg"[..], &b"abcde"[..])); + } } diff --git third_party/rust/nom/src/methods.rs third_party/rust/nom/src/methods.rs index 22868541a96c..7bdabfd063f3 100644 --- third_party/rust/nom/src/methods.rs +++ third_party/rust/nom/src/methods.rs @@ -77,12 +77,12 @@ //! counterparts: //! ```ignore //! method!(pub simple_chain<&mut Parser<'a>, &'a str, &'a str>, self, -//! chain!( -//! call_m!(self.tag_abc) ~ -//! call_m!(self.tag_def) ~ -//! call_m!(self.tag_ghi) ~ -//! last: call_m!(self.simple_peek) , -//! ||{sb.parsed = last; last} +//! do_parse!( +//! call_m!(self.tag_abc) >> +//! call_m!(self.tag_def) >> +//! call_m!(self.tag_ghi) >> +//! last: map!(call_m!(self.simple_peek), |parsed| sb.parsed = parsed) >> +//! (last) //! ) //! ); //! ``` @@ -107,30 +107,35 @@ macro_rules! method ( // Non-public immutable self ($name:ident<$a:ty>( $i:ty ) -> $o:ty, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty,$i:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( - fn $name<'a>( $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + #[allow(unused_variables)] + fn $name( $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], $o, u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { let result = $submac!(i, $($args)*); ($self_, result) @@ -138,30 +143,35 @@ macro_rules! method ( ); // Public immutable self (pub $name:ident<$a:ty>( $i:ty ) -> $o:ty, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty,$i:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( $self_: $a,i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty,$o:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( - pub fn $name<'a>( $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + #[allow(unused_variables)] + pub fn $name( $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], $o, u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty>, $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { let result = $submac!(i, $($args)*); ($self_, result) @@ -169,30 +179,35 @@ macro_rules! method ( ); // Non-public mutable self ($name:ident<$a:ty>( $i:ty ) -> $o:ty, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty,$i:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( - fn $name<'a>( mut $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + #[allow(unused_variables)] + fn $name( mut $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], $o, u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); ($name:ident<$a:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( mut $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { let result = $submac!(i, $($args)*); ($self_, result) @@ -200,30 +215,35 @@ macro_rules! method ( ); // Public mutable self (pub $name:ident<$a:ty>( $i:ty ) -> $o:ty, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty,$i:ty,$o:ty,$e:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] fn $name( mut $self_: $a, i: $i ) -> ($a, $crate::IResult<$i, $o, $e>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty,$i:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( mut $self_: $a,i: $i ) -> ($a, $crate::IResult<$i,$o,u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty,$o:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( - pub fn $name<'a>( mut $self_: $a, i: &'a[u8] ) -> ($a, $crate::IResult<&'a [u8], $o, u32>) { + #[allow(unused_variables)] + pub fn $name( mut $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], $o, u32>) { let result = $submac!(i, $($args)*); ($self_, result) } ); (pub $name:ident<$a:ty>, mut $self_:ident, $submac:ident!( $($args:tt)* )) => ( + #[allow(unused_variables)] pub fn $name( mut $self_: $a, i: &[u8] ) -> ($a, $crate::IResult<&[u8], &[u8], u32>) { let result = $submac!(i, $($args)*); ($self_, result) @@ -251,8 +271,8 @@ macro_rules! call_m ( ); -/// emulate function currying for method calls on structs -/// `apply!(self.my_function, arg1, arg2, ...)` becomes `self.my_function(input, arg1, arg2, ...)` +/// emulate function currying for method calls on structs +/// `apply_m!(self.my_function, arg1, arg2, ...)` becomes `self.my_function(input, arg1, arg2, ...)` /// /// Supports up to 6 arguments #[macro_export] @@ -274,7 +294,7 @@ mod tests { } else if ($i).starts_with($tag) { $crate::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()]) } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagStr, $i)) + $crate::IResult::Error(error_position!($crate::ErrorKind::TagStr, $i)) }; res } @@ -325,10 +345,10 @@ mod tests { peek!(call_m!(self.take3)) ); method!(pub simple_chain, &'a str, &'a str>, mut self, - chain!( - bcd: call_m!(self.tag_bcd) ~ - last: call_m!(self.simple_peek) , - ||{self.bcd = bcd; last} + do_parse!( + map!(call_m!(self.tag_bcd), |bcd| self.bcd = bcd) >> + last: call_m!(self.simple_peek) >> + (last) ) ); fn tag_stuff(mut self: Parser<'a>, input: &'a str, something: &'a str) -> (Parser<'a>, ::IResult<&'a str, &'a str>) { diff --git third_party/rust/nom/src/multi.rs third_party/rust/nom/src/multi.rs new file mode 100644 index 000000000000..f42164dcf2e3 --- /dev/null +++ third_party/rust/nom/src/multi.rs @@ -0,0 +1,1614 @@ +//! Parsers for applying parsers multiple times + +/// `separated_list!(I -> IResult, I -> IResult) => I -> IResult>` +/// separated_list(sep, X) returns Vec will return Incomplete if there may be more elements +#[macro_export] +macro_rules! separated_list( + ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( + { + use $crate::InputLength; + + //FIXME: use crate vec + let mut res = ::std::vec::Vec::new(); + let mut input = $i.clone(); + + // get the first element + let input_ = input.clone(); + match $submac!(input_, $($args2)*) { + $crate::IResult::Error(_) => $crate::IResult::Done(input, ::std::vec::Vec::new()), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i,o) => { + if i.input_len() == input.input_len() { + $crate::IResult::Error(error_position!($crate::ErrorKind::SeparatedList,input)) + } else { + res.push(o); + input = i; + + let ret; + + loop { + // get the separator first + let input_ = input.clone(); + match $sep!(input_, $($args)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); + break; + } + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(needed)) => { + let (size,overflowed) = needed.overflowing_add(($i).input_len() - input.input_len()); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i2,_) => { + let i2_len = i2.input_len(); + if i2_len == input.input_len() { + ret = $crate::IResult::Done(input, res); + break; + } + + // get the element next + match $submac!(i2, $($args2)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(needed)) => { + let (size,overflowed) = needed.overflowing_add(($i).input_len() - i2_len); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i3,o3) => { + if i3.input_len() == i2_len { + ret = $crate::IResult::Done(input, res); + break; + } + res.push(o3); + input = i3; + } + } + } + } + } + + ret + } + }, + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list!($i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + separated_list!($i, call!($f), call!($g)); + ); +); + +/// `separated_nonempty_list!(I -> IResult, I -> IResult) => I -> IResult>` +/// separated_nonempty_list(sep, X) returns Vec will return Incomplete if there may be more elements +#[macro_export] +macro_rules! separated_nonempty_list( + ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( + { + use $crate::InputLength; + + let mut res = ::std::vec::Vec::new(); + let mut input = $i.clone(); + + // get the first element + let input_ = input.clone(); + match $submac!(input_, $($args2)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i,o) => { + if i.input_len() == input.len() { + $crate::IResult::Error(error_position!($crate::ErrorKind::SeparatedNonEmptyList,input)) + } else { + res.push(o); + input = i; + + let ret; + + loop { + // get the separator first + let input_ = input.clone(); + match $sep!(input_, $($args)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); + break; + } + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(needed)) => { + let (size,overflowed) = needed.overflowing_add(($i).input_len() - input.input_len()); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i2,_) => { + let i2_len = i2.input_len(); + if i2_len == input.input_len() { + ret = $crate::IResult::Done(input, res); + break; + } + + // get the element next + match $submac!(i2, $($args2)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(needed)) => { + let (size,overflowed) = needed.overflowing_add(($i).input_len() - i2_len); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i3,o3) => { + if i3.input_len() == i2_len { + ret = $crate::IResult::Done(input, res); + break; + } + res.push(o3); + input = i3; + } + } + } + } + } + + ret + } + }, + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_nonempty_list!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_nonempty_list!($i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + separated_nonempty_list!($i, call!($f), call!($g)); + ); +); + +/// `separated_list_complete!(I -> IResult, I -> IResult) => I -> IResult>` +/// This is equivalent to the `separated_list!` combinator, except that it will return `Error` +/// when either the separator or element subparser returns `Incomplete`. +#[macro_export] +macro_rules! separated_list_complete { + ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ({ + separated_list!($i, complete!($sep!($($args)*)), complete!($submac!($($args2)*))) + }); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list_complete!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list_complete!($i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + separated_list_complete!($i, call!($f), call!($g)); + ); +} + +/// `separated_nonempty_list_complete!(I -> IResult, I -> IResult) => I -> IResult>` +/// This is equivalent to the `separated_nonempty_list!` combinator, except that it will return +/// `Error` when either the separator or element subparser returns `Incomplete`. +#[macro_export] +macro_rules! separated_nonempty_list_complete { + ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ({ + separated_nonempty_list!($i, complete!($sep!($($args)*)), complete!($submac!($($args2)*))) + }); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_nonempty_list_complete!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_nonempty_list_complete!($i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + separated_nonempty_list_complete!($i, call!($f), call!($g)); + ); +} + +/// `many0!(I -> IResult) => I -> IResult>` +/// Applies the parser 0 or more times and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); +/// # } +/// ``` +/// 0 or more +#[macro_export] +macro_rules! many0( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + + let ret; + let mut res = ::std::vec::Vec::new(); + let mut input = $i.clone(); + + loop { + if input.input_len() == 0 { + ret = $crate::IResult::Done(input, res); + break; + } + + let input_ = input.clone(); + match $submac!(input_, $($args)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i, o) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + ret = $crate::IResult::Error(error_position!($crate::ErrorKind::Many0,input)); + break; + } + + res.push(o); + input = i; + } + } + } + + ret + } + ); + ($i:expr, $f:expr) => ( + many0!($i, call!($f)); + ); +); + +/// `many1!(I -> IResult) => I -> IResult>` +/// Applies the parser 1 or more times and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Error(error_position!(ErrorKind::Many1,&b[..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! many1( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Error(_) => $crate::IResult::Error( + error_position!($crate::ErrorKind::Many1,$i) + ), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1,o1) => { + if i1.input_len() == 0 { + let mut res = ::std::vec::Vec::new(); + res.push(o1); + $crate::IResult::Done(i1,res) + } else { + + let mut res = ::std::vec::Vec::with_capacity(4); + res.push(o1); + let mut input = i1; + let mut incomplete: ::std::option::Option<$crate::Needed> = + ::std::option::Option::None; + loop { + if input.input_len() == 0 { + break; + } + let input_ = input.clone(); + match $submac!(input_, $($args)*) { + $crate::IResult::Error(_) => { + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); + incomplete = ::std::option::Option::Some( + match overflowed { + true => $crate::Needed::Unknown, + false => $crate::Needed::Size(size), + } + ); + break; + }, + $crate::IResult::Done(i, o) => { + if i.input_len() == input.input_len() { + break; + } + res.push(o); + input = i; + } + } + } + + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, res) + } + } + } + } + } + ); + ($i:expr, $f:expr) => ( + many1!($i, call!($f)); + ); +); + +/// `many_till!(I -> IResult, I -> IResult) => I -> IResult, P)>` +/// Applies the first parser until the second applies. Returns a tuple containing the list +/// of results from the first in a Vec and the result of the second. +/// +/// The first embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); +/// +/// let a = b"abcdabcdefghabcd"; +/// let b = b"efghabcd"; +/// let c = b"azerty"; +/// +/// let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); +/// let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); +/// assert_eq!(multi(&a[..]), Done(&b"abcd"[..], res_a)); +/// assert_eq!(multi(&b[..]), Done(&b"abcd"[..], res_b)); +/// assert_eq!(multi(&c[..]), Error(error_node_position!(ErrorKind::ManyTill,&c[..],error_position!(ErrorKind::Tag,&c[..])))); +/// # } +/// ``` +#[macro_export] +macro_rules! many_till( + ($i:expr, $submac1:ident!( $($args1:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + use $crate::InputLength; + + let ret; + let mut res = ::std::vec::Vec::new(); + let mut input = $i.clone(); + + loop { + match $submac2!(input, $($args2)*) { + $crate::IResult::Done(i, o) => { + ret = $crate::IResult::Done(i, (res, o)); + break; + }, + _ => { + match $submac1!(input, $($args1)*) { + $crate::IResult::Error(err) => { + ret = $crate::IResult::Error(error_node_position!($crate::ErrorKind::ManyTill,input, err)); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i, o) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + ret = $crate::IResult::Error(error_position!($crate::ErrorKind::ManyTill,input)); + break; + } + + res.push(o); + input = i; + }, + } + }, + } + } + + ret + } + ); + ($i:expr, $f:expr, $g: expr) => ( + many_till!($i, call!($f), call!($g)); + ); +); + +/// `many_m_n!(usize, usize, I -> IResult) => I -> IResult>` +/// Applies the parser between m and n times (n included) and returns the list of +/// results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]),Error(error_position!(ErrorKind::ManyMN,&a[..]))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); +/// # } +/// ``` +#[macro_export] +macro_rules! many_m_n( + ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::InputLength; + let mut res = ::std::vec::Vec::with_capacity($m); + let mut input = $i.clone(); + let mut count: usize = 0; + let mut err = false; + let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; + loop { + if count == $n { break } + let i_ = input.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Done(i, o) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i.input_len() == input.input_len() { + break; + } + res.push(o); + input = i; + count += 1; + } + $crate::IResult::Error(_) => { + err = true; + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add($i.input_len() - input.input_len()); + incomplete = ::std::option::Option::Some( + match overflowed { + true => $crate::Needed::Unknown, + false => $crate::Needed::Size(size), + } + ); + break; + }, + } + if input.input_len() == 0 { + break; + } + } + + if count < $m { + if err { + $crate::IResult::Error(error_position!($crate::ErrorKind::ManyMN,$i)) + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Incomplete( + $crate::Needed::Unknown + ) + } + } + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, res) + } + } + } + ); + ($i:expr, $m:expr, $n: expr, $f:expr) => ( + many_m_n!($i, $m, $n, call!($f)); + ); +); + +/// `count!(I -> IResult, nb) => I -> IResult>` +/// Applies the child parser a specified number of times +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); +/// +/// let a = b"abcdabcdabcdef"; +/// let b = b"abcdefgh"; +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// +/// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); +/// assert_eq!(counter(&b[..]), Error(error_position!(ErrorKind::Count, &b[..]))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! count( + ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( + { + let ret: $crate::IResult<_,_>; + let mut input = $i.clone(); + let mut res = ::std::vec::Vec::new(); + + loop { + if res.len() == $count { + ret = $crate::IResult::Done(input, res); + break; + } + + let input_ = input.clone(); + match $submac!(input_, $($args)*) { + $crate::IResult::Done(i,o) => { + res.push(o); + input = i; + }, + $crate::IResult::Error(_) => { + ret = $crate::IResult::Error(error_position!($crate::ErrorKind::Count,$i)); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + } + $crate::IResult::Incomplete($crate::Needed::Size(sz)) => { + let (size,overflowed) = sz.overflowing_add( + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input) + ); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + } + } + } + + ret + } + ); + ($i:expr, $f:expr, $count: expr) => ( + count!($i, call!($f), $count); + ); +); + +/// `count_fixed!(O, I -> IResult, nb) => I -> IResult` +/// Applies the child parser a fixed number of times and returns a fixed size array +/// The type must be specified and it must be `Copy` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done,Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], tag!( "abcd" ), 2 ) ); +/// // can omit the type specifier if returning slices +/// // named!(counter< [&[u8]; 2] >, count_fixed!( tag!( "abcd" ), 2 ) ); +/// +/// let a = b"abcdabcdabcdef"; +/// let b = b"abcdefgh"; +/// let res = [&b"abcd"[..], &b"abcd"[..]]; +/// +/// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); +/// assert_eq!(counter(&b[..]), Error(error_position!(ErrorKind::Count, &b[..]))); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! count_fixed ( + ($i:expr, $typ:ty, $submac:ident!( $($args:tt)* ), $count: expr) => ( + { + let ret; + let mut input = $i.clone(); + // `$typ` must be Copy, and thus having no destructor, this is panic safe + let mut res: [$typ; $count] = unsafe{[::std::mem::uninitialized(); $count as usize]}; + let mut cnt: usize = 0; + + loop { + if cnt == $count { + ret = $crate::IResult::Done(input, res); break; + } + + match $submac!(input, $($args)*) { + $crate::IResult::Done(i,o) => { + res[cnt] = o; + cnt += 1; + input = i; + }, + $crate::IResult::Error(_) => { + ret = $crate::IResult::Error(error_position!($crate::ErrorKind::Count,$i)); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + } + $crate::IResult::Incomplete($crate::Needed::Size(sz)) => { + let (size,overflowed) = sz.overflowing_add( + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input) + ); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + } + } + } + + ret + } +); + ($i:expr, $typ: ty, $f:ident, $count: expr) => ( + count_fixed!($i, $typ, call!($f), $count); + ); +); + +/// `length_count!(I -> IResult, I -> IResult) => I -> IResult>` +/// gets a number from the first parser, then applies the second parser that many times +#[macro_export] +macro_rules! length_count( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + match count!(i, $submac2!($($args2)*), o as usize) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(n)) => { + let (size,overflowed) = n.overflowing_add( + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i) + ); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + } + }, + $crate::IResult::Done(i2, o2) => $crate::IResult::Done(i2, o2) + } + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + length_count!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + length_count!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + length_count!($i, call!($f), call!($g)); + ); +); + +/// `length_data!(I -> IResult) => O` +/// +/// `length_data` gets a number from the first parser, than takes a subslice of the input +/// of that size, and returns that subslice +#[macro_export] +macro_rules! length_data( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + match take!(i, o as usize) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(n)) => { + let (size,overflowed) = n.overflowing_add( + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i) + ); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + } + }, + $crate::IResult::Done(i2, o2) => $crate::IResult::Done(i2, o2) + } + } + } + ); + + ($i:expr, $f:expr) => ( + length_data!($i, call!($f)); + ); +); + +/// `length_value!(I -> IResult, I -> IResult) => I -> IResult>` +/// gets a number from the first parser, takes a subslice of the input of that size, +/// then applies the second parser on that subslice. If the second parser returns +/// `Incomplete`, `length_value` will return an error +#[macro_export] +macro_rules! length_value( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + match take!(i, o as usize) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(n)) => { + let (size,overflowed) = n.overflowing_add( + $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i) + ); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + } + }, + $crate::IResult::Done(i2, o2) => { + match complete!(o2, $submac2!($($args2)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(_, o3) => $crate::IResult::Done(i2, o3) + } + } + } + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + length_value!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + length_value!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + length_value!($i, call!($f), call!($g)); + ); +); + +/// `fold_many0!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser 0 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); +/// # } +/// ``` +/// 0 or more +#[macro_export] +macro_rules! fold_many0( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( + { + use $crate::InputLength; + let ret; + let f = $f; + let mut res = $init; + let mut input = $i.clone(); + + loop { + if input.input_len() == 0 { + ret = $crate::IResult::Done(input, res); + break; + } + + match $submac!(input, $($args)*) { + $crate::IResult::Error(_) => { + ret = $crate::IResult::Done(input, res); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + ret = $crate::IResult::Incomplete($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add( ($i).input_len() - input.input_len() ); + ret = match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(size)), + }; + break; + }, + $crate::IResult::Done(i, o) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + ret = $crate::IResult::Error( + error_position!($crate::ErrorKind::Many0,input) + ); + break; + } + + res = f(res, o); + input = i; + } + } + } + + ret + } + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many0!($i, call!($f), $init, $fold_f); + ); +); + +/// `fold_many1!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser 1 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); +/// assert_eq!(multi(&b[..]), Error(error_position!(ErrorKind::Many1,&b[..]))); +/// # } +/// ``` +#[macro_export] +macro_rules! fold_many1( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( + { + use $crate::InputLength; + match $submac!($i, $($args)*) { + $crate::IResult::Error(_) => $crate::IResult::Error( + error_position!($crate::ErrorKind::Many1,$i) + ), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1,o1) => { + let acc = $init; + let f = $f; + if i1.input_len() == 0 { + let acc = f(acc, o1); + $crate::IResult::Done(i1,acc) + } else { + let mut acc = f(acc, o1); + let mut input = i1; + let mut incomplete: ::std::option::Option<$crate::Needed> = + ::std::option::Option::None; + loop { + if input.input_len() == 0 { + break; + } + match $submac!(input, $($args)*) { + $crate::IResult::Error(_) => { + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add( ($i).input_len() - input.input_len() ); + incomplete = ::std::option::Option::Some( + match overflowed { + true => $crate::Needed::Unknown, + false => $crate::Needed::Size(size), + } + ); + break; + }, + $crate::IResult::Done(i, o) => { + if i.input_len() == input.input_len() { + break; + } + acc = f(acc, o); + input = i; + } + } + } + + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, acc) + } + } + } + } + } + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many1!($i, call!($f), $init, $fold_f); + ); +); + +/// `fold_many_m_n!(usize, usize, I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser between m and n times (n included) and folds the list of return value +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{Done, Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]),Error(error_position!(ErrorKind::ManyMN,&a[..]))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); +/// # } +/// ``` +#[macro_export] +macro_rules! fold_many_m_n( + ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( + { + use $crate::InputLength; + let mut acc = $init; + let f = $f; + let mut input = $i.clone(); + let mut count: usize = 0; + let mut err = false; + let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; + loop { + if count == $n { break } + match $submac!(input, $($args)*) { + $crate::IResult::Done(i, o) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i.input_len() == input.input_len() { + break; + } + acc = f(acc, o); + input = i; + count += 1; + } + $crate::IResult::Error(_) => { + err = true; + break; + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => { + incomplete = ::std::option::Option::Some($crate::Needed::Unknown); + break; + }, + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (size,overflowed) = i.overflowing_add( ($i).input_len() - input.input_len() ); + incomplete = ::std::option::Option::Some( + match overflowed { + true => $crate::Needed::Unknown, + false => $crate::Needed::Size(size), + } + ); + break; + }, + } + if input.input_len() == 0 { + break; + } + } + + if count < $m { + if err { + $crate::IResult::Error(error_position!($crate::ErrorKind::ManyMN,$i)) + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Incomplete($crate::Needed::Unknown) + } + } + } else { + match incomplete { + ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), + ::std::option::Option::None => $crate::IResult::Done(input, acc) + } + } + } + ); + ($i:expr, $m:expr, $n: expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many_m_n!($i, $m, $n, call!($f), $init, $fold_f); + ); +); + +#[cfg(test)] +mod tests { + use internal::{Needed,IResult}; + + use internal::IResult::*; + use util::ErrorKind; + use nom::{alpha,be_u8,be_u16,le_u16,digit}; + use std::str::{self,FromStr}; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use std::cmp::min; + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: $crate::IResult<_,_> = if reduced != b { + $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) + } else if m < blen { + $crate::IResult::Incomplete($crate::Needed::Size(blen)) + } else { + $crate::IResult::Done(&$i[blen..], reduced) + }; + res + } + ); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) + }; + res + } + ) + ); + + #[test] + #[cfg(feature = "std")] + fn separated_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); + named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); + named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_list!(tag!(".."), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b",,abc"[..]; + let e = &b"abcd,abcd,ef"[..]; + let f = &b"abc"[..]; + let g = &b"abcd."[..]; + let h = &b"abcd,abc"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"ef"[..], res2)); + assert_eq!(multi(c), Done(&b"azerty"[..], Vec::new())); + assert_eq!(multi_empty(d), Error(error_position!(ErrorKind::SeparatedList, d))); + //let res3 = vec![&b""[..], &b""[..], &b""[..]]; + //assert_eq!(multi_empty(d), Done(&b"abc"[..], res3)); + let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(e), Done(&b",ef"[..], res4)); + + assert_eq!(multi(f), Incomplete(Needed::Size(4))); + assert_eq!(multi_longsep(g), Incomplete(Needed::Size(6))); + assert_eq!(multi(h), Incomplete(Needed::Size(9))); + } + + #[test] + #[cfg(feature = "std")] + fn separated_list_complete() { + named!(multi<&[u8],Vec<&[u8]> >, separated_list_complete!(tag!(","), alpha)); + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"abcd,abcd,ef"[..]; + let d = &b"abc."[..]; + let e = &b"abcd,ef."[..]; + let f = &b"123"[..]; + + assert_eq!(multi(a), Done(&b""[..], vec!(a))); + assert_eq!(multi(b), Done(&b""[..], vec!(&b"abcd"[..], &b"abcdef"[..]))); + assert_eq!(multi(c), Done(&b""[..], vec!(&b"abcd"[..], &b"abcd"[..], &b"ef"[..]))); + assert_eq!(multi(d), Done(&b"."[..], vec!(&b"abc"[..]))); + assert_eq!(multi(e), Done(&b"."[..], vec!(&b"abcd"[..], &b"ef"[..]))); + assert_eq!(multi(f), Done(&b"123"[..], Vec::new())); + } + + + #[test] + #[cfg(feature = "std")] + fn separated_nonempty_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); + named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(".."), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b"abcd,abcd,ef"[..]; + + let f = &b"abc"[..]; + let g = &b"abcd."[..]; + let h = &b"abcd,abc"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"ef"[..], res2)); + assert_eq!(multi(c), Error(error_position!(ErrorKind::Tag,c))); + let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(d), Done(&b",ef"[..], res3)); + + assert_eq!(multi(f), Incomplete(Needed::Size(4))); + assert_eq!(multi_longsep(g), Incomplete(Needed::Size(6))); + assert_eq!(multi(h), Incomplete(Needed::Size(9))); + } + + #[test] + #[cfg(feature = "std")] + fn separated_nonempty_list_complete() { + named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list_complete!(tag!(","), alpha)); + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"abcd,abcd,ef"[..]; + let d = &b"abc."[..]; + let e = &b"abcd,ef."[..]; + let f = &b"123"[..]; + + assert_eq!(multi(a), Done(&b""[..], vec!(a))); + assert_eq!(multi(b), Done(&b""[..], vec!(&b"abcd"[..], &b"abcdef"[..]))); + assert_eq!(multi(c), Done(&b""[..], vec!(&b"abcd"[..], &b"abcd"[..], &b"ef"[..]))); + assert_eq!(multi(d), Done(&b"."[..], vec!(&b"abc"[..]))); + assert_eq!(multi(e), Done(&b"."[..], vec!(&b"abcd"[..], &b"ef"[..]))); + assert_eq!(multi(f), Error(error_position!(ErrorKind::Alpha, &b"123"[..]))); + } + + + #[test] + #[cfg(feature = "std")] + fn many0() { + named!( tag_abcd, tag!("abcd") ); + named!( tag_empty, tag!("") ); + named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); + + assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); + assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); + assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); + assert_eq!(multi_empty(&b"abcdef"[..]), Error(error_position!(ErrorKind::Many0, &b"abcdef"[..]))); + } + + #[cfg(feature = "nightly")] + use test::Bencher; + + #[cfg(feature = "nightly")] + #[bench] + fn many0_bench(b: &mut Bencher) { + named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); + b.iter(|| { + multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..]) + }); + } + + #[test] + #[cfg(feature = "std")] + fn many1() { + named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res2)); + assert_eq!(multi(c), Error(error_position!(ErrorKind::Many1,c))); + assert_eq!(multi(d), Incomplete(Needed::Size(8))); + } + + #[test] + #[cfg(feature = "std")] + fn many_till() { + named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); + + let a = b"abcdabcdefghabcd"; + let b = b"efghabcd"; + let c = b"azerty"; + + let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); + let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); + assert_eq!(multi(&a[..]), Done(&b"abcd"[..], res_a)); + assert_eq!(multi(&b[..]), Done(&b"abcd"[..], res_b)); + assert_eq!(multi(&c[..]), Error(error_node_position!(ErrorKind::ManyTill,&c[..], error_position!(ErrorKind::Tag,&c[..])))); + } + + #[test] + #[cfg(feature = "std")] + fn infinite_many() { + fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { + println!("input: {:?}", input); + Error(error_position!(ErrorKind::Custom(0),input)) + } + + // should not go into an infinite loop + named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi0(a), Done(a, Vec::new())); + + named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi1(a), Error(error_position!(ErrorKind::Many1,a))); + } + + #[test] + #[cfg(feature = "std")] + fn many_m_n() { + named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Error(error_position!(ErrorKind::ManyMN,a))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res1)); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Done(&b"efgh"[..], res2)); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); + assert_eq!(multi(e), Incomplete(Needed::Size(8))); + } + + #[test] + #[cfg(feature = "std")] + fn count() { + const TIMES: usize = 2; + named!( tag_abc, tag!("abc") ); + named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]])); + assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Size(6))); + assert_eq!(cnt_2(&b"xxx"[..]), Error(error_position!(ErrorKind::Count, &b"xxx"[..]))); + assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"xxxabcabcdef"[..]))); + assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"abcxxxabcdef"[..]))); + } + + #[test] + #[cfg(feature = "std")] + fn count_zero() { + const TIMES: usize = 0; + named!( tag_abc, tag!("abc") ); + named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + let done = &b"abcabcabcdef"[..]; + let parsed_done = Vec::new(); + let rest = done; + let incomplete_1 = &b"ab"[..]; + let parsed_incompl_1 = Vec::new(); + let incomplete_2 = &b"abcab"[..]; + let parsed_incompl_2 = Vec::new(); + let error = &b"xxx"[..]; + let error_remain = &b"xxx"[..]; + let parsed_err = Vec::new(); + let error_1 = &b"xxxabcabcdef"[..]; + let parsed_err_1 = Vec::new(); + let error_1_remain = &b"xxxabcabcdef"[..]; + let error_2 = &b"abcxxxabcdef"[..]; + let parsed_err_2 = Vec::new(); + let error_2_remain = &b"abcxxxabcdef"[..]; + + assert_eq!(counter_2(done), Done(rest, parsed_done)); + assert_eq!(counter_2(incomplete_1), Done(incomplete_1, parsed_incompl_1)); + assert_eq!(counter_2(incomplete_2), Done(incomplete_2, parsed_incompl_2)); + assert_eq!(counter_2(error), Done(error_remain, parsed_err)); + assert_eq!(counter_2(error_1), Done(error_1_remain, parsed_err_1)); + assert_eq!(counter_2(error_2), Done(error_2_remain, parsed_err_2)); + } + + #[test] + fn count_fixed() { + const TIMES: usize = 2; + named!( tag_abc, tag!("abc") ); + named!( cnt_2<&[u8], [&[u8]; TIMES] >, count_fixed!(&[u8], tag_abc, TIMES ) ); + + assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], [&b"abc"[..], &b"abc"[..]])); + assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Size(6))); + assert_eq!(cnt_2(&b"xxx"[..]), Error(error_position!(ErrorKind::Count, &b"xxx"[..]))); + assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"xxxabcabcdef"[..]))); + assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"abcxxxabcdef"[..]))); + } + + #[allow(dead_code)] + pub fn compile_count_fixed(input: &[u8]) -> IResult<&[u8], ()> { + do_parse!(input, + tag!("abcd") >> + count_fixed!( u16, le_u16, 4 ) >> + eof!() >> + () + ) + } + + #[allow(unused_variables)] + #[test] + fn count_fixed_no_type() { + const TIMES: usize = 2; + named!( tag_abc, tag!("abc") ); + named!( counter_2<&[u8], [&[u8]; TIMES], () >, count_fixed!(&[u8], tag_abc, TIMES ) ); + + let done = &b"abcabcabcdef"[..]; + let parsed_main = [&b"abc"[..], &b"abc"[..]]; + let rest = &b"abcdef"[..]; + let incomplete_1 = &b"ab"[..]; + let incomplete_2 = &b"abcab"[..]; + let error = &b"xxx"[..]; + let error_1 = &b"xxxabcabcdef"[..]; + let error_1_remain = &b"xxxabcabcdef"[..]; + let error_2 = &b"abcxxxabcdef"[..]; + let error_2_remain = &b"abcxxxabcdef"[..]; + + assert_eq!(counter_2(done), Done(rest, parsed_main)); + assert_eq!(counter_2(incomplete_1), Incomplete(Needed::Size(3))); + assert_eq!(counter_2(incomplete_2), Incomplete(Needed::Size(6))); + assert_eq!(counter_2(error), Error(error_position!(ErrorKind::Count, error))); + assert_eq!(counter_2(error_1), Error(error_position!(ErrorKind::Count, error_1_remain))); + assert_eq!(counter_2(error_2), Error(error_position!(ErrorKind::Count, error_2_remain))); + } + + named!(pub number, map_res!( + map_res!( + digit, + str::from_utf8 + ), + FromStr::from_str + )); + + #[test] + #[cfg(feature = "std")] + fn length_count() { + named!(tag_abc, tag!(&b"abc"[..]) ); + named!( cnt<&[u8], Vec<&[u8]> >, length_count!(number, tag_abc) ); + + assert_eq!(cnt(&b"2abcabcabcdef"[..]), Done(&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]])); + assert_eq!(cnt(&b"2ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(cnt(&b"3abcab"[..]), Incomplete(Needed::Size(7))); + assert_eq!(cnt(&b"xxx"[..]), Error(error_position!(ErrorKind::Digit, &b"xxx"[..]))); + assert_eq!(cnt(&b"2abcxxx"[..]), Error(error_position!(ErrorKind::Count, &b"abcxxx"[..]))); + } + + #[test] + fn length_data() { + named!( take<&[u8], &[u8]>, length_data!(number) ); + + assert_eq!(take(&b"6abcabcabcdef"[..]), Done(&b"abcdef"[..], &b"abcabc"[..])); + assert_eq!(take(&b"3ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(take(&b"xxx"[..]), Error(error_position!(ErrorKind::Digit, &b"xxx"[..]))); + assert_eq!(take(&b"2abcxxx"[..]), Done(&b"cxxx"[..], &b"ab"[..])); + } + + #[test] + fn length_value_test() { + named!(length_value_1<&[u8], u16 >, length_value!(be_u8, be_u16)); + named!(length_value_2<&[u8], (u8, u8) >, length_value!(be_u8, tuple!(be_u8, be_u8))); + + let i1 = [0, 5, 6]; + assert_eq!(length_value_1(&i1), IResult::Error(error_position!(ErrorKind::Complete, &b""[..]))); + assert_eq!(length_value_2(&i1), IResult::Error(error_position!(ErrorKind::Complete, &b""[..]))); + + let i2 = [1, 5, 6, 3]; + assert_eq!(length_value_1(&i2), IResult::Error(error_position!(ErrorKind::Complete, &i2[1..2]))); + assert_eq!(length_value_2(&i2), IResult::Error(error_position!(ErrorKind::Complete, &i2[1..2]))); + + let i3 = [2, 5, 6, 3, 4, 5, 7]; + assert_eq!(length_value_1(&i3), IResult::Done(&i3[3..], 1286)); + assert_eq!(length_value_2(&i3), IResult::Done(&i3[3..], (5, 6))); + + let i4 = [3, 5, 6, 3, 4, 5]; + assert_eq!(length_value_1(&i4), IResult::Done(&i4[4..], 1286)); + assert_eq!(length_value_2(&i4), IResult::Done(&i4[4..], (5, 6))); + } + + #[test] + #[cfg(feature = "std")] + fn fold_many0() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!( tag_abcd, tag!("abcd") ); + named!( tag_empty, tag!("") ); + named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); + + assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); + assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); + assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); + assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); + assert_eq!(multi_empty(&b"abcdef"[..]), Error(error_position!(ErrorKind::Many0, &b"abcdef"[..]))); + } + + #[test] + #[cfg(feature = "std")] + fn fold_many1() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Done(&b"ef"[..], res1)); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res2)); + assert_eq!(multi(c), Error(error_position!(ErrorKind::Many1,c))); + assert_eq!(multi(d), Incomplete(Needed::Size(8))); + } + + #[test] + #[cfg(feature = "std")] + fn fold_many_m_n() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Error(error_position!(ErrorKind::ManyMN,a))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Done(&b"efgh"[..], res1)); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Done(&b"efgh"[..], res2)); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); + assert_eq!(multi(e), Incomplete(Needed::Size(8))); + } + +} diff --git third_party/rust/nom/src/nom.rs third_party/rust/nom/src/nom.rs index 74bac5bfb0c4..7f48afc511bd 100644 --- third_party/rust/nom/src/nom.rs +++ third_party/rust/nom/src/nom.rs @@ -6,16 +6,18 @@ //! but the macros system makes no promises. //! -#[cfg(feature = "core")] +#[cfg(not(feature = "std"))] use std::prelude::v1::*; use std::boxed::Box; use std::fmt::Debug; use internal::*; use internal::IResult::*; -use internal::Err::*; -use util::{ErrorKind,IterIndices,AsChar,InputLength}; +use util::ErrorKind; +use traits::{AsChar,InputLength,InputIter}; use std::mem::transmute; +use std::ops::{Range,RangeFrom,RangeTo}; +use traits::{Compare,CompareResult,Slice}; #[inline] pub fn tag_cl<'a,'b>(rec:&'a[u8]) -> Box IResult<&'b[u8], &'b[u8]> + 'a> { @@ -23,12 +25,12 @@ pub fn tag_cl<'a,'b>(rec:&'a[u8]) -> Box IResult<&'b[u8], &'b[u8 if i.len() >= rec.len() && &i[0..rec.len()] == rec { Done(&i[rec.len()..], &i[0..rec.len()]) } else { - Error(Position(ErrorKind::TagClosure, i)) + Error(error_position!(ErrorKind::TagClosure, i)) } }) } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] #[inline] pub fn print(input: T) -> IResult { println!("{:?}", input); @@ -40,38 +42,89 @@ pub fn begin(input: &[u8]) -> IResult<(), &[u8]> { Done((), input) } +pub fn crlf(input:T) -> IResult where + T:Slice>+Slice>+Slice>, + T: InputIter, + T: Compare<&'static str> { + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => { + IResult::Done(input.slice(2..), input.slice(0..2)) + }, + CompareResult::Incomplete => IResult::Incomplete(Needed::Size(2)), + CompareResult::Error => IResult::Error(error_position!(ErrorKind::CrLf, input)) + } +} + // FIXME: when rust-lang/rust#17436 is fixed, macros will be able to export // public methods -//pub is_not!(line_ending b"\r\n") -pub fn not_line_ending(input:&[u8]) -> IResult<&[u8], &[u8]> { - for (idx, item) in input.iter().enumerate() { - for &i in b"\r\n".iter() { - if *item == i { - return Done(&input[idx..], &input[0..idx]) +pub fn not_line_ending(input:T) -> IResult where + T:Slice>+Slice>+Slice>, + T: InputIter+InputLength, + T: Compare<&'static str>, + ::Item: AsChar { + match input.iter_elements().position(|item| { + let c = item.as_char(); + c == '\r' || c == '\n' + }) { + None => Done(input.slice(input.input_len()..), input), + Some(index) => { + let mut it = input.iter_elements(); + let nth = it.nth(index).unwrap().as_char(); + if nth == '\r' { + let sliced = input.slice(index..); + let comp = sliced.compare("\r\n"); + match comp { + //FIXME: calculate the right index + CompareResult::Incomplete => Incomplete(Needed::Unknown), + CompareResult::Error => Error(error_position!(ErrorKind::Tag, input)), + CompareResult::Ok => Done(input.slice(index..), input.slice(..index)) + } + } else { + Done(input.slice(index..), input.slice(..index)) + } + } } +} + +/// Recognizes an end of line (both '\n' and '\r\n') +pub fn line_ending(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + T: Compare<&'static str> { + + match input.compare("\n") { + CompareResult::Ok => Done(input.slice(1..), input.slice(0..1)), + CompareResult::Incomplete => Incomplete(Needed::Size(1)), + CompareResult::Error => match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Done(input.slice(2..), input.slice(0..2)), + CompareResult::Incomplete => Incomplete(Needed::Size(2)), + CompareResult::Error => Error(error_position!(ErrorKind::CrLf, input)) } } - Done(&input[input.len()..], input) } -named!(tag_ln, tag!("\n")); - -/// Recognizes a line feed -#[inline] -pub fn line_ending(input:&[u8]) -> IResult<&[u8], &[u8]> { - tag_ln(input) +pub fn eol(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + T: Compare<&'static str> { + line_ending(input) } +/// Tests if byte is ASCII alphabetic: A-Z, a-z #[inline] pub fn is_alphabetic(chr:u8) -> bool { (chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A) } +/// Tests if byte is ASCII digit: 0-9 #[inline] pub fn is_digit(chr: u8) -> bool { chr >= 0x30 && chr <= 0x39 } +/// Tests if byte is ASCII hex digit: 0-9, A-F, a-f #[inline] pub fn is_hex_digit(chr: u8) -> bool { (chr >= 0x30 && chr <= 0x39) || @@ -79,16 +132,19 @@ pub fn is_hex_digit(chr: u8) -> bool { (chr >= 0x61 && chr <= 0x66) } +/// Tests if byte is ASCII octal digit: 0-7 #[inline] pub fn is_oct_digit(chr: u8) -> bool { chr >= 0x30 && chr <= 0x37 } +/// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9 #[inline] pub fn is_alphanumeric(chr: u8) -> bool { is_alphabetic(chr) || is_digit(chr) } +/// Tests if byte is ASCII space or tab #[inline] pub fn is_space(chr:u8) -> bool { chr == ' ' as u8 || chr == '\t' as u8 @@ -101,154 +157,160 @@ pub fn is_space(chr:u8) -> bool { //pub filter!(oct_digit is_oct_digit) //pub filter!(alphanumeric is_alphanumeric) -use std::ops::{Index,Range,RangeFrom}; -/// Recognizes lowercase and uppercase alphabetic characters: a-zA-Z -pub fn alpha<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more lowercase and uppercase alphabetic characters: a-zA-Z +pub fn alpha(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::Alpha, input)) + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { if ! item.is_alpha() { if idx == 0 { - return Error(Position(ErrorKind::Alpha, input)) + return Error(error_position!(ErrorKind::Alpha, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } -/// Recognizes numerical characters: 0-9 -pub fn digit<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more numerical characters: 0-9 +pub fn digit(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::Digit, input)) + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { - if ! item.is_0_to_9() { + if ! item.is_dec_digit() { if idx == 0 { - return Error(Position(ErrorKind::Digit, input)) + return Error(error_position!(ErrorKind::Digit, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } -/// Recognizes hexadecimal numerical characters: 0-9, A-F, a-f -pub fn hex_digit<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more hexadecimal numerical characters: 0-9, A-F, a-f +pub fn hex_digit(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::HexDigit, input)) + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { if ! item.is_hex_digit() { if idx == 0 { - return Error(Position(ErrorKind::HexDigit, input)) + return Error(error_position!(ErrorKind::HexDigit, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } -/// Recognizes octal characters: 0-7 -pub fn oct_digit<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more octal characters: 0-7 +pub fn oct_digit(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::OctDigit, input)) + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { if ! item.is_oct_digit() { if idx == 0 { - return Error(Position(ErrorKind::OctDigit, input)) + return Error(error_position!(ErrorKind::OctDigit, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } -/// Recognizes numerical and alphabetic characters: 0-9a-zA-Z -pub fn alphanumeric<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more numerical and alphabetic characters: 0-9a-zA-Z +pub fn alphanumeric(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::AlphaNumeric, input)); + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { if ! item.is_alphanum() { if idx == 0 { - return Error(Position(ErrorKind::AlphaNumeric, input)) + return Error(error_position!(ErrorKind::AlphaNumeric, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } -/// Recognizes spaces and tabs -pub fn space<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more spaces and tabs +pub fn space(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::Space, input)); + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { let chr = item.as_char(); if ! (chr == ' ' || chr == '\t') { if idx == 0 { - return Error(Position(ErrorKind::Space, input)) + return Error(error_position!(ErrorKind::Space, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } -/// Recognizes spaces, tabs, carriage returns and line feeds -pub fn multispace<'a, T: ?Sized>(input:&'a T) -> IResult<&'a T, &'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: IterIndices+InputLength { +/// Recognizes one or more spaces, tabs, carriage returns and line feeds +pub fn multispace(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputIter+InputLength, + ::Item: AsChar { let input_length = input.input_len(); if input_length == 0 { - return Error(Position(ErrorKind::MultiSpace, input)); + return Incomplete(Needed::Unknown); } for (idx, item) in input.iter_indices() { let chr = item.as_char(); if ! (chr == ' ' || chr == '\t' || chr == '\r' || chr == '\n') { if idx == 0 { - return Error(Position(ErrorKind::MultiSpace, input)) + return Error(error_position!(ErrorKind::MultiSpace, input)) } else { - return Done(&input[idx..], &input[0..idx]) + return Done(input.slice(idx..), input.slice(0..idx)) } } } - Done(&input[input_length..], input) + Done(input.slice(input_length..), input) } pub fn sized_buffer(input:&[u8]) -> IResult<&[u8], &[u8]> { @@ -265,20 +327,6 @@ pub fn sized_buffer(input:&[u8]) -> IResult<&[u8], &[u8]> { } } -pub fn length_value(input:&[u8]) -> IResult<&[u8], &[u8]> { - let input_len = input.len(); - if input_len == 0 { - return Error(Position(ErrorKind::LengthValueFn, input)) - } - - let len = input[0] as usize; - if input_len - 1 >= len { - IResult::Done(&input[len+1..], &input[1..len+1]) - } else { - IResult::Incomplete(Needed::Size(1+len)) - } -} - /// Recognizes an unsigned 1 byte integer (equivalent to take!(1) #[inline] pub fn be_u8(i: &[u8]) -> IResult<&[u8], u8> { @@ -300,6 +348,17 @@ pub fn be_u16(i: &[u8]) -> IResult<&[u8], u16> { } } +/// Recognizes big endian unsigned 3 byte integer +#[inline] +pub fn be_u24(i: &[u8]) -> IResult<&[u8], u32> { + if i.len() < 3 { + Incomplete(Needed::Size(3)) + } else { + let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32); + Done(&i[3..], res) + } +} + /// Recognizes big endian unsigned 4 bytes integer #[inline] pub fn be_u32(i: &[u8]) -> IResult<&[u8], u32> { @@ -335,6 +394,13 @@ pub fn be_i16(i:&[u8]) -> IResult<&[u8], i16> { map!(i, be_u16, | x | { x as i16 }) } +/// Recognizes big endian signed 3 bytes integer +#[inline] +pub fn be_i24(i: &[u8]) -> IResult<&[u8], i32> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, be_u24, | x | if x & 0x80_00_00 != 0 { (x | 0xff_00_00_00) as i32 } else { x as i32 }) +} + /// Recognizes big endian signed 4 bytes integer #[inline] pub fn be_i32(i:&[u8]) -> IResult<&[u8], i32> { @@ -368,6 +434,17 @@ pub fn le_u16(i: &[u8]) -> IResult<&[u8], u16> { } } +/// Recognizes little endian unsigned 3 byte integer +#[inline] +pub fn le_u24(i: &[u8]) -> IResult<&[u8], u32> { + if i.len() < 3 { + Incomplete(Needed::Size(3)) + } else { + let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16); + Done(&i[3..], res) + } +} + /// Recognizes little endian unsigned 4 bytes integer #[inline] pub fn le_u32(i: &[u8]) -> IResult<&[u8], u32> { @@ -403,6 +480,13 @@ pub fn le_i16(i:&[u8]) -> IResult<&[u8], i16> { map!(i, le_u16, | x | { x as i16 }) } +/// Recognizes little endian signed 3 bytes integer +#[inline] +pub fn le_i24(i: &[u8]) -> IResult<&[u8], i32> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, le_u24, | x | if x & 0x80_00_00 != 0 { (x | 0xff_00_00_00) as i32 } else { x as i32 }) +} + /// Recognizes little endian signed 4 bytes integer #[inline] pub fn le_i32(i:&[u8]) -> IResult<&[u8], i32> { @@ -415,31 +499,38 @@ pub fn le_i64(i:&[u8]) -> IResult<&[u8], i64> { map!(i, le_u64, | x | { x as i64 }) } -/// if parameter is true, parse a big endian u16 integer, +/// Configurable endianness +#[derive(Debug,PartialEq,Eq,Clone,Copy)] +pub enum Endianness { + Big, + Little, +} + +/// if the parameter is nom::Endianness::Big, parse a big endian u16 integer, /// otherwise a little endian u16 integer #[macro_export] -macro_rules! u16 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_u16($i) } else { $crate::le_u16($i) } } );); -/// if parameter is true, parse a big endian u32 integer, +macro_rules! u16 ( ($i:expr, $e:expr) => ( {if $crate::Endianness::Big == $e { $crate::be_u16($i) } else { $crate::le_u16($i) } } );); +/// if the parameter is nom::Endianness::Big, parse a big endian u32 integer, /// otherwise a little endian u32 integer #[macro_export] -macro_rules! u32 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_u32($i) } else { $crate::le_u32($i) } } );); -/// if parameter is true, parse a big endian u64 integer, +macro_rules! u32 ( ($i:expr, $e:expr) => ( {if $crate::Endianness::Big == $e { $crate::be_u32($i) } else { $crate::le_u32($i) } } );); +/// if the parameter is nom::Endianness::Big, parse a big endian u64 integer, /// otherwise a little endian u64 integer #[macro_export] -macro_rules! u64 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_u64($i) } else { $crate::le_u64($i) } } );); +macro_rules! u64 ( ($i:expr, $e:expr) => ( {if $crate::Endianness::Big == $e { $crate::be_u64($i) } else { $crate::le_u64($i) } } );); -/// if parameter is true, parse a big endian i16 integer, +/// if the parameter is nom::Endianness::Big, parse a big endian i16 integer, /// otherwise a little endian i16 integer #[macro_export] -macro_rules! i16 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_i16($i) } else { $crate::le_i16($i) } } );); -/// if parameter is true, parse a big endian i32 integer, +macro_rules! i16 ( ($i:expr, $e:expr) => ( {if $crate::Endianness::Big == $e { $crate::be_i16($i) } else { $crate::le_i16($i) } } );); +/// if the parameter is nom::Endianness::Big, parse a big endian i32 integer, /// otherwise a little endian i32 integer #[macro_export] -macro_rules! i32 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_i32($i) } else { $crate::le_i32($i) } } );); -/// if parameter is true, parse a big endian i64 integer, +macro_rules! i32 ( ($i:expr, $e:expr) => ( {if $crate::Endianness::Big == $e { $crate::be_i32($i) } else { $crate::le_i32($i) } } );); +/// if the parameter is nom::Endianness::Big, parse a big endian i64 integer, /// otherwise a little endian i64 integer #[macro_export] -macro_rules! i64 ( ($i:expr, $e:expr) => ( {if $e { $crate::be_i64($i) } else { $crate::le_i64($i) } } );); +macro_rules! i64 ( ($i:expr, $e:expr) => ( {if $crate::Endianness::Big == $e { $crate::be_i64($i) } else { $crate::le_i64($i) } } );); /// Recognizes big endian 4 bytes floating point number #[inline] @@ -500,7 +591,7 @@ pub fn le_f64(input: &[u8]) -> IResult<&[u8], f64> { /// Recognizes a hex-encoded integer #[inline] pub fn hex_u32(input: &[u8]) -> IResult<&[u8], u32> { - match is_a!(input, &b"0123456789abcdef"[..]) { + match is_a!(input, &b"0123456789abcdefABCDEF"[..]) { Error(e) => Error(e), Incomplete(e) => Incomplete(e), Done(i,o) => { @@ -524,30 +615,15 @@ pub fn hex_u32(input: &[u8]) -> IResult<&[u8], u32> { } } -/// Recognizes empty input buffers -/// -/// useful to verify that the previous parsers used all of the input -#[inline] -//pub fn eof(input:&[u8]) -> IResult<&[u8], &[u8]> { -pub fn eof<'a, T:?Sized>(input: &'a T) -> IResult<&'a T,&'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: InputLength { - if input.input_len() == 0 { - Done(input, input) - } else { - Error(Position(ErrorKind::Eof, input)) - } -} - /// Recognizes non empty buffers #[inline] -pub fn non_empty<'a, T:?Sized>(input: &'a T) -> IResult<&'a T,&'a T> where - T:Index, Output=T>+Index, Output=T>, - &'a T: InputLength { +pub fn non_empty(input:T) -> IResult where + T: Slice>+Slice>+Slice>, + T: InputLength { if input.input_len() == 0 { - Error(Position(ErrorKind::NonEmpty, input)) + Error(error_position!(ErrorKind::NonEmpty, input)) } else { - Done(&input[input.input_len()..], input) + Done(input.slice(input.input_len()..), input) } } @@ -563,12 +639,102 @@ pub fn rest_s(input: &str) -> IResult<&str, &str> { IResult::Done(&input[input.len()..], input) } +/// Recognizes floating point number in a byte string and returns a f32 +#[cfg(feature = "std")] +pub fn float(input: &[u8]) -> IResult<&[u8],f32> { + flat_map!(input, + recognize!( + tuple!( + opt!(alt!(tag!("+") | tag!("-"))), + alt!( + delimited!(digit, tag!("."), opt!(digit)) + | delimited!(opt!(digit), tag!("."), digit) + ), + opt!(complete!(tuple!( + alt!(tag!("e") | tag!("E")), + opt!(alt!(tag!("+") | tag!("-"))), + digit + ) + )) + ) + ), + parse_to!(f32) + ) +} + +/// Recognizes floating point number in a string and returns a f32 +#[cfg(feature = "std")] +pub fn float_s(input: &str) -> IResult<&str,f32> { + flat_map!(input, + recognize!( + tuple!( + opt!(alt!(tag!("+") | tag!("-"))), + alt!( + delimited!(digit, tag!("."), opt!(digit)) + | delimited!(opt!(digit), tag!("."), digit) + ), + opt!(complete!(tuple!( + alt!(tag!("e") | tag!("E")), + opt!(alt!(tag!("+") | tag!("-"))), + digit + ) + )) + ) + ), + parse_to!(f32) + ) +} + +/// Recognizes floating point number in a byte string and returns a f64 +#[cfg(feature = "std")] +pub fn double(input: &[u8]) -> IResult<&[u8],f64> { + flat_map!(input, + recognize!( + tuple!( + opt!(alt!(tag!("+") | tag!("-"))), + alt!( + delimited!(digit, tag!("."), opt!(digit)) + | delimited!(opt!(digit), tag!("."), digit) + ), + opt!(complete!(tuple!( + alt!(tag!("e") | tag!("E")), + opt!(alt!(tag!("+") | tag!("-"))), + digit + ) + )) + ) + ), + parse_to!(f64) + ) +} + +/// Recognizes floating point number in a string and returns a f64 +#[cfg(feature = "std")] +pub fn double_s(input: &str) -> IResult<&str,f64> { + flat_map!(input, + recognize!( + tuple!( + opt!(alt!(tag!("+") | tag!("-"))), + alt!( + delimited!(digit, tag!("."), opt!(digit)) + | delimited!(opt!(digit), tag!("."), digit) + ), + opt!(complete!(tuple!( + alt!(tag!("e") | tag!("E")), + opt!(alt!(tag!("+") | tag!("-"))), + digit + ) + )) + ) + ), + parse_to!(f64) + ) +} + #[cfg(test)] mod tests { use super::*; use internal::{Needed,IResult}; - use internal::IResult::*; - use internal::Err::*; use util::ErrorKind; #[test] @@ -578,7 +744,7 @@ mod tests { assert_eq!(r, Done(&b"abcdefgh"[..], &b"abcd"[..])); let r2 = x(&b"abcefgh"[..]); - assert_eq!(r2, Error(Position(ErrorKind::TagClosure, &b"abcefgh"[..]))); + assert_eq!(r2, Error(error_position!(ErrorKind::TagClosure, &b"abcefgh"[..]))); } #[test] @@ -590,22 +756,22 @@ mod tests { let d: &[u8] = "azé12".as_bytes(); let e: &[u8] = b" "; assert_eq!(alpha(a), Done(empty, a)); - assert_eq!(alpha(b), Error(Position(ErrorKind::Alpha,b))); + assert_eq!(alpha(b), Error(error_position!(ErrorKind::Alpha,b))); assert_eq!(alpha(c), Done(&c[1..], &b"a"[..])); assert_eq!(alpha(d), Done("é12".as_bytes(), &b"az"[..])); - assert_eq!(digit(a), Error(Position(ErrorKind::Digit,a))); + assert_eq!(digit(a), Error(error_position!(ErrorKind::Digit,a))); assert_eq!(digit(b), Done(empty, b)); - assert_eq!(digit(c), Error(Position(ErrorKind::Digit,c))); - assert_eq!(digit(d), Error(Position(ErrorKind::Digit,d))); + assert_eq!(digit(c), Error(error_position!(ErrorKind::Digit,c))); + assert_eq!(digit(d), Error(error_position!(ErrorKind::Digit,d))); assert_eq!(hex_digit(a), Done(empty, a)); assert_eq!(hex_digit(b), Done(empty, b)); assert_eq!(hex_digit(c), Done(empty, c)); assert_eq!(hex_digit(d), Done("zé12".as_bytes(), &b"a"[..])); - assert_eq!(hex_digit(e), Error(Position(ErrorKind::HexDigit,e))); - assert_eq!(oct_digit(a), Error(Position(ErrorKind::OctDigit,a))); + assert_eq!(hex_digit(e), Error(error_position!(ErrorKind::HexDigit,e))); + assert_eq!(oct_digit(a), Error(error_position!(ErrorKind::OctDigit,a))); assert_eq!(oct_digit(b), Done(empty, b)); - assert_eq!(oct_digit(c), Error(Position(ErrorKind::OctDigit,c))); - assert_eq!(oct_digit(d), Error(Position(ErrorKind::OctDigit,d))); + assert_eq!(oct_digit(c), Error(error_position!(ErrorKind::OctDigit,c))); + assert_eq!(oct_digit(d), Error(error_position!(ErrorKind::OctDigit,d))); assert_eq!(alphanumeric(a), Done(empty, a)); assert_eq!(fix_error!(b,(), alphanumeric), Done(empty, b)); assert_eq!(alphanumeric(c), Done(empty, c)); @@ -622,22 +788,22 @@ mod tests { let d = "azé12"; let e = " "; assert_eq!(alpha(a), Done(empty, a)); - assert_eq!(alpha(b), Error(Position(ErrorKind::Alpha,b))); + assert_eq!(alpha(b), Error(error_position!(ErrorKind::Alpha,b))); assert_eq!(alpha(c), Done(&c[1..], &"a"[..])); assert_eq!(alpha(d), Done("12", &"azé"[..])); - assert_eq!(digit(a), Error(Position(ErrorKind::Digit,a))); + assert_eq!(digit(a), Error(error_position!(ErrorKind::Digit,a))); assert_eq!(digit(b), Done(empty, b)); - assert_eq!(digit(c), Error(Position(ErrorKind::Digit,c))); - assert_eq!(digit(d), Error(Position(ErrorKind::Digit,d))); + assert_eq!(digit(c), Error(error_position!(ErrorKind::Digit,c))); + assert_eq!(digit(d), Error(error_position!(ErrorKind::Digit,d))); assert_eq!(hex_digit(a), Done(empty, a)); assert_eq!(hex_digit(b), Done(empty, b)); assert_eq!(hex_digit(c), Done(empty, c)); assert_eq!(hex_digit(d), Done("zé12", &"a"[..])); - assert_eq!(hex_digit(e), Error(Position(ErrorKind::HexDigit,e))); - assert_eq!(oct_digit(a), Error(Position(ErrorKind::OctDigit,a))); + assert_eq!(hex_digit(e), Error(error_position!(ErrorKind::HexDigit,e))); + assert_eq!(oct_digit(a), Error(error_position!(ErrorKind::OctDigit,a))); assert_eq!(oct_digit(b), Done(empty, b)); - assert_eq!(oct_digit(c), Error(Position(ErrorKind::OctDigit,c))); - assert_eq!(oct_digit(d), Error(Position(ErrorKind::OctDigit,d))); + assert_eq!(oct_digit(c), Error(error_position!(ErrorKind::OctDigit,c))); + assert_eq!(oct_digit(d), Error(error_position!(ErrorKind::OctDigit,d))); assert_eq!(alphanumeric(a), Done(empty, a)); assert_eq!(fix_error!(b,(), alphanumeric), Done(empty, b)); assert_eq!(alphanumeric(c), Done(empty, c)); @@ -645,7 +811,7 @@ mod tests { assert_eq!(space(e), Done(&""[..], &" "[..])); } - use util::HexDisplay; + use util::Offset; #[test] fn offset() { let a = &b"abcd"[..]; @@ -686,18 +852,48 @@ mod tests { } #[test] - fn is_not() { + fn is_not_line_ending_bytes() { let a: &[u8] = b"ab12cd\nefgh"; assert_eq!(not_line_ending(a), Done(&b"\nefgh"[..], &b"ab12cd"[..])); let b: &[u8] = b"ab12cd\nefgh\nijkl"; assert_eq!(not_line_ending(b), Done(&b"\nefgh\nijkl"[..], &b"ab12cd"[..])); - let c: &[u8] = b"ab12cd"; - assert_eq!(not_line_ending(c), Done(&b""[..], c)); + let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; + assert_eq!(not_line_ending(c), Done(&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..])); + + let d: &[u8] = b"ab12cd"; + assert_eq!(not_line_ending(d), Done(&b""[..], d)); } #[test] + fn is_not_line_ending_str() { + /* + let a: &str = "ab12cd\nefgh"; + assert_eq!(not_line_ending(a), Done(&"\nefgh"[..], &"ab12cd"[..])); + + let b: &str = "ab12cd\nefgh\nijkl"; + assert_eq!(not_line_ending(b), Done(&"\nefgh\nijkl"[..], &"ab12cd"[..])); + + let c: &str = "ab12cd\r\nefgh\nijkl"; + assert_eq!(not_line_ending(c), Done(&"\r\nefgh\nijkl"[..], &"ab12cd"[..])); + + let d = "βèƒôřè\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(d), Done(&"\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..])); + + let e = "βèƒôřè\r\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(e), Done(&"\r\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..])); + */ + + let f = "βèƒôřè\rÂßÇáƒƭèř"; + assert_eq!(not_line_ending(f), Error(error_position!(ErrorKind::Tag,f))); + + let g: &str = "ab12cd"; + assert_eq!(not_line_ending(g), Done("", g)); + } + + #[test] + #[cfg(feature = "std")] fn buffer_with_size() { let i:Vec = vec![7,8]; let o:Vec = vec![4,5,6]; @@ -716,26 +912,6 @@ mod tests { assert_eq!(res, Done(&v2[..], ())); }*/ - #[test] - fn length_value_test() { - let i1 = vec![7,8]; - let o1 = vec![4, 5, 6]; - let arr1:[u8; 6usize] = [3, 4, 5, 6, 7, 8]; - let res1 = length_value(&arr1); - assert_eq!(Done(&i1[..], &o1[..]), res1); - - let i2:Vec = vec![4,5,6,7,8]; - let o2: &[u8] = b""; - let arr2:[u8; 6usize] = [0, 4, 5, 6, 7, 8]; - let res2 = length_value(&arr2); - assert_eq!(Done(&i2[..], o2), res2); - - let arr3:[u8; 7usize] = [8, 4, 5, 6, 7, 8, 9]; - let res3 = length_value(&arr3); - //FIXME: should be incomplete - assert_eq!(Incomplete(Needed::Size(9)), res3); - } - #[test] fn i8_tests() { assert_eq!(be_i8(&[0x00]), Done(&b""[..], 0)); @@ -752,6 +928,20 @@ mod tests { assert_eq!(be_i16(&[0x80, 0x00]), Done(&b""[..], -32768_i16)); } + #[test] + fn u24_tests() { + assert_eq!(be_u24(&[0x00, 0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(be_u24(&[0x00, 0xFF, 0xFF]), Done(&b""[..], 65535_u32)); + assert_eq!(be_u24(&[0x12, 0x34, 0x56]), Done(&b""[..], 1193046_u32)); + } + + #[test] + fn i24_tests() { + assert_eq!(be_i24(&[0xFF, 0xFF, 0xFF]), Done(&b""[..], -1_i32)); + assert_eq!(be_i24(&[0xFF, 0x00, 0x00]), Done(&b""[..], -65536_i32)); + assert_eq!(be_i24(&[0xED, 0xCB, 0xAA]), Done(&b""[..], -1193046_i32)); + } + #[test] fn i32_tests() { assert_eq!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0)); @@ -784,6 +974,20 @@ mod tests { assert_eq!(le_i16(&[0x00, 0x80]), Done(&b""[..], -32768_i16)); } + #[test] + fn le_u24_tests() { + assert_eq!(le_u24(&[0x00, 0x00, 0x00]), Done(&b""[..], 0)); + assert_eq!(le_u24(&[0xFF, 0xFF, 0x00]), Done(&b""[..], 65535_u32)); + assert_eq!(le_u24(&[0x56, 0x34, 0x12]), Done(&b""[..], 1193046_u32)); + } + + #[test] + fn le_i24_tests() { + assert_eq!(le_i24(&[0xFF, 0xFF, 0xFF]), Done(&b""[..], -1_i32)); + assert_eq!(le_i24(&[0x00, 0x00, 0xFF]), Done(&b""[..], -65536_i32)); + assert_eq!(le_i24(&[0xAA, 0xCB, 0xED]), Done(&b""[..], -1193046_i32)); + } + #[test] fn le_i32_tests() { assert_eq!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Done(&b""[..], 0)); @@ -830,6 +1034,7 @@ mod tests { assert_eq!(hex_u32(&b"ff"[..]), Done(&b""[..], 255)); assert_eq!(hex_u32(&b"1be2"[..]), Done(&b""[..], 7138)); assert_eq!(hex_u32(&b"c5a31be2"[..]), Done(&b""[..], 3315801058)); + assert_eq!(hex_u32(&b"C5A31be2"[..]), Done(&b""[..], 3315801058)); assert_eq!(hex_u32(&b"00c5a31be2"[..]), Done(&b"e2"[..], 12952347)); assert_eq!(hex_u32(&b"c5a31be201"[..]), Done(&b"01"[..], 3315801058)); assert_eq!(hex_u32(&b"ffffffff"[..]), Done(&b""[..], 4294967295)); @@ -840,49 +1045,51 @@ mod tests { fn end_of_input() { let not_over = &b"Hello, world!"[..]; let is_over = &b""[..]; + named!(eof_test, eof!()); - let res_not_over = eof(not_over); - assert_eq!(res_not_over, Error(Position(ErrorKind::Eof, not_over))); + let res_not_over = eof_test(not_over); + assert_eq!(res_not_over, Error(error_position!(ErrorKind::Eof, not_over))); - let res_over = eof(is_over); + let res_over = eof_test(is_over); assert_eq!(res_over, Done(is_over, is_over)); } #[test] fn configurable_endianness() { - named!(be_tst16, u16!(true)); - named!(le_tst16, u16!(false)); + named!(be_tst16, u16!(Endianness::Big)); + named!(le_tst16, u16!(Endianness::Little)); assert_eq!(be_tst16(&[0x80, 0x00]), Done(&b""[..], 32768_u16)); assert_eq!(le_tst16(&[0x80, 0x00]), Done(&b""[..], 128_u16)); - named!(be_tst32, u32!(true)); - named!(le_tst32, u32!(false)); + named!(be_tst32, u32!(Endianness::Big)); + named!(le_tst32, u32!(Endianness::Little)); assert_eq!(be_tst32(&[0x12, 0x00, 0x60, 0x00]), Done(&b""[..], 302014464_u32)); assert_eq!(le_tst32(&[0x12, 0x00, 0x60, 0x00]), Done(&b""[..], 6291474_u32)); - named!(be_tst64, u64!(true)); - named!(le_tst64, u64!(false)); + named!(be_tst64, u64!(Endianness::Big)); + named!(le_tst64, u64!(Endianness::Little)); assert_eq!(be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 1297142246100992000_u64)); assert_eq!(le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 36028874334666770_u64)); - named!(be_tsti16, i16!(true)); - named!(le_tsti16, i16!(false)); + named!(be_tsti16, i16!(Endianness::Big)); + named!(le_tsti16, i16!(Endianness::Little)); assert_eq!(be_tsti16(&[0x00, 0x80]), Done(&b""[..], 128_i16)); assert_eq!(le_tsti16(&[0x00, 0x80]), Done(&b""[..], -32768_i16)); - named!(be_tsti32, i32!(true)); - named!(le_tsti32, i32!(false)); + named!(be_tsti32, i32!(Endianness::Big)); + named!(le_tsti32, i32!(Endianness::Little)); assert_eq!(be_tsti32(&[0x00, 0x12, 0x60, 0x00]), Done(&b""[..], 1204224_i32)); assert_eq!(le_tsti32(&[0x00, 0x12, 0x60, 0x00]), Done(&b""[..], 6296064_i32)); - named!(be_tsti64, i64!(true)); - named!(le_tsti64, i64!(false)); + named!(be_tsti64, i64!(Endianness::Big)); + named!(le_tsti64, i64!(Endianness::Little)); assert_eq!(be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 71881672479506432_i64)); assert_eq!(le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), Done(&b""[..], 36028874334732032_i64)); } #[test] + #[cfg(feature = "std")] fn manual_configurable_endianness_test() { let x = 1; let int_parse: Box IResult<&[u8], u16> > = if x == 2 { @@ -907,10 +1114,10 @@ mod tests { assert_eq!(hex_digit(i), Done(empty, i)); let i = &b"g"[..]; - assert_eq!(hex_digit(i), Error(Position(ErrorKind::HexDigit,i))); + assert_eq!(hex_digit(i), Error(error_position!(ErrorKind::HexDigit,i))); let i = &b"G"[..]; - assert_eq!(hex_digit(i), Error(Position(ErrorKind::HexDigit,i))); + assert_eq!(hex_digit(i), Error(error_position!(ErrorKind::HexDigit,i))); assert!(is_hex_digit(b'0')); assert!(is_hex_digit(b'9')); @@ -934,7 +1141,7 @@ mod tests { assert_eq!(oct_digit(i), Done(empty, i)); let i = &b"8"[..]; - assert_eq!(oct_digit(i), Error(Position(ErrorKind::OctDigit,i))); + assert_eq!(oct_digit(i), Error(error_position!(ErrorKind::OctDigit,i))); assert!(is_oct_digit(b'0')); assert!(is_oct_digit(b'7')); @@ -947,4 +1154,72 @@ mod tests { assert!(!is_oct_digit(b'@')); assert!(!is_oct_digit(b'\x60')); } + + #[test] + fn full_line_windows() { + named!(take_full_line<(&[u8], &[u8])>, tuple!(not_line_ending, line_ending)); + let input = b"abc\r\n"; + let output = take_full_line(input); + assert_eq!(output, Done(&b""[..], (&b"abc"[..], &b"\r\n"[..]))); + } + + #[test] + fn full_line_unix() { + named!(take_full_line<(&[u8], &[u8])>, tuple!(not_line_ending, line_ending)); + let input = b"abc\n"; + let output = take_full_line(input); + assert_eq!(output, Done(&b""[..], (&b"abc"[..], &b"\n"[..]))); + } + + #[test] + fn check_windows_lineending() { + let input = b"\r\n"; + let output = line_ending(&input[..]); + assert_eq!(output, Done(&b""[..], &b"\r\n"[..])); + } + + #[test] + fn check_unix_lineending() { + let input = b"\n"; + let output = line_ending(&input[..]); + assert_eq!(output, Done(&b""[..], &b"\n"[..])); + } + + #[test] + fn cr_lf() { + assert_eq!(crlf(&b"\r\na"[..]), Done(&b"a"[..], &b"\r\n"[..])); + assert_eq!(crlf(&b"\r"[..]), Incomplete(Needed::Size(2))); + assert_eq!(crlf(&b"\ra"[..]), Error(error_position!(ErrorKind::CrLf, &b"\ra"[..]))); + + assert_eq!(crlf("\r\na"), Done("a", "\r\n")); + assert_eq!(crlf("\r"), Incomplete(Needed::Size(2))); + assert_eq!(crlf("\ra"), Error(error_position!(ErrorKind::CrLf, "\ra"))); + } + + #[test] + fn end_of_line() { + assert_eq!(eol(&b"\na"[..]), Done(&b"a"[..], &b"\n"[..])); + assert_eq!(eol(&b"\r\na"[..]), Done(&b"a"[..], &b"\r\n"[..])); + assert_eq!(eol(&b"\r"[..]), Incomplete(Needed::Size(2))); + assert_eq!(eol(&b"\ra"[..]), Error(error_position!(ErrorKind::CrLf, &b"\ra"[..]))); + + assert_eq!(eol("\na"), Done("a", "\n")); + assert_eq!(eol("\r\na"), Done("a", "\r\n")); + assert_eq!(eol("\r"), Incomplete(Needed::Size(2))); + assert_eq!(eol("\ra"), Error(error_position!(ErrorKind::CrLf, "\ra"))); + } + + #[test] + #[cfg(feature = "std")] + fn float_test() { + assert_eq!(float(&b"+3.14"[..]), Done(&b""[..], 3.14)); + assert_eq!(float_s(&"3.14"[..]), Done(&""[..], 3.14)); + assert_eq!(double(&b"3.14"[..]), Done(&b""[..], 3.14)); + assert_eq!(double_s(&"3.14"[..]), Done(&""[..], 3.14)); + + assert_eq!(float(&b"-1.234E-12"[..]), Done(&b""[..], -1.234E-12)); + assert_eq!(float_s(&"-1.234E-12"[..]), Done(&""[..], -1.234E-12)); + assert_eq!(double(&b"-1.234E-12"[..]), Done(&b""[..], -1.234E-12)); + assert_eq!(double_s(&"-1.234E-12"[..]), Done(&""[..], -1.234E-12)); + } } diff --git third_party/rust/nom/src/regexp.rs third_party/rust/nom/src/regexp.rs index 17f8ede1a3f7..b4a90f448cb2 100644 --- third_party/rust/nom/src/regexp.rs +++ third_party/rust/nom/src/regexp.rs @@ -28,11 +28,13 @@ macro_rules! re_match ( ($i:expr, $re:expr) => ( { use $crate::InputLength; + use $crate::Slice; let re = ::regex::Regex::new($re).unwrap(); if re.is_match($i) { - $crate::IResult::Done(&$i[$i.input_len()..], $i) + $crate::IResult::Done($i.slice($i.input_len()..), $i) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch)); + res } } ) @@ -48,11 +50,13 @@ macro_rules! re_match_static ( ($i:expr, $re:expr) => ( { use $crate::InputLength; + use $crate::Slice; regex!(RE, $re); if RE.is_match($i) { - $crate::IResult::Done(&$i[$i.input_len()..], $i) + $crate::IResult::Done($i.slice($i.input_len()..), $i) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch)); + res } } ) @@ -67,11 +71,13 @@ macro_rules! re_bytes_match ( ($i:expr, $re:expr) => ( { use $crate::InputLength; + use $crate::Slice; let re = ::regex::bytes::Regex::new($re).unwrap(); if re.is_match($i) { - $crate::IResult::Done(&$i[$i.input_len()..], $i) + $crate::IResult::Done($i.slice($i.input_len()..), $i) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch)); + res } } ) @@ -87,11 +93,13 @@ macro_rules! re_bytes_match_static ( ($i:expr, $re:expr) => ( { use $crate::InputLength; + use $crate::Slice; regex_bytes!(RE, $re); if RE.is_match($i) { - $crate::IResult::Done(&$i[$i.input_len()..], $i) + $crate::IResult::Done($i.slice($i.input_len()..), $i) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch)); + res } } ) @@ -105,11 +113,13 @@ macro_rules! re_bytes_match_static ( macro_rules! re_find ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::Regex::new($re).unwrap(); - if let Some((begin, end)) = re.find($i) { - $crate::IResult::Done(&$i[end..], &$i[begin..end]) + if let Some(m) = re.find($i) { + $crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end())) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind)); + res } } ) @@ -124,11 +134,13 @@ macro_rules! re_find ( macro_rules! re_find_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex!(RE, $re); - if let Some((begin, end)) = RE.find($i) { - $crate::IResult::Done(&$i[end..], &$i[begin..end]) + if let Some(m) = RE.find($i) { + $crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end())) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind)); + res } } @@ -143,11 +155,13 @@ macro_rules! re_find_static ( macro_rules! re_bytes_find ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::bytes::Regex::new($re).unwrap(); - if let Some((begin, end)) = re.find($i) { - $crate::IResult::Done(&$i[end..], &$i[begin..end]) + if let Some(m) = re.find($i) { + $crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end())) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind)); + res } } ) @@ -162,11 +176,13 @@ macro_rules! re_bytes_find ( macro_rules! re_bytes_find_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex_bytes!(RE, $re); - if let Some((begin, end)) = RE.find($i) { - $crate::IResult::Done(&$i[end..], &$i[begin..end]) + if let Some(m) = RE.find($i) { + $crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end())) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind)); + res } } @@ -181,16 +197,18 @@ macro_rules! re_bytes_find_static ( macro_rules! re_matches ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::Regex::new($re).unwrap(); - let v: Vec<&str> = re.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + let v: Vec<&str> = re.find_iter($i).map(|m| $i.slice(m.start()..m.end())).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches)); + res } } ) @@ -205,16 +223,18 @@ macro_rules! re_matches ( macro_rules! re_matches_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex!(RE, $re); - let v: Vec<&str> = RE.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + let v: Vec<&str> = RE.find_iter($i).map(|m| $i.slice(m.start()..m.end())).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches)); + res } } ) @@ -228,16 +248,18 @@ macro_rules! re_matches_static ( macro_rules! re_bytes_matches ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::bytes::Regex::new($re).unwrap(); - let v: Vec<&[u8]> = re.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + let v: Vec<&[u8]> = re.find_iter($i).map(|m| $i.slice(m.start()..m.end())).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches)); + res } } ) @@ -252,16 +274,18 @@ macro_rules! re_bytes_matches ( macro_rules! re_bytes_matches_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex_bytes!(RE, $re); - let v: Vec<&[u8]> = RE.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect(); + let v: Vec<&[u8]> = RE.find_iter($i).map(|m| $i.slice(m.start()..m.end())).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatches)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches)); + res } } ) @@ -275,16 +299,18 @@ macro_rules! re_bytes_matches_static ( macro_rules! re_capture ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::Regex::new($re).unwrap(); if let Some(c) = re.captures($i) { - let v:Vec<&str> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let v:Vec<&str> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -299,16 +325,18 @@ macro_rules! re_capture ( macro_rules! re_capture_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex!(RE, $re); if let Some(c) = RE.captures($i) { - let v:Vec<&str> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let v:Vec<&str> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -322,16 +350,18 @@ macro_rules! re_capture_static ( macro_rules! re_bytes_capture ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::bytes::Regex::new($re).unwrap(); if let Some(c) = re.captures($i) { - let v:Vec<&[u8]> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let v:Vec<&[u8]> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -346,16 +376,18 @@ macro_rules! re_bytes_capture ( macro_rules! re_bytes_capture_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex_bytes!(RE, $re); if let Some(c) = RE.captures($i) { - let v:Vec<&[u8]> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect(); + let v:Vec<&[u8]> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); let offset = { let end = v.last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -369,16 +401,18 @@ macro_rules! re_bytes_capture_static ( macro_rules! re_captures ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::Regex::new($re).unwrap(); - let v:Vec> = re.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + let v:Vec> = re.captures_iter($i).map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap().last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -393,16 +427,18 @@ macro_rules! re_captures ( macro_rules! re_captures_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex!(RE, $re); - let v:Vec> = RE.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + let v:Vec> = RE.captures_iter($i).map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap().last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -416,16 +452,18 @@ macro_rules! re_captures_static ( macro_rules! re_bytes_captures ( ($i:expr, $re:expr) => ( { + use $crate::Slice; let re = ::regex::bytes::Regex::new($re).unwrap(); - let v:Vec> = re.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + let v:Vec> = re.captures_iter($i).map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap().last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -440,16 +478,18 @@ macro_rules! re_bytes_captures ( macro_rules! re_bytes_captures_static ( ($i:expr, $re:expr) => ( { + use $crate::Slice; regex_bytes!(RE, $re); - let v:Vec> = RE.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect(); + let v:Vec> = RE.captures_iter($i).map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); if v.len() != 0 { let offset = { let end = v.last().unwrap().last().unwrap(); end.as_ptr() as usize + end.len() - $i.as_ptr() as usize }; - $crate::IResult::Done(&$i[offset..], v) + $crate::IResult::Done($i.slice(offset..), v) } else { - $crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpCapture)) + let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture)); + res } } ) @@ -457,14 +497,13 @@ macro_rules! re_bytes_captures_static ( #[cfg(test)] mod tests { use internal::IResult::*; - use internal::Err::*; use util::ErrorKind; #[test] fn re_match() { named!(rm<&str,&str>, re_match!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatch))); assert_eq!(rm("2015-09-07blah"), Done("", "2015-09-07blah")); } @@ -473,7 +512,7 @@ mod tests { fn re_match_static() { named!(rm<&str,&str>, re_match_static!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatch))); assert_eq!(rm("2015-09-07blah"), Done("", "2015-09-07blah")); } @@ -481,7 +520,7 @@ mod tests { fn re_find() { named!(rm<&str,&str>, re_find!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpFind))); assert_eq!(rm("2015-09-07blah"), Done("blah", "2015-09-07")); } @@ -490,7 +529,7 @@ mod tests { fn re_find_static() { named!(rm<&str,&str>, re_find_static!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm("2015-09-07"), Done("", "2015-09-07")); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpFind))); assert_eq!(rm("2015-09-07blah"), Done("blah", "2015-09-07")); } @@ -498,7 +537,7 @@ mod tests { fn re_matches() { named!(rm< &str,Vec<&str> >, re_matches!(r"\d{4}-\d{2}-\d{2}")); assert_eq!(rm("2015-09-07"), Done("", vec!["2015-09-07"])); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatches))); assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"), Done("pouet", vec!["2015-09-07", "2015-09-09"])); } @@ -507,32 +546,32 @@ mod tests { fn re_matches_static() { named!(rm< &str,Vec<&str> >, re_matches_static!(r"\d{4}-\d{2}-\d{2}")); assert_eq!(rm("2015-09-07"), Done("", vec!["2015-09-07"])); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatches))); assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"), Done("pouet", vec!["2015-09-07", "2015-09-09"])); } #[test] fn re_capture() { - named!(rm< &str,Vec<&str> >, re_capture!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< &str,Vec<&str> >, re_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41"), Done(" world regex 0.1.41", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); } #[cfg(feature = "regexp_macros")] #[test] fn re_capture_static() { - named!(rm< &str,Vec<&str> >, re_capture_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< &str,Vec<&str> >, re_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41"), Done(" world regex 0.1.41", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])); } #[test] fn re_captures() { - named!(rm< &str,Vec> >, re_captures!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< &str,Vec> >, re_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]])); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41 aaa"), Done(" aaa", vec![ vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], @@ -542,9 +581,9 @@ mod tests { #[cfg(feature = "regexp_macros")] #[test] fn re_captures_static() { - named!(rm< &str,Vec> >, re_captures_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< &str,Vec> >, re_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]])); - assert_eq!(rm("blah"), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41 aaa"), Done(" aaa", vec![ vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], @@ -555,7 +594,7 @@ mod tests { fn re_bytes_match() { named!(rm, re_bytes_match!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatch))); assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b""[..], &b"2015-09-07blah"[..])); } @@ -564,7 +603,7 @@ mod tests { fn re_bytes_match_static() { named!(rm, re_bytes_match_static!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatch))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatch))); assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b""[..], &b"2015-09-07blah"[..])); } @@ -572,7 +611,7 @@ mod tests { fn re_bytes_find() { named!(rm, re_bytes_find!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpFind))); assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b"blah"[..], &b"2015-09-07"[..])); } @@ -581,7 +620,7 @@ mod tests { fn re_bytes_find_static() { named!(rm, re_bytes_find_static!(r"^\d{4}-\d{2}-\d{2}")); assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpFind))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpFind))); assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b"blah"[..], &b"2015-09-07"[..])); } @@ -589,7 +628,7 @@ mod tests { fn re_bytes_matches() { named!(rm >, re_bytes_matches!(r"\d{4}-\d{2}-\d{2}")); assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], vec![&b"2015-09-07"[..]])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatches))); assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), Done(&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])); } @@ -598,32 +637,32 @@ mod tests { fn re_bytes_matches_static() { named!(rm >, re_bytes_matches_static!(r"\d{4}-\d{2}-\d{2}")); assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], vec![&b"2015-09-07"[..]])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpMatches))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatches))); assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), Done(&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])); } #[test] fn re_bytes_capture() { - named!(rm >, re_bytes_capture!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm >, re_bytes_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), Done(&b" world regex 0.1.41"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); } #[cfg(feature = "regexp_macros")] #[test] fn re_bytes_capture_static() { - named!(rm< Vec<&[u8]> >, re_bytes_capture_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< Vec<&[u8]> >, re_bytes_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), Done(&b" world regex 0.1.41"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]])); } #[test] fn re_bytes_captures() { - named!(rm< Vec> >, re_bytes_captures!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< Vec> >, re_bytes_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]]])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), Done(&b" aaa"[..], vec![ vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]], vec![&b"regex 0.1.41"[..], &b"regex"[..], &b"0.1.41"[..], &b"0"[..], &b"1"[..], &b"41"[..]], @@ -633,9 +672,9 @@ mod tests { #[cfg(feature = "regexp_macros")] #[test] fn re_bytes_captures_static() { - named!(rm< Vec> >, re_bytes_captures_static!(r"([:alpha:]+)\s+((\d+).(\d+).(\d+))")); + named!(rm< Vec> >, re_bytes_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&b"pouet"[..], vec![vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]]])); - assert_eq!(rm(&b"blah"[..]), Error(Code(ErrorKind::RegexpCapture))); + assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpCapture))); assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), Done(&b" aaa"[..], vec![ vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]], vec![&b"regex 0.1.41"[..], &b"regex"[..], &b"0.1.41"[..], &b"0"[..], &b"1"[..], &b"41"[..]], diff --git third_party/rust/nom/src/sequence.rs third_party/rust/nom/src/sequence.rs new file mode 100644 index 000000000000..0bbefe009aa2 --- /dev/null +++ third_party/rust/nom/src/sequence.rs @@ -0,0 +1,889 @@ +/// `tuple!(I->IResult, I->IResult, ... I->IResult) => I -> IResult` +/// chains parsers and assemble the sub results in a tuple. +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser +/// and take it into account if there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Error}; +/// # #[cfg(feature = "verbose-errors")] +/// # use nom::Err::Position; +/// # use nom::ErrorKind; +/// # use nom::be_u16; +/// // the return type depends of the children parsers +/// named!(parser<&[u8], (u16, &[u8], &[u8]) >, +/// tuple!( +/// be_u16 , +/// take!(3), +/// tag!("fg") +/// ) +/// ); +/// +/// # fn main() { +/// assert_eq!( +/// parser(&b"abcdefgh"[..]), +/// Done( +/// &b"h"[..], +/// (0x6162u16, &b"cde"[..], &b"fg"[..]) +/// ) +/// ); +/// # } +/// ``` +#[macro_export] +macro_rules! tuple ( + ($i:expr, $($rest:tt)*) => ( + { + tuple_parser!($i, 0usize, (), $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! tuple_parser ( + ($i:expr, $consumed:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => ( + tuple_parser!($i, $consumed, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, $consumed:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Error(e) => + $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,o) => { + let i_ = i.clone(); + tuple_parser!(i_, + $consumed + ($crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&i)), (o), $($rest)*) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Error(e) => + $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,o) => { + let i_ = i.clone(); + tuple_parser!(i_, + $consumed + ($crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&i)), ($($parsed)* , o), $($rest)*) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:tt),*), $e:ident) => ( + tuple_parser!($i, $consumed, ($($parsed),*), call!($e)); + ); + ($i:expr, $consumed:expr, (), $submac:ident!( $($args:tt)* )) => ( + { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Error(e) => + $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,o) => { + $crate::IResult::Done(i, (o)) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => + $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,o) => { + $crate::IResult::Done(i, ($($parsed),* , o)) + } + } + } + ); + ($i:expr, $consumed:expr, ($($parsed:expr),*)) => ( + { + $crate::IResult::Done($i, ($($parsed),*)) + } + ); +); + +/// `pair!(I -> IResult, I -> IResult) => I -> IResult` +/// pair(X,Y), returns (x,y) +/// +#[macro_export] +macro_rules! pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + pair!($i, call!($f), call!($g)); + ); +); + +/// `separated_pair!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` +/// separated_pair(X,sep,Y) returns (x,y) +#[macro_export] +macro_rules! separated_pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => ( + { + match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1, (o1, _, o2)) => { + $crate::IResult::Done(i1, (o1, o2)) + } + } + } + ); + + ($i:expr, $f:expr, $($rest:tt)+) => ( + separated_pair!($i, call!($f), $($rest)*); + ); +); + +/// `preceded!(I -> IResult, I -> IResult) => I -> IResult` +/// preceded(opening, X) returns X +#[macro_export] +macro_rules! preceded( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (_,o)) => { + $crate::IResult::Done(remaining, o) + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + preceded!($i, call!($f), call!($g)); + ); +); + +/// `terminated!(I -> IResult, I -> IResult) => I -> IResult` +/// terminated(X, closing) returns X +#[macro_export] +macro_rules! terminated( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (o,_)) => { + $crate::IResult::Done(remaining, o) + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + terminated!($i, call!($f), call!($g)); + ); +); + +/// `delimited!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` +/// delimited(opening, X, closing) returns X +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done}; +/// named!(bracketed, +/// delimited!( +/// tag!("("), +/// take_until!(")"), +/// tag!(")") +/// ) +/// ); +/// +/// # fn main() { +/// let input = &b"(test)"[..]; +/// assert_eq!(bracketed(input), Done(&b""[..], &b"test"[..])); +/// # } +/// ``` +#[macro_export] +macro_rules! delimited( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => ( + { + match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1, (_, o, _)) => { + $crate::IResult::Done(i1, o) + } + } + } + ); + + ($i:expr, $f:expr, $($rest:tt)+) => ( + delimited!($i, call!($f), $($rest)*); + ); +); + +/// `do_parse!(I->IResult >> I->IResult >> ... I->IResult , ( O ) ) => I -> IResult` +/// do_parse applies sub parsers in a sequence. +/// it can store intermediary results and make them available +/// for later parsers +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser +/// and take it into account if there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Incomplete}; +/// # use nom::Needed; +/// use nom::be_u8; +/// +/// // this parser implements a common pattern in binary formats, +/// // the TAG-LENGTH-VALUE, where you first recognize a specific +/// // byte slice, then the next bytes indicate the length of +/// // the data, then you take that slice and return it +/// // +/// // here, we match the tag 42, take the length in the next byte +/// // and store it in `length`, then use `take!` with `length` +/// // to obtain the subslice that we store in `bytes`, then return +/// // `bytes` +/// named!(tag_length_value, +/// do_parse!( +/// tag!( &[ 42u8 ][..] ) >> +/// length: be_u8 >> +/// bytes: take!(length) >> +/// (bytes) +/// ) +/// ); +/// +/// # fn main() { +/// let a: Vec = vec!(42, 2, 3, 4, 5); +/// let result_a: Vec = vec!(3, 4); +/// let rest_a: Vec = vec!(5); +/// assert_eq!(tag_length_value(&a[..]), Done(&rest_a[..], &result_a[..])); +/// +/// // here, the length is 5, but there are only 3 bytes afterwards (3, 4 and 5), +/// // so the parser will tell you that you need 7 bytes: one for the tag, +/// // one for the length, then 5 bytes +/// let b: Vec = vec!(42, 5, 3, 4, 5); +/// assert_eq!(tag_length_value(&b[..]), Incomplete(Needed::Size(7))); +/// # } +/// ``` +/// +/// the result is a tuple, so you can return multiple sub results, like +/// this: +/// `do_parse!(I->IResult >> I->IResult >> ... I->IResult , ( O, P ) ) => I -> IResult` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self, Done, Incomplete}; +/// # use nom::Needed; +/// use nom::be_u8; +/// named!(tag_length_value<(u8, &[u8])>, +/// do_parse!( +/// tag!( &[ 42u8 ][..] ) >> +/// length: be_u8 >> +/// bytes: take!(length) >> +/// (length, bytes) +/// ) +/// ); +/// +/// # fn main() { +/// # } +/// ``` +/// +#[macro_export] +macro_rules! do_parse ( + (__impl $i:expr, $consumed:expr, ( $($rest:expr),* )) => ( + $crate::IResult::Done($i, ( $($rest),* )) + ); + + (__impl $i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ) => ( + do_parse!(__impl $i, $consumed, $submac!( $($args)* )) + ); + + (__impl $i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ) => ( + compiler_error!("do_parse is missing the return value. A do_parse call must end + with a return value between parenthesis, as follows: + + do_parse!( + a: tag!(\"abcd\") >> + b: tag!(\"efgh\") >> + + ( Value { a: a, b: b } ) + "); + ); + + (__impl $i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( + compiler_error!("do_parse uses >> as separator, not ~"); + ); + (__impl $i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( + compiler_error!("do_parse uses >> as separator, not ~"); + ); + (__impl $i:expr, $consumed:expr, $field:ident : $e:ident ~ $($rest:tt)*) => ( + do_parse!(__impl $i, $consumed, $field: call!($e) ~ $($rest)*); + ); + (__impl $i:expr, $consumed:expr, $e:ident ~ $($rest:tt)*) => ( + do_parse!(__impl $i, $consumed, call!($e) ~ $($rest)*); + ); + + (__impl $i:expr, $consumed:expr, $e:ident >> $($rest:tt)*) => ( + do_parse!(__impl $i, $consumed, call!($e) >> $($rest)*); + ); + (__impl $i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,_) => { + let i_ = i.clone(); + do_parse!(__impl i_, + $consumed + ($crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&i)), $($rest)*) + }, + } + } + ); + + (__impl $i:expr, $consumed:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( + do_parse!(__impl $i, $consumed, $field: call!($e) >> $($rest)*); + ); + + (__impl $i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,o) => { + let $field = o; + let i_ = i.clone(); + do_parse!(__impl i_, + $consumed + ($crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&i)), $($rest)*) + }, + } + } + ); + + // ending the chain + (__impl $i:expr, $consumed:expr, $e:ident >> ( $($rest:tt)* )) => ( + do_parse!(__impl $i, $consumed, call!($e) >> ( $($rest)* )); + ); + + (__impl $i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,_) => { + $crate::IResult::Done(i, ( $($rest)* )) + }, + } + ); + + (__impl $i:expr, $consumed:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( + do_parse!(__impl $i, $consumed, $field: call!($e) >> ( $($rest)* ) ); + ); + + (__impl $i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => { + let (needed,overflowed) = $consumed.overflowing_add(i); + match overflowed { + true => $crate::IResult::Incomplete($crate::Needed::Unknown), + false => $crate::IResult::Incomplete($crate::Needed::Size(needed)), + } + }, + $crate::IResult::Done(i,o) => { + let $field = o; + $crate::IResult::Done(i, ( $($rest)* )) + }, + } + ); + + ($i:expr, $($rest:tt)*) => ( + { + do_parse!(__impl $i, 0usize, $($rest)*) + } + ); + ($submac:ident!( $($args:tt)* ) >> $($rest:tt)* ) => ( + compiler_error!("if you are using do_parse outside of a named! macro, you must + pass the input data as first argument, like this: + + let res = do_parse!(input, + a: tag!(\"abcd\") >> + b: tag!(\"efgh\") >> + ( Value { a: a, b: b } ) + );"); + ); + ($e:ident! >> $($rest:tt)* ) => ( + do_parse!( call!($e) >> $($rest)*); + ); +); + +#[cfg(test)] +mod tests { + use internal::{Needed,IResult}; + use internal::IResult::*; + use util::ErrorKind; + use nom::be_u16; + + #[cfg(feature = "verbose-errors")] + use verbose_errors::Err; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use std::cmp::min; + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: $crate::IResult<_,_> = if reduced != b { + $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) + } else if m < blen { + $crate::IResult::Incomplete($crate::Needed::Size(blen)) + } else { + $crate::IResult::Done(&$i[blen..], reduced) + }; + res + } + ); + ); + + macro_rules! take ( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt { + $crate::IResult::Incomplete($crate::Needed::Size(cnt)) + } else { + $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) + }; + res + } + ); + ); + + #[derive(PartialEq,Eq,Debug)] + struct B { + a: u8, + b: u8 + } + + #[derive(PartialEq,Eq,Debug)] + struct C { + a: u8, + b: Option + } + + #[cfg(feature = "verbose-errors")] + use util::{error_to_list, add_error_pattern, print_error}; + + #[cfg(feature = "verbose-errors")] + fn error_to_string

(e: &Err

) -> &'static str { + let v:Vec = error_to_list(e); + // do it this way if you can use slice patterns + /* + match &v[..] { + [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag", + [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`", + _ => "unrecognized error" + } + */ + if &v[..] == [ErrorKind::Custom(42),ErrorKind::Tag] { + "missing `ijkl` tag" + } else if &v[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] { + "missing `mnop` tag after `ijkl`" + } else { + "unrecognized error" + } + } + + // do it this way if you can use box patterns + /*use std::str; + fn error_to_string(e:Err) -> String + match e { + NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => { + format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap()) + }, + NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => { + format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap()) + }, + _ => "unrecognized error".to_string() + } + }*/ + + #[cfg(feature = "verbose-errors")] + use std::collections; + + #[cfg(feature = "verbose-errors")] + #[test] + fn err() { + named!(err_test, alt!( + tag!("abcd") | + preceded!(tag!("efgh"), return_error!(ErrorKind::Custom(42), + do_parse!( + tag!("ijkl") >> + res: return_error!(ErrorKind::Custom(128), tag!("mnop")) >> + (res) + ) + ) + ) + )); + let a = &b"efghblah"[..]; + let b = &b"efghijklblah"[..]; + let c = &b"efghijklmnop"[..]; + + let blah = &b"blah"[..]; + + let res_a = err_test(a); + let res_b = err_test(b); + let res_c = err_test(c); + assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah)))); + assert_eq!(res_b, Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..], error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah))))); + assert_eq!(res_c, Done(&b""[..], &b"mnop"[..])); + + // Merr-like error matching + let mut err_map = collections::HashMap::new(); + assert!(add_error_pattern(&mut err_map, err_test(&b"efghpouet"[..]), "missing `ijkl` tag")); + assert!(add_error_pattern(&mut err_map, err_test(&b"efghijklpouet"[..]), "missing `mnop` tag after `ijkl`")); + + let res_a2 = res_a.clone(); + match res_a { + Error(e) => { + assert_eq!(error_to_list(&e), [ErrorKind::Custom(42), ErrorKind::Tag]); + assert_eq!(error_to_string(&e), "missing `ijkl` tag"); + assert_eq!(err_map.get(&error_to_list(&e)), Some(&"missing `ijkl` tag")); + }, + _ => panic!() + }; + + let res_b2 = res_b.clone(); + match res_b { + Error(e) => { + assert_eq!(error_to_list(&e), [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag]); + assert_eq!(error_to_string(&e), "missing `mnop` tag after `ijkl`"); + assert_eq!(err_map.get(&error_to_list(&e)), Some(&"missing `mnop` tag after `ijkl`")); + }, + _ => panic!() + }; + + print_error(a, res_a2); + print_error(b, res_b2); + } + + #[allow(unused_variables)] + #[test] + fn add_err() { + named!(err_test, + preceded!(tag!("efgh"), add_return_error!(ErrorKind::Custom(42), + do_parse!( + tag!("ijkl") >> + res: add_return_error!(ErrorKind::Custom(128), tag!("mnop")) >> + (res) + ) + ) + )); + let a = &b"efghblah"[..]; + let b = &b"efghijklblah"[..]; + let c = &b"efghijklmnop"[..]; + + let blah = &b"blah"[..]; + + let res_a = err_test(a); + let res_b = err_test(b); + let res_c = err_test(c); + assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah)))); + assert_eq!(res_b, Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..], error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah))))); + assert_eq!(res_c, Done(&b""[..], &b"mnop"[..])); + } + + #[test] + fn complete() { + named!(err_test, + do_parse!( + tag!("ijkl") >> + res: complete!(tag!("mnop")) >> + (res) + ) + ); + let a = &b"ijklmn"[..]; + + let res_a = err_test(a); + assert_eq!(res_a, Error(error_position!(ErrorKind::Complete, &b"mn"[..]))); + } + + #[test] + fn pair() { + named!( tag_abc, tag!("abc") ); + named!( tag_def, tag!("def") ); + named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) ); + + assert_eq!(pair_abc_def(&b"abcdefghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))); + assert_eq!(pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(pair_abc_def(&b"abcd"[..]), Incomplete(Needed::Size(6))); + assert_eq!(pair_abc_def(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(pair_abc_def(&b"xxxdef"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxdef"[..]))); + assert_eq!(pair_abc_def(&b"abcxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn separated_pair() { + named!( tag_abc, tag!("abc") ); + named!( tag_def, tag!("def") ); + named!( tag_separator, tag!(",") ); + named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); + + assert_eq!(sep_pair_abc_def(&b"abc,defghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))); + assert_eq!(sep_pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Incomplete(Needed::Size(7))); + assert_eq!(sep_pair_abc_def(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(sep_pair_abc_def(&b"xxx,def"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx,def"[..]))); + assert_eq!(sep_pair_abc_def(&b"abc,xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn preceded() { + named!( tag_abcd, tag!("abcd") ); + named!( tag_efgh, tag!("efgh") ); + named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); + + assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"efgh"[..])); + assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8))); + assert_eq!(preceded_abcd_efgh(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(preceded_abcd_efgh(&b"xxxxdef"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxxdef"[..]))); + assert_eq!(preceded_abcd_efgh(&b"abcdxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn terminated() { + named!( tag_abcd, tag!("abcd") ); + named!( tag_efgh, tag!("efgh") ); + named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); + + assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"abcd"[..])); + assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4))); + assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8))); + assert_eq!(terminated_abcd_efgh(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(terminated_abcd_efgh(&b"xxxxdef"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxxdef"[..]))); + assert_eq!(terminated_abcd_efgh(&b"abcdxxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxx"[..]))); + } + + #[test] + fn delimited() { + named!( tag_abc, tag!("abc") ); + named!( tag_def, tag!("def") ); + named!( tag_ghi, tag!("ghi") ); + named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); + + assert_eq!(delimited_abc_def_ghi(&b"abcdefghijkl"[..]), Done(&b"jkl"[..], &b"def"[..])); + assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Incomplete(Needed::Size(3))); + assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Incomplete(Needed::Size(6))); + assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Incomplete(Needed::Size(9))); + assert_eq!(delimited_abc_def_ghi(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"xxxdefghi"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxdefghi"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"abcxxxghi"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxghi"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"abcdefxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))); + } + + #[test] + fn tuple_test() { + named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, + tuple!( be_u16 , take!(3), tag!("fg") ) ); + + assert_eq!(tuple_3(&b"abcdefgh"[..]), Done(&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..]))); + assert_eq!(tuple_3(&b"abcd"[..]), Incomplete(Needed::Size(5))); + assert_eq!(tuple_3(&b"abcde"[..]), Incomplete(Needed::Size(7))); + assert_eq!(tuple_3(&b"abcdejk"[..]), Error(error_position!(ErrorKind::Tag, &b"jk"[..]))); + } + + #[test] + fn do_parse() { + fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; + fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) }; + + //trace_macros!(true); + named!(do_parser<&[u8], (u8, u8)>, + do_parse!( + tag!("abcd") >> + opt!(tag!("abcd")) >> + aa: ret_int1 >> + tag!("efgh") >> + bb: ret_int2 >> + tag!("efgh") >> + (aa, bb) + ) + ); + //named!(do_parser<&[u8], (u8, u8)>, + // do_parse!( + // tag!("abcd") >> aa: ret_int1 >> tag!("efgh") >> bb: ret_int2 >> tag!("efgh") >> (aa, bb) + // ) + //); + + //trace_macros!(false); + + assert_eq!(do_parser(&b"abcdabcdefghefghX"[..]), Done(&b"X"[..], (1, 2))); + assert_eq!(do_parser(&b"abcdefghefghX"[..]), Done(&b"X"[..], (1, 2))); + assert_eq!(do_parser(&b"abcdab"[..]), Incomplete(Needed::Size(8))); + assert_eq!(do_parser(&b"abcdefghef"[..]), Incomplete(Needed::Size(12))); + } + + #[test] + fn do_parse_dependency() { + use nom::be_u8; + + named!(length_value, + do_parse!( + length: be_u8 >> + bytes: take!(length) >> + (bytes) + ) + ); + + let a = [2u8, 3, 4, 5]; + let res_a = [3u8, 4]; + assert_eq!(length_value(&a[..]), Done(&a[3..], &res_a[..])); + let b = [5u8, 3, 4, 5]; + assert_eq!(length_value(&b[..]), Incomplete(Needed::Size(6))); + } + + /* + named!(does_not_compile, + do_parse!( + length: be_u8 >> + bytes: take!(length) + ) + ); + named!(does_not_compile_either, + do_parse!( + length: be_u8 ~ + bytes: take!(length) ~ + ( () ) + ) + ); + fn still_does_not_compile() { + let data = b"abcd"; + + let res = do_parse!( + tag!("abcd") >> + tag!("efgh") >> + ( () ) + ); + } + */ +} diff --git third_party/rust/nom/src/simple_errors.rs third_party/rust/nom/src/simple_errors.rs new file mode 100644 index 000000000000..04bddd4309bf --- /dev/null +++ third_party/rust/nom/src/simple_errors.rs @@ -0,0 +1,153 @@ +//! Error management +//! +//! there are two ways to handle errors in nom. The first one, +//! activated by default, uses the `nom::ErrorKind` enum +//! in the error branch of `IResult`. This enum can hold either +//! a parser specific error code, or a custom error type you +//! specify. +//! +//! If you need more advanced error management, you can activate +//! the "verbose-errors" compilation feature, which will give you +//! the error system available in nom 1.0. The verbose errors +//! accumulate error codes and positions as you backtrack through +//! the parser tree. From there, you can precisely identify which +//! parts of the input triggered the error case. +//! +//! Please note that the verbose error management is a bit slower +//! than the simple one. +use util::ErrorKind; +use internal::{IResult, IError}; +use internal::IResult::*; + +pub type Err = ErrorKind; + +impl IResult { + /// Maps a `IResult` to `IResult` by appling a function + /// to a contained `Error` value, leaving `Done` and `Incomplete` value + /// untouched. + #[inline] + pub fn map_err(self, f: F) -> IResult + where F: FnOnce(Err) -> Err { + match self { + Error(e) => Error(f(e)), + Incomplete(n) => Incomplete(n), + Done(i, o) => Done(i, o), + } + } + + /// Unwrap the contained `Error(E)` value, or panic if the `IResult` is not + /// `Error`. + pub fn unwrap_err(self) -> Err { + match self { + Error(e) => e, + Done(_, _) => panic!("unwrap_err() called on an IResult that is Done"), + Incomplete(_) => panic!("unwrap_err() called on an IResult that is Incomplete"), + } + } + + /// Convert the IResult to a std::result::Result + pub fn to_full_result(self) -> Result> { + match self { + Done(_, o) => Ok(o), + Incomplete(n) => Err(IError::Incomplete(n)), + Error(e) => Err(IError::Error(e)) + } + } + + /// Convert the IResult to a std::result::Result, or panic if the `IResult` is `Incomplete` + pub fn to_result(self) -> Result> { + match self { + Done(_, o) => Ok(o), + Error(e) => Err(e), + Incomplete(_) => panic!("to_result() called on an IResult that is Incomplete") + } + } +} + +#[cfg(feature = "std")] +use std::any::Any; +#[cfg(feature = "std")] +use std::{error,fmt}; +#[cfg(feature = "std")] +impl error::Error for Err { + fn description(&self) -> &str { + self.description() + } +} + +#[cfg(feature = "std")] +impl fmt::Display for Err { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.description()) + } +} + +/// translate parser result from IResult to IResult with a custom type +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::IResult::Error; +/// # use nom::ErrorKind; +/// # fn main() { +/// // will add a Custom(42) error to the error chain +/// named!(err_test, add_return_error!(ErrorKind::Custom(42), tag!("abcd"))); +/// // Convert to IREsult<&[u8], &[u8], &str> +/// named!(parser<&[u8], &[u8], &str>, add_return_error!(ErrorKind::Custom("custom error message"), fix_error!(&str, err_test))); +/// +/// let a = &b"efghblah"[..]; +/// let res_a = parser(a); +/// assert_eq!(res_a, Error(error_node_position!( ErrorKind::Custom("custom error message"), a, Position(ErrorKind::Fix, a)))); +/// # } +/// ``` +#[macro_export] +macro_rules! fix_error ( + ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(_) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::IResult::Error(e) + } + } + } + ); + ($i:expr, $t:ty, $f:expr) => ( + fix_error!($i, $t, call!($f)); + ); +); + +/// `flat_map!(R -> IResult, S -> IResult) => R -> IResult` +/// +/// combines a parser R -> IResult and +/// a parser S -> IResult to return another +/// parser R -> IResult +#[macro_export] +macro_rules! flat_map( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), + $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(ref i2)) => $crate::IResult::Incomplete($crate::Needed::Size(*i2)), + $crate::IResult::Done(_, o2) => $crate::IResult::Done(i, o2) + } + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + flat_map!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $g:expr) => ( + flat_map!($i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + flat_map!($i, call!($f), $submac!($($args)*)); + ); +); diff --git third_party/rust/nom/src/str.rs third_party/rust/nom/src/str.rs index 768786edaba1..4f8643bd3e68 100644 --- third_party/rust/nom/src/str.rs +++ third_party/rust/nom/src/str.rs @@ -21,15 +21,32 @@ macro_rules! tag_s ( ($i:expr, $tag: expr) => ( { - let res: $crate::IResult<_,_> = if $tag.len() > $i.len() { - $crate::IResult::Incomplete($crate::Needed::Size($tag.len())) - //} else if &$i[0..$tag.len()] == $tag { - } else if ($i).starts_with($tag) { - $crate::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()]) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TagStr, $i)) - }; - res + tag!($i, $tag) + } + ); +); + +/// `tag_no_case_s!(&str) => &str -> IResult<&str, &str>` +/// declares a case-insensitive string as a suite to recognize +/// +/// consumes the recognized characters +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::{self,Done}; +/// # fn main() { +/// fn test(input: &str) -> IResult<&str, &str> { +/// tag_no_case_s!(input, "ABcd") +/// } +/// let r = test("aBCdefgh"); +/// assert_eq!(r, Done("efgh", "aBCd")); +/// # } +/// ``` +#[macro_export] +macro_rules! tag_no_case_s ( + ($i:expr, $tag: expr) => ( + { + tag_no_case!($i, $tag) } ); ); @@ -47,28 +64,19 @@ macro_rules! tag_s ( /// let a = "abcdefgh"; /// /// assert_eq!(take5(a), Done("fgh", "abcde")); +/// +/// let b = "12345"; +/// +/// assert_eq!(take5(b), Done("", "12345")); /// # } /// ``` #[macro_export] macro_rules! take_s ( ($i:expr, $count:expr) => ( { + let input = $i; let cnt = $count as usize; - let res: $crate::IResult<_,_> = if $i.chars().count() < cnt { - $crate::IResult::Incomplete($crate::Needed::Size(cnt)) - } else { - let mut offset = $i.len(); - let mut count = 0; - for (o, _) in $i.char_indices() { - if count == cnt { - offset = o; - break; - } - count += 1; - } - $crate::IResult::Done(&$i[offset..], &$i[..offset]) - }; - res + take!(input, cnt) } ); ); @@ -91,22 +99,7 @@ macro_rules! take_s ( macro_rules! is_not_s ( ($input:expr, $arr:expr) => ( { - use std::collections::HashSet; - let set: HashSet = $arr.chars().collect(); - let mut offset = $input.len(); - for (o, c) in $input.char_indices() { - if set.contains(&c) { - offset = o; - break; - } - } - if offset == 0 { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsAStr,$input)) - } else if offset < $input.len() { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Done("", $input) - } + is_not!($input, $arr) } ); ); @@ -131,22 +124,7 @@ macro_rules! is_not_s ( macro_rules! is_a_s ( ($input:expr, $arr:expr) => ( { - use std::collections::HashSet; - let set: HashSet = $arr.chars().collect(); - let mut offset = $input.len(); - for (o, c) in $input.char_indices() { - if !set.contains(&c) { - offset = o; - break; - } - } - if offset == 0 { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::IsAStr,$input)) - } else if offset < $input.len() { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Done("", $input) - } + is_a!($input, $arr) } ); ); @@ -173,18 +151,7 @@ macro_rules! is_a_s ( macro_rules! take_while_s ( ($input:expr, $submac:ident!( $($args:tt)* )) => ( { - let mut offset = $input.len(); - for (o, c) in $input.char_indices() { - if !$submac!(c, $($args)*) { - offset = o; - break; - } - } - if offset < $input.len() { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Done("", $input) - } + take_while!($input, $submac!($($args)*)) } ); ($input:expr, $f:expr) => ( @@ -211,22 +178,7 @@ macro_rules! take_while_s ( #[macro_export] macro_rules! take_while1_s ( ($input:expr, $submac:ident!( $($args:tt)* )) => ( - { - let mut offset = $input.len(); - for (o, c) in $input.char_indices() { - if !$submac!(c, $($args)*) { - offset = o; - break; - } - } - if offset == 0 { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeWhile1Str,$input)) - } else if offset < $input.len() { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Done("", $input) - } - } + take_while1!($input, $submac!($($args)*)) ); ($input:expr, $f:expr) => ( take_while1_s!($input, call!($f)); @@ -234,27 +186,15 @@ macro_rules! take_while1_s ( ); -/// `take_till_s!(&str -> bool) => &str -> IResult<&str, &str>` +/// `take_till_s!(char -> bool) => &str -> IResult<&str, &str>` /// returns the longest list of characters until the provided function succeeds /// /// The argument is either a function `char -> bool` or a macro returning a `bool #[macro_export] macro_rules! take_till_s ( ($input:expr, $submac:ident!( $($args:tt)* )) => ( - { - let mut offset = $input.len(); - for (o, c) in $input.char_indices() { - if $submac!(c, $($args)*) { - offset = o; - break; - } - } - if offset < $input.len() { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Done("", $input) - } + take_till!($input, $submac!($($args)*)) } ); ($input:expr, $f:expr) => ( @@ -262,48 +202,29 @@ macro_rules! take_till_s ( ); ); +/// `take_till1_s!(char -> bool) => &str -> IResult<&str, &str>` +/// returns the longest non empty list of characters until the provided function succeeds +/// +/// The argument is either a function `char -> bool` or a macro returning a `bool +#[macro_export] +macro_rules! take_till1_s ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ( + { + take_till1!($input, $submac!($($args)*)) + } + ); + ($input:expr, $f:expr) => ( + take_till1_s!($input, call!($f)); + ); +); + /// `take_until_and_consume_s!(&str) => &str -> IResult<&str, &str>` /// generates a parser consuming all chars until the specified string is found and consumes it #[macro_export] macro_rules! take_until_and_consume_s ( ($input:expr, $substr:expr) => ( { - #[inline(always)] - fn shift_window_and_cmp(window: & mut ::std::vec::Vec, c: char, substr_vec: & ::std::vec::Vec) -> bool { - window.push(c); - if window.len() > substr_vec.len() { - window.remove(0); - } - window == substr_vec - } - let res: $crate::IResult<_, _> = if $substr.len() > $input.len() { - $crate::IResult::Incomplete($crate::Needed::Size($substr.len())) - } else { - let substr_vec: ::std::vec::Vec = $substr.chars().collect(); - let mut window: ::std::vec::Vec = vec![]; - let mut offset = $input.len(); - let mut parsed = false; - for (o, c) in $input.char_indices() { - if parsed { - // The easiest way to get the byte offset of the char after the found string - offset = o; - break; - } - if shift_window_and_cmp(& mut window, c, &substr_vec) { - parsed = true; - } - } - if parsed { - if offset < $input.len() { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Done("", $input) - } - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilAndConsumeStr,$input)) - } - }; - res + take_until_and_consume!($input, $substr) } ); ); @@ -314,39 +235,7 @@ macro_rules! take_until_and_consume_s ( macro_rules! take_until_s ( ($input:expr, $substr:expr) => ( { - #[inline(always)] - fn shift_window_and_cmp(window: & mut Vec, c: char, substr_vec: &Vec) -> bool { - window.push(c); - if window.len() > substr_vec.len() { - window.remove(0); - } - window == substr_vec - } - let res: $crate::IResult<&str, &str> = if $substr.len() > $input.len() { - $crate::IResult::Incomplete($crate::Needed::Size($substr.len())) - } else { - let substr_vec: Vec = $substr.chars().collect(); - let mut window: Vec = vec![]; - let mut offset = $input.len(); - let mut parsed = false; - for (o, c) in $input.char_indices() { - if shift_window_and_cmp(& mut window, c, &substr_vec) { - parsed = true; - window.pop(); - let window_len: usize = window.iter() - .map(|x| x.len_utf8()) - .fold(0, |x, y| x + y); - offset = o - window_len; - break; - } - } - if parsed { - $crate::IResult::Done(&$input[offset..], &$input[..offset]) - } else { - $crate::IResult::Error($crate::Err::Position($crate::ErrorKind::TakeUntilStr,$input)) - } - }; - res + take_until!($input, $substr) } ); ); @@ -451,8 +340,8 @@ mod test { } } - use internal::IResult::{Done, Error}; - use internal::Err::Position; + use internal::IResult::{Done, Error, Incomplete}; + use internal::Needed; use util::ErrorKind; pub fn is_alphabetic(c:char) -> bool { @@ -480,10 +369,10 @@ mod test { let c = "abcd123"; let d = "123"; - assert_eq!(f(&a[..]), Error(Position(ErrorKind::TakeWhile1Str, &""[..]))); + assert_eq!(f(&a[..]), Incomplete(Needed::Size(1))); assert_eq!(f(&b[..]), Done(&a[..], &b[..])); assert_eq!(f(&c[..]), Done(&"123"[..], &b[..])); - assert_eq!(f(&d[..]), Error(Position(ErrorKind::TakeWhile1Str, &d[..]))); + assert_eq!(f(&d[..]), Error(error_position!(ErrorKind::TakeWhile1, &d[..]))); } #[test] @@ -557,16 +446,16 @@ mod test { fn take_until_and_consume_s_succeed() { const INPUT: &'static str = "βèƒôřèÂßÇáƒƭèř"; const FIND: &'static str = "ÂßÇ"; - const CONSUMED: &'static str = "βèƒôřèÂßÇ"; + const OUTPUT: &'static str = "βèƒôřè"; const LEFTOVER: &'static str = "áƒƭèř"; match take_until_and_consume_s!(INPUT, FIND) { IResult::Done(extra, output) => { assert!(extra == LEFTOVER, "Parser `take_until_and_consume_s`\ consumed leftover input. Leftover `{}`.", extra); - assert!(output == CONSUMED, "Parser `take_until_and_consume_s`\ - doens't return the string it consumed on success. Expected `{}`, got `{}`.", - CONSUMED, output); + assert!(output == OUTPUT, "Parser `take_until_and_consume_s`\ + doens't return the string it selected on success. Expected `{}`, got `{}`.", + OUTPUT, output); } other => panic!("Parser `take_until_and_consume_s` didn't succeed when it should have. \ Got `{:?}`.", other), @@ -731,4 +620,39 @@ mod test { Got `{:?}`.", other), }; } + + #[test] + #[cfg(feature = "std")] + fn recognize_is_a_s() { + let a = "aabbab"; + let b = "ababcd"; + + named!(f <&str,&str>, recognize!(many1!(alt!( tag_s!("a") | tag_s!("b") )))); + + assert_eq!(f(&a[..]), Done(&a[6..], &a[..])); + assert_eq!(f(&b[..]), Done(&b[4..], &b[..4])); + + } + + #[test] + fn utf8_indexing() { + named!(dot(&str) -> &str, + tag_s!(".") + ); + + dot("點"); + } + + #[test] + fn case_insensitive() { + named!(test<&str,&str>, tag_no_case!("ABcd")); + assert_eq!(test("aBCdefgh"), Done("efgh", "aBCd")); + assert_eq!(test("abcdefgh"), Done("efgh", "abcd")); + assert_eq!(test("ABCDefgh"), Done("efgh", "ABCD")); + + named!(test2<&str,&str>, tag_no_case!("ABcd")); + assert_eq!(test2("aBCdefgh"), Done("efgh", "aBCd")); + assert_eq!(test2("abcdefgh"), Done("efgh", "abcd")); + assert_eq!(test2("ABCDefgh"), Done("efgh", "ABCD")); + } } diff --git third_party/rust/nom/src/stream.rs third_party/rust/nom/src/stream.rs index 38d5c870c938..4d92bed75c8e 100644 --- third_party/rust/nom/src/stream.rs +++ third_party/rust/nom/src/stream.rs @@ -212,9 +212,11 @@ impl<'x,'b> Producer<'b,&'x[u8],Move> for MemProducer<'x> { } } { - use std::cmp; - let end = cmp::min(self.index + self.chunk_size, self.length); - consumer.handle(Input::Element(&self.buffer[self.index..end])) + if self.index + self.chunk_size > self.length { + consumer.handle(Input::Eof(Some(&self.buffer[self.index..self.length]))) + } else { + consumer.handle(Input::Element(&self.buffer[self.index..self.index + self.chunk_size])) + } } else { consumer.state() } @@ -259,7 +261,15 @@ impl FileProducer { shift(&mut self.v, self.start, self.end); self.end = self.end - self.start; self.start = 0; - match self.file.read(&mut self.v[self.end..]) { + + let remaining = &mut self.v[self.end..]; + + // already full, prevents erroneous Eof below + if remaining.is_empty() { + return Some(0); + } + + match self.file.read(remaining) { Err(_) => { self.state = FileProducerState::Error; None @@ -494,7 +504,7 @@ macro_rules! consumer_from_parser ( impl $crate::Consumer<$input, $output, (), $crate::Move> for $name { fn handle(&mut self, input: $crate::Input<$input>) -> & $crate::ConsumerState<$output, (), $crate::Move> { - use $crate::HexDisplay; + use $crate::Offset; match input { $crate::Input::Empty | $crate::Input::Eof(None) => &self.state, $crate::Input::Element(sl) | $crate::Input::Eof(Some(sl)) => { @@ -537,7 +547,7 @@ macro_rules! consumer_from_parser ( impl<'a> $crate::Consumer<&'a[u8], $output, (), $crate::Move> for $name { fn handle(&mut self, input: $crate::Input<&'a[u8]>) -> & $crate::ConsumerState<$output, (), $crate::Move> { - use $crate::HexDisplay; + use $crate::Offset; match input { $crate::Input::Empty | $crate::Input::Eof(None) => &self.state, $crate::Input::Element(sl) | $crate::Input::Eof(Some(sl)) => { @@ -577,7 +587,7 @@ macro_rules! consumer_from_parser ( mod tests { use super::*; use internal::IResult; - use util::HexDisplay; + use util::Offset; use std::str::from_utf8; use std::io::SeekFrom; @@ -897,8 +907,8 @@ mod tests { } }*/ - fn lf(i:& u8) -> bool { - *i == '\n' as u8 + fn lf(i:u8) -> bool { + i == '\n' as u8 } fn to_utf8_string(input:&[u8]) -> String { String::from(from_utf8(input).unwrap()) @@ -938,6 +948,27 @@ mod tests { //assert!(false); } + #[test] + fn small_buffer() { + let mut f = FileProducer::new("LICENSE", 10 /* smaller than a line */).unwrap(); + let mut a = LineConsumer::new(); + + for i in 0..2 { + match f.apply(&mut a) { + &ConsumerState::Continue(Move::Await(_)) => {} + _ => assert!(false, "LineConsumer should be awaiting more input: {}", i), + } + assert_eq!(FileProducerState::Normal, f.state()); + } + + f.resize(200 /* large enough for a line */); + match f.apply(&mut a) { + &ConsumerState::Done(..) => {} + _ => assert!(false, "LineConsumer should have succeeded"), + } + assert_eq!(FileProducerState::Normal, f.state()); + } + #[derive(Debug,Clone,Copy,PartialEq,Eq)] enum SeekState { Begin, diff --git third_party/rust/nom/src/traits.rs third_party/rust/nom/src/traits.rs new file mode 100644 index 000000000000..e8103336b147 --- /dev/null +++ third_party/rust/nom/src/traits.rs @@ -0,0 +1,623 @@ +//! Traits input types have to implement to work with nom combinators +//! +use std::ops::{Range,RangeTo,RangeFrom,RangeFull}; +use std::iter::Enumerate; +use std::slice::Iter; + +use std::str::Chars; +use std::str::CharIndices; +use std::str::FromStr; +use std::str::from_utf8; + +use memchr; + + +/// abstract method to calculate the input length +pub trait InputLength { + /// calculates the input length, as indicated by its name, + /// and the name of the trait itself + #[inline] + fn input_len(&self) -> usize; +} + +impl<'a, T> InputLength for &'a[T] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for &'a str { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for (&'a [u8], usize) { + #[inline] + fn input_len(&self) -> usize { + //println!("bit input length for ({:?}, {}):", self.0, self.1); + let res = self.0.len() * 8 - self.1; + //println!("-> {}", res); + res + } +} + +/// transforms common types to a char for basic token parsing +pub trait AsChar { + /// makes a char from self + #[inline] + fn as_char(self) -> char; + + /// tests that self is an alphabetic character + /// + /// warning: for `&str` it recognizes alphabetic + /// characters outside of the 52 ASCII letters + #[inline] + fn is_alpha(self) -> bool; + + /// tests that self is an alphabetic character + /// or a decimal digit + #[inline] + fn is_alphanum(self) -> bool; + /// tests that self is a decimal digit + #[inline] + fn is_dec_digit(self) -> bool; + /// tests that self is an hex digit + #[inline] + fn is_hex_digit(self) -> bool; + /// tests that self is an octal digit + #[inline] + fn is_oct_digit(self) -> bool; + /// gets the len in bytes for self + #[inline] + fn len(self) -> usize; +} + +impl AsChar for u8 { + #[inline] + fn as_char(self) -> char { self as char } + #[inline] + fn is_alpha(self) -> bool { + (self >= 0x41 && self <= 0x5A) || (self >= 0x61 && self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { self.is_alpha() || self.is_dec_digit() } + #[inline] + fn is_dec_digit(self) -> bool { + self >= 0x30 && self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (self >= 0x30 && self <= 0x39) || + (self >= 0x41 && self <= 0x46) || + (self >= 0x61 && self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + self >= 0x30 && self <= 0x37 + } + #[inline] + fn len(self) -> usize { + 1 + } +} +impl<'a> AsChar for &'a u8 { + #[inline] + fn as_char(self) -> char { *self as char } + #[inline] + fn is_alpha(self) -> bool { + (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { self.is_alpha() || self.is_dec_digit() } + #[inline] + fn is_dec_digit(self) -> bool { + *self >= 0x30 && *self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (*self >= 0x30 && *self <= 0x39) || + (*self >= 0x41 && *self <= 0x46) || + (*self >= 0x61 && *self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + *self >= 0x30 && *self <= 0x37 + } + #[inline] + fn len(self) -> usize { + 1 + } +} + +impl AsChar for char { + #[inline] + fn as_char(self) -> char { self } + #[inline] + fn is_alpha(self) -> bool { self.is_alphabetic() } + #[inline] + fn is_alphanum(self) -> bool { self.is_alpha() || self.is_dec_digit() } + #[inline] + fn is_dec_digit(self) -> bool { self.is_digit(10) } + #[inline] + fn is_hex_digit(self) -> bool { self.is_digit(16) } + #[inline] + fn is_oct_digit(self) -> bool { self.is_digit(8) } + #[inline] + fn len(self) -> usize { self.len_utf8() } +} + +impl<'a> AsChar for &'a char { + #[inline] + fn as_char(self) -> char { self.clone() } + #[inline] + fn is_alpha(self) -> bool { self.is_alphabetic() } + #[inline] + fn is_alphanum(self) -> bool { self.is_alpha() || self.is_dec_digit() } + #[inline] + fn is_dec_digit(self) -> bool { self.is_digit(10) } + #[inline] + fn is_hex_digit(self) -> bool { self.is_digit(16) } + #[inline] + fn is_oct_digit(self) -> bool { self.is_digit(8) } + #[inline] + fn len(self) -> usize { self.len_utf8() } +} + +/// abstracts common iteration operations on the input type +/// +/// it needs a distinction between `Item` and `RawItem` because +/// `&[T]` iterates on references +pub trait InputIter { + type Item; + type RawItem; + type Iter : Iterator; + type IterElem : Iterator; + + /// returns an iterator over the elements and their byte offsets + fn iter_indices(&self) -> Self::Iter; + /// returns an iterator over the elements + fn iter_elements(&self) -> Self::IterElem; + /// finds the byte position of the element + fn position

(&self, predicate: P) -> Option where P: Fn(Self::RawItem) -> bool; + /// get the byte offset from the element's position in the stream + fn slice_index(&self, count:usize) -> Option; +} + +/// abstracts slicing operations +pub trait InputTake { + /// returns a slice of `count` bytes + fn take

(&self, count: usize) -> Option<&Self>; + /// split the stream at the `count` byte offset + fn take_split

(&self, count: usize) -> Option<(&Self,&Self)>; +} + +impl<'a> InputIter for &'a [u8] { + type Item = &'a u8; + type RawItem = u8; + type Iter = Enumerate>; + type IterElem = Iter<'a, Self::RawItem>; + + #[inline] + fn iter_indices(&self) -> Self::Iter { + self.iter().enumerate() + } + #[inline] + fn iter_elements(&self) -> Self::IterElem { + self.iter() + } + #[inline] + fn position

(&self, predicate: P) -> Option where P: Fn(Self::RawItem) -> bool { + self.iter().position(|b| predicate(*b)) + } + #[inline] + fn slice_index(&self, count:usize) -> Option { + if self.len() >= count { + Some(count) + } else { + None + } + } +} + +impl InputTake for [u8] { + #[inline] + fn take

(&self, count: usize) -> Option<&Self> { + if self.len() >= count { + Some(&self[0..count]) + } else { + None + } + } + #[inline] + fn take_split

(&self, count: usize) -> Option<(&Self,&Self)> { + if self.len() >= count { + Some((&self[count..],&self[..count])) + } else { + None + } + } +} + +impl<'a> InputIter for &'a str { + type Item = char; + type RawItem = char; + type Iter = CharIndices<'a>; + type IterElem = Chars<'a>; + #[inline] + fn iter_indices(&self) -> Self::Iter { + self.char_indices() + } + #[inline] + fn iter_elements(&self) -> Self::IterElem { + self.chars() + } + fn position

(&self, predicate: P) -> Option where P: Fn(Self::RawItem) -> bool { + for (o,c) in self.char_indices() { + if predicate(c) { + return Some(o) + } + } + None + } + #[inline] + fn slice_index(&self, count:usize) -> Option { + let mut cnt = 0; + for (index, _) in self.char_indices() { + if cnt == count { + return Some(index) + } + cnt += 1; + } + if cnt == count { + return Some(self.len()) + } + None + } +} + +impl InputTake for str { + #[inline] + fn take

(&self, count: usize) -> Option<&Self> { + let mut cnt = 0; + for (index, _) in self.char_indices() { + if cnt == count { + return Some(&self[..index]) + } + cnt += 1; + } + None + } + + // return byte index + #[inline] + fn take_split

(&self, count: usize) -> Option<(&Self,&Self)> { + let mut cnt = 0; + for (index, _) in self.char_indices() { + if cnt == count { + return Some((&self[index..],&self[..index])) + } + cnt += 1; + } + None + } +} + +/// indicates wether a comparison was successful, an error, or +/// if more data was needed +#[derive(Debug,PartialEq)] +pub enum CompareResult { + Ok, + Incomplete, + Error +} + +/// abstracts comparison operations +pub trait Compare { + /// compares self to another value for equality + fn compare(&self, t:T) -> CompareResult; + /// compares self to another value for equality + /// independently of the case. + /// + /// warning: for `&str`, the comparison is done + /// by lowercasing both strings and comparing + /// the result. This is a temporary solution until + /// a better one appears + fn compare_no_case(&self, t:T) -> CompareResult; +} + +impl<'a,'b> Compare<&'b[u8]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b[u8]) -> CompareResult { + let len = self.len(); + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = &self[..m]; + let b = &t[..m]; + + if reduced != b { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } + + #[inline(always)] + fn compare_no_case(&self, t: &'b[u8]) -> CompareResult { + let len = self.len(); + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = &self[..m]; + let other = &t[..m]; + + if !reduced.iter().zip(other).all(|(a, b)| { + match (*a,*b) { + (0...64, 0...64) | (91...96, 91...96) | (123...255, 123...255) => a == b, + (65...90, 65...90) | (97...122, 97...122) | (65...90, 97...122 ) |(97...122, 65...90) => { + *a | 0b00100000 == *b | 0b00100000 + } + _ => false + } + }) { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } +} + +impl<'a,'b> Compare<&'b str> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b str) -> CompareResult { + self.compare(str::as_bytes(t)) + } + #[inline(always)] + fn compare_no_case(&self, t: &'b str) -> CompareResult { + self.compare_no_case(str::as_bytes(t)) + } +} + +impl<'a,'b> Compare<&'b str> for &'a str { + #[inline(always)] + fn compare(&self, t: &'b str) -> CompareResult { + let pos = self.chars().zip(t.chars()).position(|(a,b)| a != b); + + match pos { + Some(_) => CompareResult::Error, + None => if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + + //FIXME: this version is too simple and does not use the current locale + #[inline(always)] + fn compare_no_case(&self, t: &'b str) -> CompareResult { + let pos = self.to_lowercase().chars().zip(t.to_lowercase().chars()).position(|(a,b)| a != b); + + match pos { + Some(_) => CompareResult::Error, + None => if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } +} + +/// look for self in the given input stream +pub trait FindToken { + fn find_token(&self, input: T) -> bool; +} + +impl<'a> FindToken<&'a[u8]> for u8 { + fn find_token(&self, input: &[u8]) -> bool { + memchr::memchr(*self, input).is_some() + } +} + +impl<'a> FindToken<&'a str> for u8 { + fn find_token(&self, input: &str) -> bool { + self.find_token(str::as_bytes(input)) + } +} + +impl<'a,'b> FindToken<&'a[u8]> for &'b u8 { + fn find_token(&self, input: &[u8]) -> bool { + memchr::memchr(**self, input).is_some() + } +} + +impl<'a,'b> FindToken<&'a str> for &'b u8 { + fn find_token(&self, input: &str) -> bool { + self.find_token(str::as_bytes(input)) + } +} + +impl<'a> FindToken<&'a str> for char { + fn find_token(&self, input: &str) -> bool { + for i in input.chars() { + if *self == i { return true } + } + false + } +} + +/// look for a substring in self +pub trait FindSubstring { + fn find_substring(&self, substr: T) -> Option; +} + +impl<'a,'b> FindSubstring<&'b [u8]> for &'a[u8] { + fn find_substring(&self, substr: &'b[u8]) -> Option { + let substr_len = substr.len(); + + if substr_len == 0 { + None + } else if substr_len == 1 { + memchr::memchr(substr[0], self) + } else { + let max = self.len() - substr_len; + let mut offset = 0; + let mut haystack = &self[..]; + + while let Some(position) = memchr::memchr(substr[0], haystack) { + offset += position; + + if offset > max { + return None + } + + if &haystack[position..position + substr_len] == substr { + return Some(offset) + } + + haystack = &haystack[position + 1..]; + offset += 1; + } + + None + } + } +} + +impl<'a,'b> FindSubstring<&'b str> for &'a[u8] { + fn find_substring(&self, substr: &'b str) -> Option { + self.find_substring(str::as_bytes(substr)) + } +} + +impl<'a,'b> FindSubstring<&'b str> for &'a str { + //returns byte index + fn find_substring(&self, substr: &'b str) -> Option { + self.find(substr) + } +} + +/// used to integrate str's parse() method +pub trait ParseTo { + fn parse_to(&self) -> Option; +} + +impl<'a,R: FromStr> ParseTo for &'a[u8] { + fn parse_to(&self) -> Option { + from_utf8(self).ok().and_then(|s| s.parse().ok()) + } +} + +impl<'a,R:FromStr> ParseTo for &'a str { + fn parse_to(&self) -> Option { + self.parse().ok() + } +} + +/// slicing operations using ranges +/// +/// this trait is loosely based on +/// `Index`, but can actually return +/// something else than a `&[T]` or `&str` +pub trait Slice { + #[inline(always)] + fn slice(&self, range: R) -> Self; +} + +macro_rules! impl_fn_slice { + ( $ty:ty ) => { + fn slice(&self, range:$ty) -> Self { + &self[range] + } + } +} + +macro_rules! slice_range_impl { + ( [ $for_type:ident ], $ty:ty ) => { + impl<'a, $for_type> Slice<$ty> for &'a [$for_type] { + impl_fn_slice!( $ty ); + } + }; + ( $for_type:ty, $ty:ty ) => { + impl<'a> Slice<$ty> for &'a $for_type { + impl_fn_slice!( $ty ); + } + } +} + +macro_rules! slice_ranges_impl { + ( [ $for_type:ident ] ) => { + slice_range_impl! {[$for_type], Range} + slice_range_impl! {[$for_type], RangeTo} + slice_range_impl! {[$for_type], RangeFrom} + slice_range_impl! {[$for_type], RangeFull} + }; + ( $for_type:ty ) => { + slice_range_impl! {$for_type, Range} + slice_range_impl! {$for_type, RangeTo} + slice_range_impl! {$for_type, RangeFrom} + slice_range_impl! {$for_type, RangeFull} + } +} + +slice_ranges_impl! {str} +slice_ranges_impl! {[T]} + + +macro_rules! array_impls { + ($($N:expr)+) => { + $( + impl InputLength for [u8; $N] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } + } + + impl<'a> InputLength for &'a [u8; $N] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } + } + + impl<'a> Compare<[u8; $N]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: [u8; $N]) -> CompareResult { + self.compare(&t[..]) + } + + #[inline(always)] + fn compare_no_case(&self, t: [u8;$N]) -> CompareResult { + self.compare_no_case(&t[..]) + } + } + + impl<'a,'b> Compare<&'b [u8; $N]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b [u8; $N]) -> CompareResult { + self.compare(&t[..]) + } + + #[inline(always)] + fn compare_no_case(&self, t: &'b [u8;$N]) -> CompareResult { + self.compare_no_case(&t[..]) + } + } + )+ + }; +} + + +array_impls! { + 0 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 +} diff --git third_party/rust/nom/src/util.rs third_party/rust/nom/src/util.rs index e2428ead0a4d..75663b958284 100644 --- third_party/rust/nom/src/util.rs +++ third_party/rust/nom/src/util.rs @@ -1,19 +1,27 @@ -use internal::{IResult,Err}; -#[cfg(not(feature = "core"))] +#[cfg(feature = "verbose-errors")] +use internal::IResult; + +#[cfg(feature = "verbose-errors")] +use verbose_errors::Err; + +#[cfg(feature = "std")] use std::collections::HashMap; -#[cfg(feature = "core")] +#[cfg(not(feature = "std"))] use std::prelude::v1::*; + use std::vec::Vec; use std::string::ToString; /// useful functions to calculate the offset between slices and show a hexdump of a slice -#[cfg(not(feature = "core"))] -pub trait HexDisplay { +pub trait Offset { /// offset between the first byte of self and the first byte of the argument - fn offset(&self, second:&[u8]) -> usize;// OFFSET SHOULD GO TO ITS OWN TRAIT + fn offset(&self, second:&Self) -> usize; +} +#[cfg(feature = "std")] +pub trait HexDisplay { /// Converts the value of `self` to a hex dump, returning the owned /// string. fn to_hex(&self, chunk_size: usize) -> String; @@ -23,130 +31,28 @@ pub trait HexDisplay { fn to_hex_from(&self, chunk_size: usize, from: usize) -> String; } -pub trait InputLength { - #[inline] - fn input_len(&self) -> usize; -} - -impl<'a, T> InputLength for &'a[T] { - #[inline] - fn input_len(&self) -> usize { - self.len() - } -} +static CHARS: &'static[u8] = b"0123456789abcdef"; -impl<'a> InputLength for &'a str { - #[inline] - fn input_len(&self) -> usize { - self.len() - } -} +impl Offset for [u8] { + fn offset(&self, second:&[u8]) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); -impl<'a> InputLength for (&'a [u8], usize) { - #[inline] - fn input_len(&self) -> usize { - //println!("bit input length for ({:?}, {}):", self.0, self.1); - let res = self.0.len() * 8 - self.1; - //println!("-> {}", res); - res + snd as usize - fst as usize } } -use std::iter::Enumerate; -#[cfg(not(feature = "core"))] -use std::str::CharIndices; - -pub trait AsChar { - #[inline] - fn as_char(self) -> char; - #[inline] - fn is_alpha(self) -> bool; - #[inline] - fn is_alphanum(self) -> bool; - #[inline] - fn is_0_to_9(self) -> bool; - #[inline] - fn is_hex_digit(self) -> bool; - #[inline] - fn is_oct_digit(self) -> bool; -} +impl Offset for str { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); -impl<'a> AsChar for &'a u8 { - #[inline] - fn as_char(self) -> char { *self as char } - #[inline] - fn is_alpha(self) -> bool { - (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) - } - #[inline] - fn is_alphanum(self) -> bool { self.is_alpha() || self.is_0_to_9() } - #[inline] - fn is_0_to_9(self) -> bool { - *self >= 0x30 && *self <= 0x39 + snd as usize - fst as usize } - #[inline] - fn is_hex_digit(self) -> bool { - (*self >= 0x30 && *self <= 0x39) || - (*self >= 0x41 && *self <= 0x46) || - (*self >= 0x61 && *self <= 0x66) - } - #[inline] - fn is_oct_digit(self) -> bool { - *self >= 0x30 && *self <= 0x37 - } -} - -impl AsChar for char { - #[inline] - fn as_char(self) -> char { self } - #[inline] - fn is_alpha(self) -> bool { self.is_alphabetic() } - #[inline] - fn is_alphanum(self) -> bool { self.is_alpha() || self.is_0_to_9() } - #[inline] - fn is_0_to_9(self) -> bool { self.is_digit(10) } - #[inline] - fn is_hex_digit(self) -> bool { self.is_digit(16) } - #[inline] - fn is_oct_digit(self) -> bool { self.is_digit(8) } } -pub trait IterIndices { - type Item: AsChar; - type Iter : Iterator; - fn iter_indices(self) -> Self::Iter; -} - -impl<'a> IterIndices for &'a [u8] { - type Item = &'a u8; - type Iter = Enumerate<::std::slice::Iter<'a, u8>>; - #[inline] - fn iter_indices(self) -> Enumerate<::std::slice::Iter<'a, u8>> { - self.iter().enumerate() - } -} - -#[cfg(not(feature = "core"))] -impl<'a> IterIndices for &'a str { - type Item = char; - type Iter = CharIndices<'a>; - #[inline] - fn iter_indices(self) -> CharIndices<'a> { - self.char_indices() - } -} - -static CHARS: &'static[u8] = b"0123456789abcdef"; - -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] impl HexDisplay for [u8] { - fn offset(&self, second:&[u8]) -> usize { - let fst = self.as_ptr(); - let snd = second.as_ptr(); - - snd as usize - fst as usize - } - #[allow(unused_variables)] fn to_hex(&self, chunk_size: usize) -> String { self.to_hex_from(chunk_size, 0) @@ -241,7 +147,7 @@ macro_rules! dbg ( /// /// It also displays the input in hexdump format /// -/// ``` +/// ```ignore /// # #[macro_use] extern crate nom; /// # fn main() { /// named!(f, dbg_dmp!( tag!( "abcd" ) ) ); @@ -278,32 +184,44 @@ macro_rules! dbg_dmp ( ); ); +#[cfg(feature = "verbose-errors")] pub fn error_to_list(e:&Err) -> Vec> { let mut v:Vec> = Vec::new(); - let mut err = e; - loop { - match *err { - Err::Code(ref i) | Err::Position(ref i,_) => { + match e { + &Err::Code(ref i) | &Err::Position(ref i,_) => { v.push(i.clone()); return v; - }, - Err::Node(ref i, ref next) | Err::NodePosition(ref i, _, ref next) => { - v.push(i.clone()); - err = &*next; - } - } + }, + &Err::Node(ref i, ref next) | &Err::NodePosition(ref i, _, ref next) => { + //v.push(i.clone()); + for error in next.iter() { + if let &Err::Code(ref i2) = error { + v.push(i2.clone()); + } + if let &Err::Position(ref i2,_) = error { + v.push(i2.clone()); + } + } + v.push(i.clone()); + v.reverse() + } } + + v } +#[cfg(feature = "verbose-errors")] pub fn compare_error_paths(e1:&Err, e2:&Err) -> bool { error_to_list(e1) == error_to_list(e2) } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] +#[cfg(feature = "verbose-errors")] use std::hash::Hash; -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] +#[cfg(feature = "verbose-errors")] pub fn add_error_pattern<'a,I,O,E: Clone+Hash+Eq>(h: &mut HashMap>, &'a str>, res: IResult, message: &'a str) -> bool { if let IResult::Error(e) = res { h.insert(error_to_list(&e), message); @@ -320,32 +238,33 @@ pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) { (off1, off2) } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] +#[cfg(feature = "verbose-errors")] pub fn prepare_errors(input: &[u8], res: IResult<&[u8],O,E>) -> Option, usize, usize)> > { if let IResult::Error(e) = res { let mut v:Vec<(ErrorKind, usize, usize)> = Vec::new(); - let mut err = e.clone(); - loop { - match err { - Err::Position(i,s) => { - let (o1, o2) = slice_to_offsets(input, s); - v.push((i, o1, o2)); - //println!("v is: {:?}", v); - break; - }, - Err::NodePosition(i, s, next) => { - let (o1, o2) = slice_to_offsets(input, s); + + match e { + Err::Code(_) => {}, + Err::Position(i, p) => { + let (o1, o2) = slice_to_offsets(input, p); v.push((i, o1, o2)); - err = *next; - }, - Err::Node(_, next) => { - err = *next; - }, - Err::Code(_) => { - break; - } - } + }, + Err::Node(_, _) => {}, + Err::NodePosition(i, p, next) => { + //v.push(i.clone()); + for error in next.iter() { + if let &Err::Position(ref i2, ref p2) = error { + let (o1, o2) = slice_to_offsets(input, p2); + v.push((i2.clone(), o1, o2)); + } + } + let (o1, o2) = slice_to_offsets(input, p); + v.push((i, o1, o2)); + v.reverse() + } } + v.sort_by(|a, b| a.1.cmp(&b.1)); Some(v) } else { @@ -353,7 +272,8 @@ pub fn prepare_errors(input: &[u8], res: IResult<&[u8],O,E>) -> Opti } } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] +#[cfg(feature = "verbose-errors")] pub fn print_error(input: &[u8], res: IResult<&[u8],O,E>) { if let Some(v) = prepare_errors(input, res) { let colors = generate_colors(&v); @@ -365,7 +285,8 @@ pub fn print_error(input: &[u8], res: IResult<&[u8],O,E>) { } } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] +#[cfg(feature = "verbose-errors")] pub fn generate_colors(v: &[(ErrorKind, usize, usize)]) -> HashMap { let mut h: HashMap = HashMap::new(); let mut color = 0; @@ -417,7 +338,7 @@ pub fn write_color(v: &mut Vec, color: u8) { v.push('m' as u8); } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] pub fn print_codes(colors: HashMap, names: HashMap) -> String { let mut v = Vec::new(); for (code, &color) in &colors { @@ -439,7 +360,8 @@ pub fn print_codes(colors: HashMap, names: HashMap) -> Strin String::from_utf8_lossy(&v[..]).into_owned() } -#[cfg(not(feature = "core"))] +#[cfg(feature = "std")] +#[cfg(feature = "verbose-errors")] pub fn print_offsets(input: &[u8], from: usize, offsets: &[(ErrorKind, usize, usize)]) -> String { let mut v = Vec::with_capacity(input.len() * 3); let mut i = from; @@ -599,6 +521,7 @@ pub enum ErrorKind { SeparatedNonEmptyList, Many0, Many1, + ManyTill, Count, TakeUntilAndConsume, TakeUntil, @@ -642,7 +565,10 @@ pub enum ErrorKind { ManyMN, TakeUntilAndConsumeStr, TakeUntilStr, - Not + Not, + Permutation, + Verify, + TakeTill1, } pub fn error_to_u32(e: &ErrorKind) -> u32 { @@ -702,6 +628,10 @@ pub fn error_to_u32(e: &ErrorKind) -> u32 { ErrorKind::OctDigit => 61, ErrorKind::Many0 => 62, ErrorKind::Not => 63, + ErrorKind::Permutation => 64, + ErrorKind::ManyTill => 65, + ErrorKind::Verify => 66, + ErrorKind::TakeTill1 => 67, } } @@ -763,7 +693,46 @@ pub fn error_to_u32(e: &ErrorKind) -> u32 { ErrorKind::TakeUntilStr => "Take until on strings", ErrorKind::OctDigit => "Octal digit", ErrorKind::Not => "Negation", + ErrorKind::Permutation => "Permutation", + ErrorKind::ManyTill => "ManyTill", + ErrorKind::Verify => "predicate verification", + ErrorKind::TakeTill1 => "TakeTill1", } } + /// Convert Err into an ErrorKind. + /// + /// This allows application code to use ErrorKind and stay independent from the `verbose-errors` features activation. + pub fn into_error_kind(self) -> ErrorKind { + self + } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_offset_u8() { + let s = b"abcd123"; + let a = &s[..]; + let b = &a[2..]; + let c = &a[..4]; + let d = &a[3..5]; + assert_eq!(a.offset(b), 2); + assert_eq!(a.offset(c), 0); + assert_eq!(a.offset(d), 3); + } + + #[test] + fn test_offset_str() { + let s = "abcřèÂßÇd123"; + let a = &s[..]; + let b = &a[7..]; + let c = &a[..5]; + let d = &a[5..9]; + assert_eq!(a.offset(b), 7); + assert_eq!(a.offset(c), 0); + assert_eq!(a.offset(d), 5); + } +} diff --git third_party/rust/nom/src/verbose_errors.rs third_party/rust/nom/src/verbose_errors.rs new file mode 100644 index 000000000000..fe236b6c930a --- /dev/null +++ third_party/rust/nom/src/verbose_errors.rs @@ -0,0 +1,227 @@ +//! Error management +//! +//! there are two ways to handle errors in nom. The first one, +//! activated by default, uses the `nom::ErrorKind` enum +//! in the error branch of `IResult`. This enum can hold either +//! a parser specific error code, or a custom error type you +//! specify. +//! +//! If you need more advanced error management, you can activate +//! the "verbose-errors" compilation feature, which will give you +//! the error system available in nom 1.0. The verbose errors +//! accumulate error codes and positions as you backtrack through +//! the parser tree. From there, you can precisely identify which +//! parts of the input triggered the error case. +//! +//! Please note that the verbose error management is a bit slower +//! than the simple one. +use util::ErrorKind; +use internal::{IResult, IError}; +use internal::IResult::*; + +/// Contains the error that a parser can return +/// +/// If you use the `verbose-errors` compilation feature, +/// `nom::Err` will be the enum defined here, +/// otherwise, it will amount to a `ErrorKind`. +/// +/// It can represent a linked list of errors, indicating the path taken in the parsing tree, with corresponding position in the input data. +/// It depends on P, the input position (for a &[u8] parser, it would be a &[u8]), and E, the custom error type (by default, u32) +#[derive(Debug,PartialEq,Eq,Clone)] +pub enum Err{ + /// An error code, represented by an ErrorKind, which can contain a custom error code represented by E + Code(ErrorKind), + /// An error code, and the next error + Node(ErrorKind, Vec>), + /// An error code, and the input position + Position(ErrorKind, P), + /// An error code, the input position and the next error + NodePosition(ErrorKind, P, Vec>) +} + +impl Err { + /// Convert Err into ErrorKind. + /// + /// This allows application code to use ErrorKind and stay independent from the verbose-errors features activation. + pub fn into_error_kind(self) -> ErrorKind { + match self { + Err::Code(kind) => kind, + Err::Node(kind, _) => kind, + Err::Position(kind, _) => kind, + Err::NodePosition(kind, _, _) => kind, + } + } +} + +impl IResult { + /// Maps a `IResult` to `IResult` by appling a function + /// to a contained `Error` value, leaving `Done` and `Incomplete` value + /// untouched. + #[inline] + pub fn map_err(self, f: F) -> IResult + where F: FnOnce(Err) -> Err { + match self { + Error(e) => Error(f(e)), + Incomplete(n) => Incomplete(n), + Done(i, o) => Done(i, o), + } + } + + /// Unwrap the contained `Error(I, E)` value, or panic if the `IResult` is not + /// `Error`. + pub fn unwrap_err(self) -> Err { + match self { + Error(e) => e, + Done(_, _) => panic!("unwrap_err() called on an IResult that is Done"), + Incomplete(_) => panic!("unwrap_err() called on an IResult that is Incomplete"), + } + } + + /// Convert the IResult to a std::result::Result + pub fn to_full_result(self) -> Result> { + match self { + Done(_, o) => Ok(o), + Incomplete(n) => Err(IError::Incomplete(n)), + Error(e) => Err(IError::Error(e)) + } + } + + /// Convert the IResult to a std::result::Result + pub fn to_result(self) -> Result> { + match self { + Done(_, o) => Ok(o), + Error(e) => Err(e), + Incomplete(_) => panic!("to_result() called on an IResult that is Incomplete") + } + } +} + +#[cfg(feature = "std")] +use std::any::Any; +#[cfg(feature = "std")] +use std::{error,fmt}; +#[cfg(feature = "std")] +use std::fmt::Debug; +#[cfg(feature = "std")] +impl error::Error for Err { + fn description(&self) -> &str { + let kind = match *self { + Err::Code(ref e) | Err::Node(ref e, _) | Err::Position(ref e, _) | Err::NodePosition(ref e, _, _) => e + }; + kind.description() + } +} + +#[cfg(feature = "std")] +impl fmt::Display for Err { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Err::Code(ref e) | Err::Node(ref e, _) => { + write!(f, "{:?}", e) + }, + Err::Position(ref e, ref p) | Err::NodePosition(ref e, ref p, _) => { + write!(f, "{:?}:{:?}", p, e) + } + } + } +} + + +/// translate parser result from IResult to IResult with a custom type +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::IResult::Error; +/// # use nom::Err::{Position,NodePosition}; +/// # use nom::ErrorKind; +/// # fn main() { +/// // will add a Custom(42) error to the error chain +/// named!(err_test, add_return_error!(ErrorKind::Custom(42), tag!("abcd"))); +/// // Convert to IREsult<&[u8], &[u8], &str> +/// named!(parser<&[u8], &[u8], &str>, add_return_error!(ErrorKind::Custom("custom error message"), fix_error!(&str, err_test))); +/// +/// let a = &b"efghblah"[..]; +/// let res_a = parser(a); +/// assert_eq!(res_a, Error(NodePosition( ErrorKind::Custom("custom error message"), a, vec!(Position(ErrorKind::Fix, a))))); +/// # } +/// ``` +#[macro_export] +macro_rules! fix_error ( + ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), + $crate::IResult::Error(e) => { + let err = match e { + $crate::Err::Code($crate::ErrorKind::Custom(_)) | + $crate::Err::Node($crate::ErrorKind::Custom(_), _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Code(e) + }, + $crate::Err::Position($crate::ErrorKind::Custom(_), p) | + $crate::Err::NodePosition($crate::ErrorKind::Custom(_), p, _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Position(e, p) + }, + $crate::Err::Code(_) | + $crate::Err::Node(_, _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Code(e) + }, + $crate::Err::Position(_, p) | + $crate::Err::NodePosition(_, p, _) => { + let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix; + $crate::Err::Position(e, p) + }, + }; + $crate::IResult::Error(err) + } + } + } + ); + ($i:expr, $t:ty, $f:expr) => ( + fix_error!($i, $t, call!($f)); + ); +); + +/// `flat_map!(R -> IResult, S -> IResult) => R -> IResult` +/// +/// combines a parser R -> IResult and +/// a parser S -> IResult to return another +/// parser R -> IResult +#[macro_export] +macro_rules! flat_map( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + match $submac!($i, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)), + $crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) { + $crate::IResult::Error(e) => { + let err = match e { + $crate::Err::Code(k) | $crate::Err::Node(k, _) | $crate::Err::Position(k, _) | $crate::Err::NodePosition(k, _, _) => { + $crate::Err::Position(k, $i) + } + }; + $crate::IResult::Error(err) + }, + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(ref i2)) => $crate::IResult::Incomplete($crate::Needed::Size(*i2)), + $crate::IResult::Done(_, o2) => $crate::IResult::Done(i, o2) + } + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + flat_map!($i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $g:expr) => ( + flat_map!($i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + flat_map!($i, call!($f), $submac!($($args)*)); + ); +); diff --git third_party/rust/nom/src/whitespace.rs third_party/rust/nom/src/whitespace.rs new file mode 100644 index 000000000000..3b8f7bd5bdae --- /dev/null +++ third_party/rust/nom/src/whitespace.rs @@ -0,0 +1,1056 @@ +//! Support for whitespace delimited formats +//! +//! a lot of textual formats allows spaces and other +//! types of separators between tokens. Handling it +//! manually with nom means wrapping all parsers +//! like this: +//! +//! ```ignore +//! named!(token, delimited!(space, tk, space)); +//! ``` +//! +//! To ease the development of such parsers, you +//! can use the whitespace parsing facility, which works +//! as follows: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! # use nom::IResult::Done; +//! # fn main() { +//! named!(tuple<&[u8], (&[u8], &[u8]) >, +//! ws!(tuple!( take!(3), tag!("de") )) +//! ); +//! +//! assert_eq!( +//! tuple(&b" \t abc de fg"[..]), +//! Done(&b"fg"[..], (&b"abc"[..], &b"de"[..])) +//! ); +//! # } +//! ``` +//! +//! The `ws!` combinator will modify the parser to +//! intersperse space parsers everywhere. By default, +//! it will consume the following characters: " \t\r\n". +//! +//! If you want to modify that behaviour, you can make +//! your own whitespace wrapper. As an example, if +//! you don't want to consume ends of lines, only +//! spaces and tabs, you can do it like this: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! # use nom::IResult::Done; +//! named!(pub space, eat_separator!(&b" \t"[..])); +//! +//! #[macro_export] +//! macro_rules! sp ( +//! ($i:expr, $($args:tt)*) => ( +//! { +//! sep!($i, space, $($args)*) +//! } +//! ) +//! ); +//! +//! # fn main() { +//! named!(tuple<&[u8], (&[u8], &[u8]) >, +//! sp!(tuple!( take!(3), tag!("de") )) +//! ); +//! +//! assert_eq!( +//! tuple(&b" \t abc de fg"[..]), +//! Done(&b"fg"[..], (&b"abc"[..], &b"de"[..])) +//! ); +//! # } +//! ``` +//! +//! This combinator works by replacing each combinator with +//! a version that supports wrapping with separator parsers. +//! It will not support the combinators you wrote in your +//! own code. You can still manually wrap them with the separator +//! you want, or you can copy the macros defined in src/whitespace.rs +//! and modify them to support a new combinator: +//! * copy the combinator's code here, add the _sep suffix +//! * add the `$separator:expr` as second argument +//! * wrap any sub parsers with sep!($separator, $submac!($($args)*)) +//! * reference it in the definition of `sep!` as follows: +//! +//! ```ignore +//! ($i:expr, $separator:ident, my_combinator ! ($($rest:tt)*) ) => { +//! wrap_sep!($i, +//! $separator, +//! my_combinator_sep!($separator, $($rest)*) +//! ) +//! }; +//! ``` +//! + +#[macro_export] +macro_rules! wrap_sep ( + ($i:expr, $separator:expr, $submac:ident!( $($args:tt)* )) => ( + match ($separator)($i) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i1,_) => { + match $submac!(i1, $($args)*) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i1)))), + $crate::IResult::Done(i2,o) => { + match ($separator)(i2) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i2)))), + $crate::IResult::Done(i3,_) => $crate::IResult::Done(i3, o) + } + } + } + } + } + ); + ($i:expr, $separator:expr, $f:expr) => ( + wrap_sep!($i, $separator, call!($f)) + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! pair_sep ( + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + tuple!( + $i, + sep!($separator, $submac!($($args)*)), + sep!($separator, $submac2!($($args2)*)) + ) + ); + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:ident, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:ident, $f:expr, $g:expr) => ( + pair_sep!($i, $separator, call!($f), call!($g)); + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! delimited_sep ( + ($i:expr, $separator:ident, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ( + match tuple_sep!($i, $separator, 0usize, (), $submac1!($($args1)*), $($rest)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (_,o,_)) => { + $crate::IResult::Done(remaining, o) + } + } + ); + ($i:expr, $separator:ident, $f:expr, $($rest:tt)+) => ( + delimited_sep!($i, $separator, call!($f), $($rest)*); + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! separated_pair_sep ( + ($i:expr, $separator:ident, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ( + match tuple_sep!($i, $separator, 0usize, (), $submac1!($($args1)*), $($rest)*) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (o1,_,o2)) => { + $crate::IResult::Done(remaining, (o1,o2)) + } + } + ); + ($i:expr, $separator:ident, $f:expr, $($rest:tt)+) => ( + separated_pair_sep!($i, $separator, call!($f), $($rest)*); + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! preceded_sep ( + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (_,o)) => { + $crate::IResult::Done(remaining, o) + } + } + ); + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:ident, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:ident, $f:expr, $g:expr) => ( + preceded_sep!($i, $separator, call!($f), call!($g)); + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! terminated_sep ( + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) { + $crate::IResult::Error(a) => $crate::IResult::Error(a), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(remaining, (o,_)) => { + $crate::IResult::Done(remaining, o) + } + } + ); + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:ident, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:ident, $f:expr, $g:expr) => ( + terminated_sep!($i, $separator, call!($f), call!($g)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export] +macro_rules! tuple_sep ( + ($i:expr, $separator:ident, $consumed:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => ( + tuple_sep!($i, $separator, $consumed, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, $separator:ident, $consumed:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + tuple_sep!(i, $separator, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), (o), $($rest)*) + } + } + } + ); + ($i:expr, $separator:ident, $consumed:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + tuple_sep!(i, $separator, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), ($($parsed)* , o), $($rest)*) + } + } + } + ); + ($i:expr, $separator:ident, $consumed:expr, ($($parsed:tt),*), $e:ident) => ( + tuple_sep!($i, $separator, $consumed, ($($parsed),*), call!($e)); + ); + ($i:expr, $separator:ident, $consumed:expr, (), $submac:ident!( $($args:tt)* )) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + $crate::IResult::Done(i, (o)) + } + } + } + ); + ($i:expr, $separator:ident, $consumed:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + $crate::IResult::Done(i, ($($parsed),* , o)) + } + } + } + ); + ($i:expr, $separator:ident, $consumed:expr, ($($parsed:expr),*)) => ( + { + $crate::IResult::Done($i, ($($parsed),*)) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! do_parse_sep ( + (__impl $i:expr, $separator:ident, $consumed:expr, ( $($rest:expr),* )) => ( + $crate::IResult::Done($i, ( $($rest),* )) + ); + + (__impl $i:expr, $separator:ident, $consumed:expr, $e:ident >> $($rest:tt)*) => ( + do_parse_sep!(__impl $i, $separator, $consumed, call!($e) >> $($rest)*); + ); + (__impl $i:expr, $separator:ident, $consumed:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => + $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,_) => { + do_parse_sep!(__impl i, $separator, + $consumed + ($crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&i)), $($rest)*) + }, + } + } + ); + + (__impl $i:expr, $separator:ident, $consumed:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( + do_parse_sep!(__impl $i, $separator, $consumed, $field: call!($e) >> $($rest)*); + ); + + (__impl $i:expr, $separator:ident, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => + $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + let $field = o; + do_parse_sep!(__impl i, $separator, + $consumed + ($crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&i)), $($rest)*) + }, + } + } + ); + + // ending the chain + (__impl $i:expr, $separator:ident, $consumed:expr, $e:ident >> ( $($rest:tt)* )) => ( + do_parse_sep!(__impl $i, $separator, $consumed, call!($e) >> ( $($rest)* )); + ); + + (__impl $i:expr, $separator:ident, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => + $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,_) => { + $crate::IResult::Done(i, ( $($rest)* )) + }, + } + ); + + (__impl $i:expr, $separator:ident, $consumed:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( + do_parse_sep!(__impl $i, $separator, $consumed, $field: call!($e) >> ( $($rest)* ) ); + ); + + (__impl $i:expr, $separator:ident, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(e), + $crate::IResult::Incomplete($crate::Needed::Unknown) => + $crate::IResult::Incomplete($crate::Needed::Unknown), + $crate::IResult::Incomplete($crate::Needed::Size(i)) => + $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)), + $crate::IResult::Done(i,o) => { + let $field = o; + $crate::IResult::Done(i, ( $($rest)* )) + }, + } + ); + + ($i:expr, $separator:ident, $($rest:tt)*) => ( + { + do_parse_sep!(__impl $i, $separator, 0usize, $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! permutation_sep ( + ($i:expr, $separator:ident, $($rest:tt)*) => ( + { + let mut res = permutation_init!((), $($rest)*); + let mut input = $i; + let mut error = ::std::option::Option::None; + let mut needed = ::std::option::Option::None; + + loop { + let mut all_done = true; + permutation_iterator_sep!(0, input, $separator, all_done, needed, res, $($rest)*); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + error = ::std::option::Option::Some(error_position!($crate::ErrorKind::Permutation, input)); + } + break; + } + + if let ::std::option::Option::Some(need) = needed { + if let $crate::Needed::Size(sz) = need { + $crate::IResult::Incomplete( + $crate::Needed::Size( + $crate::InputLength::input_len(&($i)) - + $crate::InputLength::input_len(&input) + + sz + ) + ) + } else { + $crate::IResult::Incomplete($crate::Needed::Unknown) + } + } else if let ::std::option::Option::Some(e) = error { + $crate::IResult::Error(e) + } else { + let unwrapped_res = permutation_unwrap!(0, (), res, $($rest)*); + $crate::IResult::Done(input, unwrapped_res) + } + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! permutation_iterator_sep ( + ($it:tt,$i:expr, $separator:ident, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, call!($e), $($rest)*); + ); + ($it:tt, $i:expr, $separator:ident, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { + if acc!($it, $res) == ::std::option::Option::None { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Done(i,o) => { + $i = i; + acc!($it, $res) = ::std::option::Option::Some(o); + continue; + }, + $crate::IResult::Error(_) => { + $all_done = false; + }, + $crate::IResult::Incomplete(i) => { + $needed = ::std::option::Option::Some(i); + break; + } + }; + } + succ!($it, permutation_iterator_sep!($i, $separator, $all_done, $needed, $res, $($rest)*)); + }; + ($it:tt,$i:expr, $separator:ident, $all_done:expr, $needed:expr, $res:expr, $e:ident) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $res, call!($e)); + ); + ($it:tt, $i:expr, $separator:ident, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => { + if acc!($it, $res) == ::std::option::Option::None { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Done(i,o) => { + $i = i; + acc!($it, $res) = ::std::option::Option::Some(o); + continue; + }, + $crate::IResult::Error(_) => { + $all_done = false; + }, + $crate::IResult::Incomplete(i) => { + $needed = ::std::option::Option::Some(i); + break; + } + }; + } + }; +); + +#[doc(hidden)] +#[macro_export] +macro_rules! alt_sep ( + (__impl $i:expr, $separator:ident, $e:ident | $($rest:tt)*) => ( + alt_sep!(__impl $i, $separator, call!($e) | $($rest)*); + ); + + (__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + let res = sep!($i, $separator, $subrule!($($args)*)); + match res { + $crate::IResult::Done(_,_) => res, + $crate::IResult::Incomplete(_) => res, + _ => alt_sep!(__impl $i, $separator, $($rest)*) + } + } + ); + + (__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + match sep!($i, $separator, $subrule!( $($args)* )) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(_) => { + alt_sep!(__impl $i, $separator, $($rest)*) + } + } + } + ); + + (__impl $i:expr, $separator:ident, $e:ident => { $gen:expr } | $($rest:tt)*) => ( + alt_sep!(__impl $i, $separator, call!($e) => { $gen } | $($rest)*); + ); + + (__impl $i:expr, $separator:ident, $e:ident => { $gen:expr }) => ( + alt_sep!(__impl $i, $separator, call!($e) => { $gen }); + ); + + (__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + { + match sep!($i, $separator, $subrule!( $($args)* )) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(_) => { + $crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i)) + } + } + } + ); + + (__impl $i:expr, $separator:ident, $e:ident) => ( + alt_sep!(__impl $i, $separator, call!($e)); + ); + + (__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)*)) => ( + { + match sep!($i, $separator, $subrule!( $($args)* )) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,o), + $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x), + $crate::IResult::Error(_) => { + $crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i)) + } + } + } + ); + + (__impl $i:expr) => ( + $crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i)) + ); + + (__impl $i:expr, $separator:ident) => ( + $crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i)) + ); + + ($i:expr, $separator:ident, $($rest:tt)*) => ( + { + alt_sep!(__impl $i, $separator, $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! alt_complete_sep ( + ($i:expr, $separator:ident, $e:ident | $($rest:tt)*) => ( + alt_complete_sep!($i, $separator, complete!(call!($e)) | $($rest)*); + ); + + ($i:expr, $separator:ident, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + let res = complete!($i, sep!($separator, $subrule!($($args)*))); + match res { + $crate::IResult::Done(_,_) => res, + _ => alt_complete_sep!($i, $separator, $($rest)*), + } + } + ); + + ($i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + match complete!($i, sep!($separator, $subrule!($($args)*))) { + $crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)), + _ => alt_complete_sep!($i, $separator, $($rest)*), + } + } + ); + + ($i:expr, $separator:ident, $e:ident => { $gen:expr } | $($rest:tt)*) => ( + alt_complete_sep!($i, $separator, complete!(call!($e)) => { $gen } | $($rest)*); + ); + + // Tail (non-recursive) rules + + ($i:expr, $separator:ident, $e:ident => { $gen:expr }) => ( + alt_complete_sep!($i, $separator, call!($e) => { $gen }); + ); + + ($i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + alt_sep!(__impl $i, $separator, $subrule!($($args)*) => { $gen }) + ); + + ($i:expr, $separator:ident, $e:ident) => ( + alt_complete_sep!($i, $separator, call!($e)); + ); + + ($i:expr, $separator:ident, $subrule:ident!( $($args:tt)*)) => ( + alt_sep!(__impl $i, $separator, $subrule!($($args)*)) + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! switch_sep ( + (__impl $i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + match sep!($i, $separator, $submac!($($args)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!( + $crate::ErrorKind::Switch, $i, e + )), + $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), + $crate::IResult::Done(i, o) => { + match o { + $($p => match sep!(i, $separator, $subrule!($($args2)*)) { + $crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!( + $crate::ErrorKind::Switch, $i, e + )), + a => a, + }),*, + _ => $crate::IResult::Error(error_position!($crate::ErrorKind::Switch,$i)) + } + } + } + } + ); + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch_sep!(__impl $i, $separator, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $separator:ident, $e:ident, $($rest:tt)*) => ( + { + switch_sep!(__impl $i, $separator, call!($e), $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! separated_list_sep ( + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_list!( + $i, + sep!($separator, $submac!($($args)*)), + sep!($separator, $submac2!($($args2)*)) + ) + ); + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:ident, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:ident, $f:expr, $g:expr) => ( + separated_list_sep!($i, $separator, call!($f), call!($g)); + ); +); + +/// helper macros to build a separator parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// named!(pub space, eat_separator!(&b" \t"[..])); +/// # fn main() {} +/// ``` +#[macro_export] +macro_rules! eat_separator ( + ($i:expr, $arr:expr) => ( + { + use $crate::{AsChar,InputLength,InputIter,Slice}; + if ($i).input_len() == 0 { + $crate::IResult::Done(($i).slice(0..), ($i).slice(0..0)) + } else { + match ($i).iter_indices().position(|(_, item)| { + let i = item.as_char(); + for (_,c) in ($arr).iter_indices() { + if c.as_char() == i { return false; } + } + true + }) { + ::std::option::Option::Some(index) => { + $crate::IResult::Done(($i).slice(index..), ($i).slice(..index)) + }, + ::std::option::Option::None => { + $crate::IResult::Done(($i).slice(($i).input_len()..), ($i)) + } + } + } + } + ); +); + +/// sep is the parser rewriting macro for whitespace separated formats +/// +/// it takes as argument a space eating function and a parser tree, +/// and will intersperse the space parser everywhere +/// +/// ```ignore +/// #[macro_export] +/// macro_rules! ws ( +/// ($i:expr, $($args:tt)*) => ( +/// { +/// use $crate::sp; +/// sep!($i, sp, $($args)*) +/// } +/// ) +/// ); +/// ``` +#[macro_export] +macro_rules! sep ( + ($i:expr, $separator:ident, tuple ! ($($rest:tt)*) ) => { + tuple_sep!($i, $separator, 0usize, (), $($rest)*) + }; + ($i:expr, $separator:ident, pair ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + pair_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, delimited ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + delimited_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, separated_pair ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + separated_pair_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, preceded ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + preceded_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, terminated ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + terminated_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, do_parse ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + do_parse_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, permutation ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + permutation_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, alt ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + alt_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, alt_complete ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + alt_complete_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, switch ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + switch_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, separated_list ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + separated_list_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:ident, many0 ! ($($rest:tt)*) ) => { + many0!($i, wrap_sep!($separator, $($rest)*)) + }; + ($i:expr, $separator:ident, many1 ! ($($rest:tt)*) ) => { + many1!($i, wrap_sep!($separator, $($rest)*)) + }; +//FIXME: missing separated_nonempty_list, +// many_till, many_m_n, count, count_fixed, fold_many0, fold_many1, +// fold_many_m_n + ($i:expr, $separator:ident, $submac:ident!( $($args:tt)* )) => { + wrap_sep!($i, $separator, $submac!($($args)*)) + }; + ($i:expr, $separator:ident, $f:expr) => { + wrap_sep!($i, $separator, call!($f)) + }; +); + +use std::ops::{Range,RangeFrom,RangeTo}; +use internal::IResult; +#[allow(unused_imports)] +pub fn sp<'a,T>(input:T) -> IResult where + T: ::traits::Slice>+::traits::Slice>+::traits::Slice>, + T: ::traits::InputIter+::traits::InputLength, + ::Item: ::traits::AsChar { + eat_separator!(input, &b" \t\r\n"[..]) +} + +/// `ws!(I -> IResult) => I -> IResult` +/// +/// transforms a parser to automatically consume +/// whitespace between each token. By default, +/// it takes the following characters: " \t\r\n". +/// +/// If you need a whitespace parser consuming a +/// different set of characters, you can make +/// your own by reusing the `sep!` combinator. +/// +/// To use `ws!`, pass your parser as argument: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult::Done; +/// # fn main() { +/// named!(tuple<&[u8], (&[u8], &[u8]) >, +/// ws!(tuple!( take!(3), tag!("de") )) +/// ); +/// +/// assert_eq!( +/// tuple(&b" \t abc de fg"[..]), +/// Done(&b"fg"[..], (&b"abc"[..], &b"de"[..])) +/// ); +/// # } +/// ``` +/// +#[macro_export] +macro_rules! ws ( + ($i:expr, $($args:tt)*) => ( + { + use $crate::sp; + sep!($i, sp, $($args)*) + } + ) +); + +#[cfg(test)] +#[allow(dead_code)] +mod tests { + use internal::IResult::*; + use internal::{IResult,Needed}; + use super::sp; + use util::ErrorKind; + + #[test] + fn spaaaaace() { + assert_eq!(sp(&b" \t abc "[..]), Done(&b"abc "[..], &b" \t "[..])); + } + + #[test] + fn tag() { + named!(abc, ws!(tag!("abc"))); + + assert_eq!(abc(&b" \t abc def"[..]), Done(&b"def"[..], &b"abc"[..])); + } + + #[test] + fn pair() { + named!(pair_2<&[u8], (&[u8], &[u8]) >, + ws!(pair!( take!(3), tag!("de") )) + ); + + assert_eq!(pair_2(&b" \t abc de fg"[..]), Done(&b"fg"[..], (&b"abc"[..], &b"de"[..]))); + } + + #[test] + fn preceded() { + named!(prec<&[u8], &[u8] >, + ws!(preceded!( take!(3), tag!("de") )) + ); + + assert_eq!(prec(&b" \t abc de fg"[..]), Done(&b"fg"[..], &b"de"[..])); + } + + #[test] + fn terminated() { + named!(term<&[u8], &[u8] >, + ws!(terminated!( take!(3), tag!("de") )) + ); + + assert_eq!(term(&b" \t abc de fg"[..]), Done(&b"fg"[..], &b"abc"[..])); + } + + #[test] + fn tuple() { + //trace_macros!(true); + named!(tuple_2<&[u8], (&[u8], &[u8]) >, + ws!(tuple!( take!(3), tag!("de") )) + ); + //trace_macros!(false); + + assert_eq!(tuple_2(&b" \t abc de fg"[..]), Done(&b"fg"[..], (&b"abc"[..], &b"de"[..]))); + } + + #[test] + fn levels() { + //trace_macros!(true); + named!(level_2<&[u8], (&[u8], (&[u8], &[u8])) >, + ws!(pair!(take!(3), tuple!( tag!("de"), tag!("fg ") ))) + ); + //trace_macros!(false); + + assert_eq!(level_2(&b" \t abc de fg \t hi "[..]), Done(&b"hi "[..], (&b"abc"[..], (&b"de"[..], &b"fg "[..])))); + } + + #[test] + fn do_parse() { + fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) }; + fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) }; + + //trace_macros!(true); + named!(do_parser<&[u8], (u8, u8)>, + ws!(do_parse!( + tag!("abcd") >> + opt!(tag!("abcd")) >> + aa: ret_int1 >> + tag!("efgh") >> + bb: ret_int2 >> + tag!("efgh") >> + (aa, bb) + )) + ); + + //trace_macros!(false); + + assert_eq!(do_parser(&b"abcd abcd\tefghefghX"[..]), Done(&b"X"[..], (1, 2))); + assert_eq!(do_parser(&b"abcd\tefgh efgh X"[..]), Done(&b"X"[..], (1, 2))); + assert_eq!(do_parser(&b"abcd ab"[..]), Incomplete(Needed::Size(10))); + assert_eq!(do_parser(&b" abcd\tefgh\tef"[..]), Incomplete(Needed::Size(15))); + } + + #[test] + fn permutation() { + //trace_macros!(true); + named!(perm<(&[u8], &[u8], &[u8])>, + ws!(permutation!(tag!("abcd"), tag!("efg"), tag!("hi"))) + ); + //trace_macros!(false); + + let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); + + let a = &b"abcd\tefg \thijk"[..]; + assert_eq!(perm(a), Done(&b"jk"[..], expected)); + let b = &b" efg \tabcdhi jk"[..]; + assert_eq!(perm(b), Done(&b"jk"[..], expected)); + let c = &b" hi efg\tabcdjk"[..]; + assert_eq!(perm(c), Done(&b"jk"[..], expected)); + + let d = &b"efg xyzabcdefghi"[..]; + assert_eq!(perm(d), Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..]))); + + let e = &b" efg \tabc"[..]; + assert_eq!(perm(e), Incomplete(Needed::Size(10))); + } + + #[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { + Done(&b""[..], input) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8],&[u8],&'static str> { + Error(error_code!(ErrorKind::Custom("abcd"))) + } + + fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> { + Done(input, &b""[..]) + } + + fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | dont_work) + } + fn alt2(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | work) + } + fn alt3(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + + let a = &b"\tabcd"[..]; + assert_eq!(alt1(a), Error(error_position!(ErrorKind::Alt, a))); + assert_eq!(alt2(a), Done(&b""[..], a)); + assert_eq!(alt3(a), Done(a, &b""[..])); + + named!(alt4, ws!(alt!(tag!("abcd") | tag!("efgh")))); + let b = &b" efgh "[..]; + assert_eq!(alt4(a), Done(&b""[..], &b"abcd"[..])); + assert_eq!(alt4(b), Done(&b""[..], &b"efgh"[..])); + + // test the alternative syntax + named!(alt5, ws!(alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true }))); + assert_eq!(alt5(a), Done(&b""[..], false)); + assert_eq!(alt5(b), Done(&b""[..], true)); + } + + #[test] + fn alt_complete() { + named!(ac<&[u8], &[u8]>, + ws!(alt_complete!(tag!("abcd") | tag!("ef") | tag!("ghi") | tag!("kl"))) + ); + + let a = &b""[..]; + assert_eq!(ac(a), Incomplete(Needed::Size(2))); + let a = &b" \tef "[..]; + assert_eq!(ac(a), Done(&b""[..], &b"ef"[..])); + let a = &b" cde"[..]; + assert_eq!(ac(a), Error(error_position!(ErrorKind::Alt, &a[1..]))); + } + + #[allow(unused_variables)] + #[test] + fn switch() { + named!(sw, + ws!(switch!(take!(4), + b"abcd" => take!(2) | + b"efgh" => take!(4) + )) + ); + + let a = &b" abcd ef gh"[..]; + assert_eq!(sw(a), Done(&b"gh"[..], &b"ef"[..])); + + let b = &b"\tefgh ijkl "[..]; + assert_eq!(sw(b), Done(&b""[..], &b"ijkl"[..])); + let c = &b"afghijkl"[..]; + assert_eq!(sw(c), Error(error_position!(ErrorKind::Switch, &b"afghijkl"[..]))); + } + + named!(str_parse(&str) -> &str, ws!(tag!("test"))); + #[allow(unused_variables)] + #[test] + fn str_test() { + assert_eq!(str_parse(" \n test\t a\nb"), Done("a\nb", "test")); + } + + // test whitespace parser generation for alt + named!(space, tag!(" ")); + named!(pipeline_statement<&[u8], ()>, + ws!( + do_parse!( + tag!("pipeline") >> + attributes: delimited!(char!('{'), + separated_list!(char!(','), alt!( + space | + space + )), + char!('}')) >> + + ( () ) + ) + ) + ); + +} diff --git third_party/rust/nom/tests/arithmetic.rs third_party/rust/nom/tests/arithmetic.rs index eea990fb07cf..40ffc8cbfdcc 100644 --- third_party/rust/nom/tests/arithmetic.rs +++ third_party/rust/nom/tests/arithmetic.rs @@ -1,22 +1,24 @@ #[macro_use] extern crate nom; -use nom::{IResult,digit, multispace}; +use nom::{IResult,digit}; + +// Parser definition use std::str; use std::str::FromStr; -named!(parens, delimited!( - delimited!(opt!(multispace), tag!("("), opt!(multispace)), - expr, - delimited!(opt!(multispace), tag!(")"), opt!(multispace)) - ) -); +// We parse any expr surrounded by parens, ignoring all whitespaces around those +named!(parens, ws!(delimited!( tag!("("), expr, tag!(")") )) ); +// We transform an integer string into a i64, ignoring surrounding whitespaces +// We look for a digit suite, and try to convert it. +// If either str::from_utf8 or FromStr::from_str fail, +// we fallback to the parens parser defined above named!(factor, alt!( map_res!( map_res!( - delimited!(opt!(multispace), digit, opt!(multispace)), + ws!(digit), str::from_utf8 ), FromStr::from_str @@ -25,27 +27,32 @@ named!(factor, alt!( ) ); -named!(term , chain!( - mut acc: factor ~ - many0!( - alt!( - tap!(mul: preceded!(tag!("*"), factor) => acc = acc * mul) | - tap!(div: preceded!(tag!("/"), factor) => acc = acc / div) - ) - ), - || { return acc } +// We read an initial factor and for each time we find +// a * or / operator followed by another factor, we do +// the math by folding everything +named!(term , do_parse!( + init: factor >> + res: fold_many0!( + pair!(alt!(tag!("*") | tag!("/")), factor), + init, + |acc, (op, val): (&[u8], i64)| { + if (op[0] as char) == '*' { acc * val } else { acc / val } + } + ) >> + (res) ) ); -named!(expr , chain!( - mut acc: term ~ - many0!( - alt!( - tap!(add: preceded!(tag!("+"), term) => acc = acc + add) | - tap!(sub: preceded!(tag!("-"), term) => acc = acc - sub) - ) - ), - || { return acc } +named!(expr , do_parse!( + init: term >> + res: fold_many0!( + pair!(alt!(tag!("+") | tag!("-")), term), + init, + |acc, (op, val): (&[u8], i64)| { + if (op[0] as char) == '+' { acc + val } else { acc - val } + } + ) >> + (res) ) ); diff --git third_party/rust/nom/tests/arithmetic_ast.rs third_party/rust/nom/tests/arithmetic_ast.rs index 9a8956936ff8..d48b9028b9f7 100644 --- third_party/rust/nom/tests/arithmetic_ast.rs +++ third_party/rust/nom/tests/arithmetic_ast.rs @@ -86,27 +86,27 @@ fn fold_exprs(initial: Expr, remainder: Vec<(Oper, Expr)>) -> Expr { }) } -named!(term< Expr >, chain!( - initial: factor ~ +named!(term< Expr >, do_parse!( + initial: factor >> remainder: many0!( alt!( - chain!(tag!("*") ~ mul: factor, || { (Oper::Mul, mul) }) | - chain!(tag!("/") ~ div: factor, || { (Oper::Div, div) }) + do_parse!(tag!("*") >> mul: factor >> (Oper::Mul, mul)) | + do_parse!(tag!("/") >> div: factor >> (Oper::Div, div)) ) - ), - || fold_exprs(initial, remainder)) -); + ) >> + (fold_exprs(initial, remainder)) +)); -named!(expr< Expr >, chain!( - initial: term ~ +named!(expr< Expr >, do_parse!( + initial: term >> remainder: many0!( alt!( - chain!(tag!("+") ~ add: term, || { (Oper::Add, add) }) | - chain!(tag!("-") ~ sub: term, || { (Oper::Sub, sub) }) + do_parse!(tag!("+") >> add: term >> (Oper::Add, add)) | + do_parse!(tag!("-") >> sub: term >> (Oper::Sub, sub)) ) - ), - || fold_exprs(initial, remainder)) -); + ) >> + (fold_exprs(initial, remainder)) +)); #[test] fn factor_test() { diff --git third_party/rust/nom/tests/blockbuf-arithmetic.rs third_party/rust/nom/tests/blockbuf-arithmetic.rs new file mode 100644 index 000000000000..fe655185df54 --- /dev/null +++ third_party/rust/nom/tests/blockbuf-arithmetic.rs @@ -0,0 +1,321 @@ +/* +#[macro_use] +extern crate nom; +extern crate bytes; + +use nom::{Compare,CompareResult,InputLength,InputIter,Slice,HexDisplay}; + +use std::str; +use std::str::FromStr; +use bytes::{Buf,MutBuf}; +use bytes::buf::{BlockBuf,BlockBufCursor}; +use std::ops::{Range,RangeTo,RangeFrom,RangeFull}; +use std::iter::{Enumerate,Iterator}; +use std::fmt; +use std::cmp::{min,PartialEq}; + +#[derive(Clone,Copy)] +#[repr(C)] +pub struct BlockSlice<'a> { + buf: &'a BlockBuf, + start: usize, + end: usize, +} + +impl<'a> BlockSlice<'a> { + fn cursor(&self) -> WrapCursor<'a> { + let mut cur = self.buf.buf(); + cur.advance(self.start); + WrapCursor { + cursor: cur, + length: self.end - self.start, + } + } +} + +impl<'a> fmt::Debug for BlockSlice<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "BlockSlice {{ start: {}, end: {}, data:\n{}\n}}", self.start, self.end, self.buf.bytes().unwrap_or(&b""[..]).to_hex(16)) + } +} + +impl<'a> PartialEq for BlockSlice<'a> { + fn eq(&self, other: &BlockSlice<'a>) -> bool { + let bufs = (self.buf as *const BlockBuf) == (other.buf as *const BlockBuf); + self.start == other.start && self.end == other.end && bufs + } +} + +impl<'a> Slice> for BlockSlice<'a> { + fn slice(&self, range:Range) -> Self { + BlockSlice { + buf: self.buf, + start: self.start + range.start, + //FIXME: check for valid end here + end: self.start + range.end, + } + } +} + +impl<'a> Slice> for BlockSlice<'a> { + fn slice(&self, range:RangeTo) -> Self { + self.slice(0..range.end) + } +} + +impl<'a> Slice> for BlockSlice<'a> { + fn slice(&self, range:RangeFrom) -> Self { + self.slice(range.start..self.end - self.start) + } +} + +impl<'a> Slice for BlockSlice<'a> { + fn slice(&self, _:RangeFull) -> Self { + BlockSlice { + buf: self.buf, + start: self.start, + end: self.end, + } + } +} + + +impl<'a> InputIter for BlockSlice<'a> { + type Item = u8; + type RawItem = u8; + type Iter = Enumerate>; + type IterElem = WrapCursor<'a>; + + fn iter_indices(&self) -> Self::Iter { + self.cursor().enumerate() + } + fn iter_elements(&self) -> Self::IterElem { + self.cursor() + } + fn position

(&self, predicate: P) -> Option where P: Fn(Self::RawItem) -> bool { + self.cursor().position(|b| predicate(b)) + } + fn slice_index(&self, count:usize) -> Option { + if self.end - self.start >= count { + Some(count) + } else { + None + } + } +} + + +impl<'a> InputLength for BlockSlice<'a> { + fn input_len(&self) -> usize { + self.end - self.start + } +} + +impl<'a,'b> Compare<&'b[u8]> for BlockSlice<'a> { + fn compare(&self, t: &'b[u8]) -> CompareResult { + let len = self.end - self.start; + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = self.slice(..m); + let b = &t[..m]; + + for (a,b) in reduced.cursor().zip(b.iter()) { + if a != *b { + return CompareResult::Error; + } + } + if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } + + + #[inline(always)] + fn compare_no_case(&self, t: &'b[u8]) -> CompareResult { + let len = self.end - self.start; + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = self.slice(..m); + let other = &t[..m]; + + if !reduced.cursor().zip(other).all(|(a, b)| { + match (a,*b) { + (0...64, 0...64) | (91...96, 91...96) | (123...255, 123...255) => a == *b, + (65...90, 65...90) | (97...122, 97...122) | (65...90, 97...122 ) |(97...122, 65...90) => { + a & 0b01000000 == *b & 0b01000000 + } + _ => false + } + }) { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } +} + +impl<'a,'b> Compare<&'b str> for BlockSlice<'a> { + fn compare(&self, t: &'b str) -> CompareResult { + self.compare(str::as_bytes(t)) + } + fn compare_no_case(&self, t: &'b str) -> CompareResult { + self.compare_no_case(str::as_bytes(t)) + } +} + +//Wrapper to implement Iterator on BlockBufCursor +pub struct WrapCursor<'a> { + pub cursor: BlockBufCursor<'a>, + pub length: usize, +} + +impl<'a> Iterator for WrapCursor<'a> { + type Item = u8; + fn next(&mut self) -> Option { + //println!("NEXT: length={}, remaining={}", self.length, self.cursor.remaining()); + if min(self.length, self.cursor.remaining()) > 0 { + self.length -=1; + Some(self.cursor.read_u8()) + } else { + None + } + } +} + +//Reimplement eat_separator instead of fixing iterators +#[macro_export] +macro_rules! block_eat_separator ( + ($i:expr, $arr:expr) => ( + { + use nom::{InputLength,InputIter,Slice}; + if ($i).input_len() == 0 { + nom::IResult::Done($i, ($i).slice(0..0)) + } else { + match ($i).iter_indices().position(|(_, item)| { + for (_,c) in ($arr).iter_indices() { + if *c == item { return false; } + } + true + }) { + Some(index) => { + nom::IResult::Done(($i).slice(index..), ($i).slice(..index)) + }, + None => { + nom::IResult::Done(($i).slice(($i).input_len()..), $i) + } + } + } + } + ) +); + +#[macro_export] +macro_rules! block_named ( + ($name:ident, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( i: BlockSlice<'a> ) -> nom::IResult, BlockSlice<'a>, u32> { + $submac!(i, $($args)*) + } + ); + ($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + fn $name<'a>( i: BlockSlice<'a> ) -> nom::IResult, $o, u32> { + $submac!(i, $($args)*) + } + ); +); + +block_named!(sp, block_eat_separator!(&b" \t\r\n"[..])); + +macro_rules! block_ws ( + ($i:expr, $($args:tt)*) => ( + { + sep!($i, sp, $($args)*) + } + ) +); + +block_named!(digit, is_a!("0123456789")); + +block_named!(parens, block_ws!(delimited!( tag!("("), expr, tag!(")") )) ); + + +block_named!(factor, alt!( + map_res!( + block_ws!(digit), + to_i64 + ) + | parens + ) +); + +block_named!(term , do_parse!( + init: factor >> + res: fold_many0!( + pair!(alt!(tag!("*") | tag!("/")), factor), + init, + |acc, (op, val): (BlockSlice, i64)| { + if (op.cursor().next().unwrap() as char) == '*' { acc * val } else { acc / val } + } + ) >> + (res) + ) +); + +block_named!(expr , do_parse!( + init: term >> + res: fold_many0!( + pair!(alt!(tag!("+") | tag!("-")), term), + init, + |acc, (op, val): (BlockSlice, i64)| { + if (op.cursor().next().unwrap() as char) == '+' { acc + val } else { acc - val } + } + ) >> + (res) + ) +); + + +fn blockbuf_from(input: &[u8]) -> BlockBuf { + let mut b = BlockBuf::new(2, 100); + b.copy_from(input); + b +} + + +fn sl<'a>(input: &'a BlockBuf) -> BlockSlice<'a> { + BlockSlice { + buf: input, + start: 0, + end: input.len(), + } +} + +fn to_i64<'a>(input: BlockSlice<'a>) -> Result { + let v: Vec = input.cursor().collect(); + + match str::from_utf8(&v) { + Err(_) => Err(()), + Ok(s) => match FromStr::from_str(s) { + Err(_) => Err(()), + Ok(i) => Ok(i) + } + } +} + +#[test] +fn factor_test() { + let a = blockbuf_from(&b"3"[..]); + println!("calculated: {:?}", factor(sl(&a))); +} + +#[test] +fn parens_test() { + let input1 = blockbuf_from(&b" 2* ( 3 + 4 ) "[..]); + println!("calculated 1: {:?}", expr(sl(&input1))); + let input2 = blockbuf_from(&b" 2*2 / ( 5 - 1) + 3"[..]); + println!("calculated 2: {:?}", expr(sl(&input2))); +} +*/ diff --git third_party/rust/nom/tests/cross_function_backtracking.rs third_party/rust/nom/tests/cross_function_backtracking.rs index 592670c43999..3163037377a0 100644 --- third_party/rust/nom/tests/cross_function_backtracking.rs +++ third_party/rust/nom/tests/cross_function_backtracking.rs @@ -5,9 +5,10 @@ /// The solution here wraps `IResult` in a `Result`: a `Ok` indicates usual /// backtracking, `Err` indicates that we must "cut". -#[macro_use] -extern crate nom; +#[allow(unused_imports)] +#[macro_use] extern crate nom; +#[cfg(feature = "verbose_errors")] macro_rules! n ( ($name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( fn $name( i: $i ) -> std::result::Result, nom::Err<$i, u32>> { @@ -61,6 +62,7 @@ macro_rules! n ( ); ); +#[cfg(feature = "verbose_errors")] macro_rules! cut ( ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( { @@ -82,6 +84,7 @@ macro_rules! cut ( ); ); +#[cfg(feature = "verbose_errors")] macro_rules! c ( ($i:expr, $f:expr) => ( { @@ -97,14 +100,16 @@ macro_rules! c ( ); ); +#[cfg(feature = "verbose_errors")] n!(pub foo< bool >, - chain!( - tag!("a") ~ - cut!(nom::ErrorKind::Custom(42),dbg_dmp!(tag!("b"))) , - || { true } + do_parse!( + tag!("a") >> + cut!(nom::ErrorKind::Custom(42),dbg_dmp!(tag!("b"))) >> + (true) ) ); +#[cfg(feature = "verbose_errors")] n!(pub foos< Vec >, delimited!( tag!("("), @@ -113,6 +118,7 @@ n!(pub foos< Vec >, ) ); +#[cfg(feature = "verbose_errors")] #[test] fn test_ok() { let r = foos(b"(abab)"); @@ -123,6 +129,7 @@ fn test_ok() { } } +#[cfg(feature = "verbose_errors")] #[test] fn test_err() { let input = b"(ac)"; diff --git third_party/rust/nom/tests/float.rs third_party/rust/nom/tests/float.rs new file mode 100644 index 000000000000..5710e260365b --- /dev/null +++ third_party/rust/nom/tests/float.rs @@ -0,0 +1,46 @@ +#[macro_use] +extern crate nom; + +use nom::{IResult,digit}; + +use std::str; +use std::str::FromStr; + +named!(unsigned_float , map_res!( + map_res!( + recognize!( + alt!( + delimited!(digit, tag!("."), opt!(complete!(digit))) | + delimited!(opt!(digit), tag!("."), digit) + ) + ), + str::from_utf8 + ), + FromStr::from_str +)); + +named!(float , map!( + pair!( + opt!(alt!(tag!("+") | tag!("-"))), + unsigned_float + ), + |(sign, value): (Option<&[u8]>, f32)| { + sign.and_then(|s| if s[0] == ('-' as u8) { Some(-1f32) } else { None }).unwrap_or(1f32) * value + } +)); + +#[test] +fn unsigned_float_test() { + assert_eq!(unsigned_float(&b"123.456"[..]), IResult::Done(&b""[..], 123.456)); + assert_eq!(unsigned_float(&b"0.123"[..]), IResult::Done(&b""[..], 0.123)); + assert_eq!(unsigned_float(&b"123.0"[..]), IResult::Done(&b""[..], 123.0)); + assert_eq!(unsigned_float(&b"123."[..]), IResult::Done(&b""[..], 123.0)); + assert_eq!(unsigned_float(&b".123"[..]), IResult::Done(&b""[..], 0.123)); +} + +#[test] +fn float_test() { + assert_eq!(float(&b"123.456"[..]), IResult::Done(&b""[..], 123.456)); + assert_eq!(float(&b"+123.456"[..]), IResult::Done(&b""[..], 123.456)); + assert_eq!(float(&b"-123.456"[..]), IResult::Done(&b""[..], -123.456)); +} diff --git third_party/rust/nom/tests/ini.rs third_party/rust/nom/tests/ini.rs index a3a33431bf02..c72177071697 100644 --- third_party/rust/nom/tests/ini.rs +++ third_party/rust/nom/tests/ini.rs @@ -1,83 +1,68 @@ - #[macro_use] extern crate nom; -use nom::{IResult,not_line_ending, space, alphanumeric, multispace}; +use nom::{IResult, space, alphanumeric, multispace}; use std::str; use std::collections::HashMap; named!(category<&str>, map_res!( - terminated!( - delimited!(tag!("["), take_until!("]"), tag!("]")), - opt!(multispace) + delimited!( + char!('['), + take_while!(call!(|c| c != ']' as u8)), + char!(']') ), str::from_utf8 )); named!(key_value <&[u8],(&str,&str)>, - chain!( - key: map_res!(alphanumeric, std::str::from_utf8) ~ - space? ~ - tag!("=") ~ - space? ~ - val: map_res!( - take_until_either!("\n;"), + do_parse!( + key: map_res!(alphanumeric, str::from_utf8) + >> opt!(space) + >> char!('=') + >> opt!(space) + >> val: map_res!( + take_while!(call!(|c| c != '\n' as u8 && c != ';' as u8)), str::from_utf8 - ) ~ - space? ~ - chain!( - tag!(";") ~ - not_line_ending , - ||{} - ) ? ~ - multispace? , - ||{(key, val)} + ) + >> opt!(pair!(char!(';'), take_while!(call!(|c| c != '\n' as u8)))) + >> (key, val) ) ); -named!(keys_and_values_aggregator<&[u8], Vec<(&str,&str)> >, many0!(key_value)); +named!(keys_and_values<&[u8], HashMap<&str, &str> >, + map!( + many0!(terminated!(key_value, opt!(multispace))), + |vec: Vec<_>| vec.into_iter().collect() + ) +); -fn keys_and_values(input:&[u8]) -> IResult<&[u8], HashMap<&str, &str> > { - let mut h: HashMap<&str, &str> = HashMap::new(); - - match keys_and_values_aggregator(input) { - IResult::Done(i,tuple_vec) => { - for &(k,v) in &tuple_vec { - h.insert(k, v); - } - IResult::Done(i, h) - }, - IResult::Incomplete(a) => IResult::Incomplete(a), - IResult::Error(a) => IResult::Error(a) - } -} named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>, - chain!( - category: category ~ - keys: keys_and_values , - move ||{(category, keys)} + do_parse!( + category: category >> + opt!(multispace) >> + keys: keys_and_values >> + (category, keys) ) ); -named!(categories_aggregator<&[u8], Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys)); - -fn categories(input: &[u8]) -> IResult<&[u8], HashMap<&str, HashMap<&str, &str> > > { - let mut h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); - - match categories_aggregator(input) { - IResult::Done(i,tuple_vec) => { - for &(k,ref v) in &tuple_vec { - h.insert(k, v.clone()); - } - IResult::Done(i, h) - }, - IResult::Incomplete(a) => IResult::Incomplete(a), - IResult::Error(a) => IResult::Error(a) - } -} +named!(categories<&[u8], HashMap<&str, HashMap<&str,&str> > >, + map!( + many0!( + separated_pair!( + category, + opt!(multispace), + map!( + many0!(terminated!(key_value, opt!(multispace))), + |vec: Vec<_>| vec.into_iter().collect() + ) + ) + ), + |vec: Vec<_>| vec.into_iter().collect() + ) +); #[test] fn parse_category_test() { @@ -86,7 +71,7 @@ fn parse_category_test() { parameter=value key = value2"[..]; - let ini_without_category = &b"parameter=value + let ini_without_category = &b"\n\nparameter=value key = value2"[..]; let res = category(ini_file); @@ -104,7 +89,7 @@ fn parse_key_value_test() { let ini_file = &b"parameter=value key = value2"[..]; - let ini_without_key_value = &b"key = value2"[..]; + let ini_without_key_value = &b"\nkey = value2"[..]; let res = key_value(ini_file); println!("{:?}", res); @@ -122,7 +107,7 @@ fn parse_key_value_with_space_test() { let ini_file = &b"parameter = value key = value2"[..]; - let ini_without_key_value = &b"key = value2"[..]; + let ini_without_key_value = &b"\nkey = value2"[..]; let res = key_value(ini_file); println!("{:?}", res); @@ -139,7 +124,7 @@ fn parse_key_value_with_comment_test() { let ini_file = &b"parameter=value;abc key = value2"[..]; - let ini_without_key_value = &b"key = value2"[..]; + let ini_without_key_value = &b"\nkey = value2"[..]; let res = key_value(ini_file); println!("{:?}", res); diff --git third_party/rust/nom/tests/ini_str.rs third_party/rust/nom/tests/ini_str.rs index c69756dc2aca..330e4901f58a 100644 --- third_party/rust/nom/tests/ini_str.rs +++ third_party/rust/nom/tests/ini_str.rs @@ -1,4 +1,3 @@ - #[macro_use] extern crate nom; @@ -36,40 +35,35 @@ fn right_bracket(c:char) -> bool { } named!(category <&str, &str>, - chain!( - tag_s!("[") ~ - name: take_till_s!(right_bracket) ~ - tag_s!("]") ~ - space_or_line_ending? , - ||{ name } + do_parse!( + tag_s!("[") >> + name: take_till_s!(right_bracket) >> + tag_s!("]") >> + opt!(space_or_line_ending) >> + (name) ) ); named!(key_value <&str,(&str,&str)>, - chain!( - key: alphanumeric ~ - space? ~ - tag_s!("=") ~ - space? ~ - val: take_till_s!(is_line_ending_or_comment) ~ - space? ~ - pair!(tag_s!(";"), not_line_ending)? ~ - space_or_line_ending? , - ||{(key, val)} + do_parse!( + key: alphanumeric >> + opt!(space) >> + tag_s!("=") >> + opt!(space) >> + val: take_till_s!(is_line_ending_or_comment) >> + opt!(space) >> + opt!(pair!(tag_s!(";"), not_line_ending)) >> + opt!(space_or_line_ending) >> + (key, val) ) ); named!(keys_and_values_aggregator<&str, Vec<(&str,&str)> >, many0!(key_value)); fn keys_and_values(input:&str) -> IResult<&str, HashMap<&str, &str> > { - let mut h: HashMap<&str, &str> = HashMap::new(); - match keys_and_values_aggregator(input) { IResult::Done(i,tuple_vec) => { - for &(k,v) in &tuple_vec { - h.insert(k, v); - } - IResult::Done(i, h) + IResult::Done(i, tuple_vec.into_iter().collect()) }, IResult::Incomplete(a) => IResult::Incomplete(a), IResult::Error(a) => IResult::Error(a) @@ -84,14 +78,9 @@ named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>, named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys)); fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str> > > { - let mut h: HashMap<&str, HashMap<&str, &str>> = HashMap::new(); - match categories_aggregator(input) { IResult::Done(i,tuple_vec) => { - for &(k,ref v) in &tuple_vec { - h.insert(k, v.clone()); - } - IResult::Done(i, h) + IResult::Done(i, tuple_vec.into_iter().collect()) }, IResult::Incomplete(a) => IResult::Incomplete(a), IResult::Error(a) => IResult::Error(a) diff --git third_party/rust/nom/tests/issues.rs third_party/rust/nom/tests/issues.rs index 6466d7f2e069..7a726dc0591b 100644 --- third_party/rust/nom/tests/issues.rs +++ third_party/rust/nom/tests/issues.rs @@ -1,9 +1,10 @@ //#![feature(trace_macros)] +#![allow(dead_code)] + #[macro_use] extern crate nom; -use nom::{IResult,Needed,HexDisplay,space,digit,be_u16}; -use std::str; +use nom::{IResult,Needed,space,be_u16,le_u64}; #[allow(dead_code)] struct Range { @@ -24,23 +25,21 @@ pub fn take_char(input: &[u8]) -> IResult<&[u8], char> { #[allow(dead_code)] named!(range<&[u8], Range>, alt!( - chain!( - start: take_char ~ - tag!("-") ~ - end: take_char, - || { - Range { - start: start, - end: end, - } - } + do_parse!( + start: take_char >> + tag!("-") >> + end: take_char >> + (Range { + start: start, + end: end, + }) ) | map!( take_char, |c| { Range { start: c, - end: c, + end: c, } } ) @@ -66,35 +65,43 @@ fn issue_58() { //trace_macros!(false); -named!(parse_ints< Vec >, many0!(spaces_or_int)); - -fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>{ - println!("{}", input.to_hex(8)); - chain!(input, - opt!(space) ~ - x: digit, - || { - println!("x: {:?}", x); - let result = str::from_utf8(x).unwrap(); - println!("Result: {}", result); - println!("int is empty?: {}", x.is_empty()); - match result.parse(){ - Ok(i) => i, - Err(_) => panic!("UH OH! NOT A DIGIT!") - } - } - ) -} +#[cfg(feature = "std")] +mod parse_int { + use nom::HexDisplay; + use nom::{IResult,space,digit}; + use std::str; + + named!(parse_ints< Vec >, many0!(spaces_or_int)); + + fn spaces_or_int(input: &[u8]) -> IResult<&[u8], i32>{ + println!("{}", input.to_hex(8)); + do_parse!(input, + opt!(complete!(space)) >> + res: map!(complete!(digit), + |x| { + println!("x: {:?}", x); + let result = str::from_utf8(x).unwrap(); + println!("Result: {}", result); + println!("int is empty?: {}", x.is_empty()); + match result.parse(){ + Ok(i) => i, + Err(_) => panic!("UH OH! NOT A DIGIT!") + } + }) >> + (res) + ) + } -#[test] -fn issue_142(){ - let subject = parse_ints(&b"12 34 5689"[..]); - let expected = IResult::Done(&b""[..], vec![12, 34, 5689]); - assert_eq!(subject, expected); - - let subject = parse_ints(&b"12 34 5689 "[..]); - let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]); - assert_eq!(subject, expected) + #[test] + fn issue_142(){ + let subject = parse_ints(&b"12 34 5689"[..]); + let expected = IResult::Done(&b""[..], vec![12, 34, 5689]); + assert_eq!(subject, expected); + + let subject = parse_ints(&b"12 34 5689 "[..]); + let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]); + assert_eq!(subject, expected) + } } #[test] @@ -129,3 +136,22 @@ fn take_till_issue() { assert_eq!(nothing(b""), IResult::Done(&b""[..], &b""[..])); assert_eq!(nothing(b"abc"), IResult::Done(&b"abc"[..], &b""[..])); } + +named!(issue_498< Vec<&[u8]> >, separated_nonempty_list!( opt!(space), tag!("abcd") )); + +named!(issue_308(&str) -> bool, + do_parse! ( + tag_s! ("foo") >> + b: alt_complete! ( + map! (tag_s! ("1"), |_: &str|->bool {true}) | + value! (false) + ) >> + (b) )); + + +fn issue_302(input: &[u8]) -> IResult<&[u8], Option> > { + do_parse!(input, + entries: cond!(true, count!(le_u64, 3)) >> + ( entries ) + ) +} diff --git third_party/rust/nom/tests/json.rs third_party/rust/nom/tests/json.rs new file mode 100644 index 000000000000..b0369166c3b1 --- /dev/null +++ third_party/rust/nom/tests/json.rs @@ -0,0 +1,130 @@ +//#![feature(trace_macros)] + +#[macro_use] +extern crate nom; + +use nom::{digit, alphanumeric}; + +use std::str::{self,FromStr}; +use std::collections::HashMap; + +#[derive(Debug,PartialEq)] +pub enum JsonValue { + Str(String), + Num(f32), + Array(Vec), + Object(HashMap) +} + +// FIXME: since we already parsed a serie of digits and dots, +// we know it is correct UTF-8. no need to use from_utf8 to +// verify it is correct +// FIXME: use alt_complete (implement ws for alt_complete) +named!(unsigned_float , map_res!( + map_res!( + recognize!( + alt_complete!( + delimited!(digit, tag!("."), opt!(complete!(digit))) | + delimited!(opt!(digit), tag!("."), digit) | + digit + ) + ), + str::from_utf8 + ), + FromStr::from_str +)); +named!(float, map!( + pair!( + opt!(alt!(tag!("+") | tag!("-"))), + unsigned_float + ), + |(sign, value): (Option<&[u8]>, f32)| { + sign.and_then(|s| if s[0] == ('-' as u8) { Some(-1f32) } else { None }).unwrap_or(1f32) * value + } +)); + +//FIXME: verify how json strings are formatted +named!(string<&str>, + delimited!( + tag!("\""), + map_res!(escaped!(call!(alphanumeric), '\\', is_a!("\"n\\")), str::from_utf8), + tag!("\"") + ) +); + +named!(array < Vec >, + ws!( + delimited!( + tag!("["), + separated_list!(tag!(","), value), + tag!("]") + ) + ) +); + +named!(key_value<(&str,JsonValue)>, + ws!( + separated_pair!( + string, + tag!(":"), + value + ) + ) +); + +named!(hash< HashMap >, + ws!( + map!( + delimited!( + tag!("{"), + separated_list!(tag!(","), key_value), + tag!("}") + ), + |tuple_vec| { + let mut h: HashMap = HashMap::new(); + for (k, v) in tuple_vec { + h.insert(String::from(k), v); + } + h + } + ) + ) +); + +named!(value, + ws!( + alt!( + hash => { |h| JsonValue::Object(h) } | + array => { |v| JsonValue::Array(v) } | + string => { |s| JsonValue::Str(String::from(s)) } | + float => { |num| JsonValue::Num(num) } + ) + ) +); + + + +#[test] +fn hash_test() { + let test = &b" { \"a\"\t: 42, + \"b\": \"x\" + }"; + +//FIXME: top level value must be an object? + println!("{:?}", value(&test[..])); + //assert!(false); +} + +#[test] +fn parse_example_test() { + let test = &b" { \"a\"\t: 42, + \"b\": [ \"x\", \"y\", 12 ] , + \"c\": { \"hello\" : \"world\" + } + }"; + +//FIXME: top level value must be an object? + println!("{:?}", value(&test[..])); + //assert!(false); +} + diff --git third_party/rust/nom/tests/mp4.rs third_party/rust/nom/tests/mp4.rs index 8c128f57a928..e6d5c9fce7c0 100644 --- third_party/rust/nom/tests/mp4.rs +++ third_party/rust/nom/tests/mp4.rs @@ -4,10 +4,9 @@ #[macro_use] extern crate nom; -use nom::{HexDisplay,Needed,IResult,be_u16,be_u32,be_u64,be_f32,ErrorKind}; +use nom::{HexDisplay,Offset,Needed,IResult,be_u16,be_u32,be_u64,be_f32,ErrorKind}; use nom::{Consumer,ConsumerState,Move,Input,Producer,FileProducer,FileProducerState}; use nom::IResult::*; -use nom::Err::*; use std::str; use std::io::SeekFrom; @@ -94,30 +93,30 @@ pub struct Mvhd64 { #[allow(non_snake_case)] named!(mvhd32 <&[u8], MvhdBox>, - chain!( - version_flags: be_u32 ~ - created_date: be_u32 ~ - modified_date: be_u32 ~ - scale: be_u32 ~ - duration: be_u32 ~ - speed: be_f32 ~ - volume: be_u16 ~ // actually a 2 bytes decimal - take!(10) ~ - scale_a: be_f32 ~ - rotate_b: be_f32 ~ - angle_u: be_f32 ~ - rotate_c: be_f32 ~ - scale_d: be_f32 ~ - angle_v: be_f32 ~ - position_x: be_f32 ~ - position_y: be_f32 ~ - scale_w: be_f32 ~ - preview: be_u64 ~ - poster: be_u32 ~ - selection: be_u64 ~ - current_time: be_u32 ~ - track_id: be_u32, - ||{ + do_parse!( + version_flags: be_u32 >> + created_date: be_u32 >> + modified_date: be_u32 >> + scale: be_u32 >> + duration: be_u32 >> + speed: be_f32 >> + volume: be_u16 >> // actually a 2 bytes decimal + take!(10) >> + scale_a: be_f32 >> + rotate_b: be_f32 >> + angle_u: be_f32 >> + rotate_c: be_f32 >> + scale_d: be_f32 >> + angle_v: be_f32 >> + position_x: be_f32 >> + position_y: be_f32 >> + scale_w: be_f32 >> + preview: be_u64 >> + poster: be_u32 >> + selection: be_u64 >> + current_time: be_u32 >> + track_id: be_u32 >> + ( MvhdBox::M32(Mvhd32 { version_flags: version_flags, created_date: created_date, @@ -141,36 +140,35 @@ named!(mvhd32 <&[u8], MvhdBox>, current_time: current_time, track_id: track_id }) - } - ) + )) ); #[allow(non_snake_case)] named!(mvhd64 <&[u8], MvhdBox>, - chain!( - version_flags: be_u32 ~ - created_date: be_u64 ~ - modified_date: be_u64 ~ - scale: be_u32 ~ - duration: be_u64 ~ - speed: be_f32 ~ - volume: be_u16 ~ // actually a 2 bytes decimal - take!(10) ~ - scale_a: be_f32 ~ - rotate_b: be_f32 ~ - angle_u: be_f32 ~ - rotate_c: be_f32 ~ - scale_d: be_f32 ~ - angle_v: be_f32 ~ - position_x: be_f32 ~ - position_y: be_f32 ~ - scale_w: be_f32 ~ - preview: be_u64 ~ - poster: be_u32 ~ - selection: be_u64 ~ - current_time: be_u32 ~ - track_id: be_u32, - ||{ + do_parse!( + version_flags: be_u32 >> + created_date: be_u64 >> + modified_date: be_u64 >> + scale: be_u32 >> + duration: be_u64 >> + speed: be_f32 >> + volume: be_u16 >> // actually a 2 bytes decimal + take!(10) >> + scale_a: be_f32 >> + rotate_b: be_f32 >> + angle_u: be_f32 >> + rotate_c: be_f32 >> + scale_d: be_f32 >> + angle_v: be_f32 >> + position_x: be_f32 >> + position_y: be_f32 >> + scale_w: be_f32 >> + preview: be_u64 >> + poster: be_u32 >> + selection: be_u64 >> + current_time: be_u32 >> + track_id: be_u32 >> + ( MvhdBox::M64(Mvhd64 { version_flags: version_flags, created_date: created_date, @@ -194,8 +192,7 @@ named!(mvhd64 <&[u8], MvhdBox>, current_time: current_time, track_id: track_id }) - } - ) + )) ); #[derive(Debug,Clone)] @@ -246,11 +243,11 @@ struct MP4BoxHeader { named!(brand_name<&[u8],&str>, map_res!(take!(4), str::from_utf8)); named!(filetype_parser<&[u8], FileType>, - chain!( - m: brand_name ~ - v: take!(4) ~ - c: many0!(brand_name) , - ||{ FileType{ major_brand: m, major_brand_version:v, compatible_brands: c } } + do_parse!( + m: brand_name >> + v: take!(4) >> + c: many0!(brand_name) >> + (FileType{ major_brand: m, major_brand_version:v, compatible_brands: c }) ) ); @@ -262,7 +259,7 @@ fn mvhd_box(input:&[u8]) -> IResult<&[u8],MvhdBox> { } else if input.len() == 112 { mvhd64(input) } else { - Error(Position(ErrorKind::Custom(32),input)) + Error(error_position!(ErrorKind::Custom(32),input)) }; println!("res: {:?}", res); res @@ -303,18 +300,18 @@ named!(moov_type<&[u8], MP4BoxType>, ); named!(box_header<&[u8],MP4BoxHeader>, - chain!( - length: be_u32 ~ - tag: box_type , - || { MP4BoxHeader{ length: length, tag: tag} } + do_parse!( + length: be_u32 >> + tag: box_type >> + (MP4BoxHeader{ length: length, tag: tag}) ) ); named!(moov_header<&[u8],MP4BoxHeader>, - chain!( - length: be_u32 ~ - tag: moov_type , - || { MP4BoxHeader{ length: length, tag: tag} } + do_parse!( + length: be_u32 >> + tag: moov_type >> + (MP4BoxHeader{ length: length, tag: tag}) ) ); diff --git third_party/rust/nom/tests/multiline.rs third_party/rust/nom/tests/multiline.rs new file mode 100644 index 000000000000..90164729f46a --- /dev/null +++ third_party/rust/nom/tests/multiline.rs @@ -0,0 +1,21 @@ +#[macro_use] +extern crate nom; + +use nom::{IResult,alphanumeric,eol}; + +use std::str; + +named!(end_of_line, alt!(eof!() | eol)); +named!(read_line <&str>, map_res!( + terminated!(alphanumeric, end_of_line), + str::from_utf8 +)); +named!(read_lines >, many0!(read_line)); + +#[test] +fn read_lines_test() { + let res = IResult::Done(&b""[..], vec!["Duck", "Dog", "Cow"]); + + assert_eq!(read_lines(&b"Duck\nDog\nCow\n"[..]), res); + assert_eq!(read_lines(&b"Duck\nDog\nCow"[..]), res); +} diff --git third_party/rust/nom/tests/named_args.rs third_party/rust/nom/tests/named_args.rs new file mode 100644 index 000000000000..e998a0845d45 --- /dev/null +++ third_party/rust/nom/tests/named_args.rs @@ -0,0 +1,108 @@ +#[macro_use] +extern crate nom; + +use nom::{IResult,digit}; + +// Parser definition + +use std::str; +use std::str::FromStr; + +use self::Operator::*; + +enum Operator { + Slash, + Star, +} + +impl Operator { + fn to_str(&self) -> &'static str { + match *self { + Slash => "/", + Star => "*", + } + } +} + +// Parse the specified `Operator`. +named_args!(operator(op: Operator) <&[u8]>, + tag!(op.to_str()) +); + +// We parse any expr surrounded by the tags `open_tag` and `close_tag`, ignoring all whitespaces around those +named_args!(brackets<'a>(open_tag: &str, close_tag: &str) , ws!(delimited!( tag!(open_tag), expr, tag!(close_tag) )) ); + +// We transform an integer string into a i64, ignoring surrounding whitespaces +// We look for a digit suite, and try to convert it. +// If either str::from_utf8 or FromStr::from_str fail, +// we fallback to the brackets parser defined above +named!(factor, alt!( + map_res!( + map_res!( + ws!(digit), + str::from_utf8 + ), + FromStr::from_str + ) + | call!(brackets, "(", ")") + ) +); + +// We read an initial factor and for each time we find +// a * or / operator followed by another factor, we do +// the math by folding everything +named!(term , do_parse!( + init: factor >> + res: fold_many0!( + pair!(alt!(call!(operator, Star) | call!(operator, Slash)), factor), + init, + |acc, (op, val): (&[u8], i64)| { + if (op[0] as char) == '*' { acc * val } else { acc / val } + } + ) >> + (res) + ) +); + +named!(expr , do_parse!( + init: term >> + res: fold_many0!( + pair!(alt!(tag!("+") | tag!("-")), term), + init, + |acc, (op, val): (&[u8], i64)| { + if (op[0] as char) == '+' { acc + val } else { acc - val } + } + ) >> + (res) + ) +); + +#[test] +fn factor_test() { + assert_eq!(factor(&b"3"[..]), IResult::Done(&b""[..], 3)); + assert_eq!(factor(&b" 12"[..]), IResult::Done(&b""[..], 12)); + assert_eq!(factor(&b"537 "[..]), IResult::Done(&b""[..], 537)); + assert_eq!(factor(&b" 24 "[..]), IResult::Done(&b""[..], 24)); +} + + +#[test] +fn term_test() { + assert_eq!(term(&b" 12 *2 / 3"[..]), IResult::Done(&b""[..], 8)); + assert_eq!(term(&b" 2* 3 *2 *2 / 3"[..]), IResult::Done(&b""[..], 8)); + assert_eq!(term(&b" 48 / 3/2"[..]), IResult::Done(&b""[..], 8)); +} + +#[test] +fn expr_test() { + assert_eq!(expr(&b" 1 + 2 "[..]), IResult::Done(&b""[..], 3)); + assert_eq!(expr(&b" 12 + 6 - 4+ 3"[..]), IResult::Done(&b""[..], 17)); + assert_eq!(expr(&b" 1 + 2*3 + 4"[..]), IResult::Done(&b""[..], 11)); +} + +#[test] +fn parens_test() { + assert_eq!(expr(&b" ( 2 )"[..]), IResult::Done(&b""[..], 2)); + assert_eq!(expr(&b" 2* ( 3 + 4 ) "[..]), IResult::Done(&b""[..], 14)); + assert_eq!(expr(&b" 2*2 / ( 5 - 1) + 3"[..]), IResult::Done(&b""[..], 4)); +} diff --git third_party/rust/nom/tests/omnom.rs third_party/rust/nom/tests/omnom.rs index b8cfa04fb7e4..f90b37919120 100644 --- third_party/rust/nom/tests/omnom.rs +++ third_party/rust/nom/tests/omnom.rs @@ -3,7 +3,7 @@ #[macro_use] extern crate nom; -use nom::{Producer,Consumer,ConsumerState,Input,Move,MemProducer,IResult,HexDisplay}; +use nom::{Producer,Consumer,ConsumerState,Input,Move,MemProducer,IResult,Offset}; #[derive(PartialEq,Eq,Debug)] enum State { diff --git third_party/rust/nom/tests/overflow.rs third_party/rust/nom/tests/overflow.rs new file mode 100644 index 000000000000..d20099fe42ed --- /dev/null +++ third_party/rust/nom/tests/overflow.rs @@ -0,0 +1,99 @@ +#[macro_use] +extern crate nom; + +use nom::{IResult,Needed,be_u8,be_u64}; + +// Parser definition + +// We request a length that would trigger an overflow if computing consumed + requested +named!(parser01<&[u8],()>, + do_parse!( + hdr: take!(1) >> + data: take!(18446744073709551615) >> + ( () ) + ) +); + +// We request a length that would trigger an overflow if computing consumed + requested +named!(parser02<&[u8],(&[u8],&[u8])>, + tuple!(take!(1),take!(18446744073709551615)) +); + +#[test] +fn overflow_incomplete_do_parse() { + assert_eq!(parser01(&b"3"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_tuple() { + assert_eq!(parser02(&b"3"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_length_bytes() { + named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) ); + + // Trigger an overflow in length_bytes + assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_many0() { + named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) ); + + // Trigger an overflow in many0 + assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +#[cfg(feature = "std")] +fn overflow_incomplete_many1() { + named!(multi<&[u8], Vec<&[u8]> >, many1!( length_bytes!(be_u64) ) ); + + // Trigger an overflow in many1 + assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_many_till() { + named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( length_bytes!(be_u64), tag!("abc") ) ); + + // Trigger an overflow in many_till + assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_many_m_n() { + named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, length_bytes!(be_u64) ) ); + + // Trigger an overflow in many_m_n + assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_count() { + named!(counter<&[u8], Vec<&[u8]> >, count!( length_bytes!(be_u64), 2 ) ); + + assert_eq!(counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_count_fixed() { + named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], length_bytes!(be_u64), 2 ) ); + + assert_eq!(counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_length_count() { + named!(multi<&[u8], Vec<&[u8]> >, length_count!( be_u8, length_bytes!(be_u64) ) ); + + assert_eq!(multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} + +#[test] +fn overflow_incomplete_length_data() { + named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) ); + + assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), IResult::Incomplete(Needed::Unknown)); +} diff --git third_party/rust/nom/tests/reborrow_fold.rs third_party/rust/nom/tests/reborrow_fold.rs new file mode 100644 index 000000000000..0527cb2c83ee --- /dev/null +++ third_party/rust/nom/tests/reborrow_fold.rs @@ -0,0 +1,16 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + + +#[macro_use] +extern crate nom; + +use std::str; + + +named_args!(atom<'a>(tomb: &'a mut ()), + map!(map_res!(is_not_s!(" \t\r\n()"), str::from_utf8), ToString::to_string)); + + +named_args!(list<'a>(tomb: &'a mut ()), + delimited!(char!('('), fold_many0!(call!(atom, tomb), "".to_string(), |acc: String, next: String| acc + next.as_str()), char!(')'))); Index: head/www/firefox/files/patch-z-bug517422 =================================================================== --- head/www/firefox/files/patch-z-bug517422 (revision 472726) +++ head/www/firefox/files/patch-z-bug517422 (revision 472727) @@ -1,496 +1,496 @@ # Allow more config/external/ libs built against system-wide version. diff --git build/moz.configure/old.configure build/moz.configure/old.configure index d0fe7accffe6..c46bdd023225 100644 --- build/moz.configure/old.configure +++ build/moz.configure/old.configure @@ -285,7 +285,12 @@ def old_configure_options(*options): '--with-system-libvpx', '--with-system-nspr', '--with-system-nss', + '--with-system-ogg', '--with-system-png', + '--with-system-soundtouch', + '--with-system-theora', + '--with-system-tremor', + '--with-system-vorbis', '--with-system-zlib', '--with-thumb', '--with-thumb-interwork', diff --git config/external/moz.build config/external/moz.build index 029ff8504795..2c3a40caa1ba 100644 --- config/external/moz.build +++ config/external/moz.build @@ -23,12 +23,21 @@ external_dirs += ['modules/woff2'] external_dirs += ['modules/xz-embedded'] -if CONFIG['MOZ_VORBIS']: +if not CONFIG['MOZ_SYSTEM_OGG']: + external_dirs += ['media/libogg'] + +if CONFIG['MOZ_VORBIS'] and not CONFIG['MOZ_SYSTEM_VORBIS']: external_dirs += ['media/libvorbis'] -if CONFIG['MOZ_TREMOR']: +if CONFIG['MOZ_TREMOR'] and not CONFIG['MOZ_SYSTEM_TREMOR']: external_dirs += ['media/libtremor'] +if not CONFIG['MOZ_SYSTEM_THEORA']: + external_dirs += ['media/libtheora'] + +if not CONFIG['MOZ_SYSTEM_SOUNDTOUCH']: + external_dirs += ['media/libsoundtouch'] + if CONFIG['MOZ_WEBM_ENCODER']: external_dirs += ['media/libmkv'] @@ -51,11 +60,8 @@ external_dirs += [ 'media/kiss_fft', 'media/libcubeb', 'media/libnestegg', - 'media/libogg', 'media/libopus', - 'media/libtheora', 'media/libspeex_resampler', - 'media/libsoundtouch', 'media/mp4parse-rust', 'media/psshparser' ] diff --git config/system-headers.mozbuild config/system-headers.mozbuild index 09d3db5ca8c0..c6533b84c470 100644 --- config/system-headers.mozbuild +++ config/system-headers.mozbuild @@ -1325,6 +1325,28 @@ if CONFIG['MOZ_SYSTEM_HARFBUZZ']: 'harfbuzz/hb.h', ] +if CONFIG['MOZ_SYSTEM_OGG']: + system_headers += [ + 'ogg/ogg.h', + 'ogg/os_types.h', + ] + +if CONFIG['MOZ_SYSTEM_THEORA']: + system_headers += [ + 'theora/theoradec.h', + ] + +if CONFIG['MOZ_SYSTEM_VORBIS']: + system_headers += [ + 'vorbis/codec.h', + 'vorbis/vorbisenc.h', + ] + +if CONFIG['MOZ_SYSTEM_TREMOR']: + system_headers += [ + 'tremor/ivorbiscodec.h', + ] + if CONFIG['MOZ_SYSTEM_LIBVPX']: system_headers += [ 'vpx_mem/vpx_mem.h', diff --git dom/media/AudioStream.cpp dom/media/AudioStream.cpp index 93ecda4319af..ff674c64ea07 100644 --- dom/media/AudioStream.cpp +++ dom/media/AudioStream.cpp @@ -121,7 +121,9 @@ AudioStream::AudioStream(DataSource& aSource) : mMonitor("AudioStream") , mChannels(0) , mOutChannels(0) +#ifndef MOZ_SYSTEM_SOUNDTOUCH , mTimeStretcher(nullptr) +#endif , mDumpFile(nullptr) , mState(INITIALIZED) , mDataSource(aSource) @@ -142,9 +144,11 @@ AudioStream::~AudioStream() if (mDumpFile) { fclose(mDumpFile); } +#ifndef MOZ_SYSTEM_SOUNDTOUCH if (mTimeStretcher) { soundtouch::destroySoundTouchObj(mTimeStretcher); } +#endif #if defined(XP_WIN) if (XRE_IsContentProcess()) { audio::AudioNotificationReceiver::Unregister(this); @@ -168,7 +172,11 @@ nsresult AudioStream::EnsureTimeStretcherInitializedUnlocked() { mMonitor.AssertCurrentThreadOwns(); if (!mTimeStretcher) { +#ifdef MOZ_SYSTEM_SOUNDTOUCH + mTimeStretcher = new soundtouch::SoundTouch(); +#else mTimeStretcher = soundtouch::createSoundTouchObj(); +#endif mTimeStretcher->setSampleRate(mAudioClock.GetInputRate()); mTimeStretcher->setChannels(mOutChannels); mTimeStretcher->setPitch(1.0); diff --git dom/media/AudioStream.h dom/media/AudioStream.h index 7dc1f60f95cc..67d402a4117f 100644 --- dom/media/AudioStream.h +++ dom/media/AudioStream.h -@@ -15,7 +15,11 @@ - #include "mozilla/TimeStamp.h" - #include "mozilla/UniquePtr.h" - #include "CubebUtils.h" +@@ -16,7 +16,11 @@ + #include "nsAutoPtr.h" + #include "nsCOMPtr.h" + #include "nsThreadUtils.h" +#ifdef MOZ_SYSTEM_SOUNDTOUCH +#include "soundtouch/SoundTouch.h" +#else #include "soundtouch/SoundTouchFactory.h" +#endif #if defined(XP_WIN) #include "mozilla/audio/AudioNotificationReceiver.h" @@ -297,7 +301,11 @@ private: uint32_t mChannels; uint32_t mOutChannels; AudioClock mAudioClock; +#ifdef MOZ_SYSTEM_SOUNDTOUCH + nsAutoPtr mTimeStretcher; +#else soundtouch::SoundTouch* mTimeStretcher; +#endif // Output file for dumping audio FILE* mDumpFile; diff --git dom/media/moz.build dom/media/moz.build index 86b051c58d33..fb6186dce78b 100644 --- dom/media/moz.build +++ dom/media/moz.build @@ -326,6 +326,21 @@ if CONFIG['MOZ_WEBRTC']: DEFINES['MOZILLA_INTERNAL_API'] = True +if CONFIG['MOZ_SYSTEM_OGG']: + CXXFLAGS += CONFIG['MOZ_OGG_CFLAGS'] + +if CONFIG['MOZ_SYSTEM_THEORA']: + CXXFLAGS += CONFIG['MOZ_THEORA_CFLAGS'] + +if CONFIG['MOZ_SYSTEM_VORBIS']: + CXXFLAGS += CONFIG['MOZ_VORBIS_CFLAGS'] + +if CONFIG['MOZ_SYSTEM_TREMOR']: + CXXFLAGS += CONFIG['MOZ_TREMOR_CFLAGS'] + +if CONFIG['MOZ_SYSTEM_SOUNDTOUCH']: + CXXFLAGS += CONFIG['MOZ_SOUNDTOUCH_CFLAGS'] + if CONFIG['MOZ_ANDROID_HLS_SUPPORT']: DEFINES['MOZ_ANDROID_HLS_SUPPORT'] = True diff --git dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp index 078f6ea5ef60..c600db067539 100644 --- dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp +++ dom/media/platforms/ffmpeg/ffvpx/FFVPXRuntimeLinker.cpp @@ -15,9 +15,13 @@ #include #endif +#ifdef MOZ_SYSTEM_SOUNDTOUCH +#include "nsXPCOMPrivate.h" // for XUL_DLL +#else // We use a known symbol located in lgpllibs to determine its location. // soundtouch happens to be always included in lgpllibs #include "soundtouch/SoundTouch.h" +#endif namespace mozilla { @@ -64,6 +68,12 @@ FFVPXRuntimeLinker::Init() sLinkStatus = LinkStatus_FAILED; +#ifdef MOZ_SYSTEM_SOUNDTOUCH + // We retrieve the path of the XUL library as this is where mozavcodec and + // mozavutil libs are located. + char* path = + PR_GetLibraryFilePathname(XUL_DLL, (PRFuncPtr)&FFVPXRuntimeLinker::Init); +#else // We retrieve the path of the lgpllibs library as this is where mozavcodec // and mozavutil libs are located. PathString lgpllibsname = GetLibraryName(nullptr, "lgpllibs"); @@ -73,6 +83,7 @@ FFVPXRuntimeLinker::Init() PathString path = GetLibraryFilePathname(lgpllibsname.get(), (PRFuncPtr)&soundtouch::SoundTouch::getVersionId); +#endif if (path.IsEmpty()) { return false; } diff --git media/libogg/README_MOZILLA media/libogg/README_MOZILLA index 6213fdc7770c..ada799916a17 100644 --- media/libogg/README_MOZILLA +++ media/libogg/README_MOZILLA @@ -6,3 +6,6 @@ package downloaded from xiph.org and copied using the update.sh script. The int-types.patch address a bug that config_types.h generated from Linux platform can't be used on OpenSolaris directly see Mozilla bug 449754 + +The in-tree copy may be omitted during build by --with-system-ogg. +Keep version in old-configure.in in sync on updates. diff --git media/libsoundtouch/README_MOZILLA media/libsoundtouch/README_MOZILLA index dc0dbe8f6886..ea73b55e3e39 100644 --- media/libsoundtouch/README_MOZILLA +++ media/libsoundtouch/README_MOZILLA @@ -6,3 +6,5 @@ The whole library is not used, only the relevant files are imported in the tree, using the script `update.sh`. Some changes have been made to the files, using the patch `moz-libsoundtouch.patch`. We also use a custom soundtouch_config.h. +The in-tree copy may be omitted during build by --with-system-soundtouch. +Keep version in old-configure.in in sync on updates. diff --git media/libsoundtouch/src/soundtouch_perms.h media/libsoundtouch/src/soundtouch_perms.h index 0af2fe618311..10973564e7a4 100644 --- media/libsoundtouch/src/soundtouch_perms.h +++ media/libsoundtouch/src/soundtouch_perms.h @@ -12,7 +12,9 @@ #pragma GCC visibility push(default) #include "SoundTouch.h" +#ifndef MOZ_SYSTEM_SOUNDTOUCH #include "SoundTouchFactory.h" +#endif #pragma GCC visibility pop #endif // MOZILLA_SOUNDTOUCH_PERMS_H diff --git media/libtheora/README_MOZILLA media/libtheora/README_MOZILLA index d48dbfa6f63d..6f30f250220e 100644 --- media/libtheora/README_MOZILLA +++ media/libtheora/README_MOZILLA @@ -3,3 +3,6 @@ using the update.sh script. The changes made were those applied by update.sh, the addition/update of Makefile.in files for the Mozilla build system. The subversion revision used was r17578. + +The in-tree copy may be omitted during build by --with-system-theora. +Keep version in old-configure.in in sync on updates. diff --git media/libtheora/moz.build media/libtheora/moz.build index c7f85eebff95..aae7b814adb5 100644 --- media/libtheora/moz.build +++ media/libtheora/moz.build @@ -21,6 +21,9 @@ FINAL_LIBRARY = 'gkmedias' # The encoder is currently not included. DEFINES['THEORA_DISABLE_ENCODE'] = True +if CONFIG['MOZ_SYSTEM_OGG']: + CFLAGS += CONFIG['MOZ_OGG_CFLAGS'] + # Suppress warnings in third-party code. if CONFIG['CC_TYPE'] in ('clang', 'clang-cl', 'gcc'): CFLAGS += ['-Wno-type-limits'] diff --git media/libtremor/README_MOZILLA media/libtremor/README_MOZILLA index ee67b53a05c5..81c971773d55 100644 --- media/libtremor/README_MOZILLA +++ media/libtremor/README_MOZILLA @@ -5,3 +5,6 @@ Makefile.in files for the Mozilla build system. The upstream release used was http://svn.xiph.org/trunk/Tremor/ The subversion revision used was r17547. + +The in-tree copy may be omitted during build by --with-system-tremor. +Keep version in old-configure.in in sync on updates. diff --git media/libtremor/moz.build media/libtremor/moz.build index 83afc8e37c64..71ef159da3d7 100644 --- media/libtremor/moz.build +++ media/libtremor/moz.build @@ -9,3 +9,5 @@ with Files('*'): DIRS += ['include/tremor', 'lib'] +if CONFIG['MOZ_SYSTEM_OGG']: + CFLAGS += CONFIG['MOZ_OGG_CFLAGS'] diff --git media/libvorbis/README_MOZILLA media/libvorbis/README_MOZILLA index 1211ac074b33..eb31084aed25 100644 --- media/libvorbis/README_MOZILLA +++ media/libvorbis/README_MOZILLA @@ -8,3 +8,6 @@ from https://git.xiph.org/vorbis.git Some files are renamed during the copy to prevent clashes with object file names with other Mozilla libraries. + +The in-tree copy may be omitted during build by --with-system-vorbis. +Keep version in old-configure.in in sync on updates. diff --git media/libvorbis/moz.build media/libvorbis/moz.build index adf393782cc9..923b76231107 100644 --- media/libvorbis/moz.build +++ media/libvorbis/moz.build @@ -56,3 +56,6 @@ FINAL_LIBRARY = 'gkmedias' # Suppress warnings in third-party code. if CONFIG['CC_TYPE'] in ('clang', 'gcc'): CFLAGS += ['-Wno-uninitialized'] + +if CONFIG['MOZ_SYSTEM_OGG']: + CFLAGS += CONFIG['MOZ_OGG_CFLAGS'] diff --git old-configure.in old-configure.in index b614eef85c89..85f9099dba4d 100644 --- old-configure.in +++ old-configure.in @@ -2627,6 +2627,111 @@ if test -n "$MOZ_APPLEMEDIA"; then fi fi # COMPILE_ENVIRONMENT +dnl ======================================================== +dnl Check for libogg +dnl ======================================================== + +MOZ_ARG_WITH_BOOL(system-ogg, +[ --with-system-ogg Use system libogg (located with pkgconfig)], +MOZ_SYSTEM_OGG=1, +MOZ_SYSTEM_OGG=) + +if test -n "$MOZ_SYSTEM_OGG"; then + PKG_CHECK_MODULES(MOZ_OGG, ogg >= 1.3.3) + + _SAVE_LIBS=$LIBS + LIBS="$LIBS $MOZ_OGG_LIBS" + AC_CHECK_FUNC(ogg_set_mem_functions, [], + [AC_DEFINE(MOZ_OGG_NO_MEM_REPORTING)]) + LIBS=$_SAVE_LIBS +fi + +AC_SUBST(MOZ_SYSTEM_OGG) + +dnl ======================================================== +dnl Check for libvorbis +dnl ======================================================== + +MOZ_ARG_WITH_BOOL(system-vorbis, +[ --with-system-vorbis Use system libvorbis (located with pkgconfig)], +MOZ_SYSTEM_VORBIS=1, +MOZ_SYSTEM_VORBIS=) + +if test -n "$MOZ_SYSTEM_VORBIS"; then + PKG_CHECK_MODULES(MOZ_VORBIS, vorbis vorbisenc >= 1.3.6) +fi + +AC_SUBST(MOZ_SYSTEM_VORBIS) + +dnl ======================================================== +dnl Check for integer-only libvorbis aka tremor +dnl ======================================================== + +MOZ_ARG_WITH_BOOL(system-tremor, +[ --with-system-tremor Use system libtremor (located with pkgconfig)], +MOZ_SYSTEM_TREMOR=1, +MOZ_SYSTEM_TREMOR=) + +if test -n "$MOZ_SYSTEM_TREMOR"; then + PKG_CHECK_MODULES(MOZ_TREMOR, vorbisidec >= 1.2.1) +fi + +AC_SUBST(MOZ_SYSTEM_TREMOR) + +dnl ======================================================== +dnl Check for libtheora +dnl ======================================================== + +MOZ_ARG_WITH_BOOL(system-theora, +[ --with-system-theora Use system libtheora (located with pkgconfig)], +MOZ_SYSTEM_THEORA=1, +MOZ_SYSTEM_THEORA=) + +if test -n "$MOZ_SYSTEM_THEORA"; then + PKG_CHECK_MODULES(MOZ_THEORA, theora >= 1.2) +fi + +AC_SUBST(MOZ_SYSTEM_THEORA) + +dnl ======================================================== +dnl Check for libSoundTouch +dnl ======================================================== + +MOZ_ARG_WITH_BOOL(system-soundtouch, +[ --with-system-soundtouch Use system libSoundTouch (located with pkgconfig)], +MOZ_SYSTEM_SOUNDTOUCH=1, +MOZ_SYSTEM_SOUNDTOUCH=) + +if test -n "$MOZ_SYSTEM_SOUNDTOUCH"; then + PKG_CHECK_MODULES(MOZ_SOUNDTOUCH, soundtouch >= 1.9.0) + + AC_LANG_SAVE + AC_LANG_CPLUSPLUS + _SAVE_CXXFLAGS=$CXXFLAGS + CXXFLAGS="$CXXFLAGS $MOZ_SOUNDTOUCH_CFLAGS" + AC_CACHE_CHECK(for soundtouch sample type, + ac_cv_soundtouch_sample_type, + [AC_TRY_COMPILE([#include + #ifndef SOUNDTOUCH_INTEGER_SAMPLES + #error soundtouch expects float samples + #endif], + [], + [ac_cv_soundtouch_sample_type=short], + [ac_cv_soundtouch_sample_type=float])]) + CXXFLAGS=$_SAVE_CXXFLAGS + AC_LANG_RESTORE + + if test \( -n "$MOZ_SAMPLE_TYPE_S16" -a "$ac_cv_soundtouch_sample_type" != short \) \ + -o \( -n "$MOZ_SAMPLE_TYPE_FLOAT32" -a "$ac_cv_soundtouch_sample_type" != float \) ; then + AC_MSG_ERROR([SoundTouch library is built with incompatible sample type. Either rebuild the library with/without --enable-integer-samples, chase default Mozilla sample type or remove --with-system-soundtouch.]) + fi +fi + +if test -n "$MOZ_SYSTEM_SOUNDTOUCH"; then + AC_DEFINE(MOZ_SYSTEM_SOUNDTOUCH) +fi +AC_SUBST(MOZ_SYSTEM_SOUNDTOUCH) + dnl system libvpx Support dnl ======================================================== MOZ_ARG_WITH_BOOL(system-libvpx, diff --git toolkit/library/moz.build toolkit/library/moz.build index 079a575adec3..514d901195de 100644 --- toolkit/library/moz.build +++ toolkit/library/moz.build @@ -240,6 +240,21 @@ if CONFIG['MOZ_SYSTEM_HARFBUZZ']: if CONFIG['MOZ_SYSTEM_HUNSPELL']: OS_LIBS += CONFIG['MOZ_HUNSPELL_LIBS'] +if CONFIG['MOZ_SYSTEM_OGG']: + OS_LIBS += CONFIG['MOZ_OGG_LIBS'] + +if CONFIG['MOZ_SYSTEM_THEORA']: + OS_LIBS += CONFIG['MOZ_THEORA_LIBS'] + +if CONFIG['MOZ_SYSTEM_VORBIS']: + OS_LIBS += CONFIG['MOZ_VORBIS_LIBS'] + +if CONFIG['MOZ_SYSTEM_TREMOR']: + OS_LIBS += CONFIG['MOZ_TREMOR_LIBS'] + +if CONFIG['MOZ_SYSTEM_SOUNDTOUCH']: + OS_LIBS += CONFIG['MOZ_SOUNDTOUCH_LIBS'] + if CONFIG['MOZ_SYSTEM_LIBEVENT']: OS_LIBS += CONFIG['MOZ_LIBEVENT_LIBS'] diff --git xpcom/build/XPCOMInit.cpp xpcom/build/XPCOMInit.cpp index c083ab4bc4f3..62b917043a2c 100644 --- xpcom/build/XPCOMInit.cpp +++ xpcom/build/XPCOMInit.cpp @@ -132,7 +132,9 @@ extern nsresult nsStringInputStreamConstructor(nsISupports*, REFNSIID, void**); #include "mozilla/ipc/GeckoChildProcessHost.h" +#ifndef MOZ_OGG_NO_MEM_REPORTING #include "ogg/ogg.h" +#endif #if defined(MOZ_VPX) && !defined(MOZ_VPX_NO_MEM_REPORTING) #if defined(HAVE_STDINT_H) // mozilla-config.h defines HAVE_STDINT_H, and then it's defined *again* in @@ -639,11 +641,13 @@ NS_InitXPCOM2(nsIServiceManager** aResult, // this oddness. mozilla::SetICUMemoryFunctions(); +#ifndef MOZ_OGG_NO_MEM_REPORTING // Do the same for libogg. ogg_set_mem_functions(OggReporter::CountingMalloc, OggReporter::CountingCalloc, OggReporter::CountingRealloc, OggReporter::CountingFree); +#endif #if defined(MOZ_VPX) && !defined(MOZ_VPX_NO_MEM_REPORTING) // And for VPX. Index: head/www/firefox-i18n/Makefile =================================================================== --- head/www/firefox-i18n/Makefile (revision 472726) +++ head/www/firefox-i18n/Makefile (revision 472727) @@ -1,89 +1,89 @@ # Created by: Andrew Pantyukhin # $FreeBSD$ PORTNAME= firefox-i18n -PORTVERSION= 60.0.2 +PORTVERSION= 61.0 CATEGORIES= www MASTER_SITES= MOZILLA/${PORTNAME:S|-i18n||}/releases/${DISTVERSION}/linux-i686/xpi \ MOZILLA/${PORTNAME:S|-i18n||}/candidates/${DISTVERSION}-candidates/build1/linux-i686/xpi PKGNAMEPREFIX= DISTFILES= ${FIREFOX_I18N_:S/$/.xpi/} DIST_SUBDIR= xpi/${DISTNAME} MAINTAINER= gecko@FreeBSD.org COMMENT= Localized interface for Firefox EXTRACT_DEPENDS= zip:archivers/zip -USES= zip:infozip gecko:firefox,60,build +USES= zip:infozip gecko:firefox,61,build USE_XPI= firefox NO_ARCH= yes USE_SUBMAKE= yes WDIR= langpack-*@firefox.mozilla.org XPI_DISTNAMES= ${FIREFOX_I18N_} PLIST?= ${WRKDIR}/plist PLISTF?= ${WRKDIR}/plist_files .include "${.CURDIR}/Makefile.option" .include .ifndef WITHOUT_SWITCHER RUN_DEPENDS+= xpi-quick-locale-switcher>=0:www/xpi-quick-locale-switcher .endif .include "${.CURDIR}/Makefile.lang" FIREFOX_I18N_?= ${FIREFOX_I18N} .for dist in ${XPI_DISTNAMES} XPI_ID_${dist}= langpack-${dist}@firefox.mozilla.org .endfor pre-everything:: @${ECHO_CMD} @${ECHO_CMD} "Please define FIREFOX_I18N_ALL to install all languages." @${ECHO_CMD} do-extract: @${MKDIR} ${WRKSRC} @for lang in ${FIREFOX_I18N_}; do \ if ! (${UNZIP_CMD} -qo ${_DISTDIR}/$$lang.xpi -d ${WRKSRC}/langpack-$$lang@firefox.mozilla.org);\ then \ exit 1; \ fi; \ cd ${WRKSRC}/langpack-$$lang@firefox.mozilla.org/; \ ${FIND} -H -s * ! -type d | \ ${SED} -e "s|^|lib/xpi/langpack-$$lang@firefox.mozilla.org/|" >> ${PLISTF}; \ done ${CAT} ${PLISTF} | ${SORT} >> ${PLIST} do-install: @${MKDIR} ${STAGEDIR}${XPI_LIBDIR} cd ${WRKSRC}; ${PAX} -rw . ${STAGEDIR}${XPI_LIBDIR} ${CHMOD} -R a+rX,go-w ${STAGEDIR}${XPI_LIBDIR}/${WDIR}/ @for e in ${STAGEDIR}${XPI_LIBDIR}/${WDIR}; do \ for _dir in ${XPI_LINKFARMS} ; do \ ${LN} -sf $${_e##*/} ${STAGEDIR}$$_dir/ ${_Q} ; \ done \ done @${ECHO_CMD} '@exec ${INSTALL} -d ${XPI_SLDIRS_ALL:S,^${PREFIX},%D,}' ${_A} @${ECHO_CMD} '@exec for _dir in ${XPI_LINKFARMS} ; { \ ${LN} -sf ${XPI_LIBDIR}/${WDIR} $$_dir/ ${_Q}; }' ${_A} @${ECHO_CMD} '@unexec for _dir in ${XPI_LINKFARMS} ; { ${RM} $$_dir/${WDIR} ; }' ${_A} post-patch: ${FIND} ${WRKSRC} -name install.rdf -print0 | \ ${XARGS} -0L1 ${REINPLACE_CMD} -i '' -e '/ Index: head/www/firefox-i18n/distinfo =================================================================== --- head/www/firefox-i18n/distinfo (revision 472726) +++ head/www/firefox-i18n/distinfo (revision 472727) @@ -1,197 +1,197 @@ -TIMESTAMP = 1528228087 -SHA256 (xpi/firefox-i18n-60.0.2/ach.xpi) = 3264cdac77964c001d27c1da36d280702c6e28bb9caedf06a7a57d2d034f5097 -SIZE (xpi/firefox-i18n-60.0.2/ach.xpi) = 496058 -SHA256 (xpi/firefox-i18n-60.0.2/af.xpi) = 46c49276255243becdddff6102bea42c4860cc3c008d4b01f8d95b91812cdfca -SIZE (xpi/firefox-i18n-60.0.2/af.xpi) = 486534 -SHA256 (xpi/firefox-i18n-60.0.2/an.xpi) = 037f8ec9d96f019059d8131eff4ab06fff8ecc657f98ea231b1e5e7bae730880 -SIZE (xpi/firefox-i18n-60.0.2/an.xpi) = 519729 -SHA256 (xpi/firefox-i18n-60.0.2/ar.xpi) = cda7d8d114da54e4ecd6a5781795ab590c3510c59ab74d1c8343a2834489fa73 -SIZE (xpi/firefox-i18n-60.0.2/ar.xpi) = 546953 -SHA256 (xpi/firefox-i18n-60.0.2/as.xpi) = b978d40a6ebe0ec9ae07fe9d0b2a24c181422c1c09939255f7984a4b153b9a26 -SIZE (xpi/firefox-i18n-60.0.2/as.xpi) = 529953 -SHA256 (xpi/firefox-i18n-60.0.2/ast.xpi) = 5be044c2ea120e039e73a33c334314ce488bc12f0d0d5605a86320a023ed3ea3 -SIZE (xpi/firefox-i18n-60.0.2/ast.xpi) = 507870 -SHA256 (xpi/firefox-i18n-60.0.2/az.xpi) = c692cc2f12e4df1b5e45a5070766e8c5cf468aeb3315180228defdc5704e61d1 -SIZE (xpi/firefox-i18n-60.0.2/az.xpi) = 523974 -SHA256 (xpi/firefox-i18n-60.0.2/be.xpi) = cc291a855dcb454f114b40c7673db18dd7c0b2962a3c11ff9c0a986e3231eaa6 -SIZE (xpi/firefox-i18n-60.0.2/be.xpi) = 579945 -SHA256 (xpi/firefox-i18n-60.0.2/bg.xpi) = 62178b8aa781941a3de9d11e597092d479f682b5c6738de010110a3b9a9a42d8 -SIZE (xpi/firefox-i18n-60.0.2/bg.xpi) = 573080 -SHA256 (xpi/firefox-i18n-60.0.2/bn-BD.xpi) = cbd8950fdab635e864c10a5358d0495210effd6160a62b9599b093338dc67a16 -SIZE (xpi/firefox-i18n-60.0.2/bn-BD.xpi) = 589638 -SHA256 (xpi/firefox-i18n-60.0.2/bn-IN.xpi) = 026b702fdec8e922e71e6a9198d42de7a615d47d9ce47d8a58fdc8c0ee4ffe5a -SIZE (xpi/firefox-i18n-60.0.2/bn-IN.xpi) = 570083 -SHA256 (xpi/firefox-i18n-60.0.2/br.xpi) = acd17442b0918ddadaa324223b70cc240e3e5f9377ee1bf966cecb6f1bd124cf -SIZE (xpi/firefox-i18n-60.0.2/br.xpi) = 511252 -SHA256 (xpi/firefox-i18n-60.0.2/bs.xpi) = 6fc982720e171f8c4e1b06eed294735a6514e3e54bb9ada39cca697315afb48f -SIZE (xpi/firefox-i18n-60.0.2/bs.xpi) = 513936 -SHA256 (xpi/firefox-i18n-60.0.2/ca.xpi) = 41f425b998c4f37c2e506d24e06ab692597d5d5f191c1042cef42b53b164377e -SIZE (xpi/firefox-i18n-60.0.2/ca.xpi) = 524484 -SHA256 (xpi/firefox-i18n-60.0.2/cak.xpi) = e511a882edb4fe78c5759c716cc5313875dde60dcb144cb19f1ff2d3dd97478d -SIZE (xpi/firefox-i18n-60.0.2/cak.xpi) = 535516 -SHA256 (xpi/firefox-i18n-60.0.2/cs.xpi) = 5bd65f390119dcd6f09be4c71a158187e3d0ce3aa775b17459c4b57b6931877b -SIZE (xpi/firefox-i18n-60.0.2/cs.xpi) = 531015 -SHA256 (xpi/firefox-i18n-60.0.2/cy.xpi) = e2c989a044c9a4af5ec60676d1e2f652d3bea657d0b51f91fe763458781d6279 -SIZE (xpi/firefox-i18n-60.0.2/cy.xpi) = 515864 -SHA256 (xpi/firefox-i18n-60.0.2/da.xpi) = fe33cf6e6b118cbd56af791fce6b57640af600f3ed483ff71b3dace8c3ec378a -SIZE (xpi/firefox-i18n-60.0.2/da.xpi) = 509975 -SHA256 (xpi/firefox-i18n-60.0.2/de.xpi) = eda7bc1cd123f24b57ce6d0fcfca3e6c0954c71a32ef610ae62dfe8ae8477140 -SIZE (xpi/firefox-i18n-60.0.2/de.xpi) = 525689 -SHA256 (xpi/firefox-i18n-60.0.2/dsb.xpi) = 73daa18ad8e7a42c781242a506587fed18158f0bf1f9f2e7e9c2c8fa4916b4a3 -SIZE (xpi/firefox-i18n-60.0.2/dsb.xpi) = 538745 -SHA256 (xpi/firefox-i18n-60.0.2/el.xpi) = c447405287531c915afde8ca535b1b812b29a4a15b8aa1bb562b26276801a371 -SIZE (xpi/firefox-i18n-60.0.2/el.xpi) = 592917 -SHA256 (xpi/firefox-i18n-60.0.2/en-GB.xpi) = fe02706b63725e0ef67d23cecae9e0a592c1b0c8bb0050773954f142793ef0d4 -SIZE (xpi/firefox-i18n-60.0.2/en-GB.xpi) = 490903 -SHA256 (xpi/firefox-i18n-60.0.2/en-US.xpi) = 3a012162ccef7068fd5b16117ede3eb8c151e99635e83c031a37f1989565f16b -SIZE (xpi/firefox-i18n-60.0.2/en-US.xpi) = 472384 -SHA256 (xpi/firefox-i18n-60.0.2/en-ZA.xpi) = 0df9291489150fa400c0927b770f5a94e7d3ad641ea59ac720cbf006f3c58e50 -SIZE (xpi/firefox-i18n-60.0.2/en-ZA.xpi) = 473896 -SHA256 (xpi/firefox-i18n-60.0.2/eo.xpi) = 05cc1895271e735fa0d4e73c0cc792de6d7bea63034777fd7facd3e45d0630da -SIZE (xpi/firefox-i18n-60.0.2/eo.xpi) = 510494 -SHA256 (xpi/firefox-i18n-60.0.2/es-AR.xpi) = 005ee9da32ea33d4f420ecbcd8add9cf2ab5bf332b0a7699ecab4ee67331a564 -SIZE (xpi/firefox-i18n-60.0.2/es-AR.xpi) = 523819 -SHA256 (xpi/firefox-i18n-60.0.2/es-CL.xpi) = f76adcbda565019c183d8a4ecb38a911a7fca78b7db158a5a1fa5402627e0566 -SIZE (xpi/firefox-i18n-60.0.2/es-CL.xpi) = 526509 -SHA256 (xpi/firefox-i18n-60.0.2/es-ES.xpi) = 76b7ab024878427cf6ea87b9a74632c667b4cdcf03bdc899f95bb802dfc304d6 -SIZE (xpi/firefox-i18n-60.0.2/es-ES.xpi) = 459139 -SHA256 (xpi/firefox-i18n-60.0.2/es-MX.xpi) = 2829924eee326add8b7f9f9dffd6a42f54e26bb573307d7655f74dff3fa4df43 -SIZE (xpi/firefox-i18n-60.0.2/es-MX.xpi) = 527838 -SHA256 (xpi/firefox-i18n-60.0.2/et.xpi) = d9f0ff92b02ae4b6674432c191950fce9d5b85307b3dd4a7b818b6c73a701271 -SIZE (xpi/firefox-i18n-60.0.2/et.xpi) = 504174 -SHA256 (xpi/firefox-i18n-60.0.2/eu.xpi) = a80e87f4151defb992312fcd143890c6b86cf5067222a2742257f12eb81fb067 -SIZE (xpi/firefox-i18n-60.0.2/eu.xpi) = 510932 -SHA256 (xpi/firefox-i18n-60.0.2/fa.xpi) = 0bf0337cbd82f45f141eeae5433b4b77da449e3077fc0b32c9a3dc511da47f93 -SIZE (xpi/firefox-i18n-60.0.2/fa.xpi) = 565489 -SHA256 (xpi/firefox-i18n-60.0.2/ff.xpi) = bcf099ecd8ad1544e7d084417d6c876f6629b2689a194d7b38a0988a528e5d15 -SIZE (xpi/firefox-i18n-60.0.2/ff.xpi) = 511432 -SHA256 (xpi/firefox-i18n-60.0.2/fi.xpi) = 657ad94dfd99269ada99636ebf7a7ee9bb7364bdcabaede2f6305bd793793f92 -SIZE (xpi/firefox-i18n-60.0.2/fi.xpi) = 505404 -SHA256 (xpi/firefox-i18n-60.0.2/fr.xpi) = 6c13eaf3cbd4ef845be167f40509e6bb1b6732a3a8f0b7dac8a4b27bb1fb5244 -SIZE (xpi/firefox-i18n-60.0.2/fr.xpi) = 535238 -SHA256 (xpi/firefox-i18n-60.0.2/fy-NL.xpi) = 2def398c510f0417922c6155a47b532a2604794aff25dfaa1a64927e5112e69c -SIZE (xpi/firefox-i18n-60.0.2/fy-NL.xpi) = 523736 -SHA256 (xpi/firefox-i18n-60.0.2/ga-IE.xpi) = a07e724a94de27dcd7efad3b518519eb2dc8fce2e6c6ad82c49d7340f1aae654 -SIZE (xpi/firefox-i18n-60.0.2/ga-IE.xpi) = 526199 -SHA256 (xpi/firefox-i18n-60.0.2/gd.xpi) = f9f60f239d7091ac4fe4e8797fe66a2cf8f1998229439b64f02d12189fc56ed8 -SIZE (xpi/firefox-i18n-60.0.2/gd.xpi) = 520093 -SHA256 (xpi/firefox-i18n-60.0.2/gl.xpi) = 8dbe92bf6e56417998bb581600f33abdc9ced8015a2f5b5404ad16331377d469 -SIZE (xpi/firefox-i18n-60.0.2/gl.xpi) = 496593 -SHA256 (xpi/firefox-i18n-60.0.2/gn.xpi) = 2f8f02fbff9dcafe6e97f56e3bb4cd30dab599cfeac5aa08176ddf115e93c230 -SIZE (xpi/firefox-i18n-60.0.2/gn.xpi) = 532439 -SHA256 (xpi/firefox-i18n-60.0.2/gu-IN.xpi) = 0d5ccd4b83d3bc510f48466b647956d356da98b2873bf324e0449f795418b83c -SIZE (xpi/firefox-i18n-60.0.2/gu-IN.xpi) = 589483 -SHA256 (xpi/firefox-i18n-60.0.2/he.xpi) = ec0799c68a0f054ec30fdf9f7b698517b12d960235dabcbfd6ea828e38647cee -SIZE (xpi/firefox-i18n-60.0.2/he.xpi) = 535211 -SHA256 (xpi/firefox-i18n-60.0.2/hi-IN.xpi) = 3fd33dabf5a465fba7983553ba89d7189cb156132bd2060932b11f1bc3efbabf -SIZE (xpi/firefox-i18n-60.0.2/hi-IN.xpi) = 590076 -SHA256 (xpi/firefox-i18n-60.0.2/hr.xpi) = be54a7a81d3d2aecd5bda7266034f2c41dd2271ca41c4a59dfc2d95ef8ef6876 -SIZE (xpi/firefox-i18n-60.0.2/hr.xpi) = 515186 -SHA256 (xpi/firefox-i18n-60.0.2/hsb.xpi) = c552a2302b5832c953db367475d9eb292e75ef3f1319e034ade05e6e5ef1f15b -SIZE (xpi/firefox-i18n-60.0.2/hsb.xpi) = 536278 -SHA256 (xpi/firefox-i18n-60.0.2/hu.xpi) = 61bb78d4604bb4f4fee525751486e6e5612c285565bb7e42f6e3b388a9dbbe31 -SIZE (xpi/firefox-i18n-60.0.2/hu.xpi) = 537587 -SHA256 (xpi/firefox-i18n-60.0.2/hy-AM.xpi) = 54deba7f2a00e48cadd99208472e467f1f78a678db91ba3fbb8ffe2d925f5dda -SIZE (xpi/firefox-i18n-60.0.2/hy-AM.xpi) = 568629 -SHA256 (xpi/firefox-i18n-60.0.2/ia.xpi) = c69a5c604f90f239a6c78de43b1f7884a5f553eaa49211f81cf76b1c256638f0 -SIZE (xpi/firefox-i18n-60.0.2/ia.xpi) = 509951 -SHA256 (xpi/firefox-i18n-60.0.2/id.xpi) = f7fb19719341975e08328270ba1f4933d589fb4d2798ef9a00965828aba55227 -SIZE (xpi/firefox-i18n-60.0.2/id.xpi) = 501449 -SHA256 (xpi/firefox-i18n-60.0.2/is.xpi) = 0164a3686761abf0be0a1d8f773762031222e22362152865c4e38ac306b0cdd3 -SIZE (xpi/firefox-i18n-60.0.2/is.xpi) = 512502 -SHA256 (xpi/firefox-i18n-60.0.2/it.xpi) = 02b80d059ae403c04b2e130f521b1d80ea71f505cfe4dbd120ea218a84bedeb0 -SIZE (xpi/firefox-i18n-60.0.2/it.xpi) = 398263 -SHA256 (xpi/firefox-i18n-60.0.2/ja.xpi) = dcb3446a228a3cde462264e0ba39c606aea4dc711919e6c7d5dae252ecdd5a67 -SIZE (xpi/firefox-i18n-60.0.2/ja.xpi) = 571244 -SHA256 (xpi/firefox-i18n-60.0.2/ka.xpi) = fcfe7fc9d2cff0399e369eaf1d8bb5d8adb73773d095858bbefd2758d69f7f27 -SIZE (xpi/firefox-i18n-60.0.2/ka.xpi) = 556072 -SHA256 (xpi/firefox-i18n-60.0.2/kab.xpi) = 049404f2dcb4d32ddcce2b36739b4301b69586906defa775fc6500c8fad678ac -SIZE (xpi/firefox-i18n-60.0.2/kab.xpi) = 519624 -SHA256 (xpi/firefox-i18n-60.0.2/kk.xpi) = ed233b274feec630f92c432ea9adf7624548c628308724aa4046baa51a9d6004 -SIZE (xpi/firefox-i18n-60.0.2/kk.xpi) = 582205 -SHA256 (xpi/firefox-i18n-60.0.2/km.xpi) = a67c04fb6437227a68ab8083491119fc0931dc8b397d605b92f804f9f2f4c710 -SIZE (xpi/firefox-i18n-60.0.2/km.xpi) = 582332 -SHA256 (xpi/firefox-i18n-60.0.2/kn.xpi) = 3427d552400dde0e3d73019cdf6d3ff7e0235d8ea0e9647b90da923c60365924 -SIZE (xpi/firefox-i18n-60.0.2/kn.xpi) = 585098 -SHA256 (xpi/firefox-i18n-60.0.2/ko.xpi) = 22eed6eba57f8cb38bda67e8a90e7bff5668f2e596869bc5a1c60c24453733d3 -SIZE (xpi/firefox-i18n-60.0.2/ko.xpi) = 547056 -SHA256 (xpi/firefox-i18n-60.0.2/lij.xpi) = d1d4e0d940eb4a19ea6ba8f4dd883c4e365d9f1ac7dfe90160764278fec29119 -SIZE (xpi/firefox-i18n-60.0.2/lij.xpi) = 512397 -SHA256 (xpi/firefox-i18n-60.0.2/lt.xpi) = 5d7a35f2890260978d0d7110e73ae2109bb3703d6cd65d36048e66e77062c7fe -SIZE (xpi/firefox-i18n-60.0.2/lt.xpi) = 535632 -SHA256 (xpi/firefox-i18n-60.0.2/lv.xpi) = 462e8d28b118e601f8eaadf7ee495fceac969e1110debe2e0b12f4afdc5b1834 -SIZE (xpi/firefox-i18n-60.0.2/lv.xpi) = 525636 -SHA256 (xpi/firefox-i18n-60.0.2/mai.xpi) = 66e8e472fcdd9532a8401a4a9bb7293fcfab4535e6129e62d0aba974753d0c0c -SIZE (xpi/firefox-i18n-60.0.2/mai.xpi) = 548471 -SHA256 (xpi/firefox-i18n-60.0.2/mk.xpi) = d752302445ac0f14496e4230c1c094d2b78fef42f3272914c3a52b72fb8dc4d3 -SIZE (xpi/firefox-i18n-60.0.2/mk.xpi) = 513025 -SHA256 (xpi/firefox-i18n-60.0.2/ml.xpi) = 0652ddfd12240e1a7a258835b6b72af06856d541c99c6a0d2275dca9157674e5 -SIZE (xpi/firefox-i18n-60.0.2/ml.xpi) = 598198 -SHA256 (xpi/firefox-i18n-60.0.2/mr.xpi) = 2a05310d226bd5be71ede145c898eeb1c2e135aefa0245254201266e0595b969 -SIZE (xpi/firefox-i18n-60.0.2/mr.xpi) = 578337 -SHA256 (xpi/firefox-i18n-60.0.2/ms.xpi) = 877a6de579035a20ff32075e5d4d36079af06c0f307428ecf508c39d2af9aab7 -SIZE (xpi/firefox-i18n-60.0.2/ms.xpi) = 508781 -SHA256 (xpi/firefox-i18n-60.0.2/my.xpi) = 6702089f2b5560f7774f6abd432cbaeb8a3364f4897dde19a158efa647bd7a0d -SIZE (xpi/firefox-i18n-60.0.2/my.xpi) = 569572 -SHA256 (xpi/firefox-i18n-60.0.2/nb-NO.xpi) = 773f9a9843c79f34bfa459829b8c9cd3546b544dccefde0e7c468098de47f383 -SIZE (xpi/firefox-i18n-60.0.2/nb-NO.xpi) = 509104 -SHA256 (xpi/firefox-i18n-60.0.2/ne-NP.xpi) = d3e33d855d4e0d0d89fda9c4cd6b1322348059e2ec378d522904f40e81a3f763 -SIZE (xpi/firefox-i18n-60.0.2/ne-NP.xpi) = 554577 -SHA256 (xpi/firefox-i18n-60.0.2/nl.xpi) = 4f419acfd5824b75e6be4a8a95645ff6bc8798fe3e54c740f8652ca7836ef539 -SIZE (xpi/firefox-i18n-60.0.2/nl.xpi) = 516653 -SHA256 (xpi/firefox-i18n-60.0.2/nn-NO.xpi) = 29aa2f24e4bdd67c58d17caecd8b2c7d81bb0bbdc682d76487614621561f1085 -SIZE (xpi/firefox-i18n-60.0.2/nn-NO.xpi) = 510621 -SHA256 (xpi/firefox-i18n-60.0.2/oc.xpi) = ec8836cd689ebb864cb994dfaee342dbcf9a5f2423f0f399ffbc1c6a118d2a7d -SIZE (xpi/firefox-i18n-60.0.2/oc.xpi) = 526207 -SHA256 (xpi/firefox-i18n-60.0.2/or.xpi) = 9da7b23e12d90aeed04e637772a0b8a3f75d7d6dd8d7cfcfc5d60a99f8d2fd19 -SIZE (xpi/firefox-i18n-60.0.2/or.xpi) = 540089 -SHA256 (xpi/firefox-i18n-60.0.2/pa-IN.xpi) = 77432ae4cf8faa53fb3aaa51749547b63205d301cc9b3cbb17cf8837589554da -SIZE (xpi/firefox-i18n-60.0.2/pa-IN.xpi) = 557086 -SHA256 (xpi/firefox-i18n-60.0.2/pl.xpi) = 6c120bfcc19c4b68b21adffdda876eec2e928509eb0441bc0f6290098e3f333c -SIZE (xpi/firefox-i18n-60.0.2/pl.xpi) = 418192 -SHA256 (xpi/firefox-i18n-60.0.2/pt-BR.xpi) = aa3613bea650524dbb7f5dd7114759df3581479602a64f57f520e267582d4987 -SIZE (xpi/firefox-i18n-60.0.2/pt-BR.xpi) = 513944 -SHA256 (xpi/firefox-i18n-60.0.2/pt-PT.xpi) = 67f4f4e4b1bf773cbe5712042fe99af140d85da9366126aa39c59798bb1dd0cf -SIZE (xpi/firefox-i18n-60.0.2/pt-PT.xpi) = 518540 -SHA256 (xpi/firefox-i18n-60.0.2/rm.xpi) = 569994da2a6f808a8d6c533b76487e19088d60085a043afb273c0cdb0f598685 -SIZE (xpi/firefox-i18n-60.0.2/rm.xpi) = 505263 -SHA256 (xpi/firefox-i18n-60.0.2/ro.xpi) = 5a97e49719592a6d5ef0f829947afb54e2e4449a89a354bcb0bced8e6594fde5 -SIZE (xpi/firefox-i18n-60.0.2/ro.xpi) = 513283 -SHA256 (xpi/firefox-i18n-60.0.2/ru.xpi) = 2ea09073bf39fd18f2257176c71044c8fb9eda6c9e7fedab8c23316f5167d9fb -SIZE (xpi/firefox-i18n-60.0.2/ru.xpi) = 590511 -SHA256 (xpi/firefox-i18n-60.0.2/si.xpi) = 200e788abd768d1ea478a341a2b9ed56b44097b36cde8d9b1818157d30c96cec -SIZE (xpi/firefox-i18n-60.0.2/si.xpi) = 544795 -SHA256 (xpi/firefox-i18n-60.0.2/sk.xpi) = f396351241475b099f7924ee78bc156be5213323789e88f013608d908b0df8f6 -SIZE (xpi/firefox-i18n-60.0.2/sk.xpi) = 540928 -SHA256 (xpi/firefox-i18n-60.0.2/sl.xpi) = b10e2296960aa0a98aaf6fecfd08ce64e223e8f7aec169b1a94d628b2c8348c1 -SIZE (xpi/firefox-i18n-60.0.2/sl.xpi) = 518573 -SHA256 (xpi/firefox-i18n-60.0.2/son.xpi) = a63256b85d637d60086cfff632957c44c45ba53c843634f252e42601d54e7abd -SIZE (xpi/firefox-i18n-60.0.2/son.xpi) = 496920 -SHA256 (xpi/firefox-i18n-60.0.2/sq.xpi) = e8b0fe2f702f3c1858adc19521a4a661275df80664303934c79e14839843f067 -SIZE (xpi/firefox-i18n-60.0.2/sq.xpi) = 520487 -SHA256 (xpi/firefox-i18n-60.0.2/sr.xpi) = 6a4e20050e2b4416ebaa35a32fe5e3ed7ac2191bbf0a5cfa7d5e1e046c1c8dfc -SIZE (xpi/firefox-i18n-60.0.2/sr.xpi) = 547998 -SHA256 (xpi/firefox-i18n-60.0.2/sv-SE.xpi) = 942d86553fe7224497e497464537fecf8de2f5fd9819330821e05b7968a4ebd2 -SIZE (xpi/firefox-i18n-60.0.2/sv-SE.xpi) = 517850 -SHA256 (xpi/firefox-i18n-60.0.2/ta.xpi) = 8c03cea19a64a176f135b6de774740292f93cfdfff262440900b3e8117b789b9 -SIZE (xpi/firefox-i18n-60.0.2/ta.xpi) = 577139 -SHA256 (xpi/firefox-i18n-60.0.2/te.xpi) = ea6dbd7a2f4379f8c980d5bdba84cfd2bcc91b81243e01d2770dd8dc238fca82 -SIZE (xpi/firefox-i18n-60.0.2/te.xpi) = 590179 -SHA256 (xpi/firefox-i18n-60.0.2/th.xpi) = be48a1f68ce337997b6b5cc5f14a44ea53c8a6f9f5fcb9bd4f037e83a5e27f3f -SIZE (xpi/firefox-i18n-60.0.2/th.xpi) = 562042 -SHA256 (xpi/firefox-i18n-60.0.2/tr.xpi) = 114102a3af7a638d2a988c97c47f95631fe9c795770db1238e8329226e6c5087 -SIZE (xpi/firefox-i18n-60.0.2/tr.xpi) = 525034 -SHA256 (xpi/firefox-i18n-60.0.2/uk.xpi) = c72bd318699dcef64d285a970850f1b2953c263033bec419a89b86d06a000ce0 -SIZE (xpi/firefox-i18n-60.0.2/uk.xpi) = 580148 -SHA256 (xpi/firefox-i18n-60.0.2/ur.xpi) = b336ad7af245f94e221d96648a0eafd186c9b0aafabdca13d9495a5a2f0f7cea -SIZE (xpi/firefox-i18n-60.0.2/ur.xpi) = 562604 -SHA256 (xpi/firefox-i18n-60.0.2/uz.xpi) = 72c1d09b935e26dd3838c8121527a768cd3518e0010452be062bd7459b84e944 -SIZE (xpi/firefox-i18n-60.0.2/uz.xpi) = 512908 -SHA256 (xpi/firefox-i18n-60.0.2/vi.xpi) = 8598e67bf41d660c443b62b1feccd8fc4904026bd26304278057028028096822 -SIZE (xpi/firefox-i18n-60.0.2/vi.xpi) = 527412 -SHA256 (xpi/firefox-i18n-60.0.2/xh.xpi) = 4fb78a9f7515ab7e3a1d1c7329a53dd91e090312ffbec6c9141454da8e24a000 -SIZE (xpi/firefox-i18n-60.0.2/xh.xpi) = 511086 -SHA256 (xpi/firefox-i18n-60.0.2/zh-CN.xpi) = 8dccc0a7ec90bd42c0beb420d8572fa591afc3deb949fdbb68a610c53949159d -SIZE (xpi/firefox-i18n-60.0.2/zh-CN.xpi) = 542797 -SHA256 (xpi/firefox-i18n-60.0.2/zh-TW.xpi) = f0d1047fcf359865b99a44f873e210cf5adcde8a9f577cb6e5c517f2bcfa84bb -SIZE (xpi/firefox-i18n-60.0.2/zh-TW.xpi) = 541123 +TIMESTAMP = 1529363060 +SHA256 (xpi/firefox-i18n-61.0/ach.xpi) = 4ff0f07119bb1a8f53293b82760d23bd001a957a6c8c21241a79f20ea689ea54 +SIZE (xpi/firefox-i18n-61.0/ach.xpi) = 468315 +SHA256 (xpi/firefox-i18n-61.0/af.xpi) = a575a9b471c911de70eba83dca6a36177b2c8d93a0820d2ce719e91c439b2166 +SIZE (xpi/firefox-i18n-61.0/af.xpi) = 458282 +SHA256 (xpi/firefox-i18n-61.0/an.xpi) = 2f2e969cbcdf8e0ffeb1446ec0d9e4399061c8a9044334f1cc91599de0579693 +SIZE (xpi/firefox-i18n-61.0/an.xpi) = 492776 +SHA256 (xpi/firefox-i18n-61.0/ar.xpi) = d1f69c6e645d4d1e2d05d4cb5613e83f77234c2c1f48a58053ebe9043ee12d3d +SIZE (xpi/firefox-i18n-61.0/ar.xpi) = 514909 +SHA256 (xpi/firefox-i18n-61.0/as.xpi) = ebb0bc6249193cf503947c536fdf66d7777538f3328476947d9eaf5b3a505c97 +SIZE (xpi/firefox-i18n-61.0/as.xpi) = 498325 +SHA256 (xpi/firefox-i18n-61.0/ast.xpi) = 0031dde4c27ae3c097a65757a97e2a418236243bd6520dd4aa13b182eb63d606 +SIZE (xpi/firefox-i18n-61.0/ast.xpi) = 478587 +SHA256 (xpi/firefox-i18n-61.0/az.xpi) = f3665e6cbc8872a917511b1de28792df8d2358e27ea48be61699b02ceb9dac30 +SIZE (xpi/firefox-i18n-61.0/az.xpi) = 494285 +SHA256 (xpi/firefox-i18n-61.0/be.xpi) = 621c76bb5412097875f8b8435a59b81f39bdcc009fdf743296a59da0884b9cb0 +SIZE (xpi/firefox-i18n-61.0/be.xpi) = 550840 +SHA256 (xpi/firefox-i18n-61.0/bg.xpi) = 967921d5d1d6d1538572cab57c732fa2aa136baf9f5ef771f5cb6e7af3b882df +SIZE (xpi/firefox-i18n-61.0/bg.xpi) = 541818 +SHA256 (xpi/firefox-i18n-61.0/bn-BD.xpi) = 299397a1c23b1ff0c00db81beef7c911908dadece988b21c06ca2677ab656f01 +SIZE (xpi/firefox-i18n-61.0/bn-BD.xpi) = 556530 +SHA256 (xpi/firefox-i18n-61.0/bn-IN.xpi) = ac7b4891cfc627fbadebaff5a968a5507ede126641ac954dbaed415887d14ee8 +SIZE (xpi/firefox-i18n-61.0/bn-IN.xpi) = 535413 +SHA256 (xpi/firefox-i18n-61.0/br.xpi) = e7320413b5314c86d50b8303fb6811d655521fc7ead452dba14fd49a583398f9 +SIZE (xpi/firefox-i18n-61.0/br.xpi) = 486575 +SHA256 (xpi/firefox-i18n-61.0/bs.xpi) = 3fc348bfd1f84ae9fddc437e47568a7d45b55ca7a66d30ca2af2632f91d054e5 +SIZE (xpi/firefox-i18n-61.0/bs.xpi) = 489126 +SHA256 (xpi/firefox-i18n-61.0/ca.xpi) = 27de4ea1d34c3f8168d4a7cecb7d231e1b7b87e89bb4038df27cfc7ef5050efd +SIZE (xpi/firefox-i18n-61.0/ca.xpi) = 497516 +SHA256 (xpi/firefox-i18n-61.0/cak.xpi) = 4816243359dfb7f583f93b36c2834c605ca0f2d45b9cbbd6a8dfdccc5e486b20 +SIZE (xpi/firefox-i18n-61.0/cak.xpi) = 509333 +SHA256 (xpi/firefox-i18n-61.0/cs.xpi) = 770ed446a0085c965d9892ac6b0ad8084972e06064fd4d198b1b099648ab29ee +SIZE (xpi/firefox-i18n-61.0/cs.xpi) = 502595 +SHA256 (xpi/firefox-i18n-61.0/cy.xpi) = bb66b94fe6187b4972d4961e62f597da5df64b5da066b6b845df9fa3d5c4ba16 +SIZE (xpi/firefox-i18n-61.0/cy.xpi) = 490414 +SHA256 (xpi/firefox-i18n-61.0/da.xpi) = 35b316521b48446779c1f2745947361bb703c822ee67fe6a74e86223e85bb67e +SIZE (xpi/firefox-i18n-61.0/da.xpi) = 483618 +SHA256 (xpi/firefox-i18n-61.0/de.xpi) = 6aadc1264f5fd8efb3694ff08a5efb75297fc294f0a680203aae74cc070f35b8 +SIZE (xpi/firefox-i18n-61.0/de.xpi) = 502283 +SHA256 (xpi/firefox-i18n-61.0/dsb.xpi) = ccbff842b6764116b47385d2287461963fb5ac7b78f1f2aae4a8e18aed78458d +SIZE (xpi/firefox-i18n-61.0/dsb.xpi) = 512450 +SHA256 (xpi/firefox-i18n-61.0/el.xpi) = edd6f8dde4981106bf737717229a03ba651f34dc425d5840837d40070e843c8b +SIZE (xpi/firefox-i18n-61.0/el.xpi) = 559690 +SHA256 (xpi/firefox-i18n-61.0/en-GB.xpi) = 9afdfac520fc213693673987a6b051822935915ad887c77119328a3e18ae8d20 +SIZE (xpi/firefox-i18n-61.0/en-GB.xpi) = 464877 +SHA256 (xpi/firefox-i18n-61.0/en-US.xpi) = 36de1f6d998c18f3bcf3ea5e7f14aaa355311fa9218a3fb4a6954f8d5dea04d4 +SIZE (xpi/firefox-i18n-61.0/en-US.xpi) = 456416 +SHA256 (xpi/firefox-i18n-61.0/en-ZA.xpi) = 1a8d3d838a06d6ed4a92c7c9a20bdeb3daa51b277b579d7d54f84cb9dc4a50f0 +SIZE (xpi/firefox-i18n-61.0/en-ZA.xpi) = 446645 +SHA256 (xpi/firefox-i18n-61.0/eo.xpi) = 3f709d3f28be0ed4cd8248a7237fcb98080a32a5f9926dc1cac1dd0e8cd2036f +SIZE (xpi/firefox-i18n-61.0/eo.xpi) = 484761 +SHA256 (xpi/firefox-i18n-61.0/es-AR.xpi) = 9e98d6f9c26ff5b9c493f1358311a61ea0b1d7bfc347acb92a0fc1a30a330268 +SIZE (xpi/firefox-i18n-61.0/es-AR.xpi) = 498194 +SHA256 (xpi/firefox-i18n-61.0/es-CL.xpi) = 772f0f0b1c819cdad977a1cc1eeb45a7e20b4cd13e565b3c009101612783647a +SIZE (xpi/firefox-i18n-61.0/es-CL.xpi) = 500856 +SHA256 (xpi/firefox-i18n-61.0/es-ES.xpi) = 478ac6255f1a78eccc84ae147ec20a04dc8b2b81da14531614f1fb5e84a53824 +SIZE (xpi/firefox-i18n-61.0/es-ES.xpi) = 435906 +SHA256 (xpi/firefox-i18n-61.0/es-MX.xpi) = ab8875565143f143c6be267f4f2ac27d93518eaacfb36207ed3a25a43bd43cc5 +SIZE (xpi/firefox-i18n-61.0/es-MX.xpi) = 500944 +SHA256 (xpi/firefox-i18n-61.0/et.xpi) = 7472975fc121194c15c5fa30374eafbced8a284cd05d1ea32f83f6b8483caf63 +SIZE (xpi/firefox-i18n-61.0/et.xpi) = 477542 +SHA256 (xpi/firefox-i18n-61.0/eu.xpi) = 5f69e9bbd37ff36807a5d70f5c8180a1776201f666ef6475fd265aad7c5c53be +SIZE (xpi/firefox-i18n-61.0/eu.xpi) = 488816 +SHA256 (xpi/firefox-i18n-61.0/fa.xpi) = 5fa1d36c12de82753062850b8a34366f237b9083a1ed029e64bbbf3fe9ef530d +SIZE (xpi/firefox-i18n-61.0/fa.xpi) = 537573 +SHA256 (xpi/firefox-i18n-61.0/ff.xpi) = 12dcc07546d56a193bed1c221f738ca5c6bf33a65de2e35bd9deeb950955c205 +SIZE (xpi/firefox-i18n-61.0/ff.xpi) = 483852 +SHA256 (xpi/firefox-i18n-61.0/fi.xpi) = de825d8efd96d3be1143d4c3c38c086303ae99b6ae6016c06743327fb53c6573 +SIZE (xpi/firefox-i18n-61.0/fi.xpi) = 479926 +SHA256 (xpi/firefox-i18n-61.0/fr.xpi) = 5a412bcb07eb0f6cc18ad261cbdb70d591bc9203ba90f9f31b82f27eb821c19e +SIZE (xpi/firefox-i18n-61.0/fr.xpi) = 507142 +SHA256 (xpi/firefox-i18n-61.0/fy-NL.xpi) = f704a7071efbec6f224eb218117caa892e3b9b7cf3e1a95059f8f8f6138b5886 +SIZE (xpi/firefox-i18n-61.0/fy-NL.xpi) = 497982 +SHA256 (xpi/firefox-i18n-61.0/ga-IE.xpi) = a1cd760b5e516c2ce49ea486abd5255134ac807b59778f6e6aa624433b9b87fc +SIZE (xpi/firefox-i18n-61.0/ga-IE.xpi) = 495805 +SHA256 (xpi/firefox-i18n-61.0/gd.xpi) = 71e64d95918a8e709fd985a2c459797529b556209b79453f3e379a1f4a8cd534 +SIZE (xpi/firefox-i18n-61.0/gd.xpi) = 497605 +SHA256 (xpi/firefox-i18n-61.0/gl.xpi) = 3f11ddedb54e3c51b9d49f931770599be7b10e7455001f84b813f156803415b2 +SIZE (xpi/firefox-i18n-61.0/gl.xpi) = 471912 +SHA256 (xpi/firefox-i18n-61.0/gn.xpi) = bfb61578ba13e7866aa952d93e2218a4efc9d8042d6eab90efef3194505bc06b +SIZE (xpi/firefox-i18n-61.0/gn.xpi) = 506407 +SHA256 (xpi/firefox-i18n-61.0/gu-IN.xpi) = 54ab192252fa174aa514c61fe2358be1c8c5be22e1c244d062eddf6ccd6046d3 +SIZE (xpi/firefox-i18n-61.0/gu-IN.xpi) = 557297 +SHA256 (xpi/firefox-i18n-61.0/he.xpi) = a8274869e11dc27b1733e9598ab316f900075aeba606ae027edfdd8047027729 +SIZE (xpi/firefox-i18n-61.0/he.xpi) = 505556 +SHA256 (xpi/firefox-i18n-61.0/hi-IN.xpi) = 4d10b16402ca6e5112ef3aa0392a695d0237450a3c2dd7a2693e6415a39f7cf5 +SIZE (xpi/firefox-i18n-61.0/hi-IN.xpi) = 559344 +SHA256 (xpi/firefox-i18n-61.0/hr.xpi) = f8a9f1bdce13901a7932fbbef0d54004f5f222e4a0c7372b48f1f3454c8aa178 +SIZE (xpi/firefox-i18n-61.0/hr.xpi) = 486045 +SHA256 (xpi/firefox-i18n-61.0/hsb.xpi) = 84082c615cbb27ab990d2e5a9c18b2ac69750ffc1bcc9b715ee91d3188c786ff +SIZE (xpi/firefox-i18n-61.0/hsb.xpi) = 510217 +SHA256 (xpi/firefox-i18n-61.0/hu.xpi) = 47d2be51c52714acc9caedfc378f5d8170bf0eb94a680d60f9d338cd0ff39e6e +SIZE (xpi/firefox-i18n-61.0/hu.xpi) = 511138 +SHA256 (xpi/firefox-i18n-61.0/hy-AM.xpi) = e0f5638f4f61781ffbdb7c2f500993f7236860a152dc0febc155417f063ddbd6 +SIZE (xpi/firefox-i18n-61.0/hy-AM.xpi) = 537823 +SHA256 (xpi/firefox-i18n-61.0/ia.xpi) = 4aa0ed2456fa982b00b6906c03f36a7de8c17f342a9fbe2a3cdaa275f7ddd7f2 +SIZE (xpi/firefox-i18n-61.0/ia.xpi) = 483841 +SHA256 (xpi/firefox-i18n-61.0/id.xpi) = b1b4f6a9d5bd4b048508973c2f2bc89cf6ec6ffc9216736eba2210281eadba8b +SIZE (xpi/firefox-i18n-61.0/id.xpi) = 476106 +SHA256 (xpi/firefox-i18n-61.0/is.xpi) = 6c72add7fa50eed39dfd3a01bba517804cac7365de4d58c40f1565c0480fb576 +SIZE (xpi/firefox-i18n-61.0/is.xpi) = 482669 +SHA256 (xpi/firefox-i18n-61.0/it.xpi) = d90b3b8da4ae296f8989ebc38a7cbdaf8a7a5bfdb996c867b99accb75c317b46 +SIZE (xpi/firefox-i18n-61.0/it.xpi) = 372491 +SHA256 (xpi/firefox-i18n-61.0/ja.xpi) = bce9e2fc73293dd74f230936df6529cbc3cde88cbb8da4f1caa47ff8b2298bbd +SIZE (xpi/firefox-i18n-61.0/ja.xpi) = 538258 +SHA256 (xpi/firefox-i18n-61.0/ka.xpi) = c4a88b5e33ab3e81334b4f75a4a3582f1b68b0ff6a6ce316f7d6aef20ff3f1d5 +SIZE (xpi/firefox-i18n-61.0/ka.xpi) = 525910 +SHA256 (xpi/firefox-i18n-61.0/kab.xpi) = dd94ef81e523077cf0f4eda7906b124d1aa8931cb4397115c36db6f7c6ecb391 +SIZE (xpi/firefox-i18n-61.0/kab.xpi) = 495569 +SHA256 (xpi/firefox-i18n-61.0/kk.xpi) = c0ae9c06aa45f3a21a97ae1474a8916de9caf64a9ea712e0c09bf48ad35c15d8 +SIZE (xpi/firefox-i18n-61.0/kk.xpi) = 553684 +SHA256 (xpi/firefox-i18n-61.0/km.xpi) = 3faa01bc4800bb5bd02a6c6dc1a22169de2e22838fbf6e8e0375dc7bf6614713 +SIZE (xpi/firefox-i18n-61.0/km.xpi) = 547198 +SHA256 (xpi/firefox-i18n-61.0/kn.xpi) = 0ee074666999e6df54324587ca5db10804d678a17b04691fecb6ad15ad19e37b +SIZE (xpi/firefox-i18n-61.0/kn.xpi) = 552594 +SHA256 (xpi/firefox-i18n-61.0/ko.xpi) = 83909986b09d95772edba3ec8dfe00ffc5e89d4efbd65e2e32b39fa530c71643 +SIZE (xpi/firefox-i18n-61.0/ko.xpi) = 519812 +SHA256 (xpi/firefox-i18n-61.0/lij.xpi) = e6b77c22576bfa8004deb1d5a0d061fe63329cededc3cfb90a52a154120841da +SIZE (xpi/firefox-i18n-61.0/lij.xpi) = 486945 +SHA256 (xpi/firefox-i18n-61.0/lt.xpi) = 7e09fe7d6558758bf0ac7f3e267f8c2b4a2cf298bde35d0f78a8de3a1db3fa65 +SIZE (xpi/firefox-i18n-61.0/lt.xpi) = 509390 +SHA256 (xpi/firefox-i18n-61.0/lv.xpi) = 2372a664eeccf2f6ec098ec77e66324bf78960987cf54a1b9c5da35806c059c1 +SIZE (xpi/firefox-i18n-61.0/lv.xpi) = 497552 +SHA256 (xpi/firefox-i18n-61.0/mai.xpi) = 248610a2c8b97b47d506ae28a182fcd39433fcf9bd8877c02b6d48b06fd05411 +SIZE (xpi/firefox-i18n-61.0/mai.xpi) = 518899 +SHA256 (xpi/firefox-i18n-61.0/mk.xpi) = d6536015b53676148a1ae7c8e486755e434c91e0943ce684619f885418847e2b +SIZE (xpi/firefox-i18n-61.0/mk.xpi) = 480669 +SHA256 (xpi/firefox-i18n-61.0/ml.xpi) = f8fe34b1a8f0644764d1be18961cf67b9e6903929453db3555af96e6b678aef1 +SIZE (xpi/firefox-i18n-61.0/ml.xpi) = 562449 +SHA256 (xpi/firefox-i18n-61.0/mr.xpi) = 9c85c36fab015a00ac8a37967e244732331a93337c819667b55931b148d77e1d +SIZE (xpi/firefox-i18n-61.0/mr.xpi) = 547457 +SHA256 (xpi/firefox-i18n-61.0/ms.xpi) = a30b9825ca7989dec5c79db7b37b71edf3a4a5dcb59841fa67ed23c54cc81d2c +SIZE (xpi/firefox-i18n-61.0/ms.xpi) = 483495 +SHA256 (xpi/firefox-i18n-61.0/my.xpi) = 0c40918cc7753227be7368975be8c3adc22737111c3b70182546ab2d63d85e7f +SIZE (xpi/firefox-i18n-61.0/my.xpi) = 536701 +SHA256 (xpi/firefox-i18n-61.0/nb-NO.xpi) = f8cb8b6085304b4fb71d00bb35b0def3992ef118593d45116167cdd198f9a8b0 +SIZE (xpi/firefox-i18n-61.0/nb-NO.xpi) = 483524 +SHA256 (xpi/firefox-i18n-61.0/ne-NP.xpi) = 9b7b57cb0f43f079f1c8cfe9512c9008d224685dafe2ea6a31315f9e65f58cfa +SIZE (xpi/firefox-i18n-61.0/ne-NP.xpi) = 523515 +SHA256 (xpi/firefox-i18n-61.0/nl.xpi) = 732f670ac95ccea8e723d2ffe45bba10061f577dbf6b0799a54641cd5118def6 +SIZE (xpi/firefox-i18n-61.0/nl.xpi) = 489007 +SHA256 (xpi/firefox-i18n-61.0/nn-NO.xpi) = ca0a7a0134a00bb4c2a7148ed4345f1a631151fed763e25c98507189765eb1dd +SIZE (xpi/firefox-i18n-61.0/nn-NO.xpi) = 483559 +SHA256 (xpi/firefox-i18n-61.0/oc.xpi) = f8430be11eb4327899ef6d512f1bc7fa98ba2db9bb2c042bc2f2cda7ca820924 +SIZE (xpi/firefox-i18n-61.0/oc.xpi) = 499696 +SHA256 (xpi/firefox-i18n-61.0/or.xpi) = 815d53ba74a731711dd851485844c64ab06c76ebb845f593dc915e34b6cd0ec9 +SIZE (xpi/firefox-i18n-61.0/or.xpi) = 508184 +SHA256 (xpi/firefox-i18n-61.0/pa-IN.xpi) = 26fe1b7f2338644ecacc4fa79b58c9695f81268e436927b9632eb3b98fb6b2a7 +SIZE (xpi/firefox-i18n-61.0/pa-IN.xpi) = 524502 +SHA256 (xpi/firefox-i18n-61.0/pl.xpi) = 42774f47652e3b8da96aac130d0b4d7ab4e47d080d1931fa0286ad24e6de7fdd +SIZE (xpi/firefox-i18n-61.0/pl.xpi) = 390903 +SHA256 (xpi/firefox-i18n-61.0/pt-BR.xpi) = bc6b9aa57bdf0e87add7b07215fc9f60c811c9a3063c25b030daebc802b76448 +SIZE (xpi/firefox-i18n-61.0/pt-BR.xpi) = 486001 +SHA256 (xpi/firefox-i18n-61.0/pt-PT.xpi) = a8e55d09ba0049dd83e80307ad6529d4e1e829b7f4e13af009bafa8058340e0b +SIZE (xpi/firefox-i18n-61.0/pt-PT.xpi) = 492103 +SHA256 (xpi/firefox-i18n-61.0/rm.xpi) = 55566c7c5bcb72192033153e41dd046fbe58460d2791240b0d7e84c7c021b3e7 +SIZE (xpi/firefox-i18n-61.0/rm.xpi) = 483850 +SHA256 (xpi/firefox-i18n-61.0/ro.xpi) = 663d073e8e9a1fffe74db40ae3a4c1d8960bc9f70b752c6659c40bb0efd53a0e +SIZE (xpi/firefox-i18n-61.0/ro.xpi) = 486750 +SHA256 (xpi/firefox-i18n-61.0/ru.xpi) = 5914d162d9bc0973268035defa38b2720d94961e0f2abc919964d8e6b6f3cc03 +SIZE (xpi/firefox-i18n-61.0/ru.xpi) = 561624 +SHA256 (xpi/firefox-i18n-61.0/si.xpi) = 3a46f3546f82d97bfe31e95068c827781f0e0d88ee1e083ae87266f722e1fae3 +SIZE (xpi/firefox-i18n-61.0/si.xpi) = 511505 +SHA256 (xpi/firefox-i18n-61.0/sk.xpi) = cf9fd3be1bcbbab92982013f96f8a0bec42f80609f9a4620f550ad7f47c182e0 +SIZE (xpi/firefox-i18n-61.0/sk.xpi) = 514224 +SHA256 (xpi/firefox-i18n-61.0/sl.xpi) = 1ca4a44c6e96e7d7359edebf35cf22808acf45bea206e0d48c834bcc5b391b75 +SIZE (xpi/firefox-i18n-61.0/sl.xpi) = 490321 +SHA256 (xpi/firefox-i18n-61.0/son.xpi) = 8766423e4748facd423ec7026a0f7aee3917ec86ddcecd149e069f9c14ea1990 +SIZE (xpi/firefox-i18n-61.0/son.xpi) = 468634 +SHA256 (xpi/firefox-i18n-61.0/sq.xpi) = 67309f9cc1841b3e9108d7975385127724450ca6c7d79b787c68393d24f4fb72 +SIZE (xpi/firefox-i18n-61.0/sq.xpi) = 496574 +SHA256 (xpi/firefox-i18n-61.0/sr.xpi) = 56cb274163b620e7e662ebe2936c5540dc17ab59038178d887cd36b673b617be +SIZE (xpi/firefox-i18n-61.0/sr.xpi) = 519557 +SHA256 (xpi/firefox-i18n-61.0/sv-SE.xpi) = be9d00b7bc5cd208e30816e3cee6cdfce61b2d9e438a2ae0d11b0583ab5104f6 +SIZE (xpi/firefox-i18n-61.0/sv-SE.xpi) = 491597 +SHA256 (xpi/firefox-i18n-61.0/ta.xpi) = 490ec1b7b8978c07e442e9e57fa3a1ad7d453d670ab5cd2968077cf1c0b7c325 +SIZE (xpi/firefox-i18n-61.0/ta.xpi) = 541874 +SHA256 (xpi/firefox-i18n-61.0/te.xpi) = 7e893fcee8c031a035f8dcd5cdb903b946f83aa5c577f1cb0911fff9753a12ad +SIZE (xpi/firefox-i18n-61.0/te.xpi) = 558639 +SHA256 (xpi/firefox-i18n-61.0/th.xpi) = 86b389a1de8921b158b6bc3fcb090cc1917c07205014089c9ee12a43bccadb08 +SIZE (xpi/firefox-i18n-61.0/th.xpi) = 531116 +SHA256 (xpi/firefox-i18n-61.0/tr.xpi) = e4e2c85df418ce35abb5de49d18abea8f923440c06a88efa5c0e61ffdf7b59da +SIZE (xpi/firefox-i18n-61.0/tr.xpi) = 498510 +SHA256 (xpi/firefox-i18n-61.0/uk.xpi) = a45318b8c3905e0357f6f222f861f14c69b9f9f496ffe3bd99d1b289f97d7f5c +SIZE (xpi/firefox-i18n-61.0/uk.xpi) = 553597 +SHA256 (xpi/firefox-i18n-61.0/ur.xpi) = 6986874021a8e79f46db9695faccf53d99c939ca6d3ea22ec35a07c973836fa8 +SIZE (xpi/firefox-i18n-61.0/ur.xpi) = 531618 +SHA256 (xpi/firefox-i18n-61.0/uz.xpi) = 3dcfb7f9fb9e90578d0c5c56b6494664bcd16da378a81a30443bbe9297434bb6 +SIZE (xpi/firefox-i18n-61.0/uz.xpi) = 482052 +SHA256 (xpi/firefox-i18n-61.0/vi.xpi) = 27f856dd839b20a05b66c37653be78c6bb7a461bba3f25340ad6d007a9500211 +SIZE (xpi/firefox-i18n-61.0/vi.xpi) = 500411 +SHA256 (xpi/firefox-i18n-61.0/xh.xpi) = 553074aefd3b5c747246683754a3f3586734d61654211c6111dc1cc65a7b2793 +SIZE (xpi/firefox-i18n-61.0/xh.xpi) = 481790 +SHA256 (xpi/firefox-i18n-61.0/zh-CN.xpi) = 47de4388fbb67520d95b86dd1daaebb4a3f7fb7877c703c1d4c5b4d08d61614f +SIZE (xpi/firefox-i18n-61.0/zh-CN.xpi) = 514399 +SHA256 (xpi/firefox-i18n-61.0/zh-TW.xpi) = 3b33af36adb7c7c070d066b81203e0ada3bef549db74c06e0d41301a47a2448f +SIZE (xpi/firefox-i18n-61.0/zh-TW.xpi) = 513546