Hebrew OT translation to Greek NT Hebrews 10:5 (ESV) states 


  Consequently, when Christ came into the world, he said,
      “Sacrifices and offerings you have not desired,
      but a body have you prepared for me;


This is an apparent reference to Psalm 40:6:


  In sacrifice and offering you have not delighted,
  but you have given me an open ear.


The footnote in my Bible on the Psalm mentions that literally this verse would be "ears you have dug for me".

I can understand an idiomatic translation; digging out ones ears is a Hebrew idiom roughly equivalent to having an "open ear" in English. But moving to "a body have you prepared for me" seems to be far beyond translating an idiom.

I looked at one of these sites that has a whole bunch (17) of translations; all translate that phrase in Hebrews fairly similar (mentioning preparation of a body). In the case of the Psalm, likewise, they all seem to mention something about ears. Whether "you have made me listen" or "you have pierced my ears" or one of the ESV translations I mentioned above.

So most translations seem to point to the translation from Hebrew to Greek is a rather liberal one. Am I understanding this correctly? And if so, how does digging out an ear relate to preparing a body?
The footnote for Hebrews 10:5-7 in the New American Bible mentions the following:


  A passage from Ps 40:7–9 is placed in the mouth of the Son at his incarnation. As usual, the author follows the Septuagint text. There is a notable difference in Heb 10:5 (Ps 40:6), where the Masoretic text reads “ears you have dug for me” (“ears open to obedience you gave me,” NAB), but most Septuagint manuscripts have “a body you prepared for me,” a reading obviously more suited to the interpretation of Hebrews.


The original and ostensibly mistranslated passage appears in the Septuagint. Why this translation appears in the Septuagint and not the Masorectic text is something of a mystery, but there are a few different conjectures that discount the Masorectic text's translation. Notably by Benjamin Kennicott as descibed by Adam Clarke:


  Dr. Kennicott has a very ingenious conjecture here: he supposes that the Septuagint and apostle express the meaning of the words as they stood in the copy from which the Greek translation was made; and that the present Hebrew text is corrupted in the word oznayim, ears, which has been written through carelessness for az gevah, THEN A BODY. The first syllable THEN, is the same in both; and the latter which joined to makes oznayim, might have been easily mistaken for gevah, BODY; nun, being very like gimel; yod, like vau; and he, like final mem; especially if the line on which the letters were written in the MS. happened to be blacker than ordinary, which has often been a cause of mistake, it might have been easily taken for the under stroke of the mem, and thus give rise to a corrupt reading: add to this the root carah, signifies as well to prepare as to open, bore, Septuagint, and followed by the apostle, must have read the text thus: az gevah caritha li, σωμαδεκατηρτισωμοι, then a body thou hast prepared me: thus the Hebrew text, the version of the Septuagint, and the apostle, will agree in what is known to be an indisputable fact in Christianity, namely, that Christ was incarnated for the sin of the world.


That is, Psalms 40 in the Masorectic text has a transcription error that was not present in the translations upon which the Septaguint manuscripts were based.